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