]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * net/sched/cls_route.c ROUTE4 classifier. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU General Public License | |
6 | * as published by the Free Software Foundation; either version | |
7 | * 2 of the License, or (at your option) any later version. | |
8 | * | |
9 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> | |
10 | */ | |
11 | ||
12 | #include <linux/module.h> | |
5a0e3ad6 | 13 | #include <linux/slab.h> |
1da177e4 LT |
14 | #include <linux/types.h> |
15 | #include <linux/kernel.h> | |
1da177e4 | 16 | #include <linux/string.h> |
1da177e4 | 17 | #include <linux/errno.h> |
1da177e4 | 18 | #include <linux/skbuff.h> |
0ba48053 PM |
19 | #include <net/dst.h> |
20 | #include <net/route.h> | |
21 | #include <net/netlink.h> | |
1da177e4 LT |
22 | #include <net/act_api.h> |
23 | #include <net/pkt_cls.h> | |
24 | ||
25 | /* | |
cc7ec456 ED |
26 | * 1. For now we assume that route tags < 256. |
27 | * It allows to use direct table lookups, instead of hash tables. | |
28 | * 2. For now we assume that "from TAG" and "fromdev DEV" statements | |
29 | * are mutually exclusive. | |
30 | * 3. "to TAG from ANY" has higher priority, than "to ANY from XXX" | |
1da177e4 | 31 | */ |
cc7ec456 | 32 | struct route4_fastmap { |
1109c005 JF |
33 | struct route4_filter *filter; |
34 | u32 id; | |
35 | int iif; | |
1da177e4 LT |
36 | }; |
37 | ||
cc7ec456 | 38 | struct route4_head { |
1109c005 JF |
39 | struct route4_fastmap fastmap[16]; |
40 | struct route4_bucket __rcu *table[256 + 1]; | |
41 | struct rcu_head rcu; | |
1da177e4 LT |
42 | }; |
43 | ||
cc7ec456 | 44 | struct route4_bucket { |
1da177e4 | 45 | /* 16 FROM buckets + 16 IIF buckets + 1 wildcard bucket */ |
1109c005 JF |
46 | struct route4_filter __rcu *ht[16 + 16 + 1]; |
47 | struct rcu_head rcu; | |
1da177e4 LT |
48 | }; |
49 | ||
cc7ec456 | 50 | struct route4_filter { |
1109c005 | 51 | struct route4_filter __rcu *next; |
1da177e4 LT |
52 | u32 id; |
53 | int iif; | |
54 | ||
55 | struct tcf_result res; | |
56 | struct tcf_exts exts; | |
57 | u32 handle; | |
58 | struct route4_bucket *bkt; | |
1109c005 | 59 | struct tcf_proto *tp; |
7afe8e4f | 60 | struct rcu_work rwork; |
1da177e4 LT |
61 | }; |
62 | ||
cc7ec456 | 63 | #define ROUTE4_FAILURE ((struct route4_filter *)(-1L)) |
1da177e4 | 64 | |
cc7ec456 | 65 | static inline int route4_fastmap_hash(u32 id, int iif) |
1da177e4 | 66 | { |
cc7ec456 | 67 | return id & 0xF; |
1da177e4 LT |
68 | } |
69 | ||
1109c005 | 70 | static DEFINE_SPINLOCK(fastmap_lock); |
cc7ec456 | 71 | static void |
1109c005 | 72 | route4_reset_fastmap(struct route4_head *head) |
1da177e4 | 73 | { |
1109c005 | 74 | spin_lock_bh(&fastmap_lock); |
1da177e4 | 75 | memset(head->fastmap, 0, sizeof(head->fastmap)); |
1109c005 | 76 | spin_unlock_bh(&fastmap_lock); |
1da177e4 LT |
77 | } |
78 | ||
cc7ec456 | 79 | static void |
1da177e4 LT |
80 | route4_set_fastmap(struct route4_head *head, u32 id, int iif, |
81 | struct route4_filter *f) | |
82 | { | |
83 | int h = route4_fastmap_hash(id, iif); | |
cc7ec456 | 84 | |
1109c005 JF |
85 | /* fastmap updates must look atomic to aling id, iff, filter */ |
86 | spin_lock_bh(&fastmap_lock); | |
1da177e4 LT |
87 | head->fastmap[h].id = id; |
88 | head->fastmap[h].iif = iif; | |
89 | head->fastmap[h].filter = f; | |
1109c005 | 90 | spin_unlock_bh(&fastmap_lock); |
1da177e4 LT |
91 | } |
92 | ||
cc7ec456 | 93 | static inline int route4_hash_to(u32 id) |
1da177e4 | 94 | { |
cc7ec456 | 95 | return id & 0xFF; |
1da177e4 LT |
96 | } |
97 | ||
cc7ec456 | 98 | static inline int route4_hash_from(u32 id) |
1da177e4 | 99 | { |
cc7ec456 | 100 | return (id >> 16) & 0xF; |
1da177e4 LT |
101 | } |
102 | ||
cc7ec456 | 103 | static inline int route4_hash_iif(int iif) |
1da177e4 | 104 | { |
cc7ec456 | 105 | return 16 + ((iif >> 16) & 0xF); |
1da177e4 LT |
106 | } |
107 | ||
cc7ec456 | 108 | static inline int route4_hash_wild(void) |
1da177e4 LT |
109 | { |
110 | return 32; | |
111 | } | |
112 | ||
113 | #define ROUTE4_APPLY_RESULT() \ | |
114 | { \ | |
115 | *res = f->res; \ | |
6fc6d06e | 116 | if (tcf_exts_has_actions(&f->exts)) { \ |
1da177e4 LT |
117 | int r = tcf_exts_exec(skb, &f->exts, res); \ |
118 | if (r < 0) { \ | |
119 | dont_cache = 1; \ | |
120 | continue; \ | |
121 | } \ | |
122 | return r; \ | |
123 | } else if (!dont_cache) \ | |
124 | route4_set_fastmap(head, id, iif, f); \ | |
125 | return 0; \ | |
126 | } | |
127 | ||
dc7f9f6e | 128 | static int route4_classify(struct sk_buff *skb, const struct tcf_proto *tp, |
1da177e4 LT |
129 | struct tcf_result *res) |
130 | { | |
1109c005 | 131 | struct route4_head *head = rcu_dereference_bh(tp->root); |
1da177e4 LT |
132 | struct dst_entry *dst; |
133 | struct route4_bucket *b; | |
134 | struct route4_filter *f; | |
135 | u32 id, h; | |
136 | int iif, dont_cache = 0; | |
137 | ||
cc7ec456 ED |
138 | dst = skb_dst(skb); |
139 | if (!dst) | |
1da177e4 LT |
140 | goto failure; |
141 | ||
142 | id = dst->tclassid; | |
1da177e4 | 143 | |
92101b3b | 144 | iif = inet_iif(skb); |
1da177e4 LT |
145 | |
146 | h = route4_fastmap_hash(id, iif); | |
1109c005 JF |
147 | |
148 | spin_lock(&fastmap_lock); | |
1da177e4 LT |
149 | if (id == head->fastmap[h].id && |
150 | iif == head->fastmap[h].iif && | |
151 | (f = head->fastmap[h].filter) != NULL) { | |
1109c005 JF |
152 | if (f == ROUTE4_FAILURE) { |
153 | spin_unlock(&fastmap_lock); | |
1da177e4 | 154 | goto failure; |
1109c005 | 155 | } |
1da177e4 LT |
156 | |
157 | *res = f->res; | |
1109c005 | 158 | spin_unlock(&fastmap_lock); |
1da177e4 LT |
159 | return 0; |
160 | } | |
1109c005 | 161 | spin_unlock(&fastmap_lock); |
1da177e4 LT |
162 | |
163 | h = route4_hash_to(id); | |
164 | ||
165 | restart: | |
1109c005 | 166 | b = rcu_dereference_bh(head->table[h]); |
cc7ec456 | 167 | if (b) { |
1109c005 JF |
168 | for (f = rcu_dereference_bh(b->ht[route4_hash_from(id)]); |
169 | f; | |
170 | f = rcu_dereference_bh(f->next)) | |
1da177e4 LT |
171 | if (f->id == id) |
172 | ROUTE4_APPLY_RESULT(); | |
173 | ||
1109c005 JF |
174 | for (f = rcu_dereference_bh(b->ht[route4_hash_iif(iif)]); |
175 | f; | |
176 | f = rcu_dereference_bh(f->next)) | |
1da177e4 LT |
177 | if (f->iif == iif) |
178 | ROUTE4_APPLY_RESULT(); | |
179 | ||
1109c005 JF |
180 | for (f = rcu_dereference_bh(b->ht[route4_hash_wild()]); |
181 | f; | |
182 | f = rcu_dereference_bh(f->next)) | |
1da177e4 | 183 | ROUTE4_APPLY_RESULT(); |
1da177e4 LT |
184 | } |
185 | if (h < 256) { | |
186 | h = 256; | |
187 | id &= ~0xFFFF; | |
188 | goto restart; | |
189 | } | |
190 | ||
191 | if (!dont_cache) | |
192 | route4_set_fastmap(head, id, iif, ROUTE4_FAILURE); | |
193 | failure: | |
194 | return -1; | |
1da177e4 LT |
195 | } |
196 | ||
197 | static inline u32 to_hash(u32 id) | |
198 | { | |
cc7ec456 ED |
199 | u32 h = id & 0xFF; |
200 | ||
201 | if (id & 0x8000) | |
1da177e4 LT |
202 | h += 256; |
203 | return h; | |
204 | } | |
205 | ||
206 | static inline u32 from_hash(u32 id) | |
207 | { | |
208 | id &= 0xFFFF; | |
209 | if (id == 0xFFFF) | |
210 | return 32; | |
211 | if (!(id & 0x8000)) { | |
212 | if (id > 255) | |
213 | return 256; | |
cc7ec456 | 214 | return id & 0xF; |
1da177e4 | 215 | } |
cc7ec456 | 216 | return 16 + (id & 0xF); |
1da177e4 LT |
217 | } |
218 | ||
8113c095 | 219 | static void *route4_get(struct tcf_proto *tp, u32 handle) |
1da177e4 | 220 | { |
1109c005 | 221 | struct route4_head *head = rtnl_dereference(tp->root); |
1da177e4 LT |
222 | struct route4_bucket *b; |
223 | struct route4_filter *f; | |
cc7ec456 | 224 | unsigned int h1, h2; |
1da177e4 | 225 | |
1da177e4 LT |
226 | h1 = to_hash(handle); |
227 | if (h1 > 256) | |
8113c095 | 228 | return NULL; |
1da177e4 | 229 | |
cc7ec456 | 230 | h2 = from_hash(handle >> 16); |
1da177e4 | 231 | if (h2 > 32) |
8113c095 | 232 | return NULL; |
1da177e4 | 233 | |
1109c005 | 234 | b = rtnl_dereference(head->table[h1]); |
cc7ec456 | 235 | if (b) { |
1109c005 JF |
236 | for (f = rtnl_dereference(b->ht[h2]); |
237 | f; | |
238 | f = rtnl_dereference(f->next)) | |
1da177e4 | 239 | if (f->handle == handle) |
8113c095 | 240 | return f; |
1da177e4 | 241 | } |
8113c095 | 242 | return NULL; |
1da177e4 LT |
243 | } |
244 | ||
1da177e4 LT |
245 | static int route4_init(struct tcf_proto *tp) |
246 | { | |
a05c2d11 WC |
247 | struct route4_head *head; |
248 | ||
249 | head = kzalloc(sizeof(struct route4_head), GFP_KERNEL); | |
250 | if (head == NULL) | |
251 | return -ENOBUFS; | |
252 | ||
253 | rcu_assign_pointer(tp->root, head); | |
1da177e4 LT |
254 | return 0; |
255 | } | |
256 | ||
3fd51de5 CW |
257 | static void __route4_delete_filter(struct route4_filter *f) |
258 | { | |
259 | tcf_exts_destroy(&f->exts); | |
260 | tcf_exts_put_net(&f->exts); | |
261 | kfree(f); | |
262 | } | |
263 | ||
c2f3f31d | 264 | static void route4_delete_filter_work(struct work_struct *work) |
1da177e4 | 265 | { |
7afe8e4f CW |
266 | struct route4_filter *f = container_of(to_rcu_work(work), |
267 | struct route4_filter, | |
268 | rwork); | |
c2f3f31d | 269 | rtnl_lock(); |
3fd51de5 | 270 | __route4_delete_filter(f); |
c2f3f31d CW |
271 | rtnl_unlock(); |
272 | } | |
273 | ||
7afe8e4f | 274 | static void route4_queue_work(struct route4_filter *f) |
c2f3f31d | 275 | { |
7afe8e4f | 276 | tcf_queue_work(&f->rwork, route4_delete_filter_work); |
1da177e4 LT |
277 | } |
278 | ||
763dbf63 | 279 | static void route4_destroy(struct tcf_proto *tp) |
1da177e4 | 280 | { |
1109c005 | 281 | struct route4_head *head = rtnl_dereference(tp->root); |
1da177e4 LT |
282 | int h1, h2; |
283 | ||
284 | if (head == NULL) | |
763dbf63 | 285 | return; |
1da177e4 | 286 | |
cc7ec456 | 287 | for (h1 = 0; h1 <= 256; h1++) { |
1da177e4 LT |
288 | struct route4_bucket *b; |
289 | ||
1109c005 | 290 | b = rtnl_dereference(head->table[h1]); |
cc7ec456 ED |
291 | if (b) { |
292 | for (h2 = 0; h2 <= 32; h2++) { | |
1da177e4 LT |
293 | struct route4_filter *f; |
294 | ||
1109c005 JF |
295 | while ((f = rtnl_dereference(b->ht[h2])) != NULL) { |
296 | struct route4_filter *next; | |
297 | ||
298 | next = rtnl_dereference(f->next); | |
299 | RCU_INIT_POINTER(b->ht[h2], next); | |
18cdb37e | 300 | tcf_unbind_filter(tp, &f->res); |
3fd51de5 | 301 | if (tcf_exts_get_net(&f->exts)) |
7afe8e4f | 302 | route4_queue_work(f); |
3fd51de5 CW |
303 | else |
304 | __route4_delete_filter(f); | |
1da177e4 LT |
305 | } |
306 | } | |
1109c005 JF |
307 | RCU_INIT_POINTER(head->table[h1], NULL); |
308 | kfree_rcu(b, rcu); | |
1da177e4 LT |
309 | } |
310 | } | |
1109c005 | 311 | kfree_rcu(head, rcu); |
1da177e4 LT |
312 | } |
313 | ||
8113c095 | 314 | static int route4_delete(struct tcf_proto *tp, void *arg, bool *last) |
1da177e4 | 315 | { |
1109c005 | 316 | struct route4_head *head = rtnl_dereference(tp->root); |
8113c095 | 317 | struct route4_filter *f = arg; |
1109c005 JF |
318 | struct route4_filter __rcu **fp; |
319 | struct route4_filter *nf; | |
1da177e4 | 320 | struct route4_bucket *b; |
1109c005 | 321 | unsigned int h = 0; |
763dbf63 | 322 | int i, h1; |
1da177e4 LT |
323 | |
324 | if (!head || !f) | |
325 | return -EINVAL; | |
326 | ||
327 | h = f->handle; | |
328 | b = f->bkt; | |
329 | ||
1109c005 JF |
330 | fp = &b->ht[from_hash(h >> 16)]; |
331 | for (nf = rtnl_dereference(*fp); nf; | |
332 | fp = &nf->next, nf = rtnl_dereference(*fp)) { | |
333 | if (nf == f) { | |
334 | /* unlink it */ | |
335 | RCU_INIT_POINTER(*fp, rtnl_dereference(f->next)); | |
1da177e4 | 336 | |
1109c005 JF |
337 | /* Remove any fastmap lookups that might ref filter |
338 | * notice we unlink'd the filter so we can't get it | |
339 | * back in the fastmap. | |
340 | */ | |
341 | route4_reset_fastmap(head); | |
1da177e4 | 342 | |
1109c005 | 343 | /* Delete it */ |
18cdb37e | 344 | tcf_unbind_filter(tp, &f->res); |
3fd51de5 | 345 | tcf_exts_get_net(&f->exts); |
7afe8e4f | 346 | tcf_queue_work(&f->rwork, route4_delete_filter_work); |
1da177e4 | 347 | |
1109c005 JF |
348 | /* Strip RTNL protected tree */ |
349 | for (i = 0; i <= 32; i++) { | |
350 | struct route4_filter *rt; | |
351 | ||
352 | rt = rtnl_dereference(b->ht[i]); | |
353 | if (rt) | |
763dbf63 | 354 | goto out; |
1109c005 | 355 | } |
1da177e4 LT |
356 | |
357 | /* OK, session has no flows */ | |
1109c005 JF |
358 | RCU_INIT_POINTER(head->table[to_hash(h)], NULL); |
359 | kfree_rcu(b, rcu); | |
763dbf63 WC |
360 | break; |
361 | } | |
362 | } | |
1da177e4 | 363 | |
763dbf63 WC |
364 | out: |
365 | *last = true; | |
366 | for (h1 = 0; h1 <= 256; h1++) { | |
367 | if (rcu_access_pointer(head->table[h1])) { | |
368 | *last = false; | |
369 | break; | |
1da177e4 LT |
370 | } |
371 | } | |
763dbf63 | 372 | |
1da177e4 LT |
373 | return 0; |
374 | } | |
375 | ||
6fa8c014 PM |
376 | static const struct nla_policy route4_policy[TCA_ROUTE4_MAX + 1] = { |
377 | [TCA_ROUTE4_CLASSID] = { .type = NLA_U32 }, | |
378 | [TCA_ROUTE4_TO] = { .type = NLA_U32 }, | |
379 | [TCA_ROUTE4_FROM] = { .type = NLA_U32 }, | |
380 | [TCA_ROUTE4_IIF] = { .type = NLA_U32 }, | |
381 | }; | |
382 | ||
c1b52739 BL |
383 | static int route4_set_parms(struct net *net, struct tcf_proto *tp, |
384 | unsigned long base, struct route4_filter *f, | |
385 | u32 handle, struct route4_head *head, | |
2f7ef2f8 CW |
386 | struct nlattr **tb, struct nlattr *est, int new, |
387 | bool ovr) | |
1da177e4 | 388 | { |
1da177e4 LT |
389 | u32 id = 0, to = 0, nhandle = 0x8000; |
390 | struct route4_filter *fp; | |
391 | unsigned int h1; | |
392 | struct route4_bucket *b; | |
b9a24bb7 | 393 | int err; |
1da177e4 | 394 | |
8c98d571 | 395 | err = tcf_exts_validate(net, tp, tb, est, &f->exts, ovr); |
1da177e4 LT |
396 | if (err < 0) |
397 | return err; | |
398 | ||
add93b61 | 399 | if (tb[TCA_ROUTE4_TO]) { |
1da177e4 | 400 | if (new && handle & 0x8000) |
8c98d571 | 401 | return -EINVAL; |
1587bac4 | 402 | to = nla_get_u32(tb[TCA_ROUTE4_TO]); |
1da177e4 | 403 | if (to > 0xFF) |
8c98d571 | 404 | return -EINVAL; |
1da177e4 LT |
405 | nhandle = to; |
406 | } | |
407 | ||
add93b61 PM |
408 | if (tb[TCA_ROUTE4_FROM]) { |
409 | if (tb[TCA_ROUTE4_IIF]) | |
8c98d571 | 410 | return -EINVAL; |
1587bac4 | 411 | id = nla_get_u32(tb[TCA_ROUTE4_FROM]); |
1da177e4 | 412 | if (id > 0xFF) |
8c98d571 | 413 | return -EINVAL; |
1da177e4 | 414 | nhandle |= id << 16; |
add93b61 | 415 | } else if (tb[TCA_ROUTE4_IIF]) { |
1587bac4 | 416 | id = nla_get_u32(tb[TCA_ROUTE4_IIF]); |
1da177e4 | 417 | if (id > 0x7FFF) |
8c98d571 | 418 | return -EINVAL; |
1da177e4 LT |
419 | nhandle |= (id | 0x8000) << 16; |
420 | } else | |
421 | nhandle |= 0xFFFF << 16; | |
422 | ||
423 | if (handle && new) { | |
424 | nhandle |= handle & 0x7F00; | |
425 | if (nhandle != handle) | |
8c98d571 | 426 | return -EINVAL; |
1da177e4 LT |
427 | } |
428 | ||
429 | h1 = to_hash(nhandle); | |
1109c005 | 430 | b = rtnl_dereference(head->table[h1]); |
cc7ec456 | 431 | if (!b) { |
0da974f4 | 432 | b = kzalloc(sizeof(struct route4_bucket), GFP_KERNEL); |
1da177e4 | 433 | if (b == NULL) |
8c98d571 | 434 | return -ENOBUFS; |
1da177e4 | 435 | |
1109c005 | 436 | rcu_assign_pointer(head->table[h1], b); |
1da177e4 LT |
437 | } else { |
438 | unsigned int h2 = from_hash(nhandle >> 16); | |
cc7ec456 | 439 | |
1109c005 JF |
440 | for (fp = rtnl_dereference(b->ht[h2]); |
441 | fp; | |
442 | fp = rtnl_dereference(fp->next)) | |
1da177e4 | 443 | if (fp->handle == f->handle) |
8c98d571 | 444 | return -EEXIST; |
1da177e4 LT |
445 | } |
446 | ||
add93b61 | 447 | if (tb[TCA_ROUTE4_TO]) |
1da177e4 LT |
448 | f->id = to; |
449 | ||
add93b61 | 450 | if (tb[TCA_ROUTE4_FROM]) |
1da177e4 | 451 | f->id = to | id<<16; |
add93b61 | 452 | else if (tb[TCA_ROUTE4_IIF]) |
1da177e4 LT |
453 | f->iif = id; |
454 | ||
455 | f->handle = nhandle; | |
456 | f->bkt = b; | |
1109c005 | 457 | f->tp = tp; |
1da177e4 | 458 | |
add93b61 | 459 | if (tb[TCA_ROUTE4_CLASSID]) { |
1587bac4 | 460 | f->res.classid = nla_get_u32(tb[TCA_ROUTE4_CLASSID]); |
1da177e4 LT |
461 | tcf_bind_filter(tp, &f->res, base); |
462 | } | |
463 | ||
1da177e4 | 464 | return 0; |
1da177e4 LT |
465 | } |
466 | ||
c1b52739 | 467 | static int route4_change(struct net *net, struct sk_buff *in_skb, |
5a7a5555 | 468 | struct tcf_proto *tp, unsigned long base, u32 handle, |
8113c095 | 469 | struct nlattr **tca, void **arg, bool ovr) |
1da177e4 | 470 | { |
1109c005 JF |
471 | struct route4_head *head = rtnl_dereference(tp->root); |
472 | struct route4_filter __rcu **fp; | |
473 | struct route4_filter *fold, *f1, *pfp, *f = NULL; | |
1da177e4 | 474 | struct route4_bucket *b; |
add93b61 PM |
475 | struct nlattr *opt = tca[TCA_OPTIONS]; |
476 | struct nlattr *tb[TCA_ROUTE4_MAX + 1]; | |
1da177e4 | 477 | unsigned int h, th; |
1da177e4 | 478 | int err; |
1109c005 | 479 | bool new = true; |
1da177e4 LT |
480 | |
481 | if (opt == NULL) | |
482 | return handle ? -EINVAL : 0; | |
483 | ||
fceb6435 | 484 | err = nla_parse_nested(tb, TCA_ROUTE4_MAX, opt, route4_policy, NULL); |
cee63723 PM |
485 | if (err < 0) |
486 | return err; | |
1da177e4 | 487 | |
8113c095 | 488 | fold = *arg; |
1109c005 | 489 | if (fold && handle && fold->handle != handle) |
1da177e4 LT |
490 | return -EINVAL; |
491 | ||
1da177e4 | 492 | err = -ENOBUFS; |
0da974f4 | 493 | f = kzalloc(sizeof(struct route4_filter), GFP_KERNEL); |
1109c005 | 494 | if (!f) |
1da177e4 | 495 | goto errout; |
1da177e4 | 496 | |
b9a24bb7 WC |
497 | err = tcf_exts_init(&f->exts, TCA_ROUTE4_ACT, TCA_ROUTE4_POLICE); |
498 | if (err < 0) | |
499 | goto errout; | |
500 | ||
1109c005 JF |
501 | if (fold) { |
502 | f->id = fold->id; | |
503 | f->iif = fold->iif; | |
504 | f->res = fold->res; | |
505 | f->handle = fold->handle; | |
506 | ||
507 | f->tp = fold->tp; | |
508 | f->bkt = fold->bkt; | |
509 | new = false; | |
510 | } | |
511 | ||
c1b52739 | 512 | err = route4_set_parms(net, tp, base, f, handle, head, tb, |
1109c005 | 513 | tca[TCA_RATE], new, ovr); |
1da177e4 LT |
514 | if (err < 0) |
515 | goto errout; | |
516 | ||
1da177e4 | 517 | h = from_hash(f->handle >> 16); |
1109c005 JF |
518 | fp = &f->bkt->ht[h]; |
519 | for (pfp = rtnl_dereference(*fp); | |
520 | (f1 = rtnl_dereference(*fp)) != NULL; | |
521 | fp = &f1->next) | |
1da177e4 LT |
522 | if (f->handle < f1->handle) |
523 | break; | |
524 | ||
02875878 | 525 | netif_keep_dst(qdisc_dev(tp->q)); |
1109c005 JF |
526 | rcu_assign_pointer(f->next, f1); |
527 | rcu_assign_pointer(*fp, f); | |
1da177e4 | 528 | |
1109c005 JF |
529 | if (fold && fold->handle && f->handle != fold->handle) { |
530 | th = to_hash(fold->handle); | |
531 | h = from_hash(fold->handle >> 16); | |
532 | b = rtnl_dereference(head->table[th]); | |
cc7ec456 | 533 | if (b) { |
1109c005 JF |
534 | fp = &b->ht[h]; |
535 | for (pfp = rtnl_dereference(*fp); pfp; | |
536 | fp = &pfp->next, pfp = rtnl_dereference(*fp)) { | |
537 | if (pfp == f) { | |
1da177e4 LT |
538 | *fp = f->next; |
539 | break; | |
540 | } | |
541 | } | |
542 | } | |
543 | } | |
1da177e4 | 544 | |
1109c005 | 545 | route4_reset_fastmap(head); |
8113c095 | 546 | *arg = f; |
18cdb37e JF |
547 | if (fold) { |
548 | tcf_unbind_filter(tp, &fold->res); | |
3fd51de5 | 549 | tcf_exts_get_net(&fold->exts); |
7afe8e4f | 550 | tcf_queue_work(&fold->rwork, route4_delete_filter_work); |
18cdb37e | 551 | } |
1da177e4 LT |
552 | return 0; |
553 | ||
554 | errout: | |
21641c2e WC |
555 | if (f) |
556 | tcf_exts_destroy(&f->exts); | |
a51482bd | 557 | kfree(f); |
1da177e4 LT |
558 | return err; |
559 | } | |
560 | ||
561 | static void route4_walk(struct tcf_proto *tp, struct tcf_walker *arg) | |
562 | { | |
1109c005 | 563 | struct route4_head *head = rtnl_dereference(tp->root); |
cc7ec456 | 564 | unsigned int h, h1; |
1da177e4 LT |
565 | |
566 | if (head == NULL) | |
567 | arg->stop = 1; | |
568 | ||
569 | if (arg->stop) | |
570 | return; | |
571 | ||
572 | for (h = 0; h <= 256; h++) { | |
1109c005 | 573 | struct route4_bucket *b = rtnl_dereference(head->table[h]); |
1da177e4 LT |
574 | |
575 | if (b) { | |
576 | for (h1 = 0; h1 <= 32; h1++) { | |
577 | struct route4_filter *f; | |
578 | ||
1109c005 JF |
579 | for (f = rtnl_dereference(b->ht[h1]); |
580 | f; | |
581 | f = rtnl_dereference(f->next)) { | |
1da177e4 LT |
582 | if (arg->count < arg->skip) { |
583 | arg->count++; | |
584 | continue; | |
585 | } | |
8113c095 | 586 | if (arg->fn(tp, f, arg) < 0) { |
1da177e4 LT |
587 | arg->stop = 1; |
588 | return; | |
589 | } | |
590 | arg->count++; | |
591 | } | |
592 | } | |
593 | } | |
594 | } | |
595 | } | |
596 | ||
8113c095 | 597 | static int route4_dump(struct net *net, struct tcf_proto *tp, void *fh, |
1da177e4 LT |
598 | struct sk_buff *skb, struct tcmsg *t) |
599 | { | |
8113c095 | 600 | struct route4_filter *f = fh; |
4b3550ef | 601 | struct nlattr *nest; |
1da177e4 LT |
602 | u32 id; |
603 | ||
604 | if (f == NULL) | |
605 | return skb->len; | |
606 | ||
607 | t->tcm_handle = f->handle; | |
608 | ||
4b3550ef PM |
609 | nest = nla_nest_start(skb, TCA_OPTIONS); |
610 | if (nest == NULL) | |
611 | goto nla_put_failure; | |
1da177e4 | 612 | |
cc7ec456 ED |
613 | if (!(f->handle & 0x8000)) { |
614 | id = f->id & 0xFF; | |
1b34ec43 DM |
615 | if (nla_put_u32(skb, TCA_ROUTE4_TO, id)) |
616 | goto nla_put_failure; | |
1da177e4 | 617 | } |
cc7ec456 | 618 | if (f->handle & 0x80000000) { |
1b34ec43 DM |
619 | if ((f->handle >> 16) != 0xFFFF && |
620 | nla_put_u32(skb, TCA_ROUTE4_IIF, f->iif)) | |
621 | goto nla_put_failure; | |
1da177e4 | 622 | } else { |
cc7ec456 | 623 | id = f->id >> 16; |
1b34ec43 DM |
624 | if (nla_put_u32(skb, TCA_ROUTE4_FROM, id)) |
625 | goto nla_put_failure; | |
1da177e4 | 626 | } |
1b34ec43 DM |
627 | if (f->res.classid && |
628 | nla_put_u32(skb, TCA_ROUTE4_CLASSID, f->res.classid)) | |
629 | goto nla_put_failure; | |
1da177e4 | 630 | |
5da57f42 | 631 | if (tcf_exts_dump(skb, &f->exts) < 0) |
add93b61 | 632 | goto nla_put_failure; |
1da177e4 | 633 | |
4b3550ef | 634 | nla_nest_end(skb, nest); |
1da177e4 | 635 | |
5da57f42 | 636 | if (tcf_exts_dump_stats(skb, &f->exts) < 0) |
add93b61 | 637 | goto nla_put_failure; |
1da177e4 LT |
638 | |
639 | return skb->len; | |
640 | ||
add93b61 | 641 | nla_put_failure: |
6ea3b446 | 642 | nla_nest_cancel(skb, nest); |
1da177e4 LT |
643 | return -1; |
644 | } | |
645 | ||
07d79fc7 CW |
646 | static void route4_bind_class(void *fh, u32 classid, unsigned long cl) |
647 | { | |
648 | struct route4_filter *f = fh; | |
649 | ||
650 | if (f && f->res.classid == classid) | |
651 | f->res.class = cl; | |
652 | } | |
653 | ||
2eb9d75c | 654 | static struct tcf_proto_ops cls_route4_ops __read_mostly = { |
1da177e4 LT |
655 | .kind = "route", |
656 | .classify = route4_classify, | |
657 | .init = route4_init, | |
658 | .destroy = route4_destroy, | |
659 | .get = route4_get, | |
1da177e4 LT |
660 | .change = route4_change, |
661 | .delete = route4_delete, | |
662 | .walk = route4_walk, | |
663 | .dump = route4_dump, | |
07d79fc7 | 664 | .bind_class = route4_bind_class, |
1da177e4 LT |
665 | .owner = THIS_MODULE, |
666 | }; | |
667 | ||
668 | static int __init init_route4(void) | |
669 | { | |
670 | return register_tcf_proto_ops(&cls_route4_ops); | |
671 | } | |
672 | ||
673 | static void __exit exit_route4(void) | |
674 | { | |
675 | unregister_tcf_proto_ops(&cls_route4_ops); | |
676 | } | |
677 | ||
678 | module_init(init_route4) | |
679 | module_exit(exit_route4) | |
680 | MODULE_LICENSE("GPL"); |