packet.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. #if ENABLE_UDHCPC || ENABLE_UDHCPD
  15. void FAST_FUNC udhcp_init_header(struct dhcp_packet *packet, char type)
  16. {
  17. memset(packet, 0, sizeof(*packet));
  18. packet->op = BOOTREQUEST; /* if client to a server */
  19. switch (type) {
  20. case DHCPOFFER:
  21. case DHCPACK:
  22. case DHCPNAK:
  23. packet->op = BOOTREPLY; /* if server to client */
  24. }
  25. packet->htype = 1; /* ethernet */
  26. packet->hlen = 6;
  27. packet->cookie = htonl(DHCP_MAGIC);
  28. if (DHCP_END != 0)
  29. packet->options[0] = DHCP_END;
  30. udhcp_add_simple_option(packet, DHCP_MESSAGE_TYPE, type);
  31. }
  32. #endif
  33. #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 2
  34. void FAST_FUNC udhcp_dump_packet(struct dhcp_packet *packet)
  35. {
  36. char buf[sizeof(packet->chaddr)*2 + 1];
  37. if (dhcp_verbose < 2)
  38. return;
  39. bb_info_msg(
  40. //" op %x"
  41. //" htype %x"
  42. " hlen %x"
  43. //" hops %x"
  44. " xid %x"
  45. //" secs %x"
  46. //" flags %x"
  47. " ciaddr %x"
  48. " yiaddr %x"
  49. " siaddr %x"
  50. " giaddr %x"
  51. //" sname %s"
  52. //" file %s"
  53. //" cookie %x"
  54. //" options %s"
  55. //, packet->op
  56. //, packet->htype
  57. , packet->hlen
  58. //, packet->hops
  59. , packet->xid
  60. //, packet->secs
  61. //, packet->flags
  62. , packet->ciaddr
  63. , packet->yiaddr
  64. , packet->siaddr_nip
  65. , packet->gateway_nip
  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. log1s("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_simple_info_msg("packet with bad magic, ignoring");
  89. return -2;
  90. }
  91. log1("received %s", "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_hatype = ARPHRD_???;*/
  119. /*dest_sll.sll_pkttype = PACKET_???;*/
  120. dest_sll.sll_halen = 6;
  121. memcpy(dest_sll.sll_addr, dest_arp, 6);
  122. if (bind(fd, (struct sockaddr *)&dest_sll, sizeof(dest_sll)) < 0) {
  123. msg = "bind(%s)";
  124. goto ret_close;
  125. }
  126. /* We were sending full-sized DHCP packets (zero padded),
  127. * but some badly configured servers were seen dropping them.
  128. * Apparently they drop all DHCP packets >576 *ethernet* octets big,
  129. * whereas they may only drop packets >576 *IP* octets big
  130. * (which for typical Ethernet II means 590 octets: 6+6+2 + 576).
  131. *
  132. * In order to work with those buggy servers,
  133. * we truncate packets after end option byte.
  134. *
  135. * However, RFC 1542 says "The IP Total Length and UDP Length
  136. * must be large enough to contain the minimal BOOTP header of 300 octets".
  137. * Thus, we retain enough padding to not go below 300 BOOTP bytes.
  138. * Some devices have filters which drop DHCP packets shorter than that.
  139. */
  140. padding = DHCP_OPTIONS_BUFSIZE - 1 - udhcp_end_option(packet.data.options);
  141. if (padding > DHCP_SIZE - 300)
  142. padding = DHCP_SIZE - 300;
  143. packet.ip.protocol = IPPROTO_UDP;
  144. packet.ip.saddr = source_nip;
  145. packet.ip.daddr = dest_nip;
  146. packet.udp.source = htons(source_port);
  147. packet.udp.dest = htons(dest_port);
  148. /* size, excluding IP header: */
  149. packet.udp.len = htons(UDP_DHCP_SIZE - padding);
  150. /* for UDP checksumming, ip.len is set to UDP packet len */
  151. packet.ip.tot_len = packet.udp.len;
  152. packet.udp.check = inet_cksum((uint16_t *)&packet,
  153. IP_UDP_DHCP_SIZE - padding);
  154. /* but for sending, it is set to IP packet len */
  155. packet.ip.tot_len = htons(IP_UDP_DHCP_SIZE - padding);
  156. packet.ip.ihl = sizeof(packet.ip) >> 2;
  157. packet.ip.version = IPVERSION;
  158. packet.ip.ttl = IPDEFTTL;
  159. packet.ip.check = inet_cksum((uint16_t *)&packet.ip, sizeof(packet.ip));
  160. udhcp_dump_packet(dhcp_pkt);
  161. result = sendto(fd, &packet, IP_UDP_DHCP_SIZE - padding, /*flags:*/ 0,
  162. (struct sockaddr *) &dest_sll, sizeof(dest_sll));
  163. msg = "sendto";
  164. ret_close:
  165. close(fd);
  166. if (result < 0) {
  167. ret_msg:
  168. bb_perror_msg(msg, "PACKET");
  169. }
  170. return result;
  171. }
  172. /* Let the kernel do all the work for packet generation */
  173. int FAST_FUNC udhcp_send_kernel_packet(struct dhcp_packet *dhcp_pkt,
  174. uint32_t source_nip, int source_port,
  175. uint32_t dest_nip, int dest_port)
  176. {
  177. struct sockaddr_in sa;
  178. unsigned padding;
  179. int fd;
  180. int result = -1;
  181. const char *msg;
  182. fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
  183. if (fd < 0) {
  184. msg = "socket(%s)";
  185. goto ret_msg;
  186. }
  187. setsockopt_reuseaddr(fd);
  188. memset(&sa, 0, sizeof(sa));
  189. sa.sin_family = AF_INET;
  190. sa.sin_port = htons(source_port);
  191. sa.sin_addr.s_addr = source_nip;
  192. if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
  193. msg = "bind(%s)";
  194. goto ret_close;
  195. }
  196. memset(&sa, 0, sizeof(sa));
  197. sa.sin_family = AF_INET;
  198. sa.sin_port = htons(dest_port);
  199. sa.sin_addr.s_addr = dest_nip;
  200. if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
  201. msg = "connect";
  202. goto ret_close;
  203. }
  204. udhcp_dump_packet(dhcp_pkt);
  205. padding = DHCP_OPTIONS_BUFSIZE - 1 - udhcp_end_option(dhcp_pkt->options);
  206. if (padding > DHCP_SIZE - 300)
  207. padding = DHCP_SIZE - 300;
  208. result = safe_write(fd, dhcp_pkt, DHCP_SIZE - padding);
  209. msg = "write";
  210. ret_close:
  211. close(fd);
  212. if (result < 0) {
  213. ret_msg:
  214. bb_perror_msg(msg, "UDP");
  215. }
  216. return result;
  217. }