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 #include <linux/ppp_defs.h> 20 21 #include <net/sch_generic.h> 22 #include <net/pkt_cls.h> 23 #include <net/pkt_sched.h> 24 #include <net/ip.h> 25 #include <net/flow_dissector.h> 26 #include <net/geneve.h> 27 #include <net/vxlan.h> 28 #include <net/erspan.h> 29 #include <net/gtp.h> 30 #include <net/tc_wrapper.h> 31 32 #include <net/dst.h> 33 #include <net/dst_metadata.h> 34 35 #include <uapi/linux/netfilter/nf_conntrack_common.h> 36 37 #define TCA_FLOWER_KEY_CT_FLAGS_MAX \ 38 ((__TCA_FLOWER_KEY_CT_FLAGS_MAX - 1) << 1) 39 #define TCA_FLOWER_KEY_CT_FLAGS_MASK \ 40 (TCA_FLOWER_KEY_CT_FLAGS_MAX - 1) 41 42 struct fl_flow_key { 43 struct flow_dissector_key_meta meta; 44 struct flow_dissector_key_control control; 45 struct flow_dissector_key_control enc_control; 46 struct flow_dissector_key_basic basic; 47 struct flow_dissector_key_eth_addrs eth; 48 struct flow_dissector_key_vlan vlan; 49 struct flow_dissector_key_vlan cvlan; 50 union { 51 struct flow_dissector_key_ipv4_addrs ipv4; 52 struct flow_dissector_key_ipv6_addrs ipv6; 53 }; 54 struct flow_dissector_key_ports tp; 55 struct flow_dissector_key_icmp icmp; 56 struct flow_dissector_key_arp arp; 57 struct flow_dissector_key_keyid enc_key_id; 58 union { 59 struct flow_dissector_key_ipv4_addrs enc_ipv4; 60 struct flow_dissector_key_ipv6_addrs enc_ipv6; 61 }; 62 struct flow_dissector_key_ports enc_tp; 63 struct flow_dissector_key_mpls mpls; 64 struct flow_dissector_key_tcp tcp; 65 struct flow_dissector_key_ip ip; 66 struct flow_dissector_key_ip enc_ip; 67 struct flow_dissector_key_enc_opts enc_opts; 68 struct flow_dissector_key_ports_range tp_range; 69 struct flow_dissector_key_ct ct; 70 struct flow_dissector_key_hash hash; 71 struct flow_dissector_key_num_of_vlans num_of_vlans; 72 struct flow_dissector_key_pppoe pppoe; 73 struct flow_dissector_key_l2tpv3 l2tpv3; 74 } __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */ 75 76 struct fl_flow_mask_range { 77 unsigned short int start; 78 unsigned short int end; 79 }; 80 81 struct fl_flow_mask { 82 struct fl_flow_key key; 83 struct fl_flow_mask_range range; 84 u32 flags; 85 struct rhash_head ht_node; 86 struct rhashtable ht; 87 struct rhashtable_params filter_ht_params; 88 struct flow_dissector dissector; 89 struct list_head filters; 90 struct rcu_work rwork; 91 struct list_head list; 92 refcount_t refcnt; 93 }; 94 95 struct fl_flow_tmplt { 96 struct fl_flow_key dummy_key; 97 struct fl_flow_key mask; 98 struct flow_dissector dissector; 99 struct tcf_chain *chain; 100 }; 101 102 struct cls_fl_head { 103 struct rhashtable ht; 104 spinlock_t masks_lock; /* Protect masks list */ 105 struct list_head masks; 106 struct list_head hw_filters; 107 struct rcu_work rwork; 108 struct idr handle_idr; 109 }; 110 111 struct cls_fl_filter { 112 struct fl_flow_mask *mask; 113 struct rhash_head ht_node; 114 struct fl_flow_key mkey; 115 struct tcf_exts exts; 116 struct tcf_result res; 117 struct fl_flow_key key; 118 struct list_head list; 119 struct list_head hw_list; 120 u32 handle; 121 u32 flags; 122 u32 in_hw_count; 123 u8 needs_tc_skb_ext:1; 124 struct rcu_work rwork; 125 struct net_device *hw_dev; 126 /* Flower classifier is unlocked, which means that its reference counter 127 * can be changed concurrently without any kind of external 128 * synchronization. Use atomic reference counter to be concurrency-safe. 129 */ 130 refcount_t refcnt; 131 bool deleted; 132 }; 133 134 static const struct rhashtable_params mask_ht_params = { 135 .key_offset = offsetof(struct fl_flow_mask, key), 136 .key_len = sizeof(struct fl_flow_key), 137 .head_offset = offsetof(struct fl_flow_mask, ht_node), 138 .automatic_shrinking = true, 139 }; 140 141 static unsigned short int fl_mask_range(const struct fl_flow_mask *mask) 142 { 143 return mask->range.end - mask->range.start; 144 } 145 146 static void fl_mask_update_range(struct fl_flow_mask *mask) 147 { 148 const u8 *bytes = (const u8 *) &mask->key; 149 size_t size = sizeof(mask->key); 150 size_t i, first = 0, last; 151 152 for (i = 0; i < size; i++) { 153 if (bytes[i]) { 154 first = i; 155 break; 156 } 157 } 158 last = first; 159 for (i = size - 1; i != first; i--) { 160 if (bytes[i]) { 161 last = i; 162 break; 163 } 164 } 165 mask->range.start = rounddown(first, sizeof(long)); 166 mask->range.end = roundup(last + 1, sizeof(long)); 167 } 168 169 static void *fl_key_get_start(struct fl_flow_key *key, 170 const struct fl_flow_mask *mask) 171 { 172 return (u8 *) key + mask->range.start; 173 } 174 175 static void fl_set_masked_key(struct fl_flow_key *mkey, struct fl_flow_key *key, 176 struct fl_flow_mask *mask) 177 { 178 const long *lkey = fl_key_get_start(key, mask); 179 const long *lmask = fl_key_get_start(&mask->key, mask); 180 long *lmkey = fl_key_get_start(mkey, mask); 181 int i; 182 183 for (i = 0; i < fl_mask_range(mask); i += sizeof(long)) 184 *lmkey++ = *lkey++ & *lmask++; 185 } 186 187 static bool fl_mask_fits_tmplt(struct fl_flow_tmplt *tmplt, 188 struct fl_flow_mask *mask) 189 { 190 const long *lmask = fl_key_get_start(&mask->key, mask); 191 const long *ltmplt; 192 int i; 193 194 if (!tmplt) 195 return true; 196 ltmplt = fl_key_get_start(&tmplt->mask, mask); 197 for (i = 0; i < fl_mask_range(mask); i += sizeof(long)) { 198 if (~*ltmplt++ & *lmask++) 199 return false; 200 } 201 return true; 202 } 203 204 static void fl_clear_masked_range(struct fl_flow_key *key, 205 struct fl_flow_mask *mask) 206 { 207 memset(fl_key_get_start(key, mask), 0, fl_mask_range(mask)); 208 } 209 210 static bool fl_range_port_dst_cmp(struct cls_fl_filter *filter, 211 struct fl_flow_key *key, 212 struct fl_flow_key *mkey) 213 { 214 u16 min_mask, max_mask, min_val, max_val; 215 216 min_mask = ntohs(filter->mask->key.tp_range.tp_min.dst); 217 max_mask = ntohs(filter->mask->key.tp_range.tp_max.dst); 218 min_val = ntohs(filter->key.tp_range.tp_min.dst); 219 max_val = ntohs(filter->key.tp_range.tp_max.dst); 220 221 if (min_mask && max_mask) { 222 if (ntohs(key->tp_range.tp.dst) < min_val || 223 ntohs(key->tp_range.tp.dst) > max_val) 224 return false; 225 226 /* skb does not have min and max values */ 227 mkey->tp_range.tp_min.dst = filter->mkey.tp_range.tp_min.dst; 228 mkey->tp_range.tp_max.dst = filter->mkey.tp_range.tp_max.dst; 229 } 230 return true; 231 } 232 233 static bool fl_range_port_src_cmp(struct cls_fl_filter *filter, 234 struct fl_flow_key *key, 235 struct fl_flow_key *mkey) 236 { 237 u16 min_mask, max_mask, min_val, max_val; 238 239 min_mask = ntohs(filter->mask->key.tp_range.tp_min.src); 240 max_mask = ntohs(filter->mask->key.tp_range.tp_max.src); 241 min_val = ntohs(filter->key.tp_range.tp_min.src); 242 max_val = ntohs(filter->key.tp_range.tp_max.src); 243 244 if (min_mask && max_mask) { 245 if (ntohs(key->tp_range.tp.src) < min_val || 246 ntohs(key->tp_range.tp.src) > max_val) 247 return false; 248 249 /* skb does not have min and max values */ 250 mkey->tp_range.tp_min.src = filter->mkey.tp_range.tp_min.src; 251 mkey->tp_range.tp_max.src = filter->mkey.tp_range.tp_max.src; 252 } 253 return true; 254 } 255 256 static struct cls_fl_filter *__fl_lookup(struct fl_flow_mask *mask, 257 struct fl_flow_key *mkey) 258 { 259 return rhashtable_lookup_fast(&mask->ht, fl_key_get_start(mkey, mask), 260 mask->filter_ht_params); 261 } 262 263 static struct cls_fl_filter *fl_lookup_range(struct fl_flow_mask *mask, 264 struct fl_flow_key *mkey, 265 struct fl_flow_key *key) 266 { 267 struct cls_fl_filter *filter, *f; 268 269 list_for_each_entry_rcu(filter, &mask->filters, list) { 270 if (!fl_range_port_dst_cmp(filter, key, mkey)) 271 continue; 272 273 if (!fl_range_port_src_cmp(filter, key, mkey)) 274 continue; 275 276 f = __fl_lookup(mask, mkey); 277 if (f) 278 return f; 279 } 280 return NULL; 281 } 282 283 static noinline_for_stack 284 struct cls_fl_filter *fl_mask_lookup(struct fl_flow_mask *mask, struct fl_flow_key *key) 285 { 286 struct fl_flow_key mkey; 287 288 fl_set_masked_key(&mkey, key, mask); 289 if ((mask->flags & TCA_FLOWER_MASK_FLAGS_RANGE)) 290 return fl_lookup_range(mask, &mkey, key); 291 292 return __fl_lookup(mask, &mkey); 293 } 294 295 static u16 fl_ct_info_to_flower_map[] = { 296 [IP_CT_ESTABLISHED] = TCA_FLOWER_KEY_CT_FLAGS_TRACKED | 297 TCA_FLOWER_KEY_CT_FLAGS_ESTABLISHED, 298 [IP_CT_RELATED] = TCA_FLOWER_KEY_CT_FLAGS_TRACKED | 299 TCA_FLOWER_KEY_CT_FLAGS_RELATED, 300 [IP_CT_ESTABLISHED_REPLY] = TCA_FLOWER_KEY_CT_FLAGS_TRACKED | 301 TCA_FLOWER_KEY_CT_FLAGS_ESTABLISHED | 302 TCA_FLOWER_KEY_CT_FLAGS_REPLY, 303 [IP_CT_RELATED_REPLY] = TCA_FLOWER_KEY_CT_FLAGS_TRACKED | 304 TCA_FLOWER_KEY_CT_FLAGS_RELATED | 305 TCA_FLOWER_KEY_CT_FLAGS_REPLY, 306 [IP_CT_NEW] = TCA_FLOWER_KEY_CT_FLAGS_TRACKED | 307 TCA_FLOWER_KEY_CT_FLAGS_NEW, 308 }; 309 310 TC_INDIRECT_SCOPE int fl_classify(struct sk_buff *skb, 311 const struct tcf_proto *tp, 312 struct tcf_result *res) 313 { 314 struct cls_fl_head *head = rcu_dereference_bh(tp->root); 315 bool post_ct = tc_skb_cb(skb)->post_ct; 316 u16 zone = tc_skb_cb(skb)->zone; 317 struct fl_flow_key skb_key; 318 struct fl_flow_mask *mask; 319 struct cls_fl_filter *f; 320 321 list_for_each_entry_rcu(mask, &head->masks, list) { 322 flow_dissector_init_keys(&skb_key.control, &skb_key.basic); 323 fl_clear_masked_range(&skb_key, mask); 324 325 skb_flow_dissect_meta(skb, &mask->dissector, &skb_key); 326 /* skb_flow_dissect() does not set n_proto in case an unknown 327 * protocol, so do it rather here. 328 */ 329 skb_key.basic.n_proto = skb_protocol(skb, false); 330 skb_flow_dissect_tunnel_info(skb, &mask->dissector, &skb_key); 331 skb_flow_dissect_ct(skb, &mask->dissector, &skb_key, 332 fl_ct_info_to_flower_map, 333 ARRAY_SIZE(fl_ct_info_to_flower_map), 334 post_ct, zone); 335 skb_flow_dissect_hash(skb, &mask->dissector, &skb_key); 336 skb_flow_dissect(skb, &mask->dissector, &skb_key, 337 FLOW_DISSECTOR_F_STOP_BEFORE_ENCAP); 338 339 f = fl_mask_lookup(mask, &skb_key); 340 if (f && !tc_skip_sw(f->flags)) { 341 *res = f->res; 342 return tcf_exts_exec(skb, &f->exts, res); 343 } 344 } 345 return -1; 346 } 347 348 static int fl_init(struct tcf_proto *tp) 349 { 350 struct cls_fl_head *head; 351 352 head = kzalloc(sizeof(*head), GFP_KERNEL); 353 if (!head) 354 return -ENOBUFS; 355 356 spin_lock_init(&head->masks_lock); 357 INIT_LIST_HEAD_RCU(&head->masks); 358 INIT_LIST_HEAD(&head->hw_filters); 359 rcu_assign_pointer(tp->root, head); 360 idr_init(&head->handle_idr); 361 362 return rhashtable_init(&head->ht, &mask_ht_params); 363 } 364 365 static void fl_mask_free(struct fl_flow_mask *mask, bool mask_init_done) 366 { 367 /* temporary masks don't have their filters list and ht initialized */ 368 if (mask_init_done) { 369 WARN_ON(!list_empty(&mask->filters)); 370 rhashtable_destroy(&mask->ht); 371 } 372 kfree(mask); 373 } 374 375 static void fl_mask_free_work(struct work_struct *work) 376 { 377 struct fl_flow_mask *mask = container_of(to_rcu_work(work), 378 struct fl_flow_mask, rwork); 379 380 fl_mask_free(mask, true); 381 } 382 383 static void fl_uninit_mask_free_work(struct work_struct *work) 384 { 385 struct fl_flow_mask *mask = container_of(to_rcu_work(work), 386 struct fl_flow_mask, rwork); 387 388 fl_mask_free(mask, false); 389 } 390 391 static bool fl_mask_put(struct cls_fl_head *head, struct fl_flow_mask *mask) 392 { 393 if (!refcount_dec_and_test(&mask->refcnt)) 394 return false; 395 396 rhashtable_remove_fast(&head->ht, &mask->ht_node, mask_ht_params); 397 398 spin_lock(&head->masks_lock); 399 list_del_rcu(&mask->list); 400 spin_unlock(&head->masks_lock); 401 402 tcf_queue_work(&mask->rwork, fl_mask_free_work); 403 404 return true; 405 } 406 407 static struct cls_fl_head *fl_head_dereference(struct tcf_proto *tp) 408 { 409 /* Flower classifier only changes root pointer during init and destroy. 410 * Users must obtain reference to tcf_proto instance before calling its 411 * API, so tp->root pointer is protected from concurrent call to 412 * fl_destroy() by reference counting. 413 */ 414 return rcu_dereference_raw(tp->root); 415 } 416 417 static void __fl_destroy_filter(struct cls_fl_filter *f) 418 { 419 if (f->needs_tc_skb_ext) 420 tc_skb_ext_tc_disable(); 421 tcf_exts_destroy(&f->exts); 422 tcf_exts_put_net(&f->exts); 423 kfree(f); 424 } 425 426 static void fl_destroy_filter_work(struct work_struct *work) 427 { 428 struct cls_fl_filter *f = container_of(to_rcu_work(work), 429 struct cls_fl_filter, rwork); 430 431 __fl_destroy_filter(f); 432 } 433 434 static void fl_hw_destroy_filter(struct tcf_proto *tp, struct cls_fl_filter *f, 435 bool rtnl_held, struct netlink_ext_ack *extack) 436 { 437 struct tcf_block *block = tp->chain->block; 438 struct flow_cls_offload cls_flower = {}; 439 440 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, extack); 441 cls_flower.command = FLOW_CLS_DESTROY; 442 cls_flower.cookie = (unsigned long) f; 443 444 tc_setup_cb_destroy(block, tp, TC_SETUP_CLSFLOWER, &cls_flower, false, 445 &f->flags, &f->in_hw_count, rtnl_held); 446 447 } 448 449 static int fl_hw_replace_filter(struct tcf_proto *tp, 450 struct cls_fl_filter *f, bool rtnl_held, 451 struct netlink_ext_ack *extack) 452 { 453 struct tcf_block *block = tp->chain->block; 454 struct flow_cls_offload cls_flower = {}; 455 bool skip_sw = tc_skip_sw(f->flags); 456 int err = 0; 457 458 cls_flower.rule = flow_rule_alloc(tcf_exts_num_actions(&f->exts)); 459 if (!cls_flower.rule) 460 return -ENOMEM; 461 462 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, extack); 463 cls_flower.command = FLOW_CLS_REPLACE; 464 cls_flower.cookie = (unsigned long) f; 465 cls_flower.rule->match.dissector = &f->mask->dissector; 466 cls_flower.rule->match.mask = &f->mask->key; 467 cls_flower.rule->match.key = &f->mkey; 468 cls_flower.classid = f->res.classid; 469 470 err = tc_setup_offload_action(&cls_flower.rule->action, &f->exts, 471 cls_flower.common.extack); 472 if (err) { 473 kfree(cls_flower.rule); 474 475 return skip_sw ? err : 0; 476 } 477 478 err = tc_setup_cb_add(block, tp, TC_SETUP_CLSFLOWER, &cls_flower, 479 skip_sw, &f->flags, &f->in_hw_count, rtnl_held); 480 tc_cleanup_offload_action(&cls_flower.rule->action); 481 kfree(cls_flower.rule); 482 483 if (err) { 484 fl_hw_destroy_filter(tp, f, rtnl_held, NULL); 485 return err; 486 } 487 488 if (skip_sw && !(f->flags & TCA_CLS_FLAGS_IN_HW)) 489 return -EINVAL; 490 491 return 0; 492 } 493 494 static void fl_hw_update_stats(struct tcf_proto *tp, struct cls_fl_filter *f, 495 bool rtnl_held) 496 { 497 struct tcf_block *block = tp->chain->block; 498 struct flow_cls_offload cls_flower = {}; 499 500 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, NULL); 501 cls_flower.command = FLOW_CLS_STATS; 502 cls_flower.cookie = (unsigned long) f; 503 cls_flower.classid = f->res.classid; 504 505 tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, false, 506 rtnl_held); 507 508 tcf_exts_hw_stats_update(&f->exts, &cls_flower.stats, cls_flower.use_act_stats); 509 } 510 511 static void __fl_put(struct cls_fl_filter *f) 512 { 513 if (!refcount_dec_and_test(&f->refcnt)) 514 return; 515 516 if (tcf_exts_get_net(&f->exts)) 517 tcf_queue_work(&f->rwork, fl_destroy_filter_work); 518 else 519 __fl_destroy_filter(f); 520 } 521 522 static struct cls_fl_filter *__fl_get(struct cls_fl_head *head, u32 handle) 523 { 524 struct cls_fl_filter *f; 525 526 rcu_read_lock(); 527 f = idr_find(&head->handle_idr, handle); 528 if (f && !refcount_inc_not_zero(&f->refcnt)) 529 f = NULL; 530 rcu_read_unlock(); 531 532 return f; 533 } 534 535 static struct tcf_exts *fl_get_exts(const struct tcf_proto *tp, u32 handle) 536 { 537 struct cls_fl_head *head = rcu_dereference_bh(tp->root); 538 struct cls_fl_filter *f; 539 540 f = idr_find(&head->handle_idr, handle); 541 return f ? &f->exts : NULL; 542 } 543 544 static int __fl_delete(struct tcf_proto *tp, struct cls_fl_filter *f, 545 bool *last, bool rtnl_held, 546 struct netlink_ext_ack *extack) 547 { 548 struct cls_fl_head *head = fl_head_dereference(tp); 549 550 *last = false; 551 552 spin_lock(&tp->lock); 553 if (f->deleted) { 554 spin_unlock(&tp->lock); 555 return -ENOENT; 556 } 557 558 f->deleted = true; 559 rhashtable_remove_fast(&f->mask->ht, &f->ht_node, 560 f->mask->filter_ht_params); 561 idr_remove(&head->handle_idr, f->handle); 562 list_del_rcu(&f->list); 563 spin_unlock(&tp->lock); 564 565 *last = fl_mask_put(head, f->mask); 566 if (!tc_skip_hw(f->flags)) 567 fl_hw_destroy_filter(tp, f, rtnl_held, extack); 568 tcf_unbind_filter(tp, &f->res); 569 __fl_put(f); 570 571 return 0; 572 } 573 574 static void fl_destroy_sleepable(struct work_struct *work) 575 { 576 struct cls_fl_head *head = container_of(to_rcu_work(work), 577 struct cls_fl_head, 578 rwork); 579 580 rhashtable_destroy(&head->ht); 581 kfree(head); 582 module_put(THIS_MODULE); 583 } 584 585 static void fl_destroy(struct tcf_proto *tp, bool rtnl_held, 586 struct netlink_ext_ack *extack) 587 { 588 struct cls_fl_head *head = fl_head_dereference(tp); 589 struct fl_flow_mask *mask, *next_mask; 590 struct cls_fl_filter *f, *next; 591 bool last; 592 593 list_for_each_entry_safe(mask, next_mask, &head->masks, list) { 594 list_for_each_entry_safe(f, next, &mask->filters, list) { 595 __fl_delete(tp, f, &last, rtnl_held, extack); 596 if (last) 597 break; 598 } 599 } 600 idr_destroy(&head->handle_idr); 601 602 __module_get(THIS_MODULE); 603 tcf_queue_work(&head->rwork, fl_destroy_sleepable); 604 } 605 606 static void fl_put(struct tcf_proto *tp, void *arg) 607 { 608 struct cls_fl_filter *f = arg; 609 610 __fl_put(f); 611 } 612 613 static void *fl_get(struct tcf_proto *tp, u32 handle) 614 { 615 struct cls_fl_head *head = fl_head_dereference(tp); 616 617 return __fl_get(head, handle); 618 } 619 620 static const struct nla_policy fl_policy[TCA_FLOWER_MAX + 1] = { 621 [TCA_FLOWER_UNSPEC] = { .strict_start_type = 622 TCA_FLOWER_L2_MISS }, 623 [TCA_FLOWER_CLASSID] = { .type = NLA_U32 }, 624 [TCA_FLOWER_INDEV] = { .type = NLA_STRING, 625 .len = IFNAMSIZ }, 626 [TCA_FLOWER_KEY_ETH_DST] = { .len = ETH_ALEN }, 627 [TCA_FLOWER_KEY_ETH_DST_MASK] = { .len = ETH_ALEN }, 628 [TCA_FLOWER_KEY_ETH_SRC] = { .len = ETH_ALEN }, 629 [TCA_FLOWER_KEY_ETH_SRC_MASK] = { .len = ETH_ALEN }, 630 [TCA_FLOWER_KEY_ETH_TYPE] = { .type = NLA_U16 }, 631 [TCA_FLOWER_KEY_IP_PROTO] = { .type = NLA_U8 }, 632 [TCA_FLOWER_KEY_IPV4_SRC] = { .type = NLA_U32 }, 633 [TCA_FLOWER_KEY_IPV4_SRC_MASK] = { .type = NLA_U32 }, 634 [TCA_FLOWER_KEY_IPV4_DST] = { .type = NLA_U32 }, 635 [TCA_FLOWER_KEY_IPV4_DST_MASK] = { .type = NLA_U32 }, 636 [TCA_FLOWER_KEY_IPV6_SRC] = { .len = sizeof(struct in6_addr) }, 637 [TCA_FLOWER_KEY_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) }, 638 [TCA_FLOWER_KEY_IPV6_DST] = { .len = sizeof(struct in6_addr) }, 639 [TCA_FLOWER_KEY_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) }, 640 [TCA_FLOWER_KEY_TCP_SRC] = { .type = NLA_U16 }, 641 [TCA_FLOWER_KEY_TCP_DST] = { .type = NLA_U16 }, 642 [TCA_FLOWER_KEY_UDP_SRC] = { .type = NLA_U16 }, 643 [TCA_FLOWER_KEY_UDP_DST] = { .type = NLA_U16 }, 644 [TCA_FLOWER_KEY_VLAN_ID] = { .type = NLA_U16 }, 645 [TCA_FLOWER_KEY_VLAN_PRIO] = { .type = NLA_U8 }, 646 [TCA_FLOWER_KEY_VLAN_ETH_TYPE] = { .type = NLA_U16 }, 647 [TCA_FLOWER_KEY_ENC_KEY_ID] = { .type = NLA_U32 }, 648 [TCA_FLOWER_KEY_ENC_IPV4_SRC] = { .type = NLA_U32 }, 649 [TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK] = { .type = NLA_U32 }, 650 [TCA_FLOWER_KEY_ENC_IPV4_DST] = { .type = NLA_U32 }, 651 [TCA_FLOWER_KEY_ENC_IPV4_DST_MASK] = { .type = NLA_U32 }, 652 [TCA_FLOWER_KEY_ENC_IPV6_SRC] = { .len = sizeof(struct in6_addr) }, 653 [TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) }, 654 [TCA_FLOWER_KEY_ENC_IPV6_DST] = { .len = sizeof(struct in6_addr) }, 655 [TCA_FLOWER_KEY_ENC_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) }, 656 [TCA_FLOWER_KEY_TCP_SRC_MASK] = { .type = NLA_U16 }, 657 [TCA_FLOWER_KEY_TCP_DST_MASK] = { .type = NLA_U16 }, 658 [TCA_FLOWER_KEY_UDP_SRC_MASK] = { .type = NLA_U16 }, 659 [TCA_FLOWER_KEY_UDP_DST_MASK] = { .type = NLA_U16 }, 660 [TCA_FLOWER_KEY_SCTP_SRC_MASK] = { .type = NLA_U16 }, 661 [TCA_FLOWER_KEY_SCTP_DST_MASK] = { .type = NLA_U16 }, 662 [TCA_FLOWER_KEY_SCTP_SRC] = { .type = NLA_U16 }, 663 [TCA_FLOWER_KEY_SCTP_DST] = { .type = NLA_U16 }, 664 [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT] = { .type = NLA_U16 }, 665 [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK] = { .type = NLA_U16 }, 666 [TCA_FLOWER_KEY_ENC_UDP_DST_PORT] = { .type = NLA_U16 }, 667 [TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK] = { .type = NLA_U16 }, 668 [TCA_FLOWER_KEY_FLAGS] = { .type = NLA_U32 }, 669 [TCA_FLOWER_KEY_FLAGS_MASK] = { .type = NLA_U32 }, 670 [TCA_FLOWER_KEY_ICMPV4_TYPE] = { .type = NLA_U8 }, 671 [TCA_FLOWER_KEY_ICMPV4_TYPE_MASK] = { .type = NLA_U8 }, 672 [TCA_FLOWER_KEY_ICMPV4_CODE] = { .type = NLA_U8 }, 673 [TCA_FLOWER_KEY_ICMPV4_CODE_MASK] = { .type = NLA_U8 }, 674 [TCA_FLOWER_KEY_ICMPV6_TYPE] = { .type = NLA_U8 }, 675 [TCA_FLOWER_KEY_ICMPV6_TYPE_MASK] = { .type = NLA_U8 }, 676 [TCA_FLOWER_KEY_ICMPV6_CODE] = { .type = NLA_U8 }, 677 [TCA_FLOWER_KEY_ICMPV6_CODE_MASK] = { .type = NLA_U8 }, 678 [TCA_FLOWER_KEY_ARP_SIP] = { .type = NLA_U32 }, 679 [TCA_FLOWER_KEY_ARP_SIP_MASK] = { .type = NLA_U32 }, 680 [TCA_FLOWER_KEY_ARP_TIP] = { .type = NLA_U32 }, 681 [TCA_FLOWER_KEY_ARP_TIP_MASK] = { .type = NLA_U32 }, 682 [TCA_FLOWER_KEY_ARP_OP] = { .type = NLA_U8 }, 683 [TCA_FLOWER_KEY_ARP_OP_MASK] = { .type = NLA_U8 }, 684 [TCA_FLOWER_KEY_ARP_SHA] = { .len = ETH_ALEN }, 685 [TCA_FLOWER_KEY_ARP_SHA_MASK] = { .len = ETH_ALEN }, 686 [TCA_FLOWER_KEY_ARP_THA] = { .len = ETH_ALEN }, 687 [TCA_FLOWER_KEY_ARP_THA_MASK] = { .len = ETH_ALEN }, 688 [TCA_FLOWER_KEY_MPLS_TTL] = { .type = NLA_U8 }, 689 [TCA_FLOWER_KEY_MPLS_BOS] = { .type = NLA_U8 }, 690 [TCA_FLOWER_KEY_MPLS_TC] = { .type = NLA_U8 }, 691 [TCA_FLOWER_KEY_MPLS_LABEL] = { .type = NLA_U32 }, 692 [TCA_FLOWER_KEY_MPLS_OPTS] = { .type = NLA_NESTED }, 693 [TCA_FLOWER_KEY_TCP_FLAGS] = { .type = NLA_U16 }, 694 [TCA_FLOWER_KEY_TCP_FLAGS_MASK] = { .type = NLA_U16 }, 695 [TCA_FLOWER_KEY_IP_TOS] = { .type = NLA_U8 }, 696 [TCA_FLOWER_KEY_IP_TOS_MASK] = { .type = NLA_U8 }, 697 [TCA_FLOWER_KEY_IP_TTL] = { .type = NLA_U8 }, 698 [TCA_FLOWER_KEY_IP_TTL_MASK] = { .type = NLA_U8 }, 699 [TCA_FLOWER_KEY_CVLAN_ID] = { .type = NLA_U16 }, 700 [TCA_FLOWER_KEY_CVLAN_PRIO] = { .type = NLA_U8 }, 701 [TCA_FLOWER_KEY_CVLAN_ETH_TYPE] = { .type = NLA_U16 }, 702 [TCA_FLOWER_KEY_ENC_IP_TOS] = { .type = NLA_U8 }, 703 [TCA_FLOWER_KEY_ENC_IP_TOS_MASK] = { .type = NLA_U8 }, 704 [TCA_FLOWER_KEY_ENC_IP_TTL] = { .type = NLA_U8 }, 705 [TCA_FLOWER_KEY_ENC_IP_TTL_MASK] = { .type = NLA_U8 }, 706 [TCA_FLOWER_KEY_ENC_OPTS] = { .type = NLA_NESTED }, 707 [TCA_FLOWER_KEY_ENC_OPTS_MASK] = { .type = NLA_NESTED }, 708 [TCA_FLOWER_KEY_CT_STATE] = 709 NLA_POLICY_MASK(NLA_U16, TCA_FLOWER_KEY_CT_FLAGS_MASK), 710 [TCA_FLOWER_KEY_CT_STATE_MASK] = 711 NLA_POLICY_MASK(NLA_U16, TCA_FLOWER_KEY_CT_FLAGS_MASK), 712 [TCA_FLOWER_KEY_CT_ZONE] = { .type = NLA_U16 }, 713 [TCA_FLOWER_KEY_CT_ZONE_MASK] = { .type = NLA_U16 }, 714 [TCA_FLOWER_KEY_CT_MARK] = { .type = NLA_U32 }, 715 [TCA_FLOWER_KEY_CT_MARK_MASK] = { .type = NLA_U32 }, 716 [TCA_FLOWER_KEY_CT_LABELS] = { .type = NLA_BINARY, 717 .len = 128 / BITS_PER_BYTE }, 718 [TCA_FLOWER_KEY_CT_LABELS_MASK] = { .type = NLA_BINARY, 719 .len = 128 / BITS_PER_BYTE }, 720 [TCA_FLOWER_FLAGS] = { .type = NLA_U32 }, 721 [TCA_FLOWER_KEY_HASH] = { .type = NLA_U32 }, 722 [TCA_FLOWER_KEY_HASH_MASK] = { .type = NLA_U32 }, 723 [TCA_FLOWER_KEY_NUM_OF_VLANS] = { .type = NLA_U8 }, 724 [TCA_FLOWER_KEY_PPPOE_SID] = { .type = NLA_U16 }, 725 [TCA_FLOWER_KEY_PPP_PROTO] = { .type = NLA_U16 }, 726 [TCA_FLOWER_KEY_L2TPV3_SID] = { .type = NLA_U32 }, 727 [TCA_FLOWER_L2_MISS] = NLA_POLICY_MAX(NLA_U8, 1), 728 }; 729 730 static const struct nla_policy 731 enc_opts_policy[TCA_FLOWER_KEY_ENC_OPTS_MAX + 1] = { 732 [TCA_FLOWER_KEY_ENC_OPTS_UNSPEC] = { 733 .strict_start_type = TCA_FLOWER_KEY_ENC_OPTS_VXLAN }, 734 [TCA_FLOWER_KEY_ENC_OPTS_GENEVE] = { .type = NLA_NESTED }, 735 [TCA_FLOWER_KEY_ENC_OPTS_VXLAN] = { .type = NLA_NESTED }, 736 [TCA_FLOWER_KEY_ENC_OPTS_ERSPAN] = { .type = NLA_NESTED }, 737 [TCA_FLOWER_KEY_ENC_OPTS_GTP] = { .type = NLA_NESTED }, 738 }; 739 740 static const struct nla_policy 741 geneve_opt_policy[TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX + 1] = { 742 [TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS] = { .type = NLA_U16 }, 743 [TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE] = { .type = NLA_U8 }, 744 [TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA] = { .type = NLA_BINARY, 745 .len = 128 }, 746 }; 747 748 static const struct nla_policy 749 vxlan_opt_policy[TCA_FLOWER_KEY_ENC_OPT_VXLAN_MAX + 1] = { 750 [TCA_FLOWER_KEY_ENC_OPT_VXLAN_GBP] = { .type = NLA_U32 }, 751 }; 752 753 static const struct nla_policy 754 erspan_opt_policy[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_MAX + 1] = { 755 [TCA_FLOWER_KEY_ENC_OPT_ERSPAN_VER] = { .type = NLA_U8 }, 756 [TCA_FLOWER_KEY_ENC_OPT_ERSPAN_INDEX] = { .type = NLA_U32 }, 757 [TCA_FLOWER_KEY_ENC_OPT_ERSPAN_DIR] = { .type = NLA_U8 }, 758 [TCA_FLOWER_KEY_ENC_OPT_ERSPAN_HWID] = { .type = NLA_U8 }, 759 }; 760 761 static const struct nla_policy 762 gtp_opt_policy[TCA_FLOWER_KEY_ENC_OPT_GTP_MAX + 1] = { 763 [TCA_FLOWER_KEY_ENC_OPT_GTP_PDU_TYPE] = { .type = NLA_U8 }, 764 [TCA_FLOWER_KEY_ENC_OPT_GTP_QFI] = { .type = NLA_U8 }, 765 }; 766 767 static const struct nla_policy 768 mpls_stack_entry_policy[TCA_FLOWER_KEY_MPLS_OPT_LSE_MAX + 1] = { 769 [TCA_FLOWER_KEY_MPLS_OPT_LSE_DEPTH] = { .type = NLA_U8 }, 770 [TCA_FLOWER_KEY_MPLS_OPT_LSE_TTL] = { .type = NLA_U8 }, 771 [TCA_FLOWER_KEY_MPLS_OPT_LSE_BOS] = { .type = NLA_U8 }, 772 [TCA_FLOWER_KEY_MPLS_OPT_LSE_TC] = { .type = NLA_U8 }, 773 [TCA_FLOWER_KEY_MPLS_OPT_LSE_LABEL] = { .type = NLA_U32 }, 774 }; 775 776 static void fl_set_key_val(struct nlattr **tb, 777 void *val, int val_type, 778 void *mask, int mask_type, int len) 779 { 780 if (!tb[val_type]) 781 return; 782 nla_memcpy(val, tb[val_type], len); 783 if (mask_type == TCA_FLOWER_UNSPEC || !tb[mask_type]) 784 memset(mask, 0xff, len); 785 else 786 nla_memcpy(mask, tb[mask_type], len); 787 } 788 789 static int fl_set_key_port_range(struct nlattr **tb, struct fl_flow_key *key, 790 struct fl_flow_key *mask, 791 struct netlink_ext_ack *extack) 792 { 793 fl_set_key_val(tb, &key->tp_range.tp_min.dst, 794 TCA_FLOWER_KEY_PORT_DST_MIN, &mask->tp_range.tp_min.dst, 795 TCA_FLOWER_UNSPEC, sizeof(key->tp_range.tp_min.dst)); 796 fl_set_key_val(tb, &key->tp_range.tp_max.dst, 797 TCA_FLOWER_KEY_PORT_DST_MAX, &mask->tp_range.tp_max.dst, 798 TCA_FLOWER_UNSPEC, sizeof(key->tp_range.tp_max.dst)); 799 fl_set_key_val(tb, &key->tp_range.tp_min.src, 800 TCA_FLOWER_KEY_PORT_SRC_MIN, &mask->tp_range.tp_min.src, 801 TCA_FLOWER_UNSPEC, sizeof(key->tp_range.tp_min.src)); 802 fl_set_key_val(tb, &key->tp_range.tp_max.src, 803 TCA_FLOWER_KEY_PORT_SRC_MAX, &mask->tp_range.tp_max.src, 804 TCA_FLOWER_UNSPEC, sizeof(key->tp_range.tp_max.src)); 805 806 if (mask->tp_range.tp_min.dst && mask->tp_range.tp_max.dst && 807 ntohs(key->tp_range.tp_max.dst) <= 808 ntohs(key->tp_range.tp_min.dst)) { 809 NL_SET_ERR_MSG_ATTR(extack, 810 tb[TCA_FLOWER_KEY_PORT_DST_MIN], 811 "Invalid destination port range (min must be strictly smaller than max)"); 812 return -EINVAL; 813 } 814 if (mask->tp_range.tp_min.src && mask->tp_range.tp_max.src && 815 ntohs(key->tp_range.tp_max.src) <= 816 ntohs(key->tp_range.tp_min.src)) { 817 NL_SET_ERR_MSG_ATTR(extack, 818 tb[TCA_FLOWER_KEY_PORT_SRC_MIN], 819 "Invalid source port range (min must be strictly smaller than max)"); 820 return -EINVAL; 821 } 822 823 return 0; 824 } 825 826 static int fl_set_key_mpls_lse(const struct nlattr *nla_lse, 827 struct flow_dissector_key_mpls *key_val, 828 struct flow_dissector_key_mpls *key_mask, 829 struct netlink_ext_ack *extack) 830 { 831 struct nlattr *tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_MAX + 1]; 832 struct flow_dissector_mpls_lse *lse_mask; 833 struct flow_dissector_mpls_lse *lse_val; 834 u8 lse_index; 835 u8 depth; 836 int err; 837 838 err = nla_parse_nested(tb, TCA_FLOWER_KEY_MPLS_OPT_LSE_MAX, nla_lse, 839 mpls_stack_entry_policy, extack); 840 if (err < 0) 841 return err; 842 843 if (!tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_DEPTH]) { 844 NL_SET_ERR_MSG(extack, "Missing MPLS option \"depth\""); 845 return -EINVAL; 846 } 847 848 depth = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_DEPTH]); 849 850 /* LSE depth starts at 1, for consistency with terminology used by 851 * RFC 3031 (section 3.9), where depth 0 refers to unlabeled packets. 852 */ 853 if (depth < 1 || depth > FLOW_DIS_MPLS_MAX) { 854 NL_SET_ERR_MSG_ATTR(extack, 855 tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_DEPTH], 856 "Invalid MPLS depth"); 857 return -EINVAL; 858 } 859 lse_index = depth - 1; 860 861 dissector_set_mpls_lse(key_val, lse_index); 862 dissector_set_mpls_lse(key_mask, lse_index); 863 864 lse_val = &key_val->ls[lse_index]; 865 lse_mask = &key_mask->ls[lse_index]; 866 867 if (tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_TTL]) { 868 lse_val->mpls_ttl = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_TTL]); 869 lse_mask->mpls_ttl = MPLS_TTL_MASK; 870 } 871 if (tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_BOS]) { 872 u8 bos = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_BOS]); 873 874 if (bos & ~MPLS_BOS_MASK) { 875 NL_SET_ERR_MSG_ATTR(extack, 876 tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_BOS], 877 "Bottom Of Stack (BOS) must be 0 or 1"); 878 return -EINVAL; 879 } 880 lse_val->mpls_bos = bos; 881 lse_mask->mpls_bos = MPLS_BOS_MASK; 882 } 883 if (tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_TC]) { 884 u8 tc = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_TC]); 885 886 if (tc & ~MPLS_TC_MASK) { 887 NL_SET_ERR_MSG_ATTR(extack, 888 tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_TC], 889 "Traffic Class (TC) must be between 0 and 7"); 890 return -EINVAL; 891 } 892 lse_val->mpls_tc = tc; 893 lse_mask->mpls_tc = MPLS_TC_MASK; 894 } 895 if (tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_LABEL]) { 896 u32 label = nla_get_u32(tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_LABEL]); 897 898 if (label & ~MPLS_LABEL_MASK) { 899 NL_SET_ERR_MSG_ATTR(extack, 900 tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_LABEL], 901 "Label must be between 0 and 1048575"); 902 return -EINVAL; 903 } 904 lse_val->mpls_label = label; 905 lse_mask->mpls_label = MPLS_LABEL_MASK; 906 } 907 908 return 0; 909 } 910 911 static int fl_set_key_mpls_opts(const struct nlattr *nla_mpls_opts, 912 struct flow_dissector_key_mpls *key_val, 913 struct flow_dissector_key_mpls *key_mask, 914 struct netlink_ext_ack *extack) 915 { 916 struct nlattr *nla_lse; 917 int rem; 918 int err; 919 920 if (!(nla_mpls_opts->nla_type & NLA_F_NESTED)) { 921 NL_SET_ERR_MSG_ATTR(extack, nla_mpls_opts, 922 "NLA_F_NESTED is missing"); 923 return -EINVAL; 924 } 925 926 nla_for_each_nested(nla_lse, nla_mpls_opts, rem) { 927 if (nla_type(nla_lse) != TCA_FLOWER_KEY_MPLS_OPTS_LSE) { 928 NL_SET_ERR_MSG_ATTR(extack, nla_lse, 929 "Invalid MPLS option type"); 930 return -EINVAL; 931 } 932 933 err = fl_set_key_mpls_lse(nla_lse, key_val, key_mask, extack); 934 if (err < 0) 935 return err; 936 } 937 if (rem) { 938 NL_SET_ERR_MSG(extack, 939 "Bytes leftover after parsing MPLS options"); 940 return -EINVAL; 941 } 942 943 return 0; 944 } 945 946 static int fl_set_key_mpls(struct nlattr **tb, 947 struct flow_dissector_key_mpls *key_val, 948 struct flow_dissector_key_mpls *key_mask, 949 struct netlink_ext_ack *extack) 950 { 951 struct flow_dissector_mpls_lse *lse_mask; 952 struct flow_dissector_mpls_lse *lse_val; 953 954 if (tb[TCA_FLOWER_KEY_MPLS_OPTS]) { 955 if (tb[TCA_FLOWER_KEY_MPLS_TTL] || 956 tb[TCA_FLOWER_KEY_MPLS_BOS] || 957 tb[TCA_FLOWER_KEY_MPLS_TC] || 958 tb[TCA_FLOWER_KEY_MPLS_LABEL]) { 959 NL_SET_ERR_MSG_ATTR(extack, 960 tb[TCA_FLOWER_KEY_MPLS_OPTS], 961 "MPLS label, Traffic Class, Bottom Of Stack and Time To Live must be encapsulated in the MPLS options attribute"); 962 return -EBADMSG; 963 } 964 965 return fl_set_key_mpls_opts(tb[TCA_FLOWER_KEY_MPLS_OPTS], 966 key_val, key_mask, extack); 967 } 968 969 lse_val = &key_val->ls[0]; 970 lse_mask = &key_mask->ls[0]; 971 972 if (tb[TCA_FLOWER_KEY_MPLS_TTL]) { 973 lse_val->mpls_ttl = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TTL]); 974 lse_mask->mpls_ttl = MPLS_TTL_MASK; 975 dissector_set_mpls_lse(key_val, 0); 976 dissector_set_mpls_lse(key_mask, 0); 977 } 978 if (tb[TCA_FLOWER_KEY_MPLS_BOS]) { 979 u8 bos = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_BOS]); 980 981 if (bos & ~MPLS_BOS_MASK) { 982 NL_SET_ERR_MSG_ATTR(extack, 983 tb[TCA_FLOWER_KEY_MPLS_BOS], 984 "Bottom Of Stack (BOS) must be 0 or 1"); 985 return -EINVAL; 986 } 987 lse_val->mpls_bos = bos; 988 lse_mask->mpls_bos = MPLS_BOS_MASK; 989 dissector_set_mpls_lse(key_val, 0); 990 dissector_set_mpls_lse(key_mask, 0); 991 } 992 if (tb[TCA_FLOWER_KEY_MPLS_TC]) { 993 u8 tc = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TC]); 994 995 if (tc & ~MPLS_TC_MASK) { 996 NL_SET_ERR_MSG_ATTR(extack, 997 tb[TCA_FLOWER_KEY_MPLS_TC], 998 "Traffic Class (TC) must be between 0 and 7"); 999 return -EINVAL; 1000 } 1001 lse_val->mpls_tc = tc; 1002 lse_mask->mpls_tc = MPLS_TC_MASK; 1003 dissector_set_mpls_lse(key_val, 0); 1004 dissector_set_mpls_lse(key_mask, 0); 1005 } 1006 if (tb[TCA_FLOWER_KEY_MPLS_LABEL]) { 1007 u32 label = nla_get_u32(tb[TCA_FLOWER_KEY_MPLS_LABEL]); 1008 1009 if (label & ~MPLS_LABEL_MASK) { 1010 NL_SET_ERR_MSG_ATTR(extack, 1011 tb[TCA_FLOWER_KEY_MPLS_LABEL], 1012 "Label must be between 0 and 1048575"); 1013 return -EINVAL; 1014 } 1015 lse_val->mpls_label = label; 1016 lse_mask->mpls_label = MPLS_LABEL_MASK; 1017 dissector_set_mpls_lse(key_val, 0); 1018 dissector_set_mpls_lse(key_mask, 0); 1019 } 1020 return 0; 1021 } 1022 1023 static void fl_set_key_vlan(struct nlattr **tb, 1024 __be16 ethertype, 1025 int vlan_id_key, int vlan_prio_key, 1026 int vlan_next_eth_type_key, 1027 struct flow_dissector_key_vlan *key_val, 1028 struct flow_dissector_key_vlan *key_mask) 1029 { 1030 #define VLAN_PRIORITY_MASK 0x7 1031 1032 if (tb[vlan_id_key]) { 1033 key_val->vlan_id = 1034 nla_get_u16(tb[vlan_id_key]) & VLAN_VID_MASK; 1035 key_mask->vlan_id = VLAN_VID_MASK; 1036 } 1037 if (tb[vlan_prio_key]) { 1038 key_val->vlan_priority = 1039 nla_get_u8(tb[vlan_prio_key]) & 1040 VLAN_PRIORITY_MASK; 1041 key_mask->vlan_priority = VLAN_PRIORITY_MASK; 1042 } 1043 if (ethertype) { 1044 key_val->vlan_tpid = ethertype; 1045 key_mask->vlan_tpid = cpu_to_be16(~0); 1046 } 1047 if (tb[vlan_next_eth_type_key]) { 1048 key_val->vlan_eth_type = 1049 nla_get_be16(tb[vlan_next_eth_type_key]); 1050 key_mask->vlan_eth_type = cpu_to_be16(~0); 1051 } 1052 } 1053 1054 static void fl_set_key_pppoe(struct nlattr **tb, 1055 struct flow_dissector_key_pppoe *key_val, 1056 struct flow_dissector_key_pppoe *key_mask, 1057 struct fl_flow_key *key, 1058 struct fl_flow_key *mask) 1059 { 1060 /* key_val::type must be set to ETH_P_PPP_SES 1061 * because ETH_P_PPP_SES was stored in basic.n_proto 1062 * which might get overwritten by ppp_proto 1063 * or might be set to 0, the role of key_val::type 1064 * is similar to vlan_key::tpid 1065 */ 1066 key_val->type = htons(ETH_P_PPP_SES); 1067 key_mask->type = cpu_to_be16(~0); 1068 1069 if (tb[TCA_FLOWER_KEY_PPPOE_SID]) { 1070 key_val->session_id = 1071 nla_get_be16(tb[TCA_FLOWER_KEY_PPPOE_SID]); 1072 key_mask->session_id = cpu_to_be16(~0); 1073 } 1074 if (tb[TCA_FLOWER_KEY_PPP_PROTO]) { 1075 key_val->ppp_proto = 1076 nla_get_be16(tb[TCA_FLOWER_KEY_PPP_PROTO]); 1077 key_mask->ppp_proto = cpu_to_be16(~0); 1078 1079 if (key_val->ppp_proto == htons(PPP_IP)) { 1080 key->basic.n_proto = htons(ETH_P_IP); 1081 mask->basic.n_proto = cpu_to_be16(~0); 1082 } else if (key_val->ppp_proto == htons(PPP_IPV6)) { 1083 key->basic.n_proto = htons(ETH_P_IPV6); 1084 mask->basic.n_proto = cpu_to_be16(~0); 1085 } else if (key_val->ppp_proto == htons(PPP_MPLS_UC)) { 1086 key->basic.n_proto = htons(ETH_P_MPLS_UC); 1087 mask->basic.n_proto = cpu_to_be16(~0); 1088 } else if (key_val->ppp_proto == htons(PPP_MPLS_MC)) { 1089 key->basic.n_proto = htons(ETH_P_MPLS_MC); 1090 mask->basic.n_proto = cpu_to_be16(~0); 1091 } 1092 } else { 1093 key->basic.n_proto = 0; 1094 mask->basic.n_proto = cpu_to_be16(0); 1095 } 1096 } 1097 1098 static void fl_set_key_flag(u32 flower_key, u32 flower_mask, 1099 u32 *dissector_key, u32 *dissector_mask, 1100 u32 flower_flag_bit, u32 dissector_flag_bit) 1101 { 1102 if (flower_mask & flower_flag_bit) { 1103 *dissector_mask |= dissector_flag_bit; 1104 if (flower_key & flower_flag_bit) 1105 *dissector_key |= dissector_flag_bit; 1106 } 1107 } 1108 1109 static int fl_set_key_flags(struct nlattr **tb, u32 *flags_key, 1110 u32 *flags_mask, struct netlink_ext_ack *extack) 1111 { 1112 u32 key, mask; 1113 1114 /* mask is mandatory for flags */ 1115 if (!tb[TCA_FLOWER_KEY_FLAGS_MASK]) { 1116 NL_SET_ERR_MSG(extack, "Missing flags mask"); 1117 return -EINVAL; 1118 } 1119 1120 key = be32_to_cpu(nla_get_be32(tb[TCA_FLOWER_KEY_FLAGS])); 1121 mask = be32_to_cpu(nla_get_be32(tb[TCA_FLOWER_KEY_FLAGS_MASK])); 1122 1123 *flags_key = 0; 1124 *flags_mask = 0; 1125 1126 fl_set_key_flag(key, mask, flags_key, flags_mask, 1127 TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT); 1128 fl_set_key_flag(key, mask, flags_key, flags_mask, 1129 TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST, 1130 FLOW_DIS_FIRST_FRAG); 1131 1132 return 0; 1133 } 1134 1135 static void fl_set_key_ip(struct nlattr **tb, bool encap, 1136 struct flow_dissector_key_ip *key, 1137 struct flow_dissector_key_ip *mask) 1138 { 1139 int tos_key = encap ? TCA_FLOWER_KEY_ENC_IP_TOS : TCA_FLOWER_KEY_IP_TOS; 1140 int ttl_key = encap ? TCA_FLOWER_KEY_ENC_IP_TTL : TCA_FLOWER_KEY_IP_TTL; 1141 int tos_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TOS_MASK : TCA_FLOWER_KEY_IP_TOS_MASK; 1142 int ttl_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TTL_MASK : TCA_FLOWER_KEY_IP_TTL_MASK; 1143 1144 fl_set_key_val(tb, &key->tos, tos_key, &mask->tos, tos_mask, sizeof(key->tos)); 1145 fl_set_key_val(tb, &key->ttl, ttl_key, &mask->ttl, ttl_mask, sizeof(key->ttl)); 1146 } 1147 1148 static int fl_set_geneve_opt(const struct nlattr *nla, struct fl_flow_key *key, 1149 int depth, int option_len, 1150 struct netlink_ext_ack *extack) 1151 { 1152 struct nlattr *tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX + 1]; 1153 struct nlattr *class = NULL, *type = NULL, *data = NULL; 1154 struct geneve_opt *opt; 1155 int err, data_len = 0; 1156 1157 if (option_len > sizeof(struct geneve_opt)) 1158 data_len = option_len - sizeof(struct geneve_opt); 1159 1160 opt = (struct geneve_opt *)&key->enc_opts.data[key->enc_opts.len]; 1161 memset(opt, 0xff, option_len); 1162 opt->length = data_len / 4; 1163 opt->r1 = 0; 1164 opt->r2 = 0; 1165 opt->r3 = 0; 1166 1167 /* If no mask has been prodived we assume an exact match. */ 1168 if (!depth) 1169 return sizeof(struct geneve_opt) + data_len; 1170 1171 if (nla_type(nla) != TCA_FLOWER_KEY_ENC_OPTS_GENEVE) { 1172 NL_SET_ERR_MSG(extack, "Non-geneve option type for mask"); 1173 return -EINVAL; 1174 } 1175 1176 err = nla_parse_nested_deprecated(tb, 1177 TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX, 1178 nla, geneve_opt_policy, extack); 1179 if (err < 0) 1180 return err; 1181 1182 /* We are not allowed to omit any of CLASS, TYPE or DATA 1183 * fields from the key. 1184 */ 1185 if (!option_len && 1186 (!tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS] || 1187 !tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE] || 1188 !tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA])) { 1189 NL_SET_ERR_MSG(extack, "Missing tunnel key geneve option class, type or data"); 1190 return -EINVAL; 1191 } 1192 1193 /* Omitting any of CLASS, TYPE or DATA fields is allowed 1194 * for the mask. 1195 */ 1196 if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA]) { 1197 int new_len = key->enc_opts.len; 1198 1199 data = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA]; 1200 data_len = nla_len(data); 1201 if (data_len < 4) { 1202 NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is less than 4 bytes long"); 1203 return -ERANGE; 1204 } 1205 if (data_len % 4) { 1206 NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is not a multiple of 4 bytes long"); 1207 return -ERANGE; 1208 } 1209 1210 new_len += sizeof(struct geneve_opt) + data_len; 1211 BUILD_BUG_ON(FLOW_DIS_TUN_OPTS_MAX != IP_TUNNEL_OPTS_MAX); 1212 if (new_len > FLOW_DIS_TUN_OPTS_MAX) { 1213 NL_SET_ERR_MSG(extack, "Tunnel options exceeds max size"); 1214 return -ERANGE; 1215 } 1216 opt->length = data_len / 4; 1217 memcpy(opt->opt_data, nla_data(data), data_len); 1218 } 1219 1220 if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS]) { 1221 class = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS]; 1222 opt->opt_class = nla_get_be16(class); 1223 } 1224 1225 if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE]) { 1226 type = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE]; 1227 opt->type = nla_get_u8(type); 1228 } 1229 1230 return sizeof(struct geneve_opt) + data_len; 1231 } 1232 1233 static int fl_set_vxlan_opt(const struct nlattr *nla, struct fl_flow_key *key, 1234 int depth, int option_len, 1235 struct netlink_ext_ack *extack) 1236 { 1237 struct nlattr *tb[TCA_FLOWER_KEY_ENC_OPT_VXLAN_MAX + 1]; 1238 struct vxlan_metadata *md; 1239 int err; 1240 1241 md = (struct vxlan_metadata *)&key->enc_opts.data[key->enc_opts.len]; 1242 memset(md, 0xff, sizeof(*md)); 1243 1244 if (!depth) 1245 return sizeof(*md); 1246 1247 if (nla_type(nla) != TCA_FLOWER_KEY_ENC_OPTS_VXLAN) { 1248 NL_SET_ERR_MSG(extack, "Non-vxlan option type for mask"); 1249 return -EINVAL; 1250 } 1251 1252 err = nla_parse_nested(tb, TCA_FLOWER_KEY_ENC_OPT_VXLAN_MAX, nla, 1253 vxlan_opt_policy, extack); 1254 if (err < 0) 1255 return err; 1256 1257 if (!option_len && !tb[TCA_FLOWER_KEY_ENC_OPT_VXLAN_GBP]) { 1258 NL_SET_ERR_MSG(extack, "Missing tunnel key vxlan option gbp"); 1259 return -EINVAL; 1260 } 1261 1262 if (tb[TCA_FLOWER_KEY_ENC_OPT_VXLAN_GBP]) { 1263 md->gbp = nla_get_u32(tb[TCA_FLOWER_KEY_ENC_OPT_VXLAN_GBP]); 1264 md->gbp &= VXLAN_GBP_MASK; 1265 } 1266 1267 return sizeof(*md); 1268 } 1269 1270 static int fl_set_erspan_opt(const struct nlattr *nla, struct fl_flow_key *key, 1271 int depth, int option_len, 1272 struct netlink_ext_ack *extack) 1273 { 1274 struct nlattr *tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_MAX + 1]; 1275 struct erspan_metadata *md; 1276 int err; 1277 1278 md = (struct erspan_metadata *)&key->enc_opts.data[key->enc_opts.len]; 1279 memset(md, 0xff, sizeof(*md)); 1280 md->version = 1; 1281 1282 if (!depth) 1283 return sizeof(*md); 1284 1285 if (nla_type(nla) != TCA_FLOWER_KEY_ENC_OPTS_ERSPAN) { 1286 NL_SET_ERR_MSG(extack, "Non-erspan option type for mask"); 1287 return -EINVAL; 1288 } 1289 1290 err = nla_parse_nested(tb, TCA_FLOWER_KEY_ENC_OPT_ERSPAN_MAX, nla, 1291 erspan_opt_policy, extack); 1292 if (err < 0) 1293 return err; 1294 1295 if (!option_len && !tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_VER]) { 1296 NL_SET_ERR_MSG(extack, "Missing tunnel key erspan option ver"); 1297 return -EINVAL; 1298 } 1299 1300 if (tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_VER]) 1301 md->version = nla_get_u8(tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_VER]); 1302 1303 if (md->version == 1) { 1304 if (!option_len && !tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_INDEX]) { 1305 NL_SET_ERR_MSG(extack, "Missing tunnel key erspan option index"); 1306 return -EINVAL; 1307 } 1308 if (tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_INDEX]) { 1309 nla = tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_INDEX]; 1310 memset(&md->u, 0x00, sizeof(md->u)); 1311 md->u.index = nla_get_be32(nla); 1312 } 1313 } else if (md->version == 2) { 1314 if (!option_len && (!tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_DIR] || 1315 !tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_HWID])) { 1316 NL_SET_ERR_MSG(extack, "Missing tunnel key erspan option dir or hwid"); 1317 return -EINVAL; 1318 } 1319 if (tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_DIR]) { 1320 nla = tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_DIR]; 1321 md->u.md2.dir = nla_get_u8(nla); 1322 } 1323 if (tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_HWID]) { 1324 nla = tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_HWID]; 1325 set_hwid(&md->u.md2, nla_get_u8(nla)); 1326 } 1327 } else { 1328 NL_SET_ERR_MSG(extack, "Tunnel key erspan option ver is incorrect"); 1329 return -EINVAL; 1330 } 1331 1332 return sizeof(*md); 1333 } 1334 1335 static int fl_set_gtp_opt(const struct nlattr *nla, struct fl_flow_key *key, 1336 int depth, int option_len, 1337 struct netlink_ext_ack *extack) 1338 { 1339 struct nlattr *tb[TCA_FLOWER_KEY_ENC_OPT_GTP_MAX + 1]; 1340 struct gtp_pdu_session_info *sinfo; 1341 u8 len = key->enc_opts.len; 1342 int err; 1343 1344 sinfo = (struct gtp_pdu_session_info *)&key->enc_opts.data[len]; 1345 memset(sinfo, 0xff, option_len); 1346 1347 if (!depth) 1348 return sizeof(*sinfo); 1349 1350 if (nla_type(nla) != TCA_FLOWER_KEY_ENC_OPTS_GTP) { 1351 NL_SET_ERR_MSG_MOD(extack, "Non-gtp option type for mask"); 1352 return -EINVAL; 1353 } 1354 1355 err = nla_parse_nested(tb, TCA_FLOWER_KEY_ENC_OPT_GTP_MAX, nla, 1356 gtp_opt_policy, extack); 1357 if (err < 0) 1358 return err; 1359 1360 if (!option_len && 1361 (!tb[TCA_FLOWER_KEY_ENC_OPT_GTP_PDU_TYPE] || 1362 !tb[TCA_FLOWER_KEY_ENC_OPT_GTP_QFI])) { 1363 NL_SET_ERR_MSG_MOD(extack, 1364 "Missing tunnel key gtp option pdu type or qfi"); 1365 return -EINVAL; 1366 } 1367 1368 if (tb[TCA_FLOWER_KEY_ENC_OPT_GTP_PDU_TYPE]) 1369 sinfo->pdu_type = 1370 nla_get_u8(tb[TCA_FLOWER_KEY_ENC_OPT_GTP_PDU_TYPE]); 1371 1372 if (tb[TCA_FLOWER_KEY_ENC_OPT_GTP_QFI]) 1373 sinfo->qfi = nla_get_u8(tb[TCA_FLOWER_KEY_ENC_OPT_GTP_QFI]); 1374 1375 return sizeof(*sinfo); 1376 } 1377 1378 static int fl_set_enc_opt(struct nlattr **tb, struct fl_flow_key *key, 1379 struct fl_flow_key *mask, 1380 struct netlink_ext_ack *extack) 1381 { 1382 const struct nlattr *nla_enc_key, *nla_opt_key, *nla_opt_msk = NULL; 1383 int err, option_len, key_depth, msk_depth = 0; 1384 1385 err = nla_validate_nested_deprecated(tb[TCA_FLOWER_KEY_ENC_OPTS], 1386 TCA_FLOWER_KEY_ENC_OPTS_MAX, 1387 enc_opts_policy, extack); 1388 if (err) 1389 return err; 1390 1391 nla_enc_key = nla_data(tb[TCA_FLOWER_KEY_ENC_OPTS]); 1392 1393 if (tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]) { 1394 err = nla_validate_nested_deprecated(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK], 1395 TCA_FLOWER_KEY_ENC_OPTS_MAX, 1396 enc_opts_policy, extack); 1397 if (err) 1398 return err; 1399 1400 nla_opt_msk = nla_data(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]); 1401 msk_depth = nla_len(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]); 1402 if (!nla_ok(nla_opt_msk, msk_depth)) { 1403 NL_SET_ERR_MSG(extack, "Invalid nested attribute for masks"); 1404 return -EINVAL; 1405 } 1406 } 1407 1408 nla_for_each_attr(nla_opt_key, nla_enc_key, 1409 nla_len(tb[TCA_FLOWER_KEY_ENC_OPTS]), key_depth) { 1410 switch (nla_type(nla_opt_key)) { 1411 case TCA_FLOWER_KEY_ENC_OPTS_GENEVE: 1412 if (key->enc_opts.dst_opt_type && 1413 key->enc_opts.dst_opt_type != TUNNEL_GENEVE_OPT) { 1414 NL_SET_ERR_MSG(extack, "Duplicate type for geneve options"); 1415 return -EINVAL; 1416 } 1417 option_len = 0; 1418 key->enc_opts.dst_opt_type = TUNNEL_GENEVE_OPT; 1419 option_len = fl_set_geneve_opt(nla_opt_key, key, 1420 key_depth, option_len, 1421 extack); 1422 if (option_len < 0) 1423 return option_len; 1424 1425 key->enc_opts.len += option_len; 1426 /* At the same time we need to parse through the mask 1427 * in order to verify exact and mask attribute lengths. 1428 */ 1429 mask->enc_opts.dst_opt_type = TUNNEL_GENEVE_OPT; 1430 option_len = fl_set_geneve_opt(nla_opt_msk, mask, 1431 msk_depth, option_len, 1432 extack); 1433 if (option_len < 0) 1434 return option_len; 1435 1436 mask->enc_opts.len += option_len; 1437 if (key->enc_opts.len != mask->enc_opts.len) { 1438 NL_SET_ERR_MSG(extack, "Key and mask miss aligned"); 1439 return -EINVAL; 1440 } 1441 break; 1442 case TCA_FLOWER_KEY_ENC_OPTS_VXLAN: 1443 if (key->enc_opts.dst_opt_type) { 1444 NL_SET_ERR_MSG(extack, "Duplicate type for vxlan options"); 1445 return -EINVAL; 1446 } 1447 option_len = 0; 1448 key->enc_opts.dst_opt_type = TUNNEL_VXLAN_OPT; 1449 option_len = fl_set_vxlan_opt(nla_opt_key, key, 1450 key_depth, option_len, 1451 extack); 1452 if (option_len < 0) 1453 return option_len; 1454 1455 key->enc_opts.len += option_len; 1456 /* At the same time we need to parse through the mask 1457 * in order to verify exact and mask attribute lengths. 1458 */ 1459 mask->enc_opts.dst_opt_type = TUNNEL_VXLAN_OPT; 1460 option_len = fl_set_vxlan_opt(nla_opt_msk, mask, 1461 msk_depth, option_len, 1462 extack); 1463 if (option_len < 0) 1464 return option_len; 1465 1466 mask->enc_opts.len += option_len; 1467 if (key->enc_opts.len != mask->enc_opts.len) { 1468 NL_SET_ERR_MSG(extack, "Key and mask miss aligned"); 1469 return -EINVAL; 1470 } 1471 break; 1472 case TCA_FLOWER_KEY_ENC_OPTS_ERSPAN: 1473 if (key->enc_opts.dst_opt_type) { 1474 NL_SET_ERR_MSG(extack, "Duplicate type for erspan options"); 1475 return -EINVAL; 1476 } 1477 option_len = 0; 1478 key->enc_opts.dst_opt_type = TUNNEL_ERSPAN_OPT; 1479 option_len = fl_set_erspan_opt(nla_opt_key, key, 1480 key_depth, option_len, 1481 extack); 1482 if (option_len < 0) 1483 return option_len; 1484 1485 key->enc_opts.len += option_len; 1486 /* At the same time we need to parse through the mask 1487 * in order to verify exact and mask attribute lengths. 1488 */ 1489 mask->enc_opts.dst_opt_type = TUNNEL_ERSPAN_OPT; 1490 option_len = fl_set_erspan_opt(nla_opt_msk, mask, 1491 msk_depth, option_len, 1492 extack); 1493 if (option_len < 0) 1494 return option_len; 1495 1496 mask->enc_opts.len += option_len; 1497 if (key->enc_opts.len != mask->enc_opts.len) { 1498 NL_SET_ERR_MSG(extack, "Key and mask miss aligned"); 1499 return -EINVAL; 1500 } 1501 break; 1502 case TCA_FLOWER_KEY_ENC_OPTS_GTP: 1503 if (key->enc_opts.dst_opt_type) { 1504 NL_SET_ERR_MSG_MOD(extack, 1505 "Duplicate type for gtp options"); 1506 return -EINVAL; 1507 } 1508 option_len = 0; 1509 key->enc_opts.dst_opt_type = TUNNEL_GTP_OPT; 1510 option_len = fl_set_gtp_opt(nla_opt_key, key, 1511 key_depth, option_len, 1512 extack); 1513 if (option_len < 0) 1514 return option_len; 1515 1516 key->enc_opts.len += option_len; 1517 /* At the same time we need to parse through the mask 1518 * in order to verify exact and mask attribute lengths. 1519 */ 1520 mask->enc_opts.dst_opt_type = TUNNEL_GTP_OPT; 1521 option_len = fl_set_gtp_opt(nla_opt_msk, mask, 1522 msk_depth, option_len, 1523 extack); 1524 if (option_len < 0) 1525 return option_len; 1526 1527 mask->enc_opts.len += option_len; 1528 if (key->enc_opts.len != mask->enc_opts.len) { 1529 NL_SET_ERR_MSG_MOD(extack, 1530 "Key and mask miss aligned"); 1531 return -EINVAL; 1532 } 1533 break; 1534 default: 1535 NL_SET_ERR_MSG(extack, "Unknown tunnel option type"); 1536 return -EINVAL; 1537 } 1538 1539 if (!msk_depth) 1540 continue; 1541 1542 if (!nla_ok(nla_opt_msk, msk_depth)) { 1543 NL_SET_ERR_MSG(extack, "A mask attribute is invalid"); 1544 return -EINVAL; 1545 } 1546 nla_opt_msk = nla_next(nla_opt_msk, &msk_depth); 1547 } 1548 1549 return 0; 1550 } 1551 1552 static int fl_validate_ct_state(u16 state, struct nlattr *tb, 1553 struct netlink_ext_ack *extack) 1554 { 1555 if (state && !(state & TCA_FLOWER_KEY_CT_FLAGS_TRACKED)) { 1556 NL_SET_ERR_MSG_ATTR(extack, tb, 1557 "no trk, so no other flag can be set"); 1558 return -EINVAL; 1559 } 1560 1561 if (state & TCA_FLOWER_KEY_CT_FLAGS_NEW && 1562 state & TCA_FLOWER_KEY_CT_FLAGS_ESTABLISHED) { 1563 NL_SET_ERR_MSG_ATTR(extack, tb, 1564 "new and est are mutually exclusive"); 1565 return -EINVAL; 1566 } 1567 1568 if (state & TCA_FLOWER_KEY_CT_FLAGS_INVALID && 1569 state & ~(TCA_FLOWER_KEY_CT_FLAGS_TRACKED | 1570 TCA_FLOWER_KEY_CT_FLAGS_INVALID)) { 1571 NL_SET_ERR_MSG_ATTR(extack, tb, 1572 "when inv is set, only trk may be set"); 1573 return -EINVAL; 1574 } 1575 1576 if (state & TCA_FLOWER_KEY_CT_FLAGS_NEW && 1577 state & TCA_FLOWER_KEY_CT_FLAGS_REPLY) { 1578 NL_SET_ERR_MSG_ATTR(extack, tb, 1579 "new and rpl are mutually exclusive"); 1580 return -EINVAL; 1581 } 1582 1583 return 0; 1584 } 1585 1586 static int fl_set_key_ct(struct nlattr **tb, 1587 struct flow_dissector_key_ct *key, 1588 struct flow_dissector_key_ct *mask, 1589 struct netlink_ext_ack *extack) 1590 { 1591 if (tb[TCA_FLOWER_KEY_CT_STATE]) { 1592 int err; 1593 1594 if (!IS_ENABLED(CONFIG_NF_CONNTRACK)) { 1595 NL_SET_ERR_MSG(extack, "Conntrack isn't enabled"); 1596 return -EOPNOTSUPP; 1597 } 1598 fl_set_key_val(tb, &key->ct_state, TCA_FLOWER_KEY_CT_STATE, 1599 &mask->ct_state, TCA_FLOWER_KEY_CT_STATE_MASK, 1600 sizeof(key->ct_state)); 1601 1602 err = fl_validate_ct_state(key->ct_state & mask->ct_state, 1603 tb[TCA_FLOWER_KEY_CT_STATE_MASK], 1604 extack); 1605 if (err) 1606 return err; 1607 1608 } 1609 if (tb[TCA_FLOWER_KEY_CT_ZONE]) { 1610 if (!IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES)) { 1611 NL_SET_ERR_MSG(extack, "Conntrack zones isn't enabled"); 1612 return -EOPNOTSUPP; 1613 } 1614 fl_set_key_val(tb, &key->ct_zone, TCA_FLOWER_KEY_CT_ZONE, 1615 &mask->ct_zone, TCA_FLOWER_KEY_CT_ZONE_MASK, 1616 sizeof(key->ct_zone)); 1617 } 1618 if (tb[TCA_FLOWER_KEY_CT_MARK]) { 1619 if (!IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)) { 1620 NL_SET_ERR_MSG(extack, "Conntrack mark isn't enabled"); 1621 return -EOPNOTSUPP; 1622 } 1623 fl_set_key_val(tb, &key->ct_mark, TCA_FLOWER_KEY_CT_MARK, 1624 &mask->ct_mark, TCA_FLOWER_KEY_CT_MARK_MASK, 1625 sizeof(key->ct_mark)); 1626 } 1627 if (tb[TCA_FLOWER_KEY_CT_LABELS]) { 1628 if (!IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS)) { 1629 NL_SET_ERR_MSG(extack, "Conntrack labels aren't enabled"); 1630 return -EOPNOTSUPP; 1631 } 1632 fl_set_key_val(tb, key->ct_labels, TCA_FLOWER_KEY_CT_LABELS, 1633 mask->ct_labels, TCA_FLOWER_KEY_CT_LABELS_MASK, 1634 sizeof(key->ct_labels)); 1635 } 1636 1637 return 0; 1638 } 1639 1640 static bool is_vlan_key(struct nlattr *tb, __be16 *ethertype, 1641 struct fl_flow_key *key, struct fl_flow_key *mask, 1642 int vthresh) 1643 { 1644 const bool good_num_of_vlans = key->num_of_vlans.num_of_vlans > vthresh; 1645 1646 if (!tb) { 1647 *ethertype = 0; 1648 return good_num_of_vlans; 1649 } 1650 1651 *ethertype = nla_get_be16(tb); 1652 if (good_num_of_vlans || eth_type_vlan(*ethertype)) 1653 return true; 1654 1655 key->basic.n_proto = *ethertype; 1656 mask->basic.n_proto = cpu_to_be16(~0); 1657 return false; 1658 } 1659 1660 static int fl_set_key(struct net *net, struct nlattr **tb, 1661 struct fl_flow_key *key, struct fl_flow_key *mask, 1662 struct netlink_ext_ack *extack) 1663 { 1664 __be16 ethertype; 1665 int ret = 0; 1666 1667 if (tb[TCA_FLOWER_INDEV]) { 1668 int err = tcf_change_indev(net, tb[TCA_FLOWER_INDEV], extack); 1669 if (err < 0) 1670 return err; 1671 key->meta.ingress_ifindex = err; 1672 mask->meta.ingress_ifindex = 0xffffffff; 1673 } 1674 1675 fl_set_key_val(tb, &key->meta.l2_miss, TCA_FLOWER_L2_MISS, 1676 &mask->meta.l2_miss, TCA_FLOWER_UNSPEC, 1677 sizeof(key->meta.l2_miss)); 1678 1679 fl_set_key_val(tb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST, 1680 mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK, 1681 sizeof(key->eth.dst)); 1682 fl_set_key_val(tb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC, 1683 mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK, 1684 sizeof(key->eth.src)); 1685 fl_set_key_val(tb, &key->num_of_vlans, 1686 TCA_FLOWER_KEY_NUM_OF_VLANS, 1687 &mask->num_of_vlans, 1688 TCA_FLOWER_UNSPEC, 1689 sizeof(key->num_of_vlans)); 1690 1691 if (is_vlan_key(tb[TCA_FLOWER_KEY_ETH_TYPE], ðertype, key, mask, 0)) { 1692 fl_set_key_vlan(tb, ethertype, TCA_FLOWER_KEY_VLAN_ID, 1693 TCA_FLOWER_KEY_VLAN_PRIO, 1694 TCA_FLOWER_KEY_VLAN_ETH_TYPE, 1695 &key->vlan, &mask->vlan); 1696 1697 if (is_vlan_key(tb[TCA_FLOWER_KEY_VLAN_ETH_TYPE], 1698 ðertype, key, mask, 1)) { 1699 fl_set_key_vlan(tb, ethertype, 1700 TCA_FLOWER_KEY_CVLAN_ID, 1701 TCA_FLOWER_KEY_CVLAN_PRIO, 1702 TCA_FLOWER_KEY_CVLAN_ETH_TYPE, 1703 &key->cvlan, &mask->cvlan); 1704 fl_set_key_val(tb, &key->basic.n_proto, 1705 TCA_FLOWER_KEY_CVLAN_ETH_TYPE, 1706 &mask->basic.n_proto, 1707 TCA_FLOWER_UNSPEC, 1708 sizeof(key->basic.n_proto)); 1709 } 1710 } 1711 1712 if (key->basic.n_proto == htons(ETH_P_PPP_SES)) 1713 fl_set_key_pppoe(tb, &key->pppoe, &mask->pppoe, key, mask); 1714 1715 if (key->basic.n_proto == htons(ETH_P_IP) || 1716 key->basic.n_proto == htons(ETH_P_IPV6)) { 1717 fl_set_key_val(tb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO, 1718 &mask->basic.ip_proto, TCA_FLOWER_UNSPEC, 1719 sizeof(key->basic.ip_proto)); 1720 fl_set_key_ip(tb, false, &key->ip, &mask->ip); 1721 } 1722 1723 if (tb[TCA_FLOWER_KEY_IPV4_SRC] || tb[TCA_FLOWER_KEY_IPV4_DST]) { 1724 key->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; 1725 mask->control.addr_type = ~0; 1726 fl_set_key_val(tb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC, 1727 &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK, 1728 sizeof(key->ipv4.src)); 1729 fl_set_key_val(tb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST, 1730 &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK, 1731 sizeof(key->ipv4.dst)); 1732 } else if (tb[TCA_FLOWER_KEY_IPV6_SRC] || tb[TCA_FLOWER_KEY_IPV6_DST]) { 1733 key->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 1734 mask->control.addr_type = ~0; 1735 fl_set_key_val(tb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC, 1736 &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK, 1737 sizeof(key->ipv6.src)); 1738 fl_set_key_val(tb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST, 1739 &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK, 1740 sizeof(key->ipv6.dst)); 1741 } 1742 1743 if (key->basic.ip_proto == IPPROTO_TCP) { 1744 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC, 1745 &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK, 1746 sizeof(key->tp.src)); 1747 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST, 1748 &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK, 1749 sizeof(key->tp.dst)); 1750 fl_set_key_val(tb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS, 1751 &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK, 1752 sizeof(key->tcp.flags)); 1753 } else if (key->basic.ip_proto == IPPROTO_UDP) { 1754 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC, 1755 &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK, 1756 sizeof(key->tp.src)); 1757 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST, 1758 &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK, 1759 sizeof(key->tp.dst)); 1760 } else if (key->basic.ip_proto == IPPROTO_SCTP) { 1761 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC, 1762 &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK, 1763 sizeof(key->tp.src)); 1764 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST, 1765 &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK, 1766 sizeof(key->tp.dst)); 1767 } else if (key->basic.n_proto == htons(ETH_P_IP) && 1768 key->basic.ip_proto == IPPROTO_ICMP) { 1769 fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV4_TYPE, 1770 &mask->icmp.type, 1771 TCA_FLOWER_KEY_ICMPV4_TYPE_MASK, 1772 sizeof(key->icmp.type)); 1773 fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV4_CODE, 1774 &mask->icmp.code, 1775 TCA_FLOWER_KEY_ICMPV4_CODE_MASK, 1776 sizeof(key->icmp.code)); 1777 } else if (key->basic.n_proto == htons(ETH_P_IPV6) && 1778 key->basic.ip_proto == IPPROTO_ICMPV6) { 1779 fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV6_TYPE, 1780 &mask->icmp.type, 1781 TCA_FLOWER_KEY_ICMPV6_TYPE_MASK, 1782 sizeof(key->icmp.type)); 1783 fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV6_CODE, 1784 &mask->icmp.code, 1785 TCA_FLOWER_KEY_ICMPV6_CODE_MASK, 1786 sizeof(key->icmp.code)); 1787 } else if (key->basic.n_proto == htons(ETH_P_MPLS_UC) || 1788 key->basic.n_proto == htons(ETH_P_MPLS_MC)) { 1789 ret = fl_set_key_mpls(tb, &key->mpls, &mask->mpls, extack); 1790 if (ret) 1791 return ret; 1792 } else if (key->basic.n_proto == htons(ETH_P_ARP) || 1793 key->basic.n_proto == htons(ETH_P_RARP)) { 1794 fl_set_key_val(tb, &key->arp.sip, TCA_FLOWER_KEY_ARP_SIP, 1795 &mask->arp.sip, TCA_FLOWER_KEY_ARP_SIP_MASK, 1796 sizeof(key->arp.sip)); 1797 fl_set_key_val(tb, &key->arp.tip, TCA_FLOWER_KEY_ARP_TIP, 1798 &mask->arp.tip, TCA_FLOWER_KEY_ARP_TIP_MASK, 1799 sizeof(key->arp.tip)); 1800 fl_set_key_val(tb, &key->arp.op, TCA_FLOWER_KEY_ARP_OP, 1801 &mask->arp.op, TCA_FLOWER_KEY_ARP_OP_MASK, 1802 sizeof(key->arp.op)); 1803 fl_set_key_val(tb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA, 1804 mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK, 1805 sizeof(key->arp.sha)); 1806 fl_set_key_val(tb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA, 1807 mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK, 1808 sizeof(key->arp.tha)); 1809 } else if (key->basic.ip_proto == IPPROTO_L2TP) { 1810 fl_set_key_val(tb, &key->l2tpv3.session_id, 1811 TCA_FLOWER_KEY_L2TPV3_SID, 1812 &mask->l2tpv3.session_id, TCA_FLOWER_UNSPEC, 1813 sizeof(key->l2tpv3.session_id)); 1814 } 1815 1816 if (key->basic.ip_proto == IPPROTO_TCP || 1817 key->basic.ip_proto == IPPROTO_UDP || 1818 key->basic.ip_proto == IPPROTO_SCTP) { 1819 ret = fl_set_key_port_range(tb, key, mask, extack); 1820 if (ret) 1821 return ret; 1822 } 1823 1824 if (tb[TCA_FLOWER_KEY_ENC_IPV4_SRC] || 1825 tb[TCA_FLOWER_KEY_ENC_IPV4_DST]) { 1826 key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; 1827 mask->enc_control.addr_type = ~0; 1828 fl_set_key_val(tb, &key->enc_ipv4.src, 1829 TCA_FLOWER_KEY_ENC_IPV4_SRC, 1830 &mask->enc_ipv4.src, 1831 TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK, 1832 sizeof(key->enc_ipv4.src)); 1833 fl_set_key_val(tb, &key->enc_ipv4.dst, 1834 TCA_FLOWER_KEY_ENC_IPV4_DST, 1835 &mask->enc_ipv4.dst, 1836 TCA_FLOWER_KEY_ENC_IPV4_DST_MASK, 1837 sizeof(key->enc_ipv4.dst)); 1838 } 1839 1840 if (tb[TCA_FLOWER_KEY_ENC_IPV6_SRC] || 1841 tb[TCA_FLOWER_KEY_ENC_IPV6_DST]) { 1842 key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 1843 mask->enc_control.addr_type = ~0; 1844 fl_set_key_val(tb, &key->enc_ipv6.src, 1845 TCA_FLOWER_KEY_ENC_IPV6_SRC, 1846 &mask->enc_ipv6.src, 1847 TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK, 1848 sizeof(key->enc_ipv6.src)); 1849 fl_set_key_val(tb, &key->enc_ipv6.dst, 1850 TCA_FLOWER_KEY_ENC_IPV6_DST, 1851 &mask->enc_ipv6.dst, 1852 TCA_FLOWER_KEY_ENC_IPV6_DST_MASK, 1853 sizeof(key->enc_ipv6.dst)); 1854 } 1855 1856 fl_set_key_val(tb, &key->enc_key_id.keyid, TCA_FLOWER_KEY_ENC_KEY_ID, 1857 &mask->enc_key_id.keyid, TCA_FLOWER_UNSPEC, 1858 sizeof(key->enc_key_id.keyid)); 1859 1860 fl_set_key_val(tb, &key->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT, 1861 &mask->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK, 1862 sizeof(key->enc_tp.src)); 1863 1864 fl_set_key_val(tb, &key->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT, 1865 &mask->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK, 1866 sizeof(key->enc_tp.dst)); 1867 1868 fl_set_key_ip(tb, true, &key->enc_ip, &mask->enc_ip); 1869 1870 fl_set_key_val(tb, &key->hash.hash, TCA_FLOWER_KEY_HASH, 1871 &mask->hash.hash, TCA_FLOWER_KEY_HASH_MASK, 1872 sizeof(key->hash.hash)); 1873 1874 if (tb[TCA_FLOWER_KEY_ENC_OPTS]) { 1875 ret = fl_set_enc_opt(tb, key, mask, extack); 1876 if (ret) 1877 return ret; 1878 } 1879 1880 ret = fl_set_key_ct(tb, &key->ct, &mask->ct, extack); 1881 if (ret) 1882 return ret; 1883 1884 if (tb[TCA_FLOWER_KEY_FLAGS]) 1885 ret = fl_set_key_flags(tb, &key->control.flags, 1886 &mask->control.flags, extack); 1887 1888 return ret; 1889 } 1890 1891 static void fl_mask_copy(struct fl_flow_mask *dst, 1892 struct fl_flow_mask *src) 1893 { 1894 const void *psrc = fl_key_get_start(&src->key, src); 1895 void *pdst = fl_key_get_start(&dst->key, src); 1896 1897 memcpy(pdst, psrc, fl_mask_range(src)); 1898 dst->range = src->range; 1899 } 1900 1901 static const struct rhashtable_params fl_ht_params = { 1902 .key_offset = offsetof(struct cls_fl_filter, mkey), /* base offset */ 1903 .head_offset = offsetof(struct cls_fl_filter, ht_node), 1904 .automatic_shrinking = true, 1905 }; 1906 1907 static int fl_init_mask_hashtable(struct fl_flow_mask *mask) 1908 { 1909 mask->filter_ht_params = fl_ht_params; 1910 mask->filter_ht_params.key_len = fl_mask_range(mask); 1911 mask->filter_ht_params.key_offset += mask->range.start; 1912 1913 return rhashtable_init(&mask->ht, &mask->filter_ht_params); 1914 } 1915 1916 #define FL_KEY_MEMBER_OFFSET(member) offsetof(struct fl_flow_key, member) 1917 #define FL_KEY_MEMBER_SIZE(member) sizeof_field(struct fl_flow_key, member) 1918 1919 #define FL_KEY_IS_MASKED(mask, member) \ 1920 memchr_inv(((char *)mask) + FL_KEY_MEMBER_OFFSET(member), \ 1921 0, FL_KEY_MEMBER_SIZE(member)) \ 1922 1923 #define FL_KEY_SET(keys, cnt, id, member) \ 1924 do { \ 1925 keys[cnt].key_id = id; \ 1926 keys[cnt].offset = FL_KEY_MEMBER_OFFSET(member); \ 1927 cnt++; \ 1928 } while(0); 1929 1930 #define FL_KEY_SET_IF_MASKED(mask, keys, cnt, id, member) \ 1931 do { \ 1932 if (FL_KEY_IS_MASKED(mask, member)) \ 1933 FL_KEY_SET(keys, cnt, id, member); \ 1934 } while(0); 1935 1936 static void fl_init_dissector(struct flow_dissector *dissector, 1937 struct fl_flow_key *mask) 1938 { 1939 struct flow_dissector_key keys[FLOW_DISSECTOR_KEY_MAX]; 1940 size_t cnt = 0; 1941 1942 FL_KEY_SET_IF_MASKED(mask, keys, cnt, 1943 FLOW_DISSECTOR_KEY_META, meta); 1944 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_CONTROL, control); 1945 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_BASIC, basic); 1946 FL_KEY_SET_IF_MASKED(mask, keys, cnt, 1947 FLOW_DISSECTOR_KEY_ETH_ADDRS, eth); 1948 FL_KEY_SET_IF_MASKED(mask, keys, cnt, 1949 FLOW_DISSECTOR_KEY_IPV4_ADDRS, ipv4); 1950 FL_KEY_SET_IF_MASKED(mask, keys, cnt, 1951 FLOW_DISSECTOR_KEY_IPV6_ADDRS, ipv6); 1952 FL_KEY_SET_IF_MASKED(mask, keys, cnt, 1953 FLOW_DISSECTOR_KEY_PORTS, tp); 1954 FL_KEY_SET_IF_MASKED(mask, keys, cnt, 1955 FLOW_DISSECTOR_KEY_PORTS_RANGE, tp_range); 1956 FL_KEY_SET_IF_MASKED(mask, keys, cnt, 1957 FLOW_DISSECTOR_KEY_IP, ip); 1958 FL_KEY_SET_IF_MASKED(mask, keys, cnt, 1959 FLOW_DISSECTOR_KEY_TCP, tcp); 1960 FL_KEY_SET_IF_MASKED(mask, keys, cnt, 1961 FLOW_DISSECTOR_KEY_ICMP, icmp); 1962 FL_KEY_SET_IF_MASKED(mask, keys, cnt, 1963 FLOW_DISSECTOR_KEY_ARP, arp); 1964 FL_KEY_SET_IF_MASKED(mask, keys, cnt, 1965 FLOW_DISSECTOR_KEY_MPLS, mpls); 1966 FL_KEY_SET_IF_MASKED(mask, keys, cnt, 1967 FLOW_DISSECTOR_KEY_VLAN, vlan); 1968 FL_KEY_SET_IF_MASKED(mask, keys, cnt, 1969 FLOW_DISSECTOR_KEY_CVLAN, cvlan); 1970 FL_KEY_SET_IF_MASKED(mask, keys, cnt, 1971 FLOW_DISSECTOR_KEY_ENC_KEYID, enc_key_id); 1972 FL_KEY_SET_IF_MASKED(mask, keys, cnt, 1973 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS, enc_ipv4); 1974 FL_KEY_SET_IF_MASKED(mask, keys, cnt, 1975 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS, enc_ipv6); 1976 if (FL_KEY_IS_MASKED(mask, enc_ipv4) || 1977 FL_KEY_IS_MASKED(mask, enc_ipv6)) 1978 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_ENC_CONTROL, 1979 enc_control); 1980 FL_KEY_SET_IF_MASKED(mask, keys, cnt, 1981 FLOW_DISSECTOR_KEY_ENC_PORTS, enc_tp); 1982 FL_KEY_SET_IF_MASKED(mask, keys, cnt, 1983 FLOW_DISSECTOR_KEY_ENC_IP, enc_ip); 1984 FL_KEY_SET_IF_MASKED(mask, keys, cnt, 1985 FLOW_DISSECTOR_KEY_ENC_OPTS, enc_opts); 1986 FL_KEY_SET_IF_MASKED(mask, keys, cnt, 1987 FLOW_DISSECTOR_KEY_CT, ct); 1988 FL_KEY_SET_IF_MASKED(mask, keys, cnt, 1989 FLOW_DISSECTOR_KEY_HASH, hash); 1990 FL_KEY_SET_IF_MASKED(mask, keys, cnt, 1991 FLOW_DISSECTOR_KEY_NUM_OF_VLANS, num_of_vlans); 1992 FL_KEY_SET_IF_MASKED(mask, keys, cnt, 1993 FLOW_DISSECTOR_KEY_PPPOE, pppoe); 1994 FL_KEY_SET_IF_MASKED(mask, keys, cnt, 1995 FLOW_DISSECTOR_KEY_L2TPV3, l2tpv3); 1996 1997 skb_flow_dissector_init(dissector, keys, cnt); 1998 } 1999 2000 static struct fl_flow_mask *fl_create_new_mask(struct cls_fl_head *head, 2001 struct fl_flow_mask *mask) 2002 { 2003 struct fl_flow_mask *newmask; 2004 int err; 2005 2006 newmask = kzalloc(sizeof(*newmask), GFP_KERNEL); 2007 if (!newmask) 2008 return ERR_PTR(-ENOMEM); 2009 2010 fl_mask_copy(newmask, mask); 2011 2012 if ((newmask->key.tp_range.tp_min.dst && 2013 newmask->key.tp_range.tp_max.dst) || 2014 (newmask->key.tp_range.tp_min.src && 2015 newmask->key.tp_range.tp_max.src)) 2016 newmask->flags |= TCA_FLOWER_MASK_FLAGS_RANGE; 2017 2018 err = fl_init_mask_hashtable(newmask); 2019 if (err) 2020 goto errout_free; 2021 2022 fl_init_dissector(&newmask->dissector, &newmask->key); 2023 2024 INIT_LIST_HEAD_RCU(&newmask->filters); 2025 2026 refcount_set(&newmask->refcnt, 1); 2027 err = rhashtable_replace_fast(&head->ht, &mask->ht_node, 2028 &newmask->ht_node, mask_ht_params); 2029 if (err) 2030 goto errout_destroy; 2031 2032 spin_lock(&head->masks_lock); 2033 list_add_tail_rcu(&newmask->list, &head->masks); 2034 spin_unlock(&head->masks_lock); 2035 2036 return newmask; 2037 2038 errout_destroy: 2039 rhashtable_destroy(&newmask->ht); 2040 errout_free: 2041 kfree(newmask); 2042 2043 return ERR_PTR(err); 2044 } 2045 2046 static int fl_check_assign_mask(struct cls_fl_head *head, 2047 struct cls_fl_filter *fnew, 2048 struct cls_fl_filter *fold, 2049 struct fl_flow_mask *mask) 2050 { 2051 struct fl_flow_mask *newmask; 2052 int ret = 0; 2053 2054 rcu_read_lock(); 2055 2056 /* Insert mask as temporary node to prevent concurrent creation of mask 2057 * with same key. Any concurrent lookups with same key will return 2058 * -EAGAIN because mask's refcnt is zero. 2059 */ 2060 fnew->mask = rhashtable_lookup_get_insert_fast(&head->ht, 2061 &mask->ht_node, 2062 mask_ht_params); 2063 if (!fnew->mask) { 2064 rcu_read_unlock(); 2065 2066 if (fold) { 2067 ret = -EINVAL; 2068 goto errout_cleanup; 2069 } 2070 2071 newmask = fl_create_new_mask(head, mask); 2072 if (IS_ERR(newmask)) { 2073 ret = PTR_ERR(newmask); 2074 goto errout_cleanup; 2075 } 2076 2077 fnew->mask = newmask; 2078 return 0; 2079 } else if (IS_ERR(fnew->mask)) { 2080 ret = PTR_ERR(fnew->mask); 2081 } else if (fold && fold->mask != fnew->mask) { 2082 ret = -EINVAL; 2083 } else if (!refcount_inc_not_zero(&fnew->mask->refcnt)) { 2084 /* Mask was deleted concurrently, try again */ 2085 ret = -EAGAIN; 2086 } 2087 rcu_read_unlock(); 2088 return ret; 2089 2090 errout_cleanup: 2091 rhashtable_remove_fast(&head->ht, &mask->ht_node, 2092 mask_ht_params); 2093 return ret; 2094 } 2095 2096 static bool fl_needs_tc_skb_ext(const struct fl_flow_key *mask) 2097 { 2098 return mask->meta.l2_miss; 2099 } 2100 2101 static int fl_set_parms(struct net *net, struct tcf_proto *tp, 2102 struct cls_fl_filter *f, struct fl_flow_mask *mask, 2103 unsigned long base, struct nlattr **tb, 2104 struct nlattr *est, 2105 struct fl_flow_tmplt *tmplt, 2106 u32 flags, u32 fl_flags, 2107 struct netlink_ext_ack *extack) 2108 { 2109 int err; 2110 2111 err = tcf_exts_validate_ex(net, tp, tb, est, &f->exts, flags, 2112 fl_flags, extack); 2113 if (err < 0) 2114 return err; 2115 2116 if (tb[TCA_FLOWER_CLASSID]) { 2117 f->res.classid = nla_get_u32(tb[TCA_FLOWER_CLASSID]); 2118 if (flags & TCA_ACT_FLAGS_NO_RTNL) 2119 rtnl_lock(); 2120 tcf_bind_filter(tp, &f->res, base); 2121 if (flags & TCA_ACT_FLAGS_NO_RTNL) 2122 rtnl_unlock(); 2123 } 2124 2125 err = fl_set_key(net, tb, &f->key, &mask->key, extack); 2126 if (err) 2127 return err; 2128 2129 fl_mask_update_range(mask); 2130 fl_set_masked_key(&f->mkey, &f->key, mask); 2131 2132 if (!fl_mask_fits_tmplt(tmplt, mask)) { 2133 NL_SET_ERR_MSG_MOD(extack, "Mask does not fit the template"); 2134 return -EINVAL; 2135 } 2136 2137 /* Enable tc skb extension if filter matches on data extracted from 2138 * this extension. 2139 */ 2140 if (fl_needs_tc_skb_ext(&mask->key)) { 2141 f->needs_tc_skb_ext = 1; 2142 tc_skb_ext_tc_enable(); 2143 } 2144 2145 return 0; 2146 } 2147 2148 static int fl_ht_insert_unique(struct cls_fl_filter *fnew, 2149 struct cls_fl_filter *fold, 2150 bool *in_ht) 2151 { 2152 struct fl_flow_mask *mask = fnew->mask; 2153 int err; 2154 2155 err = rhashtable_lookup_insert_fast(&mask->ht, 2156 &fnew->ht_node, 2157 mask->filter_ht_params); 2158 if (err) { 2159 *in_ht = false; 2160 /* It is okay if filter with same key exists when 2161 * overwriting. 2162 */ 2163 return fold && err == -EEXIST ? 0 : err; 2164 } 2165 2166 *in_ht = true; 2167 return 0; 2168 } 2169 2170 static int fl_change(struct net *net, struct sk_buff *in_skb, 2171 struct tcf_proto *tp, unsigned long base, 2172 u32 handle, struct nlattr **tca, 2173 void **arg, u32 flags, 2174 struct netlink_ext_ack *extack) 2175 { 2176 struct cls_fl_head *head = fl_head_dereference(tp); 2177 bool rtnl_held = !(flags & TCA_ACT_FLAGS_NO_RTNL); 2178 struct cls_fl_filter *fold = *arg; 2179 struct cls_fl_filter *fnew; 2180 struct fl_flow_mask *mask; 2181 struct nlattr **tb; 2182 bool in_ht; 2183 int err; 2184 2185 if (!tca[TCA_OPTIONS]) { 2186 err = -EINVAL; 2187 goto errout_fold; 2188 } 2189 2190 mask = kzalloc(sizeof(struct fl_flow_mask), GFP_KERNEL); 2191 if (!mask) { 2192 err = -ENOBUFS; 2193 goto errout_fold; 2194 } 2195 2196 tb = kcalloc(TCA_FLOWER_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL); 2197 if (!tb) { 2198 err = -ENOBUFS; 2199 goto errout_mask_alloc; 2200 } 2201 2202 err = nla_parse_nested_deprecated(tb, TCA_FLOWER_MAX, 2203 tca[TCA_OPTIONS], fl_policy, NULL); 2204 if (err < 0) 2205 goto errout_tb; 2206 2207 if (fold && handle && fold->handle != handle) { 2208 err = -EINVAL; 2209 goto errout_tb; 2210 } 2211 2212 fnew = kzalloc(sizeof(*fnew), GFP_KERNEL); 2213 if (!fnew) { 2214 err = -ENOBUFS; 2215 goto errout_tb; 2216 } 2217 INIT_LIST_HEAD(&fnew->hw_list); 2218 refcount_set(&fnew->refcnt, 1); 2219 2220 if (tb[TCA_FLOWER_FLAGS]) { 2221 fnew->flags = nla_get_u32(tb[TCA_FLOWER_FLAGS]); 2222 2223 if (!tc_flags_valid(fnew->flags)) { 2224 kfree(fnew); 2225 err = -EINVAL; 2226 goto errout_tb; 2227 } 2228 } 2229 2230 if (!fold) { 2231 spin_lock(&tp->lock); 2232 if (!handle) { 2233 handle = 1; 2234 err = idr_alloc_u32(&head->handle_idr, NULL, &handle, 2235 INT_MAX, GFP_ATOMIC); 2236 } else { 2237 err = idr_alloc_u32(&head->handle_idr, NULL, &handle, 2238 handle, GFP_ATOMIC); 2239 2240 /* Filter with specified handle was concurrently 2241 * inserted after initial check in cls_api. This is not 2242 * necessarily an error if NLM_F_EXCL is not set in 2243 * message flags. Returning EAGAIN will cause cls_api to 2244 * try to update concurrently inserted rule. 2245 */ 2246 if (err == -ENOSPC) 2247 err = -EAGAIN; 2248 } 2249 spin_unlock(&tp->lock); 2250 2251 if (err) { 2252 kfree(fnew); 2253 goto errout_tb; 2254 } 2255 } 2256 fnew->handle = handle; 2257 2258 err = tcf_exts_init_ex(&fnew->exts, net, TCA_FLOWER_ACT, 0, tp, handle, 2259 !tc_skip_hw(fnew->flags)); 2260 if (err < 0) 2261 goto errout_idr; 2262 2263 err = fl_set_parms(net, tp, fnew, mask, base, tb, tca[TCA_RATE], 2264 tp->chain->tmplt_priv, flags, fnew->flags, 2265 extack); 2266 if (err) 2267 goto errout_idr; 2268 2269 err = fl_check_assign_mask(head, fnew, fold, mask); 2270 if (err) 2271 goto errout_idr; 2272 2273 err = fl_ht_insert_unique(fnew, fold, &in_ht); 2274 if (err) 2275 goto errout_mask; 2276 2277 if (!tc_skip_hw(fnew->flags)) { 2278 err = fl_hw_replace_filter(tp, fnew, rtnl_held, extack); 2279 if (err) 2280 goto errout_ht; 2281 } 2282 2283 if (!tc_in_hw(fnew->flags)) 2284 fnew->flags |= TCA_CLS_FLAGS_NOT_IN_HW; 2285 2286 spin_lock(&tp->lock); 2287 2288 /* tp was deleted concurrently. -EAGAIN will cause caller to lookup 2289 * proto again or create new one, if necessary. 2290 */ 2291 if (tp->deleting) { 2292 err = -EAGAIN; 2293 goto errout_hw; 2294 } 2295 2296 if (fold) { 2297 /* Fold filter was deleted concurrently. Retry lookup. */ 2298 if (fold->deleted) { 2299 err = -EAGAIN; 2300 goto errout_hw; 2301 } 2302 2303 fnew->handle = handle; 2304 2305 if (!in_ht) { 2306 struct rhashtable_params params = 2307 fnew->mask->filter_ht_params; 2308 2309 err = rhashtable_insert_fast(&fnew->mask->ht, 2310 &fnew->ht_node, 2311 params); 2312 if (err) 2313 goto errout_hw; 2314 in_ht = true; 2315 } 2316 2317 refcount_inc(&fnew->refcnt); 2318 rhashtable_remove_fast(&fold->mask->ht, 2319 &fold->ht_node, 2320 fold->mask->filter_ht_params); 2321 idr_replace(&head->handle_idr, fnew, fnew->handle); 2322 list_replace_rcu(&fold->list, &fnew->list); 2323 fold->deleted = true; 2324 2325 spin_unlock(&tp->lock); 2326 2327 fl_mask_put(head, fold->mask); 2328 if (!tc_skip_hw(fold->flags)) 2329 fl_hw_destroy_filter(tp, fold, rtnl_held, NULL); 2330 tcf_unbind_filter(tp, &fold->res); 2331 /* Caller holds reference to fold, so refcnt is always > 0 2332 * after this. 2333 */ 2334 refcount_dec(&fold->refcnt); 2335 __fl_put(fold); 2336 } else { 2337 idr_replace(&head->handle_idr, fnew, fnew->handle); 2338 2339 refcount_inc(&fnew->refcnt); 2340 list_add_tail_rcu(&fnew->list, &fnew->mask->filters); 2341 spin_unlock(&tp->lock); 2342 } 2343 2344 *arg = fnew; 2345 2346 kfree(tb); 2347 tcf_queue_work(&mask->rwork, fl_uninit_mask_free_work); 2348 return 0; 2349 2350 errout_ht: 2351 spin_lock(&tp->lock); 2352 errout_hw: 2353 fnew->deleted = true; 2354 spin_unlock(&tp->lock); 2355 if (!tc_skip_hw(fnew->flags)) 2356 fl_hw_destroy_filter(tp, fnew, rtnl_held, NULL); 2357 if (in_ht) 2358 rhashtable_remove_fast(&fnew->mask->ht, &fnew->ht_node, 2359 fnew->mask->filter_ht_params); 2360 errout_mask: 2361 fl_mask_put(head, fnew->mask); 2362 errout_idr: 2363 if (!fold) 2364 idr_remove(&head->handle_idr, fnew->handle); 2365 __fl_put(fnew); 2366 errout_tb: 2367 kfree(tb); 2368 errout_mask_alloc: 2369 tcf_queue_work(&mask->rwork, fl_uninit_mask_free_work); 2370 errout_fold: 2371 if (fold) 2372 __fl_put(fold); 2373 return err; 2374 } 2375 2376 static int fl_delete(struct tcf_proto *tp, void *arg, bool *last, 2377 bool rtnl_held, struct netlink_ext_ack *extack) 2378 { 2379 struct cls_fl_head *head = fl_head_dereference(tp); 2380 struct cls_fl_filter *f = arg; 2381 bool last_on_mask; 2382 int err = 0; 2383 2384 err = __fl_delete(tp, f, &last_on_mask, rtnl_held, extack); 2385 *last = list_empty(&head->masks); 2386 __fl_put(f); 2387 2388 return err; 2389 } 2390 2391 static void fl_walk(struct tcf_proto *tp, struct tcf_walker *arg, 2392 bool rtnl_held) 2393 { 2394 struct cls_fl_head *head = fl_head_dereference(tp); 2395 unsigned long id = arg->cookie, tmp; 2396 struct cls_fl_filter *f; 2397 2398 arg->count = arg->skip; 2399 2400 rcu_read_lock(); 2401 idr_for_each_entry_continue_ul(&head->handle_idr, f, tmp, id) { 2402 /* don't return filters that are being deleted */ 2403 if (!f || !refcount_inc_not_zero(&f->refcnt)) 2404 continue; 2405 rcu_read_unlock(); 2406 2407 if (arg->fn(tp, f, arg) < 0) { 2408 __fl_put(f); 2409 arg->stop = 1; 2410 rcu_read_lock(); 2411 break; 2412 } 2413 __fl_put(f); 2414 arg->count++; 2415 rcu_read_lock(); 2416 } 2417 rcu_read_unlock(); 2418 arg->cookie = id; 2419 } 2420 2421 static struct cls_fl_filter * 2422 fl_get_next_hw_filter(struct tcf_proto *tp, struct cls_fl_filter *f, bool add) 2423 { 2424 struct cls_fl_head *head = fl_head_dereference(tp); 2425 2426 spin_lock(&tp->lock); 2427 if (list_empty(&head->hw_filters)) { 2428 spin_unlock(&tp->lock); 2429 return NULL; 2430 } 2431 2432 if (!f) 2433 f = list_entry(&head->hw_filters, struct cls_fl_filter, 2434 hw_list); 2435 list_for_each_entry_continue(f, &head->hw_filters, hw_list) { 2436 if (!(add && f->deleted) && refcount_inc_not_zero(&f->refcnt)) { 2437 spin_unlock(&tp->lock); 2438 return f; 2439 } 2440 } 2441 2442 spin_unlock(&tp->lock); 2443 return NULL; 2444 } 2445 2446 static int fl_reoffload(struct tcf_proto *tp, bool add, flow_setup_cb_t *cb, 2447 void *cb_priv, struct netlink_ext_ack *extack) 2448 { 2449 struct tcf_block *block = tp->chain->block; 2450 struct flow_cls_offload cls_flower = {}; 2451 struct cls_fl_filter *f = NULL; 2452 int err; 2453 2454 /* hw_filters list can only be changed by hw offload functions after 2455 * obtaining rtnl lock. Make sure it is not changed while reoffload is 2456 * iterating it. 2457 */ 2458 ASSERT_RTNL(); 2459 2460 while ((f = fl_get_next_hw_filter(tp, f, add))) { 2461 cls_flower.rule = 2462 flow_rule_alloc(tcf_exts_num_actions(&f->exts)); 2463 if (!cls_flower.rule) { 2464 __fl_put(f); 2465 return -ENOMEM; 2466 } 2467 2468 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, 2469 extack); 2470 cls_flower.command = add ? 2471 FLOW_CLS_REPLACE : FLOW_CLS_DESTROY; 2472 cls_flower.cookie = (unsigned long)f; 2473 cls_flower.rule->match.dissector = &f->mask->dissector; 2474 cls_flower.rule->match.mask = &f->mask->key; 2475 cls_flower.rule->match.key = &f->mkey; 2476 2477 err = tc_setup_offload_action(&cls_flower.rule->action, &f->exts, 2478 cls_flower.common.extack); 2479 if (err) { 2480 kfree(cls_flower.rule); 2481 if (tc_skip_sw(f->flags)) { 2482 __fl_put(f); 2483 return err; 2484 } 2485 goto next_flow; 2486 } 2487 2488 cls_flower.classid = f->res.classid; 2489 2490 err = tc_setup_cb_reoffload(block, tp, add, cb, 2491 TC_SETUP_CLSFLOWER, &cls_flower, 2492 cb_priv, &f->flags, 2493 &f->in_hw_count); 2494 tc_cleanup_offload_action(&cls_flower.rule->action); 2495 kfree(cls_flower.rule); 2496 2497 if (err) { 2498 __fl_put(f); 2499 return err; 2500 } 2501 next_flow: 2502 __fl_put(f); 2503 } 2504 2505 return 0; 2506 } 2507 2508 static void fl_hw_add(struct tcf_proto *tp, void *type_data) 2509 { 2510 struct flow_cls_offload *cls_flower = type_data; 2511 struct cls_fl_filter *f = 2512 (struct cls_fl_filter *) cls_flower->cookie; 2513 struct cls_fl_head *head = fl_head_dereference(tp); 2514 2515 spin_lock(&tp->lock); 2516 list_add(&f->hw_list, &head->hw_filters); 2517 spin_unlock(&tp->lock); 2518 } 2519 2520 static void fl_hw_del(struct tcf_proto *tp, void *type_data) 2521 { 2522 struct flow_cls_offload *cls_flower = type_data; 2523 struct cls_fl_filter *f = 2524 (struct cls_fl_filter *) cls_flower->cookie; 2525 2526 spin_lock(&tp->lock); 2527 if (!list_empty(&f->hw_list)) 2528 list_del_init(&f->hw_list); 2529 spin_unlock(&tp->lock); 2530 } 2531 2532 static int fl_hw_create_tmplt(struct tcf_chain *chain, 2533 struct fl_flow_tmplt *tmplt) 2534 { 2535 struct flow_cls_offload cls_flower = {}; 2536 struct tcf_block *block = chain->block; 2537 2538 cls_flower.rule = flow_rule_alloc(0); 2539 if (!cls_flower.rule) 2540 return -ENOMEM; 2541 2542 cls_flower.common.chain_index = chain->index; 2543 cls_flower.command = FLOW_CLS_TMPLT_CREATE; 2544 cls_flower.cookie = (unsigned long) tmplt; 2545 cls_flower.rule->match.dissector = &tmplt->dissector; 2546 cls_flower.rule->match.mask = &tmplt->mask; 2547 cls_flower.rule->match.key = &tmplt->dummy_key; 2548 2549 /* We don't care if driver (any of them) fails to handle this 2550 * call. It serves just as a hint for it. 2551 */ 2552 tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, false, true); 2553 kfree(cls_flower.rule); 2554 2555 return 0; 2556 } 2557 2558 static void fl_hw_destroy_tmplt(struct tcf_chain *chain, 2559 struct fl_flow_tmplt *tmplt) 2560 { 2561 struct flow_cls_offload cls_flower = {}; 2562 struct tcf_block *block = chain->block; 2563 2564 cls_flower.common.chain_index = chain->index; 2565 cls_flower.command = FLOW_CLS_TMPLT_DESTROY; 2566 cls_flower.cookie = (unsigned long) tmplt; 2567 2568 tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, false, true); 2569 } 2570 2571 static void *fl_tmplt_create(struct net *net, struct tcf_chain *chain, 2572 struct nlattr **tca, 2573 struct netlink_ext_ack *extack) 2574 { 2575 struct fl_flow_tmplt *tmplt; 2576 struct nlattr **tb; 2577 int err; 2578 2579 if (!tca[TCA_OPTIONS]) 2580 return ERR_PTR(-EINVAL); 2581 2582 tb = kcalloc(TCA_FLOWER_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL); 2583 if (!tb) 2584 return ERR_PTR(-ENOBUFS); 2585 err = nla_parse_nested_deprecated(tb, TCA_FLOWER_MAX, 2586 tca[TCA_OPTIONS], fl_policy, NULL); 2587 if (err) 2588 goto errout_tb; 2589 2590 tmplt = kzalloc(sizeof(*tmplt), GFP_KERNEL); 2591 if (!tmplt) { 2592 err = -ENOMEM; 2593 goto errout_tb; 2594 } 2595 tmplt->chain = chain; 2596 err = fl_set_key(net, tb, &tmplt->dummy_key, &tmplt->mask, extack); 2597 if (err) 2598 goto errout_tmplt; 2599 2600 fl_init_dissector(&tmplt->dissector, &tmplt->mask); 2601 2602 err = fl_hw_create_tmplt(chain, tmplt); 2603 if (err) 2604 goto errout_tmplt; 2605 2606 kfree(tb); 2607 return tmplt; 2608 2609 errout_tmplt: 2610 kfree(tmplt); 2611 errout_tb: 2612 kfree(tb); 2613 return ERR_PTR(err); 2614 } 2615 2616 static void fl_tmplt_destroy(void *tmplt_priv) 2617 { 2618 struct fl_flow_tmplt *tmplt = tmplt_priv; 2619 2620 fl_hw_destroy_tmplt(tmplt->chain, tmplt); 2621 kfree(tmplt); 2622 } 2623 2624 static int fl_dump_key_val(struct sk_buff *skb, 2625 void *val, int val_type, 2626 void *mask, int mask_type, int len) 2627 { 2628 int err; 2629 2630 if (!memchr_inv(mask, 0, len)) 2631 return 0; 2632 err = nla_put(skb, val_type, len, val); 2633 if (err) 2634 return err; 2635 if (mask_type != TCA_FLOWER_UNSPEC) { 2636 err = nla_put(skb, mask_type, len, mask); 2637 if (err) 2638 return err; 2639 } 2640 return 0; 2641 } 2642 2643 static int fl_dump_key_port_range(struct sk_buff *skb, struct fl_flow_key *key, 2644 struct fl_flow_key *mask) 2645 { 2646 if (fl_dump_key_val(skb, &key->tp_range.tp_min.dst, 2647 TCA_FLOWER_KEY_PORT_DST_MIN, 2648 &mask->tp_range.tp_min.dst, TCA_FLOWER_UNSPEC, 2649 sizeof(key->tp_range.tp_min.dst)) || 2650 fl_dump_key_val(skb, &key->tp_range.tp_max.dst, 2651 TCA_FLOWER_KEY_PORT_DST_MAX, 2652 &mask->tp_range.tp_max.dst, TCA_FLOWER_UNSPEC, 2653 sizeof(key->tp_range.tp_max.dst)) || 2654 fl_dump_key_val(skb, &key->tp_range.tp_min.src, 2655 TCA_FLOWER_KEY_PORT_SRC_MIN, 2656 &mask->tp_range.tp_min.src, TCA_FLOWER_UNSPEC, 2657 sizeof(key->tp_range.tp_min.src)) || 2658 fl_dump_key_val(skb, &key->tp_range.tp_max.src, 2659 TCA_FLOWER_KEY_PORT_SRC_MAX, 2660 &mask->tp_range.tp_max.src, TCA_FLOWER_UNSPEC, 2661 sizeof(key->tp_range.tp_max.src))) 2662 return -1; 2663 2664 return 0; 2665 } 2666 2667 static int fl_dump_key_mpls_opt_lse(struct sk_buff *skb, 2668 struct flow_dissector_key_mpls *mpls_key, 2669 struct flow_dissector_key_mpls *mpls_mask, 2670 u8 lse_index) 2671 { 2672 struct flow_dissector_mpls_lse *lse_mask = &mpls_mask->ls[lse_index]; 2673 struct flow_dissector_mpls_lse *lse_key = &mpls_key->ls[lse_index]; 2674 int err; 2675 2676 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_OPT_LSE_DEPTH, 2677 lse_index + 1); 2678 if (err) 2679 return err; 2680 2681 if (lse_mask->mpls_ttl) { 2682 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_OPT_LSE_TTL, 2683 lse_key->mpls_ttl); 2684 if (err) 2685 return err; 2686 } 2687 if (lse_mask->mpls_bos) { 2688 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_OPT_LSE_BOS, 2689 lse_key->mpls_bos); 2690 if (err) 2691 return err; 2692 } 2693 if (lse_mask->mpls_tc) { 2694 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_OPT_LSE_TC, 2695 lse_key->mpls_tc); 2696 if (err) 2697 return err; 2698 } 2699 if (lse_mask->mpls_label) { 2700 err = nla_put_u32(skb, TCA_FLOWER_KEY_MPLS_OPT_LSE_LABEL, 2701 lse_key->mpls_label); 2702 if (err) 2703 return err; 2704 } 2705 2706 return 0; 2707 } 2708 2709 static int fl_dump_key_mpls_opts(struct sk_buff *skb, 2710 struct flow_dissector_key_mpls *mpls_key, 2711 struct flow_dissector_key_mpls *mpls_mask) 2712 { 2713 struct nlattr *opts; 2714 struct nlattr *lse; 2715 u8 lse_index; 2716 int err; 2717 2718 opts = nla_nest_start(skb, TCA_FLOWER_KEY_MPLS_OPTS); 2719 if (!opts) 2720 return -EMSGSIZE; 2721 2722 for (lse_index = 0; lse_index < FLOW_DIS_MPLS_MAX; lse_index++) { 2723 if (!(mpls_mask->used_lses & 1 << lse_index)) 2724 continue; 2725 2726 lse = nla_nest_start(skb, TCA_FLOWER_KEY_MPLS_OPTS_LSE); 2727 if (!lse) { 2728 err = -EMSGSIZE; 2729 goto err_opts; 2730 } 2731 2732 err = fl_dump_key_mpls_opt_lse(skb, mpls_key, mpls_mask, 2733 lse_index); 2734 if (err) 2735 goto err_opts_lse; 2736 nla_nest_end(skb, lse); 2737 } 2738 nla_nest_end(skb, opts); 2739 2740 return 0; 2741 2742 err_opts_lse: 2743 nla_nest_cancel(skb, lse); 2744 err_opts: 2745 nla_nest_cancel(skb, opts); 2746 2747 return err; 2748 } 2749 2750 static int fl_dump_key_mpls(struct sk_buff *skb, 2751 struct flow_dissector_key_mpls *mpls_key, 2752 struct flow_dissector_key_mpls *mpls_mask) 2753 { 2754 struct flow_dissector_mpls_lse *lse_mask; 2755 struct flow_dissector_mpls_lse *lse_key; 2756 int err; 2757 2758 if (!mpls_mask->used_lses) 2759 return 0; 2760 2761 lse_mask = &mpls_mask->ls[0]; 2762 lse_key = &mpls_key->ls[0]; 2763 2764 /* For backward compatibility, don't use the MPLS nested attributes if 2765 * the rule can be expressed using the old attributes. 2766 */ 2767 if (mpls_mask->used_lses & ~1 || 2768 (!lse_mask->mpls_ttl && !lse_mask->mpls_bos && 2769 !lse_mask->mpls_tc && !lse_mask->mpls_label)) 2770 return fl_dump_key_mpls_opts(skb, mpls_key, mpls_mask); 2771 2772 if (lse_mask->mpls_ttl) { 2773 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TTL, 2774 lse_key->mpls_ttl); 2775 if (err) 2776 return err; 2777 } 2778 if (lse_mask->mpls_tc) { 2779 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TC, 2780 lse_key->mpls_tc); 2781 if (err) 2782 return err; 2783 } 2784 if (lse_mask->mpls_label) { 2785 err = nla_put_u32(skb, TCA_FLOWER_KEY_MPLS_LABEL, 2786 lse_key->mpls_label); 2787 if (err) 2788 return err; 2789 } 2790 if (lse_mask->mpls_bos) { 2791 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_BOS, 2792 lse_key->mpls_bos); 2793 if (err) 2794 return err; 2795 } 2796 return 0; 2797 } 2798 2799 static int fl_dump_key_ip(struct sk_buff *skb, bool encap, 2800 struct flow_dissector_key_ip *key, 2801 struct flow_dissector_key_ip *mask) 2802 { 2803 int tos_key = encap ? TCA_FLOWER_KEY_ENC_IP_TOS : TCA_FLOWER_KEY_IP_TOS; 2804 int ttl_key = encap ? TCA_FLOWER_KEY_ENC_IP_TTL : TCA_FLOWER_KEY_IP_TTL; 2805 int tos_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TOS_MASK : TCA_FLOWER_KEY_IP_TOS_MASK; 2806 int ttl_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TTL_MASK : TCA_FLOWER_KEY_IP_TTL_MASK; 2807 2808 if (fl_dump_key_val(skb, &key->tos, tos_key, &mask->tos, tos_mask, sizeof(key->tos)) || 2809 fl_dump_key_val(skb, &key->ttl, ttl_key, &mask->ttl, ttl_mask, sizeof(key->ttl))) 2810 return -1; 2811 2812 return 0; 2813 } 2814 2815 static int fl_dump_key_vlan(struct sk_buff *skb, 2816 int vlan_id_key, int vlan_prio_key, 2817 struct flow_dissector_key_vlan *vlan_key, 2818 struct flow_dissector_key_vlan *vlan_mask) 2819 { 2820 int err; 2821 2822 if (!memchr_inv(vlan_mask, 0, sizeof(*vlan_mask))) 2823 return 0; 2824 if (vlan_mask->vlan_id) { 2825 err = nla_put_u16(skb, vlan_id_key, 2826 vlan_key->vlan_id); 2827 if (err) 2828 return err; 2829 } 2830 if (vlan_mask->vlan_priority) { 2831 err = nla_put_u8(skb, vlan_prio_key, 2832 vlan_key->vlan_priority); 2833 if (err) 2834 return err; 2835 } 2836 return 0; 2837 } 2838 2839 static void fl_get_key_flag(u32 dissector_key, u32 dissector_mask, 2840 u32 *flower_key, u32 *flower_mask, 2841 u32 flower_flag_bit, u32 dissector_flag_bit) 2842 { 2843 if (dissector_mask & dissector_flag_bit) { 2844 *flower_mask |= flower_flag_bit; 2845 if (dissector_key & dissector_flag_bit) 2846 *flower_key |= flower_flag_bit; 2847 } 2848 } 2849 2850 static int fl_dump_key_flags(struct sk_buff *skb, u32 flags_key, u32 flags_mask) 2851 { 2852 u32 key, mask; 2853 __be32 _key, _mask; 2854 int err; 2855 2856 if (!memchr_inv(&flags_mask, 0, sizeof(flags_mask))) 2857 return 0; 2858 2859 key = 0; 2860 mask = 0; 2861 2862 fl_get_key_flag(flags_key, flags_mask, &key, &mask, 2863 TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT); 2864 fl_get_key_flag(flags_key, flags_mask, &key, &mask, 2865 TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST, 2866 FLOW_DIS_FIRST_FRAG); 2867 2868 _key = cpu_to_be32(key); 2869 _mask = cpu_to_be32(mask); 2870 2871 err = nla_put(skb, TCA_FLOWER_KEY_FLAGS, 4, &_key); 2872 if (err) 2873 return err; 2874 2875 return nla_put(skb, TCA_FLOWER_KEY_FLAGS_MASK, 4, &_mask); 2876 } 2877 2878 static int fl_dump_key_geneve_opt(struct sk_buff *skb, 2879 struct flow_dissector_key_enc_opts *enc_opts) 2880 { 2881 struct geneve_opt *opt; 2882 struct nlattr *nest; 2883 int opt_off = 0; 2884 2885 nest = nla_nest_start_noflag(skb, TCA_FLOWER_KEY_ENC_OPTS_GENEVE); 2886 if (!nest) 2887 goto nla_put_failure; 2888 2889 while (enc_opts->len > opt_off) { 2890 opt = (struct geneve_opt *)&enc_opts->data[opt_off]; 2891 2892 if (nla_put_be16(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS, 2893 opt->opt_class)) 2894 goto nla_put_failure; 2895 if (nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE, 2896 opt->type)) 2897 goto nla_put_failure; 2898 if (nla_put(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA, 2899 opt->length * 4, opt->opt_data)) 2900 goto nla_put_failure; 2901 2902 opt_off += sizeof(struct geneve_opt) + opt->length * 4; 2903 } 2904 nla_nest_end(skb, nest); 2905 return 0; 2906 2907 nla_put_failure: 2908 nla_nest_cancel(skb, nest); 2909 return -EMSGSIZE; 2910 } 2911 2912 static int fl_dump_key_vxlan_opt(struct sk_buff *skb, 2913 struct flow_dissector_key_enc_opts *enc_opts) 2914 { 2915 struct vxlan_metadata *md; 2916 struct nlattr *nest; 2917 2918 nest = nla_nest_start_noflag(skb, TCA_FLOWER_KEY_ENC_OPTS_VXLAN); 2919 if (!nest) 2920 goto nla_put_failure; 2921 2922 md = (struct vxlan_metadata *)&enc_opts->data[0]; 2923 if (nla_put_u32(skb, TCA_FLOWER_KEY_ENC_OPT_VXLAN_GBP, md->gbp)) 2924 goto nla_put_failure; 2925 2926 nla_nest_end(skb, nest); 2927 return 0; 2928 2929 nla_put_failure: 2930 nla_nest_cancel(skb, nest); 2931 return -EMSGSIZE; 2932 } 2933 2934 static int fl_dump_key_erspan_opt(struct sk_buff *skb, 2935 struct flow_dissector_key_enc_opts *enc_opts) 2936 { 2937 struct erspan_metadata *md; 2938 struct nlattr *nest; 2939 2940 nest = nla_nest_start_noflag(skb, TCA_FLOWER_KEY_ENC_OPTS_ERSPAN); 2941 if (!nest) 2942 goto nla_put_failure; 2943 2944 md = (struct erspan_metadata *)&enc_opts->data[0]; 2945 if (nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_ERSPAN_VER, md->version)) 2946 goto nla_put_failure; 2947 2948 if (md->version == 1 && 2949 nla_put_be32(skb, TCA_FLOWER_KEY_ENC_OPT_ERSPAN_INDEX, md->u.index)) 2950 goto nla_put_failure; 2951 2952 if (md->version == 2 && 2953 (nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_ERSPAN_DIR, 2954 md->u.md2.dir) || 2955 nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_ERSPAN_HWID, 2956 get_hwid(&md->u.md2)))) 2957 goto nla_put_failure; 2958 2959 nla_nest_end(skb, nest); 2960 return 0; 2961 2962 nla_put_failure: 2963 nla_nest_cancel(skb, nest); 2964 return -EMSGSIZE; 2965 } 2966 2967 static int fl_dump_key_gtp_opt(struct sk_buff *skb, 2968 struct flow_dissector_key_enc_opts *enc_opts) 2969 2970 { 2971 struct gtp_pdu_session_info *session_info; 2972 struct nlattr *nest; 2973 2974 nest = nla_nest_start_noflag(skb, TCA_FLOWER_KEY_ENC_OPTS_GTP); 2975 if (!nest) 2976 goto nla_put_failure; 2977 2978 session_info = (struct gtp_pdu_session_info *)&enc_opts->data[0]; 2979 2980 if (nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_GTP_PDU_TYPE, 2981 session_info->pdu_type)) 2982 goto nla_put_failure; 2983 2984 if (nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_GTP_QFI, session_info->qfi)) 2985 goto nla_put_failure; 2986 2987 nla_nest_end(skb, nest); 2988 return 0; 2989 2990 nla_put_failure: 2991 nla_nest_cancel(skb, nest); 2992 return -EMSGSIZE; 2993 } 2994 2995 static int fl_dump_key_ct(struct sk_buff *skb, 2996 struct flow_dissector_key_ct *key, 2997 struct flow_dissector_key_ct *mask) 2998 { 2999 if (IS_ENABLED(CONFIG_NF_CONNTRACK) && 3000 fl_dump_key_val(skb, &key->ct_state, TCA_FLOWER_KEY_CT_STATE, 3001 &mask->ct_state, TCA_FLOWER_KEY_CT_STATE_MASK, 3002 sizeof(key->ct_state))) 3003 goto nla_put_failure; 3004 3005 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) && 3006 fl_dump_key_val(skb, &key->ct_zone, TCA_FLOWER_KEY_CT_ZONE, 3007 &mask->ct_zone, TCA_FLOWER_KEY_CT_ZONE_MASK, 3008 sizeof(key->ct_zone))) 3009 goto nla_put_failure; 3010 3011 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) && 3012 fl_dump_key_val(skb, &key->ct_mark, TCA_FLOWER_KEY_CT_MARK, 3013 &mask->ct_mark, TCA_FLOWER_KEY_CT_MARK_MASK, 3014 sizeof(key->ct_mark))) 3015 goto nla_put_failure; 3016 3017 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) && 3018 fl_dump_key_val(skb, &key->ct_labels, TCA_FLOWER_KEY_CT_LABELS, 3019 &mask->ct_labels, TCA_FLOWER_KEY_CT_LABELS_MASK, 3020 sizeof(key->ct_labels))) 3021 goto nla_put_failure; 3022 3023 return 0; 3024 3025 nla_put_failure: 3026 return -EMSGSIZE; 3027 } 3028 3029 static int fl_dump_key_options(struct sk_buff *skb, int enc_opt_type, 3030 struct flow_dissector_key_enc_opts *enc_opts) 3031 { 3032 struct nlattr *nest; 3033 int err; 3034 3035 if (!enc_opts->len) 3036 return 0; 3037 3038 nest = nla_nest_start_noflag(skb, enc_opt_type); 3039 if (!nest) 3040 goto nla_put_failure; 3041 3042 switch (enc_opts->dst_opt_type) { 3043 case TUNNEL_GENEVE_OPT: 3044 err = fl_dump_key_geneve_opt(skb, enc_opts); 3045 if (err) 3046 goto nla_put_failure; 3047 break; 3048 case TUNNEL_VXLAN_OPT: 3049 err = fl_dump_key_vxlan_opt(skb, enc_opts); 3050 if (err) 3051 goto nla_put_failure; 3052 break; 3053 case TUNNEL_ERSPAN_OPT: 3054 err = fl_dump_key_erspan_opt(skb, enc_opts); 3055 if (err) 3056 goto nla_put_failure; 3057 break; 3058 case TUNNEL_GTP_OPT: 3059 err = fl_dump_key_gtp_opt(skb, enc_opts); 3060 if (err) 3061 goto nla_put_failure; 3062 break; 3063 default: 3064 goto nla_put_failure; 3065 } 3066 nla_nest_end(skb, nest); 3067 return 0; 3068 3069 nla_put_failure: 3070 nla_nest_cancel(skb, nest); 3071 return -EMSGSIZE; 3072 } 3073 3074 static int fl_dump_key_enc_opt(struct sk_buff *skb, 3075 struct flow_dissector_key_enc_opts *key_opts, 3076 struct flow_dissector_key_enc_opts *msk_opts) 3077 { 3078 int err; 3079 3080 err = fl_dump_key_options(skb, TCA_FLOWER_KEY_ENC_OPTS, key_opts); 3081 if (err) 3082 return err; 3083 3084 return fl_dump_key_options(skb, TCA_FLOWER_KEY_ENC_OPTS_MASK, msk_opts); 3085 } 3086 3087 static int fl_dump_key(struct sk_buff *skb, struct net *net, 3088 struct fl_flow_key *key, struct fl_flow_key *mask) 3089 { 3090 if (mask->meta.ingress_ifindex) { 3091 struct net_device *dev; 3092 3093 dev = __dev_get_by_index(net, key->meta.ingress_ifindex); 3094 if (dev && nla_put_string(skb, TCA_FLOWER_INDEV, dev->name)) 3095 goto nla_put_failure; 3096 } 3097 3098 if (fl_dump_key_val(skb, &key->meta.l2_miss, 3099 TCA_FLOWER_L2_MISS, &mask->meta.l2_miss, 3100 TCA_FLOWER_UNSPEC, sizeof(key->meta.l2_miss))) 3101 goto nla_put_failure; 3102 3103 if (fl_dump_key_val(skb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST, 3104 mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK, 3105 sizeof(key->eth.dst)) || 3106 fl_dump_key_val(skb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC, 3107 mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK, 3108 sizeof(key->eth.src)) || 3109 fl_dump_key_val(skb, &key->basic.n_proto, TCA_FLOWER_KEY_ETH_TYPE, 3110 &mask->basic.n_proto, TCA_FLOWER_UNSPEC, 3111 sizeof(key->basic.n_proto))) 3112 goto nla_put_failure; 3113 3114 if (mask->num_of_vlans.num_of_vlans) { 3115 if (nla_put_u8(skb, TCA_FLOWER_KEY_NUM_OF_VLANS, key->num_of_vlans.num_of_vlans)) 3116 goto nla_put_failure; 3117 } 3118 3119 if (fl_dump_key_mpls(skb, &key->mpls, &mask->mpls)) 3120 goto nla_put_failure; 3121 3122 if (fl_dump_key_vlan(skb, TCA_FLOWER_KEY_VLAN_ID, 3123 TCA_FLOWER_KEY_VLAN_PRIO, &key->vlan, &mask->vlan)) 3124 goto nla_put_failure; 3125 3126 if (fl_dump_key_vlan(skb, TCA_FLOWER_KEY_CVLAN_ID, 3127 TCA_FLOWER_KEY_CVLAN_PRIO, 3128 &key->cvlan, &mask->cvlan) || 3129 (mask->cvlan.vlan_tpid && 3130 nla_put_be16(skb, TCA_FLOWER_KEY_VLAN_ETH_TYPE, 3131 key->cvlan.vlan_tpid))) 3132 goto nla_put_failure; 3133 3134 if (mask->basic.n_proto) { 3135 if (mask->cvlan.vlan_eth_type) { 3136 if (nla_put_be16(skb, TCA_FLOWER_KEY_CVLAN_ETH_TYPE, 3137 key->basic.n_proto)) 3138 goto nla_put_failure; 3139 } else if (mask->vlan.vlan_eth_type) { 3140 if (nla_put_be16(skb, TCA_FLOWER_KEY_VLAN_ETH_TYPE, 3141 key->vlan.vlan_eth_type)) 3142 goto nla_put_failure; 3143 } 3144 } 3145 3146 if ((key->basic.n_proto == htons(ETH_P_IP) || 3147 key->basic.n_proto == htons(ETH_P_IPV6)) && 3148 (fl_dump_key_val(skb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO, 3149 &mask->basic.ip_proto, TCA_FLOWER_UNSPEC, 3150 sizeof(key->basic.ip_proto)) || 3151 fl_dump_key_ip(skb, false, &key->ip, &mask->ip))) 3152 goto nla_put_failure; 3153 3154 if (mask->pppoe.session_id) { 3155 if (nla_put_be16(skb, TCA_FLOWER_KEY_PPPOE_SID, 3156 key->pppoe.session_id)) 3157 goto nla_put_failure; 3158 } 3159 if (mask->basic.n_proto && mask->pppoe.ppp_proto) { 3160 if (nla_put_be16(skb, TCA_FLOWER_KEY_PPP_PROTO, 3161 key->pppoe.ppp_proto)) 3162 goto nla_put_failure; 3163 } 3164 3165 if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS && 3166 (fl_dump_key_val(skb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC, 3167 &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK, 3168 sizeof(key->ipv4.src)) || 3169 fl_dump_key_val(skb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST, 3170 &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK, 3171 sizeof(key->ipv4.dst)))) 3172 goto nla_put_failure; 3173 else if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS && 3174 (fl_dump_key_val(skb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC, 3175 &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK, 3176 sizeof(key->ipv6.src)) || 3177 fl_dump_key_val(skb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST, 3178 &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK, 3179 sizeof(key->ipv6.dst)))) 3180 goto nla_put_failure; 3181 3182 if (key->basic.ip_proto == IPPROTO_TCP && 3183 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC, 3184 &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK, 3185 sizeof(key->tp.src)) || 3186 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST, 3187 &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK, 3188 sizeof(key->tp.dst)) || 3189 fl_dump_key_val(skb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS, 3190 &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK, 3191 sizeof(key->tcp.flags)))) 3192 goto nla_put_failure; 3193 else if (key->basic.ip_proto == IPPROTO_UDP && 3194 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC, 3195 &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK, 3196 sizeof(key->tp.src)) || 3197 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST, 3198 &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK, 3199 sizeof(key->tp.dst)))) 3200 goto nla_put_failure; 3201 else if (key->basic.ip_proto == IPPROTO_SCTP && 3202 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC, 3203 &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK, 3204 sizeof(key->tp.src)) || 3205 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST, 3206 &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK, 3207 sizeof(key->tp.dst)))) 3208 goto nla_put_failure; 3209 else if (key->basic.n_proto == htons(ETH_P_IP) && 3210 key->basic.ip_proto == IPPROTO_ICMP && 3211 (fl_dump_key_val(skb, &key->icmp.type, 3212 TCA_FLOWER_KEY_ICMPV4_TYPE, &mask->icmp.type, 3213 TCA_FLOWER_KEY_ICMPV4_TYPE_MASK, 3214 sizeof(key->icmp.type)) || 3215 fl_dump_key_val(skb, &key->icmp.code, 3216 TCA_FLOWER_KEY_ICMPV4_CODE, &mask->icmp.code, 3217 TCA_FLOWER_KEY_ICMPV4_CODE_MASK, 3218 sizeof(key->icmp.code)))) 3219 goto nla_put_failure; 3220 else if (key->basic.n_proto == htons(ETH_P_IPV6) && 3221 key->basic.ip_proto == IPPROTO_ICMPV6 && 3222 (fl_dump_key_val(skb, &key->icmp.type, 3223 TCA_FLOWER_KEY_ICMPV6_TYPE, &mask->icmp.type, 3224 TCA_FLOWER_KEY_ICMPV6_TYPE_MASK, 3225 sizeof(key->icmp.type)) || 3226 fl_dump_key_val(skb, &key->icmp.code, 3227 TCA_FLOWER_KEY_ICMPV6_CODE, &mask->icmp.code, 3228 TCA_FLOWER_KEY_ICMPV6_CODE_MASK, 3229 sizeof(key->icmp.code)))) 3230 goto nla_put_failure; 3231 else if ((key->basic.n_proto == htons(ETH_P_ARP) || 3232 key->basic.n_proto == htons(ETH_P_RARP)) && 3233 (fl_dump_key_val(skb, &key->arp.sip, 3234 TCA_FLOWER_KEY_ARP_SIP, &mask->arp.sip, 3235 TCA_FLOWER_KEY_ARP_SIP_MASK, 3236 sizeof(key->arp.sip)) || 3237 fl_dump_key_val(skb, &key->arp.tip, 3238 TCA_FLOWER_KEY_ARP_TIP, &mask->arp.tip, 3239 TCA_FLOWER_KEY_ARP_TIP_MASK, 3240 sizeof(key->arp.tip)) || 3241 fl_dump_key_val(skb, &key->arp.op, 3242 TCA_FLOWER_KEY_ARP_OP, &mask->arp.op, 3243 TCA_FLOWER_KEY_ARP_OP_MASK, 3244 sizeof(key->arp.op)) || 3245 fl_dump_key_val(skb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA, 3246 mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK, 3247 sizeof(key->arp.sha)) || 3248 fl_dump_key_val(skb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA, 3249 mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK, 3250 sizeof(key->arp.tha)))) 3251 goto nla_put_failure; 3252 else if (key->basic.ip_proto == IPPROTO_L2TP && 3253 fl_dump_key_val(skb, &key->l2tpv3.session_id, 3254 TCA_FLOWER_KEY_L2TPV3_SID, 3255 &mask->l2tpv3.session_id, 3256 TCA_FLOWER_UNSPEC, 3257 sizeof(key->l2tpv3.session_id))) 3258 goto nla_put_failure; 3259 3260 if ((key->basic.ip_proto == IPPROTO_TCP || 3261 key->basic.ip_proto == IPPROTO_UDP || 3262 key->basic.ip_proto == IPPROTO_SCTP) && 3263 fl_dump_key_port_range(skb, key, mask)) 3264 goto nla_put_failure; 3265 3266 if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS && 3267 (fl_dump_key_val(skb, &key->enc_ipv4.src, 3268 TCA_FLOWER_KEY_ENC_IPV4_SRC, &mask->enc_ipv4.src, 3269 TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK, 3270 sizeof(key->enc_ipv4.src)) || 3271 fl_dump_key_val(skb, &key->enc_ipv4.dst, 3272 TCA_FLOWER_KEY_ENC_IPV4_DST, &mask->enc_ipv4.dst, 3273 TCA_FLOWER_KEY_ENC_IPV4_DST_MASK, 3274 sizeof(key->enc_ipv4.dst)))) 3275 goto nla_put_failure; 3276 else if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS && 3277 (fl_dump_key_val(skb, &key->enc_ipv6.src, 3278 TCA_FLOWER_KEY_ENC_IPV6_SRC, &mask->enc_ipv6.src, 3279 TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK, 3280 sizeof(key->enc_ipv6.src)) || 3281 fl_dump_key_val(skb, &key->enc_ipv6.dst, 3282 TCA_FLOWER_KEY_ENC_IPV6_DST, 3283 &mask->enc_ipv6.dst, 3284 TCA_FLOWER_KEY_ENC_IPV6_DST_MASK, 3285 sizeof(key->enc_ipv6.dst)))) 3286 goto nla_put_failure; 3287 3288 if (fl_dump_key_val(skb, &key->enc_key_id, TCA_FLOWER_KEY_ENC_KEY_ID, 3289 &mask->enc_key_id, TCA_FLOWER_UNSPEC, 3290 sizeof(key->enc_key_id)) || 3291 fl_dump_key_val(skb, &key->enc_tp.src, 3292 TCA_FLOWER_KEY_ENC_UDP_SRC_PORT, 3293 &mask->enc_tp.src, 3294 TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK, 3295 sizeof(key->enc_tp.src)) || 3296 fl_dump_key_val(skb, &key->enc_tp.dst, 3297 TCA_FLOWER_KEY_ENC_UDP_DST_PORT, 3298 &mask->enc_tp.dst, 3299 TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK, 3300 sizeof(key->enc_tp.dst)) || 3301 fl_dump_key_ip(skb, true, &key->enc_ip, &mask->enc_ip) || 3302 fl_dump_key_enc_opt(skb, &key->enc_opts, &mask->enc_opts)) 3303 goto nla_put_failure; 3304 3305 if (fl_dump_key_ct(skb, &key->ct, &mask->ct)) 3306 goto nla_put_failure; 3307 3308 if (fl_dump_key_flags(skb, key->control.flags, mask->control.flags)) 3309 goto nla_put_failure; 3310 3311 if (fl_dump_key_val(skb, &key->hash.hash, TCA_FLOWER_KEY_HASH, 3312 &mask->hash.hash, TCA_FLOWER_KEY_HASH_MASK, 3313 sizeof(key->hash.hash))) 3314 goto nla_put_failure; 3315 3316 return 0; 3317 3318 nla_put_failure: 3319 return -EMSGSIZE; 3320 } 3321 3322 static int fl_dump(struct net *net, struct tcf_proto *tp, void *fh, 3323 struct sk_buff *skb, struct tcmsg *t, bool rtnl_held) 3324 { 3325 struct cls_fl_filter *f = fh; 3326 struct nlattr *nest; 3327 struct fl_flow_key *key, *mask; 3328 bool skip_hw; 3329 3330 if (!f) 3331 return skb->len; 3332 3333 t->tcm_handle = f->handle; 3334 3335 nest = nla_nest_start_noflag(skb, TCA_OPTIONS); 3336 if (!nest) 3337 goto nla_put_failure; 3338 3339 spin_lock(&tp->lock); 3340 3341 if (f->res.classid && 3342 nla_put_u32(skb, TCA_FLOWER_CLASSID, f->res.classid)) 3343 goto nla_put_failure_locked; 3344 3345 key = &f->key; 3346 mask = &f->mask->key; 3347 skip_hw = tc_skip_hw(f->flags); 3348 3349 if (fl_dump_key(skb, net, key, mask)) 3350 goto nla_put_failure_locked; 3351 3352 if (f->flags && nla_put_u32(skb, TCA_FLOWER_FLAGS, f->flags)) 3353 goto nla_put_failure_locked; 3354 3355 spin_unlock(&tp->lock); 3356 3357 if (!skip_hw) 3358 fl_hw_update_stats(tp, f, rtnl_held); 3359 3360 if (nla_put_u32(skb, TCA_FLOWER_IN_HW_COUNT, f->in_hw_count)) 3361 goto nla_put_failure; 3362 3363 if (tcf_exts_dump(skb, &f->exts)) 3364 goto nla_put_failure; 3365 3366 nla_nest_end(skb, nest); 3367 3368 if (tcf_exts_dump_stats(skb, &f->exts) < 0) 3369 goto nla_put_failure; 3370 3371 return skb->len; 3372 3373 nla_put_failure_locked: 3374 spin_unlock(&tp->lock); 3375 nla_put_failure: 3376 nla_nest_cancel(skb, nest); 3377 return -1; 3378 } 3379 3380 static int fl_terse_dump(struct net *net, struct tcf_proto *tp, void *fh, 3381 struct sk_buff *skb, struct tcmsg *t, bool rtnl_held) 3382 { 3383 struct cls_fl_filter *f = fh; 3384 struct nlattr *nest; 3385 bool skip_hw; 3386 3387 if (!f) 3388 return skb->len; 3389 3390 t->tcm_handle = f->handle; 3391 3392 nest = nla_nest_start_noflag(skb, TCA_OPTIONS); 3393 if (!nest) 3394 goto nla_put_failure; 3395 3396 spin_lock(&tp->lock); 3397 3398 skip_hw = tc_skip_hw(f->flags); 3399 3400 if (f->flags && nla_put_u32(skb, TCA_FLOWER_FLAGS, f->flags)) 3401 goto nla_put_failure_locked; 3402 3403 spin_unlock(&tp->lock); 3404 3405 if (!skip_hw) 3406 fl_hw_update_stats(tp, f, rtnl_held); 3407 3408 if (tcf_exts_terse_dump(skb, &f->exts)) 3409 goto nla_put_failure; 3410 3411 nla_nest_end(skb, nest); 3412 3413 return skb->len; 3414 3415 nla_put_failure_locked: 3416 spin_unlock(&tp->lock); 3417 nla_put_failure: 3418 nla_nest_cancel(skb, nest); 3419 return -1; 3420 } 3421 3422 static int fl_tmplt_dump(struct sk_buff *skb, struct net *net, void *tmplt_priv) 3423 { 3424 struct fl_flow_tmplt *tmplt = tmplt_priv; 3425 struct fl_flow_key *key, *mask; 3426 struct nlattr *nest; 3427 3428 nest = nla_nest_start_noflag(skb, TCA_OPTIONS); 3429 if (!nest) 3430 goto nla_put_failure; 3431 3432 key = &tmplt->dummy_key; 3433 mask = &tmplt->mask; 3434 3435 if (fl_dump_key(skb, net, key, mask)) 3436 goto nla_put_failure; 3437 3438 nla_nest_end(skb, nest); 3439 3440 return skb->len; 3441 3442 nla_put_failure: 3443 nla_nest_cancel(skb, nest); 3444 return -EMSGSIZE; 3445 } 3446 3447 static void fl_bind_class(void *fh, u32 classid, unsigned long cl, void *q, 3448 unsigned long base) 3449 { 3450 struct cls_fl_filter *f = fh; 3451 3452 tc_cls_bind_class(classid, cl, q, &f->res, base); 3453 } 3454 3455 static bool fl_delete_empty(struct tcf_proto *tp) 3456 { 3457 struct cls_fl_head *head = fl_head_dereference(tp); 3458 3459 spin_lock(&tp->lock); 3460 tp->deleting = idr_is_empty(&head->handle_idr); 3461 spin_unlock(&tp->lock); 3462 3463 return tp->deleting; 3464 } 3465 3466 static struct tcf_proto_ops cls_fl_ops __read_mostly = { 3467 .kind = "flower", 3468 .classify = fl_classify, 3469 .init = fl_init, 3470 .destroy = fl_destroy, 3471 .get = fl_get, 3472 .put = fl_put, 3473 .change = fl_change, 3474 .delete = fl_delete, 3475 .delete_empty = fl_delete_empty, 3476 .walk = fl_walk, 3477 .reoffload = fl_reoffload, 3478 .hw_add = fl_hw_add, 3479 .hw_del = fl_hw_del, 3480 .dump = fl_dump, 3481 .terse_dump = fl_terse_dump, 3482 .bind_class = fl_bind_class, 3483 .tmplt_create = fl_tmplt_create, 3484 .tmplt_destroy = fl_tmplt_destroy, 3485 .tmplt_dump = fl_tmplt_dump, 3486 .get_exts = fl_get_exts, 3487 .owner = THIS_MODULE, 3488 .flags = TCF_PROTO_OPS_DOIT_UNLOCKED, 3489 }; 3490 3491 static int __init cls_fl_init(void) 3492 { 3493 return register_tcf_proto_ops(&cls_fl_ops); 3494 } 3495 3496 static void __exit cls_fl_exit(void) 3497 { 3498 unregister_tcf_proto_ops(&cls_fl_ops); 3499 } 3500 3501 module_init(cls_fl_init); 3502 module_exit(cls_fl_exit); 3503 3504 MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>"); 3505 MODULE_DESCRIPTION("Flower classifier"); 3506 MODULE_LICENSE("GPL v2"); 3507