interface_bsd.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // Copyright 2011 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // +build darwin dragonfly freebsd netbsd openbsd
  5. package net
  6. import (
  7. "os"
  8. "syscall"
  9. "unsafe"
  10. )
  11. // If the ifindex is zero, interfaceTable returns mappings of all
  12. // network interfaces. Otherwise it returns a mapping of a specific
  13. // interface.
  14. func interfaceTable(ifindex int) ([]Interface, error) {
  15. tab, err := syscall.RouteRIB(syscall.NET_RT_IFLIST, ifindex)
  16. if err != nil {
  17. return nil, os.NewSyscallError("route rib", err)
  18. }
  19. msgs, err := syscall.ParseRoutingMessage(tab)
  20. if err != nil {
  21. return nil, os.NewSyscallError("route message", err)
  22. }
  23. return parseInterfaceTable(ifindex, msgs)
  24. }
  25. func parseInterfaceTable(ifindex int, msgs []syscall.RoutingMessage) ([]Interface, error) {
  26. var ift []Interface
  27. loop:
  28. for _, m := range msgs {
  29. switch m := m.(type) {
  30. case *syscall.InterfaceMessage:
  31. if ifindex == 0 || ifindex == int(m.Header.Index) {
  32. ifi, err := newLink(m)
  33. if err != nil {
  34. return nil, err
  35. }
  36. ift = append(ift, *ifi)
  37. if ifindex == int(m.Header.Index) {
  38. break loop
  39. }
  40. }
  41. }
  42. }
  43. return ift, nil
  44. }
  45. func newLink(m *syscall.InterfaceMessage) (*Interface, error) {
  46. sas, err := syscall.ParseRoutingSockaddr(m)
  47. if err != nil {
  48. return nil, os.NewSyscallError("route sockaddr", err)
  49. }
  50. ifi := &Interface{Index: int(m.Header.Index), Flags: linkFlags(m.Header.Flags)}
  51. for _, sa := range sas {
  52. switch sa := sa.(type) {
  53. case *syscall.SockaddrDatalink:
  54. // NOTE: SockaddrDatalink.Data is minimum work area,
  55. // can be larger.
  56. m.Data = m.Data[unsafe.Offsetof(sa.Data):]
  57. var name [syscall.IFNAMSIZ]byte
  58. for i := 0; i < int(sa.Nlen); i++ {
  59. name[i] = byte(m.Data[i])
  60. }
  61. ifi.Name = string(name[:sa.Nlen])
  62. ifi.MTU = int(m.Header.Data.Mtu)
  63. addr := make([]byte, sa.Alen)
  64. for i := 0; i < int(sa.Alen); i++ {
  65. addr[i] = byte(m.Data[int(sa.Nlen)+i])
  66. }
  67. ifi.HardwareAddr = addr[:sa.Alen]
  68. }
  69. }
  70. return ifi, nil
  71. }
  72. func linkFlags(rawFlags int32) Flags {
  73. var f Flags
  74. if rawFlags&syscall.IFF_UP != 0 {
  75. f |= FlagUp
  76. }
  77. if rawFlags&syscall.IFF_BROADCAST != 0 {
  78. f |= FlagBroadcast
  79. }
  80. if rawFlags&syscall.IFF_LOOPBACK != 0 {
  81. f |= FlagLoopback
  82. }
  83. if rawFlags&syscall.IFF_POINTOPOINT != 0 {
  84. f |= FlagPointToPoint
  85. }
  86. if rawFlags&syscall.IFF_MULTICAST != 0 {
  87. f |= FlagMulticast
  88. }
  89. return f
  90. }
  91. // If the ifi is nil, interfaceAddrTable returns addresses for all
  92. // network interfaces. Otherwise it returns addresses for a specific
  93. // interface.
  94. func interfaceAddrTable(ifi *Interface) ([]Addr, error) {
  95. index := 0
  96. if ifi != nil {
  97. index = ifi.Index
  98. }
  99. tab, err := syscall.RouteRIB(syscall.NET_RT_IFLIST, index)
  100. if err != nil {
  101. return nil, os.NewSyscallError("route rib", err)
  102. }
  103. msgs, err := syscall.ParseRoutingMessage(tab)
  104. if err != nil {
  105. return nil, os.NewSyscallError("route message", err)
  106. }
  107. var ift []Interface
  108. if index == 0 {
  109. ift, err = parseInterfaceTable(index, msgs)
  110. if err != nil {
  111. return nil, err
  112. }
  113. }
  114. var ifat []Addr
  115. for _, m := range msgs {
  116. switch m := m.(type) {
  117. case *syscall.InterfaceAddrMessage:
  118. if index == 0 || index == int(m.Header.Index) {
  119. if index == 0 {
  120. var err error
  121. ifi, err = interfaceByIndex(ift, int(m.Header.Index))
  122. if err != nil {
  123. return nil, err
  124. }
  125. }
  126. ifa, err := newAddr(ifi, m)
  127. if err != nil {
  128. return nil, err
  129. }
  130. if ifa != nil {
  131. ifat = append(ifat, ifa)
  132. }
  133. }
  134. }
  135. }
  136. return ifat, nil
  137. }
  138. func newAddr(ifi *Interface, m *syscall.InterfaceAddrMessage) (Addr, error) {
  139. sas, err := syscall.ParseRoutingSockaddr(m)
  140. if err != nil {
  141. return nil, os.NewSyscallError("route sockaddr", err)
  142. }
  143. ifa := &IPNet{}
  144. for i, sa := range sas {
  145. switch sa := sa.(type) {
  146. case *syscall.SockaddrInet4:
  147. switch i {
  148. case 0:
  149. ifa.Mask = IPv4Mask(sa.Addr[0], sa.Addr[1], sa.Addr[2], sa.Addr[3])
  150. case 1:
  151. ifa.IP = IPv4(sa.Addr[0], sa.Addr[1], sa.Addr[2], sa.Addr[3])
  152. }
  153. case *syscall.SockaddrInet6:
  154. switch i {
  155. case 0:
  156. ifa.Mask = make(IPMask, IPv6len)
  157. copy(ifa.Mask, sa.Addr[:])
  158. case 1:
  159. ifa.IP = make(IP, IPv6len)
  160. copy(ifa.IP, sa.Addr[:])
  161. // NOTE: KAME based IPv6 protcol stack usually embeds
  162. // the interface index in the interface-local or link-
  163. // local address as the kernel-internal form.
  164. if ifa.IP.IsLinkLocalUnicast() {
  165. ifa.IP[2], ifa.IP[3] = 0, 0
  166. }
  167. }
  168. default: // Sockaddrs contain syscall.SockaddrDatalink on NetBSD
  169. return nil, nil
  170. }
  171. }
  172. return ifa, nil
  173. }