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 | |
Nicolas Dichtel | 63c4378 | 2016-09-19 16:17:57 +0200 | [diff] [blame] | 20 | int xfrm6_rcv_spi(struct sk_buff *skb, int nexthdr, __be32 spi, |
| 21 | struct ip6_tnl *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | { |
Nicolas Dichtel | 63c4378 | 2016-09-19 16:17:57 +0200 | [diff] [blame] | 23 | XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 = t; |
Herbert Xu | 2fcb45b | 2007-12-03 22:54:12 -0800 | [diff] [blame] | 24 | XFRM_SPI_SKB_CB(skb)->family = AF_INET6; |
Herbert Xu | 716062f | 2007-11-13 21:44:23 -0800 | [diff] [blame] | 25 | XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct ipv6hdr, daddr); |
| 26 | return xfrm_input(skb, nexthdr, spi, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | EXPORT_SYMBOL(xfrm6_rcv_spi); |
| 29 | |
Herbert Xu | acf568e | 2017-12-15 16:40:44 +1100 | [diff] [blame] | 30 | static int xfrm6_transport_finish2(struct net *net, struct sock *sk, |
| 31 | struct sk_buff *skb) |
| 32 | { |
Sabrina Dubroca | 0146dca | 2020-04-27 17:59:34 +0200 | [diff] [blame] | 33 | if (xfrm_trans_queue(skb, ip6_rcv_finish)) { |
| 34 | kfree_skb(skb); |
| 35 | return NET_RX_DROP; |
| 36 | } |
| 37 | |
| 38 | return 0; |
Herbert Xu | acf568e | 2017-12-15 16:40:44 +1100 | [diff] [blame] | 39 | } |
| 40 | |
Herbert Xu | 716062f | 2007-11-13 21:44:23 -0800 | [diff] [blame] | 41 | int xfrm6_transport_finish(struct sk_buff *skb, int async) |
| 42 | { |
Steffen Klassert | 7785bba | 2017-02-15 09:40:00 +0100 | [diff] [blame] | 43 | struct xfrm_offload *xo = xfrm_offload(skb); |
Yossi Kuperman | e9cba69 | 2017-08-01 12:49:06 +0300 | [diff] [blame] | 44 | int nhlen = skb->data - skb_network_header(skb); |
Steffen Klassert | 7785bba | 2017-02-15 09:40:00 +0100 | [diff] [blame] | 45 | |
Herbert Xu | 60d5fcf | 2007-11-19 18:47:58 -0800 | [diff] [blame] | 46 | skb_network_header(skb)[IP6CB(skb)->nhoff] = |
| 47 | XFRM_MODE_SKB_CB(skb)->protocol; |
| 48 | |
Herbert Xu | 0883ae0 | 2007-12-30 21:10:14 -0800 | [diff] [blame] | 49 | #ifndef CONFIG_NETFILTER |
| 50 | if (!async) |
| 51 | return 1; |
| 52 | #endif |
| 53 | |
Yossi Kuperman | e9cba69 | 2017-08-01 12:49:06 +0300 | [diff] [blame] | 54 | __skb_push(skb, nhlen); |
Yossi Kuperman | 7c88e21 | 2017-06-22 11:37:10 +0300 | [diff] [blame] | 55 | ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr)); |
Yossi Kuperman | e9cba69 | 2017-08-01 12:49:06 +0300 | [diff] [blame] | 56 | skb_postpush_rcsum(skb, skb_network_header(skb), nhlen); |
Herbert Xu | 716062f | 2007-11-13 21:44:23 -0800 | [diff] [blame] | 57 | |
Steffen Klassert | 7785bba | 2017-02-15 09:40:00 +0100 | [diff] [blame] | 58 | if (xo && (xo->flags & XFRM_GRO)) { |
| 59 | skb_mac_header_rebuild(skb); |
Sowmini Varadhan | bfc0698 | 2018-09-03 04:36:52 -0700 | [diff] [blame] | 60 | skb_reset_transport_header(skb); |
Sabrina Dubroca | 0146dca | 2020-04-27 17:59:34 +0200 | [diff] [blame] | 61 | return 0; |
Steffen Klassert | 7785bba | 2017-02-15 09:40:00 +0100 | [diff] [blame] | 62 | } |
| 63 | |
Eric W. Biederman | 29a26a5 | 2015-09-15 20:04:16 -0500 | [diff] [blame] | 64 | NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING, |
| 65 | dev_net(skb->dev), NULL, skb, skb->dev, NULL, |
Herbert Xu | acf568e | 2017-12-15 16:40:44 +1100 | [diff] [blame] | 66 | xfrm6_transport_finish2); |
Sabrina Dubroca | 0146dca | 2020-04-27 17:59:34 +0200 | [diff] [blame] | 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | /* If it's a keepalive packet, then just eat it. |
| 71 | * If it's an encapsulated packet, then pass it to the |
| 72 | * IPsec xfrm input. |
| 73 | * Returns 0 if skb passed to xfrm or was dropped. |
| 74 | * Returns >0 if skb should be passed to UDP. |
| 75 | * Returns <0 if skb should be resubmitted (-ret is protocol) |
| 76 | */ |
| 77 | int xfrm6_udp_encap_rcv(struct sock *sk, struct sk_buff *skb) |
| 78 | { |
| 79 | struct udp_sock *up = udp_sk(sk); |
| 80 | struct udphdr *uh; |
| 81 | struct ipv6hdr *ip6h; |
| 82 | int len; |
| 83 | int ip6hlen = sizeof(struct ipv6hdr); |
| 84 | |
| 85 | __u8 *udpdata; |
| 86 | __be32 *udpdata32; |
| 87 | __u16 encap_type = up->encap_type; |
| 88 | |
| 89 | /* if this is not encapsulated socket, then just return now */ |
| 90 | if (!encap_type) |
| 91 | return 1; |
| 92 | |
| 93 | /* If this is a paged skb, make sure we pull up |
| 94 | * whatever data we need to look at. */ |
| 95 | len = skb->len - sizeof(struct udphdr); |
| 96 | if (!pskb_may_pull(skb, sizeof(struct udphdr) + min(len, 8))) |
| 97 | return 1; |
| 98 | |
| 99 | /* Now we can get the pointers */ |
| 100 | uh = udp_hdr(skb); |
| 101 | udpdata = (__u8 *)uh + sizeof(struct udphdr); |
| 102 | udpdata32 = (__be32 *)udpdata; |
| 103 | |
| 104 | switch (encap_type) { |
| 105 | default: |
| 106 | case UDP_ENCAP_ESPINUDP: |
| 107 | /* Check if this is a keepalive packet. If so, eat it. */ |
| 108 | if (len == 1 && udpdata[0] == 0xff) { |
| 109 | goto drop; |
| 110 | } else if (len > sizeof(struct ip_esp_hdr) && udpdata32[0] != 0) { |
| 111 | /* ESP Packet without Non-ESP header */ |
| 112 | len = sizeof(struct udphdr); |
| 113 | } else |
| 114 | /* Must be an IKE packet.. pass it through */ |
| 115 | return 1; |
| 116 | break; |
| 117 | case UDP_ENCAP_ESPINUDP_NON_IKE: |
| 118 | /* Check if this is a keepalive packet. If so, eat it. */ |
| 119 | if (len == 1 && udpdata[0] == 0xff) { |
| 120 | goto drop; |
| 121 | } else if (len > 2 * sizeof(u32) + sizeof(struct ip_esp_hdr) && |
| 122 | udpdata32[0] == 0 && udpdata32[1] == 0) { |
| 123 | |
| 124 | /* ESP Packet with Non-IKE marker */ |
| 125 | len = sizeof(struct udphdr) + 2 * sizeof(u32); |
| 126 | } else |
| 127 | /* Must be an IKE packet.. pass it through */ |
| 128 | return 1; |
| 129 | break; |
| 130 | } |
| 131 | |
| 132 | /* At this point we are sure that this is an ESPinUDP packet, |
| 133 | * so we need to remove 'len' bytes from the packet (the UDP |
| 134 | * header and optional ESP marker bytes) and then modify the |
| 135 | * protocol to ESP, and then call into the transform receiver. |
| 136 | */ |
| 137 | if (skb_unclone(skb, GFP_ATOMIC)) |
| 138 | goto drop; |
| 139 | |
| 140 | /* Now we can update and verify the packet length... */ |
| 141 | ip6h = ipv6_hdr(skb); |
| 142 | ip6h->payload_len = htons(ntohs(ip6h->payload_len) - len); |
| 143 | if (skb->len < ip6hlen + len) { |
| 144 | /* packet is too small!?! */ |
| 145 | goto drop; |
| 146 | } |
| 147 | |
| 148 | /* pull the data buffer up to the ESP header and set the |
| 149 | * transport header to point to ESP. Keep UDP on the stack |
| 150 | * for later. |
| 151 | */ |
| 152 | __skb_pull(skb, len); |
| 153 | skb_reset_transport_header(skb); |
| 154 | |
| 155 | /* process ESP */ |
| 156 | return xfrm6_rcv_encap(skb, IPPROTO_ESP, 0, encap_type); |
| 157 | |
| 158 | drop: |
| 159 | kfree_skb(skb); |
| 160 | return 0; |
Herbert Xu | 716062f | 2007-11-13 21:44:23 -0800 | [diff] [blame] | 161 | } |
| 162 | |
Nicolas Dichtel | 63c4378 | 2016-09-19 16:17:57 +0200 | [diff] [blame] | 163 | int xfrm6_rcv_tnl(struct sk_buff *skb, struct ip6_tnl *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | { |
Herbert Xu | 33b5ecb | 2007-10-17 21:29:25 -0700 | [diff] [blame] | 165 | return xfrm6_rcv_spi(skb, skb_network_header(skb)[IP6CB(skb)->nhoff], |
Nicolas Dichtel | 63c4378 | 2016-09-19 16:17:57 +0200 | [diff] [blame] | 166 | 0, t); |
| 167 | } |
| 168 | EXPORT_SYMBOL(xfrm6_rcv_tnl); |
| 169 | |
| 170 | int xfrm6_rcv(struct sk_buff *skb) |
| 171 | { |
| 172 | return xfrm6_rcv_tnl(skb, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | } |
YOSHIFUJI Hideaki | 7159039 | 2007-02-22 22:05:40 +0900 | [diff] [blame] | 174 | EXPORT_SYMBOL(xfrm6_rcv); |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 175 | int xfrm6_input_addr(struct sk_buff *skb, xfrm_address_t *daddr, |
| 176 | xfrm_address_t *saddr, u8 proto) |
| 177 | { |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 178 | struct net *net = dev_net(skb->dev); |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 179 | struct xfrm_state *x = NULL; |
Florian Westphal | 0ca64da | 2018-12-18 17:15:18 +0100 | [diff] [blame] | 180 | struct sec_path *sp; |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 181 | int i = 0; |
| 182 | |
Florian Westphal | 0ca64da | 2018-12-18 17:15:18 +0100 | [diff] [blame] | 183 | sp = secpath_set(skb); |
| 184 | if (!sp) { |
Steffen Klassert | b0fcee8 | 2017-02-15 09:39:24 +0100 | [diff] [blame] | 185 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINERROR); |
| 186 | goto drop; |
Masahide NAKAMURA | 9473e1f | 2007-12-20 20:41:57 -0800 | [diff] [blame] | 187 | } |
| 188 | |
Florian Westphal | 0ca64da | 2018-12-18 17:15:18 +0100 | [diff] [blame] | 189 | if (1 + sp->len == XFRM_MAX_DEPTH) { |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 190 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR); |
Masahide NAKAMURA | 9473e1f | 2007-12-20 20:41:57 -0800 | [diff] [blame] | 191 | goto drop; |
| 192 | } |
| 193 | |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 194 | for (i = 0; i < 3; i++) { |
| 195 | xfrm_address_t *dst, *src; |
YOSHIFUJI Hideaki | a002c6f | 2008-02-19 17:24:33 +0900 | [diff] [blame] | 196 | |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 197 | switch (i) { |
| 198 | case 0: |
| 199 | dst = daddr; |
| 200 | src = saddr; |
| 201 | break; |
| 202 | case 1: |
| 203 | /* lookup state with wild-card source address */ |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 204 | dst = daddr; |
YOSHIFUJI Hideaki | a002c6f | 2008-02-19 17:24:33 +0900 | [diff] [blame] | 205 | src = (xfrm_address_t *)&in6addr_any; |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 206 | break; |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 207 | default: |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 208 | /* lookup state with wild-card addresses */ |
YOSHIFUJI Hideaki | a002c6f | 2008-02-19 17:24:33 +0900 | [diff] [blame] | 209 | dst = (xfrm_address_t *)&in6addr_any; |
| 210 | src = (xfrm_address_t *)&in6addr_any; |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 211 | break; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 212 | } |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 213 | |
Jamal Hadi Salim | bd55775 | 2010-02-22 16:20:22 -0800 | [diff] [blame] | 214 | 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] | 215 | if (!x) |
| 216 | continue; |
| 217 | |
| 218 | spin_lock(&x->lock); |
| 219 | |
YOSHIFUJI Hideaki | a002c6f | 2008-02-19 17:24:33 +0900 | [diff] [blame] | 220 | if ((!i || (x->props.flags & XFRM_STATE_WILDRECV)) && |
| 221 | likely(x->km.state == XFRM_STATE_VALID) && |
| 222 | !xfrm_state_check_expire(x)) { |
| 223 | spin_unlock(&x->lock); |
| 224 | if (x->type->input(x, skb) > 0) { |
| 225 | /* found a valid state */ |
| 226 | break; |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 227 | } |
YOSHIFUJI Hideaki | a002c6f | 2008-02-19 17:24:33 +0900 | [diff] [blame] | 228 | } else |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 229 | spin_unlock(&x->lock); |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 230 | |
YOSHIFUJI Hideaki | a002c6f | 2008-02-19 17:24:33 +0900 | [diff] [blame] | 231 | xfrm_state_put(x); |
| 232 | x = NULL; |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Masahide NAKAMURA | 9473e1f | 2007-12-20 20:41:57 -0800 | [diff] [blame] | 235 | if (!x) { |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 236 | XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOSTATES); |
Paul Moore | afeb14b | 2007-12-21 14:58:11 -0800 | [diff] [blame] | 237 | xfrm_audit_state_notfound_simple(skb, AF_INET6); |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 238 | goto drop; |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Florian Westphal | 2294be0f | 2018-12-18 17:15:20 +0100 | [diff] [blame] | 241 | sp->xvec[sp->len++] = x; |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 242 | |
Masahide NAKAMURA | 9473e1f | 2007-12-20 20:41:57 -0800 | [diff] [blame] | 243 | spin_lock(&x->lock); |
| 244 | |
| 245 | x->curlft.bytes += skb->len; |
| 246 | x->curlft.packets++; |
| 247 | |
| 248 | spin_unlock(&x->lock); |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 249 | |
| 250 | return 1; |
Masahide NAKAMURA | 9473e1f | 2007-12-20 20:41:57 -0800 | [diff] [blame] | 251 | |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 252 | drop: |
Masahide NAKAMURA | fbd9a5b | 2006-08-23 18:08:21 -0700 | [diff] [blame] | 253 | return -1; |
| 254 | } |
YOSHIFUJI Hideaki | 7159039 | 2007-02-22 22:05:40 +0900 | [diff] [blame] | 255 | EXPORT_SYMBOL(xfrm6_input_addr); |