2014-09-16 00:44:20 +00:00
|
|
|
package netlink
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"unsafe"
|
|
|
|
|
|
|
|
"github.com/vishvananda/netlink/nl"
|
2017-10-20 20:38:07 +00:00
|
|
|
"golang.org/x/sys/unix"
|
2014-09-16 00:44:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2015-02-12 19:16:19 +00:00
|
|
|
NDA_UNSPEC = iota
|
|
|
|
NDA_DST
|
|
|
|
NDA_LLADDR
|
|
|
|
NDA_CACHEINFO
|
|
|
|
NDA_PROBES
|
|
|
|
NDA_VLAN
|
|
|
|
NDA_PORT
|
|
|
|
NDA_VNI
|
|
|
|
NDA_IFINDEX
|
|
|
|
NDA_MAX = NDA_IFINDEX
|
2014-09-16 00:44:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Neighbor Cache Entry States.
|
|
|
|
const (
|
|
|
|
NUD_NONE = 0x00
|
|
|
|
NUD_INCOMPLETE = 0x01
|
|
|
|
NUD_REACHABLE = 0x02
|
|
|
|
NUD_STALE = 0x04
|
|
|
|
NUD_DELAY = 0x08
|
|
|
|
NUD_PROBE = 0x10
|
|
|
|
NUD_FAILED = 0x20
|
|
|
|
NUD_NOARP = 0x40
|
|
|
|
NUD_PERMANENT = 0x80
|
|
|
|
)
|
|
|
|
|
|
|
|
// Neighbor Flags
|
|
|
|
const (
|
|
|
|
NTF_USE = 0x01
|
|
|
|
NTF_SELF = 0x02
|
|
|
|
NTF_MASTER = 0x04
|
|
|
|
NTF_PROXY = 0x08
|
|
|
|
NTF_ROUTER = 0x80
|
|
|
|
)
|
|
|
|
|
|
|
|
type Ndmsg struct {
|
|
|
|
Family uint8
|
|
|
|
Index uint32
|
|
|
|
State uint16
|
2014-10-29 00:22:52 +00:00
|
|
|
Flags uint8
|
2014-09-16 00:44:20 +00:00
|
|
|
Type uint8
|
|
|
|
}
|
|
|
|
|
|
|
|
func deserializeNdmsg(b []byte) *Ndmsg {
|
|
|
|
var dummy Ndmsg
|
|
|
|
return (*Ndmsg)(unsafe.Pointer(&b[0:unsafe.Sizeof(dummy)][0]))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (msg *Ndmsg) Serialize() []byte {
|
|
|
|
return (*(*[unsafe.Sizeof(*msg)]byte)(unsafe.Pointer(msg)))[:]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (msg *Ndmsg) Len() int {
|
|
|
|
return int(unsafe.Sizeof(*msg))
|
|
|
|
}
|
|
|
|
|
|
|
|
// NeighAdd will add an IP to MAC mapping to the ARP table
|
|
|
|
// Equivalent to: `ip neigh add ....`
|
|
|
|
func NeighAdd(neigh *Neigh) error {
|
2016-05-09 23:55:00 +00:00
|
|
|
return pkgHandle.NeighAdd(neigh)
|
2016-05-08 18:35:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NeighAdd will add an IP to MAC mapping to the ARP table
|
|
|
|
// Equivalent to: `ip neigh add ....`
|
|
|
|
func (h *Handle) NeighAdd(neigh *Neigh) error {
|
2017-10-20 20:38:07 +00:00
|
|
|
return h.neighAdd(neigh, unix.NLM_F_CREATE|unix.NLM_F_EXCL)
|
2014-09-16 00:44:20 +00:00
|
|
|
}
|
|
|
|
|
2015-11-10 13:38:50 +00:00
|
|
|
// NeighSet will add or replace an IP to MAC mapping to the ARP table
|
2014-09-16 00:44:20 +00:00
|
|
|
// Equivalent to: `ip neigh replace....`
|
|
|
|
func NeighSet(neigh *Neigh) error {
|
2016-05-09 23:55:00 +00:00
|
|
|
return pkgHandle.NeighSet(neigh)
|
2016-05-08 18:35:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NeighSet will add or replace an IP to MAC mapping to the ARP table
|
|
|
|
// Equivalent to: `ip neigh replace....`
|
|
|
|
func (h *Handle) NeighSet(neigh *Neigh) error {
|
2017-10-20 20:38:07 +00:00
|
|
|
return h.neighAdd(neigh, unix.NLM_F_CREATE|unix.NLM_F_REPLACE)
|
2014-09-16 00:44:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NeighAppend will append an entry to FDB
|
|
|
|
// Equivalent to: `bridge fdb append...`
|
|
|
|
func NeighAppend(neigh *Neigh) error {
|
2016-05-09 23:55:00 +00:00
|
|
|
return pkgHandle.NeighAppend(neigh)
|
2016-05-08 18:35:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NeighAppend will append an entry to FDB
|
|
|
|
// Equivalent to: `bridge fdb append...`
|
|
|
|
func (h *Handle) NeighAppend(neigh *Neigh) error {
|
2017-10-20 20:38:07 +00:00
|
|
|
return h.neighAdd(neigh, unix.NLM_F_CREATE|unix.NLM_F_APPEND)
|
2014-09-16 00:44:20 +00:00
|
|
|
}
|
|
|
|
|
2016-05-08 18:35:49 +00:00
|
|
|
// NeighAppend will append an entry to FDB
|
|
|
|
// Equivalent to: `bridge fdb append...`
|
2014-09-16 00:44:20 +00:00
|
|
|
func neighAdd(neigh *Neigh, mode int) error {
|
2016-05-09 23:55:00 +00:00
|
|
|
return pkgHandle.neighAdd(neigh, mode)
|
2016-05-08 18:35:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NeighAppend will append an entry to FDB
|
|
|
|
// Equivalent to: `bridge fdb append...`
|
|
|
|
func (h *Handle) neighAdd(neigh *Neigh, mode int) error {
|
2017-10-20 20:38:07 +00:00
|
|
|
req := h.newNetlinkRequest(unix.RTM_NEWNEIGH, mode|unix.NLM_F_ACK)
|
2014-09-16 00:44:20 +00:00
|
|
|
return neighHandle(neigh, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NeighDel will delete an IP address from a link device.
|
|
|
|
// Equivalent to: `ip addr del $addr dev $link`
|
|
|
|
func NeighDel(neigh *Neigh) error {
|
2016-05-09 23:55:00 +00:00
|
|
|
return pkgHandle.NeighDel(neigh)
|
2016-05-08 18:35:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NeighDel will delete an IP address from a link device.
|
|
|
|
// Equivalent to: `ip addr del $addr dev $link`
|
|
|
|
func (h *Handle) NeighDel(neigh *Neigh) error {
|
2017-10-20 20:38:07 +00:00
|
|
|
req := h.newNetlinkRequest(unix.RTM_DELNEIGH, unix.NLM_F_ACK)
|
2014-09-16 00:44:20 +00:00
|
|
|
return neighHandle(neigh, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
func neighHandle(neigh *Neigh, req *nl.NetlinkRequest) error {
|
|
|
|
var family int
|
2017-08-29 00:59:25 +00:00
|
|
|
|
2014-09-16 00:44:20 +00:00
|
|
|
if neigh.Family > 0 {
|
|
|
|
family = neigh.Family
|
|
|
|
} else {
|
|
|
|
family = nl.GetIPFamily(neigh.IP)
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := Ndmsg{
|
|
|
|
Family: uint8(family),
|
2014-10-13 23:09:09 +00:00
|
|
|
Index: uint32(neigh.LinkIndex),
|
2014-09-16 00:44:20 +00:00
|
|
|
State: uint16(neigh.State),
|
|
|
|
Type: uint8(neigh.Type),
|
|
|
|
Flags: uint8(neigh.Flags),
|
|
|
|
}
|
|
|
|
req.AddData(&msg)
|
|
|
|
|
|
|
|
ipData := neigh.IP.To4()
|
|
|
|
if ipData == nil {
|
|
|
|
ipData = neigh.IP.To16()
|
|
|
|
}
|
|
|
|
|
|
|
|
dstData := nl.NewRtAttr(NDA_DST, ipData)
|
|
|
|
req.AddData(dstData)
|
|
|
|
|
2017-08-29 00:59:25 +00:00
|
|
|
if neigh.LLIPAddr != nil {
|
|
|
|
llIPData := nl.NewRtAttr(NDA_LLADDR, neigh.LLIPAddr.To4())
|
|
|
|
req.AddData(llIPData)
|
|
|
|
} else if neigh.Flags != NTF_PROXY || neigh.HardwareAddr != nil {
|
2016-08-23 23:01:24 +00:00
|
|
|
hwData := nl.NewRtAttr(NDA_LLADDR, []byte(neigh.HardwareAddr))
|
|
|
|
req.AddData(hwData)
|
|
|
|
}
|
2014-09-16 00:44:20 +00:00
|
|
|
|
2017-05-04 13:15:30 +00:00
|
|
|
if neigh.Vlan != 0 {
|
|
|
|
vlanData := nl.NewRtAttr(NDA_VLAN, nl.Uint16Attr(uint16(neigh.Vlan)))
|
|
|
|
req.AddData(vlanData)
|
|
|
|
}
|
|
|
|
|
|
|
|
if neigh.VNI != 0 {
|
|
|
|
vniData := nl.NewRtAttr(NDA_VNI, nl.Uint32Attr(uint32(neigh.VNI)))
|
|
|
|
req.AddData(vniData)
|
|
|
|
}
|
|
|
|
|
2017-10-20 20:38:07 +00:00
|
|
|
_, err := req.Execute(unix.NETLINK_ROUTE, 0)
|
2014-09-16 00:44:20 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// NeighList gets a list of IP-MAC mappings in the system (ARP table).
|
|
|
|
// Equivalent to: `ip neighbor show`.
|
|
|
|
// The list can be filtered by link and ip family.
|
|
|
|
func NeighList(linkIndex, family int) ([]Neigh, error) {
|
2016-05-09 23:55:00 +00:00
|
|
|
return pkgHandle.NeighList(linkIndex, family)
|
2016-05-08 18:35:49 +00:00
|
|
|
}
|
|
|
|
|
2016-08-23 23:01:24 +00:00
|
|
|
// NeighProxyList gets a list of neighbor proxies in the system.
|
|
|
|
// Equivalent to: `ip neighbor show proxy`.
|
|
|
|
// The list can be filtered by link and ip family.
|
|
|
|
func NeighProxyList(linkIndex, family int) ([]Neigh, error) {
|
|
|
|
return pkgHandle.NeighProxyList(linkIndex, family)
|
|
|
|
}
|
|
|
|
|
2016-05-08 18:35:49 +00:00
|
|
|
// NeighList gets a list of IP-MAC mappings in the system (ARP table).
|
|
|
|
// Equivalent to: `ip neighbor show`.
|
|
|
|
// The list can be filtered by link and ip family.
|
|
|
|
func (h *Handle) NeighList(linkIndex, family int) ([]Neigh, error) {
|
2016-08-23 23:01:24 +00:00
|
|
|
return h.neighList(linkIndex, family, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NeighProxyList gets a list of neighbor proxies in the system.
|
|
|
|
// Equivalent to: `ip neighbor show proxy`.
|
|
|
|
// The list can be filtered by link, ip family.
|
|
|
|
func (h *Handle) NeighProxyList(linkIndex, family int) ([]Neigh, error) {
|
|
|
|
return h.neighList(linkIndex, family, NTF_PROXY)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handle) neighList(linkIndex, family, flags int) ([]Neigh, error) {
|
2017-10-20 20:38:07 +00:00
|
|
|
req := h.newNetlinkRequest(unix.RTM_GETNEIGH, unix.NLM_F_DUMP)
|
2014-09-16 00:44:20 +00:00
|
|
|
msg := Ndmsg{
|
|
|
|
Family: uint8(family),
|
2015-11-13 00:37:23 +00:00
|
|
|
Index: uint32(linkIndex),
|
2016-08-23 23:01:24 +00:00
|
|
|
Flags: uint8(flags),
|
2014-09-16 00:44:20 +00:00
|
|
|
}
|
|
|
|
req.AddData(&msg)
|
|
|
|
|
2017-10-20 20:38:07 +00:00
|
|
|
msgs, err := req.Execute(unix.NETLINK_ROUTE, unix.RTM_NEWNEIGH)
|
2014-09-16 00:44:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-06-19 00:41:46 +00:00
|
|
|
var res []Neigh
|
2014-09-16 00:44:20 +00:00
|
|
|
for _, m := range msgs {
|
|
|
|
ndm := deserializeNdmsg(m)
|
|
|
|
if linkIndex != 0 && int(ndm.Index) != linkIndex {
|
|
|
|
// Ignore messages from other interfaces
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
neigh, err := NeighDeserialize(m)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
res = append(res, *neigh)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NeighDeserialize(m []byte) (*Neigh, error) {
|
|
|
|
msg := deserializeNdmsg(m)
|
|
|
|
|
|
|
|
neigh := Neigh{
|
2014-10-13 23:09:09 +00:00
|
|
|
LinkIndex: int(msg.Index),
|
|
|
|
Family: int(msg.Family),
|
|
|
|
State: int(msg.State),
|
|
|
|
Type: int(msg.Type),
|
|
|
|
Flags: int(msg.Flags),
|
2014-09-16 00:44:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
attrs, err := nl.ParseRouteAttr(m[msg.Len():])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-08-29 00:59:25 +00:00
|
|
|
// This should be cached for perfomance
|
|
|
|
// once per table dump
|
|
|
|
link, err := LinkByIndex(neigh.LinkIndex)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
encapType := link.Attrs().EncapType
|
|
|
|
|
2014-09-16 00:44:20 +00:00
|
|
|
for _, attr := range attrs {
|
|
|
|
switch attr.Attr.Type {
|
|
|
|
case NDA_DST:
|
|
|
|
neigh.IP = net.IP(attr.Value)
|
|
|
|
case NDA_LLADDR:
|
2017-08-29 00:59:25 +00:00
|
|
|
// BUG: Is this a bug in the netlink library?
|
|
|
|
// #define RTA_LENGTH(len) (RTA_ALIGN(sizeof(struct rtattr)) + (len))
|
|
|
|
// #define RTA_PAYLOAD(rta) ((int)((rta)->rta_len) - RTA_LENGTH(0))
|
2017-10-20 20:38:07 +00:00
|
|
|
attrLen := attr.Attr.Len - unix.SizeofRtAttr
|
2017-08-29 00:59:25 +00:00
|
|
|
if attrLen == 4 && (encapType == "ipip" ||
|
|
|
|
encapType == "sit" ||
|
|
|
|
encapType == "gre") {
|
|
|
|
neigh.LLIPAddr = net.IP(attr.Value)
|
|
|
|
} else if attrLen == 16 &&
|
|
|
|
encapType == "tunnel6" {
|
|
|
|
neigh.IP = net.IP(attr.Value)
|
|
|
|
} else {
|
|
|
|
neigh.HardwareAddr = net.HardwareAddr(attr.Value)
|
|
|
|
}
|
2017-05-04 13:15:30 +00:00
|
|
|
case NDA_VLAN:
|
|
|
|
neigh.Vlan = int(native.Uint16(attr.Value[0:2]))
|
|
|
|
case NDA_VNI:
|
|
|
|
neigh.VNI = int(native.Uint32(attr.Value[0:4]))
|
2014-09-16 00:44:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &neigh, nil
|
|
|
|
}
|