cls_route.c 14.5 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
L
Linus Torvalds 已提交
2 3 4 5 6 7 8
/*
 * net/sched/cls_route.c	ROUTE4 classifier.
 *
 * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
 */

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

/*
E
Eric Dumazet 已提交
22 23 24 25 26
 * 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 已提交
27
 */
E
Eric Dumazet 已提交
28
struct route4_fastmap {
J
John Fastabend 已提交
29 30 31
	struct route4_filter		*filter;
	u32				id;
	int				iif;
L
Linus Torvalds 已提交
32 33
};

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

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

E
Eric Dumazet 已提交
46
struct route4_filter {
J
John Fastabend 已提交
47
	struct route4_filter __rcu	*next;
L
Linus Torvalds 已提交
48 49 50 51 52 53 54
	u32			id;
	int			iif;

	struct tcf_result	res;
	struct tcf_exts		exts;
	u32			handle;
	struct route4_bucket	*bkt;
J
John Fastabend 已提交
55
	struct tcf_proto	*tp;
C
Cong Wang 已提交
56
	struct rcu_work		rwork;
L
Linus Torvalds 已提交
57 58
};

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

E
Eric Dumazet 已提交
61
static inline int route4_fastmap_hash(u32 id, int iif)
L
Linus Torvalds 已提交
62
{
E
Eric Dumazet 已提交
63
	return id & 0xF;
L
Linus Torvalds 已提交
64 65
}

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

E
Eric Dumazet 已提交
75
static void
L
Linus Torvalds 已提交
76 77 78 79
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 已提交
80

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

E
Eric Dumazet 已提交
89
static inline int route4_hash_to(u32 id)
L
Linus Torvalds 已提交
90
{
E
Eric Dumazet 已提交
91
	return id & 0xFF;
L
Linus Torvalds 已提交
92 93
}

E
Eric Dumazet 已提交
94
static inline int route4_hash_from(u32 id)
L
Linus Torvalds 已提交
95
{
E
Eric Dumazet 已提交
96
	return (id >> 16) & 0xF;
L
Linus Torvalds 已提交
97 98
}

E
Eric Dumazet 已提交
99
static inline int route4_hash_iif(int iif)
L
Linus Torvalds 已提交
100
{
E
Eric Dumazet 已提交
101
	return 16 + ((iif >> 16) & 0xF);
L
Linus Torvalds 已提交
102 103
}

E
Eric Dumazet 已提交
104
static inline int route4_hash_wild(void)
L
Linus Torvalds 已提交
105 106 107 108 109 110 111
{
	return 32;
}

#define ROUTE4_APPLY_RESULT()					\
{								\
	*res = f->res;						\
112
	if (tcf_exts_has_actions(&f->exts)) {			\
L
Linus Torvalds 已提交
113 114 115 116 117 118 119 120 121 122 123
		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;						\
}

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

E
Eric Dumazet 已提交
134 135
	dst = skb_dst(skb);
	if (!dst)
L
Linus Torvalds 已提交
136 137 138 139
		goto failure;

	id = dst->tclassid;

140
	iif = inet_iif(skb);
L
Linus Torvalds 已提交
141 142

	h = route4_fastmap_hash(id, iif);
J
John Fastabend 已提交
143 144

	spin_lock(&fastmap_lock);
L
Linus Torvalds 已提交
145 146 147
	if (id == head->fastmap[h].id &&
	    iif == head->fastmap[h].iif &&
	    (f = head->fastmap[h].filter) != NULL) {
J
John Fastabend 已提交
148 149
		if (f == ROUTE4_FAILURE) {
			spin_unlock(&fastmap_lock);
L
Linus Torvalds 已提交
150
			goto failure;
J
John Fastabend 已提交
151
		}
L
Linus Torvalds 已提交
152 153

		*res = f->res;
J
John Fastabend 已提交
154
		spin_unlock(&fastmap_lock);
L
Linus Torvalds 已提交
155 156
		return 0;
	}
J
John Fastabend 已提交
157
	spin_unlock(&fastmap_lock);
L
Linus Torvalds 已提交
158 159 160 161

	h = route4_hash_to(id);

restart:
J
John Fastabend 已提交
162
	b = rcu_dereference_bh(head->table[h]);
E
Eric Dumazet 已提交
163
	if (b) {
J
John Fastabend 已提交
164 165 166
		for (f = rcu_dereference_bh(b->ht[route4_hash_from(id)]);
		     f;
		     f = rcu_dereference_bh(f->next))
L
Linus Torvalds 已提交
167 168 169
			if (f->id == id)
				ROUTE4_APPLY_RESULT();

J
John Fastabend 已提交
170 171 172
		for (f = rcu_dereference_bh(b->ht[route4_hash_iif(iif)]);
		     f;
		     f = rcu_dereference_bh(f->next))
L
Linus Torvalds 已提交
173 174 175
			if (f->iif == iif)
				ROUTE4_APPLY_RESULT();

J
John Fastabend 已提交
176 177 178
		for (f = rcu_dereference_bh(b->ht[route4_hash_wild()]);
		     f;
		     f = rcu_dereference_bh(f->next))
L
Linus Torvalds 已提交
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
			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 已提交
195 196 197
	u32 h = id & 0xFF;

	if (id & 0x8000)
L
Linus Torvalds 已提交
198 199 200 201 202 203 204 205 206 207 208 209
		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 已提交
210
		return id & 0xF;
L
Linus Torvalds 已提交
211
	}
E
Eric Dumazet 已提交
212
	return 16 + (id & 0xF);
L
Linus Torvalds 已提交
213 214
}

215
static void *route4_get(struct tcf_proto *tp, u32 handle)
L
Linus Torvalds 已提交
216
{
J
John Fastabend 已提交
217
	struct route4_head *head = rtnl_dereference(tp->root);
L
Linus Torvalds 已提交
218 219
	struct route4_bucket *b;
	struct route4_filter *f;
E
Eric Dumazet 已提交
220
	unsigned int h1, h2;
L
Linus Torvalds 已提交
221 222 223

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

E
Eric Dumazet 已提交
226
	h2 = from_hash(handle >> 16);
L
Linus Torvalds 已提交
227
	if (h2 > 32)
228
		return NULL;
L
Linus Torvalds 已提交
229

J
John Fastabend 已提交
230
	b = rtnl_dereference(head->table[h1]);
E
Eric Dumazet 已提交
231
	if (b) {
J
John Fastabend 已提交
232 233 234
		for (f = rtnl_dereference(b->ht[h2]);
		     f;
		     f = rtnl_dereference(f->next))
L
Linus Torvalds 已提交
235
			if (f->handle == handle)
236
				return f;
L
Linus Torvalds 已提交
237
	}
238
	return NULL;
L
Linus Torvalds 已提交
239 240 241 242
}

static int route4_init(struct tcf_proto *tp)
{
243 244 245 246 247 248 249
	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 已提交
250 251 252
	return 0;
}

253 254 255 256 257 258 259
static void __route4_delete_filter(struct route4_filter *f)
{
	tcf_exts_destroy(&f->exts);
	tcf_exts_put_net(&f->exts);
	kfree(f);
}

260
static void route4_delete_filter_work(struct work_struct *work)
L
Linus Torvalds 已提交
261
{
C
Cong Wang 已提交
262 263 264
	struct route4_filter *f = container_of(to_rcu_work(work),
					       struct route4_filter,
					       rwork);
265
	rtnl_lock();
266
	__route4_delete_filter(f);
267 268 269
	rtnl_unlock();
}

C
Cong Wang 已提交
270
static void route4_queue_work(struct route4_filter *f)
271
{
C
Cong Wang 已提交
272
	tcf_queue_work(&f->rwork, route4_delete_filter_work);
L
Linus Torvalds 已提交
273 274
}

275 276
static void route4_destroy(struct tcf_proto *tp, bool rtnl_held,
			   struct netlink_ext_ack *extack)
L
Linus Torvalds 已提交
277
{
J
John Fastabend 已提交
278
	struct route4_head *head = rtnl_dereference(tp->root);
L
Linus Torvalds 已提交
279 280 281
	int h1, h2;

	if (head == NULL)
282
		return;
L
Linus Torvalds 已提交
283

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

J
John Fastabend 已提交
287
		b = rtnl_dereference(head->table[h1]);
E
Eric Dumazet 已提交
288 289
		if (b) {
			for (h2 = 0; h2 <= 32; h2++) {
L
Linus Torvalds 已提交
290 291
				struct route4_filter *f;

J
John Fastabend 已提交
292 293 294 295 296
				while ((f = rtnl_dereference(b->ht[h2])) != NULL) {
					struct route4_filter *next;

					next = rtnl_dereference(f->next);
					RCU_INIT_POINTER(b->ht[h2], next);
297
					tcf_unbind_filter(tp, &f->res);
298
					if (tcf_exts_get_net(&f->exts))
C
Cong Wang 已提交
299
						route4_queue_work(f);
300 301
					else
						__route4_delete_filter(f);
L
Linus Torvalds 已提交
302 303
				}
			}
J
John Fastabend 已提交
304 305
			RCU_INIT_POINTER(head->table[h1], NULL);
			kfree_rcu(b, rcu);
L
Linus Torvalds 已提交
306 307
		}
	}
J
John Fastabend 已提交
308
	kfree_rcu(head, rcu);
L
Linus Torvalds 已提交
309 310
}

311
static int route4_delete(struct tcf_proto *tp, void *arg, bool *last,
312
			 bool rtnl_held, struct netlink_ext_ack *extack)
L
Linus Torvalds 已提交
313
{
J
John Fastabend 已提交
314
	struct route4_head *head = rtnl_dereference(tp->root);
315
	struct route4_filter *f = arg;
J
John Fastabend 已提交
316 317
	struct route4_filter __rcu **fp;
	struct route4_filter *nf;
L
Linus Torvalds 已提交
318
	struct route4_bucket *b;
J
John Fastabend 已提交
319
	unsigned int h = 0;
320
	int i, h1;
L
Linus Torvalds 已提交
321 322 323 324 325 326 327

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

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

J
John Fastabend 已提交
328 329 330 331 332 333
	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 已提交
334

J
John Fastabend 已提交
335 336 337 338 339
			/* 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 已提交
340

J
John Fastabend 已提交
341
			/* Delete it */
342
			tcf_unbind_filter(tp, &f->res);
343
			tcf_exts_get_net(&f->exts);
C
Cong Wang 已提交
344
			tcf_queue_work(&f->rwork, route4_delete_filter_work);
L
Linus Torvalds 已提交
345

J
John Fastabend 已提交
346 347 348 349 350 351
			/* Strip RTNL protected tree */
			for (i = 0; i <= 32; i++) {
				struct route4_filter *rt;

				rt = rtnl_dereference(b->ht[i]);
				if (rt)
352
					goto out;
J
John Fastabend 已提交
353
			}
L
Linus Torvalds 已提交
354 355

			/* OK, session has no flows */
J
John Fastabend 已提交
356 357
			RCU_INIT_POINTER(head->table[to_hash(h)], NULL);
			kfree_rcu(b, rcu);
358 359 360
			break;
		}
	}
L
Linus Torvalds 已提交
361

362 363 364 365 366 367
out:
	*last = true;
	for (h1 = 0; h1 <= 256; h1++) {
		if (rcu_access_pointer(head->table[h1])) {
			*last = false;
			break;
L
Linus Torvalds 已提交
368 369
		}
	}
370

L
Linus Torvalds 已提交
371 372 373
	return 0;
}

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

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

393
	err = tcf_exts_validate(net, tp, tb, est, &f->exts, ovr, true, extack);
L
Linus Torvalds 已提交
394 395 396
	if (err < 0)
		return err;

397
	if (tb[TCA_ROUTE4_TO]) {
L
Linus Torvalds 已提交
398
		if (new && handle & 0x8000)
399
			return -EINVAL;
400
		to = nla_get_u32(tb[TCA_ROUTE4_TO]);
L
Linus Torvalds 已提交
401
		if (to > 0xFF)
402
			return -EINVAL;
L
Linus Torvalds 已提交
403 404 405
		nhandle = to;
	}

406 407
	if (tb[TCA_ROUTE4_FROM]) {
		if (tb[TCA_ROUTE4_IIF])
408
			return -EINVAL;
409
		id = nla_get_u32(tb[TCA_ROUTE4_FROM]);
L
Linus Torvalds 已提交
410
		if (id > 0xFF)
411
			return -EINVAL;
L
Linus Torvalds 已提交
412
		nhandle |= id << 16;
413
	} else if (tb[TCA_ROUTE4_IIF]) {
414
		id = nla_get_u32(tb[TCA_ROUTE4_IIF]);
L
Linus Torvalds 已提交
415
		if (id > 0x7FFF)
416
			return -EINVAL;
L
Linus Torvalds 已提交
417 418 419 420 421 422 423
		nhandle |= (id | 0x8000) << 16;
	} else
		nhandle |= 0xFFFF << 16;

	if (handle && new) {
		nhandle |= handle & 0x7F00;
		if (nhandle != handle)
424
			return -EINVAL;
L
Linus Torvalds 已提交
425 426
	}

427 428 429 430 431
	if (!nhandle) {
		NL_SET_ERR_MSG(extack, "Replacing with handle of 0 is invalid");
		return -EINVAL;
	}

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

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

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

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
		tcf_bind_filter(tp, &f->res, base);
	}

	return 0;
}

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

485 486 487 488 489
	if (!handle) {
		NL_SET_ERR_MSG(extack, "Creating with handle of 0 is invalid");
		return -EINVAL;
	}

L
Linus Torvalds 已提交
490 491 492
	if (opt == NULL)
		return handle ? -EINVAL : 0;

493 494
	err = nla_parse_nested_deprecated(tb, TCA_ROUTE4_MAX, opt,
					  route4_policy, NULL);
495 496
	if (err < 0)
		return err;
L
Linus Torvalds 已提交
497

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

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

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

J
John Fastabend 已提交
511 512 513 514 515 516 517 518 519 520
	if (fold) {
		f->id = fold->id;
		f->iif = fold->iif;
		f->handle = fold->handle;

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

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

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

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

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

J
John Fastabend 已提交
554
	route4_reset_fastmap(head);
555
	*arg = f;
556 557
	if (fold) {
		tcf_unbind_filter(tp, &fold->res);
558
		tcf_exts_get_net(&fold->exts);
C
Cong Wang 已提交
559
		tcf_queue_work(&fold->rwork, route4_delete_filter_work);
560
	}
L
Linus Torvalds 已提交
561 562 563
	return 0;

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

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

576
	if (head == NULL || arg->stop)
L
Linus Torvalds 已提交
577 578 579
		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
					if (arg->count < arg->skip) {
						arg->count++;
						continue;
					}
593
					if (arg->fn(tp, f, arg) < 0) {
L
Linus Torvalds 已提交
594 595 596 597 598 599 600 601 602 603
						arg->stop = 1;
						return;
					}
					arg->count++;
				}
			}
		}
	}
}

604
static int route4_dump(struct net *net, struct tcf_proto *tp, void *fh,
605
		       struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
L
Linus Torvalds 已提交
606
{
607
	struct route4_filter *f = 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
	nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
617 618
	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 654
static void route4_bind_class(void *fh, u32 classid, unsigned long cl, void *q,
			      unsigned long base)
655 656 657
{
	struct route4_filter *f = fh;

658 659 660 661 662 663
	if (f && f->res.classid == classid) {
		if (cl)
			__tcf_bind_filter(q, &f->res, base);
		else
			__tcf_unbind_filter(q, &f->res);
	}
664 665
}

666
static struct tcf_proto_ops cls_route4_ops __read_mostly = {
L
Linus Torvalds 已提交
667 668 669 670 671 672 673 674 675
	.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,
676
	.bind_class	=	route4_bind_class,
L
Linus Torvalds 已提交
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
	.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");