act_api.c 24.6 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
#include <net/sch_generic.h>
27
#include <net/pkt_cls.h>
L
Linus Torvalds 已提交
28
#include <net/act_api.h>
29
#include <net/netlink.h>
L
Linus Torvalds 已提交
30

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

	free_percpu(p->cpu_bstats);
	free_percpu(p->cpu_qstats);
37 38 39 40 41 42

	if (p->act_cookie) {
		kfree(p->act_cookie->data);
		kfree(p->act_cookie);
	}

43 44 45
	kfree(p);
}

46
static void tcf_hash_destroy(struct tcf_hashinfo *hinfo, struct tc_action *p)
47
{
48
	spin_lock_bh(&hinfo->lock);
49
	hlist_del(&p->tcfa_head);
50
	spin_unlock_bh(&hinfo->lock);
51
	gen_kill_estimator(&p->tcfa_rate_est);
52
	/*
53
	 * gen_estimator est_timer() might access p->tcfa_lock
54 55
	 * or bstats, wait a RCU grace period before freeing p
	 */
56
	call_rcu(&p->tcfa_rcu, free_tcf);
57 58
}

59
int __tcf_hash_release(struct tc_action *p, bool bind, bool strict)
60 61 62 63 64
{
	int ret = 0;

	if (p) {
		if (bind)
65 66
			p->tcfa_bindcnt--;
		else if (strict && p->tcfa_bindcnt > 0)
67
			return -EPERM;
68

69 70 71 72 73
		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);
74
			ret = ACT_P_DELETED;
75 76
		}
	}
77

78 79
	return ret;
}
80
EXPORT_SYMBOL(__tcf_hash_release);
81

82
static int tcf_dump_walker(struct tcf_hashinfo *hinfo, struct sk_buff *skb,
83
			   struct netlink_callback *cb)
84
{
E
Eric Dumazet 已提交
85
	int err = 0, index = -1, i = 0, s_i = 0, n_i = 0;
86
	struct nlattr *nest;
87

88
	spin_lock_bh(&hinfo->lock);
89 90 91 92

	s_i = cb->args[0];

	for (i = 0; i < (hinfo->hmask + 1); i++) {
93
		struct hlist_head *head;
94
		struct tc_action *p;
95

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

98
		hlist_for_each_entry_rcu(p, head, tcfa_head) {
99 100 101
			index++;
			if (index < s_i)
				continue;
102

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

124
nla_put_failure:
125
	nla_nest_cancel(skb, nest);
126 127 128
	goto done;
}

129
static int tcf_del_walker(struct tcf_hashinfo *hinfo, struct sk_buff *skb,
130
			  const struct tc_action_ops *ops)
131
{
132
	struct nlattr *nest;
E
Eric Dumazet 已提交
133
	int i = 0, n_i = 0;
134
	int ret = -EINVAL;
135

136
	nest = nla_nest_start(skb, 0);
137 138
	if (nest == NULL)
		goto nla_put_failure;
139
	if (nla_put_string(skb, TCA_KIND, ops->kind))
140
		goto nla_put_failure;
141
	for (i = 0; i < (hinfo->hmask + 1); i++) {
142 143
		struct hlist_head *head;
		struct hlist_node *n;
144
		struct tc_action *p;
145

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

	return n_i;
161
nla_put_failure:
162
	nla_nest_cancel(skb, nest);
163
	return ret;
164 165
}

166 167
int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb,
		       struct netlink_callback *cb, int type,
168
		       const struct tc_action_ops *ops)
169
{
170 171
	struct tcf_hashinfo *hinfo = tn->hinfo;

172
	if (type == RTM_DELACTION) {
173
		return tcf_del_walker(hinfo, skb, ops);
174
	} else if (type == RTM_GETACTION) {
175
		return tcf_dump_walker(hinfo, skb, cb);
176
	} else {
177
		WARN(1, "tcf_generic_walker: unknown action %d\n", type);
178 179 180
		return -EINVAL;
	}
}
181
EXPORT_SYMBOL(tcf_generic_walker);
182

183
static struct tc_action *tcf_hash_lookup(u32 index, struct tcf_hashinfo *hinfo)
184
{
185
	struct tc_action *p = NULL;
186
	struct hlist_head *head;
187

188 189
	spin_lock_bh(&hinfo->lock);
	head = &hinfo->htab[tcf_hash(index, hinfo->hmask)];
190 191
	hlist_for_each_entry_rcu(p, head, tcfa_head)
		if (p->tcfa_index == index)
192
			break;
193
	spin_unlock_bh(&hinfo->lock);
194 195 196 197

	return p;
}

198
u32 tcf_hash_new_index(struct tc_action_net *tn)
199
{
200
	struct tcf_hashinfo *hinfo = tn->hinfo;
201
	u32 val = hinfo->index;
202 203 204 205 206 207

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

208
	hinfo->index = val;
209
	return val;
210 211 212
}
EXPORT_SYMBOL(tcf_hash_new_index);

213
int tcf_hash_search(struct tc_action_net *tn, struct tc_action **a, u32 index)
214
{
215
	struct tcf_hashinfo *hinfo = tn->hinfo;
216
	struct tc_action *p = tcf_hash_lookup(index, hinfo);
217 218

	if (p) {
219
		*a = p;
220 221 222 223
		return 1;
	}
	return 0;
}
224
EXPORT_SYMBOL(tcf_hash_search);
225

226
bool tcf_hash_check(struct tc_action_net *tn, u32 index, struct tc_action **a,
227
		    int bind)
228
{
229
	struct tcf_hashinfo *hinfo = tn->hinfo;
230 231
	struct tc_action *p = NULL;

232
	if (index && (p = tcf_hash_lookup(index, hinfo)) != NULL) {
233
		if (bind)
234 235 236
			p->tcfa_bindcnt++;
		p->tcfa_refcnt++;
		*a = p;
237
		return true;
238
	}
239
	return false;
240 241 242
}
EXPORT_SYMBOL(tcf_hash_check);

243 244 245
void tcf_hash_cleanup(struct tc_action *a, struct nlattr *est)
{
	if (est)
246
		gen_kill_estimator(&a->tcfa_rate_est);
247
	call_rcu(&a->tcfa_rcu, free_tcf);
248 249 250
}
EXPORT_SYMBOL(tcf_hash_cleanup);

251
int tcf_hash_create(struct tc_action_net *tn, u32 index, struct nlattr *est,
252 253
		    struct tc_action **a, const struct tc_action_ops *ops,
		    int bind, bool cpustats)
254
{
255
	struct tc_action *p = kzalloc(ops->size, GFP_KERNEL);
256
	struct tcf_hashinfo *hinfo = tn->hinfo;
257
	int err = -ENOMEM;
258 259

	if (unlikely(!p))
260
		return -ENOMEM;
261
	p->tcfa_refcnt = 1;
262
	if (bind)
263
		p->tcfa_bindcnt = 1;
264

265 266 267 268 269 270 271 272 273 274 275 276 277 278
	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;
		}
	}
279 280 281 282 283 284
	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;
285
	if (est) {
286 287 288
		err = gen_new_estimator(&p->tcfa_bstats, p->cpu_bstats,
					&p->tcfa_rate_est,
					&p->tcfa_lock, NULL, est);
289
		if (err) {
290 291
			free_percpu(p->cpu_qstats);
			goto err2;
292 293 294
		}
	}

295 296 297 298
	p->hinfo = hinfo;
	p->ops = ops;
	INIT_LIST_HEAD(&p->list);
	*a = p;
299
	return 0;
300 301 302
}
EXPORT_SYMBOL(tcf_hash_create);

303
void tcf_hash_insert(struct tc_action_net *tn, struct tc_action *a)
304
{
305
	struct tcf_hashinfo *hinfo = tn->hinfo;
306
	unsigned int h = tcf_hash(a->tcfa_index, hinfo->hmask);
307

308
	spin_lock_bh(&hinfo->lock);
309
	hlist_add_head(&a->tcfa_head, &hinfo->htab[h]);
310
	spin_unlock_bh(&hinfo->lock);
311 312
}
EXPORT_SYMBOL(tcf_hash_insert);
L
Linus Torvalds 已提交
313

314 315
void tcf_hashinfo_destroy(const struct tc_action_ops *ops,
			  struct tcf_hashinfo *hinfo)
316 317 318 319
{
	int i;

	for (i = 0; i < hinfo->hmask + 1; i++) {
320
		struct tc_action *p;
321 322
		struct hlist_node *n;

323
		hlist_for_each_entry_safe(p, n, &hinfo->htab[i], tcfa_head) {
324 325
			int ret;

326
			ret = __tcf_hash_release(p, false, true);
327 328 329 330 331 332 333 334
			if (ret == ACT_P_DELETED)
				module_put(ops->owner);
			else if (ret < 0)
				return;
		}
	}
	kfree(hinfo->htab);
}
335
EXPORT_SYMBOL(tcf_hashinfo_destroy);
336

337
static LIST_HEAD(act_base);
L
Linus Torvalds 已提交
338 339
static DEFINE_RWLOCK(act_mod_lock);

340 341
int tcf_register_action(struct tc_action_ops *act,
			struct pernet_operations *ops)
L
Linus Torvalds 已提交
342
{
343
	struct tc_action_ops *a;
344
	int ret;
L
Linus Torvalds 已提交
345

346
	if (!act->act || !act->dump || !act->init || !act->walk || !act->lookup)
347 348
		return -EINVAL;

349 350 351 352 353 354 355 356
	/* 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 已提交
357
	write_lock(&act_mod_lock);
358
	list_for_each_entry(a, &act_base, head) {
L
Linus Torvalds 已提交
359 360
		if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
			write_unlock(&act_mod_lock);
361
			unregister_pernet_subsys(ops);
L
Linus Torvalds 已提交
362 363 364
			return -EEXIST;
		}
	}
365
	list_add_tail(&act->head, &act_base);
L
Linus Torvalds 已提交
366
	write_unlock(&act_mod_lock);
367

L
Linus Torvalds 已提交
368 369
	return 0;
}
370
EXPORT_SYMBOL(tcf_register_action);
L
Linus Torvalds 已提交
371

372 373
int tcf_unregister_action(struct tc_action_ops *act,
			  struct pernet_operations *ops)
L
Linus Torvalds 已提交
374
{
375
	struct tc_action_ops *a;
L
Linus Torvalds 已提交
376 377 378
	int err = -ENOENT;

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

/* lookup by name */
static struct tc_action_ops *tc_lookup_action_n(char *kind)
{
396
	struct tc_action_ops *a, *res = NULL;
L
Linus Torvalds 已提交
397 398 399

	if (kind) {
		read_lock(&act_mod_lock);
400
		list_for_each_entry(a, &act_base, head) {
L
Linus Torvalds 已提交
401
			if (strcmp(kind, a->kind) == 0) {
402 403
				if (try_module_get(a->owner))
					res = a;
L
Linus Torvalds 已提交
404 405 406 407 408
				break;
			}
		}
		read_unlock(&act_mod_lock);
	}
409
	return res;
L
Linus Torvalds 已提交
410 411
}

412 413
/* lookup by nlattr */
static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
L
Linus Torvalds 已提交
414
{
415
	struct tc_action_ops *a, *res = NULL;
L
Linus Torvalds 已提交
416 417 418

	if (kind) {
		read_lock(&act_mod_lock);
419
		list_for_each_entry(a, &act_base, head) {
420
			if (nla_strcmp(kind, a->kind) == 0) {
421 422
				if (try_module_get(a->owner))
					res = a;
L
Linus Torvalds 已提交
423 424 425 426 427
				break;
			}
		}
		read_unlock(&act_mod_lock);
	}
428
	return res;
L
Linus Torvalds 已提交
429 430
}

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

436 437 438
	if (skb_skip_tc_classify(skb))
		return TC_ACT_OK;

439 440 441
	for (i = 0; i < nr_actions; i++) {
		const struct tc_action *a = actions[i];

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

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

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

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;
478
	unsigned char *b = skb_tail_pointer(skb);
479
	struct nlattr *nest;
L
Linus Torvalds 已提交
480

481 482
	if (nla_put_string(skb, TCA_KIND, a->ops->kind))
		goto nla_put_failure;
L
Linus Torvalds 已提交
483
	if (tcf_action_copy_stats(skb, a, 0))
484
		goto nla_put_failure;
485 486 487 488 489 490
	if (a->act_cookie) {
		if (nla_put(skb, TCA_ACT_COOKIE, a->act_cookie->len,
			    a->act_cookie->data))
			goto nla_put_failure;
	}

491 492 493
	nest = nla_nest_start(skb, TCA_OPTIONS);
	if (nest == NULL)
		goto nla_put_failure;
E
Eric Dumazet 已提交
494 495
	err = tcf_action_dump_old(skb, a, bind, ref);
	if (err > 0) {
496
		nla_nest_end(skb, nest);
L
Linus Torvalds 已提交
497 498 499
		return err;
	}

500
nla_put_failure:
501
	nlmsg_trim(skb, b);
L
Linus Torvalds 已提交
502 503
	return -1;
}
504
EXPORT_SYMBOL(tcf_action_dump_1);
L
Linus Torvalds 已提交
505

506 507
int tcf_action_dump(struct sk_buff *skb, struct list_head *actions,
		    int bind, int ref)
L
Linus Torvalds 已提交
508 509 510
{
	struct tc_action *a;
	int err = -EINVAL;
511
	struct nlattr *nest;
L
Linus Torvalds 已提交
512

513
	list_for_each_entry(a, actions, list) {
514 515 516
		nest = nla_nest_start(skb, a->order);
		if (nest == NULL)
			goto nla_put_failure;
L
Linus Torvalds 已提交
517 518
		err = tcf_action_dump_1(skb, a, bind, ref);
		if (err < 0)
519
			goto errout;
520
		nla_nest_end(skb, nest);
L
Linus Torvalds 已提交
521 522 523 524
	}

	return 0;

525
nla_put_failure:
526 527
	err = -EINVAL;
errout:
528
	nla_nest_cancel(skb, nest);
529
	return err;
L
Linus Torvalds 已提交
530 531
}

532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
int nla_memdup_cookie(struct tc_action *a, struct nlattr **tb)
{
	a->act_cookie = kzalloc(sizeof(*a->act_cookie), GFP_KERNEL);
	if (!a->act_cookie)
		return -ENOMEM;

	a->act_cookie->data = nla_memdup(tb[TCA_ACT_COOKIE], GFP_KERNEL);
	if (!a->act_cookie->data) {
		kfree(a->act_cookie);
		return -ENOMEM;
	}
	a->act_cookie->len = nla_len(tb[TCA_ACT_COOKIE]);

	return 0;
}

548 549 550
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 已提交
551 552 553 554
{
	struct tc_action *a;
	struct tc_action_ops *a_o;
	char act_name[IFNAMSIZ];
E
Eric Dumazet 已提交
555
	struct nlattr *tb[TCA_ACT_MAX + 1];
556
	struct nlattr *kind;
557
	int err;
L
Linus Torvalds 已提交
558 559

	if (name == NULL) {
560 561
		err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
		if (err < 0)
L
Linus Torvalds 已提交
562
			goto err_out;
563
		err = -EINVAL;
564
		kind = tb[TCA_ACT_KIND];
L
Linus Torvalds 已提交
565 566
		if (kind == NULL)
			goto err_out;
567
		if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
L
Linus Torvalds 已提交
568 569
			goto err_out;
	} else {
570
		err = -EINVAL;
L
Linus Torvalds 已提交
571 572 573 574 575 576
		if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
			goto err_out;
	}

	a_o = tc_lookup_action_n(act_name);
	if (a_o == NULL) {
577
#ifdef CONFIG_MODULES
L
Linus Torvalds 已提交
578
		rtnl_unlock();
579
		request_module("act_%s", act_name);
L
Linus Torvalds 已提交
580 581 582 583 584 585 586 587 588 589 590
		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) {
591
			err = -EAGAIN;
L
Linus Torvalds 已提交
592 593 594
			goto err_mod;
		}
#endif
595
		err = -ENOENT;
L
Linus Torvalds 已提交
596 597 598 599 600
		goto err_out;
	}

	/* backward compatibility for policer */
	if (name == NULL)
601
		err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, &a, ovr, bind);
L
Linus Torvalds 已提交
602
	else
603
		err = a_o->init(net, nla, est, &a, ovr, bind);
604
	if (err < 0)
605
		goto err_mod;
L
Linus Torvalds 已提交
606

607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
	if (tb[TCA_ACT_COOKIE]) {
		int cklen = nla_len(tb[TCA_ACT_COOKIE]);

		if (cklen > TC_COOKIE_MAX_SIZE) {
			err = -EINVAL;
			tcf_hash_release(a, bind);
			goto err_mod;
		}

		err = nla_memdup_cookie(a, tb);
		if (err < 0) {
			tcf_hash_release(a, bind);
			goto err_mod;
		}
	}

L
Linus Torvalds 已提交
623
	/* module count goes up only when brand new policy is created
E
Eric Dumazet 已提交
624 625 626
	 * if it exists and is only bound to in a_o->init() then
	 * ACT_P_CREATED is not returned (a zero is).
	 */
627
	if (err != ACT_P_CREATED)
L
Linus Torvalds 已提交
628 629 630 631 632 633 634
		module_put(a_o->owner);

	return a;

err_mod:
	module_put(a_o->owner);
err_out:
635
	return ERR_PTR(err);
L
Linus Torvalds 已提交
636 637
}

638 639 640 641 642 643 644 645 646 647 648
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 已提交
649 650
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 已提交
651
{
E
Eric Dumazet 已提交
652
	struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
653
	struct tc_action *act;
654
	int err;
L
Linus Torvalds 已提交
655 656
	int i;

657 658
	err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
	if (err < 0)
659
		return err;
L
Linus Torvalds 已提交
660

661
	for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
662
		act = tcf_action_init_1(net, tb[i], est, name, ovr, bind);
663 664
		if (IS_ERR(act)) {
			err = PTR_ERR(act);
L
Linus Torvalds 已提交
665
			goto err;
666
		}
667
		act->order = i;
668 669
		if (ovr)
			act->tcfa_refcnt++;
670
		list_add_tail(&act->list, actions);
L
Linus Torvalds 已提交
671
	}
672 673 674 675 676

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

err:
680 681
	tcf_action_destroy(actions, bind);
	return err;
L
Linus Torvalds 已提交
682 683
}

684
int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *p,
L
Linus Torvalds 已提交
685 686 687 688
			  int compat_mode)
{
	int err = 0;
	struct gnet_dump d;
689

690
	if (p == NULL)
L
Linus Torvalds 已提交
691 692 693
		goto errout;

	/* compat_mode being true specifies a call that is supposed
694
	 * to add additional backward compatibility statistic TLVs.
L
Linus Torvalds 已提交
695 696
	 */
	if (compat_mode) {
697
		if (p->type == TCA_OLD_COMPAT)
L
Linus Torvalds 已提交
698
			err = gnet_stats_start_copy_compat(skb, 0,
699 700
							   TCA_STATS,
							   TCA_XSTATS,
701
							   &p->tcfa_lock, &d,
702
							   TCA_PAD);
L
Linus Torvalds 已提交
703 704 705 706
		else
			return 0;
	} else
		err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
707
					    &p->tcfa_lock, &d, TCA_ACT_PAD);
L
Linus Torvalds 已提交
708 709 710 711

	if (err < 0)
		goto errout;

712
	if (gnet_stats_copy_basic(NULL, &d, p->cpu_bstats, &p->tcfa_bstats) < 0 ||
713
	    gnet_stats_copy_rate_est(&d, &p->tcfa_rate_est) < 0 ||
714
	    gnet_stats_copy_queue(&d, p->cpu_qstats,
715 716
				  &p->tcfa_qstats,
				  p->tcfa_qstats.qlen) < 0)
L
Linus Torvalds 已提交
717 718 719 720 721 722 723 724 725 726 727
		goto errout;

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

	return 0;

errout:
	return -1;
}

728 729 730
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 已提交
731 732 733
{
	struct tcamsg *t;
	struct nlmsghdr *nlh;
734
	unsigned char *b = skb_tail_pointer(skb);
735
	struct nlattr *nest;
L
Linus Torvalds 已提交
736

737
	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
738 739 740
	if (!nlh)
		goto out_nlmsg_trim;
	t = nlmsg_data(nlh);
L
Linus Torvalds 已提交
741
	t->tca_family = AF_UNSPEC;
742 743
	t->tca__pad1 = 0;
	t->tca__pad2 = 0;
744

745 746
	nest = nla_nest_start(skb, TCA_ACT_TAB);
	if (nest == NULL)
747
		goto out_nlmsg_trim;
L
Linus Torvalds 已提交
748

749
	if (tcf_action_dump(skb, actions, bind, ref) < 0)
750
		goto out_nlmsg_trim;
L
Linus Torvalds 已提交
751

752
	nla_nest_end(skb, nest);
753

754
	nlh->nlmsg_len = skb_tail_pointer(skb) - b;
L
Linus Torvalds 已提交
755 756
	return skb->len;

757
out_nlmsg_trim:
758
	nlmsg_trim(skb, b);
L
Linus Torvalds 已提交
759 760 761 762
	return -1;
}

static int
763
act_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
764
	       struct list_head *actions, int event)
L
Linus Torvalds 已提交
765 766 767 768 769 770
{
	struct sk_buff *skb;

	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
	if (!skb)
		return -ENOBUFS;
771 772
	if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event,
			 0, 0) <= 0) {
L
Linus Torvalds 已提交
773 774 775
		kfree_skb(skb);
		return -EINVAL;
	}
776

777
	return rtnl_unicast(skb, net, portid);
L
Linus Torvalds 已提交
778 779
}

780 781
static struct tc_action *tcf_action_get_1(struct net *net, struct nlattr *nla,
					  struct nlmsghdr *n, u32 portid)
L
Linus Torvalds 已提交
782
{
E
Eric Dumazet 已提交
783
	struct nlattr *tb[TCA_ACT_MAX + 1];
784
	const struct tc_action_ops *ops;
L
Linus Torvalds 已提交
785 786
	struct tc_action *a;
	int index;
787
	int err;
L
Linus Torvalds 已提交
788

789 790
	err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
	if (err < 0)
791
		goto err_out;
L
Linus Torvalds 已提交
792

793
	err = -EINVAL;
794 795
	if (tb[TCA_ACT_INDEX] == NULL ||
	    nla_len(tb[TCA_ACT_INDEX]) < sizeof(index))
796
		goto err_out;
797
	index = nla_get_u32(tb[TCA_ACT_INDEX]);
L
Linus Torvalds 已提交
798

799
	err = -EINVAL;
800 801 802
	ops = tc_lookup_action(tb[TCA_ACT_KIND]);
	if (!ops) /* could happen in batch of actions */
		goto err_out;
803
	err = -ENOENT;
804
	if (ops->lookup(net, &a, index) == 0)
L
Linus Torvalds 已提交
805 806
		goto err_mod;

807
	module_put(ops->owner);
L
Linus Torvalds 已提交
808
	return a;
809

L
Linus Torvalds 已提交
810
err_mod:
811
	module_put(ops->owner);
812 813
err_out:
	return ERR_PTR(err);
L
Linus Torvalds 已提交
814 815
}

816
static int tca_action_flush(struct net *net, struct nlattr *nla,
817
			    struct nlmsghdr *n, u32 portid)
L
Linus Torvalds 已提交
818 819 820 821 822 823
{
	struct sk_buff *skb;
	unsigned char *b;
	struct nlmsghdr *nlh;
	struct tcamsg *t;
	struct netlink_callback dcb;
824
	struct nlattr *nest;
E
Eric Dumazet 已提交
825
	struct nlattr *tb[TCA_ACT_MAX + 1];
826
	const struct tc_action_ops *ops;
827
	struct nlattr *kind;
828
	int err = -ENOMEM;
L
Linus Torvalds 已提交
829 830 831

	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
	if (!skb) {
832
		pr_debug("tca_action_flush: failed skb alloc\n");
833
		return err;
L
Linus Torvalds 已提交
834 835
	}

836
	b = skb_tail_pointer(skb);
L
Linus Torvalds 已提交
837

838 839
	err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
	if (err < 0)
L
Linus Torvalds 已提交
840 841
		goto err_out;

842
	err = -EINVAL;
843
	kind = tb[TCA_ACT_KIND];
844 845
	ops = tc_lookup_action(kind);
	if (!ops) /*some idjot trying to flush unknown action */
L
Linus Torvalds 已提交
846 847
		goto err_out;

848 849
	nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION,
			sizeof(*t), 0);
850 851 852
	if (!nlh)
		goto out_module_put;
	t = nlmsg_data(nlh);
L
Linus Torvalds 已提交
853
	t->tca_family = AF_UNSPEC;
854 855
	t->tca__pad1 = 0;
	t->tca__pad2 = 0;
L
Linus Torvalds 已提交
856

857 858
	nest = nla_nest_start(skb, TCA_ACT_TAB);
	if (nest == NULL)
859
		goto out_module_put;
L
Linus Torvalds 已提交
860

861
	err = ops->walk(net, skb, &dcb, RTM_DELACTION, ops);
L
Linus Torvalds 已提交
862
	if (err < 0)
863
		goto out_module_put;
864 865
	if (err == 0)
		goto noflush_out;
L
Linus Torvalds 已提交
866

867
	nla_nest_end(skb, nest);
L
Linus Torvalds 已提交
868

869
	nlh->nlmsg_len = skb_tail_pointer(skb) - b;
L
Linus Torvalds 已提交
870
	nlh->nlmsg_flags |= NLM_F_ROOT;
871
	module_put(ops->owner);
872
	err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
E
Eric Dumazet 已提交
873
			     n->nlmsg_flags & NLM_F_ECHO);
L
Linus Torvalds 已提交
874 875 876 877 878
	if (err > 0)
		return 0;

	return err;

879
out_module_put:
880
	module_put(ops->owner);
L
Linus Torvalds 已提交
881
err_out:
882
noflush_out:
L
Linus Torvalds 已提交
883 884 885 886
	kfree_skb(skb);
	return err;
}

887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904
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 */
905 906 907 908 909
	ret = tcf_action_destroy(actions, 0);
	if (ret < 0) {
		kfree_skb(skb);
		return ret;
	}
910 911 912 913 914 915 916 917

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

L
Linus Torvalds 已提交
918
static int
919
tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
920
	      u32 portid, int event)
L
Linus Torvalds 已提交
921
{
922
	int i, ret;
E
Eric Dumazet 已提交
923
	struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
924 925
	struct tc_action *act;
	LIST_HEAD(actions);
L
Linus Torvalds 已提交
926

927 928 929
	ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
	if (ret < 0)
		return ret;
L
Linus Torvalds 已提交
930

E
Eric Dumazet 已提交
931
	if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
932
		if (tb[1] != NULL)
933
			return tca_action_flush(net, tb[1], n, portid);
934 935
		else
			return -EINVAL;
L
Linus Torvalds 已提交
936 937
	}

938
	for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
939
		act = tcf_action_get_1(net, tb[i], n, portid);
940 941
		if (IS_ERR(act)) {
			ret = PTR_ERR(act);
L
Linus Torvalds 已提交
942
			goto err;
943
		}
944
		act->order = i;
945
		list_add_tail(&act->list, &actions);
L
Linus Torvalds 已提交
946 947 948
	}

	if (event == RTM_GETACTION)
949
		ret = act_get_notify(net, portid, n, &actions, event);
L
Linus Torvalds 已提交
950
	else { /* delete */
951 952
		ret = tcf_del_notify(net, n, &actions, portid);
		if (ret)
L
Linus Torvalds 已提交
953 954 955 956
			goto err;
		return ret;
	}
err:
957 958
	if (event != RTM_GETACTION)
		tcf_action_destroy(&actions, 0);
L
Linus Torvalds 已提交
959 960 961
	return ret;
}

962 963 964
static int
tcf_add_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
	       u32 portid)
L
Linus Torvalds 已提交
965 966 967 968 969 970 971 972
{
	struct sk_buff *skb;
	int err = 0;

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

973 974 975 976 977
	if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
			 RTM_NEWACTION, 0, 0) <= 0) {
		kfree_skb(skb);
		return -EINVAL;
	}
978

979 980
	err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
			     n->nlmsg_flags & NLM_F_ECHO);
L
Linus Torvalds 已提交
981 982 983 984 985
	if (err > 0)
		err = 0;
	return err;
}

J
Jamal Hadi Salim 已提交
986 987
static int tcf_action_add(struct net *net, struct nlattr *nla,
			  struct nlmsghdr *n, u32 portid, int ovr)
L
Linus Torvalds 已提交
988 989
{
	int ret = 0;
990
	LIST_HEAD(actions);
L
Linus Torvalds 已提交
991

992 993
	ret = tcf_action_init(net, nla, NULL, NULL, ovr, 0, &actions);
	if (ret)
994
		return ret;
L
Linus Torvalds 已提交
995

996
	return tcf_add_notify(net, n, &actions, portid);
L
Linus Torvalds 已提交
997 998
}

999
static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n)
L
Linus Torvalds 已提交
1000
{
1001
	struct net *net = sock_net(skb->sk);
1002
	struct nlattr *tca[TCA_ACT_MAX + 1];
1003
	u32 portid = skb ? NETLINK_CB(skb).portid : 0;
L
Linus Torvalds 已提交
1004 1005
	int ret = 0, ovr = 0;

1006 1007
	if ((n->nlmsg_type != RTM_GETACTION) &&
	    !netlink_capable(skb, CAP_NET_ADMIN))
1008 1009
		return -EPERM;

1010 1011 1012 1013 1014
	ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ACT_MAX, NULL);
	if (ret < 0)
		return ret;

	if (tca[TCA_ACT_TAB] == NULL) {
1015
		pr_notice("tc_ctl_action: received NO action attribs\n");
L
Linus Torvalds 已提交
1016 1017 1018
		return -EINVAL;
	}

E
Eric Dumazet 已提交
1019
	/* n->nlmsg_flags & NLM_F_CREATE */
L
Linus Torvalds 已提交
1020 1021 1022
	switch (n->nlmsg_type) {
	case RTM_NEWACTION:
		/* we are going to assume all other flags
L
Lucas De Marchi 已提交
1023
		 * imply create only if it doesn't exist
L
Linus Torvalds 已提交
1024 1025 1026 1027
		 * Note that CREATE | EXCL implies that
		 * but since we want avoid ambiguity (eg when flags
		 * is zero) then just set this
		 */
E
Eric Dumazet 已提交
1028
		if (n->nlmsg_flags & NLM_F_REPLACE)
L
Linus Torvalds 已提交
1029 1030
			ovr = 1;
replay:
1031
		ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr);
L
Linus Torvalds 已提交
1032 1033 1034 1035
		if (ret == -EAGAIN)
			goto replay;
		break;
	case RTM_DELACTION:
1036
		ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
1037
				    portid, RTM_DELACTION);
L
Linus Torvalds 已提交
1038 1039
		break;
	case RTM_GETACTION:
1040
		ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
1041
				    portid, RTM_GETACTION);
L
Linus Torvalds 已提交
1042 1043 1044 1045 1046 1047 1048 1049
		break;
	default:
		BUG();
	}

	return ret;
}

J
Jamal Hadi Salim 已提交
1050
static struct nlattr *find_dump_kind(const struct nlmsghdr *n)
L
Linus Torvalds 已提交
1051
{
E
Eric Dumazet 已提交
1052
	struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
1053 1054 1055
	struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
	struct nlattr *nla[TCAA_MAX + 1];
	struct nlattr *kind;
L
Linus Torvalds 已提交
1056

1057
	if (nlmsg_parse(n, sizeof(struct tcamsg), nla, TCAA_MAX, NULL) < 0)
L
Linus Torvalds 已提交
1058
		return NULL;
1059
	tb1 = nla[TCA_ACT_TAB];
L
Linus Torvalds 已提交
1060 1061 1062
	if (tb1 == NULL)
		return NULL;

1063 1064
	if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
		      NLMSG_ALIGN(nla_len(tb1)), NULL) < 0)
L
Linus Torvalds 已提交
1065 1066
		return NULL;

1067 1068
	if (tb[1] == NULL)
		return NULL;
1069
	if (nla_parse_nested(tb2, TCA_ACT_MAX, tb[1], NULL) < 0)
L
Linus Torvalds 已提交
1070
		return NULL;
1071
	kind = tb2[TCA_ACT_KIND];
L
Linus Torvalds 已提交
1072

1073
	return kind;
L
Linus Torvalds 已提交
1074 1075
}

J
Jamal Hadi Salim 已提交
1076
static int tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
L
Linus Torvalds 已提交
1077
{
1078
	struct net *net = sock_net(skb->sk);
L
Linus Torvalds 已提交
1079
	struct nlmsghdr *nlh;
1080
	unsigned char *b = skb_tail_pointer(skb);
1081
	struct nlattr *nest;
L
Linus Torvalds 已提交
1082 1083
	struct tc_action_ops *a_o;
	int ret = 0;
1084
	struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
1085
	struct nlattr *kind = find_dump_kind(cb->nlh);
L
Linus Torvalds 已提交
1086 1087

	if (kind == NULL) {
1088
		pr_info("tc_dump_action: action bad kind\n");
L
Linus Torvalds 已提交
1089 1090 1091
		return 0;
	}

1092
	a_o = tc_lookup_action(kind);
E
Eric Dumazet 已提交
1093
	if (a_o == NULL)
L
Linus Torvalds 已提交
1094 1095
		return 0;

1096
	nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
1097 1098 1099 1100
			cb->nlh->nlmsg_type, sizeof(*t), 0);
	if (!nlh)
		goto out_module_put;
	t = nlmsg_data(nlh);
L
Linus Torvalds 已提交
1101
	t->tca_family = AF_UNSPEC;
1102 1103
	t->tca__pad1 = 0;
	t->tca__pad2 = 0;
L
Linus Torvalds 已提交
1104

1105 1106
	nest = nla_nest_start(skb, TCA_ACT_TAB);
	if (nest == NULL)
1107
		goto out_module_put;
L
Linus Torvalds 已提交
1108

1109
	ret = a_o->walk(net, skb, cb, RTM_GETACTION, a_o);
L
Linus Torvalds 已提交
1110
	if (ret < 0)
1111
		goto out_module_put;
L
Linus Torvalds 已提交
1112 1113

	if (ret > 0) {
1114
		nla_nest_end(skb, nest);
L
Linus Torvalds 已提交
1115 1116
		ret = skb->len;
	} else
1117
		nlmsg_trim(skb, b);
L
Linus Torvalds 已提交
1118

1119
	nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1120
	if (NETLINK_CB(cb->skb).portid && ret)
L
Linus Torvalds 已提交
1121 1122 1123 1124
		nlh->nlmsg_flags |= NLM_F_MULTI;
	module_put(a_o->owner);
	return skb->len;

1125
out_module_put:
L
Linus Torvalds 已提交
1126
	module_put(a_o->owner);
1127
	nlmsg_trim(skb, b);
L
Linus Torvalds 已提交
1128 1129 1130 1131 1132
	return skb->len;
}

static int __init tc_action_init(void)
{
1133 1134 1135 1136
	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 已提交
1137 1138 1139 1140 1141

	return 0;
}

subsys_initcall(tc_action_init);