packet.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. memset(packet, 0, sizeof(*packet));
  80. bytes = safe_read(fd, packet, sizeof(*packet));
  81. if (bytes < 0) {
  82. log1("Packet read error, ignoring");
  83. return bytes; /* returns -1 */
  84. }
  85. if (bytes < offsetof(struct dhcp_packet, options)
  86. || packet->cookie != htonl(DHCP_MAGIC)
  87. ) {
  88. bb_info_msg("Packet with bad magic, ignoring");
  89. return -2;
  90. }
  91. log1("Received a packet");
  92. udhcp_dump_packet(packet);
  93. return bytes;
  94. }
  95. /* Construct a ip/udp header for a packet, send packet */
  96. int FAST_FUNC udhcp_send_raw_packet(struct dhcp_packet *dhcp_pkt,
  97. uint32_t source_nip, int source_port,
  98. uint32_t dest_nip, int dest_port, const uint8_t *dest_arp,
  99. int ifindex)
  100. {
  101. struct sockaddr_ll dest_sll;
  102. struct ip_udp_dhcp_packet packet;
  103. unsigned padding;
  104. int fd;
  105. int result = -1;
  106. const char *msg;
  107. fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
  108. if (fd < 0) {
  109. msg = "socket(%s)";
  110. goto ret_msg;
  111. }
  112. memset(&dest_sll, 0, sizeof(dest_sll));
  113. memset(&packet, 0, offsetof(struct ip_udp_dhcp_packet, data));
  114. packet.data = *dhcp_pkt; /* struct copy */
  115. dest_sll.sll_family = AF_PACKET;
  116. dest_sll.sll_protocol = htons(ETH_P_IP);
  117. dest_sll.sll_ifindex = ifindex;
  118. dest_sll.sll_halen = 6;
  119. memcpy(dest_sll.sll_addr, dest_arp, 6);
  120. if (bind(fd, (struct sockaddr *)&dest_sll, sizeof(dest_sll)) < 0) {
  121. msg = "bind(%s)";
  122. goto ret_close;
  123. }
  124. /* We were sending full-sized DHCP packets (zero padded),
  125. * but some badly configured servers were seen dropping them.
  126. * Apparently they drop all DHCP packets >576 *ethernet* octets big,
  127. * whereas they may only drop packets >576 *IP* octets big
  128. * (which for typical Ethernet II means 590 octets: 6+6+2 + 576).
  129. *
  130. * In order to work with those buggy servers,
  131. * we truncate packets after end option byte.
  132. */
  133. padding = DHCP_OPTIONS_BUFSIZE - 1 - udhcp_end_option(packet.data.options);
  134. packet.ip.protocol = IPPROTO_UDP;
  135. packet.ip.saddr = source_nip;
  136. packet.ip.daddr = dest_nip;
  137. packet.udp.source = htons(source_port);
  138. packet.udp.dest = htons(dest_port);
  139. /* size, excluding IP header: */
  140. packet.udp.len = htons(UDP_DHCP_SIZE - padding);
  141. /* for UDP checksumming, ip.len is set to UDP packet len */
  142. packet.ip.tot_len = packet.udp.len;
  143. packet.udp.check = inet_cksum((uint16_t *)&packet,
  144. IP_UDP_DHCP_SIZE - padding);
  145. /* but for sending, it is set to IP packet len */
  146. packet.ip.tot_len = htons(IP_UDP_DHCP_SIZE - padding);
  147. packet.ip.ihl = sizeof(packet.ip) >> 2;
  148. packet.ip.version = IPVERSION;
  149. packet.ip.ttl = IPDEFTTL;
  150. packet.ip.check = inet_cksum((uint16_t *)&packet.ip, sizeof(packet.ip));
  151. udhcp_dump_packet(dhcp_pkt);
  152. result = sendto(fd, &packet, IP_UDP_DHCP_SIZE - padding, /*flags:*/ 0,
  153. (struct sockaddr *) &dest_sll, sizeof(dest_sll));
  154. msg = "sendto";
  155. ret_close:
  156. close(fd);
  157. if (result < 0) {
  158. ret_msg:
  159. bb_perror_msg(msg, "PACKET");
  160. }
  161. return result;
  162. }
  163. /* Let the kernel do all the work for packet generation */
  164. int FAST_FUNC udhcp_send_kernel_packet(struct dhcp_packet *dhcp_pkt,
  165. uint32_t source_nip, int source_port,
  166. uint32_t dest_nip, int dest_port)
  167. {
  168. struct sockaddr_in sa;
  169. unsigned padding;
  170. int fd;
  171. int result = -1;
  172. const char *msg;
  173. fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
  174. if (fd < 0) {
  175. msg = "socket(%s)";
  176. goto ret_msg;
  177. }
  178. setsockopt_reuseaddr(fd);
  179. memset(&sa, 0, sizeof(sa));
  180. sa.sin_family = AF_INET;
  181. sa.sin_port = htons(source_port);
  182. sa.sin_addr.s_addr = source_nip;
  183. if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
  184. msg = "bind(%s)";
  185. goto ret_close;
  186. }
  187. memset(&sa, 0, sizeof(sa));
  188. sa.sin_family = AF_INET;
  189. sa.sin_port = htons(dest_port);
  190. sa.sin_addr.s_addr = dest_nip;
  191. if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
  192. msg = "connect";
  193. goto ret_close;
  194. }
  195. udhcp_dump_packet(dhcp_pkt);
  196. padding = DHCP_OPTIONS_BUFSIZE - 1 - udhcp_end_option(dhcp_pkt->options);
  197. result = safe_write(fd, dhcp_pkt, DHCP_SIZE - padding);
  198. msg = "write";
  199. ret_close:
  200. close(fd);
  201. if (result < 0) {
  202. ret_msg:
  203. bb_perror_msg(msg, "UDP");
  204. }
  205. return result;
  206. }