net.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. // Add parses a CIDR mask and appends it to the list. It panics for invalid masks and is
  80. // intended to be used for setting up static lists.
  81. func (l *Netlist) Add(cidr string) {
  82. _, n, err := net.ParseCIDR(cidr)
  83. if err != nil {
  84. panic(err)
  85. }
  86. *l = append(*l, *n)
  87. }
  88. // Contains reports whether the given IP is contained in the list.
  89. func (l *Netlist) Contains(ip net.IP) bool {
  90. if l == nil {
  91. return false
  92. }
  93. for _, net := range *l {
  94. if net.Contains(ip) {
  95. return true
  96. }
  97. }
  98. return false
  99. }
  100. // IsLAN reports whether an IP is a local network address.
  101. func IsLAN(ip net.IP) bool {
  102. if ip.IsLoopback() {
  103. return true
  104. }
  105. if v4 := ip.To4(); v4 != nil {
  106. return lan4.Contains(v4)
  107. }
  108. return lan6.Contains(ip)
  109. }
  110. // IsSpecialNetwork reports whether an IP is located in a special-use network range
  111. // This includes broadcast, multicast and documentation addresses.
  112. func IsSpecialNetwork(ip net.IP) bool {
  113. if ip.IsMulticast() {
  114. return true
  115. }
  116. if v4 := ip.To4(); v4 != nil {
  117. return special4.Contains(v4)
  118. }
  119. return special6.Contains(ip)
  120. }
  121. var (
  122. errInvalid = errors.New("invalid IP")
  123. errUnspecified = errors.New("zero address")
  124. errSpecial = errors.New("special network")
  125. errLoopback = errors.New("loopback address from non-loopback host")
  126. errLAN = errors.New("LAN address from WAN host")
  127. )
  128. // CheckRelayIP reports whether an IP relayed from the given sender IP
  129. // is a valid connection target.
  130. //
  131. // There are four rules:
  132. // - Special network addresses are never valid.
  133. // - Loopback addresses are OK if relayed by a loopback host.
  134. // - LAN addresses are OK if relayed by a LAN host.
  135. // - All other addresses are always acceptable.
  136. func CheckRelayIP(sender, addr net.IP) error {
  137. if len(addr) != net.IPv4len && len(addr) != net.IPv6len {
  138. return errInvalid
  139. }
  140. if addr.IsUnspecified() {
  141. return errUnspecified
  142. }
  143. if IsSpecialNetwork(addr) {
  144. return errSpecial
  145. }
  146. if addr.IsLoopback() && !sender.IsLoopback() {
  147. return errLoopback
  148. }
  149. if IsLAN(addr) && !IsLAN(sender) {
  150. return errLAN
  151. }
  152. return nil
  153. }