1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * net/sched/cls_flower.c Flower classifier 4 * 5 * Copyright (c) 2015 Jiri Pirko <jiri@resnulli.us> 6 */ 7 8 #include <linux/kernel.h> 9 #include <linux/init.h> 10 #include <linux/module.h> 11 #include <linux/rhashtable.h> 12 #include <linux/workqueue.h> 13 #include <linux/refcount.h> 14 15 #include <linux/if_ether.h> 16 #include <linux/in6.h> 17 #include <linux/ip.h> 18 #include <linux/mpls.h> 19 20 #include <net/sch_generic.h> 21 #include <net/pkt_cls.h> 22 #include <net/ip.h> 23 #include <net/flow_dissector.h> 24 #include <net/geneve.h> 25 26 #include <net/dst.h> 27 #include <net/dst_metadata.h> 28 29 struct fl_flow_key { 30 struct flow_dissector_key_meta meta; 31 struct flow_dissector_key_control control; 32 struct flow_dissector_key_control enc_control; 33 struct flow_dissector_key_basic basic; 34 struct flow_dissector_key_eth_addrs eth; 35 struct flow_dissector_key_vlan vlan; 36 struct flow_dissector_key_vlan cvlan; 37 union { 38 struct flow_dissector_key_ipv4_addrs ipv4; 39 struct flow_dissector_key_ipv6_addrs ipv6; 40 }; 41 struct flow_dissector_key_ports tp; 42 struct flow_dissector_key_icmp icmp; 43 struct flow_dissector_key_arp arp; 44 struct flow_dissector_key_keyid enc_key_id; 45 union { 46 struct flow_dissector_key_ipv4_addrs enc_ipv4; 47 struct flow_dissector_key_ipv6_addrs enc_ipv6; 48 }; 49 struct flow_dissector_key_ports enc_tp; 50 struct flow_dissector_key_mpls mpls; 51 struct flow_dissector_key_tcp tcp; 52 struct flow_dissector_key_ip ip; 53 struct flow_dissector_key_ip enc_ip; 54 struct flow_dissector_key_enc_opts enc_opts; 55 struct flow_dissector_key_ports tp_min; 56 struct flow_dissector_key_ports tp_max; 57 } __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */ 58 59 struct fl_flow_mask_range { 60 unsigned short int start; 61 unsigned short int end; 62 }; 63 64 struct fl_flow_mask { 65 struct fl_flow_key key; 66 struct fl_flow_mask_range range; 67 u32 flags; 68 struct rhash_head ht_node; 69 struct rhashtable ht; 70 struct rhashtable_params filter_ht_params; 71 struct flow_dissector dissector; 72 struct list_head filters; 73 struct rcu_work rwork; 74 struct list_head list; 75 refcount_t refcnt; 76 }; 77 78 struct fl_flow_tmplt { 79 struct fl_flow_key dummy_key; 80 struct fl_flow_key mask; 81 struct flow_dissector dissector; 82 struct tcf_chain *chain; 83 }; 84 85 struct cls_fl_head { 86 struct rhashtable ht; 87 spinlock_t masks_lock; /* Protect masks list */ 88 struct list_head masks; 89 struct list_head hw_filters; 90 struct rcu_work rwork; 91 struct idr handle_idr; 92 }; 93 94 struct cls_fl_filter { 95 struct fl_flow_mask *mask; 96 struct rhash_head ht_node; 97 struct fl_flow_key mkey; 98 struct tcf_exts exts; 99 struct tcf_result res; 100 struct fl_flow_key key; 101 struct list_head list; 102 struct list_head hw_list; 103 u32 handle; 104 u32 flags; 105 u32 in_hw_count; 106 struct rcu_work rwork; 107 struct net_device *hw_dev; 108 /* Flower classifier is unlocked, which means that its reference counter 109 * can be changed concurrently without any kind of external 110 * synchronization. Use atomic reference counter to be concurrency-safe. 111 */ 112 refcount_t refcnt; 113 bool deleted; 114 }; 115 116 static const struct rhashtable_params mask_ht_params = { 117 .key_offset = offsetof(struct fl_flow_mask, key), 118 .key_len = sizeof(struct fl_flow_key), 119 .head_offset = offsetof(struct fl_flow_mask, ht_node), 120 .automatic_shrinking = true, 121 }; 122 123 static unsigned short int fl_mask_range(const struct fl_flow_mask *mask) 124 { 125 return mask->range.end - mask->range.start; 126 } 127 128 static void fl_mask_update_range(struct fl_flow_mask *mask) 129 { 130 const u8 *bytes = (const u8 *) &mask->key; 131 size_t size = sizeof(mask->key); 132 size_t i, first = 0, last; 133 134 for (i = 0; i < size; i++) { 135 if (bytes[i]) { 136 first = i; 137 break; 138 } 139 } 140 last = first; 141 for (i = size - 1; i != first; i--) { 142 if (bytes[i]) { 143 last = i; 144 break; 145 } 146 } 147 mask->range.start = rounddown(first, sizeof(long)); 148 mask->range.end = roundup(last + 1, sizeof(long)); 149 } 150 151 static void *fl_key_get_start(struct fl_flow_key *key, 152 const struct fl_flow_mask *mask) 153 { 154 return (u8 *) key + mask->range.start; 155 } 156 157 static void fl_set_masked_key(struct fl_flow_key *mkey, struct fl_flow_key *key, 158 struct fl_flow_mask *mask) 159 { 160 const long *lkey = fl_key_get_start(key, mask); 161 const long *lmask = fl_key_get_start(&mask->key, mask); 162 long *lmkey = fl_key_get_start(mkey, mask); 163 int i; 164 165 for (i = 0; i < fl_mask_range(mask); i += sizeof(long)) 166 *lmkey++ = *lkey++ & *lmask++; 167 } 168 169 static bool fl_mask_fits_tmplt(struct fl_flow_tmplt *tmplt, 170 struct fl_flow_mask *mask) 171 { 172 const long *lmask = fl_key_get_start(&mask->key, mask); 173 const long *ltmplt; 174 int i; 175 176 if (!tmplt) 177 return true; 178 ltmplt = fl_key_get_start(&tmplt->mask, mask); 179 for (i = 0; i < fl_mask_range(mask); i += sizeof(long)) { 180 if (~*ltmplt++ & *lmask++) 181 return false; 182 } 183 return true; 184 } 185 186 static void fl_clear_masked_range(struct fl_flow_key *key, 187 struct fl_flow_mask *mask) 188 { 189 memset(fl_key_get_start(key, mask), 0, fl_mask_range(mask)); 190 } 191 192 static bool fl_range_port_dst_cmp(struct cls_fl_filter *filter, 193 struct fl_flow_key *key, 194 struct fl_flow_key *mkey) 195 { 196 __be16 min_mask, max_mask, min_val, max_val; 197 198 min_mask = htons(filter->mask->key.tp_min.dst); 199 max_mask = htons(filter->mask->key.tp_max.dst); 200 min_val = htons(filter->key.tp_min.dst); 201 max_val = htons(filter->key.tp_max.dst); 202 203 if (min_mask && max_mask) { 204 if (htons(key->tp.dst) < min_val || 205 htons(key->tp.dst) > max_val) 206 return false; 207 208 /* skb does not have min and max values */ 209 mkey->tp_min.dst = filter->mkey.tp_min.dst; 210 mkey->tp_max.dst = filter->mkey.tp_max.dst; 211 } 212 return true; 213 } 214 215 static bool fl_range_port_src_cmp(struct cls_fl_filter *filter, 216 struct fl_flow_key *key, 217 struct fl_flow_key *mkey) 218 { 219 __be16 min_mask, max_mask, min_val, max_val; 220 221 min_mask = htons(filter->mask->key.tp_min.src); 222 max_mask = htons(filter->mask->key.tp_max.src); 223 min_val = htons(filter->key.tp_min.src); 224 max_val = htons(filter->key.tp_max.src); 225 226 if (min_mask && max_mask) { 227 if (htons(key->tp.src) < min_val || 228 htons(key->tp.src) > max_val) 229 return false; 230 231 /* skb does not have min and max values */ 232 mkey->tp_min.src = filter->mkey.tp_min.src; 233 mkey->tp_max.src = filter->mkey.tp_max.src; 234 } 235 return true; 236 } 237 238 static struct cls_fl_filter *__fl_lookup(struct fl_flow_mask *mask, 239 struct fl_flow_key *mkey) 240 { 241 return rhashtable_lookup_fast(&mask->ht, fl_key_get_start(mkey, mask), 242 mask->filter_ht_params); 243 } 244 245 static struct cls_fl_filter *fl_lookup_range(struct fl_flow_mask *mask, 246 struct fl_flow_key *mkey, 247 struct fl_flow_key *key) 248 { 249 struct cls_fl_filter *filter, *f; 250 251 list_for_each_entry_rcu(filter, &mask->filters, list) { 252 if (!fl_range_port_dst_cmp(filter, key, mkey)) 253 continue; 254 255 if (!fl_range_port_src_cmp(filter, key, mkey)) 256 continue; 257 258 f = __fl_lookup(mask, mkey); 259 if (f) 260 return f; 261 } 262 return NULL; 263 } 264 265 static struct cls_fl_filter *fl_lookup(struct fl_flow_mask *mask, 266 struct fl_flow_key *mkey, 267 struct fl_flow_key *key) 268 { 269 if ((mask->flags & TCA_FLOWER_MASK_FLAGS_RANGE)) 270 return fl_lookup_range(mask, mkey, key); 271 272 return __fl_lookup(mask, mkey); 273 } 274 275 static int fl_classify(struct sk_buff *skb, const struct tcf_proto *tp, 276 struct tcf_result *res) 277 { 278 struct cls_fl_head *head = rcu_dereference_bh(tp->root); 279 struct cls_fl_filter *f; 280 struct fl_flow_mask *mask; 281 struct fl_flow_key skb_key; 282 struct fl_flow_key skb_mkey; 283 284 list_for_each_entry_rcu(mask, &head->masks, list) { 285 fl_clear_masked_range(&skb_key, mask); 286 287 skb_flow_dissect_meta(skb, &mask->dissector, &skb_key); 288 /* skb_flow_dissect() does not set n_proto in case an unknown 289 * protocol, so do it rather here. 290 */ 291 skb_key.basic.n_proto = skb->protocol; 292 skb_flow_dissect_tunnel_info(skb, &mask->dissector, &skb_key); 293 skb_flow_dissect(skb, &mask->dissector, &skb_key, 0); 294 295 fl_set_masked_key(&skb_mkey, &skb_key, mask); 296 297 f = fl_lookup(mask, &skb_mkey, &skb_key); 298 if (f && !tc_skip_sw(f->flags)) { 299 *res = f->res; 300 return tcf_exts_exec(skb, &f->exts, res); 301 } 302 } 303 return -1; 304 } 305 306 static int fl_init(struct tcf_proto *tp) 307 { 308 struct cls_fl_head *head; 309 310 head = kzalloc(sizeof(*head), GFP_KERNEL); 311 if (!head) 312 return -ENOBUFS; 313 314 spin_lock_init(&head->masks_lock); 315 INIT_LIST_HEAD_RCU(&head->masks); 316 INIT_LIST_HEAD(&head->hw_filters); 317 rcu_assign_pointer(tp->root, head); 318 idr_init(&head->handle_idr); 319 320 return rhashtable_init(&head->ht, &mask_ht_params); 321 } 322 323 static void fl_mask_free(struct fl_flow_mask *mask, bool mask_init_done) 324 { 325 /* temporary masks don't have their filters list and ht initialized */ 326 if (mask_init_done) { 327 WARN_ON(!list_empty(&mask->filters)); 328 rhashtable_destroy(&mask->ht); 329 } 330 kfree(mask); 331 } 332 333 static void fl_mask_free_work(struct work_struct *work) 334 { 335 struct fl_flow_mask *mask = container_of(to_rcu_work(work), 336 struct fl_flow_mask, rwork); 337 338 fl_mask_free(mask, true); 339 } 340 341 static void fl_uninit_mask_free_work(struct work_struct *work) 342 { 343 struct fl_flow_mask *mask = container_of(to_rcu_work(work), 344 struct fl_flow_mask, rwork); 345 346 fl_mask_free(mask, false); 347 } 348 349 static bool fl_mask_put(struct cls_fl_head *head, struct fl_flow_mask *mask) 350 { 351 if (!refcount_dec_and_test(&mask->refcnt)) 352 return false; 353 354 rhashtable_remove_fast(&head->ht, &mask->ht_node, mask_ht_params); 355 356 spin_lock(&head->masks_lock); 357 list_del_rcu(&mask->list); 358 spin_unlock(&head->masks_lock); 359 360 tcf_queue_work(&mask->rwork, fl_mask_free_work); 361 362 return true; 363 } 364 365 static struct cls_fl_head *fl_head_dereference(struct tcf_proto *tp) 366 { 367 /* Flower classifier only changes root pointer during init and destroy. 368 * Users must obtain reference to tcf_proto instance before calling its 369 * API, so tp->root pointer is protected from concurrent call to 370 * fl_destroy() by reference counting. 371 */ 372 return rcu_dereference_raw(tp->root); 373 } 374 375 static void __fl_destroy_filter(struct cls_fl_filter *f) 376 { 377 tcf_exts_destroy(&f->exts); 378 tcf_exts_put_net(&f->exts); 379 kfree(f); 380 } 381 382 static void fl_destroy_filter_work(struct work_struct *work) 383 { 384 struct cls_fl_filter *f = container_of(to_rcu_work(work), 385 struct cls_fl_filter, rwork); 386 387 __fl_destroy_filter(f); 388 } 389 390 static void fl_hw_destroy_filter(struct tcf_proto *tp, struct cls_fl_filter *f, 391 bool rtnl_held, struct netlink_ext_ack *extack) 392 { 393 struct tc_cls_flower_offload cls_flower = {}; 394 struct tcf_block *block = tp->chain->block; 395 396 if (!rtnl_held) 397 rtnl_lock(); 398 399 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, extack); 400 cls_flower.command = TC_CLSFLOWER_DESTROY; 401 cls_flower.cookie = (unsigned long) f; 402 403 tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, false); 404 spin_lock(&tp->lock); 405 list_del_init(&f->hw_list); 406 tcf_block_offload_dec(block, &f->flags); 407 spin_unlock(&tp->lock); 408 409 if (!rtnl_held) 410 rtnl_unlock(); 411 } 412 413 static int fl_hw_replace_filter(struct tcf_proto *tp, 414 struct cls_fl_filter *f, bool rtnl_held, 415 struct netlink_ext_ack *extack) 416 { 417 struct cls_fl_head *head = fl_head_dereference(tp); 418 struct tc_cls_flower_offload cls_flower = {}; 419 struct tcf_block *block = tp->chain->block; 420 bool skip_sw = tc_skip_sw(f->flags); 421 int err = 0; 422 423 if (!rtnl_held) 424 rtnl_lock(); 425 426 cls_flower.rule = flow_rule_alloc(tcf_exts_num_actions(&f->exts)); 427 if (!cls_flower.rule) { 428 err = -ENOMEM; 429 goto errout; 430 } 431 432 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, extack); 433 cls_flower.command = TC_CLSFLOWER_REPLACE; 434 cls_flower.cookie = (unsigned long) f; 435 cls_flower.rule->match.dissector = &f->mask->dissector; 436 cls_flower.rule->match.mask = &f->mask->key; 437 cls_flower.rule->match.key = &f->mkey; 438 cls_flower.classid = f->res.classid; 439 440 err = tc_setup_flow_action(&cls_flower.rule->action, &f->exts); 441 if (err) { 442 kfree(cls_flower.rule); 443 if (skip_sw) 444 NL_SET_ERR_MSG_MOD(extack, "Failed to setup flow action"); 445 else 446 err = 0; 447 goto errout; 448 } 449 450 err = tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, skip_sw); 451 kfree(cls_flower.rule); 452 453 if (err < 0) { 454 fl_hw_destroy_filter(tp, f, true, NULL); 455 goto errout; 456 } else if (err > 0) { 457 f->in_hw_count = err; 458 err = 0; 459 spin_lock(&tp->lock); 460 tcf_block_offload_inc(block, &f->flags); 461 spin_unlock(&tp->lock); 462 } 463 464 if (skip_sw && !(f->flags & TCA_CLS_FLAGS_IN_HW)) { 465 err = -EINVAL; 466 goto errout; 467 } 468 469 spin_lock(&tp->lock); 470 list_add(&f->hw_list, &head->hw_filters); 471 spin_unlock(&tp->lock); 472 errout: 473 if (!rtnl_held) 474 rtnl_unlock(); 475 476 return err; 477 } 478 479 static void fl_hw_update_stats(struct tcf_proto *tp, struct cls_fl_filter *f, 480 bool rtnl_held) 481 { 482 struct tc_cls_flower_offload cls_flower = {}; 483 struct tcf_block *block = tp->chain->block; 484 485 if (!rtnl_held) 486 rtnl_lock(); 487 488 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, NULL); 489 cls_flower.command = TC_CLSFLOWER_STATS; 490 cls_flower.cookie = (unsigned long) f; 491 cls_flower.classid = f->res.classid; 492 493 tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, false); 494 495 tcf_exts_stats_update(&f->exts, cls_flower.stats.bytes, 496 cls_flower.stats.pkts, 497 cls_flower.stats.lastused); 498 499 if (!rtnl_held) 500 rtnl_unlock(); 501 } 502 503 static void __fl_put(struct cls_fl_filter *f) 504 { 505 if (!refcount_dec_and_test(&f->refcnt)) 506 return; 507 508 if (tcf_exts_get_net(&f->exts)) 509 tcf_queue_work(&f->rwork, fl_destroy_filter_work); 510 else 511 __fl_destroy_filter(f); 512 } 513 514 static struct cls_fl_filter *__fl_get(struct cls_fl_head *head, u32 handle) 515 { 516 struct cls_fl_filter *f; 517 518 rcu_read_lock(); 519 f = idr_find(&head->handle_idr, handle); 520 if (f && !refcount_inc_not_zero(&f->refcnt)) 521 f = NULL; 522 rcu_read_unlock(); 523 524 return f; 525 } 526 527 static int __fl_delete(struct tcf_proto *tp, struct cls_fl_filter *f, 528 bool *last, bool rtnl_held, 529 struct netlink_ext_ack *extack) 530 { 531 struct cls_fl_head *head = fl_head_dereference(tp); 532 533 *last = false; 534 535 spin_lock(&tp->lock); 536 if (f->deleted) { 537 spin_unlock(&tp->lock); 538 return -ENOENT; 539 } 540 541 f->deleted = true; 542 rhashtable_remove_fast(&f->mask->ht, &f->ht_node, 543 f->mask->filter_ht_params); 544 idr_remove(&head->handle_idr, f->handle); 545 list_del_rcu(&f->list); 546 spin_unlock(&tp->lock); 547 548 *last = fl_mask_put(head, f->mask); 549 if (!tc_skip_hw(f->flags)) 550 fl_hw_destroy_filter(tp, f, rtnl_held, extack); 551 tcf_unbind_filter(tp, &f->res); 552 __fl_put(f); 553 554 return 0; 555 } 556 557 static void fl_destroy_sleepable(struct work_struct *work) 558 { 559 struct cls_fl_head *head = container_of(to_rcu_work(work), 560 struct cls_fl_head, 561 rwork); 562 563 rhashtable_destroy(&head->ht); 564 kfree(head); 565 module_put(THIS_MODULE); 566 } 567 568 static void fl_destroy(struct tcf_proto *tp, bool rtnl_held, 569 struct netlink_ext_ack *extack) 570 { 571 struct cls_fl_head *head = fl_head_dereference(tp); 572 struct fl_flow_mask *mask, *next_mask; 573 struct cls_fl_filter *f, *next; 574 bool last; 575 576 list_for_each_entry_safe(mask, next_mask, &head->masks, list) { 577 list_for_each_entry_safe(f, next, &mask->filters, list) { 578 __fl_delete(tp, f, &last, rtnl_held, extack); 579 if (last) 580 break; 581 } 582 } 583 idr_destroy(&head->handle_idr); 584 585 __module_get(THIS_MODULE); 586 tcf_queue_work(&head->rwork, fl_destroy_sleepable); 587 } 588 589 static void fl_put(struct tcf_proto *tp, void *arg) 590 { 591 struct cls_fl_filter *f = arg; 592 593 __fl_put(f); 594 } 595 596 static void *fl_get(struct tcf_proto *tp, u32 handle) 597 { 598 struct cls_fl_head *head = fl_head_dereference(tp); 599 600 return __fl_get(head, handle); 601 } 602 603 static const struct nla_policy fl_policy[TCA_FLOWER_MAX + 1] = { 604 [TCA_FLOWER_UNSPEC] = { .type = NLA_UNSPEC }, 605 [TCA_FLOWER_CLASSID] = { .type = NLA_U32 }, 606 [TCA_FLOWER_INDEV] = { .type = NLA_STRING, 607 .len = IFNAMSIZ }, 608 [TCA_FLOWER_KEY_ETH_DST] = { .len = ETH_ALEN }, 609 [TCA_FLOWER_KEY_ETH_DST_MASK] = { .len = ETH_ALEN }, 610 [TCA_FLOWER_KEY_ETH_SRC] = { .len = ETH_ALEN }, 611 [TCA_FLOWER_KEY_ETH_SRC_MASK] = { .len = ETH_ALEN }, 612 [TCA_FLOWER_KEY_ETH_TYPE] = { .type = NLA_U16 }, 613 [TCA_FLOWER_KEY_IP_PROTO] = { .type = NLA_U8 }, 614 [TCA_FLOWER_KEY_IPV4_SRC] = { .type = NLA_U32 }, 615 [TCA_FLOWER_KEY_IPV4_SRC_MASK] = { .type = NLA_U32 }, 616 [TCA_FLOWER_KEY_IPV4_DST] = { .type = NLA_U32 }, 617 [TCA_FLOWER_KEY_IPV4_DST_MASK] = { .type = NLA_U32 }, 618 [TCA_FLOWER_KEY_IPV6_SRC] = { .len = sizeof(struct in6_addr) }, 619 [TCA_FLOWER_KEY_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) }, 620 [TCA_FLOWER_KEY_IPV6_DST] = { .len = sizeof(struct in6_addr) }, 621 [TCA_FLOWER_KEY_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) }, 622 [TCA_FLOWER_KEY_TCP_SRC] = { .type = NLA_U16 }, 623 [TCA_FLOWER_KEY_TCP_DST] = { .type = NLA_U16 }, 624 [TCA_FLOWER_KEY_UDP_SRC] = { .type = NLA_U16 }, 625 [TCA_FLOWER_KEY_UDP_DST] = { .type = NLA_U16 }, 626 [TCA_FLOWER_KEY_VLAN_ID] = { .type = NLA_U16 }, 627 [TCA_FLOWER_KEY_VLAN_PRIO] = { .type = NLA_U8 }, 628 [TCA_FLOWER_KEY_VLAN_ETH_TYPE] = { .type = NLA_U16 }, 629 [TCA_FLOWER_KEY_ENC_KEY_ID] = { .type = NLA_U32 }, 630 [TCA_FLOWER_KEY_ENC_IPV4_SRC] = { .type = NLA_U32 }, 631 [TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK] = { .type = NLA_U32 }, 632 [TCA_FLOWER_KEY_ENC_IPV4_DST] = { .type = NLA_U32 }, 633 [TCA_FLOWER_KEY_ENC_IPV4_DST_MASK] = { .type = NLA_U32 }, 634 [TCA_FLOWER_KEY_ENC_IPV6_SRC] = { .len = sizeof(struct in6_addr) }, 635 [TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) }, 636 [TCA_FLOWER_KEY_ENC_IPV6_DST] = { .len = sizeof(struct in6_addr) }, 637 [TCA_FLOWER_KEY_ENC_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) }, 638 [TCA_FLOWER_KEY_TCP_SRC_MASK] = { .type = NLA_U16 }, 639 [TCA_FLOWER_KEY_TCP_DST_MASK] = { .type = NLA_U16 }, 640 [TCA_FLOWER_KEY_UDP_SRC_MASK] = { .type = NLA_U16 }, 641 [TCA_FLOWER_KEY_UDP_DST_MASK] = { .type = NLA_U16 }, 642 [TCA_FLOWER_KEY_SCTP_SRC_MASK] = { .type = NLA_U16 }, 643 [TCA_FLOWER_KEY_SCTP_DST_MASK] = { .type = NLA_U16 }, 644 [TCA_FLOWER_KEY_SCTP_SRC] = { .type = NLA_U16 }, 645 [TCA_FLOWER_KEY_SCTP_DST] = { .type = NLA_U16 }, 646 [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT] = { .type = NLA_U16 }, 647 [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK] = { .type = NLA_U16 }, 648 [TCA_FLOWER_KEY_ENC_UDP_DST_PORT] = { .type = NLA_U16 }, 649 [TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK] = { .type = NLA_U16 }, 650 [TCA_FLOWER_KEY_FLAGS] = { .type = NLA_U32 }, 651 [TCA_FLOWER_KEY_FLAGS_MASK] = { .type = NLA_U32 }, 652 [TCA_FLOWER_KEY_ICMPV4_TYPE] = { .type = NLA_U8 }, 653 [TCA_FLOWER_KEY_ICMPV4_TYPE_MASK] = { .type = NLA_U8 }, 654 [TCA_FLOWER_KEY_ICMPV4_CODE] = { .type = NLA_U8 }, 655 [TCA_FLOWER_KEY_ICMPV4_CODE_MASK] = { .type = NLA_U8 }, 656 [TCA_FLOWER_KEY_ICMPV6_TYPE] = { .type = NLA_U8 }, 657 [TCA_FLOWER_KEY_ICMPV6_TYPE_MASK] = { .type = NLA_U8 }, 658 [TCA_FLOWER_KEY_ICMPV6_CODE] = { .type = NLA_U8 }, 659 [TCA_FLOWER_KEY_ICMPV6_CODE_MASK] = { .type = NLA_U8 }, 660 [TCA_FLOWER_KEY_ARP_SIP] = { .type = NLA_U32 }, 661 [TCA_FLOWER_KEY_ARP_SIP_MASK] = { .type = NLA_U32 }, 662 [TCA_FLOWER_KEY_ARP_TIP] = { .type = NLA_U32 }, 663 [TCA_FLOWER_KEY_ARP_TIP_MASK] = { .type = NLA_U32 }, 664 [TCA_FLOWER_KEY_ARP_OP] = { .type = NLA_U8 }, 665 [TCA_FLOWER_KEY_ARP_OP_MASK] = { .type = NLA_U8 }, 666 [TCA_FLOWER_KEY_ARP_SHA] = { .len = ETH_ALEN }, 667 [TCA_FLOWER_KEY_ARP_SHA_MASK] = { .len = ETH_ALEN }, 668 [TCA_FLOWER_KEY_ARP_THA] = { .len = ETH_ALEN }, 669 [TCA_FLOWER_KEY_ARP_THA_MASK] = { .len = ETH_ALEN }, 670 [TCA_FLOWER_KEY_MPLS_TTL] = { .type = NLA_U8 }, 671 [TCA_FLOWER_KEY_MPLS_BOS] = { .type = NLA_U8 }, 672 [TCA_FLOWER_KEY_MPLS_TC] = { .type = NLA_U8 }, 673 [TCA_FLOWER_KEY_MPLS_LABEL] = { .type = NLA_U32 }, 674 [TCA_FLOWER_KEY_TCP_FLAGS] = { .type = NLA_U16 }, 675 [TCA_FLOWER_KEY_TCP_FLAGS_MASK] = { .type = NLA_U16 }, 676 [TCA_FLOWER_KEY_IP_TOS] = { .type = NLA_U8 }, 677 [TCA_FLOWER_KEY_IP_TOS_MASK] = { .type = NLA_U8 }, 678 [TCA_FLOWER_KEY_IP_TTL] = { .type = NLA_U8 }, 679 [TCA_FLOWER_KEY_IP_TTL_MASK] = { .type = NLA_U8 }, 680 [TCA_FLOWER_KEY_CVLAN_ID] = { .type = NLA_U16 }, 681 [TCA_FLOWER_KEY_CVLAN_PRIO] = { .type = NLA_U8 }, 682 [TCA_FLOWER_KEY_CVLAN_ETH_TYPE] = { .type = NLA_U16 }, 683 [TCA_FLOWER_KEY_ENC_IP_TOS] = { .type = NLA_U8 }, 684 [TCA_FLOWER_KEY_ENC_IP_TOS_MASK] = { .type = NLA_U8 }, 685 [TCA_FLOWER_KEY_ENC_IP_TTL] = { .type = NLA_U8 }, 686 [TCA_FLOWER_KEY_ENC_IP_TTL_MASK] = { .type = NLA_U8 }, 687 [TCA_FLOWER_KEY_ENC_OPTS] = { .type = NLA_NESTED }, 688 [TCA_FLOWER_KEY_ENC_OPTS_MASK] = { .type = NLA_NESTED }, 689 }; 690 691 static const struct nla_policy 692 enc_opts_policy[TCA_FLOWER_KEY_ENC_OPTS_MAX + 1] = { 693 [TCA_FLOWER_KEY_ENC_OPTS_GENEVE] = { .type = NLA_NESTED }, 694 }; 695 696 static const struct nla_policy 697 geneve_opt_policy[TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX + 1] = { 698 [TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS] = { .type = NLA_U16 }, 699 [TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE] = { .type = NLA_U8 }, 700 [TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA] = { .type = NLA_BINARY, 701 .len = 128 }, 702 }; 703 704 static void fl_set_key_val(struct nlattr **tb, 705 void *val, int val_type, 706 void *mask, int mask_type, int len) 707 { 708 if (!tb[val_type]) 709 return; 710 memcpy(val, nla_data(tb[val_type]), len); 711 if (mask_type == TCA_FLOWER_UNSPEC || !tb[mask_type]) 712 memset(mask, 0xff, len); 713 else 714 memcpy(mask, nla_data(tb[mask_type]), len); 715 } 716 717 static int fl_set_key_port_range(struct nlattr **tb, struct fl_flow_key *key, 718 struct fl_flow_key *mask) 719 { 720 fl_set_key_val(tb, &key->tp_min.dst, 721 TCA_FLOWER_KEY_PORT_DST_MIN, &mask->tp_min.dst, 722 TCA_FLOWER_UNSPEC, sizeof(key->tp_min.dst)); 723 fl_set_key_val(tb, &key->tp_max.dst, 724 TCA_FLOWER_KEY_PORT_DST_MAX, &mask->tp_max.dst, 725 TCA_FLOWER_UNSPEC, sizeof(key->tp_max.dst)); 726 fl_set_key_val(tb, &key->tp_min.src, 727 TCA_FLOWER_KEY_PORT_SRC_MIN, &mask->tp_min.src, 728 TCA_FLOWER_UNSPEC, sizeof(key->tp_min.src)); 729 fl_set_key_val(tb, &key->tp_max.src, 730 TCA_FLOWER_KEY_PORT_SRC_MAX, &mask->tp_max.src, 731 TCA_FLOWER_UNSPEC, sizeof(key->tp_max.src)); 732 733 if ((mask->tp_min.dst && mask->tp_max.dst && 734 htons(key->tp_max.dst) <= htons(key->tp_min.dst)) || 735 (mask->tp_min.src && mask->tp_max.src && 736 htons(key->tp_max.src) <= htons(key->tp_min.src))) 737 return -EINVAL; 738 739 return 0; 740 } 741 742 static int fl_set_key_mpls(struct nlattr **tb, 743 struct flow_dissector_key_mpls *key_val, 744 struct flow_dissector_key_mpls *key_mask) 745 { 746 if (tb[TCA_FLOWER_KEY_MPLS_TTL]) { 747 key_val->mpls_ttl = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TTL]); 748 key_mask->mpls_ttl = MPLS_TTL_MASK; 749 } 750 if (tb[TCA_FLOWER_KEY_MPLS_BOS]) { 751 u8 bos = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_BOS]); 752 753 if (bos & ~MPLS_BOS_MASK) 754 return -EINVAL; 755 key_val->mpls_bos = bos; 756 key_mask->mpls_bos = MPLS_BOS_MASK; 757 } 758 if (tb[TCA_FLOWER_KEY_MPLS_TC]) { 759 u8 tc = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TC]); 760 761 if (tc & ~MPLS_TC_MASK) 762 return -EINVAL; 763 key_val->mpls_tc = tc; 764 key_mask->mpls_tc = MPLS_TC_MASK; 765 } 766 if (tb[TCA_FLOWER_KEY_MPLS_LABEL]) { 767 u32 label = nla_get_u32(tb[TCA_FLOWER_KEY_MPLS_LABEL]); 768 769 if (label & ~MPLS_LABEL_MASK) 770 return -EINVAL; 771 key_val->mpls_label = label; 772 key_mask->mpls_label = MPLS_LABEL_MASK; 773 } 774 return 0; 775 } 776 777 static void fl_set_key_vlan(struct nlattr **tb, 778 __be16 ethertype, 779 int vlan_id_key, int vlan_prio_key, 780 struct flow_dissector_key_vlan *key_val, 781 struct flow_dissector_key_vlan *key_mask) 782 { 783 #define VLAN_PRIORITY_MASK 0x7 784 785 if (tb[vlan_id_key]) { 786 key_val->vlan_id = 787 nla_get_u16(tb[vlan_id_key]) & VLAN_VID_MASK; 788 key_mask->vlan_id = VLAN_VID_MASK; 789 } 790 if (tb[vlan_prio_key]) { 791 key_val->vlan_priority = 792 nla_get_u8(tb[vlan_prio_key]) & 793 VLAN_PRIORITY_MASK; 794 key_mask->vlan_priority = VLAN_PRIORITY_MASK; 795 } 796 key_val->vlan_tpid = ethertype; 797 key_mask->vlan_tpid = cpu_to_be16(~0); 798 } 799 800 static void fl_set_key_flag(u32 flower_key, u32 flower_mask, 801 u32 *dissector_key, u32 *dissector_mask, 802 u32 flower_flag_bit, u32 dissector_flag_bit) 803 { 804 if (flower_mask & flower_flag_bit) { 805 *dissector_mask |= dissector_flag_bit; 806 if (flower_key & flower_flag_bit) 807 *dissector_key |= dissector_flag_bit; 808 } 809 } 810 811 static int fl_set_key_flags(struct nlattr **tb, 812 u32 *flags_key, u32 *flags_mask) 813 { 814 u32 key, mask; 815 816 /* mask is mandatory for flags */ 817 if (!tb[TCA_FLOWER_KEY_FLAGS_MASK]) 818 return -EINVAL; 819 820 key = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS])); 821 mask = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS_MASK])); 822 823 *flags_key = 0; 824 *flags_mask = 0; 825 826 fl_set_key_flag(key, mask, flags_key, flags_mask, 827 TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT); 828 fl_set_key_flag(key, mask, flags_key, flags_mask, 829 TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST, 830 FLOW_DIS_FIRST_FRAG); 831 832 return 0; 833 } 834 835 static void fl_set_key_ip(struct nlattr **tb, bool encap, 836 struct flow_dissector_key_ip *key, 837 struct flow_dissector_key_ip *mask) 838 { 839 int tos_key = encap ? TCA_FLOWER_KEY_ENC_IP_TOS : TCA_FLOWER_KEY_IP_TOS; 840 int ttl_key = encap ? TCA_FLOWER_KEY_ENC_IP_TTL : TCA_FLOWER_KEY_IP_TTL; 841 int tos_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TOS_MASK : TCA_FLOWER_KEY_IP_TOS_MASK; 842 int ttl_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TTL_MASK : TCA_FLOWER_KEY_IP_TTL_MASK; 843 844 fl_set_key_val(tb, &key->tos, tos_key, &mask->tos, tos_mask, sizeof(key->tos)); 845 fl_set_key_val(tb, &key->ttl, ttl_key, &mask->ttl, ttl_mask, sizeof(key->ttl)); 846 } 847 848 static int fl_set_geneve_opt(const struct nlattr *nla, struct fl_flow_key *key, 849 int depth, int option_len, 850 struct netlink_ext_ack *extack) 851 { 852 struct nlattr *tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX + 1]; 853 struct nlattr *class = NULL, *type = NULL, *data = NULL; 854 struct geneve_opt *opt; 855 int err, data_len = 0; 856 857 if (option_len > sizeof(struct geneve_opt)) 858 data_len = option_len - sizeof(struct geneve_opt); 859 860 opt = (struct geneve_opt *)&key->enc_opts.data[key->enc_opts.len]; 861 memset(opt, 0xff, option_len); 862 opt->length = data_len / 4; 863 opt->r1 = 0; 864 opt->r2 = 0; 865 opt->r3 = 0; 866 867 /* If no mask has been prodived we assume an exact match. */ 868 if (!depth) 869 return sizeof(struct geneve_opt) + data_len; 870 871 if (nla_type(nla) != TCA_FLOWER_KEY_ENC_OPTS_GENEVE) { 872 NL_SET_ERR_MSG(extack, "Non-geneve option type for mask"); 873 return -EINVAL; 874 } 875 876 err = nla_parse_nested_deprecated(tb, 877 TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX, 878 nla, geneve_opt_policy, extack); 879 if (err < 0) 880 return err; 881 882 /* We are not allowed to omit any of CLASS, TYPE or DATA 883 * fields from the key. 884 */ 885 if (!option_len && 886 (!tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS] || 887 !tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE] || 888 !tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA])) { 889 NL_SET_ERR_MSG(extack, "Missing tunnel key geneve option class, type or data"); 890 return -EINVAL; 891 } 892 893 /* Omitting any of CLASS, TYPE or DATA fields is allowed 894 * for the mask. 895 */ 896 if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA]) { 897 int new_len = key->enc_opts.len; 898 899 data = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA]; 900 data_len = nla_len(data); 901 if (data_len < 4) { 902 NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is less than 4 bytes long"); 903 return -ERANGE; 904 } 905 if (data_len % 4) { 906 NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is not a multiple of 4 bytes long"); 907 return -ERANGE; 908 } 909 910 new_len += sizeof(struct geneve_opt) + data_len; 911 BUILD_BUG_ON(FLOW_DIS_TUN_OPTS_MAX != IP_TUNNEL_OPTS_MAX); 912 if (new_len > FLOW_DIS_TUN_OPTS_MAX) { 913 NL_SET_ERR_MSG(extack, "Tunnel options exceeds max size"); 914 return -ERANGE; 915 } 916 opt->length = data_len / 4; 917 memcpy(opt->opt_data, nla_data(data), data_len); 918 } 919 920 if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS]) { 921 class = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS]; 922 opt->opt_class = nla_get_be16(class); 923 } 924 925 if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE]) { 926 type = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE]; 927 opt->type = nla_get_u8(type); 928 } 929 930 return sizeof(struct geneve_opt) + data_len; 931 } 932 933 static int fl_set_enc_opt(struct nlattr **tb, struct fl_flow_key *key, 934 struct fl_flow_key *mask, 935 struct netlink_ext_ack *extack) 936 { 937 const struct nlattr *nla_enc_key, *nla_opt_key, *nla_opt_msk = NULL; 938 int err, option_len, key_depth, msk_depth = 0; 939 940 err = nla_validate_nested_deprecated(tb[TCA_FLOWER_KEY_ENC_OPTS], 941 TCA_FLOWER_KEY_ENC_OPTS_MAX, 942 enc_opts_policy, extack); 943 if (err) 944 return err; 945 946 nla_enc_key = nla_data(tb[TCA_FLOWER_KEY_ENC_OPTS]); 947 948 if (tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]) { 949 err = nla_validate_nested_deprecated(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK], 950 TCA_FLOWER_KEY_ENC_OPTS_MAX, 951 enc_opts_policy, extack); 952 if (err) 953 return err; 954 955 nla_opt_msk = nla_data(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]); 956 msk_depth = nla_len(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]); 957 } 958 959 nla_for_each_attr(nla_opt_key, nla_enc_key, 960 nla_len(tb[TCA_FLOWER_KEY_ENC_OPTS]), key_depth) { 961 switch (nla_type(nla_opt_key)) { 962 case TCA_FLOWER_KEY_ENC_OPTS_GENEVE: 963 option_len = 0; 964 key->enc_opts.dst_opt_type = TUNNEL_GENEVE_OPT; 965 option_len = fl_set_geneve_opt(nla_opt_key, key, 966 key_depth, option_len, 967 extack); 968 if (option_len < 0) 969 return option_len; 970 971 key->enc_opts.len += option_len; 972 /* At the same time we need to parse through the mask 973 * in order to verify exact and mask attribute lengths. 974 */ 975 mask->enc_opts.dst_opt_type = TUNNEL_GENEVE_OPT; 976 option_len = fl_set_geneve_opt(nla_opt_msk, mask, 977 msk_depth, option_len, 978 extack); 979 if (option_len < 0) 980 return option_len; 981 982 mask->enc_opts.len += option_len; 983 if (key->enc_opts.len != mask->enc_opts.len) { 984 NL_SET_ERR_MSG(extack, "Key and mask miss aligned"); 985 return -EINVAL; 986 } 987 988 if (msk_depth) 989 nla_opt_msk = nla_next(nla_opt_msk, &msk_depth); 990 break; 991 default: 992 NL_SET_ERR_MSG(extack, "Unknown tunnel option type"); 993 return -EINVAL; 994 } 995 } 996 997 return 0; 998 } 999 1000 static int fl_set_key(struct net *net, struct nlattr **tb, 1001 struct fl_flow_key *key, struct fl_flow_key *mask, 1002 struct netlink_ext_ack *extack) 1003 { 1004 __be16 ethertype; 1005 int ret = 0; 1006 1007 if (tb[TCA_FLOWER_INDEV]) { 1008 int err = tcf_change_indev(net, tb[TCA_FLOWER_INDEV], extack); 1009 if (err < 0) 1010 return err; 1011 key->meta.ingress_ifindex = err; 1012 mask->meta.ingress_ifindex = 0xffffffff; 1013 } 1014 1015 fl_set_key_val(tb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST, 1016 mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK, 1017 sizeof(key->eth.dst)); 1018 fl_set_key_val(tb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC, 1019 mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK, 1020 sizeof(key->eth.src)); 1021 1022 if (tb[TCA_FLOWER_KEY_ETH_TYPE]) { 1023 ethertype = nla_get_be16(tb[TCA_FLOWER_KEY_ETH_TYPE]); 1024 1025 if (eth_type_vlan(ethertype)) { 1026 fl_set_key_vlan(tb, ethertype, TCA_FLOWER_KEY_VLAN_ID, 1027 TCA_FLOWER_KEY_VLAN_PRIO, &key->vlan, 1028 &mask->vlan); 1029 1030 if (tb[TCA_FLOWER_KEY_VLAN_ETH_TYPE]) { 1031 ethertype = nla_get_be16(tb[TCA_FLOWER_KEY_VLAN_ETH_TYPE]); 1032 if (eth_type_vlan(ethertype)) { 1033 fl_set_key_vlan(tb, ethertype, 1034 TCA_FLOWER_KEY_CVLAN_ID, 1035 TCA_FLOWER_KEY_CVLAN_PRIO, 1036 &key->cvlan, &mask->cvlan); 1037 fl_set_key_val(tb, &key->basic.n_proto, 1038 TCA_FLOWER_KEY_CVLAN_ETH_TYPE, 1039 &mask->basic.n_proto, 1040 TCA_FLOWER_UNSPEC, 1041 sizeof(key->basic.n_proto)); 1042 } else { 1043 key->basic.n_proto = ethertype; 1044 mask->basic.n_proto = cpu_to_be16(~0); 1045 } 1046 } 1047 } else { 1048 key->basic.n_proto = ethertype; 1049 mask->basic.n_proto = cpu_to_be16(~0); 1050 } 1051 } 1052 1053 if (key->basic.n_proto == htons(ETH_P_IP) || 1054 key->basic.n_proto == htons(ETH_P_IPV6)) { 1055 fl_set_key_val(tb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO, 1056 &mask->basic.ip_proto, TCA_FLOWER_UNSPEC, 1057 sizeof(key->basic.ip_proto)); 1058 fl_set_key_ip(tb, false, &key->ip, &mask->ip); 1059 } 1060 1061 if (tb[TCA_FLOWER_KEY_IPV4_SRC] || tb[TCA_FLOWER_KEY_IPV4_DST]) { 1062 key->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; 1063 mask->control.addr_type = ~0; 1064 fl_set_key_val(tb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC, 1065 &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK, 1066 sizeof(key->ipv4.src)); 1067 fl_set_key_val(tb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST, 1068 &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK, 1069 sizeof(key->ipv4.dst)); 1070 } else if (tb[TCA_FLOWER_KEY_IPV6_SRC] || tb[TCA_FLOWER_KEY_IPV6_DST]) { 1071 key->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 1072 mask->control.addr_type = ~0; 1073 fl_set_key_val(tb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC, 1074 &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK, 1075 sizeof(key->ipv6.src)); 1076 fl_set_key_val(tb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST, 1077 &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK, 1078 sizeof(key->ipv6.dst)); 1079 } 1080 1081 if (key->basic.ip_proto == IPPROTO_TCP) { 1082 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC, 1083 &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK, 1084 sizeof(key->tp.src)); 1085 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST, 1086 &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK, 1087 sizeof(key->tp.dst)); 1088 fl_set_key_val(tb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS, 1089 &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK, 1090 sizeof(key->tcp.flags)); 1091 } else if (key->basic.ip_proto == IPPROTO_UDP) { 1092 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC, 1093 &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK, 1094 sizeof(key->tp.src)); 1095 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST, 1096 &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK, 1097 sizeof(key->tp.dst)); 1098 } else if (key->basic.ip_proto == IPPROTO_SCTP) { 1099 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC, 1100 &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK, 1101 sizeof(key->tp.src)); 1102 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST, 1103 &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK, 1104 sizeof(key->tp.dst)); 1105 } else if (key->basic.n_proto == htons(ETH_P_IP) && 1106 key->basic.ip_proto == IPPROTO_ICMP) { 1107 fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV4_TYPE, 1108 &mask->icmp.type, 1109 TCA_FLOWER_KEY_ICMPV4_TYPE_MASK, 1110 sizeof(key->icmp.type)); 1111 fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV4_CODE, 1112 &mask->icmp.code, 1113 TCA_FLOWER_KEY_ICMPV4_CODE_MASK, 1114 sizeof(key->icmp.code)); 1115 } else if (key->basic.n_proto == htons(ETH_P_IPV6) && 1116 key->basic.ip_proto == IPPROTO_ICMPV6) { 1117 fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV6_TYPE, 1118 &mask->icmp.type, 1119 TCA_FLOWER_KEY_ICMPV6_TYPE_MASK, 1120 sizeof(key->icmp.type)); 1121 fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV6_CODE, 1122 &mask->icmp.code, 1123 TCA_FLOWER_KEY_ICMPV6_CODE_MASK, 1124 sizeof(key->icmp.code)); 1125 } else if (key->basic.n_proto == htons(ETH_P_MPLS_UC) || 1126 key->basic.n_proto == htons(ETH_P_MPLS_MC)) { 1127 ret = fl_set_key_mpls(tb, &key->mpls, &mask->mpls); 1128 if (ret) 1129 return ret; 1130 } else if (key->basic.n_proto == htons(ETH_P_ARP) || 1131 key->basic.n_proto == htons(ETH_P_RARP)) { 1132 fl_set_key_val(tb, &key->arp.sip, TCA_FLOWER_KEY_ARP_SIP, 1133 &mask->arp.sip, TCA_FLOWER_KEY_ARP_SIP_MASK, 1134 sizeof(key->arp.sip)); 1135 fl_set_key_val(tb, &key->arp.tip, TCA_FLOWER_KEY_ARP_TIP, 1136 &mask->arp.tip, TCA_FLOWER_KEY_ARP_TIP_MASK, 1137 sizeof(key->arp.tip)); 1138 fl_set_key_val(tb, &key->arp.op, TCA_FLOWER_KEY_ARP_OP, 1139 &mask->arp.op, TCA_FLOWER_KEY_ARP_OP_MASK, 1140 sizeof(key->arp.op)); 1141 fl_set_key_val(tb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA, 1142 mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK, 1143 sizeof(key->arp.sha)); 1144 fl_set_key_val(tb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA, 1145 mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK, 1146 sizeof(key->arp.tha)); 1147 } 1148 1149 if (key->basic.ip_proto == IPPROTO_TCP || 1150 key->basic.ip_proto == IPPROTO_UDP || 1151 key->basic.ip_proto == IPPROTO_SCTP) { 1152 ret = fl_set_key_port_range(tb, key, mask); 1153 if (ret) 1154 return ret; 1155 } 1156 1157 if (tb[TCA_FLOWER_KEY_ENC_IPV4_SRC] || 1158 tb[TCA_FLOWER_KEY_ENC_IPV4_DST]) { 1159 key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; 1160 mask->enc_control.addr_type = ~0; 1161 fl_set_key_val(tb, &key->enc_ipv4.src, 1162 TCA_FLOWER_KEY_ENC_IPV4_SRC, 1163 &mask->enc_ipv4.src, 1164 TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK, 1165 sizeof(key->enc_ipv4.src)); 1166 fl_set_key_val(tb, &key->enc_ipv4.dst, 1167 TCA_FLOWER_KEY_ENC_IPV4_DST, 1168 &mask->enc_ipv4.dst, 1169 TCA_FLOWER_KEY_ENC_IPV4_DST_MASK, 1170 sizeof(key->enc_ipv4.dst)); 1171 } 1172 1173 if (tb[TCA_FLOWER_KEY_ENC_IPV6_SRC] || 1174 tb[TCA_FLOWER_KEY_ENC_IPV6_DST]) { 1175 key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 1176 mask->enc_control.addr_type = ~0; 1177 fl_set_key_val(tb, &key->enc_ipv6.src, 1178 TCA_FLOWER_KEY_ENC_IPV6_SRC, 1179 &mask->enc_ipv6.src, 1180 TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK, 1181 sizeof(key->enc_ipv6.src)); 1182 fl_set_key_val(tb, &key->enc_ipv6.dst, 1183 TCA_FLOWER_KEY_ENC_IPV6_DST, 1184 &mask->enc_ipv6.dst, 1185 TCA_FLOWER_KEY_ENC_IPV6_DST_MASK, 1186 sizeof(key->enc_ipv6.dst)); 1187 } 1188 1189 fl_set_key_val(tb, &key->enc_key_id.keyid, TCA_FLOWER_KEY_ENC_KEY_ID, 1190 &mask->enc_key_id.keyid, TCA_FLOWER_UNSPEC, 1191 sizeof(key->enc_key_id.keyid)); 1192 1193 fl_set_key_val(tb, &key->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT, 1194 &mask->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK, 1195 sizeof(key->enc_tp.src)); 1196 1197 fl_set_key_val(tb, &key->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT, 1198 &mask->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK, 1199 sizeof(key->enc_tp.dst)); 1200 1201 fl_set_key_ip(tb, true, &key->enc_ip, &mask->enc_ip); 1202 1203 if (tb[TCA_FLOWER_KEY_ENC_OPTS]) { 1204 ret = fl_set_enc_opt(tb, key, mask, extack); 1205 if (ret) 1206 return ret; 1207 } 1208 1209 if (tb[TCA_FLOWER_KEY_FLAGS]) 1210 ret = fl_set_key_flags(tb, &key->control.flags, &mask->control.flags); 1211 1212 return ret; 1213 } 1214 1215 static void fl_mask_copy(struct fl_flow_mask *dst, 1216 struct fl_flow_mask *src) 1217 { 1218 const void *psrc = fl_key_get_start(&src->key, src); 1219 void *pdst = fl_key_get_start(&dst->key, src); 1220 1221 memcpy(pdst, psrc, fl_mask_range(src)); 1222 dst->range = src->range; 1223 } 1224 1225 static const struct rhashtable_params fl_ht_params = { 1226 .key_offset = offsetof(struct cls_fl_filter, mkey), /* base offset */ 1227 .head_offset = offsetof(struct cls_fl_filter, ht_node), 1228 .automatic_shrinking = true, 1229 }; 1230 1231 static int fl_init_mask_hashtable(struct fl_flow_mask *mask) 1232 { 1233 mask->filter_ht_params = fl_ht_params; 1234 mask->filter_ht_params.key_len = fl_mask_range(mask); 1235 mask->filter_ht_params.key_offset += mask->range.start; 1236 1237 return rhashtable_init(&mask->ht, &mask->filter_ht_params); 1238 } 1239 1240 #define FL_KEY_MEMBER_OFFSET(member) offsetof(struct fl_flow_key, member) 1241 #define FL_KEY_MEMBER_SIZE(member) FIELD_SIZEOF(struct fl_flow_key, member) 1242 1243 #define FL_KEY_IS_MASKED(mask, member) \ 1244 memchr_inv(((char *)mask) + FL_KEY_MEMBER_OFFSET(member), \ 1245 0, FL_KEY_MEMBER_SIZE(member)) \ 1246 1247 #define FL_KEY_SET(keys, cnt, id, member) \ 1248 do { \ 1249 keys[cnt].key_id = id; \ 1250 keys[cnt].offset = FL_KEY_MEMBER_OFFSET(member); \ 1251 cnt++; \ 1252 } while(0); 1253 1254 #define FL_KEY_SET_IF_MASKED(mask, keys, cnt, id, member) \ 1255 do { \ 1256 if (FL_KEY_IS_MASKED(mask, member)) \ 1257 FL_KEY_SET(keys, cnt, id, member); \ 1258 } while(0); 1259 1260 static void fl_init_dissector(struct flow_dissector *dissector, 1261 struct fl_flow_key *mask) 1262 { 1263 struct flow_dissector_key keys[FLOW_DISSECTOR_KEY_MAX]; 1264 size_t cnt = 0; 1265 1266 FL_KEY_SET_IF_MASKED(mask, keys, cnt, 1267 FLOW_DISSECTOR_KEY_META, meta); 1268 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_CONTROL, control); 1269 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_BASIC, basic); 1270 FL_KEY_SET_IF_MASKED(mask, keys, cnt, 1271 FLOW_DISSECTOR_KEY_ETH_ADDRS, eth); 1272 FL_KEY_SET_IF_MASKED(mask, keys, cnt, 1273 FLOW_DISSECTOR_KEY_IPV4_ADDRS, ipv4); 1274 FL_KEY_SET_IF_MASKED(mask, keys, cnt, 1275 FLOW_DISSECTOR_KEY_IPV6_ADDRS, ipv6); 1276 if (FL_KEY_IS_MASKED(mask, tp) || 1277 FL_KEY_IS_MASKED(mask, tp_min) || FL_KEY_IS_MASKED(mask, tp_max)) 1278 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_PORTS, tp); 1279 FL_KEY_SET_IF_MASKED(mask, keys, cnt, 1280 FLOW_DISSECTOR_KEY_IP, ip); 1281 FL_KEY_SET_IF_MASKED(mask, keys, cnt, 1282 FLOW_DISSECTOR_KEY_TCP, tcp); 1283 FL_KEY_SET_IF_MASKED(mask, keys, cnt, 1284 FLOW_DISSECTOR_KEY_ICMP, icmp); 1285 FL_KEY_SET_IF_MASKED(mask, keys, cnt, 1286 FLOW_DISSECTOR_KEY_ARP, arp); 1287 FL_KEY_SET_IF_MASKED(mask, keys, cnt, 1288 FLOW_DISSECTOR_KEY_MPLS, mpls); 1289 FL_KEY_SET_IF_MASKED(mask, keys, cnt, 1290 FLOW_DISSECTOR_KEY_VLAN, vlan); 1291 FL_KEY_SET_IF_MASKED(mask, keys, cnt, 1292 FLOW_DISSECTOR_KEY_CVLAN, cvlan); 1293 FL_KEY_SET_IF_MASKED(mask, keys, cnt, 1294 FLOW_DISSECTOR_KEY_ENC_KEYID, enc_key_id); 1295 FL_KEY_SET_IF_MASKED(mask, keys, cnt, 1296 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS, enc_ipv4); 1297 FL_KEY_SET_IF_MASKED(mask, keys, cnt, 1298 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS, enc_ipv6); 1299 if (FL_KEY_IS_MASKED(mask, enc_ipv4) || 1300 FL_KEY_IS_MASKED(mask, enc_ipv6)) 1301 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_ENC_CONTROL, 1302 enc_control); 1303 FL_KEY_SET_IF_MASKED(mask, keys, cnt, 1304 FLOW_DISSECTOR_KEY_ENC_PORTS, enc_tp); 1305 FL_KEY_SET_IF_MASKED(mask, keys, cnt, 1306 FLOW_DISSECTOR_KEY_ENC_IP, enc_ip); 1307 FL_KEY_SET_IF_MASKED(mask, keys, cnt, 1308 FLOW_DISSECTOR_KEY_ENC_OPTS, enc_opts); 1309 1310 skb_flow_dissector_init(dissector, keys, cnt); 1311 } 1312 1313 static struct fl_flow_mask *fl_create_new_mask(struct cls_fl_head *head, 1314 struct fl_flow_mask *mask) 1315 { 1316 struct fl_flow_mask *newmask; 1317 int err; 1318 1319 newmask = kzalloc(sizeof(*newmask), GFP_KERNEL); 1320 if (!newmask) 1321 return ERR_PTR(-ENOMEM); 1322 1323 fl_mask_copy(newmask, mask); 1324 1325 if ((newmask->key.tp_min.dst && newmask->key.tp_max.dst) || 1326 (newmask->key.tp_min.src && newmask->key.tp_max.src)) 1327 newmask->flags |= TCA_FLOWER_MASK_FLAGS_RANGE; 1328 1329 err = fl_init_mask_hashtable(newmask); 1330 if (err) 1331 goto errout_free; 1332 1333 fl_init_dissector(&newmask->dissector, &newmask->key); 1334 1335 INIT_LIST_HEAD_RCU(&newmask->filters); 1336 1337 refcount_set(&newmask->refcnt, 1); 1338 err = rhashtable_replace_fast(&head->ht, &mask->ht_node, 1339 &newmask->ht_node, mask_ht_params); 1340 if (err) 1341 goto errout_destroy; 1342 1343 spin_lock(&head->masks_lock); 1344 list_add_tail_rcu(&newmask->list, &head->masks); 1345 spin_unlock(&head->masks_lock); 1346 1347 return newmask; 1348 1349 errout_destroy: 1350 rhashtable_destroy(&newmask->ht); 1351 errout_free: 1352 kfree(newmask); 1353 1354 return ERR_PTR(err); 1355 } 1356 1357 static int fl_check_assign_mask(struct cls_fl_head *head, 1358 struct cls_fl_filter *fnew, 1359 struct cls_fl_filter *fold, 1360 struct fl_flow_mask *mask) 1361 { 1362 struct fl_flow_mask *newmask; 1363 int ret = 0; 1364 1365 rcu_read_lock(); 1366 1367 /* Insert mask as temporary node to prevent concurrent creation of mask 1368 * with same key. Any concurrent lookups with same key will return 1369 * -EAGAIN because mask's refcnt is zero. 1370 */ 1371 fnew->mask = rhashtable_lookup_get_insert_fast(&head->ht, 1372 &mask->ht_node, 1373 mask_ht_params); 1374 if (!fnew->mask) { 1375 rcu_read_unlock(); 1376 1377 if (fold) { 1378 ret = -EINVAL; 1379 goto errout_cleanup; 1380 } 1381 1382 newmask = fl_create_new_mask(head, mask); 1383 if (IS_ERR(newmask)) { 1384 ret = PTR_ERR(newmask); 1385 goto errout_cleanup; 1386 } 1387 1388 fnew->mask = newmask; 1389 return 0; 1390 } else if (IS_ERR(fnew->mask)) { 1391 ret = PTR_ERR(fnew->mask); 1392 } else if (fold && fold->mask != fnew->mask) { 1393 ret = -EINVAL; 1394 } else if (!refcount_inc_not_zero(&fnew->mask->refcnt)) { 1395 /* Mask was deleted concurrently, try again */ 1396 ret = -EAGAIN; 1397 } 1398 rcu_read_unlock(); 1399 return ret; 1400 1401 errout_cleanup: 1402 rhashtable_remove_fast(&head->ht, &mask->ht_node, 1403 mask_ht_params); 1404 return ret; 1405 } 1406 1407 static int fl_set_parms(struct net *net, struct tcf_proto *tp, 1408 struct cls_fl_filter *f, struct fl_flow_mask *mask, 1409 unsigned long base, struct nlattr **tb, 1410 struct nlattr *est, bool ovr, 1411 struct fl_flow_tmplt *tmplt, bool rtnl_held, 1412 struct netlink_ext_ack *extack) 1413 { 1414 int err; 1415 1416 err = tcf_exts_validate(net, tp, tb, est, &f->exts, ovr, rtnl_held, 1417 extack); 1418 if (err < 0) 1419 return err; 1420 1421 if (tb[TCA_FLOWER_CLASSID]) { 1422 f->res.classid = nla_get_u32(tb[TCA_FLOWER_CLASSID]); 1423 if (!rtnl_held) 1424 rtnl_lock(); 1425 tcf_bind_filter(tp, &f->res, base); 1426 if (!rtnl_held) 1427 rtnl_unlock(); 1428 } 1429 1430 err = fl_set_key(net, tb, &f->key, &mask->key, extack); 1431 if (err) 1432 return err; 1433 1434 fl_mask_update_range(mask); 1435 fl_set_masked_key(&f->mkey, &f->key, mask); 1436 1437 if (!fl_mask_fits_tmplt(tmplt, mask)) { 1438 NL_SET_ERR_MSG_MOD(extack, "Mask does not fit the template"); 1439 return -EINVAL; 1440 } 1441 1442 return 0; 1443 } 1444 1445 static int fl_ht_insert_unique(struct cls_fl_filter *fnew, 1446 struct cls_fl_filter *fold, 1447 bool *in_ht) 1448 { 1449 struct fl_flow_mask *mask = fnew->mask; 1450 int err; 1451 1452 err = rhashtable_lookup_insert_fast(&mask->ht, 1453 &fnew->ht_node, 1454 mask->filter_ht_params); 1455 if (err) { 1456 *in_ht = false; 1457 /* It is okay if filter with same key exists when 1458 * overwriting. 1459 */ 1460 return fold && err == -EEXIST ? 0 : err; 1461 } 1462 1463 *in_ht = true; 1464 return 0; 1465 } 1466 1467 static int fl_change(struct net *net, struct sk_buff *in_skb, 1468 struct tcf_proto *tp, unsigned long base, 1469 u32 handle, struct nlattr **tca, 1470 void **arg, bool ovr, bool rtnl_held, 1471 struct netlink_ext_ack *extack) 1472 { 1473 struct cls_fl_head *head = fl_head_dereference(tp); 1474 struct cls_fl_filter *fold = *arg; 1475 struct cls_fl_filter *fnew; 1476 struct fl_flow_mask *mask; 1477 struct nlattr **tb; 1478 bool in_ht; 1479 int err; 1480 1481 if (!tca[TCA_OPTIONS]) { 1482 err = -EINVAL; 1483 goto errout_fold; 1484 } 1485 1486 mask = kzalloc(sizeof(struct fl_flow_mask), GFP_KERNEL); 1487 if (!mask) { 1488 err = -ENOBUFS; 1489 goto errout_fold; 1490 } 1491 1492 tb = kcalloc(TCA_FLOWER_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL); 1493 if (!tb) { 1494 err = -ENOBUFS; 1495 goto errout_mask_alloc; 1496 } 1497 1498 err = nla_parse_nested_deprecated(tb, TCA_FLOWER_MAX, 1499 tca[TCA_OPTIONS], fl_policy, NULL); 1500 if (err < 0) 1501 goto errout_tb; 1502 1503 if (fold && handle && fold->handle != handle) { 1504 err = -EINVAL; 1505 goto errout_tb; 1506 } 1507 1508 fnew = kzalloc(sizeof(*fnew), GFP_KERNEL); 1509 if (!fnew) { 1510 err = -ENOBUFS; 1511 goto errout_tb; 1512 } 1513 INIT_LIST_HEAD(&fnew->hw_list); 1514 refcount_set(&fnew->refcnt, 1); 1515 1516 err = tcf_exts_init(&fnew->exts, net, TCA_FLOWER_ACT, 0); 1517 if (err < 0) 1518 goto errout; 1519 1520 if (tb[TCA_FLOWER_FLAGS]) { 1521 fnew->flags = nla_get_u32(tb[TCA_FLOWER_FLAGS]); 1522 1523 if (!tc_flags_valid(fnew->flags)) { 1524 err = -EINVAL; 1525 goto errout; 1526 } 1527 } 1528 1529 err = fl_set_parms(net, tp, fnew, mask, base, tb, tca[TCA_RATE], ovr, 1530 tp->chain->tmplt_priv, rtnl_held, extack); 1531 if (err) 1532 goto errout; 1533 1534 err = fl_check_assign_mask(head, fnew, fold, mask); 1535 if (err) 1536 goto errout; 1537 1538 err = fl_ht_insert_unique(fnew, fold, &in_ht); 1539 if (err) 1540 goto errout_mask; 1541 1542 if (!tc_skip_hw(fnew->flags)) { 1543 err = fl_hw_replace_filter(tp, fnew, rtnl_held, extack); 1544 if (err) 1545 goto errout_ht; 1546 } 1547 1548 if (!tc_in_hw(fnew->flags)) 1549 fnew->flags |= TCA_CLS_FLAGS_NOT_IN_HW; 1550 1551 spin_lock(&tp->lock); 1552 1553 /* tp was deleted concurrently. -EAGAIN will cause caller to lookup 1554 * proto again or create new one, if necessary. 1555 */ 1556 if (tp->deleting) { 1557 err = -EAGAIN; 1558 goto errout_hw; 1559 } 1560 1561 if (fold) { 1562 /* Fold filter was deleted concurrently. Retry lookup. */ 1563 if (fold->deleted) { 1564 err = -EAGAIN; 1565 goto errout_hw; 1566 } 1567 1568 fnew->handle = handle; 1569 1570 if (!in_ht) { 1571 struct rhashtable_params params = 1572 fnew->mask->filter_ht_params; 1573 1574 err = rhashtable_insert_fast(&fnew->mask->ht, 1575 &fnew->ht_node, 1576 params); 1577 if (err) 1578 goto errout_hw; 1579 in_ht = true; 1580 } 1581 1582 refcount_inc(&fnew->refcnt); 1583 rhashtable_remove_fast(&fold->mask->ht, 1584 &fold->ht_node, 1585 fold->mask->filter_ht_params); 1586 idr_replace(&head->handle_idr, fnew, fnew->handle); 1587 list_replace_rcu(&fold->list, &fnew->list); 1588 fold->deleted = true; 1589 1590 spin_unlock(&tp->lock); 1591 1592 fl_mask_put(head, fold->mask); 1593 if (!tc_skip_hw(fold->flags)) 1594 fl_hw_destroy_filter(tp, fold, rtnl_held, NULL); 1595 tcf_unbind_filter(tp, &fold->res); 1596 /* Caller holds reference to fold, so refcnt is always > 0 1597 * after this. 1598 */ 1599 refcount_dec(&fold->refcnt); 1600 __fl_put(fold); 1601 } else { 1602 if (handle) { 1603 /* user specifies a handle and it doesn't exist */ 1604 err = idr_alloc_u32(&head->handle_idr, fnew, &handle, 1605 handle, GFP_ATOMIC); 1606 1607 /* Filter with specified handle was concurrently 1608 * inserted after initial check in cls_api. This is not 1609 * necessarily an error if NLM_F_EXCL is not set in 1610 * message flags. Returning EAGAIN will cause cls_api to 1611 * try to update concurrently inserted rule. 1612 */ 1613 if (err == -ENOSPC) 1614 err = -EAGAIN; 1615 } else { 1616 handle = 1; 1617 err = idr_alloc_u32(&head->handle_idr, fnew, &handle, 1618 INT_MAX, GFP_ATOMIC); 1619 } 1620 if (err) 1621 goto errout_hw; 1622 1623 refcount_inc(&fnew->refcnt); 1624 fnew->handle = handle; 1625 list_add_tail_rcu(&fnew->list, &fnew->mask->filters); 1626 spin_unlock(&tp->lock); 1627 } 1628 1629 *arg = fnew; 1630 1631 kfree(tb); 1632 tcf_queue_work(&mask->rwork, fl_uninit_mask_free_work); 1633 return 0; 1634 1635 errout_ht: 1636 spin_lock(&tp->lock); 1637 errout_hw: 1638 fnew->deleted = true; 1639 spin_unlock(&tp->lock); 1640 if (!tc_skip_hw(fnew->flags)) 1641 fl_hw_destroy_filter(tp, fnew, rtnl_held, NULL); 1642 if (in_ht) 1643 rhashtable_remove_fast(&fnew->mask->ht, &fnew->ht_node, 1644 fnew->mask->filter_ht_params); 1645 errout_mask: 1646 fl_mask_put(head, fnew->mask); 1647 errout: 1648 __fl_put(fnew); 1649 errout_tb: 1650 kfree(tb); 1651 errout_mask_alloc: 1652 tcf_queue_work(&mask->rwork, fl_uninit_mask_free_work); 1653 errout_fold: 1654 if (fold) 1655 __fl_put(fold); 1656 return err; 1657 } 1658 1659 static int fl_delete(struct tcf_proto *tp, void *arg, bool *last, 1660 bool rtnl_held, struct netlink_ext_ack *extack) 1661 { 1662 struct cls_fl_head *head = fl_head_dereference(tp); 1663 struct cls_fl_filter *f = arg; 1664 bool last_on_mask; 1665 int err = 0; 1666 1667 err = __fl_delete(tp, f, &last_on_mask, rtnl_held, extack); 1668 *last = list_empty(&head->masks); 1669 __fl_put(f); 1670 1671 return err; 1672 } 1673 1674 static void fl_walk(struct tcf_proto *tp, struct tcf_walker *arg, 1675 bool rtnl_held) 1676 { 1677 struct cls_fl_head *head = fl_head_dereference(tp); 1678 unsigned long id = arg->cookie, tmp; 1679 struct cls_fl_filter *f; 1680 1681 arg->count = arg->skip; 1682 1683 idr_for_each_entry_continue_ul(&head->handle_idr, f, tmp, id) { 1684 /* don't return filters that are being deleted */ 1685 if (!refcount_inc_not_zero(&f->refcnt)) 1686 continue; 1687 if (arg->fn(tp, f, arg) < 0) { 1688 __fl_put(f); 1689 arg->stop = 1; 1690 break; 1691 } 1692 __fl_put(f); 1693 arg->count++; 1694 } 1695 arg->cookie = id; 1696 } 1697 1698 static struct cls_fl_filter * 1699 fl_get_next_hw_filter(struct tcf_proto *tp, struct cls_fl_filter *f, bool add) 1700 { 1701 struct cls_fl_head *head = fl_head_dereference(tp); 1702 1703 spin_lock(&tp->lock); 1704 if (list_empty(&head->hw_filters)) { 1705 spin_unlock(&tp->lock); 1706 return NULL; 1707 } 1708 1709 if (!f) 1710 f = list_entry(&head->hw_filters, struct cls_fl_filter, 1711 hw_list); 1712 list_for_each_entry_continue(f, &head->hw_filters, hw_list) { 1713 if (!(add && f->deleted) && refcount_inc_not_zero(&f->refcnt)) { 1714 spin_unlock(&tp->lock); 1715 return f; 1716 } 1717 } 1718 1719 spin_unlock(&tp->lock); 1720 return NULL; 1721 } 1722 1723 static int fl_reoffload(struct tcf_proto *tp, bool add, tc_setup_cb_t *cb, 1724 void *cb_priv, struct netlink_ext_ack *extack) 1725 { 1726 struct tc_cls_flower_offload cls_flower = {}; 1727 struct tcf_block *block = tp->chain->block; 1728 struct cls_fl_filter *f = NULL; 1729 int err; 1730 1731 /* hw_filters list can only be changed by hw offload functions after 1732 * obtaining rtnl lock. Make sure it is not changed while reoffload is 1733 * iterating it. 1734 */ 1735 ASSERT_RTNL(); 1736 1737 while ((f = fl_get_next_hw_filter(tp, f, add))) { 1738 cls_flower.rule = 1739 flow_rule_alloc(tcf_exts_num_actions(&f->exts)); 1740 if (!cls_flower.rule) { 1741 __fl_put(f); 1742 return -ENOMEM; 1743 } 1744 1745 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, 1746 extack); 1747 cls_flower.command = add ? 1748 TC_CLSFLOWER_REPLACE : TC_CLSFLOWER_DESTROY; 1749 cls_flower.cookie = (unsigned long)f; 1750 cls_flower.rule->match.dissector = &f->mask->dissector; 1751 cls_flower.rule->match.mask = &f->mask->key; 1752 cls_flower.rule->match.key = &f->mkey; 1753 1754 err = tc_setup_flow_action(&cls_flower.rule->action, &f->exts); 1755 if (err) { 1756 kfree(cls_flower.rule); 1757 if (tc_skip_sw(f->flags)) { 1758 NL_SET_ERR_MSG_MOD(extack, "Failed to setup flow action"); 1759 __fl_put(f); 1760 return err; 1761 } 1762 goto next_flow; 1763 } 1764 1765 cls_flower.classid = f->res.classid; 1766 1767 err = cb(TC_SETUP_CLSFLOWER, &cls_flower, cb_priv); 1768 kfree(cls_flower.rule); 1769 1770 if (err) { 1771 if (add && tc_skip_sw(f->flags)) { 1772 __fl_put(f); 1773 return err; 1774 } 1775 goto next_flow; 1776 } 1777 1778 spin_lock(&tp->lock); 1779 tc_cls_offload_cnt_update(block, &f->in_hw_count, &f->flags, 1780 add); 1781 spin_unlock(&tp->lock); 1782 next_flow: 1783 __fl_put(f); 1784 } 1785 1786 return 0; 1787 } 1788 1789 static int fl_hw_create_tmplt(struct tcf_chain *chain, 1790 struct fl_flow_tmplt *tmplt) 1791 { 1792 struct tc_cls_flower_offload cls_flower = {}; 1793 struct tcf_block *block = chain->block; 1794 1795 cls_flower.rule = flow_rule_alloc(0); 1796 if (!cls_flower.rule) 1797 return -ENOMEM; 1798 1799 cls_flower.common.chain_index = chain->index; 1800 cls_flower.command = TC_CLSFLOWER_TMPLT_CREATE; 1801 cls_flower.cookie = (unsigned long) tmplt; 1802 cls_flower.rule->match.dissector = &tmplt->dissector; 1803 cls_flower.rule->match.mask = &tmplt->mask; 1804 cls_flower.rule->match.key = &tmplt->dummy_key; 1805 1806 /* We don't care if driver (any of them) fails to handle this 1807 * call. It serves just as a hint for it. 1808 */ 1809 tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, false); 1810 kfree(cls_flower.rule); 1811 1812 return 0; 1813 } 1814 1815 static void fl_hw_destroy_tmplt(struct tcf_chain *chain, 1816 struct fl_flow_tmplt *tmplt) 1817 { 1818 struct tc_cls_flower_offload cls_flower = {}; 1819 struct tcf_block *block = chain->block; 1820 1821 cls_flower.common.chain_index = chain->index; 1822 cls_flower.command = TC_CLSFLOWER_TMPLT_DESTROY; 1823 cls_flower.cookie = (unsigned long) tmplt; 1824 1825 tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, false); 1826 } 1827 1828 static void *fl_tmplt_create(struct net *net, struct tcf_chain *chain, 1829 struct nlattr **tca, 1830 struct netlink_ext_ack *extack) 1831 { 1832 struct fl_flow_tmplt *tmplt; 1833 struct nlattr **tb; 1834 int err; 1835 1836 if (!tca[TCA_OPTIONS]) 1837 return ERR_PTR(-EINVAL); 1838 1839 tb = kcalloc(TCA_FLOWER_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL); 1840 if (!tb) 1841 return ERR_PTR(-ENOBUFS); 1842 err = nla_parse_nested_deprecated(tb, TCA_FLOWER_MAX, 1843 tca[TCA_OPTIONS], fl_policy, NULL); 1844 if (err) 1845 goto errout_tb; 1846 1847 tmplt = kzalloc(sizeof(*tmplt), GFP_KERNEL); 1848 if (!tmplt) { 1849 err = -ENOMEM; 1850 goto errout_tb; 1851 } 1852 tmplt->chain = chain; 1853 err = fl_set_key(net, tb, &tmplt->dummy_key, &tmplt->mask, extack); 1854 if (err) 1855 goto errout_tmplt; 1856 1857 fl_init_dissector(&tmplt->dissector, &tmplt->mask); 1858 1859 err = fl_hw_create_tmplt(chain, tmplt); 1860 if (err) 1861 goto errout_tmplt; 1862 1863 kfree(tb); 1864 return tmplt; 1865 1866 errout_tmplt: 1867 kfree(tmplt); 1868 errout_tb: 1869 kfree(tb); 1870 return ERR_PTR(err); 1871 } 1872 1873 static void fl_tmplt_destroy(void *tmplt_priv) 1874 { 1875 struct fl_flow_tmplt *tmplt = tmplt_priv; 1876 1877 fl_hw_destroy_tmplt(tmplt->chain, tmplt); 1878 kfree(tmplt); 1879 } 1880 1881 static int fl_dump_key_val(struct sk_buff *skb, 1882 void *val, int val_type, 1883 void *mask, int mask_type, int len) 1884 { 1885 int err; 1886 1887 if (!memchr_inv(mask, 0, len)) 1888 return 0; 1889 err = nla_put(skb, val_type, len, val); 1890 if (err) 1891 return err; 1892 if (mask_type != TCA_FLOWER_UNSPEC) { 1893 err = nla_put(skb, mask_type, len, mask); 1894 if (err) 1895 return err; 1896 } 1897 return 0; 1898 } 1899 1900 static int fl_dump_key_port_range(struct sk_buff *skb, struct fl_flow_key *key, 1901 struct fl_flow_key *mask) 1902 { 1903 if (fl_dump_key_val(skb, &key->tp_min.dst, TCA_FLOWER_KEY_PORT_DST_MIN, 1904 &mask->tp_min.dst, TCA_FLOWER_UNSPEC, 1905 sizeof(key->tp_min.dst)) || 1906 fl_dump_key_val(skb, &key->tp_max.dst, TCA_FLOWER_KEY_PORT_DST_MAX, 1907 &mask->tp_max.dst, TCA_FLOWER_UNSPEC, 1908 sizeof(key->tp_max.dst)) || 1909 fl_dump_key_val(skb, &key->tp_min.src, TCA_FLOWER_KEY_PORT_SRC_MIN, 1910 &mask->tp_min.src, TCA_FLOWER_UNSPEC, 1911 sizeof(key->tp_min.src)) || 1912 fl_dump_key_val(skb, &key->tp_max.src, TCA_FLOWER_KEY_PORT_SRC_MAX, 1913 &mask->tp_max.src, TCA_FLOWER_UNSPEC, 1914 sizeof(key->tp_max.src))) 1915 return -1; 1916 1917 return 0; 1918 } 1919 1920 static int fl_dump_key_mpls(struct sk_buff *skb, 1921 struct flow_dissector_key_mpls *mpls_key, 1922 struct flow_dissector_key_mpls *mpls_mask) 1923 { 1924 int err; 1925 1926 if (!memchr_inv(mpls_mask, 0, sizeof(*mpls_mask))) 1927 return 0; 1928 if (mpls_mask->mpls_ttl) { 1929 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TTL, 1930 mpls_key->mpls_ttl); 1931 if (err) 1932 return err; 1933 } 1934 if (mpls_mask->mpls_tc) { 1935 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TC, 1936 mpls_key->mpls_tc); 1937 if (err) 1938 return err; 1939 } 1940 if (mpls_mask->mpls_label) { 1941 err = nla_put_u32(skb, TCA_FLOWER_KEY_MPLS_LABEL, 1942 mpls_key->mpls_label); 1943 if (err) 1944 return err; 1945 } 1946 if (mpls_mask->mpls_bos) { 1947 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_BOS, 1948 mpls_key->mpls_bos); 1949 if (err) 1950 return err; 1951 } 1952 return 0; 1953 } 1954 1955 static int fl_dump_key_ip(struct sk_buff *skb, bool encap, 1956 struct flow_dissector_key_ip *key, 1957 struct flow_dissector_key_ip *mask) 1958 { 1959 int tos_key = encap ? TCA_FLOWER_KEY_ENC_IP_TOS : TCA_FLOWER_KEY_IP_TOS; 1960 int ttl_key = encap ? TCA_FLOWER_KEY_ENC_IP_TTL : TCA_FLOWER_KEY_IP_TTL; 1961 int tos_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TOS_MASK : TCA_FLOWER_KEY_IP_TOS_MASK; 1962 int ttl_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TTL_MASK : TCA_FLOWER_KEY_IP_TTL_MASK; 1963 1964 if (fl_dump_key_val(skb, &key->tos, tos_key, &mask->tos, tos_mask, sizeof(key->tos)) || 1965 fl_dump_key_val(skb, &key->ttl, ttl_key, &mask->ttl, ttl_mask, sizeof(key->ttl))) 1966 return -1; 1967 1968 return 0; 1969 } 1970 1971 static int fl_dump_key_vlan(struct sk_buff *skb, 1972 int vlan_id_key, int vlan_prio_key, 1973 struct flow_dissector_key_vlan *vlan_key, 1974 struct flow_dissector_key_vlan *vlan_mask) 1975 { 1976 int err; 1977 1978 if (!memchr_inv(vlan_mask, 0, sizeof(*vlan_mask))) 1979 return 0; 1980 if (vlan_mask->vlan_id) { 1981 err = nla_put_u16(skb, vlan_id_key, 1982 vlan_key->vlan_id); 1983 if (err) 1984 return err; 1985 } 1986 if (vlan_mask->vlan_priority) { 1987 err = nla_put_u8(skb, vlan_prio_key, 1988 vlan_key->vlan_priority); 1989 if (err) 1990 return err; 1991 } 1992 return 0; 1993 } 1994 1995 static void fl_get_key_flag(u32 dissector_key, u32 dissector_mask, 1996 u32 *flower_key, u32 *flower_mask, 1997 u32 flower_flag_bit, u32 dissector_flag_bit) 1998 { 1999 if (dissector_mask & dissector_flag_bit) { 2000 *flower_mask |= flower_flag_bit; 2001 if (dissector_key & dissector_flag_bit) 2002 *flower_key |= flower_flag_bit; 2003 } 2004 } 2005 2006 static int fl_dump_key_flags(struct sk_buff *skb, u32 flags_key, u32 flags_mask) 2007 { 2008 u32 key, mask; 2009 __be32 _key, _mask; 2010 int err; 2011 2012 if (!memchr_inv(&flags_mask, 0, sizeof(flags_mask))) 2013 return 0; 2014 2015 key = 0; 2016 mask = 0; 2017 2018 fl_get_key_flag(flags_key, flags_mask, &key, &mask, 2019 TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT); 2020 fl_get_key_flag(flags_key, flags_mask, &key, &mask, 2021 TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST, 2022 FLOW_DIS_FIRST_FRAG); 2023 2024 _key = cpu_to_be32(key); 2025 _mask = cpu_to_be32(mask); 2026 2027 err = nla_put(skb, TCA_FLOWER_KEY_FLAGS, 4, &_key); 2028 if (err) 2029 return err; 2030 2031 return nla_put(skb, TCA_FLOWER_KEY_FLAGS_MASK, 4, &_mask); 2032 } 2033 2034 static int fl_dump_key_geneve_opt(struct sk_buff *skb, 2035 struct flow_dissector_key_enc_opts *enc_opts) 2036 { 2037 struct geneve_opt *opt; 2038 struct nlattr *nest; 2039 int opt_off = 0; 2040 2041 nest = nla_nest_start_noflag(skb, TCA_FLOWER_KEY_ENC_OPTS_GENEVE); 2042 if (!nest) 2043 goto nla_put_failure; 2044 2045 while (enc_opts->len > opt_off) { 2046 opt = (struct geneve_opt *)&enc_opts->data[opt_off]; 2047 2048 if (nla_put_be16(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS, 2049 opt->opt_class)) 2050 goto nla_put_failure; 2051 if (nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE, 2052 opt->type)) 2053 goto nla_put_failure; 2054 if (nla_put(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA, 2055 opt->length * 4, opt->opt_data)) 2056 goto nla_put_failure; 2057 2058 opt_off += sizeof(struct geneve_opt) + opt->length * 4; 2059 } 2060 nla_nest_end(skb, nest); 2061 return 0; 2062 2063 nla_put_failure: 2064 nla_nest_cancel(skb, nest); 2065 return -EMSGSIZE; 2066 } 2067 2068 static int fl_dump_key_options(struct sk_buff *skb, int enc_opt_type, 2069 struct flow_dissector_key_enc_opts *enc_opts) 2070 { 2071 struct nlattr *nest; 2072 int err; 2073 2074 if (!enc_opts->len) 2075 return 0; 2076 2077 nest = nla_nest_start_noflag(skb, enc_opt_type); 2078 if (!nest) 2079 goto nla_put_failure; 2080 2081 switch (enc_opts->dst_opt_type) { 2082 case TUNNEL_GENEVE_OPT: 2083 err = fl_dump_key_geneve_opt(skb, enc_opts); 2084 if (err) 2085 goto nla_put_failure; 2086 break; 2087 default: 2088 goto nla_put_failure; 2089 } 2090 nla_nest_end(skb, nest); 2091 return 0; 2092 2093 nla_put_failure: 2094 nla_nest_cancel(skb, nest); 2095 return -EMSGSIZE; 2096 } 2097 2098 static int fl_dump_key_enc_opt(struct sk_buff *skb, 2099 struct flow_dissector_key_enc_opts *key_opts, 2100 struct flow_dissector_key_enc_opts *msk_opts) 2101 { 2102 int err; 2103 2104 err = fl_dump_key_options(skb, TCA_FLOWER_KEY_ENC_OPTS, key_opts); 2105 if (err) 2106 return err; 2107 2108 return fl_dump_key_options(skb, TCA_FLOWER_KEY_ENC_OPTS_MASK, msk_opts); 2109 } 2110 2111 static int fl_dump_key(struct sk_buff *skb, struct net *net, 2112 struct fl_flow_key *key, struct fl_flow_key *mask) 2113 { 2114 if (mask->meta.ingress_ifindex) { 2115 struct net_device *dev; 2116 2117 dev = __dev_get_by_index(net, key->meta.ingress_ifindex); 2118 if (dev && nla_put_string(skb, TCA_FLOWER_INDEV, dev->name)) 2119 goto nla_put_failure; 2120 } 2121 2122 if (fl_dump_key_val(skb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST, 2123 mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK, 2124 sizeof(key->eth.dst)) || 2125 fl_dump_key_val(skb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC, 2126 mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK, 2127 sizeof(key->eth.src)) || 2128 fl_dump_key_val(skb, &key->basic.n_proto, TCA_FLOWER_KEY_ETH_TYPE, 2129 &mask->basic.n_proto, TCA_FLOWER_UNSPEC, 2130 sizeof(key->basic.n_proto))) 2131 goto nla_put_failure; 2132 2133 if (fl_dump_key_mpls(skb, &key->mpls, &mask->mpls)) 2134 goto nla_put_failure; 2135 2136 if (fl_dump_key_vlan(skb, TCA_FLOWER_KEY_VLAN_ID, 2137 TCA_FLOWER_KEY_VLAN_PRIO, &key->vlan, &mask->vlan)) 2138 goto nla_put_failure; 2139 2140 if (fl_dump_key_vlan(skb, TCA_FLOWER_KEY_CVLAN_ID, 2141 TCA_FLOWER_KEY_CVLAN_PRIO, 2142 &key->cvlan, &mask->cvlan) || 2143 (mask->cvlan.vlan_tpid && 2144 nla_put_be16(skb, TCA_FLOWER_KEY_VLAN_ETH_TYPE, 2145 key->cvlan.vlan_tpid))) 2146 goto nla_put_failure; 2147 2148 if (mask->basic.n_proto) { 2149 if (mask->cvlan.vlan_tpid) { 2150 if (nla_put_be16(skb, TCA_FLOWER_KEY_CVLAN_ETH_TYPE, 2151 key->basic.n_proto)) 2152 goto nla_put_failure; 2153 } else if (mask->vlan.vlan_tpid) { 2154 if (nla_put_be16(skb, TCA_FLOWER_KEY_VLAN_ETH_TYPE, 2155 key->basic.n_proto)) 2156 goto nla_put_failure; 2157 } 2158 } 2159 2160 if ((key->basic.n_proto == htons(ETH_P_IP) || 2161 key->basic.n_proto == htons(ETH_P_IPV6)) && 2162 (fl_dump_key_val(skb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO, 2163 &mask->basic.ip_proto, TCA_FLOWER_UNSPEC, 2164 sizeof(key->basic.ip_proto)) || 2165 fl_dump_key_ip(skb, false, &key->ip, &mask->ip))) 2166 goto nla_put_failure; 2167 2168 if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS && 2169 (fl_dump_key_val(skb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC, 2170 &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK, 2171 sizeof(key->ipv4.src)) || 2172 fl_dump_key_val(skb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST, 2173 &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK, 2174 sizeof(key->ipv4.dst)))) 2175 goto nla_put_failure; 2176 else if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS && 2177 (fl_dump_key_val(skb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC, 2178 &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK, 2179 sizeof(key->ipv6.src)) || 2180 fl_dump_key_val(skb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST, 2181 &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK, 2182 sizeof(key->ipv6.dst)))) 2183 goto nla_put_failure; 2184 2185 if (key->basic.ip_proto == IPPROTO_TCP && 2186 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC, 2187 &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK, 2188 sizeof(key->tp.src)) || 2189 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST, 2190 &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK, 2191 sizeof(key->tp.dst)) || 2192 fl_dump_key_val(skb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS, 2193 &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK, 2194 sizeof(key->tcp.flags)))) 2195 goto nla_put_failure; 2196 else if (key->basic.ip_proto == IPPROTO_UDP && 2197 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC, 2198 &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK, 2199 sizeof(key->tp.src)) || 2200 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST, 2201 &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK, 2202 sizeof(key->tp.dst)))) 2203 goto nla_put_failure; 2204 else if (key->basic.ip_proto == IPPROTO_SCTP && 2205 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC, 2206 &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK, 2207 sizeof(key->tp.src)) || 2208 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST, 2209 &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK, 2210 sizeof(key->tp.dst)))) 2211 goto nla_put_failure; 2212 else if (key->basic.n_proto == htons(ETH_P_IP) && 2213 key->basic.ip_proto == IPPROTO_ICMP && 2214 (fl_dump_key_val(skb, &key->icmp.type, 2215 TCA_FLOWER_KEY_ICMPV4_TYPE, &mask->icmp.type, 2216 TCA_FLOWER_KEY_ICMPV4_TYPE_MASK, 2217 sizeof(key->icmp.type)) || 2218 fl_dump_key_val(skb, &key->icmp.code, 2219 TCA_FLOWER_KEY_ICMPV4_CODE, &mask->icmp.code, 2220 TCA_FLOWER_KEY_ICMPV4_CODE_MASK, 2221 sizeof(key->icmp.code)))) 2222 goto nla_put_failure; 2223 else if (key->basic.n_proto == htons(ETH_P_IPV6) && 2224 key->basic.ip_proto == IPPROTO_ICMPV6 && 2225 (fl_dump_key_val(skb, &key->icmp.type, 2226 TCA_FLOWER_KEY_ICMPV6_TYPE, &mask->icmp.type, 2227 TCA_FLOWER_KEY_ICMPV6_TYPE_MASK, 2228 sizeof(key->icmp.type)) || 2229 fl_dump_key_val(skb, &key->icmp.code, 2230 TCA_FLOWER_KEY_ICMPV6_CODE, &mask->icmp.code, 2231 TCA_FLOWER_KEY_ICMPV6_CODE_MASK, 2232 sizeof(key->icmp.code)))) 2233 goto nla_put_failure; 2234 else if ((key->basic.n_proto == htons(ETH_P_ARP) || 2235 key->basic.n_proto == htons(ETH_P_RARP)) && 2236 (fl_dump_key_val(skb, &key->arp.sip, 2237 TCA_FLOWER_KEY_ARP_SIP, &mask->arp.sip, 2238 TCA_FLOWER_KEY_ARP_SIP_MASK, 2239 sizeof(key->arp.sip)) || 2240 fl_dump_key_val(skb, &key->arp.tip, 2241 TCA_FLOWER_KEY_ARP_TIP, &mask->arp.tip, 2242 TCA_FLOWER_KEY_ARP_TIP_MASK, 2243 sizeof(key->arp.tip)) || 2244 fl_dump_key_val(skb, &key->arp.op, 2245 TCA_FLOWER_KEY_ARP_OP, &mask->arp.op, 2246 TCA_FLOWER_KEY_ARP_OP_MASK, 2247 sizeof(key->arp.op)) || 2248 fl_dump_key_val(skb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA, 2249 mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK, 2250 sizeof(key->arp.sha)) || 2251 fl_dump_key_val(skb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA, 2252 mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK, 2253 sizeof(key->arp.tha)))) 2254 goto nla_put_failure; 2255 2256 if ((key->basic.ip_proto == IPPROTO_TCP || 2257 key->basic.ip_proto == IPPROTO_UDP || 2258 key->basic.ip_proto == IPPROTO_SCTP) && 2259 fl_dump_key_port_range(skb, key, mask)) 2260 goto nla_put_failure; 2261 2262 if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS && 2263 (fl_dump_key_val(skb, &key->enc_ipv4.src, 2264 TCA_FLOWER_KEY_ENC_IPV4_SRC, &mask->enc_ipv4.src, 2265 TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK, 2266 sizeof(key->enc_ipv4.src)) || 2267 fl_dump_key_val(skb, &key->enc_ipv4.dst, 2268 TCA_FLOWER_KEY_ENC_IPV4_DST, &mask->enc_ipv4.dst, 2269 TCA_FLOWER_KEY_ENC_IPV4_DST_MASK, 2270 sizeof(key->enc_ipv4.dst)))) 2271 goto nla_put_failure; 2272 else if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS && 2273 (fl_dump_key_val(skb, &key->enc_ipv6.src, 2274 TCA_FLOWER_KEY_ENC_IPV6_SRC, &mask->enc_ipv6.src, 2275 TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK, 2276 sizeof(key->enc_ipv6.src)) || 2277 fl_dump_key_val(skb, &key->enc_ipv6.dst, 2278 TCA_FLOWER_KEY_ENC_IPV6_DST, 2279 &mask->enc_ipv6.dst, 2280 TCA_FLOWER_KEY_ENC_IPV6_DST_MASK, 2281 sizeof(key->enc_ipv6.dst)))) 2282 goto nla_put_failure; 2283 2284 if (fl_dump_key_val(skb, &key->enc_key_id, TCA_FLOWER_KEY_ENC_KEY_ID, 2285 &mask->enc_key_id, TCA_FLOWER_UNSPEC, 2286 sizeof(key->enc_key_id)) || 2287 fl_dump_key_val(skb, &key->enc_tp.src, 2288 TCA_FLOWER_KEY_ENC_UDP_SRC_PORT, 2289 &mask->enc_tp.src, 2290 TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK, 2291 sizeof(key->enc_tp.src)) || 2292 fl_dump_key_val(skb, &key->enc_tp.dst, 2293 TCA_FLOWER_KEY_ENC_UDP_DST_PORT, 2294 &mask->enc_tp.dst, 2295 TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK, 2296 sizeof(key->enc_tp.dst)) || 2297 fl_dump_key_ip(skb, true, &key->enc_ip, &mask->enc_ip) || 2298 fl_dump_key_enc_opt(skb, &key->enc_opts, &mask->enc_opts)) 2299 goto nla_put_failure; 2300 2301 if (fl_dump_key_flags(skb, key->control.flags, mask->control.flags)) 2302 goto nla_put_failure; 2303 2304 return 0; 2305 2306 nla_put_failure: 2307 return -EMSGSIZE; 2308 } 2309 2310 static int fl_dump(struct net *net, struct tcf_proto *tp, void *fh, 2311 struct sk_buff *skb, struct tcmsg *t, bool rtnl_held) 2312 { 2313 struct cls_fl_filter *f = fh; 2314 struct nlattr *nest; 2315 struct fl_flow_key *key, *mask; 2316 bool skip_hw; 2317 2318 if (!f) 2319 return skb->len; 2320 2321 t->tcm_handle = f->handle; 2322 2323 nest = nla_nest_start_noflag(skb, TCA_OPTIONS); 2324 if (!nest) 2325 goto nla_put_failure; 2326 2327 spin_lock(&tp->lock); 2328 2329 if (f->res.classid && 2330 nla_put_u32(skb, TCA_FLOWER_CLASSID, f->res.classid)) 2331 goto nla_put_failure_locked; 2332 2333 key = &f->key; 2334 mask = &f->mask->key; 2335 skip_hw = tc_skip_hw(f->flags); 2336 2337 if (fl_dump_key(skb, net, key, mask)) 2338 goto nla_put_failure_locked; 2339 2340 if (f->flags && nla_put_u32(skb, TCA_FLOWER_FLAGS, f->flags)) 2341 goto nla_put_failure_locked; 2342 2343 spin_unlock(&tp->lock); 2344 2345 if (!skip_hw) 2346 fl_hw_update_stats(tp, f, rtnl_held); 2347 2348 if (nla_put_u32(skb, TCA_FLOWER_IN_HW_COUNT, f->in_hw_count)) 2349 goto nla_put_failure; 2350 2351 if (tcf_exts_dump(skb, &f->exts)) 2352 goto nla_put_failure; 2353 2354 nla_nest_end(skb, nest); 2355 2356 if (tcf_exts_dump_stats(skb, &f->exts) < 0) 2357 goto nla_put_failure; 2358 2359 return skb->len; 2360 2361 nla_put_failure_locked: 2362 spin_unlock(&tp->lock); 2363 nla_put_failure: 2364 nla_nest_cancel(skb, nest); 2365 return -1; 2366 } 2367 2368 static int fl_tmplt_dump(struct sk_buff *skb, struct net *net, void *tmplt_priv) 2369 { 2370 struct fl_flow_tmplt *tmplt = tmplt_priv; 2371 struct fl_flow_key *key, *mask; 2372 struct nlattr *nest; 2373 2374 nest = nla_nest_start_noflag(skb, TCA_OPTIONS); 2375 if (!nest) 2376 goto nla_put_failure; 2377 2378 key = &tmplt->dummy_key; 2379 mask = &tmplt->mask; 2380 2381 if (fl_dump_key(skb, net, key, mask)) 2382 goto nla_put_failure; 2383 2384 nla_nest_end(skb, nest); 2385 2386 return skb->len; 2387 2388 nla_put_failure: 2389 nla_nest_cancel(skb, nest); 2390 return -EMSGSIZE; 2391 } 2392 2393 static void fl_bind_class(void *fh, u32 classid, unsigned long cl) 2394 { 2395 struct cls_fl_filter *f = fh; 2396 2397 if (f && f->res.classid == classid) 2398 f->res.class = cl; 2399 } 2400 2401 static struct tcf_proto_ops cls_fl_ops __read_mostly = { 2402 .kind = "flower", 2403 .classify = fl_classify, 2404 .init = fl_init, 2405 .destroy = fl_destroy, 2406 .get = fl_get, 2407 .put = fl_put, 2408 .change = fl_change, 2409 .delete = fl_delete, 2410 .walk = fl_walk, 2411 .reoffload = fl_reoffload, 2412 .dump = fl_dump, 2413 .bind_class = fl_bind_class, 2414 .tmplt_create = fl_tmplt_create, 2415 .tmplt_destroy = fl_tmplt_destroy, 2416 .tmplt_dump = fl_tmplt_dump, 2417 .owner = THIS_MODULE, 2418 .flags = TCF_PROTO_OPS_DOIT_UNLOCKED, 2419 }; 2420 2421 static int __init cls_fl_init(void) 2422 { 2423 return register_tcf_proto_ops(&cls_fl_ops); 2424 } 2425 2426 static void __exit cls_fl_exit(void) 2427 { 2428 unregister_tcf_proto_ops(&cls_fl_ops); 2429 } 2430 2431 module_init(cls_fl_init); 2432 module_exit(cls_fl_exit); 2433 2434 MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>"); 2435 MODULE_DESCRIPTION("Flower classifier"); 2436 MODULE_LICENSE("GPL v2"); 2437