Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

fakedhcp.c 5.6KB

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