blob: 011136a9580987a3075d07c77d1d8c41bdbaac45 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * xfrm4_policy.c
3 *
4 * Changes:
5 * Kazunori MIYAZAWA @USAGI
6 * YOSHIFUJI Hideaki @USAGI
7 * Split up af-specific portion
8 *
9 */
10
Herbert Xuaabc9762005-05-03 16:27:10 -070011#include <linux/compiler.h>
Herbert Xuaabc9762005-05-03 16:27:10 -070012#include <linux/inetdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <net/xfrm.h>
14#include <net/ip.h>
15
16static struct dst_ops xfrm4_dst_ops;
17static struct xfrm_policy_afinfo xfrm4_policy_afinfo;
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019static int xfrm4_dst_lookup(struct xfrm_dst **dst, struct flowi *fl)
20{
21 return __ip_route_output_key((struct rtable**)dst, fl);
22}
23
Patrick McHardya1e59ab2006-09-19 12:57:34 -070024static int xfrm4_get_saddr(xfrm_address_t *saddr, xfrm_address_t *daddr)
25{
26 struct rtable *rt;
27 struct flowi fl_tunnel = {
28 .nl_u = {
29 .ip4_u = {
30 .daddr = daddr->a4,
31 },
32 },
33 };
34
35 if (!xfrm4_dst_lookup((struct xfrm_dst **)&rt, &fl_tunnel)) {
36 saddr->a4 = rt->rt_src;
37 dst_release(&rt->u.dst);
38 return 0;
39 }
40 return -EHOSTUNREACH;
41}
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043static struct dst_entry *
44__xfrm4_find_bundle(struct flowi *fl, struct xfrm_policy *policy)
45{
46 struct dst_entry *dst;
47
48 read_lock_bh(&policy->lock);
49 for (dst = policy->bundles; dst; dst = dst->next) {
50 struct xfrm_dst *xdst = (struct xfrm_dst*)dst;
51 if (xdst->u.rt.fl.oif == fl->oif && /*XXX*/
52 xdst->u.rt.fl.fl4_dst == fl->fl4_dst &&
53 xdst->u.rt.fl.fl4_src == fl->fl4_src &&
Herbert Xu4da30892006-02-23 16:19:26 -080054 xdst->u.rt.fl.fl4_tos == fl->fl4_tos &&
Venkat Yekkirala5b368e62006-10-05 15:42:18 -050055 xfrm_bundle_ok(policy, xdst, fl, AF_INET, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 dst_clone(dst);
57 break;
58 }
59 }
60 read_unlock_bh(&policy->lock);
61 return dst;
62}
63
64/* Allocate chain of dst_entry's, attach known xfrm's, calculate
65 * all the metrics... Shortly, bundle a bundle.
66 */
67
68static int
69__xfrm4_bundle_create(struct xfrm_policy *policy, struct xfrm_state **xfrm, int nx,
70 struct flowi *fl, struct dst_entry **dst_p)
71{
72 struct dst_entry *dst, *dst_prev;
73 struct rtable *rt0 = (struct rtable*)(*dst_p);
74 struct rtable *rt = rt0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 struct flowi fl_tunnel = {
76 .nl_u = {
77 .ip4_u = {
Miika Komu43372262007-02-06 14:27:32 -080078 .saddr = fl->fl4_src,
79 .daddr = fl->fl4_dst,
Herbert Xu4da30892006-02-23 16:19:26 -080080 .tos = fl->fl4_tos
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 }
82 }
83 };
84 int i;
85 int err;
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(&xfrm4_dst_ops);
94 struct xfrm_dst *xdst;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96 if (unlikely(dst1 == NULL)) {
97 err = -ENOBUFS;
98 dst_release(&rt->u.dst);
99 goto error;
100 }
101
102 if (!dst)
103 dst = dst1;
104 else {
105 dst_prev->child = dst1;
106 dst1->flags |= DST_NOHASH;
107 dst_clone(dst1);
108 }
109
110 xdst = (struct xfrm_dst *)dst1;
111 xdst->route = &rt->u.dst;
David S. Miller9d4a7062006-08-24 03:18:09 -0700112 xdst->genid = xfrm[i]->genid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114 dst1->next = dst_prev;
115 dst_prev = dst1;
Miika Komu43372262007-02-06 14:27:32 -0800116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 header_len += xfrm[i]->props.header_len;
118 trailer_len += xfrm[i]->props.trailer_len;
119
Miika Komu43372262007-02-06 14:27:32 -0800120 if (xfrm[i]->props.mode == XFRM_MODE_TUNNEL) {
121 unsigned short encap_family = xfrm[i]->props.family;
122 switch(encap_family) {
123 case AF_INET:
124 fl_tunnel.fl4_dst = xfrm[i]->id.daddr.a4;
125 fl_tunnel.fl4_src = xfrm[i]->props.saddr.a4;
126 break;
127#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
128 case AF_INET6:
129 ipv6_addr_copy(&fl_tunnel.fl6_dst, (struct in6_addr*)&xfrm[i]->id.daddr.a6);
130 ipv6_addr_copy(&fl_tunnel.fl6_src, (struct in6_addr*)&xfrm[i]->props.saddr.a6);
131 break;
132#endif
133 default:
134 BUG_ON(1);
135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 err = xfrm_dst_lookup((struct xfrm_dst **)&rt,
Miika Komu43372262007-02-06 14:27:32 -0800137 &fl_tunnel, encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 if (err)
139 goto error;
140 } else
141 dst_hold(&rt->u.dst);
142 }
143
144 dst_prev->child = &rt->u.dst;
145 dst->path = &rt->u.dst;
146
147 *dst_p = dst;
148 dst = dst_prev;
149
150 dst_prev = *dst_p;
151 i = 0;
152 for (; dst_prev != &rt->u.dst; dst_prev = dst_prev->child) {
153 struct xfrm_dst *x = (struct xfrm_dst*)dst_prev;
Miika Komu43372262007-02-06 14:27:32 -0800154 struct xfrm_state_afinfo *afinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 x->u.rt.fl = *fl;
156
157 dst_prev->xfrm = xfrm[i++];
158 dst_prev->dev = rt->u.dst.dev;
159 if (rt->u.dst.dev)
160 dev_hold(rt->u.dst.dev);
161 dst_prev->obsolete = -1;
162 dst_prev->flags |= DST_HOST;
163 dst_prev->lastuse = jiffies;
164 dst_prev->header_len = header_len;
Masahide NAKAMURA1b5c2292006-08-23 18:11:50 -0700165 dst_prev->nfheader_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 dst_prev->trailer_len = trailer_len;
167 memcpy(&dst_prev->metrics, &x->route->metrics, sizeof(dst_prev->metrics));
168
169 /* Copy neighbout for reachability confirmation */
170 dst_prev->neighbour = neigh_clone(rt->u.dst.neighbour);
171 dst_prev->input = rt->u.dst.input;
Miika Komu43372262007-02-06 14:27:32 -0800172 /* XXX: When IPv6 module can be unloaded, we should manage reference
173 * to xfrm6_output in afinfo->output. Miyazawa
174 * */
175 afinfo = xfrm_state_get_afinfo(dst_prev->xfrm->props.family);
176 if (!afinfo) {
177 dst = *dst_p;
178 goto error;
179 }
180 dst_prev->output = afinfo->output;
181 xfrm_state_put_afinfo(afinfo);
182 if (dst_prev->xfrm->props.family == AF_INET && rt->peer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 atomic_inc(&rt->peer->refcnt);
184 x->u.rt.peer = rt->peer;
185 /* Sheit... I remember I did this right. Apparently,
186 * it was magically lost, so this code needs audit */
187 x->u.rt.rt_flags = rt0->rt_flags&(RTCF_BROADCAST|RTCF_MULTICAST|RTCF_LOCAL);
188 x->u.rt.rt_type = rt->rt_type;
189 x->u.rt.rt_src = rt0->rt_src;
190 x->u.rt.rt_dst = rt0->rt_dst;
191 x->u.rt.rt_gateway = rt->rt_gateway;
192 x->u.rt.rt_spec_dst = rt0->rt_spec_dst;
Herbert Xuaabc9762005-05-03 16:27:10 -0700193 x->u.rt.idev = rt0->idev;
194 in_dev_hold(rt0->idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 header_len -= x->u.dst.xfrm->props.header_len;
196 trailer_len -= x->u.dst.xfrm->props.trailer_len;
197 }
198
199 xfrm_init_pmtu(dst);
200 return 0;
201
202error:
203 if (dst)
204 dst_free(dst);
205 return err;
206}
207
208static void
209_decode_session4(struct sk_buff *skb, struct flowi *fl)
210{
211 struct iphdr *iph = skb->nh.iph;
212 u8 *xprth = skb->nh.raw + iph->ihl*4;
213
214 memset(fl, 0, sizeof(struct flowi));
215 if (!(iph->frag_off & htons(IP_MF | IP_OFFSET))) {
216 switch (iph->protocol) {
217 case IPPROTO_UDP:
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800218 case IPPROTO_UDPLITE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 case IPPROTO_TCP:
220 case IPPROTO_SCTP:
Patrick McHardy9e999992005-12-19 14:03:46 -0800221 case IPPROTO_DCCP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 if (pskb_may_pull(skb, xprth + 4 - skb->data)) {
Al Viro8c689a62006-11-08 00:20:21 -0800223 __be16 *ports = (__be16 *)xprth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 fl->fl_ip_sport = ports[0];
226 fl->fl_ip_dport = ports[1];
227 }
228 break;
229
230 case IPPROTO_ICMP:
231 if (pskb_may_pull(skb, xprth + 2 - skb->data)) {
232 u8 *icmp = xprth;
233
234 fl->fl_icmp_type = icmp[0];
235 fl->fl_icmp_code = icmp[1];
236 }
237 break;
238
239 case IPPROTO_ESP:
240 if (pskb_may_pull(skb, xprth + 4 - skb->data)) {
Al Viro4324a172006-09-27 18:49:07 -0700241 __be32 *ehdr = (__be32 *)xprth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243 fl->fl_ipsec_spi = ehdr[0];
244 }
245 break;
246
247 case IPPROTO_AH:
248 if (pskb_may_pull(skb, xprth + 8 - skb->data)) {
Al Viro4324a172006-09-27 18:49:07 -0700249 __be32 *ah_hdr = (__be32*)xprth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 fl->fl_ipsec_spi = ah_hdr[1];
252 }
253 break;
254
255 case IPPROTO_COMP:
256 if (pskb_may_pull(skb, xprth + 4 - skb->data)) {
Al Viro4324a172006-09-27 18:49:07 -0700257 __be16 *ipcomp_hdr = (__be16 *)xprth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Alexey Dobriyan4195f812006-05-22 16:53:22 -0700259 fl->fl_ipsec_spi = htonl(ntohs(ipcomp_hdr[1]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
261 break;
262 default:
263 fl->fl_ipsec_spi = 0;
264 break;
265 };
266 }
267 fl->proto = iph->protocol;
268 fl->fl4_dst = iph->daddr;
269 fl->fl4_src = iph->saddr;
Herbert Xu4da30892006-02-23 16:19:26 -0800270 fl->fl4_tos = iph->tos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
272
273static inline int xfrm4_garbage_collect(void)
274{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 xfrm4_policy_afinfo.garbage_collect();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 return (atomic_read(&xfrm4_dst_ops.entries) > xfrm4_dst_ops.gc_thresh*2);
277}
278
279static void xfrm4_update_pmtu(struct dst_entry *dst, u32 mtu)
280{
281 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
282 struct dst_entry *path = xdst->route;
283
284 path->ops->update_pmtu(path, mtu);
285}
286
Herbert Xuaabc9762005-05-03 16:27:10 -0700287static void xfrm4_dst_destroy(struct dst_entry *dst)
288{
289 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
290
291 if (likely(xdst->u.rt.idev))
292 in_dev_put(xdst->u.rt.idev);
Miika Komu43372262007-02-06 14:27:32 -0800293 if (dst->xfrm->props.family == AF_INET && likely(xdst->u.rt.peer))
David S. Miller26db1672006-12-06 23:45:15 -0800294 inet_putpeer(xdst->u.rt.peer);
Herbert Xuaabc9762005-05-03 16:27:10 -0700295 xfrm_dst_destroy(xdst);
296}
297
298static void xfrm4_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
299 int unregister)
300{
301 struct xfrm_dst *xdst;
302
303 if (!unregister)
304 return;
305
306 xdst = (struct xfrm_dst *)dst;
307 if (xdst->u.rt.idev->dev == dev) {
308 struct in_device *loopback_idev = in_dev_get(&loopback_dev);
309 BUG_ON(!loopback_idev);
310
311 do {
312 in_dev_put(xdst->u.rt.idev);
313 xdst->u.rt.idev = loopback_idev;
314 in_dev_hold(loopback_idev);
315 xdst = (struct xfrm_dst *)xdst->u.dst.child;
316 } while (xdst->u.dst.xfrm);
317
318 __in_dev_put(loopback_idev);
319 }
320
321 xfrm_dst_ifdown(dst, dev);
322}
323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324static struct dst_ops xfrm4_dst_ops = {
325 .family = AF_INET,
326 .protocol = __constant_htons(ETH_P_IP),
327 .gc = xfrm4_garbage_collect,
328 .update_pmtu = xfrm4_update_pmtu,
Herbert Xuaabc9762005-05-03 16:27:10 -0700329 .destroy = xfrm4_dst_destroy,
330 .ifdown = xfrm4_dst_ifdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 .gc_thresh = 1024,
332 .entry_size = sizeof(struct xfrm_dst),
333};
334
335static struct xfrm_policy_afinfo xfrm4_policy_afinfo = {
336 .family = AF_INET,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 .dst_ops = &xfrm4_dst_ops,
338 .dst_lookup = xfrm4_dst_lookup,
Patrick McHardya1e59ab2006-09-19 12:57:34 -0700339 .get_saddr = xfrm4_get_saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 .find_bundle = __xfrm4_find_bundle,
341 .bundle_create = __xfrm4_bundle_create,
342 .decode_session = _decode_session4,
343};
344
345static void __init xfrm4_policy_init(void)
346{
347 xfrm_policy_register_afinfo(&xfrm4_policy_afinfo);
348}
349
350static void __exit xfrm4_policy_fini(void)
351{
352 xfrm_policy_unregister_afinfo(&xfrm4_policy_afinfo);
353}
354
355void __init xfrm4_init(void)
356{
357 xfrm4_state_init();
358 xfrm4_policy_init();
359}
360