xfrm: Reinject transport-mode packets through tasklet
[pandora-kernel.git] / net / ipv4 / xfrm4_input.c
1 /*
2  * xfrm4_input.c
3  *
4  * Changes:
5  *      YOSHIFUJI Hideaki @USAGI
6  *              Split up af-specific portion
7  *      Derek Atkins <derek@ihtfp.com>
8  *              Add Encapsulation support
9  *
10  */
11
12 #include <linux/slab.h>
13 #include <linux/module.h>
14 #include <linux/string.h>
15 #include <linux/netfilter.h>
16 #include <linux/netfilter_ipv4.h>
17 #include <net/ip.h>
18 #include <net/xfrm.h>
19
20 int xfrm4_extract_input(struct xfrm_state *x, struct sk_buff *skb)
21 {
22         return xfrm4_extract_header(skb);
23 }
24
25 static int xfrm4_rcv_encap_finish2(struct sk_buff *skb)
26 {
27         return dst_input(skb);
28 }
29
30 static inline int xfrm4_rcv_encap_finish(struct sk_buff *skb)
31 {
32         if (skb_dst(skb) == NULL) {
33                 const struct iphdr *iph = ip_hdr(skb);
34
35                 if (ip_route_input_noref(skb, iph->daddr, iph->saddr,
36                                          iph->tos, skb->dev))
37                         goto drop;
38         }
39
40         if (xfrm_trans_queue(skb, xfrm4_rcv_encap_finish2))
41                 goto drop;
42
43         return 0;
44 drop:
45         kfree_skb(skb);
46         return NET_RX_DROP;
47 }
48
49 int xfrm4_rcv_encap(struct sk_buff *skb, int nexthdr, __be32 spi,
50                     int encap_type)
51 {
52         XFRM_SPI_SKB_CB(skb)->family = AF_INET;
53         XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct iphdr, daddr);
54         return xfrm_input(skb, nexthdr, spi, encap_type);
55 }
56 EXPORT_SYMBOL(xfrm4_rcv_encap);
57
58 int xfrm4_transport_finish(struct sk_buff *skb, int async)
59 {
60         struct iphdr *iph = ip_hdr(skb);
61
62         iph->protocol = XFRM_MODE_SKB_CB(skb)->protocol;
63
64 #ifndef CONFIG_NETFILTER
65         if (!async)
66                 return -iph->protocol;
67 #endif
68
69         __skb_push(skb, skb->data - skb_network_header(skb));
70         iph->tot_len = htons(skb->len);
71         ip_send_check(iph);
72
73         NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING, skb, skb->dev, NULL,
74                 xfrm4_rcv_encap_finish);
75         return 0;
76 }
77
78 /* If it's a keepalive packet, then just eat it.
79  * If it's an encapsulated packet, then pass it to the
80  * IPsec xfrm input.
81  * Returns 0 if skb passed to xfrm or was dropped.
82  * Returns >0 if skb should be passed to UDP.
83  * Returns <0 if skb should be resubmitted (-ret is protocol)
84  */
85 int xfrm4_udp_encap_rcv(struct sock *sk, struct sk_buff *skb)
86 {
87         struct udp_sock *up = udp_sk(sk);
88         struct udphdr *uh;
89         struct iphdr *iph;
90         int iphlen, len;
91
92         __u8 *udpdata;
93         __be32 *udpdata32;
94         __u16 encap_type = up->encap_type;
95
96         /* if this is not encapsulated socket, then just return now */
97         if (!encap_type)
98                 return 1;
99
100         /* If this is a paged skb, make sure we pull up
101          * whatever data we need to look at. */
102         len = skb->len - sizeof(struct udphdr);
103         if (!pskb_may_pull(skb, sizeof(struct udphdr) + min(len, 8)))
104                 return 1;
105
106         /* Now we can get the pointers */
107         uh = udp_hdr(skb);
108         udpdata = (__u8 *)uh + sizeof(struct udphdr);
109         udpdata32 = (__be32 *)udpdata;
110
111         switch (encap_type) {
112         default:
113         case UDP_ENCAP_ESPINUDP:
114                 /* Check if this is a keepalive packet.  If so, eat it. */
115                 if (len == 1 && udpdata[0] == 0xff) {
116                         goto drop;
117                 } else if (len > sizeof(struct ip_esp_hdr) && udpdata32[0] != 0) {
118                         /* ESP Packet without Non-ESP header */
119                         len = sizeof(struct udphdr);
120                 } else
121                         /* Must be an IKE packet.. pass it through */
122                         return 1;
123                 break;
124         case UDP_ENCAP_ESPINUDP_NON_IKE:
125                 /* Check if this is a keepalive packet.  If so, eat it. */
126                 if (len == 1 && udpdata[0] == 0xff) {
127                         goto drop;
128                 } else if (len > 2 * sizeof(u32) + sizeof(struct ip_esp_hdr) &&
129                            udpdata32[0] == 0 && udpdata32[1] == 0) {
130
131                         /* ESP Packet with Non-IKE marker */
132                         len = sizeof(struct udphdr) + 2 * sizeof(u32);
133                 } else
134                         /* Must be an IKE packet.. pass it through */
135                         return 1;
136                 break;
137         }
138
139         /* At this point we are sure that this is an ESPinUDP packet,
140          * so we need to remove 'len' bytes from the packet (the UDP
141          * header and optional ESP marker bytes) and then modify the
142          * protocol to ESP, and then call into the transform receiver.
143          */
144         if (skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
145                 goto drop;
146
147         /* Now we can update and verify the packet length... */
148         iph = ip_hdr(skb);
149         iphlen = iph->ihl << 2;
150         iph->tot_len = htons(ntohs(iph->tot_len) - len);
151         if (skb->len < iphlen + len) {
152                 /* packet is too small!?! */
153                 goto drop;
154         }
155
156         /* pull the data buffer up to the ESP header and set the
157          * transport header to point to ESP.  Keep UDP on the stack
158          * for later.
159          */
160         __skb_pull(skb, len);
161         skb_reset_transport_header(skb);
162
163         /* process ESP */
164         return xfrm4_rcv_encap(skb, IPPROTO_ESP, 0, encap_type);
165
166 drop:
167         kfree_skb(skb);
168         return 0;
169 }
170
171 int xfrm4_rcv(struct sk_buff *skb)
172 {
173         return xfrm4_rcv_spi(skb, ip_hdr(skb)->protocol, 0);
174 }
175 EXPORT_SYMBOL(xfrm4_rcv);