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 | 1fd1c76 | 2020-05-26 12:56:18 -0600 | [diff] [blame] | 312 | if (unlikely(fi->nh)) { |
| 313 | dev_match = nexthop_uses_dev(fi->nh, dev); |
| 314 | } else { |
| 315 | int ret; |
David Ahern | 78f2756 | 2018-09-20 13:50:47 -0700 | [diff] [blame] | 316 | |
David Ahern | 1fd1c76 | 2020-05-26 12:56:18 -0600 | [diff] [blame] | 317 | for (ret = 0; ret < fib_info_num_path(fi); ret++) { |
| 318 | const struct fib_nh_common *nhc = fib_info_nhc(fi, ret); |
David Ahern | 78f2756 | 2018-09-20 13:50:47 -0700 | [diff] [blame] | 319 | |
David Ahern | 1fd1c76 | 2020-05-26 12:56:18 -0600 | [diff] [blame] | 320 | if (nhc_l3mdev_matches_dev(nhc, dev)) { |
| 321 | dev_match = true; |
| 322 | break; |
| 323 | } |
David Ahern | 78f2756 | 2018-09-20 13:50:47 -0700 | [diff] [blame] | 324 | } |
| 325 | } |
| 326 | #else |
David Ahern | dcb1ecb | 2019-06-03 20:19:50 -0700 | [diff] [blame] | 327 | if (fib_info_nhc(fi, 0)->nhc_dev == dev) |
David Ahern | 78f2756 | 2018-09-20 13:50:47 -0700 | [diff] [blame] | 328 | dev_match = true; |
| 329 | #endif |
| 330 | |
| 331 | return dev_match; |
| 332 | } |
| 333 | EXPORT_SYMBOL_GPL(fib_info_nh_uses_dev); |
| 334 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | /* Given (packet source, input interface) and optional (dst, oif, tos): |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 336 | * - (main) check, that source is valid i.e. not broadcast or our local |
| 337 | * address. |
| 338 | * - figure out what "logical" interface this packet arrived |
| 339 | * and calculate "specific destination" address. |
| 340 | * - check, that packet arrived from expected physical interface. |
Eric Dumazet | ebc0ffa | 2010-10-05 10:41:36 +0000 | [diff] [blame] | 341 | * called with rcu_read_lock() |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | */ |
David S. Miller | 7a9bc9b | 2012-06-29 01:32:45 -0700 | [diff] [blame] | 343 | static int __fib_validate_source(struct sk_buff *skb, __be32 src, __be32 dst, |
| 344 | u8 tos, int oif, struct net_device *dev, |
| 345 | int rpf, struct in_device *idev, u32 *itag) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | { |
David Ahern | 5a847a6 | 2018-05-16 13:36:40 -0700 | [diff] [blame] | 347 | struct net *net = dev_net(dev); |
| 348 | struct flow_keys flkeys; |
Sébastien Barré | 1dced6a | 2014-08-17 09:19:54 +0200 | [diff] [blame] | 349 | int ret, no_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | struct fib_result res; |
David S. Miller | 9e56e38 | 2012-06-28 18:54:02 -0700 | [diff] [blame] | 351 | struct flowi4 fl4; |
David S. Miller | 9e56e38 | 2012-06-28 18:54:02 -0700 | [diff] [blame] | 352 | bool dev_match; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | |
David S. Miller | 9ade228 | 2011-03-12 02:02:42 -0500 | [diff] [blame] | 354 | fl4.flowi4_oif = 0; |
David Ahern | 385add9 | 2015-09-29 20:07:13 -0700 | [diff] [blame] | 355 | fl4.flowi4_iif = l3mdev_master_ifindex_rcu(dev); |
David Ahern | cd2fbe1 | 2015-08-13 14:59:01 -0600 | [diff] [blame] | 356 | if (!fl4.flowi4_iif) |
| 357 | fl4.flowi4_iif = oif ? : LOOPBACK_IFINDEX; |
David S. Miller | 9ade228 | 2011-03-12 02:02:42 -0500 | [diff] [blame] | 358 | fl4.daddr = src; |
| 359 | fl4.saddr = dst; |
| 360 | fl4.flowi4_tos = tos; |
| 361 | fl4.flowi4_scope = RT_SCOPE_UNIVERSE; |
Thomas Graf | 1b7179d | 2015-07-21 10:43:59 +0200 | [diff] [blame] | 362 | fl4.flowi4_tun_key.tun_id = 0; |
David Ahern | b84f787 | 2015-09-29 19:07:07 -0700 | [diff] [blame] | 363 | fl4.flowi4_flags = 0; |
Julian Anastasov | 8bcfd09 | 2017-02-26 15:50:52 +0200 | [diff] [blame] | 364 | fl4.flowi4_uid = sock_net_uid(net, NULL); |
David Ahern | 1869e22 | 2020-09-13 12:43:39 -0600 | [diff] [blame] | 365 | fl4.flowi4_multipath_hash = 0; |
David S. Miller | cc7e17e | 2011-03-09 20:57:50 -0800 | [diff] [blame] | 366 | |
David S. Miller | 9e56e38 | 2012-06-28 18:54:02 -0700 | [diff] [blame] | 367 | no_addr = idev->ifa_list == NULL; |
Michael Smith | 990078a | 2011-04-07 04:51:51 +0000 | [diff] [blame] | 368 | |
David S. Miller | 9e56e38 | 2012-06-28 18:54:02 -0700 | [diff] [blame] | 369 | fl4.flowi4_mark = IN_DEV_SRC_VMARK(idev) ? skb->mark : 0; |
David Ahern | 5a847a6 | 2018-05-16 13:36:40 -0700 | [diff] [blame] | 370 | if (!fib4_rules_early_flow_dissect(net, skb, &fl4, &flkeys)) { |
| 371 | fl4.flowi4_proto = 0; |
| 372 | fl4.fl4_sport = 0; |
| 373 | fl4.fl4_dport = 0; |
| 374 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | |
Andy Gospodarek | 0eeb075 | 2015-06-23 13:45:37 -0400 | [diff] [blame] | 376 | if (fib_lookup(net, &fl4, &res, 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | goto last_resort; |
Sébastien Barré | 1dced6a | 2014-08-17 09:19:54 +0200 | [diff] [blame] | 378 | if (res.type != RTN_UNICAST && |
| 379 | (res.type != RTN_LOCAL || !IN_DEV_ACCEPT_LOCAL(idev))) |
| 380 | goto e_inval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | fib_combine_itag(itag, &res); |
David S. Miller | 6f86b32 | 2010-09-06 22:36:19 -0700 | [diff] [blame] | 382 | |
David Ahern | 78f2756 | 2018-09-20 13:50:47 -0700 | [diff] [blame] | 383 | dev_match = fib_info_nh_uses_dev(res.fi, dev); |
Cong Wang | 66f8209 | 2019-07-17 14:41:58 -0700 | [diff] [blame] | 384 | /* This is not common, loopback packets retain skb_dst so normally they |
| 385 | * would not even hit this slow path. |
| 386 | */ |
| 387 | dev_match = dev_match || (res.type == RTN_LOCAL && |
| 388 | dev == net->loopback_dev); |
David S. Miller | 6f86b32 | 2010-09-06 22:36:19 -0700 | [diff] [blame] | 389 | if (dev_match) { |
David Ahern | eba618a | 2019-04-02 14:11:55 -0700 | [diff] [blame] | 390 | ret = FIB_RES_NHC(res)->nhc_scope >= RT_SCOPE_HOST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | return ret; |
| 392 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | if (no_addr) |
| 394 | goto last_resort; |
Stephen Hemminger | c1cf842 | 2009-02-20 08:25:36 +0000 | [diff] [blame] | 395 | if (rpf == 1) |
Eric Dumazet | b5f7e75 | 2010-06-02 12:05:27 +0000 | [diff] [blame] | 396 | goto e_rpf; |
David S. Miller | 9ade228 | 2011-03-12 02:02:42 -0500 | [diff] [blame] | 397 | fl4.flowi4_oif = dev->ifindex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | |
| 399 | ret = 0; |
Andy Gospodarek | 0eeb075 | 2015-06-23 13:45:37 -0400 | [diff] [blame] | 400 | if (fib_lookup(net, &fl4, &res, FIB_LOOKUP_IGNORE_LINKSTATE) == 0) { |
David S. Miller | 41347dc | 2012-06-28 04:05:27 -0700 | [diff] [blame] | 401 | if (res.type == RTN_UNICAST) |
David Ahern | eba618a | 2019-04-02 14:11:55 -0700 | [diff] [blame] | 402 | ret = FIB_RES_NHC(res)->nhc_scope >= RT_SCOPE_HOST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | } |
| 404 | return ret; |
| 405 | |
| 406 | last_resort: |
| 407 | if (rpf) |
Eric Dumazet | b5f7e75 | 2010-06-02 12:05:27 +0000 | [diff] [blame] | 408 | goto e_rpf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | *itag = 0; |
| 410 | return 0; |
| 411 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | e_inval: |
| 413 | return -EINVAL; |
Eric Dumazet | b5f7e75 | 2010-06-02 12:05:27 +0000 | [diff] [blame] | 414 | e_rpf: |
| 415 | return -EXDEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | } |
| 417 | |
David S. Miller | 7a9bc9b | 2012-06-29 01:32:45 -0700 | [diff] [blame] | 418 | /* Ignore rp_filter for packets protected by IPsec. */ |
| 419 | int fib_validate_source(struct sk_buff *skb, __be32 src, __be32 dst, |
| 420 | u8 tos, int oif, struct net_device *dev, |
| 421 | struct in_device *idev, u32 *itag) |
| 422 | { |
| 423 | int r = secpath_exists(skb) ? 0 : IN_DEV_RPFILTER(idev); |
Paolo Abeni | 6e617de | 2017-09-20 18:26:53 +0200 | [diff] [blame] | 424 | struct net *net = dev_net(dev); |
David S. Miller | 7a9bc9b | 2012-06-29 01:32:45 -0700 | [diff] [blame] | 425 | |
Paolo Abeni | 6e617de | 2017-09-20 18:26:53 +0200 | [diff] [blame] | 426 | if (!r && !fib_num_tclassid_users(net) && |
Julian Anastasov | e81da0e | 2012-10-08 11:41:15 +0000 | [diff] [blame] | 427 | (dev->ifindex != oif || !IN_DEV_TX_REDIRECTS(idev))) { |
Paolo Abeni | 6e617de | 2017-09-20 18:26:53 +0200 | [diff] [blame] | 428 | if (IN_DEV_ACCEPT_LOCAL(idev)) |
| 429 | goto ok; |
Paolo Abeni | 032a480 | 2017-10-31 14:32:38 +0100 | [diff] [blame] | 430 | /* with custom local routes in place, checking local addresses |
| 431 | * only will be too optimistic, with custom rules, checking |
| 432 | * local addresses only can be too strict, e.g. due to vrf |
Paolo Abeni | 6e617de | 2017-09-20 18:26:53 +0200 | [diff] [blame] | 433 | */ |
Paolo Abeni | 032a480 | 2017-10-31 14:32:38 +0100 | [diff] [blame] | 434 | if (net->ipv4.fib_has_custom_local_routes || |
| 435 | fib4_has_custom_rules(net)) |
Paolo Abeni | 6e617de | 2017-09-20 18:26:53 +0200 | [diff] [blame] | 436 | goto full_check; |
| 437 | if (inet_lookup_ifaddr_rcu(net, src)) |
| 438 | return -EINVAL; |
| 439 | |
| 440 | ok: |
David S. Miller | 7a9bc9b | 2012-06-29 01:32:45 -0700 | [diff] [blame] | 441 | *itag = 0; |
| 442 | return 0; |
| 443 | } |
Paolo Abeni | 6e617de | 2017-09-20 18:26:53 +0200 | [diff] [blame] | 444 | |
| 445 | full_check: |
David S. Miller | 7a9bc9b | 2012-06-29 01:32:45 -0700 | [diff] [blame] | 446 | return __fib_validate_source(skb, src, dst, tos, oif, dev, r, idev, itag); |
| 447 | } |
| 448 | |
Al Viro | 81f7bf6 | 2006-09-27 18:40:00 -0700 | [diff] [blame] | 449 | static inline __be32 sk_extract_addr(struct sockaddr *addr) |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 450 | { |
| 451 | return ((struct sockaddr_in *) addr)->sin_addr.s_addr; |
| 452 | } |
| 453 | |
| 454 | static int put_rtax(struct nlattr *mx, int len, int type, u32 value) |
| 455 | { |
| 456 | struct nlattr *nla; |
| 457 | |
| 458 | nla = (struct nlattr *) ((char *) mx + len); |
| 459 | nla->nla_type = type; |
| 460 | nla->nla_len = nla_attr_size(4); |
| 461 | *(u32 *) nla_data(nla) = value; |
| 462 | |
| 463 | return len + nla_total_size(4); |
| 464 | } |
| 465 | |
Denis V. Lunev | 4b5d47d | 2008-01-10 03:29:23 -0800 | [diff] [blame] | 466 | 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] | 467 | struct fib_config *cfg) |
| 468 | { |
Al Viro | 6d85c10 | 2006-09-26 22:15:46 -0700 | [diff] [blame] | 469 | __be32 addr; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 470 | int plen; |
| 471 | |
| 472 | memset(cfg, 0, sizeof(*cfg)); |
Denis V. Lunev | 4b5d47d | 2008-01-10 03:29:23 -0800 | [diff] [blame] | 473 | cfg->fc_nlinfo.nl_net = net; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 474 | |
| 475 | if (rt->rt_dst.sa_family != AF_INET) |
| 476 | return -EAFNOSUPPORT; |
| 477 | |
| 478 | /* |
| 479 | * Check mask for validity: |
| 480 | * a) it must be contiguous. |
| 481 | * b) destination must have all host bits clear. |
| 482 | * c) if application forgot to set correct family (AF_INET), |
| 483 | * reject request unless it is absolutely clear i.e. |
| 484 | * both family and mask are zero. |
| 485 | */ |
| 486 | plen = 32; |
| 487 | addr = sk_extract_addr(&rt->rt_dst); |
| 488 | if (!(rt->rt_flags & RTF_HOST)) { |
Al Viro | 81f7bf6 | 2006-09-27 18:40:00 -0700 | [diff] [blame] | 489 | __be32 mask = sk_extract_addr(&rt->rt_genmask); |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 490 | |
| 491 | if (rt->rt_genmask.sa_family != AF_INET) { |
| 492 | if (mask || rt->rt_genmask.sa_family) |
| 493 | return -EAFNOSUPPORT; |
| 494 | } |
| 495 | |
| 496 | if (bad_mask(mask, addr)) |
| 497 | return -EINVAL; |
| 498 | |
| 499 | plen = inet_mask_len(mask); |
| 500 | } |
| 501 | |
| 502 | cfg->fc_dst_len = plen; |
| 503 | cfg->fc_dst = addr; |
| 504 | |
| 505 | if (cmd != SIOCDELRT) { |
| 506 | cfg->fc_nlflags = NLM_F_CREATE; |
| 507 | cfg->fc_protocol = RTPROT_BOOT; |
| 508 | } |
| 509 | |
| 510 | if (rt->rt_metric) |
| 511 | cfg->fc_priority = rt->rt_metric - 1; |
| 512 | |
| 513 | if (rt->rt_flags & RTF_REJECT) { |
| 514 | cfg->fc_scope = RT_SCOPE_HOST; |
| 515 | cfg->fc_type = RTN_UNREACHABLE; |
| 516 | return 0; |
| 517 | } |
| 518 | |
| 519 | cfg->fc_scope = RT_SCOPE_NOWHERE; |
| 520 | cfg->fc_type = RTN_UNICAST; |
| 521 | |
| 522 | if (rt->rt_dev) { |
| 523 | char *colon; |
| 524 | struct net_device *dev; |
| 525 | char devname[IFNAMSIZ]; |
| 526 | |
| 527 | if (copy_from_user(devname, rt->rt_dev, IFNAMSIZ-1)) |
| 528 | return -EFAULT; |
| 529 | |
| 530 | devname[IFNAMSIZ-1] = 0; |
| 531 | colon = strchr(devname, ':'); |
| 532 | if (colon) |
| 533 | *colon = 0; |
Denis V. Lunev | 4b5d47d | 2008-01-10 03:29:23 -0800 | [diff] [blame] | 534 | dev = __dev_get_by_name(net, devname); |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 535 | if (!dev) |
| 536 | return -ENODEV; |
| 537 | cfg->fc_oif = dev->ifindex; |
Mark Tomlinson | 5a56a0b | 2016-09-05 10:20:20 +1200 | [diff] [blame] | 538 | cfg->fc_table = l3mdev_fib_table(dev); |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 539 | if (colon) { |
Florian Westphal | cd5a411 | 2019-05-31 18:27:07 +0200 | [diff] [blame] | 540 | const struct in_ifaddr *ifa; |
| 541 | struct in_device *in_dev; |
| 542 | |
| 543 | in_dev = __in_dev_get_rtnl(dev); |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 544 | if (!in_dev) |
| 545 | return -ENODEV; |
Florian Westphal | cd5a411 | 2019-05-31 18:27:07 +0200 | [diff] [blame] | 546 | |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 547 | *colon = ':'; |
Florian Westphal | cd5a411 | 2019-05-31 18:27:07 +0200 | [diff] [blame] | 548 | |
| 549 | rcu_read_lock(); |
| 550 | in_dev_for_each_ifa_rcu(ifa, in_dev) { |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 551 | if (strcmp(ifa->ifa_label, devname) == 0) |
| 552 | break; |
Florian Westphal | cd5a411 | 2019-05-31 18:27:07 +0200 | [diff] [blame] | 553 | } |
| 554 | rcu_read_unlock(); |
| 555 | |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 556 | if (!ifa) |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 557 | return -ENODEV; |
| 558 | cfg->fc_prefsrc = ifa->ifa_local; |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | addr = sk_extract_addr(&rt->rt_gateway); |
| 563 | if (rt->rt_gateway.sa_family == AF_INET && addr) { |
David Ahern | 30bbaa1 | 2015-08-13 14:59:05 -0600 | [diff] [blame] | 564 | unsigned int addr_type; |
| 565 | |
David Ahern | f35b794 | 2019-04-05 16:30:28 -0700 | [diff] [blame] | 566 | cfg->fc_gw4 = addr; |
| 567 | cfg->fc_gw_family = AF_INET; |
David Ahern | 30bbaa1 | 2015-08-13 14:59:05 -0600 | [diff] [blame] | 568 | addr_type = inet_addr_type_table(net, addr, cfg->fc_table); |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 569 | if (rt->rt_flags & RTF_GATEWAY && |
David Ahern | 30bbaa1 | 2015-08-13 14:59:05 -0600 | [diff] [blame] | 570 | addr_type == RTN_UNICAST) |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 571 | cfg->fc_scope = RT_SCOPE_UNIVERSE; |
| 572 | } |
| 573 | |
| 574 | if (cmd == SIOCDELRT) |
| 575 | return 0; |
| 576 | |
David Ahern | f35b794 | 2019-04-05 16:30:28 -0700 | [diff] [blame] | 577 | if (rt->rt_flags & RTF_GATEWAY && !cfg->fc_gw_family) |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 578 | return -EINVAL; |
| 579 | |
| 580 | if (cfg->fc_scope == RT_SCOPE_NOWHERE) |
| 581 | cfg->fc_scope = RT_SCOPE_LINK; |
| 582 | |
| 583 | if (rt->rt_flags & (RTF_MTU | RTF_WINDOW | RTF_IRTT)) { |
| 584 | struct nlattr *mx; |
| 585 | int len = 0; |
| 586 | |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 587 | mx = kcalloc(3, nla_total_size(4), GFP_KERNEL); |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 588 | if (!mx) |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 589 | return -ENOMEM; |
| 590 | |
| 591 | if (rt->rt_flags & RTF_MTU) |
| 592 | len = put_rtax(mx, len, RTAX_ADVMSS, rt->rt_mtu - 40); |
| 593 | |
| 594 | if (rt->rt_flags & RTF_WINDOW) |
| 595 | len = put_rtax(mx, len, RTAX_WINDOW, rt->rt_window); |
| 596 | |
| 597 | if (rt->rt_flags & RTF_IRTT) |
| 598 | len = put_rtax(mx, len, RTAX_RTT, rt->rt_irtt << 3); |
| 599 | |
| 600 | cfg->fc_mx = mx; |
| 601 | cfg->fc_mx_len = len; |
| 602 | } |
| 603 | |
| 604 | return 0; |
| 605 | } |
| 606 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | /* |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 608 | * Handle IP routing ioctl calls. |
| 609 | * These are used to manipulate the routing tables |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | */ |
Al Viro | ca25c30 | 2017-07-01 08:03:10 -0400 | [diff] [blame] | 611 | 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] | 612 | { |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 613 | struct fib_config cfg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | |
| 616 | switch (cmd) { |
| 617 | case SIOCADDRT: /* Add a route */ |
| 618 | case SIOCDELRT: /* Delete a route */ |
Eric W. Biederman | 52e804c | 2012-11-16 03:03:05 +0000 | [diff] [blame] | 619 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | return -EPERM; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 621 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | rtnl_lock(); |
Al Viro | ca25c30 | 2017-07-01 08:03:10 -0400 | [diff] [blame] | 623 | err = rtentry_to_fib_config(net, cmd, rt, &cfg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | if (err == 0) { |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 625 | struct fib_table *tb; |
| 626 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | if (cmd == SIOCDELRT) { |
Denis V. Lunev | 1bad118 | 2008-01-10 03:29:53 -0800 | [diff] [blame] | 628 | tb = fib_get_table(net, cfg.fc_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | if (tb) |
David Ahern | 7805599 | 2017-05-27 16:19:26 -0600 | [diff] [blame] | 630 | err = fib_table_delete(net, tb, &cfg, |
| 631 | NULL); |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 632 | else |
| 633 | err = -ESRCH; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | } else { |
Denis V. Lunev | 1bad118 | 2008-01-10 03:29:53 -0800 | [diff] [blame] | 635 | tb = fib_new_table(net, cfg.fc_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | if (tb) |
David Ahern | 6d8422a1 | 2017-05-21 10:12:02 -0600 | [diff] [blame] | 637 | err = fib_table_insert(net, tb, |
| 638 | &cfg, NULL); |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 639 | else |
| 640 | err = -ENOBUFS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | } |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 642 | |
| 643 | /* allocated by rtentry_to_fib_config() */ |
| 644 | kfree(cfg.fc_mx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | } |
| 646 | rtnl_unlock(); |
| 647 | return err; |
| 648 | } |
| 649 | return -EINVAL; |
| 650 | } |
| 651 | |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 652 | const struct nla_policy rtm_ipv4_policy[RTA_MAX + 1] = { |
David Ahern | 7542565 | 2019-05-22 12:07:43 -0700 | [diff] [blame] | 653 | [RTA_UNSPEC] = { .strict_start_type = RTA_DPORT + 1 }, |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 654 | [RTA_DST] = { .type = NLA_U32 }, |
| 655 | [RTA_SRC] = { .type = NLA_U32 }, |
| 656 | [RTA_IIF] = { .type = NLA_U32 }, |
| 657 | [RTA_OIF] = { .type = NLA_U32 }, |
| 658 | [RTA_GATEWAY] = { .type = NLA_U32 }, |
| 659 | [RTA_PRIORITY] = { .type = NLA_U32 }, |
| 660 | [RTA_PREFSRC] = { .type = NLA_U32 }, |
| 661 | [RTA_METRICS] = { .type = NLA_NESTED }, |
Thomas Graf | 5176f91 | 2006-08-26 20:13:18 -0700 | [diff] [blame] | 662 | [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) }, |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 663 | [RTA_FLOW] = { .type = NLA_U32 }, |
Roopa Prabhu | 571e722 | 2015-07-21 10:43:47 +0200 | [diff] [blame] | 664 | [RTA_ENCAP_TYPE] = { .type = NLA_U16 }, |
| 665 | [RTA_ENCAP] = { .type = NLA_NESTED }, |
Lorenzo Colitti | 622ec2c | 2016-11-04 02:23:42 +0900 | [diff] [blame] | 666 | [RTA_UID] = { .type = NLA_U32 }, |
Liping Zhang | 3b45a41 | 2017-02-27 20:59:39 +0800 | [diff] [blame] | 667 | [RTA_MARK] = { .type = NLA_U32 }, |
Roopa Prabhu | 2eabd76 | 2018-05-22 13:44:51 -0700 | [diff] [blame] | 668 | [RTA_TABLE] = { .type = NLA_U32 }, |
Roopa Prabhu | 404eb77 | 2018-05-22 14:03:27 -0700 | [diff] [blame] | 669 | [RTA_IP_PROTO] = { .type = NLA_U8 }, |
| 670 | [RTA_SPORT] = { .type = NLA_U16 }, |
| 671 | [RTA_DPORT] = { .type = NLA_U16 }, |
David Ahern | 493ced1 | 2019-06-08 14:53:32 -0700 | [diff] [blame] | 672 | [RTA_NH_ID] = { .type = NLA_U32 }, |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 673 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | |
David Ahern | d156626 | 2019-04-05 16:30:40 -0700 | [diff] [blame] | 675 | int fib_gw_from_via(struct fib_config *cfg, struct nlattr *nla, |
| 676 | struct netlink_ext_ack *extack) |
| 677 | { |
| 678 | struct rtvia *via; |
| 679 | int alen; |
| 680 | |
| 681 | if (nla_len(nla) < offsetof(struct rtvia, rtvia_addr)) { |
| 682 | NL_SET_ERR_MSG(extack, "Invalid attribute length for RTA_VIA"); |
| 683 | return -EINVAL; |
| 684 | } |
| 685 | |
| 686 | via = nla_data(nla); |
| 687 | alen = nla_len(nla) - offsetof(struct rtvia, rtvia_addr); |
| 688 | |
| 689 | switch (via->rtvia_family) { |
| 690 | case AF_INET: |
| 691 | if (alen != sizeof(__be32)) { |
| 692 | NL_SET_ERR_MSG(extack, "Invalid IPv4 address in RTA_VIA"); |
| 693 | return -EINVAL; |
| 694 | } |
| 695 | cfg->fc_gw_family = AF_INET; |
| 696 | cfg->fc_gw4 = *((__be32 *)via->rtvia_addr); |
| 697 | break; |
| 698 | case AF_INET6: |
| 699 | #ifdef CONFIG_IPV6 |
| 700 | if (alen != sizeof(struct in6_addr)) { |
| 701 | NL_SET_ERR_MSG(extack, "Invalid IPv6 address in RTA_VIA"); |
| 702 | return -EINVAL; |
| 703 | } |
| 704 | cfg->fc_gw_family = AF_INET6; |
| 705 | cfg->fc_gw6 = *((struct in6_addr *)via->rtvia_addr); |
| 706 | #else |
| 707 | NL_SET_ERR_MSG(extack, "IPv6 support not enabled in kernel"); |
| 708 | return -EINVAL; |
| 709 | #endif |
| 710 | break; |
| 711 | default: |
| 712 | NL_SET_ERR_MSG(extack, "Unsupported address family in RTA_VIA"); |
| 713 | return -EINVAL; |
| 714 | } |
| 715 | |
| 716 | return 0; |
| 717 | } |
| 718 | |
Denis V. Lunev | 4b5d47d | 2008-01-10 03:29:23 -0800 | [diff] [blame] | 719 | 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] | 720 | struct nlmsghdr *nlh, struct fib_config *cfg, |
| 721 | struct netlink_ext_ack *extack) |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 722 | { |
David Ahern | d156626 | 2019-04-05 16:30:40 -0700 | [diff] [blame] | 723 | bool has_gw = false, has_via = false; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 724 | struct nlattr *attr; |
| 725 | int err, remaining; |
| 726 | struct rtmsg *rtm; |
| 727 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 728 | err = nlmsg_validate_deprecated(nlh, sizeof(*rtm), RTA_MAX, |
| 729 | rtm_ipv4_policy, extack); |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 730 | if (err < 0) |
| 731 | goto errout; |
| 732 | |
| 733 | memset(cfg, 0, sizeof(*cfg)); |
| 734 | |
| 735 | rtm = nlmsg_data(nlh); |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 736 | cfg->fc_dst_len = rtm->rtm_dst_len; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 737 | cfg->fc_tos = rtm->rtm_tos; |
| 738 | cfg->fc_table = rtm->rtm_table; |
| 739 | cfg->fc_protocol = rtm->rtm_protocol; |
| 740 | cfg->fc_scope = rtm->rtm_scope; |
| 741 | cfg->fc_type = rtm->rtm_type; |
| 742 | cfg->fc_flags = rtm->rtm_flags; |
| 743 | cfg->fc_nlflags = nlh->nlmsg_flags; |
| 744 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 745 | cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 746 | cfg->fc_nlinfo.nlh = nlh; |
Denis V. Lunev | 4b5d47d | 2008-01-10 03:29:23 -0800 | [diff] [blame] | 747 | cfg->fc_nlinfo.nl_net = net; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 748 | |
Thomas Graf | a0ee18b | 2007-03-24 20:32:54 -0700 | [diff] [blame] | 749 | if (cfg->fc_type > RTN_MAX) { |
David Ahern | c3ab2b4 | 2017-05-21 10:12:03 -0600 | [diff] [blame] | 750 | NL_SET_ERR_MSG(extack, "Invalid route type"); |
Thomas Graf | a0ee18b | 2007-03-24 20:32:54 -0700 | [diff] [blame] | 751 | err = -EINVAL; |
| 752 | goto errout; |
| 753 | } |
| 754 | |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 755 | nlmsg_for_each_attr(attr, nlh, sizeof(struct rtmsg), remaining) { |
Thomas Graf | 8f4c1f9 | 2007-09-12 14:44:36 +0200 | [diff] [blame] | 756 | switch (nla_type(attr)) { |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 757 | case RTA_DST: |
Al Viro | 17fb2c6 | 2006-09-26 22:15:25 -0700 | [diff] [blame] | 758 | cfg->fc_dst = nla_get_be32(attr); |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 759 | break; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 760 | case RTA_OIF: |
| 761 | cfg->fc_oif = nla_get_u32(attr); |
| 762 | break; |
| 763 | case RTA_GATEWAY: |
David Ahern | d156626 | 2019-04-05 16:30:40 -0700 | [diff] [blame] | 764 | has_gw = true; |
David Ahern | f35b794 | 2019-04-05 16:30:28 -0700 | [diff] [blame] | 765 | cfg->fc_gw4 = nla_get_be32(attr); |
David Ahern | d73f80f | 2019-04-10 10:05:51 -0700 | [diff] [blame] | 766 | if (cfg->fc_gw4) |
| 767 | cfg->fc_gw_family = AF_INET; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 768 | break; |
David Ahern | b6e9e5d | 2019-02-26 09:00:02 -0800 | [diff] [blame] | 769 | case RTA_VIA: |
David Ahern | d156626 | 2019-04-05 16:30:40 -0700 | [diff] [blame] | 770 | has_via = true; |
| 771 | err = fib_gw_from_via(cfg, attr, extack); |
| 772 | if (err) |
| 773 | goto errout; |
| 774 | break; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 775 | case RTA_PRIORITY: |
| 776 | cfg->fc_priority = nla_get_u32(attr); |
| 777 | break; |
| 778 | case RTA_PREFSRC: |
Al Viro | 17fb2c6 | 2006-09-26 22:15:25 -0700 | [diff] [blame] | 779 | cfg->fc_prefsrc = nla_get_be32(attr); |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 780 | break; |
| 781 | case RTA_METRICS: |
| 782 | cfg->fc_mx = nla_data(attr); |
| 783 | cfg->fc_mx_len = nla_len(attr); |
| 784 | break; |
| 785 | case RTA_MULTIPATH: |
David Ahern | 9ed5959 | 2017-01-17 14:57:36 -0800 | [diff] [blame] | 786 | err = lwtunnel_valid_encap_type_attr(nla_data(attr), |
David Ahern | c255bd6 | 2017-05-27 16:19:27 -0600 | [diff] [blame] | 787 | nla_len(attr), |
| 788 | extack); |
David Ahern | 9ed5959 | 2017-01-17 14:57:36 -0800 | [diff] [blame] | 789 | if (err < 0) |
| 790 | goto errout; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 791 | cfg->fc_mp = nla_data(attr); |
| 792 | cfg->fc_mp_len = nla_len(attr); |
| 793 | break; |
| 794 | case RTA_FLOW: |
| 795 | cfg->fc_flow = nla_get_u32(attr); |
| 796 | break; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 797 | case RTA_TABLE: |
| 798 | cfg->fc_table = nla_get_u32(attr); |
| 799 | break; |
Roopa Prabhu | 571e722 | 2015-07-21 10:43:47 +0200 | [diff] [blame] | 800 | case RTA_ENCAP: |
| 801 | cfg->fc_encap = attr; |
| 802 | break; |
| 803 | case RTA_ENCAP_TYPE: |
| 804 | cfg->fc_encap_type = nla_get_u16(attr); |
David Ahern | c255bd6 | 2017-05-27 16:19:27 -0600 | [diff] [blame] | 805 | err = lwtunnel_valid_encap_type(cfg->fc_encap_type, |
| 806 | extack); |
David Ahern | 9ed5959 | 2017-01-17 14:57:36 -0800 | [diff] [blame] | 807 | if (err < 0) |
| 808 | goto errout; |
Roopa Prabhu | 571e722 | 2015-07-21 10:43:47 +0200 | [diff] [blame] | 809 | break; |
David Ahern | 493ced1 | 2019-06-08 14:53:32 -0700 | [diff] [blame] | 810 | case RTA_NH_ID: |
| 811 | cfg->fc_nh_id = nla_get_u32(attr); |
| 812 | break; |
| 813 | } |
| 814 | } |
| 815 | |
| 816 | if (cfg->fc_nh_id) { |
| 817 | if (cfg->fc_oif || cfg->fc_gw_family || |
| 818 | cfg->fc_encap || cfg->fc_mp) { |
| 819 | NL_SET_ERR_MSG(extack, |
| 820 | "Nexthop specification and nexthop id are mutually exclusive"); |
| 821 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | } |
| 823 | } |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 824 | |
David Ahern | d156626 | 2019-04-05 16:30:40 -0700 | [diff] [blame] | 825 | if (has_gw && has_via) { |
| 826 | NL_SET_ERR_MSG(extack, |
| 827 | "Nexthop configuration can not contain both GATEWAY and VIA"); |
| 828 | goto errout; |
| 829 | } |
| 830 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | return 0; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 832 | errout: |
| 833 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | } |
| 835 | |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 836 | static int inet_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 837 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 839 | struct net *net = sock_net(skb->sk); |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 840 | struct fib_config cfg; |
| 841 | struct fib_table *tb; |
| 842 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | |
David Ahern | 6d8422a1 | 2017-05-21 10:12:02 -0600 | [diff] [blame] | 844 | err = rtm_to_fib_config(net, skb, nlh, &cfg, extack); |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 845 | if (err < 0) |
| 846 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | |
David Ahern | 493ced1 | 2019-06-08 14:53:32 -0700 | [diff] [blame] | 848 | if (cfg.fc_nh_id && !nexthop_find_by_id(net, cfg.fc_nh_id)) { |
| 849 | NL_SET_ERR_MSG(extack, "Nexthop id does not exist"); |
| 850 | err = -EINVAL; |
| 851 | goto errout; |
| 852 | } |
| 853 | |
Denis V. Lunev | 8ad4942 | 2008-01-10 03:24:11 -0800 | [diff] [blame] | 854 | tb = fib_get_table(net, cfg.fc_table); |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 855 | if (!tb) { |
David Ahern | c3ab2b4 | 2017-05-21 10:12:03 -0600 | [diff] [blame] | 856 | NL_SET_ERR_MSG(extack, "FIB table does not exist"); |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 857 | err = -ESRCH; |
| 858 | goto errout; |
| 859 | } |
| 860 | |
David Ahern | 7805599 | 2017-05-27 16:19:26 -0600 | [diff] [blame] | 861 | err = fib_table_delete(net, tb, &cfg, extack); |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 862 | errout: |
| 863 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | } |
| 865 | |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 866 | static int inet_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 867 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 869 | struct net *net = sock_net(skb->sk); |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 870 | struct fib_config cfg; |
| 871 | struct fib_table *tb; |
| 872 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | |
David Ahern | 6d8422a1 | 2017-05-21 10:12:02 -0600 | [diff] [blame] | 874 | err = rtm_to_fib_config(net, skb, nlh, &cfg, extack); |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 875 | if (err < 0) |
| 876 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | |
Denis V. Lunev | 226b0b4a5 | 2008-01-10 03:30:24 -0800 | [diff] [blame] | 878 | tb = fib_new_table(net, cfg.fc_table); |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 879 | if (!tb) { |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 880 | err = -ENOBUFS; |
| 881 | goto errout; |
| 882 | } |
| 883 | |
David Ahern | 6d8422a1 | 2017-05-21 10:12:02 -0600 | [diff] [blame] | 884 | err = fib_table_insert(net, tb, &cfg, extack); |
Paolo Abeni | 6e617de | 2017-09-20 18:26:53 +0200 | [diff] [blame] | 885 | if (!err && cfg.fc_type == RTN_LOCAL) |
| 886 | net->ipv4.fib_has_custom_local_routes = true; |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 887 | errout: |
| 888 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | } |
| 890 | |
David Ahern | 4724676 | 2018-10-15 18:56:42 -0700 | [diff] [blame] | 891 | int ip_valid_fib_dump_req(struct net *net, const struct nlmsghdr *nlh, |
| 892 | struct fib_dump_filter *filter, |
David Ahern | effe679 | 2018-10-15 18:56:48 -0700 | [diff] [blame] | 893 | struct netlink_callback *cb) |
David Ahern | e8ba330 | 2018-10-07 20:16:35 -0700 | [diff] [blame] | 894 | { |
David Ahern | effe679 | 2018-10-15 18:56:48 -0700 | [diff] [blame] | 895 | struct netlink_ext_ack *extack = cb->extack; |
| 896 | struct nlattr *tb[RTA_MAX + 1]; |
David Ahern | e8ba330 | 2018-10-07 20:16:35 -0700 | [diff] [blame] | 897 | struct rtmsg *rtm; |
David Ahern | effe679 | 2018-10-15 18:56:48 -0700 | [diff] [blame] | 898 | int err, i; |
| 899 | |
| 900 | ASSERT_RTNL(); |
David Ahern | e8ba330 | 2018-10-07 20:16:35 -0700 | [diff] [blame] | 901 | |
| 902 | if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) { |
| 903 | NL_SET_ERR_MSG(extack, "Invalid header for FIB dump request"); |
| 904 | return -EINVAL; |
| 905 | } |
| 906 | |
| 907 | rtm = nlmsg_data(nlh); |
| 908 | if (rtm->rtm_dst_len || rtm->rtm_src_len || rtm->rtm_tos || |
David Ahern | effe679 | 2018-10-15 18:56:48 -0700 | [diff] [blame] | 909 | rtm->rtm_scope) { |
David Ahern | e8ba330 | 2018-10-07 20:16:35 -0700 | [diff] [blame] | 910 | NL_SET_ERR_MSG(extack, "Invalid values in header for FIB dump request"); |
| 911 | return -EINVAL; |
| 912 | } |
Stefano Brivio | 564c91f | 2019-06-21 17:45:20 +0200 | [diff] [blame] | 913 | |
David Ahern | e8ba330 | 2018-10-07 20:16:35 -0700 | [diff] [blame] | 914 | if (rtm->rtm_flags & ~(RTM_F_CLONED | RTM_F_PREFIX)) { |
| 915 | NL_SET_ERR_MSG(extack, "Invalid flags for FIB dump request"); |
| 916 | return -EINVAL; |
| 917 | } |
Stefano Brivio | 564c91f | 2019-06-21 17:45:20 +0200 | [diff] [blame] | 918 | if (rtm->rtm_flags & RTM_F_CLONED) |
| 919 | filter->dump_routes = false; |
| 920 | else |
| 921 | filter->dump_exceptions = false; |
David Ahern | e8ba330 | 2018-10-07 20:16:35 -0700 | [diff] [blame] | 922 | |
David Ahern | effe679 | 2018-10-15 18:56:48 -0700 | [diff] [blame] | 923 | filter->flags = rtm->rtm_flags; |
| 924 | filter->protocol = rtm->rtm_protocol; |
| 925 | filter->rt_type = rtm->rtm_type; |
| 926 | filter->table_id = rtm->rtm_table; |
| 927 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 928 | err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX, |
| 929 | rtm_ipv4_policy, extack); |
David Ahern | effe679 | 2018-10-15 18:56:48 -0700 | [diff] [blame] | 930 | if (err < 0) |
| 931 | return err; |
| 932 | |
| 933 | for (i = 0; i <= RTA_MAX; ++i) { |
| 934 | int ifindex; |
| 935 | |
| 936 | if (!tb[i]) |
| 937 | continue; |
| 938 | |
| 939 | switch (i) { |
| 940 | case RTA_TABLE: |
| 941 | filter->table_id = nla_get_u32(tb[i]); |
| 942 | break; |
| 943 | case RTA_OIF: |
| 944 | ifindex = nla_get_u32(tb[i]); |
| 945 | filter->dev = __dev_get_by_index(net, ifindex); |
| 946 | if (!filter->dev) |
| 947 | return -ENODEV; |
| 948 | break; |
| 949 | default: |
| 950 | NL_SET_ERR_MSG(extack, "Unsupported attribute in dump request"); |
| 951 | return -EINVAL; |
| 952 | } |
| 953 | } |
| 954 | |
| 955 | if (filter->flags || filter->protocol || filter->rt_type || |
| 956 | filter->table_id || filter->dev) { |
| 957 | filter->filter_set = 1; |
| 958 | cb->answer_flags = NLM_F_DUMP_FILTERED; |
David Ahern | e8ba330 | 2018-10-07 20:16:35 -0700 | [diff] [blame] | 959 | } |
| 960 | |
| 961 | return 0; |
| 962 | } |
| 963 | EXPORT_SYMBOL_GPL(ip_valid_fib_dump_req); |
| 964 | |
Thomas Graf | 63f3444 | 2007-03-22 11:55:17 -0700 | [diff] [blame] | 965 | 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] | 966 | { |
Stefano Brivio | 564c91f | 2019-06-21 17:45:20 +0200 | [diff] [blame] | 967 | struct fib_dump_filter filter = { .dump_routes = true, |
| 968 | .dump_exceptions = true }; |
David Ahern | e8ba330 | 2018-10-07 20:16:35 -0700 | [diff] [blame] | 969 | const struct nlmsghdr *nlh = cb->nlh; |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 970 | struct net *net = sock_net(skb->sk); |
Patrick McHardy | 1af5a8c | 2006-08-10 23:10:46 -0700 | [diff] [blame] | 971 | unsigned int h, s_h; |
| 972 | unsigned int e = 0, s_e; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | struct fib_table *tb; |
Denis V. Lunev | e4aef8a | 2008-01-10 03:28:24 -0800 | [diff] [blame] | 974 | struct hlist_head *head; |
David Ahern | f6c5775 | 2017-05-15 23:19:17 -0700 | [diff] [blame] | 975 | int dumped = 0, err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | |
David Ahern | e8ba330 | 2018-10-07 20:16:35 -0700 | [diff] [blame] | 977 | if (cb->strict_check) { |
David Ahern | effe679 | 2018-10-15 18:56:48 -0700 | [diff] [blame] | 978 | err = ip_valid_fib_dump_req(net, nlh, &filter, cb); |
David Ahern | e8ba330 | 2018-10-07 20:16:35 -0700 | [diff] [blame] | 979 | if (err < 0) |
| 980 | return err; |
David Ahern | e4e92fb | 2018-10-15 18:56:51 -0700 | [diff] [blame] | 981 | } else if (nlmsg_len(nlh) >= sizeof(struct rtmsg)) { |
| 982 | struct rtmsg *rtm = nlmsg_data(nlh); |
| 983 | |
| 984 | filter.flags = rtm->rtm_flags & (RTM_F_PREFIX | RTM_F_CLONED); |
David Ahern | e8ba330 | 2018-10-07 20:16:35 -0700 | [diff] [blame] | 985 | } |
| 986 | |
Stefano Brivio | b597ca6e | 2019-06-21 17:45:21 +0200 | [diff] [blame] | 987 | /* ipv4 does not use prefix flag */ |
| 988 | if (filter.flags & RTM_F_PREFIX) |
Li RongQing | 0b8c7f6 | 2014-03-21 11:33:10 +0800 | [diff] [blame] | 989 | return skb->len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | |
David Ahern | 18a8021 | 2018-10-15 18:56:43 -0700 | [diff] [blame] | 991 | if (filter.table_id) { |
| 992 | tb = fib_get_table(net, filter.table_id); |
| 993 | if (!tb) { |
Sabrina Dubroca | 41b4bd9 | 2020-05-20 11:15:46 +0200 | [diff] [blame] | 994 | if (rtnl_msg_family(cb->nlh) != PF_INET) |
David Ahern | ae677bb | 2018-10-24 12:59:01 -0700 | [diff] [blame] | 995 | return skb->len; |
| 996 | |
David Ahern | 18a8021 | 2018-10-15 18:56:43 -0700 | [diff] [blame] | 997 | NL_SET_ERR_MSG(cb->extack, "ipv4: FIB table does not exist"); |
| 998 | return -ENOENT; |
| 999 | } |
| 1000 | |
Qian Cai | dddeb30 | 2020-03-19 22:54:21 -0400 | [diff] [blame] | 1001 | rcu_read_lock(); |
David Ahern | 18a8021 | 2018-10-15 18:56:43 -0700 | [diff] [blame] | 1002 | err = fib_table_dump(tb, skb, cb, &filter); |
Qian Cai | dddeb30 | 2020-03-19 22:54:21 -0400 | [diff] [blame] | 1003 | rcu_read_unlock(); |
David Ahern | 18a8021 | 2018-10-15 18:56:43 -0700 | [diff] [blame] | 1004 | return skb->len ? : err; |
| 1005 | } |
| 1006 | |
Patrick McHardy | 1af5a8c | 2006-08-10 23:10:46 -0700 | [diff] [blame] | 1007 | s_h = cb->args[0]; |
| 1008 | s_e = cb->args[1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | |
Alexander Duyck | a7e5353 | 2015-03-04 15:02:44 -0800 | [diff] [blame] | 1010 | rcu_read_lock(); |
| 1011 | |
Patrick McHardy | 1af5a8c | 2006-08-10 23:10:46 -0700 | [diff] [blame] | 1012 | for (h = s_h; h < FIB_TABLE_HASHSZ; h++, s_e = 0) { |
| 1013 | e = 0; |
Denis V. Lunev | e4aef8a | 2008-01-10 03:28:24 -0800 | [diff] [blame] | 1014 | head = &net->ipv4.fib_table_hash[h]; |
Alexander Duyck | a7e5353 | 2015-03-04 15:02:44 -0800 | [diff] [blame] | 1015 | hlist_for_each_entry_rcu(tb, head, tb_hlist) { |
Patrick McHardy | 1af5a8c | 2006-08-10 23:10:46 -0700 | [diff] [blame] | 1016 | if (e < s_e) |
| 1017 | goto next; |
| 1018 | if (dumped) |
| 1019 | memset(&cb->args[2], 0, sizeof(cb->args) - |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1020 | 2 * sizeof(cb->args[0])); |
David Ahern | 18a8021 | 2018-10-15 18:56:43 -0700 | [diff] [blame] | 1021 | err = fib_table_dump(tb, skb, cb, &filter); |
David Ahern | f6c5775 | 2017-05-15 23:19:17 -0700 | [diff] [blame] | 1022 | if (err < 0) { |
| 1023 | if (likely(skb->len)) |
| 1024 | goto out; |
| 1025 | |
| 1026 | goto out_err; |
| 1027 | } |
Patrick McHardy | 1af5a8c | 2006-08-10 23:10:46 -0700 | [diff] [blame] | 1028 | dumped = 1; |
| 1029 | next: |
| 1030 | e++; |
| 1031 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | } |
Patrick McHardy | 1af5a8c | 2006-08-10 23:10:46 -0700 | [diff] [blame] | 1033 | out: |
David Ahern | f6c5775 | 2017-05-15 23:19:17 -0700 | [diff] [blame] | 1034 | err = skb->len; |
| 1035 | out_err: |
Alexander Duyck | a7e5353 | 2015-03-04 15:02:44 -0800 | [diff] [blame] | 1036 | rcu_read_unlock(); |
| 1037 | |
Patrick McHardy | 1af5a8c | 2006-08-10 23:10:46 -0700 | [diff] [blame] | 1038 | cb->args[1] = e; |
| 1039 | cb->args[0] = h; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1040 | |
David Ahern | f6c5775 | 2017-05-15 23:19:17 -0700 | [diff] [blame] | 1041 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | } |
| 1043 | |
| 1044 | /* Prepare and feed intra-kernel routing request. |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 1045 | * Really, it should be netlink message, but :-( netlink |
| 1046 | * can be not configured, so that we feed it directly |
| 1047 | * to fib engine. It is legal, because all events occur |
| 1048 | * only when netlink is already locked. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | */ |
David Ahern | af4d768 | 2018-05-27 08:09:57 -0700 | [diff] [blame] | 1050 | static void fib_magic(int cmd, int type, __be32 dst, int dst_len, |
| 1051 | struct in_ifaddr *ifa, u32 rt_priority) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | { |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 1053 | struct net *net = dev_net(ifa->ifa_dev->dev); |
David Ahern | 3236b00 | 2015-09-29 20:07:14 -0700 | [diff] [blame] | 1054 | u32 tb_id = l3mdev_fib_table(ifa->ifa_dev->dev); |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 1055 | struct fib_table *tb; |
| 1056 | struct fib_config cfg = { |
| 1057 | .fc_protocol = RTPROT_KERNEL, |
| 1058 | .fc_type = type, |
| 1059 | .fc_dst = dst, |
| 1060 | .fc_dst_len = dst_len, |
David Ahern | af4d768 | 2018-05-27 08:09:57 -0700 | [diff] [blame] | 1061 | .fc_priority = rt_priority, |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 1062 | .fc_prefsrc = ifa->ifa_local, |
| 1063 | .fc_oif = ifa->ifa_dev->dev->ifindex, |
| 1064 | .fc_nlflags = NLM_F_CREATE | NLM_F_APPEND, |
Denis V. Lunev | 4d1169c | 2008-01-10 03:26:13 -0800 | [diff] [blame] | 1065 | .fc_nlinfo = { |
Denis V. Lunev | 4b5d47d | 2008-01-10 03:29:23 -0800 | [diff] [blame] | 1066 | .nl_net = net, |
Denis V. Lunev | 4d1169c | 2008-01-10 03:26:13 -0800 | [diff] [blame] | 1067 | }, |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 1068 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | |
David Ahern | 021dd3b | 2015-08-13 14:59:06 -0600 | [diff] [blame] | 1070 | if (!tb_id) |
| 1071 | tb_id = (type == RTN_UNICAST) ? RT_TABLE_MAIN : RT_TABLE_LOCAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | |
David Ahern | 021dd3b | 2015-08-13 14:59:06 -0600 | [diff] [blame] | 1073 | tb = fib_new_table(net, tb_id); |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 1074 | if (!tb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | return; |
| 1076 | |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 1077 | cfg.fc_table = tb->tb_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | |
Thomas Graf | 4e902c5 | 2006-08-17 18:14:52 -0700 | [diff] [blame] | 1079 | if (type != RTN_LOCAL) |
| 1080 | cfg.fc_scope = RT_SCOPE_LINK; |
| 1081 | else |
| 1082 | cfg.fc_scope = RT_SCOPE_HOST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | |
| 1084 | if (cmd == RTM_NEWROUTE) |
David Ahern | 6d8422a1 | 2017-05-21 10:12:02 -0600 | [diff] [blame] | 1085 | fib_table_insert(net, tb, &cfg, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 | else |
David Ahern | 7805599 | 2017-05-27 16:19:26 -0600 | [diff] [blame] | 1087 | fib_table_delete(net, tb, &cfg, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1088 | } |
| 1089 | |
Jamal Hadi Salim | 0ff60a4 | 2005-11-22 14:47:37 -0800 | [diff] [blame] | 1090 | void fib_add_ifaddr(struct in_ifaddr *ifa) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | { |
| 1092 | struct in_device *in_dev = ifa->ifa_dev; |
| 1093 | struct net_device *dev = in_dev->dev; |
| 1094 | struct in_ifaddr *prim = ifa; |
Al Viro | a144ea4 | 2006-09-28 18:00:55 -0700 | [diff] [blame] | 1095 | __be32 mask = ifa->ifa_mask; |
| 1096 | __be32 addr = ifa->ifa_local; |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 1097 | __be32 prefix = ifa->ifa_address & mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1098 | |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 1099 | if (ifa->ifa_flags & IFA_F_SECONDARY) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | prim = inet_ifa_byprefix(in_dev, prefix, mask); |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 1101 | if (!prim) { |
Joe Perches | 058bd4d | 2012-03-11 18:36:11 +0000 | [diff] [blame] | 1102 | pr_warn("%s: bug: prim == NULL\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 | return; |
| 1104 | } |
| 1105 | } |
| 1106 | |
David Ahern | af4d768 | 2018-05-27 08:09:57 -0700 | [diff] [blame] | 1107 | fib_magic(RTM_NEWROUTE, RTN_LOCAL, addr, 32, prim, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1108 | |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 1109 | if (!(dev->flags & IFF_UP)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | return; |
| 1111 | |
| 1112 | /* Add broadcast address, if it is explicitly assigned. */ |
Al Viro | a144ea4 | 2006-09-28 18:00:55 -0700 | [diff] [blame] | 1113 | if (ifa->ifa_broadcast && ifa->ifa_broadcast != htonl(0xFFFFFFFF)) |
David Ahern | af4d768 | 2018-05-27 08:09:57 -0700 | [diff] [blame] | 1114 | fib_magic(RTM_NEWROUTE, RTN_BROADCAST, ifa->ifa_broadcast, 32, |
| 1115 | prim, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 1117 | if (!ipv4_is_zeronet(prefix) && !(ifa->ifa_flags & IFA_F_SECONDARY) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1118 | (prefix != addr || ifa->ifa_prefixlen < 32)) { |
Paolo Abeni | 7b13118 | 2015-10-20 10:28:45 +0200 | [diff] [blame] | 1119 | if (!(ifa->ifa_flags & IFA_F_NOPREFIXROUTE)) |
| 1120 | fib_magic(RTM_NEWROUTE, |
| 1121 | dev->flags & IFF_LOOPBACK ? RTN_LOCAL : RTN_UNICAST, |
David Ahern | af4d768 | 2018-05-27 08:09:57 -0700 | [diff] [blame] | 1122 | prefix, ifa->ifa_prefixlen, prim, |
| 1123 | ifa->ifa_rt_priority); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1124 | |
| 1125 | /* Add network specific broadcasts, when it takes a sense */ |
| 1126 | if (ifa->ifa_prefixlen < 31) { |
David Ahern | af4d768 | 2018-05-27 08:09:57 -0700 | [diff] [blame] | 1127 | fib_magic(RTM_NEWROUTE, RTN_BROADCAST, prefix, 32, |
| 1128 | prim, 0); |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 1129 | fib_magic(RTM_NEWROUTE, RTN_BROADCAST, prefix | ~mask, |
David Ahern | af4d768 | 2018-05-27 08:09:57 -0700 | [diff] [blame] | 1130 | 32, prim, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1131 | } |
| 1132 | } |
| 1133 | } |
| 1134 | |
David Ahern | af4d768 | 2018-05-27 08:09:57 -0700 | [diff] [blame] | 1135 | void fib_modify_prefix_metric(struct in_ifaddr *ifa, u32 new_metric) |
| 1136 | { |
| 1137 | __be32 prefix = ifa->ifa_address & ifa->ifa_mask; |
| 1138 | struct in_device *in_dev = ifa->ifa_dev; |
| 1139 | struct net_device *dev = in_dev->dev; |
| 1140 | |
| 1141 | if (!(dev->flags & IFF_UP) || |
| 1142 | ifa->ifa_flags & (IFA_F_SECONDARY | IFA_F_NOPREFIXROUTE) || |
| 1143 | ipv4_is_zeronet(prefix) || |
Paolo Abeni | 0b834ba | 2019-10-26 11:53:39 +0200 | [diff] [blame] | 1144 | (prefix == ifa->ifa_local && ifa->ifa_prefixlen == 32)) |
David Ahern | af4d768 | 2018-05-27 08:09:57 -0700 | [diff] [blame] | 1145 | return; |
| 1146 | |
| 1147 | /* add the new */ |
| 1148 | fib_magic(RTM_NEWROUTE, |
| 1149 | dev->flags & IFF_LOOPBACK ? RTN_LOCAL : RTN_UNICAST, |
| 1150 | prefix, ifa->ifa_prefixlen, ifa, new_metric); |
| 1151 | |
| 1152 | /* delete the old */ |
| 1153 | fib_magic(RTM_DELROUTE, |
| 1154 | dev->flags & IFF_LOOPBACK ? RTN_LOCAL : RTN_UNICAST, |
| 1155 | prefix, ifa->ifa_prefixlen, ifa, ifa->ifa_rt_priority); |
| 1156 | } |
| 1157 | |
Julian Anastasov | e6abbaa | 2011-03-19 12:13:49 +0000 | [diff] [blame] | 1158 | /* Delete primary or secondary address. |
| 1159 | * Optionally, on secondary address promotion consider the addresses |
| 1160 | * from subnet iprim as deleted, even if they are in device list. |
| 1161 | * In this case the secondary ifa can be in device list. |
| 1162 | */ |
| 1163 | void fib_del_ifaddr(struct in_ifaddr *ifa, struct in_ifaddr *iprim) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1164 | { |
| 1165 | struct in_device *in_dev = ifa->ifa_dev; |
| 1166 | struct net_device *dev = in_dev->dev; |
| 1167 | struct in_ifaddr *ifa1; |
Julian Anastasov | e6abbaa | 2011-03-19 12:13:49 +0000 | [diff] [blame] | 1168 | struct in_ifaddr *prim = ifa, *prim1 = NULL; |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 1169 | __be32 brd = ifa->ifa_address | ~ifa->ifa_mask; |
| 1170 | __be32 any = ifa->ifa_address & ifa->ifa_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | #define LOCAL_OK 1 |
| 1172 | #define BRD_OK 2 |
| 1173 | #define BRD0_OK 4 |
| 1174 | #define BRD1_OK 8 |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 1175 | unsigned int ok = 0; |
Julian Anastasov | e6abbaa | 2011-03-19 12:13:49 +0000 | [diff] [blame] | 1176 | int subnet = 0; /* Primary network */ |
| 1177 | int gone = 1; /* Address is missing */ |
| 1178 | int same_prefsrc = 0; /* Another primary with same IP */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1179 | |
Julian Anastasov | e6abbaa | 2011-03-19 12:13:49 +0000 | [diff] [blame] | 1180 | if (ifa->ifa_flags & IFA_F_SECONDARY) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1181 | prim = inet_ifa_byprefix(in_dev, any, ifa->ifa_mask); |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 1182 | if (!prim) { |
Paolo Abeni | 391a203 | 2016-04-21 22:23:31 +0200 | [diff] [blame] | 1183 | /* if the device has been deleted, we don't perform |
| 1184 | * address promotion |
| 1185 | */ |
| 1186 | if (!in_dev->dead) |
| 1187 | pr_warn("%s: bug: prim == NULL\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1188 | return; |
| 1189 | } |
Julian Anastasov | e6abbaa | 2011-03-19 12:13:49 +0000 | [diff] [blame] | 1190 | if (iprim && iprim != prim) { |
Joe Perches | 058bd4d | 2012-03-11 18:36:11 +0000 | [diff] [blame] | 1191 | pr_warn("%s: bug: iprim != prim\n", __func__); |
Julian Anastasov | e6abbaa | 2011-03-19 12:13:49 +0000 | [diff] [blame] | 1192 | return; |
| 1193 | } |
| 1194 | } else if (!ipv4_is_zeronet(any) && |
| 1195 | (any != ifa->ifa_local || ifa->ifa_prefixlen < 32)) { |
Paolo Abeni | 7b13118 | 2015-10-20 10:28:45 +0200 | [diff] [blame] | 1196 | if (!(ifa->ifa_flags & IFA_F_NOPREFIXROUTE)) |
| 1197 | fib_magic(RTM_DELROUTE, |
| 1198 | dev->flags & IFF_LOOPBACK ? RTN_LOCAL : RTN_UNICAST, |
David Ahern | af4d768 | 2018-05-27 08:09:57 -0700 | [diff] [blame] | 1199 | any, ifa->ifa_prefixlen, prim, 0); |
Julian Anastasov | e6abbaa | 2011-03-19 12:13:49 +0000 | [diff] [blame] | 1200 | subnet = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1201 | } |
| 1202 | |
David S. Miller | fbd40ea | 2016-03-13 23:28:00 -0400 | [diff] [blame] | 1203 | if (in_dev->dead) |
| 1204 | goto no_promotions; |
| 1205 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1206 | /* Deletion is more complicated than add. |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 1207 | * We should take care of not to delete too much :-) |
| 1208 | * |
| 1209 | * Scan address list to be sure that addresses are really gone. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1210 | */ |
Florian Westphal | cd5a411 | 2019-05-31 18:27:07 +0200 | [diff] [blame] | 1211 | rcu_read_lock(); |
| 1212 | in_dev_for_each_ifa_rcu(ifa1, in_dev) { |
Julian Anastasov | e6abbaa | 2011-03-19 12:13:49 +0000 | [diff] [blame] | 1213 | if (ifa1 == ifa) { |
| 1214 | /* promotion, keep the IP */ |
| 1215 | gone = 0; |
| 1216 | continue; |
| 1217 | } |
| 1218 | /* Ignore IFAs from our subnet */ |
| 1219 | if (iprim && ifa1->ifa_mask == iprim->ifa_mask && |
| 1220 | inet_ifa_match(ifa1->ifa_address, iprim)) |
| 1221 | continue; |
| 1222 | |
| 1223 | /* Ignore ifa1 if it uses different primary IP (prefsrc) */ |
| 1224 | if (ifa1->ifa_flags & IFA_F_SECONDARY) { |
| 1225 | /* Another address from our subnet? */ |
| 1226 | if (ifa1->ifa_mask == prim->ifa_mask && |
| 1227 | inet_ifa_match(ifa1->ifa_address, prim)) |
| 1228 | prim1 = prim; |
| 1229 | else { |
| 1230 | /* We reached the secondaries, so |
| 1231 | * same_prefsrc should be determined. |
| 1232 | */ |
| 1233 | if (!same_prefsrc) |
| 1234 | continue; |
| 1235 | /* Search new prim1 if ifa1 is not |
| 1236 | * using the current prim1 |
| 1237 | */ |
| 1238 | if (!prim1 || |
| 1239 | ifa1->ifa_mask != prim1->ifa_mask || |
| 1240 | !inet_ifa_match(ifa1->ifa_address, prim1)) |
| 1241 | prim1 = inet_ifa_byprefix(in_dev, |
| 1242 | ifa1->ifa_address, |
| 1243 | ifa1->ifa_mask); |
| 1244 | if (!prim1) |
| 1245 | continue; |
| 1246 | if (prim1->ifa_local != prim->ifa_local) |
| 1247 | continue; |
| 1248 | } |
| 1249 | } else { |
| 1250 | if (prim->ifa_local != ifa1->ifa_local) |
| 1251 | continue; |
| 1252 | prim1 = ifa1; |
| 1253 | if (prim != prim1) |
| 1254 | same_prefsrc = 1; |
| 1255 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1256 | if (ifa->ifa_local == ifa1->ifa_local) |
| 1257 | ok |= LOCAL_OK; |
| 1258 | if (ifa->ifa_broadcast == ifa1->ifa_broadcast) |
| 1259 | ok |= BRD_OK; |
| 1260 | if (brd == ifa1->ifa_broadcast) |
| 1261 | ok |= BRD1_OK; |
| 1262 | if (any == ifa1->ifa_broadcast) |
| 1263 | ok |= BRD0_OK; |
Julian Anastasov | e6abbaa | 2011-03-19 12:13:49 +0000 | [diff] [blame] | 1264 | /* primary has network specific broadcasts */ |
| 1265 | if (prim1 == ifa1 && ifa1->ifa_prefixlen < 31) { |
| 1266 | __be32 brd1 = ifa1->ifa_address | ~ifa1->ifa_mask; |
| 1267 | __be32 any1 = ifa1->ifa_address & ifa1->ifa_mask; |
| 1268 | |
| 1269 | if (!ipv4_is_zeronet(any1)) { |
| 1270 | if (ifa->ifa_broadcast == brd1 || |
| 1271 | ifa->ifa_broadcast == any1) |
| 1272 | ok |= BRD_OK; |
| 1273 | if (brd == brd1 || brd == any1) |
| 1274 | ok |= BRD1_OK; |
| 1275 | if (any == brd1 || any == any1) |
| 1276 | ok |= BRD0_OK; |
| 1277 | } |
| 1278 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1279 | } |
Florian Westphal | cd5a411 | 2019-05-31 18:27:07 +0200 | [diff] [blame] | 1280 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1281 | |
David S. Miller | fbd40ea | 2016-03-13 23:28:00 -0400 | [diff] [blame] | 1282 | no_promotions: |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 1283 | if (!(ok & BRD_OK)) |
David Ahern | af4d768 | 2018-05-27 08:09:57 -0700 | [diff] [blame] | 1284 | fib_magic(RTM_DELROUTE, RTN_BROADCAST, ifa->ifa_broadcast, 32, |
| 1285 | prim, 0); |
Julian Anastasov | e6abbaa | 2011-03-19 12:13:49 +0000 | [diff] [blame] | 1286 | if (subnet && ifa->ifa_prefixlen < 31) { |
| 1287 | if (!(ok & BRD1_OK)) |
David Ahern | af4d768 | 2018-05-27 08:09:57 -0700 | [diff] [blame] | 1288 | fib_magic(RTM_DELROUTE, RTN_BROADCAST, brd, 32, |
| 1289 | prim, 0); |
Julian Anastasov | e6abbaa | 2011-03-19 12:13:49 +0000 | [diff] [blame] | 1290 | if (!(ok & BRD0_OK)) |
David Ahern | af4d768 | 2018-05-27 08:09:57 -0700 | [diff] [blame] | 1291 | fib_magic(RTM_DELROUTE, RTN_BROADCAST, any, 32, |
| 1292 | prim, 0); |
Julian Anastasov | e6abbaa | 2011-03-19 12:13:49 +0000 | [diff] [blame] | 1293 | } |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 1294 | if (!(ok & LOCAL_OK)) { |
David Ahern | 30bbaa1 | 2015-08-13 14:59:05 -0600 | [diff] [blame] | 1295 | unsigned int addr_type; |
| 1296 | |
David Ahern | af4d768 | 2018-05-27 08:09:57 -0700 | [diff] [blame] | 1297 | fib_magic(RTM_DELROUTE, RTN_LOCAL, ifa->ifa_local, 32, prim, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1298 | |
| 1299 | /* Check, that this local address finally disappeared. */ |
David Ahern | 30bbaa1 | 2015-08-13 14:59:05 -0600 | [diff] [blame] | 1300 | addr_type = inet_addr_type_dev_table(dev_net(dev), dev, |
| 1301 | ifa->ifa_local); |
| 1302 | if (gone && addr_type != RTN_LOCAL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1303 | /* And the last, but not the least thing. |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 1304 | * We must flush stray FIB entries. |
| 1305 | * |
| 1306 | * First of all, we scan fib_info list searching |
| 1307 | * for stray nexthop entries, then ignite fib_flush. |
| 1308 | */ |
Mark Tomlinson | 5a56a0b | 2016-09-05 10:20:20 +1200 | [diff] [blame] | 1309 | if (fib_sync_down_addr(dev, ifa->ifa_local)) |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 1310 | fib_flush(dev_net(dev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1311 | } |
| 1312 | } |
| 1313 | #undef LOCAL_OK |
| 1314 | #undef BRD_OK |
| 1315 | #undef BRD0_OK |
| 1316 | #undef BRD1_OK |
| 1317 | } |
| 1318 | |
Alexander Duyck | 345e9b5 | 2014-12-31 10:56:24 -0800 | [diff] [blame] | 1319 | 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] | 1320 | { |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1321 | |
Robert Olsson | 246955f | 2005-06-20 13:36:39 -0700 | [diff] [blame] | 1322 | struct fib_result res; |
David S. Miller | 9ade228 | 2011-03-12 02:02:42 -0500 | [diff] [blame] | 1323 | struct flowi4 fl4 = { |
| 1324 | .flowi4_mark = frn->fl_mark, |
| 1325 | .daddr = frn->fl_addr, |
| 1326 | .flowi4_tos = frn->fl_tos, |
| 1327 | .flowi4_scope = frn->fl_scope, |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 1328 | }; |
Alexander Duyck | 345e9b5 | 2014-12-31 10:56:24 -0800 | [diff] [blame] | 1329 | struct fib_table *tb; |
| 1330 | |
| 1331 | rcu_read_lock(); |
| 1332 | |
| 1333 | tb = fib_get_table(net, frn->tb_id_in); |
Alexey Kuznetsov | 1194ed0 | 2007-04-25 13:07:28 -0700 | [diff] [blame] | 1334 | |
| 1335 | frn->err = -ENOENT; |
Robert Olsson | 246955f | 2005-06-20 13:36:39 -0700 | [diff] [blame] | 1336 | if (tb) { |
| 1337 | local_bh_disable(); |
| 1338 | |
| 1339 | frn->tb_id = tb->tb_id; |
David S. Miller | 9ade228 | 2011-03-12 02:02:42 -0500 | [diff] [blame] | 1340 | frn->err = fib_table_lookup(tb, &fl4, &res, FIB_LOOKUP_NOREF); |
Robert Olsson | 246955f | 2005-06-20 13:36:39 -0700 | [diff] [blame] | 1341 | |
| 1342 | if (!frn->err) { |
| 1343 | frn->prefixlen = res.prefixlen; |
| 1344 | frn->nh_sel = res.nh_sel; |
| 1345 | frn->type = res.type; |
| 1346 | frn->scope = res.scope; |
| 1347 | } |
| 1348 | local_bh_enable(); |
| 1349 | } |
Alexander Duyck | 345e9b5 | 2014-12-31 10:56:24 -0800 | [diff] [blame] | 1350 | |
| 1351 | rcu_read_unlock(); |
Robert Olsson | 246955f | 2005-06-20 13:36:39 -0700 | [diff] [blame] | 1352 | } |
| 1353 | |
David S. Miller | 28f7b036 | 2007-10-10 21:32:39 -0700 | [diff] [blame] | 1354 | static void nl_fib_input(struct sk_buff *skb) |
Robert Olsson | 246955f | 2005-06-20 13:36:39 -0700 | [diff] [blame] | 1355 | { |
Denis V. Lunev | 6bd48fc | 2008-01-10 03:28:55 -0800 | [diff] [blame] | 1356 | struct net *net; |
Robert Olsson | 246955f | 2005-06-20 13:36:39 -0700 | [diff] [blame] | 1357 | struct fib_result_nl *frn; |
David S. Miller | 28f7b036 | 2007-10-10 21:32:39 -0700 | [diff] [blame] | 1358 | struct nlmsghdr *nlh; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1359 | u32 portid; |
Alexey Kuznetsov | 1194ed0 | 2007-04-25 13:07:28 -0700 | [diff] [blame] | 1360 | |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 1361 | net = sock_net(skb->sk); |
Arnaldo Carvalho de Melo | b529ccf | 2007-04-25 19:08:35 -0700 | [diff] [blame] | 1362 | nlh = nlmsg_hdr(skb); |
Eric Dumazet | c64c0b3 | 2017-03-21 19:22:28 -0700 | [diff] [blame] | 1363 | if (skb->len < nlmsg_total_size(sizeof(*frn)) || |
| 1364 | skb->len < nlh->nlmsg_len || |
Hong zhi guo | 573ce26 | 2013-03-27 06:47:04 +0000 | [diff] [blame] | 1365 | nlmsg_len(nlh) < sizeof(*frn)) |
Thomas Graf | ea86575 | 2005-12-01 14:30:00 -0800 | [diff] [blame] | 1366 | return; |
Denis V. Lunev | d883a03 | 2007-12-21 02:01:53 -0800 | [diff] [blame] | 1367 | |
Pablo Neira | 3a36515 | 2013-06-28 03:04:23 +0200 | [diff] [blame] | 1368 | skb = netlink_skb_clone(skb, GFP_KERNEL); |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 1369 | if (!skb) |
Denis V. Lunev | d883a03 | 2007-12-21 02:01:53 -0800 | [diff] [blame] | 1370 | return; |
| 1371 | nlh = nlmsg_hdr(skb); |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1372 | |
Hong zhi guo | 573ce26 | 2013-03-27 06:47:04 +0000 | [diff] [blame] | 1373 | frn = (struct fib_result_nl *) nlmsg_data(nlh); |
Alexander Duyck | 345e9b5 | 2014-12-31 10:56:24 -0800 | [diff] [blame] | 1374 | nl_fib_lookup(net, frn); |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1375 | |
Rami Rosen | 28a2828 | 2013-01-11 09:59:20 +0000 | [diff] [blame] | 1376 | portid = NETLINK_CB(skb).portid; /* netlink portid */ |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1377 | NETLINK_CB(skb).portid = 0; /* from kernel */ |
Patrick McHardy | ac6d439 | 2005-08-14 19:29:52 -0700 | [diff] [blame] | 1378 | NETLINK_CB(skb).dst_group = 0; /* unicast */ |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1379 | netlink_unicast(net->ipv4.fibnl, skb, portid, MSG_DONTWAIT); |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1380 | } |
Robert Olsson | 246955f | 2005-06-20 13:36:39 -0700 | [diff] [blame] | 1381 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 1382 | static int __net_init nl_fib_lookup_init(struct net *net) |
Robert Olsson | 246955f | 2005-06-20 13:36:39 -0700 | [diff] [blame] | 1383 | { |
Denis V. Lunev | 6bd48fc | 2008-01-10 03:28:55 -0800 | [diff] [blame] | 1384 | struct sock *sk; |
Pablo Neira Ayuso | a31f2d1 | 2012-06-29 06:15:21 +0000 | [diff] [blame] | 1385 | struct netlink_kernel_cfg cfg = { |
| 1386 | .input = nl_fib_input, |
| 1387 | }; |
| 1388 | |
Pablo Neira Ayuso | 9f00d97 | 2012-09-08 02:53:54 +0000 | [diff] [blame] | 1389 | sk = netlink_kernel_create(net, NETLINK_FIB_LOOKUP, &cfg); |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 1390 | if (!sk) |
Denis V. Lunev | 7b1a74fd | 2008-01-10 03:22:17 -0800 | [diff] [blame] | 1391 | return -EAFNOSUPPORT; |
Denis V. Lunev | 6bd48fc | 2008-01-10 03:28:55 -0800 | [diff] [blame] | 1392 | net->ipv4.fibnl = sk; |
Denis V. Lunev | 7b1a74fd | 2008-01-10 03:22:17 -0800 | [diff] [blame] | 1393 | return 0; |
| 1394 | } |
| 1395 | |
| 1396 | static void nl_fib_lookup_exit(struct net *net) |
| 1397 | { |
Denis V. Lunev | b7c6ba6 | 2008-01-28 14:41:19 -0800 | [diff] [blame] | 1398 | netlink_kernel_release(net->ipv4.fibnl); |
Denis V. Lunev | 775516b | 2008-01-18 23:55:19 -0800 | [diff] [blame] | 1399 | net->ipv4.fibnl = NULL; |
Robert Olsson | 246955f | 2005-06-20 13:36:39 -0700 | [diff] [blame] | 1400 | } |
| 1401 | |
Julian Anastasov | 4f823de | 2015-10-30 10:23:33 +0200 | [diff] [blame] | 1402 | static void fib_disable_ip(struct net_device *dev, unsigned long event, |
| 1403 | bool force) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1404 | { |
Julian Anastasov | 4f823de | 2015-10-30 10:23:33 +0200 | [diff] [blame] | 1405 | if (fib_sync_down_dev(dev, event, force)) |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 1406 | fib_flush(dev_net(dev)); |
Gao Feng | 06b4fc5 | 2017-04-26 19:04:04 +0800 | [diff] [blame] | 1407 | else |
| 1408 | rt_cache_flush(dev_net(dev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | arp_ifdown(dev); |
| 1410 | } |
| 1411 | |
| 1412 | static int fib_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr) |
| 1413 | { |
Jianjun Kong | 6ed2533e | 2008-11-03 00:25:16 -0800 | [diff] [blame] | 1414 | struct in_ifaddr *ifa = (struct in_ifaddr *)ptr; |
Denis V. Lunev | 76e6ebf | 2008-07-05 19:00:44 -0700 | [diff] [blame] | 1415 | struct net_device *dev = ifa->ifa_dev->dev; |
David S. Miller | 436c3b6 | 2011-03-24 17:42:21 -0700 | [diff] [blame] | 1416 | struct net *net = dev_net(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1417 | |
| 1418 | switch (event) { |
| 1419 | case NETDEV_UP: |
| 1420 | fib_add_ifaddr(ifa); |
| 1421 | #ifdef CONFIG_IP_ROUTE_MULTIPATH |
Andy Gospodarek | 8a3d031 | 2015-06-23 13:45:36 -0400 | [diff] [blame] | 1422 | fib_sync_up(dev, RTNH_F_DEAD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1423 | #endif |
David S. Miller | 436c3b6 | 2011-03-24 17:42:21 -0700 | [diff] [blame] | 1424 | atomic_inc(&net->ipv4.dev_addr_genid); |
Nicolas Dichtel | 4ccfe6d | 2012-09-07 00:45:29 +0000 | [diff] [blame] | 1425 | rt_cache_flush(dev_net(dev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1426 | break; |
| 1427 | case NETDEV_DOWN: |
Julian Anastasov | e6abbaa | 2011-03-19 12:13:49 +0000 | [diff] [blame] | 1428 | fib_del_ifaddr(ifa, NULL); |
David S. Miller | 436c3b6 | 2011-03-24 17:42:21 -0700 | [diff] [blame] | 1429 | atomic_inc(&net->ipv4.dev_addr_genid); |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 1430 | if (!ifa->ifa_dev->ifa_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1431 | /* Last address was deleted from this interface. |
Eric Dumazet | 6a31d2a | 2010-10-04 20:00:18 +0000 | [diff] [blame] | 1432 | * Disable IP. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1433 | */ |
Julian Anastasov | 4f823de | 2015-10-30 10:23:33 +0200 | [diff] [blame] | 1434 | fib_disable_ip(dev, event, true); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1435 | } else { |
Nicolas Dichtel | 4ccfe6d | 2012-09-07 00:45:29 +0000 | [diff] [blame] | 1436 | rt_cache_flush(dev_net(dev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1437 | } |
| 1438 | break; |
| 1439 | } |
| 1440 | return NOTIFY_DONE; |
| 1441 | } |
| 1442 | |
| 1443 | static int fib_netdev_event(struct notifier_block *this, unsigned long event, void *ptr) |
| 1444 | { |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 1445 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
Sabrina Dubroca | af7d6cc | 2018-10-09 17:48:14 +0200 | [diff] [blame] | 1446 | struct netdev_notifier_changeupper_info *upper_info = ptr; |
| 1447 | struct netdev_notifier_info_ext *info_ext = ptr; |
Eric Dumazet | 0115e8e | 2012-08-22 17:19:46 +0000 | [diff] [blame] | 1448 | struct in_device *in_dev; |
David S. Miller | 436c3b6 | 2011-03-24 17:42:21 -0700 | [diff] [blame] | 1449 | struct net *net = dev_net(dev); |
Florian Westphal | cd5a411 | 2019-05-31 18:27:07 +0200 | [diff] [blame] | 1450 | struct in_ifaddr *ifa; |
Andy Gospodarek | 8a3d031 | 2015-06-23 13:45:36 -0400 | [diff] [blame] | 1451 | unsigned int flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1452 | |
| 1453 | if (event == NETDEV_UNREGISTER) { |
Julian Anastasov | 4f823de | 2015-10-30 10:23:33 +0200 | [diff] [blame] | 1454 | fib_disable_ip(dev, event, true); |
David S. Miller | caacf05 | 2012-07-31 15:06:50 -0700 | [diff] [blame] | 1455 | rt_flush_dev(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1456 | return NOTIFY_DONE; |
| 1457 | } |
| 1458 | |
Eric Dumazet | 0115e8e | 2012-08-22 17:19:46 +0000 | [diff] [blame] | 1459 | in_dev = __in_dev_get_rtnl(dev); |
Oliver Hartkopp | a0065f2 | 2014-01-23 10:19:34 +0100 | [diff] [blame] | 1460 | if (!in_dev) |
| 1461 | return NOTIFY_DONE; |
Eric Dumazet | 0115e8e | 2012-08-22 17:19:46 +0000 | [diff] [blame] | 1462 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1463 | switch (event) { |
| 1464 | case NETDEV_UP: |
Florian Westphal | cd5a411 | 2019-05-31 18:27:07 +0200 | [diff] [blame] | 1465 | in_dev_for_each_ifa_rtnl(ifa, in_dev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1466 | fib_add_ifaddr(ifa); |
Florian Westphal | cd5a411 | 2019-05-31 18:27:07 +0200 | [diff] [blame] | 1467 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1468 | #ifdef CONFIG_IP_ROUTE_MULTIPATH |
Andy Gospodarek | 8a3d031 | 2015-06-23 13:45:36 -0400 | [diff] [blame] | 1469 | fib_sync_up(dev, RTNH_F_DEAD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1470 | #endif |
David S. Miller | 436c3b6 | 2011-03-24 17:42:21 -0700 | [diff] [blame] | 1471 | atomic_inc(&net->ipv4.dev_addr_genid); |
Nicolas Dichtel | 4ccfe6d | 2012-09-07 00:45:29 +0000 | [diff] [blame] | 1472 | rt_cache_flush(net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1473 | break; |
| 1474 | case NETDEV_DOWN: |
Julian Anastasov | 4f823de | 2015-10-30 10:23:33 +0200 | [diff] [blame] | 1475 | fib_disable_ip(dev, event, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1476 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1477 | case NETDEV_CHANGE: |
Andy Gospodarek | 8a3d031 | 2015-06-23 13:45:36 -0400 | [diff] [blame] | 1478 | flags = dev_get_flags(dev); |
| 1479 | if (flags & (IFF_RUNNING | IFF_LOWER_UP)) |
| 1480 | fib_sync_up(dev, RTNH_F_LINKDOWN); |
| 1481 | else |
Julian Anastasov | 4f823de | 2015-10-30 10:23:33 +0200 | [diff] [blame] | 1482 | fib_sync_down_dev(dev, event, false); |
Sabrina Dubroca | af7d6cc | 2018-10-09 17:48:14 +0200 | [diff] [blame] | 1483 | rt_cache_flush(net); |
| 1484 | break; |
Andy Gospodarek | 8a3d031 | 2015-06-23 13:45:36 -0400 | [diff] [blame] | 1485 | case NETDEV_CHANGEMTU: |
Sabrina Dubroca | af7d6cc | 2018-10-09 17:48:14 +0200 | [diff] [blame] | 1486 | fib_sync_mtu(dev, info_ext->ext.mtu); |
Nicolas Dichtel | 4ccfe6d | 2012-09-07 00:45:29 +0000 | [diff] [blame] | 1487 | rt_cache_flush(net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1488 | break; |
David Ahern | 7f49e7a | 2015-12-10 10:25:24 -0800 | [diff] [blame] | 1489 | case NETDEV_CHANGEUPPER: |
Sabrina Dubroca | af7d6cc | 2018-10-09 17:48:14 +0200 | [diff] [blame] | 1490 | upper_info = ptr; |
David Ahern | 7f49e7a | 2015-12-10 10:25:24 -0800 | [diff] [blame] | 1491 | /* flush all routes if dev is linked to or unlinked from |
| 1492 | * an L3 master device (e.g., VRF) |
| 1493 | */ |
Sabrina Dubroca | af7d6cc | 2018-10-09 17:48:14 +0200 | [diff] [blame] | 1494 | if (upper_info->upper_dev && |
| 1495 | netif_is_l3_master(upper_info->upper_dev)) |
David Ahern | 7f49e7a | 2015-12-10 10:25:24 -0800 | [diff] [blame] | 1496 | fib_disable_ip(dev, NETDEV_DOWN, true); |
| 1497 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1498 | } |
| 1499 | return NOTIFY_DONE; |
| 1500 | } |
| 1501 | |
| 1502 | static struct notifier_block fib_inetaddr_notifier = { |
Jianjun Kong | 6ed2533e | 2008-11-03 00:25:16 -0800 | [diff] [blame] | 1503 | .notifier_call = fib_inetaddr_event, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1504 | }; |
| 1505 | |
| 1506 | static struct notifier_block fib_netdev_notifier = { |
Jianjun Kong | 6ed2533e | 2008-11-03 00:25:16 -0800 | [diff] [blame] | 1507 | .notifier_call = fib_netdev_event, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1508 | }; |
| 1509 | |
Denis V. Lunev | 7b1a74fd | 2008-01-10 03:22:17 -0800 | [diff] [blame] | 1510 | static int __net_init ip_fib_net_init(struct net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1511 | { |
Denis V. Lunev | dce5cbe | 2008-01-31 18:44:53 -0800 | [diff] [blame] | 1512 | int err; |
Eric Dumazet | 10da66f | 2010-10-13 08:22:03 +0000 | [diff] [blame] | 1513 | size_t size = sizeof(struct hlist_head) * FIB_TABLE_HASHSZ; |
Patrick McHardy | 1af5a8c | 2006-08-10 23:10:46 -0700 | [diff] [blame] | 1514 | |
Ido Schimmel | 04b1d4e | 2017-08-03 13:28:11 +0200 | [diff] [blame] | 1515 | err = fib4_notifier_init(net); |
| 1516 | if (err) |
| 1517 | return err; |
Ido Schimmel | cacaad1 | 2016-12-03 16:45:06 +0100 | [diff] [blame] | 1518 | |
Eric Dumazet | 10da66f | 2010-10-13 08:22:03 +0000 | [diff] [blame] | 1519 | /* Avoid false sharing : Use at least a full cache line */ |
| 1520 | size = max_t(size_t, size, L1_CACHE_BYTES); |
| 1521 | |
| 1522 | net->ipv4.fib_table_hash = kzalloc(size, GFP_KERNEL); |
Ido Schimmel | 04b1d4e | 2017-08-03 13:28:11 +0200 | [diff] [blame] | 1523 | if (!net->ipv4.fib_table_hash) { |
| 1524 | err = -ENOMEM; |
| 1525 | goto err_table_hash_alloc; |
| 1526 | } |
Denis V. Lunev | e4aef8a | 2008-01-10 03:28:24 -0800 | [diff] [blame] | 1527 | |
Denis V. Lunev | dce5cbe | 2008-01-31 18:44:53 -0800 | [diff] [blame] | 1528 | err = fib4_rules_init(net); |
| 1529 | if (err < 0) |
Ido Schimmel | 04b1d4e | 2017-08-03 13:28:11 +0200 | [diff] [blame] | 1530 | goto err_rules_init; |
Denis V. Lunev | dce5cbe | 2008-01-31 18:44:53 -0800 | [diff] [blame] | 1531 | return 0; |
| 1532 | |
Ido Schimmel | 04b1d4e | 2017-08-03 13:28:11 +0200 | [diff] [blame] | 1533 | err_rules_init: |
Denis V. Lunev | dce5cbe | 2008-01-31 18:44:53 -0800 | [diff] [blame] | 1534 | kfree(net->ipv4.fib_table_hash); |
Ido Schimmel | 04b1d4e | 2017-08-03 13:28:11 +0200 | [diff] [blame] | 1535 | err_table_hash_alloc: |
| 1536 | fib4_notifier_exit(net); |
Denis V. Lunev | dce5cbe | 2008-01-31 18:44:53 -0800 | [diff] [blame] | 1537 | return err; |
Denis V. Lunev | 7b1a74fd | 2008-01-10 03:22:17 -0800 | [diff] [blame] | 1538 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1539 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 1540 | static void ip_fib_net_exit(struct net *net) |
Denis V. Lunev | 7b1a74fd | 2008-01-10 03:22:17 -0800 | [diff] [blame] | 1541 | { |
Ido Schimmel | b4681c2 | 2017-12-20 19:34:19 +0200 | [diff] [blame] | 1542 | int i; |
Thomas Graf | 63f3444 | 2007-03-22 11:55:17 -0700 | [diff] [blame] | 1543 | |
Sabrina Dubroca | 6dede75 | 2015-03-10 21:03:54 +0100 | [diff] [blame] | 1544 | rtnl_lock(); |
Alexander Duyck | 6e47d6c | 2015-03-27 14:14:22 -0700 | [diff] [blame] | 1545 | #ifdef CONFIG_IP_MULTIPLE_TABLES |
Alexander Duyck | 6e47d6c | 2015-03-27 14:14:22 -0700 | [diff] [blame] | 1546 | RCU_INIT_POINTER(net->ipv4.fib_main, NULL); |
| 1547 | RCU_INIT_POINTER(net->ipv4.fib_default, NULL); |
| 1548 | #endif |
Ido Schimmel | b4681c2 | 2017-12-20 19:34:19 +0200 | [diff] [blame] | 1549 | /* Destroy the tables in reverse order to guarantee that the |
| 1550 | * local table, ID 255, is destroyed before the main table, ID |
| 1551 | * 254. This is necessary as the local table may contain |
| 1552 | * references to data contained in the main table. |
| 1553 | */ |
| 1554 | for (i = FIB_TABLE_HASHSZ - 1; i >= 0; i--) { |
Alexander Duyck | a7e5353 | 2015-03-04 15:02:44 -0800 | [diff] [blame] | 1555 | struct hlist_head *head = &net->ipv4.fib_table_hash[i]; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1556 | struct hlist_node *tmp; |
Alexander Duyck | a7e5353 | 2015-03-04 15:02:44 -0800 | [diff] [blame] | 1557 | struct fib_table *tb; |
Denis V. Lunev | 7b1a74fd | 2008-01-10 03:22:17 -0800 | [diff] [blame] | 1558 | |
Alexander Duyck | a7e5353 | 2015-03-04 15:02:44 -0800 | [diff] [blame] | 1559 | hlist_for_each_entry_safe(tb, tmp, head, tb_hlist) { |
Alexander Duyck | a7e5353 | 2015-03-04 15:02:44 -0800 | [diff] [blame] | 1560 | hlist_del(&tb->tb_hlist); |
Ido Schimmel | f97f4dd | 2019-01-09 09:57:39 +0000 | [diff] [blame] | 1561 | fib_table_flush(net, tb, true); |
Pavel Emelyanov | 4aa2c46 | 2010-10-28 02:00:43 +0000 | [diff] [blame] | 1562 | fib_free_table(tb); |
Denis V. Lunev | 7b1a74fd | 2008-01-10 03:22:17 -0800 | [diff] [blame] | 1563 | } |
| 1564 | } |
Alexander Duyck | ad88d05 | 2015-03-27 14:14:16 -0700 | [diff] [blame] | 1565 | |
| 1566 | #ifdef CONFIG_IP_MULTIPLE_TABLES |
| 1567 | fib4_rules_exit(net); |
| 1568 | #endif |
Eric Dumazet | e2666f8 | 2011-03-30 16:57:46 -0700 | [diff] [blame] | 1569 | rtnl_unlock(); |
Denis V. Lunev | e4aef8a | 2008-01-10 03:28:24 -0800 | [diff] [blame] | 1570 | kfree(net->ipv4.fib_table_hash); |
Ido Schimmel | 04b1d4e | 2017-08-03 13:28:11 +0200 | [diff] [blame] | 1571 | fib4_notifier_exit(net); |
Denis V. Lunev | 7b1a74fd | 2008-01-10 03:22:17 -0800 | [diff] [blame] | 1572 | } |
| 1573 | |
| 1574 | static int __net_init fib_net_init(struct net *net) |
| 1575 | { |
| 1576 | int error; |
| 1577 | |
David S. Miller | f4530fa | 2012-07-05 22:13:13 -0700 | [diff] [blame] | 1578 | #ifdef CONFIG_IP_ROUTE_CLASSID |
| 1579 | net->ipv4.fib_num_tclassid_users = 0; |
| 1580 | #endif |
Denis V. Lunev | 7b1a74fd | 2008-01-10 03:22:17 -0800 | [diff] [blame] | 1581 | error = ip_fib_net_init(net); |
| 1582 | if (error < 0) |
| 1583 | goto out; |
| 1584 | error = nl_fib_lookup_init(net); |
| 1585 | if (error < 0) |
| 1586 | goto out_nlfl; |
| 1587 | error = fib_proc_init(net); |
| 1588 | if (error < 0) |
| 1589 | goto out_proc; |
| 1590 | out: |
| 1591 | return error; |
| 1592 | |
| 1593 | out_proc: |
| 1594 | nl_fib_lookup_exit(net); |
| 1595 | out_nlfl: |
| 1596 | ip_fib_net_exit(net); |
| 1597 | goto out; |
| 1598 | } |
| 1599 | |
| 1600 | static void __net_exit fib_net_exit(struct net *net) |
| 1601 | { |
| 1602 | fib_proc_exit(net); |
| 1603 | nl_fib_lookup_exit(net); |
| 1604 | ip_fib_net_exit(net); |
| 1605 | } |
| 1606 | |
| 1607 | static struct pernet_operations fib_net_ops = { |
| 1608 | .init = fib_net_init, |
| 1609 | .exit = fib_net_exit, |
| 1610 | }; |
| 1611 | |
| 1612 | void __init ip_fib_init(void) |
| 1613 | { |
Mahesh Bandewar | 8799a22 | 2017-07-19 15:41:33 -0700 | [diff] [blame] | 1614 | fib_trie_init(); |
Denis V. Lunev | 7b1a74fd | 2008-01-10 03:22:17 -0800 | [diff] [blame] | 1615 | |
| 1616 | register_pernet_subsys(&fib_net_ops); |
Mahesh Bandewar | 8799a22 | 2017-07-19 15:41:33 -0700 | [diff] [blame] | 1617 | |
Denis V. Lunev | 7b1a74fd | 2008-01-10 03:22:17 -0800 | [diff] [blame] | 1618 | register_netdevice_notifier(&fib_netdev_notifier); |
| 1619 | register_inetaddr_notifier(&fib_inetaddr_notifier); |
Stephen Hemminger | 7f9b805 | 2008-01-14 23:14:20 -0800 | [diff] [blame] | 1620 | |
Florian Westphal | b97bac6 | 2017-08-09 20:41:48 +0200 | [diff] [blame] | 1621 | rtnl_register(PF_INET, RTM_NEWROUTE, inet_rtm_newroute, NULL, 0); |
| 1622 | rtnl_register(PF_INET, RTM_DELROUTE, inet_rtm_delroute, NULL, 0); |
| 1623 | rtnl_register(PF_INET, RTM_GETROUTE, NULL, inet_dump_fib, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1624 | } |