act_api.c 24.0 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
	struct tcf_common *p = (struct tcf_common *)a;
42

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 = (struct tcf_common *)a;
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
			list_del(&a->list);
71
			tcf_hash_destroy(a->hinfo, a);
72
			ret = ACT_P_DELETED;
73 74
		}
	}
75

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

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

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

	s_i = cb->args[0];

	for (i = 0; i < (hinfo->hmask + 1); i++) {
91 92 93
		struct hlist_head *head;
		struct tcf_common *p;

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

96
		hlist_for_each_entry_rcu(p, head, tcfc_head) {
97 98 99
			index++;
			if (index < s_i)
				continue;
100

101
			nest = nla_nest_start(skb, n_i);
102 103
			if (nest == NULL)
				goto nla_put_failure;
104
			err = tcf_action_dump_1(skb, (struct tc_action *)p, 0, 0);
105 106
			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
static int tcf_del_walker(struct tcf_hashinfo *hinfo, struct sk_buff *skb,
128
			  const struct tc_action_ops *ops)
129
{
130
	struct nlattr *nest;
E
Eric Dumazet 已提交
131
	int i = 0, n_i = 0;
132
	int ret = -EINVAL;
133

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

144 145
		head = &hinfo->htab[tcf_hash(i, hinfo->hmask)];
		hlist_for_each_entry_safe(p, n, head, tcfc_head) {
146
			ret = __tcf_hash_release((struct tc_action *)p, false, true);
147
			if (ret == ACT_P_DELETED) {
148
				module_put(p->tcfc_act.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
int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb,
		       struct netlink_callback *cb, int type,
166
		       const struct tc_action_ops *ops)
167
{
168 169
	struct tcf_hashinfo *hinfo = tn->hinfo;

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

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

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

	return p;
}

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

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

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

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

	if (p) {
217
		*a = &p->tcfc_act;
218 219 220 221
		return 1;
	}
	return 0;
}
222
EXPORT_SYMBOL(tcf_hash_search);
223

224
bool tcf_hash_check(struct tc_action_net *tn, u32 index, struct tc_action **a,
225
		    int bind)
226
{
227
	struct tcf_hashinfo *hinfo = tn->hinfo;
228 229
	struct tcf_common *p = NULL;
	if (index && (p = tcf_hash_lookup(index, hinfo)) != NULL) {
230
		if (bind)
231
			p->tcfc_bindcnt++;
232
		p->tcfc_refcnt++;
233
		*a = &p->tcfc_act;
234
		return true;
235
	}
236
	return false;
237 238 239
}
EXPORT_SYMBOL(tcf_hash_check);

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

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

	if (unlikely(!p))
259
		return -ENOMEM;
260 261 262 263
	p->tcfc_refcnt = 1;
	if (bind)
		p->tcfc_bindcnt = 1;

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

294 295 296 297
	p->tcfc_act.hinfo = hinfo;
	p->tcfc_act.ops = ops;
	INIT_LIST_HEAD(&p->tcfc_act.list);
	*a = &p->tcfc_act;
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 = (struct tcf_common *)a;
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 319 320 321 322 323 324 325
{
	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;

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

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

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

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

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

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

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

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

375 376
	unregister_pernet_subsys(ops);

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

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

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

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

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

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

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

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

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

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

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

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

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

	return 0;

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

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

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

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

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

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

	return a;

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

599
int tcf_action_init(struct net *net, struct nlattr *nla,
600
				  struct nlattr *est, char *name, int ovr,
601
				  int bind, struct list_head *actions)
L
Linus Torvalds 已提交
602
{
E
Eric Dumazet 已提交
603
	struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
604
	struct tc_action *act;
605
	int err;
L
Linus Torvalds 已提交
606 607
	int i;

608 609
	err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
	if (err < 0)
610
		return err;
L
Linus Torvalds 已提交
611

612
	for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
613
		act = tcf_action_init_1(net, tb[i], est, name, ovr, bind);
614 615
		if (IS_ERR(act)) {
			err = PTR_ERR(act);
L
Linus Torvalds 已提交
616
			goto err;
617
		}
618
		act->order = i;
619
		list_add_tail(&act->list, actions);
L
Linus Torvalds 已提交
620
	}
621
	return 0;
L
Linus Torvalds 已提交
622 623

err:
624 625
	tcf_action_destroy(actions, bind);
	return err;
L
Linus Torvalds 已提交
626 627 628 629 630 631 632
}

int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *a,
			  int compat_mode)
{
	int err = 0;
	struct gnet_dump d;
633
	struct tcf_common *p = (struct tcf_common *)a;
634

635
	if (p == NULL)
L
Linus Torvalds 已提交
636 637 638
		goto errout;

	/* compat_mode being true specifies a call that is supposed
639
	 * to add additional backward compatibility statistic TLVs.
L
Linus Torvalds 已提交
640 641 642 643
	 */
	if (compat_mode) {
		if (a->type == TCA_OLD_COMPAT)
			err = gnet_stats_start_copy_compat(skb, 0,
644 645 646 647
							   TCA_STATS,
							   TCA_XSTATS,
							   &p->tcfc_lock, &d,
							   TCA_PAD);
L
Linus Torvalds 已提交
648 649 650 651
		else
			return 0;
	} else
		err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
652
					    &p->tcfc_lock, &d, TCA_ACT_PAD);
L
Linus Torvalds 已提交
653 654 655 656

	if (err < 0)
		goto errout;

657
	if (gnet_stats_copy_basic(NULL, &d, p->cpu_bstats, &p->tcfc_bstats) < 0 ||
658 659
	    gnet_stats_copy_rate_est(&d, &p->tcfc_bstats,
				     &p->tcfc_rate_est) < 0 ||
660
	    gnet_stats_copy_queue(&d, p->cpu_qstats,
661 662
				  &p->tcfc_qstats,
				  p->tcfc_qstats.qlen) < 0)
L
Linus Torvalds 已提交
663 664 665 666 667 668 669 670 671 672 673
		goto errout;

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

	return 0;

errout:
	return -1;
}

674 675 676
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 已提交
677 678 679
{
	struct tcamsg *t;
	struct nlmsghdr *nlh;
680
	unsigned char *b = skb_tail_pointer(skb);
681
	struct nlattr *nest;
L
Linus Torvalds 已提交
682

683
	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
684 685 686
	if (!nlh)
		goto out_nlmsg_trim;
	t = nlmsg_data(nlh);
L
Linus Torvalds 已提交
687
	t->tca_family = AF_UNSPEC;
688 689
	t->tca__pad1 = 0;
	t->tca__pad2 = 0;
690

691 692
	nest = nla_nest_start(skb, TCA_ACT_TAB);
	if (nest == NULL)
693
		goto out_nlmsg_trim;
L
Linus Torvalds 已提交
694

695
	if (tcf_action_dump(skb, actions, bind, ref) < 0)
696
		goto out_nlmsg_trim;
L
Linus Torvalds 已提交
697

698
	nla_nest_end(skb, nest);
699

700
	nlh->nlmsg_len = skb_tail_pointer(skb) - b;
L
Linus Torvalds 已提交
701 702
	return skb->len;

703
out_nlmsg_trim:
704
	nlmsg_trim(skb, b);
L
Linus Torvalds 已提交
705 706 707 708
	return -1;
}

static int
709
act_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
710
	       struct list_head *actions, int event)
L
Linus Torvalds 已提交
711 712 713 714 715 716
{
	struct sk_buff *skb;

	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
	if (!skb)
		return -ENOBUFS;
717 718
	if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event,
			 0, 0) <= 0) {
L
Linus Torvalds 已提交
719 720 721
		kfree_skb(skb);
		return -EINVAL;
	}
722

723
	return rtnl_unicast(skb, net, portid);
L
Linus Torvalds 已提交
724 725
}

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

735 736
	err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
	if (err < 0)
737
		goto err_out;
L
Linus Torvalds 已提交
738

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

745
	err = -EINVAL;
746 747 748
	ops = tc_lookup_action(tb[TCA_ACT_KIND]);
	if (!ops) /* could happen in batch of actions */
		goto err_out;
749
	err = -ENOENT;
750
	if (ops->lookup(net, &a, index) == 0)
L
Linus Torvalds 已提交
751 752
		goto err_mod;

753
	module_put(ops->owner);
L
Linus Torvalds 已提交
754
	return a;
755

L
Linus Torvalds 已提交
756
err_mod:
757
	module_put(ops->owner);
758 759
err_out:
	return ERR_PTR(err);
L
Linus Torvalds 已提交
760 761
}

762
static void cleanup_a(struct list_head *actions)
L
Linus Torvalds 已提交
763
{
764
	struct tc_action *a, *tmp;
L
Linus Torvalds 已提交
765

766 767
	list_for_each_entry_safe(a, tmp, actions, list) {
		list_del(&a->list);
L
Linus Torvalds 已提交
768 769 770 771
		kfree(a);
	}
}

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

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

792
	b = skb_tail_pointer(skb);
L
Linus Torvalds 已提交
793

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

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

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

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

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

823
	nla_nest_end(skb, nest);
L
Linus Torvalds 已提交
824

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

	return err;

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

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

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

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

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

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

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

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

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

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

928 929 930 931 932
	if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
			 RTM_NEWACTION, 0, 0) <= 0) {
		kfree_skb(skb);
		return -EINVAL;
	}
933

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

static int
942
tcf_action_add(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
943
	       u32 portid, int ovr)
L
Linus Torvalds 已提交
944 945
{
	int ret = 0;
946
	LIST_HEAD(actions);
L
Linus Torvalds 已提交
947

948 949
	ret = tcf_action_init(net, nla, NULL, NULL, ovr, 0, &actions);
	if (ret)
950
		goto done;
L
Linus Torvalds 已提交
951 952 953

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

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

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

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

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

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

	return ret;
}

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

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

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

1030 1031 1032 1033
	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 已提交
1034
		return NULL;
1035
	kind = tb2[TCA_ACT_KIND];
L
Linus Torvalds 已提交
1036

1037
	return kind;
L
Linus Torvalds 已提交
1038 1039 1040 1041 1042
}

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

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

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

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

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

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

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

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

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

static int __init tc_action_init(void)
{
1098 1099 1100 1101
	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 已提交
1102 1103 1104 1105 1106

	return 0;
}

subsys_initcall(tc_action_init);