blob: 254aa6d795063187b88e266eaaa8ab143344cddc [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
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090011 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 */
13
Herbert Xu66cdb3c2007-11-13 21:37:28 -080014#include <linux/err.h>
15#include <linux/kernel.h>
Herbert Xuaabc9762005-05-03 16:27:10 -070016#include <linux/netdevice.h>
17#include <net/addrconf.h>
Herbert Xu45ff5a32007-11-13 21:35:32 -080018#include <net/dst.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>
Masahide NAKAMURA59fbb3a2007-06-26 23:56:32 -070023#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
Masahide NAKAMURA2ce42722006-08-23 20:39:03 -070024#include <net/mip6.h>
25#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Linus Torvalds1da177e2005-04-16 15:20:36 -070027static struct xfrm_policy_afinfo xfrm6_policy_afinfo;
28
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -080029static struct dst_entry *xfrm6_dst_lookup(struct net *net, int tos,
David S. Miller5e6b9302011-02-24 00:14:45 -050030 const xfrm_address_t *saddr,
31 const xfrm_address_t *daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032{
David S. Miller7e1dc7b2011-03-12 02:42:11 -050033 struct flowi6 fl6;
Herbert Xu66cdb3c2007-11-13 21:37:28 -080034 struct dst_entry *dst;
35 int err;
36
David S. Miller7e1dc7b2011-03-12 02:42:11 -050037 memset(&fl6, 0, sizeof(fl6));
38 memcpy(&fl6.daddr, daddr, sizeof(fl6.daddr));
Herbert Xu66cdb3c2007-11-13 21:37:28 -080039 if (saddr)
David S. Miller7e1dc7b2011-03-12 02:42:11 -050040 memcpy(&fl6.saddr, saddr, sizeof(fl6.saddr));
Herbert Xu66cdb3c2007-11-13 21:37:28 -080041
David S. Miller7e1dc7b2011-03-12 02:42:11 -050042 dst = ip6_route_output(net, NULL,
43 flowi6_to_flowi(&fl6));
Herbert Xu66cdb3c2007-11-13 21:37:28 -080044
45 err = dst->error;
46 if (dst->error) {
Ville Nuorvala42513202006-10-16 22:10:05 -070047 dst_release(dst);
Herbert Xu66cdb3c2007-11-13 21:37:28 -080048 dst = ERR_PTR(err);
49 }
50
51 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052}
53
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -080054static int xfrm6_get_saddr(struct net *net,
55 xfrm_address_t *saddr, xfrm_address_t *daddr)
Patrick McHardya1e59ab2006-09-19 12:57:34 -070056{
Herbert Xu66cdb3c2007-11-13 21:37:28 -080057 struct dst_entry *dst;
Brian Haley191cd582008-08-14 15:33:21 -070058 struct net_device *dev;
Patrick McHardya1e59ab2006-09-19 12:57:34 -070059
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -080060 dst = xfrm6_dst_lookup(net, 0, NULL, daddr);
Herbert Xu66cdb3c2007-11-13 21:37:28 -080061 if (IS_ERR(dst))
62 return -EHOSTUNREACH;
63
Brian Haley191cd582008-08-14 15:33:21 -070064 dev = ip6_dst_idev(dst)->dev;
65 ipv6_dev_get_saddr(dev_net(dev), dev,
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +090066 (struct in6_addr *)&daddr->a6, 0,
YOSHIFUJI Hideaki5e5f3f02008-03-03 21:44:34 +090067 (struct in6_addr *)&saddr->a6);
Herbert Xu66cdb3c2007-11-13 21:37:28 -080068 dst_release(dst);
69 return 0;
Patrick McHardya1e59ab2006-09-19 12:57:34 -070070}
71
David S. Miller05d84022011-02-22 17:47:10 -080072static int xfrm6_get_tos(const struct flowi *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 return 0;
Herbert Xu25ee3282007-12-11 09:32:34 -080075}
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -080077static int xfrm6_init_path(struct xfrm_dst *path, struct dst_entry *dst,
78 int nfheader_len)
79{
80 if (dst->ops->family == AF_INET6) {
81 struct rt6_info *rt = (struct rt6_info*)dst;
82 if (rt->rt6i_node)
83 path->path_cookie = rt->rt6i_node->fn_sernum;
84 }
85
86 path->u.rt6.rt6i_nfheader_len = nfheader_len;
87
88 return 0;
89}
90
Herbert Xu87c1e122010-03-02 02:51:56 +000091static int xfrm6_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
David S. Miller0c7b3ee2011-02-22 17:48:57 -080092 const struct flowi *fl)
Herbert Xu25ee3282007-12-11 09:32:34 -080093{
94 struct rt6_info *rt = (struct rt6_info*)xdst->route;
95
96 xdst->u.dst.dev = dev;
97 dev_hold(dev);
98
Nicolas Dichtelbc8e4b92010-04-21 16:25:30 -070099 xdst->u.rt6.rt6i_idev = in6_dev_get(dev);
Herbert Xu25ee3282007-12-11 09:32:34 -0800100 if (!xdst->u.rt6.rt6i_idev)
101 return -ENODEV;
102
David S. Miller7cc2edb2011-01-26 13:41:03 -0800103 xdst->u.rt6.rt6i_peer = rt->rt6i_peer;
104 if (rt->rt6i_peer)
105 atomic_inc(&rt->rt6i_peer->refcnt);
106
Herbert Xu25ee3282007-12-11 09:32:34 -0800107 /* Sheit... I remember I did this right. Apparently,
108 * it was magically lost, so this code needs audit */
109 xdst->u.rt6.rt6i_flags = rt->rt6i_flags & (RTF_ANYCAST |
110 RTF_LOCAL);
111 xdst->u.rt6.rt6i_metric = rt->rt6i_metric;
112 xdst->u.rt6.rt6i_node = rt->rt6i_node;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -0800113 if (rt->rt6i_node)
114 xdst->route_cookie = rt->rt6i_node->fn_sernum;
Herbert Xu25ee3282007-12-11 09:32:34 -0800115 xdst->u.rt6.rt6i_gateway = rt->rt6i_gateway;
116 xdst->u.rt6.rt6i_dst = rt->rt6i_dst;
117 xdst->u.rt6.rt6i_src = rt->rt6i_src;
118
119 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
122static inline void
Herbert Xud5422ef2007-12-12 10:44:16 -0800123_decode_session6(struct sk_buff *skb, struct flowi *fl, int reverse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
David S. Miller7e1dc7b2011-03-12 02:42:11 -0500125 struct flowi6 *fl6 = &fl->u.ip6;
Nicolas Dichtel7e3a42a2008-11-01 21:12:07 -0700126 int onlyproto = 0;
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300127 u16 offset = skb_network_header_len(skb);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700128 struct ipv6hdr *hdr = ipv6_hdr(skb);
YOSHIFUJI Hideakie3cae902006-04-18 14:46:52 -0700129 struct ipv6_opt_hdr *exthdr;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700130 const unsigned char *nh = skb_network_header(skb);
131 u8 nexthdr = nh[IP6CB(skb)->nhoff];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
David S. Miller7e1dc7b2011-03-12 02:42:11 -0500133 memset(fl6, 0, sizeof(struct flowi6));
134 fl6->flowi6_mark = skb->mark;
Peter Kosyh44b451f2010-07-02 07:47:55 +0000135
David S. Miller7e1dc7b2011-03-12 02:42:11 -0500136 ipv6_addr_copy(&fl6->daddr, reverse ? &hdr->saddr : &hdr->daddr);
137 ipv6_addr_copy(&fl6->saddr, reverse ? &hdr->daddr : &hdr->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Wei Yongjun59cae002009-07-02 16:59:49 +0000139 while (nh + offset + 1 < skb->data ||
140 pskb_may_pull(skb, nh + offset + 1 - skb->data)) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700141 nh = skb_network_header(skb);
142 exthdr = (struct ipv6_opt_hdr *)(nh + offset);
YOSHIFUJI Hideakie3cae902006-04-18 14:46:52 -0700143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 switch (nexthdr) {
Nicolas Dichtel7e3a42a2008-11-01 21:12:07 -0700145 case NEXTHDR_FRAGMENT:
146 onlyproto = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 case NEXTHDR_ROUTING:
148 case NEXTHDR_HOP:
149 case NEXTHDR_DEST:
150 offset += ipv6_optlen(exthdr);
151 nexthdr = exthdr->nexthdr;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700152 exthdr = (struct ipv6_opt_hdr *)(nh + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 break;
154
155 case IPPROTO_UDP:
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800156 case IPPROTO_UDPLITE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 case IPPROTO_TCP:
158 case IPPROTO_SCTP:
Patrick McHardy9e999992005-12-19 14:03:46 -0800159 case IPPROTO_DCCP:
Wei Yongjun59cae002009-07-02 16:59:49 +0000160 if (!onlyproto && (nh + offset + 4 < skb->data ||
161 pskb_may_pull(skb, nh + offset + 4 - skb->data))) {
Al Viro8c689a62006-11-08 00:20:21 -0800162 __be16 *ports = (__be16 *)exthdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
David S. Miller7e1dc7b2011-03-12 02:42:11 -0500164 fl6->uli.ports.sport = ports[!!reverse];
165 fl6->uli.ports.dport = ports[!reverse];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 }
David S. Miller7e1dc7b2011-03-12 02:42:11 -0500167 fl6->flowi6_proto = nexthdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 return;
169
170 case IPPROTO_ICMPV6:
Nicolas Dichtel7e3a42a2008-11-01 21:12:07 -0700171 if (!onlyproto && pskb_may_pull(skb, nh + offset + 2 - skb->data)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 u8 *icmp = (u8 *)exthdr;
173
David S. Miller7e1dc7b2011-03-12 02:42:11 -0500174 fl6->uli.icmpt.type = icmp[0];
175 fl6->uli.icmpt.code = icmp[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 }
David S. Miller7e1dc7b2011-03-12 02:42:11 -0500177 fl6->flowi6_proto = nexthdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 return;
179
Masahide NAKAMURA59fbb3a2007-06-26 23:56:32 -0700180#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
Masahide NAKAMURA2ce42722006-08-23 20:39:03 -0700181 case IPPROTO_MH:
Nicolas Dichtel7e3a42a2008-11-01 21:12:07 -0700182 if (!onlyproto && pskb_may_pull(skb, nh + offset + 3 - skb->data)) {
Masahide NAKAMURA2ce42722006-08-23 20:39:03 -0700183 struct ip6_mh *mh;
184 mh = (struct ip6_mh *)exthdr;
185
David S. Miller7e1dc7b2011-03-12 02:42:11 -0500186 fl6->uli.mht.type = mh->ip6mh_type;
Masahide NAKAMURA2ce42722006-08-23 20:39:03 -0700187 }
David S. Miller7e1dc7b2011-03-12 02:42:11 -0500188 fl6->flowi6_proto = nexthdr;
Masahide NAKAMURA2ce42722006-08-23 20:39:03 -0700189 return;
190#endif
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 /* XXX Why are there these headers? */
193 case IPPROTO_AH:
194 case IPPROTO_ESP:
195 case IPPROTO_COMP:
196 default:
David S. Miller7e1dc7b2011-03-12 02:42:11 -0500197 fl6->uli.spi = 0;
198 fl6->flowi6_proto = nexthdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 return;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 }
202}
203
Daniel Lezcano569d3642008-01-18 03:56:57 -0800204static inline int xfrm6_garbage_collect(struct dst_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
Alexey Dobriyand7c75442010-01-24 22:47:53 -0800206 struct net *net = container_of(ops, struct net, xfrm.xfrm6_dst_ops);
207
208 xfrm6_policy_afinfo.garbage_collect(net);
Eric Dumazetfc66f952010-10-08 06:37:34 +0000209 return dst_entries_get_fast(ops) > ops->gc_thresh * 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
211
212static void xfrm6_update_pmtu(struct dst_entry *dst, u32 mtu)
213{
214 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
215 struct dst_entry *path = xdst->route;
216
217 path->ops->update_pmtu(path, mtu);
218}
219
Herbert Xuaabc9762005-05-03 16:27:10 -0700220static void xfrm6_dst_destroy(struct dst_entry *dst)
221{
222 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
223
224 if (likely(xdst->u.rt6.rt6i_idev))
225 in6_dev_put(xdst->u.rt6.rt6i_idev);
David S. Miller62fa8a82011-01-26 20:51:05 -0800226 dst_destroy_metrics_generic(dst);
David S. Miller7cc2edb2011-01-26 13:41:03 -0800227 if (likely(xdst->u.rt6.rt6i_peer))
228 inet_putpeer(xdst->u.rt6.rt6i_peer);
Herbert Xuaabc9762005-05-03 16:27:10 -0700229 xfrm_dst_destroy(xdst);
230}
231
232static void xfrm6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
233 int unregister)
234{
235 struct xfrm_dst *xdst;
236
237 if (!unregister)
238 return;
239
240 xdst = (struct xfrm_dst *)dst;
241 if (xdst->u.rt6.rt6i_idev->dev == dev) {
Denis V. Lunev5a3e55d2007-12-07 00:38:10 -0800242 struct inet6_dev *loopback_idev =
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900243 in6_dev_get(dev_net(dev)->loopback_dev);
Herbert Xuaabc9762005-05-03 16:27:10 -0700244 BUG_ON(!loopback_idev);
245
246 do {
247 in6_dev_put(xdst->u.rt6.rt6i_idev);
248 xdst->u.rt6.rt6i_idev = loopback_idev;
249 in6_dev_hold(loopback_idev);
250 xdst = (struct xfrm_dst *)xdst->u.dst.child;
251 } while (xdst->u.dst.xfrm);
252
253 __in6_dev_put(loopback_idev);
254 }
255
256 xfrm_dst_ifdown(dst, dev);
257}
258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259static struct dst_ops xfrm6_dst_ops = {
260 .family = AF_INET6,
Harvey Harrison09640e62009-02-01 00:45:17 -0800261 .protocol = cpu_to_be16(ETH_P_IPV6),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 .gc = xfrm6_garbage_collect,
263 .update_pmtu = xfrm6_update_pmtu,
David S. Miller62fa8a82011-01-26 20:51:05 -0800264 .cow_metrics = dst_cow_metrics_generic,
Herbert Xuaabc9762005-05-03 16:27:10 -0700265 .destroy = xfrm6_dst_destroy,
266 .ifdown = xfrm6_dst_ifdown,
Herbert Xu862b82c2007-11-13 21:43:11 -0800267 .local_out = __ip6_local_out,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 .gc_thresh = 1024,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269};
270
271static struct xfrm_policy_afinfo xfrm6_policy_afinfo = {
272 .family = AF_INET6,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 .dst_ops = &xfrm6_dst_ops,
274 .dst_lookup = xfrm6_dst_lookup,
Patrick McHardya1e59ab2006-09-19 12:57:34 -0700275 .get_saddr = xfrm6_get_saddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 .decode_session = _decode_session6,
Herbert Xu25ee3282007-12-11 09:32:34 -0800277 .get_tos = xfrm6_get_tos,
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -0800278 .init_path = xfrm6_init_path,
Herbert Xu25ee3282007-12-11 09:32:34 -0800279 .fill_dst = xfrm6_fill_dst,
David S. Miller2774c132011-03-01 14:59:04 -0800280 .blackhole_route = ip6_blackhole_route,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281};
282
Daniel Lezcano0013cab2007-12-07 00:42:11 -0800283static int __init xfrm6_policy_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
Daniel Lezcano0013cab2007-12-07 00:42:11 -0800285 return xfrm_policy_register_afinfo(&xfrm6_policy_afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286}
287
288static void xfrm6_policy_fini(void)
289{
290 xfrm_policy_unregister_afinfo(&xfrm6_policy_afinfo);
291}
292
David S. Millerdb717892009-08-04 20:32:16 -0700293#ifdef CONFIG_SYSCTL
Neil Hormana44a4a02009-07-27 08:22:46 +0000294static struct ctl_table xfrm6_policy_table[] = {
295 {
Neil Hormana44a4a02009-07-27 08:22:46 +0000296 .procname = "xfrm6_gc_thresh",
Alexey Dobriyand7c75442010-01-24 22:47:53 -0800297 .data = &init_net.xfrm.xfrm6_dst_ops.gc_thresh,
Neil Hormana44a4a02009-07-27 08:22:46 +0000298 .maxlen = sizeof(int),
299 .mode = 0644,
300 .proc_handler = proc_dointvec,
301 },
302 { }
303};
304
305static struct ctl_table_header *sysctl_hdr;
David S. Millerdb717892009-08-04 20:32:16 -0700306#endif
Neil Hormana44a4a02009-07-27 08:22:46 +0000307
Daniel Lezcano0013cab2007-12-07 00:42:11 -0800308int __init xfrm6_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
Daniel Lezcano0013cab2007-12-07 00:42:11 -0800310 int ret;
Neil Hormana33bc5c2009-07-30 18:52:15 -0700311 unsigned int gc_thresh;
Daniel Lezcano0013cab2007-12-07 00:42:11 -0800312
Neil Hormana33bc5c2009-07-30 18:52:15 -0700313 /*
314 * We need a good default value for the xfrm6 gc threshold.
315 * In ipv4 we set it to the route hash table size * 8, which
316 * is half the size of the maximaum route cache for ipv4. It
317 * would be good to do the same thing for v6, except the table is
318 * constructed differently here. Here each table for a net namespace
319 * can have FIB_TABLE_HASHSZ entries, so lets go with the same
320 * computation that we used for ipv4 here. Also, lets keep the initial
321 * gc_thresh to a minimum of 1024, since, the ipv6 route cache defaults
322 * to that as a minimum as well
323 */
324 gc_thresh = FIB6_TABLE_HASHSZ * 8;
325 xfrm6_dst_ops.gc_thresh = (gc_thresh < 1024) ? 1024 : gc_thresh;
Eric Dumazetfc66f952010-10-08 06:37:34 +0000326 dst_entries_init(&xfrm6_dst_ops);
Alexey Dobriyand7c75442010-01-24 22:47:53 -0800327
328 ret = xfrm6_policy_init();
Eric Dumazetfc66f952010-10-08 06:37:34 +0000329 if (ret) {
330 dst_entries_destroy(&xfrm6_dst_ops);
Alexey Dobriyand7c75442010-01-24 22:47:53 -0800331 goto out;
Eric Dumazetfc66f952010-10-08 06:37:34 +0000332 }
Alexey Dobriyand7c75442010-01-24 22:47:53 -0800333 ret = xfrm6_state_init();
334 if (ret)
335 goto out_policy;
336
David S. Millerdb717892009-08-04 20:32:16 -0700337#ifdef CONFIG_SYSCTL
Neil Hormana44a4a02009-07-27 08:22:46 +0000338 sysctl_hdr = register_net_sysctl_table(&init_net, net_ipv6_ctl_path,
339 xfrm6_policy_table);
David S. Millerdb717892009-08-04 20:32:16 -0700340#endif
Daniel Lezcano0013cab2007-12-07 00:42:11 -0800341out:
342 return ret;
343out_policy:
344 xfrm6_policy_fini();
345 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}
347
348void xfrm6_fini(void)
349{
David S. Millerdb717892009-08-04 20:32:16 -0700350#ifdef CONFIG_SYSCTL
Neil Hormana44a4a02009-07-27 08:22:46 +0000351 if (sysctl_hdr)
352 unregister_net_sysctl_table(sysctl_hdr);
David S. Millerdb717892009-08-04 20:32:16 -0700353#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 //xfrm6_input_fini();
355 xfrm6_policy_fini();
356 xfrm6_state_fini();
Eric Dumazetfc66f952010-10-08 06:37:34 +0000357 dst_entries_destroy(&xfrm6_dst_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358}