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 <asm/system.h>
18 #include <linux/bitops.h>
19 #include <linux/types.h>
20 #include <linux/kernel.h>
21 #include <linux/jiffies.h>
23 #include <linux/string.h>
24 #include <linux/socket.h>
25 #include <linux/sockios.h>
26 #include <linux/errno.h>
28 #include <linux/inet.h>
29 #include <linux/inetdevice.h>
30 #include <linux/netdevice.h>
31 #include <linux/if_arp.h>
32 #include <linux/proc_fs.h>
33 #include <linux/skbuff.h>
34 #include <linux/init.h>
35 #include <linux/slab.h>
39 #include <net/protocol.h>
40 #include <net/route.h>
43 #include <net/ip_fib.h>
44 #include <net/netlink.h>
45 #include <net/nexthop.h>
47 #include "fib_lookup.h"
49 static DEFINE_SPINLOCK(fib_info_lock
);
50 static struct hlist_head
*fib_info_hash
;
51 static struct hlist_head
*fib_info_laddrhash
;
52 static unsigned int fib_hash_size
;
53 static unsigned int fib_info_cnt
;
55 #define DEVINDEX_HASHBITS 8
56 #define DEVINDEX_HASHSIZE (1U << DEVINDEX_HASHBITS)
57 static struct hlist_head fib_info_devhash
[DEVINDEX_HASHSIZE
];
59 #ifdef CONFIG_IP_ROUTE_MULTIPATH
61 static DEFINE_SPINLOCK(fib_multipath_lock
);
63 #define for_nexthops(fi) { \
64 int nhsel; const struct fib_nh *nh; \
65 for (nhsel = 0, nh = (fi)->fib_nh; \
66 nhsel < (fi)->fib_nhs; \
69 #define change_nexthops(fi) { \
70 int nhsel; struct fib_nh *nexthop_nh; \
71 for (nhsel = 0, nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \
72 nhsel < (fi)->fib_nhs; \
73 nexthop_nh++, nhsel++)
75 #else /* CONFIG_IP_ROUTE_MULTIPATH */
77 /* Hope, that gcc will optimize it to get rid of dummy loop */
79 #define for_nexthops(fi) { \
80 int nhsel; const struct fib_nh *nh = (fi)->fib_nh; \
81 for (nhsel = 0; nhsel < 1; nhsel++)
83 #define change_nexthops(fi) { \
85 struct fib_nh *nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \
86 for (nhsel = 0; nhsel < 1; nhsel++)
88 #endif /* CONFIG_IP_ROUTE_MULTIPATH */
90 #define endfor_nexthops(fi) }
97 } fib_props
[RTN_MAX
+ 1] = {
100 .scope
= RT_SCOPE_NOWHERE
,
104 .scope
= RT_SCOPE_UNIVERSE
,
108 .scope
= RT_SCOPE_HOST
,
112 .scope
= RT_SCOPE_LINK
,
116 .scope
= RT_SCOPE_LINK
,
120 .scope
= RT_SCOPE_UNIVERSE
,
124 .scope
= RT_SCOPE_UNIVERSE
,
126 [RTN_UNREACHABLE
] = {
127 .error
= -EHOSTUNREACH
,
128 .scope
= RT_SCOPE_UNIVERSE
,
132 .scope
= RT_SCOPE_UNIVERSE
,
136 .scope
= RT_SCOPE_UNIVERSE
,
140 .scope
= RT_SCOPE_NOWHERE
,
144 .scope
= RT_SCOPE_NOWHERE
,
149 /* Release a nexthop info record */
151 static void free_fib_info_rcu(struct rcu_head
*head
)
153 struct fib_info
*fi
= container_of(head
, struct fib_info
, rcu
);
158 void free_fib_info(struct fib_info
*fi
)
160 if (fi
->fib_dead
== 0) {
161 pr_warning("Freeing alive fib_info %p\n", fi
);
164 change_nexthops(fi
) {
165 if (nexthop_nh
->nh_dev
)
166 dev_put(nexthop_nh
->nh_dev
);
167 nexthop_nh
->nh_dev
= NULL
;
168 } endfor_nexthops(fi
);
170 release_net(fi
->fib_net
);
171 call_rcu(&fi
->rcu
, free_fib_info_rcu
);
174 void fib_release_info(struct fib_info
*fi
)
176 spin_lock_bh(&fib_info_lock
);
177 if (fi
&& --fi
->fib_treeref
== 0) {
178 hlist_del(&fi
->fib_hash
);
180 hlist_del(&fi
->fib_lhash
);
181 change_nexthops(fi
) {
182 if (!nexthop_nh
->nh_dev
)
184 hlist_del(&nexthop_nh
->nh_hash
);
185 } endfor_nexthops(fi
)
189 spin_unlock_bh(&fib_info_lock
);
192 static inline int nh_comp(const struct fib_info
*fi
, const struct fib_info
*ofi
)
194 const struct fib_nh
*onh
= ofi
->fib_nh
;
197 if (nh
->nh_oif
!= onh
->nh_oif
||
198 nh
->nh_gw
!= onh
->nh_gw
||
199 nh
->nh_scope
!= onh
->nh_scope
||
200 #ifdef CONFIG_IP_ROUTE_MULTIPATH
201 nh
->nh_weight
!= onh
->nh_weight
||
203 #ifdef CONFIG_NET_CLS_ROUTE
204 nh
->nh_tclassid
!= onh
->nh_tclassid
||
206 ((nh
->nh_flags
^ onh
->nh_flags
) & ~RTNH_F_DEAD
))
209 } endfor_nexthops(fi
);
213 static inline unsigned int fib_devindex_hashfn(unsigned int val
)
215 unsigned int mask
= DEVINDEX_HASHSIZE
- 1;
218 (val
>> DEVINDEX_HASHBITS
) ^
219 (val
>> (DEVINDEX_HASHBITS
* 2))) & mask
;
222 static inline unsigned int fib_info_hashfn(const struct fib_info
*fi
)
224 unsigned int mask
= (fib_hash_size
- 1);
225 unsigned int val
= fi
->fib_nhs
;
227 val
^= fi
->fib_protocol
;
228 val
^= (__force u32
)fi
->fib_prefsrc
;
229 val
^= fi
->fib_priority
;
231 val
^= fib_devindex_hashfn(nh
->nh_oif
);
232 } endfor_nexthops(fi
)
234 return (val
^ (val
>> 7) ^ (val
>> 12)) & mask
;
237 static struct fib_info
*fib_find_info(const struct fib_info
*nfi
)
239 struct hlist_head
*head
;
240 struct hlist_node
*node
;
244 hash
= fib_info_hashfn(nfi
);
245 head
= &fib_info_hash
[hash
];
247 hlist_for_each_entry(fi
, node
, head
, fib_hash
) {
248 if (!net_eq(fi
->fib_net
, nfi
->fib_net
))
250 if (fi
->fib_nhs
!= nfi
->fib_nhs
)
252 if (nfi
->fib_protocol
== fi
->fib_protocol
&&
253 nfi
->fib_prefsrc
== fi
->fib_prefsrc
&&
254 nfi
->fib_priority
== fi
->fib_priority
&&
255 memcmp(nfi
->fib_metrics
, fi
->fib_metrics
,
256 sizeof(fi
->fib_metrics
)) == 0 &&
257 ((nfi
->fib_flags
^ fi
->fib_flags
) & ~RTNH_F_DEAD
) == 0 &&
258 (nfi
->fib_nhs
== 0 || nh_comp(fi
, nfi
) == 0))
265 /* Check, that the gateway is already configured.
266 * Used only by redirect accept routine.
268 int ip_fib_check_default(__be32 gw
, struct net_device
*dev
)
270 struct hlist_head
*head
;
271 struct hlist_node
*node
;
275 spin_lock(&fib_info_lock
);
277 hash
= fib_devindex_hashfn(dev
->ifindex
);
278 head
= &fib_info_devhash
[hash
];
279 hlist_for_each_entry(nh
, node
, head
, nh_hash
) {
280 if (nh
->nh_dev
== dev
&&
282 !(nh
->nh_flags
& RTNH_F_DEAD
)) {
283 spin_unlock(&fib_info_lock
);
288 spin_unlock(&fib_info_lock
);
293 static inline size_t fib_nlmsg_size(struct fib_info
*fi
)
295 size_t payload
= NLMSG_ALIGN(sizeof(struct rtmsg
))
296 + nla_total_size(4) /* RTA_TABLE */
297 + nla_total_size(4) /* RTA_DST */
298 + nla_total_size(4) /* RTA_PRIORITY */
299 + nla_total_size(4); /* RTA_PREFSRC */
301 /* space for nested metrics */
302 payload
+= nla_total_size((RTAX_MAX
* nla_total_size(4)));
305 /* Also handles the special case fib_nhs == 1 */
307 /* each nexthop is packed in an attribute */
308 size_t nhsize
= nla_total_size(sizeof(struct rtnexthop
));
310 /* may contain flow and gateway attribute */
311 nhsize
+= 2 * nla_total_size(4);
313 /* all nexthops are packed in a nested attribute */
314 payload
+= nla_total_size(fi
->fib_nhs
* nhsize
);
320 void rtmsg_fib(int event
, __be32 key
, struct fib_alias
*fa
,
321 int dst_len
, u32 tb_id
, struct nl_info
*info
,
322 unsigned int nlm_flags
)
325 u32 seq
= info
->nlh
? info
->nlh
->nlmsg_seq
: 0;
328 skb
= nlmsg_new(fib_nlmsg_size(fa
->fa_info
), GFP_KERNEL
);
332 err
= fib_dump_info(skb
, info
->pid
, seq
, event
, tb_id
,
333 fa
->fa_type
, fa
->fa_scope
, key
, dst_len
,
334 fa
->fa_tos
, fa
->fa_info
, nlm_flags
);
336 /* -EMSGSIZE implies BUG in fib_nlmsg_size() */
337 WARN_ON(err
== -EMSGSIZE
);
341 rtnl_notify(skb
, info
->nl_net
, info
->pid
, RTNLGRP_IPV4_ROUTE
,
342 info
->nlh
, GFP_KERNEL
);
346 rtnl_set_sk_err(info
->nl_net
, RTNLGRP_IPV4_ROUTE
, err
);
349 /* Return the first fib alias matching TOS with
350 * priority less than or equal to PRIO.
352 struct fib_alias
*fib_find_alias(struct list_head
*fah
, u8 tos
, u32 prio
)
355 struct fib_alias
*fa
;
356 list_for_each_entry(fa
, fah
, fa_list
) {
357 if (fa
->fa_tos
> tos
)
359 if (fa
->fa_info
->fib_priority
>= prio
||
367 int fib_detect_death(struct fib_info
*fi
, int order
,
368 struct fib_info
**last_resort
, int *last_idx
, int dflt
)
371 int state
= NUD_NONE
;
373 n
= neigh_lookup(&arp_tbl
, &fi
->fib_nh
[0].nh_gw
, fi
->fib_dev
);
375 state
= n
->nud_state
;
378 if (state
== NUD_REACHABLE
)
380 if ((state
& NUD_VALID
) && order
!= dflt
)
382 if ((state
& NUD_VALID
) ||
383 (*last_idx
< 0 && order
> dflt
)) {
390 #ifdef CONFIG_IP_ROUTE_MULTIPATH
392 static int fib_count_nexthops(struct rtnexthop
*rtnh
, int remaining
)
396 while (rtnh_ok(rtnh
, remaining
)) {
398 rtnh
= rtnh_next(rtnh
, &remaining
);
401 /* leftover implies invalid nexthop configuration, discard it */
402 return remaining
> 0 ? 0 : nhs
;
405 static int fib_get_nhs(struct fib_info
*fi
, struct rtnexthop
*rtnh
,
406 int remaining
, struct fib_config
*cfg
)
408 change_nexthops(fi
) {
411 if (!rtnh_ok(rtnh
, remaining
))
414 nexthop_nh
->nh_flags
=
415 (cfg
->fc_flags
& ~0xFF) | rtnh
->rtnh_flags
;
416 nexthop_nh
->nh_oif
= rtnh
->rtnh_ifindex
;
417 nexthop_nh
->nh_weight
= rtnh
->rtnh_hops
+ 1;
419 attrlen
= rtnh_attrlen(rtnh
);
421 struct nlattr
*nla
, *attrs
= rtnh_attrs(rtnh
);
423 nla
= nla_find(attrs
, attrlen
, RTA_GATEWAY
);
424 nexthop_nh
->nh_gw
= nla
? nla_get_be32(nla
) : 0;
425 #ifdef CONFIG_NET_CLS_ROUTE
426 nla
= nla_find(attrs
, attrlen
, RTA_FLOW
);
427 nexthop_nh
->nh_tclassid
= nla
? nla_get_u32(nla
) : 0;
431 rtnh
= rtnh_next(rtnh
, &remaining
);
432 } endfor_nexthops(fi
);
439 int fib_nh_match(struct fib_config
*cfg
, struct fib_info
*fi
)
441 #ifdef CONFIG_IP_ROUTE_MULTIPATH
442 struct rtnexthop
*rtnh
;
446 if (cfg
->fc_priority
&& cfg
->fc_priority
!= fi
->fib_priority
)
449 if (cfg
->fc_oif
|| cfg
->fc_gw
) {
450 if ((!cfg
->fc_oif
|| cfg
->fc_oif
== fi
->fib_nh
->nh_oif
) &&
451 (!cfg
->fc_gw
|| cfg
->fc_gw
== fi
->fib_nh
->nh_gw
))
456 #ifdef CONFIG_IP_ROUTE_MULTIPATH
457 if (cfg
->fc_mp
== NULL
)
461 remaining
= cfg
->fc_mp_len
;
466 if (!rtnh_ok(rtnh
, remaining
))
469 if (rtnh
->rtnh_ifindex
&& rtnh
->rtnh_ifindex
!= nh
->nh_oif
)
472 attrlen
= rtnh_attrlen(rtnh
);
474 struct nlattr
*nla
, *attrs
= rtnh_attrs(rtnh
);
476 nla
= nla_find(attrs
, attrlen
, RTA_GATEWAY
);
477 if (nla
&& nla_get_be32(nla
) != nh
->nh_gw
)
479 #ifdef CONFIG_NET_CLS_ROUTE
480 nla
= nla_find(attrs
, attrlen
, RTA_FLOW
);
481 if (nla
&& nla_get_u32(nla
) != nh
->nh_tclassid
)
486 rtnh
= rtnh_next(rtnh
, &remaining
);
487 } endfor_nexthops(fi
);
497 * Semantics of nexthop is very messy by historical reasons.
498 * We have to take into account, that:
499 * a) gateway can be actually local interface address,
500 * so that gatewayed route is direct.
501 * b) gateway must be on-link address, possibly
502 * described not by an ifaddr, but also by a direct route.
503 * c) If both gateway and interface are specified, they should not
505 * d) If we use tunnel routes, gateway could be not on-link.
507 * Attempt to reconcile all of these (alas, self-contradictory) conditions
508 * results in pretty ugly and hairy code with obscure logic.
510 * I chose to generalized it instead, so that the size
511 * of code does not increase practically, but it becomes
513 * Every prefix is assigned a "scope" value: "host" is local address,
514 * "link" is direct route,
515 * [ ... "site" ... "interior" ... ]
516 * and "universe" is true gateway route with global meaning.
518 * Every prefix refers to a set of "nexthop"s (gw, oif),
519 * where gw must have narrower scope. This recursion stops
520 * when gw has LOCAL scope or if "nexthop" is declared ONLINK,
521 * which means that gw is forced to be on link.
523 * Code is still hairy, but now it is apparently logically
524 * consistent and very flexible. F.e. as by-product it allows
525 * to co-exists in peace independent exterior and interior
528 * Normally it looks as following.
530 * {universe prefix} -> (gw, oif) [scope link]
532 * |-> {link prefix} -> (gw, oif) [scope local]
534 * |-> {local prefix} (terminal node)
536 static int fib_check_nh(struct fib_config
*cfg
, struct fib_info
*fi
,
541 struct net_device
*dev
;
543 net
= cfg
->fc_nlinfo
.nl_net
;
545 struct fib_result res
;
547 if (nh
->nh_flags
& RTNH_F_ONLINK
) {
549 if (cfg
->fc_scope
>= RT_SCOPE_LINK
)
551 if (inet_addr_type(net
, nh
->nh_gw
) != RTN_UNICAST
)
553 dev
= __dev_get_by_index(net
, nh
->nh_oif
);
556 if (!(dev
->flags
& IFF_UP
))
560 nh
->nh_scope
= RT_SCOPE_LINK
;
569 .scope
= cfg
->fc_scope
+ 1,
575 /* It is not necessary, but requires a bit of thinking */
576 if (fl
.fl4_scope
< RT_SCOPE_LINK
)
577 fl
.fl4_scope
= RT_SCOPE_LINK
;
578 err
= fib_lookup(net
, &fl
, &res
);
585 if (res
.type
!= RTN_UNICAST
&& res
.type
!= RTN_LOCAL
)
587 nh
->nh_scope
= res
.scope
;
588 nh
->nh_oif
= FIB_RES_OIF(res
);
589 nh
->nh_dev
= dev
= FIB_RES_DEV(res
);
593 err
= (dev
->flags
& IFF_UP
) ? 0 : -ENETDOWN
;
595 struct in_device
*in_dev
;
597 if (nh
->nh_flags
& (RTNH_F_PERVASIVE
| RTNH_F_ONLINK
))
602 in_dev
= inetdev_by_index(net
, nh
->nh_oif
);
606 if (!(in_dev
->dev
->flags
& IFF_UP
))
608 nh
->nh_dev
= in_dev
->dev
;
609 dev_hold(nh
->nh_dev
);
610 nh
->nh_scope
= RT_SCOPE_HOST
;
618 static inline unsigned int fib_laddr_hashfn(__be32 val
)
620 unsigned int mask
= (fib_hash_size
- 1);
622 return ((__force u32
)val
^
623 ((__force u32
)val
>> 7) ^
624 ((__force u32
)val
>> 14)) & mask
;
627 static struct hlist_head
*fib_hash_alloc(int bytes
)
629 if (bytes
<= PAGE_SIZE
)
630 return kzalloc(bytes
, GFP_KERNEL
);
632 return (struct hlist_head
*)
633 __get_free_pages(GFP_KERNEL
| __GFP_ZERO
,
637 static void fib_hash_free(struct hlist_head
*hash
, int bytes
)
642 if (bytes
<= PAGE_SIZE
)
645 free_pages((unsigned long) hash
, get_order(bytes
));
648 static void fib_hash_move(struct hlist_head
*new_info_hash
,
649 struct hlist_head
*new_laddrhash
,
650 unsigned int new_size
)
652 struct hlist_head
*old_info_hash
, *old_laddrhash
;
653 unsigned int old_size
= fib_hash_size
;
654 unsigned int i
, bytes
;
656 spin_lock_bh(&fib_info_lock
);
657 old_info_hash
= fib_info_hash
;
658 old_laddrhash
= fib_info_laddrhash
;
659 fib_hash_size
= new_size
;
661 for (i
= 0; i
< old_size
; i
++) {
662 struct hlist_head
*head
= &fib_info_hash
[i
];
663 struct hlist_node
*node
, *n
;
666 hlist_for_each_entry_safe(fi
, node
, n
, head
, fib_hash
) {
667 struct hlist_head
*dest
;
668 unsigned int new_hash
;
670 hlist_del(&fi
->fib_hash
);
672 new_hash
= fib_info_hashfn(fi
);
673 dest
= &new_info_hash
[new_hash
];
674 hlist_add_head(&fi
->fib_hash
, dest
);
677 fib_info_hash
= new_info_hash
;
679 for (i
= 0; i
< old_size
; i
++) {
680 struct hlist_head
*lhead
= &fib_info_laddrhash
[i
];
681 struct hlist_node
*node
, *n
;
684 hlist_for_each_entry_safe(fi
, node
, n
, lhead
, fib_lhash
) {
685 struct hlist_head
*ldest
;
686 unsigned int new_hash
;
688 hlist_del(&fi
->fib_lhash
);
690 new_hash
= fib_laddr_hashfn(fi
->fib_prefsrc
);
691 ldest
= &new_laddrhash
[new_hash
];
692 hlist_add_head(&fi
->fib_lhash
, ldest
);
695 fib_info_laddrhash
= new_laddrhash
;
697 spin_unlock_bh(&fib_info_lock
);
699 bytes
= old_size
* sizeof(struct hlist_head
*);
700 fib_hash_free(old_info_hash
, bytes
);
701 fib_hash_free(old_laddrhash
, bytes
);
704 struct fib_info
*fib_create_info(struct fib_config
*cfg
)
707 struct fib_info
*fi
= NULL
;
708 struct fib_info
*ofi
;
710 struct net
*net
= cfg
->fc_nlinfo
.nl_net
;
712 /* Fast check to catch the most weird cases */
713 if (fib_props
[cfg
->fc_type
].scope
> cfg
->fc_scope
)
716 #ifdef CONFIG_IP_ROUTE_MULTIPATH
718 nhs
= fib_count_nexthops(cfg
->fc_mp
, cfg
->fc_mp_len
);
725 if (fib_info_cnt
>= fib_hash_size
) {
726 unsigned int new_size
= fib_hash_size
<< 1;
727 struct hlist_head
*new_info_hash
;
728 struct hlist_head
*new_laddrhash
;
733 bytes
= new_size
* sizeof(struct hlist_head
*);
734 new_info_hash
= fib_hash_alloc(bytes
);
735 new_laddrhash
= fib_hash_alloc(bytes
);
736 if (!new_info_hash
|| !new_laddrhash
) {
737 fib_hash_free(new_info_hash
, bytes
);
738 fib_hash_free(new_laddrhash
, bytes
);
740 fib_hash_move(new_info_hash
, new_laddrhash
, new_size
);
746 fi
= kzalloc(sizeof(*fi
)+nhs
*sizeof(struct fib_nh
), GFP_KERNEL
);
751 fi
->fib_net
= hold_net(net
);
752 fi
->fib_protocol
= cfg
->fc_protocol
;
753 fi
->fib_flags
= cfg
->fc_flags
;
754 fi
->fib_priority
= cfg
->fc_priority
;
755 fi
->fib_prefsrc
= cfg
->fc_prefsrc
;
758 change_nexthops(fi
) {
759 nexthop_nh
->nh_parent
= fi
;
760 } endfor_nexthops(fi
)
766 nla_for_each_attr(nla
, cfg
->fc_mx
, cfg
->fc_mx_len
, remaining
) {
767 int type
= nla_type(nla
);
772 fi
->fib_metrics
[type
- 1] = nla_get_u32(nla
);
778 #ifdef CONFIG_IP_ROUTE_MULTIPATH
779 err
= fib_get_nhs(fi
, cfg
->fc_mp
, cfg
->fc_mp_len
, cfg
);
782 if (cfg
->fc_oif
&& fi
->fib_nh
->nh_oif
!= cfg
->fc_oif
)
784 if (cfg
->fc_gw
&& fi
->fib_nh
->nh_gw
!= cfg
->fc_gw
)
786 #ifdef CONFIG_NET_CLS_ROUTE
787 if (cfg
->fc_flow
&& fi
->fib_nh
->nh_tclassid
!= cfg
->fc_flow
)
794 struct fib_nh
*nh
= fi
->fib_nh
;
796 nh
->nh_oif
= cfg
->fc_oif
;
797 nh
->nh_gw
= cfg
->fc_gw
;
798 nh
->nh_flags
= cfg
->fc_flags
;
799 #ifdef CONFIG_NET_CLS_ROUTE
800 nh
->nh_tclassid
= cfg
->fc_flow
;
802 #ifdef CONFIG_IP_ROUTE_MULTIPATH
807 if (fib_props
[cfg
->fc_type
].error
) {
808 if (cfg
->fc_gw
|| cfg
->fc_oif
|| cfg
->fc_mp
)
813 if (cfg
->fc_scope
> RT_SCOPE_HOST
)
816 if (cfg
->fc_scope
== RT_SCOPE_HOST
) {
817 struct fib_nh
*nh
= fi
->fib_nh
;
819 /* Local address is added. */
820 if (nhs
!= 1 || nh
->nh_gw
)
822 nh
->nh_scope
= RT_SCOPE_NOWHERE
;
823 nh
->nh_dev
= dev_get_by_index(net
, fi
->fib_nh
->nh_oif
);
825 if (nh
->nh_dev
== NULL
)
828 change_nexthops(fi
) {
829 err
= fib_check_nh(cfg
, fi
, nexthop_nh
);
832 } endfor_nexthops(fi
)
835 if (fi
->fib_prefsrc
) {
836 if (cfg
->fc_type
!= RTN_LOCAL
|| !cfg
->fc_dst
||
837 fi
->fib_prefsrc
!= cfg
->fc_dst
)
838 if (inet_addr_type(net
, fi
->fib_prefsrc
) != RTN_LOCAL
)
843 ofi
= fib_find_info(fi
);
852 atomic_inc(&fi
->fib_clntref
);
853 spin_lock_bh(&fib_info_lock
);
854 hlist_add_head(&fi
->fib_hash
,
855 &fib_info_hash
[fib_info_hashfn(fi
)]);
856 if (fi
->fib_prefsrc
) {
857 struct hlist_head
*head
;
859 head
= &fib_info_laddrhash
[fib_laddr_hashfn(fi
->fib_prefsrc
)];
860 hlist_add_head(&fi
->fib_lhash
, head
);
862 change_nexthops(fi
) {
863 struct hlist_head
*head
;
866 if (!nexthop_nh
->nh_dev
)
868 hash
= fib_devindex_hashfn(nexthop_nh
->nh_dev
->ifindex
);
869 head
= &fib_info_devhash
[hash
];
870 hlist_add_head(&nexthop_nh
->nh_hash
, head
);
871 } endfor_nexthops(fi
)
872 spin_unlock_bh(&fib_info_lock
);
887 /* Note! fib_semantic_match intentionally uses RCU list functions. */
888 int fib_semantic_match(struct list_head
*head
, const struct flowi
*flp
,
889 struct fib_result
*res
, int prefixlen
, int fib_flags
)
891 struct fib_alias
*fa
;
894 list_for_each_entry_rcu(fa
, head
, fa_list
) {
898 fa
->fa_tos
!= flp
->fl4_tos
)
901 if (fa
->fa_scope
< flp
->fl4_scope
)
904 fib_alias_accessed(fa
);
906 err
= fib_props
[fa
->fa_type
].error
;
908 struct fib_info
*fi
= fa
->fa_info
;
910 if (fi
->fib_flags
& RTNH_F_DEAD
)
913 switch (fa
->fa_type
) {
920 if (nh
->nh_flags
& RTNH_F_DEAD
)
922 if (!flp
->oif
|| flp
->oif
== nh
->nh_oif
)
925 #ifdef CONFIG_IP_ROUTE_MULTIPATH
926 if (nhsel
< fi
->fib_nhs
) {
938 pr_warning("fib_semantic_match bad type %#x\n",
948 res
->prefixlen
= prefixlen
;
949 res
->nh_sel
= nh_sel
;
950 res
->type
= fa
->fa_type
;
951 res
->scope
= fa
->fa_scope
;
952 res
->fi
= fa
->fa_info
;
953 if (!(fib_flags
& FIB_LOOKUP_NOREF
))
954 atomic_inc(&res
->fi
->fib_clntref
);
958 /* Find appropriate source address to this destination */
960 __be32
__fib_res_prefsrc(struct fib_result
*res
)
962 return inet_select_addr(FIB_RES_DEV(*res
), FIB_RES_GW(*res
), res
->scope
);
965 int fib_dump_info(struct sk_buff
*skb
, u32 pid
, u32 seq
, int event
,
966 u32 tb_id
, u8 type
, u8 scope
, __be32 dst
, int dst_len
, u8 tos
,
967 struct fib_info
*fi
, unsigned int flags
)
969 struct nlmsghdr
*nlh
;
972 nlh
= nlmsg_put(skb
, pid
, seq
, event
, sizeof(*rtm
), flags
);
976 rtm
= nlmsg_data(nlh
);
977 rtm
->rtm_family
= AF_INET
;
978 rtm
->rtm_dst_len
= dst_len
;
979 rtm
->rtm_src_len
= 0;
982 rtm
->rtm_table
= tb_id
;
984 rtm
->rtm_table
= RT_TABLE_COMPAT
;
985 NLA_PUT_U32(skb
, RTA_TABLE
, tb_id
);
986 rtm
->rtm_type
= type
;
987 rtm
->rtm_flags
= fi
->fib_flags
;
988 rtm
->rtm_scope
= scope
;
989 rtm
->rtm_protocol
= fi
->fib_protocol
;
991 if (rtm
->rtm_dst_len
)
992 NLA_PUT_BE32(skb
, RTA_DST
, dst
);
994 if (fi
->fib_priority
)
995 NLA_PUT_U32(skb
, RTA_PRIORITY
, fi
->fib_priority
);
997 if (rtnetlink_put_metrics(skb
, fi
->fib_metrics
) < 0)
998 goto nla_put_failure
;
1000 if (fi
->fib_prefsrc
)
1001 NLA_PUT_BE32(skb
, RTA_PREFSRC
, fi
->fib_prefsrc
);
1003 if (fi
->fib_nhs
== 1) {
1004 if (fi
->fib_nh
->nh_gw
)
1005 NLA_PUT_BE32(skb
, RTA_GATEWAY
, fi
->fib_nh
->nh_gw
);
1007 if (fi
->fib_nh
->nh_oif
)
1008 NLA_PUT_U32(skb
, RTA_OIF
, fi
->fib_nh
->nh_oif
);
1009 #ifdef CONFIG_NET_CLS_ROUTE
1010 if (fi
->fib_nh
[0].nh_tclassid
)
1011 NLA_PUT_U32(skb
, RTA_FLOW
, fi
->fib_nh
[0].nh_tclassid
);
1014 #ifdef CONFIG_IP_ROUTE_MULTIPATH
1015 if (fi
->fib_nhs
> 1) {
1016 struct rtnexthop
*rtnh
;
1019 mp
= nla_nest_start(skb
, RTA_MULTIPATH
);
1021 goto nla_put_failure
;
1024 rtnh
= nla_reserve_nohdr(skb
, sizeof(*rtnh
));
1026 goto nla_put_failure
;
1028 rtnh
->rtnh_flags
= nh
->nh_flags
& 0xFF;
1029 rtnh
->rtnh_hops
= nh
->nh_weight
- 1;
1030 rtnh
->rtnh_ifindex
= nh
->nh_oif
;
1033 NLA_PUT_BE32(skb
, RTA_GATEWAY
, nh
->nh_gw
);
1034 #ifdef CONFIG_NET_CLS_ROUTE
1035 if (nh
->nh_tclassid
)
1036 NLA_PUT_U32(skb
, RTA_FLOW
, nh
->nh_tclassid
);
1038 /* length of rtnetlink header + attributes */
1039 rtnh
->rtnh_len
= nlmsg_get_pos(skb
) - (void *) rtnh
;
1040 } endfor_nexthops(fi
);
1042 nla_nest_end(skb
, mp
);
1045 return nlmsg_end(skb
, nlh
);
1048 nlmsg_cancel(skb
, nlh
);
1054 * - local address disappeared -> we must delete all the entries
1056 * - device went down -> we must shutdown all nexthops going via it.
1058 int fib_sync_down_addr(struct net
*net
, __be32 local
)
1061 unsigned int hash
= fib_laddr_hashfn(local
);
1062 struct hlist_head
*head
= &fib_info_laddrhash
[hash
];
1063 struct hlist_node
*node
;
1064 struct fib_info
*fi
;
1066 if (fib_info_laddrhash
== NULL
|| local
== 0)
1069 hlist_for_each_entry(fi
, node
, head
, fib_lhash
) {
1070 if (!net_eq(fi
->fib_net
, net
))
1072 if (fi
->fib_prefsrc
== local
) {
1073 fi
->fib_flags
|= RTNH_F_DEAD
;
1080 int fib_sync_down_dev(struct net_device
*dev
, int force
)
1083 int scope
= RT_SCOPE_NOWHERE
;
1084 struct fib_info
*prev_fi
= NULL
;
1085 unsigned int hash
= fib_devindex_hashfn(dev
->ifindex
);
1086 struct hlist_head
*head
= &fib_info_devhash
[hash
];
1087 struct hlist_node
*node
;
1093 hlist_for_each_entry(nh
, node
, head
, nh_hash
) {
1094 struct fib_info
*fi
= nh
->nh_parent
;
1097 BUG_ON(!fi
->fib_nhs
);
1098 if (nh
->nh_dev
!= dev
|| fi
== prev_fi
)
1102 change_nexthops(fi
) {
1103 if (nexthop_nh
->nh_flags
& RTNH_F_DEAD
)
1105 else if (nexthop_nh
->nh_dev
== dev
&&
1106 nexthop_nh
->nh_scope
!= scope
) {
1107 nexthop_nh
->nh_flags
|= RTNH_F_DEAD
;
1108 #ifdef CONFIG_IP_ROUTE_MULTIPATH
1109 spin_lock_bh(&fib_multipath_lock
);
1110 fi
->fib_power
-= nexthop_nh
->nh_power
;
1111 nexthop_nh
->nh_power
= 0;
1112 spin_unlock_bh(&fib_multipath_lock
);
1116 #ifdef CONFIG_IP_ROUTE_MULTIPATH
1117 if (force
> 1 && nexthop_nh
->nh_dev
== dev
) {
1122 } endfor_nexthops(fi
)
1123 if (dead
== fi
->fib_nhs
) {
1124 fi
->fib_flags
|= RTNH_F_DEAD
;
1132 #ifdef CONFIG_IP_ROUTE_MULTIPATH
1135 * Dead device goes up. We wake up dead nexthops.
1136 * It takes sense only on multipath routes.
1138 int fib_sync_up(struct net_device
*dev
)
1140 struct fib_info
*prev_fi
;
1142 struct hlist_head
*head
;
1143 struct hlist_node
*node
;
1147 if (!(dev
->flags
& IFF_UP
))
1151 hash
= fib_devindex_hashfn(dev
->ifindex
);
1152 head
= &fib_info_devhash
[hash
];
1155 hlist_for_each_entry(nh
, node
, head
, nh_hash
) {
1156 struct fib_info
*fi
= nh
->nh_parent
;
1159 BUG_ON(!fi
->fib_nhs
);
1160 if (nh
->nh_dev
!= dev
|| fi
== prev_fi
)
1165 change_nexthops(fi
) {
1166 if (!(nexthop_nh
->nh_flags
& RTNH_F_DEAD
)) {
1170 if (nexthop_nh
->nh_dev
== NULL
||
1171 !(nexthop_nh
->nh_dev
->flags
& IFF_UP
))
1173 if (nexthop_nh
->nh_dev
!= dev
||
1174 !__in_dev_get_rtnl(dev
))
1177 spin_lock_bh(&fib_multipath_lock
);
1178 nexthop_nh
->nh_power
= 0;
1179 nexthop_nh
->nh_flags
&= ~RTNH_F_DEAD
;
1180 spin_unlock_bh(&fib_multipath_lock
);
1181 } endfor_nexthops(fi
)
1184 fi
->fib_flags
&= ~RTNH_F_DEAD
;
1193 * The algorithm is suboptimal, but it provides really
1194 * fair weighted route distribution.
1196 void fib_select_multipath(const struct flowi
*flp
, struct fib_result
*res
)
1198 struct fib_info
*fi
= res
->fi
;
1201 spin_lock_bh(&fib_multipath_lock
);
1202 if (fi
->fib_power
<= 0) {
1204 change_nexthops(fi
) {
1205 if (!(nexthop_nh
->nh_flags
& RTNH_F_DEAD
)) {
1206 power
+= nexthop_nh
->nh_weight
;
1207 nexthop_nh
->nh_power
= nexthop_nh
->nh_weight
;
1209 } endfor_nexthops(fi
);
1210 fi
->fib_power
= power
;
1212 spin_unlock_bh(&fib_multipath_lock
);
1213 /* Race condition: route has just become dead. */
1220 /* w should be random number [0..fi->fib_power-1],
1221 * it is pretty bad approximation.
1224 w
= jiffies
% fi
->fib_power
;
1226 change_nexthops(fi
) {
1227 if (!(nexthop_nh
->nh_flags
& RTNH_F_DEAD
) &&
1228 nexthop_nh
->nh_power
) {
1229 w
-= nexthop_nh
->nh_power
;
1231 nexthop_nh
->nh_power
--;
1233 res
->nh_sel
= nhsel
;
1234 spin_unlock_bh(&fib_multipath_lock
);
1238 } endfor_nexthops(fi
);
1240 /* Race condition: route has just become dead. */
1242 spin_unlock_bh(&fib_multipath_lock
);