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