blob: ea9a0001f28d7cc64e56642c2e70be3debde410a [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 Shetty5dcf68c2022-01-28 20:29:21 +053032#include "sfe_pppoe.h"
Nitin Shetty2114a892022-01-28 20:03:56 +053033#include "sfe_vlan.h"
Nitin Shettye6ed5b52021-12-27 14:50:11 +053034
35/*
36 * sfe_ipv4_recv_gre()
37 * GRE tunnel packet receive and forwarding.
38 */
39int sfe_ipv4_recv_gre(struct sfe_ipv4 *si, struct sk_buff *skb, struct net_device *dev,
Nitin Shetty2114a892022-01-28 20:03:56 +053040 unsigned int len, struct iphdr *iph, unsigned int ihl, bool sync_on_find,
41 struct sfe_l2_info *l2_info, bool tun_outer)
Nitin Shettye6ed5b52021-12-27 14:50:11 +053042{
43 struct sfe_ipv4_connection_match *cm;
44 struct pptp_gre_header *pptp_hdr;
45 struct gre_base_hdr *gre_hdr;
46 struct net_device *xmit_dev;
47 __be16 dest_port = 0;
48 bool passthrough;
49 bool bridge_flow;
50 __be32 dest_ip;
51 __be32 src_ip;
52 bool hw_csum;
53 bool ret;
54 u8 ttl;
55
56 /*
57 * Is our packet too short to contain a valid GRE header?
58 */
59 if (unlikely(!pskb_may_pull(skb, sizeof(*gre_hdr) + ihl))) {
60 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_GRE_HEADER_INCOMPLETE);
61 DEBUG_TRACE("packet too short for GRE header\n");
62 return 0;
63 }
64
65
66 /*
67 * Read the source and destination IP address.
68 */
69 src_ip = iph->saddr;
70 dest_ip = iph->daddr;
71
72 rcu_read_lock();
73
74 /*
75 * Look for a connection match with 4 tuple if it is PPTP
76 */
77 gre_hdr = (struct gre_base_hdr *)(skb->data + ihl);
78
79 if ((gre_hdr->protocol == GRE_PROTO_PPP) && likely(pskb_may_pull(skb, (sizeof(*pptp_hdr) - 8) + ihl))) {
80 pptp_hdr = (struct pptp_gre_header *)(skb->data + ihl);
81 dest_port = pptp_hdr->call_id;
82 }
83
84#ifdef CONFIG_NF_FLOW_COOKIE
85 cm = si->sfe_flow_cookie_table[skb->flow_cookie & SFE_FLOW_COOKIE_MASK].match;
86 if (unlikely(!cm)) {
87 cm = sfe_ipv4_find_connection_match_rcu(si, dev, IPPROTO_GRE, src_ip, 0, dest_ip, dest_port);
88 }
89#else
90 cm = sfe_ipv4_find_connection_match_rcu(si, dev, IPPROTO_GRE, src_ip, 0, dest_ip, dest_port);
91#endif
92
93 if (unlikely(!cm)) {
94 rcu_read_unlock();
95 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_GRE_NO_CONNECTION);
96 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));
97 return 0;
98 }
99
100 /*
101 * Source interface validate.
102 */
103 if (unlikely((cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_SRC_INTERFACE_CHECK) && (cm->match_dev != dev))) {
Murat Sezgin9c538972022-05-17 13:33:17 -0700104 if (!(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_SRC_INTERFACE_CHECK_NO_FLUSH)) {
105 struct sfe_ipv4_connection *c = cm->connection;
106 int ret;
Nitin Shettye6ed5b52021-12-27 14:50:11 +0530107
Murat Sezgin9c538972022-05-17 13:33:17 -0700108 DEBUG_TRACE("flush on source interface check failure\n");
109 spin_lock_bh(&si->lock);
110 ret = sfe_ipv4_remove_connection(si, c);
111 spin_unlock_bh(&si->lock);
Nitin Shettye6ed5b52021-12-27 14:50:11 +0530112
Murat Sezgin9c538972022-05-17 13:33:17 -0700113 if (ret) {
114 sfe_ipv4_flush_connection(si, c, SFE_SYNC_REASON_FLUSH);
115 }
Nitin Shettye6ed5b52021-12-27 14:50:11 +0530116 }
117 rcu_read_unlock();
118 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_INVALID_SRC_IFACE);
Murat Sezgin9c538972022-05-17 13:33:17 -0700119 DEBUG_TRACE("exception the packet on source interface check failure\n");
Nitin Shettye6ed5b52021-12-27 14:50:11 +0530120 return 0;
121 }
122
123 passthrough = cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_PASSTHROUGH;
124
125 /*
126 * If our packet has been marked as "sync on find" we can't actually
127 * forward it in the fast path, but now that we've found an associated
128 * connection we need sync its status before exception it to slow path unless
129 * it is passthrough (packets not directed to DUT) packet.
130 * TODO: revisit to ensure that pass through traffic is not bypassing firewall for fragmented cases
131 */
132 if (unlikely(sync_on_find) && !passthrough) {
133 sfe_ipv4_sync_status(si, cm->connection, SFE_SYNC_REASON_STATS);
134 rcu_read_unlock();
135 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_GRE_IP_OPTIONS_OR_INITIAL_FRAGMENT);
136 DEBUG_TRACE("%px: sfe: sync on find\n", cm);
137 return 0;
138 }
139
Nitin Shetty2114a892022-01-28 20:03:56 +0530140 /*
141 * Do we expect an ingress VLAN tag for this flow?
142 */
143 if (unlikely(!sfe_vlan_validate_ingress_tag(skb, cm->ingress_vlan_hdr_cnt, cm->ingress_vlan_hdr, l2_info))) {
144 rcu_read_unlock();
145 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_INGRESS_VLAN_TAG_MISMATCH);
146 DEBUG_TRACE("VLAN tag mismatch. skb=%px\n", skb);
147 return 0;
148 }
149
Nitin Shettye6ed5b52021-12-27 14:50:11 +0530150 bridge_flow = !!(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_BRIDGE_FLOW);
151
152 /*
153 * Does our TTL allow forwarding?
154 */
155 ttl = iph->ttl;
156 if (!bridge_flow && (ttl < 2) && passthrough) {
157 sfe_ipv4_sync_status(si, cm->connection, SFE_SYNC_REASON_STATS);
158 rcu_read_unlock();
159
160 DEBUG_TRACE("%px: sfe: TTL too low\n", skb);
161 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_GRE_SMALL_TTL);
162 return 0;
163 }
164
165 /*
166 * From this point on we're good to modify the packet.
167 */
168
169 /*
170 * Check if skb was cloned. If it was, unshare it. Because
171 * the data area is going to be written in this path and we don't want to
172 * change the cloned skb's data section.
173 */
174 if (unlikely(skb_cloned(skb))) {
175 DEBUG_TRACE("%px: skb is a cloned skb\n", skb);
176 skb = skb_unshare(skb, GFP_ATOMIC);
177 if (!skb) {
178 DEBUG_WARN("Failed to unshare the cloned skb\n");
179 rcu_read_unlock();
180 return 1;
181 }
182
183 /*
184 * Update the iph and udph pointers with the unshared skb's data area.
185 */
186 iph = (struct iphdr *)skb->data;
187 }
188
189 /*
Nitin Shetty5dcf68c2022-01-28 20:29:21 +0530190 * For PPPoE packets, match server MAC and session id
Nitin Shetty2114a892022-01-28 20:03:56 +0530191 */
Nitin Shetty5dcf68c2022-01-28 20:29:21 +0530192 if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_DECAP)) {
193 struct ethhdr *eth;
194 bool pppoe_match;
Nitin Shetty2114a892022-01-28 20:03:56 +0530195
Nitin Shetty5dcf68c2022-01-28 20:29:21 +0530196 if (unlikely(!sfe_l2_parse_flag_check(l2_info, SFE_L2_PARSE_FLAGS_PPPOE_INGRESS))) {
197 rcu_read_unlock();
198 DEBUG_TRACE("%px: PPPoE header not present in packet for PPPoE rule\n", skb);
199 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_INCORRECT_PPPOE_PARSING);
200 return 0;
201 }
202
203 eth = eth_hdr(skb);
204
205 pppoe_match = (cm->pppoe_session_id == sfe_l2_pppoe_session_id_get(l2_info)) &&
206 ether_addr_equal((u8*)cm->pppoe_remote_mac, (u8 *)eth->h_source);
207
208 if (unlikely(!pppoe_match)) {
209 DEBUG_TRACE("%px: PPPoE session ID %d and %d or MAC %pM and %pM did not match\n",
210 skb, cm->pppoe_session_id, sfe_l2_pppoe_session_id_get(l2_info),
211 cm->pppoe_remote_mac, eth->h_source);
212 rcu_read_unlock();
213 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_INVALID_PPPOE_SESSION);
214 return 0;
215 }
216
217 skb->protocol = htons(l2_info->protocol);
218 this_cpu_inc(si->stats_pcpu->pppoe_decap_packets_forwarded64);
219 } else if (unlikely(sfe_l2_parse_flag_check(l2_info, SFE_L2_PARSE_FLAGS_PPPOE_INGRESS))) {
220
221 /*
222 * If packet contains PPPoE header but CME doesn't contain PPPoE flag yet we are exceptioning
223 * the packet to linux
224 */
225 if (unlikely(!(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_BRIDGE_FLOW))) {
226 rcu_read_unlock();
227 DEBUG_TRACE("%px: CME doesn't contain PPPoE flag but packet has PPPoE header\n", skb);
228 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_PPPOE_NOT_SET_IN_CME);
229 return 0;
230
231 }
232
233 /*
234 * For bridged flows when packet contains PPPoE header, restore the header back and forward
235 * to xmit interface
236 */
237 __skb_push(skb, (sizeof(struct pppoe_hdr) + sizeof(struct sfe_ppp_hdr)));
238
239 this_cpu_inc(si->stats_pcpu->pppoe_bridge_packets_forwarded64);
240 }
Nitin Shettye6ed5b52021-12-27 14:50:11 +0530241
242 /*
243 * protocol handler will be valid only in decap-path.
244 */
245 if (cm->proto) {
246 struct net_protocol *ipprot = cm->proto;
Nitin Shetty2114a892022-01-28 20:03:56 +0530247 skb_reset_network_header(skb);
Nitin Shettye6ed5b52021-12-27 14:50:11 +0530248 skb_pull(skb, ihl);
249 skb_reset_transport_header(skb);
250 skb->fast_forwarded = 1;
251
252 ret = ipprot->handler(skb);
253 if (ret) {
254 this_cpu_inc(si->stats_pcpu->packets_not_forwarded64);
255 rcu_read_unlock();
256 DEBUG_TRACE("GRE handler returned error %u\n", ret);
257 return 1;
258 }
259
260 /*
261 * Update traffic stats
262 */
263 atomic_inc(&cm->rx_packet_count);
264 atomic_add(len, &cm->rx_byte_count);
265
266 this_cpu_inc(si->stats_pcpu->packets_forwarded64);
267 rcu_read_unlock();
268 return 1;
269 }
270
271 /*
Nitin Shetty5dcf68c2022-01-28 20:29:21 +0530272 * Check if skb has enough headroom to write L2 headers
273 */
274 if (unlikely(skb_headroom(skb) < cm->l2_hdr_size)) {
275 rcu_read_unlock();
276 DEBUG_WARN("%px: Not enough headroom: %u\n", skb, skb_headroom(skb));
277 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_NO_HEADROOM);
278 return 0;
279 }
280
281 /*
Nitin Shettye6ed5b52021-12-27 14:50:11 +0530282 * If our packet is larger than the MTU of the transmit interface then
283 * we can't forward it easily.
284 */
285 if (unlikely(len > cm->xmit_dev_mtu)) {
286 sfe_ipv4_sync_status(si, cm->connection, SFE_SYNC_REASON_STATS);
287 rcu_read_unlock();
288 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_GRE_NEEDS_FRAGMENTATION);
289 DEBUG_TRACE("%px: sfe: larger than MTU\n", cm);
290 return 0;
291 }
292
293 /*
294 * Decrement our TTL
295 */
296 iph->ttl = (ttl - (u8)(!bridge_flow && !tun_outer));
297
298 /*
299 * Update DSCP
300 */
301 if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_DSCP_REMARK)) {
302 iph->tos = (iph->tos & SFE_IPV4_DSCP_MASK) | cm->dscp;
303 }
304
305 /*
Nitin Shetty5dcf68c2022-01-28 20:29:21 +0530306 * Enable HW csum if rx checksum is verified and xmit interface is CSUM offload capable.
307 */
308 hw_csum = !!(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_CSUM_OFFLOAD) && (skb->ip_summed == CHECKSUM_UNNECESSARY);
309
310 /*
Nitin Shettye6ed5b52021-12-27 14:50:11 +0530311 * Replace the IP checksum.
312 */
313 if (likely(hw_csum)) {
314 skb->ip_summed = CHECKSUM_PARTIAL;
315 } else {
316 iph->check = sfe_ipv4_gen_ip_csum(iph);
317 }
318
319 /*
320 * Update traffic stats
321 */
322 atomic_inc(&cm->rx_packet_count);
323 atomic_add(len, &cm->rx_byte_count);
324
325 xmit_dev = cm->xmit_dev;
326 skb->dev = xmit_dev;
327
328 /*
Nitin Shetty5dcf68c2022-01-28 20:29:21 +0530329 * For PPPoE flows, add PPPoE header before L2 header is added.
330 */
331 if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_ENCAP)) {
332 sfe_pppoe_add_header(skb, cm->pppoe_session_id, PPP_IP);
333 this_cpu_inc(si->stats_pcpu->pppoe_encap_packets_forwarded64);
334 }
335
336 /*
Nitin Shetty2114a892022-01-28 20:03:56 +0530337 * Check to see if we need to add VLAN tags
338 */
339 if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_INSERT_EGRESS_VLAN_TAG)) {
340 sfe_vlan_add_tag(skb, cm->egress_vlan_hdr_cnt, cm->egress_vlan_hdr);
341 }
342
343 /*
Nitin Shettye6ed5b52021-12-27 14:50:11 +0530344 * For the simple case we write this really fast.
345 */
346 if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR) {
347 struct ethhdr *eth = (struct ethhdr *)__skb_push(skb, ETH_HLEN);
Nitin Shetty5dcf68c2022-01-28 20:29:21 +0530348 eth->h_proto = skb->protocol;
Nitin Shettye6ed5b52021-12-27 14:50:11 +0530349 ether_addr_copy((u8 *)eth->h_dest, (u8 *)cm->xmit_dest_mac);
350 ether_addr_copy((u8 *)eth->h_source, (u8 *)cm->xmit_src_mac);
351 } else if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_L2_HDR) {
Nitin Shetty5dcf68c2022-01-28 20:29:21 +0530352 dev_hard_header(skb, xmit_dev, ntohs(skb->protocol), cm->xmit_dest_mac, cm->xmit_src_mac, len);
Nitin Shettye6ed5b52021-12-27 14:50:11 +0530353 }
354
355 /*
356 * Update priority of skb.
357 */
358 if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_PRIORITY_REMARK)) {
359 skb->priority = cm->priority;
360 }
361
362 /*
363 * Mark outgoing packet.
364 */
365 if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_MARK)) {
Ken Zhu7e38d1a2021-11-30 17:31:46 -0800366 skb->mark = cm->mark;
Nitin Shettye6ed5b52021-12-27 14:50:11 +0530367 }
368
369 this_cpu_inc(si->stats_pcpu->packets_forwarded64);
370
371 rcu_read_unlock();
372
373 /*
374 * We're going to check for GSO flags when we transmit the packet so
375 * start fetching the necessary cache line now.
376 */
377 prefetch(skb_shinfo(skb));
378
379 /*
380 * Mark that this packet has been fast forwarded.
381 */
382 skb->fast_forwarded = 1;
383
384 /*
385 * Send the packet on its way.
386 */
387 dev_queue_xmit(skb);
388
389 return 1;
390}