2014-01-31 10:16:42 +00:00
|
|
|
// +build amd64
|
|
|
|
|
2013-12-23 23:39:39 +00:00
|
|
|
package netlink
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
2014-03-31 21:02:42 +00:00
|
|
|
"math/rand"
|
2013-12-23 23:39:39 +00:00
|
|
|
"net"
|
|
|
|
"syscall"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
2014-02-10 19:36:23 +00:00
|
|
|
const (
|
|
|
|
IFNAMSIZ = 16
|
|
|
|
DEFAULT_CHANGE = 0xFFFFFFFF
|
|
|
|
IFLA_INFO_KIND = 1
|
2014-02-10 21:37:16 +00:00
|
|
|
IFLA_INFO_DATA = 2
|
2014-02-11 06:32:07 +00:00
|
|
|
VETH_INFO_PEER = 1
|
|
|
|
IFLA_NET_NS_FD = 28
|
2014-03-31 21:02:42 +00:00
|
|
|
SIOC_BRADDBR = 0x89a0
|
2014-02-10 19:36:23 +00:00
|
|
|
)
|
|
|
|
|
2013-12-23 23:39:39 +00:00
|
|
|
var nextSeqNr int
|
|
|
|
|
2014-03-31 21:02:42 +00:00
|
|
|
type ifreqHwaddr struct {
|
|
|
|
IfrnName [16]byte
|
|
|
|
IfruHwaddr syscall.RawSockaddr
|
|
|
|
}
|
|
|
|
|
2013-12-23 23:39:39 +00:00
|
|
|
func nativeEndian() binary.ByteOrder {
|
|
|
|
var x uint32 = 0x01020304
|
|
|
|
if *(*byte)(unsafe.Pointer(&x)) == 0x01 {
|
|
|
|
return binary.BigEndian
|
|
|
|
}
|
|
|
|
return binary.LittleEndian
|
|
|
|
}
|
|
|
|
|
|
|
|
func getSeq() int {
|
|
|
|
nextSeqNr = nextSeqNr + 1
|
|
|
|
return nextSeqNr
|
|
|
|
}
|
|
|
|
|
|
|
|
func getIpFamily(ip net.IP) int {
|
|
|
|
if len(ip) <= net.IPv4len {
|
|
|
|
return syscall.AF_INET
|
|
|
|
}
|
|
|
|
if ip.To4() != nil {
|
|
|
|
return syscall.AF_INET
|
|
|
|
}
|
|
|
|
return syscall.AF_INET6
|
|
|
|
}
|
|
|
|
|
|
|
|
type NetlinkRequestData interface {
|
2014-02-12 17:29:06 +00:00
|
|
|
Len() int
|
2013-12-23 23:39:39 +00:00
|
|
|
ToWireFormat() []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
type IfInfomsg struct {
|
|
|
|
syscall.IfInfomsg
|
|
|
|
}
|
|
|
|
|
|
|
|
func newIfInfomsg(family int) *IfInfomsg {
|
2014-02-12 17:29:06 +00:00
|
|
|
return &IfInfomsg{
|
|
|
|
IfInfomsg: syscall.IfInfomsg{
|
|
|
|
Family: uint8(family),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2013-12-23 23:39:39 +00:00
|
|
|
|
2014-02-12 17:29:06 +00:00
|
|
|
func newIfInfomsgChild(parent *RtAttr, family int) *IfInfomsg {
|
|
|
|
msg := newIfInfomsg(family)
|
|
|
|
parent.children = append(parent.children, msg)
|
2013-12-23 23:39:39 +00:00
|
|
|
return msg
|
|
|
|
}
|
|
|
|
|
|
|
|
func (msg *IfInfomsg) ToWireFormat() []byte {
|
|
|
|
native := nativeEndian()
|
|
|
|
|
2014-02-12 17:29:06 +00:00
|
|
|
length := syscall.SizeofIfInfomsg
|
|
|
|
b := make([]byte, length)
|
2013-12-23 23:39:39 +00:00
|
|
|
b[0] = msg.Family
|
|
|
|
b[1] = 0
|
|
|
|
native.PutUint16(b[2:4], msg.Type)
|
|
|
|
native.PutUint32(b[4:8], uint32(msg.Index))
|
|
|
|
native.PutUint32(b[8:12], msg.Flags)
|
|
|
|
native.PutUint32(b[12:16], msg.Change)
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2014-02-12 17:29:06 +00:00
|
|
|
func (msg *IfInfomsg) Len() int {
|
|
|
|
return syscall.SizeofIfInfomsg
|
|
|
|
}
|
|
|
|
|
2013-12-23 23:39:39 +00:00
|
|
|
type IfAddrmsg struct {
|
|
|
|
syscall.IfAddrmsg
|
|
|
|
}
|
|
|
|
|
|
|
|
func newIfAddrmsg(family int) *IfAddrmsg {
|
2014-02-12 17:29:06 +00:00
|
|
|
return &IfAddrmsg{
|
|
|
|
IfAddrmsg: syscall.IfAddrmsg{
|
|
|
|
Family: uint8(family),
|
|
|
|
},
|
|
|
|
}
|
2013-12-23 23:39:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (msg *IfAddrmsg) ToWireFormat() []byte {
|
|
|
|
native := nativeEndian()
|
|
|
|
|
2014-02-12 17:29:06 +00:00
|
|
|
length := syscall.SizeofIfAddrmsg
|
|
|
|
b := make([]byte, length)
|
2013-12-23 23:39:39 +00:00
|
|
|
b[0] = msg.Family
|
|
|
|
b[1] = msg.Prefixlen
|
|
|
|
b[2] = msg.Flags
|
|
|
|
b[3] = msg.Scope
|
|
|
|
native.PutUint32(b[4:8], msg.Index)
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2014-02-12 17:29:06 +00:00
|
|
|
func (msg *IfAddrmsg) Len() int {
|
|
|
|
return syscall.SizeofIfAddrmsg
|
|
|
|
}
|
|
|
|
|
2013-12-23 23:39:39 +00:00
|
|
|
type RtMsg struct {
|
|
|
|
syscall.RtMsg
|
|
|
|
}
|
|
|
|
|
|
|
|
func newRtMsg(family int) *RtMsg {
|
2014-02-12 17:29:06 +00:00
|
|
|
return &RtMsg{
|
|
|
|
RtMsg: syscall.RtMsg{
|
|
|
|
Family: uint8(family),
|
|
|
|
Table: syscall.RT_TABLE_MAIN,
|
|
|
|
Scope: syscall.RT_SCOPE_UNIVERSE,
|
|
|
|
Protocol: syscall.RTPROT_BOOT,
|
|
|
|
Type: syscall.RTN_UNICAST,
|
|
|
|
},
|
|
|
|
}
|
2013-12-23 23:39:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (msg *RtMsg) ToWireFormat() []byte {
|
|
|
|
native := nativeEndian()
|
|
|
|
|
2014-02-12 17:29:06 +00:00
|
|
|
length := syscall.SizeofRtMsg
|
|
|
|
b := make([]byte, length)
|
2013-12-23 23:39:39 +00:00
|
|
|
b[0] = msg.Family
|
|
|
|
b[1] = msg.Dst_len
|
|
|
|
b[2] = msg.Src_len
|
|
|
|
b[3] = msg.Tos
|
|
|
|
b[4] = msg.Table
|
|
|
|
b[5] = msg.Protocol
|
|
|
|
b[6] = msg.Scope
|
|
|
|
b[7] = msg.Type
|
|
|
|
native.PutUint32(b[8:12], msg.Flags)
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2014-02-12 17:29:06 +00:00
|
|
|
func (msg *RtMsg) Len() int {
|
|
|
|
return syscall.SizeofRtMsg
|
|
|
|
}
|
|
|
|
|
2013-12-23 23:39:39 +00:00
|
|
|
func rtaAlignOf(attrlen int) int {
|
|
|
|
return (attrlen + syscall.RTA_ALIGNTO - 1) & ^(syscall.RTA_ALIGNTO - 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
type RtAttr struct {
|
|
|
|
syscall.RtAttr
|
2014-02-12 12:09:56 +00:00
|
|
|
Data []byte
|
2014-02-12 17:29:06 +00:00
|
|
|
children []NetlinkRequestData
|
2013-12-23 23:39:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newRtAttr(attrType int, data []byte) *RtAttr {
|
2014-02-12 17:29:06 +00:00
|
|
|
return &RtAttr{
|
|
|
|
RtAttr: syscall.RtAttr{
|
|
|
|
Type: uint16(attrType),
|
|
|
|
},
|
|
|
|
children: []NetlinkRequestData{},
|
|
|
|
Data: data,
|
2014-02-12 12:09:56 +00:00
|
|
|
}
|
2013-12-23 23:39:39 +00:00
|
|
|
}
|
|
|
|
|
2014-02-12 12:09:56 +00:00
|
|
|
func newRtAttrChild(parent *RtAttr, attrType int, data []byte) *RtAttr {
|
|
|
|
attr := newRtAttr(attrType, data)
|
|
|
|
parent.children = append(parent.children, attr)
|
|
|
|
return attr
|
|
|
|
}
|
|
|
|
|
2014-02-12 17:29:06 +00:00
|
|
|
func (a *RtAttr) Len() int {
|
2014-02-12 12:09:56 +00:00
|
|
|
l := 0
|
|
|
|
for _, child := range a.children {
|
2014-02-12 17:29:06 +00:00
|
|
|
l += child.Len() + syscall.SizeofRtAttr
|
2014-02-12 12:09:56 +00:00
|
|
|
}
|
|
|
|
if l == 0 {
|
|
|
|
l++
|
|
|
|
}
|
|
|
|
return rtaAlignOf(l + len(a.Data))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *RtAttr) ToWireFormat() []byte {
|
2013-12-23 23:39:39 +00:00
|
|
|
native := nativeEndian()
|
|
|
|
|
2014-02-12 17:29:06 +00:00
|
|
|
length := a.Len()
|
2014-02-12 12:09:56 +00:00
|
|
|
buf := make([]byte, rtaAlignOf(length+syscall.SizeofRtAttr))
|
|
|
|
|
|
|
|
if a.Data != nil {
|
|
|
|
copy(buf[4:], a.Data)
|
|
|
|
} else {
|
|
|
|
next := 4
|
|
|
|
for _, child := range a.children {
|
|
|
|
childBuf := child.ToWireFormat()
|
2014-02-12 17:29:06 +00:00
|
|
|
copy(buf[next:], childBuf)
|
2014-02-12 12:09:56 +00:00
|
|
|
next += rtaAlignOf(len(childBuf))
|
|
|
|
}
|
2013-12-23 23:39:39 +00:00
|
|
|
}
|
|
|
|
|
2014-02-12 12:09:56 +00:00
|
|
|
if l := uint16(rtaAlignOf(length)); l != 0 {
|
|
|
|
native.PutUint16(buf[0:2], l+1)
|
|
|
|
}
|
|
|
|
native.PutUint16(buf[2:4], a.Type)
|
|
|
|
|
|
|
|
return buf
|
2013-12-23 23:39:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type NetlinkRequest struct {
|
|
|
|
syscall.NlMsghdr
|
|
|
|
Data []NetlinkRequestData
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rr *NetlinkRequest) ToWireFormat() []byte {
|
|
|
|
native := nativeEndian()
|
|
|
|
|
|
|
|
length := rr.Len
|
|
|
|
dataBytes := make([][]byte, len(rr.Data))
|
|
|
|
for i, data := range rr.Data {
|
|
|
|
dataBytes[i] = data.ToWireFormat()
|
2014-02-12 17:29:06 +00:00
|
|
|
length += uint32(len(dataBytes[i]))
|
2013-12-23 23:39:39 +00:00
|
|
|
}
|
|
|
|
b := make([]byte, length)
|
|
|
|
native.PutUint32(b[0:4], length)
|
|
|
|
native.PutUint16(b[4:6], rr.Type)
|
|
|
|
native.PutUint16(b[6:8], rr.Flags)
|
|
|
|
native.PutUint32(b[8:12], rr.Seq)
|
|
|
|
native.PutUint32(b[12:16], rr.Pid)
|
|
|
|
|
2014-02-12 17:29:06 +00:00
|
|
|
next := 16
|
2013-12-23 23:39:39 +00:00
|
|
|
for _, data := range dataBytes {
|
2014-02-12 17:29:06 +00:00
|
|
|
copy(b[next:], data)
|
|
|
|
next += len(data)
|
2013-12-23 23:39:39 +00:00
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rr *NetlinkRequest) AddData(data NetlinkRequestData) {
|
2014-02-10 21:37:16 +00:00
|
|
|
if data != nil {
|
|
|
|
rr.Data = append(rr.Data, data)
|
|
|
|
}
|
2013-12-23 23:39:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newNetlinkRequest(proto, flags int) *NetlinkRequest {
|
2014-02-12 17:29:06 +00:00
|
|
|
return &NetlinkRequest{
|
|
|
|
NlMsghdr: syscall.NlMsghdr{
|
|
|
|
Len: uint32(syscall.NLMSG_HDRLEN),
|
|
|
|
Type: uint16(proto),
|
|
|
|
Flags: syscall.NLM_F_REQUEST | uint16(flags),
|
|
|
|
Seq: uint32(getSeq()),
|
|
|
|
},
|
|
|
|
}
|
2013-12-23 23:39:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type NetlinkSocket struct {
|
|
|
|
fd int
|
|
|
|
lsa syscall.SockaddrNetlink
|
|
|
|
}
|
|
|
|
|
|
|
|
func getNetlinkSocket() (*NetlinkSocket, error) {
|
|
|
|
fd, err := syscall.Socket(syscall.AF_NETLINK, syscall.SOCK_RAW, syscall.NETLINK_ROUTE)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
s := &NetlinkSocket{
|
|
|
|
fd: fd,
|
|
|
|
}
|
|
|
|
s.lsa.Family = syscall.AF_NETLINK
|
|
|
|
if err := syscall.Bind(fd, &s.lsa); err != nil {
|
|
|
|
syscall.Close(fd)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *NetlinkSocket) Close() {
|
|
|
|
syscall.Close(s.fd)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *NetlinkSocket) Send(request *NetlinkRequest) error {
|
|
|
|
if err := syscall.Sendto(s.fd, request.ToWireFormat(), 0, &s.lsa); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-01-08 05:15:53 +00:00
|
|
|
func (s *NetlinkSocket) Receive() ([]syscall.NetlinkMessage, error) {
|
2013-12-23 23:39:39 +00:00
|
|
|
rb := make([]byte, syscall.Getpagesize())
|
|
|
|
nr, _, err := syscall.Recvfrom(s.fd, rb, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if nr < syscall.NLMSG_HDRLEN {
|
2014-02-12 17:29:06 +00:00
|
|
|
return nil, ErrShortResponse
|
2013-12-23 23:39:39 +00:00
|
|
|
}
|
|
|
|
rb = rb[:nr]
|
|
|
|
return syscall.ParseNetlinkMessage(rb)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *NetlinkSocket) GetPid() (uint32, error) {
|
|
|
|
lsa, err := syscall.Getsockname(s.fd)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
switch v := lsa.(type) {
|
|
|
|
case *syscall.SockaddrNetlink:
|
|
|
|
return v.Pid, nil
|
|
|
|
}
|
2014-02-12 17:29:06 +00:00
|
|
|
return 0, ErrWrongSockType
|
2013-12-23 23:39:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *NetlinkSocket) HandleAck(seq uint32) error {
|
|
|
|
native := nativeEndian()
|
|
|
|
|
|
|
|
pid, err := s.GetPid()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
for {
|
2014-01-08 05:15:53 +00:00
|
|
|
msgs, err := s.Receive()
|
2013-12-23 23:39:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, m := range msgs {
|
|
|
|
if m.Header.Seq != seq {
|
|
|
|
return fmt.Errorf("Wrong Seq nr %d, expected %d", m.Header.Seq, seq)
|
|
|
|
}
|
|
|
|
if m.Header.Pid != pid {
|
|
|
|
return fmt.Errorf("Wrong pid %d, expected %d", m.Header.Pid, pid)
|
|
|
|
}
|
|
|
|
if m.Header.Type == syscall.NLMSG_DONE {
|
|
|
|
break done
|
|
|
|
}
|
|
|
|
if m.Header.Type == syscall.NLMSG_ERROR {
|
|
|
|
error := int32(native.Uint32(m.Data[0:4]))
|
|
|
|
if error == 0 {
|
|
|
|
break done
|
|
|
|
}
|
|
|
|
return syscall.Errno(-error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a new default gateway. Identical to:
|
|
|
|
// ip route add default via $ip
|
|
|
|
func AddDefaultGw(ip net.IP) error {
|
|
|
|
s, err := getNetlinkSocket()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
family := getIpFamily(ip)
|
|
|
|
|
|
|
|
wb := newNetlinkRequest(syscall.RTM_NEWROUTE, syscall.NLM_F_CREATE|syscall.NLM_F_EXCL|syscall.NLM_F_ACK)
|
|
|
|
|
|
|
|
msg := newRtMsg(family)
|
|
|
|
wb.AddData(msg)
|
|
|
|
|
|
|
|
var ipData []byte
|
|
|
|
if family == syscall.AF_INET {
|
|
|
|
ipData = ip.To4()
|
|
|
|
} else {
|
|
|
|
ipData = ip.To16()
|
|
|
|
}
|
|
|
|
|
|
|
|
gateway := newRtAttr(syscall.RTA_GATEWAY, ipData)
|
|
|
|
|
|
|
|
wb.AddData(gateway)
|
|
|
|
|
|
|
|
if err := s.Send(wb); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.HandleAck(wb.Seq)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bring up a particular network interface
|
|
|
|
func NetworkLinkUp(iface *net.Interface) error {
|
|
|
|
s, err := getNetlinkSocket()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
wb := newNetlinkRequest(syscall.RTM_NEWLINK, syscall.NLM_F_ACK)
|
|
|
|
|
|
|
|
msg := newIfInfomsg(syscall.AF_UNSPEC)
|
|
|
|
msg.Change = syscall.IFF_UP
|
|
|
|
msg.Flags = syscall.IFF_UP
|
|
|
|
msg.Index = int32(iface.Index)
|
|
|
|
wb.AddData(msg)
|
|
|
|
|
|
|
|
if err := s.Send(wb); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.HandleAck(wb.Seq)
|
|
|
|
}
|
|
|
|
|
2014-02-11 06:32:07 +00:00
|
|
|
func NetworkLinkDown(iface *net.Interface) error {
|
|
|
|
s, err := getNetlinkSocket()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
wb := newNetlinkRequest(syscall.RTM_NEWLINK, syscall.NLM_F_ACK)
|
|
|
|
|
|
|
|
msg := newIfInfomsg(syscall.AF_UNSPEC)
|
|
|
|
msg.Change = syscall.IFF_UP
|
|
|
|
msg.Flags = 0 & ^syscall.IFF_UP
|
|
|
|
msg.Index = int32(iface.Index)
|
|
|
|
wb.AddData(msg)
|
|
|
|
|
|
|
|
if err := s.Send(wb); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.HandleAck(wb.Seq)
|
|
|
|
}
|
|
|
|
|
2013-12-23 23:39:39 +00:00
|
|
|
func NetworkSetMTU(iface *net.Interface, mtu int) error {
|
|
|
|
s, err := getNetlinkSocket()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
wb := newNetlinkRequest(syscall.RTM_SETLINK, syscall.NLM_F_ACK)
|
|
|
|
|
|
|
|
msg := newIfInfomsg(syscall.AF_UNSPEC)
|
|
|
|
msg.Type = syscall.RTM_SETLINK
|
|
|
|
msg.Flags = syscall.NLM_F_REQUEST
|
|
|
|
msg.Index = int32(iface.Index)
|
2014-02-10 19:36:23 +00:00
|
|
|
msg.Change = DEFAULT_CHANGE
|
2013-12-23 23:39:39 +00:00
|
|
|
wb.AddData(msg)
|
|
|
|
|
|
|
|
var (
|
|
|
|
b = make([]byte, 4)
|
|
|
|
native = nativeEndian()
|
|
|
|
)
|
|
|
|
native.PutUint32(b, uint32(mtu))
|
|
|
|
|
|
|
|
data := newRtAttr(syscall.IFLA_MTU, b)
|
|
|
|
wb.AddData(data)
|
|
|
|
|
|
|
|
if err := s.Send(wb); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return s.HandleAck(wb.Seq)
|
|
|
|
}
|
|
|
|
|
2014-02-08 17:53:04 +00:00
|
|
|
// same as ip link set $name master $master
|
|
|
|
func NetworkSetMaster(iface, master *net.Interface) error {
|
|
|
|
s, err := getNetlinkSocket()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
wb := newNetlinkRequest(syscall.RTM_SETLINK, syscall.NLM_F_ACK)
|
|
|
|
|
|
|
|
msg := newIfInfomsg(syscall.AF_UNSPEC)
|
|
|
|
msg.Type = syscall.RTM_SETLINK
|
|
|
|
msg.Flags = syscall.NLM_F_REQUEST
|
|
|
|
msg.Index = int32(iface.Index)
|
2014-02-10 19:36:23 +00:00
|
|
|
msg.Change = DEFAULT_CHANGE
|
2014-02-08 17:53:04 +00:00
|
|
|
wb.AddData(msg)
|
|
|
|
|
|
|
|
var (
|
|
|
|
b = make([]byte, 4)
|
|
|
|
native = nativeEndian()
|
|
|
|
)
|
|
|
|
native.PutUint32(b, uint32(master.Index))
|
|
|
|
|
|
|
|
data := newRtAttr(syscall.IFLA_MASTER, b)
|
|
|
|
wb.AddData(data)
|
|
|
|
|
|
|
|
if err := s.Send(wb); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.HandleAck(wb.Seq)
|
|
|
|
}
|
|
|
|
|
2014-02-08 18:03:16 +00:00
|
|
|
func NetworkSetNsPid(iface *net.Interface, nspid int) error {
|
|
|
|
s, err := getNetlinkSocket()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
wb := newNetlinkRequest(syscall.RTM_SETLINK, syscall.NLM_F_ACK)
|
|
|
|
|
|
|
|
msg := newIfInfomsg(syscall.AF_UNSPEC)
|
|
|
|
msg.Type = syscall.RTM_SETLINK
|
|
|
|
msg.Flags = syscall.NLM_F_REQUEST
|
|
|
|
msg.Index = int32(iface.Index)
|
2014-02-10 19:36:23 +00:00
|
|
|
msg.Change = DEFAULT_CHANGE
|
2014-02-08 18:03:16 +00:00
|
|
|
wb.AddData(msg)
|
|
|
|
|
|
|
|
var (
|
|
|
|
b = make([]byte, 4)
|
|
|
|
native = nativeEndian()
|
|
|
|
)
|
|
|
|
native.PutUint32(b, uint32(nspid))
|
|
|
|
|
|
|
|
data := newRtAttr(syscall.IFLA_NET_NS_PID, b)
|
|
|
|
wb.AddData(data)
|
|
|
|
|
|
|
|
if err := s.Send(wb); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.HandleAck(wb.Seq)
|
|
|
|
}
|
|
|
|
|
2014-02-11 06:32:07 +00:00
|
|
|
func NetworkSetNsFd(iface *net.Interface, fd int) error {
|
|
|
|
s, err := getNetlinkSocket()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
wb := newNetlinkRequest(syscall.RTM_SETLINK, syscall.NLM_F_ACK)
|
|
|
|
|
|
|
|
msg := newIfInfomsg(syscall.AF_UNSPEC)
|
|
|
|
msg.Type = syscall.RTM_SETLINK
|
|
|
|
msg.Flags = syscall.NLM_F_REQUEST
|
|
|
|
msg.Index = int32(iface.Index)
|
|
|
|
msg.Change = DEFAULT_CHANGE
|
|
|
|
wb.AddData(msg)
|
|
|
|
|
|
|
|
var (
|
|
|
|
b = make([]byte, 4)
|
|
|
|
native = nativeEndian()
|
|
|
|
)
|
|
|
|
native.PutUint32(b, uint32(fd))
|
|
|
|
|
|
|
|
data := newRtAttr(IFLA_NET_NS_FD, b)
|
|
|
|
wb.AddData(data)
|
|
|
|
|
|
|
|
if err := s.Send(wb); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.HandleAck(wb.Seq)
|
|
|
|
}
|
|
|
|
|
2013-12-23 23:39:39 +00:00
|
|
|
// Add an Ip address to an interface. This is identical to:
|
|
|
|
// ip addr add $ip/$ipNet dev $iface
|
|
|
|
func NetworkLinkAddIp(iface *net.Interface, ip net.IP, ipNet *net.IPNet) error {
|
|
|
|
s, err := getNetlinkSocket()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
family := getIpFamily(ip)
|
|
|
|
|
|
|
|
wb := newNetlinkRequest(syscall.RTM_NEWADDR, syscall.NLM_F_CREATE|syscall.NLM_F_EXCL|syscall.NLM_F_ACK)
|
|
|
|
|
|
|
|
msg := newIfAddrmsg(family)
|
|
|
|
msg.Index = uint32(iface.Index)
|
|
|
|
prefixLen, _ := ipNet.Mask.Size()
|
|
|
|
msg.Prefixlen = uint8(prefixLen)
|
|
|
|
wb.AddData(msg)
|
|
|
|
|
|
|
|
var ipData []byte
|
|
|
|
if family == syscall.AF_INET {
|
|
|
|
ipData = ip.To4()
|
|
|
|
} else {
|
|
|
|
ipData = ip.To16()
|
|
|
|
}
|
|
|
|
|
|
|
|
localData := newRtAttr(syscall.IFA_LOCAL, ipData)
|
|
|
|
wb.AddData(localData)
|
|
|
|
|
|
|
|
addrData := newRtAttr(syscall.IFA_ADDRESS, ipData)
|
|
|
|
wb.AddData(addrData)
|
|
|
|
|
|
|
|
if err := s.Send(wb); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.HandleAck(wb.Seq)
|
|
|
|
}
|
|
|
|
|
|
|
|
func zeroTerminated(s string) []byte {
|
2014-02-12 12:09:56 +00:00
|
|
|
return []byte(s + "\000")
|
2013-12-23 23:39:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func nonZeroTerminated(s string) []byte {
|
2014-02-12 17:29:06 +00:00
|
|
|
return []byte(s)
|
2013-12-23 23:39:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add a new network link of a specified type. This is identical to
|
|
|
|
// running: ip add link $name type $linkType
|
|
|
|
func NetworkLinkAdd(name string, linkType string) error {
|
|
|
|
s, err := getNetlinkSocket()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
wb := newNetlinkRequest(syscall.RTM_NEWLINK, syscall.NLM_F_CREATE|syscall.NLM_F_EXCL|syscall.NLM_F_ACK)
|
|
|
|
|
|
|
|
msg := newIfInfomsg(syscall.AF_UNSPEC)
|
|
|
|
wb.AddData(msg)
|
|
|
|
|
2014-02-11 00:41:16 +00:00
|
|
|
if name != "" {
|
|
|
|
nameData := newRtAttr(syscall.IFLA_IFNAME, zeroTerminated(name))
|
|
|
|
wb.AddData(nameData)
|
|
|
|
}
|
2013-12-23 23:39:39 +00:00
|
|
|
|
|
|
|
kindData := newRtAttr(IFLA_INFO_KIND, nonZeroTerminated(linkType))
|
|
|
|
|
|
|
|
infoData := newRtAttr(syscall.IFLA_LINKINFO, kindData.ToWireFormat())
|
|
|
|
wb.AddData(infoData)
|
|
|
|
|
|
|
|
if err := s.Send(wb); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.HandleAck(wb.Seq)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns an array of IPNet for all the currently routed subnets on ipv4
|
|
|
|
// This is similar to the first column of "ip route" output
|
2014-01-17 07:41:29 +00:00
|
|
|
func NetworkGetRoutes() ([]Route, error) {
|
2013-12-23 23:39:39 +00:00
|
|
|
native := nativeEndian()
|
|
|
|
|
|
|
|
s, err := getNetlinkSocket()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
wb := newNetlinkRequest(syscall.RTM_GETROUTE, syscall.NLM_F_DUMP)
|
|
|
|
|
|
|
|
msg := newIfInfomsg(syscall.AF_UNSPEC)
|
|
|
|
wb.AddData(msg)
|
|
|
|
|
|
|
|
if err := s.Send(wb); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
pid, err := s.GetPid()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-01-17 07:41:29 +00:00
|
|
|
res := make([]Route, 0)
|
2013-12-23 23:39:39 +00:00
|
|
|
|
|
|
|
done:
|
|
|
|
for {
|
2014-01-08 05:15:53 +00:00
|
|
|
msgs, err := s.Receive()
|
2013-12-23 23:39:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, m := range msgs {
|
|
|
|
if m.Header.Seq != wb.Seq {
|
|
|
|
return nil, fmt.Errorf("Wrong Seq nr %d, expected 1", m.Header.Seq)
|
|
|
|
}
|
|
|
|
if m.Header.Pid != pid {
|
|
|
|
return nil, fmt.Errorf("Wrong pid %d, expected %d", m.Header.Pid, pid)
|
|
|
|
}
|
|
|
|
if m.Header.Type == syscall.NLMSG_DONE {
|
|
|
|
break done
|
|
|
|
}
|
|
|
|
if m.Header.Type == syscall.NLMSG_ERROR {
|
|
|
|
error := int32(native.Uint32(m.Data[0:4]))
|
|
|
|
if error == 0 {
|
|
|
|
break done
|
|
|
|
}
|
|
|
|
return nil, syscall.Errno(-error)
|
|
|
|
}
|
|
|
|
if m.Header.Type != syscall.RTM_NEWROUTE {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-01-17 07:41:29 +00:00
|
|
|
var r Route
|
2013-12-23 23:39:39 +00:00
|
|
|
|
|
|
|
msg := (*RtMsg)(unsafe.Pointer(&m.Data[0:syscall.SizeofRtMsg][0]))
|
|
|
|
|
|
|
|
if msg.Flags&syscall.RTM_F_CLONED != 0 {
|
|
|
|
// Ignore cloned routes
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if msg.Table != syscall.RT_TABLE_MAIN {
|
|
|
|
// Ignore non-main tables
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if msg.Family != syscall.AF_INET {
|
|
|
|
// Ignore non-ipv4 routes
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if msg.Dst_len == 0 {
|
2014-01-17 07:41:29 +00:00
|
|
|
// Default routes
|
|
|
|
r.Default = true
|
2013-12-23 23:39:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
attrs, err := syscall.ParseNetlinkRouteAttr(&m)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, attr := range attrs {
|
|
|
|
switch attr.Attr.Type {
|
|
|
|
case syscall.RTA_DST:
|
|
|
|
ip := attr.Value
|
2014-01-17 07:41:29 +00:00
|
|
|
r.IPNet = &net.IPNet{
|
2013-12-23 23:39:39 +00:00
|
|
|
IP: ip,
|
|
|
|
Mask: net.CIDRMask(int(msg.Dst_len), 8*len(ip)),
|
|
|
|
}
|
|
|
|
case syscall.RTA_OIF:
|
|
|
|
index := int(native.Uint32(attr.Value[0:4]))
|
2014-01-17 07:41:29 +00:00
|
|
|
r.Iface, _ = net.InterfaceByIndex(index)
|
2013-12-23 23:39:39 +00:00
|
|
|
}
|
|
|
|
}
|
2014-01-17 07:41:29 +00:00
|
|
|
if r.Default || r.IPNet != nil {
|
|
|
|
res = append(res, r)
|
2013-12-23 23:39:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
2014-02-09 04:44:04 +00:00
|
|
|
|
2014-02-10 19:36:23 +00:00
|
|
|
func getIfSocket() (fd int, err error) {
|
|
|
|
for _, socket := range []int{
|
|
|
|
syscall.AF_INET,
|
|
|
|
syscall.AF_PACKET,
|
|
|
|
syscall.AF_INET6,
|
|
|
|
} {
|
|
|
|
if fd, err = syscall.Socket(socket, syscall.SOCK_DGRAM, 0); err == nil {
|
|
|
|
break
|
|
|
|
}
|
2014-02-09 13:54:13 +00:00
|
|
|
}
|
|
|
|
if err == nil {
|
2014-02-10 19:36:23 +00:00
|
|
|
return fd, nil
|
2014-02-09 13:54:13 +00:00
|
|
|
}
|
2014-02-10 19:36:23 +00:00
|
|
|
return -1, err
|
2014-02-09 13:54:13 +00:00
|
|
|
}
|
|
|
|
|
2014-02-11 06:32:07 +00:00
|
|
|
func NetworkChangeName(iface *net.Interface, newName string) error {
|
2014-02-09 13:54:13 +00:00
|
|
|
fd, err := getIfSocket()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer syscall.Close(fd)
|
|
|
|
|
2014-02-10 01:12:43 +00:00
|
|
|
data := [IFNAMSIZ * 2]byte{}
|
2014-02-10 22:11:17 +00:00
|
|
|
// the "-1"s here are very important for ensuring we get proper null
|
|
|
|
// termination of our new C strings
|
2014-02-11 06:32:07 +00:00
|
|
|
copy(data[:IFNAMSIZ-1], iface.Name)
|
2014-02-09 13:54:13 +00:00
|
|
|
copy(data[IFNAMSIZ:IFNAMSIZ*2-1], newName)
|
2014-02-09 04:44:04 +00:00
|
|
|
|
2014-02-09 13:54:13 +00:00
|
|
|
if _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, uintptr(fd), syscall.SIOCSIFNAME, uintptr(unsafe.Pointer(&data[0]))); errno != 0 {
|
|
|
|
return errno
|
2014-02-09 04:44:04 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2014-02-10 21:37:16 +00:00
|
|
|
|
|
|
|
func NetworkCreateVethPair(name1, name2 string) error {
|
|
|
|
s, err := getNetlinkSocket()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
wb := newNetlinkRequest(syscall.RTM_NEWLINK, syscall.NLM_F_CREATE|syscall.NLM_F_EXCL|syscall.NLM_F_ACK)
|
|
|
|
|
|
|
|
msg := newIfInfomsg(syscall.AF_UNSPEC)
|
|
|
|
wb.AddData(msg)
|
|
|
|
|
2014-02-12 12:09:56 +00:00
|
|
|
nameData := newRtAttr(syscall.IFLA_IFNAME, zeroTerminated(name1))
|
|
|
|
wb.AddData(nameData)
|
2014-02-10 21:37:16 +00:00
|
|
|
|
2014-02-12 12:09:56 +00:00
|
|
|
nest1 := newRtAttr(syscall.IFLA_LINKINFO, nil)
|
|
|
|
newRtAttrChild(nest1, IFLA_INFO_KIND, zeroTerminated("veth"))
|
|
|
|
nest2 := newRtAttrChild(nest1, IFLA_INFO_DATA, nil)
|
2014-02-12 12:12:11 +00:00
|
|
|
nest3 := newRtAttrChild(nest2, VETH_INFO_PEER, nil)
|
2014-02-10 21:37:16 +00:00
|
|
|
|
2014-02-12 17:29:06 +00:00
|
|
|
newIfInfomsgChild(nest3, syscall.AF_UNSPEC)
|
|
|
|
newRtAttrChild(nest3, syscall.IFLA_IFNAME, zeroTerminated(name2))
|
2014-02-10 21:37:16 +00:00
|
|
|
|
2014-02-12 12:09:56 +00:00
|
|
|
wb.AddData(nest1)
|
2014-02-10 21:37:16 +00:00
|
|
|
|
|
|
|
if err := s.Send(wb); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return s.HandleAck(wb.Seq)
|
|
|
|
}
|
2014-03-31 21:02:42 +00:00
|
|
|
|
|
|
|
// Create the actual bridge device. This is more backward-compatible than
|
|
|
|
// netlink.NetworkLinkAdd and works on RHEL 6.
|
|
|
|
func CreateBridge(name string, setMacAddr bool) error {
|
|
|
|
s, err := syscall.Socket(syscall.AF_INET6, syscall.SOCK_STREAM, syscall.IPPROTO_IP)
|
|
|
|
if err != nil {
|
|
|
|
// ipv6 issue, creating with ipv4
|
|
|
|
s, err = syscall.Socket(syscall.AF_INET, syscall.SOCK_STREAM, syscall.IPPROTO_IP)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
defer syscall.Close(s)
|
|
|
|
|
|
|
|
nameBytePtr, err := syscall.BytePtrFromString(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, uintptr(s), SIOC_BRADDBR, uintptr(unsafe.Pointer(nameBytePtr))); err != 0 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if setMacAddr {
|
|
|
|
return setBridgeMacAddress(s, name)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func setBridgeMacAddress(s int, name string) error {
|
|
|
|
ifr := ifreqHwaddr{}
|
|
|
|
ifr.IfruHwaddr.Family = syscall.ARPHRD_ETHER
|
|
|
|
copy(ifr.IfrnName[:], name)
|
|
|
|
|
|
|
|
for i := 0; i < 6; i++ {
|
|
|
|
ifr.IfruHwaddr.Data[i] = int8(rand.Intn(255))
|
|
|
|
}
|
|
|
|
|
|
|
|
ifr.IfruHwaddr.Data[0] &^= 0x1 // clear multicast bit
|
|
|
|
ifr.IfruHwaddr.Data[0] |= 0x2 // set local assignment bit (IEEE802)
|
|
|
|
|
|
|
|
if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, uintptr(s), syscall.SIOCSIFHWADDR, uintptr(unsafe.Pointer(&ifr))); err != 0 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|