plugin.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 nil, true
  27. }
  28. msg, err := req.GetInnerMessage()
  29. if err != nil {
  30. // BUG: this should already have failed in the main handler. Abort
  31. log.Error(err)
  32. return nil, true
  33. }
  34. if opt := msg.GetOneOption(dhcpv6.OptionServerID); opt != nil {
  35. // RFC8415 §16.{2,5,7}
  36. // These message types MUST be discarded if they contain *any* ServerID option
  37. if msg.Type() == dhcpv6.MessageTypeSolicit ||
  38. msg.Type() == dhcpv6.MessageTypeConfirm ||
  39. msg.Type() == dhcpv6.MessageTypeRebind {
  40. return nil, true
  41. }
  42. // Approximately all others MUST be discarded if the ServerID doesn't match
  43. sid := opt.(*dhcpv6.OptServerId)
  44. if !sid.Sid.Equal(*v6ServerID) {
  45. log.Infof("requested server ID does not match this server's ID. Got %v, want %v", sid.Sid, *v6ServerID)
  46. return nil, true
  47. }
  48. } else {
  49. // RFC8415 §16.{6,8,10,11}
  50. // These message types MUST be discarded if they *don't* contain a ServerID option
  51. if msg.Type() == dhcpv6.MessageTypeRequest ||
  52. msg.Type() == dhcpv6.MessageTypeRenew ||
  53. msg.Type() == dhcpv6.MessageTypeDecline ||
  54. msg.Type() == dhcpv6.MessageTypeRelease {
  55. return nil, true
  56. }
  57. }
  58. dhcpv6.WithServerID(*v6ServerID)(resp)
  59. return resp, false
  60. }
  61. // Handler4 handles DHCPv4 packets for the server_id plugin.
  62. func Handler4(req, resp *dhcpv4.DHCPv4) (*dhcpv4.DHCPv4, bool) {
  63. if v4ServerID == nil {
  64. log.Fatal("BUG: Plugin is running uninitialized!")
  65. return nil, true
  66. }
  67. if req.OpCode != dhcpv4.OpcodeBootRequest {
  68. log.Warningf("not a BootRequest, ignoring")
  69. return resp, false
  70. }
  71. if req.ServerIPAddr != nil &&
  72. !req.ServerIPAddr.Equal(net.IPv4zero) &&
  73. !req.ServerIPAddr.Equal(v4ServerID) {
  74. // This request is not for us, drop it.
  75. log.Infof("requested server ID does not match this server's ID. Got %v, want %v", req.ServerIPAddr, v4ServerID)
  76. return nil, true
  77. }
  78. resp.ServerIPAddr = make(net.IP, net.IPv4len)
  79. copy(resp.ServerIPAddr[:], v4ServerID)
  80. resp.UpdateOption(dhcpv4.OptServerIdentifier(v4ServerID))
  81. return resp, false
  82. }
  83. func setupServerID4(args ...string) (handler.Handler4, error) {
  84. log.Print("loading `server_id` plugin for DHCPv4")
  85. if len(args) < 1 {
  86. return nil, errors.New("need an argument")
  87. }
  88. serverID := net.ParseIP(args[0])
  89. if serverID == nil {
  90. return nil, errors.New("invalid or empty IP address")
  91. }
  92. if serverID.To4() == nil {
  93. return nil, errors.New("not a valid IPv4 address")
  94. }
  95. v4ServerID = serverID
  96. return Handler4, nil
  97. }
  98. func setupServerID6(args ...string) (handler.Handler6, error) {
  99. log.Print("loading `server_id` plugin for DHCPv6")
  100. if len(args) < 2 {
  101. return nil, errors.New("need a DUID type and value")
  102. }
  103. duidType := args[0]
  104. if duidType == "" {
  105. return nil, errors.New("got empty DUID type")
  106. }
  107. duidValue := args[1]
  108. if duidValue == "" {
  109. return nil, errors.New("got empty DUID value")
  110. }
  111. duidType = strings.ToLower(duidType)
  112. hwaddr, err := net.ParseMAC(duidValue)
  113. if err != nil {
  114. return nil, err
  115. }
  116. switch duidType {
  117. case "ll", "duid-ll", "duid_ll":
  118. v6ServerID = &dhcpv6.Duid{
  119. Type: dhcpv6.DUID_LL,
  120. // sorry, only ethernet for now
  121. HwType: iana.HWTypeEthernet,
  122. LinkLayerAddr: hwaddr,
  123. }
  124. case "llt", "duid-llt", "duid_llt":
  125. v6ServerID = &dhcpv6.Duid{
  126. Type: dhcpv6.DUID_LLT,
  127. // sorry, zero-time for now
  128. Time: 0,
  129. // sorry, only ethernet for now
  130. HwType: iana.HWTypeEthernet,
  131. LinkLayerAddr: hwaddr,
  132. }
  133. case "en", "uuid":
  134. return nil, errors.New("EN/UUID DUID type not supported yet")
  135. default:
  136. return nil, errors.New("Opaque DUID type not supported yet")
  137. }
  138. log.Printf("using %s %s", duidType, duidValue)
  139. return Handler6, nil
  140. }