aboutsummaryrefslogtreecommitdiffstats
path: root/wlantest/monitor.c
blob: f28708689ed93d0738a38a0ee4873d32f6bab00a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 * Linux packet socket monitor
 * Copyright (c) 2010, Jouni Malinen <j@w1.fi>
 *
 * This software may be distributed under the terms of the BSD license.
 * See README for more details.
 */

#include "utils/includes.h"
#ifndef __APPLE__
#include <net/if.h>
#include <netpacket/packet.h>
#endif /* __APPLE__ */

#include "utils/common.h"
#include "utils/eloop.h"
#include "wlantest.h"


#ifdef __APPLE__

int monitor_init(struct wlantest *wt, const char *ifname)
{
	return -1;
}


int monitor_init_wired(struct wlantest *wt, const char *ifname)
{
	return -1;
}


void monitor_deinit(struct wlantest *wt)
{
}

#else /* __APPLE__ */

static void monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
{
	struct wlantest *wt = eloop_ctx;
	u8 buf[3000];
	int len;

	len = recv(sock, buf, sizeof(buf), 0);
	if (len < 0) {
		wpa_printf(MSG_INFO, "recv(PACKET): %s", strerror(errno));
		return;
	}

	clear_notes(wt);
	os_free(wt->decrypted);
	wt->decrypted = NULL;
	write_pcap_captured(wt, buf, len);
	wlantest_process(wt, buf, len);
	write_pcapng_captured(wt, buf, len);
}


static void monitor_read_wired(int sock, void *eloop_ctx, void *sock_ctx)
{
	struct wlantest *wt = eloop_ctx;
	u8 buf[3000];
	int len;

	len = recv(sock, buf, sizeof(buf), 0);
	if (len < 0) {
		wpa_printf(MSG_INFO, "recv(PACKET): %s", strerror(errno));
		return;
	}

	wlantest_process_wired(wt, buf, len);
}


int monitor_init(struct wlantest *wt, const char *ifname)
{
	struct sockaddr_ll ll;

	os_memset(&ll, 0, sizeof(ll));
	ll.sll_family = AF_PACKET;
	ll.sll_ifindex = if_nametoindex(ifname);
	if (ll.sll_ifindex == 0) {
		wpa_printf(MSG_ERROR, "Monitor interface '%s' does not exist",
			   ifname);
		return -1;
	}

	wt->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
	if (wt->monitor_sock < 0) {
		wpa_printf(MSG_ERROR, "socket(PF_PACKET,SOCK_RAW): %s",
			   strerror(errno));
		return -1;
	}

	if (bind(wt->monitor_sock, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
		wpa_printf(MSG_ERROR, "bind(PACKET): %s", strerror(errno));
		close(wt->monitor_sock);
		wt->monitor_sock = -1;
		return -1;
	}

	if (eloop_register_read_sock(wt->monitor_sock, monitor_read, wt, NULL))
	{
		wpa_printf(MSG_ERROR, "Could not register monitor read "
			   "socket");
		close(wt->monitor_sock);
		wt->monitor_sock = -1;
		return -1;
	}

	return 0;
}


int monitor_init_wired(struct wlantest *wt, const char *ifname)
{
	struct sockaddr_ll ll;

	os_memset(&ll, 0, sizeof(ll));
	ll.sll_family = AF_PACKET;
	ll.sll_ifindex = if_nametoindex(ifname);
	if (ll.sll_ifindex == 0) {
		wpa_printf(MSG_ERROR, "Monitor interface '%s' does not exist",
			   ifname);
		return -1;
	}

	wt->monitor_wired = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
	if (wt->monitor_wired < 0) {
		wpa_printf(MSG_ERROR, "socket(PF_PACKET,SOCK_RAW): %s",
			   strerror(errno));
		return -1;
	}

	if (bind(wt->monitor_wired, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
		wpa_printf(MSG_ERROR, "bind(PACKET): %s", strerror(errno));
		close(wt->monitor_wired);
		wt->monitor_wired = -1;
		return -1;
	}

	if (eloop_register_read_sock(wt->monitor_wired, monitor_read_wired,
				     wt, NULL)) {
		wpa_printf(MSG_ERROR, "Could not register monitor read "
			   "socket");
		close(wt->monitor_wired);
		wt->monitor_wired = -1;
		return -1;
	}

	return 0;
}


void monitor_deinit(struct wlantest *wt)
{
	if (wt->monitor_sock >= 0) {
		eloop_unregister_read_sock(wt->monitor_sock);
		close(wt->monitor_sock);
		wt->monitor_sock = -1;
	}

	if (wt->monitor_wired >= 0) {
		eloop_unregister_read_sock(wt->monitor_wired);
		close(wt->monitor_wired);
		wt->monitor_wired = -1;
	}
}

#endif /* __APPLE__ */