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.

fakedhcp.c 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Copyright (C) 2008 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 <stdio.h>
  27. #include <errno.h>
  28. #include <string.h>
  29. #include <ipxe/settings.h>
  30. #include <ipxe/netdevice.h>
  31. #include <ipxe/dhcppkt.h>
  32. #include <ipxe/fakedhcp.h>
  33. /** @file
  34. *
  35. * Fake DHCP packets
  36. *
  37. */
  38. /**
  39. * Copy settings to DHCP packet
  40. *
  41. * @v dest Destination DHCP packet
  42. * @v source Source settings block
  43. * @v encapsulator Encapsulating setting tag number, or zero
  44. * @ret rc Return status code
  45. */
  46. static int copy_encap_settings ( struct dhcp_packet *dest,
  47. struct settings *source,
  48. unsigned int encapsulator ) {
  49. struct setting setting = { .name = "" };
  50. unsigned int subtag;
  51. unsigned int tag;
  52. void *data;
  53. int len;
  54. int rc;
  55. for ( subtag = DHCP_MIN_OPTION; subtag <= DHCP_MAX_OPTION; subtag++ ) {
  56. tag = DHCP_ENCAP_OPT ( encapsulator, subtag );
  57. switch ( tag ) {
  58. case DHCP_EB_ENCAP:
  59. case DHCP_VENDOR_ENCAP:
  60. /* Process encapsulated settings */
  61. if ( ( rc = copy_encap_settings ( dest, source,
  62. tag ) ) != 0 )
  63. return rc;
  64. break;
  65. default:
  66. /* Copy setting, if present */
  67. setting.tag = tag;
  68. len = fetch_raw_setting_copy ( source, &setting, &data);
  69. if ( len >= 0 ) {
  70. rc = dhcppkt_store ( dest, tag, data, len );
  71. free ( data );
  72. if ( rc != 0 )
  73. return rc;
  74. }
  75. break;
  76. }
  77. }
  78. return 0;
  79. }
  80. /**
  81. * Copy settings to DHCP packet
  82. *
  83. * @v dest Destination DHCP packet
  84. * @v source Source settings block
  85. * @ret rc Return status code
  86. */
  87. static int copy_settings ( struct dhcp_packet *dest,
  88. struct settings *source ) {
  89. return copy_encap_settings ( dest, source, 0 );
  90. }
  91. /**
  92. * Create fake DHCPDISCOVER packet
  93. *
  94. * @v netdev Network device
  95. * @v data Buffer for DHCP packet
  96. * @v max_len Size of DHCP packet buffer
  97. * @ret rc Return status code
  98. *
  99. * Used by external code.
  100. */
  101. int create_fakedhcpdiscover ( struct net_device *netdev,
  102. void *data, size_t max_len ) {
  103. struct dhcp_packet dhcppkt;
  104. struct in_addr ciaddr = { 0 };
  105. int rc;
  106. if ( ( rc = dhcp_create_request ( &dhcppkt, netdev, DHCPDISCOVER,
  107. dhcp_last_xid, ciaddr, data,
  108. max_len ) ) != 0 ) {
  109. DBG ( "Could not create DHCPDISCOVER: %s\n",
  110. strerror ( rc ) );
  111. return rc;
  112. }
  113. return 0;
  114. }
  115. /**
  116. * Create fake DHCPACK packet
  117. *
  118. * @v netdev Network device
  119. * @v data Buffer for DHCP packet
  120. * @v max_len Size of DHCP packet buffer
  121. * @ret rc Return status code
  122. *
  123. * Used by external code.
  124. */
  125. int create_fakedhcpack ( struct net_device *netdev,
  126. void *data, size_t max_len ) {
  127. struct dhcp_packet dhcppkt;
  128. int rc;
  129. /* Create base DHCPACK packet */
  130. if ( ( rc = dhcp_create_packet ( &dhcppkt, netdev, DHCPACK,
  131. dhcp_last_xid, NULL, 0,
  132. data, max_len ) ) != 0 ) {
  133. DBG ( "Could not create DHCPACK: %s\n", strerror ( rc ) );
  134. return rc;
  135. }
  136. /* Merge in globally-scoped settings, then netdev-specific
  137. * settings. Do it in this order so that netdev-specific
  138. * settings take precedence regardless of stated priorities.
  139. */
  140. if ( ( rc = copy_settings ( &dhcppkt, NULL ) ) != 0 ) {
  141. DBG ( "Could not set DHCPACK global settings: %s\n",
  142. strerror ( rc ) );
  143. return rc;
  144. }
  145. if ( ( rc = copy_settings ( &dhcppkt,
  146. netdev_settings ( netdev ) ) ) != 0 ) {
  147. DBG ( "Could not set DHCPACK netdev settings: %s\n",
  148. strerror ( rc ) );
  149. return rc;
  150. }
  151. return 0;
  152. }
  153. /**
  154. * Create fake PXE Boot Server ACK packet
  155. *
  156. * @v netdev Network device
  157. * @v data Buffer for DHCP packet
  158. * @v max_len Size of DHCP packet buffer
  159. * @ret rc Return status code
  160. *
  161. * Used by external code.
  162. */
  163. int create_fakepxebsack ( struct net_device *netdev,
  164. void *data, size_t max_len ) {
  165. struct dhcp_packet dhcppkt;
  166. struct settings *proxy_settings;
  167. struct settings *pxebs_settings;
  168. int rc;
  169. /* Identify available settings */
  170. proxy_settings = find_settings ( PROXYDHCP_SETTINGS_NAME );
  171. pxebs_settings = find_settings ( PXEBS_SETTINGS_NAME );
  172. if ( ( ! proxy_settings ) && ( ! pxebs_settings ) ) {
  173. /* No PXE boot server; return the regular DHCPACK */
  174. return create_fakedhcpack ( netdev, data, max_len );
  175. }
  176. /* Create base DHCPACK packet */
  177. if ( ( rc = dhcp_create_packet ( &dhcppkt, netdev, DHCPACK,
  178. dhcp_last_xid, NULL, 0,
  179. data, max_len ) ) != 0 ) {
  180. DBG ( "Could not create PXE BS ACK: %s\n",
  181. strerror ( rc ) );
  182. return rc;
  183. }
  184. /* Populate ciaddr */
  185. fetch_ipv4_setting ( netdev_settings ( netdev ), &ip_setting,
  186. &dhcppkt.dhcphdr->ciaddr );
  187. /* Merge in ProxyDHCP options */
  188. if ( proxy_settings &&
  189. ( ( rc = copy_settings ( &dhcppkt, proxy_settings ) ) != 0 ) ) {
  190. DBG ( "Could not copy ProxyDHCP settings: %s\n",
  191. strerror ( rc ) );
  192. return rc;
  193. }
  194. /* Merge in BootServerDHCP options, if present */
  195. if ( pxebs_settings &&
  196. ( ( rc = copy_settings ( &dhcppkt, pxebs_settings ) ) != 0 ) ) {
  197. DBG ( "Could not copy PXE BS settings: %s\n",
  198. strerror ( rc ) );
  199. return rc;
  200. }
  201. return 0;
  202. }