3
0

packet.c 6.8 KB

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