Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * xfrm4_input.c
0004  *
0005  * Changes:
0006  *  YOSHIFUJI Hideaki @USAGI
0007  *      Split up af-specific portion
0008  *  Derek Atkins <derek@ihtfp.com>
0009  *      Add Encapsulation support
0010  *
0011  */
0012 
0013 #include <linux/slab.h>
0014 #include <linux/module.h>
0015 #include <linux/string.h>
0016 #include <linux/netfilter.h>
0017 #include <linux/netfilter_ipv4.h>
0018 #include <net/ip.h>
0019 #include <net/xfrm.h>
0020 
0021 static int xfrm4_rcv_encap_finish2(struct net *net, struct sock *sk,
0022                    struct sk_buff *skb)
0023 {
0024     return dst_input(skb);
0025 }
0026 
0027 static inline int xfrm4_rcv_encap_finish(struct net *net, struct sock *sk,
0028                      struct sk_buff *skb)
0029 {
0030     if (!skb_dst(skb)) {
0031         const struct iphdr *iph = ip_hdr(skb);
0032 
0033         if (ip_route_input_noref(skb, iph->daddr, iph->saddr,
0034                      iph->tos, skb->dev))
0035             goto drop;
0036     }
0037 
0038     if (xfrm_trans_queue(skb, xfrm4_rcv_encap_finish2))
0039         goto drop;
0040 
0041     return 0;
0042 drop:
0043     kfree_skb(skb);
0044     return NET_RX_DROP;
0045 }
0046 
0047 int xfrm4_transport_finish(struct sk_buff *skb, int async)
0048 {
0049     struct xfrm_offload *xo = xfrm_offload(skb);
0050     struct iphdr *iph = ip_hdr(skb);
0051 
0052     iph->protocol = XFRM_MODE_SKB_CB(skb)->protocol;
0053 
0054 #ifndef CONFIG_NETFILTER
0055     if (!async)
0056         return -iph->protocol;
0057 #endif
0058 
0059     __skb_push(skb, skb->data - skb_network_header(skb));
0060     iph->tot_len = htons(skb->len);
0061     ip_send_check(iph);
0062 
0063     if (xo && (xo->flags & XFRM_GRO)) {
0064         skb_mac_header_rebuild(skb);
0065         skb_reset_transport_header(skb);
0066         return 0;
0067     }
0068 
0069     NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING,
0070         dev_net(skb->dev), NULL, skb, skb->dev, NULL,
0071         xfrm4_rcv_encap_finish);
0072     return 0;
0073 }
0074 
0075 /* If it's a keepalive packet, then just eat it.
0076  * If it's an encapsulated packet, then pass it to the
0077  * IPsec xfrm input.
0078  * Returns 0 if skb passed to xfrm or was dropped.
0079  * Returns >0 if skb should be passed to UDP.
0080  * Returns <0 if skb should be resubmitted (-ret is protocol)
0081  */
0082 int xfrm4_udp_encap_rcv(struct sock *sk, struct sk_buff *skb)
0083 {
0084     struct udp_sock *up = udp_sk(sk);
0085     struct udphdr *uh;
0086     struct iphdr *iph;
0087     int iphlen, len;
0088 
0089     __u8 *udpdata;
0090     __be32 *udpdata32;
0091     __u16 encap_type = up->encap_type;
0092 
0093     /* if this is not encapsulated socket, then just return now */
0094     if (!encap_type)
0095         return 1;
0096 
0097     /* If this is a paged skb, make sure we pull up
0098      * whatever data we need to look at. */
0099     len = skb->len - sizeof(struct udphdr);
0100     if (!pskb_may_pull(skb, sizeof(struct udphdr) + min(len, 8)))
0101         return 1;
0102 
0103     /* Now we can get the pointers */
0104     uh = udp_hdr(skb);
0105     udpdata = (__u8 *)uh + sizeof(struct udphdr);
0106     udpdata32 = (__be32 *)udpdata;
0107 
0108     switch (encap_type) {
0109     default:
0110     case UDP_ENCAP_ESPINUDP:
0111         /* Check if this is a keepalive packet.  If so, eat it. */
0112         if (len == 1 && udpdata[0] == 0xff) {
0113             goto drop;
0114         } else if (len > sizeof(struct ip_esp_hdr) && udpdata32[0] != 0) {
0115             /* ESP Packet without Non-ESP header */
0116             len = sizeof(struct udphdr);
0117         } else
0118             /* Must be an IKE packet.. pass it through */
0119             return 1;
0120         break;
0121     case UDP_ENCAP_ESPINUDP_NON_IKE:
0122         /* Check if this is a keepalive packet.  If so, eat it. */
0123         if (len == 1 && udpdata[0] == 0xff) {
0124             goto drop;
0125         } else if (len > 2 * sizeof(u32) + sizeof(struct ip_esp_hdr) &&
0126                udpdata32[0] == 0 && udpdata32[1] == 0) {
0127 
0128             /* ESP Packet with Non-IKE marker */
0129             len = sizeof(struct udphdr) + 2 * sizeof(u32);
0130         } else
0131             /* Must be an IKE packet.. pass it through */
0132             return 1;
0133         break;
0134     }
0135 
0136     /* At this point we are sure that this is an ESPinUDP packet,
0137      * so we need to remove 'len' bytes from the packet (the UDP
0138      * header and optional ESP marker bytes) and then modify the
0139      * protocol to ESP, and then call into the transform receiver.
0140      */
0141     if (skb_unclone(skb, GFP_ATOMIC))
0142         goto drop;
0143 
0144     /* Now we can update and verify the packet length... */
0145     iph = ip_hdr(skb);
0146     iphlen = iph->ihl << 2;
0147     iph->tot_len = htons(ntohs(iph->tot_len) - len);
0148     if (skb->len < iphlen + len) {
0149         /* packet is too small!?! */
0150         goto drop;
0151     }
0152 
0153     /* pull the data buffer up to the ESP header and set the
0154      * transport header to point to ESP.  Keep UDP on the stack
0155      * for later.
0156      */
0157     __skb_pull(skb, len);
0158     skb_reset_transport_header(skb);
0159 
0160     /* process ESP */
0161     return xfrm4_rcv_encap(skb, IPPROTO_ESP, 0, encap_type);
0162 
0163 drop:
0164     kfree_skb(skb);
0165     return 0;
0166 }
0167 
0168 int xfrm4_rcv(struct sk_buff *skb)
0169 {
0170     return xfrm4_rcv_spi(skb, ip_hdr(skb)->protocol, 0);
0171 }
0172 EXPORT_SYMBOL(xfrm4_rcv);