blob: f505f2ff16cff08435c039d93aee29eb60540659 [file] [log] [blame]
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05301/*
2 * sfe_ipv6_udp.c
3 * Shortcut forwarding engine file for IPv6 UDP
4 *
5 * Copyright (c) 2015-2016, 2019-2020, The Linux Foundation. All rights reserved.
Guduri Prathyusha5f27e232022-01-06 14:39:04 +05306 * Copyright (c) 2021-2022 Qualcomm Innovation Center, Inc. All rights reserved.
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +05307 *
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/version.h>
Suruchi Suman23a279d2021-11-16 15:13:09 +053025#include <net/ip6_checksum.h>
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +053026
27#include "sfe_debug.h"
28#include "sfe_api.h"
29#include "sfe.h"
30#include "sfe_flow_cookie.h"
31#include "sfe_ipv6.h"
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +053032#include "sfe_pppoe.h"
Wayne Tanbb7f1782021-12-13 11:16:04 -080033#include "sfe_vlan.h"
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +053034
35/*
Suruchi Suman23a279d2021-11-16 15:13:09 +053036 * sfe_ipv6_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 */
44static int sfe_ipv6_udp_sk_deliver(struct sk_buff *skb, struct sfe_ipv6_connection_match *cm,
45 unsigned int ihl)
46{
47 int (*encap_rcv)(struct sock *sk, struct sk_buff *skb);
48 struct udp_sock *up;
49 struct udphdr *udph;
50 struct sock *sk;
51 int ret;
52
53 /*
54 * Call the decap handler
55 */
56 up = rcu_dereference(cm->up);
57 encap_rcv = READ_ONCE(up->encap_rcv);
58 if (unlikely(!encap_rcv)) {
59 DEBUG_ERROR("sfe: Error: up->encap_rcv is NULL\n");
60 return 1;
61 }
62
63#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
64 nf_reset(skb);
65#else
66 nf_reset_ct(skb);
67#endif
68 skb_pull(skb, ihl);
69 skb_reset_transport_header(skb);
70
71 udph = udp_hdr(skb);
72 if (unlikely(skb->ip_summed != CHECKSUM_UNNECESSARY) && unlikely(skb->ip_summed != CHECKSUM_COMPLETE)) {
73 /*
74 * Set Pseudo Checksum using Linux API
75 */
76 if (unlikely(udp6_csum_init(skb, udp_hdr(skb), IPPROTO_UDP))) {
77 DEBUG_ERROR("sfe: udp checksum init() failed: %p\n", skb);
78 kfree_skb(skb);
79 return -1;
80 }
81
82 /*
83 * Verify checksum before giving to encap_rcv handler function.
84 */
85 if (unlikely(udp_lib_checksum_complete(skb))) {
86 DEBUG_ERROR("sfe: Invalid udp checksum: %p\n", skb);
87 kfree_skb(skb);
88 return -1;
89 }
90 }
91
92 /*
93 * Mark that this packet has been fast forwarded.
94 */
95 sk = (struct sock *)up;
96
97 /*
98 * TODO: Find the fix to set skb->ip_summed = CHECKSUM_NONE;
99 */
100
101 /*
102 * encap_rcv() returns the following value:
103 * =0 if skb was successfully passed to the encap
104 * handler or was discarded by it.
105 * >0 if skb should be passed on to UDP.
106 * <0 if skb should be resubmitted as proto -N
107 */
108 ret = encap_rcv(sk, skb);
109 if (unlikely(ret)) {
110
111 /*
112 * If encap_rcv fails, vxlan driver drops the packet.
113 * No need to free the skb here.
114 */
115 DEBUG_ERROR("sfe: udp-decap API return error: %d\n", ret);
116 return -1;
117 }
118
119 DEBUG_TRACE("sfe: udp-decap API encap_rcv successful\n");
120 return 0;
121}
122
123/*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530124 * sfe_ipv6_recv_udp()
125 * Handle UDP packet receives and forwarding.
126 */
127int sfe_ipv6_recv_udp(struct sfe_ipv6 *si, struct sk_buff *skb, struct net_device *dev,
Ken Zhu88c58152021-12-09 15:12:06 -0800128 unsigned int len, struct ipv6hdr *iph, unsigned int ihl, bool sync_on_find, struct sfe_l2_info *l2_info, bool tun_outer)
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530129{
130 struct udphdr *udph;
131 struct sfe_ipv6_addr *src_ip;
132 struct sfe_ipv6_addr *dest_ip;
133 __be16 src_port;
134 __be16 dest_port;
Parikshit Guned31a8202022-01-05 22:15:04 +0530135 u32 service_class_id;
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530136 struct sfe_ipv6_connection_match *cm;
137 struct net_device *xmit_dev;
Suruchi Suman23a279d2021-11-16 15:13:09 +0530138 int ret;
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +0530139 bool hw_csum;
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +0530140 bool bridge_flow;
Ken Zhu7e38d1a2021-11-30 17:31:46 -0800141 bool fast_xmit;
142 netdev_features_t features;
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530143
Suruchi Suman23a279d2021-11-16 15:13:09 +0530144 DEBUG_TRACE("%px: sfe: sfe_ipv6_recv_udp called.\n", skb);
145
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530146 /*
147 * Is our packet too short to contain a valid UDP header?
148 */
149 if (!pskb_may_pull(skb, (sizeof(struct udphdr) + ihl))) {
150
151 sfe_ipv6_exception_stats_inc(si,SFE_IPV6_EXCEPTION_EVENT_UDP_HEADER_INCOMPLETE);
152 DEBUG_TRACE("packet too short for UDP header\n");
153 return 0;
154 }
155
156 /*
157 * Read the IP address and port information. Read the IP header data first
158 * because we've almost certainly got that in the cache. We may not yet have
159 * the UDP header cached though so allow more time for any prefetching.
160 */
161 src_ip = (struct sfe_ipv6_addr *)iph->saddr.s6_addr32;
162 dest_ip = (struct sfe_ipv6_addr *)iph->daddr.s6_addr32;
163
164 udph = (struct udphdr *)(skb->data + ihl);
165 src_port = udph->source;
166 dest_port = udph->dest;
167
168 rcu_read_lock();
169
170 /*
171 * Look for a connection match.
172 */
173#ifdef CONFIG_NF_FLOW_COOKIE
174 cm = si->sfe_flow_cookie_table[skb->flow_cookie & SFE_FLOW_COOKIE_MASK].match;
175 if (unlikely(!cm)) {
176 cm = sfe_ipv6_find_connection_match_rcu(si, dev, IPPROTO_UDP, src_ip, src_port, dest_ip, dest_port);
177 }
178#else
179 cm = sfe_ipv6_find_connection_match_rcu(si, dev, IPPROTO_UDP, src_ip, src_port, dest_ip, dest_port);
180#endif
181 if (unlikely(!cm)) {
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530182
Suruchi Suman23a279d2021-11-16 15:13:09 +0530183 /*
184 * Try a 4-tuple lookup; required for tunnels like VxLAN.
185 */
186 cm = sfe_ipv6_find_connection_match_rcu(si, dev, IPPROTO_UDP, src_ip, 0, dest_ip, dest_port);
187 if (unlikely(!cm)) {
188 rcu_read_unlock();
189 sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_UDP_NO_CONNECTION);
190 DEBUG_TRACE("no connection found\n");
191 return 0;
192 }
193 DEBUG_TRACE("sfe: 4-tuple lookup successful\n");
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530194 }
195
196 /*
Wayne Tanbb7f1782021-12-13 11:16:04 -0800197 * Do we expect an ingress VLAN tag for this flow?
198 */
199 if (unlikely(!sfe_vlan_validate_ingress_tag(skb, cm->ingress_vlan_hdr_cnt, cm->ingress_vlan_hdr, l2_info))) {
200 rcu_read_unlock();
201 sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_INGRESS_VLAN_TAG_MISMATCH);
202 DEBUG_TRACE("VLAN tag mismatch. skb=%px\n", skb);
203 return 0;
204 }
205
206 /*
Ratheesh Kannoth5dee3772022-01-18 11:27:14 +0530207 * Source interface validate.
208 */
209 if (unlikely((cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_SRC_INTERFACE_CHECK) && (cm->match_dev != dev))) {
Murat Sezgin9c538972022-05-17 13:33:17 -0700210 if (!(cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_SRC_INTERFACE_CHECK_NO_FLUSH)) {
211 struct sfe_ipv6_connection *c = cm->connection;
212 DEBUG_TRACE("flush on source interface check failure\n");
213 spin_lock_bh(&si->lock);
214 ret = sfe_ipv6_remove_connection(si, c);
215 spin_unlock_bh(&si->lock);
Ratheesh Kannoth5dee3772022-01-18 11:27:14 +0530216
Murat Sezgin9c538972022-05-17 13:33:17 -0700217 if (ret) {
218 sfe_ipv6_flush_connection(si, c, SFE_SYNC_REASON_FLUSH);
219 }
Ratheesh Kannoth5dee3772022-01-18 11:27:14 +0530220 }
221 rcu_read_unlock();
222 sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_INVALID_SRC_IFACE);
Murat Sezgin9c538972022-05-17 13:33:17 -0700223 DEBUG_TRACE("exception the packet on source interface check failure\n");
Ratheesh Kannoth5dee3772022-01-18 11:27:14 +0530224 return 0;
225 }
226
227 /*
Wayne Tan1cabbf12022-05-01 13:01:45 -0700228 * If our packet has been marked as "sync on find" we can't actually
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530229 * forward it in the fast path, but now that we've found an associated
Ken Zhu88c58152021-12-09 15:12:06 -0800230 * connection we need sync its status before exception it to slow path.
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530231 */
Ken Zhu88c58152021-12-09 15:12:06 -0800232 if (unlikely(sync_on_find)) {
233 sfe_ipv6_sync_status(si, cm->connection, SFE_SYNC_REASON_STATS);
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530234 rcu_read_unlock();
235
236 sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_UDP_IP_OPTIONS_OR_INITIAL_FRAGMENT);
Ken Zhu88c58152021-12-09 15:12:06 -0800237 DEBUG_TRACE("Sync on find\n");
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530238 return 0;
239 }
240
241#ifdef CONFIG_XFRM
242 /*
243 * We can't accelerate the flow on this direction, just let it go
244 * through the slow path.
245 */
246 if (unlikely(!cm->flow_accel)) {
247 rcu_read_unlock();
248 this_cpu_inc(si->stats_pcpu->packets_not_forwarded64);
249 return 0;
250 }
251#endif
252
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +0530253 bridge_flow = !!(cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_BRIDGE_FLOW);
254
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530255 /*
256 * Does our hop_limit allow forwarding?
257 */
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +0530258 if (likely(!bridge_flow)) {
259 if (unlikely(iph->hop_limit < 2)) {
Ken Zhu88c58152021-12-09 15:12:06 -0800260 sfe_ipv6_sync_status(si, cm->connection, SFE_SYNC_REASON_STATS);
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +0530261 rcu_read_unlock();
262
263 sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_UDP_SMALL_TTL);
Ken Zhu88c58152021-12-09 15:12:06 -0800264 DEBUG_TRACE("hop_limit too low\n");
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +0530265 return 0;
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530266 }
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530267 }
268
269 /*
270 * If our packet is larger than the MTU of the transmit interface then
271 * we can't forward it easily.
272 */
Ken Zhu697eee02022-07-13 10:48:49 -0700273 if (unlikely((len > cm->xmit_dev_mtu) && (!cm->up))) {
Ken Zhu88c58152021-12-09 15:12:06 -0800274 sfe_ipv6_sync_status(si, cm->connection, SFE_SYNC_REASON_STATS);
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530275 rcu_read_unlock();
276
277 sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_UDP_NEEDS_FRAGMENTATION);
Ken Zhu88c58152021-12-09 15:12:06 -0800278 DEBUG_TRACE("Larger than MTU\n");
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530279 return 0;
280 }
281
282 /*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530283 * Check if skb was cloned. If it was, unshare it. Because
284 * the data area is going to be written in this path and we don't want to
285 * change the cloned skb's data section.
286 */
287 if (unlikely(skb_cloned(skb))) {
288 DEBUG_TRACE("%px: skb is a cloned skb\n", skb);
289 skb = skb_unshare(skb, GFP_ATOMIC);
290 if (!skb) {
291 DEBUG_WARN("Failed to unshare the cloned skb\n");
292 rcu_read_unlock();
293 return 0;
294 }
295
296 /*
297 * Update the iph and udph pointers with the unshared skb's data area.
298 */
299 iph = (struct ipv6hdr *)skb->data;
300 udph = (struct udphdr *)(skb->data + ihl);
301 }
302
303 /*
Wayne Tan1cabbf12022-05-01 13:01:45 -0700304 * Check if skb has enough headroom to write L2 headers
305 */
306 if (unlikely(skb_headroom(skb) < cm->l2_hdr_size)) {
307 rcu_read_unlock();
308 DEBUG_WARN("%px: Not enough headroom: %u\n", skb, skb_headroom(skb));
309 sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_NO_HEADROOM);
310 return 0;
311 }
312
313 /*
Guduri Prathyusha5f27e232022-01-06 14:39:04 +0530314 * For PPPoE packets, match server MAC and session id
315 */
316 if (unlikely(cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_PPPOE_DECAP)) {
Guduri Prathyusha5f27e232022-01-06 14:39:04 +0530317 struct ethhdr *eth;
Nitin Shetty9af87d42022-02-11 16:25:29 +0530318 bool pppoe_match;
Guduri Prathyusha5f27e232022-01-06 14:39:04 +0530319
320 if (unlikely(!sfe_l2_parse_flag_check(l2_info, SFE_L2_PARSE_FLAGS_PPPOE_INGRESS))) {
321 rcu_read_unlock();
322 DEBUG_TRACE("%px: PPPoE header not present in packet for PPPoE rule\n", skb);
323 sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_INCORRECT_PPPOE_PARSING);
324 return 0;
325 }
326
Nitin Shetty9af87d42022-02-11 16:25:29 +0530327 eth = eth_hdr(skb);
328
329 pppoe_match = (cm->pppoe_session_id == sfe_l2_pppoe_session_id_get(l2_info)) &&
330 ether_addr_equal((u8*)cm->pppoe_remote_mac, (u8 *)eth->h_source);
331
332 if (unlikely(!pppoe_match)) {
333 DEBUG_TRACE("%px: PPPoE sessions ID %d and %d or MAC %pM and %pM did not match\n",
334 skb, cm->pppoe_session_id, sfe_l2_pppoe_session_id_get(l2_info),
335 cm->pppoe_remote_mac, eth->h_source);
Guduri Prathyusha5f27e232022-01-06 14:39:04 +0530336 rcu_read_unlock();
337 sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_INVALID_PPPOE_SESSION);
338 return 0;
339 }
Nitin Shetty9af87d42022-02-11 16:25:29 +0530340
Guduri Prathyusha5f27e232022-01-06 14:39:04 +0530341 skb->protocol = htons(l2_info->protocol);
342 this_cpu_inc(si->stats_pcpu->pppoe_decap_packets_forwarded64);
Guduri Prathyusha5f27e232022-01-06 14:39:04 +0530343 } else if (unlikely(sfe_l2_parse_flag_check(l2_info, SFE_L2_PARSE_FLAGS_PPPOE_INGRESS))) {
344
345 /*
Guduri Prathyusha034d6352022-01-12 16:49:04 +0530346 * If packet contains PPPoE header but CME doesn't contain PPPoE flag yet we are exceptioning the packet to linux
Guduri Prathyusha5f27e232022-01-06 14:39:04 +0530347 */
Wayne Tan1cabbf12022-05-01 13:01:45 -0700348 if (unlikely(!bridge_flow)) {
Guduri Prathyusha034d6352022-01-12 16:49:04 +0530349 rcu_read_unlock();
350 DEBUG_TRACE("%px: CME doesn't contain PPPoE flag but packet has PPPoE header\n", skb);
Nitin Shetty9af87d42022-02-11 16:25:29 +0530351 sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_PPPOE_NOT_SET_IN_CME);
Guduri Prathyusha034d6352022-01-12 16:49:04 +0530352 return 0;
353 }
354
355 /*
356 * For bridged flows when packet contains PPPoE header, restore the header back and forward to xmit interface
357 */
Wayne Tan1cabbf12022-05-01 13:01:45 -0700358 __skb_push(skb, PPPOE_SES_HLEN);
Guduri Prathyusha034d6352022-01-12 16:49:04 +0530359 this_cpu_inc(si->stats_pcpu->pppoe_bridge_packets_forwarded64);
Guduri Prathyusha5f27e232022-01-06 14:39:04 +0530360 }
361
362 /*
363 * From this point on we're good to modify the packet.
364 */
365
366 /*
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +0530367 * For PPPoE flows, add PPPoE header before L2 header is added.
368 */
Guduri Prathyusha034d6352022-01-12 16:49:04 +0530369 if (unlikely(cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_PPPOE_ENCAP)) {
Wayne Tanbb7f1782021-12-13 11:16:04 -0800370 sfe_pppoe_add_header(skb, cm->pppoe_session_id, PPP_IPV6);
Guduri Prathyusha79a5fee2021-11-11 17:59:10 +0530371 this_cpu_inc(si->stats_pcpu->pppoe_encap_packets_forwarded64);
372 }
373
374 /*
Suruchi Suman23a279d2021-11-16 15:13:09 +0530375 * UDP sock will be valid only in decap-path.
376 * Call encap_rcv function associated with udp_sock in cm.
377 */
378 if (unlikely(cm->up)) {
379
380 /*
381 * Call decap handler associated with sock.
382 * Also validates UDP checksum before calling decap handler.
383 */
384 ret = sfe_ipv6_udp_sk_deliver(skb, cm, ihl);
385 if (unlikely(ret == -1)) {
386 rcu_read_unlock();
387 this_cpu_inc(si->stats_pcpu->packets_dropped64);
388 return 1;
389 } else if (unlikely(ret == 1)) {
390 rcu_read_unlock();
391 this_cpu_inc(si->stats_pcpu->packets_not_forwarded64);
392 return 0;
393 }
394
395 /*
396 * Update traffic stats
397 */
398 atomic_inc(&cm->rx_packet_count);
399 atomic_add(len, &cm->rx_byte_count);
400
401 rcu_read_unlock();
402 this_cpu_inc(si->stats_pcpu->packets_forwarded64);
403 DEBUG_TRACE("%p: sfe: sfe_ipv4_recv_udp -> encap_rcv done.\n", skb);
404 return 1;
405 }
406
407 /*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530408 * Update DSCP
409 */
410 if (unlikely(cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_DSCP_REMARK)) {
411 sfe_ipv6_change_dsfield(iph, cm->dscp);
412 }
413
414 /*
415 * Decrement our hop_limit.
416 */
Ratheesh Kannoth71fc51e2022-01-05 10:02:47 +0530417 if (likely(!bridge_flow)) {
418 iph->hop_limit -= (u8)!tun_outer;
419 }
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530420
421 /*
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +0530422 * Enable HW csum if rx checksum is verified and xmit interface is CSUM offload capable.
423 * Note: If L4 csum at Rx was found to be incorrect, we (router) should use incremental L4 checksum here
424 * so that HW does not re-calculate/replace the L4 csum
425 */
426 hw_csum = !!(cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_CSUM_OFFLOAD) && (skb->ip_summed == CHECKSUM_UNNECESSARY);
427
428 /*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530429 * Do we have to perform translations of the source address/port?
430 */
431 if (unlikely(cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_XLATE_SRC)) {
432 u16 udp_csum;
433
434 iph->saddr.s6_addr32[0] = cm->xlate_src_ip[0].addr[0];
435 iph->saddr.s6_addr32[1] = cm->xlate_src_ip[0].addr[1];
436 iph->saddr.s6_addr32[2] = cm->xlate_src_ip[0].addr[2];
437 iph->saddr.s6_addr32[3] = cm->xlate_src_ip[0].addr[3];
438 udph->source = cm->xlate_src_port;
439
440 /*
441 * Do we have a non-zero UDP checksum? If we do then we need
442 * to update it.
443 */
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +0530444 if (unlikely(!hw_csum)) {
445 udp_csum = udph->check;
446 if (likely(udp_csum)) {
447 u32 sum = udp_csum + cm->xlate_src_csum_adjustment;
448 sum = (sum & 0xffff) + (sum >> 16);
449 udph->check = (u16)sum;
450 }
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530451 }
452 }
453
454 /*
455 * Do we have to perform translations of the destination address/port?
456 */
457 if (unlikely(cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_XLATE_DEST)) {
458 u16 udp_csum;
459
460 iph->daddr.s6_addr32[0] = cm->xlate_dest_ip[0].addr[0];
461 iph->daddr.s6_addr32[1] = cm->xlate_dest_ip[0].addr[1];
462 iph->daddr.s6_addr32[2] = cm->xlate_dest_ip[0].addr[2];
463 iph->daddr.s6_addr32[3] = cm->xlate_dest_ip[0].addr[3];
464 udph->dest = cm->xlate_dest_port;
465
466 /*
467 * Do we have a non-zero UDP checksum? If we do then we need
468 * to update it.
469 */
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +0530470 if (unlikely(!hw_csum)) {
471 udp_csum = udph->check;
472 if (likely(udp_csum)) {
473 u32 sum = udp_csum + cm->xlate_dest_csum_adjustment;
474 sum = (sum & 0xffff) + (sum >> 16);
475 udph->check = (u16)sum;
476 }
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530477 }
478 }
479
480 /*
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +0530481 * If HW checksum offload is not possible, incremental L4 checksum is used to update the packet.
482 * Setting ip_summed to CHECKSUM_UNNECESSARY ensures checksum is not recalculated further in packet
483 * path.
484 */
485 if (likely(hw_csum)) {
486 skb->ip_summed = CHECKSUM_PARTIAL;
Ratheesh Kannotha3cf0e02021-12-09 09:44:10 +0530487 }
488
489 /*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530490 * Update traffic stats.
491 */
492 atomic_inc(&cm->rx_packet_count);
493 atomic_add(len, &cm->rx_byte_count);
494
495 xmit_dev = cm->xmit_dev;
496 skb->dev = xmit_dev;
497
498 /*
Wayne Tanbb7f1782021-12-13 11:16:04 -0800499 * Check to see if we need to add VLAN tags
500 */
501 if (unlikely(cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_INSERT_EGRESS_VLAN_TAG)) {
502 sfe_vlan_add_tag(skb, cm->egress_vlan_hdr_cnt, cm->egress_vlan_hdr);
503 }
504
505 /*
506 * Check to see if we need to write an Ethernet header.
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530507 */
508 if (likely(cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_WRITE_L2_HDR)) {
509 if (unlikely(!(cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR))) {
Guduri Prathyusha5f27e232022-01-06 14:39:04 +0530510 dev_hard_header(skb, xmit_dev, ntohs(skb->protocol),
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530511 cm->xmit_dest_mac, cm->xmit_src_mac, len);
512 } else {
513 /*
514 * For the simple case we write this really fast.
515 */
516 struct ethhdr *eth = (struct ethhdr *)__skb_push(skb, ETH_HLEN);
Guduri Prathyusha5f27e232022-01-06 14:39:04 +0530517 eth->h_proto = skb->protocol;
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530518 ether_addr_copy((u8 *)eth->h_dest, (u8 *)cm->xmit_dest_mac);
519 ether_addr_copy((u8 *)eth->h_source, (u8 *)cm->xmit_src_mac);
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530520 }
521 }
522
523 /*
524 * Update priority of skb.
525 */
526 if (unlikely(cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_PRIORITY_REMARK)) {
527 skb->priority = cm->priority;
528 }
529
530 /*
531 * Mark outgoing packet.
532 */
Ken Zhu37040ea2021-09-09 21:11:15 -0700533 if (unlikely(cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_MARK)) {
534 skb->mark = cm->mark;
Parikshit Guned31a8202022-01-05 22:15:04 +0530535 /*
536 * Update service class stats if SAWF is valid.
537 */
538 if (likely(cm->sawf_valid)) {
539 service_class_id = SFE_GET_SAWF_SERVICE_CLASS(cm->mark);
540 sfe_ipv6_service_class_stats_inc(si, service_class_id, len);
541 }
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530542 }
543
Ken Zhu7e38d1a2021-11-30 17:31:46 -0800544 /*
545 * For the first packets, check if it could got fast xmit.
546 */
547 if (unlikely(!(cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_FAST_XMIT_FLOW_CHECKED)
548 && (cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_FAST_XMIT_DEV_ADMISSION))){
549 cm->features = netif_skb_features(skb);
550 if (likely(sfe_fast_xmit_check(skb, cm->features))) {
551 cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_FAST_XMIT;
552 }
553 cm->flags |= SFE_IPV6_CONNECTION_MATCH_FLAG_FAST_XMIT_FLOW_CHECKED;
554 }
555 features = cm->features;
556
557 fast_xmit = !!(cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_FAST_XMIT);
558
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530559 rcu_read_unlock();
560
561 this_cpu_inc(si->stats_pcpu->packets_forwarded64);
562
563 /*
Ken Zhu7e38d1a2021-11-30 17:31:46 -0800564 * We do per packet condition check before we could fast xmit the
565 * packet.
566 */
567 if (likely(fast_xmit && dev_fast_xmit(skb, xmit_dev, features))) {
568 this_cpu_inc(si->stats_pcpu->packets_fast_xmited64);
569 return 1;
570 }
571
572 /*
Sourav Poddar00345c02022-07-25 17:17:25 +0530573 * We're going to check for GSO flags when we transmit the packet so
574 * start fetching the necessary cache line now.
575 */
576 prefetch(skb_shinfo(skb));
577
578 /*
Ratheesh Kannoth6307bec2021-11-25 08:26:39 +0530579 * Mark that this packet has been fast forwarded.
580 */
581 skb->fast_forwarded = 1;
582
583 /*
584 * Send the packet on its way.
585 */
586 dev_queue_xmit(skb);
587
588 return 1;
589}