1 /* 2 * net/sched/cls_flower.c Flower classifier 3 * 4 * Copyright (c) 2015 Jiri Pirko <jiri@resnulli.us> 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 */ 11 12 #include <linux/kernel.h> 13 #include <linux/init.h> 14 #include <linux/module.h> 15 #include <linux/rhashtable.h> 16 #include <linux/workqueue.h> 17 18 #include <linux/if_ether.h> 19 #include <linux/in6.h> 20 #include <linux/ip.h> 21 #include <linux/mpls.h> 22 23 #include <net/sch_generic.h> 24 #include <net/pkt_cls.h> 25 #include <net/ip.h> 26 #include <net/flow_dissector.h> 27 28 #include <net/dst.h> 29 #include <net/dst_metadata.h> 30 31 struct fl_flow_key { 32 int indev_ifindex; 33 struct flow_dissector_key_control control; 34 struct flow_dissector_key_control enc_control; 35 struct flow_dissector_key_basic basic; 36 struct flow_dissector_key_eth_addrs eth; 37 struct flow_dissector_key_vlan vlan; 38 union { 39 struct flow_dissector_key_ipv4_addrs ipv4; 40 struct flow_dissector_key_ipv6_addrs ipv6; 41 }; 42 struct flow_dissector_key_ports tp; 43 struct flow_dissector_key_icmp icmp; 44 struct flow_dissector_key_arp arp; 45 struct flow_dissector_key_keyid enc_key_id; 46 union { 47 struct flow_dissector_key_ipv4_addrs enc_ipv4; 48 struct flow_dissector_key_ipv6_addrs enc_ipv6; 49 }; 50 struct flow_dissector_key_ports enc_tp; 51 struct flow_dissector_key_mpls mpls; 52 struct flow_dissector_key_tcp tcp; 53 struct flow_dissector_key_ip ip; 54 } __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */ 55 56 struct fl_flow_mask_range { 57 unsigned short int start; 58 unsigned short int end; 59 }; 60 61 struct fl_flow_mask { 62 struct fl_flow_key key; 63 struct fl_flow_mask_range range; 64 struct rhash_head ht_node; 65 struct rhashtable ht; 66 struct rhashtable_params filter_ht_params; 67 struct flow_dissector dissector; 68 struct list_head filters; 69 struct rcu_work rwork; 70 struct list_head list; 71 }; 72 73 struct cls_fl_head { 74 struct rhashtable ht; 75 struct list_head masks; 76 struct rcu_work rwork; 77 struct idr handle_idr; 78 }; 79 80 struct cls_fl_filter { 81 struct fl_flow_mask *mask; 82 struct rhash_head ht_node; 83 struct fl_flow_key mkey; 84 struct tcf_exts exts; 85 struct tcf_result res; 86 struct fl_flow_key key; 87 struct list_head list; 88 u32 handle; 89 u32 flags; 90 struct rcu_work rwork; 91 struct net_device *hw_dev; 92 }; 93 94 static const struct rhashtable_params mask_ht_params = { 95 .key_offset = offsetof(struct fl_flow_mask, key), 96 .key_len = sizeof(struct fl_flow_key), 97 .head_offset = offsetof(struct fl_flow_mask, ht_node), 98 .automatic_shrinking = true, 99 }; 100 101 static unsigned short int fl_mask_range(const struct fl_flow_mask *mask) 102 { 103 return mask->range.end - mask->range.start; 104 } 105 106 static void fl_mask_update_range(struct fl_flow_mask *mask) 107 { 108 const u8 *bytes = (const u8 *) &mask->key; 109 size_t size = sizeof(mask->key); 110 size_t i, first = 0, last; 111 112 for (i = 0; i < size; i++) { 113 if (bytes[i]) { 114 first = i; 115 break; 116 } 117 } 118 last = first; 119 for (i = size - 1; i != first; i--) { 120 if (bytes[i]) { 121 last = i; 122 break; 123 } 124 } 125 mask->range.start = rounddown(first, sizeof(long)); 126 mask->range.end = roundup(last + 1, sizeof(long)); 127 } 128 129 static void *fl_key_get_start(struct fl_flow_key *key, 130 const struct fl_flow_mask *mask) 131 { 132 return (u8 *) key + mask->range.start; 133 } 134 135 static void fl_set_masked_key(struct fl_flow_key *mkey, struct fl_flow_key *key, 136 struct fl_flow_mask *mask) 137 { 138 const long *lkey = fl_key_get_start(key, mask); 139 const long *lmask = fl_key_get_start(&mask->key, mask); 140 long *lmkey = fl_key_get_start(mkey, mask); 141 int i; 142 143 for (i = 0; i < fl_mask_range(mask); i += sizeof(long)) 144 *lmkey++ = *lkey++ & *lmask++; 145 } 146 147 static void fl_clear_masked_range(struct fl_flow_key *key, 148 struct fl_flow_mask *mask) 149 { 150 memset(fl_key_get_start(key, mask), 0, fl_mask_range(mask)); 151 } 152 153 static struct cls_fl_filter *fl_lookup(struct fl_flow_mask *mask, 154 struct fl_flow_key *mkey) 155 { 156 return rhashtable_lookup_fast(&mask->ht, fl_key_get_start(mkey, mask), 157 mask->filter_ht_params); 158 } 159 160 static int fl_classify(struct sk_buff *skb, const struct tcf_proto *tp, 161 struct tcf_result *res) 162 { 163 struct cls_fl_head *head = rcu_dereference_bh(tp->root); 164 struct cls_fl_filter *f; 165 struct fl_flow_mask *mask; 166 struct fl_flow_key skb_key; 167 struct fl_flow_key skb_mkey; 168 169 list_for_each_entry_rcu(mask, &head->masks, list) { 170 fl_clear_masked_range(&skb_key, mask); 171 172 skb_key.indev_ifindex = skb->skb_iif; 173 /* skb_flow_dissect() does not set n_proto in case an unknown 174 * protocol, so do it rather here. 175 */ 176 skb_key.basic.n_proto = skb->protocol; 177 skb_flow_dissect_tunnel_info(skb, &mask->dissector, &skb_key); 178 skb_flow_dissect(skb, &mask->dissector, &skb_key, 0); 179 180 fl_set_masked_key(&skb_mkey, &skb_key, mask); 181 182 f = fl_lookup(mask, &skb_mkey); 183 if (f && !tc_skip_sw(f->flags)) { 184 *res = f->res; 185 return tcf_exts_exec(skb, &f->exts, res); 186 } 187 } 188 return -1; 189 } 190 191 static int fl_init(struct tcf_proto *tp) 192 { 193 struct cls_fl_head *head; 194 195 head = kzalloc(sizeof(*head), GFP_KERNEL); 196 if (!head) 197 return -ENOBUFS; 198 199 INIT_LIST_HEAD_RCU(&head->masks); 200 rcu_assign_pointer(tp->root, head); 201 idr_init(&head->handle_idr); 202 203 return rhashtable_init(&head->ht, &mask_ht_params); 204 } 205 206 static void fl_mask_free(struct fl_flow_mask *mask) 207 { 208 rhashtable_destroy(&mask->ht); 209 kfree(mask); 210 } 211 212 static void fl_mask_free_work(struct work_struct *work) 213 { 214 struct fl_flow_mask *mask = container_of(to_rcu_work(work), 215 struct fl_flow_mask, rwork); 216 217 fl_mask_free(mask); 218 } 219 220 static bool fl_mask_put(struct cls_fl_head *head, struct fl_flow_mask *mask, 221 bool async) 222 { 223 if (!list_empty(&mask->filters)) 224 return false; 225 226 rhashtable_remove_fast(&head->ht, &mask->ht_node, mask_ht_params); 227 list_del_rcu(&mask->list); 228 if (async) 229 tcf_queue_work(&mask->rwork, fl_mask_free_work); 230 else 231 fl_mask_free(mask); 232 233 return true; 234 } 235 236 static void __fl_destroy_filter(struct cls_fl_filter *f) 237 { 238 tcf_exts_destroy(&f->exts); 239 tcf_exts_put_net(&f->exts); 240 kfree(f); 241 } 242 243 static void fl_destroy_filter_work(struct work_struct *work) 244 { 245 struct cls_fl_filter *f = container_of(to_rcu_work(work), 246 struct cls_fl_filter, rwork); 247 248 rtnl_lock(); 249 __fl_destroy_filter(f); 250 rtnl_unlock(); 251 } 252 253 static void fl_hw_destroy_filter(struct tcf_proto *tp, struct cls_fl_filter *f, 254 struct netlink_ext_ack *extack) 255 { 256 struct tc_cls_flower_offload cls_flower = {}; 257 struct tcf_block *block = tp->chain->block; 258 259 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, extack); 260 cls_flower.command = TC_CLSFLOWER_DESTROY; 261 cls_flower.cookie = (unsigned long) f; 262 263 tc_setup_cb_call(block, &f->exts, TC_SETUP_CLSFLOWER, 264 &cls_flower, false); 265 tcf_block_offload_dec(block, &f->flags); 266 } 267 268 static int fl_hw_replace_filter(struct tcf_proto *tp, 269 struct cls_fl_filter *f, 270 struct netlink_ext_ack *extack) 271 { 272 struct tc_cls_flower_offload cls_flower = {}; 273 struct tcf_block *block = tp->chain->block; 274 bool skip_sw = tc_skip_sw(f->flags); 275 int err; 276 277 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, extack); 278 cls_flower.command = TC_CLSFLOWER_REPLACE; 279 cls_flower.cookie = (unsigned long) f; 280 cls_flower.dissector = &f->mask->dissector; 281 cls_flower.mask = &f->mask->key; 282 cls_flower.key = &f->mkey; 283 cls_flower.exts = &f->exts; 284 cls_flower.classid = f->res.classid; 285 286 err = tc_setup_cb_call(block, &f->exts, TC_SETUP_CLSFLOWER, 287 &cls_flower, skip_sw); 288 if (err < 0) { 289 fl_hw_destroy_filter(tp, f, NULL); 290 return err; 291 } else if (err > 0) { 292 tcf_block_offload_inc(block, &f->flags); 293 } 294 295 if (skip_sw && !(f->flags & TCA_CLS_FLAGS_IN_HW)) 296 return -EINVAL; 297 298 return 0; 299 } 300 301 static void fl_hw_update_stats(struct tcf_proto *tp, struct cls_fl_filter *f) 302 { 303 struct tc_cls_flower_offload cls_flower = {}; 304 struct tcf_block *block = tp->chain->block; 305 306 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, NULL); 307 cls_flower.command = TC_CLSFLOWER_STATS; 308 cls_flower.cookie = (unsigned long) f; 309 cls_flower.exts = &f->exts; 310 cls_flower.classid = f->res.classid; 311 312 tc_setup_cb_call(block, &f->exts, TC_SETUP_CLSFLOWER, 313 &cls_flower, false); 314 } 315 316 static bool __fl_delete(struct tcf_proto *tp, struct cls_fl_filter *f, 317 struct netlink_ext_ack *extack) 318 { 319 struct cls_fl_head *head = rtnl_dereference(tp->root); 320 bool async = tcf_exts_get_net(&f->exts); 321 bool last; 322 323 idr_remove(&head->handle_idr, f->handle); 324 list_del_rcu(&f->list); 325 last = fl_mask_put(head, f->mask, async); 326 if (!tc_skip_hw(f->flags)) 327 fl_hw_destroy_filter(tp, f, extack); 328 tcf_unbind_filter(tp, &f->res); 329 if (async) 330 tcf_queue_work(&f->rwork, fl_destroy_filter_work); 331 else 332 __fl_destroy_filter(f); 333 334 return last; 335 } 336 337 static void fl_destroy_sleepable(struct work_struct *work) 338 { 339 struct cls_fl_head *head = container_of(to_rcu_work(work), 340 struct cls_fl_head, 341 rwork); 342 343 rhashtable_destroy(&head->ht); 344 kfree(head); 345 module_put(THIS_MODULE); 346 } 347 348 static void fl_destroy(struct tcf_proto *tp, struct netlink_ext_ack *extack) 349 { 350 struct cls_fl_head *head = rtnl_dereference(tp->root); 351 struct fl_flow_mask *mask, *next_mask; 352 struct cls_fl_filter *f, *next; 353 354 list_for_each_entry_safe(mask, next_mask, &head->masks, list) { 355 list_for_each_entry_safe(f, next, &mask->filters, list) { 356 if (__fl_delete(tp, f, extack)) 357 break; 358 } 359 } 360 idr_destroy(&head->handle_idr); 361 362 __module_get(THIS_MODULE); 363 tcf_queue_work(&head->rwork, fl_destroy_sleepable); 364 } 365 366 static void *fl_get(struct tcf_proto *tp, u32 handle) 367 { 368 struct cls_fl_head *head = rtnl_dereference(tp->root); 369 370 return idr_find(&head->handle_idr, handle); 371 } 372 373 static const struct nla_policy fl_policy[TCA_FLOWER_MAX + 1] = { 374 [TCA_FLOWER_UNSPEC] = { .type = NLA_UNSPEC }, 375 [TCA_FLOWER_CLASSID] = { .type = NLA_U32 }, 376 [TCA_FLOWER_INDEV] = { .type = NLA_STRING, 377 .len = IFNAMSIZ }, 378 [TCA_FLOWER_KEY_ETH_DST] = { .len = ETH_ALEN }, 379 [TCA_FLOWER_KEY_ETH_DST_MASK] = { .len = ETH_ALEN }, 380 [TCA_FLOWER_KEY_ETH_SRC] = { .len = ETH_ALEN }, 381 [TCA_FLOWER_KEY_ETH_SRC_MASK] = { .len = ETH_ALEN }, 382 [TCA_FLOWER_KEY_ETH_TYPE] = { .type = NLA_U16 }, 383 [TCA_FLOWER_KEY_IP_PROTO] = { .type = NLA_U8 }, 384 [TCA_FLOWER_KEY_IPV4_SRC] = { .type = NLA_U32 }, 385 [TCA_FLOWER_KEY_IPV4_SRC_MASK] = { .type = NLA_U32 }, 386 [TCA_FLOWER_KEY_IPV4_DST] = { .type = NLA_U32 }, 387 [TCA_FLOWER_KEY_IPV4_DST_MASK] = { .type = NLA_U32 }, 388 [TCA_FLOWER_KEY_IPV6_SRC] = { .len = sizeof(struct in6_addr) }, 389 [TCA_FLOWER_KEY_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) }, 390 [TCA_FLOWER_KEY_IPV6_DST] = { .len = sizeof(struct in6_addr) }, 391 [TCA_FLOWER_KEY_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) }, 392 [TCA_FLOWER_KEY_TCP_SRC] = { .type = NLA_U16 }, 393 [TCA_FLOWER_KEY_TCP_DST] = { .type = NLA_U16 }, 394 [TCA_FLOWER_KEY_UDP_SRC] = { .type = NLA_U16 }, 395 [TCA_FLOWER_KEY_UDP_DST] = { .type = NLA_U16 }, 396 [TCA_FLOWER_KEY_VLAN_ID] = { .type = NLA_U16 }, 397 [TCA_FLOWER_KEY_VLAN_PRIO] = { .type = NLA_U8 }, 398 [TCA_FLOWER_KEY_VLAN_ETH_TYPE] = { .type = NLA_U16 }, 399 [TCA_FLOWER_KEY_ENC_KEY_ID] = { .type = NLA_U32 }, 400 [TCA_FLOWER_KEY_ENC_IPV4_SRC] = { .type = NLA_U32 }, 401 [TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK] = { .type = NLA_U32 }, 402 [TCA_FLOWER_KEY_ENC_IPV4_DST] = { .type = NLA_U32 }, 403 [TCA_FLOWER_KEY_ENC_IPV4_DST_MASK] = { .type = NLA_U32 }, 404 [TCA_FLOWER_KEY_ENC_IPV6_SRC] = { .len = sizeof(struct in6_addr) }, 405 [TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) }, 406 [TCA_FLOWER_KEY_ENC_IPV6_DST] = { .len = sizeof(struct in6_addr) }, 407 [TCA_FLOWER_KEY_ENC_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) }, 408 [TCA_FLOWER_KEY_TCP_SRC_MASK] = { .type = NLA_U16 }, 409 [TCA_FLOWER_KEY_TCP_DST_MASK] = { .type = NLA_U16 }, 410 [TCA_FLOWER_KEY_UDP_SRC_MASK] = { .type = NLA_U16 }, 411 [TCA_FLOWER_KEY_UDP_DST_MASK] = { .type = NLA_U16 }, 412 [TCA_FLOWER_KEY_SCTP_SRC_MASK] = { .type = NLA_U16 }, 413 [TCA_FLOWER_KEY_SCTP_DST_MASK] = { .type = NLA_U16 }, 414 [TCA_FLOWER_KEY_SCTP_SRC] = { .type = NLA_U16 }, 415 [TCA_FLOWER_KEY_SCTP_DST] = { .type = NLA_U16 }, 416 [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT] = { .type = NLA_U16 }, 417 [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK] = { .type = NLA_U16 }, 418 [TCA_FLOWER_KEY_ENC_UDP_DST_PORT] = { .type = NLA_U16 }, 419 [TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK] = { .type = NLA_U16 }, 420 [TCA_FLOWER_KEY_FLAGS] = { .type = NLA_U32 }, 421 [TCA_FLOWER_KEY_FLAGS_MASK] = { .type = NLA_U32 }, 422 [TCA_FLOWER_KEY_ICMPV4_TYPE] = { .type = NLA_U8 }, 423 [TCA_FLOWER_KEY_ICMPV4_TYPE_MASK] = { .type = NLA_U8 }, 424 [TCA_FLOWER_KEY_ICMPV4_CODE] = { .type = NLA_U8 }, 425 [TCA_FLOWER_KEY_ICMPV4_CODE_MASK] = { .type = NLA_U8 }, 426 [TCA_FLOWER_KEY_ICMPV6_TYPE] = { .type = NLA_U8 }, 427 [TCA_FLOWER_KEY_ICMPV6_TYPE_MASK] = { .type = NLA_U8 }, 428 [TCA_FLOWER_KEY_ICMPV6_CODE] = { .type = NLA_U8 }, 429 [TCA_FLOWER_KEY_ICMPV6_CODE_MASK] = { .type = NLA_U8 }, 430 [TCA_FLOWER_KEY_ARP_SIP] = { .type = NLA_U32 }, 431 [TCA_FLOWER_KEY_ARP_SIP_MASK] = { .type = NLA_U32 }, 432 [TCA_FLOWER_KEY_ARP_TIP] = { .type = NLA_U32 }, 433 [TCA_FLOWER_KEY_ARP_TIP_MASK] = { .type = NLA_U32 }, 434 [TCA_FLOWER_KEY_ARP_OP] = { .type = NLA_U8 }, 435 [TCA_FLOWER_KEY_ARP_OP_MASK] = { .type = NLA_U8 }, 436 [TCA_FLOWER_KEY_ARP_SHA] = { .len = ETH_ALEN }, 437 [TCA_FLOWER_KEY_ARP_SHA_MASK] = { .len = ETH_ALEN }, 438 [TCA_FLOWER_KEY_ARP_THA] = { .len = ETH_ALEN }, 439 [TCA_FLOWER_KEY_ARP_THA_MASK] = { .len = ETH_ALEN }, 440 [TCA_FLOWER_KEY_MPLS_TTL] = { .type = NLA_U8 }, 441 [TCA_FLOWER_KEY_MPLS_BOS] = { .type = NLA_U8 }, 442 [TCA_FLOWER_KEY_MPLS_TC] = { .type = NLA_U8 }, 443 [TCA_FLOWER_KEY_MPLS_LABEL] = { .type = NLA_U32 }, 444 [TCA_FLOWER_KEY_TCP_FLAGS] = { .type = NLA_U16 }, 445 [TCA_FLOWER_KEY_TCP_FLAGS_MASK] = { .type = NLA_U16 }, 446 [TCA_FLOWER_KEY_IP_TOS] = { .type = NLA_U8 }, 447 [TCA_FLOWER_KEY_IP_TOS_MASK] = { .type = NLA_U8 }, 448 [TCA_FLOWER_KEY_IP_TTL] = { .type = NLA_U8 }, 449 [TCA_FLOWER_KEY_IP_TTL_MASK] = { .type = NLA_U8 }, 450 }; 451 452 static void fl_set_key_val(struct nlattr **tb, 453 void *val, int val_type, 454 void *mask, int mask_type, int len) 455 { 456 if (!tb[val_type]) 457 return; 458 memcpy(val, nla_data(tb[val_type]), len); 459 if (mask_type == TCA_FLOWER_UNSPEC || !tb[mask_type]) 460 memset(mask, 0xff, len); 461 else 462 memcpy(mask, nla_data(tb[mask_type]), len); 463 } 464 465 static int fl_set_key_mpls(struct nlattr **tb, 466 struct flow_dissector_key_mpls *key_val, 467 struct flow_dissector_key_mpls *key_mask) 468 { 469 if (tb[TCA_FLOWER_KEY_MPLS_TTL]) { 470 key_val->mpls_ttl = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TTL]); 471 key_mask->mpls_ttl = MPLS_TTL_MASK; 472 } 473 if (tb[TCA_FLOWER_KEY_MPLS_BOS]) { 474 u8 bos = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_BOS]); 475 476 if (bos & ~MPLS_BOS_MASK) 477 return -EINVAL; 478 key_val->mpls_bos = bos; 479 key_mask->mpls_bos = MPLS_BOS_MASK; 480 } 481 if (tb[TCA_FLOWER_KEY_MPLS_TC]) { 482 u8 tc = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TC]); 483 484 if (tc & ~MPLS_TC_MASK) 485 return -EINVAL; 486 key_val->mpls_tc = tc; 487 key_mask->mpls_tc = MPLS_TC_MASK; 488 } 489 if (tb[TCA_FLOWER_KEY_MPLS_LABEL]) { 490 u32 label = nla_get_u32(tb[TCA_FLOWER_KEY_MPLS_LABEL]); 491 492 if (label & ~MPLS_LABEL_MASK) 493 return -EINVAL; 494 key_val->mpls_label = label; 495 key_mask->mpls_label = MPLS_LABEL_MASK; 496 } 497 return 0; 498 } 499 500 static void fl_set_key_vlan(struct nlattr **tb, 501 struct flow_dissector_key_vlan *key_val, 502 struct flow_dissector_key_vlan *key_mask) 503 { 504 #define VLAN_PRIORITY_MASK 0x7 505 506 if (tb[TCA_FLOWER_KEY_VLAN_ID]) { 507 key_val->vlan_id = 508 nla_get_u16(tb[TCA_FLOWER_KEY_VLAN_ID]) & VLAN_VID_MASK; 509 key_mask->vlan_id = VLAN_VID_MASK; 510 } 511 if (tb[TCA_FLOWER_KEY_VLAN_PRIO]) { 512 key_val->vlan_priority = 513 nla_get_u8(tb[TCA_FLOWER_KEY_VLAN_PRIO]) & 514 VLAN_PRIORITY_MASK; 515 key_mask->vlan_priority = VLAN_PRIORITY_MASK; 516 } 517 } 518 519 static void fl_set_key_flag(u32 flower_key, u32 flower_mask, 520 u32 *dissector_key, u32 *dissector_mask, 521 u32 flower_flag_bit, u32 dissector_flag_bit) 522 { 523 if (flower_mask & flower_flag_bit) { 524 *dissector_mask |= dissector_flag_bit; 525 if (flower_key & flower_flag_bit) 526 *dissector_key |= dissector_flag_bit; 527 } 528 } 529 530 static int fl_set_key_flags(struct nlattr **tb, 531 u32 *flags_key, u32 *flags_mask) 532 { 533 u32 key, mask; 534 535 /* mask is mandatory for flags */ 536 if (!tb[TCA_FLOWER_KEY_FLAGS_MASK]) 537 return -EINVAL; 538 539 key = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS])); 540 mask = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS_MASK])); 541 542 *flags_key = 0; 543 *flags_mask = 0; 544 545 fl_set_key_flag(key, mask, flags_key, flags_mask, 546 TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT); 547 fl_set_key_flag(key, mask, flags_key, flags_mask, 548 TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST, 549 FLOW_DIS_FIRST_FRAG); 550 551 return 0; 552 } 553 554 static void fl_set_key_ip(struct nlattr **tb, 555 struct flow_dissector_key_ip *key, 556 struct flow_dissector_key_ip *mask) 557 { 558 fl_set_key_val(tb, &key->tos, TCA_FLOWER_KEY_IP_TOS, 559 &mask->tos, TCA_FLOWER_KEY_IP_TOS_MASK, 560 sizeof(key->tos)); 561 562 fl_set_key_val(tb, &key->ttl, TCA_FLOWER_KEY_IP_TTL, 563 &mask->ttl, TCA_FLOWER_KEY_IP_TTL_MASK, 564 sizeof(key->ttl)); 565 } 566 567 static int fl_set_key(struct net *net, struct nlattr **tb, 568 struct fl_flow_key *key, struct fl_flow_key *mask, 569 struct netlink_ext_ack *extack) 570 { 571 __be16 ethertype; 572 int ret = 0; 573 #ifdef CONFIG_NET_CLS_IND 574 if (tb[TCA_FLOWER_INDEV]) { 575 int err = tcf_change_indev(net, tb[TCA_FLOWER_INDEV], extack); 576 if (err < 0) 577 return err; 578 key->indev_ifindex = err; 579 mask->indev_ifindex = 0xffffffff; 580 } 581 #endif 582 583 fl_set_key_val(tb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST, 584 mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK, 585 sizeof(key->eth.dst)); 586 fl_set_key_val(tb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC, 587 mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK, 588 sizeof(key->eth.src)); 589 590 if (tb[TCA_FLOWER_KEY_ETH_TYPE]) { 591 ethertype = nla_get_be16(tb[TCA_FLOWER_KEY_ETH_TYPE]); 592 593 if (ethertype == htons(ETH_P_8021Q)) { 594 fl_set_key_vlan(tb, &key->vlan, &mask->vlan); 595 fl_set_key_val(tb, &key->basic.n_proto, 596 TCA_FLOWER_KEY_VLAN_ETH_TYPE, 597 &mask->basic.n_proto, TCA_FLOWER_UNSPEC, 598 sizeof(key->basic.n_proto)); 599 } else { 600 key->basic.n_proto = ethertype; 601 mask->basic.n_proto = cpu_to_be16(~0); 602 } 603 } 604 605 if (key->basic.n_proto == htons(ETH_P_IP) || 606 key->basic.n_proto == htons(ETH_P_IPV6)) { 607 fl_set_key_val(tb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO, 608 &mask->basic.ip_proto, TCA_FLOWER_UNSPEC, 609 sizeof(key->basic.ip_proto)); 610 fl_set_key_ip(tb, &key->ip, &mask->ip); 611 } 612 613 if (tb[TCA_FLOWER_KEY_IPV4_SRC] || tb[TCA_FLOWER_KEY_IPV4_DST]) { 614 key->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; 615 mask->control.addr_type = ~0; 616 fl_set_key_val(tb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC, 617 &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK, 618 sizeof(key->ipv4.src)); 619 fl_set_key_val(tb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST, 620 &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK, 621 sizeof(key->ipv4.dst)); 622 } else if (tb[TCA_FLOWER_KEY_IPV6_SRC] || tb[TCA_FLOWER_KEY_IPV6_DST]) { 623 key->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 624 mask->control.addr_type = ~0; 625 fl_set_key_val(tb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC, 626 &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK, 627 sizeof(key->ipv6.src)); 628 fl_set_key_val(tb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST, 629 &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK, 630 sizeof(key->ipv6.dst)); 631 } 632 633 if (key->basic.ip_proto == IPPROTO_TCP) { 634 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC, 635 &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK, 636 sizeof(key->tp.src)); 637 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST, 638 &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK, 639 sizeof(key->tp.dst)); 640 fl_set_key_val(tb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS, 641 &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK, 642 sizeof(key->tcp.flags)); 643 } else if (key->basic.ip_proto == IPPROTO_UDP) { 644 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC, 645 &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK, 646 sizeof(key->tp.src)); 647 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST, 648 &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK, 649 sizeof(key->tp.dst)); 650 } else if (key->basic.ip_proto == IPPROTO_SCTP) { 651 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC, 652 &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK, 653 sizeof(key->tp.src)); 654 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST, 655 &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK, 656 sizeof(key->tp.dst)); 657 } else if (key->basic.n_proto == htons(ETH_P_IP) && 658 key->basic.ip_proto == IPPROTO_ICMP) { 659 fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV4_TYPE, 660 &mask->icmp.type, 661 TCA_FLOWER_KEY_ICMPV4_TYPE_MASK, 662 sizeof(key->icmp.type)); 663 fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV4_CODE, 664 &mask->icmp.code, 665 TCA_FLOWER_KEY_ICMPV4_CODE_MASK, 666 sizeof(key->icmp.code)); 667 } else if (key->basic.n_proto == htons(ETH_P_IPV6) && 668 key->basic.ip_proto == IPPROTO_ICMPV6) { 669 fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV6_TYPE, 670 &mask->icmp.type, 671 TCA_FLOWER_KEY_ICMPV6_TYPE_MASK, 672 sizeof(key->icmp.type)); 673 fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV6_CODE, 674 &mask->icmp.code, 675 TCA_FLOWER_KEY_ICMPV6_CODE_MASK, 676 sizeof(key->icmp.code)); 677 } else if (key->basic.n_proto == htons(ETH_P_MPLS_UC) || 678 key->basic.n_proto == htons(ETH_P_MPLS_MC)) { 679 ret = fl_set_key_mpls(tb, &key->mpls, &mask->mpls); 680 if (ret) 681 return ret; 682 } else if (key->basic.n_proto == htons(ETH_P_ARP) || 683 key->basic.n_proto == htons(ETH_P_RARP)) { 684 fl_set_key_val(tb, &key->arp.sip, TCA_FLOWER_KEY_ARP_SIP, 685 &mask->arp.sip, TCA_FLOWER_KEY_ARP_SIP_MASK, 686 sizeof(key->arp.sip)); 687 fl_set_key_val(tb, &key->arp.tip, TCA_FLOWER_KEY_ARP_TIP, 688 &mask->arp.tip, TCA_FLOWER_KEY_ARP_TIP_MASK, 689 sizeof(key->arp.tip)); 690 fl_set_key_val(tb, &key->arp.op, TCA_FLOWER_KEY_ARP_OP, 691 &mask->arp.op, TCA_FLOWER_KEY_ARP_OP_MASK, 692 sizeof(key->arp.op)); 693 fl_set_key_val(tb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA, 694 mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK, 695 sizeof(key->arp.sha)); 696 fl_set_key_val(tb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA, 697 mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK, 698 sizeof(key->arp.tha)); 699 } 700 701 if (tb[TCA_FLOWER_KEY_ENC_IPV4_SRC] || 702 tb[TCA_FLOWER_KEY_ENC_IPV4_DST]) { 703 key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; 704 mask->enc_control.addr_type = ~0; 705 fl_set_key_val(tb, &key->enc_ipv4.src, 706 TCA_FLOWER_KEY_ENC_IPV4_SRC, 707 &mask->enc_ipv4.src, 708 TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK, 709 sizeof(key->enc_ipv4.src)); 710 fl_set_key_val(tb, &key->enc_ipv4.dst, 711 TCA_FLOWER_KEY_ENC_IPV4_DST, 712 &mask->enc_ipv4.dst, 713 TCA_FLOWER_KEY_ENC_IPV4_DST_MASK, 714 sizeof(key->enc_ipv4.dst)); 715 } 716 717 if (tb[TCA_FLOWER_KEY_ENC_IPV6_SRC] || 718 tb[TCA_FLOWER_KEY_ENC_IPV6_DST]) { 719 key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 720 mask->enc_control.addr_type = ~0; 721 fl_set_key_val(tb, &key->enc_ipv6.src, 722 TCA_FLOWER_KEY_ENC_IPV6_SRC, 723 &mask->enc_ipv6.src, 724 TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK, 725 sizeof(key->enc_ipv6.src)); 726 fl_set_key_val(tb, &key->enc_ipv6.dst, 727 TCA_FLOWER_KEY_ENC_IPV6_DST, 728 &mask->enc_ipv6.dst, 729 TCA_FLOWER_KEY_ENC_IPV6_DST_MASK, 730 sizeof(key->enc_ipv6.dst)); 731 } 732 733 fl_set_key_val(tb, &key->enc_key_id.keyid, TCA_FLOWER_KEY_ENC_KEY_ID, 734 &mask->enc_key_id.keyid, TCA_FLOWER_UNSPEC, 735 sizeof(key->enc_key_id.keyid)); 736 737 fl_set_key_val(tb, &key->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT, 738 &mask->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK, 739 sizeof(key->enc_tp.src)); 740 741 fl_set_key_val(tb, &key->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT, 742 &mask->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK, 743 sizeof(key->enc_tp.dst)); 744 745 if (tb[TCA_FLOWER_KEY_FLAGS]) 746 ret = fl_set_key_flags(tb, &key->control.flags, &mask->control.flags); 747 748 return ret; 749 } 750 751 static void fl_mask_copy(struct fl_flow_mask *dst, 752 struct fl_flow_mask *src) 753 { 754 const void *psrc = fl_key_get_start(&src->key, src); 755 void *pdst = fl_key_get_start(&dst->key, src); 756 757 memcpy(pdst, psrc, fl_mask_range(src)); 758 dst->range = src->range; 759 } 760 761 static const struct rhashtable_params fl_ht_params = { 762 .key_offset = offsetof(struct cls_fl_filter, mkey), /* base offset */ 763 .head_offset = offsetof(struct cls_fl_filter, ht_node), 764 .automatic_shrinking = true, 765 }; 766 767 static int fl_init_mask_hashtable(struct fl_flow_mask *mask) 768 { 769 mask->filter_ht_params = fl_ht_params; 770 mask->filter_ht_params.key_len = fl_mask_range(mask); 771 mask->filter_ht_params.key_offset += mask->range.start; 772 773 return rhashtable_init(&mask->ht, &mask->filter_ht_params); 774 } 775 776 #define FL_KEY_MEMBER_OFFSET(member) offsetof(struct fl_flow_key, member) 777 #define FL_KEY_MEMBER_SIZE(member) (sizeof(((struct fl_flow_key *) 0)->member)) 778 779 #define FL_KEY_IS_MASKED(mask, member) \ 780 memchr_inv(((char *)mask) + FL_KEY_MEMBER_OFFSET(member), \ 781 0, FL_KEY_MEMBER_SIZE(member)) \ 782 783 #define FL_KEY_SET(keys, cnt, id, member) \ 784 do { \ 785 keys[cnt].key_id = id; \ 786 keys[cnt].offset = FL_KEY_MEMBER_OFFSET(member); \ 787 cnt++; \ 788 } while(0); 789 790 #define FL_KEY_SET_IF_MASKED(mask, keys, cnt, id, member) \ 791 do { \ 792 if (FL_KEY_IS_MASKED(mask, member)) \ 793 FL_KEY_SET(keys, cnt, id, member); \ 794 } while(0); 795 796 static void fl_init_dissector(struct fl_flow_mask *mask) 797 { 798 struct flow_dissector_key keys[FLOW_DISSECTOR_KEY_MAX]; 799 size_t cnt = 0; 800 801 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_CONTROL, control); 802 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_BASIC, basic); 803 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt, 804 FLOW_DISSECTOR_KEY_ETH_ADDRS, eth); 805 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt, 806 FLOW_DISSECTOR_KEY_IPV4_ADDRS, ipv4); 807 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt, 808 FLOW_DISSECTOR_KEY_IPV6_ADDRS, ipv6); 809 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt, 810 FLOW_DISSECTOR_KEY_PORTS, tp); 811 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt, 812 FLOW_DISSECTOR_KEY_IP, ip); 813 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt, 814 FLOW_DISSECTOR_KEY_TCP, tcp); 815 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt, 816 FLOW_DISSECTOR_KEY_ICMP, icmp); 817 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt, 818 FLOW_DISSECTOR_KEY_ARP, arp); 819 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt, 820 FLOW_DISSECTOR_KEY_MPLS, mpls); 821 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt, 822 FLOW_DISSECTOR_KEY_VLAN, vlan); 823 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt, 824 FLOW_DISSECTOR_KEY_ENC_KEYID, enc_key_id); 825 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt, 826 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS, enc_ipv4); 827 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt, 828 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS, enc_ipv6); 829 if (FL_KEY_IS_MASKED(&mask->key, enc_ipv4) || 830 FL_KEY_IS_MASKED(&mask->key, enc_ipv6)) 831 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_ENC_CONTROL, 832 enc_control); 833 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt, 834 FLOW_DISSECTOR_KEY_ENC_PORTS, enc_tp); 835 836 skb_flow_dissector_init(&mask->dissector, keys, cnt); 837 } 838 839 static struct fl_flow_mask *fl_create_new_mask(struct cls_fl_head *head, 840 struct fl_flow_mask *mask) 841 { 842 struct fl_flow_mask *newmask; 843 int err; 844 845 newmask = kzalloc(sizeof(*newmask), GFP_KERNEL); 846 if (!newmask) 847 return ERR_PTR(-ENOMEM); 848 849 fl_mask_copy(newmask, mask); 850 851 err = fl_init_mask_hashtable(newmask); 852 if (err) 853 goto errout_free; 854 855 fl_init_dissector(newmask); 856 857 INIT_LIST_HEAD_RCU(&newmask->filters); 858 859 err = rhashtable_insert_fast(&head->ht, &newmask->ht_node, 860 mask_ht_params); 861 if (err) 862 goto errout_destroy; 863 864 list_add_tail_rcu(&newmask->list, &head->masks); 865 866 return newmask; 867 868 errout_destroy: 869 rhashtable_destroy(&newmask->ht); 870 errout_free: 871 kfree(newmask); 872 873 return ERR_PTR(err); 874 } 875 876 static int fl_check_assign_mask(struct cls_fl_head *head, 877 struct cls_fl_filter *fnew, 878 struct cls_fl_filter *fold, 879 struct fl_flow_mask *mask) 880 { 881 struct fl_flow_mask *newmask; 882 883 fnew->mask = rhashtable_lookup_fast(&head->ht, mask, mask_ht_params); 884 if (!fnew->mask) { 885 if (fold) 886 return -EINVAL; 887 888 newmask = fl_create_new_mask(head, mask); 889 if (IS_ERR(newmask)) 890 return PTR_ERR(newmask); 891 892 fnew->mask = newmask; 893 } else if (fold && fold->mask != fnew->mask) { 894 return -EINVAL; 895 } 896 897 return 0; 898 } 899 900 static int fl_set_parms(struct net *net, struct tcf_proto *tp, 901 struct cls_fl_filter *f, struct fl_flow_mask *mask, 902 unsigned long base, struct nlattr **tb, 903 struct nlattr *est, bool ovr, 904 struct netlink_ext_ack *extack) 905 { 906 int err; 907 908 err = tcf_exts_validate(net, tp, tb, est, &f->exts, ovr, extack); 909 if (err < 0) 910 return err; 911 912 if (tb[TCA_FLOWER_CLASSID]) { 913 f->res.classid = nla_get_u32(tb[TCA_FLOWER_CLASSID]); 914 tcf_bind_filter(tp, &f->res, base); 915 } 916 917 err = fl_set_key(net, tb, &f->key, &mask->key, extack); 918 if (err) 919 return err; 920 921 fl_mask_update_range(mask); 922 fl_set_masked_key(&f->mkey, &f->key, mask); 923 924 return 0; 925 } 926 927 static int fl_change(struct net *net, struct sk_buff *in_skb, 928 struct tcf_proto *tp, unsigned long base, 929 u32 handle, struct nlattr **tca, 930 void **arg, bool ovr, struct netlink_ext_ack *extack) 931 { 932 struct cls_fl_head *head = rtnl_dereference(tp->root); 933 struct cls_fl_filter *fold = *arg; 934 struct cls_fl_filter *fnew; 935 struct nlattr **tb; 936 struct fl_flow_mask mask = {}; 937 int err; 938 939 if (!tca[TCA_OPTIONS]) 940 return -EINVAL; 941 942 tb = kcalloc(TCA_FLOWER_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL); 943 if (!tb) 944 return -ENOBUFS; 945 946 err = nla_parse_nested(tb, TCA_FLOWER_MAX, tca[TCA_OPTIONS], 947 fl_policy, NULL); 948 if (err < 0) 949 goto errout_tb; 950 951 if (fold && handle && fold->handle != handle) { 952 err = -EINVAL; 953 goto errout_tb; 954 } 955 956 fnew = kzalloc(sizeof(*fnew), GFP_KERNEL); 957 if (!fnew) { 958 err = -ENOBUFS; 959 goto errout_tb; 960 } 961 962 err = tcf_exts_init(&fnew->exts, TCA_FLOWER_ACT, 0); 963 if (err < 0) 964 goto errout; 965 966 if (!handle) { 967 handle = 1; 968 err = idr_alloc_u32(&head->handle_idr, fnew, &handle, 969 INT_MAX, GFP_KERNEL); 970 } else if (!fold) { 971 /* user specifies a handle and it doesn't exist */ 972 err = idr_alloc_u32(&head->handle_idr, fnew, &handle, 973 handle, GFP_KERNEL); 974 } 975 if (err) 976 goto errout; 977 fnew->handle = handle; 978 979 if (tb[TCA_FLOWER_FLAGS]) { 980 fnew->flags = nla_get_u32(tb[TCA_FLOWER_FLAGS]); 981 982 if (!tc_flags_valid(fnew->flags)) { 983 err = -EINVAL; 984 goto errout_idr; 985 } 986 } 987 988 err = fl_set_parms(net, tp, fnew, &mask, base, tb, tca[TCA_RATE], ovr, 989 extack); 990 if (err) 991 goto errout_idr; 992 993 err = fl_check_assign_mask(head, fnew, fold, &mask); 994 if (err) 995 goto errout_idr; 996 997 if (!tc_skip_sw(fnew->flags)) { 998 if (!fold && fl_lookup(fnew->mask, &fnew->mkey)) { 999 err = -EEXIST; 1000 goto errout_mask; 1001 } 1002 1003 err = rhashtable_insert_fast(&fnew->mask->ht, &fnew->ht_node, 1004 fnew->mask->filter_ht_params); 1005 if (err) 1006 goto errout_mask; 1007 } 1008 1009 if (!tc_skip_hw(fnew->flags)) { 1010 err = fl_hw_replace_filter(tp, fnew, extack); 1011 if (err) 1012 goto errout_mask; 1013 } 1014 1015 if (!tc_in_hw(fnew->flags)) 1016 fnew->flags |= TCA_CLS_FLAGS_NOT_IN_HW; 1017 1018 if (fold) { 1019 if (!tc_skip_sw(fold->flags)) 1020 rhashtable_remove_fast(&fold->mask->ht, 1021 &fold->ht_node, 1022 fold->mask->filter_ht_params); 1023 if (!tc_skip_hw(fold->flags)) 1024 fl_hw_destroy_filter(tp, fold, NULL); 1025 } 1026 1027 *arg = fnew; 1028 1029 if (fold) { 1030 idr_replace(&head->handle_idr, fnew, fnew->handle); 1031 list_replace_rcu(&fold->list, &fnew->list); 1032 tcf_unbind_filter(tp, &fold->res); 1033 tcf_exts_get_net(&fold->exts); 1034 tcf_queue_work(&fold->rwork, fl_destroy_filter_work); 1035 } else { 1036 list_add_tail_rcu(&fnew->list, &fnew->mask->filters); 1037 } 1038 1039 kfree(tb); 1040 return 0; 1041 1042 errout_mask: 1043 fl_mask_put(head, fnew->mask, false); 1044 1045 errout_idr: 1046 if (!fold) 1047 idr_remove(&head->handle_idr, fnew->handle); 1048 errout: 1049 tcf_exts_destroy(&fnew->exts); 1050 kfree(fnew); 1051 errout_tb: 1052 kfree(tb); 1053 return err; 1054 } 1055 1056 static int fl_delete(struct tcf_proto *tp, void *arg, bool *last, 1057 struct netlink_ext_ack *extack) 1058 { 1059 struct cls_fl_head *head = rtnl_dereference(tp->root); 1060 struct cls_fl_filter *f = arg; 1061 1062 if (!tc_skip_sw(f->flags)) 1063 rhashtable_remove_fast(&f->mask->ht, &f->ht_node, 1064 f->mask->filter_ht_params); 1065 __fl_delete(tp, f, extack); 1066 *last = list_empty(&head->masks); 1067 return 0; 1068 } 1069 1070 static void fl_walk(struct tcf_proto *tp, struct tcf_walker *arg) 1071 { 1072 struct cls_fl_head *head = rtnl_dereference(tp->root); 1073 struct cls_fl_filter *f; 1074 struct fl_flow_mask *mask; 1075 1076 list_for_each_entry_rcu(mask, &head->masks, list) { 1077 list_for_each_entry_rcu(f, &mask->filters, list) { 1078 if (arg->count < arg->skip) 1079 goto skip; 1080 if (arg->fn(tp, f, arg) < 0) { 1081 arg->stop = 1; 1082 break; 1083 } 1084 skip: 1085 arg->count++; 1086 } 1087 } 1088 } 1089 1090 static int fl_dump_key_val(struct sk_buff *skb, 1091 void *val, int val_type, 1092 void *mask, int mask_type, int len) 1093 { 1094 int err; 1095 1096 if (!memchr_inv(mask, 0, len)) 1097 return 0; 1098 err = nla_put(skb, val_type, len, val); 1099 if (err) 1100 return err; 1101 if (mask_type != TCA_FLOWER_UNSPEC) { 1102 err = nla_put(skb, mask_type, len, mask); 1103 if (err) 1104 return err; 1105 } 1106 return 0; 1107 } 1108 1109 static int fl_dump_key_mpls(struct sk_buff *skb, 1110 struct flow_dissector_key_mpls *mpls_key, 1111 struct flow_dissector_key_mpls *mpls_mask) 1112 { 1113 int err; 1114 1115 if (!memchr_inv(mpls_mask, 0, sizeof(*mpls_mask))) 1116 return 0; 1117 if (mpls_mask->mpls_ttl) { 1118 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TTL, 1119 mpls_key->mpls_ttl); 1120 if (err) 1121 return err; 1122 } 1123 if (mpls_mask->mpls_tc) { 1124 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TC, 1125 mpls_key->mpls_tc); 1126 if (err) 1127 return err; 1128 } 1129 if (mpls_mask->mpls_label) { 1130 err = nla_put_u32(skb, TCA_FLOWER_KEY_MPLS_LABEL, 1131 mpls_key->mpls_label); 1132 if (err) 1133 return err; 1134 } 1135 if (mpls_mask->mpls_bos) { 1136 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_BOS, 1137 mpls_key->mpls_bos); 1138 if (err) 1139 return err; 1140 } 1141 return 0; 1142 } 1143 1144 static int fl_dump_key_ip(struct sk_buff *skb, 1145 struct flow_dissector_key_ip *key, 1146 struct flow_dissector_key_ip *mask) 1147 { 1148 if (fl_dump_key_val(skb, &key->tos, TCA_FLOWER_KEY_IP_TOS, &mask->tos, 1149 TCA_FLOWER_KEY_IP_TOS_MASK, sizeof(key->tos)) || 1150 fl_dump_key_val(skb, &key->ttl, TCA_FLOWER_KEY_IP_TTL, &mask->ttl, 1151 TCA_FLOWER_KEY_IP_TTL_MASK, sizeof(key->ttl))) 1152 return -1; 1153 1154 return 0; 1155 } 1156 1157 static int fl_dump_key_vlan(struct sk_buff *skb, 1158 struct flow_dissector_key_vlan *vlan_key, 1159 struct flow_dissector_key_vlan *vlan_mask) 1160 { 1161 int err; 1162 1163 if (!memchr_inv(vlan_mask, 0, sizeof(*vlan_mask))) 1164 return 0; 1165 if (vlan_mask->vlan_id) { 1166 err = nla_put_u16(skb, TCA_FLOWER_KEY_VLAN_ID, 1167 vlan_key->vlan_id); 1168 if (err) 1169 return err; 1170 } 1171 if (vlan_mask->vlan_priority) { 1172 err = nla_put_u8(skb, TCA_FLOWER_KEY_VLAN_PRIO, 1173 vlan_key->vlan_priority); 1174 if (err) 1175 return err; 1176 } 1177 return 0; 1178 } 1179 1180 static void fl_get_key_flag(u32 dissector_key, u32 dissector_mask, 1181 u32 *flower_key, u32 *flower_mask, 1182 u32 flower_flag_bit, u32 dissector_flag_bit) 1183 { 1184 if (dissector_mask & dissector_flag_bit) { 1185 *flower_mask |= flower_flag_bit; 1186 if (dissector_key & dissector_flag_bit) 1187 *flower_key |= flower_flag_bit; 1188 } 1189 } 1190 1191 static int fl_dump_key_flags(struct sk_buff *skb, u32 flags_key, u32 flags_mask) 1192 { 1193 u32 key, mask; 1194 __be32 _key, _mask; 1195 int err; 1196 1197 if (!memchr_inv(&flags_mask, 0, sizeof(flags_mask))) 1198 return 0; 1199 1200 key = 0; 1201 mask = 0; 1202 1203 fl_get_key_flag(flags_key, flags_mask, &key, &mask, 1204 TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT); 1205 fl_get_key_flag(flags_key, flags_mask, &key, &mask, 1206 TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST, 1207 FLOW_DIS_FIRST_FRAG); 1208 1209 _key = cpu_to_be32(key); 1210 _mask = cpu_to_be32(mask); 1211 1212 err = nla_put(skb, TCA_FLOWER_KEY_FLAGS, 4, &_key); 1213 if (err) 1214 return err; 1215 1216 return nla_put(skb, TCA_FLOWER_KEY_FLAGS_MASK, 4, &_mask); 1217 } 1218 1219 static int fl_dump(struct net *net, struct tcf_proto *tp, void *fh, 1220 struct sk_buff *skb, struct tcmsg *t) 1221 { 1222 struct cls_fl_filter *f = fh; 1223 struct nlattr *nest; 1224 struct fl_flow_key *key, *mask; 1225 1226 if (!f) 1227 return skb->len; 1228 1229 t->tcm_handle = f->handle; 1230 1231 nest = nla_nest_start(skb, TCA_OPTIONS); 1232 if (!nest) 1233 goto nla_put_failure; 1234 1235 if (f->res.classid && 1236 nla_put_u32(skb, TCA_FLOWER_CLASSID, f->res.classid)) 1237 goto nla_put_failure; 1238 1239 key = &f->key; 1240 mask = &f->mask->key; 1241 1242 if (mask->indev_ifindex) { 1243 struct net_device *dev; 1244 1245 dev = __dev_get_by_index(net, key->indev_ifindex); 1246 if (dev && nla_put_string(skb, TCA_FLOWER_INDEV, dev->name)) 1247 goto nla_put_failure; 1248 } 1249 1250 if (!tc_skip_hw(f->flags)) 1251 fl_hw_update_stats(tp, f); 1252 1253 if (fl_dump_key_val(skb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST, 1254 mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK, 1255 sizeof(key->eth.dst)) || 1256 fl_dump_key_val(skb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC, 1257 mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK, 1258 sizeof(key->eth.src)) || 1259 fl_dump_key_val(skb, &key->basic.n_proto, TCA_FLOWER_KEY_ETH_TYPE, 1260 &mask->basic.n_proto, TCA_FLOWER_UNSPEC, 1261 sizeof(key->basic.n_proto))) 1262 goto nla_put_failure; 1263 1264 if (fl_dump_key_mpls(skb, &key->mpls, &mask->mpls)) 1265 goto nla_put_failure; 1266 1267 if (fl_dump_key_vlan(skb, &key->vlan, &mask->vlan)) 1268 goto nla_put_failure; 1269 1270 if ((key->basic.n_proto == htons(ETH_P_IP) || 1271 key->basic.n_proto == htons(ETH_P_IPV6)) && 1272 (fl_dump_key_val(skb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO, 1273 &mask->basic.ip_proto, TCA_FLOWER_UNSPEC, 1274 sizeof(key->basic.ip_proto)) || 1275 fl_dump_key_ip(skb, &key->ip, &mask->ip))) 1276 goto nla_put_failure; 1277 1278 if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS && 1279 (fl_dump_key_val(skb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC, 1280 &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK, 1281 sizeof(key->ipv4.src)) || 1282 fl_dump_key_val(skb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST, 1283 &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK, 1284 sizeof(key->ipv4.dst)))) 1285 goto nla_put_failure; 1286 else if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS && 1287 (fl_dump_key_val(skb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC, 1288 &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK, 1289 sizeof(key->ipv6.src)) || 1290 fl_dump_key_val(skb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST, 1291 &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK, 1292 sizeof(key->ipv6.dst)))) 1293 goto nla_put_failure; 1294 1295 if (key->basic.ip_proto == IPPROTO_TCP && 1296 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC, 1297 &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK, 1298 sizeof(key->tp.src)) || 1299 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST, 1300 &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK, 1301 sizeof(key->tp.dst)) || 1302 fl_dump_key_val(skb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS, 1303 &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK, 1304 sizeof(key->tcp.flags)))) 1305 goto nla_put_failure; 1306 else if (key->basic.ip_proto == IPPROTO_UDP && 1307 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC, 1308 &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK, 1309 sizeof(key->tp.src)) || 1310 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST, 1311 &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK, 1312 sizeof(key->tp.dst)))) 1313 goto nla_put_failure; 1314 else if (key->basic.ip_proto == IPPROTO_SCTP && 1315 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC, 1316 &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK, 1317 sizeof(key->tp.src)) || 1318 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST, 1319 &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK, 1320 sizeof(key->tp.dst)))) 1321 goto nla_put_failure; 1322 else if (key->basic.n_proto == htons(ETH_P_IP) && 1323 key->basic.ip_proto == IPPROTO_ICMP && 1324 (fl_dump_key_val(skb, &key->icmp.type, 1325 TCA_FLOWER_KEY_ICMPV4_TYPE, &mask->icmp.type, 1326 TCA_FLOWER_KEY_ICMPV4_TYPE_MASK, 1327 sizeof(key->icmp.type)) || 1328 fl_dump_key_val(skb, &key->icmp.code, 1329 TCA_FLOWER_KEY_ICMPV4_CODE, &mask->icmp.code, 1330 TCA_FLOWER_KEY_ICMPV4_CODE_MASK, 1331 sizeof(key->icmp.code)))) 1332 goto nla_put_failure; 1333 else if (key->basic.n_proto == htons(ETH_P_IPV6) && 1334 key->basic.ip_proto == IPPROTO_ICMPV6 && 1335 (fl_dump_key_val(skb, &key->icmp.type, 1336 TCA_FLOWER_KEY_ICMPV6_TYPE, &mask->icmp.type, 1337 TCA_FLOWER_KEY_ICMPV6_TYPE_MASK, 1338 sizeof(key->icmp.type)) || 1339 fl_dump_key_val(skb, &key->icmp.code, 1340 TCA_FLOWER_KEY_ICMPV6_CODE, &mask->icmp.code, 1341 TCA_FLOWER_KEY_ICMPV6_CODE_MASK, 1342 sizeof(key->icmp.code)))) 1343 goto nla_put_failure; 1344 else if ((key->basic.n_proto == htons(ETH_P_ARP) || 1345 key->basic.n_proto == htons(ETH_P_RARP)) && 1346 (fl_dump_key_val(skb, &key->arp.sip, 1347 TCA_FLOWER_KEY_ARP_SIP, &mask->arp.sip, 1348 TCA_FLOWER_KEY_ARP_SIP_MASK, 1349 sizeof(key->arp.sip)) || 1350 fl_dump_key_val(skb, &key->arp.tip, 1351 TCA_FLOWER_KEY_ARP_TIP, &mask->arp.tip, 1352 TCA_FLOWER_KEY_ARP_TIP_MASK, 1353 sizeof(key->arp.tip)) || 1354 fl_dump_key_val(skb, &key->arp.op, 1355 TCA_FLOWER_KEY_ARP_OP, &mask->arp.op, 1356 TCA_FLOWER_KEY_ARP_OP_MASK, 1357 sizeof(key->arp.op)) || 1358 fl_dump_key_val(skb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA, 1359 mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK, 1360 sizeof(key->arp.sha)) || 1361 fl_dump_key_val(skb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA, 1362 mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK, 1363 sizeof(key->arp.tha)))) 1364 goto nla_put_failure; 1365 1366 if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS && 1367 (fl_dump_key_val(skb, &key->enc_ipv4.src, 1368 TCA_FLOWER_KEY_ENC_IPV4_SRC, &mask->enc_ipv4.src, 1369 TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK, 1370 sizeof(key->enc_ipv4.src)) || 1371 fl_dump_key_val(skb, &key->enc_ipv4.dst, 1372 TCA_FLOWER_KEY_ENC_IPV4_DST, &mask->enc_ipv4.dst, 1373 TCA_FLOWER_KEY_ENC_IPV4_DST_MASK, 1374 sizeof(key->enc_ipv4.dst)))) 1375 goto nla_put_failure; 1376 else if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS && 1377 (fl_dump_key_val(skb, &key->enc_ipv6.src, 1378 TCA_FLOWER_KEY_ENC_IPV6_SRC, &mask->enc_ipv6.src, 1379 TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK, 1380 sizeof(key->enc_ipv6.src)) || 1381 fl_dump_key_val(skb, &key->enc_ipv6.dst, 1382 TCA_FLOWER_KEY_ENC_IPV6_DST, 1383 &mask->enc_ipv6.dst, 1384 TCA_FLOWER_KEY_ENC_IPV6_DST_MASK, 1385 sizeof(key->enc_ipv6.dst)))) 1386 goto nla_put_failure; 1387 1388 if (fl_dump_key_val(skb, &key->enc_key_id, TCA_FLOWER_KEY_ENC_KEY_ID, 1389 &mask->enc_key_id, TCA_FLOWER_UNSPEC, 1390 sizeof(key->enc_key_id)) || 1391 fl_dump_key_val(skb, &key->enc_tp.src, 1392 TCA_FLOWER_KEY_ENC_UDP_SRC_PORT, 1393 &mask->enc_tp.src, 1394 TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK, 1395 sizeof(key->enc_tp.src)) || 1396 fl_dump_key_val(skb, &key->enc_tp.dst, 1397 TCA_FLOWER_KEY_ENC_UDP_DST_PORT, 1398 &mask->enc_tp.dst, 1399 TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK, 1400 sizeof(key->enc_tp.dst))) 1401 goto nla_put_failure; 1402 1403 if (fl_dump_key_flags(skb, key->control.flags, mask->control.flags)) 1404 goto nla_put_failure; 1405 1406 if (f->flags && nla_put_u32(skb, TCA_FLOWER_FLAGS, f->flags)) 1407 goto nla_put_failure; 1408 1409 if (tcf_exts_dump(skb, &f->exts)) 1410 goto nla_put_failure; 1411 1412 nla_nest_end(skb, nest); 1413 1414 if (tcf_exts_dump_stats(skb, &f->exts) < 0) 1415 goto nla_put_failure; 1416 1417 return skb->len; 1418 1419 nla_put_failure: 1420 nla_nest_cancel(skb, nest); 1421 return -1; 1422 } 1423 1424 static void fl_bind_class(void *fh, u32 classid, unsigned long cl) 1425 { 1426 struct cls_fl_filter *f = fh; 1427 1428 if (f && f->res.classid == classid) 1429 f->res.class = cl; 1430 } 1431 1432 static struct tcf_proto_ops cls_fl_ops __read_mostly = { 1433 .kind = "flower", 1434 .classify = fl_classify, 1435 .init = fl_init, 1436 .destroy = fl_destroy, 1437 .get = fl_get, 1438 .change = fl_change, 1439 .delete = fl_delete, 1440 .walk = fl_walk, 1441 .dump = fl_dump, 1442 .bind_class = fl_bind_class, 1443 .owner = THIS_MODULE, 1444 }; 1445 1446 static int __init cls_fl_init(void) 1447 { 1448 return register_tcf_proto_ops(&cls_fl_ops); 1449 } 1450 1451 static void __exit cls_fl_exit(void) 1452 { 1453 unregister_tcf_proto_ops(&cls_fl_ops); 1454 } 1455 1456 module_init(cls_fl_init); 1457 module_exit(cls_fl_exit); 1458 1459 MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>"); 1460 MODULE_DESCRIPTION("Flower classifier"); 1461 MODULE_LICENSE("GPL v2"); 1462