16 #include <asm/uaccess.h>
17 #include <linux/bitops.h>
18 #include <linux/types.h>
19 #include <linux/kernel.h>
22 #include <linux/string.h>
23 #include <linux/socket.h>
25 #include <linux/errno.h>
29 #include <linux/netdevice.h>
30 #include <linux/if_arp.h>
34 #include <linux/slab.h>
51 static unsigned int fib_info_hash_size;
52 static unsigned int fib_info_cnt;
54 #define DEVINDEX_HASHBITS 8
55 #define DEVINDEX_HASHSIZE (1U << DEVINDEX_HASHBITS)
58 #ifdef CONFIG_IP_ROUTE_MULTIPATH
62 #define for_nexthops(fi) { \
63 int nhsel; const struct fib_nh *nh; \
64 for (nhsel = 0, nh = (fi)->fib_nh; \
65 nhsel < (fi)->fib_nhs; \
68 #define change_nexthops(fi) { \
69 int nhsel; struct fib_nh *nexthop_nh; \
70 for (nhsel = 0, nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \
71 nhsel < (fi)->fib_nhs; \
72 nexthop_nh++, nhsel++)
78 #define for_nexthops(fi) { \
79 int nhsel; const struct fib_nh *nh = (fi)->fib_nh; \
80 for (nhsel = 0; nhsel < 1; nhsel++)
82 #define change_nexthops(fi) { \
84 struct fib_nh *nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \
85 for (nhsel = 0; nhsel < 1; nhsel++)
89 #define endfor_nexthops(fi) }
143 static void rt_fibinfo_free(
struct rtable __rcu **rtp)
158 static void free_nh_exceptions(
struct fib_nh *nh)
205 if (nexthop_nh->nh_dev)
206 dev_put(nexthop_nh->nh_dev);
207 if (nexthop_nh->nh_exceptions)
208 free_nh_exceptions(nexthop_nh);
209 rt_fibinfo_free_cpus(nexthop_nh->nh_pcpu_rth_output);
210 rt_fibinfo_free(&nexthop_nh->nh_rth_input);
222 pr_warn(
"Freeing alive fib_info %p\n", fi);
226 #ifdef CONFIG_IP_ROUTE_CLASSID
228 if (nexthop_nh->nh_tclassid)
229 fi->
fib_net->ipv4.fib_num_tclassid_users--;
237 spin_lock_bh(&fib_info_lock);
243 if (!nexthop_nh->nh_dev)
245 hlist_del(&nexthop_nh->nh_hash);
250 spin_unlock_bh(&fib_info_lock);
253 static inline int nh_comp(
const struct fib_info *fi,
const struct fib_info *ofi)
261 #ifdef CONFIG_IP_ROUTE_MULTIPATH
262 nh->nh_weight != onh->nh_weight ||
264 #ifdef CONFIG_IP_ROUTE_CLASSID
265 nh->nh_tclassid != onh->nh_tclassid ||
274 static inline unsigned int fib_devindex_hashfn(
unsigned int val)
283 static inline unsigned int fib_info_hashfn(
const struct fib_info *fi)
285 unsigned int mask = (fib_info_hash_size - 1);
286 unsigned int val = fi->
fib_nhs;
292 val ^= fib_devindex_hashfn(nh->
nh_oif);
295 return (val ^ (val >> 7) ^ (val >> 12)) & mask;
305 hash = fib_info_hashfn(nfi);
306 head = &fib_info_hash[
hash];
309 if (!net_eq(fi->
fib_net, nfi->fib_net))
311 if (fi->
fib_nhs != nfi->fib_nhs)
321 (nfi->fib_nhs == 0 || nh_comp(fi, nfi) == 0))
338 spin_lock(&fib_info_lock);
340 hash = fib_devindex_hashfn(dev->
ifindex);
341 head = &fib_info_devhash[
hash];
346 spin_unlock(&fib_info_lock);
351 spin_unlock(&fib_info_lock);
356 static inline size_t fib_nlmsg_size(
struct fib_info *fi)
365 payload += nla_total_size((
RTAX_MAX * nla_total_size(4)));
371 size_t nhsize = nla_total_size(
sizeof(
struct rtnexthop));
374 nhsize += 2 * nla_total_size(4);
377 payload += nla_total_size(fi->
fib_nhs * nhsize);
385 unsigned int nlm_flags)
388 u32
seq = info->
nlh ? info->
nlh->nlmsg_seq : 0;
422 if (fa->
fa_info->fib_priority >= prio ||
431 struct fib_info **last_resort,
int *last_idx,
int dflt)
443 if ((state &
NUD_VALID) && order != dflt)
445 if ((state & NUD_VALID) ||
446 (*last_idx < 0 && order > dflt)) {
453 #ifdef CONFIG_IP_ROUTE_MULTIPATH
455 static int fib_count_nexthops(
struct rtnexthop *rtnh,
int remaining)
459 while (rtnh_ok(rtnh, remaining)) {
461 rtnh = rtnh_next(rtnh, &remaining);
465 return remaining > 0 ? 0 : nhs;
474 if (!rtnh_ok(rtnh, remaining))
477 nexthop_nh->nh_flags =
480 nexthop_nh->nh_weight = rtnh->
rtnh_hops + 1;
482 attrlen = rtnh_attrlen(rtnh);
484 struct nlattr *
nla, *attrs = rtnh_attrs(rtnh);
487 nexthop_nh->nh_gw = nla ? nla_get_be32(nla) : 0;
488 #ifdef CONFIG_IP_ROUTE_CLASSID
490 nexthop_nh->nh_tclassid = nla ? nla_get_u32(nla) : 0;
491 if (nexthop_nh->nh_tclassid)
492 fi->
fib_net->ipv4.fib_num_tclassid_users++;
496 rtnh = rtnh_next(rtnh, &remaining);
506 #ifdef CONFIG_IP_ROUTE_MULTIPATH
521 #ifdef CONFIG_IP_ROUTE_MULTIPATH
531 if (!rtnh_ok(rtnh, remaining))
537 attrlen = rtnh_attrlen(rtnh);
539 struct nlattr *
nla, *attrs = rtnh_attrs(rtnh);
542 if (nla && nla_get_be32(nla) != nh->
nh_gw)
544 #ifdef CONFIG_IP_ROUTE_CLASSID
546 if (nla && nla_get_u32(nla) != nh->nh_tclassid)
551 rtnh = rtnh_next(rtnh, &remaining);
639 err = fib_lookup(net, &fl4, &
res);
656 struct in_device *in_dev;
667 if (!(in_dev->dev->flags &
IFF_UP))
679 static inline unsigned int fib_laddr_hashfn(
__be32 val)
681 unsigned int mask = (fib_info_hash_size - 1);
698 static void fib_info_hash_free(
struct hlist_head *hash,
int bytes)
709 static void fib_info_hash_move(
struct hlist_head *new_info_hash,
711 unsigned int new_size)
713 struct hlist_head *old_info_hash, *old_laddrhash;
714 unsigned int old_size = fib_info_hash_size;
717 spin_lock_bh(&fib_info_lock);
718 old_info_hash = fib_info_hash;
719 old_laddrhash = fib_info_laddrhash;
720 fib_info_hash_size = new_size;
729 unsigned int new_hash;
733 new_hash = fib_info_hashfn(fi);
734 dest = &new_info_hash[new_hash];
735 hlist_add_head(&fi->
fib_hash, dest);
738 fib_info_hash = new_info_hash;
747 unsigned int new_hash;
752 ldest = &new_laddrhash[new_hash];
756 fib_info_laddrhash = new_laddrhash;
758 spin_unlock_bh(&fib_info_lock);
760 bytes = old_size *
sizeof(
struct hlist_head *);
761 fib_info_hash_free(old_info_hash, bytes);
762 fib_info_hash_free(old_laddrhash, bytes);
790 #ifdef CONFIG_IP_ROUTE_MULTIPATH
799 if (fib_info_cnt >= fib_info_hash_size) {
800 unsigned int new_size = fib_info_hash_size << 1;
807 bytes = new_size *
sizeof(
struct hlist_head *);
808 new_info_hash = fib_info_hash_alloc(bytes);
809 new_laddrhash = fib_info_hash_alloc(bytes);
810 if (!new_info_hash || !new_laddrhash) {
811 fib_info_hash_free(new_info_hash, bytes);
812 fib_info_hash_free(new_laddrhash, bytes);
814 fib_info_hash_move(new_info_hash, new_laddrhash, new_size);
816 if (!fib_info_hash_size)
841 nexthop_nh->nh_parent = fi;
843 if (!nexthop_nh->nh_pcpu_rth_output)
859 val = nla_get_u32(nla);
862 if (type ==
RTAX_MTU && val > 65535 - 15)
870 #ifdef CONFIG_IP_ROUTE_MULTIPATH
878 #ifdef CONFIG_IP_ROUTE_CLASSID
891 #ifdef CONFIG_IP_ROUTE_CLASSID
892 nh->nh_tclassid = cfg->
fc_flow;
894 fi->
fib_net->ipv4.fib_num_tclassid_users++;
896 #ifdef CONFIG_IP_ROUTE_MULTIPATH
925 if (nhs != 1 || nh->
nh_gw)
934 err = fib_check_nh(cfg, fi, nexthop_nh);
952 ofi = fib_find_info(fi);
962 spin_lock_bh(&fib_info_lock);
964 &fib_info_hash[fib_info_hashfn(fi)]);
968 head = &fib_info_laddrhash[fib_laddr_hashfn(fi->
fib_prefsrc)];
975 if (!nexthop_nh->nh_dev)
977 hash = fib_devindex_hashfn(nexthop_nh->nh_dev->ifindex);
978 head = &fib_info_devhash[
hash];
979 hlist_add_head(&nexthop_nh->nh_hash, head);
981 spin_unlock_bh(&fib_info_lock);
1003 nlh = nlmsg_put(skb, portid, seq, event,
sizeof(*rtm), flags);
1007 rtm = nlmsg_data(nlh);
1017 goto nla_put_failure;
1024 nla_put_be32(skb,
RTA_DST, dst))
1025 goto nla_put_failure;
1028 goto nla_put_failure;
1030 goto nla_put_failure;
1034 goto nla_put_failure;
1038 goto nla_put_failure;
1039 if (fi->
fib_nh->nh_oif &&
1041 goto nla_put_failure;
1042 #ifdef CONFIG_IP_ROUTE_CLASSID
1043 if (fi->
fib_nh[0].nh_tclassid &&
1045 goto nla_put_failure;
1048 #ifdef CONFIG_IP_ROUTE_MULTIPATH
1055 goto nla_put_failure;
1060 goto nla_put_failure;
1068 goto nla_put_failure;
1069 #ifdef CONFIG_IP_ROUTE_CLASSID
1070 if (nh->nh_tclassid &&
1071 nla_put_u32(skb,
RTA_FLOW, nh->nh_tclassid))
1072 goto nla_put_failure;
1075 rtnh->
rtnh_len = nlmsg_get_pos(skb) - (
void *) rtnh;
1078 nla_nest_end(skb, mp);
1081 return nlmsg_end(skb, nlh);
1084 nlmsg_cancel(skb, nlh);
1097 unsigned int hash = fib_laddr_hashfn(local);
1102 if (fib_info_laddrhash ==
NULL || local == 0)
1106 if (!net_eq(fi->
fib_net, net))
1121 unsigned int hash = fib_devindex_hashfn(dev->
ifindex);
1134 if (nh->
nh_dev != dev || fi == prev_fi)
1141 else if (nexthop_nh->nh_dev == dev &&
1142 nexthop_nh->nh_scope != scope) {
1144 #ifdef CONFIG_IP_ROUTE_MULTIPATH
1145 spin_lock_bh(&fib_multipath_lock);
1146 fi->fib_power -= nexthop_nh->nh_power;
1147 nexthop_nh->nh_power = 0;
1148 spin_unlock_bh(&fib_multipath_lock);
1152 #ifdef CONFIG_IP_ROUTE_MULTIPATH
1153 if (force > 1 && nexthop_nh->nh_dev == dev) {
1174 int order = -1, last_idx = -1;
1177 list_for_each_entry_rcu(fa, fa_head,
fa_list) {
1186 if (!next_fi->
fib_nh[0].nh_gw ||
1190 fib_alias_accessed(fa);
1193 if (next_fi != res->
fi)
1197 fib_result_assign(res, fi);
1205 if (order <= 0 || fi ==
NULL) {
1212 fib_result_assign(res, fi);
1218 fib_result_assign(res, last_resort);
1224 #ifdef CONFIG_IP_ROUTE_MULTIPATH
1243 hash = fib_devindex_hashfn(dev->
ifindex);
1244 head = &fib_info_devhash[
hash];
1252 if (nh->
nh_dev != dev || fi == prev_fi)
1262 if (nexthop_nh->nh_dev ==
NULL ||
1263 !(nexthop_nh->nh_dev->flags &
IFF_UP))
1265 if (nexthop_nh->nh_dev != dev ||
1266 !__in_dev_get_rtnl(dev))
1269 spin_lock_bh(&fib_multipath_lock);
1270 nexthop_nh->nh_power = 0;
1272 spin_unlock_bh(&fib_multipath_lock);
1293 spin_lock_bh(&fib_multipath_lock);
1294 if (fi->fib_power <= 0) {
1298 power += nexthop_nh->nh_weight;
1299 nexthop_nh->nh_power = nexthop_nh->nh_weight;
1302 fi->fib_power = power;
1304 spin_unlock_bh(&fib_multipath_lock);
1320 nexthop_nh->nh_power) {
1321 w -= nexthop_nh->nh_power;
1323 nexthop_nh->nh_power--;
1326 spin_unlock_bh(&fib_multipath_lock);
1334 spin_unlock_bh(&fib_multipath_lock);