📄 fib_semantics.c
字号:
/* * INET An implementation of the TCP/IP protocol suite for the LINUX * operating system. INET is implemented using the BSD Socket * interface as the means of communication with the user level. * * IPv4 Forwarding Information Base: semantics. * * Version: $Id: fib_semantics.c,v 1.19 2002/01/12 07:54:56 davem Exp $ * * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version * 2 of the License, or (at your option) any later version. */#include <asm/uaccess.h>#include <asm/system.h>#include <linux/bitops.h>#include <linux/types.h>#include <linux/kernel.h>#include <linux/jiffies.h>#include <linux/mm.h>#include <linux/string.h>#include <linux/socket.h>#include <linux/sockios.h>#include <linux/errno.h>#include <linux/in.h>#include <linux/inet.h>#include <linux/inetdevice.h>#include <linux/netdevice.h>#include <linux/if_arp.h>#include <linux/proc_fs.h>#include <linux/skbuff.h>#include <linux/init.h>#include <net/arp.h>#include <net/ip.h>#include <net/protocol.h>#include <net/route.h>#include <net/tcp.h>#include <net/sock.h>#include <net/ip_fib.h>#include <net/netlink.h>#include <net/nexthop.h>#include "fib_lookup.h"#define FSprintk(a...)static DEFINE_SPINLOCK(fib_info_lock);static struct hlist_head *fib_info_hash;static struct hlist_head *fib_info_laddrhash;static unsigned int fib_hash_size;static unsigned int fib_info_cnt;#define DEVINDEX_HASHBITS 8#define DEVINDEX_HASHSIZE (1U << DEVINDEX_HASHBITS)static struct hlist_head fib_info_devhash[DEVINDEX_HASHSIZE];#ifdef CONFIG_IP_ROUTE_MULTIPATHstatic DEFINE_SPINLOCK(fib_multipath_lock);#define for_nexthops(fi) { int nhsel; const struct fib_nh * nh; \for (nhsel=0, nh = (fi)->fib_nh; nhsel < (fi)->fib_nhs; nh++, nhsel++)#define change_nexthops(fi) { int nhsel; struct fib_nh * nh; \for (nhsel=0, nh = (struct fib_nh*)((fi)->fib_nh); nhsel < (fi)->fib_nhs; nh++, nhsel++)#else /* CONFIG_IP_ROUTE_MULTIPATH *//* Hope, that gcc will optimize it to get rid of dummy loop */#define for_nexthops(fi) { int nhsel=0; const struct fib_nh * nh = (fi)->fib_nh; \for (nhsel=0; nhsel < 1; nhsel++)#define change_nexthops(fi) { int nhsel=0; struct fib_nh * nh = (struct fib_nh*)((fi)->fib_nh); \for (nhsel=0; nhsel < 1; nhsel++)#endif /* CONFIG_IP_ROUTE_MULTIPATH */#define endfor_nexthops(fi) }static const struct{ int error; u8 scope;} fib_props[RTN_MAX + 1] = { { .error = 0, .scope = RT_SCOPE_NOWHERE, }, /* RTN_UNSPEC */ { .error = 0, .scope = RT_SCOPE_UNIVERSE, }, /* RTN_UNICAST */ { .error = 0, .scope = RT_SCOPE_HOST, }, /* RTN_LOCAL */ { .error = 0, .scope = RT_SCOPE_LINK, }, /* RTN_BROADCAST */ { .error = 0, .scope = RT_SCOPE_LINK, }, /* RTN_ANYCAST */ { .error = 0, .scope = RT_SCOPE_UNIVERSE, }, /* RTN_MULTICAST */ { .error = -EINVAL, .scope = RT_SCOPE_UNIVERSE, }, /* RTN_BLACKHOLE */ { .error = -EHOSTUNREACH, .scope = RT_SCOPE_UNIVERSE, }, /* RTN_UNREACHABLE */ { .error = -EACCES, .scope = RT_SCOPE_UNIVERSE, }, /* RTN_PROHIBIT */ { .error = -EAGAIN, .scope = RT_SCOPE_UNIVERSE, }, /* RTN_THROW */ { .error = -EINVAL, .scope = RT_SCOPE_NOWHERE, }, /* RTN_NAT */ { .error = -EINVAL, .scope = RT_SCOPE_NOWHERE, }, /* RTN_XRESOLVE */};/* Release a nexthop info record */void free_fib_info(struct fib_info *fi){ if (fi->fib_dead == 0) { printk("Freeing alive fib_info %p\n", fi); return; } change_nexthops(fi) { if (nh->nh_dev) dev_put(nh->nh_dev); nh->nh_dev = NULL; } endfor_nexthops(fi); fib_info_cnt--; kfree(fi);}void fib_release_info(struct fib_info *fi){ spin_lock_bh(&fib_info_lock); if (fi && --fi->fib_treeref == 0) { hlist_del(&fi->fib_hash); if (fi->fib_prefsrc) hlist_del(&fi->fib_lhash); change_nexthops(fi) { if (!nh->nh_dev) continue; hlist_del(&nh->nh_hash); } endfor_nexthops(fi) fi->fib_dead = 1; fib_info_put(fi); } spin_unlock_bh(&fib_info_lock);}static __inline__ int nh_comp(const struct fib_info *fi, const struct fib_info *ofi){ const struct fib_nh *onh = ofi->fib_nh; for_nexthops(fi) { if (nh->nh_oif != onh->nh_oif || nh->nh_gw != onh->nh_gw || nh->nh_scope != onh->nh_scope ||#ifdef CONFIG_IP_ROUTE_MULTIPATH nh->nh_weight != onh->nh_weight ||#endif#ifdef CONFIG_NET_CLS_ROUTE nh->nh_tclassid != onh->nh_tclassid ||#endif ((nh->nh_flags^onh->nh_flags)&~RTNH_F_DEAD)) return -1; onh++; } endfor_nexthops(fi); return 0;}static inline unsigned int fib_info_hashfn(const struct fib_info *fi){ unsigned int mask = (fib_hash_size - 1); unsigned int val = fi->fib_nhs; val ^= fi->fib_protocol; val ^= (__force u32)fi->fib_prefsrc; val ^= fi->fib_priority; return (val ^ (val >> 7) ^ (val >> 12)) & mask;}static struct fib_info *fib_find_info(const struct fib_info *nfi){ struct hlist_head *head; struct hlist_node *node; struct fib_info *fi; unsigned int hash; hash = fib_info_hashfn(nfi); head = &fib_info_hash[hash]; hlist_for_each_entry(fi, node, head, fib_hash) { if (fi->fib_nhs != nfi->fib_nhs) continue; if (nfi->fib_protocol == fi->fib_protocol && nfi->fib_prefsrc == fi->fib_prefsrc && nfi->fib_priority == fi->fib_priority && memcmp(nfi->fib_metrics, fi->fib_metrics, sizeof(fi->fib_metrics)) == 0 && ((nfi->fib_flags^fi->fib_flags)&~RTNH_F_DEAD) == 0 && (nfi->fib_nhs == 0 || nh_comp(fi, nfi) == 0)) return fi; } return NULL;}static inline unsigned int fib_devindex_hashfn(unsigned int val){ unsigned int mask = DEVINDEX_HASHSIZE - 1; return (val ^ (val >> DEVINDEX_HASHBITS) ^ (val >> (DEVINDEX_HASHBITS * 2))) & mask;}/* Check, that the gateway is already configured. Used only by redirect accept routine. */int ip_fib_check_default(__be32 gw, struct net_device *dev){ struct hlist_head *head; struct hlist_node *node; struct fib_nh *nh; unsigned int hash; spin_lock(&fib_info_lock); hash = fib_devindex_hashfn(dev->ifindex); head = &fib_info_devhash[hash]; hlist_for_each_entry(nh, node, head, nh_hash) { if (nh->nh_dev == dev && nh->nh_gw == gw && !(nh->nh_flags&RTNH_F_DEAD)) { spin_unlock(&fib_info_lock); return 0; } } spin_unlock(&fib_info_lock); return -1;}static inline size_t fib_nlmsg_size(struct fib_info *fi){ size_t payload = NLMSG_ALIGN(sizeof(struct rtmsg)) + nla_total_size(4) /* RTA_TABLE */ + nla_total_size(4) /* RTA_DST */ + nla_total_size(4) /* RTA_PRIORITY */ + nla_total_size(4); /* RTA_PREFSRC */ /* space for nested metrics */ payload += nla_total_size((RTAX_MAX * nla_total_size(4))); if (fi->fib_nhs) { /* Also handles the special case fib_nhs == 1 */ /* each nexthop is packed in an attribute */ size_t nhsize = nla_total_size(sizeof(struct rtnexthop)); /* may contain flow and gateway attribute */ nhsize += 2 * nla_total_size(4); /* all nexthops are packed in a nested attribute */ payload += nla_total_size(fi->fib_nhs * nhsize); } return payload;}void rtmsg_fib(int event, __be32 key, struct fib_alias *fa, int dst_len, u32 tb_id, struct nl_info *info, unsigned int nlm_flags){ struct sk_buff *skb; u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0; int err = -ENOBUFS; skb = nlmsg_new(fib_nlmsg_size(fa->fa_info), GFP_KERNEL); if (skb == NULL) goto errout; err = fib_dump_info(skb, info->pid, seq, event, tb_id, fa->fa_type, fa->fa_scope, key, dst_len, fa->fa_tos, fa->fa_info, nlm_flags); if (err < 0) { /* -EMSGSIZE implies BUG in fib_nlmsg_size() */ WARN_ON(err == -EMSGSIZE); kfree_skb(skb); goto errout; } err = rtnl_notify(skb, info->pid, RTNLGRP_IPV4_ROUTE, info->nlh, GFP_KERNEL);errout: if (err < 0) rtnl_set_sk_err(RTNLGRP_IPV4_ROUTE, err);}/* Return the first fib alias matching TOS with * priority less than or equal to PRIO. */struct fib_alias *fib_find_alias(struct list_head *fah, u8 tos, u32 prio){ if (fah) { struct fib_alias *fa; list_for_each_entry(fa, fah, fa_list) { if (fa->fa_tos > tos) continue; if (fa->fa_info->fib_priority >= prio || fa->fa_tos < tos) return fa; } } return NULL;}int fib_detect_death(struct fib_info *fi, int order, struct fib_info **last_resort, int *last_idx, int *dflt){ struct neighbour *n; int state = NUD_NONE; n = neigh_lookup(&arp_tbl, &fi->fib_nh[0].nh_gw, fi->fib_dev); if (n) { state = n->nud_state; neigh_release(n); } if (state==NUD_REACHABLE) return 0; if ((state&NUD_VALID) && order != *dflt) return 0; if ((state&NUD_VALID) || (*last_idx<0 && order > *dflt)) { *last_resort = fi; *last_idx = order; } return 1;}#ifdef CONFIG_IP_ROUTE_MULTIPATHstatic int fib_count_nexthops(struct rtnexthop *rtnh, int remaining){ int nhs = 0; while (rtnh_ok(rtnh, remaining)) { nhs++; rtnh = rtnh_next(rtnh, &remaining); } /* leftover implies invalid nexthop configuration, discard it */ return remaining > 0 ? 0 : nhs;}static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh, int remaining, struct fib_config *cfg){ change_nexthops(fi) { int attrlen; if (!rtnh_ok(rtnh, remaining)) return -EINVAL; nh->nh_flags = (cfg->fc_flags & ~0xFF) | rtnh->rtnh_flags; nh->nh_oif = rtnh->rtnh_ifindex; nh->nh_weight = rtnh->rtnh_hops + 1; attrlen = rtnh_attrlen(rtnh); if (attrlen > 0) { struct nlattr *nla, *attrs = rtnh_attrs(rtnh); nla = nla_find(attrs, attrlen, RTA_GATEWAY); nh->nh_gw = nla ? nla_get_be32(nla) : 0;#ifdef CONFIG_NET_CLS_ROUTE nla = nla_find(attrs, attrlen, RTA_FLOW); nh->nh_tclassid = nla ? nla_get_u32(nla) : 0;#endif } rtnh = rtnh_next(rtnh, &remaining); } endfor_nexthops(fi); return 0;}#endifint fib_nh_match(struct fib_config *cfg, struct fib_info *fi){#ifdef CONFIG_IP_ROUTE_MULTIPATH struct rtnexthop *rtnh; int remaining;#endif if (cfg->fc_priority && cfg->fc_priority != fi->fib_priority) return 1; if (cfg->fc_oif || cfg->fc_gw) { if ((!cfg->fc_oif || cfg->fc_oif == fi->fib_nh->nh_oif) && (!cfg->fc_gw || cfg->fc_gw == fi->fib_nh->nh_gw)) return 0; return 1; }#ifdef CONFIG_IP_ROUTE_MULTIPATH if (cfg->fc_mp == NULL) return 0; rtnh = cfg->fc_mp; remaining = cfg->fc_mp_len; for_nexthops(fi) { int attrlen; if (!rtnh_ok(rtnh, remaining)) return -EINVAL; if (rtnh->rtnh_ifindex && rtnh->rtnh_ifindex != nh->nh_oif) return 1; attrlen = rtnh_attrlen(rtnh); if (attrlen < 0) { struct nlattr *nla, *attrs = rtnh_attrs(rtnh); nla = nla_find(attrs, attrlen, RTA_GATEWAY); if (nla && nla_get_be32(nla) != nh->nh_gw) return 1;#ifdef CONFIG_NET_CLS_ROUTE nla = nla_find(attrs, attrlen, RTA_FLOW); if (nla && nla_get_u32(nla) != nh->nh_tclassid) return 1;#endif } rtnh = rtnh_next(rtnh, &remaining); } endfor_nexthops(fi);#endif return 0;}/* Picture ------- Semantics of nexthop is very messy by historical reasons. We have to take into account, that: a) gateway can be actually local interface address, so that gatewayed route is direct. b) gateway must be on-link address, possibly described not by an ifaddr, but also by a direct route. c) If both gateway and interface are specified, they should not contradict. d) If we use tunnel routes, gateway could be not on-link. Attempt to reconcile all of these (alas, self-contradictory) conditions results in pretty ugly and hairy code with obscure logic. I chose to generalized it instead, so that the size of code does not increase practically, but it becomes much more general. Every prefix is assigned a "scope" value: "host" is local address, "link" is direct route, [ ... "site" ... "interior" ... ] and "universe" is true gateway route with global meaning. Every prefix refers to a set of "nexthop"s (gw, oif), where gw must have narrower scope. This recursion stops when gw has LOCAL scope or if "nexthop" is declared ONLINK, which means that gw is forced to be on link. Code is still hairy, but now it is apparently logically consistent and very flexible. F.e. as by-product it allows to co-exists in peace independent exterior and interior routing processes. Normally it looks as following. {universe prefix} -> (gw, oif) [scope link] | |-> {link prefix} -> (gw, oif) [scope local] | |-> {local prefix} (terminal node) */static int fib_check_nh(struct fib_config *cfg, struct fib_info *fi, struct fib_nh *nh){ int err; if (nh->nh_gw) { struct fib_result res;#ifdef CONFIG_IP_ROUTE_PERVASIVE if (nh->nh_flags&RTNH_F_PERVASIVE) return 0;#endif if (nh->nh_flags&RTNH_F_ONLINK) { struct net_device *dev; if (cfg->fc_scope >= RT_SCOPE_LINK) return -EINVAL; if (inet_addr_type(nh->nh_gw) != RTN_UNICAST) return -EINVAL; if ((dev = __dev_get_by_index(&init_net, nh->nh_oif)) == NULL) return -ENODEV; if (!(dev->flags&IFF_UP)) return -ENETDOWN; nh->nh_dev = dev; dev_hold(dev); nh->nh_scope = RT_SCOPE_LINK; return 0; } { struct flowi fl = { .nl_u = { .ip4_u = { .daddr = nh->nh_gw, .scope = cfg->fc_scope + 1, }, }, .oif = nh->nh_oif, }; /* It is not necessary, but requires a bit of thinking */ if (fl.fl4_scope < RT_SCOPE_LINK) fl.fl4_scope = RT_SCOPE_LINK; if ((err = fib_lookup(&fl, &res)) != 0) return err; } err = -EINVAL; if (res.type != RTN_UNICAST && res.type != RTN_LOCAL) goto out; nh->nh_scope = res.scope; nh->nh_oif = FIB_RES_OIF(res); if ((nh->nh_dev = FIB_RES_DEV(res)) == NULL) goto out; dev_hold(nh->nh_dev); err = -ENETDOWN; if (!(nh->nh_dev->flags & IFF_UP)) goto out; err = 0;out: fib_res_put(&res); return err; } else { struct in_device *in_dev; if (nh->nh_flags&(RTNH_F_PERVASIVE|RTNH_F_ONLINK)) return -EINVAL; in_dev = inetdev_by_index(nh->nh_oif); if (in_dev == NULL) return -ENODEV; if (!(in_dev->dev->flags&IFF_UP)) { in_dev_put(in_dev); return -ENETDOWN; } nh->nh_dev = in_dev->dev; dev_hold(nh->nh_dev); nh->nh_scope = RT_SCOPE_HOST; in_dev_put(in_dev); } return 0;}static inline unsigned int fib_laddr_hashfn(__be32 val){ unsigned int mask = (fib_hash_size - 1); return ((__force u32)val ^ ((__force u32)val >> 7) ^ ((__force u32)val >> 14)) & mask;}static struct hlist_head *fib_hash_alloc(int bytes){ if (bytes <= PAGE_SIZE)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -