You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cachedhcp.c 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <stdint.h>
  25. #include <stdlib.h>
  26. #include <ipxe/dhcppkt.h>
  27. #include <ipxe/init.h>
  28. #include <ipxe/netdevice.h>
  29. #include <realmode.h>
  30. #include <pxe_api.h>
  31. /** @file
  32. *
  33. * Cached DHCP packet
  34. *
  35. */
  36. /** Cached DHCPACK physical address
  37. *
  38. * This can be set by the prefix.
  39. */
  40. uint32_t __bss16 ( cached_dhcpack_phys );
  41. #define cached_dhcpack_phys __use_data16 ( cached_dhcpack_phys )
  42. /** Colour for debug messages */
  43. #define colour &cached_dhcpack_phys
  44. /** Cached DHCPACK */
  45. static struct dhcp_packet *cached_dhcpack;
  46. /**
  47. * Cached DHCPACK startup function
  48. *
  49. */
  50. static void cachedhcp_init ( void ) {
  51. struct dhcp_packet *dhcppkt;
  52. struct dhcp_packet *tmp;
  53. struct dhcphdr *dhcphdr;
  54. size_t max_len;
  55. size_t len;
  56. /* Do nothing if no cached DHCPACK is present */
  57. if ( ! cached_dhcpack_phys ) {
  58. DBGC ( colour, "CACHEDHCP found no cached DHCPACK\n" );
  59. return;
  60. }
  61. /* No reliable way to determine length before parsing packet;
  62. * start by assuming maximum length permitted by PXE.
  63. */
  64. max_len = sizeof ( BOOTPLAYER_t );
  65. /* Allocate and populate DHCP packet */
  66. dhcppkt = zalloc ( sizeof ( *dhcppkt ) + max_len );
  67. if ( ! dhcppkt ) {
  68. DBGC ( colour, "CACHEDHCP could not allocate copy\n" );
  69. return;
  70. }
  71. dhcphdr = ( ( ( void * ) dhcppkt ) + sizeof ( *dhcppkt ) );
  72. copy_from_user ( dhcphdr, phys_to_user ( cached_dhcpack_phys ), 0,
  73. max_len );
  74. dhcppkt_init ( dhcppkt, dhcphdr, max_len );
  75. /* Shrink packet to required length. If reallocation fails,
  76. * just continue to use the original packet and waste the
  77. * unused space.
  78. */
  79. len = dhcppkt_len ( dhcppkt );
  80. assert ( len <= max_len );
  81. tmp = realloc ( dhcppkt, ( sizeof ( *dhcppkt ) + len ) );
  82. if ( tmp )
  83. dhcppkt = tmp;
  84. /* Reinitialise packet at new address */
  85. dhcphdr = ( ( ( void * ) dhcppkt ) + sizeof ( *dhcppkt ) );
  86. dhcppkt_init ( dhcppkt, dhcphdr, len );
  87. /* Store as cached DHCPACK, and mark original copy as consumed */
  88. DBGC ( colour, "CACHEDHCP found cached DHCPACK at %08x+%zx\n",
  89. cached_dhcpack_phys, len );
  90. cached_dhcpack = dhcppkt;
  91. cached_dhcpack_phys = 0;
  92. }
  93. /**
  94. * Cached DHCPACK startup function
  95. *
  96. */
  97. static void cachedhcp_startup ( void ) {
  98. /* If cached DHCP packet was not claimed by any network device
  99. * during startup, then free it.
  100. */
  101. if ( cached_dhcpack ) {
  102. DBGC ( colour, "CACHEDHCP freeing unclaimed cached DHCPACK\n" );
  103. dhcppkt_put ( cached_dhcpack );
  104. cached_dhcpack = NULL;
  105. }
  106. }
  107. /** Cached DHCPACK initialisation function */
  108. struct init_fn cachedhcp_init_fn __init_fn ( INIT_NORMAL ) = {
  109. .initialise = cachedhcp_init,
  110. };
  111. /** Cached DHCPACK startup function */
  112. struct startup_fn cachedhcp_startup_fn __startup_fn ( STARTUP_LATE ) = {
  113. .name = "cachedhcp",
  114. .startup = cachedhcp_startup,
  115. };
  116. /**
  117. * Apply cached DHCPACK to network device, if applicable
  118. *
  119. * @v netdev Network device
  120. * @ret rc Return status code
  121. */
  122. static int cachedhcp_probe ( struct net_device *netdev ) {
  123. struct ll_protocol *ll_protocol = netdev->ll_protocol;
  124. int rc;
  125. /* Do nothing unless we have a cached DHCPACK */
  126. if ( ! cached_dhcpack )
  127. return 0;
  128. /* Do nothing unless cached DHCPACK's MAC address matches this
  129. * network device.
  130. */
  131. if ( memcmp ( netdev->ll_addr, cached_dhcpack->dhcphdr->chaddr,
  132. ll_protocol->ll_addr_len ) != 0 ) {
  133. DBGC ( colour, "CACHEDHCP cached DHCPACK does not match %s\n",
  134. netdev->name );
  135. return 0;
  136. }
  137. DBGC ( colour, "CACHEDHCP cached DHCPACK is for %s\n", netdev->name );
  138. /* Register as DHCP settings for this network device */
  139. if ( ( rc = register_settings ( &cached_dhcpack->settings,
  140. netdev_settings ( netdev ),
  141. DHCP_SETTINGS_NAME ) ) != 0 ) {
  142. DBGC ( colour, "CACHEDHCP could not register settings: %s\n",
  143. strerror ( rc ) );
  144. return rc;
  145. }
  146. /* Claim cached DHCPACK */
  147. dhcppkt_put ( cached_dhcpack );
  148. cached_dhcpack = NULL;
  149. return 0;
  150. }
  151. /** Cached DHCP packet network device driver */
  152. struct net_driver cachedhcp_driver __net_driver = {
  153. .name = "cachedhcp",
  154. .probe = cachedhcp_probe,
  155. };