static int ip_route_input_slow(struct sk_buff *skb, __be32 daddr, __be32 saddr,
u8 tos, struct net_device *dev)
{
struct fib_result res;
struct in_device *in_dev = in_dev_get(dev);
struct flowi fl = { .nl_u = { .ip4_u =
{ .daddr = daddr,
.saddr = saddr,
.tos = tos,
.scope = RT_SCOPE_UNIVERSE,
} },
.mark = skb->mark,
.iif = dev->ifindex }; 初始化路由键值
unsigned flags = 0;
u32 itag = 0;
struct rtable * rth;
unsigned hash;
__be32 spec_dst;
int err = -EINVAL;
int free_res = 0;
struct net * net = dev_net(dev);
/* IP on this device is disabled. */
if (!in_dev)
goto out;
/* Check for the most weird martians, which can be not detected
by fib_lookup.
*/
if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr) || 是否是多播、组播、环回地址等
ipv4_is_loopback(saddr))
goto martian_source;
if (daddr == htonl(0xFFFFFFFF) || (saddr == 0 && daddr == 0))
goto brd_input;
/* Accept zero addresses only to limited broadcast;
* I even do not know to fix it or not. Waiting for complains :-)
*/
if (ipv4_is_zeronet(saddr)) 源地址是否是零网地址类型
goto martian_source;
if (ipv4_is_lbcast(daddr) || ipv4_is_zeronet(daddr) ||
ipv4_is_loopback(daddr))
goto martian_destination;
/*
* Now we are ready to route packet.
*/
if ((err = fib_lookup(net, &fl, &res)) != 0) { 通过路由函数查找目标地址 结果记录在res中
if (!IN_DEV_FORWARD(in_dev)) 如果设备不支持转发
goto e_hostunreach;
goto no_route;
}
free_res = 1; 默认为释放查找结果
RT_CACHE_STAT_INC(in_slow_tot);
if (res.type == RTN_BROADCAST) 路由类型为广播
goto brd_input;
if (res.type == RTN_LOCAL) { 路由类型为本地类型
int result;
result = fib_validate_source(saddr, daddr, tos, 检查源地址
net->loopback_dev->ifindex,
dev, &spec_dst, &itag, skb->mark);
if (result < 0)
goto martian_source; 源地址错误
if (result)
flags |= RTCF_DIRECTSRC;
spec_dst = daddr; 记录目标地址
goto local_input; 本地输入,跳转
}
if (!IN_DEV_FORWARD(in_dev)) 如果设备不支持转发
goto e_hostunreach;
if (res.type != RTN_UNICAST) 如果目标地址不是单播类型
goto martian_destination; 目标地址错误,跳转
err = ip_mkroute_input(skb, &res, &fl, in_dev, daddr, saddr, tos); 创建用于转发的路由表
done:
in_dev_put(in_dev);
if (free_res) 如果需要释放
fib_res_put(&res);
out: return err;
brd_input: 广播输入
if (skb->protocol != htons(ETH_P_IP)) 不是ip协议
goto e_inval;
if (ipv4_is_zeronet(saddr))
spec_dst = inet_select_addr(dev, 0, RT_SCOPE_LINK);
else {
err = fib_validate_source(saddr, 0, tos, 0, dev, &spec_dst, 检查源地址的有效性
&itag, skb->mark);
if (err < 0)
goto martian_source;
if (err)
flags |= RTCF_DIRECTSRC;
}
flags |= RTCF_BROADCAST; 增加广播标志
res.type = RTN_BROADCAST; 设置地址类型为广播类型
RT_CACHE_STAT_INC(in_brd);
local_input: 本地输入
rth = dst_alloc(&ipv4_dst_ops); 创建路由表
if (!rth)
goto e_nobufs;
rth->u.dst.output= ip_rt_bug; 设置输出方向的函数
rth->rt_genid = rt_genid(net); 产生随机值
atomic_set(&rth->u.dst.__refcnt, 1);
rth->u.dst.flags= DST_HOST; 设置路由标志
if (IN_DEV_CONF_GET(in_dev, NOPOLICY))
rth->u.dst.flags |= DST_NOPOLICY;
rth->fl.fl4_dst = daddr; 记录目标地址
rth->rt_dst = daddr;
rth->fl.fl4_tos = tos; 记录TOS
rth->fl.mark = skb->mark; 记录掩码
rth->fl.fl4_src = saddr; 记录源地址
rth->rt_src = saddr;
#ifdef CONFIG_NET_CLS_ROUTE
rth->u.dst.tclassid = itag;
#endif
rth->rt_iif =
rth->fl.iif = dev->ifindex; 记录网络设备id
rth->u.dst.dev = net->loopback_dev; 记录环回设备
dev_hold(rth->u.dst.dev);
rth->idev = in_dev_get(rth->u.dst.dev);
rth->rt_gateway = daddr; 记录网关地址
rth->rt_spec_dst= spec_dst; 记录指定目标地址
rth->u.dst.input= ip_local_deliver; 设置输入函数 ip_local_deliver在这里设置,(文章开头的疑问?)
rth->rt_flags = flags|RTCF_LOCAL; 增加本地路由地址
if (res.type == RTN_UNREACHABLE) { 如果目标地址不可达
rth->u.dst.input= ip_error;
rth->u.dst.error= -err;
rth->rt_flags &= ~RTCF_LOCAL;
}
rth->rt_type = res.type; 设置地址类型
hash = rt_hash(daddr, saddr, fl.iif, rt_genid(net)); 计算hash值
err = rt_intern_hash(hash, rth, NULL, skb); 将路由表插入hash队列并记录到数据包中
goto done;
no_route:
RT_CACHE_STAT_INC(in_no_route);
spec_dst = inet_select_addr(dev, 0, RT_SCOPE_UNIVERSE); 确定指定目标
res.type = RTN_UNREACHABLE; 设置不可达
if (err == -ESRCH)
err = -ENETUNREACH;
goto local_input;
/*
* Do not cache martian addresses: they should be logged (RFC1812)
*/
martian_destination: 目标地址错
RT_CACHE_STAT_INC(in_martian_dst);
#ifdef CONFIG_IP_ROUTE_VERBOSE
if (IN_DEV_LOG_MARTIANS(in_dev) && net_ratelimit())
printk(KERN_WARNING "martian destination %pI4 from %pI4, dev %s\n",
&daddr, &saddr, dev->name);
#endif
e_hostunreach: 主机不可达错误
err = -EHOSTUNREACH;
goto done;
e_inval: 无法识别
err = -EINVAL;
goto done;
e_nobufs: 空间不足
err = -ENOBUFS;
goto done;
martian_source: 源地址错误
ip_handle_martian_source(dev, in_dev, skb, daddr, saddr);
goto e_inval;
}
在上面函数的分析中可以看到,对于目标地址是转发情况的,调用ip_mkroute_input()函数创建转发路由表
对于广播或者本地类型,直接分配路由表并初始化,并直接指定下一步处理函数为ip_local_deliver()
static int
ip_mkroute_input(struct sk_buff *skb,
struct fib_result *res,
const struct flowi *fl,
struct in_device *in_dev,
__be32 daddr, __be32 saddr, u32 tos)
{
struct rtable* rth = NULL;
int err;
unsigned hash;
#ifdef CONFIG_IP_ROUTE_MULTIPATH 多路径选择
if (res->fi && res->fi->fib_nhs > 1 && fl->oif == 0)
fib_select_multipath(fl, res);
#endif
/* create a routing cache entry */
err = __mkroute_input(skb, res, in_dev, daddr, saddr, tos, &rth); 创建路由表
if (err)
return err;
/* put it into the cache */
hash = rt_hash(daddr, saddr, fl->iif,
rt_genid(dev_net(rth->u.dst.dev))); 计算hash值
return rt_intern_hash(hash, rth, NULL, skb); 将路由表插入路由高速缓存队列中,并记录到数据包中
}
static int __mkroute_input(struct sk_buff *skb,
struct fib_result *res,
struct in_device *in_dev,
__be32 daddr, __be32 saddr, u32 tos,
struct rtable **result)
{
struct rtable *rth;
int err;
struct in_device *out_dev;
unsigned flags = 0;
__be32 spec_dst;
u32 itag;
/* get a working reference to the output device */
out_dev = in_dev_get(FIB_RES_DEV(*res)); 取出输出设备的配置结构
if (out_dev == NULL) {
if (net_ratelimit())
printk(KERN_CRIT "Bug in ip_route_input" \
"_slow(). Please, report\n");
return -EINVAL;
}
err = fib_validate_source(saddr, daddr, tos, FIB_RES_OIF(*res),
in_dev->dev, &spec_dst, &itag, skb->mark); 检查源地址
if (err < 0) {
ip_handle_martian_source(in_dev->dev, in_dev, skb, daddr,
saddr);
err = -EINVAL;
goto cleanup;
}
if (err)
flags |= RTCF_DIRECTSRC;
if (out_dev == in_dev && err &&
(IN_DEV_SHARED_MEDIA(out_dev) ||
inet_addr_onlink(out_dev, saddr, FIB_RES_GW(*res))))
flags |= RTCF_DOREDIRECT;
if (skb->protocol != htons(ETH_P_IP)) { 如果不是ip
/* Not IP (i.e. ARP). Do not create route, if it is
* invalid for proxy arp. DNAT routes are always valid.
*/
if (out_dev == in_dev) {
err = -EINVAL;
goto cleanup;
}
}
rth = dst_alloc(&ipv4_dst_ops); 申请路由项
if (!rth) {
err = -ENOBUFS;
goto cleanup;
}
atomic_set(&rth->u.dst.__refcnt, 1);
rth->u.dst.flags= DST_HOST;
if (IN_DEV_CONF_GET(in_dev, NOPOLICY))
rth->u.dst.flags |= DST_NOPOLICY;
if (IN_DEV_CONF_GET(out_dev, NOXFRM))
rth->u.dst.flags |= DST_NOXFRM;
rth->fl.fl4_dst = daddr;
rth->rt_dst = daddr;
rth->fl.fl4_tos = tos;
rth->fl.mark = skb->mark;
rth->fl.fl4_src = saddr;
rth->rt_src = saddr;
rth->rt_gateway = daddr;
rth->rt_iif =
rth->fl.iif = in_dev->dev->ifindex;
rth->u.dst.dev = (out_dev)->dev;
dev_hold(rth->u.dst.dev);
rth->idev = in_dev_get(rth->u.dst.dev);
rth->fl.oif = 0;
rth->rt_spec_dst= spec_dst;
rth->u.dst.input = ip_forward; 设置输入函数 ip_forward在这里设置,(文章开头的疑问?)