patch-2_6_7-vs1_9_1_12
[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                 if (!dst)
111                         dst = dst1;
112                 else {
113                         dst_prev->child = dst1;
114                         dst1->flags |= DST_NOHASH;
115                         dst_clone(dst1);
116                 }
117                 dst_prev = dst1;
118                 if (xfrm[i]->props.mode) {
119                         remote = (struct in6_addr*)&xfrm[i]->id.daddr;
120                         local  = (struct in6_addr*)&xfrm[i]->props.saddr;
121                 }
122                 header_len += xfrm[i]->props.header_len;
123                 trailer_len += xfrm[i]->props.trailer_len;
124         }
125
126         if (ipv6_addr_cmp(remote, &fl->fl6_dst)) {
127                 struct flowi fl_tunnel;
128
129                 memset(&fl_tunnel, 0, sizeof(fl_tunnel));
130                 ipv6_addr_copy(&fl_tunnel.fl6_dst, remote);
131                 ipv6_addr_copy(&fl_tunnel.fl6_src, local);
132
133                 err = xfrm_dst_lookup((struct xfrm_dst **) &rt,
134                                       &fl_tunnel, AF_INET6);
135                 if (err)
136                         goto error;
137         } else {
138                 dst_hold(&rt->u.dst);
139         }
140         dst_prev->child = &rt->u.dst;
141         i = 0;
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->xfrm = xfrm[i++];
146                 dst_prev->dev = rt->u.dst.dev;
147                 if (rt->u.dst.dev)
148                         dev_hold(rt->u.dst.dev);
149                 dst_prev->obsolete      = -1;
150                 dst_prev->flags        |= DST_HOST;
151                 dst_prev->lastuse       = jiffies;
152                 dst_prev->header_len    = header_len;
153                 dst_prev->trailer_len   = trailer_len;
154                 memcpy(&dst_prev->metrics, &rt->u.dst.metrics, sizeof(dst_prev->metrics));
155                 dst_prev->path          = &rt->u.dst;
156
157                 /* Copy neighbour for reachability confirmation */
158                 dst_prev->neighbour     = neigh_clone(rt->u.dst.neighbour);
159                 dst_prev->input         = rt->u.dst.input;
160                 dst_prev->output        = dst_prev->xfrm->type->output;
161                 /* Sheit... I remember I did this right. Apparently,
162                  * it was magically lost, so this code needs audit */
163                 x->u.rt6.rt6i_flags    = rt0->rt6i_flags&(RTCF_BROADCAST|RTCF_MULTICAST|RTCF_LOCAL);
164                 x->u.rt6.rt6i_metric   = rt0->rt6i_metric;
165                 x->u.rt6.rt6i_node     = rt0->rt6i_node;
166                 x->u.rt6.rt6i_gateway  = rt0->rt6i_gateway;
167                 memcpy(&x->u.rt6.rt6i_gateway, &rt0->rt6i_gateway, sizeof(x->u.rt6.rt6i_gateway)); 
168                 x->u.rt6.rt6i_dst      = rt0->rt6i_dst;
169                 x->u.rt6.rt6i_src      = rt0->rt6i_src; 
170                 header_len -= x->u.dst.xfrm->props.header_len;
171                 trailer_len -= x->u.dst.xfrm->props.trailer_len;
172         }
173         *dst_p = dst;
174         return 0;
175
176 error:
177         if (dst)
178                 dst_free(dst);
179         return err;
180 }
181
182 static inline void
183 _decode_session6(struct sk_buff *skb, struct flowi *fl)
184 {
185         u16 offset = sizeof(struct ipv6hdr);
186         struct ipv6hdr *hdr = skb->nh.ipv6h;
187         struct ipv6_opt_hdr *exthdr = (struct ipv6_opt_hdr*)(skb->nh.raw + offset);
188         u8 nexthdr = skb->nh.ipv6h->nexthdr;
189
190         memset(fl, 0, sizeof(struct flowi));
191         ipv6_addr_copy(&fl->fl6_dst, &hdr->daddr);
192         ipv6_addr_copy(&fl->fl6_src, &hdr->saddr);
193
194         while (pskb_may_pull(skb, skb->nh.raw + offset + 1 - skb->data)) {
195                 switch (nexthdr) {
196                 case NEXTHDR_ROUTING:
197                 case NEXTHDR_HOP:
198                 case NEXTHDR_DEST:
199                         offset += ipv6_optlen(exthdr);
200                         nexthdr = exthdr->nexthdr;
201                         exthdr = (struct ipv6_opt_hdr*)(skb->nh.raw + offset);
202                         break;
203
204                 case IPPROTO_UDP:
205                 case IPPROTO_TCP:
206                 case IPPROTO_SCTP:
207                         if (pskb_may_pull(skb, skb->nh.raw + offset + 4 - skb->data)) {
208                                 u16 *ports = (u16 *)exthdr;
209
210                                 fl->fl_ip_sport = ports[0];
211                                 fl->fl_ip_dport = ports[1];
212                         }
213                         fl->proto = nexthdr;
214                         return;
215
216                 /* XXX Why are there these headers? */
217                 case IPPROTO_AH:
218                 case IPPROTO_ESP:
219                 case IPPROTO_COMP:
220                 default:
221                         fl->fl_ipsec_spi = 0;
222                         fl->proto = nexthdr;
223                         return;
224                 };
225         }
226 }
227
228 static inline int xfrm6_garbage_collect(void)
229 {
230         read_lock(&xfrm6_policy_afinfo.lock);
231         xfrm6_policy_afinfo.garbage_collect();
232         read_unlock(&xfrm6_policy_afinfo.lock);
233         return (atomic_read(&xfrm6_dst_ops.entries) > xfrm6_dst_ops.gc_thresh*2);
234 }
235
236 static void xfrm6_update_pmtu(struct dst_entry *dst, u32 mtu)
237 {
238         struct dst_entry *path = dst->path;
239
240         if (mtu >= IPV6_MIN_MTU && mtu < dst_pmtu(dst))
241                 path->ops->update_pmtu(path, mtu);
242         
243         return;
244 }
245
246 struct dst_ops xfrm6_dst_ops = {
247         .family =               AF_INET6,
248         .protocol =             __constant_htons(ETH_P_IPV6),
249         .gc =                   xfrm6_garbage_collect,
250         .update_pmtu =          xfrm6_update_pmtu,
251         .gc_thresh =            1024,
252         .entry_size =           sizeof(struct xfrm_dst),
253 };
254
255 struct xfrm_policy_afinfo xfrm6_policy_afinfo = {
256         .family =               AF_INET6,
257         .lock =                 RW_LOCK_UNLOCKED,
258         .type_map =             &xfrm6_type_map,
259         .dst_ops =              &xfrm6_dst_ops,
260         .dst_lookup =           xfrm6_dst_lookup,
261         .find_bundle =          __xfrm6_find_bundle,
262         .bundle_create =        __xfrm6_bundle_create,
263         .decode_session =       _decode_session6,
264 };
265
266 void __init xfrm6_policy_init(void)
267 {
268         xfrm_policy_register_afinfo(&xfrm6_policy_afinfo);
269 }
270
271 void __exit xfrm6_policy_fini(void)
272 {
273         xfrm_policy_unregister_afinfo(&xfrm6_policy_afinfo);
274 }
275
276 void __init xfrm6_init(void)
277 {
278         xfrm6_policy_init();
279         xfrm6_state_init();
280 }
281
282 void __exit xfrm6_fini(void)
283 {
284         //xfrm6_input_fini();
285         xfrm6_policy_fini();
286         xfrm6_state_fini();
287 }