1 // SPDX-License-Identifier: GPL-2.0-only 2 #include <linux/kernel.h> 3 #include <linux/skbuff.h> 4 #include <linux/export.h> 5 #include <linux/ip.h> 6 #include <linux/ipv6.h> 7 #include <linux/if_vlan.h> 8 #include <linux/filter.h> 9 #include <net/dsa.h> 10 #include <net/dst_metadata.h> 11 #include <net/ip.h> 12 #include <net/ipv6.h> 13 #include <net/gre.h> 14 #include <net/pptp.h> 15 #include <net/tipc.h> 16 #include <linux/igmp.h> 17 #include <linux/icmp.h> 18 #include <linux/sctp.h> 19 #include <linux/dccp.h> 20 #include <linux/if_tunnel.h> 21 #include <linux/if_pppox.h> 22 #include <linux/ppp_defs.h> 23 #include <linux/stddef.h> 24 #include <linux/if_ether.h> 25 #include <linux/if_hsr.h> 26 #include <linux/mpls.h> 27 #include <linux/tcp.h> 28 #include <linux/ptp_classify.h> 29 #include <net/flow_dissector.h> 30 #include <net/pkt_cls.h> 31 #include <scsi/fc/fc_fcoe.h> 32 #include <uapi/linux/batadv_packet.h> 33 #include <linux/bpf.h> 34 #if IS_ENABLED(CONFIG_NF_CONNTRACK) 35 #include <net/netfilter/nf_conntrack_core.h> 36 #include <net/netfilter/nf_conntrack_labels.h> 37 #endif 38 #include <linux/bpf-netns.h> 39 40 static void dissector_set_key(struct flow_dissector *flow_dissector, 41 enum flow_dissector_key_id key_id) 42 { 43 flow_dissector->used_keys |= (1ULL << key_id); 44 } 45 46 void skb_flow_dissector_init(struct flow_dissector *flow_dissector, 47 const struct flow_dissector_key *key, 48 unsigned int key_count) 49 { 50 unsigned int i; 51 52 memset(flow_dissector, 0, sizeof(*flow_dissector)); 53 54 for (i = 0; i < key_count; i++, key++) { 55 /* User should make sure that every key target offset is within 56 * boundaries of unsigned short. 57 */ 58 BUG_ON(key->offset > USHRT_MAX); 59 BUG_ON(dissector_uses_key(flow_dissector, 60 key->key_id)); 61 62 dissector_set_key(flow_dissector, key->key_id); 63 flow_dissector->offset[key->key_id] = key->offset; 64 } 65 66 /* Ensure that the dissector always includes control and basic key. 67 * That way we are able to avoid handling lack of these in fast path. 68 */ 69 BUG_ON(!dissector_uses_key(flow_dissector, 70 FLOW_DISSECTOR_KEY_CONTROL)); 71 BUG_ON(!dissector_uses_key(flow_dissector, 72 FLOW_DISSECTOR_KEY_BASIC)); 73 } 74 EXPORT_SYMBOL(skb_flow_dissector_init); 75 76 #ifdef CONFIG_BPF_SYSCALL 77 int flow_dissector_bpf_prog_attach_check(struct net *net, 78 struct bpf_prog *prog) 79 { 80 enum netns_bpf_attach_type type = NETNS_BPF_FLOW_DISSECTOR; 81 82 if (net == &init_net) { 83 /* BPF flow dissector in the root namespace overrides 84 * any per-net-namespace one. When attaching to root, 85 * make sure we don't have any BPF program attached 86 * to the non-root namespaces. 87 */ 88 struct net *ns; 89 90 for_each_net(ns) { 91 if (ns == &init_net) 92 continue; 93 if (rcu_access_pointer(ns->bpf.run_array[type])) 94 return -EEXIST; 95 } 96 } else { 97 /* Make sure root flow dissector is not attached 98 * when attaching to the non-root namespace. 99 */ 100 if (rcu_access_pointer(init_net.bpf.run_array[type])) 101 return -EEXIST; 102 } 103 104 return 0; 105 } 106 #endif /* CONFIG_BPF_SYSCALL */ 107 108 /** 109 * __skb_flow_get_ports - extract the upper layer ports and return them 110 * @skb: sk_buff to extract the ports from 111 * @thoff: transport header offset 112 * @ip_proto: protocol for which to get port offset 113 * @data: raw buffer pointer to the packet, if NULL use skb->data 114 * @hlen: packet header length, if @data is NULL use skb_headlen(skb) 115 * 116 * The function will try to retrieve the ports at offset thoff + poff where poff 117 * is the protocol port offset returned from proto_ports_offset 118 */ 119 __be32 __skb_flow_get_ports(const struct sk_buff *skb, int thoff, u8 ip_proto, 120 const void *data, int hlen) 121 { 122 int poff = proto_ports_offset(ip_proto); 123 124 if (!data) { 125 data = skb->data; 126 hlen = skb_headlen(skb); 127 } 128 129 if (poff >= 0) { 130 __be32 *ports, _ports; 131 132 ports = __skb_header_pointer(skb, thoff + poff, 133 sizeof(_ports), data, hlen, &_ports); 134 if (ports) 135 return *ports; 136 } 137 138 return 0; 139 } 140 EXPORT_SYMBOL(__skb_flow_get_ports); 141 142 static bool icmp_has_id(u8 type) 143 { 144 switch (type) { 145 case ICMP_ECHO: 146 case ICMP_ECHOREPLY: 147 case ICMP_TIMESTAMP: 148 case ICMP_TIMESTAMPREPLY: 149 case ICMPV6_ECHO_REQUEST: 150 case ICMPV6_ECHO_REPLY: 151 return true; 152 } 153 154 return false; 155 } 156 157 /** 158 * skb_flow_get_icmp_tci - extract ICMP(6) Type, Code and Identifier fields 159 * @skb: sk_buff to extract from 160 * @key_icmp: struct flow_dissector_key_icmp to fill 161 * @data: raw buffer pointer to the packet 162 * @thoff: offset to extract at 163 * @hlen: packet header length 164 */ 165 void skb_flow_get_icmp_tci(const struct sk_buff *skb, 166 struct flow_dissector_key_icmp *key_icmp, 167 const void *data, int thoff, int hlen) 168 { 169 struct icmphdr *ih, _ih; 170 171 ih = __skb_header_pointer(skb, thoff, sizeof(_ih), data, hlen, &_ih); 172 if (!ih) 173 return; 174 175 key_icmp->type = ih->type; 176 key_icmp->code = ih->code; 177 178 /* As we use 0 to signal that the Id field is not present, 179 * avoid confusion with packets without such field 180 */ 181 if (icmp_has_id(ih->type)) 182 key_icmp->id = ih->un.echo.id ? ntohs(ih->un.echo.id) : 1; 183 else 184 key_icmp->id = 0; 185 } 186 EXPORT_SYMBOL(skb_flow_get_icmp_tci); 187 188 /* If FLOW_DISSECTOR_KEY_ICMP is set, dissect an ICMP packet 189 * using skb_flow_get_icmp_tci(). 190 */ 191 static void __skb_flow_dissect_icmp(const struct sk_buff *skb, 192 struct flow_dissector *flow_dissector, 193 void *target_container, const void *data, 194 int thoff, int hlen) 195 { 196 struct flow_dissector_key_icmp *key_icmp; 197 198 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ICMP)) 199 return; 200 201 key_icmp = skb_flow_dissector_target(flow_dissector, 202 FLOW_DISSECTOR_KEY_ICMP, 203 target_container); 204 205 skb_flow_get_icmp_tci(skb, key_icmp, data, thoff, hlen); 206 } 207 208 static void __skb_flow_dissect_ah(const struct sk_buff *skb, 209 struct flow_dissector *flow_dissector, 210 void *target_container, const void *data, 211 int nhoff, int hlen) 212 { 213 struct flow_dissector_key_ipsec *key_ah; 214 struct ip_auth_hdr _hdr, *hdr; 215 216 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_IPSEC)) 217 return; 218 219 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr); 220 if (!hdr) 221 return; 222 223 key_ah = skb_flow_dissector_target(flow_dissector, 224 FLOW_DISSECTOR_KEY_IPSEC, 225 target_container); 226 227 key_ah->spi = hdr->spi; 228 } 229 230 static void __skb_flow_dissect_esp(const struct sk_buff *skb, 231 struct flow_dissector *flow_dissector, 232 void *target_container, const void *data, 233 int nhoff, int hlen) 234 { 235 struct flow_dissector_key_ipsec *key_esp; 236 struct ip_esp_hdr _hdr, *hdr; 237 238 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_IPSEC)) 239 return; 240 241 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr); 242 if (!hdr) 243 return; 244 245 key_esp = skb_flow_dissector_target(flow_dissector, 246 FLOW_DISSECTOR_KEY_IPSEC, 247 target_container); 248 249 key_esp->spi = hdr->spi; 250 } 251 252 static void __skb_flow_dissect_l2tpv3(const struct sk_buff *skb, 253 struct flow_dissector *flow_dissector, 254 void *target_container, const void *data, 255 int nhoff, int hlen) 256 { 257 struct flow_dissector_key_l2tpv3 *key_l2tpv3; 258 struct { 259 __be32 session_id; 260 } *hdr, _hdr; 261 262 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_L2TPV3)) 263 return; 264 265 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr); 266 if (!hdr) 267 return; 268 269 key_l2tpv3 = skb_flow_dissector_target(flow_dissector, 270 FLOW_DISSECTOR_KEY_L2TPV3, 271 target_container); 272 273 key_l2tpv3->session_id = hdr->session_id; 274 } 275 276 void skb_flow_dissect_meta(const struct sk_buff *skb, 277 struct flow_dissector *flow_dissector, 278 void *target_container) 279 { 280 struct flow_dissector_key_meta *meta; 281 282 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_META)) 283 return; 284 285 meta = skb_flow_dissector_target(flow_dissector, 286 FLOW_DISSECTOR_KEY_META, 287 target_container); 288 meta->ingress_ifindex = skb->skb_iif; 289 #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT) 290 if (tc_skb_ext_tc_enabled()) { 291 struct tc_skb_ext *ext; 292 293 ext = skb_ext_find(skb, TC_SKB_EXT); 294 if (ext) 295 meta->l2_miss = ext->l2_miss; 296 } 297 #endif 298 } 299 EXPORT_SYMBOL(skb_flow_dissect_meta); 300 301 static void 302 skb_flow_dissect_set_enc_addr_type(enum flow_dissector_key_id type, 303 struct flow_dissector *flow_dissector, 304 void *target_container) 305 { 306 struct flow_dissector_key_control *ctrl; 307 308 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ENC_CONTROL)) 309 return; 310 311 ctrl = skb_flow_dissector_target(flow_dissector, 312 FLOW_DISSECTOR_KEY_ENC_CONTROL, 313 target_container); 314 ctrl->addr_type = type; 315 } 316 317 void 318 skb_flow_dissect_ct(const struct sk_buff *skb, 319 struct flow_dissector *flow_dissector, 320 void *target_container, u16 *ctinfo_map, 321 size_t mapsize, bool post_ct, u16 zone) 322 { 323 #if IS_ENABLED(CONFIG_NF_CONNTRACK) 324 struct flow_dissector_key_ct *key; 325 enum ip_conntrack_info ctinfo; 326 struct nf_conn_labels *cl; 327 struct nf_conn *ct; 328 329 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_CT)) 330 return; 331 332 ct = nf_ct_get(skb, &ctinfo); 333 if (!ct && !post_ct) 334 return; 335 336 key = skb_flow_dissector_target(flow_dissector, 337 FLOW_DISSECTOR_KEY_CT, 338 target_container); 339 340 if (!ct) { 341 key->ct_state = TCA_FLOWER_KEY_CT_FLAGS_TRACKED | 342 TCA_FLOWER_KEY_CT_FLAGS_INVALID; 343 key->ct_zone = zone; 344 return; 345 } 346 347 if (ctinfo < mapsize) 348 key->ct_state = ctinfo_map[ctinfo]; 349 #if IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) 350 key->ct_zone = ct->zone.id; 351 #endif 352 #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) 353 key->ct_mark = READ_ONCE(ct->mark); 354 #endif 355 356 cl = nf_ct_labels_find(ct); 357 if (cl) 358 memcpy(key->ct_labels, cl->bits, sizeof(key->ct_labels)); 359 #endif /* CONFIG_NF_CONNTRACK */ 360 } 361 EXPORT_SYMBOL(skb_flow_dissect_ct); 362 363 void 364 skb_flow_dissect_tunnel_info(const struct sk_buff *skb, 365 struct flow_dissector *flow_dissector, 366 void *target_container) 367 { 368 struct ip_tunnel_info *info; 369 struct ip_tunnel_key *key; 370 371 /* A quick check to see if there might be something to do. */ 372 if (!dissector_uses_key(flow_dissector, 373 FLOW_DISSECTOR_KEY_ENC_KEYID) && 374 !dissector_uses_key(flow_dissector, 375 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS) && 376 !dissector_uses_key(flow_dissector, 377 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS) && 378 !dissector_uses_key(flow_dissector, 379 FLOW_DISSECTOR_KEY_ENC_CONTROL) && 380 !dissector_uses_key(flow_dissector, 381 FLOW_DISSECTOR_KEY_ENC_PORTS) && 382 !dissector_uses_key(flow_dissector, 383 FLOW_DISSECTOR_KEY_ENC_IP) && 384 !dissector_uses_key(flow_dissector, 385 FLOW_DISSECTOR_KEY_ENC_OPTS) && 386 !dissector_uses_key(flow_dissector, 387 FLOW_DISSECTOR_KEY_ENC_FLAGS)) 388 return; 389 390 info = skb_tunnel_info(skb); 391 if (!info) 392 return; 393 394 key = &info->key; 395 396 switch (ip_tunnel_info_af(info)) { 397 case AF_INET: 398 skb_flow_dissect_set_enc_addr_type(FLOW_DISSECTOR_KEY_IPV4_ADDRS, 399 flow_dissector, 400 target_container); 401 if (dissector_uses_key(flow_dissector, 402 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS)) { 403 struct flow_dissector_key_ipv4_addrs *ipv4; 404 405 ipv4 = skb_flow_dissector_target(flow_dissector, 406 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS, 407 target_container); 408 ipv4->src = key->u.ipv4.src; 409 ipv4->dst = key->u.ipv4.dst; 410 } 411 break; 412 case AF_INET6: 413 skb_flow_dissect_set_enc_addr_type(FLOW_DISSECTOR_KEY_IPV6_ADDRS, 414 flow_dissector, 415 target_container); 416 if (dissector_uses_key(flow_dissector, 417 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS)) { 418 struct flow_dissector_key_ipv6_addrs *ipv6; 419 420 ipv6 = skb_flow_dissector_target(flow_dissector, 421 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS, 422 target_container); 423 ipv6->src = key->u.ipv6.src; 424 ipv6->dst = key->u.ipv6.dst; 425 } 426 break; 427 } 428 429 if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ENC_KEYID)) { 430 struct flow_dissector_key_keyid *keyid; 431 432 keyid = skb_flow_dissector_target(flow_dissector, 433 FLOW_DISSECTOR_KEY_ENC_KEYID, 434 target_container); 435 keyid->keyid = tunnel_id_to_key32(key->tun_id); 436 } 437 438 if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ENC_PORTS)) { 439 struct flow_dissector_key_ports *tp; 440 441 tp = skb_flow_dissector_target(flow_dissector, 442 FLOW_DISSECTOR_KEY_ENC_PORTS, 443 target_container); 444 tp->src = key->tp_src; 445 tp->dst = key->tp_dst; 446 } 447 448 if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ENC_IP)) { 449 struct flow_dissector_key_ip *ip; 450 451 ip = skb_flow_dissector_target(flow_dissector, 452 FLOW_DISSECTOR_KEY_ENC_IP, 453 target_container); 454 ip->tos = key->tos; 455 ip->ttl = key->ttl; 456 } 457 458 if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ENC_OPTS)) { 459 struct flow_dissector_key_enc_opts *enc_opt; 460 IP_TUNNEL_DECLARE_FLAGS(flags) = { }; 461 u32 val; 462 463 enc_opt = skb_flow_dissector_target(flow_dissector, 464 FLOW_DISSECTOR_KEY_ENC_OPTS, 465 target_container); 466 467 if (!info->options_len) 468 return; 469 470 enc_opt->len = info->options_len; 471 ip_tunnel_info_opts_get(enc_opt->data, info); 472 473 ip_tunnel_set_options_present(flags); 474 ip_tunnel_flags_and(flags, info->key.tun_flags, flags); 475 476 val = find_next_bit(flags, __IP_TUNNEL_FLAG_NUM, 477 IP_TUNNEL_GENEVE_OPT_BIT); 478 enc_opt->dst_opt_type = val < __IP_TUNNEL_FLAG_NUM ? val : 0; 479 } 480 481 if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ENC_FLAGS)) { 482 struct flow_dissector_key_enc_flags *enc_flags; 483 IP_TUNNEL_DECLARE_FLAGS(flags) = {}; 484 485 enc_flags = skb_flow_dissector_target(flow_dissector, 486 FLOW_DISSECTOR_KEY_ENC_FLAGS, 487 target_container); 488 ip_tunnel_set_encflags_present(flags); 489 ip_tunnel_flags_and(flags, flags, info->key.tun_flags); 490 enc_flags->flags = bitmap_read(flags, IP_TUNNEL_CSUM_BIT, 32); 491 } 492 } 493 EXPORT_SYMBOL(skb_flow_dissect_tunnel_info); 494 495 void skb_flow_dissect_hash(const struct sk_buff *skb, 496 struct flow_dissector *flow_dissector, 497 void *target_container) 498 { 499 struct flow_dissector_key_hash *key; 500 501 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_HASH)) 502 return; 503 504 key = skb_flow_dissector_target(flow_dissector, 505 FLOW_DISSECTOR_KEY_HASH, 506 target_container); 507 508 key->hash = skb_get_hash_raw(skb); 509 } 510 EXPORT_SYMBOL(skb_flow_dissect_hash); 511 512 static enum flow_dissect_ret 513 __skb_flow_dissect_mpls(const struct sk_buff *skb, 514 struct flow_dissector *flow_dissector, 515 void *target_container, const void *data, int nhoff, 516 int hlen, int lse_index, bool *entropy_label) 517 { 518 struct mpls_label *hdr, _hdr; 519 u32 entry, label, bos; 520 521 if (!dissector_uses_key(flow_dissector, 522 FLOW_DISSECTOR_KEY_MPLS_ENTROPY) && 523 !dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_MPLS)) 524 return FLOW_DISSECT_RET_OUT_GOOD; 525 526 if (lse_index >= FLOW_DIS_MPLS_MAX) 527 return FLOW_DISSECT_RET_OUT_GOOD; 528 529 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, 530 hlen, &_hdr); 531 if (!hdr) 532 return FLOW_DISSECT_RET_OUT_BAD; 533 534 entry = ntohl(hdr->entry); 535 label = (entry & MPLS_LS_LABEL_MASK) >> MPLS_LS_LABEL_SHIFT; 536 bos = (entry & MPLS_LS_S_MASK) >> MPLS_LS_S_SHIFT; 537 538 if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_MPLS)) { 539 struct flow_dissector_key_mpls *key_mpls; 540 struct flow_dissector_mpls_lse *lse; 541 542 key_mpls = skb_flow_dissector_target(flow_dissector, 543 FLOW_DISSECTOR_KEY_MPLS, 544 target_container); 545 lse = &key_mpls->ls[lse_index]; 546 547 lse->mpls_ttl = (entry & MPLS_LS_TTL_MASK) >> MPLS_LS_TTL_SHIFT; 548 lse->mpls_bos = bos; 549 lse->mpls_tc = (entry & MPLS_LS_TC_MASK) >> MPLS_LS_TC_SHIFT; 550 lse->mpls_label = label; 551 dissector_set_mpls_lse(key_mpls, lse_index); 552 } 553 554 if (*entropy_label && 555 dissector_uses_key(flow_dissector, 556 FLOW_DISSECTOR_KEY_MPLS_ENTROPY)) { 557 struct flow_dissector_key_keyid *key_keyid; 558 559 key_keyid = skb_flow_dissector_target(flow_dissector, 560 FLOW_DISSECTOR_KEY_MPLS_ENTROPY, 561 target_container); 562 key_keyid->keyid = cpu_to_be32(label); 563 } 564 565 *entropy_label = label == MPLS_LABEL_ENTROPY; 566 567 return bos ? FLOW_DISSECT_RET_OUT_GOOD : FLOW_DISSECT_RET_PROTO_AGAIN; 568 } 569 570 static enum flow_dissect_ret 571 __skb_flow_dissect_arp(const struct sk_buff *skb, 572 struct flow_dissector *flow_dissector, 573 void *target_container, const void *data, 574 int nhoff, int hlen) 575 { 576 struct flow_dissector_key_arp *key_arp; 577 struct { 578 unsigned char ar_sha[ETH_ALEN]; 579 unsigned char ar_sip[4]; 580 unsigned char ar_tha[ETH_ALEN]; 581 unsigned char ar_tip[4]; 582 } *arp_eth, _arp_eth; 583 const struct arphdr *arp; 584 struct arphdr _arp; 585 586 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ARP)) 587 return FLOW_DISSECT_RET_OUT_GOOD; 588 589 arp = __skb_header_pointer(skb, nhoff, sizeof(_arp), data, 590 hlen, &_arp); 591 if (!arp) 592 return FLOW_DISSECT_RET_OUT_BAD; 593 594 if (arp->ar_hrd != htons(ARPHRD_ETHER) || 595 arp->ar_pro != htons(ETH_P_IP) || 596 arp->ar_hln != ETH_ALEN || 597 arp->ar_pln != 4 || 598 (arp->ar_op != htons(ARPOP_REPLY) && 599 arp->ar_op != htons(ARPOP_REQUEST))) 600 return FLOW_DISSECT_RET_OUT_BAD; 601 602 arp_eth = __skb_header_pointer(skb, nhoff + sizeof(_arp), 603 sizeof(_arp_eth), data, 604 hlen, &_arp_eth); 605 if (!arp_eth) 606 return FLOW_DISSECT_RET_OUT_BAD; 607 608 key_arp = skb_flow_dissector_target(flow_dissector, 609 FLOW_DISSECTOR_KEY_ARP, 610 target_container); 611 612 memcpy(&key_arp->sip, arp_eth->ar_sip, sizeof(key_arp->sip)); 613 memcpy(&key_arp->tip, arp_eth->ar_tip, sizeof(key_arp->tip)); 614 615 /* Only store the lower byte of the opcode; 616 * this covers ARPOP_REPLY and ARPOP_REQUEST. 617 */ 618 key_arp->op = ntohs(arp->ar_op) & 0xff; 619 620 ether_addr_copy(key_arp->sha, arp_eth->ar_sha); 621 ether_addr_copy(key_arp->tha, arp_eth->ar_tha); 622 623 return FLOW_DISSECT_RET_OUT_GOOD; 624 } 625 626 static enum flow_dissect_ret 627 __skb_flow_dissect_cfm(const struct sk_buff *skb, 628 struct flow_dissector *flow_dissector, 629 void *target_container, const void *data, 630 int nhoff, int hlen) 631 { 632 struct flow_dissector_key_cfm *key, *hdr, _hdr; 633 634 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_CFM)) 635 return FLOW_DISSECT_RET_OUT_GOOD; 636 637 hdr = __skb_header_pointer(skb, nhoff, sizeof(*key), data, hlen, &_hdr); 638 if (!hdr) 639 return FLOW_DISSECT_RET_OUT_BAD; 640 641 key = skb_flow_dissector_target(flow_dissector, FLOW_DISSECTOR_KEY_CFM, 642 target_container); 643 644 key->mdl_ver = hdr->mdl_ver; 645 key->opcode = hdr->opcode; 646 647 return FLOW_DISSECT_RET_OUT_GOOD; 648 } 649 650 static enum flow_dissect_ret 651 __skb_flow_dissect_gre(const struct sk_buff *skb, 652 struct flow_dissector_key_control *key_control, 653 struct flow_dissector *flow_dissector, 654 void *target_container, const void *data, 655 __be16 *p_proto, int *p_nhoff, int *p_hlen, 656 unsigned int flags) 657 { 658 struct flow_dissector_key_keyid *key_keyid; 659 struct gre_base_hdr *hdr, _hdr; 660 int offset = 0; 661 u16 gre_ver; 662 663 hdr = __skb_header_pointer(skb, *p_nhoff, sizeof(_hdr), 664 data, *p_hlen, &_hdr); 665 if (!hdr) 666 return FLOW_DISSECT_RET_OUT_BAD; 667 668 /* Only look inside GRE without routing */ 669 if (hdr->flags & GRE_ROUTING) 670 return FLOW_DISSECT_RET_OUT_GOOD; 671 672 /* Only look inside GRE for version 0 and 1 */ 673 gre_ver = ntohs(hdr->flags & GRE_VERSION); 674 if (gre_ver > 1) 675 return FLOW_DISSECT_RET_OUT_GOOD; 676 677 *p_proto = hdr->protocol; 678 if (gre_ver) { 679 /* Version1 must be PPTP, and check the flags */ 680 if (!(*p_proto == GRE_PROTO_PPP && (hdr->flags & GRE_KEY))) 681 return FLOW_DISSECT_RET_OUT_GOOD; 682 } 683 684 offset += sizeof(struct gre_base_hdr); 685 686 if (hdr->flags & GRE_CSUM) 687 offset += sizeof_field(struct gre_full_hdr, csum) + 688 sizeof_field(struct gre_full_hdr, reserved1); 689 690 if (hdr->flags & GRE_KEY) { 691 const __be32 *keyid; 692 __be32 _keyid; 693 694 keyid = __skb_header_pointer(skb, *p_nhoff + offset, 695 sizeof(_keyid), 696 data, *p_hlen, &_keyid); 697 if (!keyid) 698 return FLOW_DISSECT_RET_OUT_BAD; 699 700 if (dissector_uses_key(flow_dissector, 701 FLOW_DISSECTOR_KEY_GRE_KEYID)) { 702 key_keyid = skb_flow_dissector_target(flow_dissector, 703 FLOW_DISSECTOR_KEY_GRE_KEYID, 704 target_container); 705 if (gre_ver == 0) 706 key_keyid->keyid = *keyid; 707 else 708 key_keyid->keyid = *keyid & GRE_PPTP_KEY_MASK; 709 } 710 offset += sizeof_field(struct gre_full_hdr, key); 711 } 712 713 if (hdr->flags & GRE_SEQ) 714 offset += sizeof_field(struct pptp_gre_header, seq); 715 716 if (gre_ver == 0) { 717 if (*p_proto == htons(ETH_P_TEB)) { 718 const struct ethhdr *eth; 719 struct ethhdr _eth; 720 721 eth = __skb_header_pointer(skb, *p_nhoff + offset, 722 sizeof(_eth), 723 data, *p_hlen, &_eth); 724 if (!eth) 725 return FLOW_DISSECT_RET_OUT_BAD; 726 *p_proto = eth->h_proto; 727 offset += sizeof(*eth); 728 729 /* Cap headers that we access via pointers at the 730 * end of the Ethernet header as our maximum alignment 731 * at that point is only 2 bytes. 732 */ 733 if (NET_IP_ALIGN) 734 *p_hlen = *p_nhoff + offset; 735 } 736 } else { /* version 1, must be PPTP */ 737 u8 _ppp_hdr[PPP_HDRLEN]; 738 u8 *ppp_hdr; 739 740 if (hdr->flags & GRE_ACK) 741 offset += sizeof_field(struct pptp_gre_header, ack); 742 743 ppp_hdr = __skb_header_pointer(skb, *p_nhoff + offset, 744 sizeof(_ppp_hdr), 745 data, *p_hlen, _ppp_hdr); 746 if (!ppp_hdr) 747 return FLOW_DISSECT_RET_OUT_BAD; 748 749 switch (PPP_PROTOCOL(ppp_hdr)) { 750 case PPP_IP: 751 *p_proto = htons(ETH_P_IP); 752 break; 753 case PPP_IPV6: 754 *p_proto = htons(ETH_P_IPV6); 755 break; 756 default: 757 /* Could probably catch some more like MPLS */ 758 break; 759 } 760 761 offset += PPP_HDRLEN; 762 } 763 764 *p_nhoff += offset; 765 key_control->flags |= FLOW_DIS_ENCAPSULATION; 766 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP) 767 return FLOW_DISSECT_RET_OUT_GOOD; 768 769 return FLOW_DISSECT_RET_PROTO_AGAIN; 770 } 771 772 /** 773 * __skb_flow_dissect_batadv() - dissect batman-adv header 774 * @skb: sk_buff to with the batman-adv header 775 * @key_control: flow dissectors control key 776 * @data: raw buffer pointer to the packet, if NULL use skb->data 777 * @p_proto: pointer used to update the protocol to process next 778 * @p_nhoff: pointer used to update inner network header offset 779 * @hlen: packet header length 780 * @flags: any combination of FLOW_DISSECTOR_F_* 781 * 782 * ETH_P_BATMAN packets are tried to be dissected. Only 783 * &struct batadv_unicast packets are actually processed because they contain an 784 * inner ethernet header and are usually followed by actual network header. This 785 * allows the flow dissector to continue processing the packet. 786 * 787 * Return: FLOW_DISSECT_RET_PROTO_AGAIN when &struct batadv_unicast was found, 788 * FLOW_DISSECT_RET_OUT_GOOD when dissector should stop after encapsulation, 789 * otherwise FLOW_DISSECT_RET_OUT_BAD 790 */ 791 static enum flow_dissect_ret 792 __skb_flow_dissect_batadv(const struct sk_buff *skb, 793 struct flow_dissector_key_control *key_control, 794 const void *data, __be16 *p_proto, int *p_nhoff, 795 int hlen, unsigned int flags) 796 { 797 struct { 798 struct batadv_unicast_packet batadv_unicast; 799 struct ethhdr eth; 800 } *hdr, _hdr; 801 802 hdr = __skb_header_pointer(skb, *p_nhoff, sizeof(_hdr), data, hlen, 803 &_hdr); 804 if (!hdr) 805 return FLOW_DISSECT_RET_OUT_BAD; 806 807 if (hdr->batadv_unicast.version != BATADV_COMPAT_VERSION) 808 return FLOW_DISSECT_RET_OUT_BAD; 809 810 if (hdr->batadv_unicast.packet_type != BATADV_UNICAST) 811 return FLOW_DISSECT_RET_OUT_BAD; 812 813 *p_proto = hdr->eth.h_proto; 814 *p_nhoff += sizeof(*hdr); 815 816 key_control->flags |= FLOW_DIS_ENCAPSULATION; 817 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP) 818 return FLOW_DISSECT_RET_OUT_GOOD; 819 820 return FLOW_DISSECT_RET_PROTO_AGAIN; 821 } 822 823 static void 824 __skb_flow_dissect_tcp(const struct sk_buff *skb, 825 struct flow_dissector *flow_dissector, 826 void *target_container, const void *data, 827 int thoff, int hlen) 828 { 829 struct flow_dissector_key_tcp *key_tcp; 830 struct tcphdr *th, _th; 831 832 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_TCP)) 833 return; 834 835 th = __skb_header_pointer(skb, thoff, sizeof(_th), data, hlen, &_th); 836 if (!th) 837 return; 838 839 if (unlikely(__tcp_hdrlen(th) < sizeof(_th))) 840 return; 841 842 key_tcp = skb_flow_dissector_target(flow_dissector, 843 FLOW_DISSECTOR_KEY_TCP, 844 target_container); 845 key_tcp->flags = (*(__be16 *) &tcp_flag_word(th) & htons(0x0FFF)); 846 } 847 848 static void 849 __skb_flow_dissect_ports(const struct sk_buff *skb, 850 struct flow_dissector *flow_dissector, 851 void *target_container, const void *data, 852 int nhoff, u8 ip_proto, int hlen) 853 { 854 enum flow_dissector_key_id dissector_ports = FLOW_DISSECTOR_KEY_MAX; 855 struct flow_dissector_key_ports *key_ports; 856 857 if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_PORTS)) 858 dissector_ports = FLOW_DISSECTOR_KEY_PORTS; 859 else if (dissector_uses_key(flow_dissector, 860 FLOW_DISSECTOR_KEY_PORTS_RANGE)) 861 dissector_ports = FLOW_DISSECTOR_KEY_PORTS_RANGE; 862 863 if (dissector_ports == FLOW_DISSECTOR_KEY_MAX) 864 return; 865 866 key_ports = skb_flow_dissector_target(flow_dissector, 867 dissector_ports, 868 target_container); 869 key_ports->ports = __skb_flow_get_ports(skb, nhoff, ip_proto, 870 data, hlen); 871 } 872 873 static void 874 __skb_flow_dissect_ipv4(const struct sk_buff *skb, 875 struct flow_dissector *flow_dissector, 876 void *target_container, const void *data, 877 const struct iphdr *iph) 878 { 879 struct flow_dissector_key_ip *key_ip; 880 881 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_IP)) 882 return; 883 884 key_ip = skb_flow_dissector_target(flow_dissector, 885 FLOW_DISSECTOR_KEY_IP, 886 target_container); 887 key_ip->tos = iph->tos; 888 key_ip->ttl = iph->ttl; 889 } 890 891 static void 892 __skb_flow_dissect_ipv6(const struct sk_buff *skb, 893 struct flow_dissector *flow_dissector, 894 void *target_container, const void *data, 895 const struct ipv6hdr *iph) 896 { 897 struct flow_dissector_key_ip *key_ip; 898 899 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_IP)) 900 return; 901 902 key_ip = skb_flow_dissector_target(flow_dissector, 903 FLOW_DISSECTOR_KEY_IP, 904 target_container); 905 key_ip->tos = ipv6_get_dsfield(iph); 906 key_ip->ttl = iph->hop_limit; 907 } 908 909 /* Maximum number of protocol headers that can be parsed in 910 * __skb_flow_dissect 911 */ 912 #define MAX_FLOW_DISSECT_HDRS 15 913 914 static bool skb_flow_dissect_allowed(int *num_hdrs) 915 { 916 ++*num_hdrs; 917 918 return (*num_hdrs <= MAX_FLOW_DISSECT_HDRS); 919 } 920 921 static void __skb_flow_bpf_to_target(const struct bpf_flow_keys *flow_keys, 922 struct flow_dissector *flow_dissector, 923 void *target_container) 924 { 925 struct flow_dissector_key_ports *key_ports = NULL; 926 struct flow_dissector_key_control *key_control; 927 struct flow_dissector_key_basic *key_basic; 928 struct flow_dissector_key_addrs *key_addrs; 929 struct flow_dissector_key_tags *key_tags; 930 931 key_control = skb_flow_dissector_target(flow_dissector, 932 FLOW_DISSECTOR_KEY_CONTROL, 933 target_container); 934 key_control->thoff = flow_keys->thoff; 935 if (flow_keys->is_frag) 936 key_control->flags |= FLOW_DIS_IS_FRAGMENT; 937 if (flow_keys->is_first_frag) 938 key_control->flags |= FLOW_DIS_FIRST_FRAG; 939 if (flow_keys->is_encap) 940 key_control->flags |= FLOW_DIS_ENCAPSULATION; 941 942 key_basic = skb_flow_dissector_target(flow_dissector, 943 FLOW_DISSECTOR_KEY_BASIC, 944 target_container); 945 key_basic->n_proto = flow_keys->n_proto; 946 key_basic->ip_proto = flow_keys->ip_proto; 947 948 if (flow_keys->addr_proto == ETH_P_IP && 949 dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_IPV4_ADDRS)) { 950 key_addrs = skb_flow_dissector_target(flow_dissector, 951 FLOW_DISSECTOR_KEY_IPV4_ADDRS, 952 target_container); 953 key_addrs->v4addrs.src = flow_keys->ipv4_src; 954 key_addrs->v4addrs.dst = flow_keys->ipv4_dst; 955 key_control->addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; 956 } else if (flow_keys->addr_proto == ETH_P_IPV6 && 957 dissector_uses_key(flow_dissector, 958 FLOW_DISSECTOR_KEY_IPV6_ADDRS)) { 959 key_addrs = skb_flow_dissector_target(flow_dissector, 960 FLOW_DISSECTOR_KEY_IPV6_ADDRS, 961 target_container); 962 memcpy(&key_addrs->v6addrs.src, &flow_keys->ipv6_src, 963 sizeof(key_addrs->v6addrs.src)); 964 memcpy(&key_addrs->v6addrs.dst, &flow_keys->ipv6_dst, 965 sizeof(key_addrs->v6addrs.dst)); 966 key_control->addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 967 } 968 969 if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_PORTS)) 970 key_ports = skb_flow_dissector_target(flow_dissector, 971 FLOW_DISSECTOR_KEY_PORTS, 972 target_container); 973 else if (dissector_uses_key(flow_dissector, 974 FLOW_DISSECTOR_KEY_PORTS_RANGE)) 975 key_ports = skb_flow_dissector_target(flow_dissector, 976 FLOW_DISSECTOR_KEY_PORTS_RANGE, 977 target_container); 978 979 if (key_ports) { 980 key_ports->src = flow_keys->sport; 981 key_ports->dst = flow_keys->dport; 982 } 983 984 if (dissector_uses_key(flow_dissector, 985 FLOW_DISSECTOR_KEY_FLOW_LABEL)) { 986 key_tags = skb_flow_dissector_target(flow_dissector, 987 FLOW_DISSECTOR_KEY_FLOW_LABEL, 988 target_container); 989 key_tags->flow_label = ntohl(flow_keys->flow_label); 990 } 991 } 992 993 u32 bpf_flow_dissect(struct bpf_prog *prog, struct bpf_flow_dissector *ctx, 994 __be16 proto, int nhoff, int hlen, unsigned int flags) 995 { 996 struct bpf_flow_keys *flow_keys = ctx->flow_keys; 997 u32 result; 998 999 /* Pass parameters to the BPF program */ 1000 memset(flow_keys, 0, sizeof(*flow_keys)); 1001 flow_keys->n_proto = proto; 1002 flow_keys->nhoff = nhoff; 1003 flow_keys->thoff = flow_keys->nhoff; 1004 1005 BUILD_BUG_ON((int)BPF_FLOW_DISSECTOR_F_PARSE_1ST_FRAG != 1006 (int)FLOW_DISSECTOR_F_PARSE_1ST_FRAG); 1007 BUILD_BUG_ON((int)BPF_FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL != 1008 (int)FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL); 1009 BUILD_BUG_ON((int)BPF_FLOW_DISSECTOR_F_STOP_AT_ENCAP != 1010 (int)FLOW_DISSECTOR_F_STOP_AT_ENCAP); 1011 flow_keys->flags = flags; 1012 1013 result = bpf_prog_run_pin_on_cpu(prog, ctx); 1014 1015 flow_keys->nhoff = clamp_t(u16, flow_keys->nhoff, nhoff, hlen); 1016 flow_keys->thoff = clamp_t(u16, flow_keys->thoff, 1017 flow_keys->nhoff, hlen); 1018 1019 return result; 1020 } 1021 1022 static bool is_pppoe_ses_hdr_valid(const struct pppoe_hdr *hdr) 1023 { 1024 return hdr->ver == 1 && hdr->type == 1 && hdr->code == 0; 1025 } 1026 1027 /** 1028 * __skb_flow_dissect - extract the flow_keys struct and return it 1029 * @net: associated network namespace, derived from @skb if NULL 1030 * @skb: sk_buff to extract the flow from, can be NULL if the rest are specified 1031 * @flow_dissector: list of keys to dissect 1032 * @target_container: target structure to put dissected values into 1033 * @data: raw buffer pointer to the packet, if NULL use skb->data 1034 * @proto: protocol for which to get the flow, if @data is NULL use skb->protocol 1035 * @nhoff: network header offset, if @data is NULL use skb_network_offset(skb) 1036 * @hlen: packet header length, if @data is NULL use skb_headlen(skb) 1037 * @flags: flags that control the dissection process, e.g. 1038 * FLOW_DISSECTOR_F_STOP_AT_ENCAP. 1039 * 1040 * The function will try to retrieve individual keys into target specified 1041 * by flow_dissector from either the skbuff or a raw buffer specified by the 1042 * rest parameters. 1043 * 1044 * Caller must take care of zeroing target container memory. 1045 */ 1046 bool __skb_flow_dissect(const struct net *net, 1047 const struct sk_buff *skb, 1048 struct flow_dissector *flow_dissector, 1049 void *target_container, const void *data, 1050 __be16 proto, int nhoff, int hlen, unsigned int flags) 1051 { 1052 struct flow_dissector_key_control *key_control; 1053 struct flow_dissector_key_basic *key_basic; 1054 struct flow_dissector_key_addrs *key_addrs; 1055 struct flow_dissector_key_tags *key_tags; 1056 struct flow_dissector_key_vlan *key_vlan; 1057 enum flow_dissect_ret fdret; 1058 enum flow_dissector_key_id dissector_vlan = FLOW_DISSECTOR_KEY_MAX; 1059 bool mpls_el = false; 1060 int mpls_lse = 0; 1061 int num_hdrs = 0; 1062 u8 ip_proto = 0; 1063 bool ret; 1064 1065 if (!data) { 1066 data = skb->data; 1067 proto = skb_vlan_tag_present(skb) ? 1068 skb->vlan_proto : skb->protocol; 1069 nhoff = skb_network_offset(skb); 1070 hlen = skb_headlen(skb); 1071 #if IS_ENABLED(CONFIG_NET_DSA) 1072 if (unlikely(skb->dev && netdev_uses_dsa(skb->dev) && 1073 proto == htons(ETH_P_XDSA))) { 1074 struct metadata_dst *md_dst = skb_metadata_dst(skb); 1075 const struct dsa_device_ops *ops; 1076 int offset = 0; 1077 1078 ops = skb->dev->dsa_ptr->tag_ops; 1079 /* Only DSA header taggers break flow dissection */ 1080 if (ops->needed_headroom && 1081 (!md_dst || md_dst->type != METADATA_HW_PORT_MUX)) { 1082 if (ops->flow_dissect) 1083 ops->flow_dissect(skb, &proto, &offset); 1084 else 1085 dsa_tag_generic_flow_dissect(skb, 1086 &proto, 1087 &offset); 1088 hlen -= offset; 1089 nhoff += offset; 1090 } 1091 } 1092 #endif 1093 } 1094 1095 /* It is ensured by skb_flow_dissector_init() that control key will 1096 * be always present. 1097 */ 1098 key_control = skb_flow_dissector_target(flow_dissector, 1099 FLOW_DISSECTOR_KEY_CONTROL, 1100 target_container); 1101 1102 /* It is ensured by skb_flow_dissector_init() that basic key will 1103 * be always present. 1104 */ 1105 key_basic = skb_flow_dissector_target(flow_dissector, 1106 FLOW_DISSECTOR_KEY_BASIC, 1107 target_container); 1108 1109 if (skb) { 1110 if (!net) { 1111 if (skb->dev) 1112 net = dev_net(skb->dev); 1113 else if (skb->sk) 1114 net = sock_net(skb->sk); 1115 } 1116 } 1117 1118 WARN_ON_ONCE(!net); 1119 if (net) { 1120 enum netns_bpf_attach_type type = NETNS_BPF_FLOW_DISSECTOR; 1121 struct bpf_prog_array *run_array; 1122 1123 rcu_read_lock(); 1124 run_array = rcu_dereference(init_net.bpf.run_array[type]); 1125 if (!run_array) 1126 run_array = rcu_dereference(net->bpf.run_array[type]); 1127 1128 if (run_array) { 1129 struct bpf_flow_keys flow_keys; 1130 struct bpf_flow_dissector ctx = { 1131 .flow_keys = &flow_keys, 1132 .data = data, 1133 .data_end = data + hlen, 1134 }; 1135 __be16 n_proto = proto; 1136 struct bpf_prog *prog; 1137 u32 result; 1138 1139 if (skb) { 1140 ctx.skb = skb; 1141 /* we can't use 'proto' in the skb case 1142 * because it might be set to skb->vlan_proto 1143 * which has been pulled from the data 1144 */ 1145 n_proto = skb->protocol; 1146 } 1147 1148 prog = READ_ONCE(run_array->items[0].prog); 1149 result = bpf_flow_dissect(prog, &ctx, n_proto, nhoff, 1150 hlen, flags); 1151 if (result == BPF_FLOW_DISSECTOR_CONTINUE) 1152 goto dissect_continue; 1153 __skb_flow_bpf_to_target(&flow_keys, flow_dissector, 1154 target_container); 1155 rcu_read_unlock(); 1156 return result == BPF_OK; 1157 } 1158 dissect_continue: 1159 rcu_read_unlock(); 1160 } 1161 1162 if (dissector_uses_key(flow_dissector, 1163 FLOW_DISSECTOR_KEY_ETH_ADDRS)) { 1164 struct ethhdr *eth = eth_hdr(skb); 1165 struct flow_dissector_key_eth_addrs *key_eth_addrs; 1166 1167 key_eth_addrs = skb_flow_dissector_target(flow_dissector, 1168 FLOW_DISSECTOR_KEY_ETH_ADDRS, 1169 target_container); 1170 memcpy(key_eth_addrs, eth, sizeof(*key_eth_addrs)); 1171 } 1172 1173 if (dissector_uses_key(flow_dissector, 1174 FLOW_DISSECTOR_KEY_NUM_OF_VLANS)) { 1175 struct flow_dissector_key_num_of_vlans *key_num_of_vlans; 1176 1177 key_num_of_vlans = skb_flow_dissector_target(flow_dissector, 1178 FLOW_DISSECTOR_KEY_NUM_OF_VLANS, 1179 target_container); 1180 key_num_of_vlans->num_of_vlans = 0; 1181 } 1182 1183 proto_again: 1184 fdret = FLOW_DISSECT_RET_CONTINUE; 1185 1186 switch (proto) { 1187 case htons(ETH_P_IP): { 1188 const struct iphdr *iph; 1189 struct iphdr _iph; 1190 1191 iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph); 1192 if (!iph || iph->ihl < 5) { 1193 fdret = FLOW_DISSECT_RET_OUT_BAD; 1194 break; 1195 } 1196 1197 nhoff += iph->ihl * 4; 1198 1199 ip_proto = iph->protocol; 1200 1201 if (dissector_uses_key(flow_dissector, 1202 FLOW_DISSECTOR_KEY_IPV4_ADDRS)) { 1203 key_addrs = skb_flow_dissector_target(flow_dissector, 1204 FLOW_DISSECTOR_KEY_IPV4_ADDRS, 1205 target_container); 1206 1207 memcpy(&key_addrs->v4addrs.src, &iph->saddr, 1208 sizeof(key_addrs->v4addrs.src)); 1209 memcpy(&key_addrs->v4addrs.dst, &iph->daddr, 1210 sizeof(key_addrs->v4addrs.dst)); 1211 key_control->addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; 1212 } 1213 1214 __skb_flow_dissect_ipv4(skb, flow_dissector, 1215 target_container, data, iph); 1216 1217 if (ip_is_fragment(iph)) { 1218 key_control->flags |= FLOW_DIS_IS_FRAGMENT; 1219 1220 if (iph->frag_off & htons(IP_OFFSET)) { 1221 fdret = FLOW_DISSECT_RET_OUT_GOOD; 1222 break; 1223 } else { 1224 key_control->flags |= FLOW_DIS_FIRST_FRAG; 1225 if (!(flags & 1226 FLOW_DISSECTOR_F_PARSE_1ST_FRAG)) { 1227 fdret = FLOW_DISSECT_RET_OUT_GOOD; 1228 break; 1229 } 1230 } 1231 } 1232 1233 break; 1234 } 1235 case htons(ETH_P_IPV6): { 1236 const struct ipv6hdr *iph; 1237 struct ipv6hdr _iph; 1238 1239 iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph); 1240 if (!iph) { 1241 fdret = FLOW_DISSECT_RET_OUT_BAD; 1242 break; 1243 } 1244 1245 ip_proto = iph->nexthdr; 1246 nhoff += sizeof(struct ipv6hdr); 1247 1248 if (dissector_uses_key(flow_dissector, 1249 FLOW_DISSECTOR_KEY_IPV6_ADDRS)) { 1250 key_addrs = skb_flow_dissector_target(flow_dissector, 1251 FLOW_DISSECTOR_KEY_IPV6_ADDRS, 1252 target_container); 1253 1254 memcpy(&key_addrs->v6addrs.src, &iph->saddr, 1255 sizeof(key_addrs->v6addrs.src)); 1256 memcpy(&key_addrs->v6addrs.dst, &iph->daddr, 1257 sizeof(key_addrs->v6addrs.dst)); 1258 key_control->addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 1259 } 1260 1261 if ((dissector_uses_key(flow_dissector, 1262 FLOW_DISSECTOR_KEY_FLOW_LABEL) || 1263 (flags & FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL)) && 1264 ip6_flowlabel(iph)) { 1265 __be32 flow_label = ip6_flowlabel(iph); 1266 1267 if (dissector_uses_key(flow_dissector, 1268 FLOW_DISSECTOR_KEY_FLOW_LABEL)) { 1269 key_tags = skb_flow_dissector_target(flow_dissector, 1270 FLOW_DISSECTOR_KEY_FLOW_LABEL, 1271 target_container); 1272 key_tags->flow_label = ntohl(flow_label); 1273 } 1274 if (flags & FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL) { 1275 fdret = FLOW_DISSECT_RET_OUT_GOOD; 1276 break; 1277 } 1278 } 1279 1280 __skb_flow_dissect_ipv6(skb, flow_dissector, 1281 target_container, data, iph); 1282 1283 break; 1284 } 1285 case htons(ETH_P_8021AD): 1286 case htons(ETH_P_8021Q): { 1287 const struct vlan_hdr *vlan = NULL; 1288 struct vlan_hdr _vlan; 1289 __be16 saved_vlan_tpid = proto; 1290 1291 if (dissector_vlan == FLOW_DISSECTOR_KEY_MAX && 1292 skb && skb_vlan_tag_present(skb)) { 1293 proto = skb->protocol; 1294 } else { 1295 vlan = __skb_header_pointer(skb, nhoff, sizeof(_vlan), 1296 data, hlen, &_vlan); 1297 if (!vlan) { 1298 fdret = FLOW_DISSECT_RET_OUT_BAD; 1299 break; 1300 } 1301 1302 proto = vlan->h_vlan_encapsulated_proto; 1303 nhoff += sizeof(*vlan); 1304 } 1305 1306 if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_NUM_OF_VLANS) && 1307 !(key_control->flags & FLOW_DIS_ENCAPSULATION)) { 1308 struct flow_dissector_key_num_of_vlans *key_nvs; 1309 1310 key_nvs = skb_flow_dissector_target(flow_dissector, 1311 FLOW_DISSECTOR_KEY_NUM_OF_VLANS, 1312 target_container); 1313 key_nvs->num_of_vlans++; 1314 } 1315 1316 if (dissector_vlan == FLOW_DISSECTOR_KEY_MAX) { 1317 dissector_vlan = FLOW_DISSECTOR_KEY_VLAN; 1318 } else if (dissector_vlan == FLOW_DISSECTOR_KEY_VLAN) { 1319 dissector_vlan = FLOW_DISSECTOR_KEY_CVLAN; 1320 } else { 1321 fdret = FLOW_DISSECT_RET_PROTO_AGAIN; 1322 break; 1323 } 1324 1325 if (dissector_uses_key(flow_dissector, dissector_vlan)) { 1326 key_vlan = skb_flow_dissector_target(flow_dissector, 1327 dissector_vlan, 1328 target_container); 1329 1330 if (!vlan) { 1331 key_vlan->vlan_id = skb_vlan_tag_get_id(skb); 1332 key_vlan->vlan_priority = skb_vlan_tag_get_prio(skb); 1333 } else { 1334 key_vlan->vlan_id = ntohs(vlan->h_vlan_TCI) & 1335 VLAN_VID_MASK; 1336 key_vlan->vlan_priority = 1337 (ntohs(vlan->h_vlan_TCI) & 1338 VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT; 1339 } 1340 key_vlan->vlan_tpid = saved_vlan_tpid; 1341 key_vlan->vlan_eth_type = proto; 1342 } 1343 1344 fdret = FLOW_DISSECT_RET_PROTO_AGAIN; 1345 break; 1346 } 1347 case htons(ETH_P_PPP_SES): { 1348 struct { 1349 struct pppoe_hdr hdr; 1350 __be16 proto; 1351 } *hdr, _hdr; 1352 u16 ppp_proto; 1353 1354 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr); 1355 if (!hdr) { 1356 fdret = FLOW_DISSECT_RET_OUT_BAD; 1357 break; 1358 } 1359 1360 if (!is_pppoe_ses_hdr_valid(&hdr->hdr)) { 1361 fdret = FLOW_DISSECT_RET_OUT_BAD; 1362 break; 1363 } 1364 1365 /* least significant bit of the most significant octet 1366 * indicates if protocol field was compressed 1367 */ 1368 ppp_proto = ntohs(hdr->proto); 1369 if (ppp_proto & 0x0100) { 1370 ppp_proto = ppp_proto >> 8; 1371 nhoff += PPPOE_SES_HLEN - 1; 1372 } else { 1373 nhoff += PPPOE_SES_HLEN; 1374 } 1375 1376 if (ppp_proto == PPP_IP) { 1377 proto = htons(ETH_P_IP); 1378 fdret = FLOW_DISSECT_RET_PROTO_AGAIN; 1379 } else if (ppp_proto == PPP_IPV6) { 1380 proto = htons(ETH_P_IPV6); 1381 fdret = FLOW_DISSECT_RET_PROTO_AGAIN; 1382 } else if (ppp_proto == PPP_MPLS_UC) { 1383 proto = htons(ETH_P_MPLS_UC); 1384 fdret = FLOW_DISSECT_RET_PROTO_AGAIN; 1385 } else if (ppp_proto == PPP_MPLS_MC) { 1386 proto = htons(ETH_P_MPLS_MC); 1387 fdret = FLOW_DISSECT_RET_PROTO_AGAIN; 1388 } else if (ppp_proto_is_valid(ppp_proto)) { 1389 fdret = FLOW_DISSECT_RET_OUT_GOOD; 1390 } else { 1391 fdret = FLOW_DISSECT_RET_OUT_BAD; 1392 break; 1393 } 1394 1395 if (dissector_uses_key(flow_dissector, 1396 FLOW_DISSECTOR_KEY_PPPOE)) { 1397 struct flow_dissector_key_pppoe *key_pppoe; 1398 1399 key_pppoe = skb_flow_dissector_target(flow_dissector, 1400 FLOW_DISSECTOR_KEY_PPPOE, 1401 target_container); 1402 key_pppoe->session_id = hdr->hdr.sid; 1403 key_pppoe->ppp_proto = htons(ppp_proto); 1404 key_pppoe->type = htons(ETH_P_PPP_SES); 1405 } 1406 break; 1407 } 1408 case htons(ETH_P_TIPC): { 1409 struct tipc_basic_hdr *hdr, _hdr; 1410 1411 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), 1412 data, hlen, &_hdr); 1413 if (!hdr) { 1414 fdret = FLOW_DISSECT_RET_OUT_BAD; 1415 break; 1416 } 1417 1418 if (dissector_uses_key(flow_dissector, 1419 FLOW_DISSECTOR_KEY_TIPC)) { 1420 key_addrs = skb_flow_dissector_target(flow_dissector, 1421 FLOW_DISSECTOR_KEY_TIPC, 1422 target_container); 1423 key_addrs->tipckey.key = tipc_hdr_rps_key(hdr); 1424 key_control->addr_type = FLOW_DISSECTOR_KEY_TIPC; 1425 } 1426 fdret = FLOW_DISSECT_RET_OUT_GOOD; 1427 break; 1428 } 1429 1430 case htons(ETH_P_MPLS_UC): 1431 case htons(ETH_P_MPLS_MC): 1432 fdret = __skb_flow_dissect_mpls(skb, flow_dissector, 1433 target_container, data, 1434 nhoff, hlen, mpls_lse, 1435 &mpls_el); 1436 nhoff += sizeof(struct mpls_label); 1437 mpls_lse++; 1438 break; 1439 case htons(ETH_P_FCOE): 1440 if ((hlen - nhoff) < FCOE_HEADER_LEN) { 1441 fdret = FLOW_DISSECT_RET_OUT_BAD; 1442 break; 1443 } 1444 1445 nhoff += FCOE_HEADER_LEN; 1446 fdret = FLOW_DISSECT_RET_OUT_GOOD; 1447 break; 1448 1449 case htons(ETH_P_ARP): 1450 case htons(ETH_P_RARP): 1451 fdret = __skb_flow_dissect_arp(skb, flow_dissector, 1452 target_container, data, 1453 nhoff, hlen); 1454 break; 1455 1456 case htons(ETH_P_BATMAN): 1457 fdret = __skb_flow_dissect_batadv(skb, key_control, data, 1458 &proto, &nhoff, hlen, flags); 1459 break; 1460 1461 case htons(ETH_P_1588): { 1462 struct ptp_header *hdr, _hdr; 1463 1464 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, 1465 hlen, &_hdr); 1466 if (!hdr) { 1467 fdret = FLOW_DISSECT_RET_OUT_BAD; 1468 break; 1469 } 1470 1471 nhoff += sizeof(struct ptp_header); 1472 fdret = FLOW_DISSECT_RET_OUT_GOOD; 1473 break; 1474 } 1475 1476 case htons(ETH_P_PRP): 1477 case htons(ETH_P_HSR): { 1478 struct hsr_tag *hdr, _hdr; 1479 1480 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, 1481 &_hdr); 1482 if (!hdr) { 1483 fdret = FLOW_DISSECT_RET_OUT_BAD; 1484 break; 1485 } 1486 1487 proto = hdr->encap_proto; 1488 nhoff += HSR_HLEN; 1489 fdret = FLOW_DISSECT_RET_PROTO_AGAIN; 1490 break; 1491 } 1492 1493 case htons(ETH_P_CFM): 1494 fdret = __skb_flow_dissect_cfm(skb, flow_dissector, 1495 target_container, data, 1496 nhoff, hlen); 1497 break; 1498 1499 default: 1500 fdret = FLOW_DISSECT_RET_OUT_BAD; 1501 break; 1502 } 1503 1504 /* Process result of proto processing */ 1505 switch (fdret) { 1506 case FLOW_DISSECT_RET_OUT_GOOD: 1507 goto out_good; 1508 case FLOW_DISSECT_RET_PROTO_AGAIN: 1509 if (skb_flow_dissect_allowed(&num_hdrs)) 1510 goto proto_again; 1511 goto out_good; 1512 case FLOW_DISSECT_RET_CONTINUE: 1513 case FLOW_DISSECT_RET_IPPROTO_AGAIN: 1514 break; 1515 case FLOW_DISSECT_RET_OUT_BAD: 1516 default: 1517 goto out_bad; 1518 } 1519 1520 ip_proto_again: 1521 fdret = FLOW_DISSECT_RET_CONTINUE; 1522 1523 switch (ip_proto) { 1524 case IPPROTO_GRE: 1525 if (flags & FLOW_DISSECTOR_F_STOP_BEFORE_ENCAP) { 1526 fdret = FLOW_DISSECT_RET_OUT_GOOD; 1527 break; 1528 } 1529 1530 fdret = __skb_flow_dissect_gre(skb, key_control, flow_dissector, 1531 target_container, data, 1532 &proto, &nhoff, &hlen, flags); 1533 break; 1534 1535 case NEXTHDR_HOP: 1536 case NEXTHDR_ROUTING: 1537 case NEXTHDR_DEST: { 1538 u8 _opthdr[2], *opthdr; 1539 1540 if (proto != htons(ETH_P_IPV6)) 1541 break; 1542 1543 opthdr = __skb_header_pointer(skb, nhoff, sizeof(_opthdr), 1544 data, hlen, &_opthdr); 1545 if (!opthdr) { 1546 fdret = FLOW_DISSECT_RET_OUT_BAD; 1547 break; 1548 } 1549 1550 ip_proto = opthdr[0]; 1551 nhoff += (opthdr[1] + 1) << 3; 1552 1553 fdret = FLOW_DISSECT_RET_IPPROTO_AGAIN; 1554 break; 1555 } 1556 case NEXTHDR_FRAGMENT: { 1557 struct frag_hdr _fh, *fh; 1558 1559 if (proto != htons(ETH_P_IPV6)) 1560 break; 1561 1562 fh = __skb_header_pointer(skb, nhoff, sizeof(_fh), 1563 data, hlen, &_fh); 1564 1565 if (!fh) { 1566 fdret = FLOW_DISSECT_RET_OUT_BAD; 1567 break; 1568 } 1569 1570 key_control->flags |= FLOW_DIS_IS_FRAGMENT; 1571 1572 nhoff += sizeof(_fh); 1573 ip_proto = fh->nexthdr; 1574 1575 if (!(fh->frag_off & htons(IP6_OFFSET))) { 1576 key_control->flags |= FLOW_DIS_FIRST_FRAG; 1577 if (flags & FLOW_DISSECTOR_F_PARSE_1ST_FRAG) { 1578 fdret = FLOW_DISSECT_RET_IPPROTO_AGAIN; 1579 break; 1580 } 1581 } 1582 1583 fdret = FLOW_DISSECT_RET_OUT_GOOD; 1584 break; 1585 } 1586 case IPPROTO_IPIP: 1587 if (flags & FLOW_DISSECTOR_F_STOP_BEFORE_ENCAP) { 1588 fdret = FLOW_DISSECT_RET_OUT_GOOD; 1589 break; 1590 } 1591 1592 proto = htons(ETH_P_IP); 1593 1594 key_control->flags |= FLOW_DIS_ENCAPSULATION; 1595 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP) { 1596 fdret = FLOW_DISSECT_RET_OUT_GOOD; 1597 break; 1598 } 1599 1600 fdret = FLOW_DISSECT_RET_PROTO_AGAIN; 1601 break; 1602 1603 case IPPROTO_IPV6: 1604 if (flags & FLOW_DISSECTOR_F_STOP_BEFORE_ENCAP) { 1605 fdret = FLOW_DISSECT_RET_OUT_GOOD; 1606 break; 1607 } 1608 1609 proto = htons(ETH_P_IPV6); 1610 1611 key_control->flags |= FLOW_DIS_ENCAPSULATION; 1612 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP) { 1613 fdret = FLOW_DISSECT_RET_OUT_GOOD; 1614 break; 1615 } 1616 1617 fdret = FLOW_DISSECT_RET_PROTO_AGAIN; 1618 break; 1619 1620 1621 case IPPROTO_MPLS: 1622 proto = htons(ETH_P_MPLS_UC); 1623 fdret = FLOW_DISSECT_RET_PROTO_AGAIN; 1624 break; 1625 1626 case IPPROTO_TCP: 1627 __skb_flow_dissect_tcp(skb, flow_dissector, target_container, 1628 data, nhoff, hlen); 1629 break; 1630 1631 case IPPROTO_ICMP: 1632 case IPPROTO_ICMPV6: 1633 __skb_flow_dissect_icmp(skb, flow_dissector, target_container, 1634 data, nhoff, hlen); 1635 break; 1636 case IPPROTO_L2TP: 1637 __skb_flow_dissect_l2tpv3(skb, flow_dissector, target_container, 1638 data, nhoff, hlen); 1639 break; 1640 case IPPROTO_ESP: 1641 __skb_flow_dissect_esp(skb, flow_dissector, target_container, 1642 data, nhoff, hlen); 1643 break; 1644 case IPPROTO_AH: 1645 __skb_flow_dissect_ah(skb, flow_dissector, target_container, 1646 data, nhoff, hlen); 1647 break; 1648 default: 1649 break; 1650 } 1651 1652 if (!(key_control->flags & FLOW_DIS_IS_FRAGMENT)) 1653 __skb_flow_dissect_ports(skb, flow_dissector, target_container, 1654 data, nhoff, ip_proto, hlen); 1655 1656 /* Process result of IP proto processing */ 1657 switch (fdret) { 1658 case FLOW_DISSECT_RET_PROTO_AGAIN: 1659 if (skb_flow_dissect_allowed(&num_hdrs)) 1660 goto proto_again; 1661 break; 1662 case FLOW_DISSECT_RET_IPPROTO_AGAIN: 1663 if (skb_flow_dissect_allowed(&num_hdrs)) 1664 goto ip_proto_again; 1665 break; 1666 case FLOW_DISSECT_RET_OUT_GOOD: 1667 case FLOW_DISSECT_RET_CONTINUE: 1668 break; 1669 case FLOW_DISSECT_RET_OUT_BAD: 1670 default: 1671 goto out_bad; 1672 } 1673 1674 out_good: 1675 ret = true; 1676 1677 out: 1678 key_control->thoff = min_t(u16, nhoff, skb ? skb->len : hlen); 1679 key_basic->n_proto = proto; 1680 key_basic->ip_proto = ip_proto; 1681 1682 return ret; 1683 1684 out_bad: 1685 ret = false; 1686 goto out; 1687 } 1688 EXPORT_SYMBOL(__skb_flow_dissect); 1689 1690 static siphash_aligned_key_t hashrnd; 1691 static __always_inline void __flow_hash_secret_init(void) 1692 { 1693 net_get_random_once(&hashrnd, sizeof(hashrnd)); 1694 } 1695 1696 static const void *flow_keys_hash_start(const struct flow_keys *flow) 1697 { 1698 BUILD_BUG_ON(FLOW_KEYS_HASH_OFFSET % SIPHASH_ALIGNMENT); 1699 return &flow->FLOW_KEYS_HASH_START_FIELD; 1700 } 1701 1702 static inline size_t flow_keys_hash_length(const struct flow_keys *flow) 1703 { 1704 size_t diff = FLOW_KEYS_HASH_OFFSET + sizeof(flow->addrs); 1705 1706 BUILD_BUG_ON((sizeof(*flow) - FLOW_KEYS_HASH_OFFSET) % sizeof(u32)); 1707 1708 switch (flow->control.addr_type) { 1709 case FLOW_DISSECTOR_KEY_IPV4_ADDRS: 1710 diff -= sizeof(flow->addrs.v4addrs); 1711 break; 1712 case FLOW_DISSECTOR_KEY_IPV6_ADDRS: 1713 diff -= sizeof(flow->addrs.v6addrs); 1714 break; 1715 case FLOW_DISSECTOR_KEY_TIPC: 1716 diff -= sizeof(flow->addrs.tipckey); 1717 break; 1718 } 1719 return sizeof(*flow) - diff; 1720 } 1721 1722 __be32 flow_get_u32_src(const struct flow_keys *flow) 1723 { 1724 switch (flow->control.addr_type) { 1725 case FLOW_DISSECTOR_KEY_IPV4_ADDRS: 1726 return flow->addrs.v4addrs.src; 1727 case FLOW_DISSECTOR_KEY_IPV6_ADDRS: 1728 return (__force __be32)ipv6_addr_hash( 1729 &flow->addrs.v6addrs.src); 1730 case FLOW_DISSECTOR_KEY_TIPC: 1731 return flow->addrs.tipckey.key; 1732 default: 1733 return 0; 1734 } 1735 } 1736 EXPORT_SYMBOL(flow_get_u32_src); 1737 1738 __be32 flow_get_u32_dst(const struct flow_keys *flow) 1739 { 1740 switch (flow->control.addr_type) { 1741 case FLOW_DISSECTOR_KEY_IPV4_ADDRS: 1742 return flow->addrs.v4addrs.dst; 1743 case FLOW_DISSECTOR_KEY_IPV6_ADDRS: 1744 return (__force __be32)ipv6_addr_hash( 1745 &flow->addrs.v6addrs.dst); 1746 default: 1747 return 0; 1748 } 1749 } 1750 EXPORT_SYMBOL(flow_get_u32_dst); 1751 1752 /* Sort the source and destination IP and the ports, 1753 * to have consistent hash within the two directions 1754 */ 1755 static inline void __flow_hash_consistentify(struct flow_keys *keys) 1756 { 1757 int addr_diff, i; 1758 1759 switch (keys->control.addr_type) { 1760 case FLOW_DISSECTOR_KEY_IPV4_ADDRS: 1761 if ((__force u32)keys->addrs.v4addrs.dst < 1762 (__force u32)keys->addrs.v4addrs.src) 1763 swap(keys->addrs.v4addrs.src, keys->addrs.v4addrs.dst); 1764 1765 if ((__force u16)keys->ports.dst < 1766 (__force u16)keys->ports.src) { 1767 swap(keys->ports.src, keys->ports.dst); 1768 } 1769 break; 1770 case FLOW_DISSECTOR_KEY_IPV6_ADDRS: 1771 addr_diff = memcmp(&keys->addrs.v6addrs.dst, 1772 &keys->addrs.v6addrs.src, 1773 sizeof(keys->addrs.v6addrs.dst)); 1774 if (addr_diff < 0) { 1775 for (i = 0; i < 4; i++) 1776 swap(keys->addrs.v6addrs.src.s6_addr32[i], 1777 keys->addrs.v6addrs.dst.s6_addr32[i]); 1778 } 1779 if ((__force u16)keys->ports.dst < 1780 (__force u16)keys->ports.src) { 1781 swap(keys->ports.src, keys->ports.dst); 1782 } 1783 break; 1784 } 1785 } 1786 1787 static inline u32 __flow_hash_from_keys(struct flow_keys *keys, 1788 const siphash_key_t *keyval) 1789 { 1790 u32 hash; 1791 1792 __flow_hash_consistentify(keys); 1793 1794 hash = siphash(flow_keys_hash_start(keys), 1795 flow_keys_hash_length(keys), keyval); 1796 if (!hash) 1797 hash = 1; 1798 1799 return hash; 1800 } 1801 1802 u32 flow_hash_from_keys(struct flow_keys *keys) 1803 { 1804 __flow_hash_secret_init(); 1805 return __flow_hash_from_keys(keys, &hashrnd); 1806 } 1807 EXPORT_SYMBOL(flow_hash_from_keys); 1808 1809 u32 flow_hash_from_keys_seed(struct flow_keys *keys, 1810 const siphash_key_t *keyval) 1811 { 1812 return __flow_hash_from_keys(keys, keyval); 1813 } 1814 EXPORT_SYMBOL(flow_hash_from_keys_seed); 1815 1816 static inline u32 ___skb_get_hash(const struct sk_buff *skb, 1817 struct flow_keys *keys, 1818 const siphash_key_t *keyval) 1819 { 1820 skb_flow_dissect_flow_keys(skb, keys, 1821 FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL); 1822 1823 return __flow_hash_from_keys(keys, keyval); 1824 } 1825 1826 struct _flow_keys_digest_data { 1827 __be16 n_proto; 1828 u8 ip_proto; 1829 u8 padding; 1830 __be32 ports; 1831 __be32 src; 1832 __be32 dst; 1833 }; 1834 1835 void make_flow_keys_digest(struct flow_keys_digest *digest, 1836 const struct flow_keys *flow) 1837 { 1838 struct _flow_keys_digest_data *data = 1839 (struct _flow_keys_digest_data *)digest; 1840 1841 BUILD_BUG_ON(sizeof(*data) > sizeof(*digest)); 1842 1843 memset(digest, 0, sizeof(*digest)); 1844 1845 data->n_proto = flow->basic.n_proto; 1846 data->ip_proto = flow->basic.ip_proto; 1847 data->ports = flow->ports.ports; 1848 data->src = flow->addrs.v4addrs.src; 1849 data->dst = flow->addrs.v4addrs.dst; 1850 } 1851 EXPORT_SYMBOL(make_flow_keys_digest); 1852 1853 static struct flow_dissector flow_keys_dissector_symmetric __read_mostly; 1854 1855 u32 __skb_get_hash_symmetric_net(const struct net *net, const struct sk_buff *skb) 1856 { 1857 struct flow_keys keys; 1858 1859 __flow_hash_secret_init(); 1860 1861 memset(&keys, 0, sizeof(keys)); 1862 __skb_flow_dissect(net, skb, &flow_keys_dissector_symmetric, 1863 &keys, NULL, 0, 0, 0, 0); 1864 1865 return __flow_hash_from_keys(&keys, &hashrnd); 1866 } 1867 EXPORT_SYMBOL_GPL(__skb_get_hash_symmetric_net); 1868 1869 /** 1870 * __skb_get_hash_net: calculate a flow hash 1871 * @net: associated network namespace, derived from @skb if NULL 1872 * @skb: sk_buff to calculate flow hash from 1873 * 1874 * This function calculates a flow hash based on src/dst addresses 1875 * and src/dst port numbers. Sets hash in skb to non-zero hash value 1876 * on success, zero indicates no valid hash. Also, sets l4_hash in skb 1877 * if hash is a canonical 4-tuple hash over transport ports. 1878 */ 1879 void __skb_get_hash_net(const struct net *net, struct sk_buff *skb) 1880 { 1881 struct flow_keys keys; 1882 u32 hash; 1883 1884 memset(&keys, 0, sizeof(keys)); 1885 1886 __skb_flow_dissect(net, skb, &flow_keys_dissector, 1887 &keys, NULL, 0, 0, 0, 1888 FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL); 1889 1890 __flow_hash_secret_init(); 1891 1892 hash = __flow_hash_from_keys(&keys, &hashrnd); 1893 1894 __skb_set_sw_hash(skb, hash, flow_keys_have_l4(&keys)); 1895 } 1896 EXPORT_SYMBOL(__skb_get_hash_net); 1897 1898 __u32 skb_get_hash_perturb(const struct sk_buff *skb, 1899 const siphash_key_t *perturb) 1900 { 1901 struct flow_keys keys; 1902 1903 return ___skb_get_hash(skb, &keys, perturb); 1904 } 1905 EXPORT_SYMBOL(skb_get_hash_perturb); 1906 1907 u32 __skb_get_poff(const struct sk_buff *skb, const void *data, 1908 const struct flow_keys_basic *keys, int hlen) 1909 { 1910 u32 poff = keys->control.thoff; 1911 1912 /* skip L4 headers for fragments after the first */ 1913 if ((keys->control.flags & FLOW_DIS_IS_FRAGMENT) && 1914 !(keys->control.flags & FLOW_DIS_FIRST_FRAG)) 1915 return poff; 1916 1917 switch (keys->basic.ip_proto) { 1918 case IPPROTO_TCP: { 1919 /* access doff as u8 to avoid unaligned access */ 1920 const u8 *doff; 1921 u8 _doff; 1922 1923 doff = __skb_header_pointer(skb, poff + 12, sizeof(_doff), 1924 data, hlen, &_doff); 1925 if (!doff) 1926 return poff; 1927 1928 poff += max_t(u32, sizeof(struct tcphdr), (*doff & 0xF0) >> 2); 1929 break; 1930 } 1931 case IPPROTO_UDP: 1932 case IPPROTO_UDPLITE: 1933 poff += sizeof(struct udphdr); 1934 break; 1935 /* For the rest, we do not really care about header 1936 * extensions at this point for now. 1937 */ 1938 case IPPROTO_ICMP: 1939 poff += sizeof(struct icmphdr); 1940 break; 1941 case IPPROTO_ICMPV6: 1942 poff += sizeof(struct icmp6hdr); 1943 break; 1944 case IPPROTO_IGMP: 1945 poff += sizeof(struct igmphdr); 1946 break; 1947 case IPPROTO_DCCP: 1948 poff += sizeof(struct dccp_hdr); 1949 break; 1950 case IPPROTO_SCTP: 1951 poff += sizeof(struct sctphdr); 1952 break; 1953 } 1954 1955 return poff; 1956 } 1957 1958 /** 1959 * skb_get_poff - get the offset to the payload 1960 * @skb: sk_buff to get the payload offset from 1961 * 1962 * The function will get the offset to the payload as far as it could 1963 * be dissected. The main user is currently BPF, so that we can dynamically 1964 * truncate packets without needing to push actual payload to the user 1965 * space and can analyze headers only, instead. 1966 */ 1967 u32 skb_get_poff(const struct sk_buff *skb) 1968 { 1969 struct flow_keys_basic keys; 1970 1971 if (!skb_flow_dissect_flow_keys_basic(NULL, skb, &keys, 1972 NULL, 0, 0, 0, 0)) 1973 return 0; 1974 1975 return __skb_get_poff(skb, skb->data, &keys, skb_headlen(skb)); 1976 } 1977 1978 __u32 __get_hash_from_flowi6(const struct flowi6 *fl6, struct flow_keys *keys) 1979 { 1980 memset(keys, 0, sizeof(*keys)); 1981 1982 memcpy(&keys->addrs.v6addrs.src, &fl6->saddr, 1983 sizeof(keys->addrs.v6addrs.src)); 1984 memcpy(&keys->addrs.v6addrs.dst, &fl6->daddr, 1985 sizeof(keys->addrs.v6addrs.dst)); 1986 keys->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 1987 keys->ports.src = fl6->fl6_sport; 1988 keys->ports.dst = fl6->fl6_dport; 1989 keys->keyid.keyid = fl6->fl6_gre_key; 1990 keys->tags.flow_label = (__force u32)flowi6_get_flowlabel(fl6); 1991 keys->basic.ip_proto = fl6->flowi6_proto; 1992 1993 return flow_hash_from_keys(keys); 1994 } 1995 EXPORT_SYMBOL(__get_hash_from_flowi6); 1996 1997 static const struct flow_dissector_key flow_keys_dissector_keys[] = { 1998 { 1999 .key_id = FLOW_DISSECTOR_KEY_CONTROL, 2000 .offset = offsetof(struct flow_keys, control), 2001 }, 2002 { 2003 .key_id = FLOW_DISSECTOR_KEY_BASIC, 2004 .offset = offsetof(struct flow_keys, basic), 2005 }, 2006 { 2007 .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS, 2008 .offset = offsetof(struct flow_keys, addrs.v4addrs), 2009 }, 2010 { 2011 .key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS, 2012 .offset = offsetof(struct flow_keys, addrs.v6addrs), 2013 }, 2014 { 2015 .key_id = FLOW_DISSECTOR_KEY_TIPC, 2016 .offset = offsetof(struct flow_keys, addrs.tipckey), 2017 }, 2018 { 2019 .key_id = FLOW_DISSECTOR_KEY_PORTS, 2020 .offset = offsetof(struct flow_keys, ports), 2021 }, 2022 { 2023 .key_id = FLOW_DISSECTOR_KEY_VLAN, 2024 .offset = offsetof(struct flow_keys, vlan), 2025 }, 2026 { 2027 .key_id = FLOW_DISSECTOR_KEY_FLOW_LABEL, 2028 .offset = offsetof(struct flow_keys, tags), 2029 }, 2030 { 2031 .key_id = FLOW_DISSECTOR_KEY_GRE_KEYID, 2032 .offset = offsetof(struct flow_keys, keyid), 2033 }, 2034 }; 2035 2036 static const struct flow_dissector_key flow_keys_dissector_symmetric_keys[] = { 2037 { 2038 .key_id = FLOW_DISSECTOR_KEY_CONTROL, 2039 .offset = offsetof(struct flow_keys, control), 2040 }, 2041 { 2042 .key_id = FLOW_DISSECTOR_KEY_BASIC, 2043 .offset = offsetof(struct flow_keys, basic), 2044 }, 2045 { 2046 .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS, 2047 .offset = offsetof(struct flow_keys, addrs.v4addrs), 2048 }, 2049 { 2050 .key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS, 2051 .offset = offsetof(struct flow_keys, addrs.v6addrs), 2052 }, 2053 { 2054 .key_id = FLOW_DISSECTOR_KEY_PORTS, 2055 .offset = offsetof(struct flow_keys, ports), 2056 }, 2057 }; 2058 2059 static const struct flow_dissector_key flow_keys_basic_dissector_keys[] = { 2060 { 2061 .key_id = FLOW_DISSECTOR_KEY_CONTROL, 2062 .offset = offsetof(struct flow_keys, control), 2063 }, 2064 { 2065 .key_id = FLOW_DISSECTOR_KEY_BASIC, 2066 .offset = offsetof(struct flow_keys, basic), 2067 }, 2068 }; 2069 2070 struct flow_dissector flow_keys_dissector __read_mostly; 2071 EXPORT_SYMBOL(flow_keys_dissector); 2072 2073 struct flow_dissector flow_keys_basic_dissector __read_mostly; 2074 EXPORT_SYMBOL(flow_keys_basic_dissector); 2075 2076 static int __init init_default_flow_dissectors(void) 2077 { 2078 skb_flow_dissector_init(&flow_keys_dissector, 2079 flow_keys_dissector_keys, 2080 ARRAY_SIZE(flow_keys_dissector_keys)); 2081 skb_flow_dissector_init(&flow_keys_dissector_symmetric, 2082 flow_keys_dissector_symmetric_keys, 2083 ARRAY_SIZE(flow_keys_dissector_symmetric_keys)); 2084 skb_flow_dissector_init(&flow_keys_basic_dissector, 2085 flow_keys_basic_dissector_keys, 2086 ARRAY_SIZE(flow_keys_basic_dissector_keys)); 2087 return 0; 2088 } 2089 core_initcall(init_default_flow_dissectors); 2090