blob: 084ea3b262c55d606ffdf55f393f5c97bb9c3b34 [file] [log] [blame]
Nitin Shettye6ed5b52021-12-27 14:50:11 +05301/*
2 * sfe_ipv4_gre.c
3 * Shortcut forwarding engine file for IPv4 GRE
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>
Nitin Shettye6ed5b52021-12-27 14:50:11 +053021#include <net/protocol.h>
Nitin Shettye6ed5b52021-12-27 14:50:11 +053022#include <linux/etherdevice.h>
23#include <linux/lockdep.h>
Nitin Shetty2114a892022-01-28 20:03:56 +053024#include <net/gre.h>
25#include <net/pptp.h>
Nitin Shettye6ed5b52021-12-27 14:50:11 +053026
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"
Nitin Shetty2114a892022-01-28 20:03:56 +053032#include "sfe_vlan.h"
Nitin Shettye6ed5b52021-12-27 14:50:11 +053033
34/*
35 * sfe_ipv4_recv_gre()
36 * GRE tunnel packet receive and forwarding.
37 */
38int sfe_ipv4_recv_gre(struct sfe_ipv4 *si, struct sk_buff *skb, struct net_device *dev,
Nitin Shetty2114a892022-01-28 20:03:56 +053039 unsigned int len, struct iphdr *iph, unsigned int ihl, bool sync_on_find,
40 struct sfe_l2_info *l2_info, bool tun_outer)
Nitin Shettye6ed5b52021-12-27 14:50:11 +053041{
42 struct sfe_ipv4_connection_match *cm;
43 struct pptp_gre_header *pptp_hdr;
44 struct gre_base_hdr *gre_hdr;
45 struct net_device *xmit_dev;
46 __be16 dest_port = 0;
47 bool passthrough;
48 bool bridge_flow;
49 __be32 dest_ip;
50 __be32 src_ip;
51 bool hw_csum;
52 bool ret;
53 u8 ttl;
54
55 /*
56 * Is our packet too short to contain a valid GRE header?
57 */
58 if (unlikely(!pskb_may_pull(skb, sizeof(*gre_hdr) + ihl))) {
59 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_GRE_HEADER_INCOMPLETE);
60 DEBUG_TRACE("packet too short for GRE header\n");
61 return 0;
62 }
63
64
65 /*
66 * Read the source and destination IP address.
67 */
68 src_ip = iph->saddr;
69 dest_ip = iph->daddr;
70
71 rcu_read_lock();
72
73 /*
74 * Look for a connection match with 4 tuple if it is PPTP
75 */
76 gre_hdr = (struct gre_base_hdr *)(skb->data + ihl);
77
78 if ((gre_hdr->protocol == GRE_PROTO_PPP) && likely(pskb_may_pull(skb, (sizeof(*pptp_hdr) - 8) + ihl))) {
79 pptp_hdr = (struct pptp_gre_header *)(skb->data + ihl);
80 dest_port = pptp_hdr->call_id;
81 }
82
83#ifdef CONFIG_NF_FLOW_COOKIE
84 cm = si->sfe_flow_cookie_table[skb->flow_cookie & SFE_FLOW_COOKIE_MASK].match;
85 if (unlikely(!cm)) {
86 cm = sfe_ipv4_find_connection_match_rcu(si, dev, IPPROTO_GRE, src_ip, 0, dest_ip, dest_port);
87 }
88#else
89 cm = sfe_ipv4_find_connection_match_rcu(si, dev, IPPROTO_GRE, src_ip, 0, dest_ip, dest_port);
90#endif
91
92 if (unlikely(!cm)) {
93 rcu_read_unlock();
94 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_GRE_NO_CONNECTION);
95 DEBUG_INFO("no GRE connection match found dev %s src ip %pI4 dest ip %pI4 port %d\n", dev->name, &src_ip, &dest_ip, ntohs(dest_port));
96 return 0;
97 }
98
99 /*
100 * Source interface validate.
101 */
102 if (unlikely((cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_SRC_INTERFACE_CHECK) && (cm->match_dev != dev))) {
103 struct sfe_ipv4_connection *c = cm->connection;
104 int ret;
105
106 spin_lock_bh(&si->lock);
107 ret = sfe_ipv4_remove_connection(si, c);
108 spin_unlock_bh(&si->lock);
109
110 if (ret) {
111 sfe_ipv4_flush_connection(si, c, SFE_SYNC_REASON_FLUSH);
112 }
113 rcu_read_unlock();
114 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_INVALID_SRC_IFACE);
115 DEBUG_TRACE("flush on wrong source interface check failure\n");
116 return 0;
117 }
118
119 passthrough = cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_PASSTHROUGH;
120
121 /*
122 * If our packet has been marked as "sync on find" we can't actually
123 * forward it in the fast path, but now that we've found an associated
124 * connection we need sync its status before exception it to slow path unless
125 * it is passthrough (packets not directed to DUT) packet.
126 * TODO: revisit to ensure that pass through traffic is not bypassing firewall for fragmented cases
127 */
128 if (unlikely(sync_on_find) && !passthrough) {
129 sfe_ipv4_sync_status(si, cm->connection, SFE_SYNC_REASON_STATS);
130 rcu_read_unlock();
131 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_GRE_IP_OPTIONS_OR_INITIAL_FRAGMENT);
132 DEBUG_TRACE("%px: sfe: sync on find\n", cm);
133 return 0;
134 }
135
Nitin Shetty2114a892022-01-28 20:03:56 +0530136 /*
137 * Do we expect an ingress VLAN tag for this flow?
138 */
139 if (unlikely(!sfe_vlan_validate_ingress_tag(skb, cm->ingress_vlan_hdr_cnt, cm->ingress_vlan_hdr, l2_info))) {
140 rcu_read_unlock();
141 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_INGRESS_VLAN_TAG_MISMATCH);
142 DEBUG_TRACE("VLAN tag mismatch. skb=%px\n", skb);
143 return 0;
144 }
145
Nitin Shettye6ed5b52021-12-27 14:50:11 +0530146 bridge_flow = !!(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_BRIDGE_FLOW);
147
148 /*
149 * Does our TTL allow forwarding?
150 */
151 ttl = iph->ttl;
152 if (!bridge_flow && (ttl < 2) && passthrough) {
153 sfe_ipv4_sync_status(si, cm->connection, SFE_SYNC_REASON_STATS);
154 rcu_read_unlock();
155
156 DEBUG_TRACE("%px: sfe: TTL too low\n", skb);
157 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_GRE_SMALL_TTL);
158 return 0;
159 }
160
161 /*
162 * From this point on we're good to modify the packet.
163 */
164
165 /*
166 * Check if skb was cloned. If it was, unshare it. Because
167 * the data area is going to be written in this path and we don't want to
168 * change the cloned skb's data section.
169 */
170 if (unlikely(skb_cloned(skb))) {
171 DEBUG_TRACE("%px: skb is a cloned skb\n", skb);
172 skb = skb_unshare(skb, GFP_ATOMIC);
173 if (!skb) {
174 DEBUG_WARN("Failed to unshare the cloned skb\n");
175 rcu_read_unlock();
176 return 1;
177 }
178
179 /*
180 * Update the iph and udph pointers with the unshared skb's data area.
181 */
182 iph = (struct iphdr *)skb->data;
183 }
184
185 /*
Nitin Shetty2114a892022-01-28 20:03:56 +0530186 * Check if skb has enough headroom to write L2 headers
187 */
188 if (unlikely(skb_headroom(skb) < cm->l2_hdr_size)) {
189 rcu_read_unlock();
190 DEBUG_WARN("%px: Not enough headroom: %u\n", skb, skb_headroom(skb));
191 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_NO_HEADROOM);
192 return 0;
193 }
194
195 /*
Nitin Shettye6ed5b52021-12-27 14:50:11 +0530196 * Enable HW csum if rx checksum is verified and xmit interface is CSUM offload capable.
197 */
198 hw_csum = !!(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_CSUM_OFFLOAD) && (skb->ip_summed == CHECKSUM_UNNECESSARY);
199
200 /*
201 * protocol handler will be valid only in decap-path.
202 */
203 if (cm->proto) {
204 struct net_protocol *ipprot = cm->proto;
Nitin Shetty2114a892022-01-28 20:03:56 +0530205 skb_reset_network_header(skb);
Nitin Shettye6ed5b52021-12-27 14:50:11 +0530206 skb_pull(skb, ihl);
207 skb_reset_transport_header(skb);
208 skb->fast_forwarded = 1;
209
210 ret = ipprot->handler(skb);
211 if (ret) {
212 this_cpu_inc(si->stats_pcpu->packets_not_forwarded64);
213 rcu_read_unlock();
214 DEBUG_TRACE("GRE handler returned error %u\n", ret);
215 return 1;
216 }
217
218 /*
219 * Update traffic stats
220 */
221 atomic_inc(&cm->rx_packet_count);
222 atomic_add(len, &cm->rx_byte_count);
223
224 this_cpu_inc(si->stats_pcpu->packets_forwarded64);
225 rcu_read_unlock();
226 return 1;
227 }
228
229 /*
230 * If our packet is larger than the MTU of the transmit interface then
231 * we can't forward it easily.
232 */
233 if (unlikely(len > cm->xmit_dev_mtu)) {
234 sfe_ipv4_sync_status(si, cm->connection, SFE_SYNC_REASON_STATS);
235 rcu_read_unlock();
236 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_GRE_NEEDS_FRAGMENTATION);
237 DEBUG_TRACE("%px: sfe: larger than MTU\n", cm);
238 return 0;
239 }
240
241 /*
242 * Decrement our TTL
243 */
244 iph->ttl = (ttl - (u8)(!bridge_flow && !tun_outer));
245
246 /*
247 * Update DSCP
248 */
249 if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_DSCP_REMARK)) {
250 iph->tos = (iph->tos & SFE_IPV4_DSCP_MASK) | cm->dscp;
251 }
252
253 /*
254 * Replace the IP checksum.
255 */
256 if (likely(hw_csum)) {
257 skb->ip_summed = CHECKSUM_PARTIAL;
258 } else {
259 iph->check = sfe_ipv4_gen_ip_csum(iph);
260 }
261
262 /*
263 * Update traffic stats
264 */
265 atomic_inc(&cm->rx_packet_count);
266 atomic_add(len, &cm->rx_byte_count);
267
268 xmit_dev = cm->xmit_dev;
269 skb->dev = xmit_dev;
270
271 /*
Nitin Shetty2114a892022-01-28 20:03:56 +0530272 * Check to see if we need to add VLAN tags
273 */
274 if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_INSERT_EGRESS_VLAN_TAG)) {
275 sfe_vlan_add_tag(skb, cm->egress_vlan_hdr_cnt, cm->egress_vlan_hdr);
276 }
277
278 /*
Nitin Shettye6ed5b52021-12-27 14:50:11 +0530279 * For the simple case we write this really fast.
280 */
281 if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR) {
282 struct ethhdr *eth = (struct ethhdr *)__skb_push(skb, ETH_HLEN);
283 eth->h_proto = htons(ETH_P_IP);
284 ether_addr_copy((u8 *)eth->h_dest, (u8 *)cm->xmit_dest_mac);
285 ether_addr_copy((u8 *)eth->h_source, (u8 *)cm->xmit_src_mac);
286 } else if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_L2_HDR) {
287 dev_hard_header(skb, xmit_dev, ETH_P_IP, cm->xmit_dest_mac, cm->xmit_src_mac, len);
288 }
289
290 /*
291 * Update priority of skb.
292 */
293 if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_PRIORITY_REMARK)) {
294 skb->priority = cm->priority;
295 }
296
297 /*
298 * Mark outgoing packet.
299 */
300 if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_MARK)) {
Ken Zhu7e38d1a2021-11-30 17:31:46 -0800301 skb->mark = cm->mark;
Nitin Shettye6ed5b52021-12-27 14:50:11 +0530302 }
303
304 this_cpu_inc(si->stats_pcpu->packets_forwarded64);
305
306 rcu_read_unlock();
307
308 /*
309 * We're going to check for GSO flags when we transmit the packet so
310 * start fetching the necessary cache line now.
311 */
312 prefetch(skb_shinfo(skb));
313
314 /*
315 * Mark that this packet has been fast forwarded.
316 */
317 skb->fast_forwarded = 1;
318
319 /*
320 * Send the packet on its way.
321 */
322 dev_queue_xmit(skb);
323
324 return 1;
325}