2010-11-20 21:34:42 +01:00
|
|
|
/*
|
|
|
|
* Received Data frame processing for IPv4 packets
|
|
|
|
* Copyright (c) 2010, Jouni Malinen <j@w1.fi>
|
|
|
|
*
|
2012-02-11 15:46:35 +01:00
|
|
|
* This software may be distributed under the terms of the BSD license.
|
|
|
|
* See README for more details.
|
2010-11-20 21:34:42 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "utils/includes.h"
|
|
|
|
#include <netinet/ip.h>
|
|
|
|
#include <netinet/ip_icmp.h>
|
|
|
|
|
|
|
|
#include "utils/common.h"
|
|
|
|
#include "wlantest.h"
|
|
|
|
|
|
|
|
|
2020-04-05 16:56:59 +02:00
|
|
|
#ifndef __APPLE__
|
|
|
|
|
2013-05-26 19:58:50 +02:00
|
|
|
static void ping_update(struct wlantest *wt, struct wlantest_sta *sta, int req,
|
|
|
|
u32 src, u32 dst, u16 id, u16 seq)
|
2011-01-24 13:50:51 +01:00
|
|
|
{
|
|
|
|
if (req) {
|
|
|
|
sta->icmp_echo_req_src = src;
|
|
|
|
sta->icmp_echo_req_dst = dst;
|
|
|
|
sta->icmp_echo_req_id = id;
|
|
|
|
sta->icmp_echo_req_seq = seq;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sta->icmp_echo_req_src == dst &&
|
|
|
|
sta->icmp_echo_req_dst == src &&
|
|
|
|
sta->icmp_echo_req_id == id &&
|
|
|
|
sta->icmp_echo_req_seq == seq) {
|
|
|
|
sta->counters[WLANTEST_STA_COUNTER_PING_OK]++;
|
|
|
|
if (sta->counters[WLANTEST_STA_COUNTER_ASSOCREQ_TX] == 0 &&
|
|
|
|
sta->counters[WLANTEST_STA_COUNTER_REASSOCREQ_TX] == 0)
|
|
|
|
sta->counters[
|
|
|
|
WLANTEST_STA_COUNTER_PING_OK_FIRST_ASSOC]++;
|
2013-05-26 19:58:50 +02:00
|
|
|
add_note(wt, MSG_DEBUG, "ICMP echo (ping) match for STA "
|
|
|
|
MACSTR, MAC2STR(sta->addr));
|
2011-01-24 13:50:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-20 21:34:42 +01:00
|
|
|
static void rx_data_icmp(struct wlantest *wt, const u8 *bssid,
|
|
|
|
const u8 *sta_addr, u32 dst, u32 src,
|
2011-01-24 13:50:51 +01:00
|
|
|
const u8 *data, size_t len, const u8 *peer_addr)
|
2010-11-20 21:34:42 +01:00
|
|
|
{
|
|
|
|
struct in_addr addr;
|
|
|
|
char buf[20];
|
|
|
|
const struct icmphdr *hdr;
|
|
|
|
u16 id, seq;
|
|
|
|
struct wlantest_bss *bss;
|
|
|
|
struct wlantest_sta *sta;
|
|
|
|
|
|
|
|
hdr = (const struct icmphdr *) data;
|
|
|
|
if (len < 4)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* TODO: check hdr->checksum */
|
|
|
|
|
|
|
|
if (hdr->type != ICMP_ECHOREPLY && hdr->type != ICMP_ECHO)
|
|
|
|
return;
|
|
|
|
if (len < 8)
|
|
|
|
return;
|
|
|
|
|
|
|
|
id = ntohs(hdr->un.echo.id);
|
|
|
|
seq = ntohs(hdr->un.echo.sequence);
|
|
|
|
|
|
|
|
addr.s_addr = dst;
|
|
|
|
snprintf(buf, sizeof(buf), "%s", inet_ntoa(addr));
|
|
|
|
addr.s_addr = src;
|
2013-05-26 19:58:50 +02:00
|
|
|
add_note(wt, MSG_DEBUG, "ICMP echo %s %s -> %s id=%04x seq=%u len=%u%s",
|
|
|
|
hdr->type == ICMP_ECHO ? "request" : "response",
|
|
|
|
inet_ntoa(addr), buf, id, seq, (unsigned) len - 8,
|
|
|
|
peer_addr ? " [DL]" : "");
|
2010-11-20 21:34:42 +01:00
|
|
|
|
|
|
|
bss = bss_find(wt, bssid);
|
|
|
|
if (bss == NULL) {
|
2013-05-26 19:58:50 +02:00
|
|
|
add_note(wt, MSG_INFO, "No BSS " MACSTR
|
|
|
|
" known for ICMP packet", MAC2STR(bssid));
|
2010-11-20 21:34:42 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sta_addr == NULL)
|
|
|
|
return; /* FromDS broadcast ping */
|
|
|
|
|
|
|
|
sta = sta_find(bss, sta_addr);
|
|
|
|
if (sta == NULL) {
|
2013-05-26 19:58:50 +02:00
|
|
|
add_note(wt, MSG_INFO, "No STA " MACSTR
|
|
|
|
" known for ICMP packet", MAC2STR(sta_addr));
|
2010-11-20 21:34:42 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-05-26 19:58:50 +02:00
|
|
|
ping_update(wt, sta, hdr->type == ICMP_ECHO, src, dst, id, seq);
|
2011-01-24 13:50:51 +01:00
|
|
|
if (peer_addr && (sta = sta_find(bss, peer_addr)))
|
2013-05-26 19:58:50 +02:00
|
|
|
ping_update(wt, sta, hdr->type == ICMP_ECHO, src, dst, id, seq);
|
2010-11-20 21:34:42 +01:00
|
|
|
}
|
|
|
|
|
2020-04-05 16:56:59 +02:00
|
|
|
#endif /* __APPLE__ */
|
|
|
|
|
2010-11-20 21:34:42 +01:00
|
|
|
|
2013-05-26 21:19:52 +02:00
|
|
|
static int hwsim_test_packet(const u8 *data, size_t len)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (len != 1500 - 14)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
if (data[i] != (i & 0xff))
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-20 21:34:42 +01:00
|
|
|
void rx_data_ip(struct wlantest *wt, const u8 *bssid, const u8 *sta_addr,
|
2011-01-24 13:50:51 +01:00
|
|
|
const u8 *dst, const u8 *src, const u8 *data, size_t len,
|
|
|
|
const u8 *peer_addr)
|
2010-11-20 21:34:42 +01:00
|
|
|
{
|
2020-01-02 18:12:33 +01:00
|
|
|
const struct ip *ip;
|
2010-11-20 21:34:42 +01:00
|
|
|
const u8 *payload;
|
|
|
|
size_t plen;
|
2020-01-02 18:12:33 +01:00
|
|
|
uint16_t frag_off, ip_len;
|
2010-11-20 21:34:42 +01:00
|
|
|
|
2020-01-02 18:12:33 +01:00
|
|
|
ip = (const struct ip *) data;
|
2010-11-20 21:34:42 +01:00
|
|
|
if (len < sizeof(*ip))
|
|
|
|
return;
|
2020-01-02 18:12:33 +01:00
|
|
|
if (ip->ip_v != 4) {
|
2013-05-26 21:19:52 +02:00
|
|
|
if (hwsim_test_packet(data, len)) {
|
|
|
|
add_note(wt, MSG_INFO, "hwsim_test package");
|
|
|
|
return;
|
|
|
|
}
|
2013-05-26 19:58:50 +02:00
|
|
|
add_note(wt, MSG_DEBUG, "Unexpected IP protocol version %u in "
|
|
|
|
"IPv4 packet (bssid=" MACSTR " str=" MACSTR
|
2020-01-02 18:12:33 +01:00
|
|
|
" dst=" MACSTR ")", ip->ip_v, MAC2STR(bssid),
|
2013-05-26 19:58:50 +02:00
|
|
|
MAC2STR(src), MAC2STR(dst));
|
2010-11-20 21:34:42 +01:00
|
|
|
return;
|
|
|
|
}
|
2020-01-02 18:12:33 +01:00
|
|
|
if (ip->ip_hl * 4 < sizeof(*ip)) {
|
2013-05-26 19:58:50 +02:00
|
|
|
add_note(wt, MSG_DEBUG, "Unexpected IP header length %u in "
|
|
|
|
"IPv4 packet (bssid=" MACSTR " str=" MACSTR
|
2020-01-02 18:12:33 +01:00
|
|
|
" dst=" MACSTR ")", ip->ip_hl, MAC2STR(bssid),
|
2013-05-26 19:58:50 +02:00
|
|
|
MAC2STR(src), MAC2STR(dst));
|
2010-11-20 21:34:42 +01:00
|
|
|
return;
|
|
|
|
}
|
2020-01-02 18:12:33 +01:00
|
|
|
if (ip->ip_hl * 4 > len) {
|
2013-05-26 19:58:50 +02:00
|
|
|
add_note(wt, MSG_DEBUG, "Truncated IP header (ihl=%u len=%u) "
|
|
|
|
"in IPv4 packet (bssid=" MACSTR " str=" MACSTR
|
2020-01-02 18:12:33 +01:00
|
|
|
" dst=" MACSTR ")", ip->ip_hl, (unsigned) len,
|
2013-05-26 19:58:50 +02:00
|
|
|
MAC2STR(bssid), MAC2STR(src), MAC2STR(dst));
|
2010-11-20 21:34:42 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-01-02 18:12:33 +01:00
|
|
|
/* TODO: check header checksum in ip->ip_sum */
|
2010-11-20 21:34:42 +01:00
|
|
|
|
2020-01-02 18:12:33 +01:00
|
|
|
frag_off = be_to_host16(ip->ip_off);
|
2010-11-20 21:34:42 +01:00
|
|
|
if (frag_off & 0x1fff) {
|
|
|
|
wpa_printf(MSG_EXCESSIVE, "IP fragment reassembly not yet "
|
|
|
|
"supported");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-01-02 18:12:33 +01:00
|
|
|
ip_len = be_to_host16(ip->ip_len);
|
|
|
|
if (ip_len > len)
|
2010-11-20 21:34:42 +01:00
|
|
|
return;
|
2020-01-02 18:12:33 +01:00
|
|
|
if (ip_len < len)
|
|
|
|
len = ip_len;
|
2010-11-20 21:34:42 +01:00
|
|
|
|
2020-01-02 18:12:33 +01:00
|
|
|
payload = data + 4 * ip->ip_hl;
|
|
|
|
plen = len - 4 * ip->ip_hl;
|
2010-11-20 21:34:42 +01:00
|
|
|
|
2020-01-02 18:12:33 +01:00
|
|
|
switch (ip->ip_p) {
|
2020-04-05 16:56:59 +02:00
|
|
|
#ifndef __APPLE__
|
2010-11-20 21:34:42 +01:00
|
|
|
case IPPROTO_ICMP:
|
2020-01-02 18:12:33 +01:00
|
|
|
rx_data_icmp(wt, bssid, sta_addr, ip->ip_dst.s_addr,
|
|
|
|
ip->ip_src.s_addr, payload, plen, peer_addr);
|
2010-11-20 21:34:42 +01:00
|
|
|
break;
|
2020-04-05 16:56:59 +02:00
|
|
|
#endif /* __APPLE__ */
|
2010-11-20 21:34:42 +01:00
|
|
|
}
|
|
|
|
}
|