plugin.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package clientport
  2. import (
  3. "bytes"
  4. "errors"
  5. "fmt"
  6. "io/ioutil"
  7. "net"
  8. "strings"
  9. "github.com/coredhcp/coredhcp/handler"
  10. "github.com/coredhcp/coredhcp/logger"
  11. "github.com/coredhcp/coredhcp/plugins"
  12. "github.com/insomniacslk/dhcp/dhcpv4"
  13. "github.com/insomniacslk/dhcp/dhcpv6"
  14. )
  15. var log = logger.GetLogger()
  16. func init() {
  17. plugins.RegisterPlugin("file", setupFile6, setupFile4)
  18. }
  19. // StaticRecords holds a MAC -> IP address mapping
  20. var StaticRecords map[string]net.IP
  21. // DHCPv6Records and DHCPv4Records are mappings between MAC addresses in
  22. // form of a string, to network configurations.
  23. var (
  24. DHCPv6Records map[string]net.IP
  25. DHCPv4Records map[string]net.IP
  26. )
  27. var serverID *dhcpv6.OptServerId
  28. // LoadDHCPv6Records loads the DHCPv6Records global map with records stored on
  29. // the specified file. The records have to be one per line, a mac address and an
  30. // IPv6 address.
  31. func LoadDHCPv6Records(filename string) (map[string]net.IP, error) {
  32. log.Printf("plugins/file: reading leases from %s", filename)
  33. data, err := ioutil.ReadFile(filename)
  34. if err != nil {
  35. return nil, err
  36. }
  37. records := make(map[string]net.IP)
  38. // TODO ignore comments
  39. for _, lineBytes := range bytes.Split(data, []byte{'\n'}) {
  40. line := string(lineBytes)
  41. if len(line) == 0 {
  42. continue
  43. }
  44. tokens := strings.Fields(line)
  45. if len(tokens) != 2 {
  46. return nil, fmt.Errorf("plugins/file: malformed line: %s", line)
  47. }
  48. hwaddr, err := net.ParseMAC(tokens[0])
  49. if err != nil {
  50. return nil, fmt.Errorf("plugins/file: malformed hardware address: %s", tokens[0])
  51. }
  52. ipaddr := net.ParseIP(tokens[1])
  53. if ipaddr.To16() == nil {
  54. return nil, fmt.Errorf("plugins/file: expected an IPv6 address, got: %v", ipaddr)
  55. }
  56. records[hwaddr.String()] = ipaddr
  57. }
  58. return records, nil
  59. }
  60. // Handler6 handles DHCPv6 packets for the file plugin
  61. func Handler6(req, resp dhcpv6.DHCPv6) (dhcpv6.DHCPv6, bool) {
  62. mac, err := dhcpv6.ExtractMAC(req)
  63. if err != nil {
  64. return nil, false
  65. }
  66. log.Printf("plugins/file: looking up an IP address for MAC %s", mac.String())
  67. ipaddr, ok := StaticRecords[mac.String()]
  68. if !ok {
  69. log.Warningf("plugins/file: MAC address %s is unknown", mac.String())
  70. return nil, false
  71. }
  72. log.Printf("plugins/file: found IP address %s for MAC %s", ipaddr, mac.String())
  73. resp.AddOption(&dhcpv6.OptIANA{
  74. // FIXME this must be unique per client address
  75. IaId: [4]byte{0xaa, 0xbb, 0xcc, 0xdd},
  76. Options: []dhcpv6.Option{
  77. &dhcpv6.OptIAAddress{
  78. IPv6Addr: ipaddr,
  79. PreferredLifetime: 3600,
  80. ValidLifetime: 3600,
  81. },
  82. },
  83. })
  84. resp.AddOption(&dhcpv6.OptDNSRecursiveNameServer{
  85. NameServers: []net.IP{
  86. // FIXME this must be read from the config file
  87. net.ParseIP("2001:4860:4860::8888"),
  88. net.ParseIP("2001:4860:4860::4444"),
  89. },
  90. })
  91. if oro := req.GetOption(dhcpv6.OptionORO); len(oro) > 0 {
  92. for _, code := range oro[0].(*dhcpv6.OptRequestedOption).RequestedOptions() {
  93. if code == dhcpv6.OptionBootfileURL {
  94. // bootfile URL is requested
  95. resp.AddOption(
  96. &dhcpv6.OptBootFileURL{BootFileURL: []byte("http://[2001:db8::0:1]/nbp")},
  97. )
  98. }
  99. }
  100. }
  101. return resp, true
  102. }
  103. // Handler4 handles DHCPv4 packets for the file plugin
  104. func Handler4(req, resp *dhcpv4.DHCPv4) (*dhcpv4.DHCPv4, bool) {
  105. // TODO check the MAC address in the request
  106. // if it is present in StaticRecords, forge a response
  107. // and stop processing.
  108. return resp, true
  109. }
  110. func setupFile6(args ...string) (handler.Handler6, error) {
  111. h6, _, err := setupFile(true, args...)
  112. return h6, err
  113. }
  114. func setupFile4(args ...string) (handler.Handler4, error) {
  115. log.Print("plugins/file: loading `file` plugin for DHCPv4")
  116. return nil, nil
  117. }
  118. func setupFile(v6 bool, args ...string) (handler.Handler6, handler.Handler4, error) {
  119. if len(args) < 1 {
  120. return nil, nil, errors.New("plugins/file: need a file name")
  121. }
  122. filename := args[0]
  123. if filename == "" {
  124. return nil, nil, errors.New("plugins/file: got empty file name")
  125. }
  126. records, err := LoadDHCPv6Records(filename)
  127. if err != nil {
  128. return nil, nil, fmt.Errorf("plugins/file: failed to load DHCPv6 records: %v", err)
  129. }
  130. log.Printf("plugins/file: loaded %d leases from %s", len(records), filename)
  131. StaticRecords = records
  132. return Handler6, Handler4, nil
  133. }