blob: da05d69d8918bfa89fc6def82402706db1db6ccb [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))) {
104 struct sfe_ipv4_connection *c = cm->connection;
105 int ret;
106
107 spin_lock_bh(&si->lock);
108 ret = sfe_ipv4_remove_connection(si, c);
109 spin_unlock_bh(&si->lock);
110
111 if (ret) {
112 sfe_ipv4_flush_connection(si, c, SFE_SYNC_REASON_FLUSH);
113 }
114 rcu_read_unlock();
115 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_INVALID_SRC_IFACE);
116 DEBUG_TRACE("flush on wrong source interface check failure\n");
117 return 0;
118 }
119
120 passthrough = cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_PASSTHROUGH;
121
122 /*
123 * If our packet has been marked as "sync on find" we can't actually
124 * forward it in the fast path, but now that we've found an associated
125 * connection we need sync its status before exception it to slow path unless
126 * it is passthrough (packets not directed to DUT) packet.
127 * TODO: revisit to ensure that pass through traffic is not bypassing firewall for fragmented cases
128 */
129 if (unlikely(sync_on_find) && !passthrough) {
130 sfe_ipv4_sync_status(si, cm->connection, SFE_SYNC_REASON_STATS);
131 rcu_read_unlock();
132 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_GRE_IP_OPTIONS_OR_INITIAL_FRAGMENT);
133 DEBUG_TRACE("%px: sfe: sync on find\n", cm);
134 return 0;
135 }
136
Nitin Shetty2114a892022-01-28 20:03:56 +0530137 /*
138 * Do we expect an ingress VLAN tag for this flow?
139 */
140 if (unlikely(!sfe_vlan_validate_ingress_tag(skb, cm->ingress_vlan_hdr_cnt, cm->ingress_vlan_hdr, l2_info))) {
141 rcu_read_unlock();
142 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_INGRESS_VLAN_TAG_MISMATCH);
143 DEBUG_TRACE("VLAN tag mismatch. skb=%px\n", skb);
144 return 0;
145 }
146
Nitin Shettye6ed5b52021-12-27 14:50:11 +0530147 bridge_flow = !!(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_BRIDGE_FLOW);
148
149 /*
150 * Does our TTL allow forwarding?
151 */
152 ttl = iph->ttl;
153 if (!bridge_flow && (ttl < 2) && passthrough) {
154 sfe_ipv4_sync_status(si, cm->connection, SFE_SYNC_REASON_STATS);
155 rcu_read_unlock();
156
157 DEBUG_TRACE("%px: sfe: TTL too low\n", skb);
158 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_GRE_SMALL_TTL);
159 return 0;
160 }
161
162 /*
163 * From this point on we're good to modify the packet.
164 */
165
166 /*
167 * Check if skb was cloned. If it was, unshare it. Because
168 * the data area is going to be written in this path and we don't want to
169 * change the cloned skb's data section.
170 */
171 if (unlikely(skb_cloned(skb))) {
172 DEBUG_TRACE("%px: skb is a cloned skb\n", skb);
173 skb = skb_unshare(skb, GFP_ATOMIC);
174 if (!skb) {
175 DEBUG_WARN("Failed to unshare the cloned skb\n");
176 rcu_read_unlock();
177 return 1;
178 }
179
180 /*
181 * Update the iph and udph pointers with the unshared skb's data area.
182 */
183 iph = (struct iphdr *)skb->data;
184 }
185
186 /*
Nitin Shetty5dcf68c2022-01-28 20:29:21 +0530187 * For PPPoE packets, match server MAC and session id
Nitin Shetty2114a892022-01-28 20:03:56 +0530188 */
Nitin Shetty5dcf68c2022-01-28 20:29:21 +0530189 if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_DECAP)) {
190 struct ethhdr *eth;
191 bool pppoe_match;
Nitin Shetty2114a892022-01-28 20:03:56 +0530192
Nitin Shetty5dcf68c2022-01-28 20:29:21 +0530193 if (unlikely(!sfe_l2_parse_flag_check(l2_info, SFE_L2_PARSE_FLAGS_PPPOE_INGRESS))) {
194 rcu_read_unlock();
195 DEBUG_TRACE("%px: PPPoE header not present in packet for PPPoE rule\n", skb);
196 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_INCORRECT_PPPOE_PARSING);
197 return 0;
198 }
199
200 eth = eth_hdr(skb);
201
202 pppoe_match = (cm->pppoe_session_id == sfe_l2_pppoe_session_id_get(l2_info)) &&
203 ether_addr_equal((u8*)cm->pppoe_remote_mac, (u8 *)eth->h_source);
204
205 if (unlikely(!pppoe_match)) {
206 DEBUG_TRACE("%px: PPPoE session ID %d and %d or MAC %pM and %pM did not match\n",
207 skb, cm->pppoe_session_id, sfe_l2_pppoe_session_id_get(l2_info),
208 cm->pppoe_remote_mac, eth->h_source);
209 rcu_read_unlock();
210 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_INVALID_PPPOE_SESSION);
211 return 0;
212 }
213
214 skb->protocol = htons(l2_info->protocol);
215 this_cpu_inc(si->stats_pcpu->pppoe_decap_packets_forwarded64);
216 } else if (unlikely(sfe_l2_parse_flag_check(l2_info, SFE_L2_PARSE_FLAGS_PPPOE_INGRESS))) {
217
218 /*
219 * If packet contains PPPoE header but CME doesn't contain PPPoE flag yet we are exceptioning
220 * the packet to linux
221 */
222 if (unlikely(!(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_BRIDGE_FLOW))) {
223 rcu_read_unlock();
224 DEBUG_TRACE("%px: CME doesn't contain PPPoE flag but packet has PPPoE header\n", skb);
225 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_PPPOE_NOT_SET_IN_CME);
226 return 0;
227
228 }
229
230 /*
231 * For bridged flows when packet contains PPPoE header, restore the header back and forward
232 * to xmit interface
233 */
234 __skb_push(skb, (sizeof(struct pppoe_hdr) + sizeof(struct sfe_ppp_hdr)));
235
236 this_cpu_inc(si->stats_pcpu->pppoe_bridge_packets_forwarded64);
237 }
Nitin Shettye6ed5b52021-12-27 14:50:11 +0530238
239 /*
240 * protocol handler will be valid only in decap-path.
241 */
242 if (cm->proto) {
243 struct net_protocol *ipprot = cm->proto;
Nitin Shetty2114a892022-01-28 20:03:56 +0530244 skb_reset_network_header(skb);
Nitin Shettye6ed5b52021-12-27 14:50:11 +0530245 skb_pull(skb, ihl);
246 skb_reset_transport_header(skb);
247 skb->fast_forwarded = 1;
248
249 ret = ipprot->handler(skb);
250 if (ret) {
251 this_cpu_inc(si->stats_pcpu->packets_not_forwarded64);
252 rcu_read_unlock();
253 DEBUG_TRACE("GRE handler returned error %u\n", ret);
254 return 1;
255 }
256
257 /*
258 * Update traffic stats
259 */
260 atomic_inc(&cm->rx_packet_count);
261 atomic_add(len, &cm->rx_byte_count);
262
263 this_cpu_inc(si->stats_pcpu->packets_forwarded64);
264 rcu_read_unlock();
265 return 1;
266 }
267
268 /*
Nitin Shetty5dcf68c2022-01-28 20:29:21 +0530269 * Check if skb has enough headroom to write L2 headers
270 */
271 if (unlikely(skb_headroom(skb) < cm->l2_hdr_size)) {
272 rcu_read_unlock();
273 DEBUG_WARN("%px: Not enough headroom: %u\n", skb, skb_headroom(skb));
274 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_NO_HEADROOM);
275 return 0;
276 }
277
278 /*
Nitin Shettye6ed5b52021-12-27 14:50:11 +0530279 * If our packet is larger than the MTU of the transmit interface then
280 * we can't forward it easily.
281 */
282 if (unlikely(len > cm->xmit_dev_mtu)) {
283 sfe_ipv4_sync_status(si, cm->connection, SFE_SYNC_REASON_STATS);
284 rcu_read_unlock();
285 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_GRE_NEEDS_FRAGMENTATION);
286 DEBUG_TRACE("%px: sfe: larger than MTU\n", cm);
287 return 0;
288 }
289
290 /*
291 * Decrement our TTL
292 */
293 iph->ttl = (ttl - (u8)(!bridge_flow && !tun_outer));
294
295 /*
296 * Update DSCP
297 */
298 if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_DSCP_REMARK)) {
299 iph->tos = (iph->tos & SFE_IPV4_DSCP_MASK) | cm->dscp;
300 }
301
302 /*
Nitin Shetty5dcf68c2022-01-28 20:29:21 +0530303 * Enable HW csum if rx checksum is verified and xmit interface is CSUM offload capable.
304 */
305 hw_csum = !!(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_CSUM_OFFLOAD) && (skb->ip_summed == CHECKSUM_UNNECESSARY);
306
307 /*
Nitin Shettye6ed5b52021-12-27 14:50:11 +0530308 * Replace the IP checksum.
309 */
310 if (likely(hw_csum)) {
311 skb->ip_summed = CHECKSUM_PARTIAL;
312 } else {
313 iph->check = sfe_ipv4_gen_ip_csum(iph);
314 }
315
316 /*
317 * Update traffic stats
318 */
319 atomic_inc(&cm->rx_packet_count);
320 atomic_add(len, &cm->rx_byte_count);
321
322 xmit_dev = cm->xmit_dev;
323 skb->dev = xmit_dev;
324
325 /*
Nitin Shetty5dcf68c2022-01-28 20:29:21 +0530326 * For PPPoE flows, add PPPoE header before L2 header is added.
327 */
328 if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_PPPOE_ENCAP)) {
329 sfe_pppoe_add_header(skb, cm->pppoe_session_id, PPP_IP);
330 this_cpu_inc(si->stats_pcpu->pppoe_encap_packets_forwarded64);
331 }
332
333 /*
Nitin Shetty2114a892022-01-28 20:03:56 +0530334 * Check to see if we need to add VLAN tags
335 */
336 if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_INSERT_EGRESS_VLAN_TAG)) {
337 sfe_vlan_add_tag(skb, cm->egress_vlan_hdr_cnt, cm->egress_vlan_hdr);
338 }
339
340 /*
Nitin Shettye6ed5b52021-12-27 14:50:11 +0530341 * For the simple case we write this really fast.
342 */
343 if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR) {
344 struct ethhdr *eth = (struct ethhdr *)__skb_push(skb, ETH_HLEN);
Nitin Shetty5dcf68c2022-01-28 20:29:21 +0530345 eth->h_proto = skb->protocol;
Nitin Shettye6ed5b52021-12-27 14:50:11 +0530346 ether_addr_copy((u8 *)eth->h_dest, (u8 *)cm->xmit_dest_mac);
347 ether_addr_copy((u8 *)eth->h_source, (u8 *)cm->xmit_src_mac);
348 } else if (cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_L2_HDR) {
Nitin Shetty5dcf68c2022-01-28 20:29:21 +0530349 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 +0530350 }
351
352 /*
353 * Update priority of skb.
354 */
355 if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_PRIORITY_REMARK)) {
356 skb->priority = cm->priority;
357 }
358
359 /*
360 * Mark outgoing packet.
361 */
362 if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_MARK)) {
Ken Zhu7e38d1a2021-11-30 17:31:46 -0800363 skb->mark = cm->mark;
Nitin Shettye6ed5b52021-12-27 14:50:11 +0530364 }
365
366 this_cpu_inc(si->stats_pcpu->packets_forwarded64);
367
368 rcu_read_unlock();
369
370 /*
371 * We're going to check for GSO flags when we transmit the packet so
372 * start fetching the necessary cache line now.
373 */
374 prefetch(skb_shinfo(skb));
375
376 /*
377 * Mark that this packet has been fast forwarded.
378 */
379 skb->fast_forwarded = 1;
380
381 /*
382 * Send the packet on its way.
383 */
384 dev_queue_xmit(skb);
385
386 return 1;
387}