确认给定参数范围的ip地址是否存在;
/*
* Confirm that local IP address exists using wildcards:
* - net: netns to check, cannot be NULL
* - in_dev: only on this interface, NULL=any interface
* - dst: only in the same subnet as dst, 0=any dst
* - local: address, 0=autoselect the local address
* - scope: maximum allowed scope value for the local address
*/ /*
确认参数中给定的本地地址是否存在
net: net命名空间,不能为空
in_dev: 在此ip控制块上,空表示任何设备上的控制块
dst: 与dst在同一子网,0表示不限制
local: 本地地址,0表示自动选择本地地址
scope: 本地地址最大允许的地址范围
*/ __be32 inet_confirm_addr(struct net *net, struct in_device *in_dev,
__be32 dst, __be32 local, int scope)
{
__be32 addr = ;
struct net_device *dev; /* ip控制块存在,则从该控制块中选择地址 */
if (in_dev)
return confirm_addr_indev(in_dev, dst, local, scope); rcu_read_lock();
/* ip控制块不存在,则遍历所有设备 */
for_each_netdev_rcu(net, dev) {
/* 找到设备中的ip控制块 */
in_dev = __in_dev_get_rcu(dev); /* 如果ip控制块存在 */
if (in_dev) {
/* 从该控制块选择地址 */
addr = confirm_addr_indev(in_dev, dst, local, scope);
/* 找到则结束 */
if (addr)
break;
}
}
rcu_read_unlock(); return addr;
}
/*
从指定ip控制块中获取参数允许范围内的地址
1. local dst都不在,找一个满足范围的ip
2. local存在,dst不存在,找与local相等的满足范围的ip
3. local不存在,dst存在,找满足范围的与dst在同一子网的ip
4. local dst都存在,找与local相等,并且与dst在同一子网的ip
*/
static __be32 confirm_addr_indev(struct in_device *in_dev, __be32 dst,
__be32 local, int scope)
{
int same = ;
__be32 addr = ; /* 遍历地址列表 */
for_ifa(in_dev) { if (!addr && /* 目标地址为空 */
(local == ifa->ifa_local || !local) && /* 本地地址相等或者参数地址为空 */
ifa->ifa_scope <= scope) { /* 当前地址满足<= 参数地址范围 */
/* 找到目标地址 */
addr = ifa->ifa_local; /* 满足子网要求 */
if (same)
break;
} /* 未判断子网是否相同 */
if (!same) {
/* local dst不要求,或者在同一子网 */
same = (!local || inet_ifa_match(local, ifa)) &&
(!dst || inet_ifa_match(dst, ifa)); /* 满足子网要求且地址存在,则具体判断是哪种情况 */
if (same && addr) { /* local存在或者local和dst都不存在 */
if (local || !dst)
break; /* local不存在,dst存在 */ /* Is the selected addr into dst subnet? */
/* 目标地址与dst在同一子网 */
if (inet_ifa_match(addr, ifa))
break; /* 目标地址与dst不在同一子网 */ /* No, then can we use new local src? */
/* 如果ifa满足范围,则使用这个ifa的地址 */
if (ifa->ifa_scope <= scope) {
addr = ifa->ifa_local;
break;
} /* 查找失败,继续查找 */ /* search for large dst subnet for addr */
same = ;
}
}
} endfor_ifa(in_dev); return same ? addr : ;
}