blob: e54c2bcbb4657efa827357f1a38986e0f821ee77 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * INET An implementation of the TCP/IP protocol suite for the LINUX
4 * operating system. INET is implemented using the BSD Socket
5 * interface as the means of communication with the user level.
6 *
7 * IPv4 Forwarding Information Base: FIB frontend.
8 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/module.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080013#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/bitops.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080015#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/types.h>
17#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/mm.h>
19#include <linux/string.h>
20#include <linux/socket.h>
21#include <linux/sockios.h>
22#include <linux/errno.h>
23#include <linux/in.h>
24#include <linux/inet.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020025#include <linux/inetdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/netdevice.h>
Thomas Graf18237302006-08-04 23:04:54 -070027#include <linux/if_addr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/if_arp.h>
29#include <linux/skbuff.h>
David S. Miller7a9bc9b2012-06-29 01:32:45 -070030#include <linux/cache.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/init.h>
Patrick McHardy1af5a8c2006-08-10 23:10:46 -070032#include <linux/list.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35#include <net/ip.h>
36#include <net/protocol.h>
37#include <net/route.h>
38#include <net/tcp.h>
39#include <net/sock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <net/arp.h>
41#include <net/ip_fib.h>
Thomas Graf63f34442007-03-22 11:55:17 -070042#include <net/rtnetlink.h>
Michael Smith990078a2011-04-07 04:51:51 +000043#include <net/xfrm.h>
David Ahern385add92015-09-29 20:07:13 -070044#include <net/l3mdev.h>
David Ahern9ed59592017-01-17 14:57:36 -080045#include <net/lwtunnel.h>
David Ahernf6d3c192015-08-28 08:42:09 -070046#include <trace/events/fib.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#ifndef CONFIG_IP_MULTIPLE_TABLES
49
Denis V. Lunev7b1a74fd2008-01-10 03:22:17 -080050static int __net_init fib4_rules_init(struct net *net)
Pavel Emelyanovc3e9a352007-11-06 23:34:04 -080051{
Denis V. Lunev93456b62008-01-10 03:23:38 -080052 struct fib_table *local_table, *main_table;
53
Alexander Duyck0ddcf432015-03-06 13:47:00 -080054 main_table = fib_trie_table(RT_TABLE_MAIN, NULL);
Ian Morris51456b22015-04-03 09:17:26 +010055 if (!main_table)
Alexander Duyck61f0d862015-03-11 14:02:16 -070056 return -ENOMEM;
Denis V. Lunevdbb50162008-01-10 03:21:49 -080057
Alexander Duyck0ddcf432015-03-06 13:47:00 -080058 local_table = fib_trie_table(RT_TABLE_LOCAL, main_table);
Ian Morris51456b22015-04-03 09:17:26 +010059 if (!local_table)
Alexander Duyck61f0d862015-03-11 14:02:16 -070060 goto fail;
Alexander Duyck0ddcf432015-03-06 13:47:00 -080061
Denis V. Lunev93456b62008-01-10 03:23:38 -080062 hlist_add_head_rcu(&local_table->tb_hlist,
Denis V. Luneve4aef8a2008-01-10 03:28:24 -080063 &net->ipv4.fib_table_hash[TABLE_LOCAL_INDEX]);
Denis V. Lunev93456b62008-01-10 03:23:38 -080064 hlist_add_head_rcu(&main_table->tb_hlist,
Denis V. Luneve4aef8a2008-01-10 03:28:24 -080065 &net->ipv4.fib_table_hash[TABLE_MAIN_INDEX]);
Denis V. Lunevdbb50162008-01-10 03:21:49 -080066 return 0;
67
68fail:
Alexander Duyck61f0d862015-03-11 14:02:16 -070069 fib_free_table(main_table);
Denis V. Lunevdbb50162008-01-10 03:21:49 -080070 return -ENOMEM;
Pavel Emelyanovc3e9a352007-11-06 23:34:04 -080071}
Paolo Abeni032a4802017-10-31 14:32:38 +010072
73static bool fib4_has_custom_rules(struct net *net)
74{
75 return false;
76}
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#else
78
Denis V. Lunev8ad49422008-01-10 03:24:11 -080079struct fib_table *fib_new_table(struct net *net, u32 id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
Alexander Duyck0ddcf432015-03-06 13:47:00 -080081 struct fib_table *tb, *alias = NULL;
Patrick McHardy1af5a8c2006-08-10 23:10:46 -070082 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Patrick McHardy1af5a8c2006-08-10 23:10:46 -070084 if (id == 0)
85 id = RT_TABLE_MAIN;
Denis V. Lunev8ad49422008-01-10 03:24:11 -080086 tb = fib_get_table(net, id);
Patrick McHardy1af5a8c2006-08-10 23:10:46 -070087 if (tb)
88 return tb;
Stephen Hemminger7f9b8052008-01-14 23:14:20 -080089
Alexander Duyck5350d542017-01-02 13:32:54 -080090 if (id == RT_TABLE_LOCAL && !net->ipv4.fib_has_custom_rules)
Alexander Duyck0ddcf432015-03-06 13:47:00 -080091 alias = fib_new_table(net, RT_TABLE_MAIN);
92
93 tb = fib_trie_table(id, alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 if (!tb)
95 return NULL;
David S. Millerf4530fa2012-07-05 22:13:13 -070096
97 switch (id) {
David S. Millerf4530fa2012-07-05 22:13:13 -070098 case RT_TABLE_MAIN:
Alexander Duycka7e53532015-03-04 15:02:44 -080099 rcu_assign_pointer(net->ipv4.fib_main, tb);
David S. Millerf4530fa2012-07-05 22:13:13 -0700100 break;
David S. Millerf4530fa2012-07-05 22:13:13 -0700101 case RT_TABLE_DEFAULT:
Alexander Duycka7e53532015-03-04 15:02:44 -0800102 rcu_assign_pointer(net->ipv4.fib_default, tb);
David S. Millerf4530fa2012-07-05 22:13:13 -0700103 break;
David S. Millerf4530fa2012-07-05 22:13:13 -0700104 default:
105 break;
106 }
107
Patrick McHardy1af5a8c2006-08-10 23:10:46 -0700108 h = id & (FIB_TABLE_HASHSZ - 1);
Denis V. Luneve4aef8a2008-01-10 03:28:24 -0800109 hlist_add_head_rcu(&tb->tb_hlist, &net->ipv4.fib_table_hash[h]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 return tb;
111}
David Ahernb3b46632016-05-04 21:46:12 -0700112EXPORT_SYMBOL_GPL(fib_new_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Alexander Duyck345e9b52014-12-31 10:56:24 -0800114/* caller must hold either rtnl or rcu read lock */
Denis V. Lunev8ad49422008-01-10 03:24:11 -0800115struct fib_table *fib_get_table(struct net *net, u32 id)
Patrick McHardy1af5a8c2006-08-10 23:10:46 -0700116{
117 struct fib_table *tb;
Denis V. Luneve4aef8a2008-01-10 03:28:24 -0800118 struct hlist_head *head;
Patrick McHardy1af5a8c2006-08-10 23:10:46 -0700119 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Patrick McHardy1af5a8c2006-08-10 23:10:46 -0700121 if (id == 0)
122 id = RT_TABLE_MAIN;
123 h = id & (FIB_TABLE_HASHSZ - 1);
Denis V. Luneve4aef8a2008-01-10 03:28:24 -0800124
Denis V. Luneve4aef8a2008-01-10 03:28:24 -0800125 head = &net->ipv4.fib_table_hash[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800126 hlist_for_each_entry_rcu(tb, head, tb_hlist) {
Alexander Duyck345e9b52014-12-31 10:56:24 -0800127 if (tb->tb_id == id)
Patrick McHardy1af5a8c2006-08-10 23:10:46 -0700128 return tb;
Patrick McHardy1af5a8c2006-08-10 23:10:46 -0700129 }
Patrick McHardy1af5a8c2006-08-10 23:10:46 -0700130 return NULL;
131}
Paolo Abeni032a4802017-10-31 14:32:38 +0100132
133static bool fib4_has_custom_rules(struct net *net)
134{
135 return net->ipv4.fib_has_custom_rules;
136}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137#endif /* CONFIG_IP_MULTIPLE_TABLES */
138
Alexander Duyck0ddcf432015-03-06 13:47:00 -0800139static void fib_replace_table(struct net *net, struct fib_table *old,
140 struct fib_table *new)
141{
142#ifdef CONFIG_IP_MULTIPLE_TABLES
143 switch (new->tb_id) {
Alexander Duyck0ddcf432015-03-06 13:47:00 -0800144 case RT_TABLE_MAIN:
145 rcu_assign_pointer(net->ipv4.fib_main, new);
146 break;
147 case RT_TABLE_DEFAULT:
148 rcu_assign_pointer(net->ipv4.fib_default, new);
149 break;
150 default:
151 break;
152 }
153
154#endif
155 /* replace the old table in the hlist */
156 hlist_replace_rcu(&old->tb_hlist, &new->tb_hlist);
157}
158
159int fib_unmerge(struct net *net)
160{
Alexander Duyck3b709332016-11-15 05:46:06 -0500161 struct fib_table *old, *new, *main_table;
Alexander Duyck0ddcf432015-03-06 13:47:00 -0800162
Alexander Duyck3c9e9f72015-03-12 14:46:23 -0700163 /* attempt to fetch local table if it has been allocated */
Alexander Duyck0ddcf432015-03-06 13:47:00 -0800164 old = fib_get_table(net, RT_TABLE_LOCAL);
Alexander Duyck3c9e9f72015-03-12 14:46:23 -0700165 if (!old)
166 return 0;
Alexander Duyck0ddcf432015-03-06 13:47:00 -0800167
Alexander Duyck3c9e9f72015-03-12 14:46:23 -0700168 new = fib_trie_unmerge(old);
Alexander Duyck0ddcf432015-03-06 13:47:00 -0800169 if (!new)
170 return -ENOMEM;
171
Alexander Duyck3b709332016-11-15 05:46:06 -0500172 /* table is already unmerged */
173 if (new == old)
174 return 0;
175
Alexander Duyck0ddcf432015-03-06 13:47:00 -0800176 /* replace merged table with clean table */
Alexander Duyck3b709332016-11-15 05:46:06 -0500177 fib_replace_table(net, old, new);
178 fib_free_table(old);
179
180 /* attempt to fetch main table if it has been allocated */
181 main_table = fib_get_table(net, RT_TABLE_MAIN);
182 if (!main_table)
183 return 0;
184
185 /* flush local entries from main table */
186 fib_table_flush_external(main_table);
Alexander Duyck0ddcf432015-03-06 13:47:00 -0800187
188 return 0;
189}
190
Denis V. Luneve4aef8a2008-01-10 03:28:24 -0800191static void fib_flush(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
193 int flushed = 0;
Patrick McHardy1af5a8c2006-08-10 23:10:46 -0700194 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Patrick McHardy1af5a8c2006-08-10 23:10:46 -0700196 for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
Alexander Duycka7e53532015-03-04 15:02:44 -0800197 struct hlist_head *head = &net->ipv4.fib_table_hash[h];
198 struct hlist_node *tmp;
199 struct fib_table *tb;
200
201 hlist_for_each_entry_safe(tb, tmp, head, tb_hlist)
Ido Schimmelf97f4dd2019-01-09 09:57:39 +0000202 flushed += fib_table_flush(net, tb, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 if (flushed)
Nicolas Dichtel4ccfe6d2012-09-07 00:45:29 +0000206 rt_cache_flush(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207}
208
Laszlo Attila Toth055381162007-12-04 23:28:46 -0800209/*
210 * Find address type as if only "dev" was present in the system. If
211 * on_dev is NULL then all interfaces are taken into consideration.
212 */
Eric Dumazet95c96172012-04-15 05:58:06 +0000213static inline unsigned int __inet_dev_addr_type(struct net *net,
214 const struct net_device *dev,
David Ahern9b8ff512015-09-01 14:26:35 -0600215 __be32 addr, u32 tb_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
David S. Miller9ade2282011-03-12 02:02:42 -0500217 struct flowi4 fl4 = { .daddr = addr };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 struct fib_result res;
Eric Dumazet95c96172012-04-15 05:58:06 +0000219 unsigned int ret = RTN_BROADCAST;
David Ahern15be405e2015-08-13 14:59:04 -0600220 struct fib_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Jan Engelhardt1e637c72008-01-21 03:18:08 -0800222 if (ipv4_is_zeronet(addr) || ipv4_is_lbcast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 return RTN_BROADCAST;
Joe Perchesf97c1e02007-12-16 13:45:43 -0800224 if (ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 return RTN_MULTICAST;
226
Alexander Duyck345e9b52014-12-31 10:56:24 -0800227 rcu_read_lock();
228
David Ahern15be405e2015-08-13 14:59:04 -0600229 table = fib_get_table(net, tb_id);
230 if (table) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 ret = RTN_UNICAST;
David Ahern15be405e2015-08-13 14:59:04 -0600232 if (!fib_table_lookup(table, &fl4, &res, FIB_LOOKUP_NOREF)) {
Laszlo Attila Toth055381162007-12-04 23:28:46 -0800233 if (!dev || dev == res.fi->fib_dev)
234 ret = res.type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
236 }
Alexander Duyck345e9b52014-12-31 10:56:24 -0800237
238 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 return ret;
240}
241
David Ahern9b8ff512015-09-01 14:26:35 -0600242unsigned int inet_addr_type_table(struct net *net, __be32 addr, u32 tb_id)
David Ahern15be405e2015-08-13 14:59:04 -0600243{
244 return __inet_dev_addr_type(net, NULL, addr, tb_id);
245}
246EXPORT_SYMBOL(inet_addr_type_table);
247
Eric W. Biederman6b175b22008-01-10 03:25:28 -0800248unsigned int inet_addr_type(struct net *net, __be32 addr)
Laszlo Attila Toth055381162007-12-04 23:28:46 -0800249{
David Ahern15be405e2015-08-13 14:59:04 -0600250 return __inet_dev_addr_type(net, NULL, addr, RT_TABLE_LOCAL);
Laszlo Attila Toth055381162007-12-04 23:28:46 -0800251}
Eric Dumazet4bc2f182010-07-09 21:22:10 +0000252EXPORT_SYMBOL(inet_addr_type);
Laszlo Attila Toth055381162007-12-04 23:28:46 -0800253
Eric W. Biederman6b175b22008-01-10 03:25:28 -0800254unsigned int inet_dev_addr_type(struct net *net, const struct net_device *dev,
255 __be32 addr)
Laszlo Attila Toth055381162007-12-04 23:28:46 -0800256{
David Ahern3236b002015-09-29 20:07:14 -0700257 u32 rt_table = l3mdev_fib_table(dev) ? : RT_TABLE_LOCAL;
David Ahern15be405e2015-08-13 14:59:04 -0600258
259 return __inet_dev_addr_type(net, dev, addr, rt_table);
Laszlo Attila Toth055381162007-12-04 23:28:46 -0800260}
Eric Dumazet4bc2f182010-07-09 21:22:10 +0000261EXPORT_SYMBOL(inet_dev_addr_type);
Laszlo Attila Toth055381162007-12-04 23:28:46 -0800262
David Ahern30bbaa12015-08-13 14:59:05 -0600263/* inet_addr_type with dev == NULL but using the table from a dev
264 * if one is associated
265 */
266unsigned int inet_addr_type_dev_table(struct net *net,
267 const struct net_device *dev,
268 __be32 addr)
269{
David Ahern3236b002015-09-29 20:07:14 -0700270 u32 rt_table = l3mdev_fib_table(dev) ? : RT_TABLE_LOCAL;
David Ahern30bbaa12015-08-13 14:59:05 -0600271
272 return __inet_dev_addr_type(net, NULL, addr, rt_table);
273}
274EXPORT_SYMBOL(inet_addr_type_dev_table);
275
David S. Miller35ebf652012-06-28 03:59:11 -0700276__be32 fib_compute_spec_dst(struct sk_buff *skb)
277{
278 struct net_device *dev = skb->dev;
279 struct in_device *in_dev;
280 struct fib_result res;
David S. Millera207a4b2012-06-28 18:33:24 -0700281 struct rtable *rt;
David S. Miller35ebf652012-06-28 03:59:11 -0700282 struct net *net;
David S. Millera207a4b2012-06-28 18:33:24 -0700283 int scope;
David S. Miller35ebf652012-06-28 03:59:11 -0700284
David S. Millera207a4b2012-06-28 18:33:24 -0700285 rt = skb_rtable(skb);
Julian Anastasov0cc535a2012-07-18 21:35:03 +0000286 if ((rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST | RTCF_LOCAL)) ==
287 RTCF_LOCAL)
David S. Miller35ebf652012-06-28 03:59:11 -0700288 return ip_hdr(skb)->daddr;
289
290 in_dev = __in_dev_get_rcu(dev);
David S. Miller35ebf652012-06-28 03:59:11 -0700291
292 net = dev_net(dev);
David S. Millera207a4b2012-06-28 18:33:24 -0700293
294 scope = RT_SCOPE_UNIVERSE;
295 if (!ipv4_is_zeronet(ip_hdr(skb)->saddr)) {
Lorenzo Bianconi9fc12022018-07-27 18:15:46 +0200296 bool vmark = in_dev && IN_DEV_SRC_VMARK(in_dev);
Lance Richardson4cfc86f2016-03-22 14:56:57 -0400297 struct flowi4 fl4 = {
298 .flowi4_iif = LOOPBACK_IFINDEX,
David Aherne7372192018-07-07 16:15:26 -0700299 .flowi4_oif = l3mdev_master_ifindex_rcu(dev),
Lance Richardson4cfc86f2016-03-22 14:56:57 -0400300 .daddr = ip_hdr(skb)->saddr,
301 .flowi4_tos = RT_TOS(ip_hdr(skb)->tos),
302 .flowi4_scope = scope,
Lorenzo Bianconi9fc12022018-07-27 18:15:46 +0200303 .flowi4_mark = vmark ? skb->mark : 0,
Lance Richardson4cfc86f2016-03-22 14:56:57 -0400304 };
Andy Gospodarek0eeb0752015-06-23 13:45:37 -0400305 if (!fib_lookup(net, &fl4, &res, 0))
David Aherneba618a2019-04-02 14:11:55 -0700306 return fib_result_prefsrc(net, &res);
David S. Millera207a4b2012-06-28 18:33:24 -0700307 } else {
308 scope = RT_SCOPE_LINK;
309 }
310
311 return inet_select_addr(dev, ip_hdr(skb)->saddr, scope);
David S. Miller35ebf652012-06-28 03:59:11 -0700312}
313
David Ahern78f27562018-09-20 13:50:47 -0700314bool fib_info_nh_uses_dev(struct fib_info *fi, const struct net_device *dev)
315{
316 bool dev_match = false;
Eric Dumazet075e2642018-09-21 10:58:07 -0700317#ifdef CONFIG_IP_ROUTE_MULTIPATH
David Ahern78f27562018-09-20 13:50:47 -0700318 int ret;
319
David Ahern78f27562018-09-20 13:50:47 -0700320 for (ret = 0; ret < fi->fib_nhs; ret++) {
321 struct fib_nh *nh = &fi->fib_nh[ret];
322
David Ahernb75ed8b2019-03-27 20:53:55 -0700323 if (nh->fib_nh_dev == dev) {
David Ahern78f27562018-09-20 13:50:47 -0700324 dev_match = true;
325 break;
David Ahernb75ed8b2019-03-27 20:53:55 -0700326 } else if (l3mdev_master_ifindex_rcu(nh->fib_nh_dev) == dev->ifindex) {
David Ahern78f27562018-09-20 13:50:47 -0700327 dev_match = true;
328 break;
329 }
330 }
331#else
David Ahernb75ed8b2019-03-27 20:53:55 -0700332 if (fi->fib_nh[0].fib_nh_dev == dev)
David Ahern78f27562018-09-20 13:50:47 -0700333 dev_match = true;
334#endif
335
336 return dev_match;
337}
338EXPORT_SYMBOL_GPL(fib_info_nh_uses_dev);
339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340/* Given (packet source, input interface) and optional (dst, oif, tos):
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000341 * - (main) check, that source is valid i.e. not broadcast or our local
342 * address.
343 * - figure out what "logical" interface this packet arrived
344 * and calculate "specific destination" address.
345 * - check, that packet arrived from expected physical interface.
Eric Dumazetebc0ffa2010-10-05 10:41:36 +0000346 * called with rcu_read_lock()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 */
David S. Miller7a9bc9b2012-06-29 01:32:45 -0700348static int __fib_validate_source(struct sk_buff *skb, __be32 src, __be32 dst,
349 u8 tos, int oif, struct net_device *dev,
350 int rpf, struct in_device *idev, u32 *itag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
David Ahern5a847a62018-05-16 13:36:40 -0700352 struct net *net = dev_net(dev);
353 struct flow_keys flkeys;
Sébastien Barré1dced6a2014-08-17 09:19:54 +0200354 int ret, no_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 struct fib_result res;
David S. Miller9e56e382012-06-28 18:54:02 -0700356 struct flowi4 fl4;
David S. Miller9e56e382012-06-28 18:54:02 -0700357 bool dev_match;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
David S. Miller9ade2282011-03-12 02:02:42 -0500359 fl4.flowi4_oif = 0;
David Ahern385add92015-09-29 20:07:13 -0700360 fl4.flowi4_iif = l3mdev_master_ifindex_rcu(dev);
David Aherncd2fbe12015-08-13 14:59:01 -0600361 if (!fl4.flowi4_iif)
362 fl4.flowi4_iif = oif ? : LOOPBACK_IFINDEX;
David S. Miller9ade2282011-03-12 02:02:42 -0500363 fl4.daddr = src;
364 fl4.saddr = dst;
365 fl4.flowi4_tos = tos;
366 fl4.flowi4_scope = RT_SCOPE_UNIVERSE;
Thomas Graf1b7179d2015-07-21 10:43:59 +0200367 fl4.flowi4_tun_key.tun_id = 0;
David Ahernb84f7872015-09-29 19:07:07 -0700368 fl4.flowi4_flags = 0;
Julian Anastasov8bcfd092017-02-26 15:50:52 +0200369 fl4.flowi4_uid = sock_net_uid(net, NULL);
David S. Millercc7e17e2011-03-09 20:57:50 -0800370
David S. Miller9e56e382012-06-28 18:54:02 -0700371 no_addr = idev->ifa_list == NULL;
Michael Smith990078a2011-04-07 04:51:51 +0000372
David S. Miller9e56e382012-06-28 18:54:02 -0700373 fl4.flowi4_mark = IN_DEV_SRC_VMARK(idev) ? skb->mark : 0;
David Ahern5a847a62018-05-16 13:36:40 -0700374 if (!fib4_rules_early_flow_dissect(net, skb, &fl4, &flkeys)) {
375 fl4.flowi4_proto = 0;
376 fl4.fl4_sport = 0;
377 fl4.fl4_dport = 0;
378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Andy Gospodarek0eeb0752015-06-23 13:45:37 -0400380 if (fib_lookup(net, &fl4, &res, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 goto last_resort;
Sébastien Barré1dced6a2014-08-17 09:19:54 +0200382 if (res.type != RTN_UNICAST &&
383 (res.type != RTN_LOCAL || !IN_DEV_ACCEPT_LOCAL(idev)))
384 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 fib_combine_itag(itag, &res);
David S. Miller6f86b322010-09-06 22:36:19 -0700386
David Ahern78f27562018-09-20 13:50:47 -0700387 dev_match = fib_info_nh_uses_dev(res.fi, dev);
David S. Miller6f86b322010-09-06 22:36:19 -0700388 if (dev_match) {
David Aherneba618a2019-04-02 14:11:55 -0700389 ret = FIB_RES_NHC(res)->nhc_scope >= RT_SCOPE_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 return ret;
391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 if (no_addr)
393 goto last_resort;
Stephen Hemmingerc1cf8422009-02-20 08:25:36 +0000394 if (rpf == 1)
Eric Dumazetb5f7e752010-06-02 12:05:27 +0000395 goto e_rpf;
David S. Miller9ade2282011-03-12 02:02:42 -0500396 fl4.flowi4_oif = dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
398 ret = 0;
Andy Gospodarek0eeb0752015-06-23 13:45:37 -0400399 if (fib_lookup(net, &fl4, &res, FIB_LOOKUP_IGNORE_LINKSTATE) == 0) {
David S. Miller41347dc2012-06-28 04:05:27 -0700400 if (res.type == RTN_UNICAST)
David Aherneba618a2019-04-02 14:11:55 -0700401 ret = FIB_RES_NHC(res)->nhc_scope >= RT_SCOPE_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 }
403 return ret;
404
405last_resort:
406 if (rpf)
Eric Dumazetb5f7e752010-06-02 12:05:27 +0000407 goto e_rpf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 *itag = 0;
409 return 0;
410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411e_inval:
412 return -EINVAL;
Eric Dumazetb5f7e752010-06-02 12:05:27 +0000413e_rpf:
414 return -EXDEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415}
416
David S. Miller7a9bc9b2012-06-29 01:32:45 -0700417/* Ignore rp_filter for packets protected by IPsec. */
418int fib_validate_source(struct sk_buff *skb, __be32 src, __be32 dst,
419 u8 tos, int oif, struct net_device *dev,
420 struct in_device *idev, u32 *itag)
421{
422 int r = secpath_exists(skb) ? 0 : IN_DEV_RPFILTER(idev);
Paolo Abeni6e617de2017-09-20 18:26:53 +0200423 struct net *net = dev_net(dev);
David S. Miller7a9bc9b2012-06-29 01:32:45 -0700424
Paolo Abeni6e617de2017-09-20 18:26:53 +0200425 if (!r && !fib_num_tclassid_users(net) &&
Julian Anastasove81da0e2012-10-08 11:41:15 +0000426 (dev->ifindex != oif || !IN_DEV_TX_REDIRECTS(idev))) {
Paolo Abeni6e617de2017-09-20 18:26:53 +0200427 if (IN_DEV_ACCEPT_LOCAL(idev))
428 goto ok;
Paolo Abeni032a4802017-10-31 14:32:38 +0100429 /* with custom local routes in place, checking local addresses
430 * only will be too optimistic, with custom rules, checking
431 * local addresses only can be too strict, e.g. due to vrf
Paolo Abeni6e617de2017-09-20 18:26:53 +0200432 */
Paolo Abeni032a4802017-10-31 14:32:38 +0100433 if (net->ipv4.fib_has_custom_local_routes ||
434 fib4_has_custom_rules(net))
Paolo Abeni6e617de2017-09-20 18:26:53 +0200435 goto full_check;
436 if (inet_lookup_ifaddr_rcu(net, src))
437 return -EINVAL;
438
439ok:
David S. Miller7a9bc9b2012-06-29 01:32:45 -0700440 *itag = 0;
441 return 0;
442 }
Paolo Abeni6e617de2017-09-20 18:26:53 +0200443
444full_check:
David S. Miller7a9bc9b2012-06-29 01:32:45 -0700445 return __fib_validate_source(skb, src, dst, tos, oif, dev, r, idev, itag);
446}
447
Al Viro81f7bf62006-09-27 18:40:00 -0700448static inline __be32 sk_extract_addr(struct sockaddr *addr)
Thomas Graf4e902c52006-08-17 18:14:52 -0700449{
450 return ((struct sockaddr_in *) addr)->sin_addr.s_addr;
451}
452
453static int put_rtax(struct nlattr *mx, int len, int type, u32 value)
454{
455 struct nlattr *nla;
456
457 nla = (struct nlattr *) ((char *) mx + len);
458 nla->nla_type = type;
459 nla->nla_len = nla_attr_size(4);
460 *(u32 *) nla_data(nla) = value;
461
462 return len + nla_total_size(4);
463}
464
Denis V. Lunev4b5d47d2008-01-10 03:29:23 -0800465static int rtentry_to_fib_config(struct net *net, int cmd, struct rtentry *rt,
Thomas Graf4e902c52006-08-17 18:14:52 -0700466 struct fib_config *cfg)
467{
Al Viro6d85c102006-09-26 22:15:46 -0700468 __be32 addr;
Thomas Graf4e902c52006-08-17 18:14:52 -0700469 int plen;
470
471 memset(cfg, 0, sizeof(*cfg));
Denis V. Lunev4b5d47d2008-01-10 03:29:23 -0800472 cfg->fc_nlinfo.nl_net = net;
Thomas Graf4e902c52006-08-17 18:14:52 -0700473
474 if (rt->rt_dst.sa_family != AF_INET)
475 return -EAFNOSUPPORT;
476
477 /*
478 * Check mask for validity:
479 * a) it must be contiguous.
480 * b) destination must have all host bits clear.
481 * c) if application forgot to set correct family (AF_INET),
482 * reject request unless it is absolutely clear i.e.
483 * both family and mask are zero.
484 */
485 plen = 32;
486 addr = sk_extract_addr(&rt->rt_dst);
487 if (!(rt->rt_flags & RTF_HOST)) {
Al Viro81f7bf62006-09-27 18:40:00 -0700488 __be32 mask = sk_extract_addr(&rt->rt_genmask);
Thomas Graf4e902c52006-08-17 18:14:52 -0700489
490 if (rt->rt_genmask.sa_family != AF_INET) {
491 if (mask || rt->rt_genmask.sa_family)
492 return -EAFNOSUPPORT;
493 }
494
495 if (bad_mask(mask, addr))
496 return -EINVAL;
497
498 plen = inet_mask_len(mask);
499 }
500
501 cfg->fc_dst_len = plen;
502 cfg->fc_dst = addr;
503
504 if (cmd != SIOCDELRT) {
505 cfg->fc_nlflags = NLM_F_CREATE;
506 cfg->fc_protocol = RTPROT_BOOT;
507 }
508
509 if (rt->rt_metric)
510 cfg->fc_priority = rt->rt_metric - 1;
511
512 if (rt->rt_flags & RTF_REJECT) {
513 cfg->fc_scope = RT_SCOPE_HOST;
514 cfg->fc_type = RTN_UNREACHABLE;
515 return 0;
516 }
517
518 cfg->fc_scope = RT_SCOPE_NOWHERE;
519 cfg->fc_type = RTN_UNICAST;
520
521 if (rt->rt_dev) {
522 char *colon;
523 struct net_device *dev;
524 char devname[IFNAMSIZ];
525
526 if (copy_from_user(devname, rt->rt_dev, IFNAMSIZ-1))
527 return -EFAULT;
528
529 devname[IFNAMSIZ-1] = 0;
530 colon = strchr(devname, ':');
531 if (colon)
532 *colon = 0;
Denis V. Lunev4b5d47d2008-01-10 03:29:23 -0800533 dev = __dev_get_by_name(net, devname);
Thomas Graf4e902c52006-08-17 18:14:52 -0700534 if (!dev)
535 return -ENODEV;
536 cfg->fc_oif = dev->ifindex;
Mark Tomlinson5a56a0b2016-09-05 10:20:20 +1200537 cfg->fc_table = l3mdev_fib_table(dev);
Thomas Graf4e902c52006-08-17 18:14:52 -0700538 if (colon) {
539 struct in_ifaddr *ifa;
540 struct in_device *in_dev = __in_dev_get_rtnl(dev);
541 if (!in_dev)
542 return -ENODEV;
543 *colon = ':';
544 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next)
545 if (strcmp(ifa->ifa_label, devname) == 0)
546 break;
Ian Morris51456b22015-04-03 09:17:26 +0100547 if (!ifa)
Thomas Graf4e902c52006-08-17 18:14:52 -0700548 return -ENODEV;
549 cfg->fc_prefsrc = ifa->ifa_local;
550 }
551 }
552
553 addr = sk_extract_addr(&rt->rt_gateway);
554 if (rt->rt_gateway.sa_family == AF_INET && addr) {
David Ahern30bbaa12015-08-13 14:59:05 -0600555 unsigned int addr_type;
556
David Ahernf35b7942019-04-05 16:30:28 -0700557 cfg->fc_gw4 = addr;
558 cfg->fc_gw_family = AF_INET;
David Ahern30bbaa12015-08-13 14:59:05 -0600559 addr_type = inet_addr_type_table(net, addr, cfg->fc_table);
Thomas Graf4e902c52006-08-17 18:14:52 -0700560 if (rt->rt_flags & RTF_GATEWAY &&
David Ahern30bbaa12015-08-13 14:59:05 -0600561 addr_type == RTN_UNICAST)
Thomas Graf4e902c52006-08-17 18:14:52 -0700562 cfg->fc_scope = RT_SCOPE_UNIVERSE;
563 }
564
565 if (cmd == SIOCDELRT)
566 return 0;
567
David Ahernf35b7942019-04-05 16:30:28 -0700568 if (rt->rt_flags & RTF_GATEWAY && !cfg->fc_gw_family)
Thomas Graf4e902c52006-08-17 18:14:52 -0700569 return -EINVAL;
570
571 if (cfg->fc_scope == RT_SCOPE_NOWHERE)
572 cfg->fc_scope = RT_SCOPE_LINK;
573
574 if (rt->rt_flags & (RTF_MTU | RTF_WINDOW | RTF_IRTT)) {
575 struct nlattr *mx;
576 int len = 0;
577
Kees Cook6396bb22018-06-12 14:03:40 -0700578 mx = kcalloc(3, nla_total_size(4), GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +0100579 if (!mx)
Thomas Graf4e902c52006-08-17 18:14:52 -0700580 return -ENOMEM;
581
582 if (rt->rt_flags & RTF_MTU)
583 len = put_rtax(mx, len, RTAX_ADVMSS, rt->rt_mtu - 40);
584
585 if (rt->rt_flags & RTF_WINDOW)
586 len = put_rtax(mx, len, RTAX_WINDOW, rt->rt_window);
587
588 if (rt->rt_flags & RTF_IRTT)
589 len = put_rtax(mx, len, RTAX_RTT, rt->rt_irtt << 3);
590
591 cfg->fc_mx = mx;
592 cfg->fc_mx_len = len;
593 }
594
595 return 0;
596}
597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598/*
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000599 * Handle IP routing ioctl calls.
600 * These are used to manipulate the routing tables
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 */
Al Viroca25c302017-07-01 08:03:10 -0400602int ip_rt_ioctl(struct net *net, unsigned int cmd, struct rtentry *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
Thomas Graf4e902c52006-08-17 18:14:52 -0700604 struct fib_config cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607 switch (cmd) {
608 case SIOCADDRT: /* Add a route */
609 case SIOCDELRT: /* Delete a route */
Eric W. Biederman52e804c2012-11-16 03:03:05 +0000610 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 return -EPERM;
Thomas Graf4e902c52006-08-17 18:14:52 -0700612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 rtnl_lock();
Al Viroca25c302017-07-01 08:03:10 -0400614 err = rtentry_to_fib_config(net, cmd, rt, &cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 if (err == 0) {
Thomas Graf4e902c52006-08-17 18:14:52 -0700616 struct fib_table *tb;
617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 if (cmd == SIOCDELRT) {
Denis V. Lunev1bad1182008-01-10 03:29:53 -0800619 tb = fib_get_table(net, cfg.fc_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 if (tb)
David Ahern78055992017-05-27 16:19:26 -0600621 err = fib_table_delete(net, tb, &cfg,
622 NULL);
Thomas Graf4e902c52006-08-17 18:14:52 -0700623 else
624 err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 } else {
Denis V. Lunev1bad1182008-01-10 03:29:53 -0800626 tb = fib_new_table(net, cfg.fc_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 if (tb)
David Ahern6d8422a12017-05-21 10:12:02 -0600628 err = fib_table_insert(net, tb,
629 &cfg, NULL);
Thomas Graf4e902c52006-08-17 18:14:52 -0700630 else
631 err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 }
Thomas Graf4e902c52006-08-17 18:14:52 -0700633
634 /* allocated by rtentry_to_fib_config() */
635 kfree(cfg.fc_mx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 }
637 rtnl_unlock();
638 return err;
639 }
640 return -EINVAL;
641}
642
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000643const struct nla_policy rtm_ipv4_policy[RTA_MAX + 1] = {
Thomas Graf4e902c52006-08-17 18:14:52 -0700644 [RTA_DST] = { .type = NLA_U32 },
645 [RTA_SRC] = { .type = NLA_U32 },
646 [RTA_IIF] = { .type = NLA_U32 },
647 [RTA_OIF] = { .type = NLA_U32 },
648 [RTA_GATEWAY] = { .type = NLA_U32 },
649 [RTA_PRIORITY] = { .type = NLA_U32 },
650 [RTA_PREFSRC] = { .type = NLA_U32 },
651 [RTA_METRICS] = { .type = NLA_NESTED },
Thomas Graf5176f912006-08-26 20:13:18 -0700652 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
Thomas Graf4e902c52006-08-17 18:14:52 -0700653 [RTA_FLOW] = { .type = NLA_U32 },
Roopa Prabhu571e7222015-07-21 10:43:47 +0200654 [RTA_ENCAP_TYPE] = { .type = NLA_U16 },
655 [RTA_ENCAP] = { .type = NLA_NESTED },
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +0900656 [RTA_UID] = { .type = NLA_U32 },
Liping Zhang3b45a412017-02-27 20:59:39 +0800657 [RTA_MARK] = { .type = NLA_U32 },
Roopa Prabhu2eabd762018-05-22 13:44:51 -0700658 [RTA_TABLE] = { .type = NLA_U32 },
Roopa Prabhu404eb772018-05-22 14:03:27 -0700659 [RTA_IP_PROTO] = { .type = NLA_U8 },
660 [RTA_SPORT] = { .type = NLA_U16 },
661 [RTA_DPORT] = { .type = NLA_U16 },
Thomas Graf4e902c52006-08-17 18:14:52 -0700662};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
David Ahernd1566262019-04-05 16:30:40 -0700664int fib_gw_from_via(struct fib_config *cfg, struct nlattr *nla,
665 struct netlink_ext_ack *extack)
666{
667 struct rtvia *via;
668 int alen;
669
670 if (nla_len(nla) < offsetof(struct rtvia, rtvia_addr)) {
671 NL_SET_ERR_MSG(extack, "Invalid attribute length for RTA_VIA");
672 return -EINVAL;
673 }
674
675 via = nla_data(nla);
676 alen = nla_len(nla) - offsetof(struct rtvia, rtvia_addr);
677
678 switch (via->rtvia_family) {
679 case AF_INET:
680 if (alen != sizeof(__be32)) {
681 NL_SET_ERR_MSG(extack, "Invalid IPv4 address in RTA_VIA");
682 return -EINVAL;
683 }
684 cfg->fc_gw_family = AF_INET;
685 cfg->fc_gw4 = *((__be32 *)via->rtvia_addr);
686 break;
687 case AF_INET6:
688#ifdef CONFIG_IPV6
689 if (alen != sizeof(struct in6_addr)) {
690 NL_SET_ERR_MSG(extack, "Invalid IPv6 address in RTA_VIA");
691 return -EINVAL;
692 }
693 cfg->fc_gw_family = AF_INET6;
694 cfg->fc_gw6 = *((struct in6_addr *)via->rtvia_addr);
695#else
696 NL_SET_ERR_MSG(extack, "IPv6 support not enabled in kernel");
697 return -EINVAL;
698#endif
699 break;
700 default:
701 NL_SET_ERR_MSG(extack, "Unsupported address family in RTA_VIA");
702 return -EINVAL;
703 }
704
705 return 0;
706}
707
Denis V. Lunev4b5d47d2008-01-10 03:29:23 -0800708static int rtm_to_fib_config(struct net *net, struct sk_buff *skb,
David Ahern6d8422a12017-05-21 10:12:02 -0600709 struct nlmsghdr *nlh, struct fib_config *cfg,
710 struct netlink_ext_ack *extack)
Thomas Graf4e902c52006-08-17 18:14:52 -0700711{
David Ahernd1566262019-04-05 16:30:40 -0700712 bool has_gw = false, has_via = false;
Thomas Graf4e902c52006-08-17 18:14:52 -0700713 struct nlattr *attr;
714 int err, remaining;
715 struct rtmsg *rtm;
716
Johannes Berg8cb08172019-04-26 14:07:28 +0200717 err = nlmsg_validate_deprecated(nlh, sizeof(*rtm), RTA_MAX,
718 rtm_ipv4_policy, extack);
Thomas Graf4e902c52006-08-17 18:14:52 -0700719 if (err < 0)
720 goto errout;
721
722 memset(cfg, 0, sizeof(*cfg));
723
724 rtm = nlmsg_data(nlh);
Thomas Graf4e902c52006-08-17 18:14:52 -0700725 cfg->fc_dst_len = rtm->rtm_dst_len;
Thomas Graf4e902c52006-08-17 18:14:52 -0700726 cfg->fc_tos = rtm->rtm_tos;
727 cfg->fc_table = rtm->rtm_table;
728 cfg->fc_protocol = rtm->rtm_protocol;
729 cfg->fc_scope = rtm->rtm_scope;
730 cfg->fc_type = rtm->rtm_type;
731 cfg->fc_flags = rtm->rtm_flags;
732 cfg->fc_nlflags = nlh->nlmsg_flags;
733
Eric W. Biederman15e47302012-09-07 20:12:54 +0000734 cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid;
Thomas Graf4e902c52006-08-17 18:14:52 -0700735 cfg->fc_nlinfo.nlh = nlh;
Denis V. Lunev4b5d47d2008-01-10 03:29:23 -0800736 cfg->fc_nlinfo.nl_net = net;
Thomas Graf4e902c52006-08-17 18:14:52 -0700737
Thomas Grafa0ee18b2007-03-24 20:32:54 -0700738 if (cfg->fc_type > RTN_MAX) {
David Ahernc3ab2b42017-05-21 10:12:03 -0600739 NL_SET_ERR_MSG(extack, "Invalid route type");
Thomas Grafa0ee18b2007-03-24 20:32:54 -0700740 err = -EINVAL;
741 goto errout;
742 }
743
Thomas Graf4e902c52006-08-17 18:14:52 -0700744 nlmsg_for_each_attr(attr, nlh, sizeof(struct rtmsg), remaining) {
Thomas Graf8f4c1f92007-09-12 14:44:36 +0200745 switch (nla_type(attr)) {
Thomas Graf4e902c52006-08-17 18:14:52 -0700746 case RTA_DST:
Al Viro17fb2c62006-09-26 22:15:25 -0700747 cfg->fc_dst = nla_get_be32(attr);
Thomas Graf4e902c52006-08-17 18:14:52 -0700748 break;
Thomas Graf4e902c52006-08-17 18:14:52 -0700749 case RTA_OIF:
750 cfg->fc_oif = nla_get_u32(attr);
751 break;
752 case RTA_GATEWAY:
David Ahernd1566262019-04-05 16:30:40 -0700753 has_gw = true;
David Ahernf35b7942019-04-05 16:30:28 -0700754 cfg->fc_gw4 = nla_get_be32(attr);
David Ahernd73f80f2019-04-10 10:05:51 -0700755 if (cfg->fc_gw4)
756 cfg->fc_gw_family = AF_INET;
Thomas Graf4e902c52006-08-17 18:14:52 -0700757 break;
David Ahernb6e9e5d2019-02-26 09:00:02 -0800758 case RTA_VIA:
David Ahernd1566262019-04-05 16:30:40 -0700759 has_via = true;
760 err = fib_gw_from_via(cfg, attr, extack);
761 if (err)
762 goto errout;
763 break;
Thomas Graf4e902c52006-08-17 18:14:52 -0700764 case RTA_PRIORITY:
765 cfg->fc_priority = nla_get_u32(attr);
766 break;
767 case RTA_PREFSRC:
Al Viro17fb2c62006-09-26 22:15:25 -0700768 cfg->fc_prefsrc = nla_get_be32(attr);
Thomas Graf4e902c52006-08-17 18:14:52 -0700769 break;
770 case RTA_METRICS:
771 cfg->fc_mx = nla_data(attr);
772 cfg->fc_mx_len = nla_len(attr);
773 break;
774 case RTA_MULTIPATH:
David Ahern9ed59592017-01-17 14:57:36 -0800775 err = lwtunnel_valid_encap_type_attr(nla_data(attr),
David Ahernc255bd62017-05-27 16:19:27 -0600776 nla_len(attr),
777 extack);
David Ahern9ed59592017-01-17 14:57:36 -0800778 if (err < 0)
779 goto errout;
Thomas Graf4e902c52006-08-17 18:14:52 -0700780 cfg->fc_mp = nla_data(attr);
781 cfg->fc_mp_len = nla_len(attr);
782 break;
783 case RTA_FLOW:
784 cfg->fc_flow = nla_get_u32(attr);
785 break;
Thomas Graf4e902c52006-08-17 18:14:52 -0700786 case RTA_TABLE:
787 cfg->fc_table = nla_get_u32(attr);
788 break;
Roopa Prabhu571e7222015-07-21 10:43:47 +0200789 case RTA_ENCAP:
790 cfg->fc_encap = attr;
791 break;
792 case RTA_ENCAP_TYPE:
793 cfg->fc_encap_type = nla_get_u16(attr);
David Ahernc255bd62017-05-27 16:19:27 -0600794 err = lwtunnel_valid_encap_type(cfg->fc_encap_type,
795 extack);
David Ahern9ed59592017-01-17 14:57:36 -0800796 if (err < 0)
797 goto errout;
Roopa Prabhu571e7222015-07-21 10:43:47 +0200798 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 }
800 }
Thomas Graf4e902c52006-08-17 18:14:52 -0700801
David Ahernd1566262019-04-05 16:30:40 -0700802 if (has_gw && has_via) {
803 NL_SET_ERR_MSG(extack,
804 "Nexthop configuration can not contain both GATEWAY and VIA");
805 goto errout;
806 }
807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 return 0;
Thomas Graf4e902c52006-08-17 18:14:52 -0700809errout:
810 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811}
812
David Ahernc21ef3e2017-04-16 09:48:24 -0700813static int inet_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
814 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900816 struct net *net = sock_net(skb->sk);
Thomas Graf4e902c52006-08-17 18:14:52 -0700817 struct fib_config cfg;
818 struct fib_table *tb;
819 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
David Ahern6d8422a12017-05-21 10:12:02 -0600821 err = rtm_to_fib_config(net, skb, nlh, &cfg, extack);
Thomas Graf4e902c52006-08-17 18:14:52 -0700822 if (err < 0)
823 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Denis V. Lunev8ad49422008-01-10 03:24:11 -0800825 tb = fib_get_table(net, cfg.fc_table);
Ian Morris51456b22015-04-03 09:17:26 +0100826 if (!tb) {
David Ahernc3ab2b42017-05-21 10:12:03 -0600827 NL_SET_ERR_MSG(extack, "FIB table does not exist");
Thomas Graf4e902c52006-08-17 18:14:52 -0700828 err = -ESRCH;
829 goto errout;
830 }
831
David Ahern78055992017-05-27 16:19:26 -0600832 err = fib_table_delete(net, tb, &cfg, extack);
Thomas Graf4e902c52006-08-17 18:14:52 -0700833errout:
834 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835}
836
David Ahernc21ef3e2017-04-16 09:48:24 -0700837static int inet_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
838 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900840 struct net *net = sock_net(skb->sk);
Thomas Graf4e902c52006-08-17 18:14:52 -0700841 struct fib_config cfg;
842 struct fib_table *tb;
843 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
David Ahern6d8422a12017-05-21 10:12:02 -0600845 err = rtm_to_fib_config(net, skb, nlh, &cfg, extack);
Thomas Graf4e902c52006-08-17 18:14:52 -0700846 if (err < 0)
847 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Denis V. Lunev226b0b4a52008-01-10 03:30:24 -0800849 tb = fib_new_table(net, cfg.fc_table);
Ian Morris51456b22015-04-03 09:17:26 +0100850 if (!tb) {
Thomas Graf4e902c52006-08-17 18:14:52 -0700851 err = -ENOBUFS;
852 goto errout;
853 }
854
David Ahern6d8422a12017-05-21 10:12:02 -0600855 err = fib_table_insert(net, tb, &cfg, extack);
Paolo Abeni6e617de2017-09-20 18:26:53 +0200856 if (!err && cfg.fc_type == RTN_LOCAL)
857 net->ipv4.fib_has_custom_local_routes = true;
Thomas Graf4e902c52006-08-17 18:14:52 -0700858errout:
859 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860}
861
David Ahern47246762018-10-15 18:56:42 -0700862int ip_valid_fib_dump_req(struct net *net, const struct nlmsghdr *nlh,
863 struct fib_dump_filter *filter,
David Aherneffe6792018-10-15 18:56:48 -0700864 struct netlink_callback *cb)
David Aherne8ba3302018-10-07 20:16:35 -0700865{
David Aherneffe6792018-10-15 18:56:48 -0700866 struct netlink_ext_ack *extack = cb->extack;
867 struct nlattr *tb[RTA_MAX + 1];
David Aherne8ba3302018-10-07 20:16:35 -0700868 struct rtmsg *rtm;
David Aherneffe6792018-10-15 18:56:48 -0700869 int err, i;
870
871 ASSERT_RTNL();
David Aherne8ba3302018-10-07 20:16:35 -0700872
873 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
874 NL_SET_ERR_MSG(extack, "Invalid header for FIB dump request");
875 return -EINVAL;
876 }
877
878 rtm = nlmsg_data(nlh);
879 if (rtm->rtm_dst_len || rtm->rtm_src_len || rtm->rtm_tos ||
David Aherneffe6792018-10-15 18:56:48 -0700880 rtm->rtm_scope) {
David Aherne8ba3302018-10-07 20:16:35 -0700881 NL_SET_ERR_MSG(extack, "Invalid values in header for FIB dump request");
882 return -EINVAL;
883 }
884 if (rtm->rtm_flags & ~(RTM_F_CLONED | RTM_F_PREFIX)) {
885 NL_SET_ERR_MSG(extack, "Invalid flags for FIB dump request");
886 return -EINVAL;
887 }
888
David Ahernae677bb2018-10-24 12:59:01 -0700889 filter->dump_all_families = (rtm->rtm_family == AF_UNSPEC);
David Aherneffe6792018-10-15 18:56:48 -0700890 filter->flags = rtm->rtm_flags;
891 filter->protocol = rtm->rtm_protocol;
892 filter->rt_type = rtm->rtm_type;
893 filter->table_id = rtm->rtm_table;
894
Johannes Berg8cb08172019-04-26 14:07:28 +0200895 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
896 rtm_ipv4_policy, extack);
David Aherneffe6792018-10-15 18:56:48 -0700897 if (err < 0)
898 return err;
899
900 for (i = 0; i <= RTA_MAX; ++i) {
901 int ifindex;
902
903 if (!tb[i])
904 continue;
905
906 switch (i) {
907 case RTA_TABLE:
908 filter->table_id = nla_get_u32(tb[i]);
909 break;
910 case RTA_OIF:
911 ifindex = nla_get_u32(tb[i]);
912 filter->dev = __dev_get_by_index(net, ifindex);
913 if (!filter->dev)
914 return -ENODEV;
915 break;
916 default:
917 NL_SET_ERR_MSG(extack, "Unsupported attribute in dump request");
918 return -EINVAL;
919 }
920 }
921
922 if (filter->flags || filter->protocol || filter->rt_type ||
923 filter->table_id || filter->dev) {
924 filter->filter_set = 1;
925 cb->answer_flags = NLM_F_DUMP_FILTERED;
David Aherne8ba3302018-10-07 20:16:35 -0700926 }
927
928 return 0;
929}
930EXPORT_SYMBOL_GPL(ip_valid_fib_dump_req);
931
Thomas Graf63f34442007-03-22 11:55:17 -0700932static int inet_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
David Aherne8ba3302018-10-07 20:16:35 -0700934 const struct nlmsghdr *nlh = cb->nlh;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900935 struct net *net = sock_net(skb->sk);
David Ahern47246762018-10-15 18:56:42 -0700936 struct fib_dump_filter filter = {};
Patrick McHardy1af5a8c2006-08-10 23:10:46 -0700937 unsigned int h, s_h;
938 unsigned int e = 0, s_e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 struct fib_table *tb;
Denis V. Luneve4aef8a2008-01-10 03:28:24 -0800940 struct hlist_head *head;
David Ahernf6c57752017-05-15 23:19:17 -0700941 int dumped = 0, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
David Aherne8ba3302018-10-07 20:16:35 -0700943 if (cb->strict_check) {
David Aherneffe6792018-10-15 18:56:48 -0700944 err = ip_valid_fib_dump_req(net, nlh, &filter, cb);
David Aherne8ba3302018-10-07 20:16:35 -0700945 if (err < 0)
946 return err;
David Aherne4e92fb2018-10-15 18:56:51 -0700947 } else if (nlmsg_len(nlh) >= sizeof(struct rtmsg)) {
948 struct rtmsg *rtm = nlmsg_data(nlh);
949
950 filter.flags = rtm->rtm_flags & (RTM_F_PREFIX | RTM_F_CLONED);
David Aherne8ba3302018-10-07 20:16:35 -0700951 }
952
David Aherne4e92fb2018-10-15 18:56:51 -0700953 /* fib entries are never clones and ipv4 does not use prefix flag */
954 if (filter.flags & (RTM_F_PREFIX | RTM_F_CLONED))
Li RongQing0b8c7f62014-03-21 11:33:10 +0800955 return skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
David Ahern18a80212018-10-15 18:56:43 -0700957 if (filter.table_id) {
958 tb = fib_get_table(net, filter.table_id);
959 if (!tb) {
David Ahernae677bb2018-10-24 12:59:01 -0700960 if (filter.dump_all_families)
961 return skb->len;
962
David Ahern18a80212018-10-15 18:56:43 -0700963 NL_SET_ERR_MSG(cb->extack, "ipv4: FIB table does not exist");
964 return -ENOENT;
965 }
966
967 err = fib_table_dump(tb, skb, cb, &filter);
968 return skb->len ? : err;
969 }
970
Patrick McHardy1af5a8c2006-08-10 23:10:46 -0700971 s_h = cb->args[0];
972 s_e = cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Alexander Duycka7e53532015-03-04 15:02:44 -0800974 rcu_read_lock();
975
Patrick McHardy1af5a8c2006-08-10 23:10:46 -0700976 for (h = s_h; h < FIB_TABLE_HASHSZ; h++, s_e = 0) {
977 e = 0;
Denis V. Luneve4aef8a2008-01-10 03:28:24 -0800978 head = &net->ipv4.fib_table_hash[h];
Alexander Duycka7e53532015-03-04 15:02:44 -0800979 hlist_for_each_entry_rcu(tb, head, tb_hlist) {
Patrick McHardy1af5a8c2006-08-10 23:10:46 -0700980 if (e < s_e)
981 goto next;
982 if (dumped)
983 memset(&cb->args[2], 0, sizeof(cb->args) -
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900984 2 * sizeof(cb->args[0]));
David Ahern18a80212018-10-15 18:56:43 -0700985 err = fib_table_dump(tb, skb, cb, &filter);
David Ahernf6c57752017-05-15 23:19:17 -0700986 if (err < 0) {
987 if (likely(skb->len))
988 goto out;
989
990 goto out_err;
991 }
Patrick McHardy1af5a8c2006-08-10 23:10:46 -0700992 dumped = 1;
993next:
994 e++;
995 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 }
Patrick McHardy1af5a8c2006-08-10 23:10:46 -0700997out:
David Ahernf6c57752017-05-15 23:19:17 -0700998 err = skb->len;
999out_err:
Alexander Duycka7e53532015-03-04 15:02:44 -08001000 rcu_read_unlock();
1001
Patrick McHardy1af5a8c2006-08-10 23:10:46 -07001002 cb->args[1] = e;
1003 cb->args[0] = h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
David Ahernf6c57752017-05-15 23:19:17 -07001005 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006}
1007
1008/* Prepare and feed intra-kernel routing request.
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001009 * Really, it should be netlink message, but :-( netlink
1010 * can be not configured, so that we feed it directly
1011 * to fib engine. It is legal, because all events occur
1012 * only when netlink is already locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 */
David Ahernaf4d7682018-05-27 08:09:57 -07001014static void fib_magic(int cmd, int type, __be32 dst, int dst_len,
1015 struct in_ifaddr *ifa, u32 rt_priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001017 struct net *net = dev_net(ifa->ifa_dev->dev);
David Ahern3236b002015-09-29 20:07:14 -07001018 u32 tb_id = l3mdev_fib_table(ifa->ifa_dev->dev);
Thomas Graf4e902c52006-08-17 18:14:52 -07001019 struct fib_table *tb;
1020 struct fib_config cfg = {
1021 .fc_protocol = RTPROT_KERNEL,
1022 .fc_type = type,
1023 .fc_dst = dst,
1024 .fc_dst_len = dst_len,
David Ahernaf4d7682018-05-27 08:09:57 -07001025 .fc_priority = rt_priority,
Thomas Graf4e902c52006-08-17 18:14:52 -07001026 .fc_prefsrc = ifa->ifa_local,
1027 .fc_oif = ifa->ifa_dev->dev->ifindex,
1028 .fc_nlflags = NLM_F_CREATE | NLM_F_APPEND,
Denis V. Lunev4d1169c2008-01-10 03:26:13 -08001029 .fc_nlinfo = {
Denis V. Lunev4b5d47d2008-01-10 03:29:23 -08001030 .nl_net = net,
Denis V. Lunev4d1169c2008-01-10 03:26:13 -08001031 },
Thomas Graf4e902c52006-08-17 18:14:52 -07001032 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
David Ahern021dd3b2015-08-13 14:59:06 -06001034 if (!tb_id)
1035 tb_id = (type == RTN_UNICAST) ? RT_TABLE_MAIN : RT_TABLE_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
David Ahern021dd3b2015-08-13 14:59:06 -06001037 tb = fib_new_table(net, tb_id);
Ian Morris51456b22015-04-03 09:17:26 +01001038 if (!tb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 return;
1040
Thomas Graf4e902c52006-08-17 18:14:52 -07001041 cfg.fc_table = tb->tb_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Thomas Graf4e902c52006-08-17 18:14:52 -07001043 if (type != RTN_LOCAL)
1044 cfg.fc_scope = RT_SCOPE_LINK;
1045 else
1046 cfg.fc_scope = RT_SCOPE_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
1048 if (cmd == RTM_NEWROUTE)
David Ahern6d8422a12017-05-21 10:12:02 -06001049 fib_table_insert(net, tb, &cfg, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 else
David Ahern78055992017-05-27 16:19:26 -06001051 fib_table_delete(net, tb, &cfg, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052}
1053
Jamal Hadi Salim0ff60a42005-11-22 14:47:37 -08001054void fib_add_ifaddr(struct in_ifaddr *ifa)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055{
1056 struct in_device *in_dev = ifa->ifa_dev;
1057 struct net_device *dev = in_dev->dev;
1058 struct in_ifaddr *prim = ifa;
Al Viroa144ea42006-09-28 18:00:55 -07001059 __be32 mask = ifa->ifa_mask;
1060 __be32 addr = ifa->ifa_local;
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001061 __be32 prefix = ifa->ifa_address & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001063 if (ifa->ifa_flags & IFA_F_SECONDARY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 prim = inet_ifa_byprefix(in_dev, prefix, mask);
Ian Morris51456b22015-04-03 09:17:26 +01001065 if (!prim) {
Joe Perches058bd4d2012-03-11 18:36:11 +00001066 pr_warn("%s: bug: prim == NULL\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 return;
1068 }
1069 }
1070
David Ahernaf4d7682018-05-27 08:09:57 -07001071 fib_magic(RTM_NEWROUTE, RTN_LOCAL, addr, 32, prim, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001073 if (!(dev->flags & IFF_UP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 return;
1075
1076 /* Add broadcast address, if it is explicitly assigned. */
Al Viroa144ea42006-09-28 18:00:55 -07001077 if (ifa->ifa_broadcast && ifa->ifa_broadcast != htonl(0xFFFFFFFF))
David Ahernaf4d7682018-05-27 08:09:57 -07001078 fib_magic(RTM_NEWROUTE, RTN_BROADCAST, ifa->ifa_broadcast, 32,
1079 prim, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001081 if (!ipv4_is_zeronet(prefix) && !(ifa->ifa_flags & IFA_F_SECONDARY) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 (prefix != addr || ifa->ifa_prefixlen < 32)) {
Paolo Abeni7b131182015-10-20 10:28:45 +02001083 if (!(ifa->ifa_flags & IFA_F_NOPREFIXROUTE))
1084 fib_magic(RTM_NEWROUTE,
1085 dev->flags & IFF_LOOPBACK ? RTN_LOCAL : RTN_UNICAST,
David Ahernaf4d7682018-05-27 08:09:57 -07001086 prefix, ifa->ifa_prefixlen, prim,
1087 ifa->ifa_rt_priority);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
1089 /* Add network specific broadcasts, when it takes a sense */
1090 if (ifa->ifa_prefixlen < 31) {
David Ahernaf4d7682018-05-27 08:09:57 -07001091 fib_magic(RTM_NEWROUTE, RTN_BROADCAST, prefix, 32,
1092 prim, 0);
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001093 fib_magic(RTM_NEWROUTE, RTN_BROADCAST, prefix | ~mask,
David Ahernaf4d7682018-05-27 08:09:57 -07001094 32, prim, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 }
1096 }
1097}
1098
David Ahernaf4d7682018-05-27 08:09:57 -07001099void fib_modify_prefix_metric(struct in_ifaddr *ifa, u32 new_metric)
1100{
1101 __be32 prefix = ifa->ifa_address & ifa->ifa_mask;
1102 struct in_device *in_dev = ifa->ifa_dev;
1103 struct net_device *dev = in_dev->dev;
1104
1105 if (!(dev->flags & IFF_UP) ||
1106 ifa->ifa_flags & (IFA_F_SECONDARY | IFA_F_NOPREFIXROUTE) ||
1107 ipv4_is_zeronet(prefix) ||
1108 prefix == ifa->ifa_local || ifa->ifa_prefixlen == 32)
1109 return;
1110
1111 /* add the new */
1112 fib_magic(RTM_NEWROUTE,
1113 dev->flags & IFF_LOOPBACK ? RTN_LOCAL : RTN_UNICAST,
1114 prefix, ifa->ifa_prefixlen, ifa, new_metric);
1115
1116 /* delete the old */
1117 fib_magic(RTM_DELROUTE,
1118 dev->flags & IFF_LOOPBACK ? RTN_LOCAL : RTN_UNICAST,
1119 prefix, ifa->ifa_prefixlen, ifa, ifa->ifa_rt_priority);
1120}
1121
Julian Anastasove6abbaa2011-03-19 12:13:49 +00001122/* Delete primary or secondary address.
1123 * Optionally, on secondary address promotion consider the addresses
1124 * from subnet iprim as deleted, even if they are in device list.
1125 * In this case the secondary ifa can be in device list.
1126 */
1127void fib_del_ifaddr(struct in_ifaddr *ifa, struct in_ifaddr *iprim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128{
1129 struct in_device *in_dev = ifa->ifa_dev;
1130 struct net_device *dev = in_dev->dev;
1131 struct in_ifaddr *ifa1;
Julian Anastasove6abbaa2011-03-19 12:13:49 +00001132 struct in_ifaddr *prim = ifa, *prim1 = NULL;
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001133 __be32 brd = ifa->ifa_address | ~ifa->ifa_mask;
1134 __be32 any = ifa->ifa_address & ifa->ifa_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135#define LOCAL_OK 1
1136#define BRD_OK 2
1137#define BRD0_OK 4
1138#define BRD1_OK 8
Eric Dumazet95c96172012-04-15 05:58:06 +00001139 unsigned int ok = 0;
Julian Anastasove6abbaa2011-03-19 12:13:49 +00001140 int subnet = 0; /* Primary network */
1141 int gone = 1; /* Address is missing */
1142 int same_prefsrc = 0; /* Another primary with same IP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Julian Anastasove6abbaa2011-03-19 12:13:49 +00001144 if (ifa->ifa_flags & IFA_F_SECONDARY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 prim = inet_ifa_byprefix(in_dev, any, ifa->ifa_mask);
Ian Morris51456b22015-04-03 09:17:26 +01001146 if (!prim) {
Paolo Abeni391a2032016-04-21 22:23:31 +02001147 /* if the device has been deleted, we don't perform
1148 * address promotion
1149 */
1150 if (!in_dev->dead)
1151 pr_warn("%s: bug: prim == NULL\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 return;
1153 }
Julian Anastasove6abbaa2011-03-19 12:13:49 +00001154 if (iprim && iprim != prim) {
Joe Perches058bd4d2012-03-11 18:36:11 +00001155 pr_warn("%s: bug: iprim != prim\n", __func__);
Julian Anastasove6abbaa2011-03-19 12:13:49 +00001156 return;
1157 }
1158 } else if (!ipv4_is_zeronet(any) &&
1159 (any != ifa->ifa_local || ifa->ifa_prefixlen < 32)) {
Paolo Abeni7b131182015-10-20 10:28:45 +02001160 if (!(ifa->ifa_flags & IFA_F_NOPREFIXROUTE))
1161 fib_magic(RTM_DELROUTE,
1162 dev->flags & IFF_LOOPBACK ? RTN_LOCAL : RTN_UNICAST,
David Ahernaf4d7682018-05-27 08:09:57 -07001163 any, ifa->ifa_prefixlen, prim, 0);
Julian Anastasove6abbaa2011-03-19 12:13:49 +00001164 subnet = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 }
1166
David S. Millerfbd40ea2016-03-13 23:28:00 -04001167 if (in_dev->dead)
1168 goto no_promotions;
1169
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 /* Deletion is more complicated than add.
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001171 * We should take care of not to delete too much :-)
1172 *
1173 * Scan address list to be sure that addresses are really gone.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 */
1175
1176 for (ifa1 = in_dev->ifa_list; ifa1; ifa1 = ifa1->ifa_next) {
Julian Anastasove6abbaa2011-03-19 12:13:49 +00001177 if (ifa1 == ifa) {
1178 /* promotion, keep the IP */
1179 gone = 0;
1180 continue;
1181 }
1182 /* Ignore IFAs from our subnet */
1183 if (iprim && ifa1->ifa_mask == iprim->ifa_mask &&
1184 inet_ifa_match(ifa1->ifa_address, iprim))
1185 continue;
1186
1187 /* Ignore ifa1 if it uses different primary IP (prefsrc) */
1188 if (ifa1->ifa_flags & IFA_F_SECONDARY) {
1189 /* Another address from our subnet? */
1190 if (ifa1->ifa_mask == prim->ifa_mask &&
1191 inet_ifa_match(ifa1->ifa_address, prim))
1192 prim1 = prim;
1193 else {
1194 /* We reached the secondaries, so
1195 * same_prefsrc should be determined.
1196 */
1197 if (!same_prefsrc)
1198 continue;
1199 /* Search new prim1 if ifa1 is not
1200 * using the current prim1
1201 */
1202 if (!prim1 ||
1203 ifa1->ifa_mask != prim1->ifa_mask ||
1204 !inet_ifa_match(ifa1->ifa_address, prim1))
1205 prim1 = inet_ifa_byprefix(in_dev,
1206 ifa1->ifa_address,
1207 ifa1->ifa_mask);
1208 if (!prim1)
1209 continue;
1210 if (prim1->ifa_local != prim->ifa_local)
1211 continue;
1212 }
1213 } else {
1214 if (prim->ifa_local != ifa1->ifa_local)
1215 continue;
1216 prim1 = ifa1;
1217 if (prim != prim1)
1218 same_prefsrc = 1;
1219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 if (ifa->ifa_local == ifa1->ifa_local)
1221 ok |= LOCAL_OK;
1222 if (ifa->ifa_broadcast == ifa1->ifa_broadcast)
1223 ok |= BRD_OK;
1224 if (brd == ifa1->ifa_broadcast)
1225 ok |= BRD1_OK;
1226 if (any == ifa1->ifa_broadcast)
1227 ok |= BRD0_OK;
Julian Anastasove6abbaa2011-03-19 12:13:49 +00001228 /* primary has network specific broadcasts */
1229 if (prim1 == ifa1 && ifa1->ifa_prefixlen < 31) {
1230 __be32 brd1 = ifa1->ifa_address | ~ifa1->ifa_mask;
1231 __be32 any1 = ifa1->ifa_address & ifa1->ifa_mask;
1232
1233 if (!ipv4_is_zeronet(any1)) {
1234 if (ifa->ifa_broadcast == brd1 ||
1235 ifa->ifa_broadcast == any1)
1236 ok |= BRD_OK;
1237 if (brd == brd1 || brd == any1)
1238 ok |= BRD1_OK;
1239 if (any == brd1 || any == any1)
1240 ok |= BRD0_OK;
1241 }
1242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 }
1244
David S. Millerfbd40ea2016-03-13 23:28:00 -04001245no_promotions:
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001246 if (!(ok & BRD_OK))
David Ahernaf4d7682018-05-27 08:09:57 -07001247 fib_magic(RTM_DELROUTE, RTN_BROADCAST, ifa->ifa_broadcast, 32,
1248 prim, 0);
Julian Anastasove6abbaa2011-03-19 12:13:49 +00001249 if (subnet && ifa->ifa_prefixlen < 31) {
1250 if (!(ok & BRD1_OK))
David Ahernaf4d7682018-05-27 08:09:57 -07001251 fib_magic(RTM_DELROUTE, RTN_BROADCAST, brd, 32,
1252 prim, 0);
Julian Anastasove6abbaa2011-03-19 12:13:49 +00001253 if (!(ok & BRD0_OK))
David Ahernaf4d7682018-05-27 08:09:57 -07001254 fib_magic(RTM_DELROUTE, RTN_BROADCAST, any, 32,
1255 prim, 0);
Julian Anastasove6abbaa2011-03-19 12:13:49 +00001256 }
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001257 if (!(ok & LOCAL_OK)) {
David Ahern30bbaa12015-08-13 14:59:05 -06001258 unsigned int addr_type;
1259
David Ahernaf4d7682018-05-27 08:09:57 -07001260 fib_magic(RTM_DELROUTE, RTN_LOCAL, ifa->ifa_local, 32, prim, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
1262 /* Check, that this local address finally disappeared. */
David Ahern30bbaa12015-08-13 14:59:05 -06001263 addr_type = inet_addr_type_dev_table(dev_net(dev), dev,
1264 ifa->ifa_local);
1265 if (gone && addr_type != RTN_LOCAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 /* And the last, but not the least thing.
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001267 * We must flush stray FIB entries.
1268 *
1269 * First of all, we scan fib_info list searching
1270 * for stray nexthop entries, then ignite fib_flush.
1271 */
Mark Tomlinson5a56a0b2016-09-05 10:20:20 +12001272 if (fib_sync_down_addr(dev, ifa->ifa_local))
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001273 fib_flush(dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 }
1275 }
1276#undef LOCAL_OK
1277#undef BRD_OK
1278#undef BRD0_OK
1279#undef BRD1_OK
1280}
1281
Alexander Duyck345e9b52014-12-31 10:56:24 -08001282static void nl_fib_lookup(struct net *net, struct fib_result_nl *frn)
Robert Olsson246955f2005-06-20 13:36:39 -07001283{
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001284
Robert Olsson246955f2005-06-20 13:36:39 -07001285 struct fib_result res;
David S. Miller9ade2282011-03-12 02:02:42 -05001286 struct flowi4 fl4 = {
1287 .flowi4_mark = frn->fl_mark,
1288 .daddr = frn->fl_addr,
1289 .flowi4_tos = frn->fl_tos,
1290 .flowi4_scope = frn->fl_scope,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001291 };
Alexander Duyck345e9b52014-12-31 10:56:24 -08001292 struct fib_table *tb;
1293
1294 rcu_read_lock();
1295
1296 tb = fib_get_table(net, frn->tb_id_in);
Alexey Kuznetsov1194ed02007-04-25 13:07:28 -07001297
1298 frn->err = -ENOENT;
Robert Olsson246955f2005-06-20 13:36:39 -07001299 if (tb) {
1300 local_bh_disable();
1301
1302 frn->tb_id = tb->tb_id;
David S. Miller9ade2282011-03-12 02:02:42 -05001303 frn->err = fib_table_lookup(tb, &fl4, &res, FIB_LOOKUP_NOREF);
Robert Olsson246955f2005-06-20 13:36:39 -07001304
1305 if (!frn->err) {
1306 frn->prefixlen = res.prefixlen;
1307 frn->nh_sel = res.nh_sel;
1308 frn->type = res.type;
1309 frn->scope = res.scope;
1310 }
1311 local_bh_enable();
1312 }
Alexander Duyck345e9b52014-12-31 10:56:24 -08001313
1314 rcu_read_unlock();
Robert Olsson246955f2005-06-20 13:36:39 -07001315}
1316
David S. Miller28f7b0362007-10-10 21:32:39 -07001317static void nl_fib_input(struct sk_buff *skb)
Robert Olsson246955f2005-06-20 13:36:39 -07001318{
Denis V. Lunev6bd48fc2008-01-10 03:28:55 -08001319 struct net *net;
Robert Olsson246955f2005-06-20 13:36:39 -07001320 struct fib_result_nl *frn;
David S. Miller28f7b0362007-10-10 21:32:39 -07001321 struct nlmsghdr *nlh;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001322 u32 portid;
Alexey Kuznetsov1194ed02007-04-25 13:07:28 -07001323
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001324 net = sock_net(skb->sk);
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -07001325 nlh = nlmsg_hdr(skb);
Eric Dumazetc64c0b32017-03-21 19:22:28 -07001326 if (skb->len < nlmsg_total_size(sizeof(*frn)) ||
1327 skb->len < nlh->nlmsg_len ||
Hong zhi guo573ce262013-03-27 06:47:04 +00001328 nlmsg_len(nlh) < sizeof(*frn))
Thomas Grafea865752005-12-01 14:30:00 -08001329 return;
Denis V. Lunevd883a032007-12-21 02:01:53 -08001330
Pablo Neira3a365152013-06-28 03:04:23 +02001331 skb = netlink_skb_clone(skb, GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +01001332 if (!skb)
Denis V. Lunevd883a032007-12-21 02:01:53 -08001333 return;
1334 nlh = nlmsg_hdr(skb);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001335
Hong zhi guo573ce262013-03-27 06:47:04 +00001336 frn = (struct fib_result_nl *) nlmsg_data(nlh);
Alexander Duyck345e9b52014-12-31 10:56:24 -08001337 nl_fib_lookup(net, frn);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001338
Rami Rosen28a28282013-01-11 09:59:20 +00001339 portid = NETLINK_CB(skb).portid; /* netlink portid */
Eric W. Biederman15e47302012-09-07 20:12:54 +00001340 NETLINK_CB(skb).portid = 0; /* from kernel */
Patrick McHardyac6d4392005-08-14 19:29:52 -07001341 NETLINK_CB(skb).dst_group = 0; /* unicast */
Eric W. Biederman15e47302012-09-07 20:12:54 +00001342 netlink_unicast(net->ipv4.fibnl, skb, portid, MSG_DONTWAIT);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001343}
Robert Olsson246955f2005-06-20 13:36:39 -07001344
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001345static int __net_init nl_fib_lookup_init(struct net *net)
Robert Olsson246955f2005-06-20 13:36:39 -07001346{
Denis V. Lunev6bd48fc2008-01-10 03:28:55 -08001347 struct sock *sk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00001348 struct netlink_kernel_cfg cfg = {
1349 .input = nl_fib_input,
1350 };
1351
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00001352 sk = netlink_kernel_create(net, NETLINK_FIB_LOOKUP, &cfg);
Ian Morris51456b22015-04-03 09:17:26 +01001353 if (!sk)
Denis V. Lunev7b1a74fd2008-01-10 03:22:17 -08001354 return -EAFNOSUPPORT;
Denis V. Lunev6bd48fc2008-01-10 03:28:55 -08001355 net->ipv4.fibnl = sk;
Denis V. Lunev7b1a74fd2008-01-10 03:22:17 -08001356 return 0;
1357}
1358
1359static void nl_fib_lookup_exit(struct net *net)
1360{
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08001361 netlink_kernel_release(net->ipv4.fibnl);
Denis V. Lunev775516b2008-01-18 23:55:19 -08001362 net->ipv4.fibnl = NULL;
Robert Olsson246955f2005-06-20 13:36:39 -07001363}
1364
Julian Anastasov4f823de2015-10-30 10:23:33 +02001365static void fib_disable_ip(struct net_device *dev, unsigned long event,
1366 bool force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367{
Julian Anastasov4f823de2015-10-30 10:23:33 +02001368 if (fib_sync_down_dev(dev, event, force))
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001369 fib_flush(dev_net(dev));
Gao Feng06b4fc52017-04-26 19:04:04 +08001370 else
1371 rt_cache_flush(dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 arp_ifdown(dev);
1373}
1374
1375static int fib_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
1376{
Jianjun Kong6ed2533e2008-11-03 00:25:16 -08001377 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
Denis V. Lunev76e6ebf2008-07-05 19:00:44 -07001378 struct net_device *dev = ifa->ifa_dev->dev;
David S. Miller436c3b62011-03-24 17:42:21 -07001379 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
1381 switch (event) {
1382 case NETDEV_UP:
1383 fib_add_ifaddr(ifa);
1384#ifdef CONFIG_IP_ROUTE_MULTIPATH
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001385 fib_sync_up(dev, RTNH_F_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386#endif
David S. Miller436c3b62011-03-24 17:42:21 -07001387 atomic_inc(&net->ipv4.dev_addr_genid);
Nicolas Dichtel4ccfe6d2012-09-07 00:45:29 +00001388 rt_cache_flush(dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 break;
1390 case NETDEV_DOWN:
Julian Anastasove6abbaa2011-03-19 12:13:49 +00001391 fib_del_ifaddr(ifa, NULL);
David S. Miller436c3b62011-03-24 17:42:21 -07001392 atomic_inc(&net->ipv4.dev_addr_genid);
Ian Morris51456b22015-04-03 09:17:26 +01001393 if (!ifa->ifa_dev->ifa_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 /* Last address was deleted from this interface.
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001395 * Disable IP.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 */
Julian Anastasov4f823de2015-10-30 10:23:33 +02001397 fib_disable_ip(dev, event, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 } else {
Nicolas Dichtel4ccfe6d2012-09-07 00:45:29 +00001399 rt_cache_flush(dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 }
1401 break;
1402 }
1403 return NOTIFY_DONE;
1404}
1405
1406static int fib_netdev_event(struct notifier_block *this, unsigned long event, void *ptr)
1407{
Jiri Pirko351638e2013-05-28 01:30:21 +00001408 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02001409 struct netdev_notifier_changeupper_info *upper_info = ptr;
1410 struct netdev_notifier_info_ext *info_ext = ptr;
Eric Dumazet0115e8e2012-08-22 17:19:46 +00001411 struct in_device *in_dev;
David S. Miller436c3b62011-03-24 17:42:21 -07001412 struct net *net = dev_net(dev);
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001413 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
1415 if (event == NETDEV_UNREGISTER) {
Julian Anastasov4f823de2015-10-30 10:23:33 +02001416 fib_disable_ip(dev, event, true);
David S. Millercaacf052012-07-31 15:06:50 -07001417 rt_flush_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 return NOTIFY_DONE;
1419 }
1420
Eric Dumazet0115e8e2012-08-22 17:19:46 +00001421 in_dev = __in_dev_get_rtnl(dev);
Oliver Hartkoppa0065f22014-01-23 10:19:34 +01001422 if (!in_dev)
1423 return NOTIFY_DONE;
Eric Dumazet0115e8e2012-08-22 17:19:46 +00001424
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 switch (event) {
1426 case NETDEV_UP:
1427 for_ifa(in_dev) {
1428 fib_add_ifaddr(ifa);
1429 } endfor_ifa(in_dev);
1430#ifdef CONFIG_IP_ROUTE_MULTIPATH
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001431 fib_sync_up(dev, RTNH_F_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432#endif
David S. Miller436c3b62011-03-24 17:42:21 -07001433 atomic_inc(&net->ipv4.dev_addr_genid);
Nicolas Dichtel4ccfe6d2012-09-07 00:45:29 +00001434 rt_cache_flush(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 break;
1436 case NETDEV_DOWN:
Julian Anastasov4f823de2015-10-30 10:23:33 +02001437 fib_disable_ip(dev, event, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 case NETDEV_CHANGE:
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001440 flags = dev_get_flags(dev);
1441 if (flags & (IFF_RUNNING | IFF_LOWER_UP))
1442 fib_sync_up(dev, RTNH_F_LINKDOWN);
1443 else
Julian Anastasov4f823de2015-10-30 10:23:33 +02001444 fib_sync_down_dev(dev, event, false);
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02001445 rt_cache_flush(net);
1446 break;
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001447 case NETDEV_CHANGEMTU:
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02001448 fib_sync_mtu(dev, info_ext->ext.mtu);
Nicolas Dichtel4ccfe6d2012-09-07 00:45:29 +00001449 rt_cache_flush(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 break;
David Ahern7f49e7a2015-12-10 10:25:24 -08001451 case NETDEV_CHANGEUPPER:
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02001452 upper_info = ptr;
David Ahern7f49e7a2015-12-10 10:25:24 -08001453 /* flush all routes if dev is linked to or unlinked from
1454 * an L3 master device (e.g., VRF)
1455 */
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02001456 if (upper_info->upper_dev &&
1457 netif_is_l3_master(upper_info->upper_dev))
David Ahern7f49e7a2015-12-10 10:25:24 -08001458 fib_disable_ip(dev, NETDEV_DOWN, true);
1459 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 }
1461 return NOTIFY_DONE;
1462}
1463
1464static struct notifier_block fib_inetaddr_notifier = {
Jianjun Kong6ed2533e2008-11-03 00:25:16 -08001465 .notifier_call = fib_inetaddr_event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466};
1467
1468static struct notifier_block fib_netdev_notifier = {
Jianjun Kong6ed2533e2008-11-03 00:25:16 -08001469 .notifier_call = fib_netdev_event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470};
1471
Denis V. Lunev7b1a74fd2008-01-10 03:22:17 -08001472static int __net_init ip_fib_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473{
Denis V. Lunevdce5cbe2008-01-31 18:44:53 -08001474 int err;
Eric Dumazet10da66f2010-10-13 08:22:03 +00001475 size_t size = sizeof(struct hlist_head) * FIB_TABLE_HASHSZ;
Patrick McHardy1af5a8c2006-08-10 23:10:46 -07001476
Ido Schimmel04b1d4e2017-08-03 13:28:11 +02001477 err = fib4_notifier_init(net);
1478 if (err)
1479 return err;
Ido Schimmelcacaad12016-12-03 16:45:06 +01001480
Eric Dumazet10da66f2010-10-13 08:22:03 +00001481 /* Avoid false sharing : Use at least a full cache line */
1482 size = max_t(size_t, size, L1_CACHE_BYTES);
1483
1484 net->ipv4.fib_table_hash = kzalloc(size, GFP_KERNEL);
Ido Schimmel04b1d4e2017-08-03 13:28:11 +02001485 if (!net->ipv4.fib_table_hash) {
1486 err = -ENOMEM;
1487 goto err_table_hash_alloc;
1488 }
Denis V. Luneve4aef8a2008-01-10 03:28:24 -08001489
Denis V. Lunevdce5cbe2008-01-31 18:44:53 -08001490 err = fib4_rules_init(net);
1491 if (err < 0)
Ido Schimmel04b1d4e2017-08-03 13:28:11 +02001492 goto err_rules_init;
Denis V. Lunevdce5cbe2008-01-31 18:44:53 -08001493 return 0;
1494
Ido Schimmel04b1d4e2017-08-03 13:28:11 +02001495err_rules_init:
Denis V. Lunevdce5cbe2008-01-31 18:44:53 -08001496 kfree(net->ipv4.fib_table_hash);
Ido Schimmel04b1d4e2017-08-03 13:28:11 +02001497err_table_hash_alloc:
1498 fib4_notifier_exit(net);
Denis V. Lunevdce5cbe2008-01-31 18:44:53 -08001499 return err;
Denis V. Lunev7b1a74fd2008-01-10 03:22:17 -08001500}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001502static void ip_fib_net_exit(struct net *net)
Denis V. Lunev7b1a74fd2008-01-10 03:22:17 -08001503{
Ido Schimmelb4681c22017-12-20 19:34:19 +02001504 int i;
Thomas Graf63f34442007-03-22 11:55:17 -07001505
Sabrina Dubroca6dede752015-03-10 21:03:54 +01001506 rtnl_lock();
Alexander Duyck6e47d6c2015-03-27 14:14:22 -07001507#ifdef CONFIG_IP_MULTIPLE_TABLES
Alexander Duyck6e47d6c2015-03-27 14:14:22 -07001508 RCU_INIT_POINTER(net->ipv4.fib_main, NULL);
1509 RCU_INIT_POINTER(net->ipv4.fib_default, NULL);
1510#endif
Ido Schimmelb4681c22017-12-20 19:34:19 +02001511 /* Destroy the tables in reverse order to guarantee that the
1512 * local table, ID 255, is destroyed before the main table, ID
1513 * 254. This is necessary as the local table may contain
1514 * references to data contained in the main table.
1515 */
1516 for (i = FIB_TABLE_HASHSZ - 1; i >= 0; i--) {
Alexander Duycka7e53532015-03-04 15:02:44 -08001517 struct hlist_head *head = &net->ipv4.fib_table_hash[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001518 struct hlist_node *tmp;
Alexander Duycka7e53532015-03-04 15:02:44 -08001519 struct fib_table *tb;
Denis V. Lunev7b1a74fd2008-01-10 03:22:17 -08001520
Alexander Duycka7e53532015-03-04 15:02:44 -08001521 hlist_for_each_entry_safe(tb, tmp, head, tb_hlist) {
Alexander Duycka7e53532015-03-04 15:02:44 -08001522 hlist_del(&tb->tb_hlist);
Ido Schimmelf97f4dd2019-01-09 09:57:39 +00001523 fib_table_flush(net, tb, true);
Pavel Emelyanov4aa2c462010-10-28 02:00:43 +00001524 fib_free_table(tb);
Denis V. Lunev7b1a74fd2008-01-10 03:22:17 -08001525 }
1526 }
Alexander Duyckad88d052015-03-27 14:14:16 -07001527
1528#ifdef CONFIG_IP_MULTIPLE_TABLES
1529 fib4_rules_exit(net);
1530#endif
Eric Dumazete2666f82011-03-30 16:57:46 -07001531 rtnl_unlock();
Denis V. Luneve4aef8a2008-01-10 03:28:24 -08001532 kfree(net->ipv4.fib_table_hash);
Ido Schimmel04b1d4e2017-08-03 13:28:11 +02001533 fib4_notifier_exit(net);
Denis V. Lunev7b1a74fd2008-01-10 03:22:17 -08001534}
1535
1536static int __net_init fib_net_init(struct net *net)
1537{
1538 int error;
1539
David S. Millerf4530fa2012-07-05 22:13:13 -07001540#ifdef CONFIG_IP_ROUTE_CLASSID
1541 net->ipv4.fib_num_tclassid_users = 0;
1542#endif
Denis V. Lunev7b1a74fd2008-01-10 03:22:17 -08001543 error = ip_fib_net_init(net);
1544 if (error < 0)
1545 goto out;
1546 error = nl_fib_lookup_init(net);
1547 if (error < 0)
1548 goto out_nlfl;
1549 error = fib_proc_init(net);
1550 if (error < 0)
1551 goto out_proc;
1552out:
1553 return error;
1554
1555out_proc:
1556 nl_fib_lookup_exit(net);
1557out_nlfl:
1558 ip_fib_net_exit(net);
1559 goto out;
1560}
1561
1562static void __net_exit fib_net_exit(struct net *net)
1563{
1564 fib_proc_exit(net);
1565 nl_fib_lookup_exit(net);
1566 ip_fib_net_exit(net);
1567}
1568
1569static struct pernet_operations fib_net_ops = {
1570 .init = fib_net_init,
1571 .exit = fib_net_exit,
1572};
1573
1574void __init ip_fib_init(void)
1575{
Mahesh Bandewar8799a222017-07-19 15:41:33 -07001576 fib_trie_init();
Denis V. Lunev7b1a74fd2008-01-10 03:22:17 -08001577
1578 register_pernet_subsys(&fib_net_ops);
Mahesh Bandewar8799a222017-07-19 15:41:33 -07001579
Denis V. Lunev7b1a74fd2008-01-10 03:22:17 -08001580 register_netdevice_notifier(&fib_netdev_notifier);
1581 register_inetaddr_notifier(&fib_inetaddr_notifier);
Stephen Hemminger7f9b8052008-01-14 23:14:20 -08001582
Florian Westphalb97bac62017-08-09 20:41:48 +02001583 rtnl_register(PF_INET, RTM_NEWROUTE, inet_rtm_newroute, NULL, 0);
1584 rtnl_register(PF_INET, RTM_DELROUTE, inet_rtm_delroute, NULL, 0);
1585 rtnl_register(PF_INET, RTM_GETROUTE, NULL, inet_dump_fib, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586}