]> git.karo-electronics.de Git - mv-sheeva.git/blob - net/ipv6/xfrm6_policy.c
[XFRM] STATE: Add a hook to obtain local/remote outbound address.
[mv-sheeva.git] / net / ipv6 / xfrm6_policy.c
1 /*
2  * xfrm6_policy.c: based on xfrm4_policy.c
3  *
4  * Authors:
5  *      Mitsuru KANDA @USAGI
6  *      Kazunori MIYAZAWA @USAGI
7  *      Kunihiro Ishiguro <kunihiro@ipinfusion.com>
8  *              IPv6 support
9  *      YOSHIFUJI Hideaki
10  *              Split up af-specific portion
11  * 
12  */
13
14 #include <linux/compiler.h>
15 #include <linux/netdevice.h>
16 #include <net/addrconf.h>
17 #include <net/xfrm.h>
18 #include <net/ip.h>
19 #include <net/ipv6.h>
20 #include <net/ip6_route.h>
21
22 static struct dst_ops xfrm6_dst_ops;
23 static struct xfrm_policy_afinfo xfrm6_policy_afinfo;
24
25 static int xfrm6_dst_lookup(struct xfrm_dst **dst, struct flowi *fl)
26 {
27         int err = 0;
28         *dst = (struct xfrm_dst*)ip6_route_output(NULL, fl);
29         if (!*dst)
30                 err = -ENETUNREACH;
31         return err;
32 }
33
34 static struct dst_entry *
35 __xfrm6_find_bundle(struct flowi *fl, struct xfrm_policy *policy)
36 {
37         struct dst_entry *dst;
38
39         /* Still not clear if we should set fl->fl6_{src,dst}... */
40         read_lock_bh(&policy->lock);
41         for (dst = policy->bundles; dst; dst = dst->next) {
42                 struct xfrm_dst *xdst = (struct xfrm_dst*)dst;
43                 struct in6_addr fl_dst_prefix, fl_src_prefix;
44
45                 ipv6_addr_prefix(&fl_dst_prefix,
46                                  &fl->fl6_dst,
47                                  xdst->u.rt6.rt6i_dst.plen);
48                 ipv6_addr_prefix(&fl_src_prefix,
49                                  &fl->fl6_src,
50                                  xdst->u.rt6.rt6i_src.plen);
51                 if (ipv6_addr_equal(&xdst->u.rt6.rt6i_dst.addr, &fl_dst_prefix) &&
52                     ipv6_addr_equal(&xdst->u.rt6.rt6i_src.addr, &fl_src_prefix) &&
53                     xfrm_bundle_ok(xdst, fl, AF_INET6)) {
54                         dst_clone(dst);
55                         break;
56                 }
57         }
58         read_unlock_bh(&policy->lock);
59         return dst;
60 }
61
62 static inline struct in6_addr*
63 __xfrm6_bundle_addr_remote(struct xfrm_state *x, struct in6_addr *addr)
64 {
65         return (x->type->remote_addr) ?
66                 (struct in6_addr*)x->type->remote_addr(x, (xfrm_address_t *)addr) :
67                 (struct in6_addr*)&x->id.daddr;
68 }
69
70 static inline struct in6_addr*
71 __xfrm6_bundle_addr_local(struct xfrm_state *x, struct in6_addr *addr)
72 {
73         return (x->type->local_addr) ?
74                 (struct in6_addr*)x->type->local_addr(x, (xfrm_address_t *)addr) :
75                 (struct in6_addr*)&x->props.saddr;
76 }
77
78 /* Allocate chain of dst_entry's, attach known xfrm's, calculate
79  * all the metrics... Shortly, bundle a bundle.
80  */
81
82 static int
83 __xfrm6_bundle_create(struct xfrm_policy *policy, struct xfrm_state **xfrm, int nx,
84                       struct flowi *fl, struct dst_entry **dst_p)
85 {
86         struct dst_entry *dst, *dst_prev;
87         struct rt6_info *rt0 = (struct rt6_info*)(*dst_p);
88         struct rt6_info *rt  = rt0;
89         struct in6_addr *remote = &fl->fl6_dst;
90         struct in6_addr *local  = &fl->fl6_src;
91         struct flowi fl_tunnel = {
92                 .nl_u = {
93                         .ip6_u = {
94                                 .saddr = *local,
95                                 .daddr = *remote
96                         }
97                 }
98         };
99         int i;
100         int err = 0;
101         int header_len = 0;
102         int trailer_len = 0;
103
104         dst = dst_prev = NULL;
105         dst_hold(&rt->u.dst);
106
107         for (i = 0; i < nx; i++) {
108                 struct dst_entry *dst1 = dst_alloc(&xfrm6_dst_ops);
109                 struct xfrm_dst *xdst;
110                 int tunnel = 0;
111
112                 if (unlikely(dst1 == NULL)) {
113                         err = -ENOBUFS;
114                         dst_release(&rt->u.dst);
115                         goto error;
116                 }
117
118                 if (!dst)
119                         dst = dst1;
120                 else {
121                         dst_prev->child = dst1;
122                         dst1->flags |= DST_NOHASH;
123                         dst_clone(dst1);
124                 }
125
126                 xdst = (struct xfrm_dst *)dst1;
127                 xdst->route = &rt->u.dst;
128                 if (rt->rt6i_node)
129                         xdst->route_cookie = rt->rt6i_node->fn_sernum;
130
131                 dst1->next = dst_prev;
132                 dst_prev = dst1;
133                 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
134                         remote = __xfrm6_bundle_addr_remote(xfrm[i], remote);
135                         local  = __xfrm6_bundle_addr_local(xfrm[i], local);
136                         tunnel = 1;
137                 }
138                 header_len += xfrm[i]->props.header_len;
139                 trailer_len += xfrm[i]->props.trailer_len;
140
141                 if (tunnel) {
142                         ipv6_addr_copy(&fl_tunnel.fl6_dst, remote);
143                         ipv6_addr_copy(&fl_tunnel.fl6_src, local);
144                         err = xfrm_dst_lookup((struct xfrm_dst **) &rt,
145                                               &fl_tunnel, AF_INET6);
146                         if (err)
147                                 goto error;
148                 } else
149                         dst_hold(&rt->u.dst);
150         }
151
152         dst_prev->child = &rt->u.dst;
153         dst->path = &rt->u.dst;
154         if (rt->rt6i_node)
155                 ((struct xfrm_dst *)dst)->path_cookie = rt->rt6i_node->fn_sernum;
156
157         *dst_p = dst;
158         dst = dst_prev;
159
160         dst_prev = *dst_p;
161         i = 0;
162         for (; dst_prev != &rt->u.dst; dst_prev = dst_prev->child) {
163                 struct xfrm_dst *x = (struct xfrm_dst*)dst_prev;
164
165                 dst_prev->xfrm = xfrm[i++];
166                 dst_prev->dev = rt->u.dst.dev;
167                 if (rt->u.dst.dev)
168                         dev_hold(rt->u.dst.dev);
169                 dst_prev->obsolete      = -1;
170                 dst_prev->flags        |= DST_HOST;
171                 dst_prev->lastuse       = jiffies;
172                 dst_prev->header_len    = header_len;
173                 dst_prev->trailer_len   = trailer_len;
174                 memcpy(&dst_prev->metrics, &x->route->metrics, sizeof(dst_prev->metrics));
175
176                 /* Copy neighbour for reachability confirmation */
177                 dst_prev->neighbour     = neigh_clone(rt->u.dst.neighbour);
178                 dst_prev->input         = rt->u.dst.input;
179                 dst_prev->output        = xfrm6_output;
180                 /* Sheit... I remember I did this right. Apparently,
181                  * it was magically lost, so this code needs audit */
182                 x->u.rt6.rt6i_flags    = rt0->rt6i_flags&(RTCF_BROADCAST|RTCF_MULTICAST|RTCF_LOCAL);
183                 x->u.rt6.rt6i_metric   = rt0->rt6i_metric;
184                 x->u.rt6.rt6i_node     = rt0->rt6i_node;
185                 x->u.rt6.rt6i_gateway  = rt0->rt6i_gateway;
186                 memcpy(&x->u.rt6.rt6i_gateway, &rt0->rt6i_gateway, sizeof(x->u.rt6.rt6i_gateway)); 
187                 x->u.rt6.rt6i_dst      = rt0->rt6i_dst;
188                 x->u.rt6.rt6i_src      = rt0->rt6i_src; 
189                 x->u.rt6.rt6i_idev     = rt0->rt6i_idev;
190                 in6_dev_hold(rt0->rt6i_idev);
191                 header_len -= x->u.dst.xfrm->props.header_len;
192                 trailer_len -= x->u.dst.xfrm->props.trailer_len;
193         }
194
195         xfrm_init_pmtu(dst);
196         return 0;
197
198 error:
199         if (dst)
200                 dst_free(dst);
201         return err;
202 }
203
204 static inline void
205 _decode_session6(struct sk_buff *skb, struct flowi *fl)
206 {
207         u16 offset = skb->h.raw - skb->nh.raw;
208         struct ipv6hdr *hdr = skb->nh.ipv6h;
209         struct ipv6_opt_hdr *exthdr;
210         u8 nexthdr = skb->nh.raw[IP6CB(skb)->nhoff];
211
212         memset(fl, 0, sizeof(struct flowi));
213         ipv6_addr_copy(&fl->fl6_dst, &hdr->daddr);
214         ipv6_addr_copy(&fl->fl6_src, &hdr->saddr);
215
216         while (pskb_may_pull(skb, skb->nh.raw + offset + 1 - skb->data)) {
217                 exthdr = (struct ipv6_opt_hdr*)(skb->nh.raw + offset);
218
219                 switch (nexthdr) {
220                 case NEXTHDR_ROUTING:
221                 case NEXTHDR_HOP:
222                 case NEXTHDR_DEST:
223                         offset += ipv6_optlen(exthdr);
224                         nexthdr = exthdr->nexthdr;
225                         exthdr = (struct ipv6_opt_hdr*)(skb->nh.raw + offset);
226                         break;
227
228                 case IPPROTO_UDP:
229                 case IPPROTO_TCP:
230                 case IPPROTO_SCTP:
231                 case IPPROTO_DCCP:
232                         if (pskb_may_pull(skb, skb->nh.raw + offset + 4 - skb->data)) {
233                                 u16 *ports = (u16 *)exthdr;
234
235                                 fl->fl_ip_sport = ports[0];
236                                 fl->fl_ip_dport = ports[1];
237                         }
238                         fl->proto = nexthdr;
239                         return;
240
241                 case IPPROTO_ICMPV6:
242                         if (pskb_may_pull(skb, skb->nh.raw + offset + 2 - skb->data)) {
243                                 u8 *icmp = (u8 *)exthdr;
244
245                                 fl->fl_icmp_type = icmp[0];
246                                 fl->fl_icmp_code = icmp[1];
247                         }
248                         fl->proto = nexthdr;
249                         return;
250
251                 /* XXX Why are there these headers? */
252                 case IPPROTO_AH:
253                 case IPPROTO_ESP:
254                 case IPPROTO_COMP:
255                 default:
256                         fl->fl_ipsec_spi = 0;
257                         fl->proto = nexthdr;
258                         return;
259                 };
260         }
261 }
262
263 static inline int xfrm6_garbage_collect(void)
264 {
265         xfrm6_policy_afinfo.garbage_collect();
266         return (atomic_read(&xfrm6_dst_ops.entries) > xfrm6_dst_ops.gc_thresh*2);
267 }
268
269 static void xfrm6_update_pmtu(struct dst_entry *dst, u32 mtu)
270 {
271         struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
272         struct dst_entry *path = xdst->route;
273
274         path->ops->update_pmtu(path, mtu);
275 }
276
277 static void xfrm6_dst_destroy(struct dst_entry *dst)
278 {
279         struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
280
281         if (likely(xdst->u.rt6.rt6i_idev))
282                 in6_dev_put(xdst->u.rt6.rt6i_idev);
283         xfrm_dst_destroy(xdst);
284 }
285
286 static void xfrm6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
287                              int unregister)
288 {
289         struct xfrm_dst *xdst;
290
291         if (!unregister)
292                 return;
293
294         xdst = (struct xfrm_dst *)dst;
295         if (xdst->u.rt6.rt6i_idev->dev == dev) {
296                 struct inet6_dev *loopback_idev = in6_dev_get(&loopback_dev);
297                 BUG_ON(!loopback_idev);
298
299                 do {
300                         in6_dev_put(xdst->u.rt6.rt6i_idev);
301                         xdst->u.rt6.rt6i_idev = loopback_idev;
302                         in6_dev_hold(loopback_idev);
303                         xdst = (struct xfrm_dst *)xdst->u.dst.child;
304                 } while (xdst->u.dst.xfrm);
305
306                 __in6_dev_put(loopback_idev);
307         }
308
309         xfrm_dst_ifdown(dst, dev);
310 }
311
312 static struct dst_ops xfrm6_dst_ops = {
313         .family =               AF_INET6,
314         .protocol =             __constant_htons(ETH_P_IPV6),
315         .gc =                   xfrm6_garbage_collect,
316         .update_pmtu =          xfrm6_update_pmtu,
317         .destroy =              xfrm6_dst_destroy,
318         .ifdown =               xfrm6_dst_ifdown,
319         .gc_thresh =            1024,
320         .entry_size =           sizeof(struct xfrm_dst),
321 };
322
323 static struct xfrm_policy_afinfo xfrm6_policy_afinfo = {
324         .family =               AF_INET6,
325         .dst_ops =              &xfrm6_dst_ops,
326         .dst_lookup =           xfrm6_dst_lookup,
327         .find_bundle =          __xfrm6_find_bundle,
328         .bundle_create =        __xfrm6_bundle_create,
329         .decode_session =       _decode_session6,
330 };
331
332 static void __init xfrm6_policy_init(void)
333 {
334         xfrm_policy_register_afinfo(&xfrm6_policy_afinfo);
335 }
336
337 static void xfrm6_policy_fini(void)
338 {
339         xfrm_policy_unregister_afinfo(&xfrm6_policy_afinfo);
340 }
341
342 void __init xfrm6_init(void)
343 {
344         xfrm6_policy_init();
345         xfrm6_state_init();
346 }
347
348 void xfrm6_fini(void)
349 {
350         //xfrm6_input_fini();
351         xfrm6_policy_fini();
352         xfrm6_state_fini();
353 }