Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 1 | /* |
| 2 | * sfe_ipv4_tcp.c |
| 3 | * Shortcut forwarding engine - IPv4 TCP 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/tcp.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" |
Guduri Prathyusha | 79a5fee | 2021-11-11 17:59:10 +0530 | [diff] [blame] | 31 | #include "sfe_pppoe.h" |
Wayne Tan | bb7f178 | 2021-12-13 11:16:04 -0800 | [diff] [blame] | 32 | #include "sfe_vlan.h" |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 33 | |
| 34 | /* |
| 35 | * sfe_ipv4_process_tcp_option_sack() |
| 36 | * Parse TCP SACK option and update ack according |
| 37 | */ |
| 38 | static bool sfe_ipv4_process_tcp_option_sack(const struct tcphdr *th, const u32 data_offs, |
| 39 | u32 *ack) |
| 40 | { |
| 41 | u32 length = sizeof(struct tcphdr); |
| 42 | u8 *ptr = (u8 *)th + length; |
| 43 | |
| 44 | /* |
| 45 | * Ignore processing if TCP packet has only TIMESTAMP option. |
| 46 | */ |
| 47 | if (likely(data_offs == length + TCPOLEN_TIMESTAMP + 1 + 1) |
| 48 | && likely(ptr[0] == TCPOPT_NOP) |
| 49 | && likely(ptr[1] == TCPOPT_NOP) |
| 50 | && likely(ptr[2] == TCPOPT_TIMESTAMP) |
| 51 | && likely(ptr[3] == TCPOLEN_TIMESTAMP)) { |
| 52 | return true; |
| 53 | } |
| 54 | |
| 55 | /* |
| 56 | * TCP options. Parse SACK option. |
| 57 | */ |
| 58 | while (length < data_offs) { |
| 59 | u8 size; |
| 60 | u8 kind; |
| 61 | |
| 62 | ptr = (u8 *)th + length; |
| 63 | kind = *ptr; |
| 64 | |
| 65 | /* |
| 66 | * NOP, for padding |
| 67 | * Not in the switch because to fast escape and to not calculate size |
| 68 | */ |
| 69 | if (kind == TCPOPT_NOP) { |
| 70 | length++; |
| 71 | continue; |
| 72 | } |
| 73 | |
| 74 | if (kind == TCPOPT_SACK) { |
| 75 | u32 sack = 0; |
| 76 | u8 re = 1 + 1; |
| 77 | |
| 78 | size = *(ptr + 1); |
| 79 | if ((size < (1 + 1 + TCPOLEN_SACK_PERBLOCK)) |
| 80 | || ((size - (1 + 1)) % (TCPOLEN_SACK_PERBLOCK)) |
| 81 | || (size > (data_offs - length))) { |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | re += 4; |
| 86 | while (re < size) { |
| 87 | u32 sack_re; |
| 88 | u8 *sptr = ptr + re; |
| 89 | sack_re = (sptr[0] << 24) | (sptr[1] << 16) | (sptr[2] << 8) | sptr[3]; |
| 90 | if (sack_re > sack) { |
| 91 | sack = sack_re; |
| 92 | } |
| 93 | re += TCPOLEN_SACK_PERBLOCK; |
| 94 | } |
| 95 | if (sack > *ack) { |
| 96 | *ack = sack; |
| 97 | } |
| 98 | length += size; |
| 99 | continue; |
| 100 | } |
| 101 | if (kind == TCPOPT_EOL) { |
| 102 | return true; |
| 103 | } |
| 104 | size = *(ptr + 1); |
| 105 | if (size < 2) { |
| 106 | return false; |
| 107 | } |
| 108 | length += size; |
| 109 | } |
| 110 | |
| 111 | return true; |
| 112 | } |
| 113 | |
| 114 | /* |
| 115 | * sfe_ipv4_recv_tcp() |
| 116 | * Handle TCP packet receives and forwarding. |
| 117 | */ |
| 118 | int sfe_ipv4_recv_tcp(struct sfe_ipv4 *si, struct sk_buff *skb, struct net_device *dev, |
Ken Zhu | 88c5815 | 2021-12-09 15:12:06 -0800 | [diff] [blame] | 119 | unsigned int len, struct iphdr *iph, unsigned int ihl, bool sync_on_find, struct sfe_l2_info *l2_info) |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 120 | { |
| 121 | struct tcphdr *tcph; |
| 122 | __be32 src_ip; |
| 123 | __be32 dest_ip; |
| 124 | __be16 src_port; |
| 125 | __be16 dest_port; |
| 126 | struct sfe_ipv4_connection_match *cm; |
| 127 | struct sfe_ipv4_connection_match *counter_cm; |
| 128 | u8 ttl; |
| 129 | u32 flags; |
| 130 | struct net_device *xmit_dev; |
| 131 | bool ret; |
Ratheesh Kannoth | a3cf0e0 | 2021-12-09 09:44:10 +0530 | [diff] [blame] | 132 | bool hw_csum; |
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 | /* |
Wayne Tan | bb7f178 | 2021-12-13 11:16:04 -0800 | [diff] [blame] | 136 | * Is our packet too short to contain a valid TCP header? |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 137 | */ |
| 138 | if (unlikely(!pskb_may_pull(skb, (sizeof(struct tcphdr) + ihl)))) { |
| 139 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_TCP_HEADER_INCOMPLETE); |
| 140 | DEBUG_TRACE("packet too short for TCP header\n"); |
| 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 TCP header cached though so allow more time for any prefetching. |
| 148 | */ |
| 149 | src_ip = iph->saddr; |
| 150 | dest_ip = iph->daddr; |
| 151 | |
| 152 | tcph = (struct tcphdr *)(skb->data + ihl); |
| 153 | src_port = tcph->source; |
| 154 | dest_port = tcph->dest; |
| 155 | flags = tcp_flag_word(tcph); |
| 156 | |
| 157 | rcu_read_lock(); |
| 158 | |
| 159 | /* |
| 160 | * Look for a connection match. |
| 161 | */ |
| 162 | #ifdef CONFIG_NF_FLOW_COOKIE |
| 163 | cm = si->sfe_flow_cookie_table[skb->flow_cookie & SFE_FLOW_COOKIE_MASK].match; |
| 164 | if (unlikely(!cm)) { |
| 165 | cm = sfe_ipv4_find_connection_match_rcu(si, dev, IPPROTO_TCP, src_ip, src_port, dest_ip, dest_port); |
| 166 | } |
| 167 | #else |
| 168 | cm = sfe_ipv4_find_connection_match_rcu(si, dev, IPPROTO_TCP, src_ip, src_port, dest_ip, dest_port); |
| 169 | #endif |
| 170 | if (unlikely(!cm)) { |
| 171 | /* |
| 172 | * We didn't get a connection but as TCP is connection-oriented that |
| 173 | * may be because this is a non-fast connection (not running established). |
| 174 | * For diagnostic purposes we differentiate this here. |
| 175 | */ |
| 176 | if (likely((flags & (TCP_FLAG_SYN | TCP_FLAG_RST | TCP_FLAG_FIN | TCP_FLAG_ACK)) == TCP_FLAG_ACK)) { |
| 177 | |
| 178 | rcu_read_unlock(); |
| 179 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_TCP_NO_CONNECTION_FAST_FLAGS); |
| 180 | DEBUG_TRACE("no connection found - fast flags\n"); |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | rcu_read_unlock(); |
| 185 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_TCP_NO_CONNECTION_SLOW_FLAGS); |
| 186 | DEBUG_TRACE("no connection found - slow flags: 0x%x\n", |
| 187 | flags & (TCP_FLAG_SYN | TCP_FLAG_RST | TCP_FLAG_FIN | TCP_FLAG_ACK)); |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | /* |
Ratheesh Kannoth | 5dee377 | 2022-01-18 11:27:14 +0530 | [diff] [blame] | 192 | * Source interface validate. |
| 193 | */ |
| 194 | if (unlikely((cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_SRC_INTERFACE_CHECK) && (cm->match_dev != dev))) { |
| 195 | struct sfe_ipv4_connection *c = cm->connection; |
| 196 | spin_lock_bh(&si->lock); |
| 197 | ret = sfe_ipv4_remove_connection(si, c); |
| 198 | spin_unlock_bh(&si->lock); |
| 199 | |
| 200 | if (ret) { |
| 201 | sfe_ipv4_flush_connection(si, c, SFE_SYNC_REASON_FLUSH); |
| 202 | } |
| 203 | rcu_read_unlock(); |
| 204 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_INVALID_SRC_IFACE); |
| 205 | DEBUG_TRACE("flush on wrong source interface check failure\n"); |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | /* |
| 210 | * If our packet has beern marked as "flush on find" we can't actually |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 211 | * 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] | 212 | * connection we need sync its status before throw it slow path. |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 213 | */ |
Ken Zhu | 88c5815 | 2021-12-09 15:12:06 -0800 | [diff] [blame] | 214 | if (unlikely(sync_on_find)) { |
| 215 | sfe_ipv4_sync_status(si, cm->connection, SFE_SYNC_REASON_STATS); |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 216 | rcu_read_unlock(); |
| 217 | |
| 218 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_TCP_IP_OPTIONS_OR_INITIAL_FRAGMENT); |
Ken Zhu | 88c5815 | 2021-12-09 15:12:06 -0800 | [diff] [blame] | 219 | DEBUG_TRACE("Sync on find\n"); |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | #ifdef CONFIG_XFRM |
| 224 | /* |
| 225 | * We can't accelerate the flow on this direction, just let it go |
| 226 | * through the slow path. |
| 227 | */ |
| 228 | if (unlikely(!cm->flow_accel)) { |
| 229 | rcu_read_unlock(); |
| 230 | this_cpu_inc(si->stats_pcpu->packets_not_forwarded64); |
| 231 | return 0; |
| 232 | } |
| 233 | #endif |
Ratheesh Kannoth | 71fc51e | 2022-01-05 10:02:47 +0530 | [diff] [blame] | 234 | |
Wayne Tan | bb7f178 | 2021-12-13 11:16:04 -0800 | [diff] [blame] | 235 | /* |
| 236 | * Do we expect an ingress VLAN tag for this flow? |
| 237 | */ |
| 238 | if (unlikely(!sfe_vlan_validate_ingress_tag(skb, cm->ingress_vlan_hdr_cnt, cm->ingress_vlan_hdr, l2_info))) { |
| 239 | rcu_read_unlock(); |
| 240 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_INGRESS_VLAN_TAG_MISMATCH); |
| 241 | DEBUG_TRACE("VLAN tag mismatch. skb=%px\n", skb); |
| 242 | return 0; |
| 243 | } |
| 244 | |
Ratheesh Kannoth | 71fc51e | 2022-01-05 10:02:47 +0530 | [diff] [blame] | 245 | bridge_flow = !!(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_BRIDGE_FLOW); |
| 246 | |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 247 | /* |
| 248 | * Does our TTL allow forwarding? |
| 249 | */ |
Ratheesh Kannoth | 71fc51e | 2022-01-05 10:02:47 +0530 | [diff] [blame] | 250 | if (likely(!bridge_flow)) { |
| 251 | ttl = iph->ttl; |
| 252 | if (unlikely(ttl < 2)) { |
Ken Zhu | 88c5815 | 2021-12-09 15:12:06 -0800 | [diff] [blame] | 253 | sfe_ipv4_sync_status(si, cm->connection, SFE_SYNC_REASON_STATS); |
Ratheesh Kannoth | 71fc51e | 2022-01-05 10:02:47 +0530 | [diff] [blame] | 254 | rcu_read_unlock(); |
Ken Zhu | 88c5815 | 2021-12-09 15:12:06 -0800 | [diff] [blame] | 255 | |
Ratheesh Kannoth | 71fc51e | 2022-01-05 10:02:47 +0530 | [diff] [blame] | 256 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_TCP_SMALL_TTL); |
Ken Zhu | 88c5815 | 2021-12-09 15:12:06 -0800 | [diff] [blame] | 257 | DEBUG_TRACE("TTL too low\n"); |
Ratheesh Kannoth | 71fc51e | 2022-01-05 10:02:47 +0530 | [diff] [blame] | 258 | return 0; |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 259 | } |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | /* |
| 263 | * If our packet is larger than the MTU of the transmit interface then |
| 264 | * we can't forward it easily. |
| 265 | */ |
| 266 | if (unlikely((len > cm->xmit_dev_mtu) && !skb_is_gso(skb))) { |
Ken Zhu | 88c5815 | 2021-12-09 15:12:06 -0800 | [diff] [blame] | 267 | sfe_ipv4_sync_status(si, cm->connection, SFE_SYNC_REASON_STATS); |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 268 | rcu_read_unlock(); |
Ken Zhu | 88c5815 | 2021-12-09 15:12:06 -0800 | [diff] [blame] | 269 | |
Wayne Tan | bb7f178 | 2021-12-13 11:16:04 -0800 | [diff] [blame] | 270 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_TCP_IP_OPTIONS_OR_INITIAL_FRAGMENT); |
Ken Zhu | 88c5815 | 2021-12-09 15:12:06 -0800 | [diff] [blame] | 271 | DEBUG_TRACE("Larger than MTU\n"); |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 272 | return 0; |
| 273 | } |
| 274 | |
| 275 | /* |
| 276 | * Look at our TCP flags. Anything missing an ACK or that has RST, SYN or FIN |
| 277 | * set is not a fast path packet. |
| 278 | */ |
| 279 | if (unlikely((flags & (TCP_FLAG_SYN | TCP_FLAG_RST | TCP_FLAG_FIN | TCP_FLAG_ACK)) != TCP_FLAG_ACK)) { |
| 280 | struct sfe_ipv4_connection *c = cm->connection; |
| 281 | spin_lock_bh(&si->lock); |
| 282 | ret = sfe_ipv4_remove_connection(si, c); |
| 283 | spin_unlock_bh(&si->lock); |
| 284 | |
| 285 | DEBUG_TRACE("TCP flags: 0x%x are not fast\n", |
| 286 | flags & (TCP_FLAG_SYN | TCP_FLAG_RST | TCP_FLAG_FIN | TCP_FLAG_ACK)); |
| 287 | if (ret) { |
| 288 | sfe_ipv4_flush_connection(si, c, SFE_SYNC_REASON_FLUSH); |
| 289 | } |
| 290 | rcu_read_unlock(); |
| 291 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_TCP_FLAGS); |
| 292 | return 0; |
| 293 | } |
| 294 | |
| 295 | counter_cm = cm->counter_match; |
| 296 | |
| 297 | /* |
| 298 | * Are we doing sequence number checking? |
| 299 | */ |
| 300 | if (likely(!(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK))) { |
| 301 | u32 seq; |
| 302 | u32 ack; |
| 303 | u32 sack; |
| 304 | u32 data_offs; |
| 305 | u32 end; |
| 306 | u32 left_edge; |
| 307 | u32 scaled_win; |
| 308 | u32 max_end; |
| 309 | |
| 310 | /* |
| 311 | * Is our sequence fully past the right hand edge of the window? |
| 312 | */ |
| 313 | seq = ntohl(tcph->seq); |
| 314 | if (unlikely((s32)(seq - (cm->protocol_state.tcp.max_end + 1)) > 0)) { |
| 315 | struct sfe_ipv4_connection *c = cm->connection; |
| 316 | spin_lock_bh(&si->lock); |
| 317 | ret = sfe_ipv4_remove_connection(si, c); |
| 318 | spin_unlock_bh(&si->lock); |
| 319 | |
| 320 | DEBUG_TRACE("seq: %u exceeds right edge: %u\n", |
| 321 | seq, cm->protocol_state.tcp.max_end + 1); |
| 322 | if (ret) { |
| 323 | sfe_ipv4_flush_connection(si, c, SFE_SYNC_REASON_FLUSH); |
| 324 | } |
| 325 | rcu_read_unlock(); |
| 326 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_TCP_SEQ_EXCEEDS_RIGHT_EDGE); |
| 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | /* |
| 331 | * Check that our TCP data offset isn't too short. |
| 332 | */ |
| 333 | data_offs = tcph->doff << 2; |
| 334 | if (unlikely(data_offs < sizeof(struct tcphdr))) { |
| 335 | struct sfe_ipv4_connection *c = cm->connection; |
| 336 | spin_lock_bh(&si->lock); |
| 337 | ret = sfe_ipv4_remove_connection(si, c); |
| 338 | spin_unlock_bh(&si->lock); |
| 339 | |
| 340 | DEBUG_TRACE("TCP data offset: %u, too small\n", data_offs); |
| 341 | if (ret) { |
| 342 | sfe_ipv4_flush_connection(si, c, SFE_SYNC_REASON_FLUSH); |
| 343 | } |
| 344 | rcu_read_unlock(); |
| 345 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_TCP_SMALL_DATA_OFFS); |
| 346 | return 0; |
| 347 | } |
| 348 | |
| 349 | /* |
| 350 | * Update ACK according to any SACK option. |
| 351 | */ |
| 352 | ack = ntohl(tcph->ack_seq); |
| 353 | sack = ack; |
| 354 | if (unlikely(!sfe_ipv4_process_tcp_option_sack(tcph, data_offs, &sack))) { |
| 355 | struct sfe_ipv4_connection *c = cm->connection; |
| 356 | spin_lock_bh(&si->lock); |
| 357 | ret = sfe_ipv4_remove_connection(si, c); |
| 358 | spin_unlock_bh(&si->lock); |
| 359 | |
| 360 | DEBUG_TRACE("TCP option SACK size is wrong\n"); |
| 361 | if (ret) { |
| 362 | sfe_ipv4_flush_connection(si, c, SFE_SYNC_REASON_FLUSH); |
| 363 | } |
| 364 | rcu_read_unlock(); |
| 365 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_TCP_BAD_SACK); |
| 366 | return 0; |
| 367 | } |
| 368 | |
| 369 | /* |
| 370 | * Check that our TCP data offset isn't past the end of the packet. |
| 371 | */ |
| 372 | data_offs += sizeof(struct iphdr); |
| 373 | if (unlikely(len < data_offs)) { |
| 374 | struct sfe_ipv4_connection *c = cm->connection; |
| 375 | spin_lock_bh(&si->lock); |
| 376 | ret = sfe_ipv4_remove_connection(si, c); |
| 377 | spin_unlock_bh(&si->lock); |
| 378 | |
| 379 | DEBUG_TRACE("TCP data offset: %u, past end of packet: %u\n", |
| 380 | data_offs, len); |
| 381 | if (ret) { |
| 382 | sfe_ipv4_flush_connection(si, c, SFE_SYNC_REASON_FLUSH); |
| 383 | } |
| 384 | rcu_read_unlock(); |
| 385 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_TCP_BIG_DATA_OFFS); |
| 386 | return 0; |
| 387 | } |
| 388 | |
| 389 | end = seq + len - data_offs; |
| 390 | |
| 391 | /* |
| 392 | * Is our sequence fully before the left hand edge of the window? |
| 393 | */ |
| 394 | if (unlikely((s32)(end - (cm->protocol_state.tcp.end |
| 395 | - counter_cm->protocol_state.tcp.max_win - 1)) < 0)) { |
| 396 | struct sfe_ipv4_connection *c = cm->connection; |
| 397 | spin_lock_bh(&si->lock); |
| 398 | ret = sfe_ipv4_remove_connection(si, c); |
| 399 | spin_unlock_bh(&si->lock); |
| 400 | |
| 401 | DEBUG_TRACE("seq: %u before left edge: %u\n", |
| 402 | end, cm->protocol_state.tcp.end - counter_cm->protocol_state.tcp.max_win - 1); |
| 403 | if (ret) { |
| 404 | sfe_ipv4_flush_connection(si, c, SFE_SYNC_REASON_FLUSH); |
| 405 | } |
| 406 | rcu_read_unlock(); |
| 407 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_TCP_SEQ_BEFORE_LEFT_EDGE); |
| 408 | return 0; |
| 409 | } |
| 410 | |
| 411 | /* |
| 412 | * Are we acking data that is to the right of what has been sent? |
| 413 | */ |
| 414 | if (unlikely((s32)(sack - (counter_cm->protocol_state.tcp.end + 1)) > 0)) { |
| 415 | struct sfe_ipv4_connection *c = cm->connection; |
| 416 | spin_lock_bh(&si->lock); |
| 417 | ret = sfe_ipv4_remove_connection(si, c); |
| 418 | spin_unlock_bh(&si->lock); |
| 419 | |
| 420 | DEBUG_TRACE("ack: %u exceeds right edge: %u\n", |
| 421 | sack, counter_cm->protocol_state.tcp.end + 1); |
| 422 | if (ret) { |
| 423 | sfe_ipv4_flush_connection(si, c, SFE_SYNC_REASON_FLUSH); |
| 424 | } |
| 425 | rcu_read_unlock(); |
| 426 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_TCP_ACK_EXCEEDS_RIGHT_EDGE); |
| 427 | return 0; |
| 428 | } |
| 429 | |
| 430 | /* |
| 431 | * Is our ack too far before the left hand edge of the window? |
| 432 | */ |
| 433 | left_edge = counter_cm->protocol_state.tcp.end |
| 434 | - cm->protocol_state.tcp.max_win |
| 435 | - SFE_IPV4_TCP_MAX_ACK_WINDOW |
| 436 | - 1; |
| 437 | if (unlikely((s32)(sack - left_edge) < 0)) { |
| 438 | struct sfe_ipv4_connection *c = cm->connection; |
| 439 | spin_lock_bh(&si->lock); |
| 440 | ret = sfe_ipv4_remove_connection(si, c); |
| 441 | spin_unlock_bh(&si->lock); |
| 442 | |
| 443 | DEBUG_TRACE("ack: %u before left edge: %u\n", sack, left_edge); |
| 444 | if (ret) { |
| 445 | sfe_ipv4_flush_connection(si, c, SFE_SYNC_REASON_FLUSH); |
| 446 | } |
| 447 | rcu_read_unlock(); |
| 448 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_TCP_ACK_BEFORE_LEFT_EDGE); |
| 449 | return 0; |
| 450 | } |
| 451 | |
| 452 | /* |
| 453 | * Have we just seen the largest window size yet for this connection? If yes |
| 454 | * then we need to record the new value. |
| 455 | */ |
| 456 | scaled_win = ntohs(tcph->window) << cm->protocol_state.tcp.win_scale; |
| 457 | scaled_win += (sack - ack); |
| 458 | if (unlikely(cm->protocol_state.tcp.max_win < scaled_win)) { |
| 459 | cm->protocol_state.tcp.max_win = scaled_win; |
| 460 | } |
| 461 | |
| 462 | /* |
| 463 | * If our sequence and/or ack numbers have advanced then record the new state. |
| 464 | */ |
| 465 | if (likely((s32)(end - cm->protocol_state.tcp.end) >= 0)) { |
| 466 | cm->protocol_state.tcp.end = end; |
| 467 | } |
| 468 | |
| 469 | max_end = sack + scaled_win; |
| 470 | if (likely((s32)(max_end - counter_cm->protocol_state.tcp.max_end) >= 0)) { |
| 471 | counter_cm->protocol_state.tcp.max_end = max_end; |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | /* |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 476 | * Check if skb was cloned. If it was, unshare it. Because |
| 477 | * the data area is going to be written in this path and we don't want to |
| 478 | * change the cloned skb's data section. |
| 479 | */ |
| 480 | if (unlikely(skb_cloned(skb))) { |
| 481 | DEBUG_TRACE("%px: skb is a cloned skb\n", skb); |
| 482 | skb = skb_unshare(skb, GFP_ATOMIC); |
| 483 | if (!skb) { |
| 484 | DEBUG_WARN("Failed to unshare the cloned skb\n"); |
| 485 | rcu_read_unlock(); |
| 486 | return 0; |
| 487 | } |
| 488 | |
| 489 | /* |
| 490 | * Update the iph and tcph pointers with the unshared skb's data area. |
| 491 | */ |
| 492 | iph = (struct iphdr *)skb->data; |
| 493 | tcph = (struct tcphdr *)(skb->data + ihl); |
| 494 | } |
| 495 | |
| 496 | /* |
Guduri Prathyusha | 5f27e23 | 2022-01-06 14:39:04 +0530 | [diff] [blame] | 497 | * For PPPoE packets, match server MAC and session id |
| 498 | */ |
| 499 | if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_DECAP)) { |
| 500 | struct pppoe_hdr *ph; |
| 501 | struct ethhdr *eth; |
| 502 | |
| 503 | if (unlikely(!sfe_l2_parse_flag_check(l2_info, SFE_L2_PARSE_FLAGS_PPPOE_INGRESS))) { |
| 504 | rcu_read_unlock(); |
| 505 | DEBUG_TRACE("%px: PPPoE header not present in packet for PPPoE rule\n", skb); |
| 506 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_INCORRECT_PPPOE_PARSING); |
| 507 | return 0; |
| 508 | } |
| 509 | |
| 510 | ph = (struct pppoe_hdr *)(skb->head + sfe_l2_pppoe_hdr_offset_get(l2_info)); |
| 511 | eth = (struct ethhdr *)(skb->head + sfe_l2_hdr_offset_get(l2_info)); |
| 512 | if (unlikely(cm->pppoe_session_id != ntohs(ph->sid)) || unlikely(!(ether_addr_equal((u8*)cm->pppoe_remote_mac, (u8 *)eth->h_source)))) { |
| 513 | DEBUG_TRACE("%px: PPPoE sessions with session IDs %d and %d or server MACs %pM and %pM did not match\n", |
| 514 | skb, cm->pppoe_session_id, htons(ph->sid), cm->pppoe_remote_mac, eth->h_source); |
| 515 | rcu_read_unlock(); |
| 516 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_INVALID_PPPOE_SESSION); |
| 517 | return 0; |
| 518 | } |
| 519 | skb->protocol = htons(l2_info->protocol); |
| 520 | this_cpu_inc(si->stats_pcpu->pppoe_decap_packets_forwarded64); |
| 521 | |
| 522 | } else if (unlikely(sfe_l2_parse_flag_check(l2_info, SFE_L2_PARSE_FLAGS_PPPOE_INGRESS))) { |
| 523 | |
| 524 | /* |
Guduri Prathyusha | 034d635 | 2022-01-12 16:49:04 +0530 | [diff] [blame] | 525 | * 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] | 526 | */ |
Guduri Prathyusha | 034d635 | 2022-01-12 16:49:04 +0530 | [diff] [blame] | 527 | if (unlikely(!(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_BRIDGE_FLOW))) { |
| 528 | rcu_read_unlock(); |
| 529 | DEBUG_TRACE("%px: CME doesn't contain PPPoE flag but packet has PPPoE header\n", skb); |
| 530 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_PPPOE_NOT_SET_IN_CME); |
| 531 | return 0; |
| 532 | } |
| 533 | |
| 534 | /* |
| 535 | * For bridged flows when packet contains PPPoE header, restore the header back and forward to xmit interface |
| 536 | */ |
| 537 | __skb_push(skb, (sizeof(struct pppoe_hdr) + sizeof(struct sfe_ppp_hdr))); |
| 538 | l2_info->l2_hdr_size -= (sizeof(struct pppoe_hdr) + sizeof(struct sfe_ppp_hdr)); |
| 539 | this_cpu_inc(si->stats_pcpu->pppoe_bridge_packets_forwarded64); |
Guduri Prathyusha | 5f27e23 | 2022-01-06 14:39:04 +0530 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | /* |
Wayne Tan | bb7f178 | 2021-12-13 11:16:04 -0800 | [diff] [blame] | 543 | * Check if skb has enough headroom to write L2 headers |
| 544 | */ |
| 545 | if (unlikely(skb_headroom(skb) < cm->l2_hdr_size)) { |
| 546 | rcu_read_unlock(); |
| 547 | DEBUG_WARN("%px: Not enough headroom: %u\n", skb, skb_headroom(skb)); |
| 548 | sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_NO_HEADROOM); |
| 549 | return 0; |
| 550 | } |
| 551 | |
| 552 | /* |
Guduri Prathyusha | 5f27e23 | 2022-01-06 14:39:04 +0530 | [diff] [blame] | 553 | * From this point on we're good to modify the packet. |
| 554 | */ |
| 555 | |
| 556 | /* |
Guduri Prathyusha | 79a5fee | 2021-11-11 17:59:10 +0530 | [diff] [blame] | 557 | * For PPPoE flows, add PPPoE header before L2 header is added. |
| 558 | */ |
Guduri Prathyusha | 034d635 | 2022-01-12 16:49:04 +0530 | [diff] [blame] | 559 | if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_ENCAP)) { |
Wayne Tan | bb7f178 | 2021-12-13 11:16:04 -0800 | [diff] [blame] | 560 | sfe_pppoe_add_header(skb, cm->pppoe_session_id, PPP_IP); |
Guduri Prathyusha | 79a5fee | 2021-11-11 17:59:10 +0530 | [diff] [blame] | 561 | this_cpu_inc(si->stats_pcpu->pppoe_encap_packets_forwarded64); |
| 562 | } |
| 563 | |
| 564 | /* |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 565 | * Update DSCP |
| 566 | */ |
| 567 | if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_DSCP_REMARK)) { |
| 568 | iph->tos = (iph->tos & SFE_IPV4_DSCP_MASK) | cm->dscp; |
| 569 | } |
| 570 | |
| 571 | /* |
| 572 | * Decrement our TTL. |
| 573 | */ |
Ratheesh Kannoth | 71fc51e | 2022-01-05 10:02:47 +0530 | [diff] [blame] | 574 | if (likely(!bridge_flow)) { |
| 575 | iph->ttl = ttl - 1; |
| 576 | } |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 577 | |
| 578 | /* |
Ratheesh Kannoth | a3cf0e0 | 2021-12-09 09:44:10 +0530 | [diff] [blame] | 579 | * Enable HW csum if rx checksum is verified and xmit interface is CSUM offload capable. |
| 580 | * Note: If L4 csum at Rx was found to be incorrect, we (router) should use incremental L4 checksum here |
| 581 | * so that HW does not re-calculate/replace the L4 csum |
| 582 | */ |
| 583 | hw_csum = !!(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_CSUM_OFFLOAD) && (skb->ip_summed == CHECKSUM_UNNECESSARY); |
| 584 | |
| 585 | /* |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 586 | * Do we have to perform translations of the source address/port? |
| 587 | */ |
| 588 | if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_SRC)) { |
| 589 | u16 tcp_csum; |
| 590 | u32 sum; |
| 591 | |
| 592 | iph->saddr = cm->xlate_src_ip; |
| 593 | tcph->source = cm->xlate_src_port; |
| 594 | |
Ratheesh Kannoth | a3cf0e0 | 2021-12-09 09:44:10 +0530 | [diff] [blame] | 595 | if (unlikely(!hw_csum)) { |
| 596 | tcp_csum = tcph->check; |
| 597 | if (unlikely(skb->ip_summed == CHECKSUM_PARTIAL)) { |
| 598 | sum = tcp_csum + cm->xlate_src_partial_csum_adjustment; |
| 599 | } else { |
| 600 | sum = tcp_csum + cm->xlate_src_csum_adjustment; |
| 601 | } |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 602 | |
Ratheesh Kannoth | a3cf0e0 | 2021-12-09 09:44:10 +0530 | [diff] [blame] | 603 | sum = (sum & 0xffff) + (sum >> 16); |
| 604 | tcph->check = (u16)sum; |
| 605 | } |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | /* |
| 609 | * Do we have to perform translations of the destination address/port? |
| 610 | */ |
| 611 | if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_XLATE_DEST)) { |
| 612 | u16 tcp_csum; |
| 613 | u32 sum; |
| 614 | |
| 615 | iph->daddr = cm->xlate_dest_ip; |
| 616 | tcph->dest = cm->xlate_dest_port; |
| 617 | |
Ratheesh Kannoth | a3cf0e0 | 2021-12-09 09:44:10 +0530 | [diff] [blame] | 618 | if (unlikely(!hw_csum)) { |
| 619 | tcp_csum = tcph->check; |
| 620 | if (unlikely(skb->ip_summed == CHECKSUM_PARTIAL)) { |
| 621 | sum = tcp_csum + cm->xlate_dest_partial_csum_adjustment; |
| 622 | } else { |
| 623 | sum = tcp_csum + cm->xlate_dest_csum_adjustment; |
| 624 | } |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 625 | |
Ratheesh Kannoth | a3cf0e0 | 2021-12-09 09:44:10 +0530 | [diff] [blame] | 626 | sum = (sum & 0xffff) + (sum >> 16); |
| 627 | tcph->check = (u16)sum; |
| 628 | } |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | /* |
Ratheesh Kannoth | a3cf0e0 | 2021-12-09 09:44:10 +0530 | [diff] [blame] | 632 | * If HW checksum offload is not possible, full L3 checksum and incremental L4 checksum |
| 633 | * are used to update the packet. Setting ip_summed to CHECKSUM_UNNECESSARY ensures checksum is |
| 634 | * not recalculated further in packet path. |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 635 | */ |
Ratheesh Kannoth | a3cf0e0 | 2021-12-09 09:44:10 +0530 | [diff] [blame] | 636 | if (likely(hw_csum)) { |
| 637 | skb->ip_summed = CHECKSUM_PARTIAL; |
| 638 | } else { |
| 639 | iph->check = sfe_ipv4_gen_ip_csum(iph); |
| 640 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
| 641 | } |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 642 | |
| 643 | /* |
| 644 | * Update traffic stats. |
| 645 | */ |
| 646 | atomic_inc(&cm->rx_packet_count); |
| 647 | atomic_add(len, &cm->rx_byte_count); |
| 648 | |
| 649 | xmit_dev = cm->xmit_dev; |
| 650 | skb->dev = xmit_dev; |
| 651 | |
| 652 | /* |
Wayne Tan | bb7f178 | 2021-12-13 11:16:04 -0800 | [diff] [blame] | 653 | * Check to see if we need to add VLAN tags |
| 654 | */ |
| 655 | if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_INSERT_EGRESS_VLAN_TAG)) { |
| 656 | sfe_vlan_add_tag(skb, cm->egress_vlan_hdr_cnt, cm->egress_vlan_hdr); |
| 657 | } |
| 658 | |
| 659 | /* |
| 660 | * Check to see if we need to write an Ethernet header. |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 661 | */ |
| 662 | if (likely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_L2_HDR)) { |
| 663 | 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] | 664 | dev_hard_header(skb, xmit_dev, ntohs(skb->protocol), |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 665 | cm->xmit_dest_mac, cm->xmit_src_mac, len); |
| 666 | } else { |
| 667 | /* |
| 668 | * For the simple case we write this really fast. |
| 669 | */ |
| 670 | struct ethhdr *eth = (struct ethhdr *)__skb_push(skb, ETH_HLEN); |
| 671 | |
Guduri Prathyusha | 5f27e23 | 2022-01-06 14:39:04 +0530 | [diff] [blame] | 672 | eth->h_proto = skb->protocol; |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 673 | |
| 674 | ether_addr_copy((u8 *)eth->h_dest, (u8 *)cm->xmit_dest_mac); |
| 675 | ether_addr_copy((u8 *)eth->h_source, (u8 *)cm->xmit_src_mac); |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | /* |
| 680 | * Update priority of skb. |
| 681 | */ |
| 682 | if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_PRIORITY_REMARK)) { |
| 683 | skb->priority = cm->priority; |
| 684 | } |
| 685 | |
| 686 | /* |
| 687 | * Mark outgoing packet |
| 688 | */ |
Ken Zhu | 37040ea | 2021-09-09 21:11:15 -0700 | [diff] [blame] | 689 | if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_MARK)) { |
| 690 | skb->mark = cm->connection->mark; |
Ratheesh Kannoth | 6307bec | 2021-11-25 08:26:39 +0530 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | rcu_read_unlock(); |
| 694 | |
| 695 | this_cpu_inc(si->stats_pcpu->packets_forwarded64); |
| 696 | |
| 697 | /* |
| 698 | * We're going to check for GSO flags when we transmit the packet so |
| 699 | * start fetching the necessary cache line now. |
| 700 | */ |
| 701 | prefetch(skb_shinfo(skb)); |
| 702 | |
| 703 | /* |
| 704 | * Mark that this packet has been fast forwarded. |
| 705 | */ |
| 706 | skb->fast_forwarded = 1; |
| 707 | |
| 708 | /* |
| 709 | * Send the packet on its way. |
| 710 | */ |
| 711 | dev_queue_xmit(skb); |
| 712 | |
| 713 | return 1; |
| 714 | } |