address.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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 (a *LinkAddr) lenAndSpace() (int, int) {
  21. l := 8 + len(a.Name) + len(a.Addr)
  22. return l, roundup(l)
  23. }
  24. func (a *LinkAddr) marshal(b []byte) (int, error) {
  25. l, ll := a.lenAndSpace()
  26. if len(b) < ll {
  27. return 0, errShortBuffer
  28. }
  29. nlen, alen := len(a.Name), len(a.Addr)
  30. if nlen > 255 || alen > 255 {
  31. return 0, errInvalidAddr
  32. }
  33. b[0] = byte(l)
  34. b[1] = sysAF_LINK
  35. if a.Index > 0 {
  36. nativeEndian.PutUint16(b[2:4], uint16(a.Index))
  37. }
  38. data := b[8:]
  39. if nlen > 0 {
  40. b[5] = byte(nlen)
  41. copy(data[:nlen], a.Name)
  42. data = data[nlen:]
  43. }
  44. if alen > 0 {
  45. b[6] = byte(alen)
  46. copy(data[:alen], a.Addr)
  47. data = data[alen:]
  48. }
  49. return ll, nil
  50. }
  51. func parseLinkAddr(b []byte) (Addr, error) {
  52. if len(b) < 8 {
  53. return nil, errInvalidAddr
  54. }
  55. _, a, err := parseKernelLinkAddr(sysAF_LINK, b[4:])
  56. if err != nil {
  57. return nil, err
  58. }
  59. a.(*LinkAddr).Index = int(nativeEndian.Uint16(b[2:4]))
  60. return a, nil
  61. }
  62. // parseKernelLinkAddr parses b as a link-layer address in
  63. // conventional BSD kernel form.
  64. func parseKernelLinkAddr(_ int, b []byte) (int, Addr, error) {
  65. // The encoding looks like the following:
  66. // +----------------------------+
  67. // | Type (1 octet) |
  68. // +----------------------------+
  69. // | Name length (1 octet) |
  70. // +----------------------------+
  71. // | Address length (1 octet) |
  72. // +----------------------------+
  73. // | Selector length (1 octet) |
  74. // +----------------------------+
  75. // | Data (variable) |
  76. // +----------------------------+
  77. //
  78. // On some platforms, all-bit-one of length field means "don't
  79. // care".
  80. nlen, alen, slen := int(b[1]), int(b[2]), int(b[3])
  81. if nlen == 0xff {
  82. nlen = 0
  83. }
  84. if alen == 0xff {
  85. alen = 0
  86. }
  87. if slen == 0xff {
  88. slen = 0
  89. }
  90. l := 4 + nlen + alen + slen
  91. if len(b) < l {
  92. return 0, nil, errInvalidAddr
  93. }
  94. data := b[4:]
  95. var name string
  96. var addr []byte
  97. if nlen > 0 {
  98. name = string(data[:nlen])
  99. data = data[nlen:]
  100. }
  101. if alen > 0 {
  102. addr = data[:alen]
  103. data = data[alen:]
  104. }
  105. return l, &LinkAddr{Name: name, Addr: addr}, nil
  106. }
  107. // An Inet4Addr represents an internet address for IPv4.
  108. type Inet4Addr struct {
  109. IP [4]byte // IP address
  110. }
  111. // Family implements the Family method of Addr interface.
  112. func (a *Inet4Addr) Family() int { return sysAF_INET }
  113. func (a *Inet4Addr) lenAndSpace() (int, int) {
  114. return sizeofSockaddrInet, roundup(sizeofSockaddrInet)
  115. }
  116. func (a *Inet4Addr) marshal(b []byte) (int, error) {
  117. l, ll := a.lenAndSpace()
  118. if len(b) < ll {
  119. return 0, errShortBuffer
  120. }
  121. b[0] = byte(l)
  122. b[1] = sysAF_INET
  123. copy(b[4:8], a.IP[:])
  124. return ll, nil
  125. }
  126. // An Inet6Addr represents an internet address for IPv6.
  127. type Inet6Addr struct {
  128. IP [16]byte // IP address
  129. ZoneID int // zone identifier
  130. }
  131. // Family implements the Family method of Addr interface.
  132. func (a *Inet6Addr) Family() int { return sysAF_INET6 }
  133. func (a *Inet6Addr) lenAndSpace() (int, int) {
  134. return sizeofSockaddrInet6, roundup(sizeofSockaddrInet6)
  135. }
  136. func (a *Inet6Addr) marshal(b []byte) (int, error) {
  137. l, ll := a.lenAndSpace()
  138. if len(b) < ll {
  139. return 0, errShortBuffer
  140. }
  141. b[0] = byte(l)
  142. b[1] = sysAF_INET6
  143. copy(b[8:24], a.IP[:])
  144. if a.ZoneID > 0 {
  145. nativeEndian.PutUint32(b[24:28], uint32(a.ZoneID))
  146. }
  147. return ll, nil
  148. }
  149. // parseInetAddr parses b as an internet address for IPv4 or IPv6.
  150. func parseInetAddr(af int, b []byte) (Addr, error) {
  151. switch af {
  152. case sysAF_INET:
  153. if len(b) < sizeofSockaddrInet {
  154. return nil, errInvalidAddr
  155. }
  156. a := &Inet4Addr{}
  157. copy(a.IP[:], b[4:8])
  158. return a, nil
  159. case sysAF_INET6:
  160. if len(b) < sizeofSockaddrInet6 {
  161. return nil, errInvalidAddr
  162. }
  163. a := &Inet6Addr{ZoneID: int(nativeEndian.Uint32(b[24:28]))}
  164. copy(a.IP[:], b[8:24])
  165. if a.IP[0] == 0xfe && a.IP[1]&0xc0 == 0x80 || a.IP[0] == 0xff && (a.IP[1]&0x0f == 0x01 || a.IP[1]&0x0f == 0x02) {
  166. // KAME based IPv6 protocol stack usually
  167. // embeds the interface index in the
  168. // interface-local or link-local address as
  169. // the kernel-internal form.
  170. id := int(bigEndian.Uint16(a.IP[2:4]))
  171. if id != 0 {
  172. a.ZoneID = id
  173. a.IP[2], a.IP[3] = 0, 0
  174. }
  175. }
  176. return a, nil
  177. default:
  178. return nil, errInvalidAddr
  179. }
  180. }
  181. // parseKernelInetAddr parses b as an internet address in conventional
  182. // BSD kernel form.
  183. func parseKernelInetAddr(af int, b []byte) (int, Addr, error) {
  184. // The encoding looks similar to the NLRI encoding.
  185. // +----------------------------+
  186. // | Length (1 octet) |
  187. // +----------------------------+
  188. // | Address prefix (variable) |
  189. // +----------------------------+
  190. //
  191. // The differences between the kernel form and the NLRI
  192. // encoding are:
  193. //
  194. // - The length field of the kernel form indicates the prefix
  195. // length in bytes, not in bits
  196. //
  197. // - In the kernel form, zero value of the length field
  198. // doesn't mean 0.0.0.0/0 or ::/0
  199. //
  200. // - The kernel form appends leading bytes to the prefix field
  201. // to make the <length, prefix> tuple to be conformed with
  202. // the routing message boundary
  203. l := int(b[0])
  204. if runtime.GOOS == "darwin" {
  205. // On Darwin, an address in the kernel form is also
  206. // used as a message filler.
  207. if l == 0 || len(b) > roundup(l) {
  208. l = roundup(l)
  209. }
  210. } else {
  211. l = roundup(l)
  212. }
  213. if len(b) < l {
  214. return 0, nil, errInvalidAddr
  215. }
  216. // Don't reorder case expressions.
  217. // The case expressions for IPv6 must come first.
  218. const (
  219. off4 = 4 // offset of in_addr
  220. off6 = 8 // offset of in6_addr
  221. )
  222. switch {
  223. case b[0] == sizeofSockaddrInet6:
  224. a := &Inet6Addr{}
  225. copy(a.IP[:], b[off6:off6+16])
  226. return int(b[0]), a, nil
  227. case af == sysAF_INET6:
  228. a := &Inet6Addr{}
  229. if l-1 < off6 {
  230. copy(a.IP[:], b[1:l])
  231. } else {
  232. copy(a.IP[:], b[l-off6:l])
  233. }
  234. return int(b[0]), a, nil
  235. case b[0] == sizeofSockaddrInet:
  236. a := &Inet4Addr{}
  237. copy(a.IP[:], b[off4:off4+4])
  238. return int(b[0]), a, nil
  239. default: // an old fashion, AF_UNSPEC or unknown means AF_INET
  240. a := &Inet4Addr{}
  241. if l-1 < off4 {
  242. copy(a.IP[:], b[1:l])
  243. } else {
  244. copy(a.IP[:], b[l-off4:l])
  245. }
  246. return int(b[0]), a, nil
  247. }
  248. }
  249. // A DefaultAddr represents an address of various operating
  250. // system-specific features.
  251. type DefaultAddr struct {
  252. af int
  253. Raw []byte // raw format of address
  254. }
  255. // Family implements the Family method of Addr interface.
  256. func (a *DefaultAddr) Family() int { return a.af }
  257. func (a *DefaultAddr) lenAndSpace() (int, int) {
  258. l := len(a.Raw)
  259. return l, roundup(l)
  260. }
  261. func (a *DefaultAddr) marshal(b []byte) (int, error) {
  262. l, ll := a.lenAndSpace()
  263. if len(b) < ll {
  264. return 0, errShortBuffer
  265. }
  266. if l > 255 {
  267. return 0, errInvalidAddr
  268. }
  269. b[1] = byte(l)
  270. copy(b[:l], a.Raw)
  271. return ll, nil
  272. }
  273. func parseDefaultAddr(b []byte) (Addr, error) {
  274. if len(b) < 2 || len(b) < int(b[0]) {
  275. return nil, errInvalidAddr
  276. }
  277. a := &DefaultAddr{af: int(b[1]), Raw: b[:b[0]]}
  278. return a, nil
  279. }
  280. func addrsSpace(as []Addr) int {
  281. var l int
  282. for _, a := range as {
  283. switch a := a.(type) {
  284. case *LinkAddr:
  285. _, ll := a.lenAndSpace()
  286. l += ll
  287. case *Inet4Addr:
  288. _, ll := a.lenAndSpace()
  289. l += ll
  290. case *Inet6Addr:
  291. _, ll := a.lenAndSpace()
  292. l += ll
  293. case *DefaultAddr:
  294. _, ll := a.lenAndSpace()
  295. l += ll
  296. }
  297. }
  298. return l
  299. }
  300. // marshalAddrs marshals as and returns a bitmap indicating which
  301. // address is stored in b.
  302. func marshalAddrs(b []byte, as []Addr) (uint, error) {
  303. var attrs uint
  304. for i, a := range as {
  305. switch a := a.(type) {
  306. case *LinkAddr:
  307. l, err := a.marshal(b)
  308. if err != nil {
  309. return 0, err
  310. }
  311. b = b[l:]
  312. attrs |= 1 << uint(i)
  313. case *Inet4Addr:
  314. l, err := a.marshal(b)
  315. if err != nil {
  316. return 0, err
  317. }
  318. b = b[l:]
  319. attrs |= 1 << uint(i)
  320. case *Inet6Addr:
  321. l, err := a.marshal(b)
  322. if err != nil {
  323. return 0, err
  324. }
  325. b = b[l:]
  326. attrs |= 1 << uint(i)
  327. case *DefaultAddr:
  328. l, err := a.marshal(b)
  329. if err != nil {
  330. return 0, err
  331. }
  332. b = b[l:]
  333. attrs |= 1 << uint(i)
  334. }
  335. }
  336. return attrs, nil
  337. }
  338. func parseAddrs(attrs uint, fn func(int, []byte) (int, Addr, error), b []byte) ([]Addr, error) {
  339. var as [sysRTAX_MAX]Addr
  340. af := int(sysAF_UNSPEC)
  341. for i := uint(0); i < sysRTAX_MAX && len(b) >= roundup(0); i++ {
  342. if attrs&(1<<i) == 0 {
  343. continue
  344. }
  345. if i <= sysRTAX_BRD {
  346. switch b[1] {
  347. case sysAF_LINK:
  348. a, err := parseLinkAddr(b)
  349. if err != nil {
  350. return nil, err
  351. }
  352. as[i] = a
  353. l := roundup(int(b[0]))
  354. if len(b) < l {
  355. return nil, errMessageTooShort
  356. }
  357. b = b[l:]
  358. case sysAF_INET, sysAF_INET6:
  359. af = int(b[1])
  360. a, err := parseInetAddr(af, b)
  361. if err != nil {
  362. return nil, err
  363. }
  364. as[i] = a
  365. l := roundup(int(b[0]))
  366. if len(b) < l {
  367. return nil, errMessageTooShort
  368. }
  369. b = b[l:]
  370. default:
  371. l, a, err := fn(af, b)
  372. if err != nil {
  373. return nil, err
  374. }
  375. as[i] = a
  376. ll := roundup(l)
  377. if len(b) < ll {
  378. b = b[l:]
  379. } else {
  380. b = b[ll:]
  381. }
  382. }
  383. } else {
  384. a, err := parseDefaultAddr(b)
  385. if err != nil {
  386. return nil, err
  387. }
  388. as[i] = a
  389. l := roundup(int(b[0]))
  390. if len(b) < l {
  391. return nil, errMessageTooShort
  392. }
  393. b = b[l:]
  394. }
  395. }
  396. return as[:], nil
  397. }