blob: 213be9c050addf59205ed7b9e50c505aedc4524c [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 Ahern78f27562018-09-20 13:50:47 -0700312 int ret;
313
David Ahern5481d732019-06-03 20:19:49 -0700314 for (ret = 0; ret < fib_info_num_path(fi); ret++) {
David Aherndcb1ecb2019-06-03 20:19:50 -0700315 const struct fib_nh_common *nhc = fib_info_nhc(fi, ret);
David Ahern78f27562018-09-20 13:50:47 -0700316
David Aherndcb1ecb2019-06-03 20:19:50 -0700317 if (nhc->nhc_dev == dev) {
David Ahern78f27562018-09-20 13:50:47 -0700318 dev_match = true;
319 break;
David Aherndcb1ecb2019-06-03 20:19:50 -0700320 } else if (l3mdev_master_ifindex_rcu(nhc->nhc_dev) == dev->ifindex) {
David Ahern78f27562018-09-20 13:50:47 -0700321 dev_match = true;
322 break;
323 }
324 }
325#else
David Aherndcb1ecb2019-06-03 20:19:50 -0700326 if (fib_info_nhc(fi, 0)->nhc_dev == dev)
David Ahern78f27562018-09-20 13:50:47 -0700327 dev_match = true;
328#endif
329
330 return dev_match;
331}
332EXPORT_SYMBOL_GPL(fib_info_nh_uses_dev);
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334/* Given (packet source, input interface) and optional (dst, oif, tos):
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000335 * - (main) check, that source is valid i.e. not broadcast or our local
336 * address.
337 * - figure out what "logical" interface this packet arrived
338 * and calculate "specific destination" address.
339 * - check, that packet arrived from expected physical interface.
Eric Dumazetebc0ffa2010-10-05 10:41:36 +0000340 * called with rcu_read_lock()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 */
David S. Miller7a9bc9b2012-06-29 01:32:45 -0700342static int __fib_validate_source(struct sk_buff *skb, __be32 src, __be32 dst,
343 u8 tos, int oif, struct net_device *dev,
344 int rpf, struct in_device *idev, u32 *itag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
David Ahern5a847a62018-05-16 13:36:40 -0700346 struct net *net = dev_net(dev);
347 struct flow_keys flkeys;
Sébastien Barré1dced6a2014-08-17 09:19:54 +0200348 int ret, no_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 struct fib_result res;
David S. Miller9e56e382012-06-28 18:54:02 -0700350 struct flowi4 fl4;
David S. Miller9e56e382012-06-28 18:54:02 -0700351 bool dev_match;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
David S. Miller9ade2282011-03-12 02:02:42 -0500353 fl4.flowi4_oif = 0;
David Ahern385add92015-09-29 20:07:13 -0700354 fl4.flowi4_iif = l3mdev_master_ifindex_rcu(dev);
David Aherncd2fbe12015-08-13 14:59:01 -0600355 if (!fl4.flowi4_iif)
356 fl4.flowi4_iif = oif ? : LOOPBACK_IFINDEX;
David S. Miller9ade2282011-03-12 02:02:42 -0500357 fl4.daddr = src;
358 fl4.saddr = dst;
359 fl4.flowi4_tos = tos;
360 fl4.flowi4_scope = RT_SCOPE_UNIVERSE;
Thomas Graf1b7179d2015-07-21 10:43:59 +0200361 fl4.flowi4_tun_key.tun_id = 0;
David Ahernb84f7872015-09-29 19:07:07 -0700362 fl4.flowi4_flags = 0;
Julian Anastasov8bcfd092017-02-26 15:50:52 +0200363 fl4.flowi4_uid = sock_net_uid(net, NULL);
David S. Millercc7e17e2011-03-09 20:57:50 -0800364
David S. Miller9e56e382012-06-28 18:54:02 -0700365 no_addr = idev->ifa_list == NULL;
Michael Smith990078a2011-04-07 04:51:51 +0000366
David S. Miller9e56e382012-06-28 18:54:02 -0700367 fl4.flowi4_mark = IN_DEV_SRC_VMARK(idev) ? skb->mark : 0;
David Ahern5a847a62018-05-16 13:36:40 -0700368 if (!fib4_rules_early_flow_dissect(net, skb, &fl4, &flkeys)) {
369 fl4.flowi4_proto = 0;
370 fl4.fl4_sport = 0;
371 fl4.fl4_dport = 0;
372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Andy Gospodarek0eeb0752015-06-23 13:45:37 -0400374 if (fib_lookup(net, &fl4, &res, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 goto last_resort;
Sébastien Barré1dced6a2014-08-17 09:19:54 +0200376 if (res.type != RTN_UNICAST &&
377 (res.type != RTN_LOCAL || !IN_DEV_ACCEPT_LOCAL(idev)))
378 goto e_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 fib_combine_itag(itag, &res);
David S. Miller6f86b322010-09-06 22:36:19 -0700380
David Ahern78f27562018-09-20 13:50:47 -0700381 dev_match = fib_info_nh_uses_dev(res.fi, dev);
Cong Wang66f82092019-07-17 14:41:58 -0700382 /* This is not common, loopback packets retain skb_dst so normally they
383 * would not even hit this slow path.
384 */
385 dev_match = dev_match || (res.type == RTN_LOCAL &&
386 dev == net->loopback_dev);
David S. Miller6f86b322010-09-06 22:36:19 -0700387 if (dev_match) {
David Aherneba618a2019-04-02 14:11:55 -0700388 ret = FIB_RES_NHC(res)->nhc_scope >= RT_SCOPE_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return ret;
390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 if (no_addr)
392 goto last_resort;
Stephen Hemmingerc1cf8422009-02-20 08:25:36 +0000393 if (rpf == 1)
Eric Dumazetb5f7e752010-06-02 12:05:27 +0000394 goto e_rpf;
David S. Miller9ade2282011-03-12 02:02:42 -0500395 fl4.flowi4_oif = dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 ret = 0;
Andy Gospodarek0eeb0752015-06-23 13:45:37 -0400398 if (fib_lookup(net, &fl4, &res, FIB_LOOKUP_IGNORE_LINKSTATE) == 0) {
David S. Miller41347dc2012-06-28 04:05:27 -0700399 if (res.type == RTN_UNICAST)
David Aherneba618a2019-04-02 14:11:55 -0700400 ret = FIB_RES_NHC(res)->nhc_scope >= RT_SCOPE_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 }
402 return ret;
403
404last_resort:
405 if (rpf)
Eric Dumazetb5f7e752010-06-02 12:05:27 +0000406 goto e_rpf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 *itag = 0;
408 return 0;
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410e_inval:
411 return -EINVAL;
Eric Dumazetb5f7e752010-06-02 12:05:27 +0000412e_rpf:
413 return -EXDEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414}
415
David S. Miller7a9bc9b2012-06-29 01:32:45 -0700416/* Ignore rp_filter for packets protected by IPsec. */
417int fib_validate_source(struct sk_buff *skb, __be32 src, __be32 dst,
418 u8 tos, int oif, struct net_device *dev,
419 struct in_device *idev, u32 *itag)
420{
421 int r = secpath_exists(skb) ? 0 : IN_DEV_RPFILTER(idev);
Paolo Abeni6e617de2017-09-20 18:26:53 +0200422 struct net *net = dev_net(dev);
David S. Miller7a9bc9b2012-06-29 01:32:45 -0700423
Paolo Abeni6e617de2017-09-20 18:26:53 +0200424 if (!r && !fib_num_tclassid_users(net) &&
Julian Anastasove81da0e2012-10-08 11:41:15 +0000425 (dev->ifindex != oif || !IN_DEV_TX_REDIRECTS(idev))) {
Paolo Abeni6e617de2017-09-20 18:26:53 +0200426 if (IN_DEV_ACCEPT_LOCAL(idev))
427 goto ok;
Paolo Abeni032a4802017-10-31 14:32:38 +0100428 /* with custom local routes in place, checking local addresses
429 * only will be too optimistic, with custom rules, checking
430 * local addresses only can be too strict, e.g. due to vrf
Paolo Abeni6e617de2017-09-20 18:26:53 +0200431 */
Paolo Abeni032a4802017-10-31 14:32:38 +0100432 if (net->ipv4.fib_has_custom_local_routes ||
433 fib4_has_custom_rules(net))
Paolo Abeni6e617de2017-09-20 18:26:53 +0200434 goto full_check;
435 if (inet_lookup_ifaddr_rcu(net, src))
436 return -EINVAL;
437
438ok:
David S. Miller7a9bc9b2012-06-29 01:32:45 -0700439 *itag = 0;
440 return 0;
441 }
Paolo Abeni6e617de2017-09-20 18:26:53 +0200442
443full_check:
David S. Miller7a9bc9b2012-06-29 01:32:45 -0700444 return __fib_validate_source(skb, src, dst, tos, oif, dev, r, idev, itag);
445}
446
Al Viro81f7bf62006-09-27 18:40:00 -0700447static inline __be32 sk_extract_addr(struct sockaddr *addr)
Thomas Graf4e902c52006-08-17 18:14:52 -0700448{
449 return ((struct sockaddr_in *) addr)->sin_addr.s_addr;
450}
451
452static int put_rtax(struct nlattr *mx, int len, int type, u32 value)
453{
454 struct nlattr *nla;
455
456 nla = (struct nlattr *) ((char *) mx + len);
457 nla->nla_type = type;
458 nla->nla_len = nla_attr_size(4);
459 *(u32 *) nla_data(nla) = value;
460
461 return len + nla_total_size(4);
462}
463
Denis V. Lunev4b5d47d2008-01-10 03:29:23 -0800464static int rtentry_to_fib_config(struct net *net, int cmd, struct rtentry *rt,
Thomas Graf4e902c52006-08-17 18:14:52 -0700465 struct fib_config *cfg)
466{
Al Viro6d85c102006-09-26 22:15:46 -0700467 __be32 addr;
Thomas Graf4e902c52006-08-17 18:14:52 -0700468 int plen;
469
470 memset(cfg, 0, sizeof(*cfg));
Denis V. Lunev4b5d47d2008-01-10 03:29:23 -0800471 cfg->fc_nlinfo.nl_net = net;
Thomas Graf4e902c52006-08-17 18:14:52 -0700472
473 if (rt->rt_dst.sa_family != AF_INET)
474 return -EAFNOSUPPORT;
475
476 /*
477 * Check mask for validity:
478 * a) it must be contiguous.
479 * b) destination must have all host bits clear.
480 * c) if application forgot to set correct family (AF_INET),
481 * reject request unless it is absolutely clear i.e.
482 * both family and mask are zero.
483 */
484 plen = 32;
485 addr = sk_extract_addr(&rt->rt_dst);
486 if (!(rt->rt_flags & RTF_HOST)) {
Al Viro81f7bf62006-09-27 18:40:00 -0700487 __be32 mask = sk_extract_addr(&rt->rt_genmask);
Thomas Graf4e902c52006-08-17 18:14:52 -0700488
489 if (rt->rt_genmask.sa_family != AF_INET) {
490 if (mask || rt->rt_genmask.sa_family)
491 return -EAFNOSUPPORT;
492 }
493
494 if (bad_mask(mask, addr))
495 return -EINVAL;
496
497 plen = inet_mask_len(mask);
498 }
499
500 cfg->fc_dst_len = plen;
501 cfg->fc_dst = addr;
502
503 if (cmd != SIOCDELRT) {
504 cfg->fc_nlflags = NLM_F_CREATE;
505 cfg->fc_protocol = RTPROT_BOOT;
506 }
507
508 if (rt->rt_metric)
509 cfg->fc_priority = rt->rt_metric - 1;
510
511 if (rt->rt_flags & RTF_REJECT) {
512 cfg->fc_scope = RT_SCOPE_HOST;
513 cfg->fc_type = RTN_UNREACHABLE;
514 return 0;
515 }
516
517 cfg->fc_scope = RT_SCOPE_NOWHERE;
518 cfg->fc_type = RTN_UNICAST;
519
520 if (rt->rt_dev) {
521 char *colon;
522 struct net_device *dev;
523 char devname[IFNAMSIZ];
524
525 if (copy_from_user(devname, rt->rt_dev, IFNAMSIZ-1))
526 return -EFAULT;
527
528 devname[IFNAMSIZ-1] = 0;
529 colon = strchr(devname, ':');
530 if (colon)
531 *colon = 0;
Denis V. Lunev4b5d47d2008-01-10 03:29:23 -0800532 dev = __dev_get_by_name(net, devname);
Thomas Graf4e902c52006-08-17 18:14:52 -0700533 if (!dev)
534 return -ENODEV;
535 cfg->fc_oif = dev->ifindex;
Mark Tomlinson5a56a0b2016-09-05 10:20:20 +1200536 cfg->fc_table = l3mdev_fib_table(dev);
Thomas Graf4e902c52006-08-17 18:14:52 -0700537 if (colon) {
Florian Westphalcd5a4112019-05-31 18:27:07 +0200538 const struct in_ifaddr *ifa;
539 struct in_device *in_dev;
540
541 in_dev = __in_dev_get_rtnl(dev);
Thomas Graf4e902c52006-08-17 18:14:52 -0700542 if (!in_dev)
543 return -ENODEV;
Florian Westphalcd5a4112019-05-31 18:27:07 +0200544
Thomas Graf4e902c52006-08-17 18:14:52 -0700545 *colon = ':';
Florian Westphalcd5a4112019-05-31 18:27:07 +0200546
547 rcu_read_lock();
548 in_dev_for_each_ifa_rcu(ifa, in_dev) {
Thomas Graf4e902c52006-08-17 18:14:52 -0700549 if (strcmp(ifa->ifa_label, devname) == 0)
550 break;
Florian Westphalcd5a4112019-05-31 18:27:07 +0200551 }
552 rcu_read_unlock();
553
Ian Morris51456b22015-04-03 09:17:26 +0100554 if (!ifa)
Thomas Graf4e902c52006-08-17 18:14:52 -0700555 return -ENODEV;
556 cfg->fc_prefsrc = ifa->ifa_local;
557 }
558 }
559
560 addr = sk_extract_addr(&rt->rt_gateway);
561 if (rt->rt_gateway.sa_family == AF_INET && addr) {
David Ahern30bbaa12015-08-13 14:59:05 -0600562 unsigned int addr_type;
563
David Ahernf35b7942019-04-05 16:30:28 -0700564 cfg->fc_gw4 = addr;
565 cfg->fc_gw_family = AF_INET;
David Ahern30bbaa12015-08-13 14:59:05 -0600566 addr_type = inet_addr_type_table(net, addr, cfg->fc_table);
Thomas Graf4e902c52006-08-17 18:14:52 -0700567 if (rt->rt_flags & RTF_GATEWAY &&
David Ahern30bbaa12015-08-13 14:59:05 -0600568 addr_type == RTN_UNICAST)
Thomas Graf4e902c52006-08-17 18:14:52 -0700569 cfg->fc_scope = RT_SCOPE_UNIVERSE;
570 }
571
572 if (cmd == SIOCDELRT)
573 return 0;
574
David Ahernf35b7942019-04-05 16:30:28 -0700575 if (rt->rt_flags & RTF_GATEWAY && !cfg->fc_gw_family)
Thomas Graf4e902c52006-08-17 18:14:52 -0700576 return -EINVAL;
577
578 if (cfg->fc_scope == RT_SCOPE_NOWHERE)
579 cfg->fc_scope = RT_SCOPE_LINK;
580
581 if (rt->rt_flags & (RTF_MTU | RTF_WINDOW | RTF_IRTT)) {
582 struct nlattr *mx;
583 int len = 0;
584
Kees Cook6396bb22018-06-12 14:03:40 -0700585 mx = kcalloc(3, nla_total_size(4), GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +0100586 if (!mx)
Thomas Graf4e902c52006-08-17 18:14:52 -0700587 return -ENOMEM;
588
589 if (rt->rt_flags & RTF_MTU)
590 len = put_rtax(mx, len, RTAX_ADVMSS, rt->rt_mtu - 40);
591
592 if (rt->rt_flags & RTF_WINDOW)
593 len = put_rtax(mx, len, RTAX_WINDOW, rt->rt_window);
594
595 if (rt->rt_flags & RTF_IRTT)
596 len = put_rtax(mx, len, RTAX_RTT, rt->rt_irtt << 3);
597
598 cfg->fc_mx = mx;
599 cfg->fc_mx_len = len;
600 }
601
602 return 0;
603}
604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605/*
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000606 * Handle IP routing ioctl calls.
607 * These are used to manipulate the routing tables
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 */
Al Viroca25c302017-07-01 08:03:10 -0400609int ip_rt_ioctl(struct net *net, unsigned int cmd, struct rtentry *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
Thomas Graf4e902c52006-08-17 18:14:52 -0700611 struct fib_config cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 switch (cmd) {
615 case SIOCADDRT: /* Add a route */
616 case SIOCDELRT: /* Delete a route */
Eric W. Biederman52e804c2012-11-16 03:03:05 +0000617 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 return -EPERM;
Thomas Graf4e902c52006-08-17 18:14:52 -0700619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 rtnl_lock();
Al Viroca25c302017-07-01 08:03:10 -0400621 err = rtentry_to_fib_config(net, cmd, rt, &cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 if (err == 0) {
Thomas Graf4e902c52006-08-17 18:14:52 -0700623 struct fib_table *tb;
624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 if (cmd == SIOCDELRT) {
Denis V. Lunev1bad1182008-01-10 03:29:53 -0800626 tb = fib_get_table(net, cfg.fc_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 if (tb)
David Ahern78055992017-05-27 16:19:26 -0600628 err = fib_table_delete(net, tb, &cfg,
629 NULL);
Thomas Graf4e902c52006-08-17 18:14:52 -0700630 else
631 err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 } else {
Denis V. Lunev1bad1182008-01-10 03:29:53 -0800633 tb = fib_new_table(net, cfg.fc_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 if (tb)
David Ahern6d8422a12017-05-21 10:12:02 -0600635 err = fib_table_insert(net, tb,
636 &cfg, NULL);
Thomas Graf4e902c52006-08-17 18:14:52 -0700637 else
638 err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 }
Thomas Graf4e902c52006-08-17 18:14:52 -0700640
641 /* allocated by rtentry_to_fib_config() */
642 kfree(cfg.fc_mx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 }
644 rtnl_unlock();
645 return err;
646 }
647 return -EINVAL;
648}
649
Eric Dumazet6a31d2a2010-10-04 20:00:18 +0000650const struct nla_policy rtm_ipv4_policy[RTA_MAX + 1] = {
David Ahern75425652019-05-22 12:07:43 -0700651 [RTA_UNSPEC] = { .strict_start_type = RTA_DPORT + 1 },
Thomas Graf4e902c52006-08-17 18:14:52 -0700652 [RTA_DST] = { .type = NLA_U32 },
653 [RTA_SRC] = { .type = NLA_U32 },
654 [RTA_IIF] = { .type = NLA_U32 },
655 [RTA_OIF] = { .type = NLA_U32 },
656 [RTA_GATEWAY] = { .type = NLA_U32 },
657 [RTA_PRIORITY] = { .type = NLA_U32 },
658 [RTA_PREFSRC] = { .type = NLA_U32 },
659 [RTA_METRICS] = { .type = NLA_NESTED },
Thomas Graf5176f912006-08-26 20:13:18 -0700660 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
Thomas Graf4e902c52006-08-17 18:14:52 -0700661 [RTA_FLOW] = { .type = NLA_U32 },
Roopa Prabhu571e7222015-07-21 10:43:47 +0200662 [RTA_ENCAP_TYPE] = { .type = NLA_U16 },
663 [RTA_ENCAP] = { .type = NLA_NESTED },
Lorenzo Colitti622ec2c2016-11-04 02:23:42 +0900664 [RTA_UID] = { .type = NLA_U32 },
Liping Zhang3b45a412017-02-27 20:59:39 +0800665 [RTA_MARK] = { .type = NLA_U32 },
Roopa Prabhu2eabd762018-05-22 13:44:51 -0700666 [RTA_TABLE] = { .type = NLA_U32 },
Roopa Prabhu404eb772018-05-22 14:03:27 -0700667 [RTA_IP_PROTO] = { .type = NLA_U8 },
668 [RTA_SPORT] = { .type = NLA_U16 },
669 [RTA_DPORT] = { .type = NLA_U16 },
David Ahern493ced12019-06-08 14:53:32 -0700670 [RTA_NH_ID] = { .type = NLA_U32 },
Thomas Graf4e902c52006-08-17 18:14:52 -0700671};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
David Ahernd1566262019-04-05 16:30:40 -0700673int fib_gw_from_via(struct fib_config *cfg, struct nlattr *nla,
674 struct netlink_ext_ack *extack)
675{
676 struct rtvia *via;
677 int alen;
678
679 if (nla_len(nla) < offsetof(struct rtvia, rtvia_addr)) {
680 NL_SET_ERR_MSG(extack, "Invalid attribute length for RTA_VIA");
681 return -EINVAL;
682 }
683
684 via = nla_data(nla);
685 alen = nla_len(nla) - offsetof(struct rtvia, rtvia_addr);
686
687 switch (via->rtvia_family) {
688 case AF_INET:
689 if (alen != sizeof(__be32)) {
690 NL_SET_ERR_MSG(extack, "Invalid IPv4 address in RTA_VIA");
691 return -EINVAL;
692 }
693 cfg->fc_gw_family = AF_INET;
694 cfg->fc_gw4 = *((__be32 *)via->rtvia_addr);
695 break;
696 case AF_INET6:
697#ifdef CONFIG_IPV6
698 if (alen != sizeof(struct in6_addr)) {
699 NL_SET_ERR_MSG(extack, "Invalid IPv6 address in RTA_VIA");
700 return -EINVAL;
701 }
702 cfg->fc_gw_family = AF_INET6;
703 cfg->fc_gw6 = *((struct in6_addr *)via->rtvia_addr);
704#else
705 NL_SET_ERR_MSG(extack, "IPv6 support not enabled in kernel");
706 return -EINVAL;
707#endif
708 break;
709 default:
710 NL_SET_ERR_MSG(extack, "Unsupported address family in RTA_VIA");
711 return -EINVAL;
712 }
713
714 return 0;
715}
716
Denis V. Lunev4b5d47d2008-01-10 03:29:23 -0800717static int rtm_to_fib_config(struct net *net, struct sk_buff *skb,
David Ahern6d8422a12017-05-21 10:12:02 -0600718 struct nlmsghdr *nlh, struct fib_config *cfg,
719 struct netlink_ext_ack *extack)
Thomas Graf4e902c52006-08-17 18:14:52 -0700720{
David Ahernd1566262019-04-05 16:30:40 -0700721 bool has_gw = false, has_via = false;
Thomas Graf4e902c52006-08-17 18:14:52 -0700722 struct nlattr *attr;
723 int err, remaining;
724 struct rtmsg *rtm;
725
Johannes Berg8cb08172019-04-26 14:07:28 +0200726 err = nlmsg_validate_deprecated(nlh, sizeof(*rtm), RTA_MAX,
727 rtm_ipv4_policy, extack);
Thomas Graf4e902c52006-08-17 18:14:52 -0700728 if (err < 0)
729 goto errout;
730
731 memset(cfg, 0, sizeof(*cfg));
732
733 rtm = nlmsg_data(nlh);
Thomas Graf4e902c52006-08-17 18:14:52 -0700734 cfg->fc_dst_len = rtm->rtm_dst_len;
Thomas Graf4e902c52006-08-17 18:14:52 -0700735 cfg->fc_tos = rtm->rtm_tos;
736 cfg->fc_table = rtm->rtm_table;
737 cfg->fc_protocol = rtm->rtm_protocol;
738 cfg->fc_scope = rtm->rtm_scope;
739 cfg->fc_type = rtm->rtm_type;
740 cfg->fc_flags = rtm->rtm_flags;
741 cfg->fc_nlflags = nlh->nlmsg_flags;
742
Eric W. Biederman15e47302012-09-07 20:12:54 +0000743 cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid;
Thomas Graf4e902c52006-08-17 18:14:52 -0700744 cfg->fc_nlinfo.nlh = nlh;
Denis V. Lunev4b5d47d2008-01-10 03:29:23 -0800745 cfg->fc_nlinfo.nl_net = net;
Thomas Graf4e902c52006-08-17 18:14:52 -0700746
Thomas Grafa0ee18b2007-03-24 20:32:54 -0700747 if (cfg->fc_type > RTN_MAX) {
David Ahernc3ab2b42017-05-21 10:12:03 -0600748 NL_SET_ERR_MSG(extack, "Invalid route type");
Thomas Grafa0ee18b2007-03-24 20:32:54 -0700749 err = -EINVAL;
750 goto errout;
751 }
752
Thomas Graf4e902c52006-08-17 18:14:52 -0700753 nlmsg_for_each_attr(attr, nlh, sizeof(struct rtmsg), remaining) {
Thomas Graf8f4c1f92007-09-12 14:44:36 +0200754 switch (nla_type(attr)) {
Thomas Graf4e902c52006-08-17 18:14:52 -0700755 case RTA_DST:
Al Viro17fb2c62006-09-26 22:15:25 -0700756 cfg->fc_dst = nla_get_be32(attr);
Thomas Graf4e902c52006-08-17 18:14:52 -0700757 break;
Thomas Graf4e902c52006-08-17 18:14:52 -0700758 case RTA_OIF:
759 cfg->fc_oif = nla_get_u32(attr);
760 break;
761 case RTA_GATEWAY:
David Ahernd1566262019-04-05 16:30:40 -0700762 has_gw = true;
David Ahernf35b7942019-04-05 16:30:28 -0700763 cfg->fc_gw4 = nla_get_be32(attr);
David Ahernd73f80f2019-04-10 10:05:51 -0700764 if (cfg->fc_gw4)
765 cfg->fc_gw_family = AF_INET;
Thomas Graf4e902c52006-08-17 18:14:52 -0700766 break;
David Ahernb6e9e5d2019-02-26 09:00:02 -0800767 case RTA_VIA:
David Ahernd1566262019-04-05 16:30:40 -0700768 has_via = true;
769 err = fib_gw_from_via(cfg, attr, extack);
770 if (err)
771 goto errout;
772 break;
Thomas Graf4e902c52006-08-17 18:14:52 -0700773 case RTA_PRIORITY:
774 cfg->fc_priority = nla_get_u32(attr);
775 break;
776 case RTA_PREFSRC:
Al Viro17fb2c62006-09-26 22:15:25 -0700777 cfg->fc_prefsrc = nla_get_be32(attr);
Thomas Graf4e902c52006-08-17 18:14:52 -0700778 break;
779 case RTA_METRICS:
780 cfg->fc_mx = nla_data(attr);
781 cfg->fc_mx_len = nla_len(attr);
782 break;
783 case RTA_MULTIPATH:
David Ahern9ed59592017-01-17 14:57:36 -0800784 err = lwtunnel_valid_encap_type_attr(nla_data(attr),
David Ahernc255bd62017-05-27 16:19:27 -0600785 nla_len(attr),
786 extack);
David Ahern9ed59592017-01-17 14:57:36 -0800787 if (err < 0)
788 goto errout;
Thomas Graf4e902c52006-08-17 18:14:52 -0700789 cfg->fc_mp = nla_data(attr);
790 cfg->fc_mp_len = nla_len(attr);
791 break;
792 case RTA_FLOW:
793 cfg->fc_flow = nla_get_u32(attr);
794 break;
Thomas Graf4e902c52006-08-17 18:14:52 -0700795 case RTA_TABLE:
796 cfg->fc_table = nla_get_u32(attr);
797 break;
Roopa Prabhu571e7222015-07-21 10:43:47 +0200798 case RTA_ENCAP:
799 cfg->fc_encap = attr;
800 break;
801 case RTA_ENCAP_TYPE:
802 cfg->fc_encap_type = nla_get_u16(attr);
David Ahernc255bd62017-05-27 16:19:27 -0600803 err = lwtunnel_valid_encap_type(cfg->fc_encap_type,
804 extack);
David Ahern9ed59592017-01-17 14:57:36 -0800805 if (err < 0)
806 goto errout;
Roopa Prabhu571e7222015-07-21 10:43:47 +0200807 break;
David Ahern493ced12019-06-08 14:53:32 -0700808 case RTA_NH_ID:
809 cfg->fc_nh_id = nla_get_u32(attr);
810 break;
811 }
812 }
813
814 if (cfg->fc_nh_id) {
815 if (cfg->fc_oif || cfg->fc_gw_family ||
816 cfg->fc_encap || cfg->fc_mp) {
817 NL_SET_ERR_MSG(extack,
818 "Nexthop specification and nexthop id are mutually exclusive");
819 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 }
821 }
Thomas Graf4e902c52006-08-17 18:14:52 -0700822
David Ahernd1566262019-04-05 16:30:40 -0700823 if (has_gw && has_via) {
824 NL_SET_ERR_MSG(extack,
825 "Nexthop configuration can not contain both GATEWAY and VIA");
826 goto errout;
827 }
828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 return 0;
Thomas Graf4e902c52006-08-17 18:14:52 -0700830errout:
831 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832}
833
David Ahernc21ef3e2017-04-16 09:48:24 -0700834static int inet_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
835 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900837 struct net *net = sock_net(skb->sk);
Thomas Graf4e902c52006-08-17 18:14:52 -0700838 struct fib_config cfg;
839 struct fib_table *tb;
840 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
David Ahern6d8422a12017-05-21 10:12:02 -0600842 err = rtm_to_fib_config(net, skb, nlh, &cfg, extack);
Thomas Graf4e902c52006-08-17 18:14:52 -0700843 if (err < 0)
844 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
David Ahern493ced12019-06-08 14:53:32 -0700846 if (cfg.fc_nh_id && !nexthop_find_by_id(net, cfg.fc_nh_id)) {
847 NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
848 err = -EINVAL;
849 goto errout;
850 }
851
Denis V. Lunev8ad49422008-01-10 03:24:11 -0800852 tb = fib_get_table(net, cfg.fc_table);
Ian Morris51456b22015-04-03 09:17:26 +0100853 if (!tb) {
David Ahernc3ab2b42017-05-21 10:12:03 -0600854 NL_SET_ERR_MSG(extack, "FIB table does not exist");
Thomas Graf4e902c52006-08-17 18:14:52 -0700855 err = -ESRCH;
856 goto errout;
857 }
858
David Ahern78055992017-05-27 16:19:26 -0600859 err = fib_table_delete(net, tb, &cfg, extack);
Thomas Graf4e902c52006-08-17 18:14:52 -0700860errout:
861 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862}
863
David Ahernc21ef3e2017-04-16 09:48:24 -0700864static int inet_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
865 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900867 struct net *net = sock_net(skb->sk);
Thomas Graf4e902c52006-08-17 18:14:52 -0700868 struct fib_config cfg;
869 struct fib_table *tb;
870 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
David Ahern6d8422a12017-05-21 10:12:02 -0600872 err = rtm_to_fib_config(net, skb, nlh, &cfg, extack);
Thomas Graf4e902c52006-08-17 18:14:52 -0700873 if (err < 0)
874 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Denis V. Lunev226b0b4a52008-01-10 03:30:24 -0800876 tb = fib_new_table(net, cfg.fc_table);
Ian Morris51456b22015-04-03 09:17:26 +0100877 if (!tb) {
Thomas Graf4e902c52006-08-17 18:14:52 -0700878 err = -ENOBUFS;
879 goto errout;
880 }
881
David Ahern6d8422a12017-05-21 10:12:02 -0600882 err = fib_table_insert(net, tb, &cfg, extack);
Paolo Abeni6e617de2017-09-20 18:26:53 +0200883 if (!err && cfg.fc_type == RTN_LOCAL)
884 net->ipv4.fib_has_custom_local_routes = true;
Thomas Graf4e902c52006-08-17 18:14:52 -0700885errout:
886 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887}
888
David Ahern47246762018-10-15 18:56:42 -0700889int ip_valid_fib_dump_req(struct net *net, const struct nlmsghdr *nlh,
890 struct fib_dump_filter *filter,
David Aherneffe6792018-10-15 18:56:48 -0700891 struct netlink_callback *cb)
David Aherne8ba3302018-10-07 20:16:35 -0700892{
David Aherneffe6792018-10-15 18:56:48 -0700893 struct netlink_ext_ack *extack = cb->extack;
894 struct nlattr *tb[RTA_MAX + 1];
David Aherne8ba3302018-10-07 20:16:35 -0700895 struct rtmsg *rtm;
David Aherneffe6792018-10-15 18:56:48 -0700896 int err, i;
897
898 ASSERT_RTNL();
David Aherne8ba3302018-10-07 20:16:35 -0700899
900 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
901 NL_SET_ERR_MSG(extack, "Invalid header for FIB dump request");
902 return -EINVAL;
903 }
904
905 rtm = nlmsg_data(nlh);
906 if (rtm->rtm_dst_len || rtm->rtm_src_len || rtm->rtm_tos ||
David Aherneffe6792018-10-15 18:56:48 -0700907 rtm->rtm_scope) {
David Aherne8ba3302018-10-07 20:16:35 -0700908 NL_SET_ERR_MSG(extack, "Invalid values in header for FIB dump request");
909 return -EINVAL;
910 }
Stefano Brivio564c91f2019-06-21 17:45:20 +0200911
David Aherne8ba3302018-10-07 20:16:35 -0700912 if (rtm->rtm_flags & ~(RTM_F_CLONED | RTM_F_PREFIX)) {
913 NL_SET_ERR_MSG(extack, "Invalid flags for FIB dump request");
914 return -EINVAL;
915 }
Stefano Brivio564c91f2019-06-21 17:45:20 +0200916 if (rtm->rtm_flags & RTM_F_CLONED)
917 filter->dump_routes = false;
918 else
919 filter->dump_exceptions = false;
David Aherne8ba3302018-10-07 20:16:35 -0700920
David Ahernae677bb2018-10-24 12:59:01 -0700921 filter->dump_all_families = (rtm->rtm_family == AF_UNSPEC);
David Aherneffe6792018-10-15 18:56:48 -0700922 filter->flags = rtm->rtm_flags;
923 filter->protocol = rtm->rtm_protocol;
924 filter->rt_type = rtm->rtm_type;
925 filter->table_id = rtm->rtm_table;
926
Johannes Berg8cb08172019-04-26 14:07:28 +0200927 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
928 rtm_ipv4_policy, extack);
David Aherneffe6792018-10-15 18:56:48 -0700929 if (err < 0)
930 return err;
931
932 for (i = 0; i <= RTA_MAX; ++i) {
933 int ifindex;
934
935 if (!tb[i])
936 continue;
937
938 switch (i) {
939 case RTA_TABLE:
940 filter->table_id = nla_get_u32(tb[i]);
941 break;
942 case RTA_OIF:
943 ifindex = nla_get_u32(tb[i]);
944 filter->dev = __dev_get_by_index(net, ifindex);
945 if (!filter->dev)
946 return -ENODEV;
947 break;
948 default:
949 NL_SET_ERR_MSG(extack, "Unsupported attribute in dump request");
950 return -EINVAL;
951 }
952 }
953
954 if (filter->flags || filter->protocol || filter->rt_type ||
955 filter->table_id || filter->dev) {
956 filter->filter_set = 1;
957 cb->answer_flags = NLM_F_DUMP_FILTERED;
David Aherne8ba3302018-10-07 20:16:35 -0700958 }
959
960 return 0;
961}
962EXPORT_SYMBOL_GPL(ip_valid_fib_dump_req);
963
Thomas Graf63f34442007-03-22 11:55:17 -0700964static int inet_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965{
Stefano Brivio564c91f2019-06-21 17:45:20 +0200966 struct fib_dump_filter filter = { .dump_routes = true,
967 .dump_exceptions = true };
David Aherne8ba3302018-10-07 20:16:35 -0700968 const struct nlmsghdr *nlh = cb->nlh;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900969 struct net *net = sock_net(skb->sk);
Patrick McHardy1af5a8c2006-08-10 23:10:46 -0700970 unsigned int h, s_h;
971 unsigned int e = 0, s_e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 struct fib_table *tb;
Denis V. Luneve4aef8a2008-01-10 03:28:24 -0800973 struct hlist_head *head;
David Ahernf6c57752017-05-15 23:19:17 -0700974 int dumped = 0, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
David Aherne8ba3302018-10-07 20:16:35 -0700976 if (cb->strict_check) {
David Aherneffe6792018-10-15 18:56:48 -0700977 err = ip_valid_fib_dump_req(net, nlh, &filter, cb);
David Aherne8ba3302018-10-07 20:16:35 -0700978 if (err < 0)
979 return err;
David Aherne4e92fb2018-10-15 18:56:51 -0700980 } else if (nlmsg_len(nlh) >= sizeof(struct rtmsg)) {
981 struct rtmsg *rtm = nlmsg_data(nlh);
982
983 filter.flags = rtm->rtm_flags & (RTM_F_PREFIX | RTM_F_CLONED);
David Aherne8ba3302018-10-07 20:16:35 -0700984 }
985
Stefano Briviob597ca6e2019-06-21 17:45:21 +0200986 /* ipv4 does not use prefix flag */
987 if (filter.flags & RTM_F_PREFIX)
Li RongQing0b8c7f62014-03-21 11:33:10 +0800988 return skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
David Ahern18a80212018-10-15 18:56:43 -0700990 if (filter.table_id) {
991 tb = fib_get_table(net, filter.table_id);
992 if (!tb) {
David Ahernae677bb2018-10-24 12:59:01 -0700993 if (filter.dump_all_families)
994 return skb->len;
995
David Ahern18a80212018-10-15 18:56:43 -0700996 NL_SET_ERR_MSG(cb->extack, "ipv4: FIB table does not exist");
997 return -ENOENT;
998 }
999
Qian Caidddeb302020-03-19 22:54:21 -04001000 rcu_read_lock();
David Ahern18a80212018-10-15 18:56:43 -07001001 err = fib_table_dump(tb, skb, cb, &filter);
Qian Caidddeb302020-03-19 22:54:21 -04001002 rcu_read_unlock();
David Ahern18a80212018-10-15 18:56:43 -07001003 return skb->len ? : err;
1004 }
1005
Patrick McHardy1af5a8c2006-08-10 23:10:46 -07001006 s_h = cb->args[0];
1007 s_e = cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Alexander Duycka7e53532015-03-04 15:02:44 -08001009 rcu_read_lock();
1010
Patrick McHardy1af5a8c2006-08-10 23:10:46 -07001011 for (h = s_h; h < FIB_TABLE_HASHSZ; h++, s_e = 0) {
1012 e = 0;
Denis V. Luneve4aef8a2008-01-10 03:28:24 -08001013 head = &net->ipv4.fib_table_hash[h];
Alexander Duycka7e53532015-03-04 15:02:44 -08001014 hlist_for_each_entry_rcu(tb, head, tb_hlist) {
Patrick McHardy1af5a8c2006-08-10 23:10:46 -07001015 if (e < s_e)
1016 goto next;
1017 if (dumped)
1018 memset(&cb->args[2], 0, sizeof(cb->args) -
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001019 2 * sizeof(cb->args[0]));
David Ahern18a80212018-10-15 18:56:43 -07001020 err = fib_table_dump(tb, skb, cb, &filter);
David Ahernf6c57752017-05-15 23:19:17 -07001021 if (err < 0) {
1022 if (likely(skb->len))
1023 goto out;
1024
1025 goto out_err;
1026 }
Patrick McHardy1af5a8c2006-08-10 23:10:46 -07001027 dumped = 1;
1028next:
1029 e++;
1030 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 }
Patrick McHardy1af5a8c2006-08-10 23:10:46 -07001032out:
David Ahernf6c57752017-05-15 23:19:17 -07001033 err = skb->len;
1034out_err:
Alexander Duycka7e53532015-03-04 15:02:44 -08001035 rcu_read_unlock();
1036
Patrick McHardy1af5a8c2006-08-10 23:10:46 -07001037 cb->args[1] = e;
1038 cb->args[0] = h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
David Ahernf6c57752017-05-15 23:19:17 -07001040 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041}
1042
1043/* Prepare and feed intra-kernel routing request.
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001044 * Really, it should be netlink message, but :-( netlink
1045 * can be not configured, so that we feed it directly
1046 * to fib engine. It is legal, because all events occur
1047 * only when netlink is already locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 */
David Ahernaf4d7682018-05-27 08:09:57 -07001049static void fib_magic(int cmd, int type, __be32 dst, int dst_len,
1050 struct in_ifaddr *ifa, u32 rt_priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001052 struct net *net = dev_net(ifa->ifa_dev->dev);
David Ahern3236b002015-09-29 20:07:14 -07001053 u32 tb_id = l3mdev_fib_table(ifa->ifa_dev->dev);
Thomas Graf4e902c52006-08-17 18:14:52 -07001054 struct fib_table *tb;
1055 struct fib_config cfg = {
1056 .fc_protocol = RTPROT_KERNEL,
1057 .fc_type = type,
1058 .fc_dst = dst,
1059 .fc_dst_len = dst_len,
David Ahernaf4d7682018-05-27 08:09:57 -07001060 .fc_priority = rt_priority,
Thomas Graf4e902c52006-08-17 18:14:52 -07001061 .fc_prefsrc = ifa->ifa_local,
1062 .fc_oif = ifa->ifa_dev->dev->ifindex,
1063 .fc_nlflags = NLM_F_CREATE | NLM_F_APPEND,
Denis V. Lunev4d1169c2008-01-10 03:26:13 -08001064 .fc_nlinfo = {
Denis V. Lunev4b5d47d2008-01-10 03:29:23 -08001065 .nl_net = net,
Denis V. Lunev4d1169c2008-01-10 03:26:13 -08001066 },
Thomas Graf4e902c52006-08-17 18:14:52 -07001067 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
David Ahern021dd3b2015-08-13 14:59:06 -06001069 if (!tb_id)
1070 tb_id = (type == RTN_UNICAST) ? RT_TABLE_MAIN : RT_TABLE_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
David Ahern021dd3b2015-08-13 14:59:06 -06001072 tb = fib_new_table(net, tb_id);
Ian Morris51456b22015-04-03 09:17:26 +01001073 if (!tb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 return;
1075
Thomas Graf4e902c52006-08-17 18:14:52 -07001076 cfg.fc_table = tb->tb_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Thomas Graf4e902c52006-08-17 18:14:52 -07001078 if (type != RTN_LOCAL)
1079 cfg.fc_scope = RT_SCOPE_LINK;
1080 else
1081 cfg.fc_scope = RT_SCOPE_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
1083 if (cmd == RTM_NEWROUTE)
David Ahern6d8422a12017-05-21 10:12:02 -06001084 fib_table_insert(net, tb, &cfg, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 else
David Ahern78055992017-05-27 16:19:26 -06001086 fib_table_delete(net, tb, &cfg, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087}
1088
Jamal Hadi Salim0ff60a42005-11-22 14:47:37 -08001089void fib_add_ifaddr(struct in_ifaddr *ifa)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
1091 struct in_device *in_dev = ifa->ifa_dev;
1092 struct net_device *dev = in_dev->dev;
1093 struct in_ifaddr *prim = ifa;
Al Viroa144ea42006-09-28 18:00:55 -07001094 __be32 mask = ifa->ifa_mask;
1095 __be32 addr = ifa->ifa_local;
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001096 __be32 prefix = ifa->ifa_address & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001098 if (ifa->ifa_flags & IFA_F_SECONDARY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 prim = inet_ifa_byprefix(in_dev, prefix, mask);
Ian Morris51456b22015-04-03 09:17:26 +01001100 if (!prim) {
Joe Perches058bd4d2012-03-11 18:36:11 +00001101 pr_warn("%s: bug: prim == NULL\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 return;
1103 }
1104 }
1105
David Ahernaf4d7682018-05-27 08:09:57 -07001106 fib_magic(RTM_NEWROUTE, RTN_LOCAL, addr, 32, prim, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001108 if (!(dev->flags & IFF_UP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 return;
1110
1111 /* Add broadcast address, if it is explicitly assigned. */
Al Viroa144ea42006-09-28 18:00:55 -07001112 if (ifa->ifa_broadcast && ifa->ifa_broadcast != htonl(0xFFFFFFFF))
David Ahernaf4d7682018-05-27 08:09:57 -07001113 fib_magic(RTM_NEWROUTE, RTN_BROADCAST, ifa->ifa_broadcast, 32,
1114 prim, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001116 if (!ipv4_is_zeronet(prefix) && !(ifa->ifa_flags & IFA_F_SECONDARY) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 (prefix != addr || ifa->ifa_prefixlen < 32)) {
Paolo Abeni7b131182015-10-20 10:28:45 +02001118 if (!(ifa->ifa_flags & IFA_F_NOPREFIXROUTE))
1119 fib_magic(RTM_NEWROUTE,
1120 dev->flags & IFF_LOOPBACK ? RTN_LOCAL : RTN_UNICAST,
David Ahernaf4d7682018-05-27 08:09:57 -07001121 prefix, ifa->ifa_prefixlen, prim,
1122 ifa->ifa_rt_priority);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
1124 /* Add network specific broadcasts, when it takes a sense */
1125 if (ifa->ifa_prefixlen < 31) {
David Ahernaf4d7682018-05-27 08:09:57 -07001126 fib_magic(RTM_NEWROUTE, RTN_BROADCAST, prefix, 32,
1127 prim, 0);
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001128 fib_magic(RTM_NEWROUTE, RTN_BROADCAST, prefix | ~mask,
David Ahernaf4d7682018-05-27 08:09:57 -07001129 32, prim, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 }
1131 }
1132}
1133
David Ahernaf4d7682018-05-27 08:09:57 -07001134void fib_modify_prefix_metric(struct in_ifaddr *ifa, u32 new_metric)
1135{
1136 __be32 prefix = ifa->ifa_address & ifa->ifa_mask;
1137 struct in_device *in_dev = ifa->ifa_dev;
1138 struct net_device *dev = in_dev->dev;
1139
1140 if (!(dev->flags & IFF_UP) ||
1141 ifa->ifa_flags & (IFA_F_SECONDARY | IFA_F_NOPREFIXROUTE) ||
1142 ipv4_is_zeronet(prefix) ||
Paolo Abeni0b834ba2019-10-26 11:53:39 +02001143 (prefix == ifa->ifa_local && ifa->ifa_prefixlen == 32))
David Ahernaf4d7682018-05-27 08:09:57 -07001144 return;
1145
1146 /* add the new */
1147 fib_magic(RTM_NEWROUTE,
1148 dev->flags & IFF_LOOPBACK ? RTN_LOCAL : RTN_UNICAST,
1149 prefix, ifa->ifa_prefixlen, ifa, new_metric);
1150
1151 /* delete the old */
1152 fib_magic(RTM_DELROUTE,
1153 dev->flags & IFF_LOOPBACK ? RTN_LOCAL : RTN_UNICAST,
1154 prefix, ifa->ifa_prefixlen, ifa, ifa->ifa_rt_priority);
1155}
1156
Julian Anastasove6abbaa2011-03-19 12:13:49 +00001157/* Delete primary or secondary address.
1158 * Optionally, on secondary address promotion consider the addresses
1159 * from subnet iprim as deleted, even if they are in device list.
1160 * In this case the secondary ifa can be in device list.
1161 */
1162void fib_del_ifaddr(struct in_ifaddr *ifa, struct in_ifaddr *iprim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163{
1164 struct in_device *in_dev = ifa->ifa_dev;
1165 struct net_device *dev = in_dev->dev;
1166 struct in_ifaddr *ifa1;
Julian Anastasove6abbaa2011-03-19 12:13:49 +00001167 struct in_ifaddr *prim = ifa, *prim1 = NULL;
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001168 __be32 brd = ifa->ifa_address | ~ifa->ifa_mask;
1169 __be32 any = ifa->ifa_address & ifa->ifa_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170#define LOCAL_OK 1
1171#define BRD_OK 2
1172#define BRD0_OK 4
1173#define BRD1_OK 8
Eric Dumazet95c96172012-04-15 05:58:06 +00001174 unsigned int ok = 0;
Julian Anastasove6abbaa2011-03-19 12:13:49 +00001175 int subnet = 0; /* Primary network */
1176 int gone = 1; /* Address is missing */
1177 int same_prefsrc = 0; /* Another primary with same IP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
Julian Anastasove6abbaa2011-03-19 12:13:49 +00001179 if (ifa->ifa_flags & IFA_F_SECONDARY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 prim = inet_ifa_byprefix(in_dev, any, ifa->ifa_mask);
Ian Morris51456b22015-04-03 09:17:26 +01001181 if (!prim) {
Paolo Abeni391a2032016-04-21 22:23:31 +02001182 /* if the device has been deleted, we don't perform
1183 * address promotion
1184 */
1185 if (!in_dev->dead)
1186 pr_warn("%s: bug: prim == NULL\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 return;
1188 }
Julian Anastasove6abbaa2011-03-19 12:13:49 +00001189 if (iprim && iprim != prim) {
Joe Perches058bd4d2012-03-11 18:36:11 +00001190 pr_warn("%s: bug: iprim != prim\n", __func__);
Julian Anastasove6abbaa2011-03-19 12:13:49 +00001191 return;
1192 }
1193 } else if (!ipv4_is_zeronet(any) &&
1194 (any != ifa->ifa_local || ifa->ifa_prefixlen < 32)) {
Paolo Abeni7b131182015-10-20 10:28:45 +02001195 if (!(ifa->ifa_flags & IFA_F_NOPREFIXROUTE))
1196 fib_magic(RTM_DELROUTE,
1197 dev->flags & IFF_LOOPBACK ? RTN_LOCAL : RTN_UNICAST,
David Ahernaf4d7682018-05-27 08:09:57 -07001198 any, ifa->ifa_prefixlen, prim, 0);
Julian Anastasove6abbaa2011-03-19 12:13:49 +00001199 subnet = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 }
1201
David S. Millerfbd40ea2016-03-13 23:28:00 -04001202 if (in_dev->dead)
1203 goto no_promotions;
1204
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 /* Deletion is more complicated than add.
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001206 * We should take care of not to delete too much :-)
1207 *
1208 * Scan address list to be sure that addresses are really gone.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 */
Florian Westphalcd5a4112019-05-31 18:27:07 +02001210 rcu_read_lock();
1211 in_dev_for_each_ifa_rcu(ifa1, in_dev) {
Julian Anastasove6abbaa2011-03-19 12:13:49 +00001212 if (ifa1 == ifa) {
1213 /* promotion, keep the IP */
1214 gone = 0;
1215 continue;
1216 }
1217 /* Ignore IFAs from our subnet */
1218 if (iprim && ifa1->ifa_mask == iprim->ifa_mask &&
1219 inet_ifa_match(ifa1->ifa_address, iprim))
1220 continue;
1221
1222 /* Ignore ifa1 if it uses different primary IP (prefsrc) */
1223 if (ifa1->ifa_flags & IFA_F_SECONDARY) {
1224 /* Another address from our subnet? */
1225 if (ifa1->ifa_mask == prim->ifa_mask &&
1226 inet_ifa_match(ifa1->ifa_address, prim))
1227 prim1 = prim;
1228 else {
1229 /* We reached the secondaries, so
1230 * same_prefsrc should be determined.
1231 */
1232 if (!same_prefsrc)
1233 continue;
1234 /* Search new prim1 if ifa1 is not
1235 * using the current prim1
1236 */
1237 if (!prim1 ||
1238 ifa1->ifa_mask != prim1->ifa_mask ||
1239 !inet_ifa_match(ifa1->ifa_address, prim1))
1240 prim1 = inet_ifa_byprefix(in_dev,
1241 ifa1->ifa_address,
1242 ifa1->ifa_mask);
1243 if (!prim1)
1244 continue;
1245 if (prim1->ifa_local != prim->ifa_local)
1246 continue;
1247 }
1248 } else {
1249 if (prim->ifa_local != ifa1->ifa_local)
1250 continue;
1251 prim1 = ifa1;
1252 if (prim != prim1)
1253 same_prefsrc = 1;
1254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 if (ifa->ifa_local == ifa1->ifa_local)
1256 ok |= LOCAL_OK;
1257 if (ifa->ifa_broadcast == ifa1->ifa_broadcast)
1258 ok |= BRD_OK;
1259 if (brd == ifa1->ifa_broadcast)
1260 ok |= BRD1_OK;
1261 if (any == ifa1->ifa_broadcast)
1262 ok |= BRD0_OK;
Julian Anastasove6abbaa2011-03-19 12:13:49 +00001263 /* primary has network specific broadcasts */
1264 if (prim1 == ifa1 && ifa1->ifa_prefixlen < 31) {
1265 __be32 brd1 = ifa1->ifa_address | ~ifa1->ifa_mask;
1266 __be32 any1 = ifa1->ifa_address & ifa1->ifa_mask;
1267
1268 if (!ipv4_is_zeronet(any1)) {
1269 if (ifa->ifa_broadcast == brd1 ||
1270 ifa->ifa_broadcast == any1)
1271 ok |= BRD_OK;
1272 if (brd == brd1 || brd == any1)
1273 ok |= BRD1_OK;
1274 if (any == brd1 || any == any1)
1275 ok |= BRD0_OK;
1276 }
1277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 }
Florian Westphalcd5a4112019-05-31 18:27:07 +02001279 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
David S. Millerfbd40ea2016-03-13 23:28:00 -04001281no_promotions:
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001282 if (!(ok & BRD_OK))
David Ahernaf4d7682018-05-27 08:09:57 -07001283 fib_magic(RTM_DELROUTE, RTN_BROADCAST, ifa->ifa_broadcast, 32,
1284 prim, 0);
Julian Anastasove6abbaa2011-03-19 12:13:49 +00001285 if (subnet && ifa->ifa_prefixlen < 31) {
1286 if (!(ok & BRD1_OK))
David Ahernaf4d7682018-05-27 08:09:57 -07001287 fib_magic(RTM_DELROUTE, RTN_BROADCAST, brd, 32,
1288 prim, 0);
Julian Anastasove6abbaa2011-03-19 12:13:49 +00001289 if (!(ok & BRD0_OK))
David Ahernaf4d7682018-05-27 08:09:57 -07001290 fib_magic(RTM_DELROUTE, RTN_BROADCAST, any, 32,
1291 prim, 0);
Julian Anastasove6abbaa2011-03-19 12:13:49 +00001292 }
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001293 if (!(ok & LOCAL_OK)) {
David Ahern30bbaa12015-08-13 14:59:05 -06001294 unsigned int addr_type;
1295
David Ahernaf4d7682018-05-27 08:09:57 -07001296 fib_magic(RTM_DELROUTE, RTN_LOCAL, ifa->ifa_local, 32, prim, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
1298 /* Check, that this local address finally disappeared. */
David Ahern30bbaa12015-08-13 14:59:05 -06001299 addr_type = inet_addr_type_dev_table(dev_net(dev), dev,
1300 ifa->ifa_local);
1301 if (gone && addr_type != RTN_LOCAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 /* And the last, but not the least thing.
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001303 * We must flush stray FIB entries.
1304 *
1305 * First of all, we scan fib_info list searching
1306 * for stray nexthop entries, then ignite fib_flush.
1307 */
Mark Tomlinson5a56a0b2016-09-05 10:20:20 +12001308 if (fib_sync_down_addr(dev, ifa->ifa_local))
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001309 fib_flush(dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 }
1311 }
1312#undef LOCAL_OK
1313#undef BRD_OK
1314#undef BRD0_OK
1315#undef BRD1_OK
1316}
1317
Alexander Duyck345e9b52014-12-31 10:56:24 -08001318static void nl_fib_lookup(struct net *net, struct fib_result_nl *frn)
Robert Olsson246955f2005-06-20 13:36:39 -07001319{
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001320
Robert Olsson246955f2005-06-20 13:36:39 -07001321 struct fib_result res;
David S. Miller9ade2282011-03-12 02:02:42 -05001322 struct flowi4 fl4 = {
1323 .flowi4_mark = frn->fl_mark,
1324 .daddr = frn->fl_addr,
1325 .flowi4_tos = frn->fl_tos,
1326 .flowi4_scope = frn->fl_scope,
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001327 };
Alexander Duyck345e9b52014-12-31 10:56:24 -08001328 struct fib_table *tb;
1329
1330 rcu_read_lock();
1331
1332 tb = fib_get_table(net, frn->tb_id_in);
Alexey Kuznetsov1194ed02007-04-25 13:07:28 -07001333
1334 frn->err = -ENOENT;
Robert Olsson246955f2005-06-20 13:36:39 -07001335 if (tb) {
1336 local_bh_disable();
1337
1338 frn->tb_id = tb->tb_id;
David S. Miller9ade2282011-03-12 02:02:42 -05001339 frn->err = fib_table_lookup(tb, &fl4, &res, FIB_LOOKUP_NOREF);
Robert Olsson246955f2005-06-20 13:36:39 -07001340
1341 if (!frn->err) {
1342 frn->prefixlen = res.prefixlen;
1343 frn->nh_sel = res.nh_sel;
1344 frn->type = res.type;
1345 frn->scope = res.scope;
1346 }
1347 local_bh_enable();
1348 }
Alexander Duyck345e9b52014-12-31 10:56:24 -08001349
1350 rcu_read_unlock();
Robert Olsson246955f2005-06-20 13:36:39 -07001351}
1352
David S. Miller28f7b0362007-10-10 21:32:39 -07001353static void nl_fib_input(struct sk_buff *skb)
Robert Olsson246955f2005-06-20 13:36:39 -07001354{
Denis V. Lunev6bd48fc2008-01-10 03:28:55 -08001355 struct net *net;
Robert Olsson246955f2005-06-20 13:36:39 -07001356 struct fib_result_nl *frn;
David S. Miller28f7b0362007-10-10 21:32:39 -07001357 struct nlmsghdr *nlh;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001358 u32 portid;
Alexey Kuznetsov1194ed02007-04-25 13:07:28 -07001359
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001360 net = sock_net(skb->sk);
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -07001361 nlh = nlmsg_hdr(skb);
Eric Dumazetc64c0b32017-03-21 19:22:28 -07001362 if (skb->len < nlmsg_total_size(sizeof(*frn)) ||
1363 skb->len < nlh->nlmsg_len ||
Hong zhi guo573ce262013-03-27 06:47:04 +00001364 nlmsg_len(nlh) < sizeof(*frn))
Thomas Grafea865752005-12-01 14:30:00 -08001365 return;
Denis V. Lunevd883a032007-12-21 02:01:53 -08001366
Pablo Neira3a365152013-06-28 03:04:23 +02001367 skb = netlink_skb_clone(skb, GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +01001368 if (!skb)
Denis V. Lunevd883a032007-12-21 02:01:53 -08001369 return;
1370 nlh = nlmsg_hdr(skb);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001371
Hong zhi guo573ce262013-03-27 06:47:04 +00001372 frn = (struct fib_result_nl *) nlmsg_data(nlh);
Alexander Duyck345e9b52014-12-31 10:56:24 -08001373 nl_fib_lookup(net, frn);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001374
Rami Rosen28a28282013-01-11 09:59:20 +00001375 portid = NETLINK_CB(skb).portid; /* netlink portid */
Eric W. Biederman15e47302012-09-07 20:12:54 +00001376 NETLINK_CB(skb).portid = 0; /* from kernel */
Patrick McHardyac6d4392005-08-14 19:29:52 -07001377 NETLINK_CB(skb).dst_group = 0; /* unicast */
Eric W. Biederman15e47302012-09-07 20:12:54 +00001378 netlink_unicast(net->ipv4.fibnl, skb, portid, MSG_DONTWAIT);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001379}
Robert Olsson246955f2005-06-20 13:36:39 -07001380
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001381static int __net_init nl_fib_lookup_init(struct net *net)
Robert Olsson246955f2005-06-20 13:36:39 -07001382{
Denis V. Lunev6bd48fc2008-01-10 03:28:55 -08001383 struct sock *sk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00001384 struct netlink_kernel_cfg cfg = {
1385 .input = nl_fib_input,
1386 };
1387
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00001388 sk = netlink_kernel_create(net, NETLINK_FIB_LOOKUP, &cfg);
Ian Morris51456b22015-04-03 09:17:26 +01001389 if (!sk)
Denis V. Lunev7b1a74fd2008-01-10 03:22:17 -08001390 return -EAFNOSUPPORT;
Denis V. Lunev6bd48fc2008-01-10 03:28:55 -08001391 net->ipv4.fibnl = sk;
Denis V. Lunev7b1a74fd2008-01-10 03:22:17 -08001392 return 0;
1393}
1394
1395static void nl_fib_lookup_exit(struct net *net)
1396{
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08001397 netlink_kernel_release(net->ipv4.fibnl);
Denis V. Lunev775516b2008-01-18 23:55:19 -08001398 net->ipv4.fibnl = NULL;
Robert Olsson246955f2005-06-20 13:36:39 -07001399}
1400
Julian Anastasov4f823de2015-10-30 10:23:33 +02001401static void fib_disable_ip(struct net_device *dev, unsigned long event,
1402 bool force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403{
Julian Anastasov4f823de2015-10-30 10:23:33 +02001404 if (fib_sync_down_dev(dev, event, force))
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001405 fib_flush(dev_net(dev));
Gao Feng06b4fc52017-04-26 19:04:04 +08001406 else
1407 rt_cache_flush(dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 arp_ifdown(dev);
1409}
1410
1411static int fib_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
1412{
Jianjun Kong6ed2533e2008-11-03 00:25:16 -08001413 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
Denis V. Lunev76e6ebf2008-07-05 19:00:44 -07001414 struct net_device *dev = ifa->ifa_dev->dev;
David S. Miller436c3b62011-03-24 17:42:21 -07001415 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
1417 switch (event) {
1418 case NETDEV_UP:
1419 fib_add_ifaddr(ifa);
1420#ifdef CONFIG_IP_ROUTE_MULTIPATH
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001421 fib_sync_up(dev, RTNH_F_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422#endif
David S. Miller436c3b62011-03-24 17:42:21 -07001423 atomic_inc(&net->ipv4.dev_addr_genid);
Nicolas Dichtel4ccfe6d2012-09-07 00:45:29 +00001424 rt_cache_flush(dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 break;
1426 case NETDEV_DOWN:
Julian Anastasove6abbaa2011-03-19 12:13:49 +00001427 fib_del_ifaddr(ifa, NULL);
David S. Miller436c3b62011-03-24 17:42:21 -07001428 atomic_inc(&net->ipv4.dev_addr_genid);
Ian Morris51456b22015-04-03 09:17:26 +01001429 if (!ifa->ifa_dev->ifa_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 /* Last address was deleted from this interface.
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00001431 * Disable IP.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 */
Julian Anastasov4f823de2015-10-30 10:23:33 +02001433 fib_disable_ip(dev, event, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 } else {
Nicolas Dichtel4ccfe6d2012-09-07 00:45:29 +00001435 rt_cache_flush(dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 }
1437 break;
1438 }
1439 return NOTIFY_DONE;
1440}
1441
1442static int fib_netdev_event(struct notifier_block *this, unsigned long event, void *ptr)
1443{
Jiri Pirko351638e2013-05-28 01:30:21 +00001444 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02001445 struct netdev_notifier_changeupper_info *upper_info = ptr;
1446 struct netdev_notifier_info_ext *info_ext = ptr;
Eric Dumazet0115e8e2012-08-22 17:19:46 +00001447 struct in_device *in_dev;
David S. Miller436c3b62011-03-24 17:42:21 -07001448 struct net *net = dev_net(dev);
Florian Westphalcd5a4112019-05-31 18:27:07 +02001449 struct in_ifaddr *ifa;
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001450 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
1452 if (event == NETDEV_UNREGISTER) {
Julian Anastasov4f823de2015-10-30 10:23:33 +02001453 fib_disable_ip(dev, event, true);
David S. Millercaacf052012-07-31 15:06:50 -07001454 rt_flush_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 return NOTIFY_DONE;
1456 }
1457
Eric Dumazet0115e8e2012-08-22 17:19:46 +00001458 in_dev = __in_dev_get_rtnl(dev);
Oliver Hartkoppa0065f22014-01-23 10:19:34 +01001459 if (!in_dev)
1460 return NOTIFY_DONE;
Eric Dumazet0115e8e2012-08-22 17:19:46 +00001461
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 switch (event) {
1463 case NETDEV_UP:
Florian Westphalcd5a4112019-05-31 18:27:07 +02001464 in_dev_for_each_ifa_rtnl(ifa, in_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 fib_add_ifaddr(ifa);
Florian Westphalcd5a4112019-05-31 18:27:07 +02001466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467#ifdef CONFIG_IP_ROUTE_MULTIPATH
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001468 fib_sync_up(dev, RTNH_F_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469#endif
David S. Miller436c3b62011-03-24 17:42:21 -07001470 atomic_inc(&net->ipv4.dev_addr_genid);
Nicolas Dichtel4ccfe6d2012-09-07 00:45:29 +00001471 rt_cache_flush(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 break;
1473 case NETDEV_DOWN:
Julian Anastasov4f823de2015-10-30 10:23:33 +02001474 fib_disable_ip(dev, event, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 case NETDEV_CHANGE:
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001477 flags = dev_get_flags(dev);
1478 if (flags & (IFF_RUNNING | IFF_LOWER_UP))
1479 fib_sync_up(dev, RTNH_F_LINKDOWN);
1480 else
Julian Anastasov4f823de2015-10-30 10:23:33 +02001481 fib_sync_down_dev(dev, event, false);
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02001482 rt_cache_flush(net);
1483 break;
Andy Gospodarek8a3d0312015-06-23 13:45:36 -04001484 case NETDEV_CHANGEMTU:
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02001485 fib_sync_mtu(dev, info_ext->ext.mtu);
Nicolas Dichtel4ccfe6d2012-09-07 00:45:29 +00001486 rt_cache_flush(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 break;
David Ahern7f49e7a2015-12-10 10:25:24 -08001488 case NETDEV_CHANGEUPPER:
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02001489 upper_info = ptr;
David Ahern7f49e7a2015-12-10 10:25:24 -08001490 /* flush all routes if dev is linked to or unlinked from
1491 * an L3 master device (e.g., VRF)
1492 */
Sabrina Dubrocaaf7d6cc2018-10-09 17:48:14 +02001493 if (upper_info->upper_dev &&
1494 netif_is_l3_master(upper_info->upper_dev))
David Ahern7f49e7a2015-12-10 10:25:24 -08001495 fib_disable_ip(dev, NETDEV_DOWN, true);
1496 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 }
1498 return NOTIFY_DONE;
1499}
1500
1501static struct notifier_block fib_inetaddr_notifier = {
Jianjun Kong6ed2533e2008-11-03 00:25:16 -08001502 .notifier_call = fib_inetaddr_event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503};
1504
1505static struct notifier_block fib_netdev_notifier = {
Jianjun Kong6ed2533e2008-11-03 00:25:16 -08001506 .notifier_call = fib_netdev_event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507};
1508
Denis V. Lunev7b1a74fd2008-01-10 03:22:17 -08001509static int __net_init ip_fib_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510{
Denis V. Lunevdce5cbe2008-01-31 18:44:53 -08001511 int err;
Eric Dumazet10da66f2010-10-13 08:22:03 +00001512 size_t size = sizeof(struct hlist_head) * FIB_TABLE_HASHSZ;
Patrick McHardy1af5a8c2006-08-10 23:10:46 -07001513
Ido Schimmel04b1d4e2017-08-03 13:28:11 +02001514 err = fib4_notifier_init(net);
1515 if (err)
1516 return err;
Ido Schimmelcacaad12016-12-03 16:45:06 +01001517
Eric Dumazet10da66f2010-10-13 08:22:03 +00001518 /* Avoid false sharing : Use at least a full cache line */
1519 size = max_t(size_t, size, L1_CACHE_BYTES);
1520
1521 net->ipv4.fib_table_hash = kzalloc(size, GFP_KERNEL);
Ido Schimmel04b1d4e2017-08-03 13:28:11 +02001522 if (!net->ipv4.fib_table_hash) {
1523 err = -ENOMEM;
1524 goto err_table_hash_alloc;
1525 }
Denis V. Luneve4aef8a2008-01-10 03:28:24 -08001526
Denis V. Lunevdce5cbe2008-01-31 18:44:53 -08001527 err = fib4_rules_init(net);
1528 if (err < 0)
Ido Schimmel04b1d4e2017-08-03 13:28:11 +02001529 goto err_rules_init;
Denis V. Lunevdce5cbe2008-01-31 18:44:53 -08001530 return 0;
1531
Ido Schimmel04b1d4e2017-08-03 13:28:11 +02001532err_rules_init:
Denis V. Lunevdce5cbe2008-01-31 18:44:53 -08001533 kfree(net->ipv4.fib_table_hash);
Ido Schimmel04b1d4e2017-08-03 13:28:11 +02001534err_table_hash_alloc:
1535 fib4_notifier_exit(net);
Denis V. Lunevdce5cbe2008-01-31 18:44:53 -08001536 return err;
Denis V. Lunev7b1a74fd2008-01-10 03:22:17 -08001537}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001539static void ip_fib_net_exit(struct net *net)
Denis V. Lunev7b1a74fd2008-01-10 03:22:17 -08001540{
Ido Schimmelb4681c22017-12-20 19:34:19 +02001541 int i;
Thomas Graf63f34442007-03-22 11:55:17 -07001542
Sabrina Dubroca6dede752015-03-10 21:03:54 +01001543 rtnl_lock();
Alexander Duyck6e47d6c2015-03-27 14:14:22 -07001544#ifdef CONFIG_IP_MULTIPLE_TABLES
Alexander Duyck6e47d6c2015-03-27 14:14:22 -07001545 RCU_INIT_POINTER(net->ipv4.fib_main, NULL);
1546 RCU_INIT_POINTER(net->ipv4.fib_default, NULL);
1547#endif
Ido Schimmelb4681c22017-12-20 19:34:19 +02001548 /* Destroy the tables in reverse order to guarantee that the
1549 * local table, ID 255, is destroyed before the main table, ID
1550 * 254. This is necessary as the local table may contain
1551 * references to data contained in the main table.
1552 */
1553 for (i = FIB_TABLE_HASHSZ - 1; i >= 0; i--) {
Alexander Duycka7e53532015-03-04 15:02:44 -08001554 struct hlist_head *head = &net->ipv4.fib_table_hash[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001555 struct hlist_node *tmp;
Alexander Duycka7e53532015-03-04 15:02:44 -08001556 struct fib_table *tb;
Denis V. Lunev7b1a74fd2008-01-10 03:22:17 -08001557
Alexander Duycka7e53532015-03-04 15:02:44 -08001558 hlist_for_each_entry_safe(tb, tmp, head, tb_hlist) {
Alexander Duycka7e53532015-03-04 15:02:44 -08001559 hlist_del(&tb->tb_hlist);
Ido Schimmelf97f4dd2019-01-09 09:57:39 +00001560 fib_table_flush(net, tb, true);
Pavel Emelyanov4aa2c462010-10-28 02:00:43 +00001561 fib_free_table(tb);
Denis V. Lunev7b1a74fd2008-01-10 03:22:17 -08001562 }
1563 }
Alexander Duyckad88d052015-03-27 14:14:16 -07001564
1565#ifdef CONFIG_IP_MULTIPLE_TABLES
1566 fib4_rules_exit(net);
1567#endif
Eric Dumazete2666f82011-03-30 16:57:46 -07001568 rtnl_unlock();
Denis V. Luneve4aef8a2008-01-10 03:28:24 -08001569 kfree(net->ipv4.fib_table_hash);
Ido Schimmel04b1d4e2017-08-03 13:28:11 +02001570 fib4_notifier_exit(net);
Denis V. Lunev7b1a74fd2008-01-10 03:22:17 -08001571}
1572
1573static int __net_init fib_net_init(struct net *net)
1574{
1575 int error;
1576
David S. Millerf4530fa2012-07-05 22:13:13 -07001577#ifdef CONFIG_IP_ROUTE_CLASSID
1578 net->ipv4.fib_num_tclassid_users = 0;
1579#endif
Denis V. Lunev7b1a74fd2008-01-10 03:22:17 -08001580 error = ip_fib_net_init(net);
1581 if (error < 0)
1582 goto out;
1583 error = nl_fib_lookup_init(net);
1584 if (error < 0)
1585 goto out_nlfl;
1586 error = fib_proc_init(net);
1587 if (error < 0)
1588 goto out_proc;
1589out:
1590 return error;
1591
1592out_proc:
1593 nl_fib_lookup_exit(net);
1594out_nlfl:
1595 ip_fib_net_exit(net);
1596 goto out;
1597}
1598
1599static void __net_exit fib_net_exit(struct net *net)
1600{
1601 fib_proc_exit(net);
1602 nl_fib_lookup_exit(net);
1603 ip_fib_net_exit(net);
1604}
1605
1606static struct pernet_operations fib_net_ops = {
1607 .init = fib_net_init,
1608 .exit = fib_net_exit,
1609};
1610
1611void __init ip_fib_init(void)
1612{
Mahesh Bandewar8799a222017-07-19 15:41:33 -07001613 fib_trie_init();
Denis V. Lunev7b1a74fd2008-01-10 03:22:17 -08001614
1615 register_pernet_subsys(&fib_net_ops);
Mahesh Bandewar8799a222017-07-19 15:41:33 -07001616
Denis V. Lunev7b1a74fd2008-01-10 03:22:17 -08001617 register_netdevice_notifier(&fib_netdev_notifier);
1618 register_inetaddr_notifier(&fib_inetaddr_notifier);
Stephen Hemminger7f9b8052008-01-14 23:14:20 -08001619
Florian Westphalb97bac62017-08-09 20:41:48 +02001620 rtnl_register(PF_INET, RTM_NEWROUTE, inet_rtm_newroute, NULL, 0);
1621 rtnl_register(PF_INET, RTM_DELROUTE, inet_rtm_delroute, NULL, 0);
1622 rtnl_register(PF_INET, RTM_GETROUTE, NULL, inet_dump_fib, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623}