nbp.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. // Plugin wraps plugin registration information
  40. var Plugin = plugins.Plugin{
  41. Name: "nbp",
  42. Setup6: setup6,
  43. Setup4: setup4,
  44. }
  45. var (
  46. opt59, opt60 dhcpv6.Option
  47. opt66, opt67 *dhcpv4.Option
  48. )
  49. func parseArgs(args ...string) (*url.URL, error) {
  50. if len(args) != 1 {
  51. return nil, fmt.Errorf("Exactly one argument must be passed to NBP plugin, got %d", len(args))
  52. }
  53. return url.Parse(args[0])
  54. }
  55. func setup6(args ...string) (handler.Handler6, error) {
  56. u, err := parseArgs(args...)
  57. if err != nil {
  58. return nil, err
  59. }
  60. opt59 = dhcpv6.OptBootFileURL(u.String())
  61. params := u.Query().Get("params")
  62. if params != "" {
  63. opt60 = &dhcpv6.OptionGeneric{
  64. OptionCode: dhcpv6.OptionBootfileParam,
  65. OptionData: []byte(params),
  66. }
  67. }
  68. log.Printf("loaded NBP plugin for DHCPv6.")
  69. return nbpHandler6, nil
  70. }
  71. func setup4(args ...string) (handler.Handler4, error) {
  72. u, err := parseArgs(args...)
  73. if err != nil {
  74. return nil, err
  75. }
  76. otsn := dhcpv4.OptTFTPServerName(u.Host)
  77. opt66 = &otsn
  78. obfn := dhcpv4.OptBootFileName(u.Path)
  79. opt67 = &obfn
  80. log.Printf("loaded NBP plugin for DHCPv4.")
  81. return nbpHandler4, nil
  82. }
  83. func nbpHandler6(req, resp dhcpv6.DHCPv6) (dhcpv6.DHCPv6, bool) {
  84. if opt59 == nil {
  85. // nothing to do
  86. return resp, true
  87. }
  88. decap, err := req.GetInnerMessage()
  89. if err != nil {
  90. log.Errorf("Could not decapsulate request: %v", err)
  91. // drop the request, this is probably a critical error in the packet.
  92. return nil, true
  93. }
  94. for _, code := range decap.Options.RequestedOptions() {
  95. if code == dhcpv6.OptionBootfileURL {
  96. // bootfile URL is requested
  97. resp.AddOption(opt59)
  98. } else if code == dhcpv6.OptionBootfileParam {
  99. // optionally add opt60, bootfile params, if requested
  100. if opt60 != nil {
  101. resp.AddOption(opt60)
  102. }
  103. }
  104. }
  105. log.Debugf("Added NBP %s to request", opt59)
  106. return resp, true
  107. }
  108. func nbpHandler4(req, resp *dhcpv4.DHCPv4) (*dhcpv4.DHCPv4, bool) {
  109. if opt66 == nil || opt67 == nil {
  110. // nothing to do
  111. return resp, true
  112. }
  113. if req.IsOptionRequested(dhcpv4.OptionTFTPServerName) {
  114. resp.Options.Update(*opt66)
  115. }
  116. if req.IsOptionRequested(dhcpv4.OptionBootfileName) {
  117. resp.Options.Update(*opt67)
  118. }
  119. log.Debugf("Added NBP %s / %s to request", opt66, opt67)
  120. return resp, true
  121. }