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>
25 #include <linux/sched.h>
27 #include <linux/string.h>
28 #include <linux/socket.h>
29 #include <linux/sockios.h>
30 #include <linux/errno.h>
32 #include <linux/inet.h>
33 #include <linux/inetdevice.h>
34 #include <linux/netdevice.h>
35 #include <linux/if_addr.h>
36 #include <linux/if_arp.h>
37 #include <linux/skbuff.h>
38 #include <linux/netlink.h>
39 #include <linux/init.h>
42 #include <net/protocol.h>
43 #include <net/route.h>
48 #include <net/ip_fib.h>
50 #define FFprint(a...) printk(KERN_DEBUG a)
52 #ifndef CONFIG_IP_MULTIPLE_TABLES
54 #define RT_TABLE_MIN RT_TABLE_MAIN
56 struct fib_table *ip_fib_local_table;
57 struct fib_table *ip_fib_main_table;
61 #define RT_TABLE_MIN 1
63 struct fib_table *fib_tables[RT_TABLE_MAX+1];
65 struct fib_table *__fib_new_table(u32 id)
69 tb = fib_hash_init(id);
77 #endif /* CONFIG_IP_MULTIPLE_TABLES */
80 static void fib_flush(void)
83 #ifdef CONFIG_IP_MULTIPLE_TABLES
87 for (id = RT_TABLE_MAX; id>0; id--) {
88 if ((tb = fib_get_table(id))==NULL)
90 flushed += tb->tb_flush(tb);
92 #else /* CONFIG_IP_MULTIPLE_TABLES */
93 flushed += ip_fib_main_table->tb_flush(ip_fib_main_table);
94 flushed += ip_fib_local_table->tb_flush(ip_fib_local_table);
95 #endif /* CONFIG_IP_MULTIPLE_TABLES */
102 * Find the first device with a given source address.
105 struct net_device * ip_dev_find(u32 addr)
107 struct flowi fl = { .nl_u = { .ip4_u = { .daddr = addr } } };
108 struct fib_result res;
109 struct net_device *dev = NULL;
111 #ifdef CONFIG_IP_MULTIPLE_TABLES
115 if (!ip_fib_local_table ||
116 ip_fib_local_table->tb_lookup(ip_fib_local_table, &fl, &res))
118 if (res.type != RTN_LOCAL)
120 dev = FIB_RES_DEV(res);
129 unsigned inet_addr_type(u32 addr)
131 struct flowi fl = { .nl_u = { .ip4_u = { .daddr = addr } } };
132 struct fib_result res;
133 unsigned ret = RTN_BROADCAST;
135 if (ZERONET(addr) || BADCLASS(addr))
136 return RTN_BROADCAST;
138 return RTN_MULTICAST;
140 #ifdef CONFIG_IP_MULTIPLE_TABLES
144 if (ip_fib_local_table) {
146 if (!ip_fib_local_table->tb_lookup(ip_fib_local_table,
155 /* Given (packet source, input interface) and optional (dst, oif, tos):
156 - (main) check, that source is valid i.e. not broadcast or our local
158 - figure out what "logical" interface this packet arrived
159 and calculate "specific destination" address.
160 - check, that packet arrived from expected physical interface.
163 int fib_validate_source(u32 src, u32 dst, u8 tos, int oif,
164 struct net_device *dev, u32 *spec_dst, u32 *itag)
166 struct in_device *in_dev;
167 struct flowi fl = { .nl_u = { .ip4_u =
172 struct fib_result res;
178 in_dev = __in_dev_get_rcu(dev);
180 no_addr = in_dev->ifa_list == NULL;
181 rpf = IN_DEV_RPFILTER(in_dev);
188 if (fib_lookup(&fl, &res))
190 if (res.type != RTN_UNICAST)
192 *spec_dst = FIB_RES_PREFSRC(res);
193 fib_combine_itag(itag, &res);
194 #ifdef CONFIG_IP_ROUTE_MULTIPATH
195 if (FIB_RES_DEV(res) == dev || res.fi->fib_nhs > 1)
197 if (FIB_RES_DEV(res) == dev)
200 ret = FIB_RES_NH(res).nh_scope >= RT_SCOPE_HOST;
209 fl.oif = dev->ifindex;
212 if (fib_lookup(&fl, &res) == 0) {
213 if (res.type == RTN_UNICAST) {
214 *spec_dst = FIB_RES_PREFSRC(res);
215 ret = FIB_RES_NH(res).nh_scope >= RT_SCOPE_HOST;
224 *spec_dst = inet_select_addr(dev, 0, RT_SCOPE_UNIVERSE);
234 #ifndef CONFIG_IP_NOSIOCRT
237 * Handle IP routing ioctl calls. These are used to manipulate the routing tables
240 int ip_rt_ioctl(unsigned int cmd, void __user *arg)
251 case SIOCADDRT: /* Add a route */
252 case SIOCDELRT: /* Delete a route */
253 if (!capable(CAP_NET_ADMIN))
255 if (copy_from_user(&r, arg, sizeof(struct rtentry)))
258 err = fib_convert_rtentry(cmd, &req.nlh, &req.rtm, &rta, &r);
260 if (cmd == SIOCDELRT) {
261 struct fib_table *tb = fib_get_table(req.rtm.rtm_table);
264 err = tb->tb_delete(tb, &req.rtm, &rta, &req.nlh, NULL);
266 struct fib_table *tb = fib_new_table(req.rtm.rtm_table);
269 err = tb->tb_insert(tb, &req.rtm, &rta, &req.nlh, NULL);
281 int ip_rt_ioctl(unsigned int cmd, void *arg)
288 static int inet_check_attr(struct rtmsg *r, struct rtattr **rta)
292 for (i=1; i<=RTA_MAX; i++, rta++) {
293 struct rtattr *attr = *rta;
295 if (RTA_PAYLOAD(attr) < 4)
297 if (i != RTA_MULTIPATH && i != RTA_METRICS &&
299 *rta = (struct rtattr*)RTA_DATA(attr);
305 int inet_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
307 struct fib_table * tb;
308 struct rtattr **rta = arg;
309 struct rtmsg *r = NLMSG_DATA(nlh);
311 if (inet_check_attr(r, rta))
314 tb = fib_get_table(rtm_get_table(rta, r->rtm_table));
316 return tb->tb_delete(tb, r, (struct kern_rta*)rta, nlh, &NETLINK_CB(skb));
320 int inet_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
322 struct fib_table * tb;
323 struct rtattr **rta = arg;
324 struct rtmsg *r = NLMSG_DATA(nlh);
326 if (inet_check_attr(r, rta))
329 tb = fib_new_table(rtm_get_table(rta, r->rtm_table));
331 return tb->tb_insert(tb, r, (struct kern_rta*)rta, nlh, &NETLINK_CB(skb));
335 int inet_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
339 struct fib_table *tb;
341 if (NLMSG_PAYLOAD(cb->nlh, 0) >= sizeof(struct rtmsg) &&
342 ((struct rtmsg*)NLMSG_DATA(cb->nlh))->rtm_flags&RTM_F_CLONED)
343 return ip_rt_dump(skb, cb);
347 s_t = cb->args[0] = RT_TABLE_MIN;
349 for (t=s_t; t<=RT_TABLE_MAX; t++) {
350 if (t < s_t) continue;
352 memset(&cb->args[1], 0, sizeof(cb->args)-sizeof(cb->args[0]));
353 if ((tb = fib_get_table(t))==NULL)
355 if (tb->tb_dump(tb, skb, cb) < 0)
364 /* Prepare and feed intra-kernel routing request.
365 Really, it should be netlink message, but :-( netlink
366 can be not configured, so that we feed it directly
367 to fib engine. It is legal, because all events occur
368 only when netlink is already locked.
371 static void fib_magic(int cmd, int type, u32 dst, int dst_len, struct in_ifaddr *ifa)
373 struct fib_table * tb;
380 memset(&req.rtm, 0, sizeof(req.rtm));
381 memset(&rta, 0, sizeof(rta));
383 if (type == RTN_UNICAST)
384 tb = fib_new_table(RT_TABLE_MAIN);
386 tb = fib_new_table(RT_TABLE_LOCAL);
391 req.nlh.nlmsg_len = sizeof(req);
392 req.nlh.nlmsg_type = cmd;
393 req.nlh.nlmsg_flags = NLM_F_REQUEST|NLM_F_CREATE|NLM_F_APPEND;
394 req.nlh.nlmsg_pid = 0;
395 req.nlh.nlmsg_seq = 0;
397 req.rtm.rtm_dst_len = dst_len;
398 req.rtm.rtm_table = tb->tb_id;
399 req.rtm.rtm_protocol = RTPROT_KERNEL;
400 req.rtm.rtm_scope = (type != RTN_LOCAL ? RT_SCOPE_LINK : RT_SCOPE_HOST);
401 req.rtm.rtm_type = type;
404 rta.rta_prefsrc = &ifa->ifa_local;
405 rta.rta_oif = &ifa->ifa_dev->dev->ifindex;
407 if (cmd == RTM_NEWROUTE)
408 tb->tb_insert(tb, &req.rtm, &rta, &req.nlh, NULL);
410 tb->tb_delete(tb, &req.rtm, &rta, &req.nlh, NULL);
413 void fib_add_ifaddr(struct in_ifaddr *ifa)
415 struct in_device *in_dev = ifa->ifa_dev;
416 struct net_device *dev = in_dev->dev;
417 struct in_ifaddr *prim = ifa;
418 u32 mask = ifa->ifa_mask;
419 u32 addr = ifa->ifa_local;
420 u32 prefix = ifa->ifa_address&mask;
422 if (ifa->ifa_flags&IFA_F_SECONDARY) {
423 prim = inet_ifa_byprefix(in_dev, prefix, mask);
425 printk(KERN_DEBUG "fib_add_ifaddr: bug: prim == NULL\n");
430 fib_magic(RTM_NEWROUTE, RTN_LOCAL, addr, 32, prim);
432 if (!(dev->flags&IFF_UP))
435 /* Add broadcast address, if it is explicitly assigned. */
436 if (ifa->ifa_broadcast && ifa->ifa_broadcast != 0xFFFFFFFF)
437 fib_magic(RTM_NEWROUTE, RTN_BROADCAST, ifa->ifa_broadcast, 32, prim);
439 if (!ZERONET(prefix) && !(ifa->ifa_flags&IFA_F_SECONDARY) &&
440 (prefix != addr || ifa->ifa_prefixlen < 32)) {
441 fib_magic(RTM_NEWROUTE, dev->flags&IFF_LOOPBACK ? RTN_LOCAL :
442 RTN_UNICAST, prefix, ifa->ifa_prefixlen, prim);
444 /* Add network specific broadcasts, when it takes a sense */
445 if (ifa->ifa_prefixlen < 31) {
446 fib_magic(RTM_NEWROUTE, RTN_BROADCAST, prefix, 32, prim);
447 fib_magic(RTM_NEWROUTE, RTN_BROADCAST, prefix|~mask, 32, prim);
452 static void fib_del_ifaddr(struct in_ifaddr *ifa)
454 struct in_device *in_dev = ifa->ifa_dev;
455 struct net_device *dev = in_dev->dev;
456 struct in_ifaddr *ifa1;
457 struct in_ifaddr *prim = ifa;
458 u32 brd = ifa->ifa_address|~ifa->ifa_mask;
459 u32 any = ifa->ifa_address&ifa->ifa_mask;
466 if (!(ifa->ifa_flags&IFA_F_SECONDARY))
467 fib_magic(RTM_DELROUTE, dev->flags&IFF_LOOPBACK ? RTN_LOCAL :
468 RTN_UNICAST, any, ifa->ifa_prefixlen, prim);
470 prim = inet_ifa_byprefix(in_dev, any, ifa->ifa_mask);
472 printk(KERN_DEBUG "fib_del_ifaddr: bug: prim == NULL\n");
477 /* Deletion is more complicated than add.
478 We should take care of not to delete too much :-)
480 Scan address list to be sure that addresses are really gone.
483 for (ifa1 = in_dev->ifa_list; ifa1; ifa1 = ifa1->ifa_next) {
484 if (ifa->ifa_local == ifa1->ifa_local)
486 if (ifa->ifa_broadcast == ifa1->ifa_broadcast)
488 if (brd == ifa1->ifa_broadcast)
490 if (any == ifa1->ifa_broadcast)
495 fib_magic(RTM_DELROUTE, RTN_BROADCAST, ifa->ifa_broadcast, 32, prim);
497 fib_magic(RTM_DELROUTE, RTN_BROADCAST, brd, 32, prim);
499 fib_magic(RTM_DELROUTE, RTN_BROADCAST, any, 32, prim);
500 if (!(ok&LOCAL_OK)) {
501 fib_magic(RTM_DELROUTE, RTN_LOCAL, ifa->ifa_local, 32, prim);
503 /* Check, that this local address finally disappeared. */
504 if (inet_addr_type(ifa->ifa_local) != RTN_LOCAL) {
505 /* And the last, but not the least thing.
506 We must flush stray FIB entries.
508 First of all, we scan fib_info list searching
509 for stray nexthop entries, then ignite fib_flush.
511 if (fib_sync_down(ifa->ifa_local, NULL, 0))
521 static void nl_fib_lookup(struct fib_result_nl *frn, struct fib_table *tb )
524 struct fib_result res;
525 struct flowi fl = { .nl_u = { .ip4_u = { .daddr = frn->fl_addr,
526 .fwmark = frn->fl_fwmark,
528 .scope = frn->fl_scope } } };
532 frn->tb_id = tb->tb_id;
533 frn->err = tb->tb_lookup(tb, &fl, &res);
536 frn->prefixlen = res.prefixlen;
537 frn->nh_sel = res.nh_sel;
538 frn->type = res.type;
539 frn->scope = res.scope;
545 static void nl_fib_input(struct sock *sk, int len)
547 struct sk_buff *skb = NULL;
548 struct nlmsghdr *nlh = NULL;
549 struct fib_result_nl *frn;
551 struct fib_table *tb;
553 skb = skb_dequeue(&sk->sk_receive_queue);
554 nlh = (struct nlmsghdr *)skb->data;
555 if (skb->len < NLMSG_SPACE(0) || skb->len < nlh->nlmsg_len ||
556 nlh->nlmsg_len < NLMSG_LENGTH(sizeof(*frn))) {
561 frn = (struct fib_result_nl *) NLMSG_DATA(nlh);
562 tb = fib_get_table(frn->tb_id_in);
564 nl_fib_lookup(frn, tb);
566 pid = nlh->nlmsg_pid; /*pid of sending process */
567 NETLINK_CB(skb).pid = 0; /* from kernel */
568 NETLINK_CB(skb).dst_pid = pid;
569 NETLINK_CB(skb).dst_group = 0; /* unicast */
570 netlink_unicast(sk, skb, pid, MSG_DONTWAIT);
573 static void nl_fib_lookup_init(void)
575 netlink_kernel_create(NETLINK_FIB_LOOKUP, 0, nl_fib_input, THIS_MODULE);
578 static void fib_disable_ip(struct net_device *dev, int force)
580 if (fib_sync_down(0, dev, force))
586 static int fib_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
588 struct in_ifaddr *ifa = (struct in_ifaddr*)ptr;
593 #ifdef CONFIG_IP_ROUTE_MULTIPATH
594 fib_sync_up(ifa->ifa_dev->dev);
600 if (ifa->ifa_dev->ifa_list == NULL) {
601 /* Last address was deleted from this interface.
604 fib_disable_ip(ifa->ifa_dev->dev, 1);
613 static int fib_netdev_event(struct notifier_block *this, unsigned long event, void *ptr)
615 struct net_device *dev = ptr;
616 struct in_device *in_dev = __in_dev_get_rtnl(dev);
618 if (event == NETDEV_UNREGISTER) {
619 fib_disable_ip(dev, 2);
630 } endfor_ifa(in_dev);
631 #ifdef CONFIG_IP_ROUTE_MULTIPATH
637 fib_disable_ip(dev, 0);
639 case NETDEV_CHANGEMTU:
647 static struct notifier_block fib_inetaddr_notifier = {
648 .notifier_call =fib_inetaddr_event,
651 static struct notifier_block fib_netdev_notifier = {
652 .notifier_call =fib_netdev_event,
655 void __init ip_fib_init(void)
657 #ifndef CONFIG_IP_MULTIPLE_TABLES
658 ip_fib_local_table = fib_hash_init(RT_TABLE_LOCAL);
659 ip_fib_main_table = fib_hash_init(RT_TABLE_MAIN);
664 register_netdevice_notifier(&fib_netdev_notifier);
665 register_inetaddr_notifier(&fib_inetaddr_notifier);
666 nl_fib_lookup_init();
669 EXPORT_SYMBOL(inet_addr_type);
670 EXPORT_SYMBOL(ip_dev_find);