blob: cebc847319692ac9f5f486de074516ffaf8459c1 [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 Xu66cdb3c2007-11-13 21:37:28 -080011#include <linux/err.h>
12#include <linux/kernel.h>
Herbert Xuaabc9762005-05-03 16:27:10 -070013#include <linux/inetdevice.h>
Herbert Xu45ff5a32007-11-13 21:35:32 -080014#include <net/dst.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <net/xfrm.h>
16#include <net/ip.h>
17
18static struct dst_ops xfrm4_dst_ops;
19static struct xfrm_policy_afinfo xfrm4_policy_afinfo;
20
Herbert Xu66cdb3c2007-11-13 21:37:28 -080021static struct dst_entry *xfrm4_dst_lookup(int tos, xfrm_address_t *saddr,
22 xfrm_address_t *daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070023{
Herbert Xu66cdb3c2007-11-13 21:37:28 -080024 struct flowi fl = {
Patrick McHardya1e59ab2006-09-19 12:57:34 -070025 .nl_u = {
26 .ip4_u = {
Herbert Xu66cdb3c2007-11-13 21:37:28 -080027 .tos = tos,
Patrick McHardya1e59ab2006-09-19 12:57:34 -070028 .daddr = daddr->a4,
29 },
30 },
31 };
Herbert Xu66cdb3c2007-11-13 21:37:28 -080032 struct dst_entry *dst;
33 struct rtable *rt;
34 int err;
Patrick McHardya1e59ab2006-09-19 12:57:34 -070035
Herbert Xu66cdb3c2007-11-13 21:37:28 -080036 if (saddr)
37 fl.fl4_src = saddr->a4;
38
39 err = __ip_route_output_key(&rt, &fl);
40 dst = &rt->u.dst;
41 if (err)
42 dst = ERR_PTR(err);
43 return dst;
44}
45
46static int xfrm4_get_saddr(xfrm_address_t *saddr, xfrm_address_t *daddr)
47{
48 struct dst_entry *dst;
49 struct rtable *rt;
50
51 dst = xfrm4_dst_lookup(0, NULL, daddr);
52 if (IS_ERR(dst))
53 return -EHOSTUNREACH;
54
55 rt = (struct rtable *)dst;
56 saddr->a4 = rt->rt_src;
57 dst_release(dst);
58 return 0;
Patrick McHardya1e59ab2006-09-19 12:57:34 -070059}
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061static struct dst_entry *
62__xfrm4_find_bundle(struct flowi *fl, struct xfrm_policy *policy)
63{
64 struct dst_entry *dst;
65
66 read_lock_bh(&policy->lock);
67 for (dst = policy->bundles; dst; dst = dst->next) {
68 struct xfrm_dst *xdst = (struct xfrm_dst*)dst;
69 if (xdst->u.rt.fl.oif == fl->oif && /*XXX*/
70 xdst->u.rt.fl.fl4_dst == fl->fl4_dst &&
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090071 xdst->u.rt.fl.fl4_src == fl->fl4_src &&
72 xdst->u.rt.fl.fl4_tos == fl->fl4_tos &&
Venkat Yekkirala5b368e62006-10-05 15:42:18 -050073 xfrm_bundle_ok(policy, xdst, fl, AF_INET, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 dst_clone(dst);
75 break;
76 }
77 }
78 read_unlock_bh(&policy->lock);
79 return dst;
80}
81
82/* Allocate chain of dst_entry's, attach known xfrm's, calculate
83 * all the metrics... Shortly, bundle a bundle.
84 */
85
86static int
87__xfrm4_bundle_create(struct xfrm_policy *policy, struct xfrm_state **xfrm, int nx,
88 struct flowi *fl, struct dst_entry **dst_p)
89{
90 struct dst_entry *dst, *dst_prev;
91 struct rtable *rt0 = (struct rtable*)(*dst_p);
92 struct rtable *rt = rt0;
Herbert Xu66cdb3c2007-11-13 21:37:28 -080093 int tos = fl->fl4_tos;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 int i;
95 int err;
96 int header_len = 0;
97 int trailer_len = 0;
98
99 dst = dst_prev = NULL;
100 dst_hold(&rt->u.dst);
101
102 for (i = 0; i < nx; i++) {
103 struct dst_entry *dst1 = dst_alloc(&xfrm4_dst_ops);
104 struct xfrm_dst *xdst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106 if (unlikely(dst1 == NULL)) {
107 err = -ENOBUFS;
108 dst_release(&rt->u.dst);
109 goto error;
110 }
111
112 if (!dst)
113 dst = dst1;
114 else {
115 dst_prev->child = dst1;
116 dst1->flags |= DST_NOHASH;
117 dst_clone(dst1);
118 }
119
120 xdst = (struct xfrm_dst *)dst1;
121 xdst->route = &rt->u.dst;
David S. Miller9d4a7062006-08-24 03:18:09 -0700122 xdst->genid = xfrm[i]->genid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 dst1->next = dst_prev;
125 dst_prev = dst1;
Miika Komu43372262007-02-06 14:27:32 -0800126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 header_len += xfrm[i]->props.header_len;
128 trailer_len += xfrm[i]->props.trailer_len;
129
Herbert Xu1bfcb102007-10-17 21:31:50 -0700130 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800131 dst1 = xfrm_dst_lookup(xfrm[i], tos);
132 err = PTR_ERR(dst1);
133 if (IS_ERR(dst1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 goto error;
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800135
136 rt = (struct rtable *)dst1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 } else
138 dst_hold(&rt->u.dst);
139 }
140
141 dst_prev->child = &rt->u.dst;
142 dst->path = &rt->u.dst;
143
Herbert Xu8ce68ce2007-11-13 21:35:01 -0800144 /* Copy neighbout for reachability confirmation */
145 dst->neighbour = neigh_clone(rt->u.dst.neighbour);
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 *dst_p = dst;
148 dst = dst_prev;
149
150 dst_prev = *dst_p;
151 i = 0;
Herbert Xufff69382007-11-13 21:36:07 -0800152 err = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 for (; dst_prev != &rt->u.dst; dst_prev = dst_prev->child) {
154 struct xfrm_dst *x = (struct xfrm_dst*)dst_prev;
155 x->u.rt.fl = *fl;
156
157 dst_prev->xfrm = xfrm[i++];
158 dst_prev->dev = rt->u.dst.dev;
Herbert Xufff69382007-11-13 21:36:07 -0800159 if (!rt->u.dst.dev)
160 goto error;
161 dev_hold(rt->u.dst.dev);
162
163 x->u.rt.idev = in_dev_get(rt->u.dst.dev);
164 if (!x->u.rt.idev)
165 goto error;
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 dst_prev->obsolete = -1;
168 dst_prev->flags |= DST_HOST;
169 dst_prev->lastuse = jiffies;
170 dst_prev->header_len = header_len;
171 dst_prev->trailer_len = trailer_len;
172 memcpy(&dst_prev->metrics, &x->route->metrics, sizeof(dst_prev->metrics));
173
Herbert Xu45ff5a32007-11-13 21:35:32 -0800174 dst_prev->input = dst_discard;
Herbert Xu13996372007-10-17 21:35:51 -0700175 dst_prev->output = dst_prev->xfrm->outer_mode->afinfo->output;
Herbert Xued3e37d2007-10-17 21:34:46 -0700176 if (rt0->peer)
177 atomic_inc(&rt0->peer->refcnt);
178 x->u.rt.peer = rt0->peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 /* Sheit... I remember I did this right. Apparently,
180 * it was magically lost, so this code needs audit */
181 x->u.rt.rt_flags = rt0->rt_flags&(RTCF_BROADCAST|RTCF_MULTICAST|RTCF_LOCAL);
Herbert Xued3e37d2007-10-17 21:34:46 -0700182 x->u.rt.rt_type = rt0->rt_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 x->u.rt.rt_src = rt0->rt_src;
184 x->u.rt.rt_dst = rt0->rt_dst;
Herbert Xued3e37d2007-10-17 21:34:46 -0700185 x->u.rt.rt_gateway = rt0->rt_gateway;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 x->u.rt.rt_spec_dst = rt0->rt_spec_dst;
187 header_len -= x->u.dst.xfrm->props.header_len;
188 trailer_len -= x->u.dst.xfrm->props.trailer_len;
189 }
190
191 xfrm_init_pmtu(dst);
192 return 0;
193
194error:
195 if (dst)
196 dst_free(dst);
197 return err;
198}
199
200static void
201_decode_session4(struct sk_buff *skb, struct flowi *fl)
202{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700203 struct iphdr *iph = ip_hdr(skb);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700204 u8 *xprth = skb_network_header(skb) + iph->ihl * 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206 memset(fl, 0, sizeof(struct flowi));
207 if (!(iph->frag_off & htons(IP_MF | IP_OFFSET))) {
208 switch (iph->protocol) {
209 case IPPROTO_UDP:
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800210 case IPPROTO_UDPLITE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 case IPPROTO_TCP:
212 case IPPROTO_SCTP:
Patrick McHardy9e999992005-12-19 14:03:46 -0800213 case IPPROTO_DCCP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 if (pskb_may_pull(skb, xprth + 4 - skb->data)) {
Al Viro8c689a62006-11-08 00:20:21 -0800215 __be16 *ports = (__be16 *)xprth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217 fl->fl_ip_sport = ports[0];
218 fl->fl_ip_dport = ports[1];
219 }
220 break;
221
222 case IPPROTO_ICMP:
223 if (pskb_may_pull(skb, xprth + 2 - skb->data)) {
224 u8 *icmp = xprth;
225
226 fl->fl_icmp_type = icmp[0];
227 fl->fl_icmp_code = icmp[1];
228 }
229 break;
230
231 case IPPROTO_ESP:
232 if (pskb_may_pull(skb, xprth + 4 - skb->data)) {
Al Viro4324a172006-09-27 18:49:07 -0700233 __be32 *ehdr = (__be32 *)xprth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235 fl->fl_ipsec_spi = ehdr[0];
236 }
237 break;
238
239 case IPPROTO_AH:
240 if (pskb_may_pull(skb, xprth + 8 - skb->data)) {
Al Viro4324a172006-09-27 18:49:07 -0700241 __be32 *ah_hdr = (__be32*)xprth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243 fl->fl_ipsec_spi = ah_hdr[1];
244 }
245 break;
246
247 case IPPROTO_COMP:
248 if (pskb_may_pull(skb, xprth + 4 - skb->data)) {
Al Viro4324a172006-09-27 18:49:07 -0700249 __be16 *ipcomp_hdr = (__be16 *)xprth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Alexey Dobriyan4195f812006-05-22 16:53:22 -0700251 fl->fl_ipsec_spi = htonl(ntohs(ipcomp_hdr[1]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
253 break;
254 default:
255 fl->fl_ipsec_spi = 0;
256 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 }
259 fl->proto = iph->protocol;
260 fl->fl4_dst = iph->daddr;
261 fl->fl4_src = iph->saddr;
Herbert Xu4da30892006-02-23 16:19:26 -0800262 fl->fl4_tos = iph->tos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263}
264
265static inline int xfrm4_garbage_collect(void)
266{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 xfrm4_policy_afinfo.garbage_collect();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return (atomic_read(&xfrm4_dst_ops.entries) > xfrm4_dst_ops.gc_thresh*2);
269}
270
271static void xfrm4_update_pmtu(struct dst_entry *dst, u32 mtu)
272{
273 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
274 struct dst_entry *path = xdst->route;
275
276 path->ops->update_pmtu(path, mtu);
277}
278
Herbert Xuaabc9762005-05-03 16:27:10 -0700279static void xfrm4_dst_destroy(struct dst_entry *dst)
280{
281 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
282
283 if (likely(xdst->u.rt.idev))
284 in_dev_put(xdst->u.rt.idev);
Herbert Xued3e37d2007-10-17 21:34:46 -0700285 if (likely(xdst->u.rt.peer))
David S. Miller26db1672006-12-06 23:45:15 -0800286 inet_putpeer(xdst->u.rt.peer);
Herbert Xuaabc9762005-05-03 16:27:10 -0700287 xfrm_dst_destroy(xdst);
288}
289
290static void xfrm4_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
291 int unregister)
292{
293 struct xfrm_dst *xdst;
294
295 if (!unregister)
296 return;
297
298 xdst = (struct xfrm_dst *)dst;
299 if (xdst->u.rt.idev->dev == dev) {
Eric W. Biederman2774c7a2007-09-26 22:10:56 -0700300 struct in_device *loopback_idev = in_dev_get(init_net.loopback_dev);
Herbert Xuaabc9762005-05-03 16:27:10 -0700301 BUG_ON(!loopback_idev);
302
303 do {
304 in_dev_put(xdst->u.rt.idev);
305 xdst->u.rt.idev = loopback_idev;
306 in_dev_hold(loopback_idev);
307 xdst = (struct xfrm_dst *)xdst->u.dst.child;
308 } while (xdst->u.dst.xfrm);
309
310 __in_dev_put(loopback_idev);
311 }
312
313 xfrm_dst_ifdown(dst, dev);
314}
315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316static struct dst_ops xfrm4_dst_ops = {
317 .family = AF_INET,
318 .protocol = __constant_htons(ETH_P_IP),
319 .gc = xfrm4_garbage_collect,
320 .update_pmtu = xfrm4_update_pmtu,
Herbert Xuaabc9762005-05-03 16:27:10 -0700321 .destroy = xfrm4_dst_destroy,
322 .ifdown = xfrm4_dst_ifdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 .gc_thresh = 1024,
324 .entry_size = sizeof(struct xfrm_dst),
325};
326
327static struct xfrm_policy_afinfo xfrm4_policy_afinfo = {
328 .family = AF_INET,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 .dst_ops = &xfrm4_dst_ops,
330 .dst_lookup = xfrm4_dst_lookup,
Patrick McHardya1e59ab2006-09-19 12:57:34 -0700331 .get_saddr = xfrm4_get_saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 .find_bundle = __xfrm4_find_bundle,
333 .bundle_create = __xfrm4_bundle_create,
334 .decode_session = _decode_session4,
335};
336
337static void __init xfrm4_policy_init(void)
338{
339 xfrm_policy_register_afinfo(&xfrm4_policy_afinfo);
340}
341
342static void __exit xfrm4_policy_fini(void)
343{
344 xfrm_policy_unregister_afinfo(&xfrm4_policy_afinfo);
345}
346
347void __init xfrm4_init(void)
348{
349 xfrm4_state_init();
350 xfrm4_policy_init();
351}
352