d6_packet.c 4.4 KB

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