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)
34 b[0] = byte(rr.Header.Len >> 24)
35 b[1] = byte(rr.Header.Len >> 16)
36 b[2] = byte(rr.Header.Len >> 8)
37 b[3] = byte(rr.Header.Len)
38 b[4] = byte(rr.Header.Type >> 8)
39 b[5] = byte(rr.Header.Type)
40 b[6] = byte(rr.Header.Flags >> 8)
41 b[7] = byte(rr.Header.Flags)
42 b[8] = byte(rr.Header.Seq >> 24)
43 b[9] = byte(rr.Header.Seq >> 16)
44 b[10] = byte(rr.Header.Seq >> 8)
45 b[11] = byte(rr.Header.Seq)
46 b[12] = byte(rr.Header.Pid >> 24)
47 b[13] = byte(rr.Header.Pid >> 16)
48 b[14] = byte(rr.Header.Pid >> 8)
49 b[15] = byte(rr.Header.Pid)
50 b[16] = byte(rr.Data.Family)
52 b[0] = byte(rr.Header.Len)
53 b[1] = byte(rr.Header.Len >> 8)
54 b[2] = byte(rr.Header.Len >> 16)
55 b[3] = byte(rr.Header.Len >> 24)
56 b[4] = byte(rr.Header.Type)
57 b[5] = byte(rr.Header.Type >> 8)
58 b[6] = byte(rr.Header.Flags)
59 b[7] = byte(rr.Header.Flags >> 8)
60 b[8] = byte(rr.Header.Seq)
61 b[9] = byte(rr.Header.Seq >> 8)
62 b[10] = byte(rr.Header.Seq >> 16)
63 b[11] = byte(rr.Header.Seq >> 24)
64 b[12] = byte(rr.Header.Pid)
65 b[13] = byte(rr.Header.Pid >> 8)
66 b[14] = byte(rr.Header.Pid >> 16)
67 b[15] = byte(rr.Header.Pid >> 24)
68 b[16] = byte(rr.Data.Family)
73 func newNetlinkRouteRequest(proto, seq, family int) []byte {
74 rr := &NetlinkRouteRequest{}
75 rr.Header.Len = uint32(NLMSG_HDRLEN + SizeofRtGenmsg)
76 rr.Header.Type = uint16(proto)
77 rr.Header.Flags = NLM_F_DUMP | NLM_F_REQUEST
78 rr.Header.Seq = uint32(seq)
79 rr.Data.Family = uint8(family)
80 return rr.toWireFormat()
83 // NetlinkRIB returns routing information base, as known as RIB,
84 // which consists of network facility information, states and
86 func NetlinkRIB(proto, family int) ([]byte, error) {
92 s, e := Socket(AF_NETLINK, SOCK_RAW, 0)
98 lsanl.Family = AF_NETLINK
105 wb := newNetlinkRouteRequest(proto, seq, family)
106 e = Sendto(s, wb, 0, &lsanl)
118 rb = make([]byte, Getpagesize())
119 nr, _, e = Recvfrom(s, rb, 0)
123 if nr < NLMSG_HDRLEN {
127 tab = append(tab, rb...)
129 msgs, _ := ParseNetlinkMessage(rb)
130 for _, m := range msgs {
131 if lsa, e = Getsockname(s); e != nil {
134 switch v := lsa.(type) {
135 case *SockaddrNetlink:
136 if m.Header.Seq != uint32(seq) || m.Header.Pid != v.Pid {
142 if m.Header.Type == NLMSG_DONE {
145 if m.Header.Type == NLMSG_ERROR {
155 // NetlinkMessage represents the netlink message.
156 type NetlinkMessage struct {
161 // ParseNetlinkMessage parses buf as netlink messages and returns
162 // the slice containing the NetlinkMessage structs.
163 func ParseNetlinkMessage(buf []byte) ([]NetlinkMessage, error) {
169 msgs []NetlinkMessage
172 for len(buf) >= NLMSG_HDRLEN {
173 h, dbuf, dlen, e = netlinkMessageHeaderAndData(buf)
177 m := NetlinkMessage{}
179 m.Data = dbuf[:int(h.Len)-NLMSG_HDRLEN]
180 msgs = append(msgs, m)
187 func netlinkMessageHeaderAndData(buf []byte) (*NlMsghdr, []byte, int, error) {
188 h := (*NlMsghdr)(unsafe.Pointer(&buf[0]))
189 if int(h.Len) < NLMSG_HDRLEN || int(h.Len) > len(buf) {
190 return nil, nil, 0, EINVAL
192 return h, buf[NLMSG_HDRLEN:], nlmAlignOf(int(h.Len)), nil
195 // NetlinkRouteAttr represents the netlink route attribute.
196 type NetlinkRouteAttr struct {
201 // ParseNetlinkRouteAttr parses msg's payload as netlink route
202 // attributes and returns the slice containing the NetlinkRouteAttr
204 func ParseNetlinkRouteAttr(msg *NetlinkMessage) ([]NetlinkRouteAttr, error) {
211 attrs []NetlinkRouteAttr
214 switch msg.Header.Type {
215 case RTM_NEWLINK, RTM_DELLINK:
216 buf = msg.Data[SizeofIfInfomsg:]
217 case RTM_NEWADDR, RTM_DELADDR:
218 buf = msg.Data[SizeofIfAddrmsg:]
219 case RTM_NEWROUTE, RTM_DELROUTE:
220 buf = msg.Data[SizeofRtMsg:]
225 for len(buf) >= SizeofRtAttr {
226 a, vbuf, alen, e = netlinkRouteAttrAndValue(buf)
230 ra := NetlinkRouteAttr{}
232 ra.Value = vbuf[:int(a.Len)-SizeofRtAttr]
233 attrs = append(attrs, ra)
240 func netlinkRouteAttrAndValue(buf []byte) (*RtAttr, []byte, int, error) {
241 h := (*RtAttr)(unsafe.Pointer(&buf[0]))
242 if int(h.Len) < SizeofRtAttr || int(h.Len) > len(buf) {
243 return nil, nil, 0, EINVAL
245 return h, buf[SizeofRtAttr:], rtaAlignOf(int(h.Len)), nil