aboutsummaryrefslogtreecommitdiffstats
path: root/wlantest/wired.c
blob: 9267f97769ffe15431b223e09e08800b0ef9ad2f (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
 * Received frame processing for wired interface
 * 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"
#include <net/ethernet.h>
#include <netinet/ip.h>
#include <netinet/udp.h>

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


static struct wlantest_radius * radius_get(struct wlantest *wt, u32 srv,
					   u32 cli)
{
	struct wlantest_radius *r;

	dl_list_for_each(r, &wt->radius, struct wlantest_radius, list) {
		if (r->srv == srv && r->cli == cli)
			return r;
	}

	r = os_zalloc(sizeof(*r));
	if (r == NULL)
		return NULL;

	r->srv = srv;
	r->cli = cli;
	dl_list_add(&wt->radius, &r->list);

	return r;
}


static const char * radius_code_string(u8 code)
{
	switch (code) {
	case RADIUS_CODE_ACCESS_REQUEST:
		return "Access-Request";
	case RADIUS_CODE_ACCESS_ACCEPT:
		return "Access-Accept";
	case RADIUS_CODE_ACCESS_REJECT:
		return "Access-Reject";
	case RADIUS_CODE_ACCOUNTING_REQUEST:
		return "Accounting-Request";
	case RADIUS_CODE_ACCOUNTING_RESPONSE:
		return "Accounting-Response";
	case RADIUS_CODE_ACCESS_CHALLENGE:
		return "Access-Challenge";
	case RADIUS_CODE_STATUS_SERVER:
		return "Status-Server";
	case RADIUS_CODE_STATUS_CLIENT:
		return "Status-Client";
	case RADIUS_CODE_RESERVED:
		return "Reserved";
	default:
		return "?Unknown?";
	}
}


static void process_radius_access_request(struct wlantest *wt, u32 dst,
					  u32 src, const u8 *data, size_t len)
{
	struct radius_msg *msg;
	struct wlantest_radius *r;

	msg = radius_msg_parse(data, len);
	if (msg == NULL) {
		wpa_printf(MSG_DEBUG, "Failed to parse RADIUS Access-Request");
		return;
	}

	r = radius_get(wt, dst, src);
	if (r) {
		radius_msg_free(r->last_req);
		r->last_req = msg;
		return;
	}
	radius_msg_free(msg);
}


static void wlantest_add_pmk(struct wlantest *wt, const u8 *pmk, size_t pmk_len)
{
	struct wlantest_pmk *p;

	p = os_zalloc(sizeof(*p));
	if (p == NULL)
		return;
	os_memcpy(p->pmk, pmk, pmk_len);
	p->pmk_len = pmk_len;
	dl_list_add(&wt->pmk, &p->list);
	wpa_hexdump(MSG_INFO, "Add PMK", pmk, pmk_len);
}


static void process_radius_access_accept(struct wlantest *wt, u32 dst, u32 src,
					 const u8 *data, size_t len)
{
	struct radius_msg *msg;
	struct wlantest_radius *r;
	struct radius_ms_mppe_keys *keys;
	struct wlantest_radius_secret *s;

	r = radius_get(wt, src, dst);
	if (r == NULL || r->last_req == NULL) {
		wpa_printf(MSG_DEBUG, "No RADIUS Access-Challenge found for "
			   "decrypting Access-Accept keys");
		return;
	}

	msg = radius_msg_parse(data, len);
	if (msg == NULL) {
		wpa_printf(MSG_DEBUG, "Failed to parse RADIUS Access-Accept");
		return;
	}

	dl_list_for_each(s, &wt->secret, struct wlantest_radius_secret, list) {
		int found = 0;
		keys = radius_msg_get_ms_keys(msg, r->last_req,
					      (u8 *) s->secret,
					      os_strlen(s->secret));
		if (keys && keys->send && keys->recv) {
			u8 pmk[PMK_LEN_MAX];
			size_t pmk_len, len2;

			wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Send-Key",
					keys->send, keys->send_len);
			wpa_hexdump_key(MSG_DEBUG, "MS-MPPE-Recv-Key",
					keys->recv, keys->recv_len);
			pmk_len = keys->recv_len;
			if (pmk_len > PMK_LEN_MAX)
				pmk_len = PMK_LEN_MAX;
			os_memcpy(pmk, keys->recv, pmk_len);
			if (pmk_len < PMK_LEN_MAX) {
				len2 = keys->send_len;
				if (pmk_len + len2 > PMK_LEN_MAX)
					len2 = PMK_LEN_MAX - pmk_len;
				os_memcpy(pmk + pmk_len, keys->send, len2);
				pmk_len += len2;
			}
			wlantest_add_pmk(wt, pmk, pmk_len);
			found = 1;
		}

		if (keys) {
			os_free(keys->send);
			os_free(keys->recv);
			os_free(keys);
		}

		if (found)
			break;
	}

	radius_msg_free(msg);
}


static void process_radius(struct wlantest *wt, u32 dst, u16 dport, u32 src,
			   u16 sport, const u8 *data, size_t len)
{
	struct in_addr addr;
	char buf[20];
	const struct radius_hdr *hdr;
	u16 rlen;

	if (len < sizeof(*hdr))
		return;
	hdr = (const struct radius_hdr *) data;
	rlen = be_to_host16(hdr->length);
	if (len < rlen)
		return;
	if (len > rlen)
		len = rlen;

	addr.s_addr = dst;
	snprintf(buf, sizeof(buf), "%s", inet_ntoa(addr));

	addr.s_addr = src;
	wpa_printf(MSG_DEBUG, "RADIUS %s:%u -> %s:%u id=%u %s",
		   inet_ntoa(addr), sport, buf, dport, hdr->identifier,
		   radius_code_string(hdr->code));

	switch (hdr->code) {
	case RADIUS_CODE_ACCESS_REQUEST:
		process_radius_access_request(wt, dst, src, data, len);
		break;
	case RADIUS_CODE_ACCESS_ACCEPT:
		process_radius_access_accept(wt, dst, src, data, len);
		break;
	}
}


static void process_udp(struct wlantest *wt, u32 dst, u32 src,
			const u8 *data, size_t len)
{
	const struct udphdr *udp;
	u16 sport, dport, ulen;
	const u8 *payload;
	size_t plen;

	if (len < sizeof(*udp))
		return;
	udp = (const struct udphdr *) data;
	/* TODO: check UDP checksum */
	sport = be_to_host16(udp->source);
	dport = be_to_host16(udp->dest);
	ulen = be_to_host16(udp->len);

	if (ulen > len)
		return;
	if (len < ulen)
		len = ulen;

	payload = (const u8 *) (udp + 1);
	plen = len - sizeof(*udp);

	if (sport == 1812 || dport == 1812)
		process_radius(wt, dst, dport, src, sport, payload, plen);
}


static void process_ipv4(struct wlantest *wt, const u8 *data, size_t len)
{
	const struct iphdr *ip;
	const u8 *payload;
	size_t plen;
	u16 frag_off, tot_len;

	if (len < sizeof(*ip))
		return;

	ip = (const struct iphdr *) data;
	if (ip->version != 4)
		return;
	if (ip->ihl < 5)
		return;

	/* TODO: check header checksum in ip->check */

	frag_off = be_to_host16(ip->frag_off);
	if (frag_off & 0x1fff) {
		wpa_printf(MSG_EXCESSIVE, "IP fragment reassembly not yet "
			   "supported");
		return;
	}

	tot_len = be_to_host16(ip->tot_len);
	if (tot_len > len)
		return;
	if (tot_len < len)
		len = tot_len;

	payload = data + 4 * ip->ihl;
	plen = len - 4 * ip->ihl;
	if (payload + plen > data + len)
		return;

	switch (ip->protocol) {
	case IPPROTO_UDP:
		process_udp(wt, ip->daddr, ip->saddr, payload, plen);
		break;
	}
}


void wlantest_process_wired(struct wlantest *wt, const u8 *data, size_t len)
{
	const struct ether_header *eth;
	u16 ethertype;

	wpa_hexdump(MSG_EXCESSIVE, "Process wired frame", data, len);

	if (len < sizeof(*eth))
		return;

	eth = (const struct ether_header *) data;
	ethertype = be_to_host16(eth->ether_type);

	switch (ethertype) {
	case ETHERTYPE_IP:
		process_ipv4(wt, data + sizeof(*eth), len - sizeof(*eth));
		break;
	}
}