netlink(7) Miscellaneous Information Manual netlink(7) - (AF_NETLINK) #include #include #include netlink_socket = socket(AF_NETLINK, socket_type, netlink_family); . . . . . SOCK_RAW SOCK_DGRAM socket_type. . netlink_family . : NETLINK_ROUTE (IPv4 IPv6) IP ( rtnetlink(7)). NETLINK_W1 ( 2.6.13 2.16.17) . NETLINK_USERSOCK . NETLINK_FIREWALL ( 3.4 ) IPv4 netfilter . ip_queue. ( nfnetlink_queue) NETLINK_FIREWALL 3.5. NETLINK_SOCK_DIAG ( 3.3) ( sock_diag(7)). NETLINK_INET_DIAG ( 2.6.14) NETLINK_SOCK_DIAG. NETLINK_NFLOG ( 3.16 ) ULOG Netfilter/iptables. NETLINK_XFRM IPsec. NETLINK_SELINUX ( 2.6.4) SELinux. NETLINK_ISCSI ( 2.6.15) Open-iSCSI. NETLINK_AUDIT ( 2.6.6) . NETLINK_FIB_LOOKUP ( 2.6.13) FIB . NETLINK_CONNECTOR ( 2.6.14) . Documentation/driver-api/connector.rst ( /Documentation/connector/connector.* 5.2 ) . NETLINK_NETFILTER ( 2.6.14) Netfilter. NETLINK_SCSITRANSPORT ( 2.6.19) SCSI. NETLINK_RDMA ( 3.0) RDMA . NETLINK_IP6_FW ( 3.4 ) IPv6 netfilter . ip6_queue. NETLINK_DNRTMSG DECnet. NETLINK_KOBJECT_UEVENT ( 2.6.10) . NETLINK_GENERIC ( 2.6.15) netlink netlink . NETLINK_CRYPTO ( 3.2) netlink . Netlink nlmsghdr . NLMSG_*. netlink(3) . ( nlmsghdr ) NLM_F_MULTI NLMSG_DONE. nlmsghdr . struct nlmsghdr { __u32 nlmsg_len; /* Size 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_NOOP NLMSG_ERROR nlmsgerr NLMSG_DONE . . struct nlmsgerr { int error; /* Negative errno or 0 for acknowledgements */ struct nlmsghdr msg; /* Message header that caused the error */ /* * followed by the message contents * unless NETLINK_CAP_ACK was set * or the ACK indicates success (error == 0). * For example Generic Netlink message with attributes. * message size is aligned with NLMSG_ALIGN() */ /* * followed by TLVs defined in enum nlmsgerr_attrs * if NETLINK_EXT_ACK was set */ }; netlink rtnetlink(7) NETLINK_ROUTE. nlmsg_flags ------------------------------------------------------------------------ NLM_F_REQUEST . NLM_F_MULTI NLMSG_DONE. NLM_F_ACK . NLM_F_ECHO . GET ------------------------------------------------------------------------ NLM_F_ROOT . NLM_F_MATCH . . NLM_F_ATOMIC . NLM_F_DUMP (NLM_F_ROOT|NLM_F_MATCH). NLM_F_ATOMIC CAP_NET_ADMIN UID 0. NEW ------------------------------------------------------------------------ NLM_F_REPLACE . NLM_F_EXCL . NLM_F_CREATE . NLM_F_APPEND . nlmsg_seq nlmsg_pid . nlmsg_pid . 1:1 nlmsg_pid (PID) netlink. ADDRESS FORMATS . nlmsg_seq nlmsg_pid netlink. Netlink . () . NLM_F_ACK. NLMSG_ERROR 0. . NLMSG_ERROR . . . netlink : . ( ENOBUFS recvmsg(2)) . sockaddr_nl netlink . sockaddr_nl ( ) netlink (nl_groups 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 */ }; nl_pid netlink. 0 . nl_pid (PID) . nl_pid netlink . netlink nl_pid . nl_pid netlink. nl_pid bind(2) nl_pid . 0 . netlink nl_pid netlink . nl_groups netlink. netlink 32 . bind(2) nl_groups sockaddr_nl . . nl_groups sendmsg(2) connect(2). UID 0 CAP_NET_ADMIN netlink. Linux 2.6.13 . PID . Linux / . Linux 3.0 NETLINK_KOBJECT_UEVENT NETLINK_GENERIC NETLINK_ROUTE NETLINK_SELINUX . . netlink getsockopt(2) setsockopt(2) SOL_NETLINK. optval int. NETLINK_PKTINFO ( Linux 2.6.14) nl_pktinfo . NETLINK_ADD_MEMBERSHIP NETLINK_DROP_MEMBERSHIP ( Linux 2.6.14) / optval. NETLINK_LIST_MEMBERSHIPS ( Linux 4.2) . optval __u32 optlen . optlen. NETLINK_BROADCAST_ERROR ( Linux 2.6.30) netlink_broadcast() ESRCH ENOBUFS. NETLINK_NO_ENOBUFS ( 2.6.30) ENOBUFS. NETLINK_LISTEN_ALL_NSID ( 4.2) netlink nsid . nsid . NETLINK_CAP_ACK ( 4.3) . netlink . netlink . netlink 2.2. 2.0 netlink ( ). . netlink libnetlink libnl . . netlink NETLINK_ROUTE RTMGRP_LINK ( /// ) RTMGRP_IPV4_IFADDR ( / IPv4). struct sockaddr_nl sa; memset(&sa, 0, sizeof(sa)); sa.nl_family = AF_NETLINK; sa.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR; fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE); bind(fd, (struct sockaddr *) &sa, sizeof(sa)); netlink (pid 0). . struct nlmsghdr *nh; /* The nlmsghdr with payload to send */ struct sockaddr_nl sa; struct iovec iov = { nh, nh->nlmsg_len }; struct msghdr msg; msg = { &sa, sizeof(sa), &iov, 1, NULL, 0, 0 }; memset(&sa, 0, sizeof(sa)); sa.nl_family = AF_NETLINK; nh->nlmsg_pid = 0; nh->nlmsg_seq = ++sequence_number; /* Request an ack from kernel by setting NLM_F_ACK */ nh->nlmsg_flags |= NLM_F_ACK; sendmsg(fd, &msg, 0); netlink. int size; /* 8192 to avoid message truncation on platforms with page size > 4096 */ struct nlmsghdr buf[8192/sizeof(struct nlmsghdr)]; struct iovec iov = { buf, sizeof(buf) }; struct sockaddr_nl sa; struct msghdr msg; struct nlmsghdr *nh; msg = { &sa, sizeof(sa), &iov, 1, NULL, 0, 0 }; size = recvmsg(fd, &msg, 0); for (nh = (struct nlmsghdr *) buf; NLMSG_OK (nh, size); nh = NLMSG_NEXT (nh, size)) { /* The end of multipart message */ if (nh->nlmsg_type == NLMSG_DONE) return; if (nh->nlmsg_type == NLMSG_ERROR) /* Do some error handling */ ... /* Continue with parsing payload */ ... } cmsg(3), netlink(3), capabilities(7), rtnetlink(7), sock_diag(7) libnetlink libnl RFC 3549 "Linux Netlink as an IP Services Protocol" 3 . . : . 6.18 8 2026 netlink(7)