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 /* The IDR is keyed on the mapped id, and that is
1007 * what the destroy paths remove. Ask for it here,
1008 * so a manual handle colliding with the
1009 * auto-allocated id space is rejected (-ENOSPC)
1010 * instead of aliasing a future auto id.
1011 */
1012 u32 id = handle2id(handle);
1013
1014 err = idr_alloc_u32(&tp_c->handle_idr, ht, &id, id,
1015 GFP_KERNEL);
1016 if (err) {
1017 kfree(ht);
1018 return err;
1019 }
1020 }
1021 refcount_set(&ht->refcnt, 1);
1022 ht->divisor = divisor;
1023 ht->handle = handle;
1024 ht->prio = tp->prio;
1025 idr_init(&ht->handle_idr);
1026 ht->flags = userflags;
1027
1028 err = u32_replace_hw_hnode(tp, ht, userflags, extack);
1029 if (err) {
1030 idr_remove(&tp_c->handle_idr, handle2id(handle));
1031 kfree(ht);
1032 return err;
1033 }
1034
1035 RCU_INIT_POINTER(ht->next, tp_c->hlist);
1036 rcu_assign_pointer(tp_c->hlist, ht);
1037 *arg = ht;
1038
1039 return 0;
1040 }
1041
1042 if (tb[TCA_U32_HASH]) {
1043 htid = nla_get_u32(tb[TCA_U32_HASH]);
1044 if (TC_U32_HTID(htid) == TC_U32_ROOT) {
1045 ht = rtnl_dereference(tp->root);
1046 htid = ht->handle;
1047 } else {
1048 ht = u32_lookup_ht(tp->data, TC_U32_HTID(htid));
1049 if (!ht) {
1050 NL_SET_ERR_MSG_MOD(extack, "Specified hash table not found");
1051 return -EINVAL;
1052 }
1053 }
1054 } else {
1055 ht = rtnl_dereference(tp->root);
1056 htid = ht->handle;
1057 }
1058
1059 if (ht->divisor < TC_U32_HASH(htid)) {
1060 NL_SET_ERR_MSG_MOD(extack, "Specified hash table buckets exceed configured value");
1061 return -EINVAL;
1062 }
1063
1064 /* At this point, we need to derive the new handle that will be used to
1065 * uniquely map the identity of this table match entry. The
1066 * identity of the entry that we need to construct is 32 bits made of:
1067 * htid(12b):bucketid(8b):node/entryid(12b)
1068 *
1069 * At this point _we have the table(ht)_ in which we will insert this
1070 * entry. We carry the table's id in variable "htid".
1071 * Note that earlier code picked the ht selection either by a) the user
1072 * providing the htid specified via TCA_U32_HASH attribute or b) when
1073 * no such attribute is passed then the root ht, is default to at ID
1074 * 0x[800][00][000]. Rule: the root table has a single bucket with ID 0.
1075 * If OTOH the user passed us the htid, they may also pass a bucketid of
1076 * choice. 0 is fine. For example a user htid is 0x[600][01][000] it is
1077 * indicating hash bucketid of 1. Rule: the entry/node ID _cannot_ be
1078 * passed via the htid, so even if it was non-zero it will be ignored.
1079 *
1080 * We may also have a handle, if the user passed one. The handle also
1081 * carries the same addressing of htid(12b):bucketid(8b):node/entryid(12b).
1082 * Rule: the bucketid on the handle is ignored even if one was passed;
1083 * rather the value on "htid" is always assumed to be the bucketid.
1084 */
1085 if (handle) {
1086 /* Rule: The htid from handle and tableid from htid must match */
1087 if (TC_U32_HTID(handle) && TC_U32_HTID(handle ^ htid)) {
1088 NL_SET_ERR_MSG_MOD(extack, "Handle specified hash table address mismatch");
1089 return -EINVAL;
1090 }
1091 /* Ok, so far we have a valid htid(12b):bucketid(8b) but we
1092 * need to finalize the table entry identification with the last
1093 * part - the node/entryid(12b)). Rule: Nodeid _cannot be 0_ for
1094 * entries. Rule: nodeid of 0 is reserved only for tables(see
1095 * earlier code which processes TC_U32_DIVISOR attribute).
1096 * Rule: The nodeid can only be derived from the handle (and not
1097 * htid).
1098 * Rule: if the handle specified zero for the node id example
1099 * 0x60000000, then pick a new nodeid from the pool of IDs
1100 * this hash table has been allocating from.
1101 * If OTOH it is specified (i.e for example the user passed a
1102 * handle such as 0x60000123), then we use it generate our final
1103 * handle which is used to uniquely identify the match entry.
1104 */
1105 if (!TC_U32_NODE(handle)) {
1106 handle = gen_new_kid(ht, htid, &err);
1107 if (err)
1108 return u32_kid_extack(err, extack);
1109 } else {
1110 handle = htid | TC_U32_NODE(handle);
1111 err = idr_alloc_u32(&ht->handle_idr, NULL, &handle,
1112 handle, GFP_KERNEL);
1113 if (err)
1114 return err;
1115 }
1116 } else {
1117 /* The user did not give us a handle; lets just generate one
1118 * from the table's pool of nodeids.
1119 */
1120 handle = gen_new_kid(ht, htid, &err);
1121 if (err)
1122 return u32_kid_extack(err, extack);
1123 }
1124
1125 if (tb[TCA_U32_SEL] == NULL) {
1126 NL_SET_ERR_MSG_MOD(extack, "Selector not specified");
1127 err = -EINVAL;
1128 goto erridr;
1129 }
1130
1131 s = nla_data(tb[TCA_U32_SEL]);
1132 sel_size = struct_size(s, keys, s->nkeys);
1133 if (nla_len(tb[TCA_U32_SEL]) < sel_size) {
1134 err = -EINVAL;
1135 goto erridr;
1136 }
1137
1138 if (s->offshift >= 16) {
1139 NL_SET_ERR_MSG_MOD(extack,
1140 "offshift must be less than 16");
1141 err = -EINVAL;
1142 goto erridr;
1143 }
1144
1145 n = kzalloc_flex(*n, sel.keys, s->nkeys, GFP_KERNEL_ACCOUNT);
1146 if (n == NULL) {
1147 err = -ENOBUFS;
1148 goto erridr;
1149 }
1150
1151 #ifdef CONFIG_CLS_U32_PERF
1152 n->pf = __alloc_percpu_gfp(struct_size(n->pf, kcnts, s->nkeys),
1153 __alignof__(struct tc_u32_pcnt),
1154 GFP_KERNEL_ACCOUNT);
1155 if (!n->pf) {
1156 err = -ENOBUFS;
1157 goto errfree;
1158 }
1159 #endif
1160
1161 unsafe_memcpy(&n->sel, s, sel_size,
1162 /* A composite flex-array structure destination,
1163 * which was correctly sized with struct_size(),
1164 * bounds-checked against nla_len(), and allocated
1165 * above. */);
1166 RCU_INIT_POINTER(n->ht_up, ht);
1167 n->handle = handle;
1168 n->fshift = s->hmask ? ffs(ntohl(s->hmask)) - 1 : 0;
1169 n->flags = userflags;
1170
1171 err = tcf_exts_init(&n->exts, net, TCA_U32_ACT, TCA_U32_POLICE);
1172 if (err < 0)
1173 goto errout;
1174
1175 #ifdef CONFIG_CLS_U32_MARK
1176 n->pcpu_success = alloc_percpu_gfp(u32, GFP_KERNEL_ACCOUNT);
1177 if (!n->pcpu_success) {
1178 err = -ENOMEM;
1179 goto errout;
1180 }
1181
1182 if (tb[TCA_U32_MARK]) {
1183 struct tc_u32_mark *mark;
1184
1185 mark = nla_data(tb[TCA_U32_MARK]);
1186 n->val = mark->val;
1187 n->mask = mark->mask;
1188 }
1189 #endif
1190
1191 err = u32_set_parms(net, tp, n, tb, tca[TCA_RATE],
1192 flags, n->flags, extack);
1193
1194 u32_bind_filter(tp, n, base, tb);
1195
1196 if (err == 0) {
1197 struct tc_u_knode __rcu **ins;
1198 struct tc_u_knode *pins;
1199
1200 err = u32_replace_hw_knode(tp, n, flags, extack);
1201 if (err)
1202 goto errunbind;
1203
1204 if (!tc_in_hw(n->flags))
1205 n->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
1206
1207 tcf_proto_update_usesw(tp, n->flags);
1208
1209 ins = &ht->ht[TC_U32_HASH(handle)];
1210 for (pins = rtnl_dereference(*ins); pins;
1211 ins = &pins->next, pins = rtnl_dereference(*ins))
1212 if (TC_U32_NODE(handle) < TC_U32_NODE(pins->handle))
1213 break;
1214
1215 RCU_INIT_POINTER(n->next, pins);
1216 rcu_assign_pointer(*ins, n);
1217 tp_c->knodes++;
1218 *arg = n;
1219 return 0;
1220 }
1221
1222 errunbind:
1223 u32_unbind_filter(tp, n, tb);
1224
1225 #ifdef CONFIG_CLS_U32_MARK
1226 free_percpu(n->pcpu_success);
1227 #endif
1228
1229 errout:
1230 tcf_exts_destroy(&n->exts);
1231 #ifdef CONFIG_CLS_U32_PERF
1232 errfree:
1233 free_percpu(n->pf);
1234 #endif
1235 kfree(n);
1236 erridr:
1237 idr_remove(&ht->handle_idr, handle);
1238 return err;
1239 }
1240
u32_walk(struct tcf_proto * tp,struct tcf_walker * arg,bool rtnl_held)1241 static void u32_walk(struct tcf_proto *tp, struct tcf_walker *arg,
1242 bool rtnl_held)
1243 {
1244 struct tc_u_common *tp_c = tp->data;
1245 struct tc_u_hnode *ht;
1246 struct tc_u_knode *n;
1247 unsigned int h;
1248
1249 if (arg->stop)
1250 return;
1251
1252 for (ht = rtnl_dereference(tp_c->hlist);
1253 ht;
1254 ht = rtnl_dereference(ht->next)) {
1255 if (ht->prio != tp->prio)
1256 continue;
1257
1258 if (!tc_cls_stats_dump(tp, arg, ht))
1259 return;
1260
1261 for (h = 0; h <= ht->divisor; h++) {
1262 for (n = rtnl_dereference(ht->ht[h]);
1263 n;
1264 n = rtnl_dereference(n->next)) {
1265 if (!tc_cls_stats_dump(tp, arg, n))
1266 return;
1267 }
1268 }
1269 }
1270 }
1271
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)1272 static int u32_reoffload_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht,
1273 bool add, flow_setup_cb_t *cb, void *cb_priv,
1274 struct netlink_ext_ack *extack)
1275 {
1276 struct tc_cls_u32_offload cls_u32 = {};
1277 int err;
1278
1279 tc_cls_common_offload_init(&cls_u32.common, tp, ht->flags, extack);
1280 cls_u32.command = add ? TC_CLSU32_NEW_HNODE : TC_CLSU32_DELETE_HNODE;
1281 cls_u32.hnode.divisor = ht->divisor;
1282 cls_u32.hnode.handle = ht->handle;
1283 cls_u32.hnode.prio = ht->prio;
1284
1285 err = cb(TC_SETUP_CLSU32, &cls_u32, cb_priv);
1286 if (err && add && tc_skip_sw(ht->flags))
1287 return err;
1288
1289 return 0;
1290 }
1291
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)1292 static int u32_reoffload_knode(struct tcf_proto *tp, struct tc_u_knode *n,
1293 bool add, flow_setup_cb_t *cb, void *cb_priv,
1294 struct netlink_ext_ack *extack)
1295 {
1296 struct tc_u_hnode *ht = rtnl_dereference(n->ht_down);
1297 struct tcf_block *block = tp->chain->block;
1298 struct tc_cls_u32_offload cls_u32 = {};
1299
1300 tc_cls_common_offload_init(&cls_u32.common, tp, n->flags, extack);
1301 cls_u32.command = add ?
1302 TC_CLSU32_REPLACE_KNODE : TC_CLSU32_DELETE_KNODE;
1303 cls_u32.knode.handle = n->handle;
1304
1305 if (add) {
1306 cls_u32.knode.fshift = n->fshift;
1307 #ifdef CONFIG_CLS_U32_MARK
1308 cls_u32.knode.val = n->val;
1309 cls_u32.knode.mask = n->mask;
1310 #else
1311 cls_u32.knode.val = 0;
1312 cls_u32.knode.mask = 0;
1313 #endif
1314 cls_u32.knode.sel = &n->sel;
1315 cls_u32.knode.res = &n->res;
1316 cls_u32.knode.exts = &n->exts;
1317 if (n->ht_down)
1318 cls_u32.knode.link_handle = ht->handle;
1319 }
1320
1321 return tc_setup_cb_reoffload(block, tp, add, cb, TC_SETUP_CLSU32,
1322 &cls_u32, cb_priv, &n->flags,
1323 &n->in_hw_count);
1324 }
1325
u32_reoffload(struct tcf_proto * tp,bool add,flow_setup_cb_t * cb,void * cb_priv,struct netlink_ext_ack * extack)1326 static int u32_reoffload(struct tcf_proto *tp, bool add, flow_setup_cb_t *cb,
1327 void *cb_priv, struct netlink_ext_ack *extack)
1328 {
1329 struct tc_u_common *tp_c = tp->data;
1330 struct tc_u_hnode *ht;
1331 struct tc_u_knode *n;
1332 unsigned int h;
1333 int err;
1334
1335 for (ht = rtnl_dereference(tp_c->hlist);
1336 ht;
1337 ht = rtnl_dereference(ht->next)) {
1338 if (ht->prio != tp->prio)
1339 continue;
1340
1341 /* When adding filters to a new dev, try to offload the
1342 * hashtable first. When removing, do the filters before the
1343 * hashtable.
1344 */
1345 if (add && !tc_skip_hw(ht->flags)) {
1346 err = u32_reoffload_hnode(tp, ht, add, cb, cb_priv,
1347 extack);
1348 if (err)
1349 return err;
1350 }
1351
1352 for (h = 0; h <= ht->divisor; h++) {
1353 for (n = rtnl_dereference(ht->ht[h]);
1354 n;
1355 n = rtnl_dereference(n->next)) {
1356 if (tc_skip_hw(n->flags))
1357 continue;
1358
1359 err = u32_reoffload_knode(tp, n, add, cb,
1360 cb_priv, extack);
1361 if (err)
1362 return err;
1363 }
1364 }
1365
1366 if (!add && !tc_skip_hw(ht->flags))
1367 u32_reoffload_hnode(tp, ht, add, cb, cb_priv, extack);
1368 }
1369
1370 return 0;
1371 }
1372
u32_bind_class(void * fh,u32 classid,unsigned long cl,void * q,unsigned long base)1373 static void u32_bind_class(void *fh, u32 classid, unsigned long cl, void *q,
1374 unsigned long base)
1375 {
1376 struct tc_u_knode *n = fh;
1377
1378 if (TC_U32_KEY(n->handle) == 0)
1379 return;
1380
1381 tc_cls_bind_class(classid, cl, q, &n->res, base);
1382 }
1383
u32_dump(struct net * net,struct tcf_proto * tp,void * fh,struct sk_buff * skb,struct tcmsg * t,bool rtnl_held)1384 static int u32_dump(struct net *net, struct tcf_proto *tp, void *fh,
1385 struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
1386 {
1387 struct tc_u_knode *n = fh;
1388 struct tc_u_hnode *ht_up, *ht_down;
1389 struct nlattr *nest;
1390
1391 if (n == NULL)
1392 return skb->len;
1393
1394 t->tcm_handle = n->handle;
1395
1396 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
1397 if (nest == NULL)
1398 goto nla_put_failure;
1399
1400 if (TC_U32_KEY(n->handle) == 0) {
1401 struct tc_u_hnode *ht = fh;
1402 u32 divisor = ht->divisor + 1;
1403
1404 if (nla_put_u32(skb, TCA_U32_DIVISOR, divisor))
1405 goto nla_put_failure;
1406 } else {
1407 #ifdef CONFIG_CLS_U32_PERF
1408 struct tc_u32_pcnt *gpf;
1409 int cpu;
1410 #endif
1411
1412 if (nla_put(skb, TCA_U32_SEL, struct_size(&n->sel, keys, n->sel.nkeys),
1413 &n->sel))
1414 goto nla_put_failure;
1415
1416 ht_up = rtnl_dereference(n->ht_up);
1417 if (ht_up) {
1418 u32 htid = n->handle & 0xFFFFF000;
1419 if (nla_put_u32(skb, TCA_U32_HASH, htid))
1420 goto nla_put_failure;
1421 }
1422 if (n->res.classid &&
1423 nla_put_u32(skb, TCA_U32_CLASSID, n->res.classid))
1424 goto nla_put_failure;
1425
1426 ht_down = rtnl_dereference(n->ht_down);
1427 if (ht_down &&
1428 nla_put_u32(skb, TCA_U32_LINK, ht_down->handle))
1429 goto nla_put_failure;
1430
1431 if (n->flags && nla_put_u32(skb, TCA_U32_FLAGS, n->flags))
1432 goto nla_put_failure;
1433
1434 #ifdef CONFIG_CLS_U32_MARK
1435 if ((n->val || n->mask)) {
1436 struct tc_u32_mark mark = {.val = n->val,
1437 .mask = n->mask,
1438 .success = 0};
1439 int cpum;
1440
1441 for_each_possible_cpu(cpum) {
1442 __u32 cnt = *per_cpu_ptr(n->pcpu_success, cpum);
1443
1444 mark.success += cnt;
1445 }
1446
1447 if (nla_put(skb, TCA_U32_MARK, sizeof(mark), &mark))
1448 goto nla_put_failure;
1449 }
1450 #endif
1451
1452 if (tcf_exts_dump(skb, &n->exts) < 0)
1453 goto nla_put_failure;
1454
1455 if (n->ifindex) {
1456 struct net_device *dev;
1457 dev = __dev_get_by_index(net, n->ifindex);
1458 if (dev && nla_put_string(skb, TCA_U32_INDEV, dev->name))
1459 goto nla_put_failure;
1460 }
1461 #ifdef CONFIG_CLS_U32_PERF
1462 gpf = kzalloc_flex(*gpf, kcnts, n->sel.nkeys);
1463 if (!gpf)
1464 goto nla_put_failure;
1465
1466 for_each_possible_cpu(cpu) {
1467 int i;
1468 struct tc_u32_pcnt *pf = per_cpu_ptr(n->pf, cpu);
1469
1470 gpf->rcnt += pf->rcnt;
1471 gpf->rhit += pf->rhit;
1472 for (i = 0; i < n->sel.nkeys; i++)
1473 gpf->kcnts[i] += pf->kcnts[i];
1474 }
1475
1476 if (nla_put_64bit(skb, TCA_U32_PCNT, struct_size(gpf, kcnts, n->sel.nkeys),
1477 gpf, TCA_U32_PAD)) {
1478 kfree(gpf);
1479 goto nla_put_failure;
1480 }
1481 kfree(gpf);
1482 #endif
1483 }
1484
1485 nla_nest_end(skb, nest);
1486
1487 if (TC_U32_KEY(n->handle))
1488 if (tcf_exts_dump_stats(skb, &n->exts) < 0)
1489 goto nla_put_failure;
1490 return skb->len;
1491
1492 nla_put_failure:
1493 nla_nest_cancel(skb, nest);
1494 return -1;
1495 }
1496
1497 static struct tcf_proto_ops cls_u32_ops __read_mostly = {
1498 .kind = "u32",
1499 .classify = u32_classify,
1500 .init = u32_init,
1501 .destroy = u32_destroy,
1502 .get = u32_get,
1503 .change = u32_change,
1504 .delete = u32_delete,
1505 .walk = u32_walk,
1506 .reoffload = u32_reoffload,
1507 .dump = u32_dump,
1508 .bind_class = u32_bind_class,
1509 .owner = THIS_MODULE,
1510 };
1511 MODULE_ALIAS_NET_CLS("u32");
1512
init_u32(void)1513 static int __init init_u32(void)
1514 {
1515 int i, ret;
1516
1517 pr_info("u32 classifier\n");
1518 #ifdef CONFIG_CLS_U32_PERF
1519 pr_info(" Performance counters on\n");
1520 #endif
1521 pr_info(" input device check on\n");
1522 #ifdef CONFIG_NET_CLS_ACT
1523 pr_info(" Actions configured\n");
1524 #endif
1525 tc_u_common_hash = kvmalloc_objs(struct hlist_head, U32_HASH_SIZE);
1526 if (!tc_u_common_hash)
1527 return -ENOMEM;
1528
1529 for (i = 0; i < U32_HASH_SIZE; i++)
1530 INIT_HLIST_HEAD(&tc_u_common_hash[i]);
1531
1532 ret = register_tcf_proto_ops(&cls_u32_ops);
1533 if (ret)
1534 kvfree(tc_u_common_hash);
1535 return ret;
1536 }
1537
exit_u32(void)1538 static void __exit exit_u32(void)
1539 {
1540 unregister_tcf_proto_ops(&cls_u32_ops);
1541 kvfree(tc_u_common_hash);
1542 }
1543
1544 module_init(init_u32)
1545 module_exit(exit_u32)
1546 MODULE_DESCRIPTION("Universal 32bit based TC Classifier");
1547 MODULE_LICENSE("GPL");
1548