blob: 9e83bcb6bc99dd8cb7e71c9f1dbb5b7ae5570626 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * xfrm4_policy.c
4 *
5 * Changes:
6 * Kazunori MIYAZAWA @USAGI
7 * YOSHIFUJI Hideaki @USAGI
8 * Split up af-specific portion
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09009 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11
Herbert Xu66cdb3c2007-11-13 21:37:28 -080012#include <linux/err.h>
13#include <linux/kernel.h>
Herbert Xuaabc9762005-05-03 16:27:10 -070014#include <linux/inetdevice.h>
Herbert Xu45ff5a32007-11-13 21:35:32 -080015#include <net/dst.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <net/xfrm.h>
17#include <net/ip.h>
David Ahern385add92015-09-29 20:07:13 -070018#include <net/l3mdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
David S. Miller8f01cb02011-05-09 15:13:28 -070020static struct dst_entry *__xfrm4_dst_lookup(struct net *net, struct flowi4 *fl4,
David Ahern42a7b322015-08-10 16:58:11 -060021 int tos, int oif,
David S. Miller8f01cb02011-05-09 15:13:28 -070022 const xfrm_address_t *saddr,
Lorenzo Colitti077fbac2017-08-11 02:11:33 +090023 const xfrm_address_t *daddr,
24 u32 mark)
Linus Torvalds1da177e2005-04-16 15:20:36 -070025{
Herbert Xu66cdb3c2007-11-13 21:37:28 -080026 struct rtable *rt;
Patrick McHardya1e59ab2006-09-19 12:57:34 -070027
David S. Miller8f01cb02011-05-09 15:13:28 -070028 memset(fl4, 0, sizeof(*fl4));
29 fl4->daddr = daddr->a4;
30 fl4->flowi4_tos = tos;
David Ahern11d7a0b2016-08-14 19:52:56 -070031 fl4->flowi4_oif = l3mdev_master_ifindex_by_index(net, oif);
Lorenzo Colitti077fbac2017-08-11 02:11:33 +090032 fl4->flowi4_mark = mark;
Herbert Xu66cdb3c2007-11-13 21:37:28 -080033 if (saddr)
David S. Miller8f01cb02011-05-09 15:13:28 -070034 fl4->saddr = saddr->a4;
Herbert Xu66cdb3c2007-11-13 21:37:28 -080035
David Ahern58189ca2015-09-15 15:10:50 -070036 fl4->flowi4_flags = FLOWI_FLAG_SKIP_NH_OIF;
37
David S. Miller8f01cb02011-05-09 15:13:28 -070038 rt = __ip_route_output_key(net, fl4);
David S. Millerb23dd4f2011-03-02 14:31:35 -080039 if (!IS_ERR(rt))
40 return &rt->dst;
41
42 return ERR_CAST(rt);
Herbert Xu66cdb3c2007-11-13 21:37:28 -080043}
44
David Ahern42a7b322015-08-10 16:58:11 -060045static struct dst_entry *xfrm4_dst_lookup(struct net *net, int tos, int oif,
David S. Miller8f01cb02011-05-09 15:13:28 -070046 const xfrm_address_t *saddr,
Lorenzo Colitti077fbac2017-08-11 02:11:33 +090047 const xfrm_address_t *daddr,
48 u32 mark)
David S. Miller8f01cb02011-05-09 15:13:28 -070049{
50 struct flowi4 fl4;
51
Lorenzo Colitti077fbac2017-08-11 02:11:33 +090052 return __xfrm4_dst_lookup(net, &fl4, tos, oif, saddr, daddr, mark);
David S. Miller8f01cb02011-05-09 15:13:28 -070053}
54
David Ahern42a7b322015-08-10 16:58:11 -060055static int xfrm4_get_saddr(struct net *net, int oif,
Lorenzo Colitti077fbac2017-08-11 02:11:33 +090056 xfrm_address_t *saddr, xfrm_address_t *daddr,
57 u32 mark)
Herbert Xu66cdb3c2007-11-13 21:37:28 -080058{
59 struct dst_entry *dst;
David S. Miller8f01cb02011-05-09 15:13:28 -070060 struct flowi4 fl4;
Herbert Xu66cdb3c2007-11-13 21:37:28 -080061
Lorenzo Colitti077fbac2017-08-11 02:11:33 +090062 dst = __xfrm4_dst_lookup(net, &fl4, 0, oif, NULL, daddr, mark);
Herbert Xu66cdb3c2007-11-13 21:37:28 -080063 if (IS_ERR(dst))
64 return -EHOSTUNREACH;
65
David S. Miller8f01cb02011-05-09 15:13:28 -070066 saddr->a4 = fl4.saddr;
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 xfrm4_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{
74 struct rtable *rt = (struct rtable *)xdst->route;
David S. Miller7e1dc7b2011-03-12 02:42:11 -050075 const struct flowi4 *fl4 = &fl->u.ip4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Yan, Zhengb7323392011-10-22 21:58:20 +000077 xdst->u.rt.rt_iif = fl4->flowi4_iif;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Herbert Xu25ee3282007-12-11 09:32:34 -080079 xdst->u.dst.dev = dev;
Eric Dumazet4177e492021-12-07 11:32:03 -080080 dev_hold_track(dev, &xdst->u.dst.dev_tracker, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Herbert Xu25ee3282007-12-11 09:32:34 -080082 /* Sheit... I remember I did this right. Apparently,
83 * it was magically lost, so this code needs audit */
David S. Miller9917e1e82012-07-17 14:44:26 -070084 xdst->u.rt.rt_is_input = rt->rt_is_input;
Herbert Xu25ee3282007-12-11 09:32:34 -080085 xdst->u.rt.rt_flags = rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST |
86 RTCF_LOCAL);
87 xdst->u.rt.rt_type = rt->rt_type;
David Ahern77d5bc72019-09-17 10:39:49 -070088 xdst->u.rt.rt_uses_gateway = rt->rt_uses_gateway;
David Ahern1550c172019-04-05 16:30:27 -070089 xdst->u.rt.rt_gw_family = rt->rt_gw_family;
90 if (rt->rt_gw_family == AF_INET)
91 xdst->u.rt.rt_gw4 = rt->rt_gw4;
David Ahern0f5f7d72019-04-05 16:30:29 -070092 else if (rt->rt_gw_family == AF_INET6)
93 xdst->u.rt.rt_gw6 = rt->rt_gw6;
David S. Miller59436342012-07-10 06:58:42 -070094 xdst->u.rt.rt_pmtu = rt->rt_pmtu;
Sabrina Dubrocad52e5a72018-03-14 10:21:14 +010095 xdst->u.rt.rt_mtu_locked = rt->rt_mtu_locked;
David S. Millercaacf052012-07-31 15:06:50 -070096 INIT_LIST_HEAD(&xdst->u.rt.rt_uncached);
Xin Long510c3212018-02-14 19:06:02 +080097 rt_add_uncached_list(&xdst->u.rt);
Miika Komu43372262007-02-06 14:27:32 -080098
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
David S. Miller6700c272012-07-17 03:29:28 -0700102static void xfrm4_update_pmtu(struct dst_entry *dst, struct sock *sk,
Hangbin Liubd085ef2019-12-22 10:51:09 +0800103 struct sk_buff *skb, u32 mtu,
104 bool confirm_neigh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
106 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
107 struct dst_entry *path = xdst->route;
108
Hangbin Liubd085ef2019-12-22 10:51:09 +0800109 path->ops->update_pmtu(path, sk, skb, mtu, confirm_neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
111
David S. Miller6700c272012-07-17 03:29:28 -0700112static void xfrm4_redirect(struct dst_entry *dst, struct sock *sk,
113 struct sk_buff *skb)
David S. Miller55be7a92012-07-11 21:27:49 -0700114{
115 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
116 struct dst_entry *path = xdst->route;
117
David S. Miller6700c272012-07-17 03:29:28 -0700118 path->ops->redirect(path, sk, skb);
David S. Miller55be7a92012-07-11 21:27:49 -0700119}
120
Herbert Xuaabc9762005-05-03 16:27:10 -0700121static void xfrm4_dst_destroy(struct dst_entry *dst)
122{
123 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
124
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.rt.rt_uncached_list)
127 rt_del_uncached_list(&xdst->u.rt);
Herbert Xuaabc9762005-05-03 16:27:10 -0700128 xfrm_dst_destroy(xdst);
129}
130
131static void xfrm4_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
132 int unregister)
133{
Herbert Xuaabc9762005-05-03 16:27:10 -0700134 if (!unregister)
135 return;
136
Herbert Xuaabc9762005-05-03 16:27:10 -0700137 xfrm_dst_ifdown(dst, dev);
138}
139
Dan Streetmana8a572a2015-10-29 09:51:16 -0400140static struct dst_ops xfrm4_dst_ops_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 .family = AF_INET,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 .update_pmtu = xfrm4_update_pmtu,
David S. Miller55be7a92012-07-11 21:27:49 -0700143 .redirect = xfrm4_redirect,
David S. Miller62fa8a82011-01-26 20:51:05 -0800144 .cow_metrics = dst_cow_metrics_generic,
Herbert Xuaabc9762005-05-03 16:27:10 -0700145 .destroy = xfrm4_dst_destroy,
146 .ifdown = xfrm4_dst_ifdown,
Herbert Xu862b82c2007-11-13 21:43:11 -0800147 .local_out = __ip_local_out,
Florian Westphal3c2a89d2017-07-17 13:57:20 +0200148 .gc_thresh = 32768,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149};
150
Florian Westphal37b10382017-02-07 15:00:19 +0100151static const struct xfrm_policy_afinfo xfrm4_policy_afinfo = {
Dan Streetmana8a572a2015-10-29 09:51:16 -0400152 .dst_ops = &xfrm4_dst_ops_template,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 .dst_lookup = xfrm4_dst_lookup,
Patrick McHardya1e59ab2006-09-19 12:57:34 -0700154 .get_saddr = xfrm4_get_saddr,
Herbert Xu25ee3282007-12-11 09:32:34 -0800155 .fill_dst = xfrm4_fill_dst,
David S. Miller2774c132011-03-01 14:59:04 -0800156 .blackhole_route = ipv4_blackhole_route,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157};
158
Randy Dunlapf8167002009-08-04 20:18:33 -0700159#ifdef CONFIG_SYSCTL
Neil Hormana44a4a02009-07-27 08:22:46 +0000160static struct ctl_table xfrm4_policy_table[] = {
161 {
Neil Hormana44a4a02009-07-27 08:22:46 +0000162 .procname = "xfrm4_gc_thresh",
Alexey Dobriyand7c75442010-01-24 22:47:53 -0800163 .data = &init_net.xfrm.xfrm4_dst_ops.gc_thresh,
Neil Hormana44a4a02009-07-27 08:22:46 +0000164 .maxlen = sizeof(int),
165 .mode = 0644,
166 .proc_handler = proc_dointvec,
167 },
168 { }
169};
170
Arnd Bergmann318d3cc2016-06-16 15:59:25 +0200171static __net_init int xfrm4_net_sysctl_init(struct net *net)
Michal Kubecek8d068872013-02-06 10:46:33 +0100172{
173 struct ctl_table *table;
174 struct ctl_table_header *hdr;
175
176 table = xfrm4_policy_table;
177 if (!net_eq(net, &init_net)) {
178 table = kmemdup(table, sizeof(xfrm4_policy_table), GFP_KERNEL);
179 if (!table)
180 goto err_alloc;
181
182 table[0].data = &net->xfrm.xfrm4_dst_ops.gc_thresh;
183 }
184
185 hdr = register_net_sysctl(net, "net/ipv4", table);
186 if (!hdr)
187 goto err_reg;
188
189 net->ipv4.xfrm4_hdr = hdr;
190 return 0;
191
192err_reg:
193 if (!net_eq(net, &init_net))
194 kfree(table);
195err_alloc:
196 return -ENOMEM;
197}
198
Arnd Bergmann318d3cc2016-06-16 15:59:25 +0200199static __net_exit void xfrm4_net_sysctl_exit(struct net *net)
Michal Kubecek8d068872013-02-06 10:46:33 +0100200{
201 struct ctl_table *table;
202
Ian Morris51456b22015-04-03 09:17:26 +0100203 if (!net->ipv4.xfrm4_hdr)
Michal Kubecek8d068872013-02-06 10:46:33 +0100204 return;
205
206 table = net->ipv4.xfrm4_hdr->ctl_table_arg;
207 unregister_net_sysctl_table(net->ipv4.xfrm4_hdr);
208 if (!net_eq(net, &init_net))
209 kfree(table);
210}
Dan Streetmana8a572a2015-10-29 09:51:16 -0400211#else /* CONFIG_SYSCTL */
Arnd Bergmann318d3cc2016-06-16 15:59:25 +0200212static inline int xfrm4_net_sysctl_init(struct net *net)
Dan Streetmana8a572a2015-10-29 09:51:16 -0400213{
214 return 0;
215}
216
Arnd Bergmann318d3cc2016-06-16 15:59:25 +0200217static inline void xfrm4_net_sysctl_exit(struct net *net)
Dan Streetmana8a572a2015-10-29 09:51:16 -0400218{
219}
220#endif
221
222static int __net_init xfrm4_net_init(struct net *net)
223{
224 int ret;
225
226 memcpy(&net->xfrm.xfrm4_dst_ops, &xfrm4_dst_ops_template,
227 sizeof(xfrm4_dst_ops_template));
228 ret = dst_entries_init(&net->xfrm.xfrm4_dst_ops);
229 if (ret)
230 return ret;
231
232 ret = xfrm4_net_sysctl_init(net);
233 if (ret)
234 dst_entries_destroy(&net->xfrm.xfrm4_dst_ops);
235
236 return ret;
237}
238
239static void __net_exit xfrm4_net_exit(struct net *net)
240{
241 xfrm4_net_sysctl_exit(net);
242 dst_entries_destroy(&net->xfrm.xfrm4_dst_ops);
243}
Michal Kubecek8d068872013-02-06 10:46:33 +0100244
245static struct pernet_operations __net_initdata xfrm4_net_ops = {
246 .init = xfrm4_net_init,
247 .exit = xfrm4_net_exit,
248};
Neil Hormana44a4a02009-07-27 08:22:46 +0000249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250static void __init xfrm4_policy_init(void)
251{
Florian Westphala2817d82017-02-07 15:00:17 +0100252 xfrm_policy_register_afinfo(&xfrm4_policy_afinfo, AF_INET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
Steffen Klassert703fb942012-11-13 08:52:24 +0100255void __init xfrm4_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
Alexey Dobriyand7c75442010-01-24 22:47:53 -0800257 xfrm4_state_init();
258 xfrm4_policy_init();
Steffen Klassert2f32b512014-03-14 07:28:07 +0100259 xfrm4_protocol_init();
Michal Kubecek8d068872013-02-06 10:46:33 +0100260 register_pernet_subsys(&xfrm4_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}