ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.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/config.h>
15 #include <net/xfrm.h>
16 #include <net/ip.h>
17 #include <net/ipv6.h>
18 #include <net/ip6_route.h>
19
20 extern struct dst_ops xfrm6_dst_ops;
21 extern struct xfrm_policy_afinfo xfrm6_policy_afinfo;
22
23 static struct xfrm_type_map xfrm6_type_map = { .lock = RW_LOCK_UNLOCKED };
24
25 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 /* Check that the bundle accepts the flow and its components are
35  * still valid.
36  */
37
38 static int __xfrm6_bundle_ok(struct xfrm_dst *xdst, struct flowi *fl)
39 {
40         do {
41                 if (xdst->u.dst.ops != &xfrm6_dst_ops)
42                         return 1;
43
44                 if (!xfrm_selector_match(&xdst->u.dst.xfrm->sel, fl, AF_INET6))
45                         return 0;
46                 if (xdst->u.dst.xfrm->km.state != XFRM_STATE_VALID ||
47                     xdst->u.dst.path->obsolete > 0)
48                         return 0;
49                 xdst = (struct xfrm_dst*)xdst->u.dst.child;
50         } while (xdst);
51         return 0;
52 }
53
54 static struct dst_entry *
55 __xfrm6_find_bundle(struct flowi *fl, struct xfrm_policy *policy)
56 {
57         struct dst_entry *dst;
58
59         /* Still not clear if we should set fl->fl6_{src,dst}... */
60         read_lock_bh(&policy->lock);
61         for (dst = policy->bundles; dst; dst = dst->next) {
62                 struct xfrm_dst *xdst = (struct xfrm_dst*)dst;
63                 struct in6_addr fl_dst_prefix, fl_src_prefix;
64
65                 ipv6_addr_prefix(&fl_dst_prefix,
66                                  &fl->fl6_dst,
67                                  xdst->u.rt6.rt6i_dst.plen);
68                 ipv6_addr_prefix(&fl_src_prefix,
69                                  &fl->fl6_src,
70                                  xdst->u.rt6.rt6i_src.plen);
71                 if (!ipv6_addr_cmp(&xdst->u.rt6.rt6i_dst.addr, &fl_dst_prefix) &&
72                     !ipv6_addr_cmp(&xdst->u.rt6.rt6i_src.addr, &fl_src_prefix) &&
73                     __xfrm6_bundle_ok(xdst, fl)) {
74                         dst_clone(dst);
75                         break;
76                 }
77         }
78         read_unlock_bh(&policy->lock);
79         return dst;
80 }
81
82 /* Allocate chain of dst_entry's, attach known xfrm's, calculate
83  * all the metrics... Shortly, bundle a bundle.
84  */
85
86 static int
87 __xfrm6_bundle_create(struct xfrm_policy *policy, struct xfrm_state **xfrm, int nx,
88                       struct flowi *fl, struct dst_entry **dst_p)
89 {
90         struct dst_entry *dst, *dst_prev;
91         struct rt6_info *rt0 = (struct rt6_info*)(*dst_p);
92         struct rt6_info *rt  = rt0;
93         struct in6_addr *remote = &fl->fl6_dst;
94         struct in6_addr *local  = &fl->fl6_src;
95         int i;
96         int err = 0;
97         int header_len = 0;
98         int trailer_len = 0;
99
100         dst = dst_prev = NULL;
101
102         for (i = 0; i < nx; i++) {
103                 struct dst_entry *dst1 = dst_alloc(&xfrm6_dst_ops);
104
105                 if (unlikely(dst1 == NULL)) {
106                         err = -ENOBUFS;
107                         goto error;
108                 }
109
110                 dst1->xfrm = xfrm[i];
111                 if (!dst)
112                         dst = dst1;
113                 else {
114                         dst_prev->child = dst1;
115                         dst1->flags |= DST_NOHASH;
116                         dst_clone(dst1);
117                 }
118                 dst_prev = dst1;
119                 if (xfrm[i]->props.mode) {
120                         remote = (struct in6_addr*)&xfrm[i]->id.daddr;
121                         local  = (struct in6_addr*)&xfrm[i]->props.saddr;
122                 }
123                 header_len += xfrm[i]->props.header_len;
124                 trailer_len += xfrm[i]->props.trailer_len;
125         }
126
127         if (ipv6_addr_cmp(remote, &fl->fl6_dst)) {
128                 struct flowi fl_tunnel;
129
130                 memset(&fl_tunnel, 0, sizeof(fl_tunnel));
131                 ipv6_addr_copy(&fl_tunnel.fl6_dst, remote);
132                 ipv6_addr_copy(&fl_tunnel.fl6_src, local);
133
134                 err = xfrm_dst_lookup((struct xfrm_dst **) &rt,
135                                       &fl_tunnel, AF_INET6);
136                 if (err)
137                         goto error;
138         } else {
139                 dst_hold(&rt->u.dst);
140         }
141         dst_prev->child = &rt->u.dst;
142         for (dst_prev = dst; dst_prev != &rt->u.dst; dst_prev = dst_prev->child) {
143                 struct xfrm_dst *x = (struct xfrm_dst*)dst_prev;
144
145                 dst_prev->dev = rt->u.dst.dev;
146                 if (rt->u.dst.dev)
147                         dev_hold(rt->u.dst.dev);
148                 dst_prev->obsolete      = -1;
149                 dst_prev->flags        |= DST_HOST;
150                 dst_prev->lastuse       = jiffies;
151                 dst_prev->header_len    = header_len;
152                 dst_prev->trailer_len   = trailer_len;
153                 memcpy(&dst_prev->metrics, &rt->u.dst.metrics, sizeof(dst_prev->metrics));
154                 dst_prev->path          = &rt->u.dst;
155
156                 /* Copy neighbour for reachability confirmation */
157                 dst_prev->neighbour     = neigh_clone(rt->u.dst.neighbour);
158                 dst_prev->input         = rt->u.dst.input;
159                 dst_prev->output        = dst_prev->xfrm->type->output;
160                 /* Sheit... I remember I did this right. Apparently,
161                  * it was magically lost, so this code needs audit */
162                 x->u.rt6.rt6i_flags    = rt0->rt6i_flags&(RTCF_BROADCAST|RTCF_MULTICAST|RTCF_LOCAL);
163                 x->u.rt6.rt6i_metric   = rt0->rt6i_metric;
164                 x->u.rt6.rt6i_node     = rt0->rt6i_node;
165                 x->u.rt6.rt6i_gateway  = rt0->rt6i_gateway;
166                 memcpy(&x->u.rt6.rt6i_gateway, &rt0->rt6i_gateway, sizeof(x->u.rt6.rt6i_gateway)); 
167                 x->u.rt6.rt6i_dst      = rt0->rt6i_dst;
168                 x->u.rt6.rt6i_src      = rt0->rt6i_src; 
169                 header_len -= x->u.dst.xfrm->props.header_len;
170                 trailer_len -= x->u.dst.xfrm->props.trailer_len;
171         }
172         *dst_p = dst;
173         return 0;
174
175 error:
176         if (dst)
177                 dst_free(dst);
178         return err;
179 }
180
181 static inline void
182 _decode_session6(struct sk_buff *skb, struct flowi *fl)
183 {
184         u16 offset = sizeof(struct ipv6hdr);
185         struct ipv6hdr *hdr = skb->nh.ipv6h;
186         struct ipv6_opt_hdr *exthdr = (struct ipv6_opt_hdr*)(skb->nh.raw + offset);
187         u8 nexthdr = skb->nh.ipv6h->nexthdr;
188
189         memset(fl, 0, sizeof(struct flowi));
190         ipv6_addr_copy(&fl->fl6_dst, &hdr->daddr);
191         ipv6_addr_copy(&fl->fl6_src, &hdr->saddr);
192
193         while (pskb_may_pull(skb, skb->nh.raw + offset + 1 - skb->data)) {
194                 switch (nexthdr) {
195                 case NEXTHDR_ROUTING:
196                 case NEXTHDR_HOP:
197                 case NEXTHDR_DEST:
198                         offset += ipv6_optlen(exthdr);
199                         nexthdr = exthdr->nexthdr;
200                         exthdr = (struct ipv6_opt_hdr*)(skb->nh.raw + offset);
201                         break;
202
203                 case IPPROTO_UDP:
204                 case IPPROTO_TCP:
205                 case IPPROTO_SCTP:
206                         if (pskb_may_pull(skb, skb->nh.raw + offset + 4 - skb->data)) {
207                                 u16 *ports = (u16 *)exthdr;
208
209                                 fl->fl_ip_sport = ports[0];
210                                 fl->fl_ip_dport = ports[1];
211                         }
212                         fl->proto = nexthdr;
213                         return;
214
215                 /* XXX Why are there these headers? */
216                 case IPPROTO_AH:
217                 case IPPROTO_ESP:
218                 case IPPROTO_COMP:
219                 default:
220                         fl->fl_ipsec_spi = 0;
221                         fl->proto = nexthdr;
222                         return;
223                 };
224         }
225 }
226
227 static inline int xfrm6_garbage_collect(void)
228 {
229         read_lock(&xfrm6_policy_afinfo.lock);
230         xfrm6_policy_afinfo.garbage_collect();
231         read_unlock(&xfrm6_policy_afinfo.lock);
232         return (atomic_read(&xfrm6_dst_ops.entries) > xfrm6_dst_ops.gc_thresh*2);
233 }
234
235 static void xfrm6_update_pmtu(struct dst_entry *dst, u32 mtu)
236 {
237         struct dst_entry *path = dst->path;
238
239         if (mtu >= 1280 && mtu < dst_pmtu(dst))
240                 return;
241
242         path->ops->update_pmtu(path, mtu);
243 }
244
245 struct dst_ops xfrm6_dst_ops = {
246         .family =               AF_INET6,
247         .protocol =             __constant_htons(ETH_P_IPV6),
248         .gc =                   xfrm6_garbage_collect,
249         .update_pmtu =          xfrm6_update_pmtu,
250         .gc_thresh =            1024,
251         .entry_size =           sizeof(struct xfrm_dst),
252 };
253
254 struct xfrm_policy_afinfo xfrm6_policy_afinfo = {
255         .family =               AF_INET6,
256         .lock =                 RW_LOCK_UNLOCKED,
257         .type_map =             &xfrm6_type_map,
258         .dst_ops =              &xfrm6_dst_ops,
259         .dst_lookup =           xfrm6_dst_lookup,
260         .find_bundle =          __xfrm6_find_bundle,
261         .bundle_create =        __xfrm6_bundle_create,
262         .decode_session =       _decode_session6,
263 };
264
265 void __init xfrm6_policy_init(void)
266 {
267         xfrm_policy_register_afinfo(&xfrm6_policy_afinfo);
268 }
269
270 void __exit xfrm6_policy_fini(void)
271 {
272         xfrm_policy_unregister_afinfo(&xfrm6_policy_afinfo);
273 }
274
275 void __init xfrm6_init(void)
276 {
277         xfrm6_policy_init();
278         xfrm6_state_init();
279 }
280
281 void __exit xfrm6_fini(void)
282 {
283         //xfrm6_input_fini();
284         xfrm6_policy_fini();
285         xfrm6_state_fini();
286 }