blob: 0213939f077d77ca85cac1a52426e3ebc47cee3c [file] [log] [blame]
Suhas N Bhargav592e64c2021-11-12 16:53:08 +05301/*
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"
Pritam Kumar Jha53cea892022-09-27 13:32:53 +053032#include "sfe_vlan.h"
Suhas N Bhargav592e64c2021-11-12 16:53:08 +053033
34/*
35 * sfe_ipv4_recv_esp()
36 * Handle ESP packet receives and forwarding
37 */
38int sfe_ipv4_recv_esp(struct sfe_ipv4 *si, struct sk_buff *skb, struct net_device *dev,
Pritam Kumar Jha53cea892022-09-27 13:32:53 +053039 unsigned int len, struct iphdr *iph, unsigned int ihl,
40 bool sync_on_find, struct sfe_l2_info *l2_info, bool tun_outer)
Suhas N Bhargav592e64c2021-11-12 16:53:08 +053041{
42 struct sfe_ipv4_connection_match *cm;
43 struct net_device *xmit_dev;
44 struct net_protocol *ipprot;
45 netdev_features_t features;
46 bool passthrough;
47 bool bridge_flow;
48 bool fast_xmit;
49 bool hw_csum;
50 __be32 src_ip;
51 __be32 dest_ip;
52 bool ret;
53 u8 ttl;
54
55 /*
56 * Read the IP address from the iphdr, and set the src/dst ports to 0.
57 */
58 src_ip = iph->saddr;
59 dest_ip = iph->daddr;
60 rcu_read_lock();
61
62 /*
63 * Look for a connection match.
64 */
65#ifdef CONFIG_NF_FLOW_COOKIE
66 cm = si->sfe_flow_cookie_table[skb->flow_cookie & SFE_FLOW_COOKIE_MASK].match;
67 if (unlikely(!cm)) {
68 cm = sfe_ipv4_find_ipv4_connection_match_rcu(si, dev, IPPROTO_ESP, src_ip, 0, dest_ip, 0);
69 }
70#else
71 cm = sfe_ipv4_find_connection_match_rcu(si, dev, IPPROTO_ESP, src_ip, 0, dest_ip, 0);
72#endif
73 if (unlikely(!cm)) {
74 rcu_read_unlock();
75 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_ESP_NO_CONNECTION);
76 DEBUG_TRACE("no connection found for esp packet\n");
77 return 0;
78 }
79
80 /*
81 * Source interface validate.
82 */
83 if (unlikely((cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_SRC_INTERFACE_CHECK) && (cm->match_dev != dev))) {
84 struct sfe_ipv4_connection *c = cm->connection;
85 int ret;
86
87 spin_lock_bh(&si->lock);
88 ret = sfe_ipv4_remove_connection(si, c);
89 spin_unlock_bh(&si->lock);
90
91 if (ret) {
92 sfe_ipv4_flush_connection(si, c, SFE_SYNC_REASON_FLUSH);
93 }
94 rcu_read_unlock();
95 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_INVALID_SRC_IFACE);
96 DEBUG_TRACE("flush on wrong source interface check failure\n");
97 return 0;
98 }
99
100 passthrough = cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_PASSTHROUGH;
101 bridge_flow = !!(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_BRIDGE_FLOW);
102
103 /*
104 * If our packet has been marked as "sync on find" we can't actually
105 * forward it in the fast path, but now that we've found an associated
106 * connection we need sync its status before exception it to slow path unless
107 * it is passthrough (packets not directed to DUT) packet.
108 * TODO: revisit to ensure that pass through traffic is not bypassing firewall for fragmented cases
109 */
110 if (unlikely(sync_on_find) && !passthrough) {
111 sfe_ipv4_sync_status(si, cm->connection, SFE_SYNC_REASON_STATS);
112 rcu_read_unlock();
113 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_ESP_IP_OPTIONS_OR_INITIAL_FRAGMENT);
114 DEBUG_TRACE("%px: sfe: sync on find\n", cm);
115 return 0;
116 }
117
118 /*
Pritam Kumar Jha53cea892022-09-27 13:32:53 +0530119 * Do we expect an ingress VLAN tag for this flow?
120 */
121 if (unlikely(!sfe_vlan_validate_ingress_tag(skb, cm->ingress_vlan_hdr_cnt, cm->ingress_vlan_hdr, l2_info))) {
122 rcu_read_unlock();
123 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_INGRESS_VLAN_TAG_MISMATCH);
124 DEBUG_TRACE("VLAN tag mismatch. skb=%px\n", skb);
125 return 0;
126 }
127
128 /*
Suhas N Bhargav592e64c2021-11-12 16:53:08 +0530129 * Check if skb was cloned. If it was, unshare it.
130 */
131 if (unlikely(skb_cloned(skb))) {
132 DEBUG_TRACE("%px: skb is a cloned skb\n", skb);
133 skb = skb_unshare(skb, GFP_ATOMIC);
134 if (!skb) {
135 DEBUG_WARN("Failed to unshare the cloned skb\n");
136 rcu_read_unlock();
137 return 0;
138 }
139
140 /*
141 * Update the iphdr pointer with the unshared skb's data area.
142 */
143 iph = (struct iphdr *)skb->data;
144 }
145
146 /*
147 * Enable HW csum if rx checksum is verified and xmit interface is CSUM offload capable.
148 */
149 hw_csum = !!(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_CSUM_OFFLOAD) && (skb->ip_summed == CHECKSUM_UNNECESSARY);
150
151 /*
152 * proto decap packet.
153 * Invoke the inet_protocol handler for delivery of the packet.
154 */
155 ipprot = rcu_dereference(cm->proto);
156 if (likely(ipprot)) {
157 skb_reset_network_header(skb);
158 skb_pull(skb, ihl);
159 skb_reset_transport_header(skb);
160 xmit_dev = cm->xmit_dev;
161 skb->dev = xmit_dev;
162
163 ret = ipprot->handler(skb);
164 if (ret) {
165 rcu_read_unlock();
166 this_cpu_inc(si->stats_pcpu->packets_not_forwarded64);
167 DEBUG_TRACE("ESP handler returned error %u\n", ret);
168 return 0;
169 }
170
171 /*
172 * Update traffic stats.
173 */
174 atomic_inc(&cm->rx_packet_count);
175 atomic_add(len, &cm->rx_byte_count);
176
177 rcu_read_unlock();
178 this_cpu_inc(si->stats_pcpu->packets_forwarded64);
179 return 1;
180 }
181
182 /*
183 * esp passthrough / ip local out scenarios.
184 */
185 /*
186 * If our packet is larger than the MTU of the transmit interface then
187 * we can't forward it easily.
188 */
189 if (unlikely(len > cm->xmit_dev_mtu)) {
190 sfe_ipv4_sync_status(si, cm->connection, SFE_SYNC_REASON_STATS);
191 rcu_read_unlock();
192 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_ESP_NEEDS_FRAGMENTATION);
193 DEBUG_TRACE("%px: sfe: larger than MTU\n", cm);
194 return 0;
195 }
196
197 /*
Pritam Kumar Jha53cea892022-09-27 13:32:53 +0530198 * Check if skb has enough headroom to write L2 headers
199 */
200 if (unlikely(skb_headroom(skb) < cm->l2_hdr_size)) {
201 rcu_read_unlock();
202 DEBUG_TRACE("%px: Not enough headroom: %u\n", skb, skb_headroom(skb));
203 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_NO_HEADROOM);
204 return 0;
205 }
206
207 /*
Suhas N Bhargav592e64c2021-11-12 16:53:08 +0530208 * need to ensure that TTL is >=2.
209 */
210 ttl = iph->ttl;
211 if (!bridge_flow && (ttl < 2) && passthrough) {
212 sfe_ipv4_sync_status(si, cm->connection, SFE_SYNC_REASON_STATS);
213 rcu_read_unlock();
214
215 DEBUG_TRACE("%px: sfe: TTL too low\n", skb);
216 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_ESP_SMALL_TTL);
217 return 0;
218 }
219
220 /*
221 * decrement TTL by 1.
222 */
223 iph->ttl = (ttl - (u8)(!bridge_flow && !tun_outer));
224
225 /*
226 * Update DSCP
227 */
228 if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_DSCP_REMARK)) {
229 iph->tos = (iph->tos & SFE_IPV4_DSCP_MASK) | cm->dscp;
230 }
231
232 /*
233 * Replace the IP checksum.
234 */
235 if (likely(hw_csum)) {
236 skb->ip_summed = CHECKSUM_PARTIAL;
237 } else {
238 iph->check = sfe_ipv4_gen_ip_csum(iph);
239 }
240
241 /*
242 * Update traffic stats.
243 */
244 atomic_inc(&cm->rx_packet_count);
245 atomic_add(len, &cm->rx_byte_count);
246
247 xmit_dev = cm->xmit_dev;
248 skb->dev = xmit_dev;
249
250 /*
Pritam Kumar Jha53cea892022-09-27 13:32:53 +0530251 * Check to see if we need to add VLAN tags
252 */
253 if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_INSERT_EGRESS_VLAN_TAG)) {
254 sfe_vlan_add_tag(skb, cm->egress_vlan_hdr_cnt, cm->egress_vlan_hdr);
255 }
256
257 /*
Suhas N Bhargav592e64c2021-11-12 16:53:08 +0530258 * write the layer - 2 header.
259 */
260 if (likely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_L2_HDR)) {
261 if (unlikely(!(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR))) {
262 dev_hard_header(skb, xmit_dev, ETH_P_IP, cm->xmit_dest_mac, cm->xmit_src_mac, len);
263 } else {
264 /*
265 * For the simple case we write this really fast.
266 */
267 struct ethhdr *eth = (struct ethhdr *)__skb_push(skb, ETH_HLEN);
268 eth->h_proto = htons(ETH_P_IP);
269 ether_addr_copy((u8 *)eth->h_dest, (u8 *)cm->xmit_dest_mac);
270 ether_addr_copy((u8 *)eth->h_source, (u8 *)cm->xmit_src_mac);
271 }
272 }
273
274 /*
275 * Update priority of skb
276 */
277 if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_PRIORITY_REMARK)) {
278 skb->priority = cm->priority;
279 }
280
281 /*
282 * Mark outgoing packet.
283 */
284 if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_MARK)) {
285 skb->mark = cm->mark;
286 }
287
Hardik S. Panchalb171d082022-06-27 13:50:03 +0530288 /*
289 * For the first packets, check if it could got fast xmit.
290 */
291 if (unlikely(!(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_FAST_XMIT_FLOW_CHECKED)
292 && (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_FAST_XMIT_DEV_ADMISSION))){
293 cm->features = netif_skb_features(skb);
294 if (likely(sfe_fast_xmit_check(skb, cm->features))) {
295 cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_FAST_XMIT;
296 }
297 cm->flags |= SFE_IPV4_CONNECTION_MATCH_FLAG_FAST_XMIT_FLOW_CHECKED;
298 }
299
300 features = cm->features;
Suhas N Bhargav592e64c2021-11-12 16:53:08 +0530301 fast_xmit = !!(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_FAST_XMIT);
302
303 rcu_read_unlock();
304 this_cpu_inc(si->stats_pcpu->packets_forwarded64);
305 prefetch(skb_shinfo(skb));
306
307 /*
308 * We do per packet condition check before we could fast xmit the
309 * packet.
310 */
311 if (likely(fast_xmit && dev_fast_xmit(skb, xmit_dev, features))) {
312 this_cpu_inc(si->stats_pcpu->packets_fast_xmited64);
313 return 1;
314 }
315
316 /*
317 * Mark that this packet has been fast forwarded.
318 */
319 skb->fast_forwarded = 1;
320
321 dev_queue_xmit(skb);
322 return 1;
323}