packet.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. IF_FEATURE_UDHCPD_BOOTP(case MSGTYPE_BOOTP:)
  21. /* reply to a BOOTP (not DHCP) client */
  22. case DHCPOFFER:
  23. case DHCPACK:
  24. case DHCPNAK:
  25. packet->op = BOOTREPLY; /* if server to client */
  26. }
  27. packet->htype = 1; /* ethernet */
  28. packet->hlen = 6;
  29. packet->cookie = htonl(RFC1048_MAGIC);
  30. if (DHCP_END != 0)
  31. packet->options[0] = DHCP_END;
  32. IF_FEATURE_UDHCPD_BOOTP(if (type != MSGTYPE_BOOTP))
  33. udhcp_add_simple_option(packet, DHCP_MESSAGE_TYPE, type);
  34. }
  35. #endif
  36. #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 2
  37. void FAST_FUNC udhcp_dump_packet(struct dhcp_packet *packet)
  38. {
  39. char buf[sizeof(packet->chaddr)*2 + 1];
  40. if (dhcp_verbose < 2)
  41. return;
  42. bb_info_msg(
  43. //" op %x"
  44. //" htype %x"
  45. " hlen %x"
  46. //" hops %x"
  47. " xid %x"
  48. //" secs %x"
  49. //" flags %x"
  50. " ciaddr %x"
  51. " yiaddr %x"
  52. " siaddr %x"
  53. " giaddr %x"
  54. //" sname %s"
  55. //" file %s"
  56. //" cookie %x"
  57. //" options %s"
  58. //, packet->op
  59. //, packet->htype
  60. , packet->hlen
  61. //, packet->hops
  62. , packet->xid
  63. //, packet->secs
  64. //, packet->flags
  65. , packet->ciaddr
  66. , packet->yiaddr
  67. , packet->siaddr_nip
  68. , packet->gateway_nip
  69. //, packet->sname[64]
  70. //, packet->file[128]
  71. //, packet->cookie
  72. //, packet->options[]
  73. );
  74. *bin2hex(buf, (void *) packet->chaddr, sizeof(packet->chaddr)) = '\0';
  75. bb_info_msg(" chaddr %s", buf);
  76. }
  77. #endif
  78. /* Read a packet from socket fd, return -1 on read error, -2 on packet error */
  79. int FAST_FUNC udhcp_recv_kernel_packet(struct dhcp_packet *packet, int fd)
  80. {
  81. int bytes;
  82. memset(packet, 0, sizeof(*packet));
  83. bytes = safe_read(fd, packet, sizeof(*packet));
  84. if (bytes < 0) {
  85. log1s("packet read error, ignoring");
  86. return bytes; /* returns -1 */
  87. }
  88. if (bytes < offsetof(struct dhcp_packet, options)
  89. || packet->cookie != htonl(RFC1048_MAGIC)
  90. ) {
  91. bb_simple_info_msg("packet with bad magic, ignoring");
  92. return -2;
  93. }
  94. log2("received %s", "a packet");
  95. /* log2 because more informative msg for valid packets is printed later at log1 level */
  96. udhcp_dump_packet(packet);
  97. return bytes;
  98. }
  99. /* Construct a ip/udp header for a packet, send packet */
  100. int FAST_FUNC udhcp_send_raw_packet(struct dhcp_packet *dhcp_pkt,
  101. uint32_t source_nip, int source_port,
  102. uint32_t dest_nip, int dest_port, const uint8_t *dest_arp,
  103. int ifindex)
  104. {
  105. struct sockaddr_ll dest_sll;
  106. struct ip_udp_dhcp_packet packet;
  107. unsigned padding;
  108. int fd;
  109. int result = -1;
  110. const char *msg;
  111. fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
  112. if (fd < 0) {
  113. msg = "socket(%s)";
  114. goto ret_msg;
  115. }
  116. memset(&dest_sll, 0, sizeof(dest_sll));
  117. memset(&packet, 0, offsetof(struct ip_udp_dhcp_packet, data));
  118. packet.data = *dhcp_pkt; /* struct copy */
  119. dest_sll.sll_family = AF_PACKET;
  120. dest_sll.sll_protocol = htons(ETH_P_IP);
  121. dest_sll.sll_ifindex = ifindex;
  122. /*dest_sll.sll_hatype = ARPHRD_???;*/
  123. /*dest_sll.sll_pkttype = PACKET_???;*/
  124. dest_sll.sll_halen = 6;
  125. memcpy(dest_sll.sll_addr, dest_arp, 6);
  126. //TODO: is bind() necessary? we sendto() to this destination, should work anyway
  127. if (bind(fd, (struct sockaddr *)&dest_sll, sizeof(dest_sll)) < 0) {
  128. msg = "bind(%s)";
  129. goto ret_close;
  130. }
  131. /* We were sending full-sized DHCP packets (zero padded),
  132. * but some badly configured servers were seen dropping them.
  133. * Apparently they drop all DHCP packets >576 *ethernet* octets big,
  134. * whereas they may only drop packets >576 *IP* octets big
  135. * (which for typical Ethernet II means 590 octets: 6+6+2 + 576).
  136. *
  137. * In order to work with those buggy servers,
  138. * we truncate packets after end option byte.
  139. *
  140. * However, RFC 1542 says "The IP Total Length and UDP Length
  141. * must be large enough to contain the minimal BOOTP header of 300 octets".
  142. * Thus, we retain enough padding to not go below 300 BOOTP bytes.
  143. * Some devices have filters which drop DHCP packets shorter than that.
  144. */
  145. padding = DHCP_OPTIONS_BUFSIZE - 1 - udhcp_end_option(packet.data.options);
  146. if (padding > DHCP_SIZE - 300)
  147. padding = DHCP_SIZE - 300;
  148. packet.ip.protocol = IPPROTO_UDP;
  149. packet.ip.saddr = source_nip;
  150. packet.ip.daddr = dest_nip;
  151. packet.udp.source = htons(source_port);
  152. packet.udp.dest = htons(dest_port);
  153. /* size, excluding IP header: */
  154. packet.udp.len = htons(UDP_DHCP_SIZE - padding);
  155. /* for UDP checksumming, ip.len is set to UDP packet len */
  156. packet.ip.tot_len = packet.udp.len;
  157. packet.udp.check = inet_cksum(&packet,
  158. IP_UDP_DHCP_SIZE - padding);
  159. /* but for sending, it is set to IP packet len */
  160. packet.ip.tot_len = htons(IP_UDP_DHCP_SIZE - padding);
  161. packet.ip.ihl = sizeof(packet.ip) >> 2;
  162. packet.ip.version = IPVERSION;
  163. packet.ip.ttl = IPDEFTTL;
  164. packet.ip.check = inet_cksum(&packet.ip, sizeof(packet.ip));
  165. udhcp_dump_packet(dhcp_pkt);
  166. result = sendto(fd, &packet, IP_UDP_DHCP_SIZE - padding, /*flags:*/ 0,
  167. (struct sockaddr *) &dest_sll, sizeof(dest_sll));
  168. msg = "sendto";
  169. ret_close:
  170. close(fd);
  171. if (result < 0) {
  172. ret_msg:
  173. bb_perror_msg(msg, "PACKET");
  174. }
  175. return result;
  176. }
  177. /* Let the kernel do all the work for packet generation */
  178. int FAST_FUNC udhcp_send_kernel_packet(struct dhcp_packet *dhcp_pkt,
  179. uint32_t source_nip, int source_port,
  180. uint32_t dest_nip, int dest_port,
  181. const char *ifname)
  182. {
  183. struct sockaddr_in sa;
  184. unsigned padding;
  185. int fd;
  186. int result = -1;
  187. const char *msg;
  188. fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
  189. if (fd < 0) {
  190. msg = "socket(%s)";
  191. goto ret_msg;
  192. }
  193. setsockopt_reuseaddr(fd);
  194. /* If interface carrier goes down, unless we
  195. * bind socket to a particular netdev, the packet
  196. * can go out through another interface, eg. via
  197. * default route despite being bound to a specific
  198. * source IP. As such, bind to device hard and fail
  199. * otherwise. Sending renewal packets on foreign
  200. * interfaces makes no sense.
  201. */
  202. if (ifname) {
  203. if (setsockopt_bindtodevice(fd, ifname) < 0) {
  204. msg = "bindtodevice";
  205. goto ret_close;
  206. }
  207. }
  208. memset(&sa, 0, sizeof(sa));
  209. sa.sin_family = AF_INET;
  210. sa.sin_port = htons(source_port);
  211. sa.sin_addr.s_addr = source_nip;
  212. if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
  213. msg = "bind(%s)";
  214. goto ret_close;
  215. }
  216. memset(&sa, 0, sizeof(sa));
  217. sa.sin_family = AF_INET;
  218. sa.sin_port = htons(dest_port);
  219. sa.sin_addr.s_addr = dest_nip;
  220. if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
  221. msg = "connect";
  222. goto ret_close;
  223. }
  224. udhcp_dump_packet(dhcp_pkt);
  225. padding = DHCP_OPTIONS_BUFSIZE - 1 - udhcp_end_option(dhcp_pkt->options);
  226. if (padding > DHCP_SIZE - 300)
  227. padding = DHCP_SIZE - 300;
  228. result = safe_write(fd, dhcp_pkt, DHCP_SIZE - padding);
  229. msg = "write";
  230. ret_close:
  231. close(fd);
  232. if (result < 0) {
  233. ret_msg:
  234. bb_perror_msg(msg, "UDP");
  235. }
  236. return result;
  237. }