Skip to content

Commit 7ac993f

Browse files
IPv6: add iputil package, filter ips by version 4 when looking up underlayIPs interface names
1 parent c31f317 commit 7ac993f

File tree

4 files changed

+54
-17
lines changed

4 files changed

+54
-17
lines changed

src/code.cloudfoundry.org/cni-wrapper-plugin/main.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"code.cloudfoundry.org/lib/common"
2020
"code.cloudfoundry.org/lib/datastore"
2121
"code.cloudfoundry.org/lib/interfacelookup"
22+
"code.cloudfoundry.org/lib/iputil"
2223
"code.cloudfoundry.org/lib/rules"
2324
"code.cloudfoundry.org/lib/serial"
2425
"github.com/containernetworking/cni/pkg/skel"
@@ -149,7 +150,8 @@ func cmdAdd(args *skel.CmdArgs) error {
149150
if len(cfg.TemporaryUnderlayInterfaceNames) > 0 {
150151
interfaceNames = cfg.TemporaryUnderlayInterfaceNames
151152
} else {
152-
interfaceNames, err = interfaceNameLookup.GetNamesFromIPs(cfg.UnderlayIPs)
153+
v4UnderlayIps, _ := iputil.FilterIPsByVersion(cfg.UnderlayIPs, iputil.IPVersion4)
154+
interfaceNames, err = interfaceNameLookup.GetNamesFromIPs(v4UnderlayIps)
153155
if err != nil {
154156
return fmt.Errorf("looking up interface names: %s", err) // not tested
155157
}
@@ -427,14 +429,12 @@ func cmdDel(args *skel.CmdArgs) error {
427429
NetlinkAdapter: &adapter.NetlinkAdapter{},
428430
}
429431

430-
// TODO: The way cfg.UnderlayIPs is populated is from bosh spec.networks. This will be a problem if there are ipv6 networks there
431-
// because we will not be able to find the interface name for ipv6 networks and error will be returned.
432-
// Should be fixed in the future when theres a working bosh ipv6 implementation
433432
var interfaceNames []string
434433
if len(cfg.TemporaryUnderlayInterfaceNames) > 0 {
435434
interfaceNames = cfg.TemporaryUnderlayInterfaceNames
436435
} else {
437-
interfaceNames, err = interfaceNameLookup.GetNamesFromIPs(cfg.UnderlayIPs)
436+
v4UnderlayIps, _ := iputil.FilterIPsByVersion(cfg.UnderlayIPs, iputil.IPVersion4)
437+
interfaceNames, err = interfaceNameLookup.GetNamesFromIPs(v4UnderlayIps)
438438
if err != nil {
439439
return fmt.Errorf("looking up interface names: %s", err) // not tested
440440
}

src/code.cloudfoundry.org/lib/interfacelookup/interface_name_lookup.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type InterfaceNameLookup struct {
2323
NetlinkAdapter netlinkAdapter
2424
}
2525

26+
// GetNameFromIP Only works for IPv4 addresses
2627
func (i InterfaceNameLookup) GetNameFromIP(ip string) (string, error) {
2728
links, err := common.RetryWithBackoff(retryInterval, maxRetries, func() ([]netlink.Link, error) {
2829
return i.NetlinkAdapter.LinkList()
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package iputil
2+
3+
import (
4+
"fmt"
5+
"net"
6+
)
7+
8+
type IPVersion int
9+
10+
const (
11+
InvalidIPVersion IPVersion = iota
12+
IPVersion4 IPVersion = 4
13+
IPVersion6 IPVersion = 6
14+
)
15+
16+
func GetFamily(ip net.IP) IPVersion {
17+
if len(ip) == 0 {
18+
return InvalidIPVersion
19+
}
20+
if ip.To4() != nil {
21+
return IPVersion4
22+
}
23+
if ip.To16() != nil {
24+
return IPVersion6
25+
}
26+
return InvalidIPVersion
27+
}
28+
29+
func FilterIPsByVersion(ipStrs []string, version IPVersion) ([]string, error) {
30+
filtered := make([]string, 0, len(ipStrs))
31+
32+
for _, str := range ipStrs {
33+
ip := net.ParseIP(str)
34+
if ip == nil {
35+
return nil, fmt.Errorf("invalid IP string: %s", str)
36+
}
37+
38+
family := GetFamily(ip)
39+
40+
if family == version {
41+
filtered = append(filtered, str)
42+
}
43+
}
44+
45+
return filtered, nil
46+
}

src/code.cloudfoundry.org/vxlan-policy-agent/cmd/vxlan-policy-agent/main.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"flag"
55
"fmt"
66
"log"
7-
"net"
87
"net/http"
98
"os"
109
"path/filepath"
@@ -18,6 +17,7 @@ import (
1817
"code.cloudfoundry.org/lib/common"
1918
"code.cloudfoundry.org/lib/datastore"
2019
"code.cloudfoundry.org/lib/interfacelookup"
20+
"code.cloudfoundry.org/lib/iputil"
2121
"code.cloudfoundry.org/lib/rules"
2222
"code.cloudfoundry.org/lib/serial"
2323
"code.cloudfoundry.org/policy_client"
@@ -383,17 +383,7 @@ func createForceUpdateServer(listenAddress string, handlers map[string]http.Hand
383383
}
384384

385385
func lookupInterfaceNames(lookup interfacelookup.InterfaceNameLookup, ips []string) ([]string, error) {
386-
var ipsV4 []string
387-
for _, ip := range ips {
388-
parsed := net.ParseIP(ip)
389-
if parsed == nil {
390-
return nil, fmt.Errorf("invalid IP address: %s", ip)
391-
}
392-
if parsed.To4() != nil {
393-
ipsV4 = append(ipsV4, ip)
394-
}
395-
}
396-
386+
ipsV4, _ := iputil.FilterIPsByVersion(ips, iputil.IPVersion4)
397387
var v4names, err = lookup.GetNamesFromIPs(ipsV4)
398388
if err != nil {
399389
return nil, err

0 commit comments

Comments
 (0)