From 330175889ed92ef3410c5f19ae1c6b8393e9a878 Mon Sep 17 00:00:00 2001 From: Arceliar Date: Thu, 14 Jun 2018 07:08:48 -0500 Subject: [PATCH 1/3] switch address range from fd00::/8 to the deprecated 0200::/7 range --- src/yggdrasil/address.go | 31 +++++++++++++++---------------- src/yggdrasil/admin.go | 2 +- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/yggdrasil/address.go b/src/yggdrasil/address.go index e0baf9c..a499edf 100644 --- a/src/yggdrasil/address.go +++ b/src/yggdrasil/address.go @@ -7,9 +7,10 @@ type address [16]byte type subnet [8]byte // address_prefix is the prefix used for all addresses and subnets in the network. -// The current implementation requires this to be a multiple of 8 bits. +// The current implementation requires this to be a muliple of 8 bits + 7 bits. +// The 8th bit of the last byte is used to signal nodes (0) or /64 prefixes (1). // Nodes that configure this differently will be unable to communicate with eachother, though routing and the DHT machinery *should* still work. -var address_prefix = [...]byte{0xfd} +var address_prefix = [...]byte{0x02} // isValid returns true if an address falls within the range used by nodes in the network. func (a *address) isValid() bool { @@ -18,7 +19,7 @@ func (a *address) isValid() bool { return false } } - return (*a)[len(address_prefix)]&0x80 == 0 + return (*a)[len(address_prefix)-1]&0x80 == 0 } // isValid returns true if a prefix falls within the range usable by the network. @@ -28,14 +29,13 @@ func (s *subnet) isValid() bool { return false } } - return (*s)[len(address_prefix)]&0x80 != 0 + return (*s)[len(address_prefix)-1]&0x80 != 0 } // address_addrForNodeID takes a *NodeID as an argument and returns an *address. -// This address begins with the address prefix. -// The next bit is 0 for an address, and 1 for a subnet. -// The following 7 bits are set to the number of leading 1 bits in the NodeID. -// The NodeID, excluding the leading 1 bits and the first leading 1 bit, is truncated to the appropriate length and makes up the remainder of the address. +// This subnet begins with the address prefix, with the last bit set to 0 to indicate an address. +// The following 8 bits are set to the number of leading 1 bits in the NodeID. +// The NodeID, excluding the leading 1 bits and the first leading 0 bit, is truncated to the appropriate length and makes up the remainder of the address. func address_addrForNodeID(nid *NodeID) *address { // 128 bit address // Begins with prefix @@ -67,16 +67,15 @@ func address_addrForNodeID(nid *NodeID) *address { } } copy(addr[:], address_prefix[:]) - addr[len(address_prefix)] = ones & 0x7f + addr[len(address_prefix)] = ones copy(addr[len(address_prefix)+1:], temp) return &addr } // address_subnetForNodeID takes a *NodeID as an argument and returns a *subnet. -// This subnet begins with the address prefix. -// The next bit is 0 for an address, and 1 for a subnet. -// The following 7 bits are set to the number of leading 1 bits in the NodeID. -// The NodeID, excluding the leading 1 bits and the first leading 1 bit, is truncated to the appropriate length and makes up the remainder of the subnet. +// This subnet begins with the address prefix, with the last bit set to 1 to indicate a prefix. +// The following 8 bits are set to the number of leading 1 bits in the NodeID. +// The NodeID, excluding the leading 1 bits and the first leading 0 bit, is truncated to the appropriate length and makes up the remainder of the subnet. func address_subnetForNodeID(nid *NodeID) *subnet { // Exactly as the address version, with two exceptions: // 1) The first bit after the fixed prefix is a 1 instead of a 0 @@ -84,7 +83,7 @@ func address_subnetForNodeID(nid *NodeID) *subnet { addr := *address_addrForNodeID(nid) var snet subnet copy(snet[:], addr[:]) - snet[len(address_prefix)] |= 0x80 + snet[len(address_prefix)-1] |= 0x80 return &snet } @@ -97,7 +96,7 @@ func (a *address) getNodeIDandMask() (*NodeID, *NodeID) { // This means truncated leading 1s, first leading 0, and visible part of addr var nid NodeID var mask NodeID - ones := int(a[len(address_prefix)] & 0x7f) + ones := int(a[len(address_prefix)]) for idx := 0; idx < ones; idx++ { nid[idx/8] |= 0x80 >> byte(idx%8) } @@ -125,7 +124,7 @@ func (s *subnet) getNodeIDandMask() (*NodeID, *NodeID) { // As with the address version, but visible parts of the subnet prefix instead var nid NodeID var mask NodeID - ones := int(s[len(address_prefix)] & 0x7f) + ones := int(s[len(address_prefix)]) for idx := 0; idx < ones; idx++ { nid[idx/8] |= 0x80 >> byte(idx%8) } diff --git a/src/yggdrasil/admin.go b/src/yggdrasil/admin.go index f09835f..0a9194e 100644 --- a/src/yggdrasil/admin.go +++ b/src/yggdrasil/admin.go @@ -406,7 +406,7 @@ func (a *admin) startTunWithMTU(ifname string, iftapmode bool, ifmtu int) error _ = a.core.tun.close() // Then reconfigure and start it addr := a.core.router.addr - straddr := fmt.Sprintf("%s/%v", net.IP(addr[:]).String(), 8*len(address_prefix)) + straddr := fmt.Sprintf("%s/%v", net.IP(addr[:]).String(), 8*len(address_prefix)-1) if ifname != "none" { err := a.core.tun.setup(ifname, iftapmode, straddr, ifmtu) if err != nil { From e7fca666554cddb3f7104d920c59c98cdb79ca7f Mon Sep 17 00:00:00 2001 From: Arceliar Date: Thu, 14 Jun 2018 07:58:07 -0500 Subject: [PATCH 2/3] fix address/prefix code, platform specific parts still need testing --- src/yggdrasil/address.go | 9 +++++---- src/yggdrasil/core.go | 2 +- src/yggdrasil/icmpv6.go | 2 +- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/yggdrasil/address.go b/src/yggdrasil/address.go index a499edf..dd2c410 100644 --- a/src/yggdrasil/address.go +++ b/src/yggdrasil/address.go @@ -19,17 +19,18 @@ func (a *address) isValid() bool { return false } } - return (*a)[len(address_prefix)-1]&0x80 == 0 + return true } // isValid returns true if a prefix falls within the range usable by the network. func (s *subnet) isValid() bool { - for idx := range address_prefix { + l := len(address_prefix) + for idx := range address_prefix[:l-1] { if (*s)[idx] != address_prefix[idx] { return false } } - return (*s)[len(address_prefix)-1]&0x80 != 0 + return (*s)[l-1] == address_prefix[l-1]|0x01 } // address_addrForNodeID takes a *NodeID as an argument and returns an *address. @@ -83,7 +84,7 @@ func address_subnetForNodeID(nid *NodeID) *subnet { addr := *address_addrForNodeID(nid) var snet subnet copy(snet[:], addr[:]) - snet[len(address_prefix)-1] |= 0x80 + snet[len(address_prefix)-1] |= 0x01 return &snet } diff --git a/src/yggdrasil/core.go b/src/yggdrasil/core.go index e1da6fe..bd7fece 100644 --- a/src/yggdrasil/core.go +++ b/src/yggdrasil/core.go @@ -117,7 +117,7 @@ func (c *Core) Start(nc *config.NodeConfig, log *log.Logger) error { } ip := net.IP(c.router.addr[:]).String() - if err := c.tun.start(nc.IfName, nc.IfTAPMode, fmt.Sprintf("%s/8", ip), nc.IfMTU); err != nil { + if err := c.tun.start(nc.IfName, nc.IfTAPMode, fmt.Sprintf("%s/%d", ip, 8*len(address_prefix)-1), nc.IfMTU); err != nil { c.log.Println("Failed to start TUN/TAP") return err } diff --git a/src/yggdrasil/icmpv6.go b/src/yggdrasil/icmpv6.go index 1eb1c67..0491f88 100644 --- a/src/yggdrasil/icmpv6.go +++ b/src/yggdrasil/icmpv6.go @@ -252,7 +252,7 @@ func (i *icmpv6) handle_ndp(in []byte) ([]byte, error) { case source.isValid(): case snet.isValid(): default: - return nil, errors.New("Not an NDP for fd00::/8") + return nil, errors.New("Not an NDP for 0200::/7") } // Create our NDP message body response From 93ffc0b876562b35053b5668c69b94d8a4f152c7 Mon Sep 17 00:00:00 2001 From: Arceliar Date: Thu, 14 Jun 2018 08:00:57 -0500 Subject: [PATCH 3/3] macos --- src/yggdrasil/tun_darwin.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/yggdrasil/tun_darwin.go b/src/yggdrasil/tun_darwin.go index bdfda38..ff85aa4 100644 --- a/src/yggdrasil/tun_darwin.go +++ b/src/yggdrasil/tun_darwin.go @@ -87,7 +87,7 @@ func (tun *tunDevice) setupAddress(addr string) error { ar.ifra_prefixmask.sin6_len = uint8(unsafe.Sizeof(ar.ifra_prefixmask)) b := make([]byte, 16) - binary.LittleEndian.PutUint16(b, uint16(0xFF00)) + binary.LittleEndian.PutUint16(b, uint16(0xFE00)) ar.ifra_prefixmask.sin6_addr[0] = uint16(binary.BigEndian.Uint16(b)) ar.ifra_addr.sin6_len = uint8(unsafe.Sizeof(ar.ifra_addr))