11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * xfrm4_input.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Changes: 51da177e4SLinus Torvalds * YOSHIFUJI Hideaki @USAGI 61da177e4SLinus Torvalds * Split up af-specific portion 71da177e4SLinus Torvalds * Derek Atkins <derek@ihtfp.com> 81da177e4SLinus Torvalds * Add Encapsulation support 91da177e4SLinus Torvalds * 101da177e4SLinus Torvalds */ 111da177e4SLinus Torvalds 125a0e3ad6STejun Heo #include <linux/slab.h> 131da177e4SLinus Torvalds #include <linux/module.h> 141da177e4SLinus Torvalds #include <linux/string.h> 15b05e1066SPatrick McHardy #include <linux/netfilter.h> 16b05e1066SPatrick McHardy #include <linux/netfilter_ipv4.h> 171da177e4SLinus Torvalds #include <net/ip.h> 181da177e4SLinus Torvalds #include <net/xfrm.h> 191da177e4SLinus Torvalds 20227620e2SHerbert Xu int xfrm4_extract_input(struct xfrm_state *x, struct sk_buff *skb) 21227620e2SHerbert Xu { 22227620e2SHerbert Xu return xfrm4_extract_header(skb); 23227620e2SHerbert Xu } 24227620e2SHerbert Xu 250c4b51f0SEric W. Biederman static inline int xfrm4_rcv_encap_finish(struct net *net, struct sock *sk, 260c4b51f0SEric W. Biederman struct sk_buff *skb) 27b05e1066SPatrick McHardy { 2851456b29SIan Morris if (!skb_dst(skb)) { 29eddc9ec5SArnaldo Carvalho de Melo const struct iphdr *iph = ip_hdr(skb); 30eddc9ec5SArnaldo Carvalho de Melo 31c6cffba4SDavid S. Miller if (ip_route_input_noref(skb, iph->daddr, iph->saddr, 32c10237e0SDavid S. Miller iph->tos, skb->dev)) 33b05e1066SPatrick McHardy goto drop; 34b05e1066SPatrick McHardy } 35b05e1066SPatrick McHardy return dst_input(skb); 36b05e1066SPatrick McHardy drop: 37b05e1066SPatrick McHardy kfree_skb(skb); 38b05e1066SPatrick McHardy return NET_RX_DROP; 39b05e1066SPatrick McHardy } 40b05e1066SPatrick McHardy 41716062fdSHerbert Xu int xfrm4_transport_finish(struct sk_buff *skb, int async) 42716062fdSHerbert Xu { 43*7785bba2SSteffen Klassert struct xfrm_offload *xo = xfrm_offload(skb); 4460d5fcfbSHerbert Xu struct iphdr *iph = ip_hdr(skb); 4560d5fcfbSHerbert Xu 4660d5fcfbSHerbert Xu iph->protocol = XFRM_MODE_SKB_CB(skb)->protocol; 4760d5fcfbSHerbert Xu 480883ae0eSHerbert Xu #ifndef CONFIG_NETFILTER 490883ae0eSHerbert Xu if (!async) 500883ae0eSHerbert Xu return -iph->protocol; 510883ae0eSHerbert Xu #endif 520883ae0eSHerbert Xu 53d56f90a7SArnaldo Carvalho de Melo __skb_push(skb, skb->data - skb_network_header(skb)); 5460d5fcfbSHerbert Xu iph->tot_len = htons(skb->len); 5560d5fcfbSHerbert Xu ip_send_check(iph); 56b05e1066SPatrick McHardy 57*7785bba2SSteffen Klassert if (xo && (xo->flags & XFRM_GRO)) { 58*7785bba2SSteffen Klassert skb_mac_header_rebuild(skb); 59*7785bba2SSteffen Klassert return 0; 60*7785bba2SSteffen Klassert } 61*7785bba2SSteffen Klassert 6229a26a56SEric W. Biederman NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING, 6329a26a56SEric W. Biederman dev_net(skb->dev), NULL, skb, skb->dev, NULL, 64b05e1066SPatrick McHardy xfrm4_rcv_encap_finish); 65b05e1066SPatrick McHardy return 0; 661da177e4SLinus Torvalds } 671da177e4SLinus Torvalds 68067b207bSJames Chapman /* If it's a keepalive packet, then just eat it. 69067b207bSJames Chapman * If it's an encapsulated packet, then pass it to the 70067b207bSJames Chapman * IPsec xfrm input. 71067b207bSJames Chapman * Returns 0 if skb passed to xfrm or was dropped. 72067b207bSJames Chapman * Returns >0 if skb should be passed to UDP. 73067b207bSJames Chapman * Returns <0 if skb should be resubmitted (-ret is protocol) 74067b207bSJames Chapman */ 75067b207bSJames Chapman int xfrm4_udp_encap_rcv(struct sock *sk, struct sk_buff *skb) 76067b207bSJames Chapman { 77067b207bSJames Chapman struct udp_sock *up = udp_sk(sk); 78067b207bSJames Chapman struct udphdr *uh; 79067b207bSJames Chapman struct iphdr *iph; 80067b207bSJames Chapman int iphlen, len; 81067b207bSJames Chapman 82067b207bSJames Chapman __u8 *udpdata; 83067b207bSJames Chapman __be32 *udpdata32; 84067b207bSJames Chapman __u16 encap_type = up->encap_type; 85067b207bSJames Chapman 86067b207bSJames Chapman /* if this is not encapsulated socket, then just return now */ 87067b207bSJames Chapman if (!encap_type) 88067b207bSJames Chapman return 1; 89067b207bSJames Chapman 90067b207bSJames Chapman /* If this is a paged skb, make sure we pull up 91067b207bSJames Chapman * whatever data we need to look at. */ 92067b207bSJames Chapman len = skb->len - sizeof(struct udphdr); 93067b207bSJames Chapman if (!pskb_may_pull(skb, sizeof(struct udphdr) + min(len, 8))) 94067b207bSJames Chapman return 1; 95067b207bSJames Chapman 96067b207bSJames Chapman /* Now we can get the pointers */ 97067b207bSJames Chapman uh = udp_hdr(skb); 98067b207bSJames Chapman udpdata = (__u8 *)uh + sizeof(struct udphdr); 99067b207bSJames Chapman udpdata32 = (__be32 *)udpdata; 100067b207bSJames Chapman 101067b207bSJames Chapman switch (encap_type) { 102067b207bSJames Chapman default: 103067b207bSJames Chapman case UDP_ENCAP_ESPINUDP: 104067b207bSJames Chapman /* Check if this is a keepalive packet. If so, eat it. */ 105067b207bSJames Chapman if (len == 1 && udpdata[0] == 0xff) { 106067b207bSJames Chapman goto drop; 107067b207bSJames Chapman } else if (len > sizeof(struct ip_esp_hdr) && udpdata32[0] != 0) { 108067b207bSJames Chapman /* ESP Packet without Non-ESP header */ 109067b207bSJames Chapman len = sizeof(struct udphdr); 110067b207bSJames Chapman } else 111067b207bSJames Chapman /* Must be an IKE packet.. pass it through */ 112067b207bSJames Chapman return 1; 113067b207bSJames Chapman break; 114067b207bSJames Chapman case UDP_ENCAP_ESPINUDP_NON_IKE: 115067b207bSJames Chapman /* Check if this is a keepalive packet. If so, eat it. */ 116067b207bSJames Chapman if (len == 1 && udpdata[0] == 0xff) { 117067b207bSJames Chapman goto drop; 118067b207bSJames Chapman } else if (len > 2 * sizeof(u32) + sizeof(struct ip_esp_hdr) && 119067b207bSJames Chapman udpdata32[0] == 0 && udpdata32[1] == 0) { 120067b207bSJames Chapman 121067b207bSJames Chapman /* ESP Packet with Non-IKE marker */ 122067b207bSJames Chapman len = sizeof(struct udphdr) + 2 * sizeof(u32); 123067b207bSJames Chapman } else 124067b207bSJames Chapman /* Must be an IKE packet.. pass it through */ 125067b207bSJames Chapman return 1; 126067b207bSJames Chapman break; 127067b207bSJames Chapman } 128067b207bSJames Chapman 129067b207bSJames Chapman /* At this point we are sure that this is an ESPinUDP packet, 130067b207bSJames Chapman * so we need to remove 'len' bytes from the packet (the UDP 131067b207bSJames Chapman * header and optional ESP marker bytes) and then modify the 132067b207bSJames Chapman * protocol to ESP, and then call into the transform receiver. 133067b207bSJames Chapman */ 13414bbd6a5SPravin B Shelar if (skb_unclone(skb, GFP_ATOMIC)) 135067b207bSJames Chapman goto drop; 136067b207bSJames Chapman 137067b207bSJames Chapman /* Now we can update and verify the packet length... */ 138067b207bSJames Chapman iph = ip_hdr(skb); 139067b207bSJames Chapman iphlen = iph->ihl << 2; 140067b207bSJames Chapman iph->tot_len = htons(ntohs(iph->tot_len) - len); 141067b207bSJames Chapman if (skb->len < iphlen + len) { 142067b207bSJames Chapman /* packet is too small!?! */ 143067b207bSJames Chapman goto drop; 144067b207bSJames Chapman } 145067b207bSJames Chapman 146067b207bSJames Chapman /* pull the data buffer up to the ESP header and set the 147067b207bSJames Chapman * transport header to point to ESP. Keep UDP on the stack 148067b207bSJames Chapman * for later. 149067b207bSJames Chapman */ 150067b207bSJames Chapman __skb_pull(skb, len); 151067b207bSJames Chapman skb_reset_transport_header(skb); 152067b207bSJames Chapman 153067b207bSJames Chapman /* process ESP */ 154f2712fd0SHerbert Xu return xfrm4_rcv_encap(skb, IPPROTO_ESP, 0, encap_type); 155067b207bSJames Chapman 156067b207bSJames Chapman drop: 157067b207bSJames Chapman kfree_skb(skb); 158067b207bSJames Chapman return 0; 159067b207bSJames Chapman } 160067b207bSJames Chapman 161067b207bSJames Chapman int xfrm4_rcv(struct sk_buff *skb) 162067b207bSJames Chapman { 163c4541b41SHerbert Xu return xfrm4_rcv_spi(skb, ip_hdr(skb)->protocol, 0); 164067b207bSJames Chapman } 165067b207bSJames Chapman EXPORT_SYMBOL(xfrm4_rcv); 166