^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * INET An implementation of the TCP/IP protocol suite for the LINUX
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * operating system. INET is implemented using the BSD Socket
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * interface as the means of communication with the user level.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * IPv4 Forwarding Information Base: FIB frontend.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/bitops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/capability.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/socket.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/sockios.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/in.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/inet.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/inetdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/netdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/if_addr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/if_arp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <linux/cache.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #include <net/ip.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #include <net/protocol.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #include <net/route.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #include <net/tcp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #include <net/sock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #include <net/arp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #include <net/ip_fib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #include <net/nexthop.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #include <net/rtnetlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) #include <net/xfrm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #include <net/l3mdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) #include <net/lwtunnel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #include <trace/events/fib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) #ifndef CONFIG_IP_MULTIPLE_TABLES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) static int __net_init fib4_rules_init(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) struct fib_table *local_table, *main_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) main_table = fib_trie_table(RT_TABLE_MAIN, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) if (!main_table)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) local_table = fib_trie_table(RT_TABLE_LOCAL, main_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) if (!local_table)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) hlist_add_head_rcu(&local_table->tb_hlist,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) &net->ipv4.fib_table_hash[TABLE_LOCAL_INDEX]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) hlist_add_head_rcu(&main_table->tb_hlist,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) &net->ipv4.fib_table_hash[TABLE_MAIN_INDEX]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) fib_free_table(main_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) struct fib_table *fib_new_table(struct net *net, u32 id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) struct fib_table *tb, *alias = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) unsigned int h;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) if (id == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) id = RT_TABLE_MAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) tb = fib_get_table(net, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (tb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) return tb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) if (id == RT_TABLE_LOCAL && !net->ipv4.fib_has_custom_rules)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) alias = fib_new_table(net, RT_TABLE_MAIN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) tb = fib_trie_table(id, alias);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) if (!tb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) switch (id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) case RT_TABLE_MAIN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) rcu_assign_pointer(net->ipv4.fib_main, tb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) case RT_TABLE_DEFAULT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) rcu_assign_pointer(net->ipv4.fib_default, tb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) h = id & (FIB_TABLE_HASHSZ - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) hlist_add_head_rcu(&tb->tb_hlist, &net->ipv4.fib_table_hash[h]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) return tb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) EXPORT_SYMBOL_GPL(fib_new_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) /* caller must hold either rtnl or rcu read lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) struct fib_table *fib_get_table(struct net *net, u32 id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) struct fib_table *tb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) struct hlist_head *head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) unsigned int h;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if (id == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) id = RT_TABLE_MAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) h = id & (FIB_TABLE_HASHSZ - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) head = &net->ipv4.fib_table_hash[h];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) hlist_for_each_entry_rcu(tb, head, tb_hlist,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) lockdep_rtnl_is_held()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) if (tb->tb_id == id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) return tb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) #endif /* CONFIG_IP_MULTIPLE_TABLES */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) static void fib_replace_table(struct net *net, struct fib_table *old,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) struct fib_table *new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) #ifdef CONFIG_IP_MULTIPLE_TABLES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) switch (new->tb_id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) case RT_TABLE_MAIN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) rcu_assign_pointer(net->ipv4.fib_main, new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) case RT_TABLE_DEFAULT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) rcu_assign_pointer(net->ipv4.fib_default, new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) /* replace the old table in the hlist */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) hlist_replace_rcu(&old->tb_hlist, &new->tb_hlist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) int fib_unmerge(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) struct fib_table *old, *new, *main_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) /* attempt to fetch local table if it has been allocated */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) old = fib_get_table(net, RT_TABLE_LOCAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) if (!old)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) new = fib_trie_unmerge(old);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) if (!new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) /* table is already unmerged */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) if (new == old)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) /* replace merged table with clean table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) fib_replace_table(net, old, new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) fib_free_table(old);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) /* attempt to fetch main table if it has been allocated */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) main_table = fib_get_table(net, RT_TABLE_MAIN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) if (!main_table)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) /* flush local entries from main table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) fib_table_flush_external(main_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) void fib_flush(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) int flushed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) unsigned int h;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) struct hlist_head *head = &net->ipv4.fib_table_hash[h];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) struct hlist_node *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) struct fib_table *tb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) hlist_for_each_entry_safe(tb, tmp, head, tb_hlist)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) flushed += fib_table_flush(net, tb, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) if (flushed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) rt_cache_flush(net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) * Find address type as if only "dev" was present in the system. If
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) * on_dev is NULL then all interfaces are taken into consideration.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) static inline unsigned int __inet_dev_addr_type(struct net *net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) const struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) __be32 addr, u32 tb_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) struct flowi4 fl4 = { .daddr = addr };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) struct fib_result res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) unsigned int ret = RTN_BROADCAST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) struct fib_table *table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) if (ipv4_is_zeronet(addr) || ipv4_is_lbcast(addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) return RTN_BROADCAST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) if (ipv4_is_multicast(addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) return RTN_MULTICAST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) table = fib_get_table(net, tb_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) if (table) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) ret = RTN_UNICAST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) if (!fib_table_lookup(table, &fl4, &res, FIB_LOOKUP_NOREF)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) struct fib_nh_common *nhc = fib_info_nhc(res.fi, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) if (!dev || dev == nhc->nhc_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) ret = res.type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) unsigned int inet_addr_type_table(struct net *net, __be32 addr, u32 tb_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) return __inet_dev_addr_type(net, NULL, addr, tb_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) EXPORT_SYMBOL(inet_addr_type_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) unsigned int inet_addr_type(struct net *net, __be32 addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) return __inet_dev_addr_type(net, NULL, addr, RT_TABLE_LOCAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) EXPORT_SYMBOL(inet_addr_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) unsigned int inet_dev_addr_type(struct net *net, const struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) __be32 addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) u32 rt_table = l3mdev_fib_table(dev) ? : RT_TABLE_LOCAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) return __inet_dev_addr_type(net, dev, addr, rt_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) EXPORT_SYMBOL(inet_dev_addr_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) /* inet_addr_type with dev == NULL but using the table from a dev
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) * if one is associated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) unsigned int inet_addr_type_dev_table(struct net *net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) const struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) __be32 addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) u32 rt_table = l3mdev_fib_table(dev) ? : RT_TABLE_LOCAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) return __inet_dev_addr_type(net, NULL, addr, rt_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) EXPORT_SYMBOL(inet_addr_type_dev_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) __be32 fib_compute_spec_dst(struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) struct net_device *dev = skb->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) struct in_device *in_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) struct fib_result res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) struct rtable *rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) struct net *net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) int scope;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) rt = skb_rtable(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) if ((rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST | RTCF_LOCAL)) ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) RTCF_LOCAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) return ip_hdr(skb)->daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) in_dev = __in_dev_get_rcu(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) net = dev_net(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) scope = RT_SCOPE_UNIVERSE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) if (!ipv4_is_zeronet(ip_hdr(skb)->saddr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) bool vmark = in_dev && IN_DEV_SRC_VMARK(in_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) struct flowi4 fl4 = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) .flowi4_iif = LOOPBACK_IFINDEX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) .flowi4_oif = l3mdev_master_ifindex_rcu(dev),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) .daddr = ip_hdr(skb)->saddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) .flowi4_tos = ip_hdr(skb)->tos & IPTOS_RT_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) .flowi4_scope = scope,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) .flowi4_mark = vmark ? skb->mark : 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) if (!fib_lookup(net, &fl4, &res, 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) return fib_result_prefsrc(net, &res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) scope = RT_SCOPE_LINK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) return inet_select_addr(dev, ip_hdr(skb)->saddr, scope);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) bool fib_info_nh_uses_dev(struct fib_info *fi, const struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) bool dev_match = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) #ifdef CONFIG_IP_ROUTE_MULTIPATH
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) if (unlikely(fi->nh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) dev_match = nexthop_uses_dev(fi->nh, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) for (ret = 0; ret < fib_info_num_path(fi); ret++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) const struct fib_nh_common *nhc = fib_info_nhc(fi, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) if (nhc_l3mdev_matches_dev(nhc, dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) dev_match = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) if (fib_info_nhc(fi, 0)->nhc_dev == dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) dev_match = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) return dev_match;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) EXPORT_SYMBOL_GPL(fib_info_nh_uses_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) /* Given (packet source, input interface) and optional (dst, oif, tos):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) * - (main) check, that source is valid i.e. not broadcast or our local
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) * address.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) * - figure out what "logical" interface this packet arrived
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) * and calculate "specific destination" address.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) * - check, that packet arrived from expected physical interface.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) * called with rcu_read_lock()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) static int __fib_validate_source(struct sk_buff *skb, __be32 src, __be32 dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) u8 tos, int oif, struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) int rpf, struct in_device *idev, u32 *itag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) struct net *net = dev_net(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) struct flow_keys flkeys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) int ret, no_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) struct fib_result res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) struct flowi4 fl4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) bool dev_match;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) fl4.flowi4_oif = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) fl4.flowi4_iif = l3mdev_master_ifindex_rcu(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) if (!fl4.flowi4_iif)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) fl4.flowi4_iif = oif ? : LOOPBACK_IFINDEX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) fl4.daddr = src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) fl4.saddr = dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) fl4.flowi4_tos = tos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) fl4.flowi4_scope = RT_SCOPE_UNIVERSE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) fl4.flowi4_tun_key.tun_id = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) fl4.flowi4_flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) fl4.flowi4_uid = sock_net_uid(net, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) fl4.flowi4_multipath_hash = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) no_addr = idev->ifa_list == NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) fl4.flowi4_mark = IN_DEV_SRC_VMARK(idev) ? skb->mark : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) if (!fib4_rules_early_flow_dissect(net, skb, &fl4, &flkeys)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) fl4.flowi4_proto = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) fl4.fl4_sport = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) fl4.fl4_dport = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) swap(fl4.fl4_sport, fl4.fl4_dport);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) if (fib_lookup(net, &fl4, &res, 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) goto last_resort;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) if (res.type != RTN_UNICAST &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) (res.type != RTN_LOCAL || !IN_DEV_ACCEPT_LOCAL(idev)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) goto e_inval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) fib_combine_itag(itag, &res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) dev_match = fib_info_nh_uses_dev(res.fi, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) /* This is not common, loopback packets retain skb_dst so normally they
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) * would not even hit this slow path.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) dev_match = dev_match || (res.type == RTN_LOCAL &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) dev == net->loopback_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) if (dev_match) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) ret = FIB_RES_NHC(res)->nhc_scope >= RT_SCOPE_HOST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) if (no_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) goto last_resort;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) if (rpf == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) goto e_rpf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) fl4.flowi4_oif = dev->ifindex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) if (fib_lookup(net, &fl4, &res, FIB_LOOKUP_IGNORE_LINKSTATE) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) if (res.type == RTN_UNICAST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) ret = FIB_RES_NHC(res)->nhc_scope >= RT_SCOPE_HOST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) last_resort:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) if (rpf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) goto e_rpf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) *itag = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) e_inval:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) e_rpf:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) return -EXDEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) /* Ignore rp_filter for packets protected by IPsec. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) int fib_validate_source(struct sk_buff *skb, __be32 src, __be32 dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) u8 tos, int oif, struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) struct in_device *idev, u32 *itag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) int r = secpath_exists(skb) ? 0 : IN_DEV_RPFILTER(idev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) struct net *net = dev_net(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) if (!r && !fib_num_tclassid_users(net) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) (dev->ifindex != oif || !IN_DEV_TX_REDIRECTS(idev))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) if (IN_DEV_ACCEPT_LOCAL(idev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) goto ok;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) /* with custom local routes in place, checking local addresses
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) * only will be too optimistic, with custom rules, checking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) * local addresses only can be too strict, e.g. due to vrf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) if (net->ipv4.fib_has_custom_local_routes ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) fib4_has_custom_rules(net))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) goto full_check;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) if (inet_lookup_ifaddr_rcu(net, src))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) ok:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) *itag = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) full_check:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) return __fib_validate_source(skb, src, dst, tos, oif, dev, r, idev, itag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) static inline __be32 sk_extract_addr(struct sockaddr *addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) return ((struct sockaddr_in *) addr)->sin_addr.s_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) static int put_rtax(struct nlattr *mx, int len, int type, u32 value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) struct nlattr *nla;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) nla = (struct nlattr *) ((char *) mx + len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) nla->nla_type = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) nla->nla_len = nla_attr_size(4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) *(u32 *) nla_data(nla) = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) return len + nla_total_size(4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) static int rtentry_to_fib_config(struct net *net, int cmd, struct rtentry *rt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) struct fib_config *cfg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) __be32 addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) int plen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) memset(cfg, 0, sizeof(*cfg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) cfg->fc_nlinfo.nl_net = net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) if (rt->rt_dst.sa_family != AF_INET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) return -EAFNOSUPPORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) * Check mask for validity:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) * a) it must be contiguous.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) * b) destination must have all host bits clear.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) * c) if application forgot to set correct family (AF_INET),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) * reject request unless it is absolutely clear i.e.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) * both family and mask are zero.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) plen = 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) addr = sk_extract_addr(&rt->rt_dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) if (!(rt->rt_flags & RTF_HOST)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) __be32 mask = sk_extract_addr(&rt->rt_genmask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) if (rt->rt_genmask.sa_family != AF_INET) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) if (mask || rt->rt_genmask.sa_family)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) return -EAFNOSUPPORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) if (bad_mask(mask, addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) plen = inet_mask_len(mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) cfg->fc_dst_len = plen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) cfg->fc_dst = addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) if (cmd != SIOCDELRT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) cfg->fc_nlflags = NLM_F_CREATE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) cfg->fc_protocol = RTPROT_BOOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) if (rt->rt_metric)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) cfg->fc_priority = rt->rt_metric - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) if (rt->rt_flags & RTF_REJECT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) cfg->fc_scope = RT_SCOPE_HOST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) cfg->fc_type = RTN_UNREACHABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) cfg->fc_scope = RT_SCOPE_NOWHERE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) cfg->fc_type = RTN_UNICAST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) if (rt->rt_dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) char *colon;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) struct net_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) char devname[IFNAMSIZ];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) if (copy_from_user(devname, rt->rt_dev, IFNAMSIZ-1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) devname[IFNAMSIZ-1] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) colon = strchr(devname, ':');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) if (colon)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) *colon = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) dev = __dev_get_by_name(net, devname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) cfg->fc_oif = dev->ifindex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) cfg->fc_table = l3mdev_fib_table(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) if (colon) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) const struct in_ifaddr *ifa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) struct in_device *in_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) in_dev = __in_dev_get_rtnl(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) if (!in_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) *colon = ':';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) in_dev_for_each_ifa_rcu(ifa, in_dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) if (strcmp(ifa->ifa_label, devname) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) if (!ifa)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) cfg->fc_prefsrc = ifa->ifa_local;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) addr = sk_extract_addr(&rt->rt_gateway);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) if (rt->rt_gateway.sa_family == AF_INET && addr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) unsigned int addr_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) cfg->fc_gw4 = addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) cfg->fc_gw_family = AF_INET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) addr_type = inet_addr_type_table(net, addr, cfg->fc_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) if (rt->rt_flags & RTF_GATEWAY &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) addr_type == RTN_UNICAST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) cfg->fc_scope = RT_SCOPE_UNIVERSE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) if (cmd == SIOCDELRT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) if (rt->rt_flags & RTF_GATEWAY && !cfg->fc_gw_family)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) if (cfg->fc_scope == RT_SCOPE_NOWHERE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) cfg->fc_scope = RT_SCOPE_LINK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) if (rt->rt_flags & (RTF_MTU | RTF_WINDOW | RTF_IRTT)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) struct nlattr *mx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) int len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) mx = kcalloc(3, nla_total_size(4), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) if (!mx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) if (rt->rt_flags & RTF_MTU)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) len = put_rtax(mx, len, RTAX_ADVMSS, rt->rt_mtu - 40);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) if (rt->rt_flags & RTF_WINDOW)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) len = put_rtax(mx, len, RTAX_WINDOW, rt->rt_window);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) if (rt->rt_flags & RTF_IRTT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) len = put_rtax(mx, len, RTAX_RTT, rt->rt_irtt << 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) cfg->fc_mx = mx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) cfg->fc_mx_len = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) * Handle IP routing ioctl calls.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) * These are used to manipulate the routing tables
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) int ip_rt_ioctl(struct net *net, unsigned int cmd, struct rtentry *rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) struct fib_config cfg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) case SIOCADDRT: /* Add a route */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) case SIOCDELRT: /* Delete a route */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) rtnl_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) err = rtentry_to_fib_config(net, cmd, rt, &cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) if (err == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) struct fib_table *tb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) if (cmd == SIOCDELRT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) tb = fib_get_table(net, cfg.fc_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) if (tb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) err = fib_table_delete(net, tb, &cfg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) err = -ESRCH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) tb = fib_new_table(net, cfg.fc_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) if (tb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) err = fib_table_insert(net, tb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) &cfg, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) err = -ENOBUFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) /* allocated by rtentry_to_fib_config() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) kfree(cfg.fc_mx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) rtnl_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) const struct nla_policy rtm_ipv4_policy[RTA_MAX + 1] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) [RTA_UNSPEC] = { .strict_start_type = RTA_DPORT + 1 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) [RTA_DST] = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) [RTA_SRC] = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) [RTA_IIF] = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) [RTA_OIF] = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) [RTA_GATEWAY] = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) [RTA_PRIORITY] = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) [RTA_PREFSRC] = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) [RTA_METRICS] = { .type = NLA_NESTED },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) [RTA_FLOW] = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) [RTA_ENCAP_TYPE] = { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) [RTA_ENCAP] = { .type = NLA_NESTED },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) [RTA_UID] = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) [RTA_MARK] = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) [RTA_TABLE] = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) [RTA_IP_PROTO] = { .type = NLA_U8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) [RTA_SPORT] = { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) [RTA_DPORT] = { .type = NLA_U16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) [RTA_NH_ID] = { .type = NLA_U32 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) int fib_gw_from_via(struct fib_config *cfg, struct nlattr *nla,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) struct rtvia *via;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) int alen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) if (nla_len(nla) < offsetof(struct rtvia, rtvia_addr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) NL_SET_ERR_MSG(extack, "Invalid attribute length for RTA_VIA");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) via = nla_data(nla);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) alen = nla_len(nla) - offsetof(struct rtvia, rtvia_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) switch (via->rtvia_family) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) case AF_INET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) if (alen != sizeof(__be32)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) NL_SET_ERR_MSG(extack, "Invalid IPv4 address in RTA_VIA");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) cfg->fc_gw_family = AF_INET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) cfg->fc_gw4 = *((__be32 *)via->rtvia_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) case AF_INET6:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) #if IS_ENABLED(CONFIG_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) if (alen != sizeof(struct in6_addr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) NL_SET_ERR_MSG(extack, "Invalid IPv6 address in RTA_VIA");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) cfg->fc_gw_family = AF_INET6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) cfg->fc_gw6 = *((struct in6_addr *)via->rtvia_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) NL_SET_ERR_MSG(extack, "IPv6 support not enabled in kernel");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) NL_SET_ERR_MSG(extack, "Unsupported address family in RTA_VIA");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) static int rtm_to_fib_config(struct net *net, struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) struct nlmsghdr *nlh, struct fib_config *cfg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) bool has_gw = false, has_via = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) struct nlattr *attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) int err, remaining;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) struct rtmsg *rtm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) err = nlmsg_validate_deprecated(nlh, sizeof(*rtm), RTA_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) rtm_ipv4_policy, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) memset(cfg, 0, sizeof(*cfg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) rtm = nlmsg_data(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) cfg->fc_dst_len = rtm->rtm_dst_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) cfg->fc_tos = rtm->rtm_tos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) cfg->fc_table = rtm->rtm_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) cfg->fc_protocol = rtm->rtm_protocol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) cfg->fc_scope = rtm->rtm_scope;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) cfg->fc_type = rtm->rtm_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) cfg->fc_flags = rtm->rtm_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) cfg->fc_nlflags = nlh->nlmsg_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) cfg->fc_nlinfo.nlh = nlh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) cfg->fc_nlinfo.nl_net = net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) if (cfg->fc_type > RTN_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) NL_SET_ERR_MSG(extack, "Invalid route type");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) nlmsg_for_each_attr(attr, nlh, sizeof(struct rtmsg), remaining) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) switch (nla_type(attr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) case RTA_DST:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) cfg->fc_dst = nla_get_be32(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) case RTA_OIF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) cfg->fc_oif = nla_get_u32(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) case RTA_GATEWAY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) has_gw = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) cfg->fc_gw4 = nla_get_be32(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) if (cfg->fc_gw4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) cfg->fc_gw_family = AF_INET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) case RTA_VIA:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) has_via = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) err = fib_gw_from_via(cfg, attr, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) case RTA_PRIORITY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) cfg->fc_priority = nla_get_u32(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) case RTA_PREFSRC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) cfg->fc_prefsrc = nla_get_be32(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) case RTA_METRICS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) cfg->fc_mx = nla_data(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) cfg->fc_mx_len = nla_len(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) case RTA_MULTIPATH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) err = lwtunnel_valid_encap_type_attr(nla_data(attr),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) nla_len(attr),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) cfg->fc_mp = nla_data(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) cfg->fc_mp_len = nla_len(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) case RTA_FLOW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) cfg->fc_flow = nla_get_u32(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) case RTA_TABLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) cfg->fc_table = nla_get_u32(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) case RTA_ENCAP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) cfg->fc_encap = attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) case RTA_ENCAP_TYPE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) cfg->fc_encap_type = nla_get_u16(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) err = lwtunnel_valid_encap_type(cfg->fc_encap_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) case RTA_NH_ID:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) cfg->fc_nh_id = nla_get_u32(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) if (cfg->fc_nh_id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) if (cfg->fc_oif || cfg->fc_gw_family ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) cfg->fc_encap || cfg->fc_mp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) NL_SET_ERR_MSG(extack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) "Nexthop specification and nexthop id are mutually exclusive");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) if (has_gw && has_via) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) NL_SET_ERR_MSG(extack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) "Nexthop configuration can not contain both GATEWAY and VIA");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) errout:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) static int inet_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) struct net *net = sock_net(skb->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) struct fib_config cfg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) struct fib_table *tb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) err = rtm_to_fib_config(net, skb, nlh, &cfg, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) if (cfg.fc_nh_id && !nexthop_find_by_id(net, cfg.fc_nh_id)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) tb = fib_get_table(net, cfg.fc_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) if (!tb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) NL_SET_ERR_MSG(extack, "FIB table does not exist");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) err = -ESRCH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) err = fib_table_delete(net, tb, &cfg, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) errout:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) static int inet_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) struct net *net = sock_net(skb->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) struct fib_config cfg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) struct fib_table *tb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) err = rtm_to_fib_config(net, skb, nlh, &cfg, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) tb = fib_new_table(net, cfg.fc_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) if (!tb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) err = -ENOBUFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) err = fib_table_insert(net, tb, &cfg, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) if (!err && cfg.fc_type == RTN_LOCAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) net->ipv4.fib_has_custom_local_routes = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) errout:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) int ip_valid_fib_dump_req(struct net *net, const struct nlmsghdr *nlh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) struct fib_dump_filter *filter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) struct netlink_callback *cb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) struct netlink_ext_ack *extack = cb->extack;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) struct nlattr *tb[RTA_MAX + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) struct rtmsg *rtm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) int err, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) ASSERT_RTNL();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) NL_SET_ERR_MSG(extack, "Invalid header for FIB dump request");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) rtm = nlmsg_data(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) if (rtm->rtm_dst_len || rtm->rtm_src_len || rtm->rtm_tos ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) rtm->rtm_scope) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) NL_SET_ERR_MSG(extack, "Invalid values in header for FIB dump request");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) if (rtm->rtm_flags & ~(RTM_F_CLONED | RTM_F_PREFIX)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) NL_SET_ERR_MSG(extack, "Invalid flags for FIB dump request");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) if (rtm->rtm_flags & RTM_F_CLONED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) filter->dump_routes = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) filter->dump_exceptions = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) filter->flags = rtm->rtm_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) filter->protocol = rtm->rtm_protocol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) filter->rt_type = rtm->rtm_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) filter->table_id = rtm->rtm_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) rtm_ipv4_policy, extack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) for (i = 0; i <= RTA_MAX; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) int ifindex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) if (!tb[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) switch (i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) case RTA_TABLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) filter->table_id = nla_get_u32(tb[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) case RTA_OIF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) ifindex = nla_get_u32(tb[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) filter->dev = __dev_get_by_index(net, ifindex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) if (!filter->dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) NL_SET_ERR_MSG(extack, "Unsupported attribute in dump request");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) if (filter->flags || filter->protocol || filter->rt_type ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) filter->table_id || filter->dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) filter->filter_set = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) cb->answer_flags = NLM_F_DUMP_FILTERED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) EXPORT_SYMBOL_GPL(ip_valid_fib_dump_req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) static int inet_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) struct fib_dump_filter filter = { .dump_routes = true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) .dump_exceptions = true };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) const struct nlmsghdr *nlh = cb->nlh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) struct net *net = sock_net(skb->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) unsigned int h, s_h;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) unsigned int e = 0, s_e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) struct fib_table *tb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) struct hlist_head *head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) int dumped = 0, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) if (cb->strict_check) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) err = ip_valid_fib_dump_req(net, nlh, &filter, cb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) } else if (nlmsg_len(nlh) >= sizeof(struct rtmsg)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) struct rtmsg *rtm = nlmsg_data(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) filter.flags = rtm->rtm_flags & (RTM_F_PREFIX | RTM_F_CLONED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) /* ipv4 does not use prefix flag */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) if (filter.flags & RTM_F_PREFIX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) return skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) if (filter.table_id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) tb = fib_get_table(net, filter.table_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) if (!tb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) if (rtnl_msg_family(cb->nlh) != PF_INET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) return skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) NL_SET_ERR_MSG(cb->extack, "ipv4: FIB table does not exist");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) err = fib_table_dump(tb, skb, cb, &filter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) return skb->len ? : err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) s_h = cb->args[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) s_e = cb->args[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) for (h = s_h; h < FIB_TABLE_HASHSZ; h++, s_e = 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) e = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) head = &net->ipv4.fib_table_hash[h];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) hlist_for_each_entry_rcu(tb, head, tb_hlist) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) if (e < s_e)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) goto next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) if (dumped)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) memset(&cb->args[2], 0, sizeof(cb->args) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 2 * sizeof(cb->args[0]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) err = fib_table_dump(tb, skb, cb, &filter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) if (likely(skb->len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) dumped = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) next:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) e++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) err = skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) out_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) cb->args[1] = e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) cb->args[0] = h;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) /* Prepare and feed intra-kernel routing request.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) * Really, it should be netlink message, but :-( netlink
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) * can be not configured, so that we feed it directly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) * to fib engine. It is legal, because all events occur
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) * only when netlink is already locked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) static void fib_magic(int cmd, int type, __be32 dst, int dst_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) struct in_ifaddr *ifa, u32 rt_priority)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) struct net *net = dev_net(ifa->ifa_dev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) u32 tb_id = l3mdev_fib_table(ifa->ifa_dev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) struct fib_table *tb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) struct fib_config cfg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) .fc_protocol = RTPROT_KERNEL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) .fc_type = type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) .fc_dst = dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) .fc_dst_len = dst_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) .fc_priority = rt_priority,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) .fc_prefsrc = ifa->ifa_local,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) .fc_oif = ifa->ifa_dev->dev->ifindex,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) .fc_nlflags = NLM_F_CREATE | NLM_F_APPEND,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) .fc_nlinfo = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) .nl_net = net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) if (!tb_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) tb_id = (type == RTN_UNICAST) ? RT_TABLE_MAIN : RT_TABLE_LOCAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) tb = fib_new_table(net, tb_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) if (!tb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) cfg.fc_table = tb->tb_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) if (type != RTN_LOCAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) cfg.fc_scope = RT_SCOPE_LINK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) cfg.fc_scope = RT_SCOPE_HOST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) if (cmd == RTM_NEWROUTE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) fib_table_insert(net, tb, &cfg, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) fib_table_delete(net, tb, &cfg, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) void fib_add_ifaddr(struct in_ifaddr *ifa)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) struct in_device *in_dev = ifa->ifa_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) struct net_device *dev = in_dev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) struct in_ifaddr *prim = ifa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) __be32 mask = ifa->ifa_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) __be32 addr = ifa->ifa_local;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) __be32 prefix = ifa->ifa_address & mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) if (ifa->ifa_flags & IFA_F_SECONDARY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) prim = inet_ifa_byprefix(in_dev, prefix, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) if (!prim) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) pr_warn("%s: bug: prim == NULL\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) fib_magic(RTM_NEWROUTE, RTN_LOCAL, addr, 32, prim, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) if (!(dev->flags & IFF_UP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) /* Add broadcast address, if it is explicitly assigned. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) if (ifa->ifa_broadcast && ifa->ifa_broadcast != htonl(0xFFFFFFFF))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) fib_magic(RTM_NEWROUTE, RTN_BROADCAST, ifa->ifa_broadcast, 32,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) prim, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) if (!ipv4_is_zeronet(prefix) && !(ifa->ifa_flags & IFA_F_SECONDARY) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) (prefix != addr || ifa->ifa_prefixlen < 32)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) if (!(ifa->ifa_flags & IFA_F_NOPREFIXROUTE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) fib_magic(RTM_NEWROUTE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) dev->flags & IFF_LOOPBACK ? RTN_LOCAL : RTN_UNICAST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) prefix, ifa->ifa_prefixlen, prim,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) ifa->ifa_rt_priority);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) /* Add network specific broadcasts, when it takes a sense */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) if (ifa->ifa_prefixlen < 31) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) fib_magic(RTM_NEWROUTE, RTN_BROADCAST, prefix, 32,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) prim, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) fib_magic(RTM_NEWROUTE, RTN_BROADCAST, prefix | ~mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 32, prim, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) void fib_modify_prefix_metric(struct in_ifaddr *ifa, u32 new_metric)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) __be32 prefix = ifa->ifa_address & ifa->ifa_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) struct in_device *in_dev = ifa->ifa_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) struct net_device *dev = in_dev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) if (!(dev->flags & IFF_UP) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) ifa->ifa_flags & (IFA_F_SECONDARY | IFA_F_NOPREFIXROUTE) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) ipv4_is_zeronet(prefix) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) (prefix == ifa->ifa_local && ifa->ifa_prefixlen == 32))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) /* add the new */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) fib_magic(RTM_NEWROUTE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) dev->flags & IFF_LOOPBACK ? RTN_LOCAL : RTN_UNICAST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) prefix, ifa->ifa_prefixlen, ifa, new_metric);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) /* delete the old */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) fib_magic(RTM_DELROUTE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) dev->flags & IFF_LOOPBACK ? RTN_LOCAL : RTN_UNICAST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) prefix, ifa->ifa_prefixlen, ifa, ifa->ifa_rt_priority);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) /* Delete primary or secondary address.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) * Optionally, on secondary address promotion consider the addresses
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) * from subnet iprim as deleted, even if they are in device list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) * In this case the secondary ifa can be in device list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) void fib_del_ifaddr(struct in_ifaddr *ifa, struct in_ifaddr *iprim)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) struct in_device *in_dev = ifa->ifa_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) struct net_device *dev = in_dev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) struct in_ifaddr *ifa1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) struct in_ifaddr *prim = ifa, *prim1 = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) __be32 brd = ifa->ifa_address | ~ifa->ifa_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) __be32 any = ifa->ifa_address & ifa->ifa_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) #define LOCAL_OK 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) #define BRD_OK 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) #define BRD0_OK 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) #define BRD1_OK 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) unsigned int ok = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) int subnet = 0; /* Primary network */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) int gone = 1; /* Address is missing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) int same_prefsrc = 0; /* Another primary with same IP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) if (ifa->ifa_flags & IFA_F_SECONDARY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) prim = inet_ifa_byprefix(in_dev, any, ifa->ifa_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) if (!prim) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) /* if the device has been deleted, we don't perform
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) * address promotion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) if (!in_dev->dead)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) pr_warn("%s: bug: prim == NULL\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) if (iprim && iprim != prim) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) pr_warn("%s: bug: iprim != prim\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) } else if (!ipv4_is_zeronet(any) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) (any != ifa->ifa_local || ifa->ifa_prefixlen < 32)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) if (!(ifa->ifa_flags & IFA_F_NOPREFIXROUTE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) fib_magic(RTM_DELROUTE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) dev->flags & IFF_LOOPBACK ? RTN_LOCAL : RTN_UNICAST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) any, ifa->ifa_prefixlen, prim, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) subnet = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) if (in_dev->dead)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) goto no_promotions;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) /* Deletion is more complicated than add.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) * We should take care of not to delete too much :-)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) * Scan address list to be sure that addresses are really gone.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) in_dev_for_each_ifa_rcu(ifa1, in_dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) if (ifa1 == ifa) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) /* promotion, keep the IP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) gone = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) /* Ignore IFAs from our subnet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) if (iprim && ifa1->ifa_mask == iprim->ifa_mask &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) inet_ifa_match(ifa1->ifa_address, iprim))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) /* Ignore ifa1 if it uses different primary IP (prefsrc) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) if (ifa1->ifa_flags & IFA_F_SECONDARY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) /* Another address from our subnet? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) if (ifa1->ifa_mask == prim->ifa_mask &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) inet_ifa_match(ifa1->ifa_address, prim))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) prim1 = prim;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) /* We reached the secondaries, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) * same_prefsrc should be determined.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) if (!same_prefsrc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) /* Search new prim1 if ifa1 is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) * using the current prim1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) if (!prim1 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) ifa1->ifa_mask != prim1->ifa_mask ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) !inet_ifa_match(ifa1->ifa_address, prim1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) prim1 = inet_ifa_byprefix(in_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) ifa1->ifa_address,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) ifa1->ifa_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) if (!prim1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) if (prim1->ifa_local != prim->ifa_local)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) if (prim->ifa_local != ifa1->ifa_local)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) prim1 = ifa1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) if (prim != prim1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) same_prefsrc = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) if (ifa->ifa_local == ifa1->ifa_local)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) ok |= LOCAL_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) if (ifa->ifa_broadcast == ifa1->ifa_broadcast)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) ok |= BRD_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) if (brd == ifa1->ifa_broadcast)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) ok |= BRD1_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) if (any == ifa1->ifa_broadcast)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) ok |= BRD0_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) /* primary has network specific broadcasts */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) if (prim1 == ifa1 && ifa1->ifa_prefixlen < 31) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) __be32 brd1 = ifa1->ifa_address | ~ifa1->ifa_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) __be32 any1 = ifa1->ifa_address & ifa1->ifa_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) if (!ipv4_is_zeronet(any1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) if (ifa->ifa_broadcast == brd1 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) ifa->ifa_broadcast == any1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) ok |= BRD_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) if (brd == brd1 || brd == any1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) ok |= BRD1_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) if (any == brd1 || any == any1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) ok |= BRD0_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) no_promotions:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) if (!(ok & BRD_OK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) fib_magic(RTM_DELROUTE, RTN_BROADCAST, ifa->ifa_broadcast, 32,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) prim, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) if (subnet && ifa->ifa_prefixlen < 31) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) if (!(ok & BRD1_OK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) fib_magic(RTM_DELROUTE, RTN_BROADCAST, brd, 32,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) prim, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) if (!(ok & BRD0_OK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) fib_magic(RTM_DELROUTE, RTN_BROADCAST, any, 32,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) prim, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) if (!(ok & LOCAL_OK)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) unsigned int addr_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) fib_magic(RTM_DELROUTE, RTN_LOCAL, ifa->ifa_local, 32, prim, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) /* Check, that this local address finally disappeared. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) addr_type = inet_addr_type_dev_table(dev_net(dev), dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) ifa->ifa_local);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) if (gone && addr_type != RTN_LOCAL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) /* And the last, but not the least thing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) * We must flush stray FIB entries.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) * First of all, we scan fib_info list searching
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) * for stray nexthop entries, then ignite fib_flush.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) if (fib_sync_down_addr(dev, ifa->ifa_local))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) fib_flush(dev_net(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) #undef LOCAL_OK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) #undef BRD_OK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) #undef BRD0_OK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) #undef BRD1_OK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) static void nl_fib_lookup(struct net *net, struct fib_result_nl *frn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) struct fib_result res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) struct flowi4 fl4 = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) .flowi4_mark = frn->fl_mark,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) .daddr = frn->fl_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) .flowi4_tos = frn->fl_tos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) .flowi4_scope = frn->fl_scope,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) struct fib_table *tb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) tb = fib_get_table(net, frn->tb_id_in);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) frn->err = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) if (tb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) local_bh_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) frn->tb_id = tb->tb_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) frn->err = fib_table_lookup(tb, &fl4, &res, FIB_LOOKUP_NOREF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) if (!frn->err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) frn->prefixlen = res.prefixlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) frn->nh_sel = res.nh_sel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) frn->type = res.type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) frn->scope = res.scope;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) local_bh_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) static void nl_fib_input(struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) struct net *net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) struct fib_result_nl *frn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) struct nlmsghdr *nlh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) u32 portid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) net = sock_net(skb->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) nlh = nlmsg_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) if (skb->len < nlmsg_total_size(sizeof(*frn)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) skb->len < nlh->nlmsg_len ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) nlmsg_len(nlh) < sizeof(*frn))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) skb = netlink_skb_clone(skb, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) if (!skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) nlh = nlmsg_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) frn = (struct fib_result_nl *) nlmsg_data(nlh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) nl_fib_lookup(net, frn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) portid = NETLINK_CB(skb).portid; /* netlink portid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) NETLINK_CB(skb).portid = 0; /* from kernel */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) NETLINK_CB(skb).dst_group = 0; /* unicast */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) netlink_unicast(net->ipv4.fibnl, skb, portid, MSG_DONTWAIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) static int __net_init nl_fib_lookup_init(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) struct sock *sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) struct netlink_kernel_cfg cfg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) .input = nl_fib_input,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) sk = netlink_kernel_create(net, NETLINK_FIB_LOOKUP, &cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) if (!sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) return -EAFNOSUPPORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) net->ipv4.fibnl = sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) static void nl_fib_lookup_exit(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) netlink_kernel_release(net->ipv4.fibnl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) net->ipv4.fibnl = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) static void fib_disable_ip(struct net_device *dev, unsigned long event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) bool force)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) if (fib_sync_down_dev(dev, event, force))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) fib_flush(dev_net(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) rt_cache_flush(dev_net(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) arp_ifdown(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) static int fib_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) struct net_device *dev = ifa->ifa_dev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) struct net *net = dev_net(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) switch (event) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) case NETDEV_UP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) fib_add_ifaddr(ifa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) #ifdef CONFIG_IP_ROUTE_MULTIPATH
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) fib_sync_up(dev, RTNH_F_DEAD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) atomic_inc(&net->ipv4.dev_addr_genid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) rt_cache_flush(dev_net(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) case NETDEV_DOWN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) fib_del_ifaddr(ifa, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) atomic_inc(&net->ipv4.dev_addr_genid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) if (!ifa->ifa_dev->ifa_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) /* Last address was deleted from this interface.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) * Disable IP.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) fib_disable_ip(dev, event, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) rt_cache_flush(dev_net(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) static int fib_netdev_event(struct notifier_block *this, unsigned long event, void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) struct net_device *dev = netdev_notifier_info_to_dev(ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) struct netdev_notifier_changeupper_info *upper_info = ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) struct netdev_notifier_info_ext *info_ext = ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) struct in_device *in_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) struct net *net = dev_net(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) struct in_ifaddr *ifa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) unsigned int flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) if (event == NETDEV_UNREGISTER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) fib_disable_ip(dev, event, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) rt_flush_dev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) in_dev = __in_dev_get_rtnl(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) if (!in_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) switch (event) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) case NETDEV_UP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) in_dev_for_each_ifa_rtnl(ifa, in_dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) fib_add_ifaddr(ifa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) #ifdef CONFIG_IP_ROUTE_MULTIPATH
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) fib_sync_up(dev, RTNH_F_DEAD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) atomic_inc(&net->ipv4.dev_addr_genid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) rt_cache_flush(net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) case NETDEV_DOWN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) fib_disable_ip(dev, event, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) case NETDEV_CHANGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) flags = dev_get_flags(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) if (flags & (IFF_RUNNING | IFF_LOWER_UP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) fib_sync_up(dev, RTNH_F_LINKDOWN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) fib_sync_down_dev(dev, event, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) rt_cache_flush(net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) case NETDEV_CHANGEMTU:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) fib_sync_mtu(dev, info_ext->ext.mtu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) rt_cache_flush(net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) case NETDEV_CHANGEUPPER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) upper_info = ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) /* flush all routes if dev is linked to or unlinked from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) * an L3 master device (e.g., VRF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) if (upper_info->upper_dev &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) netif_is_l3_master(upper_info->upper_dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) fib_disable_ip(dev, NETDEV_DOWN, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) static struct notifier_block fib_inetaddr_notifier = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) .notifier_call = fib_inetaddr_event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) static struct notifier_block fib_netdev_notifier = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) .notifier_call = fib_netdev_event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) static int __net_init ip_fib_net_init(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) size_t size = sizeof(struct hlist_head) * FIB_TABLE_HASHSZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) err = fib4_notifier_init(net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) /* Avoid false sharing : Use at least a full cache line */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) size = max_t(size_t, size, L1_CACHE_BYTES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) net->ipv4.fib_table_hash = kzalloc(size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) if (!net->ipv4.fib_table_hash) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) goto err_table_hash_alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) err = fib4_rules_init(net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) goto err_rules_init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) err_rules_init:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) kfree(net->ipv4.fib_table_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) err_table_hash_alloc:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) fib4_notifier_exit(net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) static void ip_fib_net_exit(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) rtnl_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) #ifdef CONFIG_IP_MULTIPLE_TABLES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) RCU_INIT_POINTER(net->ipv4.fib_main, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) RCU_INIT_POINTER(net->ipv4.fib_default, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) /* Destroy the tables in reverse order to guarantee that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) * local table, ID 255, is destroyed before the main table, ID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) * 254. This is necessary as the local table may contain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) * references to data contained in the main table.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) for (i = FIB_TABLE_HASHSZ - 1; i >= 0; i--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) struct hlist_head *head = &net->ipv4.fib_table_hash[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) struct hlist_node *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) struct fib_table *tb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) hlist_for_each_entry_safe(tb, tmp, head, tb_hlist) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) hlist_del(&tb->tb_hlist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) fib_table_flush(net, tb, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) fib_free_table(tb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) #ifdef CONFIG_IP_MULTIPLE_TABLES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) fib4_rules_exit(net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) rtnl_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) kfree(net->ipv4.fib_table_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) fib4_notifier_exit(net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) static int __net_init fib_net_init(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) #ifdef CONFIG_IP_ROUTE_CLASSID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) atomic_set(&net->ipv4.fib_num_tclassid_users, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) error = ip_fib_net_init(net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) if (error < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) error = nl_fib_lookup_init(net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) if (error < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) goto out_nlfl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) error = fib_proc_init(net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) if (error < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) goto out_proc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) out_proc:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) nl_fib_lookup_exit(net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) out_nlfl:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) ip_fib_net_exit(net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) static void __net_exit fib_net_exit(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) fib_proc_exit(net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) nl_fib_lookup_exit(net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) ip_fib_net_exit(net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) static struct pernet_operations fib_net_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) .init = fib_net_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) .exit = fib_net_exit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) void __init ip_fib_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) fib_trie_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) register_pernet_subsys(&fib_net_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) register_netdevice_notifier(&fib_netdev_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) register_inetaddr_notifier(&fib_inetaddr_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) rtnl_register(PF_INET, RTM_NEWROUTE, inet_rtm_newroute, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) rtnl_register(PF_INET, RTM_DELROUTE, inet_rtm_delroute, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) rtnl_register(PF_INET, RTM_GETROUTE, NULL, inet_dump_fib, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) }