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