2017-05-30 20:36:49 +00:00
|
|
|
package main
|
|
|
|
|
2017-06-04 19:48:15 +00:00
|
|
|
import (
|
2017-06-28 21:45:45 +00:00
|
|
|
"errors"
|
2017-06-04 19:48:15 +00:00
|
|
|
"net"
|
|
|
|
)
|
|
|
|
|
2017-06-01 19:31:30 +00:00
|
|
|
/* Binary trie
|
2017-06-28 21:45:45 +00:00
|
|
|
*
|
|
|
|
* The net.IPs used here are not formatted the
|
|
|
|
* same way as those created by the "net" functions.
|
|
|
|
* Here the IPs are slices of either 4 or 16 byte (not always 16)
|
2017-06-01 19:31:30 +00:00
|
|
|
*
|
2017-12-01 22:37:26 +00:00
|
|
|
* Synchronization done separately
|
2017-06-01 19:31:30 +00:00
|
|
|
* See: routing.go
|
2017-05-30 20:36:49 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
type Trie struct {
|
|
|
|
cidr uint
|
|
|
|
child [2]*Trie
|
|
|
|
bits []byte
|
|
|
|
peer *Peer
|
|
|
|
|
2017-07-13 12:32:40 +00:00
|
|
|
// index of "branching" bit
|
|
|
|
|
2017-05-30 20:36:49 +00:00
|
|
|
bit_at_byte uint
|
|
|
|
bit_at_shift uint
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Finds length of matching prefix
|
2017-12-01 22:37:26 +00:00
|
|
|
*
|
|
|
|
* TODO: Only use during insertion (xor + prefix mask for lookup)
|
|
|
|
* Check out
|
|
|
|
* prefix_matches(struct allowedips_node *node, const u8 *key, u8 bits)
|
|
|
|
* https://git.zx2c4.com/WireGuard/commit/?h=jd/precomputed-prefix-match
|
2017-05-30 20:36:49 +00:00
|
|
|
*
|
2017-08-04 14:15:53 +00:00
|
|
|
* Assumption:
|
|
|
|
* len(ip1) == len(ip2)
|
|
|
|
* len(ip1) mod 4 = 0
|
2017-05-30 20:36:49 +00:00
|
|
|
*/
|
2017-08-04 14:15:53 +00:00
|
|
|
func commonBits(ip1 []byte, ip2 []byte) uint {
|
2017-05-30 20:36:49 +00:00
|
|
|
var i uint
|
2017-08-11 14:18:20 +00:00
|
|
|
size := uint(len(ip1))
|
2017-08-04 14:15:53 +00:00
|
|
|
|
2017-07-13 12:32:40 +00:00
|
|
|
for i = 0; i < size; i++ {
|
2017-06-04 19:48:15 +00:00
|
|
|
v := ip1[i] ^ ip2[i]
|
2017-05-30 20:36:49 +00:00
|
|
|
if v != 0 {
|
|
|
|
v >>= 1
|
|
|
|
if v == 0 {
|
|
|
|
return i*8 + 7
|
|
|
|
}
|
|
|
|
|
|
|
|
v >>= 1
|
|
|
|
if v == 0 {
|
|
|
|
return i*8 + 6
|
|
|
|
}
|
|
|
|
|
|
|
|
v >>= 1
|
|
|
|
if v == 0 {
|
|
|
|
return i*8 + 5
|
|
|
|
}
|
|
|
|
|
|
|
|
v >>= 1
|
|
|
|
if v == 0 {
|
|
|
|
return i*8 + 4
|
|
|
|
}
|
|
|
|
|
|
|
|
v >>= 1
|
|
|
|
if v == 0 {
|
|
|
|
return i*8 + 3
|
|
|
|
}
|
|
|
|
|
|
|
|
v >>= 1
|
|
|
|
if v == 0 {
|
|
|
|
return i*8 + 2
|
|
|
|
}
|
|
|
|
|
|
|
|
v >>= 1
|
|
|
|
if v == 0 {
|
|
|
|
return i*8 + 1
|
|
|
|
}
|
|
|
|
return i * 8
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return i * 8
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node *Trie) RemovePeer(p *Peer) *Trie {
|
|
|
|
if node == nil {
|
|
|
|
return node
|
|
|
|
}
|
|
|
|
|
2017-12-01 22:37:26 +00:00
|
|
|
// walk recursively
|
2017-05-30 20:36:49 +00:00
|
|
|
|
|
|
|
node.child[0] = node.child[0].RemovePeer(p)
|
|
|
|
node.child[1] = node.child[1].RemovePeer(p)
|
|
|
|
|
|
|
|
if node.peer != p {
|
|
|
|
return node
|
|
|
|
}
|
|
|
|
|
2017-07-13 12:32:40 +00:00
|
|
|
// remove peer & merge
|
2017-05-30 20:36:49 +00:00
|
|
|
|
|
|
|
node.peer = nil
|
|
|
|
if node.child[0] == nil {
|
|
|
|
return node.child[1]
|
|
|
|
}
|
|
|
|
return node.child[0]
|
|
|
|
}
|
|
|
|
|
2017-06-04 19:48:15 +00:00
|
|
|
func (node *Trie) choose(ip net.IP) byte {
|
|
|
|
return (ip[node.bit_at_byte] >> node.bit_at_shift) & 1
|
2017-06-01 19:31:30 +00:00
|
|
|
}
|
|
|
|
|
2017-06-04 19:48:15 +00:00
|
|
|
func (node *Trie) Insert(ip net.IP, cidr uint, peer *Peer) *Trie {
|
2017-06-01 19:31:30 +00:00
|
|
|
|
2017-07-13 12:32:40 +00:00
|
|
|
// at leaf
|
2017-06-01 19:31:30 +00:00
|
|
|
|
2017-05-30 20:36:49 +00:00
|
|
|
if node == nil {
|
|
|
|
return &Trie{
|
2017-06-04 19:48:15 +00:00
|
|
|
bits: ip,
|
2017-05-30 20:36:49 +00:00
|
|
|
peer: peer,
|
|
|
|
cidr: cidr,
|
|
|
|
bit_at_byte: cidr / 8,
|
|
|
|
bit_at_shift: 7 - (cidr % 8),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-13 12:32:40 +00:00
|
|
|
// traverse deeper
|
2017-05-30 20:36:49 +00:00
|
|
|
|
2017-06-04 19:48:15 +00:00
|
|
|
common := commonBits(node.bits, ip)
|
2017-05-30 20:36:49 +00:00
|
|
|
if node.cidr <= cidr && common >= node.cidr {
|
|
|
|
if node.cidr == cidr {
|
|
|
|
node.peer = peer
|
|
|
|
return node
|
|
|
|
}
|
2017-06-04 19:48:15 +00:00
|
|
|
bit := node.choose(ip)
|
|
|
|
node.child[bit] = node.child[bit].Insert(ip, cidr, peer)
|
2017-05-30 20:36:49 +00:00
|
|
|
return node
|
|
|
|
}
|
|
|
|
|
2017-07-13 12:32:40 +00:00
|
|
|
// split node
|
2017-05-30 20:36:49 +00:00
|
|
|
|
|
|
|
newNode := &Trie{
|
2017-06-04 19:48:15 +00:00
|
|
|
bits: ip,
|
2017-05-30 20:36:49 +00:00
|
|
|
peer: peer,
|
|
|
|
cidr: cidr,
|
|
|
|
bit_at_byte: cidr / 8,
|
|
|
|
bit_at_shift: 7 - (cidr % 8),
|
|
|
|
}
|
|
|
|
|
|
|
|
cidr = min(cidr, common)
|
|
|
|
|
2017-07-13 12:32:40 +00:00
|
|
|
// check for shorter prefix
|
2017-05-30 20:36:49 +00:00
|
|
|
|
2017-06-01 19:31:30 +00:00
|
|
|
if newNode.cidr == cidr {
|
|
|
|
bit := newNode.choose(node.bits)
|
|
|
|
newNode.child[bit] = node
|
|
|
|
return newNode
|
|
|
|
}
|
|
|
|
|
2017-07-13 12:32:40 +00:00
|
|
|
// create new parent for node & newNode
|
2017-05-30 20:36:49 +00:00
|
|
|
|
2017-06-01 19:31:30 +00:00
|
|
|
parent := &Trie{
|
2017-06-04 19:48:15 +00:00
|
|
|
bits: ip,
|
2017-06-01 19:31:30 +00:00
|
|
|
peer: nil,
|
|
|
|
cidr: cidr,
|
|
|
|
bit_at_byte: cidr / 8,
|
|
|
|
bit_at_shift: 7 - (cidr % 8),
|
2017-05-30 20:36:49 +00:00
|
|
|
}
|
|
|
|
|
2017-06-04 19:48:15 +00:00
|
|
|
bit := parent.choose(ip)
|
2017-06-01 19:31:30 +00:00
|
|
|
parent.child[bit] = newNode
|
|
|
|
parent.child[bit^1] = node
|
|
|
|
|
|
|
|
return parent
|
|
|
|
}
|
|
|
|
|
2017-06-04 19:48:15 +00:00
|
|
|
func (node *Trie) Lookup(ip net.IP) *Peer {
|
2017-06-01 19:31:30 +00:00
|
|
|
var found *Peer
|
2017-06-04 19:48:15 +00:00
|
|
|
size := uint(len(ip))
|
|
|
|
for node != nil && commonBits(node.bits, ip) >= node.cidr {
|
2017-06-01 19:31:30 +00:00
|
|
|
if node.peer != nil {
|
|
|
|
found = node.peer
|
|
|
|
}
|
|
|
|
if node.bit_at_byte == size {
|
|
|
|
break
|
|
|
|
}
|
2017-06-04 19:48:15 +00:00
|
|
|
bit := node.choose(ip)
|
2017-06-01 19:31:30 +00:00
|
|
|
node = node.child[bit]
|
|
|
|
}
|
|
|
|
return found
|
|
|
|
}
|
2017-05-30 20:36:49 +00:00
|
|
|
|
2017-06-01 19:31:30 +00:00
|
|
|
func (node *Trie) Count() uint {
|
|
|
|
if node == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
l := node.child[0].Count()
|
|
|
|
r := node.child[1].Count()
|
|
|
|
return l + r
|
2017-05-30 20:36:49 +00:00
|
|
|
}
|
2017-06-28 21:45:45 +00:00
|
|
|
|
2017-06-29 12:39:21 +00:00
|
|
|
func (node *Trie) AllowedIPs(p *Peer, results []net.IPNet) []net.IPNet {
|
|
|
|
if node == nil {
|
|
|
|
return results
|
|
|
|
}
|
2017-06-28 21:45:45 +00:00
|
|
|
if node.peer == p {
|
|
|
|
var mask net.IPNet
|
|
|
|
mask.Mask = net.CIDRMask(int(node.cidr), len(node.bits)*8)
|
|
|
|
if len(node.bits) == net.IPv4len {
|
|
|
|
mask.IP = net.IPv4(
|
|
|
|
node.bits[0],
|
|
|
|
node.bits[1],
|
|
|
|
node.bits[2],
|
|
|
|
node.bits[3],
|
|
|
|
)
|
|
|
|
} else if len(node.bits) == net.IPv6len {
|
|
|
|
mask.IP = node.bits
|
|
|
|
} else {
|
|
|
|
panic(errors.New("bug: unexpected address length"))
|
|
|
|
}
|
|
|
|
results = append(results, mask)
|
|
|
|
}
|
2017-06-29 12:39:21 +00:00
|
|
|
results = node.child[0].AllowedIPs(p, results)
|
|
|
|
results = node.child[1].AllowedIPs(p, results)
|
|
|
|
return results
|
2017-06-28 21:45:45 +00:00
|
|
|
}
|