packet.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /* vi: set sw=4 ts=4: */
  2. #include <netinet/in.h>
  3. #if (defined(__GLIBC__) && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1) || defined _NEWLIB_VERSION
  4. #include <netpacket/packet.h>
  5. #include <net/ethernet.h>
  6. #else
  7. #include <asm/types.h>
  8. #include <linux/if_packet.h>
  9. #include <linux/if_ether.h>
  10. #endif
  11. #include "common.h"
  12. #include "dhcpd.h"
  13. #include "options.h"
  14. void udhcp_init_header(struct dhcpMessage *packet, char type)
  15. {
  16. memset(packet, 0, sizeof(struct dhcpMessage));
  17. switch (type) {
  18. case DHCPDISCOVER:
  19. case DHCPREQUEST:
  20. case DHCPRELEASE:
  21. case DHCPINFORM:
  22. packet->op = BOOTREQUEST;
  23. break;
  24. case DHCPOFFER:
  25. case DHCPACK:
  26. case DHCPNAK:
  27. packet->op = BOOTREPLY;
  28. }
  29. packet->htype = ETH_10MB;
  30. packet->hlen = ETH_10MB_LEN;
  31. packet->cookie = htonl(DHCP_MAGIC);
  32. packet->options[0] = DHCP_END;
  33. add_simple_option(packet->options, DHCP_MESSAGE_TYPE, type);
  34. }
  35. /* read a packet from socket fd, return -1 on read error, -2 on packet error */
  36. int udhcp_get_packet(struct dhcpMessage *packet, int fd)
  37. {
  38. static const char broken_vendors[][8] = {
  39. "MSFT 98",
  40. ""
  41. };
  42. int bytes;
  43. int i;
  44. char unsigned *vendor;
  45. memset(packet, 0, sizeof(struct dhcpMessage));
  46. bytes = read(fd, packet, sizeof(struct dhcpMessage));
  47. if (bytes < 0) {
  48. DEBUG("cannot read on listening socket, ignoring");
  49. return -1;
  50. }
  51. if (ntohl(packet->cookie) != DHCP_MAGIC) {
  52. bb_error_msg("received bogus message, ignoring");
  53. return -2;
  54. }
  55. DEBUG("Received a packet");
  56. if (packet->op == BOOTREQUEST && (vendor = get_option(packet, DHCP_VENDOR))) {
  57. for (i = 0; broken_vendors[i][0]; i++) {
  58. if (vendor[OPT_LEN - 2] == (uint8_t)strlen(broken_vendors[i])
  59. && !strncmp((char*)vendor, broken_vendors[i], vendor[OPT_LEN - 2])
  60. ) {
  61. DEBUG("broken client (%s), forcing broadcast",
  62. broken_vendors[i]);
  63. packet->flags |= htons(BROADCAST_FLAG);
  64. }
  65. }
  66. }
  67. return bytes;
  68. }
  69. uint16_t udhcp_checksum(void *addr, int count)
  70. {
  71. /* Compute Internet Checksum for "count" bytes
  72. * beginning at location "addr".
  73. */
  74. int32_t sum = 0;
  75. uint16_t *source = (uint16_t *) addr;
  76. while (count > 1) {
  77. /* This is the inner loop */
  78. sum += *source++;
  79. count -= 2;
  80. }
  81. /* Add left-over byte, if any */
  82. if (count > 0) {
  83. /* Make sure that the left-over byte is added correctly both
  84. * with little and big endian hosts */
  85. uint16_t tmp = 0;
  86. *(uint8_t *) (&tmp) = * (uint8_t *) source;
  87. sum += tmp;
  88. }
  89. /* Fold 32-bit sum to 16 bits */
  90. while (sum >> 16)
  91. sum = (sum & 0xffff) + (sum >> 16);
  92. return ~sum;
  93. }
  94. /* Construct a ip/udp header for a packet, and specify the source and dest hardware address */
  95. void BUG_sizeof_struct_udp_dhcp_packet_must_be_576(void);
  96. int udhcp_raw_packet(struct dhcpMessage *payload,
  97. uint32_t source_ip, int source_port,
  98. uint32_t dest_ip, int dest_port, uint8_t *dest_arp, int ifindex)
  99. {
  100. int fd;
  101. int result;
  102. struct sockaddr_ll dest;
  103. struct udp_dhcp_packet packet;
  104. fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
  105. if (fd < 0) {
  106. bb_perror_msg("socket");
  107. return -1;
  108. }
  109. memset(&dest, 0, sizeof(dest));
  110. memset(&packet, 0, sizeof(packet));
  111. dest.sll_family = AF_PACKET;
  112. dest.sll_protocol = htons(ETH_P_IP);
  113. dest.sll_ifindex = ifindex;
  114. dest.sll_halen = 6;
  115. memcpy(dest.sll_addr, dest_arp, 6);
  116. if (bind(fd, (struct sockaddr *)&dest, sizeof(struct sockaddr_ll)) < 0) {
  117. bb_perror_msg("bind");
  118. close(fd);
  119. return -1;
  120. }
  121. packet.ip.protocol = IPPROTO_UDP;
  122. packet.ip.saddr = source_ip;
  123. packet.ip.daddr = dest_ip;
  124. packet.udp.source = htons(source_port);
  125. packet.udp.dest = htons(dest_port);
  126. packet.udp.len = htons(sizeof(packet.udp) + sizeof(struct dhcpMessage)); /* cheat on the psuedo-header */
  127. packet.ip.tot_len = packet.udp.len;
  128. memcpy(&(packet.data), payload, sizeof(struct dhcpMessage));
  129. packet.udp.check = udhcp_checksum(&packet, sizeof(struct udp_dhcp_packet));
  130. packet.ip.tot_len = htons(sizeof(struct udp_dhcp_packet));
  131. packet.ip.ihl = sizeof(packet.ip) >> 2;
  132. packet.ip.version = IPVERSION;
  133. packet.ip.ttl = IPDEFTTL;
  134. packet.ip.check = udhcp_checksum(&(packet.ip), sizeof(packet.ip));
  135. if (sizeof(struct udp_dhcp_packet) != 576)
  136. BUG_sizeof_struct_udp_dhcp_packet_must_be_576();
  137. result = sendto(fd, &packet, sizeof(struct udp_dhcp_packet), 0,
  138. (struct sockaddr *) &dest, sizeof(dest));
  139. if (result <= 0) {
  140. bb_perror_msg("sendto");
  141. }
  142. close(fd);
  143. return result;
  144. }
  145. /* Let the kernel do all the work for packet generation */
  146. int udhcp_kernel_packet(struct dhcpMessage *payload,
  147. uint32_t source_ip, int source_port,
  148. uint32_t dest_ip, int dest_port)
  149. {
  150. int fd, result;
  151. struct sockaddr_in client;
  152. fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
  153. if (fd < 0)
  154. return -1;
  155. if (setsockopt_reuseaddr(fd) == -1) {
  156. close(fd);
  157. return -1;
  158. }
  159. memset(&client, 0, sizeof(client));
  160. client.sin_family = AF_INET;
  161. client.sin_port = htons(source_port);
  162. client.sin_addr.s_addr = source_ip;
  163. if (bind(fd, (struct sockaddr *)&client, sizeof(struct sockaddr)) == -1) {
  164. close(fd);
  165. return -1;
  166. }
  167. memset(&client, 0, sizeof(client));
  168. client.sin_family = AF_INET;
  169. client.sin_port = htons(dest_port);
  170. client.sin_addr.s_addr = dest_ip;
  171. if (connect(fd, (struct sockaddr *)&client, sizeof(struct sockaddr)) == -1) {
  172. close(fd);
  173. return -1;
  174. }
  175. result = write(fd, payload, sizeof(struct dhcpMessage));
  176. close(fd);
  177. return result;
  178. }