2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
6 * IPv4 Forwarding Information Base: semantics.
8 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
16 #include <asm/uaccess.h>
17 #include <linux/bitops.h>
18 #include <linux/types.h>
19 #include <linux/kernel.h>
20 #include <linux/jiffies.h>
22 #include <linux/string.h>
23 #include <linux/socket.h>
24 #include <linux/sockios.h>
25 #include <linux/errno.h>
27 #include <linux/inet.h>
28 #include <linux/inetdevice.h>
29 #include <linux/netdevice.h>
30 #include <linux/if_arp.h>
31 #include <linux/proc_fs.h>
32 #include <linux/skbuff.h>
33 #include <linux/init.h>
34 #include <linux/slab.h>
38 #include <net/protocol.h>
39 #include <net/route.h>
42 #include <net/ip_fib.h>
43 #include <net/netlink.h>
44 #include <net/nexthop.h>
46 #include "fib_lookup.h"
48 static DEFINE_SPINLOCK(fib_info_lock
);
49 static struct hlist_head
*fib_info_hash
;
50 static struct hlist_head
*fib_info_laddrhash
;
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)
56 static struct hlist_head fib_info_devhash
[DEVINDEX_HASHSIZE
];
58 #ifdef CONFIG_IP_ROUTE_MULTIPATH
60 static DEFINE_SPINLOCK(fib_multipath_lock
);
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++)
74 #else /* CONFIG_IP_ROUTE_MULTIPATH */
76 /* Hope, that gcc will optimize it to get rid of dummy loop */
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++)
87 #endif /* CONFIG_IP_ROUTE_MULTIPATH */
89 #define endfor_nexthops(fi) }
92 const struct fib_prop fib_props
[RTN_MAX
+ 1] = {
95 .scope
= RT_SCOPE_NOWHERE
,
99 .scope
= RT_SCOPE_UNIVERSE
,
103 .scope
= RT_SCOPE_HOST
,
107 .scope
= RT_SCOPE_LINK
,
111 .scope
= RT_SCOPE_LINK
,
115 .scope
= RT_SCOPE_UNIVERSE
,
119 .scope
= RT_SCOPE_UNIVERSE
,
121 [RTN_UNREACHABLE
] = {
122 .error
= -EHOSTUNREACH
,
123 .scope
= RT_SCOPE_UNIVERSE
,
127 .scope
= RT_SCOPE_UNIVERSE
,
131 .scope
= RT_SCOPE_UNIVERSE
,
135 .scope
= RT_SCOPE_NOWHERE
,
139 .scope
= RT_SCOPE_NOWHERE
,
143 static void rt_fibinfo_free(struct rtable __rcu
**rtp
)
145 struct rtable
*rt
= rcu_dereference_protected(*rtp
, 1);
150 /* Not even needed : RCU_INIT_POINTER(*rtp, NULL);
151 * because we waited an RCU grace period before calling
152 * free_fib_info_rcu()
158 static void free_nh_exceptions(struct fib_nh
*nh
)
160 struct fnhe_hash_bucket
*hash
;
163 hash
= rcu_dereference_protected(nh
->nh_exceptions
, 1);
166 for (i
= 0; i
< FNHE_HASH_SIZE
; i
++) {
167 struct fib_nh_exception
*fnhe
;
169 fnhe
= rcu_dereference_protected(hash
[i
].chain
, 1);
171 struct fib_nh_exception
*next
;
173 next
= rcu_dereference_protected(fnhe
->fnhe_next
, 1);
175 rt_fibinfo_free(&fnhe
->fnhe_rth_input
);
176 rt_fibinfo_free(&fnhe
->fnhe_rth_output
);
186 static void rt_fibinfo_free_cpus(struct rtable __rcu
* __percpu
*rtp
)
193 for_each_possible_cpu(cpu
) {
196 rt
= rcu_dereference_protected(*per_cpu_ptr(rtp
, cpu
), 1);
203 /* Release a nexthop info record */
204 static void free_fib_info_rcu(struct rcu_head
*head
)
206 struct fib_info
*fi
= container_of(head
, struct fib_info
, rcu
);
208 change_nexthops(fi
) {
209 if (nexthop_nh
->nh_dev
)
210 dev_put(nexthop_nh
->nh_dev
);
211 free_nh_exceptions(nexthop_nh
);
212 rt_fibinfo_free_cpus(nexthop_nh
->nh_pcpu_rth_output
);
213 rt_fibinfo_free(&nexthop_nh
->nh_rth_input
);
214 } endfor_nexthops(fi
);
216 release_net(fi
->fib_net
);
217 if (fi
->fib_metrics
!= (u32
*) dst_default_metrics
)
218 kfree(fi
->fib_metrics
);
222 void free_fib_info(struct fib_info
*fi
)
224 if (fi
->fib_dead
== 0) {
225 pr_warn("Freeing alive fib_info %p\n", fi
);
229 #ifdef CONFIG_IP_ROUTE_CLASSID
230 change_nexthops(fi
) {
231 if (nexthop_nh
->nh_tclassid
)
232 fi
->fib_net
->ipv4
.fib_num_tclassid_users
--;
233 } endfor_nexthops(fi
);
235 call_rcu(&fi
->rcu
, free_fib_info_rcu
);
238 void fib_release_info(struct fib_info
*fi
)
240 spin_lock_bh(&fib_info_lock
);
241 if (fi
&& --fi
->fib_treeref
== 0) {
242 hlist_del(&fi
->fib_hash
);
244 hlist_del(&fi
->fib_lhash
);
245 change_nexthops(fi
) {
246 if (!nexthop_nh
->nh_dev
)
248 hlist_del(&nexthop_nh
->nh_hash
);
249 } endfor_nexthops(fi
)
253 spin_unlock_bh(&fib_info_lock
);
256 static inline int nh_comp(const struct fib_info
*fi
, const struct fib_info
*ofi
)
258 const struct fib_nh
*onh
= ofi
->fib_nh
;
261 if (nh
->nh_oif
!= onh
->nh_oif
||
262 nh
->nh_gw
!= onh
->nh_gw
||
263 nh
->nh_scope
!= onh
->nh_scope
||
264 #ifdef CONFIG_IP_ROUTE_MULTIPATH
265 nh
->nh_weight
!= onh
->nh_weight
||
267 #ifdef CONFIG_IP_ROUTE_CLASSID
268 nh
->nh_tclassid
!= onh
->nh_tclassid
||
270 ((nh
->nh_flags
^ onh
->nh_flags
) & ~RTNH_F_DEAD
))
273 } endfor_nexthops(fi
);
277 static inline unsigned int fib_devindex_hashfn(unsigned int val
)
279 unsigned int mask
= DEVINDEX_HASHSIZE
- 1;
282 (val
>> DEVINDEX_HASHBITS
) ^
283 (val
>> (DEVINDEX_HASHBITS
* 2))) & mask
;
286 static inline unsigned int fib_info_hashfn(const struct fib_info
*fi
)
288 unsigned int mask
= (fib_info_hash_size
- 1);
289 unsigned int val
= fi
->fib_nhs
;
291 val
^= (fi
->fib_protocol
<< 8) | fi
->fib_scope
;
292 val
^= (__force u32
)fi
->fib_prefsrc
;
293 val
^= fi
->fib_priority
;
295 val
^= fib_devindex_hashfn(nh
->nh_oif
);
296 } endfor_nexthops(fi
)
298 return (val
^ (val
>> 7) ^ (val
>> 12)) & mask
;
301 static struct fib_info
*fib_find_info(const struct fib_info
*nfi
)
303 struct hlist_head
*head
;
307 hash
= fib_info_hashfn(nfi
);
308 head
= &fib_info_hash
[hash
];
310 hlist_for_each_entry(fi
, head
, fib_hash
) {
311 if (!net_eq(fi
->fib_net
, nfi
->fib_net
))
313 if (fi
->fib_nhs
!= nfi
->fib_nhs
)
315 if (nfi
->fib_protocol
== fi
->fib_protocol
&&
316 nfi
->fib_scope
== fi
->fib_scope
&&
317 nfi
->fib_prefsrc
== fi
->fib_prefsrc
&&
318 nfi
->fib_priority
== fi
->fib_priority
&&
319 nfi
->fib_type
== fi
->fib_type
&&
320 memcmp(nfi
->fib_metrics
, fi
->fib_metrics
,
321 sizeof(u32
) * RTAX_MAX
) == 0 &&
322 ((nfi
->fib_flags
^ fi
->fib_flags
) & ~RTNH_F_DEAD
) == 0 &&
323 (nfi
->fib_nhs
== 0 || nh_comp(fi
, nfi
) == 0))
330 /* Check, that the gateway is already configured.
331 * Used only by redirect accept routine.
333 int ip_fib_check_default(__be32 gw
, struct net_device
*dev
)
335 struct hlist_head
*head
;
339 spin_lock(&fib_info_lock
);
341 hash
= fib_devindex_hashfn(dev
->ifindex
);
342 head
= &fib_info_devhash
[hash
];
343 hlist_for_each_entry(nh
, head
, nh_hash
) {
344 if (nh
->nh_dev
== dev
&&
346 !(nh
->nh_flags
& RTNH_F_DEAD
)) {
347 spin_unlock(&fib_info_lock
);
352 spin_unlock(&fib_info_lock
);
357 static inline size_t fib_nlmsg_size(struct fib_info
*fi
)
359 size_t payload
= NLMSG_ALIGN(sizeof(struct rtmsg
))
360 + nla_total_size(4) /* RTA_TABLE */
361 + nla_total_size(4) /* RTA_DST */
362 + nla_total_size(4) /* RTA_PRIORITY */
363 + nla_total_size(4) /* RTA_PREFSRC */
364 + nla_total_size(TCP_CA_NAME_MAX
); /* RTAX_CC_ALGO */
366 /* space for nested metrics */
367 payload
+= nla_total_size((RTAX_MAX
* nla_total_size(4)));
370 /* Also handles the special case fib_nhs == 1 */
372 /* each nexthop is packed in an attribute */
373 size_t nhsize
= nla_total_size(sizeof(struct rtnexthop
));
375 /* may contain flow and gateway attribute */
376 nhsize
+= 2 * nla_total_size(4);
378 /* all nexthops are packed in a nested attribute */
379 payload
+= nla_total_size(fi
->fib_nhs
* nhsize
);
385 void rtmsg_fib(int event
, __be32 key
, struct fib_alias
*fa
,
386 int dst_len
, u32 tb_id
, const struct nl_info
*info
,
387 unsigned int nlm_flags
)
390 u32 seq
= info
->nlh
? info
->nlh
->nlmsg_seq
: 0;
393 skb
= nlmsg_new(fib_nlmsg_size(fa
->fa_info
), GFP_KERNEL
);
397 err
= fib_dump_info(skb
, info
->portid
, seq
, event
, tb_id
,
398 fa
->fa_type
, key
, dst_len
,
399 fa
->fa_tos
, fa
->fa_info
, nlm_flags
);
401 /* -EMSGSIZE implies BUG in fib_nlmsg_size() */
402 WARN_ON(err
== -EMSGSIZE
);
406 rtnl_notify(skb
, info
->nl_net
, info
->portid
, RTNLGRP_IPV4_ROUTE
,
407 info
->nlh
, GFP_KERNEL
);
411 rtnl_set_sk_err(info
->nl_net
, RTNLGRP_IPV4_ROUTE
, err
);
414 /* Return the first fib alias matching TOS with
415 * priority less than or equal to PRIO.
417 struct fib_alias
*fib_find_alias(struct list_head
*fah
, u8 tos
, u32 prio
)
420 struct fib_alias
*fa
;
421 list_for_each_entry(fa
, fah
, fa_list
) {
422 if (fa
->fa_tos
> tos
)
424 if (fa
->fa_info
->fib_priority
>= prio
||
432 static int fib_detect_death(struct fib_info
*fi
, int order
,
433 struct fib_info
**last_resort
, int *last_idx
,
437 int state
= NUD_NONE
;
439 n
= neigh_lookup(&arp_tbl
, &fi
->fib_nh
[0].nh_gw
, fi
->fib_dev
);
441 state
= n
->nud_state
;
444 if (state
== NUD_REACHABLE
)
446 if ((state
& NUD_VALID
) && order
!= dflt
)
448 if ((state
& NUD_VALID
) ||
449 (*last_idx
< 0 && order
> dflt
)) {
456 #ifdef CONFIG_IP_ROUTE_MULTIPATH
458 static int fib_count_nexthops(struct rtnexthop
*rtnh
, int remaining
)
462 while (rtnh_ok(rtnh
, remaining
)) {
464 rtnh
= rtnh_next(rtnh
, &remaining
);
467 /* leftover implies invalid nexthop configuration, discard it */
468 return remaining
> 0 ? 0 : nhs
;
471 static int fib_get_nhs(struct fib_info
*fi
, struct rtnexthop
*rtnh
,
472 int remaining
, struct fib_config
*cfg
)
474 change_nexthops(fi
) {
477 if (!rtnh_ok(rtnh
, remaining
))
480 nexthop_nh
->nh_flags
=
481 (cfg
->fc_flags
& ~0xFF) | rtnh
->rtnh_flags
;
482 nexthop_nh
->nh_oif
= rtnh
->rtnh_ifindex
;
483 nexthop_nh
->nh_weight
= rtnh
->rtnh_hops
+ 1;
485 attrlen
= rtnh_attrlen(rtnh
);
487 struct nlattr
*nla
, *attrs
= rtnh_attrs(rtnh
);
489 nla
= nla_find(attrs
, attrlen
, RTA_GATEWAY
);
490 nexthop_nh
->nh_gw
= nla
? nla_get_be32(nla
) : 0;
491 #ifdef CONFIG_IP_ROUTE_CLASSID
492 nla
= nla_find(attrs
, attrlen
, RTA_FLOW
);
493 nexthop_nh
->nh_tclassid
= nla
? nla_get_u32(nla
) : 0;
494 if (nexthop_nh
->nh_tclassid
)
495 fi
->fib_net
->ipv4
.fib_num_tclassid_users
++;
499 rtnh
= rtnh_next(rtnh
, &remaining
);
500 } endfor_nexthops(fi
);
507 int fib_nh_match(struct fib_config
*cfg
, struct fib_info
*fi
)
509 #ifdef CONFIG_IP_ROUTE_MULTIPATH
510 struct rtnexthop
*rtnh
;
514 if (cfg
->fc_priority
&& cfg
->fc_priority
!= fi
->fib_priority
)
517 if (cfg
->fc_oif
|| cfg
->fc_gw
) {
518 if ((!cfg
->fc_oif
|| cfg
->fc_oif
== fi
->fib_nh
->nh_oif
) &&
519 (!cfg
->fc_gw
|| cfg
->fc_gw
== fi
->fib_nh
->nh_gw
))
524 #ifdef CONFIG_IP_ROUTE_MULTIPATH
525 if (cfg
->fc_mp
== NULL
)
529 remaining
= cfg
->fc_mp_len
;
534 if (!rtnh_ok(rtnh
, remaining
))
537 if (rtnh
->rtnh_ifindex
&& rtnh
->rtnh_ifindex
!= nh
->nh_oif
)
540 attrlen
= rtnh_attrlen(rtnh
);
542 struct nlattr
*nla
, *attrs
= rtnh_attrs(rtnh
);
544 nla
= nla_find(attrs
, attrlen
, RTA_GATEWAY
);
545 if (nla
&& nla_get_be32(nla
) != nh
->nh_gw
)
547 #ifdef CONFIG_IP_ROUTE_CLASSID
548 nla
= nla_find(attrs
, attrlen
, RTA_FLOW
);
549 if (nla
&& nla_get_u32(nla
) != nh
->nh_tclassid
)
554 rtnh
= rtnh_next(rtnh
, &remaining
);
555 } endfor_nexthops(fi
);
565 * Semantics of nexthop is very messy by historical reasons.
566 * We have to take into account, that:
567 * a) gateway can be actually local interface address,
568 * so that gatewayed route is direct.
569 * b) gateway must be on-link address, possibly
570 * described not by an ifaddr, but also by a direct route.
571 * c) If both gateway and interface are specified, they should not
573 * d) If we use tunnel routes, gateway could be not on-link.
575 * Attempt to reconcile all of these (alas, self-contradictory) conditions
576 * results in pretty ugly and hairy code with obscure logic.
578 * I chose to generalized it instead, so that the size
579 * of code does not increase practically, but it becomes
581 * Every prefix is assigned a "scope" value: "host" is local address,
582 * "link" is direct route,
583 * [ ... "site" ... "interior" ... ]
584 * and "universe" is true gateway route with global meaning.
586 * Every prefix refers to a set of "nexthop"s (gw, oif),
587 * where gw must have narrower scope. This recursion stops
588 * when gw has LOCAL scope or if "nexthop" is declared ONLINK,
589 * which means that gw is forced to be on link.
591 * Code is still hairy, but now it is apparently logically
592 * consistent and very flexible. F.e. as by-product it allows
593 * to co-exists in peace independent exterior and interior
596 * Normally it looks as following.
598 * {universe prefix} -> (gw, oif) [scope link]
600 * |-> {link prefix} -> (gw, oif) [scope local]
602 * |-> {local prefix} (terminal node)
604 static int fib_check_nh(struct fib_config
*cfg
, struct fib_info
*fi
,
609 struct net_device
*dev
;
611 net
= cfg
->fc_nlinfo
.nl_net
;
613 struct fib_result res
;
615 if (nh
->nh_flags
& RTNH_F_ONLINK
) {
617 if (cfg
->fc_scope
>= RT_SCOPE_LINK
)
619 if (inet_addr_type(net
, nh
->nh_gw
) != RTN_UNICAST
)
621 dev
= __dev_get_by_index(net
, nh
->nh_oif
);
624 if (!(dev
->flags
& IFF_UP
))
628 nh
->nh_scope
= RT_SCOPE_LINK
;
633 struct flowi4 fl4
= {
635 .flowi4_scope
= cfg
->fc_scope
+ 1,
636 .flowi4_oif
= nh
->nh_oif
,
637 .flowi4_iif
= LOOPBACK_IFINDEX
,
640 /* It is not necessary, but requires a bit of thinking */
641 if (fl4
.flowi4_scope
< RT_SCOPE_LINK
)
642 fl4
.flowi4_scope
= RT_SCOPE_LINK
;
643 err
= fib_lookup(net
, &fl4
, &res
);
650 if (res
.type
!= RTN_UNICAST
&& res
.type
!= RTN_LOCAL
)
652 nh
->nh_scope
= res
.scope
;
653 nh
->nh_oif
= FIB_RES_OIF(res
);
654 nh
->nh_dev
= dev
= FIB_RES_DEV(res
);
658 err
= (dev
->flags
& IFF_UP
) ? 0 : -ENETDOWN
;
660 struct in_device
*in_dev
;
662 if (nh
->nh_flags
& (RTNH_F_PERVASIVE
| RTNH_F_ONLINK
))
667 in_dev
= inetdev_by_index(net
, nh
->nh_oif
);
671 if (!(in_dev
->dev
->flags
& IFF_UP
))
673 nh
->nh_dev
= in_dev
->dev
;
674 dev_hold(nh
->nh_dev
);
675 nh
->nh_scope
= RT_SCOPE_HOST
;
683 static inline unsigned int fib_laddr_hashfn(__be32 val
)
685 unsigned int mask
= (fib_info_hash_size
- 1);
687 return ((__force u32
)val
^
688 ((__force u32
)val
>> 7) ^
689 ((__force u32
)val
>> 14)) & mask
;
692 static struct hlist_head
*fib_info_hash_alloc(int bytes
)
694 if (bytes
<= PAGE_SIZE
)
695 return kzalloc(bytes
, GFP_KERNEL
);
697 return (struct hlist_head
*)
698 __get_free_pages(GFP_KERNEL
| __GFP_ZERO
,
702 static void fib_info_hash_free(struct hlist_head
*hash
, int bytes
)
707 if (bytes
<= PAGE_SIZE
)
710 free_pages((unsigned long) hash
, get_order(bytes
));
713 static void fib_info_hash_move(struct hlist_head
*new_info_hash
,
714 struct hlist_head
*new_laddrhash
,
715 unsigned int new_size
)
717 struct hlist_head
*old_info_hash
, *old_laddrhash
;
718 unsigned int old_size
= fib_info_hash_size
;
719 unsigned int i
, bytes
;
721 spin_lock_bh(&fib_info_lock
);
722 old_info_hash
= fib_info_hash
;
723 old_laddrhash
= fib_info_laddrhash
;
724 fib_info_hash_size
= new_size
;
726 for (i
= 0; i
< old_size
; i
++) {
727 struct hlist_head
*head
= &fib_info_hash
[i
];
728 struct hlist_node
*n
;
731 hlist_for_each_entry_safe(fi
, n
, head
, fib_hash
) {
732 struct hlist_head
*dest
;
733 unsigned int new_hash
;
735 hlist_del(&fi
->fib_hash
);
737 new_hash
= fib_info_hashfn(fi
);
738 dest
= &new_info_hash
[new_hash
];
739 hlist_add_head(&fi
->fib_hash
, dest
);
742 fib_info_hash
= new_info_hash
;
744 for (i
= 0; i
< old_size
; i
++) {
745 struct hlist_head
*lhead
= &fib_info_laddrhash
[i
];
746 struct hlist_node
*n
;
749 hlist_for_each_entry_safe(fi
, n
, lhead
, fib_lhash
) {
750 struct hlist_head
*ldest
;
751 unsigned int new_hash
;
753 hlist_del(&fi
->fib_lhash
);
755 new_hash
= fib_laddr_hashfn(fi
->fib_prefsrc
);
756 ldest
= &new_laddrhash
[new_hash
];
757 hlist_add_head(&fi
->fib_lhash
, ldest
);
760 fib_info_laddrhash
= new_laddrhash
;
762 spin_unlock_bh(&fib_info_lock
);
764 bytes
= old_size
* sizeof(struct hlist_head
*);
765 fib_info_hash_free(old_info_hash
, bytes
);
766 fib_info_hash_free(old_laddrhash
, bytes
);
769 __be32
fib_info_update_nh_saddr(struct net
*net
, struct fib_nh
*nh
)
771 nh
->nh_saddr
= inet_select_addr(nh
->nh_dev
,
773 nh
->nh_parent
->fib_scope
);
774 nh
->nh_saddr_genid
= atomic_read(&net
->ipv4
.dev_addr_genid
);
779 struct fib_info
*fib_create_info(struct fib_config
*cfg
)
782 struct fib_info
*fi
= NULL
;
783 struct fib_info
*ofi
;
785 struct net
*net
= cfg
->fc_nlinfo
.nl_net
;
787 if (cfg
->fc_type
> RTN_MAX
)
790 /* Fast check to catch the most weird cases */
791 if (fib_props
[cfg
->fc_type
].scope
> cfg
->fc_scope
)
794 #ifdef CONFIG_IP_ROUTE_MULTIPATH
796 nhs
= fib_count_nexthops(cfg
->fc_mp
, cfg
->fc_mp_len
);
803 if (fib_info_cnt
>= fib_info_hash_size
) {
804 unsigned int new_size
= fib_info_hash_size
<< 1;
805 struct hlist_head
*new_info_hash
;
806 struct hlist_head
*new_laddrhash
;
811 bytes
= new_size
* sizeof(struct hlist_head
*);
812 new_info_hash
= fib_info_hash_alloc(bytes
);
813 new_laddrhash
= fib_info_hash_alloc(bytes
);
814 if (!new_info_hash
|| !new_laddrhash
) {
815 fib_info_hash_free(new_info_hash
, bytes
);
816 fib_info_hash_free(new_laddrhash
, bytes
);
818 fib_info_hash_move(new_info_hash
, new_laddrhash
, new_size
);
820 if (!fib_info_hash_size
)
824 fi
= kzalloc(sizeof(*fi
)+nhs
*sizeof(struct fib_nh
), GFP_KERNEL
);
829 fi
->fib_metrics
= kzalloc(sizeof(u32
) * RTAX_MAX
, GFP_KERNEL
);
830 if (!fi
->fib_metrics
)
833 fi
->fib_metrics
= (u32
*) dst_default_metrics
;
835 fi
->fib_net
= hold_net(net
);
836 fi
->fib_protocol
= cfg
->fc_protocol
;
837 fi
->fib_scope
= cfg
->fc_scope
;
838 fi
->fib_flags
= cfg
->fc_flags
;
839 fi
->fib_priority
= cfg
->fc_priority
;
840 fi
->fib_prefsrc
= cfg
->fc_prefsrc
;
841 fi
->fib_type
= cfg
->fc_type
;
844 change_nexthops(fi
) {
845 nexthop_nh
->nh_parent
= fi
;
846 nexthop_nh
->nh_pcpu_rth_output
= alloc_percpu(struct rtable __rcu
*);
847 if (!nexthop_nh
->nh_pcpu_rth_output
)
849 } endfor_nexthops(fi
)
855 nla_for_each_attr(nla
, cfg
->fc_mx
, cfg
->fc_mx_len
, remaining
) {
856 int type
= nla_type(nla
);
863 if (type
== RTAX_CC_ALGO
) {
864 char tmp
[TCP_CA_NAME_MAX
];
866 nla_strlcpy(tmp
, nla
, sizeof(tmp
));
867 val
= tcp_ca_get_key_by_name(tmp
);
868 if (val
== TCP_CA_UNSPEC
)
871 val
= nla_get_u32(nla
);
873 if (type
== RTAX_ADVMSS
&& val
> 65535 - 40)
875 if (type
== RTAX_MTU
&& val
> 65535 - 15)
877 fi
->fib_metrics
[type
- 1] = val
;
883 #ifdef CONFIG_IP_ROUTE_MULTIPATH
884 err
= fib_get_nhs(fi
, cfg
->fc_mp
, cfg
->fc_mp_len
, cfg
);
887 if (cfg
->fc_oif
&& fi
->fib_nh
->nh_oif
!= cfg
->fc_oif
)
889 if (cfg
->fc_gw
&& fi
->fib_nh
->nh_gw
!= cfg
->fc_gw
)
891 #ifdef CONFIG_IP_ROUTE_CLASSID
892 if (cfg
->fc_flow
&& fi
->fib_nh
->nh_tclassid
!= cfg
->fc_flow
)
899 struct fib_nh
*nh
= fi
->fib_nh
;
901 nh
->nh_oif
= cfg
->fc_oif
;
902 nh
->nh_gw
= cfg
->fc_gw
;
903 nh
->nh_flags
= cfg
->fc_flags
;
904 #ifdef CONFIG_IP_ROUTE_CLASSID
905 nh
->nh_tclassid
= cfg
->fc_flow
;
907 fi
->fib_net
->ipv4
.fib_num_tclassid_users
++;
909 #ifdef CONFIG_IP_ROUTE_MULTIPATH
914 if (fib_props
[cfg
->fc_type
].error
) {
915 if (cfg
->fc_gw
|| cfg
->fc_oif
|| cfg
->fc_mp
)
919 switch (cfg
->fc_type
) {
931 if (cfg
->fc_scope
> RT_SCOPE_HOST
)
934 if (cfg
->fc_scope
== RT_SCOPE_HOST
) {
935 struct fib_nh
*nh
= fi
->fib_nh
;
937 /* Local address is added. */
938 if (nhs
!= 1 || nh
->nh_gw
)
940 nh
->nh_scope
= RT_SCOPE_NOWHERE
;
941 nh
->nh_dev
= dev_get_by_index(net
, fi
->fib_nh
->nh_oif
);
943 if (nh
->nh_dev
== NULL
)
946 change_nexthops(fi
) {
947 err
= fib_check_nh(cfg
, fi
, nexthop_nh
);
950 } endfor_nexthops(fi
)
953 if (fi
->fib_prefsrc
) {
954 if (cfg
->fc_type
!= RTN_LOCAL
|| !cfg
->fc_dst
||
955 fi
->fib_prefsrc
!= cfg
->fc_dst
)
956 if (inet_addr_type(net
, fi
->fib_prefsrc
) != RTN_LOCAL
)
960 change_nexthops(fi
) {
961 fib_info_update_nh_saddr(net
, nexthop_nh
);
962 } endfor_nexthops(fi
)
965 ofi
= fib_find_info(fi
);
974 atomic_inc(&fi
->fib_clntref
);
975 spin_lock_bh(&fib_info_lock
);
976 hlist_add_head(&fi
->fib_hash
,
977 &fib_info_hash
[fib_info_hashfn(fi
)]);
978 if (fi
->fib_prefsrc
) {
979 struct hlist_head
*head
;
981 head
= &fib_info_laddrhash
[fib_laddr_hashfn(fi
->fib_prefsrc
)];
982 hlist_add_head(&fi
->fib_lhash
, head
);
984 change_nexthops(fi
) {
985 struct hlist_head
*head
;
988 if (!nexthop_nh
->nh_dev
)
990 hash
= fib_devindex_hashfn(nexthop_nh
->nh_dev
->ifindex
);
991 head
= &fib_info_devhash
[hash
];
992 hlist_add_head(&nexthop_nh
->nh_hash
, head
);
993 } endfor_nexthops(fi
)
994 spin_unlock_bh(&fib_info_lock
);
1006 return ERR_PTR(err
);
1009 int fib_dump_info(struct sk_buff
*skb
, u32 portid
, u32 seq
, int event
,
1010 u32 tb_id
, u8 type
, __be32 dst
, int dst_len
, u8 tos
,
1011 struct fib_info
*fi
, unsigned int flags
)
1013 struct nlmsghdr
*nlh
;
1016 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*rtm
), flags
);
1020 rtm
= nlmsg_data(nlh
);
1021 rtm
->rtm_family
= AF_INET
;
1022 rtm
->rtm_dst_len
= dst_len
;
1023 rtm
->rtm_src_len
= 0;
1026 rtm
->rtm_table
= tb_id
;
1028 rtm
->rtm_table
= RT_TABLE_COMPAT
;
1029 if (nla_put_u32(skb
, RTA_TABLE
, tb_id
))
1030 goto nla_put_failure
;
1031 rtm
->rtm_type
= type
;
1032 rtm
->rtm_flags
= fi
->fib_flags
;
1033 rtm
->rtm_scope
= fi
->fib_scope
;
1034 rtm
->rtm_protocol
= fi
->fib_protocol
;
1036 if (rtm
->rtm_dst_len
&&
1037 nla_put_be32(skb
, RTA_DST
, dst
))
1038 goto nla_put_failure
;
1039 if (fi
->fib_priority
&&
1040 nla_put_u32(skb
, RTA_PRIORITY
, fi
->fib_priority
))
1041 goto nla_put_failure
;
1042 if (rtnetlink_put_metrics(skb
, fi
->fib_metrics
) < 0)
1043 goto nla_put_failure
;
1045 if (fi
->fib_prefsrc
&&
1046 nla_put_be32(skb
, RTA_PREFSRC
, fi
->fib_prefsrc
))
1047 goto nla_put_failure
;
1048 if (fi
->fib_nhs
== 1) {
1049 if (fi
->fib_nh
->nh_gw
&&
1050 nla_put_be32(skb
, RTA_GATEWAY
, fi
->fib_nh
->nh_gw
))
1051 goto nla_put_failure
;
1052 if (fi
->fib_nh
->nh_oif
&&
1053 nla_put_u32(skb
, RTA_OIF
, fi
->fib_nh
->nh_oif
))
1054 goto nla_put_failure
;
1055 #ifdef CONFIG_IP_ROUTE_CLASSID
1056 if (fi
->fib_nh
[0].nh_tclassid
&&
1057 nla_put_u32(skb
, RTA_FLOW
, fi
->fib_nh
[0].nh_tclassid
))
1058 goto nla_put_failure
;
1061 #ifdef CONFIG_IP_ROUTE_MULTIPATH
1062 if (fi
->fib_nhs
> 1) {
1063 struct rtnexthop
*rtnh
;
1066 mp
= nla_nest_start(skb
, RTA_MULTIPATH
);
1068 goto nla_put_failure
;
1071 rtnh
= nla_reserve_nohdr(skb
, sizeof(*rtnh
));
1073 goto nla_put_failure
;
1075 rtnh
->rtnh_flags
= nh
->nh_flags
& 0xFF;
1076 rtnh
->rtnh_hops
= nh
->nh_weight
- 1;
1077 rtnh
->rtnh_ifindex
= nh
->nh_oif
;
1080 nla_put_be32(skb
, RTA_GATEWAY
, nh
->nh_gw
))
1081 goto nla_put_failure
;
1082 #ifdef CONFIG_IP_ROUTE_CLASSID
1083 if (nh
->nh_tclassid
&&
1084 nla_put_u32(skb
, RTA_FLOW
, nh
->nh_tclassid
))
1085 goto nla_put_failure
;
1087 /* length of rtnetlink header + attributes */
1088 rtnh
->rtnh_len
= nlmsg_get_pos(skb
) - (void *) rtnh
;
1089 } endfor_nexthops(fi
);
1091 nla_nest_end(skb
, mp
);
1094 return nlmsg_end(skb
, nlh
);
1097 nlmsg_cancel(skb
, nlh
);
1103 * - local address disappeared -> we must delete all the entries
1105 * - device went down -> we must shutdown all nexthops going via it.
1107 int fib_sync_down_addr(struct net
*net
, __be32 local
)
1110 unsigned int hash
= fib_laddr_hashfn(local
);
1111 struct hlist_head
*head
= &fib_info_laddrhash
[hash
];
1112 struct fib_info
*fi
;
1114 if (fib_info_laddrhash
== NULL
|| local
== 0)
1117 hlist_for_each_entry(fi
, head
, fib_lhash
) {
1118 if (!net_eq(fi
->fib_net
, net
))
1120 if (fi
->fib_prefsrc
== local
) {
1121 fi
->fib_flags
|= RTNH_F_DEAD
;
1128 int fib_sync_down_dev(struct net_device
*dev
, int force
)
1131 int scope
= RT_SCOPE_NOWHERE
;
1132 struct fib_info
*prev_fi
= NULL
;
1133 unsigned int hash
= fib_devindex_hashfn(dev
->ifindex
);
1134 struct hlist_head
*head
= &fib_info_devhash
[hash
];
1140 hlist_for_each_entry(nh
, head
, nh_hash
) {
1141 struct fib_info
*fi
= nh
->nh_parent
;
1144 BUG_ON(!fi
->fib_nhs
);
1145 if (nh
->nh_dev
!= dev
|| fi
== prev_fi
)
1149 change_nexthops(fi
) {
1150 if (nexthop_nh
->nh_flags
& RTNH_F_DEAD
)
1152 else if (nexthop_nh
->nh_dev
== dev
&&
1153 nexthop_nh
->nh_scope
!= scope
) {
1154 nexthop_nh
->nh_flags
|= RTNH_F_DEAD
;
1155 #ifdef CONFIG_IP_ROUTE_MULTIPATH
1156 spin_lock_bh(&fib_multipath_lock
);
1157 fi
->fib_power
-= nexthop_nh
->nh_power
;
1158 nexthop_nh
->nh_power
= 0;
1159 spin_unlock_bh(&fib_multipath_lock
);
1163 #ifdef CONFIG_IP_ROUTE_MULTIPATH
1164 if (force
> 1 && nexthop_nh
->nh_dev
== dev
) {
1169 } endfor_nexthops(fi
)
1170 if (dead
== fi
->fib_nhs
) {
1171 fi
->fib_flags
|= RTNH_F_DEAD
;
1179 /* Must be invoked inside of an RCU protected region. */
1180 void fib_select_default(struct fib_result
*res
)
1182 struct fib_info
*fi
= NULL
, *last_resort
= NULL
;
1183 struct list_head
*fa_head
= res
->fa_head
;
1184 struct fib_table
*tb
= res
->table
;
1185 int order
= -1, last_idx
= -1;
1186 struct fib_alias
*fa
;
1188 list_for_each_entry_rcu(fa
, fa_head
, fa_list
) {
1189 struct fib_info
*next_fi
= fa
->fa_info
;
1191 if (next_fi
->fib_scope
!= res
->scope
||
1192 fa
->fa_type
!= RTN_UNICAST
)
1195 if (next_fi
->fib_priority
> res
->fi
->fib_priority
)
1197 if (!next_fi
->fib_nh
[0].nh_gw
||
1198 next_fi
->fib_nh
[0].nh_scope
!= RT_SCOPE_LINK
)
1201 fib_alias_accessed(fa
);
1204 if (next_fi
!= res
->fi
)
1206 } else if (!fib_detect_death(fi
, order
, &last_resort
,
1207 &last_idx
, tb
->tb_default
)) {
1208 fib_result_assign(res
, fi
);
1209 tb
->tb_default
= order
;
1216 if (order
<= 0 || fi
== NULL
) {
1217 tb
->tb_default
= -1;
1221 if (!fib_detect_death(fi
, order
, &last_resort
, &last_idx
,
1223 fib_result_assign(res
, fi
);
1224 tb
->tb_default
= order
;
1229 fib_result_assign(res
, last_resort
);
1230 tb
->tb_default
= last_idx
;
1235 #ifdef CONFIG_IP_ROUTE_MULTIPATH
1238 * Dead device goes up. We wake up dead nexthops.
1239 * It takes sense only on multipath routes.
1241 int fib_sync_up(struct net_device
*dev
)
1243 struct fib_info
*prev_fi
;
1245 struct hlist_head
*head
;
1249 if (!(dev
->flags
& IFF_UP
))
1253 hash
= fib_devindex_hashfn(dev
->ifindex
);
1254 head
= &fib_info_devhash
[hash
];
1257 hlist_for_each_entry(nh
, head
, nh_hash
) {
1258 struct fib_info
*fi
= nh
->nh_parent
;
1261 BUG_ON(!fi
->fib_nhs
);
1262 if (nh
->nh_dev
!= dev
|| fi
== prev_fi
)
1267 change_nexthops(fi
) {
1268 if (!(nexthop_nh
->nh_flags
& RTNH_F_DEAD
)) {
1272 if (nexthop_nh
->nh_dev
== NULL
||
1273 !(nexthop_nh
->nh_dev
->flags
& IFF_UP
))
1275 if (nexthop_nh
->nh_dev
!= dev
||
1276 !__in_dev_get_rtnl(dev
))
1279 spin_lock_bh(&fib_multipath_lock
);
1280 nexthop_nh
->nh_power
= 0;
1281 nexthop_nh
->nh_flags
&= ~RTNH_F_DEAD
;
1282 spin_unlock_bh(&fib_multipath_lock
);
1283 } endfor_nexthops(fi
)
1286 fi
->fib_flags
&= ~RTNH_F_DEAD
;
1295 * The algorithm is suboptimal, but it provides really
1296 * fair weighted route distribution.
1298 void fib_select_multipath(struct fib_result
*res
)
1300 struct fib_info
*fi
= res
->fi
;
1303 spin_lock_bh(&fib_multipath_lock
);
1304 if (fi
->fib_power
<= 0) {
1306 change_nexthops(fi
) {
1307 if (!(nexthop_nh
->nh_flags
& RTNH_F_DEAD
)) {
1308 power
+= nexthop_nh
->nh_weight
;
1309 nexthop_nh
->nh_power
= nexthop_nh
->nh_weight
;
1311 } endfor_nexthops(fi
);
1312 fi
->fib_power
= power
;
1314 spin_unlock_bh(&fib_multipath_lock
);
1315 /* Race condition: route has just become dead. */
1322 /* w should be random number [0..fi->fib_power-1],
1323 * it is pretty bad approximation.
1326 w
= jiffies
% fi
->fib_power
;
1328 change_nexthops(fi
) {
1329 if (!(nexthop_nh
->nh_flags
& RTNH_F_DEAD
) &&
1330 nexthop_nh
->nh_power
) {
1331 w
-= nexthop_nh
->nh_power
;
1333 nexthop_nh
->nh_power
--;
1335 res
->nh_sel
= nhsel
;
1336 spin_unlock_bh(&fib_multipath_lock
);
1340 } endfor_nexthops(fi
);
1342 /* Race condition: route has just become dead. */
1344 spin_unlock_bh(&fib_multipath_lock
);