參考連結:https://www.systutorials.com/docs/linux/man/7-netlink/
Netlink是使用者程式與核心通訊的socket方法,通過Netlink可以獲得修改核心的設定,常見的有獲得介面的IP地址列表、更改路由表或鄰居表。舊版本的核心提供很多從核心獲取資訊的方式,至今仍在被廣泛使用。
其次,除了可以獲取修改核心設定外,還能夠監聽核心相關設定資訊變化的事件,例如:介面狀態、介面地址、核心路由表或者核心鄰居表項的變更。
下面,我們先列舉一個簡單的例子:監聽介面的狀態變化,並列印出出,發生變化的介面資訊。
咋們直接上程式碼,然後在詳細描述,實現的關鍵步驟。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <sys/time.h>
#include <asm/types.h>
#include <linux/if.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#define dprint(format, ...) \
printf("[%15s:%-4d] " format , __FUNCTION__, __LINE__, ##__VA_ARGS__)
static int gnl_fd;
static void parse_rtattr(struct rtattr **tb, int max, struct rtattr *attr, int len)
{
for ( ; RTA_OK(attr, len); attr = RTA_NEXT(attr, len)) {
if (attr->rta_type <= max) {
tb[attr->rta_type] = attr;
}
}
}
static void show_iflink_msg(struct nlmsghdr *nh_msg)
{
int msg_len;
/**
* @brief #define IFLA_MAX (__IFLA_MAX - 1)
* 標頭檔案:linux/if_link.h
*/
struct rtattr *tb[IFLA_MAX + 1];
struct ifinfomsg *ifmsg; /* 6 */
bzero(tb, sizeof(tb));
ifmsg = NLMSG_DATA(nh_msg); /* 7 */
msg_len = nh_msg->nlmsg_len - NLMSG_SPACE(sizeof(*ifmsg));
parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifmsg), msg_len); /* 8 */
dprint(" >> if intf_index: %d\n", ifmsg->ifi_index);
dprint(" >> if intf_name : %s\n", (tb[IFLA_IFNAME] ? RTA_DATA(tb[IFLA_IFNAME]) : " "));
dprint(" >> if link_type : %s\n", (nh_msg->nlmsg_type == RTM_NEWLINK) ? "NEWLINK" : "DELLINK");
dprint(" >> if link_state: %s\n\n", (ifmsg->ifi_flags & IFF_UP) ? "up" : "down");
return;
}
int main(int argc, char **argv)
{
fd_set rd_set;
int max_fd = -1;
int iret, old_iret = -1;
struct timeval tmval;
struct sockaddr_nl sa_nl;
char sbuff[2048];
struct nlmsghdr *nh_msg;
memset(&sa_nl, 0, sizeof(sa_nl));
sa_nl.nl_family = PF_NETLINK; /* 1 */
sa_nl.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR; /* 2 */
gnl_fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE); /* 3 */
bind(gnl_fd, (struct sockaddr *) &sa_nl, sizeof(sa_nl));
dprint("begin listen gnl_fd socket ...\n");
for ( ; ; ) {
FD_ZERO(&rd_set);
FD_SET(gnl_fd, &rd_set);
tmval.tv_sec = 1;
tmval.tv_usec = 0;
max_fd = (max_fd > gnl_fd) ? max_fd : gnl_fd;
iret = select(max_fd + 1, &rd_set, NULL, NULL, &tmval);
if (old_iret != iret) {
dprint("select return value %d, errno %d.\n", iret, errno);
old_iret = iret;
}
if (iret == -1 || iret == 0 || !FD_ISSET(gnl_fd, &rd_set)) {
if (iret == -1 && errno != EINTR)
break;
continue;
}
iret = read(gnl_fd, sbuff, sizeof(sbuff));
dprint(" >> read gnl_fd return value %d.\n", iret);
if (iret <= 0) {
continue;
}
nh_msg = (struct nlmsghdr *)sbuff;
for ( ; NLMSG_OK(nh_msg, iret); nh_msg = NLMSG_NEXT(nh_msg, iret)) { /* 4 */
dprint(" >> recive nh_msg type %u, portid %u.\n", nh_msg->nlmsg_type, nh_msg->nlmsg_pid);
/**
* @brief 這裡的 nlmsg_type 對應到 linux/rtnetlink.h 中
* enum { RTM_BASE = 16, ... } 等列舉型別
*/
switch (nh_msg->nlmsg_type) { /* 5 */
case RTM_NEWLINK:
case RTM_DELLINK:
show_iflink_msg(nh_msg);
break;
default:
break;
}
}
}
close(gnl_fd);
dprint("close gnl_fd socket, bye bye...\n");
return 0;
}
struct sockaddr_nl {
sa_family_t nl_family; /* AF_NETLINK */
unsigned short nl_pad; /* Zero */
pid_t nl_pid; /* Port ID */
__u32 nl_groups; /* Multicast groups mask */
};
常用的設定選項,在標頭檔案 linux/rtnetlink.h 檔案約659行
#define RTMGRP_LINK 1
#define RTMGRP_NOTIFY 2
#define RTMGRP_NEIGH 4
#define RTMGRP_TC 8
#define RTMGRP_IPV4_IFADDR 0x10
#define RTMGRP_IPV4_MROUTE 0x20
#define RTMGRP_IPV4_ROUTE 0x40
#define RTMGRP_IPV4_RULE 0x80
#define RTMGRP_IPV6_IFADDR 0x100
#define RTMGRP_IPV6_MROUTE 0x200
#define RTMGRP_IPV6_ROUTE 0x400
#define RTMGRP_IPV6_IFINFO 0x800
#define RTMGRP_DECnet_IFADDR 0x1000
#define RTMGRP_DECnet_ROUTE 0x4000
#define RTMGRP_IPV6_PREFIX 0x20000
在我們範例中,我們僅想監聽介面鏈路狀態和介面地址變化;所以,只需要設定上LINK和IFADDR即可;其他設定,根據自己需求進行設定
3. 注意socket(…)函數中第三個引數NETLINK_ROUTE,這個值我們又是從哪裡獲取,又是怎麼確定應該使用它而不是別的值呢,這裡就需要簡單解釋下。
這個值在標頭檔案:linux/netlink.h 中約第9行開始
當前可用的宏定義有以下這麼多:
#define NETLINK_ROUTE 0 /* Routing/device hook */
#define NETLINK_UNUSED 1 /* Unused number */
#define NETLINK_USERSOCK 2 /* Reserved for user mode socket protocols */
#define NETLINK_FIREWALL 3 /* Unused number, formerly ip_queue */
#define NETLINK_SOCK_DIAG 4 /* socket monitoring */
#define NETLINK_NFLOG 5 /* netfilter/iptables ULOG */
#define NETLINK_XFRM 6 /* ipsec */
#define NETLINK_SELINUX 7 /* SELinux event notifications */
#define NETLINK_ISCSI 8 /* Open-iSCSI */
#define NETLINK_AUDIT 9 /* auditing */
#define NETLINK_FIB_LOOKUP 10
#define NETLINK_CONNECTOR 11
#define NETLINK_NETFILTER 12 /* netfilter subsystem */
#define NETLINK_IP6_FW 13
#define NETLINK_DNRTMSG 14 /* DECnet routing messages */
#define NETLINK_KOBJECT_UEVENT 15 /* Kernel messages to userspace */
#define NETLINK_GENERIC 16
/* leave room for NETLINK_DM (DM Events) */
#define NETLINK_SCSITRANSPORT 18 /* SCSI Transports */
#define NETLINK_ECRYPTFS 19
#define NETLINK_RDMA 20
#define NETLINK_CRYPTO 21 /* Crypto layer */
#define NETLINK_SMC 22 /* SMC monitoring */
#define NETLINK_INET_DIAG NETLINK_SOCK_DIAG
#define MAX_LINKS 32
根據《深入Linux核心架構與底層原理》這本書9.2.2節介紹,每個宏的含義如下(這裡只列舉幾個常用的)
struct nlmsghdr {
__u32 nlmsg_len; /* Length of message including header */
__u16 nlmsg_type; /* Type of message content */
__u16 nlmsg_flags; /* Additional flags */
__u32 nlmsg_seq; /* Sequence number */
__u32 nlmsg_pid; /* Sender port ID */
};
這裡最常用到的就是 nlmsg_type 這個欄位了,在下一點進行介紹。
其次,對於這個 nlmsg_flags 欄位,再做下介紹:
Standard flag bits in nlmsg_flags
NLM_F_REQUEST Must be set on all request messages.
NLM_F_MULTI The message is part of a multipart message terminated by NLMSG_DONE.
NLM_F_ACK Request for 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 Return all entries matching criteria passed in message content. Not implemented yet.
NLM_F_ATOMIC Return an atomic snapshot of the table.
NLM_F_DUMP Convenience macro; equivalent to (NLM_F_ROOT|NLM_F_MATCH).
Note that NLM_F_ATOMIC requires the CAP_NET_ADMIN capability or an effective UID of 0.
Additional flag bits for NEW requests(以下這幾個,我們可能會常用到)
NLM_F_REPLACE Replace existing matching 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.
/****
* Routing/neighbour discovery messages.
****/
/* Types of messages */
enum {
RTM_BASE = 16,
#define RTM_BASE RTM_BASE
RTM_NEWLINK = 16,
#define RTM_NEWLINK RTM_NEWLINK
RTM_DELLINK,
#define RTM_DELLINK RTM_DELLINK
RTM_GETLINK,
#define RTM_GETLINK RTM_GETLINK
RTM_SETLINK,
#define RTM_SETLINK RTM_SETLINK
RTM_NEWADDR = 20,
#define RTM_NEWADDR RTM_NEWADDR
RTM_DELADDR,
#define RTM_DELADDR RTM_DELADDR
RTM_GETADDR,
#define RTM_GETADDR RTM_GETADDR
...
/*
* IFLA_AF_SPEC
* Contains nested attributes for address family specific attributes.
* Each address family may create a attribute with the address family
* number as type and create its own attribute structure in it.
*
* Example:
* [IFLA_AF_SPEC] = {
* [AF_INET] = {
* [IFLA_INET_CONF] = ...,
* },
* [AF_INET6] = {
* [IFLA_INET6_FLAGS] = ...,
* [IFLA_INET6_CONF] = ...,
* }
* }
*/
enum {
IFLA_UNSPEC,
IFLA_ADDRESS,
IFLA_BROADCAST,
IFLA_IFNAME,
IFLA_MTU,
IFLA_LINK,
IFLA_QDISC,
IFLA_STATS,
IFLA_COST,
#define IFLA_COST IFLA_COST
IFLA_PRIORITY,
#define IFLA_PRIORITY IFLA_PRIORITY
IFLA_MASTER,
這些屬性值,都是可以通過 RTA_DATA( tb[IFLA_XXX] ) 獲取到。
至此,一個簡單的範例也就講述完畢。
進行Netlink程式設計的一個簡單的總結:
取得的型別的訊息結構體了,然後就是從訊息結構體解析出,攜帶的資料,我們就需要struct rtattr *tb結構體以及相關API的使用;然後就是,需要在相關標頭檔案中,找到這個訊息結構體描述的事物,它具有哪些屬性。比如:描述的網路卡(介面),它具有索引值、網路卡名稱,MTU,Link狀態等屬性;更具屬性的列舉變數,使用 RTA_DATA ( tb[XXX] )來獲取相應的值。
好用的Linux線上手冊:https://www.man7.org/linux/man-pages/index.html