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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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/netdevice.h>
  30. #include <ipxe/dhcp.h>
  31. #include <ipxe/dhcpopts.h>
  32. #include <ipxe/dhcppkt.h>
  33. /** @file
  34. *
  35. * DHCP packets
  36. *
  37. */
  38. /****************************************************************************
  39. *
  40. * DHCP packet raw interface
  41. *
  42. */
  43. /**
  44. * Calculate used length of an IPv4 field within a DHCP packet
  45. *
  46. * @v data Field data
  47. * @v len Length of field
  48. * @ret used Used length of field
  49. */
  50. static size_t used_len_ipv4 ( const void *data, size_t len __unused ) {
  51. const struct in_addr *in = data;
  52. return ( in->s_addr ? sizeof ( *in ) : 0 );
  53. }
  54. /**
  55. * Calculate used length of a string field within a DHCP packet
  56. *
  57. * @v data Field data
  58. * @v len Length of field
  59. * @ret used Used length of field
  60. */
  61. static size_t used_len_string ( const void *data, size_t len ) {
  62. return strnlen ( data, len );
  63. }
  64. /** A dedicated field within a DHCP packet */
  65. struct dhcp_packet_field {
  66. /** Settings tag number */
  67. unsigned int tag;
  68. /** Offset within DHCP packet */
  69. uint16_t offset;
  70. /** Length of field */
  71. uint16_t len;
  72. /** Calculate used length of field
  73. *
  74. * @v data Field data
  75. * @v len Length of field
  76. * @ret used Used length of field
  77. */
  78. size_t ( * used_len ) ( const void *data, size_t len );
  79. };
  80. /** Declare a dedicated field within a DHCP packet
  81. *
  82. * @v _tag Settings tag number
  83. * @v _field Field name
  84. * @v _used_len Function to calculate used length of field
  85. */
  86. #define DHCP_PACKET_FIELD( _tag, _field, _used_len ) { \
  87. .tag = (_tag), \
  88. .offset = offsetof ( struct dhcphdr, _field ), \
  89. .len = sizeof ( ( ( struct dhcphdr * ) 0 )->_field ), \
  90. .used_len = _used_len, \
  91. }
  92. /** Dedicated fields within a DHCP packet */
  93. static struct dhcp_packet_field dhcp_packet_fields[] = {
  94. DHCP_PACKET_FIELD ( DHCP_EB_YIADDR, yiaddr, used_len_ipv4 ),
  95. DHCP_PACKET_FIELD ( DHCP_EB_SIADDR, siaddr, used_len_ipv4 ),
  96. DHCP_PACKET_FIELD ( DHCP_TFTP_SERVER_NAME, sname, used_len_string ),
  97. DHCP_PACKET_FIELD ( DHCP_BOOTFILE_NAME, file, used_len_string ),
  98. };
  99. /**
  100. * Get address of a DHCP packet field
  101. *
  102. * @v dhcphdr DHCP packet header
  103. * @v field DHCP packet field
  104. * @ret data Packet field data
  105. */
  106. static inline void * dhcp_packet_field ( struct dhcphdr *dhcphdr,
  107. struct dhcp_packet_field *field ) {
  108. return ( ( ( void * ) dhcphdr ) + field->offset );
  109. }
  110. /**
  111. * Find DHCP packet field corresponding to settings tag number
  112. *
  113. * @v tag Settings tag number
  114. * @ret field DHCP packet field, or NULL
  115. */
  116. static struct dhcp_packet_field *
  117. find_dhcp_packet_field ( unsigned int tag ) {
  118. struct dhcp_packet_field *field;
  119. unsigned int i;
  120. for ( i = 0 ; i < ( sizeof ( dhcp_packet_fields ) /
  121. sizeof ( dhcp_packet_fields[0] ) ) ; i++ ) {
  122. field = &dhcp_packet_fields[i];
  123. if ( field->tag == tag )
  124. return field;
  125. }
  126. return NULL;
  127. }
  128. /**
  129. * Check applicability of DHCP setting
  130. *
  131. * @v dhcppkt DHCP packet
  132. * @v tag Setting tag number
  133. * @ret applies Setting applies within this settings block
  134. */
  135. static int dhcppkt_applies ( struct dhcp_packet *dhcppkt __unused,
  136. unsigned int tag ) {
  137. return dhcpopt_applies ( tag );
  138. }
  139. /**
  140. * Store value of DHCP packet setting
  141. *
  142. * @v dhcppkt DHCP packet
  143. * @v tag Setting tag number
  144. * @v data Setting data, or NULL to clear setting
  145. * @v len Length of setting data
  146. * @ret rc Return status code
  147. */
  148. int dhcppkt_store ( struct dhcp_packet *dhcppkt, unsigned int tag,
  149. const void *data, size_t len ) {
  150. struct dhcp_packet_field *field;
  151. void *field_data;
  152. /* If this is a special field, fill it in */
  153. if ( ( field = find_dhcp_packet_field ( tag ) ) != NULL ) {
  154. if ( len > field->len )
  155. return -ENOSPC;
  156. field_data = dhcp_packet_field ( dhcppkt->dhcphdr, field );
  157. memset ( field_data, 0, field->len );
  158. memcpy ( dhcp_packet_field ( dhcppkt->dhcphdr, field ),
  159. data, len );
  160. /* Erase any equivalent option from the options block */
  161. dhcpopt_store ( &dhcppkt->options, tag, NULL, 0 );
  162. return 0;
  163. }
  164. /* Otherwise, use the generic options block */
  165. return dhcpopt_store ( &dhcppkt->options, tag, data, len );
  166. }
  167. /**
  168. * Fetch value of DHCP packet setting
  169. *
  170. * @v dhcppkt DHCP packet
  171. * @v tag Setting tag number
  172. * @v data Buffer to fill with setting data
  173. * @v len Length of buffer
  174. * @ret len Length of setting data, or negative error
  175. */
  176. int dhcppkt_fetch ( struct dhcp_packet *dhcppkt, unsigned int tag,
  177. void *data, size_t len ) {
  178. struct dhcp_packet_field *field;
  179. void *field_data;
  180. size_t field_len = 0;
  181. /* Identify special field, if any */
  182. if ( ( field = find_dhcp_packet_field ( tag ) ) != NULL ) {
  183. field_data = dhcp_packet_field ( dhcppkt->dhcphdr, field );
  184. field_len = field->used_len ( field_data, field->len );
  185. }
  186. /* Return special field, if it exists and is populated */
  187. if ( field_len ) {
  188. if ( len > field_len )
  189. len = field_len;
  190. memcpy ( data, field_data, len );
  191. return field_len;
  192. }
  193. /* Otherwise, use the generic options block */
  194. return dhcpopt_fetch ( &dhcppkt->options, tag, data, len );
  195. }
  196. /****************************************************************************
  197. *
  198. * DHCP packet settings interface
  199. *
  200. */
  201. /**
  202. * Check applicability of DHCP setting
  203. *
  204. * @v settings Settings block
  205. * @v setting Setting
  206. * @ret applies Setting applies within this settings block
  207. */
  208. static int dhcppkt_settings_applies ( struct settings *settings,
  209. const struct setting *setting ) {
  210. struct dhcp_packet *dhcppkt =
  211. container_of ( settings, struct dhcp_packet, settings );
  212. return ( ( setting->scope == NULL ) &&
  213. dhcppkt_applies ( dhcppkt, setting->tag ) );
  214. }
  215. /**
  216. * Store value of DHCP setting
  217. *
  218. * @v settings Settings block
  219. * @v setting Setting to store
  220. * @v data Setting data, or NULL to clear setting
  221. * @v len Length of setting data
  222. * @ret rc Return status code
  223. */
  224. static int dhcppkt_settings_store ( struct settings *settings,
  225. const struct setting *setting,
  226. const void *data, size_t len ) {
  227. struct dhcp_packet *dhcppkt =
  228. container_of ( settings, struct dhcp_packet, settings );
  229. return dhcppkt_store ( dhcppkt, setting->tag, data, len );
  230. }
  231. /**
  232. * Fetch value of DHCP setting
  233. *
  234. * @v settings Settings block, or NULL to search all blocks
  235. * @v setting Setting to fetch
  236. * @v data Buffer to fill with setting data
  237. * @v len Length of buffer
  238. * @ret len Length of setting data, or negative error
  239. */
  240. static int dhcppkt_settings_fetch ( struct settings *settings,
  241. struct setting *setting,
  242. void *data, size_t len ) {
  243. struct dhcp_packet *dhcppkt =
  244. container_of ( settings, struct dhcp_packet, settings );
  245. return dhcppkt_fetch ( dhcppkt, setting->tag, data, len );
  246. }
  247. /** DHCP settings operations */
  248. static struct settings_operations dhcppkt_settings_operations = {
  249. .applies = dhcppkt_settings_applies,
  250. .store = dhcppkt_settings_store,
  251. .fetch = dhcppkt_settings_fetch,
  252. };
  253. /****************************************************************************
  254. *
  255. * Constructor
  256. *
  257. */
  258. /**
  259. * Initialise DHCP packet
  260. *
  261. * @v dhcppkt DHCP packet structure to fill in
  262. * @v data DHCP packet raw data
  263. * @v max_len Length of raw data buffer
  264. *
  265. * Initialise a DHCP packet structure from a data buffer containing a
  266. * DHCP packet.
  267. */
  268. void dhcppkt_init ( struct dhcp_packet *dhcppkt, struct dhcphdr *data,
  269. size_t len ) {
  270. ref_init ( &dhcppkt->refcnt, NULL );
  271. dhcppkt->dhcphdr = data;
  272. dhcpopt_init ( &dhcppkt->options, &dhcppkt->dhcphdr->options,
  273. ( len - offsetof ( struct dhcphdr, options ) ),
  274. dhcpopt_no_realloc );
  275. settings_init ( &dhcppkt->settings, &dhcppkt_settings_operations,
  276. &dhcppkt->refcnt, NULL );
  277. }