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