act_api.c 24.5 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 tcf_hashinfo *hinfo, struct tc_action *a)
40
{
41 42
	struct tcf_common *p = a->priv;

43 44 45 46 47 48 49 50 51
	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
	 */
52
	call_rcu(&p->tcfc_rcu, free_tcf);
53 54
}

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

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

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

75 76
	return ret;
}
77
EXPORT_SYMBOL(__tcf_hash_release);
78

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

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

	s_i = cb->args[0];

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

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

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

122
nla_put_failure:
123
	nla_nest_cancel(skb, nest);
124 125 126
	goto done;
}

127 128
static int tcf_del_walker(struct tcf_hashinfo *hinfo, struct sk_buff *skb,
			  struct tc_action *a)
129
{
130 131 132
	struct hlist_head *head;
	struct hlist_node *n;
	struct tcf_common *p;
133
	struct nlattr *nest;
E
Eric Dumazet 已提交
134
	int i = 0, n_i = 0;
135
	int ret = -EINVAL;
136

137 138 139
	nest = nla_nest_start(skb, a->order);
	if (nest == NULL)
		goto nla_put_failure;
140 141
	if (nla_put_string(skb, TCA_KIND, a->ops->kind))
		goto nla_put_failure;
142
	for (i = 0; i < (hinfo->hmask + 1); i++) {
143 144
		head = &hinfo->htab[tcf_hash(i, hinfo->hmask)];
		hlist_for_each_entry_safe(p, n, head, tcfc_head) {
145
			a->priv = p;
146
			ret = __tcf_hash_release(a, false, true);
147
			if (ret == ACT_P_DELETED) {
E
Eric Dumazet 已提交
148
				module_put(a->ops->owner);
149
				n_i++;
150 151
			} else if (ret < 0)
				goto nla_put_failure;
152 153
		}
	}
154 155
	if (nla_put_u32(skb, TCA_FCNT, n_i))
		goto nla_put_failure;
156
	nla_nest_end(skb, nest);
157 158

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

164 165 166
int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb,
		       struct netlink_callback *cb, int type,
		       struct tc_action *a)
167
{
168 169 170 171
	struct tcf_hashinfo *hinfo = tn->hinfo;

	a->hinfo = hinfo;

172
	if (type == RTM_DELACTION) {
173
		return tcf_del_walker(hinfo, skb, a);
174
	} else if (type == RTM_GETACTION) {
175
		return tcf_dump_walker(hinfo, skb, cb, a);
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 tcf_common *tcf_hash_lookup(u32 index, struct tcf_hashinfo *hinfo)
184
{
185 186
	struct tcf_common *p = NULL;
	struct hlist_head *head;
187

188 189 190
	spin_lock_bh(&hinfo->lock);
	head = &hinfo->htab[tcf_hash(index, hinfo->hmask)];
	hlist_for_each_entry_rcu(p, head, tcfc_head)
191 192
		if (p->tcfc_index == index)
			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 217 218 219
	struct tcf_common *p = tcf_hash_lookup(index, hinfo);

	if (p) {
		a->priv = p;
220
		a->hinfo = hinfo;
221 222 223 224
		return 1;
	}
	return 0;
}
225
EXPORT_SYMBOL(tcf_hash_search);
226

227 228
int tcf_hash_check(struct tc_action_net *tn, u32 index, struct tc_action *a,
		   int bind)
229
{
230
	struct tcf_hashinfo *hinfo = tn->hinfo;
231 232
	struct tcf_common *p = NULL;
	if (index && (p = tcf_hash_lookup(index, hinfo)) != NULL) {
233
		if (bind)
234
			p->tcfc_bindcnt++;
235
		p->tcfc_refcnt++;
236
		a->priv = p;
237
		a->hinfo = hinfo;
238
		return 1;
239
	}
240
	return 0;
241 242 243
}
EXPORT_SYMBOL(tcf_hash_check);

244 245 246 247 248 249
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);
250
	call_rcu(&pc->tcfc_rcu, free_tcf);
251 252 253
}
EXPORT_SYMBOL(tcf_hash_cleanup);

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

	if (unlikely(!p))
262
		return -ENOMEM;
263 264 265 266
	p->tcfc_refcnt = 1;
	if (bind)
		p->tcfc_bindcnt = 1;

267 268 269 270 271 272 273 274 275 276 277 278 279 280
	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;
		}
	}
281
	spin_lock_init(&p->tcfc_lock);
282
	INIT_HLIST_NODE(&p->tcfc_head);
283
	p->tcfc_index = index ? index : tcf_hash_new_index(tn);
284 285
	p->tcfc_tm.install = jiffies;
	p->tcfc_tm.lastuse = jiffies;
286
	if (est) {
287 288 289
		err = gen_new_estimator(&p->tcfc_bstats, p->cpu_bstats,
					&p->tcfc_rate_est,
					&p->tcfc_lock, est);
290
		if (err) {
291 292
			free_percpu(p->cpu_qstats);
			goto err2;
293 294 295
		}
	}

296
	a->priv = (void *) p;
297
	a->hinfo = hinfo;
298
	return 0;
299 300 301
}
EXPORT_SYMBOL(tcf_hash_create);

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

308 309 310
	spin_lock_bh(&hinfo->lock);
	hlist_add_head(&p->tcfc_head, &hinfo->htab[h]);
	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
{
	struct tc_action a = {
		.ops = ops,
319
		.hinfo = hinfo,
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
	};
	int i;

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

		hlist_for_each_entry_safe(p, n, &hinfo->htab[i], tcfc_head) {
			int ret;

			a.priv = p;
			ret = __tcf_hash_release(&a, false, true);
			if (ret == ACT_P_DELETED)
				module_put(ops->owner);
			else if (ret < 0)
				return;
		}
	}
	kfree(hinfo->htab);
}
340
EXPORT_SYMBOL(tcf_hashinfo_destroy);
341

342
static LIST_HEAD(act_base);
L
Linus Torvalds 已提交
343 344
static DEFINE_RWLOCK(act_mod_lock);

345 346
int tcf_register_action(struct tc_action_ops *act,
			struct pernet_operations *ops)
L
Linus Torvalds 已提交
347
{
348
	struct tc_action_ops *a;
349
	int ret;
L
Linus Torvalds 已提交
350

351
	if (!act->act || !act->dump || !act->init || !act->walk || !act->lookup)
352 353
		return -EINVAL;

L
Linus Torvalds 已提交
354
	write_lock(&act_mod_lock);
355
	list_for_each_entry(a, &act_base, head) {
L
Linus Torvalds 已提交
356 357 358 359 360
		if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
			write_unlock(&act_mod_lock);
			return -EEXIST;
		}
	}
361
	list_add_tail(&act->head, &act_base);
L
Linus Torvalds 已提交
362
	write_unlock(&act_mod_lock);
363 364 365 366 367 368 369

	ret = register_pernet_subsys(ops);
	if (ret) {
		tcf_unregister_action(act, ops);
		return ret;
	}

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

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

380 381
	unregister_pernet_subsys(ops);

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

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

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

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

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

433
int tcf_action_exec(struct sk_buff *skb, const struct list_head *actions,
434
		    struct tcf_result *res)
L
Linus Torvalds 已提交
435
{
436
	const struct tc_action *a;
L
Linus Torvalds 已提交
437 438 439 440 441 442 443
	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;
	}
444
	list_for_each_entry(a, actions, list) {
L
Linus Torvalds 已提交
445
repeat:
446 447 448 449 450
		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 已提交
451 452 453 454
	}
exec_done:
	return ret;
}
455
EXPORT_SYMBOL(tcf_action_exec);
L
Linus Torvalds 已提交
456

457
int tcf_action_destroy(struct list_head *actions, int bind)
L
Linus Torvalds 已提交
458
{
459
	struct tc_action *a, *tmp;
460
	int ret = 0;
L
Linus Torvalds 已提交
461

462
	list_for_each_entry_safe(a, tmp, actions, list) {
463
		ret = __tcf_hash_release(a, bind, true);
464
		if (ret == ACT_P_DELETED)
465
			module_put(a->ops->owner);
466 467
		else if (ret < 0)
			return ret;
468 469
		list_del(&a->list);
		kfree(a);
L
Linus Torvalds 已提交
470
	}
471
	return ret;
L
Linus Torvalds 已提交
472 473 474 475 476 477 478 479 480 481 482 483
}

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

487 488
	if (nla_put_string(skb, TCA_KIND, a->ops->kind))
		goto nla_put_failure;
L
Linus Torvalds 已提交
489
	if (tcf_action_copy_stats(skb, a, 0))
490
		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

int
507
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
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 已提交
535 536 537 538
{
	struct tc_action *a;
	struct tc_action_ops *a_o;
	char act_name[IFNAMSIZ];
E
Eric Dumazet 已提交
539
	struct nlattr *tb[TCA_ACT_MAX + 1];
540
	struct nlattr *kind;
541
	int err;
L
Linus Torvalds 已提交
542 543

	if (name == NULL) {
544 545
		err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
		if (err < 0)
L
Linus Torvalds 已提交
546
			goto err_out;
547
		err = -EINVAL;
548
		kind = tb[TCA_ACT_KIND];
L
Linus Torvalds 已提交
549 550
		if (kind == NULL)
			goto err_out;
551
		if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
L
Linus Torvalds 已提交
552 553
			goto err_out;
	} else {
554
		err = -EINVAL;
L
Linus Torvalds 已提交
555 556 557 558 559 560
		if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
			goto err_out;
	}

	a_o = tc_lookup_action_n(act_name);
	if (a_o == NULL) {
561
#ifdef CONFIG_MODULES
L
Linus Torvalds 已提交
562
		rtnl_unlock();
563
		request_module("act_%s", act_name);
L
Linus Torvalds 已提交
564 565 566 567 568 569 570 571 572 573 574
		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) {
575
			err = -EAGAIN;
L
Linus Torvalds 已提交
576 577 578
			goto err_mod;
		}
#endif
579
		err = -ENOENT;
L
Linus Torvalds 已提交
580 581 582
		goto err_out;
	}

583
	err = -ENOMEM;
584
	a = kzalloc(sizeof(*a), GFP_KERNEL);
L
Linus Torvalds 已提交
585 586 587
	if (a == NULL)
		goto err_mod;

588
	a->ops = a_o;
589
	INIT_LIST_HEAD(&a->list);
L
Linus Torvalds 已提交
590 591
	/* backward compatibility for policer */
	if (name == NULL)
592
		err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, a, ovr, bind);
L
Linus Torvalds 已提交
593
	else
594
		err = a_o->init(net, nla, est, a, ovr, bind);
595
	if (err < 0)
L
Linus Torvalds 已提交
596 597 598
		goto err_free;

	/* module count goes up only when brand new policy is created
E
Eric Dumazet 已提交
599 600 601
	 * if it exists and is only bound to in a_o->init() then
	 * ACT_P_CREATED is not returned (a zero is).
	 */
602
	if (err != ACT_P_CREATED)
L
Linus Torvalds 已提交
603 604 605 606 607 608 609 610 611
		module_put(a_o->owner);

	return a;

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

615
int tcf_action_init(struct net *net, struct nlattr *nla,
616
				  struct nlattr *est, char *name, int ovr,
617
				  int bind, struct list_head *actions)
L
Linus Torvalds 已提交
618
{
E
Eric Dumazet 已提交
619
	struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
620
	struct tc_action *act;
621
	int err;
L
Linus Torvalds 已提交
622 623
	int i;

624 625
	err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
	if (err < 0)
626
		return err;
L
Linus Torvalds 已提交
627

628
	for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
629
		act = tcf_action_init_1(net, tb[i], est, name, ovr, bind);
630 631
		if (IS_ERR(act)) {
			err = PTR_ERR(act);
L
Linus Torvalds 已提交
632
			goto err;
633
		}
634
		act->order = i;
635
		list_add_tail(&act->list, actions);
L
Linus Torvalds 已提交
636
	}
637
	return 0;
L
Linus Torvalds 已提交
638 639

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

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

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

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

	if (err < 0)
		goto errout;

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

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

	return 0;

errout:
	return -1;
}

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

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

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

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

714
	nla_nest_end(skb, nest);
715

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

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

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

	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
	if (!skb)
		return -ENOBUFS;
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 743 744 745 746 747 748 749 750 751 752 753 754
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;
}

755 756
static struct tc_action *tcf_action_get_1(struct net *net, struct nlattr *nla,
					  struct nlmsghdr *n, u32 portid)
L
Linus Torvalds 已提交
757
{
E
Eric Dumazet 已提交
758
	struct nlattr *tb[TCA_ACT_MAX + 1];
L
Linus Torvalds 已提交
759 760
	struct tc_action *a;
	int index;
761
	int err;
L
Linus Torvalds 已提交
762

763 764
	err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
	if (err < 0)
765
		goto err_out;
L
Linus Torvalds 已提交
766

767
	err = -EINVAL;
768 769
	if (tb[TCA_ACT_INDEX] == NULL ||
	    nla_len(tb[TCA_ACT_INDEX]) < sizeof(index))
770
		goto err_out;
771
	index = nla_get_u32(tb[TCA_ACT_INDEX]);
L
Linus Torvalds 已提交
772

773
	err = -ENOMEM;
774
	a = create_a(0);
L
Linus Torvalds 已提交
775
	if (a == NULL)
776
		goto err_out;
L
Linus Torvalds 已提交
777

778
	err = -EINVAL;
779
	a->ops = tc_lookup_action(tb[TCA_ACT_KIND]);
780
	if (a->ops == NULL) /* could happen in batch of actions */
L
Linus Torvalds 已提交
781
		goto err_free;
782
	err = -ENOENT;
783
	if (a->ops->lookup(net, a, index) == 0)
L
Linus Torvalds 已提交
784 785 786 787
		goto err_mod;

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

L
Linus Torvalds 已提交
789 790 791 792
err_mod:
	module_put(a->ops->owner);
err_free:
	kfree(a);
793 794
err_out:
	return ERR_PTR(err);
L
Linus Torvalds 已提交
795 796
}

797
static void cleanup_a(struct list_head *actions)
L
Linus Torvalds 已提交
798
{
799
	struct tc_action *a, *tmp;
L
Linus Torvalds 已提交
800

801 802
	list_for_each_entry_safe(a, tmp, actions, list) {
		list_del(&a->list);
L
Linus Torvalds 已提交
803 804 805 806
		kfree(a);
	}
}

807
static int tca_action_flush(struct net *net, struct nlattr *nla,
808
			    struct nlmsghdr *n, u32 portid)
L
Linus Torvalds 已提交
809 810 811 812 813 814
{
	struct sk_buff *skb;
	unsigned char *b;
	struct nlmsghdr *nlh;
	struct tcamsg *t;
	struct netlink_callback dcb;
815
	struct nlattr *nest;
E
Eric Dumazet 已提交
816
	struct nlattr *tb[TCA_ACT_MAX + 1];
817
	struct nlattr *kind;
818
	struct tc_action a;
819
	int err = -ENOMEM;
L
Linus Torvalds 已提交
820 821 822

	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
	if (!skb) {
823
		pr_debug("tca_action_flush: failed skb alloc\n");
824
		return err;
L
Linus Torvalds 已提交
825 826
	}

827
	b = skb_tail_pointer(skb);
L
Linus Torvalds 已提交
828

829 830
	err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
	if (err < 0)
L
Linus Torvalds 已提交
831 832
		goto err_out;

833
	err = -EINVAL;
834
	kind = tb[TCA_ACT_KIND];
835 836 837 838
	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 已提交
839 840
		goto err_out;

841
	nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION, sizeof(*t), 0);
842 843 844
	if (!nlh)
		goto out_module_put;
	t = nlmsg_data(nlh);
L
Linus Torvalds 已提交
845
	t->tca_family = AF_UNSPEC;
846 847
	t->tca__pad1 = 0;
	t->tca__pad2 = 0;
L
Linus Torvalds 已提交
848

849 850
	nest = nla_nest_start(skb, TCA_ACT_TAB);
	if (nest == NULL)
851
		goto out_module_put;
L
Linus Torvalds 已提交
852

853
	err = a.ops->walk(net, skb, &dcb, RTM_DELACTION, &a);
L
Linus Torvalds 已提交
854
	if (err < 0)
855
		goto out_module_put;
856 857
	if (err == 0)
		goto noflush_out;
L
Linus Torvalds 已提交
858

859
	nla_nest_end(skb, nest);
L
Linus Torvalds 已提交
860

861
	nlh->nlmsg_len = skb_tail_pointer(skb) - b;
L
Linus Torvalds 已提交
862
	nlh->nlmsg_flags |= NLM_F_ROOT;
863
	module_put(a.ops->owner);
864
	err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
E
Eric Dumazet 已提交
865
			     n->nlmsg_flags & NLM_F_ECHO);
L
Linus Torvalds 已提交
866 867 868 869 870
	if (err > 0)
		return 0;

	return err;

871
out_module_put:
872
	module_put(a.ops->owner);
L
Linus Torvalds 已提交
873
err_out:
874
noflush_out:
L
Linus Torvalds 已提交
875 876 877 878
	kfree_skb(skb);
	return err;
}

879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
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 */
897 898 899 900 901
	ret = tcf_action_destroy(actions, 0);
	if (ret < 0) {
		kfree_skb(skb);
		return ret;
	}
902 903 904 905 906 907 908 909

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

L
Linus Torvalds 已提交
910
static int
911
tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
912
	      u32 portid, int event)
L
Linus Torvalds 已提交
913
{
914
	int i, ret;
E
Eric Dumazet 已提交
915
	struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
916 917
	struct tc_action *act;
	LIST_HEAD(actions);
L
Linus Torvalds 已提交
918

919 920 921
	ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
	if (ret < 0)
		return ret;
L
Linus Torvalds 已提交
922

E
Eric Dumazet 已提交
923
	if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
924
		if (tb[1] != NULL)
925
			return tca_action_flush(net, tb[1], n, portid);
926 927
		else
			return -EINVAL;
L
Linus Torvalds 已提交
928 929
	}

930
	for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
931
		act = tcf_action_get_1(net, tb[i], n, portid);
932 933
		if (IS_ERR(act)) {
			ret = PTR_ERR(act);
L
Linus Torvalds 已提交
934
			goto err;
935
		}
936
		act->order = i;
937
		list_add_tail(&act->list, &actions);
L
Linus Torvalds 已提交
938 939 940
	}

	if (event == RTM_GETACTION)
941
		ret = act_get_notify(net, portid, n, &actions, event);
L
Linus Torvalds 已提交
942
	else { /* delete */
943 944
		ret = tcf_del_notify(net, n, &actions, portid);
		if (ret)
L
Linus Torvalds 已提交
945 946 947 948
			goto err;
		return ret;
	}
err:
949
	cleanup_a(&actions);
L
Linus Torvalds 已提交
950 951 952
	return ret;
}

953 954 955
static int
tcf_add_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
	       u32 portid)
L
Linus Torvalds 已提交
956 957 958 959 960 961 962 963
{
	struct sk_buff *skb;
	int err = 0;

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

964 965 966 967 968
	if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
			 RTM_NEWACTION, 0, 0) <= 0) {
		kfree_skb(skb);
		return -EINVAL;
	}
969

970 971
	err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
			     n->nlmsg_flags & NLM_F_ECHO);
L
Linus Torvalds 已提交
972 973 974 975 976 977
	if (err > 0)
		err = 0;
	return err;
}

static int
978
tcf_action_add(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
979
	       u32 portid, int ovr)
L
Linus Torvalds 已提交
980 981
{
	int ret = 0;
982
	LIST_HEAD(actions);
L
Linus Torvalds 已提交
983

984 985
	ret = tcf_action_init(net, nla, NULL, NULL, ovr, 0, &actions);
	if (ret)
986
		goto done;
L
Linus Torvalds 已提交
987 988 989

	/* dump then free all the actions after update; inserted policy
	 * stays intact
E
Eric Dumazet 已提交
990
	 */
991
	ret = tcf_add_notify(net, n, &actions, portid);
992
	cleanup_a(&actions);
L
Linus Torvalds 已提交
993 994 995 996
done:
	return ret;
}

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

1004
	if ((n->nlmsg_type != RTM_GETACTION) && !netlink_capable(skb, CAP_NET_ADMIN))
1005 1006
		return -EPERM;

1007 1008 1009 1010 1011
	ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ACT_MAX, NULL);
	if (ret < 0)
		return ret;

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

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

	return ret;
}

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

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

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

1065 1066 1067 1068
	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 已提交
1069
		return NULL;
1070
	kind = tb2[TCA_ACT_KIND];
L
Linus Torvalds 已提交
1071

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

static int
tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
{
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 1084
	struct tc_action_ops *a_o;
	struct tc_action a;
	int ret = 0;
1085
	struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
1086
	struct nlattr *kind = find_dump_kind(cb->nlh);
L
Linus Torvalds 已提交
1087 1088

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

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

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

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

1109 1110
	nest = nla_nest_start(skb, TCA_ACT_TAB);
	if (nest == NULL)
1111
		goto out_module_put;
L
Linus Torvalds 已提交
1112

1113
	ret = a_o->walk(net, skb, cb, RTM_GETACTION, &a);
L
Linus Torvalds 已提交
1114
	if (ret < 0)
1115
		goto out_module_put;
L
Linus Torvalds 已提交
1116 1117

	if (ret > 0) {
1118
		nla_nest_end(skb, nest);
L
Linus Torvalds 已提交
1119 1120
		ret = skb->len;
	} else
1121
		nla_nest_cancel(skb, nest);
L
Linus Torvalds 已提交
1122

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

1129
out_module_put:
L
Linus Torvalds 已提交
1130
	module_put(a_o->owner);
1131
	nlmsg_trim(skb, b);
L
Linus Torvalds 已提交
1132 1133 1134 1135 1136
	return skb->len;
}

static int __init tc_action_init(void)
{
1137 1138 1139 1140
	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 已提交
1141 1142 1143 1144 1145

	return 0;
}

subsys_initcall(tc_action_init);