1/*
2 * hidl interface for wpa_supplicant daemon
3 * Copyright (c) 2004-2016, Jouni Malinen <j@w1.fi>
4 * Copyright (c) 2004-2016, Roshan Pius <rpius@google.com>
5 *
6 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
8 */
9
10#include <hwbinder/IPCThreadState.h>
11
12#include <hidl/HidlTransportSupport.h>
13#include "hidl_manager.h"
14
15extern "C" {
16#include "hidl.h"
17#include "hidl_i.h"
18#include "utils/common.h"
19#include "utils/eloop.h"
20#include "utils/includes.h"
21}
22
23using android::hardware::configureRpcThreadpool;
24using android::hardware::setupTransportPolling;
25using android::hardware::handleTransportPoll;
26using android::hardware::wifi::supplicant::V1_0::implementation::HidlManager;
27
28void wpas_hidl_sock_handler(
29    int sock, void * /* eloop_ctx */, void * /* sock_ctx */)
30{
31	handleTransportPoll(sock);
32}
33
34struct wpas_hidl_priv *wpas_hidl_init(struct wpa_global *global)
35{
36	struct wpas_hidl_priv *priv;
37	HidlManager *hidl_manager;
38
39	priv = (wpas_hidl_priv *)os_zalloc(sizeof(*priv));
40	if (!priv)
41		return NULL;
42	priv->global = global;
43
44	wpa_printf(MSG_DEBUG, "Initing hidl control");
45
46	configureRpcThreadpool(1, true /* callerWillJoin */);
47	priv->hidl_fd = setupTransportPolling();
48	if (priv->hidl_fd < 0)
49		goto err;
50
51	wpa_printf(MSG_INFO, "Processing hidl events on FD %d", priv->hidl_fd);
52	// Look for read events from the hidl socket in the eloop.
53	if (eloop_register_read_sock(
54		priv->hidl_fd, wpas_hidl_sock_handler, global, priv) < 0)
55		goto err;
56
57	hidl_manager = HidlManager::getInstance();
58	if (!hidl_manager)
59		goto err;
60	hidl_manager->registerHidlService(global);
61	// We may not need to store this hidl manager reference in the
62	// global data strucure because we've made it a singleton class.
63	priv->hidl_manager = (void *)hidl_manager;
64
65	return priv;
66err:
67	wpas_hidl_deinit(priv);
68	return NULL;
69}
70
71void wpas_hidl_deinit(struct wpas_hidl_priv *priv)
72{
73	if (!priv)
74		return;
75
76	wpa_printf(MSG_DEBUG, "Deiniting hidl control");
77
78	HidlManager::destroyInstance();
79	eloop_unregister_read_sock(priv->hidl_fd);
80	os_free(priv);
81}
82
83int wpas_hidl_register_interface(struct wpa_supplicant *wpa_s)
84{
85	if (!wpa_s || !wpa_s->global->hidl)
86		return 1;
87
88	wpa_printf(
89	    MSG_DEBUG, "Registering interface to hidl control: %s",
90	    wpa_s->ifname);
91
92	HidlManager *hidl_manager = HidlManager::getInstance();
93	if (!hidl_manager)
94		return 1;
95
96	return hidl_manager->registerInterface(wpa_s);
97}
98
99int wpas_hidl_unregister_interface(struct wpa_supplicant *wpa_s)
100{
101	if (!wpa_s || !wpa_s->global->hidl)
102		return 1;
103
104	wpa_printf(
105	    MSG_DEBUG, "Deregistering interface from hidl control: %s",
106	    wpa_s->ifname);
107
108	HidlManager *hidl_manager = HidlManager::getInstance();
109	if (!hidl_manager)
110		return 1;
111
112	return hidl_manager->unregisterInterface(wpa_s);
113}
114
115int wpas_hidl_register_network(
116    struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
117{
118	if (!wpa_s || !wpa_s->global->hidl || !ssid)
119		return 1;
120
121	wpa_printf(
122	    MSG_DEBUG, "Registering network to hidl control: %d", ssid->id);
123
124	HidlManager *hidl_manager = HidlManager::getInstance();
125	if (!hidl_manager)
126		return 1;
127
128	return hidl_manager->registerNetwork(wpa_s, ssid);
129}
130
131int wpas_hidl_unregister_network(
132    struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
133{
134	if (!wpa_s || !wpa_s->global->hidl || !ssid)
135		return 1;
136
137	wpa_printf(
138	    MSG_DEBUG, "Deregistering network from hidl control: %d", ssid->id);
139
140	HidlManager *hidl_manager = HidlManager::getInstance();
141	if (!hidl_manager)
142		return 1;
143
144	return hidl_manager->unregisterNetwork(wpa_s, ssid);
145}
146
147int wpas_hidl_notify_state_changed(struct wpa_supplicant *wpa_s)
148{
149	if (!wpa_s || !wpa_s->global->hidl)
150		return 1;
151
152	wpa_printf(
153	    MSG_DEBUG, "Notifying state change event to hidl control: %d",
154	    wpa_s->wpa_state);
155
156	HidlManager *hidl_manager = HidlManager::getInstance();
157	if (!hidl_manager)
158		return 1;
159
160	return hidl_manager->notifyStateChange(wpa_s);
161}
162
163int wpas_hidl_notify_network_request(
164    struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
165    enum wpa_ctrl_req_type rtype, const char *default_txt)
166{
167	if (!wpa_s || !wpa_s->global->hidl || !ssid)
168		return 1;
169
170	wpa_printf(
171	    MSG_DEBUG, "Notifying network request to hidl control: %d",
172	    ssid->id);
173
174	HidlManager *hidl_manager = HidlManager::getInstance();
175	if (!hidl_manager)
176		return 1;
177
178	return hidl_manager->notifyNetworkRequest(
179	    wpa_s, ssid, rtype, default_txt);
180}
181
182void wpas_hidl_notify_anqp_query_done(
183    struct wpa_supplicant *wpa_s, const u8 *bssid, const char *result,
184    const struct wpa_bss_anqp *anqp)
185{
186	if (!wpa_s || !wpa_s->global->hidl || !bssid || !result || !anqp)
187		return;
188
189	wpa_printf(
190	    MSG_DEBUG,
191	    "Notifying ANQP query done to hidl control: " MACSTR "result: %s",
192	    MAC2STR(bssid), result);
193
194	HidlManager *hidl_manager = HidlManager::getInstance();
195	if (!hidl_manager)
196		return;
197
198	hidl_manager->notifyAnqpQueryDone(wpa_s, bssid, result, anqp);
199}
200
201void wpas_hidl_notify_hs20_icon_query_done(
202    struct wpa_supplicant *wpa_s, const u8 *bssid, const char *file_name,
203    const u8 *image, u32 image_length)
204{
205	if (!wpa_s || !wpa_s->global->hidl || !bssid || !file_name || !image)
206		return;
207
208	wpa_printf(
209	    MSG_DEBUG, "Notifying HS20 icon query done to hidl control: " MACSTR
210		       "file_name: %s",
211	    MAC2STR(bssid), file_name);
212
213	HidlManager *hidl_manager = HidlManager::getInstance();
214	if (!hidl_manager)
215		return;
216
217	hidl_manager->notifyHs20IconQueryDone(
218	    wpa_s, bssid, file_name, image, image_length);
219}
220
221void wpas_hidl_notify_hs20_rx_subscription_remediation(
222    struct wpa_supplicant *wpa_s, const char *url, u8 osu_method)
223{
224	if (!wpa_s || !wpa_s->global->hidl || !url)
225		return;
226
227	wpa_printf(
228	    MSG_DEBUG,
229	    "Notifying HS20 subscription remediation rx to hidl control: %s",
230	    url);
231
232	HidlManager *hidl_manager = HidlManager::getInstance();
233	if (!hidl_manager)
234		return;
235
236	hidl_manager->notifyHs20RxSubscriptionRemediation(
237	    wpa_s, url, osu_method);
238}
239
240void wpas_hidl_notify_hs20_rx_deauth_imminent_notice(
241    struct wpa_supplicant *wpa_s, u8 code, u16 reauth_delay, const char *url)
242{
243	if (!wpa_s || !wpa_s->global->hidl || !url)
244		return;
245
246	wpa_printf(
247	    MSG_DEBUG,
248	    "Notifying HS20 deauth imminent notice rx to hidl control: %s",
249	    url);
250
251	HidlManager *hidl_manager = HidlManager::getInstance();
252	if (!hidl_manager)
253		return;
254
255	hidl_manager->notifyHs20RxDeauthImminentNotice(
256	    wpa_s, code, reauth_delay, url);
257}
258
259void wpas_hidl_notify_disconnect_reason(struct wpa_supplicant *wpa_s)
260{
261	if (!wpa_s)
262		return;
263
264	wpa_printf(
265	    MSG_DEBUG, "Notifying disconnect reason to hidl control: %d",
266	    wpa_s->disconnect_reason);
267
268	HidlManager *hidl_manager = HidlManager::getInstance();
269	if (!hidl_manager)
270		return;
271
272	hidl_manager->notifyDisconnectReason(wpa_s);
273}
274
275void wpas_hidl_notify_assoc_reject(struct wpa_supplicant *wpa_s)
276{
277	if (!wpa_s)
278		return;
279
280	wpa_printf(
281	    MSG_DEBUG, "Notifying assoc reject to hidl control: %d",
282	    wpa_s->assoc_status_code);
283
284	HidlManager *hidl_manager = HidlManager::getInstance();
285	if (!hidl_manager)
286		return;
287
288	hidl_manager->notifyAssocReject(wpa_s);
289}
290
291void wpas_hidl_notify_auth_timeout(struct wpa_supplicant *wpa_s)
292{
293	if (!wpa_s)
294		return;
295
296	wpa_printf(MSG_DEBUG, "Notifying auth timeout to hidl control");
297
298	HidlManager *hidl_manager = HidlManager::getInstance();
299	if (!hidl_manager)
300		return;
301
302	hidl_manager->notifyAuthTimeout(wpa_s);
303}
304
305void wpas_hidl_notify_bssid_changed(struct wpa_supplicant *wpa_s)
306{
307	if (!wpa_s)
308		return;
309
310	wpa_printf(MSG_DEBUG, "Notifying bssid changed to hidl control");
311
312	HidlManager *hidl_manager = HidlManager::getInstance();
313	if (!hidl_manager)
314		return;
315
316	hidl_manager->notifyBssidChanged(wpa_s);
317}
318
319void wpas_hidl_notify_wps_event_fail(
320    struct wpa_supplicant *wpa_s, uint8_t *peer_macaddr, uint16_t config_error,
321    uint16_t error_indication)
322{
323	if (!wpa_s || !peer_macaddr)
324		return;
325
326	wpa_printf(
327	    MSG_DEBUG, "Notifying Wps event fail to hidl control: %d, %d",
328	    config_error, error_indication);
329
330	HidlManager *hidl_manager = HidlManager::getInstance();
331	if (!hidl_manager)
332		return;
333
334	hidl_manager->notifyWpsEventFail(
335	    wpa_s, peer_macaddr, config_error, error_indication);
336}
337
338void wpas_hidl_notify_wps_event_success(struct wpa_supplicant *wpa_s)
339{
340	if (!wpa_s)
341		return;
342
343	wpa_printf(MSG_DEBUG, "Notifying Wps event success to hidl control");
344
345	HidlManager *hidl_manager = HidlManager::getInstance();
346	if (!hidl_manager)
347		return;
348
349	hidl_manager->notifyWpsEventSuccess(wpa_s);
350}
351
352void wpas_hidl_notify_wps_event_pbc_overlap(struct wpa_supplicant *wpa_s)
353{
354	if (!wpa_s)
355		return;
356
357	wpa_printf(
358	    MSG_DEBUG, "Notifying Wps event PBC overlap to hidl control");
359
360	HidlManager *hidl_manager = HidlManager::getInstance();
361	if (!hidl_manager)
362		return;
363
364	hidl_manager->notifyWpsEventPbcOverlap(wpa_s);
365}
366
367void wpas_hidl_notify_p2p_device_found(
368    struct wpa_supplicant *wpa_s, const u8 *addr,
369    const struct p2p_peer_info *info, const u8 *peer_wfd_device_info,
370    u8 peer_wfd_device_info_len)
371{
372	if (!wpa_s || !addr || !info)
373		return;
374
375	wpa_printf(
376	    MSG_DEBUG, "Notifying P2P device found to hidl control " MACSTR,
377	    MAC2STR(info->p2p_device_addr));
378
379	HidlManager *hidl_manager = HidlManager::getInstance();
380	if (!hidl_manager)
381		return;
382
383	hidl_manager->notifyP2pDeviceFound(
384	    wpa_s, addr, info, peer_wfd_device_info, peer_wfd_device_info_len);
385}
386
387void wpas_hidl_notify_p2p_device_lost(
388    struct wpa_supplicant *wpa_s, const u8 *p2p_device_addr)
389{
390	if (!wpa_s || !p2p_device_addr)
391		return;
392
393	wpa_printf(
394	    MSG_DEBUG, "Notifying P2P device lost to hidl control " MACSTR,
395	    MAC2STR(p2p_device_addr));
396
397	HidlManager *hidl_manager = HidlManager::getInstance();
398	if (!hidl_manager)
399		return;
400
401	hidl_manager->notifyP2pDeviceLost(wpa_s, p2p_device_addr);
402}
403
404void wpas_hidl_notify_p2p_find_stopped(struct wpa_supplicant *wpa_s)
405{
406	if (!wpa_s)
407		return;
408
409	wpa_printf(MSG_DEBUG, "Notifying P2P find stop to hidl control");
410
411	HidlManager *hidl_manager = HidlManager::getInstance();
412	if (!hidl_manager)
413		return;
414
415	hidl_manager->notifyP2pFindStopped(wpa_s);
416}
417
418void wpas_hidl_notify_p2p_go_neg_req(
419    struct wpa_supplicant *wpa_s, const u8 *src_addr, u16 dev_passwd_id,
420    u8 go_intent)
421{
422	if (!wpa_s || !src_addr)
423		return;
424
425	wpa_printf(
426	    MSG_DEBUG,
427	    "Notifying P2P GO negotiation request to hidl control " MACSTR,
428	    MAC2STR(src_addr));
429
430	HidlManager *hidl_manager = HidlManager::getInstance();
431	if (!hidl_manager)
432		return;
433
434	hidl_manager->notifyP2pGoNegReq(
435	    wpa_s, src_addr, dev_passwd_id, go_intent);
436}
437
438void wpas_hidl_notify_p2p_go_neg_completed(
439    struct wpa_supplicant *wpa_s, const struct p2p_go_neg_results *res)
440{
441	if (!wpa_s || !res)
442		return;
443
444	wpa_printf(
445	    MSG_DEBUG,
446	    "Notifying P2P GO negotiation completed to hidl control: %d",
447	    res->status);
448
449	HidlManager *hidl_manager = HidlManager::getInstance();
450	if (!hidl_manager)
451		return;
452
453	hidl_manager->notifyP2pGoNegCompleted(wpa_s, res);
454}
455
456void wpas_hidl_notify_p2p_group_formation_failure(
457    struct wpa_supplicant *wpa_s, const char *reason)
458{
459	if (!wpa_s || !reason)
460		return;
461
462	wpa_printf(
463	    MSG_DEBUG,
464	    "Notifying P2P Group formation failure to hidl control: %s",
465	    reason);
466
467	HidlManager *hidl_manager = HidlManager::getInstance();
468	if (!hidl_manager)
469		return;
470
471	hidl_manager->notifyP2pGroupFormationFailure(wpa_s, reason);
472}
473
474void wpas_hidl_notify_p2p_group_started(
475    struct wpa_supplicant *wpa_s, const struct wpa_ssid *ssid, int persistent,
476    int client)
477{
478	if (!wpa_s || !ssid)
479		return;
480
481	wpa_printf(
482	    MSG_DEBUG, "Notifying P2P Group start to hidl control: %d",
483	    ssid->id);
484
485	HidlManager *hidl_manager = HidlManager::getInstance();
486	if (!hidl_manager)
487		return;
488
489	hidl_manager->notifyP2pGroupStarted(wpa_s, ssid, persistent, client);
490}
491
492void wpas_hidl_notify_p2p_group_removed(
493    struct wpa_supplicant *wpa_s, const struct wpa_ssid *ssid, const char *role)
494{
495	if (!wpa_s || !ssid || !role)
496		return;
497
498	wpa_printf(
499	    MSG_DEBUG, "Notifying P2P Group removed to hidl control: %d",
500	    ssid->id);
501
502	HidlManager *hidl_manager = HidlManager::getInstance();
503	if (!hidl_manager)
504		return;
505
506	hidl_manager->notifyP2pGroupRemoved(wpa_s, ssid, role);
507}
508
509void wpas_hidl_notify_p2p_invitation_received(
510    struct wpa_supplicant *wpa_s, const u8 *sa, const u8 *go_dev_addr,
511    const u8 *bssid, int id, int op_freq)
512{
513	if (!wpa_s || !sa || !go_dev_addr || !bssid)
514		return;
515
516	wpa_printf(
517	    MSG_DEBUG,
518	    "Notifying P2P invitation received to hidl control: %d " MACSTR, id,
519	    MAC2STR(bssid));
520
521	HidlManager *hidl_manager = HidlManager::getInstance();
522	if (!hidl_manager)
523		return;
524
525	hidl_manager->notifyP2pInvitationReceived(
526	    wpa_s, sa, go_dev_addr, bssid, id, op_freq);
527}
528
529void wpas_hidl_notify_p2p_invitation_result(
530    struct wpa_supplicant *wpa_s, int status, const u8 *bssid)
531{
532	if (!wpa_s)
533		return;
534	if (bssid) {
535		wpa_printf(
536			MSG_DEBUG,
537			"Notifying P2P invitation result to hidl control: " MACSTR,
538			MAC2STR(bssid));
539	} else {
540		wpa_printf(
541			MSG_DEBUG,
542			"Notifying P2P invitation result to hidl control: NULL bssid");
543	}
544
545	HidlManager *hidl_manager = HidlManager::getInstance();
546	if (!hidl_manager)
547		return;
548
549	hidl_manager->notifyP2pInvitationResult(wpa_s, status, bssid);
550}
551
552void wpas_hidl_notify_p2p_provision_discovery(
553    struct wpa_supplicant *wpa_s, const u8 *dev_addr, int request,
554    enum p2p_prov_disc_status status, u16 config_methods,
555    unsigned int generated_pin)
556{
557	if (!wpa_s || !dev_addr)
558		return;
559
560	wpa_printf(
561	    MSG_DEBUG,
562	    "Notifying P2P provision discovery to hidl control " MACSTR,
563	    MAC2STR(dev_addr));
564
565	HidlManager *hidl_manager = HidlManager::getInstance();
566	if (!hidl_manager)
567		return;
568
569	hidl_manager->notifyP2pProvisionDiscovery(
570	    wpa_s, dev_addr, request, status, config_methods, generated_pin);
571}
572
573void wpas_hidl_notify_p2p_sd_response(
574    struct wpa_supplicant *wpa_s, const u8 *sa, u16 update_indic,
575    const u8 *tlvs, size_t tlvs_len)
576{
577	if (!wpa_s || !sa || !tlvs)
578		return;
579
580	wpa_printf(
581	    MSG_DEBUG,
582	    "Notifying P2P service discovery response to hidl control " MACSTR,
583	    MAC2STR(sa));
584
585	HidlManager *hidl_manager = HidlManager::getInstance();
586	if (!hidl_manager)
587		return;
588
589	hidl_manager->notifyP2pSdResponse(
590	    wpa_s, sa, update_indic, tlvs, tlvs_len);
591}
592
593void wpas_hidl_notify_ap_sta_authorized(
594    struct wpa_supplicant *wpa_s, const u8 *sta, const u8 *p2p_dev_addr)
595{
596	if (!wpa_s || !sta)
597		return;
598
599	wpa_printf(
600	    MSG_DEBUG,
601	    "Notifying P2P AP STA authorized to hidl control " MACSTR,
602	    MAC2STR(sta));
603
604	HidlManager *hidl_manager = HidlManager::getInstance();
605	if (!hidl_manager)
606		return;
607
608	hidl_manager->notifyApStaAuthorized(wpa_s, sta, p2p_dev_addr);
609}
610
611void wpas_hidl_notify_ap_sta_deauthorized(
612    struct wpa_supplicant *wpa_s, const u8 *sta, const u8 *p2p_dev_addr)
613{
614	if (!wpa_s || !sta)
615		return;
616
617	wpa_printf(
618	    MSG_DEBUG,
619	    "Notifying P2P AP STA deauthorized to hidl control " MACSTR,
620	    MAC2STR(sta));
621
622	HidlManager *hidl_manager = HidlManager::getInstance();
623	if (!hidl_manager)
624		return;
625
626	hidl_manager->notifyApStaDeauthorized(wpa_s, sta, p2p_dev_addr);
627}
628