blob: 699e0730ce8e86545ef2812da316f8e726e4a75d [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * xfrm6_policy.c: based on xfrm4_policy.c
4 *
5 * Authors:
6 * Mitsuru KANDA @USAGI
Ian Morris67ba4152014-08-24 21:53:10 +01007 * Kazunori MIYAZAWA @USAGI
8 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
9 * IPv6 support
10 * YOSHIFUJI Hideaki
11 * Split up af-specific portion
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090012 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 */
14
Herbert Xu66cdb3c2007-11-13 21:37:28 -080015#include <linux/err.h>
16#include <linux/kernel.h>
Herbert Xuaabc9762005-05-03 16:27:10 -070017#include <linux/netdevice.h>
18#include <net/addrconf.h>
Herbert Xu45ff5a32007-11-13 21:35:32 -080019#include <net/dst.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <net/xfrm.h>
21#include <net/ip.h>
22#include <net/ipv6.h>
23#include <net/ip6_route.h>
David Ahern385add92015-09-29 20:07:13 -070024#include <net/l3mdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
David Ahern42a7b322015-08-10 16:58:11 -060026static struct dst_entry *xfrm6_dst_lookup(struct net *net, int tos, int oif,
David S. Miller5e6b9302011-02-24 00:14:45 -050027 const xfrm_address_t *saddr,
Lorenzo Colitti077fbac2017-08-11 02:11:33 +090028 const xfrm_address_t *daddr,
29 u32 mark)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030{
David S. Miller7e1dc7b2011-03-12 02:42:11 -050031 struct flowi6 fl6;
Herbert Xu66cdb3c2007-11-13 21:37:28 -080032 struct dst_entry *dst;
33 int err;
34
David S. Miller7e1dc7b2011-03-12 02:42:11 -050035 memset(&fl6, 0, sizeof(fl6));
David Ahern11d7a0b2016-08-14 19:52:56 -070036 fl6.flowi6_oif = l3mdev_master_ifindex_by_index(net, oif);
David Ahern41489872015-10-05 08:32:51 -060037 fl6.flowi6_flags = FLOWI_FLAG_SKIP_NH_OIF;
Lorenzo Colitti077fbac2017-08-11 02:11:33 +090038 fl6.flowi6_mark = mark;
David S. Miller7e1dc7b2011-03-12 02:42:11 -050039 memcpy(&fl6.daddr, daddr, sizeof(fl6.daddr));
Herbert Xu66cdb3c2007-11-13 21:37:28 -080040 if (saddr)
David S. Miller7e1dc7b2011-03-12 02:42:11 -050041 memcpy(&fl6.saddr, saddr, sizeof(fl6.saddr));
Herbert Xu66cdb3c2007-11-13 21:37:28 -080042
David S. Miller4c9483b2011-03-12 16:22:43 -050043 dst = ip6_route_output(net, NULL, &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
David Ahern42a7b322015-08-10 16:58:11 -060054static int xfrm6_get_saddr(struct net *net, int oif,
Lorenzo Colitti077fbac2017-08-11 02:11:33 +090055 xfrm_address_t *saddr, xfrm_address_t *daddr,
56 u32 mark)
Patrick McHardya1e59ab2006-09-19 12:57:34 -070057{
Herbert Xu66cdb3c2007-11-13 21:37:28 -080058 struct dst_entry *dst;
Brian Haley191cd582008-08-14 15:33:21 -070059 struct net_device *dev;
Patrick McHardya1e59ab2006-09-19 12:57:34 -070060
Lorenzo Colitti077fbac2017-08-11 02:11:33 +090061 dst = xfrm6_dst_lookup(net, 0, oif, NULL, daddr, mark);
Herbert Xu66cdb3c2007-11-13 21:37:28 -080062 if (IS_ERR(dst))
63 return -EHOSTUNREACH;
64
Brian Haley191cd582008-08-14 15:33:21 -070065 dev = ip6_dst_idev(dst)->dev;
Jiri Benc15e318b2015-03-29 16:59:24 +020066 ipv6_dev_get_saddr(dev_net(dev), dev, &daddr->in6, 0, &saddr->in6);
Herbert Xu66cdb3c2007-11-13 21:37:28 -080067 dst_release(dst);
68 return 0;
Patrick McHardya1e59ab2006-09-19 12:57:34 -070069}
70
Herbert Xu87c1e122010-03-02 02:51:56 +000071static int xfrm6_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
David S. Miller0c7b3ee2011-02-22 17:48:57 -080072 const struct flowi *fl)
Herbert Xu25ee3282007-12-11 09:32:34 -080073{
Ian Morris67ba4152014-08-24 21:53:10 +010074 struct rt6_info *rt = (struct rt6_info *)xdst->route;
Herbert Xu25ee3282007-12-11 09:32:34 -080075
76 xdst->u.dst.dev = dev;
77 dev_hold(dev);
78
Nicolas Dichtelbc8e4b92010-04-21 16:25:30 -070079 xdst->u.rt6.rt6i_idev = in6_dev_get(dev);
Cong Wang84c4a9d2013-05-09 22:40:00 +000080 if (!xdst->u.rt6.rt6i_idev) {
81 dev_put(dev);
Herbert Xu25ee3282007-12-11 09:32:34 -080082 return -ENODEV;
Cong Wang84c4a9d2013-05-09 22:40:00 +000083 }
Herbert Xu25ee3282007-12-11 09:32:34 -080084
85 /* Sheit... I remember I did this right. Apparently,
86 * it was magically lost, so this code needs audit */
87 xdst->u.rt6.rt6i_flags = rt->rt6i_flags & (RTF_ANYCAST |
88 RTF_LOCAL);
Martin KaFai Laub197df42015-05-22 20:56:01 -070089 xdst->route_cookie = rt6_get_cookie(rt);
Herbert Xu25ee3282007-12-11 09:32:34 -080090 xdst->u.rt6.rt6i_gateway = rt->rt6i_gateway;
91 xdst->u.rt6.rt6i_dst = rt->rt6i_dst;
92 xdst->u.rt6.rt6i_src = rt->rt6i_src;
Xin Long510c3212018-02-14 19:06:02 +080093 INIT_LIST_HEAD(&xdst->u.rt6.rt6i_uncached);
94 rt6_uncached_list_add(&xdst->u.rt6);
95 atomic_inc(&dev_net(dev)->ipv6.rt6_stats->fib_rt_uncache);
Herbert Xu25ee3282007-12-11 09:32:34 -080096
97 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098}
99
David S. Miller6700c272012-07-17 03:29:28 -0700100static void xfrm6_update_pmtu(struct dst_entry *dst, struct sock *sk,
101 struct sk_buff *skb, u32 mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
103 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
104 struct dst_entry *path = xdst->route;
105
David S. Miller6700c272012-07-17 03:29:28 -0700106 path->ops->update_pmtu(path, sk, skb, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
David S. Miller6700c272012-07-17 03:29:28 -0700109static void xfrm6_redirect(struct dst_entry *dst, struct sock *sk,
110 struct sk_buff *skb)
David S. Millerec18d9a2012-07-12 00:25:15 -0700111{
112 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
113 struct dst_entry *path = xdst->route;
114
David S. Miller6700c272012-07-17 03:29:28 -0700115 path->ops->redirect(path, sk, skb);
David S. Millerec18d9a2012-07-12 00:25:15 -0700116}
117
Herbert Xuaabc9762005-05-03 16:27:10 -0700118static void xfrm6_dst_destroy(struct dst_entry *dst)
119{
120 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
121
122 if (likely(xdst->u.rt6.rt6i_idev))
123 in6_dev_put(xdst->u.rt6.rt6i_idev);
David S. Miller62fa8a82011-01-26 20:51:05 -0800124 dst_destroy_metrics_generic(dst);
Xin Long510c3212018-02-14 19:06:02 +0800125 if (xdst->u.rt6.rt6i_uncached_list)
126 rt6_uncached_list_del(&xdst->u.rt6);
Herbert Xuaabc9762005-05-03 16:27:10 -0700127 xfrm_dst_destroy(xdst);
128}
129
130static void xfrm6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
131 int unregister)
132{
133 struct xfrm_dst *xdst;
134
135 if (!unregister)
136 return;
137
138 xdst = (struct xfrm_dst *)dst;
139 if (xdst->u.rt6.rt6i_idev->dev == dev) {
Denis V. Lunev5a3e55d2007-12-07 00:38:10 -0800140 struct inet6_dev *loopback_idev =
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900141 in6_dev_get(dev_net(dev)->loopback_dev);
Herbert Xuaabc9762005-05-03 16:27:10 -0700142
143 do {
144 in6_dev_put(xdst->u.rt6.rt6i_idev);
145 xdst->u.rt6.rt6i_idev = loopback_idev;
146 in6_dev_hold(loopback_idev);
David Millerb92cf4a2017-11-28 15:40:22 -0500147 xdst = (struct xfrm_dst *)xfrm_dst_child(&xdst->u.dst);
Herbert Xuaabc9762005-05-03 16:27:10 -0700148 } while (xdst->u.dst.xfrm);
149
150 __in6_dev_put(loopback_idev);
151 }
152
153 xfrm_dst_ifdown(dst, dev);
154}
155
Dan Streetmana8a572a2015-10-29 09:51:16 -0400156static struct dst_ops xfrm6_dst_ops_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 .family = AF_INET6,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 .update_pmtu = xfrm6_update_pmtu,
David S. Millerec18d9a2012-07-12 00:25:15 -0700159 .redirect = xfrm6_redirect,
David S. Miller62fa8a82011-01-26 20:51:05 -0800160 .cow_metrics = dst_cow_metrics_generic,
Herbert Xuaabc9762005-05-03 16:27:10 -0700161 .destroy = xfrm6_dst_destroy,
162 .ifdown = xfrm6_dst_ifdown,
Herbert Xu862b82c2007-11-13 21:43:11 -0800163 .local_out = __ip6_local_out,
Florian Westphal3c2a89d2017-07-17 13:57:20 +0200164 .gc_thresh = 32768,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165};
166
Florian Westphal37b10382017-02-07 15:00:19 +0100167static const struct xfrm_policy_afinfo xfrm6_policy_afinfo = {
Dan Streetmana8a572a2015-10-29 09:51:16 -0400168 .dst_ops = &xfrm6_dst_ops_template,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 .dst_lookup = xfrm6_dst_lookup,
Ian Morris67ba4152014-08-24 21:53:10 +0100170 .get_saddr = xfrm6_get_saddr,
Herbert Xu25ee3282007-12-11 09:32:34 -0800171 .fill_dst = xfrm6_fill_dst,
David S. Miller2774c132011-03-01 14:59:04 -0800172 .blackhole_route = ip6_blackhole_route,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173};
174
Daniel Lezcano0013cab2007-12-07 00:42:11 -0800175static int __init xfrm6_policy_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Florian Westphala2817d82017-02-07 15:00:17 +0100177 return xfrm_policy_register_afinfo(&xfrm6_policy_afinfo, AF_INET6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
180static void xfrm6_policy_fini(void)
181{
182 xfrm_policy_unregister_afinfo(&xfrm6_policy_afinfo);
183}
184
David S. Millerdb717892009-08-04 20:32:16 -0700185#ifdef CONFIG_SYSCTL
Neil Hormana44a4a02009-07-27 08:22:46 +0000186static struct ctl_table xfrm6_policy_table[] = {
187 {
Neil Hormana44a4a02009-07-27 08:22:46 +0000188 .procname = "xfrm6_gc_thresh",
Ian Morris67ba4152014-08-24 21:53:10 +0100189 .data = &init_net.xfrm.xfrm6_dst_ops.gc_thresh,
190 .maxlen = sizeof(int),
191 .mode = 0644,
Neil Hormana44a4a02009-07-27 08:22:46 +0000192 .proc_handler = proc_dointvec,
193 },
194 { }
195};
196
Dan Streetmana8a572a2015-10-29 09:51:16 -0400197static int __net_init xfrm6_net_sysctl_init(struct net *net)
Michal Kubecek8d068872013-02-06 10:46:33 +0100198{
199 struct ctl_table *table;
200 struct ctl_table_header *hdr;
201
202 table = xfrm6_policy_table;
203 if (!net_eq(net, &init_net)) {
204 table = kmemdup(table, sizeof(xfrm6_policy_table), GFP_KERNEL);
205 if (!table)
206 goto err_alloc;
207
208 table[0].data = &net->xfrm.xfrm6_dst_ops.gc_thresh;
209 }
210
211 hdr = register_net_sysctl(net, "net/ipv6", table);
212 if (!hdr)
213 goto err_reg;
214
215 net->ipv6.sysctl.xfrm6_hdr = hdr;
216 return 0;
217
218err_reg:
219 if (!net_eq(net, &init_net))
220 kfree(table);
221err_alloc:
222 return -ENOMEM;
223}
224
Dan Streetmana8a572a2015-10-29 09:51:16 -0400225static void __net_exit xfrm6_net_sysctl_exit(struct net *net)
Michal Kubecek8d068872013-02-06 10:46:33 +0100226{
227 struct ctl_table *table;
228
Ian Morris63159f22015-03-29 14:00:04 +0100229 if (!net->ipv6.sysctl.xfrm6_hdr)
Michal Kubecek8d068872013-02-06 10:46:33 +0100230 return;
231
232 table = net->ipv6.sysctl.xfrm6_hdr->ctl_table_arg;
233 unregister_net_sysctl_table(net->ipv6.sysctl.xfrm6_hdr);
234 if (!net_eq(net, &init_net))
235 kfree(table);
236}
Dan Streetmana8a572a2015-10-29 09:51:16 -0400237#else /* CONFIG_SYSCTL */
Arnd Bergmann318d3cc2016-06-16 15:59:25 +0200238static inline int xfrm6_net_sysctl_init(struct net *net)
Dan Streetmana8a572a2015-10-29 09:51:16 -0400239{
240 return 0;
241}
242
Arnd Bergmann318d3cc2016-06-16 15:59:25 +0200243static inline void xfrm6_net_sysctl_exit(struct net *net)
Dan Streetmana8a572a2015-10-29 09:51:16 -0400244{
245}
246#endif
247
248static int __net_init xfrm6_net_init(struct net *net)
249{
250 int ret;
251
252 memcpy(&net->xfrm.xfrm6_dst_ops, &xfrm6_dst_ops_template,
253 sizeof(xfrm6_dst_ops_template));
254 ret = dst_entries_init(&net->xfrm.xfrm6_dst_ops);
255 if (ret)
256 return ret;
257
258 ret = xfrm6_net_sysctl_init(net);
259 if (ret)
260 dst_entries_destroy(&net->xfrm.xfrm6_dst_ops);
261
262 return ret;
263}
264
265static void __net_exit xfrm6_net_exit(struct net *net)
266{
267 xfrm6_net_sysctl_exit(net);
268 dst_entries_destroy(&net->xfrm.xfrm6_dst_ops);
269}
Michal Kubecek8d068872013-02-06 10:46:33 +0100270
271static struct pernet_operations xfrm6_net_ops = {
272 .init = xfrm6_net_init,
273 .exit = xfrm6_net_exit,
274};
Neil Hormana44a4a02009-07-27 08:22:46 +0000275
Daniel Lezcano0013cab2007-12-07 00:42:11 -0800276int __init xfrm6_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
Daniel Lezcano0013cab2007-12-07 00:42:11 -0800278 int ret;
279
Alexey Dobriyand7c75442010-01-24 22:47:53 -0800280 ret = xfrm6_policy_init();
Dan Streetmana8a572a2015-10-29 09:51:16 -0400281 if (ret)
Alexey Dobriyand7c75442010-01-24 22:47:53 -0800282 goto out;
Alexey Dobriyand7c75442010-01-24 22:47:53 -0800283 ret = xfrm6_state_init();
284 if (ret)
285 goto out_policy;
286
Steffen Klassert7e14ea12014-03-14 07:28:07 +0100287 ret = xfrm6_protocol_init();
288 if (ret)
289 goto out_state;
290
Michal Kubecek8d068872013-02-06 10:46:33 +0100291 register_pernet_subsys(&xfrm6_net_ops);
Daniel Lezcano0013cab2007-12-07 00:42:11 -0800292out:
293 return ret;
Steffen Klassert7e14ea12014-03-14 07:28:07 +0100294out_state:
295 xfrm6_state_fini();
Daniel Lezcano0013cab2007-12-07 00:42:11 -0800296out_policy:
297 xfrm6_policy_fini();
298 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300
301void xfrm6_fini(void)
302{
Michal Kubecek8d068872013-02-06 10:46:33 +0100303 unregister_pernet_subsys(&xfrm6_net_ops);
Steffen Klassert7e14ea12014-03-14 07:28:07 +0100304 xfrm6_protocol_fini();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 xfrm6_policy_fini();
306 xfrm6_state_fini();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}