File Index
Files and directories
Sign in
FILE

xfrm6_input.c

File preview with syntax highlighting.

linux-6.19.14/net/ipv6/xfrm6_input.c C
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * xfrm6_input.c: based on net/ipv4/xfrm4_input.c
  4 *
  5 * Authors:
  6 *	Mitsuru KANDA @USAGI
  7 *	Kazunori MIYAZAWA @USAGI
  8 *	Kunihiro Ishiguro <kunihiro@ipinfusion.com>
  9 *	YOSHIFUJI Hideaki @USAGI
 10 *		IPv6 support
 11 */
 12
 13#include <linux/module.h>
 14#include <linux/string.h>
 15#include <linux/netfilter.h>
 16#include <linux/netfilter_ipv6.h>
 17#include <net/ipv6.h>
 18#include <net/xfrm.h>
 19#include <net/protocol.h>
 20#include <net/gro.h>
 21
 22int xfrm6_rcv_spi(struct sk_buff *skb, int nexthdr, __be32 spi,
 23		  struct ip6_tnl *t)
 24{
 25	XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 = t;
 26	XFRM_SPI_SKB_CB(skb)->family = AF_INET6;
 27	XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct ipv6hdr, daddr);
 28	return xfrm_input(skb, nexthdr, spi, 0);
 29}
 30EXPORT_SYMBOL(xfrm6_rcv_spi);
 31
 32static int xfrm6_transport_finish2(struct net *net, struct sock *sk,
 33				   struct sk_buff *skb)
 34{
 35	if (xfrm_trans_queue(skb, ip6_rcv_finish)) {
 36		kfree_skb(skb);
 37		return NET_RX_DROP;
 38	}
 39
 40	return 0;
 41}
 42
 43int xfrm6_transport_finish(struct sk_buff *skb, int async)
 44{
 45	struct xfrm_offload *xo = xfrm_offload(skb);
 46	struct net_device *dev = skb->dev;
 47	int nhlen = -skb_network_offset(skb);
 48
 49	skb_network_header(skb)[IP6CB(skb)->nhoff] =
 50		XFRM_MODE_SKB_CB(skb)->protocol;
 51
 52#ifndef CONFIG_NETFILTER
 53	if (!async)
 54		return 1;
 55#endif
 56
 57	__skb_push(skb, nhlen);
 58	ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
 59	skb_postpush_rcsum(skb, skb_network_header(skb), nhlen);
 60
 61	if (xo && (xo->flags & XFRM_GRO)) {
 62		/* The full l2 header needs to be preserved so that re-injecting the packet at l2
 63		 * works correctly in the presence of vlan tags.
 64		 */
 65		skb_mac_header_rebuild_full(skb, xo->orig_mac_len);
 66		skb_reset_network_header(skb);
 67		skb_reset_transport_header(skb);
 68		return 0;
 69	}
 70
 71	NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING,
 72		dev_net(dev), NULL, skb, dev, NULL,
 73		xfrm6_transport_finish2);
 74	if (async)
 75		dev_put(dev);
 76	return 0;
 77}
 78
 79static int __xfrm6_udp_encap_rcv(struct sock *sk, struct sk_buff *skb, bool pull)
 80{
 81	struct udp_sock *up = udp_sk(sk);
 82	struct udphdr *uh;
 83	struct ipv6hdr *ip6h;
 84	int len;
 85	int ip6hlen = sizeof(struct ipv6hdr);
 86	__u8 *udpdata;
 87	__be32 *udpdata32;
 88	u16 encap_type;
 89
 90	encap_type = READ_ONCE(up->encap_type);
 91	/* if this is not encapsulated socket, then just return now */
 92	if (!encap_type)
 93		return 1;
 94
 95	/* If this is a paged skb, make sure we pull up
 96	 * whatever data we need to look at. */
 97	len = skb->len - sizeof(struct udphdr);
 98	if (!pskb_may_pull(skb, sizeof(struct udphdr) + min(len, 8)))
 99		return 1;
100
101	/* Now we can get the pointers */
102	uh = udp_hdr(skb);
103	udpdata = (__u8 *)uh + sizeof(struct udphdr);
104	udpdata32 = (__be32 *)udpdata;
105
106	switch (encap_type) {
107	default:
108	case UDP_ENCAP_ESPINUDP:
109		/* Check if this is a keepalive packet.  If so, eat it. */
110		if (len == 1 && udpdata[0] == 0xff) {
111			return -EINVAL;
112		} else if (len > sizeof(struct ip_esp_hdr) && udpdata32[0] != 0) {
113			/* ESP Packet without Non-ESP header */
114			len = sizeof(struct udphdr);
115		} else
116			/* Must be an IKE packet.. pass it through */
117			return 1;
118		break;
119	}
120
121	/* At this point we are sure that this is an ESPinUDP packet,
122	 * so we need to remove 'len' bytes from the packet (the UDP
123	 * header and optional ESP marker bytes) and then modify the
124	 * protocol to ESP, and then call into the transform receiver.
125	 */
126	if (skb_unclone(skb, GFP_ATOMIC))
127		return -EINVAL;
128
129	/* Now we can update and verify the packet length... */
130	ip6h = ipv6_hdr(skb);
131	ip6h->payload_len = htons(ntohs(ip6h->payload_len) - len);
132	if (skb->len < ip6hlen + len) {
133		/* packet is too small!?! */
134		return -EINVAL;
135	}
136
137	/* pull the data buffer up to the ESP header and set the
138	 * transport header to point to ESP.  Keep UDP on the stack
139	 * for later.
140	 */
141	if (pull) {
142		__skb_pull(skb, len);
143		skb_reset_transport_header(skb);
144	} else {
145		skb_set_transport_header(skb, len);
146	}
147
148	/* process ESP */
149	return 0;
150}
151
152/* If it's a keepalive packet, then just eat it.
153 * If it's an encapsulated packet, then pass it to the
154 * IPsec xfrm input.
155 * Returns 0 if skb passed to xfrm or was dropped.
156 * Returns >0 if skb should be passed to UDP.
157 * Returns <0 if skb should be resubmitted (-ret is protocol)
158 */
159int xfrm6_udp_encap_rcv(struct sock *sk, struct sk_buff *skb)
160{
161	int ret;
162
163	if (skb->protocol == htons(ETH_P_IP))
164		return xfrm4_udp_encap_rcv(sk, skb);
165
166	ret = __xfrm6_udp_encap_rcv(sk, skb, true);
167	if (!ret)
168		return xfrm6_rcv_encap(skb, IPPROTO_ESP, 0,
169				       udp_sk(sk)->encap_type);
170
171	if (ret < 0) {
172		kfree_skb(skb);
173		return 0;
174	}
175
176	return ret;
177}
178
179struct sk_buff *xfrm6_gro_udp_encap_rcv(struct sock *sk, struct list_head *head,
180					struct sk_buff *skb)
181{
182	int offset = skb_gro_offset(skb);
183	const struct net_offload *ops;
184	struct sk_buff *pp = NULL;
185	int len, dlen;
186	__u8 *udpdata;
187	__be32 *udpdata32;
188
189	if (skb->protocol == htons(ETH_P_IP))
190		return xfrm4_gro_udp_encap_rcv(sk, head, skb);
191
192	len = skb->len - offset;
193	dlen = offset + min(len, 8);
194	udpdata = skb_gro_header(skb, dlen, offset);
195	udpdata32 = (__be32 *)udpdata;
196	if (unlikely(!udpdata))
197		return NULL;
198
199	rcu_read_lock();
200	ops = rcu_dereference(inet6_offloads[IPPROTO_ESP]);
201	if (!ops || !ops->callbacks.gro_receive)
202		goto out;
203
204	/* check if it is a keepalive or IKE packet */
205	if (len <= sizeof(struct ip_esp_hdr) || udpdata32[0] == 0)
206		goto out;
207
208	/* set the transport header to ESP */
209	skb_set_transport_header(skb, offset);
210
211	NAPI_GRO_CB(skb)->proto = IPPROTO_UDP;
212
213	pp = call_gro_receive(ops->callbacks.gro_receive, head, skb);
214	rcu_read_unlock();
215
216	return pp;
217
218out:
219	rcu_read_unlock();
220	NAPI_GRO_CB(skb)->same_flow = 0;
221	NAPI_GRO_CB(skb)->flush = 1;
222
223	return NULL;
224}
225
226int xfrm6_rcv_tnl(struct sk_buff *skb, struct ip6_tnl *t)
227{
228	return xfrm6_rcv_spi(skb, skb_network_header(skb)[IP6CB(skb)->nhoff],
229			     0, t);
230}
231EXPORT_SYMBOL(xfrm6_rcv_tnl);
232
233int xfrm6_rcv(struct sk_buff *skb)
234{
235	return xfrm6_rcv_tnl(skb, NULL);
236}
237EXPORT_SYMBOL(xfrm6_rcv);
238int xfrm6_input_addr(struct sk_buff *skb, xfrm_address_t *daddr,
239		     xfrm_address_t *saddr, u8 proto)
240{
241	struct net *net = dev_net(skb->dev);
242	struct xfrm_state *x = NULL;
243	struct sec_path *sp;
244	int i = 0;
245
246	sp = secpath_set(skb);
247	if (!sp) {
248		XFRM_INC_STATS(net, LINUX_MIB_XFRMINERROR);
249		goto drop;
250	}
251
252	if (1 + sp->len == XFRM_MAX_DEPTH) {
253		XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
254		goto drop;
255	}
256
257	for (i = 0; i < 3; i++) {
258		xfrm_address_t *dst, *src;
259
260		switch (i) {
261		case 0:
262			dst = daddr;
263			src = saddr;
264			break;
265		case 1:
266			/* lookup state with wild-card source address */
267			dst = daddr;
268			src = (xfrm_address_t *)&in6addr_any;
269			break;
270		default:
271			/* lookup state with wild-card addresses */
272			dst = (xfrm_address_t *)&in6addr_any;
273			src = (xfrm_address_t *)&in6addr_any;
274			break;
275		}
276
277		x = xfrm_state_lookup_byaddr(net, skb->mark, dst, src, proto, AF_INET6);
278		if (!x)
279			continue;
280
281		if (unlikely(x->dir && x->dir != XFRM_SA_DIR_IN)) {
282			XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEDIRERROR);
283			xfrm_state_put(x);
284			x = NULL;
285			continue;
286		}
287
288		spin_lock(&x->lock);
289
290		if ((!i || (x->props.flags & XFRM_STATE_WILDRECV)) &&
291		    likely(x->km.state == XFRM_STATE_VALID) &&
292		    !xfrm_state_check_expire(x)) {
293			spin_unlock(&x->lock);
294			if (x->type->input(x, skb) > 0) {
295				/* found a valid state */
296				break;
297			}
298		} else
299			spin_unlock(&x->lock);
300
301		xfrm_state_put(x);
302		x = NULL;
303	}
304
305	if (!x) {
306		XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOSTATES);
307		xfrm_audit_state_notfound_simple(skb, AF_INET6);
308		goto drop;
309	}
310
311	sp->xvec[sp->len++] = x;
312
313	spin_lock(&x->lock);
314
315	x->curlft.bytes += skb->len;
316	x->curlft.packets++;
317
318	spin_unlock(&x->lock);
319
320	return 1;
321
322drop:
323	return -1;
324}
325EXPORT_SYMBOL(xfrm6_input_addr);
~/ Read-only file preview.