cls_route.c 14.4 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * net/sched/cls_route.c	ROUTE4 classifier.
 *
 *		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.
 *
 * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
 */

#include <linux/module.h>
13
#include <linux/slab.h>
L
Linus Torvalds 已提交
14 15 16 17 18
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/errno.h>
#include <linux/skbuff.h>
19 20 21
#include <net/dst.h>
#include <net/route.h>
#include <net/netlink.h>
L
Linus Torvalds 已提交
22 23 24 25
#include <net/act_api.h>
#include <net/pkt_cls.h>

/*
E
Eric Dumazet 已提交
26 27 28 29 30
 * 1. For now we assume that route tags < 256.
 *    It allows to use direct table lookups, instead of hash tables.
 * 2. For now we assume that "from TAG" and "fromdev DEV" statements
 *    are mutually  exclusive.
 * 3. "to TAG from ANY" has higher priority, than "to ANY from XXX"
L
Linus Torvalds 已提交
31
 */
E
Eric Dumazet 已提交
32
struct route4_fastmap {
J
John Fastabend 已提交
33 34 35
	struct route4_filter		*filter;
	u32				id;
	int				iif;
L
Linus Torvalds 已提交
36 37
};

E
Eric Dumazet 已提交
38
struct route4_head {
J
John Fastabend 已提交
39 40 41
	struct route4_fastmap		fastmap[16];
	struct route4_bucket __rcu	*table[256 + 1];
	struct rcu_head			rcu;
L
Linus Torvalds 已提交
42 43
};

E
Eric Dumazet 已提交
44
struct route4_bucket {
L
Linus Torvalds 已提交
45
	/* 16 FROM buckets + 16 IIF buckets + 1 wildcard bucket */
J
John Fastabend 已提交
46 47
	struct route4_filter __rcu	*ht[16 + 16 + 1];
	struct rcu_head			rcu;
L
Linus Torvalds 已提交
48 49
};

E
Eric Dumazet 已提交
50
struct route4_filter {
J
John Fastabend 已提交
51
	struct route4_filter __rcu	*next;
L
Linus Torvalds 已提交
52 53 54 55 56 57 58
	u32			id;
	int			iif;

	struct tcf_result	res;
	struct tcf_exts		exts;
	u32			handle;
	struct route4_bucket	*bkt;
J
John Fastabend 已提交
59
	struct tcf_proto	*tp;
60 61 62 63
	union {
		struct work_struct	work;
		struct rcu_head		rcu;
	};
L
Linus Torvalds 已提交
64 65
};

E
Eric Dumazet 已提交
66
#define ROUTE4_FAILURE ((struct route4_filter *)(-1L))
L
Linus Torvalds 已提交
67

E
Eric Dumazet 已提交
68
static inline int route4_fastmap_hash(u32 id, int iif)
L
Linus Torvalds 已提交
69
{
E
Eric Dumazet 已提交
70
	return id & 0xF;
L
Linus Torvalds 已提交
71 72
}

J
John Fastabend 已提交
73
static DEFINE_SPINLOCK(fastmap_lock);
E
Eric Dumazet 已提交
74
static void
J
John Fastabend 已提交
75
route4_reset_fastmap(struct route4_head *head)
L
Linus Torvalds 已提交
76
{
J
John Fastabend 已提交
77
	spin_lock_bh(&fastmap_lock);
L
Linus Torvalds 已提交
78
	memset(head->fastmap, 0, sizeof(head->fastmap));
J
John Fastabend 已提交
79
	spin_unlock_bh(&fastmap_lock);
L
Linus Torvalds 已提交
80 81
}

E
Eric Dumazet 已提交
82
static void
L
Linus Torvalds 已提交
83 84 85 86
route4_set_fastmap(struct route4_head *head, u32 id, int iif,
		   struct route4_filter *f)
{
	int h = route4_fastmap_hash(id, iif);
E
Eric Dumazet 已提交
87

J
John Fastabend 已提交
88 89
	/* fastmap updates must look atomic to aling id, iff, filter */
	spin_lock_bh(&fastmap_lock);
L
Linus Torvalds 已提交
90 91 92
	head->fastmap[h].id = id;
	head->fastmap[h].iif = iif;
	head->fastmap[h].filter = f;
J
John Fastabend 已提交
93
	spin_unlock_bh(&fastmap_lock);
L
Linus Torvalds 已提交
94 95
}

E
Eric Dumazet 已提交
96
static inline int route4_hash_to(u32 id)
L
Linus Torvalds 已提交
97
{
E
Eric Dumazet 已提交
98
	return id & 0xFF;
L
Linus Torvalds 已提交
99 100
}

E
Eric Dumazet 已提交
101
static inline int route4_hash_from(u32 id)
L
Linus Torvalds 已提交
102
{
E
Eric Dumazet 已提交
103
	return (id >> 16) & 0xF;
L
Linus Torvalds 已提交
104 105
}

E
Eric Dumazet 已提交
106
static inline int route4_hash_iif(int iif)
L
Linus Torvalds 已提交
107
{
E
Eric Dumazet 已提交
108
	return 16 + ((iif >> 16) & 0xF);
L
Linus Torvalds 已提交
109 110
}

E
Eric Dumazet 已提交
111
static inline int route4_hash_wild(void)
L
Linus Torvalds 已提交
112 113 114 115 116 117 118
{
	return 32;
}

#define ROUTE4_APPLY_RESULT()					\
{								\
	*res = f->res;						\
119
	if (tcf_exts_has_actions(&f->exts)) {			\
L
Linus Torvalds 已提交
120 121 122 123 124 125 126 127 128 129 130
		int r = tcf_exts_exec(skb, &f->exts, res);	\
		if (r < 0) {					\
			dont_cache = 1;				\
			continue;				\
		}						\
		return r;					\
	} else if (!dont_cache)					\
		route4_set_fastmap(head, id, iif, f);		\
	return 0;						\
}

131
static int route4_classify(struct sk_buff *skb, const struct tcf_proto *tp,
L
Linus Torvalds 已提交
132 133
			   struct tcf_result *res)
{
J
John Fastabend 已提交
134
	struct route4_head *head = rcu_dereference_bh(tp->root);
L
Linus Torvalds 已提交
135 136 137 138 139 140
	struct dst_entry *dst;
	struct route4_bucket *b;
	struct route4_filter *f;
	u32 id, h;
	int iif, dont_cache = 0;

E
Eric Dumazet 已提交
141 142
	dst = skb_dst(skb);
	if (!dst)
L
Linus Torvalds 已提交
143 144 145 146
		goto failure;

	id = dst->tclassid;

147
	iif = inet_iif(skb);
L
Linus Torvalds 已提交
148 149

	h = route4_fastmap_hash(id, iif);
J
John Fastabend 已提交
150 151

	spin_lock(&fastmap_lock);
L
Linus Torvalds 已提交
152 153 154
	if (id == head->fastmap[h].id &&
	    iif == head->fastmap[h].iif &&
	    (f = head->fastmap[h].filter) != NULL) {
J
John Fastabend 已提交
155 156
		if (f == ROUTE4_FAILURE) {
			spin_unlock(&fastmap_lock);
L
Linus Torvalds 已提交
157
			goto failure;
J
John Fastabend 已提交
158
		}
L
Linus Torvalds 已提交
159 160

		*res = f->res;
J
John Fastabend 已提交
161
		spin_unlock(&fastmap_lock);
L
Linus Torvalds 已提交
162 163
		return 0;
	}
J
John Fastabend 已提交
164
	spin_unlock(&fastmap_lock);
L
Linus Torvalds 已提交
165 166 167 168

	h = route4_hash_to(id);

restart:
J
John Fastabend 已提交
169
	b = rcu_dereference_bh(head->table[h]);
E
Eric Dumazet 已提交
170
	if (b) {
J
John Fastabend 已提交
171 172 173
		for (f = rcu_dereference_bh(b->ht[route4_hash_from(id)]);
		     f;
		     f = rcu_dereference_bh(f->next))
L
Linus Torvalds 已提交
174 175 176
			if (f->id == id)
				ROUTE4_APPLY_RESULT();

J
John Fastabend 已提交
177 178 179
		for (f = rcu_dereference_bh(b->ht[route4_hash_iif(iif)]);
		     f;
		     f = rcu_dereference_bh(f->next))
L
Linus Torvalds 已提交
180 181 182
			if (f->iif == iif)
				ROUTE4_APPLY_RESULT();

J
John Fastabend 已提交
183 184 185
		for (f = rcu_dereference_bh(b->ht[route4_hash_wild()]);
		     f;
		     f = rcu_dereference_bh(f->next))
L
Linus Torvalds 已提交
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
			ROUTE4_APPLY_RESULT();
	}
	if (h < 256) {
		h = 256;
		id &= ~0xFFFF;
		goto restart;
	}

	if (!dont_cache)
		route4_set_fastmap(head, id, iif, ROUTE4_FAILURE);
failure:
	return -1;
}

static inline u32 to_hash(u32 id)
{
E
Eric Dumazet 已提交
202 203 204
	u32 h = id & 0xFF;

	if (id & 0x8000)
L
Linus Torvalds 已提交
205 206 207 208 209 210 211 212 213 214 215 216
		h += 256;
	return h;
}

static inline u32 from_hash(u32 id)
{
	id &= 0xFFFF;
	if (id == 0xFFFF)
		return 32;
	if (!(id & 0x8000)) {
		if (id > 255)
			return 256;
E
Eric Dumazet 已提交
217
		return id & 0xF;
L
Linus Torvalds 已提交
218
	}
E
Eric Dumazet 已提交
219
	return 16 + (id & 0xF);
L
Linus Torvalds 已提交
220 221
}

222
static void *route4_get(struct tcf_proto *tp, u32 handle)
L
Linus Torvalds 已提交
223
{
J
John Fastabend 已提交
224
	struct route4_head *head = rtnl_dereference(tp->root);
L
Linus Torvalds 已提交
225 226
	struct route4_bucket *b;
	struct route4_filter *f;
E
Eric Dumazet 已提交
227
	unsigned int h1, h2;
L
Linus Torvalds 已提交
228 229 230

	h1 = to_hash(handle);
	if (h1 > 256)
231
		return NULL;
L
Linus Torvalds 已提交
232

E
Eric Dumazet 已提交
233
	h2 = from_hash(handle >> 16);
L
Linus Torvalds 已提交
234
	if (h2 > 32)
235
		return NULL;
L
Linus Torvalds 已提交
236

J
John Fastabend 已提交
237
	b = rtnl_dereference(head->table[h1]);
E
Eric Dumazet 已提交
238
	if (b) {
J
John Fastabend 已提交
239 240 241
		for (f = rtnl_dereference(b->ht[h2]);
		     f;
		     f = rtnl_dereference(f->next))
L
Linus Torvalds 已提交
242
			if (f->handle == handle)
243
				return f;
L
Linus Torvalds 已提交
244
	}
245
	return NULL;
L
Linus Torvalds 已提交
246 247 248 249
}

static int route4_init(struct tcf_proto *tp)
{
250 251 252 253 254 255 256
	struct route4_head *head;

	head = kzalloc(sizeof(struct route4_head), GFP_KERNEL);
	if (head == NULL)
		return -ENOBUFS;

	rcu_assign_pointer(tp->root, head);
L
Linus Torvalds 已提交
257 258 259
	return 0;
}

260 261 262 263 264 265 266
static void __route4_delete_filter(struct route4_filter *f)
{
	tcf_exts_destroy(&f->exts);
	tcf_exts_put_net(&f->exts);
	kfree(f);
}

267
static void route4_delete_filter_work(struct work_struct *work)
L
Linus Torvalds 已提交
268
{
269
	struct route4_filter *f = container_of(work, struct route4_filter, work);
J
John Fastabend 已提交
270

271
	rtnl_lock();
272
	__route4_delete_filter(f);
273 274 275 276 277 278 279 280 281
	rtnl_unlock();
}

static void route4_delete_filter(struct rcu_head *head)
{
	struct route4_filter *f = container_of(head, struct route4_filter, rcu);

	INIT_WORK(&f->work, route4_delete_filter_work);
	tcf_queue_work(&f->work);
L
Linus Torvalds 已提交
282 283
}

284
static void route4_destroy(struct tcf_proto *tp)
L
Linus Torvalds 已提交
285
{
J
John Fastabend 已提交
286
	struct route4_head *head = rtnl_dereference(tp->root);
L
Linus Torvalds 已提交
287 288 289
	int h1, h2;

	if (head == NULL)
290
		return;
L
Linus Torvalds 已提交
291

E
Eric Dumazet 已提交
292
	for (h1 = 0; h1 <= 256; h1++) {
L
Linus Torvalds 已提交
293 294
		struct route4_bucket *b;

J
John Fastabend 已提交
295
		b = rtnl_dereference(head->table[h1]);
E
Eric Dumazet 已提交
296 297
		if (b) {
			for (h2 = 0; h2 <= 32; h2++) {
L
Linus Torvalds 已提交
298 299
				struct route4_filter *f;

J
John Fastabend 已提交
300 301 302 303 304
				while ((f = rtnl_dereference(b->ht[h2])) != NULL) {
					struct route4_filter *next;

					next = rtnl_dereference(f->next);
					RCU_INIT_POINTER(b->ht[h2], next);
305
					tcf_unbind_filter(tp, &f->res);
306 307 308 309
					if (tcf_exts_get_net(&f->exts))
						call_rcu(&f->rcu, route4_delete_filter);
					else
						__route4_delete_filter(f);
L
Linus Torvalds 已提交
310 311
				}
			}
J
John Fastabend 已提交
312 313
			RCU_INIT_POINTER(head->table[h1], NULL);
			kfree_rcu(b, rcu);
L
Linus Torvalds 已提交
314 315
		}
	}
J
John Fastabend 已提交
316
	kfree_rcu(head, rcu);
L
Linus Torvalds 已提交
317 318
}

319
static int route4_delete(struct tcf_proto *tp, void *arg, bool *last)
L
Linus Torvalds 已提交
320
{
J
John Fastabend 已提交
321
	struct route4_head *head = rtnl_dereference(tp->root);
322
	struct route4_filter *f = arg;
J
John Fastabend 已提交
323 324
	struct route4_filter __rcu **fp;
	struct route4_filter *nf;
L
Linus Torvalds 已提交
325
	struct route4_bucket *b;
J
John Fastabend 已提交
326
	unsigned int h = 0;
327
	int i, h1;
L
Linus Torvalds 已提交
328 329 330 331 332 333 334

	if (!head || !f)
		return -EINVAL;

	h = f->handle;
	b = f->bkt;

J
John Fastabend 已提交
335 336 337 338 339 340
	fp = &b->ht[from_hash(h >> 16)];
	for (nf = rtnl_dereference(*fp); nf;
	     fp = &nf->next, nf = rtnl_dereference(*fp)) {
		if (nf == f) {
			/* unlink it */
			RCU_INIT_POINTER(*fp, rtnl_dereference(f->next));
L
Linus Torvalds 已提交
341

J
John Fastabend 已提交
342 343 344 345 346
			/* Remove any fastmap lookups that might ref filter
			 * notice we unlink'd the filter so we can't get it
			 * back in the fastmap.
			 */
			route4_reset_fastmap(head);
L
Linus Torvalds 已提交
347

J
John Fastabend 已提交
348
			/* Delete it */
349
			tcf_unbind_filter(tp, &f->res);
350
			tcf_exts_get_net(&f->exts);
J
John Fastabend 已提交
351
			call_rcu(&f->rcu, route4_delete_filter);
L
Linus Torvalds 已提交
352

J
John Fastabend 已提交
353 354 355 356 357 358
			/* Strip RTNL protected tree */
			for (i = 0; i <= 32; i++) {
				struct route4_filter *rt;

				rt = rtnl_dereference(b->ht[i]);
				if (rt)
359
					goto out;
J
John Fastabend 已提交
360
			}
L
Linus Torvalds 已提交
361 362

			/* OK, session has no flows */
J
John Fastabend 已提交
363 364
			RCU_INIT_POINTER(head->table[to_hash(h)], NULL);
			kfree_rcu(b, rcu);
365 366 367
			break;
		}
	}
L
Linus Torvalds 已提交
368

369 370 371 372 373 374
out:
	*last = true;
	for (h1 = 0; h1 <= 256; h1++) {
		if (rcu_access_pointer(head->table[h1])) {
			*last = false;
			break;
L
Linus Torvalds 已提交
375 376
		}
	}
377

L
Linus Torvalds 已提交
378 379 380
	return 0;
}

381 382 383 384 385 386 387
static const struct nla_policy route4_policy[TCA_ROUTE4_MAX + 1] = {
	[TCA_ROUTE4_CLASSID]	= { .type = NLA_U32 },
	[TCA_ROUTE4_TO]		= { .type = NLA_U32 },
	[TCA_ROUTE4_FROM]	= { .type = NLA_U32 },
	[TCA_ROUTE4_IIF]	= { .type = NLA_U32 },
};

388 389 390
static int route4_set_parms(struct net *net, struct tcf_proto *tp,
			    unsigned long base, struct route4_filter *f,
			    u32 handle, struct route4_head *head,
391
			    struct nlattr **tb, struct nlattr *est, int new,
392
			    bool ovr, struct netlink_ext_ack *extack)
L
Linus Torvalds 已提交
393 394 395 396 397
{
	u32 id = 0, to = 0, nhandle = 0x8000;
	struct route4_filter *fp;
	unsigned int h1;
	struct route4_bucket *b;
398
	int err;
L
Linus Torvalds 已提交
399

400
	err = tcf_exts_validate(net, tp, tb, est, &f->exts, ovr, extack);
L
Linus Torvalds 已提交
401 402 403
	if (err < 0)
		return err;

404
	if (tb[TCA_ROUTE4_TO]) {
L
Linus Torvalds 已提交
405
		if (new && handle & 0x8000)
406
			return -EINVAL;
407
		to = nla_get_u32(tb[TCA_ROUTE4_TO]);
L
Linus Torvalds 已提交
408
		if (to > 0xFF)
409
			return -EINVAL;
L
Linus Torvalds 已提交
410 411 412
		nhandle = to;
	}

413 414
	if (tb[TCA_ROUTE4_FROM]) {
		if (tb[TCA_ROUTE4_IIF])
415
			return -EINVAL;
416
		id = nla_get_u32(tb[TCA_ROUTE4_FROM]);
L
Linus Torvalds 已提交
417
		if (id > 0xFF)
418
			return -EINVAL;
L
Linus Torvalds 已提交
419
		nhandle |= id << 16;
420
	} else if (tb[TCA_ROUTE4_IIF]) {
421
		id = nla_get_u32(tb[TCA_ROUTE4_IIF]);
L
Linus Torvalds 已提交
422
		if (id > 0x7FFF)
423
			return -EINVAL;
L
Linus Torvalds 已提交
424 425 426 427 428 429 430
		nhandle |= (id | 0x8000) << 16;
	} else
		nhandle |= 0xFFFF << 16;

	if (handle && new) {
		nhandle |= handle & 0x7F00;
		if (nhandle != handle)
431
			return -EINVAL;
L
Linus Torvalds 已提交
432 433 434
	}

	h1 = to_hash(nhandle);
J
John Fastabend 已提交
435
	b = rtnl_dereference(head->table[h1]);
E
Eric Dumazet 已提交
436
	if (!b) {
437
		b = kzalloc(sizeof(struct route4_bucket), GFP_KERNEL);
L
Linus Torvalds 已提交
438
		if (b == NULL)
439
			return -ENOBUFS;
L
Linus Torvalds 已提交
440

J
John Fastabend 已提交
441
		rcu_assign_pointer(head->table[h1], b);
L
Linus Torvalds 已提交
442 443
	} else {
		unsigned int h2 = from_hash(nhandle >> 16);
E
Eric Dumazet 已提交
444

J
John Fastabend 已提交
445 446 447
		for (fp = rtnl_dereference(b->ht[h2]);
		     fp;
		     fp = rtnl_dereference(fp->next))
L
Linus Torvalds 已提交
448
			if (fp->handle == f->handle)
449
				return -EEXIST;
L
Linus Torvalds 已提交
450 451
	}

452
	if (tb[TCA_ROUTE4_TO])
L
Linus Torvalds 已提交
453 454
		f->id = to;

455
	if (tb[TCA_ROUTE4_FROM])
L
Linus Torvalds 已提交
456
		f->id = to | id<<16;
457
	else if (tb[TCA_ROUTE4_IIF])
L
Linus Torvalds 已提交
458 459 460 461
		f->iif = id;

	f->handle = nhandle;
	f->bkt = b;
J
John Fastabend 已提交
462
	f->tp = tp;
L
Linus Torvalds 已提交
463

464
	if (tb[TCA_ROUTE4_CLASSID]) {
465
		f->res.classid = nla_get_u32(tb[TCA_ROUTE4_CLASSID]);
L
Linus Torvalds 已提交
466 467 468 469 470 471
		tcf_bind_filter(tp, &f->res, base);
	}

	return 0;
}

472
static int route4_change(struct net *net, struct sk_buff *in_skb,
J
Jamal Hadi Salim 已提交
473
			 struct tcf_proto *tp, unsigned long base, u32 handle,
474 475
			 struct nlattr **tca, void **arg, bool ovr,
			 struct netlink_ext_ack *extack)
L
Linus Torvalds 已提交
476
{
J
John Fastabend 已提交
477 478 479
	struct route4_head *head = rtnl_dereference(tp->root);
	struct route4_filter __rcu **fp;
	struct route4_filter *fold, *f1, *pfp, *f = NULL;
L
Linus Torvalds 已提交
480
	struct route4_bucket *b;
481 482
	struct nlattr *opt = tca[TCA_OPTIONS];
	struct nlattr *tb[TCA_ROUTE4_MAX + 1];
L
Linus Torvalds 已提交
483 484
	unsigned int h, th;
	int err;
J
John Fastabend 已提交
485
	bool new = true;
L
Linus Torvalds 已提交
486 487 488 489

	if (opt == NULL)
		return handle ? -EINVAL : 0;

490
	err = nla_parse_nested(tb, TCA_ROUTE4_MAX, opt, route4_policy, NULL);
491 492
	if (err < 0)
		return err;
L
Linus Torvalds 已提交
493

494
	fold = *arg;
J
John Fastabend 已提交
495
	if (fold && handle && fold->handle != handle)
L
Linus Torvalds 已提交
496 497 498
			return -EINVAL;

	err = -ENOBUFS;
499
	f = kzalloc(sizeof(struct route4_filter), GFP_KERNEL);
J
John Fastabend 已提交
500
	if (!f)
L
Linus Torvalds 已提交
501 502
		goto errout;

503 504 505 506
	err = tcf_exts_init(&f->exts, TCA_ROUTE4_ACT, TCA_ROUTE4_POLICE);
	if (err < 0)
		goto errout;

J
John Fastabend 已提交
507 508 509 510 511 512 513 514 515 516 517
	if (fold) {
		f->id = fold->id;
		f->iif = fold->iif;
		f->res = fold->res;
		f->handle = fold->handle;

		f->tp = fold->tp;
		f->bkt = fold->bkt;
		new = false;
	}

518
	err = route4_set_parms(net, tp, base, f, handle, head, tb,
519
			       tca[TCA_RATE], new, ovr, extack);
L
Linus Torvalds 已提交
520 521 522 523
	if (err < 0)
		goto errout;

	h = from_hash(f->handle >> 16);
J
John Fastabend 已提交
524 525 526 527
	fp = &f->bkt->ht[h];
	for (pfp = rtnl_dereference(*fp);
	     (f1 = rtnl_dereference(*fp)) != NULL;
	     fp = &f1->next)
L
Linus Torvalds 已提交
528 529 530
		if (f->handle < f1->handle)
			break;

531
	tcf_block_netif_keep_dst(tp->chain->block);
J
John Fastabend 已提交
532 533
	rcu_assign_pointer(f->next, f1);
	rcu_assign_pointer(*fp, f);
L
Linus Torvalds 已提交
534

J
John Fastabend 已提交
535 536 537 538
	if (fold && fold->handle && f->handle != fold->handle) {
		th = to_hash(fold->handle);
		h = from_hash(fold->handle >> 16);
		b = rtnl_dereference(head->table[th]);
E
Eric Dumazet 已提交
539
		if (b) {
J
John Fastabend 已提交
540 541 542 543
			fp = &b->ht[h];
			for (pfp = rtnl_dereference(*fp); pfp;
			     fp = &pfp->next, pfp = rtnl_dereference(*fp)) {
				if (pfp == f) {
L
Linus Torvalds 已提交
544 545 546 547 548 549 550
					*fp = f->next;
					break;
				}
			}
		}
	}

J
John Fastabend 已提交
551
	route4_reset_fastmap(head);
552
	*arg = f;
553 554
	if (fold) {
		tcf_unbind_filter(tp, &fold->res);
555
		tcf_exts_get_net(&fold->exts);
J
John Fastabend 已提交
556
		call_rcu(&fold->rcu, route4_delete_filter);
557
	}
L
Linus Torvalds 已提交
558 559 560
	return 0;

errout:
561 562
	if (f)
		tcf_exts_destroy(&f->exts);
J
Jesper Juhl 已提交
563
	kfree(f);
L
Linus Torvalds 已提交
564 565 566 567 568
	return err;
}

static void route4_walk(struct tcf_proto *tp, struct tcf_walker *arg)
{
J
John Fastabend 已提交
569
	struct route4_head *head = rtnl_dereference(tp->root);
E
Eric Dumazet 已提交
570
	unsigned int h, h1;
L
Linus Torvalds 已提交
571 572 573 574 575 576 577 578

	if (head == NULL)
		arg->stop = 1;

	if (arg->stop)
		return;

	for (h = 0; h <= 256; h++) {
J
John Fastabend 已提交
579
		struct route4_bucket *b = rtnl_dereference(head->table[h]);
L
Linus Torvalds 已提交
580 581 582 583 584

		if (b) {
			for (h1 = 0; h1 <= 32; h1++) {
				struct route4_filter *f;

J
John Fastabend 已提交
585 586 587
				for (f = rtnl_dereference(b->ht[h1]);
				     f;
				     f = rtnl_dereference(f->next)) {
L
Linus Torvalds 已提交
588 589 590 591
					if (arg->count < arg->skip) {
						arg->count++;
						continue;
					}
592
					if (arg->fn(tp, f, arg) < 0) {
L
Linus Torvalds 已提交
593 594 595 596 597 598 599 600 601 602
						arg->stop = 1;
						return;
					}
					arg->count++;
				}
			}
		}
	}
}

603
static int route4_dump(struct net *net, struct tcf_proto *tp, void *fh,
L
Linus Torvalds 已提交
604 605
		       struct sk_buff *skb, struct tcmsg *t)
{
606
	struct route4_filter *f = fh;
607
	struct nlattr *nest;
L
Linus Torvalds 已提交
608 609 610 611 612 613 614
	u32 id;

	if (f == NULL)
		return skb->len;

	t->tcm_handle = f->handle;

615 616 617
	nest = nla_nest_start(skb, TCA_OPTIONS);
	if (nest == NULL)
		goto nla_put_failure;
L
Linus Torvalds 已提交
618

E
Eric Dumazet 已提交
619 620
	if (!(f->handle & 0x8000)) {
		id = f->id & 0xFF;
621 622
		if (nla_put_u32(skb, TCA_ROUTE4_TO, id))
			goto nla_put_failure;
L
Linus Torvalds 已提交
623
	}
E
Eric Dumazet 已提交
624
	if (f->handle & 0x80000000) {
625 626 627
		if ((f->handle >> 16) != 0xFFFF &&
		    nla_put_u32(skb, TCA_ROUTE4_IIF, f->iif))
			goto nla_put_failure;
L
Linus Torvalds 已提交
628
	} else {
E
Eric Dumazet 已提交
629
		id = f->id >> 16;
630 631
		if (nla_put_u32(skb, TCA_ROUTE4_FROM, id))
			goto nla_put_failure;
L
Linus Torvalds 已提交
632
	}
633 634 635
	if (f->res.classid &&
	    nla_put_u32(skb, TCA_ROUTE4_CLASSID, f->res.classid))
		goto nla_put_failure;
L
Linus Torvalds 已提交
636

637
	if (tcf_exts_dump(skb, &f->exts) < 0)
638
		goto nla_put_failure;
L
Linus Torvalds 已提交
639

640
	nla_nest_end(skb, nest);
L
Linus Torvalds 已提交
641

642
	if (tcf_exts_dump_stats(skb, &f->exts) < 0)
643
		goto nla_put_failure;
L
Linus Torvalds 已提交
644 645 646

	return skb->len;

647
nla_put_failure:
648
	nla_nest_cancel(skb, nest);
L
Linus Torvalds 已提交
649 650 651
	return -1;
}

652 653 654 655 656 657 658 659
static void route4_bind_class(void *fh, u32 classid, unsigned long cl)
{
	struct route4_filter *f = fh;

	if (f && f->res.classid == classid)
		f->res.class = cl;
}

660
static struct tcf_proto_ops cls_route4_ops __read_mostly = {
L
Linus Torvalds 已提交
661 662 663 664 665 666 667 668 669
	.kind		=	"route",
	.classify	=	route4_classify,
	.init		=	route4_init,
	.destroy	=	route4_destroy,
	.get		=	route4_get,
	.change		=	route4_change,
	.delete		=	route4_delete,
	.walk		=	route4_walk,
	.dump		=	route4_dump,
670
	.bind_class	=	route4_bind_class,
L
Linus Torvalds 已提交
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686
	.owner		=	THIS_MODULE,
};

static int __init init_route4(void)
{
	return register_tcf_proto_ops(&cls_route4_ops);
}

static void __exit exit_route4(void)
{
	unregister_tcf_proto_ops(&cls_route4_ops);
}

module_init(init_route4)
module_exit(exit_route4)
MODULE_LICENSE("GPL");