1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * net/sched/cls_u32.c Ugly (or Universal) 32bit key Packet Classifier.
4 *
5 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
6 *
7 * The filters are packed to hash tables of key nodes
8 * with a set of 32bit key/mask pairs at every node.
9 * Nodes reference next level hash tables etc.
10 *
11 * This scheme is the best universal classifier I managed to
12 * invent; it is not super-fast, but it is not slow (provided you
13 * program it correctly), and general enough. And its relative
14 * speed grows as the number of rules becomes larger.
15 *
16 * It seems that it represents the best middle point between
17 * speed and manageability both by human and by machine.
18 *
19 * It is especially useful for link sharing combined with QoS;
20 * pure RSVP doesn't need such a general approach and can use
21 * much simpler (and faster) schemes, sort of cls_rsvp.c.
22 *
23 * nfmark match added by Catalin(ux aka Dino) BOIE <catab at umbrella.ro>
24 */
25
26 #include <linux/module.h>
27 #include <linux/slab.h>
28 #include <linux/types.h>
29 #include <linux/kernel.h>
30 #include <linux/string.h>
31 #include <linux/errno.h>
32 #include <linux/percpu.h>
33 #include <linux/rtnetlink.h>
34 #include <linux/skbuff.h>
35 #include <linux/bitmap.h>
36 #include <linux/netdevice.h>
37 #include <linux/hash.h>
38 #include <net/netlink.h>
39 #include <net/act_api.h>
40 #include <net/pkt_cls.h>
41 #include <linux/idr.h>
42 #include <net/tc_wrapper.h>
43
44 struct tc_u_knode {
45 struct tc_u_knode __rcu *next;
46 u32 handle;
47 struct tc_u_hnode __rcu *ht_up;
48 struct tcf_exts exts;
49 int ifindex;
50 u8 fshift;
51 struct tcf_result res;
52 struct tc_u_hnode __rcu *ht_down;
53 #ifdef CONFIG_CLS_U32_PERF
54 struct tc_u32_pcnt __percpu *pf;
55 #endif
56 u32 flags;
57 unsigned int in_hw_count;
58 #ifdef CONFIG_CLS_U32_MARK
59 u32 val;
60 u32 mask;
61 u32 __percpu *pcpu_success;
62 #endif
63 struct rcu_work rwork;
64 /* The 'sel' field MUST be the last field in structure to allow for
65 * tc_u32_keys allocated at end of structure.
66 */
67 struct tc_u32_sel sel;
68 };
69
70 struct tc_u_hnode {
71 struct tc_u_hnode __rcu *next;
72 u32 handle;
73 u32 prio;
74 refcount_t refcnt;
75 unsigned int divisor;
76 struct idr handle_idr;
77 bool is_root;
78 struct rcu_head rcu;
79 u32 flags;
80 /* The 'ht' field MUST be the last field in structure to allow for
81 * more entries allocated at end of structure.
82 */
83 struct tc_u_knode __rcu *ht[];
84 };
85
86 struct tc_u_common {
87 struct tc_u_hnode __rcu *hlist;
88 void *ptr;
89 refcount_t refcnt;
90 struct idr handle_idr;
91 struct hlist_node hnode;
92 long knodes;
93 };
94
handle2id(u32 h)95 static u32 handle2id(u32 h)
96 {
97 return ((h & 0x80000000) ? ((h >> 20) & 0x7FF) : h);
98 }
99
id2handle(u32 id)100 static u32 id2handle(u32 id)
101 {
102 return (id | 0x800U) << 20;
103 }
104
u32_hash_fold(__be32 key,const struct tc_u32_sel * sel,u8 fshift)105 static inline unsigned int u32_hash_fold(__be32 key,
106 const struct tc_u32_sel *sel,
107 u8 fshift)
108 {
109 unsigned int h = ntohl(key & sel->hmask) >> fshift;
110
111 return h;
112 }
113
u32_classify(struct sk_buff * skb,const struct tcf_proto * tp,struct tcf_result * res)114 TC_INDIRECT_SCOPE int u32_classify(struct sk_buff *skb,
115 const struct tcf_proto *tp,
116 struct tcf_result *res)
117 {
118 struct {
119 struct tc_u_knode *knode;
120 unsigned int off;
121 } stack[TC_U32_MAXDEPTH];
122
123 struct tc_u_hnode *ht = rcu_dereference_bh(tp->root);
124 unsigned int off = skb_network_offset(skb);
125 struct tc_u_knode *n;
126 int sdepth = 0;
127 int off2 = 0;
128 int sel = 0;
129 #ifdef CONFIG_CLS_U32_PERF
130 int j;
131 #endif
132 int i, r;
133
134 next_ht:
135 n = rcu_dereference_bh(ht->ht[sel]);
136
137 next_knode:
138 if (n) {
139 struct tc_u32_key *key = n->sel.keys;
140
141 #ifdef CONFIG_CLS_U32_PERF
142 __this_cpu_inc(n->pf->rcnt);
143 j = 0;
144 #endif
145
146 if (tc_skip_sw(n->flags)) {
147 n = rcu_dereference_bh(n->next);
148 goto next_knode;
149 }
150
151 #ifdef CONFIG_CLS_U32_MARK
152 if ((skb->mark & n->mask) != n->val) {
153 n = rcu_dereference_bh(n->next);
154 goto next_knode;
155 } else {
156 __this_cpu_inc(*n->pcpu_success);
157 }
158 #endif
159
160 for (i = n->sel.nkeys; i > 0; i--, key++) {
161 int toff = off + key->off + (off2 & key->offmask);
162 __be32 *data, hdata;
163
164 data = skb_header_pointer_careful(skb, toff, 4,
165 &hdata);
166 if (!data)
167 goto out;
168 if ((*data ^ key->val) & key->mask) {
169 n = rcu_dereference_bh(n->next);
170 goto next_knode;
171 }
172 #ifdef CONFIG_CLS_U32_PERF
173 __this_cpu_inc(n->pf->kcnts[j]);
174 j++;
175 #endif
176 }
177
178 ht = rcu_dereference_bh(n->ht_down);
179 if (!ht) {
180 check_terminal:
181 if (n->sel.flags & TC_U32_TERMINAL) {
182
183 *res = n->res;
184 if (!tcf_match_indev(skb, n->ifindex)) {
185 n = rcu_dereference_bh(n->next);
186 goto next_knode;
187 }
188 #ifdef CONFIG_CLS_U32_PERF
189 __this_cpu_inc(n->pf->rhit);
190 #endif
191 r = tcf_exts_exec(skb, &n->exts, res);
192 if (r < 0) {
193 n = rcu_dereference_bh(n->next);
194 goto next_knode;
195 }
196
197 return r;
198 }
199 n = rcu_dereference_bh(n->next);
200 goto next_knode;
201 }
202
203 /* PUSH */
204 if (sdepth >= TC_U32_MAXDEPTH)
205 goto deadloop;
206 stack[sdepth].knode = n;
207 stack[sdepth].off = off;
208 sdepth++;
209
210 ht = rcu_dereference_bh(n->ht_down);
211 sel = 0;
212 if (ht->divisor) {
213 __be32 *data, hdata;
214
215 data = skb_header_pointer_careful(skb,
216 off + n->sel.hoff,
217 4, &hdata);
218 if (!data)
219 goto out;
220 sel = ht->divisor & u32_hash_fold(*data, &n->sel,
221 n->fshift);
222 }
223 if (!(n->sel.flags & (TC_U32_VAROFFSET | TC_U32_OFFSET | TC_U32_EAT)))
224 goto next_ht;
225
226 if (n->sel.flags & (TC_U32_OFFSET | TC_U32_VAROFFSET)) {
227 off2 = n->sel.off + 3;
228 if (n->sel.flags & TC_U32_VAROFFSET) {
229 __be16 *data, hdata;
230
231 data = skb_header_pointer_careful(skb,
232 off + n->sel.offoff,
233 2, &hdata);
234 if (!data)
235 goto out;
236 off2 += ntohs(n->sel.offmask & *data) >>
237 n->sel.offshift;
238 }
239 off2 &= ~3;
240 }
241 if (n->sel.flags & TC_U32_EAT) {
242 off += off2;
243 off2 = 0;
244 }
245
246 if (off < skb->len)
247 goto next_ht;
248 }
249
250 /* POP */
251 if (sdepth--) {
252 n = stack[sdepth].knode;
253 ht = rcu_dereference_bh(n->ht_up);
254 off = stack[sdepth].off;
255 goto check_terminal;
256 }
257 out:
258 return -1;
259
260 deadloop:
261 net_warn_ratelimited("cls_u32: dead loop\n");
262 return -1;
263 }
264
u32_lookup_ht(struct tc_u_common * tp_c,u32 handle)265 static struct tc_u_hnode *u32_lookup_ht(struct tc_u_common *tp_c, u32 handle)
266 {
267 struct tc_u_hnode *ht;
268
269 for (ht = rtnl_dereference(tp_c->hlist);
270 ht;
271 ht = rtnl_dereference(ht->next))
272 if (ht->handle == handle)
273 break;
274
275 return ht;
276 }
277
u32_lookup_key(struct tc_u_hnode * ht,u32 handle)278 static struct tc_u_knode *u32_lookup_key(struct tc_u_hnode *ht, u32 handle)
279 {
280 unsigned int sel;
281 struct tc_u_knode *n = NULL;
282
283 sel = TC_U32_HASH(handle);
284 if (sel > ht->divisor)
285 goto out;
286
287 for (n = rtnl_dereference(ht->ht[sel]);
288 n;
289 n = rtnl_dereference(n->next))
290 if (n->handle == handle)
291 break;
292 out:
293 return n;
294 }
295
296
u32_get(struct tcf_proto * tp,u32 handle)297 static void *u32_get(struct tcf_proto *tp, u32 handle)
298 {
299 struct tc_u_hnode *ht;
300 struct tc_u_common *tp_c = tp->data;
301
302 if (TC_U32_HTID(handle) == TC_U32_ROOT)
303 ht = rtnl_dereference(tp->root);
304 else
305 ht = u32_lookup_ht(tp_c, TC_U32_HTID(handle));
306
307 if (!ht)
308 return NULL;
309
310 if (TC_U32_KEY(handle) == 0)
311 return ht;
312
313 return u32_lookup_key(ht, handle);
314 }
315
316 /* Protected by rtnl lock */
gen_new_htid(struct tc_u_common * tp_c,struct tc_u_hnode * ptr)317 static u32 gen_new_htid(struct tc_u_common *tp_c, struct tc_u_hnode *ptr)
318 {
319 int id = idr_alloc_cyclic(&tp_c->handle_idr, ptr, 1, 0x7FF, GFP_KERNEL);
320 if (id < 0)
321 return 0;
322 return id2handle(id);
323 }
324
325 static struct hlist_head *tc_u_common_hash;
326
327 #define U32_HASH_SHIFT 10
328 #define U32_HASH_SIZE (1 << U32_HASH_SHIFT)
329
tc_u_common_ptr(const struct tcf_proto * tp)330 static void *tc_u_common_ptr(const struct tcf_proto *tp)
331 {
332 struct tcf_block *block = tp->chain->block;
333
334 /* The block sharing is currently supported only
335 * for classless qdiscs. In that case we use block
336 * for tc_u_common identification. In case the
337 * block is not shared, block->q is a valid pointer
338 * and we can use that. That works for classful qdiscs.
339 */
340 if (tcf_block_shared(block))
341 return block;
342 else
343 return block->q;
344 }
345
tc_u_hash(void * key)346 static struct hlist_head *tc_u_hash(void *key)
347 {
348 return tc_u_common_hash + hash_ptr(key, U32_HASH_SHIFT);
349 }
350
tc_u_common_find(void * key)351 static struct tc_u_common *tc_u_common_find(void *key)
352 {
353 struct tc_u_common *tc;
354 hlist_for_each_entry(tc, tc_u_hash(key), hnode) {
355 if (tc->ptr == key)
356 return tc;
357 }
358 return NULL;
359 }
360
u32_init(struct tcf_proto * tp)361 static int u32_init(struct tcf_proto *tp)
362 {
363 struct tc_u_hnode *root_ht;
364 void *key = tc_u_common_ptr(tp);
365 struct tc_u_common *tp_c = tc_u_common_find(key);
366
367 root_ht = kzalloc_flex(*root_ht, ht, 1);
368 if (root_ht == NULL)
369 return -ENOBUFS;
370
371 refcount_set(&root_ht->refcnt, 1);
372 root_ht->handle = tp_c ? gen_new_htid(tp_c, root_ht) : id2handle(0);
373 if (root_ht->handle == 0) {
374 kfree(root_ht);
375 return -ENOMEM;
376 }
377 root_ht->prio = tp->prio;
378 root_ht->is_root = true;
379 idr_init(&root_ht->handle_idr);
380
381 if (tp_c == NULL) {
382 tp_c = kzalloc_obj(*tp_c);
383 if (tp_c == NULL) {
384 kfree(root_ht);
385 return -ENOBUFS;
386 }
387 refcount_set(&tp_c->refcnt, 1);
388 tp_c->ptr = key;
389 INIT_HLIST_NODE(&tp_c->hnode);
390 idr_init(&tp_c->handle_idr);
391
392 hlist_add_head(&tp_c->hnode, tc_u_hash(key));
393 } else {
394 refcount_inc(&tp_c->refcnt);
395 }
396
397 RCU_INIT_POINTER(root_ht->next, tp_c->hlist);
398 rcu_assign_pointer(tp_c->hlist, root_ht);
399
400 /* root_ht must be destroyed when tcf_proto is destroyed */
401 rcu_assign_pointer(tp->root, root_ht);
402 tp->data = tp_c;
403 return 0;
404 }
405
__u32_destroy_key(struct tc_u_knode * n)406 static void __u32_destroy_key(struct tc_u_knode *n)
407 {
408 struct tc_u_hnode *ht = rtnl_dereference(n->ht_down);
409
410 tcf_exts_destroy(&n->exts);
411 if (ht && refcount_dec_and_test(&ht->refcnt))
412 kfree(ht);
413 kfree(n);
414 }
415
u32_destroy_key(struct tc_u_knode * n,bool free_pf)416 static void u32_destroy_key(struct tc_u_knode *n, bool free_pf)
417 {
418 tcf_exts_put_net(&n->exts);
419 #ifdef CONFIG_CLS_U32_PERF
420 if (free_pf)
421 free_percpu(n->pf);
422 #endif
423 #ifdef CONFIG_CLS_U32_MARK
424 if (free_pf)
425 free_percpu(n->pcpu_success);
426 #endif
427 __u32_destroy_key(n);
428 }
429
430 /* u32_delete_key_rcu should be called when free'ing a copied
431 * version of a tc_u_knode obtained from u32_init_knode(). When
432 * copies are obtained from u32_init_knode() the statistics are
433 * shared between the old and new copies to allow readers to
434 * continue to update the statistics during the copy. To support
435 * this the u32_delete_key_rcu variant does not free the percpu
436 * statistics.
437 */
u32_delete_key_work(struct work_struct * work)438 static void u32_delete_key_work(struct work_struct *work)
439 {
440 struct tc_u_knode *key = container_of(to_rcu_work(work),
441 struct tc_u_knode,
442 rwork);
443 rtnl_lock();
444 u32_destroy_key(key, false);
445 rtnl_unlock();
446 }
447
448 /* u32_delete_key_freepf_rcu is the rcu callback variant
449 * that free's the entire structure including the statistics
450 * percpu variables. Only use this if the key is not a copy
451 * returned by u32_init_knode(). See u32_delete_key_rcu()
452 * for the variant that should be used with keys return from
453 * u32_init_knode()
454 */
u32_delete_key_freepf_work(struct work_struct * work)455 static void u32_delete_key_freepf_work(struct work_struct *work)
456 {
457 struct tc_u_knode *key = container_of(to_rcu_work(work),
458 struct tc_u_knode,
459 rwork);
460 rtnl_lock();
461 u32_destroy_key(key, true);
462 rtnl_unlock();
463 }
464
u32_delete_key(struct tcf_proto * tp,struct tc_u_knode * key)465 static int u32_delete_key(struct tcf_proto *tp, struct tc_u_knode *key)
466 {
467 struct tc_u_common *tp_c = tp->data;
468 struct tc_u_knode __rcu **kp;
469 struct tc_u_knode *pkp;
470 struct tc_u_hnode *ht = rtnl_dereference(key->ht_up);
471
472 if (ht) {
473 kp = &ht->ht[TC_U32_HASH(key->handle)];
474 for (pkp = rtnl_dereference(*kp); pkp;
475 kp = &pkp->next, pkp = rtnl_dereference(*kp)) {
476 if (pkp == key) {
477 RCU_INIT_POINTER(*kp, key->next);
478 tp_c->knodes--;
479
480 tcf_unbind_filter(tp, &key->res);
481 idr_remove(&ht->handle_idr, key->handle);
482 tcf_exts_get_net(&key->exts);
483 tcf_queue_work(&key->rwork, u32_delete_key_freepf_work);
484 return 0;
485 }
486 }
487 }
488 WARN_ON(1);
489 return 0;
490 }
491
u32_clear_hw_hnode(struct tcf_proto * tp,struct tc_u_hnode * h,struct netlink_ext_ack * extack)492 static void u32_clear_hw_hnode(struct tcf_proto *tp, struct tc_u_hnode *h,
493 struct netlink_ext_ack *extack)
494 {
495 struct tcf_block *block = tp->chain->block;
496 struct tc_cls_u32_offload cls_u32 = {};
497
498 tc_cls_common_offload_init(&cls_u32.common, tp, h->flags, extack);
499 cls_u32.command = TC_CLSU32_DELETE_HNODE;
500 cls_u32.hnode.divisor = h->divisor;
501 cls_u32.hnode.handle = h->handle;
502 cls_u32.hnode.prio = h->prio;
503
504 tc_setup_cb_call(block, TC_SETUP_CLSU32, &cls_u32, false, true);
505 }
506
u32_replace_hw_hnode(struct tcf_proto * tp,struct tc_u_hnode * h,u32 flags,struct netlink_ext_ack * extack)507 static int u32_replace_hw_hnode(struct tcf_proto *tp, struct tc_u_hnode *h,
508 u32 flags, struct netlink_ext_ack *extack)
509 {
510 struct tcf_block *block = tp->chain->block;
511 struct tc_cls_u32_offload cls_u32 = {};
512 bool skip_sw = tc_skip_sw(flags);
513 bool offloaded = false;
514 int err;
515
516 tc_cls_common_offload_init(&cls_u32.common, tp, flags, extack);
517 cls_u32.command = TC_CLSU32_NEW_HNODE;
518 cls_u32.hnode.divisor = h->divisor;
519 cls_u32.hnode.handle = h->handle;
520 cls_u32.hnode.prio = h->prio;
521
522 err = tc_setup_cb_call(block, TC_SETUP_CLSU32, &cls_u32, skip_sw, true);
523 if (err < 0) {
524 u32_clear_hw_hnode(tp, h, NULL);
525 return err;
526 } else if (err > 0) {
527 offloaded = true;
528 }
529
530 if (skip_sw && !offloaded)
531 return -EINVAL;
532
533 return 0;
534 }
535
u32_remove_hw_knode(struct tcf_proto * tp,struct tc_u_knode * n,struct netlink_ext_ack * extack)536 static void u32_remove_hw_knode(struct tcf_proto *tp, struct tc_u_knode *n,
537 struct netlink_ext_ack *extack)
538 {
539 struct tcf_block *block = tp->chain->block;
540 struct tc_cls_u32_offload cls_u32 = {};
541
542 tc_cls_common_offload_init(&cls_u32.common, tp, n->flags, extack);
543 cls_u32.command = TC_CLSU32_DELETE_KNODE;
544 cls_u32.knode.handle = n->handle;
545
546 tc_setup_cb_destroy(block, tp, TC_SETUP_CLSU32, &cls_u32, false,
547 &n->flags, &n->in_hw_count, true);
548 }
549
u32_replace_hw_knode(struct tcf_proto * tp,struct tc_u_knode * n,u32 flags,struct netlink_ext_ack * extack)550 static int u32_replace_hw_knode(struct tcf_proto *tp, struct tc_u_knode *n,
551 u32 flags, struct netlink_ext_ack *extack)
552 {
553 struct tc_u_hnode *ht = rtnl_dereference(n->ht_down);
554 struct tcf_block *block = tp->chain->block;
555 struct tc_cls_u32_offload cls_u32 = {};
556 bool skip_sw = tc_skip_sw(flags);
557 int err;
558
559 tc_cls_common_offload_init(&cls_u32.common, tp, flags, extack);
560 cls_u32.command = TC_CLSU32_REPLACE_KNODE;
561 cls_u32.knode.handle = n->handle;
562 cls_u32.knode.fshift = n->fshift;
563 #ifdef CONFIG_CLS_U32_MARK
564 cls_u32.knode.val = n->val;
565 cls_u32.knode.mask = n->mask;
566 #else
567 cls_u32.knode.val = 0;
568 cls_u32.knode.mask = 0;
569 #endif
570 cls_u32.knode.sel = &n->sel;
571 cls_u32.knode.res = &n->res;
572 cls_u32.knode.exts = &n->exts;
573 if (n->ht_down)
574 cls_u32.knode.link_handle = ht->handle;
575
576 err = tc_setup_cb_add(block, tp, TC_SETUP_CLSU32, &cls_u32, skip_sw,
577 &n->flags, &n->in_hw_count, true);
578 if (err) {
579 u32_remove_hw_knode(tp, n, NULL);
580 return err;
581 }
582
583 if (skip_sw && !(n->flags & TCA_CLS_FLAGS_IN_HW))
584 return -EINVAL;
585
586 return 0;
587 }
588
u32_clear_hnode(struct tcf_proto * tp,struct tc_u_hnode * ht,struct netlink_ext_ack * extack)589 static void u32_clear_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht,
590 struct netlink_ext_ack *extack)
591 {
592 struct tc_u_common *tp_c = tp->data;
593 struct tc_u_knode *n;
594 unsigned int h;
595
596 for (h = 0; h <= ht->divisor; h++) {
597 while ((n = rtnl_dereference(ht->ht[h])) != NULL) {
598 RCU_INIT_POINTER(ht->ht[h],
599 rtnl_dereference(n->next));
600 tp_c->knodes--;
601 tcf_unbind_filter(tp, &n->res);
602 u32_remove_hw_knode(tp, n, extack);
603 idr_remove(&ht->handle_idr, n->handle);
604 if (tcf_exts_get_net(&n->exts))
605 tcf_queue_work(&n->rwork, u32_delete_key_freepf_work);
606 else
607 u32_destroy_key(n, true);
608 }
609 }
610 }
611
u32_destroy_hnode(struct tcf_proto * tp,struct tc_u_hnode * ht,struct netlink_ext_ack * extack)612 static int u32_destroy_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht,
613 struct netlink_ext_ack *extack)
614 {
615 struct tc_u_common *tp_c = tp->data;
616 struct tc_u_hnode __rcu **hn;
617 struct tc_u_hnode *phn;
618
619 u32_clear_hnode(tp, ht, extack);
620
621 hn = &tp_c->hlist;
622 for (phn = rtnl_dereference(*hn);
623 phn;
624 hn = &phn->next, phn = rtnl_dereference(*hn)) {
625 if (phn == ht) {
626 u32_clear_hw_hnode(tp, ht, extack);
627 idr_destroy(&ht->handle_idr);
628 idr_remove(&tp_c->handle_idr, handle2id(ht->handle));
629 RCU_INIT_POINTER(*hn, ht->next);
630 kfree_rcu(ht, rcu);
631 return 0;
632 }
633 }
634
635 return -ENOENT;
636 }
637
u32_destroy(struct tcf_proto * tp,bool rtnl_held,struct netlink_ext_ack * extack)638 static void u32_destroy(struct tcf_proto *tp, bool rtnl_held,
639 struct netlink_ext_ack *extack)
640 {
641 struct tc_u_common *tp_c = tp->data;
642 struct tc_u_hnode *root_ht = rtnl_dereference(tp->root);
643
644 WARN_ON(root_ht == NULL);
645
646 if (root_ht && refcount_dec_and_test(&root_ht->refcnt))
647 u32_destroy_hnode(tp, root_ht, extack);
648
649 if (refcount_dec_and_test(&tp_c->refcnt)) {
650 struct tc_u_hnode *ht;
651
652 hlist_del(&tp_c->hnode);
653
654 while ((ht = rtnl_dereference(tp_c->hlist)) != NULL) {
655 u32_clear_hnode(tp, ht, extack);
656 RCU_INIT_POINTER(tp_c->hlist, ht->next);
657
658 /* u32_destroy_key() will later free ht for us, if it's
659 * still referenced by some knode
660 */
661 if (refcount_dec_and_test(&ht->refcnt))
662 kfree_rcu(ht, rcu);
663 }
664
665 idr_destroy(&tp_c->handle_idr);
666 kfree(tp_c);
667 }
668
669 tp->data = NULL;
670 }
671
u32_delete(struct tcf_proto * tp,void * arg,bool * last,bool rtnl_held,struct netlink_ext_ack * extack)672 static int u32_delete(struct tcf_proto *tp, void *arg, bool *last,
673 bool rtnl_held, struct netlink_ext_ack *extack)
674 {
675 struct tc_u_hnode *ht = arg;
676 struct tc_u_common *tp_c = tp->data;
677 int ret = 0;
678
679 if (TC_U32_KEY(ht->handle)) {
680 u32_remove_hw_knode(tp, (struct tc_u_knode *)ht, extack);
681 ret = u32_delete_key(tp, (struct tc_u_knode *)ht);
682 goto out;
683 }
684
685 if (ht->is_root) {
686 NL_SET_ERR_MSG_MOD(extack, "Not allowed to delete root node");
687 return -EINVAL;
688 }
689
690 if (refcount_dec_if_one(&ht->refcnt)) {
691 u32_destroy_hnode(tp, ht, extack);
692 } else {
693 NL_SET_ERR_MSG_MOD(extack, "Can not delete in-use filter");
694 return -EBUSY;
695 }
696
697 out:
698 *last = refcount_read(&tp_c->refcnt) == 1 && tp_c->knodes == 0;
699 return ret;
700 }
701
gen_new_kid(struct tc_u_hnode * ht,u32 htid,int * err)702 static u32 gen_new_kid(struct tc_u_hnode *ht, u32 htid, int *err)
703 {
704 u32 index = htid | 0x800;
705 u32 max = htid | 0xFFF;
706
707 *err = 0;
708
709 if (idr_alloc_u32(&ht->handle_idr, NULL, &index, max, GFP_KERNEL)) {
710 index = htid + 1;
711 *err = idr_alloc_u32(&ht->handle_idr, NULL, &index, max,
712 GFP_KERNEL);
713 if (*err)
714 return 0;
715 }
716
717 return index;
718 }
719
u32_kid_extack(int err,struct netlink_ext_ack * extack)720 static int u32_kid_extack(int err, struct netlink_ext_ack *extack)
721 {
722 if (err == -ENOSPC)
723 NL_SET_ERR_MSG_MOD(extack, "Hash table node ID pool exhausted");
724 else
725 NL_SET_ERR_MSG_MOD(extack, "Failed to allocate node ID");
726 return err;
727 }
728
729 static const struct nla_policy u32_policy[TCA_U32_MAX + 1] = {
730 [TCA_U32_CLASSID] = { .type = NLA_U32 },
731 [TCA_U32_HASH] = { .type = NLA_U32 },
732 [TCA_U32_LINK] = { .type = NLA_U32 },
733 [TCA_U32_DIVISOR] = { .type = NLA_U32 },
734 [TCA_U32_SEL] = { .len = sizeof(struct tc_u32_sel) },
735 [TCA_U32_INDEV] = { .type = NLA_STRING, .len = IFNAMSIZ },
736 [TCA_U32_MARK] = { .len = sizeof(struct tc_u32_mark) },
737 [TCA_U32_FLAGS] = { .type = NLA_U32 },
738 };
739
u32_unbind_filter(struct tcf_proto * tp,struct tc_u_knode * n,struct nlattr ** tb)740 static void u32_unbind_filter(struct tcf_proto *tp, struct tc_u_knode *n,
741 struct nlattr **tb)
742 {
743 if (tb[TCA_U32_CLASSID])
744 tcf_unbind_filter(tp, &n->res);
745 }
746
u32_bind_filter(struct tcf_proto * tp,struct tc_u_knode * n,unsigned long base,struct nlattr ** tb)747 static void u32_bind_filter(struct tcf_proto *tp, struct tc_u_knode *n,
748 unsigned long base, struct nlattr **tb)
749 {
750 if (tb[TCA_U32_CLASSID]) {
751 n->res.classid = nla_get_u32(tb[TCA_U32_CLASSID]);
752 tcf_bind_filter(tp, &n->res, base);
753 }
754 }
755
u32_set_parms(struct net * net,struct tcf_proto * tp,struct tc_u_knode * n,struct nlattr ** tb,struct nlattr * est,u32 flags,u32 fl_flags,struct netlink_ext_ack * extack)756 static int u32_set_parms(struct net *net, struct tcf_proto *tp,
757 struct tc_u_knode *n, struct nlattr **tb,
758 struct nlattr *est, u32 flags, u32 fl_flags,
759 struct netlink_ext_ack *extack)
760 {
761 int err, ifindex = -1;
762
763 err = tcf_exts_validate_ex(net, tp, tb, est, &n->exts, flags,
764 fl_flags, extack);
765 if (err < 0)
766 return err;
767
768 if (tb[TCA_U32_INDEV]) {
769 ifindex = tcf_change_indev(net, tb[TCA_U32_INDEV], extack);
770 if (ifindex < 0)
771 return -EINVAL;
772 }
773
774 if (tb[TCA_U32_LINK]) {
775 u32 handle = nla_get_u32(tb[TCA_U32_LINK]);
776 struct tc_u_hnode *ht_down = NULL, *ht_old;
777
778 if (TC_U32_KEY(handle)) {
779 NL_SET_ERR_MSG_MOD(extack, "u32 Link handle must be a hash table");
780 return -EINVAL;
781 }
782
783 if (handle) {
784 ht_down = u32_lookup_ht(tp->data, handle);
785
786 if (!ht_down) {
787 NL_SET_ERR_MSG_MOD(extack, "Link hash table not found");
788 return -EINVAL;
789 }
790 if (ht_down->is_root) {
791 NL_SET_ERR_MSG_MOD(extack, "Not linking to root node");
792 return -EINVAL;
793 }
794 refcount_inc(&ht_down->refcnt);
795 }
796
797 ht_old = rtnl_dereference(n->ht_down);
798 rcu_assign_pointer(n->ht_down, ht_down);
799
800 if (ht_old)
801 refcount_dec(&ht_old->refcnt);
802 }
803
804 if (ifindex >= 0)
805 n->ifindex = ifindex;
806
807 return 0;
808 }
809
u32_replace_knode(struct tcf_proto * tp,struct tc_u_common * tp_c,struct tc_u_knode * n)810 static void u32_replace_knode(struct tcf_proto *tp, struct tc_u_common *tp_c,
811 struct tc_u_knode *n)
812 {
813 struct tc_u_knode __rcu **ins;
814 struct tc_u_knode *pins;
815 struct tc_u_hnode *ht;
816
817 if (TC_U32_HTID(n->handle) == TC_U32_ROOT)
818 ht = rtnl_dereference(tp->root);
819 else
820 ht = u32_lookup_ht(tp_c, TC_U32_HTID(n->handle));
821
822 ins = &ht->ht[TC_U32_HASH(n->handle)];
823
824 /* The node must always exist for it to be replaced if this is not the
825 * case then something went very wrong elsewhere.
826 */
827 for (pins = rtnl_dereference(*ins); ;
828 ins = &pins->next, pins = rtnl_dereference(*ins))
829 if (pins->handle == n->handle)
830 break;
831
832 idr_replace(&ht->handle_idr, n, n->handle);
833 RCU_INIT_POINTER(n->next, pins->next);
834 rcu_assign_pointer(*ins, n);
835 }
836
u32_init_knode(struct net * net,struct tcf_proto * tp,struct tc_u_knode * n)837 static struct tc_u_knode *u32_init_knode(struct net *net, struct tcf_proto *tp,
838 struct tc_u_knode *n)
839 {
840 struct tc_u_hnode *ht = rtnl_dereference(n->ht_down);
841 struct tc_u32_sel *s = &n->sel;
842 struct tc_u_knode *new;
843
844 new = kzalloc_flex(*new, sel.keys, s->nkeys, GFP_KERNEL_ACCOUNT);
845 if (!new)
846 return NULL;
847
848 RCU_INIT_POINTER(new->next, n->next);
849 new->handle = n->handle;
850 RCU_INIT_POINTER(new->ht_up, n->ht_up);
851
852 new->ifindex = n->ifindex;
853 new->fshift = n->fshift;
854 new->flags = n->flags;
855 RCU_INIT_POINTER(new->ht_down, ht);
856
857 #ifdef CONFIG_CLS_U32_PERF
858 /* Statistics may be incremented by readers during update
859 * so we must keep them in tact. When the node is later destroyed
860 * a special destroy call must be made to not free the pf memory.
861 */
862 new->pf = n->pf;
863 #endif
864
865 #ifdef CONFIG_CLS_U32_MARK
866 new->val = n->val;
867 new->mask = n->mask;
868 /* Similarly success statistics must be moved as pointers */
869 new->pcpu_success = n->pcpu_success;
870 #endif
871 unsafe_memcpy(&new->sel, s, struct_size(s, keys, s->nkeys),
872 /* A composite flex-array structure destination,
873 * which was correctly sized with kzalloc_flex(),
874 * above. */);
875
876 if (tcf_exts_init(&new->exts, net, TCA_U32_ACT, TCA_U32_POLICE)) {
877 kfree(new);
878 return NULL;
879 }
880
881 /* bump reference count as long as we hold pointer to structure */
882 if (ht)
883 refcount_inc(&ht->refcnt);
884
885 return new;
886 }
887
u32_change(struct net * net,struct sk_buff * in_skb,struct tcf_proto * tp,unsigned long base,u32 handle,struct nlattr ** tca,void ** arg,u32 flags,struct netlink_ext_ack * extack)888 static int u32_change(struct net *net, struct sk_buff *in_skb,
889 struct tcf_proto *tp, unsigned long base, u32 handle,
890 struct nlattr **tca, void **arg, u32 flags,
891 struct netlink_ext_ack *extack)
892 {
893 struct tc_u_common *tp_c = tp->data;
894 struct tc_u_hnode *ht;
895 struct tc_u_knode *n;
896 struct tc_u32_sel *s;
897 struct nlattr *opt = tca[TCA_OPTIONS];
898 struct nlattr *tb[TCA_U32_MAX + 1];
899 u32 htid, userflags = 0;
900 size_t sel_size;
901 int err;
902
903 if (!opt) {
904 if (handle) {
905 NL_SET_ERR_MSG_MOD(extack, "Filter handle requires options");
906 return -EINVAL;
907 } else {
908 return 0;
909 }
910 }
911
912 err = nla_parse_nested_deprecated(tb, TCA_U32_MAX, opt, u32_policy,
913 extack);
914 if (err < 0)
915 return err;
916
917 if (tb[TCA_U32_FLAGS]) {
918 userflags = nla_get_u32(tb[TCA_U32_FLAGS]);
919 if (!tc_flags_valid(userflags)) {
920 NL_SET_ERR_MSG_MOD(extack, "Invalid filter flags");
921 return -EINVAL;
922 }
923 }
924
925 n = *arg;
926 if (n) {
927 struct tc_u_knode *new;
928
929 if (TC_U32_KEY(n->handle) == 0) {
930 NL_SET_ERR_MSG_MOD(extack, "Key node id cannot be zero");
931 return -EINVAL;
932 }
933
934 if ((n->flags ^ userflags) &
935 ~(TCA_CLS_FLAGS_IN_HW | TCA_CLS_FLAGS_NOT_IN_HW)) {
936 NL_SET_ERR_MSG_MOD(extack, "Key node flags do not match passed flags");
937 return -EINVAL;
938 }
939
940 new = u32_init_knode(net, tp, n);
941 if (!new)
942 return -ENOMEM;
943
944 err = u32_set_parms(net, tp, new, tb, tca[TCA_RATE],
945 flags, new->flags, extack);
946
947 if (err) {
948 __u32_destroy_key(new);
949 return err;
950 }
951
952 u32_bind_filter(tp, new, base, tb);
953
954 err = u32_replace_hw_knode(tp, new, flags, extack);
955 if (err) {
956 u32_unbind_filter(tp, new, tb);
957
958 if (tb[TCA_U32_LINK]) {
959 struct tc_u_hnode *ht_old;
960
961 ht_old = rtnl_dereference(n->ht_down);
962 if (ht_old)
963 refcount_inc(&ht_old->refcnt);
964 }
965 __u32_destroy_key(new);
966 return err;
967 }
968
969 if (!tc_in_hw(new->flags))
970 new->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
971
972 tcf_proto_update_usesw(tp, new->flags);
973
974 u32_replace_knode(tp, tp_c, new);
975 tcf_unbind_filter(tp, &n->res);
976 tcf_exts_get_net(&n->exts);
977 tcf_queue_work(&n->rwork, u32_delete_key_work);
978 return 0;
979 }
980
981 if (tb[TCA_U32_DIVISOR]) {
982 unsigned int divisor = nla_get_u32(tb[TCA_U32_DIVISOR]);
983
984 if (!is_power_of_2(divisor)) {
985 NL_SET_ERR_MSG_MOD(extack, "Divisor is not a power of 2");
986 return -EINVAL;
987 }
988 if (divisor-- > 0x100) {
989 NL_SET_ERR_MSG_MOD(extack, "Exceeded maximum 256 hash buckets");
990 return -EINVAL;
991 }
992 if (TC_U32_KEY(handle)) {
993 NL_SET_ERR_MSG_MOD(extack, "Divisor can only be used on a hash table");
994 return -EINVAL;
995 }
996 ht = kzalloc_flex(*ht, ht, divisor + 1);
997 if (ht == NULL)
998 return -ENOBUFS;
999 if (handle == 0) {
1000 handle = gen_new_htid(tp->data, ht);
1001 if (handle == 0) {
1002 kfree(ht);
1003 return -ENOMEM;
1004 }
1005 } else {
1006 err = idr_alloc_u32(&tp_c->handle_idr, ht, &handle,
1007 handle, GFP_KERNEL);
1008 if (err) {
1009 kfree(ht);
1010 return err;
1011 }
1012 }
1013 refcount_set(&ht->refcnt, 1);
1014 ht->divisor = divisor;
1015 ht->handle = handle;
1016 ht->prio = tp->prio;
1017 idr_init(&ht->handle_idr);
1018 ht->flags = userflags;
1019
1020 err = u32_replace_hw_hnode(tp, ht, userflags, extack);
1021 if (err) {
1022 idr_remove(&tp_c->handle_idr, handle2id(handle));
1023 kfree(ht);
1024 return err;
1025 }
1026
1027 RCU_INIT_POINTER(ht->next, tp_c->hlist);
1028 rcu_assign_pointer(tp_c->hlist, ht);
1029 *arg = ht;
1030
1031 return 0;
1032 }
1033
1034 if (tb[TCA_U32_HASH]) {
1035 htid = nla_get_u32(tb[TCA_U32_HASH]);
1036 if (TC_U32_HTID(htid) == TC_U32_ROOT) {
1037 ht = rtnl_dereference(tp->root);
1038 htid = ht->handle;
1039 } else {
1040 ht = u32_lookup_ht(tp->data, TC_U32_HTID(htid));
1041 if (!ht) {
1042 NL_SET_ERR_MSG_MOD(extack, "Specified hash table not found");
1043 return -EINVAL;
1044 }
1045 }
1046 } else {
1047 ht = rtnl_dereference(tp->root);
1048 htid = ht->handle;
1049 }
1050
1051 if (ht->divisor < TC_U32_HASH(htid)) {
1052 NL_SET_ERR_MSG_MOD(extack, "Specified hash table buckets exceed configured value");
1053 return -EINVAL;
1054 }
1055
1056 /* At this point, we need to derive the new handle that will be used to
1057 * uniquely map the identity of this table match entry. The
1058 * identity of the entry that we need to construct is 32 bits made of:
1059 * htid(12b):bucketid(8b):node/entryid(12b)
1060 *
1061 * At this point _we have the table(ht)_ in which we will insert this
1062 * entry. We carry the table's id in variable "htid".
1063 * Note that earlier code picked the ht selection either by a) the user
1064 * providing the htid specified via TCA_U32_HASH attribute or b) when
1065 * no such attribute is passed then the root ht, is default to at ID
1066 * 0x[800][00][000]. Rule: the root table has a single bucket with ID 0.
1067 * If OTOH the user passed us the htid, they may also pass a bucketid of
1068 * choice. 0 is fine. For example a user htid is 0x[600][01][000] it is
1069 * indicating hash bucketid of 1. Rule: the entry/node ID _cannot_ be
1070 * passed via the htid, so even if it was non-zero it will be ignored.
1071 *
1072 * We may also have a handle, if the user passed one. The handle also
1073 * carries the same addressing of htid(12b):bucketid(8b):node/entryid(12b).
1074 * Rule: the bucketid on the handle is ignored even if one was passed;
1075 * rather the value on "htid" is always assumed to be the bucketid.
1076 */
1077 if (handle) {
1078 /* Rule: The htid from handle and tableid from htid must match */
1079 if (TC_U32_HTID(handle) && TC_U32_HTID(handle ^ htid)) {
1080 NL_SET_ERR_MSG_MOD(extack, "Handle specified hash table address mismatch");
1081 return -EINVAL;
1082 }
1083 /* Ok, so far we have a valid htid(12b):bucketid(8b) but we
1084 * need to finalize the table entry identification with the last
1085 * part - the node/entryid(12b)). Rule: Nodeid _cannot be 0_ for
1086 * entries. Rule: nodeid of 0 is reserved only for tables(see
1087 * earlier code which processes TC_U32_DIVISOR attribute).
1088 * Rule: The nodeid can only be derived from the handle (and not
1089 * htid).
1090 * Rule: if the handle specified zero for the node id example
1091 * 0x60000000, then pick a new nodeid from the pool of IDs
1092 * this hash table has been allocating from.
1093 * If OTOH it is specified (i.e for example the user passed a
1094 * handle such as 0x60000123), then we use it generate our final
1095 * handle which is used to uniquely identify the match entry.
1096 */
1097 if (!TC_U32_NODE(handle)) {
1098 handle = gen_new_kid(ht, htid, &err);
1099 if (err)
1100 return u32_kid_extack(err, extack);
1101 } else {
1102 handle = htid | TC_U32_NODE(handle);
1103 err = idr_alloc_u32(&ht->handle_idr, NULL, &handle,
1104 handle, GFP_KERNEL);
1105 if (err)
1106 return err;
1107 }
1108 } else {
1109 /* The user did not give us a handle; lets just generate one
1110 * from the table's pool of nodeids.
1111 */
1112 handle = gen_new_kid(ht, htid, &err);
1113 if (err)
1114 return u32_kid_extack(err, extack);
1115 }
1116
1117 if (tb[TCA_U32_SEL] == NULL) {
1118 NL_SET_ERR_MSG_MOD(extack, "Selector not specified");
1119 err = -EINVAL;
1120 goto erridr;
1121 }
1122
1123 s = nla_data(tb[TCA_U32_SEL]);
1124 sel_size = struct_size(s, keys, s->nkeys);
1125 if (nla_len(tb[TCA_U32_SEL]) < sel_size) {
1126 err = -EINVAL;
1127 goto erridr;
1128 }
1129
1130 if (s->offshift >= 16) {
1131 NL_SET_ERR_MSG_MOD(extack,
1132 "offshift must be less than 16");
1133 err = -EINVAL;
1134 goto erridr;
1135 }
1136
1137 n = kzalloc_flex(*n, sel.keys, s->nkeys, GFP_KERNEL_ACCOUNT);
1138 if (n == NULL) {
1139 err = -ENOBUFS;
1140 goto erridr;
1141 }
1142
1143 #ifdef CONFIG_CLS_U32_PERF
1144 n->pf = __alloc_percpu_gfp(struct_size(n->pf, kcnts, s->nkeys),
1145 __alignof__(struct tc_u32_pcnt),
1146 GFP_KERNEL_ACCOUNT);
1147 if (!n->pf) {
1148 err = -ENOBUFS;
1149 goto errfree;
1150 }
1151 #endif
1152
1153 unsafe_memcpy(&n->sel, s, sel_size,
1154 /* A composite flex-array structure destination,
1155 * which was correctly sized with struct_size(),
1156 * bounds-checked against nla_len(), and allocated
1157 * above. */);
1158 RCU_INIT_POINTER(n->ht_up, ht);
1159 n->handle = handle;
1160 n->fshift = s->hmask ? ffs(ntohl(s->hmask)) - 1 : 0;
1161 n->flags = userflags;
1162
1163 err = tcf_exts_init(&n->exts, net, TCA_U32_ACT, TCA_U32_POLICE);
1164 if (err < 0)
1165 goto errout;
1166
1167 #ifdef CONFIG_CLS_U32_MARK
1168 n->pcpu_success = alloc_percpu_gfp(u32, GFP_KERNEL_ACCOUNT);
1169 if (!n->pcpu_success) {
1170 err = -ENOMEM;
1171 goto errout;
1172 }
1173
1174 if (tb[TCA_U32_MARK]) {
1175 struct tc_u32_mark *mark;
1176
1177 mark = nla_data(tb[TCA_U32_MARK]);
1178 n->val = mark->val;
1179 n->mask = mark->mask;
1180 }
1181 #endif
1182
1183 err = u32_set_parms(net, tp, n, tb, tca[TCA_RATE],
1184 flags, n->flags, extack);
1185
1186 u32_bind_filter(tp, n, base, tb);
1187
1188 if (err == 0) {
1189 struct tc_u_knode __rcu **ins;
1190 struct tc_u_knode *pins;
1191
1192 err = u32_replace_hw_knode(tp, n, flags, extack);
1193 if (err)
1194 goto errunbind;
1195
1196 if (!tc_in_hw(n->flags))
1197 n->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
1198
1199 tcf_proto_update_usesw(tp, n->flags);
1200
1201 ins = &ht->ht[TC_U32_HASH(handle)];
1202 for (pins = rtnl_dereference(*ins); pins;
1203 ins = &pins->next, pins = rtnl_dereference(*ins))
1204 if (TC_U32_NODE(handle) < TC_U32_NODE(pins->handle))
1205 break;
1206
1207 RCU_INIT_POINTER(n->next, pins);
1208 rcu_assign_pointer(*ins, n);
1209 tp_c->knodes++;
1210 *arg = n;
1211 return 0;
1212 }
1213
1214 errunbind:
1215 u32_unbind_filter(tp, n, tb);
1216
1217 #ifdef CONFIG_CLS_U32_MARK
1218 free_percpu(n->pcpu_success);
1219 #endif
1220
1221 errout:
1222 tcf_exts_destroy(&n->exts);
1223 #ifdef CONFIG_CLS_U32_PERF
1224 errfree:
1225 free_percpu(n->pf);
1226 #endif
1227 kfree(n);
1228 erridr:
1229 idr_remove(&ht->handle_idr, handle);
1230 return err;
1231 }
1232
u32_walk(struct tcf_proto * tp,struct tcf_walker * arg,bool rtnl_held)1233 static void u32_walk(struct tcf_proto *tp, struct tcf_walker *arg,
1234 bool rtnl_held)
1235 {
1236 struct tc_u_common *tp_c = tp->data;
1237 struct tc_u_hnode *ht;
1238 struct tc_u_knode *n;
1239 unsigned int h;
1240
1241 if (arg->stop)
1242 return;
1243
1244 for (ht = rtnl_dereference(tp_c->hlist);
1245 ht;
1246 ht = rtnl_dereference(ht->next)) {
1247 if (ht->prio != tp->prio)
1248 continue;
1249
1250 if (!tc_cls_stats_dump(tp, arg, ht))
1251 return;
1252
1253 for (h = 0; h <= ht->divisor; h++) {
1254 for (n = rtnl_dereference(ht->ht[h]);
1255 n;
1256 n = rtnl_dereference(n->next)) {
1257 if (!tc_cls_stats_dump(tp, arg, n))
1258 return;
1259 }
1260 }
1261 }
1262 }
1263
u32_reoffload_hnode(struct tcf_proto * tp,struct tc_u_hnode * ht,bool add,flow_setup_cb_t * cb,void * cb_priv,struct netlink_ext_ack * extack)1264 static int u32_reoffload_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht,
1265 bool add, flow_setup_cb_t *cb, void *cb_priv,
1266 struct netlink_ext_ack *extack)
1267 {
1268 struct tc_cls_u32_offload cls_u32 = {};
1269 int err;
1270
1271 tc_cls_common_offload_init(&cls_u32.common, tp, ht->flags, extack);
1272 cls_u32.command = add ? TC_CLSU32_NEW_HNODE : TC_CLSU32_DELETE_HNODE;
1273 cls_u32.hnode.divisor = ht->divisor;
1274 cls_u32.hnode.handle = ht->handle;
1275 cls_u32.hnode.prio = ht->prio;
1276
1277 err = cb(TC_SETUP_CLSU32, &cls_u32, cb_priv);
1278 if (err && add && tc_skip_sw(ht->flags))
1279 return err;
1280
1281 return 0;
1282 }
1283
u32_reoffload_knode(struct tcf_proto * tp,struct tc_u_knode * n,bool add,flow_setup_cb_t * cb,void * cb_priv,struct netlink_ext_ack * extack)1284 static int u32_reoffload_knode(struct tcf_proto *tp, struct tc_u_knode *n,
1285 bool add, flow_setup_cb_t *cb, void *cb_priv,
1286 struct netlink_ext_ack *extack)
1287 {
1288 struct tc_u_hnode *ht = rtnl_dereference(n->ht_down);
1289 struct tcf_block *block = tp->chain->block;
1290 struct tc_cls_u32_offload cls_u32 = {};
1291
1292 tc_cls_common_offload_init(&cls_u32.common, tp, n->flags, extack);
1293 cls_u32.command = add ?
1294 TC_CLSU32_REPLACE_KNODE : TC_CLSU32_DELETE_KNODE;
1295 cls_u32.knode.handle = n->handle;
1296
1297 if (add) {
1298 cls_u32.knode.fshift = n->fshift;
1299 #ifdef CONFIG_CLS_U32_MARK
1300 cls_u32.knode.val = n->val;
1301 cls_u32.knode.mask = n->mask;
1302 #else
1303 cls_u32.knode.val = 0;
1304 cls_u32.knode.mask = 0;
1305 #endif
1306 cls_u32.knode.sel = &n->sel;
1307 cls_u32.knode.res = &n->res;
1308 cls_u32.knode.exts = &n->exts;
1309 if (n->ht_down)
1310 cls_u32.knode.link_handle = ht->handle;
1311 }
1312
1313 return tc_setup_cb_reoffload(block, tp, add, cb, TC_SETUP_CLSU32,
1314 &cls_u32, cb_priv, &n->flags,
1315 &n->in_hw_count);
1316 }
1317
u32_reoffload(struct tcf_proto * tp,bool add,flow_setup_cb_t * cb,void * cb_priv,struct netlink_ext_ack * extack)1318 static int u32_reoffload(struct tcf_proto *tp, bool add, flow_setup_cb_t *cb,
1319 void *cb_priv, struct netlink_ext_ack *extack)
1320 {
1321 struct tc_u_common *tp_c = tp->data;
1322 struct tc_u_hnode *ht;
1323 struct tc_u_knode *n;
1324 unsigned int h;
1325 int err;
1326
1327 for (ht = rtnl_dereference(tp_c->hlist);
1328 ht;
1329 ht = rtnl_dereference(ht->next)) {
1330 if (ht->prio != tp->prio)
1331 continue;
1332
1333 /* When adding filters to a new dev, try to offload the
1334 * hashtable first. When removing, do the filters before the
1335 * hashtable.
1336 */
1337 if (add && !tc_skip_hw(ht->flags)) {
1338 err = u32_reoffload_hnode(tp, ht, add, cb, cb_priv,
1339 extack);
1340 if (err)
1341 return err;
1342 }
1343
1344 for (h = 0; h <= ht->divisor; h++) {
1345 for (n = rtnl_dereference(ht->ht[h]);
1346 n;
1347 n = rtnl_dereference(n->next)) {
1348 if (tc_skip_hw(n->flags))
1349 continue;
1350
1351 err = u32_reoffload_knode(tp, n, add, cb,
1352 cb_priv, extack);
1353 if (err)
1354 return err;
1355 }
1356 }
1357
1358 if (!add && !tc_skip_hw(ht->flags))
1359 u32_reoffload_hnode(tp, ht, add, cb, cb_priv, extack);
1360 }
1361
1362 return 0;
1363 }
1364
u32_bind_class(void * fh,u32 classid,unsigned long cl,void * q,unsigned long base)1365 static void u32_bind_class(void *fh, u32 classid, unsigned long cl, void *q,
1366 unsigned long base)
1367 {
1368 struct tc_u_knode *n = fh;
1369
1370 if (TC_U32_KEY(n->handle) == 0)
1371 return;
1372
1373 tc_cls_bind_class(classid, cl, q, &n->res, base);
1374 }
1375
u32_dump(struct net * net,struct tcf_proto * tp,void * fh,struct sk_buff * skb,struct tcmsg * t,bool rtnl_held)1376 static int u32_dump(struct net *net, struct tcf_proto *tp, void *fh,
1377 struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
1378 {
1379 struct tc_u_knode *n = fh;
1380 struct tc_u_hnode *ht_up, *ht_down;
1381 struct nlattr *nest;
1382
1383 if (n == NULL)
1384 return skb->len;
1385
1386 t->tcm_handle = n->handle;
1387
1388 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
1389 if (nest == NULL)
1390 goto nla_put_failure;
1391
1392 if (TC_U32_KEY(n->handle) == 0) {
1393 struct tc_u_hnode *ht = fh;
1394 u32 divisor = ht->divisor + 1;
1395
1396 if (nla_put_u32(skb, TCA_U32_DIVISOR, divisor))
1397 goto nla_put_failure;
1398 } else {
1399 #ifdef CONFIG_CLS_U32_PERF
1400 struct tc_u32_pcnt *gpf;
1401 int cpu;
1402 #endif
1403
1404 if (nla_put(skb, TCA_U32_SEL, struct_size(&n->sel, keys, n->sel.nkeys),
1405 &n->sel))
1406 goto nla_put_failure;
1407
1408 ht_up = rtnl_dereference(n->ht_up);
1409 if (ht_up) {
1410 u32 htid = n->handle & 0xFFFFF000;
1411 if (nla_put_u32(skb, TCA_U32_HASH, htid))
1412 goto nla_put_failure;
1413 }
1414 if (n->res.classid &&
1415 nla_put_u32(skb, TCA_U32_CLASSID, n->res.classid))
1416 goto nla_put_failure;
1417
1418 ht_down = rtnl_dereference(n->ht_down);
1419 if (ht_down &&
1420 nla_put_u32(skb, TCA_U32_LINK, ht_down->handle))
1421 goto nla_put_failure;
1422
1423 if (n->flags && nla_put_u32(skb, TCA_U32_FLAGS, n->flags))
1424 goto nla_put_failure;
1425
1426 #ifdef CONFIG_CLS_U32_MARK
1427 if ((n->val || n->mask)) {
1428 struct tc_u32_mark mark = {.val = n->val,
1429 .mask = n->mask,
1430 .success = 0};
1431 int cpum;
1432
1433 for_each_possible_cpu(cpum) {
1434 __u32 cnt = *per_cpu_ptr(n->pcpu_success, cpum);
1435
1436 mark.success += cnt;
1437 }
1438
1439 if (nla_put(skb, TCA_U32_MARK, sizeof(mark), &mark))
1440 goto nla_put_failure;
1441 }
1442 #endif
1443
1444 if (tcf_exts_dump(skb, &n->exts) < 0)
1445 goto nla_put_failure;
1446
1447 if (n->ifindex) {
1448 struct net_device *dev;
1449 dev = __dev_get_by_index(net, n->ifindex);
1450 if (dev && nla_put_string(skb, TCA_U32_INDEV, dev->name))
1451 goto nla_put_failure;
1452 }
1453 #ifdef CONFIG_CLS_U32_PERF
1454 gpf = kzalloc_flex(*gpf, kcnts, n->sel.nkeys);
1455 if (!gpf)
1456 goto nla_put_failure;
1457
1458 for_each_possible_cpu(cpu) {
1459 int i;
1460 struct tc_u32_pcnt *pf = per_cpu_ptr(n->pf, cpu);
1461
1462 gpf->rcnt += pf->rcnt;
1463 gpf->rhit += pf->rhit;
1464 for (i = 0; i < n->sel.nkeys; i++)
1465 gpf->kcnts[i] += pf->kcnts[i];
1466 }
1467
1468 if (nla_put_64bit(skb, TCA_U32_PCNT, struct_size(gpf, kcnts, n->sel.nkeys),
1469 gpf, TCA_U32_PAD)) {
1470 kfree(gpf);
1471 goto nla_put_failure;
1472 }
1473 kfree(gpf);
1474 #endif
1475 }
1476
1477 nla_nest_end(skb, nest);
1478
1479 if (TC_U32_KEY(n->handle))
1480 if (tcf_exts_dump_stats(skb, &n->exts) < 0)
1481 goto nla_put_failure;
1482 return skb->len;
1483
1484 nla_put_failure:
1485 nla_nest_cancel(skb, nest);
1486 return -1;
1487 }
1488
1489 static struct tcf_proto_ops cls_u32_ops __read_mostly = {
1490 .kind = "u32",
1491 .classify = u32_classify,
1492 .init = u32_init,
1493 .destroy = u32_destroy,
1494 .get = u32_get,
1495 .change = u32_change,
1496 .delete = u32_delete,
1497 .walk = u32_walk,
1498 .reoffload = u32_reoffload,
1499 .dump = u32_dump,
1500 .bind_class = u32_bind_class,
1501 .owner = THIS_MODULE,
1502 };
1503 MODULE_ALIAS_NET_CLS("u32");
1504
init_u32(void)1505 static int __init init_u32(void)
1506 {
1507 int i, ret;
1508
1509 pr_info("u32 classifier\n");
1510 #ifdef CONFIG_CLS_U32_PERF
1511 pr_info(" Performance counters on\n");
1512 #endif
1513 pr_info(" input device check on\n");
1514 #ifdef CONFIG_NET_CLS_ACT
1515 pr_info(" Actions configured\n");
1516 #endif
1517 tc_u_common_hash = kvmalloc_objs(struct hlist_head, U32_HASH_SIZE);
1518 if (!tc_u_common_hash)
1519 return -ENOMEM;
1520
1521 for (i = 0; i < U32_HASH_SIZE; i++)
1522 INIT_HLIST_HEAD(&tc_u_common_hash[i]);
1523
1524 ret = register_tcf_proto_ops(&cls_u32_ops);
1525 if (ret)
1526 kvfree(tc_u_common_hash);
1527 return ret;
1528 }
1529
exit_u32(void)1530 static void __exit exit_u32(void)
1531 {
1532 unregister_tcf_proto_ops(&cls_u32_ops);
1533 kvfree(tc_u_common_hash);
1534 }
1535
1536 module_init(init_u32)
1537 module_exit(exit_u32)
1538 MODULE_DESCRIPTION("Universal 32bit based TC Classifier");
1539 MODULE_LICENSE("GPL");
1540