// SPDX-License-Identifier: GPL-2.0-only/* * Copyright (c) 2007-2017 Nicira, Inc. */ #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt #include <linux/skbuff.h>#include <linux/in.h>#include <linux/ip.h>#include <linux/openvswitch.h>#include <linux/sctp.h>#include <linux/tcp.h>#include <linux/udp.h>#include <linux/in6.h>#include <linux/if_arp.h>#include <linux/if_vlan.h> #include <net/dst.h>#include <net/gso.h>#include <net/ip.h>#include <net/ipv6.h>#include <net/ip6_fib.h>#include <net/ip6_route.h>#include <net/checksum.h>#include <net/dsfield.h>#include <net/mpls.h> #if IS_ENABLED(CONFIG_PSAMPLE)#include <net/psample.h>#endif #include <net/sctp/checksum.h> #include "datapath.h"#include "drop.h"#include "flow.h"#include "conntrack.h"#include "vport.h"#include "flow_netlink.h"#include "openvswitch_trace.h" struct ovs_pcpu_storage __percpu *ovs_pcpu_storage; /* Make a clone of the 'key', using the pre-allocated percpu 'flow_keys' * space. Return NULL if out of key spaces. */static struct sw_flow_key *clone_key(const struct sw_flow_key *key_){ struct ovs_pcpu_storage *ovs_pcpu = this_cpu_ptr(ovs_pcpu_storage); struct action_flow_keys *keys = &ovs_pcpu->flow_keys; int level = ovs_pcpu->exec_level; struct sw_flow_key *key = NULL; if (level <= OVS_DEFERRED_ACTION_THRESHOLD) { key = &keys->key[level - 1]; *key = *key_; } return key;} static void action_fifo_init(struct action_fifo *fifo){ fifo->head = 0; fifo->tail = 0;} static bool action_fifo_is_empty(const struct action_fifo *fifo){ return (fifo->head == fifo->tail);} static struct deferred_action *action_fifo_get(struct action_fifo *fifo){ if (action_fifo_is_empty(fifo)) return NULL; return &fifo->fifo[fifo->tail++];} static struct deferred_action *action_fifo_put(struct action_fifo *fifo){ if (fifo->head >= DEFERRED_ACTION_FIFO_SIZE - 1) return NULL; return &fifo->fifo[fifo->head++];} /* Return true if fifo is not full */static struct deferred_action *add_deferred_actions(struct sk_buff *skb, const struct sw_flow_key *key, const struct nlattr *actions, const int actions_len){ struct action_fifo *fifo = this_cpu_ptr(&ovs_pcpu_storage->action_fifos); struct deferred_action *da; da = action_fifo_put(fifo); if (da) { da->skb = skb; da->actions = actions; da->actions_len = actions_len; da->pkt_key = *key; } return da;} static void invalidate_flow_key(struct sw_flow_key *key){ key->mac_proto |= SW_FLOW_KEY_INVALID;} static bool is_flow_key_valid(const struct sw_flow_key *key){ return !(key->mac_proto & SW_FLOW_KEY_INVALID);} static int clone_execute(struct datapath *dp, struct sk_buff *skb, struct sw_flow_key *key, u32 recirc_id, const struct nlattr *actions, int len, bool last, bool clone_flow_key); static int do_execute_actions(struct datapath *dp, struct sk_buff *skb, struct sw_flow_key *key, const struct nlattr *attr, int len); static int push_mpls(struct sk_buff *skb, struct sw_flow_key *key, __be32 mpls_lse, __be16 mpls_ethertype, __u16 mac_len){ int err; err = skb_mpls_push(skb, mpls_lse, mpls_ethertype, mac_len, !!mac_len); if (err) return err; if (!mac_len) key->mac_proto = MAC_PROTO_NONE; invalidate_flow_key(key); return 0;} static int pop_mpls(struct sk_buff *skb, struct sw_flow_key *key, const __be16 ethertype){ int err; err = skb_mpls_pop(skb, ethertype, skb->mac_len, ovs_key_mac_proto(key) == MAC_PROTO_ETHERNET); if (err) return err; if (ethertype == htons(ETH_P_TEB)) key->mac_proto = MAC_PROTO_ETHERNET; invalidate_flow_key(key); return 0;} static int set_mpls(struct sk_buff *skb, struct sw_flow_key *flow_key, const __be32 *mpls_lse, const __be32 *mask){ struct mpls_shim_hdr *stack; __be32 lse; int err; if (!pskb_may_pull(skb, skb_network_offset(skb) + MPLS_HLEN)) return -ENOMEM; stack = mpls_hdr(skb); lse = OVS_MASKED(stack->label_stack_entry, *mpls_lse, *mask); err = skb_mpls_update_lse(skb, lse); if (err) return err; flow_key->mpls.lse[0] = lse; return 0;} static int pop_vlan(struct sk_buff *skb, struct sw_flow_key *key){ int err; err = skb_vlan_pop(skb); if (skb_vlan_tag_present(skb)) { invalidate_flow_key(key); } else { key->eth.vlan.tci = 0; key->eth.vlan.tpid = 0; } return err;} static int push_vlan(struct sk_buff *skb, struct sw_flow_key *key, const struct ovs_action_push_vlan *vlan){ int err; if (skb_vlan_tag_present(skb)) { invalidate_flow_key(key); } else { key->eth.vlan.tci = vlan->vlan_tci; key->eth.vlan.tpid = vlan->vlan_tpid; } err = skb_vlan_push(skb, vlan->vlan_tpid, ntohs(vlan->vlan_tci) & ~VLAN_CFI_MASK); skb_reset_mac_len(skb); return err;} /* 'src' is already properly masked. */static void ether_addr_copy_masked(u8 *dst_, const u8 *src_, const u8 *mask_){ u16 *dst = (u16 *)dst_; const u16 *src = (const u16 *)src_; const u16 *mask = (const u16 *)mask_; OVS_SET_MASKED(dst[0], src[0], mask[0]); OVS_SET_MASKED(dst[1], src[1], mask[1]); OVS_SET_MASKED(dst[2], src[2], mask[2]);} static int set_eth_addr(struct sk_buff *skb, struct sw_flow_key *flow_key, const struct ovs_key_ethernet *key, const struct ovs_key_ethernet *mask){ int err; err = skb_ensure_writable(skb, ETH_HLEN); if (unlikely(err)) return err; skb_postpull_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2); ether_addr_copy_masked(eth_hdr(skb)->h_source, key->eth_src, mask->eth_src); ether_addr_copy_masked(eth_hdr(skb)->h_dest, key->eth_dst, mask->eth_dst); skb_postpush_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2); ether_addr_copy(flow_key->eth.src, eth_hdr(skb)->h_source); ether_addr_copy(flow_key->eth.dst, eth_hdr(skb)->h_dest); return 0;} /* pop_eth does not support VLAN packets as this action is never called * for them. */static int pop_eth(struct sk_buff *skb, struct sw_flow_key *key){ int err; err = skb_eth_pop(skb); if (err) return err; /* safe right before invalidate_flow_key */ key->mac_proto = MAC_PROTO_NONE; invalidate_flow_key(key); return 0;} static int push_eth(struct sk_buff *skb, struct sw_flow_key *key, const struct ovs_action_push_eth *ethh){ int err; err = skb_eth_push(skb, ethh->addresses.eth_dst, ethh->addresses.eth_src); if (err) return err; /* safe right before invalidate_flow_key */ key->mac_proto = MAC_PROTO_ETHERNET; invalidate_flow_key(key); return 0;} static noinline_for_stack int push_nsh(struct sk_buff *skb, struct sw_flow_key *key, const struct nlattr *a){ u8 buffer[NSH_HDR_MAX_LEN]; struct nshhdr *nh = (struct nshhdr *)buffer; int err; err = nsh_hdr_from_nlattr(a, nh, NSH_HDR_MAX_LEN); if (err) return err; err = nsh_push(skb, nh); if (err) return err; /* safe right before invalidate_flow_key */ key->mac_proto = MAC_PROTO_NONE; invalidate_flow_key(key); return 0;} static int pop_nsh(struct sk_buff *skb, struct sw_flow_key *key){ int err; err = nsh_pop(skb); if (err) return err; /* safe right before invalidate_flow_key */ if (skb->protocol == htons(ETH_P_TEB)) key->mac_proto = MAC_PROTO_ETHERNET; else key->mac_proto = MAC_PROTO_NONE; invalidate_flow_key(key); return 0;} static void update_ip_l4_checksum(struct sk_buff *skb, struct iphdr *nh, __be32 addr, __be32 new_addr){ int transport_len = skb->len - skb_transport_offset(skb); if (nh->frag_off & htons(IP_OFFSET)) return; if (nh->protocol == IPPROTO_TCP) { if (likely(transport_len >= sizeof(struct tcphdr))) inet_proto_csum_replace4(&tcp_hdr(skb)->check, skb, addr, new_addr, true); } else if (nh->protocol == IPPROTO_UDP) { if (likely(transport_len >= sizeof(struct udphdr))) { struct udphdr *uh = udp_hdr(skb); if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) { inet_proto_csum_replace4(&uh->check, skb, addr, new_addr, true); if (!uh->check) uh->check = CSUM_MANGLED_0; } } }} static void set_ip_addr(struct sk_buff *skb, struct iphdr *nh, __be32 *addr, __be32 new_addr){ update_ip_l4_checksum(skb, nh, *addr, new_addr); csum_replace4(&nh->check, *addr, new_addr); skb_clear_hash(skb); ovs_ct_clear(skb, NULL); *addr = new_addr;} static void update_ipv6_checksum(struct sk_buff *skb, u8 l4_proto, __be32 addr[4], const __be32 new_addr[4]){ int transport_len = skb->len - skb_transport_offset(skb); if (l4_proto == NEXTHDR_TCP) { if (likely(transport_len >= sizeof(struct tcphdr))) inet_proto_csum_replace16(&tcp_hdr(skb)->check, skb, addr, new_addr, true); } else if (l4_proto == NEXTHDR_UDP) { if (likely(transport_len >= sizeof(struct udphdr))) { struct udphdr *uh = udp_hdr(skb); if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) { inet_proto_csum_replace16(&uh->check, skb, addr, new_addr, true); if (!uh->check) uh->check = CSUM_MANGLED_0; } } } else if (l4_proto == NEXTHDR_ICMP) { if (likely(transport_len >= sizeof(struct icmp6hdr))) inet_proto_csum_replace16(&icmp6_hdr(skb)->icmp6_cksum, skb, addr, new_addr, true); }} static void mask_ipv6_addr(const __be32 old[4], const __be32 addr[4], const __be32 mask[4], __be32 masked[4]){ masked[0] = OVS_MASKED(old[0], addr[0], mask[0]); masked[1] = OVS_MASKED(old[1], addr[1], mask[1]); masked[2] = OVS_MASKED(old[2], addr[2], mask[2]); masked[3] = OVS_MASKED(old[3], addr[3], mask[3]);} static void set_ipv6_addr(struct sk_buff *skb, u8 l4_proto, __be32 addr[4], const __be32 new_addr[4], bool recalculate_csum){ if (recalculate_csum) update_ipv6_checksum(skb, l4_proto, addr, new_addr); skb_clear_hash(skb); ovs_ct_clear(skb, NULL); memcpy(addr, new_addr, sizeof(__be32[4]));} static void set_ipv6_dsfield(struct sk_buff *skb, struct ipv6hdr *nh, u8 ipv6_tclass, u8 mask){ u8 old_ipv6_tclass = ipv6_get_dsfield(nh); ipv6_tclass = OVS_MASKED(old_ipv6_tclass, ipv6_tclass, mask); if (skb->ip_summed == CHECKSUM_COMPLETE) csum_replace(&skb->csum, (__force __wsum)(old_ipv6_tclass << 12), (__force __wsum)(ipv6_tclass << 12)); ipv6_change_dsfield(nh, ~mask, ipv6_tclass);} static void set_ipv6_fl(struct sk_buff *skb, struct ipv6hdr *nh, u32 fl, u32 mask){ u32 ofl; ofl = nh->flow_lbl[0] << 16 | nh->flow_lbl[1] << 8 | nh->flow_lbl[2]; fl = OVS_MASKED(ofl, fl, mask); /* Bits 21-24 are always unmasked, so this retains their values. */ nh->flow_lbl[0] = (u8)(fl >> 16); nh->flow_lbl[1] = (u8)(fl >> 8); nh->flow_lbl[2] = (u8)fl; if (skb->ip_summed == CHECKSUM_COMPLETE) csum_replace(&skb->csum, (__force __wsum)htonl(ofl), (__force __wsum)htonl(fl));} static void set_ipv6_ttl(struct sk_buff *skb, struct ipv6hdr *nh, u8 new_ttl, u8 mask){ new_ttl = OVS_MASKED(nh->hop_limit, new_ttl, mask); if (skb->ip_summed == CHECKSUM_COMPLETE) csum_replace(&skb->csum, (__force __wsum)(nh->hop_limit << 8), (__force __wsum)(new_ttl << 8)); nh->hop_limit = new_ttl;} static void set_ip_ttl(struct sk_buff *skb, struct iphdr *nh, u8 new_ttl, u8 mask){ new_ttl = OVS_MASKED(nh->ttl, new_ttl, mask); csum_replace2(&nh->check, htons(nh->ttl << 8), htons(new_ttl << 8)); nh->ttl = new_ttl;} static int set_ipv4(struct sk_buff *skb, struct sw_flow_key *flow_key, const struct ovs_key_ipv4 *key, const struct ovs_key_ipv4 *mask){ struct iphdr *nh; __be32 new_addr; int err; err = skb_ensure_writable(skb, skb_network_offset(skb) + sizeof(struct iphdr)); if (unlikely(err)) return err; nh = ip_hdr(skb); /* Setting an IP addresses is typically only a side effect of * matching on them in the current userspace implementation, so it * makes sense to check if the value actually changed. */ if (mask->ipv4_src) { new_addr = OVS_MASKED(nh->saddr, key->ipv4_src, mask->ipv4_src); if (unlikely(new_addr != nh->saddr)) { set_ip_addr(skb, nh, &nh->saddr, new_addr); flow_key->ipv4.addr.src = new_addr; } } if (mask->ipv4_dst) { new_addr = OVS_MASKED(nh->daddr, key->ipv4_dst, mask->ipv4_dst); if (unlikely(new_addr != nh->daddr)) { set_ip_addr(skb, nh, &nh->daddr, new_addr); flow_key->ipv4.addr.dst = new_addr; } } if (mask->ipv4_tos) { ipv4_change_dsfield(nh, ~mask->ipv4_tos, key->ipv4_tos); flow_key->ip.tos = nh->tos; } if (mask->ipv4_ttl) { set_ip_ttl(skb, nh, key->ipv4_ttl, mask->ipv4_ttl); flow_key->ip.ttl = nh->ttl; } return 0;} Lines 1–500 of 1601. Load more
// SPDX-License-Identifier: GPL-2.0-only /* * Copyright (c) 2007-2017 Nicira, Inc. */ #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt #include <linux/skbuff.h> #include <linux/in.h> #include <linux/ip.h> #include <linux/openvswitch.h> #include <linux/sctp.h> #include <linux/tcp.h> #include <linux/udp.h> #include <linux/in6.h> #include <linux/if_arp.h> #include <linux/if_vlan.h> #include <net/dst.h> #include <net/gso.h> #include <net/ip.h> #include <net/ipv6.h> #include <net/ip6_fib.h> #include <net/ip6_route.h> #include <net/checksum.h> #include <net/dsfield.h> #include <net/mpls.h> #if IS_ENABLED(CONFIG_PSAMPLE) #include <net/psample.h> #endif #include <net/sctp/checksum.h> #include "datapath.h" #include "drop.h" #include "flow.h" #include "conntrack.h" #include "vport.h" #include "flow_netlink.h" #include "openvswitch_trace.h" struct ovs_pcpu_storage __percpu *ovs_pcpu_storage; /* Make a clone of the 'key', using the pre-allocated percpu 'flow_keys' * space. Return NULL if out of key spaces. */ static struct sw_flow_key *clone_key(const struct sw_flow_key *key_) { struct ovs_pcpu_storage *ovs_pcpu = this_cpu_ptr(ovs_pcpu_storage); struct action_flow_keys *keys = &ovs_pcpu->flow_keys; int level = ovs_pcpu->exec_level; struct sw_flow_key *key = NULL; if (level <= OVS_DEFERRED_ACTION_THRESHOLD) { key = &keys->key[level - 1]; *key = *key_; } return key; } static void action_fifo_init(struct action_fifo *fifo) { fifo->head = 0; fifo->tail = 0; } static bool action_fifo_is_empty(const struct action_fifo *fifo) { return (fifo->head == fifo->tail); } static struct deferred_action *action_fifo_get(struct action_fifo *fifo) { if (action_fifo_is_empty(fifo)) return NULL; return &fifo->fifo[fifo->tail++]; } static struct deferred_action *action_fifo_put(struct action_fifo *fifo) { if (fifo->head >= DEFERRED_ACTION_FIFO_SIZE - 1) return NULL; return &fifo->fifo[fifo->head++]; } /* Return true if fifo is not full */ static struct deferred_action *add_deferred_actions(struct sk_buff *skb, const struct sw_flow_key *key, const struct nlattr *actions, const int actions_len) { struct action_fifo *fifo = this_cpu_ptr(&ovs_pcpu_storage->action_fifos); struct deferred_action *da; da = action_fifo_put(fifo); if (da) { da->skb = skb; da->actions = actions; da->actions_len = actions_len; da->pkt_key = *key; } return da; } static void invalidate_flow_key(struct sw_flow_key *key) { key->mac_proto |= SW_FLOW_KEY_INVALID; } static bool is_flow_key_valid(const struct sw_flow_key *key) { return !(key->mac_proto & SW_FLOW_KEY_INVALID); } static int clone_execute(struct datapath *dp, struct sk_buff *skb, struct sw_flow_key *key, u32 recirc_id, const struct nlattr *actions, int len, bool last, bool clone_flow_key); static int do_execute_actions(struct datapath *dp, struct sk_buff *skb, struct sw_flow_key *key, const struct nlattr *attr, int len); static int push_mpls(struct sk_buff *skb, struct sw_flow_key *key, __be32 mpls_lse, __be16 mpls_ethertype, __u16 mac_len) { int err; err = skb_mpls_push(skb, mpls_lse, mpls_ethertype, mac_len, !!mac_len); if (err) return err; if (!mac_len) key->mac_proto = MAC_PROTO_NONE; invalidate_flow_key(key); return 0; } static int pop_mpls(struct sk_buff *skb, struct sw_flow_key *key, const __be16 ethertype) { int err; err = skb_mpls_pop(skb, ethertype, skb->mac_len, ovs_key_mac_proto(key) == MAC_PROTO_ETHERNET); if (err) return err; if (ethertype == htons(ETH_P_TEB)) key->mac_proto = MAC_PROTO_ETHERNET; invalidate_flow_key(key); return 0; } static int set_mpls(struct sk_buff *skb, struct sw_flow_key *flow_key, const __be32 *mpls_lse, const __be32 *mask) { struct mpls_shim_hdr *stack; __be32 lse; int err; if (!pskb_may_pull(skb, skb_network_offset(skb) + MPLS_HLEN)) return -ENOMEM; stack = mpls_hdr(skb); lse = OVS_MASKED(stack->label_stack_entry, *mpls_lse, *mask); err = skb_mpls_update_lse(skb, lse); if (err) return err; flow_key->mpls.lse[0] = lse; return 0; } static int pop_vlan(struct sk_buff *skb, struct sw_flow_key *key) { int err; err = skb_vlan_pop(skb); if (skb_vlan_tag_present(skb)) { invalidate_flow_key(key); } else { key->eth.vlan.tci = 0; key->eth.vlan.tpid = 0; } return err; } static int push_vlan(struct sk_buff *skb, struct sw_flow_key *key, const struct ovs_action_push_vlan *vlan) { int err; if (skb_vlan_tag_present(skb)) { invalidate_flow_key(key); } else { key->eth.vlan.tci = vlan->vlan_tci; key->eth.vlan.tpid = vlan->vlan_tpid; } err = skb_vlan_push(skb, vlan->vlan_tpid, ntohs(vlan->vlan_tci) & ~VLAN_CFI_MASK); skb_reset_mac_len(skb); return err; } /* 'src' is already properly masked. */ static void ether_addr_copy_masked(u8 *dst_, const u8 *src_, const u8 *mask_) { u16 *dst = (u16 *)dst_; const u16 *src = (const u16 *)src_; const u16 *mask = (const u16 *)mask_; OVS_SET_MASKED(dst[0], src[0], mask[0]); OVS_SET_MASKED(dst[1], src[1], mask[1]); OVS_SET_MASKED(dst[2], src[2], mask[2]); } static int set_eth_addr(struct sk_buff *skb, struct sw_flow_key *flow_key, const struct ovs_key_ethernet *key, const struct ovs_key_ethernet *mask) { int err; err = skb_ensure_writable(skb, ETH_HLEN); if (unlikely(err)) return err; skb_postpull_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2); ether_addr_copy_masked(eth_hdr(skb)->h_source, key->eth_src, mask->eth_src); ether_addr_copy_masked(eth_hdr(skb)->h_dest, key->eth_dst, mask->eth_dst); skb_postpush_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2); ether_addr_copy(flow_key->eth.src, eth_hdr(skb)->h_source); ether_addr_copy(flow_key->eth.dst, eth_hdr(skb)->h_dest); return 0; } /* pop_eth does not support VLAN packets as this action is never called * for them. */ static int pop_eth(struct sk_buff *skb, struct sw_flow_key *key) { int err; err = skb_eth_pop(skb); if (err) return err; /* safe right before invalidate_flow_key */ key->mac_proto = MAC_PROTO_NONE; invalidate_flow_key(key); return 0; } static int push_eth(struct sk_buff *skb, struct sw_flow_key *key, const struct ovs_action_push_eth *ethh) { int err; err = skb_eth_push(skb, ethh->addresses.eth_dst, ethh->addresses.eth_src); if (err) return err; /* safe right before invalidate_flow_key */ key->mac_proto = MAC_PROTO_ETHERNET; invalidate_flow_key(key); return 0; } static noinline_for_stack int push_nsh(struct sk_buff *skb, struct sw_flow_key *key, const struct nlattr *a) { u8 buffer[NSH_HDR_MAX_LEN]; struct nshhdr *nh = (struct nshhdr *)buffer; int err; err = nsh_hdr_from_nlattr(a, nh, NSH_HDR_MAX_LEN); if (err) return err; err = nsh_push(skb, nh); if (err) return err; /* safe right before invalidate_flow_key */ key->mac_proto = MAC_PROTO_NONE; invalidate_flow_key(key); return 0; } static int pop_nsh(struct sk_buff *skb, struct sw_flow_key *key) { int err; err = nsh_pop(skb); if (err) return err; /* safe right before invalidate_flow_key */ if (skb->protocol == htons(ETH_P_TEB)) key->mac_proto = MAC_PROTO_ETHERNET; else key->mac_proto = MAC_PROTO_NONE; invalidate_flow_key(key); return 0; } static void update_ip_l4_checksum(struct sk_buff *skb, struct iphdr *nh, __be32 addr, __be32 new_addr) { int transport_len = skb->len - skb_transport_offset(skb); if (nh->frag_off & htons(IP_OFFSET)) return; if (nh->protocol == IPPROTO_TCP) { if (likely(transport_len >= sizeof(struct tcphdr))) inet_proto_csum_replace4(&tcp_hdr(skb)->check, skb, addr, new_addr, true); } else if (nh->protocol == IPPROTO_UDP) { if (likely(transport_len >= sizeof(struct udphdr))) { struct udphdr *uh = udp_hdr(skb); if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) { inet_proto_csum_replace4(&uh->check, skb, addr, new_addr, true); if (!uh->check) uh->check = CSUM_MANGLED_0; } } } } static void set_ip_addr(struct sk_buff *skb, struct iphdr *nh, __be32 *addr, __be32 new_addr) { update_ip_l4_checksum(skb, nh, *addr, new_addr); csum_replace4(&nh->check, *addr, new_addr); skb_clear_hash(skb); ovs_ct_clear(skb, NULL); *addr = new_addr; } static void update_ipv6_checksum(struct sk_buff *skb, u8 l4_proto, __be32 addr[4], const __be32 new_addr[4]) { int transport_len = skb->len - skb_transport_offset(skb); if (l4_proto == NEXTHDR_TCP) { if (likely(transport_len >= sizeof(struct tcphdr))) inet_proto_csum_replace16(&tcp_hdr(skb)->check, skb, addr, new_addr, true); } else if (l4_proto == NEXTHDR_UDP) { if (likely(transport_len >= sizeof(struct udphdr))) { struct udphdr *uh = udp_hdr(skb); if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) { inet_proto_csum_replace16(&uh->check, skb, addr, new_addr, true); if (!uh->check) uh->check = CSUM_MANGLED_0; } } } else if (l4_proto == NEXTHDR_ICMP) { if (likely(transport_len >= sizeof(struct icmp6hdr))) inet_proto_csum_replace16(&icmp6_hdr(skb)->icmp6_cksum, skb, addr, new_addr, true); } } static void mask_ipv6_addr(const __be32 old[4], const __be32 addr[4], const __be32 mask[4], __be32 masked[4]) { masked[0] = OVS_MASKED(old[0], addr[0], mask[0]); masked[1] = OVS_MASKED(old[1], addr[1], mask[1]); masked[2] = OVS_MASKED(old[2], addr[2], mask[2]); masked[3] = OVS_MASKED(old[3], addr[3], mask[3]); } static void set_ipv6_addr(struct sk_buff *skb, u8 l4_proto, __be32 addr[4], const __be32 new_addr[4], bool recalculate_csum) { if (recalculate_csum) update_ipv6_checksum(skb, l4_proto, addr, new_addr); skb_clear_hash(skb); ovs_ct_clear(skb, NULL); memcpy(addr, new_addr, sizeof(__be32[4])); } static void set_ipv6_dsfield(struct sk_buff *skb, struct ipv6hdr *nh, u8 ipv6_tclass, u8 mask) { u8 old_ipv6_tclass = ipv6_get_dsfield(nh); ipv6_tclass = OVS_MASKED(old_ipv6_tclass, ipv6_tclass, mask); if (skb->ip_summed == CHECKSUM_COMPLETE) csum_replace(&skb->csum, (__force __wsum)(old_ipv6_tclass << 12), (__force __wsum)(ipv6_tclass << 12)); ipv6_change_dsfield(nh, ~mask, ipv6_tclass); } static void set_ipv6_fl(struct sk_buff *skb, struct ipv6hdr *nh, u32 fl, u32 mask) { u32 ofl; ofl = nh->flow_lbl[0] << 16 | nh->flow_lbl[1] << 8 | nh->flow_lbl[2]; fl = OVS_MASKED(ofl, fl, mask); /* Bits 21-24 are always unmasked, so this retains their values. */ nh->flow_lbl[0] = (u8)(fl >> 16); nh->flow_lbl[1] = (u8)(fl >> 8); nh->flow_lbl[2] = (u8)fl; if (skb->ip_summed == CHECKSUM_COMPLETE) csum_replace(&skb->csum, (__force __wsum)htonl(ofl), (__force __wsum)htonl(fl)); } static void set_ipv6_ttl(struct sk_buff *skb, struct ipv6hdr *nh, u8 new_ttl, u8 mask) { new_ttl = OVS_MASKED(nh->hop_limit, new_ttl, mask); if (skb->ip_summed == CHECKSUM_COMPLETE) csum_replace(&skb->csum, (__force __wsum)(nh->hop_limit << 8), (__force __wsum)(new_ttl << 8)); nh->hop_limit = new_ttl; } static void set_ip_ttl(struct sk_buff *skb, struct iphdr *nh, u8 new_ttl, u8 mask) { new_ttl = OVS_MASKED(nh->ttl, new_ttl, mask); csum_replace2(&nh->check, htons(nh->ttl << 8), htons(new_ttl << 8)); nh->ttl = new_ttl; } static int set_ipv4(struct sk_buff *skb, struct sw_flow_key *flow_key, const struct ovs_key_ipv4 *key, const struct ovs_key_ipv4 *mask) { struct iphdr *nh; __be32 new_addr; int err; err = skb_ensure_writable(skb, skb_network_offset(skb) + sizeof(struct iphdr)); if (unlikely(err)) return err; nh = ip_hdr(skb); /* Setting an IP addresses is typically only a side effect of * matching on them in the current userspace implementation, so it * makes sense to check if the value actually changed. */ if (mask->ipv4_src) { new_addr = OVS_MASKED(nh->saddr, key->ipv4_src, mask->ipv4_src); if (unlikely(new_addr != nh->saddr)) { set_ip_addr(skb, nh, &nh->saddr, new_addr); flow_key->ipv4.addr.src = new_addr; } } if (mask->ipv4_dst) { new_addr = OVS_MASKED(nh->daddr, key->ipv4_dst, mask->ipv4_dst); if (unlikely(new_addr != nh->daddr)) { set_ip_addr(skb, nh, &nh->daddr, new_addr); flow_key->ipv4.addr.dst = new_addr; } } if (mask->ipv4_tos) { ipv4_change_dsfield(nh, ~mask->ipv4_tos, key->ipv4_tos); flow_key->ip.tos = nh->tos; } if (mask->ipv4_ttl) { set_ip_ttl(skb, nh, key->ipv4_ttl, mask->ipv4_ttl); flow_key->ip.ttl = nh->ttl; } return 0; }