Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * xfrm6_output.c - Common IPsec encapsulation code for IPv6.
0004  * Copyright (C) 2002 USAGI/WIDE Project
0005  * Copyright (c) 2004 Herbert Xu <herbert@gondor.apana.org.au>
0006  */
0007 
0008 #include <linux/if_ether.h>
0009 #include <linux/kernel.h>
0010 #include <linux/module.h>
0011 #include <linux/skbuff.h>
0012 #include <linux/icmpv6.h>
0013 #include <linux/netfilter_ipv6.h>
0014 #include <net/dst.h>
0015 #include <net/ipv6.h>
0016 #include <net/ip6_route.h>
0017 #include <net/xfrm.h>
0018 
0019 void xfrm6_local_rxpmtu(struct sk_buff *skb, u32 mtu)
0020 {
0021     struct flowi6 fl6;
0022     struct sock *sk = skb->sk;
0023 
0024     fl6.flowi6_oif = sk->sk_bound_dev_if;
0025     fl6.daddr = ipv6_hdr(skb)->daddr;
0026 
0027     ipv6_local_rxpmtu(sk, &fl6, mtu);
0028 }
0029 
0030 void xfrm6_local_error(struct sk_buff *skb, u32 mtu)
0031 {
0032     struct flowi6 fl6;
0033     const struct ipv6hdr *hdr;
0034     struct sock *sk = skb->sk;
0035 
0036     hdr = skb->encapsulation ? inner_ipv6_hdr(skb) : ipv6_hdr(skb);
0037     fl6.fl6_dport = inet_sk(sk)->inet_dport;
0038     fl6.daddr = hdr->daddr;
0039 
0040     ipv6_local_error(sk, EMSGSIZE, &fl6, mtu);
0041 }
0042 
0043 static int __xfrm6_output_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
0044 {
0045     return xfrm_output(sk, skb);
0046 }
0047 
0048 static int xfrm6_noneed_fragment(struct sk_buff *skb)
0049 {
0050     struct frag_hdr *fh;
0051     u8 prevhdr = ipv6_hdr(skb)->nexthdr;
0052 
0053     if (prevhdr != NEXTHDR_FRAGMENT)
0054         return 0;
0055     fh = (struct frag_hdr *)(skb->data + sizeof(struct ipv6hdr));
0056     if (fh->nexthdr == NEXTHDR_ESP || fh->nexthdr == NEXTHDR_AUTH)
0057         return 1;
0058     return 0;
0059 }
0060 
0061 static int __xfrm6_output(struct net *net, struct sock *sk, struct sk_buff *skb)
0062 {
0063     struct dst_entry *dst = skb_dst(skb);
0064     struct xfrm_state *x = dst->xfrm;
0065     unsigned int mtu;
0066     bool toobig;
0067 
0068 #ifdef CONFIG_NETFILTER
0069     if (!x) {
0070         IP6CB(skb)->flags |= IP6SKB_REROUTED;
0071         return dst_output(net, sk, skb);
0072     }
0073 #endif
0074 
0075     if (x->props.mode != XFRM_MODE_TUNNEL)
0076         goto skip_frag;
0077 
0078     if (skb->protocol == htons(ETH_P_IPV6))
0079         mtu = ip6_skb_dst_mtu(skb);
0080     else
0081         mtu = dst_mtu(skb_dst(skb));
0082 
0083     toobig = skb->len > mtu && !skb_is_gso(skb);
0084 
0085     if (toobig && xfrm6_local_dontfrag(skb->sk)) {
0086         xfrm6_local_rxpmtu(skb, mtu);
0087         kfree_skb(skb);
0088         return -EMSGSIZE;
0089     } else if (toobig && xfrm6_noneed_fragment(skb)) {
0090         skb->ignore_df = 1;
0091         goto skip_frag;
0092     } else if (!skb->ignore_df && toobig && skb->sk) {
0093         xfrm_local_error(skb, mtu);
0094         kfree_skb(skb);
0095         return -EMSGSIZE;
0096     }
0097 
0098     if (toobig || dst_allfrag(skb_dst(skb)))
0099         return ip6_fragment(net, sk, skb,
0100                     __xfrm6_output_finish);
0101 
0102 skip_frag:
0103     return xfrm_output(sk, skb);
0104 }
0105 
0106 int xfrm6_output(struct net *net, struct sock *sk, struct sk_buff *skb)
0107 {
0108     return NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING,
0109                 net, sk, skb,  skb->dev, skb_dst(skb)->dev,
0110                 __xfrm6_output,
0111                 !(IP6CB(skb)->flags & IP6SKB_REROUTED));
0112 }