DESCRIPTION Netlink is used to transfer information between the kernel and user-space processes. It consists of a standard sockets-based interface for user space processes and an internal kernel API for kernel modules. The internal kernel interface is not documented in this manual page. There is also an obsolete netlink interface via netlink character devices; this interface is not docu‐ mented here and is provided only for backward 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 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 2.16.17) Messages from 1-wire subsystem.
NETLINK_USERSOCK Reserved for user-mode socket protocols.
NETLINK_FIREWALL (up to and including Linux 3.4) Transport IPv4 packets from netfilter to user space. Used by ip_queue kernel module. After a long period of being declared obsolete (in favor of the more advanced nfnetlink_queue fea‐ ture), NETLINK_FIREWALL was removed in Linux 3.5.
for _, m := range msgs { switch m.Header.Type { case syscall.NLMSG_DONE: fmt.Println("recv done") goto done case syscall.RTM_NEWROUTE: // 解析数据 } } done:
fmt.Print("Scope ") switch rtmsg.Scope { case syscall.RT_SCOPE_UNIVERSE: fmt.Print("RT_SCOPE_UNIVERSE ") case syscall.RT_SCOPE_SITE: fmt.Print("RT_SCOPE_SITE ") case syscall.RT_SCOPE_LINK: fmt.Print("RT_SCOPE_LINK ") case syscall.RT_SCOPE_HOST: fmt.Print("RT_SCOPE_HOST ") case syscall.RT_SCOPE_NOWHERE: fmt.Print("RT_SCOPE_NOWHERE ") }
fmt.Print(" Protocol ") switch rtmsg.Protocol { case syscall.RTPROT_UNSPEC: fmt.Print("RTPROT_UNSPEC") case syscall.RTPROT_REDIRECT: fmt.Print("RTPROT_REDIRECT") case syscall.RTPROT_KERNEL: fmt.Print("RTPROT_KERNEL") case syscall.RTPROT_BOOT: fmt.Print("RTPROT_BOOT") case syscall.RTPROT_STATIC: fmt.Print("RTPROT_STATIC") }
fmt.Print(" Type ") switch rtmsg.Type { case syscall.RTN_UNSPEC: fmt.Print("RTN_UNSPEC") case syscall.RTN_UNICAST: fmt.Print("RTN_UNICAST") case syscall.RTN_LOCAL: fmt.Print("RTN_LOCAL") case syscall.RTN_BROADCAST: fmt.Print("RTN_BROADCAST") case syscall.RTN_ANYCAST: fmt.Print("RTN_ANYCAST") case syscall.RTN_MULTICAST: fmt.Print("RTN_MULTICAST") case syscall.RTN_BLACKHOLE: fmt.Print("RTN_BLACKHOLE") case syscall.RTN_UNREACHABLE: fmt.Print("RTN_UNREACHABLE") case syscall.RTN_PROHIBIT: fmt.Print("RTN_PROHIBIT") case syscall.RTN_THROW: fmt.Print("RTN_THROW") case syscall.RTN_NAT: fmt.Print("RTN_NAT") case syscall.RTN_XRESOLVE: fmt.Print("RTN_XRESOLVE") } fmt.Print(" Family ") switch rtmsg.Family { case syscall.AF_INET: fmt.Print("AF_INET") case syscall.AF_INET6: fmt.Print("AF_INET6") }