Merge tag 'sh-for-5.9' of git://git.libc.org/linux-sh
[linux/fpc-iii.git] / net / xfrm / xfrm_output.c
bloba7ab19353313ce1d8b6ce6a4e6f6c1821c8982d0
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * xfrm_output.c - Common IPsec encapsulation code.
5 * Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au>
6 */
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>
15 #include <net/dst.h>
16 #include <net/icmp.h>
17 #include <net/inet_ecn.h>
18 #include <net/xfrm.h>
20 #if IS_ENABLED(CONFIG_IPV6)
21 #include <net/ip6_route.h>
22 #include <net/ipv6_stubs.h>
23 #endif
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)
34 - skb_headroom(skb);
35 int ntail = dst->dev->needed_tailroom - skb_tailroom(skb);
37 if (nhead <= 0) {
38 if (ntail <= 0)
39 return 0;
40 nhead = 0;
41 } else if (ntail < 0)
42 ntail = 0;
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)));
55 skb_dst_drop(skb);
56 return child;
59 /* Add encapsulation header.
61 * The IP header will be moved forward to make space for the encapsulation
62 * header.
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;
75 __skb_pull(skb, ihl);
76 memmove(skb_network_header(skb), iph, ihl);
77 return 0;
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)
88 struct ipv6hdr *iph;
89 u8 *prevhdr;
90 int hdr_len;
92 iph = ipv6_hdr(skb);
93 skb_set_inner_transport_header(skb, skb_transport_offset(skb));
95 hdr_len = x->type->hdr_offset(x, skb, &prevhdr);
96 if (hdr_len < 0)
97 return hdr_len;
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);
104 return 0;
105 #else
106 WARN_ON_ONCE(1);
107 return -EAFNOSUPPORT;
108 #endif
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)
119 struct ipv6hdr *iph;
120 u8 *prevhdr;
121 int hdr_len;
123 iph = ipv6_hdr(skb);
125 hdr_len = x->type->hdr_offset(x, skb, &prevhdr);
126 if (hdr_len < 0)
127 return hdr_len;
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();
137 return 0;
138 #else
139 WARN_ON_ONCE(1);
140 return -EAFNOSUPPORT;
141 #endif
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;
152 int hdrlen, optlen;
154 hdrlen = 0;
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))
175 return -EINVAL;
177 ph->padlen = 4 - (optlen & 4);
178 ph->hdrlen = optlen / 8;
179 ph->nexthdr = top_iph->protocol;
180 if (ph->padlen)
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;
190 return 0;
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;
201 int flags;
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);
212 top_iph->ihl = 5;
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)
219 top_iph->tos = 0;
220 else
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);
238 return 0;
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;
246 int dsfield;
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)
264 dsfield = 0;
265 else
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;
274 return 0;
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;
281 int optlen, hdr_len;
283 hdr_len = 0;
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))
301 return -EINVAL;
303 ph->padlen = 4 - (optlen & 4);
304 ph->hdrlen = optlen / 8;
305 ph->nexthdr = top_iph->nexthdr;
306 if (ph->padlen)
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;
314 return 0;
316 #endif
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)
330 int err;
332 err = xfrm_inner_extract_output(x, skb);
333 if (err)
334 return err;
336 IPCB(skb)->flags |= IPSKB_XFRM_TUNNEL_SIZE;
337 skb->protocol = htons(ETH_P_IP);
339 switch (x->outer_mode.encap) {
340 case XFRM_MODE_BEET:
341 return xfrm4_beet_encap_add(x, skb);
342 case XFRM_MODE_TUNNEL:
343 return xfrm4_tunnel_encap_add(x, skb);
346 WARN_ON_ONCE(1);
347 return -EOPNOTSUPP;
350 static int xfrm6_prepare_output(struct xfrm_state *x, struct sk_buff *skb)
352 #if IS_ENABLED(CONFIG_IPV6)
353 int err;
355 err = xfrm_inner_extract_output(x, skb);
356 if (err)
357 return err;
359 skb->ignore_df = 1;
360 skb->protocol = htons(ETH_P_IPV6);
362 switch (x->outer_mode.encap) {
363 case XFRM_MODE_BEET:
364 return xfrm6_beet_encap_add(x, skb);
365 case XFRM_MODE_TUNNEL:
366 return xfrm6_tunnel_encap_add(x, skb);
367 default:
368 WARN_ON_ONCE(1);
369 return -EOPNOTSUPP;
371 #endif
372 WARN_ON_ONCE(1);
373 return -EAFNOSUPPORT;
376 static int xfrm_outer_mode_output(struct xfrm_state *x, struct sk_buff *skb)
378 switch (x->outer_mode.encap) {
379 case XFRM_MODE_BEET:
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);
385 break;
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);
391 break;
392 case XFRM_MODE_ROUTEOPTIMIZATION:
393 if (x->outer_mode.family == AF_INET6)
394 return xfrm6_ro_output(x, skb);
395 WARN_ON_ONCE(1);
396 break;
397 default:
398 WARN_ON_ONCE(1);
399 break;
402 return -EOPNOTSUPP;
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);
411 #endif
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);
419 if (err <= 0)
420 goto resume;
422 do {
423 err = xfrm_skb_check_space(skb);
424 if (err) {
425 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
426 goto error_nolock;
429 skb->mark = xfrm_smark_get(skb->mark, x);
431 err = xfrm_outer_mode_output(x, skb);
432 if (err) {
433 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEMODEERROR);
434 goto error_nolock;
437 spin_lock_bh(&x->lock);
439 if (unlikely(x->km.state != XFRM_STATE_VALID)) {
440 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEINVALID);
441 err = -EINVAL;
442 goto error;
445 err = xfrm_state_check_expire(x);
446 if (err) {
447 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEEXPIRED);
448 goto error;
451 err = x->repl->overflow(x, skb);
452 if (err) {
453 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATESEQERROR);
454 goto error;
457 x->curlft.bytes += skb->len;
458 x->curlft.packets++;
460 spin_unlock_bh(&x->lock);
462 skb_dst_force(skb);
463 if (!skb_dst(skb)) {
464 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
465 err = -EHOSTUNREACH;
466 goto error_nolock;
469 if (xfrm_offload(skb)) {
470 x->type_offload->encap(x, skb);
471 } else {
472 /* Inner headers are invalid now. */
473 skb->encapsulation = 0;
475 err = x->type->output(x, skb);
476 if (err == -EINPROGRESS)
477 goto out;
480 resume:
481 if (err) {
482 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTSTATEPROTOERROR);
483 goto error_nolock;
486 dst = skb_dst_pop(skb);
487 if (!dst) {
488 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
489 err = -EHOSTUNREACH;
490 goto error_nolock;
492 skb_dst_set(skb, dst);
493 x = dst->xfrm;
494 } while (x && !(x->outer_mode.flags & XFRM_MODE_FLAG_TUNNEL));
496 return 0;
498 error:
499 spin_unlock_bh(&x->lock);
500 error_nolock:
501 kfree_skb(skb);
502 out:
503 return err;
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)) {
511 nf_reset_ct(skb);
513 err = skb_dst(skb)->ops->local_out(net, skb->sk, skb);
514 if (unlikely(err != 1))
515 goto out;
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))
524 goto out;
527 if (err == -EINPROGRESS)
528 err = 0;
530 out:
531 return err;
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);
547 kfree_skb(skb);
548 if (IS_ERR(segs))
549 return PTR_ERR(segs);
550 if (segs == NULL)
551 return -EINVAL;
553 skb_list_walk_safe(segs, segs, nskb) {
554 int err;
556 skb_mark_not_on_list(segs);
557 err = xfrm_output2(net, sk, segs);
559 if (unlikely(err)) {
560 kfree_skb_list(nskb);
561 return err;
565 return 0;
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;
572 int err;
574 switch (x->outer_mode.family) {
575 case AF_INET:
576 memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
577 IPCB(skb)->flags |= IPSKB_XFRM_TRANSFORMED;
578 break;
579 case AF_INET6:
580 memset(IP6CB(skb), 0, sizeof(*IP6CB(skb)));
582 IP6CB(skb)->flags |= IP6SKB_XFRM_TRANSFORMED;
583 break;
586 secpath_reset(skb);
588 if (xfrm_dev_offload_ok(skb, x)) {
589 struct sec_path *sp;
591 sp = secpath_set(skb);
592 if (!sp) {
593 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
594 kfree_skb(skb);
595 return -ENOMEM;
597 skb->encapsulation = 1;
599 sp->olen++;
600 sp->xvec[sp->len++] = x;
601 xfrm_state_hold(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;
608 goto out;
611 if (x->xso.dev && x->xso.dev->features & NETIF_F_HW_ESP_TX_CSUM)
612 goto out;
613 } else {
614 if (skb_is_gso(skb))
615 return xfrm_output_gso(net, sk, skb);
618 if (skb->ip_summed == CHECKSUM_PARTIAL) {
619 err = skb_checksum_help(skb);
620 if (err) {
621 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
622 kfree_skb(skb);
623 return err;
627 out:
628 return xfrm_output2(net, sk, skb);
630 EXPORT_SYMBOL_GPL(xfrm_output);
632 static int xfrm4_tunnel_check_size(struct sk_buff *skb)
634 int mtu, ret = 0;
636 if (IPCB(skb)->flags & IPSKB_XFRM_TUNNEL_SIZE)
637 goto out;
639 if (!(ip_hdr(skb)->frag_off & htons(IP_DF)) || skb->ignore_df)
640 goto out;
642 mtu = dst_mtu(skb_dst(skb));
643 if ((!skb_is_gso(skb) && skb->len > mtu) ||
644 (skb_is_gso(skb) &&
645 !skb_gso_validate_network_len(skb, ip_skb_dst_mtu(skb->sk, skb)))) {
646 skb->protocol = htons(ETH_P_IP);
648 if (skb->sk)
649 xfrm_local_error(skb, mtu);
650 else
651 icmp_send(skb, ICMP_DEST_UNREACH,
652 ICMP_FRAG_NEEDED, htonl(mtu));
653 ret = -EMSGSIZE;
655 out:
656 return ret;
659 static int xfrm4_extract_output(struct xfrm_state *x, struct sk_buff *skb)
661 int err;
663 err = xfrm4_tunnel_check_size(skb);
664 if (err)
665 return err;
667 XFRM_MODE_SKB_CB(skb)->protocol = ip_hdr(skb)->protocol;
669 xfrm4_extract_header(skb);
670 return 0;
673 #if IS_ENABLED(CONFIG_IPV6)
674 static int xfrm6_tunnel_check_size(struct sk_buff *skb)
676 int mtu, ret = 0;
677 struct dst_entry *dst = skb_dst(skb);
679 if (skb->ignore_df)
680 goto out;
682 mtu = dst_mtu(dst);
683 if (mtu < IPV6_MIN_MTU)
684 mtu = IPV6_MIN_MTU;
686 if ((!skb_is_gso(skb) && skb->len > mtu) ||
687 (skb_is_gso(skb) &&
688 !skb_gso_validate_network_len(skb, ip6_skb_dst_mtu(skb)))) {
689 skb->dev = dst->dev;
690 skb->protocol = htons(ETH_P_IPV6);
692 if (xfrm6_local_dontfrag(skb->sk))
693 ipv6_stub->xfrm6_local_rxpmtu(skb, mtu);
694 else if (skb->sk)
695 xfrm_local_error(skb, mtu);
696 else
697 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
698 ret = -EMSGSIZE;
700 out:
701 return ret;
703 #endif
705 static int xfrm6_extract_output(struct xfrm_state *x, struct sk_buff *skb)
707 #if IS_ENABLED(CONFIG_IPV6)
708 int err;
710 err = xfrm6_tunnel_check_size(skb);
711 if (err)
712 return err;
714 XFRM_MODE_SKB_CB(skb)->protocol = ipv6_hdr(skb)->nexthdr;
716 xfrm6_extract_header(skb);
717 return 0;
718 #else
719 WARN_ON_ONCE(1);
720 return -EAFNOSUPPORT;
721 #endif
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));
731 else
732 inner_mode = &x->inner_mode;
734 if (inner_mode == NULL)
735 return -EAFNOSUPPORT;
737 switch (inner_mode->family) {
738 case AF_INET:
739 return xfrm4_extract_output(x, skb);
740 case AF_INET6:
741 return xfrm6_extract_output(x, skb);
744 return -EAFNOSUPPORT;
747 void xfrm_local_error(struct sk_buff *skb, int mtu)
749 unsigned int proto;
750 struct xfrm_state_afinfo *afinfo;
752 if (skb->protocol == htons(ETH_P_IP))
753 proto = AF_INET;
754 else if (skb->protocol == htons(ETH_P_IPV6) &&
755 skb->sk->sk_family == AF_INET6)
756 proto = AF_INET6;
757 else
758 return;
760 afinfo = xfrm_state_get_afinfo(proto);
761 if (afinfo) {
762 afinfo->local_error(skb, mtu);
763 rcu_read_unlock();
766 EXPORT_SYMBOL_GPL(xfrm_local_error);