netlink 中文man页面

系统
Netlink 用于在内核模块与在用户地址空间中的进程之间传递消息的。它包含了用于用户进程的基于标准套接字的接口和用于内核模块的一个内部核心 API。有关这个内部核心接口的资料没有包含在此手册页中。同样还有一个过时的通过 netlink 字符设备的接口也没有包含在此,它只是提供向下兼容特性。

NAME 名称

netlink, PF_NETLINK - 内核与用户之间的通讯  

SYNOPSIS 总揽

#include <asm/types.h>

#include <sys/socket.h>
#include <linux/netlink.h>
netlink_socket = socket(PF_NETLINK, socket_type, netlink_family);

DESCRIPTION 描述

Netlink 用于在内核模块与在用户地址空间中的进程之间传递消息的。它包含了用于用户进程的基于标准套接字的接口和用于内核模块的一个内部核心 API。有关这个内部核心接口的资料没有包含在此手册页中。同样还有一个过时的通过 netlink 字符设备的接口也没有包含在此,它只是提供向下兼容特性。

Netlink 是一个面向数据包的服务。 SOCK_RAWSOCK_DGRAM 都是 socket_type 的有效值。然而 netlink 协议对数据包 datagram 和原套接字(raw sockets)并不作区分。

netlink_family 选择核心模块或 netlink 组进行通讯。现有可指定的 netlink 的种类有:

NETLINK_ROUTE
接收路由更新信息,可以用来修改 IPv4 的路由表。(参见 rtnetlink(7))。
NETLINK_FIREWALL
接收 IPv4 防火墙编码发送的数据包。
NETLINK_ARPD
用以维护用户地址空间里的 arp 表
NETLINK_ROUTE6
接收和发送 IPv6 路由表更新消息。
NETLINK_IP6_FW
接收未通过 IPv6 防火墙检查的数据包(尚未实现)
NETLINK_TAPBASE...NETLINK_TAPBASE+15
ethertap 设备实例。Ethertap 是从用户程序空间对以太网驱动程序进行仿真的“伪”网络通道设备。
NETLINK_SKIP
Enskip 的保留选项。
NETLINK_USERSOCK
为今后用户程序空间协议用保留选项。

Netlink 数据信息由具有一个或多个 nlmsghdr 数据报头及其有效数据的字节流组成。对于分成多个数据包的 Netlink 信息,数据报头中的 NLM_F_MULTI 标志位将被设置,除了***一个包的报头具有标志 NLMSG_DONE外。 字节流应只能用标准的 NLMSG_* 宏来访问,参阅 netlink(3).

Netlink 不是可靠的协议。它只是尽可能地将信息传输到目的地,但在内存耗尽或发生其他错误时,它会丢失信息。为保证信息可靠传输,可以设置标志 NLM_F_ACK 来要求接收方确认。数据接收确认是一个 NLMSG_ERROR 数据包,包中的出错字段设置为 0。应用程序必须自己创建收到信息确认消息。在信息传送过程中,内核一直(尝试)对每个出错的数据包发送 NLMSG_ERROR 消息。用户进程也应当遵循这一个惯例。

每一个 netlink 数据类都有一个32位广播分组,当对套接字调用 bind(2) 时, sockaddr_nl 中的 nl_groups 字段设置成所要侦听的广播组的位掩码。其默认值为 0,表示不接收任何广播。

一个套接字可以对任意一个多址广播组广播消息,只要在调用 sendmsg(2) 或调用 connect(2) 时,将位掩码 nl_groups 设置成要发送消息的广播组的值就可以了。只有具有有效 uid 为 0 的用户或具有

CAP_NET_ADMIN 权限的用户才可能发送或侦听针对 netlink 多址广播组的消息。任何一个对多址广播组消息的响应需发回进程标识 pid 和广播组地址。

struct nlmsghdr
{
__u32 nlmsg_len; /* 包括报头在内的消息长度*/
__u16 nlmsg_type; /* 消息正文 */
__u16 nlmsg_flags; /* 附加标志*/
__u32 nlmsg_seq; /* 序列号*/
__u32 nlmsg_pid; /* 发送进程号 PID */
};


struct nlmsgerr
{
int error; /* 负数表示的出错号 errno 或为 0 要求确认 acks*/ 
struct nlmsghdr msg; /* 造成出错的消息报头*/ 
};

在每个 nlmsghdr 后跟随着有效数据。 nlmsg_type 可以成为标准消息的类型: NLMSG_NOOP 可以忽略的消息, NLMSG_ERROR 发出错误发生的消息,有关数据中包含一个 nlmsgerr 结构, NLMSG_DONE 一个多数据包消息结束的信息。

一个 netlink 类通常指定更多的消息类型,请参阅有关手册页,如 NETLINK_ROUTE. 中的 rtnetlink(7)

nlmsg_flags 的标准标志位
 
NLM_F_REQUEST: 设置全部请求消息
 
NLM_F_MULTI:T{
 

 
此消息是多数据包消息之一,通过标志
 

 
结束。
 

 
T}
 

 
NLM_F_ACK: 数据成功接收返回确认消息
 

 
NLM_F_ECHO: 要求响应请求信息
 

 

为 GET 请求设立的附加标志位
 
NLM_F_ROOT 返回对象表而不是单个数据项
 
NLM_F_MATCH 尚未实现
 
NLM_F_ATOMIC 返回对象表的原子快照(atomic snapshot)
 
NLM_F_DUMP 尚未列入文档
 

对新建 NEW 请求设立的附加标志位
 
NLM_F_REPLACE 替换现有的对象
 
NLM_F_EXCL 如对象已存在,不作替换
 
NLM_F_CREATE 创建对象,如果对象不存在
 
NLM_F_APPEND 对象表添加对象项
 

注 NLM_F_ATOMIC 要求用户有 CAP_NET_ADMIN 或超级用户权。

地址格式

sockaddr_nl 描述了在用户空间或在核心空间里一个 netlink 客户对象的数据结构。一个 sockaddr_nl 对象可以是单址广播或对一个 netlink 多址组 (nl_groups 不为 0).

struct sockaddr_nl
{
sa_family_t nl_family; /* AF_NETLINK */
unsigned short nl_pad; /* 零 */
pid_t nl_pid; /* 进程标识号pid */
__u32 nl_groups; /* 多址广播组掩码*/
};

nl_pid 是用户空间中 netlink 的进程标识号 pid,如果是在内核时此值为 0。 nl_groups 是一个代表 neltlink 组号的位掩码。

BUGS

本手册页并不完整。

NOTES 注意

通过 libnetlink 调用 netlink 功能通常比通过低层内核接口要来得好些。

VERSIONS 版本

netlink 套接字接口是 Linux 2.2 新特性

Linux 2.0 支持更多的基于netlink接口的原始设备(作为向下兼容特性,这些设备目前仍可使用。旧接口特性没有在此叙述。

另见

cmsg(3), rtnetlink(7), netlink(3).

ftp://ftp.inr.ac.ru/ip-routing/iproute2* 有关 libnetlink 部分

#p#

NAME

netlink, PF_NETLINK - Communication between kernel and user  

SYNOPSIS

#include <asm/types.h>

#include <sys/socket.h>
#include <linux/netlink.h>
netlink_socket = socket(PF_NETLINK, socket_type, netlink_family);

DESCRIPTION

Netlink is used to transfer information between kernel modules and user space processes. It consists of a standard sockets based interface for user processes and an internal kernel API for kernel modules. The internal kernel interface is not documented in this man page. Also there is an obsolete netlink interface via netlink character devices, this interface is not documented here and is only provided for backwards compatibility.

Netlink is a datagram oriented service. Both SOCK_RAW and SOCK_DGRAM are valid values for socket_type; however the netlink protocol does not distinguish between datagram and raw sockets.

netlink_family selects the kernel module or netlink group to communicate with. The currently assigned netlink families are:

NETLINK_ROUTE
Receives routing updates and may be used to modify the IPv4 routing table (see rtnetlink(7)).
NETLINK_FIREWALL
Receives packets sent by the IPv4 firewall code.
NETLINK_ARPD
For managing the arp table in user space.
NETLINK_ROUTE6
Receives and sends IPv6 routing table updates.
NETLINK_IP6_FW
to receive packets that failed the IPv6 firewall checks (currently not implemented).
NETLINK_TAPBASE...NETLINK_TAPBASE+15
are the instances of the ethertap device. Ethertap is a pseudo network tunnel device that allows an ethernet driver to be simulated from user space.
NETLINK_SKIP
Reserved for ENskip.
NETLINK_USERSOCK
is reserved for future user space protocols.

Netlink messages consist of a byte stream with one or multiple nlmsghdr headers and associated payload. For multipart messages the first and all following headers have the NLM_F_MULTI flag set, except for the last header which has the type NLMSG_DONE. The byte stream should only be accessed with the standard NLMSG_* macros, see netlink(3).

Netlink is not a reliable protocol. It tries its best to deliver a message to its destination(s), but may drop messages when an out of memory condition or other error occurs. For reliable transfer the sender can request an acknowledgement from the receiver by setting the NLM_F_ACK flag. An acknowledgment is an NLMSG_ERROR packet with the error field set to 0. The application must generate acks for received messages itself. The kernel tries to send an NLMSG_ERROR message for every failed packet. A user process should follow this convention too.

Each netlink family has a set of 32 multicast groups. When bind(2) is called on the socket, the nl_groups field in the sockaddr_nl should be set to a bitmask of the groups which it wishes to listen to. The default value for this field is zero which means that no multicasts will be received. A socket may multicast messages to any of the multicast groups by setting nl_groups to a bitmask of the groups it wishes to send to when it calls sendmsg(2) or does a connect(2). Only users with an effective uid of 0 or the CAP_NET_ADMIN capability may send or listen to a netlink multicast group. Any replies to a message received for a multicast group should be sent back to the sending pid and the multicast group.

struct nlmsghdr
{
    __u32    nlmsg_len;  /* Length of message including header */
    __u16    nlmsg_type; /* Message content */
    __u16    nlmsg_flags;/* Additional flags */
    __u32    nlmsg_seq;  /* Sequence number */
    __u32    nlmsg_pid;  /* PID of the process that opened the socket */
};


struct nlmsgerr
{
    int      error;      /* negative errno or 0 for acks. */ 
    struct nlmsghdr msg; /* message header that caused the error */ 
};

After each nlmsghdr the payload follows. nlmsg_type can be one of the standard message types: NLMSG_NOOP message is to be ignored, NLMSG_ERROR the message signals an error and the payload contains a nlmsgerr structure, NLMSG_DONE message terminates a multipart message,

A netlink family usually specifies more message types, see the appropriate man pages for that, e.g. rtnetlink(7) for NETLINK_ROUTE.

Standard Flag bits in nlmsg_flags
 
NLM_F_REQUEST:set on all request messages
 
NLM_F_MULTI:T{
 

 
the message is part of a multipart message terminated by
 

 
NLMSG_DONE
 

 
T}
 

 
NLM_F_ACK:reply with an acknowledgment on success
 

 
NLM_F_ECHO:echo this request
 

 

Additional flag bits for GET requests
 
NLM_F_ROOT Return the complete table instead of a single entry.
 
NLM_F_MATCH Not implemented yet.
 
NLM_F_ATOMIC Return an atomic snapshot of the table.
 
NLM_F_DUMP not documented yet.
 

Additional flag bits for NEW requests
 
NLM_F_REPLACE Override existing object.
 
NLM_F_EXCL Don't replace if the object already exists.
 
NLM_F_CREATE Create object if it doesn't already exist.
 
NLM_F_APPEND Add to the end of the object list.
 

Note that NLM_F_ATOMIC requires CAP_NET_ADMIN or super user rights.

ADDRESS FORMATS

The sockaddr_nl structure describes a netlink client in user space or in the kernel. A sockaddr_nl can be either unicast (only send to one peer) or send to netlink groups (nl_groups not equal 0).

struct sockaddr_nl
{
    sa_family_t nl_family;    /* AF_NETLINK */
    unsigned short nl_pad;    /* zero */
    pid_t       nl_pid;       /* process pid */
    __u32       nl_groups;    /* multicast groups mask */
};

nl_pid is the pid of the process owning the destination socket, or 0 if the destination is in the kernel. nl_groups is a bitmask with every bit representing a netlink group number.

BUGS

This man page is not complete.

NOTES

It is often better to use netlink via libnetlink than via the low level kernel interface.

VERSIONS

The socket interface to netlink is a new feature of Linux 2.2

Linux 2.0 supported a more primitive device based netlink interface (which is still available as a compatibility option). This obsolete interface is not described here.

SEE ALSO

cmsg(3), rtnetlink(7), netlink(3)

ftp://ftp.inr.ac.ru/ip-routing/iproute2* for libnetlink

责任编辑:韩亚珊 来源: CMPP.net
相关推荐

2011-08-24 16:48:36

man中文man

2011-08-15 10:21:09

man中文man

2011-08-11 16:11:49

at中文man

2011-08-25 10:21:56

man.conf中文man

2011-08-25 15:39:42

fcloseall中文man

2011-08-25 15:00:15

cfgetispeed中文man

2011-08-19 18:35:50

issue中文man

2011-08-25 17:03:51

pclose中文man

2011-08-25 17:40:25

setvbuf中文man

2011-08-23 14:21:16

poweroff中文man

2011-08-24 15:52:59

intro中文man

2011-08-23 13:40:31

2011-08-25 17:24:54

puts中文man

2011-08-25 18:34:55

ungetc中文man

2011-08-23 10:03:40

useradd中文man

2011-08-23 10:29:02

chpasswd中文man

2011-08-23 10:34:22

convertquot中文man

2011-08-23 15:39:34

rpmbuild中文man

2011-08-24 15:48:38

INSERT中文man

2011-08-25 09:40:49

UPDATE中文man
点赞
收藏

51CTO技术栈公众号