options.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package icmp6
  15. import (
  16. "encoding/binary"
  17. "net"
  18. )
  19. type ICMP6OptionType int
  20. const (
  21. ICMP6OptMTU ICMP6OptionType = 1
  22. ICMP6OptPrefixInfo ICMP6OptionType = 2
  23. ICMP6OptLinkLayerAddress ICMP6OptionType = 3
  24. ICMP6OptRouteInfo ICMP6OptionType = 24
  25. )
  26. type ICMP6Option struct {
  27. Type ICMP6OptionType
  28. Data []byte
  29. }
  30. /*
  31. https://datatracker.ietf.org/doc/html/rfc4861#section-4.6.4
  32. 0 1 2 3
  33. 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  34. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  35. | Type | Length | Reserved |
  36. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  37. | MTU |
  38. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  39. */
  40. type icmpV6OptMtu struct {
  41. MTU uint32
  42. }
  43. func (opt icmpV6OptMtu) Bytes() []byte {
  44. buf := make([]byte, 6)
  45. binary.BigEndian.PutUint32(buf[2:], opt.MTU)
  46. return buf
  47. }
  48. func NewIcmpV6OptMtu(mtu uint32) icmpV6OptMtu {
  49. return icmpV6OptMtu{
  50. MTU: mtu,
  51. }
  52. }
  53. func DecodeIcmpV6OptMtu(data []byte) uint32 {
  54. return binary.BigEndian.Uint32(data[2:])
  55. }
  56. /*
  57. https://datatracker.ietf.org/doc/html/rfc4861#section-4.6.2
  58. 0 1 2 3
  59. 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  60. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  61. | Type | Length | Prefix Length |L|A| Reserved1 |
  62. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  63. | Valid Lifetime |
  64. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  65. | Preferred Lifetime |
  66. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  67. | Reserved2 |
  68. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  69. | |
  70. + +
  71. | |
  72. + Prefix +
  73. | |
  74. + +
  75. | |
  76. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  77. */
  78. type icmpv6OptPrefixInfo struct {
  79. PrefixLen uint8
  80. Flag uint8
  81. ValidLifetime uint32
  82. PreferredLifetime uint32
  83. Reserved2 uint32
  84. Prefix []byte
  85. }
  86. func (opt icmpv6OptPrefixInfo) Bytes() []byte {
  87. buf := make([]byte, 30)
  88. buf[0] = opt.PrefixLen
  89. buf[1] = opt.Flag
  90. binary.BigEndian.PutUint32(buf[2:], opt.ValidLifetime)
  91. binary.BigEndian.PutUint32(buf[6:], opt.PreferredLifetime)
  92. binary.BigEndian.PutUint32(buf[10:], opt.Reserved2)
  93. copy(buf[14:], opt.Prefix[:])
  94. return buf
  95. }
  96. func DecodeIcmpv6OptPrefixInfo(data []byte) SPrefixInfoOption {
  97. rawInfo := icmpv6OptPrefixInfo{
  98. PrefixLen: data[0],
  99. Flag: data[1],
  100. ValidLifetime: binary.BigEndian.Uint32(data[2:6]),
  101. PreferredLifetime: binary.BigEndian.Uint32(data[6:10]),
  102. Reserved2: binary.BigEndian.Uint32(data[10:14]),
  103. Prefix: data[14:],
  104. }
  105. return SPrefixInfoOption{
  106. IsOnlink: rawInfo.Flag&0x80 != 0,
  107. IsAutoconf: rawInfo.Flag&0x40 != 0,
  108. Prefix: net.IP(rawInfo.Prefix),
  109. PrefixLen: rawInfo.PrefixLen,
  110. ValidLifetime: rawInfo.ValidLifetime,
  111. PreferredLifetime: rawInfo.PreferredLifetime,
  112. }
  113. }
  114. func NewIcmpv6OptPrefixInfo(prefInfo SPrefixInfoOption) icmpv6OptPrefixInfo {
  115. flag := uint8(0)
  116. if prefInfo.IsOnlink {
  117. flag |= 0x80
  118. }
  119. if prefInfo.IsAutoconf {
  120. flag |= 0x40
  121. }
  122. return icmpv6OptPrefixInfo{
  123. PrefixLen: prefInfo.PrefixLen,
  124. Flag: flag,
  125. ValidLifetime: prefInfo.ValidLifetime,
  126. PreferredLifetime: prefInfo.PreferredLifetime,
  127. Reserved2: 0,
  128. Prefix: prefInfo.Prefix.To16(),
  129. }
  130. }
  131. /*
  132. https://datatracker.ietf.org/doc/html/rfc4861#section-4.6.1
  133. 0 1 2 3
  134. 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  135. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  136. | Type | Length | Link-Layer Address ...
  137. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  138. */
  139. type icmpv6OptSourceTargetAddress struct {
  140. Mac net.HardwareAddr
  141. }
  142. func (opt icmpv6OptSourceTargetAddress) Bytes() []byte {
  143. return opt.Mac[0:6]
  144. }
  145. func DecodeIcmpv6OptSourceTargetAddress(data []byte) net.HardwareAddr {
  146. return net.HardwareAddr(data[0:6])
  147. }
  148. func NewIcmpv6OptSourceTargetAddress(macAddr net.HardwareAddr) icmpv6OptSourceTargetAddress {
  149. return icmpv6OptSourceTargetAddress{
  150. Mac: macAddr,
  151. }
  152. }
  153. /*
  154. https://datatracker.ietf.org/doc/html/rfc4191#section-2.3
  155. 0 1 2 3
  156. 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  157. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  158. | Type | Length | Prefix Length |Resvd|Prf|Resvd|
  159. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  160. | Route Lifetime |
  161. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  162. | Prefix (Variable Length) |
  163. . .
  164. . .
  165. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  166. */
  167. type icmpv6OptRouteInfo struct {
  168. PrefixLen uint8
  169. Reserved1 uint8
  170. RouteLifetime uint32
  171. Prefix []byte
  172. }
  173. func (opt icmpv6OptRouteInfo) Bytes() []byte {
  174. buf := make([]byte, 6)
  175. buf[0] = opt.PrefixLen
  176. buf[1] = opt.Reserved1
  177. binary.BigEndian.PutUint32(buf[2:6], opt.RouteLifetime)
  178. buf = append(buf, opt.Prefix...)
  179. return buf
  180. }
  181. func DecodeIcmpv6OptRouteInfo(data []byte) SRouteInfoOption {
  182. rawInfo := icmpv6OptRouteInfo{
  183. PrefixLen: data[0],
  184. Reserved1: data[1],
  185. RouteLifetime: binary.BigEndian.Uint32(data[2:6]),
  186. Prefix: data[6:],
  187. }
  188. for i := len(rawInfo.Prefix); i < 16; i++ {
  189. rawInfo.Prefix = append(rawInfo.Prefix, 0)
  190. }
  191. var preference TPreference
  192. switch rawInfo.Reserved1 {
  193. case 0xc0:
  194. preference = PreferenceLow
  195. case 0x0:
  196. preference = PreferenceMedium
  197. case 0x40:
  198. preference = PreferenceHigh
  199. }
  200. return SRouteInfoOption{
  201. RouteLifetime: rawInfo.RouteLifetime,
  202. Prefix: net.IP(rawInfo.Prefix[:16]),
  203. PrefixLen: rawInfo.PrefixLen,
  204. Preference: preference,
  205. }
  206. }
  207. func NewIcmpv6OptRouteInfo(routeInfo SRouteInfoOption) icmpv6OptRouteInfo {
  208. prefix := routeInfo.Prefix.To16()
  209. prefixLen := routeInfo.PrefixLen
  210. routeLifetime := routeInfo.RouteLifetime
  211. if prefixLen <= 0 {
  212. prefix = prefix[0:0]
  213. } else if prefixLen <= 64 {
  214. prefix = prefix[0:8]
  215. } else {
  216. prefix = prefix[0:16]
  217. }
  218. return icmpv6OptRouteInfo{
  219. PrefixLen: prefixLen,
  220. Reserved1: 0,
  221. RouteLifetime: routeLifetime,
  222. Prefix: prefix,
  223. }
  224. }