rx_ip.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Received Data frame processing for IPv4 packets
  3. * Copyright (c) 2010, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "utils/includes.h"
  15. #include <netinet/ip.h>
  16. #include <netinet/ip_icmp.h>
  17. #include "utils/common.h"
  18. #include "wlantest.h"
  19. static void rx_data_icmp(struct wlantest *wt, const u8 *bssid,
  20. const u8 *sta_addr, u32 dst, u32 src,
  21. const u8 *data, size_t len)
  22. {
  23. struct in_addr addr;
  24. char buf[20];
  25. const struct icmphdr *hdr;
  26. u16 id, seq;
  27. struct wlantest_bss *bss;
  28. struct wlantest_sta *sta;
  29. hdr = (const struct icmphdr *) data;
  30. if (len < 4)
  31. return;
  32. /* TODO: check hdr->checksum */
  33. if (hdr->type != ICMP_ECHOREPLY && hdr->type != ICMP_ECHO)
  34. return;
  35. if (len < 8)
  36. return;
  37. id = ntohs(hdr->un.echo.id);
  38. seq = ntohs(hdr->un.echo.sequence);
  39. addr.s_addr = dst;
  40. snprintf(buf, sizeof(buf), "%s", inet_ntoa(addr));
  41. addr.s_addr = src;
  42. wpa_printf(MSG_DEBUG, "ICMP echo %s %s -> %s id=%04x seq=%u len=%u",
  43. hdr->type == ICMP_ECHO ? "request" : "response",
  44. inet_ntoa(addr), buf, id, seq, (unsigned) len - 8);
  45. bss = bss_find(wt, bssid);
  46. if (bss == NULL) {
  47. wpa_printf(MSG_INFO, "No BSS " MACSTR " known for ICMP packet",
  48. MAC2STR(bssid));
  49. return;
  50. }
  51. if (sta_addr == NULL)
  52. return; /* FromDS broadcast ping */
  53. sta = sta_find(bss, sta_addr);
  54. if (sta == NULL) {
  55. wpa_printf(MSG_INFO, "No STA " MACSTR " known for ICMP packet",
  56. MAC2STR(sta_addr));
  57. return;
  58. }
  59. if (hdr->type == ICMP_ECHO) {
  60. sta->icmp_echo_req_src = src;
  61. sta->icmp_echo_req_dst = dst;
  62. sta->icmp_echo_req_id = id;
  63. sta->icmp_echo_req_seq = seq;
  64. return;
  65. }
  66. if (sta->icmp_echo_req_src == dst &&
  67. sta->icmp_echo_req_dst == src &&
  68. sta->icmp_echo_req_id == id &&
  69. sta->icmp_echo_req_seq == seq) {
  70. sta->counters[WLANTEST_STA_COUNTER_PING_OK]++;
  71. wpa_printf(MSG_DEBUG, "ICMP echo (ping) match for STA " MACSTR,
  72. MAC2STR(sta->addr));
  73. }
  74. }
  75. void rx_data_ip(struct wlantest *wt, const u8 *bssid, const u8 *sta_addr,
  76. const u8 *dst, const u8 *src, const u8 *data, size_t len)
  77. {
  78. const struct iphdr *ip;
  79. const u8 *payload;
  80. size_t plen;
  81. u16 frag_off, tot_len;
  82. ip = (const struct iphdr *) data;
  83. if (len < sizeof(*ip))
  84. return;
  85. if (ip->version != 4) {
  86. wpa_printf(MSG_DEBUG, "Unexpected IP protocol version %u in "
  87. "IPv4 packet (bssid=" MACSTR " str=" MACSTR
  88. " dst=" MACSTR ")", ip->version, MAC2STR(bssid),
  89. MAC2STR(src), MAC2STR(dst));
  90. return;
  91. }
  92. if (ip->ihl * 4 < sizeof(*ip)) {
  93. wpa_printf(MSG_DEBUG, "Unexpected IP header length %u in "
  94. "IPv4 packet (bssid=" MACSTR " str=" MACSTR
  95. " dst=" MACSTR ")", ip->ihl, MAC2STR(bssid),
  96. MAC2STR(src), MAC2STR(dst));
  97. return;
  98. }
  99. if (ip->ihl * 4 > len) {
  100. wpa_printf(MSG_DEBUG, "Truncated IP header (ihl=%u len=%u) in "
  101. "IPv4 packet (bssid=" MACSTR " str=" MACSTR
  102. " dst=" MACSTR ")", ip->ihl, (unsigned) len,
  103. MAC2STR(bssid), MAC2STR(src), MAC2STR(dst));
  104. return;
  105. }
  106. /* TODO: check header checksum in ip->check */
  107. frag_off = be_to_host16(ip->frag_off);
  108. if (frag_off & 0x1fff) {
  109. wpa_printf(MSG_EXCESSIVE, "IP fragment reassembly not yet "
  110. "supported");
  111. return;
  112. }
  113. tot_len = be_to_host16(ip->tot_len);
  114. if (tot_len > len)
  115. return;
  116. if (tot_len < len)
  117. len = tot_len;
  118. payload = data + 4 * ip->ihl;
  119. plen = len - 4 * ip->ihl;
  120. switch (ip->protocol) {
  121. case IPPROTO_ICMP:
  122. rx_data_icmp(wt, bssid, sta_addr, ip->daddr, ip->saddr,
  123. payload, plen);
  124. break;
  125. }
  126. }