cls_route.c 14.2 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 60
	struct tcf_proto	*tp;
	struct rcu_head		rcu;
L
Linus Torvalds 已提交
61 62
};

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

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

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

E
Eric Dumazet 已提交
79
static void
L
Linus Torvalds 已提交
80 81 82 83
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 已提交
84

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

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

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

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

E
Eric Dumazet 已提交
108
static inline int route4_hash_wild(void)
L
Linus Torvalds 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
{
	return 32;
}

#define ROUTE4_APPLY_RESULT()					\
{								\
	*res = f->res;						\
	if (tcf_exts_is_available(&f->exts)) {			\
		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;						\
}

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

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

	id = dst->tclassid;
	if (head == NULL)
		goto old_method;

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

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

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

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

	h = route4_hash_to(id);

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

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

J
John Fastabend 已提交
182 183 184
		for (f = rcu_dereference_bh(b->ht[route4_hash_wild()]);
		     f;
		     f = rcu_dereference_bh(f->next))
L
Linus Torvalds 已提交
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
			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;

old_method:
	if (id && (TC_H_MAJ(id) == 0 ||
		   !(TC_H_MAJ(id^tp->q->handle)))) {
		res->classid = id;
		res->class = 0;
		return 0;
	}
	return -1;
}

static inline u32 to_hash(u32 id)
{
E
Eric Dumazet 已提交
210 211 212
	u32 h = id & 0xFF;

	if (id & 0x8000)
L
Linus Torvalds 已提交
213 214 215 216 217 218 219 220 221 222 223 224
		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 已提交
225
		return id & 0xF;
L
Linus Torvalds 已提交
226
	}
E
Eric Dumazet 已提交
227
	return 16 + (id & 0xF);
L
Linus Torvalds 已提交
228 229 230 231
}

static unsigned long route4_get(struct tcf_proto *tp, u32 handle)
{
J
John Fastabend 已提交
232
	struct route4_head *head = rtnl_dereference(tp->root);
L
Linus Torvalds 已提交
233 234
	struct route4_bucket *b;
	struct route4_filter *f;
E
Eric Dumazet 已提交
235
	unsigned int h1, h2;
L
Linus Torvalds 已提交
236 237 238 239 240 241 242 243

	if (!head)
		return 0;

	h1 = to_hash(handle);
	if (h1 > 256)
		return 0;

E
Eric Dumazet 已提交
244
	h2 = from_hash(handle >> 16);
L
Linus Torvalds 已提交
245 246 247
	if (h2 > 32)
		return 0;

J
John Fastabend 已提交
248
	b = rtnl_dereference(head->table[h1]);
E
Eric Dumazet 已提交
249
	if (b) {
J
John Fastabend 已提交
250 251 252
		for (f = rtnl_dereference(b->ht[h2]);
		     f;
		     f = rtnl_dereference(f->next))
L
Linus Torvalds 已提交
253 254 255 256 257 258 259 260
			if (f->handle == handle)
				return (unsigned long)f;
	}
	return 0;
}

static int route4_init(struct tcf_proto *tp)
{
261 262 263 264 265 266 267
	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 已提交
268 269 270
	return 0;
}

J
Jamal Hadi Salim 已提交
271
static void route4_delete_filter(struct rcu_head *head)
L
Linus Torvalds 已提交
272
{
J
John Fastabend 已提交
273 274
	struct route4_filter *f = container_of(head, struct route4_filter, rcu);

275
	tcf_exts_destroy(&f->exts);
L
Linus Torvalds 已提交
276 277 278
	kfree(f);
}

279
static bool route4_destroy(struct tcf_proto *tp, bool force)
L
Linus Torvalds 已提交
280
{
J
John Fastabend 已提交
281
	struct route4_head *head = rtnl_dereference(tp->root);
L
Linus Torvalds 已提交
282 283 284
	int h1, h2;

	if (head == NULL)
285 286 287 288 289 290 291 292
		return true;

	if (!force) {
		for (h1 = 0; h1 <= 256; h1++) {
			if (rcu_access_pointer(head->table[h1]))
				return false;
		}
	}
L
Linus Torvalds 已提交
293

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

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

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

					next = rtnl_dereference(f->next);
					RCU_INIT_POINTER(b->ht[h2], next);
307
					tcf_unbind_filter(tp, &f->res);
J
John Fastabend 已提交
308
					call_rcu(&f->rcu, route4_delete_filter);
L
Linus Torvalds 已提交
309 310
				}
			}
J
John Fastabend 已提交
311 312
			RCU_INIT_POINTER(head->table[h1], NULL);
			kfree_rcu(b, rcu);
L
Linus Torvalds 已提交
313 314
		}
	}
J
John Fastabend 已提交
315 316
	RCU_INIT_POINTER(tp->root, NULL);
	kfree_rcu(head, rcu);
317
	return true;
L
Linus Torvalds 已提交
318 319 320 321
}

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

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

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

J
John Fastabend 已提交
336 337 338 339 340 341
	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 已提交
342

J
John Fastabend 已提交
343 344 345 346 347
			/* 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 已提交
348

J
John Fastabend 已提交
349
			/* Delete it */
350
			tcf_unbind_filter(tp, &f->res);
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)
L
Linus Torvalds 已提交
359
					return 0;
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);
L
Linus Torvalds 已提交
365 366 367 368 369 370 371

			return 0;
		}
	}
	return 0;
}

372 373 374 375 376 377 378
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 },
};

379 380 381
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,
382 383
			    struct nlattr **tb, struct nlattr *est, int new,
			    bool ovr)
L
Linus Torvalds 已提交
384 385 386 387 388 389
{
	u32 id = 0, to = 0, nhandle = 0x8000;
	struct route4_filter *fp;
	unsigned int h1;
	struct route4_bucket *b;
	struct tcf_exts e;
390
	int err;
L
Linus Torvalds 已提交
391

392
	err = tcf_exts_init(&e, TCA_ROUTE4_ACT, TCA_ROUTE4_POLICE);
L
Linus Torvalds 已提交
393 394
	if (err < 0)
		return err;
395 396 397
	err = tcf_exts_validate(net, tp, tb, est, &e, ovr);
	if (err < 0)
		goto errout;
L
Linus Torvalds 已提交
398 399

	err = -EINVAL;
400
	if (tb[TCA_ROUTE4_TO]) {
L
Linus Torvalds 已提交
401 402
		if (new && handle & 0x8000)
			goto errout;
403
		to = nla_get_u32(tb[TCA_ROUTE4_TO]);
L
Linus Torvalds 已提交
404 405 406 407 408
		if (to > 0xFF)
			goto errout;
		nhandle = to;
	}

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

	if (handle && new) {
		nhandle |= handle & 0x7F00;
		if (nhandle != handle)
			goto errout;
	}

	h1 = to_hash(nhandle);
J
John Fastabend 已提交
431
	b = rtnl_dereference(head->table[h1]);
E
Eric Dumazet 已提交
432
	if (!b) {
L
Linus Torvalds 已提交
433
		err = -ENOBUFS;
434
		b = kzalloc(sizeof(struct route4_bucket), GFP_KERNEL);
L
Linus Torvalds 已提交
435 436 437
		if (b == NULL)
			goto errout;

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

L
Linus Torvalds 已提交
442
		err = -EEXIST;
J
John Fastabend 已提交
443 444 445
		for (fp = rtnl_dereference(b->ht[h2]);
		     fp;
		     fp = rtnl_dereference(fp->next))
L
Linus Torvalds 已提交
446 447 448 449
			if (fp->handle == f->handle)
				goto errout;
	}

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

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

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

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

	tcf_exts_change(tp, &f->exts, &e);

	return 0;
errout:
471
	tcf_exts_destroy(&e);
L
Linus Torvalds 已提交
472 473 474
	return err;
}

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

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

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

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

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

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

J
John Fastabend 已提交
509 510 511 512 513 514 515 516 517 518 519
	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;
	}

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

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

533
	netif_keep_dst(qdisc_dev(tp->q));
J
John Fastabend 已提交
534 535
	rcu_assign_pointer(f->next, f1);
	rcu_assign_pointer(*fp, f);
L
Linus Torvalds 已提交
536

J
John Fastabend 已提交
537 538 539 540
	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 已提交
541
		if (b) {
J
John Fastabend 已提交
542 543 544 545
			fp = &b->ht[h];
			for (pfp = rtnl_dereference(*fp); pfp;
			     fp = &pfp->next, pfp = rtnl_dereference(*fp)) {
				if (pfp == f) {
L
Linus Torvalds 已提交
546 547 548 549 550 551 552
					*fp = f->next;
					break;
				}
			}
		}
	}

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

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

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

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

	if (arg->stop)
		return;

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

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

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

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

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

	t->tcm_handle = f->handle;

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

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

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

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

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

	return skb->len;

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

653
static struct tcf_proto_ops cls_route4_ops __read_mostly = {
L
Linus Torvalds 已提交
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
	.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,
	.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");