blob: fad687ee6dd81af9ee3591eb2333cfed8ceae8ce [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;
Eric Dumazet4177e492021-12-07 11:32:03 -080077 dev_hold_track(dev, &xdst->u.dst.dev_tracker, GFP_ATOMIC);
Herbert Xu25ee3282007-12-11 09:32:34 -080078
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) {
Eric Dumazet4177e492021-12-07 11:32:03 -080081 dev_put_track(dev, &xdst->u.dst.dev_tracker);
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,
Hangbin Liubd085ef2019-12-22 10:51:09 +0800101 struct sk_buff *skb, u32 mtu,
102 bool confirm_neigh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
104 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
105 struct dst_entry *path = xdst->route;
106
Hangbin Liubd085ef2019-12-22 10:51:09 +0800107 path->ops->update_pmtu(path, sk, skb, mtu, confirm_neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
109
David S. Miller6700c272012-07-17 03:29:28 -0700110static void xfrm6_redirect(struct dst_entry *dst, struct sock *sk,
111 struct sk_buff *skb)
David S. Millerec18d9a2012-07-12 00:25:15 -0700112{
113 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
114 struct dst_entry *path = xdst->route;
115
David S. Miller6700c272012-07-17 03:29:28 -0700116 path->ops->redirect(path, sk, skb);
David S. Millerec18d9a2012-07-12 00:25:15 -0700117}
118
Herbert Xuaabc9762005-05-03 16:27:10 -0700119static void xfrm6_dst_destroy(struct dst_entry *dst)
120{
121 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
122
123 if (likely(xdst->u.rt6.rt6i_idev))
124 in6_dev_put(xdst->u.rt6.rt6i_idev);
David S. Miller62fa8a82011-01-26 20:51:05 -0800125 dst_destroy_metrics_generic(dst);
Xin Long510c3212018-02-14 19:06:02 +0800126 if (xdst->u.rt6.rt6i_uncached_list)
127 rt6_uncached_list_del(&xdst->u.rt6);
Herbert Xuaabc9762005-05-03 16:27:10 -0700128 xfrm_dst_destroy(xdst);
129}
130
131static void xfrm6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
132 int unregister)
133{
134 struct xfrm_dst *xdst;
135
136 if (!unregister)
137 return;
138
139 xdst = (struct xfrm_dst *)dst;
140 if (xdst->u.rt6.rt6i_idev->dev == dev) {
Denis V. Lunev5a3e55d2007-12-07 00:38:10 -0800141 struct inet6_dev *loopback_idev =
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900142 in6_dev_get(dev_net(dev)->loopback_dev);
Herbert Xuaabc9762005-05-03 16:27:10 -0700143
144 do {
145 in6_dev_put(xdst->u.rt6.rt6i_idev);
146 xdst->u.rt6.rt6i_idev = loopback_idev;
147 in6_dev_hold(loopback_idev);
David Millerb92cf4a2017-11-28 15:40:22 -0500148 xdst = (struct xfrm_dst *)xfrm_dst_child(&xdst->u.dst);
Herbert Xuaabc9762005-05-03 16:27:10 -0700149 } while (xdst->u.dst.xfrm);
150
151 __in6_dev_put(loopback_idev);
152 }
153
154 xfrm_dst_ifdown(dst, dev);
155}
156
Dan Streetmana8a572a2015-10-29 09:51:16 -0400157static struct dst_ops xfrm6_dst_ops_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 .family = AF_INET6,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 .update_pmtu = xfrm6_update_pmtu,
David S. Millerec18d9a2012-07-12 00:25:15 -0700160 .redirect = xfrm6_redirect,
David S. Miller62fa8a82011-01-26 20:51:05 -0800161 .cow_metrics = dst_cow_metrics_generic,
Herbert Xuaabc9762005-05-03 16:27:10 -0700162 .destroy = xfrm6_dst_destroy,
163 .ifdown = xfrm6_dst_ifdown,
Herbert Xu862b82c2007-11-13 21:43:11 -0800164 .local_out = __ip6_local_out,
Florian Westphal3c2a89d2017-07-17 13:57:20 +0200165 .gc_thresh = 32768,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166};
167
Florian Westphal37b10382017-02-07 15:00:19 +0100168static const struct xfrm_policy_afinfo xfrm6_policy_afinfo = {
Dan Streetmana8a572a2015-10-29 09:51:16 -0400169 .dst_ops = &xfrm6_dst_ops_template,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 .dst_lookup = xfrm6_dst_lookup,
Ian Morris67ba4152014-08-24 21:53:10 +0100171 .get_saddr = xfrm6_get_saddr,
Herbert Xu25ee3282007-12-11 09:32:34 -0800172 .fill_dst = xfrm6_fill_dst,
David S. Miller2774c132011-03-01 14:59:04 -0800173 .blackhole_route = ip6_blackhole_route,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174};
175
Daniel Lezcano0013cab2007-12-07 00:42:11 -0800176static int __init xfrm6_policy_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
Florian Westphala2817d82017-02-07 15:00:17 +0100178 return xfrm_policy_register_afinfo(&xfrm6_policy_afinfo, AF_INET6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
181static void xfrm6_policy_fini(void)
182{
183 xfrm_policy_unregister_afinfo(&xfrm6_policy_afinfo);
184}
185
David S. Millerdb717892009-08-04 20:32:16 -0700186#ifdef CONFIG_SYSCTL
Neil Hormana44a4a02009-07-27 08:22:46 +0000187static struct ctl_table xfrm6_policy_table[] = {
188 {
Neil Hormana44a4a02009-07-27 08:22:46 +0000189 .procname = "xfrm6_gc_thresh",
Ian Morris67ba4152014-08-24 21:53:10 +0100190 .data = &init_net.xfrm.xfrm6_dst_ops.gc_thresh,
191 .maxlen = sizeof(int),
192 .mode = 0644,
Neil Hormana44a4a02009-07-27 08:22:46 +0000193 .proc_handler = proc_dointvec,
194 },
195 { }
196};
197
Dan Streetmana8a572a2015-10-29 09:51:16 -0400198static int __net_init xfrm6_net_sysctl_init(struct net *net)
Michal Kubecek8d068872013-02-06 10:46:33 +0100199{
200 struct ctl_table *table;
201 struct ctl_table_header *hdr;
202
203 table = xfrm6_policy_table;
204 if (!net_eq(net, &init_net)) {
205 table = kmemdup(table, sizeof(xfrm6_policy_table), GFP_KERNEL);
206 if (!table)
207 goto err_alloc;
208
209 table[0].data = &net->xfrm.xfrm6_dst_ops.gc_thresh;
210 }
211
212 hdr = register_net_sysctl(net, "net/ipv6", table);
213 if (!hdr)
214 goto err_reg;
215
216 net->ipv6.sysctl.xfrm6_hdr = hdr;
217 return 0;
218
219err_reg:
220 if (!net_eq(net, &init_net))
221 kfree(table);
222err_alloc:
223 return -ENOMEM;
224}
225
Dan Streetmana8a572a2015-10-29 09:51:16 -0400226static void __net_exit xfrm6_net_sysctl_exit(struct net *net)
Michal Kubecek8d068872013-02-06 10:46:33 +0100227{
228 struct ctl_table *table;
229
Ian Morris63159f22015-03-29 14:00:04 +0100230 if (!net->ipv6.sysctl.xfrm6_hdr)
Michal Kubecek8d068872013-02-06 10:46:33 +0100231 return;
232
233 table = net->ipv6.sysctl.xfrm6_hdr->ctl_table_arg;
234 unregister_net_sysctl_table(net->ipv6.sysctl.xfrm6_hdr);
235 if (!net_eq(net, &init_net))
236 kfree(table);
237}
Dan Streetmana8a572a2015-10-29 09:51:16 -0400238#else /* CONFIG_SYSCTL */
Arnd Bergmann318d3cc2016-06-16 15:59:25 +0200239static inline int xfrm6_net_sysctl_init(struct net *net)
Dan Streetmana8a572a2015-10-29 09:51:16 -0400240{
241 return 0;
242}
243
Arnd Bergmann318d3cc2016-06-16 15:59:25 +0200244static inline void xfrm6_net_sysctl_exit(struct net *net)
Dan Streetmana8a572a2015-10-29 09:51:16 -0400245{
246}
247#endif
248
249static int __net_init xfrm6_net_init(struct net *net)
250{
251 int ret;
252
253 memcpy(&net->xfrm.xfrm6_dst_ops, &xfrm6_dst_ops_template,
254 sizeof(xfrm6_dst_ops_template));
255 ret = dst_entries_init(&net->xfrm.xfrm6_dst_ops);
256 if (ret)
257 return ret;
258
259 ret = xfrm6_net_sysctl_init(net);
260 if (ret)
261 dst_entries_destroy(&net->xfrm.xfrm6_dst_ops);
262
263 return ret;
264}
265
266static void __net_exit xfrm6_net_exit(struct net *net)
267{
268 xfrm6_net_sysctl_exit(net);
269 dst_entries_destroy(&net->xfrm.xfrm6_dst_ops);
270}
Michal Kubecek8d068872013-02-06 10:46:33 +0100271
272static struct pernet_operations xfrm6_net_ops = {
273 .init = xfrm6_net_init,
274 .exit = xfrm6_net_exit,
275};
Neil Hormana44a4a02009-07-27 08:22:46 +0000276
Daniel Lezcano0013cab2007-12-07 00:42:11 -0800277int __init xfrm6_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
Daniel Lezcano0013cab2007-12-07 00:42:11 -0800279 int ret;
280
Alexey Dobriyand7c75442010-01-24 22:47:53 -0800281 ret = xfrm6_policy_init();
Dan Streetmana8a572a2015-10-29 09:51:16 -0400282 if (ret)
Alexey Dobriyand7c75442010-01-24 22:47:53 -0800283 goto out;
Alexey Dobriyand7c75442010-01-24 22:47:53 -0800284 ret = xfrm6_state_init();
285 if (ret)
286 goto out_policy;
287
Steffen Klassert7e14ea12014-03-14 07:28:07 +0100288 ret = xfrm6_protocol_init();
289 if (ret)
290 goto out_state;
291
Michal Kubecek8d068872013-02-06 10:46:33 +0100292 register_pernet_subsys(&xfrm6_net_ops);
Daniel Lezcano0013cab2007-12-07 00:42:11 -0800293out:
294 return ret;
Steffen Klassert7e14ea12014-03-14 07:28:07 +0100295out_state:
296 xfrm6_state_fini();
Daniel Lezcano0013cab2007-12-07 00:42:11 -0800297out_policy:
298 xfrm6_policy_fini();
299 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
301
302void xfrm6_fini(void)
303{
Michal Kubecek8d068872013-02-06 10:46:33 +0100304 unregister_pernet_subsys(&xfrm6_net_ops);
Steffen Klassert7e14ea12014-03-14 07:28:07 +0100305 xfrm6_protocol_fini();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 xfrm6_policy_fini();
307 xfrm6_state_fini();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308}