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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 len;
  55. /* Do nothing if no cached DHCPACK is present */
  56. if ( ! cached_dhcpack_phys ) {
  57. DBGC ( colour, "CACHEDHCP found no cached DHCPACK\n" );
  58. return;
  59. }
  60. /* No reliable way to determine length before parsing packet;
  61. * start by assuming maximum length permitted by PXE.
  62. */
  63. len = sizeof ( BOOTPLAYER_t );
  64. /* Allocate and populate DHCP packet */
  65. dhcppkt = zalloc ( sizeof ( *dhcppkt ) + len );
  66. if ( ! dhcppkt ) {
  67. DBGC ( colour, "CACHEDHCP could not allocate copy\n" );
  68. return;
  69. }
  70. dhcphdr = ( ( ( void * ) dhcppkt ) + sizeof ( *dhcppkt ) );
  71. copy_from_user ( dhcphdr, phys_to_user ( cached_dhcpack_phys ), 0,
  72. len );
  73. dhcppkt_init ( dhcppkt, dhcphdr, len );
  74. /* Resize packet to required length. If reallocation fails,
  75. * just continue to use the original packet.
  76. */
  77. len = dhcppkt_len ( dhcppkt );
  78. tmp = realloc ( dhcppkt, ( sizeof ( *dhcppkt ) + len ) );
  79. if ( tmp )
  80. dhcppkt = tmp;
  81. /* Reinitialise packet at new address */
  82. dhcphdr = ( ( ( void * ) dhcppkt ) + sizeof ( *dhcppkt ) );
  83. dhcppkt_init ( dhcppkt, dhcphdr, len );
  84. /* Store as cached DHCPACK, and mark original copy as consumed */
  85. DBGC ( colour, "CACHEDHCP found cached DHCPACK at %08x+%zx\n",
  86. cached_dhcpack_phys, len );
  87. cached_dhcpack = dhcppkt;
  88. cached_dhcpack_phys = 0;
  89. }
  90. /**
  91. * Cached DHCPACK startup function
  92. *
  93. */
  94. static void cachedhcp_startup ( void ) {
  95. /* If cached DHCP packet was not claimed by any network device
  96. * during startup, then free it.
  97. */
  98. if ( cached_dhcpack ) {
  99. DBGC ( colour, "CACHEDHCP freeing unclaimed cached DHCPACK\n" );
  100. dhcppkt_put ( cached_dhcpack );
  101. cached_dhcpack = NULL;
  102. }
  103. }
  104. /** Cached DHCPACK initialisation function */
  105. struct init_fn cachedhcp_init_fn __init_fn ( INIT_NORMAL ) = {
  106. .initialise = cachedhcp_init,
  107. };
  108. /** Cached DHCPACK startup function */
  109. struct startup_fn cachedhcp_startup_fn __startup_fn ( STARTUP_LATE ) = {
  110. .startup = cachedhcp_startup,
  111. };
  112. /**
  113. * Apply cached DHCPACK to network device, if applicable
  114. *
  115. * @v netdev Network device
  116. * @ret rc Return status code
  117. */
  118. static int cachedhcp_probe ( struct net_device *netdev ) {
  119. struct ll_protocol *ll_protocol = netdev->ll_protocol;
  120. int rc;
  121. /* Do nothing unless we have a cached DHCPACK */
  122. if ( ! cached_dhcpack )
  123. return 0;
  124. /* Do nothing unless cached DHCPACK's MAC address matches this
  125. * network device.
  126. */
  127. if ( memcmp ( netdev->ll_addr, cached_dhcpack->dhcphdr->chaddr,
  128. ll_protocol->ll_addr_len ) != 0 ) {
  129. DBGC ( colour, "CACHEDHCP cached DHCPACK does not match %s\n",
  130. netdev->name );
  131. return 0;
  132. }
  133. DBGC ( colour, "CACHEDHCP cached DHCPACK is for %s\n", netdev->name );
  134. /* Register as DHCP settings for this network device */
  135. if ( ( rc = register_settings ( &cached_dhcpack->settings,
  136. netdev_settings ( netdev ),
  137. DHCP_SETTINGS_NAME ) ) != 0 ) {
  138. DBGC ( colour, "CACHEDHCP could not register settings: %s\n",
  139. strerror ( rc ) );
  140. return rc;
  141. }
  142. /* Claim cached DHCPACK */
  143. dhcppkt_put ( cached_dhcpack );
  144. cached_dhcpack = NULL;
  145. return 0;
  146. }
  147. /** Cached DHCP packet network device driver */
  148. struct net_driver cachedhcp_driver __net_driver = {
  149. .name = "cachedhcp",
  150. .probe = cachedhcp_probe,
  151. };