]> git.ipfire.org Git - thirdparty/hostap.git/blame - wlantest/monitor.c
wlantest: Process TX status frames as RX frames too
[thirdparty/hostap.git] / wlantest / monitor.c
CommitLineData
a149fcc7
JM
1/*
2 * Linux packet socket monitor
3 * Copyright (c) 2010, Jouni Malinen <j@w1.fi>
4 *
0f3d578e
JM
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
a149fcc7
JM
7 */
8
9#include "utils/includes.h"
10#include <net/if.h>
11#include <netpacket/packet.h>
12
13#include "utils/common.h"
14#include "utils/eloop.h"
15#include "wlantest.h"
16
17
18static void monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
19{
20 struct wlantest *wt = eloop_ctx;
21 u8 buf[3000];
22 int len;
23
24 len = recv(sock, buf, sizeof(buf), 0);
25 if (len < 0) {
26 wpa_printf(MSG_INFO, "recv(PACKET): %s", strerror(errno));
27 return;
28 }
29
64f45d07 30 write_pcap_captured(wt, buf, len);
a149fcc7
JM
31 wlantest_process(wt, buf, len);
32}
33
34
3215df77
JM
35static void monitor_read_wired(int sock, void *eloop_ctx, void *sock_ctx)
36{
37 struct wlantest *wt = eloop_ctx;
38 u8 buf[3000];
39 int len;
40
41 len = recv(sock, buf, sizeof(buf), 0);
42 if (len < 0) {
43 wpa_printf(MSG_INFO, "recv(PACKET): %s", strerror(errno));
44 return;
45 }
46
47 wlantest_process_wired(wt, buf, len);
48}
49
50
a149fcc7
JM
51int monitor_init(struct wlantest *wt, const char *ifname)
52{
53 struct sockaddr_ll ll;
54
55 os_memset(&ll, 0, sizeof(ll));
56 ll.sll_family = AF_PACKET;
57 ll.sll_ifindex = if_nametoindex(ifname);
58 if (ll.sll_ifindex == 0) {
59 wpa_printf(MSG_ERROR, "Monitor interface '%s' does not exist",
60 ifname);
61 return -1;
62 }
63
64 wt->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
65 if (wt->monitor_sock < 0) {
66 wpa_printf(MSG_ERROR, "socket(PF_PACKET,SOCK_RAW): %s",
67 strerror(errno));
68 return -1;
69 }
70
71 if (bind(wt->monitor_sock, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
72 wpa_printf(MSG_ERROR, "bind(PACKET): %s", strerror(errno));
73 close(wt->monitor_sock);
74 wt->monitor_sock = -1;
75 return -1;
76 }
77
78 if (eloop_register_read_sock(wt->monitor_sock, monitor_read, wt, NULL))
79 {
80 wpa_printf(MSG_ERROR, "Could not register monitor read "
81 "socket");
82 close(wt->monitor_sock);
83 wt->monitor_sock = -1;
84 return -1;
85 }
86
87 return 0;
88}
89
90
3215df77
JM
91int monitor_init_wired(struct wlantest *wt, const char *ifname)
92{
93 struct sockaddr_ll ll;
94
95 os_memset(&ll, 0, sizeof(ll));
96 ll.sll_family = AF_PACKET;
97 ll.sll_ifindex = if_nametoindex(ifname);
98 if (ll.sll_ifindex == 0) {
99 wpa_printf(MSG_ERROR, "Monitor interface '%s' does not exist",
100 ifname);
101 return -1;
102 }
103
104 wt->monitor_wired = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
105 if (wt->monitor_wired < 0) {
106 wpa_printf(MSG_ERROR, "socket(PF_PACKET,SOCK_RAW): %s",
107 strerror(errno));
108 return -1;
109 }
110
111 if (bind(wt->monitor_wired, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
112 wpa_printf(MSG_ERROR, "bind(PACKET): %s", strerror(errno));
113 close(wt->monitor_wired);
114 wt->monitor_wired = -1;
115 return -1;
116 }
117
118 if (eloop_register_read_sock(wt->monitor_wired, monitor_read_wired,
119 wt, NULL)) {
120 wpa_printf(MSG_ERROR, "Could not register monitor read "
121 "socket");
122 close(wt->monitor_wired);
123 wt->monitor_wired = -1;
124 return -1;
125 }
126
127 return 0;
128}
129
130
a149fcc7
JM
131void monitor_deinit(struct wlantest *wt)
132{
133 if (wt->monitor_sock >= 0) {
134 eloop_unregister_read_sock(wt->monitor_sock);
135 close(wt->monitor_sock);
136 wt->monitor_sock = -1;
137 }
3215df77
JM
138
139 if (wt->monitor_wired >= 0) {
140 eloop_unregister_read_sock(wt->monitor_wired);
141 close(wt->monitor_wired);
142 wt->monitor_wired = -1;
143 }
a149fcc7 144}