1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * xfrm_output.c - Common IPsec encapsulation code.
5 * Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au>
8 #include <linux/errno.h>
9 #include <linux/module.h>
10 #include <linux/netdevice.h>
11 #include <linux/netfilter.h>
12 #include <linux/skbuff.h>
13 #include <linux/slab.h>
14 #include <linux/spinlock.h>
17 #include <net/inet_ecn.h>
20 #if IS_ENABLED(CONFIG_IPV6)
21 #include <net/ip6_route.h>
22 #include <net/ipv6_stubs.h>
25 #include "xfrm_inout.h"
27 static int xfrm_output2(struct net
*net
, struct sock
*sk
, struct sk_buff
*skb
);
28 static int xfrm_inner_extract_output(struct xfrm_state
*x
, struct sk_buff
*skb
);
30 static int xfrm_skb_check_space(struct sk_buff
*skb
)
32 struct dst_entry
*dst
= skb_dst(skb
);
33 int nhead
= dst
->header_len
+ LL_RESERVED_SPACE(dst
->dev
)
35 int ntail
= dst
->dev
->needed_tailroom
- skb_tailroom(skb
);
44 return pskb_expand_head(skb
, nhead
, ntail
, GFP_ATOMIC
);
47 /* Children define the path of the packet through the
48 * Linux networking. Thus, destinations are stackable.
51 static struct dst_entry
*skb_dst_pop(struct sk_buff
*skb
)
53 struct dst_entry
*child
= dst_clone(xfrm_dst_child(skb_dst(skb
)));
59 /* Add encapsulation header.
61 * The IP header will be moved forward to make space for the encapsulation
64 static int xfrm4_transport_output(struct xfrm_state
*x
, struct sk_buff
*skb
)
66 struct iphdr
*iph
= ip_hdr(skb
);
67 int ihl
= iph
->ihl
* 4;
69 skb_set_inner_transport_header(skb
, skb_transport_offset(skb
));
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
;
76 memmove(skb_network_header(skb
), iph
, ihl
);
80 /* Add encapsulation header.
82 * The IP header and mutable extension headers will be moved forward to make
83 * space for the encapsulation header.
85 static int xfrm6_transport_output(struct xfrm_state
*x
, struct sk_buff
*skb
)
87 #if IS_ENABLED(CONFIG_IPV6)
93 skb_set_inner_transport_header(skb
, skb_transport_offset(skb
));
95 hdr_len
= x
->type
->hdr_offset(x
, skb
, &prevhdr
);
98 skb_set_mac_header(skb
,
99 (prevhdr
- x
->props
.header_len
) - skb
->data
);
100 skb_set_network_header(skb
, -x
->props
.header_len
);
101 skb
->transport_header
= skb
->network_header
+ hdr_len
;
102 __skb_pull(skb
, hdr_len
);
103 memmove(ipv6_hdr(skb
), iph
, hdr_len
);
107 return -EAFNOSUPPORT
;
111 /* Add route optimization header space.
113 * The IP header and mutable extension headers will be moved forward to make
114 * space for the route optimization header.
116 static int xfrm6_ro_output(struct xfrm_state
*x
, struct sk_buff
*skb
)
118 #if IS_ENABLED(CONFIG_IPV6)
125 hdr_len
= x
->type
->hdr_offset(x
, skb
, &prevhdr
);
128 skb_set_mac_header(skb
,
129 (prevhdr
- x
->props
.header_len
) - skb
->data
);
130 skb_set_network_header(skb
, -x
->props
.header_len
);
131 skb
->transport_header
= skb
->network_header
+ hdr_len
;
132 __skb_pull(skb
, hdr_len
);
133 memmove(ipv6_hdr(skb
), iph
, hdr_len
);
135 x
->lastused
= ktime_get_real_seconds();
140 return -EAFNOSUPPORT
;
144 /* Add encapsulation header.
146 * The top IP header will be constructed per draft-nikander-esp-beet-mode-06.txt.
148 static int xfrm4_beet_encap_add(struct xfrm_state
*x
, struct sk_buff
*skb
)
150 struct ip_beet_phdr
*ph
;
151 struct iphdr
*top_iph
;
155 optlen
= XFRM_MODE_SKB_CB(skb
)->optlen
;
156 if (unlikely(optlen
))
157 hdrlen
+= IPV4_BEET_PHMAXLEN
- (optlen
& 4);
159 skb_set_network_header(skb
, -x
->props
.header_len
- hdrlen
+
160 (XFRM_MODE_SKB_CB(skb
)->ihl
- sizeof(*top_iph
)));
161 if (x
->sel
.family
!= AF_INET6
)
162 skb
->network_header
+= IPV4_BEET_PHMAXLEN
;
163 skb
->mac_header
= skb
->network_header
+
164 offsetof(struct iphdr
, protocol
);
165 skb
->transport_header
= skb
->network_header
+ sizeof(*top_iph
);
167 xfrm4_beet_make_header(skb
);
169 ph
= __skb_pull(skb
, XFRM_MODE_SKB_CB(skb
)->ihl
- hdrlen
);
171 top_iph
= ip_hdr(skb
);
173 if (unlikely(optlen
)) {
174 if (WARN_ON(optlen
< 0))
177 ph
->padlen
= 4 - (optlen
& 4);
178 ph
->hdrlen
= optlen
/ 8;
179 ph
->nexthdr
= top_iph
->protocol
;
181 memset(ph
+ 1, IPOPT_NOP
, ph
->padlen
);
183 top_iph
->protocol
= IPPROTO_BEETPH
;
184 top_iph
->ihl
= sizeof(struct iphdr
) / 4;
187 top_iph
->saddr
= x
->props
.saddr
.a4
;
188 top_iph
->daddr
= x
->id
.daddr
.a4
;
193 /* Add encapsulation header.
195 * The top IP header will be constructed per RFC 2401.
197 static int xfrm4_tunnel_encap_add(struct xfrm_state
*x
, struct sk_buff
*skb
)
199 struct dst_entry
*dst
= skb_dst(skb
);
200 struct iphdr
*top_iph
;
203 skb_set_inner_network_header(skb
, skb_network_offset(skb
));
204 skb_set_inner_transport_header(skb
, skb_transport_offset(skb
));
206 skb_set_network_header(skb
, -x
->props
.header_len
);
207 skb
->mac_header
= skb
->network_header
+
208 offsetof(struct iphdr
, protocol
);
209 skb
->transport_header
= skb
->network_header
+ sizeof(*top_iph
);
210 top_iph
= ip_hdr(skb
);
213 top_iph
->version
= 4;
215 top_iph
->protocol
= xfrm_af2proto(skb_dst(skb
)->ops
->family
);
217 /* DS disclosing depends on XFRM_SA_XFLAG_DONT_ENCAP_DSCP */
218 if (x
->props
.extra_flags
& XFRM_SA_XFLAG_DONT_ENCAP_DSCP
)
221 top_iph
->tos
= XFRM_MODE_SKB_CB(skb
)->tos
;
222 top_iph
->tos
= INET_ECN_encapsulate(top_iph
->tos
,
223 XFRM_MODE_SKB_CB(skb
)->tos
);
225 flags
= x
->props
.flags
;
226 if (flags
& XFRM_STATE_NOECN
)
227 IP_ECN_clear(top_iph
);
229 top_iph
->frag_off
= (flags
& XFRM_STATE_NOPMTUDISC
) ?
230 0 : (XFRM_MODE_SKB_CB(skb
)->frag_off
& htons(IP_DF
));
232 top_iph
->ttl
= ip4_dst_hoplimit(xfrm_dst_child(dst
));
234 top_iph
->saddr
= x
->props
.saddr
.a4
;
235 top_iph
->daddr
= x
->id
.daddr
.a4
;
236 ip_select_ident(dev_net(dst
->dev
), skb
, NULL
);
241 #if IS_ENABLED(CONFIG_IPV6)
242 static int xfrm6_tunnel_encap_add(struct xfrm_state
*x
, struct sk_buff
*skb
)
244 struct dst_entry
*dst
= skb_dst(skb
);
245 struct ipv6hdr
*top_iph
;
248 skb_set_inner_network_header(skb
, skb_network_offset(skb
));
249 skb_set_inner_transport_header(skb
, skb_transport_offset(skb
));
251 skb_set_network_header(skb
, -x
->props
.header_len
);
252 skb
->mac_header
= skb
->network_header
+
253 offsetof(struct ipv6hdr
, nexthdr
);
254 skb
->transport_header
= skb
->network_header
+ sizeof(*top_iph
);
255 top_iph
= ipv6_hdr(skb
);
257 top_iph
->version
= 6;
259 memcpy(top_iph
->flow_lbl
, XFRM_MODE_SKB_CB(skb
)->flow_lbl
,
260 sizeof(top_iph
->flow_lbl
));
261 top_iph
->nexthdr
= xfrm_af2proto(skb_dst(skb
)->ops
->family
);
263 if (x
->props
.extra_flags
& XFRM_SA_XFLAG_DONT_ENCAP_DSCP
)
266 dsfield
= XFRM_MODE_SKB_CB(skb
)->tos
;
267 dsfield
= INET_ECN_encapsulate(dsfield
, XFRM_MODE_SKB_CB(skb
)->tos
);
268 if (x
->props
.flags
& XFRM_STATE_NOECN
)
269 dsfield
&= ~INET_ECN_MASK
;
270 ipv6_change_dsfield(top_iph
, 0, dsfield
);
271 top_iph
->hop_limit
= ip6_dst_hoplimit(xfrm_dst_child(dst
));
272 top_iph
->saddr
= *(struct in6_addr
*)&x
->props
.saddr
;
273 top_iph
->daddr
= *(struct in6_addr
*)&x
->id
.daddr
;
277 static int xfrm6_beet_encap_add(struct xfrm_state
*x
, struct sk_buff
*skb
)
279 struct ipv6hdr
*top_iph
;
280 struct ip_beet_phdr
*ph
;
284 optlen
= XFRM_MODE_SKB_CB(skb
)->optlen
;
285 if (unlikely(optlen
))
286 hdr_len
+= IPV4_BEET_PHMAXLEN
- (optlen
& 4);
288 skb_set_network_header(skb
, -x
->props
.header_len
- hdr_len
);
289 if (x
->sel
.family
!= AF_INET6
)
290 skb
->network_header
+= IPV4_BEET_PHMAXLEN
;
291 skb
->mac_header
= skb
->network_header
+
292 offsetof(struct ipv6hdr
, nexthdr
);
293 skb
->transport_header
= skb
->network_header
+ sizeof(*top_iph
);
294 ph
= __skb_pull(skb
, XFRM_MODE_SKB_CB(skb
)->ihl
- hdr_len
);
296 xfrm6_beet_make_header(skb
);
298 top_iph
= ipv6_hdr(skb
);
299 if (unlikely(optlen
)) {
300 if (WARN_ON(optlen
< 0))
303 ph
->padlen
= 4 - (optlen
& 4);
304 ph
->hdrlen
= optlen
/ 8;
305 ph
->nexthdr
= top_iph
->nexthdr
;
307 memset(ph
+ 1, IPOPT_NOP
, ph
->padlen
);
309 top_iph
->nexthdr
= IPPROTO_BEETPH
;
312 top_iph
->saddr
= *(struct in6_addr
*)&x
->props
.saddr
;
313 top_iph
->daddr
= *(struct in6_addr
*)&x
->id
.daddr
;
318 /* Add encapsulation header.
320 * On exit, the transport header will be set to the start of the
321 * encapsulation header to be filled in by x->type->output and the mac
322 * header will be set to the nextheader (protocol for IPv4) field of the
323 * extension header directly preceding the encapsulation header, or in
324 * its absence, that of the top IP header.
325 * The value of the network header will always point to the top IP header
326 * while skb->data will point to the payload.
328 static int xfrm4_prepare_output(struct xfrm_state
*x
, struct sk_buff
*skb
)
332 err
= xfrm_inner_extract_output(x
, skb
);
336 IPCB(skb
)->flags
|= IPSKB_XFRM_TUNNEL_SIZE
;
337 skb
->protocol
= htons(ETH_P_IP
);
339 switch (x
->outer_mode
.encap
) {
341 return xfrm4_beet_encap_add(x
, skb
);
342 case XFRM_MODE_TUNNEL
:
343 return xfrm4_tunnel_encap_add(x
, skb
);
350 static int xfrm6_prepare_output(struct xfrm_state
*x
, struct sk_buff
*skb
)
352 #if IS_ENABLED(CONFIG_IPV6)
355 err
= xfrm_inner_extract_output(x
, skb
);
360 skb
->protocol
= htons(ETH_P_IPV6
);
362 switch (x
->outer_mode
.encap
) {
364 return xfrm6_beet_encap_add(x
, skb
);
365 case XFRM_MODE_TUNNEL
:
366 return xfrm6_tunnel_encap_add(x
, skb
);
373 return -EAFNOSUPPORT
;
376 static int xfrm_outer_mode_output(struct xfrm_state
*x
, struct sk_buff
*skb
)
378 switch (x
->outer_mode
.encap
) {
380 case XFRM_MODE_TUNNEL
:
381 if (x
->outer_mode
.family
== AF_INET
)
382 return xfrm4_prepare_output(x
, skb
);
383 if (x
->outer_mode
.family
== AF_INET6
)
384 return xfrm6_prepare_output(x
, skb
);
386 case XFRM_MODE_TRANSPORT
:
387 if (x
->outer_mode
.family
== AF_INET
)
388 return xfrm4_transport_output(x
, skb
);
389 if (x
->outer_mode
.family
== AF_INET6
)
390 return xfrm6_transport_output(x
, skb
);
392 case XFRM_MODE_ROUTEOPTIMIZATION
:
393 if (x
->outer_mode
.family
== AF_INET6
)
394 return xfrm6_ro_output(x
, skb
);
405 #if IS_ENABLED(CONFIG_NET_PKTGEN)
406 int pktgen_xfrm_outer_mode_output(struct xfrm_state
*x
, struct sk_buff
*skb
)
408 return xfrm_outer_mode_output(x
, skb
);
410 EXPORT_SYMBOL_GPL(pktgen_xfrm_outer_mode_output
);
413 static int xfrm_output_one(struct sk_buff
*skb
, int err
)
415 struct dst_entry
*dst
= skb_dst(skb
);
416 struct xfrm_state
*x
= dst
->xfrm
;
417 struct net
*net
= xs_net(x
);
423 err
= xfrm_skb_check_space(skb
);
425 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTERROR
);
429 skb
->mark
= xfrm_smark_get(skb
->mark
, x
);
431 err
= xfrm_outer_mode_output(x
, skb
);
433 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTSTATEMODEERROR
);
437 spin_lock_bh(&x
->lock
);
439 if (unlikely(x
->km
.state
!= XFRM_STATE_VALID
)) {
440 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTSTATEINVALID
);
445 err
= xfrm_state_check_expire(x
);
447 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTSTATEEXPIRED
);
451 err
= x
->repl
->overflow(x
, skb
);
453 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTSTATESEQERROR
);
457 x
->curlft
.bytes
+= skb
->len
;
460 spin_unlock_bh(&x
->lock
);
464 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTERROR
);
469 if (xfrm_offload(skb
)) {
470 x
->type_offload
->encap(x
, skb
);
472 /* Inner headers are invalid now. */
473 skb
->encapsulation
= 0;
475 err
= x
->type
->output(x
, skb
);
476 if (err
== -EINPROGRESS
)
482 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTSTATEPROTOERROR
);
486 dst
= skb_dst_pop(skb
);
488 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTERROR
);
492 skb_dst_set(skb
, dst
);
494 } while (x
&& !(x
->outer_mode
.flags
& XFRM_MODE_FLAG_TUNNEL
));
499 spin_unlock_bh(&x
->lock
);
506 int xfrm_output_resume(struct sk_buff
*skb
, int err
)
508 struct net
*net
= xs_net(skb_dst(skb
)->xfrm
);
510 while (likely((err
= xfrm_output_one(skb
, err
)) == 0)) {
513 err
= skb_dst(skb
)->ops
->local_out(net
, skb
->sk
, skb
);
514 if (unlikely(err
!= 1))
517 if (!skb_dst(skb
)->xfrm
)
518 return dst_output(net
, skb
->sk
, skb
);
520 err
= nf_hook(skb_dst(skb
)->ops
->family
,
521 NF_INET_POST_ROUTING
, net
, skb
->sk
, skb
,
522 NULL
, skb_dst(skb
)->dev
, xfrm_output2
);
523 if (unlikely(err
!= 1))
527 if (err
== -EINPROGRESS
)
533 EXPORT_SYMBOL_GPL(xfrm_output_resume
);
535 static int xfrm_output2(struct net
*net
, struct sock
*sk
, struct sk_buff
*skb
)
537 return xfrm_output_resume(skb
, 1);
540 static int xfrm_output_gso(struct net
*net
, struct sock
*sk
, struct sk_buff
*skb
)
542 struct sk_buff
*segs
, *nskb
;
544 BUILD_BUG_ON(sizeof(*IPCB(skb
)) > SKB_GSO_CB_OFFSET
);
545 BUILD_BUG_ON(sizeof(*IP6CB(skb
)) > SKB_GSO_CB_OFFSET
);
546 segs
= skb_gso_segment(skb
, 0);
549 return PTR_ERR(segs
);
553 skb_list_walk_safe(segs
, segs
, nskb
) {
556 skb_mark_not_on_list(segs
);
557 err
= xfrm_output2(net
, sk
, segs
);
560 kfree_skb_list(nskb
);
568 int xfrm_output(struct sock
*sk
, struct sk_buff
*skb
)
570 struct net
*net
= dev_net(skb_dst(skb
)->dev
);
571 struct xfrm_state
*x
= skb_dst(skb
)->xfrm
;
574 switch (x
->outer_mode
.family
) {
576 memset(IPCB(skb
), 0, sizeof(*IPCB(skb
)));
577 IPCB(skb
)->flags
|= IPSKB_XFRM_TRANSFORMED
;
580 memset(IP6CB(skb
), 0, sizeof(*IP6CB(skb
)));
582 IP6CB(skb
)->flags
|= IP6SKB_XFRM_TRANSFORMED
;
588 if (xfrm_dev_offload_ok(skb
, x
)) {
591 sp
= secpath_set(skb
);
593 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTERROR
);
597 skb
->encapsulation
= 1;
600 sp
->xvec
[sp
->len
++] = x
;
603 if (skb_is_gso(skb
)) {
604 if (skb
->inner_protocol
)
605 return xfrm_output_gso(net
, sk
, skb
);
607 skb_shinfo(skb
)->gso_type
|= SKB_GSO_ESP
;
611 if (x
->xso
.dev
&& x
->xso
.dev
->features
& NETIF_F_HW_ESP_TX_CSUM
)
615 return xfrm_output_gso(net
, sk
, skb
);
618 if (skb
->ip_summed
== CHECKSUM_PARTIAL
) {
619 err
= skb_checksum_help(skb
);
621 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTERROR
);
628 return xfrm_output2(net
, sk
, skb
);
630 EXPORT_SYMBOL_GPL(xfrm_output
);
632 static int xfrm4_tunnel_check_size(struct sk_buff
*skb
)
636 if (IPCB(skb
)->flags
& IPSKB_XFRM_TUNNEL_SIZE
)
639 if (!(ip_hdr(skb
)->frag_off
& htons(IP_DF
)) || skb
->ignore_df
)
642 mtu
= dst_mtu(skb_dst(skb
));
643 if ((!skb_is_gso(skb
) && skb
->len
> mtu
) ||
645 !skb_gso_validate_network_len(skb
, ip_skb_dst_mtu(skb
->sk
, skb
)))) {
646 skb
->protocol
= htons(ETH_P_IP
);
649 xfrm_local_error(skb
, mtu
);
651 icmp_send(skb
, ICMP_DEST_UNREACH
,
652 ICMP_FRAG_NEEDED
, htonl(mtu
));
659 static int xfrm4_extract_output(struct xfrm_state
*x
, struct sk_buff
*skb
)
663 err
= xfrm4_tunnel_check_size(skb
);
667 XFRM_MODE_SKB_CB(skb
)->protocol
= ip_hdr(skb
)->protocol
;
669 xfrm4_extract_header(skb
);
673 #if IS_ENABLED(CONFIG_IPV6)
674 static int xfrm6_tunnel_check_size(struct sk_buff
*skb
)
677 struct dst_entry
*dst
= skb_dst(skb
);
683 if (mtu
< IPV6_MIN_MTU
)
686 if ((!skb_is_gso(skb
) && skb
->len
> mtu
) ||
688 !skb_gso_validate_network_len(skb
, ip6_skb_dst_mtu(skb
)))) {
690 skb
->protocol
= htons(ETH_P_IPV6
);
692 if (xfrm6_local_dontfrag(skb
->sk
))
693 ipv6_stub
->xfrm6_local_rxpmtu(skb
, mtu
);
695 xfrm_local_error(skb
, mtu
);
697 icmpv6_send(skb
, ICMPV6_PKT_TOOBIG
, 0, mtu
);
705 static int xfrm6_extract_output(struct xfrm_state
*x
, struct sk_buff
*skb
)
707 #if IS_ENABLED(CONFIG_IPV6)
710 err
= xfrm6_tunnel_check_size(skb
);
714 XFRM_MODE_SKB_CB(skb
)->protocol
= ipv6_hdr(skb
)->nexthdr
;
716 xfrm6_extract_header(skb
);
720 return -EAFNOSUPPORT
;
724 static int xfrm_inner_extract_output(struct xfrm_state
*x
, struct sk_buff
*skb
)
726 const struct xfrm_mode
*inner_mode
;
728 if (x
->sel
.family
== AF_UNSPEC
)
729 inner_mode
= xfrm_ip2inner_mode(x
,
730 xfrm_af2proto(skb_dst(skb
)->ops
->family
));
732 inner_mode
= &x
->inner_mode
;
734 if (inner_mode
== NULL
)
735 return -EAFNOSUPPORT
;
737 switch (inner_mode
->family
) {
739 return xfrm4_extract_output(x
, skb
);
741 return xfrm6_extract_output(x
, skb
);
744 return -EAFNOSUPPORT
;
747 void xfrm_local_error(struct sk_buff
*skb
, int mtu
)
750 struct xfrm_state_afinfo
*afinfo
;
752 if (skb
->protocol
== htons(ETH_P_IP
))
754 else if (skb
->protocol
== htons(ETH_P_IPV6
) &&
755 skb
->sk
->sk_family
== AF_INET6
)
760 afinfo
= xfrm_state_get_afinfo(proto
);
762 afinfo
->local_error(skb
, mtu
);
766 EXPORT_SYMBOL_GPL(xfrm_local_error
);