netlink(7) Miscellaneous Information Manual netlink(7) netlink - (AF_NETLINK) #include #include #include netlink_socket = socket(AF_NETLINK, socket_type, netlink_family); netlink . , , API , . . , netlink, netlink. ; . Netlink . socket_type SOCK_RAW, SOCK_DGRAM. , netlink (raw) . netlink_family netlink . netlink: NETLINK_ROUTE Receives routing and link updates and may be used to modify the routing tables (both IPv4 and IPv6), IP addresses, link parameters, neighbor setups, queueing disciplines, traffic classes, and packet classifiers (see rtnetlink(7)). NETLINK_W1 (Linux 2.6.13 to Linux 2.16.17) . NETLINK_USERSOCK . NETLINK_FIREWALL ( Linux 3.4 ) IPv4 netfilter . ip_queue. ( nfnetlink_queue), NETLINK_FIREWALL Linux 3.5. NETLINK_SOCK_DIAG ( Linux 3.3) ( sock_diag(7)). NETLINK_INET_DIAG ( Linux 2.6.14) An obsolete synonym for NETLINK_SOCK_DIAG. NETLINK_NFLOG ( Linux 3.16 ) Netfilter/iptables ULOG. NETLINK_XFRM IPsec. NETLINK_SELINUX ( Linux 2.6.4) SELinux. NETLINK_ISCSI ( Linux 2.6.15) Open-iSCSI. NETLINK_AUDIT ( Linux 2.6.6) . NETLINK_FIB_LOOKUP ( Linux 2.6.13) FIB . NETLINK_CONNECTOR ( Linux 2.6.14) Kernel connector. See Documentation/driver-api/connector.rst (or /Documentation/connector/connector.* in Linux 5.2 and earlier) in the Linux kernel source tree for further information. NETLINK_NETFILTER ( Linux 2.6.14) netfilter. NETLINK_SCSITRANSPORT ( Linux 2.6.19) SCSI. NETLINK_RDMA ( Linux 3.0) Infiniband RDMA. NETLINK_IP6_FW ( Linux 3.4 ) IPv6 netfilter . ip6_queue. NETLINK_DNRTMSG DECnet. NETLINK_KOBJECT_UEVENT ( Linux 2.6.10) . NETLINK_GENERIC ( Linux 2.6.15) netlink netlink. NETLINK_CRYPTO ( Linux 3.2) netlink , . netlink nlmsghdr . NLMSG_*. netlink(3). , ( nlmsghdr ), NLM_F_MULTI. NLMSG_DONE . nlmsghdr : struct nlmsghdr { __u32 nlmsg_len; /* , */ __u16 nlmsg_type; /* */ __u16 nlmsg_flags; /* */ __u32 nlmsg_seq; /* */ __u32 nlmsg_pid; /* ID */ }; nlmsg_type can be one of the standard message types: NLMSG_NOOP message is to be ignored, NLMSG_ERROR message signals an error and the payload contains an nlmsgerr structure, NLMSG_DONE message terminates a multipart message. Error messages get the original request appended, unless the user requests to cap the error message, and get extra error data if requested. 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 length 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 Request for an acknowledgement on success. NLM_F_ECHO . GET ------------------------------------------------------------------------ NLM_F_ROOT . NLM_F_MATCH , , . . NLM_F_ATOMIC (snapshot) . NLM_F_DUMP Convenience macro; equivalent to (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. . nlmsg_seq nlmsg_pid netlink. 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 acknowledgement is an NLMSG_ERROR packet with the error field set to 0. The application must generate acknowledgements 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. , . netlink, : . ( ENOBUFS, recvmsg(2)) . sockaddr_nl netlink . ( ), netlink ( nl_groups ). struct sockaddr_nl { sa_family_t nl_family; /* AF_NETLINK */ unsigned short nl_pad; /* */ pid_t nl_pid; /* ID */ __u32 nl_groups; /* */ }; nl_pid -- netlink. 0, . nl_pid, , PID , . , nl_pid netlink, . netlink, nl_pid ID . nl_pid netlink. nl_pid bind(2), , nl_pid . 0, . netlink ID , , netlink, nl_pid. nl_groups -- , netlink. netlink 32- . bind(2), nl_groups sockaddr_nl , . , , . , nl_groups , sendmsg(2) connect(2). netlink UID, 0, CAP_NET_ADMIN. 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 (since Linux 2.6.14) / , optval. NETLINK_LIST_MEMBERSHIPS ( Linux 4.2) , optval __u32, optlen -- . , optlen. NETLINK_BROADCAST_ERROR ( Linux 2.6.30) When not set, netlink_broadcast() only reports ESRCH errors and silently ignore ENOBUFS errors. NETLINK_NO_ENOBUFS ( Linux 2.6.30) , ENOBUFS. NETLINK_LISTEN_ALL_NSID ( Linux 4.2) , netlink , nsid , . nsid . NETLINK_CAP_ACK (since Linux 4.3) The kernel may fail to allocate the necessary room for the acknowledgement message back to user space. This option trims off the payload of the original netlink message. The netlink message header is still included, so the user can guess from the sequence number which message triggered the acknowledgement. netlink Linux 2.2. Linux 2.0 ( ). . 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 len; /* 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 }; len = recvmsg(fd, &msg, 0); for (nh = (struct nlmsghdr *) buf; NLMSG_OK (nh, len); nh = NLMSG_NEXT (nh, len)) { /* 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 information about libnl RFC 3549 <> Alex Nik , Azamat Hackimov , Dmitry Bolkhovskikh , Yuri Kozlov ; GNU 3 , . . , , . Linux man-pages 6.06 31 2023 . netlink(7)