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