OSDN Git Service

1c99a81785f0ce84cb0e11c016c478e7d37b6d30
[pf3gnuchains/gcc-fork.git] / libgo / go / syscall / netlink_linux.go
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.
4
5 // Netlink sockets and messages
6
7 package syscall
8
9 import (
10         "unsafe"
11 )
12
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)
16 }
17
18 // Round the length of a netlink route attribute up to align it
19 // properly.
20 func rtaAlignOf(attrlen int) int {
21         return (attrlen + RTA_ALIGNTO - 1) & ^(RTA_ALIGNTO - 1)
22 }
23
24 // NetlinkRouteRequest represents the request message to receive
25 // routing and link states from the kernel.
26 type NetlinkRouteRequest struct {
27         Header NlMsghdr
28         Data   RtGenmsg
29 }
30
31 func (rr *NetlinkRouteRequest) toWireFormat() []byte {
32         b := make([]byte, rr.Header.Len)
33         if BigEndian {
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)
51         } else {
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)
69         }
70         return b
71 }
72
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()
81 }
82
83 // NetlinkRIB returns routing information base, as known as RIB,
84 // which consists of network facility information, states and
85 // parameters.
86 func NetlinkRIB(proto, family int) ([]byte, error) {
87         var (
88                 lsanl SockaddrNetlink
89                 tab   []byte
90         )
91
92         s, e := Socket(AF_NETLINK, SOCK_RAW, 0)
93         if e != nil {
94                 return nil, e
95         }
96         defer Close(s)
97
98         lsanl.Family = AF_NETLINK
99         e = Bind(s, &lsanl)
100         if e != nil {
101                 return nil, e
102         }
103
104         seq := 1
105         wb := newNetlinkRouteRequest(proto, seq, family)
106         e = Sendto(s, wb, 0, &lsanl)
107         if e != nil {
108                 return nil, e
109         }
110
111         for {
112                 var (
113                         rb  []byte
114                         nr  int
115                         lsa Sockaddr
116                 )
117
118                 rb = make([]byte, Getpagesize())
119                 nr, _, e = Recvfrom(s, rb, 0)
120                 if e != nil {
121                         return nil, e
122                 }
123                 if nr < NLMSG_HDRLEN {
124                         return nil, EINVAL
125                 }
126                 rb = rb[:nr]
127                 tab = append(tab, rb...)
128
129                 msgs, _ := ParseNetlinkMessage(rb)
130                 for _, m := range msgs {
131                         if lsa, e = Getsockname(s); e != nil {
132                                 return nil, e
133                         }
134                         switch v := lsa.(type) {
135                         case *SockaddrNetlink:
136                                 if m.Header.Seq != uint32(seq) || m.Header.Pid != v.Pid {
137                                         return nil, EINVAL
138                                 }
139                         default:
140                                 return nil, EINVAL
141                         }
142                         if m.Header.Type == NLMSG_DONE {
143                                 goto done
144                         }
145                         if m.Header.Type == NLMSG_ERROR {
146                                 return nil, EINVAL
147                         }
148                 }
149         }
150
151 done:
152         return tab, nil
153 }
154
155 // NetlinkMessage represents the netlink message.
156 type NetlinkMessage struct {
157         Header NlMsghdr
158         Data   []byte
159 }
160
161 // ParseNetlinkMessage parses buf as netlink messages and returns
162 // the slice containing the NetlinkMessage structs.
163 func ParseNetlinkMessage(buf []byte) ([]NetlinkMessage, error) {
164         var (
165                 h    *NlMsghdr
166                 dbuf []byte
167                 dlen int
168                 e    error
169                 msgs []NetlinkMessage
170         )
171
172         for len(buf) >= NLMSG_HDRLEN {
173                 h, dbuf, dlen, e = netlinkMessageHeaderAndData(buf)
174                 if e != nil {
175                         break
176                 }
177                 m := NetlinkMessage{}
178                 m.Header = *h
179                 m.Data = dbuf[:int(h.Len)-NLMSG_HDRLEN]
180                 msgs = append(msgs, m)
181                 buf = buf[dlen:]
182         }
183
184         return msgs, e
185 }
186
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
191         }
192         return h, buf[NLMSG_HDRLEN:], nlmAlignOf(int(h.Len)), nil
193 }
194
195 // NetlinkRouteAttr represents the netlink route attribute.
196 type NetlinkRouteAttr struct {
197         Attr  RtAttr
198         Value []byte
199 }
200
201 // ParseNetlinkRouteAttr parses msg's payload as netlink route
202 // attributes and returns the slice containing the NetlinkRouteAttr
203 // structs.
204 func ParseNetlinkRouteAttr(msg *NetlinkMessage) ([]NetlinkRouteAttr, error) {
205         var (
206                 buf   []byte
207                 a     *RtAttr
208                 alen  int
209                 vbuf  []byte
210                 e     error
211                 attrs []NetlinkRouteAttr
212         )
213
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:]
221         default:
222                 return nil, EINVAL
223         }
224
225         for len(buf) >= SizeofRtAttr {
226                 a, vbuf, alen, e = netlinkRouteAttrAndValue(buf)
227                 if e != nil {
228                         break
229                 }
230                 ra := NetlinkRouteAttr{}
231                 ra.Attr = *a
232                 ra.Value = vbuf[:int(a.Len)-SizeofRtAttr]
233                 attrs = append(attrs, ra)
234                 buf = buf[alen:]
235         }
236
237         return attrs, nil
238 }
239
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
244         }
245         return h, buf[SizeofRtAttr:], rtaAlignOf(int(h.Len)), nil
246 }