]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - net/ipv4/xfrm4_policy.c
Merge branch 'release' of git://git.kernel.org/pub/scm/linux/kernel/git/aegl/linux-2.6
[linux-2.6-omap-h63xx.git] / net / ipv4 / xfrm4_policy.c
1 /* 
2  * xfrm4_policy.c
3  *
4  * Changes:
5  *      Kazunori MIYAZAWA @USAGI
6  *      YOSHIFUJI Hideaki @USAGI
7  *              Split up af-specific portion
8  *      
9  */
10
11 #include <linux/compiler.h>
12 #include <linux/config.h>
13 #include <linux/inetdevice.h>
14 #include <net/xfrm.h>
15 #include <net/ip.h>
16
17 static struct dst_ops xfrm4_dst_ops;
18 static struct xfrm_policy_afinfo xfrm4_policy_afinfo;
19
20 static struct xfrm_type_map xfrm4_type_map = { .lock = RW_LOCK_UNLOCKED };
21
22 static int xfrm4_dst_lookup(struct xfrm_dst **dst, struct flowi *fl)
23 {
24         return __ip_route_output_key((struct rtable**)dst, fl);
25 }
26
27 static struct dst_entry *
28 __xfrm4_find_bundle(struct flowi *fl, struct xfrm_policy *policy)
29 {
30         struct dst_entry *dst;
31
32         read_lock_bh(&policy->lock);
33         for (dst = policy->bundles; dst; dst = dst->next) {
34                 struct xfrm_dst *xdst = (struct xfrm_dst*)dst;
35                 if (xdst->u.rt.fl.oif == fl->oif &&     /*XXX*/
36                     xdst->u.rt.fl.fl4_dst == fl->fl4_dst &&
37                     xdst->u.rt.fl.fl4_src == fl->fl4_src &&
38                     xfrm_bundle_ok(xdst, fl, AF_INET)) {
39                         dst_clone(dst);
40                         break;
41                 }
42         }
43         read_unlock_bh(&policy->lock);
44         return dst;
45 }
46
47 /* Allocate chain of dst_entry's, attach known xfrm's, calculate
48  * all the metrics... Shortly, bundle a bundle.
49  */
50
51 static int
52 __xfrm4_bundle_create(struct xfrm_policy *policy, struct xfrm_state **xfrm, int nx,
53                       struct flowi *fl, struct dst_entry **dst_p)
54 {
55         struct dst_entry *dst, *dst_prev;
56         struct rtable *rt0 = (struct rtable*)(*dst_p);
57         struct rtable *rt = rt0;
58         u32 remote = fl->fl4_dst;
59         u32 local  = fl->fl4_src;
60         struct flowi fl_tunnel = {
61                 .nl_u = {
62                         .ip4_u = {
63                                 .saddr = local,
64                                 .daddr = remote
65                         }
66                 }
67         };
68         int i;
69         int err;
70         int header_len = 0;
71         int trailer_len = 0;
72
73         dst = dst_prev = NULL;
74         dst_hold(&rt->u.dst);
75
76         for (i = 0; i < nx; i++) {
77                 struct dst_entry *dst1 = dst_alloc(&xfrm4_dst_ops);
78                 struct xfrm_dst *xdst;
79                 int tunnel = 0;
80
81                 if (unlikely(dst1 == NULL)) {
82                         err = -ENOBUFS;
83                         dst_release(&rt->u.dst);
84                         goto error;
85                 }
86
87                 if (!dst)
88                         dst = dst1;
89                 else {
90                         dst_prev->child = dst1;
91                         dst1->flags |= DST_NOHASH;
92                         dst_clone(dst1);
93                 }
94
95                 xdst = (struct xfrm_dst *)dst1;
96                 xdst->route = &rt->u.dst;
97
98                 dst1->next = dst_prev;
99                 dst_prev = dst1;
100                 if (xfrm[i]->props.mode) {
101                         remote = xfrm[i]->id.daddr.a4;
102                         local  = xfrm[i]->props.saddr.a4;
103                         tunnel = 1;
104                 }
105                 header_len += xfrm[i]->props.header_len;
106                 trailer_len += xfrm[i]->props.trailer_len;
107
108                 if (tunnel) {
109                         fl_tunnel.fl4_src = local;
110                         fl_tunnel.fl4_dst = remote;
111                         err = xfrm_dst_lookup((struct xfrm_dst **)&rt,
112                                               &fl_tunnel, AF_INET);
113                         if (err)
114                                 goto error;
115                 } else
116                         dst_hold(&rt->u.dst);
117         }
118
119         dst_prev->child = &rt->u.dst;
120         dst->path = &rt->u.dst;
121
122         *dst_p = dst;
123         dst = dst_prev;
124
125         dst_prev = *dst_p;
126         i = 0;
127         for (; dst_prev != &rt->u.dst; dst_prev = dst_prev->child) {
128                 struct xfrm_dst *x = (struct xfrm_dst*)dst_prev;
129                 x->u.rt.fl = *fl;
130
131                 dst_prev->xfrm = xfrm[i++];
132                 dst_prev->dev = rt->u.dst.dev;
133                 if (rt->u.dst.dev)
134                         dev_hold(rt->u.dst.dev);
135                 dst_prev->obsolete      = -1;
136                 dst_prev->flags        |= DST_HOST;
137                 dst_prev->lastuse       = jiffies;
138                 dst_prev->header_len    = header_len;
139                 dst_prev->trailer_len   = trailer_len;
140                 memcpy(&dst_prev->metrics, &x->route->metrics, sizeof(dst_prev->metrics));
141
142                 /* Copy neighbout for reachability confirmation */
143                 dst_prev->neighbour     = neigh_clone(rt->u.dst.neighbour);
144                 dst_prev->input         = rt->u.dst.input;
145                 dst_prev->output        = xfrm4_output;
146                 if (rt->peer)
147                         atomic_inc(&rt->peer->refcnt);
148                 x->u.rt.peer = rt->peer;
149                 /* Sheit... I remember I did this right. Apparently,
150                  * it was magically lost, so this code needs audit */
151                 x->u.rt.rt_flags = rt0->rt_flags&(RTCF_BROADCAST|RTCF_MULTICAST|RTCF_LOCAL);
152                 x->u.rt.rt_type = rt->rt_type;
153                 x->u.rt.rt_src = rt0->rt_src;
154                 x->u.rt.rt_dst = rt0->rt_dst;
155                 x->u.rt.rt_gateway = rt->rt_gateway;
156                 x->u.rt.rt_spec_dst = rt0->rt_spec_dst;
157                 x->u.rt.idev = rt0->idev;
158                 in_dev_hold(rt0->idev);
159                 header_len -= x->u.dst.xfrm->props.header_len;
160                 trailer_len -= x->u.dst.xfrm->props.trailer_len;
161         }
162
163         xfrm_init_pmtu(dst);
164         return 0;
165
166 error:
167         if (dst)
168                 dst_free(dst);
169         return err;
170 }
171
172 static void
173 _decode_session4(struct sk_buff *skb, struct flowi *fl)
174 {
175         struct iphdr *iph = skb->nh.iph;
176         u8 *xprth = skb->nh.raw + iph->ihl*4;
177
178         memset(fl, 0, sizeof(struct flowi));
179         if (!(iph->frag_off & htons(IP_MF | IP_OFFSET))) {
180                 switch (iph->protocol) {
181                 case IPPROTO_UDP:
182                 case IPPROTO_TCP:
183                 case IPPROTO_SCTP:
184                 case IPPROTO_DCCP:
185                         if (pskb_may_pull(skb, xprth + 4 - skb->data)) {
186                                 u16 *ports = (u16 *)xprth;
187
188                                 fl->fl_ip_sport = ports[0];
189                                 fl->fl_ip_dport = ports[1];
190                         }
191                         break;
192
193                 case IPPROTO_ICMP:
194                         if (pskb_may_pull(skb, xprth + 2 - skb->data)) {
195                                 u8 *icmp = xprth;
196
197                                 fl->fl_icmp_type = icmp[0];
198                                 fl->fl_icmp_code = icmp[1];
199                         }
200                         break;
201
202                 case IPPROTO_ESP:
203                         if (pskb_may_pull(skb, xprth + 4 - skb->data)) {
204                                 u32 *ehdr = (u32 *)xprth;
205
206                                 fl->fl_ipsec_spi = ehdr[0];
207                         }
208                         break;
209
210                 case IPPROTO_AH:
211                         if (pskb_may_pull(skb, xprth + 8 - skb->data)) {
212                                 u32 *ah_hdr = (u32*)xprth;
213
214                                 fl->fl_ipsec_spi = ah_hdr[1];
215                         }
216                         break;
217
218                 case IPPROTO_COMP:
219                         if (pskb_may_pull(skb, xprth + 4 - skb->data)) {
220                                 u16 *ipcomp_hdr = (u16 *)xprth;
221
222                                 fl->fl_ipsec_spi = ntohl(ntohs(ipcomp_hdr[1]));
223                         }
224                         break;
225                 default:
226                         fl->fl_ipsec_spi = 0;
227                         break;
228                 };
229         }
230         fl->proto = iph->protocol;
231         fl->fl4_dst = iph->daddr;
232         fl->fl4_src = iph->saddr;
233 }
234
235 static inline int xfrm4_garbage_collect(void)
236 {
237         read_lock(&xfrm4_policy_afinfo.lock);
238         xfrm4_policy_afinfo.garbage_collect();
239         read_unlock(&xfrm4_policy_afinfo.lock);
240         return (atomic_read(&xfrm4_dst_ops.entries) > xfrm4_dst_ops.gc_thresh*2);
241 }
242
243 static void xfrm4_update_pmtu(struct dst_entry *dst, u32 mtu)
244 {
245         struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
246         struct dst_entry *path = xdst->route;
247
248         path->ops->update_pmtu(path, mtu);
249 }
250
251 static void xfrm4_dst_destroy(struct dst_entry *dst)
252 {
253         struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
254
255         if (likely(xdst->u.rt.idev))
256                 in_dev_put(xdst->u.rt.idev);
257         xfrm_dst_destroy(xdst);
258 }
259
260 static void xfrm4_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
261                              int unregister)
262 {
263         struct xfrm_dst *xdst;
264
265         if (!unregister)
266                 return;
267
268         xdst = (struct xfrm_dst *)dst;
269         if (xdst->u.rt.idev->dev == dev) {
270                 struct in_device *loopback_idev = in_dev_get(&loopback_dev);
271                 BUG_ON(!loopback_idev);
272
273                 do {
274                         in_dev_put(xdst->u.rt.idev);
275                         xdst->u.rt.idev = loopback_idev;
276                         in_dev_hold(loopback_idev);
277                         xdst = (struct xfrm_dst *)xdst->u.dst.child;
278                 } while (xdst->u.dst.xfrm);
279
280                 __in_dev_put(loopback_idev);
281         }
282
283         xfrm_dst_ifdown(dst, dev);
284 }
285
286 static struct dst_ops xfrm4_dst_ops = {
287         .family =               AF_INET,
288         .protocol =             __constant_htons(ETH_P_IP),
289         .gc =                   xfrm4_garbage_collect,
290         .update_pmtu =          xfrm4_update_pmtu,
291         .destroy =              xfrm4_dst_destroy,
292         .ifdown =               xfrm4_dst_ifdown,
293         .gc_thresh =            1024,
294         .entry_size =           sizeof(struct xfrm_dst),
295 };
296
297 static struct xfrm_policy_afinfo xfrm4_policy_afinfo = {
298         .family =               AF_INET,
299         .lock =                 RW_LOCK_UNLOCKED,
300         .type_map =             &xfrm4_type_map,
301         .dst_ops =              &xfrm4_dst_ops,
302         .dst_lookup =           xfrm4_dst_lookup,
303         .find_bundle =          __xfrm4_find_bundle,
304         .bundle_create =        __xfrm4_bundle_create,
305         .decode_session =       _decode_session4,
306 };
307
308 static void __init xfrm4_policy_init(void)
309 {
310         xfrm_policy_register_afinfo(&xfrm4_policy_afinfo);
311 }
312
313 static void __exit xfrm4_policy_fini(void)
314 {
315         xfrm_policy_unregister_afinfo(&xfrm4_policy_afinfo);
316 }
317
318 void __init xfrm4_init(void)
319 {
320         xfrm4_state_init();
321         xfrm4_policy_init();
322 }
323