plugin.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package serverid
  2. import (
  3. "errors"
  4. "net"
  5. "strings"
  6. "github.com/coredhcp/coredhcp/handler"
  7. "github.com/coredhcp/coredhcp/logger"
  8. "github.com/coredhcp/coredhcp/plugins"
  9. "github.com/insomniacslk/dhcp/dhcpv4"
  10. "github.com/insomniacslk/dhcp/dhcpv6"
  11. "github.com/insomniacslk/dhcp/iana"
  12. )
  13. var log = logger.GetLogger("plugins/server_id")
  14. func init() {
  15. plugins.RegisterPlugin("server_id", setupServerID6, setupServerID4)
  16. }
  17. // V6ServerID is the DUID of the v6 server
  18. var (
  19. V6ServerID *dhcpv6.Duid
  20. V4ServerID net.IP
  21. )
  22. // Handler6 handles DHCPv6 packets for the server_id plugin.
  23. func Handler6(req, resp dhcpv6.DHCPv6) (dhcpv6.DHCPv6, bool) {
  24. if V6ServerID == nil {
  25. return resp, false
  26. }
  27. if opt := req.GetOneOption(dhcpv6.OptionServerID); opt != nil {
  28. sid := opt.(*dhcpv6.OptServerId)
  29. if !sid.Sid.Equal(*V6ServerID) {
  30. log.Infof("requested server ID does not match this server's ID. Got %v, want %v", sid.Sid, *V6ServerID)
  31. return nil, true
  32. }
  33. }
  34. dhcpv6.WithServerID(*V6ServerID)(resp)
  35. return resp, false
  36. }
  37. // Handler4 handles DHCPv4 packets for the server_id plugin.
  38. func Handler4(req, resp *dhcpv4.DHCPv4) (*dhcpv4.DHCPv4, bool) {
  39. if V4ServerID == nil || resp == nil {
  40. return resp, false
  41. }
  42. if req.OpCode != dhcpv4.OpcodeBootRequest {
  43. log.Warningf("not a BootRequest, ignoring")
  44. return resp, false
  45. }
  46. if req.ServerIPAddr != nil &&
  47. !req.ServerIPAddr.Equal(net.IPv4zero) &&
  48. !req.ServerIPAddr.Equal(V4ServerID) {
  49. // This request is not for us, drop it.
  50. log.Infof("requested server ID does not match this server's ID. Got %v, want %v", req.ServerIPAddr, V4ServerID)
  51. return nil, true
  52. }
  53. resp.ServerIPAddr = make(net.IP, net.IPv4len)
  54. copy(resp.ServerIPAddr[:], V4ServerID)
  55. resp.UpdateOption(dhcpv4.OptServerIdentifier(V4ServerID))
  56. return resp, false
  57. }
  58. func setupServerID4(args ...string) (handler.Handler4, error) {
  59. log.Print("loading `server_id` plugin for DHCPv4")
  60. if len(args) < 1 {
  61. return nil, errors.New("need an argument")
  62. }
  63. serverID := net.ParseIP(args[0])
  64. if serverID == nil {
  65. return nil, errors.New("invalid or empty IP address")
  66. }
  67. if serverID.To4() == nil {
  68. return nil, errors.New("not a valid IPv4 address")
  69. }
  70. V4ServerID = serverID
  71. return Handler4, nil
  72. }
  73. func setupServerID6(args ...string) (handler.Handler6, error) {
  74. log.Print("loading `server_id` plugin for DHCPv6")
  75. if len(args) < 2 {
  76. return nil, errors.New("need a DUID type and value")
  77. }
  78. duidType := args[0]
  79. if duidType == "" {
  80. return nil, errors.New("got empty DUID type")
  81. }
  82. duidValue := args[1]
  83. if duidValue == "" {
  84. return nil, errors.New("got empty DUID value")
  85. }
  86. duidType = strings.ToLower(duidType)
  87. hwaddr, err := net.ParseMAC(duidValue)
  88. if err != nil {
  89. return nil, err
  90. }
  91. switch duidType {
  92. case "ll", "duid-ll", "duid_ll":
  93. V6ServerID = &dhcpv6.Duid{
  94. Type: dhcpv6.DUID_LL,
  95. // sorry, only ethernet for now
  96. HwType: iana.HWTypeEthernet,
  97. LinkLayerAddr: hwaddr,
  98. }
  99. case "llt", "duid-llt", "duid_llt":
  100. V6ServerID = &dhcpv6.Duid{
  101. Type: dhcpv6.DUID_LLT,
  102. // sorry, zero-time for now
  103. Time: 0,
  104. // sorry, only ethernet for now
  105. HwType: iana.HWTypeEthernet,
  106. LinkLayerAddr: hwaddr,
  107. }
  108. case "en", "uuid":
  109. return nil, errors.New("EN/UUID DUID type not supported yet")
  110. default:
  111. return nil, errors.New("Opaque DUID type not supported yet")
  112. }
  113. log.Printf("using %s %s", duidType, duidValue)
  114. return Handler6, nil
  115. }