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.

dhcppkt.c 8.3KB

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