1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * xfrm6_input.c: based on net/ipv4/xfrm4_input.c
41da177e4SLinus Torvalds *
51da177e4SLinus Torvalds * Authors:
61da177e4SLinus Torvalds * Mitsuru KANDA @USAGI
71da177e4SLinus Torvalds * Kazunori MIYAZAWA @USAGI
81da177e4SLinus Torvalds * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
91da177e4SLinus Torvalds * YOSHIFUJI Hideaki @USAGI
101da177e4SLinus Torvalds * IPv6 support
111da177e4SLinus Torvalds */
121da177e4SLinus Torvalds
131da177e4SLinus Torvalds #include <linux/module.h>
141da177e4SLinus Torvalds #include <linux/string.h>
15b05e1066SPatrick McHardy #include <linux/netfilter.h>
16b05e1066SPatrick McHardy #include <linux/netfilter_ipv6.h>
171da177e4SLinus Torvalds #include <net/ipv6.h>
181da177e4SLinus Torvalds #include <net/xfrm.h>
19221ddb72SSteffen Klassert #include <net/protocol.h>
20221ddb72SSteffen Klassert #include <net/gro.h>
211da177e4SLinus Torvalds
xfrm6_rcv_spi(struct sk_buff * skb,int nexthdr,__be32 spi,struct ip6_tnl * t)2263c43787SNicolas Dichtel int xfrm6_rcv_spi(struct sk_buff *skb, int nexthdr, __be32 spi,
2363c43787SNicolas Dichtel struct ip6_tnl *t)
241da177e4SLinus Torvalds {
2563c43787SNicolas Dichtel XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 = t;
262fcb45b6SHerbert Xu XFRM_SPI_SKB_CB(skb)->family = AF_INET6;
27716062fdSHerbert Xu XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct ipv6hdr, daddr);
28716062fdSHerbert Xu return xfrm_input(skb, nexthdr, spi, 0);
291da177e4SLinus Torvalds }
30716062fdSHerbert Xu EXPORT_SYMBOL(xfrm6_rcv_spi);
311da177e4SLinus Torvalds
xfrm6_transport_finish2(struct net * net,struct sock * sk,struct sk_buff * skb)32acf568eeSHerbert Xu static int xfrm6_transport_finish2(struct net *net, struct sock *sk,
33acf568eeSHerbert Xu struct sk_buff *skb)
34acf568eeSHerbert Xu {
350146dca7SSabrina Dubroca if (xfrm_trans_queue(skb, ip6_rcv_finish)) {
360146dca7SSabrina Dubroca kfree_skb(skb);
370146dca7SSabrina Dubroca return NET_RX_DROP;
380146dca7SSabrina Dubroca }
390146dca7SSabrina Dubroca
400146dca7SSabrina Dubroca return 0;
41acf568eeSHerbert Xu }
42acf568eeSHerbert Xu
xfrm6_transport_finish(struct sk_buff * skb,int async)43716062fdSHerbert Xu int xfrm6_transport_finish(struct sk_buff *skb, int async)
44716062fdSHerbert Xu {
457785bba2SSteffen Klassert struct xfrm_offload *xo = xfrm_offload(skb);
4680bfab79SEric Dumazet int nhlen = -skb_network_offset(skb);
477785bba2SSteffen Klassert
4860d5fcfbSHerbert Xu skb_network_header(skb)[IP6CB(skb)->nhoff] =
4960d5fcfbSHerbert Xu XFRM_MODE_SKB_CB(skb)->protocol;
5060d5fcfbSHerbert Xu
510883ae0eSHerbert Xu #ifndef CONFIG_NETFILTER
520883ae0eSHerbert Xu if (!async)
530883ae0eSHerbert Xu return 1;
540883ae0eSHerbert Xu #endif
550883ae0eSHerbert Xu
56e9cba694SYossi Kuperman __skb_push(skb, nhlen);
577c88e21aSYossi Kuperman ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
58e9cba694SYossi Kuperman skb_postpush_rcsum(skb, skb_network_header(skb), nhlen);
59b05e1066SPatrick McHardy
607785bba2SSteffen Klassert if (xo && (xo->flags & XFRM_GRO)) {
6158fbfecaSPaul Davey /* The full l2 header needs to be preserved so that re-injecting the packet at l2
6258fbfecaSPaul Davey * works correctly in the presence of vlan tags.
6358fbfecaSPaul Davey */
6458fbfecaSPaul Davey skb_mac_header_rebuild_full(skb, xo->orig_mac_len);
6558fbfecaSPaul Davey skb_reset_network_header(skb);
66bfc0698bSSowmini Varadhan skb_reset_transport_header(skb);
670146dca7SSabrina Dubroca return 0;
687785bba2SSteffen Klassert }
697785bba2SSteffen Klassert
7029a26a56SEric W. Biederman NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING,
7129a26a56SEric W. Biederman dev_net(skb->dev), NULL, skb, skb->dev, NULL,
72acf568eeSHerbert Xu xfrm6_transport_finish2);
730146dca7SSabrina Dubroca return 0;
740146dca7SSabrina Dubroca }
750146dca7SSabrina Dubroca
__xfrm6_udp_encap_rcv(struct sock * sk,struct sk_buff * skb,bool pull)76221ddb72SSteffen Klassert static int __xfrm6_udp_encap_rcv(struct sock *sk, struct sk_buff *skb, bool pull)
770146dca7SSabrina Dubroca {
780146dca7SSabrina Dubroca struct udp_sock *up = udp_sk(sk);
790146dca7SSabrina Dubroca struct udphdr *uh;
800146dca7SSabrina Dubroca struct ipv6hdr *ip6h;
810146dca7SSabrina Dubroca int len;
820146dca7SSabrina Dubroca int ip6hlen = sizeof(struct ipv6hdr);
830146dca7SSabrina Dubroca __u8 *udpdata;
840146dca7SSabrina Dubroca __be32 *udpdata32;
8570a36f57SEric Dumazet u16 encap_type;
860146dca7SSabrina Dubroca
8770a36f57SEric Dumazet encap_type = READ_ONCE(up->encap_type);
880146dca7SSabrina Dubroca /* if this is not encapsulated socket, then just return now */
890146dca7SSabrina Dubroca if (!encap_type)
900146dca7SSabrina Dubroca return 1;
910146dca7SSabrina Dubroca
920146dca7SSabrina Dubroca /* If this is a paged skb, make sure we pull up
930146dca7SSabrina Dubroca * whatever data we need to look at. */
940146dca7SSabrina Dubroca len = skb->len - sizeof(struct udphdr);
950146dca7SSabrina Dubroca if (!pskb_may_pull(skb, sizeof(struct udphdr) + min(len, 8)))
960146dca7SSabrina Dubroca return 1;
970146dca7SSabrina Dubroca
980146dca7SSabrina Dubroca /* Now we can get the pointers */
990146dca7SSabrina Dubroca uh = udp_hdr(skb);
1000146dca7SSabrina Dubroca udpdata = (__u8 *)uh + sizeof(struct udphdr);
1010146dca7SSabrina Dubroca udpdata32 = (__be32 *)udpdata;
1020146dca7SSabrina Dubroca
1030146dca7SSabrina Dubroca switch (encap_type) {
1040146dca7SSabrina Dubroca default:
1050146dca7SSabrina Dubroca case UDP_ENCAP_ESPINUDP:
1060146dca7SSabrina Dubroca /* Check if this is a keepalive packet. If so, eat it. */
1070146dca7SSabrina Dubroca if (len == 1 && udpdata[0] == 0xff) {
108221ddb72SSteffen Klassert return -EINVAL;
1090146dca7SSabrina Dubroca } else if (len > sizeof(struct ip_esp_hdr) && udpdata32[0] != 0) {
1100146dca7SSabrina Dubroca /* ESP Packet without Non-ESP header */
1110146dca7SSabrina Dubroca len = sizeof(struct udphdr);
1120146dca7SSabrina Dubroca } else
1130146dca7SSabrina Dubroca /* Must be an IKE packet.. pass it through */
1140146dca7SSabrina Dubroca return 1;
1150146dca7SSabrina Dubroca break;
1160146dca7SSabrina Dubroca }
1170146dca7SSabrina Dubroca
1180146dca7SSabrina Dubroca /* At this point we are sure that this is an ESPinUDP packet,
1190146dca7SSabrina Dubroca * so we need to remove 'len' bytes from the packet (the UDP
1200146dca7SSabrina Dubroca * header and optional ESP marker bytes) and then modify the
1210146dca7SSabrina Dubroca * protocol to ESP, and then call into the transform receiver.
1220146dca7SSabrina Dubroca */
1230146dca7SSabrina Dubroca if (skb_unclone(skb, GFP_ATOMIC))
124221ddb72SSteffen Klassert return -EINVAL;
1250146dca7SSabrina Dubroca
1260146dca7SSabrina Dubroca /* Now we can update and verify the packet length... */
1270146dca7SSabrina Dubroca ip6h = ipv6_hdr(skb);
1280146dca7SSabrina Dubroca ip6h->payload_len = htons(ntohs(ip6h->payload_len) - len);
1290146dca7SSabrina Dubroca if (skb->len < ip6hlen + len) {
1300146dca7SSabrina Dubroca /* packet is too small!?! */
131221ddb72SSteffen Klassert return -EINVAL;
1320146dca7SSabrina Dubroca }
1330146dca7SSabrina Dubroca
1340146dca7SSabrina Dubroca /* pull the data buffer up to the ESP header and set the
1350146dca7SSabrina Dubroca * transport header to point to ESP. Keep UDP on the stack
1360146dca7SSabrina Dubroca * for later.
1370146dca7SSabrina Dubroca */
138221ddb72SSteffen Klassert if (pull) {
1390146dca7SSabrina Dubroca __skb_pull(skb, len);
1400146dca7SSabrina Dubroca skb_reset_transport_header(skb);
141221ddb72SSteffen Klassert } else {
142221ddb72SSteffen Klassert skb_set_transport_header(skb, len);
143221ddb72SSteffen Klassert }
1440146dca7SSabrina Dubroca
1450146dca7SSabrina Dubroca /* process ESP */
146221ddb72SSteffen Klassert return 0;
147221ddb72SSteffen Klassert }
1480146dca7SSabrina Dubroca
149221ddb72SSteffen Klassert /* If it's a keepalive packet, then just eat it.
150221ddb72SSteffen Klassert * If it's an encapsulated packet, then pass it to the
151221ddb72SSteffen Klassert * IPsec xfrm input.
152221ddb72SSteffen Klassert * Returns 0 if skb passed to xfrm or was dropped.
153221ddb72SSteffen Klassert * Returns >0 if skb should be passed to UDP.
154221ddb72SSteffen Klassert * Returns <0 if skb should be resubmitted (-ret is protocol)
155221ddb72SSteffen Klassert */
xfrm6_udp_encap_rcv(struct sock * sk,struct sk_buff * skb)156221ddb72SSteffen Klassert int xfrm6_udp_encap_rcv(struct sock *sk, struct sk_buff *skb)
157221ddb72SSteffen Klassert {
158221ddb72SSteffen Klassert int ret;
159221ddb72SSteffen Klassert
16053a5b4f2SSteffen Klassert if (skb->protocol == htons(ETH_P_IP))
16153a5b4f2SSteffen Klassert return xfrm4_udp_encap_rcv(sk, skb);
16253a5b4f2SSteffen Klassert
163221ddb72SSteffen Klassert ret = __xfrm6_udp_encap_rcv(sk, skb, true);
164221ddb72SSteffen Klassert if (!ret)
165221ddb72SSteffen Klassert return xfrm6_rcv_encap(skb, IPPROTO_ESP, 0,
166221ddb72SSteffen Klassert udp_sk(sk)->encap_type);
167221ddb72SSteffen Klassert
168221ddb72SSteffen Klassert if (ret < 0) {
1690146dca7SSabrina Dubroca kfree_skb(skb);
1700146dca7SSabrina Dubroca return 0;
1711da177e4SLinus Torvalds }
1721da177e4SLinus Torvalds
173221ddb72SSteffen Klassert return ret;
174221ddb72SSteffen Klassert }
175221ddb72SSteffen Klassert
xfrm6_gro_udp_encap_rcv(struct sock * sk,struct list_head * head,struct sk_buff * skb)176221ddb72SSteffen Klassert struct sk_buff *xfrm6_gro_udp_encap_rcv(struct sock *sk, struct list_head *head,
177221ddb72SSteffen Klassert struct sk_buff *skb)
178221ddb72SSteffen Klassert {
179221ddb72SSteffen Klassert int offset = skb_gro_offset(skb);
180221ddb72SSteffen Klassert const struct net_offload *ops;
181221ddb72SSteffen Klassert struct sk_buff *pp = NULL;
182221ddb72SSteffen Klassert int ret;
183221ddb72SSteffen Klassert
18453a5b4f2SSteffen Klassert if (skb->protocol == htons(ETH_P_IP))
18553a5b4f2SSteffen Klassert return xfrm4_gro_udp_encap_rcv(sk, head, skb);
18653a5b4f2SSteffen Klassert
187221ddb72SSteffen Klassert offset = offset - sizeof(struct udphdr);
188221ddb72SSteffen Klassert
189221ddb72SSteffen Klassert if (!pskb_pull(skb, offset))
190221ddb72SSteffen Klassert return NULL;
191221ddb72SSteffen Klassert
192221ddb72SSteffen Klassert rcu_read_lock();
193221ddb72SSteffen Klassert ops = rcu_dereference(inet6_offloads[IPPROTO_ESP]);
194221ddb72SSteffen Klassert if (!ops || !ops->callbacks.gro_receive)
195221ddb72SSteffen Klassert goto out;
196221ddb72SSteffen Klassert
197221ddb72SSteffen Klassert ret = __xfrm6_udp_encap_rcv(sk, skb, false);
198221ddb72SSteffen Klassert if (ret)
199221ddb72SSteffen Klassert goto out;
200221ddb72SSteffen Klassert
201221ddb72SSteffen Klassert skb_push(skb, offset);
202221ddb72SSteffen Klassert NAPI_GRO_CB(skb)->proto = IPPROTO_UDP;
203221ddb72SSteffen Klassert
204221ddb72SSteffen Klassert pp = call_gro_receive(ops->callbacks.gro_receive, head, skb);
205221ddb72SSteffen Klassert rcu_read_unlock();
206221ddb72SSteffen Klassert
207221ddb72SSteffen Klassert return pp;
208221ddb72SSteffen Klassert
209221ddb72SSteffen Klassert out:
210221ddb72SSteffen Klassert rcu_read_unlock();
211221ddb72SSteffen Klassert skb_push(skb, offset);
212221ddb72SSteffen Klassert NAPI_GRO_CB(skb)->same_flow = 0;
213221ddb72SSteffen Klassert NAPI_GRO_CB(skb)->flush = 1;
214221ddb72SSteffen Klassert
215221ddb72SSteffen Klassert return NULL;
216221ddb72SSteffen Klassert }
217221ddb72SSteffen Klassert
xfrm6_rcv_tnl(struct sk_buff * skb,struct ip6_tnl * t)21863c43787SNicolas Dichtel int xfrm6_rcv_tnl(struct sk_buff *skb, struct ip6_tnl *t)
2191da177e4SLinus Torvalds {
22033b5ecb8SHerbert Xu return xfrm6_rcv_spi(skb, skb_network_header(skb)[IP6CB(skb)->nhoff],
22163c43787SNicolas Dichtel 0, t);
22263c43787SNicolas Dichtel }
22363c43787SNicolas Dichtel EXPORT_SYMBOL(xfrm6_rcv_tnl);
22463c43787SNicolas Dichtel
xfrm6_rcv(struct sk_buff * skb)22563c43787SNicolas Dichtel int xfrm6_rcv(struct sk_buff *skb)
22663c43787SNicolas Dichtel {
22763c43787SNicolas Dichtel return xfrm6_rcv_tnl(skb, NULL);
2281da177e4SLinus Torvalds }
2297159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(xfrm6_rcv);
xfrm6_input_addr(struct sk_buff * skb,xfrm_address_t * daddr,xfrm_address_t * saddr,u8 proto)230fbd9a5b4SMasahide NAKAMURA int xfrm6_input_addr(struct sk_buff *skb, xfrm_address_t *daddr,
231fbd9a5b4SMasahide NAKAMURA xfrm_address_t *saddr, u8 proto)
232fbd9a5b4SMasahide NAKAMURA {
23359c9940eSAlexey Dobriyan struct net *net = dev_net(skb->dev);
234fbd9a5b4SMasahide NAKAMURA struct xfrm_state *x = NULL;
2350ca64da1SFlorian Westphal struct sec_path *sp;
236fbd9a5b4SMasahide NAKAMURA int i = 0;
237fbd9a5b4SMasahide NAKAMURA
2380ca64da1SFlorian Westphal sp = secpath_set(skb);
2390ca64da1SFlorian Westphal if (!sp) {
24059c9940eSAlexey Dobriyan XFRM_INC_STATS(net, LINUX_MIB_XFRMINERROR);
2419473e1f6SMasahide NAKAMURA goto drop;
2429473e1f6SMasahide NAKAMURA }
2439473e1f6SMasahide NAKAMURA
2440ca64da1SFlorian Westphal if (1 + sp->len == XFRM_MAX_DEPTH) {
24559c9940eSAlexey Dobriyan XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
2469473e1f6SMasahide NAKAMURA goto drop;
2479473e1f6SMasahide NAKAMURA }
2489473e1f6SMasahide NAKAMURA
249fbd9a5b4SMasahide NAKAMURA for (i = 0; i < 3; i++) {
250fbd9a5b4SMasahide NAKAMURA xfrm_address_t *dst, *src;
251a002c6fdSYOSHIFUJI Hideaki
252fbd9a5b4SMasahide NAKAMURA switch (i) {
253fbd9a5b4SMasahide NAKAMURA case 0:
254fbd9a5b4SMasahide NAKAMURA dst = daddr;
255fbd9a5b4SMasahide NAKAMURA src = saddr;
256fbd9a5b4SMasahide NAKAMURA break;
257fbd9a5b4SMasahide NAKAMURA case 1:
258fbd9a5b4SMasahide NAKAMURA /* lookup state with wild-card source address */
259fbd9a5b4SMasahide NAKAMURA dst = daddr;
260a002c6fdSYOSHIFUJI Hideaki src = (xfrm_address_t *)&in6addr_any;
261fbd9a5b4SMasahide NAKAMURA break;
262fbd9a5b4SMasahide NAKAMURA default:
263fbd9a5b4SMasahide NAKAMURA /* lookup state with wild-card addresses */
264a002c6fdSYOSHIFUJI Hideaki dst = (xfrm_address_t *)&in6addr_any;
265a002c6fdSYOSHIFUJI Hideaki src = (xfrm_address_t *)&in6addr_any;
266fbd9a5b4SMasahide NAKAMURA break;
267fbd9a5b4SMasahide NAKAMURA }
268fbd9a5b4SMasahide NAKAMURA
269bd55775cSJamal Hadi Salim x = xfrm_state_lookup_byaddr(net, skb->mark, dst, src, proto, AF_INET6);
270fbd9a5b4SMasahide NAKAMURA if (!x)
271fbd9a5b4SMasahide NAKAMURA continue;
272fbd9a5b4SMasahide NAKAMURA
273*304b44f0SAntony Antony if (unlikely(x->dir && x->dir != XFRM_SA_DIR_IN)) {
274*304b44f0SAntony Antony XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEDIRERROR);
275*304b44f0SAntony Antony xfrm_state_put(x);
276*304b44f0SAntony Antony x = NULL;
277*304b44f0SAntony Antony continue;
278*304b44f0SAntony Antony }
279*304b44f0SAntony Antony
280fbd9a5b4SMasahide NAKAMURA spin_lock(&x->lock);
281fbd9a5b4SMasahide NAKAMURA
282a002c6fdSYOSHIFUJI Hideaki if ((!i || (x->props.flags & XFRM_STATE_WILDRECV)) &&
283a002c6fdSYOSHIFUJI Hideaki likely(x->km.state == XFRM_STATE_VALID) &&
284a002c6fdSYOSHIFUJI Hideaki !xfrm_state_check_expire(x)) {
285fbd9a5b4SMasahide NAKAMURA spin_unlock(&x->lock);
286a002c6fdSYOSHIFUJI Hideaki if (x->type->input(x, skb) > 0) {
287a002c6fdSYOSHIFUJI Hideaki /* found a valid state */
2889473e1f6SMasahide NAKAMURA break;
2899473e1f6SMasahide NAKAMURA }
290a002c6fdSYOSHIFUJI Hideaki } else
291a002c6fdSYOSHIFUJI Hideaki spin_unlock(&x->lock);
292a002c6fdSYOSHIFUJI Hideaki
293a002c6fdSYOSHIFUJI Hideaki xfrm_state_put(x);
294a002c6fdSYOSHIFUJI Hideaki x = NULL;
295a002c6fdSYOSHIFUJI Hideaki }
2969473e1f6SMasahide NAKAMURA
2979473e1f6SMasahide NAKAMURA if (!x) {
29859c9940eSAlexey Dobriyan XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOSTATES);
299afeb14b4SPaul Moore xfrm_audit_state_notfound_simple(skb, AF_INET6);
3009473e1f6SMasahide NAKAMURA goto drop;
3019473e1f6SMasahide NAKAMURA }
3029473e1f6SMasahide NAKAMURA
3032294be0fSFlorian Westphal sp->xvec[sp->len++] = x;
3049473e1f6SMasahide NAKAMURA
3059473e1f6SMasahide NAKAMURA spin_lock(&x->lock);
3069473e1f6SMasahide NAKAMURA
307fbd9a5b4SMasahide NAKAMURA x->curlft.bytes += skb->len;
308fbd9a5b4SMasahide NAKAMURA x->curlft.packets++;
309fbd9a5b4SMasahide NAKAMURA
310fbd9a5b4SMasahide NAKAMURA spin_unlock(&x->lock);
311fbd9a5b4SMasahide NAKAMURA
312fbd9a5b4SMasahide NAKAMURA return 1;
3139473e1f6SMasahide NAKAMURA
314fbd9a5b4SMasahide NAKAMURA drop:
315fbd9a5b4SMasahide NAKAMURA return -1;
316fbd9a5b4SMasahide NAKAMURA }
3177159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(xfrm6_input_addr);
318