3
0

d6_packet.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Copyright (C) 2011 Denys Vlasenko.
  4. *
  5. * Licensed under GPLv2, see file LICENSE in this source tree.
  6. */
  7. #include "common.h"
  8. #include "d6_common.h"
  9. #include "dhcpc.h"
  10. #include "dhcpd.h"
  11. #include <netinet/in.h>
  12. #include <netinet/if_ether.h>
  13. #include <netpacket/packet.h>
  14. #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 2
  15. void FAST_FUNC d6_dump_packet(struct d6_packet *packet)
  16. {
  17. if (dhcp_verbose < 2)
  18. return;
  19. bb_info_msg(
  20. " xid %x"
  21. , packet->d6_xid32
  22. );
  23. //*bin2hex(buf, (void *) packet->chaddr, sizeof(packet->chaddr)) = '\0';
  24. //bb_error_msg(" chaddr %s", buf);
  25. }
  26. #endif
  27. int FAST_FUNC d6_recv_kernel_packet(struct in6_addr *peer_ipv6 UNUSED_PARAM,
  28. struct d6_packet *packet, int fd)
  29. {
  30. int bytes;
  31. memset(packet, 0, sizeof(*packet));
  32. bytes = safe_read(fd, packet, sizeof(*packet));
  33. if (bytes < 0) {
  34. log1s("packet read error, ignoring");
  35. return bytes; /* returns -1 */
  36. }
  37. if (bytes < offsetof(struct d6_packet, d6_options)) {
  38. bb_simple_info_msg("packet with bad magic, ignoring");
  39. return -2;
  40. }
  41. log2("received %s", "a packet");
  42. /* log2 because more informative msg for valid packets is printed later at log1 level */
  43. d6_dump_packet(packet);
  44. return bytes;
  45. }
  46. /* Construct a ipv6+udp header for a packet, send packet */
  47. int FAST_FUNC d6_send_raw_packet_from_client_data_ifindex(
  48. struct d6_packet *d6_pkt, unsigned d6_pkt_size,
  49. struct in6_addr *src_ipv6, int source_port,
  50. struct in6_addr *dst_ipv6, int dest_port, const uint8_t *dest_arp)
  51. {
  52. struct sockaddr_ll dest_sll;
  53. struct ip6_udp_d6_packet packet;
  54. int fd;
  55. int result = -1;
  56. const char *msg;
  57. fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IPV6));
  58. if (fd < 0) {
  59. msg = "socket(%s)";
  60. goto ret_msg;
  61. }
  62. memset(&dest_sll, 0, sizeof(dest_sll));
  63. memset(&packet, 0, offsetof(struct ip6_udp_d6_packet, data));
  64. packet.data = *d6_pkt; /* struct copy */
  65. dest_sll.sll_family = AF_PACKET;
  66. dest_sll.sll_protocol = htons(ETH_P_IPV6);
  67. dest_sll.sll_ifindex = client_data.ifindex;
  68. /*dest_sll.sll_hatype = ARPHRD_???;*/
  69. /*dest_sll.sll_pkttype = PACKET_???;*/
  70. dest_sll.sll_halen = 6;
  71. memcpy(dest_sll.sll_addr, dest_arp, 6);
  72. //TODO: is bind() necessary? we sendto() to this destination, should work anyway
  73. if (bind(fd, (struct sockaddr *)&dest_sll, sizeof(dest_sll)) < 0) {
  74. msg = "bind(%s)";
  75. goto ret_close;
  76. }
  77. packet.ip6.ip6_vfc = (6 << 4); /* 4 bits version, top 4 bits of tclass */
  78. // In case we have no IPv6 on our interface at all, we can try
  79. // to fill "all hosts" mcast address as source:
  80. // /* FF02::1 is Link-local "All_Nodes" address */
  81. // packet.ip6.ip6_dst.s6_addr[0] = 0xff;
  82. // packet.ip6.ip6_dst.s6_addr[1] = 0x02;
  83. // packet.ip6.ip6_dst.s6_addr[15] = 0x01;
  84. // Maybe some servers will be able to respond to us this way?
  85. // Users report that leaving ::0 address there makes servers try to reply to ::0,
  86. // which doesn't work.
  87. if (src_ipv6)
  88. packet.ip6.ip6_src = *src_ipv6; /* struct copy */
  89. packet.ip6.ip6_dst = *dst_ipv6; /* struct copy */
  90. packet.udp.source = htons(source_port);
  91. packet.udp.dest = htons(dest_port);
  92. /* size, excluding IP header: */
  93. packet.udp.len = htons(sizeof(struct udphdr) + d6_pkt_size);
  94. packet.ip6.ip6_plen = packet.udp.len;
  95. /*
  96. * Someone was smoking weed (at least) while inventing UDP checksumming:
  97. * UDP checksum skips first four bytes of IPv6 header.
  98. * 'next header' field should be summed as if it is one more byte
  99. * to the right, therefore we write its value (IPPROTO_UDP)
  100. * into ip6_hlim, and its 'real' location remains zero-filled for now.
  101. */
  102. packet.ip6.ip6_hlim = IPPROTO_UDP;
  103. packet.udp.check = inet_cksum(
  104. (uint8_t *)&packet + 4,
  105. offsetof(struct ip6_udp_d6_packet, data) - 4 + d6_pkt_size
  106. );
  107. /* fix 'hop limit' and 'next header' after UDP checksumming */
  108. packet.ip6.ip6_hlim = 1; /* observed Windows machines to use hlim=1 */
  109. packet.ip6.ip6_nxt = IPPROTO_UDP;
  110. d6_dump_packet(d6_pkt);
  111. result = sendto(fd, &packet, offsetof(struct ip6_udp_d6_packet, data) + d6_pkt_size,
  112. /*flags:*/ 0,
  113. (struct sockaddr *) &dest_sll, sizeof(dest_sll)
  114. );
  115. msg = "sendto";
  116. ret_close:
  117. close(fd);
  118. if (result < 0) {
  119. ret_msg:
  120. bb_perror_msg(msg, "PACKET");
  121. }
  122. return result;
  123. }
  124. /* Let the kernel do all the work for packet generation */
  125. int FAST_FUNC d6_send_kernel_packet_from_client_data_ifindex(
  126. struct d6_packet *d6_pkt, unsigned d6_pkt_size,
  127. struct in6_addr *src_ipv6, int source_port,
  128. struct in6_addr *dst_ipv6, int dest_port)
  129. {
  130. struct sockaddr_in6 sa;
  131. int fd;
  132. int result = -1;
  133. const char *msg;
  134. fd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
  135. if (fd < 0) {
  136. msg = "socket(%s)";
  137. goto ret_msg;
  138. }
  139. setsockopt_reuseaddr(fd);
  140. memset(&sa, 0, sizeof(sa));
  141. sa.sin6_family = AF_INET6;
  142. sa.sin6_port = htons(source_port);
  143. sa.sin6_addr = *src_ipv6; /* struct copy */
  144. if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
  145. msg = "bind(%s)";
  146. goto ret_close;
  147. }
  148. memset(&sa, 0, sizeof(sa));
  149. sa.sin6_family = AF_INET6;
  150. sa.sin6_port = htons(dest_port);
  151. sa.sin6_addr = *dst_ipv6; /* struct copy */
  152. sa.sin6_scope_id = client_data.ifindex;
  153. if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
  154. msg = "connect";
  155. goto ret_close;
  156. }
  157. d6_dump_packet(d6_pkt);
  158. result = safe_write(fd, d6_pkt, d6_pkt_size);
  159. msg = "write";
  160. ret_close:
  161. close(fd);
  162. if (result < 0) {
  163. ret_msg:
  164. bb_perror_msg(msg, "UDP");
  165. }
  166. return result;
  167. }