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