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, | |
695176bf | 201 | struct nlattr **tca, unsigned long base, u32 flags, |
50a56190 | 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 | ||
695176bf CW |
208 | err = tcf_exts_validate(net, tp, tb, tca[TCA_RATE], &f->exts, flags, |
209 | 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, |
695176bf | 240 | u32 flags, struct netlink_ext_ack *extack) |
1da177e4 | 241 | { |
e35a8ee5 | 242 | struct fw_head *head = rtnl_dereference(tp->root); |
8113c095 | 243 | struct fw_filter *f = *arg; |
add93b61 PM |
244 | struct nlattr *opt = tca[TCA_OPTIONS]; |
245 | struct nlattr *tb[TCA_FW_MAX + 1]; | |
1da177e4 LT |
246 | int err; |
247 | ||
248 | if (!opt) | |
d8aecb10 | 249 | return handle ? -EINVAL : 0; /* Succeed if it is old method. */ |
1da177e4 | 250 | |
8cb08174 JB |
251 | err = nla_parse_nested_deprecated(tb, TCA_FW_MAX, opt, fw_policy, |
252 | NULL); | |
cee63723 PM |
253 | if (err < 0) |
254 | return err; | |
1da177e4 | 255 | |
e35a8ee5 JF |
256 | if (f) { |
257 | struct fw_filter *pfp, *fnew; | |
258 | struct fw_filter __rcu **fp; | |
259 | ||
1da177e4 LT |
260 | if (f->id != handle && handle) |
261 | return -EINVAL; | |
e35a8ee5 JF |
262 | |
263 | fnew = kzalloc(sizeof(struct fw_filter), GFP_KERNEL); | |
264 | if (!fnew) | |
265 | return -ENOBUFS; | |
266 | ||
267 | fnew->id = f->id; | |
268 | fnew->res = f->res; | |
e35a8ee5 | 269 | fnew->ifindex = f->ifindex; |
e35a8ee5 JF |
270 | fnew->tp = f->tp; |
271 | ||
14215108 CW |
272 | err = tcf_exts_init(&fnew->exts, net, TCA_FW_ACT, |
273 | TCA_FW_POLICE); | |
b9a24bb7 WC |
274 | if (err < 0) { |
275 | kfree(fnew); | |
276 | return err; | |
277 | } | |
e1f93eb0 | 278 | |
695176bf | 279 | err = fw_set_parms(net, tp, fnew, tb, tca, base, flags, extack); |
e35a8ee5 | 280 | if (err < 0) { |
b9a24bb7 | 281 | tcf_exts_destroy(&fnew->exts); |
e35a8ee5 JF |
282 | kfree(fnew); |
283 | return err; | |
284 | } | |
285 | ||
286 | fp = &head->ht[fw_hash(fnew->id)]; | |
287 | for (pfp = rtnl_dereference(*fp); pfp; | |
288 | fp = &pfp->next, pfp = rtnl_dereference(*fp)) | |
289 | if (pfp == f) | |
290 | break; | |
291 | ||
292 | RCU_INIT_POINTER(fnew->next, rtnl_dereference(pfp->next)); | |
293 | rcu_assign_pointer(*fp, fnew); | |
18cdb37e | 294 | tcf_unbind_filter(tp, &f->res); |
d5f984f5 | 295 | tcf_exts_get_net(&f->exts); |
aaa908ff | 296 | tcf_queue_work(&f->rwork, fw_delete_filter_work); |
e35a8ee5 | 297 | |
8113c095 | 298 | *arg = fnew; |
e35a8ee5 | 299 | return err; |
1da177e4 LT |
300 | } |
301 | ||
302 | if (!handle) | |
303 | return -EINVAL; | |
304 | ||
d8aecb10 WC |
305 | if (!head) { |
306 | u32 mask = 0xFFFFFFFF; | |
6fa8c014 | 307 | if (tb[TCA_FW_MASK]) |
d8aecb10 WC |
308 | mask = nla_get_u32(tb[TCA_FW_MASK]); |
309 | ||
310 | head = kzalloc(sizeof(*head), GFP_KERNEL); | |
311 | if (!head) | |
312 | return -ENOBUFS; | |
313 | head->mask = mask; | |
314 | ||
315 | rcu_assign_pointer(tp->root, head); | |
1da177e4 LT |
316 | } |
317 | ||
0da974f4 | 318 | f = kzalloc(sizeof(struct fw_filter), GFP_KERNEL); |
1da177e4 LT |
319 | if (f == NULL) |
320 | return -ENOBUFS; | |
1da177e4 | 321 | |
14215108 | 322 | err = tcf_exts_init(&f->exts, net, TCA_FW_ACT, TCA_FW_POLICE); |
b9a24bb7 WC |
323 | if (err < 0) |
324 | goto errout; | |
1da177e4 | 325 | f->id = handle; |
e35a8ee5 | 326 | f->tp = tp; |
1da177e4 | 327 | |
695176bf | 328 | err = fw_set_parms(net, tp, f, tb, tca, base, flags, extack); |
1da177e4 LT |
329 | if (err < 0) |
330 | goto errout; | |
331 | ||
e35a8ee5 JF |
332 | RCU_INIT_POINTER(f->next, head->ht[fw_hash(handle)]); |
333 | rcu_assign_pointer(head->ht[fw_hash(handle)], f); | |
1da177e4 | 334 | |
8113c095 | 335 | *arg = f; |
1da177e4 LT |
336 | return 0; |
337 | ||
338 | errout: | |
b9a24bb7 | 339 | tcf_exts_destroy(&f->exts); |
a51482bd | 340 | kfree(f); |
1da177e4 LT |
341 | return err; |
342 | } | |
343 | ||
12db03b6 VB |
344 | static void fw_walk(struct tcf_proto *tp, struct tcf_walker *arg, |
345 | bool rtnl_held) | |
1da177e4 | 346 | { |
e35a8ee5 | 347 | struct fw_head *head = rtnl_dereference(tp->root); |
1da177e4 LT |
348 | int h; |
349 | ||
1d997875 VB |
350 | if (head == NULL) |
351 | arg->stop = 1; | |
352 | ||
353 | if (arg->stop) | |
1da177e4 LT |
354 | return; |
355 | ||
c5c13faf | 356 | for (h = 0; h < HTSIZE; h++) { |
1da177e4 LT |
357 | struct fw_filter *f; |
358 | ||
e35a8ee5 JF |
359 | for (f = rtnl_dereference(head->ht[h]); f; |
360 | f = rtnl_dereference(f->next)) { | |
1da177e4 LT |
361 | if (arg->count < arg->skip) { |
362 | arg->count++; | |
363 | continue; | |
364 | } | |
8113c095 | 365 | if (arg->fn(tp, f, arg) < 0) { |
1da177e4 LT |
366 | arg->stop = 1; |
367 | return; | |
368 | } | |
369 | arg->count++; | |
370 | } | |
371 | } | |
372 | } | |
373 | ||
8113c095 | 374 | static int fw_dump(struct net *net, struct tcf_proto *tp, void *fh, |
12db03b6 | 375 | struct sk_buff *skb, struct tcmsg *t, bool rtnl_held) |
1da177e4 | 376 | { |
e35a8ee5 | 377 | struct fw_head *head = rtnl_dereference(tp->root); |
8113c095 | 378 | struct fw_filter *f = fh; |
4b3550ef | 379 | struct nlattr *nest; |
1da177e4 LT |
380 | |
381 | if (f == NULL) | |
382 | return skb->len; | |
383 | ||
384 | t->tcm_handle = f->id; | |
385 | ||
6fc6d06e | 386 | if (!f->res.classid && !tcf_exts_has_actions(&f->exts)) |
1da177e4 LT |
387 | return skb->len; |
388 | ||
ae0be8de | 389 | nest = nla_nest_start_noflag(skb, TCA_OPTIONS); |
4b3550ef PM |
390 | if (nest == NULL) |
391 | goto nla_put_failure; | |
1da177e4 | 392 | |
1b34ec43 DM |
393 | if (f->res.classid && |
394 | nla_put_u32(skb, TCA_FW_CLASSID, f->res.classid)) | |
395 | goto nla_put_failure; | |
2519a602 WC |
396 | if (f->ifindex) { |
397 | struct net_device *dev; | |
398 | dev = __dev_get_by_index(net, f->ifindex); | |
399 | if (dev && nla_put_string(skb, TCA_FW_INDEV, dev->name)) | |
400 | goto nla_put_failure; | |
401 | } | |
1b34ec43 DM |
402 | if (head->mask != 0xFFFFFFFF && |
403 | nla_put_u32(skb, TCA_FW_MASK, head->mask)) | |
404 | goto nla_put_failure; | |
1da177e4 | 405 | |
5da57f42 | 406 | if (tcf_exts_dump(skb, &f->exts) < 0) |
add93b61 | 407 | goto nla_put_failure; |
1da177e4 | 408 | |
4b3550ef | 409 | nla_nest_end(skb, nest); |
1da177e4 | 410 | |
5da57f42 | 411 | if (tcf_exts_dump_stats(skb, &f->exts) < 0) |
add93b61 | 412 | goto nla_put_failure; |
1da177e4 LT |
413 | |
414 | return skb->len; | |
415 | ||
add93b61 | 416 | nla_put_failure: |
6ea3b446 | 417 | nla_nest_cancel(skb, nest); |
1da177e4 LT |
418 | return -1; |
419 | } | |
420 | ||
2e24cd75 CW |
421 | static void fw_bind_class(void *fh, u32 classid, unsigned long cl, void *q, |
422 | unsigned long base) | |
07d79fc7 CW |
423 | { |
424 | struct fw_filter *f = fh; | |
425 | ||
2e24cd75 CW |
426 | if (f && f->res.classid == classid) { |
427 | if (cl) | |
428 | __tcf_bind_filter(q, &f->res, base); | |
429 | else | |
430 | __tcf_unbind_filter(q, &f->res); | |
431 | } | |
07d79fc7 CW |
432 | } |
433 | ||
2eb9d75c | 434 | static struct tcf_proto_ops cls_fw_ops __read_mostly = { |
1da177e4 LT |
435 | .kind = "fw", |
436 | .classify = fw_classify, | |
437 | .init = fw_init, | |
438 | .destroy = fw_destroy, | |
439 | .get = fw_get, | |
1da177e4 LT |
440 | .change = fw_change, |
441 | .delete = fw_delete, | |
442 | .walk = fw_walk, | |
443 | .dump = fw_dump, | |
07d79fc7 | 444 | .bind_class = fw_bind_class, |
1da177e4 LT |
445 | .owner = THIS_MODULE, |
446 | }; | |
447 | ||
448 | static int __init init_fw(void) | |
449 | { | |
450 | return register_tcf_proto_ops(&cls_fw_ops); | |
451 | } | |
452 | ||
10297b99 | 453 | static void __exit exit_fw(void) |
1da177e4 LT |
454 | { |
455 | unregister_tcf_proto_ops(&cls_fw_ops); | |
456 | } | |
457 | ||
458 | module_init(init_fw) | |
459 | module_exit(exit_fw) | |
460 | MODULE_LICENSE("GPL"); |