plugin.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 ipv6only
  5. // This plugin implements RFC8925: if the client has requested the
  6. // IPv6-Only Preferred option, then add the option response and then
  7. // terminate processing immediately.
  8. //
  9. // This module should be invoked *before* any IP address
  10. // allocation has been done, so that the yiaddr is 0.0.0.0 and
  11. // no pool addresses are consumed for compatible clients.
  12. //
  13. // The optional argument is the V6ONLY_WAIT configuration variable,
  14. // described in RFC8925 section 3.2.
  15. import (
  16. "errors"
  17. "time"
  18. "github.com/coredhcp/coredhcp/handler"
  19. "github.com/coredhcp/coredhcp/logger"
  20. "github.com/coredhcp/coredhcp/plugins"
  21. "github.com/insomniacslk/dhcp/dhcpv4"
  22. "github.com/sirupsen/logrus"
  23. )
  24. var log = logger.GetLogger("plugins/ipv6only")
  25. var v6only_wait time.Duration
  26. var Plugin = plugins.Plugin{
  27. Name: "ipv6only",
  28. Setup4: setup4,
  29. }
  30. func setup4(args ...string) (handler.Handler4, error) {
  31. if len(args) > 0 {
  32. dur, err := time.ParseDuration(args[0])
  33. if err != nil {
  34. log.Errorf("invalid duration: %v", args[0])
  35. return nil, errors.New("ipv6only failed to initialize")
  36. }
  37. v6only_wait = dur
  38. }
  39. if len(args) > 1 {
  40. return nil, errors.New("too many arguments")
  41. }
  42. return Handler4, nil
  43. }
  44. func Handler4(req, resp *dhcpv4.DHCPv4) (*dhcpv4.DHCPv4, bool) {
  45. v6pref := req.IsOptionRequested(dhcpv4.OptionIPv6OnlyPreferred)
  46. log.WithFields(logrus.Fields{
  47. "mac": req.ClientHWAddr.String(),
  48. "ipv6only": v6pref,
  49. }).Debug("ipv6only status")
  50. if v6pref {
  51. resp.UpdateOption(dhcpv4.OptIPv6OnlyPreferred(v6only_wait))
  52. return resp, true
  53. }
  54. return resp, false
  55. }