1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Netlink sockets and messages
13 // Round the length of a netlink message up to align it properly.
14 func nlmAlignOf(msglen int) int {
15 return (msglen + NLMSG_ALIGNTO - 1) & ^(NLMSG_ALIGNTO - 1)
18 // Round the length of a netlink route attribute up to align it
20 func rtaAlignOf(attrlen int) int {
21 return (attrlen + RTA_ALIGNTO - 1) & ^(RTA_ALIGNTO - 1)
24 // NetlinkRouteRequest represents the request message to receive
25 // routing and link states from the kernel.
26 type NetlinkRouteRequest struct {
31 func (rr *NetlinkRouteRequest) toWireFormat() []byte {
32 b := make([]byte, rr.Header.Len)
33 b[0] = byte(rr.Header.Len)
34 b[1] = byte(rr.Header.Len >> 8)
35 b[2] = byte(rr.Header.Len >> 16)
36 b[3] = byte(rr.Header.Len >> 24)
37 b[4] = byte(rr.Header.Type)
38 b[5] = byte(rr.Header.Type >> 8)
39 b[6] = byte(rr.Header.Flags)
40 b[7] = byte(rr.Header.Flags >> 8)
41 b[8] = byte(rr.Header.Seq)
42 b[9] = byte(rr.Header.Seq >> 8)
43 b[10] = byte(rr.Header.Seq >> 16)
44 b[11] = byte(rr.Header.Seq >> 24)
45 b[12] = byte(rr.Header.Pid)
46 b[13] = byte(rr.Header.Pid >> 8)
47 b[14] = byte(rr.Header.Pid >> 16)
48 b[15] = byte(rr.Header.Pid >> 24)
49 b[16] = byte(rr.Data.Family)
53 func newNetlinkRouteRequest(proto, seq, family int) []byte {
54 rr := &NetlinkRouteRequest{}
55 rr.Header.Len = uint32(NLMSG_HDRLEN + SizeofRtGenmsg)
56 rr.Header.Type = uint16(proto)
57 rr.Header.Flags = NLM_F_DUMP | NLM_F_REQUEST
58 rr.Header.Seq = uint32(seq)
59 rr.Data.Family = uint8(family)
60 return rr.toWireFormat()
63 // NetlinkRIB returns routing information base, as known as RIB,
64 // which consists of network facility information, states and
66 func NetlinkRIB(proto, family int) ([]byte, error) {
72 s, e := Socket(AF_NETLINK, SOCK_RAW, 0)
78 lsanl.Family = AF_NETLINK
85 wb := newNetlinkRouteRequest(proto, seq, family)
86 e = Sendto(s, wb, 0, &lsanl)
98 rb = make([]byte, Getpagesize())
99 nr, _, e = Recvfrom(s, rb, 0)
103 if nr < NLMSG_HDRLEN {
107 tab = append(tab, rb...)
109 msgs, _ := ParseNetlinkMessage(rb)
110 for _, m := range msgs {
111 if lsa, e = Getsockname(s); e != nil {
114 switch v := lsa.(type) {
115 case *SockaddrNetlink:
116 if m.Header.Seq != uint32(seq) || m.Header.Pid != v.Pid {
122 if m.Header.Type == NLMSG_DONE {
125 if m.Header.Type == NLMSG_ERROR {
135 // NetlinkMessage represents the netlink message.
136 type NetlinkMessage struct {
141 // ParseNetlinkMessage parses buf as netlink messages and returns
142 // the slice containing the NetlinkMessage structs.
143 func ParseNetlinkMessage(buf []byte) ([]NetlinkMessage, error) {
149 msgs []NetlinkMessage
152 for len(buf) >= NLMSG_HDRLEN {
153 h, dbuf, dlen, e = netlinkMessageHeaderAndData(buf)
157 m := NetlinkMessage{}
159 m.Data = dbuf[:int(h.Len)-NLMSG_HDRLEN]
160 msgs = append(msgs, m)
167 func netlinkMessageHeaderAndData(buf []byte) (*NlMsghdr, []byte, int, error) {
168 h := (*NlMsghdr)(unsafe.Pointer(&buf[0]))
169 if int(h.Len) < NLMSG_HDRLEN || int(h.Len) > len(buf) {
170 return nil, nil, 0, EINVAL
172 return h, buf[NLMSG_HDRLEN:], nlmAlignOf(int(h.Len)), nil
175 // NetlinkRouteAttr represents the netlink route attribute.
176 type NetlinkRouteAttr struct {
181 // ParseNetlinkRouteAttr parses msg's payload as netlink route
182 // attributes and returns the slice containing the NetlinkRouteAttr
184 func ParseNetlinkRouteAttr(msg *NetlinkMessage) ([]NetlinkRouteAttr, error) {
191 attrs []NetlinkRouteAttr
194 switch msg.Header.Type {
195 case RTM_NEWLINK, RTM_DELLINK:
196 buf = msg.Data[SizeofIfInfomsg:]
197 case RTM_NEWADDR, RTM_DELADDR:
198 buf = msg.Data[SizeofIfAddrmsg:]
199 case RTM_NEWROUTE, RTM_DELROUTE:
200 buf = msg.Data[SizeofRtMsg:]
205 for len(buf) >= SizeofRtAttr {
206 a, vbuf, alen, e = netlinkRouteAttrAndValue(buf)
210 ra := NetlinkRouteAttr{}
212 ra.Value = vbuf[:int(a.Len)-SizeofRtAttr]
213 attrs = append(attrs, ra)
220 func netlinkRouteAttrAndValue(buf []byte) (*RtAttr, []byte, int, error) {
221 h := (*RtAttr)(unsafe.Pointer(&buf[0]))
222 if int(h.Len) < SizeofRtAttr || int(h.Len) > len(buf) {
223 return nil, nil, 0, EINVAL
225 return h, buf[SizeofRtAttr:], rtaAlignOf(int(h.Len)), nil