nativetap.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. // This wraps EthernetTap from osdep/
  14. package zerotier
  15. //#cgo CFLAGS: -O3
  16. //#include "../../native/GoGlue.h"
  17. import "C"
  18. import (
  19. "fmt"
  20. "net"
  21. "sync"
  22. "sync/atomic"
  23. "unsafe"
  24. )
  25. // nativeTap is a Tap implementation that wraps a native C++ interface to a system tun/tap device
  26. type nativeTap struct {
  27. tap unsafe.Pointer
  28. networkStatus uint32
  29. enabled uint32
  30. multicastGroupHandlers []func(bool, *MulticastGroup)
  31. multicastGroupHandlersLock sync.Mutex
  32. }
  33. // Close is a no-op for the native tap because GoGlue does this when networks are left
  34. func (t *nativeTap) Close() {}
  35. // Type returns a human-readable description of this tap implementation
  36. func (t *nativeTap) Type() string {
  37. return "native"
  38. }
  39. // Error gets this tap device's error status
  40. func (t *nativeTap) Error() (int, string) {
  41. return 0, ""
  42. }
  43. // SetEnabled sets this tap's enabled state
  44. func (t *nativeTap) SetEnabled(enabled bool) {
  45. if enabled && atomic.SwapUint32(&t.enabled, 1) == 0 {
  46. C.ZT_GoTap_setEnabled(t.tap, 1)
  47. } else if !enabled && atomic.SwapUint32(&t.enabled, 0) == 1 {
  48. C.ZT_GoTap_setEnabled(t.tap, 0)
  49. }
  50. }
  51. // Enabled returns true if this tap is currently processing packets
  52. func (t *nativeTap) Enabled() bool {
  53. return atomic.LoadUint32(&t.enabled) != 0
  54. }
  55. // AddIP adds an IP address (with netmask) to this tap
  56. func (t *nativeTap) AddIP(ip *net.IPNet) error {
  57. bits, _ := ip.Mask.Size()
  58. if len(ip.IP) == 16 {
  59. if bits > 128 || bits < 0 {
  60. return ErrInvalidParameter
  61. }
  62. C.ZT_GoTap_addIp(t.tap, C.int(AFInet6), unsafe.Pointer(&ip.IP[0]), C.int(bits))
  63. } else if len(ip.IP) == 4 {
  64. if bits > 32 || bits < 0 {
  65. return ErrInvalidParameter
  66. }
  67. C.ZT_GoTap_addIp(t.tap, C.int(AFInet), unsafe.Pointer(&ip.IP[0]), C.int(bits))
  68. }
  69. return ErrInvalidParameter
  70. }
  71. // RemoveIP removes this IP address (with netmask) from this tap
  72. func (t *nativeTap) RemoveIP(ip *net.IPNet) error {
  73. bits, _ := ip.Mask.Size()
  74. if len(ip.IP) == 16 {
  75. if bits > 128 || bits < 0 {
  76. return ErrInvalidParameter
  77. }
  78. C.ZT_GoTap_removeIp(t.tap, C.int(AFInet6), unsafe.Pointer(&ip.IP[0]), C.int(bits))
  79. return nil
  80. }
  81. if len(ip.IP) == 4 {
  82. if bits > 32 || bits < 0 {
  83. return ErrInvalidParameter
  84. }
  85. C.ZT_GoTap_removeIp(t.tap, C.int(AFInet), unsafe.Pointer(&ip.IP[0]), C.int(bits))
  86. return nil
  87. }
  88. return ErrInvalidParameter
  89. }
  90. // IPs returns IPs currently assigned to this tap (including externally or system-assigned IPs)
  91. func (t *nativeTap) IPs() (ips []net.IPNet, err error) {
  92. defer func() {
  93. e := recover()
  94. if e != nil {
  95. err = fmt.Errorf("%v", e)
  96. }
  97. }()
  98. var ipbuf [16384]byte
  99. count := int(C.ZT_GoTap_ips(t.tap, unsafe.Pointer(&ipbuf[0]), 16384))
  100. ipptr := 0
  101. for i := 0; i < count; i++ {
  102. af := int(ipbuf[ipptr])
  103. ipptr++
  104. switch af {
  105. case AFInet:
  106. var ip [4]byte
  107. for j := 0; j < 4; j++ {
  108. ip[j] = ipbuf[ipptr]
  109. ipptr++
  110. }
  111. bits := ipbuf[ipptr]
  112. ipptr++
  113. ips = append(ips, net.IPNet{IP: net.IP(ip[:]), Mask: net.CIDRMask(int(bits), 32)})
  114. case AFInet6:
  115. var ip [16]byte
  116. for j := 0; j < 16; j++ {
  117. ip[j] = ipbuf[ipptr]
  118. ipptr++
  119. }
  120. bits := ipbuf[ipptr]
  121. ipptr++
  122. ips = append(ips, net.IPNet{IP: net.IP(ip[:]), Mask: net.CIDRMask(int(bits), 128)})
  123. }
  124. }
  125. return
  126. }
  127. // DeviceName gets this tap's OS-specific device name
  128. func (t *nativeTap) DeviceName() string {
  129. var dn [256]byte
  130. C.ZT_GoTap_deviceName(t.tap, (*C.char)(unsafe.Pointer(&dn[0])))
  131. for i, b := range dn {
  132. if b == 0 {
  133. return string(dn[0:i])
  134. }
  135. }
  136. return ""
  137. }
  138. // AddMulticastGroupChangeHandler adds a function to be called when the tap subscribes or unsubscribes to a multicast group.
  139. func (t *nativeTap) AddMulticastGroupChangeHandler(handler func(bool, *MulticastGroup)) {
  140. t.multicastGroupHandlersLock.Lock()
  141. t.multicastGroupHandlers = append(t.multicastGroupHandlers, handler)
  142. t.multicastGroupHandlersLock.Unlock()
  143. }
  144. // AddRoute adds or updates a managed route on this tap's interface
  145. func (t *nativeTap) AddRoute(r *Route) error {
  146. rc := 0
  147. if r != nil {
  148. var via []byte
  149. if r.Via != nil {
  150. via = *r.Via
  151. }
  152. if len(r.Target.IP) == 4 {
  153. mask, _ := r.Target.Mask.Size()
  154. if len(via) == 4 {
  155. rc = int(C.ZT_GoTap_addRoute(t.tap, AFInet, unsafe.Pointer(&r.Target.IP[0]), C.int(mask), AFInet, unsafe.Pointer(&via[0]), C.uint(r.Metric)))
  156. } else {
  157. rc = int(C.ZT_GoTap_addRoute(t.tap, AFInet, unsafe.Pointer(&r.Target.IP[0]), C.int(mask), 0, nil, C.uint(r.Metric)))
  158. }
  159. } else if len(r.Target.IP) == 16 {
  160. mask, _ := r.Target.Mask.Size()
  161. if len(via) == 16 {
  162. rc = int(C.ZT_GoTap_addRoute(t.tap, AFInet6, unsafe.Pointer(&r.Target.IP[0]), C.int(mask), AFInet6, unsafe.Pointer(&via[0]), C.uint(r.Metric)))
  163. } else {
  164. rc = int(C.ZT_GoTap_addRoute(t.tap, AFInet6, unsafe.Pointer(&r.Target.IP[0]), C.int(mask), 0, nil, C.uint(r.Metric)))
  165. }
  166. }
  167. }
  168. if rc != 0 {
  169. return fmt.Errorf("tap device error adding route: %d", rc)
  170. }
  171. return nil
  172. }
  173. // RemoveRoute removes a managed route on this tap's interface
  174. func (t *nativeTap) RemoveRoute(r *Route) error {
  175. rc := 0
  176. if r != nil {
  177. var via []byte
  178. if r.Via != nil {
  179. via = *r.Via
  180. }
  181. if len(r.Target.IP) == 4 {
  182. mask, _ := r.Target.Mask.Size()
  183. if len(via) == 4 {
  184. rc = int(C.ZT_GoTap_removeRoute(t.tap, AFInet, unsafe.Pointer(&r.Target.IP[0]), C.int(mask), AFInet, unsafe.Pointer(&(via[0])), C.uint(r.Metric)))
  185. } else {
  186. rc = int(C.ZT_GoTap_removeRoute(t.tap, AFInet, unsafe.Pointer(&r.Target.IP[0]), C.int(mask), 0, nil, C.uint(r.Metric)))
  187. }
  188. } else if len(r.Target.IP) == 16 {
  189. mask, _ := r.Target.Mask.Size()
  190. if len(via) == 16 {
  191. rc = int(C.ZT_GoTap_removeRoute(t.tap, AFInet6, unsafe.Pointer(&r.Target.IP[0]), C.int(mask), AFInet6, unsafe.Pointer(&via[0]), C.uint(r.Metric)))
  192. } else {
  193. rc = int(C.ZT_GoTap_removeRoute(t.tap, AFInet6, unsafe.Pointer(&r.Target.IP[0]), C.int(mask), 0, nil, C.uint(r.Metric)))
  194. }
  195. }
  196. }
  197. if rc != 0 {
  198. return fmt.Errorf("tap device error removing route: %d", rc)
  199. }
  200. return nil
  201. }
  202. func handleTapMulticastGroupChange(gn unsafe.Pointer, nwid, mac C.uint64_t, adi C.uint32_t, added bool) {
  203. go func() {
  204. nodesByUserPtrLock.RLock()
  205. node := nodesByUserPtr[uintptr(gn)]
  206. nodesByUserPtrLock.RUnlock()
  207. if node == nil {
  208. return
  209. }
  210. node.networksLock.RLock()
  211. network := node.networks[NetworkID(nwid)]
  212. node.networksLock.RUnlock()
  213. if network != nil {
  214. tap, _ := network.tap.(*nativeTap)
  215. if tap != nil {
  216. mg := &MulticastGroup{MAC: MAC(mac), ADI: uint32(adi)}
  217. tap.multicastGroupHandlersLock.Lock()
  218. defer tap.multicastGroupHandlersLock.Unlock()
  219. for _, h := range tap.multicastGroupHandlers {
  220. h(added, mg)
  221. }
  222. }
  223. }
  224. }()
  225. }
  226. //export goHandleTapAddedMulticastGroup
  227. func goHandleTapAddedMulticastGroup(gn, _ unsafe.Pointer, nwid, mac C.uint64_t, adi C.uint32_t) {
  228. handleTapMulticastGroupChange(gn, nwid, mac, adi, true)
  229. }
  230. //export goHandleTapRemovedMulticastGroup
  231. func goHandleTapRemovedMulticastGroup(gn, _ unsafe.Pointer, nwid, mac C.uint64_t, adi C.uint32_t) {
  232. handleTapMulticastGroupChange(gn, nwid, mac, adi, false)
  233. }