Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * xfrm6_input.c: based on net/ipv4/xfrm4_input.c |
| 3 | * |
| 4 | * Authors: |
| 5 | * Mitsuru KANDA @USAGI |
Ian Morris | 67ba415 | 2014-08-24 21:53:10 +0100 | [diff] [blame] | 6 | * Kazunori MIYAZAWA @USAGI |
| 7 | * Kunihiro Ishiguro <kunihiro@ipinfusion.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * YOSHIFUJI Hideaki @USAGI |
| 9 | * IPv6 support |
| 10 | */ |
| 11 | |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/string.h> |
Patrick McHardy | b05e106 | 2006-01-06 23:03:34 -0800 | [diff] [blame] | 14 | #include <linux/netfilter.h> |
| 15 | #include <linux/netfilter_ipv6.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <net/ipv6.h> |
| 17 | #include <net/xfrm.h> |
| 18 | |
Herbert Xu | 227620e | 2007-11-13 21:41:28 -0800 | [diff] [blame] | 19 | int xfrm6_extract_input(struct xfrm_state *x, struct sk_buff *skb) |
| 20 | { |
| 21 | return xfrm6_extract_header(skb); |
| 22 | } |
| 23 | |
Nicolas Dichtel | 63c4378 | 2016-09-19 16:17:57 +0200 | [diff] [blame] | 24 | int xfrm6_rcv_spi(struct sk_buff *skb, int nexthdr, __be32 spi, |
| 25 | struct ip6_tnl *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | { |
Nicolas Dichtel | 63c4378 | 2016-09-19 16:17:57 +0200 | [diff] [blame] | 27 | XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 = t; |
Herbert Xu | 2fcb45b | 2007-12-03 22:54:12 -0800 | [diff] [blame] | 28 | XFRM_SPI_SKB_CB(skb)->family = AF_INET6; |
Herbert Xu | 716062f | 2007-11-13 21:44:23 -0800 | [diff] [blame] | 29 | XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct ipv6hdr, daddr); |
| 30 | return xfrm_input(skb, nexthdr, spi, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | EXPORT_SYMBOL(xfrm6_rcv_spi); |
| 33 | |
Herbert Xu | 716062f | 2007-11-13 21:44:23 -0800 | [diff] [blame] | 34 | int xfrm6_transport_finish(struct sk_buff *skb, int async) |
| 35 | { |
Steffen Klassert | 7785bba | 2017-02-15 09:40:00 +0100 | [diff] [blame] | 36 | struct xfrm_offload *xo = xfrm_offload(skb); |
| 37 | |
Herbert Xu | 60d5fcf | 2007-11-19 18:47:58 -0800 | [diff] [blame] | 38 | skb_network_header(skb)[IP6CB(skb)->nhoff] = |
| 39 | XFRM_MODE_SKB_CB(skb)->protocol; |
| 40 | |
Herbert Xu | 0883ae0 | 2007-12-30 21:10:14 -0800 | [diff] [blame] | 41 | #ifndef CONFIG_NETFILTER |
| 42 | if (!async) |
| 43 | return 1; |
| 44 | #endif |
| 45 | |
Herbert Xu | 716062f | 2007-11-13 21:44:23 -0800 | [diff] [blame] | 46 | ipv6_hdr(skb)->payload_len = htons(skb->len); |
| 47 | __skb_push(skb, skb->data - skb_network_header(skb)); |
| 48 | |
Steffen Klassert | 7785bba | 2017-02-15 09:40:00 +0100 | [diff] [blame] | 49 | if (xo && (xo->flags & XFRM_GRO)) { |
| 50 | skb_mac_header_rebuild(skb); |
| 51 | return -1; |
| 52 | } |
| 53 | |
Eric W. Biederman | 29a26a5 | 2015-09-15 20:04:16 -0500 | [diff] [blame] | 54 | NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING, |
| 55 | dev_net(skb->dev), NULL, skb, skb->dev, NULL, |
Herbert Xu | 716062f | 2007-11-13 21:44:23 -0800 | [diff] [blame] | 56 | ip6_rcv_finish); |
| 57 | return -1; |
Herbert Xu | 716062f | 2007-11-13 21:44:23 -0800 | [diff] [blame] | 58 | } |
| 59 | |
Nicolas Dichtel | 63c4378 | 2016-09-19 16:17:57 +0200 | [diff] [blame] | 60 | int xfrm6_rcv_tnl(struct sk_buff *skb, struct ip6_tnl *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | { |
Herbert Xu | 33b5ecb | 2007-10-17 21:29:25 -0700 | [diff] [blame] | 62 | return xfrm6_rcv_spi(skb, skb_network_header(skb)[IP6CB(skb)->nhoff], |
Nicolas Dichtel | 63c4378 | 2016-09-19 16:17:57 +0200 | [diff] [blame] | 63 | 0, t); |
| 64 | } |
| 65 | EXPORT_SYMBOL(xfrm6_rcv_tnl); |
| 66 | |
| 67 | int xfrm6_rcv(struct sk_buff *skb) |
| 68 | { |
| 69 | return xfrm6_rcv_tnl(skb, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | } |
YOSHIFUJI Hideaki | 7159039 | 2007-02-22 22:05:40 +0900 | [diff] [blame] | 71 | EXPORT_SYMBOL(xfrm6_rcv); |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 72 | int xfrm6_input_addr(struct sk_buff *skb, xfrm_address_t *daddr, |
| 73 | xfrm_address_t *saddr, u8 proto) |
| 74 | { |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 75 | struct net *net = dev_net(skb->dev); |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 76 | struct xfrm_state *x = NULL; |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 77 | int i = 0; |
| 78 | |
Steffen Klassert | b0fcee8 | 2017-02-15 09:39:24 +0100 | [diff] [blame] | 79 | if (secpath_set(skb)) { |
| 80 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINERROR); |
| 81 | goto drop; |
Masahide NAKAMURA | 9473e1f | 2007-12-20 20:41:57 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | if (1 + skb->sp->len == XFRM_MAX_DEPTH) { |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 85 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR); |
Masahide NAKAMURA | 9473e1f | 2007-12-20 20:41:57 -0800 | [diff] [blame] | 86 | goto drop; |
| 87 | } |
| 88 | |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 89 | for (i = 0; i < 3; i++) { |
| 90 | xfrm_address_t *dst, *src; |
YOSHIFUJI Hideaki | a002c6f | 2008-02-19 17:24:33 +0900 | [diff] [blame] | 91 | |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 92 | 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 NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 99 | dst = daddr; |
YOSHIFUJI Hideaki | a002c6f | 2008-02-19 17:24:33 +0900 | [diff] [blame] | 100 | src = (xfrm_address_t *)&in6addr_any; |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 101 | break; |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 102 | default: |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 103 | /* lookup state with wild-card addresses */ |
YOSHIFUJI Hideaki | a002c6f | 2008-02-19 17:24:33 +0900 | [diff] [blame] | 104 | dst = (xfrm_address_t *)&in6addr_any; |
| 105 | src = (xfrm_address_t *)&in6addr_any; |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 106 | break; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 107 | } |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 108 | |
Jamal Hadi Salim | bd55775 | 2010-02-22 16:20:22 -0800 | [diff] [blame] | 109 | x = xfrm_state_lookup_byaddr(net, skb->mark, dst, src, proto, AF_INET6); |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 110 | if (!x) |
| 111 | continue; |
| 112 | |
| 113 | spin_lock(&x->lock); |
| 114 | |
YOSHIFUJI Hideaki | a002c6f | 2008-02-19 17:24:33 +0900 | [diff] [blame] | 115 | 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 NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 122 | } |
YOSHIFUJI Hideaki | a002c6f | 2008-02-19 17:24:33 +0900 | [diff] [blame] | 123 | } else |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 124 | spin_unlock(&x->lock); |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 125 | |
YOSHIFUJI Hideaki | a002c6f | 2008-02-19 17:24:33 +0900 | [diff] [blame] | 126 | xfrm_state_put(x); |
| 127 | x = NULL; |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Masahide NAKAMURA | 9473e1f | 2007-12-20 20:41:57 -0800 | [diff] [blame] | 130 | if (!x) { |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 131 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOSTATES); |
Paul Moore | afeb14b | 2007-12-21 14:58:11 -0800 | [diff] [blame] | 132 | xfrm_audit_state_notfound_simple(skb, AF_INET6); |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 133 | goto drop; |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Masahide NAKAMURA | 9473e1f | 2007-12-20 20:41:57 -0800 | [diff] [blame] | 136 | skb->sp->xvec[skb->sp->len++] = x; |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 137 | |
Masahide NAKAMURA | 9473e1f | 2007-12-20 20:41:57 -0800 | [diff] [blame] | 138 | spin_lock(&x->lock); |
| 139 | |
| 140 | x->curlft.bytes += skb->len; |
| 141 | x->curlft.packets++; |
| 142 | |
| 143 | spin_unlock(&x->lock); |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 144 | |
| 145 | return 1; |
Masahide NAKAMURA | 9473e1f | 2007-12-20 20:41:57 -0800 | [diff] [blame] | 146 | |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 147 | drop: |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 148 | return -1; |
| 149 | } |
YOSHIFUJI Hideaki | 7159039 | 2007-02-22 22:05:40 +0900 | [diff] [blame] | 150 | EXPORT_SYMBOL(xfrm6_input_addr); |