act_api.c 23.8 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 32 33 34 35 36 37 38
static void free_tcf(struct rcu_head *head)
{
	struct tcf_common *p = container_of(head, struct tcf_common, tcfc_rcu);

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

39
static void tcf_hash_destroy(struct tc_action *a)
40
{
41 42 43
	struct tcf_common *p = a->priv;
	struct tcf_hashinfo *hinfo = a->ops->hinfo;

44 45 46 47 48 49 50 51 52
	spin_lock_bh(&hinfo->lock);
	hlist_del(&p->tcfc_head);
	spin_unlock_bh(&hinfo->lock);
	gen_kill_estimator(&p->tcfc_bstats,
			   &p->tcfc_rate_est);
	/*
	 * gen_estimator est_timer() might access p->tcfc_lock
	 * or bstats, wait a RCU grace period before freeing p
	 */
53
	call_rcu(&p->tcfc_rcu, free_tcf);
54 55
}

56
int __tcf_hash_release(struct tc_action *a, bool bind, bool strict)
57
{
58
	struct tcf_common *p = a->priv;
59 60 61 62 63
	int ret = 0;

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

		p->tcfc_refcnt--;
68
		if (p->tcfc_bindcnt <= 0 && p->tcfc_refcnt <= 0) {
W
WANG Cong 已提交
69 70
			if (a->ops->cleanup)
				a->ops->cleanup(a, bind);
71
			tcf_hash_destroy(a);
72 73 74
			ret = 1;
		}
	}
75

76 77
	return ret;
}
78
EXPORT_SYMBOL(__tcf_hash_release);
79 80

static int tcf_dump_walker(struct sk_buff *skb, struct netlink_callback *cb,
81
			   struct tc_action *a)
82
{
83
	struct tcf_hashinfo *hinfo = a->ops->hinfo;
84
	struct hlist_head *head;
85
	struct tcf_common *p;
E
Eric Dumazet 已提交
86
	int err = 0, index = -1, i = 0, s_i = 0, n_i = 0;
87
	struct nlattr *nest;
88

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

	s_i = cb->args[0];

	for (i = 0; i < (hinfo->hmask + 1); i++) {
94
		head = &hinfo->htab[tcf_hash(i, hinfo->hmask)];
95

96
		hlist_for_each_entry_rcu(p, head, tcfc_head) {
97 98 99 100 101
			index++;
			if (index < s_i)
				continue;
			a->priv = p;
			a->order = n_i;
102 103 104 105

			nest = nla_nest_start(skb, a->order);
			if (nest == NULL)
				goto nla_put_failure;
106 107 108
			err = tcf_action_dump_1(skb, a, 0, 0);
			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 sk_buff *skb, struct tc_action *a)
130
{
131
	struct tcf_hashinfo *hinfo = a->ops->hinfo;
132 133 134
	struct hlist_head *head;
	struct hlist_node *n;
	struct tcf_common *p;
135
	struct nlattr *nest;
E
Eric Dumazet 已提交
136
	int i = 0, n_i = 0;
137
	int ret = -EINVAL;
138

139 140 141
	nest = nla_nest_start(skb, a->order);
	if (nest == NULL)
		goto nla_put_failure;
142 143
	if (nla_put_string(skb, TCA_KIND, a->ops->kind))
		goto nla_put_failure;
144
	for (i = 0; i < (hinfo->hmask + 1); i++) {
145 146
		head = &hinfo->htab[tcf_hash(i, hinfo->hmask)];
		hlist_for_each_entry_safe(p, n, head, tcfc_head) {
147
			a->priv = p;
148
			ret = __tcf_hash_release(a, false, true);
149
			if (ret == ACT_P_DELETED) {
E
Eric Dumazet 已提交
150
				module_put(a->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
static int tcf_generic_walker(struct sk_buff *skb, struct netlink_callback *cb,
			      int type, struct tc_action *a)
168 169
{
	if (type == RTM_DELACTION) {
170
		return tcf_del_walker(skb, a);
171
	} else if (type == RTM_GETACTION) {
172
		return tcf_dump_walker(skb, cb, a);
173
	} else {
174
		WARN(1, "tcf_generic_walker: unknown action %d\n", type);
175 176 177 178
		return -EINVAL;
	}
}

179
static struct tcf_common *tcf_hash_lookup(u32 index, struct tcf_hashinfo *hinfo)
180
{
181 182
	struct tcf_common *p = NULL;
	struct hlist_head *head;
183

184 185 186
	spin_lock_bh(&hinfo->lock);
	head = &hinfo->htab[tcf_hash(index, hinfo->hmask)];
	hlist_for_each_entry_rcu(p, head, tcfc_head)
187 188
		if (p->tcfc_index == index)
			break;
189
	spin_unlock_bh(&hinfo->lock);
190 191 192 193

	return p;
}

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

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

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

208
int tcf_hash_search(struct tc_action *a, u32 index)
209 210 211 212 213 214 215 216 217 218
{
	struct tcf_hashinfo *hinfo = a->ops->hinfo;
	struct tcf_common *p = tcf_hash_lookup(index, hinfo);

	if (p) {
		a->priv = p;
		return 1;
	}
	return 0;
}
219
EXPORT_SYMBOL(tcf_hash_search);
220

221
int tcf_hash_check(u32 index, struct tc_action *a, int bind)
222
{
223
	struct tcf_hashinfo *hinfo = a->ops->hinfo;
224 225
	struct tcf_common *p = NULL;
	if (index && (p = tcf_hash_lookup(index, hinfo)) != NULL) {
226
		if (bind)
227
			p->tcfc_bindcnt++;
228
		p->tcfc_refcnt++;
229
		a->priv = p;
230
		return 1;
231
	}
232
	return 0;
233 234 235
}
EXPORT_SYMBOL(tcf_hash_check);

236 237 238 239 240 241
void tcf_hash_cleanup(struct tc_action *a, struct nlattr *est)
{
	struct tcf_common *pc = a->priv;
	if (est)
		gen_kill_estimator(&pc->tcfc_bstats,
				   &pc->tcfc_rate_est);
242
	call_rcu(&pc->tcfc_rcu, free_tcf);
243 244 245 246
}
EXPORT_SYMBOL(tcf_hash_cleanup);

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

	if (unlikely(!p))
254
		return -ENOMEM;
255 256 257 258
	p->tcfc_refcnt = 1;
	if (bind)
		p->tcfc_bindcnt = 1;

259 260 261 262 263 264 265 266 267 268 269 270 271 272
	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;
		}
	}
273
	spin_lock_init(&p->tcfc_lock);
274
	INIT_HLIST_NODE(&p->tcfc_head);
275
	p->tcfc_index = index ? index : tcf_hash_new_index(hinfo);
276 277
	p->tcfc_tm.install = jiffies;
	p->tcfc_tm.lastuse = jiffies;
278
	if (est) {
279 280 281
		err = gen_new_estimator(&p->tcfc_bstats, p->cpu_bstats,
					&p->tcfc_rate_est,
					&p->tcfc_lock, est);
282
		if (err) {
283 284
			free_percpu(p->cpu_qstats);
			goto err2;
285 286 287
		}
	}

288
	a->priv = (void *) p;
289
	return 0;
290 291 292
}
EXPORT_SYMBOL(tcf_hash_create);

293
void tcf_hash_insert(struct tc_action *a)
294
{
295 296
	struct tcf_common *p = a->priv;
	struct tcf_hashinfo *hinfo = a->ops->hinfo;
297 298
	unsigned int h = tcf_hash(p->tcfc_index, hinfo->hmask);

299 300 301
	spin_lock_bh(&hinfo->lock);
	hlist_add_head(&p->tcfc_head, &hinfo->htab[h]);
	spin_unlock_bh(&hinfo->lock);
302 303
}
EXPORT_SYMBOL(tcf_hash_insert);
L
Linus Torvalds 已提交
304

305
static LIST_HEAD(act_base);
L
Linus Torvalds 已提交
306 307
static DEFINE_RWLOCK(act_mod_lock);

308
int tcf_register_action(struct tc_action_ops *act, unsigned int mask)
L
Linus Torvalds 已提交
309
{
310
	struct tc_action_ops *a;
311
	int err;
L
Linus Torvalds 已提交
312

W
WANG Cong 已提交
313 314
	/* Must supply act, dump and init */
	if (!act->act || !act->dump || !act->init)
315 316
		return -EINVAL;

317
	/* Supply defaults */
318 319
	if (!act->lookup)
		act->lookup = tcf_hash_search;
320 321
	if (!act->walk)
		act->walk = tcf_generic_walker;
322

323 324 325 326 327 328 329 330 331
	act->hinfo = kmalloc(sizeof(struct tcf_hashinfo), GFP_KERNEL);
	if (!act->hinfo)
		return -ENOMEM;
	err = tcf_hashinfo_init(act->hinfo, mask);
	if (err) {
		kfree(act->hinfo);
		return err;
	}

L
Linus Torvalds 已提交
332
	write_lock(&act_mod_lock);
333
	list_for_each_entry(a, &act_base, head) {
L
Linus Torvalds 已提交
334 335
		if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
			write_unlock(&act_mod_lock);
336 337
			tcf_hashinfo_destroy(act->hinfo);
			kfree(act->hinfo);
L
Linus Torvalds 已提交
338 339 340
			return -EEXIST;
		}
	}
341
	list_add_tail(&act->head, &act_base);
L
Linus Torvalds 已提交
342 343 344
	write_unlock(&act_mod_lock);
	return 0;
}
345
EXPORT_SYMBOL(tcf_register_action);
L
Linus Torvalds 已提交
346 347 348

int tcf_unregister_action(struct tc_action_ops *act)
{
349
	struct tc_action_ops *a;
L
Linus Torvalds 已提交
350 351 352
	int err = -ENOENT;

	write_lock(&act_mod_lock);
353 354 355
	list_for_each_entry(a, &act_base, head) {
		if (a == act) {
			list_del(&act->head);
356 357
			tcf_hashinfo_destroy(act->hinfo);
			kfree(act->hinfo);
358
			err = 0;
L
Linus Torvalds 已提交
359
			break;
360
		}
L
Linus Torvalds 已提交
361 362 363 364
	}
	write_unlock(&act_mod_lock);
	return err;
}
365
EXPORT_SYMBOL(tcf_unregister_action);
L
Linus Torvalds 已提交
366 367 368 369

/* lookup by name */
static struct tc_action_ops *tc_lookup_action_n(char *kind)
{
370
	struct tc_action_ops *a, *res = NULL;
L
Linus Torvalds 已提交
371 372 373

	if (kind) {
		read_lock(&act_mod_lock);
374
		list_for_each_entry(a, &act_base, head) {
L
Linus Torvalds 已提交
375
			if (strcmp(kind, a->kind) == 0) {
376 377
				if (try_module_get(a->owner))
					res = a;
L
Linus Torvalds 已提交
378 379 380 381 382
				break;
			}
		}
		read_unlock(&act_mod_lock);
	}
383
	return res;
L
Linus Torvalds 已提交
384 385
}

386 387
/* lookup by nlattr */
static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
L
Linus Torvalds 已提交
388
{
389
	struct tc_action_ops *a, *res = NULL;
L
Linus Torvalds 已提交
390 391 392

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

405
int tcf_action_exec(struct sk_buff *skb, const struct list_head *actions,
406
		    struct tcf_result *res)
L
Linus Torvalds 已提交
407
{
408
	const struct tc_action *a;
L
Linus Torvalds 已提交
409 410 411 412 413 414 415
	int ret = -1;

	if (skb->tc_verd & TC_NCLS) {
		skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
		ret = TC_ACT_OK;
		goto exec_done;
	}
416
	list_for_each_entry(a, actions, list) {
L
Linus Torvalds 已提交
417
repeat:
418 419 420 421 422
		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 已提交
423 424 425 426
	}
exec_done:
	return ret;
}
427
EXPORT_SYMBOL(tcf_action_exec);
L
Linus Torvalds 已提交
428

429
int tcf_action_destroy(struct list_head *actions, int bind)
L
Linus Torvalds 已提交
430
{
431
	struct tc_action *a, *tmp;
432
	int ret = 0;
L
Linus Torvalds 已提交
433

434
	list_for_each_entry_safe(a, tmp, actions, list) {
435
		ret = __tcf_hash_release(a, bind, true);
436
		if (ret == ACT_P_DELETED)
437
			module_put(a->ops->owner);
438 439
		else if (ret < 0)
			return ret;
440 441
		list_del(&a->list);
		kfree(a);
L
Linus Torvalds 已提交
442
	}
443
	return ret;
L
Linus Torvalds 已提交
444 445 446 447 448 449 450 451 452 453 454 455
}

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

459 460
	if (nla_put_string(skb, TCA_KIND, a->ops->kind))
		goto nla_put_failure;
L
Linus Torvalds 已提交
461
	if (tcf_action_copy_stats(skb, a, 0))
462
		goto nla_put_failure;
463 464 465
	nest = nla_nest_start(skb, TCA_OPTIONS);
	if (nest == NULL)
		goto nla_put_failure;
E
Eric Dumazet 已提交
466 467
	err = tcf_action_dump_old(skb, a, bind, ref);
	if (err > 0) {
468
		nla_nest_end(skb, nest);
L
Linus Torvalds 已提交
469 470 471
		return err;
	}

472
nla_put_failure:
473
	nlmsg_trim(skb, b);
L
Linus Torvalds 已提交
474 475
	return -1;
}
476
EXPORT_SYMBOL(tcf_action_dump_1);
L
Linus Torvalds 已提交
477 478

int
479
tcf_action_dump(struct sk_buff *skb, struct list_head *actions, int bind, int ref)
L
Linus Torvalds 已提交
480 481 482
{
	struct tc_action *a;
	int err = -EINVAL;
483
	struct nlattr *nest;
L
Linus Torvalds 已提交
484

485
	list_for_each_entry(a, actions, list) {
486 487 488
		nest = nla_nest_start(skb, a->order);
		if (nest == NULL)
			goto nla_put_failure;
L
Linus Torvalds 已提交
489 490
		err = tcf_action_dump_1(skb, a, bind, ref);
		if (err < 0)
491
			goto errout;
492
		nla_nest_end(skb, nest);
L
Linus Torvalds 已提交
493 494 495 496
	}

	return 0;

497
nla_put_failure:
498 499
	err = -EINVAL;
errout:
500
	nla_nest_cancel(skb, nest);
501
	return err;
L
Linus Torvalds 已提交
502 503
}

504 505 506
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 已提交
507 508 509 510
{
	struct tc_action *a;
	struct tc_action_ops *a_o;
	char act_name[IFNAMSIZ];
E
Eric Dumazet 已提交
511
	struct nlattr *tb[TCA_ACT_MAX + 1];
512
	struct nlattr *kind;
513
	int err;
L
Linus Torvalds 已提交
514 515

	if (name == NULL) {
516 517
		err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
		if (err < 0)
L
Linus Torvalds 已提交
518
			goto err_out;
519
		err = -EINVAL;
520
		kind = tb[TCA_ACT_KIND];
L
Linus Torvalds 已提交
521 522
		if (kind == NULL)
			goto err_out;
523
		if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
L
Linus Torvalds 已提交
524 525
			goto err_out;
	} else {
526
		err = -EINVAL;
L
Linus Torvalds 已提交
527 528 529 530 531 532
		if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
			goto err_out;
	}

	a_o = tc_lookup_action_n(act_name);
	if (a_o == NULL) {
533
#ifdef CONFIG_MODULES
L
Linus Torvalds 已提交
534
		rtnl_unlock();
535
		request_module("act_%s", act_name);
L
Linus Torvalds 已提交
536 537 538 539 540 541 542 543 544 545 546
		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) {
547
			err = -EAGAIN;
L
Linus Torvalds 已提交
548 549 550
			goto err_mod;
		}
#endif
551
		err = -ENOENT;
L
Linus Torvalds 已提交
552 553 554
		goto err_out;
	}

555
	err = -ENOMEM;
556
	a = kzalloc(sizeof(*a), GFP_KERNEL);
L
Linus Torvalds 已提交
557 558 559
	if (a == NULL)
		goto err_mod;

560
	a->ops = a_o;
561
	INIT_LIST_HEAD(&a->list);
L
Linus Torvalds 已提交
562 563
	/* backward compatibility for policer */
	if (name == NULL)
564
		err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, a, ovr, bind);
L
Linus Torvalds 已提交
565
	else
566
		err = a_o->init(net, nla, est, a, ovr, bind);
567
	if (err < 0)
L
Linus Torvalds 已提交
568 569 570
		goto err_free;

	/* module count goes up only when brand new policy is created
E
Eric Dumazet 已提交
571 572 573
	 * if it exists and is only bound to in a_o->init() then
	 * ACT_P_CREATED is not returned (a zero is).
	 */
574
	if (err != ACT_P_CREATED)
L
Linus Torvalds 已提交
575 576 577 578 579 580 581 582 583
		module_put(a_o->owner);

	return a;

err_free:
	kfree(a);
err_mod:
	module_put(a_o->owner);
err_out:
584
	return ERR_PTR(err);
L
Linus Torvalds 已提交
585 586
}

587
int tcf_action_init(struct net *net, struct nlattr *nla,
588
				  struct nlattr *est, char *name, int ovr,
589
				  int bind, struct list_head *actions)
L
Linus Torvalds 已提交
590
{
E
Eric Dumazet 已提交
591
	struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
592
	struct tc_action *act;
593
	int err;
L
Linus Torvalds 已提交
594 595
	int i;

596 597
	err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
	if (err < 0)
598
		return err;
L
Linus Torvalds 已提交
599

600
	for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
601
		act = tcf_action_init_1(net, tb[i], est, name, ovr, bind);
602 603
		if (IS_ERR(act)) {
			err = PTR_ERR(act);
L
Linus Torvalds 已提交
604
			goto err;
605
		}
606
		act->order = i;
607
		list_add_tail(&act->list, actions);
L
Linus Torvalds 已提交
608
	}
609
	return 0;
L
Linus Torvalds 已提交
610 611

err:
612 613
	tcf_action_destroy(actions, bind);
	return err;
L
Linus Torvalds 已提交
614 615 616 617 618 619 620
}

int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *a,
			  int compat_mode)
{
	int err = 0;
	struct gnet_dump d;
621
	struct tcf_common *p = a->priv;
622

623
	if (p == NULL)
L
Linus Torvalds 已提交
624 625 626
		goto errout;

	/* compat_mode being true specifies a call that is supposed
627
	 * to add additional backward compatibility statistic TLVs.
L
Linus Torvalds 已提交
628 629 630 631
	 */
	if (compat_mode) {
		if (a->type == TCA_OLD_COMPAT)
			err = gnet_stats_start_copy_compat(skb, 0,
632
				TCA_STATS, TCA_XSTATS, &p->tcfc_lock, &d);
L
Linus Torvalds 已提交
633 634 635 636
		else
			return 0;
	} else
		err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
637
					    &p->tcfc_lock, &d);
L
Linus Torvalds 已提交
638 639 640 641

	if (err < 0)
		goto errout;

642
	if (gnet_stats_copy_basic(&d, p->cpu_bstats, &p->tcfc_bstats) < 0 ||
643 644
	    gnet_stats_copy_rate_est(&d, &p->tcfc_bstats,
				     &p->tcfc_rate_est) < 0 ||
645
	    gnet_stats_copy_queue(&d, p->cpu_qstats,
646 647
				  &p->tcfc_qstats,
				  p->tcfc_qstats.qlen) < 0)
L
Linus Torvalds 已提交
648 649 650 651 652 653 654 655 656 657 658 659
		goto errout;

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

	return 0;

errout:
	return -1;
}

static int
660
tca_get_fill(struct sk_buff *skb, struct list_head *actions, u32 portid, u32 seq,
661
	     u16 flags, int event, int bind, int ref)
L
Linus Torvalds 已提交
662 663 664
{
	struct tcamsg *t;
	struct nlmsghdr *nlh;
665
	unsigned char *b = skb_tail_pointer(skb);
666
	struct nlattr *nest;
L
Linus Torvalds 已提交
667

668
	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
669 670 671
	if (!nlh)
		goto out_nlmsg_trim;
	t = nlmsg_data(nlh);
L
Linus Torvalds 已提交
672
	t->tca_family = AF_UNSPEC;
673 674
	t->tca__pad1 = 0;
	t->tca__pad2 = 0;
675

676 677
	nest = nla_nest_start(skb, TCA_ACT_TAB);
	if (nest == NULL)
678
		goto out_nlmsg_trim;
L
Linus Torvalds 已提交
679

680
	if (tcf_action_dump(skb, actions, bind, ref) < 0)
681
		goto out_nlmsg_trim;
L
Linus Torvalds 已提交
682

683
	nla_nest_end(skb, nest);
684

685
	nlh->nlmsg_len = skb_tail_pointer(skb) - b;
L
Linus Torvalds 已提交
686 687
	return skb->len;

688
out_nlmsg_trim:
689
	nlmsg_trim(skb, b);
L
Linus Torvalds 已提交
690 691 692 693
	return -1;
}

static int
694
act_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
695
	       struct list_head *actions, int event)
L
Linus Torvalds 已提交
696 697 698 699 700 701
{
	struct sk_buff *skb;

	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
	if (!skb)
		return -ENOBUFS;
702
	if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event, 0, 0) <= 0) {
L
Linus Torvalds 已提交
703 704 705
		kfree_skb(skb);
		return -EINVAL;
	}
706

707
	return rtnl_unicast(skb, net, portid);
L
Linus Torvalds 已提交
708 709
}

710 711 712 713 714 715 716 717 718 719 720 721 722 723
static struct tc_action *create_a(int i)
{
	struct tc_action *act;

	act = kzalloc(sizeof(*act), GFP_KERNEL);
	if (act == NULL) {
		pr_debug("create_a: failed to alloc!\n");
		return NULL;
	}
	act->order = i;
	INIT_LIST_HEAD(&act->list);
	return act;
}

L
Linus Torvalds 已提交
724
static struct tc_action *
725
tcf_action_get_1(struct nlattr *nla, struct nlmsghdr *n, u32 portid)
L
Linus Torvalds 已提交
726
{
E
Eric Dumazet 已提交
727
	struct nlattr *tb[TCA_ACT_MAX + 1];
L
Linus Torvalds 已提交
728 729
	struct tc_action *a;
	int index;
730
	int err;
L
Linus Torvalds 已提交
731

732 733
	err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
	if (err < 0)
734
		goto err_out;
L
Linus Torvalds 已提交
735

736
	err = -EINVAL;
737 738
	if (tb[TCA_ACT_INDEX] == NULL ||
	    nla_len(tb[TCA_ACT_INDEX]) < sizeof(index))
739
		goto err_out;
740
	index = nla_get_u32(tb[TCA_ACT_INDEX]);
L
Linus Torvalds 已提交
741

742
	err = -ENOMEM;
743
	a = create_a(0);
L
Linus Torvalds 已提交
744
	if (a == NULL)
745
		goto err_out;
L
Linus Torvalds 已提交
746

747
	err = -EINVAL;
748
	a->ops = tc_lookup_action(tb[TCA_ACT_KIND]);
749
	if (a->ops == NULL) /* could happen in batch of actions */
L
Linus Torvalds 已提交
750
		goto err_free;
751
	err = -ENOENT;
L
Linus Torvalds 已提交
752 753 754 755 756
	if (a->ops->lookup(a, index) == 0)
		goto err_mod;

	module_put(a->ops->owner);
	return a;
757

L
Linus Torvalds 已提交
758 759 760 761
err_mod:
	module_put(a->ops->owner);
err_free:
	kfree(a);
762 763
err_out:
	return ERR_PTR(err);
L
Linus Torvalds 已提交
764 765
}

766
static void cleanup_a(struct list_head *actions)
L
Linus Torvalds 已提交
767
{
768
	struct tc_action *a, *tmp;
L
Linus Torvalds 已提交
769

770 771
	list_for_each_entry_safe(a, tmp, actions, list) {
		list_del(&a->list);
L
Linus Torvalds 已提交
772 773 774 775
		kfree(a);
	}
}

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

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

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

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

802
	err = -EINVAL;
803
	kind = tb[TCA_ACT_KIND];
804 805 806 807
	memset(&a, 0, sizeof(struct tc_action));
	INIT_LIST_HEAD(&a.list);
	a.ops = tc_lookup_action(kind);
	if (a.ops == NULL) /*some idjot trying to flush unknown action */
L
Linus Torvalds 已提交
808 809
		goto err_out;

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 = a.ops->walk(skb, &dcb, RTM_DELACTION, &a);
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(a.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(a.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(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
		list_add_tail(&act->list, &actions);
L
Linus Torvalds 已提交
907 908 909
	}

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

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

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

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

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

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

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

	/* dump then free all the actions after update; inserted policy
	 * stays intact
E
Eric Dumazet 已提交
959
	 */
960
	ret = tcf_add_notify(net, n, &actions, portid);
961
	cleanup_a(&actions);
L
Linus Torvalds 已提交
962 963 964 965
done:
	return ret;
}

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

973
	if ((n->nlmsg_type != RTM_GETACTION) && !netlink_capable(skb, CAP_NET_ADMIN))
974 975
		return -EPERM;

976 977 978 979 980
	ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ACT_MAX, NULL);
	if (ret < 0)
		return ret;

	if (tca[TCA_ACT_TAB] == NULL) {
981
		pr_notice("tc_ctl_action: received NO action attribs\n");
L
Linus Torvalds 已提交
982 983 984
		return -EINVAL;
	}

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

	return ret;
}

1016
static struct nlattr *
1017
find_dump_kind(const struct nlmsghdr *n)
L
Linus Torvalds 已提交
1018
{
E
Eric Dumazet 已提交
1019
	struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
1020 1021 1022
	struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
	struct nlattr *nla[TCAA_MAX + 1];
	struct nlattr *kind;
L
Linus Torvalds 已提交
1023

1024
	if (nlmsg_parse(n, sizeof(struct tcamsg), nla, TCAA_MAX, NULL) < 0)
L
Linus Torvalds 已提交
1025
		return NULL;
1026
	tb1 = nla[TCA_ACT_TAB];
L
Linus Torvalds 已提交
1027 1028 1029
	if (tb1 == NULL)
		return NULL;

1030 1031
	if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
		      NLMSG_ALIGN(nla_len(tb1)), NULL) < 0)
L
Linus Torvalds 已提交
1032 1033
		return NULL;

1034 1035 1036 1037
	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 已提交
1038
		return NULL;
1039
	kind = tb2[TCA_ACT_KIND];
L
Linus Torvalds 已提交
1040

1041
	return kind;
L
Linus Torvalds 已提交
1042 1043 1044 1045 1046 1047
}

static int
tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
{
	struct nlmsghdr *nlh;
1048
	unsigned char *b = skb_tail_pointer(skb);
1049
	struct nlattr *nest;
L
Linus Torvalds 已提交
1050 1051 1052
	struct tc_action_ops *a_o;
	struct tc_action a;
	int ret = 0;
1053
	struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
1054
	struct nlattr *kind = find_dump_kind(cb->nlh);
L
Linus Torvalds 已提交
1055 1056

	if (kind == NULL) {
1057
		pr_info("tc_dump_action: action bad kind\n");
L
Linus Torvalds 已提交
1058 1059 1060
		return 0;
	}

1061
	a_o = tc_lookup_action(kind);
E
Eric Dumazet 已提交
1062
	if (a_o == NULL)
L
Linus Torvalds 已提交
1063 1064 1065 1066 1067
		return 0;

	memset(&a, 0, sizeof(struct tc_action));
	a.ops = a_o;

1068
	nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
1069 1070 1071 1072
			cb->nlh->nlmsg_type, sizeof(*t), 0);
	if (!nlh)
		goto out_module_put;
	t = nlmsg_data(nlh);
L
Linus Torvalds 已提交
1073
	t->tca_family = AF_UNSPEC;
1074 1075
	t->tca__pad1 = 0;
	t->tca__pad2 = 0;
L
Linus Torvalds 已提交
1076

1077 1078
	nest = nla_nest_start(skb, TCA_ACT_TAB);
	if (nest == NULL)
1079
		goto out_module_put;
L
Linus Torvalds 已提交
1080 1081 1082

	ret = a_o->walk(skb, cb, RTM_GETACTION, &a);
	if (ret < 0)
1083
		goto out_module_put;
L
Linus Torvalds 已提交
1084 1085

	if (ret > 0) {
1086
		nla_nest_end(skb, nest);
L
Linus Torvalds 已提交
1087 1088
		ret = skb->len;
	} else
1089
		nla_nest_cancel(skb, nest);
L
Linus Torvalds 已提交
1090

1091
	nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1092
	if (NETLINK_CB(cb->skb).portid && ret)
L
Linus Torvalds 已提交
1093 1094 1095 1096
		nlh->nlmsg_flags |= NLM_F_MULTI;
	module_put(a_o->owner);
	return skb->len;

1097
out_module_put:
L
Linus Torvalds 已提交
1098
	module_put(a_o->owner);
1099
	nlmsg_trim(skb, b);
L
Linus Torvalds 已提交
1100 1101 1102 1103 1104
	return skb->len;
}

static int __init tc_action_init(void)
{
1105 1106 1107 1108
	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 已提交
1109 1110 1111 1112 1113

	return 0;
}

subsys_initcall(tc_action_init);