Suhas N Bhargav | 592e64c | 2021-11-12 16:53:08 +0530 | [diff] [blame^] | 1 | /* |
| 2 | * sfe_ipv4_esp.c |
| 3 | * Shortcut forwarding engine - IPv4 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/ip.h> |
| 23 | #include <linux/etherdevice.h> |
| 24 | #include <linux/lockdep.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_ipv4.h" |
| 31 | #include "sfe_ipv4_esp.h" |
| 32 | |
| 33 | /* |
| 34 | * sfe_ipv4_recv_esp() |
| 35 | * Handle ESP packet receives and forwarding |
| 36 | */ |
| 37 | int sfe_ipv4_recv_esp(struct sfe_ipv4 *si, struct sk_buff *skb, struct net_device *dev, |
| 38 | unsigned int len, struct iphdr *iph, unsigned int ihl, |
| 39 | bool sync_on_find, bool tun_outer) |
| 40 | { |
| 41 | struct sfe_ipv4_connection_match *cm; |
| 42 | struct net_device *xmit_dev; |
| 43 | struct net_protocol *ipprot; |
| 44 | netdev_features_t features; |
| 45 | bool passthrough; |
| 46 | bool bridge_flow; |
| 47 | bool fast_xmit; |
| 48 | bool hw_csum; |
| 49 | __be32 src_ip; |
| 50 | __be32 dest_ip; |
| 51 | bool ret; |
| 52 | u8 ttl; |
| 53 | |
| 54 | /* |
| 55 | * Read the IP address from the iphdr, and set the src/dst ports to 0. |
| 56 | */ |
| 57 | src_ip = iph->saddr; |
| 58 | dest_ip = iph->daddr; |
| 59 | rcu_read_lock(); |
| 60 | |
| 61 | /* |
| 62 | * Look for a connection match. |
| 63 | */ |
| 64 | #ifdef CONFIG_NF_FLOW_COOKIE |
| 65 | cm = si->sfe_flow_cookie_table[skb->flow_cookie & SFE_FLOW_COOKIE_MASK].match; |
| 66 | if (unlikely(!cm)) { |
| 67 | cm = sfe_ipv4_find_ipv4_connection_match_rcu(si, dev, IPPROTO_ESP, src_ip, 0, dest_ip, 0); |
| 68 | } |
| 69 | #else |
| 70 | cm = sfe_ipv4_find_connection_match_rcu(si, dev, IPPROTO_ESP, src_ip, 0, dest_ip, 0); |
| 71 | #endif |
| 72 | if (unlikely(!cm)) { |
| 73 | rcu_read_unlock(); |
| 74 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_ESP_NO_CONNECTION); |
| 75 | DEBUG_TRACE("no connection found for esp packet\n"); |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | /* |
| 80 | * Source interface validate. |
| 81 | */ |
| 82 | if (unlikely((cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_SRC_INTERFACE_CHECK) && (cm->match_dev != dev))) { |
| 83 | struct sfe_ipv4_connection *c = cm->connection; |
| 84 | int ret; |
| 85 | |
| 86 | spin_lock_bh(&si->lock); |
| 87 | ret = sfe_ipv4_remove_connection(si, c); |
| 88 | spin_unlock_bh(&si->lock); |
| 89 | |
| 90 | if (ret) { |
| 91 | sfe_ipv4_flush_connection(si, c, SFE_SYNC_REASON_FLUSH); |
| 92 | } |
| 93 | rcu_read_unlock(); |
| 94 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_INVALID_SRC_IFACE); |
| 95 | DEBUG_TRACE("flush on wrong source interface check failure\n"); |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | passthrough = cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_PASSTHROUGH; |
| 100 | bridge_flow = !!(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_BRIDGE_FLOW); |
| 101 | |
| 102 | /* |
| 103 | * If our packet has been marked as "sync on find" we can't actually |
| 104 | * forward it in the fast path, but now that we've found an associated |
| 105 | * connection we need sync its status before exception it to slow path unless |
| 106 | * it is passthrough (packets not directed to DUT) packet. |
| 107 | * TODO: revisit to ensure that pass through traffic is not bypassing firewall for fragmented cases |
| 108 | */ |
| 109 | if (unlikely(sync_on_find) && !passthrough) { |
| 110 | sfe_ipv4_sync_status(si, cm->connection, SFE_SYNC_REASON_STATS); |
| 111 | rcu_read_unlock(); |
| 112 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_ESP_IP_OPTIONS_OR_INITIAL_FRAGMENT); |
| 113 | DEBUG_TRACE("%px: sfe: sync on find\n", cm); |
| 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 iphdr *)skb->data; |
| 133 | } |
| 134 | |
| 135 | /* |
| 136 | * Enable HW csum if rx checksum is verified and xmit interface is CSUM offload capable. |
| 137 | */ |
| 138 | hw_csum = !!(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_CSUM_OFFLOAD) && (skb->ip_summed == CHECKSUM_UNNECESSARY); |
| 139 | |
| 140 | /* |
| 141 | * proto decap packet. |
| 142 | * Invoke the inet_protocol handler for delivery of the packet. |
| 143 | */ |
| 144 | ipprot = rcu_dereference(cm->proto); |
| 145 | if (likely(ipprot)) { |
| 146 | skb_reset_network_header(skb); |
| 147 | skb_pull(skb, ihl); |
| 148 | skb_reset_transport_header(skb); |
| 149 | xmit_dev = cm->xmit_dev; |
| 150 | skb->dev = xmit_dev; |
| 151 | |
| 152 | ret = ipprot->handler(skb); |
| 153 | if (ret) { |
| 154 | rcu_read_unlock(); |
| 155 | this_cpu_inc(si->stats_pcpu->packets_not_forwarded64); |
| 156 | DEBUG_TRACE("ESP handler returned error %u\n", ret); |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | /* |
| 161 | * Update traffic stats. |
| 162 | */ |
| 163 | atomic_inc(&cm->rx_packet_count); |
| 164 | atomic_add(len, &cm->rx_byte_count); |
| 165 | |
| 166 | rcu_read_unlock(); |
| 167 | this_cpu_inc(si->stats_pcpu->packets_forwarded64); |
| 168 | return 1; |
| 169 | } |
| 170 | |
| 171 | /* |
| 172 | * esp passthrough / ip local out scenarios. |
| 173 | */ |
| 174 | /* |
| 175 | * If our packet is larger than the MTU of the transmit interface then |
| 176 | * we can't forward it easily. |
| 177 | */ |
| 178 | if (unlikely(len > cm->xmit_dev_mtu)) { |
| 179 | sfe_ipv4_sync_status(si, cm->connection, SFE_SYNC_REASON_STATS); |
| 180 | rcu_read_unlock(); |
| 181 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_ESP_NEEDS_FRAGMENTATION); |
| 182 | DEBUG_TRACE("%px: sfe: larger than MTU\n", cm); |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | /* |
| 187 | * need to ensure that TTL is >=2. |
| 188 | */ |
| 189 | ttl = iph->ttl; |
| 190 | if (!bridge_flow && (ttl < 2) && passthrough) { |
| 191 | sfe_ipv4_sync_status(si, cm->connection, SFE_SYNC_REASON_STATS); |
| 192 | rcu_read_unlock(); |
| 193 | |
| 194 | DEBUG_TRACE("%px: sfe: TTL too low\n", skb); |
| 195 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_ESP_SMALL_TTL); |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | /* |
| 200 | * decrement TTL by 1. |
| 201 | */ |
| 202 | iph->ttl = (ttl - (u8)(!bridge_flow && !tun_outer)); |
| 203 | |
| 204 | /* |
| 205 | * Update DSCP |
| 206 | */ |
| 207 | if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_DSCP_REMARK)) { |
| 208 | iph->tos = (iph->tos & SFE_IPV4_DSCP_MASK) | cm->dscp; |
| 209 | } |
| 210 | |
| 211 | /* |
| 212 | * Replace the IP checksum. |
| 213 | */ |
| 214 | if (likely(hw_csum)) { |
| 215 | skb->ip_summed = CHECKSUM_PARTIAL; |
| 216 | } else { |
| 217 | iph->check = sfe_ipv4_gen_ip_csum(iph); |
| 218 | } |
| 219 | |
| 220 | /* |
| 221 | * Update traffic stats. |
| 222 | */ |
| 223 | atomic_inc(&cm->rx_packet_count); |
| 224 | atomic_add(len, &cm->rx_byte_count); |
| 225 | |
| 226 | xmit_dev = cm->xmit_dev; |
| 227 | skb->dev = xmit_dev; |
| 228 | |
| 229 | /* |
| 230 | * write the layer - 2 header. |
| 231 | */ |
| 232 | if (likely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_L2_HDR)) { |
| 233 | if (unlikely(!(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR))) { |
| 234 | dev_hard_header(skb, xmit_dev, ETH_P_IP, cm->xmit_dest_mac, cm->xmit_src_mac, len); |
| 235 | } else { |
| 236 | /* |
| 237 | * For the simple case we write this really fast. |
| 238 | */ |
| 239 | struct ethhdr *eth = (struct ethhdr *)__skb_push(skb, ETH_HLEN); |
| 240 | eth->h_proto = htons(ETH_P_IP); |
| 241 | ether_addr_copy((u8 *)eth->h_dest, (u8 *)cm->xmit_dest_mac); |
| 242 | ether_addr_copy((u8 *)eth->h_source, (u8 *)cm->xmit_src_mac); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | /* |
| 247 | * Update priority of skb |
| 248 | */ |
| 249 | if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_PRIORITY_REMARK)) { |
| 250 | skb->priority = cm->priority; |
| 251 | } |
| 252 | |
| 253 | /* |
| 254 | * Mark outgoing packet. |
| 255 | */ |
| 256 | if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_MARK)) { |
| 257 | skb->mark = cm->mark; |
| 258 | } |
| 259 | |
| 260 | fast_xmit = !!(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_FAST_XMIT); |
| 261 | |
| 262 | rcu_read_unlock(); |
| 263 | this_cpu_inc(si->stats_pcpu->packets_forwarded64); |
| 264 | prefetch(skb_shinfo(skb)); |
| 265 | |
| 266 | /* |
| 267 | * We do per packet condition check before we could fast xmit the |
| 268 | * packet. |
| 269 | */ |
| 270 | if (likely(fast_xmit && dev_fast_xmit(skb, xmit_dev, features))) { |
| 271 | this_cpu_inc(si->stats_pcpu->packets_fast_xmited64); |
| 272 | return 1; |
| 273 | } |
| 274 | |
| 275 | /* |
| 276 | * Mark that this packet has been fast forwarded. |
| 277 | */ |
| 278 | skb->fast_forwarded = 1; |
| 279 | |
| 280 | dev_queue_xmit(skb); |
| 281 | return 1; |
| 282 | } |