plugin.go 3.4 KB

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