tun_linux.go 7.0 KB

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