IPsec: correct semantics for SELinux policy matching
[hh.org.git] / net / ipv6 / xfrm6_policy.c
blob73cee2ec07e8ed22fcba162427ce91622e5d7f59
1 /*
2 * xfrm6_policy.c: based on xfrm4_policy.c
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
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 #ifdef CONFIG_IPV6_MIP6
22 #include <net/mip6.h>
23 #endif
25 static struct dst_ops xfrm6_dst_ops;
26 static struct xfrm_policy_afinfo xfrm6_policy_afinfo;
28 static int xfrm6_dst_lookup(struct xfrm_dst **dst, struct flowi *fl)
30 int err = 0;
31 *dst = (struct xfrm_dst*)ip6_route_output(NULL, fl);
32 if (!*dst)
33 err = -ENETUNREACH;
34 return err;
37 static int xfrm6_get_saddr(xfrm_address_t *saddr, xfrm_address_t *daddr)
39 struct rt6_info *rt;
40 struct flowi fl_tunnel = {
41 .nl_u = {
42 .ip6_u = {
43 .daddr = *(struct in6_addr *)&daddr->a6,
48 if (!xfrm6_dst_lookup((struct xfrm_dst **)&rt, &fl_tunnel)) {
49 ipv6_get_saddr(&rt->u.dst, (struct in6_addr *)&daddr->a6,
50 (struct in6_addr *)&saddr->a6);
51 dst_release(&rt->u.dst);
52 return 0;
54 return -EHOSTUNREACH;
57 static struct dst_entry *
58 __xfrm6_find_bundle(struct flowi *fl, struct xfrm_policy *policy)
60 struct dst_entry *dst;
62 /* Still not clear if we should set fl->fl6_{src,dst}... */
63 read_lock_bh(&policy->lock);
64 for (dst = policy->bundles; dst; dst = dst->next) {
65 struct xfrm_dst *xdst = (struct xfrm_dst*)dst;
66 struct in6_addr fl_dst_prefix, fl_src_prefix;
68 ipv6_addr_prefix(&fl_dst_prefix,
69 &fl->fl6_dst,
70 xdst->u.rt6.rt6i_dst.plen);
71 ipv6_addr_prefix(&fl_src_prefix,
72 &fl->fl6_src,
73 xdst->u.rt6.rt6i_src.plen);
74 if (ipv6_addr_equal(&xdst->u.rt6.rt6i_dst.addr, &fl_dst_prefix) &&
75 ipv6_addr_equal(&xdst->u.rt6.rt6i_src.addr, &fl_src_prefix) &&
76 xfrm_bundle_ok(policy, xdst, fl, AF_INET6,
77 (xdst->u.rt6.rt6i_dst.plen != 128 ||
78 xdst->u.rt6.rt6i_src.plen != 128))) {
79 dst_clone(dst);
80 break;
83 read_unlock_bh(&policy->lock);
84 return dst;
87 static inline struct in6_addr*
88 __xfrm6_bundle_addr_remote(struct xfrm_state *x, struct in6_addr *addr)
90 return (x->type->remote_addr) ?
91 (struct in6_addr*)x->type->remote_addr(x, (xfrm_address_t *)addr) :
92 (struct in6_addr*)&x->id.daddr;
95 static inline struct in6_addr*
96 __xfrm6_bundle_addr_local(struct xfrm_state *x, struct in6_addr *addr)
98 return (x->type->local_addr) ?
99 (struct in6_addr*)x->type->local_addr(x, (xfrm_address_t *)addr) :
100 (struct in6_addr*)&x->props.saddr;
103 static inline void
104 __xfrm6_bundle_len_inc(int *len, int *nflen, struct xfrm_state *x)
106 if (x->type->flags & XFRM_TYPE_NON_FRAGMENT)
107 *nflen += x->props.header_len;
108 else
109 *len += x->props.header_len;
112 static inline void
113 __xfrm6_bundle_len_dec(int *len, int *nflen, struct xfrm_state *x)
115 if (x->type->flags & XFRM_TYPE_NON_FRAGMENT)
116 *nflen -= x->props.header_len;
117 else
118 *len -= x->props.header_len;
121 /* Allocate chain of dst_entry's, attach known xfrm's, calculate
122 * all the metrics... Shortly, bundle a bundle.
125 static int
126 __xfrm6_bundle_create(struct xfrm_policy *policy, struct xfrm_state **xfrm, int nx,
127 struct flowi *fl, struct dst_entry **dst_p)
129 struct dst_entry *dst, *dst_prev;
130 struct rt6_info *rt0 = (struct rt6_info*)(*dst_p);
131 struct rt6_info *rt = rt0;
132 struct in6_addr *remote = &fl->fl6_dst;
133 struct in6_addr *local = &fl->fl6_src;
134 struct flowi fl_tunnel = {
135 .nl_u = {
136 .ip6_u = {
137 .saddr = *local,
138 .daddr = *remote
142 int i;
143 int err = 0;
144 int header_len = 0;
145 int nfheader_len = 0;
146 int trailer_len = 0;
148 dst = dst_prev = NULL;
149 dst_hold(&rt->u.dst);
151 for (i = 0; i < nx; i++) {
152 struct dst_entry *dst1 = dst_alloc(&xfrm6_dst_ops);
153 struct xfrm_dst *xdst;
154 int tunnel = 0;
156 if (unlikely(dst1 == NULL)) {
157 err = -ENOBUFS;
158 dst_release(&rt->u.dst);
159 goto error;
162 if (!dst)
163 dst = dst1;
164 else {
165 dst_prev->child = dst1;
166 dst1->flags |= DST_NOHASH;
167 dst_clone(dst1);
170 xdst = (struct xfrm_dst *)dst1;
171 xdst->route = &rt->u.dst;
172 xdst->genid = xfrm[i]->genid;
173 if (rt->rt6i_node)
174 xdst->route_cookie = rt->rt6i_node->fn_sernum;
176 dst1->next = dst_prev;
177 dst_prev = dst1;
178 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
179 remote = __xfrm6_bundle_addr_remote(xfrm[i], remote);
180 local = __xfrm6_bundle_addr_local(xfrm[i], local);
181 tunnel = 1;
183 __xfrm6_bundle_len_inc(&header_len, &nfheader_len, xfrm[i]);
184 trailer_len += xfrm[i]->props.trailer_len;
186 if (tunnel) {
187 ipv6_addr_copy(&fl_tunnel.fl6_dst, remote);
188 ipv6_addr_copy(&fl_tunnel.fl6_src, local);
189 err = xfrm_dst_lookup((struct xfrm_dst **) &rt,
190 &fl_tunnel, AF_INET6);
191 if (err)
192 goto error;
193 } else
194 dst_hold(&rt->u.dst);
197 dst_prev->child = &rt->u.dst;
198 dst->path = &rt->u.dst;
199 if (rt->rt6i_node)
200 ((struct xfrm_dst *)dst)->path_cookie = rt->rt6i_node->fn_sernum;
202 *dst_p = dst;
203 dst = dst_prev;
205 dst_prev = *dst_p;
206 i = 0;
207 for (; dst_prev != &rt->u.dst; dst_prev = dst_prev->child) {
208 struct xfrm_dst *x = (struct xfrm_dst*)dst_prev;
210 dst_prev->xfrm = xfrm[i++];
211 dst_prev->dev = rt->u.dst.dev;
212 if (rt->u.dst.dev)
213 dev_hold(rt->u.dst.dev);
214 dst_prev->obsolete = -1;
215 dst_prev->flags |= DST_HOST;
216 dst_prev->lastuse = jiffies;
217 dst_prev->header_len = header_len;
218 dst_prev->nfheader_len = nfheader_len;
219 dst_prev->trailer_len = trailer_len;
220 memcpy(&dst_prev->metrics, &x->route->metrics, sizeof(dst_prev->metrics));
222 /* Copy neighbour for reachability confirmation */
223 dst_prev->neighbour = neigh_clone(rt->u.dst.neighbour);
224 dst_prev->input = rt->u.dst.input;
225 dst_prev->output = xfrm6_output;
226 /* Sheit... I remember I did this right. Apparently,
227 * it was magically lost, so this code needs audit */
228 x->u.rt6.rt6i_flags = rt0->rt6i_flags&(RTCF_BROADCAST|RTCF_MULTICAST|RTCF_LOCAL);
229 x->u.rt6.rt6i_metric = rt0->rt6i_metric;
230 x->u.rt6.rt6i_node = rt0->rt6i_node;
231 x->u.rt6.rt6i_gateway = rt0->rt6i_gateway;
232 memcpy(&x->u.rt6.rt6i_gateway, &rt0->rt6i_gateway, sizeof(x->u.rt6.rt6i_gateway));
233 x->u.rt6.rt6i_dst = rt0->rt6i_dst;
234 x->u.rt6.rt6i_src = rt0->rt6i_src;
235 x->u.rt6.rt6i_idev = rt0->rt6i_idev;
236 in6_dev_hold(rt0->rt6i_idev);
237 __xfrm6_bundle_len_dec(&header_len, &nfheader_len, x->u.dst.xfrm);
238 trailer_len -= x->u.dst.xfrm->props.trailer_len;
241 xfrm_init_pmtu(dst);
242 return 0;
244 error:
245 if (dst)
246 dst_free(dst);
247 return err;
250 static inline void
251 _decode_session6(struct sk_buff *skb, struct flowi *fl)
253 u16 offset = skb->h.raw - skb->nh.raw;
254 struct ipv6hdr *hdr = skb->nh.ipv6h;
255 struct ipv6_opt_hdr *exthdr;
256 u8 nexthdr = skb->nh.raw[IP6CB(skb)->nhoff];
258 memset(fl, 0, sizeof(struct flowi));
259 ipv6_addr_copy(&fl->fl6_dst, &hdr->daddr);
260 ipv6_addr_copy(&fl->fl6_src, &hdr->saddr);
262 while (pskb_may_pull(skb, skb->nh.raw + offset + 1 - skb->data)) {
263 exthdr = (struct ipv6_opt_hdr*)(skb->nh.raw + offset);
265 switch (nexthdr) {
266 case NEXTHDR_ROUTING:
267 case NEXTHDR_HOP:
268 case NEXTHDR_DEST:
269 offset += ipv6_optlen(exthdr);
270 nexthdr = exthdr->nexthdr;
271 exthdr = (struct ipv6_opt_hdr*)(skb->nh.raw + offset);
272 break;
274 case IPPROTO_UDP:
275 case IPPROTO_TCP:
276 case IPPROTO_SCTP:
277 case IPPROTO_DCCP:
278 if (pskb_may_pull(skb, skb->nh.raw + offset + 4 - skb->data)) {
279 u16 *ports = (u16 *)exthdr;
281 fl->fl_ip_sport = ports[0];
282 fl->fl_ip_dport = ports[1];
284 fl->proto = nexthdr;
285 return;
287 case IPPROTO_ICMPV6:
288 if (pskb_may_pull(skb, skb->nh.raw + offset + 2 - skb->data)) {
289 u8 *icmp = (u8 *)exthdr;
291 fl->fl_icmp_type = icmp[0];
292 fl->fl_icmp_code = icmp[1];
294 fl->proto = nexthdr;
295 return;
297 #ifdef CONFIG_IPV6_MIP6
298 case IPPROTO_MH:
299 if (pskb_may_pull(skb, skb->nh.raw + offset + 3 - skb->data)) {
300 struct ip6_mh *mh;
301 mh = (struct ip6_mh *)exthdr;
303 fl->fl_mh_type = mh->ip6mh_type;
305 fl->proto = nexthdr;
306 return;
307 #endif
309 /* XXX Why are there these headers? */
310 case IPPROTO_AH:
311 case IPPROTO_ESP:
312 case IPPROTO_COMP:
313 default:
314 fl->fl_ipsec_spi = 0;
315 fl->proto = nexthdr;
316 return;
321 static inline int xfrm6_garbage_collect(void)
323 xfrm6_policy_afinfo.garbage_collect();
324 return (atomic_read(&xfrm6_dst_ops.entries) > xfrm6_dst_ops.gc_thresh*2);
327 static void xfrm6_update_pmtu(struct dst_entry *dst, u32 mtu)
329 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
330 struct dst_entry *path = xdst->route;
332 path->ops->update_pmtu(path, mtu);
335 static void xfrm6_dst_destroy(struct dst_entry *dst)
337 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
339 if (likely(xdst->u.rt6.rt6i_idev))
340 in6_dev_put(xdst->u.rt6.rt6i_idev);
341 xfrm_dst_destroy(xdst);
344 static void xfrm6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
345 int unregister)
347 struct xfrm_dst *xdst;
349 if (!unregister)
350 return;
352 xdst = (struct xfrm_dst *)dst;
353 if (xdst->u.rt6.rt6i_idev->dev == dev) {
354 struct inet6_dev *loopback_idev = in6_dev_get(&loopback_dev);
355 BUG_ON(!loopback_idev);
357 do {
358 in6_dev_put(xdst->u.rt6.rt6i_idev);
359 xdst->u.rt6.rt6i_idev = loopback_idev;
360 in6_dev_hold(loopback_idev);
361 xdst = (struct xfrm_dst *)xdst->u.dst.child;
362 } while (xdst->u.dst.xfrm);
364 __in6_dev_put(loopback_idev);
367 xfrm_dst_ifdown(dst, dev);
370 static struct dst_ops xfrm6_dst_ops = {
371 .family = AF_INET6,
372 .protocol = __constant_htons(ETH_P_IPV6),
373 .gc = xfrm6_garbage_collect,
374 .update_pmtu = xfrm6_update_pmtu,
375 .destroy = xfrm6_dst_destroy,
376 .ifdown = xfrm6_dst_ifdown,
377 .gc_thresh = 1024,
378 .entry_size = sizeof(struct xfrm_dst),
381 static struct xfrm_policy_afinfo xfrm6_policy_afinfo = {
382 .family = AF_INET6,
383 .dst_ops = &xfrm6_dst_ops,
384 .dst_lookup = xfrm6_dst_lookup,
385 .get_saddr = xfrm6_get_saddr,
386 .find_bundle = __xfrm6_find_bundle,
387 .bundle_create = __xfrm6_bundle_create,
388 .decode_session = _decode_session6,
391 static void __init xfrm6_policy_init(void)
393 xfrm_policy_register_afinfo(&xfrm6_policy_afinfo);
396 static void xfrm6_policy_fini(void)
398 xfrm_policy_unregister_afinfo(&xfrm6_policy_afinfo);
401 void __init xfrm6_init(void)
403 xfrm6_policy_init();
404 xfrm6_state_init();
407 void xfrm6_fini(void)
409 //xfrm6_input_fini();
410 xfrm6_policy_fini();
411 xfrm6_state_fini();