| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"crypto/ed25519" |
| 6 |
"fmt" |
| 7 |
"net" |
| 8 |
"sync" |
| 9 |
"time" |
| 10 |
|
| 11 |
"github.com/vishvananda/netlink" |
| 12 |
"golang.zx2c4.com/wireguard/tun" |
| 13 |
|
| 14 |
iwt "github.com/Arceliar/ironwood/types" |
| 15 |
) |
| 16 |
|
| 17 |
func setupTun(ifname, address string) tun.Device { |
| 18 |
dev, err := tun.CreateTUN(ifname, 1500) |
| 19 |
if err != nil { |
| 20 |
panic(err) |
| 21 |
} |
| 22 |
nladdr, err := netlink.ParseAddr(address) |
| 23 |
if err != nil { |
| 24 |
panic(err) |
| 25 |
} |
| 26 |
name, err := dev.Name() |
| 27 |
if err != nil { |
| 28 |
panic(err) |
| 29 |
} |
| 30 |
nlintf, err := netlink.LinkByName(name) |
| 31 |
if err != nil { |
| 32 |
panic(err) |
| 33 |
} else if err := netlink.AddrAdd(nlintf, nladdr); err != nil { |
| 34 |
panic(err) |
| 35 |
} else if err := netlink.LinkSetMTU(nlintf, 1500); err != nil { |
| 36 |
panic(err) |
| 37 |
} else if err := netlink.LinkSetUp(nlintf); err != nil { |
| 38 |
panic(err) |
| 39 |
} |
| 40 |
return dev |
| 41 |
} |
| 42 |
|
| 43 |
const tunOffsetBytes = 4 |
| 44 |
|
| 45 |
func tunReader(dev tun.Device, pc iwt.PacketConn) { |
| 46 |
localAddr := pc.LocalAddr() |
| 47 |
pubKey := ed25519.PublicKey(localAddr.(iwt.Addr)) |
| 48 |
addrBytes := getAddr(pubKey) |
| 49 |
buf := make([]byte, 2048) |
| 50 |
for { |
| 51 |
n, err := dev.Read(buf, tunOffsetBytes) |
| 52 |
if err != nil { |
| 53 |
panic(err) |
| 54 |
} |
| 55 |
if n <= tunOffsetBytes { |
| 56 |
panic("tunOffsetBytes") |
| 57 |
} |
| 58 |
bs := buf[tunOffsetBytes : tunOffsetBytes+n] |
| 59 |
if len(bs) < 40 { |
| 60 |
panic("undersized packet") |
| 61 |
} |
| 62 |
var srcAddr, dstAddr [16]byte |
| 63 |
copy(srcAddr[:], bs[8:24]) |
| 64 |
copy(dstAddr[:], bs[24:40]) |
| 65 |
if srcAddr != addrBytes { |
| 66 |
//panic("wrong source address") |
| 67 |
continue |
| 68 |
} |
| 69 |
if dstAddr[0] != 0xfd { |
| 70 |
//panic("wrong dest subnet") |
| 71 |
continue |
| 72 |
} |
| 73 |
destKey, isGood := getKey(dstAddr) |
| 74 |
//destKey, isGood := getTargetKey(dstAddr) |
| 75 |
if !isGood { |
| 76 |
destKey, _ := getTargetKey(dstAddr) |
| 77 |
pc.SendLookup(destKey) |
| 78 |
pushBufMsg(dstAddr, bs) |
| 79 |
continue |
| 80 |
} |
| 81 |
//destKey = pc.GetKeyFor(destKey) |
| 82 |
if !checkKey(dstAddr, destKey) { |
| 83 |
continue |
| 84 |
} |
| 85 |
if isGood { |
| 86 |
dest := iwt.Addr(destKey) |
| 87 |
n, err = pc.WriteTo(bs, dest) |
| 88 |
if err != nil { |
| 89 |
panic(err) |
| 90 |
} |
| 91 |
if n != len(bs) { |
| 92 |
panic("failed to write full packet to packetconn") |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
func tunWriter(dev tun.Device, pc net.PacketConn) { |
| 99 |
localAddr := pc.LocalAddr() |
| 100 |
pubKey := ed25519.PublicKey(localAddr.(iwt.Addr)) |
| 101 |
addrBytes := getAddr(pubKey) |
| 102 |
rawBuf := make([]byte, 2048) |
| 103 |
for { |
| 104 |
buf := rawBuf |
| 105 |
n, remote, err := pc.ReadFrom(buf[tunOffsetBytes:]) |
| 106 |
if err != nil { |
| 107 |
panic(err) |
| 108 |
} |
| 109 |
if n < 40 { |
| 110 |
panic("undersized packet") |
| 111 |
} |
| 112 |
buf = buf[:tunOffsetBytes+n] |
| 113 |
bs := buf[tunOffsetBytes : tunOffsetBytes+n] |
| 114 |
var srcAddr, dstAddr [16]byte |
| 115 |
copy(srcAddr[:], bs[8:24]) |
| 116 |
copy(dstAddr[:], bs[24:40]) |
| 117 |
if srcAddr[0] != 0xfd { |
| 118 |
fmt.Println(net.IP(srcAddr[:]).String()) // FIXME |
| 119 |
panic("wrong source subnet") |
| 120 |
continue |
| 121 |
} |
| 122 |
if dstAddr[0] != 0xfd { |
| 123 |
panic("wrong dest subnet") |
| 124 |
continue |
| 125 |
} |
| 126 |
if dstAddr != addrBytes { |
| 127 |
panic("wrong dest addr") |
| 128 |
continue |
| 129 |
} |
| 130 |
remoteKey := ed25519.PublicKey(remote.(iwt.Addr)) |
| 131 |
if !checkKey(srcAddr, remoteKey) { |
| 132 |
continue |
| 133 |
} |
| 134 |
//putKey(remoteKey) |
| 135 |
n, err = dev.Write(buf, tunOffsetBytes) |
| 136 |
if err != nil { |
| 137 |
panic(err) |
| 138 |
} |
| 139 |
if n != len(buf) { |
| 140 |
panic("wrong number of bytes written") |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
var keyMutex sync.Mutex |
| 146 |
var keyMap map[[16]byte]*keyInfo |
| 147 |
|
| 148 |
type keyInfo struct { |
| 149 |
key ed25519.PublicKey |
| 150 |
timer *time.Timer |
| 151 |
} |
| 152 |
|
| 153 |
func putKey(key ed25519.PublicKey) { |
| 154 |
addr := getAddr(key) |
| 155 |
info := new(keyInfo) |
| 156 |
info.key = ed25519.PublicKey(append([]byte(nil), key...)) |
| 157 |
info.timer = time.AfterFunc(time.Minute, func() { |
| 158 |
keyMutex.Lock() |
| 159 |
defer keyMutex.Unlock() |
| 160 |
delete(keyMap, addr) |
| 161 |
}) |
| 162 |
keyMutex.Lock() |
| 163 |
defer keyMutex.Unlock() |
| 164 |
if keyMap == nil { |
| 165 |
keyMap = make(map[[16]byte]*keyInfo) |
| 166 |
} |
| 167 |
if old, isIn := keyMap[addr]; isIn { |
| 168 |
old.timer.Stop() |
| 169 |
} |
| 170 |
keyMap[addr] = info |
| 171 |
} |
| 172 |
|
| 173 |
func getTargetKey(addr [16]byte) (ed25519.PublicKey, bool) { |
| 174 |
destKey := ed25519.PublicKey(make([]byte, ed25519.PublicKeySize)) |
| 175 |
copy(destKey, addr[1:]) |
| 176 |
for idx := range destKey { |
| 177 |
destKey[idx] = ^destKey[idx] |
| 178 |
} |
| 179 |
return destKey, true |
| 180 |
} |
| 181 |
|
| 182 |
func getKey(addr [16]byte) (ed25519.PublicKey, bool) { |
| 183 |
keyMutex.Lock() |
| 184 |
info := keyMap[addr] |
| 185 |
keyMutex.Unlock() |
| 186 |
if info != nil { |
| 187 |
//fmt.Println("Found key", net.IP(addr).String(), info.key) |
| 188 |
return info.key, true |
| 189 |
} |
| 190 |
destKey := ed25519.PublicKey(make([]byte, ed25519.PublicKeySize)) |
| 191 |
copy(destKey, addr[1:]) |
| 192 |
for idx := range destKey { |
| 193 |
destKey[idx] = ^destKey[idx] |
| 194 |
} |
| 195 |
return destKey, false |
| 196 |
} |
| 197 |
|
| 198 |
func checkKey(addr [16]byte, key ed25519.PublicKey) bool { |
| 199 |
tmp := addr |
| 200 |
for idx := range tmp { |
| 201 |
tmp[idx] = ^tmp[idx] |
| 202 |
} |
| 203 |
return bytes.Equal(tmp[1:], key[:len(addr)-1]) |
| 204 |
} |
| 205 |
|
| 206 |
func getAddr(key ed25519.PublicKey) (addr [16]byte) { |
| 207 |
copy(addr[1:], key) |
| 208 |
for idx := range addr { |
| 209 |
addr[idx] = ^addr[idx] |
| 210 |
} |
| 211 |
addr[0] = 0xfd |
| 212 |
return |
| 213 |
} |
| 214 |
|
| 215 |
func transformKey(key ed25519.PublicKey) ed25519.PublicKey { |
| 216 |
addr := getAddr(key) |
| 217 |
xform, _ := getTargetKey(addr) |
| 218 |
return xform |
| 219 |
} |
| 220 |
|
| 221 |
// Buffer traffic while waiting for a key |
| 222 |
|
| 223 |
var bufMutex sync.Mutex |
| 224 |
var bufMap map[[16]byte]*bufInfo |
| 225 |
|
| 226 |
type bufInfo struct { |
| 227 |
msg []byte |
| 228 |
timer *time.Timer |
| 229 |
} |
| 230 |
|
| 231 |
func pushBufMsg(addr [16]byte, msg []byte) { |
| 232 |
info := new(bufInfo) |
| 233 |
info.msg = append(info.msg, msg...) |
| 234 |
bufMutex.Lock() |
| 235 |
defer bufMutex.Unlock() |
| 236 |
if bufMap == nil { |
| 237 |
bufMap = make(map[[16]byte]*bufInfo) |
| 238 |
} |
| 239 |
old := bufMap[addr] |
| 240 |
bufMap[addr] = info |
| 241 |
info.timer = time.AfterFunc(time.Minute, func() { |
| 242 |
bufMutex.Lock() |
| 243 |
defer bufMutex.Unlock() |
| 244 |
if n := bufMap[addr]; n == info { |
| 245 |
delete(bufMap, addr) |
| 246 |
} |
| 247 |
}) |
| 248 |
if old != nil { |
| 249 |
old.timer.Stop() |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
func popBufMsg(addr [16]byte) []byte { |
| 254 |
bufMutex.Lock() |
| 255 |
defer bufMutex.Unlock() |
| 256 |
if info := bufMap[addr]; info != nil { |
| 257 |
info.timer.Stop() |
| 258 |
return info.msg |
| 259 |
} |
| 260 |
return nil |
| 261 |
} |
| 262 |
|
| 263 |
const ( |
| 264 |
oobKeyReq = 1 |
| 265 |
oobKeyRes = 2 |
| 266 |
) |
| 267 |
|
| 268 |
func flushBuffer(pc net.PacketConn, destKey ed25519.PublicKey) { |
| 269 |
addr := getAddr(destKey) |
| 270 |
if bs := popBufMsg(addr); bs != nil { |
| 271 |
dest := iwt.Addr(destKey) |
| 272 |
n, err := pc.WriteTo(bs, dest) |
| 273 |
if err != nil { |
| 274 |
panic(err) |
| 275 |
} |
| 276 |
if n != len(bs) { |
| 277 |
panic("failed to write full packet to packetconn") |
| 278 |
} |
| 279 |
} |
| 280 |
} |