plugin.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 autoconfigure
  5. // This plugin implements RFC2563:
  6. // 1. If the client has been allocated an IP address, do nothing
  7. // 2. If the client has not been allocated an IP address
  8. // (yiaddr=0.0.0.0), then:
  9. // 2a. If the client has requested the "AutoConfigure" option,
  10. // then add the defined value to the response
  11. // 2b. Otherwise, terminate processing and send no reply
  12. //
  13. // This plugin should be used at the end of the plugin chain,
  14. // after any IP address allocation has taken place.
  15. //
  16. // The optional argument is the string "DoNotAutoConfigure" or
  17. // "AutoConfigure" (or "0" or "1" respectively). The default
  18. // is DoNotAutoConfigure.
  19. import (
  20. "errors"
  21. "fmt"
  22. "github.com/coredhcp/coredhcp/handler"
  23. "github.com/coredhcp/coredhcp/logger"
  24. "github.com/coredhcp/coredhcp/plugins"
  25. "github.com/insomniacslk/dhcp/dhcpv4"
  26. "github.com/sirupsen/logrus"
  27. )
  28. var log = logger.GetLogger("plugins/autoconfigure")
  29. var autoconfigure dhcpv4.AutoConfiguration
  30. var Plugin = plugins.Plugin{
  31. Name: "autoconfigure",
  32. Setup4: setup4,
  33. }
  34. var argMap = map[string]dhcpv4.AutoConfiguration{
  35. "0": dhcpv4.AutoConfiguration(0),
  36. "1": dhcpv4.AutoConfiguration(1),
  37. "DoNotAutoConfigure": dhcpv4.DoNotAutoConfigure,
  38. "AutoConfigure": dhcpv4.AutoConfigure,
  39. }
  40. func setup4(args ...string) (handler.Handler4, error) {
  41. if len(args) > 0 {
  42. var ok bool
  43. autoconfigure, ok = argMap[args[0]]
  44. if !ok {
  45. return nil, fmt.Errorf("unexpected value '%v' for autoconfigure argument", args[0])
  46. }
  47. }
  48. if len(args) > 1 {
  49. return nil, errors.New("too many arguments")
  50. }
  51. return Handler4, nil
  52. }
  53. func Handler4(req, resp *dhcpv4.DHCPv4) (*dhcpv4.DHCPv4, bool) {
  54. if resp.MessageType() != dhcpv4.MessageTypeOffer || !resp.YourIPAddr.IsUnspecified() {
  55. return resp, false
  56. }
  57. ac, ok := req.AutoConfigure()
  58. if ok {
  59. resp.UpdateOption(dhcpv4.OptAutoConfigure(autoconfigure))
  60. log.WithFields(logrus.Fields{
  61. "mac": req.ClientHWAddr.String(),
  62. "autoconfigure": fmt.Sprintf("%v", ac),
  63. }).Debugf("Responded with autoconfigure %v", autoconfigure)
  64. return resp, false
  65. }
  66. log.WithFields(logrus.Fields{
  67. "mac": req.ClientHWAddr.String(),
  68. "autoconfigure": "nil",
  69. }).Debugf("Client does not support autoconfigure")
  70. // RFC2563 2.3: if no address is chosen for the host [...]
  71. // If the DHCPDISCOVER does not contain the Auto-Configure option,
  72. // it is not answered.
  73. return nil, true
  74. }