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.2KB

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