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