2011-09-16 17:47:21 +02:00
|
|
|
// Copyright 2011 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Netlink sockets and messages
|
|
|
|
|
|
|
|
package syscall
|
|
|
|
|
2013-01-29 21:52:43 +01:00
|
|
|
import "unsafe"
|
2011-09-16 17:47:21 +02:00
|
|
|
|
|
|
|
// Round the length of a netlink message up to align it properly.
|
|
|
|
func nlmAlignOf(msglen int) int {
|
|
|
|
return (msglen + NLMSG_ALIGNTO - 1) & ^(NLMSG_ALIGNTO - 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Round the length of a netlink route attribute up to align it
|
|
|
|
// properly.
|
|
|
|
func rtaAlignOf(attrlen int) int {
|
|
|
|
return (attrlen + RTA_ALIGNTO - 1) & ^(RTA_ALIGNTO - 1)
|
|
|
|
}
|
|
|
|
|
2013-01-29 21:52:43 +01:00
|
|
|
// NetlinkRouteRequest represents a request message to receive routing
|
|
|
|
// and link states from the kernel.
|
2011-09-16 17:47:21 +02:00
|
|
|
type NetlinkRouteRequest struct {
|
|
|
|
Header NlMsghdr
|
|
|
|
Data RtGenmsg
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rr *NetlinkRouteRequest) toWireFormat() []byte {
|
|
|
|
b := make([]byte, rr.Header.Len)
|
2012-04-20 22:11:28 +02:00
|
|
|
*(*uint32)(unsafe.Pointer(&b[0:4][0])) = rr.Header.Len
|
|
|
|
*(*uint16)(unsafe.Pointer(&b[4:6][0])) = rr.Header.Type
|
|
|
|
*(*uint16)(unsafe.Pointer(&b[6:8][0])) = rr.Header.Flags
|
|
|
|
*(*uint32)(unsafe.Pointer(&b[8:12][0])) = rr.Header.Seq
|
|
|
|
*(*uint32)(unsafe.Pointer(&b[12:16][0])) = rr.Header.Pid
|
|
|
|
b[16] = byte(rr.Data.Family)
|
2011-09-16 17:47:21 +02:00
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func newNetlinkRouteRequest(proto, seq, family int) []byte {
|
|
|
|
rr := &NetlinkRouteRequest{}
|
|
|
|
rr.Header.Len = uint32(NLMSG_HDRLEN + SizeofRtGenmsg)
|
|
|
|
rr.Header.Type = uint16(proto)
|
|
|
|
rr.Header.Flags = NLM_F_DUMP | NLM_F_REQUEST
|
|
|
|
rr.Header.Seq = uint32(seq)
|
|
|
|
rr.Data.Family = uint8(family)
|
|
|
|
return rr.toWireFormat()
|
|
|
|
}
|
|
|
|
|
2013-01-29 21:52:43 +01:00
|
|
|
// NetlinkRIB returns routing information base, as known as RIB, which
|
|
|
|
// consists of network facility information, states and parameters.
|
2011-12-13 00:40:51 +01:00
|
|
|
func NetlinkRIB(proto, family int) ([]byte, error) {
|
2013-01-29 21:52:43 +01:00
|
|
|
s, err := Socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
defer Close(s)
|
2013-01-29 21:52:43 +01:00
|
|
|
lsa := &SockaddrNetlink{Family: AF_NETLINK}
|
|
|
|
if err := Bind(s, lsa); err != nil {
|
|
|
|
return nil, err
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
2013-01-29 21:52:43 +01:00
|
|
|
wb := newNetlinkRouteRequest(proto, 1, family)
|
|
|
|
if err := Sendto(s, wb, 0, lsa); err != nil {
|
|
|
|
return nil, err
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
2013-01-29 21:52:43 +01:00
|
|
|
var tab []byte
|
2015-01-15 01:27:56 +01:00
|
|
|
rbNew := make([]byte, Getpagesize())
|
2013-01-29 21:52:43 +01:00
|
|
|
done:
|
2011-09-16 17:47:21 +02:00
|
|
|
for {
|
2015-01-15 01:27:56 +01:00
|
|
|
rb := rbNew
|
2013-01-29 21:52:43 +01:00
|
|
|
nr, _, err := Recvfrom(s, rb, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
if nr < NLMSG_HDRLEN {
|
|
|
|
return nil, EINVAL
|
|
|
|
}
|
|
|
|
rb = rb[:nr]
|
|
|
|
tab = append(tab, rb...)
|
2013-01-29 21:52:43 +01:00
|
|
|
msgs, err := ParseNetlinkMessage(rb)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
for _, m := range msgs {
|
2013-01-29 21:52:43 +01:00
|
|
|
lsa, err := Getsockname(s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
switch v := lsa.(type) {
|
|
|
|
case *SockaddrNetlink:
|
2013-01-29 21:52:43 +01:00
|
|
|
if m.Header.Seq != 1 || m.Header.Pid != v.Pid {
|
2011-09-16 17:47:21 +02:00
|
|
|
return nil, EINVAL
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return nil, EINVAL
|
|
|
|
}
|
|
|
|
if m.Header.Type == NLMSG_DONE {
|
2013-01-29 21:52:43 +01:00
|
|
|
break done
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
if m.Header.Type == NLMSG_ERROR {
|
|
|
|
return nil, EINVAL
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-12-13 00:40:51 +01:00
|
|
|
return tab, nil
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
|
2013-01-29 21:52:43 +01:00
|
|
|
// NetlinkMessage represents a netlink message.
|
2011-09-16 17:47:21 +02:00
|
|
|
type NetlinkMessage struct {
|
|
|
|
Header NlMsghdr
|
|
|
|
Data []byte
|
|
|
|
}
|
|
|
|
|
2013-01-29 21:52:43 +01:00
|
|
|
// ParseNetlinkMessage parses b as an array of netlink messages and
|
|
|
|
// returns the slice containing the NetlinkMessage structures.
|
|
|
|
func ParseNetlinkMessage(b []byte) ([]NetlinkMessage, error) {
|
|
|
|
var msgs []NetlinkMessage
|
|
|
|
for len(b) >= NLMSG_HDRLEN {
|
|
|
|
h, dbuf, dlen, err := netlinkMessageHeaderAndData(b)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
2013-01-29 21:52:43 +01:00
|
|
|
m := NetlinkMessage{Header: *h, Data: dbuf[:int(h.Len)-NLMSG_HDRLEN]}
|
2011-09-16 17:47:21 +02:00
|
|
|
msgs = append(msgs, m)
|
2013-01-29 21:52:43 +01:00
|
|
|
b = b[dlen:]
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
2013-01-29 21:52:43 +01:00
|
|
|
return msgs, nil
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
|
2013-01-29 21:52:43 +01:00
|
|
|
func netlinkMessageHeaderAndData(b []byte) (*NlMsghdr, []byte, int, error) {
|
|
|
|
h := (*NlMsghdr)(unsafe.Pointer(&b[0]))
|
|
|
|
if int(h.Len) < NLMSG_HDRLEN || int(h.Len) > len(b) {
|
2011-09-16 17:47:21 +02:00
|
|
|
return nil, nil, 0, EINVAL
|
|
|
|
}
|
2013-01-29 21:52:43 +01:00
|
|
|
return h, b[NLMSG_HDRLEN:], nlmAlignOf(int(h.Len)), nil
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
|
2013-01-29 21:52:43 +01:00
|
|
|
// NetlinkRouteAttr represents a netlink route attribute.
|
2011-09-16 17:47:21 +02:00
|
|
|
type NetlinkRouteAttr struct {
|
|
|
|
Attr RtAttr
|
|
|
|
Value []byte
|
|
|
|
}
|
|
|
|
|
2013-01-29 21:52:43 +01:00
|
|
|
// ParseNetlinkRouteAttr parses m's payload as an array of netlink
|
|
|
|
// route attributes and returns the slice containing the
|
|
|
|
// NetlinkRouteAttr structures.
|
|
|
|
func ParseNetlinkRouteAttr(m *NetlinkMessage) ([]NetlinkRouteAttr, error) {
|
|
|
|
var b []byte
|
|
|
|
switch m.Header.Type {
|
2011-10-23 21:04:37 +02:00
|
|
|
case RTM_NEWLINK, RTM_DELLINK:
|
2013-01-29 21:52:43 +01:00
|
|
|
b = m.Data[SizeofIfInfomsg:]
|
2011-10-23 21:04:37 +02:00
|
|
|
case RTM_NEWADDR, RTM_DELADDR:
|
2013-01-29 21:52:43 +01:00
|
|
|
b = m.Data[SizeofIfAddrmsg:]
|
2011-10-23 21:04:37 +02:00
|
|
|
case RTM_NEWROUTE, RTM_DELROUTE:
|
2013-01-29 21:52:43 +01:00
|
|
|
b = m.Data[SizeofRtMsg:]
|
2011-09-16 17:47:21 +02:00
|
|
|
default:
|
|
|
|
return nil, EINVAL
|
|
|
|
}
|
2013-01-29 21:52:43 +01:00
|
|
|
var attrs []NetlinkRouteAttr
|
|
|
|
for len(b) >= SizeofRtAttr {
|
|
|
|
a, vbuf, alen, err := netlinkRouteAttrAndValue(b)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
2013-01-29 21:52:43 +01:00
|
|
|
ra := NetlinkRouteAttr{Attr: *a, Value: vbuf[:int(a.Len)-SizeofRtAttr]}
|
2011-09-16 17:47:21 +02:00
|
|
|
attrs = append(attrs, ra)
|
2013-01-29 21:52:43 +01:00
|
|
|
b = b[alen:]
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
2011-12-13 00:40:51 +01:00
|
|
|
return attrs, nil
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
|
2013-01-29 21:52:43 +01:00
|
|
|
func netlinkRouteAttrAndValue(b []byte) (*RtAttr, []byte, int, error) {
|
|
|
|
a := (*RtAttr)(unsafe.Pointer(&b[0]))
|
|
|
|
if int(a.Len) < SizeofRtAttr || int(a.Len) > len(b) {
|
2011-09-16 17:47:21 +02:00
|
|
|
return nil, nil, 0, EINVAL
|
|
|
|
}
|
2013-01-29 21:52:43 +01:00
|
|
|
return a, b[SizeofRtAttr:], rtaAlignOf(int(a.Len)), nil
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|