clientpacket.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 <features.h>
  11. #if (defined(__GLIBC__) && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1) || defined _NEWLIB_VERSION
  12. #include <netpacket/packet.h>
  13. #include <net/ethernet.h>
  14. #else
  15. #include <asm/types.h>
  16. #include <linux/if_packet.h>
  17. #include <linux/if_ether.h>
  18. #endif
  19. #include "common.h"
  20. #include "dhcpd.h"
  21. #include "dhcpc.h"
  22. #include "options.h"
  23. /* Create a random xid */
  24. uint32_t 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 a packet with the proper defaults */
  34. static void init_packet(struct dhcpMessage *packet, char type)
  35. {
  36. udhcp_init_header(packet, type);
  37. memcpy(packet->chaddr, client_config.arp, 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. add_option_string(packet->options, client_config.vendorclass);
  45. }
  46. /* Add a parameter request list for stubborn DHCP servers. Pull the data
  47. * from the struct in options.c. Don't do bounds checking here because it
  48. * goes towards the head of the packet. */
  49. static void add_requests(struct dhcpMessage *packet)
  50. {
  51. int end = end_option(packet->options);
  52. int i, len = 0;
  53. packet->options[end + OPT_CODE] = DHCP_PARAM_REQ;
  54. for (i = 0; dhcp_options[i].code; i++)
  55. if (dhcp_options[i].flags & OPTION_REQ)
  56. packet->options[end + OPT_DATA + len++] = dhcp_options[i].code;
  57. packet->options[end + OPT_LEN] = len;
  58. packet->options[end + OPT_DATA + len] = DHCP_END;
  59. }
  60. /* Broadcast a DHCP discover packet to the network, with an optionally requested IP */
  61. int send_discover(uint32_t xid, uint32_t requested)
  62. {
  63. struct dhcpMessage packet;
  64. init_packet(&packet, DHCPDISCOVER);
  65. packet.xid = xid;
  66. if (requested)
  67. add_simple_option(packet.options, DHCP_REQUESTED_IP, requested);
  68. add_requests(&packet);
  69. bb_info_msg("Sending discover...");
  70. return udhcp_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
  71. SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
  72. }
  73. /* Broadcasts a DHCP request message */
  74. int send_selecting(uint32_t xid, uint32_t server, uint32_t requested)
  75. {
  76. struct dhcpMessage packet;
  77. struct in_addr addr;
  78. init_packet(&packet, DHCPREQUEST);
  79. packet.xid = xid;
  80. add_simple_option(packet.options, DHCP_REQUESTED_IP, requested);
  81. add_simple_option(packet.options, DHCP_SERVER_ID, server);
  82. add_requests(&packet);
  83. addr.s_addr = requested;
  84. bb_info_msg("Sending select for %s...", inet_ntoa(addr));
  85. return udhcp_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
  86. SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
  87. }
  88. /* Unicasts or broadcasts a DHCP renew message */
  89. int send_renew(uint32_t xid, uint32_t server, uint32_t ciaddr)
  90. {
  91. struct dhcpMessage packet;
  92. init_packet(&packet, DHCPREQUEST);
  93. packet.xid = xid;
  94. packet.ciaddr = ciaddr;
  95. add_requests(&packet);
  96. bb_info_msg("Sending renew...");
  97. if (server)
  98. return udhcp_kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT);
  99. return udhcp_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
  100. SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
  101. }
  102. /* Unicasts a DHCP release message */
  103. int send_release(uint32_t server, uint32_t ciaddr)
  104. {
  105. struct dhcpMessage packet;
  106. init_packet(&packet, DHCPRELEASE);
  107. packet.xid = random_xid();
  108. packet.ciaddr = ciaddr;
  109. add_simple_option(packet.options, DHCP_REQUESTED_IP, ciaddr);
  110. add_simple_option(packet.options, DHCP_SERVER_ID, server);
  111. bb_info_msg("Sending release...");
  112. return udhcp_kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT);
  113. }
  114. /* return -1 on errors that are fatal for the socket, -2 for those that aren't */
  115. int get_raw_packet(struct dhcpMessage *payload, int fd)
  116. {
  117. int bytes;
  118. struct udp_dhcp_packet packet;
  119. uint32_t source, dest;
  120. uint16_t check;
  121. memset(&packet, 0, sizeof(struct udp_dhcp_packet));
  122. bytes = read(fd, &packet, sizeof(struct udp_dhcp_packet));
  123. if (bytes < 0) {
  124. DEBUG("Cannot read on raw listening socket - ignoring");
  125. usleep(500000); /* possible down interface, looping condition */
  126. return -1;
  127. }
  128. if (bytes < (int) (sizeof(struct iphdr) + sizeof(struct udphdr))) {
  129. DEBUG("Message too short, ignoring");
  130. return -2;
  131. }
  132. if (bytes < ntohs(packet.ip.tot_len)) {
  133. DEBUG("Truncated packet");
  134. return -2;
  135. }
  136. /* ignore any extra garbage bytes */
  137. bytes = ntohs(packet.ip.tot_len);
  138. /* Make sure its the right packet for us, and that it passes sanity checks */
  139. if (packet.ip.protocol != IPPROTO_UDP || packet.ip.version != IPVERSION
  140. || packet.ip.ihl != sizeof(packet.ip) >> 2
  141. || packet.udp.dest != htons(CLIENT_PORT)
  142. || bytes > (int) sizeof(struct udp_dhcp_packet)
  143. || ntohs(packet.udp.len) != (uint16_t)(bytes - sizeof(packet.ip))
  144. ) {
  145. DEBUG("Unrelated/bogus packet");
  146. return -2;
  147. }
  148. /* check IP checksum */
  149. check = packet.ip.check;
  150. packet.ip.check = 0;
  151. if (check != udhcp_checksum(&(packet.ip), sizeof(packet.ip))) {
  152. DEBUG("bad IP header checksum, ignoring");
  153. return -1;
  154. }
  155. /* verify the UDP checksum by replacing the header with a psuedo header */
  156. source = packet.ip.saddr;
  157. dest = packet.ip.daddr;
  158. check = packet.udp.check;
  159. packet.udp.check = 0;
  160. memset(&packet.ip, 0, sizeof(packet.ip));
  161. packet.ip.protocol = IPPROTO_UDP;
  162. packet.ip.saddr = source;
  163. packet.ip.daddr = dest;
  164. packet.ip.tot_len = packet.udp.len; /* cheat on the psuedo-header */
  165. if (check && check != udhcp_checksum(&packet, bytes)) {
  166. bb_error_msg("packet with bad UDP checksum received, ignoring");
  167. return -2;
  168. }
  169. memcpy(payload, &(packet.data), bytes - (sizeof(packet.ip) + sizeof(packet.udp)));
  170. if (ntohl(payload->cookie) != DHCP_MAGIC) {
  171. bb_error_msg("received bogus message (bad magic) - ignoring");
  172. return -2;
  173. }
  174. DEBUG("oooooh!!! got some!");
  175. return bytes - (sizeof(packet.ip) + sizeof(packet.udp));
  176. }