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: FIB frontend.
8 * Version: $Id: fib_frontend.c,v 1.26 2001/10/31 21:55:54 davem Exp $
10 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
18 #include <linux/module.h>
19 #include <asm/uaccess.h>
20 #include <asm/system.h>
21 #include <linux/bitops.h>
22 #include <linux/capability.h>
23 #include <linux/types.h>
24 #include <linux/kernel.h>
26 #include <linux/string.h>
27 #include <linux/socket.h>
28 #include <linux/sockios.h>
29 #include <linux/errno.h>
31 #include <linux/inet.h>
32 #include <linux/inetdevice.h>
33 #include <linux/netdevice.h>
34 #include <linux/if_addr.h>
35 #include <linux/if_arp.h>
36 #include <linux/skbuff.h>
37 #include <linux/init.h>
38 #include <linux/list.h>
41 #include <net/protocol.h>
42 #include <net/route.h>
47 #include <net/ip_fib.h>
48 #include <net/rtnetlink.h>
50 #ifndef CONFIG_IP_MULTIPLE_TABLES
52 static int __net_init fib4_rules_init(struct net *net)
54 struct fib_table *local_table, *main_table;
56 local_table = fib_hash_init(RT_TABLE_LOCAL);
57 if (local_table == NULL)
60 main_table = fib_hash_init(RT_TABLE_MAIN);
61 if (main_table == NULL)
64 hlist_add_head_rcu(&local_table->tb_hlist,
65 &net->ipv4.fib_table_hash[TABLE_LOCAL_INDEX]);
66 hlist_add_head_rcu(&main_table->tb_hlist,
67 &net->ipv4.fib_table_hash[TABLE_MAIN_INDEX]);
76 struct fib_table *fib_new_table(struct net *net, u32 id)
83 tb = fib_get_table(net, id);
86 tb = fib_hash_init(id);
89 h = id & (FIB_TABLE_HASHSZ - 1);
90 hlist_add_head_rcu(&tb->tb_hlist, &net->ipv4.fib_table_hash[h]);
94 struct fib_table *fib_get_table(struct net *net, u32 id)
97 struct hlist_node *node;
98 struct hlist_head *head;
103 h = id & (FIB_TABLE_HASHSZ - 1);
106 head = &net->ipv4.fib_table_hash[h];
107 hlist_for_each_entry_rcu(tb, node, head, tb_hlist) {
108 if (tb->tb_id == id) {
116 #endif /* CONFIG_IP_MULTIPLE_TABLES */
118 static void fib_flush(struct net *net)
121 struct fib_table *tb;
122 struct hlist_node *node;
123 struct hlist_head *head;
126 for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
127 head = &net->ipv4.fib_table_hash[h];
128 hlist_for_each_entry(tb, node, head, tb_hlist)
129 flushed += tb->tb_flush(tb);
137 * Find the first device with a given source address.
140 struct net_device * ip_dev_find(__be32 addr)
142 struct flowi fl = { .nl_u = { .ip4_u = { .daddr = addr } } };
143 struct fib_result res;
144 struct net_device *dev = NULL;
145 struct fib_table *local_table;
147 #ifdef CONFIG_IP_MULTIPLE_TABLES
151 local_table = fib_get_table(&init_net, RT_TABLE_LOCAL);
152 if (!local_table || local_table->tb_lookup(local_table, &fl, &res))
154 if (res.type != RTN_LOCAL)
156 dev = FIB_RES_DEV(res);
166 * Find address type as if only "dev" was present in the system. If
167 * on_dev is NULL then all interfaces are taken into consideration.
169 static inline unsigned __inet_dev_addr_type(struct net *net,
170 const struct net_device *dev,
173 struct flowi fl = { .nl_u = { .ip4_u = { .daddr = addr } } };
174 struct fib_result res;
175 unsigned ret = RTN_BROADCAST;
176 struct fib_table *local_table;
178 if (ipv4_is_zeronet(addr) || ipv4_is_badclass(addr))
179 return RTN_BROADCAST;
180 if (ipv4_is_multicast(addr))
181 return RTN_MULTICAST;
183 #ifdef CONFIG_IP_MULTIPLE_TABLES
187 local_table = fib_get_table(net, RT_TABLE_LOCAL);
190 if (!local_table->tb_lookup(local_table, &fl, &res)) {
191 if (!dev || dev == res.fi->fib_dev)
199 unsigned int inet_addr_type(struct net *net, __be32 addr)
201 return __inet_dev_addr_type(net, NULL, addr);
204 unsigned int inet_dev_addr_type(struct net *net, const struct net_device *dev,
207 return __inet_dev_addr_type(net, dev, addr);
210 /* Given (packet source, input interface) and optional (dst, oif, tos):
211 - (main) check, that source is valid i.e. not broadcast or our local
213 - figure out what "logical" interface this packet arrived
214 and calculate "specific destination" address.
215 - check, that packet arrived from expected physical interface.
218 int fib_validate_source(__be32 src, __be32 dst, u8 tos, int oif,
219 struct net_device *dev, __be32 *spec_dst, u32 *itag)
221 struct in_device *in_dev;
222 struct flowi fl = { .nl_u = { .ip4_u =
227 struct fib_result res;
233 in_dev = __in_dev_get_rcu(dev);
235 no_addr = in_dev->ifa_list == NULL;
236 rpf = IN_DEV_RPFILTER(in_dev);
243 if (fib_lookup(&fl, &res))
245 if (res.type != RTN_UNICAST)
247 *spec_dst = FIB_RES_PREFSRC(res);
248 fib_combine_itag(itag, &res);
249 #ifdef CONFIG_IP_ROUTE_MULTIPATH
250 if (FIB_RES_DEV(res) == dev || res.fi->fib_nhs > 1)
252 if (FIB_RES_DEV(res) == dev)
255 ret = FIB_RES_NH(res).nh_scope >= RT_SCOPE_HOST;
264 fl.oif = dev->ifindex;
267 if (fib_lookup(&fl, &res) == 0) {
268 if (res.type == RTN_UNICAST) {
269 *spec_dst = FIB_RES_PREFSRC(res);
270 ret = FIB_RES_NH(res).nh_scope >= RT_SCOPE_HOST;
279 *spec_dst = inet_select_addr(dev, 0, RT_SCOPE_UNIVERSE);
289 static inline __be32 sk_extract_addr(struct sockaddr *addr)
291 return ((struct sockaddr_in *) addr)->sin_addr.s_addr;
294 static int put_rtax(struct nlattr *mx, int len, int type, u32 value)
298 nla = (struct nlattr *) ((char *) mx + len);
299 nla->nla_type = type;
300 nla->nla_len = nla_attr_size(4);
301 *(u32 *) nla_data(nla) = value;
303 return len + nla_total_size(4);
306 static int rtentry_to_fib_config(struct net *net, int cmd, struct rtentry *rt,
307 struct fib_config *cfg)
312 memset(cfg, 0, sizeof(*cfg));
313 cfg->fc_nlinfo.nl_net = net;
315 if (rt->rt_dst.sa_family != AF_INET)
316 return -EAFNOSUPPORT;
319 * Check mask for validity:
320 * a) it must be contiguous.
321 * b) destination must have all host bits clear.
322 * c) if application forgot to set correct family (AF_INET),
323 * reject request unless it is absolutely clear i.e.
324 * both family and mask are zero.
327 addr = sk_extract_addr(&rt->rt_dst);
328 if (!(rt->rt_flags & RTF_HOST)) {
329 __be32 mask = sk_extract_addr(&rt->rt_genmask);
331 if (rt->rt_genmask.sa_family != AF_INET) {
332 if (mask || rt->rt_genmask.sa_family)
333 return -EAFNOSUPPORT;
336 if (bad_mask(mask, addr))
339 plen = inet_mask_len(mask);
342 cfg->fc_dst_len = plen;
345 if (cmd != SIOCDELRT) {
346 cfg->fc_nlflags = NLM_F_CREATE;
347 cfg->fc_protocol = RTPROT_BOOT;
351 cfg->fc_priority = rt->rt_metric - 1;
353 if (rt->rt_flags & RTF_REJECT) {
354 cfg->fc_scope = RT_SCOPE_HOST;
355 cfg->fc_type = RTN_UNREACHABLE;
359 cfg->fc_scope = RT_SCOPE_NOWHERE;
360 cfg->fc_type = RTN_UNICAST;
364 struct net_device *dev;
365 char devname[IFNAMSIZ];
367 if (copy_from_user(devname, rt->rt_dev, IFNAMSIZ-1))
370 devname[IFNAMSIZ-1] = 0;
371 colon = strchr(devname, ':');
374 dev = __dev_get_by_name(net, devname);
377 cfg->fc_oif = dev->ifindex;
379 struct in_ifaddr *ifa;
380 struct in_device *in_dev = __in_dev_get_rtnl(dev);
384 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next)
385 if (strcmp(ifa->ifa_label, devname) == 0)
389 cfg->fc_prefsrc = ifa->ifa_local;
393 addr = sk_extract_addr(&rt->rt_gateway);
394 if (rt->rt_gateway.sa_family == AF_INET && addr) {
396 if (rt->rt_flags & RTF_GATEWAY &&
397 inet_addr_type(net, addr) == RTN_UNICAST)
398 cfg->fc_scope = RT_SCOPE_UNIVERSE;
401 if (cmd == SIOCDELRT)
404 if (rt->rt_flags & RTF_GATEWAY && !cfg->fc_gw)
407 if (cfg->fc_scope == RT_SCOPE_NOWHERE)
408 cfg->fc_scope = RT_SCOPE_LINK;
410 if (rt->rt_flags & (RTF_MTU | RTF_WINDOW | RTF_IRTT)) {
414 mx = kzalloc(3 * nla_total_size(4), GFP_KERNEL);
418 if (rt->rt_flags & RTF_MTU)
419 len = put_rtax(mx, len, RTAX_ADVMSS, rt->rt_mtu - 40);
421 if (rt->rt_flags & RTF_WINDOW)
422 len = put_rtax(mx, len, RTAX_WINDOW, rt->rt_window);
424 if (rt->rt_flags & RTF_IRTT)
425 len = put_rtax(mx, len, RTAX_RTT, rt->rt_irtt << 3);
428 cfg->fc_mx_len = len;
435 * Handle IP routing ioctl calls. These are used to manipulate the routing tables
438 int ip_rt_ioctl(struct net *net, unsigned int cmd, void __user *arg)
440 struct fib_config cfg;
445 case SIOCADDRT: /* Add a route */
446 case SIOCDELRT: /* Delete a route */
447 if (!capable(CAP_NET_ADMIN))
450 if (copy_from_user(&rt, arg, sizeof(rt)))
454 err = rtentry_to_fib_config(net, cmd, &rt, &cfg);
456 struct fib_table *tb;
458 if (cmd == SIOCDELRT) {
459 tb = fib_get_table(net, cfg.fc_table);
461 err = tb->tb_delete(tb, &cfg);
465 tb = fib_new_table(net, cfg.fc_table);
467 err = tb->tb_insert(tb, &cfg);
472 /* allocated by rtentry_to_fib_config() */
481 const struct nla_policy rtm_ipv4_policy[RTA_MAX+1] = {
482 [RTA_DST] = { .type = NLA_U32 },
483 [RTA_SRC] = { .type = NLA_U32 },
484 [RTA_IIF] = { .type = NLA_U32 },
485 [RTA_OIF] = { .type = NLA_U32 },
486 [RTA_GATEWAY] = { .type = NLA_U32 },
487 [RTA_PRIORITY] = { .type = NLA_U32 },
488 [RTA_PREFSRC] = { .type = NLA_U32 },
489 [RTA_METRICS] = { .type = NLA_NESTED },
490 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
491 [RTA_PROTOINFO] = { .type = NLA_U32 },
492 [RTA_FLOW] = { .type = NLA_U32 },
495 static int rtm_to_fib_config(struct net *net, struct sk_buff *skb,
496 struct nlmsghdr *nlh, struct fib_config *cfg)
502 err = nlmsg_validate(nlh, sizeof(*rtm), RTA_MAX, rtm_ipv4_policy);
506 memset(cfg, 0, sizeof(*cfg));
508 rtm = nlmsg_data(nlh);
509 cfg->fc_dst_len = rtm->rtm_dst_len;
510 cfg->fc_tos = rtm->rtm_tos;
511 cfg->fc_table = rtm->rtm_table;
512 cfg->fc_protocol = rtm->rtm_protocol;
513 cfg->fc_scope = rtm->rtm_scope;
514 cfg->fc_type = rtm->rtm_type;
515 cfg->fc_flags = rtm->rtm_flags;
516 cfg->fc_nlflags = nlh->nlmsg_flags;
518 cfg->fc_nlinfo.pid = NETLINK_CB(skb).pid;
519 cfg->fc_nlinfo.nlh = nlh;
520 cfg->fc_nlinfo.nl_net = net;
522 if (cfg->fc_type > RTN_MAX) {
527 nlmsg_for_each_attr(attr, nlh, sizeof(struct rtmsg), remaining) {
528 switch (nla_type(attr)) {
530 cfg->fc_dst = nla_get_be32(attr);
533 cfg->fc_oif = nla_get_u32(attr);
536 cfg->fc_gw = nla_get_be32(attr);
539 cfg->fc_priority = nla_get_u32(attr);
542 cfg->fc_prefsrc = nla_get_be32(attr);
545 cfg->fc_mx = nla_data(attr);
546 cfg->fc_mx_len = nla_len(attr);
549 cfg->fc_mp = nla_data(attr);
550 cfg->fc_mp_len = nla_len(attr);
553 cfg->fc_flow = nla_get_u32(attr);
556 cfg->fc_table = nla_get_u32(attr);
566 static int inet_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
568 struct net *net = skb->sk->sk_net;
569 struct fib_config cfg;
570 struct fib_table *tb;
573 err = rtm_to_fib_config(net, skb, nlh, &cfg);
577 tb = fib_get_table(net, cfg.fc_table);
583 err = tb->tb_delete(tb, &cfg);
588 static int inet_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
590 struct net *net = skb->sk->sk_net;
591 struct fib_config cfg;
592 struct fib_table *tb;
595 err = rtm_to_fib_config(net, skb, nlh, &cfg);
599 tb = fib_new_table(net, cfg.fc_table);
605 err = tb->tb_insert(tb, &cfg);
610 static int inet_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
612 struct net *net = skb->sk->sk_net;
614 unsigned int e = 0, s_e;
615 struct fib_table *tb;
616 struct hlist_node *node;
617 struct hlist_head *head;
620 if (nlmsg_len(cb->nlh) >= sizeof(struct rtmsg) &&
621 ((struct rtmsg *) nlmsg_data(cb->nlh))->rtm_flags & RTM_F_CLONED)
622 return ip_rt_dump(skb, cb);
627 for (h = s_h; h < FIB_TABLE_HASHSZ; h++, s_e = 0) {
629 head = &net->ipv4.fib_table_hash[h];
630 hlist_for_each_entry(tb, node, head, tb_hlist) {
634 memset(&cb->args[2], 0, sizeof(cb->args) -
635 2 * sizeof(cb->args[0]));
636 if (tb->tb_dump(tb, skb, cb) < 0)
650 /* Prepare and feed intra-kernel routing request.
651 Really, it should be netlink message, but :-( netlink
652 can be not configured, so that we feed it directly
653 to fib engine. It is legal, because all events occur
654 only when netlink is already locked.
657 static void fib_magic(int cmd, int type, __be32 dst, int dst_len, struct in_ifaddr *ifa)
659 struct net *net = ifa->ifa_dev->dev->nd_net;
660 struct fib_table *tb;
661 struct fib_config cfg = {
662 .fc_protocol = RTPROT_KERNEL,
665 .fc_dst_len = dst_len,
666 .fc_prefsrc = ifa->ifa_local,
667 .fc_oif = ifa->ifa_dev->dev->ifindex,
668 .fc_nlflags = NLM_F_CREATE | NLM_F_APPEND,
674 if (type == RTN_UNICAST)
675 tb = fib_new_table(net, RT_TABLE_MAIN);
677 tb = fib_new_table(net, RT_TABLE_LOCAL);
682 cfg.fc_table = tb->tb_id;
684 if (type != RTN_LOCAL)
685 cfg.fc_scope = RT_SCOPE_LINK;
687 cfg.fc_scope = RT_SCOPE_HOST;
689 if (cmd == RTM_NEWROUTE)
690 tb->tb_insert(tb, &cfg);
692 tb->tb_delete(tb, &cfg);
695 void fib_add_ifaddr(struct in_ifaddr *ifa)
697 struct in_device *in_dev = ifa->ifa_dev;
698 struct net_device *dev = in_dev->dev;
699 struct in_ifaddr *prim = ifa;
700 __be32 mask = ifa->ifa_mask;
701 __be32 addr = ifa->ifa_local;
702 __be32 prefix = ifa->ifa_address&mask;
704 if (ifa->ifa_flags&IFA_F_SECONDARY) {
705 prim = inet_ifa_byprefix(in_dev, prefix, mask);
707 printk(KERN_WARNING "fib_add_ifaddr: bug: prim == NULL\n");
712 fib_magic(RTM_NEWROUTE, RTN_LOCAL, addr, 32, prim);
714 if (!(dev->flags&IFF_UP))
717 /* Add broadcast address, if it is explicitly assigned. */
718 if (ifa->ifa_broadcast && ifa->ifa_broadcast != htonl(0xFFFFFFFF))
719 fib_magic(RTM_NEWROUTE, RTN_BROADCAST, ifa->ifa_broadcast, 32, prim);
721 if (!ipv4_is_zeronet(prefix) && !(ifa->ifa_flags&IFA_F_SECONDARY) &&
722 (prefix != addr || ifa->ifa_prefixlen < 32)) {
723 fib_magic(RTM_NEWROUTE, dev->flags&IFF_LOOPBACK ? RTN_LOCAL :
724 RTN_UNICAST, prefix, ifa->ifa_prefixlen, prim);
726 /* Add network specific broadcasts, when it takes a sense */
727 if (ifa->ifa_prefixlen < 31) {
728 fib_magic(RTM_NEWROUTE, RTN_BROADCAST, prefix, 32, prim);
729 fib_magic(RTM_NEWROUTE, RTN_BROADCAST, prefix|~mask, 32, prim);
734 static void fib_del_ifaddr(struct in_ifaddr *ifa)
736 struct in_device *in_dev = ifa->ifa_dev;
737 struct net_device *dev = in_dev->dev;
738 struct in_ifaddr *ifa1;
739 struct in_ifaddr *prim = ifa;
740 __be32 brd = ifa->ifa_address|~ifa->ifa_mask;
741 __be32 any = ifa->ifa_address&ifa->ifa_mask;
748 if (!(ifa->ifa_flags&IFA_F_SECONDARY))
749 fib_magic(RTM_DELROUTE, dev->flags&IFF_LOOPBACK ? RTN_LOCAL :
750 RTN_UNICAST, any, ifa->ifa_prefixlen, prim);
752 prim = inet_ifa_byprefix(in_dev, any, ifa->ifa_mask);
754 printk(KERN_WARNING "fib_del_ifaddr: bug: prim == NULL\n");
759 /* Deletion is more complicated than add.
760 We should take care of not to delete too much :-)
762 Scan address list to be sure that addresses are really gone.
765 for (ifa1 = in_dev->ifa_list; ifa1; ifa1 = ifa1->ifa_next) {
766 if (ifa->ifa_local == ifa1->ifa_local)
768 if (ifa->ifa_broadcast == ifa1->ifa_broadcast)
770 if (brd == ifa1->ifa_broadcast)
772 if (any == ifa1->ifa_broadcast)
777 fib_magic(RTM_DELROUTE, RTN_BROADCAST, ifa->ifa_broadcast, 32, prim);
779 fib_magic(RTM_DELROUTE, RTN_BROADCAST, brd, 32, prim);
781 fib_magic(RTM_DELROUTE, RTN_BROADCAST, any, 32, prim);
782 if (!(ok&LOCAL_OK)) {
783 fib_magic(RTM_DELROUTE, RTN_LOCAL, ifa->ifa_local, 32, prim);
785 /* Check, that this local address finally disappeared. */
786 if (inet_addr_type(dev->nd_net, ifa->ifa_local) != RTN_LOCAL) {
787 /* And the last, but not the least thing.
788 We must flush stray FIB entries.
790 First of all, we scan fib_info list searching
791 for stray nexthop entries, then ignite fib_flush.
793 if (fib_sync_down(ifa->ifa_local, NULL, 0))
794 fib_flush(dev->nd_net);
803 static void nl_fib_lookup(struct fib_result_nl *frn, struct fib_table *tb )
806 struct fib_result res;
807 struct flowi fl = { .mark = frn->fl_mark,
808 .nl_u = { .ip4_u = { .daddr = frn->fl_addr,
810 .scope = frn->fl_scope } } };
812 #ifdef CONFIG_IP_MULTIPLE_TABLES
820 frn->tb_id = tb->tb_id;
821 frn->err = tb->tb_lookup(tb, &fl, &res);
824 frn->prefixlen = res.prefixlen;
825 frn->nh_sel = res.nh_sel;
826 frn->type = res.type;
827 frn->scope = res.scope;
834 static void nl_fib_input(struct sk_buff *skb)
837 struct fib_result_nl *frn;
838 struct nlmsghdr *nlh;
839 struct fib_table *tb;
842 net = skb->sk->sk_net;
843 nlh = nlmsg_hdr(skb);
844 if (skb->len < NLMSG_SPACE(0) || skb->len < nlh->nlmsg_len ||
845 nlh->nlmsg_len < NLMSG_LENGTH(sizeof(*frn)))
848 skb = skb_clone(skb, GFP_KERNEL);
851 nlh = nlmsg_hdr(skb);
853 frn = (struct fib_result_nl *) NLMSG_DATA(nlh);
854 tb = fib_get_table(net, frn->tb_id_in);
856 nl_fib_lookup(frn, tb);
858 pid = NETLINK_CB(skb).pid; /* pid of sending process */
859 NETLINK_CB(skb).pid = 0; /* from kernel */
860 NETLINK_CB(skb).dst_group = 0; /* unicast */
861 netlink_unicast(net->ipv4.fibnl, skb, pid, MSG_DONTWAIT);
864 static int nl_fib_lookup_init(struct net *net)
867 sk = netlink_kernel_create(net, NETLINK_FIB_LOOKUP, 0,
868 nl_fib_input, NULL, THIS_MODULE);
870 return -EAFNOSUPPORT;
871 /* Don't hold an extra reference on the namespace */
873 net->ipv4.fibnl = sk;
877 static void nl_fib_lookup_exit(struct net *net)
879 /* At the last minute lie and say this is a socket for the
880 * initial network namespace. So the socket will be safe to free.
882 net->ipv4.fibnl->sk_net = get_net(&init_net);
883 sock_put(net->ipv4.fibnl);
886 static void fib_disable_ip(struct net_device *dev, int force)
888 if (fib_sync_down(0, dev, force))
889 fib_flush(dev->nd_net);
894 static int fib_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
896 struct in_ifaddr *ifa = (struct in_ifaddr*)ptr;
901 #ifdef CONFIG_IP_ROUTE_MULTIPATH
902 fib_sync_up(ifa->ifa_dev->dev);
908 if (ifa->ifa_dev->ifa_list == NULL) {
909 /* Last address was deleted from this interface.
912 fib_disable_ip(ifa->ifa_dev->dev, 1);
921 static int fib_netdev_event(struct notifier_block *this, unsigned long event, void *ptr)
923 struct net_device *dev = ptr;
924 struct in_device *in_dev = __in_dev_get_rtnl(dev);
926 if (event == NETDEV_UNREGISTER) {
927 fib_disable_ip(dev, 2);
938 } endfor_ifa(in_dev);
939 #ifdef CONFIG_IP_ROUTE_MULTIPATH
945 fib_disable_ip(dev, 0);
947 case NETDEV_CHANGEMTU:
955 static struct notifier_block fib_inetaddr_notifier = {
956 .notifier_call =fib_inetaddr_event,
959 static struct notifier_block fib_netdev_notifier = {
960 .notifier_call =fib_netdev_event,
963 static int __net_init ip_fib_net_init(struct net *net)
967 net->ipv4.fib_table_hash = kzalloc(
968 sizeof(struct hlist_head)*FIB_TABLE_HASHSZ, GFP_KERNEL);
969 if (net->ipv4.fib_table_hash == NULL)
972 for (i = 0; i < FIB_TABLE_HASHSZ; i++)
973 INIT_HLIST_HEAD(&net->ipv4.fib_table_hash[i]);
975 return fib4_rules_init(net);
978 static void __net_exit ip_fib_net_exit(struct net *net)
982 #ifdef CONFIG_IP_MULTIPLE_TABLES
983 fib4_rules_exit(net);
986 for (i = 0; i < FIB_TABLE_HASHSZ; i++) {
987 struct fib_table *tb;
988 struct hlist_head *head;
989 struct hlist_node *node, *tmp;
991 head = &net->ipv4.fib_table_hash[i];
992 hlist_for_each_entry_safe(tb, node, tmp, head, tb_hlist) {
998 kfree(net->ipv4.fib_table_hash);
1001 static int __net_init fib_net_init(struct net *net)
1005 error = ip_fib_net_init(net);
1008 error = nl_fib_lookup_init(net);
1011 error = fib_proc_init(net);
1018 nl_fib_lookup_exit(net);
1020 ip_fib_net_exit(net);
1024 static void __net_exit fib_net_exit(struct net *net)
1027 nl_fib_lookup_exit(net);
1028 ip_fib_net_exit(net);
1031 static struct pernet_operations fib_net_ops = {
1032 .init = fib_net_init,
1033 .exit = fib_net_exit,
1036 void __init ip_fib_init(void)
1038 rtnl_register(PF_INET, RTM_NEWROUTE, inet_rtm_newroute, NULL);
1039 rtnl_register(PF_INET, RTM_DELROUTE, inet_rtm_delroute, NULL);
1040 rtnl_register(PF_INET, RTM_GETROUTE, NULL, inet_dump_fib);
1042 register_pernet_subsys(&fib_net_ops);
1043 register_netdevice_notifier(&fib_netdev_notifier);
1044 register_inetaddr_notifier(&fib_inetaddr_notifier);
1047 EXPORT_SYMBOL(inet_addr_type);
1048 EXPORT_SYMBOL(inet_dev_addr_type);
1049 EXPORT_SYMBOL(ip_dev_find);