blob: 1b73dce8274355b5c153d2faaf559e40637227bf [file] [log] [blame]
Mikio Hara4d118832011-05-26 17:04:58 -04001// 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.
4
5// Netlink sockets and messages
6
7package syscall
8
Mikio Haraa0509d82013-01-14 19:29:03 +09009import "unsafe"
Mikio Hara4d118832011-05-26 17:04:58 -040010
11// Round the length of a netlink message up to align it properly.
12func nlmAlignOf(msglen int) int {
13 return (msglen + NLMSG_ALIGNTO - 1) & ^(NLMSG_ALIGNTO - 1)
14}
15
16// Round the length of a netlink route attribute up to align it
17// properly.
18func rtaAlignOf(attrlen int) int {
19 return (attrlen + RTA_ALIGNTO - 1) & ^(RTA_ALIGNTO - 1)
20}
21
Mikio Haraa0509d82013-01-14 19:29:03 +090022// NetlinkRouteRequest represents a request message to receive routing
23// and link states from the kernel.
Mikio Hara4d118832011-05-26 17:04:58 -040024type NetlinkRouteRequest struct {
25 Header NlMsghdr
26 Data RtGenmsg
27}
28
29func (rr *NetlinkRouteRequest) toWireFormat() []byte {
30 b := make([]byte, rr.Header.Len)
Ian Lance Taylor35bc9d12012-04-04 17:41:36 -070031 *(*uint32)(unsafe.Pointer(&b[0:4][0])) = rr.Header.Len
32 *(*uint16)(unsafe.Pointer(&b[4:6][0])) = rr.Header.Type
33 *(*uint16)(unsafe.Pointer(&b[6:8][0])) = rr.Header.Flags
34 *(*uint32)(unsafe.Pointer(&b[8:12][0])) = rr.Header.Seq
35 *(*uint32)(unsafe.Pointer(&b[12:16][0])) = rr.Header.Pid
Mikio Hara4d118832011-05-26 17:04:58 -040036 b[16] = byte(rr.Data.Family)
37 return b
38}
39
40func newNetlinkRouteRequest(proto, seq, family int) []byte {
41 rr := &NetlinkRouteRequest{}
Ian Lance Taylor35bc9d12012-04-04 17:41:36 -070042 rr.Header.Len = uint32(NLMSG_HDRLEN + SizeofRtGenmsg)
Mikio Hara4d118832011-05-26 17:04:58 -040043 rr.Header.Type = uint16(proto)
44 rr.Header.Flags = NLM_F_DUMP | NLM_F_REQUEST
45 rr.Header.Seq = uint32(seq)
46 rr.Data.Family = uint8(family)
47 return rr.toWireFormat()
48}
49
Mikio Haraa0509d82013-01-14 19:29:03 +090050// NetlinkRIB returns routing information base, as known as RIB, which
51// consists of network facility information, states and parameters.
Russ Coxc017a822011-11-13 22:44:52 -050052func NetlinkRIB(proto, family int) ([]byte, error) {
Mikio Haraa0509d82013-01-14 19:29:03 +090053 s, err := Socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)
54 if err != nil {
55 return nil, err
Mikio Hara4d118832011-05-26 17:04:58 -040056 }
57 defer Close(s)
Mikio Haraa0509d82013-01-14 19:29:03 +090058 lsa := &SockaddrNetlink{Family: AF_NETLINK}
59 if err := Bind(s, lsa); err != nil {
60 return nil, err
Mikio Hara4d118832011-05-26 17:04:58 -040061 }
Mikio Haraa0509d82013-01-14 19:29:03 +090062 wb := newNetlinkRouteRequest(proto, 1, family)
63 if err := Sendto(s, wb, 0, lsa); err != nil {
64 return nil, err
Mikio Hara4d118832011-05-26 17:04:58 -040065 }
Mikio Haraa0509d82013-01-14 19:29:03 +090066 var tab []byte
Cristian Staretu8aea9a02014-07-09 18:50:38 +100067 rbNew := make([]byte, Getpagesize())
Mikio Haraa0509d82013-01-14 19:29:03 +090068done:
Mikio Hara4d118832011-05-26 17:04:58 -040069 for {
Cristian Staretu8aea9a02014-07-09 18:50:38 +100070 rb := rbNew
Mikio Haraa0509d82013-01-14 19:29:03 +090071 nr, _, err := Recvfrom(s, rb, 0)
72 if err != nil {
73 return nil, err
Mikio Hara4d118832011-05-26 17:04:58 -040074 }
75 if nr < NLMSG_HDRLEN {
76 return nil, EINVAL
77 }
78 rb = rb[:nr]
79 tab = append(tab, rb...)
Mikio Haraa0509d82013-01-14 19:29:03 +090080 msgs, err := ParseNetlinkMessage(rb)
81 if err != nil {
82 return nil, err
83 }
Mikio Hara4d118832011-05-26 17:04:58 -040084 for _, m := range msgs {
Mikio Haraa0509d82013-01-14 19:29:03 +090085 lsa, err := Getsockname(s)
86 if err != nil {
87 return nil, err
Mikio Hara4d118832011-05-26 17:04:58 -040088 }
89 switch v := lsa.(type) {
90 case *SockaddrNetlink:
Mikio Haraa0509d82013-01-14 19:29:03 +090091 if m.Header.Seq != 1 || m.Header.Pid != v.Pid {
Mikio Hara4d118832011-05-26 17:04:58 -040092 return nil, EINVAL
93 }
94 default:
95 return nil, EINVAL
96 }
97 if m.Header.Type == NLMSG_DONE {
Mikio Haraa0509d82013-01-14 19:29:03 +090098 break done
Mikio Hara4d118832011-05-26 17:04:58 -040099 }
100 if m.Header.Type == NLMSG_ERROR {
101 return nil, EINVAL
102 }
103 }
104 }
Russ Coxc017a822011-11-13 22:44:52 -0500105 return tab, nil
Mikio Hara4d118832011-05-26 17:04:58 -0400106}
107
Mikio Haraa0509d82013-01-14 19:29:03 +0900108// NetlinkMessage represents a netlink message.
Mikio Hara4d118832011-05-26 17:04:58 -0400109type NetlinkMessage struct {
110 Header NlMsghdr
111 Data []byte
112}
113
Mikio Haraa0509d82013-01-14 19:29:03 +0900114// ParseNetlinkMessage parses b as an array of netlink messages and
115// returns the slice containing the NetlinkMessage structures.
116func ParseNetlinkMessage(b []byte) ([]NetlinkMessage, error) {
117 var msgs []NetlinkMessage
118 for len(b) >= NLMSG_HDRLEN {
119 h, dbuf, dlen, err := netlinkMessageHeaderAndData(b)
120 if err != nil {
121 return nil, err
Mikio Hara4d118832011-05-26 17:04:58 -0400122 }
Mikio Haraa0509d82013-01-14 19:29:03 +0900123 m := NetlinkMessage{Header: *h, Data: dbuf[:int(h.Len)-NLMSG_HDRLEN]}
Mikio Hara4d118832011-05-26 17:04:58 -0400124 msgs = append(msgs, m)
Mikio Haraa0509d82013-01-14 19:29:03 +0900125 b = b[dlen:]
Mikio Hara4d118832011-05-26 17:04:58 -0400126 }
Mikio Haraa0509d82013-01-14 19:29:03 +0900127 return msgs, nil
Mikio Hara4d118832011-05-26 17:04:58 -0400128}
129
Mikio Haraa0509d82013-01-14 19:29:03 +0900130func netlinkMessageHeaderAndData(b []byte) (*NlMsghdr, []byte, int, error) {
131 h := (*NlMsghdr)(unsafe.Pointer(&b[0]))
132 if int(h.Len) < NLMSG_HDRLEN || int(h.Len) > len(b) {
Mikio Hara4d118832011-05-26 17:04:58 -0400133 return nil, nil, 0, EINVAL
134 }
Mikio Haraa0509d82013-01-14 19:29:03 +0900135 return h, b[NLMSG_HDRLEN:], nlmAlignOf(int(h.Len)), nil
Mikio Hara4d118832011-05-26 17:04:58 -0400136}
137
Mikio Haraa0509d82013-01-14 19:29:03 +0900138// NetlinkRouteAttr represents a netlink route attribute.
Mikio Hara4d118832011-05-26 17:04:58 -0400139type NetlinkRouteAttr struct {
140 Attr RtAttr
141 Value []byte
142}
143
Mikio Haraa0509d82013-01-14 19:29:03 +0900144// ParseNetlinkRouteAttr parses m's payload as an array of netlink
145// route attributes and returns the slice containing the
146// NetlinkRouteAttr structures.
147func ParseNetlinkRouteAttr(m *NetlinkMessage) ([]NetlinkRouteAttr, error) {
148 var b []byte
149 switch m.Header.Type {
Mikio Hara73d27dd2011-09-05 08:11:51 -0400150 case RTM_NEWLINK, RTM_DELLINK:
Mikio Haraa0509d82013-01-14 19:29:03 +0900151 b = m.Data[SizeofIfInfomsg:]
Mikio Hara73d27dd2011-09-05 08:11:51 -0400152 case RTM_NEWADDR, RTM_DELADDR:
Mikio Haraa0509d82013-01-14 19:29:03 +0900153 b = m.Data[SizeofIfAddrmsg:]
Mikio Hara73d27dd2011-09-05 08:11:51 -0400154 case RTM_NEWROUTE, RTM_DELROUTE:
Mikio Haraa0509d82013-01-14 19:29:03 +0900155 b = m.Data[SizeofRtMsg:]
Mikio Hara4d118832011-05-26 17:04:58 -0400156 default:
157 return nil, EINVAL
158 }
Mikio Haraa0509d82013-01-14 19:29:03 +0900159 var attrs []NetlinkRouteAttr
160 for len(b) >= SizeofRtAttr {
161 a, vbuf, alen, err := netlinkRouteAttrAndValue(b)
162 if err != nil {
163 return nil, err
Mikio Hara4d118832011-05-26 17:04:58 -0400164 }
Mikio Haraa0509d82013-01-14 19:29:03 +0900165 ra := NetlinkRouteAttr{Attr: *a, Value: vbuf[:int(a.Len)-SizeofRtAttr]}
Mikio Hara4d118832011-05-26 17:04:58 -0400166 attrs = append(attrs, ra)
Mikio Haraa0509d82013-01-14 19:29:03 +0900167 b = b[alen:]
Mikio Hara4d118832011-05-26 17:04:58 -0400168 }
Russ Coxc017a822011-11-13 22:44:52 -0500169 return attrs, nil
Mikio Hara4d118832011-05-26 17:04:58 -0400170}
171
Mikio Haraa0509d82013-01-14 19:29:03 +0900172func netlinkRouteAttrAndValue(b []byte) (*RtAttr, []byte, int, error) {
173 a := (*RtAttr)(unsafe.Pointer(&b[0]))
174 if int(a.Len) < SizeofRtAttr || int(a.Len) > len(b) {
Mikio Hara4d118832011-05-26 17:04:58 -0400175 return nil, nil, 0, EINVAL
176 }
Mikio Haraa0509d82013-01-14 19:29:03 +0900177 return a, b[SizeofRtAttr:], rtaAlignOf(int(a.Len)), nil
Mikio Hara4d118832011-05-26 17:04:58 -0400178}