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