Suhas N Bhargav | 592e64c | 2021-11-12 16:53:08 +0530 | [diff] [blame] | 1 | /* |
| 2 | * sfe_ipv6_esp.c |
| 3 | * Shortcut forwarding engine - IPv6 ESP implementation |
| 4 | * |
| 5 | * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved. |
| 6 | * |
| 7 | * Permission to use, copy, modify, and/or distribute this software for any |
| 8 | * purpose with or without fee is hereby granted, provided that the above |
| 9 | * copyright notice and this permission notice appear in all copies. |
| 10 | * |
| 11 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 12 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 13 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 14 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 15 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 16 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 17 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 18 | */ |
| 19 | |
| 20 | #include <linux/skbuff.h> |
| 21 | #include <net/protocol.h> |
| 22 | #include <net/ip6_checksum.h> |
| 23 | #include <linux/etherdevice.h> |
| 24 | #include <linux/version.h> |
| 25 | |
| 26 | #include "sfe_debug.h" |
| 27 | #include "sfe_api.h" |
| 28 | #include "sfe.h" |
| 29 | #include "sfe_flow_cookie.h" |
| 30 | #include "sfe_ipv6.h" |
| 31 | #include "sfe_ipv6_esp.h" |
| 32 | |
| 33 | /* |
| 34 | * sfe_ipv6_recv_esp() |
| 35 | * Handle ESP packet receives and forwarding |
| 36 | */ |
| 37 | int sfe_ipv6_recv_esp(struct sfe_ipv6 *si, struct sk_buff *skb, struct net_device *dev, |
| 38 | unsigned int len, struct ipv6hdr *iph, unsigned int ihl, |
| 39 | bool sync_on_find, bool tun_outer) |
| 40 | { |
| 41 | struct sfe_ipv6_connection_match *cm; |
| 42 | struct sfe_ipv6_addr *src_ip; |
| 43 | struct sfe_ipv6_addr *dest_ip; |
| 44 | struct net_device *xmit_dev; |
| 45 | struct inet6_protocol *ipprot; |
| 46 | netdev_features_t features; |
| 47 | bool bridge_flow; |
| 48 | bool passthrough; |
| 49 | bool fast_xmit; |
| 50 | bool ret; |
| 51 | |
| 52 | /* |
| 53 | * Read the IP address from the iphdr, and set the src/dst ports to 0. |
| 54 | */ |
| 55 | src_ip = (struct sfe_ipv6_addr *)iph->saddr.s6_addr32; |
| 56 | dest_ip = (struct sfe_ipv6_addr *)iph->daddr.s6_addr32; |
| 57 | rcu_read_lock(); |
| 58 | |
| 59 | /* |
| 60 | * Look for a connection match. |
| 61 | */ |
| 62 | #ifdef CONFIG_NF_FLOW_COOKIE |
| 63 | cm = si->sfe_flow_cookie_table[skb->flow_cookie & SFE_FLOW_COOKIE_MASK].match; |
| 64 | if (unlikely(!cm)) { |
| 65 | cm = sfe_ipv6_find_connection_match_rcu(si, dev, IPPROTO_ESP, src_ip, 0, dest_ip, 0); |
| 66 | } |
| 67 | #else |
| 68 | cm = sfe_ipv6_find_connection_match_rcu(si, dev, IPPROTO_ESP, src_ip, 0, dest_ip, 0); |
| 69 | #endif |
| 70 | if (unlikely(!cm)) { |
| 71 | rcu_read_unlock(); |
| 72 | sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_ESP_NO_CONNECTION); |
| 73 | |
| 74 | DEBUG_TRACE("no connection found for esp packet\n"); |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | * Source interface validate. |
| 80 | */ |
| 81 | if (unlikely((cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_SRC_INTERFACE_CHECK) && (cm->match_dev != dev))) { |
| 82 | struct sfe_ipv6_connection *c = cm->connection; |
| 83 | int ret; |
| 84 | |
| 85 | spin_lock_bh(&si->lock); |
| 86 | ret = sfe_ipv6_remove_connection(si, c); |
| 87 | spin_unlock_bh(&si->lock); |
| 88 | |
| 89 | if (ret) { |
| 90 | sfe_ipv6_flush_connection(si, c, SFE_SYNC_REASON_FLUSH); |
| 91 | } |
| 92 | rcu_read_unlock(); |
| 93 | sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_INVALID_SRC_IFACE); |
| 94 | DEBUG_TRACE("flush on wrong source interface check failure\n"); |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | passthrough = cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_PASSTHROUGH; |
| 99 | bridge_flow = !!(cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_BRIDGE_FLOW); |
| 100 | |
| 101 | /* |
| 102 | * If our packet has beern marked as "sync on find" we can't actually |
| 103 | * forward it in the fast path, but now that we've found an associated |
| 104 | * connection we need sync its status before exception it to slow path. unless |
| 105 | * it is passthrough packet. |
| 106 | * TODO: revisit to ensure that pass through traffic is not bypassing firewall for fragmented cases |
| 107 | */ |
| 108 | if (unlikely(sync_on_find) && !passthrough) { |
| 109 | sfe_ipv6_sync_status(si, cm->connection, SFE_SYNC_REASON_STATS); |
| 110 | rcu_read_unlock(); |
| 111 | |
| 112 | sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_ESP_IP_OPTIONS_OR_INITIAL_FRAGMENT); |
| 113 | DEBUG_TRACE("Sync on find\n"); |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | /* |
| 118 | * Check if skb was cloned. If it was, unshare it. |
| 119 | */ |
| 120 | if (unlikely(skb_cloned(skb))) { |
| 121 | DEBUG_TRACE("%px: skb is a cloned skb\n", skb); |
| 122 | skb = skb_unshare(skb, GFP_ATOMIC); |
| 123 | if (!skb) { |
| 124 | DEBUG_WARN("Failed to unshare the cloned skb\n"); |
| 125 | rcu_read_unlock(); |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | /* |
| 130 | * Update the iphdr pointer with the unshared skb's data area. |
| 131 | */ |
| 132 | iph = (struct ipv6hdr *)skb->data; |
| 133 | } |
| 134 | |
| 135 | /* |
| 136 | * proto decap packet. |
| 137 | * Invoke the inet_protocol handler for delivery of the packet. |
| 138 | */ |
| 139 | ipprot = rcu_dereference(cm->proto); |
| 140 | if (likely(ipprot)) { |
| 141 | skb_reset_network_header(skb); |
| 142 | skb_pull(skb, ihl); |
| 143 | skb_reset_transport_header(skb); |
| 144 | xmit_dev = cm->xmit_dev; |
| 145 | skb->dev = xmit_dev; |
| 146 | |
| 147 | ret = ipprot->handler(skb); |
| 148 | if (ret) { |
| 149 | rcu_read_unlock(); |
| 150 | this_cpu_inc(si->stats_pcpu->packets_not_forwarded64); |
| 151 | DEBUG_TRACE("ESP handler returned error %u\n", ret); |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | rcu_read_unlock(); |
| 156 | this_cpu_inc(si->stats_pcpu->packets_forwarded64); |
| 157 | return 1; |
| 158 | } |
| 159 | |
| 160 | /* |
| 161 | * esp passthrough / ip local out scenarios |
| 162 | */ |
| 163 | /* |
| 164 | * If our packet is larger than the MTU of the transmit interface then |
| 165 | * we can't forward it easily. |
| 166 | */ |
| 167 | if (unlikely(len > cm->xmit_dev_mtu)) { |
| 168 | sfe_ipv6_sync_status(si, cm->connection, SFE_SYNC_REASON_STATS); |
| 169 | rcu_read_unlock(); |
| 170 | |
| 171 | sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_ESP_NEEDS_FRAGMENTATION); |
| 172 | DEBUG_TRACE("Larger than MTU\n"); |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | /* |
| 177 | * need to ensure that TTL is >=2. |
| 178 | */ |
| 179 | if (!bridge_flow && (iph->hop_limit < 2) && passthrough) { |
| 180 | sfe_ipv6_sync_status(si, cm->connection, SFE_SYNC_REASON_STATS); |
| 181 | rcu_read_unlock(); |
| 182 | |
| 183 | sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_ESP_SMALL_TTL); |
| 184 | DEBUG_TRACE("hop_limit too low\n"); |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | /* |
| 189 | * decrement TTL by 1. |
| 190 | */ |
| 191 | iph->hop_limit = iph->hop_limit - (u8)(!bridge_flow && !tun_outer); |
| 192 | |
| 193 | /* |
| 194 | * Update DSCP |
| 195 | */ |
| 196 | if (unlikely(cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_DSCP_REMARK)) { |
| 197 | sfe_ipv6_change_dsfield(iph, cm->dscp); |
| 198 | } |
| 199 | |
| 200 | /* |
| 201 | * Update traffic stats. |
| 202 | */ |
| 203 | atomic_inc(&cm->rx_packet_count); |
| 204 | atomic_add(len, &cm->rx_byte_count); |
| 205 | |
| 206 | xmit_dev = cm->xmit_dev; |
| 207 | skb->dev = xmit_dev; |
| 208 | |
| 209 | /* |
| 210 | * write the layer - 2 header. |
| 211 | */ |
| 212 | if (likely(cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_WRITE_L2_HDR)) { |
| 213 | if (unlikely(!(cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR))) { |
| 214 | dev_hard_header(skb, xmit_dev, ETH_P_IPV6, cm->xmit_dest_mac, cm->xmit_src_mac, len); |
| 215 | } else { |
| 216 | /* |
| 217 | * For the simple case we write this really fast. |
| 218 | */ |
| 219 | struct ethhdr *eth = (struct ethhdr *)__skb_push(skb, ETH_HLEN); |
| 220 | eth->h_proto = htons(ETH_P_IPV6); |
| 221 | ether_addr_copy((u8 *)eth->h_dest, (u8 *)cm->xmit_dest_mac); |
| 222 | ether_addr_copy((u8 *)eth->h_source, (u8 *)cm->xmit_src_mac); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | /* |
| 227 | * Update priority of skb. |
| 228 | */ |
| 229 | if (unlikely(cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_PRIORITY_REMARK)) { |
| 230 | skb->priority = cm->priority; |
| 231 | } |
| 232 | |
| 233 | /* |
| 234 | * Mark outgoing packet. |
| 235 | */ |
| 236 | if (unlikely(cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_MARK)) { |
| 237 | skb->mark = cm->mark; |
| 238 | } |
| 239 | |
| 240 | /* |
| 241 | * For the first packets, check if it could got fast xmit. |
| 242 | */ |
| 243 | if (unlikely(!(cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_FAST_XMIT_FLOW_CHECKED) |
| 244 | && (cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_FAST_XMIT_DEV_ADMISSION))){ |
| 245 | cm->features = netif_skb_features(skb); |
| 246 | if (likely(sfe_fast_xmit_check(skb, cm->features))) { |
| 247 | cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_FAST_XMIT; |
| 248 | } |
| 249 | cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_FAST_XMIT_FLOW_CHECKED; |
| 250 | } |
| 251 | |
| 252 | features = cm->features; |
| 253 | fast_xmit = !!(cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_FAST_XMIT); |
| 254 | |
| 255 | rcu_read_unlock(); |
| 256 | this_cpu_inc(si->stats_pcpu->packets_forwarded64); |
| 257 | prefetch(skb_shinfo(skb)); |
| 258 | |
| 259 | /* |
| 260 | * We do per packet condition check before we could fast xmit the |
| 261 | * packet. |
| 262 | */ |
| 263 | if (likely(fast_xmit && dev_fast_xmit(skb, xmit_dev, features))) { |
| 264 | this_cpu_inc(si->stats_pcpu->packets_fast_xmited64); |
| 265 | return 1; |
| 266 | } |
| 267 | |
| 268 | /* |
| 269 | * Mark that this packet has been fast forwarded. |
| 270 | */ |
| 271 | skb->fast_forwarded = 1; |
| 272 | |
| 273 | dev_queue_xmit(skb); |
| 274 | return 1; |
| 275 | } |