clientpacket.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /* vi: set sw=4 ts=4: */
  2. /* clientpacket.c
  3. *
  4. * Packet generation and dispatching functions for the DHCP client.
  5. *
  6. * Russ Dill <Russ.Dill@asu.edu> July 2001
  7. *
  8. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  9. */
  10. #include "common.h"
  11. #include "dhcpd.h"
  12. #include "dhcpc.h"
  13. #include "options.h"
  14. //#include <features.h>
  15. #if (defined(__GLIBC__) && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1) || defined _NEWLIB_VERSION
  16. #include <netpacket/packet.h>
  17. #include <net/ethernet.h>
  18. #else
  19. #include <asm/types.h>
  20. #include <linux/if_packet.h>
  21. #include <linux/if_ether.h>
  22. #endif
  23. /* Create a random xid */
  24. uint32_t FAST_FUNC random_xid(void)
  25. {
  26. static smallint initialized;
  27. if (!initialized) {
  28. srand(monotonic_us());
  29. initialized = 1;
  30. }
  31. return rand();
  32. }
  33. /* Initialize the packet with the proper defaults */
  34. static void init_packet(struct dhcp_packet *packet, char type)
  35. {
  36. udhcp_init_header(packet, type);
  37. memcpy(packet->chaddr, client_config.client_mac, 6);
  38. if (client_config.clientid)
  39. add_option_string(packet->options, client_config.clientid);
  40. if (client_config.hostname)
  41. add_option_string(packet->options, client_config.hostname);
  42. if (client_config.fqdn)
  43. add_option_string(packet->options, client_config.fqdn);
  44. if ((type != DHCPDECLINE) && (type != DHCPRELEASE))
  45. add_option_string(packet->options, client_config.vendorclass);
  46. }
  47. /* Add a parameter request list for stubborn DHCP servers. Pull the data
  48. * from the struct in options.c. Don't do bounds checking here because it
  49. * goes towards the head of the packet. */
  50. static void add_param_req_option(struct dhcp_packet *packet)
  51. {
  52. uint8_t c;
  53. int end = end_option(packet->options);
  54. int i, len = 0;
  55. for (i = 0; (c = dhcp_options[i].code) != 0; i++) {
  56. if (((dhcp_options[i].flags & OPTION_REQ)
  57. && !client_config.no_default_options)
  58. || (client_config.opt_mask[c >> 3] & (1 << (c & 7)))
  59. ) {
  60. packet->options[end + OPT_DATA + len] = c;
  61. len++;
  62. }
  63. }
  64. if (len) {
  65. packet->options[end + OPT_CODE] = DHCP_PARAM_REQ;
  66. packet->options[end + OPT_LEN] = len;
  67. packet->options[end + OPT_DATA + len] = DHCP_END;
  68. }
  69. }
  70. /* RFC 2131
  71. * 4.4.4 Use of broadcast and unicast
  72. *
  73. * The DHCP client broadcasts DHCPDISCOVER, DHCPREQUEST and DHCPINFORM
  74. * messages, unless the client knows the address of a DHCP server.
  75. * The client unicasts DHCPRELEASE messages to the server. Because
  76. * the client is declining the use of the IP address supplied by the server,
  77. * the client broadcasts DHCPDECLINE messages.
  78. *
  79. * When the DHCP client knows the address of a DHCP server, in either
  80. * INIT or REBOOTING state, the client may use that address
  81. * in the DHCPDISCOVER or DHCPREQUEST rather than the IP broadcast address.
  82. * The client may also use unicast to send DHCPINFORM messages
  83. * to a known DHCP server. If the client receives no response to DHCP
  84. * messages sent to the IP address of a known DHCP server, the DHCP
  85. * client reverts to using the IP broadcast address.
  86. */
  87. static int raw_bcast_from_client_config_ifindex(struct dhcp_packet *packet)
  88. {
  89. return udhcp_send_raw_packet(packet,
  90. /*src*/ INADDR_ANY, CLIENT_PORT,
  91. /*dst*/ INADDR_BROADCAST, SERVER_PORT, MAC_BCAST_ADDR,
  92. client_config.ifindex);
  93. }
  94. #if ENABLE_FEATURE_UDHCPC_ARPING
  95. /* Broadcast a DHCP decline message */
  96. int FAST_FUNC send_decline(uint32_t xid, uint32_t server, uint32_t requested)
  97. {
  98. struct dhcp_packet packet;
  99. init_packet(&packet, DHCPDECLINE);
  100. packet.xid = xid;
  101. add_simple_option(packet.options, DHCP_REQUESTED_IP, requested);
  102. add_simple_option(packet.options, DHCP_SERVER_ID, server);
  103. bb_info_msg("Sending decline...");
  104. return raw_bcast_from_client_config_ifindex(&packet);
  105. }
  106. #endif
  107. /* Broadcast a DHCP discover packet to the network, with an optionally requested IP */
  108. int FAST_FUNC send_discover(uint32_t xid, uint32_t requested)
  109. {
  110. struct dhcp_packet packet;
  111. init_packet(&packet, DHCPDISCOVER);
  112. packet.xid = xid;
  113. if (requested)
  114. add_simple_option(packet.options, DHCP_REQUESTED_IP, requested);
  115. /* Explicitly saying that we want RFC-compliant packets helps
  116. * some buggy DHCP servers to NOT send bigger packets */
  117. add_simple_option(packet.options, DHCP_MAX_SIZE, htons(576));
  118. add_param_req_option(&packet);
  119. bb_info_msg("Sending discover...");
  120. return raw_bcast_from_client_config_ifindex(&packet);
  121. }
  122. /* Broadcast a DHCP request message */
  123. /* RFC 2131 3.1 paragraph 3:
  124. * "The client _broadcasts_ a DHCPREQUEST message..."
  125. */
  126. int FAST_FUNC send_select(uint32_t xid, uint32_t server, uint32_t requested)
  127. {
  128. struct dhcp_packet packet;
  129. struct in_addr addr;
  130. init_packet(&packet, DHCPREQUEST);
  131. packet.xid = xid;
  132. add_simple_option(packet.options, DHCP_REQUESTED_IP, requested);
  133. add_simple_option(packet.options, DHCP_SERVER_ID, server);
  134. add_param_req_option(&packet);
  135. addr.s_addr = requested;
  136. bb_info_msg("Sending select for %s...", inet_ntoa(addr));
  137. return raw_bcast_from_client_config_ifindex(&packet);
  138. }
  139. /* Unicast or broadcast a DHCP renew message */
  140. int FAST_FUNC send_renew(uint32_t xid, uint32_t server, uint32_t ciaddr)
  141. {
  142. struct dhcp_packet packet;
  143. init_packet(&packet, DHCPREQUEST);
  144. packet.xid = xid;
  145. packet.ciaddr = ciaddr;
  146. add_param_req_option(&packet);
  147. bb_info_msg("Sending renew...");
  148. if (server)
  149. return udhcp_send_kernel_packet(&packet,
  150. ciaddr, CLIENT_PORT,
  151. server, SERVER_PORT);
  152. return raw_bcast_from_client_config_ifindex(&packet);
  153. }
  154. /* Unicast a DHCP release message */
  155. int FAST_FUNC send_release(uint32_t server, uint32_t ciaddr)
  156. {
  157. struct dhcp_packet packet;
  158. init_packet(&packet, DHCPRELEASE);
  159. packet.xid = random_xid();
  160. packet.ciaddr = ciaddr;
  161. add_simple_option(packet.options, DHCP_SERVER_ID, server);
  162. bb_info_msg("Sending release...");
  163. return udhcp_send_kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT);
  164. }
  165. /* Returns -1 on errors that are fatal for the socket, -2 for those that aren't */
  166. int FAST_FUNC udhcp_recv_raw_packet(struct dhcp_packet *dhcp_pkt, int fd)
  167. {
  168. int bytes;
  169. struct ip_udp_dhcp_packet packet;
  170. uint16_t check;
  171. memset(&packet, 0, sizeof(packet));
  172. bytes = safe_read(fd, &packet, sizeof(packet));
  173. if (bytes < 0) {
  174. log1("Packet read error, ignoring");
  175. /* NB: possible down interface, etc. Caller should pause. */
  176. return bytes; /* returns -1 */
  177. }
  178. if (bytes < (int) (sizeof(packet.ip) + sizeof(packet.udp))) {
  179. log1("Packet is too short, ignoring");
  180. return -2;
  181. }
  182. if (bytes < ntohs(packet.ip.tot_len)) {
  183. /* packet is bigger than sizeof(packet), we did partial read */
  184. log1("Oversized packet, ignoring");
  185. return -2;
  186. }
  187. /* ignore any extra garbage bytes */
  188. bytes = ntohs(packet.ip.tot_len);
  189. /* make sure its the right packet for us, and that it passes sanity checks */
  190. if (packet.ip.protocol != IPPROTO_UDP || packet.ip.version != IPVERSION
  191. || packet.ip.ihl != (sizeof(packet.ip) >> 2)
  192. || packet.udp.dest != htons(CLIENT_PORT)
  193. /* || bytes > (int) sizeof(packet) - can't happen */
  194. || ntohs(packet.udp.len) != (uint16_t)(bytes - sizeof(packet.ip))
  195. ) {
  196. log1("Unrelated/bogus packet, ignoring");
  197. return -2;
  198. }
  199. /* verify IP checksum */
  200. check = packet.ip.check;
  201. packet.ip.check = 0;
  202. if (check != udhcp_checksum(&packet.ip, sizeof(packet.ip))) {
  203. log1("Bad IP header checksum, ignoring");
  204. return -2;
  205. }
  206. /* verify UDP checksum. IP header has to be modified for this */
  207. memset(&packet.ip, 0, offsetof(struct iphdr, protocol));
  208. /* ip.xx fields which are not memset: protocol, check, saddr, daddr */
  209. packet.ip.tot_len = packet.udp.len; /* yes, this is needed */
  210. check = packet.udp.check;
  211. packet.udp.check = 0;
  212. if (check && check != udhcp_checksum(&packet, bytes)) {
  213. log1("Packet with bad UDP checksum received, ignoring");
  214. return -2;
  215. }
  216. memcpy(dhcp_pkt, &packet.data, bytes - (sizeof(packet.ip) + sizeof(packet.udp)));
  217. if (dhcp_pkt->cookie != htonl(DHCP_MAGIC)) {
  218. bb_info_msg("Packet with bad magic, ignoring");
  219. return -2;
  220. }
  221. log1("Got valid DHCP packet");
  222. udhcp_dump_packet(dhcp_pkt);
  223. return bytes - (sizeof(packet.ip) + sizeof(packet.udp));
  224. }