tun_linux.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. // +build !android
  2. package nebula
  3. import (
  4. "fmt"
  5. "io"
  6. "net"
  7. "os"
  8. "strings"
  9. "unsafe"
  10. "github.com/vishvananda/netlink"
  11. "golang.org/x/sys/unix"
  12. )
  13. type Tun struct {
  14. io.ReadWriteCloser
  15. fd int
  16. Device string
  17. Cidr *net.IPNet
  18. MaxMTU int
  19. DefaultMTU int
  20. TXQueueLen int
  21. Routes []route
  22. UnsafeRoutes []route
  23. }
  24. type ifReq struct {
  25. Name [16]byte
  26. Flags uint16
  27. pad [8]byte
  28. }
  29. func ioctl(a1, a2, a3 uintptr) error {
  30. _, _, errno := unix.Syscall(unix.SYS_IOCTL, a1, a2, a3)
  31. if errno != 0 {
  32. return errno
  33. }
  34. return nil
  35. }
  36. /*
  37. func ipv4(addr string) (o [4]byte, err error) {
  38. ip := net.ParseIP(addr).To4()
  39. if ip == nil {
  40. err = fmt.Errorf("failed to parse addr %s", addr)
  41. return
  42. }
  43. for i, b := range ip {
  44. o[i] = b
  45. }
  46. return
  47. }
  48. */
  49. const (
  50. cIFF_TUN = 0x0001
  51. cIFF_NO_PI = 0x1000
  52. )
  53. type ifreqAddr struct {
  54. Name [16]byte
  55. Addr unix.RawSockaddrInet4
  56. pad [8]byte
  57. }
  58. type ifreqMTU struct {
  59. Name [16]byte
  60. MTU int32
  61. pad [8]byte
  62. }
  63. type ifreqQLEN struct {
  64. Name [16]byte
  65. Value int32
  66. pad [8]byte
  67. }
  68. func newTunFromFd(deviceFd int, cidr *net.IPNet, defaultMTU int, routes []route, unsafeRoutes []route, txQueueLen int) (ifce *Tun, err error) {
  69. file := os.NewFile(uintptr(deviceFd), "/dev/net/tun")
  70. ifce = &Tun{
  71. ReadWriteCloser: file,
  72. fd: int(file.Fd()),
  73. Device: "tun0",
  74. Cidr: cidr,
  75. DefaultMTU: defaultMTU,
  76. TXQueueLen: txQueueLen,
  77. Routes: routes,
  78. UnsafeRoutes: unsafeRoutes,
  79. }
  80. return
  81. }
  82. func newTun(deviceName string, cidr *net.IPNet, defaultMTU int, routes []route, unsafeRoutes []route, txQueueLen int) (ifce *Tun, err error) {
  83. fd, err := unix.Open("/dev/net/tun", os.O_RDWR, 0)
  84. if err != nil {
  85. return nil, err
  86. }
  87. var req ifReq
  88. req.Flags = uint16(cIFF_TUN | cIFF_NO_PI)
  89. copy(req.Name[:], deviceName)
  90. if err = ioctl(uintptr(fd), uintptr(unix.TUNSETIFF), uintptr(unsafe.Pointer(&req))); err != nil {
  91. return
  92. }
  93. name := strings.Trim(string(req.Name[:]), "\x00")
  94. file := os.NewFile(uintptr(fd), "/dev/net/tun")
  95. maxMTU := defaultMTU
  96. for _, r := range routes {
  97. if r.mtu > maxMTU {
  98. maxMTU = r.mtu
  99. }
  100. }
  101. ifce = &Tun{
  102. ReadWriteCloser: file,
  103. fd: int(file.Fd()),
  104. Device: name,
  105. Cidr: cidr,
  106. MaxMTU: maxMTU,
  107. DefaultMTU: defaultMTU,
  108. TXQueueLen: txQueueLen,
  109. Routes: routes,
  110. UnsafeRoutes: unsafeRoutes,
  111. }
  112. return
  113. }
  114. func (c *Tun) WriteRaw(b []byte) error {
  115. var nn int
  116. for {
  117. max := len(b)
  118. n, err := unix.Write(c.fd, b[nn:max])
  119. if n > 0 {
  120. nn += n
  121. }
  122. if nn == len(b) {
  123. return err
  124. }
  125. if err != nil {
  126. return err
  127. }
  128. if n == 0 {
  129. return io.ErrUnexpectedEOF
  130. }
  131. }
  132. }
  133. func (c Tun) deviceBytes() (o [16]byte) {
  134. for i, c := range c.Device {
  135. o[i] = byte(c)
  136. }
  137. return
  138. }
  139. func (c Tun) Activate() error {
  140. devName := c.deviceBytes()
  141. var addr, mask [4]byte
  142. copy(addr[:], c.Cidr.IP.To4())
  143. copy(mask[:], c.Cidr.Mask)
  144. s, err := unix.Socket(
  145. unix.AF_INET,
  146. unix.SOCK_DGRAM,
  147. unix.IPPROTO_IP,
  148. )
  149. if err != nil {
  150. return err
  151. }
  152. fd := uintptr(s)
  153. ifra := ifreqAddr{
  154. Name: devName,
  155. Addr: unix.RawSockaddrInet4{
  156. Family: unix.AF_INET,
  157. Addr: addr,
  158. },
  159. }
  160. // Set the device ip address
  161. if err = ioctl(fd, unix.SIOCSIFADDR, uintptr(unsafe.Pointer(&ifra))); err != nil {
  162. return fmt.Errorf("failed to set tun address: %s", err)
  163. }
  164. // Set the device network
  165. ifra.Addr.Addr = mask
  166. if err = ioctl(fd, unix.SIOCSIFNETMASK, uintptr(unsafe.Pointer(&ifra))); err != nil {
  167. return fmt.Errorf("failed to set tun netmask: %s", err)
  168. }
  169. // Set the device name
  170. ifrf := ifReq{Name: devName}
  171. if err = ioctl(fd, unix.SIOCGIFFLAGS, uintptr(unsafe.Pointer(&ifrf))); err != nil {
  172. return fmt.Errorf("failed to set tun device name: %s", err)
  173. }
  174. // Set the MTU on the device
  175. ifm := ifreqMTU{Name: devName, MTU: int32(c.MaxMTU)}
  176. if err = ioctl(fd, unix.SIOCSIFMTU, uintptr(unsafe.Pointer(&ifm))); err != nil {
  177. // This is currently a non fatal condition because the route table must have the MTU set appropriately as well
  178. l.WithError(err).Error("Failed to set tun mtu")
  179. }
  180. // Set the transmit queue length
  181. ifrq := ifreqQLEN{Name: devName, Value: int32(c.TXQueueLen)}
  182. if err = ioctl(fd, unix.SIOCSIFTXQLEN, uintptr(unsafe.Pointer(&ifrq))); err != nil {
  183. // If we can't set the queue length nebula will still work but it may lead to packet loss
  184. l.WithError(err).Error("Failed to set tun tx queue length")
  185. }
  186. // Bring up the interface
  187. ifrf.Flags = ifrf.Flags | unix.IFF_UP
  188. if err = ioctl(fd, unix.SIOCSIFFLAGS, uintptr(unsafe.Pointer(&ifrf))); err != nil {
  189. return fmt.Errorf("failed to bring the tun device up: %s", err)
  190. }
  191. // Set the routes
  192. link, err := netlink.LinkByName(c.Device)
  193. if err != nil {
  194. return fmt.Errorf("failed to get tun device link: %s", err)
  195. }
  196. // Default route
  197. dr := &net.IPNet{IP: c.Cidr.IP.Mask(c.Cidr.Mask), Mask: c.Cidr.Mask}
  198. nr := netlink.Route{
  199. LinkIndex: link.Attrs().Index,
  200. Dst: dr,
  201. MTU: c.DefaultMTU,
  202. AdvMSS: c.advMSS(route{}),
  203. Scope: unix.RT_SCOPE_LINK,
  204. Src: c.Cidr.IP,
  205. Protocol: unix.RTPROT_KERNEL,
  206. Table: unix.RT_TABLE_MAIN,
  207. Type: unix.RTN_UNICAST,
  208. }
  209. err = netlink.RouteReplace(&nr)
  210. if err != nil {
  211. return fmt.Errorf("failed to set mtu %v on the default route %v; %v", c.DefaultMTU, dr, err)
  212. }
  213. // Path routes
  214. for _, r := range c.Routes {
  215. nr := netlink.Route{
  216. LinkIndex: link.Attrs().Index,
  217. Dst: r.route,
  218. MTU: r.mtu,
  219. AdvMSS: c.advMSS(r),
  220. Scope: unix.RT_SCOPE_LINK,
  221. }
  222. err = netlink.RouteAdd(&nr)
  223. if err != nil {
  224. return fmt.Errorf("failed to set mtu %v on route %v; %v", r.mtu, r.route, err)
  225. }
  226. }
  227. // Unsafe path routes
  228. for _, r := range c.UnsafeRoutes {
  229. nr := netlink.Route{
  230. LinkIndex: link.Attrs().Index,
  231. Dst: r.route,
  232. MTU: r.mtu,
  233. AdvMSS: c.advMSS(r),
  234. Scope: unix.RT_SCOPE_LINK,
  235. }
  236. err = netlink.RouteAdd(&nr)
  237. if err != nil {
  238. return fmt.Errorf("failed to set mtu %v on route %v; %v", r.mtu, r.route, err)
  239. }
  240. }
  241. // Run the interface
  242. ifrf.Flags = ifrf.Flags | unix.IFF_UP | unix.IFF_RUNNING
  243. if err = ioctl(fd, unix.SIOCSIFFLAGS, uintptr(unsafe.Pointer(&ifrf))); err != nil {
  244. return fmt.Errorf("failed to run tun device: %s", err)
  245. }
  246. return nil
  247. }
  248. func (c *Tun) CidrNet() *net.IPNet {
  249. return c.Cidr
  250. }
  251. func (c *Tun) DeviceName() string {
  252. return c.Device
  253. }
  254. func (c Tun) advMSS(r route) int {
  255. mtu := r.mtu
  256. if r.mtu == 0 {
  257. mtu = c.DefaultMTU
  258. }
  259. // We only need to set advmss if the route MTU does not match the device MTU
  260. if mtu != c.MaxMTU {
  261. return mtu - 40
  262. }
  263. return 0
  264. }