blob: 86a23e4a6a50ffce36e5ac1849361f4dd91e6eac [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>
David Ahern5481d732019-06-03 20:19:49 -070042#include <net/nexthop.h>
Thomas Graf63f34442007-03-22 11:55:17 -070043#include <net/rtnetlink.h>
Michael Smith990078a2011-04-07 04:51:51 +000044#include <net/xfrm.h>
David Ahern385add92015-09-29 20:07:13 -070045#include <net/l3mdev.h>
David Ahern9ed59592017-01-17 14:57:36 -080046#include <net/lwtunnel.h>
David Ahernf6d3c192015-08-28 08:42:09 -070047#include <trace/events/fib.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#ifndef CONFIG_IP_MULTIPLE_TABLES
50
Denis V. Lunev7b1a74fd2008-01-10 03:22:17 -080051static int __net_init fib4_rules_init(struct net *net)
Pavel Emelyanovc3e9a352007-11-06 23:34:04 -080052{
Denis V. Lunev93456b62008-01-10 03:23:38 -080053 struct fib_table *local_table, *main_table;
54
Alexander Duyck0ddcf432015-03-06 13:47:00 -080055 main_table = fib_trie_table(RT_TABLE_MAIN, NULL);
Ian Morris51456b22015-04-03 09:17:26 +010056 if (!main_table)
Alexander Duyck61f0d862015-03-11 14:02:16 -070057 return -ENOMEM;
Denis V. Lunevdbb50162008-01-10 03:21:49 -080058
Alexander Duyck0ddcf432015-03-06 13:47:00 -080059 local_table = fib_trie_table(RT_TABLE_LOCAL, main_table);
Ian Morris51456b22015-04-03 09:17:26 +010060 if (!local_table)
Alexander Duyck61f0d862015-03-11 14:02:16 -070061 goto fail;
Alexander Duyck0ddcf432015-03-06 13:47:00 -080062
Denis V. Lunev93456b62008-01-10 03:23:38 -080063 hlist_add_head_rcu(&local_table->tb_hlist,
Denis V. Luneve4aef8a2008-01-10 03:28:24 -080064 &net->ipv4.fib_table_hash[TABLE_LOCAL_INDEX]);
Denis V. Lunev93456b62008-01-10 03:23:38 -080065 hlist_add_head_rcu(&main_table->tb_hlist,
Denis V. Luneve4aef8a2008-01-10 03:28:24 -080066 &net->ipv4.fib_table_hash[TABLE_MAIN_INDEX]);
Denis V. Lunevdbb50162008-01-10 03:21:49 -080067 return 0;
68
69fail:
Alexander Duyck61f0d862015-03-11 14:02:16 -070070 fib_free_table(main_table);
Denis V. Lunevdbb50162008-01-10 03:21:49 -080071 return -ENOMEM;
Pavel Emelyanovc3e9a352007-11-06 23:34:04 -080072}
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#else
74
Denis V. Lunev8ad49422008-01-10 03:24:11 -080075struct fib_table *fib_new_table(struct net *net, u32 id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
Alexander Duyck0ddcf432015-03-06 13:47:00 -080077 struct fib_table *tb, *alias = NULL;
Patrick McHardy1af5a8c2006-08-10 23:10:46 -070078 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Patrick McHardy1af5a8c2006-08-10 23:10:46 -070080 if (id == 0)
81 id = RT_TABLE_MAIN;
Denis V. Lunev8ad49422008-01-10 03:24:11 -080082 tb = fib_get_table(net, id);
Patrick McHardy1af5a8c2006-08-10 23:10:46 -070083 if (tb)
84 return tb;
Stephen Hemminger7f9b8052008-01-14 23:14:20 -080085
Alexander Duyck5350d542017-01-02 13:32:54 -080086 if (id == RT_TABLE_LOCAL && !net->ipv4.fib_has_custom_rules)
Alexander Duyck0ddcf432015-03-06 13:47:00 -080087 alias = fib_new_table(net, RT_TABLE_MAIN);
88
89 tb = fib_trie_table(id, alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 if (!tb)
91 return NULL;
David S. Millerf4530fa2012-07-05 22:13:13 -070092
93 switch (id) {
David S. Millerf4530fa2012-07-05 22:13:13 -070094 case RT_TABLE_MAIN:
Alexander Duycka7e53532015-03-04 15:02:44 -080095 rcu_assign_pointer(net->ipv4.fib_main, tb);
David S. Millerf4530fa2012-07-05 22:13:13 -070096 break;
David S. Millerf4530fa2012-07-05 22:13:13 -070097 case RT_TABLE_DEFAULT:
Alexander Duycka7e53532015-03-04 15:02:44 -080098 rcu_assign_pointer(net->ipv4.fib_default, tb);
David S. Millerf4530fa2012-07-05 22:13:13 -070099 break;
David S. Millerf4530fa2012-07-05 22:13:13 -0700100 default:
101 break;
102 }
103
Patrick McHardy1af5a8c2006-08-10 23:10:46 -0700104 h = id & (FIB_TABLE_HASHSZ - 1);
Denis V. Luneve4aef8a2008-01-10 03:28:24 -0800105 hlist_add_head_rcu(&tb->tb_hlist, &net->ipv4.fib_table_hash[h]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 return tb;
107}
David Ahernb3b46632016-05-04 21:46:12 -0700108EXPORT_SYMBOL_GPL(fib_new_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Alexander Duyck345e9b52014-12-31 10:56:24 -0800110/* caller must hold either rtnl or rcu read lock */
Denis V. Lunev8ad49422008-01-10 03:24:11 -0800111struct fib_table *fib_get_table(struct net *net, u32 id)
Patrick McHardy1af5a8c2006-08-10 23:10:46 -0700112{
113 struct fib_table *tb;
Denis V. Luneve4aef8a2008-01-10 03:28:24 -0800114 struct hlist_head *head;
Patrick McHardy1af5a8c2006-08-10 23:10:46 -0700115 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Patrick McHardy1af5a8c2006-08-10 23:10:46 -0700117 if (id == 0)
118 id = RT_TABLE_MAIN;
119 h = id & (FIB_TABLE_HASHSZ - 1);
Denis V. Luneve4aef8a2008-01-10 03:28:24 -0800120
Denis V. Luneve4aef8a2008-01-10 03:28:24 -0800121 head = &net->ipv4.fib_table_hash[h];
Joel Fernandes (Google)7fd69b02019-07-16 18:12:24 -0400122 hlist_for_each_entry_rcu(tb, head, tb_hlist,
123 lockdep_rtnl_is_held()) {
Alexander Duyck345e9b52014-12-31 10:56:24 -0800124 if (tb->tb_id == id)
Patrick McHardy1af5a8c2006-08-10 23:10:46 -0700125 return tb;
Patrick McHardy1af5a8c2006-08-10 23:10:46 -0700126 }
Patrick McHardy1af5a8c2006-08-10 23:10:46 -0700127 return NULL;
128}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129#endif /* CONFIG_IP_MULTIPLE_TABLES */
130
Alexander Duyck0ddcf432015-03-06 13:47:00 -0800131static void fib_replace_table(struct net *net, struct fib_table *old,
132 struct fib_table *new)
133{
134#ifdef CONFIG_IP_MULTIPLE_TABLES
135 switch (new->tb_id) {
Alexander Duyck0ddcf432015-03-06 13:47:00 -0800136 case RT_TABLE_MAIN:
137 rcu_assign_pointer(net->ipv4.fib_main, new);
138 break;
139 case RT_TABLE_DEFAULT:
140 rcu_assign_pointer(net->ipv4.fib_default, new);
141 break;
142 default:
143 break;
144 }
145
146#endif
147 /* replace the old table in the hlist */
148 hlist_replace_rcu(&old->tb_hlist, &new->tb_hlist);
149}
150
151int fib_unmerge(struct net *net)
152{
Alexander Duyck3b709332016-11-15 05:46:06 -0500153 struct fib_table *old, *new, *main_table;
Alexander Duyck0ddcf432015-03-06 13:47:00 -0800154
Alexander Duyck3c9e9f72015-03-12 14:46:23 -0700155 /* attempt to fetch local table if it has been allocated */
Alexander Duyck0ddcf432015-03-06 13:47:00 -0800156 old = fib_get_table(net, RT_TABLE_LOCAL);
Alexander Duyck3c9e9f72015-03-12 14:46:23 -0700157 if (!old)
158 return 0;
Alexander Duyck0ddcf432015-03-06 13:47:00 -0800159
Alexander Duyck3c9e9f72015-03-12 14:46:23 -0700160 new = fib_trie_unmerge(old);
Alexander Duyck0ddcf432015-03-06 13:47:00 -0800161 if (!new)
162 return -ENOMEM;
163
Alexander Duyck3b709332016-11-15 05:46:06 -0500164 /* table is already unmerged */
165 if (new == old)
166 return 0;
167
Alexander Duyck0ddcf432015-03-06 13:47:00 -0800168 /* replace merged table with clean table */
Alexander Duyck3b709332016-11-15 05:46:06 -0500169 fib_replace_table(net, old, new);
170 fib_free_table(old);
171
172 /* attempt to fetch main table if it has been allocated */
173 main_table = fib_get_table(net, RT_TABLE_MAIN);
174 if (!main_table)
175 return 0;
176
177 /* flush local entries from main table */
178 fib_table_flush_external(main_table);
Alexander Duyck0ddcf432015-03-06 13:47:00 -0800179
180 return 0;
181}
182
David Ahern9bd83662019-05-22 12:04:44 -0700183void fib_flush(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
185 int flushed = 0;
Patrick McHardy1af5a8c2006-08-10 23:10:46 -0700186 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Patrick McHardy1af5a8c2006-08-10 23:10:46 -0700188 for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
Alexander Duycka7e53532015-03-04 15:02:44 -0800189 struct hlist_head *head = &net->ipv4.fib_table_hash[h];
190 struct hlist_node *tmp;
191 struct fib_table *tb;
192
193 hlist_for_each_entry_safe(tb, tmp, head, tb_hlist)
Ido Schimmelf97f4dd2019-01-09 09:57:39 +0000194 flushed += fib_table_flush(net, tb, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 if (flushed)
Nicolas Dichtel4ccfe6d2012-09-07 00:45:29 +0000198 rt_cache_flush(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
200
Laszlo Attila Toth055381162007-12-04 23:28:46 -0800201/*
202 * Find address type as if only "dev" was present in the system. If
203 * on_dev is NULL then all interfaces are taken into consideration.
204 */
Eric Dumazet95c96172012-04-15 05:58:06 +0000205static inline unsigned int __inet_dev_addr_type(struct net *net,
206 const struct net_device *dev,
David Ahern9b8ff512015-09-01 14:26:35 -0600207 __be32 addr, u32 tb_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
David S. Miller9ade2282011-03-12 02:02:42 -0500209 struct flowi4 fl4 = { .daddr = addr };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 struct fib_result res;
Eric Dumazet95c96172012-04-15 05:58:06 +0000211 unsigned int ret = RTN_BROADCAST;
David Ahern15be405e2015-08-13 14:59:04 -0600212 struct fib_table *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Jan Engelhardt1e637c72008-01-21 03:18:08 -0800214 if (ipv4_is_zeronet(addr) || ipv4_is_lbcast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 return RTN_BROADCAST;
Joe Perchesf97c1e02007-12-16 13:45:43 -0800216 if (ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 return RTN_MULTICAST;
218
Alexander Duyck345e9b52014-12-31 10:56:24 -0800219 rcu_read_lock();
220
David Ahern15be405e2015-08-13 14:59:04 -0600221 table = fib_get_table(net, tb_id);
222 if (table) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 ret = RTN_UNICAST;
David Ahern15be405e2015-08-13 14:59:04 -0600224 if (!fib_table_lookup(table, &fl4, &res, FIB_LOOKUP_NOREF)) {
David Aherndcb1ecb2019-06-03 20:19:50 -0700225 struct fib_nh_common *nhc = fib_info_nhc(res.fi, 0);
David Ahern5481d732019-06-03 20:19:49 -0700226
David Aherndcb1ecb2019-06-03 20:19:50 -0700227 if (!dev || dev == nhc->nhc_dev)
Laszlo Attila Toth055381162007-12-04 23:28:46 -0800228 ret = res.type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 }
230 }
Alexander Duyck345e9b52014-12-31 10:56:24 -0800231
232 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 return ret;
234}
235
David Ahern9b8ff512015-09-01 14:26:35 -0600236unsigned int inet_addr_type_table(struct net *net, __be32 addr, u32 tb_id)
David Ahern15be405e2015-08-13 14:59:04 -0600237{
238 return __inet_dev_addr_type(net, NULL, addr, tb_id);
239}
240EXPORT_SYMBOL(inet_addr_type_table);
241
Eric W. Biederman6b175b22008-01-10 03:25:28 -0800242unsigned int inet_addr_type(struct net *net, __be32 addr)
Laszlo Attila Toth055381162007-12-04 23:28:46 -0800243{
David Ahern15be405e2015-08-13 14:59:04 -0600244 return __inet_dev_addr_type(net, NULL, addr, RT_TABLE_LOCAL);
Laszlo Attila Toth055381162007-12-04 23:28:46 -0800245}
Eric Dumazet4bc2f182010-07-09 21:22:10 +0000246EXPORT_SYMBOL(inet_addr_type);
Laszlo Attila Toth055381162007-12-04 23:28:46 -0800247
Eric W. Biederman6b175b22008-01-10 03:25:28 -0800248unsigned int inet_dev_addr_type(struct net *net, const struct net_device *dev,
249 __be32 addr)
Laszlo Attila Toth055381162007-12-04 23:28:46 -0800250{
David Ahern3236b002015-09-29 20:07:14 -0700251 u32 rt_table = l3mdev_fib_table(dev) ? : RT_TABLE_LOCAL;
David Ahern15be405e2015-08-13 14:59:04 -0600252
253 return __inet_dev_addr_type(net, dev, addr, rt_table);
Laszlo Attila Toth055381162007-12-04 23:28:46 -0800254}
Eric Dumazet4bc2f182010-07-09 21:22:10 +0000255EXPORT_SYMBOL(inet_dev_addr_type);
Laszlo Attila Toth055381162007-12-04 23:28:46 -0800256
David Ahern30bbaa12015-08-13 14:59:05 -0600257/* inet_addr_type with dev == NULL but using the table from a dev
258 * if one is associated
259 */
260unsigned int inet_addr_type_dev_table(struct net *net,
261 const struct net_device *dev,
262 __be32 addr)
263{
David Ahern3236b002015-09-29 20:07:14 -0700264 u32 rt_table = l3mdev_fib_table(dev) ? : RT_TABLE_LOCAL;
David Ahern30bbaa12015-08-13 14:59:05 -0600265
266 return __inet_dev_addr_type(net, NULL, addr, rt_table);
267}
268EXPORT_SYMBOL(inet_addr_type_dev_table);
269
David S. Miller35ebf652012-06-28 03:59:11 -0700270__be32 fib_compute_spec_dst(struct sk_buff *skb)
271{
272 struct net_device *dev = skb->dev;
273 struct in_device *in_dev;
274 struct fib_result res;
David S. Millera207a4b2012-06-28 18:33:24 -0700275 struct rtable *rt;
David S. Miller35ebf652012-06-28 03:59:11 -0700276 struct net *net;
David S. Millera207a4b2012-06-28 18:33:24 -0700277 int scope;
David S. Miller35ebf652012-06-28 03:59:11 -0700278
David S. Millera207a4b2012-06-28 18:33:24 -0700279 rt = skb_rtable(skb);
Julian Anastasov0cc535a2012-07-18 21:35:03 +0000280 if ((rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST | RTCF_LOCAL)) ==
281 RTCF_LOCAL)
David S. Miller35ebf652012-06-28 03:59:11 -0700282 return ip_hdr(skb)->daddr;
283
284 in_dev = __in_dev_get_rcu(dev);
David S. Miller35ebf652012-06-28 03:59:11 -0700285
286 net = dev_net(dev);
David S. Millera207a4b2012-06-28 18:33:24 -0700287
288 scope = RT_SCOPE_UNIVERSE;
289 if (!ipv4_is_zeronet(ip_hdr(skb)->saddr)) {
Lorenzo Bianconi9fc12022018-07-27 18:15:46 +0200290 bool vmark = in_dev && IN_DEV_SRC_VMARK(in_dev);
Lance Richardson4cfc86f2016-03-22 14:56:57 -0400291 struct flowi4 fl4 = {
292 .flowi4_iif = LOOPBACK_IFINDEX,
David Aherne7372192018-07-07 16:15:26 -0700293 .flowi4_oif = l3mdev_master_ifindex_rcu(dev),
Lance Richardson4cfc86f2016-03-22 14:56:57 -0400294 .daddr = ip_hdr(skb)->saddr,
295 .flowi4_tos = RT_TOS(ip_hdr(skb)->tos),
296 .flowi4_scope = scope,
Lorenzo Bianconi9fc12022018-07-27 18:15:46 +0200297 .flowi4_mark = vmark ? skb->mark : 0,
Lance Richardson4cfc86f2016-03-22 14:56:57 -0400298 };
Andy Gospodarek0eeb0752015-06-23 13:45:37 -0400299 if (!fib_lookup(net, &fl4, &res, 0))
David Aherneba618a2019-04-02 14:11:55 -0700300 return fib_result_prefsrc(net, &res);
David S. Millera207a4b2012-06-28 18:33:24 -0700301 } else {
302 scope = RT_SCOPE_LINK;
303 }
304
305 return inet_select_addr(dev, ip_hdr(skb)->saddr, scope);
David S. Miller35ebf652012-06-28 03:59:11 -0700306}
307
David Ahern78f27562018-09-20 13:50:47 -0700308bool fib_info_nh_uses_dev(struct fib_info *fi, const struct net_device *dev)
309{
310 bool dev_match = false;
Eric Dumazet075e2642018-09-21 10:58:07 -0700311#ifdef CONFIG_IP_ROUTE_MULTIPATH
David Ahern1fd1c762020-05-26 12:56:18 -0600312 if (unlikely(fi->nh)) {
313 dev_match = nexthop_uses_dev(fi->nh, dev);
314 } else {
315 int ret;
David Ahern78f27562018-09-20 13:50:47 -0700316
David Ahern1fd1c762020-05-26 12:56:18 -0600317 for (ret = 0; ret < fib_info_num_path(fi); ret++) {
318 const struct fib_nh_common *nhc = fib_info_nhc(fi, ret);
David Ahern78f27562018-09-20 13:50:47 -0700319
David Ahern1fd1c762020-05-26 12:56:18 -0600320 if (nhc_l3mdev_matches_dev(nhc, dev)) {
321 dev_match = true;
322 break;
323 }
David Ahern78f27562018-09-20 13:50:47 -0700324 }
325 }
326#else
David Aherndcb1ecb2019-06-03 20:19:50 -0700327 if (fib_info_nhc(fi, 0)->nhc_dev == dev)
David Ahern78f27562018-09-20 13:50:47 -0700328 dev_match = true;
329#endif
330
331 return dev_match;
332}
333EXPORT_SYMBOL_GPL(fib_info_nh_uses_dev);
334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335/* Given (packet source, input interface) and optional (dst, oif, tos):
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000336 * - (main) check, that source is valid i.e. not broadcast or our local
337 * address.
338 * - figure out what "logical" interface this packet arrived
339 * and calculate "specific destination" address.
340 * - check, that packet arrived from expected physical interface.
Eric Dumazetebc0ffa2010-10-05 10:41:36 +0000341 * called with rcu_read_lock()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 */
David S. Miller7a9bc9b2012-06-29 01:32:45 -0700343static int __fib_validate_source(struct sk_buff *skb, __be32 src, __be32 dst,
344 u8 tos, int oif, struct net_device *dev,
345 int rpf, struct in_device *idev, u32 *itag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
David Ahern5a847a62018-05-16 13:36:40 -0700347 struct net *net = dev_net(dev);
348 struct flow_keys flkeys;
Sébastien Barré1dced6a2014-08-17 09:19:54 +0200349 int ret, no_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 struct fib_result res;
David S. Miller9e56e382012-06-28 18:54:02 -0700351 struct flowi4 fl4;
David S. Miller9e56e382012-06-28 18:54:02 -0700352 bool dev_match;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
David S. Miller9ade2282011-03-12 02:02:42 -0500354 fl4.flowi4_oif = 0;
David Ahern385add92015-09-29 20:07:13 -0700355 fl4.flowi4_iif = l3mdev_master_ifindex_rcu(dev);
David Aherncd2fbe12015-08-13 14:59:01 -0600356 if (!fl4.flowi4_iif)
357 fl4.flowi4_iif = oif ? : LOOPBACK_IFINDEX;
David S. Miller9ade2282011-03-12 02:02:42 -0500358 fl4.daddr = src;
359 fl4.saddr = dst;
360 fl4.flowi4_tos = tos;
361 fl4.flowi4_scope = RT_SCOPE_UNIVERSE;
Thomas Graf1b7179d2015-07-21 10:43:59 +0200362 fl4.flowi4_tun_key.tun_id = 0;
David Ahernb84f7872015-09-29 19:07:07 -0700363 fl4.flowi4_flags = 0;
Julian Anastasov8bcfd092017-02-26 15:50:52 +0200364 fl4.flowi4_uid = sock_net_uid(net, NULL);
David Ahern1869e222020-09-13 12:43:39 -0600365 fl4.flowi4_multipath_hash = 0;
David S. Millercc7e17e2011-03-09 20:57:50 -0800366
David S. Miller9e56e382012-06-28 18:54:02 -0700367 no_addr = idev->ifa_list == NULL;
Michael Smith990078a2011-04-07 04:51:51 +0000368
David S. Miller9e56e382012-06-28 18:54:02 -0700369 fl4.flowi4_mark = IN_DEV_SRC_VMARK(idev) ? skb->mark : 0;
David Ahern5a847a62018-05-16 13:36:40 -0700370 if (!fib4_rules_early_flow_dissect(net, skb, &fl4, &flkeys)) {
371 fl4.flowi4_proto = 0;
372 fl4.fl4_sport = 0;
373 fl4.fl4_dport = 0;
374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Andy Gospodarek0eeb0752015-06-23 13:45:37 -0400376 if (fib_lookup(net, &fl4, &res, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 goto last_resort;
Sébastien Barré1dced6a2014-08-17 09:19:54 +0200378 if (res.type != RTN_UNICAST &&
379 (res.type != RTN_LOCAL || !IN_DEV_ACCEPT_LOCAL(idev)))
380 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 fib_combine_itag(itag, &res);
David S. Miller6f86b322010-09-06 22:36:19 -0700382
David Ahern78f27562018-09-20 13:50:47 -0700383 dev_match = fib_info_nh_uses_dev(res.fi, dev);
Cong Wang66f82092019-07-17 14:41:58 -0700384 /* This is not common, loopback packets retain skb_dst so normally they
385 * would not even hit this slow path.
386 */
387 dev_match = dev_match || (res.type == RTN_LOCAL &&
388 dev == net->loopback_dev);
David S. Miller6f86b322010-09-06 22:36:19 -0700389 if (dev_match) {
David Aherneba618a2019-04-02 14:11:55 -0700390 ret = FIB_RES_NHC(res)->nhc_scope >= RT_SCOPE_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 return ret;
392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 if (no_addr)
394 goto last_resort;
Stephen Hemmingerc1cf8422009-02-20 08:25:36 +0000395 if (rpf == 1)
Eric Dumazetb5f7e752010-06-02 12:05:27 +0000396 goto e_rpf;
David S. Miller9ade2282011-03-12 02:02:42 -0500397 fl4.flowi4_oif = dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 ret = 0;
Andy Gospodarek0eeb0752015-06-23 13:45:37 -0400400 if (fib_lookup(net, &fl4, &res, FIB_LOOKUP_IGNORE_LINKSTATE) == 0) {
David S. Miller41347dc2012-06-28 04:05:27 -0700401 if (res.type == RTN_UNICAST)
David Aherneba618a2019-04-02 14:11:55 -0700402 ret = FIB_RES_NHC(res)->nhc_scope >= RT_SCOPE_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 }
404 return ret;
405
406last_resort:
407 if (rpf)
Eric Dumazetb5f7e752010-06-02 12:05:27 +0000408 goto e_rpf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 *itag = 0;
410 return 0;
411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412e_inval:
413 return -EINVAL;
Eric Dumazetb5f7e752010-06-02 12:05:27 +0000414e_rpf:
415 return -EXDEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416}
417
David S. Miller7a9bc9b2012-06-29 01:32:45 -0700418/* Ignore rp_filter for packets protected by IPsec. */
419int fib_validate_source(struct sk_buff *skb, __be32 src, __be32 dst,
420 u8 tos, int oif, struct net_device *dev,
421 struct in_device *idev, u32 *itag)
422{
423 int r = secpath_exists(skb) ? 0 : IN_DEV_RPFILTER(idev);
Paolo Abeni6e617de2017-09-20 18:26:53 +0200424 struct net *net = dev_net(dev);
David S. Miller7a9bc9b2012-06-29 01:32:45 -0700425
Paolo Abeni6e617de2017-09-20 18:26:53 +0200426 if (!r && !fib_num_tclassid_users(net) &&
Julian Anastasove81da0e2012-10-08 11:41:15 +0000427 (dev->ifindex != oif || !IN_DEV_TX_REDIRECTS(idev))) {
Paolo Abeni6e617de2017-09-20 18:26:53 +0200428 if (IN_DEV_ACCEPT_LOCAL(idev))
429 goto ok;
Paolo Abeni032a4802017-10-31 14:32:38 +0100430 /* with custom local routes in place, checking local addresses
431 * only will be too optimistic, with custom rules, checking
432 * local addresses only can be too strict, e.g. due to vrf
Paolo Abeni6e617de2017-09-20 18:26:53 +0200433 */
Paolo Abeni032a4802017-10-31 14:32:38 +0100434 if (net->ipv4.fib_has_custom_local_routes ||
435 fib4_has_custom_rules(net))
Paolo Abeni6e617de2017-09-20 18:26:53 +0200436 goto full_check;
437 if (inet_lookup_ifaddr_rcu(net, src))
438 return -EINVAL;
439
440ok:
David S. Miller7a9bc9b2012-06-29 01:32:45 -0700441 *itag = 0;
442 return 0;
443 }
Paolo Abeni6e617de2017-09-20 18:26:53 +0200444
445full_check:
David S. Miller7a9bc9b2012-06-29 01:32:45 -0700446 return __fib_validate_source(skb, src, dst, tos, oif, dev, r, idev, itag);
447}
448
Al Viro81f7bf62006-09-27 18:40:00 -0700449static inline __be32 sk_extract_addr(struct sockaddr *addr)
Thomas Graf4e902c52006-08-17 18:14:52 -0700450{
451 return ((struct sockaddr_in *) addr)->sin_addr.s_addr;
452}
453
454static int put_rtax(struct nlattr *mx, int len, int type, u32 value)
455{
456 struct nlattr *nla;
457
458 nla = (struct nlattr *) ((char *) mx + len);
459 nla->nla_type = type;
460 nla->nla_len = nla_attr_size(4);
461 *(u32 *) nla_data(nla) = value;
462
463 return len + nla_total_size(4);
464}
465
Denis V. Lunev4b5d47d2008-01-10 03:29:23 -0800466static int rtentry_to_fib_config(struct net *net, int cmd, struct rtentry *rt,
Thomas Graf4e902c52006-08-17 18:14:52 -0700467 struct fib_config *cfg)
468{
Al Viro6d85c102006-09-26 22:15:46 -0700469 __be32 addr;
Thomas Graf4e902c52006-08-17 18:14:52 -0700470 int plen;
471
472 memset(cfg, 0, sizeof(*cfg));
Denis V. Lunev4b5d47d2008-01-10 03:29:23 -0800473 cfg->fc_nlinfo.nl_net = net;
Thomas Graf4e902c52006-08-17 18:14:52 -0700474
475 if (rt->rt_dst.sa_family != AF_INET)
476 return -EAFNOSUPPORT;
477
478 /*
479 * Check mask for validity:
480 * a) it must be contiguous.
481 * b) destination must have all host bits clear.
482 * c) if application forgot to set correct family (AF_INET),
483 * reject request unless it is absolutely clear i.e.
484 * both family and mask are zero.
485 */
486 plen = 32;
487 addr = sk_extract_addr(&rt->rt_dst);
488 if (!(rt->rt_flags & RTF_HOST)) {
Al Viro81f7bf62006-09-27 18:40:00 -0700489 __be32 mask = sk_extract_addr(&rt->rt_genmask);
Thomas Graf4e902c52006-08-17 18:14:52 -0700490
491 if (rt->rt_genmask.sa_family != AF_INET) {
492 if (mask || rt->rt_genmask.sa_family)
493 return -EAFNOSUPPORT;
494 }
495
496 if (bad_mask(mask, addr))
497 return -EINVAL;
498
499 plen = inet_mask_len(mask);
500 }
501
502 cfg->fc_dst_len = plen;
503 cfg->fc_dst = addr;
504
505 if (cmd != SIOCDELRT) {
506 cfg->fc_nlflags = NLM_F_CREATE;
507 cfg->fc_protocol = RTPROT_BOOT;
508 }
509
510 if (rt->rt_metric)
511 cfg->fc_priority = rt->rt_metric - 1;
512
513 if (rt->rt_flags & RTF_REJECT) {
514 cfg->fc_scope = RT_SCOPE_HOST;
515 cfg->fc_type = RTN_UNREACHABLE;
516 return 0;
517 }
518
519 cfg->fc_scope = RT_SCOPE_NOWHERE;
520 cfg->fc_type = RTN_UNICAST;
521
522 if (rt->rt_dev) {
523 char *colon;
524 struct net_device *dev;
525 char devname[IFNAMSIZ];
526
527 if (copy_from_user(devname, rt->rt_dev, IFNAMSIZ-1))
528 return -EFAULT;
529
530 devname[IFNAMSIZ-1] = 0;
531 colon = strchr(devname, ':');
532 if (colon)
533 *colon = 0;
Denis V. Lunev4b5d47d2008-01-10 03:29:23 -0800534 dev = __dev_get_by_name(net, devname);
Thomas Graf4e902c52006-08-17 18:14:52 -0700535 if (!dev)
536 return -ENODEV;
537 cfg->fc_oif = dev->ifindex;
Mark Tomlinson5a56a0b2016-09-05 10:20:20 +1200538 cfg->fc_table = l3mdev_fib_table(dev);
Thomas Graf4e902c52006-08-17 18:14:52 -0700539 if (colon) {
Florian Westphalcd5a4112019-05-31 18:27:07 +0200540 const struct in_ifaddr *ifa;
541 struct in_device *in_dev;
542
543 in_dev = __in_dev_get_rtnl(dev);
Thomas Graf4e902c52006-08-17 18:14:52 -0700544 if (!in_dev)
545 return -ENODEV;
Florian Westphalcd5a4112019-05-31 18:27:07 +0200546
Thomas Graf4e902c52006-08-17 18:14:52 -0700547 *colon = ':';
Florian Westphalcd5a4112019-05-31 18:27:07 +0200548
549 rcu_read_lock();
550 in_dev_for_each_ifa_rcu(ifa, in_dev) {
Thomas Graf4e902c52006-08-17 18:14:52 -0700551 if (strcmp(ifa->ifa_label, devname) == 0)
552 break;
Florian Westphalcd5a4112019-05-31 18:27:07 +0200553 }
554 rcu_read_unlock();
555
Ian Morris51456b22015-04-03 09:17:26 +0100556 if (!ifa)
Thomas Graf4e902c52006-08-17 18:14:52 -0700557 return -ENODEV;
558 cfg->fc_prefsrc = ifa->ifa_local;
559 }
560 }
561
562 addr = sk_extract_addr(&rt->rt_gateway);
563 if (rt->rt_gateway.sa_family == AF_INET && addr) {
David Ahern30bbaa12015-08-13 14:59:05 -0600564 unsigned int addr_type;
565
David Ahernf35b7942019-04-05 16:30:28 -0700566 cfg->fc_gw4 = addr;
567 cfg->fc_gw_family = AF_INET;
David Ahern30bbaa12015-08-13 14:59:05 -0600568 addr_type = inet_addr_type_table(net, addr, cfg->fc_table);
Thomas Graf4e902c52006-08-17 18:14:52 -0700569 if (rt->rt_flags & RTF_GATEWAY &&
David Ahern30bbaa12015-08-13 14:59:05 -0600570 addr_type == RTN_UNICAST)
Thomas Graf4e902c52006-08-17 18:14:52 -0700571 cfg->fc_scope = RT_SCOPE_UNIVERSE;
572 }
573
574 if (cmd == SIOCDELRT)
575 return 0;
576
David Ahernf35b7942019-04-05 16:30:28 -0700577 if (rt->rt_flags & RTF_GATEWAY && !cfg->fc_gw_family)
Thomas Graf4e902c52006-08-17 18:14:52 -0700578 return -EINVAL;
579
580 if (cfg->fc_scope == RT_SCOPE_NOWHERE)
581 cfg->fc_scope = RT_SCOPE_LINK;
582
583 if (rt->rt_flags & (RTF_MTU | RTF_WINDOW | RTF_IRTT)) {
584 struct nlattr *mx;
585 int len = 0;
586
Kees Cook6396bb22018-06-12 14:03:40 -0700587 mx = kcalloc(3, nla_total_size(4), GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +0100588 if (!mx)
Thomas Graf4e902c52006-08-17 18:14:52 -0700589 return -ENOMEM;
590
591 if (rt->rt_flags & RTF_MTU)
592 len = put_rtax(mx, len, RTAX_ADVMSS, rt->rt_mtu - 40);
593
594 if (rt->rt_flags & RTF_WINDOW)
595 len = put_rtax(mx, len, RTAX_WINDOW, rt->rt_window);
596
597 if (rt->rt_flags & RTF_IRTT)
598 len = put_rtax(mx, len, RTAX_RTT, rt->rt_irtt << 3);
599
600 cfg->fc_mx = mx;
601 cfg->fc_mx_len = len;
602 }
603
604 return 0;
605}
606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607/*
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000608 * Handle IP routing ioctl calls.
609 * These are used to manipulate the routing tables
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 */
Al Viroca25c302017-07-01 08:03:10 -0400611int ip_rt_ioctl(struct net *net, unsigned int cmd, struct rtentry *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
Thomas Graf4e902c52006-08-17 18:14:52 -0700613 struct fib_config cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 switch (cmd) {
617 case SIOCADDRT: /* Add a route */
618 case SIOCDELRT: /* Delete a route */
Eric W. Biederman52e804c2012-11-16 03:03:05 +0000619 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 return -EPERM;
Thomas Graf4e902c52006-08-17 18:14:52 -0700621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 rtnl_lock();
Al Viroca25c302017-07-01 08:03:10 -0400623 err = rtentry_to_fib_config(net, cmd, rt, &cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 if (err == 0) {
Thomas Graf4e902c52006-08-17 18:14:52 -0700625 struct fib_table *tb;
626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 if (cmd == SIOCDELRT) {
Denis V. Lunev1bad1182008-01-10 03:29:53 -0800628 tb = fib_get_table(net, cfg.fc_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 if (tb)
David Ahern78055992017-05-27 16:19:26 -0600630 err = fib_table_delete(net, tb, &cfg,
631 NULL);
Thomas Graf4e902c52006-08-17 18:14:52 -0700632 else
633 err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 } else {
Denis V. Lunev1bad1182008-01-10 03:29:53 -0800635 tb = fib_new_table(net, cfg.fc_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 if (tb)
David Ahern6d8422a12017-05-21 10:12:02 -0600637 err = fib_table_insert(net, tb,
638 &cfg, NULL);
Thomas Graf4e902c52006-08-17 18:14:52 -0700639 else
640 err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 }
Thomas Graf4e902c52006-08-17 18:14:52 -0700642
643 /* allocated by rtentry_to_fib_config() */
644 kfree(cfg.fc_mx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 }
646 rtnl_unlock();
647 return err;
648 }
649 return -EINVAL;
650}
651
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000652const struct nla_policy rtm_ipv4_policy[RTA_MAX + 1] = {
David Ahern75425652019-05-22 12:07:43 -0700653 [RTA_UNSPEC] = { .strict_start_type = RTA_DPORT + 1 },
Thomas Graf4e902c52006-08-17 18:14:52 -0700654 [RTA_DST] = { .type = NLA_U32 },
655 [RTA_SRC] = { .type = NLA_U32 },
656 [RTA_IIF] = { .type = NLA_U32 },
657 [RTA_OIF] = { .type = NLA_U32 },
658 [RTA_GATEWAY] = { .type = NLA_U32 },
659 [RTA_PRIORITY] = { .type = NLA_U32 },
660 [RTA_PREFSRC] = { .type = NLA_U32 },
661 [RTA_METRICS] = { .type = NLA_NESTED },
Thomas Graf5176f912006-08-26 20:13:18 -0700662 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
Thomas Graf4e902c52006-08-17 18:14:52 -0700663 [RTA_FLOW] = { .type = NLA_U32 },
Roopa Prabhu571e7222015-07-21 10:43:47 +0200664 [RTA_ENCAP_TYPE] = { .type = NLA_U16 },
665 [RTA_ENCAP] = { .type = NLA_NESTED },
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +0900666 [RTA_UID] = { .type = NLA_U32 },
Liping Zhang3b45a412017-02-27 20:59:39 +0800667 [RTA_MARK] = { .type = NLA_U32 },
Roopa Prabhu2eabd762018-05-22 13:44:51 -0700668 [RTA_TABLE] = { .type = NLA_U32 },
Roopa Prabhu404eb772018-05-22 14:03:27 -0700669 [RTA_IP_PROTO] = { .type = NLA_U8 },
670 [RTA_SPORT] = { .type = NLA_U16 },
671 [RTA_DPORT] = { .type = NLA_U16 },
David Ahern493ced12019-06-08 14:53:32 -0700672 [RTA_NH_ID] = { .type = NLA_U32 },
Thomas Graf4e902c52006-08-17 18:14:52 -0700673};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
David Ahernd1566262019-04-05 16:30:40 -0700675int fib_gw_from_via(struct fib_config *cfg, struct nlattr *nla,
676 struct netlink_ext_ack *extack)
677{
678 struct rtvia *via;
679 int alen;
680
681 if (nla_len(nla) < offsetof(struct rtvia, rtvia_addr)) {
682 NL_SET_ERR_MSG(extack, "Invalid attribute length for RTA_VIA");
683 return -EINVAL;
684 }
685
686 via = nla_data(nla);
687 alen = nla_len(nla) - offsetof(struct rtvia, rtvia_addr);
688
689 switch (via->rtvia_family) {
690 case AF_INET:
691 if (alen != sizeof(__be32)) {
692 NL_SET_ERR_MSG(extack, "Invalid IPv4 address in RTA_VIA");
693 return -EINVAL;
694 }
695 cfg->fc_gw_family = AF_INET;
696 cfg->fc_gw4 = *((__be32 *)via->rtvia_addr);
697 break;
698 case AF_INET6:
699#ifdef CONFIG_IPV6
700 if (alen != sizeof(struct in6_addr)) {
701 NL_SET_ERR_MSG(extack, "Invalid IPv6 address in RTA_VIA");
702 return -EINVAL;
703 }
704 cfg->fc_gw_family = AF_INET6;
705 cfg->fc_gw6 = *((struct in6_addr *)via->rtvia_addr);
706#else
707 NL_SET_ERR_MSG(extack, "IPv6 support not enabled in kernel");
708 return -EINVAL;
709#endif
710 break;
711 default:
712 NL_SET_ERR_MSG(extack, "Unsupported address family in RTA_VIA");
713 return -EINVAL;
714 }
715
716 return 0;
717}
718
Denis V. Lunev4b5d47d2008-01-10 03:29:23 -0800719static int rtm_to_fib_config(struct net *net, struct sk_buff *skb,
David Ahern6d8422a12017-05-21 10:12:02 -0600720 struct nlmsghdr *nlh, struct fib_config *cfg,
721 struct netlink_ext_ack *extack)
Thomas Graf4e902c52006-08-17 18:14:52 -0700722{
David Ahernd1566262019-04-05 16:30:40 -0700723 bool has_gw = false, has_via = false;
Thomas Graf4e902c52006-08-17 18:14:52 -0700724 struct nlattr *attr;
725 int err, remaining;
726 struct rtmsg *rtm;
727
Johannes Berg8cb08172019-04-26 14:07:28 +0200728 err = nlmsg_validate_deprecated(nlh, sizeof(*rtm), RTA_MAX,
729 rtm_ipv4_policy, extack);
Thomas Graf4e902c52006-08-17 18:14:52 -0700730 if (err < 0)
731 goto errout;
732
733 memset(cfg, 0, sizeof(*cfg));
734
735 rtm = nlmsg_data(nlh);
Thomas Graf4e902c52006-08-17 18:14:52 -0700736 cfg->fc_dst_len = rtm->rtm_dst_len;
Thomas Graf4e902c52006-08-17 18:14:52 -0700737 cfg->fc_tos = rtm->rtm_tos;
738 cfg->fc_table = rtm->rtm_table;
739 cfg->fc_protocol = rtm->rtm_protocol;
740 cfg->fc_scope = rtm->rtm_scope;
741 cfg->fc_type = rtm->rtm_type;
742 cfg->fc_flags = rtm->rtm_flags;
743 cfg->fc_nlflags = nlh->nlmsg_flags;
744
Eric W. Biederman15e47302012-09-07 20:12:54 +0000745 cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid;
Thomas Graf4e902c52006-08-17 18:14:52 -0700746 cfg->fc_nlinfo.nlh = nlh;
Denis V. Lunev4b5d47d2008-01-10 03:29:23 -0800747 cfg->fc_nlinfo.nl_net = net;
Thomas Graf4e902c52006-08-17 18:14:52 -0700748
Thomas Grafa0ee18b2007-03-24 20:32:54 -0700749 if (cfg->fc_type > RTN_MAX) {
David Ahernc3ab2b42017-05-21 10:12:03 -0600750 NL_SET_ERR_MSG(extack, "Invalid route type");
Thomas Grafa0ee18b2007-03-24 20:32:54 -0700751 err = -EINVAL;
752 goto errout;
753 }
754
Thomas Graf4e902c52006-08-17 18:14:52 -0700755 nlmsg_for_each_attr(attr, nlh, sizeof(struct rtmsg), remaining) {
Thomas Graf8f4c1f92007-09-12 14:44:36 +0200756 switch (nla_type(attr)) {
Thomas Graf4e902c52006-08-17 18:14:52 -0700757 case RTA_DST:
Al Viro17fb2c62006-09-26 22:15:25 -0700758 cfg->fc_dst = nla_get_be32(attr);
Thomas Graf4e902c52006-08-17 18:14:52 -0700759 break;
Thomas Graf4e902c52006-08-17 18:14:52 -0700760 case RTA_OIF:
761 cfg->fc_oif = nla_get_u32(attr);
762 break;
763 case RTA_GATEWAY:
David Ahernd1566262019-04-05 16:30:40 -0700764 has_gw = true;
David Ahernf35b7942019-04-05 16:30:28 -0700765 cfg->fc_gw4 = nla_get_be32(attr);
David Ahernd73f80f2019-04-10 10:05:51 -0700766 if (cfg->fc_gw4)
767 cfg->fc_gw_family = AF_INET;
Thomas Graf4e902c52006-08-17 18:14:52 -0700768 break;
David Ahernb6e9e5d2019-02-26 09:00:02 -0800769 case RTA_VIA:
David Ahernd1566262019-04-05 16:30:40 -0700770 has_via = true;
771 err = fib_gw_from_via(cfg, attr, extack);
772 if (err)
773 goto errout;
774 break;
Thomas Graf4e902c52006-08-17 18:14:52 -0700775 case RTA_PRIORITY:
776 cfg->fc_priority = nla_get_u32(attr);
777 break;
778 case RTA_PREFSRC:
Al Viro17fb2c62006-09-26 22:15:25 -0700779 cfg->fc_prefsrc = nla_get_be32(attr);
Thomas Graf4e902c52006-08-17 18:14:52 -0700780 break;
781 case RTA_METRICS:
782 cfg->fc_mx = nla_data(attr);
783 cfg->fc_mx_len = nla_len(attr);
784 break;
785 case RTA_MULTIPATH:
David Ahern9ed59592017-01-17 14:57:36 -0800786 err = lwtunnel_valid_encap_type_attr(nla_data(attr),
David Ahernc255bd62017-05-27 16:19:27 -0600787 nla_len(attr),
788 extack);
David Ahern9ed59592017-01-17 14:57:36 -0800789 if (err < 0)
790 goto errout;
Thomas Graf4e902c52006-08-17 18:14:52 -0700791 cfg->fc_mp = nla_data(attr);
792 cfg->fc_mp_len = nla_len(attr);
793 break;
794 case RTA_FLOW:
795 cfg->fc_flow = nla_get_u32(attr);
796 break;
Thomas Graf4e902c52006-08-17 18:14:52 -0700797 case RTA_TABLE:
798 cfg->fc_table = nla_get_u32(attr);
799 break;
Roopa Prabhu571e7222015-07-21 10:43:47 +0200800 case RTA_ENCAP:
801 cfg->fc_encap = attr;
802 break;
803 case RTA_ENCAP_TYPE:
804 cfg->fc_encap_type = nla_get_u16(attr);
David Ahernc255bd62017-05-27 16:19:27 -0600805 err = lwtunnel_valid_encap_type(cfg->fc_encap_type,
806 extack);
David Ahern9ed59592017-01-17 14:57:36 -0800807 if (err < 0)
808 goto errout;
Roopa Prabhu571e7222015-07-21 10:43:47 +0200809 break;
David Ahern493ced12019-06-08 14:53:32 -0700810 case RTA_NH_ID:
811 cfg->fc_nh_id = nla_get_u32(attr);
812 break;
813 }
814 }
815
816 if (cfg->fc_nh_id) {
817 if (cfg->fc_oif || cfg->fc_gw_family ||
818 cfg->fc_encap || cfg->fc_mp) {
819 NL_SET_ERR_MSG(extack,
820 "Nexthop specification and nexthop id are mutually exclusive");
821 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 }
823 }
Thomas Graf4e902c52006-08-17 18:14:52 -0700824
David Ahernd1566262019-04-05 16:30:40 -0700825 if (has_gw && has_via) {
826 NL_SET_ERR_MSG(extack,
827 "Nexthop configuration can not contain both GATEWAY and VIA");
828 goto errout;
829 }
830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 return 0;
Thomas Graf4e902c52006-08-17 18:14:52 -0700832errout:
833 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834}
835
David Ahernc21ef3e2017-04-16 09:48:24 -0700836static int inet_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
837 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900839 struct net *net = sock_net(skb->sk);
Thomas Graf4e902c52006-08-17 18:14:52 -0700840 struct fib_config cfg;
841 struct fib_table *tb;
842 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
David Ahern6d8422a12017-05-21 10:12:02 -0600844 err = rtm_to_fib_config(net, skb, nlh, &cfg, extack);
Thomas Graf4e902c52006-08-17 18:14:52 -0700845 if (err < 0)
846 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
David Ahern493ced12019-06-08 14:53:32 -0700848 if (cfg.fc_nh_id && !nexthop_find_by_id(net, cfg.fc_nh_id)) {
849 NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
850 err = -EINVAL;
851 goto errout;
852 }
853
Denis V. Lunev8ad49422008-01-10 03:24:11 -0800854 tb = fib_get_table(net, cfg.fc_table);
Ian Morris51456b22015-04-03 09:17:26 +0100855 if (!tb) {
David Ahernc3ab2b42017-05-21 10:12:03 -0600856 NL_SET_ERR_MSG(extack, "FIB table does not exist");
Thomas Graf4e902c52006-08-17 18:14:52 -0700857 err = -ESRCH;
858 goto errout;
859 }
860
David Ahern78055992017-05-27 16:19:26 -0600861 err = fib_table_delete(net, tb, &cfg, extack);
Thomas Graf4e902c52006-08-17 18:14:52 -0700862errout:
863 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864}
865
David Ahernc21ef3e2017-04-16 09:48:24 -0700866static int inet_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
867 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900869 struct net *net = sock_net(skb->sk);
Thomas Graf4e902c52006-08-17 18:14:52 -0700870 struct fib_config cfg;
871 struct fib_table *tb;
872 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
David Ahern6d8422a12017-05-21 10:12:02 -0600874 err = rtm_to_fib_config(net, skb, nlh, &cfg, extack);
Thomas Graf4e902c52006-08-17 18:14:52 -0700875 if (err < 0)
876 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Denis V. Lunev226b0b4a52008-01-10 03:30:24 -0800878 tb = fib_new_table(net, cfg.fc_table);
Ian Morris51456b22015-04-03 09:17:26 +0100879 if (!tb) {
Thomas Graf4e902c52006-08-17 18:14:52 -0700880 err = -ENOBUFS;
881 goto errout;
882 }
883
David Ahern6d8422a12017-05-21 10:12:02 -0600884 err = fib_table_insert(net, tb, &cfg, extack);
Paolo Abeni6e617de2017-09-20 18:26:53 +0200885 if (!err && cfg.fc_type == RTN_LOCAL)
886 net->ipv4.fib_has_custom_local_routes = true;
Thomas Graf4e902c52006-08-17 18:14:52 -0700887errout:
888 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889}
890
David Ahern47246762018-10-15 18:56:42 -0700891int ip_valid_fib_dump_req(struct net *net, const struct nlmsghdr *nlh,
892 struct fib_dump_filter *filter,
David Aherneffe6792018-10-15 18:56:48 -0700893 struct netlink_callback *cb)
David Aherne8ba3302018-10-07 20:16:35 -0700894{
David Aherneffe6792018-10-15 18:56:48 -0700895 struct netlink_ext_ack *extack = cb->extack;
896 struct nlattr *tb[RTA_MAX + 1];
David Aherne8ba3302018-10-07 20:16:35 -0700897 struct rtmsg *rtm;
David Aherneffe6792018-10-15 18:56:48 -0700898 int err, i;
899
900 ASSERT_RTNL();
David Aherne8ba3302018-10-07 20:16:35 -0700901
902 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
903 NL_SET_ERR_MSG(extack, "Invalid header for FIB dump request");
904 return -EINVAL;
905 }
906
907 rtm = nlmsg_data(nlh);
908 if (rtm->rtm_dst_len || rtm->rtm_src_len || rtm->rtm_tos ||
David Aherneffe6792018-10-15 18:56:48 -0700909 rtm->rtm_scope) {
David Aherne8ba3302018-10-07 20:16:35 -0700910 NL_SET_ERR_MSG(extack, "Invalid values in header for FIB dump request");
911 return -EINVAL;
912 }
Stefano Brivio564c91f2019-06-21 17:45:20 +0200913
David Aherne8ba3302018-10-07 20:16:35 -0700914 if (rtm->rtm_flags & ~(RTM_F_CLONED | RTM_F_PREFIX)) {
915 NL_SET_ERR_MSG(extack, "Invalid flags for FIB dump request");
916 return -EINVAL;
917 }
Stefano Brivio564c91f2019-06-21 17:45:20 +0200918 if (rtm->rtm_flags & RTM_F_CLONED)
919 filter->dump_routes = false;
920 else
921 filter->dump_exceptions = false;
David Aherne8ba3302018-10-07 20:16:35 -0700922
David Aherneffe6792018-10-15 18:56:48 -0700923 filter->flags = rtm->rtm_flags;
924 filter->protocol = rtm->rtm_protocol;
925 filter->rt_type = rtm->rtm_type;
926 filter->table_id = rtm->rtm_table;
927
Johannes Berg8cb08172019-04-26 14:07:28 +0200928 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
929 rtm_ipv4_policy, extack);
David Aherneffe6792018-10-15 18:56:48 -0700930 if (err < 0)
931 return err;
932
933 for (i = 0; i <= RTA_MAX; ++i) {
934 int ifindex;
935
936 if (!tb[i])
937 continue;
938
939 switch (i) {
940 case RTA_TABLE:
941 filter->table_id = nla_get_u32(tb[i]);
942 break;
943 case RTA_OIF:
944 ifindex = nla_get_u32(tb[i]);
945 filter->dev = __dev_get_by_index(net, ifindex);
946 if (!filter->dev)
947 return -ENODEV;
948 break;
949 default:
950 NL_SET_ERR_MSG(extack, "Unsupported attribute in dump request");
951 return -EINVAL;
952 }
953 }
954
955 if (filter->flags || filter->protocol || filter->rt_type ||
956 filter->table_id || filter->dev) {
957 filter->filter_set = 1;
958 cb->answer_flags = NLM_F_DUMP_FILTERED;
David Aherne8ba3302018-10-07 20:16:35 -0700959 }
960
961 return 0;
962}
963EXPORT_SYMBOL_GPL(ip_valid_fib_dump_req);
964
Thomas Graf63f34442007-03-22 11:55:17 -0700965static int inet_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966{
Stefano Brivio564c91f2019-06-21 17:45:20 +0200967 struct fib_dump_filter filter = { .dump_routes = true,
968 .dump_exceptions = true };
David Aherne8ba3302018-10-07 20:16:35 -0700969 const struct nlmsghdr *nlh = cb->nlh;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900970 struct net *net = sock_net(skb->sk);
Patrick McHardy1af5a8c2006-08-10 23:10:46 -0700971 unsigned int h, s_h;
972 unsigned int e = 0, s_e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 struct fib_table *tb;
Denis V. Luneve4aef8a2008-01-10 03:28:24 -0800974 struct hlist_head *head;
David Ahernf6c57752017-05-15 23:19:17 -0700975 int dumped = 0, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
David Aherne8ba3302018-10-07 20:16:35 -0700977 if (cb->strict_check) {
David Aherneffe6792018-10-15 18:56:48 -0700978 err = ip_valid_fib_dump_req(net, nlh, &filter, cb);
David Aherne8ba3302018-10-07 20:16:35 -0700979 if (err < 0)
980 return err;
David Aherne4e92fb2018-10-15 18:56:51 -0700981 } else if (nlmsg_len(nlh) >= sizeof(struct rtmsg)) {
982 struct rtmsg *rtm = nlmsg_data(nlh);
983
984 filter.flags = rtm->rtm_flags & (RTM_F_PREFIX | RTM_F_CLONED);
David Aherne8ba3302018-10-07 20:16:35 -0700985 }
986
Stefano Briviob597ca6e2019-06-21 17:45:21 +0200987 /* ipv4 does not use prefix flag */
988 if (filter.flags & RTM_F_PREFIX)
Li RongQing0b8c7f62014-03-21 11:33:10 +0800989 return skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
David Ahern18a80212018-10-15 18:56:43 -0700991 if (filter.table_id) {
992 tb = fib_get_table(net, filter.table_id);
993 if (!tb) {
Sabrina Dubroca41b4bd92020-05-20 11:15:46 +0200994 if (rtnl_msg_family(cb->nlh) != PF_INET)
David Ahernae677bb2018-10-24 12:59:01 -0700995 return skb->len;
996
David Ahern18a80212018-10-15 18:56:43 -0700997 NL_SET_ERR_MSG(cb->extack, "ipv4: FIB table does not exist");
998 return -ENOENT;
999 }
1000
Qian Caidddeb302020-03-19 22:54:21 -04001001 rcu_read_lock();
David Ahern18a80212018-10-15 18:56:43 -07001002 err = fib_table_dump(tb, skb, cb, &filter);
Qian Caidddeb302020-03-19 22:54:21 -04001003 rcu_read_unlock();
David Ahern18a80212018-10-15 18:56:43 -07001004 return skb->len ? : err;
1005 }
1006
Patrick McHardy1af5a8c2006-08-10 23:10:46 -07001007 s_h = cb->args[0];
1008 s_e = cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Alexander Duycka7e53532015-03-04 15:02:44 -08001010 rcu_read_lock();
1011
Patrick McHardy1af5a8c2006-08-10 23:10:46 -07001012 for (h = s_h; h < FIB_TABLE_HASHSZ; h++, s_e = 0) {
1013 e = 0;
Denis V. Luneve4aef8a2008-01-10 03:28:24 -08001014 head = &net->ipv4.fib_table_hash[h];
Alexander Duycka7e53532015-03-04 15:02:44 -08001015 hlist_for_each_entry_rcu(tb, head, tb_hlist) {
Patrick McHardy1af5a8c2006-08-10 23:10:46 -07001016 if (e < s_e)
1017 goto next;
1018 if (dumped)
1019 memset(&cb->args[2], 0, sizeof(cb->args) -
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001020 2 * sizeof(cb->args[0]));
David Ahern18a80212018-10-15 18:56:43 -07001021 err = fib_table_dump(tb, skb, cb, &filter);
David Ahernf6c57752017-05-15 23:19:17 -07001022 if (err < 0) {
1023 if (likely(skb->len))
1024 goto out;
1025
1026 goto out_err;
1027 }
Patrick McHardy1af5a8c2006-08-10 23:10:46 -07001028 dumped = 1;
1029next:
1030 e++;
1031 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 }
Patrick McHardy1af5a8c2006-08-10 23:10:46 -07001033out:
David Ahernf6c57752017-05-15 23:19:17 -07001034 err = skb->len;
1035out_err:
Alexander Duycka7e53532015-03-04 15:02:44 -08001036 rcu_read_unlock();
1037
Patrick McHardy1af5a8c2006-08-10 23:10:46 -07001038 cb->args[1] = e;
1039 cb->args[0] = h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
David Ahernf6c57752017-05-15 23:19:17 -07001041 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042}
1043
1044/* Prepare and feed intra-kernel routing request.
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001045 * Really, it should be netlink message, but :-( netlink
1046 * can be not configured, so that we feed it directly
1047 * to fib engine. It is legal, because all events occur
1048 * only when netlink is already locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 */
David Ahernaf4d7682018-05-27 08:09:57 -07001050static void fib_magic(int cmd, int type, __be32 dst, int dst_len,
1051 struct in_ifaddr *ifa, u32 rt_priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001053 struct net *net = dev_net(ifa->ifa_dev->dev);
David Ahern3236b002015-09-29 20:07:14 -07001054 u32 tb_id = l3mdev_fib_table(ifa->ifa_dev->dev);
Thomas Graf4e902c52006-08-17 18:14:52 -07001055 struct fib_table *tb;
1056 struct fib_config cfg = {
1057 .fc_protocol = RTPROT_KERNEL,
1058 .fc_type = type,
1059 .fc_dst = dst,
1060 .fc_dst_len = dst_len,
David Ahernaf4d7682018-05-27 08:09:57 -07001061 .fc_priority = rt_priority,
Thomas Graf4e902c52006-08-17 18:14:52 -07001062 .fc_prefsrc = ifa->ifa_local,
1063 .fc_oif = ifa->ifa_dev->dev->ifindex,
1064 .fc_nlflags = NLM_F_CREATE | NLM_F_APPEND,
Denis V. Lunev4d1169c2008-01-10 03:26:13 -08001065 .fc_nlinfo = {
Denis V. Lunev4b5d47d2008-01-10 03:29:23 -08001066 .nl_net = net,
Denis V. Lunev4d1169c2008-01-10 03:26:13 -08001067 },
Thomas Graf4e902c52006-08-17 18:14:52 -07001068 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
David Ahern021dd3b2015-08-13 14:59:06 -06001070 if (!tb_id)
1071 tb_id = (type == RTN_UNICAST) ? RT_TABLE_MAIN : RT_TABLE_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
David Ahern021dd3b2015-08-13 14:59:06 -06001073 tb = fib_new_table(net, tb_id);
Ian Morris51456b22015-04-03 09:17:26 +01001074 if (!tb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 return;
1076
Thomas Graf4e902c52006-08-17 18:14:52 -07001077 cfg.fc_table = tb->tb_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
Thomas Graf4e902c52006-08-17 18:14:52 -07001079 if (type != RTN_LOCAL)
1080 cfg.fc_scope = RT_SCOPE_LINK;
1081 else
1082 cfg.fc_scope = RT_SCOPE_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
1084 if (cmd == RTM_NEWROUTE)
David Ahern6d8422a12017-05-21 10:12:02 -06001085 fib_table_insert(net, tb, &cfg, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 else
David Ahern78055992017-05-27 16:19:26 -06001087 fib_table_delete(net, tb, &cfg, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088}
1089
Jamal Hadi Salim0ff60a42005-11-22 14:47:37 -08001090void fib_add_ifaddr(struct in_ifaddr *ifa)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091{
1092 struct in_device *in_dev = ifa->ifa_dev;
1093 struct net_device *dev = in_dev->dev;
1094 struct in_ifaddr *prim = ifa;
Al Viroa144ea42006-09-28 18:00:55 -07001095 __be32 mask = ifa->ifa_mask;
1096 __be32 addr = ifa->ifa_local;
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001097 __be32 prefix = ifa->ifa_address & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001099 if (ifa->ifa_flags & IFA_F_SECONDARY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 prim = inet_ifa_byprefix(in_dev, prefix, mask);
Ian Morris51456b22015-04-03 09:17:26 +01001101 if (!prim) {
Joe Perches058bd4d2012-03-11 18:36:11 +00001102 pr_warn("%s: bug: prim == NULL\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 return;
1104 }
1105 }
1106
David Ahernaf4d7682018-05-27 08:09:57 -07001107 fib_magic(RTM_NEWROUTE, RTN_LOCAL, addr, 32, prim, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001109 if (!(dev->flags & IFF_UP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 return;
1111
1112 /* Add broadcast address, if it is explicitly assigned. */
Al Viroa144ea42006-09-28 18:00:55 -07001113 if (ifa->ifa_broadcast && ifa->ifa_broadcast != htonl(0xFFFFFFFF))
David Ahernaf4d7682018-05-27 08:09:57 -07001114 fib_magic(RTM_NEWROUTE, RTN_BROADCAST, ifa->ifa_broadcast, 32,
1115 prim, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001117 if (!ipv4_is_zeronet(prefix) && !(ifa->ifa_flags & IFA_F_SECONDARY) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 (prefix != addr || ifa->ifa_prefixlen < 32)) {
Paolo Abeni7b131182015-10-20 10:28:45 +02001119 if (!(ifa->ifa_flags & IFA_F_NOPREFIXROUTE))
1120 fib_magic(RTM_NEWROUTE,
1121 dev->flags & IFF_LOOPBACK ? RTN_LOCAL : RTN_UNICAST,
David Ahernaf4d7682018-05-27 08:09:57 -07001122 prefix, ifa->ifa_prefixlen, prim,
1123 ifa->ifa_rt_priority);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
1125 /* Add network specific broadcasts, when it takes a sense */
1126 if (ifa->ifa_prefixlen < 31) {
David Ahernaf4d7682018-05-27 08:09:57 -07001127 fib_magic(RTM_NEWROUTE, RTN_BROADCAST, prefix, 32,
1128 prim, 0);
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001129 fib_magic(RTM_NEWROUTE, RTN_BROADCAST, prefix | ~mask,
David Ahernaf4d7682018-05-27 08:09:57 -07001130 32, prim, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 }
1132 }
1133}
1134
David Ahernaf4d7682018-05-27 08:09:57 -07001135void fib_modify_prefix_metric(struct in_ifaddr *ifa, u32 new_metric)
1136{
1137 __be32 prefix = ifa->ifa_address & ifa->ifa_mask;
1138 struct in_device *in_dev = ifa->ifa_dev;
1139 struct net_device *dev = in_dev->dev;
1140
1141 if (!(dev->flags & IFF_UP) ||
1142 ifa->ifa_flags & (IFA_F_SECONDARY | IFA_F_NOPREFIXROUTE) ||
1143 ipv4_is_zeronet(prefix) ||
Paolo Abeni0b834ba2019-10-26 11:53:39 +02001144 (prefix == ifa->ifa_local && ifa->ifa_prefixlen == 32))
David Ahernaf4d7682018-05-27 08:09:57 -07001145 return;
1146
1147 /* add the new */
1148 fib_magic(RTM_NEWROUTE,
1149 dev->flags & IFF_LOOPBACK ? RTN_LOCAL : RTN_UNICAST,
1150 prefix, ifa->ifa_prefixlen, ifa, new_metric);
1151
1152 /* delete the old */
1153 fib_magic(RTM_DELROUTE,
1154 dev->flags & IFF_LOOPBACK ? RTN_LOCAL : RTN_UNICAST,
1155 prefix, ifa->ifa_prefixlen, ifa, ifa->ifa_rt_priority);
1156}
1157
Julian Anastasove6abbaa2011-03-19 12:13:49 +00001158/* Delete primary or secondary address.
1159 * Optionally, on secondary address promotion consider the addresses
1160 * from subnet iprim as deleted, even if they are in device list.
1161 * In this case the secondary ifa can be in device list.
1162 */
1163void fib_del_ifaddr(struct in_ifaddr *ifa, struct in_ifaddr *iprim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164{
1165 struct in_device *in_dev = ifa->ifa_dev;
1166 struct net_device *dev = in_dev->dev;
1167 struct in_ifaddr *ifa1;
Julian Anastasove6abbaa2011-03-19 12:13:49 +00001168 struct in_ifaddr *prim = ifa, *prim1 = NULL;
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001169 __be32 brd = ifa->ifa_address | ~ifa->ifa_mask;
1170 __be32 any = ifa->ifa_address & ifa->ifa_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171#define LOCAL_OK 1
1172#define BRD_OK 2
1173#define BRD0_OK 4
1174#define BRD1_OK 8
Eric Dumazet95c96172012-04-15 05:58:06 +00001175 unsigned int ok = 0;
Julian Anastasove6abbaa2011-03-19 12:13:49 +00001176 int subnet = 0; /* Primary network */
1177 int gone = 1; /* Address is missing */
1178 int same_prefsrc = 0; /* Another primary with same IP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Julian Anastasove6abbaa2011-03-19 12:13:49 +00001180 if (ifa->ifa_flags & IFA_F_SECONDARY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 prim = inet_ifa_byprefix(in_dev, any, ifa->ifa_mask);
Ian Morris51456b22015-04-03 09:17:26 +01001182 if (!prim) {
Paolo Abeni391a2032016-04-21 22:23:31 +02001183 /* if the device has been deleted, we don't perform
1184 * address promotion
1185 */
1186 if (!in_dev->dead)
1187 pr_warn("%s: bug: prim == NULL\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 return;
1189 }
Julian Anastasove6abbaa2011-03-19 12:13:49 +00001190 if (iprim && iprim != prim) {
Joe Perches058bd4d2012-03-11 18:36:11 +00001191 pr_warn("%s: bug: iprim != prim\n", __func__);
Julian Anastasove6abbaa2011-03-19 12:13:49 +00001192 return;
1193 }
1194 } else if (!ipv4_is_zeronet(any) &&
1195 (any != ifa->ifa_local || ifa->ifa_prefixlen < 32)) {
Paolo Abeni7b131182015-10-20 10:28:45 +02001196 if (!(ifa->ifa_flags & IFA_F_NOPREFIXROUTE))
1197 fib_magic(RTM_DELROUTE,
1198 dev->flags & IFF_LOOPBACK ? RTN_LOCAL : RTN_UNICAST,
David Ahernaf4d7682018-05-27 08:09:57 -07001199 any, ifa->ifa_prefixlen, prim, 0);
Julian Anastasove6abbaa2011-03-19 12:13:49 +00001200 subnet = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 }
1202
David S. Millerfbd40ea2016-03-13 23:28:00 -04001203 if (in_dev->dead)
1204 goto no_promotions;
1205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 /* Deletion is more complicated than add.
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001207 * We should take care of not to delete too much :-)
1208 *
1209 * Scan address list to be sure that addresses are really gone.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 */
Florian Westphalcd5a4112019-05-31 18:27:07 +02001211 rcu_read_lock();
1212 in_dev_for_each_ifa_rcu(ifa1, in_dev) {
Julian Anastasove6abbaa2011-03-19 12:13:49 +00001213 if (ifa1 == ifa) {
1214 /* promotion, keep the IP */
1215 gone = 0;
1216 continue;
1217 }
1218 /* Ignore IFAs from our subnet */
1219 if (iprim && ifa1->ifa_mask == iprim->ifa_mask &&
1220 inet_ifa_match(ifa1->ifa_address, iprim))
1221 continue;
1222
1223 /* Ignore ifa1 if it uses different primary IP (prefsrc) */
1224 if (ifa1->ifa_flags & IFA_F_SECONDARY) {
1225 /* Another address from our subnet? */
1226 if (ifa1->ifa_mask == prim->ifa_mask &&
1227 inet_ifa_match(ifa1->ifa_address, prim))
1228 prim1 = prim;
1229 else {
1230 /* We reached the secondaries, so
1231 * same_prefsrc should be determined.
1232 */
1233 if (!same_prefsrc)
1234 continue;
1235 /* Search new prim1 if ifa1 is not
1236 * using the current prim1
1237 */
1238 if (!prim1 ||
1239 ifa1->ifa_mask != prim1->ifa_mask ||
1240 !inet_ifa_match(ifa1->ifa_address, prim1))
1241 prim1 = inet_ifa_byprefix(in_dev,
1242 ifa1->ifa_address,
1243 ifa1->ifa_mask);
1244 if (!prim1)
1245 continue;
1246 if (prim1->ifa_local != prim->ifa_local)
1247 continue;
1248 }
1249 } else {
1250 if (prim->ifa_local != ifa1->ifa_local)
1251 continue;
1252 prim1 = ifa1;
1253 if (prim != prim1)
1254 same_prefsrc = 1;
1255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 if (ifa->ifa_local == ifa1->ifa_local)
1257 ok |= LOCAL_OK;
1258 if (ifa->ifa_broadcast == ifa1->ifa_broadcast)
1259 ok |= BRD_OK;
1260 if (brd == ifa1->ifa_broadcast)
1261 ok |= BRD1_OK;
1262 if (any == ifa1->ifa_broadcast)
1263 ok |= BRD0_OK;
Julian Anastasove6abbaa2011-03-19 12:13:49 +00001264 /* primary has network specific broadcasts */
1265 if (prim1 == ifa1 && ifa1->ifa_prefixlen < 31) {
1266 __be32 brd1 = ifa1->ifa_address | ~ifa1->ifa_mask;
1267 __be32 any1 = ifa1->ifa_address & ifa1->ifa_mask;
1268
1269 if (!ipv4_is_zeronet(any1)) {
1270 if (ifa->ifa_broadcast == brd1 ||
1271 ifa->ifa_broadcast == any1)
1272 ok |= BRD_OK;
1273 if (brd == brd1 || brd == any1)
1274 ok |= BRD1_OK;
1275 if (any == brd1 || any == any1)
1276 ok |= BRD0_OK;
1277 }
1278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 }
Florian Westphalcd5a4112019-05-31 18:27:07 +02001280 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
David S. Millerfbd40ea2016-03-13 23:28:00 -04001282no_promotions:
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001283 if (!(ok & BRD_OK))
David Ahernaf4d7682018-05-27 08:09:57 -07001284 fib_magic(RTM_DELROUTE, RTN_BROADCAST, ifa->ifa_broadcast, 32,
1285 prim, 0);
Julian Anastasove6abbaa2011-03-19 12:13:49 +00001286 if (subnet && ifa->ifa_prefixlen < 31) {
1287 if (!(ok & BRD1_OK))
David Ahernaf4d7682018-05-27 08:09:57 -07001288 fib_magic(RTM_DELROUTE, RTN_BROADCAST, brd, 32,
1289 prim, 0);
Julian Anastasove6abbaa2011-03-19 12:13:49 +00001290 if (!(ok & BRD0_OK))
David Ahernaf4d7682018-05-27 08:09:57 -07001291 fib_magic(RTM_DELROUTE, RTN_BROADCAST, any, 32,
1292 prim, 0);
Julian Anastasove6abbaa2011-03-19 12:13:49 +00001293 }
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001294 if (!(ok & LOCAL_OK)) {
David Ahern30bbaa12015-08-13 14:59:05 -06001295 unsigned int addr_type;
1296
David Ahernaf4d7682018-05-27 08:09:57 -07001297 fib_magic(RTM_DELROUTE, RTN_LOCAL, ifa->ifa_local, 32, prim, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
1299 /* Check, that this local address finally disappeared. */
David Ahern30bbaa12015-08-13 14:59:05 -06001300 addr_type = inet_addr_type_dev_table(dev_net(dev), dev,
1301 ifa->ifa_local);
1302 if (gone && addr_type != RTN_LOCAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 /* And the last, but not the least thing.
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001304 * We must flush stray FIB entries.
1305 *
1306 * First of all, we scan fib_info list searching
1307 * for stray nexthop entries, then ignite fib_flush.
1308 */
Mark Tomlinson5a56a0b2016-09-05 10:20:20 +12001309 if (fib_sync_down_addr(dev, ifa->ifa_local))
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001310 fib_flush(dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 }
1312 }
1313#undef LOCAL_OK
1314#undef BRD_OK
1315#undef BRD0_OK
1316#undef BRD1_OK
1317}
1318
Alexander Duyck345e9b52014-12-31 10:56:24 -08001319static void nl_fib_lookup(struct net *net, struct fib_result_nl *frn)
Robert Olsson246955f2005-06-20 13:36:39 -07001320{
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001321
Robert Olsson246955f2005-06-20 13:36:39 -07001322 struct fib_result res;
David S. Miller9ade2282011-03-12 02:02:42 -05001323 struct flowi4 fl4 = {
1324 .flowi4_mark = frn->fl_mark,
1325 .daddr = frn->fl_addr,
1326 .flowi4_tos = frn->fl_tos,
1327 .flowi4_scope = frn->fl_scope,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001328 };
Alexander Duyck345e9b52014-12-31 10:56:24 -08001329 struct fib_table *tb;
1330
1331 rcu_read_lock();
1332
1333 tb = fib_get_table(net, frn->tb_id_in);
Alexey Kuznetsov1194ed02007-04-25 13:07:28 -07001334
1335 frn->err = -ENOENT;
Robert Olsson246955f2005-06-20 13:36:39 -07001336 if (tb) {
1337 local_bh_disable();
1338
1339 frn->tb_id = tb->tb_id;
David S. Miller9ade2282011-03-12 02:02:42 -05001340 frn->err = fib_table_lookup(tb, &fl4, &res, FIB_LOOKUP_NOREF);
Robert Olsson246955f2005-06-20 13:36:39 -07001341
1342 if (!frn->err) {
1343 frn->prefixlen = res.prefixlen;
1344 frn->nh_sel = res.nh_sel;
1345 frn->type = res.type;
1346 frn->scope = res.scope;
1347 }
1348 local_bh_enable();
1349 }
Alexander Duyck345e9b52014-12-31 10:56:24 -08001350
1351 rcu_read_unlock();
Robert Olsson246955f2005-06-20 13:36:39 -07001352}
1353
David S. Miller28f7b0362007-10-10 21:32:39 -07001354static void nl_fib_input(struct sk_buff *skb)
Robert Olsson246955f2005-06-20 13:36:39 -07001355{
Denis V. Lunev6bd48fc2008-01-10 03:28:55 -08001356 struct net *net;
Robert Olsson246955f2005-06-20 13:36:39 -07001357 struct fib_result_nl *frn;
David S. Miller28f7b0362007-10-10 21:32:39 -07001358 struct nlmsghdr *nlh;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001359 u32 portid;
Alexey Kuznetsov1194ed02007-04-25 13:07:28 -07001360
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001361 net = sock_net(skb->sk);
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -07001362 nlh = nlmsg_hdr(skb);
Eric Dumazetc64c0b32017-03-21 19:22:28 -07001363 if (skb->len < nlmsg_total_size(sizeof(*frn)) ||
1364 skb->len < nlh->nlmsg_len ||
Hong zhi guo573ce262013-03-27 06:47:04 +00001365 nlmsg_len(nlh) < sizeof(*frn))
Thomas Grafea865752005-12-01 14:30:00 -08001366 return;
Denis V. Lunevd883a032007-12-21 02:01:53 -08001367
Pablo Neira3a365152013-06-28 03:04:23 +02001368 skb = netlink_skb_clone(skb, GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +01001369 if (!skb)
Denis V. Lunevd883a032007-12-21 02:01:53 -08001370 return;
1371 nlh = nlmsg_hdr(skb);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001372
Hong zhi guo573ce262013-03-27 06:47:04 +00001373 frn = (struct fib_result_nl *) nlmsg_data(nlh);
Alexander Duyck345e9b52014-12-31 10:56:24 -08001374 nl_fib_lookup(net, frn);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001375
Rami Rosen28a28282013-01-11 09:59:20 +00001376 portid = NETLINK_CB(skb).portid; /* netlink portid */
Eric W. Biederman15e47302012-09-07 20:12:54 +00001377 NETLINK_CB(skb).portid = 0; /* from kernel */
Patrick McHardyac6d4392005-08-14 19:29:52 -07001378 NETLINK_CB(skb).dst_group = 0; /* unicast */
Eric W. Biederman15e47302012-09-07 20:12:54 +00001379 netlink_unicast(net->ipv4.fibnl, skb, portid, MSG_DONTWAIT);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001380}
Robert Olsson246955f2005-06-20 13:36:39 -07001381
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001382static int __net_init nl_fib_lookup_init(struct net *net)
Robert Olsson246955f2005-06-20 13:36:39 -07001383{
Denis V. Lunev6bd48fc2008-01-10 03:28:55 -08001384 struct sock *sk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00001385 struct netlink_kernel_cfg cfg = {
1386 .input = nl_fib_input,
1387 };
1388
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00001389 sk = netlink_kernel_create(net, NETLINK_FIB_LOOKUP, &cfg);
Ian Morris51456b22015-04-03 09:17:26 +01001390 if (!sk)
Denis V. Lunev7b1a74fd2008-01-10 03:22:17 -08001391 return -EAFNOSUPPORT;
Denis V. Lunev6bd48fc2008-01-10 03:28:55 -08001392 net->ipv4.fibnl = sk;
Denis V. Lunev7b1a74fd2008-01-10 03:22:17 -08001393 return 0;
1394}
1395
1396static void nl_fib_lookup_exit(struct net *net)
1397{
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08001398 netlink_kernel_release(net->ipv4.fibnl);
Denis V. Lunev775516b2008-01-18 23:55:19 -08001399 net->ipv4.fibnl = NULL;
Robert Olsson246955f2005-06-20 13:36:39 -07001400}
1401
Julian Anastasov4f823de2015-10-30 10:23:33 +02001402static void fib_disable_ip(struct net_device *dev, unsigned long event,
1403 bool force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
Julian Anastasov4f823de2015-10-30 10:23:33 +02001405 if (fib_sync_down_dev(dev, event, force))
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001406 fib_flush(dev_net(dev));
Gao Feng06b4fc52017-04-26 19:04:04 +08001407 else
1408 rt_cache_flush(dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 arp_ifdown(dev);
1410}
1411
1412static int fib_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
1413{
Jianjun Kong6ed2533e2008-11-03 00:25:16 -08001414 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
Denis V. Lunev76e6ebf2008-07-05 19:00:44 -07001415 struct net_device *dev = ifa->ifa_dev->dev;
David S. Miller436c3b62011-03-24 17:42:21 -07001416 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
1418 switch (event) {
1419 case NETDEV_UP:
1420 fib_add_ifaddr(ifa);
1421#ifdef CONFIG_IP_ROUTE_MULTIPATH
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001422 fib_sync_up(dev, RTNH_F_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423#endif
David S. Miller436c3b62011-03-24 17:42:21 -07001424 atomic_inc(&net->ipv4.dev_addr_genid);
Nicolas Dichtel4ccfe6d2012-09-07 00:45:29 +00001425 rt_cache_flush(dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 break;
1427 case NETDEV_DOWN:
Julian Anastasove6abbaa2011-03-19 12:13:49 +00001428 fib_del_ifaddr(ifa, NULL);
David S. Miller436c3b62011-03-24 17:42:21 -07001429 atomic_inc(&net->ipv4.dev_addr_genid);
Ian Morris51456b22015-04-03 09:17:26 +01001430 if (!ifa->ifa_dev->ifa_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 /* Last address was deleted from this interface.
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001432 * Disable IP.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 */
Julian Anastasov4f823de2015-10-30 10:23:33 +02001434 fib_disable_ip(dev, event, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 } else {
Nicolas Dichtel4ccfe6d2012-09-07 00:45:29 +00001436 rt_cache_flush(dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 }
1438 break;
1439 }
1440 return NOTIFY_DONE;
1441}
1442
1443static int fib_netdev_event(struct notifier_block *this, unsigned long event, void *ptr)
1444{
Jiri Pirko351638e2013-05-28 01:30:21 +00001445 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02001446 struct netdev_notifier_changeupper_info *upper_info = ptr;
1447 struct netdev_notifier_info_ext *info_ext = ptr;
Eric Dumazet0115e8e2012-08-22 17:19:46 +00001448 struct in_device *in_dev;
David S. Miller436c3b62011-03-24 17:42:21 -07001449 struct net *net = dev_net(dev);
Florian Westphalcd5a4112019-05-31 18:27:07 +02001450 struct in_ifaddr *ifa;
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001451 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
1453 if (event == NETDEV_UNREGISTER) {
Julian Anastasov4f823de2015-10-30 10:23:33 +02001454 fib_disable_ip(dev, event, true);
David S. Millercaacf052012-07-31 15:06:50 -07001455 rt_flush_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 return NOTIFY_DONE;
1457 }
1458
Eric Dumazet0115e8e2012-08-22 17:19:46 +00001459 in_dev = __in_dev_get_rtnl(dev);
Oliver Hartkoppa0065f22014-01-23 10:19:34 +01001460 if (!in_dev)
1461 return NOTIFY_DONE;
Eric Dumazet0115e8e2012-08-22 17:19:46 +00001462
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 switch (event) {
1464 case NETDEV_UP:
Florian Westphalcd5a4112019-05-31 18:27:07 +02001465 in_dev_for_each_ifa_rtnl(ifa, in_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 fib_add_ifaddr(ifa);
Florian Westphalcd5a4112019-05-31 18:27:07 +02001467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468#ifdef CONFIG_IP_ROUTE_MULTIPATH
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001469 fib_sync_up(dev, RTNH_F_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470#endif
David S. Miller436c3b62011-03-24 17:42:21 -07001471 atomic_inc(&net->ipv4.dev_addr_genid);
Nicolas Dichtel4ccfe6d2012-09-07 00:45:29 +00001472 rt_cache_flush(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 break;
1474 case NETDEV_DOWN:
Julian Anastasov4f823de2015-10-30 10:23:33 +02001475 fib_disable_ip(dev, event, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 case NETDEV_CHANGE:
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001478 flags = dev_get_flags(dev);
1479 if (flags & (IFF_RUNNING | IFF_LOWER_UP))
1480 fib_sync_up(dev, RTNH_F_LINKDOWN);
1481 else
Julian Anastasov4f823de2015-10-30 10:23:33 +02001482 fib_sync_down_dev(dev, event, false);
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02001483 rt_cache_flush(net);
1484 break;
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001485 case NETDEV_CHANGEMTU:
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02001486 fib_sync_mtu(dev, info_ext->ext.mtu);
Nicolas Dichtel4ccfe6d2012-09-07 00:45:29 +00001487 rt_cache_flush(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 break;
David Ahern7f49e7a2015-12-10 10:25:24 -08001489 case NETDEV_CHANGEUPPER:
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02001490 upper_info = ptr;
David Ahern7f49e7a2015-12-10 10:25:24 -08001491 /* flush all routes if dev is linked to or unlinked from
1492 * an L3 master device (e.g., VRF)
1493 */
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02001494 if (upper_info->upper_dev &&
1495 netif_is_l3_master(upper_info->upper_dev))
David Ahern7f49e7a2015-12-10 10:25:24 -08001496 fib_disable_ip(dev, NETDEV_DOWN, true);
1497 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 }
1499 return NOTIFY_DONE;
1500}
1501
1502static struct notifier_block fib_inetaddr_notifier = {
Jianjun Kong6ed2533e2008-11-03 00:25:16 -08001503 .notifier_call = fib_inetaddr_event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504};
1505
1506static struct notifier_block fib_netdev_notifier = {
Jianjun Kong6ed2533e2008-11-03 00:25:16 -08001507 .notifier_call = fib_netdev_event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508};
1509
Denis V. Lunev7b1a74fd2008-01-10 03:22:17 -08001510static int __net_init ip_fib_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511{
Denis V. Lunevdce5cbe2008-01-31 18:44:53 -08001512 int err;
Eric Dumazet10da66f2010-10-13 08:22:03 +00001513 size_t size = sizeof(struct hlist_head) * FIB_TABLE_HASHSZ;
Patrick McHardy1af5a8c2006-08-10 23:10:46 -07001514
Ido Schimmel04b1d4e2017-08-03 13:28:11 +02001515 err = fib4_notifier_init(net);
1516 if (err)
1517 return err;
Ido Schimmelcacaad12016-12-03 16:45:06 +01001518
Eric Dumazet10da66f2010-10-13 08:22:03 +00001519 /* Avoid false sharing : Use at least a full cache line */
1520 size = max_t(size_t, size, L1_CACHE_BYTES);
1521
1522 net->ipv4.fib_table_hash = kzalloc(size, GFP_KERNEL);
Ido Schimmel04b1d4e2017-08-03 13:28:11 +02001523 if (!net->ipv4.fib_table_hash) {
1524 err = -ENOMEM;
1525 goto err_table_hash_alloc;
1526 }
Denis V. Luneve4aef8a2008-01-10 03:28:24 -08001527
Denis V. Lunevdce5cbe2008-01-31 18:44:53 -08001528 err = fib4_rules_init(net);
1529 if (err < 0)
Ido Schimmel04b1d4e2017-08-03 13:28:11 +02001530 goto err_rules_init;
Denis V. Lunevdce5cbe2008-01-31 18:44:53 -08001531 return 0;
1532
Ido Schimmel04b1d4e2017-08-03 13:28:11 +02001533err_rules_init:
Denis V. Lunevdce5cbe2008-01-31 18:44:53 -08001534 kfree(net->ipv4.fib_table_hash);
Ido Schimmel04b1d4e2017-08-03 13:28:11 +02001535err_table_hash_alloc:
1536 fib4_notifier_exit(net);
Denis V. Lunevdce5cbe2008-01-31 18:44:53 -08001537 return err;
Denis V. Lunev7b1a74fd2008-01-10 03:22:17 -08001538}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001540static void ip_fib_net_exit(struct net *net)
Denis V. Lunev7b1a74fd2008-01-10 03:22:17 -08001541{
Ido Schimmelb4681c22017-12-20 19:34:19 +02001542 int i;
Thomas Graf63f34442007-03-22 11:55:17 -07001543
Sabrina Dubroca6dede752015-03-10 21:03:54 +01001544 rtnl_lock();
Alexander Duyck6e47d6c2015-03-27 14:14:22 -07001545#ifdef CONFIG_IP_MULTIPLE_TABLES
Alexander Duyck6e47d6c2015-03-27 14:14:22 -07001546 RCU_INIT_POINTER(net->ipv4.fib_main, NULL);
1547 RCU_INIT_POINTER(net->ipv4.fib_default, NULL);
1548#endif
Ido Schimmelb4681c22017-12-20 19:34:19 +02001549 /* Destroy the tables in reverse order to guarantee that the
1550 * local table, ID 255, is destroyed before the main table, ID
1551 * 254. This is necessary as the local table may contain
1552 * references to data contained in the main table.
1553 */
1554 for (i = FIB_TABLE_HASHSZ - 1; i >= 0; i--) {
Alexander Duycka7e53532015-03-04 15:02:44 -08001555 struct hlist_head *head = &net->ipv4.fib_table_hash[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001556 struct hlist_node *tmp;
Alexander Duycka7e53532015-03-04 15:02:44 -08001557 struct fib_table *tb;
Denis V. Lunev7b1a74fd2008-01-10 03:22:17 -08001558
Alexander Duycka7e53532015-03-04 15:02:44 -08001559 hlist_for_each_entry_safe(tb, tmp, head, tb_hlist) {
Alexander Duycka7e53532015-03-04 15:02:44 -08001560 hlist_del(&tb->tb_hlist);
Ido Schimmelf97f4dd2019-01-09 09:57:39 +00001561 fib_table_flush(net, tb, true);
Pavel Emelyanov4aa2c462010-10-28 02:00:43 +00001562 fib_free_table(tb);
Denis V. Lunev7b1a74fd2008-01-10 03:22:17 -08001563 }
1564 }
Alexander Duyckad88d052015-03-27 14:14:16 -07001565
1566#ifdef CONFIG_IP_MULTIPLE_TABLES
1567 fib4_rules_exit(net);
1568#endif
Eric Dumazete2666f82011-03-30 16:57:46 -07001569 rtnl_unlock();
Denis V. Luneve4aef8a2008-01-10 03:28:24 -08001570 kfree(net->ipv4.fib_table_hash);
Ido Schimmel04b1d4e2017-08-03 13:28:11 +02001571 fib4_notifier_exit(net);
Denis V. Lunev7b1a74fd2008-01-10 03:22:17 -08001572}
1573
1574static int __net_init fib_net_init(struct net *net)
1575{
1576 int error;
1577
David S. Millerf4530fa2012-07-05 22:13:13 -07001578#ifdef CONFIG_IP_ROUTE_CLASSID
1579 net->ipv4.fib_num_tclassid_users = 0;
1580#endif
Denis V. Lunev7b1a74fd2008-01-10 03:22:17 -08001581 error = ip_fib_net_init(net);
1582 if (error < 0)
1583 goto out;
1584 error = nl_fib_lookup_init(net);
1585 if (error < 0)
1586 goto out_nlfl;
1587 error = fib_proc_init(net);
1588 if (error < 0)
1589 goto out_proc;
1590out:
1591 return error;
1592
1593out_proc:
1594 nl_fib_lookup_exit(net);
1595out_nlfl:
1596 ip_fib_net_exit(net);
1597 goto out;
1598}
1599
1600static void __net_exit fib_net_exit(struct net *net)
1601{
1602 fib_proc_exit(net);
1603 nl_fib_lookup_exit(net);
1604 ip_fib_net_exit(net);
1605}
1606
1607static struct pernet_operations fib_net_ops = {
1608 .init = fib_net_init,
1609 .exit = fib_net_exit,
1610};
1611
1612void __init ip_fib_init(void)
1613{
Mahesh Bandewar8799a222017-07-19 15:41:33 -07001614 fib_trie_init();
Denis V. Lunev7b1a74fd2008-01-10 03:22:17 -08001615
1616 register_pernet_subsys(&fib_net_ops);
Mahesh Bandewar8799a222017-07-19 15:41:33 -07001617
Denis V. Lunev7b1a74fd2008-01-10 03:22:17 -08001618 register_netdevice_notifier(&fib_netdev_notifier);
1619 register_inetaddr_notifier(&fib_inetaddr_notifier);
Stephen Hemminger7f9b8052008-01-14 23:14:20 -08001620
Florian Westphalb97bac62017-08-09 20:41:48 +02001621 rtnl_register(PF_INET, RTM_NEWROUTE, inet_rtm_newroute, NULL, 0);
1622 rtnl_register(PF_INET, RTM_DELROUTE, inet_rtm_delroute, NULL, 0);
1623 rtnl_register(PF_INET, RTM_GETROUTE, NULL, inet_dump_fib, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624}