1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2007-2017 Nicira, Inc. 4 */ 5 6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 7 8 #include <linux/skbuff.h> 9 #include <linux/in.h> 10 #include <linux/ip.h> 11 #include <linux/openvswitch.h> 12 #include <linux/sctp.h> 13 #include <linux/tcp.h> 14 #include <linux/udp.h> 15 #include <linux/in6.h> 16 #include <linux/if_arp.h> 17 #include <linux/if_vlan.h> 18 19 #include <net/dst.h> 20 #include <net/gso.h> 21 #include <net/ip.h> 22 #include <net/ipv6.h> 23 #include <net/ip6_fib.h> 24 #include <net/checksum.h> 25 #include <net/dsfield.h> 26 #include <net/mpls.h> 27 28 #if IS_ENABLED(CONFIG_PSAMPLE) 29 #include <net/psample.h> 30 #endif 31 32 #include <net/sctp/checksum.h> 33 34 #include "datapath.h" 35 #include "drop.h" 36 #include "flow.h" 37 #include "conntrack.h" 38 #include "vport.h" 39 #include "flow_netlink.h" 40 #include "openvswitch_trace.h" 41 42 struct ovs_pcpu_storage __percpu *ovs_pcpu_storage; 43 44 /* Make a clone of the 'key', using the pre-allocated percpu 'flow_keys' 45 * space. Return NULL if out of key spaces. 46 */ 47 static struct sw_flow_key *clone_key(const struct sw_flow_key *key_) 48 { 49 struct ovs_pcpu_storage *ovs_pcpu = this_cpu_ptr(ovs_pcpu_storage); 50 struct action_flow_keys *keys = &ovs_pcpu->flow_keys; 51 int level = ovs_pcpu->exec_level; 52 struct sw_flow_key *key = NULL; 53 54 if (level <= OVS_DEFERRED_ACTION_THRESHOLD) { 55 key = &keys->key[level - 1]; 56 *key = *key_; 57 } 58 59 return key; 60 } 61 62 static void action_fifo_init(struct action_fifo *fifo) 63 { 64 fifo->head = 0; 65 fifo->tail = 0; 66 } 67 68 static bool action_fifo_is_empty(const struct action_fifo *fifo) 69 { 70 return (fifo->head == fifo->tail); 71 } 72 73 static struct deferred_action *action_fifo_get(struct action_fifo *fifo) 74 { 75 if (action_fifo_is_empty(fifo)) 76 return NULL; 77 78 return &fifo->fifo[fifo->tail++]; 79 } 80 81 static struct deferred_action *action_fifo_put(struct action_fifo *fifo) 82 { 83 if (fifo->head >= DEFERRED_ACTION_FIFO_SIZE - 1) 84 return NULL; 85 86 return &fifo->fifo[fifo->head++]; 87 } 88 89 /* Return true if fifo is not full */ 90 static struct deferred_action *add_deferred_actions(struct sk_buff *skb, 91 const struct sw_flow_key *key, 92 const struct nlattr *actions, 93 const int actions_len) 94 { 95 struct action_fifo *fifo = this_cpu_ptr(&ovs_pcpu_storage->action_fifos); 96 struct deferred_action *da; 97 98 da = action_fifo_put(fifo); 99 if (da) { 100 da->skb = skb; 101 da->actions = actions; 102 da->actions_len = actions_len; 103 da->pkt_key = *key; 104 } 105 106 return da; 107 } 108 109 static void invalidate_flow_key(struct sw_flow_key *key) 110 { 111 key->mac_proto |= SW_FLOW_KEY_INVALID; 112 } 113 114 static bool is_flow_key_valid(const struct sw_flow_key *key) 115 { 116 return !(key->mac_proto & SW_FLOW_KEY_INVALID); 117 } 118 119 static int clone_execute(struct datapath *dp, struct sk_buff *skb, 120 struct sw_flow_key *key, 121 u32 recirc_id, 122 const struct nlattr *actions, int len, 123 bool last, bool clone_flow_key); 124 125 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb, 126 struct sw_flow_key *key, 127 const struct nlattr *attr, int len); 128 129 static int push_mpls(struct sk_buff *skb, struct sw_flow_key *key, 130 __be32 mpls_lse, __be16 mpls_ethertype, __u16 mac_len) 131 { 132 int err; 133 134 err = skb_mpls_push(skb, mpls_lse, mpls_ethertype, mac_len, !!mac_len); 135 if (err) 136 return err; 137 138 if (!mac_len) 139 key->mac_proto = MAC_PROTO_NONE; 140 141 invalidate_flow_key(key); 142 return 0; 143 } 144 145 static int pop_mpls(struct sk_buff *skb, struct sw_flow_key *key, 146 const __be16 ethertype) 147 { 148 int err; 149 150 err = skb_mpls_pop(skb, ethertype, skb->mac_len, 151 ovs_key_mac_proto(key) == MAC_PROTO_ETHERNET); 152 if (err) 153 return err; 154 155 if (ethertype == htons(ETH_P_TEB)) 156 key->mac_proto = MAC_PROTO_ETHERNET; 157 158 invalidate_flow_key(key); 159 return 0; 160 } 161 162 static int set_mpls(struct sk_buff *skb, struct sw_flow_key *flow_key, 163 const __be32 *mpls_lse, const __be32 *mask) 164 { 165 struct mpls_shim_hdr *stack; 166 __be32 lse; 167 int err; 168 169 if (!pskb_may_pull(skb, skb_network_offset(skb) + MPLS_HLEN)) 170 return -ENOMEM; 171 172 stack = mpls_hdr(skb); 173 lse = OVS_MASKED(stack->label_stack_entry, *mpls_lse, *mask); 174 err = skb_mpls_update_lse(skb, lse); 175 if (err) 176 return err; 177 178 flow_key->mpls.lse[0] = lse; 179 return 0; 180 } 181 182 static int pop_vlan(struct sk_buff *skb, struct sw_flow_key *key) 183 { 184 int err; 185 186 err = skb_vlan_pop(skb); 187 if (skb_vlan_tag_present(skb)) { 188 invalidate_flow_key(key); 189 } else { 190 key->eth.vlan.tci = 0; 191 key->eth.vlan.tpid = 0; 192 } 193 return err; 194 } 195 196 static int push_vlan(struct sk_buff *skb, struct sw_flow_key *key, 197 const struct ovs_action_push_vlan *vlan) 198 { 199 int err; 200 201 if (skb_vlan_tag_present(skb)) { 202 invalidate_flow_key(key); 203 } else { 204 key->eth.vlan.tci = vlan->vlan_tci; 205 key->eth.vlan.tpid = vlan->vlan_tpid; 206 } 207 err = skb_vlan_push(skb, vlan->vlan_tpid, 208 ntohs(vlan->vlan_tci) & ~VLAN_CFI_MASK); 209 skb_reset_mac_len(skb); 210 return err; 211 } 212 213 /* 'src' is already properly masked. */ 214 static void ether_addr_copy_masked(u8 *dst_, const u8 *src_, const u8 *mask_) 215 { 216 u16 *dst = (u16 *)dst_; 217 const u16 *src = (const u16 *)src_; 218 const u16 *mask = (const u16 *)mask_; 219 220 OVS_SET_MASKED(dst[0], src[0], mask[0]); 221 OVS_SET_MASKED(dst[1], src[1], mask[1]); 222 OVS_SET_MASKED(dst[2], src[2], mask[2]); 223 } 224 225 static int set_eth_addr(struct sk_buff *skb, struct sw_flow_key *flow_key, 226 const struct ovs_key_ethernet *key, 227 const struct ovs_key_ethernet *mask) 228 { 229 int err; 230 231 err = skb_ensure_writable(skb, ETH_HLEN); 232 if (unlikely(err)) 233 return err; 234 235 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2); 236 237 ether_addr_copy_masked(eth_hdr(skb)->h_source, key->eth_src, 238 mask->eth_src); 239 ether_addr_copy_masked(eth_hdr(skb)->h_dest, key->eth_dst, 240 mask->eth_dst); 241 242 skb_postpush_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2); 243 244 ether_addr_copy(flow_key->eth.src, eth_hdr(skb)->h_source); 245 ether_addr_copy(flow_key->eth.dst, eth_hdr(skb)->h_dest); 246 return 0; 247 } 248 249 /* pop_eth does not support VLAN packets as this action is never called 250 * for them. 251 */ 252 static int pop_eth(struct sk_buff *skb, struct sw_flow_key *key) 253 { 254 int err; 255 256 err = skb_eth_pop(skb); 257 if (err) 258 return err; 259 260 /* safe right before invalidate_flow_key */ 261 key->mac_proto = MAC_PROTO_NONE; 262 invalidate_flow_key(key); 263 return 0; 264 } 265 266 static int push_eth(struct sk_buff *skb, struct sw_flow_key *key, 267 const struct ovs_action_push_eth *ethh) 268 { 269 int err; 270 271 err = skb_eth_push(skb, ethh->addresses.eth_dst, 272 ethh->addresses.eth_src); 273 if (err) 274 return err; 275 276 /* safe right before invalidate_flow_key */ 277 key->mac_proto = MAC_PROTO_ETHERNET; 278 invalidate_flow_key(key); 279 return 0; 280 } 281 282 static noinline_for_stack int push_nsh(struct sk_buff *skb, 283 struct sw_flow_key *key, 284 const struct nlattr *a) 285 { 286 u8 buffer[NSH_HDR_MAX_LEN]; 287 struct nshhdr *nh = (struct nshhdr *)buffer; 288 int err; 289 290 err = nsh_hdr_from_nlattr(a, nh, NSH_HDR_MAX_LEN); 291 if (err) 292 return err; 293 294 err = nsh_push(skb, nh); 295 if (err) 296 return err; 297 298 /* safe right before invalidate_flow_key */ 299 key->mac_proto = MAC_PROTO_NONE; 300 invalidate_flow_key(key); 301 return 0; 302 } 303 304 static int pop_nsh(struct sk_buff *skb, struct sw_flow_key *key) 305 { 306 int err; 307 308 err = nsh_pop(skb); 309 if (err) 310 return err; 311 312 /* safe right before invalidate_flow_key */ 313 if (skb->protocol == htons(ETH_P_TEB)) 314 key->mac_proto = MAC_PROTO_ETHERNET; 315 else 316 key->mac_proto = MAC_PROTO_NONE; 317 invalidate_flow_key(key); 318 return 0; 319 } 320 321 static void update_ip_l4_checksum(struct sk_buff *skb, struct iphdr *nh, 322 __be32 addr, __be32 new_addr) 323 { 324 int transport_len = skb->len - skb_transport_offset(skb); 325 326 if (nh->frag_off & htons(IP_OFFSET)) 327 return; 328 329 if (nh->protocol == IPPROTO_TCP) { 330 if (likely(transport_len >= sizeof(struct tcphdr))) 331 inet_proto_csum_replace4(&tcp_hdr(skb)->check, skb, 332 addr, new_addr, true); 333 } else if (nh->protocol == IPPROTO_UDP) { 334 if (likely(transport_len >= sizeof(struct udphdr))) { 335 struct udphdr *uh = udp_hdr(skb); 336 337 if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) { 338 inet_proto_csum_replace4(&uh->check, skb, 339 addr, new_addr, true); 340 if (!uh->check) 341 uh->check = CSUM_MANGLED_0; 342 } 343 } 344 } 345 } 346 347 static void set_ip_addr(struct sk_buff *skb, struct iphdr *nh, 348 __be32 *addr, __be32 new_addr) 349 { 350 update_ip_l4_checksum(skb, nh, *addr, new_addr); 351 csum_replace4(&nh->check, *addr, new_addr); 352 skb_clear_hash(skb); 353 ovs_ct_clear(skb, NULL); 354 *addr = new_addr; 355 } 356 357 static void update_ipv6_checksum(struct sk_buff *skb, u8 l4_proto, 358 __be32 addr[4], const __be32 new_addr[4]) 359 { 360 int transport_len = skb->len - skb_transport_offset(skb); 361 362 if (l4_proto == NEXTHDR_TCP) { 363 if (likely(transport_len >= sizeof(struct tcphdr))) 364 inet_proto_csum_replace16(&tcp_hdr(skb)->check, skb, 365 addr, new_addr, true); 366 } else if (l4_proto == NEXTHDR_UDP) { 367 if (likely(transport_len >= sizeof(struct udphdr))) { 368 struct udphdr *uh = udp_hdr(skb); 369 370 if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) { 371 inet_proto_csum_replace16(&uh->check, skb, 372 addr, new_addr, true); 373 if (!uh->check) 374 uh->check = CSUM_MANGLED_0; 375 } 376 } 377 } else if (l4_proto == NEXTHDR_ICMP) { 378 if (likely(transport_len >= sizeof(struct icmp6hdr))) 379 inet_proto_csum_replace16(&icmp6_hdr(skb)->icmp6_cksum, 380 skb, addr, new_addr, true); 381 } 382 } 383 384 static void mask_ipv6_addr(const __be32 old[4], const __be32 addr[4], 385 const __be32 mask[4], __be32 masked[4]) 386 { 387 masked[0] = OVS_MASKED(old[0], addr[0], mask[0]); 388 masked[1] = OVS_MASKED(old[1], addr[1], mask[1]); 389 masked[2] = OVS_MASKED(old[2], addr[2], mask[2]); 390 masked[3] = OVS_MASKED(old[3], addr[3], mask[3]); 391 } 392 393 static void set_ipv6_addr(struct sk_buff *skb, u8 l4_proto, 394 __be32 addr[4], const __be32 new_addr[4], 395 bool recalculate_csum) 396 { 397 if (recalculate_csum) 398 update_ipv6_checksum(skb, l4_proto, addr, new_addr); 399 400 skb_clear_hash(skb); 401 ovs_ct_clear(skb, NULL); 402 memcpy(addr, new_addr, sizeof(__be32[4])); 403 } 404 405 static void set_ipv6_dsfield(struct sk_buff *skb, struct ipv6hdr *nh, u8 ipv6_tclass, u8 mask) 406 { 407 u8 old_ipv6_tclass = ipv6_get_dsfield(nh); 408 409 ipv6_tclass = OVS_MASKED(old_ipv6_tclass, ipv6_tclass, mask); 410 411 if (skb->ip_summed == CHECKSUM_COMPLETE) 412 csum_replace(&skb->csum, (__force __wsum)(old_ipv6_tclass << 12), 413 (__force __wsum)(ipv6_tclass << 12)); 414 415 ipv6_change_dsfield(nh, ~mask, ipv6_tclass); 416 } 417 418 static void set_ipv6_fl(struct sk_buff *skb, struct ipv6hdr *nh, u32 fl, u32 mask) 419 { 420 u32 ofl; 421 422 ofl = nh->flow_lbl[0] << 16 | nh->flow_lbl[1] << 8 | nh->flow_lbl[2]; 423 fl = OVS_MASKED(ofl, fl, mask); 424 425 /* Bits 21-24 are always unmasked, so this retains their values. */ 426 nh->flow_lbl[0] = (u8)(fl >> 16); 427 nh->flow_lbl[1] = (u8)(fl >> 8); 428 nh->flow_lbl[2] = (u8)fl; 429 430 if (skb->ip_summed == CHECKSUM_COMPLETE) 431 csum_replace(&skb->csum, (__force __wsum)htonl(ofl), (__force __wsum)htonl(fl)); 432 } 433 434 static void set_ipv6_ttl(struct sk_buff *skb, struct ipv6hdr *nh, u8 new_ttl, u8 mask) 435 { 436 new_ttl = OVS_MASKED(nh->hop_limit, new_ttl, mask); 437 438 if (skb->ip_summed == CHECKSUM_COMPLETE) 439 csum_replace(&skb->csum, (__force __wsum)(nh->hop_limit << 8), 440 (__force __wsum)(new_ttl << 8)); 441 nh->hop_limit = new_ttl; 442 } 443 444 static void set_ip_ttl(struct sk_buff *skb, struct iphdr *nh, u8 new_ttl, 445 u8 mask) 446 { 447 new_ttl = OVS_MASKED(nh->ttl, new_ttl, mask); 448 449 csum_replace2(&nh->check, htons(nh->ttl << 8), htons(new_ttl << 8)); 450 nh->ttl = new_ttl; 451 } 452 453 static int set_ipv4(struct sk_buff *skb, struct sw_flow_key *flow_key, 454 const struct ovs_key_ipv4 *key, 455 const struct ovs_key_ipv4 *mask) 456 { 457 struct iphdr *nh; 458 __be32 new_addr; 459 int err; 460 461 err = skb_ensure_writable(skb, skb_network_offset(skb) + 462 sizeof(struct iphdr)); 463 if (unlikely(err)) 464 return err; 465 466 nh = ip_hdr(skb); 467 468 /* Setting an IP addresses is typically only a side effect of 469 * matching on them in the current userspace implementation, so it 470 * makes sense to check if the value actually changed. 471 */ 472 if (mask->ipv4_src) { 473 new_addr = OVS_MASKED(nh->saddr, key->ipv4_src, mask->ipv4_src); 474 475 if (unlikely(new_addr != nh->saddr)) { 476 set_ip_addr(skb, nh, &nh->saddr, new_addr); 477 flow_key->ipv4.addr.src = new_addr; 478 } 479 } 480 if (mask->ipv4_dst) { 481 new_addr = OVS_MASKED(nh->daddr, key->ipv4_dst, mask->ipv4_dst); 482 483 if (unlikely(new_addr != nh->daddr)) { 484 set_ip_addr(skb, nh, &nh->daddr, new_addr); 485 flow_key->ipv4.addr.dst = new_addr; 486 } 487 } 488 if (mask->ipv4_tos) { 489 ipv4_change_dsfield(nh, ~mask->ipv4_tos, key->ipv4_tos); 490 flow_key->ip.tos = nh->tos; 491 } 492 if (mask->ipv4_ttl) { 493 set_ip_ttl(skb, nh, key->ipv4_ttl, mask->ipv4_ttl); 494 flow_key->ip.ttl = nh->ttl; 495 } 496 497 return 0; 498 } 499 500 static bool is_ipv6_mask_nonzero(const __be32 addr[4]) 501 { 502 return !!(addr[0] | addr[1] | addr[2] | addr[3]); 503 } 504 505 static int set_ipv6(struct sk_buff *skb, struct sw_flow_key *flow_key, 506 const struct ovs_key_ipv6 *key, 507 const struct ovs_key_ipv6 *mask) 508 { 509 struct ipv6hdr *nh; 510 int err; 511 512 err = skb_ensure_writable(skb, skb_network_offset(skb) + 513 sizeof(struct ipv6hdr)); 514 if (unlikely(err)) 515 return err; 516 517 nh = ipv6_hdr(skb); 518 519 /* Setting an IP addresses is typically only a side effect of 520 * matching on them in the current userspace implementation, so it 521 * makes sense to check if the value actually changed. 522 */ 523 if (is_ipv6_mask_nonzero(mask->ipv6_src)) { 524 __be32 *saddr = (__be32 *)&nh->saddr; 525 __be32 masked[4]; 526 527 mask_ipv6_addr(saddr, key->ipv6_src, mask->ipv6_src, masked); 528 529 if (unlikely(memcmp(saddr, masked, sizeof(masked)))) { 530 set_ipv6_addr(skb, flow_key->ip.proto, saddr, masked, 531 true); 532 memcpy(&flow_key->ipv6.addr.src, masked, 533 sizeof(flow_key->ipv6.addr.src)); 534 } 535 } 536 if (is_ipv6_mask_nonzero(mask->ipv6_dst)) { 537 unsigned int offset = 0; 538 int flags = IP6_FH_F_SKIP_RH; 539 bool recalc_csum = true; 540 __be32 *daddr = (__be32 *)&nh->daddr; 541 __be32 masked[4]; 542 543 mask_ipv6_addr(daddr, key->ipv6_dst, mask->ipv6_dst, masked); 544 545 if (unlikely(memcmp(daddr, masked, sizeof(masked)))) { 546 if (ipv6_ext_hdr(nh->nexthdr)) 547 recalc_csum = (ipv6_find_hdr(skb, &offset, 548 NEXTHDR_ROUTING, 549 NULL, &flags) 550 != NEXTHDR_ROUTING); 551 552 set_ipv6_addr(skb, flow_key->ip.proto, daddr, masked, 553 recalc_csum); 554 memcpy(&flow_key->ipv6.addr.dst, masked, 555 sizeof(flow_key->ipv6.addr.dst)); 556 } 557 } 558 if (mask->ipv6_tclass) { 559 set_ipv6_dsfield(skb, nh, key->ipv6_tclass, mask->ipv6_tclass); 560 flow_key->ip.tos = ipv6_get_dsfield(nh); 561 } 562 if (mask->ipv6_label) { 563 set_ipv6_fl(skb, nh, ntohl(key->ipv6_label), 564 ntohl(mask->ipv6_label)); 565 flow_key->ipv6.label = 566 *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL); 567 } 568 if (mask->ipv6_hlimit) { 569 set_ipv6_ttl(skb, nh, key->ipv6_hlimit, mask->ipv6_hlimit); 570 flow_key->ip.ttl = nh->hop_limit; 571 } 572 return 0; 573 } 574 575 static int set_nsh(struct sk_buff *skb, struct sw_flow_key *flow_key, 576 const struct nlattr *a) 577 { 578 struct nshhdr *nh; 579 size_t length; 580 int err; 581 u8 flags; 582 u8 ttl; 583 int i; 584 585 struct ovs_key_nsh key; 586 struct ovs_key_nsh mask; 587 588 err = nsh_key_from_nlattr(a, &key, &mask); 589 if (err) 590 return err; 591 592 /* Make sure the NSH base header is there */ 593 if (!pskb_may_pull(skb, skb_network_offset(skb) + NSH_BASE_HDR_LEN)) 594 return -ENOMEM; 595 596 nh = nsh_hdr(skb); 597 length = nsh_hdr_len(nh); 598 599 /* Make sure the whole NSH header is there */ 600 err = skb_ensure_writable(skb, skb_network_offset(skb) + 601 length); 602 if (unlikely(err)) 603 return err; 604 605 nh = nsh_hdr(skb); 606 skb_postpull_rcsum(skb, nh, length); 607 flags = nsh_get_flags(nh); 608 flags = OVS_MASKED(flags, key.base.flags, mask.base.flags); 609 flow_key->nsh.base.flags = flags; 610 ttl = nsh_get_ttl(nh); 611 ttl = OVS_MASKED(ttl, key.base.ttl, mask.base.ttl); 612 flow_key->nsh.base.ttl = ttl; 613 nsh_set_flags_and_ttl(nh, flags, ttl); 614 nh->path_hdr = OVS_MASKED(nh->path_hdr, key.base.path_hdr, 615 mask.base.path_hdr); 616 flow_key->nsh.base.path_hdr = nh->path_hdr; 617 switch (nh->mdtype) { 618 case NSH_M_TYPE1: 619 for (i = 0; i < NSH_MD1_CONTEXT_SIZE; i++) { 620 nh->md1.context[i] = 621 OVS_MASKED(nh->md1.context[i], key.context[i], 622 mask.context[i]); 623 } 624 memcpy(flow_key->nsh.context, nh->md1.context, 625 sizeof(nh->md1.context)); 626 break; 627 case NSH_M_TYPE2: 628 memset(flow_key->nsh.context, 0, 629 sizeof(flow_key->nsh.context)); 630 break; 631 default: 632 return -EINVAL; 633 } 634 skb_postpush_rcsum(skb, nh, length); 635 return 0; 636 } 637 638 /* Must follow skb_ensure_writable() since that can move the skb data. */ 639 static void set_tp_port(struct sk_buff *skb, __be16 *port, 640 __be16 new_port, __sum16 *check) 641 { 642 ovs_ct_clear(skb, NULL); 643 inet_proto_csum_replace2(check, skb, *port, new_port, false); 644 *port = new_port; 645 } 646 647 static int set_udp(struct sk_buff *skb, struct sw_flow_key *flow_key, 648 const struct ovs_key_udp *key, 649 const struct ovs_key_udp *mask) 650 { 651 struct udphdr *uh; 652 __be16 src, dst; 653 int err; 654 655 err = skb_ensure_writable(skb, skb_transport_offset(skb) + 656 sizeof(struct udphdr)); 657 if (unlikely(err)) 658 return err; 659 660 uh = udp_hdr(skb); 661 /* Either of the masks is non-zero, so do not bother checking them. */ 662 src = OVS_MASKED(uh->source, key->udp_src, mask->udp_src); 663 dst = OVS_MASKED(uh->dest, key->udp_dst, mask->udp_dst); 664 665 if (uh->check && skb->ip_summed != CHECKSUM_PARTIAL) { 666 if (likely(src != uh->source)) { 667 set_tp_port(skb, &uh->source, src, &uh->check); 668 flow_key->tp.src = src; 669 } 670 if (likely(dst != uh->dest)) { 671 set_tp_port(skb, &uh->dest, dst, &uh->check); 672 flow_key->tp.dst = dst; 673 } 674 675 if (unlikely(!uh->check)) 676 uh->check = CSUM_MANGLED_0; 677 } else { 678 uh->source = src; 679 uh->dest = dst; 680 flow_key->tp.src = src; 681 flow_key->tp.dst = dst; 682 ovs_ct_clear(skb, NULL); 683 } 684 685 skb_clear_hash(skb); 686 687 return 0; 688 } 689 690 static int set_tcp(struct sk_buff *skb, struct sw_flow_key *flow_key, 691 const struct ovs_key_tcp *key, 692 const struct ovs_key_tcp *mask) 693 { 694 struct tcphdr *th; 695 __be16 src, dst; 696 int err; 697 698 err = skb_ensure_writable(skb, skb_transport_offset(skb) + 699 sizeof(struct tcphdr)); 700 if (unlikely(err)) 701 return err; 702 703 th = tcp_hdr(skb); 704 src = OVS_MASKED(th->source, key->tcp_src, mask->tcp_src); 705 if (likely(src != th->source)) { 706 set_tp_port(skb, &th->source, src, &th->check); 707 flow_key->tp.src = src; 708 } 709 dst = OVS_MASKED(th->dest, key->tcp_dst, mask->tcp_dst); 710 if (likely(dst != th->dest)) { 711 set_tp_port(skb, &th->dest, dst, &th->check); 712 flow_key->tp.dst = dst; 713 } 714 skb_clear_hash(skb); 715 716 return 0; 717 } 718 719 static int set_sctp(struct sk_buff *skb, struct sw_flow_key *flow_key, 720 const struct ovs_key_sctp *key, 721 const struct ovs_key_sctp *mask) 722 { 723 unsigned int sctphoff = skb_transport_offset(skb); 724 struct sctphdr *sh; 725 __le32 old_correct_csum, new_csum, old_csum; 726 int err; 727 728 err = skb_ensure_writable(skb, sctphoff + sizeof(struct sctphdr)); 729 if (unlikely(err)) 730 return err; 731 732 sh = sctp_hdr(skb); 733 old_csum = sh->checksum; 734 old_correct_csum = sctp_compute_cksum(skb, sctphoff); 735 736 sh->source = OVS_MASKED(sh->source, key->sctp_src, mask->sctp_src); 737 sh->dest = OVS_MASKED(sh->dest, key->sctp_dst, mask->sctp_dst); 738 739 new_csum = sctp_compute_cksum(skb, sctphoff); 740 741 /* Carry any checksum errors through. */ 742 sh->checksum = old_csum ^ old_correct_csum ^ new_csum; 743 744 skb_clear_hash(skb); 745 ovs_ct_clear(skb, NULL); 746 747 flow_key->tp.src = sh->source; 748 flow_key->tp.dst = sh->dest; 749 750 return 0; 751 } 752 753 static int ovs_vport_output(struct net *net, struct sock *sk, 754 struct sk_buff *skb) 755 { 756 struct ovs_frag_data *data = this_cpu_ptr(&ovs_pcpu_storage->frag_data); 757 struct vport *vport = data->vport; 758 759 if (skb_cow_head(skb, data->l2_len) < 0) { 760 kfree_skb_reason(skb, SKB_DROP_REASON_NOMEM); 761 return -ENOMEM; 762 } 763 764 __skb_dst_copy(skb, data->dst); 765 *OVS_CB(skb) = data->cb; 766 skb->inner_protocol = data->inner_protocol; 767 if (data->vlan_tci & VLAN_CFI_MASK) 768 __vlan_hwaccel_put_tag(skb, data->vlan_proto, data->vlan_tci & ~VLAN_CFI_MASK); 769 else 770 __vlan_hwaccel_clear_tag(skb); 771 772 /* Reconstruct the MAC header. */ 773 skb_push(skb, data->l2_len); 774 memcpy(skb->data, &data->l2_data, data->l2_len); 775 skb_postpush_rcsum(skb, skb->data, data->l2_len); 776 skb_reset_mac_header(skb); 777 778 if (eth_p_mpls(skb->protocol)) { 779 skb->inner_network_header = skb->network_header; 780 skb_set_network_header(skb, data->network_offset); 781 skb_reset_mac_len(skb); 782 } 783 784 ovs_vport_send(vport, skb, data->mac_proto); 785 return 0; 786 } 787 788 static unsigned int 789 ovs_dst_get_mtu(const struct dst_entry *dst) 790 { 791 return dst->dev->mtu; 792 } 793 794 static struct dst_ops ovs_dst_ops = { 795 .family = AF_UNSPEC, 796 .mtu = ovs_dst_get_mtu, 797 }; 798 799 /* prepare_frag() is called once per (larger-than-MTU) frame; its inverse is 800 * ovs_vport_output(), which is called once per fragmented packet. 801 */ 802 static void prepare_frag(struct vport *vport, struct sk_buff *skb, 803 u16 orig_network_offset, u8 mac_proto) 804 { 805 unsigned int hlen = skb_network_offset(skb); 806 struct ovs_frag_data *data; 807 808 data = this_cpu_ptr(&ovs_pcpu_storage->frag_data); 809 data->dst = skb->_skb_refdst; 810 data->vport = vport; 811 data->cb = *OVS_CB(skb); 812 data->inner_protocol = skb->inner_protocol; 813 data->network_offset = orig_network_offset; 814 if (skb_vlan_tag_present(skb)) 815 data->vlan_tci = skb_vlan_tag_get(skb) | VLAN_CFI_MASK; 816 else 817 data->vlan_tci = 0; 818 data->vlan_proto = skb->vlan_proto; 819 data->mac_proto = mac_proto; 820 data->l2_len = hlen; 821 memcpy(&data->l2_data, skb->data, hlen); 822 823 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm)); 824 skb_pull(skb, hlen); 825 } 826 827 static void ovs_fragment(struct net *net, struct vport *vport, 828 struct sk_buff *skb, u16 mru, 829 struct sw_flow_key *key) 830 { 831 enum ovs_drop_reason reason; 832 u16 orig_network_offset = 0; 833 834 if (eth_p_mpls(skb->protocol)) { 835 orig_network_offset = skb_network_offset(skb); 836 skb->network_header = skb->inner_network_header; 837 } 838 839 if (skb_network_offset(skb) > MAX_L2_LEN) { 840 OVS_NLERR(1, "L2 header too long to fragment"); 841 reason = OVS_DROP_FRAG_L2_TOO_LONG; 842 goto err; 843 } 844 845 if (key->eth.type == htons(ETH_P_IP)) { 846 struct rtable ovs_rt = { 0 }; 847 unsigned long orig_dst; 848 849 prepare_frag(vport, skb, orig_network_offset, 850 ovs_key_mac_proto(key)); 851 dst_init(&ovs_rt.dst, &ovs_dst_ops, NULL, 852 DST_OBSOLETE_NONE, DST_NOCOUNT); 853 ovs_rt.dst.dev = vport->dev; 854 855 orig_dst = skb->_skb_refdst; 856 skb_dst_set_noref(skb, &ovs_rt.dst); 857 IPCB(skb)->frag_max_size = mru; 858 859 ip_do_fragment(net, skb->sk, skb, ovs_vport_output); 860 refdst_drop(orig_dst); 861 } else if (key->eth.type == htons(ETH_P_IPV6)) { 862 unsigned long orig_dst; 863 struct rt6_info ovs_rt; 864 865 prepare_frag(vport, skb, orig_network_offset, 866 ovs_key_mac_proto(key)); 867 memset(&ovs_rt, 0, sizeof(ovs_rt)); 868 dst_init(&ovs_rt.dst, &ovs_dst_ops, NULL, 869 DST_OBSOLETE_NONE, DST_NOCOUNT); 870 ovs_rt.dst.dev = vport->dev; 871 872 orig_dst = skb->_skb_refdst; 873 skb_dst_set_noref(skb, &ovs_rt.dst); 874 IP6CB(skb)->frag_max_size = mru; 875 876 ipv6_stub->ipv6_fragment(net, skb->sk, skb, ovs_vport_output); 877 refdst_drop(orig_dst); 878 } else { 879 WARN_ONCE(1, "Failed fragment ->%s: eth=%04x, MRU=%d, MTU=%d.", 880 ovs_vport_name(vport), ntohs(key->eth.type), mru, 881 vport->dev->mtu); 882 reason = OVS_DROP_FRAG_INVALID_PROTO; 883 goto err; 884 } 885 886 return; 887 err: 888 ovs_kfree_skb_reason(skb, reason); 889 } 890 891 static void do_output(struct datapath *dp, struct sk_buff *skb, int out_port, 892 struct sw_flow_key *key) 893 { 894 struct vport *vport = ovs_vport_rcu(dp, out_port); 895 896 if (likely(vport && 897 netif_running(vport->dev) && 898 netif_carrier_ok(vport->dev))) { 899 u16 mru = OVS_CB(skb)->mru; 900 u32 cutlen = OVS_CB(skb)->cutlen; 901 902 if (unlikely(cutlen > 0)) { 903 if (skb->len - cutlen > ovs_mac_header_len(key)) 904 pskb_trim(skb, skb->len - cutlen); 905 else 906 pskb_trim(skb, ovs_mac_header_len(key)); 907 } 908 909 if (likely(!mru || 910 (skb->len <= mru + vport->dev->hard_header_len))) { 911 ovs_vport_send(vport, skb, ovs_key_mac_proto(key)); 912 } else if (mru <= vport->dev->mtu) { 913 struct net *net = read_pnet(&dp->net); 914 915 ovs_fragment(net, vport, skb, mru, key); 916 } else { 917 kfree_skb_reason(skb, SKB_DROP_REASON_PKT_TOO_BIG); 918 } 919 } else { 920 kfree_skb_reason(skb, SKB_DROP_REASON_DEV_READY); 921 } 922 } 923 924 static int output_userspace(struct datapath *dp, struct sk_buff *skb, 925 struct sw_flow_key *key, const struct nlattr *attr, 926 const struct nlattr *actions, int actions_len, 927 uint32_t cutlen) 928 { 929 struct dp_upcall_info upcall; 930 const struct nlattr *a; 931 int rem; 932 933 memset(&upcall, 0, sizeof(upcall)); 934 upcall.cmd = OVS_PACKET_CMD_ACTION; 935 upcall.mru = OVS_CB(skb)->mru; 936 937 nla_for_each_nested(a, attr, rem) { 938 switch (nla_type(a)) { 939 case OVS_USERSPACE_ATTR_USERDATA: 940 upcall.userdata = a; 941 break; 942 943 case OVS_USERSPACE_ATTR_PID: 944 if (OVS_CB(skb)->upcall_pid) 945 upcall.portid = OVS_CB(skb)->upcall_pid; 946 else if (dp->user_features & 947 OVS_DP_F_DISPATCH_UPCALL_PER_CPU) 948 upcall.portid = 949 ovs_dp_get_upcall_portid(dp, 950 smp_processor_id()); 951 else 952 upcall.portid = nla_get_u32(a); 953 break; 954 955 case OVS_USERSPACE_ATTR_EGRESS_TUN_PORT: { 956 /* Get out tunnel info. */ 957 struct vport *vport; 958 959 vport = ovs_vport_rcu(dp, nla_get_u32(a)); 960 if (vport) { 961 int err; 962 963 err = dev_fill_metadata_dst(vport->dev, skb); 964 if (!err) 965 upcall.egress_tun_info = skb_tunnel_info(skb); 966 } 967 968 break; 969 } 970 971 case OVS_USERSPACE_ATTR_ACTIONS: { 972 /* Include actions. */ 973 upcall.actions = actions; 974 upcall.actions_len = actions_len; 975 break; 976 } 977 978 } /* End of switch. */ 979 } 980 981 return ovs_dp_upcall(dp, skb, key, &upcall, cutlen); 982 } 983 984 static int dec_ttl_exception_handler(struct datapath *dp, struct sk_buff *skb, 985 struct sw_flow_key *key, 986 const struct nlattr *attr) 987 { 988 /* The first attribute is always 'OVS_DEC_TTL_ATTR_ACTION'. */ 989 struct nlattr *actions = nla_data(attr); 990 991 if (nla_len(actions)) 992 return clone_execute(dp, skb, key, 0, nla_data(actions), 993 nla_len(actions), true, false); 994 995 ovs_kfree_skb_reason(skb, OVS_DROP_IP_TTL); 996 return 0; 997 } 998 999 /* When 'last' is true, sample() should always consume the 'skb'. 1000 * Otherwise, sample() should keep 'skb' intact regardless what 1001 * actions are executed within sample(). 1002 */ 1003 static int sample(struct datapath *dp, struct sk_buff *skb, 1004 struct sw_flow_key *key, const struct nlattr *attr, 1005 bool last) 1006 { 1007 struct nlattr *actions; 1008 struct nlattr *sample_arg; 1009 int rem = nla_len(attr); 1010 const struct sample_arg *arg; 1011 u32 init_probability; 1012 bool clone_flow_key; 1013 int err; 1014 1015 /* The first action is always 'OVS_SAMPLE_ATTR_ARG'. */ 1016 sample_arg = nla_data(attr); 1017 arg = nla_data(sample_arg); 1018 actions = nla_next(sample_arg, &rem); 1019 init_probability = OVS_CB(skb)->probability; 1020 1021 if ((arg->probability != U32_MAX) && 1022 (!arg->probability || get_random_u32() > arg->probability)) { 1023 if (last) 1024 ovs_kfree_skb_reason(skb, OVS_DROP_LAST_ACTION); 1025 return 0; 1026 } 1027 1028 OVS_CB(skb)->probability = arg->probability; 1029 1030 clone_flow_key = !arg->exec; 1031 err = clone_execute(dp, skb, key, 0, actions, rem, last, 1032 clone_flow_key); 1033 1034 if (!last) 1035 OVS_CB(skb)->probability = init_probability; 1036 1037 return err; 1038 } 1039 1040 /* When 'last' is true, clone() should always consume the 'skb'. 1041 * Otherwise, clone() should keep 'skb' intact regardless what 1042 * actions are executed within clone(). 1043 */ 1044 static int clone(struct datapath *dp, struct sk_buff *skb, 1045 struct sw_flow_key *key, const struct nlattr *attr, 1046 bool last) 1047 { 1048 struct nlattr *actions; 1049 struct nlattr *clone_arg; 1050 int rem = nla_len(attr); 1051 bool dont_clone_flow_key; 1052 1053 /* The first action is always 'OVS_CLONE_ATTR_EXEC'. */ 1054 clone_arg = nla_data(attr); 1055 dont_clone_flow_key = nla_get_u32(clone_arg); 1056 actions = nla_next(clone_arg, &rem); 1057 1058 return clone_execute(dp, skb, key, 0, actions, rem, last, 1059 !dont_clone_flow_key); 1060 } 1061 1062 static void execute_hash(struct sk_buff *skb, struct sw_flow_key *key, 1063 const struct nlattr *attr) 1064 { 1065 struct ovs_action_hash *hash_act = nla_data(attr); 1066 u32 hash = 0; 1067 1068 if (hash_act->hash_alg == OVS_HASH_ALG_L4) { 1069 /* OVS_HASH_ALG_L4 hasing type. */ 1070 hash = skb_get_hash(skb); 1071 } else if (hash_act->hash_alg == OVS_HASH_ALG_SYM_L4) { 1072 /* OVS_HASH_ALG_SYM_L4 hashing type. NOTE: this doesn't 1073 * extend past an encapsulated header. 1074 */ 1075 hash = __skb_get_hash_symmetric(skb); 1076 } 1077 1078 hash = jhash_1word(hash, hash_act->hash_basis); 1079 if (!hash) 1080 hash = 0x1; 1081 1082 key->ovs_flow_hash = hash; 1083 } 1084 1085 static int execute_set_action(struct sk_buff *skb, 1086 struct sw_flow_key *flow_key, 1087 const struct nlattr *a) 1088 { 1089 /* Only tunnel set execution is supported without a mask. */ 1090 if (nla_type(a) == OVS_KEY_ATTR_TUNNEL_INFO) { 1091 struct ovs_tunnel_info *tun = nla_data(a); 1092 1093 skb_dst_drop(skb); 1094 dst_hold((struct dst_entry *)tun->tun_dst); 1095 skb_dst_set(skb, (struct dst_entry *)tun->tun_dst); 1096 return 0; 1097 } 1098 1099 return -EINVAL; 1100 } 1101 1102 /* Mask is at the midpoint of the data. */ 1103 #define get_mask(a, type) ((const type)nla_data(a) + 1) 1104 1105 static int execute_masked_set_action(struct sk_buff *skb, 1106 struct sw_flow_key *flow_key, 1107 const struct nlattr *a) 1108 { 1109 int err = 0; 1110 1111 switch (nla_type(a)) { 1112 case OVS_KEY_ATTR_PRIORITY: 1113 OVS_SET_MASKED(skb->priority, nla_get_u32(a), 1114 *get_mask(a, u32 *)); 1115 flow_key->phy.priority = skb->priority; 1116 break; 1117 1118 case OVS_KEY_ATTR_SKB_MARK: 1119 OVS_SET_MASKED(skb->mark, nla_get_u32(a), *get_mask(a, u32 *)); 1120 flow_key->phy.skb_mark = skb->mark; 1121 break; 1122 1123 case OVS_KEY_ATTR_TUNNEL_INFO: 1124 /* Masked data not supported for tunnel. */ 1125 err = -EINVAL; 1126 break; 1127 1128 case OVS_KEY_ATTR_ETHERNET: 1129 err = set_eth_addr(skb, flow_key, nla_data(a), 1130 get_mask(a, struct ovs_key_ethernet *)); 1131 break; 1132 1133 case OVS_KEY_ATTR_NSH: 1134 err = set_nsh(skb, flow_key, a); 1135 break; 1136 1137 case OVS_KEY_ATTR_IPV4: 1138 err = set_ipv4(skb, flow_key, nla_data(a), 1139 get_mask(a, struct ovs_key_ipv4 *)); 1140 break; 1141 1142 case OVS_KEY_ATTR_IPV6: 1143 err = set_ipv6(skb, flow_key, nla_data(a), 1144 get_mask(a, struct ovs_key_ipv6 *)); 1145 break; 1146 1147 case OVS_KEY_ATTR_TCP: 1148 err = set_tcp(skb, flow_key, nla_data(a), 1149 get_mask(a, struct ovs_key_tcp *)); 1150 break; 1151 1152 case OVS_KEY_ATTR_UDP: 1153 err = set_udp(skb, flow_key, nla_data(a), 1154 get_mask(a, struct ovs_key_udp *)); 1155 break; 1156 1157 case OVS_KEY_ATTR_SCTP: 1158 err = set_sctp(skb, flow_key, nla_data(a), 1159 get_mask(a, struct ovs_key_sctp *)); 1160 break; 1161 1162 case OVS_KEY_ATTR_MPLS: 1163 err = set_mpls(skb, flow_key, nla_data(a), get_mask(a, 1164 __be32 *)); 1165 break; 1166 1167 case OVS_KEY_ATTR_CT_STATE: 1168 case OVS_KEY_ATTR_CT_ZONE: 1169 case OVS_KEY_ATTR_CT_MARK: 1170 case OVS_KEY_ATTR_CT_LABELS: 1171 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4: 1172 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6: 1173 err = -EINVAL; 1174 break; 1175 } 1176 1177 return err; 1178 } 1179 1180 static int execute_recirc(struct datapath *dp, struct sk_buff *skb, 1181 struct sw_flow_key *key, 1182 const struct nlattr *a, bool last) 1183 { 1184 u32 recirc_id; 1185 1186 if (!is_flow_key_valid(key)) { 1187 int err; 1188 1189 err = ovs_flow_key_update(skb, key); 1190 if (err) 1191 return err; 1192 } 1193 BUG_ON(!is_flow_key_valid(key)); 1194 1195 recirc_id = nla_get_u32(a); 1196 return clone_execute(dp, skb, key, recirc_id, NULL, 0, last, true); 1197 } 1198 1199 static int execute_check_pkt_len(struct datapath *dp, struct sk_buff *skb, 1200 struct sw_flow_key *key, 1201 const struct nlattr *attr, bool last) 1202 { 1203 struct ovs_skb_cb *ovs_cb = OVS_CB(skb); 1204 const struct nlattr *actions, *cpl_arg; 1205 int len, max_len, rem = nla_len(attr); 1206 const struct check_pkt_len_arg *arg; 1207 bool clone_flow_key; 1208 1209 /* The first netlink attribute in 'attr' is always 1210 * 'OVS_CHECK_PKT_LEN_ATTR_ARG'. 1211 */ 1212 cpl_arg = nla_data(attr); 1213 arg = nla_data(cpl_arg); 1214 1215 len = ovs_cb->mru ? ovs_cb->mru + skb->mac_len : skb->len; 1216 max_len = arg->pkt_len; 1217 1218 if ((skb_is_gso(skb) && skb_gso_validate_mac_len(skb, max_len)) || 1219 len <= max_len) { 1220 /* Second netlink attribute in 'attr' is always 1221 * 'OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL'. 1222 */ 1223 actions = nla_next(cpl_arg, &rem); 1224 clone_flow_key = !arg->exec_for_lesser_equal; 1225 } else { 1226 /* Third netlink attribute in 'attr' is always 1227 * 'OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER'. 1228 */ 1229 actions = nla_next(cpl_arg, &rem); 1230 actions = nla_next(actions, &rem); 1231 clone_flow_key = !arg->exec_for_greater; 1232 } 1233 1234 return clone_execute(dp, skb, key, 0, nla_data(actions), 1235 nla_len(actions), last, clone_flow_key); 1236 } 1237 1238 static int execute_dec_ttl(struct sk_buff *skb, struct sw_flow_key *key) 1239 { 1240 int err; 1241 1242 if (skb->protocol == htons(ETH_P_IPV6)) { 1243 struct ipv6hdr *nh; 1244 1245 err = skb_ensure_writable(skb, skb_network_offset(skb) + 1246 sizeof(*nh)); 1247 if (unlikely(err)) 1248 return err; 1249 1250 nh = ipv6_hdr(skb); 1251 1252 if (nh->hop_limit <= 1) 1253 return -EHOSTUNREACH; 1254 1255 key->ip.ttl = --nh->hop_limit; 1256 } else if (skb->protocol == htons(ETH_P_IP)) { 1257 struct iphdr *nh; 1258 u8 old_ttl; 1259 1260 err = skb_ensure_writable(skb, skb_network_offset(skb) + 1261 sizeof(*nh)); 1262 if (unlikely(err)) 1263 return err; 1264 1265 nh = ip_hdr(skb); 1266 if (nh->ttl <= 1) 1267 return -EHOSTUNREACH; 1268 1269 old_ttl = nh->ttl--; 1270 csum_replace2(&nh->check, htons(old_ttl << 8), 1271 htons(nh->ttl << 8)); 1272 key->ip.ttl = nh->ttl; 1273 } 1274 return 0; 1275 } 1276 1277 #if IS_ENABLED(CONFIG_PSAMPLE) 1278 static void execute_psample(struct datapath *dp, struct sk_buff *skb, 1279 const struct nlattr *attr) 1280 { 1281 struct psample_group psample_group = {}; 1282 struct psample_metadata md = {}; 1283 const struct nlattr *a; 1284 u32 rate; 1285 int rem; 1286 1287 nla_for_each_attr(a, nla_data(attr), nla_len(attr), rem) { 1288 switch (nla_type(a)) { 1289 case OVS_PSAMPLE_ATTR_GROUP: 1290 psample_group.group_num = nla_get_u32(a); 1291 break; 1292 1293 case OVS_PSAMPLE_ATTR_COOKIE: 1294 md.user_cookie = nla_data(a); 1295 md.user_cookie_len = nla_len(a); 1296 break; 1297 } 1298 } 1299 1300 psample_group.net = ovs_dp_get_net(dp); 1301 md.in_ifindex = OVS_CB(skb)->input_vport->dev->ifindex; 1302 md.trunc_size = skb->len - OVS_CB(skb)->cutlen; 1303 md.rate_as_probability = 1; 1304 1305 rate = OVS_CB(skb)->probability ? OVS_CB(skb)->probability : U32_MAX; 1306 1307 psample_sample_packet(&psample_group, skb, rate, &md); 1308 } 1309 #else 1310 static void execute_psample(struct datapath *dp, struct sk_buff *skb, 1311 const struct nlattr *attr) 1312 {} 1313 #endif 1314 1315 /* Execute a list of actions against 'skb'. */ 1316 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb, 1317 struct sw_flow_key *key, 1318 const struct nlattr *attr, int len) 1319 { 1320 const struct nlattr *a; 1321 int rem; 1322 1323 for (a = attr, rem = len; rem > 0; 1324 a = nla_next(a, &rem)) { 1325 int err = 0; 1326 1327 if (trace_ovs_do_execute_action_enabled()) 1328 trace_ovs_do_execute_action(dp, skb, key, a, rem); 1329 1330 /* Actions that rightfully have to consume the skb should do it 1331 * and return directly. 1332 */ 1333 switch (nla_type(a)) { 1334 case OVS_ACTION_ATTR_OUTPUT: { 1335 int port = nla_get_u32(a); 1336 struct sk_buff *clone; 1337 1338 /* Every output action needs a separate clone 1339 * of 'skb', In case the output action is the 1340 * last action, cloning can be avoided. 1341 */ 1342 if (nla_is_last(a, rem)) { 1343 do_output(dp, skb, port, key); 1344 /* 'skb' has been used for output. 1345 */ 1346 return 0; 1347 } 1348 1349 clone = skb_clone(skb, GFP_ATOMIC); 1350 if (clone) 1351 do_output(dp, clone, port, key); 1352 OVS_CB(skb)->cutlen = 0; 1353 break; 1354 } 1355 1356 case OVS_ACTION_ATTR_TRUNC: { 1357 struct ovs_action_trunc *trunc = nla_data(a); 1358 1359 if (skb->len > trunc->max_len) 1360 OVS_CB(skb)->cutlen = skb->len - trunc->max_len; 1361 break; 1362 } 1363 1364 case OVS_ACTION_ATTR_USERSPACE: 1365 output_userspace(dp, skb, key, a, attr, 1366 len, OVS_CB(skb)->cutlen); 1367 OVS_CB(skb)->cutlen = 0; 1368 if (nla_is_last(a, rem)) { 1369 consume_skb(skb); 1370 return 0; 1371 } 1372 break; 1373 1374 case OVS_ACTION_ATTR_HASH: 1375 execute_hash(skb, key, a); 1376 break; 1377 1378 case OVS_ACTION_ATTR_PUSH_MPLS: { 1379 struct ovs_action_push_mpls *mpls = nla_data(a); 1380 1381 err = push_mpls(skb, key, mpls->mpls_lse, 1382 mpls->mpls_ethertype, skb->mac_len); 1383 break; 1384 } 1385 case OVS_ACTION_ATTR_ADD_MPLS: { 1386 struct ovs_action_add_mpls *mpls = nla_data(a); 1387 __u16 mac_len = 0; 1388 1389 if (mpls->tun_flags & OVS_MPLS_L3_TUNNEL_FLAG_MASK) 1390 mac_len = skb->mac_len; 1391 1392 err = push_mpls(skb, key, mpls->mpls_lse, 1393 mpls->mpls_ethertype, mac_len); 1394 break; 1395 } 1396 case OVS_ACTION_ATTR_POP_MPLS: 1397 err = pop_mpls(skb, key, nla_get_be16(a)); 1398 break; 1399 1400 case OVS_ACTION_ATTR_PUSH_VLAN: 1401 err = push_vlan(skb, key, nla_data(a)); 1402 break; 1403 1404 case OVS_ACTION_ATTR_POP_VLAN: 1405 err = pop_vlan(skb, key); 1406 break; 1407 1408 case OVS_ACTION_ATTR_RECIRC: { 1409 bool last = nla_is_last(a, rem); 1410 1411 err = execute_recirc(dp, skb, key, a, last); 1412 if (last) { 1413 /* If this is the last action, the skb has 1414 * been consumed or freed. 1415 * Return immediately. 1416 */ 1417 return err; 1418 } 1419 break; 1420 } 1421 1422 case OVS_ACTION_ATTR_SET: 1423 err = execute_set_action(skb, key, nla_data(a)); 1424 break; 1425 1426 case OVS_ACTION_ATTR_SET_MASKED: 1427 case OVS_ACTION_ATTR_SET_TO_MASKED: 1428 err = execute_masked_set_action(skb, key, nla_data(a)); 1429 break; 1430 1431 case OVS_ACTION_ATTR_SAMPLE: { 1432 bool last = nla_is_last(a, rem); 1433 1434 err = sample(dp, skb, key, a, last); 1435 if (last) 1436 return err; 1437 1438 break; 1439 } 1440 1441 case OVS_ACTION_ATTR_CT: 1442 if (!is_flow_key_valid(key)) { 1443 err = ovs_flow_key_update(skb, key); 1444 if (err) 1445 return err; 1446 } 1447 1448 err = ovs_ct_execute(ovs_dp_get_net(dp), skb, key, 1449 nla_data(a)); 1450 1451 /* Hide stolen IP fragments from user space. */ 1452 if (err) 1453 return err == -EINPROGRESS ? 0 : err; 1454 break; 1455 1456 case OVS_ACTION_ATTR_CT_CLEAR: 1457 err = ovs_ct_clear(skb, key); 1458 break; 1459 1460 case OVS_ACTION_ATTR_PUSH_ETH: 1461 err = push_eth(skb, key, nla_data(a)); 1462 break; 1463 1464 case OVS_ACTION_ATTR_POP_ETH: 1465 err = pop_eth(skb, key); 1466 break; 1467 1468 case OVS_ACTION_ATTR_PUSH_NSH: 1469 err = push_nsh(skb, key, nla_data(a)); 1470 break; 1471 1472 case OVS_ACTION_ATTR_POP_NSH: 1473 err = pop_nsh(skb, key); 1474 break; 1475 1476 case OVS_ACTION_ATTR_METER: 1477 if (ovs_meter_execute(dp, skb, key, nla_get_u32(a))) { 1478 ovs_kfree_skb_reason(skb, OVS_DROP_METER); 1479 return 0; 1480 } 1481 break; 1482 1483 case OVS_ACTION_ATTR_CLONE: { 1484 bool last = nla_is_last(a, rem); 1485 1486 err = clone(dp, skb, key, a, last); 1487 if (last) 1488 return err; 1489 1490 break; 1491 } 1492 1493 case OVS_ACTION_ATTR_CHECK_PKT_LEN: { 1494 bool last = nla_is_last(a, rem); 1495 1496 err = execute_check_pkt_len(dp, skb, key, a, last); 1497 if (last) 1498 return err; 1499 1500 break; 1501 } 1502 1503 case OVS_ACTION_ATTR_DEC_TTL: 1504 err = execute_dec_ttl(skb, key); 1505 if (err == -EHOSTUNREACH) 1506 return dec_ttl_exception_handler(dp, skb, 1507 key, a); 1508 break; 1509 1510 case OVS_ACTION_ATTR_DROP: { 1511 enum ovs_drop_reason reason = nla_get_u32(a) 1512 ? OVS_DROP_EXPLICIT_WITH_ERROR 1513 : OVS_DROP_EXPLICIT; 1514 1515 ovs_kfree_skb_reason(skb, reason); 1516 return 0; 1517 } 1518 1519 case OVS_ACTION_ATTR_PSAMPLE: 1520 execute_psample(dp, skb, a); 1521 OVS_CB(skb)->cutlen = 0; 1522 if (nla_is_last(a, rem)) { 1523 consume_skb(skb); 1524 return 0; 1525 } 1526 break; 1527 } 1528 1529 if (unlikely(err)) { 1530 ovs_kfree_skb_reason(skb, OVS_DROP_ACTION_ERROR); 1531 return err; 1532 } 1533 } 1534 1535 ovs_kfree_skb_reason(skb, OVS_DROP_LAST_ACTION); 1536 return 0; 1537 } 1538 1539 /* Execute the actions on the clone of the packet. The effect of the 1540 * execution does not affect the original 'skb' nor the original 'key'. 1541 * 1542 * The execution may be deferred in case the actions can not be executed 1543 * immediately. 1544 */ 1545 static int clone_execute(struct datapath *dp, struct sk_buff *skb, 1546 struct sw_flow_key *key, u32 recirc_id, 1547 const struct nlattr *actions, int len, 1548 bool last, bool clone_flow_key) 1549 { 1550 struct deferred_action *da; 1551 struct sw_flow_key *clone; 1552 1553 skb = last ? skb : skb_clone(skb, GFP_ATOMIC); 1554 if (!skb) { 1555 /* Out of memory, skip this action. 1556 */ 1557 return 0; 1558 } 1559 1560 /* When clone_flow_key is false, the 'key' will not be change 1561 * by the actions, then the 'key' can be used directly. 1562 * Otherwise, try to clone key from the next recursion level of 1563 * 'flow_keys'. If clone is successful, execute the actions 1564 * without deferring. 1565 */ 1566 clone = clone_flow_key ? clone_key(key) : key; 1567 if (clone) { 1568 int err = 0; 1569 if (actions) { /* Sample action */ 1570 if (clone_flow_key) 1571 __this_cpu_inc(ovs_pcpu_storage->exec_level); 1572 1573 err = do_execute_actions(dp, skb, clone, 1574 actions, len); 1575 1576 if (clone_flow_key) 1577 __this_cpu_dec(ovs_pcpu_storage->exec_level); 1578 } else { /* Recirc action */ 1579 clone->recirc_id = recirc_id; 1580 ovs_dp_process_packet(skb, clone); 1581 } 1582 return err; 1583 } 1584 1585 /* Out of 'flow_keys' space. Defer actions */ 1586 da = add_deferred_actions(skb, key, actions, len); 1587 if (da) { 1588 if (!actions) { /* Recirc action */ 1589 key = &da->pkt_key; 1590 key->recirc_id = recirc_id; 1591 } 1592 } else { 1593 /* Out of per CPU action FIFO space. Drop the 'skb' and 1594 * log an error. 1595 */ 1596 ovs_kfree_skb_reason(skb, OVS_DROP_DEFERRED_LIMIT); 1597 1598 if (net_ratelimit()) { 1599 if (actions) { /* Sample action */ 1600 pr_warn("%s: deferred action limit reached, drop sample action\n", 1601 ovs_dp_name(dp)); 1602 } else { /* Recirc action */ 1603 pr_warn("%s: deferred action limit reached, drop recirc action (recirc_id=%#x)\n", 1604 ovs_dp_name(dp), recirc_id); 1605 } 1606 } 1607 } 1608 return 0; 1609 } 1610 1611 static void process_deferred_actions(struct datapath *dp) 1612 { 1613 struct action_fifo *fifo = this_cpu_ptr(&ovs_pcpu_storage->action_fifos); 1614 1615 /* Do not touch the FIFO in case there is no deferred actions. */ 1616 if (action_fifo_is_empty(fifo)) 1617 return; 1618 1619 /* Finishing executing all deferred actions. */ 1620 do { 1621 struct deferred_action *da = action_fifo_get(fifo); 1622 struct sk_buff *skb = da->skb; 1623 struct sw_flow_key *key = &da->pkt_key; 1624 const struct nlattr *actions = da->actions; 1625 int actions_len = da->actions_len; 1626 1627 if (actions) 1628 do_execute_actions(dp, skb, key, actions, actions_len); 1629 else 1630 ovs_dp_process_packet(skb, key); 1631 } while (!action_fifo_is_empty(fifo)); 1632 1633 /* Reset FIFO for the next packet. */ 1634 action_fifo_init(fifo); 1635 } 1636 1637 /* Execute a list of actions against 'skb'. */ 1638 int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb, 1639 const struct sw_flow_actions *acts, 1640 struct sw_flow_key *key) 1641 { 1642 int err, level; 1643 1644 level = __this_cpu_inc_return(ovs_pcpu_storage->exec_level); 1645 if (unlikely(level > OVS_RECURSION_LIMIT)) { 1646 net_crit_ratelimited("ovs: recursion limit reached on datapath %s, probable configuration error\n", 1647 ovs_dp_name(dp)); 1648 ovs_kfree_skb_reason(skb, OVS_DROP_RECURSION_LIMIT); 1649 err = -ENETDOWN; 1650 goto out; 1651 } 1652 1653 OVS_CB(skb)->acts_origlen = acts->orig_len; 1654 err = do_execute_actions(dp, skb, key, 1655 acts->actions, acts->actions_len); 1656 1657 if (level == 1) 1658 process_deferred_actions(dp); 1659 1660 out: 1661 __this_cpu_dec(ovs_pcpu_storage->exec_level); 1662 return err; 1663 } 1664