blob: b5789562aded9274f706225df3f69e45efac5008 [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{
Herbert Xu60d5fcf2007-11-19 18:47:58 -080036 skb_network_header(skb)[IP6CB(skb)->nhoff] =
37 XFRM_MODE_SKB_CB(skb)->protocol;
38
Herbert Xu0883ae02007-12-30 21:10:14 -080039#ifndef CONFIG_NETFILTER
40 if (!async)
41 return 1;
42#endif
43
Herbert Xu716062f2007-11-13 21:44:23 -080044 ipv6_hdr(skb)->payload_len = htons(skb->len);
45 __skb_push(skb, skb->data - skb_network_header(skb));
46
Eric W. Biederman29a26a52015-09-15 20:04:16 -050047 NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING,
48 dev_net(skb->dev), NULL, skb, skb->dev, NULL,
Herbert Xu716062f2007-11-13 21:44:23 -080049 ip6_rcv_finish);
50 return -1;
Herbert Xu716062f2007-11-13 21:44:23 -080051}
52
Nicolas Dichtel63c43782016-09-19 16:17:57 +020053int xfrm6_rcv_tnl(struct sk_buff *skb, struct ip6_tnl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
Herbert Xu33b5ecb2007-10-17 21:29:25 -070055 return xfrm6_rcv_spi(skb, skb_network_header(skb)[IP6CB(skb)->nhoff],
Nicolas Dichtel63c43782016-09-19 16:17:57 +020056 0, t);
57}
58EXPORT_SYMBOL(xfrm6_rcv_tnl);
59
60int xfrm6_rcv(struct sk_buff *skb)
61{
62 return xfrm6_rcv_tnl(skb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +090064EXPORT_SYMBOL(xfrm6_rcv);
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -070065int xfrm6_input_addr(struct sk_buff *skb, xfrm_address_t *daddr,
66 xfrm_address_t *saddr, u8 proto)
67{
Alexey Dobriyan59c99402008-11-25 17:59:52 -080068 struct net *net = dev_net(skb->dev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090069 struct xfrm_state *x = NULL;
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -070070 int i = 0;
71
Masahide NAKAMURA9473e1f2007-12-20 20:41:57 -080072 /* Allocate new secpath or COW existing one. */
73 if (!skb->sp || atomic_read(&skb->sp->refcnt) != 1) {
74 struct sec_path *sp;
75
76 sp = secpath_dup(skb->sp);
77 if (!sp) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -080078 XFRM_INC_STATS(net, LINUX_MIB_XFRMINERROR);
Masahide NAKAMURA9473e1f2007-12-20 20:41:57 -080079 goto drop;
80 }
81 if (skb->sp)
82 secpath_put(skb->sp);
83 skb->sp = sp;
84 }
85
86 if (1 + skb->sp->len == XFRM_MAX_DEPTH) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -080087 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
Masahide NAKAMURA9473e1f2007-12-20 20:41:57 -080088 goto drop;
89 }
90
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -070091 for (i = 0; i < 3; i++) {
92 xfrm_address_t *dst, *src;
YOSHIFUJI Hideakia002c6f2008-02-19 17:24:33 +090093
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -070094 switch (i) {
95 case 0:
96 dst = daddr;
97 src = saddr;
98 break;
99 case 1:
100 /* lookup state with wild-card source address */
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700101 dst = daddr;
YOSHIFUJI Hideakia002c6f2008-02-19 17:24:33 +0900102 src = (xfrm_address_t *)&in6addr_any;
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700103 break;
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700104 default:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900105 /* lookup state with wild-card addresses */
YOSHIFUJI Hideakia002c6f2008-02-19 17:24:33 +0900106 dst = (xfrm_address_t *)&in6addr_any;
107 src = (xfrm_address_t *)&in6addr_any;
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700108 break;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900109 }
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700110
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800111 x = xfrm_state_lookup_byaddr(net, skb->mark, dst, src, proto, AF_INET6);
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700112 if (!x)
113 continue;
114
115 spin_lock(&x->lock);
116
YOSHIFUJI Hideakia002c6f2008-02-19 17:24:33 +0900117 if ((!i || (x->props.flags & XFRM_STATE_WILDRECV)) &&
118 likely(x->km.state == XFRM_STATE_VALID) &&
119 !xfrm_state_check_expire(x)) {
120 spin_unlock(&x->lock);
121 if (x->type->input(x, skb) > 0) {
122 /* found a valid state */
123 break;
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700124 }
YOSHIFUJI Hideakia002c6f2008-02-19 17:24:33 +0900125 } else
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700126 spin_unlock(&x->lock);
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700127
YOSHIFUJI Hideakia002c6f2008-02-19 17:24:33 +0900128 xfrm_state_put(x);
129 x = NULL;
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700130 }
131
Masahide NAKAMURA9473e1f2007-12-20 20:41:57 -0800132 if (!x) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800133 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOSTATES);
Paul Mooreafeb14b2007-12-21 14:58:11 -0800134 xfrm_audit_state_notfound_simple(skb, AF_INET6);
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700135 goto drop;
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700136 }
137
Masahide NAKAMURA9473e1f2007-12-20 20:41:57 -0800138 skb->sp->xvec[skb->sp->len++] = x;
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700139
Masahide NAKAMURA9473e1f2007-12-20 20:41:57 -0800140 spin_lock(&x->lock);
141
142 x->curlft.bytes += skb->len;
143 x->curlft.packets++;
144
145 spin_unlock(&x->lock);
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700146
147 return 1;
Masahide NAKAMURA9473e1f2007-12-20 20:41:57 -0800148
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700149drop:
Masahide NAKAMURAfbd9a5b2006-08-23 18:08:21 -0700150 return -1;
151}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900152EXPORT_SYMBOL(xfrm6_input_addr);