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