blob: 08a807b29298f5a2c14c30eaedcba87f3057431d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * xfrm6_input.c: based on net/ipv4/xfrm4_input.c
3 *
4 * Authors:
5 * Mitsuru KANDA @USAGI
Ian Morris67ba4152014-08-24 21:53:10 +01006 * Kazunori MIYAZAWA @USAGI
7 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * YOSHIFUJI Hideaki @USAGI
9 * IPv6 support
10 */
11
12#include <linux/module.h>
13#include <linux/string.h>
Patrick McHardyb05e1062006-01-06 23:03:34 -080014#include <linux/netfilter.h>
15#include <linux/netfilter_ipv6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <net/ipv6.h>
17#include <net/xfrm.h>
18
Herbert Xu227620e2007-11-13 21:41:28 -080019int xfrm6_extract_input(struct xfrm_state *x, struct sk_buff *skb)
20{
21 return xfrm6_extract_header(skb);
22}
23
Nicolas Dichtel63c43782016-09-19 16:17:57 +020024int xfrm6_rcv_spi(struct sk_buff *skb, int nexthdr, __be32 spi,
25 struct ip6_tnl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -070026{
Nicolas Dichtel63c43782016-09-19 16:17:57 +020027 XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 = t;
Herbert Xu2fcb45b2007-12-03 22:54:12 -080028 XFRM_SPI_SKB_CB(skb)->family = AF_INET6;
Herbert Xu716062f2007-11-13 21:44:23 -080029 XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct ipv6hdr, daddr);
30 return xfrm_input(skb, nexthdr, spi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070031}
Linus Torvalds1da177e2005-04-16 15:20:36 -070032EXPORT_SYMBOL(xfrm6_rcv_spi);
33
Herbert Xu716062f2007-11-13 21:44:23 -080034int xfrm6_transport_finish(struct sk_buff *skb, int async)
35{
Steffen Klassert7785bba2017-02-15 09:40:00 +010036 struct xfrm_offload *xo = xfrm_offload(skb);
37
Herbert Xu60d5fcf2007-11-19 18:47:58 -080038 skb_network_header(skb)[IP6CB(skb)->nhoff] =
39 XFRM_MODE_SKB_CB(skb)->protocol;
40
Herbert Xu0883ae02007-12-30 21:10:14 -080041#ifndef CONFIG_NETFILTER
42 if (!async)
43 return 1;
44#endif
45
Herbert Xu716062f2007-11-13 21:44:23 -080046 ipv6_hdr(skb)->payload_len = htons(skb->len);
47 __skb_push(skb, skb->data - skb_network_header(skb));
48
Steffen Klassert7785bba2017-02-15 09:40:00 +010049 if (xo && (xo->flags & XFRM_GRO)) {
50 skb_mac_header_rebuild(skb);
51 return -1;
52 }
53
Eric W. Biederman29a26a52015-09-15 20:04:16 -050054 NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING,
55 dev_net(skb->dev), NULL, skb, skb->dev, NULL,
Herbert Xu716062f2007-11-13 21:44:23 -080056 ip6_rcv_finish);
57 return -1;
Herbert Xu716062f2007-11-13 21:44:23 -080058}
59
Nicolas Dichtel63c43782016-09-19 16:17:57 +020060int xfrm6_rcv_tnl(struct sk_buff *skb, struct ip6_tnl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
Herbert Xu33b5ecb2007-10-17 21:29:25 -070062 return xfrm6_rcv_spi(skb, skb_network_header(skb)[IP6CB(skb)->nhoff],
Nicolas Dichtel63c43782016-09-19 16:17:57 +020063 0, t);
64}
65EXPORT_SYMBOL(xfrm6_rcv_tnl);
66
67int xfrm6_rcv(struct sk_buff *skb)
68{
69 return xfrm6_rcv_tnl(skb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +090071EXPORT_SYMBOL(xfrm6_rcv);
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -070072int xfrm6_input_addr(struct sk_buff *skb, xfrm_address_t *daddr,
73 xfrm_address_t *saddr, u8 proto)
74{
Alexey Dobriyan59c99402008-11-25 17:59:52 -080075 struct net *net = dev_net(skb->dev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090076 struct xfrm_state *x = NULL;
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -070077 int i = 0;
78
Steffen Klassertb0fcee82017-02-15 09:39:24 +010079 if (secpath_set(skb)) {
80 XFRM_INC_STATS(net, LINUX_MIB_XFRMINERROR);
81 goto drop;
Masahide NAKAMURA9473e1f2007-12-20 20:41:57 -080082 }
83
84 if (1 + skb->sp->len == XFRM_MAX_DEPTH) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -080085 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
Masahide NAKAMURA9473e1f2007-12-20 20:41:57 -080086 goto drop;
87 }
88
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -070089 for (i = 0; i < 3; i++) {
90 xfrm_address_t *dst, *src;
YOSHIFUJI Hideakia002c6f2008-02-19 17:24:33 +090091
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -070092 switch (i) {
93 case 0:
94 dst = daddr;
95 src = saddr;
96 break;
97 case 1:
98 /* lookup state with wild-card source address */
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -070099 dst = daddr;
YOSHIFUJI Hideakia002c6f2008-02-19 17:24:33 +0900100 src = (xfrm_address_t *)&in6addr_any;
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700101 break;
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700102 default:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900103 /* lookup state with wild-card addresses */
YOSHIFUJI Hideakia002c6f2008-02-19 17:24:33 +0900104 dst = (xfrm_address_t *)&in6addr_any;
105 src = (xfrm_address_t *)&in6addr_any;
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700106 break;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900107 }
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700108
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800109 x = xfrm_state_lookup_byaddr(net, skb->mark, dst, src, proto, AF_INET6);
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700110 if (!x)
111 continue;
112
113 spin_lock(&x->lock);
114
YOSHIFUJI Hideakia002c6f2008-02-19 17:24:33 +0900115 if ((!i || (x->props.flags & XFRM_STATE_WILDRECV)) &&
116 likely(x->km.state == XFRM_STATE_VALID) &&
117 !xfrm_state_check_expire(x)) {
118 spin_unlock(&x->lock);
119 if (x->type->input(x, skb) > 0) {
120 /* found a valid state */
121 break;
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700122 }
YOSHIFUJI Hideakia002c6f2008-02-19 17:24:33 +0900123 } else
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700124 spin_unlock(&x->lock);
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700125
YOSHIFUJI Hideakia002c6f2008-02-19 17:24:33 +0900126 xfrm_state_put(x);
127 x = NULL;
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700128 }
129
Masahide NAKAMURA9473e1f2007-12-20 20:41:57 -0800130 if (!x) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800131 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOSTATES);
Paul Mooreafeb14b2007-12-21 14:58:11 -0800132 xfrm_audit_state_notfound_simple(skb, AF_INET6);
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700133 goto drop;
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700134 }
135
Masahide NAKAMURA9473e1f2007-12-20 20:41:57 -0800136 skb->sp->xvec[skb->sp->len++] = x;
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700137
Masahide NAKAMURA9473e1f2007-12-20 20:41:57 -0800138 spin_lock(&x->lock);
139
140 x->curlft.bytes += skb->len;
141 x->curlft.packets++;
142
143 spin_unlock(&x->lock);
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700144
145 return 1;
Masahide NAKAMURA9473e1f2007-12-20 20:41:57 -0800146
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700147drop:
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700148 return -1;
149}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900150EXPORT_SYMBOL(xfrm6_input_addr);