diff options
author | Mikael Magnusson <mikma@users.sourceforge.net> | 2021-01-24 01:12:33 +0100 |
---|---|---|
committer | Mikael Magnusson <mikma@users.sourceforge.net> | 2021-01-24 01:12:33 +0100 |
commit | ea4772ea81aa177047d0afed3a0de9f3bea848b1 (patch) | |
tree | 5c8d17dc94b5240820bef8457f7b866b47b23427 /pkg | |
parent | 920d615f275e27114e5c0bb4ed76f9b578c4095e (diff) |
Support IPv6 gre tunnelsgre
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/tcpip/link/tunnel/gre.go | 20 | ||||
-rw-r--r-- | pkg/tcpip/sample/tun_tcp_connect/main.go | 43 |
2 files changed, 52 insertions, 11 deletions
diff --git a/pkg/tcpip/link/tunnel/gre.go b/pkg/tcpip/link/tunnel/gre.go index c62cfa283..64a54f11e 100644 --- a/pkg/tcpip/link/tunnel/gre.go +++ b/pkg/tcpip/link/tunnel/gre.go @@ -23,6 +23,7 @@ import ( "gvisor.dev/gvisor/pkg/tcpip/header" "gvisor.dev/gvisor/pkg/tcpip/link/channel" "gvisor.dev/gvisor/pkg/tcpip/network/ipv4" + "gvisor.dev/gvisor/pkg/tcpip/network/ipv6" "gvisor.dev/gvisor/pkg/tcpip/stack" "gvisor.dev/gvisor/pkg/tcpip/transport/gre" "gvisor.dev/gvisor/pkg/tcpip/transport/raw" @@ -126,10 +127,23 @@ func (info *GreHandlerInfo) greRead(ep *channel.Endpoint) { } } -func (e *Endpoint) Start(s *stack.Stack, laddr, raddr *tcpip.Address) { +func networkProtocolNumber(addr *tcpip.Address) tcpip.NetworkProtocolNumber { + if addr.To4() != "" { + return ipv4.ProtocolNumber + } else { + return ipv6.ProtocolNumber + } +} + +func (e *Endpoint) Start(s *stack.Stack, laddr, raddr *tcpip.Address) *tcpip.Error { + proto := networkProtocolNumber(laddr) + if proto != networkProtocolNumber(raddr) { + return tcpip.ErrBadAddress + } + // Create TCP endpoint. var rawWq waiter.Queue - rawEp, tcperr := raw.NewEndpoint(s, ipv4.ProtocolNumber, header.GREProtocolNumber, &rawWq) + rawEp, tcperr := raw.NewEndpoint(s, proto, header.GREProtocolNumber, &rawWq) if tcperr != nil { log.Fatal(tcperr) } @@ -164,4 +178,6 @@ func (e *Endpoint) Start(s *stack.Stack, laddr, raddr *tcpip.Address) { if tcperr != nil { log.Fatal(tcperr) } + + return nil } diff --git a/pkg/tcpip/sample/tun_tcp_connect/main.go b/pkg/tcpip/sample/tun_tcp_connect/main.go index a1a818a6c..6fbd230aa 100644 --- a/pkg/tcpip/sample/tun_tcp_connect/main.go +++ b/pkg/tcpip/sample/tun_tcp_connect/main.go @@ -60,6 +60,7 @@ import ( "gvisor.dev/gvisor/pkg/tcpip/link/tun" "gvisor.dev/gvisor/pkg/tcpip/link/tunnel" "gvisor.dev/gvisor/pkg/tcpip/network/ipv4" + "gvisor.dev/gvisor/pkg/tcpip/network/ipv6" "gvisor.dev/gvisor/pkg/tcpip/stack" "gvisor.dev/gvisor/pkg/tcpip/transport/gre" "gvisor.dev/gvisor/pkg/tcpip/transport/tcp" @@ -95,6 +96,26 @@ func writer(ch chan struct{}, ep tcpip.Endpoint) { } } +func parseIP(s string) tcpip.Address { + addr := tcpip.Address(net.ParseIP(s)) + ip4addr := addr.To4(); if ip4addr != "" { + return ip4addr + } else { + return addr + } +} + +func addAddress(s *stack.Stack, id tcpip.NICID, addr tcpip.Address) *tcpip.Error { + var proto tcpip.NetworkProtocolNumber + if addr.To4() == "" { + proto = ipv6.ProtocolNumber + } else { + proto = ipv4.ProtocolNumber + } + + return s.AddAddress(id, proto, addr) +} + func main() { if len(os.Args) != 8 { log.Fatal("Usage: ", os.Args[0], " <tun-device> <local-ipv4-address> <local-port> <remote-ipv4-address> <remote-port> <local-gre-address> <remote-gre-address>") @@ -110,12 +131,12 @@ func main() { rand.Seed(time.Now().UnixNano()) - addr := tcpip.Address(net.ParseIP(addrName).To4()) - greAddr := tcpip.Address(net.ParseIP(greAddrName).To4()) - greRemoteAddr := tcpip.Address(net.ParseIP(greRemoteAddrName).To4()) + addr := parseIP(addrName) + greAddr := parseIP(greAddrName) + greRemoteAddr := parseIP(greRemoteAddrName) remote := tcpip.FullAddress{ NIC: 0, - Addr: tcpip.Address(net.ParseIP(remoteAddrName).To4()), + Addr: parseIP(remoteAddrName), } log.Printf("local:%v remote:%v", addr, remote) @@ -136,7 +157,7 @@ func main() { // Create the stack with ipv4 and tcp protocols, then add a tun-based // NIC and ipv4 address. s := stack.New(stack.Options{ - NetworkProtocols: []stack.NetworkProtocolFactory{ipv4.NewProtocol}, + NetworkProtocols: []stack.NetworkProtocolFactory{ipv4.NewProtocol, ipv6.NewProtocol}, TransportProtocols: []stack.TransportProtocolFactory{tcp.NewProtocol, gre.NewProtocol}, }) @@ -158,7 +179,7 @@ func main() { log.Fatal(err) } - if err := s.AddAddress(1, ipv4.ProtocolNumber, greAddr); err != nil { + if err := addAddress(s, 1, greAddr); err != nil { log.Fatal(err) } @@ -166,14 +187,14 @@ func main() { if err := s.CreateNIC(2, sniffer.New(greEP)); err != nil { log.Fatal(err) } - if err := s.AddAddress(2, ipv4.ProtocolNumber, addr); err != nil { + if err := addAddress(s, 2, addr); err != nil { log.Fatal(err) } // Add default route. subnet, err := tcpip.NewSubnet( - tcpip.Address(net.ParseIP("10.0.0.0").To4()), - tcpip.AddressMask(net.ParseIP("255.255.255.0").To4())) + tcpip.Address(parseIP("10.0.0.0")), + tcpip.AddressMask(parseIP("255.255.255.0"))) if err != nil { panic(err) } @@ -186,6 +207,10 @@ func main() { Destination: header.IPv4EmptySubnet, NIC: 2, }, + { + Destination: header.IPv6EmptySubnet, + NIC: 1, + }, }) log.Printf("Nics enabled 1:%v 2:%v 3:%v", s.CheckNIC(1), s.CheckNIC(2), s.CheckNIC(3)) |