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>
18 #include <net/inet_ecn.h>
21 #if IS_ENABLED(CONFIG_IPV6)
22 #include <net/ip6_route.h>
23 #include <net/ipv6_stubs.h>
26 #include "xfrm_inout.h"
28 static int xfrm_output2(struct net
*net
, struct sock
*sk
, struct sk_buff
*skb
);
29 static int xfrm_inner_extract_output(struct xfrm_state
*x
, struct sk_buff
*skb
);
31 static int xfrm_skb_check_space(struct sk_buff
*skb
)
33 struct dst_entry
*dst
= skb_dst(skb
);
34 int nhead
= dst
->header_len
+ LL_RESERVED_SPACE(dst
->dev
)
36 int ntail
= dst
->dev
->needed_tailroom
- skb_tailroom(skb
);
45 return pskb_expand_head(skb
, nhead
, ntail
, GFP_ATOMIC
);
48 /* Children define the path of the packet through the
49 * Linux networking. Thus, destinations are stackable.
52 static struct dst_entry
*skb_dst_pop(struct sk_buff
*skb
)
54 struct dst_entry
*child
= dst_clone(xfrm_dst_child(skb_dst(skb
)));
60 /* Add encapsulation header.
62 * The IP header will be moved forward to make space for the encapsulation
65 static int xfrm4_transport_output(struct xfrm_state
*x
, struct sk_buff
*skb
)
67 struct iphdr
*iph
= ip_hdr(skb
);
68 int ihl
= iph
->ihl
* 4;
70 skb_set_inner_transport_header(skb
, skb_transport_offset(skb
));
72 skb_set_network_header(skb
, -x
->props
.header_len
);
73 skb
->mac_header
= skb
->network_header
+
74 offsetof(struct iphdr
, protocol
);
75 skb
->transport_header
= skb
->network_header
+ ihl
;
77 memmove(skb_network_header(skb
), iph
, ihl
);
81 #if IS_ENABLED(CONFIG_IPV6_MIP6)
82 static int mip6_rthdr_offset(struct sk_buff
*skb
, u8
**nexthdr
, int type
)
84 const unsigned char *nh
= skb_network_header(skb
);
85 unsigned int offset
= sizeof(struct ipv6hdr
);
86 unsigned int packet_len
;
89 packet_len
= skb_tail_pointer(skb
) - nh
;
90 *nexthdr
= &ipv6_hdr(skb
)->nexthdr
;
92 while (offset
<= packet_len
) {
93 struct ipv6_opt_hdr
*exthdr
;
99 if (type
== IPPROTO_ROUTING
&& offset
+ 3 <= packet_len
) {
100 struct ipv6_rt_hdr
*rt
;
102 rt
= (struct ipv6_rt_hdr
*)(nh
+ offset
);
109 /* HAO MUST NOT appear more than once.
110 * XXX: It is better to try to find by the end of
111 * XXX: packet if HAO exists.
113 if (ipv6_find_tlv(skb
, offset
, IPV6_TLV_HAO
) >= 0) {
114 net_dbg_ratelimited("mip6: hao exists already, override\n");
126 if (offset
+ sizeof(struct ipv6_opt_hdr
) > packet_len
)
129 exthdr
= (struct ipv6_opt_hdr
*)(skb_network_header(skb
) +
131 offset
+= ipv6_optlen(exthdr
);
132 if (offset
> IPV6_MAXPLEN
)
134 *nexthdr
= &exthdr
->nexthdr
;
141 #if IS_ENABLED(CONFIG_IPV6)
142 static int xfrm6_hdr_offset(struct xfrm_state
*x
, struct sk_buff
*skb
, u8
**prevhdr
)
144 switch (x
->type
->proto
) {
145 #if IS_ENABLED(CONFIG_IPV6_MIP6)
146 case IPPROTO_DSTOPTS
:
147 case IPPROTO_ROUTING
:
148 return mip6_rthdr_offset(skb
, prevhdr
, x
->type
->proto
);
154 return ip6_find_1stfragopt(skb
, prevhdr
);
158 /* Add encapsulation header.
160 * The IP header and mutable extension headers will be moved forward to make
161 * space for the encapsulation header.
163 static int xfrm6_transport_output(struct xfrm_state
*x
, struct sk_buff
*skb
)
165 #if IS_ENABLED(CONFIG_IPV6)
171 skb_set_inner_transport_header(skb
, skb_transport_offset(skb
));
173 hdr_len
= xfrm6_hdr_offset(x
, skb
, &prevhdr
);
176 skb_set_mac_header(skb
,
177 (prevhdr
- x
->props
.header_len
) - skb
->data
);
178 skb_set_network_header(skb
, -x
->props
.header_len
);
179 skb
->transport_header
= skb
->network_header
+ hdr_len
;
180 __skb_pull(skb
, hdr_len
);
181 memmove(ipv6_hdr(skb
), iph
, hdr_len
);
185 return -EAFNOSUPPORT
;
189 /* Add route optimization header space.
191 * The IP header and mutable extension headers will be moved forward to make
192 * space for the route optimization header.
194 static int xfrm6_ro_output(struct xfrm_state
*x
, struct sk_buff
*skb
)
196 #if IS_ENABLED(CONFIG_IPV6)
203 hdr_len
= xfrm6_hdr_offset(x
, skb
, &prevhdr
);
206 skb_set_mac_header(skb
,
207 (prevhdr
- x
->props
.header_len
) - skb
->data
);
208 skb_set_network_header(skb
, -x
->props
.header_len
);
209 skb
->transport_header
= skb
->network_header
+ hdr_len
;
210 __skb_pull(skb
, hdr_len
);
211 memmove(ipv6_hdr(skb
), iph
, hdr_len
);
216 return -EAFNOSUPPORT
;
220 /* Add encapsulation header.
222 * The top IP header will be constructed per draft-nikander-esp-beet-mode-06.txt.
224 static int xfrm4_beet_encap_add(struct xfrm_state
*x
, struct sk_buff
*skb
)
226 struct ip_beet_phdr
*ph
;
227 struct iphdr
*top_iph
;
231 optlen
= XFRM_MODE_SKB_CB(skb
)->optlen
;
232 if (unlikely(optlen
))
233 hdrlen
+= IPV4_BEET_PHMAXLEN
- (optlen
& 4);
235 skb_set_network_header(skb
, -x
->props
.header_len
- hdrlen
+
236 (XFRM_MODE_SKB_CB(skb
)->ihl
- sizeof(*top_iph
)));
237 if (x
->sel
.family
!= AF_INET6
)
238 skb
->network_header
+= IPV4_BEET_PHMAXLEN
;
239 skb
->mac_header
= skb
->network_header
+
240 offsetof(struct iphdr
, protocol
);
241 skb
->transport_header
= skb
->network_header
+ sizeof(*top_iph
);
243 xfrm4_beet_make_header(skb
);
245 ph
= __skb_pull(skb
, XFRM_MODE_SKB_CB(skb
)->ihl
- hdrlen
);
247 top_iph
= ip_hdr(skb
);
249 if (unlikely(optlen
)) {
250 if (WARN_ON(optlen
< 0))
253 ph
->padlen
= 4 - (optlen
& 4);
254 ph
->hdrlen
= optlen
/ 8;
255 ph
->nexthdr
= top_iph
->protocol
;
257 memset(ph
+ 1, IPOPT_NOP
, ph
->padlen
);
259 top_iph
->protocol
= IPPROTO_BEETPH
;
260 top_iph
->ihl
= sizeof(struct iphdr
) / 4;
263 top_iph
->saddr
= x
->props
.saddr
.a4
;
264 top_iph
->daddr
= x
->id
.daddr
.a4
;
269 /* Add encapsulation header.
271 * The top IP header will be constructed per RFC 2401.
273 static int xfrm4_tunnel_encap_add(struct xfrm_state
*x
, struct sk_buff
*skb
)
275 bool small_ipv6
= (skb
->protocol
== htons(ETH_P_IPV6
)) && (skb
->len
<= IPV6_MIN_MTU
);
276 struct dst_entry
*dst
= skb_dst(skb
);
277 struct iphdr
*top_iph
;
280 skb_set_inner_network_header(skb
, skb_network_offset(skb
));
281 skb_set_inner_transport_header(skb
, skb_transport_offset(skb
));
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
);
290 top_iph
->version
= 4;
292 top_iph
->protocol
= xfrm_af2proto(skb_dst(skb
)->ops
->family
);
294 /* DS disclosing depends on XFRM_SA_XFLAG_DONT_ENCAP_DSCP */
295 if (x
->props
.extra_flags
& XFRM_SA_XFLAG_DONT_ENCAP_DSCP
)
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
);
302 flags
= x
->props
.flags
;
303 if (flags
& XFRM_STATE_NOECN
)
304 IP_ECN_clear(top_iph
);
306 top_iph
->frag_off
= (flags
& XFRM_STATE_NOPMTUDISC
) || small_ipv6
?
307 0 : (XFRM_MODE_SKB_CB(skb
)->frag_off
& htons(IP_DF
));
309 top_iph
->ttl
= ip4_dst_hoplimit(xfrm_dst_child(dst
));
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
);
318 #if IS_ENABLED(CONFIG_IPV6)
319 static int xfrm6_tunnel_encap_add(struct xfrm_state
*x
, struct sk_buff
*skb
)
321 struct dst_entry
*dst
= skb_dst(skb
);
322 struct ipv6hdr
*top_iph
;
325 skb_set_inner_network_header(skb
, skb_network_offset(skb
));
326 skb_set_inner_transport_header(skb
, skb_transport_offset(skb
));
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
);
334 top_iph
->version
= 6;
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
);
340 if (x
->props
.extra_flags
& XFRM_SA_XFLAG_DONT_ENCAP_DSCP
)
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
;
354 static int xfrm6_beet_encap_add(struct xfrm_state
*x
, struct sk_buff
*skb
)
356 struct ipv6hdr
*top_iph
;
357 struct ip_beet_phdr
*ph
;
361 optlen
= XFRM_MODE_SKB_CB(skb
)->optlen
;
362 if (unlikely(optlen
))
363 hdr_len
+= IPV4_BEET_PHMAXLEN
- (optlen
& 4);
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
);
373 xfrm6_beet_make_header(skb
);
375 top_iph
= ipv6_hdr(skb
);
376 if (unlikely(optlen
)) {
377 if (WARN_ON(optlen
< 0))
380 ph
->padlen
= 4 - (optlen
& 4);
381 ph
->hdrlen
= optlen
/ 8;
382 ph
->nexthdr
= top_iph
->nexthdr
;
384 memset(ph
+ 1, IPOPT_NOP
, ph
->padlen
);
386 top_iph
->nexthdr
= IPPROTO_BEETPH
;
389 top_iph
->saddr
= *(struct in6_addr
*)&x
->props
.saddr
;
390 top_iph
->daddr
= *(struct in6_addr
*)&x
->id
.daddr
;
395 /* Add encapsulation header.
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.
405 static int xfrm4_prepare_output(struct xfrm_state
*x
, struct sk_buff
*skb
)
409 err
= xfrm_inner_extract_output(x
, skb
);
413 IPCB(skb
)->flags
|= IPSKB_XFRM_TUNNEL_SIZE
;
414 skb
->protocol
= htons(ETH_P_IP
);
416 switch (x
->props
.mode
) {
418 return xfrm4_beet_encap_add(x
, skb
);
419 case XFRM_MODE_TUNNEL
:
420 return xfrm4_tunnel_encap_add(x
, skb
);
427 static int xfrm6_prepare_output(struct xfrm_state
*x
, struct sk_buff
*skb
)
429 #if IS_ENABLED(CONFIG_IPV6)
432 err
= xfrm_inner_extract_output(x
, skb
);
437 skb
->protocol
= htons(ETH_P_IPV6
);
439 switch (x
->props
.mode
) {
441 return xfrm6_beet_encap_add(x
, skb
);
442 case XFRM_MODE_TUNNEL
:
443 return xfrm6_tunnel_encap_add(x
, skb
);
450 return -EAFNOSUPPORT
;
453 static int xfrm_outer_mode_output(struct xfrm_state
*x
, struct sk_buff
*skb
)
455 switch (x
->props
.mode
) {
457 case XFRM_MODE_TUNNEL
:
458 if (x
->props
.family
== AF_INET
)
459 return xfrm4_prepare_output(x
, skb
);
460 if (x
->props
.family
== AF_INET6
)
461 return xfrm6_prepare_output(x
, skb
);
463 case XFRM_MODE_TRANSPORT
:
464 if (x
->props
.family
== AF_INET
)
465 return xfrm4_transport_output(x
, skb
);
466 if (x
->props
.family
== AF_INET6
)
467 return xfrm6_transport_output(x
, skb
);
469 case XFRM_MODE_ROUTEOPTIMIZATION
:
470 if (x
->props
.family
== AF_INET6
)
471 return xfrm6_ro_output(x
, skb
);
482 #if IS_ENABLED(CONFIG_NET_PKTGEN)
483 int pktgen_xfrm_outer_mode_output(struct xfrm_state
*x
, struct sk_buff
*skb
)
485 return xfrm_outer_mode_output(x
, skb
);
487 EXPORT_SYMBOL_GPL(pktgen_xfrm_outer_mode_output
);
490 static int xfrm_output_one(struct sk_buff
*skb
, int err
)
492 struct dst_entry
*dst
= skb_dst(skb
);
493 struct xfrm_state
*x
= dst
->xfrm
;
494 struct net
*net
= xs_net(x
);
496 if (err
<= 0 || x
->xso
.type
== XFRM_DEV_OFFLOAD_PACKET
)
500 err
= xfrm_skb_check_space(skb
);
502 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTERROR
);
506 skb
->mark
= xfrm_smark_get(skb
->mark
, x
);
508 err
= xfrm_outer_mode_output(x
, skb
);
510 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTSTATEMODEERROR
);
514 spin_lock_bh(&x
->lock
);
516 if (unlikely(x
->km
.state
!= XFRM_STATE_VALID
)) {
517 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTSTATEINVALID
);
522 err
= xfrm_state_check_expire(x
);
524 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTSTATEEXPIRED
);
528 err
= xfrm_replay_overflow(x
, skb
);
530 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTSTATESEQERROR
);
534 x
->curlft
.bytes
+= skb
->len
;
536 x
->lastused
= ktime_get_real_seconds();
538 spin_unlock_bh(&x
->lock
);
542 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTERROR
);
547 if (xfrm_offload(skb
)) {
548 x
->type_offload
->encap(x
, skb
);
550 /* Inner headers are invalid now. */
551 skb
->encapsulation
= 0;
553 err
= x
->type
->output(x
, skb
);
554 if (err
== -EINPROGRESS
)
560 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTSTATEPROTOERROR
);
564 dst
= skb_dst_pop(skb
);
566 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTERROR
);
570 skb_dst_set(skb
, dst
);
572 } while (x
&& !(x
->outer_mode
.flags
& XFRM_MODE_FLAG_TUNNEL
));
577 spin_unlock_bh(&x
->lock
);
584 int xfrm_output_resume(struct sock
*sk
, struct sk_buff
*skb
, int err
)
586 struct net
*net
= xs_net(skb_dst(skb
)->xfrm
);
588 while (likely((err
= xfrm_output_one(skb
, err
)) == 0)) {
591 err
= skb_dst(skb
)->ops
->local_out(net
, sk
, skb
);
592 if (unlikely(err
!= 1))
595 if (!skb_dst(skb
)->xfrm
)
596 return dst_output(net
, sk
, skb
);
598 err
= nf_hook(skb_dst(skb
)->ops
->family
,
599 NF_INET_POST_ROUTING
, net
, sk
, skb
,
600 NULL
, skb_dst(skb
)->dev
, xfrm_output2
);
601 if (unlikely(err
!= 1))
605 if (err
== -EINPROGRESS
)
611 EXPORT_SYMBOL_GPL(xfrm_output_resume
);
613 static int xfrm_output2(struct net
*net
, struct sock
*sk
, struct sk_buff
*skb
)
615 return xfrm_output_resume(sk
, skb
, 1);
618 static int xfrm_output_gso(struct net
*net
, struct sock
*sk
, struct sk_buff
*skb
)
620 struct sk_buff
*segs
, *nskb
;
622 BUILD_BUG_ON(sizeof(*IPCB(skb
)) > SKB_GSO_CB_OFFSET
);
623 BUILD_BUG_ON(sizeof(*IP6CB(skb
)) > SKB_GSO_CB_OFFSET
);
624 segs
= skb_gso_segment(skb
, 0);
627 return PTR_ERR(segs
);
631 skb_list_walk_safe(segs
, segs
, nskb
) {
634 skb_mark_not_on_list(segs
);
635 err
= xfrm_output2(net
, sk
, segs
);
638 kfree_skb_list(nskb
);
646 /* For partial checksum offload, the outer header checksum is calculated
647 * by software and the inner header checksum is calculated by hardware.
648 * This requires hardware to know the inner packet type to calculate
649 * the inner header checksum. Save inner ip protocol here to avoid
650 * traversing the packet in the vendor's xmit code.
651 * For IPsec tunnel mode save the ip protocol from the IP header of the
652 * plain text packet. Otherwise If the encap type is IPIP, just save
653 * skb->inner_ipproto in any other case get the ip protocol from the IP
656 static void xfrm_get_inner_ipproto(struct sk_buff
*skb
, struct xfrm_state
*x
)
658 struct xfrm_offload
*xo
= xfrm_offload(skb
);
659 const struct ethhdr
*eth
;
664 if (x
->outer_mode
.encap
== XFRM_MODE_TUNNEL
) {
665 switch (x
->outer_mode
.family
) {
667 xo
->inner_ipproto
= ip_hdr(skb
)->protocol
;
670 xo
->inner_ipproto
= ipv6_hdr(skb
)->nexthdr
;
679 /* non-Tunnel Mode */
680 if (!skb
->encapsulation
)
683 if (skb
->inner_protocol_type
== ENCAP_TYPE_IPPROTO
) {
684 xo
->inner_ipproto
= skb
->inner_ipproto
;
688 if (skb
->inner_protocol_type
!= ENCAP_TYPE_ETHER
)
691 eth
= (struct ethhdr
*)skb_inner_mac_header(skb
);
693 switch (ntohs(eth
->h_proto
)) {
695 xo
->inner_ipproto
= inner_ipv6_hdr(skb
)->nexthdr
;
698 xo
->inner_ipproto
= inner_ip_hdr(skb
)->protocol
;
703 int xfrm_output(struct sock
*sk
, struct sk_buff
*skb
)
705 struct net
*net
= dev_net(skb_dst(skb
)->dev
);
706 struct xfrm_state
*x
= skb_dst(skb
)->xfrm
;
710 family
= (x
->xso
.type
!= XFRM_DEV_OFFLOAD_PACKET
) ? x
->outer_mode
.family
711 : skb_dst(skb
)->ops
->family
;
715 memset(IPCB(skb
), 0, sizeof(*IPCB(skb
)));
716 IPCB(skb
)->flags
|= IPSKB_XFRM_TRANSFORMED
;
719 memset(IP6CB(skb
), 0, sizeof(*IP6CB(skb
)));
721 IP6CB(skb
)->flags
|= IP6SKB_XFRM_TRANSFORMED
;
725 if (x
->xso
.type
== XFRM_DEV_OFFLOAD_PACKET
) {
726 if (!xfrm_dev_offload_ok(skb
, x
)) {
727 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTERROR
);
729 return -EHOSTUNREACH
;
732 return xfrm_output_resume(sk
, skb
, 0);
737 if (xfrm_dev_offload_ok(skb
, x
)) {
740 sp
= secpath_set(skb
);
742 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTERROR
);
748 sp
->xvec
[sp
->len
++] = x
;
751 xfrm_get_inner_ipproto(skb
, x
);
752 skb
->encapsulation
= 1;
754 if (skb_is_gso(skb
)) {
755 if (skb
->inner_protocol
)
756 return xfrm_output_gso(net
, sk
, skb
);
758 skb_shinfo(skb
)->gso_type
|= SKB_GSO_ESP
;
762 if (x
->xso
.dev
&& x
->xso
.dev
->features
& NETIF_F_HW_ESP_TX_CSUM
)
766 return xfrm_output_gso(net
, sk
, skb
);
769 if (skb
->ip_summed
== CHECKSUM_PARTIAL
) {
770 err
= skb_checksum_help(skb
);
772 XFRM_INC_STATS(net
, LINUX_MIB_XFRMOUTERROR
);
779 return xfrm_output2(net
, sk
, skb
);
781 EXPORT_SYMBOL_GPL(xfrm_output
);
783 static int xfrm4_tunnel_check_size(struct sk_buff
*skb
)
787 if (IPCB(skb
)->flags
& IPSKB_XFRM_TUNNEL_SIZE
)
790 if (!(ip_hdr(skb
)->frag_off
& htons(IP_DF
)) || skb
->ignore_df
)
793 mtu
= dst_mtu(skb_dst(skb
));
794 if ((!skb_is_gso(skb
) && skb
->len
> mtu
) ||
796 !skb_gso_validate_network_len(skb
, ip_skb_dst_mtu(skb
->sk
, skb
)))) {
797 skb
->protocol
= htons(ETH_P_IP
);
800 xfrm_local_error(skb
, mtu
);
802 icmp_send(skb
, ICMP_DEST_UNREACH
,
803 ICMP_FRAG_NEEDED
, htonl(mtu
));
810 static int xfrm4_extract_output(struct xfrm_state
*x
, struct sk_buff
*skb
)
814 if (x
->outer_mode
.encap
== XFRM_MODE_BEET
&&
815 ip_is_fragment(ip_hdr(skb
))) {
816 net_warn_ratelimited("BEET mode doesn't support inner IPv4 fragments\n");
817 return -EAFNOSUPPORT
;
820 err
= xfrm4_tunnel_check_size(skb
);
824 XFRM_MODE_SKB_CB(skb
)->protocol
= ip_hdr(skb
)->protocol
;
826 xfrm4_extract_header(skb
);
830 #if IS_ENABLED(CONFIG_IPV6)
831 static int xfrm6_tunnel_check_size(struct sk_buff
*skb
)
834 struct dst_entry
*dst
= skb_dst(skb
);
840 if (mtu
< IPV6_MIN_MTU
)
843 if ((!skb_is_gso(skb
) && skb
->len
> mtu
) ||
845 !skb_gso_validate_network_len(skb
, ip6_skb_dst_mtu(skb
)))) {
847 skb
->protocol
= htons(ETH_P_IPV6
);
849 if (xfrm6_local_dontfrag(skb
->sk
))
850 ipv6_stub
->xfrm6_local_rxpmtu(skb
, mtu
);
852 xfrm_local_error(skb
, mtu
);
854 icmpv6_send(skb
, ICMPV6_PKT_TOOBIG
, 0, mtu
);
862 static int xfrm6_extract_output(struct xfrm_state
*x
, struct sk_buff
*skb
)
864 #if IS_ENABLED(CONFIG_IPV6)
867 err
= xfrm6_tunnel_check_size(skb
);
871 XFRM_MODE_SKB_CB(skb
)->protocol
= ipv6_hdr(skb
)->nexthdr
;
873 xfrm6_extract_header(skb
);
877 return -EAFNOSUPPORT
;
881 static int xfrm_inner_extract_output(struct xfrm_state
*x
, struct sk_buff
*skb
)
883 switch (skb
->protocol
) {
884 case htons(ETH_P_IP
):
885 return xfrm4_extract_output(x
, skb
);
886 case htons(ETH_P_IPV6
):
887 return xfrm6_extract_output(x
, skb
);
890 return -EAFNOSUPPORT
;
893 void xfrm_local_error(struct sk_buff
*skb
, int mtu
)
896 struct xfrm_state_afinfo
*afinfo
;
898 if (skb
->protocol
== htons(ETH_P_IP
))
900 else if (skb
->protocol
== htons(ETH_P_IPV6
) &&
901 skb
->sk
->sk_family
== AF_INET6
)
906 afinfo
= xfrm_state_get_afinfo(proto
);
908 afinfo
->local_error(skb
, mtu
);
912 EXPORT_SYMBOL_GPL(xfrm_local_error
);