xref: /freebsd/contrib/wpa/src/ap/ieee802_11_shared.c (revision 0b3105a37d7adcadcb720112fed4dc4e8040be99)
1 /*
2  * hostapd / IEEE 802.11 Management
3  * Copyright (c) 2002-2012, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #include "utils/includes.h"
10 
11 #include "utils/common.h"
12 #include "common/ieee802_11_defs.h"
13 #include "hostapd.h"
14 #include "sta_info.h"
15 #include "ap_config.h"
16 #include "ap_drv_ops.h"
17 #include "ieee802_11.h"
18 
19 
20 #ifdef CONFIG_IEEE80211W
21 
22 u8 * hostapd_eid_assoc_comeback_time(struct hostapd_data *hapd,
23 				     struct sta_info *sta, u8 *eid)
24 {
25 	u8 *pos = eid;
26 	u32 timeout, tu;
27 	struct os_reltime now, passed;
28 
29 	*pos++ = WLAN_EID_TIMEOUT_INTERVAL;
30 	*pos++ = 5;
31 	*pos++ = WLAN_TIMEOUT_ASSOC_COMEBACK;
32 	os_get_reltime(&now);
33 	os_reltime_sub(&now, &sta->sa_query_start, &passed);
34 	tu = (passed.sec * 1000000 + passed.usec) / 1024;
35 	if (hapd->conf->assoc_sa_query_max_timeout > tu)
36 		timeout = hapd->conf->assoc_sa_query_max_timeout - tu;
37 	else
38 		timeout = 0;
39 	if (timeout < hapd->conf->assoc_sa_query_max_timeout)
40 		timeout++; /* add some extra time for local timers */
41 	WPA_PUT_LE32(pos, timeout);
42 	pos += 4;
43 
44 	return pos;
45 }
46 
47 
48 /* MLME-SAQuery.request */
49 void ieee802_11_send_sa_query_req(struct hostapd_data *hapd,
50 				  const u8 *addr, const u8 *trans_id)
51 {
52 	struct ieee80211_mgmt mgmt;
53 	u8 *end;
54 
55 	wpa_printf(MSG_DEBUG, "IEEE 802.11: Sending SA Query Request to "
56 		   MACSTR, MAC2STR(addr));
57 	wpa_hexdump(MSG_DEBUG, "IEEE 802.11: SA Query Transaction ID",
58 		    trans_id, WLAN_SA_QUERY_TR_ID_LEN);
59 
60 	os_memset(&mgmt, 0, sizeof(mgmt));
61 	mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
62 					  WLAN_FC_STYPE_ACTION);
63 	os_memcpy(mgmt.da, addr, ETH_ALEN);
64 	os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
65 	os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
66 	mgmt.u.action.category = WLAN_ACTION_SA_QUERY;
67 	mgmt.u.action.u.sa_query_req.action = WLAN_SA_QUERY_REQUEST;
68 	os_memcpy(mgmt.u.action.u.sa_query_req.trans_id, trans_id,
69 		  WLAN_SA_QUERY_TR_ID_LEN);
70 	end = mgmt.u.action.u.sa_query_req.trans_id + WLAN_SA_QUERY_TR_ID_LEN;
71 	if (hostapd_drv_send_mlme(hapd, &mgmt, end - (u8 *) &mgmt, 0) < 0)
72 		wpa_printf(MSG_INFO, "ieee802_11_send_sa_query_req: send failed");
73 }
74 
75 
76 static void ieee802_11_send_sa_query_resp(struct hostapd_data *hapd,
77 					  const u8 *sa, const u8 *trans_id)
78 {
79 	struct sta_info *sta;
80 	struct ieee80211_mgmt resp;
81 	u8 *end;
82 
83 	wpa_printf(MSG_DEBUG, "IEEE 802.11: Received SA Query Request from "
84 		   MACSTR, MAC2STR(sa));
85 	wpa_hexdump(MSG_DEBUG, "IEEE 802.11: SA Query Transaction ID",
86 		    trans_id, WLAN_SA_QUERY_TR_ID_LEN);
87 
88 	sta = ap_get_sta(hapd, sa);
89 	if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) {
90 		wpa_printf(MSG_DEBUG, "IEEE 802.11: Ignore SA Query Request "
91 			   "from unassociated STA " MACSTR, MAC2STR(sa));
92 		return;
93 	}
94 
95 	wpa_printf(MSG_DEBUG, "IEEE 802.11: Sending SA Query Response to "
96 		   MACSTR, MAC2STR(sa));
97 
98 	os_memset(&resp, 0, sizeof(resp));
99 	resp.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
100 					  WLAN_FC_STYPE_ACTION);
101 	os_memcpy(resp.da, sa, ETH_ALEN);
102 	os_memcpy(resp.sa, hapd->own_addr, ETH_ALEN);
103 	os_memcpy(resp.bssid, hapd->own_addr, ETH_ALEN);
104 	resp.u.action.category = WLAN_ACTION_SA_QUERY;
105 	resp.u.action.u.sa_query_req.action = WLAN_SA_QUERY_RESPONSE;
106 	os_memcpy(resp.u.action.u.sa_query_req.trans_id, trans_id,
107 		  WLAN_SA_QUERY_TR_ID_LEN);
108 	end = resp.u.action.u.sa_query_req.trans_id + WLAN_SA_QUERY_TR_ID_LEN;
109 	if (hostapd_drv_send_mlme(hapd, &resp, end - (u8 *) &resp, 0) < 0)
110 		wpa_printf(MSG_INFO, "ieee80211_mgmt_sa_query_request: send failed");
111 }
112 
113 
114 void ieee802_11_sa_query_action(struct hostapd_data *hapd, const u8 *sa,
115 				const u8 action_type, const u8 *trans_id)
116 {
117 	struct sta_info *sta;
118 	int i;
119 
120 	if (action_type == WLAN_SA_QUERY_REQUEST) {
121 		ieee802_11_send_sa_query_resp(hapd, sa, trans_id);
122 		return;
123 	}
124 
125 	if (action_type != WLAN_SA_QUERY_RESPONSE) {
126 		wpa_printf(MSG_DEBUG, "IEEE 802.11: Unexpected SA Query "
127 			   "Action %d", action_type);
128 		return;
129 	}
130 
131 	wpa_printf(MSG_DEBUG, "IEEE 802.11: Received SA Query Response from "
132 		   MACSTR, MAC2STR(sa));
133 	wpa_hexdump(MSG_DEBUG, "IEEE 802.11: SA Query Transaction ID",
134 		    trans_id, WLAN_SA_QUERY_TR_ID_LEN);
135 
136 	/* MLME-SAQuery.confirm */
137 
138 	sta = ap_get_sta(hapd, sa);
139 	if (sta == NULL || sta->sa_query_trans_id == NULL) {
140 		wpa_printf(MSG_DEBUG, "IEEE 802.11: No matching STA with "
141 			   "pending SA Query request found");
142 		return;
143 	}
144 
145 	for (i = 0; i < sta->sa_query_count; i++) {
146 		if (os_memcmp(sta->sa_query_trans_id +
147 			      i * WLAN_SA_QUERY_TR_ID_LEN,
148 			      trans_id, WLAN_SA_QUERY_TR_ID_LEN) == 0)
149 			break;
150 	}
151 
152 	if (i >= sta->sa_query_count) {
153 		wpa_printf(MSG_DEBUG, "IEEE 802.11: No matching SA Query "
154 			   "transaction identifier found");
155 		return;
156 	}
157 
158 	hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
159 		       HOSTAPD_LEVEL_DEBUG,
160 		       "Reply to pending SA Query received");
161 	ap_sta_stop_sa_query(hapd, sta);
162 }
163 
164 #endif /* CONFIG_IEEE80211W */
165 
166 
167 static void hostapd_ext_capab_byte(struct hostapd_data *hapd, u8 *pos, int idx)
168 {
169 	*pos = 0x00;
170 
171 	switch (idx) {
172 	case 0: /* Bits 0-7 */
173 		if (hapd->iconf->obss_interval)
174 			*pos |= 0x01; /* Bit 0 - Coexistence management */
175 		break;
176 	case 1: /* Bits 8-15 */
177 		if (hapd->conf->proxy_arp)
178 			*pos |= 0x10; /* Bit 12 - Proxy ARP */
179 		break;
180 	case 2: /* Bits 16-23 */
181 		if (hapd->conf->wnm_sleep_mode)
182 			*pos |= 0x02; /* Bit 17 - WNM-Sleep Mode */
183 		if (hapd->conf->bss_transition)
184 			*pos |= 0x08; /* Bit 19 - BSS Transition */
185 		break;
186 	case 3: /* Bits 24-31 */
187 #ifdef CONFIG_WNM
188 		*pos |= 0x02; /* Bit 25 - SSID List */
189 #endif /* CONFIG_WNM */
190 		if (hapd->conf->time_advertisement == 2)
191 			*pos |= 0x08; /* Bit 27 - UTC TSF Offset */
192 		if (hapd->conf->interworking)
193 			*pos |= 0x80; /* Bit 31 - Interworking */
194 		break;
195 	case 4: /* Bits 32-39 */
196 		if (hapd->conf->qos_map_set_len)
197 			*pos |= 0x01; /* Bit 32 - QoS Map */
198 		if (hapd->conf->tdls & TDLS_PROHIBIT)
199 			*pos |= 0x40; /* Bit 38 - TDLS Prohibited */
200 		if (hapd->conf->tdls & TDLS_PROHIBIT_CHAN_SWITCH) {
201 			/* Bit 39 - TDLS Channel Switching Prohibited */
202 			*pos |= 0x80;
203 		}
204 		break;
205 	case 5: /* Bits 40-47 */
206 #ifdef CONFIG_HS20
207 		if (hapd->conf->hs20)
208 			*pos |= 0x40; /* Bit 46 - WNM-Notification */
209 #endif /* CONFIG_HS20 */
210 		break;
211 	case 6: /* Bits 48-55 */
212 		if (hapd->conf->ssid.utf8_ssid)
213 			*pos |= 0x01; /* Bit 48 - UTF-8 SSID */
214 		break;
215 	}
216 }
217 
218 
219 u8 * hostapd_eid_ext_capab(struct hostapd_data *hapd, u8 *eid)
220 {
221 	u8 *pos = eid;
222 	u8 len = 0, i;
223 
224 	if (hapd->conf->tdls & (TDLS_PROHIBIT | TDLS_PROHIBIT_CHAN_SWITCH))
225 		len = 5;
226 	if (len < 4 && hapd->conf->interworking)
227 		len = 4;
228 	if (len < 3 && hapd->conf->wnm_sleep_mode)
229 		len = 3;
230 	if (len < 1 && hapd->iconf->obss_interval)
231 		len = 1;
232 	if (len < 7 && hapd->conf->ssid.utf8_ssid)
233 		len = 7;
234 #ifdef CONFIG_WNM
235 	if (len < 4)
236 		len = 4;
237 #endif /* CONFIG_WNM */
238 #ifdef CONFIG_HS20
239 	if (hapd->conf->hs20 && len < 6)
240 		len = 6;
241 #endif /* CONFIG_HS20 */
242 	if (len < hapd->iface->extended_capa_len)
243 		len = hapd->iface->extended_capa_len;
244 	if (len == 0)
245 		return eid;
246 
247 	*pos++ = WLAN_EID_EXT_CAPAB;
248 	*pos++ = len;
249 	for (i = 0; i < len; i++, pos++) {
250 		hostapd_ext_capab_byte(hapd, pos, i);
251 
252 		if (i < hapd->iface->extended_capa_len) {
253 			*pos &= ~hapd->iface->extended_capa_mask[i];
254 			*pos |= hapd->iface->extended_capa[i];
255 		}
256 	}
257 
258 	while (len > 0 && eid[1 + len] == 0) {
259 		len--;
260 		eid[1] = len;
261 	}
262 	if (len == 0)
263 		return eid;
264 
265 	return eid + 2 + len;
266 }
267 
268 
269 u8 * hostapd_eid_qos_map_set(struct hostapd_data *hapd, u8 *eid)
270 {
271 	u8 *pos = eid;
272 	u8 len = hapd->conf->qos_map_set_len;
273 
274 	if (!len)
275 		return eid;
276 
277 	*pos++ = WLAN_EID_QOS_MAP_SET;
278 	*pos++ = len;
279 	os_memcpy(pos, hapd->conf->qos_map_set, len);
280 	pos += len;
281 
282 	return pos;
283 }
284 
285 
286 u8 * hostapd_eid_interworking(struct hostapd_data *hapd, u8 *eid)
287 {
288 	u8 *pos = eid;
289 #ifdef CONFIG_INTERWORKING
290 	u8 *len;
291 
292 	if (!hapd->conf->interworking)
293 		return eid;
294 
295 	*pos++ = WLAN_EID_INTERWORKING;
296 	len = pos++;
297 
298 	*pos = hapd->conf->access_network_type;
299 	if (hapd->conf->internet)
300 		*pos |= INTERWORKING_ANO_INTERNET;
301 	if (hapd->conf->asra)
302 		*pos |= INTERWORKING_ANO_ASRA;
303 	if (hapd->conf->esr)
304 		*pos |= INTERWORKING_ANO_ESR;
305 	if (hapd->conf->uesa)
306 		*pos |= INTERWORKING_ANO_UESA;
307 	pos++;
308 
309 	if (hapd->conf->venue_info_set) {
310 		*pos++ = hapd->conf->venue_group;
311 		*pos++ = hapd->conf->venue_type;
312 	}
313 
314 	if (!is_zero_ether_addr(hapd->conf->hessid)) {
315 		os_memcpy(pos, hapd->conf->hessid, ETH_ALEN);
316 		pos += ETH_ALEN;
317 	}
318 
319 	*len = pos - len - 1;
320 #endif /* CONFIG_INTERWORKING */
321 
322 	return pos;
323 }
324 
325 
326 u8 * hostapd_eid_adv_proto(struct hostapd_data *hapd, u8 *eid)
327 {
328 	u8 *pos = eid;
329 #ifdef CONFIG_INTERWORKING
330 
331 	/* TODO: Separate configuration for ANQP? */
332 	if (!hapd->conf->interworking)
333 		return eid;
334 
335 	*pos++ = WLAN_EID_ADV_PROTO;
336 	*pos++ = 2;
337 	*pos++ = 0x7F; /* Query Response Length Limit | PAME-BI */
338 	*pos++ = ACCESS_NETWORK_QUERY_PROTOCOL;
339 #endif /* CONFIG_INTERWORKING */
340 
341 	return pos;
342 }
343 
344 
345 u8 * hostapd_eid_roaming_consortium(struct hostapd_data *hapd, u8 *eid)
346 {
347 	u8 *pos = eid;
348 #ifdef CONFIG_INTERWORKING
349 	u8 *len;
350 	unsigned int i, count;
351 
352 	if (!hapd->conf->interworking ||
353 	    hapd->conf->roaming_consortium == NULL ||
354 	    hapd->conf->roaming_consortium_count == 0)
355 		return eid;
356 
357 	*pos++ = WLAN_EID_ROAMING_CONSORTIUM;
358 	len = pos++;
359 
360 	/* Number of ANQP OIs (in addition to the max 3 listed here) */
361 	if (hapd->conf->roaming_consortium_count > 3 + 255)
362 		*pos++ = 255;
363 	else if (hapd->conf->roaming_consortium_count > 3)
364 		*pos++ = hapd->conf->roaming_consortium_count - 3;
365 	else
366 		*pos++ = 0;
367 
368 	/* OU #1 and #2 Lengths */
369 	*pos = hapd->conf->roaming_consortium[0].len;
370 	if (hapd->conf->roaming_consortium_count > 1)
371 		*pos |= hapd->conf->roaming_consortium[1].len << 4;
372 	pos++;
373 
374 	if (hapd->conf->roaming_consortium_count > 3)
375 		count = 3;
376 	else
377 		count = hapd->conf->roaming_consortium_count;
378 
379 	for (i = 0; i < count; i++) {
380 		os_memcpy(pos, hapd->conf->roaming_consortium[i].oi,
381 			  hapd->conf->roaming_consortium[i].len);
382 		pos += hapd->conf->roaming_consortium[i].len;
383 	}
384 
385 	*len = pos - len - 1;
386 #endif /* CONFIG_INTERWORKING */
387 
388 	return pos;
389 }
390 
391 
392 u8 * hostapd_eid_time_adv(struct hostapd_data *hapd, u8 *eid)
393 {
394 	if (hapd->conf->time_advertisement != 2)
395 		return eid;
396 
397 	if (hapd->time_adv == NULL &&
398 	    hostapd_update_time_adv(hapd) < 0)
399 		return eid;
400 
401 	if (hapd->time_adv == NULL)
402 		return eid;
403 
404 	os_memcpy(eid, wpabuf_head(hapd->time_adv),
405 		  wpabuf_len(hapd->time_adv));
406 	eid += wpabuf_len(hapd->time_adv);
407 
408 	return eid;
409 }
410 
411 
412 u8 * hostapd_eid_time_zone(struct hostapd_data *hapd, u8 *eid)
413 {
414 	size_t len;
415 
416 	if (hapd->conf->time_advertisement != 2)
417 		return eid;
418 
419 	len = os_strlen(hapd->conf->time_zone);
420 
421 	*eid++ = WLAN_EID_TIME_ZONE;
422 	*eid++ = len;
423 	os_memcpy(eid, hapd->conf->time_zone, len);
424 	eid += len;
425 
426 	return eid;
427 }
428 
429 
430 int hostapd_update_time_adv(struct hostapd_data *hapd)
431 {
432 	const int elen = 2 + 1 + 10 + 5 + 1;
433 	struct os_time t;
434 	struct os_tm tm;
435 	u8 *pos;
436 
437 	if (hapd->conf->time_advertisement != 2)
438 		return 0;
439 
440 	if (os_get_time(&t) < 0 || os_gmtime(t.sec, &tm) < 0)
441 		return -1;
442 
443 	if (!hapd->time_adv) {
444 		hapd->time_adv = wpabuf_alloc(elen);
445 		if (hapd->time_adv == NULL)
446 			return -1;
447 		pos = wpabuf_put(hapd->time_adv, elen);
448 	} else
449 		pos = wpabuf_mhead_u8(hapd->time_adv);
450 
451 	*pos++ = WLAN_EID_TIME_ADVERTISEMENT;
452 	*pos++ = 1 + 10 + 5 + 1;
453 
454 	*pos++ = 2; /* UTC time at which the TSF timer is 0 */
455 
456 	/* Time Value at TSF 0 */
457 	/* FIX: need to calculate this based on the current TSF value */
458 	WPA_PUT_LE16(pos, tm.year); /* Year */
459 	pos += 2;
460 	*pos++ = tm.month; /* Month */
461 	*pos++ = tm.day; /* Day of month */
462 	*pos++ = tm.hour; /* Hours */
463 	*pos++ = tm.min; /* Minutes */
464 	*pos++ = tm.sec; /* Seconds */
465 	WPA_PUT_LE16(pos, 0); /* Milliseconds (not used) */
466 	pos += 2;
467 	*pos++ = 0; /* Reserved */
468 
469 	/* Time Error */
470 	/* TODO: fill in an estimate on the error */
471 	*pos++ = 0;
472 	*pos++ = 0;
473 	*pos++ = 0;
474 	*pos++ = 0;
475 	*pos++ = 0;
476 
477 	*pos++ = hapd->time_update_counter++;
478 
479 	return 0;
480 }
481 
482 
483 u8 * hostapd_eid_bss_max_idle_period(struct hostapd_data *hapd, u8 *eid)
484 {
485 	u8 *pos = eid;
486 
487 #ifdef CONFIG_WNM
488 	if (hapd->conf->ap_max_inactivity > 0) {
489 		unsigned int val;
490 		*pos++ = WLAN_EID_BSS_MAX_IDLE_PERIOD;
491 		*pos++ = 3;
492 		val = hapd->conf->ap_max_inactivity;
493 		if (val > 68000)
494 			val = 68000;
495 		val *= 1000;
496 		val /= 1024;
497 		if (val == 0)
498 			val = 1;
499 		if (val > 65535)
500 			val = 65535;
501 		WPA_PUT_LE16(pos, val);
502 		pos += 2;
503 		*pos++ = 0x00; /* TODO: Protected Keep-Alive Required */
504 	}
505 #endif /* CONFIG_WNM */
506 
507 	return pos;
508 }
509