net.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // Copyright 2016 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. // Package netutil contains extensions to the net package.
  17. package netutil
  18. import (
  19. "errors"
  20. "net"
  21. "strings"
  22. )
  23. var lan4, lan6, special4, special6 Netlist
  24. func init() {
  25. // Lists from RFC 5735, RFC 5156,
  26. // https://www.iana.org/assignments/iana-ipv4-special-registry/
  27. lan4.Add("0.0.0.0/8") // "This" network
  28. lan4.Add("10.0.0.0/8") // Private Use
  29. lan4.Add("172.16.0.0/12") // Private Use
  30. lan4.Add("192.168.0.0/16") // Private Use
  31. lan6.Add("fe80::/10") // Link-Local
  32. lan6.Add("fc00::/7") // Unique-Local
  33. special4.Add("192.0.0.0/29") // IPv4 Service Continuity
  34. special4.Add("192.0.0.9/32") // PCP Anycast
  35. special4.Add("192.0.0.170/32") // NAT64/DNS64 Discovery
  36. special4.Add("192.0.0.171/32") // NAT64/DNS64 Discovery
  37. special4.Add("192.0.2.0/24") // TEST-NET-1
  38. special4.Add("192.31.196.0/24") // AS112
  39. special4.Add("192.52.193.0/24") // AMT
  40. special4.Add("192.88.99.0/24") // 6to4 Relay Anycast
  41. special4.Add("192.175.48.0/24") // AS112
  42. special4.Add("198.18.0.0/15") // Device Benchmark Testing
  43. special4.Add("198.51.100.0/24") // TEST-NET-2
  44. special4.Add("203.0.113.0/24") // TEST-NET-3
  45. special4.Add("255.255.255.255/32") // Limited Broadcast
  46. // http://www.iana.org/assignments/iana-ipv6-special-registry/
  47. special6.Add("100::/64")
  48. special6.Add("2001::/32")
  49. special6.Add("2001:1::1/128")
  50. special6.Add("2001:2::/48")
  51. special6.Add("2001:3::/32")
  52. special6.Add("2001:4:112::/48")
  53. special6.Add("2001:5::/32")
  54. special6.Add("2001:10::/28")
  55. special6.Add("2001:20::/28")
  56. special6.Add("2001:db8::/32")
  57. special6.Add("2002::/16")
  58. }
  59. // Netlist is a list of IP networks.
  60. type Netlist []net.IPNet
  61. // ParseNetlist parses a comma-separated list of CIDR masks.
  62. // Whitespace and extra commas are ignored.
  63. func ParseNetlist(s string) (*Netlist, error) {
  64. ws := strings.NewReplacer(" ", "", "\n", "", "\t", "")
  65. masks := strings.Split(ws.Replace(s), ",")
  66. l := make(Netlist, 0)
  67. for _, mask := range masks {
  68. if mask == "" {
  69. continue
  70. }
  71. _, n, err := net.ParseCIDR(mask)
  72. if err != nil {
  73. return nil, err
  74. }
  75. l = append(l, *n)
  76. }
  77. return &l, nil
  78. }
  79. // MarshalTOML implements toml.MarshalerRec.
  80. func (l Netlist) MarshalTOML() interface{} {
  81. list := make([]string, 0, len(l))
  82. for _, net := range l {
  83. list = append(list, net.String())
  84. }
  85. return list
  86. }
  87. // UnmarshalTOML implements toml.UnmarshalerRec.
  88. func (l *Netlist) UnmarshalTOML(fn func(interface{}) error) error {
  89. var masks []string
  90. if err := fn(&masks); err != nil {
  91. return err
  92. }
  93. for _, mask := range masks {
  94. _, n, err := net.ParseCIDR(mask)
  95. if err != nil {
  96. return err
  97. }
  98. *l = append(*l, *n)
  99. }
  100. return nil
  101. }
  102. // Add parses a CIDR mask and appends it to the list. It panics for invalid masks and is
  103. // intended to be used for setting up static lists.
  104. func (l *Netlist) Add(cidr string) {
  105. _, n, err := net.ParseCIDR(cidr)
  106. if err != nil {
  107. panic(err)
  108. }
  109. *l = append(*l, *n)
  110. }
  111. // Contains reports whether the given IP is contained in the list.
  112. func (l *Netlist) Contains(ip net.IP) bool {
  113. if l == nil {
  114. return false
  115. }
  116. for _, net := range *l {
  117. if net.Contains(ip) {
  118. return true
  119. }
  120. }
  121. return false
  122. }
  123. // IsLAN reports whether an IP is a local network address.
  124. func IsLAN(ip net.IP) bool {
  125. if ip.IsLoopback() {
  126. return true
  127. }
  128. if v4 := ip.To4(); v4 != nil {
  129. return lan4.Contains(v4)
  130. }
  131. return lan6.Contains(ip)
  132. }
  133. // IsSpecialNetwork reports whether an IP is located in a special-use network range
  134. // This includes broadcast, multicast and documentation addresses.
  135. func IsSpecialNetwork(ip net.IP) bool {
  136. if ip.IsMulticast() {
  137. return true
  138. }
  139. if v4 := ip.To4(); v4 != nil {
  140. return special4.Contains(v4)
  141. }
  142. return special6.Contains(ip)
  143. }
  144. var (
  145. errInvalid = errors.New("invalid IP")
  146. errUnspecified = errors.New("zero address")
  147. errSpecial = errors.New("special network")
  148. errLoopback = errors.New("loopback address from non-loopback host")
  149. errLAN = errors.New("LAN address from WAN host")
  150. )
  151. // CheckRelayIP reports whether an IP relayed from the given sender IP
  152. // is a valid connection target.
  153. //
  154. // There are four rules:
  155. // - Special network addresses are never valid.
  156. // - Loopback addresses are OK if relayed by a loopback host.
  157. // - LAN addresses are OK if relayed by a LAN host.
  158. // - All other addresses are always acceptable.
  159. func CheckRelayIP(sender, addr net.IP) error {
  160. if len(addr) != net.IPv4len && len(addr) != net.IPv6len {
  161. return errInvalid
  162. }
  163. if addr.IsUnspecified() {
  164. return errUnspecified
  165. }
  166. if IsSpecialNetwork(addr) {
  167. return errSpecial
  168. }
  169. if addr.IsLoopback() && !sender.IsLoopback() {
  170. return errLoopback
  171. }
  172. if IsLAN(addr) && !IsLAN(sender) {
  173. return errLAN
  174. }
  175. return nil
  176. }