blob: eae7d56d7947ef1667f290dc3cafcb116c21b9f8 [file] [log] [blame]
Nitin Shettye6ed5b52021-12-27 14:50:11 +05301/*
2 * sfe_ipv6_gre.c
3 * Shortcut forwarding engine file for IPv6 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>
21#include <net/gre.h>
22#include <net/protocol.h>
23#include <linux/etherdevice.h>
24#include <linux/version.h>
25#include <net/ip6_checksum.h>
26
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"
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_ipv6_recv_gre()
37 * Handle GRE packet receives and forwarding.
38 */
39int sfe_ipv6_recv_gre(struct sfe_ipv6 *si, struct sk_buff *skb, struct net_device *dev,
Nitin Shetty2114a892022-01-28 20:03:56 +053040 unsigned int len, struct ipv6hdr *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_ipv6_connection_match *cm;
44 struct sfe_ipv6_addr *dest_ip;
45 struct sfe_ipv6_addr *src_ip;
46 struct net_device *xmit_dev;
47 bool bridge_flow;
48 bool passthrough;
49 bool ret;
50
51 /*
52 * Is our packet too short to contain a valid UDP header?
53 */
54 if (!pskb_may_pull(skb, (sizeof(struct gre_base_hdr) + ihl))) {
55
56 sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_GRE_HEADER_INCOMPLETE);
57 DEBUG_TRACE("packet too short for GRE header\n");
58 return 0;
59 }
60
61 /*
62 * Read the IP address and port information. Read the IP header data first
63 * because we've almost certainly got that in the cache. We may not yet have
64 * the UDP header cached though so allow more time for any prefetching.
65 */
66 src_ip = (struct sfe_ipv6_addr *)iph->saddr.s6_addr32;
67 dest_ip = (struct sfe_ipv6_addr *)iph->daddr.s6_addr32;
68
69 rcu_read_lock();
70
71 /*
72 * Look for a connection match.
73 */
74#ifdef CONFIG_NF_FLOW_COOKIE
75 cm = si->sfe_flow_cookie_table[skb->flow_cookie & SFE_FLOW_COOKIE_MASK].match;
76 if (unlikely(!cm)) {
77 cm = sfe_ipv6_find_connection_match_rcu(si, dev, IPPROTO_GRE, src_ip, 0, dest_ip, 0);
78 }
79#else
80 cm = sfe_ipv6_find_connection_match_rcu(si, dev, IPPROTO_GRE, src_ip, 0, dest_ip, 0);
81#endif
82 if (unlikely(!cm)) {
83 rcu_read_unlock();
84 sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_GRE_NO_CONNECTION);
85 DEBUG_TRACE("no connection match found dev %s src ip %pI6 dest ip %pI6\n", dev->name, src_ip, dest_ip);
86 return 0;
87 }
88
89 /*
Nitin Shetty2114a892022-01-28 20:03:56 +053090 * Do we expect an ingress VLAN tag for this flow?
91 */
92 if (unlikely(!sfe_vlan_validate_ingress_tag(skb, cm->ingress_vlan_hdr_cnt, cm->ingress_vlan_hdr, l2_info))) {
93 rcu_read_unlock();
94 sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_INGRESS_VLAN_TAG_MISMATCH);
95 DEBUG_TRACE("VLAN tag mismatch. skb=%px\n", skb);
96 return 0;
97 }
98
99 /*
Nitin Shettye6ed5b52021-12-27 14:50:11 +0530100 * Source interface validate.
101 */
102 if (unlikely((cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_SRC_INTERFACE_CHECK) && (cm->match_dev != dev))) {
Murat Sezgin9c538972022-05-17 13:33:17 -0700103 if (!(cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_SRC_INTERFACE_CHECK_NO_FLUSH)) {
104 struct sfe_ipv6_connection *c = cm->connection;
105 int ret;
106 DEBUG_TRACE("flush on source interface check failure\n");
107 spin_lock_bh(&si->lock);
108 ret = sfe_ipv6_remove_connection(si, c);
109 spin_unlock_bh(&si->lock);
Nitin Shettye6ed5b52021-12-27 14:50:11 +0530110
Murat Sezgin9c538972022-05-17 13:33:17 -0700111 if (ret) {
112 sfe_ipv6_flush_connection(si, c, SFE_SYNC_REASON_FLUSH);
113 }
Nitin Shettye6ed5b52021-12-27 14:50:11 +0530114 }
115 rcu_read_unlock();
116 sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_INVALID_SRC_IFACE);
Murat Sezgin9c538972022-05-17 13:33:17 -0700117 DEBUG_TRACE("exception the packet on source interface check failure\n");
Nitin Shettye6ed5b52021-12-27 14:50:11 +0530118 return 0;
119 }
120
121 passthrough = cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_PASSTHROUGH;
122
123 /*
124 * If our packet has beern marked as "sync on find" we can't actually
125 * forward it in the fast path, but now that we've found an associated
126 * connection we need sync its status before exception it to slow path. unless
127 * it is passthrough packet.
128 * TODO: revisit to ensure that pass through traffic is not bypassing firewall for fragmented cases
129 */
130 if (unlikely(sync_on_find) && !passthrough) {
131 sfe_ipv6_sync_status(si, cm->connection, SFE_SYNC_REASON_STATS);
132 rcu_read_unlock();
133
134 sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_GRE_IP_OPTIONS_OR_INITIAL_FRAGMENT);
135 DEBUG_TRACE("Sync on find\n");
136 return 0;
137 }
138
139 bridge_flow = !!(cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_BRIDGE_FLOW);
140
141 /*
142 * Does our hop_limit allow forwarding?
143 */
144 if (!bridge_flow && (iph->hop_limit < 2) && passthrough) {
145 sfe_ipv6_sync_status(si, cm->connection, SFE_SYNC_REASON_STATS);
146 rcu_read_unlock();
147
148 sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_GRE_SMALL_TTL);
149 DEBUG_TRACE("hop_limit too low\n");
150 return 0;
151 }
152
153 /*
Nitin Shettye6ed5b52021-12-27 14:50:11 +0530154 * Check if skb was cloned. If it was, unshare it. Because
155 * the data area is going to be written in this path and we don't want to
156 * change the cloned skb's data section.
157 */
158 if (unlikely(skb_cloned(skb))) {
159 DEBUG_TRACE("%px: skb is a cloned skb\n", skb);
160 skb = skb_unshare(skb, GFP_ATOMIC);
161 if (!skb) {
162 DEBUG_WARN("Failed to unshare the cloned skb\n");
163 rcu_read_unlock();
164 return 1;
165 }
166
167 /*
168 * Update the iph and udph pointers with the unshared skb's data area.
169 */
170 iph = (struct ipv6hdr *)skb->data;
171 }
172
173 /*
Nitin Shetty5dcf68c2022-01-28 20:29:21 +0530174 * For PPPoE packets, match server MAC and session id
Nitin Shetty2114a892022-01-28 20:03:56 +0530175 */
Nitin Shetty5dcf68c2022-01-28 20:29:21 +0530176 if (unlikely(cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_PPPOE_DECAP)) {
177 struct ethhdr *eth;
178 bool pppoe_match;
179
180 if (unlikely(!sfe_l2_parse_flag_check(l2_info, SFE_L2_PARSE_FLAGS_PPPOE_INGRESS))) {
181 rcu_read_unlock();
182 DEBUG_TRACE("%px: PPPoE header not present in packet for PPPoE rule\n", skb);
183 sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_INCORRECT_PPPOE_PARSING);
184 return 0;
185 }
186
187 eth = eth_hdr(skb);
188
189 pppoe_match = (cm->pppoe_session_id == sfe_l2_pppoe_session_id_get(l2_info)) &&
190 ether_addr_equal((u8*)cm->pppoe_remote_mac, (u8 *)eth->h_source);
191
192 if (unlikely(!pppoe_match)) {
193 DEBUG_TRACE("%px: PPPoE sessions ID %d and %d or MAC %pM and %pM did not match\n",
194 skb, cm->pppoe_session_id, sfe_l2_pppoe_session_id_get(l2_info),
195 cm->pppoe_remote_mac, eth->h_source);
196 rcu_read_unlock();
197 sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_INVALID_PPPOE_SESSION);
198 return 0;
199 }
200
201 skb->protocol = htons(l2_info->protocol);
202 this_cpu_inc(si->stats_pcpu->pppoe_decap_packets_forwarded64);
203 } else if (unlikely(sfe_l2_parse_flag_check(l2_info, SFE_L2_PARSE_FLAGS_PPPOE_INGRESS))) {
204
205 /*
206 * If packet contains PPPoE header but CME doesn't contain PPPoE flag yet we are exceptioning the packet to linux
207 */
208 if (unlikely(!(cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_BRIDGE_FLOW))) {
209 rcu_read_unlock();
210 DEBUG_TRACE("%px: CME doesn't contain PPPoE flag but packet has PPPoE header\n", skb);
211 sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_PPPOE_NOT_SET_IN_CME);
212 return 0;
213
214 }
215
216 /*
217 * For bridged flows when packet contains PPPoE header, restore the header back and forward to xmit interface
218 */
219 __skb_push(skb, (sizeof(struct pppoe_hdr) + sizeof(struct sfe_ppp_hdr)));
220
221 this_cpu_inc(si->stats_pcpu->pppoe_bridge_packets_forwarded64);
Nitin Shetty2114a892022-01-28 20:03:56 +0530222 }
223
224 /*
Nitin Shettye6ed5b52021-12-27 14:50:11 +0530225 * protocol handler will be valid only in decap-path.
226 */
227 if (cm->proto) {
228 struct inet6_protocol *ipprot = cm->proto;
229 skb_pull(skb, ihl);
230 skb_reset_transport_header(skb);
231 skb->fast_forwarded = 1;
232
233 ret = ipprot->handler(skb);
234 if (ret) {
235 this_cpu_inc(si->stats_pcpu->packets_not_forwarded64);
236 rcu_read_unlock();
237 return 1;
238 }
239
240 /*
241 * Update traffic stats.
242 */
243 atomic_inc(&cm->rx_packet_count);
244 atomic_add(len, &cm->rx_byte_count);
245
246 this_cpu_inc(si->stats_pcpu->packets_forwarded64);
247 rcu_read_unlock();
248 DEBUG_TRACE("%p: %s decap done\n",skb, __func__);
249 return 1;
250 }
251
252 /*
Nitin Shetty5dcf68c2022-01-28 20:29:21 +0530253 * Check if skb has enough headroom to write L2 headers
254 */
255 if (unlikely(skb_headroom(skb) < cm->l2_hdr_size)) {
256 rcu_read_unlock();
257 DEBUG_WARN("%px: Not enough headroom: %u\n", skb, skb_headroom(skb));
258 sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_NO_HEADROOM);
259 return 0;
260 }
261
262 /*
Nitin Shettye6ed5b52021-12-27 14:50:11 +0530263 * If our packet is larger than the MTU of the transmit interface then
264 * we can't forward it easily.
265 */
266 if (unlikely(len > cm->xmit_dev_mtu)) {
267 sfe_ipv6_sync_status(si, cm->connection, SFE_SYNC_REASON_STATS);
268 rcu_read_unlock();
269
270 sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_GRE_NEEDS_FRAGMENTATION);
271 DEBUG_TRACE("Larger than MTU\n");
272 return 0;
273 }
274
275 /*
276 * Update DSCP
277 */
278 if (unlikely(cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_DSCP_REMARK)) {
279 sfe_ipv6_change_dsfield(iph, cm->dscp);
280 }
281
Nitin Shettybe7e0532022-02-16 00:13:48 +0530282 iph->hop_limit -= (u8)(!bridge_flow & !tun_outer);
Nitin Shettye6ed5b52021-12-27 14:50:11 +0530283
284 /*
285 * Update traffic stats.
286 */
287 atomic_inc(&cm->rx_packet_count);
288 atomic_add(len, &cm->rx_byte_count);
289
290 xmit_dev = cm->xmit_dev;
291 skb->dev = xmit_dev;
292
Nitin Shetty2114a892022-01-28 20:03:56 +0530293 /*
Nitin Shetty5dcf68c2022-01-28 20:29:21 +0530294 * For PPPoE flows, add PPPoE header before L2 header is added.
295 */
296 if (unlikely(cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_PPPOE_ENCAP)) {
297 sfe_pppoe_add_header(skb, cm->pppoe_session_id, PPP_IPV6);
298 this_cpu_inc(si->stats_pcpu->pppoe_encap_packets_forwarded64);
299 }
300
301 /*
Nitin Shetty2114a892022-01-28 20:03:56 +0530302 * Check to see if we need to add VLAN tags
303 */
304 if (unlikely(cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_INSERT_EGRESS_VLAN_TAG)) {
305 sfe_vlan_add_tag(skb, cm->egress_vlan_hdr_cnt, cm->egress_vlan_hdr);
306 }
307
Nitin Shettye6ed5b52021-12-27 14:50:11 +0530308 if (cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR) {
309 /*
310 * For the simple case we write this really fast.
311 */
312 struct ethhdr *eth = (struct ethhdr *)__skb_push(skb, ETH_HLEN);
Nitin Shetty5dcf68c2022-01-28 20:29:21 +0530313 eth->h_proto = skb->protocol;
Nitin Shettye6ed5b52021-12-27 14:50:11 +0530314 ether_addr_copy((u8 *)eth->h_dest, (u8 *)cm->xmit_dest_mac);
315 ether_addr_copy((u8 *)eth->h_source, (u8 *)cm->xmit_src_mac);
316 } else if (cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_WRITE_L2_HDR) {
Nitin Shetty5dcf68c2022-01-28 20:29:21 +0530317 dev_hard_header(skb, xmit_dev, ntohs(skb->protocol),
Nitin Shettye6ed5b52021-12-27 14:50:11 +0530318 cm->xmit_dest_mac, cm->xmit_src_mac, len);
319 }
320
321 /*
322 * Update priority of skb.
323 */
324 if (unlikely(cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_PRIORITY_REMARK)) {
325 skb->priority = cm->priority;
326 }
327
328 /*
329 * Mark outgoing packet.
330 */
331 if (unlikely(cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_MARK)) {
332 skb->mark = cm->mark;
333 }
334
335 rcu_read_unlock();
336
337 this_cpu_inc(si->stats_pcpu->packets_forwarded64);
338
339 /*
340 * We're going to check for GSO flags when we transmit the packet so
341 * start fetching the necessary cache line now.
342 */
343 prefetch(skb_shinfo(skb));
344
345 /*
346 * Mark that this packet has been fast forwarded.
347 */
348 skb->fast_forwarded = 1;
349
350 /*
351 * Send the packet on its way.
352 */
353 dev_queue_xmit(skb);
354
355 return 1;
356}