16 #include <linux/module.h>
17 #include <asm/uaccess.h>
18 #include <linux/bitops.h>
19 #include <linux/capability.h>
20 #include <linux/types.h>
21 #include <linux/kernel.h>
23 #include <linux/string.h>
24 #include <linux/socket.h>
26 #include <linux/errno.h>
30 #include <linux/netdevice.h>
32 #include <linux/if_arp.h>
36 #include <linux/list.h>
37 #include <linux/slab.h>
49 #ifndef CONFIG_IP_MULTIPLE_TABLES
53 struct fib_table *local_table, *main_table;
56 if (local_table ==
NULL)
60 if (main_table ==
NULL)
63 hlist_add_head_rcu(&local_table->
tb_hlist,
65 hlist_add_head_rcu(&main_table->
tb_hlist,
82 tb = fib_get_table(net,
id);
100 net->
ipv4.fib_default =
tb;
108 hlist_add_head_rcu(&tb->
tb_hlist, &net->
ipv4.fib_table_hash[h]);
112 struct fib_table *fib_get_table(
struct net *net,
u32 id)
124 head = &net->
ipv4.fib_table_hash[
h];
125 hlist_for_each_entry_rcu(tb, node, head, tb_hlist) {
126 if (tb->
tb_id ==
id) {
136 static void fib_flush(
struct net *net)
145 head = &net->
ipv4.fib_table_hash[
h];
158 static
inline unsigned int __inet_dev_addr_type(
struct net *net,
167 if (ipv4_is_zeronet(addr) || ipv4_is_lbcast(addr))
169 if (ipv4_is_multicast(addr))
177 if (!dev || dev ==
res.fi->fib_dev)
187 return __inet_dev_addr_type(net,
NULL, addr);
194 return __inet_dev_addr_type(net, dev, addr);
201 struct in_device *in_dev;
208 rt = skb_rtable(skb);
211 return ip_hdr(skb)->daddr;
213 in_dev = __in_dev_get_rcu(dev);
219 if (!ipv4_is_zeronet(ip_hdr(skb)->
saddr)) {
222 fl4.
daddr = ip_hdr(skb)->saddr;
224 fl4.flowi4_tos =
RT_TOS(ip_hdr(skb)->tos);
225 fl4.flowi4_scope =
scope;
226 fl4.flowi4_mark = IN_DEV_SRC_VMARK(in_dev) ? skb->
mark : 0;
227 if (!fib_lookup(net, &fl4, &res))
246 int rpf,
struct in_device *
idev,
u32 *itag)
248 int ret, no_addr, accept_local;
255 fl4.flowi4_iif = oif;
258 fl4.flowi4_tos = tos;
261 no_addr = idev->ifa_list ==
NULL;
263 accept_local = IN_DEV_ACCEPT_LOCAL(idev);
264 fl4.flowi4_mark = IN_DEV_SRC_VMARK(idev) ? skb->
mark : 0;
267 if (fib_lookup(net, &fl4, &
res))
273 fib_combine_itag(itag, &
res);
276 #ifdef CONFIG_IP_ROUTE_MULTIPATH
277 for (ret = 0; ret <
res.fi->fib_nhs; ret++) {
300 if (fib_lookup(net, &fl4, &
res) == 0) {
321 struct in_device *idev,
u32 *itag)
323 int r = secpath_exists(skb) ? 0 : IN_DEV_RPFILTER(idev);
325 if (!r && !fib_num_tclassid_users(dev_net(dev)) &&
326 (dev->
ifindex != oif || !IN_DEV_TX_REDIRECTS(idev))) {
330 return __fib_validate_source(skb, src, dst, tos, oif, dev, r, idev, itag);
335 return ((
struct sockaddr_in *) addr)->sin_addr.s_addr;
342 nla = (
struct nlattr *) ((
char *) mx + len);
344 nla->
nla_len = nla_attr_size(4);
347 return len + nla_total_size(4);
350 static int rtentry_to_fib_config(
struct net *net,
int cmd,
struct rtentry *rt,
356 memset(cfg, 0,
sizeof(*cfg));
371 addr = sk_extract_addr(&rt->
rt_dst);
380 if (bad_mask(mask, addr))
383 plen = inet_mask_len(mask);
415 colon =
strchr(devname,
':');
423 struct in_ifaddr *ifa;
424 struct in_device *in_dev = __in_dev_get_rtnl(dev);
428 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next)
429 if (
strcmp(ifa->ifa_label, devname) == 0)
458 mx = kzalloc(3 * nla_total_size(4),
GFP_KERNEL);
498 err = rtentry_to_fib_config(net, cmd, &rt, &cfg);
503 tb = fib_get_table(net, cfg.
fc_table);
509 tb = fib_new_table(net, cfg.
fc_table);
535 [RTA_FLOW] = { .type =
NLA_U32 },
538 static int rtm_to_fib_config(
struct net *net,
struct sk_buff *skb,
545 err = nlmsg_validate(nlh,
sizeof(*rtm),
RTA_MAX, rtm_ipv4_policy);
549 memset(cfg, 0,
sizeof(*cfg));
551 rtm = nlmsg_data(nlh);
571 switch (nla_type(attr)) {
573 cfg->
fc_dst = nla_get_be32(attr);
576 cfg->
fc_oif = nla_get_u32(attr);
579 cfg->
fc_gw = nla_get_be32(attr);
588 cfg->
fc_mx = nla_data(attr);
592 cfg->
fc_mp = nla_data(attr);
596 cfg->
fc_flow = nla_get_u32(attr);
611 struct net *net = sock_net(skb->
sk);
616 err = rtm_to_fib_config(net, skb, nlh, &cfg);
620 tb = fib_get_table(net, cfg.
fc_table);
631 static int inet_rtm_newroute(
struct sk_buff *skb,
struct nlmsghdr *nlh,
void *arg)
633 struct net *net = sock_net(skb->
sk);
638 err = rtm_to_fib_config(net, skb, nlh, &cfg);
642 tb = fib_new_table(net, cfg.
fc_table);
655 struct net *net = sock_net(skb->
sk);
657 unsigned int e = 0, s_e;
663 if (nlmsg_len(cb->
nlh) >=
sizeof(
struct rtmsg) &&
672 head = &net->
ipv4.fib_table_hash[
h];
678 2 *
sizeof(cb->
args[0]));
699 static void fib_magic(
int cmd,
int type,
__be32 dst,
int dst_len,
struct in_ifaddr *ifa)
701 struct net *net = dev_net(ifa->ifa_dev->dev);
707 .fc_dst_len = dst_len,
708 .fc_prefsrc = ifa->ifa_local,
709 .fc_oif = ifa->ifa_dev->dev->ifindex,
739 struct in_device *in_dev = ifa->ifa_dev;
741 struct in_ifaddr *
prim = ifa;
742 __be32 mask = ifa->ifa_mask;
743 __be32 addr = ifa->ifa_local;
749 pr_warn(
"%s: bug: prim == NULL\n", __func__);
760 if (ifa->ifa_broadcast && ifa->ifa_broadcast !=
htonl(0xFFFFFFFF))
764 (prefix != addr || ifa->ifa_prefixlen < 32)) {
767 prefix, ifa->ifa_prefixlen, prim);
770 if (ifa->ifa_prefixlen < 31) {
785 struct in_device *in_dev = ifa->ifa_dev;
787 struct in_ifaddr *ifa1;
788 struct in_ifaddr *
prim = ifa, *prim1 =
NULL;
789 __be32 brd = ifa->ifa_address | ~ifa->ifa_mask;
790 __be32 any = ifa->ifa_address & ifa->ifa_mask;
798 int same_prefsrc = 0;
803 pr_warn(
"%s: bug: prim == NULL\n", __func__);
806 if (iprim && iprim != prim) {
807 pr_warn(
"%s: bug: iprim != prim\n", __func__);
810 }
else if (!ipv4_is_zeronet(any) &&
811 (any != ifa->ifa_local || ifa->ifa_prefixlen < 32)) {
814 any, ifa->ifa_prefixlen, prim);
824 for (ifa1 = in_dev->ifa_list; ifa1; ifa1 = ifa1->ifa_next) {
831 if (iprim && ifa1->ifa_mask == iprim->ifa_mask &&
832 inet_ifa_match(ifa1->ifa_address, iprim))
838 if (ifa1->ifa_mask == prim->ifa_mask &&
839 inet_ifa_match(ifa1->ifa_address, prim))
851 ifa1->ifa_mask != prim1->ifa_mask ||
852 !inet_ifa_match(ifa1->ifa_address, prim1))
858 if (prim1->ifa_local != prim->ifa_local)
862 if (prim->ifa_local != ifa1->ifa_local)
868 if (ifa->ifa_local == ifa1->ifa_local)
870 if (ifa->ifa_broadcast == ifa1->ifa_broadcast)
872 if (brd == ifa1->ifa_broadcast)
874 if (any == ifa1->ifa_broadcast)
877 if (prim1 == ifa1 && ifa1->ifa_prefixlen < 31) {
878 __be32 brd1 = ifa1->ifa_address | ~ifa1->ifa_mask;
879 __be32 any1 = ifa1->ifa_address & ifa1->ifa_mask;
881 if (!ipv4_is_zeronet(any1)) {
882 if (ifa->ifa_broadcast == brd1 ||
883 ifa->ifa_broadcast == any1)
885 if (brd == brd1 || brd == any1)
887 if (any == brd1 || any == any1)
895 if (subnet && ifa->ifa_prefixlen < 31) {
914 fib_flush(dev_net(dev));
930 .flowi4_tos = frn->
fl_tos,
953 static void nl_fib_input(
struct sk_buff *skb)
961 net = sock_net(skb->
sk);
962 nlh = nlmsg_hdr(skb);
970 nlh = nlmsg_hdr(skb);
973 tb = fib_get_table(net, frn->
tb_id_in);
975 nl_fib_lookup(frn, tb);
983 static int __net_init nl_fib_lookup_init(
struct net *net)
987 .input = nl_fib_input,
997 static void nl_fib_lookup_exit(
struct net *net)
1006 fib_flush(dev_net(dev));
1013 struct in_ifaddr *ifa = (
struct in_ifaddr *)ptr;
1015 struct net *net = dev_net(dev);
1020 #ifdef CONFIG_IP_ROUTE_MULTIPATH
1029 if (ifa->ifa_dev->ifa_list ==
NULL) {
1033 fib_disable_ip(dev, 1);
1042 static int fib_netdev_event(
struct notifier_block *
this,
unsigned long event,
void *ptr)
1045 struct in_device *in_dev;
1046 struct net *net = dev_net(dev);
1049 fib_disable_ip(dev, 2);
1054 in_dev = __in_dev_get_rtnl(dev);
1060 } endfor_ifa(in_dev);
1061 #ifdef CONFIG_IP_ROUTE_MULTIPATH
1068 fib_disable_ip(dev, 0);
1079 .notifier_call = fib_inetaddr_event,
1083 .notifier_call = fib_netdev_event,
1086 static int __net_init ip_fib_net_init(
struct net *net)
1095 if (net->
ipv4.fib_table_hash ==
NULL)
1108 static void ip_fib_net_exit(
struct net *net)
1112 #ifdef CONFIG_IP_MULTIPLE_TABLES
1122 head = &net->
ipv4.fib_table_hash[
i];
1133 static int __net_init fib_net_init(
struct net *net)
1137 #ifdef CONFIG_IP_ROUTE_CLASSID
1138 net->
ipv4.fib_num_tclassid_users = 0;
1140 error = ip_fib_net_init(net);
1143 error = nl_fib_lookup_init(net);
1146 error = fib_proc_init(net);
1153 nl_fib_lookup_exit(net);
1155 ip_fib_net_exit(net);
1159 static void __net_exit fib_net_exit(
struct net *net)
1162 nl_fib_lookup_exit(net);
1163 ip_fib_net_exit(net);
1167 .init = fib_net_init,
1168 .exit = fib_net_exit,