blob: 3f33ee9f23349bd6e0d6e53d5b26d947c577fb9a [file] [log] [blame]
Tian Yangd98d91b2022-03-09 14:50:12 -08001/*
2 * sfe_ipv4_tun6rd.c
3 * Shortcut forwarding engine file for IPv4 TUN6RD
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 <linux/etherdevice.h>
22#include <linux/version.h>
23#include <net/protocol.h>
24#include <net/ip.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"
Tian Yang46d6eb02022-03-31 10:26:16 -070031#include "sfe_vlan.h"
Tian Yangd98d91b2022-03-09 14:50:12 -080032
33/*
34 * sfe_ipv4_recv_tun6rd()
35 * Handle TUN6RD packet receives and forwarding.
36 */
37int sfe_ipv4_recv_tun6rd(struct sfe_ipv4 *si, struct sk_buff *skb, struct net_device *dev,
38 unsigned int len, struct iphdr *iph, unsigned int ihl,
39 bool sync_on_find, struct sfe_l2_info *l2_info, bool tun_outer)
40{
41 __be32 src_ip;
42 __be32 dest_ip;
43 __be16 src_port = 0;
44 __be16 dest_port = 0;
45 struct sfe_ipv4_connection_match *cm;
46
47 DEBUG_TRACE("%px: sfe: sfe_ipv4_recv_tun6rd called.\n", skb);
48
49 /*
50 * Read the IP address information. Read the IP header data first
51 * because we've almost certainly got that in the cache.
52 */
53 src_ip = iph->saddr;
54 dest_ip = iph->daddr;
55
56 rcu_read_lock();
57
58 /*
59 * Look for a connection match.
60 */
61#ifdef CONFIG_NF_FLOW_COOKIE
62 cm = si->sfe_flow_cookie_table[skb->flow_cookie & SFE_FLOW_COOKIE_MASK].match;
63 if (unlikely(!cm)) {
64 cm = sfe_ipv4_find_connection_match_rcu(si, dev, IPPROTO_IPV6, src_ip, src_port, dest_ip, dest_port);
65 }
66#else
67 cm = sfe_ipv4_find_connection_match_rcu(si, dev, IPPROTO_IPV6, src_ip, src_port, dest_ip, dest_port);
68#endif
69 if (unlikely(!cm)) {
70 rcu_read_unlock();
71 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_TUN6RD_NO_CONNECTION);
72 DEBUG_TRACE("%px: no tun6rd connection found\n", skb);
73 return 0;
74 }
75
76 /*
77 * If our packet has been marked as "sync on find" we will sync the status
78 * and forward it to slowpath.
79 */
80 if (unlikely(sync_on_find)) {
81 sfe_ipv4_sync_status(si, cm->connection, SFE_SYNC_REASON_STATS);
82 rcu_read_unlock();
83 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_TUN6RD_SYNC_ON_FIND);
84 DEBUG_TRACE("%px: Sync on find\n", skb);
85
86 return 0;
87 }
88
89 /*
90 * If cm->proto is set, it means the decap path.
91 * Otherwise we forward the packet in encap path.
92 */
93 if(cm->proto) {
94#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
95 const struct net_protocol *ipprot = cm->proto;
96#else
97 struct net_protocol *ipprot = cm->proto;
98#endif
Tian Yang46d6eb02022-03-31 10:26:16 -070099
100 /*
101 * Do we expect an ingress VLAN tag for this flow?
102 * Note: We will only have ingress tag check in decap direction.
103 * Here, no modification is needed, we only check tag match between
104 * vlan hdr stored in cm and l2_info.
105 */
106 if (unlikely(!sfe_vlan_validate_ingress_tag(skb, cm->ingress_vlan_hdr_cnt, cm->ingress_vlan_hdr, l2_info))) {
107 rcu_read_unlock();
108 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_INGRESS_VLAN_TAG_MISMATCH);
109 DEBUG_TRACE("VLAN tag mismatch. skb=%px\n"
110 "cm: %u [0]=%x/%x [1]=%x/%x\n"
111 "l2_info+: %u [0]=%x/%x [1]=%x/%x\n", skb,
112 cm->ingress_vlan_hdr_cnt,
113 htons(cm->ingress_vlan_hdr[0].tpid), cm->ingress_vlan_hdr[0].tci,
114 htons(cm->ingress_vlan_hdr[1].tpid), cm->ingress_vlan_hdr[1].tci,
115 l2_info->vlan_hdr_cnt,
116 htons(l2_info->vlan_hdr[0].tpid), l2_info->vlan_hdr[0].tci,
117 htons(l2_info->vlan_hdr[1].tpid), l2_info->vlan_hdr[1].tci);
118 return 0;
119 }
120 skb_reset_network_header(skb);
Tian Yangd98d91b2022-03-09 14:50:12 -0800121 skb_pull(skb, ihl);
122 skb_reset_transport_header(skb);
123
124 /*
125 * ipprot->handler(skb) will always return 0;
126 * There is no way to tell whether the packet is dropped later in linux or not.
127 * Hence here inc the byte/packet count always.
128 */
129 atomic_inc(&cm->rx_packet_count);
130 atomic_add(len, &cm->rx_byte_count);
Tian Yangd98d91b2022-03-09 14:50:12 -0800131 rcu_read_unlock();
132 this_cpu_inc(si->stats_pcpu->packets_forwarded64);
133 DEBUG_TRACE("%px: %s decap done \n", skb, __func__);
Tian Yang46d6eb02022-03-31 10:26:16 -0700134
135 /*
136 * Update top interface for tunnel searching.
137 */
138 skb->dev = cm->top_interface_dev;
139 ipprot->handler(skb);
Tian Yangd98d91b2022-03-09 14:50:12 -0800140 return 1;
141
142 }
143
144 /*
145 * If our packet is larger than the MTU of the transmit interface then
146 * we can't forward it easily.
147 */
148 if (unlikely(len > cm->xmit_dev_mtu)) {
149 sfe_ipv4_sync_status(si, cm->connection, SFE_SYNC_REASON_STATS);
150 rcu_read_unlock();
151
152 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_TUN6RD_NEEDS_FRAGMENTATION);
153 DEBUG_TRACE("%px: Larger than mtu\n", skb);
154 return 0;
155 }
156
157 /*
158 * Update DSCP
159 */
160 if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_DSCP_REMARK)) {
161 iph->tos = (iph->tos & SFE_IPV4_DSCP_MASK) | cm->dscp;
162 }
163
164 /*
165 * Update traffic stats.
166 */
167 atomic_inc(&cm->rx_packet_count);
168 atomic_add(len, &cm->rx_byte_count);
169
170 skb->dev = cm->xmit_dev;
171
172 /*
Tian Yang46d6eb02022-03-31 10:26:16 -0700173 * Check to see if we need to add VLAN tags
174 */
175 if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_INSERT_EGRESS_VLAN_TAG)) {
176
177 /*
178 * Check if skb has enough headroom to write L2 headers
179 */
180 if (unlikely(skb_headroom(skb) < cm->l2_hdr_size)) {
181 rcu_read_unlock();
182 DEBUG_WARN("%px: Not enough headroom: %u\n", skb, skb_headroom(skb));
183 sfe_ipv4_exception_stats_inc(si, SFE_IPV4_EXCEPTION_EVENT_NO_HEADROOM);
184 return 0;
185 }
186 sfe_vlan_add_tag(skb, cm->egress_vlan_hdr_cnt, cm->egress_vlan_hdr);
187 }
188
189 /*
Tian Yangd98d91b2022-03-09 14:50:12 -0800190 * Check to see if we need to write a header.
191 */
192 if (likely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_L2_HDR)) {
193 if (unlikely(!(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR))) {
Tian Yang46d6eb02022-03-31 10:26:16 -0700194 dev_hard_header(skb, cm->xmit_dev, ntohs(skb->protocol),
Tian Yangd98d91b2022-03-09 14:50:12 -0800195 cm->xmit_dest_mac, cm->xmit_src_mac, len);
196 } else {
197 struct ethhdr *eth = (struct ethhdr *)__skb_push(skb, ETH_HLEN);
Tian Yang46d6eb02022-03-31 10:26:16 -0700198 eth->h_proto = skb->protocol;
Tian Yangd98d91b2022-03-09 14:50:12 -0800199 ether_addr_copy((u8 *)eth->h_dest, (u8 *)cm->xmit_dest_mac);
200 ether_addr_copy((u8 *)eth->h_source, (u8 *)cm->xmit_src_mac);
201 }
202 }
203
204 /*
205 * Update priority of skb.
206 */
207 if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_PRIORITY_REMARK)) {
208 skb->priority = cm->priority;
209 }
210
211 /*
212 * Mark outgoing packet.
213 */
214 if (unlikely(cm->flags & SFE_IPV4_CONNECTION_MATCH_FLAG_MARK)) {
215 skb->mark = cm->mark;
216 }
217
218 rcu_read_unlock();
219
220 this_cpu_inc(si->stats_pcpu->packets_forwarded64);
221
222 /*
223 * We're going to check for GSO flags when we transmit the packet so
224 * start fetching the necessary cache line now.
225 */
226 prefetch(skb_shinfo(skb));
227
228 /*
229 * Mark that this packet has been fast forwarded and send it on its way.
230 */
231 skb->fast_forwarded = 1;
232 dev_queue_xmit(skb);
233
234 return 1;
235}