packet.c 7.1 KB

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