Source file
src/syscall/netlink_linux.go
1
2
3
4
5
6
7 package syscall
8
9 import "unsafe"
10
11
12 func nlmAlignOf(msglen int) int {
13 return (msglen + NLMSG_ALIGNTO - 1) & ^(NLMSG_ALIGNTO - 1)
14 }
15
16
17
18 func rtaAlignOf(attrlen int) int {
19 return (attrlen + RTA_ALIGNTO - 1) & ^(RTA_ALIGNTO - 1)
20 }
21
22
23
24 type NetlinkRouteRequest struct {
25 Header NlMsghdr
26 Data RtGenmsg
27 }
28
29 func (rr *NetlinkRouteRequest) toWireFormat() []byte {
30 b := make([]byte, rr.Header.Len)
31 *(*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
36 b[16] = byte(rr.Data.Family)
37 return b
38 }
39
40 func newNetlinkRouteRequest(proto, seq, family int) []byte {
41 rr := &NetlinkRouteRequest{}
42 rr.Header.Len = uint32(NLMSG_HDRLEN + SizeofRtGenmsg)
43 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
50
51
52 func NetlinkRIB(proto, family int) ([]byte, error) {
53 s, err := cloexecSocket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)
54 if err != nil {
55 return nil, err
56 }
57 defer Close(s)
58 sa := &SockaddrNetlink{Family: AF_NETLINK}
59 if err := Bind(s, sa); err != nil {
60 return nil, err
61 }
62 wb := newNetlinkRouteRequest(proto, 1, family)
63 if err := Sendto(s, wb, 0, sa); err != nil {
64 return nil, err
65 }
66 lsa, err := Getsockname(s)
67 if err != nil {
68 return nil, err
69 }
70 lsanl, ok := lsa.(*SockaddrNetlink)
71 if !ok {
72 return nil, EINVAL
73 }
74 var tab []byte
75 rbNew := make([]byte, Getpagesize())
76 done:
77 for {
78 rb := rbNew
79 nr, _, err := Recvfrom(s, rb, 0)
80 if err != nil {
81 return nil, err
82 }
83 if nr < NLMSG_HDRLEN {
84 return nil, EINVAL
85 }
86 rb = rb[:nr]
87 tab = append(tab, rb...)
88 msgs, err := ParseNetlinkMessage(rb)
89 if err != nil {
90 return nil, err
91 }
92 for _, m := range msgs {
93 if m.Header.Seq != 1 || m.Header.Pid != lsanl.Pid {
94 return nil, EINVAL
95 }
96 if m.Header.Type == NLMSG_DONE {
97 break done
98 }
99 if m.Header.Type == NLMSG_ERROR {
100 return nil, EINVAL
101 }
102 }
103 }
104 return tab, nil
105 }
106
107
108 type NetlinkMessage struct {
109 Header NlMsghdr
110 Data []byte
111 }
112
113
114
115 func ParseNetlinkMessage(b []byte) ([]NetlinkMessage, error) {
116 var msgs []NetlinkMessage
117 for len(b) >= NLMSG_HDRLEN {
118 h, dbuf, dlen, err := netlinkMessageHeaderAndData(b)
119 if err != nil {
120 return nil, err
121 }
122 m := NetlinkMessage{Header: *h, Data: dbuf[:int(h.Len)-NLMSG_HDRLEN]}
123 msgs = append(msgs, m)
124 b = b[dlen:]
125 }
126 return msgs, nil
127 }
128
129 func netlinkMessageHeaderAndData(b []byte) (*NlMsghdr, []byte, int, error) {
130 h := (*NlMsghdr)(unsafe.Pointer(&b[0]))
131 l := nlmAlignOf(int(h.Len))
132 if int(h.Len) < NLMSG_HDRLEN || l > len(b) {
133 return nil, nil, 0, EINVAL
134 }
135 return h, b[NLMSG_HDRLEN:], l, nil
136 }
137
138
139 type NetlinkRouteAttr struct {
140 Attr RtAttr
141 Value []byte
142 }
143
144
145
146
147 func ParseNetlinkRouteAttr(m *NetlinkMessage) ([]NetlinkRouteAttr, error) {
148 var b []byte
149 switch m.Header.Type {
150 case RTM_NEWLINK, RTM_DELLINK:
151 b = m.Data[SizeofIfInfomsg:]
152 case RTM_NEWADDR, RTM_DELADDR:
153 b = m.Data[SizeofIfAddrmsg:]
154 case RTM_NEWROUTE, RTM_DELROUTE:
155 b = m.Data[SizeofRtMsg:]
156 default:
157 return nil, EINVAL
158 }
159 var attrs []NetlinkRouteAttr
160 for len(b) >= SizeofRtAttr {
161 a, vbuf, alen, err := netlinkRouteAttrAndValue(b)
162 if err != nil {
163 return nil, err
164 }
165 ra := NetlinkRouteAttr{Attr: *a, Value: vbuf[:int(a.Len)-SizeofRtAttr]}
166 attrs = append(attrs, ra)
167 b = b[alen:]
168 }
169 return attrs, nil
170 }
171
172 func 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) {
175 return nil, nil, 0, EINVAL
176 }
177 return a, b[SizeofRtAttr:], rtaAlignOf(int(a.Len)), nil
178 }
179
View as plain text