nbp.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 nbp implements handling of an NBP (Network Boot Program) using an
  5. // URL, e.g. http://[fe80::abcd:efff:fe12:3456]/my-nbp or tftp://10.0.0.1/my-nbp .
  6. // The NBP information is only added if it is requested by the client.
  7. //
  8. // Note that for DHCPv4 the URL will be split into TFTP server name (option 66)
  9. // and Bootfile name (option 67), so the scheme will be stripped out, and it
  10. // will be treated as a TFTP URL. Anything other than host name and file path
  11. // will be ignored (no port, no query string, etc).
  12. //
  13. // For DHCPv6 OPT_BOOTFILE_URL (option 59) is used, and the value is passed
  14. // unmodified. If the query string is specified and contains a "param" key,
  15. // its value is also passed as OPT_BOOTFILE_PARAM (option 60), so it will be
  16. // duplicated between option 59 and 60.
  17. //
  18. // Example usage:
  19. //
  20. // server6:
  21. // - plugins:
  22. // - nbp: http://[2001:db8:a::1]/nbp
  23. //
  24. // server4:
  25. // - plugins:
  26. // - nbp: tftp://10.0.0.254/nbp
  27. //
  28. package nbp
  29. import (
  30. "fmt"
  31. "net/url"
  32. "github.com/coredhcp/coredhcp/handler"
  33. "github.com/coredhcp/coredhcp/logger"
  34. "github.com/coredhcp/coredhcp/plugins"
  35. "github.com/insomniacslk/dhcp/dhcpv4"
  36. "github.com/insomniacslk/dhcp/dhcpv6"
  37. )
  38. var log = logger.GetLogger("plugins/nbp")
  39. func init() {
  40. plugins.RegisterPlugin("nbp", setupNBP6, setupNBP4)
  41. }
  42. var (
  43. opt59, opt60 dhcpv6.Option
  44. opt66, opt67 *dhcpv4.Option
  45. )
  46. func parseArgs(args ...string) (*url.URL, error) {
  47. if len(args) != 1 {
  48. return nil, fmt.Errorf("Exactly one argument must be passed to NBP plugin, got %d", len(args))
  49. }
  50. return url.Parse(args[0])
  51. }
  52. func setupNBP6(args ...string) (handler.Handler6, error) {
  53. u, err := parseArgs(args...)
  54. if err != nil {
  55. return nil, err
  56. }
  57. opt59 = dhcpv6.OptBootFileURL(u.String())
  58. params := u.Query().Get("params")
  59. if params != "" {
  60. opt60 = &dhcpv6.OptionGeneric{
  61. OptionCode: dhcpv6.OptionBootfileParam,
  62. OptionData: []byte(params),
  63. }
  64. }
  65. log.Printf("loaded NBP plugin for DHCPv6.")
  66. return nbpHandler6, nil
  67. }
  68. func setupNBP4(args ...string) (handler.Handler4, error) {
  69. u, err := parseArgs(args...)
  70. if err != nil {
  71. return nil, err
  72. }
  73. otsn := dhcpv4.OptTFTPServerName(u.Host)
  74. opt66 = &otsn
  75. obfn := dhcpv4.OptBootFileName(u.Path)
  76. opt67 = &obfn
  77. log.Printf("loaded NBP plugin for DHCPv4.")
  78. return nbpHandler4, nil
  79. }
  80. func nbpHandler6(req, resp dhcpv6.DHCPv6) (dhcpv6.DHCPv6, bool) {
  81. if opt59 == nil {
  82. // nothing to do
  83. return resp, true
  84. }
  85. decap, err := req.GetInnerMessage()
  86. if err != nil {
  87. log.Errorf("Could not decapsulate request: %v", err)
  88. // drop the request, this is probably a critical error in the packet.
  89. return nil, true
  90. }
  91. if oro := decap.GetOption(dhcpv6.OptionORO); len(oro) > 0 {
  92. for _, code := range oro[0].(*dhcpv6.OptRequestedOption).RequestedOptions() {
  93. if code == dhcpv6.OptionBootfileURL {
  94. // bootfile URL is requested
  95. resp.AddOption(opt59)
  96. } else if code == dhcpv6.OptionBootfileParam {
  97. // optionally add opt60, bootfile params, if requested
  98. if opt60 != nil {
  99. resp.AddOption(opt60)
  100. }
  101. }
  102. }
  103. }
  104. log.Debugf("Added NBP %s to request", opt59)
  105. return resp, true
  106. }
  107. func nbpHandler4(req, resp *dhcpv4.DHCPv4) (*dhcpv4.DHCPv4, bool) {
  108. if opt66 == nil || opt67 == nil {
  109. // nothing to do
  110. return resp, true
  111. }
  112. if req.IsOptionRequested(dhcpv4.OptionTFTPServerName) {
  113. resp.Options.Update(*opt66)
  114. }
  115. if req.IsOptionRequested(dhcpv4.OptionBootfileName) {
  116. resp.Options.Update(*opt67)
  117. }
  118. log.Debugf("Added NBP %s / %s to request", opt66, opt67)
  119. return resp, true
  120. }