Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 1 | /* |
| 2 | * sfe_ipv4_udp.c |
| 3 | * Shortcut forwarding engine - IPv4 UDP implementation |
| 4 | * |
| 5 | * Copyright (c) 2013-2016, 2019-2020, The Linux Foundation. All rights reserved. |
Guduri Prathyusha | 5f27e23 | 2022-01-06 14:39:04 +0530 | [diff] [blame] | 6 | * Copyright (c) 2021-2022 Qualcomm Innovation Center, Inc. All rights reserved. |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 7 | * |
| 8 | * Permission to use, copy, modify, and/or distribute this software for any |
| 9 | * purpose with or without fee is hereby granted, provided that the above |
| 10 | * copyright notice and this permission notice appear in all copies. |
| 11 | * |
| 12 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 13 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 14 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 15 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 16 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 17 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 18 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 19 | */ |
| 20 | |
| 21 | #include <linux/skbuff.h> |
| 22 | #include <net/udp.h> |
| 23 | #include <linux/etherdevice.h> |
| 24 | #include <linux/lockdep.h> |
Amitesh Anand | 63be37d | 2021-12-24 20:51:48 +0530 | [diff] [blame] | 25 | #include <linux/version.h> |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 26 | |
| 27 | #include "sfe_debug.h" |
| 28 | #include "sfe_api.h" |
| 29 | #include "sfe.h" |
| 30 | #include "sfe_flow_cookie.h" |
| 31 | #include "sfe_ipv4.h" |
Guduri Prathyusha | 79a5fee | 2021-11-11 17:59:10 +0530 | [diff] [blame] | 32 | #include "sfe_pppoe.h" |
Wayne Tan | bb7f178 | 2021-12-13 11:16:04 -0800 | [diff] [blame] | 33 | #include "sfe_vlan.h" |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 34 | |
| 35 | /* |
Amitesh Anand | 63be37d | 2021-12-24 20:51:48 +0530 | [diff] [blame] | 36 | * sfe_ipv4_udp_sk_deliver() |
| 37 | * Deliver the packet to the protocol handler registered with Linux. |
| 38 | * To be called under rcu_read_lock() |
| 39 | * Returns: |
| 40 | * 1 if the packet needs to be passed to Linux. |
| 41 | * 0 if the packet is processed successfully. |
| 42 | * -1 if the packet is dropped in SFE. |
| 43 | */ |
| 44 | static int sfe_ipv4_udp_sk_deliver(struct sk_buff *skb, struct sfe_ipv4_connection_match *cm, unsigned int ihl) |
| 45 | { |
| 46 | struct udp_sock *up; |
| 47 | struct sock *sk; |
| 48 | int ret; |
| 49 | int (*encap_rcv)(struct sock *sk, struct sk_buff *skb); |
| 50 | |
| 51 | /* |
| 52 | * Call the decap handler for valid encap_rcv handler. |
| 53 | */ |
| 54 | up = rcu_dereference(cm->up); |
| 55 | encap_rcv = READ_ONCE(up->encap_rcv); |
| 56 | if (!encap_rcv) { |
| 57 | DEBUG_ERROR("%px: sfe: Error: up->encap_rcv is NULL\n", skb); |
| 58 | return 1; |
| 59 | } |
| 60 | |
| 61 | #if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0)) |
| 62 | nf_reset(skb); |
| 63 | #else |
| 64 | nf_reset_ct(skb); |
| 65 | #endif |
| 66 | |
| 67 | skb_pull(skb, ihl); |
| 68 | skb_reset_transport_header(skb); |
| 69 | |
| 70 | /* |
| 71 | * Verify checksum before giving to encap_rcv handler function. |
| 72 | * TODO: The following approach is ignorant for UDPLITE for now. |
| 73 | * Instead, consider calling Linux API to do checksum validation. |
| 74 | */ |
| 75 | if (unlikely(skb->ip_summed != CHECKSUM_UNNECESSARY) && unlikely(skb->ip_summed != CHECKSUM_COMPLETE)) { |
| 76 | skb->csum = inet_compute_pseudo(skb, IPPROTO_UDP); |
| 77 | if (unlikely(__skb_checksum_complete(skb))) { |
| 78 | DEBUG_ERROR("%px: sfe: Invalid udp checksum\n", skb); |
| 79 | kfree_skb(skb); |
| 80 | return -1; |
| 81 | } |
| 82 | DEBUG_TRACE("%px: sfe: udp checksum verified in s/w correctly.\n", skb); |
| 83 | } |
| 84 | |
| 85 | sk = (struct sock *)up; |
| 86 | |
| 87 | /* |
| 88 | * At this point, L4 checksum has already been verified and pkt is going |
| 89 | * to Linux's tunnel decap-handler. Setting ip_summed field to CHECKSUM_NONE, |
| 90 | * to ensure that later packet's inner header checksum is validated correctly. |
| 91 | * TODO: Find the fix to set skb->ip_summed = CHECKSUM_NONE; |
| 92 | */ |
| 93 | |
| 94 | /* |
| 95 | * encap_rcv() returns the following value: |
| 96 | * =0 if skb was successfully passed to the encap |
| 97 | * handler or was discarded by it. |
| 98 | * >0 if skb should be passed on to UDP. |
| 99 | * <0 if skb should be resubmitted as proto -N |
| 100 | */ |
| 101 | ret = encap_rcv(sk, skb); |
| 102 | if (unlikely(ret)) { |
| 103 | /* |
| 104 | * If encap_rcv fails, vxlan driver drops the packet. |
| 105 | * No need to free the skb here. |
| 106 | */ |
| 107 | |
| 108 | DEBUG_ERROR("%px: sfe: udp-decap API return error: %d\n", skb, ret); |
| 109 | return -1; |
| 110 | } |
| 111 | |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | /* |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 116 | * sfe_ipv4_recv_udp() |
| 117 | * Handle UDP packet receives and forwarding. |
| 118 | */ |
| 119 | int sfe_ipv4_recv_udp(struct sfe_ipv4 *si, struct sk_buff *skb, struct net_device *dev, |
Ken Zhu | 88c5815 | 2021-12-09 15:12:06 -0800 | [diff] [blame] | 120 | unsigned int len, struct iphdr *iph, unsigned int ihl, |
| 121 | bool sync_on_find, struct sfe_l2_info *l2_info, bool tun_outer) |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 122 | { |
| 123 | struct udphdr *udph; |
| 124 | __be32 src_ip; |
| 125 | __be32 dest_ip; |
| 126 | __be16 src_port; |
| 127 | __be16 dest_port; |
| 128 | struct sfe_ipv4_connection_match *cm; |
| 129 | u8 ttl; |
| 130 | struct net_device *xmit_dev; |
Ratheesh Kannoth | a3cf0e0 | 2021-12-09 09:44:10 +0530 | [diff] [blame] | 131 | bool hw_csum; |
Amitesh Anand | 63be37d | 2021-12-24 20:51:48 +0530 | [diff] [blame] | 132 | int err; |
Ratheesh Kannoth | 71fc51e | 2022-01-05 10:02:47 +0530 | [diff] [blame] | 133 | bool bridge_flow; |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 134 | |
| 135 | /* |
| 136 | * Is our packet too short to contain a valid UDP header? |
| 137 | */ |
| 138 | if (unlikely(!pskb_may_pull(skb, (sizeof(struct udphdr) + ihl)))) { |
| 139 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_UDP_HEADER_INCOMPLETE); |
Amitesh Anand | 63be37d | 2021-12-24 20:51:48 +0530 | [diff] [blame] | 140 | DEBUG_TRACE("%px: packet too short for UDP header\n", skb); |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | /* |
| 145 | * Read the IP address and port information. Read the IP header data first |
| 146 | * because we've almost certainly got that in the cache. We may not yet have |
| 147 | * the UDP header cached though so allow more time for any prefetching. |
| 148 | */ |
| 149 | src_ip = iph->saddr; |
| 150 | dest_ip = iph->daddr; |
| 151 | |
| 152 | udph = (struct udphdr *)(skb->data + ihl); |
| 153 | src_port = udph->source; |
| 154 | dest_port = udph->dest; |
| 155 | |
| 156 | rcu_read_lock(); |
| 157 | |
| 158 | /* |
| 159 | * Look for a connection match. |
| 160 | */ |
| 161 | #ifdef CONFIG_NF_FLOW_COOKIE |
| 162 | cm = si->sfe_flow_cookie_table[skb->flow_cookie & SFE_FLOW_COOKIE_MASK].match; |
| 163 | if (unlikely(!cm)) { |
| 164 | cm = sfe_ipv4_find_connection_match_rcu(si, dev, IPPROTO_UDP, src_ip, src_port, dest_ip, dest_port); |
| 165 | } |
| 166 | #else |
Amitesh Anand | 63be37d | 2021-12-24 20:51:48 +0530 | [diff] [blame] | 167 | /* |
| 168 | * 5-tuple lookup for UDP flow. |
| 169 | */ |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 170 | cm = sfe_ipv4_find_connection_match_rcu(si, dev, IPPROTO_UDP, src_ip, src_port, dest_ip, dest_port); |
| 171 | #endif |
| 172 | if (unlikely(!cm)) { |
| 173 | |
Amitesh Anand | 63be37d | 2021-12-24 20:51:48 +0530 | [diff] [blame] | 174 | /* |
| 175 | * try a 4-tuple lookup; required for tunnels like vxlan. |
| 176 | */ |
| 177 | cm = sfe_ipv4_find_connection_match_rcu(si, dev, IPPROTO_UDP, src_ip, 0, dest_ip, dest_port); |
| 178 | if (unlikely(!cm)) { |
| 179 | rcu_read_unlock(); |
| 180 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_UDP_NO_CONNECTION); |
| 181 | DEBUG_TRACE("%px: sfe: no connection found in 4-tuple lookup.\n", skb); |
| 182 | return 0; |
| 183 | } |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | /* |
Ken Zhu | 88c5815 | 2021-12-09 15:12:06 -0800 | [diff] [blame] | 187 | * If our packet has beern marked as "sync on find" we can't actually |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 188 | * forward it in the fast path, but now that we've found an associated |
Ken Zhu | 88c5815 | 2021-12-09 15:12:06 -0800 | [diff] [blame] | 189 | * connection we need sync its status before exception it to slow path. |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 190 | */ |
Ken Zhu | 88c5815 | 2021-12-09 15:12:06 -0800 | [diff] [blame] | 191 | if (unlikely(sync_on_find)) { |
| 192 | sfe_ipv4_sync_status(si, cm->connection, SFE_SYNC_REASON_STATS); |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 193 | rcu_read_unlock(); |
| 194 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_UDP_IP_OPTIONS_OR_INITIAL_FRAGMENT); |
Ken Zhu | 88c5815 | 2021-12-09 15:12:06 -0800 | [diff] [blame] | 195 | DEBUG_TRACE("%px: sfe: sync on find\n", cm); |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | #ifdef CONFIG_XFRM |
| 200 | /* |
| 201 | * We can't accelerate the flow on this direction, just let it go |
| 202 | * through the slow path. |
| 203 | */ |
| 204 | if (unlikely(!cm->flow_accel)) { |
| 205 | rcu_read_unlock(); |
| 206 | this_cpu_inc(si->stats_pcpu->packets_not_forwarded64); |
| 207 | return 0; |
| 208 | } |
| 209 | #endif |
| 210 | |
Wayne Tan | bb7f178 | 2021-12-13 11:16:04 -0800 | [diff] [blame] | 211 | /* |
| 212 | * Do we expect an ingress VLAN tag for this flow? |
| 213 | */ |
| 214 | if (unlikely(!sfe_vlan_validate_ingress_tag(skb, cm->ingress_vlan_hdr_cnt, cm->ingress_vlan_hdr, l2_info))) { |
| 215 | rcu_read_unlock(); |
| 216 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_INGRESS_VLAN_TAG_MISMATCH); |
| 217 | DEBUG_TRACE("VLAN tag mismatch. skb=%px\n", skb); |
| 218 | return 0; |
| 219 | } |
| 220 | |
Ratheesh Kannoth | 71fc51e | 2022-01-05 10:02:47 +0530 | [diff] [blame] | 221 | bridge_flow = !!(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_BRIDGE_FLOW); |
| 222 | |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 223 | /* |
| 224 | * Does our TTL allow forwarding? |
| 225 | */ |
Ratheesh Kannoth | 71fc51e | 2022-01-05 10:02:47 +0530 | [diff] [blame] | 226 | if (likely(!bridge_flow)) { |
| 227 | ttl = iph->ttl; |
| 228 | if (unlikely(ttl < 2)) { |
Ken Zhu | 88c5815 | 2021-12-09 15:12:06 -0800 | [diff] [blame] | 229 | sfe_ipv4_sync_status(si, cm->connection, SFE_SYNC_REASON_STATS); |
Ratheesh Kannoth | 71fc51e | 2022-01-05 10:02:47 +0530 | [diff] [blame] | 230 | rcu_read_unlock(); |
| 231 | |
Ken Zhu | 88c5815 | 2021-12-09 15:12:06 -0800 | [diff] [blame] | 232 | DEBUG_TRACE("%px: sfe: TTL too low\n", skb); |
Ratheesh Kannoth | 71fc51e | 2022-01-05 10:02:47 +0530 | [diff] [blame] | 233 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_UDP_SMALL_TTL); |
| 234 | return 0; |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 235 | } |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | /* |
| 239 | * If our packet is larger than the MTU of the transmit interface then |
| 240 | * we can't forward it easily. |
| 241 | */ |
| 242 | if (unlikely(len > cm->xmit_dev_mtu)) { |
Ken Zhu | 88c5815 | 2021-12-09 15:12:06 -0800 | [diff] [blame] | 243 | sfe_ipv4_sync_status(si, cm->connection, SFE_SYNC_REASON_STATS); |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 244 | rcu_read_unlock(); |
| 245 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_UDP_NEEDS_FRAGMENTATION); |
Ken Zhu | 88c5815 | 2021-12-09 15:12:06 -0800 | [diff] [blame] | 246 | DEBUG_TRACE("%px: sfe: larger than MTU\n", cm); |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 247 | return 0; |
| 248 | } |
| 249 | |
| 250 | /* |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 251 | * Check if skb was cloned. If it was, unshare it. Because |
| 252 | * the data area is going to be written in this path and we don't want to |
| 253 | * change the cloned skb's data section. |
| 254 | */ |
| 255 | if (unlikely(skb_cloned(skb))) { |
| 256 | DEBUG_TRACE("%px: skb is a cloned skb\n", skb); |
| 257 | skb = skb_unshare(skb, GFP_ATOMIC); |
| 258 | if (!skb) { |
Amitesh Anand | 63be37d | 2021-12-24 20:51:48 +0530 | [diff] [blame] | 259 | DEBUG_WARN("%px: Failed to unshare the cloned skb\n", skb); |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 260 | rcu_read_unlock(); |
| 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | /* |
| 265 | * Update the iph and udph pointers with the unshared skb's data area. |
| 266 | */ |
| 267 | iph = (struct iphdr *)skb->data; |
| 268 | udph = (struct udphdr *)(skb->data + ihl); |
| 269 | } |
| 270 | |
| 271 | /* |
Guduri Prathyusha | 5f27e23 | 2022-01-06 14:39:04 +0530 | [diff] [blame] | 272 | * For PPPoE packets, match server MAC and session id |
| 273 | */ |
| 274 | if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_DECAP)) { |
| 275 | struct pppoe_hdr *ph; |
| 276 | struct ethhdr *eth; |
| 277 | |
| 278 | if (unlikely(!sfe_l2_parse_flag_check(l2_info, SFE_L2_PARSE_FLAGS_PPPOE_INGRESS))) { |
| 279 | rcu_read_unlock(); |
| 280 | DEBUG_TRACE("%px: PPPoE header not present in packet for PPPoE rule\n", skb); |
| 281 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_INCORRECT_PPPOE_PARSING); |
| 282 | return 0; |
| 283 | } |
| 284 | |
| 285 | ph = (struct pppoe_hdr *)(skb->head + sfe_l2_pppoe_hdr_offset_get(l2_info)); |
| 286 | eth = (struct ethhdr *)(skb->head + sfe_l2_hdr_offset_get(l2_info)); |
| 287 | if (unlikely(cm->pppoe_session_id != ntohs(ph->sid)) || unlikely(!(ether_addr_equal((u8*)cm->pppoe_remote_mac, (u8 *)eth->h_source)))) { |
| 288 | DEBUG_TRACE("%px: PPPoE sessions with session IDs %d and %d or server MACs %pM and %pM did not match\n", |
| 289 | skb, cm->pppoe_session_id, htons(ph->sid), cm->pppoe_remote_mac, eth->h_source); |
| 290 | rcu_read_unlock(); |
| 291 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_INVALID_PPPOE_SESSION); |
| 292 | return 0; |
| 293 | } |
| 294 | skb->protocol = htons(l2_info->protocol); |
| 295 | this_cpu_inc(si->stats_pcpu->pppoe_decap_packets_forwarded64); |
| 296 | |
| 297 | } else if (unlikely(sfe_l2_parse_flag_check(l2_info, SFE_L2_PARSE_FLAGS_PPPOE_INGRESS))) { |
| 298 | |
| 299 | /* |
Guduri Prathyusha | 034d635 | 2022-01-12 16:49:04 +0530 | [diff] [blame^] | 300 | * If packet contains PPPoE header but CME doesn't contain PPPoE flag yet we are exceptioning the packet to linux |
Guduri Prathyusha | 5f27e23 | 2022-01-06 14:39:04 +0530 | [diff] [blame] | 301 | */ |
Guduri Prathyusha | 034d635 | 2022-01-12 16:49:04 +0530 | [diff] [blame^] | 302 | if (unlikely(!(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_BRIDGE_FLOW))) { |
| 303 | rcu_read_unlock(); |
| 304 | DEBUG_TRACE("%px: CME doesn't contain PPPoE flag but packet has PPPoE header\n", skb); |
| 305 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_PPPOE_NOT_SET_IN_CME); |
| 306 | return 0; |
| 307 | |
| 308 | } |
| 309 | |
| 310 | /* |
| 311 | * For bridged flows when packet contains PPPoE header, restore the header back and forward to xmit interface |
| 312 | */ |
| 313 | __skb_push(skb, (sizeof(struct pppoe_hdr) + sizeof(struct sfe_ppp_hdr))); |
| 314 | l2_info->l2_hdr_size -= (sizeof(struct pppoe_hdr) + sizeof(struct sfe_ppp_hdr)); |
| 315 | this_cpu_inc(si->stats_pcpu->pppoe_bridge_packets_forwarded64); |
Guduri Prathyusha | 5f27e23 | 2022-01-06 14:39:04 +0530 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | /* |
Wayne Tan | bb7f178 | 2021-12-13 11:16:04 -0800 | [diff] [blame] | 319 | * Check if skb has enough headroom to write L2 headers |
| 320 | */ |
| 321 | if (unlikely(skb_headroom(skb) < cm->l2_hdr_size)) { |
| 322 | rcu_read_unlock(); |
| 323 | DEBUG_WARN("%px: Not enough headroom: %u\n", skb, skb_headroom(skb)); |
| 324 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_NO_HEADROOM); |
| 325 | return 0; |
| 326 | } |
| 327 | |
| 328 | /* |
Guduri Prathyusha | 5f27e23 | 2022-01-06 14:39:04 +0530 | [diff] [blame] | 329 | * From this point on we're good to modify the packet. |
| 330 | */ |
| 331 | |
| 332 | /* |
Guduri Prathyusha | 79a5fee | 2021-11-11 17:59:10 +0530 | [diff] [blame] | 333 | * For PPPoE flows, add PPPoE header before L2 header is added. |
| 334 | */ |
Guduri Prathyusha | 034d635 | 2022-01-12 16:49:04 +0530 | [diff] [blame^] | 335 | if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_ENCAP)) { |
Wayne Tan | bb7f178 | 2021-12-13 11:16:04 -0800 | [diff] [blame] | 336 | sfe_pppoe_add_header(skb, cm->pppoe_session_id, PPP_IP); |
Guduri Prathyusha | 79a5fee | 2021-11-11 17:59:10 +0530 | [diff] [blame] | 337 | this_cpu_inc(si->stats_pcpu->pppoe_encap_packets_forwarded64); |
| 338 | } |
| 339 | |
| 340 | /* |
Ratheesh Kannoth | a3cf0e0 | 2021-12-09 09:44:10 +0530 | [diff] [blame] | 341 | * Enable HW csum if rx checksum is verified and xmit interface is CSUM offload capable. |
| 342 | * Note: If L4 csum at Rx was found to be incorrect, we (router) should use incremental L4 checksum here |
| 343 | * so that HW does not re-calculate/replace the L4 csum |
| 344 | */ |
| 345 | hw_csum = !!(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_CSUM_OFFLOAD) && (skb->ip_summed == CHECKSUM_UNNECESSARY); |
| 346 | |
| 347 | /* |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 348 | * Do we have to perform translations of the source address/port? |
| 349 | */ |
| 350 | if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC)) { |
| 351 | u16 udp_csum; |
| 352 | |
| 353 | iph->saddr = cm->xlate_src_ip; |
| 354 | udph->source = cm->xlate_src_port; |
| 355 | |
| 356 | /* |
| 357 | * Do we have a non-zero UDP checksum? If we do then we need |
| 358 | * to update it. |
| 359 | */ |
Ratheesh Kannoth | a3cf0e0 | 2021-12-09 09:44:10 +0530 | [diff] [blame] | 360 | if (unlikely(!hw_csum)) { |
| 361 | udp_csum = udph->check; |
| 362 | if (likely(udp_csum)) { |
| 363 | u32 sum; |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 364 | |
Ratheesh Kannoth | a3cf0e0 | 2021-12-09 09:44:10 +0530 | [diff] [blame] | 365 | if (unlikely(skb->ip_summed == CHECKSUM_PARTIAL)) { |
| 366 | sum = udp_csum + cm->xlate_src_partial_csum_adjustment; |
| 367 | } else { |
| 368 | sum = udp_csum + cm->xlate_src_csum_adjustment; |
| 369 | } |
| 370 | |
| 371 | sum = (sum & 0xffff) + (sum >> 16); |
| 372 | udph->check = (u16)sum; |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 373 | } |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 374 | } |
| 375 | } |
| 376 | |
| 377 | /* |
| 378 | * Do we have to perform translations of the destination address/port? |
| 379 | */ |
| 380 | if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST)) { |
| 381 | u16 udp_csum; |
| 382 | |
| 383 | iph->daddr = cm->xlate_dest_ip; |
| 384 | udph->dest = cm->xlate_dest_port; |
| 385 | |
| 386 | /* |
| 387 | * Do we have a non-zero UDP checksum? If we do then we need |
| 388 | * to update it. |
| 389 | */ |
Ratheesh Kannoth | a3cf0e0 | 2021-12-09 09:44:10 +0530 | [diff] [blame] | 390 | if (unlikely(!hw_csum)) { |
| 391 | udp_csum = udph->check; |
| 392 | if (likely(udp_csum)) { |
| 393 | u32 sum; |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 394 | |
Ratheesh Kannoth | a3cf0e0 | 2021-12-09 09:44:10 +0530 | [diff] [blame] | 395 | /* |
| 396 | * TODO: Use a common API for below incremental checksum calculation |
| 397 | * for IPv4/IPv6 UDP/TCP |
| 398 | */ |
| 399 | if (unlikely(skb->ip_summed == CHECKSUM_PARTIAL)) { |
| 400 | sum = udp_csum + cm->xlate_dest_partial_csum_adjustment; |
| 401 | } else { |
| 402 | sum = udp_csum + cm->xlate_dest_csum_adjustment; |
| 403 | } |
| 404 | |
| 405 | sum = (sum & 0xffff) + (sum >> 16); |
| 406 | udph->check = (u16)sum; |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 407 | } |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 408 | } |
| 409 | } |
| 410 | |
| 411 | /* |
Amitesh Anand | 63be37d | 2021-12-24 20:51:48 +0530 | [diff] [blame] | 412 | * UDP sock will be valid only in decap-path. |
| 413 | * Call encap_rcv function associated with udp_sock in cm. |
| 414 | */ |
| 415 | if (unlikely(cm->up)) { |
| 416 | /* |
| 417 | * Call decap handler associated with sock. |
| 418 | * Also validates UDP checksum before calling decap handler. |
| 419 | */ |
| 420 | err = sfe_ipv4_udp_sk_deliver(skb, cm, ihl); |
| 421 | if (unlikely(err == -1)) { |
| 422 | rcu_read_unlock(); |
| 423 | this_cpu_inc(si->stats_pcpu->packets_dropped64); |
| 424 | return 1; |
| 425 | } else if (unlikely(err == 1)) { |
| 426 | rcu_read_unlock(); |
| 427 | this_cpu_inc(si->stats_pcpu->packets_not_forwarded64); |
| 428 | return 0; |
| 429 | } |
| 430 | |
| 431 | /* |
| 432 | * Update traffic stats. |
| 433 | */ |
| 434 | atomic_inc(&cm->rx_packet_count); |
| 435 | atomic_add(len, &cm->rx_byte_count); |
| 436 | |
| 437 | rcu_read_unlock(); |
| 438 | this_cpu_inc(si->stats_pcpu->packets_forwarded64); |
| 439 | DEBUG_TRACE("%px: sfe: sfe_ipv4_recv_udp -> encap_rcv done.\n", skb); |
| 440 | return 1; |
| 441 | } |
| 442 | |
| 443 | /* |
| 444 | * Decrement our TTL |
| 445 | * Except when called from hook function in post-decap. |
| 446 | */ |
Ratheesh Kannoth | 71fc51e | 2022-01-05 10:02:47 +0530 | [diff] [blame] | 447 | if (likely(!bridge_flow)) { |
| 448 | iph->ttl -= (u8)(!tun_outer); |
| 449 | } |
Amitesh Anand | 63be37d | 2021-12-24 20:51:48 +0530 | [diff] [blame] | 450 | |
| 451 | /* |
| 452 | * Update DSCP |
| 453 | */ |
| 454 | if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_DSCP_REMARK)) { |
| 455 | iph->tos = (iph->tos & SFE_IPV4_DSCP_MASK) | cm->dscp; |
| 456 | } |
| 457 | |
| 458 | /* |
Ratheesh Kannoth | a3cf0e0 | 2021-12-09 09:44:10 +0530 | [diff] [blame] | 459 | * If HW checksum offload is not possible, full L3 checksum and incremental L4 checksum |
| 460 | * are used to update the packet. Setting ip_summed to CHECKSUM_UNNECESSARY ensures checksum is |
| 461 | * not recalculated further in packet path. |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 462 | */ |
Ratheesh Kannoth | a3cf0e0 | 2021-12-09 09:44:10 +0530 | [diff] [blame] | 463 | if (likely(hw_csum)) { |
| 464 | skb->ip_summed = CHECKSUM_PARTIAL; |
| 465 | } else { |
| 466 | iph->check = sfe_ipv4_gen_ip_csum(iph); |
Ratheesh Kannoth | a3cf0e0 | 2021-12-09 09:44:10 +0530 | [diff] [blame] | 467 | } |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 468 | |
| 469 | /* |
| 470 | * Update traffic stats. |
| 471 | */ |
| 472 | atomic_inc(&cm->rx_packet_count); |
| 473 | atomic_add(len, &cm->rx_byte_count); |
| 474 | |
| 475 | xmit_dev = cm->xmit_dev; |
| 476 | skb->dev = xmit_dev; |
| 477 | |
| 478 | /* |
Wayne Tan | bb7f178 | 2021-12-13 11:16:04 -0800 | [diff] [blame] | 479 | * Check to see if we need to add VLAN tags |
| 480 | */ |
| 481 | if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_INSERT_EGRESS_VLAN_TAG)) { |
| 482 | sfe_vlan_add_tag(skb, cm->egress_vlan_hdr_cnt, cm->egress_vlan_hdr); |
| 483 | } |
| 484 | |
| 485 | /* |
| 486 | * Check to see if we need to write an Ethernet header. |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 487 | */ |
| 488 | if (likely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_L2_HDR)) { |
| 489 | if (unlikely(!(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR))) { |
Guduri Prathyusha | 5f27e23 | 2022-01-06 14:39:04 +0530 | [diff] [blame] | 490 | dev_hard_header(skb, xmit_dev, ntohs(skb->protocol), |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 491 | cm->xmit_dest_mac, cm->xmit_src_mac, len); |
| 492 | } else { |
| 493 | /* |
| 494 | * For the simple case we write this really fast. |
| 495 | */ |
| 496 | struct ethhdr *eth = (struct ethhdr *)__skb_push(skb, ETH_HLEN); |
Guduri Prathyusha | 5f27e23 | 2022-01-06 14:39:04 +0530 | [diff] [blame] | 497 | eth->h_proto = skb->protocol; |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 498 | ether_addr_copy((u8 *)eth->h_dest, (u8 *)cm->xmit_dest_mac); |
| 499 | ether_addr_copy((u8 *)eth->h_source, (u8 *)cm->xmit_src_mac); |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | /* |
| 504 | * Update priority of skb. |
| 505 | */ |
| 506 | if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_PRIORITY_REMARK)) { |
| 507 | skb->priority = cm->priority; |
| 508 | } |
| 509 | |
| 510 | /* |
| 511 | * Mark outgoing packet. |
| 512 | */ |
Ken Zhu | 37040ea | 2021-09-09 21:11:15 -0700 | [diff] [blame] | 513 | if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_MARK)) { |
| 514 | skb->mark = cm->connection->mark; |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | rcu_read_unlock(); |
| 518 | |
| 519 | this_cpu_inc(si->stats_pcpu->packets_forwarded64); |
| 520 | |
| 521 | /* |
| 522 | * We're going to check for GSO flags when we transmit the packet so |
| 523 | * start fetching the necessary cache line now. |
| 524 | */ |
| 525 | prefetch(skb_shinfo(skb)); |
| 526 | |
| 527 | /* |
| 528 | * Mark that this packet has been fast forwarded. |
| 529 | */ |
| 530 | skb->fast_forwarded = 1; |
| 531 | |
| 532 | /* |
| 533 | * Send the packet on its way. |
| 534 | */ |
| 535 | dev_queue_xmit(skb); |
| 536 | |
| 537 | return 1; |
| 538 | } |