wlantest: Add ping_ok_first_assoc STA counter
[mech_eap.git] / wlantest / rx_ip.c
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
15 #include "utils/includes.h"
16 #include <netinet/ip.h>
17 #include <netinet/ip_icmp.h>
18
19 #include "utils/common.h"
20 #include "wlantest.h"
21
22
23 static void rx_data_icmp(struct wlantest *wt, const u8 *bssid,
24                          const u8 *sta_addr, u32 dst, u32 src,
25                          const u8 *data, size_t len)
26 {
27         struct in_addr addr;
28         char buf[20];
29         const struct icmphdr *hdr;
30         u16 id, seq;
31         struct wlantest_bss *bss;
32         struct wlantest_sta *sta;
33
34         hdr = (const struct icmphdr *) data;
35         if (len < 4)
36                 return;
37
38         /* TODO: check hdr->checksum */
39
40         if (hdr->type != ICMP_ECHOREPLY && hdr->type != ICMP_ECHO)
41                 return;
42         if (len < 8)
43                 return;
44
45         id = ntohs(hdr->un.echo.id);
46         seq = ntohs(hdr->un.echo.sequence);
47
48         addr.s_addr = dst;
49         snprintf(buf, sizeof(buf), "%s", inet_ntoa(addr));
50         addr.s_addr = src;
51         wpa_printf(MSG_DEBUG, "ICMP echo %s %s -> %s id=%04x seq=%u len=%u",
52                    hdr->type == ICMP_ECHO ? "request" : "response",
53                    inet_ntoa(addr), buf, id, seq, (unsigned) len - 8);
54
55         bss = bss_find(wt, bssid);
56         if (bss == NULL) {
57                 wpa_printf(MSG_INFO, "No BSS " MACSTR " known for ICMP packet",
58                            MAC2STR(bssid));
59                 return;
60         }
61
62         if (sta_addr == NULL)
63                 return; /* FromDS broadcast ping */
64
65         sta = sta_find(bss, sta_addr);
66         if (sta == NULL) {
67                 wpa_printf(MSG_INFO, "No STA " MACSTR " known for ICMP packet",
68                            MAC2STR(sta_addr));
69                 return;
70         }
71
72         if (hdr->type == ICMP_ECHO) {
73                 sta->icmp_echo_req_src = src;
74                 sta->icmp_echo_req_dst = dst;
75                 sta->icmp_echo_req_id = id;
76                 sta->icmp_echo_req_seq = seq;
77                 return;
78         }
79
80         if (sta->icmp_echo_req_src == dst &&
81             sta->icmp_echo_req_dst == src &&
82             sta->icmp_echo_req_id == id &&
83             sta->icmp_echo_req_seq == seq) {
84                 sta->counters[WLANTEST_STA_COUNTER_PING_OK]++;
85                 if (sta->counters[WLANTEST_STA_COUNTER_ASSOCREQ_TX] == 0 &&
86                     sta->counters[WLANTEST_STA_COUNTER_REASSOCREQ_TX] == 0)
87                         sta->counters[
88                                 WLANTEST_STA_COUNTER_PING_OK_FIRST_ASSOC]++;
89                 wpa_printf(MSG_DEBUG, "ICMP echo (ping) match for STA " MACSTR,
90                            MAC2STR(sta->addr));
91         }
92 }
93
94
95 void rx_data_ip(struct wlantest *wt, const u8 *bssid, const u8 *sta_addr,
96                 const u8 *dst, const u8 *src, const u8 *data, size_t len)
97 {
98         const struct iphdr *ip;
99         const u8 *payload;
100         size_t plen;
101         u16 frag_off, tot_len;
102
103         ip = (const struct iphdr *) data;
104         if (len < sizeof(*ip))
105                 return;
106         if (ip->version != 4) {
107                 wpa_printf(MSG_DEBUG, "Unexpected IP protocol version %u in "
108                            "IPv4 packet (bssid=" MACSTR " str=" MACSTR
109                            " dst=" MACSTR ")", ip->version, MAC2STR(bssid),
110                            MAC2STR(src), MAC2STR(dst));
111                 return;
112         }
113         if (ip->ihl * 4 < sizeof(*ip)) {
114                 wpa_printf(MSG_DEBUG, "Unexpected IP header length %u in "
115                            "IPv4 packet (bssid=" MACSTR " str=" MACSTR
116                            " dst=" MACSTR ")", ip->ihl, MAC2STR(bssid),
117                            MAC2STR(src), MAC2STR(dst));
118                 return;
119         }
120         if (ip->ihl * 4 > len) {
121                 wpa_printf(MSG_DEBUG, "Truncated IP header (ihl=%u len=%u) in "
122                            "IPv4 packet (bssid=" MACSTR " str=" MACSTR
123                            " dst=" MACSTR ")", ip->ihl, (unsigned) len,
124                            MAC2STR(bssid), MAC2STR(src), MAC2STR(dst));
125                 return;
126         }
127
128         /* TODO: check header checksum in ip->check */
129
130         frag_off = be_to_host16(ip->frag_off);
131         if (frag_off & 0x1fff) {
132                 wpa_printf(MSG_EXCESSIVE, "IP fragment reassembly not yet "
133                            "supported");
134                 return;
135         }
136
137         tot_len = be_to_host16(ip->tot_len);
138         if (tot_len > len)
139                 return;
140         if (tot_len < len)
141                 len = tot_len;
142
143         payload = data + 4 * ip->ihl;
144         plen = len - 4 * ip->ihl;
145
146         switch (ip->protocol) {
147         case IPPROTO_ICMP:
148                 rx_data_icmp(wt, bssid, sta_addr, ip->daddr, ip->saddr,
149                              payload, plen);
150                 break;
151         }
152 }