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 free_nh_exceptions(struct fib_nh
*nh
)
145 struct fnhe_hash_bucket
*hash
= nh
->nh_exceptions
;
148 for (i
= 0; i
< FNHE_HASH_SIZE
; i
++) {
149 struct fib_nh_exception
*fnhe
;
151 fnhe
= rcu_dereference_protected(hash
[i
].chain
, 1);
153 struct fib_nh_exception
*next
;
155 next
= rcu_dereference_protected(fnhe
->fnhe_next
, 1);
164 /* Release a nexthop info record */
165 static void free_fib_info_rcu(struct rcu_head
*head
)
167 struct fib_info
*fi
= container_of(head
, struct fib_info
, rcu
);
169 change_nexthops(fi
) {
170 if (nexthop_nh
->nh_dev
)
171 dev_put(nexthop_nh
->nh_dev
);
172 if (nexthop_nh
->nh_exceptions
)
173 free_nh_exceptions(nexthop_nh
);
174 if (nexthop_nh
->nh_rth_output
)
175 dst_release(&nexthop_nh
->nh_rth_output
->dst
);
176 } endfor_nexthops(fi
);
178 release_net(fi
->fib_net
);
179 if (fi
->fib_metrics
!= (u32
*) dst_default_metrics
)
180 kfree(fi
->fib_metrics
);
184 void free_fib_info(struct fib_info
*fi
)
186 if (fi
->fib_dead
== 0) {
187 pr_warn("Freeing alive fib_info %p\n", fi
);
191 #ifdef CONFIG_IP_ROUTE_CLASSID
192 change_nexthops(fi
) {
193 if (nexthop_nh
->nh_tclassid
)
194 fi
->fib_net
->ipv4
.fib_num_tclassid_users
--;
195 } endfor_nexthops(fi
);
197 call_rcu(&fi
->rcu
, free_fib_info_rcu
);
200 void fib_release_info(struct fib_info
*fi
)
202 spin_lock_bh(&fib_info_lock
);
203 if (fi
&& --fi
->fib_treeref
== 0) {
204 hlist_del(&fi
->fib_hash
);
206 hlist_del(&fi
->fib_lhash
);
207 change_nexthops(fi
) {
208 if (!nexthop_nh
->nh_dev
)
210 hlist_del(&nexthop_nh
->nh_hash
);
211 } endfor_nexthops(fi
)
215 spin_unlock_bh(&fib_info_lock
);
218 static inline int nh_comp(const struct fib_info
*fi
, const struct fib_info
*ofi
)
220 const struct fib_nh
*onh
= ofi
->fib_nh
;
223 if (nh
->nh_oif
!= onh
->nh_oif
||
224 nh
->nh_gw
!= onh
->nh_gw
||
225 nh
->nh_scope
!= onh
->nh_scope
||
226 #ifdef CONFIG_IP_ROUTE_MULTIPATH
227 nh
->nh_weight
!= onh
->nh_weight
||
229 #ifdef CONFIG_IP_ROUTE_CLASSID
230 nh
->nh_tclassid
!= onh
->nh_tclassid
||
232 ((nh
->nh_flags
^ onh
->nh_flags
) & ~RTNH_F_DEAD
))
235 } endfor_nexthops(fi
);
239 static inline unsigned int fib_devindex_hashfn(unsigned int val
)
241 unsigned int mask
= DEVINDEX_HASHSIZE
- 1;
244 (val
>> DEVINDEX_HASHBITS
) ^
245 (val
>> (DEVINDEX_HASHBITS
* 2))) & mask
;
248 static inline unsigned int fib_info_hashfn(const struct fib_info
*fi
)
250 unsigned int mask
= (fib_info_hash_size
- 1);
251 unsigned int val
= fi
->fib_nhs
;
253 val
^= (fi
->fib_protocol
<< 8) | fi
->fib_scope
;
254 val
^= (__force u32
)fi
->fib_prefsrc
;
255 val
^= fi
->fib_priority
;
257 val
^= fib_devindex_hashfn(nh
->nh_oif
);
258 } endfor_nexthops(fi
)
260 return (val
^ (val
>> 7) ^ (val
>> 12)) & mask
;
263 static struct fib_info
*fib_find_info(const struct fib_info
*nfi
)
265 struct hlist_head
*head
;
266 struct hlist_node
*node
;
270 hash
= fib_info_hashfn(nfi
);
271 head
= &fib_info_hash
[hash
];
273 hlist_for_each_entry(fi
, node
, head
, fib_hash
) {
274 if (!net_eq(fi
->fib_net
, nfi
->fib_net
))
276 if (fi
->fib_nhs
!= nfi
->fib_nhs
)
278 if (nfi
->fib_protocol
== fi
->fib_protocol
&&
279 nfi
->fib_scope
== fi
->fib_scope
&&
280 nfi
->fib_prefsrc
== fi
->fib_prefsrc
&&
281 nfi
->fib_priority
== fi
->fib_priority
&&
282 memcmp(nfi
->fib_metrics
, fi
->fib_metrics
,
283 sizeof(u32
) * RTAX_MAX
) == 0 &&
284 ((nfi
->fib_flags
^ fi
->fib_flags
) & ~RTNH_F_DEAD
) == 0 &&
285 (nfi
->fib_nhs
== 0 || nh_comp(fi
, nfi
) == 0))
292 /* Check, that the gateway is already configured.
293 * Used only by redirect accept routine.
295 int ip_fib_check_default(__be32 gw
, struct net_device
*dev
)
297 struct hlist_head
*head
;
298 struct hlist_node
*node
;
302 spin_lock(&fib_info_lock
);
304 hash
= fib_devindex_hashfn(dev
->ifindex
);
305 head
= &fib_info_devhash
[hash
];
306 hlist_for_each_entry(nh
, node
, head
, nh_hash
) {
307 if (nh
->nh_dev
== dev
&&
309 !(nh
->nh_flags
& RTNH_F_DEAD
)) {
310 spin_unlock(&fib_info_lock
);
315 spin_unlock(&fib_info_lock
);
320 static inline size_t fib_nlmsg_size(struct fib_info
*fi
)
322 size_t payload
= NLMSG_ALIGN(sizeof(struct rtmsg
))
323 + nla_total_size(4) /* RTA_TABLE */
324 + nla_total_size(4) /* RTA_DST */
325 + nla_total_size(4) /* RTA_PRIORITY */
326 + nla_total_size(4); /* RTA_PREFSRC */
328 /* space for nested metrics */
329 payload
+= nla_total_size((RTAX_MAX
* nla_total_size(4)));
332 /* Also handles the special case fib_nhs == 1 */
334 /* each nexthop is packed in an attribute */
335 size_t nhsize
= nla_total_size(sizeof(struct rtnexthop
));
337 /* may contain flow and gateway attribute */
338 nhsize
+= 2 * nla_total_size(4);
340 /* all nexthops are packed in a nested attribute */
341 payload
+= nla_total_size(fi
->fib_nhs
* nhsize
);
347 void rtmsg_fib(int event
, __be32 key
, struct fib_alias
*fa
,
348 int dst_len
, u32 tb_id
, struct nl_info
*info
,
349 unsigned int nlm_flags
)
352 u32 seq
= info
->nlh
? info
->nlh
->nlmsg_seq
: 0;
355 skb
= nlmsg_new(fib_nlmsg_size(fa
->fa_info
), GFP_KERNEL
);
359 err
= fib_dump_info(skb
, info
->pid
, seq
, event
, tb_id
,
360 fa
->fa_type
, key
, dst_len
,
361 fa
->fa_tos
, fa
->fa_info
, nlm_flags
);
363 /* -EMSGSIZE implies BUG in fib_nlmsg_size() */
364 WARN_ON(err
== -EMSGSIZE
);
368 rtnl_notify(skb
, info
->nl_net
, info
->pid
, RTNLGRP_IPV4_ROUTE
,
369 info
->nlh
, GFP_KERNEL
);
373 rtnl_set_sk_err(info
->nl_net
, RTNLGRP_IPV4_ROUTE
, err
);
376 /* Return the first fib alias matching TOS with
377 * priority less than or equal to PRIO.
379 struct fib_alias
*fib_find_alias(struct list_head
*fah
, u8 tos
, u32 prio
)
382 struct fib_alias
*fa
;
383 list_for_each_entry(fa
, fah
, fa_list
) {
384 if (fa
->fa_tos
> tos
)
386 if (fa
->fa_info
->fib_priority
>= prio
||
394 int fib_detect_death(struct fib_info
*fi
, int order
,
395 struct fib_info
**last_resort
, int *last_idx
, int dflt
)
398 int state
= NUD_NONE
;
400 n
= neigh_lookup(&arp_tbl
, &fi
->fib_nh
[0].nh_gw
, fi
->fib_dev
);
402 state
= n
->nud_state
;
405 if (state
== NUD_REACHABLE
)
407 if ((state
& NUD_VALID
) && order
!= dflt
)
409 if ((state
& NUD_VALID
) ||
410 (*last_idx
< 0 && order
> dflt
)) {
417 #ifdef CONFIG_IP_ROUTE_MULTIPATH
419 static int fib_count_nexthops(struct rtnexthop
*rtnh
, int remaining
)
423 while (rtnh_ok(rtnh
, remaining
)) {
425 rtnh
= rtnh_next(rtnh
, &remaining
);
428 /* leftover implies invalid nexthop configuration, discard it */
429 return remaining
> 0 ? 0 : nhs
;
432 static int fib_get_nhs(struct fib_info
*fi
, struct rtnexthop
*rtnh
,
433 int remaining
, struct fib_config
*cfg
)
435 change_nexthops(fi
) {
438 if (!rtnh_ok(rtnh
, remaining
))
441 nexthop_nh
->nh_flags
=
442 (cfg
->fc_flags
& ~0xFF) | rtnh
->rtnh_flags
;
443 nexthop_nh
->nh_oif
= rtnh
->rtnh_ifindex
;
444 nexthop_nh
->nh_weight
= rtnh
->rtnh_hops
+ 1;
446 attrlen
= rtnh_attrlen(rtnh
);
448 struct nlattr
*nla
, *attrs
= rtnh_attrs(rtnh
);
450 nla
= nla_find(attrs
, attrlen
, RTA_GATEWAY
);
451 nexthop_nh
->nh_gw
= nla
? nla_get_be32(nla
) : 0;
452 #ifdef CONFIG_IP_ROUTE_CLASSID
453 nla
= nla_find(attrs
, attrlen
, RTA_FLOW
);
454 nexthop_nh
->nh_tclassid
= nla
? nla_get_u32(nla
) : 0;
455 if (nexthop_nh
->nh_tclassid
)
456 fi
->fib_net
->ipv4
.fib_num_tclassid_users
++;
460 rtnh
= rtnh_next(rtnh
, &remaining
);
461 } endfor_nexthops(fi
);
468 int fib_nh_match(struct fib_config
*cfg
, struct fib_info
*fi
)
470 #ifdef CONFIG_IP_ROUTE_MULTIPATH
471 struct rtnexthop
*rtnh
;
475 if (cfg
->fc_priority
&& cfg
->fc_priority
!= fi
->fib_priority
)
478 if (cfg
->fc_oif
|| cfg
->fc_gw
) {
479 if ((!cfg
->fc_oif
|| cfg
->fc_oif
== fi
->fib_nh
->nh_oif
) &&
480 (!cfg
->fc_gw
|| cfg
->fc_gw
== fi
->fib_nh
->nh_gw
))
485 #ifdef CONFIG_IP_ROUTE_MULTIPATH
486 if (cfg
->fc_mp
== NULL
)
490 remaining
= cfg
->fc_mp_len
;
495 if (!rtnh_ok(rtnh
, remaining
))
498 if (rtnh
->rtnh_ifindex
&& rtnh
->rtnh_ifindex
!= nh
->nh_oif
)
501 attrlen
= rtnh_attrlen(rtnh
);
503 struct nlattr
*nla
, *attrs
= rtnh_attrs(rtnh
);
505 nla
= nla_find(attrs
, attrlen
, RTA_GATEWAY
);
506 if (nla
&& nla_get_be32(nla
) != nh
->nh_gw
)
508 #ifdef CONFIG_IP_ROUTE_CLASSID
509 nla
= nla_find(attrs
, attrlen
, RTA_FLOW
);
510 if (nla
&& nla_get_u32(nla
) != nh
->nh_tclassid
)
515 rtnh
= rtnh_next(rtnh
, &remaining
);
516 } endfor_nexthops(fi
);
526 * Semantics of nexthop is very messy by historical reasons.
527 * We have to take into account, that:
528 * a) gateway can be actually local interface address,
529 * so that gatewayed route is direct.
530 * b) gateway must be on-link address, possibly
531 * described not by an ifaddr, but also by a direct route.
532 * c) If both gateway and interface are specified, they should not
534 * d) If we use tunnel routes, gateway could be not on-link.
536 * Attempt to reconcile all of these (alas, self-contradictory) conditions
537 * results in pretty ugly and hairy code with obscure logic.
539 * I chose to generalized it instead, so that the size
540 * of code does not increase practically, but it becomes
542 * Every prefix is assigned a "scope" value: "host" is local address,
543 * "link" is direct route,
544 * [ ... "site" ... "interior" ... ]
545 * and "universe" is true gateway route with global meaning.
547 * Every prefix refers to a set of "nexthop"s (gw, oif),
548 * where gw must have narrower scope. This recursion stops
549 * when gw has LOCAL scope or if "nexthop" is declared ONLINK,
550 * which means that gw is forced to be on link.
552 * Code is still hairy, but now it is apparently logically
553 * consistent and very flexible. F.e. as by-product it allows
554 * to co-exists in peace independent exterior and interior
557 * Normally it looks as following.
559 * {universe prefix} -> (gw, oif) [scope link]
561 * |-> {link prefix} -> (gw, oif) [scope local]
563 * |-> {local prefix} (terminal node)
565 static int fib_check_nh(struct fib_config
*cfg
, struct fib_info
*fi
,
570 struct net_device
*dev
;
572 net
= cfg
->fc_nlinfo
.nl_net
;
574 struct fib_result res
;
576 if (nh
->nh_flags
& RTNH_F_ONLINK
) {
578 if (cfg
->fc_scope
>= RT_SCOPE_LINK
)
580 if (inet_addr_type(net
, nh
->nh_gw
) != RTN_UNICAST
)
582 dev
= __dev_get_by_index(net
, nh
->nh_oif
);
585 if (!(dev
->flags
& IFF_UP
))
589 nh
->nh_scope
= RT_SCOPE_LINK
;
594 struct flowi4 fl4
= {
596 .flowi4_scope
= cfg
->fc_scope
+ 1,
597 .flowi4_oif
= nh
->nh_oif
,
600 /* It is not necessary, but requires a bit of thinking */
601 if (fl4
.flowi4_scope
< RT_SCOPE_LINK
)
602 fl4
.flowi4_scope
= RT_SCOPE_LINK
;
603 err
= fib_lookup(net
, &fl4
, &res
);
610 if (res
.type
!= RTN_UNICAST
&& res
.type
!= RTN_LOCAL
)
612 nh
->nh_scope
= res
.scope
;
613 nh
->nh_oif
= FIB_RES_OIF(res
);
614 nh
->nh_dev
= dev
= FIB_RES_DEV(res
);
618 err
= (dev
->flags
& IFF_UP
) ? 0 : -ENETDOWN
;
620 struct in_device
*in_dev
;
622 if (nh
->nh_flags
& (RTNH_F_PERVASIVE
| RTNH_F_ONLINK
))
627 in_dev
= inetdev_by_index(net
, nh
->nh_oif
);
631 if (!(in_dev
->dev
->flags
& IFF_UP
))
633 nh
->nh_dev
= in_dev
->dev
;
634 dev_hold(nh
->nh_dev
);
635 nh
->nh_scope
= RT_SCOPE_HOST
;
643 static inline unsigned int fib_laddr_hashfn(__be32 val
)
645 unsigned int mask
= (fib_info_hash_size
- 1);
647 return ((__force u32
)val
^
648 ((__force u32
)val
>> 7) ^
649 ((__force u32
)val
>> 14)) & mask
;
652 static struct hlist_head
*fib_info_hash_alloc(int bytes
)
654 if (bytes
<= PAGE_SIZE
)
655 return kzalloc(bytes
, GFP_KERNEL
);
657 return (struct hlist_head
*)
658 __get_free_pages(GFP_KERNEL
| __GFP_ZERO
,
662 static void fib_info_hash_free(struct hlist_head
*hash
, int bytes
)
667 if (bytes
<= PAGE_SIZE
)
670 free_pages((unsigned long) hash
, get_order(bytes
));
673 static void fib_info_hash_move(struct hlist_head
*new_info_hash
,
674 struct hlist_head
*new_laddrhash
,
675 unsigned int new_size
)
677 struct hlist_head
*old_info_hash
, *old_laddrhash
;
678 unsigned int old_size
= fib_info_hash_size
;
679 unsigned int i
, bytes
;
681 spin_lock_bh(&fib_info_lock
);
682 old_info_hash
= fib_info_hash
;
683 old_laddrhash
= fib_info_laddrhash
;
684 fib_info_hash_size
= new_size
;
686 for (i
= 0; i
< old_size
; i
++) {
687 struct hlist_head
*head
= &fib_info_hash
[i
];
688 struct hlist_node
*node
, *n
;
691 hlist_for_each_entry_safe(fi
, node
, n
, head
, fib_hash
) {
692 struct hlist_head
*dest
;
693 unsigned int new_hash
;
695 hlist_del(&fi
->fib_hash
);
697 new_hash
= fib_info_hashfn(fi
);
698 dest
= &new_info_hash
[new_hash
];
699 hlist_add_head(&fi
->fib_hash
, dest
);
702 fib_info_hash
= new_info_hash
;
704 for (i
= 0; i
< old_size
; i
++) {
705 struct hlist_head
*lhead
= &fib_info_laddrhash
[i
];
706 struct hlist_node
*node
, *n
;
709 hlist_for_each_entry_safe(fi
, node
, n
, lhead
, fib_lhash
) {
710 struct hlist_head
*ldest
;
711 unsigned int new_hash
;
713 hlist_del(&fi
->fib_lhash
);
715 new_hash
= fib_laddr_hashfn(fi
->fib_prefsrc
);
716 ldest
= &new_laddrhash
[new_hash
];
717 hlist_add_head(&fi
->fib_lhash
, ldest
);
720 fib_info_laddrhash
= new_laddrhash
;
722 spin_unlock_bh(&fib_info_lock
);
724 bytes
= old_size
* sizeof(struct hlist_head
*);
725 fib_info_hash_free(old_info_hash
, bytes
);
726 fib_info_hash_free(old_laddrhash
, bytes
);
729 __be32
fib_info_update_nh_saddr(struct net
*net
, struct fib_nh
*nh
)
731 nh
->nh_saddr
= inet_select_addr(nh
->nh_dev
,
733 nh
->nh_parent
->fib_scope
);
734 nh
->nh_saddr_genid
= atomic_read(&net
->ipv4
.dev_addr_genid
);
739 struct fib_info
*fib_create_info(struct fib_config
*cfg
)
742 struct fib_info
*fi
= NULL
;
743 struct fib_info
*ofi
;
745 struct net
*net
= cfg
->fc_nlinfo
.nl_net
;
747 if (cfg
->fc_type
> RTN_MAX
)
750 /* Fast check to catch the most weird cases */
751 if (fib_props
[cfg
->fc_type
].scope
> cfg
->fc_scope
)
754 #ifdef CONFIG_IP_ROUTE_MULTIPATH
756 nhs
= fib_count_nexthops(cfg
->fc_mp
, cfg
->fc_mp_len
);
763 if (fib_info_cnt
>= fib_info_hash_size
) {
764 unsigned int new_size
= fib_info_hash_size
<< 1;
765 struct hlist_head
*new_info_hash
;
766 struct hlist_head
*new_laddrhash
;
771 bytes
= new_size
* sizeof(struct hlist_head
*);
772 new_info_hash
= fib_info_hash_alloc(bytes
);
773 new_laddrhash
= fib_info_hash_alloc(bytes
);
774 if (!new_info_hash
|| !new_laddrhash
) {
775 fib_info_hash_free(new_info_hash
, bytes
);
776 fib_info_hash_free(new_laddrhash
, bytes
);
778 fib_info_hash_move(new_info_hash
, new_laddrhash
, new_size
);
780 if (!fib_info_hash_size
)
784 fi
= kzalloc(sizeof(*fi
)+nhs
*sizeof(struct fib_nh
), GFP_KERNEL
);
788 fi
->fib_metrics
= kzalloc(sizeof(u32
) * RTAX_MAX
, GFP_KERNEL
);
789 if (!fi
->fib_metrics
)
792 fi
->fib_metrics
= (u32
*) dst_default_metrics
;
795 fi
->fib_net
= hold_net(net
);
796 fi
->fib_protocol
= cfg
->fc_protocol
;
797 fi
->fib_scope
= cfg
->fc_scope
;
798 fi
->fib_flags
= cfg
->fc_flags
;
799 fi
->fib_priority
= cfg
->fc_priority
;
800 fi
->fib_prefsrc
= cfg
->fc_prefsrc
;
803 change_nexthops(fi
) {
804 nexthop_nh
->nh_parent
= fi
;
805 } endfor_nexthops(fi
)
811 nla_for_each_attr(nla
, cfg
->fc_mx
, cfg
->fc_mx_len
, remaining
) {
812 int type
= nla_type(nla
);
819 val
= nla_get_u32(nla
);
820 if (type
== RTAX_ADVMSS
&& val
> 65535 - 40)
822 if (type
== RTAX_MTU
&& val
> 65535 - 15)
824 fi
->fib_metrics
[type
- 1] = val
;
830 #ifdef CONFIG_IP_ROUTE_MULTIPATH
831 err
= fib_get_nhs(fi
, cfg
->fc_mp
, cfg
->fc_mp_len
, cfg
);
834 if (cfg
->fc_oif
&& fi
->fib_nh
->nh_oif
!= cfg
->fc_oif
)
836 if (cfg
->fc_gw
&& fi
->fib_nh
->nh_gw
!= cfg
->fc_gw
)
838 #ifdef CONFIG_IP_ROUTE_CLASSID
839 if (cfg
->fc_flow
&& fi
->fib_nh
->nh_tclassid
!= cfg
->fc_flow
)
846 struct fib_nh
*nh
= fi
->fib_nh
;
848 nh
->nh_oif
= cfg
->fc_oif
;
849 nh
->nh_gw
= cfg
->fc_gw
;
850 nh
->nh_flags
= cfg
->fc_flags
;
851 #ifdef CONFIG_IP_ROUTE_CLASSID
852 nh
->nh_tclassid
= cfg
->fc_flow
;
854 fi
->fib_net
->ipv4
.fib_num_tclassid_users
++;
856 #ifdef CONFIG_IP_ROUTE_MULTIPATH
861 if (fib_props
[cfg
->fc_type
].error
) {
862 if (cfg
->fc_gw
|| cfg
->fc_oif
|| cfg
->fc_mp
)
866 switch (cfg
->fc_type
) {
878 if (cfg
->fc_scope
> RT_SCOPE_HOST
)
881 if (cfg
->fc_scope
== RT_SCOPE_HOST
) {
882 struct fib_nh
*nh
= fi
->fib_nh
;
884 /* Local address is added. */
885 if (nhs
!= 1 || nh
->nh_gw
)
887 nh
->nh_scope
= RT_SCOPE_NOWHERE
;
888 nh
->nh_dev
= dev_get_by_index(net
, fi
->fib_nh
->nh_oif
);
890 if (nh
->nh_dev
== NULL
)
893 change_nexthops(fi
) {
894 err
= fib_check_nh(cfg
, fi
, nexthop_nh
);
897 } endfor_nexthops(fi
)
900 if (fi
->fib_prefsrc
) {
901 if (cfg
->fc_type
!= RTN_LOCAL
|| !cfg
->fc_dst
||
902 fi
->fib_prefsrc
!= cfg
->fc_dst
)
903 if (inet_addr_type(net
, fi
->fib_prefsrc
) != RTN_LOCAL
)
907 change_nexthops(fi
) {
908 fib_info_update_nh_saddr(net
, nexthop_nh
);
909 } endfor_nexthops(fi
)
912 ofi
= fib_find_info(fi
);
921 atomic_inc(&fi
->fib_clntref
);
922 spin_lock_bh(&fib_info_lock
);
923 hlist_add_head(&fi
->fib_hash
,
924 &fib_info_hash
[fib_info_hashfn(fi
)]);
925 if (fi
->fib_prefsrc
) {
926 struct hlist_head
*head
;
928 head
= &fib_info_laddrhash
[fib_laddr_hashfn(fi
->fib_prefsrc
)];
929 hlist_add_head(&fi
->fib_lhash
, head
);
931 change_nexthops(fi
) {
932 struct hlist_head
*head
;
935 if (!nexthop_nh
->nh_dev
)
937 hash
= fib_devindex_hashfn(nexthop_nh
->nh_dev
->ifindex
);
938 head
= &fib_info_devhash
[hash
];
939 hlist_add_head(&nexthop_nh
->nh_hash
, head
);
940 } endfor_nexthops(fi
)
941 spin_unlock_bh(&fib_info_lock
);
956 int fib_dump_info(struct sk_buff
*skb
, u32 pid
, u32 seq
, int event
,
957 u32 tb_id
, u8 type
, __be32 dst
, int dst_len
, u8 tos
,
958 struct fib_info
*fi
, unsigned int flags
)
960 struct nlmsghdr
*nlh
;
963 nlh
= nlmsg_put(skb
, pid
, seq
, event
, sizeof(*rtm
), flags
);
967 rtm
= nlmsg_data(nlh
);
968 rtm
->rtm_family
= AF_INET
;
969 rtm
->rtm_dst_len
= dst_len
;
970 rtm
->rtm_src_len
= 0;
973 rtm
->rtm_table
= tb_id
;
975 rtm
->rtm_table
= RT_TABLE_COMPAT
;
976 if (nla_put_u32(skb
, RTA_TABLE
, tb_id
))
977 goto nla_put_failure
;
978 rtm
->rtm_type
= type
;
979 rtm
->rtm_flags
= fi
->fib_flags
;
980 rtm
->rtm_scope
= fi
->fib_scope
;
981 rtm
->rtm_protocol
= fi
->fib_protocol
;
983 if (rtm
->rtm_dst_len
&&
984 nla_put_be32(skb
, RTA_DST
, dst
))
985 goto nla_put_failure
;
986 if (fi
->fib_priority
&&
987 nla_put_u32(skb
, RTA_PRIORITY
, fi
->fib_priority
))
988 goto nla_put_failure
;
989 if (rtnetlink_put_metrics(skb
, fi
->fib_metrics
) < 0)
990 goto nla_put_failure
;
992 if (fi
->fib_prefsrc
&&
993 nla_put_be32(skb
, RTA_PREFSRC
, fi
->fib_prefsrc
))
994 goto nla_put_failure
;
995 if (fi
->fib_nhs
== 1) {
996 if (fi
->fib_nh
->nh_gw
&&
997 nla_put_be32(skb
, RTA_GATEWAY
, fi
->fib_nh
->nh_gw
))
998 goto nla_put_failure
;
999 if (fi
->fib_nh
->nh_oif
&&
1000 nla_put_u32(skb
, RTA_OIF
, fi
->fib_nh
->nh_oif
))
1001 goto nla_put_failure
;
1002 #ifdef CONFIG_IP_ROUTE_CLASSID
1003 if (fi
->fib_nh
[0].nh_tclassid
&&
1004 nla_put_u32(skb
, RTA_FLOW
, fi
->fib_nh
[0].nh_tclassid
))
1005 goto nla_put_failure
;
1008 #ifdef CONFIG_IP_ROUTE_MULTIPATH
1009 if (fi
->fib_nhs
> 1) {
1010 struct rtnexthop
*rtnh
;
1013 mp
= nla_nest_start(skb
, RTA_MULTIPATH
);
1015 goto nla_put_failure
;
1018 rtnh
= nla_reserve_nohdr(skb
, sizeof(*rtnh
));
1020 goto nla_put_failure
;
1022 rtnh
->rtnh_flags
= nh
->nh_flags
& 0xFF;
1023 rtnh
->rtnh_hops
= nh
->nh_weight
- 1;
1024 rtnh
->rtnh_ifindex
= nh
->nh_oif
;
1027 nla_put_be32(skb
, RTA_GATEWAY
, nh
->nh_gw
))
1028 goto nla_put_failure
;
1029 #ifdef CONFIG_IP_ROUTE_CLASSID
1030 if (nh
->nh_tclassid
&&
1031 nla_put_u32(skb
, RTA_FLOW
, nh
->nh_tclassid
))
1032 goto nla_put_failure
;
1034 /* length of rtnetlink header + attributes */
1035 rtnh
->rtnh_len
= nlmsg_get_pos(skb
) - (void *) rtnh
;
1036 } endfor_nexthops(fi
);
1038 nla_nest_end(skb
, mp
);
1041 return nlmsg_end(skb
, nlh
);
1044 nlmsg_cancel(skb
, nlh
);
1050 * - local address disappeared -> we must delete all the entries
1052 * - device went down -> we must shutdown all nexthops going via it.
1054 int fib_sync_down_addr(struct net
*net
, __be32 local
)
1057 unsigned int hash
= fib_laddr_hashfn(local
);
1058 struct hlist_head
*head
= &fib_info_laddrhash
[hash
];
1059 struct hlist_node
*node
;
1060 struct fib_info
*fi
;
1062 if (fib_info_laddrhash
== NULL
|| local
== 0)
1065 hlist_for_each_entry(fi
, node
, head
, fib_lhash
) {
1066 if (!net_eq(fi
->fib_net
, net
))
1068 if (fi
->fib_prefsrc
== local
) {
1069 fi
->fib_flags
|= RTNH_F_DEAD
;
1076 int fib_sync_down_dev(struct net_device
*dev
, int force
)
1079 int scope
= RT_SCOPE_NOWHERE
;
1080 struct fib_info
*prev_fi
= NULL
;
1081 unsigned int hash
= fib_devindex_hashfn(dev
->ifindex
);
1082 struct hlist_head
*head
= &fib_info_devhash
[hash
];
1083 struct hlist_node
*node
;
1089 hlist_for_each_entry(nh
, node
, head
, nh_hash
) {
1090 struct fib_info
*fi
= nh
->nh_parent
;
1093 BUG_ON(!fi
->fib_nhs
);
1094 if (nh
->nh_dev
!= dev
|| fi
== prev_fi
)
1098 change_nexthops(fi
) {
1099 if (nexthop_nh
->nh_flags
& RTNH_F_DEAD
)
1101 else if (nexthop_nh
->nh_dev
== dev
&&
1102 nexthop_nh
->nh_scope
!= scope
) {
1103 nexthop_nh
->nh_flags
|= RTNH_F_DEAD
;
1104 #ifdef CONFIG_IP_ROUTE_MULTIPATH
1105 spin_lock_bh(&fib_multipath_lock
);
1106 fi
->fib_power
-= nexthop_nh
->nh_power
;
1107 nexthop_nh
->nh_power
= 0;
1108 spin_unlock_bh(&fib_multipath_lock
);
1112 #ifdef CONFIG_IP_ROUTE_MULTIPATH
1113 if (force
> 1 && nexthop_nh
->nh_dev
== dev
) {
1118 } endfor_nexthops(fi
)
1119 if (dead
== fi
->fib_nhs
) {
1120 fi
->fib_flags
|= RTNH_F_DEAD
;
1128 /* Must be invoked inside of an RCU protected region. */
1129 void fib_select_default(struct fib_result
*res
)
1131 struct fib_info
*fi
= NULL
, *last_resort
= NULL
;
1132 struct list_head
*fa_head
= res
->fa_head
;
1133 struct fib_table
*tb
= res
->table
;
1134 int order
= -1, last_idx
= -1;
1135 struct fib_alias
*fa
;
1137 list_for_each_entry_rcu(fa
, fa_head
, fa_list
) {
1138 struct fib_info
*next_fi
= fa
->fa_info
;
1140 if (next_fi
->fib_scope
!= res
->scope
||
1141 fa
->fa_type
!= RTN_UNICAST
)
1144 if (next_fi
->fib_priority
> res
->fi
->fib_priority
)
1146 if (!next_fi
->fib_nh
[0].nh_gw
||
1147 next_fi
->fib_nh
[0].nh_scope
!= RT_SCOPE_LINK
)
1150 fib_alias_accessed(fa
);
1153 if (next_fi
!= res
->fi
)
1155 } else if (!fib_detect_death(fi
, order
, &last_resort
,
1156 &last_idx
, tb
->tb_default
)) {
1157 fib_result_assign(res
, fi
);
1158 tb
->tb_default
= order
;
1165 if (order
<= 0 || fi
== NULL
) {
1166 tb
->tb_default
= -1;
1170 if (!fib_detect_death(fi
, order
, &last_resort
, &last_idx
,
1172 fib_result_assign(res
, fi
);
1173 tb
->tb_default
= order
;
1178 fib_result_assign(res
, last_resort
);
1179 tb
->tb_default
= last_idx
;
1184 #ifdef CONFIG_IP_ROUTE_MULTIPATH
1187 * Dead device goes up. We wake up dead nexthops.
1188 * It takes sense only on multipath routes.
1190 int fib_sync_up(struct net_device
*dev
)
1192 struct fib_info
*prev_fi
;
1194 struct hlist_head
*head
;
1195 struct hlist_node
*node
;
1199 if (!(dev
->flags
& IFF_UP
))
1203 hash
= fib_devindex_hashfn(dev
->ifindex
);
1204 head
= &fib_info_devhash
[hash
];
1207 hlist_for_each_entry(nh
, node
, head
, nh_hash
) {
1208 struct fib_info
*fi
= nh
->nh_parent
;
1211 BUG_ON(!fi
->fib_nhs
);
1212 if (nh
->nh_dev
!= dev
|| fi
== prev_fi
)
1217 change_nexthops(fi
) {
1218 if (!(nexthop_nh
->nh_flags
& RTNH_F_DEAD
)) {
1222 if (nexthop_nh
->nh_dev
== NULL
||
1223 !(nexthop_nh
->nh_dev
->flags
& IFF_UP
))
1225 if (nexthop_nh
->nh_dev
!= dev
||
1226 !__in_dev_get_rtnl(dev
))
1229 spin_lock_bh(&fib_multipath_lock
);
1230 nexthop_nh
->nh_power
= 0;
1231 nexthop_nh
->nh_flags
&= ~RTNH_F_DEAD
;
1232 spin_unlock_bh(&fib_multipath_lock
);
1233 } endfor_nexthops(fi
)
1236 fi
->fib_flags
&= ~RTNH_F_DEAD
;
1245 * The algorithm is suboptimal, but it provides really
1246 * fair weighted route distribution.
1248 void fib_select_multipath(struct fib_result
*res
)
1250 struct fib_info
*fi
= res
->fi
;
1253 spin_lock_bh(&fib_multipath_lock
);
1254 if (fi
->fib_power
<= 0) {
1256 change_nexthops(fi
) {
1257 if (!(nexthop_nh
->nh_flags
& RTNH_F_DEAD
)) {
1258 power
+= nexthop_nh
->nh_weight
;
1259 nexthop_nh
->nh_power
= nexthop_nh
->nh_weight
;
1261 } endfor_nexthops(fi
);
1262 fi
->fib_power
= power
;
1264 spin_unlock_bh(&fib_multipath_lock
);
1265 /* Race condition: route has just become dead. */
1272 /* w should be random number [0..fi->fib_power-1],
1273 * it is pretty bad approximation.
1276 w
= jiffies
% fi
->fib_power
;
1278 change_nexthops(fi
) {
1279 if (!(nexthop_nh
->nh_flags
& RTNH_F_DEAD
) &&
1280 nexthop_nh
->nh_power
) {
1281 w
-= nexthop_nh
->nh_power
;
1283 nexthop_nh
->nh_power
--;
1285 res
->nh_sel
= nhsel
;
1286 spin_unlock_bh(&fib_multipath_lock
);
1290 } endfor_nexthops(fi
);
1292 /* Race condition: route has just become dead. */
1294 spin_unlock_bh(&fib_multipath_lock
);