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 #define FFprint(a...) printk(KERN_DEBUG a)
52 #ifndef CONFIG_IP_MULTIPLE_TABLES
54 struct fib_table *ip_fib_local_table;
55 struct fib_table *ip_fib_main_table;
57 #define FIB_TABLE_HASHSZ 1
58 static struct hlist_head fib_table_hash[FIB_TABLE_HASHSZ];
62 #define FIB_TABLE_HASHSZ 256
63 static struct hlist_head fib_table_hash[FIB_TABLE_HASHSZ];
65 static struct sock *fibnl = NULL;
68 struct fib_table *fib_new_table(u32 id)
75 tb = fib_get_table(id);
78 tb = fib_hash_init(id);
81 h = id & (FIB_TABLE_HASHSZ - 1);
82 hlist_add_head_rcu(&tb->tb_hlist, &fib_table_hash[h]);
86 struct fib_table *fib_get_table(u32 id)
89 struct hlist_node *node;
94 h = id & (FIB_TABLE_HASHSZ - 1);
96 hlist_for_each_entry_rcu(tb, node, &fib_table_hash[h], tb_hlist) {
97 if (tb->tb_id == id) {
105 #endif /* CONFIG_IP_MULTIPLE_TABLES */
107 static void fib_flush(void)
110 struct fib_table *tb;
111 struct hlist_node *node;
114 for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
115 hlist_for_each_entry(tb, node, &fib_table_hash[h], tb_hlist)
116 flushed += tb->tb_flush(tb);
124 * Find the first device with a given source address.
127 struct net_device * ip_dev_find(__be32 addr)
129 struct flowi fl = { .nl_u = { .ip4_u = { .daddr = addr } } };
130 struct fib_result res;
131 struct net_device *dev = NULL;
133 #ifdef CONFIG_IP_MULTIPLE_TABLES
137 if (!ip_fib_local_table ||
138 ip_fib_local_table->tb_lookup(ip_fib_local_table, &fl, &res))
140 if (res.type != RTN_LOCAL)
142 dev = FIB_RES_DEV(res);
151 unsigned inet_addr_type(__be32 addr)
153 struct flowi fl = { .nl_u = { .ip4_u = { .daddr = addr } } };
154 struct fib_result res;
155 unsigned ret = RTN_BROADCAST;
157 if (ZERONET(addr) || BADCLASS(addr))
158 return RTN_BROADCAST;
160 return RTN_MULTICAST;
162 #ifdef CONFIG_IP_MULTIPLE_TABLES
166 if (ip_fib_local_table) {
168 if (!ip_fib_local_table->tb_lookup(ip_fib_local_table,
177 /* Given (packet source, input interface) and optional (dst, oif, tos):
178 - (main) check, that source is valid i.e. not broadcast or our local
180 - figure out what "logical" interface this packet arrived
181 and calculate "specific destination" address.
182 - check, that packet arrived from expected physical interface.
185 int fib_validate_source(__be32 src, __be32 dst, u8 tos, int oif,
186 struct net_device *dev, __be32 *spec_dst, u32 *itag)
188 struct in_device *in_dev;
189 struct flowi fl = { .nl_u = { .ip4_u =
194 struct fib_result res;
200 in_dev = __in_dev_get_rcu(dev);
202 no_addr = in_dev->ifa_list == NULL;
203 rpf = IN_DEV_RPFILTER(in_dev);
210 if (fib_lookup(&fl, &res))
212 if (res.type != RTN_UNICAST)
214 *spec_dst = FIB_RES_PREFSRC(res);
215 fib_combine_itag(itag, &res);
216 #ifdef CONFIG_IP_ROUTE_MULTIPATH
217 if (FIB_RES_DEV(res) == dev || res.fi->fib_nhs > 1)
219 if (FIB_RES_DEV(res) == dev)
222 ret = FIB_RES_NH(res).nh_scope >= RT_SCOPE_HOST;
231 fl.oif = dev->ifindex;
234 if (fib_lookup(&fl, &res) == 0) {
235 if (res.type == RTN_UNICAST) {
236 *spec_dst = FIB_RES_PREFSRC(res);
237 ret = FIB_RES_NH(res).nh_scope >= RT_SCOPE_HOST;
246 *spec_dst = inet_select_addr(dev, 0, RT_SCOPE_UNIVERSE);
256 static inline __be32 sk_extract_addr(struct sockaddr *addr)
258 return ((struct sockaddr_in *) addr)->sin_addr.s_addr;
261 static int put_rtax(struct nlattr *mx, int len, int type, u32 value)
265 nla = (struct nlattr *) ((char *) mx + len);
266 nla->nla_type = type;
267 nla->nla_len = nla_attr_size(4);
268 *(u32 *) nla_data(nla) = value;
270 return len + nla_total_size(4);
273 static int rtentry_to_fib_config(int cmd, struct rtentry *rt,
274 struct fib_config *cfg)
279 memset(cfg, 0, sizeof(*cfg));
281 if (rt->rt_dst.sa_family != AF_INET)
282 return -EAFNOSUPPORT;
285 * Check mask for validity:
286 * a) it must be contiguous.
287 * b) destination must have all host bits clear.
288 * c) if application forgot to set correct family (AF_INET),
289 * reject request unless it is absolutely clear i.e.
290 * both family and mask are zero.
293 addr = sk_extract_addr(&rt->rt_dst);
294 if (!(rt->rt_flags & RTF_HOST)) {
295 __be32 mask = sk_extract_addr(&rt->rt_genmask);
297 if (rt->rt_genmask.sa_family != AF_INET) {
298 if (mask || rt->rt_genmask.sa_family)
299 return -EAFNOSUPPORT;
302 if (bad_mask(mask, addr))
305 plen = inet_mask_len(mask);
308 cfg->fc_dst_len = plen;
311 if (cmd != SIOCDELRT) {
312 cfg->fc_nlflags = NLM_F_CREATE;
313 cfg->fc_protocol = RTPROT_BOOT;
317 cfg->fc_priority = rt->rt_metric - 1;
319 if (rt->rt_flags & RTF_REJECT) {
320 cfg->fc_scope = RT_SCOPE_HOST;
321 cfg->fc_type = RTN_UNREACHABLE;
325 cfg->fc_scope = RT_SCOPE_NOWHERE;
326 cfg->fc_type = RTN_UNICAST;
330 struct net_device *dev;
331 char devname[IFNAMSIZ];
333 if (copy_from_user(devname, rt->rt_dev, IFNAMSIZ-1))
336 devname[IFNAMSIZ-1] = 0;
337 colon = strchr(devname, ':');
340 dev = __dev_get_by_name(&init_net, devname);
343 cfg->fc_oif = dev->ifindex;
345 struct in_ifaddr *ifa;
346 struct in_device *in_dev = __in_dev_get_rtnl(dev);
350 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next)
351 if (strcmp(ifa->ifa_label, devname) == 0)
355 cfg->fc_prefsrc = ifa->ifa_local;
359 addr = sk_extract_addr(&rt->rt_gateway);
360 if (rt->rt_gateway.sa_family == AF_INET && addr) {
362 if (rt->rt_flags & RTF_GATEWAY &&
363 inet_addr_type(addr) == RTN_UNICAST)
364 cfg->fc_scope = RT_SCOPE_UNIVERSE;
367 if (cmd == SIOCDELRT)
370 if (rt->rt_flags & RTF_GATEWAY && !cfg->fc_gw)
373 if (cfg->fc_scope == RT_SCOPE_NOWHERE)
374 cfg->fc_scope = RT_SCOPE_LINK;
376 if (rt->rt_flags & (RTF_MTU | RTF_WINDOW | RTF_IRTT)) {
380 mx = kzalloc(3 * nla_total_size(4), GFP_KERNEL);
384 if (rt->rt_flags & RTF_MTU)
385 len = put_rtax(mx, len, RTAX_ADVMSS, rt->rt_mtu - 40);
387 if (rt->rt_flags & RTF_WINDOW)
388 len = put_rtax(mx, len, RTAX_WINDOW, rt->rt_window);
390 if (rt->rt_flags & RTF_IRTT)
391 len = put_rtax(mx, len, RTAX_RTT, rt->rt_irtt << 3);
394 cfg->fc_mx_len = len;
401 * Handle IP routing ioctl calls. These are used to manipulate the routing tables
404 int ip_rt_ioctl(unsigned int cmd, void __user *arg)
406 struct fib_config cfg;
411 case SIOCADDRT: /* Add a route */
412 case SIOCDELRT: /* Delete a route */
413 if (!capable(CAP_NET_ADMIN))
416 if (copy_from_user(&rt, arg, sizeof(rt)))
420 err = rtentry_to_fib_config(cmd, &rt, &cfg);
422 struct fib_table *tb;
424 if (cmd == SIOCDELRT) {
425 tb = fib_get_table(cfg.fc_table);
427 err = tb->tb_delete(tb, &cfg);
431 tb = fib_new_table(cfg.fc_table);
433 err = tb->tb_insert(tb, &cfg);
438 /* allocated by rtentry_to_fib_config() */
447 const struct nla_policy rtm_ipv4_policy[RTA_MAX+1] = {
448 [RTA_DST] = { .type = NLA_U32 },
449 [RTA_SRC] = { .type = NLA_U32 },
450 [RTA_IIF] = { .type = NLA_U32 },
451 [RTA_OIF] = { .type = NLA_U32 },
452 [RTA_GATEWAY] = { .type = NLA_U32 },
453 [RTA_PRIORITY] = { .type = NLA_U32 },
454 [RTA_PREFSRC] = { .type = NLA_U32 },
455 [RTA_METRICS] = { .type = NLA_NESTED },
456 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
457 [RTA_PROTOINFO] = { .type = NLA_U32 },
458 [RTA_FLOW] = { .type = NLA_U32 },
461 static int rtm_to_fib_config(struct sk_buff *skb, struct nlmsghdr *nlh,
462 struct fib_config *cfg)
468 err = nlmsg_validate(nlh, sizeof(*rtm), RTA_MAX, rtm_ipv4_policy);
472 memset(cfg, 0, sizeof(*cfg));
474 rtm = nlmsg_data(nlh);
475 cfg->fc_dst_len = rtm->rtm_dst_len;
476 cfg->fc_tos = rtm->rtm_tos;
477 cfg->fc_table = rtm->rtm_table;
478 cfg->fc_protocol = rtm->rtm_protocol;
479 cfg->fc_scope = rtm->rtm_scope;
480 cfg->fc_type = rtm->rtm_type;
481 cfg->fc_flags = rtm->rtm_flags;
482 cfg->fc_nlflags = nlh->nlmsg_flags;
484 cfg->fc_nlinfo.pid = NETLINK_CB(skb).pid;
485 cfg->fc_nlinfo.nlh = nlh;
487 if (cfg->fc_type > RTN_MAX) {
492 nlmsg_for_each_attr(attr, nlh, sizeof(struct rtmsg), remaining) {
493 switch (nla_type(attr)) {
495 cfg->fc_dst = nla_get_be32(attr);
498 cfg->fc_oif = nla_get_u32(attr);
501 cfg->fc_gw = nla_get_be32(attr);
504 cfg->fc_priority = nla_get_u32(attr);
507 cfg->fc_prefsrc = nla_get_be32(attr);
510 cfg->fc_mx = nla_data(attr);
511 cfg->fc_mx_len = nla_len(attr);
514 cfg->fc_mp = nla_data(attr);
515 cfg->fc_mp_len = nla_len(attr);
518 cfg->fc_flow = nla_get_u32(attr);
521 cfg->fc_table = nla_get_u32(attr);
531 static int inet_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
533 struct fib_config cfg;
534 struct fib_table *tb;
537 err = rtm_to_fib_config(skb, nlh, &cfg);
541 tb = fib_get_table(cfg.fc_table);
547 err = tb->tb_delete(tb, &cfg);
552 static int inet_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
554 struct fib_config cfg;
555 struct fib_table *tb;
558 err = rtm_to_fib_config(skb, nlh, &cfg);
562 tb = fib_new_table(cfg.fc_table);
568 err = tb->tb_insert(tb, &cfg);
573 static int inet_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
576 unsigned int e = 0, s_e;
577 struct fib_table *tb;
578 struct hlist_node *node;
581 if (nlmsg_len(cb->nlh) >= sizeof(struct rtmsg) &&
582 ((struct rtmsg *) nlmsg_data(cb->nlh))->rtm_flags & RTM_F_CLONED)
583 return ip_rt_dump(skb, cb);
588 for (h = s_h; h < FIB_TABLE_HASHSZ; h++, s_e = 0) {
590 hlist_for_each_entry(tb, node, &fib_table_hash[h], tb_hlist) {
594 memset(&cb->args[2], 0, sizeof(cb->args) -
595 2 * sizeof(cb->args[0]));
596 if (tb->tb_dump(tb, skb, cb) < 0)
610 /* Prepare and feed intra-kernel routing request.
611 Really, it should be netlink message, but :-( netlink
612 can be not configured, so that we feed it directly
613 to fib engine. It is legal, because all events occur
614 only when netlink is already locked.
617 static void fib_magic(int cmd, int type, __be32 dst, int dst_len, struct in_ifaddr *ifa)
619 struct fib_table *tb;
620 struct fib_config cfg = {
621 .fc_protocol = RTPROT_KERNEL,
624 .fc_dst_len = dst_len,
625 .fc_prefsrc = ifa->ifa_local,
626 .fc_oif = ifa->ifa_dev->dev->ifindex,
627 .fc_nlflags = NLM_F_CREATE | NLM_F_APPEND,
630 if (type == RTN_UNICAST)
631 tb = fib_new_table(RT_TABLE_MAIN);
633 tb = fib_new_table(RT_TABLE_LOCAL);
638 cfg.fc_table = tb->tb_id;
640 if (type != RTN_LOCAL)
641 cfg.fc_scope = RT_SCOPE_LINK;
643 cfg.fc_scope = RT_SCOPE_HOST;
645 if (cmd == RTM_NEWROUTE)
646 tb->tb_insert(tb, &cfg);
648 tb->tb_delete(tb, &cfg);
651 void fib_add_ifaddr(struct in_ifaddr *ifa)
653 struct in_device *in_dev = ifa->ifa_dev;
654 struct net_device *dev = in_dev->dev;
655 struct in_ifaddr *prim = ifa;
656 __be32 mask = ifa->ifa_mask;
657 __be32 addr = ifa->ifa_local;
658 __be32 prefix = ifa->ifa_address&mask;
660 if (ifa->ifa_flags&IFA_F_SECONDARY) {
661 prim = inet_ifa_byprefix(in_dev, prefix, mask);
663 printk(KERN_DEBUG "fib_add_ifaddr: bug: prim == NULL\n");
668 fib_magic(RTM_NEWROUTE, RTN_LOCAL, addr, 32, prim);
670 if (!(dev->flags&IFF_UP))
673 /* Add broadcast address, if it is explicitly assigned. */
674 if (ifa->ifa_broadcast && ifa->ifa_broadcast != htonl(0xFFFFFFFF))
675 fib_magic(RTM_NEWROUTE, RTN_BROADCAST, ifa->ifa_broadcast, 32, prim);
677 if (!ZERONET(prefix) && !(ifa->ifa_flags&IFA_F_SECONDARY) &&
678 (prefix != addr || ifa->ifa_prefixlen < 32)) {
679 fib_magic(RTM_NEWROUTE, dev->flags&IFF_LOOPBACK ? RTN_LOCAL :
680 RTN_UNICAST, prefix, ifa->ifa_prefixlen, prim);
682 /* Add network specific broadcasts, when it takes a sense */
683 if (ifa->ifa_prefixlen < 31) {
684 fib_magic(RTM_NEWROUTE, RTN_BROADCAST, prefix, 32, prim);
685 fib_magic(RTM_NEWROUTE, RTN_BROADCAST, prefix|~mask, 32, prim);
690 static void fib_del_ifaddr(struct in_ifaddr *ifa)
692 struct in_device *in_dev = ifa->ifa_dev;
693 struct net_device *dev = in_dev->dev;
694 struct in_ifaddr *ifa1;
695 struct in_ifaddr *prim = ifa;
696 __be32 brd = ifa->ifa_address|~ifa->ifa_mask;
697 __be32 any = ifa->ifa_address&ifa->ifa_mask;
704 if (!(ifa->ifa_flags&IFA_F_SECONDARY))
705 fib_magic(RTM_DELROUTE, dev->flags&IFF_LOOPBACK ? RTN_LOCAL :
706 RTN_UNICAST, any, ifa->ifa_prefixlen, prim);
708 prim = inet_ifa_byprefix(in_dev, any, ifa->ifa_mask);
710 printk(KERN_DEBUG "fib_del_ifaddr: bug: prim == NULL\n");
715 /* Deletion is more complicated than add.
716 We should take care of not to delete too much :-)
718 Scan address list to be sure that addresses are really gone.
721 for (ifa1 = in_dev->ifa_list; ifa1; ifa1 = ifa1->ifa_next) {
722 if (ifa->ifa_local == ifa1->ifa_local)
724 if (ifa->ifa_broadcast == ifa1->ifa_broadcast)
726 if (brd == ifa1->ifa_broadcast)
728 if (any == ifa1->ifa_broadcast)
733 fib_magic(RTM_DELROUTE, RTN_BROADCAST, ifa->ifa_broadcast, 32, prim);
735 fib_magic(RTM_DELROUTE, RTN_BROADCAST, brd, 32, prim);
737 fib_magic(RTM_DELROUTE, RTN_BROADCAST, any, 32, prim);
738 if (!(ok&LOCAL_OK)) {
739 fib_magic(RTM_DELROUTE, RTN_LOCAL, ifa->ifa_local, 32, prim);
741 /* Check, that this local address finally disappeared. */
742 if (inet_addr_type(ifa->ifa_local) != RTN_LOCAL) {
743 /* And the last, but not the least thing.
744 We must flush stray FIB entries.
746 First of all, we scan fib_info list searching
747 for stray nexthop entries, then ignite fib_flush.
749 if (fib_sync_down(ifa->ifa_local, NULL, 0))
759 static void nl_fib_lookup(struct fib_result_nl *frn, struct fib_table *tb )
762 struct fib_result res;
763 struct flowi fl = { .mark = frn->fl_mark,
764 .nl_u = { .ip4_u = { .daddr = frn->fl_addr,
766 .scope = frn->fl_scope } } };
768 #ifdef CONFIG_IP_MULTIPLE_TABLES
776 frn->tb_id = tb->tb_id;
777 frn->err = tb->tb_lookup(tb, &fl, &res);
780 frn->prefixlen = res.prefixlen;
781 frn->nh_sel = res.nh_sel;
782 frn->type = res.type;
783 frn->scope = res.scope;
790 static void nl_fib_input(struct sock *sk, int len)
792 struct sk_buff *skb = NULL;
793 struct nlmsghdr *nlh = NULL;
794 struct fib_result_nl *frn;
796 struct fib_table *tb;
798 skb = skb_dequeue(&sk->sk_receive_queue);
802 nlh = nlmsg_hdr(skb);
803 if (skb->len < NLMSG_SPACE(0) || skb->len < nlh->nlmsg_len ||
804 nlh->nlmsg_len < NLMSG_LENGTH(sizeof(*frn))) {
809 frn = (struct fib_result_nl *) NLMSG_DATA(nlh);
810 tb = fib_get_table(frn->tb_id_in);
812 nl_fib_lookup(frn, tb);
814 pid = NETLINK_CB(skb).pid; /* pid of sending process */
815 NETLINK_CB(skb).pid = 0; /* from kernel */
816 NETLINK_CB(skb).dst_group = 0; /* unicast */
817 netlink_unicast(fibnl, skb, pid, MSG_DONTWAIT);
820 static void nl_fib_lookup_init(void)
822 fibnl = netlink_kernel_create(&init_net, NETLINK_FIB_LOOKUP, 0,
823 nl_fib_input, NULL, THIS_MODULE);
826 static void fib_disable_ip(struct net_device *dev, int force)
828 if (fib_sync_down(0, dev, force))
834 static int fib_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
836 struct in_ifaddr *ifa = (struct in_ifaddr*)ptr;
841 #ifdef CONFIG_IP_ROUTE_MULTIPATH
842 fib_sync_up(ifa->ifa_dev->dev);
848 if (ifa->ifa_dev->ifa_list == NULL) {
849 /* Last address was deleted from this interface.
852 fib_disable_ip(ifa->ifa_dev->dev, 1);
861 static int fib_netdev_event(struct notifier_block *this, unsigned long event, void *ptr)
863 struct net_device *dev = ptr;
864 struct in_device *in_dev = __in_dev_get_rtnl(dev);
866 if (dev->nd_net != &init_net)
869 if (event == NETDEV_UNREGISTER) {
870 fib_disable_ip(dev, 2);
881 } endfor_ifa(in_dev);
882 #ifdef CONFIG_IP_ROUTE_MULTIPATH
888 fib_disable_ip(dev, 0);
890 case NETDEV_CHANGEMTU:
898 static struct notifier_block fib_inetaddr_notifier = {
899 .notifier_call =fib_inetaddr_event,
902 static struct notifier_block fib_netdev_notifier = {
903 .notifier_call =fib_netdev_event,
906 void __init ip_fib_init(void)
910 for (i = 0; i < FIB_TABLE_HASHSZ; i++)
911 INIT_HLIST_HEAD(&fib_table_hash[i]);
912 #ifndef CONFIG_IP_MULTIPLE_TABLES
913 ip_fib_local_table = fib_hash_init(RT_TABLE_LOCAL);
914 hlist_add_head_rcu(&ip_fib_local_table->tb_hlist, &fib_table_hash[0]);
915 ip_fib_main_table = fib_hash_init(RT_TABLE_MAIN);
916 hlist_add_head_rcu(&ip_fib_main_table->tb_hlist, &fib_table_hash[0]);
921 register_netdevice_notifier(&fib_netdev_notifier);
922 register_inetaddr_notifier(&fib_inetaddr_notifier);
923 nl_fib_lookup_init();
925 rtnl_register(PF_INET, RTM_NEWROUTE, inet_rtm_newroute, NULL);
926 rtnl_register(PF_INET, RTM_DELROUTE, inet_rtm_delroute, NULL);
927 rtnl_register(PF_INET, RTM_GETROUTE, NULL, inet_dump_fib);
930 EXPORT_SYMBOL(inet_addr_type);
931 EXPORT_SYMBOL(ip_dev_find);