act_api.c 26.4 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
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>
Linus Torvalds's avatar
Linus Torvalds committed
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>
Linus Torvalds's avatar
Linus Torvalds committed
26
#include <net/sch_generic.h>
27
#include <net/pkt_cls.h>
Linus Torvalds's avatar
Linus Torvalds committed
28
#include <net/act_api.h>
29
#include <net/netlink.h>
Linus Torvalds's avatar
Linus Torvalds committed
30

31
32
33
34
35
36
static int tcf_action_goto_chain_init(struct tc_action *a, struct tcf_proto *tp)
{
	u32 chain_index = a->tcfa_action & TC_ACT_EXT_VAL_MASK;

	if (!tp)
		return -EINVAL;
37
	a->goto_chain = tcf_chain_get(tp->chain->block, chain_index, true);
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
	if (!a->goto_chain)
		return -ENOMEM;
	return 0;
}

static void tcf_action_goto_chain_fini(struct tc_action *a)
{
	tcf_chain_put(a->goto_chain);
}

static void tcf_action_goto_chain_exec(const struct tc_action *a,
				       struct tcf_result *res)
{
	const struct tcf_chain *chain = a->goto_chain;

	res->goto_tp = rcu_dereference_bh(chain->filter_chain);
}

56
57
static void free_tcf(struct rcu_head *head)
{
58
	struct tc_action *p = container_of(head, struct tc_action, tcfa_rcu);
59
60
61

	free_percpu(p->cpu_bstats);
	free_percpu(p->cpu_qstats);
62
63
64
65
66

	if (p->act_cookie) {
		kfree(p->act_cookie->data);
		kfree(p->act_cookie);
	}
67
68
	if (p->goto_chain)
		tcf_action_goto_chain_fini(p);
69

70
71
72
	kfree(p);
}

73
static void tcf_hash_destroy(struct tcf_hashinfo *hinfo, struct tc_action *p)
74
{
75
	spin_lock_bh(&hinfo->lock);
76
	hlist_del(&p->tcfa_head);
77
	spin_unlock_bh(&hinfo->lock);
78
	gen_kill_estimator(&p->tcfa_rate_est);
79
	/*
80
	 * gen_estimator est_timer() might access p->tcfa_lock
81
82
	 * or bstats, wait a RCU grace period before freeing p
	 */
83
	call_rcu(&p->tcfa_rcu, free_tcf);
84
85
}

86
int __tcf_hash_release(struct tc_action *p, bool bind, bool strict)
87
88
89
90
91
{
	int ret = 0;

	if (p) {
		if (bind)
92
93
			p->tcfa_bindcnt--;
		else if (strict && p->tcfa_bindcnt > 0)
94
			return -EPERM;
95

96
97
98
99
100
		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);
101
			ret = ACT_P_DELETED;
102
103
		}
	}
104

105
106
	return ret;
}
107
EXPORT_SYMBOL(__tcf_hash_release);
108

109
static int tcf_dump_walker(struct tcf_hashinfo *hinfo, struct sk_buff *skb,
110
			   struct netlink_callback *cb)
111
{
Eric Dumazet's avatar
Eric Dumazet committed
112
	int err = 0, index = -1, i = 0, s_i = 0, n_i = 0;
113
	struct nlattr *nest;
114

115
	spin_lock_bh(&hinfo->lock);
116
117
118
119

	s_i = cb->args[0];

	for (i = 0; i < (hinfo->hmask + 1); i++) {
120
		struct hlist_head *head;
121
		struct tc_action *p;
122

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

125
		hlist_for_each_entry_rcu(p, head, tcfa_head) {
126
127
128
			index++;
			if (index < s_i)
				continue;
129

130
			nest = nla_nest_start(skb, n_i);
131
132
			if (nest == NULL)
				goto nla_put_failure;
133
			err = tcf_action_dump_1(skb, p, 0, 0);
134
135
			if (err < 0) {
				index--;
136
				nlmsg_trim(skb, nest);
137
138
				goto done;
			}
139
			nla_nest_end(skb, nest);
140
141
142
143
144
145
			n_i++;
			if (n_i >= TCA_ACT_MAX_PRIO)
				goto done;
		}
	}
done:
146
	spin_unlock_bh(&hinfo->lock);
147
148
149
150
	if (n_i)
		cb->args[0] += n_i;
	return n_i;

151
nla_put_failure:
152
	nla_nest_cancel(skb, nest);
153
154
155
	goto done;
}

156
static int tcf_del_walker(struct tcf_hashinfo *hinfo, struct sk_buff *skb,
157
			  const struct tc_action_ops *ops)
158
{
159
	struct nlattr *nest;
Eric Dumazet's avatar
Eric Dumazet committed
160
	int i = 0, n_i = 0;
161
	int ret = -EINVAL;
162

163
	nest = nla_nest_start(skb, 0);
164
165
	if (nest == NULL)
		goto nla_put_failure;
166
	if (nla_put_string(skb, TCA_KIND, ops->kind))
167
		goto nla_put_failure;
168
	for (i = 0; i < (hinfo->hmask + 1); i++) {
169
170
		struct hlist_head *head;
		struct hlist_node *n;
171
		struct tc_action *p;
172

173
		head = &hinfo->htab[tcf_hash(i, hinfo->hmask)];
174
175
		hlist_for_each_entry_safe(p, n, head, tcfa_head) {
			ret = __tcf_hash_release(p, false, true);
176
			if (ret == ACT_P_DELETED) {
177
				module_put(p->ops->owner);
178
				n_i++;
179
180
			} else if (ret < 0)
				goto nla_put_failure;
181
182
		}
	}
183
184
	if (nla_put_u32(skb, TCA_FCNT, n_i))
		goto nla_put_failure;
185
	nla_nest_end(skb, nest);
186
187

	return n_i;
188
nla_put_failure:
189
	nla_nest_cancel(skb, nest);
190
	return ret;
191
192
}

193
194
int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb,
		       struct netlink_callback *cb, int type,
195
		       const struct tc_action_ops *ops)
196
{
197
198
	struct tcf_hashinfo *hinfo = tn->hinfo;

199
	if (type == RTM_DELACTION) {
200
		return tcf_del_walker(hinfo, skb, ops);
201
	} else if (type == RTM_GETACTION) {
202
		return tcf_dump_walker(hinfo, skb, cb);
203
	} else {
204
		WARN(1, "tcf_generic_walker: unknown action %d\n", type);
205
206
207
		return -EINVAL;
	}
}
208
EXPORT_SYMBOL(tcf_generic_walker);
209

210
static struct tc_action *tcf_hash_lookup(u32 index, struct tcf_hashinfo *hinfo)
211
{
212
	struct tc_action *p = NULL;
213
	struct hlist_head *head;
214

215
216
	spin_lock_bh(&hinfo->lock);
	head = &hinfo->htab[tcf_hash(index, hinfo->hmask)];
217
218
	hlist_for_each_entry_rcu(p, head, tcfa_head)
		if (p->tcfa_index == index)
219
			break;
220
	spin_unlock_bh(&hinfo->lock);
221
222
223
224

	return p;
}

225
u32 tcf_hash_new_index(struct tc_action_net *tn)
226
{
227
	struct tcf_hashinfo *hinfo = tn->hinfo;
228
	u32 val = hinfo->index;
229
230
231
232
233
234

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

235
	hinfo->index = val;
236
	return val;
237
238
239
}
EXPORT_SYMBOL(tcf_hash_new_index);

240
int tcf_hash_search(struct tc_action_net *tn, struct tc_action **a, u32 index)
241
{
242
	struct tcf_hashinfo *hinfo = tn->hinfo;
243
	struct tc_action *p = tcf_hash_lookup(index, hinfo);
244
245

	if (p) {
246
		*a = p;
247
248
249
250
		return 1;
	}
	return 0;
}
251
EXPORT_SYMBOL(tcf_hash_search);
252

253
bool tcf_hash_check(struct tc_action_net *tn, u32 index, struct tc_action **a,
254
		    int bind)
255
{
256
	struct tcf_hashinfo *hinfo = tn->hinfo;
257
258
	struct tc_action *p = NULL;

259
	if (index && (p = tcf_hash_lookup(index, hinfo)) != NULL) {
260
		if (bind)
261
262
263
			p->tcfa_bindcnt++;
		p->tcfa_refcnt++;
		*a = p;
264
		return true;
265
	}
266
	return false;
267
268
269
}
EXPORT_SYMBOL(tcf_hash_check);

270
271
272
void tcf_hash_cleanup(struct tc_action *a, struct nlattr *est)
{
	if (est)
273
		gen_kill_estimator(&a->tcfa_rate_est);
274
	call_rcu(&a->tcfa_rcu, free_tcf);
275
276
277
}
EXPORT_SYMBOL(tcf_hash_cleanup);

278
int tcf_hash_create(struct tc_action_net *tn, u32 index, struct nlattr *est,
279
280
		    struct tc_action **a, const struct tc_action_ops *ops,
		    int bind, bool cpustats)
281
{
282
	struct tc_action *p = kzalloc(ops->size, GFP_KERNEL);
283
	struct tcf_hashinfo *hinfo = tn->hinfo;
284
	int err = -ENOMEM;
285
286

	if (unlikely(!p))
287
		return -ENOMEM;
288
	p->tcfa_refcnt = 1;
289
	if (bind)
290
		p->tcfa_bindcnt = 1;
291

292
293
294
295
296
297
298
299
300
301
302
303
304
305
	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;
		}
	}
306
307
308
309
310
311
	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;
312
	if (est) {
313
314
315
		err = gen_new_estimator(&p->tcfa_bstats, p->cpu_bstats,
					&p->tcfa_rate_est,
					&p->tcfa_lock, NULL, est);
316
		if (err) {
317
318
			free_percpu(p->cpu_qstats);
			goto err2;
319
320
321
		}
	}

322
323
324
325
	p->hinfo = hinfo;
	p->ops = ops;
	INIT_LIST_HEAD(&p->list);
	*a = p;
326
	return 0;
327
328
329
}
EXPORT_SYMBOL(tcf_hash_create);

330
void tcf_hash_insert(struct tc_action_net *tn, struct tc_action *a)
331
{
332
	struct tcf_hashinfo *hinfo = tn->hinfo;
333
	unsigned int h = tcf_hash(a->tcfa_index, hinfo->hmask);
334

335
	spin_lock_bh(&hinfo->lock);
336
	hlist_add_head(&a->tcfa_head, &hinfo->htab[h]);
337
	spin_unlock_bh(&hinfo->lock);
338
339
}
EXPORT_SYMBOL(tcf_hash_insert);
Linus Torvalds's avatar
Linus Torvalds committed
340

341
342
void tcf_hashinfo_destroy(const struct tc_action_ops *ops,
			  struct tcf_hashinfo *hinfo)
343
344
345
346
{
	int i;

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

350
		hlist_for_each_entry_safe(p, n, &hinfo->htab[i], tcfa_head) {
351
352
			int ret;

353
			ret = __tcf_hash_release(p, false, true);
354
355
356
357
358
359
360
361
			if (ret == ACT_P_DELETED)
				module_put(ops->owner);
			else if (ret < 0)
				return;
		}
	}
	kfree(hinfo->htab);
}
362
EXPORT_SYMBOL(tcf_hashinfo_destroy);
363

364
static LIST_HEAD(act_base);
Linus Torvalds's avatar
Linus Torvalds committed
365
366
static DEFINE_RWLOCK(act_mod_lock);

367
368
int tcf_register_action(struct tc_action_ops *act,
			struct pernet_operations *ops)
Linus Torvalds's avatar
Linus Torvalds committed
369
{
370
	struct tc_action_ops *a;
371
	int ret;
Linus Torvalds's avatar
Linus Torvalds committed
372

373
	if (!act->act || !act->dump || !act->init || !act->walk || !act->lookup)
374
375
		return -EINVAL;

376
377
378
379
380
381
382
383
	/* We have to register pernet ops before making the action ops visible,
	 * otherwise tcf_action_init_1() could get a partially initialized
	 * netns.
	 */
	ret = register_pernet_subsys(ops);
	if (ret)
		return ret;

Linus Torvalds's avatar
Linus Torvalds committed
384
	write_lock(&act_mod_lock);
385
	list_for_each_entry(a, &act_base, head) {
Linus Torvalds's avatar
Linus Torvalds committed
386
387
		if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
			write_unlock(&act_mod_lock);
388
			unregister_pernet_subsys(ops);
Linus Torvalds's avatar
Linus Torvalds committed
389
390
391
			return -EEXIST;
		}
	}
392
	list_add_tail(&act->head, &act_base);
Linus Torvalds's avatar
Linus Torvalds committed
393
	write_unlock(&act_mod_lock);
394

Linus Torvalds's avatar
Linus Torvalds committed
395
396
	return 0;
}
397
EXPORT_SYMBOL(tcf_register_action);
Linus Torvalds's avatar
Linus Torvalds committed
398

399
400
int tcf_unregister_action(struct tc_action_ops *act,
			  struct pernet_operations *ops)
Linus Torvalds's avatar
Linus Torvalds committed
401
{
402
	struct tc_action_ops *a;
Linus Torvalds's avatar
Linus Torvalds committed
403
404
405
	int err = -ENOENT;

	write_lock(&act_mod_lock);
406
407
408
409
	list_for_each_entry(a, &act_base, head) {
		if (a == act) {
			list_del(&act->head);
			err = 0;
Linus Torvalds's avatar
Linus Torvalds committed
410
			break;
411
		}
Linus Torvalds's avatar
Linus Torvalds committed
412
413
	}
	write_unlock(&act_mod_lock);
414
415
	if (!err)
		unregister_pernet_subsys(ops);
Linus Torvalds's avatar
Linus Torvalds committed
416
417
	return err;
}
418
EXPORT_SYMBOL(tcf_unregister_action);
Linus Torvalds's avatar
Linus Torvalds committed
419
420
421
422

/* lookup by name */
static struct tc_action_ops *tc_lookup_action_n(char *kind)
{
423
	struct tc_action_ops *a, *res = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
424
425
426

	if (kind) {
		read_lock(&act_mod_lock);
427
		list_for_each_entry(a, &act_base, head) {
Linus Torvalds's avatar
Linus Torvalds committed
428
			if (strcmp(kind, a->kind) == 0) {
429
430
				if (try_module_get(a->owner))
					res = a;
Linus Torvalds's avatar
Linus Torvalds committed
431
432
433
434
435
				break;
			}
		}
		read_unlock(&act_mod_lock);
	}
436
	return res;
Linus Torvalds's avatar
Linus Torvalds committed
437
438
}

439
440
/* lookup by nlattr */
static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
Linus Torvalds's avatar
Linus Torvalds committed
441
{
442
	struct tc_action_ops *a, *res = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
443
444
445

	if (kind) {
		read_lock(&act_mod_lock);
446
		list_for_each_entry(a, &act_base, head) {
447
			if (nla_strcmp(kind, a->kind) == 0) {
448
449
				if (try_module_get(a->owner))
					res = a;
Linus Torvalds's avatar
Linus Torvalds committed
450
451
452
453
454
				break;
			}
		}
		read_unlock(&act_mod_lock);
	}
455
	return res;
Linus Torvalds's avatar
Linus Torvalds committed
456
457
}

458
459
/*TCA_ACT_MAX_PRIO is 32, there count upto 32 */
#define TCA_ACT_MAX_PRIO_MASK 0x1FF
460
461
int tcf_action_exec(struct sk_buff *skb, struct tc_action **actions,
		    int nr_actions, struct tcf_result *res)
Linus Torvalds's avatar
Linus Torvalds committed
462
{
463
	int ret = -1, i;
464
465
	u32 jmp_prgcnt = 0;
	u32 jmp_ttl = TCA_ACT_MAX_PRIO; /*matches actions per filter */
Linus Torvalds's avatar
Linus Torvalds committed
466

467
468
469
	if (skb_skip_tc_classify(skb))
		return TC_ACT_OK;

470
restart_act_graph:
471
472
473
	for (i = 0; i < nr_actions; i++) {
		const struct tc_action *a = actions[i];

474
475
476
477
		if (jmp_prgcnt > 0) {
			jmp_prgcnt -= 1;
			continue;
		}
Linus Torvalds's avatar
Linus Torvalds committed
478
repeat:
479
480
481
		ret = a->ops->act(skb, a, res);
		if (ret == TC_ACT_REPEAT)
			goto repeat;	/* we need a ttl - JHS */
482

483
		if (TC_ACT_EXT_CMP(ret, TC_ACT_JUMP)) {
484
485
486
487
488
489
490
491
492
493
494
			jmp_prgcnt = ret & TCA_ACT_MAX_PRIO_MASK;
			if (!jmp_prgcnt || (jmp_prgcnt > nr_actions)) {
				/* faulty opcode, stop pipeline */
				return TC_ACT_OK;
			} else {
				jmp_ttl -= 1;
				if (jmp_ttl > 0)
					goto restart_act_graph;
				else /* faulty graph, stop pipeline */
					return TC_ACT_OK;
			}
495
496
		} else if (TC_ACT_EXT_CMP(ret, TC_ACT_GOTO_CHAIN)) {
			tcf_action_goto_chain_exec(a, res);
497
498
		}

499
		if (ret != TC_ACT_PIPE)
500
			break;
Linus Torvalds's avatar
Linus Torvalds committed
501
	}
502

Linus Torvalds's avatar
Linus Torvalds committed
503
504
	return ret;
}
505
EXPORT_SYMBOL(tcf_action_exec);
Linus Torvalds's avatar
Linus Torvalds committed
506

507
int tcf_action_destroy(struct list_head *actions, int bind)
Linus Torvalds's avatar
Linus Torvalds committed
508
{
509
	struct tc_action *a, *tmp;
510
	int ret = 0;
Linus Torvalds's avatar
Linus Torvalds committed
511

512
	list_for_each_entry_safe(a, tmp, actions, list) {
513
		ret = __tcf_hash_release(a, bind, true);
514
		if (ret == ACT_P_DELETED)
515
			module_put(a->ops->owner);
516
517
		else if (ret < 0)
			return ret;
Linus Torvalds's avatar
Linus Torvalds committed
518
	}
519
	return ret;
Linus Torvalds's avatar
Linus Torvalds committed
520
521
522
523
524
525
526
527
528
529
530
531
}

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;
532
	unsigned char *b = skb_tail_pointer(skb);
533
	struct nlattr *nest;
Linus Torvalds's avatar
Linus Torvalds committed
534

535
536
	if (nla_put_string(skb, TCA_KIND, a->ops->kind))
		goto nla_put_failure;
Linus Torvalds's avatar
Linus Torvalds committed
537
	if (tcf_action_copy_stats(skb, a, 0))
538
		goto nla_put_failure;
539
540
541
542
543
544
	if (a->act_cookie) {
		if (nla_put(skb, TCA_ACT_COOKIE, a->act_cookie->len,
			    a->act_cookie->data))
			goto nla_put_failure;
	}

545
546
547
	nest = nla_nest_start(skb, TCA_OPTIONS);
	if (nest == NULL)
		goto nla_put_failure;
Eric Dumazet's avatar
Eric Dumazet committed
548
549
	err = tcf_action_dump_old(skb, a, bind, ref);
	if (err > 0) {
550
		nla_nest_end(skb, nest);
Linus Torvalds's avatar
Linus Torvalds committed
551
552
553
		return err;
	}

554
nla_put_failure:
555
	nlmsg_trim(skb, b);
Linus Torvalds's avatar
Linus Torvalds committed
556
557
	return -1;
}
558
EXPORT_SYMBOL(tcf_action_dump_1);
Linus Torvalds's avatar
Linus Torvalds committed
559

560
561
int tcf_action_dump(struct sk_buff *skb, struct list_head *actions,
		    int bind, int ref)
Linus Torvalds's avatar
Linus Torvalds committed
562
563
564
{
	struct tc_action *a;
	int err = -EINVAL;
565
	struct nlattr *nest;
Linus Torvalds's avatar
Linus Torvalds committed
566

567
	list_for_each_entry(a, actions, list) {
568
569
570
		nest = nla_nest_start(skb, a->order);
		if (nest == NULL)
			goto nla_put_failure;
Linus Torvalds's avatar
Linus Torvalds committed
571
572
		err = tcf_action_dump_1(skb, a, bind, ref);
		if (err < 0)
573
			goto errout;
574
		nla_nest_end(skb, nest);
Linus Torvalds's avatar
Linus Torvalds committed
575
576
577
578
	}

	return 0;

579
nla_put_failure:
580
581
	err = -EINVAL;
errout:
582
	nla_nest_cancel(skb, nest);
583
	return err;
Linus Torvalds's avatar
Linus Torvalds committed
584
585
}

586
static struct tc_cookie *nla_memdup_cookie(struct nlattr **tb)
587
{
588
589
590
591
592
593
594
595
	struct tc_cookie *c = kzalloc(sizeof(*c), GFP_KERNEL);
	if (!c)
		return NULL;

	c->data = nla_memdup(tb[TCA_ACT_COOKIE], GFP_KERNEL);
	if (!c->data) {
		kfree(c);
		return NULL;
596
	}
597
	c->len = nla_len(tb[TCA_ACT_COOKIE]);
598

599
	return c;
600
601
}

602
603
604
struct tc_action *tcf_action_init_1(struct net *net, struct tcf_proto *tp,
				    struct nlattr *nla, struct nlattr *est,
				    char *name, int ovr, int bind)
Linus Torvalds's avatar
Linus Torvalds committed
605
606
607
{
	struct tc_action *a;
	struct tc_action_ops *a_o;
608
	struct tc_cookie *cookie = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
609
	char act_name[IFNAMSIZ];
Eric Dumazet's avatar
Eric Dumazet committed
610
	struct nlattr *tb[TCA_ACT_MAX + 1];
611
	struct nlattr *kind;
612
	int err;
Linus Torvalds's avatar
Linus Torvalds committed
613
614

	if (name == NULL) {
615
		err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, NULL);
616
		if (err < 0)
Linus Torvalds's avatar
Linus Torvalds committed
617
			goto err_out;
618
		err = -EINVAL;
619
		kind = tb[TCA_ACT_KIND];
Linus Torvalds's avatar
Linus Torvalds committed
620
621
		if (kind == NULL)
			goto err_out;
622
		if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
Linus Torvalds's avatar
Linus Torvalds committed
623
			goto err_out;
624
625
626
627
628
629
630
631
632
633
634
635
		if (tb[TCA_ACT_COOKIE]) {
			int cklen = nla_len(tb[TCA_ACT_COOKIE]);

			if (cklen > TC_COOKIE_MAX_SIZE)
				goto err_out;

			cookie = nla_memdup_cookie(tb);
			if (!cookie) {
				err = -ENOMEM;
				goto err_out;
			}
		}
Linus Torvalds's avatar
Linus Torvalds committed
636
	} else {
637
		err = -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
638
639
640
641
642
643
		if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
			goto err_out;
	}

	a_o = tc_lookup_action_n(act_name);
	if (a_o == NULL) {
644
#ifdef CONFIG_MODULES
Linus Torvalds's avatar
Linus Torvalds committed
645
		rtnl_unlock();
646
		request_module("act_%s", act_name);
Linus Torvalds's avatar
Linus Torvalds committed
647
648
649
650
651
652
653
654
655
656
657
		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) {
658
			err = -EAGAIN;
Linus Torvalds's avatar
Linus Torvalds committed
659
660
661
			goto err_mod;
		}
#endif
662
		err = -ENOENT;
Linus Torvalds's avatar
Linus Torvalds committed
663
664
665
666
667
		goto err_out;
	}

	/* backward compatibility for policer */
	if (name == NULL)
668
		err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, &a, ovr, bind);
Linus Torvalds's avatar
Linus Torvalds committed
669
	else
670
		err = a_o->init(net, nla, est, &a, ovr, bind);
671
	if (err < 0)
672
		goto err_mod;
Linus Torvalds's avatar
Linus Torvalds committed
673

674
675
676
677
	if (name == NULL && tb[TCA_ACT_COOKIE]) {
		if (a->act_cookie) {
			kfree(a->act_cookie->data);
			kfree(a->act_cookie);
678
		}
679
		a->act_cookie = cookie;
680
681
	}

Linus Torvalds's avatar
Linus Torvalds committed
682
	/* module count goes up only when brand new policy is created
Eric Dumazet's avatar
Eric Dumazet committed
683
684
685
	 * if it exists and is only bound to in a_o->init() then
	 * ACT_P_CREATED is not returned (a zero is).
	 */
686
	if (err != ACT_P_CREATED)
Linus Torvalds's avatar
Linus Torvalds committed
687
688
		module_put(a_o->owner);

689
690
691
692
693
694
695
696
697
698
699
	if (TC_ACT_EXT_CMP(a->tcfa_action, TC_ACT_GOTO_CHAIN)) {
		err = tcf_action_goto_chain_init(a, tp);
		if (err) {
			LIST_HEAD(actions);

			list_add_tail(&a->list, &actions);
			tcf_action_destroy(&actions, bind);
			return ERR_PTR(err);
		}
	}

Linus Torvalds's avatar
Linus Torvalds committed
700
701
702
703
704
	return a;

err_mod:
	module_put(a_o->owner);
err_out:
705
706
707
708
	if (cookie) {
		kfree(cookie->data);
		kfree(cookie);
	}
709
	return ERR_PTR(err);
Linus Torvalds's avatar
Linus Torvalds committed
710
711
}

712
713
714
715
716
717
718
719
720
721
722
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--;
}

723
724
725
int tcf_action_init(struct net *net, struct tcf_proto *tp, struct nlattr *nla,
		    struct nlattr *est, char *name, int ovr, int bind,
		    struct list_head *actions)
Linus Torvalds's avatar
Linus Torvalds committed
726
{
Eric Dumazet's avatar
Eric Dumazet committed
727
	struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
728
	struct tc_action *act;
729
	int err;
Linus Torvalds's avatar
Linus Torvalds committed
730
731
	int i;

732
	err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL, NULL);
733
	if (err < 0)
734
		return err;
Linus Torvalds's avatar
Linus Torvalds committed
735

736
	for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
737
		act = tcf_action_init_1(net, tp, tb[i], est, name, ovr, bind);
738
739
		if (IS_ERR(act)) {
			err = PTR_ERR(act);
Linus Torvalds's avatar
Linus Torvalds committed
740
			goto err;
741
		}
742
		act->order = i;
743
744
		if (ovr)
			act->tcfa_refcnt++;
745
		list_add_tail(&act->list, actions);
Linus Torvalds's avatar
Linus Torvalds committed
746
	}
747
748
749
750
751

	/* Remove the temp refcnt which was necessary to protect against
	 * destroying an existing action which was being replaced
	 */
	cleanup_a(actions, ovr);
752
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
753
754

err:
755
756
	tcf_action_destroy(actions, bind);
	return err;
Linus Torvalds's avatar
Linus Torvalds committed
757
758
}

759
int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *p,
Linus Torvalds's avatar
Linus Torvalds committed
760
761
762
763
			  int compat_mode)
{
	int err = 0;
	struct gnet_dump d;
764

765
	if (p == NULL)
Linus Torvalds's avatar
Linus Torvalds committed
766
767
768
		goto errout;

	/* compat_mode being true specifies a call that is supposed
769
	 * to add additional backward compatibility statistic TLVs.
Linus Torvalds's avatar
Linus Torvalds committed
770
771
	 */
	if (compat_mode) {
772
		if (p->type == TCA_OLD_COMPAT)
Linus Torvalds's avatar
Linus Torvalds committed
773
			err = gnet_stats_start_copy_compat(skb, 0,
774
775
							   TCA_STATS,
							   TCA_XSTATS,
776
							   &p->tcfa_lock, &d,
777
							   TCA_PAD);
Linus Torvalds's avatar
Linus Torvalds committed
778
779
780
781
		else
			return 0;
	} else
		err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
782
					    &p->tcfa_lock, &d, TCA_ACT_PAD);
Linus Torvalds's avatar
Linus Torvalds committed
783
784
785
786

	if (err < 0)
		goto errout;

787
	if (gnet_stats_copy_basic(NULL, &d, p->cpu_bstats, &p->tcfa_bstats) < 0 ||
788
	    gnet_stats_copy_rate_est(&d, &p->tcfa_rate_est) < 0 ||
789
	    gnet_stats_copy_queue(&d, p->cpu_qstats,
790
791
				  &p->tcfa_qstats,
				  p->tcfa_qstats.qlen) < 0)
Linus Torvalds's avatar
Linus Torvalds committed
792
793
794
795
796
797
798
799
800
801
802
		goto errout;

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

	return 0;

errout:
	return -1;
}

803
804
805
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)
Linus Torvalds's avatar
Linus Torvalds committed
806
807
808
{
	struct tcamsg *t;
	struct nlmsghdr *nlh;
809
	unsigned char *b = skb_tail_pointer(skb);
810
	struct nlattr *nest;
Linus Torvalds's avatar
Linus Torvalds committed
811

812
	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
813
814
815
	if (!nlh)
		goto out_nlmsg_trim;
	t = nlmsg_data(nlh);
Linus Torvalds's avatar
Linus Torvalds committed
816
	t->tca_family = AF_UNSPEC;
817
818
	t->tca__pad1 = 0;
	t->tca__pad2 = 0;
819

820
821
	nest = nla_nest_start(skb, TCA_ACT_TAB);
	if (nest == NULL)
822
		goto out_nlmsg_trim;
Linus Torvalds's avatar
Linus Torvalds committed
823

824
	if (tcf_action_dump(skb, actions, bind, ref) < 0)
825
		goto out_nlmsg_trim;
Linus Torvalds's avatar
Linus Torvalds committed
826

827
	nla_nest_end(skb, nest);
828

829
	nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds's avatar
Linus Torvalds committed
830
831
	return skb->len;

832
out_nlmsg_trim:
833
	nlmsg_trim(skb, b);
Linus Torvalds's avatar
Linus Torvalds committed
834
835
836
837
	return -1;
}

static int
838
tcf_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
839
	       struct list_head *actions, int event)
Linus Torvalds's avatar
Linus Torvalds committed
840
841
842
843
844
845
{
	struct sk_buff *skb;

	skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
	if (!skb)
		return -ENOBUFS;
846
847
	if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event,
			 0, 0) <= 0) {
Linus Torvalds's avatar
Linus Torvalds committed
848
849
850
		kfree_skb(skb);
		return -EINVAL;
	}
851

852
	return rtnl_unicast(skb, net, portid);
Linus Torvalds's avatar
Linus Torvalds committed
853
854
}

855
856
static struct tc_action *tcf_action_get_1(struct net *net, struct nlattr *nla,
					  struct nlmsghdr *n, u32 portid)
Linus Torvalds's avatar
Linus Torvalds committed
857
{
Eric Dumazet's avatar
Eric Dumazet committed
858
	struct nlattr *tb[TCA_ACT_MAX + 1];
859
	const struct tc_action_ops *ops;
Linus Torvalds's avatar
Linus Torvalds committed
860
861
	struct tc_action *a;
	int index;
862
	int err;
Linus Torvalds's avatar
Linus Torvalds committed
863

864
	err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, NULL);
865
	if (err < 0)
866
		goto err_out;
Linus Torvalds's avatar
Linus Torvalds committed
867

868
	err = -EINVAL;
869
870
	if (tb[TCA_ACT_INDEX] == NULL ||
	    nla_len(tb[TCA_ACT_INDEX]) < sizeof(index))
871
		goto err_out;
872
	index = nla_get_u32(tb[TCA_ACT_INDEX]);
Linus Torvalds's avatar
Linus Torvalds committed
873

874
	err = -EINVAL;
875
876
877
	ops = tc_lookup_action(tb[TCA_ACT_KIND]);
	if (!ops) /* could happen in batch of actions */
		goto err_out;
878
	err = -ENOENT;
879
	if (ops->lookup(net, &a, index) == 0)
Linus Torvalds's avatar
Linus Torvalds committed
880
881
		goto err_mod;

882
	module_put(ops->owner);
Linus Torvalds's avatar
Linus Torvalds committed
883
	return a;
884

Linus Torvalds's avatar
Linus Torvalds committed
885
err_mod:
886
	module_put(ops->owner);
887
888
err_out:
	return ERR_PTR(err);
Linus Torvalds's avatar
Linus Torvalds committed
889
890
}

891
static int tca_action_flush(struct net *net,