Support IPv6 GRE Tun and Tap

Signed-off-by: Alessandro Boch <aboch@tetrationanalytics.com>
This commit is contained in:
Alessandro Boch 2018-01-18 21:27:08 -08:00 committed by Alessandro Boch
parent 7291c36428
commit 5a988e882d
3 changed files with 57 additions and 15 deletions

View File

@ -705,6 +705,9 @@ func (gretap *Gretap) Attrs() *LinkAttrs {
}
func (gretap *Gretap) Type() string {
if gretap.Local.To4() == nil {
return "ip6gretap"
}
return "gretap"
}
@ -793,6 +796,9 @@ func (gretun *Gretun) Attrs() *LinkAttrs {
}
func (gretun *Gretun) Type() string {
if gretun.Local.To4() == nil {
return "ip6gre"
}
return "gre"
}

View File

@ -1310,12 +1310,16 @@ func LinkDeserialize(hdr *unix.NlMsghdr, m []byte) (Link, error) {
link = &Macvtap{}
case "gretap":
link = &Gretap{}
case "ip6gretap":
link = &Gretap{}
case "ipip":
link = &Iptun{}
case "sit":
link = &Sittun{}
case "gre":
link = &Gretun{}
case "ip6gre":
link = &Gretun{}
case "vti":
link = &Vti{}
case "vrf":
@ -1345,12 +1349,16 @@ func LinkDeserialize(hdr *unix.NlMsghdr, m []byte) (Link, error) {
parseMacvtapData(link, data)
case "gretap":
parseGretapData(link, data)
case "ip6gretap":
parseGretapData(link, data)
case "ipip":
parseIptunData(link, data)
case "sit":
parseSittunData(link, data)
case "gre":
parseGretunData(link, data)
case "ip6gre":
parseGretunData(link, data)
case "vti":
parseVtiData(link, data)
case "vrf":
@ -1850,12 +1858,17 @@ func addGretapAttrs(gretap *Gretap, linkInfo *nl.RtAttr) {
return
}
ip := gretap.Local.To4()
if ip != nil {
if ip := gretap.Local; ip != nil {
if ip.To4() != nil {
ip = ip.To4()
}
nl.NewRtAttrChild(data, nl.IFLA_GRE_LOCAL, []byte(ip))
}
ip = gretap.Remote.To4()
if ip != nil {
if ip := gretap.Remote; ip != nil {
if ip.To4() != nil {
ip = ip.To4()
}
nl.NewRtAttrChild(data, nl.IFLA_GRE_REMOTE, []byte(ip))
}
@ -1894,9 +1907,9 @@ func parseGretapData(link Link, data []syscall.NetlinkRouteAttr) {
case nl.IFLA_GRE_IKEY:
gre.OKey = ntohl(datum.Value[0:4])
case nl.IFLA_GRE_LOCAL:
gre.Local = net.IP(datum.Value[0:4])
gre.Local = net.IP(datum.Value[0:16])
case nl.IFLA_GRE_REMOTE:
gre.Remote = net.IP(datum.Value[0:4])
gre.Remote = net.IP(datum.Value[0:16])
case nl.IFLA_GRE_ENCAP_SPORT:
gre.EncapSport = ntohs(datum.Value[0:2])
case nl.IFLA_GRE_ENCAP_DPORT:
@ -1927,12 +1940,17 @@ func parseGretapData(link Link, data []syscall.NetlinkRouteAttr) {
func addGretunAttrs(gre *Gretun, linkInfo *nl.RtAttr) {
data := nl.NewRtAttrChild(linkInfo, nl.IFLA_INFO_DATA, nil)
ip := gre.Local.To4()
if ip != nil {
if ip := gre.Local; ip != nil {
if ip.To4() != nil {
ip = ip.To4()
}
nl.NewRtAttrChild(data, nl.IFLA_GRE_LOCAL, []byte(ip))
}
ip = gre.Remote.To4()
if ip != nil {
if ip := gre.Remote; ip != nil {
if ip.To4() != nil {
ip = ip.To4()
}
nl.NewRtAttrChild(data, nl.IFLA_GRE_REMOTE, []byte(ip))
}
@ -1971,9 +1989,9 @@ func parseGretunData(link Link, data []syscall.NetlinkRouteAttr) {
case nl.IFLA_GRE_IKEY:
gre.OKey = ntohl(datum.Value[0:4])
case nl.IFLA_GRE_LOCAL:
gre.Local = net.IP(datum.Value[0:4])
gre.Local = net.IP(datum.Value[0:16])
case nl.IFLA_GRE_REMOTE:
gre.Remote = net.IP(datum.Value[0:4])
gre.Remote = net.IP(datum.Value[0:16])
case nl.IFLA_GRE_IFLAGS:
gre.IFlags = ntohs(datum.Value[0:2])
case nl.IFLA_GRE_OFLAGS:

View File

@ -314,12 +314,20 @@ func TestLinkAddDelGretap(t *testing.T) {
defer tearDown()
testLinkAddDel(t, &Gretap{
LinkAttrs: LinkAttrs{Name: "foo"},
LinkAttrs: LinkAttrs{Name: "foo4"},
IKey: 0x101,
OKey: 0x101,
PMtuDisc: 1,
Local: net.IPv4(127, 0, 0, 1),
Remote: net.IPv4(127, 0, 0, 1)})
testLinkAddDel(t, &Gretap{
LinkAttrs: LinkAttrs{Name: "foo6"},
IKey: 0x101,
OKey: 0x101,
PMtuDisc: 1,
Local: net.ParseIP("2001:db8:abcd::1"),
Remote: net.ParseIP("2001:db8:ef33::2")})
}
func TestLinkAddDelGretun(t *testing.T) {
@ -327,9 +335,14 @@ func TestLinkAddDelGretun(t *testing.T) {
defer tearDown()
testLinkAddDel(t, &Gretun{
LinkAttrs: LinkAttrs{Name: "foo"},
LinkAttrs: LinkAttrs{Name: "foo4"},
Local: net.IPv4(127, 0, 0, 1),
Remote: net.IPv4(127, 0, 0, 1)})
testLinkAddDel(t, &Gretun{
LinkAttrs: LinkAttrs{Name: "foo6"},
Local: net.ParseIP("2001:db8:abcd::1"),
Remote: net.ParseIP("2001:db8:ef33::2")})
}
func TestLinkAddDelGretunPointToMultiPoint(t *testing.T) {
@ -341,10 +354,15 @@ func TestLinkAddDelGretunPointToMultiPoint(t *testing.T) {
Local: net.IPv4(127, 0, 0, 1),
IKey: 1234,
OKey: 1234})
testLinkAddDel(t, &Gretun{
LinkAttrs: LinkAttrs{Name: "foo6"},
Local: net.ParseIP("2001:db8:1234::4"),
IKey: 5678,
OKey: 7890})
}
func TestLinkAddDelGretapFlowBased(t *testing.T) {
t.Skip("Fails with \"link_test.go:29: numerical result out of range\". Need to investigate.")
minKernelRequired(t, 4, 3)
tearDown := setUpNetlinkTest(t)