plugin.go 3.4 KB

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