Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Herbert Xu | 406ef77 | 2007-10-08 17:16:30 -0700 | [diff] [blame] | 2 | /* |
| 3 | * xfrm_output.c - Common IPsec encapsulation code. |
| 4 | * |
| 5 | * Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au> |
Herbert Xu | 406ef77 | 2007-10-08 17:16:30 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/errno.h> |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/netdevice.h> |
Herbert Xu | 862b82c | 2007-11-13 21:43:11 -0800 | [diff] [blame] | 11 | #include <linux/netfilter.h> |
Herbert Xu | 406ef77 | 2007-10-08 17:16:30 -0700 | [diff] [blame] | 12 | #include <linux/skbuff.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 13 | #include <linux/slab.h> |
Herbert Xu | 406ef77 | 2007-10-08 17:16:30 -0700 | [diff] [blame] | 14 | #include <linux/spinlock.h> |
Herbert Xu | 406ef77 | 2007-10-08 17:16:30 -0700 | [diff] [blame] | 15 | #include <net/dst.h> |
Florian Westphal | 6d64be3 | 2020-05-04 10:06:03 +0200 | [diff] [blame] | 16 | #include <net/icmp.h> |
Florian Westphal | 1de7083 | 2019-03-29 21:16:29 +0100 | [diff] [blame] | 17 | #include <net/inet_ecn.h> |
Herbert Xu | 406ef77 | 2007-10-08 17:16:30 -0700 | [diff] [blame] | 18 | #include <net/xfrm.h> |
| 19 | |
Florian Westphal | f3075f48 | 2020-05-04 10:06:08 +0200 | [diff] [blame] | 20 | #if IS_ENABLED(CONFIG_IPV6) |
| 21 | #include <net/ip6_route.h> |
| 22 | #include <net/ipv6_stubs.h> |
| 23 | #endif |
| 24 | |
Florian Westphal | 1de7083 | 2019-03-29 21:16:29 +0100 | [diff] [blame] | 25 | #include "xfrm_inout.h" |
| 26 | |
Eric W. Biederman | 0c4b51f | 2015-09-15 20:04:18 -0500 | [diff] [blame] | 27 | static int xfrm_output2(struct net *net, struct sock *sk, struct sk_buff *skb); |
Florian Westphal | 0c620e9 | 2019-03-29 21:16:25 +0100 | [diff] [blame] | 28 | static int xfrm_inner_extract_output(struct xfrm_state *x, struct sk_buff *skb); |
Herbert Xu | c6581a4 | 2007-11-13 21:43:43 -0800 | [diff] [blame] | 29 | |
Steffen Klassert | 26b2072 | 2012-03-21 23:32:39 +0000 | [diff] [blame] | 30 | static int xfrm_skb_check_space(struct sk_buff *skb) |
Herbert Xu | 83815de | 2007-10-08 17:25:08 -0700 | [diff] [blame] | 31 | { |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 32 | struct dst_entry *dst = skb_dst(skb); |
Herbert Xu | 550ade8 | 2007-11-13 21:33:01 -0800 | [diff] [blame] | 33 | int nhead = dst->header_len + LL_RESERVED_SPACE(dst->dev) |
Herbert Xu | 83815de | 2007-10-08 17:25:08 -0700 | [diff] [blame] | 34 | - skb_headroom(skb); |
Johannes Berg | f5184d2 | 2008-05-12 20:48:31 -0700 | [diff] [blame] | 35 | int ntail = dst->dev->needed_tailroom - skb_tailroom(skb); |
Herbert Xu | 83815de | 2007-10-08 17:25:08 -0700 | [diff] [blame] | 36 | |
Herbert Xu | d01dbeb | 2008-09-30 02:03:19 -0700 | [diff] [blame] | 37 | if (nhead <= 0) { |
| 38 | if (ntail <= 0) |
| 39 | return 0; |
| 40 | nhead = 0; |
| 41 | } else if (ntail < 0) |
| 42 | ntail = 0; |
Herbert Xu | 83815de | 2007-10-08 17:25:08 -0700 | [diff] [blame] | 43 | |
Herbert Xu | d01dbeb | 2008-09-30 02:03:19 -0700 | [diff] [blame] | 44 | return pskb_expand_head(skb, nhead, ntail, GFP_ATOMIC); |
Herbert Xu | 83815de | 2007-10-08 17:25:08 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Ying Xue | 9449c3c | 2015-05-12 18:29:44 +0800 | [diff] [blame] | 47 | /* Children define the path of the packet through the |
| 48 | * Linux networking. Thus, destinations are stackable. |
| 49 | */ |
| 50 | |
| 51 | static struct dst_entry *skb_dst_pop(struct sk_buff *skb) |
| 52 | { |
David Miller | b92cf4a | 2017-11-28 15:40:22 -0500 | [diff] [blame] | 53 | struct dst_entry *child = dst_clone(xfrm_dst_child(skb_dst(skb))); |
Ying Xue | 9449c3c | 2015-05-12 18:29:44 +0800 | [diff] [blame] | 54 | |
| 55 | skb_dst_drop(skb); |
| 56 | return child; |
| 57 | } |
| 58 | |
Florian Westphal | 0c620e9 | 2019-03-29 21:16:25 +0100 | [diff] [blame] | 59 | /* Add encapsulation header. |
| 60 | * |
| 61 | * The IP header will be moved forward to make space for the encapsulation |
| 62 | * header. |
| 63 | */ |
| 64 | static int xfrm4_transport_output(struct xfrm_state *x, struct sk_buff *skb) |
| 65 | { |
Florian Westphal | 0c620e9 | 2019-03-29 21:16:25 +0100 | [diff] [blame] | 66 | struct iphdr *iph = ip_hdr(skb); |
| 67 | int ihl = iph->ihl * 4; |
| 68 | |
| 69 | skb_set_inner_transport_header(skb, skb_transport_offset(skb)); |
| 70 | |
| 71 | skb_set_network_header(skb, -x->props.header_len); |
| 72 | skb->mac_header = skb->network_header + |
| 73 | offsetof(struct iphdr, protocol); |
| 74 | skb->transport_header = skb->network_header + ihl; |
| 75 | __skb_pull(skb, ihl); |
| 76 | memmove(skb_network_header(skb), iph, ihl); |
| 77 | return 0; |
Florian Westphal | 0c620e9 | 2019-03-29 21:16:25 +0100 | [diff] [blame] | 78 | } |
| 79 | |
Florian Westphal | 9acf4d3 | 2021-06-11 12:50:10 +0200 | [diff] [blame^] | 80 | static int xfrm6_hdr_offset(struct xfrm_state *x, struct sk_buff *skb, u8 **prevhdr) |
| 81 | { |
| 82 | return x->type->hdr_offset(x, skb, prevhdr); |
| 83 | } |
| 84 | |
Florian Westphal | 0c620e9 | 2019-03-29 21:16:25 +0100 | [diff] [blame] | 85 | /* Add encapsulation header. |
| 86 | * |
| 87 | * The IP header and mutable extension headers will be moved forward to make |
| 88 | * space for the encapsulation header. |
| 89 | */ |
| 90 | static int xfrm6_transport_output(struct xfrm_state *x, struct sk_buff *skb) |
| 91 | { |
Florian Westphal | 4c145dc | 2019-03-29 21:16:31 +0100 | [diff] [blame] | 92 | #if IS_ENABLED(CONFIG_IPV6) |
Florian Westphal | 0c620e9 | 2019-03-29 21:16:25 +0100 | [diff] [blame] | 93 | struct ipv6hdr *iph; |
| 94 | u8 *prevhdr; |
| 95 | int hdr_len; |
| 96 | |
| 97 | iph = ipv6_hdr(skb); |
| 98 | skb_set_inner_transport_header(skb, skb_transport_offset(skb)); |
| 99 | |
Florian Westphal | 9acf4d3 | 2021-06-11 12:50:10 +0200 | [diff] [blame^] | 100 | hdr_len = xfrm6_hdr_offset(x, skb, &prevhdr); |
Florian Westphal | 0c620e9 | 2019-03-29 21:16:25 +0100 | [diff] [blame] | 101 | if (hdr_len < 0) |
| 102 | return hdr_len; |
| 103 | skb_set_mac_header(skb, |
| 104 | (prevhdr - x->props.header_len) - skb->data); |
| 105 | skb_set_network_header(skb, -x->props.header_len); |
| 106 | skb->transport_header = skb->network_header + hdr_len; |
| 107 | __skb_pull(skb, hdr_len); |
| 108 | memmove(ipv6_hdr(skb), iph, hdr_len); |
| 109 | return 0; |
| 110 | #else |
| 111 | WARN_ON_ONCE(1); |
Florian Westphal | 4c145dc | 2019-03-29 21:16:31 +0100 | [diff] [blame] | 112 | return -EAFNOSUPPORT; |
Florian Westphal | 0c620e9 | 2019-03-29 21:16:25 +0100 | [diff] [blame] | 113 | #endif |
| 114 | } |
| 115 | |
| 116 | /* Add route optimization header space. |
| 117 | * |
| 118 | * The IP header and mutable extension headers will be moved forward to make |
| 119 | * space for the route optimization header. |
| 120 | */ |
| 121 | static int xfrm6_ro_output(struct xfrm_state *x, struct sk_buff *skb) |
| 122 | { |
Florian Westphal | 4c145dc | 2019-03-29 21:16:31 +0100 | [diff] [blame] | 123 | #if IS_ENABLED(CONFIG_IPV6) |
Florian Westphal | 0c620e9 | 2019-03-29 21:16:25 +0100 | [diff] [blame] | 124 | struct ipv6hdr *iph; |
| 125 | u8 *prevhdr; |
| 126 | int hdr_len; |
| 127 | |
| 128 | iph = ipv6_hdr(skb); |
| 129 | |
Florian Westphal | 9acf4d3 | 2021-06-11 12:50:10 +0200 | [diff] [blame^] | 130 | hdr_len = xfrm6_hdr_offset(x, skb, &prevhdr); |
Florian Westphal | 0c620e9 | 2019-03-29 21:16:25 +0100 | [diff] [blame] | 131 | if (hdr_len < 0) |
| 132 | return hdr_len; |
| 133 | skb_set_mac_header(skb, |
| 134 | (prevhdr - x->props.header_len) - skb->data); |
| 135 | skb_set_network_header(skb, -x->props.header_len); |
| 136 | skb->transport_header = skb->network_header + hdr_len; |
| 137 | __skb_pull(skb, hdr_len); |
| 138 | memmove(ipv6_hdr(skb), iph, hdr_len); |
| 139 | |
| 140 | x->lastused = ktime_get_real_seconds(); |
| 141 | |
| 142 | return 0; |
| 143 | #else |
| 144 | WARN_ON_ONCE(1); |
Florian Westphal | 4c145dc | 2019-03-29 21:16:31 +0100 | [diff] [blame] | 145 | return -EAFNOSUPPORT; |
Florian Westphal | 0c620e9 | 2019-03-29 21:16:25 +0100 | [diff] [blame] | 146 | #endif |
| 147 | } |
| 148 | |
Florian Westphal | 1de7083 | 2019-03-29 21:16:29 +0100 | [diff] [blame] | 149 | /* Add encapsulation header. |
| 150 | * |
| 151 | * The top IP header will be constructed per draft-nikander-esp-beet-mode-06.txt. |
| 152 | */ |
| 153 | static int xfrm4_beet_encap_add(struct xfrm_state *x, struct sk_buff *skb) |
| 154 | { |
| 155 | struct ip_beet_phdr *ph; |
| 156 | struct iphdr *top_iph; |
| 157 | int hdrlen, optlen; |
| 158 | |
| 159 | hdrlen = 0; |
| 160 | optlen = XFRM_MODE_SKB_CB(skb)->optlen; |
| 161 | if (unlikely(optlen)) |
| 162 | hdrlen += IPV4_BEET_PHMAXLEN - (optlen & 4); |
| 163 | |
| 164 | skb_set_network_header(skb, -x->props.header_len - hdrlen + |
| 165 | (XFRM_MODE_SKB_CB(skb)->ihl - sizeof(*top_iph))); |
| 166 | if (x->sel.family != AF_INET6) |
| 167 | skb->network_header += IPV4_BEET_PHMAXLEN; |
| 168 | skb->mac_header = skb->network_header + |
| 169 | offsetof(struct iphdr, protocol); |
| 170 | skb->transport_header = skb->network_header + sizeof(*top_iph); |
| 171 | |
| 172 | xfrm4_beet_make_header(skb); |
| 173 | |
| 174 | ph = __skb_pull(skb, XFRM_MODE_SKB_CB(skb)->ihl - hdrlen); |
| 175 | |
| 176 | top_iph = ip_hdr(skb); |
| 177 | |
| 178 | if (unlikely(optlen)) { |
| 179 | if (WARN_ON(optlen < 0)) |
| 180 | return -EINVAL; |
| 181 | |
| 182 | ph->padlen = 4 - (optlen & 4); |
| 183 | ph->hdrlen = optlen / 8; |
| 184 | ph->nexthdr = top_iph->protocol; |
| 185 | if (ph->padlen) |
| 186 | memset(ph + 1, IPOPT_NOP, ph->padlen); |
| 187 | |
| 188 | top_iph->protocol = IPPROTO_BEETPH; |
| 189 | top_iph->ihl = sizeof(struct iphdr) / 4; |
| 190 | } |
| 191 | |
| 192 | top_iph->saddr = x->props.saddr.a4; |
| 193 | top_iph->daddr = x->id.daddr.a4; |
| 194 | |
| 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | /* Add encapsulation header. |
| 199 | * |
| 200 | * The top IP header will be constructed per RFC 2401. |
| 201 | */ |
| 202 | static int xfrm4_tunnel_encap_add(struct xfrm_state *x, struct sk_buff *skb) |
| 203 | { |
| 204 | struct dst_entry *dst = skb_dst(skb); |
| 205 | struct iphdr *top_iph; |
| 206 | int flags; |
| 207 | |
| 208 | skb_set_inner_network_header(skb, skb_network_offset(skb)); |
| 209 | skb_set_inner_transport_header(skb, skb_transport_offset(skb)); |
| 210 | |
| 211 | skb_set_network_header(skb, -x->props.header_len); |
| 212 | skb->mac_header = skb->network_header + |
| 213 | offsetof(struct iphdr, protocol); |
| 214 | skb->transport_header = skb->network_header + sizeof(*top_iph); |
| 215 | top_iph = ip_hdr(skb); |
| 216 | |
| 217 | top_iph->ihl = 5; |
| 218 | top_iph->version = 4; |
| 219 | |
| 220 | top_iph->protocol = xfrm_af2proto(skb_dst(skb)->ops->family); |
| 221 | |
| 222 | /* DS disclosing depends on XFRM_SA_XFLAG_DONT_ENCAP_DSCP */ |
| 223 | if (x->props.extra_flags & XFRM_SA_XFLAG_DONT_ENCAP_DSCP) |
| 224 | top_iph->tos = 0; |
| 225 | else |
| 226 | top_iph->tos = XFRM_MODE_SKB_CB(skb)->tos; |
| 227 | top_iph->tos = INET_ECN_encapsulate(top_iph->tos, |
| 228 | XFRM_MODE_SKB_CB(skb)->tos); |
| 229 | |
| 230 | flags = x->props.flags; |
| 231 | if (flags & XFRM_STATE_NOECN) |
| 232 | IP_ECN_clear(top_iph); |
| 233 | |
| 234 | top_iph->frag_off = (flags & XFRM_STATE_NOPMTUDISC) ? |
| 235 | 0 : (XFRM_MODE_SKB_CB(skb)->frag_off & htons(IP_DF)); |
| 236 | |
| 237 | top_iph->ttl = ip4_dst_hoplimit(xfrm_dst_child(dst)); |
| 238 | |
| 239 | top_iph->saddr = x->props.saddr.a4; |
| 240 | top_iph->daddr = x->id.daddr.a4; |
| 241 | ip_select_ident(dev_net(dst->dev), skb, NULL); |
| 242 | |
| 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | #if IS_ENABLED(CONFIG_IPV6) |
| 247 | static int xfrm6_tunnel_encap_add(struct xfrm_state *x, struct sk_buff *skb) |
| 248 | { |
| 249 | struct dst_entry *dst = skb_dst(skb); |
| 250 | struct ipv6hdr *top_iph; |
| 251 | int dsfield; |
| 252 | |
| 253 | skb_set_inner_network_header(skb, skb_network_offset(skb)); |
| 254 | skb_set_inner_transport_header(skb, skb_transport_offset(skb)); |
| 255 | |
| 256 | skb_set_network_header(skb, -x->props.header_len); |
| 257 | skb->mac_header = skb->network_header + |
| 258 | offsetof(struct ipv6hdr, nexthdr); |
| 259 | skb->transport_header = skb->network_header + sizeof(*top_iph); |
| 260 | top_iph = ipv6_hdr(skb); |
| 261 | |
| 262 | top_iph->version = 6; |
| 263 | |
| 264 | memcpy(top_iph->flow_lbl, XFRM_MODE_SKB_CB(skb)->flow_lbl, |
| 265 | sizeof(top_iph->flow_lbl)); |
| 266 | top_iph->nexthdr = xfrm_af2proto(skb_dst(skb)->ops->family); |
| 267 | |
| 268 | if (x->props.extra_flags & XFRM_SA_XFLAG_DONT_ENCAP_DSCP) |
| 269 | dsfield = 0; |
| 270 | else |
| 271 | dsfield = XFRM_MODE_SKB_CB(skb)->tos; |
| 272 | dsfield = INET_ECN_encapsulate(dsfield, XFRM_MODE_SKB_CB(skb)->tos); |
| 273 | if (x->props.flags & XFRM_STATE_NOECN) |
| 274 | dsfield &= ~INET_ECN_MASK; |
| 275 | ipv6_change_dsfield(top_iph, 0, dsfield); |
| 276 | top_iph->hop_limit = ip6_dst_hoplimit(xfrm_dst_child(dst)); |
| 277 | top_iph->saddr = *(struct in6_addr *)&x->props.saddr; |
| 278 | top_iph->daddr = *(struct in6_addr *)&x->id.daddr; |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | static int xfrm6_beet_encap_add(struct xfrm_state *x, struct sk_buff *skb) |
| 283 | { |
| 284 | struct ipv6hdr *top_iph; |
| 285 | struct ip_beet_phdr *ph; |
| 286 | int optlen, hdr_len; |
| 287 | |
| 288 | hdr_len = 0; |
| 289 | optlen = XFRM_MODE_SKB_CB(skb)->optlen; |
| 290 | if (unlikely(optlen)) |
| 291 | hdr_len += IPV4_BEET_PHMAXLEN - (optlen & 4); |
| 292 | |
| 293 | skb_set_network_header(skb, -x->props.header_len - hdr_len); |
| 294 | if (x->sel.family != AF_INET6) |
| 295 | skb->network_header += IPV4_BEET_PHMAXLEN; |
| 296 | skb->mac_header = skb->network_header + |
| 297 | offsetof(struct ipv6hdr, nexthdr); |
| 298 | skb->transport_header = skb->network_header + sizeof(*top_iph); |
| 299 | ph = __skb_pull(skb, XFRM_MODE_SKB_CB(skb)->ihl - hdr_len); |
| 300 | |
| 301 | xfrm6_beet_make_header(skb); |
| 302 | |
| 303 | top_iph = ipv6_hdr(skb); |
| 304 | if (unlikely(optlen)) { |
| 305 | if (WARN_ON(optlen < 0)) |
| 306 | return -EINVAL; |
| 307 | |
| 308 | ph->padlen = 4 - (optlen & 4); |
| 309 | ph->hdrlen = optlen / 8; |
| 310 | ph->nexthdr = top_iph->nexthdr; |
| 311 | if (ph->padlen) |
| 312 | memset(ph + 1, IPOPT_NOP, ph->padlen); |
| 313 | |
| 314 | top_iph->nexthdr = IPPROTO_BEETPH; |
| 315 | } |
| 316 | |
| 317 | top_iph->saddr = *(struct in6_addr *)&x->props.saddr; |
| 318 | top_iph->daddr = *(struct in6_addr *)&x->id.daddr; |
| 319 | return 0; |
| 320 | } |
| 321 | #endif |
| 322 | |
| 323 | /* Add encapsulation header. |
| 324 | * |
| 325 | * On exit, the transport header will be set to the start of the |
| 326 | * encapsulation header to be filled in by x->type->output and the mac |
| 327 | * header will be set to the nextheader (protocol for IPv4) field of the |
| 328 | * extension header directly preceding the encapsulation header, or in |
| 329 | * its absence, that of the top IP header. |
| 330 | * The value of the network header will always point to the top IP header |
| 331 | * while skb->data will point to the payload. |
| 332 | */ |
Florian Westphal | 0c620e9 | 2019-03-29 21:16:25 +0100 | [diff] [blame] | 333 | static int xfrm4_prepare_output(struct xfrm_state *x, struct sk_buff *skb) |
| 334 | { |
| 335 | int err; |
| 336 | |
| 337 | err = xfrm_inner_extract_output(x, skb); |
| 338 | if (err) |
| 339 | return err; |
| 340 | |
| 341 | IPCB(skb)->flags |= IPSKB_XFRM_TUNNEL_SIZE; |
| 342 | skb->protocol = htons(ETH_P_IP); |
| 343 | |
Florian Westphal | c9500d7 | 2019-03-29 21:16:32 +0100 | [diff] [blame] | 344 | switch (x->outer_mode.encap) { |
Florian Westphal | 1de7083 | 2019-03-29 21:16:29 +0100 | [diff] [blame] | 345 | case XFRM_MODE_BEET: |
| 346 | return xfrm4_beet_encap_add(x, skb); |
| 347 | case XFRM_MODE_TUNNEL: |
| 348 | return xfrm4_tunnel_encap_add(x, skb); |
| 349 | } |
| 350 | |
| 351 | WARN_ON_ONCE(1); |
| 352 | return -EOPNOTSUPP; |
Florian Westphal | 0c620e9 | 2019-03-29 21:16:25 +0100 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | static int xfrm6_prepare_output(struct xfrm_state *x, struct sk_buff *skb) |
| 356 | { |
| 357 | #if IS_ENABLED(CONFIG_IPV6) |
| 358 | int err; |
| 359 | |
| 360 | err = xfrm_inner_extract_output(x, skb); |
| 361 | if (err) |
| 362 | return err; |
| 363 | |
| 364 | skb->ignore_df = 1; |
| 365 | skb->protocol = htons(ETH_P_IPV6); |
| 366 | |
Florian Westphal | c9500d7 | 2019-03-29 21:16:32 +0100 | [diff] [blame] | 367 | switch (x->outer_mode.encap) { |
Florian Westphal | 1de7083 | 2019-03-29 21:16:29 +0100 | [diff] [blame] | 368 | case XFRM_MODE_BEET: |
| 369 | return xfrm6_beet_encap_add(x, skb); |
| 370 | case XFRM_MODE_TUNNEL: |
| 371 | return xfrm6_tunnel_encap_add(x, skb); |
| 372 | default: |
| 373 | WARN_ON_ONCE(1); |
| 374 | return -EOPNOTSUPP; |
| 375 | } |
Florian Westphal | 0c620e9 | 2019-03-29 21:16:25 +0100 | [diff] [blame] | 376 | #endif |
Florian Westphal | 1de7083 | 2019-03-29 21:16:29 +0100 | [diff] [blame] | 377 | WARN_ON_ONCE(1); |
| 378 | return -EAFNOSUPPORT; |
Florian Westphal | 0c620e9 | 2019-03-29 21:16:25 +0100 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | static int xfrm_outer_mode_output(struct xfrm_state *x, struct sk_buff *skb) |
| 382 | { |
Florian Westphal | c9500d7 | 2019-03-29 21:16:32 +0100 | [diff] [blame] | 383 | switch (x->outer_mode.encap) { |
Florian Westphal | 0c620e9 | 2019-03-29 21:16:25 +0100 | [diff] [blame] | 384 | case XFRM_MODE_BEET: |
| 385 | case XFRM_MODE_TUNNEL: |
Florian Westphal | c9500d7 | 2019-03-29 21:16:32 +0100 | [diff] [blame] | 386 | if (x->outer_mode.family == AF_INET) |
Florian Westphal | 0c620e9 | 2019-03-29 21:16:25 +0100 | [diff] [blame] | 387 | return xfrm4_prepare_output(x, skb); |
Florian Westphal | c9500d7 | 2019-03-29 21:16:32 +0100 | [diff] [blame] | 388 | if (x->outer_mode.family == AF_INET6) |
Florian Westphal | 0c620e9 | 2019-03-29 21:16:25 +0100 | [diff] [blame] | 389 | return xfrm6_prepare_output(x, skb); |
| 390 | break; |
| 391 | case XFRM_MODE_TRANSPORT: |
Florian Westphal | c9500d7 | 2019-03-29 21:16:32 +0100 | [diff] [blame] | 392 | if (x->outer_mode.family == AF_INET) |
Florian Westphal | 0c620e9 | 2019-03-29 21:16:25 +0100 | [diff] [blame] | 393 | return xfrm4_transport_output(x, skb); |
Florian Westphal | c9500d7 | 2019-03-29 21:16:32 +0100 | [diff] [blame] | 394 | if (x->outer_mode.family == AF_INET6) |
Florian Westphal | 0c620e9 | 2019-03-29 21:16:25 +0100 | [diff] [blame] | 395 | return xfrm6_transport_output(x, skb); |
| 396 | break; |
| 397 | case XFRM_MODE_ROUTEOPTIMIZATION: |
Florian Westphal | c9500d7 | 2019-03-29 21:16:32 +0100 | [diff] [blame] | 398 | if (x->outer_mode.family == AF_INET6) |
Florian Westphal | 0c620e9 | 2019-03-29 21:16:25 +0100 | [diff] [blame] | 399 | return xfrm6_ro_output(x, skb); |
| 400 | WARN_ON_ONCE(1); |
| 401 | break; |
| 402 | default: |
| 403 | WARN_ON_ONCE(1); |
| 404 | break; |
| 405 | } |
| 406 | |
| 407 | return -EOPNOTSUPP; |
| 408 | } |
| 409 | |
| 410 | #if IS_ENABLED(CONFIG_NET_PKTGEN) |
| 411 | int pktgen_xfrm_outer_mode_output(struct xfrm_state *x, struct sk_buff *skb) |
| 412 | { |
| 413 | return xfrm_outer_mode_output(x, skb); |
| 414 | } |
| 415 | EXPORT_SYMBOL_GPL(pktgen_xfrm_outer_mode_output); |
| 416 | #endif |
| 417 | |
Herbert Xu | c6581a4 | 2007-11-13 21:43:43 -0800 | [diff] [blame] | 418 | static int xfrm_output_one(struct sk_buff *skb, int err) |
Herbert Xu | 406ef77 | 2007-10-08 17:16:30 -0700 | [diff] [blame] | 419 | { |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 420 | struct dst_entry *dst = skb_dst(skb); |
Herbert Xu | 406ef77 | 2007-10-08 17:16:30 -0700 | [diff] [blame] | 421 | struct xfrm_state *x = dst->xfrm; |
Alexey Dobriyan | a6483b7 | 2008-11-25 17:38:20 -0800 | [diff] [blame] | 422 | struct net *net = xs_net(x); |
Herbert Xu | 406ef77 | 2007-10-08 17:16:30 -0700 | [diff] [blame] | 423 | |
Herbert Xu | c6581a4 | 2007-11-13 21:43:43 -0800 | [diff] [blame] | 424 | if (err <= 0) |
| 425 | goto resume; |
Herbert Xu | 406ef77 | 2007-10-08 17:16:30 -0700 | [diff] [blame] | 426 | |
| 427 | do { |
Steffen Klassert | 26b2072 | 2012-03-21 23:32:39 +0000 | [diff] [blame] | 428 | err = xfrm_skb_check_space(skb); |
Masahide NAKAMURA | b15c4bc | 2007-12-24 16:00:09 -0800 | [diff] [blame] | 429 | if (err) { |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 430 | XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR); |
Herbert Xu | 910ef70 | 2007-12-18 22:14:25 -0800 | [diff] [blame] | 431 | goto error_nolock; |
Masahide NAKAMURA | b15c4bc | 2007-12-24 16:00:09 -0800 | [diff] [blame] | 432 | } |
Herbert Xu | 910ef70 | 2007-12-18 22:14:25 -0800 | [diff] [blame] | 433 | |
Steffen Klassert | 9b42c1f | 2018-06-12 12:44:26 +0200 | [diff] [blame] | 434 | skb->mark = xfrm_smark_get(skb->mark, x); |
Lorenzo Colitti | 077fbac | 2017-08-11 02:11:33 +0900 | [diff] [blame] | 435 | |
Florian Westphal | 0c620e9 | 2019-03-29 21:16:25 +0100 | [diff] [blame] | 436 | err = xfrm_outer_mode_output(x, skb); |
Masahide NAKAMURA | b15c4bc | 2007-12-24 16:00:09 -0800 | [diff] [blame] | 437 | if (err) { |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 438 | XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEMODEERROR); |
Herbert Xu | 910ef70 | 2007-12-18 22:14:25 -0800 | [diff] [blame] | 439 | goto error_nolock; |
Masahide NAKAMURA | b15c4bc | 2007-12-24 16:00:09 -0800 | [diff] [blame] | 440 | } |
Herbert Xu | a2deb6d2 | 2007-11-13 21:39:38 -0800 | [diff] [blame] | 441 | |
Herbert Xu | 406ef77 | 2007-10-08 17:16:30 -0700 | [diff] [blame] | 442 | spin_lock_bh(&x->lock); |
Li RongQing | bb65a9c | 2012-12-28 16:06:28 +0800 | [diff] [blame] | 443 | |
| 444 | if (unlikely(x->km.state != XFRM_STATE_VALID)) { |
| 445 | XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEINVALID); |
Timo Teräs | 497574c | 2013-05-22 01:40:47 +0000 | [diff] [blame] | 446 | err = -EINVAL; |
Li RongQing | fa8599d | 2013-02-01 13:17:16 +0800 | [diff] [blame] | 447 | goto error; |
Li RongQing | bb65a9c | 2012-12-28 16:06:28 +0800 | [diff] [blame] | 448 | } |
| 449 | |
Herbert Xu | 910ef70 | 2007-12-18 22:14:25 -0800 | [diff] [blame] | 450 | err = xfrm_state_check_expire(x); |
Masahide NAKAMURA | b15c4bc | 2007-12-24 16:00:09 -0800 | [diff] [blame] | 451 | if (err) { |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 452 | XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEEXPIRED); |
Herbert Xu | 406ef77 | 2007-10-08 17:16:30 -0700 | [diff] [blame] | 453 | goto error; |
Masahide NAKAMURA | b15c4bc | 2007-12-24 16:00:09 -0800 | [diff] [blame] | 454 | } |
Herbert Xu | 406ef77 | 2007-10-08 17:16:30 -0700 | [diff] [blame] | 455 | |
Steffen Klassert | 9fdc488 | 2011-03-08 00:08:32 +0000 | [diff] [blame] | 456 | err = x->repl->overflow(x, skb); |
| 457 | if (err) { |
| 458 | XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATESEQERROR); |
| 459 | goto error; |
Herbert Xu | 436a0a4 | 2007-10-08 17:25:53 -0700 | [diff] [blame] | 460 | } |
| 461 | |
Herbert Xu | 406ef77 | 2007-10-08 17:16:30 -0700 | [diff] [blame] | 462 | x->curlft.bytes += skb->len; |
| 463 | x->curlft.packets++; |
| 464 | |
Herbert Xu | 406ef77 | 2007-10-08 17:16:30 -0700 | [diff] [blame] | 465 | spin_unlock_bh(&x->lock); |
| 466 | |
Steffen Klassert | 3bc0732 | 2011-03-15 21:08:28 +0000 | [diff] [blame] | 467 | skb_dst_force(skb); |
Steffen Klassert | 9e14379 | 2018-09-11 10:31:15 +0200 | [diff] [blame] | 468 | if (!skb_dst(skb)) { |
| 469 | XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR); |
Wei Yongjun | 533555e | 2018-10-27 06:12:06 +0000 | [diff] [blame] | 470 | err = -EHOSTUNREACH; |
Steffen Klassert | 9e14379 | 2018-09-11 10:31:15 +0200 | [diff] [blame] | 471 | goto error_nolock; |
| 472 | } |
Steffen Klassert | 3bc0732 | 2011-03-15 21:08:28 +0000 | [diff] [blame] | 473 | |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 474 | if (xfrm_offload(skb)) { |
| 475 | x->type_offload->encap(x, skb); |
| 476 | } else { |
Steffen Klassert | 73b9fc4 | 2017-10-30 10:04:04 +0100 | [diff] [blame] | 477 | /* Inner headers are invalid now. */ |
| 478 | skb->encapsulation = 0; |
| 479 | |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 480 | err = x->type->output(x, skb); |
| 481 | if (err == -EINPROGRESS) |
| 482 | goto out; |
| 483 | } |
Herbert Xu | c6581a4 | 2007-11-13 21:43:43 -0800 | [diff] [blame] | 484 | |
| 485 | resume: |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 486 | if (err) { |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 487 | XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEPROTOERROR); |
Herbert Xu | b7c6538c | 2007-10-09 13:33:35 -0700 | [diff] [blame] | 488 | goto error_nolock; |
Masahide NAKAMURA | 0aa6477 | 2007-12-20 20:43:36 -0800 | [diff] [blame] | 489 | } |
Herbert Xu | b7c6538c | 2007-10-09 13:33:35 -0700 | [diff] [blame] | 490 | |
Steffen Klassert | 8764ab2 | 2010-06-04 01:57:38 +0000 | [diff] [blame] | 491 | dst = skb_dst_pop(skb); |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 492 | if (!dst) { |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 493 | XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR); |
Herbert Xu | 406ef77 | 2007-10-08 17:16:30 -0700 | [diff] [blame] | 494 | err = -EHOSTUNREACH; |
| 495 | goto error_nolock; |
| 496 | } |
Steffen Klassert | e433430 | 2011-03-15 21:09:32 +0000 | [diff] [blame] | 497 | skb_dst_set(skb, dst); |
Herbert Xu | 406ef77 | 2007-10-08 17:16:30 -0700 | [diff] [blame] | 498 | x = dst->xfrm; |
Florian Westphal | c9500d7 | 2019-03-29 21:16:32 +0100 | [diff] [blame] | 499 | } while (x && !(x->outer_mode.flags & XFRM_MODE_FLAG_TUNNEL)); |
Herbert Xu | 406ef77 | 2007-10-08 17:16:30 -0700 | [diff] [blame] | 500 | |
Jean Sacren | ebd4687 | 2013-06-01 16:23:15 +0000 | [diff] [blame] | 501 | return 0; |
Herbert Xu | 406ef77 | 2007-10-08 17:16:30 -0700 | [diff] [blame] | 502 | |
Herbert Xu | 406ef77 | 2007-10-08 17:16:30 -0700 | [diff] [blame] | 503 | error: |
| 504 | spin_unlock_bh(&x->lock); |
Herbert Xu | 862b82c | 2007-11-13 21:43:11 -0800 | [diff] [blame] | 505 | error_nolock: |
| 506 | kfree_skb(skb); |
Jean Sacren | ebd4687 | 2013-06-01 16:23:15 +0000 | [diff] [blame] | 507 | out: |
| 508 | return err; |
Herbert Xu | 862b82c | 2007-11-13 21:43:11 -0800 | [diff] [blame] | 509 | } |
| 510 | |
Evan Nimmo | 9ab1265 | 2021-03-02 08:00:04 +1300 | [diff] [blame] | 511 | int xfrm_output_resume(struct sock *sk, struct sk_buff *skb, int err) |
Herbert Xu | 862b82c | 2007-11-13 21:43:11 -0800 | [diff] [blame] | 512 | { |
Eric W. Biederman | 29a26a5 | 2015-09-15 20:04:16 -0500 | [diff] [blame] | 513 | struct net *net = xs_net(skb_dst(skb)->xfrm); |
Eric W. Biederman | be10de0 | 2015-09-17 17:21:31 -0500 | [diff] [blame] | 514 | |
Herbert Xu | c6581a4 | 2007-11-13 21:43:43 -0800 | [diff] [blame] | 515 | while (likely((err = xfrm_output_one(skb, err)) == 0)) { |
Florian Westphal | 895b5c9 | 2019-09-29 20:54:03 +0200 | [diff] [blame] | 516 | nf_reset_ct(skb); |
Herbert Xu | 862b82c | 2007-11-13 21:43:11 -0800 | [diff] [blame] | 517 | |
Evan Nimmo | 9ab1265 | 2021-03-02 08:00:04 +1300 | [diff] [blame] | 518 | err = skb_dst(skb)->ops->local_out(net, sk, skb); |
Herbert Xu | 862b82c | 2007-11-13 21:43:11 -0800 | [diff] [blame] | 519 | if (unlikely(err != 1)) |
Herbert Xu | c6581a4 | 2007-11-13 21:43:43 -0800 | [diff] [blame] | 520 | goto out; |
Herbert Xu | 862b82c | 2007-11-13 21:43:11 -0800 | [diff] [blame] | 521 | |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 522 | if (!skb_dst(skb)->xfrm) |
Evan Nimmo | 9ab1265 | 2021-03-02 08:00:04 +1300 | [diff] [blame] | 523 | return dst_output(net, sk, skb); |
Herbert Xu | 862b82c | 2007-11-13 21:43:11 -0800 | [diff] [blame] | 524 | |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 525 | err = nf_hook(skb_dst(skb)->ops->family, |
Evan Nimmo | 9ab1265 | 2021-03-02 08:00:04 +1300 | [diff] [blame] | 526 | NF_INET_POST_ROUTING, net, sk, skb, |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 527 | NULL, skb_dst(skb)->dev, xfrm_output2); |
Herbert Xu | 862b82c | 2007-11-13 21:43:11 -0800 | [diff] [blame] | 528 | if (unlikely(err != 1)) |
Herbert Xu | c6581a4 | 2007-11-13 21:43:43 -0800 | [diff] [blame] | 529 | goto out; |
Herbert Xu | 862b82c | 2007-11-13 21:43:11 -0800 | [diff] [blame] | 530 | } |
| 531 | |
Herbert Xu | c6581a4 | 2007-11-13 21:43:43 -0800 | [diff] [blame] | 532 | if (err == -EINPROGRESS) |
| 533 | err = 0; |
| 534 | |
| 535 | out: |
Herbert Xu | 862b82c | 2007-11-13 21:43:11 -0800 | [diff] [blame] | 536 | return err; |
| 537 | } |
Herbert Xu | c6581a4 | 2007-11-13 21:43:43 -0800 | [diff] [blame] | 538 | EXPORT_SYMBOL_GPL(xfrm_output_resume); |
Herbert Xu | 862b82c | 2007-11-13 21:43:11 -0800 | [diff] [blame] | 539 | |
Eric W. Biederman | 0c4b51f | 2015-09-15 20:04:18 -0500 | [diff] [blame] | 540 | static int xfrm_output2(struct net *net, struct sock *sk, struct sk_buff *skb) |
Herbert Xu | c6581a4 | 2007-11-13 21:43:43 -0800 | [diff] [blame] | 541 | { |
Evan Nimmo | 9ab1265 | 2021-03-02 08:00:04 +1300 | [diff] [blame] | 542 | return xfrm_output_resume(sk, skb, 1); |
Herbert Xu | c6581a4 | 2007-11-13 21:43:43 -0800 | [diff] [blame] | 543 | } |
| 544 | |
Eric W. Biederman | 0c4b51f | 2015-09-15 20:04:18 -0500 | [diff] [blame] | 545 | static int xfrm_output_gso(struct net *net, struct sock *sk, struct sk_buff *skb) |
Herbert Xu | 862b82c | 2007-11-13 21:43:11 -0800 | [diff] [blame] | 546 | { |
Jason A. Donenfeld | c3b18e0 | 2020-01-13 18:42:28 -0500 | [diff] [blame] | 547 | struct sk_buff *segs, *nskb; |
Herbert Xu | 862b82c | 2007-11-13 21:43:11 -0800 | [diff] [blame] | 548 | |
Cambda Zhu | a08e7fd | 2020-03-26 15:33:14 +0800 | [diff] [blame] | 549 | BUILD_BUG_ON(sizeof(*IPCB(skb)) > SKB_GSO_CB_OFFSET); |
| 550 | BUILD_BUG_ON(sizeof(*IP6CB(skb)) > SKB_GSO_CB_OFFSET); |
Herbert Xu | 862b82c | 2007-11-13 21:43:11 -0800 | [diff] [blame] | 551 | segs = skb_gso_segment(skb, 0); |
| 552 | kfree_skb(skb); |
Hirofumi Nakagawa | 801678c | 2008-04-29 01:03:09 -0700 | [diff] [blame] | 553 | if (IS_ERR(segs)) |
Herbert Xu | 862b82c | 2007-11-13 21:43:11 -0800 | [diff] [blame] | 554 | return PTR_ERR(segs); |
Florian Westphal | 330966e | 2014-10-20 13:49:17 +0200 | [diff] [blame] | 555 | if (segs == NULL) |
| 556 | return -EINVAL; |
Herbert Xu | 862b82c | 2007-11-13 21:43:11 -0800 | [diff] [blame] | 557 | |
Jason A. Donenfeld | c3b18e0 | 2020-01-13 18:42:28 -0500 | [diff] [blame] | 558 | skb_list_walk_safe(segs, segs, nskb) { |
Herbert Xu | 862b82c | 2007-11-13 21:43:11 -0800 | [diff] [blame] | 559 | int err; |
| 560 | |
David S. Miller | a8305bf | 2018-07-29 20:42:53 -0700 | [diff] [blame] | 561 | skb_mark_not_on_list(segs); |
Eric W. Biederman | 0c4b51f | 2015-09-15 20:04:18 -0500 | [diff] [blame] | 562 | err = xfrm_output2(net, sk, segs); |
Herbert Xu | 862b82c | 2007-11-13 21:43:11 -0800 | [diff] [blame] | 563 | |
| 564 | if (unlikely(err)) { |
Florian Westphal | 46cfd72 | 2014-09-10 01:08:46 +0200 | [diff] [blame] | 565 | kfree_skb_list(nskb); |
Herbert Xu | 862b82c | 2007-11-13 21:43:11 -0800 | [diff] [blame] | 566 | return err; |
| 567 | } |
Jason A. Donenfeld | c3b18e0 | 2020-01-13 18:42:28 -0500 | [diff] [blame] | 568 | } |
Herbert Xu | 862b82c | 2007-11-13 21:43:11 -0800 | [diff] [blame] | 569 | |
| 570 | return 0; |
Herbert Xu | 406ef77 | 2007-10-08 17:16:30 -0700 | [diff] [blame] | 571 | } |
Herbert Xu | c6581a4 | 2007-11-13 21:43:43 -0800 | [diff] [blame] | 572 | |
David Miller | 7026b1d | 2015-04-05 22:19:04 -0400 | [diff] [blame] | 573 | int xfrm_output(struct sock *sk, struct sk_buff *skb) |
Herbert Xu | c6581a4 | 2007-11-13 21:43:43 -0800 | [diff] [blame] | 574 | { |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 575 | struct net *net = dev_net(skb_dst(skb)->dev); |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 576 | struct xfrm_state *x = skb_dst(skb)->xfrm; |
Herbert Xu | c6581a4 | 2007-11-13 21:43:43 -0800 | [diff] [blame] | 577 | int err; |
| 578 | |
Florian Westphal | 2ab6096 | 2020-05-04 10:06:09 +0200 | [diff] [blame] | 579 | switch (x->outer_mode.family) { |
| 580 | case AF_INET: |
| 581 | memset(IPCB(skb), 0, sizeof(*IPCB(skb))); |
Florian Westphal | 2ab6096 | 2020-05-04 10:06:09 +0200 | [diff] [blame] | 582 | IPCB(skb)->flags |= IPSKB_XFRM_TRANSFORMED; |
Florian Westphal | 2ab6096 | 2020-05-04 10:06:09 +0200 | [diff] [blame] | 583 | break; |
| 584 | case AF_INET6: |
| 585 | memset(IP6CB(skb), 0, sizeof(*IP6CB(skb))); |
| 586 | |
Florian Westphal | 2ab6096 | 2020-05-04 10:06:09 +0200 | [diff] [blame] | 587 | IP6CB(skb)->flags |= IP6SKB_XFRM_TRANSFORMED; |
Florian Westphal | 2ab6096 | 2020-05-04 10:06:09 +0200 | [diff] [blame] | 588 | break; |
| 589 | } |
| 590 | |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 591 | secpath_reset(skb); |
| 592 | |
| 593 | if (xfrm_dev_offload_ok(skb, x)) { |
| 594 | struct sec_path *sp; |
| 595 | |
Florian Westphal | a84e3f5 | 2018-12-18 17:15:26 +0100 | [diff] [blame] | 596 | sp = secpath_set(skb); |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 597 | if (!sp) { |
| 598 | XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR); |
| 599 | kfree_skb(skb); |
| 600 | return -ENOMEM; |
| 601 | } |
Ilan Tayari | f1bd7d6 | 2017-04-14 10:07:39 +0200 | [diff] [blame] | 602 | skb->encapsulation = 1; |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 603 | |
| 604 | sp->olen++; |
Florian Westphal | a84e3f5 | 2018-12-18 17:15:26 +0100 | [diff] [blame] | 605 | sp->xvec[sp->len++] = x; |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 606 | xfrm_state_hold(x); |
| 607 | |
| 608 | if (skb_is_gso(skb)) { |
Xin Long | a204aef | 2020-04-20 21:51:09 +0800 | [diff] [blame] | 609 | if (skb->inner_protocol) |
| 610 | return xfrm_output_gso(net, sk, skb); |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 611 | |
Xin Long | a204aef | 2020-04-20 21:51:09 +0800 | [diff] [blame] | 612 | skb_shinfo(skb)->gso_type |= SKB_GSO_ESP; |
| 613 | goto out; |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | if (x->xso.dev && x->xso.dev->features & NETIF_F_HW_ESP_TX_CSUM) |
| 617 | goto out; |
Xin Long | a204aef | 2020-04-20 21:51:09 +0800 | [diff] [blame] | 618 | } else { |
| 619 | if (skb_is_gso(skb)) |
| 620 | return xfrm_output_gso(net, sk, skb); |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 621 | } |
| 622 | |
Herbert Xu | c6581a4 | 2007-11-13 21:43:43 -0800 | [diff] [blame] | 623 | if (skb->ip_summed == CHECKSUM_PARTIAL) { |
| 624 | err = skb_checksum_help(skb); |
| 625 | if (err) { |
Alexey Dobriyan | 59c9940 | 2008-11-25 17:59:52 -0800 | [diff] [blame] | 626 | XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR); |
Herbert Xu | c6581a4 | 2007-11-13 21:43:43 -0800 | [diff] [blame] | 627 | kfree_skb(skb); |
| 628 | return err; |
| 629 | } |
| 630 | } |
| 631 | |
Steffen Klassert | d77e38e | 2017-04-14 10:06:10 +0200 | [diff] [blame] | 632 | out: |
Eric W. Biederman | 0c4b51f | 2015-09-15 20:04:18 -0500 | [diff] [blame] | 633 | return xfrm_output2(net, sk, skb); |
Herbert Xu | c6581a4 | 2007-11-13 21:43:43 -0800 | [diff] [blame] | 634 | } |
Fabian Frederick | fc68086 | 2014-05-12 19:54:34 +0200 | [diff] [blame] | 635 | EXPORT_SYMBOL_GPL(xfrm_output); |
Kazunori MIYAZAWA | df9dcb4 | 2008-03-24 14:51:51 -0700 | [diff] [blame] | 636 | |
Florian Westphal | 6d64be3 | 2020-05-04 10:06:03 +0200 | [diff] [blame] | 637 | static int xfrm4_tunnel_check_size(struct sk_buff *skb) |
| 638 | { |
| 639 | int mtu, ret = 0; |
| 640 | |
| 641 | if (IPCB(skb)->flags & IPSKB_XFRM_TUNNEL_SIZE) |
| 642 | goto out; |
| 643 | |
| 644 | if (!(ip_hdr(skb)->frag_off & htons(IP_DF)) || skb->ignore_df) |
| 645 | goto out; |
| 646 | |
| 647 | mtu = dst_mtu(skb_dst(skb)); |
| 648 | if ((!skb_is_gso(skb) && skb->len > mtu) || |
| 649 | (skb_is_gso(skb) && |
| 650 | !skb_gso_validate_network_len(skb, ip_skb_dst_mtu(skb->sk, skb)))) { |
| 651 | skb->protocol = htons(ETH_P_IP); |
| 652 | |
| 653 | if (skb->sk) |
| 654 | xfrm_local_error(skb, mtu); |
| 655 | else |
| 656 | icmp_send(skb, ICMP_DEST_UNREACH, |
| 657 | ICMP_FRAG_NEEDED, htonl(mtu)); |
| 658 | ret = -EMSGSIZE; |
| 659 | } |
| 660 | out: |
| 661 | return ret; |
| 662 | } |
| 663 | |
| 664 | static int xfrm4_extract_output(struct xfrm_state *x, struct sk_buff *skb) |
| 665 | { |
| 666 | int err; |
| 667 | |
Xin Long | 68dc022 | 2021-03-19 18:27:58 +0800 | [diff] [blame] | 668 | if (x->outer_mode.encap == XFRM_MODE_BEET && |
| 669 | ip_is_fragment(ip_hdr(skb))) { |
| 670 | net_warn_ratelimited("BEET mode doesn't support inner IPv4 fragments\n"); |
| 671 | return -EAFNOSUPPORT; |
| 672 | } |
| 673 | |
Florian Westphal | 6d64be3 | 2020-05-04 10:06:03 +0200 | [diff] [blame] | 674 | err = xfrm4_tunnel_check_size(skb); |
| 675 | if (err) |
| 676 | return err; |
| 677 | |
| 678 | XFRM_MODE_SKB_CB(skb)->protocol = ip_hdr(skb)->protocol; |
| 679 | |
| 680 | xfrm4_extract_header(skb); |
| 681 | return 0; |
| 682 | } |
| 683 | |
Florian Westphal | f3075f48 | 2020-05-04 10:06:08 +0200 | [diff] [blame] | 684 | #if IS_ENABLED(CONFIG_IPV6) |
| 685 | static int xfrm6_tunnel_check_size(struct sk_buff *skb) |
| 686 | { |
| 687 | int mtu, ret = 0; |
| 688 | struct dst_entry *dst = skb_dst(skb); |
| 689 | |
| 690 | if (skb->ignore_df) |
| 691 | goto out; |
| 692 | |
| 693 | mtu = dst_mtu(dst); |
| 694 | if (mtu < IPV6_MIN_MTU) |
| 695 | mtu = IPV6_MIN_MTU; |
| 696 | |
| 697 | if ((!skb_is_gso(skb) && skb->len > mtu) || |
| 698 | (skb_is_gso(skb) && |
| 699 | !skb_gso_validate_network_len(skb, ip6_skb_dst_mtu(skb)))) { |
| 700 | skb->dev = dst->dev; |
| 701 | skb->protocol = htons(ETH_P_IPV6); |
| 702 | |
| 703 | if (xfrm6_local_dontfrag(skb->sk)) |
| 704 | ipv6_stub->xfrm6_local_rxpmtu(skb, mtu); |
| 705 | else if (skb->sk) |
| 706 | xfrm_local_error(skb, mtu); |
| 707 | else |
| 708 | icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu); |
| 709 | ret = -EMSGSIZE; |
| 710 | } |
| 711 | out: |
| 712 | return ret; |
| 713 | } |
| 714 | #endif |
| 715 | |
| 716 | static int xfrm6_extract_output(struct xfrm_state *x, struct sk_buff *skb) |
| 717 | { |
| 718 | #if IS_ENABLED(CONFIG_IPV6) |
Xin Long | 68dc022 | 2021-03-19 18:27:58 +0800 | [diff] [blame] | 719 | unsigned int ptr = 0; |
Florian Westphal | f3075f48 | 2020-05-04 10:06:08 +0200 | [diff] [blame] | 720 | int err; |
| 721 | |
Xin Long | 68dc022 | 2021-03-19 18:27:58 +0800 | [diff] [blame] | 722 | if (x->outer_mode.encap == XFRM_MODE_BEET && |
| 723 | ipv6_find_hdr(skb, &ptr, NEXTHDR_FRAGMENT, NULL, NULL) >= 0) { |
| 724 | net_warn_ratelimited("BEET mode doesn't support inner IPv6 fragments\n"); |
| 725 | return -EAFNOSUPPORT; |
| 726 | } |
| 727 | |
Florian Westphal | f3075f48 | 2020-05-04 10:06:08 +0200 | [diff] [blame] | 728 | err = xfrm6_tunnel_check_size(skb); |
| 729 | if (err) |
| 730 | return err; |
| 731 | |
| 732 | XFRM_MODE_SKB_CB(skb)->protocol = ipv6_hdr(skb)->nexthdr; |
| 733 | |
| 734 | xfrm6_extract_header(skb); |
| 735 | return 0; |
| 736 | #else |
| 737 | WARN_ON_ONCE(1); |
| 738 | return -EAFNOSUPPORT; |
| 739 | #endif |
| 740 | } |
| 741 | |
Florian Westphal | 0c620e9 | 2019-03-29 21:16:25 +0100 | [diff] [blame] | 742 | static int xfrm_inner_extract_output(struct xfrm_state *x, struct sk_buff *skb) |
Kazunori MIYAZAWA | df9dcb4 | 2008-03-24 14:51:51 -0700 | [diff] [blame] | 743 | { |
Florian Westphal | 4c145dc | 2019-03-29 21:16:31 +0100 | [diff] [blame] | 744 | const struct xfrm_mode *inner_mode; |
Florian Westphal | 733a5fa | 2019-03-29 21:16:30 +0100 | [diff] [blame] | 745 | |
Kazunori MIYAZAWA | df9dcb4 | 2008-03-24 14:51:51 -0700 | [diff] [blame] | 746 | if (x->sel.family == AF_UNSPEC) |
| 747 | inner_mode = xfrm_ip2inner_mode(x, |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 748 | xfrm_af2proto(skb_dst(skb)->ops->family)); |
Kazunori MIYAZAWA | df9dcb4 | 2008-03-24 14:51:51 -0700 | [diff] [blame] | 749 | else |
Florian Westphal | c9500d7 | 2019-03-29 21:16:32 +0100 | [diff] [blame] | 750 | inner_mode = &x->inner_mode; |
Kazunori MIYAZAWA | df9dcb4 | 2008-03-24 14:51:51 -0700 | [diff] [blame] | 751 | |
| 752 | if (inner_mode == NULL) |
| 753 | return -EAFNOSUPPORT; |
Florian Westphal | 733a5fa | 2019-03-29 21:16:30 +0100 | [diff] [blame] | 754 | |
Florian Westphal | 6d64be3 | 2020-05-04 10:06:03 +0200 | [diff] [blame] | 755 | switch (inner_mode->family) { |
| 756 | case AF_INET: |
| 757 | return xfrm4_extract_output(x, skb); |
Florian Westphal | f3075f48 | 2020-05-04 10:06:08 +0200 | [diff] [blame] | 758 | case AF_INET6: |
| 759 | return xfrm6_extract_output(x, skb); |
Florian Westphal | 6d64be3 | 2020-05-04 10:06:03 +0200 | [diff] [blame] | 760 | } |
Florian Westphal | 733a5fa | 2019-03-29 21:16:30 +0100 | [diff] [blame] | 761 | |
Florian Westphal | f3075f48 | 2020-05-04 10:06:08 +0200 | [diff] [blame] | 762 | return -EAFNOSUPPORT; |
Kazunori MIYAZAWA | df9dcb4 | 2008-03-24 14:51:51 -0700 | [diff] [blame] | 763 | } |
| 764 | |
Hannes Frederic Sowa | 628e341 | 2013-08-14 13:05:23 +0200 | [diff] [blame] | 765 | void xfrm_local_error(struct sk_buff *skb, int mtu) |
| 766 | { |
Hannes Frederic Sowa | 844d487 | 2013-08-18 13:47:01 +0200 | [diff] [blame] | 767 | unsigned int proto; |
Hannes Frederic Sowa | 628e341 | 2013-08-14 13:05:23 +0200 | [diff] [blame] | 768 | struct xfrm_state_afinfo *afinfo; |
| 769 | |
Hannes Frederic Sowa | 844d487 | 2013-08-18 13:47:01 +0200 | [diff] [blame] | 770 | if (skb->protocol == htons(ETH_P_IP)) |
| 771 | proto = AF_INET; |
Xin Long | f6a23d8 | 2020-05-26 17:41:46 +0800 | [diff] [blame] | 772 | else if (skb->protocol == htons(ETH_P_IPV6) && |
| 773 | skb->sk->sk_family == AF_INET6) |
Hannes Frederic Sowa | 844d487 | 2013-08-18 13:47:01 +0200 | [diff] [blame] | 774 | proto = AF_INET6; |
| 775 | else |
| 776 | return; |
| 777 | |
| 778 | afinfo = xfrm_state_get_afinfo(proto); |
Taehee Yoo | 46c0ef6 | 2018-03-16 11:35:51 +0900 | [diff] [blame] | 779 | if (afinfo) { |
Florian Westphal | af5d27c | 2017-01-09 14:20:47 +0100 | [diff] [blame] | 780 | afinfo->local_error(skb, mtu); |
Taehee Yoo | 46c0ef6 | 2018-03-16 11:35:51 +0900 | [diff] [blame] | 781 | rcu_read_unlock(); |
| 782 | } |
Hannes Frederic Sowa | 628e341 | 2013-08-14 13:05:23 +0200 | [diff] [blame] | 783 | } |
Hannes Frederic Sowa | 628e341 | 2013-08-14 13:05:23 +0200 | [diff] [blame] | 784 | EXPORT_SYMBOL_GPL(xfrm_local_error); |