1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2007-2014 Nicira, Inc. 4 */ 5 6 #include <linux/uaccess.h> 7 #include <linux/netdevice.h> 8 #include <linux/etherdevice.h> 9 #include <linux/if_ether.h> 10 #include <linux/if_vlan.h> 11 #include <linux/kernel.h> 12 #include <linux/jhash.h> 13 #include <linux/jiffies.h> 14 #include <linux/llc.h> 15 #include <linux/module.h> 16 #include <linux/in.h> 17 #include <linux/rcupdate.h> 18 #include <linux/cpumask.h> 19 #include <linux/if_arp.h> 20 #include <linux/ip.h> 21 #include <linux/ipv6.h> 22 #include <linux/mpls.h> 23 #include <linux/sctp.h> 24 #include <linux/smp.h> 25 #include <linux/tcp.h> 26 #include <linux/udp.h> 27 #include <linux/icmp.h> 28 #include <linux/icmpv6.h> 29 #include <linux/rculist.h> 30 #include <net/ip.h> 31 #include <net/ip_tunnels.h> 32 #include <net/ipv6.h> 33 #include <net/mpls.h> 34 #include <net/ndisc.h> 35 #include <net/nsh.h> 36 #include <net/pkt_cls.h> 37 #include <net/netfilter/nf_conntrack_zones.h> 38 39 #include "conntrack.h" 40 #include "datapath.h" 41 #include "flow.h" 42 #include "flow_netlink.h" 43 #include "vport.h" 44 45 u64 ovs_flow_used_time(unsigned long flow_jiffies) 46 { 47 struct timespec64 cur_ts; 48 u64 cur_ms, idle_ms; 49 50 ktime_get_ts64(&cur_ts); 51 idle_ms = jiffies_to_msecs(jiffies - flow_jiffies); 52 cur_ms = (u64)(u32)cur_ts.tv_sec * MSEC_PER_SEC + 53 cur_ts.tv_nsec / NSEC_PER_MSEC; 54 55 return cur_ms - idle_ms; 56 } 57 58 #define TCP_FLAGS_BE16(tp) (*(__be16 *)&tcp_flag_word(tp) & htons(0x0FFF)) 59 60 void ovs_flow_stats_update(struct sw_flow *flow, __be16 tcp_flags, 61 const struct sk_buff *skb) 62 { 63 struct sw_flow_stats *stats; 64 unsigned int cpu = smp_processor_id(); 65 int len = skb->len + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0); 66 67 stats = rcu_dereference(flow->stats[cpu]); 68 69 /* Check if already have CPU-specific stats. */ 70 if (likely(stats)) { 71 spin_lock(&stats->lock); 72 /* Mark if we write on the pre-allocated stats. */ 73 if (cpu == 0 && unlikely(flow->stats_last_writer != cpu)) 74 flow->stats_last_writer = cpu; 75 } else { 76 stats = rcu_dereference(flow->stats[0]); /* Pre-allocated. */ 77 spin_lock(&stats->lock); 78 79 /* If the current CPU is the only writer on the 80 * pre-allocated stats keep using them. 81 */ 82 if (unlikely(flow->stats_last_writer != cpu)) { 83 /* A previous locker may have already allocated the 84 * stats, so we need to check again. If CPU-specific 85 * stats were already allocated, we update the pre- 86 * allocated stats as we have already locked them. 87 */ 88 if (likely(flow->stats_last_writer != -1) && 89 likely(!rcu_access_pointer(flow->stats[cpu]))) { 90 /* Try to allocate CPU-specific stats. */ 91 struct sw_flow_stats *new_stats; 92 93 new_stats = 94 kmem_cache_alloc_node(flow_stats_cache, 95 GFP_NOWAIT | 96 __GFP_THISNODE | 97 __GFP_NOWARN | 98 __GFP_NOMEMALLOC, 99 numa_node_id()); 100 if (likely(new_stats)) { 101 new_stats->used = jiffies; 102 new_stats->packet_count = 1; 103 new_stats->byte_count = len; 104 new_stats->tcp_flags = tcp_flags; 105 spin_lock_init(&new_stats->lock); 106 107 rcu_assign_pointer(flow->stats[cpu], 108 new_stats); 109 cpumask_set_cpu(cpu, 110 flow->cpu_used_mask); 111 goto unlock; 112 } 113 } 114 flow->stats_last_writer = cpu; 115 } 116 } 117 118 stats->used = jiffies; 119 stats->packet_count++; 120 stats->byte_count += len; 121 stats->tcp_flags |= tcp_flags; 122 unlock: 123 spin_unlock(&stats->lock); 124 } 125 126 /* Must be called with rcu_read_lock or ovs_mutex. */ 127 void ovs_flow_stats_get(const struct sw_flow *flow, 128 struct ovs_flow_stats *ovs_stats, 129 unsigned long *used, __be16 *tcp_flags) 130 { 131 unsigned int cpu; 132 133 *used = 0; 134 *tcp_flags = 0; 135 memset(ovs_stats, 0, sizeof(*ovs_stats)); 136 137 for_each_cpu(cpu, flow->cpu_used_mask) { 138 struct sw_flow_stats *stats = rcu_dereference_ovsl(flow->stats[cpu]); 139 140 if (stats) { 141 /* Local CPU may write on non-local stats, so we must 142 * block bottom-halves here. 143 */ 144 spin_lock_bh(&stats->lock); 145 if (!*used || time_after(stats->used, *used)) 146 *used = stats->used; 147 *tcp_flags |= stats->tcp_flags; 148 ovs_stats->n_packets += stats->packet_count; 149 ovs_stats->n_bytes += stats->byte_count; 150 spin_unlock_bh(&stats->lock); 151 } 152 } 153 } 154 155 /* Called with ovs_mutex. */ 156 void ovs_flow_stats_clear(struct sw_flow *flow) 157 { 158 unsigned int cpu; 159 160 for_each_cpu(cpu, flow->cpu_used_mask) { 161 struct sw_flow_stats *stats = ovsl_dereference(flow->stats[cpu]); 162 163 if (stats) { 164 spin_lock_bh(&stats->lock); 165 stats->used = 0; 166 stats->packet_count = 0; 167 stats->byte_count = 0; 168 stats->tcp_flags = 0; 169 spin_unlock_bh(&stats->lock); 170 } 171 } 172 } 173 174 static int check_header(struct sk_buff *skb, int len) 175 { 176 if (unlikely(skb->len < len)) 177 return -EINVAL; 178 if (unlikely(!pskb_may_pull(skb, len))) 179 return -ENOMEM; 180 return 0; 181 } 182 183 static bool arphdr_ok(struct sk_buff *skb) 184 { 185 return pskb_may_pull(skb, skb_network_offset(skb) + 186 sizeof(struct arp_eth_header)); 187 } 188 189 static int check_iphdr(struct sk_buff *skb) 190 { 191 unsigned int nh_ofs = skb_network_offset(skb); 192 unsigned int ip_len; 193 int err; 194 195 err = check_header(skb, nh_ofs + sizeof(struct iphdr)); 196 if (unlikely(err)) 197 return err; 198 199 ip_len = ip_hdrlen(skb); 200 if (unlikely(ip_len < sizeof(struct iphdr) || 201 skb->len < nh_ofs + ip_len)) 202 return -EINVAL; 203 204 skb_set_transport_header(skb, nh_ofs + ip_len); 205 return 0; 206 } 207 208 static bool tcphdr_ok(struct sk_buff *skb) 209 { 210 int th_ofs = skb_transport_offset(skb); 211 int tcp_len; 212 213 if (unlikely(!pskb_may_pull(skb, th_ofs + sizeof(struct tcphdr)))) 214 return false; 215 216 tcp_len = tcp_hdrlen(skb); 217 if (unlikely(tcp_len < sizeof(struct tcphdr) || 218 skb->len < th_ofs + tcp_len)) 219 return false; 220 221 return true; 222 } 223 224 static bool udphdr_ok(struct sk_buff *skb) 225 { 226 return pskb_may_pull(skb, skb_transport_offset(skb) + 227 sizeof(struct udphdr)); 228 } 229 230 static bool sctphdr_ok(struct sk_buff *skb) 231 { 232 return pskb_may_pull(skb, skb_transport_offset(skb) + 233 sizeof(struct sctphdr)); 234 } 235 236 static bool icmphdr_ok(struct sk_buff *skb) 237 { 238 return pskb_may_pull(skb, skb_transport_offset(skb) + 239 sizeof(struct icmphdr)); 240 } 241 242 /** 243 * get_ipv6_ext_hdrs() - Parses packet and sets IPv6 extension header flags. 244 * 245 * @skb: buffer where extension header data starts in packet 246 * @nh: ipv6 header 247 * @ext_hdrs: flags are stored here 248 * 249 * OFPIEH12_UNREP is set if more than one of a given IPv6 extension header 250 * is unexpectedly encountered. (Two destination options headers may be 251 * expected and would not cause this bit to be set.) 252 * 253 * OFPIEH12_UNSEQ is set if IPv6 extension headers were not in the order 254 * preferred (but not required) by RFC 2460: 255 * 256 * When more than one extension header is used in the same packet, it is 257 * recommended that those headers appear in the following order: 258 * IPv6 header 259 * Hop-by-Hop Options header 260 * Destination Options header 261 * Routing header 262 * Fragment header 263 * Authentication header 264 * Encapsulating Security Payload header 265 * Destination Options header 266 * upper-layer header 267 */ 268 static void get_ipv6_ext_hdrs(struct sk_buff *skb, struct ipv6hdr *nh, 269 u16 *ext_hdrs) 270 { 271 u8 next_type = nh->nexthdr; 272 unsigned int start = skb_network_offset(skb) + sizeof(struct ipv6hdr); 273 int dest_options_header_count = 0; 274 275 *ext_hdrs = 0; 276 277 while (ipv6_ext_hdr(next_type)) { 278 struct ipv6_opt_hdr _hdr, *hp; 279 280 switch (next_type) { 281 case IPPROTO_NONE: 282 *ext_hdrs |= OFPIEH12_NONEXT; 283 /* stop parsing */ 284 return; 285 286 case IPPROTO_ESP: 287 if (*ext_hdrs & OFPIEH12_ESP) 288 *ext_hdrs |= OFPIEH12_UNREP; 289 if ((*ext_hdrs & ~(OFPIEH12_HOP | OFPIEH12_DEST | 290 OFPIEH12_ROUTER | OFPIEH12_FRAG | 291 OFPIEH12_AUTH | OFPIEH12_UNREP)) || 292 dest_options_header_count >= 2) { 293 *ext_hdrs |= OFPIEH12_UNSEQ; 294 } 295 *ext_hdrs |= OFPIEH12_ESP; 296 break; 297 298 case IPPROTO_AH: 299 if (*ext_hdrs & OFPIEH12_AUTH) 300 *ext_hdrs |= OFPIEH12_UNREP; 301 if ((*ext_hdrs & 302 ~(OFPIEH12_HOP | OFPIEH12_DEST | OFPIEH12_ROUTER | 303 OFPIEH12_FRAG | OFPIEH12_UNREP)) || 304 dest_options_header_count >= 2) { 305 *ext_hdrs |= OFPIEH12_UNSEQ; 306 } 307 *ext_hdrs |= OFPIEH12_AUTH; 308 break; 309 310 case IPPROTO_DSTOPTS: 311 if (dest_options_header_count == 0) { 312 if (*ext_hdrs & 313 ~(OFPIEH12_HOP | OFPIEH12_UNREP)) 314 *ext_hdrs |= OFPIEH12_UNSEQ; 315 *ext_hdrs |= OFPIEH12_DEST; 316 } else if (dest_options_header_count == 1) { 317 if (*ext_hdrs & 318 ~(OFPIEH12_HOP | OFPIEH12_DEST | 319 OFPIEH12_ROUTER | OFPIEH12_FRAG | 320 OFPIEH12_AUTH | OFPIEH12_ESP | 321 OFPIEH12_UNREP)) { 322 *ext_hdrs |= OFPIEH12_UNSEQ; 323 } 324 } else { 325 *ext_hdrs |= OFPIEH12_UNREP; 326 } 327 dest_options_header_count++; 328 break; 329 330 case IPPROTO_FRAGMENT: 331 if (*ext_hdrs & OFPIEH12_FRAG) 332 *ext_hdrs |= OFPIEH12_UNREP; 333 if ((*ext_hdrs & ~(OFPIEH12_HOP | 334 OFPIEH12_DEST | 335 OFPIEH12_ROUTER | 336 OFPIEH12_UNREP)) || 337 dest_options_header_count >= 2) { 338 *ext_hdrs |= OFPIEH12_UNSEQ; 339 } 340 *ext_hdrs |= OFPIEH12_FRAG; 341 break; 342 343 case IPPROTO_ROUTING: 344 if (*ext_hdrs & OFPIEH12_ROUTER) 345 *ext_hdrs |= OFPIEH12_UNREP; 346 if ((*ext_hdrs & ~(OFPIEH12_HOP | 347 OFPIEH12_DEST | 348 OFPIEH12_UNREP)) || 349 dest_options_header_count >= 2) { 350 *ext_hdrs |= OFPIEH12_UNSEQ; 351 } 352 *ext_hdrs |= OFPIEH12_ROUTER; 353 break; 354 355 case IPPROTO_HOPOPTS: 356 if (*ext_hdrs & OFPIEH12_HOP) 357 *ext_hdrs |= OFPIEH12_UNREP; 358 /* OFPIEH12_HOP is set to 1 if a hop-by-hop IPv6 359 * extension header is present as the first 360 * extension header in the packet. 361 */ 362 if (*ext_hdrs == 0) 363 *ext_hdrs |= OFPIEH12_HOP; 364 else 365 *ext_hdrs |= OFPIEH12_UNSEQ; 366 break; 367 368 default: 369 return; 370 } 371 372 if (next_type == IPPROTO_FRAGMENT) { 373 struct frag_hdr _fh, *fh; 374 375 fh = skb_header_pointer(skb, start, sizeof(_fh), &_fh); 376 if (!fh) 377 break; 378 /* Only the first fragment has the headers that follow. */ 379 if (fh->frag_off & htons(IP6_OFFSET)) 380 break; 381 start += sizeof(_fh); 382 next_type = fh->nexthdr; 383 continue; 384 } 385 386 hp = skb_header_pointer(skb, start, sizeof(_hdr), &_hdr); 387 if (!hp) 388 break; 389 if (next_type == IPPROTO_AH) 390 start += ipv6_authlen(hp); 391 else 392 start += ipv6_optlen(hp); 393 next_type = hp->nexthdr; 394 } 395 } 396 397 static int parse_ipv6hdr(struct sk_buff *skb, struct sw_flow_key *key) 398 { 399 unsigned short frag_off; 400 unsigned int payload_ofs = 0; 401 unsigned int nh_ofs = skb_network_offset(skb); 402 unsigned int nh_len; 403 struct ipv6hdr *nh; 404 int err, nexthdr, flags = 0; 405 406 err = check_header(skb, nh_ofs + sizeof(*nh)); 407 if (unlikely(err)) 408 return err; 409 410 nh = ipv6_hdr(skb); 411 412 get_ipv6_ext_hdrs(skb, nh, &key->ipv6.exthdrs); 413 414 key->ip.proto = NEXTHDR_NONE; 415 key->ip.tos = ipv6_get_dsfield(nh); 416 key->ip.ttl = nh->hop_limit; 417 key->ipv6.label = *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL); 418 key->ipv6.addr.src = nh->saddr; 419 key->ipv6.addr.dst = nh->daddr; 420 421 nexthdr = ipv6_find_hdr(skb, &payload_ofs, -1, &frag_off, &flags); 422 if (flags & IP6_FH_F_FRAG) { 423 if (frag_off) { 424 key->ip.frag = OVS_FRAG_TYPE_LATER; 425 key->ip.proto = NEXTHDR_FRAGMENT; 426 return 0; 427 } 428 key->ip.frag = OVS_FRAG_TYPE_FIRST; 429 } else { 430 key->ip.frag = OVS_FRAG_TYPE_NONE; 431 } 432 433 /* Delayed handling of error in ipv6_find_hdr() as it 434 * always sets flags and frag_off to a valid value which may be 435 * used to set key->ip.frag above. 436 */ 437 if (unlikely(nexthdr < 0)) 438 return -EPROTO; 439 440 nh_len = payload_ofs - nh_ofs; 441 if (unlikely(!skb_set_transport_header_careful(skb, payload_ofs))) 442 return -EPROTO; 443 key->ip.proto = nexthdr; 444 return nh_len; 445 } 446 447 static bool icmp6hdr_ok(struct sk_buff *skb) 448 { 449 return pskb_may_pull(skb, skb_transport_offset(skb) + 450 sizeof(struct icmp6hdr)); 451 } 452 453 /** 454 * parse_vlan_tag - Parse vlan tag from vlan header. 455 * @skb: skb containing frame to parse 456 * @key_vh: pointer to parsed vlan tag 457 * @untag_vlan: should the vlan header be removed from the frame 458 * 459 * Return: ERROR on memory error. 460 * %0 if it encounters a non-vlan or incomplete packet. 461 * %1 after successfully parsing vlan tag. 462 */ 463 static int parse_vlan_tag(struct sk_buff *skb, struct vlan_head *key_vh, 464 bool untag_vlan) 465 { 466 struct vlan_head *vh = (struct vlan_head *)skb->data; 467 468 if (likely(!eth_type_vlan(vh->tpid))) 469 return 0; 470 471 if (unlikely(skb->len < sizeof(struct vlan_head) + sizeof(__be16))) 472 return 0; 473 474 if (unlikely(!pskb_may_pull(skb, sizeof(struct vlan_head) + 475 sizeof(__be16)))) 476 return -ENOMEM; 477 478 vh = (struct vlan_head *)skb->data; 479 key_vh->tci = vh->tci | htons(VLAN_CFI_MASK); 480 key_vh->tpid = vh->tpid; 481 482 if (unlikely(untag_vlan)) { 483 int offset = skb->data - skb_mac_header(skb); 484 u16 tci; 485 int err; 486 487 __skb_push(skb, offset); 488 err = __skb_vlan_pop(skb, &tci); 489 __skb_pull(skb, offset); 490 if (err) 491 return err; 492 __vlan_hwaccel_put_tag(skb, key_vh->tpid, tci); 493 } else { 494 __skb_pull(skb, sizeof(struct vlan_head)); 495 } 496 return 1; 497 } 498 499 static void clear_vlan(struct sw_flow_key *key) 500 { 501 key->eth.vlan.tci = 0; 502 key->eth.vlan.tpid = 0; 503 key->eth.cvlan.tci = 0; 504 key->eth.cvlan.tpid = 0; 505 } 506 507 static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key) 508 { 509 int res; 510 511 if (skb_vlan_tag_present(skb)) { 512 key->eth.vlan.tci = htons(skb->vlan_tci) | htons(VLAN_CFI_MASK); 513 key->eth.vlan.tpid = skb->vlan_proto; 514 } else { 515 /* Parse outer vlan tag in the non-accelerated case. */ 516 res = parse_vlan_tag(skb, &key->eth.vlan, true); 517 if (res <= 0) 518 return res; 519 } 520 521 /* Parse inner vlan tag. */ 522 res = parse_vlan_tag(skb, &key->eth.cvlan, false); 523 if (res <= 0) 524 return res; 525 526 return 0; 527 } 528 529 static __be16 parse_ethertype(struct sk_buff *skb) 530 { 531 struct llc_snap_hdr { 532 u8 dsap; /* Always 0xAA */ 533 u8 ssap; /* Always 0xAA */ 534 u8 ctrl; 535 u8 oui[3]; 536 __be16 ethertype; 537 }; 538 struct llc_snap_hdr *llc; 539 __be16 proto; 540 541 proto = *(__be16 *) skb->data; 542 __skb_pull(skb, sizeof(__be16)); 543 544 if (eth_proto_is_802_3(proto)) 545 return proto; 546 547 if (skb->len < sizeof(struct llc_snap_hdr)) 548 return htons(ETH_P_802_2); 549 550 if (unlikely(!pskb_may_pull(skb, sizeof(struct llc_snap_hdr)))) 551 return htons(0); 552 553 llc = (struct llc_snap_hdr *) skb->data; 554 if (llc->dsap != LLC_SAP_SNAP || 555 llc->ssap != LLC_SAP_SNAP || 556 (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0) 557 return htons(ETH_P_802_2); 558 559 __skb_pull(skb, sizeof(struct llc_snap_hdr)); 560 561 if (eth_proto_is_802_3(llc->ethertype)) 562 return llc->ethertype; 563 564 return htons(ETH_P_802_2); 565 } 566 567 static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key, 568 int nh_len) 569 { 570 struct icmp6hdr *icmp = icmp6_hdr(skb); 571 572 /* The ICMPv6 type and code fields use the 16-bit transport port 573 * fields, so we need to store them in 16-bit network byte order. 574 */ 575 key->tp.src = htons(icmp->icmp6_type); 576 key->tp.dst = htons(icmp->icmp6_code); 577 578 if (icmp->icmp6_code == 0 && 579 (icmp->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION || 580 icmp->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT)) { 581 int icmp_len = skb->len - skb_transport_offset(skb); 582 struct nd_msg *nd; 583 int offset; 584 585 memset(&key->ipv6.nd, 0, sizeof(key->ipv6.nd)); 586 587 /* In order to process neighbor discovery options, we need the 588 * entire packet. 589 */ 590 if (unlikely(icmp_len < sizeof(*nd))) 591 return 0; 592 593 if (unlikely(skb_linearize(skb))) 594 return -ENOMEM; 595 596 nd = (struct nd_msg *)skb_transport_header(skb); 597 key->ipv6.nd.target = nd->target; 598 599 icmp_len -= sizeof(*nd); 600 offset = 0; 601 while (icmp_len >= 8) { 602 struct nd_opt_hdr *nd_opt = 603 (struct nd_opt_hdr *)(nd->opt + offset); 604 int opt_len = nd_opt->nd_opt_len * 8; 605 606 if (unlikely(!opt_len || opt_len > icmp_len)) 607 return 0; 608 609 /* Store the link layer address if the appropriate 610 * option is provided. It is considered an error if 611 * the same link layer option is specified twice. 612 */ 613 if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LL_ADDR 614 && opt_len == 8) { 615 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.sll))) 616 goto invalid; 617 ether_addr_copy(key->ipv6.nd.sll, 618 &nd->opt[offset+sizeof(*nd_opt)]); 619 } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LL_ADDR 620 && opt_len == 8) { 621 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.tll))) 622 goto invalid; 623 ether_addr_copy(key->ipv6.nd.tll, 624 &nd->opt[offset+sizeof(*nd_opt)]); 625 } 626 627 icmp_len -= opt_len; 628 offset += opt_len; 629 } 630 } 631 632 return 0; 633 634 invalid: 635 memset(&key->ipv6.nd.target, 0, sizeof(key->ipv6.nd.target)); 636 memset(key->ipv6.nd.sll, 0, sizeof(key->ipv6.nd.sll)); 637 memset(key->ipv6.nd.tll, 0, sizeof(key->ipv6.nd.tll)); 638 639 return 0; 640 } 641 642 static int parse_nsh(struct sk_buff *skb, struct sw_flow_key *key) 643 { 644 struct nshhdr *nh; 645 unsigned int nh_ofs = skb_network_offset(skb); 646 u8 version, length; 647 int err; 648 649 err = check_header(skb, nh_ofs + NSH_BASE_HDR_LEN); 650 if (unlikely(err)) 651 return err; 652 653 nh = nsh_hdr(skb); 654 version = nsh_get_ver(nh); 655 length = nsh_hdr_len(nh); 656 657 if (version != 0) 658 return -EINVAL; 659 660 err = check_header(skb, nh_ofs + length); 661 if (unlikely(err)) 662 return err; 663 664 nh = nsh_hdr(skb); 665 key->nsh.base.flags = nsh_get_flags(nh); 666 key->nsh.base.ttl = nsh_get_ttl(nh); 667 key->nsh.base.mdtype = nh->mdtype; 668 key->nsh.base.np = nh->np; 669 key->nsh.base.path_hdr = nh->path_hdr; 670 switch (key->nsh.base.mdtype) { 671 case NSH_M_TYPE1: 672 if (length != NSH_M_TYPE1_LEN) 673 return -EINVAL; 674 memcpy(key->nsh.context, nh->md1.context, 675 sizeof(nh->md1)); 676 break; 677 case NSH_M_TYPE2: 678 memset(key->nsh.context, 0, 679 sizeof(nh->md1)); 680 break; 681 default: 682 return -EINVAL; 683 } 684 685 return 0; 686 } 687 688 /** 689 * key_extract_l3l4 - extracts L3/L4 header information. 690 * @skb: sk_buff that contains the frame, with skb->data pointing to the 691 * L3 header 692 * @key: output flow key 693 * 694 * Return: %0 if successful, otherwise a negative errno value. 695 */ 696 static int key_extract_l3l4(struct sk_buff *skb, struct sw_flow_key *key) 697 { 698 int error; 699 700 /* Network layer. */ 701 if (key->eth.type == htons(ETH_P_IP)) { 702 struct iphdr *nh; 703 __be16 offset; 704 705 error = check_iphdr(skb); 706 if (unlikely(error)) { 707 memset(&key->ip, 0, sizeof(key->ip)); 708 memset(&key->ipv4, 0, sizeof(key->ipv4)); 709 if (error == -EINVAL) { 710 skb->transport_header = skb->network_header; 711 error = 0; 712 } 713 return error; 714 } 715 716 nh = ip_hdr(skb); 717 key->ipv4.addr.src = nh->saddr; 718 key->ipv4.addr.dst = nh->daddr; 719 720 key->ip.proto = nh->protocol; 721 key->ip.tos = nh->tos; 722 key->ip.ttl = nh->ttl; 723 724 offset = nh->frag_off & htons(IP_OFFSET); 725 if (offset) { 726 key->ip.frag = OVS_FRAG_TYPE_LATER; 727 memset(&key->tp, 0, sizeof(key->tp)); 728 return 0; 729 } 730 if (nh->frag_off & htons(IP_MF) || 731 skb_shinfo(skb)->gso_type & SKB_GSO_UDP) 732 key->ip.frag = OVS_FRAG_TYPE_FIRST; 733 else 734 key->ip.frag = OVS_FRAG_TYPE_NONE; 735 736 /* Transport layer. */ 737 if (key->ip.proto == IPPROTO_TCP) { 738 if (tcphdr_ok(skb)) { 739 struct tcphdr *tcp = tcp_hdr(skb); 740 key->tp.src = tcp->source; 741 key->tp.dst = tcp->dest; 742 key->tp.flags = TCP_FLAGS_BE16(tcp); 743 } else { 744 memset(&key->tp, 0, sizeof(key->tp)); 745 } 746 747 } else if (key->ip.proto == IPPROTO_UDP) { 748 if (udphdr_ok(skb)) { 749 struct udphdr *udp = udp_hdr(skb); 750 key->tp.src = udp->source; 751 key->tp.dst = udp->dest; 752 } else { 753 memset(&key->tp, 0, sizeof(key->tp)); 754 } 755 } else if (key->ip.proto == IPPROTO_SCTP) { 756 if (sctphdr_ok(skb)) { 757 struct sctphdr *sctp = sctp_hdr(skb); 758 key->tp.src = sctp->source; 759 key->tp.dst = sctp->dest; 760 } else { 761 memset(&key->tp, 0, sizeof(key->tp)); 762 } 763 } else if (key->ip.proto == IPPROTO_ICMP) { 764 if (icmphdr_ok(skb)) { 765 struct icmphdr *icmp = icmp_hdr(skb); 766 /* The ICMP type and code fields use the 16-bit 767 * transport port fields, so we need to store 768 * them in 16-bit network byte order. */ 769 key->tp.src = htons(icmp->type); 770 key->tp.dst = htons(icmp->code); 771 } else { 772 memset(&key->tp, 0, sizeof(key->tp)); 773 } 774 } 775 776 } else if (key->eth.type == htons(ETH_P_ARP) || 777 key->eth.type == htons(ETH_P_RARP)) { 778 struct arp_eth_header *arp; 779 bool arp_available = arphdr_ok(skb); 780 781 arp = (struct arp_eth_header *)skb_network_header(skb); 782 783 if (arp_available && 784 arp->ar_hrd == htons(ARPHRD_ETHER) && 785 arp->ar_pro == htons(ETH_P_IP) && 786 arp->ar_hln == ETH_ALEN && 787 arp->ar_pln == 4) { 788 789 /* We only match on the lower 8 bits of the opcode. */ 790 if (ntohs(arp->ar_op) <= 0xff) 791 key->ip.proto = ntohs(arp->ar_op); 792 else 793 key->ip.proto = 0; 794 795 memcpy(&key->ipv4.addr.src, arp->ar_sip, sizeof(key->ipv4.addr.src)); 796 memcpy(&key->ipv4.addr.dst, arp->ar_tip, sizeof(key->ipv4.addr.dst)); 797 ether_addr_copy(key->ipv4.arp.sha, arp->ar_sha); 798 ether_addr_copy(key->ipv4.arp.tha, arp->ar_tha); 799 } else { 800 memset(&key->ip, 0, sizeof(key->ip)); 801 memset(&key->ipv4, 0, sizeof(key->ipv4)); 802 } 803 } else if (eth_p_mpls(key->eth.type)) { 804 size_t label_count = 1; 805 806 memset(&key->mpls, 0, sizeof(key->mpls)); 807 skb_set_inner_network_header(skb, skb->mac_len); 808 while (1) { 809 __be32 lse; 810 811 error = check_header(skb, skb->mac_len + 812 label_count * MPLS_HLEN); 813 if (unlikely(error)) 814 return 0; 815 816 memcpy(&lse, skb_inner_network_header(skb), MPLS_HLEN); 817 818 if (label_count <= MPLS_LABEL_DEPTH) 819 memcpy(&key->mpls.lse[label_count - 1], &lse, 820 MPLS_HLEN); 821 822 skb_set_inner_network_header(skb, skb->mac_len + 823 label_count * MPLS_HLEN); 824 if (lse & htonl(MPLS_LS_S_MASK)) 825 break; 826 827 label_count++; 828 } 829 if (label_count > MPLS_LABEL_DEPTH) 830 label_count = MPLS_LABEL_DEPTH; 831 832 key->mpls.num_labels_mask = GENMASK(label_count - 1, 0); 833 } else if (key->eth.type == htons(ETH_P_IPV6)) { 834 int nh_len; /* IPv6 Header + Extensions */ 835 836 nh_len = parse_ipv6hdr(skb, key); 837 if (unlikely(nh_len < 0)) { 838 switch (nh_len) { 839 case -EINVAL: 840 memset(&key->ip, 0, sizeof(key->ip)); 841 memset(&key->ipv6.addr, 0, sizeof(key->ipv6.addr)); 842 fallthrough; 843 case -EPROTO: 844 skb->transport_header = skb->network_header; 845 error = 0; 846 break; 847 default: 848 error = nh_len; 849 } 850 return error; 851 } 852 853 if (key->ip.frag == OVS_FRAG_TYPE_LATER) { 854 memset(&key->tp, 0, sizeof(key->tp)); 855 return 0; 856 } 857 if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP) 858 key->ip.frag = OVS_FRAG_TYPE_FIRST; 859 860 /* Transport layer. */ 861 if (key->ip.proto == NEXTHDR_TCP) { 862 if (tcphdr_ok(skb)) { 863 struct tcphdr *tcp = tcp_hdr(skb); 864 key->tp.src = tcp->source; 865 key->tp.dst = tcp->dest; 866 key->tp.flags = TCP_FLAGS_BE16(tcp); 867 } else { 868 memset(&key->tp, 0, sizeof(key->tp)); 869 } 870 } else if (key->ip.proto == NEXTHDR_UDP) { 871 if (udphdr_ok(skb)) { 872 struct udphdr *udp = udp_hdr(skb); 873 key->tp.src = udp->source; 874 key->tp.dst = udp->dest; 875 } else { 876 memset(&key->tp, 0, sizeof(key->tp)); 877 } 878 } else if (key->ip.proto == NEXTHDR_SCTP) { 879 if (sctphdr_ok(skb)) { 880 struct sctphdr *sctp = sctp_hdr(skb); 881 key->tp.src = sctp->source; 882 key->tp.dst = sctp->dest; 883 } else { 884 memset(&key->tp, 0, sizeof(key->tp)); 885 } 886 } else if (key->ip.proto == NEXTHDR_ICMP) { 887 if (icmp6hdr_ok(skb)) { 888 error = parse_icmpv6(skb, key, nh_len); 889 if (error) 890 return error; 891 } else { 892 memset(&key->tp, 0, sizeof(key->tp)); 893 } 894 } 895 } else if (key->eth.type == htons(ETH_P_NSH)) { 896 error = parse_nsh(skb, key); 897 if (error) 898 return error; 899 } 900 return 0; 901 } 902 903 /** 904 * key_extract - extracts a flow key from an Ethernet frame. 905 * @skb: sk_buff that contains the frame, with skb->data pointing to the 906 * Ethernet header 907 * @key: output flow key 908 * 909 * Initializes @skb header fields as follows: 910 * 911 * - skb->mac_header: the L2 header. 912 * 913 * - skb->network_header: just past the L2 header, or just past the 914 * VLAN header, to the first byte of the L2 payload. 915 * 916 * - skb->transport_header: If key->eth.type is ETH_P_IP or ETH_P_IPV6 917 * on output, then just past the IP header, if one is present and 918 * of a correct length, otherwise the same as skb->network_header. 919 * For other key->eth.type values it is left untouched. 920 * 921 * - skb->protocol: the type of the data starting at skb->network_header. 922 * Equals to key->eth.type. 923 * 924 * Return: %0 if successful, otherwise a negative errno value. 925 */ 926 static int key_extract(struct sk_buff *skb, struct sw_flow_key *key) 927 { 928 /* Flags are always used as part of stats */ 929 key->tp.flags = 0; 930 931 skb_reset_mac_header(skb); 932 933 /* Link layer. */ 934 clear_vlan(key); 935 if (ovs_key_mac_proto(key) == MAC_PROTO_NONE) { 936 if (unlikely(eth_type_vlan(skb->protocol))) 937 return -EINVAL; 938 939 skb_reset_network_header(skb); 940 key->eth.type = skb->protocol; 941 } else { 942 struct ethhdr *eth; 943 int err; 944 945 err = check_header(skb, ETH_HLEN); 946 if (unlikely(err)) 947 return err; 948 949 eth = eth_hdr(skb); 950 ether_addr_copy(key->eth.src, eth->h_source); 951 ether_addr_copy(key->eth.dst, eth->h_dest); 952 953 __skb_pull(skb, 2 * ETH_ALEN); 954 /* We are going to push all headers that we pull, so no need to 955 * update skb->csum here. 956 */ 957 958 if (unlikely(parse_vlan(skb, key))) 959 return -ENOMEM; 960 961 key->eth.type = parse_ethertype(skb); 962 if (unlikely(key->eth.type == htons(0))) 963 return -ENOMEM; 964 965 /* Multiple tagged packets need to retain TPID to satisfy 966 * skb_vlan_pop(), which will later shift the ethertype into 967 * skb->protocol. 968 */ 969 if (key->eth.cvlan.tci & htons(VLAN_CFI_MASK)) 970 skb->protocol = key->eth.cvlan.tpid; 971 else 972 skb->protocol = key->eth.type; 973 974 skb_reset_network_header(skb); 975 __skb_push(skb, skb->data - skb_mac_header(skb)); 976 } 977 978 skb_reset_mac_len(skb); 979 980 /* Fill out L3/L4 key info, if any */ 981 return key_extract_l3l4(skb, key); 982 } 983 984 /* In the case of conntrack fragment handling it expects L3 headers, 985 * add a helper. 986 */ 987 int ovs_flow_key_update_l3l4(struct sk_buff *skb, struct sw_flow_key *key) 988 { 989 return key_extract_l3l4(skb, key); 990 } 991 992 int ovs_flow_key_update(struct sk_buff *skb, struct sw_flow_key *key) 993 { 994 int res; 995 996 res = key_extract(skb, key); 997 if (!res) 998 key->mac_proto &= ~SW_FLOW_KEY_INVALID; 999 1000 return res; 1001 } 1002 1003 static int key_extract_mac_proto(struct sk_buff *skb) 1004 { 1005 switch (skb->dev->type) { 1006 case ARPHRD_ETHER: 1007 return MAC_PROTO_ETHERNET; 1008 case ARPHRD_NONE: 1009 if (skb->protocol == htons(ETH_P_TEB)) 1010 return MAC_PROTO_ETHERNET; 1011 return MAC_PROTO_NONE; 1012 } 1013 WARN_ON_ONCE(1); 1014 return -EINVAL; 1015 } 1016 1017 int ovs_flow_key_extract(const struct ip_tunnel_info *tun_info, 1018 struct sk_buff *skb, struct sw_flow_key *key) 1019 { 1020 #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT) 1021 struct tc_skb_ext *tc_ext; 1022 #endif 1023 bool post_ct = false, post_ct_snat = false, post_ct_dnat = false; 1024 int res, err; 1025 u16 zone = 0; 1026 1027 /* Extract metadata from packet. */ 1028 if (tun_info) { 1029 key->tun_proto = ip_tunnel_info_af(tun_info); 1030 memcpy(&key->tun_key, &tun_info->key, sizeof(key->tun_key)); 1031 1032 if (tun_info->options_len) { 1033 BUILD_BUG_ON((1 << (sizeof(tun_info->options_len) * 1034 8)) - 1 1035 > sizeof(key->tun_opts)); 1036 1037 ip_tunnel_info_opts_get(TUN_METADATA_OPTS(key, tun_info->options_len), 1038 tun_info); 1039 key->tun_opts_len = tun_info->options_len; 1040 } else { 1041 key->tun_opts_len = 0; 1042 } 1043 } else { 1044 key->tun_proto = 0; 1045 key->tun_opts_len = 0; 1046 memset(&key->tun_key, 0, sizeof(key->tun_key)); 1047 } 1048 1049 key->phy.priority = skb->priority; 1050 key->phy.in_port = OVS_CB(skb)->input_vport->port_no; 1051 key->phy.skb_mark = skb->mark; 1052 key->ovs_flow_hash = 0; 1053 res = key_extract_mac_proto(skb); 1054 if (res < 0) 1055 return res; 1056 key->mac_proto = res; 1057 1058 #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT) 1059 if (tc_skb_ext_tc_enabled()) { 1060 tc_ext = skb_ext_find(skb, TC_SKB_EXT); 1061 key->recirc_id = tc_ext && !tc_ext->act_miss ? 1062 tc_ext->chain : 0; 1063 OVS_CB(skb)->mru = tc_ext ? tc_ext->mru : 0; 1064 post_ct = tc_ext ? tc_ext->post_ct : false; 1065 post_ct_snat = post_ct ? tc_ext->post_ct_snat : false; 1066 post_ct_dnat = post_ct ? tc_ext->post_ct_dnat : false; 1067 zone = post_ct ? tc_ext->zone : 0; 1068 } else { 1069 key->recirc_id = 0; 1070 } 1071 #else 1072 key->recirc_id = 0; 1073 #endif 1074 1075 err = key_extract(skb, key); 1076 if (!err) { 1077 ovs_ct_fill_key(skb, key, post_ct); /* Must be after key_extract(). */ 1078 if (post_ct) { 1079 if (!skb_get_nfct(skb)) { 1080 key->ct_zone = zone; 1081 } else { 1082 if (!post_ct_dnat) 1083 key->ct_state &= ~OVS_CS_F_DST_NAT; 1084 if (!post_ct_snat) 1085 key->ct_state &= ~OVS_CS_F_SRC_NAT; 1086 } 1087 } 1088 } 1089 return err; 1090 } 1091 1092 int ovs_flow_key_extract_userspace(struct net *net, const struct nlattr *attr, 1093 struct sk_buff *skb, 1094 struct sw_flow_key *key, bool log) 1095 { 1096 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1]; 1097 u64 attrs = 0; 1098 int err; 1099 1100 err = parse_flow_nlattrs(attr, a, &attrs, log); 1101 if (err) 1102 return -EINVAL; 1103 1104 /* Extract metadata from netlink attributes. */ 1105 err = ovs_nla_get_flow_metadata(net, a, attrs, key, log); 1106 if (err) 1107 return err; 1108 1109 /* key_extract assumes that skb->protocol is set-up for 1110 * layer 3 packets which is the case for other callers, 1111 * in particular packets received from the network stack. 1112 * Here the correct value can be set from the metadata 1113 * extracted above. 1114 * For L2 packet key eth type would be zero. skb protocol 1115 * would be set to correct value later during key-extact. 1116 */ 1117 1118 skb->protocol = key->eth.type; 1119 err = key_extract(skb, key); 1120 if (err) 1121 return err; 1122 1123 /* Check that we have conntrack original direction tuple metadata only 1124 * for packets for which it makes sense. Otherwise the key may be 1125 * corrupted due to overlapping key fields. 1126 */ 1127 if (attrs & (1 << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4) && 1128 key->eth.type != htons(ETH_P_IP)) 1129 return -EINVAL; 1130 if (attrs & (1 << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6) && 1131 (key->eth.type != htons(ETH_P_IPV6) || 1132 sw_flow_key_is_nd(key))) 1133 return -EINVAL; 1134 1135 return 0; 1136 } 1137