Linux Audio

Check our new training course

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