Linux路由proto字段

 

ip -d route

 

 

proto字段的定义如下(部分):

#define RTPROT_UNSPEC   0
#define RTPROT_REDIRECT 1   /* Route installed by ICMP redirects; not used by current IPv4 */
#define RTPROT_KERNEL   2   /* Route installed by kernel        */
#define RTPROT_BOOT     3   /* Route installed during boot      */
#define RTPROT_STATIC   4   /* Route installed by administrator */

 

proto字段的定义在内核中并没有实质的意义,只是一个显示字段。RTPROT_UNSPEC表示未指定;RTPROT_REDIRECT已经不再使用;内核自身添加的路由使用RTPROT_KERNEL;RTPROT_BOOT为在启动过程中安装的路由;

例如对于RTPROT_KERNEL类型,内核函数fib_magic添加的路由的protocol固定为RTPROT_KERNEL。在为内核接口添加IP地址时,触发此函数,用于添加相关的直连路由。

static void fib_magic(int cmd, int type, __be32 dst, int dst_len, struct in_ifaddr *ifa, u32 rt_priority)
{   
    struct fib_config cfg = {
        .fc_protocol = RTPROT_KERNEL,
        .fc_type = type,
        .fc_dst = dst,
        .fc_dst_len = dst_len,
        .fc_priority = rt_priority,
        .fc_prefsrc = ifa->ifa_local,
        .fc_oif = ifa->ifa_dev->dev->ifindex,

    if (cmd == RTM_NEWROUTE)
        fib_table_insert(net, tb, &cfg, NULL);
}

 

对于RTPROT_BOOT类型,一方面通过ioctl接口(如route命令)添加的路由proto字段会设置为RTPROT_BOOT;此外,如果使能了IP地址自动配置功能,内核可在启动过程中通过DHCP或者RARP等获取IP地址信息,此时,在添加与之相关的路由时,路由项也会使用RTPROT_BOOT类型。

数值大于RTPROT_STATIC(4)的proto值,不由内核解释。内核只是在路由下发时保存此值,显示时回显此值。目前定义的值如下:

#define RTPROT_GATED    8   /* Apparently, GateD */
#define RTPROT_RA       9   /* RDISC/ND router advertisements */
#define RTPROT_MRT      10  /* Merit MRT */
#define RTPROT_ZEBRA    11  /* Zebra */
#define RTPROT_BIRD     12  /* BIRD */
#define RTPROT_DNROUTED 13  /* DECnet routing daemon */
#define RTPROT_XORP     14  /* XORP */
#define RTPROT_NTK      15  /* Netsukuku */
#define RTPROT_DHCP     16     /* DHCP client */
#define RTPROT_MROUTED  17     /* Multicast daemon */
#define RTPROT_BABEL    42     /* Babel daemon */
#define RTPROT_BGP      186    /* BGP Routes */
#define RTPROT_ISIS     187    /* ISIS Routes */
#define RTPROT_OSPF     188    /* OSPF Routes */
#define RTPROT_RIP      189    /* RIP Routes */
#define RTPROT_EIGRP    192    /* EIGRP Routes */

 

比如quagga路由程序下发的动态路由(RIP/OSPF/BGP/ISIS)同一使用RTPROT_ZEBRA类型proto值。这也说明这个字段的使用比较混乱。

内核版本 5.0

posted @ 2020-09-10 17:09  ascertain  阅读(1174)  评论(0编辑  收藏  举报