address.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. // Copyright 2016 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 route
  6. import "runtime"
  7. // An Addr represents an address associated with packet routing.
  8. type Addr interface {
  9. // Family returns an address family.
  10. Family() int
  11. }
  12. // A LinkAddr represents a link-layer address.
  13. type LinkAddr struct {
  14. Index int // interface index when attached
  15. Name string // interface name when attached
  16. Addr []byte // link-layer address when attached
  17. }
  18. // Family implements the Family method of Addr interface.
  19. func (a *LinkAddr) Family() int { return sysAF_LINK }
  20. func parseLinkAddr(b []byte) (Addr, error) {
  21. if len(b) < 8 {
  22. return nil, errInvalidAddr
  23. }
  24. _, a, err := parseKernelLinkAddr(sysAF_LINK, b[4:])
  25. if err != nil {
  26. return nil, err
  27. }
  28. a.(*LinkAddr).Index = int(nativeEndian.Uint16(b[2:4]))
  29. return a, nil
  30. }
  31. // parseKernelLinkAddr parses b as a link-layer address in
  32. // conventional BSD kernel form.
  33. func parseKernelLinkAddr(_ int, b []byte) (int, Addr, error) {
  34. // The encoding looks like the following:
  35. // +----------------------------+
  36. // | Type (1 octet) |
  37. // +----------------------------+
  38. // | Name length (1 octet) |
  39. // +----------------------------+
  40. // | Address length (1 octet) |
  41. // +----------------------------+
  42. // | Selector length (1 octet) |
  43. // +----------------------------+
  44. // | Data (variable) |
  45. // +----------------------------+
  46. //
  47. // On some platforms, all-bit-one of length field means "don't
  48. // care".
  49. nlen, alen, slen := int(b[1]), int(b[2]), int(b[3])
  50. if nlen == 0xff {
  51. nlen = 0
  52. }
  53. if alen == 0xff {
  54. alen = 0
  55. }
  56. if slen == 0xff {
  57. slen = 0
  58. }
  59. l := 4 + nlen + alen + slen
  60. if len(b) < l {
  61. return 0, nil, errInvalidAddr
  62. }
  63. data := b[4:]
  64. var name string
  65. var addr []byte
  66. if nlen > 0 {
  67. name = string(data[:nlen])
  68. data = data[nlen:]
  69. }
  70. if alen > 0 {
  71. addr = data[:alen]
  72. data = data[alen:]
  73. }
  74. return l, &LinkAddr{Name: name, Addr: addr}, nil
  75. }
  76. // An Inet4Addr represents an internet address for IPv4.
  77. type Inet4Addr struct {
  78. IP [4]byte // IP address
  79. }
  80. // Family implements the Family method of Addr interface.
  81. func (a *Inet4Addr) Family() int { return sysAF_INET }
  82. // An Inet6Addr represents an internet address for IPv6.
  83. type Inet6Addr struct {
  84. IP [16]byte // IP address
  85. ZoneID int // zone identifier
  86. }
  87. // Family implements the Family method of Addr interface.
  88. func (a *Inet6Addr) Family() int { return sysAF_INET6 }
  89. // parseInetAddr parses b as an internet address for IPv4 or IPv6.
  90. func parseInetAddr(af int, b []byte) (Addr, error) {
  91. switch af {
  92. case sysAF_INET:
  93. if len(b) < 16 {
  94. return nil, errInvalidAddr
  95. }
  96. a := &Inet4Addr{}
  97. copy(a.IP[:], b[4:8])
  98. return a, nil
  99. case sysAF_INET6:
  100. if len(b) < 28 {
  101. return nil, errInvalidAddr
  102. }
  103. a := &Inet6Addr{ZoneID: int(nativeEndian.Uint32(b[24:28]))}
  104. copy(a.IP[:], b[8:24])
  105. if a.IP[0] == 0xfe && a.IP[1]&0xc0 == 0x80 || a.IP[0] == 0xff && (a.IP[1]&0x0f == 0x01 || a.IP[1]&0x0f == 0x02) {
  106. // KAME based IPv6 protocol stack usually
  107. // embeds the interface index in the
  108. // interface-local or link-local address as
  109. // the kernel-internal form.
  110. id := int(bigEndian.Uint16(a.IP[2:4]))
  111. if id != 0 {
  112. a.ZoneID = id
  113. a.IP[2], a.IP[3] = 0, 0
  114. }
  115. }
  116. return a, nil
  117. default:
  118. return nil, errInvalidAddr
  119. }
  120. }
  121. // parseKernelInetAddr parses b as an internet address in conventional
  122. // BSD kernel form.
  123. func parseKernelInetAddr(af int, b []byte) (int, Addr, error) {
  124. // The encoding looks similar to the NLRI encoding.
  125. // +----------------------------+
  126. // | Length (1 octet) |
  127. // +----------------------------+
  128. // | Address prefix (variable) |
  129. // +----------------------------+
  130. //
  131. // The differences between the kernel form and the NLRI
  132. // encoding are:
  133. //
  134. // - The length field of the kernel form indicates the prefix
  135. // length in bytes, not in bits
  136. //
  137. // - In the kernel form, zero value of the length field
  138. // doesn't mean 0.0.0.0/0 or ::/0
  139. //
  140. // - The kernel form appends leading bytes to the prefix field
  141. // to make the <length, prefix> tuple to be conformed with
  142. // the routing message boundary
  143. l := int(b[0])
  144. if runtime.GOOS == "darwin" {
  145. // On Darwn, an address in the kernel form is also
  146. // used as a message filler.
  147. if l == 0 || len(b) > roundup(l) {
  148. l = roundup(l)
  149. }
  150. } else {
  151. l = roundup(l)
  152. }
  153. if len(b) < l {
  154. return 0, nil, errInvalidAddr
  155. }
  156. // Don't reorder case expressions.
  157. // The case expressions for IPv6 must come first.
  158. const (
  159. off4 = 4 // offset of in_addr
  160. off6 = 8 // offset of in6_addr
  161. )
  162. switch {
  163. case b[0] == 28: // size of sockaddr_in6
  164. a := &Inet6Addr{}
  165. copy(a.IP[:], b[off6:off6+16])
  166. return int(b[0]), a, nil
  167. case af == sysAF_INET6:
  168. a := &Inet6Addr{}
  169. if l-1 < off6 {
  170. copy(a.IP[:], b[1:l])
  171. } else {
  172. copy(a.IP[:], b[l-off6:l])
  173. }
  174. return int(b[0]), a, nil
  175. case b[0] == 16: // size of sockaddr_in
  176. a := &Inet4Addr{}
  177. copy(a.IP[:], b[off4:off4+4])
  178. return int(b[0]), a, nil
  179. default: // an old fashion, AF_UNSPEC or unknown means AF_INET
  180. a := &Inet4Addr{}
  181. if l-1 < off4 {
  182. copy(a.IP[:], b[1:l])
  183. } else {
  184. copy(a.IP[:], b[l-off4:l])
  185. }
  186. return int(b[0]), a, nil
  187. }
  188. }
  189. // A DefaultAddr represents an address of various operating
  190. // system-specific features.
  191. type DefaultAddr struct {
  192. af int
  193. Raw []byte // raw format of address
  194. }
  195. // Family implements the Family method of Addr interface.
  196. func (a *DefaultAddr) Family() int { return a.af }
  197. func parseDefaultAddr(b []byte) (Addr, error) {
  198. if len(b) < 2 || len(b) < int(b[0]) {
  199. return nil, errInvalidAddr
  200. }
  201. a := &DefaultAddr{af: int(b[1]), Raw: b[:b[0]]}
  202. return a, nil
  203. }
  204. func parseAddrs(attrs uint, fn func(int, []byte) (int, Addr, error), b []byte) ([]Addr, error) {
  205. var as [sysRTAX_MAX]Addr
  206. af := int(sysAF_UNSPEC)
  207. for i := uint(0); i < sysRTAX_MAX && len(b) >= roundup(0); i++ {
  208. if attrs&(1<<i) == 0 {
  209. continue
  210. }
  211. if i <= sysRTAX_BRD {
  212. switch b[1] {
  213. case sysAF_LINK:
  214. a, err := parseLinkAddr(b)
  215. if err != nil {
  216. return nil, err
  217. }
  218. as[i] = a
  219. l := roundup(int(b[0]))
  220. if len(b) < l {
  221. return nil, errMessageTooShort
  222. }
  223. b = b[l:]
  224. case sysAF_INET, sysAF_INET6:
  225. af = int(b[1])
  226. a, err := parseInetAddr(af, b)
  227. if err != nil {
  228. return nil, err
  229. }
  230. as[i] = a
  231. l := roundup(int(b[0]))
  232. if len(b) < l {
  233. return nil, errMessageTooShort
  234. }
  235. b = b[l:]
  236. default:
  237. l, a, err := fn(af, b)
  238. if err != nil {
  239. return nil, err
  240. }
  241. as[i] = a
  242. ll := roundup(l)
  243. if len(b) < ll {
  244. b = b[l:]
  245. } else {
  246. b = b[ll:]
  247. }
  248. }
  249. } else {
  250. a, err := parseDefaultAddr(b)
  251. if err != nil {
  252. return nil, err
  253. }
  254. as[i] = a
  255. l := roundup(int(b[0]))
  256. if len(b) < l {
  257. return nil, errMessageTooShort
  258. }
  259. b = b[l:]
  260. }
  261. }
  262. return as[:], nil
  263. }