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