2015-07-02 01:00:48 -04:00
|
|
|
package osl
|
2015-06-15 14:35:13 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
|
2017-07-26 17:18:31 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2015-06-15 14:35:13 -04:00
|
|
|
"github.com/vishvananda/netlink"
|
|
|
|
)
|
|
|
|
|
2017-09-05 13:43:20 -04:00
|
|
|
// NeighborSearchError indicates that the neighbor is already present
|
|
|
|
type NeighborSearchError struct {
|
|
|
|
ip net.IP
|
|
|
|
mac net.HardwareAddr
|
|
|
|
present bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n NeighborSearchError) Error() string {
|
|
|
|
return fmt.Sprintf("Search neighbor failed for IP %v, mac %v, present in db:%t", n.ip, n.mac, n.present)
|
|
|
|
}
|
|
|
|
|
2015-06-15 14:35:13 -04:00
|
|
|
// NeighOption is a function option type to set interface options
|
|
|
|
type NeighOption func(nh *neigh)
|
|
|
|
|
|
|
|
type neigh struct {
|
|
|
|
dstIP net.IP
|
|
|
|
dstMac net.HardwareAddr
|
|
|
|
linkName string
|
|
|
|
linkDst string
|
|
|
|
family int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *networkNamespace) findNeighbor(dstIP net.IP, dstMac net.HardwareAddr) *neigh {
|
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
|
|
|
|
for _, nh := range n.neighbors {
|
|
|
|
if nh.dstIP.Equal(dstIP) && bytes.Equal(nh.dstMac, dstMac) {
|
|
|
|
return nh
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-07 13:45:40 -04:00
|
|
|
func (n *networkNamespace) DeleteNeighbor(dstIP net.IP, dstMac net.HardwareAddr, osDelete bool) error {
|
2016-05-16 14:51:40 -04:00
|
|
|
var (
|
|
|
|
iface netlink.Link
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
2015-06-15 14:35:13 -04:00
|
|
|
nh := n.findNeighbor(dstIP, dstMac)
|
|
|
|
if nh == nil {
|
2017-09-05 13:43:20 -04:00
|
|
|
return NeighborSearchError{dstIP, dstMac, false}
|
2015-06-15 14:35:13 -04:00
|
|
|
}
|
|
|
|
|
2016-09-07 13:45:40 -04:00
|
|
|
if osDelete {
|
|
|
|
n.Lock()
|
|
|
|
nlh := n.nlHandle
|
|
|
|
n.Unlock()
|
|
|
|
|
|
|
|
if nh.linkDst != "" {
|
|
|
|
iface, err = nlh.LinkByName(nh.linkDst)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not find interface with destination name %s: %v",
|
|
|
|
nh.linkDst, err)
|
|
|
|
}
|
2015-06-15 14:35:13 -04:00
|
|
|
}
|
|
|
|
|
2016-09-07 13:45:40 -04:00
|
|
|
nlnh := &netlink.Neigh{
|
|
|
|
IP: dstIP,
|
|
|
|
State: netlink.NUD_PERMANENT,
|
|
|
|
Family: nh.family,
|
|
|
|
}
|
2015-06-15 14:35:13 -04:00
|
|
|
|
2016-09-07 13:45:40 -04:00
|
|
|
if nlnh.Family > 0 {
|
|
|
|
nlnh.HardwareAddr = dstMac
|
|
|
|
nlnh.Flags = netlink.NTF_SELF
|
|
|
|
}
|
2015-06-15 14:35:13 -04:00
|
|
|
|
2016-09-07 13:45:40 -04:00
|
|
|
if nh.linkDst != "" {
|
|
|
|
nlnh.LinkIndex = iface.Attrs().Index
|
|
|
|
}
|
2015-06-15 14:35:13 -04:00
|
|
|
|
2017-03-16 21:12:52 -04:00
|
|
|
// If the kernel deletion fails for the neighbor entry still remote it
|
|
|
|
// from the namespace cache. Otherwise if the neighbor moves back to the
|
|
|
|
// same host again, kernel update can fail.
|
2016-09-07 13:45:40 -04:00
|
|
|
if err := nlh.NeighDel(nlnh); err != nil {
|
2017-03-16 21:12:52 -04:00
|
|
|
logrus.Warnf("Deleting neighbor IP %s, mac %s failed, %v", dstIP, dstMac, err)
|
2016-09-07 13:45:40 -04:00
|
|
|
}
|
2017-06-01 15:20:46 -04:00
|
|
|
|
|
|
|
// Delete the dynamic entry in the bridge
|
|
|
|
if nlnh.Family > 0 {
|
|
|
|
nlnh := &netlink.Neigh{
|
|
|
|
IP: dstIP,
|
|
|
|
Family: nh.family,
|
|
|
|
}
|
|
|
|
|
|
|
|
nlnh.HardwareAddr = dstMac
|
|
|
|
nlnh.Flags = netlink.NTF_MASTER
|
|
|
|
if nh.linkDst != "" {
|
|
|
|
nlnh.LinkIndex = iface.Attrs().Index
|
|
|
|
}
|
2017-07-28 18:21:54 -04:00
|
|
|
nlh.NeighDel(nlnh)
|
2017-06-01 15:20:46 -04:00
|
|
|
}
|
2016-05-16 14:51:40 -04:00
|
|
|
}
|
2015-06-15 14:35:13 -04:00
|
|
|
|
2016-09-07 13:45:40 -04:00
|
|
|
n.Lock()
|
2016-05-16 14:51:40 -04:00
|
|
|
for i, nh := range n.neighbors {
|
|
|
|
if nh.dstIP.Equal(dstIP) && bytes.Equal(nh.dstMac, dstMac) {
|
|
|
|
n.neighbors = append(n.neighbors[:i], n.neighbors[i+1:]...)
|
2016-11-15 00:41:22 -05:00
|
|
|
break
|
2015-06-15 14:35:13 -04:00
|
|
|
}
|
2016-05-16 14:51:40 -04:00
|
|
|
}
|
2016-09-07 13:45:40 -04:00
|
|
|
n.Unlock()
|
2017-09-05 13:43:20 -04:00
|
|
|
logrus.Debugf("Neighbor entry deleted for IP %v, mac %v osDelete:%t", dstIP, dstMac, osDelete)
|
2015-06-15 14:35:13 -04:00
|
|
|
|
2016-05-16 14:51:40 -04:00
|
|
|
return nil
|
2015-06-15 14:35:13 -04:00
|
|
|
}
|
|
|
|
|
2017-03-16 21:12:52 -04:00
|
|
|
func (n *networkNamespace) AddNeighbor(dstIP net.IP, dstMac net.HardwareAddr, force bool, options ...NeighOption) error {
|
2016-05-16 14:51:40 -04:00
|
|
|
var (
|
2017-09-05 13:43:20 -04:00
|
|
|
iface netlink.Link
|
|
|
|
err error
|
|
|
|
neighborAlreadyPresent bool
|
2016-05-16 14:51:40 -04:00
|
|
|
)
|
|
|
|
|
2017-03-16 21:12:52 -04:00
|
|
|
// If the namespace already has the neighbor entry but the AddNeighbor is called
|
|
|
|
// because of a miss notification (force flag) program the kernel anyway.
|
2015-06-15 14:35:13 -04:00
|
|
|
nh := n.findNeighbor(dstIP, dstMac)
|
|
|
|
if nh != nil {
|
2017-09-05 13:43:20 -04:00
|
|
|
neighborAlreadyPresent = true
|
|
|
|
logrus.Warnf("Neighbor entry already present for IP %v, mac %v neighbor:%+v forceUpdate:%t", dstIP, dstMac, nh, force)
|
2017-03-16 21:12:52 -04:00
|
|
|
if !force {
|
2017-09-05 13:43:20 -04:00
|
|
|
return NeighborSearchError{dstIP, dstMac, true}
|
2017-03-16 21:12:52 -04:00
|
|
|
}
|
2015-06-15 14:35:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
nh = &neigh{
|
|
|
|
dstIP: dstIP,
|
|
|
|
dstMac: dstMac,
|
|
|
|
}
|
|
|
|
|
|
|
|
nh.processNeighOptions(options...)
|
|
|
|
|
|
|
|
if nh.linkName != "" {
|
|
|
|
nh.linkDst = n.findDst(nh.linkName, false)
|
|
|
|
if nh.linkDst == "" {
|
|
|
|
return fmt.Errorf("could not find the interface with name %s", nh.linkName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-16 14:51:40 -04:00
|
|
|
n.Lock()
|
|
|
|
nlh := n.nlHandle
|
|
|
|
n.Unlock()
|
|
|
|
|
|
|
|
if nh.linkDst != "" {
|
|
|
|
iface, err = nlh.LinkByName(nh.linkDst)
|
|
|
|
if err != nil {
|
2017-09-05 13:43:20 -04:00
|
|
|
return fmt.Errorf("could not find interface with destination name %s: %v", nh.linkDst, err)
|
2015-06-15 14:35:13 -04:00
|
|
|
}
|
2016-05-16 14:51:40 -04:00
|
|
|
}
|
2015-06-15 14:35:13 -04:00
|
|
|
|
2016-05-16 14:51:40 -04:00
|
|
|
nlnh := &netlink.Neigh{
|
|
|
|
IP: dstIP,
|
|
|
|
HardwareAddr: dstMac,
|
|
|
|
State: netlink.NUD_PERMANENT,
|
|
|
|
Family: nh.family,
|
|
|
|
}
|
2015-06-15 14:35:13 -04:00
|
|
|
|
2016-05-16 14:51:40 -04:00
|
|
|
if nlnh.Family > 0 {
|
|
|
|
nlnh.Flags = netlink.NTF_SELF
|
|
|
|
}
|
2015-06-15 14:35:13 -04:00
|
|
|
|
2016-05-16 14:51:40 -04:00
|
|
|
if nh.linkDst != "" {
|
|
|
|
nlnh.LinkIndex = iface.Attrs().Index
|
|
|
|
}
|
2015-06-15 14:35:13 -04:00
|
|
|
|
2016-05-16 14:51:40 -04:00
|
|
|
if err := nlh.NeighSet(nlnh); err != nil {
|
2017-09-05 13:43:20 -04:00
|
|
|
return fmt.Errorf("could not add neighbor entry:%+v error:%v", nlnh, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if neighborAlreadyPresent {
|
|
|
|
return nil
|
2016-05-16 14:51:40 -04:00
|
|
|
}
|
2015-06-15 14:35:13 -04:00
|
|
|
|
2017-03-02 03:04:09 -05:00
|
|
|
n.Lock()
|
2016-05-16 14:51:40 -04:00
|
|
|
n.neighbors = append(n.neighbors, nh)
|
2017-03-02 03:04:09 -05:00
|
|
|
n.Unlock()
|
2017-09-20 11:37:18 -04:00
|
|
|
logrus.Debugf("Neighbor entry added for IP:%v, mac:%v on ifc:%s", dstIP, dstMac, nh.linkName)
|
2015-06-15 14:35:13 -04:00
|
|
|
|
2016-05-16 14:51:40 -04:00
|
|
|
return nil
|
2015-06-15 14:35:13 -04:00
|
|
|
}
|