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