mirror of
https://github.com/vishvananda/netlink.git
synced 2025-10-05 15:56:54 +08:00
added encapsulation attributes to Iptun
added encapsulation attributes to Gretun implemented Sittun struct for supporting SIT tunnels
This commit is contained in:

committed by
Vish (Ishaya) Abrams

parent
12728257a9
commit
c29ba20204
63
link.go
63
link.go
@@ -700,12 +700,17 @@ func (gretap *Gretap) Type() string {
|
||||
|
||||
type Iptun struct {
|
||||
LinkAttrs
|
||||
Ttl uint8
|
||||
Tos uint8
|
||||
PMtuDisc uint8
|
||||
Link uint32
|
||||
Local net.IP
|
||||
Remote net.IP
|
||||
Ttl uint8
|
||||
Tos uint8
|
||||
PMtuDisc uint8
|
||||
Link uint32
|
||||
Local net.IP
|
||||
Remote net.IP
|
||||
EncapSport uint16
|
||||
EncapDport uint16
|
||||
EncapType uint16
|
||||
EncapFlags uint16
|
||||
FlowBased bool
|
||||
}
|
||||
|
||||
func (iptun *Iptun) Attrs() *LinkAttrs {
|
||||
@@ -716,6 +721,28 @@ func (iptun *Iptun) Type() string {
|
||||
return "ipip"
|
||||
}
|
||||
|
||||
type Sittun struct {
|
||||
LinkAttrs
|
||||
Link uint32
|
||||
Local net.IP
|
||||
Remote net.IP
|
||||
Ttl uint8
|
||||
Tos uint8
|
||||
PMtuDisc uint8
|
||||
EncapType uint16
|
||||
EncapFlags uint16
|
||||
EncapSport uint16
|
||||
EncapDport uint16
|
||||
}
|
||||
|
||||
func (sittun *Sittun) Attrs() *LinkAttrs {
|
||||
return &sittun.LinkAttrs
|
||||
}
|
||||
|
||||
func (sittun *Sittun) Type() string {
|
||||
return "sit"
|
||||
}
|
||||
|
||||
type Vti struct {
|
||||
LinkAttrs
|
||||
IKey uint32
|
||||
@@ -735,16 +762,20 @@ func (iptun *Vti) Type() string {
|
||||
|
||||
type Gretun struct {
|
||||
LinkAttrs
|
||||
Link uint32
|
||||
IFlags uint16
|
||||
OFlags uint16
|
||||
IKey uint32
|
||||
OKey uint32
|
||||
Local net.IP
|
||||
Remote net.IP
|
||||
Ttl uint8
|
||||
Tos uint8
|
||||
PMtuDisc uint8
|
||||
Link uint32
|
||||
IFlags uint16
|
||||
OFlags uint16
|
||||
IKey uint32
|
||||
OKey uint32
|
||||
Local net.IP
|
||||
Remote net.IP
|
||||
Ttl uint8
|
||||
Tos uint8
|
||||
PMtuDisc uint8
|
||||
EncapType uint16
|
||||
EncapFlags uint16
|
||||
EncapSport uint16
|
||||
EncapDport uint16
|
||||
}
|
||||
|
||||
func (gretun *Gretun) Attrs() *LinkAttrs {
|
||||
|
@@ -945,6 +945,8 @@ func (h *Handle) linkModify(link Link, flags int) error {
|
||||
addGretapAttrs(link, linkInfo)
|
||||
case *Iptun:
|
||||
addIptunAttrs(link, linkInfo)
|
||||
case *Sittun:
|
||||
addSittunAttrs(link, linkInfo)
|
||||
case *Gretun:
|
||||
addGretunAttrs(link, linkInfo)
|
||||
case *Vti:
|
||||
@@ -1184,6 +1186,8 @@ func LinkDeserialize(hdr *unix.NlMsghdr, m []byte) (Link, error) {
|
||||
link = &Gretap{}
|
||||
case "ipip":
|
||||
link = &Iptun{}
|
||||
case "sit":
|
||||
link = &Sittun{}
|
||||
case "gre":
|
||||
link = &Gretun{}
|
||||
case "vti":
|
||||
@@ -1217,6 +1221,8 @@ func LinkDeserialize(hdr *unix.NlMsghdr, m []byte) (Link, error) {
|
||||
parseGretapData(link, data)
|
||||
case "ipip":
|
||||
parseIptunData(link, data)
|
||||
case "sit":
|
||||
parseSittunData(link, data)
|
||||
case "gre":
|
||||
parseGretunData(link, data)
|
||||
case "vti":
|
||||
@@ -1810,6 +1816,10 @@ func addGretunAttrs(gre *Gretun, linkInfo *nl.RtAttr) {
|
||||
nl.NewRtAttrChild(data, nl.IFLA_GRE_PMTUDISC, nl.Uint8Attr(gre.PMtuDisc))
|
||||
nl.NewRtAttrChild(data, nl.IFLA_GRE_TTL, nl.Uint8Attr(gre.Ttl))
|
||||
nl.NewRtAttrChild(data, nl.IFLA_GRE_TOS, nl.Uint8Attr(gre.Tos))
|
||||
nl.NewRtAttrChild(data, nl.IFLA_GRE_ENCAP_TYPE, nl.Uint16Attr(gre.EncapType))
|
||||
nl.NewRtAttrChild(data, nl.IFLA_GRE_ENCAP_FLAGS, nl.Uint16Attr(gre.EncapFlags))
|
||||
nl.NewRtAttrChild(data, nl.IFLA_GRE_ENCAP_SPORT, htons(gre.EncapSport))
|
||||
nl.NewRtAttrChild(data, nl.IFLA_GRE_ENCAP_DPORT, htons(gre.EncapDport))
|
||||
}
|
||||
|
||||
func parseGretunData(link Link, data []syscall.NetlinkRouteAttr) {
|
||||
@@ -1835,6 +1845,14 @@ func parseGretunData(link Link, data []syscall.NetlinkRouteAttr) {
|
||||
gre.Tos = uint8(datum.Value[0])
|
||||
case nl.IFLA_GRE_PMTUDISC:
|
||||
gre.PMtuDisc = uint8(datum.Value[0])
|
||||
case nl.IFLA_GRE_ENCAP_TYPE:
|
||||
gre.EncapType = native.Uint16(datum.Value[0:2])
|
||||
case nl.IFLA_GRE_ENCAP_FLAGS:
|
||||
gre.EncapFlags = native.Uint16(datum.Value[0:2])
|
||||
case nl.IFLA_GRE_ENCAP_SPORT:
|
||||
gre.EncapSport = ntohs(datum.Value[0:2])
|
||||
case nl.IFLA_GRE_ENCAP_DPORT:
|
||||
gre.EncapDport = ntohs(datum.Value[0:2])
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1881,6 +1899,12 @@ func parseLinkXdp(data []byte) (*LinkXdp, error) {
|
||||
}
|
||||
|
||||
func addIptunAttrs(iptun *Iptun, linkInfo *nl.RtAttr) {
|
||||
if iptun.FlowBased {
|
||||
// In flow based mode, no other attributes need to be configured
|
||||
nl.NewRtAttrChild(linkInfo, nl.IFLA_IPTUN_COLLECT_METADATA, boolAttr(iptun.FlowBased))
|
||||
return
|
||||
}
|
||||
|
||||
data := nl.NewRtAttrChild(linkInfo, nl.IFLA_INFO_DATA, nil)
|
||||
|
||||
ip := iptun.Local.To4()
|
||||
@@ -1899,6 +1923,10 @@ func addIptunAttrs(iptun *Iptun, linkInfo *nl.RtAttr) {
|
||||
nl.NewRtAttrChild(data, nl.IFLA_IPTUN_PMTUDISC, nl.Uint8Attr(iptun.PMtuDisc))
|
||||
nl.NewRtAttrChild(data, nl.IFLA_IPTUN_TTL, nl.Uint8Attr(iptun.Ttl))
|
||||
nl.NewRtAttrChild(data, nl.IFLA_IPTUN_TOS, nl.Uint8Attr(iptun.Tos))
|
||||
nl.NewRtAttrChild(data, nl.IFLA_IPTUN_ENCAP_TYPE, nl.Uint16Attr(iptun.EncapType))
|
||||
nl.NewRtAttrChild(data, nl.IFLA_IPTUN_ENCAP_FLAGS, nl.Uint16Attr(iptun.EncapFlags))
|
||||
nl.NewRtAttrChild(data, nl.IFLA_IPTUN_ENCAP_SPORT, htons(iptun.EncapSport))
|
||||
nl.NewRtAttrChild(data, nl.IFLA_IPTUN_ENCAP_DPORT, htons(iptun.EncapDport))
|
||||
}
|
||||
|
||||
func parseIptunData(link Link, data []syscall.NetlinkRouteAttr) {
|
||||
@@ -1915,6 +1943,68 @@ func parseIptunData(link Link, data []syscall.NetlinkRouteAttr) {
|
||||
iptun.Tos = uint8(datum.Value[0])
|
||||
case nl.IFLA_IPTUN_PMTUDISC:
|
||||
iptun.PMtuDisc = uint8(datum.Value[0])
|
||||
case nl.IFLA_IPTUN_ENCAP_SPORT:
|
||||
iptun.EncapSport = ntohs(datum.Value[0:2])
|
||||
case nl.IFLA_IPTUN_ENCAP_DPORT:
|
||||
iptun.EncapDport = ntohs(datum.Value[0:2])
|
||||
case nl.IFLA_IPTUN_ENCAP_TYPE:
|
||||
iptun.EncapType = native.Uint16(datum.Value[0:2])
|
||||
case nl.IFLA_IPTUN_ENCAP_FLAGS:
|
||||
iptun.EncapFlags = native.Uint16(datum.Value[0:2])
|
||||
case nl.IFLA_IPTUN_COLLECT_METADATA:
|
||||
iptun.FlowBased = int8(datum.Value[0]) != 0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func addSittunAttrs(sittun *Sittun, linkInfo *nl.RtAttr) {
|
||||
data := nl.NewRtAttrChild(linkInfo, nl.IFLA_INFO_DATA, nil)
|
||||
|
||||
if sittun.Link != 0 {
|
||||
nl.NewRtAttrChild(data, nl.IFLA_IPTUN_LINK, nl.Uint32Attr(sittun.Link))
|
||||
}
|
||||
|
||||
ip := sittun.Local.To4()
|
||||
if ip != nil {
|
||||
nl.NewRtAttrChild(data, nl.IFLA_IPTUN_LOCAL, []byte(ip))
|
||||
}
|
||||
|
||||
ip = sittun.Remote.To4()
|
||||
if ip != nil {
|
||||
nl.NewRtAttrChild(data, nl.IFLA_IPTUN_REMOTE, []byte(ip))
|
||||
}
|
||||
|
||||
nl.NewRtAttrChild(data, nl.IFLA_IPTUN_TTL, nl.Uint8Attr(sittun.Ttl))
|
||||
nl.NewRtAttrChild(data, nl.IFLA_IPTUN_TOS, nl.Uint8Attr(sittun.Tos))
|
||||
nl.NewRtAttrChild(data, nl.IFLA_IPTUN_PMTUDISC, nl.Uint8Attr(sittun.PMtuDisc))
|
||||
nl.NewRtAttrChild(data, nl.IFLA_IPTUN_ENCAP_TYPE, nl.Uint16Attr(sittun.EncapType))
|
||||
nl.NewRtAttrChild(data, nl.IFLA_IPTUN_ENCAP_FLAGS, nl.Uint16Attr(sittun.EncapFlags))
|
||||
nl.NewRtAttrChild(data, nl.IFLA_IPTUN_ENCAP_SPORT, htons(sittun.EncapSport))
|
||||
nl.NewRtAttrChild(data, nl.IFLA_IPTUN_ENCAP_DPORT, htons(sittun.EncapDport))
|
||||
}
|
||||
|
||||
func parseSittunData(link Link, data []syscall.NetlinkRouteAttr) {
|
||||
sittun := link.(*Sittun)
|
||||
for _, datum := range data {
|
||||
switch datum.Attr.Type {
|
||||
case nl.IFLA_IPTUN_LOCAL:
|
||||
sittun.Local = net.IP(datum.Value[0:4])
|
||||
case nl.IFLA_IPTUN_REMOTE:
|
||||
sittun.Remote = net.IP(datum.Value[0:4])
|
||||
case nl.IFLA_IPTUN_TTL:
|
||||
sittun.Ttl = uint8(datum.Value[0])
|
||||
case nl.IFLA_IPTUN_TOS:
|
||||
sittun.Tos = uint8(datum.Value[0])
|
||||
case nl.IFLA_IPTUN_PMTUDISC:
|
||||
sittun.PMtuDisc = uint8(datum.Value[0])
|
||||
case nl.IFLA_IPTUN_ENCAP_TYPE:
|
||||
sittun.EncapType = native.Uint16(datum.Value[0:2])
|
||||
case nl.IFLA_IPTUN_ENCAP_FLAGS:
|
||||
sittun.EncapFlags = native.Uint16(datum.Value[0:2])
|
||||
case nl.IFLA_IPTUN_ENCAP_SPORT:
|
||||
sittun.EncapSport = ntohs(datum.Value[0:2])
|
||||
case nl.IFLA_IPTUN_ENCAP_DPORT:
|
||||
sittun.EncapDport = ntohs(datum.Value[0:2])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
18
link_test.go
18
link_test.go
@@ -176,6 +176,13 @@ func testLinkAddDel(t *testing.T, link Link) {
|
||||
}
|
||||
}
|
||||
|
||||
if _, ok := link.(*Sittun); ok {
|
||||
_, ok := result.(*Sittun)
|
||||
if !ok {
|
||||
t.Fatal("Result of create is not a sittun")
|
||||
}
|
||||
}
|
||||
|
||||
if _, ok := link.(*Gretap); ok {
|
||||
_, ok := result.(*Gretap)
|
||||
if !ok {
|
||||
@@ -1221,6 +1228,17 @@ func TestLinkAddDelIptun(t *testing.T) {
|
||||
Remote: net.IPv4(127, 0, 0, 1)})
|
||||
}
|
||||
|
||||
func TestLinkAddDelSittun(t *testing.T) {
|
||||
tearDown := setUpNetlinkTest(t)
|
||||
defer tearDown()
|
||||
|
||||
testLinkAddDel(t, &Sittun{
|
||||
LinkAttrs: LinkAttrs{Name: "sittunfoo"},
|
||||
PMtuDisc: 1,
|
||||
Local: net.IPv4(127, 0, 0, 1),
|
||||
Remote: net.IPv4(127, 0, 0, 1)})
|
||||
}
|
||||
|
||||
func TestLinkAddDelVti(t *testing.T) {
|
||||
tearDown := setUpNetlinkTest(t)
|
||||
defer tearDown()
|
||||
|
@@ -476,7 +476,12 @@ const (
|
||||
IFLA_IPTUN_6RD_RELAY_PREFIX
|
||||
IFLA_IPTUN_6RD_PREFIXLEN
|
||||
IFLA_IPTUN_6RD_RELAY_PREFIXLEN
|
||||
IFLA_IPTUN_MAX = IFLA_IPTUN_6RD_RELAY_PREFIXLEN
|
||||
IFLA_IPTUN_ENCAP_TYPE
|
||||
IFLA_IPTUN_ENCAP_FLAGS
|
||||
IFLA_IPTUN_ENCAP_SPORT
|
||||
IFLA_IPTUN_ENCAP_DPORT
|
||||
IFLA_IPTUN_COLLECT_METADATA
|
||||
IFLA_IPTUN_MAX = IFLA_IPTUN_COLLECT_METADATA
|
||||
)
|
||||
|
||||
const (
|
||||
|
Reference in New Issue
Block a user