]>
Commit | Line | Data |
---|---|---|
2874c5fd | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
1da177e4 LT |
2 | /* |
3 | * net/sched/cls_fw.c Classifier mapping ipchains' fwmark to traffic class. | |
4 | * | |
1da177e4 LT |
5 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> |
6 | * | |
7 | * Changes: | |
8 | * Karlis Peisenieks <karlis@mt.lv> : 990415 : fw_walk off by one | |
9 | * Karlis Peisenieks <karlis@mt.lv> : 990415 : fw_delete killed all the filter (and kernel). | |
10 | * Alex <alex@pilotsoft.com> : 2004xxyy: Added Action extension | |
1da177e4 LT |
11 | */ |
12 | ||
1da177e4 | 13 | #include <linux/module.h> |
5a0e3ad6 | 14 | #include <linux/slab.h> |
1da177e4 LT |
15 | #include <linux/types.h> |
16 | #include <linux/kernel.h> | |
1da177e4 | 17 | #include <linux/string.h> |
1da177e4 | 18 | #include <linux/errno.h> |
1da177e4 | 19 | #include <linux/skbuff.h> |
0ba48053 | 20 | #include <net/netlink.h> |
1da177e4 LT |
21 | #include <net/act_api.h> |
22 | #include <net/pkt_cls.h> | |
1abf2720 | 23 | #include <net/sch_generic.h> |
1da177e4 | 24 | |
d37d8ac1 | 25 | #define HTSIZE 256 |
c5c13faf | 26 | |
cc7ec456 | 27 | struct fw_head { |
d37d8ac1 | 28 | u32 mask; |
e35a8ee5 JF |
29 | struct fw_filter __rcu *ht[HTSIZE]; |
30 | struct rcu_head rcu; | |
1da177e4 LT |
31 | }; |
32 | ||
cc7ec456 | 33 | struct fw_filter { |
e35a8ee5 | 34 | struct fw_filter __rcu *next; |
1da177e4 LT |
35 | u32 id; |
36 | struct tcf_result res; | |
2519a602 | 37 | int ifindex; |
1da177e4 | 38 | struct tcf_exts exts; |
e35a8ee5 | 39 | struct tcf_proto *tp; |
aaa908ff | 40 | struct rcu_work rwork; |
1da177e4 LT |
41 | }; |
42 | ||
d37d8ac1 | 43 | static u32 fw_hash(u32 handle) |
1da177e4 | 44 | { |
d37d8ac1 ED |
45 | handle ^= (handle >> 16); |
46 | handle ^= (handle >> 8); | |
47 | return handle % HTSIZE; | |
1da177e4 LT |
48 | } |
49 | ||
dc7f9f6e | 50 | static int fw_classify(struct sk_buff *skb, const struct tcf_proto *tp, |
5a7a5555 | 51 | struct tcf_result *res) |
1da177e4 | 52 | { |
e35a8ee5 | 53 | struct fw_head *head = rcu_dereference_bh(tp->root); |
1da177e4 LT |
54 | struct fw_filter *f; |
55 | int r; | |
5c804bfd | 56 | u32 id = skb->mark; |
1da177e4 LT |
57 | |
58 | if (head != NULL) { | |
5c804bfd | 59 | id &= head->mask; |
e35a8ee5 JF |
60 | |
61 | for (f = rcu_dereference_bh(head->ht[fw_hash(id)]); f; | |
62 | f = rcu_dereference_bh(f->next)) { | |
1da177e4 LT |
63 | if (f->id == id) { |
64 | *res = f->res; | |
2519a602 | 65 | if (!tcf_match_indev(skb, f->ifindex)) |
1da177e4 | 66 | continue; |
1da177e4 LT |
67 | r = tcf_exts_exec(skb, &f->exts, res); |
68 | if (r < 0) | |
69 | continue; | |
70 | ||
71 | return r; | |
72 | } | |
73 | } | |
74 | } else { | |
1abf2720 JP |
75 | struct Qdisc *q = tcf_block_q(tp->chain->block); |
76 | ||
d8aecb10 | 77 | /* Old method: classify the packet using its skb mark. */ |
cc7ec456 | 78 | if (id && (TC_H_MAJ(id) == 0 || |
1abf2720 | 79 | !(TC_H_MAJ(id ^ q->handle)))) { |
1da177e4 LT |
80 | res->classid = id; |
81 | res->class = 0; | |
82 | return 0; | |
83 | } | |
84 | } | |
85 | ||
86 | return -1; | |
87 | } | |
88 | ||
8113c095 | 89 | static void *fw_get(struct tcf_proto *tp, u32 handle) |
1da177e4 | 90 | { |
e35a8ee5 | 91 | struct fw_head *head = rtnl_dereference(tp->root); |
1da177e4 LT |
92 | struct fw_filter *f; |
93 | ||
94 | if (head == NULL) | |
8113c095 | 95 | return NULL; |
1da177e4 | 96 | |
e35a8ee5 JF |
97 | f = rtnl_dereference(head->ht[fw_hash(handle)]); |
98 | for (; f; f = rtnl_dereference(f->next)) { | |
1da177e4 | 99 | if (f->id == handle) |
8113c095 | 100 | return f; |
1da177e4 | 101 | } |
8113c095 | 102 | return NULL; |
1da177e4 LT |
103 | } |
104 | ||
1da177e4 LT |
105 | static int fw_init(struct tcf_proto *tp) |
106 | { | |
d8aecb10 WC |
107 | /* We don't allocate fw_head here, because in the old method |
108 | * we don't need it at all. | |
109 | */ | |
1da177e4 LT |
110 | return 0; |
111 | } | |
112 | ||
d5f984f5 CW |
113 | static void __fw_delete_filter(struct fw_filter *f) |
114 | { | |
115 | tcf_exts_destroy(&f->exts); | |
116 | tcf_exts_put_net(&f->exts); | |
117 | kfree(f); | |
118 | } | |
119 | ||
e071dff2 | 120 | static void fw_delete_filter_work(struct work_struct *work) |
1da177e4 | 121 | { |
aaa908ff CW |
122 | struct fw_filter *f = container_of(to_rcu_work(work), |
123 | struct fw_filter, | |
124 | rwork); | |
e071dff2 | 125 | rtnl_lock(); |
d5f984f5 | 126 | __fw_delete_filter(f); |
e071dff2 CW |
127 | rtnl_unlock(); |
128 | } | |
129 | ||
12db03b6 VB |
130 | static void fw_destroy(struct tcf_proto *tp, bool rtnl_held, |
131 | struct netlink_ext_ack *extack) | |
1da177e4 | 132 | { |
e35a8ee5 | 133 | struct fw_head *head = rtnl_dereference(tp->root); |
1da177e4 LT |
134 | struct fw_filter *f; |
135 | int h; | |
136 | ||
137 | if (head == NULL) | |
763dbf63 | 138 | return; |
1da177e4 | 139 | |
cc7ec456 | 140 | for (h = 0; h < HTSIZE; h++) { |
e35a8ee5 JF |
141 | while ((f = rtnl_dereference(head->ht[h])) != NULL) { |
142 | RCU_INIT_POINTER(head->ht[h], | |
143 | rtnl_dereference(f->next)); | |
18cdb37e | 144 | tcf_unbind_filter(tp, &f->res); |
d5f984f5 | 145 | if (tcf_exts_get_net(&f->exts)) |
aaa908ff | 146 | tcf_queue_work(&f->rwork, fw_delete_filter_work); |
d5f984f5 CW |
147 | else |
148 | __fw_delete_filter(f); | |
1da177e4 LT |
149 | } |
150 | } | |
e35a8ee5 | 151 | kfree_rcu(head, rcu); |
1da177e4 LT |
152 | } |
153 | ||
571acf21 | 154 | static int fw_delete(struct tcf_proto *tp, void *arg, bool *last, |
12db03b6 | 155 | bool rtnl_held, struct netlink_ext_ack *extack) |
1da177e4 | 156 | { |
e35a8ee5 | 157 | struct fw_head *head = rtnl_dereference(tp->root); |
8113c095 | 158 | struct fw_filter *f = arg; |
e35a8ee5 JF |
159 | struct fw_filter __rcu **fp; |
160 | struct fw_filter *pfp; | |
763dbf63 WC |
161 | int ret = -EINVAL; |
162 | int h; | |
1da177e4 LT |
163 | |
164 | if (head == NULL || f == NULL) | |
165 | goto out; | |
166 | ||
e35a8ee5 JF |
167 | fp = &head->ht[fw_hash(f->id)]; |
168 | ||
169 | for (pfp = rtnl_dereference(*fp); pfp; | |
170 | fp = &pfp->next, pfp = rtnl_dereference(*fp)) { | |
171 | if (pfp == f) { | |
172 | RCU_INIT_POINTER(*fp, rtnl_dereference(f->next)); | |
18cdb37e | 173 | tcf_unbind_filter(tp, &f->res); |
d5f984f5 | 174 | tcf_exts_get_net(&f->exts); |
aaa908ff | 175 | tcf_queue_work(&f->rwork, fw_delete_filter_work); |
763dbf63 WC |
176 | ret = 0; |
177 | break; | |
1da177e4 LT |
178 | } |
179 | } | |
763dbf63 WC |
180 | |
181 | *last = true; | |
182 | for (h = 0; h < HTSIZE; h++) { | |
183 | if (rcu_access_pointer(head->ht[h])) { | |
184 | *last = false; | |
185 | break; | |
186 | } | |
187 | } | |
188 | ||
1da177e4 | 189 | out: |
763dbf63 | 190 | return ret; |
1da177e4 LT |
191 | } |
192 | ||
6fa8c014 PM |
193 | static const struct nla_policy fw_policy[TCA_FW_MAX + 1] = { |
194 | [TCA_FW_CLASSID] = { .type = NLA_U32 }, | |
195 | [TCA_FW_INDEV] = { .type = NLA_STRING, .len = IFNAMSIZ }, | |
196 | [TCA_FW_MASK] = { .type = NLA_U32 }, | |
197 | }; | |
198 | ||
1e5003af JP |
199 | static int fw_set_parms(struct net *net, struct tcf_proto *tp, |
200 | struct fw_filter *f, struct nlattr **tb, | |
50a56190 AA |
201 | struct nlattr **tca, unsigned long base, bool ovr, |
202 | struct netlink_ext_ack *extack) | |
1da177e4 | 203 | { |
e35a8ee5 | 204 | struct fw_head *head = rtnl_dereference(tp->root); |
b4e9b520 | 205 | u32 mask; |
1da177e4 LT |
206 | int err; |
207 | ||
50a56190 | 208 | err = tcf_exts_validate(net, tp, tb, tca[TCA_RATE], &f->exts, ovr, |
ec6743a1 | 209 | true, extack); |
1da177e4 LT |
210 | if (err < 0) |
211 | return err; | |
212 | ||
add93b61 | 213 | if (tb[TCA_FW_CLASSID]) { |
1587bac4 | 214 | f->res.classid = nla_get_u32(tb[TCA_FW_CLASSID]); |
1da177e4 LT |
215 | tcf_bind_filter(tp, &f->res, base); |
216 | } | |
217 | ||
add93b61 | 218 | if (tb[TCA_FW_INDEV]) { |
2519a602 | 219 | int ret; |
1057c55f | 220 | ret = tcf_change_indev(net, tb[TCA_FW_INDEV], extack); |
94611bff JP |
221 | if (ret < 0) |
222 | return ret; | |
2519a602 | 223 | f->ifindex = ret; |
1da177e4 | 224 | } |
1da177e4 | 225 | |
cb95ec62 | 226 | err = -EINVAL; |
add93b61 | 227 | if (tb[TCA_FW_MASK]) { |
1587bac4 | 228 | mask = nla_get_u32(tb[TCA_FW_MASK]); |
b4e9b520 | 229 | if (mask != head->mask) |
94611bff | 230 | return err; |
b4e9b520 | 231 | } else if (head->mask != 0xFFFFFFFF) |
94611bff | 232 | return err; |
1da177e4 LT |
233 | |
234 | return 0; | |
1da177e4 LT |
235 | } |
236 | ||
c1b52739 | 237 | static int fw_change(struct net *net, struct sk_buff *in_skb, |
af4c6641 | 238 | struct tcf_proto *tp, unsigned long base, |
8113c095 | 239 | u32 handle, struct nlattr **tca, void **arg, |
12db03b6 VB |
240 | bool ovr, bool rtnl_held, |
241 | struct netlink_ext_ack *extack) | |
1da177e4 | 242 | { |
e35a8ee5 | 243 | struct fw_head *head = rtnl_dereference(tp->root); |
8113c095 | 244 | struct fw_filter *f = *arg; |
add93b61 PM |
245 | struct nlattr *opt = tca[TCA_OPTIONS]; |
246 | struct nlattr *tb[TCA_FW_MAX + 1]; | |
1da177e4 LT |
247 | int err; |
248 | ||
249 | if (!opt) | |
d8aecb10 | 250 | return handle ? -EINVAL : 0; /* Succeed if it is old method. */ |
1da177e4 | 251 | |
8cb08174 JB |
252 | err = nla_parse_nested_deprecated(tb, TCA_FW_MAX, opt, fw_policy, |
253 | NULL); | |
cee63723 PM |
254 | if (err < 0) |
255 | return err; | |
1da177e4 | 256 | |
e35a8ee5 JF |
257 | if (f) { |
258 | struct fw_filter *pfp, *fnew; | |
259 | struct fw_filter __rcu **fp; | |
260 | ||
1da177e4 LT |
261 | if (f->id != handle && handle) |
262 | return -EINVAL; | |
e35a8ee5 JF |
263 | |
264 | fnew = kzalloc(sizeof(struct fw_filter), GFP_KERNEL); | |
265 | if (!fnew) | |
266 | return -ENOBUFS; | |
267 | ||
268 | fnew->id = f->id; | |
269 | fnew->res = f->res; | |
e35a8ee5 | 270 | fnew->ifindex = f->ifindex; |
e35a8ee5 JF |
271 | fnew->tp = f->tp; |
272 | ||
14215108 CW |
273 | err = tcf_exts_init(&fnew->exts, net, TCA_FW_ACT, |
274 | TCA_FW_POLICE); | |
b9a24bb7 WC |
275 | if (err < 0) { |
276 | kfree(fnew); | |
277 | return err; | |
278 | } | |
e1f93eb0 | 279 | |
50a56190 | 280 | err = fw_set_parms(net, tp, fnew, tb, tca, base, ovr, extack); |
e35a8ee5 | 281 | if (err < 0) { |
b9a24bb7 | 282 | tcf_exts_destroy(&fnew->exts); |
e35a8ee5 JF |
283 | kfree(fnew); |
284 | return err; | |
285 | } | |
286 | ||
287 | fp = &head->ht[fw_hash(fnew->id)]; | |
288 | for (pfp = rtnl_dereference(*fp); pfp; | |
289 | fp = &pfp->next, pfp = rtnl_dereference(*fp)) | |
290 | if (pfp == f) | |
291 | break; | |
292 | ||
293 | RCU_INIT_POINTER(fnew->next, rtnl_dereference(pfp->next)); | |
294 | rcu_assign_pointer(*fp, fnew); | |
18cdb37e | 295 | tcf_unbind_filter(tp, &f->res); |
d5f984f5 | 296 | tcf_exts_get_net(&f->exts); |
aaa908ff | 297 | tcf_queue_work(&f->rwork, fw_delete_filter_work); |
e35a8ee5 | 298 | |
8113c095 | 299 | *arg = fnew; |
e35a8ee5 | 300 | return err; |
1da177e4 LT |
301 | } |
302 | ||
303 | if (!handle) | |
304 | return -EINVAL; | |
305 | ||
d8aecb10 WC |
306 | if (!head) { |
307 | u32 mask = 0xFFFFFFFF; | |
6fa8c014 | 308 | if (tb[TCA_FW_MASK]) |
d8aecb10 WC |
309 | mask = nla_get_u32(tb[TCA_FW_MASK]); |
310 | ||
311 | head = kzalloc(sizeof(*head), GFP_KERNEL); | |
312 | if (!head) | |
313 | return -ENOBUFS; | |
314 | head->mask = mask; | |
315 | ||
316 | rcu_assign_pointer(tp->root, head); | |
1da177e4 LT |
317 | } |
318 | ||
0da974f4 | 319 | f = kzalloc(sizeof(struct fw_filter), GFP_KERNEL); |
1da177e4 LT |
320 | if (f == NULL) |
321 | return -ENOBUFS; | |
1da177e4 | 322 | |
14215108 | 323 | err = tcf_exts_init(&f->exts, net, TCA_FW_ACT, TCA_FW_POLICE); |
b9a24bb7 WC |
324 | if (err < 0) |
325 | goto errout; | |
1da177e4 | 326 | f->id = handle; |
e35a8ee5 | 327 | f->tp = tp; |
1da177e4 | 328 | |
50a56190 | 329 | err = fw_set_parms(net, tp, f, tb, tca, base, ovr, extack); |
1da177e4 LT |
330 | if (err < 0) |
331 | goto errout; | |
332 | ||
e35a8ee5 JF |
333 | RCU_INIT_POINTER(f->next, head->ht[fw_hash(handle)]); |
334 | rcu_assign_pointer(head->ht[fw_hash(handle)], f); | |
1da177e4 | 335 | |
8113c095 | 336 | *arg = f; |
1da177e4 LT |
337 | return 0; |
338 | ||
339 | errout: | |
b9a24bb7 | 340 | tcf_exts_destroy(&f->exts); |
a51482bd | 341 | kfree(f); |
1da177e4 LT |
342 | return err; |
343 | } | |
344 | ||
12db03b6 VB |
345 | static void fw_walk(struct tcf_proto *tp, struct tcf_walker *arg, |
346 | bool rtnl_held) | |
1da177e4 | 347 | { |
e35a8ee5 | 348 | struct fw_head *head = rtnl_dereference(tp->root); |
1da177e4 LT |
349 | int h; |
350 | ||
1d997875 VB |
351 | if (head == NULL) |
352 | arg->stop = 1; | |
353 | ||
354 | if (arg->stop) | |
1da177e4 LT |
355 | return; |
356 | ||
c5c13faf | 357 | for (h = 0; h < HTSIZE; h++) { |
1da177e4 LT |
358 | struct fw_filter *f; |
359 | ||
e35a8ee5 JF |
360 | for (f = rtnl_dereference(head->ht[h]); f; |
361 | f = rtnl_dereference(f->next)) { | |
1da177e4 LT |
362 | if (arg->count < arg->skip) { |
363 | arg->count++; | |
364 | continue; | |
365 | } | |
8113c095 | 366 | if (arg->fn(tp, f, arg) < 0) { |
1da177e4 LT |
367 | arg->stop = 1; |
368 | return; | |
369 | } | |
370 | arg->count++; | |
371 | } | |
372 | } | |
373 | } | |
374 | ||
8113c095 | 375 | static int fw_dump(struct net *net, struct tcf_proto *tp, void *fh, |
12db03b6 | 376 | struct sk_buff *skb, struct tcmsg *t, bool rtnl_held) |
1da177e4 | 377 | { |
e35a8ee5 | 378 | struct fw_head *head = rtnl_dereference(tp->root); |
8113c095 | 379 | struct fw_filter *f = fh; |
4b3550ef | 380 | struct nlattr *nest; |
1da177e4 LT |
381 | |
382 | if (f == NULL) | |
383 | return skb->len; | |
384 | ||
385 | t->tcm_handle = f->id; | |
386 | ||
6fc6d06e | 387 | if (!f->res.classid && !tcf_exts_has_actions(&f->exts)) |
1da177e4 LT |
388 | return skb->len; |
389 | ||
ae0be8de | 390 | nest = nla_nest_start_noflag(skb, TCA_OPTIONS); |
4b3550ef PM |
391 | if (nest == NULL) |
392 | goto nla_put_failure; | |
1da177e4 | 393 | |
1b34ec43 DM |
394 | if (f->res.classid && |
395 | nla_put_u32(skb, TCA_FW_CLASSID, f->res.classid)) | |
396 | goto nla_put_failure; | |
2519a602 WC |
397 | if (f->ifindex) { |
398 | struct net_device *dev; | |
399 | dev = __dev_get_by_index(net, f->ifindex); | |
400 | if (dev && nla_put_string(skb, TCA_FW_INDEV, dev->name)) | |
401 | goto nla_put_failure; | |
402 | } | |
1b34ec43 DM |
403 | if (head->mask != 0xFFFFFFFF && |
404 | nla_put_u32(skb, TCA_FW_MASK, head->mask)) | |
405 | goto nla_put_failure; | |
1da177e4 | 406 | |
5da57f42 | 407 | if (tcf_exts_dump(skb, &f->exts) < 0) |
add93b61 | 408 | goto nla_put_failure; |
1da177e4 | 409 | |
4b3550ef | 410 | nla_nest_end(skb, nest); |
1da177e4 | 411 | |
5da57f42 | 412 | if (tcf_exts_dump_stats(skb, &f->exts) < 0) |
add93b61 | 413 | goto nla_put_failure; |
1da177e4 LT |
414 | |
415 | return skb->len; | |
416 | ||
add93b61 | 417 | nla_put_failure: |
6ea3b446 | 418 | nla_nest_cancel(skb, nest); |
1da177e4 LT |
419 | return -1; |
420 | } | |
421 | ||
07d79fc7 CW |
422 | static void fw_bind_class(void *fh, u32 classid, unsigned long cl) |
423 | { | |
424 | struct fw_filter *f = fh; | |
425 | ||
426 | if (f && f->res.classid == classid) | |
427 | f->res.class = cl; | |
428 | } | |
429 | ||
2eb9d75c | 430 | static struct tcf_proto_ops cls_fw_ops __read_mostly = { |
1da177e4 LT |
431 | .kind = "fw", |
432 | .classify = fw_classify, | |
433 | .init = fw_init, | |
434 | .destroy = fw_destroy, | |
435 | .get = fw_get, | |
1da177e4 LT |
436 | .change = fw_change, |
437 | .delete = fw_delete, | |
438 | .walk = fw_walk, | |
439 | .dump = fw_dump, | |
07d79fc7 | 440 | .bind_class = fw_bind_class, |
1da177e4 LT |
441 | .owner = THIS_MODULE, |
442 | }; | |
443 | ||
444 | static int __init init_fw(void) | |
445 | { | |
446 | return register_tcf_proto_ops(&cls_fw_ops); | |
447 | } | |
448 | ||
10297b99 | 449 | static void __exit exit_fw(void) |
1da177e4 LT |
450 | { |
451 | unregister_tcf_proto_ops(&cls_fw_ops); | |
452 | } | |
453 | ||
454 | module_init(init_fw) | |
455 | module_exit(exit_fw) | |
456 | MODULE_LICENSE("GPL"); |