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

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

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

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

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

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

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

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

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

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

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

	s_i = cb->args[0];

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

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

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

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

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

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

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

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

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

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

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

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

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

	return p;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

L
Linus Torvalds 已提交
344
	write_lock(&act_mod_lock);
345
	list_for_each_entry(a, &act_base, head) {
L
Linus Torvalds 已提交
346 347 348 349 350
		if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
			write_unlock(&act_mod_lock);
			return -EEXIST;
		}
	}
351
	list_add_tail(&act->head, &act_base);
L
Linus Torvalds 已提交
352
	write_unlock(&act_mod_lock);
353 354 355 356 357 358 359

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

L
Linus Torvalds 已提交
360 361
	return 0;
}
362
EXPORT_SYMBOL(tcf_register_action);
L
Linus Torvalds 已提交
363

364 365
int tcf_unregister_action(struct tc_action_ops *act,
			  struct pernet_operations *ops)
L
Linus Torvalds 已提交
366
{
367
	struct tc_action_ops *a;
L
Linus Torvalds 已提交
368 369
	int err = -ENOENT;

370 371
	unregister_pernet_subsys(ops);

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

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

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

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

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

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

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

L
Linus Torvalds 已提交
436
repeat:
437 438 439 440 441
		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 已提交
442 443 444 445
	}
exec_done:
	return ret;
}
446
EXPORT_SYMBOL(tcf_action_exec);
L
Linus Torvalds 已提交
447

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

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

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

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

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

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

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

	return 0;

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

521 522 523
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 已提交
524 525 526 527
{
	struct tc_action *a;
	struct tc_action_ops *a_o;
	char act_name[IFNAMSIZ];
E
Eric Dumazet 已提交
528
	struct nlattr *tb[TCA_ACT_MAX + 1];
529
	struct nlattr *kind;
530
	int err;
L
Linus Torvalds 已提交
531 532

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

	a_o = tc_lookup_action_n(act_name);
	if (a_o == NULL) {
550
#ifdef CONFIG_MODULES
L
Linus Torvalds 已提交
551
		rtnl_unlock();
552
		request_module("act_%s", act_name);
L
Linus Torvalds 已提交
553 554 555 556 557 558 559 560 561 562 563
		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) {
564
			err = -EAGAIN;
L
Linus Torvalds 已提交
565 566 567
			goto err_mod;
		}
#endif
568
		err = -ENOENT;
L
Linus Torvalds 已提交
569 570 571 572 573
		goto err_out;
	}

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

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

	return a;

err_mod:
	module_put(a_o->owner);
err_out:
592
	return ERR_PTR(err);
L
Linus Torvalds 已提交
593 594
}

595 596 597 598 599 600 601 602 603 604 605
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 已提交
606 607
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 已提交
608
{
E
Eric Dumazet 已提交
609
	struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
610
	struct tc_action *act;
611
	int err;
L
Linus Torvalds 已提交
612 613
	int i;

614 615
	err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
	if (err < 0)
616
		return err;
L
Linus Torvalds 已提交
617

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

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

err:
637 638
	tcf_action_destroy(actions, bind);
	return err;
L
Linus Torvalds 已提交
639 640
}

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

647
	if (p == NULL)
L
Linus Torvalds 已提交
648 649 650
		goto errout;

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

	if (err < 0)
		goto errout;

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

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

	return 0;

errout:
	return -1;
}

686 687 688
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 已提交
689 690 691
{
	struct tcamsg *t;
	struct nlmsghdr *nlh;
692
	unsigned char *b = skb_tail_pointer(skb);
693
	struct nlattr *nest;
L
Linus Torvalds 已提交
694

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

703 704
	nest = nla_nest_start(skb, TCA_ACT_TAB);
	if (nest == NULL)
705
		goto out_nlmsg_trim;
L
Linus Torvalds 已提交
706

707
	if (tcf_action_dump(skb, actions, bind, ref) < 0)
708
		goto out_nlmsg_trim;
L
Linus Torvalds 已提交
709

710
	nla_nest_end(skb, nest);
711

712
	nlh->nlmsg_len = skb_tail_pointer(skb) - b;
L
Linus Torvalds 已提交
713 714
	return skb->len;

715
out_nlmsg_trim:
716
	nlmsg_trim(skb, b);
L
Linus Torvalds 已提交
717 718 719 720
	return -1;
}

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

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

735
	return rtnl_unicast(skb, net, portid);
L
Linus Torvalds 已提交
736 737
}

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

747 748
	err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
	if (err < 0)
749
		goto err_out;
L
Linus Torvalds 已提交
750

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

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

765
	module_put(ops->owner);
L
Linus Torvalds 已提交
766
	return a;
767

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

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

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

794
	b = skb_tail_pointer(skb);
L
Linus Torvalds 已提交
795

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

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

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

815 816
	nest = nla_nest_start(skb, TCA_ACT_TAB);
	if (nest == NULL)
817
		goto out_module_put;
L
Linus Torvalds 已提交
818

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

825
	nla_nest_end(skb, nest);
L
Linus Torvalds 已提交
826

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

	return err;

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

845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862
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 */
863 864 865 866 867
	ret = tcf_action_destroy(actions, 0);
	if (ret < 0) {
		kfree_skb(skb);
		return ret;
	}
868 869 870 871 872 873 874 875

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

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

885 886 887
	ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
	if (ret < 0)
		return ret;
L
Linus Torvalds 已提交
888

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

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

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

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

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

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

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

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

951 952
	ret = tcf_action_init(net, nla, NULL, NULL, ovr, 0, &actions);
	if (ret)
953
		return ret;
L
Linus Torvalds 已提交
954

955
	return tcf_add_notify(net, n, &actions, portid);
L
Linus Torvalds 已提交
956 957
}

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

965 966
	if ((n->nlmsg_type != RTM_GETACTION) &&
	    !netlink_capable(skb, CAP_NET_ADMIN))
967 968
		return -EPERM;

969 970 971 972 973
	ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ACT_MAX, NULL);
	if (ret < 0)
		return ret;

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

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

	return ret;
}

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

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

1022 1023
	if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
		      NLMSG_ALIGN(nla_len(tb1)), NULL) < 0)
L
Linus Torvalds 已提交
1024 1025
		return NULL;

1026 1027 1028 1029
	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 已提交
1030
		return NULL;
1031
	kind = tb2[TCA_ACT_KIND];
L
Linus Torvalds 已提交
1032

1033
	return kind;
L
Linus Torvalds 已提交
1034 1035
}

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

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

1052
	a_o = tc_lookup_action(kind);
E
Eric Dumazet 已提交
1053
	if (a_o == NULL)
L
Linus Torvalds 已提交
1054 1055
		return 0;

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

1065 1066
	nest = nla_nest_start(skb, TCA_ACT_TAB);
	if (nest == NULL)
1067
		goto out_module_put;
L
Linus Torvalds 已提交
1068

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

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

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

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

static int __init tc_action_init(void)
{
1093 1094 1095 1096
	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 已提交
1097 1098 1099 1100 1101

	return 0;
}

subsys_initcall(tc_action_init);