plugin.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // Copyright 2018-present the CoreDHCP Authors. All rights reserved
  2. // This source code is licensed under the MIT license found in the
  3. // LICENSE file in the root directory of this source tree.
  4. // Package file enables static mapping of MAC <--> IP addresses.
  5. // The mapping is stored in a text file, where each mapping is described by one line containing
  6. // two fields separated by spaces: MAC address, and IP address. For example:
  7. //
  8. // $ cat file_leases.txt
  9. // 00:11:22:33:44:55 10.0.0.1
  10. // 01:23:45:67:89:01 10.0.10.10
  11. //
  12. // To specify the plugin configuration in the server6/server4 sections of the config file, just
  13. // pass the leases file name as plugin argument, e.g.:
  14. //
  15. // $ cat config.yml
  16. //
  17. // server6:
  18. // ...
  19. // plugins:
  20. // - file: "file_leases.txt"
  21. // ...
  22. //
  23. // If the file path is not absolute, it is relative to the cwd where coredhcp is run.
  24. package file
  25. import (
  26. "bytes"
  27. "errors"
  28. "fmt"
  29. "io/ioutil"
  30. "net"
  31. "strings"
  32. "time"
  33. "github.com/coredhcp/coredhcp/handler"
  34. "github.com/coredhcp/coredhcp/logger"
  35. "github.com/coredhcp/coredhcp/plugins"
  36. "github.com/insomniacslk/dhcp/dhcpv4"
  37. "github.com/insomniacslk/dhcp/dhcpv6"
  38. )
  39. var log = logger.GetLogger("plugins/file")
  40. func init() {
  41. plugins.RegisterPlugin("file", setupFile6, setupFile4)
  42. }
  43. // StaticRecords holds a MAC -> IP address mapping
  44. var StaticRecords map[string]net.IP
  45. // DHCPv6Records and DHCPv4Records are mappings between MAC addresses in
  46. // form of a string, to network configurations.
  47. var (
  48. DHCPv6Records map[string]net.IP
  49. DHCPv4Records map[string]net.IP
  50. )
  51. // LoadDHCPv4Records loads the DHCPv4Records global map with records stored on
  52. // the specified file. The records have to be one per line, a mac address and an
  53. // IPv4 address.
  54. func LoadDHCPv4Records(filename string) (map[string]net.IP, error) {
  55. log.Infof("reading leases from %s", filename)
  56. data, err := ioutil.ReadFile(filename)
  57. if err != nil {
  58. return nil, err
  59. }
  60. records := make(map[string]net.IP)
  61. for _, lineBytes := range bytes.Split(data, []byte{'\n'}) {
  62. line := string(lineBytes)
  63. if len(line) == 0 {
  64. continue
  65. }
  66. tokens := strings.Fields(line)
  67. if len(tokens) != 2 {
  68. return nil, fmt.Errorf("malformed line, want 2 fields, got %d: %s", len(tokens), line)
  69. }
  70. hwaddr, err := net.ParseMAC(tokens[0])
  71. if err != nil {
  72. return nil, fmt.Errorf("malformed hardware address: %s", tokens[0])
  73. }
  74. ipaddr := net.ParseIP(tokens[1])
  75. if ipaddr.To4() == nil {
  76. return nil, fmt.Errorf("expected an IPv4 address, got: %v", ipaddr)
  77. }
  78. records[hwaddr.String()] = ipaddr
  79. }
  80. return records, nil
  81. }
  82. // LoadDHCPv6Records loads the DHCPv6Records global map with records stored on
  83. // the specified file. The records have to be one per line, a mac address and an
  84. // IPv6 address.
  85. func LoadDHCPv6Records(filename string) (map[string]net.IP, error) {
  86. log.Infof("reading leases from %s", filename)
  87. data, err := ioutil.ReadFile(filename)
  88. if err != nil {
  89. return nil, err
  90. }
  91. records := make(map[string]net.IP)
  92. // TODO ignore comments
  93. for _, lineBytes := range bytes.Split(data, []byte{'\n'}) {
  94. line := string(lineBytes)
  95. if len(line) == 0 {
  96. continue
  97. }
  98. tokens := strings.Fields(line)
  99. if len(tokens) != 2 {
  100. return nil, fmt.Errorf("malformed line: %s", line)
  101. }
  102. hwaddr, err := net.ParseMAC(tokens[0])
  103. if err != nil {
  104. return nil, fmt.Errorf("malformed hardware address: %s", tokens[0])
  105. }
  106. ipaddr := net.ParseIP(tokens[1])
  107. if ipaddr.To16() == nil {
  108. return nil, fmt.Errorf("expected an IPv6 address, got: %v", ipaddr)
  109. }
  110. records[hwaddr.String()] = ipaddr
  111. }
  112. return records, nil
  113. }
  114. // Handler6 handles DHCPv6 packets for the file plugin
  115. func Handler6(req, resp dhcpv6.DHCPv6) (dhcpv6.DHCPv6, bool) {
  116. mac, err := dhcpv6.ExtractMAC(req)
  117. if err != nil {
  118. log.Warningf("Could not find client MAC, passing")
  119. return resp, false
  120. }
  121. log.Debugf("looking up an IP address for MAC %s", mac.String())
  122. ipaddr, ok := StaticRecords[mac.String()]
  123. if !ok {
  124. log.Warningf("MAC address %s is unknown", mac.String())
  125. return resp, false
  126. }
  127. log.Debugf("found IP address %s for MAC %s", ipaddr, mac.String())
  128. resp.AddOption(&dhcpv6.OptIANA{
  129. // FIXME copy this field from the client, reject/drop if missing
  130. IaId: [4]byte{0xaa, 0xbb, 0xcc, 0xdd},
  131. Options: []dhcpv6.Option{
  132. &dhcpv6.OptIAAddress{
  133. IPv6Addr: ipaddr,
  134. PreferredLifetime: 3600 * time.Second,
  135. ValidLifetime: 3600 * time.Second,
  136. },
  137. },
  138. })
  139. return resp, false
  140. }
  141. // Handler4 handles DHCPv4 packets for the file plugin
  142. func Handler4(req, resp *dhcpv4.DHCPv4) (*dhcpv4.DHCPv4, bool) {
  143. ipaddr, ok := StaticRecords[req.ClientHWAddr.String()]
  144. if !ok {
  145. log.Warningf("MAC address %s is unknown", req.ClientHWAddr.String())
  146. return resp, false
  147. }
  148. resp.YourIPAddr = ipaddr
  149. log.Debugf("found IP address %s for MAC %s", ipaddr, req.ClientHWAddr.String())
  150. return resp, true
  151. }
  152. func setupFile6(args ...string) (handler.Handler6, error) {
  153. h6, _, err := setupFile(true, args...)
  154. return h6, err
  155. }
  156. func setupFile4(args ...string) (handler.Handler4, error) {
  157. _, h4, err := setupFile(false, args...)
  158. return h4, err
  159. }
  160. func setupFile(v6 bool, args ...string) (handler.Handler6, handler.Handler4, error) {
  161. var err error
  162. var records map[string]net.IP
  163. if len(args) < 1 {
  164. return nil, nil, errors.New("need a file name")
  165. }
  166. filename := args[0]
  167. if filename == "" {
  168. return nil, nil, errors.New("got empty file name")
  169. }
  170. if v6 {
  171. records, err = LoadDHCPv6Records(filename)
  172. } else {
  173. records, err = LoadDHCPv4Records(filename)
  174. }
  175. if err != nil {
  176. return nil, nil, fmt.Errorf("failed to load DHCPv6 records: %v", err)
  177. }
  178. StaticRecords = records
  179. log.Infof("loaded %d leases from %s", len(records), filename)
  180. return Handler6, Handler4, nil
  181. }