summaryrefslogtreecommitdiff
path: root/package/hostapd/patches/006-use-nl80211-for-sta.patch
blob: 133a750194ad807e3089c0a44d66b24ae39cac3c (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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
---
 hostapd/driver_devicescape.c |  332 ++++++++++++++++++++++++++++++++-----------
 1 file changed, 251 insertions(+), 81 deletions(-)

--- hostap.orig/hostapd/driver_devicescape.c	2007-11-14 17:31:15.000000000 +0100
+++ hostap/hostapd/driver_devicescape.c	2007-11-14 17:31:16.000000000 +0100
@@ -75,8 +75,14 @@ struct i802_driver_data {
 
 #define HAPD_DECL	struct hostapd_data *hapd = iface->bss[0]
 
-static int i802_sta_set_flags(void *priv, const u8 *addr,
-			      int total_flags, int flags_or, int flags_and);
+/* helper for netlink get routines */
+static int ack_wait_handler(struct nl_msg *msg, void *arg)
+{
+	int *finished = arg;
+
+	*finished = 1;
+	return NL_STOP;
+}
 
 
 static int hostapd_set_iface_flags(struct i802_driver_data *drv, int dev_up)
@@ -255,14 +261,6 @@ static int get_key_handler(struct nl_msg
 	return NL_SKIP;
 }
 
-static int ack_wait_handler(struct nl_msg *msg, void *arg)
-{
-	int *finished = arg;
-
-	*finished = 1;
-	return NL_STOP;
-}
-
 static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
 			   int idx, u8 *seq)
 {
@@ -629,43 +627,126 @@ static int i802_get_retry(void *priv, in
 static int i802_flush(void *priv)
 {
 	struct i802_driver_data *drv = priv;
-	struct prism2_hostapd_param param;
+	struct nl_msg *msg;
+	int ret = -1;
 
-	memset(&param, 0, sizeof(param));
-	param.cmd = PRISM2_HOSTAPD_FLUSH;
-	return hostapd_ioctl(drv, &param, sizeof(param));
+	msg = nlmsg_alloc();
+	if (!msg)
+		goto out;
+
+	genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
+		    0, NL80211_CMD_NEW_STATION, 0);
+
+	/*
+	 * XXX: FIX! this needs to flush all VLANs too
+	 */
+	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
+		    if_nametoindex(drv->iface));
+
+	ret = 0;
+
+	if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
+	    nl_wait_for_ack(drv->nl_handle) < 0) {
+		ret = -1;
+	}
+
+ nla_put_failure:
+	nlmsg_free(msg);
+
+ out:
+	return ret;
 }
 
 
+static int get_sta_handler(struct nl_msg *msg, void *arg)
+{
+	struct nlattr *tb[NL80211_ATTR_MAX + 1];
+	struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
+	struct hostap_sta_driver_data *data = arg;
+	struct nlattr *stats[NL80211_STA_STAT_MAX + 1];
+	static struct nla_policy stats_policy[NL80211_STA_STAT_MAX + 1] = {
+		[NL80211_STA_STAT_INACTIVE_TIME] = { .type = NLA_U32 },
+		[NL80211_STA_STAT_RX_BYTES] = { .type = NLA_U32 },
+		[NL80211_STA_STAT_TX_BYTES] = { .type = NLA_U32 },
+	};
+
+	nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
+		  genlmsg_attrlen(gnlh, 0), NULL);
+
+	/*
+	 * TODO: validate the interface and mac address!
+	 * Otherwise, there's a race condition as soon as
+	 * the kernel starts sending station notifications.
+	 */
+
+	if (!tb[NL80211_ATTR_STA_STATS]) {
+		printf("sta stats missing!\n");
+		return NL_SKIP;
+	}
+	if (nla_parse_nested(stats, NL80211_STA_STAT_MAX,
+			     tb[NL80211_ATTR_STA_STATS],
+			     stats_policy)) {
+		printf("failed to parse nested attributes!\n");
+		return NL_SKIP;
+	}
+
+	if (stats[NL80211_STA_STAT_INACTIVE_TIME])
+		data->inactive_msec =
+			nla_get_u32(stats[NL80211_STA_STAT_INACTIVE_TIME]);
+	if (stats[NL80211_STA_STAT_RX_BYTES])
+		data->rx_bytes = nla_get_u32(stats[NL80211_STA_STAT_RX_BYTES]);
+	if (stats[NL80211_STA_STAT_TX_BYTES])
+		data->rx_bytes = nla_get_u32(stats[NL80211_STA_STAT_TX_BYTES]);
+
+	return NL_SKIP;
+}
+
 static int i802_read_sta_data(void *priv, struct hostap_sta_driver_data *data,
 			      const u8 *addr)
 {
 	struct i802_driver_data *drv = priv;
-	struct prism2_hostapd_param param;
+	struct nl_msg *msg;
+	struct nl_cb *cb = NULL;
+	int ret = -1;
+	int err = 0;
+	int finished = 0;
+
+	msg = nlmsg_alloc();
+	if (!msg)
+		goto out;
 
-	memset(data, 0, sizeof(*data));
+	genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
+		    0, NL80211_CMD_GET_STATION, 0);
 
-	memset(&param, 0, sizeof(param));
-	param.cmd = PRISM2_HOSTAPD_GET_INFO_STA;
-	memcpy(param.sta_addr, addr, ETH_ALEN);
-	if (hostapd_ioctl(drv, &param, sizeof(param))) {
-		printf("  Could not get station info from kernel driver.\n");
-		return -1;
-	}
+	NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
+	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
+
+	cb = nl_cb_alloc(NL_CB_CUSTOM);
+	if (!cb)
+		goto out;
+
+	if (nl_send_auto_complete(drv->nl_handle, msg) < 0)
+		goto out;
+
+	nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, get_sta_handler, data);
+	nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_wait_handler, &finished);
+
+	err = nl_recvmsgs(drv->nl_handle, cb);
+
+	if (!finished)
+		err = nl_wait_for_ack(drv->nl_handle);
+
+	if (err < 0)
+		goto out;
+
+	ret = 0;
+
+ out:
+	nl_cb_put(cb);
+ nla_put_failure:
+	nlmsg_free(msg);
+	return ret;
 
-	data->inactive_msec = param.u.get_info_sta.inactive_msec;
-	data->rx_packets = param.u.get_info_sta.rx_packets;
-	data->tx_packets = param.u.get_info_sta.tx_packets;
-	data->rx_bytes = param.u.get_info_sta.rx_bytes;
-	data->tx_bytes = param.u.get_info_sta.tx_bytes;
-	data->current_tx_rate = param.u.get_info_sta.current_tx_rate;
-	data->flags = param.u.get_info_sta.flags;
-	data->num_ps_buf_frames = param.u.get_info_sta.num_ps_buf_frames;
-	data->tx_retry_failed = param.u.get_info_sta.tx_retry_failed;
-	data->tx_retry_count = param.u.get_info_sta.tx_retry_count;
-	data->last_rssi = param.u.get_info_sta.last_rssi;
-	data->last_ack_rssi = param.u.get_info_sta.last_ack_rssi;
-	return 0;
 }
 
 
@@ -744,35 +825,70 @@ static int i802_sta_add(const char *ifna
 			size_t supp_rates_len, int flags)
 {
 	struct i802_driver_data *drv = priv;
-	struct prism2_hostapd_param param;
-	size_t len;
+	struct nl_msg *msg;
+	int ret = -1;
 
-	memset(&param, 0, sizeof(param));
-	param.cmd = PRISM2_HOSTAPD_ADD_STA;
-	memcpy(param.sta_addr, addr, ETH_ALEN);
-	param.u.add_sta.aid = aid;
-	param.u.add_sta.capability = capability;
-	len = supp_rates_len;
-	if (len > sizeof(param.u.add_sta.supp_rates))
-		len = sizeof(param.u.add_sta.supp_rates);
-	memcpy(param.u.add_sta.supp_rates, supp_rates, len);
-	return hostapd_ioctl_iface(ifname, drv, &param, sizeof(param));
+	msg = nlmsg_alloc();
+	if (!msg)
+		goto out;
+
+	genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
+		    0, NL80211_CMD_NEW_STATION, 0);
+
+	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
+		    if_nametoindex(drv->iface));
+	NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
+	NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, aid);
+	NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, supp_rates_len,
+		supp_rates);
+	NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL, 0);
+
+	ret = nl_send_auto_complete(drv->nl_handle, msg);
+	if (ret < 0)
+		goto nla_put_failure;
+
+	ret = nl_wait_for_ack(drv->nl_handle);
+	/* ignore EEXIST, this happens if a STA associates while associated */
+	if (ret == -EEXIST || ret >= 0)
+		ret = 0;
+
+ nla_put_failure:
+	nlmsg_free(msg);
+
+ out:
+	return ret;
 }
 
 
 static int i802_sta_remove(void *priv, const u8 *addr)
 {
 	struct i802_driver_data *drv = priv;
-	struct prism2_hostapd_param param;
+	struct nl_msg *msg;
+	int ret = -1;
 
-	i802_sta_set_flags(drv, addr, 0, 0, ~WLAN_STA_AUTHORIZED);
+	msg = nlmsg_alloc();
+	if (!msg)
+		goto out;
 
-	memset(&param, 0, sizeof(param));
-	param.cmd = PRISM2_HOSTAPD_REMOVE_STA;
-	memcpy(param.sta_addr, addr, ETH_ALEN);
-	if (hostapd_ioctl(drv, &param, sizeof(param)))
-		return -1;
-	return 0;
+	genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
+		    0, NL80211_CMD_DEL_STATION, 0);
+
+	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
+		    if_nametoindex(drv->iface));
+	NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
+
+	ret = 0;
+
+	if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
+	    nl_wait_for_ack(drv->nl_handle) < 0) {
+		ret = -1;
+	}
+
+ nla_put_failure:
+	nlmsg_free(msg);
+
+ out:
+	return ret;
 }
 
 
@@ -780,14 +896,51 @@ static int i802_sta_set_flags(void *priv
 			      int total_flags, int flags_or, int flags_and)
 {
 	struct i802_driver_data *drv = priv;
-	struct prism2_hostapd_param param;
+	struct nl_msg *msg, *flags = NULL;
+	int ret = -1;
 
-	memset(&param, 0, sizeof(param));
-	param.cmd = PRISM2_HOSTAPD_SET_FLAGS_STA;
-	memcpy(param.sta_addr, addr, ETH_ALEN);
-	param.u.set_flags_sta.flags_or = flags_or;
-	param.u.set_flags_sta.flags_and = flags_and;
-	return hostapd_ioctl(drv, &param, sizeof(param));
+	msg = nlmsg_alloc();
+	if (!msg)
+		goto out;
+
+	flags = nlmsg_alloc();
+	if (!flags)
+		goto free_msg;
+
+	genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
+		    0, NL80211_CMD_SET_STATION, 0);
+
+	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
+		    if_nametoindex(drv->iface));
+	NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
+
+	if (total_flags & WLAN_STA_AUTHORIZED)
+		NLA_PUT_FLAG(flags, NL80211_STA_FLAG_AUTHORIZED);
+
+	if (total_flags & WLAN_STA_WME)
+		NLA_PUT_FLAG(flags, NL80211_STA_FLAG_WME);
+
+	if (total_flags & WLAN_STA_SHORT_PREAMBLE)
+		NLA_PUT_FLAG(flags, NL80211_STA_FLAG_SHORT_PREAMBLE);
+
+	if (nla_put_nested(msg, NL80211_ATTR_STA_FLAGS, flags))
+		goto nla_put_failure;
+
+	ret = 0;
+
+	if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
+	    nl_wait_for_ack(drv->nl_handle) < 0) {
+		ret = -1;
+	}
+
+ nla_put_failure:
+	nlmsg_free(flags);
+
+ free_msg:
+	nlmsg_free(msg);
+
+ out:
+	return ret;
 }
 
 
@@ -1257,18 +1410,38 @@ static struct hostapd_hw_modes * i802_ge
 }
 
 
-static int i802_set_sta_vlan(void *priv, const u8 *addr, const char *ifname,
-			     int vlan_id)
+static int i802_set_sta_vlan(void *priv, const u8 *addr,
+			     const char *ifname, int vlan_id)
 {
 	struct i802_driver_data *drv = priv;
-	struct prism2_hostapd_param param;
+	struct nl_msg *msg;
+	int ret = -1;
 
-	memset(&param, 0, sizeof(param));
-	param.cmd = PRISM2_HOSTAPD_SET_STA_VLAN;
-	memcpy(param.sta_addr, addr, ETH_ALEN);
-	os_strlcpy(param.u.set_sta_vlan.vlan_name, ifname, IFNAMSIZ);
-	param.u.set_sta_vlan.vlan_id = vlan_id;
-	return hostapd_ioctl(drv, &param, sizeof(param));
+	msg = nlmsg_alloc();
+	if (!msg)
+		goto out;
+
+	genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
+		    0, NL80211_CMD_SET_STATION, 0);
+
+	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
+		    if_nametoindex(drv->iface));
+	NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
+	NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
+		    if_nametoindex(ifname));
+
+	ret = 0;
+
+	if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
+	    nl_wait_for_ack(drv->nl_handle) < 0) {
+		ret = -1;
+	}
+
+ nla_put_failure:
+	nlmsg_free(msg);
+
+ out:
+	return ret;
 }
 
 
@@ -1752,17 +1925,14 @@ static int i802_init_sockets(struct i802
 
 static int i802_get_inact_sec(void *priv, const u8 *addr)
 {
-	struct i802_driver_data *drv = priv;
-	struct prism2_hostapd_param param;
+	struct hostap_sta_driver_data data;
+	int ret;
 
-	memset(&param, 0, sizeof(param));
-	param.cmd = PRISM2_HOSTAPD_GET_INFO_STA;
-	memcpy(param.sta_addr, addr, ETH_ALEN);
-	if (hostapd_ioctl(drv, &param, sizeof(param))) {
+	data.inactive_msec = -1;
+	ret = i802_read_sta_data(priv, &data, addr);
+	if (ret || data.inactive_msec == -1)
 		return -1;
-	}
-
-	return param.u.get_info_sta.inactive_msec / 1000;
+	return data.inactive_msec / 1000;
 }