1 #include <linux/kernel.h> 2 #include <linux/skbuff.h> 3 #include <linux/export.h> 4 #include <linux/ip.h> 5 #include <linux/ipv6.h> 6 #include <linux/if_vlan.h> 7 #include <net/dsa.h> 8 #include <net/ip.h> 9 #include <net/ipv6.h> 10 #include <net/gre.h> 11 #include <net/pptp.h> 12 #include <linux/igmp.h> 13 #include <linux/icmp.h> 14 #include <linux/sctp.h> 15 #include <linux/dccp.h> 16 #include <linux/if_tunnel.h> 17 #include <linux/if_pppox.h> 18 #include <linux/ppp_defs.h> 19 #include <linux/stddef.h> 20 #include <linux/if_ether.h> 21 #include <linux/mpls.h> 22 #include <linux/tcp.h> 23 #include <net/flow_dissector.h> 24 #include <scsi/fc/fc_fcoe.h> 25 26 static void dissector_set_key(struct flow_dissector *flow_dissector, 27 enum flow_dissector_key_id key_id) 28 { 29 flow_dissector->used_keys |= (1 << key_id); 30 } 31 32 void skb_flow_dissector_init(struct flow_dissector *flow_dissector, 33 const struct flow_dissector_key *key, 34 unsigned int key_count) 35 { 36 unsigned int i; 37 38 memset(flow_dissector, 0, sizeof(*flow_dissector)); 39 40 for (i = 0; i < key_count; i++, key++) { 41 /* User should make sure that every key target offset is withing 42 * boundaries of unsigned short. 43 */ 44 BUG_ON(key->offset > USHRT_MAX); 45 BUG_ON(dissector_uses_key(flow_dissector, 46 key->key_id)); 47 48 dissector_set_key(flow_dissector, key->key_id); 49 flow_dissector->offset[key->key_id] = key->offset; 50 } 51 52 /* Ensure that the dissector always includes control and basic key. 53 * That way we are able to avoid handling lack of these in fast path. 54 */ 55 BUG_ON(!dissector_uses_key(flow_dissector, 56 FLOW_DISSECTOR_KEY_CONTROL)); 57 BUG_ON(!dissector_uses_key(flow_dissector, 58 FLOW_DISSECTOR_KEY_BASIC)); 59 } 60 EXPORT_SYMBOL(skb_flow_dissector_init); 61 62 /** 63 * skb_flow_get_be16 - extract be16 entity 64 * @skb: sk_buff to extract from 65 * @poff: offset to extract at 66 * @data: raw buffer pointer to the packet 67 * @hlen: packet header length 68 * 69 * The function will try to retrieve a be32 entity at 70 * offset poff 71 */ 72 static __be16 skb_flow_get_be16(const struct sk_buff *skb, int poff, 73 void *data, int hlen) 74 { 75 __be16 *u, _u; 76 77 u = __skb_header_pointer(skb, poff, sizeof(_u), data, hlen, &_u); 78 if (u) 79 return *u; 80 81 return 0; 82 } 83 84 /** 85 * __skb_flow_get_ports - extract the upper layer ports and return them 86 * @skb: sk_buff to extract the ports from 87 * @thoff: transport header offset 88 * @ip_proto: protocol for which to get port offset 89 * @data: raw buffer pointer to the packet, if NULL use skb->data 90 * @hlen: packet header length, if @data is NULL use skb_headlen(skb) 91 * 92 * The function will try to retrieve the ports at offset thoff + poff where poff 93 * is the protocol port offset returned from proto_ports_offset 94 */ 95 __be32 __skb_flow_get_ports(const struct sk_buff *skb, int thoff, u8 ip_proto, 96 void *data, int hlen) 97 { 98 int poff = proto_ports_offset(ip_proto); 99 100 if (!data) { 101 data = skb->data; 102 hlen = skb_headlen(skb); 103 } 104 105 if (poff >= 0) { 106 __be32 *ports, _ports; 107 108 ports = __skb_header_pointer(skb, thoff + poff, 109 sizeof(_ports), data, hlen, &_ports); 110 if (ports) 111 return *ports; 112 } 113 114 return 0; 115 } 116 EXPORT_SYMBOL(__skb_flow_get_ports); 117 118 enum flow_dissect_ret { 119 FLOW_DISSECT_RET_OUT_GOOD, 120 FLOW_DISSECT_RET_OUT_BAD, 121 FLOW_DISSECT_RET_OUT_PROTO_AGAIN, 122 }; 123 124 static enum flow_dissect_ret 125 __skb_flow_dissect_mpls(const struct sk_buff *skb, 126 struct flow_dissector *flow_dissector, 127 void *target_container, void *data, int nhoff, int hlen) 128 { 129 struct flow_dissector_key_keyid *key_keyid; 130 struct mpls_label *hdr, _hdr[2]; 131 u32 entry, label; 132 133 if (!dissector_uses_key(flow_dissector, 134 FLOW_DISSECTOR_KEY_MPLS_ENTROPY) && 135 !dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_MPLS)) 136 return FLOW_DISSECT_RET_OUT_GOOD; 137 138 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, 139 hlen, &_hdr); 140 if (!hdr) 141 return FLOW_DISSECT_RET_OUT_BAD; 142 143 entry = ntohl(hdr[0].entry); 144 label = (entry & MPLS_LS_LABEL_MASK) >> MPLS_LS_LABEL_SHIFT; 145 146 if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_MPLS)) { 147 struct flow_dissector_key_mpls *key_mpls; 148 149 key_mpls = skb_flow_dissector_target(flow_dissector, 150 FLOW_DISSECTOR_KEY_MPLS, 151 target_container); 152 key_mpls->mpls_label = label; 153 key_mpls->mpls_ttl = (entry & MPLS_LS_TTL_MASK) 154 >> MPLS_LS_TTL_SHIFT; 155 key_mpls->mpls_tc = (entry & MPLS_LS_TC_MASK) 156 >> MPLS_LS_TC_SHIFT; 157 key_mpls->mpls_bos = (entry & MPLS_LS_S_MASK) 158 >> MPLS_LS_S_SHIFT; 159 } 160 161 if (label == MPLS_LABEL_ENTROPY) { 162 key_keyid = skb_flow_dissector_target(flow_dissector, 163 FLOW_DISSECTOR_KEY_MPLS_ENTROPY, 164 target_container); 165 key_keyid->keyid = hdr[1].entry & htonl(MPLS_LS_LABEL_MASK); 166 } 167 return FLOW_DISSECT_RET_OUT_GOOD; 168 } 169 170 static enum flow_dissect_ret 171 __skb_flow_dissect_arp(const struct sk_buff *skb, 172 struct flow_dissector *flow_dissector, 173 void *target_container, void *data, int nhoff, int hlen) 174 { 175 struct flow_dissector_key_arp *key_arp; 176 struct { 177 unsigned char ar_sha[ETH_ALEN]; 178 unsigned char ar_sip[4]; 179 unsigned char ar_tha[ETH_ALEN]; 180 unsigned char ar_tip[4]; 181 } *arp_eth, _arp_eth; 182 const struct arphdr *arp; 183 struct arphdr _arp; 184 185 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_ARP)) 186 return FLOW_DISSECT_RET_OUT_GOOD; 187 188 arp = __skb_header_pointer(skb, nhoff, sizeof(_arp), data, 189 hlen, &_arp); 190 if (!arp) 191 return FLOW_DISSECT_RET_OUT_BAD; 192 193 if (arp->ar_hrd != htons(ARPHRD_ETHER) || 194 arp->ar_pro != htons(ETH_P_IP) || 195 arp->ar_hln != ETH_ALEN || 196 arp->ar_pln != 4 || 197 (arp->ar_op != htons(ARPOP_REPLY) && 198 arp->ar_op != htons(ARPOP_REQUEST))) 199 return FLOW_DISSECT_RET_OUT_BAD; 200 201 arp_eth = __skb_header_pointer(skb, nhoff + sizeof(_arp), 202 sizeof(_arp_eth), data, 203 hlen, &_arp_eth); 204 if (!arp_eth) 205 return FLOW_DISSECT_RET_OUT_BAD; 206 207 key_arp = skb_flow_dissector_target(flow_dissector, 208 FLOW_DISSECTOR_KEY_ARP, 209 target_container); 210 211 memcpy(&key_arp->sip, arp_eth->ar_sip, sizeof(key_arp->sip)); 212 memcpy(&key_arp->tip, arp_eth->ar_tip, sizeof(key_arp->tip)); 213 214 /* Only store the lower byte of the opcode; 215 * this covers ARPOP_REPLY and ARPOP_REQUEST. 216 */ 217 key_arp->op = ntohs(arp->ar_op) & 0xff; 218 219 ether_addr_copy(key_arp->sha, arp_eth->ar_sha); 220 ether_addr_copy(key_arp->tha, arp_eth->ar_tha); 221 222 return FLOW_DISSECT_RET_OUT_GOOD; 223 } 224 225 static enum flow_dissect_ret 226 __skb_flow_dissect_gre(const struct sk_buff *skb, 227 struct flow_dissector_key_control *key_control, 228 struct flow_dissector *flow_dissector, 229 void *target_container, void *data, 230 __be16 *p_proto, int *p_nhoff, int *p_hlen, 231 unsigned int flags) 232 { 233 struct flow_dissector_key_keyid *key_keyid; 234 struct gre_base_hdr *hdr, _hdr; 235 int offset = 0; 236 u16 gre_ver; 237 238 hdr = __skb_header_pointer(skb, *p_nhoff, sizeof(_hdr), 239 data, *p_hlen, &_hdr); 240 if (!hdr) 241 return FLOW_DISSECT_RET_OUT_BAD; 242 243 /* Only look inside GRE without routing */ 244 if (hdr->flags & GRE_ROUTING) 245 return FLOW_DISSECT_RET_OUT_GOOD; 246 247 /* Only look inside GRE for version 0 and 1 */ 248 gre_ver = ntohs(hdr->flags & GRE_VERSION); 249 if (gre_ver > 1) 250 return FLOW_DISSECT_RET_OUT_GOOD; 251 252 *p_proto = hdr->protocol; 253 if (gre_ver) { 254 /* Version1 must be PPTP, and check the flags */ 255 if (!(*p_proto == GRE_PROTO_PPP && (hdr->flags & GRE_KEY))) 256 return FLOW_DISSECT_RET_OUT_GOOD; 257 } 258 259 offset += sizeof(struct gre_base_hdr); 260 261 if (hdr->flags & GRE_CSUM) 262 offset += sizeof(((struct gre_full_hdr *) 0)->csum) + 263 sizeof(((struct gre_full_hdr *) 0)->reserved1); 264 265 if (hdr->flags & GRE_KEY) { 266 const __be32 *keyid; 267 __be32 _keyid; 268 269 keyid = __skb_header_pointer(skb, *p_nhoff + offset, 270 sizeof(_keyid), 271 data, *p_hlen, &_keyid); 272 if (!keyid) 273 return FLOW_DISSECT_RET_OUT_BAD; 274 275 if (dissector_uses_key(flow_dissector, 276 FLOW_DISSECTOR_KEY_GRE_KEYID)) { 277 key_keyid = skb_flow_dissector_target(flow_dissector, 278 FLOW_DISSECTOR_KEY_GRE_KEYID, 279 target_container); 280 if (gre_ver == 0) 281 key_keyid->keyid = *keyid; 282 else 283 key_keyid->keyid = *keyid & GRE_PPTP_KEY_MASK; 284 } 285 offset += sizeof(((struct gre_full_hdr *) 0)->key); 286 } 287 288 if (hdr->flags & GRE_SEQ) 289 offset += sizeof(((struct pptp_gre_header *) 0)->seq); 290 291 if (gre_ver == 0) { 292 if (*p_proto == htons(ETH_P_TEB)) { 293 const struct ethhdr *eth; 294 struct ethhdr _eth; 295 296 eth = __skb_header_pointer(skb, *p_nhoff + offset, 297 sizeof(_eth), 298 data, *p_hlen, &_eth); 299 if (!eth) 300 return FLOW_DISSECT_RET_OUT_BAD; 301 *p_proto = eth->h_proto; 302 offset += sizeof(*eth); 303 304 /* Cap headers that we access via pointers at the 305 * end of the Ethernet header as our maximum alignment 306 * at that point is only 2 bytes. 307 */ 308 if (NET_IP_ALIGN) 309 *p_hlen = *p_nhoff + offset; 310 } 311 } else { /* version 1, must be PPTP */ 312 u8 _ppp_hdr[PPP_HDRLEN]; 313 u8 *ppp_hdr; 314 315 if (hdr->flags & GRE_ACK) 316 offset += sizeof(((struct pptp_gre_header *) 0)->ack); 317 318 ppp_hdr = __skb_header_pointer(skb, *p_nhoff + offset, 319 sizeof(_ppp_hdr), 320 data, *p_hlen, _ppp_hdr); 321 if (!ppp_hdr) 322 return FLOW_DISSECT_RET_OUT_BAD; 323 324 switch (PPP_PROTOCOL(ppp_hdr)) { 325 case PPP_IP: 326 *p_proto = htons(ETH_P_IP); 327 break; 328 case PPP_IPV6: 329 *p_proto = htons(ETH_P_IPV6); 330 break; 331 default: 332 /* Could probably catch some more like MPLS */ 333 break; 334 } 335 336 offset += PPP_HDRLEN; 337 } 338 339 *p_nhoff += offset; 340 key_control->flags |= FLOW_DIS_ENCAPSULATION; 341 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP) 342 return FLOW_DISSECT_RET_OUT_GOOD; 343 344 return FLOW_DISSECT_RET_OUT_PROTO_AGAIN; 345 } 346 347 static void 348 __skb_flow_dissect_tcp(const struct sk_buff *skb, 349 struct flow_dissector *flow_dissector, 350 void *target_container, void *data, int thoff, int hlen) 351 { 352 struct flow_dissector_key_tcp *key_tcp; 353 struct tcphdr *th, _th; 354 355 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_TCP)) 356 return; 357 358 th = __skb_header_pointer(skb, thoff, sizeof(_th), data, hlen, &_th); 359 if (!th) 360 return; 361 362 if (unlikely(__tcp_hdrlen(th) < sizeof(_th))) 363 return; 364 365 key_tcp = skb_flow_dissector_target(flow_dissector, 366 FLOW_DISSECTOR_KEY_TCP, 367 target_container); 368 key_tcp->flags = (*(__be16 *) &tcp_flag_word(th) & htons(0x0FFF)); 369 } 370 371 static void 372 __skb_flow_dissect_ipv4(const struct sk_buff *skb, 373 struct flow_dissector *flow_dissector, 374 void *target_container, void *data, const struct iphdr *iph) 375 { 376 struct flow_dissector_key_ip *key_ip; 377 378 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_IP)) 379 return; 380 381 key_ip = skb_flow_dissector_target(flow_dissector, 382 FLOW_DISSECTOR_KEY_IP, 383 target_container); 384 key_ip->tos = iph->tos; 385 key_ip->ttl = iph->ttl; 386 } 387 388 static void 389 __skb_flow_dissect_ipv6(const struct sk_buff *skb, 390 struct flow_dissector *flow_dissector, 391 void *target_container, void *data, const struct ipv6hdr *iph) 392 { 393 struct flow_dissector_key_ip *key_ip; 394 395 if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_IP)) 396 return; 397 398 key_ip = skb_flow_dissector_target(flow_dissector, 399 FLOW_DISSECTOR_KEY_IP, 400 target_container); 401 key_ip->tos = ipv6_get_dsfield(iph); 402 key_ip->ttl = iph->hop_limit; 403 } 404 405 /** 406 * __skb_flow_dissect - extract the flow_keys struct and return it 407 * @skb: sk_buff to extract the flow from, can be NULL if the rest are specified 408 * @flow_dissector: list of keys to dissect 409 * @target_container: target structure to put dissected values into 410 * @data: raw buffer pointer to the packet, if NULL use skb->data 411 * @proto: protocol for which to get the flow, if @data is NULL use skb->protocol 412 * @nhoff: network header offset, if @data is NULL use skb_network_offset(skb) 413 * @hlen: packet header length, if @data is NULL use skb_headlen(skb) 414 * 415 * The function will try to retrieve individual keys into target specified 416 * by flow_dissector from either the skbuff or a raw buffer specified by the 417 * rest parameters. 418 * 419 * Caller must take care of zeroing target container memory. 420 */ 421 bool __skb_flow_dissect(const struct sk_buff *skb, 422 struct flow_dissector *flow_dissector, 423 void *target_container, 424 void *data, __be16 proto, int nhoff, int hlen, 425 unsigned int flags) 426 { 427 struct flow_dissector_key_control *key_control; 428 struct flow_dissector_key_basic *key_basic; 429 struct flow_dissector_key_addrs *key_addrs; 430 struct flow_dissector_key_ports *key_ports; 431 struct flow_dissector_key_icmp *key_icmp; 432 struct flow_dissector_key_tags *key_tags; 433 struct flow_dissector_key_vlan *key_vlan; 434 bool skip_vlan = false; 435 u8 ip_proto = 0; 436 bool ret; 437 438 if (!data) { 439 data = skb->data; 440 proto = skb_vlan_tag_present(skb) ? 441 skb->vlan_proto : skb->protocol; 442 nhoff = skb_network_offset(skb); 443 hlen = skb_headlen(skb); 444 #if IS_ENABLED(CONFIG_NET_DSA) 445 if (unlikely(skb->dev && netdev_uses_dsa(skb->dev))) { 446 const struct dsa_device_ops *ops; 447 int offset; 448 449 ops = skb->dev->dsa_ptr->tag_ops; 450 if (ops->flow_dissect && 451 !ops->flow_dissect(skb, &proto, &offset)) { 452 hlen -= offset; 453 nhoff += offset; 454 } 455 } 456 #endif 457 } 458 459 /* It is ensured by skb_flow_dissector_init() that control key will 460 * be always present. 461 */ 462 key_control = skb_flow_dissector_target(flow_dissector, 463 FLOW_DISSECTOR_KEY_CONTROL, 464 target_container); 465 466 /* It is ensured by skb_flow_dissector_init() that basic key will 467 * be always present. 468 */ 469 key_basic = skb_flow_dissector_target(flow_dissector, 470 FLOW_DISSECTOR_KEY_BASIC, 471 target_container); 472 473 if (dissector_uses_key(flow_dissector, 474 FLOW_DISSECTOR_KEY_ETH_ADDRS)) { 475 struct ethhdr *eth = eth_hdr(skb); 476 struct flow_dissector_key_eth_addrs *key_eth_addrs; 477 478 key_eth_addrs = skb_flow_dissector_target(flow_dissector, 479 FLOW_DISSECTOR_KEY_ETH_ADDRS, 480 target_container); 481 memcpy(key_eth_addrs, ð->h_dest, sizeof(*key_eth_addrs)); 482 } 483 484 proto_again: 485 switch (proto) { 486 case htons(ETH_P_IP): { 487 const struct iphdr *iph; 488 struct iphdr _iph; 489 ip: 490 iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph); 491 if (!iph || iph->ihl < 5) 492 goto out_bad; 493 nhoff += iph->ihl * 4; 494 495 ip_proto = iph->protocol; 496 497 if (dissector_uses_key(flow_dissector, 498 FLOW_DISSECTOR_KEY_IPV4_ADDRS)) { 499 key_addrs = skb_flow_dissector_target(flow_dissector, 500 FLOW_DISSECTOR_KEY_IPV4_ADDRS, 501 target_container); 502 503 memcpy(&key_addrs->v4addrs, &iph->saddr, 504 sizeof(key_addrs->v4addrs)); 505 key_control->addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; 506 } 507 508 if (ip_is_fragment(iph)) { 509 key_control->flags |= FLOW_DIS_IS_FRAGMENT; 510 511 if (iph->frag_off & htons(IP_OFFSET)) { 512 goto out_good; 513 } else { 514 key_control->flags |= FLOW_DIS_FIRST_FRAG; 515 if (!(flags & FLOW_DISSECTOR_F_PARSE_1ST_FRAG)) 516 goto out_good; 517 } 518 } 519 520 __skb_flow_dissect_ipv4(skb, flow_dissector, 521 target_container, data, iph); 522 523 if (flags & FLOW_DISSECTOR_F_STOP_AT_L3) 524 goto out_good; 525 526 break; 527 } 528 case htons(ETH_P_IPV6): { 529 const struct ipv6hdr *iph; 530 struct ipv6hdr _iph; 531 532 ipv6: 533 iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph); 534 if (!iph) 535 goto out_bad; 536 537 ip_proto = iph->nexthdr; 538 nhoff += sizeof(struct ipv6hdr); 539 540 if (dissector_uses_key(flow_dissector, 541 FLOW_DISSECTOR_KEY_IPV6_ADDRS)) { 542 key_addrs = skb_flow_dissector_target(flow_dissector, 543 FLOW_DISSECTOR_KEY_IPV6_ADDRS, 544 target_container); 545 546 memcpy(&key_addrs->v6addrs, &iph->saddr, 547 sizeof(key_addrs->v6addrs)); 548 key_control->addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 549 } 550 551 if ((dissector_uses_key(flow_dissector, 552 FLOW_DISSECTOR_KEY_FLOW_LABEL) || 553 (flags & FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL)) && 554 ip6_flowlabel(iph)) { 555 __be32 flow_label = ip6_flowlabel(iph); 556 557 if (dissector_uses_key(flow_dissector, 558 FLOW_DISSECTOR_KEY_FLOW_LABEL)) { 559 key_tags = skb_flow_dissector_target(flow_dissector, 560 FLOW_DISSECTOR_KEY_FLOW_LABEL, 561 target_container); 562 key_tags->flow_label = ntohl(flow_label); 563 } 564 if (flags & FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL) 565 goto out_good; 566 } 567 568 __skb_flow_dissect_ipv6(skb, flow_dissector, 569 target_container, data, iph); 570 571 if (flags & FLOW_DISSECTOR_F_STOP_AT_L3) 572 goto out_good; 573 574 break; 575 } 576 case htons(ETH_P_8021AD): 577 case htons(ETH_P_8021Q): { 578 const struct vlan_hdr *vlan; 579 struct vlan_hdr _vlan; 580 bool vlan_tag_present = skb && skb_vlan_tag_present(skb); 581 582 if (vlan_tag_present) 583 proto = skb->protocol; 584 585 if (!vlan_tag_present || eth_type_vlan(skb->protocol)) { 586 vlan = __skb_header_pointer(skb, nhoff, sizeof(_vlan), 587 data, hlen, &_vlan); 588 if (!vlan) 589 goto out_bad; 590 proto = vlan->h_vlan_encapsulated_proto; 591 nhoff += sizeof(*vlan); 592 if (skip_vlan) 593 goto proto_again; 594 } 595 596 skip_vlan = true; 597 if (dissector_uses_key(flow_dissector, 598 FLOW_DISSECTOR_KEY_VLAN)) { 599 key_vlan = skb_flow_dissector_target(flow_dissector, 600 FLOW_DISSECTOR_KEY_VLAN, 601 target_container); 602 603 if (vlan_tag_present) { 604 key_vlan->vlan_id = skb_vlan_tag_get_id(skb); 605 key_vlan->vlan_priority = 606 (skb_vlan_tag_get_prio(skb) >> VLAN_PRIO_SHIFT); 607 } else { 608 key_vlan->vlan_id = ntohs(vlan->h_vlan_TCI) & 609 VLAN_VID_MASK; 610 key_vlan->vlan_priority = 611 (ntohs(vlan->h_vlan_TCI) & 612 VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT; 613 } 614 } 615 616 goto proto_again; 617 } 618 case htons(ETH_P_PPP_SES): { 619 struct { 620 struct pppoe_hdr hdr; 621 __be16 proto; 622 } *hdr, _hdr; 623 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr); 624 if (!hdr) 625 goto out_bad; 626 proto = hdr->proto; 627 nhoff += PPPOE_SES_HLEN; 628 switch (proto) { 629 case htons(PPP_IP): 630 goto ip; 631 case htons(PPP_IPV6): 632 goto ipv6; 633 default: 634 goto out_bad; 635 } 636 } 637 case htons(ETH_P_TIPC): { 638 struct { 639 __be32 pre[3]; 640 __be32 srcnode; 641 } *hdr, _hdr; 642 hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr); 643 if (!hdr) 644 goto out_bad; 645 646 if (dissector_uses_key(flow_dissector, 647 FLOW_DISSECTOR_KEY_TIPC_ADDRS)) { 648 key_addrs = skb_flow_dissector_target(flow_dissector, 649 FLOW_DISSECTOR_KEY_TIPC_ADDRS, 650 target_container); 651 key_addrs->tipcaddrs.srcnode = hdr->srcnode; 652 key_control->addr_type = FLOW_DISSECTOR_KEY_TIPC_ADDRS; 653 } 654 goto out_good; 655 } 656 657 case htons(ETH_P_MPLS_UC): 658 case htons(ETH_P_MPLS_MC): 659 mpls: 660 switch (__skb_flow_dissect_mpls(skb, flow_dissector, 661 target_container, data, 662 nhoff, hlen)) { 663 case FLOW_DISSECT_RET_OUT_GOOD: 664 goto out_good; 665 case FLOW_DISSECT_RET_OUT_BAD: 666 default: 667 goto out_bad; 668 } 669 case htons(ETH_P_FCOE): 670 if ((hlen - nhoff) < FCOE_HEADER_LEN) 671 goto out_bad; 672 673 nhoff += FCOE_HEADER_LEN; 674 goto out_good; 675 676 case htons(ETH_P_ARP): 677 case htons(ETH_P_RARP): 678 switch (__skb_flow_dissect_arp(skb, flow_dissector, 679 target_container, data, 680 nhoff, hlen)) { 681 case FLOW_DISSECT_RET_OUT_GOOD: 682 goto out_good; 683 case FLOW_DISSECT_RET_OUT_BAD: 684 default: 685 goto out_bad; 686 } 687 default: 688 goto out_bad; 689 } 690 691 ip_proto_again: 692 switch (ip_proto) { 693 case IPPROTO_GRE: 694 switch (__skb_flow_dissect_gre(skb, key_control, flow_dissector, 695 target_container, data, 696 &proto, &nhoff, &hlen, flags)) { 697 case FLOW_DISSECT_RET_OUT_GOOD: 698 goto out_good; 699 case FLOW_DISSECT_RET_OUT_BAD: 700 goto out_bad; 701 case FLOW_DISSECT_RET_OUT_PROTO_AGAIN: 702 goto proto_again; 703 } 704 case NEXTHDR_HOP: 705 case NEXTHDR_ROUTING: 706 case NEXTHDR_DEST: { 707 u8 _opthdr[2], *opthdr; 708 709 if (proto != htons(ETH_P_IPV6)) 710 break; 711 712 opthdr = __skb_header_pointer(skb, nhoff, sizeof(_opthdr), 713 data, hlen, &_opthdr); 714 if (!opthdr) 715 goto out_bad; 716 717 ip_proto = opthdr[0]; 718 nhoff += (opthdr[1] + 1) << 3; 719 720 goto ip_proto_again; 721 } 722 case NEXTHDR_FRAGMENT: { 723 struct frag_hdr _fh, *fh; 724 725 if (proto != htons(ETH_P_IPV6)) 726 break; 727 728 fh = __skb_header_pointer(skb, nhoff, sizeof(_fh), 729 data, hlen, &_fh); 730 731 if (!fh) 732 goto out_bad; 733 734 key_control->flags |= FLOW_DIS_IS_FRAGMENT; 735 736 nhoff += sizeof(_fh); 737 ip_proto = fh->nexthdr; 738 739 if (!(fh->frag_off & htons(IP6_OFFSET))) { 740 key_control->flags |= FLOW_DIS_FIRST_FRAG; 741 if (flags & FLOW_DISSECTOR_F_PARSE_1ST_FRAG) 742 goto ip_proto_again; 743 } 744 goto out_good; 745 } 746 case IPPROTO_IPIP: 747 proto = htons(ETH_P_IP); 748 749 key_control->flags |= FLOW_DIS_ENCAPSULATION; 750 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP) 751 goto out_good; 752 753 goto ip; 754 case IPPROTO_IPV6: 755 proto = htons(ETH_P_IPV6); 756 757 key_control->flags |= FLOW_DIS_ENCAPSULATION; 758 if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP) 759 goto out_good; 760 761 goto ipv6; 762 case IPPROTO_MPLS: 763 proto = htons(ETH_P_MPLS_UC); 764 goto mpls; 765 case IPPROTO_TCP: 766 __skb_flow_dissect_tcp(skb, flow_dissector, target_container, 767 data, nhoff, hlen); 768 break; 769 default: 770 break; 771 } 772 773 if (dissector_uses_key(flow_dissector, 774 FLOW_DISSECTOR_KEY_PORTS)) { 775 key_ports = skb_flow_dissector_target(flow_dissector, 776 FLOW_DISSECTOR_KEY_PORTS, 777 target_container); 778 key_ports->ports = __skb_flow_get_ports(skb, nhoff, ip_proto, 779 data, hlen); 780 } 781 782 if (dissector_uses_key(flow_dissector, 783 FLOW_DISSECTOR_KEY_ICMP)) { 784 key_icmp = skb_flow_dissector_target(flow_dissector, 785 FLOW_DISSECTOR_KEY_ICMP, 786 target_container); 787 key_icmp->icmp = skb_flow_get_be16(skb, nhoff, data, hlen); 788 } 789 790 out_good: 791 ret = true; 792 793 key_control->thoff = (u16)nhoff; 794 out: 795 key_basic->n_proto = proto; 796 key_basic->ip_proto = ip_proto; 797 798 return ret; 799 800 out_bad: 801 ret = false; 802 key_control->thoff = min_t(u16, nhoff, skb ? skb->len : hlen); 803 goto out; 804 } 805 EXPORT_SYMBOL(__skb_flow_dissect); 806 807 static u32 hashrnd __read_mostly; 808 static __always_inline void __flow_hash_secret_init(void) 809 { 810 net_get_random_once(&hashrnd, sizeof(hashrnd)); 811 } 812 813 static __always_inline u32 __flow_hash_words(const u32 *words, u32 length, 814 u32 keyval) 815 { 816 return jhash2(words, length, keyval); 817 } 818 819 static inline const u32 *flow_keys_hash_start(const struct flow_keys *flow) 820 { 821 const void *p = flow; 822 823 BUILD_BUG_ON(FLOW_KEYS_HASH_OFFSET % sizeof(u32)); 824 return (const u32 *)(p + FLOW_KEYS_HASH_OFFSET); 825 } 826 827 static inline size_t flow_keys_hash_length(const struct flow_keys *flow) 828 { 829 size_t diff = FLOW_KEYS_HASH_OFFSET + sizeof(flow->addrs); 830 BUILD_BUG_ON((sizeof(*flow) - FLOW_KEYS_HASH_OFFSET) % sizeof(u32)); 831 BUILD_BUG_ON(offsetof(typeof(*flow), addrs) != 832 sizeof(*flow) - sizeof(flow->addrs)); 833 834 switch (flow->control.addr_type) { 835 case FLOW_DISSECTOR_KEY_IPV4_ADDRS: 836 diff -= sizeof(flow->addrs.v4addrs); 837 break; 838 case FLOW_DISSECTOR_KEY_IPV6_ADDRS: 839 diff -= sizeof(flow->addrs.v6addrs); 840 break; 841 case FLOW_DISSECTOR_KEY_TIPC_ADDRS: 842 diff -= sizeof(flow->addrs.tipcaddrs); 843 break; 844 } 845 return (sizeof(*flow) - diff) / sizeof(u32); 846 } 847 848 __be32 flow_get_u32_src(const struct flow_keys *flow) 849 { 850 switch (flow->control.addr_type) { 851 case FLOW_DISSECTOR_KEY_IPV4_ADDRS: 852 return flow->addrs.v4addrs.src; 853 case FLOW_DISSECTOR_KEY_IPV6_ADDRS: 854 return (__force __be32)ipv6_addr_hash( 855 &flow->addrs.v6addrs.src); 856 case FLOW_DISSECTOR_KEY_TIPC_ADDRS: 857 return flow->addrs.tipcaddrs.srcnode; 858 default: 859 return 0; 860 } 861 } 862 EXPORT_SYMBOL(flow_get_u32_src); 863 864 __be32 flow_get_u32_dst(const struct flow_keys *flow) 865 { 866 switch (flow->control.addr_type) { 867 case FLOW_DISSECTOR_KEY_IPV4_ADDRS: 868 return flow->addrs.v4addrs.dst; 869 case FLOW_DISSECTOR_KEY_IPV6_ADDRS: 870 return (__force __be32)ipv6_addr_hash( 871 &flow->addrs.v6addrs.dst); 872 default: 873 return 0; 874 } 875 } 876 EXPORT_SYMBOL(flow_get_u32_dst); 877 878 static inline void __flow_hash_consistentify(struct flow_keys *keys) 879 { 880 int addr_diff, i; 881 882 switch (keys->control.addr_type) { 883 case FLOW_DISSECTOR_KEY_IPV4_ADDRS: 884 addr_diff = (__force u32)keys->addrs.v4addrs.dst - 885 (__force u32)keys->addrs.v4addrs.src; 886 if ((addr_diff < 0) || 887 (addr_diff == 0 && 888 ((__force u16)keys->ports.dst < 889 (__force u16)keys->ports.src))) { 890 swap(keys->addrs.v4addrs.src, keys->addrs.v4addrs.dst); 891 swap(keys->ports.src, keys->ports.dst); 892 } 893 break; 894 case FLOW_DISSECTOR_KEY_IPV6_ADDRS: 895 addr_diff = memcmp(&keys->addrs.v6addrs.dst, 896 &keys->addrs.v6addrs.src, 897 sizeof(keys->addrs.v6addrs.dst)); 898 if ((addr_diff < 0) || 899 (addr_diff == 0 && 900 ((__force u16)keys->ports.dst < 901 (__force u16)keys->ports.src))) { 902 for (i = 0; i < 4; i++) 903 swap(keys->addrs.v6addrs.src.s6_addr32[i], 904 keys->addrs.v6addrs.dst.s6_addr32[i]); 905 swap(keys->ports.src, keys->ports.dst); 906 } 907 break; 908 } 909 } 910 911 static inline u32 __flow_hash_from_keys(struct flow_keys *keys, u32 keyval) 912 { 913 u32 hash; 914 915 __flow_hash_consistentify(keys); 916 917 hash = __flow_hash_words(flow_keys_hash_start(keys), 918 flow_keys_hash_length(keys), keyval); 919 if (!hash) 920 hash = 1; 921 922 return hash; 923 } 924 925 u32 flow_hash_from_keys(struct flow_keys *keys) 926 { 927 __flow_hash_secret_init(); 928 return __flow_hash_from_keys(keys, hashrnd); 929 } 930 EXPORT_SYMBOL(flow_hash_from_keys); 931 932 static inline u32 ___skb_get_hash(const struct sk_buff *skb, 933 struct flow_keys *keys, u32 keyval) 934 { 935 skb_flow_dissect_flow_keys(skb, keys, 936 FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL); 937 938 return __flow_hash_from_keys(keys, keyval); 939 } 940 941 struct _flow_keys_digest_data { 942 __be16 n_proto; 943 u8 ip_proto; 944 u8 padding; 945 __be32 ports; 946 __be32 src; 947 __be32 dst; 948 }; 949 950 void make_flow_keys_digest(struct flow_keys_digest *digest, 951 const struct flow_keys *flow) 952 { 953 struct _flow_keys_digest_data *data = 954 (struct _flow_keys_digest_data *)digest; 955 956 BUILD_BUG_ON(sizeof(*data) > sizeof(*digest)); 957 958 memset(digest, 0, sizeof(*digest)); 959 960 data->n_proto = flow->basic.n_proto; 961 data->ip_proto = flow->basic.ip_proto; 962 data->ports = flow->ports.ports; 963 data->src = flow->addrs.v4addrs.src; 964 data->dst = flow->addrs.v4addrs.dst; 965 } 966 EXPORT_SYMBOL(make_flow_keys_digest); 967 968 static struct flow_dissector flow_keys_dissector_symmetric __read_mostly; 969 970 u32 __skb_get_hash_symmetric(const struct sk_buff *skb) 971 { 972 struct flow_keys keys; 973 974 __flow_hash_secret_init(); 975 976 memset(&keys, 0, sizeof(keys)); 977 __skb_flow_dissect(skb, &flow_keys_dissector_symmetric, &keys, 978 NULL, 0, 0, 0, 979 FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL); 980 981 return __flow_hash_from_keys(&keys, hashrnd); 982 } 983 EXPORT_SYMBOL_GPL(__skb_get_hash_symmetric); 984 985 /** 986 * __skb_get_hash: calculate a flow hash 987 * @skb: sk_buff to calculate flow hash from 988 * 989 * This function calculates a flow hash based on src/dst addresses 990 * and src/dst port numbers. Sets hash in skb to non-zero hash value 991 * on success, zero indicates no valid hash. Also, sets l4_hash in skb 992 * if hash is a canonical 4-tuple hash over transport ports. 993 */ 994 void __skb_get_hash(struct sk_buff *skb) 995 { 996 struct flow_keys keys; 997 u32 hash; 998 999 __flow_hash_secret_init(); 1000 1001 hash = ___skb_get_hash(skb, &keys, hashrnd); 1002 1003 __skb_set_sw_hash(skb, hash, flow_keys_have_l4(&keys)); 1004 } 1005 EXPORT_SYMBOL(__skb_get_hash); 1006 1007 __u32 skb_get_hash_perturb(const struct sk_buff *skb, u32 perturb) 1008 { 1009 struct flow_keys keys; 1010 1011 return ___skb_get_hash(skb, &keys, perturb); 1012 } 1013 EXPORT_SYMBOL(skb_get_hash_perturb); 1014 1015 u32 __skb_get_poff(const struct sk_buff *skb, void *data, 1016 const struct flow_keys *keys, int hlen) 1017 { 1018 u32 poff = keys->control.thoff; 1019 1020 /* skip L4 headers for fragments after the first */ 1021 if ((keys->control.flags & FLOW_DIS_IS_FRAGMENT) && 1022 !(keys->control.flags & FLOW_DIS_FIRST_FRAG)) 1023 return poff; 1024 1025 switch (keys->basic.ip_proto) { 1026 case IPPROTO_TCP: { 1027 /* access doff as u8 to avoid unaligned access */ 1028 const u8 *doff; 1029 u8 _doff; 1030 1031 doff = __skb_header_pointer(skb, poff + 12, sizeof(_doff), 1032 data, hlen, &_doff); 1033 if (!doff) 1034 return poff; 1035 1036 poff += max_t(u32, sizeof(struct tcphdr), (*doff & 0xF0) >> 2); 1037 break; 1038 } 1039 case IPPROTO_UDP: 1040 case IPPROTO_UDPLITE: 1041 poff += sizeof(struct udphdr); 1042 break; 1043 /* For the rest, we do not really care about header 1044 * extensions at this point for now. 1045 */ 1046 case IPPROTO_ICMP: 1047 poff += sizeof(struct icmphdr); 1048 break; 1049 case IPPROTO_ICMPV6: 1050 poff += sizeof(struct icmp6hdr); 1051 break; 1052 case IPPROTO_IGMP: 1053 poff += sizeof(struct igmphdr); 1054 break; 1055 case IPPROTO_DCCP: 1056 poff += sizeof(struct dccp_hdr); 1057 break; 1058 case IPPROTO_SCTP: 1059 poff += sizeof(struct sctphdr); 1060 break; 1061 } 1062 1063 return poff; 1064 } 1065 1066 /** 1067 * skb_get_poff - get the offset to the payload 1068 * @skb: sk_buff to get the payload offset from 1069 * 1070 * The function will get the offset to the payload as far as it could 1071 * be dissected. The main user is currently BPF, so that we can dynamically 1072 * truncate packets without needing to push actual payload to the user 1073 * space and can analyze headers only, instead. 1074 */ 1075 u32 skb_get_poff(const struct sk_buff *skb) 1076 { 1077 struct flow_keys keys; 1078 1079 if (!skb_flow_dissect_flow_keys(skb, &keys, 0)) 1080 return 0; 1081 1082 return __skb_get_poff(skb, skb->data, &keys, skb_headlen(skb)); 1083 } 1084 1085 __u32 __get_hash_from_flowi6(const struct flowi6 *fl6, struct flow_keys *keys) 1086 { 1087 memset(keys, 0, sizeof(*keys)); 1088 1089 memcpy(&keys->addrs.v6addrs.src, &fl6->saddr, 1090 sizeof(keys->addrs.v6addrs.src)); 1091 memcpy(&keys->addrs.v6addrs.dst, &fl6->daddr, 1092 sizeof(keys->addrs.v6addrs.dst)); 1093 keys->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; 1094 keys->ports.src = fl6->fl6_sport; 1095 keys->ports.dst = fl6->fl6_dport; 1096 keys->keyid.keyid = fl6->fl6_gre_key; 1097 keys->tags.flow_label = (__force u32)fl6->flowlabel; 1098 keys->basic.ip_proto = fl6->flowi6_proto; 1099 1100 return flow_hash_from_keys(keys); 1101 } 1102 EXPORT_SYMBOL(__get_hash_from_flowi6); 1103 1104 __u32 __get_hash_from_flowi4(const struct flowi4 *fl4, struct flow_keys *keys) 1105 { 1106 memset(keys, 0, sizeof(*keys)); 1107 1108 keys->addrs.v4addrs.src = fl4->saddr; 1109 keys->addrs.v4addrs.dst = fl4->daddr; 1110 keys->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; 1111 keys->ports.src = fl4->fl4_sport; 1112 keys->ports.dst = fl4->fl4_dport; 1113 keys->keyid.keyid = fl4->fl4_gre_key; 1114 keys->basic.ip_proto = fl4->flowi4_proto; 1115 1116 return flow_hash_from_keys(keys); 1117 } 1118 EXPORT_SYMBOL(__get_hash_from_flowi4); 1119 1120 static const struct flow_dissector_key flow_keys_dissector_keys[] = { 1121 { 1122 .key_id = FLOW_DISSECTOR_KEY_CONTROL, 1123 .offset = offsetof(struct flow_keys, control), 1124 }, 1125 { 1126 .key_id = FLOW_DISSECTOR_KEY_BASIC, 1127 .offset = offsetof(struct flow_keys, basic), 1128 }, 1129 { 1130 .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS, 1131 .offset = offsetof(struct flow_keys, addrs.v4addrs), 1132 }, 1133 { 1134 .key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS, 1135 .offset = offsetof(struct flow_keys, addrs.v6addrs), 1136 }, 1137 { 1138 .key_id = FLOW_DISSECTOR_KEY_TIPC_ADDRS, 1139 .offset = offsetof(struct flow_keys, addrs.tipcaddrs), 1140 }, 1141 { 1142 .key_id = FLOW_DISSECTOR_KEY_PORTS, 1143 .offset = offsetof(struct flow_keys, ports), 1144 }, 1145 { 1146 .key_id = FLOW_DISSECTOR_KEY_VLAN, 1147 .offset = offsetof(struct flow_keys, vlan), 1148 }, 1149 { 1150 .key_id = FLOW_DISSECTOR_KEY_FLOW_LABEL, 1151 .offset = offsetof(struct flow_keys, tags), 1152 }, 1153 { 1154 .key_id = FLOW_DISSECTOR_KEY_GRE_KEYID, 1155 .offset = offsetof(struct flow_keys, keyid), 1156 }, 1157 }; 1158 1159 static const struct flow_dissector_key flow_keys_dissector_symmetric_keys[] = { 1160 { 1161 .key_id = FLOW_DISSECTOR_KEY_CONTROL, 1162 .offset = offsetof(struct flow_keys, control), 1163 }, 1164 { 1165 .key_id = FLOW_DISSECTOR_KEY_BASIC, 1166 .offset = offsetof(struct flow_keys, basic), 1167 }, 1168 { 1169 .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS, 1170 .offset = offsetof(struct flow_keys, addrs.v4addrs), 1171 }, 1172 { 1173 .key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS, 1174 .offset = offsetof(struct flow_keys, addrs.v6addrs), 1175 }, 1176 { 1177 .key_id = FLOW_DISSECTOR_KEY_PORTS, 1178 .offset = offsetof(struct flow_keys, ports), 1179 }, 1180 }; 1181 1182 static const struct flow_dissector_key flow_keys_buf_dissector_keys[] = { 1183 { 1184 .key_id = FLOW_DISSECTOR_KEY_CONTROL, 1185 .offset = offsetof(struct flow_keys, control), 1186 }, 1187 { 1188 .key_id = FLOW_DISSECTOR_KEY_BASIC, 1189 .offset = offsetof(struct flow_keys, basic), 1190 }, 1191 }; 1192 1193 struct flow_dissector flow_keys_dissector __read_mostly; 1194 EXPORT_SYMBOL(flow_keys_dissector); 1195 1196 struct flow_dissector flow_keys_buf_dissector __read_mostly; 1197 1198 static int __init init_default_flow_dissectors(void) 1199 { 1200 skb_flow_dissector_init(&flow_keys_dissector, 1201 flow_keys_dissector_keys, 1202 ARRAY_SIZE(flow_keys_dissector_keys)); 1203 skb_flow_dissector_init(&flow_keys_dissector_symmetric, 1204 flow_keys_dissector_symmetric_keys, 1205 ARRAY_SIZE(flow_keys_dissector_symmetric_keys)); 1206 skb_flow_dissector_init(&flow_keys_buf_dissector, 1207 flow_keys_buf_dissector_keys, 1208 ARRAY_SIZE(flow_keys_buf_dissector_keys)); 1209 return 0; 1210 } 1211 1212 core_initcall(init_default_flow_dissectors); 1213