d6_packet.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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
  28. UNUSED_PARAM
  29. , struct d6_packet *packet, int fd)
  30. {
  31. int bytes;
  32. memset(packet, 0, sizeof(*packet));
  33. bytes = safe_read(fd, packet, sizeof(*packet));
  34. if (bytes < 0) {
  35. log1s("packet read error, ignoring");
  36. return bytes; /* returns -1 */
  37. }
  38. if (bytes < offsetof(struct d6_packet, d6_options)) {
  39. bb_simple_info_msg("packet with bad magic, ignoring");
  40. return -2;
  41. }
  42. log1("received %s", "a packet");
  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. if (bind(fd, (struct sockaddr *)&dest_sll, sizeof(dest_sll)) < 0) {
  73. msg = "bind(%s)";
  74. goto ret_close;
  75. }
  76. packet.ip6.ip6_vfc = (6 << 4); /* 4 bits version, top 4 bits of tclass */
  77. if (src_ipv6)
  78. packet.ip6.ip6_src = *src_ipv6; /* struct copy */
  79. packet.ip6.ip6_dst = *dst_ipv6; /* struct copy */
  80. packet.udp.source = htons(source_port);
  81. packet.udp.dest = htons(dest_port);
  82. /* size, excluding IP header: */
  83. packet.udp.len = htons(sizeof(struct udphdr) + d6_pkt_size);
  84. packet.ip6.ip6_plen = packet.udp.len;
  85. /*
  86. * Someone was smoking weed (at least) while inventing UDP checksumming:
  87. * UDP checksum skips first four bytes of IPv6 header.
  88. * 'next header' field should be summed as if it is one more byte
  89. * to the right, therefore we write its value (IPPROTO_UDP)
  90. * into ip6_hlim, and its 'real' location remains zero-filled for now.
  91. */
  92. packet.ip6.ip6_hlim = IPPROTO_UDP;
  93. packet.udp.check = inet_cksum(
  94. (uint8_t *)&packet + 4,
  95. offsetof(struct ip6_udp_d6_packet, data) - 4 + d6_pkt_size
  96. );
  97. /* fix 'hop limit' and 'next header' after UDP checksumming */
  98. packet.ip6.ip6_hlim = 1; /* observed Windows machines to use hlim=1 */
  99. packet.ip6.ip6_nxt = IPPROTO_UDP;
  100. d6_dump_packet(d6_pkt);
  101. result = sendto(fd, &packet, offsetof(struct ip6_udp_d6_packet, data) + d6_pkt_size,
  102. /*flags:*/ 0,
  103. (struct sockaddr *) &dest_sll, sizeof(dest_sll)
  104. );
  105. msg = "sendto";
  106. ret_close:
  107. close(fd);
  108. if (result < 0) {
  109. ret_msg:
  110. bb_perror_msg(msg, "PACKET");
  111. }
  112. return result;
  113. }
  114. /* Let the kernel do all the work for packet generation */
  115. int FAST_FUNC d6_send_kernel_packet_from_client_data_ifindex(
  116. struct d6_packet *d6_pkt, unsigned d6_pkt_size,
  117. struct in6_addr *src_ipv6, int source_port,
  118. struct in6_addr *dst_ipv6, int dest_port)
  119. {
  120. struct sockaddr_in6 sa;
  121. int fd;
  122. int result = -1;
  123. const char *msg;
  124. fd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
  125. if (fd < 0) {
  126. msg = "socket(%s)";
  127. goto ret_msg;
  128. }
  129. setsockopt_reuseaddr(fd);
  130. memset(&sa, 0, sizeof(sa));
  131. sa.sin6_family = AF_INET6;
  132. sa.sin6_port = htons(source_port);
  133. sa.sin6_addr = *src_ipv6; /* struct copy */
  134. if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
  135. msg = "bind(%s)";
  136. goto ret_close;
  137. }
  138. memset(&sa, 0, sizeof(sa));
  139. sa.sin6_family = AF_INET6;
  140. sa.sin6_port = htons(dest_port);
  141. sa.sin6_addr = *dst_ipv6; /* struct copy */
  142. sa.sin6_scope_id = client_data.ifindex;
  143. if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
  144. msg = "connect";
  145. goto ret_close;
  146. }
  147. d6_dump_packet(d6_pkt);
  148. result = safe_write(fd, d6_pkt, d6_pkt_size);
  149. msg = "write";
  150. ret_close:
  151. close(fd);
  152. if (result < 0) {
  153. ret_msg:
  154. bb_perror_msg(msg, "UDP");
  155. }
  156. return result;
  157. }