Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

fakedhcp.c 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. FILE_LICENCE ( GPL2_OR_LATER );
  19. #include <stdint.h>
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <errno.h>
  23. #include <string.h>
  24. #include <ipxe/settings.h>
  25. #include <ipxe/netdevice.h>
  26. #include <ipxe/dhcppkt.h>
  27. #include <ipxe/fakedhcp.h>
  28. /** @file
  29. *
  30. * Fake DHCP packets
  31. *
  32. */
  33. /**
  34. * Copy settings to DHCP packet
  35. *
  36. * @v dest Destination DHCP packet
  37. * @v source Source settings block
  38. * @v encapsulator Encapsulating setting tag number, or zero
  39. * @ret rc Return status code
  40. */
  41. static int copy_encap_settings ( struct dhcp_packet *dest,
  42. struct settings *source,
  43. unsigned int encapsulator ) {
  44. struct setting setting = { .name = "" };
  45. unsigned int subtag;
  46. unsigned int tag;
  47. int len;
  48. int check_len;
  49. int rc;
  50. for ( subtag = DHCP_MIN_OPTION; subtag <= DHCP_MAX_OPTION; subtag++ ) {
  51. tag = DHCP_ENCAP_OPT ( encapsulator, subtag );
  52. switch ( tag ) {
  53. case DHCP_EB_ENCAP:
  54. case DHCP_VENDOR_ENCAP:
  55. /* Process encapsulated settings */
  56. if ( ( rc = copy_encap_settings ( dest, source,
  57. tag ) ) != 0 )
  58. return rc;
  59. break;
  60. default:
  61. /* Copy setting, if present */
  62. setting.tag = tag;
  63. len = fetch_setting_len ( source, &setting );
  64. if ( len < 0 )
  65. break;
  66. {
  67. char buf[len];
  68. check_len = fetch_setting ( source, &setting,
  69. buf, sizeof (buf));
  70. assert ( check_len == len );
  71. if ( ( rc = dhcppkt_store ( dest, tag, buf,
  72. sizeof(buf) )) !=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. /* Merge in ProxyDHCP options */
  185. if ( proxy_settings &&
  186. ( ( rc = copy_settings ( &dhcppkt, proxy_settings ) ) != 0 ) ) {
  187. DBG ( "Could not copy ProxyDHCP settings: %s\n",
  188. strerror ( rc ) );
  189. return rc;
  190. }
  191. /* Merge in BootServerDHCP options, if present */
  192. if ( pxebs_settings &&
  193. ( ( rc = copy_settings ( &dhcppkt, pxebs_settings ) ) != 0 ) ) {
  194. DBG ( "Could not copy PXE BS settings: %s\n",
  195. strerror ( rc ) );
  196. return rc;
  197. }
  198. return 0;
  199. }