P2P: Verify that assoc req IEs are available for group update
[mech_eap.git] / src / ap / drv_callbacks.c
1 /*
2  * hostapd / Callback functions for driver wrappers
3  * Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include "utils/includes.h"
16
17 #include "utils/common.h"
18 #include "radius/radius.h"
19 #include "drivers/driver.h"
20 #include "common/ieee802_11_defs.h"
21 #include "common/ieee802_11_common.h"
22 #include "crypto/random.h"
23 #include "p2p/p2p.h"
24 #include "wps/wps.h"
25 #include "hostapd.h"
26 #include "ieee802_11.h"
27 #include "sta_info.h"
28 #include "accounting.h"
29 #include "tkip_countermeasures.h"
30 #include "ieee802_1x.h"
31 #include "wpa_auth.h"
32 #include "wps_hostapd.h"
33 #include "ap_drv_ops.h"
34 #include "ap_config.h"
35
36
37 int hostapd_notif_assoc(struct hostapd_data *hapd, const u8 *addr,
38                         const u8 *req_ies, size_t req_ies_len, int reassoc)
39 {
40         struct sta_info *sta;
41         int new_assoc, res;
42         struct ieee802_11_elems elems;
43         const u8 *ie;
44         size_t ielen;
45         u16 reason = WLAN_REASON_UNSPECIFIED;
46
47         if (addr == NULL) {
48                 /*
49                  * This could potentially happen with unexpected event from the
50                  * driver wrapper. This was seen at least in one case where the
51                  * driver ended up being set to station mode while hostapd was
52                  * running, so better make sure we stop processing such an
53                  * event here.
54                  */
55                 wpa_printf(MSG_DEBUG, "hostapd_notif_assoc: Skip event with "
56                            "no address");
57                 return -1;
58         }
59         random_add_randomness(addr, ETH_ALEN);
60
61         hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
62                        HOSTAPD_LEVEL_INFO, "associated");
63
64         ieee802_11_parse_elems(req_ies, req_ies_len, &elems, 0);
65         if (elems.wps_ie) {
66                 ie = elems.wps_ie - 2;
67                 ielen = elems.wps_ie_len + 2;
68                 wpa_printf(MSG_DEBUG, "STA included WPS IE in (Re)AssocReq");
69         } else if (elems.rsn_ie) {
70                 ie = elems.rsn_ie - 2;
71                 ielen = elems.rsn_ie_len + 2;
72                 wpa_printf(MSG_DEBUG, "STA included RSN IE in (Re)AssocReq");
73         } else if (elems.wpa_ie) {
74                 ie = elems.wpa_ie - 2;
75                 ielen = elems.wpa_ie_len + 2;
76                 wpa_printf(MSG_DEBUG, "STA included WPA IE in (Re)AssocReq");
77         } else {
78                 ie = NULL;
79                 ielen = 0;
80                 wpa_printf(MSG_DEBUG, "STA did not include WPS/RSN/WPA IE in "
81                            "(Re)AssocReq");
82         }
83
84         sta = ap_get_sta(hapd, addr);
85         if (sta) {
86                 accounting_sta_stop(hapd, sta);
87         } else {
88                 sta = ap_sta_add(hapd, addr);
89                 if (sta == NULL)
90                         return -1;
91         }
92         sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS | WLAN_STA_WPS2);
93
94 #ifdef CONFIG_P2P
95         if (elems.p2p) {
96                 wpabuf_free(sta->p2p_ie);
97                 sta->p2p_ie = ieee802_11_vendor_ie_concat(req_ies, req_ies_len,
98                                                           P2P_IE_VENDOR_TYPE);
99         }
100 #endif /* CONFIG_P2P */
101
102         if (hapd->conf->wpa) {
103                 if (ie == NULL || ielen == 0) {
104 #ifdef CONFIG_WPS
105                         if (hapd->conf->wps_state) {
106                                 wpa_printf(MSG_DEBUG, "STA did not include "
107                                            "WPA/RSN IE in (Re)Association "
108                                            "Request - possible WPS use");
109                                 sta->flags |= WLAN_STA_MAYBE_WPS;
110                                 goto skip_wpa_check;
111                         }
112 #endif /* CONFIG_WPS */
113
114                         wpa_printf(MSG_DEBUG, "No WPA/RSN IE from STA");
115                         return -1;
116                 }
117 #ifdef CONFIG_WPS
118                 if (hapd->conf->wps_state && ie[0] == 0xdd && ie[1] >= 4 &&
119                     os_memcmp(ie + 2, "\x00\x50\xf2\x04", 4) == 0) {
120                         struct wpabuf *wps;
121                         sta->flags |= WLAN_STA_WPS;
122                         wps = ieee802_11_vendor_ie_concat(ie, ielen,
123                                                           WPS_IE_VENDOR_TYPE);
124                         if (wps) {
125                                 if (wps_is_20(wps)) {
126                                         wpa_printf(MSG_DEBUG, "WPS: STA "
127                                                    "supports WPS 2.0");
128                                         sta->flags |= WLAN_STA_WPS2;
129                                 }
130                                 wpabuf_free(wps);
131                         }
132                         goto skip_wpa_check;
133                 }
134 #endif /* CONFIG_WPS */
135
136                 if (sta->wpa_sm == NULL)
137                         sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
138                                                         sta->addr);
139                 if (sta->wpa_sm == NULL) {
140                         wpa_printf(MSG_ERROR, "Failed to initialize WPA state "
141                                    "machine");
142                         return -1;
143                 }
144                 res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
145                                           ie, ielen, NULL, 0);
146                 if (res != WPA_IE_OK) {
147                         wpa_printf(MSG_DEBUG, "WPA/RSN information element "
148                                    "rejected? (res %u)", res);
149                         wpa_hexdump(MSG_DEBUG, "IE", ie, ielen);
150                         if (res == WPA_INVALID_GROUP)
151                                 reason = WLAN_REASON_GROUP_CIPHER_NOT_VALID;
152                         else if (res == WPA_INVALID_PAIRWISE)
153                                 reason = WLAN_REASON_PAIRWISE_CIPHER_NOT_VALID;
154                         else if (res == WPA_INVALID_AKMP)
155                                 reason = WLAN_REASON_AKMP_NOT_VALID;
156 #ifdef CONFIG_IEEE80211W
157                         else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION)
158                                 reason = WLAN_REASON_INVALID_IE;
159                         else if (res == WPA_INVALID_MGMT_GROUP_CIPHER)
160                                 reason = WLAN_REASON_GROUP_CIPHER_NOT_VALID;
161 #endif /* CONFIG_IEEE80211W */
162                         else
163                                 reason = WLAN_REASON_INVALID_IE;
164                         goto fail;
165                 }
166         } else if (hapd->conf->wps_state) {
167 #ifdef CONFIG_WPS
168                 struct wpabuf *wps;
169                 if (req_ies)
170                         wps = ieee802_11_vendor_ie_concat(req_ies, req_ies_len,
171                                                           WPS_IE_VENDOR_TYPE);
172                 else
173                         wps = NULL;
174 #ifdef CONFIG_WPS_STRICT
175                 if (wps && wps_validate_assoc_req(wps) < 0) {
176                         reason = WLAN_REASON_INVALID_IE;
177                         wpabuf_free(wps);
178                         goto fail;
179                 }
180 #endif /* CONFIG_WPS_STRICT */
181                 if (wps) {
182                         sta->flags |= WLAN_STA_WPS;
183                         if (wps_is_20(wps)) {
184                                 wpa_printf(MSG_DEBUG, "WPS: STA supports "
185                                            "WPS 2.0");
186                                 sta->flags |= WLAN_STA_WPS2;
187                         }
188                 } else
189                         sta->flags |= WLAN_STA_MAYBE_WPS;
190                 wpabuf_free(wps);
191 #endif /* CONFIG_WPS */
192         }
193 #ifdef CONFIG_WPS
194 skip_wpa_check:
195 #endif /* CONFIG_WPS */
196
197         new_assoc = (sta->flags & WLAN_STA_ASSOC) == 0;
198         sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
199         wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
200
201         hostapd_new_assoc_sta(hapd, sta, !new_assoc);
202
203         ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
204
205 #ifdef CONFIG_P2P
206         if (req_ies) {
207                 p2p_group_notif_assoc(hapd->p2p_group, sta->addr,
208                                       req_ies, req_ies_len);
209         }
210 #endif /* CONFIG_P2P */
211
212         return 0;
213
214 fail:
215         hostapd_drv_sta_disassoc(hapd, sta->addr, reason);
216         ap_free_sta(hapd, sta);
217         return -1;
218 }
219
220
221 void hostapd_notif_disassoc(struct hostapd_data *hapd, const u8 *addr)
222 {
223         struct sta_info *sta;
224
225         if (addr == NULL) {
226                 /*
227                  * This could potentially happen with unexpected event from the
228                  * driver wrapper. This was seen at least in one case where the
229                  * driver ended up reporting a station mode event while hostapd
230                  * was running, so better make sure we stop processing such an
231                  * event here.
232                  */
233                 wpa_printf(MSG_DEBUG, "hostapd_notif_disassoc: Skip event "
234                            "with no address");
235                 return;
236         }
237
238         hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
239                        HOSTAPD_LEVEL_INFO, "disassociated");
240
241         sta = ap_get_sta(hapd, addr);
242         if (sta == NULL) {
243                 wpa_printf(MSG_DEBUG, "Disassociation notification for "
244                            "unknown STA " MACSTR, MAC2STR(addr));
245                 return;
246         }
247
248         ap_sta_set_authorized(hapd, sta, 0);
249         sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
250         wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
251         sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
252         ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
253         ap_free_sta(hapd, sta);
254 }
255
256
257 void hostapd_event_sta_low_ack(struct hostapd_data *hapd, const u8 *addr)
258 {
259         struct sta_info *sta = ap_get_sta(hapd, addr);
260
261         if (!sta || !hapd->conf->disassoc_low_ack)
262                 return;
263
264         hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
265                        HOSTAPD_LEVEL_INFO, "disconnected due to excessive "
266                        "missing ACKs");
267         hostapd_drv_sta_disassoc(hapd, addr, WLAN_REASON_DISASSOC_LOW_ACK);
268         if (sta)
269                 ap_sta_disassociate(hapd, sta, WLAN_REASON_DISASSOC_LOW_ACK);
270 }
271
272
273 int hostapd_probe_req_rx(struct hostapd_data *hapd, const u8 *sa, const u8 *da,
274                          const u8 *bssid, const u8 *ie, size_t ie_len)
275 {
276         size_t i;
277         int ret = 0;
278
279         if (sa == NULL || ie == NULL)
280                 return -1;
281
282         random_add_randomness(sa, ETH_ALEN);
283         for (i = 0; hapd->probereq_cb && i < hapd->num_probereq_cb; i++) {
284                 if (hapd->probereq_cb[i].cb(hapd->probereq_cb[i].ctx,
285                                             sa, da, bssid, ie, ie_len) > 0) {
286                         ret = 1;
287                         break;
288                 }
289         }
290         return ret;
291 }
292
293
294 #ifdef HOSTAPD
295
296 #ifdef NEED_AP_MLME
297
298 #define HAPD_BROADCAST ((struct hostapd_data *) -1)
299
300 static struct hostapd_data * get_hapd_bssid(struct hostapd_iface *iface,
301                                             const u8 *bssid)
302 {
303         size_t i;
304
305         if (bssid == NULL)
306                 return NULL;
307         if (bssid[0] == 0xff && bssid[1] == 0xff && bssid[2] == 0xff &&
308             bssid[3] == 0xff && bssid[4] == 0xff && bssid[5] == 0xff)
309                 return HAPD_BROADCAST;
310
311         for (i = 0; i < iface->num_bss; i++) {
312                 if (os_memcmp(bssid, iface->bss[i]->own_addr, ETH_ALEN) == 0)
313                         return iface->bss[i];
314         }
315
316         return NULL;
317 }
318
319
320 static void hostapd_rx_from_unknown_sta(struct hostapd_data *hapd,
321                                         const u8 *bssid, const u8 *addr,
322                                         int wds)
323 {
324         hapd = get_hapd_bssid(hapd->iface, bssid);
325         if (hapd == NULL || hapd == HAPD_BROADCAST)
326                 return;
327
328         ieee802_11_rx_from_unknown(hapd, addr, wds);
329 }
330
331
332 static void hostapd_mgmt_rx(struct hostapd_data *hapd, struct rx_mgmt *rx_mgmt)
333 {
334         struct hostapd_iface *iface = hapd->iface;
335         const struct ieee80211_hdr *hdr;
336         const u8 *bssid;
337         struct hostapd_frame_info fi;
338
339         hdr = (const struct ieee80211_hdr *) rx_mgmt->frame;
340         bssid = get_hdr_bssid(hdr, rx_mgmt->frame_len);
341         if (bssid == NULL)
342                 return;
343
344         hapd = get_hapd_bssid(iface, bssid);
345         if (hapd == NULL) {
346                 u16 fc;
347                 fc = le_to_host16(hdr->frame_control);
348
349                 /*
350                  * Drop frames to unknown BSSIDs except for Beacon frames which
351                  * could be used to update neighbor information.
352                  */
353                 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
354                     WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
355                         hapd = iface->bss[0];
356                 else
357                         return;
358         }
359
360         os_memset(&fi, 0, sizeof(fi));
361         fi.datarate = rx_mgmt->datarate;
362         fi.ssi_signal = rx_mgmt->ssi_signal;
363
364         if (hapd == HAPD_BROADCAST) {
365                 size_t i;
366                 for (i = 0; i < iface->num_bss; i++)
367                         ieee802_11_mgmt(iface->bss[i], rx_mgmt->frame,
368                                         rx_mgmt->frame_len, &fi);
369         } else
370                 ieee802_11_mgmt(hapd, rx_mgmt->frame, rx_mgmt->frame_len, &fi);
371
372         random_add_randomness(&fi, sizeof(fi));
373 }
374
375
376 static void hostapd_rx_action(struct hostapd_data *hapd,
377                               struct rx_action *rx_action)
378 {
379         struct rx_mgmt rx_mgmt;
380         u8 *buf;
381         struct ieee80211_hdr *hdr;
382
383         wpa_printf(MSG_DEBUG, "EVENT_RX_ACTION DA=" MACSTR " SA=" MACSTR
384                    " BSSID=" MACSTR " category=%u",
385                    MAC2STR(rx_action->da), MAC2STR(rx_action->sa),
386                    MAC2STR(rx_action->bssid), rx_action->category);
387         wpa_hexdump(MSG_MSGDUMP, "Received action frame contents",
388                     rx_action->data, rx_action->len);
389
390         buf = os_zalloc(24 + 1 + rx_action->len);
391         if (buf == NULL)
392                 return;
393         hdr = (struct ieee80211_hdr *) buf;
394         hdr->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
395                                           WLAN_FC_STYPE_ACTION);
396         if (rx_action->category == WLAN_ACTION_SA_QUERY) {
397                 /*
398                  * Assume frame was protected; it would have been dropped if
399                  * not.
400                  */
401                 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
402         }
403         os_memcpy(hdr->addr1, rx_action->da, ETH_ALEN);
404         os_memcpy(hdr->addr2, rx_action->sa, ETH_ALEN);
405         os_memcpy(hdr->addr3, rx_action->bssid, ETH_ALEN);
406         buf[24] = rx_action->category;
407         os_memcpy(buf + 24 + 1, rx_action->data, rx_action->len);
408         os_memset(&rx_mgmt, 0, sizeof(rx_mgmt));
409         rx_mgmt.frame = buf;
410         rx_mgmt.frame_len = 24 + 1 + rx_action->len;
411         hostapd_mgmt_rx(hapd, &rx_mgmt);
412         os_free(buf);
413 }
414
415
416 static void hostapd_mgmt_tx_cb(struct hostapd_data *hapd, const u8 *buf,
417                                size_t len, u16 stype, int ok)
418 {
419         struct ieee80211_hdr *hdr;
420         hdr = (struct ieee80211_hdr *) buf;
421         hapd = get_hapd_bssid(hapd->iface, get_hdr_bssid(hdr, len));
422         if (hapd == NULL || hapd == HAPD_BROADCAST)
423                 return;
424         ieee802_11_mgmt_cb(hapd, buf, len, stype, ok);
425 }
426
427 #endif /* NEED_AP_MLME */
428
429
430 static int hostapd_event_new_sta(struct hostapd_data *hapd, const u8 *addr)
431 {
432         struct sta_info *sta = ap_get_sta(hapd, addr);
433         if (sta)
434                 return 0;
435
436         wpa_printf(MSG_DEBUG, "Data frame from unknown STA " MACSTR
437                    " - adding a new STA", MAC2STR(addr));
438         sta = ap_sta_add(hapd, addr);
439         if (sta) {
440                 hostapd_new_assoc_sta(hapd, sta, 0);
441         } else {
442                 wpa_printf(MSG_DEBUG, "Failed to add STA entry for " MACSTR,
443                            MAC2STR(addr));
444                 return -1;
445         }
446
447         return 0;
448 }
449
450
451 static void hostapd_event_eapol_rx(struct hostapd_data *hapd, const u8 *src,
452                                    const u8 *data, size_t data_len)
453 {
454         struct hostapd_iface *iface = hapd->iface;
455         size_t j;
456
457         for (j = 0; j < iface->num_bss; j++) {
458                 if (ap_get_sta(iface->bss[j], src)) {
459                         hapd = iface->bss[j];
460                         break;
461                 }
462         }
463
464         ieee802_1x_receive(hapd, src, data, data_len);
465 }
466
467
468 void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
469                           union wpa_event_data *data)
470 {
471         struct hostapd_data *hapd = ctx;
472
473         switch (event) {
474         case EVENT_MICHAEL_MIC_FAILURE:
475                 michael_mic_failure(hapd, data->michael_mic_failure.src, 1);
476                 break;
477         case EVENT_SCAN_RESULTS:
478                 if (hapd->iface->scan_cb)
479                         hapd->iface->scan_cb(hapd->iface);
480                 break;
481 #ifdef CONFIG_IEEE80211R
482         case EVENT_FT_RRB_RX:
483                 wpa_ft_rrb_rx(hapd->wpa_auth, data->ft_rrb_rx.src,
484                               data->ft_rrb_rx.data, data->ft_rrb_rx.data_len);
485                 break;
486 #endif /* CONFIG_IEEE80211R */
487         case EVENT_WPS_BUTTON_PUSHED:
488                 hostapd_wps_button_pushed(hapd, NULL);
489                 break;
490 #ifdef NEED_AP_MLME
491         case EVENT_TX_STATUS:
492                 switch (data->tx_status.type) {
493                 case WLAN_FC_TYPE_MGMT:
494                         hostapd_mgmt_tx_cb(hapd, data->tx_status.data,
495                                            data->tx_status.data_len,
496                                            data->tx_status.stype,
497                                            data->tx_status.ack);
498                         break;
499                 case WLAN_FC_TYPE_DATA:
500                         hostapd_tx_status(hapd, data->tx_status.dst,
501                                           data->tx_status.data,
502                                           data->tx_status.data_len,
503                                           data->tx_status.ack);
504                         break;
505                 }
506                 break;
507         case EVENT_DRIVER_CLIENT_POLL_OK:
508                 hostapd_client_poll_ok(hapd, data->client_poll.addr);
509                 break;
510         case EVENT_RX_FROM_UNKNOWN:
511                 hostapd_rx_from_unknown_sta(hapd, data->rx_from_unknown.bssid,
512                                             data->rx_from_unknown.addr,
513                                             data->rx_from_unknown.wds);
514                 break;
515         case EVENT_RX_MGMT:
516                 hostapd_mgmt_rx(hapd, &data->rx_mgmt);
517                 break;
518 #endif /* NEED_AP_MLME */
519         case EVENT_RX_PROBE_REQ:
520                 if (data->rx_probe_req.sa == NULL ||
521                     data->rx_probe_req.ie == NULL)
522                         break;
523                 hostapd_probe_req_rx(hapd, data->rx_probe_req.sa,
524                                      data->rx_probe_req.da,
525                                      data->rx_probe_req.bssid,
526                                      data->rx_probe_req.ie,
527                                      data->rx_probe_req.ie_len);
528                 break;
529         case EVENT_NEW_STA:
530                 hostapd_event_new_sta(hapd, data->new_sta.addr);
531                 break;
532         case EVENT_EAPOL_RX:
533                 hostapd_event_eapol_rx(hapd, data->eapol_rx.src,
534                                        data->eapol_rx.data,
535                                        data->eapol_rx.data_len);
536                 break;
537         case EVENT_ASSOC:
538                 hostapd_notif_assoc(hapd, data->assoc_info.addr,
539                                     data->assoc_info.req_ies,
540                                     data->assoc_info.req_ies_len,
541                                     data->assoc_info.reassoc);
542                 break;
543         case EVENT_DISASSOC:
544                 if (data)
545                         hostapd_notif_disassoc(hapd, data->disassoc_info.addr);
546                 break;
547         case EVENT_DEAUTH:
548                 if (data)
549                         hostapd_notif_disassoc(hapd, data->deauth_info.addr);
550                 break;
551         case EVENT_STATION_LOW_ACK:
552                 if (!data)
553                         break;
554                 hostapd_event_sta_low_ack(hapd, data->low_ack.addr);
555                 break;
556 #ifdef NEED_AP_MLME
557         case EVENT_RX_ACTION:
558                 if (data->rx_action.da == NULL || data->rx_action.sa == NULL ||
559                     data->rx_action.bssid == NULL)
560                         break;
561                 hostapd_rx_action(hapd, &data->rx_action);
562                 break;
563 #endif /* NEED_AP_MLME */
564         default:
565                 wpa_printf(MSG_DEBUG, "Unknown event %d", event);
566                 break;
567         }
568 }
569
570 #endif /* HOSTAPD */