Add ip6tnl support

This commit is contained in:
Oleg Senin 2019-09-24 02:17:22 +03:00 committed by Alessandro Boch
parent 07ace697be
commit 6b3a223c53
3 changed files with 94 additions and 0 deletions

21
link.go
View File

@ -871,6 +871,27 @@ func (iptun *Iptun) Type() string {
return "ipip"
}
type Ip6tnl struct {
LinkAttrs
Link uint32
Local net.IP
Remote net.IP
Ttl uint8
Tos uint8
EncapLimit uint8
Flags uint32
Proto uint8
FlowInfo uint32
}
func (ip6tnl *Ip6tnl) Attrs() *LinkAttrs {
return &ip6tnl.LinkAttrs
}
func (ip6tnl *Ip6tnl) Type() string {
return "ip6tnl"
}
type Sittun struct {
LinkAttrs
Link uint32

View File

@ -1300,6 +1300,8 @@ func (h *Handle) linkModify(link Link, flags int) error {
addGretapAttrs(link, linkInfo)
case *Iptun:
addIptunAttrs(link, linkInfo)
case *Ip6tnl:
addIp6tnlAttrs(link, linkInfo)
case *Sittun:
addSittunAttrs(link, linkInfo)
case *Gretun:
@ -1557,6 +1559,8 @@ func LinkDeserialize(hdr *unix.NlMsghdr, m []byte) (Link, error) {
link = &Gretap{}
case "ipip":
link = &Iptun{}
case "ip6tnl":
link = &Ip6tnl{}
case "sit":
link = &Sittun{}
case "gre":
@ -1602,6 +1606,8 @@ func LinkDeserialize(hdr *unix.NlMsghdr, m []byte) (Link, error) {
parseGretapData(link, data)
case "ipip":
parseIptunData(link, data)
case "ip6tnl":
parseIp6tnlData(link, data)
case "sit":
parseSittunData(link, data)
case "gre":
@ -2557,6 +2563,55 @@ func parseIptunData(link Link, data []syscall.NetlinkRouteAttr) {
}
}
func addIp6tnlAttrs(ip6tnl *Ip6tnl, linkInfo *nl.RtAttr) {
data := linkInfo.AddRtAttr(nl.IFLA_INFO_DATA, nil)
if ip6tnl.Link != 0 {
data.AddRtAttr(nl.IFLA_IPTUN_LINK, nl.Uint32Attr(ip6tnl.Link))
}
ip := ip6tnl.Local.To16()
if ip != nil {
data.AddRtAttr(nl.IFLA_IPTUN_LOCAL, []byte(ip))
}
ip = ip6tnl.Remote.To16()
if ip != nil {
data.AddRtAttr(nl.IFLA_IPTUN_REMOTE, []byte(ip))
}
data.AddRtAttr(nl.IFLA_IPTUN_TTL, nl.Uint8Attr(ip6tnl.Ttl))
data.AddRtAttr(nl.IFLA_IPTUN_TOS, nl.Uint8Attr(ip6tnl.Tos))
data.AddRtAttr(nl.IFLA_IPTUN_ENCAP_LIMIT, nl.Uint8Attr(ip6tnl.EncapLimit))
data.AddRtAttr(nl.IFLA_IPTUN_FLAGS, nl.Uint32Attr(ip6tnl.Flags))
data.AddRtAttr(nl.IFLA_IPTUN_PROTO, nl.Uint8Attr(ip6tnl.Proto))
data.AddRtAttr(nl.IFLA_IPTUN_FLOWINFO, nl.Uint32Attr(ip6tnl.FlowInfo))
}
func parseIp6tnlData(link Link, data []syscall.NetlinkRouteAttr) {
ip6tnl := link.(*Ip6tnl)
for _, datum := range data {
switch datum.Attr.Type {
case nl.IFLA_IPTUN_LOCAL:
ip6tnl.Local = net.IP(datum.Value[:16])
case nl.IFLA_IPTUN_REMOTE:
ip6tnl.Remote = net.IP(datum.Value[:16])
case nl.IFLA_IPTUN_TTL:
ip6tnl.Ttl = uint8(datum.Value[0])
case nl.IFLA_IPTUN_TOS:
ip6tnl.Tos = uint8(datum.Value[0])
case nl.IFLA_IPTUN_ENCAP_LIMIT:
ip6tnl.EncapLimit = uint8(datum.Value[0])
case nl.IFLA_IPTUN_FLAGS:
ip6tnl.Flags = native.Uint32(datum.Value[:4])
case nl.IFLA_IPTUN_PROTO:
ip6tnl.Proto = uint8(datum.Value[0])
case nl.IFLA_IPTUN_FLOWINFO:
ip6tnl.FlowInfo = native.Uint32(datum.Value[:4])
}
}
}
func addSittunAttrs(sittun *Sittun, linkInfo *nl.RtAttr) {
data := linkInfo.AddRtAttr(nl.IFLA_INFO_DATA, nil)

View File

@ -200,6 +200,13 @@ func testLinkAddDel(t *testing.T, link Link) {
}
}
if _, ok := link.(*Ip6tnl); ok {
_, ok := result.(*Ip6tnl)
if !ok {
t.Fatal("Result of create is not a ip6tnl")
}
}
if _, ok := link.(*Sittun); ok {
_, ok := result.(*Sittun)
if !ok {
@ -1607,6 +1614,17 @@ func TestLinkAddDelIptun(t *testing.T) {
Remote: net.IPv4(127, 0, 0, 1)})
}
func TestLinkAddDelIp6tnl(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
testLinkAddDel(t, &Ip6tnl{
LinkAttrs: LinkAttrs{Name: "ip6tnltest"},
Local: net.ParseIP("2001:db8::100"),
Remote: net.ParseIP("2001:db8::200"),
})
}
func TestLinkAddDelSittun(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()