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