blob: 8690f26428c60d1077ddd5b052622b2b10080ec0 [file] [log] [blame]
Tian Yangafb03452022-01-13 18:53:13 -08001/*
2 * sfe_ipv6_tunipip6.c
3 * Shortcut forwarding engine file for IPv6 TUNIPIP6
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/ip6_checksum.h>
24#include <net/protocol.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_ipv6.h"
Tian Yang435afc42022-02-02 12:47:32 -080031#include "sfe_vlan.h"
Tian Yangafb03452022-01-13 18:53:13 -080032
33/*
34 * sfe_ipv6_recv_tunipip6()
35 * Handle TUNIPIP6 packet receives and forwarding.
36 */
37int sfe_ipv6_recv_tunipip6(struct sfe_ipv6 *si, struct sk_buff *skb, struct net_device *dev,
38 unsigned int len, struct ipv6hdr *iph, unsigned int ihl,
39 bool sync_on_find, struct sfe_l2_info *l2_info, bool tun_outer)
40{
41 struct sfe_ipv6_addr *src_ip;
42 struct sfe_ipv6_addr *dest_ip;
43 __be16 src_port = 0;
44 __be16 dest_port = 0;
Tian Yang010f1ba2022-03-29 11:46:47 -070045 unsigned int ihl_tmp = sizeof(struct ipv6hdr);
Tian Yangafb03452022-01-13 18:53:13 -080046 struct sfe_ipv6_connection_match *cm;
Tian Yang010f1ba2022-03-29 11:46:47 -070047 bool non_dst = false;
48 u8 next_hdr;
Tian Yangafb03452022-01-13 18:53:13 -080049
50 DEBUG_TRACE("%px: sfe: sfe_ipv6_recv_tunipip6 called.\n", skb);
51
52 /*
53 * Read the IP address information. Read the IP header data first
54 * because we've almost certainly got that in the cache.
55 */
56 src_ip = (struct sfe_ipv6_addr *)iph->saddr.s6_addr32;
57 dest_ip = (struct sfe_ipv6_addr *)iph->daddr.s6_addr32;
58
59 rcu_read_lock();
60
61 /*
62 * Look for a connection match.
63 */
64#ifdef CONFIG_NF_FLOW_COOKIE
65 cm = si->sfe_flow_cookie_table[skb->flow_cookie & SFE_FLOW_COOKIE_MASK].match;
66 if (unlikely(!cm)) {
67 cm = sfe_ipv6_find_connection_match_rcu(si, dev, IPPROTO_IPIP, src_ip, src_port, dest_ip, dest_port);
68 }
69#else
70 cm = sfe_ipv6_find_connection_match_rcu(si, dev, IPPROTO_IPIP, src_ip, src_port, dest_ip, dest_port);
71#endif
72 if (unlikely(!cm)) {
73 rcu_read_unlock();
74 sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_TUNIPIP6_NO_CONNECTION);
75 DEBUG_TRACE("%px: no connection found\n", skb);
76 return 0;
77 }
78
Tian Yang010f1ba2022-03-29 11:46:47 -070079 next_hdr = iph->nexthdr;
80
81 /*
82 * Try to find an extension header(if any) that is not NEXTHDR_DEST.
83 */
84 while (unlikely(sfe_ipv6_is_ext_hdr(next_hdr))) {
85 struct sfe_ipv6_ext_hdr *ext_hdr;
86 unsigned int ext_hdr_len;
87
88 if(next_hdr != NEXTHDR_DEST) {
89 non_dst = true;
90 break;
91 }
92
93 ext_hdr = (struct sfe_ipv6_ext_hdr *)(skb->data + ihl_tmp);
94
95 ext_hdr_len = ext_hdr->hdr_len;
96 ext_hdr_len <<= 3;
97 ext_hdr_len += sizeof(struct sfe_ipv6_ext_hdr);
98 ihl_tmp += ext_hdr_len;
99
100 next_hdr = ext_hdr->next_hdr;
101 }
102
Tian Yangafb03452022-01-13 18:53:13 -0800103 /*
104 * If our packet has been marked as "sync on find" we will sync the status
Tian Yang010f1ba2022-03-29 11:46:47 -0700105 * and forward it to slowpath, except that encap_limit is set for dslite tunnel
106 * which is embedded in exthdr type NEXTHDR_DEST.
Tian Yangafb03452022-01-13 18:53:13 -0800107 */
Tian Yang010f1ba2022-03-29 11:46:47 -0700108 if (unlikely(sync_on_find && non_dst)) {
Tian Yangafb03452022-01-13 18:53:13 -0800109 sfe_ipv6_sync_status(si, cm->connection, SFE_SYNC_REASON_STATS);
110 rcu_read_unlock();
111 sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_TUNIPIP6_SYNC_ON_FIND);
112 DEBUG_TRACE("%px: Sync on find\n", skb);
113
114 return 0;
115 }
116
117 /*
118 * If cm->proto is set, it means the decap path.
119 * Otherwise we forward the packet in encap path.
120 */
121 if(cm->proto) {
122#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0))
123 const struct inet6_protocol *ipprot = cm->proto;
124#else
125 struct inet6_protocol *ipprot = cm->proto;
126#endif
Tian Yang435afc42022-02-02 12:47:32 -0800127
128 /*
129 * Do we expect an ingress VLAN tag for this flow?
130 * Note: We will only have ingress tag check in decap direction.
131 */
132 if (unlikely(!sfe_vlan_validate_ingress_tag(skb, cm->ingress_vlan_hdr_cnt, cm->ingress_vlan_hdr, l2_info))) {
133 rcu_read_unlock();
134 sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_INGRESS_VLAN_TAG_MISMATCH);
135 DEBUG_TRACE("VLAN tag mismatch. skb=%px\n"
136 "cm: %u [0]=%x/%x [1]=%x/%x\n"
137 "l2_info+: %u [0]=%x/%x [1]=%x/%x\n", skb,
138 cm->ingress_vlan_hdr_cnt,
139 htons(cm->ingress_vlan_hdr[0].tpid), cm->ingress_vlan_hdr[0].tci,
140 htons(cm->ingress_vlan_hdr[1].tpid), cm->ingress_vlan_hdr[1].tci,
141 l2_info->vlan_hdr_cnt,
142 htons(l2_info->vlan_hdr[0].tpid), l2_info->vlan_hdr[0].tci,
143 htons(l2_info->vlan_hdr[1].tpid), l2_info->vlan_hdr[1].tci);
144 return 0;
145 }
146 skb_reset_network_header(skb);
Tian Yangafb03452022-01-13 18:53:13 -0800147 skb_pull(skb, ihl);
148 skb_reset_transport_header(skb);
149
150 /*
151 * ipprot->handler(skb) will always return 0;
152 * There is no way to tell whether the packet is dropped later in linux or not.
153 * Hence here inc the byte/packet count always.
154 */
155 atomic_inc(&cm->rx_packet_count);
156 atomic_add(len, &cm->rx_byte_count);
157 this_cpu_inc(si->stats_pcpu->packets_forwarded64);
158 rcu_read_unlock();
159 DEBUG_TRACE("%px: %s decap done \n",skb, __func__);
Tian Yang435afc42022-02-02 12:47:32 -0800160
161 /*
162 * Update top interface for tunnel searching.
163 */
164 skb->dev = cm->top_interface_dev;
Tian Yangafb03452022-01-13 18:53:13 -0800165 ipprot->handler(skb);
166 return 1;
Tian Yang435afc42022-02-02 12:47:32 -0800167 }
Tian Yangafb03452022-01-13 18:53:13 -0800168
Tian Yang435afc42022-02-02 12:47:32 -0800169 /*
170 * Check if skb has enough headroom to write L2 headers
171 */
172 if (unlikely(skb_headroom(skb) < cm->l2_hdr_size)) {
173 rcu_read_unlock();
174 DEBUG_WARN("%px: Not enough headroom: %u\n", skb, skb_headroom(skb));
175 sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_NO_HEADROOM);
176 return 0;
Tian Yangafb03452022-01-13 18:53:13 -0800177 }
178
179 /*
180 * If our packet is larger than the MTU of the transmit interface then
181 * we can't forward it easily.
182 */
183 if (unlikely(len > cm->xmit_dev_mtu)) {
184 sfe_ipv6_sync_status(si, cm->connection, SFE_SYNC_REASON_STATS);
185 rcu_read_unlock();
186
187 sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_TUNIPIP6_NEEDS_FRAGMENTATION);
188 DEBUG_TRACE("%px: Larger than mtu\n", skb);
189 return 0;
190 }
191
192 /*
193 * Update DSCP
194 */
195 if (unlikely(cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_DSCP_REMARK)) {
196 sfe_ipv6_change_dsfield(iph, cm->dscp);
197 }
198
199 /*
200 * Update traffic stats.
201 */
202 atomic_inc(&cm->rx_packet_count);
203 atomic_add(len, &cm->rx_byte_count);
204
205 skb->dev = cm->xmit_dev;
206
207 /*
Tian Yang435afc42022-02-02 12:47:32 -0800208 * Check to see if we need to add VLAN tags
209 */
210 if (unlikely(cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_INSERT_EGRESS_VLAN_TAG)) {
211 sfe_vlan_add_tag(skb, cm->egress_vlan_hdr_cnt, cm->egress_vlan_hdr);
212 }
213
214 /*
Tian Yangafb03452022-01-13 18:53:13 -0800215 * Check to see if we need to write a header.
216 */
217 if (likely(cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_WRITE_L2_HDR)) {
218 if (unlikely(!(cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR))) {
Tian Yang435afc42022-02-02 12:47:32 -0800219 dev_hard_header(skb, cm->xmit_dev, ntohs(skb->protocol),
Tian Yangafb03452022-01-13 18:53:13 -0800220 cm->xmit_dest_mac, cm->xmit_src_mac, len);
221 } else {
222 struct ethhdr *eth = (struct ethhdr *)__skb_push(skb, ETH_HLEN);
Tian Yang435afc42022-02-02 12:47:32 -0800223 eth->h_proto = skb->protocol;
Tian Yangafb03452022-01-13 18:53:13 -0800224 ether_addr_copy((u8 *)eth->h_dest, (u8 *)cm->xmit_dest_mac);
225 ether_addr_copy((u8 *)eth->h_source, (u8 *)cm->xmit_src_mac);
226 }
227 }
228
229 /*
230 * Update priority of skb.
231 */
232 if (unlikely(cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_PRIORITY_REMARK)) {
233 skb->priority = cm->priority;
234 }
235
236 /*
237 * Mark outgoing packet.
238 */
239 if (unlikely(cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_MARK)) {
240 skb->mark = cm->mark;
241 }
242
243 rcu_read_unlock();
244
245 this_cpu_inc(si->stats_pcpu->packets_forwarded64);
246
247 /*
248 * We're going to check for GSO flags when we transmit the packet so
249 * start fetching the necessary cache line now.
250 */
251 prefetch(skb_shinfo(skb));
252
253 /*
254 * Mark that this packet has been fast forwarded and send it on its way.
255 */
256 skb->fast_forwarded = 1;
257 dev_queue_xmit(skb);
258
259 return 1;
260}