packet.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. #if 0
  39. static const char broken_vendors[][8] = {
  40. "MSFT 98",
  41. ""
  42. };
  43. #endif
  44. int bytes;
  45. unsigned char *vendor;
  46. memset(packet, 0, sizeof(*packet));
  47. bytes = read(fd, packet, sizeof(*packet));
  48. if (bytes < 0) {
  49. DEBUG("cannot read on listening socket, ignoring");
  50. return -1;
  51. }
  52. if (ntohl(packet->cookie) != DHCP_MAGIC) {
  53. bb_error_msg("received bogus message, ignoring");
  54. return -2;
  55. }
  56. DEBUG("Received a packet");
  57. if (packet->op == BOOTREQUEST) {
  58. vendor = get_option(packet, DHCP_VENDOR);
  59. if (vendor) {
  60. #if 0
  61. int i;
  62. for (i = 0; broken_vendors[i][0]; i++) {
  63. if (vendor[OPT_LEN - 2] == (uint8_t)strlen(broken_vendors[i])
  64. && !strncmp((char*)vendor, broken_vendors[i], vendor[OPT_LEN - 2])
  65. ) {
  66. DEBUG("broken client (%s), forcing broadcast",
  67. broken_vendors[i]);
  68. packet->flags |= htons(BROADCAST_FLAG);
  69. }
  70. }
  71. #else
  72. if (vendor[OPT_LEN - 2] == (uint8_t)(sizeof("MSFT 98")-1)
  73. && memcmp(vendor, "MSFT 98", sizeof("MSFT 98")-1) == 0
  74. ) {
  75. DEBUG("broken client (%s), forcing broadcast", "MSFT 98");
  76. packet->flags |= htons(BROADCAST_FLAG);
  77. }
  78. #endif
  79. }
  80. }
  81. return bytes;
  82. }
  83. uint16_t udhcp_checksum(void *addr, int count)
  84. {
  85. /* Compute Internet Checksum for "count" bytes
  86. * beginning at location "addr".
  87. */
  88. int32_t sum = 0;
  89. uint16_t *source = (uint16_t *) addr;
  90. while (count > 1) {
  91. /* This is the inner loop */
  92. sum += *source++;
  93. count -= 2;
  94. }
  95. /* Add left-over byte, if any */
  96. if (count > 0) {
  97. /* Make sure that the left-over byte is added correctly both
  98. * with little and big endian hosts */
  99. uint16_t tmp = 0;
  100. *(uint8_t *) (&tmp) = * (uint8_t *) source;
  101. sum += tmp;
  102. }
  103. /* Fold 32-bit sum to 16 bits */
  104. while (sum >> 16)
  105. sum = (sum & 0xffff) + (sum >> 16);
  106. return ~sum;
  107. }
  108. /* Construct a ip/udp header for a packet, and specify the source and dest hardware address */
  109. void BUG_sizeof_struct_udp_dhcp_packet_must_be_576(void);
  110. int udhcp_raw_packet(struct dhcpMessage *payload,
  111. uint32_t source_ip, int source_port,
  112. uint32_t dest_ip, int dest_port, const uint8_t *dest_arp, int ifindex)
  113. {
  114. int fd;
  115. int result;
  116. struct sockaddr_ll dest;
  117. struct udp_dhcp_packet packet;
  118. fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
  119. if (fd < 0) {
  120. bb_perror_msg("socket");
  121. return -1;
  122. }
  123. memset(&dest, 0, sizeof(dest));
  124. memset(&packet, 0, sizeof(packet));
  125. dest.sll_family = AF_PACKET;
  126. dest.sll_protocol = htons(ETH_P_IP);
  127. dest.sll_ifindex = ifindex;
  128. dest.sll_halen = 6;
  129. memcpy(dest.sll_addr, dest_arp, 6);
  130. if (bind(fd, (struct sockaddr *)&dest, sizeof(struct sockaddr_ll)) < 0) {
  131. bb_perror_msg("bind");
  132. close(fd);
  133. return -1;
  134. }
  135. packet.ip.protocol = IPPROTO_UDP;
  136. packet.ip.saddr = source_ip;
  137. packet.ip.daddr = dest_ip;
  138. packet.udp.source = htons(source_port);
  139. packet.udp.dest = htons(dest_port);
  140. packet.udp.len = htons(sizeof(packet.udp) + sizeof(struct dhcpMessage)); /* cheat on the psuedo-header */
  141. packet.ip.tot_len = packet.udp.len;
  142. memcpy(&(packet.data), payload, sizeof(struct dhcpMessage));
  143. packet.udp.check = udhcp_checksum(&packet, sizeof(struct udp_dhcp_packet));
  144. packet.ip.tot_len = htons(sizeof(struct udp_dhcp_packet));
  145. packet.ip.ihl = sizeof(packet.ip) >> 2;
  146. packet.ip.version = IPVERSION;
  147. packet.ip.ttl = IPDEFTTL;
  148. packet.ip.check = udhcp_checksum(&(packet.ip), sizeof(packet.ip));
  149. if (sizeof(struct udp_dhcp_packet) != 576)
  150. BUG_sizeof_struct_udp_dhcp_packet_must_be_576();
  151. result = sendto(fd, &packet, sizeof(struct udp_dhcp_packet), 0,
  152. (struct sockaddr *) &dest, sizeof(dest));
  153. if (result <= 0) {
  154. bb_perror_msg("sendto");
  155. }
  156. close(fd);
  157. return result;
  158. }
  159. /* Let the kernel do all the work for packet generation */
  160. int udhcp_kernel_packet(struct dhcpMessage *payload,
  161. uint32_t source_ip, int source_port,
  162. uint32_t dest_ip, int dest_port)
  163. {
  164. int fd, result;
  165. struct sockaddr_in client;
  166. fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
  167. if (fd < 0)
  168. return -1;
  169. setsockopt_reuseaddr(fd);
  170. memset(&client, 0, sizeof(client));
  171. client.sin_family = AF_INET;
  172. client.sin_port = htons(source_port);
  173. client.sin_addr.s_addr = source_ip;
  174. if (bind(fd, (struct sockaddr *)&client, sizeof(client)) == -1) {
  175. close(fd);
  176. return -1;
  177. }
  178. memset(&client, 0, sizeof(client));
  179. client.sin_family = AF_INET;
  180. client.sin_port = htons(dest_port);
  181. client.sin_addr.s_addr = dest_ip;
  182. if (connect(fd, (struct sockaddr *)&client, sizeof(struct sockaddr)) == -1) {
  183. close(fd);
  184. return -1;
  185. }
  186. result = write(fd, payload, sizeof(struct dhcpMessage));
  187. close(fd);
  188. return result;
  189. }