blob: 0b2975ef06687539307417d46a5ebfcc68f98634 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Herbert Xu406ef772007-10-08 17:16:30 -07002/*
3 * xfrm_output.c - Common IPsec encapsulation code.
4 *
5 * Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au>
Herbert Xu406ef772007-10-08 17:16:30 -07006 */
7
8#include <linux/errno.h>
9#include <linux/module.h>
10#include <linux/netdevice.h>
Herbert Xu862b82c2007-11-13 21:43:11 -080011#include <linux/netfilter.h>
Herbert Xu406ef772007-10-08 17:16:30 -070012#include <linux/skbuff.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Herbert Xu406ef772007-10-08 17:16:30 -070014#include <linux/spinlock.h>
Herbert Xu406ef772007-10-08 17:16:30 -070015#include <net/dst.h>
Florian Westphal6d64be32020-05-04 10:06:03 +020016#include <net/icmp.h>
Florian Westphal1de70832019-03-29 21:16:29 +010017#include <net/inet_ecn.h>
Herbert Xu406ef772007-10-08 17:16:30 -070018#include <net/xfrm.h>
19
Florian Westphalf3075f482020-05-04 10:06:08 +020020#if IS_ENABLED(CONFIG_IPV6)
21#include <net/ip6_route.h>
22#include <net/ipv6_stubs.h>
23#endif
24
Florian Westphal1de70832019-03-29 21:16:29 +010025#include "xfrm_inout.h"
26
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -050027static int xfrm_output2(struct net *net, struct sock *sk, struct sk_buff *skb);
Florian Westphal0c620e92019-03-29 21:16:25 +010028static int xfrm_inner_extract_output(struct xfrm_state *x, struct sk_buff *skb);
Herbert Xuc6581a42007-11-13 21:43:43 -080029
Steffen Klassert26b20722012-03-21 23:32:39 +000030static int xfrm_skb_check_space(struct sk_buff *skb)
Herbert Xu83815de2007-10-08 17:25:08 -070031{
Eric Dumazetadf30902009-06-02 05:19:30 +000032 struct dst_entry *dst = skb_dst(skb);
Herbert Xu550ade82007-11-13 21:33:01 -080033 int nhead = dst->header_len + LL_RESERVED_SPACE(dst->dev)
Herbert Xu83815de2007-10-08 17:25:08 -070034 - skb_headroom(skb);
Johannes Bergf5184d22008-05-12 20:48:31 -070035 int ntail = dst->dev->needed_tailroom - skb_tailroom(skb);
Herbert Xu83815de2007-10-08 17:25:08 -070036
Herbert Xud01dbeb2008-09-30 02:03:19 -070037 if (nhead <= 0) {
38 if (ntail <= 0)
39 return 0;
40 nhead = 0;
41 } else if (ntail < 0)
42 ntail = 0;
Herbert Xu83815de2007-10-08 17:25:08 -070043
Herbert Xud01dbeb2008-09-30 02:03:19 -070044 return pskb_expand_head(skb, nhead, ntail, GFP_ATOMIC);
Herbert Xu83815de2007-10-08 17:25:08 -070045}
46
Ying Xue9449c3c2015-05-12 18:29:44 +080047/* Children define the path of the packet through the
48 * Linux networking. Thus, destinations are stackable.
49 */
50
51static struct dst_entry *skb_dst_pop(struct sk_buff *skb)
52{
David Millerb92cf4a2017-11-28 15:40:22 -050053 struct dst_entry *child = dst_clone(xfrm_dst_child(skb_dst(skb)));
Ying Xue9449c3c2015-05-12 18:29:44 +080054
55 skb_dst_drop(skb);
56 return child;
57}
58
Florian Westphal0c620e92019-03-29 21:16:25 +010059/* Add encapsulation header.
60 *
61 * The IP header will be moved forward to make space for the encapsulation
62 * header.
63 */
64static int xfrm4_transport_output(struct xfrm_state *x, struct sk_buff *skb)
65{
Florian Westphal0c620e92019-03-29 21:16:25 +010066 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 Westphal0c620e92019-03-29 21:16:25 +010078}
79
Florian Westphal37b9e7e2021-06-11 12:50:11 +020080#if IS_ENABLED(CONFIG_IPV6_MIP6)
Florian Westphal3ca5ca82021-06-11 12:50:14 +020081static int mip6_rthdr_offset(struct sk_buff *skb, u8 **nexthdr, int type)
Florian Westphal37b9e7e2021-06-11 12:50:11 +020082{
Florian Westphal37b9e7e2021-06-11 12:50:11 +020083 const unsigned char *nh = skb_network_header(skb);
Florian Westphal3ca5ca82021-06-11 12:50:14 +020084 unsigned int offset = sizeof(struct ipv6hdr);
85 unsigned int packet_len;
Florian Westphal37b9e7e2021-06-11 12:50:11 +020086 int found_rhdr = 0;
87
Florian Westphal3ca5ca82021-06-11 12:50:14 +020088 packet_len = skb_tail_pointer(skb) - nh;
Florian Westphal37b9e7e2021-06-11 12:50:11 +020089 *nexthdr = &ipv6_hdr(skb)->nexthdr;
90
Florian Westphal3ca5ca82021-06-11 12:50:14 +020091 while (offset <= packet_len) {
92 struct ipv6_opt_hdr *exthdr;
93
Florian Westphal37b9e7e2021-06-11 12:50:11 +020094 switch (**nexthdr) {
95 case NEXTHDR_HOP:
96 break;
97 case NEXTHDR_ROUTING:
Florian Westphal3ca5ca82021-06-11 12:50:14 +020098 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 Westphal37b9e7e2021-06-11 12:50:11 +0200105 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 Westphal3ca5ca82021-06-11 12:50:14 +0200125 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 Westphal37b9e7e2021-06-11 12:50:11 +0200130 offset += ipv6_optlen(exthdr);
Florian Westphal3ca5ca82021-06-11 12:50:14 +0200131 if (offset > IPV6_MAXPLEN)
132 return -EINVAL;
Florian Westphal37b9e7e2021-06-11 12:50:11 +0200133 *nexthdr = &exthdr->nexthdr;
Florian Westphal37b9e7e2021-06-11 12:50:11 +0200134 }
135
Florian Westphal3ca5ca82021-06-11 12:50:14 +0200136 return -EINVAL;
Florian Westphal848b18f2021-06-11 12:50:12 +0200137}
Florian Westphal37b9e7e2021-06-11 12:50:11 +0200138#endif
139
Florian Westphal30ad6a82021-06-15 16:27:20 +0200140#if IS_ENABLED(CONFIG_IPV6)
Florian Westphal9acf4d32021-06-11 12:50:10 +0200141static int xfrm6_hdr_offset(struct xfrm_state *x, struct sk_buff *skb, u8 **prevhdr)
142{
Florian Westphal37b9e7e2021-06-11 12:50:11 +0200143 switch (x->type->proto) {
144#if IS_ENABLED(CONFIG_IPV6_MIP6)
145 case IPPROTO_DSTOPTS:
Florian Westphal848b18f2021-06-11 12:50:12 +0200146 case IPPROTO_ROUTING:
Florian Westphal3ca5ca82021-06-11 12:50:14 +0200147 return mip6_rthdr_offset(skb, prevhdr, x->type->proto);
Florian Westphal37b9e7e2021-06-11 12:50:11 +0200148#endif
149 default:
150 break;
151 }
152
Florian Westphald1002d22021-06-11 12:50:13 +0200153 return ip6_find_1stfragopt(skb, prevhdr);
Florian Westphal9acf4d32021-06-11 12:50:10 +0200154}
Florian Westphal30ad6a82021-06-15 16:27:20 +0200155#endif
Florian Westphal9acf4d32021-06-11 12:50:10 +0200156
Florian Westphal0c620e92019-03-29 21:16:25 +0100157/* 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 */
162static int xfrm6_transport_output(struct xfrm_state *x, struct sk_buff *skb)
163{
Florian Westphal4c145dc2019-03-29 21:16:31 +0100164#if IS_ENABLED(CONFIG_IPV6)
Florian Westphal0c620e92019-03-29 21:16:25 +0100165 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 Westphal9acf4d32021-06-11 12:50:10 +0200172 hdr_len = xfrm6_hdr_offset(x, skb, &prevhdr);
Florian Westphal0c620e92019-03-29 21:16:25 +0100173 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 Westphal4c145dc2019-03-29 21:16:31 +0100184 return -EAFNOSUPPORT;
Florian Westphal0c620e92019-03-29 21:16:25 +0100185#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 */
193static int xfrm6_ro_output(struct xfrm_state *x, struct sk_buff *skb)
194{
Florian Westphal4c145dc2019-03-29 21:16:31 +0100195#if IS_ENABLED(CONFIG_IPV6)
Florian Westphal0c620e92019-03-29 21:16:25 +0100196 struct ipv6hdr *iph;
197 u8 *prevhdr;
198 int hdr_len;
199
200 iph = ipv6_hdr(skb);
201
Florian Westphal9acf4d32021-06-11 12:50:10 +0200202 hdr_len = xfrm6_hdr_offset(x, skb, &prevhdr);
Florian Westphal0c620e92019-03-29 21:16:25 +0100203 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 Westphal4c145dc2019-03-29 21:16:31 +0100217 return -EAFNOSUPPORT;
Florian Westphal0c620e92019-03-29 21:16:25 +0100218#endif
219}
220
Florian Westphal1de70832019-03-29 21:16:29 +0100221/* Add encapsulation header.
222 *
223 * The top IP header will be constructed per draft-nikander-esp-beet-mode-06.txt.
224 */
225static 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 */
274static 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)
319static 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
354static 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 Westphal0c620e92019-03-29 21:16:25 +0100405static 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 Westphalc9500d72019-03-29 21:16:32 +0100416 switch (x->outer_mode.encap) {
Florian Westphal1de70832019-03-29 21:16:29 +0100417 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 Westphal0c620e92019-03-29 21:16:25 +0100425}
426
427static 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 Westphalc9500d72019-03-29 21:16:32 +0100439 switch (x->outer_mode.encap) {
Florian Westphal1de70832019-03-29 21:16:29 +0100440 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 Westphal0c620e92019-03-29 21:16:25 +0100448#endif
Florian Westphal1de70832019-03-29 21:16:29 +0100449 WARN_ON_ONCE(1);
450 return -EAFNOSUPPORT;
Florian Westphal0c620e92019-03-29 21:16:25 +0100451}
452
453static int xfrm_outer_mode_output(struct xfrm_state *x, struct sk_buff *skb)
454{
Florian Westphalc9500d72019-03-29 21:16:32 +0100455 switch (x->outer_mode.encap) {
Florian Westphal0c620e92019-03-29 21:16:25 +0100456 case XFRM_MODE_BEET:
457 case XFRM_MODE_TUNNEL:
Florian Westphalc9500d72019-03-29 21:16:32 +0100458 if (x->outer_mode.family == AF_INET)
Florian Westphal0c620e92019-03-29 21:16:25 +0100459 return xfrm4_prepare_output(x, skb);
Florian Westphalc9500d72019-03-29 21:16:32 +0100460 if (x->outer_mode.family == AF_INET6)
Florian Westphal0c620e92019-03-29 21:16:25 +0100461 return xfrm6_prepare_output(x, skb);
462 break;
463 case XFRM_MODE_TRANSPORT:
Florian Westphalc9500d72019-03-29 21:16:32 +0100464 if (x->outer_mode.family == AF_INET)
Florian Westphal0c620e92019-03-29 21:16:25 +0100465 return xfrm4_transport_output(x, skb);
Florian Westphalc9500d72019-03-29 21:16:32 +0100466 if (x->outer_mode.family == AF_INET6)
Florian Westphal0c620e92019-03-29 21:16:25 +0100467 return xfrm6_transport_output(x, skb);
468 break;
469 case XFRM_MODE_ROUTEOPTIMIZATION:
Florian Westphalc9500d72019-03-29 21:16:32 +0100470 if (x->outer_mode.family == AF_INET6)
Florian Westphal0c620e92019-03-29 21:16:25 +0100471 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)
483int pktgen_xfrm_outer_mode_output(struct xfrm_state *x, struct sk_buff *skb)
484{
485 return xfrm_outer_mode_output(x, skb);
486}
487EXPORT_SYMBOL_GPL(pktgen_xfrm_outer_mode_output);
488#endif
489
Herbert Xuc6581a42007-11-13 21:43:43 -0800490static int xfrm_output_one(struct sk_buff *skb, int err)
Herbert Xu406ef772007-10-08 17:16:30 -0700491{
Eric Dumazetadf30902009-06-02 05:19:30 +0000492 struct dst_entry *dst = skb_dst(skb);
Herbert Xu406ef772007-10-08 17:16:30 -0700493 struct xfrm_state *x = dst->xfrm;
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800494 struct net *net = xs_net(x);
Herbert Xu406ef772007-10-08 17:16:30 -0700495
Herbert Xuc6581a42007-11-13 21:43:43 -0800496 if (err <= 0)
497 goto resume;
Herbert Xu406ef772007-10-08 17:16:30 -0700498
499 do {
Steffen Klassert26b20722012-03-21 23:32:39 +0000500 err = xfrm_skb_check_space(skb);
Masahide NAKAMURAb15c4bc2007-12-24 16:00:09 -0800501 if (err) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800502 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
Herbert Xu910ef702007-12-18 22:14:25 -0800503 goto error_nolock;
Masahide NAKAMURAb15c4bc2007-12-24 16:00:09 -0800504 }
Herbert Xu910ef702007-12-18 22:14:25 -0800505
Steffen Klassert9b42c1f2018-06-12 12:44:26 +0200506 skb->mark = xfrm_smark_get(skb->mark, x);
Lorenzo Colitti077fbac2017-08-11 02:11:33 +0900507
Florian Westphal0c620e92019-03-29 21:16:25 +0100508 err = xfrm_outer_mode_output(x, skb);
Masahide NAKAMURAb15c4bc2007-12-24 16:00:09 -0800509 if (err) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800510 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEMODEERROR);
Herbert Xu910ef702007-12-18 22:14:25 -0800511 goto error_nolock;
Masahide NAKAMURAb15c4bc2007-12-24 16:00:09 -0800512 }
Herbert Xua2deb6d22007-11-13 21:39:38 -0800513
Herbert Xu406ef772007-10-08 17:16:30 -0700514 spin_lock_bh(&x->lock);
Li RongQingbb65a9c2012-12-28 16:06:28 +0800515
516 if (unlikely(x->km.state != XFRM_STATE_VALID)) {
517 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEINVALID);
Timo Teräs497574c2013-05-22 01:40:47 +0000518 err = -EINVAL;
Li RongQingfa8599d2013-02-01 13:17:16 +0800519 goto error;
Li RongQingbb65a9c2012-12-28 16:06:28 +0800520 }
521
Herbert Xu910ef702007-12-18 22:14:25 -0800522 err = xfrm_state_check_expire(x);
Masahide NAKAMURAb15c4bc2007-12-24 16:00:09 -0800523 if (err) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800524 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEEXPIRED);
Herbert Xu406ef772007-10-08 17:16:30 -0700525 goto error;
Masahide NAKAMURAb15c4bc2007-12-24 16:00:09 -0800526 }
Herbert Xu406ef772007-10-08 17:16:30 -0700527
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000528 err = x->repl->overflow(x, skb);
529 if (err) {
530 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATESEQERROR);
531 goto error;
Herbert Xu436a0a42007-10-08 17:25:53 -0700532 }
533
Herbert Xu406ef772007-10-08 17:16:30 -0700534 x->curlft.bytes += skb->len;
535 x->curlft.packets++;
536
Herbert Xu406ef772007-10-08 17:16:30 -0700537 spin_unlock_bh(&x->lock);
538
Steffen Klassert3bc07322011-03-15 21:08:28 +0000539 skb_dst_force(skb);
Steffen Klassert9e143792018-09-11 10:31:15 +0200540 if (!skb_dst(skb)) {
541 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
Wei Yongjun533555e2018-10-27 06:12:06 +0000542 err = -EHOSTUNREACH;
Steffen Klassert9e143792018-09-11 10:31:15 +0200543 goto error_nolock;
544 }
Steffen Klassert3bc07322011-03-15 21:08:28 +0000545
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200546 if (xfrm_offload(skb)) {
547 x->type_offload->encap(x, skb);
548 } else {
Steffen Klassert73b9fc42017-10-30 10:04:04 +0100549 /* Inner headers are invalid now. */
550 skb->encapsulation = 0;
551
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200552 err = x->type->output(x, skb);
553 if (err == -EINPROGRESS)
554 goto out;
555 }
Herbert Xuc6581a42007-11-13 21:43:43 -0800556
557resume:
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800558 if (err) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800559 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEPROTOERROR);
Herbert Xub7c6538c2007-10-09 13:33:35 -0700560 goto error_nolock;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -0800561 }
Herbert Xub7c6538c2007-10-09 13:33:35 -0700562
Steffen Klassert8764ab22010-06-04 01:57:38 +0000563 dst = skb_dst_pop(skb);
Eric Dumazetadf30902009-06-02 05:19:30 +0000564 if (!dst) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800565 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
Herbert Xu406ef772007-10-08 17:16:30 -0700566 err = -EHOSTUNREACH;
567 goto error_nolock;
568 }
Steffen Klasserte4334302011-03-15 21:09:32 +0000569 skb_dst_set(skb, dst);
Herbert Xu406ef772007-10-08 17:16:30 -0700570 x = dst->xfrm;
Florian Westphalc9500d72019-03-29 21:16:32 +0100571 } while (x && !(x->outer_mode.flags & XFRM_MODE_FLAG_TUNNEL));
Herbert Xu406ef772007-10-08 17:16:30 -0700572
Jean Sacrenebd46872013-06-01 16:23:15 +0000573 return 0;
Herbert Xu406ef772007-10-08 17:16:30 -0700574
Herbert Xu406ef772007-10-08 17:16:30 -0700575error:
576 spin_unlock_bh(&x->lock);
Herbert Xu862b82c2007-11-13 21:43:11 -0800577error_nolock:
578 kfree_skb(skb);
Jean Sacrenebd46872013-06-01 16:23:15 +0000579out:
580 return err;
Herbert Xu862b82c2007-11-13 21:43:11 -0800581}
582
Evan Nimmo9ab12652021-03-02 08:00:04 +1300583int xfrm_output_resume(struct sock *sk, struct sk_buff *skb, int err)
Herbert Xu862b82c2007-11-13 21:43:11 -0800584{
Eric W. Biederman29a26a52015-09-15 20:04:16 -0500585 struct net *net = xs_net(skb_dst(skb)->xfrm);
Eric W. Biedermanbe10de02015-09-17 17:21:31 -0500586
Herbert Xuc6581a42007-11-13 21:43:43 -0800587 while (likely((err = xfrm_output_one(skb, err)) == 0)) {
Florian Westphal895b5c92019-09-29 20:54:03 +0200588 nf_reset_ct(skb);
Herbert Xu862b82c2007-11-13 21:43:11 -0800589
Evan Nimmo9ab12652021-03-02 08:00:04 +1300590 err = skb_dst(skb)->ops->local_out(net, sk, skb);
Herbert Xu862b82c2007-11-13 21:43:11 -0800591 if (unlikely(err != 1))
Herbert Xuc6581a42007-11-13 21:43:43 -0800592 goto out;
Herbert Xu862b82c2007-11-13 21:43:11 -0800593
Eric Dumazetadf30902009-06-02 05:19:30 +0000594 if (!skb_dst(skb)->xfrm)
Evan Nimmo9ab12652021-03-02 08:00:04 +1300595 return dst_output(net, sk, skb);
Herbert Xu862b82c2007-11-13 21:43:11 -0800596
Eric Dumazetadf30902009-06-02 05:19:30 +0000597 err = nf_hook(skb_dst(skb)->ops->family,
Evan Nimmo9ab12652021-03-02 08:00:04 +1300598 NF_INET_POST_ROUTING, net, sk, skb,
Eric Dumazetadf30902009-06-02 05:19:30 +0000599 NULL, skb_dst(skb)->dev, xfrm_output2);
Herbert Xu862b82c2007-11-13 21:43:11 -0800600 if (unlikely(err != 1))
Herbert Xuc6581a42007-11-13 21:43:43 -0800601 goto out;
Herbert Xu862b82c2007-11-13 21:43:11 -0800602 }
603
Herbert Xuc6581a42007-11-13 21:43:43 -0800604 if (err == -EINPROGRESS)
605 err = 0;
606
607out:
Herbert Xu862b82c2007-11-13 21:43:11 -0800608 return err;
609}
Herbert Xuc6581a42007-11-13 21:43:43 -0800610EXPORT_SYMBOL_GPL(xfrm_output_resume);
Herbert Xu862b82c2007-11-13 21:43:11 -0800611
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -0500612static int xfrm_output2(struct net *net, struct sock *sk, struct sk_buff *skb)
Herbert Xuc6581a42007-11-13 21:43:43 -0800613{
Evan Nimmo9ab12652021-03-02 08:00:04 +1300614 return xfrm_output_resume(sk, skb, 1);
Herbert Xuc6581a42007-11-13 21:43:43 -0800615}
616
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -0500617static int xfrm_output_gso(struct net *net, struct sock *sk, struct sk_buff *skb)
Herbert Xu862b82c2007-11-13 21:43:11 -0800618{
Jason A. Donenfeldc3b18e02020-01-13 18:42:28 -0500619 struct sk_buff *segs, *nskb;
Herbert Xu862b82c2007-11-13 21:43:11 -0800620
Cambda Zhua08e7fd2020-03-26 15:33:14 +0800621 BUILD_BUG_ON(sizeof(*IPCB(skb)) > SKB_GSO_CB_OFFSET);
622 BUILD_BUG_ON(sizeof(*IP6CB(skb)) > SKB_GSO_CB_OFFSET);
Herbert Xu862b82c2007-11-13 21:43:11 -0800623 segs = skb_gso_segment(skb, 0);
624 kfree_skb(skb);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -0700625 if (IS_ERR(segs))
Herbert Xu862b82c2007-11-13 21:43:11 -0800626 return PTR_ERR(segs);
Florian Westphal330966e2014-10-20 13:49:17 +0200627 if (segs == NULL)
628 return -EINVAL;
Herbert Xu862b82c2007-11-13 21:43:11 -0800629
Jason A. Donenfeldc3b18e02020-01-13 18:42:28 -0500630 skb_list_walk_safe(segs, segs, nskb) {
Herbert Xu862b82c2007-11-13 21:43:11 -0800631 int err;
632
David S. Millera8305bf2018-07-29 20:42:53 -0700633 skb_mark_not_on_list(segs);
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -0500634 err = xfrm_output2(net, sk, segs);
Herbert Xu862b82c2007-11-13 21:43:11 -0800635
636 if (unlikely(err)) {
Florian Westphal46cfd722014-09-10 01:08:46 +0200637 kfree_skb_list(nskb);
Herbert Xu862b82c2007-11-13 21:43:11 -0800638 return err;
639 }
Jason A. Donenfeldc3b18e02020-01-13 18:42:28 -0500640 }
Herbert Xu862b82c2007-11-13 21:43:11 -0800641
642 return 0;
Herbert Xu406ef772007-10-08 17:16:30 -0700643}
Herbert Xuc6581a42007-11-13 21:43:43 -0800644
David Miller7026b1d2015-04-05 22:19:04 -0400645int xfrm_output(struct sock *sk, struct sk_buff *skb)
Herbert Xuc6581a42007-11-13 21:43:43 -0800646{
Eric Dumazetadf30902009-06-02 05:19:30 +0000647 struct net *net = dev_net(skb_dst(skb)->dev);
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200648 struct xfrm_state *x = skb_dst(skb)->xfrm;
Herbert Xuc6581a42007-11-13 21:43:43 -0800649 int err;
650
Florian Westphal2ab60962020-05-04 10:06:09 +0200651 switch (x->outer_mode.family) {
652 case AF_INET:
653 memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
Florian Westphal2ab60962020-05-04 10:06:09 +0200654 IPCB(skb)->flags |= IPSKB_XFRM_TRANSFORMED;
Florian Westphal2ab60962020-05-04 10:06:09 +0200655 break;
656 case AF_INET6:
657 memset(IP6CB(skb), 0, sizeof(*IP6CB(skb)));
658
Florian Westphal2ab60962020-05-04 10:06:09 +0200659 IP6CB(skb)->flags |= IP6SKB_XFRM_TRANSFORMED;
Florian Westphal2ab60962020-05-04 10:06:09 +0200660 break;
661 }
662
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200663 secpath_reset(skb);
664
665 if (xfrm_dev_offload_ok(skb, x)) {
666 struct sec_path *sp;
667
Florian Westphala84e3f52018-12-18 17:15:26 +0100668 sp = secpath_set(skb);
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200669 if (!sp) {
670 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
671 kfree_skb(skb);
672 return -ENOMEM;
673 }
Ilan Tayarif1bd7d62017-04-14 10:07:39 +0200674 skb->encapsulation = 1;
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200675
676 sp->olen++;
Florian Westphala84e3f52018-12-18 17:15:26 +0100677 sp->xvec[sp->len++] = x;
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200678 xfrm_state_hold(x);
679
680 if (skb_is_gso(skb)) {
Xin Longa204aef2020-04-20 21:51:09 +0800681 if (skb->inner_protocol)
682 return xfrm_output_gso(net, sk, skb);
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200683
Xin Longa204aef2020-04-20 21:51:09 +0800684 skb_shinfo(skb)->gso_type |= SKB_GSO_ESP;
685 goto out;
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200686 }
687
688 if (x->xso.dev && x->xso.dev->features & NETIF_F_HW_ESP_TX_CSUM)
689 goto out;
Xin Longa204aef2020-04-20 21:51:09 +0800690 } else {
691 if (skb_is_gso(skb))
692 return xfrm_output_gso(net, sk, skb);
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200693 }
694
Herbert Xuc6581a42007-11-13 21:43:43 -0800695 if (skb->ip_summed == CHECKSUM_PARTIAL) {
696 err = skb_checksum_help(skb);
697 if (err) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -0800698 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
Herbert Xuc6581a42007-11-13 21:43:43 -0800699 kfree_skb(skb);
700 return err;
701 }
702 }
703
Steffen Klassertd77e38e2017-04-14 10:06:10 +0200704out:
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -0500705 return xfrm_output2(net, sk, skb);
Herbert Xuc6581a42007-11-13 21:43:43 -0800706}
Fabian Frederickfc680862014-05-12 19:54:34 +0200707EXPORT_SYMBOL_GPL(xfrm_output);
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700708
Florian Westphal6d64be32020-05-04 10:06:03 +0200709static int xfrm4_tunnel_check_size(struct sk_buff *skb)
710{
711 int mtu, ret = 0;
712
713 if (IPCB(skb)->flags & IPSKB_XFRM_TUNNEL_SIZE)
714 goto out;
715
716 if (!(ip_hdr(skb)->frag_off & htons(IP_DF)) || skb->ignore_df)
717 goto out;
718
719 mtu = dst_mtu(skb_dst(skb));
720 if ((!skb_is_gso(skb) && skb->len > mtu) ||
721 (skb_is_gso(skb) &&
722 !skb_gso_validate_network_len(skb, ip_skb_dst_mtu(skb->sk, skb)))) {
723 skb->protocol = htons(ETH_P_IP);
724
725 if (skb->sk)
726 xfrm_local_error(skb, mtu);
727 else
728 icmp_send(skb, ICMP_DEST_UNREACH,
729 ICMP_FRAG_NEEDED, htonl(mtu));
730 ret = -EMSGSIZE;
731 }
732out:
733 return ret;
734}
735
736static int xfrm4_extract_output(struct xfrm_state *x, struct sk_buff *skb)
737{
738 int err;
739
Xin Long68dc0222021-03-19 18:27:58 +0800740 if (x->outer_mode.encap == XFRM_MODE_BEET &&
741 ip_is_fragment(ip_hdr(skb))) {
742 net_warn_ratelimited("BEET mode doesn't support inner IPv4 fragments\n");
743 return -EAFNOSUPPORT;
744 }
745
Florian Westphal6d64be32020-05-04 10:06:03 +0200746 err = xfrm4_tunnel_check_size(skb);
747 if (err)
748 return err;
749
750 XFRM_MODE_SKB_CB(skb)->protocol = ip_hdr(skb)->protocol;
751
752 xfrm4_extract_header(skb);
753 return 0;
754}
755
Florian Westphalf3075f482020-05-04 10:06:08 +0200756#if IS_ENABLED(CONFIG_IPV6)
757static int xfrm6_tunnel_check_size(struct sk_buff *skb)
758{
759 int mtu, ret = 0;
760 struct dst_entry *dst = skb_dst(skb);
761
762 if (skb->ignore_df)
763 goto out;
764
765 mtu = dst_mtu(dst);
766 if (mtu < IPV6_MIN_MTU)
767 mtu = IPV6_MIN_MTU;
768
769 if ((!skb_is_gso(skb) && skb->len > mtu) ||
770 (skb_is_gso(skb) &&
771 !skb_gso_validate_network_len(skb, ip6_skb_dst_mtu(skb)))) {
772 skb->dev = dst->dev;
773 skb->protocol = htons(ETH_P_IPV6);
774
775 if (xfrm6_local_dontfrag(skb->sk))
776 ipv6_stub->xfrm6_local_rxpmtu(skb, mtu);
777 else if (skb->sk)
778 xfrm_local_error(skb, mtu);
779 else
780 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
781 ret = -EMSGSIZE;
782 }
783out:
784 return ret;
785}
786#endif
787
788static int xfrm6_extract_output(struct xfrm_state *x, struct sk_buff *skb)
789{
790#if IS_ENABLED(CONFIG_IPV6)
Xin Long68dc0222021-03-19 18:27:58 +0800791 unsigned int ptr = 0;
Florian Westphalf3075f482020-05-04 10:06:08 +0200792 int err;
793
Xin Long68dc0222021-03-19 18:27:58 +0800794 if (x->outer_mode.encap == XFRM_MODE_BEET &&
795 ipv6_find_hdr(skb, &ptr, NEXTHDR_FRAGMENT, NULL, NULL) >= 0) {
796 net_warn_ratelimited("BEET mode doesn't support inner IPv6 fragments\n");
797 return -EAFNOSUPPORT;
798 }
799
Florian Westphalf3075f482020-05-04 10:06:08 +0200800 err = xfrm6_tunnel_check_size(skb);
801 if (err)
802 return err;
803
804 XFRM_MODE_SKB_CB(skb)->protocol = ipv6_hdr(skb)->nexthdr;
805
806 xfrm6_extract_header(skb);
807 return 0;
808#else
809 WARN_ON_ONCE(1);
810 return -EAFNOSUPPORT;
811#endif
812}
813
Florian Westphal0c620e92019-03-29 21:16:25 +0100814static int xfrm_inner_extract_output(struct xfrm_state *x, struct sk_buff *skb)
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700815{
Florian Westphal4c145dc2019-03-29 21:16:31 +0100816 const struct xfrm_mode *inner_mode;
Florian Westphal733a5fa2019-03-29 21:16:30 +0100817
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700818 if (x->sel.family == AF_UNSPEC)
819 inner_mode = xfrm_ip2inner_mode(x,
Eric Dumazetadf30902009-06-02 05:19:30 +0000820 xfrm_af2proto(skb_dst(skb)->ops->family));
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700821 else
Florian Westphalc9500d72019-03-29 21:16:32 +0100822 inner_mode = &x->inner_mode;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700823
824 if (inner_mode == NULL)
825 return -EAFNOSUPPORT;
Florian Westphal733a5fa2019-03-29 21:16:30 +0100826
Florian Westphal6d64be32020-05-04 10:06:03 +0200827 switch (inner_mode->family) {
828 case AF_INET:
829 return xfrm4_extract_output(x, skb);
Florian Westphalf3075f482020-05-04 10:06:08 +0200830 case AF_INET6:
831 return xfrm6_extract_output(x, skb);
Florian Westphal6d64be32020-05-04 10:06:03 +0200832 }
Florian Westphal733a5fa2019-03-29 21:16:30 +0100833
Florian Westphalf3075f482020-05-04 10:06:08 +0200834 return -EAFNOSUPPORT;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700835}
836
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +0200837void xfrm_local_error(struct sk_buff *skb, int mtu)
838{
Hannes Frederic Sowa844d4872013-08-18 13:47:01 +0200839 unsigned int proto;
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +0200840 struct xfrm_state_afinfo *afinfo;
841
Hannes Frederic Sowa844d4872013-08-18 13:47:01 +0200842 if (skb->protocol == htons(ETH_P_IP))
843 proto = AF_INET;
Xin Longf6a23d82020-05-26 17:41:46 +0800844 else if (skb->protocol == htons(ETH_P_IPV6) &&
845 skb->sk->sk_family == AF_INET6)
Hannes Frederic Sowa844d4872013-08-18 13:47:01 +0200846 proto = AF_INET6;
847 else
848 return;
849
850 afinfo = xfrm_state_get_afinfo(proto);
Taehee Yoo46c0ef62018-03-16 11:35:51 +0900851 if (afinfo) {
Florian Westphalaf5d27c2017-01-09 14:20:47 +0100852 afinfo->local_error(skb, mtu);
Taehee Yoo46c0ef62018-03-16 11:35:51 +0900853 rcu_read_unlock();
854 }
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +0200855}
Hannes Frederic Sowa628e3412013-08-14 13:05:23 +0200856EXPORT_SYMBOL_GPL(xfrm_local_error);