2015-07-02 05:00:48 +00:00
|
|
|
package osl
|
2015-06-15 18:35:13 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
|
2017-07-26 21:18:31 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2015-06-15 18:35:13 +00:00
|
|
|
"github.com/vishvananda/netlink"
|
|
|
|
)
|
|
|
|
|
2017-09-05 17:43:20 +00: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 18:35:13 +00: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 17:45:40 +00:00
|
|
|
func (n *networkNamespace) DeleteNeighbor(dstIP net.IP, dstMac net.HardwareAddr, osDelete bool) error {
|
2016-05-16 18:51:40 +00:00
|
|
|
var (
|
|
|
|
iface netlink.Link
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
2015-06-15 18:35:13 +00:00
|
|
|
nh := n.findNeighbor(dstIP, dstMac)
|
|
|
|
if nh == nil {
|
2017-09-05 17:43:20 +00:00
|
|
|
return NeighborSearchError{dstIP, dstMac, false}
|
2015-06-15 18:35:13 +00:00
|
|
|
}
|
|
|
|
|
2016-09-07 17:45:40 +00: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 18:35:13 +00:00
|
|
|
}
|
|
|
|
|
2016-09-07 17:45:40 +00:00
|
|
|
nlnh := &netlink.Neigh{
|
|
|
|
IP: dstIP,
|
|
|
|
State: netlink.NUD_PERMANENT,
|
|
|
|
Family: nh.family,
|
|
|
|
}
|
2015-06-15 18:35:13 +00:00
|
|
|
|
2016-09-07 17:45:40 +00:00
|
|
|
if nlnh.Family > 0 {
|
|
|
|
nlnh.HardwareAddr = dstMac
|
|
|
|
nlnh.Flags = netlink.NTF_SELF
|
|
|
|
}
|
2015-06-15 18:35:13 +00:00
|
|
|
|
2016-09-07 17:45:40 +00:00
|
|
|
if nh.linkDst != "" {
|
|
|
|
nlnh.LinkIndex = iface.Attrs().Index
|
|
|
|
}
|
2015-06-15 18:35:13 +00:00
|
|
|
|
2017-03-17 01:12:52 +00: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 17:45:40 +00:00
|
|
|
if err := nlh.NeighDel(nlnh); err != nil {
|
2017-03-17 01:12:52 +00:00
|
|
|
logrus.Warnf("Deleting neighbor IP %s, mac %s failed, %v", dstIP, dstMac, err)
|
2016-09-07 17:45:40 +00:00
|
|
|
}
|
2017-06-01 19:20:46 +00: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
|
|
|
|
}
|
2021-05-28 00:15:56 +00:00
|
|
|
if err := nlh.NeighDel(nlnh); err != nil {
|
|
|
|
logrus.WithError(err).Warn("error while deleting neighbor entry")
|
|
|
|
}
|
2017-06-01 19:20:46 +00:00
|
|
|
}
|
2016-05-16 18:51:40 +00:00
|
|
|
}
|
2015-06-15 18:35:13 +00:00
|
|
|
|
2016-09-07 17:45:40 +00:00
|
|
|
n.Lock()
|
2016-05-16 18:51:40 +00: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 05:41:22 +00:00
|
|
|
break
|
2015-06-15 18:35:13 +00:00
|
|
|
}
|
2016-05-16 18:51:40 +00:00
|
|
|
}
|
2016-09-07 17:45:40 +00:00
|
|
|
n.Unlock()
|
2017-09-05 17:43:20 +00:00
|
|
|
logrus.Debugf("Neighbor entry deleted for IP %v, mac %v osDelete:%t", dstIP, dstMac, osDelete)
|
2015-06-15 18:35:13 +00:00
|
|
|
|
2016-05-16 18:51:40 +00:00
|
|
|
return nil
|
2015-06-15 18:35:13 +00:00
|
|
|
}
|
|
|
|
|
2017-03-17 01:12:52 +00:00
|
|
|
func (n *networkNamespace) AddNeighbor(dstIP net.IP, dstMac net.HardwareAddr, force bool, options ...NeighOption) error {
|
2016-05-16 18:51:40 +00:00
|
|
|
var (
|
2017-09-05 17:43:20 +00:00
|
|
|
iface netlink.Link
|
|
|
|
err error
|
|
|
|
neighborAlreadyPresent bool
|
2016-05-16 18:51:40 +00:00
|
|
|
)
|
|
|
|
|
2017-03-17 01:12:52 +00: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 18:35:13 +00:00
|
|
|
nh := n.findNeighbor(dstIP, dstMac)
|
|
|
|
if nh != nil {
|
2017-09-05 17:43:20 +00:00
|
|
|
neighborAlreadyPresent = true
|
|
|
|
logrus.Warnf("Neighbor entry already present for IP %v, mac %v neighbor:%+v forceUpdate:%t", dstIP, dstMac, nh, force)
|
2017-03-17 01:12:52 +00:00
|
|
|
if !force {
|
2017-09-05 17:43:20 +00:00
|
|
|
return NeighborSearchError{dstIP, dstMac, true}
|
2017-03-17 01:12:52 +00:00
|
|
|
}
|
2015-06-15 18:35:13 +00: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 18:51:40 +00:00
|
|
|
n.Lock()
|
|
|
|
nlh := n.nlHandle
|
|
|
|
n.Unlock()
|
|
|
|
|
|
|
|
if nh.linkDst != "" {
|
|
|
|
iface, err = nlh.LinkByName(nh.linkDst)
|
|
|
|
if err != nil {
|
2017-09-05 17:43:20 +00:00
|
|
|
return fmt.Errorf("could not find interface with destination name %s: %v", nh.linkDst, err)
|
2015-06-15 18:35:13 +00:00
|
|
|
}
|
2016-05-16 18:51:40 +00:00
|
|
|
}
|
2015-06-15 18:35:13 +00:00
|
|
|
|
2016-05-16 18:51:40 +00:00
|
|
|
nlnh := &netlink.Neigh{
|
|
|
|
IP: dstIP,
|
|
|
|
HardwareAddr: dstMac,
|
|
|
|
State: netlink.NUD_PERMANENT,
|
|
|
|
Family: nh.family,
|
|
|
|
}
|
2015-06-15 18:35:13 +00:00
|
|
|
|
2016-05-16 18:51:40 +00:00
|
|
|
if nlnh.Family > 0 {
|
|
|
|
nlnh.Flags = netlink.NTF_SELF
|
|
|
|
}
|
2015-06-15 18:35:13 +00:00
|
|
|
|
2016-05-16 18:51:40 +00:00
|
|
|
if nh.linkDst != "" {
|
|
|
|
nlnh.LinkIndex = iface.Attrs().Index
|
|
|
|
}
|
2015-06-15 18:35:13 +00:00
|
|
|
|
2016-05-16 18:51:40 +00:00
|
|
|
if err := nlh.NeighSet(nlnh); err != nil {
|
2017-09-05 17:43:20 +00:00
|
|
|
return fmt.Errorf("could not add neighbor entry:%+v error:%v", nlnh, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if neighborAlreadyPresent {
|
|
|
|
return nil
|
2016-05-16 18:51:40 +00:00
|
|
|
}
|
2015-06-15 18:35:13 +00:00
|
|
|
|
2017-03-02 08:04:09 +00:00
|
|
|
n.Lock()
|
2016-05-16 18:51:40 +00:00
|
|
|
n.neighbors = append(n.neighbors, nh)
|
2017-03-02 08:04:09 +00:00
|
|
|
n.Unlock()
|
2017-09-20 15:37:18 +00:00
|
|
|
logrus.Debugf("Neighbor entry added for IP:%v, mac:%v on ifc:%s", dstIP, dstMac, nh.linkName)
|
2015-06-15 18:35:13 +00:00
|
|
|
|
2016-05-16 18:51:40 +00:00
|
|
|
return nil
|
2015-06-15 18:35:13 +00:00
|
|
|
}
|