tun: support IPv6
This commit is contained in:
parent
8e914c87e0
commit
267885f1b4
1
go.mod
1
go.mod
@ -33,7 +33,6 @@ require (
|
|||||||
github.com/ryanuber/go-glob v0.0.0-20170128012129-256dc444b735
|
github.com/ryanuber/go-glob v0.0.0-20170128012129-256dc444b735
|
||||||
github.com/shadowsocks/go-shadowsocks2 v0.0.11
|
github.com/shadowsocks/go-shadowsocks2 v0.0.11
|
||||||
github.com/shadowsocks/shadowsocks-go v0.0.0-20170121203516-97a5c71f80ba
|
github.com/shadowsocks/shadowsocks-go v0.0.0-20170121203516-97a5c71f80ba
|
||||||
github.com/songgao/packets v0.0.0-20160404182456-549a10cd4091
|
|
||||||
github.com/songgao/water v0.0.0-20190725173103-fd331bda3f4b
|
github.com/songgao/water v0.0.0-20190725173103-fd331bda3f4b
|
||||||
github.com/templexxx/cpufeat v0.0.0-20180724012125-cef66df7f161 // indirect
|
github.com/templexxx/cpufeat v0.0.0-20180724012125-cef66df7f161 // indirect
|
||||||
github.com/templexxx/xor v0.0.0-20181023030647-4e92f724b73b // indirect
|
github.com/templexxx/xor v0.0.0-20181023030647-4e92f724b73b // indirect
|
||||||
|
2
go.sum
2
go.sum
@ -69,8 +69,6 @@ github.com/shadowsocks/go-shadowsocks2 v0.0.11 h1:dXloqEhYnZV40jblWTK8kWeC0Eb+dg
|
|||||||
github.com/shadowsocks/go-shadowsocks2 v0.0.11/go.mod h1:R+KWaoIwRRhnpw6XV+dZil0XHi64Hc1D7hXUyXTjUzQ=
|
github.com/shadowsocks/go-shadowsocks2 v0.0.11/go.mod h1:R+KWaoIwRRhnpw6XV+dZil0XHi64Hc1D7hXUyXTjUzQ=
|
||||||
github.com/shadowsocks/shadowsocks-go v0.0.0-20170121203516-97a5c71f80ba h1:tJgNXb3S+RkB4kNPi6N5OmEWe3m+Y3Qs6LUMiNDAONM=
|
github.com/shadowsocks/shadowsocks-go v0.0.0-20170121203516-97a5c71f80ba h1:tJgNXb3S+RkB4kNPi6N5OmEWe3m+Y3Qs6LUMiNDAONM=
|
||||||
github.com/shadowsocks/shadowsocks-go v0.0.0-20170121203516-97a5c71f80ba/go.mod h1:mttDPaeLm87u74HMrP+n2tugXvIKWcwff/cqSX0lehY=
|
github.com/shadowsocks/shadowsocks-go v0.0.0-20170121203516-97a5c71f80ba/go.mod h1:mttDPaeLm87u74HMrP+n2tugXvIKWcwff/cqSX0lehY=
|
||||||
github.com/songgao/packets v0.0.0-20160404182456-549a10cd4091 h1:1zN6ImoqhSJhN8hGXFaJlSC8msLmIbX8bFqOfWLKw0w=
|
|
||||||
github.com/songgao/packets v0.0.0-20160404182456-549a10cd4091/go.mod h1:N20Z5Y8oye9a7HmytmZ+tr8Q2vlP0tAHP13kTHzwvQY=
|
|
||||||
github.com/songgao/water v0.0.0-20190725173103-fd331bda3f4b h1:+y4hCMc/WKsDbAPsOQZgBSaSZ26uh2afyaWeVg/3s/c=
|
github.com/songgao/water v0.0.0-20190725173103-fd331bda3f4b h1:+y4hCMc/WKsDbAPsOQZgBSaSZ26uh2afyaWeVg/3s/c=
|
||||||
github.com/songgao/water v0.0.0-20190725173103-fd331bda3f4b/go.mod h1:P5HUIBuIWKbyjl083/loAegFkfbFNx5i2qEP4CNbm7E=
|
github.com/songgao/water v0.0.0-20190725173103-fd331bda3f4b/go.mod h1:P5HUIBuIWKbyjl083/loAegFkfbFNx5i2qEP4CNbm7E=
|
||||||
github.com/templexxx/cpufeat v0.0.0-20180724012125-cef66df7f161 h1:89CEmDvlq/F7SJEOqkIdNDGJXrQIhuIx9D2DBXjavSU=
|
github.com/templexxx/cpufeat v0.0.0-20180724012125-cef66df7f161 h1:89CEmDvlq/F7SJEOqkIdNDGJXrQIhuIx9D2DBXjavSU=
|
||||||
|
243
tuntap.go
243
tuntap.go
@ -3,6 +3,7 @@ package gost
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
@ -11,13 +12,31 @@ import (
|
|||||||
|
|
||||||
"github.com/go-log/log"
|
"github.com/go-log/log"
|
||||||
"github.com/shadowsocks/go-shadowsocks2/core"
|
"github.com/shadowsocks/go-shadowsocks2/core"
|
||||||
"github.com/songgao/packets/ethernet"
|
|
||||||
"github.com/songgao/water"
|
"github.com/songgao/water"
|
||||||
"github.com/songgao/water/waterutil"
|
"github.com/songgao/water/waterutil"
|
||||||
"golang.org/x/net/ipv4"
|
"golang.org/x/net/ipv4"
|
||||||
"golang.org/x/net/ipv6"
|
"golang.org/x/net/ipv6"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var mIPProts = map[waterutil.IPProtocol]string{
|
||||||
|
waterutil.HOPOPT: "HOPOPT",
|
||||||
|
waterutil.ICMP: "ICMP",
|
||||||
|
waterutil.IGMP: "IGMP",
|
||||||
|
waterutil.GGP: "GGP",
|
||||||
|
waterutil.TCP: "TCP",
|
||||||
|
waterutil.UDP: "UDP",
|
||||||
|
waterutil.IPv6_Route: "IPv6-Route",
|
||||||
|
waterutil.IPv6_Frag: "IPv6-Frag",
|
||||||
|
waterutil.IPv6_ICMP: "IPv6-ICMP",
|
||||||
|
}
|
||||||
|
|
||||||
|
func ipProtocol(p waterutil.IPProtocol) string {
|
||||||
|
if v, ok := mIPProts[p]; ok {
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("unknown(%d)", p)
|
||||||
|
}
|
||||||
|
|
||||||
type TunConfig struct {
|
type TunConfig struct {
|
||||||
Name string
|
Name string
|
||||||
Addr string
|
Addr string
|
||||||
@ -25,10 +44,17 @@ type TunConfig struct {
|
|||||||
Routes []string
|
Routes []string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type tunRouteKey [16]byte
|
||||||
|
|
||||||
|
func ipToTunRouteKey(ip net.IP) (key tunRouteKey) {
|
||||||
|
copy(key[:], ip.To16())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
type tunHandler struct {
|
type tunHandler struct {
|
||||||
raddr string
|
raddr string
|
||||||
options *HandlerOptions
|
options *HandlerOptions
|
||||||
ipNet *net.IPNet
|
ifce *net.Interface
|
||||||
routes sync.Map
|
routes sync.Map
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -71,7 +97,9 @@ func (h *tunHandler) Handle(conn net.Conn) {
|
|||||||
}
|
}
|
||||||
defer tc.Close()
|
defer tc.Close()
|
||||||
|
|
||||||
log.Logf("[tun] %s - %s: tun creation successful", tc.LocalAddr(), conn.LocalAddr())
|
addrs, _ := h.ifce.Addrs()
|
||||||
|
log.Logf("[tun] %s - %s: name: %s, mtu: %d, addrs: %s",
|
||||||
|
tc.LocalAddr(), conn.LocalAddr(), h.ifce.Name, h.ifce.MTU, addrs)
|
||||||
|
|
||||||
var raddr net.Addr
|
var raddr net.Addr
|
||||||
if h.raddr != "" {
|
if h.raddr != "" {
|
||||||
@ -96,7 +124,7 @@ func (h *tunHandler) Handle(conn net.Conn) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *tunHandler) createTun() (conn net.Conn, err error) {
|
func (h *tunHandler) createTun() (conn net.Conn, err error) {
|
||||||
conn, h.ipNet, err = createTun(h.options.TunConfig)
|
conn, h.ifce, err = createTun(h.options.TunConfig)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -114,41 +142,52 @@ func (h *tunHandler) transportTun(tun net.Conn, conn net.PacketConn, raddr net.A
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
header, err := ipv4.ParseHeader(b[:n])
|
var src, dst net.IP
|
||||||
if err != nil {
|
if waterutil.IsIPv4(b[:n]) {
|
||||||
log.Logf("[tun] %s: %v", tun.LocalAddr(), err)
|
header, err := ipv4.ParseHeader(b[:n])
|
||||||
|
if err != nil {
|
||||||
|
log.Logf("[tun] %s: %v", tun.LocalAddr(), err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if Debug {
|
||||||
|
log.Logf("[tun] %s -> %s %-4s %d/%-4d %-4x %d",
|
||||||
|
header.Src, header.Dst, ipProtocol(waterutil.IPv4Protocol(b[:n])),
|
||||||
|
header.Len, header.TotalLen, header.ID, header.Flags)
|
||||||
|
}
|
||||||
|
src, dst = header.Src, header.Dst
|
||||||
|
} else if waterutil.IsIPv6(b[:n]) {
|
||||||
|
header, err := ipv6.ParseHeader(b[:n])
|
||||||
|
if err != nil {
|
||||||
|
log.Logf("[tun] %s: %v", tun.LocalAddr(), err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if Debug {
|
||||||
|
log.Logf("[tun] %s -> %s %s %d %d",
|
||||||
|
header.Src, header.Dst,
|
||||||
|
ipProtocol(waterutil.IPProtocol(header.NextHeader)),
|
||||||
|
header.PayloadLen, header.TrafficClass)
|
||||||
|
}
|
||||||
|
src, dst = header.Src, header.Dst
|
||||||
|
} else {
|
||||||
|
log.Logf("[tun] unknown packet")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// client side, deliver packet directly.
|
||||||
|
if raddr != nil {
|
||||||
|
_, err := conn.WriteTo(b[:n], raddr)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if header.Version != ipv4.Version {
|
var addr net.Addr
|
||||||
if Debug && header.Version == ipv6.Version {
|
if v, ok := h.routes.Load(ipToTunRouteKey(dst)); ok {
|
||||||
if hdr, _ := ipv6.ParseHeader(b[:n]); hdr != nil {
|
|
||||||
log.Logf("[tun] %s: %s -> %s %d %d",
|
|
||||||
tun.LocalAddr(), hdr.Src, hdr.Dst, hdr.PayloadLen, hdr.TrafficClass)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
log.Logf("[tun] %s: v%d ignored, only support ipv4",
|
|
||||||
tun.LocalAddr(), header.Version)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
addr := raddr
|
|
||||||
if v, ok := h.routes.Load(header.Dst.String()); ok {
|
|
||||||
addr = v.(net.Addr)
|
addr = v.(net.Addr)
|
||||||
}
|
}
|
||||||
if addr == nil {
|
if addr == nil {
|
||||||
log.Logf("[tun] %s: no route for %s -> %s %d/%d %x %d %d",
|
log.Logf("[tun] no route for %s -> %s", src, dst)
|
||||||
tun.LocalAddr(), header.Src, header.Dst,
|
|
||||||
header.Len, header.TotalLen, header.ID, header.Flags, header.Protocol)
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if Debug {
|
|
||||||
log.Logf("[tun] %s >>> %s: %s -> %s %d/%d %x %d %d",
|
|
||||||
tun.LocalAddr(), addr, header.Src, header.Dst,
|
|
||||||
header.Len, header.TotalLen, header.ID, header.Flags, header.Protocol)
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := conn.WriteTo(b[:n], addr); err != nil {
|
if _, err := conn.WriteTo(b[:n], addr); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -173,40 +212,60 @@ func (h *tunHandler) transportTun(tun net.Conn, conn net.PacketConn, raddr net.A
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
header, err := ipv4.ParseHeader(b[:n])
|
var src, dst net.IP
|
||||||
if err != nil {
|
if waterutil.IsIPv4(b[:n]) {
|
||||||
log.Logf("[tun] %s <- %s: %v", tun.LocalAddr(), addr, err)
|
header, err := ipv4.ParseHeader(b[:n])
|
||||||
return err
|
if err != nil {
|
||||||
}
|
log.Logf("[tun] %s: %v", tun.LocalAddr(), err)
|
||||||
|
return nil
|
||||||
if header.Version != ipv4.Version {
|
|
||||||
if Debug && header.Version == ipv6.Version {
|
|
||||||
if hdr, _ := ipv6.ParseHeader(b[:n]); hdr != nil {
|
|
||||||
log.Logf("[tun] %s <<< %s: %s -> %s %d %d",
|
|
||||||
tun.LocalAddr(), addr, hdr.Src, hdr.Dst, hdr.PayloadLen, hdr.TrafficClass)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
log.Logf("[tun] %s <- %s: v%d ignored, only support ipv4",
|
if Debug {
|
||||||
tun.LocalAddr(), addr, header.Version)
|
log.Logf("[tun] %s -> %s %-4s %d/%-4d %-4x %d",
|
||||||
|
header.Src, header.Dst, ipProtocol(waterutil.IPv4Protocol(b[:n])),
|
||||||
|
header.Len, header.TotalLen, header.ID, header.Flags)
|
||||||
|
}
|
||||||
|
src, dst = header.Src, header.Dst
|
||||||
|
} else if waterutil.IsIPv6(b[:n]) {
|
||||||
|
header, err := ipv6.ParseHeader(b[:n])
|
||||||
|
if err != nil {
|
||||||
|
log.Logf("[tun] %s: %v", tun.LocalAddr(), err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if Debug {
|
||||||
|
log.Logf("[tun] %s -> %s %s %d %d",
|
||||||
|
header.Src, header.Dst,
|
||||||
|
ipProtocol(waterutil.IPProtocol(header.NextHeader)),
|
||||||
|
header.PayloadLen, header.TrafficClass)
|
||||||
|
}
|
||||||
|
src, dst = header.Src, header.Dst
|
||||||
|
} else {
|
||||||
|
log.Logf("[tun] unknown packet")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if Debug {
|
// client side, deliver packet to tun device.
|
||||||
log.Logf("[tun] %s <<< %s: %s -> %s %d/%d %x %d %d",
|
if raddr != nil {
|
||||||
tun.LocalAddr(), addr, header.Src, header.Dst,
|
_, err := tun.Write(b[:n])
|
||||||
header.Len, header.TotalLen, header.ID, header.Flags, header.Protocol)
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if h.ipNet != nil && h.ipNet.IP.Equal(header.Src.Mask(h.ipNet.Mask)) {
|
rkey := ipToTunRouteKey(src)
|
||||||
if actual, loaded := h.routes.LoadOrStore(header.Src.String(), addr); loaded {
|
if actual, loaded := h.routes.LoadOrStore(rkey, addr); loaded {
|
||||||
if actual.(net.Addr).String() != addr.String() {
|
if actual.(net.Addr).String() != addr.String() {
|
||||||
log.Logf("[tun] %s <- %s: update route: %s -> %s (old %s)",
|
log.Logf("[tun] update route: %s -> %s (old %s)",
|
||||||
tun.LocalAddr(), addr, header.Src, addr, actual.(net.Addr))
|
src, addr, actual.(net.Addr))
|
||||||
h.routes.Store(header.Src.String(), addr)
|
h.routes.Store(rkey, addr)
|
||||||
}
|
|
||||||
} else {
|
|
||||||
log.Logf("[tun] %s: new route: %s -> %s", tun.LocalAddr(), header.Src, addr)
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
log.Logf("[tun] new route: %s -> %s", src, addr)
|
||||||
|
}
|
||||||
|
|
||||||
|
if v, ok := h.routes.Load(ipToTunRouteKey(dst)); ok {
|
||||||
|
if Debug {
|
||||||
|
log.Logf("[tun] find route: %s -> %s", dst, v)
|
||||||
|
}
|
||||||
|
_, err := conn.WriteTo(b[:n], v.(net.Addr))
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := tun.Write(b[:n]); err != nil {
|
if _, err := tun.Write(b[:n]); err != nil {
|
||||||
@ -285,14 +344,14 @@ func (l *tunListener) Close() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var mEtherTypes = map[ethernet.Ethertype]string{
|
var mEtherTypes = map[waterutil.Ethertype]string{
|
||||||
ethernet.IPv4: "ip",
|
waterutil.IPv4: "ip",
|
||||||
ethernet.ARP: "arp",
|
waterutil.ARP: "arp",
|
||||||
ethernet.RARP: "rarp",
|
waterutil.RARP: "rarp",
|
||||||
ethernet.IPv6: "ip6",
|
waterutil.IPv6: "ip6",
|
||||||
}
|
}
|
||||||
|
|
||||||
func etherType(et ethernet.Ethertype) string {
|
func etherType(et waterutil.Ethertype) string {
|
||||||
if s, ok := mEtherTypes[et]; ok {
|
if s, ok := mEtherTypes[et]; ok {
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
@ -360,7 +419,7 @@ func (h *tapHandler) Handle(conn net.Conn) {
|
|||||||
defer tc.Close()
|
defer tc.Close()
|
||||||
|
|
||||||
addrs, _ := h.ifce.Addrs()
|
addrs, _ := h.ifce.Addrs()
|
||||||
log.Logf("[tap] %s - %s: name: %s, mac: %s, mtu: %d, addr: %s",
|
log.Logf("[tap] %s - %s: name: %s, mac: %s, mtu: %d, addrs: %s",
|
||||||
tc.LocalAddr(), conn.LocalAddr(),
|
tc.LocalAddr(), conn.LocalAddr(),
|
||||||
h.ifce.Name, h.ifce.HardwareAddr, h.ifce.MTU, addrs)
|
h.ifce.Name, h.ifce.HardwareAddr, h.ifce.MTU, addrs)
|
||||||
|
|
||||||
@ -405,22 +464,23 @@ func (h *tapHandler) transportTap(tap net.Conn, conn net.PacketConn, raddr net.A
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
frame := ethernet.Frame(b[:n])
|
src := waterutil.MACSource(b[:n])
|
||||||
|
dst := waterutil.MACDestination(b[:n])
|
||||||
|
eType := etherType(waterutil.MACEthertype(b[:n]))
|
||||||
|
|
||||||
if Debug {
|
if Debug {
|
||||||
log.Logf("[tap] %s -> %s %s %d",
|
log.Logf("[tap] %s -> %s %s %d", src, dst, eType, n)
|
||||||
frame.Source(), frame.Destination(), etherType(frame.Ethertype()), n)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// client side delivers frame directly.
|
// client side, deliver frame directly.
|
||||||
if raddr != nil {
|
if raddr != nil {
|
||||||
_, err := conn.WriteTo(b[:n], raddr)
|
_, err := conn.WriteTo(b[:n], raddr)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// server side broadcast.
|
// server side, broadcast.
|
||||||
if waterutil.IsBroadcast(frame.Destination()) {
|
if waterutil.IsBroadcast(dst) {
|
||||||
h.routes.Range(func(k, v interface{}) bool {
|
go h.routes.Range(func(k, v interface{}) bool {
|
||||||
conn.WriteTo(b[:n], v.(net.Addr))
|
conn.WriteTo(b[:n], v.(net.Addr))
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
@ -428,12 +488,11 @@ func (h *tapHandler) transportTap(tap net.Conn, conn net.PacketConn, raddr net.A
|
|||||||
}
|
}
|
||||||
|
|
||||||
var addr net.Addr
|
var addr net.Addr
|
||||||
if v, ok := h.routes.Load(hwAddrToTapRouteKey(frame.Destination())); ok {
|
if v, ok := h.routes.Load(hwAddrToTapRouteKey(dst)); ok {
|
||||||
addr = v.(net.Addr)
|
addr = v.(net.Addr)
|
||||||
}
|
}
|
||||||
if addr == nil {
|
if addr == nil {
|
||||||
log.Logf("[tap] no route for %s -> %s %s %d",
|
log.Logf("[tap] no route for %s -> %s %s %d", src, dst, eType, n)
|
||||||
frame.Source(), frame.Destination(), etherType(frame.Ethertype()), n)
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -461,18 +520,18 @@ func (h *tapHandler) transportTap(tap net.Conn, conn net.PacketConn, raddr net.A
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
frame := ethernet.Frame(b[:n])
|
src := waterutil.MACSource(b[:n])
|
||||||
|
dst := waterutil.MACDestination(b[:n])
|
||||||
|
eType := etherType(waterutil.MACEthertype(b[:n]))
|
||||||
|
|
||||||
// ignore the frame send by myself
|
// ignore the frame send by myself
|
||||||
if bytes.Equal(frame.Source(), h.ifce.HardwareAddr) {
|
if bytes.Equal(src, h.ifce.HardwareAddr) {
|
||||||
log.Logf("[tap] %s -> %s %s %d ignored",
|
log.Logf("[tap] %s -> %s %s %d ignored", src, dst, eType, n)
|
||||||
frame.Source(), frame.Destination(), etherType(frame.Ethertype()), n)
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if Debug {
|
if Debug {
|
||||||
log.Logf("[tap] %s -> %s %s %d",
|
log.Logf("[tap] %s -> %s %s %d", src, dst, eType, n)
|
||||||
frame.Source(), frame.Destination(), etherType(frame.Ethertype()), n)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// client side, deliver frame to tap device.
|
// client side, deliver frame to tap device.
|
||||||
@ -482,30 +541,30 @@ func (h *tapHandler) transportTap(tap net.Conn, conn net.PacketConn, raddr net.A
|
|||||||
}
|
}
|
||||||
|
|
||||||
// server side, record route.
|
// server side, record route.
|
||||||
rkey := hwAddrToTapRouteKey(frame.Source())
|
rkey := hwAddrToTapRouteKey(src)
|
||||||
if actual, loaded := h.routes.LoadOrStore(rkey, addr); loaded {
|
if actual, loaded := h.routes.LoadOrStore(rkey, addr); loaded {
|
||||||
if actual.(net.Addr).String() != addr.String() {
|
if actual.(net.Addr).String() != addr.String() {
|
||||||
log.Logf("[tap] update route: %s -> %s (old %s)",
|
log.Logf("[tap] update route: %s -> %s (old %s)",
|
||||||
frame.Source(), addr, actual.(net.Addr))
|
src, addr, actual.(net.Addr))
|
||||||
h.routes.Store(rkey, addr)
|
h.routes.Store(rkey, addr)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
log.Logf("[tap] new route: %s -> %s",
|
log.Logf("[tap] new route: %s -> %s", src, addr)
|
||||||
frame.Source(), addr)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if waterutil.IsBroadcast(frame.Destination()) {
|
if waterutil.IsBroadcast(dst) {
|
||||||
h.routes.Range(func(k, v interface{}) bool {
|
go h.routes.Range(func(k, v interface{}) bool {
|
||||||
if k.(tapRouteKey) != hwAddrToTapRouteKey(frame.Source()) {
|
if k.(tapRouteKey) != rkey {
|
||||||
conn.WriteTo(b[:n], v.(net.Addr))
|
conn.WriteTo(b[:n], v.(net.Addr))
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
if v, ok := h.routes.Load(hwAddrToTapRouteKey(frame.Destination())); ok {
|
if v, ok := h.routes.Load(hwAddrToTapRouteKey(dst)); ok {
|
||||||
log.Logf("[tap] find route: %s -> %s",
|
if Debug {
|
||||||
frame.Destination(), v)
|
log.Logf("[tap] find route: %s -> %s", dst, v)
|
||||||
|
}
|
||||||
_, err := conn.WriteTo(b[:n], v.(net.Addr))
|
_, err := conn.WriteTo(b[:n], v.(net.Addr))
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -622,3 +681,7 @@ func (c *tunTapConn) SetReadDeadline(t time.Time) error {
|
|||||||
func (c *tunTapConn) SetWriteDeadline(t time.Time) error {
|
func (c *tunTapConn) SetWriteDeadline(t time.Time) error {
|
||||||
return &net.OpError{Op: "set", Net: "tuntap", Source: nil, Addr: nil, Err: errors.New("deadline not supported")}
|
return &net.OpError{Op: "set", Net: "tuntap", Source: nil, Addr: nil, Err: errors.New("deadline not supported")}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func IsIPv6Multicast(addr net.HardwareAddr) bool {
|
||||||
|
return addr[0] == 0x33 && addr[1] == 0x33
|
||||||
|
}
|
||||||
|
@ -11,8 +11,8 @@ import (
|
|||||||
"github.com/songgao/water"
|
"github.com/songgao/water"
|
||||||
)
|
)
|
||||||
|
|
||||||
func createTun(cfg TunConfig) (conn net.Conn, ipNet *net.IPNet, err error) {
|
func createTun(cfg TunConfig) (conn net.Conn, itf *net.Interface, err error) {
|
||||||
ip, ipNet, err := net.ParseCIDR(cfg.Addr)
|
ip, _, err := net.ParseCIDR(cfg.Addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -41,6 +41,11 @@ func createTun(cfg TunConfig) (conn net.Conn, ipNet *net.IPNet, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
itf, err = net.InterfaceByName(ifce.Name())
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
conn = &tunTapConn{
|
conn = &tunTapConn{
|
||||||
ifce: ifce,
|
ifce: ifce,
|
||||||
addr: &net.IPAddr{IP: ip},
|
addr: &net.IPAddr{IP: ip},
|
||||||
|
@ -10,7 +10,7 @@ import (
|
|||||||
"github.com/songgao/water"
|
"github.com/songgao/water"
|
||||||
)
|
)
|
||||||
|
|
||||||
func createTun(cfg TunConfig) (conn net.Conn, ipNet *net.IPNet, err error) {
|
func createTun(cfg TunConfig) (conn net.Conn, itf *net.Interface, err error) {
|
||||||
ip, ipNet, err := net.ParseCIDR(cfg.Addr)
|
ip, ipNet, err := net.ParseCIDR(cfg.Addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
@ -61,6 +61,11 @@ func createTun(cfg TunConfig) (conn net.Conn, ipNet *net.IPNet, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
itf, err = net.InterfaceByName(ifce.Name())
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
conn = &tunTapConn{
|
conn = &tunTapConn{
|
||||||
ifce: ifce,
|
ifce: ifce,
|
||||||
addr: &net.IPAddr{IP: ip},
|
addr: &net.IPAddr{IP: ip},
|
||||||
|
@ -12,8 +12,8 @@ import (
|
|||||||
"github.com/songgao/water"
|
"github.com/songgao/water"
|
||||||
)
|
)
|
||||||
|
|
||||||
func createTun(cfg TunConfig) (conn net.Conn, ipNet *net.IPNet, err error) {
|
func createTun(cfg TunConfig) (conn net.Conn, itf *net.Interface, err error) {
|
||||||
ip, ipNet, err := net.ParseCIDR(cfg.Addr)
|
ip, _, err := net.ParseCIDR(cfg.Addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -42,6 +42,11 @@ func createTun(cfg TunConfig) (conn net.Conn, ipNet *net.IPNet, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
itf, err = net.InterfaceByName(ifce.Name())
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
conn = &tunTapConn{
|
conn = &tunTapConn{
|
||||||
ifce: ifce,
|
ifce: ifce,
|
||||||
addr: &net.IPAddr{IP: ip},
|
addr: &net.IPAddr{IP: ip},
|
||||||
|
@ -10,7 +10,7 @@ import (
|
|||||||
"github.com/songgao/water"
|
"github.com/songgao/water"
|
||||||
)
|
)
|
||||||
|
|
||||||
func createTun(cfg TunConfig) (conn net.Conn, ipNet *net.IPNet, err error) {
|
func createTun(cfg TunConfig) (conn net.Conn, itf *net.Interface, err error) {
|
||||||
ip, ipNet, err := net.ParseCIDR(cfg.Addr)
|
ip, ipNet, err := net.ParseCIDR(cfg.Addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
@ -42,6 +42,11 @@ func createTun(cfg TunConfig) (conn net.Conn, ipNet *net.IPNet, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
itf, err = net.InterfaceByName(ifce.Name())
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
conn = &tunTapConn{
|
conn = &tunTapConn{
|
||||||
ifce: ifce,
|
ifce: ifce,
|
||||||
addr: &net.IPAddr{IP: ip},
|
addr: &net.IPAddr{IP: ip},
|
||||||
|
Loading…
Reference in New Issue
Block a user