1f05cddf9SRui Paulo /*
2f05cddf9SRui Paulo  * WPA Supplicant - auto scan
3f05cddf9SRui Paulo  * Copyright (c) 2012, Intel Corporation. All rights reserved.
4780fb4a2SCy Schubert  * Copyright 2015	Intel Deutschland GmbH
5f05cddf9SRui Paulo  *
6f05cddf9SRui Paulo  * This software may be distributed under the terms of the BSD license.
7f05cddf9SRui Paulo  * See README for more details.
8f05cddf9SRui Paulo  */
9f05cddf9SRui Paulo 
10f05cddf9SRui Paulo #include "includes.h"
11f05cddf9SRui Paulo 
12f05cddf9SRui Paulo #include "common.h"
13f05cddf9SRui Paulo #include "config.h"
14f05cddf9SRui Paulo #include "wpa_supplicant_i.h"
15f05cddf9SRui Paulo #include "bss.h"
16f05cddf9SRui Paulo #include "scan.h"
17f05cddf9SRui Paulo #include "autoscan.h"
18f05cddf9SRui Paulo 
19f05cddf9SRui Paulo 
20f05cddf9SRui Paulo static const struct autoscan_ops * autoscan_modules[] = {
21f05cddf9SRui Paulo #ifdef CONFIG_AUTOSCAN_EXPONENTIAL
22f05cddf9SRui Paulo 	&autoscan_exponential_ops,
23f05cddf9SRui Paulo #endif /* CONFIG_AUTOSCAN_EXPONENTIAL */
24f05cddf9SRui Paulo #ifdef CONFIG_AUTOSCAN_PERIODIC
25f05cddf9SRui Paulo 	&autoscan_periodic_ops,
26f05cddf9SRui Paulo #endif /* CONFIG_AUTOSCAN_PERIODIC */
27f05cddf9SRui Paulo 	NULL
28f05cddf9SRui Paulo };
29f05cddf9SRui Paulo 
30f05cddf9SRui Paulo 
request_scan(struct wpa_supplicant * wpa_s)31f05cddf9SRui Paulo static void request_scan(struct wpa_supplicant *wpa_s)
32f05cddf9SRui Paulo {
33f05cddf9SRui Paulo 	wpa_s->scan_req = MANUAL_SCAN_REQ;
34f05cddf9SRui Paulo 
35f05cddf9SRui Paulo 	if (wpa_supplicant_req_sched_scan(wpa_s))
36f05cddf9SRui Paulo 		wpa_supplicant_req_scan(wpa_s, wpa_s->scan_interval, 0);
37f05cddf9SRui Paulo }
38f05cddf9SRui Paulo 
39f05cddf9SRui Paulo 
autoscan_init(struct wpa_supplicant * wpa_s,int req_scan)40f05cddf9SRui Paulo int autoscan_init(struct wpa_supplicant *wpa_s, int req_scan)
41f05cddf9SRui Paulo {
42f05cddf9SRui Paulo 	const char *name = wpa_s->conf->autoscan;
43f05cddf9SRui Paulo 	const char *params;
44f05cddf9SRui Paulo 	size_t nlen;
45f05cddf9SRui Paulo 	int i;
46f05cddf9SRui Paulo 	const struct autoscan_ops *ops = NULL;
47780fb4a2SCy Schubert 	struct sched_scan_plan *scan_plans;
48780fb4a2SCy Schubert 
49780fb4a2SCy Schubert 	/* Give preference to scheduled scan plans if supported/configured */
50*85732ac8SCy Schubert 	if (wpa_s->sched_scan_plans) {
51*85732ac8SCy Schubert 		wpa_printf(MSG_DEBUG,
52*85732ac8SCy Schubert 			   "autoscan: sched_scan_plans set - use it instead");
53780fb4a2SCy Schubert 		return 0;
54*85732ac8SCy Schubert 	}
55f05cddf9SRui Paulo 
56*85732ac8SCy Schubert 	if (wpa_s->autoscan && wpa_s->autoscan_priv) {
57*85732ac8SCy Schubert 		wpa_printf(MSG_DEBUG, "autoscan: Already initialized");
58f05cddf9SRui Paulo 		return 0;
59*85732ac8SCy Schubert 	}
60f05cddf9SRui Paulo 
61f05cddf9SRui Paulo 	if (name == NULL)
62f05cddf9SRui Paulo 		return 0;
63f05cddf9SRui Paulo 
64f05cddf9SRui Paulo 	params = os_strchr(name, ':');
65f05cddf9SRui Paulo 	if (params == NULL) {
66f05cddf9SRui Paulo 		params = "";
67f05cddf9SRui Paulo 		nlen = os_strlen(name);
68f05cddf9SRui Paulo 	} else {
69f05cddf9SRui Paulo 		nlen = params - name;
70f05cddf9SRui Paulo 		params++;
71f05cddf9SRui Paulo 	}
72f05cddf9SRui Paulo 
73f05cddf9SRui Paulo 	for (i = 0; autoscan_modules[i]; i++) {
74f05cddf9SRui Paulo 		if (os_strncmp(name, autoscan_modules[i]->name, nlen) == 0) {
75f05cddf9SRui Paulo 			ops = autoscan_modules[i];
76f05cddf9SRui Paulo 			break;
77f05cddf9SRui Paulo 		}
78f05cddf9SRui Paulo 	}
79f05cddf9SRui Paulo 
80f05cddf9SRui Paulo 	if (ops == NULL) {
81f05cddf9SRui Paulo 		wpa_printf(MSG_ERROR, "autoscan: Could not find module "
82f05cddf9SRui Paulo 			   "matching the parameter '%s'", name);
83f05cddf9SRui Paulo 		return -1;
84f05cddf9SRui Paulo 	}
85f05cddf9SRui Paulo 
86780fb4a2SCy Schubert 	scan_plans = os_malloc(sizeof(*wpa_s->sched_scan_plans));
87780fb4a2SCy Schubert 	if (!scan_plans)
88780fb4a2SCy Schubert 		return -1;
89780fb4a2SCy Schubert 
90f05cddf9SRui Paulo 	wpa_s->autoscan_params = NULL;
91f05cddf9SRui Paulo 
92f05cddf9SRui Paulo 	wpa_s->autoscan_priv = ops->init(wpa_s, params);
93780fb4a2SCy Schubert 	if (!wpa_s->autoscan_priv) {
94780fb4a2SCy Schubert 		os_free(scan_plans);
95f05cddf9SRui Paulo 		return -1;
96780fb4a2SCy Schubert 	}
97780fb4a2SCy Schubert 
98780fb4a2SCy Schubert 	scan_plans[0].interval = 5;
99780fb4a2SCy Schubert 	scan_plans[0].iterations = 0;
100780fb4a2SCy Schubert 	os_free(wpa_s->sched_scan_plans);
101780fb4a2SCy Schubert 	wpa_s->sched_scan_plans = scan_plans;
102780fb4a2SCy Schubert 	wpa_s->sched_scan_plans_num = 1;
103f05cddf9SRui Paulo 	wpa_s->autoscan = ops;
104f05cddf9SRui Paulo 
105f05cddf9SRui Paulo 	wpa_printf(MSG_DEBUG, "autoscan: Initialized module '%s' with "
106f05cddf9SRui Paulo 		   "parameters '%s'", ops->name, params);
107f05cddf9SRui Paulo 	if (!req_scan)
108f05cddf9SRui Paulo 		return 0;
109f05cddf9SRui Paulo 
110f05cddf9SRui Paulo 	/*
111f05cddf9SRui Paulo 	 * Cancelling existing scan requests, if any.
112f05cddf9SRui Paulo 	 */
113f05cddf9SRui Paulo 	wpa_supplicant_cancel_sched_scan(wpa_s);
114f05cddf9SRui Paulo 	wpa_supplicant_cancel_scan(wpa_s);
115f05cddf9SRui Paulo 
116f05cddf9SRui Paulo 	/*
117f05cddf9SRui Paulo 	 * Firing first scan, which will lead to call autoscan_notify_scan.
118f05cddf9SRui Paulo 	 */
119f05cddf9SRui Paulo 	request_scan(wpa_s);
120f05cddf9SRui Paulo 
121f05cddf9SRui Paulo 	return 0;
122f05cddf9SRui Paulo }
123f05cddf9SRui Paulo 
124f05cddf9SRui Paulo 
autoscan_deinit(struct wpa_supplicant * wpa_s)125f05cddf9SRui Paulo void autoscan_deinit(struct wpa_supplicant *wpa_s)
126f05cddf9SRui Paulo {
127f05cddf9SRui Paulo 	if (wpa_s->autoscan && wpa_s->autoscan_priv) {
128f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "autoscan: Deinitializing module '%s'",
129f05cddf9SRui Paulo 			   wpa_s->autoscan->name);
130f05cddf9SRui Paulo 		wpa_s->autoscan->deinit(wpa_s->autoscan_priv);
131f05cddf9SRui Paulo 		wpa_s->autoscan = NULL;
132f05cddf9SRui Paulo 		wpa_s->autoscan_priv = NULL;
133f05cddf9SRui Paulo 
134f05cddf9SRui Paulo 		wpa_s->scan_interval = 5;
135780fb4a2SCy Schubert 
136780fb4a2SCy Schubert 		os_free(wpa_s->sched_scan_plans);
137780fb4a2SCy Schubert 		wpa_s->sched_scan_plans = NULL;
138780fb4a2SCy Schubert 		wpa_s->sched_scan_plans_num = 0;
139f05cddf9SRui Paulo 	}
140f05cddf9SRui Paulo }
141f05cddf9SRui Paulo 
142f05cddf9SRui Paulo 
autoscan_notify_scan(struct wpa_supplicant * wpa_s,struct wpa_scan_results * scan_res)143f05cddf9SRui Paulo int autoscan_notify_scan(struct wpa_supplicant *wpa_s,
144f05cddf9SRui Paulo 			 struct wpa_scan_results *scan_res)
145f05cddf9SRui Paulo {
146f05cddf9SRui Paulo 	int interval;
147f05cddf9SRui Paulo 
148f05cddf9SRui Paulo 	if (wpa_s->autoscan && wpa_s->autoscan_priv) {
149f05cddf9SRui Paulo 		interval = wpa_s->autoscan->notify_scan(wpa_s->autoscan_priv,
150f05cddf9SRui Paulo 							scan_res);
151f05cddf9SRui Paulo 
152f05cddf9SRui Paulo 		if (interval <= 0)
153f05cddf9SRui Paulo 			return -1;
154f05cddf9SRui Paulo 
155f05cddf9SRui Paulo 		wpa_s->scan_interval = interval;
156780fb4a2SCy Schubert 		wpa_s->sched_scan_plans[0].interval = interval;
157f05cddf9SRui Paulo 
158f05cddf9SRui Paulo 		request_scan(wpa_s);
159f05cddf9SRui Paulo 	}
160f05cddf9SRui Paulo 
161f05cddf9SRui Paulo 	return 0;
162f05cddf9SRui Paulo }
163