3
0

packet.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * packet.c -- packet ops
  4. * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001
  5. *
  6. * Licensed under GPLv2, see file LICENSE in this tarball for details.
  7. */
  8. #include <netinet/in.h>
  9. #if (defined(__GLIBC__) && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1) || defined _NEWLIB_VERSION
  10. #include <netpacket/packet.h>
  11. #include <net/ethernet.h>
  12. #else
  13. #include <asm/types.h>
  14. #include <linux/if_packet.h>
  15. #include <linux/if_ether.h>
  16. #endif
  17. #include "common.h"
  18. #include "dhcpd.h"
  19. #include "options.h"
  20. void FAST_FUNC udhcp_init_header(struct dhcp_packet *packet, char type)
  21. {
  22. memset(packet, 0, sizeof(struct dhcp_packet));
  23. packet->op = BOOTREQUEST; /* if client to a server */
  24. switch (type) {
  25. case DHCPOFFER:
  26. case DHCPACK:
  27. case DHCPNAK:
  28. packet->op = BOOTREPLY; /* if server to client */
  29. }
  30. packet->htype = ETH_10MB;
  31. packet->hlen = ETH_10MB_LEN;
  32. packet->cookie = htonl(DHCP_MAGIC);
  33. packet->options[0] = DHCP_END;
  34. add_simple_option(packet->options, DHCP_MESSAGE_TYPE, type);
  35. }
  36. #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 2
  37. void FAST_FUNC udhcp_dump_packet(struct dhcp_packet *packet)
  38. {
  39. char buf[sizeof(packet->chaddr)*2 + 1];
  40. if (dhcp_verbose < 2)
  41. return;
  42. bb_info_msg(
  43. //" op %x"
  44. //" htype %x"
  45. " hlen %x"
  46. //" hops %x"
  47. " xid %x"
  48. //" secs %x"
  49. //" flags %x"
  50. " ciaddr %x"
  51. " yiaddr %x"
  52. " siaddr %x"
  53. " giaddr %x"
  54. //" chaddr %s"
  55. //" sname %s"
  56. //" file %s"
  57. //" cookie %x"
  58. //" options %s"
  59. //, packet->op
  60. //, packet->htype
  61. , packet->hlen
  62. //, packet->hops
  63. , packet->xid
  64. //, packet->secs
  65. //, packet->flags
  66. , packet->ciaddr
  67. , packet->yiaddr
  68. , packet->siaddr_nip
  69. , packet->gateway_nip
  70. //, packet->chaddr[16]
  71. //, packet->sname[64]
  72. //, packet->file[128]
  73. //, packet->cookie
  74. //, packet->options[]
  75. );
  76. *bin2hex(buf, (void *) packet->chaddr, sizeof(packet->chaddr)) = '\0';
  77. bb_info_msg(" chaddr %s", buf);
  78. }
  79. #endif
  80. /* Read a packet from socket fd, return -1 on read error, -2 on packet error */
  81. int FAST_FUNC udhcp_recv_kernel_packet(struct dhcp_packet *packet, int fd)
  82. {
  83. int bytes;
  84. unsigned char *vendor;
  85. memset(packet, 0, sizeof(*packet));
  86. bytes = safe_read(fd, packet, sizeof(*packet));
  87. if (bytes < 0) {
  88. log1("Packet read error, ignoring");
  89. return bytes; /* returns -1 */
  90. }
  91. if (packet->cookie != htonl(DHCP_MAGIC)) {
  92. bb_info_msg("Packet with bad magic, ignoring");
  93. return -2;
  94. }
  95. log1("Received a packet");
  96. udhcp_dump_packet(packet);
  97. if (packet->op == BOOTREQUEST) {
  98. vendor = get_option(packet, DHCP_VENDOR);
  99. if (vendor) {
  100. #if 0
  101. static const char broken_vendors[][8] = {
  102. "MSFT 98",
  103. ""
  104. };
  105. int i;
  106. for (i = 0; broken_vendors[i][0]; i++) {
  107. if (vendor[OPT_LEN - 2] == (uint8_t)strlen(broken_vendors[i])
  108. && !strncmp((char*)vendor, broken_vendors[i], vendor[OPT_LEN - 2])
  109. ) {
  110. log1("Broken client (%s), forcing broadcast replies",
  111. broken_vendors[i]);
  112. packet->flags |= htons(BROADCAST_FLAG);
  113. }
  114. }
  115. #else
  116. if (vendor[OPT_LEN - 2] == (uint8_t)(sizeof("MSFT 98")-1)
  117. && memcmp(vendor, "MSFT 98", sizeof("MSFT 98")-1) == 0
  118. ) {
  119. log1("Broken client (%s), forcing broadcast replies", "MSFT 98");
  120. packet->flags |= htons(BROADCAST_FLAG);
  121. }
  122. #endif
  123. }
  124. }
  125. return bytes;
  126. }
  127. uint16_t FAST_FUNC udhcp_checksum(void *addr, int count)
  128. {
  129. /* Compute Internet Checksum for "count" bytes
  130. * beginning at location "addr".
  131. */
  132. int32_t sum = 0;
  133. uint16_t *source = (uint16_t *) addr;
  134. while (count > 1) {
  135. /* This is the inner loop */
  136. sum += *source++;
  137. count -= 2;
  138. }
  139. /* Add left-over byte, if any */
  140. if (count > 0) {
  141. /* Make sure that the left-over byte is added correctly both
  142. * with little and big endian hosts */
  143. uint16_t tmp = 0;
  144. *(uint8_t*)&tmp = *(uint8_t*)source;
  145. sum += tmp;
  146. }
  147. /* Fold 32-bit sum to 16 bits */
  148. while (sum >> 16)
  149. sum = (sum & 0xffff) + (sum >> 16);
  150. return ~sum;
  151. }
  152. /* Construct a ip/udp header for a packet, send packet */
  153. int FAST_FUNC udhcp_send_raw_packet(struct dhcp_packet *dhcp_pkt,
  154. uint32_t source_ip, int source_port,
  155. uint32_t dest_ip, int dest_port, const uint8_t *dest_arp,
  156. int ifindex)
  157. {
  158. struct sockaddr_ll dest;
  159. struct ip_udp_dhcp_packet packet;
  160. int fd;
  161. int result = -1;
  162. const char *msg;
  163. enum {
  164. IP_UPD_DHCP_SIZE = sizeof(struct ip_udp_dhcp_packet) - CONFIG_UDHCPC_SLACK_FOR_BUGGY_SERVERS,
  165. UPD_DHCP_SIZE = IP_UPD_DHCP_SIZE - offsetof(struct ip_udp_dhcp_packet, udp),
  166. };
  167. fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
  168. if (fd < 0) {
  169. msg = "socket(%s)";
  170. goto ret_msg;
  171. }
  172. memset(&dest, 0, sizeof(dest));
  173. memset(&packet, 0, sizeof(packet));
  174. packet.data = *dhcp_pkt; /* struct copy */
  175. dest.sll_family = AF_PACKET;
  176. dest.sll_protocol = htons(ETH_P_IP);
  177. dest.sll_ifindex = ifindex;
  178. dest.sll_halen = 6;
  179. memcpy(dest.sll_addr, dest_arp, 6);
  180. if (bind(fd, (struct sockaddr *)&dest, sizeof(dest)) < 0) {
  181. msg = "bind(%s)";
  182. goto ret_close;
  183. }
  184. packet.ip.protocol = IPPROTO_UDP;
  185. packet.ip.saddr = source_ip;
  186. packet.ip.daddr = dest_ip;
  187. packet.udp.source = htons(source_port);
  188. packet.udp.dest = htons(dest_port);
  189. /* size, excluding IP header: */
  190. packet.udp.len = htons(UPD_DHCP_SIZE);
  191. /* for UDP checksumming, ip.len is set to UDP packet len */
  192. packet.ip.tot_len = packet.udp.len;
  193. packet.udp.check = udhcp_checksum(&packet, IP_UPD_DHCP_SIZE);
  194. /* but for sending, it is set to IP packet len */
  195. packet.ip.tot_len = htons(IP_UPD_DHCP_SIZE);
  196. packet.ip.ihl = sizeof(packet.ip) >> 2;
  197. packet.ip.version = IPVERSION;
  198. packet.ip.ttl = IPDEFTTL;
  199. packet.ip.check = udhcp_checksum(&packet.ip, sizeof(packet.ip));
  200. /* Currently we send full-sized DHCP packets (zero padded).
  201. * If you need to change this: last byte of the packet is
  202. * packet.data.options[end_option(packet.data.options)]
  203. */
  204. udhcp_dump_packet(dhcp_pkt);
  205. result = sendto(fd, &packet, IP_UPD_DHCP_SIZE, 0,
  206. (struct sockaddr *) &dest, sizeof(dest));
  207. msg = "sendto";
  208. ret_close:
  209. close(fd);
  210. if (result < 0) {
  211. ret_msg:
  212. bb_perror_msg(msg, "PACKET");
  213. }
  214. return result;
  215. }
  216. /* Let the kernel do all the work for packet generation */
  217. int FAST_FUNC udhcp_send_kernel_packet(struct dhcp_packet *dhcp_pkt,
  218. uint32_t source_ip, int source_port,
  219. uint32_t dest_ip, int dest_port)
  220. {
  221. struct sockaddr_in client;
  222. int fd;
  223. int result = -1;
  224. const char *msg;
  225. enum {
  226. DHCP_SIZE = sizeof(struct dhcp_packet) - CONFIG_UDHCPC_SLACK_FOR_BUGGY_SERVERS,
  227. };
  228. fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
  229. if (fd < 0) {
  230. msg = "socket(%s)";
  231. goto ret_msg;
  232. }
  233. setsockopt_reuseaddr(fd);
  234. memset(&client, 0, sizeof(client));
  235. client.sin_family = AF_INET;
  236. client.sin_port = htons(source_port);
  237. client.sin_addr.s_addr = source_ip;
  238. if (bind(fd, (struct sockaddr *)&client, sizeof(client)) == -1) {
  239. msg = "bind(%s)";
  240. goto ret_close;
  241. }
  242. memset(&client, 0, sizeof(client));
  243. client.sin_family = AF_INET;
  244. client.sin_port = htons(dest_port);
  245. client.sin_addr.s_addr = dest_ip;
  246. if (connect(fd, (struct sockaddr *)&client, sizeof(client)) == -1) {
  247. msg = "connect";
  248. goto ret_close;
  249. }
  250. /* Currently we send full-sized DHCP packets (see above) */
  251. udhcp_dump_packet(dhcp_pkt);
  252. result = safe_write(fd, dhcp_pkt, DHCP_SIZE);
  253. msg = "write";
  254. ret_close:
  255. close(fd);
  256. if (result < 0) {
  257. ret_msg:
  258. bb_perror_msg(msg, "UDP");
  259. }
  260. return result;
  261. }