act_api.c 23.9 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * net/sched/act_api.c	Packet action API.
 *
 *		This program is free software; you can redistribute it and/or
 *		modify it under the terms of the GNU General Public License
 *		as published by the Free Software Foundation; either version
 *		2 of the License, or (at your option) any later version.
 *
 * Author:	Jamal Hadi Salim
 *
 *
 */

#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/errno.h>
18
#include <linux/slab.h>
L
Linus Torvalds 已提交
19 20 21
#include <linux/skbuff.h>
#include <linux/init.h>
#include <linux/kmod.h>
22
#include <linux/err.h>
23
#include <linux/module.h>
24 25
#include <net/net_namespace.h>
#include <net/sock.h>
L
Linus Torvalds 已提交
26 27
#include <net/sch_generic.h>
#include <net/act_api.h>
28
#include <net/netlink.h>
L
Linus Torvalds 已提交
29

30 31
static void free_tcf(struct rcu_head *head)
{
32
	struct tc_action *p = container_of(head, struct tc_action, tcfa_rcu);
33 34 35 36 37 38

	free_percpu(p->cpu_bstats);
	free_percpu(p->cpu_qstats);
	kfree(p);
}

39
static void tcf_hash_destroy(struct tcf_hashinfo *hinfo, struct tc_action *p)
40
{
41
	spin_lock_bh(&hinfo->lock);
42
	hlist_del(&p->tcfa_head);
43
	spin_unlock_bh(&hinfo->lock);
44 45
	gen_kill_estimator(&p->tcfa_bstats,
			   &p->tcfa_rate_est);
46
	/*
47
	 * gen_estimator est_timer() might access p->tcfa_lock
48 49
	 * or bstats, wait a RCU grace period before freeing p
	 */
50
	call_rcu(&p->tcfa_rcu, free_tcf);
51 52
}

53
int __tcf_hash_release(struct tc_action *p, bool bind, bool strict)
54 55 56 57 58
{
	int ret = 0;

	if (p) {
		if (bind)
59 60
			p->tcfa_bindcnt--;
		else if (strict && p->tcfa_bindcnt > 0)
61
			return -EPERM;
62

63 64 65 66 67
		p->tcfa_refcnt--;
		if (p->tcfa_bindcnt <= 0 && p->tcfa_refcnt <= 0) {
			if (p->ops->cleanup)
				p->ops->cleanup(p, bind);
			tcf_hash_destroy(p->hinfo, p);
68
			ret = ACT_P_DELETED;
69 70
		}
	}
71

72 73
	return ret;
}
74
EXPORT_SYMBOL(__tcf_hash_release);
75

76
static int tcf_dump_walker(struct tcf_hashinfo *hinfo, struct sk_buff *skb,
77
			   struct netlink_callback *cb)
78
{
E
Eric Dumazet 已提交
79
	int err = 0, index = -1, i = 0, s_i = 0, n_i = 0;
80
	struct nlattr *nest;
81

82
	spin_lock_bh(&hinfo->lock);
83 84 85 86

	s_i = cb->args[0];

	for (i = 0; i < (hinfo->hmask + 1); i++) {
87
		struct hlist_head *head;
88
		struct tc_action *p;
89

90
		head = &hinfo->htab[tcf_hash(i, hinfo->hmask)];
91

92
		hlist_for_each_entry_rcu(p, head, tcfa_head) {
93 94 95
			index++;
			if (index < s_i)
				continue;
96

97
			nest = nla_nest_start(skb, n_i);
98 99
			if (nest == NULL)
				goto nla_put_failure;
100
			err = tcf_action_dump_1(skb, p, 0, 0);
101 102
			if (err < 0) {
				index--;
103
				nlmsg_trim(skb, nest);
104 105
				goto done;
			}
106
			nla_nest_end(skb, nest);
107 108 109 110 111 112
			n_i++;
			if (n_i >= TCA_ACT_MAX_PRIO)
				goto done;
		}
	}
done:
113
	spin_unlock_bh(&hinfo->lock);
114 115 116 117
	if (n_i)
		cb->args[0] += n_i;
	return n_i;

118
nla_put_failure:
119
	nla_nest_cancel(skb, nest);
120 121 122
	goto done;
}

123
static int tcf_del_walker(struct tcf_hashinfo *hinfo, struct sk_buff *skb,
124
			  const struct tc_action_ops *ops)
125
{
126
	struct nlattr *nest;
E
Eric Dumazet 已提交
127
	int i = 0, n_i = 0;
128
	int ret = -EINVAL;
129

130
	nest = nla_nest_start(skb, 0);
131 132
	if (nest == NULL)
		goto nla_put_failure;
133
	if (nla_put_string(skb, TCA_KIND, ops->kind))
134
		goto nla_put_failure;
135
	for (i = 0; i < (hinfo->hmask + 1); i++) {
136 137
		struct hlist_head *head;
		struct hlist_node *n;
138
		struct tc_action *p;
139

140
		head = &hinfo->htab[tcf_hash(i, hinfo->hmask)];
141 142
		hlist_for_each_entry_safe(p, n, head, tcfa_head) {
			ret = __tcf_hash_release(p, false, true);
143
			if (ret == ACT_P_DELETED) {
144
				module_put(p->ops->owner);
145
				n_i++;
146 147
			} else if (ret < 0)
				goto nla_put_failure;
148 149
		}
	}
150 151
	if (nla_put_u32(skb, TCA_FCNT, n_i))
		goto nla_put_failure;
152
	nla_nest_end(skb, nest);
153 154

	return n_i;
155
nla_put_failure:
156
	nla_nest_cancel(skb, nest);
157
	return ret;
158 159
}

160 161
int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb,
		       struct netlink_callback *cb, int type,
162
		       const struct tc_action_ops *ops)
163
{
164 165
	struct tcf_hashinfo *hinfo = tn->hinfo;

166
	if (type == RTM_DELACTION) {
167
		return tcf_del_walker(hinfo, skb, ops);
168
	} else if (type == RTM_GETACTION) {
169
		return tcf_dump_walker(hinfo, skb, cb);
170
	} else {
171
		WARN(1, "tcf_generic_walker: unknown action %d\n", type);
172 173 174
		return -EINVAL;
	}
}
175
EXPORT_SYMBOL(tcf_generic_walker);
176

177
static struct tc_action *tcf_hash_lookup(u32 index, struct tcf_hashinfo *hinfo)
178
{
179
	struct tc_action *p = NULL;
180
	struct hlist_head *head;
181

182 183
	spin_lock_bh(&hinfo->lock);
	head = &hinfo->htab[tcf_hash(index, hinfo->hmask)];
184 185
	hlist_for_each_entry_rcu(p, head, tcfa_head)
		if (p->tcfa_index == index)
186
			break;
187
	spin_unlock_bh(&hinfo->lock);
188 189 190 191

	return p;
}

192
u32 tcf_hash_new_index(struct tc_action_net *tn)
193
{
194
	struct tcf_hashinfo *hinfo = tn->hinfo;
195
	u32 val = hinfo->index;
196 197 198 199 200 201

	do {
		if (++val == 0)
			val = 1;
	} while (tcf_hash_lookup(val, hinfo));

202
	hinfo->index = val;
203
	return val;
204 205 206
}
EXPORT_SYMBOL(tcf_hash_new_index);

207
int tcf_hash_search(struct tc_action_net *tn, struct tc_action **a, u32 index)
208
{
209
	struct tcf_hashinfo *hinfo = tn->hinfo;
210
	struct tc_action *p = tcf_hash_lookup(index, hinfo);
211 212

	if (p) {
213
		*a = p;
214 215 216 217
		return 1;
	}
	return 0;
}
218
EXPORT_SYMBOL(tcf_hash_search);
219

220
bool tcf_hash_check(struct tc_action_net *tn, u32 index, struct tc_action **a,
221
		    int bind)
222
{
223
	struct tcf_hashinfo *hinfo = tn->hinfo;
224 225
	struct tc_action *p = NULL;

226
	if (index && (p = tcf_hash_lookup(index, hinfo)) != NULL) {
227
		if (bind)
228 229 230
			p->tcfa_bindcnt++;
		p->tcfa_refcnt++;
		*a = p;
231
		return true;
232
	}
233
	return false;
234 235 236
}
EXPORT_SYMBOL(tcf_hash_check);

237 238 239
void tcf_hash_cleanup(struct tc_action *a, struct nlattr *est)
{
	if (est)
240 241 242
		gen_kill_estimator(&a->tcfa_bstats,
				   &a->tcfa_rate_est);
	call_rcu(&a->tcfa_rcu, free_tcf);
243 244 245
}
EXPORT_SYMBOL(tcf_hash_cleanup);

246
int tcf_hash_create(struct tc_action_net *tn, u32 index, struct nlattr *est,
247 248
		    struct tc_action **a, const struct tc_action_ops *ops,
		    int bind, bool cpustats)
249
{
250
	struct tc_action *p = kzalloc(ops->size, GFP_KERNEL);
251
	struct tcf_hashinfo *hinfo = tn->hinfo;
252
	int err = -ENOMEM;
253 254

	if (unlikely(!p))
255
		return -ENOMEM;
256
	p->tcfa_refcnt = 1;
257
	if (bind)
258
		p->tcfa_bindcnt = 1;
259

260 261 262 263 264 265 266 267 268 269 270 271 272 273
	if (cpustats) {
		p->cpu_bstats = netdev_alloc_pcpu_stats(struct gnet_stats_basic_cpu);
		if (!p->cpu_bstats) {
err1:
			kfree(p);
			return err;
		}
		p->cpu_qstats = alloc_percpu(struct gnet_stats_queue);
		if (!p->cpu_qstats) {
err2:
			free_percpu(p->cpu_bstats);
			goto err1;
		}
	}
274 275 276 277 278 279
	spin_lock_init(&p->tcfa_lock);
	INIT_HLIST_NODE(&p->tcfa_head);
	p->tcfa_index = index ? index : tcf_hash_new_index(tn);
	p->tcfa_tm.install = jiffies;
	p->tcfa_tm.lastuse = jiffies;
	p->tcfa_tm.firstuse = 0;
280
	if (est) {
281 282 283
		err = gen_new_estimator(&p->tcfa_bstats, p->cpu_bstats,
					&p->tcfa_rate_est,
					&p->tcfa_lock, NULL, est);
284
		if (err) {
285 286
			free_percpu(p->cpu_qstats);
			goto err2;
287 288 289
		}
	}

290 291 292 293
	p->hinfo = hinfo;
	p->ops = ops;
	INIT_LIST_HEAD(&p->list);
	*a = p;
294
	return 0;
295 296 297
}
EXPORT_SYMBOL(tcf_hash_create);

298
void tcf_hash_insert(struct tc_action_net *tn, struct tc_action *a)
299
{
300
	struct tcf_hashinfo *hinfo = tn->hinfo;
301
	unsigned int h = tcf_hash(a->tcfa_index, hinfo->hmask);
302

303
	spin_lock_bh(&hinfo->lock);
304
	hlist_add_head(&a->tcfa_head, &hinfo->htab[h]);
305
	spin_unlock_bh(&hinfo->lock);
306 307
}
EXPORT_SYMBOL(tcf_hash_insert);
L
Linus Torvalds 已提交
308

309 310
void tcf_hashinfo_destroy(const struct tc_action_ops *ops,
			  struct tcf_hashinfo *hinfo)
311 312 313 314
{
	int i;

	for (i = 0; i < hinfo->hmask + 1; i++) {
315
		struct tc_action *p;
316 317
		struct hlist_node *n;

318
		hlist_for_each_entry_safe(p, n, &hinfo->htab[i], tcfa_head) {
319 320
			int ret;

321
			ret = __tcf_hash_release(p, false, true);
322 323 324 325 326 327 328 329
			if (ret == ACT_P_DELETED)
				module_put(ops->owner);
			else if (ret < 0)
				return;
		}
	}
	kfree(hinfo->htab);
}
330
EXPORT_SYMBOL(tcf_hashinfo_destroy);
331

332
static LIST_HEAD(act_base);
L
Linus Torvalds 已提交
333 334
static DEFINE_RWLOCK(act_mod_lock);

335 336
int tcf_register_action(struct tc_action_ops *act,
			struct pernet_operations *ops)
L
Linus Torvalds 已提交
337
{
338
	struct tc_action_ops *a;
339
	int ret;
L
Linus Torvalds 已提交
340

341
	if (!act->act || !act->dump || !act->init || !act->walk || !act->lookup)
342 343
		return -EINVAL;

344 345 346 347 348 349 350 351
	/* We have to register pernet ops before making the action ops visible,
	 * otherwise tcf_action_init_1() could get a partially initialized
	 * netns.
	 */
	ret = register_pernet_subsys(ops);
	if (ret)
		return ret;

L
Linus Torvalds 已提交
352
	write_lock(&act_mod_lock);
353
	list_for_each_entry(a, &act_base, head) {
L
Linus Torvalds 已提交
354 355
		if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
			write_unlock(&act_mod_lock);
356
			unregister_pernet_subsys(ops);
L
Linus Torvalds 已提交
357 358 359
			return -EEXIST;
		}
	}
360
	list_add_tail(&act->head, &act_base);
L
Linus Torvalds 已提交
361
	write_unlock(&act_mod_lock);
362

L
Linus Torvalds 已提交
363 364
	return 0;
}
365
EXPORT_SYMBOL(tcf_register_action);
L
Linus Torvalds 已提交
366

367 368
int tcf_unregister_action(struct tc_action_ops *act,
			  struct pernet_operations *ops)
L
Linus Torvalds 已提交
369
{
370
	struct tc_action_ops *a;
L
Linus Torvalds 已提交
371 372 373
	int err = -ENOENT;

	write_lock(&act_mod_lock);
374 375 376 377
	list_for_each_entry(a, &act_base, head) {
		if (a == act) {
			list_del(&act->head);
			err = 0;
L
Linus Torvalds 已提交
378
			break;
379
		}
L
Linus Torvalds 已提交
380 381
	}
	write_unlock(&act_mod_lock);
382 383
	if (!err)
		unregister_pernet_subsys(ops);
L
Linus Torvalds 已提交
384 385
	return err;
}
386
EXPORT_SYMBOL(tcf_unregister_action);
L
Linus Torvalds 已提交
387 388 389 390

/* lookup by name */
static struct tc_action_ops *tc_lookup_action_n(char *kind)
{
391
	struct tc_action_ops *a, *res = NULL;
L
Linus Torvalds 已提交
392 393 394

	if (kind) {
		read_lock(&act_mod_lock);
395
		list_for_each_entry(a, &act_base, head) {
L
Linus Torvalds 已提交
396
			if (strcmp(kind, a->kind) == 0) {
397 398
				if (try_module_get(a->owner))
					res = a;
L
Linus Torvalds 已提交
399 400 401 402 403
				break;
			}
		}
		read_unlock(&act_mod_lock);
	}
404
	return res;
L
Linus Torvalds 已提交
405 406
}

407 408
/* lookup by nlattr */
static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
L
Linus Torvalds 已提交
409
{
410
	struct tc_action_ops *a, *res = NULL;
L
Linus Torvalds 已提交
411 412 413

	if (kind) {
		read_lock(&act_mod_lock);
414
		list_for_each_entry(a, &act_base, head) {
415
			if (nla_strcmp(kind, a->kind) == 0) {
416 417
				if (try_module_get(a->owner))
					res = a;
L
Linus Torvalds 已提交
418 419 420 421 422
				break;
			}
		}
		read_unlock(&act_mod_lock);
	}
423
	return res;
L
Linus Torvalds 已提交
424 425
}

426 427
int tcf_action_exec(struct sk_buff *skb, struct tc_action **actions,
		    int nr_actions, struct tcf_result *res)
L
Linus Torvalds 已提交
428
{
429
	int ret = -1, i;
L
Linus Torvalds 已提交
430 431 432 433 434 435

	if (skb->tc_verd & TC_NCLS) {
		skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
		ret = TC_ACT_OK;
		goto exec_done;
	}
436 437 438
	for (i = 0; i < nr_actions; i++) {
		const struct tc_action *a = actions[i];

L
Linus Torvalds 已提交
439
repeat:
440 441 442 443 444
		ret = a->ops->act(skb, a, res);
		if (ret == TC_ACT_REPEAT)
			goto repeat;	/* we need a ttl - JHS */
		if (ret != TC_ACT_PIPE)
			goto exec_done;
L
Linus Torvalds 已提交
445 446 447 448
	}
exec_done:
	return ret;
}
449
EXPORT_SYMBOL(tcf_action_exec);
L
Linus Torvalds 已提交
450

451
int tcf_action_destroy(struct list_head *actions, int bind)
L
Linus Torvalds 已提交
452
{
453
	struct tc_action *a, *tmp;
454
	int ret = 0;
L
Linus Torvalds 已提交
455

456
	list_for_each_entry_safe(a, tmp, actions, list) {
457
		ret = __tcf_hash_release(a, bind, true);
458
		if (ret == ACT_P_DELETED)
459
			module_put(a->ops->owner);
460 461
		else if (ret < 0)
			return ret;
L
Linus Torvalds 已提交
462
	}
463
	return ret;
L
Linus Torvalds 已提交
464 465 466 467 468 469 470 471 472 473 474 475
}

int
tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
{
	return a->ops->dump(skb, a, bind, ref);
}

int
tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
{
	int err = -EINVAL;
476
	unsigned char *b = skb_tail_pointer(skb);
477
	struct nlattr *nest;
L
Linus Torvalds 已提交
478

479 480
	if (nla_put_string(skb, TCA_KIND, a->ops->kind))
		goto nla_put_failure;
L
Linus Torvalds 已提交
481
	if (tcf_action_copy_stats(skb, a, 0))
482
		goto nla_put_failure;
483 484 485
	nest = nla_nest_start(skb, TCA_OPTIONS);
	if (nest == NULL)
		goto nla_put_failure;
E
Eric Dumazet 已提交
486 487
	err = tcf_action_dump_old(skb, a, bind, ref);
	if (err > 0) {
488
		nla_nest_end(skb, nest);
L
Linus Torvalds 已提交
489 490 491
		return err;
	}

492
nla_put_failure:
493
	nlmsg_trim(skb, b);
L
Linus Torvalds 已提交
494 495
	return -1;
}
496
EXPORT_SYMBOL(tcf_action_dump_1);
L
Linus Torvalds 已提交
497

498 499
int tcf_action_dump(struct sk_buff *skb, struct list_head *actions,
		    int bind, int ref)
L
Linus Torvalds 已提交
500 501 502
{
	struct tc_action *a;
	int err = -EINVAL;
503
	struct nlattr *nest;
L
Linus Torvalds 已提交
504

505
	list_for_each_entry(a, actions, list) {
506 507 508
		nest = nla_nest_start(skb, a->order);
		if (nest == NULL)
			goto nla_put_failure;
L
Linus Torvalds 已提交
509 510
		err = tcf_action_dump_1(skb, a, bind, ref);
		if (err < 0)
511
			goto errout;
512
		nla_nest_end(skb, nest);
L
Linus Torvalds 已提交
513 514 515 516
	}

	return 0;

517
nla_put_failure:
518 519
	err = -EINVAL;
errout:
520
	nla_nest_cancel(skb, nest);
521
	return err;
L
Linus Torvalds 已提交
522 523
}

524 525 526
struct tc_action *tcf_action_init_1(struct net *net, struct nlattr *nla,
				    struct nlattr *est, char *name, int ovr,
				    int bind)
L
Linus Torvalds 已提交
527 528 529 530
{
	struct tc_action *a;
	struct tc_action_ops *a_o;
	char act_name[IFNAMSIZ];
E
Eric Dumazet 已提交
531
	struct nlattr *tb[TCA_ACT_MAX + 1];
532
	struct nlattr *kind;
533
	int err;
L
Linus Torvalds 已提交
534 535

	if (name == NULL) {
536 537
		err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
		if (err < 0)
L
Linus Torvalds 已提交
538
			goto err_out;
539
		err = -EINVAL;
540
		kind = tb[TCA_ACT_KIND];
L
Linus Torvalds 已提交
541 542
		if (kind == NULL)
			goto err_out;
543
		if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
L
Linus Torvalds 已提交
544 545
			goto err_out;
	} else {
546
		err = -EINVAL;
L
Linus Torvalds 已提交
547 548 549 550 551 552
		if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
			goto err_out;
	}

	a_o = tc_lookup_action_n(act_name);
	if (a_o == NULL) {
553
#ifdef CONFIG_MODULES
L
Linus Torvalds 已提交
554
		rtnl_unlock();
555
		request_module("act_%s", act_name);
L
Linus Torvalds 已提交
556 557 558 559 560 561 562 563 564 565 566
		rtnl_lock();

		a_o = tc_lookup_action_n(act_name);

		/* We dropped the RTNL semaphore in order to
		 * perform the module load.  So, even if we
		 * succeeded in loading the module we have to
		 * tell the caller to replay the request.  We
		 * indicate this using -EAGAIN.
		 */
		if (a_o != NULL) {
567
			err = -EAGAIN;
L
Linus Torvalds 已提交
568 569 570
			goto err_mod;
		}
#endif
571
		err = -ENOENT;
L
Linus Torvalds 已提交
572 573 574 575 576
		goto err_out;
	}

	/* backward compatibility for policer */
	if (name == NULL)
577
		err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, &a, ovr, bind);
L
Linus Torvalds 已提交
578
	else
579
		err = a_o->init(net, nla, est, &a, ovr, bind);
580
	if (err < 0)
581
		goto err_mod;
L
Linus Torvalds 已提交
582 583

	/* module count goes up only when brand new policy is created
E
Eric Dumazet 已提交
584 585 586
	 * if it exists and is only bound to in a_o->init() then
	 * ACT_P_CREATED is not returned (a zero is).
	 */
587
	if (err != ACT_P_CREATED)
L
Linus Torvalds 已提交
588 589 590 591 592 593 594
		module_put(a_o->owner);

	return a;

err_mod:
	module_put(a_o->owner);
err_out:
595
	return ERR_PTR(err);
L
Linus Torvalds 已提交
596 597
}

598 599 600 601 602 603 604 605 606 607 608
static void cleanup_a(struct list_head *actions, int ovr)
{
	struct tc_action *a;

	if (!ovr)
		return;

	list_for_each_entry(a, actions, list)
		a->tcfa_refcnt--;
}

J
Jamal Hadi Salim 已提交
609 610
int tcf_action_init(struct net *net, struct nlattr *nla, struct nlattr *est,
		    char *name, int ovr, int bind, struct list_head *actions)
L
Linus Torvalds 已提交
611
{
E
Eric Dumazet 已提交
612
	struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
613
	struct tc_action *act;
614
	int err;
L
Linus Torvalds 已提交
615 616
	int i;

617 618
	err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
	if (err < 0)
619
		return err;
L
Linus Torvalds 已提交
620

621
	for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
622
		act = tcf_action_init_1(net, tb[i], est, name, ovr, bind);
623 624
		if (IS_ERR(act)) {
			err = PTR_ERR(act);
L
Linus Torvalds 已提交
625
			goto err;
626
		}
627
		act->order = i;
628 629
		if (ovr)
			act->tcfa_refcnt++;
630
		list_add_tail(&act->list, actions);
L
Linus Torvalds 已提交
631
	}
632 633 634 635 636

	/* Remove the temp refcnt which was necessary to protect against
	 * destroying an existing action which was being replaced
	 */
	cleanup_a(actions, ovr);
637
	return 0;
L
Linus Torvalds 已提交
638 639

err:
640 641
	tcf_action_destroy(actions, bind);
	return err;
L
Linus Torvalds 已提交
642 643
}

644
int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *p,
L
Linus Torvalds 已提交
645 646 647 648
			  int compat_mode)
{
	int err = 0;
	struct gnet_dump d;
649

650
	if (p == NULL)
L
Linus Torvalds 已提交
651 652 653
		goto errout;

	/* compat_mode being true specifies a call that is supposed
654
	 * to add additional backward compatibility statistic TLVs.
L
Linus Torvalds 已提交
655 656
	 */
	if (compat_mode) {
657
		if (p->type == TCA_OLD_COMPAT)
L
Linus Torvalds 已提交
658
			err = gnet_stats_start_copy_compat(skb, 0,
659 660
							   TCA_STATS,
							   TCA_XSTATS,
661
							   &p->tcfa_lock, &d,
662
							   TCA_PAD);
L
Linus Torvalds 已提交
663 664 665 666
		else
			return 0;
	} else
		err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
667
					    &p->tcfa_lock, &d, TCA_ACT_PAD);
L
Linus Torvalds 已提交
668 669 670 671

	if (err < 0)
		goto errout;

672 673 674
	if (gnet_stats_copy_basic(NULL, &d, p->cpu_bstats, &p->tcfa_bstats) < 0 ||
	    gnet_stats_copy_rate_est(&d, &p->tcfa_bstats,
				     &p->tcfa_rate_est) < 0 ||
675
	    gnet_stats_copy_queue(&d, p->cpu_qstats,
676 677
				  &p->tcfa_qstats,
				  p->tcfa_qstats.qlen) < 0)
L
Linus Torvalds 已提交
678 679 680 681 682 683 684 685 686 687 688
		goto errout;

	if (gnet_stats_finish_copy(&d) < 0)
		goto errout;

	return 0;

errout:
	return -1;
}

689 690 691
static int tca_get_fill(struct sk_buff *skb, struct list_head *actions,
			u32 portid, u32 seq, u16 flags, int event, int bind,
			int ref)
L
Linus Torvalds 已提交
692 693 694
{
	struct tcamsg *t;
	struct nlmsghdr *nlh;
695
	unsigned char *b = skb_tail_pointer(skb);
696
	struct nlattr *nest;
L
Linus Torvalds 已提交
697

698
	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
699 700 701
	if (!nlh)
		goto out_nlmsg_trim;
	t = nlmsg_data(nlh);
L
Linus Torvalds 已提交
702
	t->tca_family = AF_UNSPEC;
703 704
	t->tca__pad1 = 0;
	t->tca__pad2 = 0;
705

706 707
	nest = nla_nest_start(skb, TCA_ACT_TAB);
	if (nest == NULL)
708
		goto out_nlmsg_trim;
L
Linus Torvalds 已提交
709

710
	if (tcf_action_dump(skb, actions, bind, ref) < 0)
711
		goto out_nlmsg_trim;
L
Linus Torvalds 已提交
712

713
	nla_nest_end(skb, nest);
714

715
	nlh->nlmsg_len = skb_tail_pointer(skb) - b;
L
Linus Torvalds 已提交
716 717
	return skb->len;

718
out_nlmsg_trim:
719
	nlmsg_trim(skb, b);
L
Linus Torvalds 已提交
720 721 722 723
	return -1;
}

static int
724
act_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
725
	       struct list_head *actions, int event)
L
Linus Torvalds 已提交
726 727 728 729 730 731
{
	struct sk_buff *skb;

	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
	if (!skb)
		return -ENOBUFS;
732 733
	if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event,
			 0, 0) <= 0) {
L
Linus Torvalds 已提交
734 735 736
		kfree_skb(skb);
		return -EINVAL;
	}
737

738
	return rtnl_unicast(skb, net, portid);
L
Linus Torvalds 已提交
739 740
}

741 742
static struct tc_action *tcf_action_get_1(struct net *net, struct nlattr *nla,
					  struct nlmsghdr *n, u32 portid)
L
Linus Torvalds 已提交
743
{
E
Eric Dumazet 已提交
744
	struct nlattr *tb[TCA_ACT_MAX + 1];
745
	const struct tc_action_ops *ops;
L
Linus Torvalds 已提交
746 747
	struct tc_action *a;
	int index;
748
	int err;
L
Linus Torvalds 已提交
749

750 751
	err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
	if (err < 0)
752
		goto err_out;
L
Linus Torvalds 已提交
753

754
	err = -EINVAL;
755 756
	if (tb[TCA_ACT_INDEX] == NULL ||
	    nla_len(tb[TCA_ACT_INDEX]) < sizeof(index))
757
		goto err_out;
758
	index = nla_get_u32(tb[TCA_ACT_INDEX]);
L
Linus Torvalds 已提交
759

760
	err = -EINVAL;
761 762 763
	ops = tc_lookup_action(tb[TCA_ACT_KIND]);
	if (!ops) /* could happen in batch of actions */
		goto err_out;
764
	err = -ENOENT;
765
	if (ops->lookup(net, &a, index) == 0)
L
Linus Torvalds 已提交
766 767
		goto err_mod;

768
	module_put(ops->owner);
L
Linus Torvalds 已提交
769
	return a;
770

L
Linus Torvalds 已提交
771
err_mod:
772
	module_put(ops->owner);
773 774
err_out:
	return ERR_PTR(err);
L
Linus Torvalds 已提交
775 776
}

777
static int tca_action_flush(struct net *net, struct nlattr *nla,
778
			    struct nlmsghdr *n, u32 portid)
L
Linus Torvalds 已提交
779 780 781 782 783 784
{
	struct sk_buff *skb;
	unsigned char *b;
	struct nlmsghdr *nlh;
	struct tcamsg *t;
	struct netlink_callback dcb;
785
	struct nlattr *nest;
E
Eric Dumazet 已提交
786
	struct nlattr *tb[TCA_ACT_MAX + 1];
787
	const struct tc_action_ops *ops;
788
	struct nlattr *kind;
789
	int err = -ENOMEM;
L
Linus Torvalds 已提交
790 791 792

	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
	if (!skb) {
793
		pr_debug("tca_action_flush: failed skb alloc\n");
794
		return err;
L
Linus Torvalds 已提交
795 796
	}

797
	b = skb_tail_pointer(skb);
L
Linus Torvalds 已提交
798

799 800
	err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
	if (err < 0)
L
Linus Torvalds 已提交
801 802
		goto err_out;

803
	err = -EINVAL;
804
	kind = tb[TCA_ACT_KIND];
805 806
	ops = tc_lookup_action(kind);
	if (!ops) /*some idjot trying to flush unknown action */
L
Linus Torvalds 已提交
807 808
		goto err_out;

809 810
	nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION,
			sizeof(*t), 0);
811 812 813
	if (!nlh)
		goto out_module_put;
	t = nlmsg_data(nlh);
L
Linus Torvalds 已提交
814
	t->tca_family = AF_UNSPEC;
815 816
	t->tca__pad1 = 0;
	t->tca__pad2 = 0;
L
Linus Torvalds 已提交
817

818 819
	nest = nla_nest_start(skb, TCA_ACT_TAB);
	if (nest == NULL)
820
		goto out_module_put;
L
Linus Torvalds 已提交
821

822
	err = ops->walk(net, skb, &dcb, RTM_DELACTION, ops);
L
Linus Torvalds 已提交
823
	if (err < 0)
824
		goto out_module_put;
825 826
	if (err == 0)
		goto noflush_out;
L
Linus Torvalds 已提交
827

828
	nla_nest_end(skb, nest);
L
Linus Torvalds 已提交
829

830
	nlh->nlmsg_len = skb_tail_pointer(skb) - b;
L
Linus Torvalds 已提交
831
	nlh->nlmsg_flags |= NLM_F_ROOT;
832
	module_put(ops->owner);
833
	err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
E
Eric Dumazet 已提交
834
			     n->nlmsg_flags & NLM_F_ECHO);
L
Linus Torvalds 已提交
835 836 837 838 839
	if (err > 0)
		return 0;

	return err;

840
out_module_put:
841
	module_put(ops->owner);
L
Linus Torvalds 已提交
842
err_out:
843
noflush_out:
L
Linus Torvalds 已提交
844 845 846 847
	kfree_skb(skb);
	return err;
}

848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865
static int
tcf_del_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
	       u32 portid)
{
	int ret;
	struct sk_buff *skb;

	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
	if (!skb)
		return -ENOBUFS;

	if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION,
			 0, 1) <= 0) {
		kfree_skb(skb);
		return -EINVAL;
	}

	/* now do the delete */
866 867 868 869 870
	ret = tcf_action_destroy(actions, 0);
	if (ret < 0) {
		kfree_skb(skb);
		return ret;
	}
871 872 873 874 875 876 877 878

	ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
			     n->nlmsg_flags & NLM_F_ECHO);
	if (ret > 0)
		return 0;
	return ret;
}

L
Linus Torvalds 已提交
879
static int
880
tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
881
	      u32 portid, int event)
L
Linus Torvalds 已提交
882
{
883
	int i, ret;
E
Eric Dumazet 已提交
884
	struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
885 886
	struct tc_action *act;
	LIST_HEAD(actions);
L
Linus Torvalds 已提交
887

888 889 890
	ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
	if (ret < 0)
		return ret;
L
Linus Torvalds 已提交
891

E
Eric Dumazet 已提交
892
	if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
893
		if (tb[1] != NULL)
894
			return tca_action_flush(net, tb[1], n, portid);
895 896
		else
			return -EINVAL;
L
Linus Torvalds 已提交
897 898
	}

899
	for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
900
		act = tcf_action_get_1(net, tb[i], n, portid);
901 902
		if (IS_ERR(act)) {
			ret = PTR_ERR(act);
L
Linus Torvalds 已提交
903
			goto err;
904
		}
905
		act->order = i;
906 907
		if (event == RTM_GETACTION)
			act->tcfa_refcnt++;
908
		list_add_tail(&act->list, &actions);
L
Linus Torvalds 已提交
909 910 911
	}

	if (event == RTM_GETACTION)
912
		ret = act_get_notify(net, portid, n, &actions, event);
L
Linus Torvalds 已提交
913
	else { /* delete */
914 915
		ret = tcf_del_notify(net, n, &actions, portid);
		if (ret)
L
Linus Torvalds 已提交
916 917 918 919
			goto err;
		return ret;
	}
err:
920
	tcf_action_destroy(&actions, 0);
L
Linus Torvalds 已提交
921 922 923
	return ret;
}

924 925 926
static int
tcf_add_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
	       u32 portid)
L
Linus Torvalds 已提交
927 928 929 930 931 932 933 934
{
	struct sk_buff *skb;
	int err = 0;

	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
	if (!skb)
		return -ENOBUFS;

935 936 937 938 939
	if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
			 RTM_NEWACTION, 0, 0) <= 0) {
		kfree_skb(skb);
		return -EINVAL;
	}
940

941 942
	err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
			     n->nlmsg_flags & NLM_F_ECHO);
L
Linus Torvalds 已提交
943 944 945 946 947
	if (err > 0)
		err = 0;
	return err;
}

J
Jamal Hadi Salim 已提交
948 949
static int tcf_action_add(struct net *net, struct nlattr *nla,
			  struct nlmsghdr *n, u32 portid, int ovr)
L
Linus Torvalds 已提交
950 951
{
	int ret = 0;
952
	LIST_HEAD(actions);
L
Linus Torvalds 已提交
953

954 955
	ret = tcf_action_init(net, nla, NULL, NULL, ovr, 0, &actions);
	if (ret)
956
		return ret;
L
Linus Torvalds 已提交
957

958
	return tcf_add_notify(net, n, &actions, portid);
L
Linus Torvalds 已提交
959 960
}

961
static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n)
L
Linus Torvalds 已提交
962
{
963
	struct net *net = sock_net(skb->sk);
964
	struct nlattr *tca[TCA_ACT_MAX + 1];
965
	u32 portid = skb ? NETLINK_CB(skb).portid : 0;
L
Linus Torvalds 已提交
966 967
	int ret = 0, ovr = 0;

968 969
	if ((n->nlmsg_type != RTM_GETACTION) &&
	    !netlink_capable(skb, CAP_NET_ADMIN))
970 971
		return -EPERM;

972 973 974 975 976
	ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ACT_MAX, NULL);
	if (ret < 0)
		return ret;

	if (tca[TCA_ACT_TAB] == NULL) {
977
		pr_notice("tc_ctl_action: received NO action attribs\n");
L
Linus Torvalds 已提交
978 979 980
		return -EINVAL;
	}

E
Eric Dumazet 已提交
981
	/* n->nlmsg_flags & NLM_F_CREATE */
L
Linus Torvalds 已提交
982 983 984
	switch (n->nlmsg_type) {
	case RTM_NEWACTION:
		/* we are going to assume all other flags
L
Lucas De Marchi 已提交
985
		 * imply create only if it doesn't exist
L
Linus Torvalds 已提交
986 987 988 989
		 * Note that CREATE | EXCL implies that
		 * but since we want avoid ambiguity (eg when flags
		 * is zero) then just set this
		 */
E
Eric Dumazet 已提交
990
		if (n->nlmsg_flags & NLM_F_REPLACE)
L
Linus Torvalds 已提交
991 992
			ovr = 1;
replay:
993
		ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr);
L
Linus Torvalds 已提交
994 995 996 997
		if (ret == -EAGAIN)
			goto replay;
		break;
	case RTM_DELACTION:
998
		ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
999
				    portid, RTM_DELACTION);
L
Linus Torvalds 已提交
1000 1001
		break;
	case RTM_GETACTION:
1002
		ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
1003
				    portid, RTM_GETACTION);
L
Linus Torvalds 已提交
1004 1005 1006 1007 1008 1009 1010 1011
		break;
	default:
		BUG();
	}

	return ret;
}

J
Jamal Hadi Salim 已提交
1012
static struct nlattr *find_dump_kind(const struct nlmsghdr *n)
L
Linus Torvalds 已提交
1013
{
E
Eric Dumazet 已提交
1014
	struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
1015 1016 1017
	struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
	struct nlattr *nla[TCAA_MAX + 1];
	struct nlattr *kind;
L
Linus Torvalds 已提交
1018

1019
	if (nlmsg_parse(n, sizeof(struct tcamsg), nla, TCAA_MAX, NULL) < 0)
L
Linus Torvalds 已提交
1020
		return NULL;
1021
	tb1 = nla[TCA_ACT_TAB];
L
Linus Torvalds 已提交
1022 1023 1024
	if (tb1 == NULL)
		return NULL;

1025 1026
	if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
		      NLMSG_ALIGN(nla_len(tb1)), NULL) < 0)
L
Linus Torvalds 已提交
1027 1028
		return NULL;

1029 1030 1031 1032
	if (tb[1] == NULL)
		return NULL;
	if (nla_parse(tb2, TCA_ACT_MAX, nla_data(tb[1]),
		      nla_len(tb[1]), NULL) < 0)
L
Linus Torvalds 已提交
1033
		return NULL;
1034
	kind = tb2[TCA_ACT_KIND];
L
Linus Torvalds 已提交
1035

1036
	return kind;
L
Linus Torvalds 已提交
1037 1038
}

J
Jamal Hadi Salim 已提交
1039
static int tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
L
Linus Torvalds 已提交
1040
{
1041
	struct net *net = sock_net(skb->sk);
L
Linus Torvalds 已提交
1042
	struct nlmsghdr *nlh;
1043
	unsigned char *b = skb_tail_pointer(skb);
1044
	struct nlattr *nest;
L
Linus Torvalds 已提交
1045 1046
	struct tc_action_ops *a_o;
	int ret = 0;
1047
	struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
1048
	struct nlattr *kind = find_dump_kind(cb->nlh);
L
Linus Torvalds 已提交
1049 1050

	if (kind == NULL) {
1051
		pr_info("tc_dump_action: action bad kind\n");
L
Linus Torvalds 已提交
1052 1053 1054
		return 0;
	}

1055
	a_o = tc_lookup_action(kind);
E
Eric Dumazet 已提交
1056
	if (a_o == NULL)
L
Linus Torvalds 已提交
1057 1058
		return 0;

1059
	nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
1060 1061 1062 1063
			cb->nlh->nlmsg_type, sizeof(*t), 0);
	if (!nlh)
		goto out_module_put;
	t = nlmsg_data(nlh);
L
Linus Torvalds 已提交
1064
	t->tca_family = AF_UNSPEC;
1065 1066
	t->tca__pad1 = 0;
	t->tca__pad2 = 0;
L
Linus Torvalds 已提交
1067

1068 1069
	nest = nla_nest_start(skb, TCA_ACT_TAB);
	if (nest == NULL)
1070
		goto out_module_put;
L
Linus Torvalds 已提交
1071

1072
	ret = a_o->walk(net, skb, cb, RTM_GETACTION, a_o);
L
Linus Torvalds 已提交
1073
	if (ret < 0)
1074
		goto out_module_put;
L
Linus Torvalds 已提交
1075 1076

	if (ret > 0) {
1077
		nla_nest_end(skb, nest);
L
Linus Torvalds 已提交
1078 1079
		ret = skb->len;
	} else
1080
		nlmsg_trim(skb, b);
L
Linus Torvalds 已提交
1081

1082
	nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1083
	if (NETLINK_CB(cb->skb).portid && ret)
L
Linus Torvalds 已提交
1084 1085 1086 1087
		nlh->nlmsg_flags |= NLM_F_MULTI;
	module_put(a_o->owner);
	return skb->len;

1088
out_module_put:
L
Linus Torvalds 已提交
1089
	module_put(a_o->owner);
1090
	nlmsg_trim(skb, b);
L
Linus Torvalds 已提交
1091 1092 1093 1094 1095
	return skb->len;
}

static int __init tc_action_init(void)
{
1096 1097 1098 1099
	rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, NULL);
	rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, NULL);
	rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
		      NULL);
L
Linus Torvalds 已提交
1100 1101 1102 1103 1104

	return 0;
}

subsys_initcall(tc_action_init);