Skip to content

Commit 607e90b

Browse files
author
Ravi Sankar Penta
committed
Bug 1505266 - Validate node IP is local during sdn node initialization
1 parent 913ec92 commit 607e90b

File tree

3 files changed

+48
-35
lines changed

3 files changed

+48
-35
lines changed

pkg/network/common/common.go

+29
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import (
2222
kapi "k8s.io/kubernetes/pkg/api"
2323
"k8s.io/kubernetes/pkg/apis/extensions"
2424
kinternalinformers "k8s.io/kubernetes/pkg/client/informers/informers_generated/internalversion"
25+
26+
"github.com/vishvananda/netlink"
2527
)
2628

2729
func HostSubnetToString(subnet *networkapi.HostSubnet) string {
@@ -276,3 +278,30 @@ func RegisterSharedInformerEventHandlers(kubeInformers kinternalinformers.Shared
276278
},
277279
})
278280
}
281+
282+
var (
283+
ErrorNetworkInterfaceNotFound = fmt.Errorf("could not find network interface")
284+
)
285+
286+
func GetLinkDetails(ip string) (netlink.Link, *net.IPNet, error) {
287+
links, err := netlink.LinkList()
288+
if err != nil {
289+
return nil, nil, err
290+
}
291+
292+
for _, link := range links {
293+
addrs, err := netlink.AddrList(link, netlink.FAMILY_V4)
294+
if err != nil {
295+
glog.Warningf("Could not get addresses of interface %q: %v", link.Attrs().Name, err)
296+
continue
297+
}
298+
299+
for _, addr := range addrs {
300+
if addr.IP.String() == ip {
301+
return link, addr.IPNet, nil
302+
}
303+
}
304+
}
305+
306+
return nil, nil, ErrorNetworkInterfaceNotFound
307+
}

pkg/network/node/egressip.go

+11-35
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,8 @@ type egressIPWatcher struct {
5151
namespacesByVNID map[uint32]*namespaceEgress
5252
namespacesByEgressIP map[string]*namespaceEgress
5353

54-
localEgressLink netlink.Link
55-
localEgressNet *net.IPNet
56-
localEgressIPMaskLen int
54+
localEgressLink netlink.Link
55+
localEgressNet *net.IPNet
5756

5857
testModeChan chan string
5958
}
@@ -72,8 +71,11 @@ func newEgressIPWatcher(localIP string, oc *ovsController) *egressIPWatcher {
7271
}
7372

7473
func (eip *egressIPWatcher) Start(networkClient networkclient.Interface, iptables *NodeIPTables) error {
75-
if err := eip.findEgressLink(); err != nil {
76-
return fmt.Errorf("could not find egress network interface: %v", err)
74+
var err error
75+
eip.localEgressLink, eip.localEgressNet, err = common.GetLinkDetails(eip.localIP)
76+
if err != nil {
77+
// Not expected, should already be caught by node.New()
78+
return err
7779
}
7880

7981
eip.iptables = iptables
@@ -84,34 +86,6 @@ func (eip *egressIPWatcher) Start(networkClient networkclient.Interface, iptable
8486
return nil
8587
}
8688

87-
func (eip *egressIPWatcher) findEgressLink() error {
88-
links, err := netlink.LinkList()
89-
if err != nil {
90-
return err
91-
}
92-
for _, link := range links {
93-
addrs, err := netlink.AddrList(link, syscall.AF_INET)
94-
if err != nil {
95-
glog.Warningf("Could not get addresses of interface %q while trying to find egress interface: %v", link.Attrs().Name, err)
96-
continue
97-
}
98-
99-
for _, addr := range addrs {
100-
if addr.IP.String() == eip.localIP {
101-
_, eip.localEgressNet, err = net.ParseCIDR(addr.IPNet.String())
102-
if err != nil {
103-
return fmt.Errorf("could not parse CIDR network from address %q: %v", addr.IP.String(), err)
104-
}
105-
eip.localEgressLink = link
106-
eip.localEgressIPMaskLen, _ = addr.Mask.Size()
107-
return nil
108-
}
109-
}
110-
}
111-
112-
return fmt.Errorf("could not find network interface with the address %q", eip.localIP)
113-
}
114-
11589
func ipToHex(ip string) string {
11690
bytes := net.ParseIP(ip)
11791
if bytes == nil {
@@ -287,7 +261,8 @@ func (eip *egressIPWatcher) claimEgressIP(egressIP, egressHex string) error {
287261
return nil
288262
}
289263

290-
egressIPNet := fmt.Sprintf("%s/%d", egressIP, eip.localEgressIPMaskLen)
264+
localEgressIPMaskLen, _ := eip.localEgressNet.Mask.Size()
265+
egressIPNet := fmt.Sprintf("%s/%d", egressIP, localEgressIPMaskLen)
291266
addr, err := netlink.ParseAddr(egressIPNet)
292267
if err != nil {
293268
return fmt.Errorf("could not parse egress IP %q: %v", egressIPNet, err)
@@ -317,7 +292,8 @@ func (eip *egressIPWatcher) releaseEgressIP(egressIP, egressHex string) error {
317292
return nil
318293
}
319294

320-
egressIPNet := fmt.Sprintf("%s/%d", egressIP, eip.localEgressIPMaskLen)
295+
localEgressIPMaskLen, _ := eip.localEgressNet.Mask.Size()
296+
egressIPNet := fmt.Sprintf("%s/%d", egressIP, localEgressIPMaskLen)
321297
addr, err := netlink.ParseAddr(egressIPNet)
322298
if err != nil {
323299
return fmt.Errorf("could not parse egress IP %q: %v", egressIPNet, err)

pkg/network/node/node.go

+8
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,14 @@ func (c *OsdnNodeConfig) setNodeIP() error {
222222
}
223223
}
224224

225+
if _, _, err := common.GetLinkDetails(c.SelfIP); err != nil {
226+
if err == common.ErrorNetworkInterfaceNotFound {
227+
return fmt.Errorf("node IP %q is not a local/private address (hostname %q)", c.SelfIP, c.Hostname)
228+
} else {
229+
return err
230+
}
231+
}
232+
225233
return nil
226234
}
227235

0 commit comments

Comments
 (0)