Skip to content

Commit

Permalink
mtu settings and fix mss
Browse files Browse the repository at this point in the history
  • Loading branch information
bigeagle committed Nov 2, 2013
1 parent 693f7aa commit a5940b2
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 10 deletions.
17 changes: 13 additions & 4 deletions hop/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ func NewClient(cfg HopClientConfig) error {
return err
}

if cfg.MTU != 0 {
MTU = cfg.MTU
}

hopClient := new(HopClient)
rand.Read(hopClient.sid[:])
Expand Down Expand Up @@ -168,7 +171,7 @@ func (clt *HopClient) handleInterface() {
}
}()

frame := make([]byte, MTU)
frame := make([]byte, IFACE_BUFSIZE)
for {
n, err := clt.iface.Read(frame)
if err != nil {
Expand Down Expand Up @@ -221,7 +224,7 @@ func (clt *HopClient) handleUDP(server string) {
}()

// add route through net gateway
if clt.cfg.Redirect_gateway {
if clt.cfg.Redirect_gateway && (!clt.cfg.Local) {
if atomic.CompareAndSwapInt32(&clt.srvRoute, 0, 1) {
if udpAddr, ok := udpConn.RemoteAddr().(*net.UDPAddr); ok {
srvIP := udpAddr.IP.To4()
Expand Down Expand Up @@ -327,7 +330,9 @@ func (clt *HopClient) handleHandshakeAck(u *net.UDPConn, hp *HopPacket) {
ip := net.IP(_ip)
subnet := &net.IPNet{_net, _mask}
setTunIP(clt.iface, ip, subnet)

if clt.cfg.FixMSS {
fixMSS(clt.iface.Name())
}
res := atomic.CompareAndSwapInt32(&clt.state, HOP_STAT_HANDSHAKE, HOP_STAT_WORKING)
if !res {
logger.Error("Client state not expected: %d", clt.state)
Expand Down Expand Up @@ -368,8 +373,12 @@ func (clt *HopClient) cleanUp() {
delRoute("0.0.0.0/1")
delRoute("128.0.0.0/1")
}
if clt.cfg.FixMSS {
clearMSS(clt.iface.Name())
}


timeout := time.After(5 * time.Second)
timeout := time.After(3 * time.Second)
if clt.state != HOP_STAT_INIT {
clt.finishSession()
}
Expand Down
5 changes: 3 additions & 2 deletions hop/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import (

var logger = logging.GetLogger()

var MTU = 1400

const (
MTU = 1400
IFACE_BUFSIZE = 1500
IFACE_BUFSIZE = 2000
)
4 changes: 3 additions & 1 deletion hop/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ type HopServerConfig struct {
HopStart int
HopEnd int
Addr string
MTU int
Key string
MorphMethod string
}
Expand All @@ -215,7 +216,8 @@ type HopClientConfig struct {
HopStart int
HopEnd int
Key string
MTU string
MTU int
FixMSS bool
Local bool
MorphMethod string
Redirect_gateway bool
Expand Down
31 changes: 30 additions & 1 deletion hop/iface.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func newTun(name string) (iface *water.Interface, err error) {
}
logger.Info("interface %v created", iface.Name())

sargs := fmt.Sprintf("link set dev %s up mtu %d", iface.Name(), MTU)
sargs := fmt.Sprintf("link set dev %s up mtu %d qlen 100", iface.Name(), MTU)
args := strings.Split(sargs, " ")
cmd := exec.Command("ip", args...)
logger.Info("ip %s", sargs)
Expand Down Expand Up @@ -231,3 +231,32 @@ func unredirectPort(from, to string) error {
}
return nil
}

func fixMSS(iface string) error {
mss := MTU - 40
logger.Info("Fix MSS with iptables to %d", mss)
sargs := fmt.Sprintf("-I FORWARD -o %s -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --set-mss %d", iface, mss)
args := strings.Split(sargs, " ")
cmd := exec.Command("iptables", args...)
err := cmd.Run()

if err != nil {
return err
}
return nil
}

func clearMSS(iface string) error {
mss := MTU - 40
logger.Info("Clean MSS fix", mss)
sargs := fmt.Sprintf("-D FORWARD -o %s -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --set-mss %d", iface, mss)
args := strings.Split(sargs, " ")
cmd := exec.Command("iptables", args...)
err := cmd.Run()

if err != nil {
return err
}

return nil
}
6 changes: 5 additions & 1 deletion hop/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ func NewServer(cfg HopServerConfig) error {
return err
}

if cfg.MTU != 0 {
MTU = cfg.MTU
}

hopServer := new(HopServer)
hopServer.fromNet = make(chan *udpPacket, 32)
hopServer.fromIface = make(chan []byte, 32)
Expand Down Expand Up @@ -140,7 +144,7 @@ func NewServer(cfg HopServerConfig) error {
}
}()

buf := make([]byte, MTU)
buf := make([]byte, IFACE_BUFSIZE)
for {
n, err := iface.Read(buf)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
var srvMode, cltMode, debug, getVersion bool
var cfgFile string

var VERSION = "0.3dev"
var VERSION = "0.3alpha1"

func main() {
flag.BoolVar(&getVersion, "version", false, "Get Version info")
Expand Down

0 comments on commit a5940b2

Please sign in to comment.