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