packet.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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(&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(&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. const char *ifname)
  177. {
  178. struct sockaddr_in sa;
  179. unsigned padding;
  180. int fd;
  181. int result = -1;
  182. const char *msg;
  183. fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
  184. if (fd < 0) {
  185. msg = "socket(%s)";
  186. goto ret_msg;
  187. }
  188. setsockopt_reuseaddr(fd);
  189. /* If interface carrier goes down, unless we
  190. * bind socket to a particular netdev, the packet
  191. * can go out through another interface, eg. via
  192. * default route despite being bound to a specific
  193. * source IP. As such, bind to device hard and fail
  194. * otherwise. Sending renewal packets on foreign
  195. * interfaces makes no sense.
  196. */
  197. if (ifname) {
  198. if (setsockopt_bindtodevice(fd, ifname) < 0) {
  199. msg = "bindtodevice";
  200. goto ret_close;
  201. }
  202. }
  203. memset(&sa, 0, sizeof(sa));
  204. sa.sin_family = AF_INET;
  205. sa.sin_port = htons(source_port);
  206. sa.sin_addr.s_addr = source_nip;
  207. if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
  208. msg = "bind(%s)";
  209. goto ret_close;
  210. }
  211. memset(&sa, 0, sizeof(sa));
  212. sa.sin_family = AF_INET;
  213. sa.sin_port = htons(dest_port);
  214. sa.sin_addr.s_addr = dest_nip;
  215. if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
  216. msg = "connect";
  217. goto ret_close;
  218. }
  219. udhcp_dump_packet(dhcp_pkt);
  220. padding = DHCP_OPTIONS_BUFSIZE - 1 - udhcp_end_option(dhcp_pkt->options);
  221. if (padding > DHCP_SIZE - 300)
  222. padding = DHCP_SIZE - 300;
  223. result = safe_write(fd, dhcp_pkt, DHCP_SIZE - padding);
  224. msg = "write";
  225. ret_close:
  226. close(fd);
  227. if (result < 0) {
  228. ret_msg:
  229. bb_perror_msg(msg, "UDP");
  230. }
  231. return result;
  232. }