5 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31 #include <sys/ioctl.h>
32 #include <sys/socket.h>
33 #include <linux/if_arp.h>
34 #include <linux/wireless.h>
35 #include <net/ethernet.h>
38 #define IFF_LOWER_UP 0x10000
41 #include <dbus/dbus.h>
44 #define CONNMAN_API_SUBJECT_TO_CHANGE
45 #include <connman/plugin.h>
46 #include <connman/inet.h>
47 #include <connman/device.h>
48 #include <connman/rtnl.h>
49 #include <connman/technology.h>
50 #include <connman/log.h>
51 #include <connman/option.h>
52 #include <connman/storage.h>
54 #include <gsupplicant/gsupplicant.h>
56 #define CLEANUP_TIMEOUT 8 /* in seconds */
57 #define INACTIVE_TIMEOUT 12 /* in seconds */
58 #define MAXIMUM_RETRIES 4
60 struct connman_technology *wifi_technology = NULL;
64 struct connman_device *device;
65 struct connman_network *network;
66 struct connman_network *pending_network;
68 GSupplicantInterface *interface;
69 GSupplicantState state;
70 connman_bool_t connected;
71 connman_bool_t disconnecting;
72 connman_bool_t tethering;
73 connman_bool_t bridged;
81 static GList *iface_list = NULL;
83 static void handle_tethering(struct wifi_data *wifi)
85 if (wifi->tethering == FALSE)
88 if (wifi->bridge == NULL)
91 if (wifi->bridged == TRUE)
94 DBG("index %d bridge %s", wifi->index, wifi->bridge);
96 if (connman_inet_add_to_bridge(wifi->index, wifi->bridge) < 0)
102 static void wifi_newlink(unsigned flags, unsigned change, void *user_data)
104 struct connman_device *device = user_data;
105 struct wifi_data *wifi = connman_device_get_data(device);
107 DBG("index %d flags %d change %d", wifi->index, flags, change);
112 if ((wifi->flags & IFF_UP) != (flags & IFF_UP)) {
116 DBG("interface down");
119 if ((wifi->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
120 if (flags & IFF_LOWER_UP) {
123 handle_tethering(wifi);
131 static int wifi_probe(struct connman_device *device)
133 struct wifi_data *wifi;
135 DBG("device %p", device);
137 wifi = g_try_new0(struct wifi_data, 1);
141 wifi->connected = FALSE;
142 wifi->disconnecting = FALSE;
143 wifi->tethering = FALSE;
144 wifi->bridged = FALSE;
146 wifi->state = G_SUPPLICANT_STATE_INACTIVE;
148 connman_device_set_data(device, wifi);
149 wifi->device = connman_device_ref(device);
151 wifi->index = connman_device_get_index(device);
154 wifi->watch = connman_rtnl_add_newlink_watch(wifi->index,
155 wifi_newlink, device);
157 iface_list = g_list_append(iface_list, wifi);
162 static void remove_networks(struct connman_device *device,
163 struct wifi_data *wifi)
167 for (list = wifi->networks; list != NULL; list = list->next) {
168 struct connman_network *network = list->data;
170 connman_device_remove_network(device, network);
171 connman_network_unref(network);
174 g_slist_free(wifi->networks);
175 wifi->networks = NULL;
178 static void wifi_remove(struct connman_device *device)
180 struct wifi_data *wifi = connman_device_get_data(device);
182 DBG("device %p wifi %p", device, wifi);
187 iface_list = g_list_remove(iface_list, wifi);
189 remove_networks(device, wifi);
191 connman_device_set_data(device, NULL);
192 connman_device_unref(wifi->device);
193 connman_rtnl_remove_watch(wifi->watch);
195 g_supplicant_interface_set_data(wifi->interface, NULL);
197 g_free(wifi->identifier);
201 static void interface_create_callback(int result,
202 GSupplicantInterface *interface,
205 struct wifi_data *wifi = user_data;
207 DBG("result %d ifname %s, wifi %p", result,
208 g_supplicant_interface_get_ifname(interface),
211 if (result < 0 || wifi == NULL)
214 wifi->interface = interface;
215 g_supplicant_interface_set_data(interface, wifi);
217 if (g_supplicant_interface_get_ready(interface) == FALSE)
220 DBG("interface is ready wifi %p tethering %d", wifi, wifi->tethering);
222 if (wifi->device == NULL) {
223 connman_error("WiFi device not set");
227 connman_device_set_powered(wifi->device, TRUE);
230 static void interface_remove_callback(int result,
231 GSupplicantInterface *interface,
234 struct wifi_data *wifi;
236 wifi = g_supplicant_interface_get_data(interface);
238 DBG("result %d wifi %p", result, wifi);
240 if (result < 0 || wifi == NULL)
243 wifi->interface = NULL;
247 static int wifi_enable(struct connman_device *device)
249 struct wifi_data *wifi = connman_device_get_data(device);
250 const char *interface = connman_device_get_string(device, "Interface");
251 const char *driver = connman_option_get_string("wifi");
254 DBG("device %p %p", device, wifi);
256 ret = g_supplicant_interface_create(interface, driver, NULL,
257 interface_create_callback,
265 static int wifi_disable(struct connman_device *device)
267 struct wifi_data *wifi = connman_device_get_data(device);
270 DBG("device %p", device);
272 wifi->connected = FALSE;
273 wifi->disconnecting = FALSE;
275 if (wifi->pending_network != NULL)
276 wifi->pending_network = NULL;
278 remove_networks(device, wifi);
280 ret = g_supplicant_interface_remove(wifi->interface,
281 interface_remove_callback,
289 static void scan_callback(int result, GSupplicantInterface *interface,
292 struct connman_device *device = user_data;
294 DBG("result %d", result);
297 connman_device_reset_scanning(device);
299 connman_device_set_scanning(device, FALSE);
300 connman_device_unref(device);
303 static int add_scan_param(gchar *hex_ssid, int freq,
304 GSupplicantScanParams *scan_data,
305 int driver_max_scan_ssids)
309 if (driver_max_scan_ssids > scan_data->num_ssids && hex_ssid != NULL) {
311 unsigned int j = 0, hex;
312 size_t hex_ssid_len = strlen(hex_ssid);
314 ssid = g_try_malloc0(hex_ssid_len / 2);
318 for (i = 0; i < hex_ssid_len; i += 2) {
319 sscanf(hex_ssid + i, "%02x", &hex);
323 memcpy(scan_data->ssids[scan_data->num_ssids].ssid, ssid, j);
324 scan_data->ssids[scan_data->num_ssids].ssid_len = j;
325 scan_data->num_ssids++;
330 /* Don't add duplicate entries */
331 for (i = 0; i < G_SUPPLICANT_MAX_FAST_SCAN; i++) {
332 if (scan_data->freqs[i] == 0) {
333 scan_data->freqs[i] = freq;
335 } else if (scan_data->freqs[i] == freq)
342 struct last_connected {
348 static gint sort_entry(gconstpointer a, gconstpointer b, gpointer user_data)
350 GTimeVal *aval = (GTimeVal *)a;
351 GTimeVal *bval = (GTimeVal *)b;
353 /* Note that the sort order is descending */
354 if (aval->tv_sec < bval->tv_sec)
357 if (aval->tv_sec > bval->tv_sec)
363 static void free_entry(gpointer data)
365 struct last_connected *entry = data;
371 static int get_latest_connections(int max_ssids,
372 GSupplicantScanParams *scan_data)
375 GSequence *latest_list;
376 struct last_connected *entry;
385 latest_list = g_sequence_new(free_entry);
386 if (latest_list == NULL)
389 services = connman_storage_get_services();
390 for (i = 0; services && services[i]; i++) {
391 if (strncmp(services[i], "wifi_", 5) != 0)
394 keyfile = connman_storage_load_service(services[i]);
396 str = g_key_file_get_string(keyfile,
397 services[i], "Favorite", NULL);
398 if (str == NULL || g_strcmp0(str, "true")) {
401 g_key_file_free(keyfile);
406 str = g_key_file_get_string(keyfile,
407 services[i], "AutoConnect", NULL);
408 if (str == NULL || g_strcmp0(str, "true")) {
411 g_key_file_free(keyfile);
416 str = g_key_file_get_string(keyfile,
417 services[i], "Modified", NULL);
419 g_time_val_from_iso8601(str, &modified);
423 ssid = g_key_file_get_string(keyfile,
424 services[i], "SSID", NULL);
426 freq = g_key_file_get_integer(keyfile, services[i],
429 entry = g_try_new(struct last_connected, 1);
431 g_sequence_free(latest_list);
432 g_key_file_free(keyfile);
438 entry->modified = modified;
441 g_sequence_insert_sorted(latest_list, entry,
447 g_key_file_free(keyfile);
450 g_strfreev(services);
452 num_ssids = num_ssids > G_SUPPLICANT_MAX_FAST_SCAN ?
453 G_SUPPLICANT_MAX_FAST_SCAN : num_ssids;
455 iter = g_sequence_get_begin_iter(latest_list);
457 for (i = 0; i < num_ssids; i++) {
458 entry = g_sequence_get(iter);
460 DBG("ssid %s freq %d modified %lu", entry->ssid, entry->freq,
461 entry->modified.tv_sec);
463 add_scan_param(entry->ssid, entry->freq, scan_data, max_ssids);
465 iter = g_sequence_iter_next(iter);
468 g_sequence_free(latest_list);
472 static int wifi_scan(struct connman_device *device)
474 struct wifi_data *wifi = connman_device_get_data(device);
477 DBG("device %p %p", device, wifi->interface);
479 if (wifi->tethering == TRUE)
482 connman_device_ref(device);
483 ret = g_supplicant_interface_scan(wifi->interface, NULL,
484 scan_callback, device);
486 connman_device_set_scanning(device, TRUE);
488 connman_device_unref(device);
493 static int wifi_scan_fast(struct connman_device *device)
495 struct wifi_data *wifi = connman_device_get_data(device);
496 GSupplicantScanParams *scan_params = NULL;
498 int driver_max_ssids = 0;
500 DBG("device %p %p", device, wifi->interface);
502 if (wifi->tethering == TRUE)
505 driver_max_ssids = g_supplicant_interface_get_max_scan_ssids(
507 DBG("max ssids %d", driver_max_ssids);
508 if (driver_max_ssids == 0)
509 return wifi_scan(device);
511 scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
512 if (scan_params == NULL)
515 ret = get_latest_connections(driver_max_ssids, scan_params);
518 return wifi_scan(device);
521 connman_device_ref(device);
522 ret = g_supplicant_interface_scan(wifi->interface, scan_params,
523 scan_callback, device);
525 connman_device_set_scanning(device, TRUE);
528 connman_device_unref(device);
534 static struct connman_device_driver wifi_ng_driver = {
536 .type = CONNMAN_DEVICE_TYPE_WIFI,
537 .priority = CONNMAN_DEVICE_PRIORITY_LOW,
539 .remove = wifi_remove,
540 .enable = wifi_enable,
541 .disable = wifi_disable,
543 .scan_fast = wifi_scan_fast,
546 static void system_ready(void)
550 if (connman_device_driver_register(&wifi_ng_driver) < 0)
551 connman_error("Failed to register WiFi driver");
554 static void system_killed(void)
558 connman_device_driver_unregister(&wifi_ng_driver);
561 static int network_probe(struct connman_network *network)
563 DBG("network %p", network);
568 static void network_remove(struct connman_network *network)
570 struct connman_device *device = connman_network_get_device(network);
571 struct wifi_data *wifi;
573 DBG("network %p", network);
575 wifi = connman_device_get_data(device);
579 if (wifi->network != network)
582 wifi->network = NULL;
585 static void connect_callback(int result, GSupplicantInterface *interface,
588 struct connman_network *network = user_data;
590 DBG("network %p result %d", network, result);
592 if (result == -ENOKEY) {
593 connman_network_set_error(network,
594 CONNMAN_NETWORK_ERROR_INVALID_KEY);
595 } else if (result < 0) {
596 connman_network_set_error(network,
597 CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
601 static GSupplicantSecurity network_security(const char *security)
603 if (g_str_equal(security, "none") == TRUE)
604 return G_SUPPLICANT_SECURITY_NONE;
605 else if (g_str_equal(security, "wep") == TRUE)
606 return G_SUPPLICANT_SECURITY_WEP;
607 else if (g_str_equal(security, "psk") == TRUE)
608 return G_SUPPLICANT_SECURITY_PSK;
609 else if (g_str_equal(security, "wpa") == TRUE)
610 return G_SUPPLICANT_SECURITY_PSK;
611 else if (g_str_equal(security, "rsn") == TRUE)
612 return G_SUPPLICANT_SECURITY_PSK;
613 else if (g_str_equal(security, "ieee8021x") == TRUE)
614 return G_SUPPLICANT_SECURITY_IEEE8021X;
616 return G_SUPPLICANT_SECURITY_UNKNOWN;
619 static void ssid_init(GSupplicantSSID *ssid, struct connman_network *network)
621 const char *security, *passphrase, *agent_passphrase;
623 memset(ssid, 0, sizeof(*ssid));
624 ssid->mode = G_SUPPLICANT_MODE_INFRA;
625 ssid->ssid = connman_network_get_blob(network, "WiFi.SSID",
628 security = connman_network_get_string(network, "WiFi.Security");
629 ssid->security = network_security(security);
630 passphrase = connman_network_get_string(network,
632 if (passphrase == NULL || strlen(passphrase) == 0) {
634 /* Use agent provided passphrase as a fallback */
635 agent_passphrase = connman_network_get_string(network,
636 "WiFi.AgentPassphrase");
638 if (agent_passphrase == NULL || strlen(agent_passphrase) == 0)
639 ssid->passphrase = NULL;
641 ssid->passphrase = agent_passphrase;
643 ssid->passphrase = passphrase;
645 ssid->eap = connman_network_get_string(network, "WiFi.EAP");
648 * If our private key password is unset,
649 * we use the supplied passphrase. That is needed
650 * for PEAP where 2 passphrases (identity and client
651 * cert may have to be provided.
653 if (connman_network_get_string(network,
654 "WiFi.PrivateKeyPassphrase") == NULL)
655 connman_network_set_string(network,
656 "WiFi.PrivateKeyPassphrase",
658 /* We must have an identity for both PEAP and TLS */
659 ssid->identity = connman_network_get_string(network, "WiFi.Identity");
661 /* Use agent provided identity as a fallback */
662 if (ssid->identity == NULL || strlen(ssid->identity) == 0)
663 ssid->identity = connman_network_get_string(network,
664 "WiFi.AgentIdentity");
666 ssid->ca_cert_path = connman_network_get_string(network,
668 ssid->client_cert_path = connman_network_get_string(network,
669 "WiFi.ClientCertFile");
670 ssid->private_key_path = connman_network_get_string(network,
671 "WiFi.PrivateKeyFile");
672 ssid->private_key_passphrase = connman_network_get_string(network,
673 "WiFi.PrivateKeyPassphrase");
674 ssid->phase2_auth = connman_network_get_string(network, "WiFi.Phase2");
676 ssid->use_wps = connman_network_get_bool(network, "WiFi.UseWPS");
677 ssid->pin_wps = connman_network_get_string(network, "WiFi.PinWPS");
681 static int network_connect(struct connman_network *network)
683 struct connman_device *device = connman_network_get_device(network);
684 struct wifi_data *wifi;
685 GSupplicantInterface *interface;
686 GSupplicantSSID *ssid;
688 DBG("network %p", network);
693 wifi = connman_device_get_data(device);
697 ssid = g_try_malloc0(sizeof(GSupplicantSSID));
701 interface = wifi->interface;
703 ssid_init(ssid, network);
705 if (wifi->disconnecting == TRUE)
706 wifi->pending_network = network;
708 wifi->network = network;
711 return g_supplicant_interface_connect(interface, ssid,
712 connect_callback, network);
718 static void disconnect_callback(int result, GSupplicantInterface *interface,
721 struct wifi_data *wifi = user_data;
723 if (wifi->network != NULL) {
725 * if result < 0 supplican return an error because
726 * the network is not current.
727 * we wont receive G_SUPPLICANT_STATE_DISCONNECTED since it
728 * failed, call connman_network_set_connected to report
729 * disconnect is completed.
732 connman_network_set_connected(wifi->network, FALSE);
735 wifi->network = NULL;
737 wifi->disconnecting = FALSE;
739 if (wifi->pending_network != NULL) {
740 network_connect(wifi->pending_network);
741 wifi->pending_network = NULL;
746 static int network_disconnect(struct connman_network *network)
748 struct connman_device *device = connman_network_get_device(network);
749 struct wifi_data *wifi;
752 DBG("network %p", network);
754 wifi = connman_device_get_data(device);
755 if (wifi == NULL || wifi->interface == NULL)
758 connman_network_set_associating(network, FALSE);
760 if (wifi->disconnecting == TRUE)
763 wifi->disconnecting = TRUE;
765 err = g_supplicant_interface_disconnect(wifi->interface,
766 disconnect_callback, wifi);
768 wifi->disconnecting = FALSE;
773 static struct connman_network_driver network_driver = {
775 .type = CONNMAN_NETWORK_TYPE_WIFI,
776 .priority = CONNMAN_NETWORK_PRIORITY_LOW,
777 .probe = network_probe,
778 .remove = network_remove,
779 .connect = network_connect,
780 .disconnect = network_disconnect,
783 static void interface_added(GSupplicantInterface *interface)
785 const char *ifname = g_supplicant_interface_get_ifname(interface);
786 const char *driver = g_supplicant_interface_get_driver(interface);
787 struct wifi_data *wifi;
789 wifi = g_supplicant_interface_get_data(interface);
792 * We can get here with a NULL wifi pointer when
793 * the interface added signal is sent before the
794 * interface creation callback is called.
799 DBG("ifname %s driver %s wifi %p tethering %d",
800 ifname, driver, wifi, wifi->tethering);
802 if (wifi->device == NULL) {
803 connman_error("WiFi device not set");
807 connman_device_set_powered(wifi->device, TRUE);
809 if (wifi->tethering == TRUE)
813 static connman_bool_t is_idle(struct wifi_data *wifi)
815 DBG("state %d", wifi->state);
817 switch (wifi->state) {
818 case G_SUPPLICANT_STATE_UNKNOWN:
819 case G_SUPPLICANT_STATE_DISCONNECTED:
820 case G_SUPPLICANT_STATE_INACTIVE:
821 case G_SUPPLICANT_STATE_SCANNING:
824 case G_SUPPLICANT_STATE_AUTHENTICATING:
825 case G_SUPPLICANT_STATE_ASSOCIATING:
826 case G_SUPPLICANT_STATE_ASSOCIATED:
827 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
828 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
829 case G_SUPPLICANT_STATE_COMPLETED:
836 static connman_bool_t is_idle_wps(GSupplicantInterface *interface,
837 struct wifi_data *wifi)
839 /* First, let's check if WPS processing did not went wrong */
840 if (g_supplicant_interface_get_wps_state(interface) ==
841 G_SUPPLICANT_WPS_STATE_FAIL)
844 /* Unlike normal connection, being associated while processing wps
845 * actually means that we are idling. */
846 switch (wifi->state) {
847 case G_SUPPLICANT_STATE_UNKNOWN:
848 case G_SUPPLICANT_STATE_DISCONNECTED:
849 case G_SUPPLICANT_STATE_INACTIVE:
850 case G_SUPPLICANT_STATE_SCANNING:
851 case G_SUPPLICANT_STATE_ASSOCIATED:
853 case G_SUPPLICANT_STATE_AUTHENTICATING:
854 case G_SUPPLICANT_STATE_ASSOCIATING:
855 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
856 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
857 case G_SUPPLICANT_STATE_COMPLETED:
864 static connman_bool_t handle_wps_completion(GSupplicantInterface *interface,
865 struct connman_network *network,
866 struct connman_device *device,
867 struct wifi_data *wifi)
871 wps = connman_network_get_bool(network, "WiFi.UseWPS");
873 const unsigned char *ssid, *wps_ssid;
874 unsigned int ssid_len, wps_ssid_len;
877 /* Checking if we got associated with requested
879 ssid = connman_network_get_blob(network, "WiFi.SSID",
882 wps_ssid = g_supplicant_interface_get_wps_ssid(
883 interface, &wps_ssid_len);
885 if (wps_ssid == NULL || wps_ssid_len != ssid_len ||
886 memcmp(ssid, wps_ssid, ssid_len) != 0) {
887 connman_network_set_associating(network, FALSE);
888 g_supplicant_interface_disconnect(wifi->interface,
889 disconnect_callback, wifi);
893 wps_key = g_supplicant_interface_get_wps_key(interface);
894 connman_network_set_string(network, "WiFi.Passphrase",
897 connman_network_set_string(network, "WiFi.PinWPS", NULL);
903 static connman_bool_t handle_4way_handshake_failure(GSupplicantInterface *interface,
904 struct connman_network *network,
905 struct wifi_data *wifi)
907 if (wifi->state != G_SUPPLICANT_STATE_4WAY_HANDSHAKE)
912 if (wifi->retries < MAXIMUM_RETRIES)
915 /* We disable the selected network, if not then
916 * wpa_supplicant will loop retrying */
917 if (g_supplicant_interface_enable_selected_network(interface,
919 DBG("Could not disables selected network");
921 connman_network_set_error(network, CONNMAN_NETWORK_ERROR_INVALID_KEY);
926 static void interface_state(GSupplicantInterface *interface)
928 struct connman_network *network;
929 struct connman_device *device;
930 struct wifi_data *wifi;
931 GSupplicantState state = g_supplicant_interface_get_state(interface);
934 wifi = g_supplicant_interface_get_data(interface);
936 DBG("wifi %p interface state %d", wifi, state);
941 network = wifi->network;
942 device = wifi->device;
944 if (device == NULL || network == NULL)
948 case G_SUPPLICANT_STATE_SCANNING:
951 case G_SUPPLICANT_STATE_AUTHENTICATING:
952 case G_SUPPLICANT_STATE_ASSOCIATING:
953 connman_network_set_associating(network, TRUE);
956 case G_SUPPLICANT_STATE_COMPLETED:
957 if (handle_wps_completion(interface, network, device, wifi) ==
961 /* reset scan trigger and schedule background scan */
962 connman_device_schedule_scan(device);
964 connman_network_set_connected(network, TRUE);
967 case G_SUPPLICANT_STATE_DISCONNECTED:
969 * If we're in one of the idle modes, we have
970 * not started association yet and thus setting
971 * those ones to FALSE could cancel an association
974 wps = connman_network_get_bool(network, "WiFi.UseWPS");
976 if (is_idle_wps(interface, wifi) == TRUE)
982 /* If previous state was 4way-handshake, then
983 * it's either: psk was incorrect and thus we retry
984 * or if we reach the maximum retries we declare the
986 if (handle_4way_handshake_failure(interface,
987 network, wifi) == TRUE)
990 connman_network_set_associating(network, FALSE);
991 connman_network_set_connected(network, FALSE);
994 case G_SUPPLICANT_STATE_INACTIVE:
995 connman_network_set_associating(network, FALSE);
998 case G_SUPPLICANT_STATE_UNKNOWN:
999 case G_SUPPLICANT_STATE_ASSOCIATED:
1000 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
1001 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
1005 wifi->state = state;
1010 static void interface_removed(GSupplicantInterface *interface)
1012 const char *ifname = g_supplicant_interface_get_ifname(interface);
1013 struct wifi_data *wifi;
1015 DBG("ifname %s", ifname);
1017 wifi = g_supplicant_interface_get_data(interface);
1019 if (wifi != NULL && wifi->tethering == TRUE)
1022 if (wifi == NULL || wifi->device == NULL) {
1023 connman_error("Wrong wifi pointer");
1027 connman_device_set_powered(wifi->device, FALSE);
1030 static void scan_started(GSupplicantInterface *interface)
1035 static void scan_finished(GSupplicantInterface *interface)
1040 static unsigned char calculate_strength(GSupplicantNetwork *supplicant_network)
1042 unsigned char strength;
1044 strength = 120 + g_supplicant_network_get_signal(supplicant_network);
1051 static void network_added(GSupplicantNetwork *supplicant_network)
1053 struct connman_network *network;
1054 GSupplicantInterface *interface;
1055 struct wifi_data *wifi;
1056 const char *name, *identifier, *security, *group;
1057 const unsigned char *ssid;
1058 unsigned int ssid_len;
1063 interface = g_supplicant_network_get_interface(supplicant_network);
1064 wifi = g_supplicant_interface_get_data(interface);
1065 name = g_supplicant_network_get_name(supplicant_network);
1066 identifier = g_supplicant_network_get_identifier(supplicant_network);
1067 security = g_supplicant_network_get_security(supplicant_network);
1068 group = g_supplicant_network_get_identifier(supplicant_network);
1069 wps = g_supplicant_network_get_wps(supplicant_network);
1074 ssid = g_supplicant_network_get_ssid(supplicant_network, &ssid_len);
1076 network = connman_device_get_network(wifi->device, identifier);
1078 if (network == NULL) {
1079 network = connman_network_create(identifier,
1080 CONNMAN_NETWORK_TYPE_WIFI);
1081 if (network == NULL)
1084 connman_network_set_index(network, wifi->index);
1086 if (connman_device_add_network(wifi->device, network) < 0) {
1087 connman_network_unref(network);
1091 wifi->networks = g_slist_append(wifi->networks, network);
1094 if (name != NULL && name[0] != '\0')
1095 connman_network_set_name(network, name);
1097 connman_network_set_blob(network, "WiFi.SSID",
1099 connman_network_set_string(network, "WiFi.Security", security);
1100 connman_network_set_strength(network,
1101 calculate_strength(supplicant_network));
1102 connman_network_set_bool(network, "WiFi.WPS", wps);
1104 connman_network_set_frequency(network,
1105 g_supplicant_network_get_frequency(supplicant_network));
1107 connman_network_set_available(network, TRUE);
1110 connman_network_set_group(network, group);
1113 static void network_removed(GSupplicantNetwork *network)
1115 GSupplicantInterface *interface;
1116 struct wifi_data *wifi;
1117 const char *name, *identifier;
1118 struct connman_network *connman_network;
1120 interface = g_supplicant_network_get_interface(network);
1121 wifi = g_supplicant_interface_get_data(interface);
1122 identifier = g_supplicant_network_get_identifier(network);
1123 name = g_supplicant_network_get_name(network);
1125 DBG("name %s", name);
1130 connman_network = connman_device_get_network(wifi->device, identifier);
1131 if (connman_network == NULL)
1134 wifi->networks = g_slist_remove(wifi->networks, connman_network);
1136 connman_device_remove_network(wifi->device, connman_network);
1137 connman_network_unref(connman_network);
1140 static void network_changed(GSupplicantNetwork *network, const char *property)
1142 GSupplicantInterface *interface;
1143 struct wifi_data *wifi;
1144 const char *name, *identifier;
1145 struct connman_network *connman_network;
1147 interface = g_supplicant_network_get_interface(network);
1148 wifi = g_supplicant_interface_get_data(interface);
1149 identifier = g_supplicant_network_get_identifier(network);
1150 name = g_supplicant_network_get_name(network);
1152 DBG("name %s", name);
1157 connman_network = connman_device_get_network(wifi->device, identifier);
1158 if (connman_network == NULL)
1161 if (g_str_equal(property, "Signal") == TRUE) {
1162 connman_network_set_strength(connman_network,
1163 calculate_strength(network));
1164 connman_network_update(connman_network);
1168 static void debug(const char *str)
1170 if (getenv("CONNMAN_SUPPLICANT_DEBUG"))
1171 connman_debug("%s", str);
1174 static const GSupplicantCallbacks callbacks = {
1175 .system_ready = system_ready,
1176 .system_killed = system_killed,
1177 .interface_added = interface_added,
1178 .interface_state = interface_state,
1179 .interface_removed = interface_removed,
1180 .scan_started = scan_started,
1181 .scan_finished = scan_finished,
1182 .network_added = network_added,
1183 .network_removed = network_removed,
1184 .network_changed = network_changed,
1189 static int tech_probe(struct connman_technology *technology)
1191 wifi_technology = technology;
1196 static void tech_remove(struct connman_technology *technology)
1198 wifi_technology = NULL;
1201 struct wifi_tethering_info {
1202 struct wifi_data *wifi;
1203 struct connman_technology *technology;
1205 GSupplicantSSID *ssid;
1208 static GSupplicantSSID *ssid_ap_init(const char *ssid, const char *passphrase)
1210 GSupplicantSSID *ap;
1212 ap = g_try_malloc0(sizeof(GSupplicantSSID));
1216 ap->mode = G_SUPPLICANT_MODE_MASTER;
1218 ap->ssid_len = strlen(ssid);
1222 if (passphrase == NULL || strlen(passphrase) == 0) {
1223 ap->security = G_SUPPLICANT_SECURITY_NONE;
1224 ap->passphrase = NULL;
1226 ap->security = G_SUPPLICANT_SECURITY_PSK;
1227 ap->protocol = G_SUPPLICANT_PROTO_RSN;
1228 ap->pairwise_cipher = G_SUPPLICANT_PAIRWISE_CCMP;
1229 ap->group_cipher = G_SUPPLICANT_GROUP_CCMP;
1230 ap->passphrase = passphrase;
1236 static void ap_start_callback(int result, GSupplicantInterface *interface,
1239 struct wifi_tethering_info *info = user_data;
1241 DBG("result %d index %d bridge %s",
1242 result, info->wifi->index, info->wifi->bridge);
1245 connman_inet_remove_from_bridge(info->wifi->index,
1246 info->wifi->bridge);
1247 connman_technology_tethering_notify(info->technology, FALSE);
1250 g_free(info->ifname);
1254 static void ap_create_callback(int result,
1255 GSupplicantInterface *interface,
1258 struct wifi_tethering_info *info = user_data;
1260 DBG("result %d ifname %s", result,
1261 g_supplicant_interface_get_ifname(interface));
1264 connman_inet_remove_from_bridge(info->wifi->index,
1265 info->wifi->bridge);
1266 connman_technology_tethering_notify(info->technology, FALSE);
1268 g_free(info->ifname);
1273 info->wifi->interface = interface;
1274 g_supplicant_interface_set_data(interface, info->wifi);
1276 if (g_supplicant_interface_set_apscan(interface, 2) < 0)
1277 connman_error("Failed to set interface ap_scan property");
1279 g_supplicant_interface_connect(interface, info->ssid,
1280 ap_start_callback, info);
1283 static void sta_remove_callback(int result,
1284 GSupplicantInterface *interface,
1287 struct wifi_tethering_info *info = user_data;
1288 const char *driver = connman_option_get_string("wifi");
1290 DBG("ifname %s result %d ", info->ifname, result);
1293 info->wifi->tethering = TRUE;
1295 g_free(info->ifname);
1300 info->wifi->interface = NULL;
1302 connman_technology_tethering_notify(info->technology, TRUE);
1304 g_supplicant_interface_create(info->ifname, driver, info->wifi->bridge,
1309 static int tech_set_tethering(struct connman_technology *technology,
1310 const char *identifier, const char *passphrase,
1311 const char *bridge, connman_bool_t enabled)
1314 GSupplicantInterface *interface;
1315 struct wifi_data *wifi;
1316 struct wifi_tethering_info *info;
1323 if (enabled == FALSE) {
1324 for (list = iface_list; list; list = list->next) {
1327 if (wifi->tethering == TRUE) {
1328 wifi->tethering = FALSE;
1330 connman_inet_remove_from_bridge(wifi->index,
1332 wifi->bridged = FALSE;
1336 connman_technology_tethering_notify(technology, FALSE);
1341 for (list = iface_list; list; list = list->next) {
1344 interface = wifi->interface;
1346 if (interface == NULL)
1349 ifname = g_supplicant_interface_get_ifname(wifi->interface);
1351 mode = g_supplicant_interface_get_mode(interface);
1352 if ((mode & G_SUPPLICANT_CAPABILITY_MODE_AP) == 0) {
1353 DBG("%s does not support AP mode", ifname);
1357 info = g_try_malloc0(sizeof(struct wifi_tethering_info));
1362 info->technology = technology;
1363 info->wifi->bridge = bridge;
1364 info->ssid = ssid_ap_init(identifier, passphrase);
1365 if (info->ssid == NULL) {
1369 info->ifname = g_strdup(ifname);
1370 if (info->ifname == NULL) {
1375 info->wifi->tethering = TRUE;
1377 err = g_supplicant_interface_remove(interface,
1378 sta_remove_callback,
1387 static void regdom_callback(void *user_data)
1389 char *alpha2 = user_data;
1393 if (wifi_technology == NULL)
1396 connman_technology_regdom_notify(wifi_technology, alpha2);
1399 static int tech_set_regdom(struct connman_technology *technology, const char *alpha2)
1401 return g_supplicant_set_country(alpha2, regdom_callback, alpha2);
1404 static struct connman_technology_driver tech_driver = {
1406 .type = CONNMAN_SERVICE_TYPE_WIFI,
1407 .probe = tech_probe,
1408 .remove = tech_remove,
1409 .set_tethering = tech_set_tethering,
1410 .set_regdom = tech_set_regdom,
1413 static int wifi_init(void)
1417 err = connman_network_driver_register(&network_driver);
1421 err = g_supplicant_register(&callbacks);
1423 connman_network_driver_unregister(&network_driver);
1427 err = connman_technology_driver_register(&tech_driver);
1429 g_supplicant_unregister(&callbacks);
1430 connman_network_driver_unregister(&network_driver);
1437 static void wifi_exit(void)
1441 connman_technology_driver_unregister(&tech_driver);
1443 g_supplicant_unregister(&callbacks);
1445 connman_network_driver_unregister(&network_driver);
1448 CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
1449 CONNMAN_PLUGIN_PRIORITY_DEFAULT, wifi_init, wifi_exit)