blob: d903c8bdffcd15a2832d95b73a77a5ad201662bb [file] [log] [blame]
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * xfrm4_policy.c
3 *
4 * Changes:
5 * Kazunori MIYAZAWA @USAGI
6 * YOSHIFUJI Hideaki @USAGI
7 * Split up af-specific portion
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09008 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
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>
Herbert Xu45ff5a32007-11-13 21:35:32 -080013#include <net/dst.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <net/xfrm.h>
15#include <net/ip.h>
16
17static struct dst_ops xfrm4_dst_ops;
18static struct xfrm_policy_afinfo xfrm4_policy_afinfo;
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020static int xfrm4_dst_lookup(struct xfrm_dst **dst, struct flowi *fl)
21{
22 return __ip_route_output_key((struct rtable**)dst, fl);
23}
24
Patrick McHardya1e59ab2006-09-19 12:57:34 -070025static int xfrm4_get_saddr(xfrm_address_t *saddr, xfrm_address_t *daddr)
26{
27 struct rtable *rt;
28 struct flowi fl_tunnel = {
29 .nl_u = {
30 .ip4_u = {
31 .daddr = daddr->a4,
32 },
33 },
34 };
35
36 if (!xfrm4_dst_lookup((struct xfrm_dst **)&rt, &fl_tunnel)) {
37 saddr->a4 = rt->rt_src;
38 dst_release(&rt->u.dst);
39 return 0;
40 }
41 return -EHOSTUNREACH;
42}
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044static struct dst_entry *
45__xfrm4_find_bundle(struct flowi *fl, struct xfrm_policy *policy)
46{
47 struct dst_entry *dst;
48
49 read_lock_bh(&policy->lock);
50 for (dst = policy->bundles; dst; dst = dst->next) {
51 struct xfrm_dst *xdst = (struct xfrm_dst*)dst;
52 if (xdst->u.rt.fl.oif == fl->oif && /*XXX*/
53 xdst->u.rt.fl.fl4_dst == fl->fl4_dst &&
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090054 xdst->u.rt.fl.fl4_src == fl->fl4_src &&
55 xdst->u.rt.fl.fl4_tos == fl->fl4_tos &&
Venkat Yekkirala5b368e62006-10-05 15:42:18 -050056 xfrm_bundle_ok(policy, xdst, fl, AF_INET, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 dst_clone(dst);
58 break;
59 }
60 }
61 read_unlock_bh(&policy->lock);
62 return dst;
63}
64
65/* Allocate chain of dst_entry's, attach known xfrm's, calculate
66 * all the metrics... Shortly, bundle a bundle.
67 */
68
69static int
70__xfrm4_bundle_create(struct xfrm_policy *policy, struct xfrm_state **xfrm, int nx,
71 struct flowi *fl, struct dst_entry **dst_p)
72{
73 struct dst_entry *dst, *dst_prev;
74 struct rtable *rt0 = (struct rtable*)(*dst_p);
75 struct rtable *rt = rt0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 struct flowi fl_tunnel = {
77 .nl_u = {
78 .ip4_u = {
Miika Komu43372262007-02-06 14:27:32 -080079 .saddr = fl->fl4_src,
80 .daddr = fl->fl4_dst,
Herbert Xu4da30892006-02-23 16:19:26 -080081 .tos = fl->fl4_tos
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 }
83 }
84 };
85 int i;
86 int err;
87 int header_len = 0;
88 int trailer_len = 0;
89
90 dst = dst_prev = NULL;
91 dst_hold(&rt->u.dst);
92
93 for (i = 0; i < nx; i++) {
94 struct dst_entry *dst1 = dst_alloc(&xfrm4_dst_ops);
95 struct xfrm_dst *xdst;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
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;
David S. Miller9d4a7062006-08-24 03:18:09 -0700113 xdst->genid = xfrm[i]->genid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115 dst1->next = dst_prev;
116 dst_prev = dst1;
Miika Komu43372262007-02-06 14:27:32 -0800117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 header_len += xfrm[i]->props.header_len;
119 trailer_len += xfrm[i]->props.trailer_len;
120
Herbert Xu1bfcb102007-10-17 21:31:50 -0700121 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
Miika Komu43372262007-02-06 14:27:32 -0800122 unsigned short encap_family = xfrm[i]->props.family;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800123 switch (encap_family) {
Miika Komu43372262007-02-06 14:27:32 -0800124 case AF_INET:
125 fl_tunnel.fl4_dst = xfrm[i]->id.daddr.a4;
126 fl_tunnel.fl4_src = xfrm[i]->props.saddr.a4;
127 break;
128#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
129 case AF_INET6:
130 ipv6_addr_copy(&fl_tunnel.fl6_dst, (struct in6_addr*)&xfrm[i]->id.daddr.a6);
131 ipv6_addr_copy(&fl_tunnel.fl6_src, (struct in6_addr*)&xfrm[i]->props.saddr.a6);
132 break;
133#endif
134 default:
135 BUG_ON(1);
136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 err = xfrm_dst_lookup((struct xfrm_dst **)&rt,
Miika Komu43372262007-02-06 14:27:32 -0800138 &fl_tunnel, encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 if (err)
140 goto error;
141 } else
142 dst_hold(&rt->u.dst);
143 }
144
145 dst_prev->child = &rt->u.dst;
146 dst->path = &rt->u.dst;
147
Herbert Xu8ce68ce2007-11-13 21:35:01 -0800148 /* Copy neighbout for reachability confirmation */
149 dst->neighbour = neigh_clone(rt->u.dst.neighbour);
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 *dst_p = dst;
152 dst = dst_prev;
153
154 dst_prev = *dst_p;
155 i = 0;
Herbert Xufff69382007-11-13 21:36:07 -0800156 err = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 for (; dst_prev != &rt->u.dst; dst_prev = dst_prev->child) {
158 struct xfrm_dst *x = (struct xfrm_dst*)dst_prev;
159 x->u.rt.fl = *fl;
160
161 dst_prev->xfrm = xfrm[i++];
162 dst_prev->dev = rt->u.dst.dev;
Herbert Xufff69382007-11-13 21:36:07 -0800163 if (!rt->u.dst.dev)
164 goto error;
165 dev_hold(rt->u.dst.dev);
166
167 x->u.rt.idev = in_dev_get(rt->u.dst.dev);
168 if (!x->u.rt.idev)
169 goto error;
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 dst_prev->obsolete = -1;
172 dst_prev->flags |= DST_HOST;
173 dst_prev->lastuse = jiffies;
174 dst_prev->header_len = header_len;
175 dst_prev->trailer_len = trailer_len;
176 memcpy(&dst_prev->metrics, &x->route->metrics, sizeof(dst_prev->metrics));
177
Herbert Xu45ff5a32007-11-13 21:35:32 -0800178 dst_prev->input = dst_discard;
Herbert Xu13996372007-10-17 21:35:51 -0700179 dst_prev->output = dst_prev->xfrm->outer_mode->afinfo->output;
Herbert Xued3e37d2007-10-17 21:34:46 -0700180 if (rt0->peer)
181 atomic_inc(&rt0->peer->refcnt);
182 x->u.rt.peer = rt0->peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 /* Sheit... I remember I did this right. Apparently,
184 * it was magically lost, so this code needs audit */
185 x->u.rt.rt_flags = rt0->rt_flags&(RTCF_BROADCAST|RTCF_MULTICAST|RTCF_LOCAL);
Herbert Xued3e37d2007-10-17 21:34:46 -0700186 x->u.rt.rt_type = rt0->rt_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 x->u.rt.rt_src = rt0->rt_src;
188 x->u.rt.rt_dst = rt0->rt_dst;
Herbert Xued3e37d2007-10-17 21:34:46 -0700189 x->u.rt.rt_gateway = rt0->rt_gateway;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 x->u.rt.rt_spec_dst = rt0->rt_spec_dst;
191 header_len -= x->u.dst.xfrm->props.header_len;
192 trailer_len -= x->u.dst.xfrm->props.trailer_len;
193 }
194
195 xfrm_init_pmtu(dst);
196 return 0;
197
198error:
199 if (dst)
200 dst_free(dst);
201 return err;
202}
203
204static void
205_decode_session4(struct sk_buff *skb, struct flowi *fl)
206{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700207 struct iphdr *iph = ip_hdr(skb);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700208 u8 *xprth = skb_network_header(skb) + iph->ihl * 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 memset(fl, 0, sizeof(struct flowi));
211 if (!(iph->frag_off & htons(IP_MF | IP_OFFSET))) {
212 switch (iph->protocol) {
213 case IPPROTO_UDP:
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800214 case IPPROTO_UDPLITE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 case IPPROTO_TCP:
216 case IPPROTO_SCTP:
Patrick McHardy9e999992005-12-19 14:03:46 -0800217 case IPPROTO_DCCP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 if (pskb_may_pull(skb, xprth + 4 - skb->data)) {
Al Viro8c689a62006-11-08 00:20:21 -0800219 __be16 *ports = (__be16 *)xprth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221 fl->fl_ip_sport = ports[0];
222 fl->fl_ip_dport = ports[1];
223 }
224 break;
225
226 case IPPROTO_ICMP:
227 if (pskb_may_pull(skb, xprth + 2 - skb->data)) {
228 u8 *icmp = xprth;
229
230 fl->fl_icmp_type = icmp[0];
231 fl->fl_icmp_code = icmp[1];
232 }
233 break;
234
235 case IPPROTO_ESP:
236 if (pskb_may_pull(skb, xprth + 4 - skb->data)) {
Al Viro4324a172006-09-27 18:49:07 -0700237 __be32 *ehdr = (__be32 *)xprth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239 fl->fl_ipsec_spi = ehdr[0];
240 }
241 break;
242
243 case IPPROTO_AH:
244 if (pskb_may_pull(skb, xprth + 8 - skb->data)) {
Al Viro4324a172006-09-27 18:49:07 -0700245 __be32 *ah_hdr = (__be32*)xprth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247 fl->fl_ipsec_spi = ah_hdr[1];
248 }
249 break;
250
251 case IPPROTO_COMP:
252 if (pskb_may_pull(skb, xprth + 4 - skb->data)) {
Al Viro4324a172006-09-27 18:49:07 -0700253 __be16 *ipcomp_hdr = (__be16 *)xprth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Alexey Dobriyan4195f812006-05-22 16:53:22 -0700255 fl->fl_ipsec_spi = htonl(ntohs(ipcomp_hdr[1]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 }
257 break;
258 default:
259 fl->fl_ipsec_spi = 0;
260 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 }
263 fl->proto = iph->protocol;
264 fl->fl4_dst = iph->daddr;
265 fl->fl4_src = iph->saddr;
Herbert Xu4da30892006-02-23 16:19:26 -0800266 fl->fl4_tos = iph->tos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268
269static inline int xfrm4_garbage_collect(void)
270{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 xfrm4_policy_afinfo.garbage_collect();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 return (atomic_read(&xfrm4_dst_ops.entries) > xfrm4_dst_ops.gc_thresh*2);
273}
274
275static void xfrm4_update_pmtu(struct dst_entry *dst, u32 mtu)
276{
277 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
278 struct dst_entry *path = xdst->route;
279
280 path->ops->update_pmtu(path, mtu);
281}
282
Herbert Xuaabc9762005-05-03 16:27:10 -0700283static void xfrm4_dst_destroy(struct dst_entry *dst)
284{
285 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
286
287 if (likely(xdst->u.rt.idev))
288 in_dev_put(xdst->u.rt.idev);
Herbert Xued3e37d2007-10-17 21:34:46 -0700289 if (likely(xdst->u.rt.peer))
David S. Miller26db1672006-12-06 23:45:15 -0800290 inet_putpeer(xdst->u.rt.peer);
Herbert Xuaabc9762005-05-03 16:27:10 -0700291 xfrm_dst_destroy(xdst);
292}
293
294static void xfrm4_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
295 int unregister)
296{
297 struct xfrm_dst *xdst;
298
299 if (!unregister)
300 return;
301
302 xdst = (struct xfrm_dst *)dst;
303 if (xdst->u.rt.idev->dev == dev) {
Eric W. Biederman2774c7a2007-09-26 22:10:56 -0700304 struct in_device *loopback_idev = in_dev_get(init_net.loopback_dev);
Herbert Xuaabc9762005-05-03 16:27:10 -0700305 BUG_ON(!loopback_idev);
306
307 do {
308 in_dev_put(xdst->u.rt.idev);
309 xdst->u.rt.idev = loopback_idev;
310 in_dev_hold(loopback_idev);
311 xdst = (struct xfrm_dst *)xdst->u.dst.child;
312 } while (xdst->u.dst.xfrm);
313
314 __in_dev_put(loopback_idev);
315 }
316
317 xfrm_dst_ifdown(dst, dev);
318}
319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320static struct dst_ops xfrm4_dst_ops = {
321 .family = AF_INET,
322 .protocol = __constant_htons(ETH_P_IP),
323 .gc = xfrm4_garbage_collect,
324 .update_pmtu = xfrm4_update_pmtu,
Herbert Xuaabc9762005-05-03 16:27:10 -0700325 .destroy = xfrm4_dst_destroy,
326 .ifdown = xfrm4_dst_ifdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 .gc_thresh = 1024,
328 .entry_size = sizeof(struct xfrm_dst),
329};
330
331static struct xfrm_policy_afinfo xfrm4_policy_afinfo = {
332 .family = AF_INET,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 .dst_ops = &xfrm4_dst_ops,
334 .dst_lookup = xfrm4_dst_lookup,
Patrick McHardya1e59ab2006-09-19 12:57:34 -0700335 .get_saddr = xfrm4_get_saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 .find_bundle = __xfrm4_find_bundle,
337 .bundle_create = __xfrm4_bundle_create,
338 .decode_session = _decode_session4,
339};
340
341static void __init xfrm4_policy_init(void)
342{
343 xfrm_policy_register_afinfo(&xfrm4_policy_afinfo);
344}
345
346static void __exit xfrm4_policy_fini(void)
347{
348 xfrm_policy_unregister_afinfo(&xfrm4_policy_afinfo);
349}
350
351void __init xfrm4_init(void)
352{
353 xfrm4_state_init();
354 xfrm4_policy_init();
355}
356