hwsim_test.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * hwsim_test - Data connectivity test for mac80211_hwsim
  3. * Copyright (c) 2009, Atheros Communications
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <unistd.h>
  12. #include <sys/ioctl.h>
  13. #include <sys/socket.h>
  14. #include <sys/select.h>
  15. #include <netpacket/packet.h>
  16. #include <net/ethernet.h>
  17. #include <net/if.h>
  18. #include <arpa/inet.h>
  19. #define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
  20. #define MACSTR "%02x:%02x:%02x:%02x:%02x:%02x"
  21. #define HWSIM_ETHERTYPE ETHERTYPE_IP
  22. #define HWSIM_PACKETLEN 1500
  23. static unsigned char addr1[ETH_ALEN], addr2[ETH_ALEN], bcast[ETH_ALEN];
  24. static void tx(int s, const char *ifname, int ifindex,
  25. const unsigned char *src, const unsigned char *dst)
  26. {
  27. char buf[HWSIM_PACKETLEN], *pos;
  28. struct ether_header *eth;
  29. int i;
  30. printf("TX: %s(ifindex=%d) " MACSTR " -> " MACSTR "\n",
  31. ifname, ifindex, MAC2STR(src), MAC2STR(dst));
  32. eth = (struct ether_header *) buf;
  33. memcpy(eth->ether_dhost, dst, ETH_ALEN);
  34. memcpy(eth->ether_shost, src, ETH_ALEN);
  35. eth->ether_type = htons(HWSIM_ETHERTYPE);
  36. pos = (char *) (eth + 1);
  37. for (i = 0; i < sizeof(buf) - sizeof(*eth); i++)
  38. *pos++ = i;
  39. if (send(s, buf, sizeof(buf), 0) < 0)
  40. perror("send");
  41. }
  42. struct rx_result {
  43. int rx_unicast1:1;
  44. int rx_broadcast1:1;
  45. int rx_unicast2:1;
  46. int rx_broadcast2:1;
  47. };
  48. static void rx(int s, int iface, const char *ifname, int ifindex,
  49. struct rx_result *res)
  50. {
  51. char buf[HWSIM_PACKETLEN + 1], *pos;
  52. struct ether_header *eth;
  53. int len, i;
  54. len = recv(s, buf, sizeof(buf), 0);
  55. if (len < 0) {
  56. perror("recv");
  57. return;
  58. }
  59. eth = (struct ether_header *) buf;
  60. printf("RX: %s(ifindex=%d) " MACSTR " -> " MACSTR " (len=%d)\n",
  61. ifname, ifindex,
  62. MAC2STR(eth->ether_shost), MAC2STR(eth->ether_dhost), len);
  63. if (len != HWSIM_PACKETLEN) {
  64. printf("Ignore frame with unexpected RX length\n");
  65. return;
  66. }
  67. pos = (char *) (eth + 1);
  68. for (i = 0; i < sizeof(buf) - 1 - sizeof(*eth); i++) {
  69. if ((unsigned char) *pos != (unsigned char) i) {
  70. printf("Ignore frame with unexpected contents\n");
  71. printf("i=%d received=0x%x expected=0x%x\n",
  72. i, (unsigned char) *pos, (unsigned char) i);
  73. return;
  74. }
  75. pos++;
  76. }
  77. if (iface == 1 &&
  78. memcmp(eth->ether_dhost, addr1, ETH_ALEN) == 0 &&
  79. memcmp(eth->ether_shost, addr2, ETH_ALEN) == 0)
  80. res->rx_unicast1 = 1;
  81. else if (iface == 1 &&
  82. memcmp(eth->ether_dhost, bcast, ETH_ALEN) == 0 &&
  83. memcmp(eth->ether_shost, addr2, ETH_ALEN) == 0)
  84. res->rx_broadcast1 = 1;
  85. else if (iface == 2 &&
  86. memcmp(eth->ether_dhost, addr2, ETH_ALEN) == 0 &&
  87. memcmp(eth->ether_shost, addr1, ETH_ALEN) == 0)
  88. res->rx_unicast2 = 1;
  89. else if (iface == 2 &&
  90. memcmp(eth->ether_dhost, bcast, ETH_ALEN) == 0 &&
  91. memcmp(eth->ether_shost, addr1, ETH_ALEN) == 0)
  92. res->rx_broadcast2 = 1;
  93. }
  94. int main(int argc, char *argv[])
  95. {
  96. int s1 = -1, s2 = -1, ret = -1;
  97. struct ifreq ifr;
  98. int ifindex1, ifindex2;
  99. struct sockaddr_ll ll;
  100. fd_set rfds;
  101. struct timeval tv;
  102. struct rx_result res;
  103. if (argc != 3) {
  104. fprintf(stderr, "usage: hwsim_test <ifname1> <ifname2>\n");
  105. return -1;
  106. }
  107. memset(bcast, 0xff, ETH_ALEN);
  108. s1 = socket(PF_PACKET, SOCK_RAW, htons(HWSIM_ETHERTYPE));
  109. if (s1 < 0) {
  110. perror("socket");
  111. goto fail;
  112. }
  113. s2 = socket(PF_PACKET, SOCK_RAW, htons(HWSIM_ETHERTYPE));
  114. if (s2 < 0) {
  115. perror("socket");
  116. goto fail;
  117. }
  118. memset(&ifr, 0, sizeof(ifr));
  119. strncpy(ifr.ifr_name, argv[1], sizeof(ifr.ifr_name));
  120. if (ioctl(s1, SIOCGIFINDEX, &ifr) < 0) {
  121. perror("ioctl[SIOCGIFINDEX]");
  122. goto fail;
  123. }
  124. ifindex1 = ifr.ifr_ifindex;
  125. if (ioctl(s1, SIOCGIFHWADDR, &ifr) < 0) {
  126. perror("ioctl[SIOCGIFHWADDR]");
  127. goto fail;
  128. }
  129. memcpy(addr1, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
  130. memset(&ifr, 0, sizeof(ifr));
  131. strncpy(ifr.ifr_name, argv[2], sizeof(ifr.ifr_name));
  132. if (ioctl(s2, SIOCGIFINDEX, &ifr) < 0) {
  133. perror("ioctl[SIOCGIFINDEX]");
  134. goto fail;
  135. }
  136. ifindex2 = ifr.ifr_ifindex;
  137. if (ioctl(s2, SIOCGIFHWADDR, &ifr) < 0) {
  138. perror("ioctl[SIOCGIFHWADDR]");
  139. goto fail;
  140. }
  141. memcpy(addr2, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
  142. memset(&ll, 0, sizeof(ll));
  143. ll.sll_family = PF_PACKET;
  144. ll.sll_ifindex = ifindex1;
  145. ll.sll_protocol = htons(HWSIM_ETHERTYPE);
  146. if (bind(s1, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
  147. perror("bind");
  148. goto fail;
  149. }
  150. memset(&ll, 0, sizeof(ll));
  151. ll.sll_family = PF_PACKET;
  152. ll.sll_ifindex = ifindex2;
  153. ll.sll_protocol = htons(HWSIM_ETHERTYPE);
  154. if (bind(s2, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
  155. perror("bind");
  156. goto fail;
  157. }
  158. tx(s1, argv[1], ifindex1, addr1, addr2);
  159. tx(s1, argv[1], ifindex1, addr1, bcast);
  160. tx(s2, argv[2], ifindex2, addr2, addr1);
  161. tx(s2, argv[2], ifindex2, addr2, bcast);
  162. tv.tv_sec = 1;
  163. tv.tv_usec = 0;
  164. memset(&res, 0, sizeof(res));
  165. for (;;) {
  166. int r;
  167. FD_ZERO(&rfds);
  168. FD_SET(s1, &rfds);
  169. FD_SET(s2, &rfds);
  170. r = select(s2 + 1, &rfds, NULL, NULL, &tv);
  171. if (r < 0) {
  172. perror("select");
  173. goto fail;
  174. }
  175. if (r == 0)
  176. break; /* timeout */
  177. if (FD_ISSET(s1, &rfds))
  178. rx(s1, 1, argv[1], ifindex1, &res);
  179. if (FD_ISSET(s2, &rfds))
  180. rx(s2, 2, argv[2], ifindex2, &res);
  181. if (res.rx_unicast1 && res.rx_broadcast1 &&
  182. res.rx_unicast2 && res.rx_broadcast2) {
  183. ret = 0;
  184. break;
  185. }
  186. }
  187. if (ret) {
  188. printf("Did not receive all expected frames:\n"
  189. "rx_unicast1=%d rx_broadcast1=%d "
  190. "rx_unicast2=%d rx_broadcast2=%d\n",
  191. res.rx_unicast1, res.rx_broadcast1,
  192. res.rx_unicast2, res.rx_broadcast2);
  193. } else {
  194. printf("Both unicast and broadcast working in both "
  195. "directions\n");
  196. }
  197. fail:
  198. close(s1);
  199. close(s2);
  200. return ret;
  201. }