clientpacket.c 6.3 KB

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