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 95 static u32 handle2id(u32 h) 96 { 97 return ((h & 0x80000000) ? ((h >> 20) & 0x7FF) : h); 98 } 99 100 static u32 id2handle(u32 id) 101 { 102 return (id | 0x800U) << 20; 103 } 104 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 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 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 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 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 */ 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 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 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 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 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 root_ht->prio = tp->prio; 374 root_ht->is_root = true; 375 idr_init(&root_ht->handle_idr); 376 377 if (tp_c == NULL) { 378 tp_c = kzalloc_obj(*tp_c); 379 if (tp_c == NULL) { 380 kfree(root_ht); 381 return -ENOBUFS; 382 } 383 refcount_set(&tp_c->refcnt, 1); 384 tp_c->ptr = key; 385 INIT_HLIST_NODE(&tp_c->hnode); 386 idr_init(&tp_c->handle_idr); 387 388 hlist_add_head(&tp_c->hnode, tc_u_hash(key)); 389 } else { 390 refcount_inc(&tp_c->refcnt); 391 } 392 393 RCU_INIT_POINTER(root_ht->next, tp_c->hlist); 394 rcu_assign_pointer(tp_c->hlist, root_ht); 395 396 /* root_ht must be destroyed when tcf_proto is destroyed */ 397 rcu_assign_pointer(tp->root, root_ht); 398 tp->data = tp_c; 399 return 0; 400 } 401 402 static void __u32_destroy_key(struct tc_u_knode *n) 403 { 404 struct tc_u_hnode *ht = rtnl_dereference(n->ht_down); 405 406 tcf_exts_destroy(&n->exts); 407 if (ht && refcount_dec_and_test(&ht->refcnt)) 408 kfree(ht); 409 kfree(n); 410 } 411 412 static void u32_destroy_key(struct tc_u_knode *n, bool free_pf) 413 { 414 tcf_exts_put_net(&n->exts); 415 #ifdef CONFIG_CLS_U32_PERF 416 if (free_pf) 417 free_percpu(n->pf); 418 #endif 419 #ifdef CONFIG_CLS_U32_MARK 420 if (free_pf) 421 free_percpu(n->pcpu_success); 422 #endif 423 __u32_destroy_key(n); 424 } 425 426 /* u32_delete_key_rcu should be called when free'ing a copied 427 * version of a tc_u_knode obtained from u32_init_knode(). When 428 * copies are obtained from u32_init_knode() the statistics are 429 * shared between the old and new copies to allow readers to 430 * continue to update the statistics during the copy. To support 431 * this the u32_delete_key_rcu variant does not free the percpu 432 * statistics. 433 */ 434 static void u32_delete_key_work(struct work_struct *work) 435 { 436 struct tc_u_knode *key = container_of(to_rcu_work(work), 437 struct tc_u_knode, 438 rwork); 439 rtnl_lock(); 440 u32_destroy_key(key, false); 441 rtnl_unlock(); 442 } 443 444 /* u32_delete_key_freepf_rcu is the rcu callback variant 445 * that free's the entire structure including the statistics 446 * percpu variables. Only use this if the key is not a copy 447 * returned by u32_init_knode(). See u32_delete_key_rcu() 448 * for the variant that should be used with keys return from 449 * u32_init_knode() 450 */ 451 static void u32_delete_key_freepf_work(struct work_struct *work) 452 { 453 struct tc_u_knode *key = container_of(to_rcu_work(work), 454 struct tc_u_knode, 455 rwork); 456 rtnl_lock(); 457 u32_destroy_key(key, true); 458 rtnl_unlock(); 459 } 460 461 static int u32_delete_key(struct tcf_proto *tp, struct tc_u_knode *key) 462 { 463 struct tc_u_common *tp_c = tp->data; 464 struct tc_u_knode __rcu **kp; 465 struct tc_u_knode *pkp; 466 struct tc_u_hnode *ht = rtnl_dereference(key->ht_up); 467 468 if (ht) { 469 kp = &ht->ht[TC_U32_HASH(key->handle)]; 470 for (pkp = rtnl_dereference(*kp); pkp; 471 kp = &pkp->next, pkp = rtnl_dereference(*kp)) { 472 if (pkp == key) { 473 RCU_INIT_POINTER(*kp, key->next); 474 tp_c->knodes--; 475 476 tcf_unbind_filter(tp, &key->res); 477 idr_remove(&ht->handle_idr, key->handle); 478 tcf_exts_get_net(&key->exts); 479 tcf_queue_work(&key->rwork, u32_delete_key_freepf_work); 480 return 0; 481 } 482 } 483 } 484 WARN_ON(1); 485 return 0; 486 } 487 488 static void u32_clear_hw_hnode(struct tcf_proto *tp, struct tc_u_hnode *h, 489 struct netlink_ext_ack *extack) 490 { 491 struct tcf_block *block = tp->chain->block; 492 struct tc_cls_u32_offload cls_u32 = {}; 493 494 tc_cls_common_offload_init(&cls_u32.common, tp, h->flags, extack); 495 cls_u32.command = TC_CLSU32_DELETE_HNODE; 496 cls_u32.hnode.divisor = h->divisor; 497 cls_u32.hnode.handle = h->handle; 498 cls_u32.hnode.prio = h->prio; 499 500 tc_setup_cb_call(block, TC_SETUP_CLSU32, &cls_u32, false, true); 501 } 502 503 static int u32_replace_hw_hnode(struct tcf_proto *tp, struct tc_u_hnode *h, 504 u32 flags, struct netlink_ext_ack *extack) 505 { 506 struct tcf_block *block = tp->chain->block; 507 struct tc_cls_u32_offload cls_u32 = {}; 508 bool skip_sw = tc_skip_sw(flags); 509 bool offloaded = false; 510 int err; 511 512 tc_cls_common_offload_init(&cls_u32.common, tp, flags, extack); 513 cls_u32.command = TC_CLSU32_NEW_HNODE; 514 cls_u32.hnode.divisor = h->divisor; 515 cls_u32.hnode.handle = h->handle; 516 cls_u32.hnode.prio = h->prio; 517 518 err = tc_setup_cb_call(block, TC_SETUP_CLSU32, &cls_u32, skip_sw, true); 519 if (err < 0) { 520 u32_clear_hw_hnode(tp, h, NULL); 521 return err; 522 } else if (err > 0) { 523 offloaded = true; 524 } 525 526 if (skip_sw && !offloaded) 527 return -EINVAL; 528 529 return 0; 530 } 531 532 static void u32_remove_hw_knode(struct tcf_proto *tp, struct tc_u_knode *n, 533 struct netlink_ext_ack *extack) 534 { 535 struct tcf_block *block = tp->chain->block; 536 struct tc_cls_u32_offload cls_u32 = {}; 537 538 tc_cls_common_offload_init(&cls_u32.common, tp, n->flags, extack); 539 cls_u32.command = TC_CLSU32_DELETE_KNODE; 540 cls_u32.knode.handle = n->handle; 541 542 tc_setup_cb_destroy(block, tp, TC_SETUP_CLSU32, &cls_u32, false, 543 &n->flags, &n->in_hw_count, true); 544 } 545 546 static int u32_replace_hw_knode(struct tcf_proto *tp, struct tc_u_knode *n, 547 u32 flags, struct netlink_ext_ack *extack) 548 { 549 struct tc_u_hnode *ht = rtnl_dereference(n->ht_down); 550 struct tcf_block *block = tp->chain->block; 551 struct tc_cls_u32_offload cls_u32 = {}; 552 bool skip_sw = tc_skip_sw(flags); 553 int err; 554 555 tc_cls_common_offload_init(&cls_u32.common, tp, flags, extack); 556 cls_u32.command = TC_CLSU32_REPLACE_KNODE; 557 cls_u32.knode.handle = n->handle; 558 cls_u32.knode.fshift = n->fshift; 559 #ifdef CONFIG_CLS_U32_MARK 560 cls_u32.knode.val = n->val; 561 cls_u32.knode.mask = n->mask; 562 #else 563 cls_u32.knode.val = 0; 564 cls_u32.knode.mask = 0; 565 #endif 566 cls_u32.knode.sel = &n->sel; 567 cls_u32.knode.res = &n->res; 568 cls_u32.knode.exts = &n->exts; 569 if (n->ht_down) 570 cls_u32.knode.link_handle = ht->handle; 571 572 err = tc_setup_cb_add(block, tp, TC_SETUP_CLSU32, &cls_u32, skip_sw, 573 &n->flags, &n->in_hw_count, true); 574 if (err) { 575 u32_remove_hw_knode(tp, n, NULL); 576 return err; 577 } 578 579 if (skip_sw && !(n->flags & TCA_CLS_FLAGS_IN_HW)) 580 return -EINVAL; 581 582 return 0; 583 } 584 585 static void u32_clear_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht, 586 struct netlink_ext_ack *extack) 587 { 588 struct tc_u_common *tp_c = tp->data; 589 struct tc_u_knode *n; 590 unsigned int h; 591 592 for (h = 0; h <= ht->divisor; h++) { 593 while ((n = rtnl_dereference(ht->ht[h])) != NULL) { 594 RCU_INIT_POINTER(ht->ht[h], 595 rtnl_dereference(n->next)); 596 tp_c->knodes--; 597 tcf_unbind_filter(tp, &n->res); 598 u32_remove_hw_knode(tp, n, extack); 599 idr_remove(&ht->handle_idr, n->handle); 600 if (tcf_exts_get_net(&n->exts)) 601 tcf_queue_work(&n->rwork, u32_delete_key_freepf_work); 602 else 603 u32_destroy_key(n, true); 604 } 605 } 606 } 607 608 static int u32_destroy_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht, 609 struct netlink_ext_ack *extack) 610 { 611 struct tc_u_common *tp_c = tp->data; 612 struct tc_u_hnode __rcu **hn; 613 struct tc_u_hnode *phn; 614 615 u32_clear_hnode(tp, ht, extack); 616 617 hn = &tp_c->hlist; 618 for (phn = rtnl_dereference(*hn); 619 phn; 620 hn = &phn->next, phn = rtnl_dereference(*hn)) { 621 if (phn == ht) { 622 u32_clear_hw_hnode(tp, ht, extack); 623 idr_destroy(&ht->handle_idr); 624 idr_remove(&tp_c->handle_idr, handle2id(ht->handle)); 625 RCU_INIT_POINTER(*hn, ht->next); 626 kfree_rcu(ht, rcu); 627 return 0; 628 } 629 } 630 631 return -ENOENT; 632 } 633 634 static void u32_destroy(struct tcf_proto *tp, bool rtnl_held, 635 struct netlink_ext_ack *extack) 636 { 637 struct tc_u_common *tp_c = tp->data; 638 struct tc_u_hnode *root_ht = rtnl_dereference(tp->root); 639 640 WARN_ON(root_ht == NULL); 641 642 if (root_ht && refcount_dec_and_test(&root_ht->refcnt)) 643 u32_destroy_hnode(tp, root_ht, extack); 644 645 if (refcount_dec_and_test(&tp_c->refcnt)) { 646 struct tc_u_hnode *ht; 647 648 hlist_del(&tp_c->hnode); 649 650 while ((ht = rtnl_dereference(tp_c->hlist)) != NULL) { 651 u32_clear_hnode(tp, ht, extack); 652 RCU_INIT_POINTER(tp_c->hlist, ht->next); 653 654 /* u32_destroy_key() will later free ht for us, if it's 655 * still referenced by some knode 656 */ 657 if (refcount_dec_and_test(&ht->refcnt)) 658 kfree_rcu(ht, rcu); 659 } 660 661 idr_destroy(&tp_c->handle_idr); 662 kfree(tp_c); 663 } 664 665 tp->data = NULL; 666 } 667 668 static int u32_delete(struct tcf_proto *tp, void *arg, bool *last, 669 bool rtnl_held, struct netlink_ext_ack *extack) 670 { 671 struct tc_u_hnode *ht = arg; 672 struct tc_u_common *tp_c = tp->data; 673 int ret = 0; 674 675 if (TC_U32_KEY(ht->handle)) { 676 u32_remove_hw_knode(tp, (struct tc_u_knode *)ht, extack); 677 ret = u32_delete_key(tp, (struct tc_u_knode *)ht); 678 goto out; 679 } 680 681 if (ht->is_root) { 682 NL_SET_ERR_MSG_MOD(extack, "Not allowed to delete root node"); 683 return -EINVAL; 684 } 685 686 if (refcount_dec_if_one(&ht->refcnt)) { 687 u32_destroy_hnode(tp, ht, extack); 688 } else { 689 NL_SET_ERR_MSG_MOD(extack, "Can not delete in-use filter"); 690 return -EBUSY; 691 } 692 693 out: 694 *last = refcount_read(&tp_c->refcnt) == 1 && tp_c->knodes == 0; 695 return ret; 696 } 697 698 static u32 gen_new_kid(struct tc_u_hnode *ht, u32 htid) 699 { 700 u32 index = htid | 0x800; 701 u32 max = htid | 0xFFF; 702 703 if (idr_alloc_u32(&ht->handle_idr, NULL, &index, max, GFP_KERNEL)) { 704 index = htid + 1; 705 if (idr_alloc_u32(&ht->handle_idr, NULL, &index, max, 706 GFP_KERNEL)) 707 index = max; 708 } 709 710 return index; 711 } 712 713 static const struct nla_policy u32_policy[TCA_U32_MAX + 1] = { 714 [TCA_U32_CLASSID] = { .type = NLA_U32 }, 715 [TCA_U32_HASH] = { .type = NLA_U32 }, 716 [TCA_U32_LINK] = { .type = NLA_U32 }, 717 [TCA_U32_DIVISOR] = { .type = NLA_U32 }, 718 [TCA_U32_SEL] = { .len = sizeof(struct tc_u32_sel) }, 719 [TCA_U32_INDEV] = { .type = NLA_STRING, .len = IFNAMSIZ }, 720 [TCA_U32_MARK] = { .len = sizeof(struct tc_u32_mark) }, 721 [TCA_U32_FLAGS] = { .type = NLA_U32 }, 722 }; 723 724 static void u32_unbind_filter(struct tcf_proto *tp, struct tc_u_knode *n, 725 struct nlattr **tb) 726 { 727 if (tb[TCA_U32_CLASSID]) 728 tcf_unbind_filter(tp, &n->res); 729 } 730 731 static void u32_bind_filter(struct tcf_proto *tp, struct tc_u_knode *n, 732 unsigned long base, struct nlattr **tb) 733 { 734 if (tb[TCA_U32_CLASSID]) { 735 n->res.classid = nla_get_u32(tb[TCA_U32_CLASSID]); 736 tcf_bind_filter(tp, &n->res, base); 737 } 738 } 739 740 static int u32_set_parms(struct net *net, struct tcf_proto *tp, 741 struct tc_u_knode *n, struct nlattr **tb, 742 struct nlattr *est, u32 flags, u32 fl_flags, 743 struct netlink_ext_ack *extack) 744 { 745 int err, ifindex = -1; 746 747 err = tcf_exts_validate_ex(net, tp, tb, est, &n->exts, flags, 748 fl_flags, extack); 749 if (err < 0) 750 return err; 751 752 if (tb[TCA_U32_INDEV]) { 753 ifindex = tcf_change_indev(net, tb[TCA_U32_INDEV], extack); 754 if (ifindex < 0) 755 return -EINVAL; 756 } 757 758 if (tb[TCA_U32_LINK]) { 759 u32 handle = nla_get_u32(tb[TCA_U32_LINK]); 760 struct tc_u_hnode *ht_down = NULL, *ht_old; 761 762 if (TC_U32_KEY(handle)) { 763 NL_SET_ERR_MSG_MOD(extack, "u32 Link handle must be a hash table"); 764 return -EINVAL; 765 } 766 767 if (handle) { 768 ht_down = u32_lookup_ht(tp->data, handle); 769 770 if (!ht_down) { 771 NL_SET_ERR_MSG_MOD(extack, "Link hash table not found"); 772 return -EINVAL; 773 } 774 if (ht_down->is_root) { 775 NL_SET_ERR_MSG_MOD(extack, "Not linking to root node"); 776 return -EINVAL; 777 } 778 refcount_inc(&ht_down->refcnt); 779 } 780 781 ht_old = rtnl_dereference(n->ht_down); 782 rcu_assign_pointer(n->ht_down, ht_down); 783 784 if (ht_old) 785 refcount_dec(&ht_old->refcnt); 786 } 787 788 if (ifindex >= 0) 789 n->ifindex = ifindex; 790 791 return 0; 792 } 793 794 static void u32_replace_knode(struct tcf_proto *tp, struct tc_u_common *tp_c, 795 struct tc_u_knode *n) 796 { 797 struct tc_u_knode __rcu **ins; 798 struct tc_u_knode *pins; 799 struct tc_u_hnode *ht; 800 801 if (TC_U32_HTID(n->handle) == TC_U32_ROOT) 802 ht = rtnl_dereference(tp->root); 803 else 804 ht = u32_lookup_ht(tp_c, TC_U32_HTID(n->handle)); 805 806 ins = &ht->ht[TC_U32_HASH(n->handle)]; 807 808 /* The node must always exist for it to be replaced if this is not the 809 * case then something went very wrong elsewhere. 810 */ 811 for (pins = rtnl_dereference(*ins); ; 812 ins = &pins->next, pins = rtnl_dereference(*ins)) 813 if (pins->handle == n->handle) 814 break; 815 816 idr_replace(&ht->handle_idr, n, n->handle); 817 RCU_INIT_POINTER(n->next, pins->next); 818 rcu_assign_pointer(*ins, n); 819 } 820 821 static struct tc_u_knode *u32_init_knode(struct net *net, struct tcf_proto *tp, 822 struct tc_u_knode *n) 823 { 824 struct tc_u_hnode *ht = rtnl_dereference(n->ht_down); 825 struct tc_u32_sel *s = &n->sel; 826 struct tc_u_knode *new; 827 828 new = kzalloc_flex(*new, sel.keys, s->nkeys); 829 if (!new) 830 return NULL; 831 832 RCU_INIT_POINTER(new->next, n->next); 833 new->handle = n->handle; 834 RCU_INIT_POINTER(new->ht_up, n->ht_up); 835 836 new->ifindex = n->ifindex; 837 new->fshift = n->fshift; 838 new->flags = n->flags; 839 RCU_INIT_POINTER(new->ht_down, ht); 840 841 #ifdef CONFIG_CLS_U32_PERF 842 /* Statistics may be incremented by readers during update 843 * so we must keep them in tact. When the node is later destroyed 844 * a special destroy call must be made to not free the pf memory. 845 */ 846 new->pf = n->pf; 847 #endif 848 849 #ifdef CONFIG_CLS_U32_MARK 850 new->val = n->val; 851 new->mask = n->mask; 852 /* Similarly success statistics must be moved as pointers */ 853 new->pcpu_success = n->pcpu_success; 854 #endif 855 unsafe_memcpy(&new->sel, s, struct_size(s, keys, s->nkeys), 856 /* A composite flex-array structure destination, 857 * which was correctly sized with kzalloc_flex(), 858 * above. */); 859 860 if (tcf_exts_init(&new->exts, net, TCA_U32_ACT, TCA_U32_POLICE)) { 861 kfree(new); 862 return NULL; 863 } 864 865 /* bump reference count as long as we hold pointer to structure */ 866 if (ht) 867 refcount_inc(&ht->refcnt); 868 869 return new; 870 } 871 872 static int u32_change(struct net *net, struct sk_buff *in_skb, 873 struct tcf_proto *tp, unsigned long base, u32 handle, 874 struct nlattr **tca, void **arg, u32 flags, 875 struct netlink_ext_ack *extack) 876 { 877 struct tc_u_common *tp_c = tp->data; 878 struct tc_u_hnode *ht; 879 struct tc_u_knode *n; 880 struct tc_u32_sel *s; 881 struct nlattr *opt = tca[TCA_OPTIONS]; 882 struct nlattr *tb[TCA_U32_MAX + 1]; 883 u32 htid, userflags = 0; 884 size_t sel_size; 885 int err; 886 887 if (!opt) { 888 if (handle) { 889 NL_SET_ERR_MSG_MOD(extack, "Filter handle requires options"); 890 return -EINVAL; 891 } else { 892 return 0; 893 } 894 } 895 896 err = nla_parse_nested_deprecated(tb, TCA_U32_MAX, opt, u32_policy, 897 extack); 898 if (err < 0) 899 return err; 900 901 if (tb[TCA_U32_FLAGS]) { 902 userflags = nla_get_u32(tb[TCA_U32_FLAGS]); 903 if (!tc_flags_valid(userflags)) { 904 NL_SET_ERR_MSG_MOD(extack, "Invalid filter flags"); 905 return -EINVAL; 906 } 907 } 908 909 n = *arg; 910 if (n) { 911 struct tc_u_knode *new; 912 913 if (TC_U32_KEY(n->handle) == 0) { 914 NL_SET_ERR_MSG_MOD(extack, "Key node id cannot be zero"); 915 return -EINVAL; 916 } 917 918 if ((n->flags ^ userflags) & 919 ~(TCA_CLS_FLAGS_IN_HW | TCA_CLS_FLAGS_NOT_IN_HW)) { 920 NL_SET_ERR_MSG_MOD(extack, "Key node flags do not match passed flags"); 921 return -EINVAL; 922 } 923 924 new = u32_init_knode(net, tp, n); 925 if (!new) 926 return -ENOMEM; 927 928 err = u32_set_parms(net, tp, new, tb, tca[TCA_RATE], 929 flags, new->flags, extack); 930 931 if (err) { 932 __u32_destroy_key(new); 933 return err; 934 } 935 936 u32_bind_filter(tp, new, base, tb); 937 938 err = u32_replace_hw_knode(tp, new, flags, extack); 939 if (err) { 940 u32_unbind_filter(tp, new, tb); 941 942 if (tb[TCA_U32_LINK]) { 943 struct tc_u_hnode *ht_old; 944 945 ht_old = rtnl_dereference(n->ht_down); 946 if (ht_old) 947 refcount_inc(&ht_old->refcnt); 948 } 949 __u32_destroy_key(new); 950 return err; 951 } 952 953 if (!tc_in_hw(new->flags)) 954 new->flags |= TCA_CLS_FLAGS_NOT_IN_HW; 955 956 tcf_proto_update_usesw(tp, new->flags); 957 958 u32_replace_knode(tp, tp_c, new); 959 tcf_unbind_filter(tp, &n->res); 960 tcf_exts_get_net(&n->exts); 961 tcf_queue_work(&n->rwork, u32_delete_key_work); 962 return 0; 963 } 964 965 if (tb[TCA_U32_DIVISOR]) { 966 unsigned int divisor = nla_get_u32(tb[TCA_U32_DIVISOR]); 967 968 if (!is_power_of_2(divisor)) { 969 NL_SET_ERR_MSG_MOD(extack, "Divisor is not a power of 2"); 970 return -EINVAL; 971 } 972 if (divisor-- > 0x100) { 973 NL_SET_ERR_MSG_MOD(extack, "Exceeded maximum 256 hash buckets"); 974 return -EINVAL; 975 } 976 if (TC_U32_KEY(handle)) { 977 NL_SET_ERR_MSG_MOD(extack, "Divisor can only be used on a hash table"); 978 return -EINVAL; 979 } 980 ht = kzalloc_flex(*ht, ht, divisor + 1); 981 if (ht == NULL) 982 return -ENOBUFS; 983 if (handle == 0) { 984 handle = gen_new_htid(tp->data, ht); 985 if (handle == 0) { 986 kfree(ht); 987 return -ENOMEM; 988 } 989 } else { 990 err = idr_alloc_u32(&tp_c->handle_idr, ht, &handle, 991 handle, GFP_KERNEL); 992 if (err) { 993 kfree(ht); 994 return err; 995 } 996 } 997 refcount_set(&ht->refcnt, 1); 998 ht->divisor = divisor; 999 ht->handle = handle; 1000 ht->prio = tp->prio; 1001 idr_init(&ht->handle_idr); 1002 ht->flags = userflags; 1003 1004 err = u32_replace_hw_hnode(tp, ht, userflags, extack); 1005 if (err) { 1006 idr_remove(&tp_c->handle_idr, handle2id(handle)); 1007 kfree(ht); 1008 return err; 1009 } 1010 1011 RCU_INIT_POINTER(ht->next, tp_c->hlist); 1012 rcu_assign_pointer(tp_c->hlist, ht); 1013 *arg = ht; 1014 1015 return 0; 1016 } 1017 1018 if (tb[TCA_U32_HASH]) { 1019 htid = nla_get_u32(tb[TCA_U32_HASH]); 1020 if (TC_U32_HTID(htid) == TC_U32_ROOT) { 1021 ht = rtnl_dereference(tp->root); 1022 htid = ht->handle; 1023 } else { 1024 ht = u32_lookup_ht(tp->data, TC_U32_HTID(htid)); 1025 if (!ht) { 1026 NL_SET_ERR_MSG_MOD(extack, "Specified hash table not found"); 1027 return -EINVAL; 1028 } 1029 } 1030 } else { 1031 ht = rtnl_dereference(tp->root); 1032 htid = ht->handle; 1033 } 1034 1035 if (ht->divisor < TC_U32_HASH(htid)) { 1036 NL_SET_ERR_MSG_MOD(extack, "Specified hash table buckets exceed configured value"); 1037 return -EINVAL; 1038 } 1039 1040 /* At this point, we need to derive the new handle that will be used to 1041 * uniquely map the identity of this table match entry. The 1042 * identity of the entry that we need to construct is 32 bits made of: 1043 * htid(12b):bucketid(8b):node/entryid(12b) 1044 * 1045 * At this point _we have the table(ht)_ in which we will insert this 1046 * entry. We carry the table's id in variable "htid". 1047 * Note that earlier code picked the ht selection either by a) the user 1048 * providing the htid specified via TCA_U32_HASH attribute or b) when 1049 * no such attribute is passed then the root ht, is default to at ID 1050 * 0x[800][00][000]. Rule: the root table has a single bucket with ID 0. 1051 * If OTOH the user passed us the htid, they may also pass a bucketid of 1052 * choice. 0 is fine. For example a user htid is 0x[600][01][000] it is 1053 * indicating hash bucketid of 1. Rule: the entry/node ID _cannot_ be 1054 * passed via the htid, so even if it was non-zero it will be ignored. 1055 * 1056 * We may also have a handle, if the user passed one. The handle also 1057 * carries the same addressing of htid(12b):bucketid(8b):node/entryid(12b). 1058 * Rule: the bucketid on the handle is ignored even if one was passed; 1059 * rather the value on "htid" is always assumed to be the bucketid. 1060 */ 1061 if (handle) { 1062 /* Rule: The htid from handle and tableid from htid must match */ 1063 if (TC_U32_HTID(handle) && TC_U32_HTID(handle ^ htid)) { 1064 NL_SET_ERR_MSG_MOD(extack, "Handle specified hash table address mismatch"); 1065 return -EINVAL; 1066 } 1067 /* Ok, so far we have a valid htid(12b):bucketid(8b) but we 1068 * need to finalize the table entry identification with the last 1069 * part - the node/entryid(12b)). Rule: Nodeid _cannot be 0_ for 1070 * entries. Rule: nodeid of 0 is reserved only for tables(see 1071 * earlier code which processes TC_U32_DIVISOR attribute). 1072 * Rule: The nodeid can only be derived from the handle (and not 1073 * htid). 1074 * Rule: if the handle specified zero for the node id example 1075 * 0x60000000, then pick a new nodeid from the pool of IDs 1076 * this hash table has been allocating from. 1077 * If OTOH it is specified (i.e for example the user passed a 1078 * handle such as 0x60000123), then we use it generate our final 1079 * handle which is used to uniquely identify the match entry. 1080 */ 1081 if (!TC_U32_NODE(handle)) { 1082 handle = gen_new_kid(ht, htid); 1083 } else { 1084 handle = htid | TC_U32_NODE(handle); 1085 err = idr_alloc_u32(&ht->handle_idr, NULL, &handle, 1086 handle, GFP_KERNEL); 1087 if (err) 1088 return err; 1089 } 1090 } else { 1091 /* The user did not give us a handle; lets just generate one 1092 * from the table's pool of nodeids. 1093 */ 1094 handle = gen_new_kid(ht, htid); 1095 } 1096 1097 if (tb[TCA_U32_SEL] == NULL) { 1098 NL_SET_ERR_MSG_MOD(extack, "Selector not specified"); 1099 err = -EINVAL; 1100 goto erridr; 1101 } 1102 1103 s = nla_data(tb[TCA_U32_SEL]); 1104 sel_size = struct_size(s, keys, s->nkeys); 1105 if (nla_len(tb[TCA_U32_SEL]) < sel_size) { 1106 err = -EINVAL; 1107 goto erridr; 1108 } 1109 1110 n = kzalloc_flex(*n, sel.keys, s->nkeys); 1111 if (n == NULL) { 1112 err = -ENOBUFS; 1113 goto erridr; 1114 } 1115 1116 #ifdef CONFIG_CLS_U32_PERF 1117 n->pf = __alloc_percpu(struct_size(n->pf, kcnts, s->nkeys), 1118 __alignof__(struct tc_u32_pcnt)); 1119 if (!n->pf) { 1120 err = -ENOBUFS; 1121 goto errfree; 1122 } 1123 #endif 1124 1125 unsafe_memcpy(&n->sel, s, sel_size, 1126 /* A composite flex-array structure destination, 1127 * which was correctly sized with struct_size(), 1128 * bounds-checked against nla_len(), and allocated 1129 * above. */); 1130 RCU_INIT_POINTER(n->ht_up, ht); 1131 n->handle = handle; 1132 n->fshift = s->hmask ? ffs(ntohl(s->hmask)) - 1 : 0; 1133 n->flags = userflags; 1134 1135 err = tcf_exts_init(&n->exts, net, TCA_U32_ACT, TCA_U32_POLICE); 1136 if (err < 0) 1137 goto errout; 1138 1139 #ifdef CONFIG_CLS_U32_MARK 1140 n->pcpu_success = alloc_percpu(u32); 1141 if (!n->pcpu_success) { 1142 err = -ENOMEM; 1143 goto errout; 1144 } 1145 1146 if (tb[TCA_U32_MARK]) { 1147 struct tc_u32_mark *mark; 1148 1149 mark = nla_data(tb[TCA_U32_MARK]); 1150 n->val = mark->val; 1151 n->mask = mark->mask; 1152 } 1153 #endif 1154 1155 err = u32_set_parms(net, tp, n, tb, tca[TCA_RATE], 1156 flags, n->flags, extack); 1157 1158 u32_bind_filter(tp, n, base, tb); 1159 1160 if (err == 0) { 1161 struct tc_u_knode __rcu **ins; 1162 struct tc_u_knode *pins; 1163 1164 err = u32_replace_hw_knode(tp, n, flags, extack); 1165 if (err) 1166 goto errunbind; 1167 1168 if (!tc_in_hw(n->flags)) 1169 n->flags |= TCA_CLS_FLAGS_NOT_IN_HW; 1170 1171 tcf_proto_update_usesw(tp, n->flags); 1172 1173 ins = &ht->ht[TC_U32_HASH(handle)]; 1174 for (pins = rtnl_dereference(*ins); pins; 1175 ins = &pins->next, pins = rtnl_dereference(*ins)) 1176 if (TC_U32_NODE(handle) < TC_U32_NODE(pins->handle)) 1177 break; 1178 1179 RCU_INIT_POINTER(n->next, pins); 1180 rcu_assign_pointer(*ins, n); 1181 tp_c->knodes++; 1182 *arg = n; 1183 return 0; 1184 } 1185 1186 errunbind: 1187 u32_unbind_filter(tp, n, tb); 1188 1189 #ifdef CONFIG_CLS_U32_MARK 1190 free_percpu(n->pcpu_success); 1191 #endif 1192 1193 errout: 1194 tcf_exts_destroy(&n->exts); 1195 #ifdef CONFIG_CLS_U32_PERF 1196 errfree: 1197 free_percpu(n->pf); 1198 #endif 1199 kfree(n); 1200 erridr: 1201 idr_remove(&ht->handle_idr, handle); 1202 return err; 1203 } 1204 1205 static void u32_walk(struct tcf_proto *tp, struct tcf_walker *arg, 1206 bool rtnl_held) 1207 { 1208 struct tc_u_common *tp_c = tp->data; 1209 struct tc_u_hnode *ht; 1210 struct tc_u_knode *n; 1211 unsigned int h; 1212 1213 if (arg->stop) 1214 return; 1215 1216 for (ht = rtnl_dereference(tp_c->hlist); 1217 ht; 1218 ht = rtnl_dereference(ht->next)) { 1219 if (ht->prio != tp->prio) 1220 continue; 1221 1222 if (!tc_cls_stats_dump(tp, arg, ht)) 1223 return; 1224 1225 for (h = 0; h <= ht->divisor; h++) { 1226 for (n = rtnl_dereference(ht->ht[h]); 1227 n; 1228 n = rtnl_dereference(n->next)) { 1229 if (!tc_cls_stats_dump(tp, arg, n)) 1230 return; 1231 } 1232 } 1233 } 1234 } 1235 1236 static int u32_reoffload_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht, 1237 bool add, flow_setup_cb_t *cb, void *cb_priv, 1238 struct netlink_ext_ack *extack) 1239 { 1240 struct tc_cls_u32_offload cls_u32 = {}; 1241 int err; 1242 1243 tc_cls_common_offload_init(&cls_u32.common, tp, ht->flags, extack); 1244 cls_u32.command = add ? TC_CLSU32_NEW_HNODE : TC_CLSU32_DELETE_HNODE; 1245 cls_u32.hnode.divisor = ht->divisor; 1246 cls_u32.hnode.handle = ht->handle; 1247 cls_u32.hnode.prio = ht->prio; 1248 1249 err = cb(TC_SETUP_CLSU32, &cls_u32, cb_priv); 1250 if (err && add && tc_skip_sw(ht->flags)) 1251 return err; 1252 1253 return 0; 1254 } 1255 1256 static int u32_reoffload_knode(struct tcf_proto *tp, struct tc_u_knode *n, 1257 bool add, flow_setup_cb_t *cb, void *cb_priv, 1258 struct netlink_ext_ack *extack) 1259 { 1260 struct tc_u_hnode *ht = rtnl_dereference(n->ht_down); 1261 struct tcf_block *block = tp->chain->block; 1262 struct tc_cls_u32_offload cls_u32 = {}; 1263 1264 tc_cls_common_offload_init(&cls_u32.common, tp, n->flags, extack); 1265 cls_u32.command = add ? 1266 TC_CLSU32_REPLACE_KNODE : TC_CLSU32_DELETE_KNODE; 1267 cls_u32.knode.handle = n->handle; 1268 1269 if (add) { 1270 cls_u32.knode.fshift = n->fshift; 1271 #ifdef CONFIG_CLS_U32_MARK 1272 cls_u32.knode.val = n->val; 1273 cls_u32.knode.mask = n->mask; 1274 #else 1275 cls_u32.knode.val = 0; 1276 cls_u32.knode.mask = 0; 1277 #endif 1278 cls_u32.knode.sel = &n->sel; 1279 cls_u32.knode.res = &n->res; 1280 cls_u32.knode.exts = &n->exts; 1281 if (n->ht_down) 1282 cls_u32.knode.link_handle = ht->handle; 1283 } 1284 1285 return tc_setup_cb_reoffload(block, tp, add, cb, TC_SETUP_CLSU32, 1286 &cls_u32, cb_priv, &n->flags, 1287 &n->in_hw_count); 1288 } 1289 1290 static int u32_reoffload(struct tcf_proto *tp, bool add, flow_setup_cb_t *cb, 1291 void *cb_priv, struct netlink_ext_ack *extack) 1292 { 1293 struct tc_u_common *tp_c = tp->data; 1294 struct tc_u_hnode *ht; 1295 struct tc_u_knode *n; 1296 unsigned int h; 1297 int err; 1298 1299 for (ht = rtnl_dereference(tp_c->hlist); 1300 ht; 1301 ht = rtnl_dereference(ht->next)) { 1302 if (ht->prio != tp->prio) 1303 continue; 1304 1305 /* When adding filters to a new dev, try to offload the 1306 * hashtable first. When removing, do the filters before the 1307 * hashtable. 1308 */ 1309 if (add && !tc_skip_hw(ht->flags)) { 1310 err = u32_reoffload_hnode(tp, ht, add, cb, cb_priv, 1311 extack); 1312 if (err) 1313 return err; 1314 } 1315 1316 for (h = 0; h <= ht->divisor; h++) { 1317 for (n = rtnl_dereference(ht->ht[h]); 1318 n; 1319 n = rtnl_dereference(n->next)) { 1320 if (tc_skip_hw(n->flags)) 1321 continue; 1322 1323 err = u32_reoffload_knode(tp, n, add, cb, 1324 cb_priv, extack); 1325 if (err) 1326 return err; 1327 } 1328 } 1329 1330 if (!add && !tc_skip_hw(ht->flags)) 1331 u32_reoffload_hnode(tp, ht, add, cb, cb_priv, extack); 1332 } 1333 1334 return 0; 1335 } 1336 1337 static void u32_bind_class(void *fh, u32 classid, unsigned long cl, void *q, 1338 unsigned long base) 1339 { 1340 struct tc_u_knode *n = fh; 1341 1342 tc_cls_bind_class(classid, cl, q, &n->res, base); 1343 } 1344 1345 static int u32_dump(struct net *net, struct tcf_proto *tp, void *fh, 1346 struct sk_buff *skb, struct tcmsg *t, bool rtnl_held) 1347 { 1348 struct tc_u_knode *n = fh; 1349 struct tc_u_hnode *ht_up, *ht_down; 1350 struct nlattr *nest; 1351 1352 if (n == NULL) 1353 return skb->len; 1354 1355 t->tcm_handle = n->handle; 1356 1357 nest = nla_nest_start_noflag(skb, TCA_OPTIONS); 1358 if (nest == NULL) 1359 goto nla_put_failure; 1360 1361 if (TC_U32_KEY(n->handle) == 0) { 1362 struct tc_u_hnode *ht = fh; 1363 u32 divisor = ht->divisor + 1; 1364 1365 if (nla_put_u32(skb, TCA_U32_DIVISOR, divisor)) 1366 goto nla_put_failure; 1367 } else { 1368 #ifdef CONFIG_CLS_U32_PERF 1369 struct tc_u32_pcnt *gpf; 1370 int cpu; 1371 #endif 1372 1373 if (nla_put(skb, TCA_U32_SEL, struct_size(&n->sel, keys, n->sel.nkeys), 1374 &n->sel)) 1375 goto nla_put_failure; 1376 1377 ht_up = rtnl_dereference(n->ht_up); 1378 if (ht_up) { 1379 u32 htid = n->handle & 0xFFFFF000; 1380 if (nla_put_u32(skb, TCA_U32_HASH, htid)) 1381 goto nla_put_failure; 1382 } 1383 if (n->res.classid && 1384 nla_put_u32(skb, TCA_U32_CLASSID, n->res.classid)) 1385 goto nla_put_failure; 1386 1387 ht_down = rtnl_dereference(n->ht_down); 1388 if (ht_down && 1389 nla_put_u32(skb, TCA_U32_LINK, ht_down->handle)) 1390 goto nla_put_failure; 1391 1392 if (n->flags && nla_put_u32(skb, TCA_U32_FLAGS, n->flags)) 1393 goto nla_put_failure; 1394 1395 #ifdef CONFIG_CLS_U32_MARK 1396 if ((n->val || n->mask)) { 1397 struct tc_u32_mark mark = {.val = n->val, 1398 .mask = n->mask, 1399 .success = 0}; 1400 int cpum; 1401 1402 for_each_possible_cpu(cpum) { 1403 __u32 cnt = *per_cpu_ptr(n->pcpu_success, cpum); 1404 1405 mark.success += cnt; 1406 } 1407 1408 if (nla_put(skb, TCA_U32_MARK, sizeof(mark), &mark)) 1409 goto nla_put_failure; 1410 } 1411 #endif 1412 1413 if (tcf_exts_dump(skb, &n->exts) < 0) 1414 goto nla_put_failure; 1415 1416 if (n->ifindex) { 1417 struct net_device *dev; 1418 dev = __dev_get_by_index(net, n->ifindex); 1419 if (dev && nla_put_string(skb, TCA_U32_INDEV, dev->name)) 1420 goto nla_put_failure; 1421 } 1422 #ifdef CONFIG_CLS_U32_PERF 1423 gpf = kzalloc_flex(*gpf, kcnts, n->sel.nkeys); 1424 if (!gpf) 1425 goto nla_put_failure; 1426 1427 for_each_possible_cpu(cpu) { 1428 int i; 1429 struct tc_u32_pcnt *pf = per_cpu_ptr(n->pf, cpu); 1430 1431 gpf->rcnt += pf->rcnt; 1432 gpf->rhit += pf->rhit; 1433 for (i = 0; i < n->sel.nkeys; i++) 1434 gpf->kcnts[i] += pf->kcnts[i]; 1435 } 1436 1437 if (nla_put_64bit(skb, TCA_U32_PCNT, struct_size(gpf, kcnts, n->sel.nkeys), 1438 gpf, TCA_U32_PAD)) { 1439 kfree(gpf); 1440 goto nla_put_failure; 1441 } 1442 kfree(gpf); 1443 #endif 1444 } 1445 1446 nla_nest_end(skb, nest); 1447 1448 if (TC_U32_KEY(n->handle)) 1449 if (tcf_exts_dump_stats(skb, &n->exts) < 0) 1450 goto nla_put_failure; 1451 return skb->len; 1452 1453 nla_put_failure: 1454 nla_nest_cancel(skb, nest); 1455 return -1; 1456 } 1457 1458 static struct tcf_proto_ops cls_u32_ops __read_mostly = { 1459 .kind = "u32", 1460 .classify = u32_classify, 1461 .init = u32_init, 1462 .destroy = u32_destroy, 1463 .get = u32_get, 1464 .change = u32_change, 1465 .delete = u32_delete, 1466 .walk = u32_walk, 1467 .reoffload = u32_reoffload, 1468 .dump = u32_dump, 1469 .bind_class = u32_bind_class, 1470 .owner = THIS_MODULE, 1471 }; 1472 MODULE_ALIAS_NET_CLS("u32"); 1473 1474 static int __init init_u32(void) 1475 { 1476 int i, ret; 1477 1478 pr_info("u32 classifier\n"); 1479 #ifdef CONFIG_CLS_U32_PERF 1480 pr_info(" Performance counters on\n"); 1481 #endif 1482 pr_info(" input device check on\n"); 1483 #ifdef CONFIG_NET_CLS_ACT 1484 pr_info(" Actions configured\n"); 1485 #endif 1486 tc_u_common_hash = kvmalloc_objs(struct hlist_head, U32_HASH_SIZE); 1487 if (!tc_u_common_hash) 1488 return -ENOMEM; 1489 1490 for (i = 0; i < U32_HASH_SIZE; i++) 1491 INIT_HLIST_HEAD(&tc_u_common_hash[i]); 1492 1493 ret = register_tcf_proto_ops(&cls_u32_ops); 1494 if (ret) 1495 kvfree(tc_u_common_hash); 1496 return ret; 1497 } 1498 1499 static void __exit exit_u32(void) 1500 { 1501 unregister_tcf_proto_ops(&cls_u32_ops); 1502 kvfree(tc_u_common_hash); 1503 } 1504 1505 module_init(init_u32) 1506 module_exit(exit_u32) 1507 MODULE_DESCRIPTION("Universal 32bit based TC Classifier"); 1508 MODULE_LICENSE("GPL"); 1509