nativetap.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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: 2024-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. //#cgo CFLAGS: -O3
  16. //#include "../../native/GoGlue.h"
  17. import "C"
  18. import (
  19. "fmt"
  20. "net"
  21. "sync"
  22. "sync/atomic"
  23. "syscall"
  24. "unsafe"
  25. )
  26. // nativeTap is a Tap implementation that wraps a native C++ interface to a system tun/tap device
  27. type nativeTap struct {
  28. tap unsafe.Pointer
  29. networkStatus uint32
  30. enabled uint32
  31. multicastGroupHandlers []func(bool, *MulticastGroup)
  32. multicastGroupHandlersLock sync.Mutex
  33. }
  34. // Close is a no-op for the native tap because GoGlue does this when networks are left
  35. func (t *nativeTap) Close() {}
  36. // Type returns a human-readable description of this tap implementation
  37. func (t *nativeTap) Type() string {
  38. return "native"
  39. }
  40. // Error gets this tap device's error status
  41. func (t *nativeTap) Error() (int, string) {
  42. return 0, ""
  43. }
  44. // SetEnabled sets this tap's enabled state
  45. func (t *nativeTap) SetEnabled(enabled bool) {
  46. if enabled && atomic.SwapUint32(&t.enabled, 1) == 0 {
  47. C.ZT_GoTap_setEnabled(t.tap, 1)
  48. } else if !enabled && atomic.SwapUint32(&t.enabled, 0) == 1 {
  49. C.ZT_GoTap_setEnabled(t.tap, 0)
  50. }
  51. }
  52. // Enabled returns true if this tap is currently processing packets
  53. func (t *nativeTap) Enabled() bool {
  54. return atomic.LoadUint32(&t.enabled) != 0
  55. }
  56. // AddIP adds an IP address (with netmask) to this tap
  57. func (t *nativeTap) AddIP(ip *net.IPNet) error {
  58. bits, _ := ip.Mask.Size()
  59. if len(ip.IP) == 16 {
  60. if bits > 128 || bits < 0 {
  61. return ErrInvalidParameter
  62. }
  63. C.ZT_GoTap_addIp(t.tap, C.int(syscall.AF_INET6), unsafe.Pointer(&ip.IP[0]), C.int(bits))
  64. } else if len(ip.IP) == 4 {
  65. if bits > 32 || bits < 0 {
  66. return ErrInvalidParameter
  67. }
  68. C.ZT_GoTap_addIp(t.tap, C.int(syscall.AF_INET), unsafe.Pointer(&ip.IP[0]), C.int(bits))
  69. }
  70. return ErrInvalidParameter
  71. }
  72. // RemoveIP removes this IP address (with netmask) from this tap
  73. func (t *nativeTap) RemoveIP(ip *net.IPNet) error {
  74. bits, _ := ip.Mask.Size()
  75. if len(ip.IP) == 16 {
  76. if bits > 128 || bits < 0 {
  77. return ErrInvalidParameter
  78. }
  79. C.ZT_GoTap_removeIp(t.tap, C.int(syscall.AF_INET6), unsafe.Pointer(&ip.IP[0]), C.int(bits))
  80. return nil
  81. }
  82. if len(ip.IP) == 4 {
  83. if bits > 32 || bits < 0 {
  84. return ErrInvalidParameter
  85. }
  86. C.ZT_GoTap_removeIp(t.tap, C.int(syscall.AF_INET), unsafe.Pointer(&ip.IP[0]), C.int(bits))
  87. return nil
  88. }
  89. return ErrInvalidParameter
  90. }
  91. // IPs returns IPs currently assigned to this tap (including externally or system-assigned IPs)
  92. func (t *nativeTap) IPs() (ips []net.IPNet, err error) {
  93. defer func() {
  94. e := recover()
  95. if e != nil {
  96. err = fmt.Errorf("%v", e)
  97. }
  98. }()
  99. var ipbuf [16384]byte
  100. count := int(C.ZT_GoTap_ips(t.tap, unsafe.Pointer(&ipbuf[0]), 16384))
  101. ipptr := 0
  102. for i := 0; i < count; i++ {
  103. af := int(ipbuf[ipptr])
  104. ipptr++
  105. switch af {
  106. case syscall.AF_INET:
  107. var ip [4]byte
  108. for j := 0; j < 4; j++ {
  109. ip[j] = ipbuf[ipptr]
  110. ipptr++
  111. }
  112. bits := ipbuf[ipptr]
  113. ipptr++
  114. ips = append(ips, net.IPNet{IP: net.IP(ip[:]), Mask: net.CIDRMask(int(bits), 32)})
  115. case syscall.AF_INET6:
  116. var ip [16]byte
  117. for j := 0; j < 16; j++ {
  118. ip[j] = ipbuf[ipptr]
  119. ipptr++
  120. }
  121. bits := ipbuf[ipptr]
  122. ipptr++
  123. ips = append(ips, net.IPNet{IP: net.IP(ip[:]), Mask: net.CIDRMask(int(bits), 128)})
  124. }
  125. }
  126. return
  127. }
  128. // DeviceName gets this tap's OS-specific device name
  129. func (t *nativeTap) DeviceName() string {
  130. var dn [256]byte
  131. C.ZT_GoTap_deviceName(t.tap, (*C.char)(unsafe.Pointer(&dn[0])))
  132. for i, b := range dn {
  133. if b == 0 {
  134. return string(dn[0:i])
  135. }
  136. }
  137. return ""
  138. }
  139. // AddMulticastGroupChangeHandler adds a function to be called when the tap subscribes or unsubscribes to a multicast group.
  140. func (t *nativeTap) AddMulticastGroupChangeHandler(handler func(bool, *MulticastGroup)) {
  141. t.multicastGroupHandlersLock.Lock()
  142. t.multicastGroupHandlers = append(t.multicastGroupHandlers, handler)
  143. t.multicastGroupHandlersLock.Unlock()
  144. }
  145. func handleTapMulticastGroupChange(gn unsafe.Pointer, nwid, mac C.uint64_t, adi C.uint32_t, added bool) {
  146. go func() {
  147. nodesByUserPtrLock.RLock()
  148. node := nodesByUserPtr[uintptr(gn)]
  149. nodesByUserPtrLock.RUnlock()
  150. if node == nil {
  151. return
  152. }
  153. node.networksLock.RLock()
  154. network := node.networks[NetworkID(nwid)]
  155. node.networksLock.RUnlock()
  156. if network != nil {
  157. tap, _ := network.tap.(*nativeTap)
  158. if tap != nil {
  159. mg := &MulticastGroup{MAC: MAC(mac), ADI: uint32(adi)}
  160. tap.multicastGroupHandlersLock.Lock()
  161. defer tap.multicastGroupHandlersLock.Unlock()
  162. for _, h := range tap.multicastGroupHandlers {
  163. h(added, mg)
  164. }
  165. }
  166. }
  167. }()
  168. }
  169. //export goHandleTapAddedMulticastGroup
  170. func goHandleTapAddedMulticastGroup(gn, _ unsafe.Pointer, nwid, mac C.uint64_t, adi C.uint32_t) {
  171. handleTapMulticastGroupChange(gn, nwid, mac, adi, true)
  172. }
  173. //export goHandleTapRemovedMulticastGroup
  174. func goHandleTapRemovedMulticastGroup(gn, _ unsafe.Pointer, nwid, mac C.uint64_t, adi C.uint32_t) {
  175. handleTapMulticastGroupChange(gn, nwid, mac, adi, false)
  176. }