[I/OAT]: TCP recv offload to I/OAT
[linux-2.6] / 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/module.h>
13 #include <linux/string.h>
14 #include <linux/netfilter.h>
15 #include <linux/netfilter_ipv4.h>
16 #include <net/inet_ecn.h>
17 #include <net/ip.h>
18 #include <net/xfrm.h>
19
20 int xfrm4_rcv(struct sk_buff *skb)
21 {
22         return xfrm4_rcv_encap(skb, 0);
23 }
24
25 EXPORT_SYMBOL(xfrm4_rcv);
26
27 static inline void ipip_ecn_decapsulate(struct sk_buff *skb)
28 {
29         struct iphdr *outer_iph = skb->nh.iph;
30         struct iphdr *inner_iph = skb->h.ipiph;
31
32         if (INET_ECN_is_ce(outer_iph->tos))
33                 IP_ECN_set_ce(inner_iph);
34 }
35
36 static int xfrm4_parse_spi(struct sk_buff *skb, u8 nexthdr, u32 *spi, u32 *seq)
37 {
38         switch (nexthdr) {
39         case IPPROTO_IPIP:
40                 *spi = skb->nh.iph->saddr;
41                 *seq = 0;
42                 return 0;
43         }
44
45         return xfrm_parse_spi(skb, nexthdr, spi, seq);
46 }
47
48 #ifdef CONFIG_NETFILTER
49 static inline int xfrm4_rcv_encap_finish(struct sk_buff *skb)
50 {
51         struct iphdr *iph = skb->nh.iph;
52
53         if (skb->dst == NULL) {
54                 if (ip_route_input(skb, iph->daddr, iph->saddr, iph->tos,
55                                    skb->dev))
56                         goto drop;
57         }
58         return dst_input(skb);
59 drop:
60         kfree_skb(skb);
61         return NET_RX_DROP;
62 }
63 #endif
64
65 int xfrm4_rcv_encap(struct sk_buff *skb, __u16 encap_type)
66 {
67         int err;
68         u32 spi, seq;
69         struct xfrm_state *xfrm_vec[XFRM_MAX_DEPTH];
70         struct xfrm_state *x;
71         int xfrm_nr = 0;
72         int decaps = 0;
73
74         if ((err = xfrm4_parse_spi(skb, skb->nh.iph->protocol, &spi, &seq)) != 0)
75                 goto drop;
76
77         do {
78                 struct iphdr *iph = skb->nh.iph;
79
80                 if (xfrm_nr == XFRM_MAX_DEPTH)
81                         goto drop;
82
83                 x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, spi, iph->protocol, AF_INET);
84                 if (x == NULL)
85                         goto drop;
86
87                 spin_lock(&x->lock);
88                 if (unlikely(x->km.state != XFRM_STATE_VALID))
89                         goto drop_unlock;
90
91                 if ((x->encap ? x->encap->encap_type : 0) != encap_type)
92                         goto drop_unlock;
93
94                 if (x->props.replay_window && xfrm_replay_check(x, seq))
95                         goto drop_unlock;
96
97                 if (xfrm_state_check_expire(x))
98                         goto drop_unlock;
99
100                 if (x->type->input(x, skb))
101                         goto drop_unlock;
102
103                 /* only the first xfrm gets the encap type */
104                 encap_type = 0;
105
106                 if (x->props.replay_window)
107                         xfrm_replay_advance(x, seq);
108
109                 x->curlft.bytes += skb->len;
110                 x->curlft.packets++;
111
112                 spin_unlock(&x->lock);
113
114                 xfrm_vec[xfrm_nr++] = x;
115
116                 iph = skb->nh.iph;
117
118                 if (x->props.mode) {
119                         if (iph->protocol != IPPROTO_IPIP)
120                                 goto drop;
121                         if (!pskb_may_pull(skb, sizeof(struct iphdr)))
122                                 goto drop;
123                         if (skb_cloned(skb) &&
124                             pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
125                                 goto drop;
126                         if (x->props.flags & XFRM_STATE_DECAP_DSCP)
127                                 ipv4_copy_dscp(iph, skb->h.ipiph);
128                         if (!(x->props.flags & XFRM_STATE_NOECN))
129                                 ipip_ecn_decapsulate(skb);
130                         skb->mac.raw = memmove(skb->data - skb->mac_len,
131                                                skb->mac.raw, skb->mac_len);
132                         skb->nh.raw = skb->data;
133                         memset(&(IPCB(skb)->opt), 0, sizeof(struct ip_options));
134                         decaps = 1;
135                         break;
136                 }
137
138                 if ((err = xfrm_parse_spi(skb, skb->nh.iph->protocol, &spi, &seq)) < 0)
139                         goto drop;
140         } while (!err);
141
142         /* Allocate new secpath or COW existing one. */
143
144         if (!skb->sp || atomic_read(&skb->sp->refcnt) != 1) {
145                 struct sec_path *sp;
146                 sp = secpath_dup(skb->sp);
147                 if (!sp)
148                         goto drop;
149                 if (skb->sp)
150                         secpath_put(skb->sp);
151                 skb->sp = sp;
152         }
153         if (xfrm_nr + skb->sp->len > XFRM_MAX_DEPTH)
154                 goto drop;
155
156         memcpy(skb->sp->xvec + skb->sp->len, xfrm_vec,
157                xfrm_nr * sizeof(xfrm_vec[0]));
158         skb->sp->len += xfrm_nr;
159
160         nf_reset(skb);
161
162         if (decaps) {
163                 if (!(skb->dev->flags&IFF_LOOPBACK)) {
164                         dst_release(skb->dst);
165                         skb->dst = NULL;
166                 }
167                 netif_rx(skb);
168                 return 0;
169         } else {
170 #ifdef CONFIG_NETFILTER
171                 __skb_push(skb, skb->data - skb->nh.raw);
172                 skb->nh.iph->tot_len = htons(skb->len);
173                 ip_send_check(skb->nh.iph);
174
175                 NF_HOOK(PF_INET, NF_IP_PRE_ROUTING, skb, skb->dev, NULL,
176                         xfrm4_rcv_encap_finish);
177                 return 0;
178 #else
179                 return -skb->nh.iph->protocol;
180 #endif
181         }
182
183 drop_unlock:
184         spin_unlock(&x->lock);
185         xfrm_state_put(x);
186 drop:
187         while (--xfrm_nr >= 0)
188                 xfrm_state_put(xfrm_vec[xfrm_nr]);
189
190         kfree_skb(skb);
191         return 0;
192 }