node.go 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  1. /*
  2. * Copyright (c)2019 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2023-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. package zerotier
  14. // This wraps the C++ Node implementation, C++ EthernetTap implementations,
  15. // and generally contains all the other CGO stuff.
  16. //#cgo CFLAGS: -O3
  17. //#cgo LDFLAGS: ${SRCDIR}/../../../build/node/libzt_core.a ${SRCDIR}/../../../build/osdep/libzt_osdep.a ${SRCDIR}/../../../build/go/native/libzt_go_native.a -lc++ -lpthread
  18. //#define ZT_CGO 1
  19. //#include "../../native/GoGlue.h"
  20. import "C"
  21. import (
  22. "bytes"
  23. "encoding/binary"
  24. "errors"
  25. "fmt"
  26. "io/ioutil"
  27. rand "math/rand"
  28. "net"
  29. "net/http"
  30. "os"
  31. "path"
  32. "sort"
  33. "sync"
  34. "sync/atomic"
  35. "time"
  36. "unsafe"
  37. acl "github.com/hectane/go-acl"
  38. )
  39. // Network status states
  40. const (
  41. NetworkStatusRequestConfiguration int = C.ZT_NETWORK_STATUS_REQUESTING_CONFIGURATION
  42. NetworkStatusOK int = C.ZT_NETWORK_STATUS_OK
  43. NetworkStatusAccessDenied int = C.ZT_NETWORK_STATUS_ACCESS_DENIED
  44. NetworkStatusNotFound int = C.ZT_NETWORK_STATUS_NOT_FOUND
  45. NetworkTypePrivate int = C.ZT_NETWORK_TYPE_PRIVATE
  46. NetworkTypePublic int = C.ZT_NETWORK_TYPE_PUBLIC
  47. // CoreVersionMajor is the major version of the ZeroTier core
  48. CoreVersionMajor int = C.ZEROTIER_ONE_VERSION_MAJOR
  49. // CoreVersionMinor is the minor version of the ZeroTier core
  50. CoreVersionMinor int = C.ZEROTIER_ONE_VERSION_MINOR
  51. // CoreVersionRevision is the revision of the ZeroTier core
  52. CoreVersionRevision int = C.ZEROTIER_ONE_VERSION_REVISION
  53. // CoreVersionBuild is the build version of the ZeroTier core
  54. CoreVersionBuild int = C.ZEROTIER_ONE_VERSION_BUILD
  55. // PlatformDefaultHomePath is the default location of ZeroTier's working path on this system
  56. PlatformDefaultHomePath string = C.GoString(C.ZT_PLATFORM_DEFAULT_HOMEPATH)
  57. AFInet = C.AF_INET
  58. AFInet6 = C.AF_INET6
  59. defaultVirtualNetworkMTU = C.ZT_DEFAULT_MTU
  60. )
  61. var (
  62. nodesByUserPtr map[uintptr]*Node
  63. nodesByUserPtrLock sync.RWMutex
  64. )
  65. func sockaddrStorageToIPNet(ss *C.struct_sockaddr_storage) *net.IPNet {
  66. var a net.IPNet
  67. switch ss.ss_family {
  68. case AFInet:
  69. sa4 := (*C.struct_sockaddr_in)(unsafe.Pointer(ss))
  70. var ip4 [4]byte
  71. copy(ip4[:], (*[4]byte)(unsafe.Pointer(&sa4.sin_addr))[:])
  72. a.IP = net.IP(ip4[:])
  73. a.Mask = net.CIDRMask(int(binary.BigEndian.Uint16(((*[2]byte)(unsafe.Pointer(&sa4.sin_port)))[:])), 32)
  74. return &a
  75. case AFInet6:
  76. sa6 := (*C.struct_sockaddr_in6)(unsafe.Pointer(ss))
  77. var ip6 [16]byte
  78. copy(ip6[:], (*[16]byte)(unsafe.Pointer(&sa6.sin6_addr))[:])
  79. a.IP = net.IP(ip6[:])
  80. a.Mask = net.CIDRMask(int(binary.BigEndian.Uint16(((*[2]byte)(unsafe.Pointer(&sa6.sin6_port)))[:])), 128)
  81. return &a
  82. }
  83. return nil
  84. }
  85. func sockaddrStorageToUDPAddr(ss *C.struct_sockaddr_storage) *net.UDPAddr {
  86. var a net.UDPAddr
  87. switch ss.ss_family {
  88. case AFInet:
  89. sa4 := (*C.struct_sockaddr_in)(unsafe.Pointer(ss))
  90. var ip4 [4]byte
  91. copy(ip4[:], (*[4]byte)(unsafe.Pointer(&sa4.sin_addr))[:])
  92. a.IP = net.IP(ip4[:])
  93. a.Port = int(binary.BigEndian.Uint16(((*[2]byte)(unsafe.Pointer(&sa4.sin_port)))[:]))
  94. return &a
  95. case AFInet6:
  96. sa6 := (*C.struct_sockaddr_in6)(unsafe.Pointer(ss))
  97. var ip6 [16]byte
  98. copy(ip6[:], (*[16]byte)(unsafe.Pointer(&sa6.sin6_addr))[:])
  99. a.IP = net.IP(ip6[:])
  100. a.Port = int(binary.BigEndian.Uint16(((*[2]byte)(unsafe.Pointer(&sa6.sin6_port)))[:]))
  101. return &a
  102. }
  103. return nil
  104. }
  105. func sockaddrStorageToUDPAddr2(ss unsafe.Pointer) *net.UDPAddr {
  106. return sockaddrStorageToUDPAddr((*C.struct_sockaddr_storage)(ss))
  107. }
  108. func makeSockaddrStorage(ip net.IP, port int, ss *C.struct_sockaddr_storage) bool {
  109. C.memset(unsafe.Pointer(ss), 0, C.sizeof_struct_sockaddr_storage)
  110. if len(ip) == 4 {
  111. sa4 := (*C.struct_sockaddr_in)(unsafe.Pointer(ss))
  112. sa4.sin_family = AFInet
  113. copy(((*[4]byte)(unsafe.Pointer(&sa4.sin_addr)))[:], ip)
  114. binary.BigEndian.PutUint16(((*[2]byte)(unsafe.Pointer(&sa4.sin_port)))[:], uint16(port))
  115. return true
  116. }
  117. if len(ip) == 16 {
  118. sa6 := (*C.struct_sockaddr_in6)(unsafe.Pointer(ss))
  119. sa6.sin6_family = AFInet6
  120. copy(((*[16]byte)(unsafe.Pointer(&sa6.sin6_addr)))[:], ip)
  121. binary.BigEndian.PutUint16(((*[2]byte)(unsafe.Pointer(&sa6.sin6_port)))[:], uint16(port))
  122. return true
  123. }
  124. return false
  125. }
  126. //////////////////////////////////////////////////////////////////////////////
  127. // Node is an instance of the ZeroTier core node and related C++ I/O code
  128. type Node struct {
  129. networks map[NetworkID]*Network
  130. networksByMAC map[MAC]*Network // locked by networksLock
  131. interfaceAddresses map[string]net.IP // physical external IPs on the machine
  132. basePath string
  133. localConfigPath string
  134. localConfig LocalConfig
  135. localConfigLock sync.RWMutex
  136. networksLock sync.RWMutex
  137. interfaceAddressesLock sync.Mutex
  138. gn *C.ZT_GoNode
  139. zn *C.ZT_Node
  140. id *Identity
  141. apiServer *http.Server
  142. online uint32
  143. running uint32
  144. runLock sync.Mutex
  145. }
  146. // NewNode creates and initializes a new instance of the ZeroTier node service
  147. func NewNode(basePath string) (*Node, error) {
  148. os.MkdirAll(basePath, 0755)
  149. if _, err := os.Stat(basePath); err != nil {
  150. return nil, err
  151. }
  152. n := new(Node)
  153. n.networks = make(map[NetworkID]*Network)
  154. n.networksByMAC = make(map[MAC]*Network)
  155. n.interfaceAddresses = make(map[string]net.IP)
  156. n.basePath = basePath
  157. n.localConfigPath = path.Join(basePath, "local.conf")
  158. err := n.localConfig.Read(n.localConfigPath, true)
  159. if err != nil {
  160. return nil, err
  161. }
  162. if n.localConfig.Settings.PortAutoSearch {
  163. portsChanged := false
  164. portCheckCount := 0
  165. for portCheckCount < 2048 {
  166. portCheckCount++
  167. if checkPort(n.localConfig.Settings.PrimaryPort) {
  168. break
  169. }
  170. n.localConfig.Settings.PrimaryPort++
  171. n.localConfig.Settings.PrimaryPort &= 0xffff
  172. portsChanged = true
  173. }
  174. if portCheckCount == 2048 {
  175. return nil, errors.New("unable to bind to primary port, tried 2048 later ports")
  176. }
  177. if n.localConfig.Settings.SecondaryPort > 0 {
  178. portCheckCount = 0
  179. for portCheckCount < 2048 {
  180. portCheckCount++
  181. if checkPort(n.localConfig.Settings.SecondaryPort) {
  182. break
  183. }
  184. n.localConfig.Settings.SecondaryPort++
  185. n.localConfig.Settings.SecondaryPort &= 0xffff
  186. portsChanged = true
  187. }
  188. if portCheckCount == 2048 {
  189. n.localConfig.Settings.SecondaryPort = 0
  190. }
  191. }
  192. if n.localConfig.Settings.TertiaryPort > 0 {
  193. portCheckCount = 0
  194. for portCheckCount < 2048 {
  195. portCheckCount++
  196. if checkPort(n.localConfig.Settings.TertiaryPort) {
  197. break
  198. }
  199. n.localConfig.Settings.TertiaryPort++
  200. n.localConfig.Settings.TertiaryPort &= 0xffff
  201. portsChanged = true
  202. }
  203. if portCheckCount == 2048 {
  204. n.localConfig.Settings.TertiaryPort = 0
  205. }
  206. }
  207. if portsChanged {
  208. n.localConfig.Write(n.localConfigPath)
  209. }
  210. } else if !checkPort(n.localConfig.Settings.PrimaryPort) {
  211. return nil, errors.New("unable to bind to primary port")
  212. }
  213. cpath := C.CString(basePath)
  214. n.gn = C.ZT_GoNode_new(cpath)
  215. C.free(unsafe.Pointer(cpath))
  216. if n.gn == nil {
  217. return nil, ErrNodeInitFailed
  218. }
  219. n.zn = (*C.ZT_Node)(C.ZT_GoNode_getNode(n.gn))
  220. var ns C.ZT_NodeStatus
  221. C.ZT_Node_status(unsafe.Pointer(n.zn), &ns)
  222. n.id, err = NewIdentityFromString(C.GoString(ns.secretIdentity))
  223. if err != nil {
  224. C.ZT_GoNode_delete(n.gn)
  225. return nil, err
  226. }
  227. n.apiServer, err = createAPIServer(basePath, n)
  228. if err != nil {
  229. C.ZT_GoNode_delete(n.gn)
  230. return nil, err
  231. }
  232. gnRawAddr := uintptr(unsafe.Pointer(n.gn))
  233. nodesByUserPtrLock.Lock()
  234. nodesByUserPtr[gnRawAddr] = n
  235. nodesByUserPtrLock.Unlock()
  236. n.online = 0
  237. n.running = 1
  238. n.runLock.Lock()
  239. go func() {
  240. lastScannedInterfaces := int64(0)
  241. for atomic.LoadUint32(&n.running) != 0 {
  242. time.Sleep(1 * time.Second)
  243. now := TimeMs()
  244. if (now - lastScannedInterfaces) >= 30000 {
  245. lastScannedInterfaces = now
  246. interfaceAddresses := make(map[string]net.IP)
  247. ifs, _ := net.Interfaces()
  248. if len(ifs) > 0 {
  249. n.networksLock.RLock()
  250. for _, i := range ifs {
  251. m, _ := NewMACFromBytes(i.HardwareAddr)
  252. if _, isZeroTier := n.networksByMAC[m]; !isZeroTier {
  253. addrs, _ := i.Addrs()
  254. if len(addrs) > 0 {
  255. for _, a := range addrs {
  256. ipn, _ := a.(*net.IPNet)
  257. if ipn != nil {
  258. interfaceAddresses[ipn.IP.String()] = ipn.IP
  259. }
  260. }
  261. }
  262. }
  263. }
  264. n.networksLock.RUnlock()
  265. }
  266. n.localConfigLock.RLock()
  267. n.interfaceAddressesLock.Lock()
  268. for astr, ipn := range interfaceAddresses {
  269. if _, alreadyKnown := n.interfaceAddresses[astr]; !alreadyKnown {
  270. ipCstr := C.CString(ipn.String())
  271. if n.localConfig.Settings.PrimaryPort > 0 && n.localConfig.Settings.PrimaryPort < 65536 {
  272. C.ZT_GoNode_phyStartListen(n.gn, nil, ipCstr, C.int(n.localConfig.Settings.PrimaryPort))
  273. }
  274. if n.localConfig.Settings.SecondaryPort > 0 && n.localConfig.Settings.SecondaryPort < 65536 {
  275. C.ZT_GoNode_phyStartListen(n.gn, nil, ipCstr, C.int(n.localConfig.Settings.SecondaryPort))
  276. }
  277. if n.localConfig.Settings.TertiaryPort > 0 && n.localConfig.Settings.TertiaryPort < 65536 {
  278. C.ZT_GoNode_phyStartListen(n.gn, nil, ipCstr, C.int(n.localConfig.Settings.TertiaryPort))
  279. }
  280. C.free(unsafe.Pointer(ipCstr))
  281. }
  282. }
  283. for astr, ipn := range n.interfaceAddresses {
  284. if _, stillPresent := interfaceAddresses[astr]; !stillPresent {
  285. ipCstr := C.CString(ipn.String())
  286. if n.localConfig.Settings.PrimaryPort > 0 && n.localConfig.Settings.PrimaryPort < 65536 {
  287. C.ZT_GoNode_phyStopListen(n.gn, nil, ipCstr, C.int(n.localConfig.Settings.PrimaryPort))
  288. }
  289. if n.localConfig.Settings.SecondaryPort > 0 && n.localConfig.Settings.SecondaryPort < 65536 {
  290. C.ZT_GoNode_phyStopListen(n.gn, nil, ipCstr, C.int(n.localConfig.Settings.SecondaryPort))
  291. }
  292. if n.localConfig.Settings.TertiaryPort > 0 && n.localConfig.Settings.TertiaryPort < 65536 {
  293. C.ZT_GoNode_phyStopListen(n.gn, nil, ipCstr, C.int(n.localConfig.Settings.TertiaryPort))
  294. }
  295. C.free(unsafe.Pointer(ipCstr))
  296. }
  297. }
  298. n.interfaceAddresses = interfaceAddresses
  299. n.interfaceAddressesLock.Unlock()
  300. n.localConfigLock.RUnlock()
  301. }
  302. }
  303. n.runLock.Unlock()
  304. }()
  305. return n, nil
  306. }
  307. // Close closes this Node and frees its underlying C++ Node structures
  308. func (n *Node) Close() {
  309. if atomic.SwapUint32(&n.running, 0) != 0 {
  310. n.apiServer.Close()
  311. C.ZT_GoNode_delete(n.gn)
  312. nodesByUserPtrLock.Lock()
  313. delete(nodesByUserPtr, uintptr(unsafe.Pointer(n.gn)))
  314. nodesByUserPtrLock.Unlock()
  315. n.runLock.Lock() // wait for gorountine to die
  316. n.runLock.Unlock()
  317. }
  318. }
  319. // Address returns this node's address
  320. func (n *Node) Address() Address { return n.id.address }
  321. // Identity returns this node's identity (including secret portion)
  322. func (n *Node) Identity() *Identity { return n.id }
  323. // Online returns true if this node can reach something
  324. func (n *Node) Online() bool { return atomic.LoadUint32(&n.online) != 0 }
  325. // InterfaceAddresses are external IPs belonging to physical interfaces on this machine
  326. func (n *Node) InterfaceAddresses() []net.IP {
  327. var ea []net.IP
  328. n.interfaceAddressesLock.Lock()
  329. for _, a := range n.interfaceAddresses {
  330. ea = append(ea, a)
  331. }
  332. n.interfaceAddressesLock.Unlock()
  333. sort.Slice(ea, func(a, b int) bool { return bytes.Compare(ea[a], ea[b]) < 0 })
  334. return ea
  335. }
  336. // LocalConfig gets this node's local configuration
  337. func (n *Node) LocalConfig() LocalConfig {
  338. n.localConfigLock.RLock()
  339. defer n.localConfigLock.RUnlock()
  340. return n.localConfig
  341. }
  342. // Join joins a network
  343. // If tap is nil, the default system tap for this OS/platform is used (if available).
  344. func (n *Node) Join(nwid uint64, tap Tap) (*Network, error) {
  345. n.networksLock.RLock()
  346. if nw, have := n.networks[NetworkID(nwid)]; have {
  347. return nw, nil
  348. }
  349. n.networksLock.RUnlock()
  350. if tap != nil {
  351. return nil, errors.New("non-native taps not implemented yet")
  352. }
  353. ntap := C.ZT_GoNode_join(n.gn, C.uint64_t(nwid))
  354. if ntap == nil {
  355. return nil, ErrTapInitFailed
  356. }
  357. nw, err := newNetwork(n, NetworkID(nwid), &nativeTap{tap: unsafe.Pointer(ntap), enabled: 1})
  358. if err != nil {
  359. C.ZT_GoNode_leave(n.gn, C.uint64_t(nwid))
  360. return nil, err
  361. }
  362. n.networksLock.Lock()
  363. n.networks[NetworkID(nwid)] = nw
  364. n.networksLock.Unlock()
  365. return nw, nil
  366. }
  367. // Leave leaves a network
  368. func (n *Node) Leave(nwid uint64) error {
  369. C.ZT_GoNode_leave(n.gn, C.uint64_t(nwid))
  370. n.networksLock.Lock()
  371. delete(n.networks, NetworkID(nwid))
  372. n.networksLock.Unlock()
  373. return nil
  374. }
  375. // Networks returns a list of networks that this node has joined
  376. func (n *Node) Networks() []*Network {
  377. var nws []*Network
  378. n.networksLock.RLock()
  379. for _, nw := range n.networks {
  380. nws = append(nws, nw)
  381. }
  382. n.networksLock.RUnlock()
  383. return nws
  384. }
  385. // AddStaticRoot adds a statically defined root server to this node.
  386. // If a static root with the given identity already exists this will update its IP and port information.
  387. func (n *Node) AddStaticRoot(id *Identity, addrs []net.Addr) {
  388. var saddrs []C.struct_sockaddr_storage
  389. for _, a := range addrs {
  390. aa, _ := a.(*net.UDPAddr)
  391. if aa != nil {
  392. ss := new(C.struct_sockaddr_storage)
  393. if makeSockaddrStorage(aa.IP, aa.Port, ss) {
  394. saddrs = append(saddrs, *ss)
  395. }
  396. }
  397. }
  398. if len(saddrs) > 0 {
  399. ids := C.CString(id.String())
  400. C.ZT_Node_setStaticRoot(unsafe.Pointer(n.zn), ids, &saddrs[0], C.uint(len(saddrs)))
  401. C.free(unsafe.Pointer(ids))
  402. }
  403. }
  404. // RemoveStaticRoot removes a statically defined root server from this node.
  405. func (n *Node) RemoveStaticRoot(id *Identity) {
  406. ids := C.CString(id.String())
  407. C.ZT_Node_removeStaticRoot(unsafe.Pointer(n.zn), ids)
  408. C.free(unsafe.Pointer(ids))
  409. }
  410. // AddDynamicRoot adds a dynamic root to this node.
  411. // If the locator parameter is non-empty it can contain a binary serialized locator
  412. // to use if (or until) one can be fetched via DNS.
  413. func (n *Node) AddDynamicRoot(dnsName string, locator []byte) {
  414. dn := C.CString(dnsName)
  415. if len(locator) > 0 {
  416. C.ZT_Node_setDynamicRoot(unsafe.Pointer(n.zn), dn, unsafe.Pointer(&locator[0]), C.uint(len(locator)))
  417. } else {
  418. C.ZT_Node_setDynamicRoot(unsafe.Pointer(n.zn), dn, nil, 0)
  419. }
  420. C.free(unsafe.Pointer(dn))
  421. }
  422. // RemoveDynamicRoot removes a dynamic root from this node.
  423. func (n *Node) RemoveDynamicRoot(dnsName string) {
  424. dn := C.CString(dnsName)
  425. C.ZT_Node_removeDynamicRoot(unsafe.Pointer(n.zn), dn)
  426. C.free(unsafe.Pointer(dn))
  427. }
  428. // Roots retrieves a list of root servers on this node and their preferred and online status.
  429. func (n *Node) Roots() []*Root {
  430. var roots []*Root
  431. rl := C.ZT_Node_listRoots(unsafe.Pointer(n.zn), C.int64_t(TimeMs()))
  432. if rl != nil {
  433. for i := 0; i < int(rl.count); i++ {
  434. root := (*C.ZT_Root)(unsafe.Pointer(uintptr(unsafe.Pointer(rl)) + C.sizeof_ZT_RootList))
  435. id, err := NewIdentityFromString(C.GoString(root.identity))
  436. if err == nil {
  437. var addrs []InetAddress
  438. for j := uintptr(0); j < uintptr(root.addressCount); j++ {
  439. a := NewInetAddressFromSockaddr(unsafe.Pointer(uintptr(unsafe.Pointer(root.addresses)) + (j * C.sizeof_struct_sockaddr_storage)))
  440. if a != nil && a.Valid() {
  441. addrs = append(addrs, *a)
  442. }
  443. }
  444. roots = append(roots, &Root{
  445. DNSName: C.GoString(root.dnsName),
  446. Identity: id,
  447. Addresses: addrs,
  448. Preferred: (root.preferred != 0),
  449. Online: (root.online != 0),
  450. })
  451. }
  452. }
  453. C.ZT_Node_freeQueryResult(unsafe.Pointer(n.zn), unsafe.Pointer(rl))
  454. }
  455. return roots
  456. }
  457. // Peers retrieves a list of current peers
  458. func (n *Node) Peers() []*Peer {
  459. var peers []*Peer
  460. pl := C.ZT_Node_peers(unsafe.Pointer(n.zn))
  461. if pl != nil {
  462. for i := uintptr(0); i < uintptr(pl.peerCount); i++ {
  463. p := (*C.ZT_Peer)(unsafe.Pointer(uintptr(unsafe.Pointer(pl.peers)) + (i * C.sizeof_ZT_Peer)))
  464. p2 := new(Peer)
  465. p2.Address = Address(p.address)
  466. p2.Version = [3]int{int(p.versionMajor), int(p.versionMinor), int(p.versionRev)}
  467. p2.Latency = int(p.latency)
  468. p2.Role = int(p.role)
  469. p2.Paths = make([]Path, 0, int(p.pathCount))
  470. for j := uintptr(0); j < uintptr(p.pathCount); j++ {
  471. pt := &p.paths[j]
  472. a := sockaddrStorageToUDPAddr(&pt.address)
  473. if a != nil {
  474. p2.Paths = append(p2.Paths, Path{
  475. IP: a.IP,
  476. Port: a.Port,
  477. LastSend: int64(pt.lastSend),
  478. LastReceive: int64(pt.lastReceive),
  479. TrustedPathID: uint64(pt.trustedPathId),
  480. Latency: float32(pt.latency),
  481. PacketDelayVariance: float32(pt.packetDelayVariance),
  482. ThroughputDisturbCoeff: float32(pt.throughputDisturbCoeff),
  483. PacketErrorRatio: float32(pt.packetErrorRatio),
  484. PacketLossRatio: float32(pt.packetLossRatio),
  485. Stability: float32(pt.stability),
  486. Throughput: uint64(pt.throughput),
  487. MaxThroughput: uint64(pt.maxThroughput),
  488. Allocation: float32(pt.allocation),
  489. })
  490. }
  491. }
  492. peers = append(peers, p2)
  493. }
  494. C.ZT_Node_freeQueryResult(unsafe.Pointer(n.zn), unsafe.Pointer(pl))
  495. }
  496. return peers
  497. }
  498. //////////////////////////////////////////////////////////////////////////////
  499. func (n *Node) multicastSubscribe(nwid uint64, mg *MulticastGroup) {
  500. C.ZT_Node_multicastSubscribe(unsafe.Pointer(n.zn), nil, C.uint64_t(nwid), C.uint64_t(mg.MAC), C.ulong(mg.ADI))
  501. }
  502. func (n *Node) multicastUnsubscribe(nwid uint64, mg *MulticastGroup) {
  503. C.ZT_Node_multicastUnsubscribe(unsafe.Pointer(n.zn), C.uint64_t(nwid), C.uint64_t(mg.MAC), C.ulong(mg.ADI))
  504. }
  505. func (n *Node) pathCheck(ztAddress Address, af int, ip net.IP, port int) bool {
  506. n.localConfigLock.RLock()
  507. defer n.localConfigLock.RUnlock()
  508. for cidr, phy := range n.localConfig.Physical {
  509. if phy.Blacklist {
  510. _, ipn, _ := net.ParseCIDR(cidr)
  511. if ipn != nil && ipn.Contains(ip) {
  512. return false
  513. }
  514. }
  515. }
  516. return true
  517. }
  518. func (n *Node) pathLookup(ztAddress Address) (net.IP, int) {
  519. n.localConfigLock.RLock()
  520. defer n.localConfigLock.RUnlock()
  521. virt := n.localConfig.Virtual[ztAddress]
  522. if virt != nil && len(virt.Try) > 0 {
  523. idx := rand.Int() % len(virt.Try)
  524. return virt.Try[idx].IP, virt.Try[idx].Port
  525. }
  526. return nil, 0
  527. }
  528. func (n *Node) makeStateObjectPath(objType int, id [2]uint64) (string, bool) {
  529. var fp string
  530. secret := false
  531. switch objType {
  532. case C.ZT_STATE_OBJECT_IDENTITY_PUBLIC:
  533. fp = path.Join(n.basePath, "identity.public")
  534. case C.ZT_STATE_OBJECT_IDENTITY_SECRET:
  535. fp = path.Join(n.basePath, "identity.secret")
  536. secret = true
  537. case C.ZT_STATE_OBJECT_PEER:
  538. fp = path.Join(n.basePath, "peers.d")
  539. os.Mkdir(fp, 0700)
  540. fp = path.Join(fp, fmt.Sprintf("%.10x.peer", id[0]))
  541. secret = true
  542. case C.ZT_STATE_OBJECT_NETWORK_CONFIG:
  543. fp = path.Join(n.basePath, "networks.d")
  544. os.Mkdir(fp, 0755)
  545. fp = path.Join(fp, fmt.Sprintf("%.16x.conf", id[0]))
  546. case C.ZT_STATE_OBJECT_ROOT_LIST:
  547. fp = path.Join(n.basePath, "roots")
  548. }
  549. return fp, secret
  550. }
  551. func (n *Node) stateObjectPut(objType int, id [2]uint64, data []byte) {
  552. fp, secret := n.makeStateObjectPath(objType, id)
  553. if len(fp) > 0 {
  554. fileMode := os.FileMode(0644)
  555. if secret {
  556. fileMode = os.FileMode(0600)
  557. }
  558. ioutil.WriteFile(fp, data, fileMode)
  559. if secret {
  560. acl.Chmod(fp, 0600) // this emulates Unix chmod on Windows and uses os.Chmod on Unix-type systems
  561. }
  562. }
  563. }
  564. func (n *Node) stateObjectDelete(objType int, id [2]uint64) {
  565. fp, _ := n.makeStateObjectPath(objType, id)
  566. if len(fp) > 0 {
  567. os.Remove(fp)
  568. }
  569. }
  570. func (n *Node) stateObjectGet(objType int, id [2]uint64) ([]byte, bool) {
  571. fp, _ := n.makeStateObjectPath(objType, id)
  572. if len(fp) > 0 {
  573. fd, err := ioutil.ReadFile(fp)
  574. if err != nil {
  575. return nil, false
  576. }
  577. return fd, true
  578. }
  579. return nil, false
  580. }
  581. func (n *Node) handleTrace(traceMessage string) {
  582. }
  583. func (n *Node) handleUserMessage(originAddress, messageTypeID uint64, data []byte) {
  584. }
  585. func (n *Node) handleRemoteTrace(originAddress uint64, dictData []byte) {
  586. }
  587. //////////////////////////////////////////////////////////////////////////////
  588. //export goPathCheckFunc
  589. func goPathCheckFunc(gn unsafe.Pointer, ztAddress C.uint64_t, af C.int, ip unsafe.Pointer, port C.int) C.int {
  590. nodesByUserPtrLock.RLock()
  591. node := nodesByUserPtr[uintptr(gn)]
  592. nodesByUserPtrLock.RUnlock()
  593. if node != nil && node.pathCheck(Address(ztAddress), int(af), nil, int(port)) {
  594. return 1
  595. }
  596. return 0
  597. }
  598. //export goPathLookupFunc
  599. func goPathLookupFunc(gn unsafe.Pointer, ztAddress C.uint64_t, desiredAddressFamily int, familyP, ipP, portP unsafe.Pointer) C.int {
  600. nodesByUserPtrLock.RLock()
  601. node := nodesByUserPtr[uintptr(gn)]
  602. nodesByUserPtrLock.RUnlock()
  603. if node == nil {
  604. return 0
  605. }
  606. ip, port := node.pathLookup(Address(ztAddress))
  607. if len(ip) > 0 && port > 0 && port <= 65535 {
  608. ip4 := ip.To4()
  609. if len(ip4) == 4 {
  610. *((*C.int)(familyP)) = C.int(AFInet)
  611. copy((*[4]byte)(ipP)[:], ip4)
  612. *((*C.int)(portP)) = C.int(port)
  613. return 1
  614. } else if len(ip) == 16 {
  615. *((*C.int)(familyP)) = C.int(AFInet6)
  616. copy((*[16]byte)(ipP)[:], ip)
  617. *((*C.int)(portP)) = C.int(port)
  618. return 1
  619. }
  620. }
  621. return 0
  622. }
  623. //export goStateObjectPutFunc
  624. func goStateObjectPutFunc(gn unsafe.Pointer, objType C.int, id, data unsafe.Pointer, len C.int) {
  625. go func() {
  626. nodesByUserPtrLock.RLock()
  627. node := nodesByUserPtr[uintptr(gn)]
  628. nodesByUserPtrLock.RUnlock()
  629. if node == nil {
  630. return
  631. }
  632. if len < 0 {
  633. node.stateObjectDelete(int(objType), *((*[2]uint64)(id)))
  634. } else {
  635. node.stateObjectPut(int(objType), *((*[2]uint64)(id)), C.GoBytes(data, len))
  636. }
  637. }()
  638. }
  639. //export goStateObjectGetFunc
  640. func goStateObjectGetFunc(gn unsafe.Pointer, objType C.int, id, data unsafe.Pointer, bufSize C.uint) C.int {
  641. nodesByUserPtrLock.RLock()
  642. node := nodesByUserPtr[uintptr(gn)]
  643. nodesByUserPtrLock.RUnlock()
  644. if node == nil {
  645. return -1
  646. }
  647. tmp, found := node.stateObjectGet(int(objType), *((*[2]uint64)(id)))
  648. if found && len(tmp) < int(bufSize) {
  649. if len(tmp) > 0 {
  650. C.memcpy(data, unsafe.Pointer(&(tmp[0])), C.ulong(len(tmp)))
  651. }
  652. return C.int(len(tmp))
  653. }
  654. return -1
  655. }
  656. //export goDNSResolverFunc
  657. func goDNSResolverFunc(gn unsafe.Pointer, dnsRecordTypes unsafe.Pointer, numDNSRecordTypes C.int, name unsafe.Pointer, requestID C.uintptr_t) {
  658. go func() {
  659. nodesByUserPtrLock.RLock()
  660. node := nodesByUserPtr[uintptr(gn)]
  661. nodesByUserPtrLock.RUnlock()
  662. if node == nil {
  663. return
  664. }
  665. recordTypes := C.GoBytes(dnsRecordTypes, numDNSRecordTypes)
  666. recordName := C.GoString((*C.char)(name))
  667. recordNameCStrCopy := C.CString(recordName)
  668. for _, rt := range recordTypes {
  669. switch rt {
  670. case C.ZT_DNS_RECORD_TXT:
  671. recs, _ := net.LookupTXT(recordName)
  672. for _, rec := range recs {
  673. if len(rec) > 0 {
  674. rnCS := C.CString(rec)
  675. C.ZT_Node_processDNSResult(unsafe.Pointer(node.zn), nil, requestID, recordNameCStrCopy, C.ZT_DNS_RECORD_TXT, unsafe.Pointer(rnCS), C.uint(len(rec)), 0)
  676. C.free(unsafe.Pointer(rnCS))
  677. }
  678. }
  679. }
  680. }
  681. C.ZT_Node_processDNSResult(unsafe.Pointer(node.zn), nil, requestID, recordNameCStrCopy, C.ZT_DNS_RECORD__END_OF_RESULTS, nil, 0, 0)
  682. C.free(unsafe.Pointer(recordNameCStrCopy))
  683. }()
  684. }
  685. //export goVirtualNetworkConfigFunc
  686. func goVirtualNetworkConfigFunc(gn, tapP unsafe.Pointer, nwid C.uint64_t, op C.int, conf unsafe.Pointer) {
  687. go func() {
  688. nodesByUserPtrLock.RLock()
  689. node := nodesByUserPtr[uintptr(gn)]
  690. nodesByUserPtrLock.RUnlock()
  691. if node == nil {
  692. return
  693. }
  694. node.networksLock.RLock()
  695. network := node.networks[uint64(nwid)]
  696. node.networksLock.RUnlock()
  697. if network != nil {
  698. ncc := (*C.ZT_VirtualNetworkConfig)(conf)
  699. if network.networkConfigRevision() > uint64(ncc.netconfRevision) {
  700. return
  701. }
  702. var nc NetworkConfig
  703. nc.ID = NetworkID(ncc.nwid)
  704. nc.MAC = MAC(ncc.mac)
  705. nc.Name = C.GoString(&ncc.name[0])
  706. nc.Status = int(ncc.status)
  707. nc.Type = int(ncc._type)
  708. nc.MTU = int(ncc.mtu)
  709. nc.Bridge = (ncc.bridge != 0)
  710. nc.BroadcastEnabled = (ncc.broadcastEnabled != 0)
  711. nc.NetconfRevision = uint64(ncc.netconfRevision)
  712. for i := 0; i < int(ncc.assignedAddressCount); i++ {
  713. a := sockaddrStorageToIPNet(&ncc.assignedAddresses[i])
  714. if a != nil {
  715. nc.AssignedAddresses = append(nc.AssignedAddresses, *a)
  716. }
  717. }
  718. for i := 0; i < int(ncc.routeCount); i++ {
  719. tgt := sockaddrStorageToIPNet(&ncc.routes[i].target)
  720. viaN := sockaddrStorageToIPNet(&ncc.routes[i].via)
  721. var via net.IP
  722. if viaN != nil {
  723. via = viaN.IP
  724. }
  725. if tgt != nil {
  726. nc.Routes = append(nc.Routes, Route{
  727. Target: *tgt,
  728. Via: via,
  729. Flags: uint16(ncc.routes[i].flags),
  730. Metric: uint16(ncc.routes[i].metric),
  731. })
  732. }
  733. }
  734. network.updateConfig(&nc, nil)
  735. }
  736. }()
  737. }
  738. //export goZtEvent
  739. func goZtEvent(gn unsafe.Pointer, eventType C.int, data unsafe.Pointer) {
  740. go func() {
  741. nodesByUserPtrLock.RLock()
  742. node := nodesByUserPtr[uintptr(gn)]
  743. nodesByUserPtrLock.RUnlock()
  744. if node == nil {
  745. return
  746. }
  747. switch eventType {
  748. case C.ZT_EVENT_OFFLINE:
  749. atomic.StoreUint32(&node.online, 0)
  750. case C.ZT_EVENT_ONLINE:
  751. atomic.StoreUint32(&node.online, 1)
  752. case C.ZT_EVENT_TRACE:
  753. node.handleTrace(C.GoString((*C.char)(data)))
  754. case C.ZT_EVENT_USER_MESSAGE:
  755. um := (*C.ZT_UserMessage)(data)
  756. node.handleUserMessage(uint64(um.origin), uint64(um.typeId), C.GoBytes(um.data, C.int(um.length)))
  757. case C.ZT_EVENT_REMOTE_TRACE:
  758. rt := (*C.ZT_RemoteTrace)(data)
  759. node.handleRemoteTrace(uint64(rt.origin), C.GoBytes(unsafe.Pointer(rt.data), C.int(rt.len)))
  760. }
  761. }()
  762. }
  763. //////////////////////////////////////////////////////////////////////////////
  764. // nativeTap is a Tap implementation that wraps a native C++ interface to a system tun/tap device
  765. type nativeTap struct {
  766. tap unsafe.Pointer
  767. networkStatus uint32
  768. enabled uint32
  769. multicastGroupHandlers []func(bool, *MulticastGroup)
  770. multicastGroupHandlersLock sync.Mutex
  771. }
  772. // Type returns a human-readable description of this tap implementation
  773. func (t *nativeTap) Type() string {
  774. return "native"
  775. }
  776. // Error gets this tap device's error status
  777. func (t *nativeTap) Error() (int, string) {
  778. return 0, ""
  779. }
  780. // SetEnabled sets this tap's enabled state
  781. func (t *nativeTap) SetEnabled(enabled bool) {
  782. if enabled && atomic.SwapUint32(&t.enabled, 1) == 0 {
  783. C.ZT_GoTap_setEnabled(t.tap, 1)
  784. } else if !enabled && atomic.SwapUint32(&t.enabled, 0) == 1 {
  785. C.ZT_GoTap_setEnabled(t.tap, 0)
  786. }
  787. }
  788. // Enabled returns true if this tap is currently processing packets
  789. func (t *nativeTap) Enabled() bool {
  790. return atomic.LoadUint32(&t.enabled) != 0
  791. }
  792. // AddIP adds an IP address (with netmask) to this tap
  793. func (t *nativeTap) AddIP(ip *net.IPNet) error {
  794. bits, _ := ip.Mask.Size()
  795. if len(ip.IP) == 16 {
  796. if bits > 128 || bits < 0 {
  797. return ErrInvalidParameter
  798. }
  799. C.ZT_GoTap_addIp(t.tap, C.int(AFInet6), unsafe.Pointer(&ip.IP[0]), C.int(bits))
  800. } else if len(ip.IP) == 4 {
  801. if bits > 32 || bits < 0 {
  802. return ErrInvalidParameter
  803. }
  804. C.ZT_GoTap_addIp(t.tap, C.int(AFInet), unsafe.Pointer(&ip.IP[0]), C.int(bits))
  805. }
  806. return ErrInvalidParameter
  807. }
  808. // RemoveIP removes this IP address (with netmask) from this tap
  809. func (t *nativeTap) RemoveIP(ip *net.IPNet) error {
  810. bits, _ := ip.Mask.Size()
  811. if len(ip.IP) == 16 {
  812. if bits > 128 || bits < 0 {
  813. return ErrInvalidParameter
  814. }
  815. C.ZT_GoTap_removeIp(t.tap, C.int(AFInet6), unsafe.Pointer(&ip.IP[0]), C.int(bits))
  816. return nil
  817. }
  818. if len(ip.IP) == 4 {
  819. if bits > 32 || bits < 0 {
  820. return ErrInvalidParameter
  821. }
  822. C.ZT_GoTap_removeIp(t.tap, C.int(AFInet), unsafe.Pointer(&ip.IP[0]), C.int(bits))
  823. return nil
  824. }
  825. return ErrInvalidParameter
  826. }
  827. // IPs returns IPs currently assigned to this tap (including externally or system-assigned IPs)
  828. func (t *nativeTap) IPs() (ips []net.IPNet, err error) {
  829. defer func() {
  830. e := recover()
  831. if e != nil {
  832. err = fmt.Errorf("%v", e)
  833. }
  834. }()
  835. var ipbuf [16384]byte
  836. count := int(C.ZT_GoTap_ips(t.tap, unsafe.Pointer(&ipbuf[0]), 16384))
  837. ipptr := 0
  838. for i := 0; i < count; i++ {
  839. af := int(ipbuf[ipptr])
  840. ipptr++
  841. switch af {
  842. case AFInet:
  843. var ip [4]byte
  844. for j := 0; j < 4; j++ {
  845. ip[j] = ipbuf[ipptr]
  846. ipptr++
  847. }
  848. bits := ipbuf[ipptr]
  849. ipptr++
  850. ips = append(ips, net.IPNet{IP: net.IP(ip[:]), Mask: net.CIDRMask(int(bits), 32)})
  851. case AFInet6:
  852. var ip [16]byte
  853. for j := 0; j < 16; j++ {
  854. ip[j] = ipbuf[ipptr]
  855. ipptr++
  856. }
  857. bits := ipbuf[ipptr]
  858. ipptr++
  859. ips = append(ips, net.IPNet{IP: net.IP(ip[:]), Mask: net.CIDRMask(int(bits), 128)})
  860. }
  861. }
  862. return
  863. }
  864. // DeviceName gets this tap's OS-specific device name
  865. func (t *nativeTap) DeviceName() string {
  866. var dn [256]byte
  867. C.ZT_GoTap_deviceName(t.tap, (*C.char)(unsafe.Pointer(&dn[0])))
  868. for i, b := range dn {
  869. if b == 0 {
  870. return string(dn[0:i])
  871. }
  872. }
  873. return ""
  874. }
  875. // AddMulticastGroupChangeHandler adds a function to be called when the tap subscribes or unsubscribes to a multicast group.
  876. func (t *nativeTap) AddMulticastGroupChangeHandler(handler func(bool, *MulticastGroup)) {
  877. t.multicastGroupHandlersLock.Lock()
  878. t.multicastGroupHandlers = append(t.multicastGroupHandlers, handler)
  879. t.multicastGroupHandlersLock.Unlock()
  880. }
  881. // AddRoute adds or updates a managed route on this tap's interface
  882. func (t *nativeTap) AddRoute(r *Route) error {
  883. rc := 0
  884. if r != nil {
  885. if len(r.Target.IP) == 4 {
  886. mask, _ := r.Target.Mask.Size()
  887. if len(r.Via) == 4 {
  888. rc = int(C.ZT_GoTap_addRoute(t.tap, AFInet, unsafe.Pointer(&r.Target.IP[0]), C.int(mask), AFInet, unsafe.Pointer(&r.Via[0]), C.uint(r.Metric)))
  889. } else {
  890. rc = int(C.ZT_GoTap_addRoute(t.tap, AFInet, unsafe.Pointer(&r.Target.IP[0]), C.int(mask), 0, nil, C.uint(r.Metric)))
  891. }
  892. } else if len(r.Target.IP) == 16 {
  893. mask, _ := r.Target.Mask.Size()
  894. if len(r.Via) == 4 {
  895. rc = int(C.ZT_GoTap_addRoute(t.tap, AFInet6, unsafe.Pointer(&r.Target.IP[0]), C.int(mask), AFInet6, unsafe.Pointer(&r.Via[0]), C.uint(r.Metric)))
  896. } else {
  897. rc = int(C.ZT_GoTap_addRoute(t.tap, AFInet6, unsafe.Pointer(&r.Target.IP[0]), C.int(mask), 0, nil, C.uint(r.Metric)))
  898. }
  899. }
  900. }
  901. if rc != 0 {
  902. return fmt.Errorf("tap device error adding route: %d", rc)
  903. }
  904. return nil
  905. }
  906. // RemoveRoute removes a managed route on this tap's interface
  907. func (t *nativeTap) RemoveRoute(r *Route) error {
  908. rc := 0
  909. if r != nil {
  910. if len(r.Target.IP) == 4 {
  911. mask, _ := r.Target.Mask.Size()
  912. if len(r.Via) == 4 {
  913. rc = int(C.ZT_GoTap_removeRoute(t.tap, AFInet, unsafe.Pointer(&r.Target.IP[0]), C.int(mask), AFInet, unsafe.Pointer(&r.Via[0]), C.uint(r.Metric)))
  914. } else {
  915. rc = int(C.ZT_GoTap_removeRoute(t.tap, AFInet, unsafe.Pointer(&r.Target.IP[0]), C.int(mask), 0, nil, C.uint(r.Metric)))
  916. }
  917. } else if len(r.Target.IP) == 16 {
  918. mask, _ := r.Target.Mask.Size()
  919. if len(r.Via) == 4 {
  920. rc = int(C.ZT_GoTap_removeRoute(t.tap, AFInet6, unsafe.Pointer(&r.Target.IP[0]), C.int(mask), AFInet6, unsafe.Pointer(&r.Via[0]), C.uint(r.Metric)))
  921. } else {
  922. rc = int(C.ZT_GoTap_removeRoute(t.tap, AFInet6, unsafe.Pointer(&r.Target.IP[0]), C.int(mask), 0, nil, C.uint(r.Metric)))
  923. }
  924. }
  925. }
  926. if rc != 0 {
  927. return fmt.Errorf("tap device error removing route: %d", rc)
  928. }
  929. return nil
  930. }
  931. //////////////////////////////////////////////////////////////////////////////
  932. func handleTapMulticastGroupChange(gn unsafe.Pointer, nwid, mac C.uint64_t, adi C.uint32_t, added bool) {
  933. go func() {
  934. nodesByUserPtrLock.RLock()
  935. node := nodesByUserPtr[uintptr(gn)]
  936. nodesByUserPtrLock.RUnlock()
  937. if node == nil {
  938. return
  939. }
  940. node.networksLock.RLock()
  941. network := node.networks[uint64(nwid)]
  942. node.networksLock.RUnlock()
  943. if network != nil {
  944. tap, _ := network.tap.(*nativeTap)
  945. if tap != nil {
  946. mg := &MulticastGroup{MAC: MAC(mac), ADI: uint32(adi)}
  947. tap.multicastGroupHandlersLock.Lock()
  948. defer tap.multicastGroupHandlersLock.Unlock()
  949. for _, h := range tap.multicastGroupHandlers {
  950. h(added, mg)
  951. }
  952. }
  953. }
  954. }()
  955. }
  956. //export goHandleTapAddedMulticastGroup
  957. func goHandleTapAddedMulticastGroup(gn, tapP unsafe.Pointer, nwid, mac C.uint64_t, adi C.uint32_t) {
  958. handleTapMulticastGroupChange(gn, nwid, mac, adi, true)
  959. }
  960. //export goHandleTapRemovedMulticastGroup
  961. func goHandleTapRemovedMulticastGroup(gn, tapP unsafe.Pointer, nwid, mac C.uint64_t, adi C.uint32_t) {
  962. handleTapMulticastGroupChange(gn, nwid, mac, adi, false)
  963. }