Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/module.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 13 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/bitops.h> |
Randy Dunlap | 4fc268d | 2006-01-11 12:17:47 -0800 | [diff] [blame] | 15 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/types.h> |
| 17 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #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 Melo | 14c8502 | 2005-12-27 02:43:12 -0200 | [diff] [blame] | 25 | #include <linux/inetdevice.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/netdevice.h> |
Thomas Graf | 1823730 | 2006-08-04 23:04:54 -0700 | [diff] [blame] | 27 | #include <linux/if_addr.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/if_arp.h> |
| 29 | #include <linux/skbuff.h> |
David S. Miller | 7a9bc9b | 2012-06-29 01:32:45 -0700 | [diff] [blame] | 30 | #include <linux/cache.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <linux/init.h> |
Patrick McHardy | 1af5a8c | 2006-08-10 23:10:46 -0700 | [diff] [blame] | 32 | #include <linux/list.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 33 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #include <net/arp.h> |
| 41 | #include <net/ip_fib.h> |
David Ahern | 5481d73 | 2019-06-03 20:19:49 -0700 | [diff] [blame] | 42 | #include <net/nexthop.h> |
Thomas Graf | 63f3444 | 2007-03-22 11:55:17 -0700 | [diff] [blame] | 43 | #include <net/rtnetlink.h> |
Michael Smith | 990078a | 2011-04-07 04:51:51 +0000 | [diff] [blame] | 44 | #include <net/xfrm.h> |
David Ahern | 385add9 | 2015-09-29 20:07:13 -0700 | [diff] [blame] | 45 | #include <net/l3mdev.h> |
David Ahern | 9ed5959 | 2017-01-17 14:57:36 -0800 | [diff] [blame] | 46 | #include <net/lwtunnel.h> |
David Ahern | f6d3c19 | 2015-08-28 08:42:09 -0700 | [diff] [blame] | 47 | #include <trace/events/fib.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | #ifndef CONFIG_IP_MULTIPLE_TABLES |
| 50 | |
Denis V. Lunev | 7b1a74fd | 2008-01-10 03:22:17 -0800 | [diff] [blame] | 51 | static int __net_init fib4_rules_init(struct net *net) |
Pavel Emelyanov | c3e9a35 | 2007-11-06 23:34:04 -0800 | [diff] [blame] | 52 | { |
Denis V. Lunev | 93456b6 | 2008-01-10 03:23:38 -0800 | [diff] [blame] | 53 | struct fib_table *local_table, *main_table; |
| 54 | |
Alexander Duyck | 0ddcf43 | 2015-03-06 13:47:00 -0800 | [diff] [blame] | 55 | main_table = fib_trie_table(RT_TABLE_MAIN, NULL); |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 56 | if (!main_table) |
Alexander Duyck | 61f0d86 | 2015-03-11 14:02:16 -0700 | [diff] [blame] | 57 | return -ENOMEM; |
Denis V. Lunev | dbb5016 | 2008-01-10 03:21:49 -0800 | [diff] [blame] | 58 | |
Alexander Duyck | 0ddcf43 | 2015-03-06 13:47:00 -0800 | [diff] [blame] | 59 | local_table = fib_trie_table(RT_TABLE_LOCAL, main_table); |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 60 | if (!local_table) |
Alexander Duyck | 61f0d86 | 2015-03-11 14:02:16 -0700 | [diff] [blame] | 61 | goto fail; |
Alexander Duyck | 0ddcf43 | 2015-03-06 13:47:00 -0800 | [diff] [blame] | 62 | |
Denis V. Lunev | 93456b6 | 2008-01-10 03:23:38 -0800 | [diff] [blame] | 63 | hlist_add_head_rcu(&local_table->tb_hlist, |
Denis V. Lunev | e4aef8a | 2008-01-10 03:28:24 -0800 | [diff] [blame] | 64 | &net->ipv4.fib_table_hash[TABLE_LOCAL_INDEX]); |
Denis V. Lunev | 93456b6 | 2008-01-10 03:23:38 -0800 | [diff] [blame] | 65 | hlist_add_head_rcu(&main_table->tb_hlist, |
Denis V. Lunev | e4aef8a | 2008-01-10 03:28:24 -0800 | [diff] [blame] | 66 | &net->ipv4.fib_table_hash[TABLE_MAIN_INDEX]); |
Denis V. Lunev | dbb5016 | 2008-01-10 03:21:49 -0800 | [diff] [blame] | 67 | return 0; |
| 68 | |
| 69 | fail: |
Alexander Duyck | 61f0d86 | 2015-03-11 14:02:16 -0700 | [diff] [blame] | 70 | fib_free_table(main_table); |
Denis V. Lunev | dbb5016 | 2008-01-10 03:21:49 -0800 | [diff] [blame] | 71 | return -ENOMEM; |
Pavel Emelyanov | c3e9a35 | 2007-11-06 23:34:04 -0800 | [diff] [blame] | 72 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | #else |
| 74 | |
Denis V. Lunev | 8ad4942 | 2008-01-10 03:24:11 -0800 | [diff] [blame] | 75 | struct fib_table *fib_new_table(struct net *net, u32 id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | { |
Alexander Duyck | 0ddcf43 | 2015-03-06 13:47:00 -0800 | [diff] [blame] | 77 | struct fib_table *tb, *alias = NULL; |
Patrick McHardy | 1af5a8c | 2006-08-10 23:10:46 -0700 | [diff] [blame] | 78 | unsigned int h; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
Patrick McHardy | 1af5a8c | 2006-08-10 23:10:46 -0700 | [diff] [blame] | 80 | if (id == 0) |
| 81 | id = RT_TABLE_MAIN; |
Denis V. Lunev | 8ad4942 | 2008-01-10 03:24:11 -0800 | [diff] [blame] | 82 | tb = fib_get_table(net, id); |
Patrick McHardy | 1af5a8c | 2006-08-10 23:10:46 -0700 | [diff] [blame] | 83 | if (tb) |
| 84 | return tb; |
Stephen Hemminger | 7f9b805 | 2008-01-14 23:14:20 -0800 | [diff] [blame] | 85 | |
Alexander Duyck | 5350d54 | 2017-01-02 13:32:54 -0800 | [diff] [blame] | 86 | if (id == RT_TABLE_LOCAL && !net->ipv4.fib_has_custom_rules) |
Alexander Duyck | 0ddcf43 | 2015-03-06 13:47:00 -0800 | [diff] [blame] | 87 | alias = fib_new_table(net, RT_TABLE_MAIN); |
| 88 | |
| 89 | tb = fib_trie_table(id, alias); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | if (!tb) |
| 91 | return NULL; |
David S. Miller | f4530fa | 2012-07-05 22:13:13 -0700 | [diff] [blame] | 92 | |
| 93 | switch (id) { |
David S. Miller | f4530fa | 2012-07-05 22:13:13 -0700 | [diff] [blame] | 94 | case RT_TABLE_MAIN: |
Alexander Duyck | a7e5353 | 2015-03-04 15:02:44 -0800 | [diff] [blame] | 95 | rcu_assign_pointer(net->ipv4.fib_main, tb); |
David S. Miller | f4530fa | 2012-07-05 22:13:13 -0700 | [diff] [blame] | 96 | break; |
David S. Miller | f4530fa | 2012-07-05 22:13:13 -0700 | [diff] [blame] | 97 | case RT_TABLE_DEFAULT: |
Alexander Duyck | a7e5353 | 2015-03-04 15:02:44 -0800 | [diff] [blame] | 98 | rcu_assign_pointer(net->ipv4.fib_default, tb); |
David S. Miller | f4530fa | 2012-07-05 22:13:13 -0700 | [diff] [blame] | 99 | break; |
David S. Miller | f4530fa | 2012-07-05 22:13:13 -0700 | [diff] [blame] | 100 | default: |
| 101 | break; |
| 102 | } |
| 103 | |
Patrick McHardy | 1af5a8c | 2006-08-10 23:10:46 -0700 | [diff] [blame] | 104 | h = id & (FIB_TABLE_HASHSZ - 1); |
Denis V. Lunev | e4aef8a | 2008-01-10 03:28:24 -0800 | [diff] [blame] | 105 | hlist_add_head_rcu(&tb->tb_hlist, &net->ipv4.fib_table_hash[h]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | return tb; |
| 107 | } |
David Ahern | b3b4663 | 2016-05-04 21:46:12 -0700 | [diff] [blame] | 108 | EXPORT_SYMBOL_GPL(fib_new_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
Alexander Duyck | 345e9b5 | 2014-12-31 10:56:24 -0800 | [diff] [blame] | 110 | /* caller must hold either rtnl or rcu read lock */ |
Denis V. Lunev | 8ad4942 | 2008-01-10 03:24:11 -0800 | [diff] [blame] | 111 | struct fib_table *fib_get_table(struct net *net, u32 id) |
Patrick McHardy | 1af5a8c | 2006-08-10 23:10:46 -0700 | [diff] [blame] | 112 | { |
| 113 | struct fib_table *tb; |
Denis V. Lunev | e4aef8a | 2008-01-10 03:28:24 -0800 | [diff] [blame] | 114 | struct hlist_head *head; |
Patrick McHardy | 1af5a8c | 2006-08-10 23:10:46 -0700 | [diff] [blame] | 115 | unsigned int h; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | |
Patrick McHardy | 1af5a8c | 2006-08-10 23:10:46 -0700 | [diff] [blame] | 117 | if (id == 0) |
| 118 | id = RT_TABLE_MAIN; |
| 119 | h = id & (FIB_TABLE_HASHSZ - 1); |
Denis V. Lunev | e4aef8a | 2008-01-10 03:28:24 -0800 | [diff] [blame] | 120 | |
Denis V. Lunev | e4aef8a | 2008-01-10 03:28:24 -0800 | [diff] [blame] | 121 | head = &net->ipv4.fib_table_hash[h]; |
Joel Fernandes (Google) | 7fd69b0 | 2019-07-16 18:12:24 -0400 | [diff] [blame] | 122 | hlist_for_each_entry_rcu(tb, head, tb_hlist, |
| 123 | lockdep_rtnl_is_held()) { |
Alexander Duyck | 345e9b5 | 2014-12-31 10:56:24 -0800 | [diff] [blame] | 124 | if (tb->tb_id == id) |
Patrick McHardy | 1af5a8c | 2006-08-10 23:10:46 -0700 | [diff] [blame] | 125 | return tb; |
Patrick McHardy | 1af5a8c | 2006-08-10 23:10:46 -0700 | [diff] [blame] | 126 | } |
Patrick McHardy | 1af5a8c | 2006-08-10 23:10:46 -0700 | [diff] [blame] | 127 | return NULL; |
| 128 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | #endif /* CONFIG_IP_MULTIPLE_TABLES */ |
| 130 | |
Alexander Duyck | 0ddcf43 | 2015-03-06 13:47:00 -0800 | [diff] [blame] | 131 | static 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 Duyck | 0ddcf43 | 2015-03-06 13:47:00 -0800 | [diff] [blame] | 136 | 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 | |
| 151 | int fib_unmerge(struct net *net) |
| 152 | { |
Alexander Duyck | 3b70933 | 2016-11-15 05:46:06 -0500 | [diff] [blame] | 153 | struct fib_table *old, *new, *main_table; |
Alexander Duyck | 0ddcf43 | 2015-03-06 13:47:00 -0800 | [diff] [blame] | 154 | |
Alexander Duyck | 3c9e9f7 | 2015-03-12 14:46:23 -0700 | [diff] [blame] | 155 | /* attempt to fetch local table if it has been allocated */ |
Alexander Duyck | 0ddcf43 | 2015-03-06 13:47:00 -0800 | [diff] [blame] | 156 | old = fib_get_table(net, RT_TABLE_LOCAL); |
Alexander Duyck | 3c9e9f7 | 2015-03-12 14:46:23 -0700 | [diff] [blame] | 157 | if (!old) |
| 158 | return 0; |
Alexander Duyck | 0ddcf43 | 2015-03-06 13:47:00 -0800 | [diff] [blame] | 159 | |
Alexander Duyck | 3c9e9f7 | 2015-03-12 14:46:23 -0700 | [diff] [blame] | 160 | new = fib_trie_unmerge(old); |
Alexander Duyck | 0ddcf43 | 2015-03-06 13:47:00 -0800 | [diff] [blame] | 161 | if (!new) |
| 162 | return -ENOMEM; |
| 163 | |
Alexander Duyck | 3b70933 | 2016-11-15 05:46:06 -0500 | [diff] [blame] | 164 | /* table is already unmerged */ |
| 165 | if (new == old) |
| 166 | return 0; |
| 167 | |
Alexander Duyck | 0ddcf43 | 2015-03-06 13:47:00 -0800 | [diff] [blame] | 168 | /* replace merged table with clean table */ |
Alexander Duyck | 3b70933 | 2016-11-15 05:46:06 -0500 | [diff] [blame] | 169 | 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 Duyck | 0ddcf43 | 2015-03-06 13:47:00 -0800 | [diff] [blame] | 179 | |
| 180 | return 0; |
| 181 | } |
| 182 | |
David Ahern | 9bd8366 | 2019-05-22 12:04:44 -0700 | [diff] [blame] | 183 | void fib_flush(struct net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | { |
| 185 | int flushed = 0; |
Patrick McHardy | 1af5a8c | 2006-08-10 23:10:46 -0700 | [diff] [blame] | 186 | unsigned int h; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | |
Patrick McHardy | 1af5a8c | 2006-08-10 23:10:46 -0700 | [diff] [blame] | 188 | for (h = 0; h < FIB_TABLE_HASHSZ; h++) { |
Alexander Duyck | a7e5353 | 2015-03-04 15:02:44 -0800 | [diff] [blame] | 189 | 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 Schimmel | f97f4dd | 2019-01-09 09:57:39 +0000 | [diff] [blame] | 194 | flushed += fib_table_flush(net, tb, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | |
| 197 | if (flushed) |
Nicolas Dichtel | 4ccfe6d | 2012-09-07 00:45:29 +0000 | [diff] [blame] | 198 | rt_cache_flush(net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Laszlo Attila Toth | 05538116 | 2007-12-04 23:28:46 -0800 | [diff] [blame] | 201 | /* |
| 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 Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 205 | static inline unsigned int __inet_dev_addr_type(struct net *net, |
| 206 | const struct net_device *dev, |
David Ahern | 9b8ff51 | 2015-09-01 14:26:35 -0600 | [diff] [blame] | 207 | __be32 addr, u32 tb_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | { |
David S. Miller | 9ade228 | 2011-03-12 02:02:42 -0500 | [diff] [blame] | 209 | struct flowi4 fl4 = { .daddr = addr }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | struct fib_result res; |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 211 | unsigned int ret = RTN_BROADCAST; |
David Ahern | 15be405e | 2015-08-13 14:59:04 -0600 | [diff] [blame] | 212 | struct fib_table *table; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | |
Jan Engelhardt | 1e637c7 | 2008-01-21 03:18:08 -0800 | [diff] [blame] | 214 | if (ipv4_is_zeronet(addr) || ipv4_is_lbcast(addr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | return RTN_BROADCAST; |
Joe Perches | f97c1e0 | 2007-12-16 13:45:43 -0800 | [diff] [blame] | 216 | if (ipv4_is_multicast(addr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | return RTN_MULTICAST; |
| 218 | |
Alexander Duyck | 345e9b5 | 2014-12-31 10:56:24 -0800 | [diff] [blame] | 219 | rcu_read_lock(); |
| 220 | |
David Ahern | 15be405e | 2015-08-13 14:59:04 -0600 | [diff] [blame] | 221 | table = fib_get_table(net, tb_id); |
| 222 | if (table) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | ret = RTN_UNICAST; |
David Ahern | 15be405e | 2015-08-13 14:59:04 -0600 | [diff] [blame] | 224 | if (!fib_table_lookup(table, &fl4, &res, FIB_LOOKUP_NOREF)) { |
David Ahern | dcb1ecb | 2019-06-03 20:19:50 -0700 | [diff] [blame] | 225 | struct fib_nh_common *nhc = fib_info_nhc(res.fi, 0); |
David Ahern | 5481d73 | 2019-06-03 20:19:49 -0700 | [diff] [blame] | 226 | |
David Ahern | dcb1ecb | 2019-06-03 20:19:50 -0700 | [diff] [blame] | 227 | if (!dev || dev == nhc->nhc_dev) |
Laszlo Attila Toth | 05538116 | 2007-12-04 23:28:46 -0800 | [diff] [blame] | 228 | ret = res.type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | } |
| 230 | } |
Alexander Duyck | 345e9b5 | 2014-12-31 10:56:24 -0800 | [diff] [blame] | 231 | |
| 232 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | return ret; |
| 234 | } |
| 235 | |
David Ahern | 9b8ff51 | 2015-09-01 14:26:35 -0600 | [diff] [blame] | 236 | unsigned int inet_addr_type_table(struct net *net, __be32 addr, u32 tb_id) |
David Ahern | 15be405e | 2015-08-13 14:59:04 -0600 | [diff] [blame] | 237 | { |
| 238 | return __inet_dev_addr_type(net, NULL, addr, tb_id); |
| 239 | } |
| 240 | EXPORT_SYMBOL(inet_addr_type_table); |
| 241 | |
Eric W. Biederman | 6b175b2 | 2008-01-10 03:25:28 -0800 | [diff] [blame] | 242 | unsigned int inet_addr_type(struct net *net, __be32 addr) |
Laszlo Attila Toth | 05538116 | 2007-12-04 23:28:46 -0800 | [diff] [blame] | 243 | { |
David Ahern | 15be405e | 2015-08-13 14:59:04 -0600 | [diff] [blame] | 244 | return __inet_dev_addr_type(net, NULL, addr, RT_TABLE_LOCAL); |
Laszlo Attila Toth | 05538116 | 2007-12-04 23:28:46 -0800 | [diff] [blame] | 245 | } |
Eric Dumazet | 4bc2f18 | 2010-07-09 21:22:10 +0000 | [diff] [blame] | 246 | EXPORT_SYMBOL(inet_addr_type); |
Laszlo Attila Toth | 05538116 | 2007-12-04 23:28:46 -0800 | [diff] [blame] | 247 | |
Eric W. Biederman | 6b175b2 | 2008-01-10 03:25:28 -0800 | [diff] [blame] | 248 | unsigned int inet_dev_addr_type(struct net *net, const struct net_device *dev, |
| 249 | __be32 addr) |
Laszlo Attila Toth | 05538116 | 2007-12-04 23:28:46 -0800 | [diff] [blame] | 250 | { |
David Ahern | 3236b00 | 2015-09-29 20:07:14 -0700 | [diff] [blame] | 251 | u32 rt_table = l3mdev_fib_table(dev) ? : RT_TABLE_LOCAL; |
David Ahern | 15be405e | 2015-08-13 14:59:04 -0600 | [diff] [blame] | 252 | |
| 253 | return __inet_dev_addr_type(net, dev, addr, rt_table); |
Laszlo Attila Toth | 05538116 | 2007-12-04 23:28:46 -0800 | [diff] [blame] | 254 | } |
Eric Dumazet | 4bc2f18 | 2010-07-09 21:22:10 +0000 | [diff] [blame] | 255 | EXPORT_SYMBOL(inet_dev_addr_type); |
Laszlo Attila Toth | 05538116 | 2007-12-04 23:28:46 -0800 | [diff] [blame] | 256 | |
David Ahern | 30bbaa1 | 2015-08-13 14:59:05 -0600 | [diff] [blame] | 257 | /* inet_addr_type with dev == NULL but using the table from a dev |
| 258 | * if one is associated |
| 259 | */ |
| 260 | unsigned int inet_addr_type_dev_table(struct net *net, |
| 261 | const struct net_device *dev, |
| 262 | __be32 addr) |
| 263 | { |
David Ahern | 3236b00 | 2015-09-29 20:07:14 -0700 | [diff] [blame] | 264 | u32 rt_table = l3mdev_fib_table(dev) ? : RT_TABLE_LOCAL; |
David Ahern | 30bbaa1 | 2015-08-13 14:59:05 -0600 | [diff] [blame] | 265 | |
| 266 | return __inet_dev_addr_type(net, NULL, addr, rt_table); |
| 267 | } |
| 268 | EXPORT_SYMBOL(inet_addr_type_dev_table); |
| 269 | |
David S. Miller | 35ebf65 | 2012-06-28 03:59:11 -0700 | [diff] [blame] | 270 | __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. Miller | a207a4b | 2012-06-28 18:33:24 -0700 | [diff] [blame] | 275 | struct rtable *rt; |
David S. Miller | 35ebf65 | 2012-06-28 03:59:11 -0700 | [diff] [blame] | 276 | struct net *net; |
David S. Miller | a207a4b | 2012-06-28 18:33:24 -0700 | [diff] [blame] | 277 | int scope; |
David S. Miller | 35ebf65 | 2012-06-28 03:59:11 -0700 | [diff] [blame] | 278 | |
David S. Miller | a207a4b | 2012-06-28 18:33:24 -0700 | [diff] [blame] | 279 | rt = skb_rtable(skb); |
Julian Anastasov | 0cc535a | 2012-07-18 21:35:03 +0000 | [diff] [blame] | 280 | if ((rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST | RTCF_LOCAL)) == |
| 281 | RTCF_LOCAL) |
David S. Miller | 35ebf65 | 2012-06-28 03:59:11 -0700 | [diff] [blame] | 282 | return ip_hdr(skb)->daddr; |
| 283 | |
| 284 | in_dev = __in_dev_get_rcu(dev); |
David S. Miller | 35ebf65 | 2012-06-28 03:59:11 -0700 | [diff] [blame] | 285 | |
| 286 | net = dev_net(dev); |
David S. Miller | a207a4b | 2012-06-28 18:33:24 -0700 | [diff] [blame] | 287 | |
| 288 | scope = RT_SCOPE_UNIVERSE; |
| 289 | if (!ipv4_is_zeronet(ip_hdr(skb)->saddr)) { |
Lorenzo Bianconi | 9fc1202 | 2018-07-27 18:15:46 +0200 | [diff] [blame] | 290 | bool vmark = in_dev && IN_DEV_SRC_VMARK(in_dev); |
Lance Richardson | 4cfc86f | 2016-03-22 14:56:57 -0400 | [diff] [blame] | 291 | struct flowi4 fl4 = { |
| 292 | .flowi4_iif = LOOPBACK_IFINDEX, |
David Ahern | e737219 | 2018-07-07 16:15:26 -0700 | [diff] [blame] | 293 | .flowi4_oif = l3mdev_master_ifindex_rcu(dev), |
Lance Richardson | 4cfc86f | 2016-03-22 14:56:57 -0400 | [diff] [blame] | 294 | .daddr = ip_hdr(skb)->saddr, |
| 295 | .flowi4_tos = RT_TOS(ip_hdr(skb)->tos), |
| 296 | .flowi4_scope = scope, |
Lorenzo Bianconi | 9fc1202 | 2018-07-27 18:15:46 +0200 | [diff] [blame] | 297 | .flowi4_mark = vmark ? skb->mark : 0, |
Lance Richardson | 4cfc86f | 2016-03-22 14:56:57 -0400 | [diff] [blame] | 298 | }; |
Andy Gospodarek | 0eeb075 | 2015-06-23 13:45:37 -0400 | [diff] [blame] | 299 | if (!fib_lookup(net, &fl4, &res, 0)) |
David Ahern | eba618a | 2019-04-02 14:11:55 -0700 | [diff] [blame] | 300 | return fib_result_prefsrc(net, &res); |
David S. Miller | a207a4b | 2012-06-28 18:33:24 -0700 | [diff] [blame] | 301 | } else { |
| 302 | scope = RT_SCOPE_LINK; |
| 303 | } |
| 304 | |
| 305 | return inet_select_addr(dev, ip_hdr(skb)->saddr, scope); |
David S. Miller | 35ebf65 | 2012-06-28 03:59:11 -0700 | [diff] [blame] | 306 | } |
| 307 | |
David Ahern | 78f2756 | 2018-09-20 13:50:47 -0700 | [diff] [blame] | 308 | bool fib_info_nh_uses_dev(struct fib_info *fi, const struct net_device *dev) |
| 309 | { |
| 310 | bool dev_match = false; |
Eric Dumazet | 075e264 | 2018-09-21 10:58:07 -0700 | [diff] [blame] | 311 | #ifdef CONFIG_IP_ROUTE_MULTIPATH |
David Ahern | 78f2756 | 2018-09-20 13:50:47 -0700 | [diff] [blame] | 312 | int ret; |
| 313 | |
David Ahern | 5481d73 | 2019-06-03 20:19:49 -0700 | [diff] [blame] | 314 | for (ret = 0; ret < fib_info_num_path(fi); ret++) { |
David Ahern | dcb1ecb | 2019-06-03 20:19:50 -0700 | [diff] [blame] | 315 | const struct fib_nh_common *nhc = fib_info_nhc(fi, ret); |
David Ahern | 78f2756 | 2018-09-20 13:50:47 -0700 | [diff] [blame] | 316 | |
David Ahern | dcb1ecb | 2019-06-03 20:19:50 -0700 | [diff] [blame] | 317 | if (nhc->nhc_dev == dev) { |
David Ahern | 78f2756 | 2018-09-20 13:50:47 -0700 | [diff] [blame] | 318 | dev_match = true; |
| 319 | break; |
David Ahern | dcb1ecb | 2019-06-03 20:19:50 -0700 | [diff] [blame] | 320 | } else if (l3mdev_master_ifindex_rcu(nhc->nhc_dev) == dev->ifindex) { |
David Ahern | 78f2756 | 2018-09-20 13:50:47 -0700 | [diff] [blame] | 321 | dev_match = true; |
| 322 | break; |
| 323 | } |
| 324 | } |
| 325 | #else |
David Ahern | dcb1ecb | 2019-06-03 20:19:50 -0700 | [diff] [blame] | 326 | if (fib_info_nhc(fi, 0)->nhc_dev == dev) |
David Ahern | 78f2756 | 2018-09-20 13:50:47 -0700 | [diff] [blame] | 327 | dev_match = true; |
| 328 | #endif |
| 329 | |
| 330 | return dev_match; |
| 331 | } |
| 332 | EXPORT_SYMBOL_GPL(fib_info_nh_uses_dev); |
| 333 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | /* Given (packet source, input interface) and optional (dst, oif, tos): |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 335 | * - (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 Dumazet | ebc0ffa | 2010-10-05 10:41:36 +0000 | [diff] [blame] | 340 | * called with rcu_read_lock() |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | */ |
David S. Miller | 7a9bc9b | 2012-06-29 01:32:45 -0700 | [diff] [blame] | 342 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | { |
David Ahern | 5a847a6 | 2018-05-16 13:36:40 -0700 | [diff] [blame] | 346 | struct net *net = dev_net(dev); |
| 347 | struct flow_keys flkeys; |
Sébastien Barré | 1dced6a | 2014-08-17 09:19:54 +0200 | [diff] [blame] | 348 | int ret, no_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | struct fib_result res; |
David S. Miller | 9e56e38 | 2012-06-28 18:54:02 -0700 | [diff] [blame] | 350 | struct flowi4 fl4; |
David S. Miller | 9e56e38 | 2012-06-28 18:54:02 -0700 | [diff] [blame] | 351 | bool dev_match; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | |
David S. Miller | 9ade228 | 2011-03-12 02:02:42 -0500 | [diff] [blame] | 353 | fl4.flowi4_oif = 0; |
David Ahern | 385add9 | 2015-09-29 20:07:13 -0700 | [diff] [blame] | 354 | fl4.flowi4_iif = l3mdev_master_ifindex_rcu(dev); |
David Ahern | cd2fbe1 | 2015-08-13 14:59:01 -0600 | [diff] [blame] | 355 | if (!fl4.flowi4_iif) |
| 356 | fl4.flowi4_iif = oif ? : LOOPBACK_IFINDEX; |
David S. Miller | 9ade228 | 2011-03-12 02:02:42 -0500 | [diff] [blame] | 357 | fl4.daddr = src; |
| 358 | fl4.saddr = dst; |
| 359 | fl4.flowi4_tos = tos; |
| 360 | fl4.flowi4_scope = RT_SCOPE_UNIVERSE; |
Thomas Graf | 1b7179d | 2015-07-21 10:43:59 +0200 | [diff] [blame] | 361 | fl4.flowi4_tun_key.tun_id = 0; |
David Ahern | b84f787 | 2015-09-29 19:07:07 -0700 | [diff] [blame] | 362 | fl4.flowi4_flags = 0; |
Julian Anastasov | 8bcfd09 | 2017-02-26 15:50:52 +0200 | [diff] [blame] | 363 | fl4.flowi4_uid = sock_net_uid(net, NULL); |
David S. Miller | cc7e17e | 2011-03-09 20:57:50 -0800 | [diff] [blame] | 364 | |
David S. Miller | 9e56e38 | 2012-06-28 18:54:02 -0700 | [diff] [blame] | 365 | no_addr = idev->ifa_list == NULL; |
Michael Smith | 990078a | 2011-04-07 04:51:51 +0000 | [diff] [blame] | 366 | |
David S. Miller | 9e56e38 | 2012-06-28 18:54:02 -0700 | [diff] [blame] | 367 | fl4.flowi4_mark = IN_DEV_SRC_VMARK(idev) ? skb->mark : 0; |
David Ahern | 5a847a6 | 2018-05-16 13:36:40 -0700 | [diff] [blame] | 368 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | |
Andy Gospodarek | 0eeb075 | 2015-06-23 13:45:37 -0400 | [diff] [blame] | 374 | if (fib_lookup(net, &fl4, &res, 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | goto last_resort; |
Sébastien Barré | 1dced6a | 2014-08-17 09:19:54 +0200 | [diff] [blame] | 376 | if (res.type != RTN_UNICAST && |
| 377 | (res.type != RTN_LOCAL || !IN_DEV_ACCEPT_LOCAL(idev))) |
| 378 | goto e_inval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | fib_combine_itag(itag, &res); |
David S. Miller | 6f86b32 | 2010-09-06 22:36:19 -0700 | [diff] [blame] | 380 | |
David Ahern | 78f2756 | 2018-09-20 13:50:47 -0700 | [diff] [blame] | 381 | dev_match = fib_info_nh_uses_dev(res.fi, dev); |
Cong Wang | 66f8209 | 2019-07-17 14:41:58 -0700 | [diff] [blame] | 382 | /* 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. Miller | 6f86b32 | 2010-09-06 22:36:19 -0700 | [diff] [blame] | 387 | if (dev_match) { |
David Ahern | eba618a | 2019-04-02 14:11:55 -0700 | [diff] [blame] | 388 | ret = FIB_RES_NHC(res)->nhc_scope >= RT_SCOPE_HOST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | return ret; |
| 390 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | if (no_addr) |
| 392 | goto last_resort; |
Stephen Hemminger | c1cf842 | 2009-02-20 08:25:36 +0000 | [diff] [blame] | 393 | if (rpf == 1) |
Eric Dumazet | b5f7e75 | 2010-06-02 12:05:27 +0000 | [diff] [blame] | 394 | goto e_rpf; |
David S. Miller | 9ade228 | 2011-03-12 02:02:42 -0500 | [diff] [blame] | 395 | fl4.flowi4_oif = dev->ifindex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | |
| 397 | ret = 0; |
Andy Gospodarek | 0eeb075 | 2015-06-23 13:45:37 -0400 | [diff] [blame] | 398 | if (fib_lookup(net, &fl4, &res, FIB_LOOKUP_IGNORE_LINKSTATE) == 0) { |
David S. Miller | 41347dc | 2012-06-28 04:05:27 -0700 | [diff] [blame] | 399 | if (res.type == RTN_UNICAST) |
David Ahern | eba618a | 2019-04-02 14:11:55 -0700 | [diff] [blame] | 400 | ret = FIB_RES_NHC(res)->nhc_scope >= RT_SCOPE_HOST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | } |
| 402 | return ret; |
| 403 | |
| 404 | last_resort: |
| 405 | if (rpf) |
Eric Dumazet | b5f7e75 | 2010-06-02 12:05:27 +0000 | [diff] [blame] | 406 | goto e_rpf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | *itag = 0; |
| 408 | return 0; |
| 409 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | e_inval: |
| 411 | return -EINVAL; |
Eric Dumazet | b5f7e75 | 2010-06-02 12:05:27 +0000 | [diff] [blame] | 412 | e_rpf: |
| 413 | return -EXDEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | } |
| 415 | |
David S. Miller | 7a9bc9b | 2012-06-29 01:32:45 -0700 | [diff] [blame] | 416 | /* Ignore rp_filter for packets protected by IPsec. */ |
| 417 | int 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 Abeni | 6e617de | 2017-09-20 18:26:53 +0200 | [diff] [blame] | 422 | struct net *net = dev_net(dev); |
David S. Miller | 7a9bc9b | 2012-06-29 01:32:45 -0700 | [diff] [blame] | 423 | |
Paolo Abeni | 6e617de | 2017-09-20 18:26:53 +0200 | [diff] [blame] | 424 | if (!r && !fib_num_tclassid_users(net) && |
Julian Anastasov | e81da0e | 2012-10-08 11:41:15 +0000 | [diff] [blame] | 425 | (dev->ifindex != oif || !IN_DEV_TX_REDIRECTS(idev))) { |
Paolo Abeni | 6e617de | 2017-09-20 18:26:53 +0200 | [diff] [blame] | 426 | if (IN_DEV_ACCEPT_LOCAL(idev)) |
| 427 | goto ok; |
Paolo Abeni | 032a480 | 2017-10-31 14:32:38 +0100 | [diff] [blame] | 428 | /* 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 Abeni | 6e617de | 2017-09-20 18:26:53 +0200 | [diff] [blame] | 431 | */ |
Paolo Abeni | 032a480 | 2017-10-31 14:32:38 +0100 | [diff] [blame] | 432 | if (net->ipv4.fib_has_custom_local_routes || |
| 433 | fib4_has_custom_rules(net)) |
Paolo Abeni | 6e617de | 2017-09-20 18:26:53 +0200 | [diff] [blame] | 434 | goto full_check; |
| 435 | if (inet_lookup_ifaddr_rcu(net, src)) |
| 436 | return -EINVAL; |
| 437 | |
| 438 | ok: |
David S. Miller | 7a9bc9b | 2012-06-29 01:32:45 -0700 | [diff] [blame] | 439 | *itag = 0; |
| 440 | return 0; |
| 441 | } |
Paolo Abeni | 6e617de | 2017-09-20 18:26:53 +0200 | [diff] [blame] | 442 | |
| 443 | full_check: |
David S. Miller | 7a9bc9b | 2012-06-29 01:32:45 -0700 | [diff] [blame] | 444 | return __fib_validate_source(skb, src, dst, tos, oif, dev, r, idev, itag); |
| 445 | } |
| 446 | |
Al Viro | 81f7bf6 | 2006-09-27 18:40:00 -0700 | [diff] [blame] | 447 | static inline __be32 sk_extract_addr(struct sockaddr *addr) |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 448 | { |
| 449 | return ((struct sockaddr_in *) addr)->sin_addr.s_addr; |
| 450 | } |
| 451 | |
| 452 | static 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. Lunev | 4b5d47d | 2008-01-10 03:29:23 -0800 | [diff] [blame] | 464 | static int rtentry_to_fib_config(struct net *net, int cmd, struct rtentry *rt, |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 465 | struct fib_config *cfg) |
| 466 | { |
Al Viro | 6d85c10 | 2006-09-26 22:15:46 -0700 | [diff] [blame] | 467 | __be32 addr; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 468 | int plen; |
| 469 | |
| 470 | memset(cfg, 0, sizeof(*cfg)); |
Denis V. Lunev | 4b5d47d | 2008-01-10 03:29:23 -0800 | [diff] [blame] | 471 | cfg->fc_nlinfo.nl_net = net; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 472 | |
| 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 Viro | 81f7bf6 | 2006-09-27 18:40:00 -0700 | [diff] [blame] | 487 | __be32 mask = sk_extract_addr(&rt->rt_genmask); |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 488 | |
| 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. Lunev | 4b5d47d | 2008-01-10 03:29:23 -0800 | [diff] [blame] | 532 | dev = __dev_get_by_name(net, devname); |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 533 | if (!dev) |
| 534 | return -ENODEV; |
| 535 | cfg->fc_oif = dev->ifindex; |
Mark Tomlinson | 5a56a0b | 2016-09-05 10:20:20 +1200 | [diff] [blame] | 536 | cfg->fc_table = l3mdev_fib_table(dev); |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 537 | if (colon) { |
Florian Westphal | cd5a411 | 2019-05-31 18:27:07 +0200 | [diff] [blame] | 538 | const struct in_ifaddr *ifa; |
| 539 | struct in_device *in_dev; |
| 540 | |
| 541 | in_dev = __in_dev_get_rtnl(dev); |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 542 | if (!in_dev) |
| 543 | return -ENODEV; |
Florian Westphal | cd5a411 | 2019-05-31 18:27:07 +0200 | [diff] [blame] | 544 | |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 545 | *colon = ':'; |
Florian Westphal | cd5a411 | 2019-05-31 18:27:07 +0200 | [diff] [blame] | 546 | |
| 547 | rcu_read_lock(); |
| 548 | in_dev_for_each_ifa_rcu(ifa, in_dev) { |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 549 | if (strcmp(ifa->ifa_label, devname) == 0) |
| 550 | break; |
Florian Westphal | cd5a411 | 2019-05-31 18:27:07 +0200 | [diff] [blame] | 551 | } |
| 552 | rcu_read_unlock(); |
| 553 | |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 554 | if (!ifa) |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 555 | 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 Ahern | 30bbaa1 | 2015-08-13 14:59:05 -0600 | [diff] [blame] | 562 | unsigned int addr_type; |
| 563 | |
David Ahern | f35b794 | 2019-04-05 16:30:28 -0700 | [diff] [blame] | 564 | cfg->fc_gw4 = addr; |
| 565 | cfg->fc_gw_family = AF_INET; |
David Ahern | 30bbaa1 | 2015-08-13 14:59:05 -0600 | [diff] [blame] | 566 | addr_type = inet_addr_type_table(net, addr, cfg->fc_table); |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 567 | if (rt->rt_flags & RTF_GATEWAY && |
David Ahern | 30bbaa1 | 2015-08-13 14:59:05 -0600 | [diff] [blame] | 568 | addr_type == RTN_UNICAST) |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 569 | cfg->fc_scope = RT_SCOPE_UNIVERSE; |
| 570 | } |
| 571 | |
| 572 | if (cmd == SIOCDELRT) |
| 573 | return 0; |
| 574 | |
David Ahern | f35b794 | 2019-04-05 16:30:28 -0700 | [diff] [blame] | 575 | if (rt->rt_flags & RTF_GATEWAY && !cfg->fc_gw_family) |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 576 | 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 Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 585 | mx = kcalloc(3, nla_total_size(4), GFP_KERNEL); |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 586 | if (!mx) |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 587 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | /* |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 606 | * Handle IP routing ioctl calls. |
| 607 | * These are used to manipulate the routing tables |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | */ |
Al Viro | ca25c30 | 2017-07-01 08:03:10 -0400 | [diff] [blame] | 609 | int ip_rt_ioctl(struct net *net, unsigned int cmd, struct rtentry *rt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | { |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 611 | struct fib_config cfg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | |
| 614 | switch (cmd) { |
| 615 | case SIOCADDRT: /* Add a route */ |
| 616 | case SIOCDELRT: /* Delete a route */ |
Eric W. Biederman | 52e804c | 2012-11-16 03:03:05 +0000 | [diff] [blame] | 617 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | return -EPERM; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 619 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | rtnl_lock(); |
Al Viro | ca25c30 | 2017-07-01 08:03:10 -0400 | [diff] [blame] | 621 | err = rtentry_to_fib_config(net, cmd, rt, &cfg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | if (err == 0) { |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 623 | struct fib_table *tb; |
| 624 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | if (cmd == SIOCDELRT) { |
Denis V. Lunev | 1bad118 | 2008-01-10 03:29:53 -0800 | [diff] [blame] | 626 | tb = fib_get_table(net, cfg.fc_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | if (tb) |
David Ahern | 7805599 | 2017-05-27 16:19:26 -0600 | [diff] [blame] | 628 | err = fib_table_delete(net, tb, &cfg, |
| 629 | NULL); |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 630 | else |
| 631 | err = -ESRCH; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | } else { |
Denis V. Lunev | 1bad118 | 2008-01-10 03:29:53 -0800 | [diff] [blame] | 633 | tb = fib_new_table(net, cfg.fc_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | if (tb) |
David Ahern | 6d8422a1 | 2017-05-21 10:12:02 -0600 | [diff] [blame] | 635 | err = fib_table_insert(net, tb, |
| 636 | &cfg, NULL); |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 637 | else |
| 638 | err = -ENOBUFS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | } |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 640 | |
| 641 | /* allocated by rtentry_to_fib_config() */ |
| 642 | kfree(cfg.fc_mx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | } |
| 644 | rtnl_unlock(); |
| 645 | return err; |
| 646 | } |
| 647 | return -EINVAL; |
| 648 | } |
| 649 | |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 650 | const struct nla_policy rtm_ipv4_policy[RTA_MAX + 1] = { |
David Ahern | 7542565 | 2019-05-22 12:07:43 -0700 | [diff] [blame] | 651 | [RTA_UNSPEC] = { .strict_start_type = RTA_DPORT + 1 }, |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 652 | [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 Graf | 5176f91 | 2006-08-26 20:13:18 -0700 | [diff] [blame] | 660 | [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) }, |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 661 | [RTA_FLOW] = { .type = NLA_U32 }, |
Roopa Prabhu | 571e722 | 2015-07-21 10:43:47 +0200 | [diff] [blame] | 662 | [RTA_ENCAP_TYPE] = { .type = NLA_U16 }, |
| 663 | [RTA_ENCAP] = { .type = NLA_NESTED }, |
Lorenzo Colitti | 622ec2c | 2016-11-04 02:23:42 +0900 | [diff] [blame] | 664 | [RTA_UID] = { .type = NLA_U32 }, |
Liping Zhang | 3b45a41 | 2017-02-27 20:59:39 +0800 | [diff] [blame] | 665 | [RTA_MARK] = { .type = NLA_U32 }, |
Roopa Prabhu | 2eabd76 | 2018-05-22 13:44:51 -0700 | [diff] [blame] | 666 | [RTA_TABLE] = { .type = NLA_U32 }, |
Roopa Prabhu | 404eb77 | 2018-05-22 14:03:27 -0700 | [diff] [blame] | 667 | [RTA_IP_PROTO] = { .type = NLA_U8 }, |
| 668 | [RTA_SPORT] = { .type = NLA_U16 }, |
| 669 | [RTA_DPORT] = { .type = NLA_U16 }, |
David Ahern | 493ced1 | 2019-06-08 14:53:32 -0700 | [diff] [blame] | 670 | [RTA_NH_ID] = { .type = NLA_U32 }, |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 671 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | |
David Ahern | d156626 | 2019-04-05 16:30:40 -0700 | [diff] [blame] | 673 | int 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. Lunev | 4b5d47d | 2008-01-10 03:29:23 -0800 | [diff] [blame] | 717 | static int rtm_to_fib_config(struct net *net, struct sk_buff *skb, |
David Ahern | 6d8422a1 | 2017-05-21 10:12:02 -0600 | [diff] [blame] | 718 | struct nlmsghdr *nlh, struct fib_config *cfg, |
| 719 | struct netlink_ext_ack *extack) |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 720 | { |
David Ahern | d156626 | 2019-04-05 16:30:40 -0700 | [diff] [blame] | 721 | bool has_gw = false, has_via = false; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 722 | struct nlattr *attr; |
| 723 | int err, remaining; |
| 724 | struct rtmsg *rtm; |
| 725 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 726 | err = nlmsg_validate_deprecated(nlh, sizeof(*rtm), RTA_MAX, |
| 727 | rtm_ipv4_policy, extack); |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 728 | if (err < 0) |
| 729 | goto errout; |
| 730 | |
| 731 | memset(cfg, 0, sizeof(*cfg)); |
| 732 | |
| 733 | rtm = nlmsg_data(nlh); |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 734 | cfg->fc_dst_len = rtm->rtm_dst_len; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 735 | 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. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 743 | cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 744 | cfg->fc_nlinfo.nlh = nlh; |
Denis V. Lunev | 4b5d47d | 2008-01-10 03:29:23 -0800 | [diff] [blame] | 745 | cfg->fc_nlinfo.nl_net = net; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 746 | |
Thomas Graf | a0ee18b | 2007-03-24 20:32:54 -0700 | [diff] [blame] | 747 | if (cfg->fc_type > RTN_MAX) { |
David Ahern | c3ab2b4 | 2017-05-21 10:12:03 -0600 | [diff] [blame] | 748 | NL_SET_ERR_MSG(extack, "Invalid route type"); |
Thomas Graf | a0ee18b | 2007-03-24 20:32:54 -0700 | [diff] [blame] | 749 | err = -EINVAL; |
| 750 | goto errout; |
| 751 | } |
| 752 | |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 753 | nlmsg_for_each_attr(attr, nlh, sizeof(struct rtmsg), remaining) { |
Thomas Graf | 8f4c1f9 | 2007-09-12 14:44:36 +0200 | [diff] [blame] | 754 | switch (nla_type(attr)) { |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 755 | case RTA_DST: |
Al Viro | 17fb2c6 | 2006-09-26 22:15:25 -0700 | [diff] [blame] | 756 | cfg->fc_dst = nla_get_be32(attr); |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 757 | break; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 758 | case RTA_OIF: |
| 759 | cfg->fc_oif = nla_get_u32(attr); |
| 760 | break; |
| 761 | case RTA_GATEWAY: |
David Ahern | d156626 | 2019-04-05 16:30:40 -0700 | [diff] [blame] | 762 | has_gw = true; |
David Ahern | f35b794 | 2019-04-05 16:30:28 -0700 | [diff] [blame] | 763 | cfg->fc_gw4 = nla_get_be32(attr); |
David Ahern | d73f80f | 2019-04-10 10:05:51 -0700 | [diff] [blame] | 764 | if (cfg->fc_gw4) |
| 765 | cfg->fc_gw_family = AF_INET; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 766 | break; |
David Ahern | b6e9e5d | 2019-02-26 09:00:02 -0800 | [diff] [blame] | 767 | case RTA_VIA: |
David Ahern | d156626 | 2019-04-05 16:30:40 -0700 | [diff] [blame] | 768 | has_via = true; |
| 769 | err = fib_gw_from_via(cfg, attr, extack); |
| 770 | if (err) |
| 771 | goto errout; |
| 772 | break; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 773 | case RTA_PRIORITY: |
| 774 | cfg->fc_priority = nla_get_u32(attr); |
| 775 | break; |
| 776 | case RTA_PREFSRC: |
Al Viro | 17fb2c6 | 2006-09-26 22:15:25 -0700 | [diff] [blame] | 777 | cfg->fc_prefsrc = nla_get_be32(attr); |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 778 | 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 Ahern | 9ed5959 | 2017-01-17 14:57:36 -0800 | [diff] [blame] | 784 | err = lwtunnel_valid_encap_type_attr(nla_data(attr), |
David Ahern | c255bd6 | 2017-05-27 16:19:27 -0600 | [diff] [blame] | 785 | nla_len(attr), |
| 786 | extack); |
David Ahern | 9ed5959 | 2017-01-17 14:57:36 -0800 | [diff] [blame] | 787 | if (err < 0) |
| 788 | goto errout; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 789 | 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 Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 795 | case RTA_TABLE: |
| 796 | cfg->fc_table = nla_get_u32(attr); |
| 797 | break; |
Roopa Prabhu | 571e722 | 2015-07-21 10:43:47 +0200 | [diff] [blame] | 798 | 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 Ahern | c255bd6 | 2017-05-27 16:19:27 -0600 | [diff] [blame] | 803 | err = lwtunnel_valid_encap_type(cfg->fc_encap_type, |
| 804 | extack); |
David Ahern | 9ed5959 | 2017-01-17 14:57:36 -0800 | [diff] [blame] | 805 | if (err < 0) |
| 806 | goto errout; |
Roopa Prabhu | 571e722 | 2015-07-21 10:43:47 +0200 | [diff] [blame] | 807 | break; |
David Ahern | 493ced1 | 2019-06-08 14:53:32 -0700 | [diff] [blame] | 808 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | } |
| 821 | } |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 822 | |
David Ahern | d156626 | 2019-04-05 16:30:40 -0700 | [diff] [blame] | 823 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | return 0; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 830 | errout: |
| 831 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | } |
| 833 | |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 834 | static int inet_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 835 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 837 | struct net *net = sock_net(skb->sk); |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 838 | struct fib_config cfg; |
| 839 | struct fib_table *tb; |
| 840 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | |
David Ahern | 6d8422a1 | 2017-05-21 10:12:02 -0600 | [diff] [blame] | 842 | err = rtm_to_fib_config(net, skb, nlh, &cfg, extack); |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 843 | if (err < 0) |
| 844 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | |
David Ahern | 493ced1 | 2019-06-08 14:53:32 -0700 | [diff] [blame] | 846 | 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. Lunev | 8ad4942 | 2008-01-10 03:24:11 -0800 | [diff] [blame] | 852 | tb = fib_get_table(net, cfg.fc_table); |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 853 | if (!tb) { |
David Ahern | c3ab2b4 | 2017-05-21 10:12:03 -0600 | [diff] [blame] | 854 | NL_SET_ERR_MSG(extack, "FIB table does not exist"); |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 855 | err = -ESRCH; |
| 856 | goto errout; |
| 857 | } |
| 858 | |
David Ahern | 7805599 | 2017-05-27 16:19:26 -0600 | [diff] [blame] | 859 | err = fib_table_delete(net, tb, &cfg, extack); |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 860 | errout: |
| 861 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | } |
| 863 | |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 864 | static int inet_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 865 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 867 | struct net *net = sock_net(skb->sk); |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 868 | struct fib_config cfg; |
| 869 | struct fib_table *tb; |
| 870 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | |
David Ahern | 6d8422a1 | 2017-05-21 10:12:02 -0600 | [diff] [blame] | 872 | err = rtm_to_fib_config(net, skb, nlh, &cfg, extack); |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 873 | if (err < 0) |
| 874 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | |
Denis V. Lunev | 226b0b4a5 | 2008-01-10 03:30:24 -0800 | [diff] [blame] | 876 | tb = fib_new_table(net, cfg.fc_table); |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 877 | if (!tb) { |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 878 | err = -ENOBUFS; |
| 879 | goto errout; |
| 880 | } |
| 881 | |
David Ahern | 6d8422a1 | 2017-05-21 10:12:02 -0600 | [diff] [blame] | 882 | err = fib_table_insert(net, tb, &cfg, extack); |
Paolo Abeni | 6e617de | 2017-09-20 18:26:53 +0200 | [diff] [blame] | 883 | if (!err && cfg.fc_type == RTN_LOCAL) |
| 884 | net->ipv4.fib_has_custom_local_routes = true; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 885 | errout: |
| 886 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | } |
| 888 | |
David Ahern | 4724676 | 2018-10-15 18:56:42 -0700 | [diff] [blame] | 889 | int ip_valid_fib_dump_req(struct net *net, const struct nlmsghdr *nlh, |
| 890 | struct fib_dump_filter *filter, |
David Ahern | effe679 | 2018-10-15 18:56:48 -0700 | [diff] [blame] | 891 | struct netlink_callback *cb) |
David Ahern | e8ba330 | 2018-10-07 20:16:35 -0700 | [diff] [blame] | 892 | { |
David Ahern | effe679 | 2018-10-15 18:56:48 -0700 | [diff] [blame] | 893 | struct netlink_ext_ack *extack = cb->extack; |
| 894 | struct nlattr *tb[RTA_MAX + 1]; |
David Ahern | e8ba330 | 2018-10-07 20:16:35 -0700 | [diff] [blame] | 895 | struct rtmsg *rtm; |
David Ahern | effe679 | 2018-10-15 18:56:48 -0700 | [diff] [blame] | 896 | int err, i; |
| 897 | |
| 898 | ASSERT_RTNL(); |
David Ahern | e8ba330 | 2018-10-07 20:16:35 -0700 | [diff] [blame] | 899 | |
| 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 Ahern | effe679 | 2018-10-15 18:56:48 -0700 | [diff] [blame] | 907 | rtm->rtm_scope) { |
David Ahern | e8ba330 | 2018-10-07 20:16:35 -0700 | [diff] [blame] | 908 | NL_SET_ERR_MSG(extack, "Invalid values in header for FIB dump request"); |
| 909 | return -EINVAL; |
| 910 | } |
Stefano Brivio | 564c91f | 2019-06-21 17:45:20 +0200 | [diff] [blame] | 911 | |
David Ahern | e8ba330 | 2018-10-07 20:16:35 -0700 | [diff] [blame] | 912 | 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 Brivio | 564c91f | 2019-06-21 17:45:20 +0200 | [diff] [blame] | 916 | if (rtm->rtm_flags & RTM_F_CLONED) |
| 917 | filter->dump_routes = false; |
| 918 | else |
| 919 | filter->dump_exceptions = false; |
David Ahern | e8ba330 | 2018-10-07 20:16:35 -0700 | [diff] [blame] | 920 | |
David Ahern | ae677bb | 2018-10-24 12:59:01 -0700 | [diff] [blame] | 921 | filter->dump_all_families = (rtm->rtm_family == AF_UNSPEC); |
David Ahern | effe679 | 2018-10-15 18:56:48 -0700 | [diff] [blame] | 922 | 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 Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 927 | err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX, |
| 928 | rtm_ipv4_policy, extack); |
David Ahern | effe679 | 2018-10-15 18:56:48 -0700 | [diff] [blame] | 929 | 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 Ahern | e8ba330 | 2018-10-07 20:16:35 -0700 | [diff] [blame] | 958 | } |
| 959 | |
| 960 | return 0; |
| 961 | } |
| 962 | EXPORT_SYMBOL_GPL(ip_valid_fib_dump_req); |
| 963 | |
Thomas Graf | 63f3444 | 2007-03-22 11:55:17 -0700 | [diff] [blame] | 964 | static int inet_dump_fib(struct sk_buff *skb, struct netlink_callback *cb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | { |
Stefano Brivio | 564c91f | 2019-06-21 17:45:20 +0200 | [diff] [blame] | 966 | struct fib_dump_filter filter = { .dump_routes = true, |
| 967 | .dump_exceptions = true }; |
David Ahern | e8ba330 | 2018-10-07 20:16:35 -0700 | [diff] [blame] | 968 | const struct nlmsghdr *nlh = cb->nlh; |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 969 | struct net *net = sock_net(skb->sk); |
Patrick McHardy | 1af5a8c | 2006-08-10 23:10:46 -0700 | [diff] [blame] | 970 | unsigned int h, s_h; |
| 971 | unsigned int e = 0, s_e; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | struct fib_table *tb; |
Denis V. Lunev | e4aef8a | 2008-01-10 03:28:24 -0800 | [diff] [blame] | 973 | struct hlist_head *head; |
David Ahern | f6c5775 | 2017-05-15 23:19:17 -0700 | [diff] [blame] | 974 | int dumped = 0, err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | |
David Ahern | e8ba330 | 2018-10-07 20:16:35 -0700 | [diff] [blame] | 976 | if (cb->strict_check) { |
David Ahern | effe679 | 2018-10-15 18:56:48 -0700 | [diff] [blame] | 977 | err = ip_valid_fib_dump_req(net, nlh, &filter, cb); |
David Ahern | e8ba330 | 2018-10-07 20:16:35 -0700 | [diff] [blame] | 978 | if (err < 0) |
| 979 | return err; |
David Ahern | e4e92fb | 2018-10-15 18:56:51 -0700 | [diff] [blame] | 980 | } 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 Ahern | e8ba330 | 2018-10-07 20:16:35 -0700 | [diff] [blame] | 984 | } |
| 985 | |
Stefano Brivio | b597ca6e | 2019-06-21 17:45:21 +0200 | [diff] [blame] | 986 | /* ipv4 does not use prefix flag */ |
| 987 | if (filter.flags & RTM_F_PREFIX) |
Li RongQing | 0b8c7f6 | 2014-03-21 11:33:10 +0800 | [diff] [blame] | 988 | return skb->len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | |
David Ahern | 18a8021 | 2018-10-15 18:56:43 -0700 | [diff] [blame] | 990 | if (filter.table_id) { |
| 991 | tb = fib_get_table(net, filter.table_id); |
| 992 | if (!tb) { |
David Ahern | ae677bb | 2018-10-24 12:59:01 -0700 | [diff] [blame] | 993 | if (filter.dump_all_families) |
| 994 | return skb->len; |
| 995 | |
David Ahern | 18a8021 | 2018-10-15 18:56:43 -0700 | [diff] [blame] | 996 | NL_SET_ERR_MSG(cb->extack, "ipv4: FIB table does not exist"); |
| 997 | return -ENOENT; |
| 998 | } |
| 999 | |
Qian Cai | dddeb30 | 2020-03-19 22:54:21 -0400 | [diff] [blame^] | 1000 | rcu_read_lock(); |
David Ahern | 18a8021 | 2018-10-15 18:56:43 -0700 | [diff] [blame] | 1001 | err = fib_table_dump(tb, skb, cb, &filter); |
Qian Cai | dddeb30 | 2020-03-19 22:54:21 -0400 | [diff] [blame^] | 1002 | rcu_read_unlock(); |
David Ahern | 18a8021 | 2018-10-15 18:56:43 -0700 | [diff] [blame] | 1003 | return skb->len ? : err; |
| 1004 | } |
| 1005 | |
Patrick McHardy | 1af5a8c | 2006-08-10 23:10:46 -0700 | [diff] [blame] | 1006 | s_h = cb->args[0]; |
| 1007 | s_e = cb->args[1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | |
Alexander Duyck | a7e5353 | 2015-03-04 15:02:44 -0800 | [diff] [blame] | 1009 | rcu_read_lock(); |
| 1010 | |
Patrick McHardy | 1af5a8c | 2006-08-10 23:10:46 -0700 | [diff] [blame] | 1011 | for (h = s_h; h < FIB_TABLE_HASHSZ; h++, s_e = 0) { |
| 1012 | e = 0; |
Denis V. Lunev | e4aef8a | 2008-01-10 03:28:24 -0800 | [diff] [blame] | 1013 | head = &net->ipv4.fib_table_hash[h]; |
Alexander Duyck | a7e5353 | 2015-03-04 15:02:44 -0800 | [diff] [blame] | 1014 | hlist_for_each_entry_rcu(tb, head, tb_hlist) { |
Patrick McHardy | 1af5a8c | 2006-08-10 23:10:46 -0700 | [diff] [blame] | 1015 | if (e < s_e) |
| 1016 | goto next; |
| 1017 | if (dumped) |
| 1018 | memset(&cb->args[2], 0, sizeof(cb->args) - |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1019 | 2 * sizeof(cb->args[0])); |
David Ahern | 18a8021 | 2018-10-15 18:56:43 -0700 | [diff] [blame] | 1020 | err = fib_table_dump(tb, skb, cb, &filter); |
David Ahern | f6c5775 | 2017-05-15 23:19:17 -0700 | [diff] [blame] | 1021 | if (err < 0) { |
| 1022 | if (likely(skb->len)) |
| 1023 | goto out; |
| 1024 | |
| 1025 | goto out_err; |
| 1026 | } |
Patrick McHardy | 1af5a8c | 2006-08-10 23:10:46 -0700 | [diff] [blame] | 1027 | dumped = 1; |
| 1028 | next: |
| 1029 | e++; |
| 1030 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | } |
Patrick McHardy | 1af5a8c | 2006-08-10 23:10:46 -0700 | [diff] [blame] | 1032 | out: |
David Ahern | f6c5775 | 2017-05-15 23:19:17 -0700 | [diff] [blame] | 1033 | err = skb->len; |
| 1034 | out_err: |
Alexander Duyck | a7e5353 | 2015-03-04 15:02:44 -0800 | [diff] [blame] | 1035 | rcu_read_unlock(); |
| 1036 | |
Patrick McHardy | 1af5a8c | 2006-08-10 23:10:46 -0700 | [diff] [blame] | 1037 | cb->args[1] = e; |
| 1038 | cb->args[0] = h; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | |
David Ahern | f6c5775 | 2017-05-15 23:19:17 -0700 | [diff] [blame] | 1040 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | } |
| 1042 | |
| 1043 | /* Prepare and feed intra-kernel routing request. |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 1044 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | */ |
David Ahern | af4d768 | 2018-05-27 08:09:57 -0700 | [diff] [blame] | 1049 | static void fib_magic(int cmd, int type, __be32 dst, int dst_len, |
| 1050 | struct in_ifaddr *ifa, u32 rt_priority) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | { |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 1052 | struct net *net = dev_net(ifa->ifa_dev->dev); |
David Ahern | 3236b00 | 2015-09-29 20:07:14 -0700 | [diff] [blame] | 1053 | u32 tb_id = l3mdev_fib_table(ifa->ifa_dev->dev); |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 1054 | 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 Ahern | af4d768 | 2018-05-27 08:09:57 -0700 | [diff] [blame] | 1060 | .fc_priority = rt_priority, |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 1061 | .fc_prefsrc = ifa->ifa_local, |
| 1062 | .fc_oif = ifa->ifa_dev->dev->ifindex, |
| 1063 | .fc_nlflags = NLM_F_CREATE | NLM_F_APPEND, |
Denis V. Lunev | 4d1169c | 2008-01-10 03:26:13 -0800 | [diff] [blame] | 1064 | .fc_nlinfo = { |
Denis V. Lunev | 4b5d47d | 2008-01-10 03:29:23 -0800 | [diff] [blame] | 1065 | .nl_net = net, |
Denis V. Lunev | 4d1169c | 2008-01-10 03:26:13 -0800 | [diff] [blame] | 1066 | }, |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 1067 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | |
David Ahern | 021dd3b | 2015-08-13 14:59:06 -0600 | [diff] [blame] | 1069 | if (!tb_id) |
| 1070 | tb_id = (type == RTN_UNICAST) ? RT_TABLE_MAIN : RT_TABLE_LOCAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1071 | |
David Ahern | 021dd3b | 2015-08-13 14:59:06 -0600 | [diff] [blame] | 1072 | tb = fib_new_table(net, tb_id); |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 1073 | if (!tb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | return; |
| 1075 | |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 1076 | cfg.fc_table = tb->tb_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 1078 | if (type != RTN_LOCAL) |
| 1079 | cfg.fc_scope = RT_SCOPE_LINK; |
| 1080 | else |
| 1081 | cfg.fc_scope = RT_SCOPE_HOST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | |
| 1083 | if (cmd == RTM_NEWROUTE) |
David Ahern | 6d8422a1 | 2017-05-21 10:12:02 -0600 | [diff] [blame] | 1084 | fib_table_insert(net, tb, &cfg, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | else |
David Ahern | 7805599 | 2017-05-27 16:19:26 -0600 | [diff] [blame] | 1086 | fib_table_delete(net, tb, &cfg, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | } |
| 1088 | |
Jamal Hadi Salim | 0ff60a4 | 2005-11-22 14:47:37 -0800 | [diff] [blame] | 1089 | void fib_add_ifaddr(struct in_ifaddr *ifa) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1090 | { |
| 1091 | struct in_device *in_dev = ifa->ifa_dev; |
| 1092 | struct net_device *dev = in_dev->dev; |
| 1093 | struct in_ifaddr *prim = ifa; |
Al Viro | a144ea4 | 2006-09-28 18:00:55 -0700 | [diff] [blame] | 1094 | __be32 mask = ifa->ifa_mask; |
| 1095 | __be32 addr = ifa->ifa_local; |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 1096 | __be32 prefix = ifa->ifa_address & mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 1098 | if (ifa->ifa_flags & IFA_F_SECONDARY) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | prim = inet_ifa_byprefix(in_dev, prefix, mask); |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 1100 | if (!prim) { |
Joe Perches | 058bd4d | 2012-03-11 18:36:11 +0000 | [diff] [blame] | 1101 | pr_warn("%s: bug: prim == NULL\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1102 | return; |
| 1103 | } |
| 1104 | } |
| 1105 | |
David Ahern | af4d768 | 2018-05-27 08:09:57 -0700 | [diff] [blame] | 1106 | fib_magic(RTM_NEWROUTE, RTN_LOCAL, addr, 32, prim, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1107 | |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 1108 | if (!(dev->flags & IFF_UP)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1109 | return; |
| 1110 | |
| 1111 | /* Add broadcast address, if it is explicitly assigned. */ |
Al Viro | a144ea4 | 2006-09-28 18:00:55 -0700 | [diff] [blame] | 1112 | if (ifa->ifa_broadcast && ifa->ifa_broadcast != htonl(0xFFFFFFFF)) |
David Ahern | af4d768 | 2018-05-27 08:09:57 -0700 | [diff] [blame] | 1113 | fib_magic(RTM_NEWROUTE, RTN_BROADCAST, ifa->ifa_broadcast, 32, |
| 1114 | prim, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 1116 | if (!ipv4_is_zeronet(prefix) && !(ifa->ifa_flags & IFA_F_SECONDARY) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1117 | (prefix != addr || ifa->ifa_prefixlen < 32)) { |
Paolo Abeni | 7b13118 | 2015-10-20 10:28:45 +0200 | [diff] [blame] | 1118 | if (!(ifa->ifa_flags & IFA_F_NOPREFIXROUTE)) |
| 1119 | fib_magic(RTM_NEWROUTE, |
| 1120 | dev->flags & IFF_LOOPBACK ? RTN_LOCAL : RTN_UNICAST, |
David Ahern | af4d768 | 2018-05-27 08:09:57 -0700 | [diff] [blame] | 1121 | prefix, ifa->ifa_prefixlen, prim, |
| 1122 | ifa->ifa_rt_priority); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1123 | |
| 1124 | /* Add network specific broadcasts, when it takes a sense */ |
| 1125 | if (ifa->ifa_prefixlen < 31) { |
David Ahern | af4d768 | 2018-05-27 08:09:57 -0700 | [diff] [blame] | 1126 | fib_magic(RTM_NEWROUTE, RTN_BROADCAST, prefix, 32, |
| 1127 | prim, 0); |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 1128 | fib_magic(RTM_NEWROUTE, RTN_BROADCAST, prefix | ~mask, |
David Ahern | af4d768 | 2018-05-27 08:09:57 -0700 | [diff] [blame] | 1129 | 32, prim, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | } |
| 1131 | } |
| 1132 | } |
| 1133 | |
David Ahern | af4d768 | 2018-05-27 08:09:57 -0700 | [diff] [blame] | 1134 | void 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 Abeni | 0b834ba | 2019-10-26 11:53:39 +0200 | [diff] [blame] | 1143 | (prefix == ifa->ifa_local && ifa->ifa_prefixlen == 32)) |
David Ahern | af4d768 | 2018-05-27 08:09:57 -0700 | [diff] [blame] | 1144 | 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 Anastasov | e6abbaa | 2011-03-19 12:13:49 +0000 | [diff] [blame] | 1157 | /* 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 | */ |
| 1162 | void fib_del_ifaddr(struct in_ifaddr *ifa, struct in_ifaddr *iprim) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1163 | { |
| 1164 | struct in_device *in_dev = ifa->ifa_dev; |
| 1165 | struct net_device *dev = in_dev->dev; |
| 1166 | struct in_ifaddr *ifa1; |
Julian Anastasov | e6abbaa | 2011-03-19 12:13:49 +0000 | [diff] [blame] | 1167 | struct in_ifaddr *prim = ifa, *prim1 = NULL; |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 1168 | __be32 brd = ifa->ifa_address | ~ifa->ifa_mask; |
| 1169 | __be32 any = ifa->ifa_address & ifa->ifa_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1170 | #define LOCAL_OK 1 |
| 1171 | #define BRD_OK 2 |
| 1172 | #define BRD0_OK 4 |
| 1173 | #define BRD1_OK 8 |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 1174 | unsigned int ok = 0; |
Julian Anastasov | e6abbaa | 2011-03-19 12:13:49 +0000 | [diff] [blame] | 1175 | int subnet = 0; /* Primary network */ |
| 1176 | int gone = 1; /* Address is missing */ |
| 1177 | int same_prefsrc = 0; /* Another primary with same IP */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1178 | |
Julian Anastasov | e6abbaa | 2011-03-19 12:13:49 +0000 | [diff] [blame] | 1179 | if (ifa->ifa_flags & IFA_F_SECONDARY) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1180 | prim = inet_ifa_byprefix(in_dev, any, ifa->ifa_mask); |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 1181 | if (!prim) { |
Paolo Abeni | 391a203 | 2016-04-21 22:23:31 +0200 | [diff] [blame] | 1182 | /* 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1187 | return; |
| 1188 | } |
Julian Anastasov | e6abbaa | 2011-03-19 12:13:49 +0000 | [diff] [blame] | 1189 | if (iprim && iprim != prim) { |
Joe Perches | 058bd4d | 2012-03-11 18:36:11 +0000 | [diff] [blame] | 1190 | pr_warn("%s: bug: iprim != prim\n", __func__); |
Julian Anastasov | e6abbaa | 2011-03-19 12:13:49 +0000 | [diff] [blame] | 1191 | return; |
| 1192 | } |
| 1193 | } else if (!ipv4_is_zeronet(any) && |
| 1194 | (any != ifa->ifa_local || ifa->ifa_prefixlen < 32)) { |
Paolo Abeni | 7b13118 | 2015-10-20 10:28:45 +0200 | [diff] [blame] | 1195 | if (!(ifa->ifa_flags & IFA_F_NOPREFIXROUTE)) |
| 1196 | fib_magic(RTM_DELROUTE, |
| 1197 | dev->flags & IFF_LOOPBACK ? RTN_LOCAL : RTN_UNICAST, |
David Ahern | af4d768 | 2018-05-27 08:09:57 -0700 | [diff] [blame] | 1198 | any, ifa->ifa_prefixlen, prim, 0); |
Julian Anastasov | e6abbaa | 2011-03-19 12:13:49 +0000 | [diff] [blame] | 1199 | subnet = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | } |
| 1201 | |
David S. Miller | fbd40ea | 2016-03-13 23:28:00 -0400 | [diff] [blame] | 1202 | if (in_dev->dead) |
| 1203 | goto no_promotions; |
| 1204 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1205 | /* Deletion is more complicated than add. |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 1206 | * We should take care of not to delete too much :-) |
| 1207 | * |
| 1208 | * Scan address list to be sure that addresses are really gone. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1209 | */ |
Florian Westphal | cd5a411 | 2019-05-31 18:27:07 +0200 | [diff] [blame] | 1210 | rcu_read_lock(); |
| 1211 | in_dev_for_each_ifa_rcu(ifa1, in_dev) { |
Julian Anastasov | e6abbaa | 2011-03-19 12:13:49 +0000 | [diff] [blame] | 1212 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1255 | 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 Anastasov | e6abbaa | 2011-03-19 12:13:49 +0000 | [diff] [blame] | 1263 | /* 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1278 | } |
Florian Westphal | cd5a411 | 2019-05-31 18:27:07 +0200 | [diff] [blame] | 1279 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1280 | |
David S. Miller | fbd40ea | 2016-03-13 23:28:00 -0400 | [diff] [blame] | 1281 | no_promotions: |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 1282 | if (!(ok & BRD_OK)) |
David Ahern | af4d768 | 2018-05-27 08:09:57 -0700 | [diff] [blame] | 1283 | fib_magic(RTM_DELROUTE, RTN_BROADCAST, ifa->ifa_broadcast, 32, |
| 1284 | prim, 0); |
Julian Anastasov | e6abbaa | 2011-03-19 12:13:49 +0000 | [diff] [blame] | 1285 | if (subnet && ifa->ifa_prefixlen < 31) { |
| 1286 | if (!(ok & BRD1_OK)) |
David Ahern | af4d768 | 2018-05-27 08:09:57 -0700 | [diff] [blame] | 1287 | fib_magic(RTM_DELROUTE, RTN_BROADCAST, brd, 32, |
| 1288 | prim, 0); |
Julian Anastasov | e6abbaa | 2011-03-19 12:13:49 +0000 | [diff] [blame] | 1289 | if (!(ok & BRD0_OK)) |
David Ahern | af4d768 | 2018-05-27 08:09:57 -0700 | [diff] [blame] | 1290 | fib_magic(RTM_DELROUTE, RTN_BROADCAST, any, 32, |
| 1291 | prim, 0); |
Julian Anastasov | e6abbaa | 2011-03-19 12:13:49 +0000 | [diff] [blame] | 1292 | } |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 1293 | if (!(ok & LOCAL_OK)) { |
David Ahern | 30bbaa1 | 2015-08-13 14:59:05 -0600 | [diff] [blame] | 1294 | unsigned int addr_type; |
| 1295 | |
David Ahern | af4d768 | 2018-05-27 08:09:57 -0700 | [diff] [blame] | 1296 | fib_magic(RTM_DELROUTE, RTN_LOCAL, ifa->ifa_local, 32, prim, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1297 | |
| 1298 | /* Check, that this local address finally disappeared. */ |
David Ahern | 30bbaa1 | 2015-08-13 14:59:05 -0600 | [diff] [blame] | 1299 | addr_type = inet_addr_type_dev_table(dev_net(dev), dev, |
| 1300 | ifa->ifa_local); |
| 1301 | if (gone && addr_type != RTN_LOCAL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | /* And the last, but not the least thing. |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 1303 | * 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 Tomlinson | 5a56a0b | 2016-09-05 10:20:20 +1200 | [diff] [blame] | 1308 | if (fib_sync_down_addr(dev, ifa->ifa_local)) |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 1309 | fib_flush(dev_net(dev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1310 | } |
| 1311 | } |
| 1312 | #undef LOCAL_OK |
| 1313 | #undef BRD_OK |
| 1314 | #undef BRD0_OK |
| 1315 | #undef BRD1_OK |
| 1316 | } |
| 1317 | |
Alexander Duyck | 345e9b5 | 2014-12-31 10:56:24 -0800 | [diff] [blame] | 1318 | static void nl_fib_lookup(struct net *net, struct fib_result_nl *frn) |
Robert Olsson | 246955f | 2005-06-20 13:36:39 -0700 | [diff] [blame] | 1319 | { |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1320 | |
Robert Olsson | 246955f | 2005-06-20 13:36:39 -0700 | [diff] [blame] | 1321 | struct fib_result res; |
David S. Miller | 9ade228 | 2011-03-12 02:02:42 -0500 | [diff] [blame] | 1322 | 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 Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 1327 | }; |
Alexander Duyck | 345e9b5 | 2014-12-31 10:56:24 -0800 | [diff] [blame] | 1328 | struct fib_table *tb; |
| 1329 | |
| 1330 | rcu_read_lock(); |
| 1331 | |
| 1332 | tb = fib_get_table(net, frn->tb_id_in); |
Alexey Kuznetsov | 1194ed0 | 2007-04-25 13:07:28 -0700 | [diff] [blame] | 1333 | |
| 1334 | frn->err = -ENOENT; |
Robert Olsson | 246955f | 2005-06-20 13:36:39 -0700 | [diff] [blame] | 1335 | if (tb) { |
| 1336 | local_bh_disable(); |
| 1337 | |
| 1338 | frn->tb_id = tb->tb_id; |
David S. Miller | 9ade228 | 2011-03-12 02:02:42 -0500 | [diff] [blame] | 1339 | frn->err = fib_table_lookup(tb, &fl4, &res, FIB_LOOKUP_NOREF); |
Robert Olsson | 246955f | 2005-06-20 13:36:39 -0700 | [diff] [blame] | 1340 | |
| 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 Duyck | 345e9b5 | 2014-12-31 10:56:24 -0800 | [diff] [blame] | 1349 | |
| 1350 | rcu_read_unlock(); |
Robert Olsson | 246955f | 2005-06-20 13:36:39 -0700 | [diff] [blame] | 1351 | } |
| 1352 | |
David S. Miller | 28f7b036 | 2007-10-10 21:32:39 -0700 | [diff] [blame] | 1353 | static void nl_fib_input(struct sk_buff *skb) |
Robert Olsson | 246955f | 2005-06-20 13:36:39 -0700 | [diff] [blame] | 1354 | { |
Denis V. Lunev | 6bd48fc | 2008-01-10 03:28:55 -0800 | [diff] [blame] | 1355 | struct net *net; |
Robert Olsson | 246955f | 2005-06-20 13:36:39 -0700 | [diff] [blame] | 1356 | struct fib_result_nl *frn; |
David S. Miller | 28f7b036 | 2007-10-10 21:32:39 -0700 | [diff] [blame] | 1357 | struct nlmsghdr *nlh; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1358 | u32 portid; |
Alexey Kuznetsov | 1194ed0 | 2007-04-25 13:07:28 -0700 | [diff] [blame] | 1359 | |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 1360 | net = sock_net(skb->sk); |
Arnaldo Carvalho de Melo | b529ccf | 2007-04-25 19:08:35 -0700 | [diff] [blame] | 1361 | nlh = nlmsg_hdr(skb); |
Eric Dumazet | c64c0b3 | 2017-03-21 19:22:28 -0700 | [diff] [blame] | 1362 | if (skb->len < nlmsg_total_size(sizeof(*frn)) || |
| 1363 | skb->len < nlh->nlmsg_len || |
Hong zhi guo | 573ce26 | 2013-03-27 06:47:04 +0000 | [diff] [blame] | 1364 | nlmsg_len(nlh) < sizeof(*frn)) |
Thomas Graf | ea86575 | 2005-12-01 14:30:00 -0800 | [diff] [blame] | 1365 | return; |
Denis V. Lunev | d883a03 | 2007-12-21 02:01:53 -0800 | [diff] [blame] | 1366 | |
Pablo Neira | 3a36515 | 2013-06-28 03:04:23 +0200 | [diff] [blame] | 1367 | skb = netlink_skb_clone(skb, GFP_KERNEL); |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 1368 | if (!skb) |
Denis V. Lunev | d883a03 | 2007-12-21 02:01:53 -0800 | [diff] [blame] | 1369 | return; |
| 1370 | nlh = nlmsg_hdr(skb); |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1371 | |
Hong zhi guo | 573ce26 | 2013-03-27 06:47:04 +0000 | [diff] [blame] | 1372 | frn = (struct fib_result_nl *) nlmsg_data(nlh); |
Alexander Duyck | 345e9b5 | 2014-12-31 10:56:24 -0800 | [diff] [blame] | 1373 | nl_fib_lookup(net, frn); |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1374 | |
Rami Rosen | 28a2828 | 2013-01-11 09:59:20 +0000 | [diff] [blame] | 1375 | portid = NETLINK_CB(skb).portid; /* netlink portid */ |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1376 | NETLINK_CB(skb).portid = 0; /* from kernel */ |
Patrick McHardy | ac6d439 | 2005-08-14 19:29:52 -0700 | [diff] [blame] | 1377 | NETLINK_CB(skb).dst_group = 0; /* unicast */ |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1378 | netlink_unicast(net->ipv4.fibnl, skb, portid, MSG_DONTWAIT); |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1379 | } |
Robert Olsson | 246955f | 2005-06-20 13:36:39 -0700 | [diff] [blame] | 1380 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 1381 | static int __net_init nl_fib_lookup_init(struct net *net) |
Robert Olsson | 246955f | 2005-06-20 13:36:39 -0700 | [diff] [blame] | 1382 | { |
Denis V. Lunev | 6bd48fc | 2008-01-10 03:28:55 -0800 | [diff] [blame] | 1383 | struct sock *sk; |
Pablo Neira Ayuso | a31f2d1 | 2012-06-29 06:15:21 +0000 | [diff] [blame] | 1384 | struct netlink_kernel_cfg cfg = { |
| 1385 | .input = nl_fib_input, |
| 1386 | }; |
| 1387 | |
Pablo Neira Ayuso | 9f00d97 | 2012-09-08 02:53:54 +0000 | [diff] [blame] | 1388 | sk = netlink_kernel_create(net, NETLINK_FIB_LOOKUP, &cfg); |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 1389 | if (!sk) |
Denis V. Lunev | 7b1a74fd | 2008-01-10 03:22:17 -0800 | [diff] [blame] | 1390 | return -EAFNOSUPPORT; |
Denis V. Lunev | 6bd48fc | 2008-01-10 03:28:55 -0800 | [diff] [blame] | 1391 | net->ipv4.fibnl = sk; |
Denis V. Lunev | 7b1a74fd | 2008-01-10 03:22:17 -0800 | [diff] [blame] | 1392 | return 0; |
| 1393 | } |
| 1394 | |
| 1395 | static void nl_fib_lookup_exit(struct net *net) |
| 1396 | { |
Denis V. Lunev | b7c6ba6 | 2008-01-28 14:41:19 -0800 | [diff] [blame] | 1397 | netlink_kernel_release(net->ipv4.fibnl); |
Denis V. Lunev | 775516b | 2008-01-18 23:55:19 -0800 | [diff] [blame] | 1398 | net->ipv4.fibnl = NULL; |
Robert Olsson | 246955f | 2005-06-20 13:36:39 -0700 | [diff] [blame] | 1399 | } |
| 1400 | |
Julian Anastasov | 4f823de | 2015-10-30 10:23:33 +0200 | [diff] [blame] | 1401 | static void fib_disable_ip(struct net_device *dev, unsigned long event, |
| 1402 | bool force) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1403 | { |
Julian Anastasov | 4f823de | 2015-10-30 10:23:33 +0200 | [diff] [blame] | 1404 | if (fib_sync_down_dev(dev, event, force)) |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 1405 | fib_flush(dev_net(dev)); |
Gao Feng | 06b4fc5 | 2017-04-26 19:04:04 +0800 | [diff] [blame] | 1406 | else |
| 1407 | rt_cache_flush(dev_net(dev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1408 | arp_ifdown(dev); |
| 1409 | } |
| 1410 | |
| 1411 | static int fib_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr) |
| 1412 | { |
Jianjun Kong | 6ed2533e | 2008-11-03 00:25:16 -0800 | [diff] [blame] | 1413 | struct in_ifaddr *ifa = (struct in_ifaddr *)ptr; |
Denis V. Lunev | 76e6ebf | 2008-07-05 19:00:44 -0700 | [diff] [blame] | 1414 | struct net_device *dev = ifa->ifa_dev->dev; |
David S. Miller | 436c3b6 | 2011-03-24 17:42:21 -0700 | [diff] [blame] | 1415 | struct net *net = dev_net(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1416 | |
| 1417 | switch (event) { |
| 1418 | case NETDEV_UP: |
| 1419 | fib_add_ifaddr(ifa); |
| 1420 | #ifdef CONFIG_IP_ROUTE_MULTIPATH |
Andy Gospodarek | 8a3d031 | 2015-06-23 13:45:36 -0400 | [diff] [blame] | 1421 | fib_sync_up(dev, RTNH_F_DEAD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1422 | #endif |
David S. Miller | 436c3b6 | 2011-03-24 17:42:21 -0700 | [diff] [blame] | 1423 | atomic_inc(&net->ipv4.dev_addr_genid); |
Nicolas Dichtel | 4ccfe6d | 2012-09-07 00:45:29 +0000 | [diff] [blame] | 1424 | rt_cache_flush(dev_net(dev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1425 | break; |
| 1426 | case NETDEV_DOWN: |
Julian Anastasov | e6abbaa | 2011-03-19 12:13:49 +0000 | [diff] [blame] | 1427 | fib_del_ifaddr(ifa, NULL); |
David S. Miller | 436c3b6 | 2011-03-24 17:42:21 -0700 | [diff] [blame] | 1428 | atomic_inc(&net->ipv4.dev_addr_genid); |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 1429 | if (!ifa->ifa_dev->ifa_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1430 | /* Last address was deleted from this interface. |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 1431 | * Disable IP. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1432 | */ |
Julian Anastasov | 4f823de | 2015-10-30 10:23:33 +0200 | [diff] [blame] | 1433 | fib_disable_ip(dev, event, true); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1434 | } else { |
Nicolas Dichtel | 4ccfe6d | 2012-09-07 00:45:29 +0000 | [diff] [blame] | 1435 | rt_cache_flush(dev_net(dev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1436 | } |
| 1437 | break; |
| 1438 | } |
| 1439 | return NOTIFY_DONE; |
| 1440 | } |
| 1441 | |
| 1442 | static int fib_netdev_event(struct notifier_block *this, unsigned long event, void *ptr) |
| 1443 | { |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 1444 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
Sabrina Dubroca | af7d6cc | 2018-10-09 17:48:14 +0200 | [diff] [blame] | 1445 | struct netdev_notifier_changeupper_info *upper_info = ptr; |
| 1446 | struct netdev_notifier_info_ext *info_ext = ptr; |
Eric Dumazet | 0115e8e | 2012-08-22 17:19:46 +0000 | [diff] [blame] | 1447 | struct in_device *in_dev; |
David S. Miller | 436c3b6 | 2011-03-24 17:42:21 -0700 | [diff] [blame] | 1448 | struct net *net = dev_net(dev); |
Florian Westphal | cd5a411 | 2019-05-31 18:27:07 +0200 | [diff] [blame] | 1449 | struct in_ifaddr *ifa; |
Andy Gospodarek | 8a3d031 | 2015-06-23 13:45:36 -0400 | [diff] [blame] | 1450 | unsigned int flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | |
| 1452 | if (event == NETDEV_UNREGISTER) { |
Julian Anastasov | 4f823de | 2015-10-30 10:23:33 +0200 | [diff] [blame] | 1453 | fib_disable_ip(dev, event, true); |
David S. Miller | caacf05 | 2012-07-31 15:06:50 -0700 | [diff] [blame] | 1454 | rt_flush_dev(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1455 | return NOTIFY_DONE; |
| 1456 | } |
| 1457 | |
Eric Dumazet | 0115e8e | 2012-08-22 17:19:46 +0000 | [diff] [blame] | 1458 | in_dev = __in_dev_get_rtnl(dev); |
Oliver Hartkopp | a0065f2 | 2014-01-23 10:19:34 +0100 | [diff] [blame] | 1459 | if (!in_dev) |
| 1460 | return NOTIFY_DONE; |
Eric Dumazet | 0115e8e | 2012-08-22 17:19:46 +0000 | [diff] [blame] | 1461 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1462 | switch (event) { |
| 1463 | case NETDEV_UP: |
Florian Westphal | cd5a411 | 2019-05-31 18:27:07 +0200 | [diff] [blame] | 1464 | in_dev_for_each_ifa_rtnl(ifa, in_dev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1465 | fib_add_ifaddr(ifa); |
Florian Westphal | cd5a411 | 2019-05-31 18:27:07 +0200 | [diff] [blame] | 1466 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1467 | #ifdef CONFIG_IP_ROUTE_MULTIPATH |
Andy Gospodarek | 8a3d031 | 2015-06-23 13:45:36 -0400 | [diff] [blame] | 1468 | fib_sync_up(dev, RTNH_F_DEAD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1469 | #endif |
David S. Miller | 436c3b6 | 2011-03-24 17:42:21 -0700 | [diff] [blame] | 1470 | atomic_inc(&net->ipv4.dev_addr_genid); |
Nicolas Dichtel | 4ccfe6d | 2012-09-07 00:45:29 +0000 | [diff] [blame] | 1471 | rt_cache_flush(net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1472 | break; |
| 1473 | case NETDEV_DOWN: |
Julian Anastasov | 4f823de | 2015-10-30 10:23:33 +0200 | [diff] [blame] | 1474 | fib_disable_ip(dev, event, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1475 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1476 | case NETDEV_CHANGE: |
Andy Gospodarek | 8a3d031 | 2015-06-23 13:45:36 -0400 | [diff] [blame] | 1477 | flags = dev_get_flags(dev); |
| 1478 | if (flags & (IFF_RUNNING | IFF_LOWER_UP)) |
| 1479 | fib_sync_up(dev, RTNH_F_LINKDOWN); |
| 1480 | else |
Julian Anastasov | 4f823de | 2015-10-30 10:23:33 +0200 | [diff] [blame] | 1481 | fib_sync_down_dev(dev, event, false); |
Sabrina Dubroca | af7d6cc | 2018-10-09 17:48:14 +0200 | [diff] [blame] | 1482 | rt_cache_flush(net); |
| 1483 | break; |
Andy Gospodarek | 8a3d031 | 2015-06-23 13:45:36 -0400 | [diff] [blame] | 1484 | case NETDEV_CHANGEMTU: |
Sabrina Dubroca | af7d6cc | 2018-10-09 17:48:14 +0200 | [diff] [blame] | 1485 | fib_sync_mtu(dev, info_ext->ext.mtu); |
Nicolas Dichtel | 4ccfe6d | 2012-09-07 00:45:29 +0000 | [diff] [blame] | 1486 | rt_cache_flush(net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1487 | break; |
David Ahern | 7f49e7a | 2015-12-10 10:25:24 -0800 | [diff] [blame] | 1488 | case NETDEV_CHANGEUPPER: |
Sabrina Dubroca | af7d6cc | 2018-10-09 17:48:14 +0200 | [diff] [blame] | 1489 | upper_info = ptr; |
David Ahern | 7f49e7a | 2015-12-10 10:25:24 -0800 | [diff] [blame] | 1490 | /* flush all routes if dev is linked to or unlinked from |
| 1491 | * an L3 master device (e.g., VRF) |
| 1492 | */ |
Sabrina Dubroca | af7d6cc | 2018-10-09 17:48:14 +0200 | [diff] [blame] | 1493 | if (upper_info->upper_dev && |
| 1494 | netif_is_l3_master(upper_info->upper_dev)) |
David Ahern | 7f49e7a | 2015-12-10 10:25:24 -0800 | [diff] [blame] | 1495 | fib_disable_ip(dev, NETDEV_DOWN, true); |
| 1496 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1497 | } |
| 1498 | return NOTIFY_DONE; |
| 1499 | } |
| 1500 | |
| 1501 | static struct notifier_block fib_inetaddr_notifier = { |
Jianjun Kong | 6ed2533e | 2008-11-03 00:25:16 -0800 | [diff] [blame] | 1502 | .notifier_call = fib_inetaddr_event, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1503 | }; |
| 1504 | |
| 1505 | static struct notifier_block fib_netdev_notifier = { |
Jianjun Kong | 6ed2533e | 2008-11-03 00:25:16 -0800 | [diff] [blame] | 1506 | .notifier_call = fib_netdev_event, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1507 | }; |
| 1508 | |
Denis V. Lunev | 7b1a74fd | 2008-01-10 03:22:17 -0800 | [diff] [blame] | 1509 | static int __net_init ip_fib_net_init(struct net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1510 | { |
Denis V. Lunev | dce5cbe | 2008-01-31 18:44:53 -0800 | [diff] [blame] | 1511 | int err; |
Eric Dumazet | 10da66f | 2010-10-13 08:22:03 +0000 | [diff] [blame] | 1512 | size_t size = sizeof(struct hlist_head) * FIB_TABLE_HASHSZ; |
Patrick McHardy | 1af5a8c | 2006-08-10 23:10:46 -0700 | [diff] [blame] | 1513 | |
Ido Schimmel | 04b1d4e | 2017-08-03 13:28:11 +0200 | [diff] [blame] | 1514 | err = fib4_notifier_init(net); |
| 1515 | if (err) |
| 1516 | return err; |
Ido Schimmel | cacaad1 | 2016-12-03 16:45:06 +0100 | [diff] [blame] | 1517 | |
Eric Dumazet | 10da66f | 2010-10-13 08:22:03 +0000 | [diff] [blame] | 1518 | /* 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 Schimmel | 04b1d4e | 2017-08-03 13:28:11 +0200 | [diff] [blame] | 1522 | if (!net->ipv4.fib_table_hash) { |
| 1523 | err = -ENOMEM; |
| 1524 | goto err_table_hash_alloc; |
| 1525 | } |
Denis V. Lunev | e4aef8a | 2008-01-10 03:28:24 -0800 | [diff] [blame] | 1526 | |
Denis V. Lunev | dce5cbe | 2008-01-31 18:44:53 -0800 | [diff] [blame] | 1527 | err = fib4_rules_init(net); |
| 1528 | if (err < 0) |
Ido Schimmel | 04b1d4e | 2017-08-03 13:28:11 +0200 | [diff] [blame] | 1529 | goto err_rules_init; |
Denis V. Lunev | dce5cbe | 2008-01-31 18:44:53 -0800 | [diff] [blame] | 1530 | return 0; |
| 1531 | |
Ido Schimmel | 04b1d4e | 2017-08-03 13:28:11 +0200 | [diff] [blame] | 1532 | err_rules_init: |
Denis V. Lunev | dce5cbe | 2008-01-31 18:44:53 -0800 | [diff] [blame] | 1533 | kfree(net->ipv4.fib_table_hash); |
Ido Schimmel | 04b1d4e | 2017-08-03 13:28:11 +0200 | [diff] [blame] | 1534 | err_table_hash_alloc: |
| 1535 | fib4_notifier_exit(net); |
Denis V. Lunev | dce5cbe | 2008-01-31 18:44:53 -0800 | [diff] [blame] | 1536 | return err; |
Denis V. Lunev | 7b1a74fd | 2008-01-10 03:22:17 -0800 | [diff] [blame] | 1537 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1538 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 1539 | static void ip_fib_net_exit(struct net *net) |
Denis V. Lunev | 7b1a74fd | 2008-01-10 03:22:17 -0800 | [diff] [blame] | 1540 | { |
Ido Schimmel | b4681c2 | 2017-12-20 19:34:19 +0200 | [diff] [blame] | 1541 | int i; |
Thomas Graf | 63f3444 | 2007-03-22 11:55:17 -0700 | [diff] [blame] | 1542 | |
Sabrina Dubroca | 6dede75 | 2015-03-10 21:03:54 +0100 | [diff] [blame] | 1543 | rtnl_lock(); |
Alexander Duyck | 6e47d6c | 2015-03-27 14:14:22 -0700 | [diff] [blame] | 1544 | #ifdef CONFIG_IP_MULTIPLE_TABLES |
Alexander Duyck | 6e47d6c | 2015-03-27 14:14:22 -0700 | [diff] [blame] | 1545 | RCU_INIT_POINTER(net->ipv4.fib_main, NULL); |
| 1546 | RCU_INIT_POINTER(net->ipv4.fib_default, NULL); |
| 1547 | #endif |
Ido Schimmel | b4681c2 | 2017-12-20 19:34:19 +0200 | [diff] [blame] | 1548 | /* 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 Duyck | a7e5353 | 2015-03-04 15:02:44 -0800 | [diff] [blame] | 1554 | struct hlist_head *head = &net->ipv4.fib_table_hash[i]; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1555 | struct hlist_node *tmp; |
Alexander Duyck | a7e5353 | 2015-03-04 15:02:44 -0800 | [diff] [blame] | 1556 | struct fib_table *tb; |
Denis V. Lunev | 7b1a74fd | 2008-01-10 03:22:17 -0800 | [diff] [blame] | 1557 | |
Alexander Duyck | a7e5353 | 2015-03-04 15:02:44 -0800 | [diff] [blame] | 1558 | hlist_for_each_entry_safe(tb, tmp, head, tb_hlist) { |
Alexander Duyck | a7e5353 | 2015-03-04 15:02:44 -0800 | [diff] [blame] | 1559 | hlist_del(&tb->tb_hlist); |
Ido Schimmel | f97f4dd | 2019-01-09 09:57:39 +0000 | [diff] [blame] | 1560 | fib_table_flush(net, tb, true); |
Pavel Emelyanov | 4aa2c46 | 2010-10-28 02:00:43 +0000 | [diff] [blame] | 1561 | fib_free_table(tb); |
Denis V. Lunev | 7b1a74fd | 2008-01-10 03:22:17 -0800 | [diff] [blame] | 1562 | } |
| 1563 | } |
Alexander Duyck | ad88d05 | 2015-03-27 14:14:16 -0700 | [diff] [blame] | 1564 | |
| 1565 | #ifdef CONFIG_IP_MULTIPLE_TABLES |
| 1566 | fib4_rules_exit(net); |
| 1567 | #endif |
Eric Dumazet | e2666f8 | 2011-03-30 16:57:46 -0700 | [diff] [blame] | 1568 | rtnl_unlock(); |
Denis V. Lunev | e4aef8a | 2008-01-10 03:28:24 -0800 | [diff] [blame] | 1569 | kfree(net->ipv4.fib_table_hash); |
Ido Schimmel | 04b1d4e | 2017-08-03 13:28:11 +0200 | [diff] [blame] | 1570 | fib4_notifier_exit(net); |
Denis V. Lunev | 7b1a74fd | 2008-01-10 03:22:17 -0800 | [diff] [blame] | 1571 | } |
| 1572 | |
| 1573 | static int __net_init fib_net_init(struct net *net) |
| 1574 | { |
| 1575 | int error; |
| 1576 | |
David S. Miller | f4530fa | 2012-07-05 22:13:13 -0700 | [diff] [blame] | 1577 | #ifdef CONFIG_IP_ROUTE_CLASSID |
| 1578 | net->ipv4.fib_num_tclassid_users = 0; |
| 1579 | #endif |
Denis V. Lunev | 7b1a74fd | 2008-01-10 03:22:17 -0800 | [diff] [blame] | 1580 | 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; |
| 1589 | out: |
| 1590 | return error; |
| 1591 | |
| 1592 | out_proc: |
| 1593 | nl_fib_lookup_exit(net); |
| 1594 | out_nlfl: |
| 1595 | ip_fib_net_exit(net); |
| 1596 | goto out; |
| 1597 | } |
| 1598 | |
| 1599 | static 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 | |
| 1606 | static struct pernet_operations fib_net_ops = { |
| 1607 | .init = fib_net_init, |
| 1608 | .exit = fib_net_exit, |
| 1609 | }; |
| 1610 | |
| 1611 | void __init ip_fib_init(void) |
| 1612 | { |
Mahesh Bandewar | 8799a22 | 2017-07-19 15:41:33 -0700 | [diff] [blame] | 1613 | fib_trie_init(); |
Denis V. Lunev | 7b1a74fd | 2008-01-10 03:22:17 -0800 | [diff] [blame] | 1614 | |
| 1615 | register_pernet_subsys(&fib_net_ops); |
Mahesh Bandewar | 8799a22 | 2017-07-19 15:41:33 -0700 | [diff] [blame] | 1616 | |
Denis V. Lunev | 7b1a74fd | 2008-01-10 03:22:17 -0800 | [diff] [blame] | 1617 | register_netdevice_notifier(&fib_netdev_notifier); |
| 1618 | register_inetaddr_notifier(&fib_inetaddr_notifier); |
Stephen Hemminger | 7f9b805 | 2008-01-14 23:14:20 -0800 | [diff] [blame] | 1619 | |
Florian Westphal | b97bac6 | 2017-08-09 20:41:48 +0200 | [diff] [blame] | 1620 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1623 | } |