blob: aaf2f89a81f2a04935ea3689c74efc35f6941cb2 [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;
45 struct sfe_ipv6_connection_match *cm;
46
47 DEBUG_TRACE("%px: sfe: sfe_ipv6_recv_tunipip6 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 = (struct sfe_ipv6_addr *)iph->saddr.s6_addr32;
54 dest_ip = (struct sfe_ipv6_addr *)iph->daddr.s6_addr32;
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_ipv6_find_connection_match_rcu(si, dev, IPPROTO_IPIP, src_ip, src_port, dest_ip, dest_port);
65 }
66#else
67 cm = sfe_ipv6_find_connection_match_rcu(si, dev, IPPROTO_IPIP, src_ip, src_port, dest_ip, dest_port);
68#endif
69 if (unlikely(!cm)) {
70 rcu_read_unlock();
71 sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_TUNIPIP6_NO_CONNECTION);
72 DEBUG_TRACE("%px: no 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_ipv6_sync_status(si, cm->connection, SFE_SYNC_REASON_STATS);
82 rcu_read_unlock();
83 sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_TUNIPIP6_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 inet6_protocol *ipprot = cm->proto;
96#else
97 struct inet6_protocol *ipprot = cm->proto;
98#endif
Tian Yang435afc42022-02-02 12:47:32 -080099
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 */
104 if (unlikely(!sfe_vlan_validate_ingress_tag(skb, cm->ingress_vlan_hdr_cnt, cm->ingress_vlan_hdr, l2_info))) {
105 rcu_read_unlock();
106 sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_INGRESS_VLAN_TAG_MISMATCH);
107 DEBUG_TRACE("VLAN tag mismatch. skb=%px\n"
108 "cm: %u [0]=%x/%x [1]=%x/%x\n"
109 "l2_info+: %u [0]=%x/%x [1]=%x/%x\n", skb,
110 cm->ingress_vlan_hdr_cnt,
111 htons(cm->ingress_vlan_hdr[0].tpid), cm->ingress_vlan_hdr[0].tci,
112 htons(cm->ingress_vlan_hdr[1].tpid), cm->ingress_vlan_hdr[1].tci,
113 l2_info->vlan_hdr_cnt,
114 htons(l2_info->vlan_hdr[0].tpid), l2_info->vlan_hdr[0].tci,
115 htons(l2_info->vlan_hdr[1].tpid), l2_info->vlan_hdr[1].tci);
116 return 0;
117 }
118 skb_reset_network_header(skb);
Tian Yangafb03452022-01-13 18:53:13 -0800119 skb_pull(skb, ihl);
120 skb_reset_transport_header(skb);
121
122 /*
123 * ipprot->handler(skb) will always return 0;
124 * There is no way to tell whether the packet is dropped later in linux or not.
125 * Hence here inc the byte/packet count always.
126 */
127 atomic_inc(&cm->rx_packet_count);
128 atomic_add(len, &cm->rx_byte_count);
129 this_cpu_inc(si->stats_pcpu->packets_forwarded64);
130 rcu_read_unlock();
131 DEBUG_TRACE("%px: %s decap done \n",skb, __func__);
Tian Yang435afc42022-02-02 12:47:32 -0800132
133 /*
134 * Update top interface for tunnel searching.
135 */
136 skb->dev = cm->top_interface_dev;
Tian Yangafb03452022-01-13 18:53:13 -0800137 ipprot->handler(skb);
138 return 1;
Tian Yang435afc42022-02-02 12:47:32 -0800139 }
Tian Yangafb03452022-01-13 18:53:13 -0800140
Tian Yang435afc42022-02-02 12:47:32 -0800141 /*
142 * Check if skb has enough headroom to write L2 headers
143 */
144 if (unlikely(skb_headroom(skb) < cm->l2_hdr_size)) {
145 rcu_read_unlock();
146 DEBUG_WARN("%px: Not enough headroom: %u\n", skb, skb_headroom(skb));
147 sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_NO_HEADROOM);
148 return 0;
Tian Yangafb03452022-01-13 18:53:13 -0800149 }
150
151 /*
152 * If our packet is larger than the MTU of the transmit interface then
153 * we can't forward it easily.
154 */
155 if (unlikely(len > cm->xmit_dev_mtu)) {
156 sfe_ipv6_sync_status(si, cm->connection, SFE_SYNC_REASON_STATS);
157 rcu_read_unlock();
158
159 sfe_ipv6_exception_stats_inc(si, SFE_IPV6_EXCEPTION_EVENT_TUNIPIP6_NEEDS_FRAGMENTATION);
160 DEBUG_TRACE("%px: Larger than mtu\n", skb);
161 return 0;
162 }
163
164 /*
165 * Update DSCP
166 */
167 if (unlikely(cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_DSCP_REMARK)) {
168 sfe_ipv6_change_dsfield(iph, cm->dscp);
169 }
170
171 /*
172 * Update traffic stats.
173 */
174 atomic_inc(&cm->rx_packet_count);
175 atomic_add(len, &cm->rx_byte_count);
176
177 skb->dev = cm->xmit_dev;
178
179 /*
Tian Yang435afc42022-02-02 12:47:32 -0800180 * Check to see if we need to add VLAN tags
181 */
182 if (unlikely(cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_INSERT_EGRESS_VLAN_TAG)) {
183 sfe_vlan_add_tag(skb, cm->egress_vlan_hdr_cnt, cm->egress_vlan_hdr);
184 }
185
186 /*
Tian Yangafb03452022-01-13 18:53:13 -0800187 * Check to see if we need to write a header.
188 */
189 if (likely(cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_WRITE_L2_HDR)) {
190 if (unlikely(!(cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR))) {
Tian Yang435afc42022-02-02 12:47:32 -0800191 dev_hard_header(skb, cm->xmit_dev, ntohs(skb->protocol),
Tian Yangafb03452022-01-13 18:53:13 -0800192 cm->xmit_dest_mac, cm->xmit_src_mac, len);
193 } else {
194 struct ethhdr *eth = (struct ethhdr *)__skb_push(skb, ETH_HLEN);
Tian Yang435afc42022-02-02 12:47:32 -0800195 eth->h_proto = skb->protocol;
Tian Yangafb03452022-01-13 18:53:13 -0800196 ether_addr_copy((u8 *)eth->h_dest, (u8 *)cm->xmit_dest_mac);
197 ether_addr_copy((u8 *)eth->h_source, (u8 *)cm->xmit_src_mac);
198 }
199 }
200
201 /*
202 * Update priority of skb.
203 */
204 if (unlikely(cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_PRIORITY_REMARK)) {
205 skb->priority = cm->priority;
206 }
207
208 /*
209 * Mark outgoing packet.
210 */
211 if (unlikely(cm->flags & SFE_IPV6_CONNECTION_MATCH_FLAG_MARK)) {
212 skb->mark = cm->mark;
213 }
214
215 rcu_read_unlock();
216
217 this_cpu_inc(si->stats_pcpu->packets_forwarded64);
218
219 /*
220 * We're going to check for GSO flags when we transmit the packet so
221 * start fetching the necessary cache line now.
222 */
223 prefetch(skb_shinfo(skb));
224
225 /*
226 * Mark that this packet has been fast forwarded and send it on its way.
227 */
228 skb->fast_forwarded = 1;
229 dev_queue_xmit(skb);
230
231 return 1;
232}