转至:http://blogold.chinaunix.net/u/15993/showart_90109.html
rtnetlink就是一组操作rtnetlink消息的宏。
在netlink中,初始化一个netlink_route套接字时,最后一个字段使用NETLINK_ROUTE时就是rtnetlink_socket。
#include <asm/types.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <sys/socket.h>
rtnetlink_socket = socket(PF_NETLINK, int socket_type, NETLINK_ROUTE);
这组宏包括:
int RTA_OK(struct rtattr *rta, int rtabuflen);
void *RTA_DATA(struct rtattr *rta);
unsigned int RTA_PAYLOAD(struct rtattr *rta);
struct rtattr *RTA_NEXT(struct rtattr *rta, unsigned int rtabuflen);
unsigned int RTA_LENGTH(unsigned int length);
unsigned int RTA_SPACE(unsigned int length);
一些rtnetlink消息在初始化头后面有附加的属性:
struct rtattr
{
unsigned short rta_len; /* Length of option */
unsigned short rta_type; /* Type of option */
/* Data follows */
};
RTM_NEWLINK, RTM_DELLINK, RTM_GETLINK
创建或者删除一个特定的网络接口,或者从一个特定的网络接口上获得信息。这些消息含有一个ifinfomsg类型的结构,紧跟在后面的是一系列的rtattr结构。
struct ifinfomsg
{
unsigned char ifi_family; /* AF_UNSPEC */
unsigned char __ifi_pad; /* unused */
unsigned short ifi_type; /* Device type */
int ifi_index; /* Interface index */
unsigned int ifi_flags; /* Device flags */
unsigned int ifi_change; /* change mask */
}
其中ifi_change是为将来预留的,总是被设为0xFFFFFFFF
rta_type value type description
--------------------------------------------------------------
IFLA_UNSPEC - unspecified.
IFLA_ADDRESS hardware address interface L2 address
IFLA_BROADCAST hardware address L2 broadcast address.
IFLA_IFNAME asciiz string Device name.
IFLA_MTU unsigned int MTU of the device.
IFLA_LINK int Link type.
IFLA_QDISC asciiz string Queueing discipline.
IFLA_STATS struct Interface Statistics.
net_device_stats
RTM_NEWADDR, RTM_DELADDR, RTM_GETADDR
添加,删除或者接收一个和接口相关的IP地址的信息。在linux2.2中,一个网口是可以有多个IP地址信息的。这些消息含有一个ifaddrmsg类型的结构,紧跟在后面的是一系列的rtattr结构。
struct ifaddrmsg
{
unsigned char ifa_family; /* Address type */
unsigned char ifa_prefixlen;/* Prefixlength of the address */
unsigned char ifa_flags; /* Address flags */
unsigned char ifa_scope; /* Address scope */
int ifa_index; /* Interface index */
};
rta_type value type description
-------------------------------------------------------------
IFA_UNSPEC - unspecified.
IFA_ADDRESS raw protocol address interface address
IFA_LOCAL raw protocol address local address
IFA_LABEL asciiz string name of the interface
IFA_BROADCAST raw protocol address broadcast address.
IFA_ANYCAST raw protocol address anycast address
IFA_CACHEINFO struct ifa_cacheinfo Address information.
创建,删除一个网络路由或者从一个网络路由上获得信息。这些消息包含了一个rtmsg结构,紧跟着的是一系列的rtattr结构,这是可选的。
struct rtmsg
{
unsigned char rtm_family; /* Address family of route */
unsigned char rtm_dst_len; /* Length of source */
unsigned char rtm_src_len; /* Length of destination */
unsigned char rtm_tos; /* TOS filter */
unsigned char rtm_protocol;/* Routing protocol; see below */
unsigned char rtm_scope; /* See below */
unsigned char rtm_type; /* See below */
};
-----------------------------------------------------------
RTN_UNSPEC unknown route
RTN_UNICAST a gateway or direct route
RTN_LOCAL a local interface route
RTN_BROADCAST a local broadcast route (sent as a
broadcast)
RTN_ANYCAST a local broadcast route (sent as a uni-
cast)
RTN_MULTICAST a multicast route
RTN_BLACKHOLE a packet dropping route
RTN_UNREACHABLE an unreachable destination
RTN_PROHIBIT a packet rejection route
RTN_THROW continue routing lookup in another table
RTN_NAT a network address translation rule
RTN_XRESOLVE refer to an external resolver (not
implemented)
-----------------------------------------
RTPROT_UNSPEC unknown
RTPROT_REDIRECT by an ICMP redirect
(currently unused)
RTPROT_KERNEL by the kernel
RTPROT_BOOT during boot
RTPROT_STATIC by the administrator
比RTPROT_STATIC大的值就不会再被kernel解释了,他们就只是用于用户信息了。
RT_SCOPE_UNIVERSE global route
RT_SCOPE_SITE interior route in the
local autonomous system
RT_SCOPE_LINK route on this link
RT_SCOPE_HOST route on the local host
RT_SCOPE_NOWHERE destination doesn't exist
RTM_F_NOTIFY if the route changes, notify the user via
rtnetlink
RTM_F_CLONED route is cloned from another route
RTM_F_EQUALIZE a multicast equalizer (not yet implemented)
RT_TABLE_UNSPEC an unspecified routing table
RT_TABLE_DEFAULT the default table
RT_TABLE_MAIN the main table
RT_TABLE_LOCAL the local table
rta_type value type description
--------------------------------------------------------------
RTA_UNSPEC - ignored.
RTA_DST protocol address Route destination address.
RTA_SRC protocol address Route source address.
RTA_IIF int Input interface index.
RTA_OIF int Output interface index.
RTA_GATEWAY protocol address The gateway of the route
RTA_PRIORITY int Priority of route.
RTA_PREFSRC
RTA_METRICS int Route metric
RTA_MULTIPATH
RTA_PROTOINFO
RTA_FLOW
RTA_CACHEINFO
RTM_NEWQDISC, RTM_DELQDISC, RTM_GETQDISC
RTM_NEWTCLASS, RTM_DELTCLASS, RTM_GETTCLASS
RTM_NEWTFILTER, RTM_DELTFILTER, RTM_GETTFILTER