blob: a52cb3fc6df5a9f6186ecc264a3aa07b6c2781b8 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * xfrm6_input.c: based on net/ipv4/xfrm4_input.c
4 *
5 * Authors:
6 * Mitsuru KANDA @USAGI
Ian Morris67ba4152014-08-24 21:53:10 +01007 * Kazunori MIYAZAWA @USAGI
8 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * YOSHIFUJI Hideaki @USAGI
10 * IPv6 support
11 */
12
13#include <linux/module.h>
14#include <linux/string.h>
Patrick McHardyb05e1062006-01-06 23:03:34 -080015#include <linux/netfilter.h>
16#include <linux/netfilter_ipv6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <net/ipv6.h>
18#include <net/xfrm.h>
19
Herbert Xu227620e2007-11-13 21:41:28 -080020int xfrm6_extract_input(struct xfrm_state *x, struct sk_buff *skb)
21{
22 return xfrm6_extract_header(skb);
23}
24
Nicolas Dichtel63c43782016-09-19 16:17:57 +020025int xfrm6_rcv_spi(struct sk_buff *skb, int nexthdr, __be32 spi,
26 struct ip6_tnl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027{
Nicolas Dichtel63c43782016-09-19 16:17:57 +020028 XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 = t;
Herbert Xu2fcb45b2007-12-03 22:54:12 -080029 XFRM_SPI_SKB_CB(skb)->family = AF_INET6;
Herbert Xu716062f2007-11-13 21:44:23 -080030 XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct ipv6hdr, daddr);
31 return xfrm_input(skb, nexthdr, spi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032}
Linus Torvalds1da177e2005-04-16 15:20:36 -070033EXPORT_SYMBOL(xfrm6_rcv_spi);
34
Herbert Xuacf568e2017-12-15 16:40:44 +110035static int xfrm6_transport_finish2(struct net *net, struct sock *sk,
36 struct sk_buff *skb)
37{
38 if (xfrm_trans_queue(skb, ip6_rcv_finish))
39 __kfree_skb(skb);
40 return -1;
41}
42
Herbert Xu716062f2007-11-13 21:44:23 -080043int xfrm6_transport_finish(struct sk_buff *skb, int async)
44{
Steffen Klassert7785bba2017-02-15 09:40:00 +010045 struct xfrm_offload *xo = xfrm_offload(skb);
Yossi Kupermane9cba692017-08-01 12:49:06 +030046 int nhlen = skb->data - skb_network_header(skb);
Steffen Klassert7785bba2017-02-15 09:40:00 +010047
Herbert Xu60d5fcf2007-11-19 18:47:58 -080048 skb_network_header(skb)[IP6CB(skb)->nhoff] =
49 XFRM_MODE_SKB_CB(skb)->protocol;
50
Herbert Xu0883ae02007-12-30 21:10:14 -080051#ifndef CONFIG_NETFILTER
52 if (!async)
53 return 1;
54#endif
55
Yossi Kupermane9cba692017-08-01 12:49:06 +030056 __skb_push(skb, nhlen);
Yossi Kuperman7c88e212017-06-22 11:37:10 +030057 ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
Yossi Kupermane9cba692017-08-01 12:49:06 +030058 skb_postpush_rcsum(skb, skb_network_header(skb), nhlen);
Herbert Xu716062f2007-11-13 21:44:23 -080059
Steffen Klassert7785bba2017-02-15 09:40:00 +010060 if (xo && (xo->flags & XFRM_GRO)) {
61 skb_mac_header_rebuild(skb);
Sowmini Varadhanbfc06982018-09-03 04:36:52 -070062 skb_reset_transport_header(skb);
Steffen Klassert7785bba2017-02-15 09:40:00 +010063 return -1;
64 }
65
Eric W. Biederman29a26a52015-09-15 20:04:16 -050066 NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING,
67 dev_net(skb->dev), NULL, skb, skb->dev, NULL,
Herbert Xuacf568e2017-12-15 16:40:44 +110068 xfrm6_transport_finish2);
Herbert Xu716062f2007-11-13 21:44:23 -080069 return -1;
Herbert Xu716062f2007-11-13 21:44:23 -080070}
71
Nicolas Dichtel63c43782016-09-19 16:17:57 +020072int xfrm6_rcv_tnl(struct sk_buff *skb, struct ip6_tnl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
Herbert Xu33b5ecb2007-10-17 21:29:25 -070074 return xfrm6_rcv_spi(skb, skb_network_header(skb)[IP6CB(skb)->nhoff],
Nicolas Dichtel63c43782016-09-19 16:17:57 +020075 0, t);
76}
77EXPORT_SYMBOL(xfrm6_rcv_tnl);
78
79int xfrm6_rcv(struct sk_buff *skb)
80{
81 return xfrm6_rcv_tnl(skb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +090083EXPORT_SYMBOL(xfrm6_rcv);
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -070084int xfrm6_input_addr(struct sk_buff *skb, xfrm_address_t *daddr,
85 xfrm_address_t *saddr, u8 proto)
86{
Alexey Dobriyan59c99402008-11-25 17:59:52 -080087 struct net *net = dev_net(skb->dev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090088 struct xfrm_state *x = NULL;
Florian Westphal0ca64da2018-12-18 17:15:18 +010089 struct sec_path *sp;
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -070090 int i = 0;
91
Florian Westphal0ca64da2018-12-18 17:15:18 +010092 sp = secpath_set(skb);
93 if (!sp) {
Steffen Klassertb0fcee82017-02-15 09:39:24 +010094 XFRM_INC_STATS(net, LINUX_MIB_XFRMINERROR);
95 goto drop;
Masahide NAKAMURA9473e1f2007-12-20 20:41:57 -080096 }
97
Florian Westphal0ca64da2018-12-18 17:15:18 +010098 if (1 + sp->len == XFRM_MAX_DEPTH) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -080099 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
Masahide NAKAMURA9473e1f2007-12-20 20:41:57 -0800100 goto drop;
101 }
102
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700103 for (i = 0; i < 3; i++) {
104 xfrm_address_t *dst, *src;
YOSHIFUJI Hideakia002c6f2008-02-19 17:24:33 +0900105
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700106 switch (i) {
107 case 0:
108 dst = daddr;
109 src = saddr;
110 break;
111 case 1:
112 /* lookup state with wild-card source address */
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700113 dst = daddr;
YOSHIFUJI Hideakia002c6f2008-02-19 17:24:33 +0900114 src = (xfrm_address_t *)&in6addr_any;
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700115 break;
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700116 default:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900117 /* lookup state with wild-card addresses */
YOSHIFUJI Hideakia002c6f2008-02-19 17:24:33 +0900118 dst = (xfrm_address_t *)&in6addr_any;
119 src = (xfrm_address_t *)&in6addr_any;
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700120 break;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900121 }
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700122
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800123 x = xfrm_state_lookup_byaddr(net, skb->mark, dst, src, proto, AF_INET6);
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700124 if (!x)
125 continue;
126
127 spin_lock(&x->lock);
128
YOSHIFUJI Hideakia002c6f2008-02-19 17:24:33 +0900129 if ((!i || (x->props.flags & XFRM_STATE_WILDRECV)) &&
130 likely(x->km.state == XFRM_STATE_VALID) &&
131 !xfrm_state_check_expire(x)) {
132 spin_unlock(&x->lock);
133 if (x->type->input(x, skb) > 0) {
134 /* found a valid state */
135 break;
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700136 }
YOSHIFUJI Hideakia002c6f2008-02-19 17:24:33 +0900137 } else
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700138 spin_unlock(&x->lock);
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700139
YOSHIFUJI Hideakia002c6f2008-02-19 17:24:33 +0900140 xfrm_state_put(x);
141 x = NULL;
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700142 }
143
Masahide NAKAMURA9473e1f2007-12-20 20:41:57 -0800144 if (!x) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800145 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOSTATES);
Paul Mooreafeb14b2007-12-21 14:58:11 -0800146 xfrm_audit_state_notfound_simple(skb, AF_INET6);
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700147 goto drop;
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700148 }
149
Florian Westphal2294be0f2018-12-18 17:15:20 +0100150 sp->xvec[sp->len++] = x;
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700151
Masahide NAKAMURA9473e1f2007-12-20 20:41:57 -0800152 spin_lock(&x->lock);
153
154 x->curlft.bytes += skb->len;
155 x->curlft.packets++;
156
157 spin_unlock(&x->lock);
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700158
159 return 1;
Masahide NAKAMURA9473e1f2007-12-20 20:41:57 -0800160
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700161drop:
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700162 return -1;
163}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900164EXPORT_SYMBOL(xfrm6_input_addr);