d6_packet.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 "dhcpd.h"
  10. #include <netinet/in.h>
  11. #include <netinet/if_ether.h>
  12. #include <netpacket/packet.h>
  13. #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 2
  14. void FAST_FUNC d6_dump_packet(struct d6_packet *packet)
  15. {
  16. if (dhcp_verbose < 2)
  17. return;
  18. bb_error_msg(
  19. " xid %x"
  20. , packet->d6_xid32
  21. );
  22. //*bin2hex(buf, (void *) packet->chaddr, sizeof(packet->chaddr)) = '\0';
  23. //bb_error_msg(" chaddr %s", buf);
  24. }
  25. #endif
  26. int FAST_FUNC d6_recv_kernel_packet(struct in6_addr *peer_ipv6
  27. 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. log1("packet read error, ignoring");
  35. return bytes; /* returns -1 */
  36. }
  37. if (bytes < offsetof(struct d6_packet, d6_options)) {
  38. bb_error_msg("packet with bad magic, ignoring");
  39. return -2;
  40. }
  41. log1("received %s", "a packet");
  42. d6_dump_packet(packet);
  43. return bytes;
  44. }
  45. /* Construct a ipv6+udp header for a packet, send packet */
  46. int FAST_FUNC d6_send_raw_packet(
  47. struct d6_packet *d6_pkt, unsigned d6_pkt_size,
  48. struct in6_addr *src_ipv6, int source_port,
  49. struct in6_addr *dst_ipv6, int dest_port, const uint8_t *dest_arp,
  50. int ifindex)
  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 = 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. (uint16_t *)&packet + 2,
  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(
  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. int ifindex)
  120. {
  121. struct sockaddr_in6 sa;
  122. int fd;
  123. int result = -1;
  124. const char *msg;
  125. fd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
  126. if (fd < 0) {
  127. msg = "socket(%s)";
  128. goto ret_msg;
  129. }
  130. setsockopt_reuseaddr(fd);
  131. memset(&sa, 0, sizeof(sa));
  132. sa.sin6_family = AF_INET6;
  133. sa.sin6_port = htons(source_port);
  134. sa.sin6_addr = *src_ipv6; /* struct copy */
  135. if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
  136. msg = "bind(%s)";
  137. goto ret_close;
  138. }
  139. memset(&sa, 0, sizeof(sa));
  140. sa.sin6_family = AF_INET6;
  141. sa.sin6_port = htons(dest_port);
  142. sa.sin6_addr = *dst_ipv6; /* struct copy */
  143. sa.sin6_scope_id = ifindex;
  144. if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
  145. msg = "connect";
  146. goto ret_close;
  147. }
  148. d6_dump_packet(d6_pkt);
  149. result = safe_write(fd, d6_pkt, d6_pkt_size);
  150. msg = "write";
  151. ret_close:
  152. close(fd);
  153. if (result < 0) {
  154. ret_msg:
  155. bb_perror_msg(msg, "UDP");
  156. }
  157. return result;
  158. }