packet.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. log2("received %s", "a packet");
  92. /* log2 because more informative msg for valid packets is printed later at log1 level */
  93. udhcp_dump_packet(packet);
  94. return bytes;
  95. }
  96. /* Construct a ip/udp header for a packet, send packet */
  97. int FAST_FUNC udhcp_send_raw_packet(struct dhcp_packet *dhcp_pkt,
  98. uint32_t source_nip, int source_port,
  99. uint32_t dest_nip, int dest_port, const uint8_t *dest_arp,
  100. int ifindex)
  101. {
  102. struct sockaddr_ll dest_sll;
  103. struct ip_udp_dhcp_packet packet;
  104. unsigned padding;
  105. int fd;
  106. int result = -1;
  107. const char *msg;
  108. fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
  109. if (fd < 0) {
  110. msg = "socket(%s)";
  111. goto ret_msg;
  112. }
  113. memset(&dest_sll, 0, sizeof(dest_sll));
  114. memset(&packet, 0, offsetof(struct ip_udp_dhcp_packet, data));
  115. packet.data = *dhcp_pkt; /* struct copy */
  116. dest_sll.sll_family = AF_PACKET;
  117. dest_sll.sll_protocol = htons(ETH_P_IP);
  118. dest_sll.sll_ifindex = ifindex;
  119. /*dest_sll.sll_hatype = ARPHRD_???;*/
  120. /*dest_sll.sll_pkttype = PACKET_???;*/
  121. dest_sll.sll_halen = 6;
  122. memcpy(dest_sll.sll_addr, dest_arp, 6);
  123. if (bind(fd, (struct sockaddr *)&dest_sll, sizeof(dest_sll)) < 0) {
  124. msg = "bind(%s)";
  125. goto ret_close;
  126. }
  127. /* We were sending full-sized DHCP packets (zero padded),
  128. * but some badly configured servers were seen dropping them.
  129. * Apparently they drop all DHCP packets >576 *ethernet* octets big,
  130. * whereas they may only drop packets >576 *IP* octets big
  131. * (which for typical Ethernet II means 590 octets: 6+6+2 + 576).
  132. *
  133. * In order to work with those buggy servers,
  134. * we truncate packets after end option byte.
  135. *
  136. * However, RFC 1542 says "The IP Total Length and UDP Length
  137. * must be large enough to contain the minimal BOOTP header of 300 octets".
  138. * Thus, we retain enough padding to not go below 300 BOOTP bytes.
  139. * Some devices have filters which drop DHCP packets shorter than that.
  140. */
  141. padding = DHCP_OPTIONS_BUFSIZE - 1 - udhcp_end_option(packet.data.options);
  142. if (padding > DHCP_SIZE - 300)
  143. padding = DHCP_SIZE - 300;
  144. packet.ip.protocol = IPPROTO_UDP;
  145. packet.ip.saddr = source_nip;
  146. packet.ip.daddr = dest_nip;
  147. packet.udp.source = htons(source_port);
  148. packet.udp.dest = htons(dest_port);
  149. /* size, excluding IP header: */
  150. packet.udp.len = htons(UDP_DHCP_SIZE - padding);
  151. /* for UDP checksumming, ip.len is set to UDP packet len */
  152. packet.ip.tot_len = packet.udp.len;
  153. packet.udp.check = inet_cksum(&packet,
  154. IP_UDP_DHCP_SIZE - padding);
  155. /* but for sending, it is set to IP packet len */
  156. packet.ip.tot_len = htons(IP_UDP_DHCP_SIZE - padding);
  157. packet.ip.ihl = sizeof(packet.ip) >> 2;
  158. packet.ip.version = IPVERSION;
  159. packet.ip.ttl = IPDEFTTL;
  160. packet.ip.check = inet_cksum(&packet.ip, sizeof(packet.ip));
  161. udhcp_dump_packet(dhcp_pkt);
  162. result = sendto(fd, &packet, IP_UDP_DHCP_SIZE - padding, /*flags:*/ 0,
  163. (struct sockaddr *) &dest_sll, sizeof(dest_sll));
  164. msg = "sendto";
  165. ret_close:
  166. close(fd);
  167. if (result < 0) {
  168. ret_msg:
  169. bb_perror_msg(msg, "PACKET");
  170. }
  171. return result;
  172. }
  173. /* Let the kernel do all the work for packet generation */
  174. int FAST_FUNC udhcp_send_kernel_packet(struct dhcp_packet *dhcp_pkt,
  175. uint32_t source_nip, int source_port,
  176. uint32_t dest_nip, int dest_port,
  177. const char *ifname)
  178. {
  179. struct sockaddr_in sa;
  180. unsigned padding;
  181. int fd;
  182. int result = -1;
  183. const char *msg;
  184. fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
  185. if (fd < 0) {
  186. msg = "socket(%s)";
  187. goto ret_msg;
  188. }
  189. setsockopt_reuseaddr(fd);
  190. /* If interface carrier goes down, unless we
  191. * bind socket to a particular netdev, the packet
  192. * can go out through another interface, eg. via
  193. * default route despite being bound to a specific
  194. * source IP. As such, bind to device hard and fail
  195. * otherwise. Sending renewal packets on foreign
  196. * interfaces makes no sense.
  197. */
  198. if (ifname) {
  199. if (setsockopt_bindtodevice(fd, ifname) < 0) {
  200. msg = "bindtodevice";
  201. goto ret_close;
  202. }
  203. }
  204. memset(&sa, 0, sizeof(sa));
  205. sa.sin_family = AF_INET;
  206. sa.sin_port = htons(source_port);
  207. sa.sin_addr.s_addr = source_nip;
  208. if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
  209. msg = "bind(%s)";
  210. goto ret_close;
  211. }
  212. memset(&sa, 0, sizeof(sa));
  213. sa.sin_family = AF_INET;
  214. sa.sin_port = htons(dest_port);
  215. sa.sin_addr.s_addr = dest_nip;
  216. if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
  217. msg = "connect";
  218. goto ret_close;
  219. }
  220. udhcp_dump_packet(dhcp_pkt);
  221. padding = DHCP_OPTIONS_BUFSIZE - 1 - udhcp_end_option(dhcp_pkt->options);
  222. if (padding > DHCP_SIZE - 300)
  223. padding = DHCP_SIZE - 300;
  224. result = safe_write(fd, dhcp_pkt, DHCP_SIZE - padding);
  225. msg = "write";
  226. ret_close:
  227. close(fd);
  228. if (result < 0) {
  229. ret_msg:
  230. bb_perror_msg(msg, "UDP");
  231. }
  232. return result;
  233. }