blob: cdef8f9a3b0166942c3bad33b649dffa97578ca8 [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;
80 dev_hold(dev);
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 Ahern1550c172019-04-05 16:30:27 -070088 xdst->u.rt.rt_gw_family = rt->rt_gw_family;
89 if (rt->rt_gw_family == AF_INET)
90 xdst->u.rt.rt_gw4 = rt->rt_gw4;
David Ahern0f5f7d72019-04-05 16:30:29 -070091 else if (rt->rt_gw_family == AF_INET6)
92 xdst->u.rt.rt_gw6 = rt->rt_gw6;
David S. Miller59436342012-07-10 06:58:42 -070093 xdst->u.rt.rt_pmtu = rt->rt_pmtu;
Sabrina Dubrocad52e5a7e2018-03-14 10:21:14 +010094 xdst->u.rt.rt_mtu_locked = rt->rt_mtu_locked;
David S. Millercaacf052012-07-31 15:06:50 -070095 INIT_LIST_HEAD(&xdst->u.rt.rt_uncached);
Xin Long510c3212018-02-14 19:06:02 +080096 rt_add_uncached_list(&xdst->u.rt);
Miika Komu43372262007-02-06 14:27:32 -080097
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
100
David S. Miller6700c272012-07-17 03:29:28 -0700101static void xfrm4_update_pmtu(struct dst_entry *dst, struct sock *sk,
102 struct sk_buff *skb, u32 mtu)
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
David S. Miller6700c272012-07-17 03:29:28 -0700107 path->ops->update_pmtu(path, sk, skb, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
109
David S. Miller6700c272012-07-17 03:29:28 -0700110static void xfrm4_redirect(struct dst_entry *dst, struct sock *sk,
111 struct sk_buff *skb)
David S. Miller55be7a92012-07-11 21:27:49 -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. Miller55be7a92012-07-11 21:27:49 -0700117}
118
Herbert Xuaabc9762005-05-03 16:27:10 -0700119static void xfrm4_dst_destroy(struct dst_entry *dst)
120{
121 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
122
David S. Miller62fa8a82011-01-26 20:51:05 -0800123 dst_destroy_metrics_generic(dst);
Xin Long510c3212018-02-14 19:06:02 +0800124 if (xdst->u.rt.rt_uncached_list)
125 rt_del_uncached_list(&xdst->u.rt);
Herbert Xuaabc9762005-05-03 16:27:10 -0700126 xfrm_dst_destroy(xdst);
127}
128
129static void xfrm4_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
130 int unregister)
131{
Herbert Xuaabc9762005-05-03 16:27:10 -0700132 if (!unregister)
133 return;
134
Herbert Xuaabc9762005-05-03 16:27:10 -0700135 xfrm_dst_ifdown(dst, dev);
136}
137
Dan Streetmana8a572a2015-10-29 09:51:16 -0400138static struct dst_ops xfrm4_dst_ops_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 .family = AF_INET,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 .update_pmtu = xfrm4_update_pmtu,
David S. Miller55be7a92012-07-11 21:27:49 -0700141 .redirect = xfrm4_redirect,
David S. Miller62fa8a82011-01-26 20:51:05 -0800142 .cow_metrics = dst_cow_metrics_generic,
Herbert Xuaabc9762005-05-03 16:27:10 -0700143 .destroy = xfrm4_dst_destroy,
144 .ifdown = xfrm4_dst_ifdown,
Herbert Xu862b82c2007-11-13 21:43:11 -0800145 .local_out = __ip_local_out,
Florian Westphal3c2a89d2017-07-17 13:57:20 +0200146 .gc_thresh = 32768,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147};
148
Florian Westphal37b10382017-02-07 15:00:19 +0100149static const struct xfrm_policy_afinfo xfrm4_policy_afinfo = {
Dan Streetmana8a572a2015-10-29 09:51:16 -0400150 .dst_ops = &xfrm4_dst_ops_template,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 .dst_lookup = xfrm4_dst_lookup,
Patrick McHardya1e59ab2006-09-19 12:57:34 -0700152 .get_saddr = xfrm4_get_saddr,
Herbert Xu25ee3282007-12-11 09:32:34 -0800153 .fill_dst = xfrm4_fill_dst,
David S. Miller2774c132011-03-01 14:59:04 -0800154 .blackhole_route = ipv4_blackhole_route,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155};
156
Randy Dunlapf8167002009-08-04 20:18:33 -0700157#ifdef CONFIG_SYSCTL
Neil Hormana44a4a02009-07-27 08:22:46 +0000158static struct ctl_table xfrm4_policy_table[] = {
159 {
Neil Hormana44a4a02009-07-27 08:22:46 +0000160 .procname = "xfrm4_gc_thresh",
Alexey Dobriyand7c75442010-01-24 22:47:53 -0800161 .data = &init_net.xfrm.xfrm4_dst_ops.gc_thresh,
Neil Hormana44a4a02009-07-27 08:22:46 +0000162 .maxlen = sizeof(int),
163 .mode = 0644,
164 .proc_handler = proc_dointvec,
165 },
166 { }
167};
168
Arnd Bergmann318d3cc2016-06-16 15:59:25 +0200169static __net_init int xfrm4_net_sysctl_init(struct net *net)
Michal Kubecek8d068872013-02-06 10:46:33 +0100170{
171 struct ctl_table *table;
172 struct ctl_table_header *hdr;
173
174 table = xfrm4_policy_table;
175 if (!net_eq(net, &init_net)) {
176 table = kmemdup(table, sizeof(xfrm4_policy_table), GFP_KERNEL);
177 if (!table)
178 goto err_alloc;
179
180 table[0].data = &net->xfrm.xfrm4_dst_ops.gc_thresh;
181 }
182
183 hdr = register_net_sysctl(net, "net/ipv4", table);
184 if (!hdr)
185 goto err_reg;
186
187 net->ipv4.xfrm4_hdr = hdr;
188 return 0;
189
190err_reg:
191 if (!net_eq(net, &init_net))
192 kfree(table);
193err_alloc:
194 return -ENOMEM;
195}
196
Arnd Bergmann318d3cc2016-06-16 15:59:25 +0200197static __net_exit void xfrm4_net_sysctl_exit(struct net *net)
Michal Kubecek8d068872013-02-06 10:46:33 +0100198{
199 struct ctl_table *table;
200
Ian Morris51456b22015-04-03 09:17:26 +0100201 if (!net->ipv4.xfrm4_hdr)
Michal Kubecek8d068872013-02-06 10:46:33 +0100202 return;
203
204 table = net->ipv4.xfrm4_hdr->ctl_table_arg;
205 unregister_net_sysctl_table(net->ipv4.xfrm4_hdr);
206 if (!net_eq(net, &init_net))
207 kfree(table);
208}
Dan Streetmana8a572a2015-10-29 09:51:16 -0400209#else /* CONFIG_SYSCTL */
Arnd Bergmann318d3cc2016-06-16 15:59:25 +0200210static inline int xfrm4_net_sysctl_init(struct net *net)
Dan Streetmana8a572a2015-10-29 09:51:16 -0400211{
212 return 0;
213}
214
Arnd Bergmann318d3cc2016-06-16 15:59:25 +0200215static inline void xfrm4_net_sysctl_exit(struct net *net)
Dan Streetmana8a572a2015-10-29 09:51:16 -0400216{
217}
218#endif
219
220static int __net_init xfrm4_net_init(struct net *net)
221{
222 int ret;
223
224 memcpy(&net->xfrm.xfrm4_dst_ops, &xfrm4_dst_ops_template,
225 sizeof(xfrm4_dst_ops_template));
226 ret = dst_entries_init(&net->xfrm.xfrm4_dst_ops);
227 if (ret)
228 return ret;
229
230 ret = xfrm4_net_sysctl_init(net);
231 if (ret)
232 dst_entries_destroy(&net->xfrm.xfrm4_dst_ops);
233
234 return ret;
235}
236
237static void __net_exit xfrm4_net_exit(struct net *net)
238{
239 xfrm4_net_sysctl_exit(net);
240 dst_entries_destroy(&net->xfrm.xfrm4_dst_ops);
241}
Michal Kubecek8d068872013-02-06 10:46:33 +0100242
243static struct pernet_operations __net_initdata xfrm4_net_ops = {
244 .init = xfrm4_net_init,
245 .exit = xfrm4_net_exit,
246};
Neil Hormana44a4a02009-07-27 08:22:46 +0000247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248static void __init xfrm4_policy_init(void)
249{
Florian Westphala2817d82017-02-07 15:00:17 +0100250 xfrm_policy_register_afinfo(&xfrm4_policy_afinfo, AF_INET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
252
Steffen Klassert703fb942012-11-13 08:52:24 +0100253void __init xfrm4_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
Alexey Dobriyand7c75442010-01-24 22:47:53 -0800255 xfrm4_state_init();
256 xfrm4_policy_init();
Steffen Klassert2f32b51b2014-03-14 07:28:07 +0100257 xfrm4_protocol_init();
Michal Kubecek8d068872013-02-06 10:46:33 +0100258 register_pernet_subsys(&xfrm4_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}