nativetap.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Copyright (C)2013-2020 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: 2025-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 the C++ EthernetTap and its implementations.
  14. package zerotier
  15. //#include "../../serviceiocore/GoGlue.h"
  16. import "C"
  17. import (
  18. "fmt"
  19. "net"
  20. "sync"
  21. "sync/atomic"
  22. "syscall"
  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 *InetAddress) error {
  57. if len(ip.IP) == 16 {
  58. if ip.Port > 128 || ip.Port < 0 {
  59. return ErrInvalidParameter
  60. }
  61. C.ZT_GoTap_addIp(t.tap, C.int(syscall.AF_INET6), unsafe.Pointer(&ip.IP[0]), C.int(ip.Port))
  62. } else if len(ip.IP) == 4 {
  63. if ip.Port > 32 || ip.Port < 0 {
  64. return ErrInvalidParameter
  65. }
  66. C.ZT_GoTap_addIp(t.tap, C.int(syscall.AF_INET), unsafe.Pointer(&ip.IP[0]), C.int(ip.Port))
  67. }
  68. return ErrInvalidParameter
  69. }
  70. // RemoveIP removes this IP address (with netmask) from this tap
  71. func (t *nativeTap) RemoveIP(ip *InetAddress) error {
  72. if len(ip.IP) == 16 {
  73. if ip.Port > 128 || ip.Port < 0 {
  74. return ErrInvalidParameter
  75. }
  76. C.ZT_GoTap_removeIp(t.tap, C.int(syscall.AF_INET6), unsafe.Pointer(&ip.IP[0]), C.int(ip.Port))
  77. return nil
  78. }
  79. if len(ip.IP) == 4 {
  80. if ip.Port > 32 || ip.Port < 0 {
  81. return ErrInvalidParameter
  82. }
  83. C.ZT_GoTap_removeIp(t.tap, C.int(syscall.AF_INET), unsafe.Pointer(&ip.IP[0]), C.int(ip.Port))
  84. return nil
  85. }
  86. return ErrInvalidParameter
  87. }
  88. // IPs returns IPs currently assigned to this tap (including externally or system-assigned IPs)
  89. func (t *nativeTap) IPs() (ips []net.IPNet, err error) {
  90. defer func() {
  91. e := recover()
  92. if e != nil {
  93. err = fmt.Errorf("%v", e)
  94. }
  95. }()
  96. var ipbuf [16384]byte
  97. count := int(C.ZT_GoTap_ips(t.tap, unsafe.Pointer(&ipbuf[0]), 16384))
  98. ipptr := 0
  99. for i := 0; i < count; i++ {
  100. af := int(ipbuf[ipptr])
  101. ipptr++
  102. switch af {
  103. case syscall.AF_INET:
  104. var ip [4]byte
  105. for j := 0; j < 4; j++ {
  106. ip[j] = ipbuf[ipptr]
  107. ipptr++
  108. }
  109. bits := ipbuf[ipptr]
  110. ipptr++
  111. ips = append(ips, net.IPNet{IP: net.IP(ip[:]), Mask: net.CIDRMask(int(bits), 32)})
  112. case syscall.AF_INET6:
  113. var ip [16]byte
  114. for j := 0; j < 16; j++ {
  115. ip[j] = ipbuf[ipptr]
  116. ipptr++
  117. }
  118. bits := ipbuf[ipptr]
  119. ipptr++
  120. ips = append(ips, net.IPNet{IP: net.IP(ip[:]), Mask: net.CIDRMask(int(bits), 128)})
  121. }
  122. }
  123. return
  124. }
  125. // DeviceName gets this tap's OS-specific device name
  126. func (t *nativeTap) DeviceName() string {
  127. var dn [256]byte
  128. C.ZT_GoTap_deviceName(t.tap, (*C.char)(unsafe.Pointer(&dn[0])))
  129. for i, b := range dn {
  130. if b == 0 {
  131. return string(dn[0:i])
  132. }
  133. }
  134. return ""
  135. }
  136. // AddMulticastGroupChangeHandler adds a function to be called when the tap subscribes or unsubscribes to a multicast group.
  137. func (t *nativeTap) AddMulticastGroupChangeHandler(handler func(bool, *MulticastGroup)) {
  138. t.multicastGroupHandlersLock.Lock()
  139. t.multicastGroupHandlers = append(t.multicastGroupHandlers, handler)
  140. t.multicastGroupHandlersLock.Unlock()
  141. }
  142. func handleTapMulticastGroupChange(gn unsafe.Pointer, nwid, mac C.uint64_t, adi C.uint32_t, added bool) {
  143. node := cNodeRefs[uintptr(gn)]
  144. if node == nil {
  145. return
  146. }
  147. node.networksLock.RLock()
  148. network := node.networks[NetworkID(nwid)]
  149. node.networksLock.RUnlock()
  150. if network == nil {
  151. return
  152. }
  153. node.runWaitGroup.Add(1)
  154. go func() {
  155. defer node.runWaitGroup.Done()
  156. tap, _ := network.tap.(*nativeTap)
  157. if tap != nil {
  158. mg := &MulticastGroup{MAC: MAC(mac), ADI: uint32(adi)}
  159. tap.multicastGroupHandlersLock.Lock()
  160. defer tap.multicastGroupHandlersLock.Unlock()
  161. for _, h := range tap.multicastGroupHandlers {
  162. h(added, mg)
  163. }
  164. }
  165. }()
  166. }
  167. //export goHandleTapAddedMulticastGroup
  168. func goHandleTapAddedMulticastGroup(gn, _ unsafe.Pointer, nwid, mac C.uint64_t, adi C.uint32_t) {
  169. handleTapMulticastGroupChange(gn, nwid, mac, adi, true)
  170. }
  171. //export goHandleTapRemovedMulticastGroup
  172. func goHandleTapRemovedMulticastGroup(gn, _ unsafe.Pointer, nwid, mac C.uint64_t, adi C.uint32_t) {
  173. handleTapMulticastGroupChange(gn, nwid, mac, adi, false)
  174. }