2 * Received Data frame processing for IPv4 packets
3 * Copyright (c) 2010, Jouni Malinen <j@w1.fi>
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.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
15 #include "utils/includes.h"
16 #include <netinet/ip.h>
17 #include <netinet/ip_icmp.h>
19 #include "utils/common.h"
23 static void ping_update(struct wlantest_sta *sta, int req, u32 src, u32 dst,
27 sta->icmp_echo_req_src = src;
28 sta->icmp_echo_req_dst = dst;
29 sta->icmp_echo_req_id = id;
30 sta->icmp_echo_req_seq = seq;
34 if (sta->icmp_echo_req_src == dst &&
35 sta->icmp_echo_req_dst == src &&
36 sta->icmp_echo_req_id == id &&
37 sta->icmp_echo_req_seq == seq) {
38 sta->counters[WLANTEST_STA_COUNTER_PING_OK]++;
39 if (sta->counters[WLANTEST_STA_COUNTER_ASSOCREQ_TX] == 0 &&
40 sta->counters[WLANTEST_STA_COUNTER_REASSOCREQ_TX] == 0)
42 WLANTEST_STA_COUNTER_PING_OK_FIRST_ASSOC]++;
43 wpa_printf(MSG_DEBUG, "ICMP echo (ping) match for STA " MACSTR,
49 static void rx_data_icmp(struct wlantest *wt, const u8 *bssid,
50 const u8 *sta_addr, u32 dst, u32 src,
51 const u8 *data, size_t len, const u8 *peer_addr)
55 const struct icmphdr *hdr;
57 struct wlantest_bss *bss;
58 struct wlantest_sta *sta;
60 hdr = (const struct icmphdr *) data;
64 /* TODO: check hdr->checksum */
66 if (hdr->type != ICMP_ECHOREPLY && hdr->type != ICMP_ECHO)
71 id = ntohs(hdr->un.echo.id);
72 seq = ntohs(hdr->un.echo.sequence);
75 snprintf(buf, sizeof(buf), "%s", inet_ntoa(addr));
77 wpa_printf(MSG_DEBUG, "ICMP echo %s %s -> %s id=%04x seq=%u len=%u%s",
78 hdr->type == ICMP_ECHO ? "request" : "response",
79 inet_ntoa(addr), buf, id, seq, (unsigned) len - 8,
80 peer_addr ? " [DL]" : "");
82 bss = bss_find(wt, bssid);
84 wpa_printf(MSG_INFO, "No BSS " MACSTR " known for ICMP packet",
90 return; /* FromDS broadcast ping */
92 sta = sta_find(bss, sta_addr);
94 wpa_printf(MSG_INFO, "No STA " MACSTR " known for ICMP packet",
99 ping_update(sta, hdr->type == ICMP_ECHO, src, dst, id, seq);
100 if (peer_addr && (sta = sta_find(bss, peer_addr)))
101 ping_update(sta, hdr->type == ICMP_ECHO, src, dst, id, seq);
105 void rx_data_ip(struct wlantest *wt, const u8 *bssid, const u8 *sta_addr,
106 const u8 *dst, const u8 *src, const u8 *data, size_t len,
109 const struct iphdr *ip;
112 u16 frag_off, tot_len;
114 ip = (const struct iphdr *) data;
115 if (len < sizeof(*ip))
117 if (ip->version != 4) {
118 wpa_printf(MSG_DEBUG, "Unexpected IP protocol version %u in "
119 "IPv4 packet (bssid=" MACSTR " str=" MACSTR
120 " dst=" MACSTR ")", ip->version, MAC2STR(bssid),
121 MAC2STR(src), MAC2STR(dst));
124 if (ip->ihl * 4 < sizeof(*ip)) {
125 wpa_printf(MSG_DEBUG, "Unexpected IP header length %u in "
126 "IPv4 packet (bssid=" MACSTR " str=" MACSTR
127 " dst=" MACSTR ")", ip->ihl, MAC2STR(bssid),
128 MAC2STR(src), MAC2STR(dst));
131 if (ip->ihl * 4 > len) {
132 wpa_printf(MSG_DEBUG, "Truncated IP header (ihl=%u len=%u) in "
133 "IPv4 packet (bssid=" MACSTR " str=" MACSTR
134 " dst=" MACSTR ")", ip->ihl, (unsigned) len,
135 MAC2STR(bssid), MAC2STR(src), MAC2STR(dst));
139 /* TODO: check header checksum in ip->check */
141 frag_off = be_to_host16(ip->frag_off);
142 if (frag_off & 0x1fff) {
143 wpa_printf(MSG_EXCESSIVE, "IP fragment reassembly not yet "
148 tot_len = be_to_host16(ip->tot_len);
154 payload = data + 4 * ip->ihl;
155 plen = len - 4 * ip->ihl;
157 switch (ip->protocol) {
159 rx_data_icmp(wt, bssid, sta_addr, ip->daddr, ip->saddr,
160 payload, plen, peer_addr);