]> Cypherpunks repositories - gostls13.git/commitdiff
net: relax IP interface address determination on linux
authorMikio Hara <mikioh.mikioh@gmail.com>
Tue, 23 Jun 2015 12:40:33 +0000 (21:40 +0900)
committerMikio Hara <mikioh.mikioh@gmail.com>
Sat, 27 Jun 2015 00:39:30 +0000 (00:39 +0000)
Linux allows to have a peer IP address on IP interface over ethernet
link encapsulation, though it only installs a static route with the peer
address as an on-link nexthop.

Fixes #11338.

Change-Id: Ie2583737e4c7cec39baabb89dd732463d3f10a61
Reviewed-on: https://go-review.googlesource.com/11352
Reviewed-by: Russ Cox <rsc@golang.org>
src/net/interface_bsd_test.go
src/net/interface_linux.go
src/net/interface_linux_test.go
src/net/interface_unix_test.go

index 88daf73935abb79ed4f6a9c2f610b9d5d31c8805..43ccc89f4329f91d4a706e5678298616cc6be33b 100644 (file)
@@ -28,10 +28,8 @@ func (ti *testInterface) setBroadcast(suffix int) error {
        return nil
 }
 
-func (ti *testInterface) setPointToPoint(suffix int, local, remote string) error {
+func (ti *testInterface) setPointToPoint(suffix int) error {
        ti.name = fmt.Sprintf("gif%d", suffix)
-       ti.local = local
-       ti.remote = remote
        xname, err := exec.LookPath("ifconfig")
        if err != nil {
                return err
index 6551a3562e97aee4904a8baad8d94e7a165b5994..ef2042920ed3b706f7cba1446215ce65666b9942 100644 (file)
@@ -176,17 +176,15 @@ func newAddr(ifi *Interface, ifam *syscall.IfAddrmsg, attrs []syscall.NetlinkRou
        var ipPointToPoint bool
        // Seems like we need to make sure whether the IP interface
        // stack consists of IP point-to-point numbered or unnumbered
-       // addressing over point-to-point link encapsulation.
-       if ifi.Flags&FlagPointToPoint != 0 {
-               for _, a := range attrs {
-                       if a.Attr.Type == syscall.IFA_LOCAL {
-                               ipPointToPoint = true
-                               break
-                       }
+       // addressing.
+       for _, a := range attrs {
+               if a.Attr.Type == syscall.IFA_LOCAL {
+                       ipPointToPoint = true
+                       break
                }
        }
        for _, a := range attrs {
-               if ipPointToPoint && a.Attr.Type == syscall.IFA_ADDRESS || !ipPointToPoint && a.Attr.Type == syscall.IFA_LOCAL {
+               if ipPointToPoint && a.Attr.Type == syscall.IFA_ADDRESS {
                        continue
                }
                switch ifam.Family {
index 059bde11c6ce77ec28e25dbbdb31ea1f4888379e..6251b2674c1987c13bb72a2ac5a9203270ed6336 100644 (file)
@@ -20,6 +20,14 @@ func (ti *testInterface) setBroadcast(suffix int) error {
                Path: xname,
                Args: []string{"ip", "link", "add", ti.name, "type", "dummy"},
        })
+       ti.setupCmds = append(ti.setupCmds, &exec.Cmd{
+               Path: xname,
+               Args: []string{"ip", "address", "add", ti.local, "peer", ti.remote, "dev", ti.name},
+       })
+       ti.teardownCmds = append(ti.teardownCmds, &exec.Cmd{
+               Path: xname,
+               Args: []string{"ip", "address", "del", ti.local, "peer", ti.remote, "dev", ti.name},
+       })
        ti.teardownCmds = append(ti.teardownCmds, &exec.Cmd{
                Path: xname,
                Args: []string{"ip", "link", "delete", ti.name, "type", "dummy"},
@@ -27,29 +35,27 @@ func (ti *testInterface) setBroadcast(suffix int) error {
        return nil
 }
 
-func (ti *testInterface) setPointToPoint(suffix int, local, remote string) error {
+func (ti *testInterface) setPointToPoint(suffix int) error {
        ti.name = fmt.Sprintf("gotest%d", suffix)
-       ti.local = local
-       ti.remote = remote
        xname, err := exec.LookPath("ip")
        if err != nil {
                return err
        }
        ti.setupCmds = append(ti.setupCmds, &exec.Cmd{
                Path: xname,
-               Args: []string{"ip", "tunnel", "add", ti.name, "mode", "gre", "local", local, "remote", remote},
+               Args: []string{"ip", "tunnel", "add", ti.name, "mode", "gre", "local", ti.local, "remote", ti.remote},
+       })
+       ti.setupCmds = append(ti.setupCmds, &exec.Cmd{
+               Path: xname,
+               Args: []string{"ip", "address", "add", ti.local, "peer", ti.remote, "dev", ti.name},
        })
        ti.teardownCmds = append(ti.teardownCmds, &exec.Cmd{
                Path: xname,
-               Args: []string{"ip", "tunnel", "del", ti.name, "mode", "gre", "local", local, "remote", remote},
+               Args: []string{"ip", "address", "del", ti.local, "peer", ti.remote, "dev", ti.name},
        })
-       xname, err = exec.LookPath("ifconfig")
-       if err != nil {
-               return err
-       }
-       ti.setupCmds = append(ti.setupCmds, &exec.Cmd{
+       ti.teardownCmds = append(ti.teardownCmds, &exec.Cmd{
                Path: xname,
-               Args: []string{"ifconfig", ti.name, "inet", local, "dstaddr", remote},
+               Args: []string{"ip", "tunnel", "del", ti.name, "mode", "gre", "local", ti.local, "remote", ti.remote},
        })
        return nil
 }
index 84bf06cbce57cfaeae96850bafa8b3c6628ef5aa..93b3b79afda8c4099c023e42f4417fc698c611a6 100644 (file)
@@ -54,8 +54,8 @@ func TestPointToPointInterface(t *testing.T) {
        local, remote := "169.254.0.1", "169.254.0.254"
        ip := ParseIP(remote)
        for i := 0; i < 3; i++ {
-               ti := &testInterface{}
-               if err := ti.setPointToPoint(5963+i, local, remote); err != nil {
+               ti := &testInterface{local: local, remote: remote}
+               if err := ti.setPointToPoint(5963 + i); err != nil {
                        t.Skipf("test requries external command: %v", err)
                }
                if err := ti.setup(); err != nil {
@@ -69,17 +69,18 @@ func TestPointToPointInterface(t *testing.T) {
                        t.Fatal(err)
                }
                for _, ifi := range ift {
-                       if ti.name == ifi.Name {
-                               ifat, err := ifi.Addrs()
-                               if err != nil {
+                       if ti.name != ifi.Name {
+                               continue
+                       }
+                       ifat, err := ifi.Addrs()
+                       if err != nil {
+                               ti.teardown()
+                               t.Fatal(err)
+                       }
+                       for _, ifa := range ifat {
+                               if ip.Equal(ifa.(*IPNet).IP) {
                                        ti.teardown()
-                                       t.Fatal(err)
-                               }
-                               for _, ifa := range ifat {
-                                       if ip.Equal(ifa.(*IPNet).IP) {
-                                               ti.teardown()
-                                               t.Fatalf("got %v; want %v", ip, local)
-                                       }
+                                       t.Fatalf("got %v", ifa)
                                }
                        }
                }
@@ -99,12 +100,14 @@ func TestInterfaceArrivalAndDeparture(t *testing.T) {
                t.Skip("must be root")
        }
 
+       local, remote := "169.254.0.1", "169.254.0.254"
+       ip := ParseIP(remote)
        for i := 0; i < 3; i++ {
                ift1, err := Interfaces()
                if err != nil {
                        t.Fatal(err)
                }
-               ti := &testInterface{}
+               ti := &testInterface{local: local, remote: remote}
                if err := ti.setBroadcast(5682 + i); err != nil {
                        t.Skipf("test requires external command: %v", err)
                }
@@ -128,6 +131,22 @@ func TestInterfaceArrivalAndDeparture(t *testing.T) {
                        ti.teardown()
                        t.Fatalf("got %v; want gt %v", len(ift2), len(ift1))
                }
+               for _, ifi := range ift2 {
+                       if ti.name != ifi.Name {
+                               continue
+                       }
+                       ifat, err := ifi.Addrs()
+                       if err != nil {
+                               ti.teardown()
+                               t.Fatal(err)
+                       }
+                       for _, ifa := range ifat {
+                               if ip.Equal(ifa.(*IPNet).IP) {
+                                       ti.teardown()
+                                       t.Fatalf("got %v", ifa)
+                               }
+                       }
+               }
                if err := ti.teardown(); err != nil {
                        t.Fatal(err)
                } else {