1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * xfrm4_input.c 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Changes: 61da177e4SLinus Torvalds * YOSHIFUJI Hideaki @USAGI 71da177e4SLinus Torvalds * Split up af-specific portion 81da177e4SLinus Torvalds * Derek Atkins <derek@ihtfp.com> 91da177e4SLinus Torvalds * Add Encapsulation support 101da177e4SLinus Torvalds * 111da177e4SLinus Torvalds */ 121da177e4SLinus Torvalds 135a0e3ad6STejun Heo #include <linux/slab.h> 141da177e4SLinus Torvalds #include <linux/module.h> 151da177e4SLinus Torvalds #include <linux/string.h> 16b05e1066SPatrick McHardy #include <linux/netfilter.h> 17b05e1066SPatrick McHardy #include <linux/netfilter_ipv4.h> 181da177e4SLinus Torvalds #include <net/ip.h> 191da177e4SLinus Torvalds #include <net/xfrm.h> 201da177e4SLinus Torvalds 21acf568eeSHerbert Xu static int xfrm4_rcv_encap_finish2(struct net *net, struct sock *sk, 22acf568eeSHerbert Xu struct sk_buff *skb) 23acf568eeSHerbert Xu { 24acf568eeSHerbert Xu return dst_input(skb); 25acf568eeSHerbert Xu } 26acf568eeSHerbert Xu 270c4b51f0SEric W. Biederman static inline int xfrm4_rcv_encap_finish(struct net *net, struct sock *sk, 280c4b51f0SEric W. Biederman struct sk_buff *skb) 29b05e1066SPatrick McHardy { 3051456b29SIan Morris if (!skb_dst(skb)) { 31eddc9ec5SArnaldo Carvalho de Melo const struct iphdr *iph = ip_hdr(skb); 32eddc9ec5SArnaldo Carvalho de Melo 33c6cffba4SDavid S. Miller if (ip_route_input_noref(skb, iph->daddr, iph->saddr, 34c10237e0SDavid S. Miller iph->tos, skb->dev)) 35b05e1066SPatrick McHardy goto drop; 36b05e1066SPatrick McHardy } 37acf568eeSHerbert Xu 38acf568eeSHerbert Xu if (xfrm_trans_queue(skb, xfrm4_rcv_encap_finish2)) 39acf568eeSHerbert Xu goto drop; 40acf568eeSHerbert Xu 41acf568eeSHerbert Xu return 0; 42b05e1066SPatrick McHardy drop: 43b05e1066SPatrick McHardy kfree_skb(skb); 44b05e1066SPatrick McHardy return NET_RX_DROP; 45b05e1066SPatrick McHardy } 46b05e1066SPatrick McHardy 47716062fdSHerbert Xu int xfrm4_transport_finish(struct sk_buff *skb, int async) 48716062fdSHerbert Xu { 497785bba2SSteffen Klassert struct xfrm_offload *xo = xfrm_offload(skb); 5060d5fcfbSHerbert Xu struct iphdr *iph = ip_hdr(skb); 5160d5fcfbSHerbert Xu 5260d5fcfbSHerbert Xu iph->protocol = XFRM_MODE_SKB_CB(skb)->protocol; 5360d5fcfbSHerbert Xu 540883ae0eSHerbert Xu #ifndef CONFIG_NETFILTER 550883ae0eSHerbert Xu if (!async) 560883ae0eSHerbert Xu return -iph->protocol; 570883ae0eSHerbert Xu #endif 580883ae0eSHerbert Xu 59d56f90a7SArnaldo Carvalho de Melo __skb_push(skb, skb->data - skb_network_header(skb)); 6060d5fcfbSHerbert Xu iph->tot_len = htons(skb->len); 6160d5fcfbSHerbert Xu ip_send_check(iph); 62b05e1066SPatrick McHardy 637785bba2SSteffen Klassert if (xo && (xo->flags & XFRM_GRO)) { 647785bba2SSteffen Klassert skb_mac_header_rebuild(skb); 65bfc0698bSSowmini Varadhan skb_reset_transport_header(skb); 667785bba2SSteffen Klassert return 0; 677785bba2SSteffen Klassert } 687785bba2SSteffen Klassert 6929a26a56SEric W. Biederman NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING, 7029a26a56SEric W. Biederman dev_net(skb->dev), NULL, skb, skb->dev, NULL, 71b05e1066SPatrick McHardy xfrm4_rcv_encap_finish); 72b05e1066SPatrick McHardy return 0; 731da177e4SLinus Torvalds } 741da177e4SLinus Torvalds 75067b207bSJames Chapman /* If it's a keepalive packet, then just eat it. 76067b207bSJames Chapman * If it's an encapsulated packet, then pass it to the 77067b207bSJames Chapman * IPsec xfrm input. 78067b207bSJames Chapman * Returns 0 if skb passed to xfrm or was dropped. 79067b207bSJames Chapman * Returns >0 if skb should be passed to UDP. 80067b207bSJames Chapman * Returns <0 if skb should be resubmitted (-ret is protocol) 81067b207bSJames Chapman */ 82067b207bSJames Chapman int xfrm4_udp_encap_rcv(struct sock *sk, struct sk_buff *skb) 83067b207bSJames Chapman { 84067b207bSJames Chapman struct udp_sock *up = udp_sk(sk); 85067b207bSJames Chapman struct udphdr *uh; 86067b207bSJames Chapman struct iphdr *iph; 87067b207bSJames Chapman int iphlen, len; 88067b207bSJames Chapman 89067b207bSJames Chapman __u8 *udpdata; 90067b207bSJames Chapman __be32 *udpdata32; 91067b207bSJames Chapman __u16 encap_type = up->encap_type; 92067b207bSJames Chapman 93067b207bSJames Chapman /* if this is not encapsulated socket, then just return now */ 94067b207bSJames Chapman if (!encap_type) 95067b207bSJames Chapman return 1; 96067b207bSJames Chapman 97067b207bSJames Chapman /* If this is a paged skb, make sure we pull up 98067b207bSJames Chapman * whatever data we need to look at. */ 99067b207bSJames Chapman len = skb->len - sizeof(struct udphdr); 100067b207bSJames Chapman if (!pskb_may_pull(skb, sizeof(struct udphdr) + min(len, 8))) 101067b207bSJames Chapman return 1; 102067b207bSJames Chapman 103067b207bSJames Chapman /* Now we can get the pointers */ 104067b207bSJames Chapman uh = udp_hdr(skb); 105067b207bSJames Chapman udpdata = (__u8 *)uh + sizeof(struct udphdr); 106067b207bSJames Chapman udpdata32 = (__be32 *)udpdata; 107067b207bSJames Chapman 108067b207bSJames Chapman switch (encap_type) { 109067b207bSJames Chapman default: 110067b207bSJames Chapman case UDP_ENCAP_ESPINUDP: 111067b207bSJames Chapman /* Check if this is a keepalive packet. If so, eat it. */ 112067b207bSJames Chapman if (len == 1 && udpdata[0] == 0xff) { 113067b207bSJames Chapman goto drop; 114067b207bSJames Chapman } else if (len > sizeof(struct ip_esp_hdr) && udpdata32[0] != 0) { 115067b207bSJames Chapman /* ESP Packet without Non-ESP header */ 116067b207bSJames Chapman len = sizeof(struct udphdr); 117067b207bSJames Chapman } else 118067b207bSJames Chapman /* Must be an IKE packet.. pass it through */ 119067b207bSJames Chapman return 1; 120067b207bSJames Chapman break; 121067b207bSJames Chapman case UDP_ENCAP_ESPINUDP_NON_IKE: 122067b207bSJames Chapman /* Check if this is a keepalive packet. If so, eat it. */ 123067b207bSJames Chapman if (len == 1 && udpdata[0] == 0xff) { 124067b207bSJames Chapman goto drop; 125067b207bSJames Chapman } else if (len > 2 * sizeof(u32) + sizeof(struct ip_esp_hdr) && 126067b207bSJames Chapman udpdata32[0] == 0 && udpdata32[1] == 0) { 127067b207bSJames Chapman 128067b207bSJames Chapman /* ESP Packet with Non-IKE marker */ 129067b207bSJames Chapman len = sizeof(struct udphdr) + 2 * sizeof(u32); 130067b207bSJames Chapman } else 131067b207bSJames Chapman /* Must be an IKE packet.. pass it through */ 132067b207bSJames Chapman return 1; 133067b207bSJames Chapman break; 134067b207bSJames Chapman } 135067b207bSJames Chapman 136067b207bSJames Chapman /* At this point we are sure that this is an ESPinUDP packet, 137067b207bSJames Chapman * so we need to remove 'len' bytes from the packet (the UDP 138067b207bSJames Chapman * header and optional ESP marker bytes) and then modify the 139067b207bSJames Chapman * protocol to ESP, and then call into the transform receiver. 140067b207bSJames Chapman */ 14114bbd6a5SPravin B Shelar if (skb_unclone(skb, GFP_ATOMIC)) 142067b207bSJames Chapman goto drop; 143067b207bSJames Chapman 144067b207bSJames Chapman /* Now we can update and verify the packet length... */ 145067b207bSJames Chapman iph = ip_hdr(skb); 146067b207bSJames Chapman iphlen = iph->ihl << 2; 147067b207bSJames Chapman iph->tot_len = htons(ntohs(iph->tot_len) - len); 148067b207bSJames Chapman if (skb->len < iphlen + len) { 149067b207bSJames Chapman /* packet is too small!?! */ 150067b207bSJames Chapman goto drop; 151067b207bSJames Chapman } 152067b207bSJames Chapman 153067b207bSJames Chapman /* pull the data buffer up to the ESP header and set the 154067b207bSJames Chapman * transport header to point to ESP. Keep UDP on the stack 155067b207bSJames Chapman * for later. 156067b207bSJames Chapman */ 157067b207bSJames Chapman __skb_pull(skb, len); 158067b207bSJames Chapman skb_reset_transport_header(skb); 159067b207bSJames Chapman 160067b207bSJames Chapman /* process ESP */ 161f2712fd0SHerbert Xu return xfrm4_rcv_encap(skb, IPPROTO_ESP, 0, encap_type); 162067b207bSJames Chapman 163067b207bSJames Chapman drop: 164067b207bSJames Chapman kfree_skb(skb); 165067b207bSJames Chapman return 0; 166067b207bSJames Chapman } 167*1166a530SMaciej Żenczykowski EXPORT_SYMBOL(xfrm4_udp_encap_rcv); 168067b207bSJames Chapman 169067b207bSJames Chapman int xfrm4_rcv(struct sk_buff *skb) 170067b207bSJames Chapman { 171c4541b41SHerbert Xu return xfrm4_rcv_spi(skb, ip_hdr(skb)->protocol, 0); 172067b207bSJames Chapman } 173067b207bSJames Chapman EXPORT_SYMBOL(xfrm4_rcv); 174