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 */
59 struct connman_technology *wifi_technology = NULL;
63 struct connman_device *device;
64 struct connman_network *network;
65 struct connman_network *pending_network;
67 GSupplicantInterface *interface;
68 GSupplicantState state;
69 connman_bool_t connected;
70 connman_bool_t disconnecting;
71 connman_bool_t tethering;
72 connman_bool_t bridged;
79 static GList *iface_list = NULL;
81 static void handle_tethering(struct wifi_data *wifi)
83 if (wifi->tethering == FALSE)
86 if (wifi->bridge == NULL)
89 if (wifi->bridged == TRUE)
92 DBG("index %d bridge %s", wifi->index, wifi->bridge);
94 if (connman_inet_add_to_bridge(wifi->index, wifi->bridge) < 0)
100 static void wifi_newlink(unsigned flags, unsigned change, void *user_data)
102 struct connman_device *device = user_data;
103 struct wifi_data *wifi = connman_device_get_data(device);
105 DBG("index %d flags %d change %d", wifi->index, flags, change);
110 if ((wifi->flags & IFF_UP) != (flags & IFF_UP)) {
114 DBG("interface down");
117 if ((wifi->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
118 if (flags & IFF_LOWER_UP) {
121 handle_tethering(wifi);
129 static int wifi_probe(struct connman_device *device)
131 struct wifi_data *wifi;
133 DBG("device %p", device);
135 wifi = g_try_new0(struct wifi_data, 1);
139 wifi->connected = FALSE;
140 wifi->disconnecting = FALSE;
141 wifi->tethering = FALSE;
142 wifi->bridged = FALSE;
144 wifi->state = G_SUPPLICANT_STATE_INACTIVE;
146 connman_device_set_data(device, wifi);
147 wifi->device = connman_device_ref(device);
149 wifi->index = connman_device_get_index(device);
152 wifi->watch = connman_rtnl_add_newlink_watch(wifi->index,
153 wifi_newlink, device);
155 iface_list = g_list_append(iface_list, wifi);
160 static void remove_networks(struct connman_device *device,
161 struct wifi_data *wifi)
165 for (list = wifi->networks; list != NULL; list = list->next) {
166 struct connman_network *network = list->data;
168 connman_device_remove_network(device, network);
169 connman_network_unref(network);
172 g_slist_free(wifi->networks);
173 wifi->networks = NULL;
176 static void wifi_remove(struct connman_device *device)
178 struct wifi_data *wifi = connman_device_get_data(device);
180 DBG("device %p", device);
185 iface_list = g_list_remove(iface_list, wifi);
187 remove_networks(device, wifi);
189 connman_device_set_data(device, NULL);
190 connman_device_unref(wifi->device);
191 connman_rtnl_remove_watch(wifi->watch);
193 g_supplicant_interface_set_data(wifi->interface, NULL);
195 g_free(wifi->identifier);
199 static void interface_create_callback(int result,
200 GSupplicantInterface *interface,
203 struct wifi_data *wifi = user_data;
205 DBG("result %d ifname %s, wifi %p", result,
206 g_supplicant_interface_get_ifname(interface),
209 if (result < 0 || wifi == NULL)
212 wifi->interface = interface;
213 g_supplicant_interface_set_data(interface, wifi);
216 static void interface_remove_callback(int result,
217 GSupplicantInterface *interface,
220 struct wifi_data *wifi = user_data;
222 DBG("result %d wifi %p", result, wifi);
224 if (result < 0 || wifi == NULL)
227 wifi->interface = NULL;
231 static int wifi_enable(struct connman_device *device)
233 struct wifi_data *wifi = connman_device_get_data(device);
234 const char *interface = connman_device_get_string(device, "Interface");
235 const char *driver = connman_option_get_string("wifi");
238 DBG("device %p %p", device, wifi);
240 ret = g_supplicant_interface_create(interface, driver, NULL,
241 interface_create_callback,
249 static int wifi_disable(struct connman_device *device)
251 struct wifi_data *wifi = connman_device_get_data(device);
254 DBG("device %p", device);
256 wifi->connected = FALSE;
257 wifi->disconnecting = FALSE;
259 if (wifi->pending_network != NULL)
260 wifi->pending_network = NULL;
262 remove_networks(device, wifi);
264 ret = g_supplicant_interface_remove(wifi->interface,
265 interface_remove_callback,
273 static void scan_callback(int result, GSupplicantInterface *interface,
276 struct connman_device *device = user_data;
278 DBG("result %d", result);
281 connman_device_reset_scanning(device);
283 connman_device_set_scanning(device, FALSE);
284 connman_device_unref(device);
287 static int add_scan_param(gchar *hex_ssid, int freq,
288 GSupplicantScanParams *scan_data,
289 int driver_max_scan_ssids)
293 if (driver_max_scan_ssids > scan_data->num_ssids && hex_ssid != NULL) {
295 unsigned int j = 0, hex;
296 size_t hex_ssid_len = strlen(hex_ssid);
298 ssid = g_try_malloc0(hex_ssid_len / 2);
302 for (i = 0; i < hex_ssid_len; i += 2) {
303 sscanf(hex_ssid + i, "%02x", &hex);
307 memcpy(scan_data->ssids[scan_data->num_ssids].ssid, ssid, j);
308 scan_data->ssids[scan_data->num_ssids].ssid_len = j;
309 scan_data->num_ssids++;
314 /* Don't add duplicate entries */
315 for (i = 0; i < G_SUPPLICANT_MAX_FAST_SCAN; i++) {
316 if (scan_data->freqs[i] == 0) {
317 scan_data->freqs[i] = freq;
319 } else if (scan_data->freqs[i] == freq)
326 struct last_connected {
332 static gint sort_entry(gconstpointer a, gconstpointer b, gpointer user_data)
334 GTimeVal *aval = (GTimeVal *)a;
335 GTimeVal *bval = (GTimeVal *)b;
337 /* Note that the sort order is descending */
338 if (aval->tv_sec < bval->tv_sec)
341 if (aval->tv_sec > bval->tv_sec)
347 static void free_entry(gpointer data)
349 struct last_connected *entry = data;
355 static int get_latest_connections(int max_ssids,
356 GSupplicantScanParams *scan_data)
359 GSequence *latest_list;
360 struct last_connected *entry;
369 latest_list = g_sequence_new(free_entry);
370 if (latest_list == NULL)
373 services = connman_storage_get_services();
374 for (i = 0; services && services[i]; i++) {
375 keyfile = connman_storage_load_service(services[i]);
377 str = g_key_file_get_string(keyfile,
378 services[i], "Favorite", NULL);
379 if (str == NULL || g_strcmp0(str, "true")) {
382 g_key_file_free(keyfile);
387 str = g_key_file_get_string(keyfile,
388 services[i], "AutoConnect", NULL);
389 if (str == NULL || g_strcmp0(str, "true")) {
392 g_key_file_free(keyfile);
397 str = g_key_file_get_string(keyfile,
398 services[i], "Modified", NULL);
400 g_time_val_from_iso8601(str, &modified);
404 ssid = g_key_file_get_string(keyfile,
405 services[i], "SSID", NULL);
407 freq = g_key_file_get_integer(keyfile, services[i],
410 entry = g_try_new(struct last_connected, 1);
412 g_sequence_free(latest_list);
413 g_key_file_free(keyfile);
419 entry->modified = modified;
422 g_sequence_insert_sorted(latest_list, entry,
428 g_key_file_free(keyfile);
431 g_strfreev(services);
433 num_ssids = num_ssids > G_SUPPLICANT_MAX_FAST_SCAN ?
434 G_SUPPLICANT_MAX_FAST_SCAN : num_ssids;
436 iter = g_sequence_get_begin_iter(latest_list);
438 for (i = 0; i < num_ssids; i++) {
439 entry = g_sequence_get(iter);
441 DBG("ssid %s freq %d modified %lu", entry->ssid, entry->freq,
442 entry->modified.tv_sec);
444 add_scan_param(entry->ssid, entry->freq, scan_data, max_ssids);
446 iter = g_sequence_iter_next(iter);
449 g_sequence_free(latest_list);
453 static int wifi_scan(struct connman_device *device)
455 struct wifi_data *wifi = connman_device_get_data(device);
458 DBG("device %p %p", device, wifi->interface);
460 if (wifi->tethering == TRUE)
463 connman_device_ref(device);
464 ret = g_supplicant_interface_scan(wifi->interface, NULL,
465 scan_callback, device);
467 connman_device_set_scanning(device, TRUE);
469 connman_device_unref(device);
474 static int wifi_scan_fast(struct connman_device *device)
476 struct wifi_data *wifi = connman_device_get_data(device);
477 GSupplicantScanParams *scan_params = NULL;
479 int driver_max_ssids = 0;
481 DBG("device %p %p", device, wifi->interface);
483 if (wifi->tethering == TRUE)
486 driver_max_ssids = g_supplicant_interface_get_max_scan_ssids(
488 DBG("max ssids %d", driver_max_ssids);
489 if (driver_max_ssids == 0)
490 return wifi_scan(device);
492 scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
493 if (scan_params == NULL)
496 ret = get_latest_connections(driver_max_ssids, scan_params);
499 return wifi_scan(device);
502 connman_device_ref(device);
503 ret = g_supplicant_interface_scan(wifi->interface, scan_params,
504 scan_callback, device);
506 connman_device_set_scanning(device, TRUE);
509 connman_device_unref(device);
515 static struct connman_device_driver wifi_ng_driver = {
517 .type = CONNMAN_DEVICE_TYPE_WIFI,
518 .priority = CONNMAN_DEVICE_PRIORITY_LOW,
520 .remove = wifi_remove,
521 .enable = wifi_enable,
522 .disable = wifi_disable,
524 .scan_fast = wifi_scan_fast,
527 static void system_ready(void)
531 if (connman_device_driver_register(&wifi_ng_driver) < 0)
532 connman_error("Failed to register WiFi driver");
535 static void system_killed(void)
539 connman_device_driver_unregister(&wifi_ng_driver);
542 static int network_probe(struct connman_network *network)
544 DBG("network %p", network);
549 static void network_remove(struct connman_network *network)
551 struct connman_device *device = connman_network_get_device(network);
552 struct wifi_data *wifi;
554 DBG("network %p", network);
556 wifi = connman_device_get_data(device);
560 if (wifi->network != network)
563 wifi->network = NULL;
566 static void connect_callback(int result, GSupplicantInterface *interface,
569 struct connman_network *network = user_data;
571 DBG("network %p result %d", network, result);
573 if (result == -ENOKEY) {
574 connman_network_set_error(network,
575 CONNMAN_NETWORK_ERROR_INVALID_KEY);
576 } else if (result < 0) {
577 connman_network_set_error(network,
578 CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
582 static GSupplicantSecurity network_security(const char *security)
584 if (g_str_equal(security, "none") == TRUE)
585 return G_SUPPLICANT_SECURITY_NONE;
586 else if (g_str_equal(security, "wep") == TRUE)
587 return G_SUPPLICANT_SECURITY_WEP;
588 else if (g_str_equal(security, "psk") == TRUE)
589 return G_SUPPLICANT_SECURITY_PSK;
590 else if (g_str_equal(security, "wpa") == TRUE)
591 return G_SUPPLICANT_SECURITY_PSK;
592 else if (g_str_equal(security, "rsn") == TRUE)
593 return G_SUPPLICANT_SECURITY_PSK;
594 else if (g_str_equal(security, "ieee8021x") == TRUE)
595 return G_SUPPLICANT_SECURITY_IEEE8021X;
597 return G_SUPPLICANT_SECURITY_UNKNOWN;
600 static void ssid_init(GSupplicantSSID *ssid, struct connman_network *network)
602 const char *security, *passphrase, *agent_passphrase;
604 memset(ssid, 0, sizeof(*ssid));
605 ssid->mode = G_SUPPLICANT_MODE_INFRA;
606 ssid->ssid = connman_network_get_blob(network, "WiFi.SSID",
609 security = connman_network_get_string(network, "WiFi.Security");
610 ssid->security = network_security(security);
611 passphrase = connman_network_get_string(network,
613 if (passphrase == NULL || strlen(passphrase) == 0) {
615 /* Use agent provided passphrase as a fallback */
616 agent_passphrase = connman_network_get_string(network,
617 "WiFi.AgentPassphrase");
619 if (agent_passphrase == NULL || strlen(agent_passphrase) == 0)
620 ssid->passphrase = NULL;
622 ssid->passphrase = agent_passphrase;
624 ssid->passphrase = passphrase;
626 ssid->eap = connman_network_get_string(network, "WiFi.EAP");
629 * If our private key password is unset,
630 * we use the supplied passphrase. That is needed
631 * for PEAP where 2 passphrases (identity and client
632 * cert may have to be provided.
634 if (connman_network_get_string(network,
635 "WiFi.PrivateKeyPassphrase") == NULL)
636 connman_network_set_string(network,
637 "WiFi.PrivateKeyPassphrase",
639 /* We must have an identity for both PEAP and TLS */
640 ssid->identity = connman_network_get_string(network, "WiFi.Identity");
642 /* Use agent provided identity as a fallback */
643 if (ssid->identity == NULL || strlen(ssid->identity) == 0)
644 ssid->identity = connman_network_get_string(network,
645 "WiFi.AgentIdentity");
647 ssid->ca_cert_path = connman_network_get_string(network,
649 ssid->client_cert_path = connman_network_get_string(network,
650 "WiFi.ClientCertFile");
651 ssid->private_key_path = connman_network_get_string(network,
652 "WiFi.PrivateKeyFile");
653 ssid->private_key_passphrase = connman_network_get_string(network,
654 "WiFi.PrivateKeyPassphrase");
655 ssid->phase2_auth = connman_network_get_string(network, "WiFi.Phase2");
657 ssid->use_wps = connman_network_get_bool(network, "WiFi.UseWPS");
658 ssid->pin_wps = connman_network_get_string(network, "WiFi.PinWPS");
662 static int network_connect(struct connman_network *network)
664 struct connman_device *device = connman_network_get_device(network);
665 struct wifi_data *wifi;
666 GSupplicantInterface *interface;
667 GSupplicantSSID *ssid;
669 DBG("network %p", network);
674 wifi = connman_device_get_data(device);
678 ssid = g_try_malloc0(sizeof(GSupplicantSSID));
682 interface = wifi->interface;
684 ssid_init(ssid, network);
686 if (wifi->disconnecting == TRUE)
687 wifi->pending_network = network;
689 wifi->network = network;
691 return g_supplicant_interface_connect(interface, ssid,
692 connect_callback, network);
698 static void disconnect_callback(int result, GSupplicantInterface *interface,
701 struct wifi_data *wifi = user_data;
703 if (wifi->network != NULL) {
705 * if result < 0 supplican return an error because
706 * the network is not current.
707 * we wont receive G_SUPPLICANT_STATE_DISCONNECTED since it
708 * failed, call connman_network_set_connected to report
709 * disconnect is completed.
712 connman_network_set_connected(wifi->network, FALSE);
715 wifi->network = NULL;
717 wifi->disconnecting = FALSE;
719 if (wifi->pending_network != NULL) {
720 network_connect(wifi->pending_network);
721 wifi->pending_network = NULL;
726 static int network_disconnect(struct connman_network *network)
728 struct connman_device *device = connman_network_get_device(network);
729 struct wifi_data *wifi;
732 DBG("network %p", network);
734 wifi = connman_device_get_data(device);
735 if (wifi == NULL || wifi->interface == NULL)
738 connman_network_set_associating(network, FALSE);
740 if (wifi->disconnecting == TRUE)
743 wifi->disconnecting = TRUE;
745 err = g_supplicant_interface_disconnect(wifi->interface,
746 disconnect_callback, wifi);
748 wifi->disconnecting = FALSE;
753 static struct connman_network_driver network_driver = {
755 .type = CONNMAN_NETWORK_TYPE_WIFI,
756 .priority = CONNMAN_NETWORK_PRIORITY_LOW,
757 .probe = network_probe,
758 .remove = network_remove,
759 .connect = network_connect,
760 .disconnect = network_disconnect,
763 static void interface_added(GSupplicantInterface *interface)
765 const char *ifname = g_supplicant_interface_get_ifname(interface);
766 const char *driver = g_supplicant_interface_get_driver(interface);
767 struct wifi_data *wifi;
769 wifi = g_supplicant_interface_get_data(interface);
772 * We can get here with a NULL wifi pointer when
773 * the interface added signal is sent before the
774 * interface creation callback is called.
779 DBG("ifname %s driver %s wifi %p tethering %d",
780 ifname, driver, wifi, wifi->tethering);
782 if (wifi->device == NULL) {
783 connman_error("WiFi device not set");
787 connman_device_set_powered(wifi->device, TRUE);
789 if (wifi->tethering == TRUE)
793 static connman_bool_t is_idle(struct wifi_data *wifi)
795 DBG("state %d", wifi->state);
797 switch (wifi->state) {
798 case G_SUPPLICANT_STATE_UNKNOWN:
799 case G_SUPPLICANT_STATE_DISCONNECTED:
800 case G_SUPPLICANT_STATE_INACTIVE:
801 case G_SUPPLICANT_STATE_SCANNING:
804 case G_SUPPLICANT_STATE_AUTHENTICATING:
805 case G_SUPPLICANT_STATE_ASSOCIATING:
806 case G_SUPPLICANT_STATE_ASSOCIATED:
807 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
808 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
809 case G_SUPPLICANT_STATE_COMPLETED:
816 static connman_bool_t is_idle_wps(GSupplicantInterface *interface,
817 struct wifi_data *wifi)
819 /* First, let's check if WPS processing did not went wrong */
820 if (g_supplicant_interface_get_wps_state(interface) ==
821 G_SUPPLICANT_WPS_STATE_FAIL)
824 /* Unlike normal connection, being associated while processing wps
825 * actually means that we are idling. */
826 switch (wifi->state) {
827 case G_SUPPLICANT_STATE_UNKNOWN:
828 case G_SUPPLICANT_STATE_DISCONNECTED:
829 case G_SUPPLICANT_STATE_INACTIVE:
830 case G_SUPPLICANT_STATE_SCANNING:
831 case G_SUPPLICANT_STATE_ASSOCIATED:
833 case G_SUPPLICANT_STATE_AUTHENTICATING:
834 case G_SUPPLICANT_STATE_ASSOCIATING:
835 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
836 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
837 case G_SUPPLICANT_STATE_COMPLETED:
844 static connman_bool_t handle_wps_completion(GSupplicantInterface *interface,
845 struct connman_network *network,
846 struct connman_device *device,
847 struct wifi_data *wifi)
851 wps = connman_network_get_bool(network, "WiFi.UseWPS");
853 const unsigned char *ssid, *wps_ssid;
854 unsigned int ssid_len, wps_ssid_len;
857 /* Checking if we got associated with requested
859 ssid = connman_network_get_blob(network, "WiFi.SSID",
862 wps_ssid = g_supplicant_interface_get_wps_ssid(
863 interface, &wps_ssid_len);
865 if (wps_ssid == NULL || wps_ssid_len != ssid_len ||
866 memcmp(ssid, wps_ssid, ssid_len) != 0) {
867 connman_network_set_associating(network, FALSE);
868 g_supplicant_interface_disconnect(wifi->interface,
869 disconnect_callback, wifi);
873 wps_key = g_supplicant_interface_get_wps_key(interface);
874 connman_network_set_string(network, "WiFi.Passphrase",
877 connman_network_set_string(network, "WiFi.PinWPS", NULL);
883 static void interface_state(GSupplicantInterface *interface)
885 struct connman_network *network;
886 struct connman_device *device;
887 struct wifi_data *wifi;
888 GSupplicantState state = g_supplicant_interface_get_state(interface);
891 wifi = g_supplicant_interface_get_data(interface);
893 DBG("wifi %p interface state %d", wifi, state);
898 network = wifi->network;
899 device = wifi->device;
901 if (device == NULL || network == NULL)
905 case G_SUPPLICANT_STATE_SCANNING:
908 case G_SUPPLICANT_STATE_AUTHENTICATING:
909 case G_SUPPLICANT_STATE_ASSOCIATING:
910 connman_network_set_associating(network, TRUE);
913 case G_SUPPLICANT_STATE_COMPLETED:
914 if (handle_wps_completion(interface, network, device, wifi) ==
918 /* reset scan trigger and schedule background scan */
919 connman_device_schedule_scan(device);
921 connman_network_set_connected(network, TRUE);
924 case G_SUPPLICANT_STATE_DISCONNECTED:
926 * If we're in one of the idle modes, we have
927 * not started association yet and thus setting
928 * those ones to FALSE could cancel an association
931 wps = connman_network_get_bool(network, "WiFi.UseWPS");
933 if (is_idle_wps(interface, wifi) == TRUE)
938 connman_network_set_associating(network, FALSE);
939 connman_network_set_connected(network, FALSE);
942 case G_SUPPLICANT_STATE_INACTIVE:
943 connman_network_set_associating(network, FALSE);
946 case G_SUPPLICANT_STATE_UNKNOWN:
947 case G_SUPPLICANT_STATE_ASSOCIATED:
948 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
949 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
958 static void interface_removed(GSupplicantInterface *interface)
960 const char *ifname = g_supplicant_interface_get_ifname(interface);
961 struct wifi_data *wifi;
963 DBG("ifname %s", ifname);
965 wifi = g_supplicant_interface_get_data(interface);
967 if (wifi != NULL && wifi->tethering == TRUE)
970 if (wifi == NULL || wifi->device == NULL) {
971 connman_error("Wrong wifi pointer");
975 connman_device_set_powered(wifi->device, FALSE);
978 static void scan_started(GSupplicantInterface *interface)
983 static void scan_finished(GSupplicantInterface *interface)
988 static unsigned char calculate_strength(GSupplicantNetwork *supplicant_network)
990 unsigned char strength;
992 strength = 120 + g_supplicant_network_get_signal(supplicant_network);
999 static void network_added(GSupplicantNetwork *supplicant_network)
1001 struct connman_network *network;
1002 GSupplicantInterface *interface;
1003 struct wifi_data *wifi;
1004 const char *name, *identifier, *security, *group;
1005 const unsigned char *ssid;
1006 unsigned int ssid_len;
1011 interface = g_supplicant_network_get_interface(supplicant_network);
1012 wifi = g_supplicant_interface_get_data(interface);
1013 name = g_supplicant_network_get_name(supplicant_network);
1014 identifier = g_supplicant_network_get_identifier(supplicant_network);
1015 security = g_supplicant_network_get_security(supplicant_network);
1016 group = g_supplicant_network_get_identifier(supplicant_network);
1017 wps = g_supplicant_network_get_wps(supplicant_network);
1022 ssid = g_supplicant_network_get_ssid(supplicant_network, &ssid_len);
1024 network = connman_device_get_network(wifi->device, identifier);
1026 if (network == NULL) {
1027 network = connman_network_create(identifier,
1028 CONNMAN_NETWORK_TYPE_WIFI);
1029 if (network == NULL)
1032 connman_network_set_index(network, wifi->index);
1034 if (connman_device_add_network(wifi->device, network) < 0) {
1035 connman_network_unref(network);
1039 wifi->networks = g_slist_append(wifi->networks, network);
1042 if (name != NULL && name[0] != '\0')
1043 connman_network_set_name(network, name);
1045 connman_network_set_blob(network, "WiFi.SSID",
1047 connman_network_set_string(network, "WiFi.Security", security);
1048 connman_network_set_strength(network,
1049 calculate_strength(supplicant_network));
1050 connman_network_set_bool(network, "WiFi.WPS", wps);
1052 connman_network_set_frequency(network,
1053 g_supplicant_network_get_frequency(supplicant_network));
1055 connman_network_set_available(network, TRUE);
1058 connman_network_set_group(network, group);
1061 static void network_removed(GSupplicantNetwork *network)
1063 GSupplicantInterface *interface;
1064 struct wifi_data *wifi;
1065 const char *name, *identifier;
1066 struct connman_network *connman_network;
1068 interface = g_supplicant_network_get_interface(network);
1069 wifi = g_supplicant_interface_get_data(interface);
1070 identifier = g_supplicant_network_get_identifier(network);
1071 name = g_supplicant_network_get_name(network);
1073 DBG("name %s", name);
1078 connman_network = connman_device_get_network(wifi->device, identifier);
1079 if (connman_network == NULL)
1082 wifi->networks = g_slist_remove(wifi->networks, connman_network);
1084 connman_device_remove_network(wifi->device, connman_network);
1085 connman_network_unref(connman_network);
1088 static void network_changed(GSupplicantNetwork *network, const char *property)
1090 GSupplicantInterface *interface;
1091 struct wifi_data *wifi;
1092 const char *name, *identifier;
1093 struct connman_network *connman_network;
1095 interface = g_supplicant_network_get_interface(network);
1096 wifi = g_supplicant_interface_get_data(interface);
1097 identifier = g_supplicant_network_get_identifier(network);
1098 name = g_supplicant_network_get_name(network);
1100 DBG("name %s", name);
1105 connman_network = connman_device_get_network(wifi->device, identifier);
1106 if (connman_network == NULL)
1109 if (g_str_equal(property, "Signal") == TRUE) {
1110 connman_network_set_strength(connman_network,
1111 calculate_strength(network));
1112 connman_network_update(connman_network);
1116 static void debug(const char *str)
1118 if (getenv("CONNMAN_SUPPLICANT_DEBUG"))
1119 connman_debug("%s", str);
1122 static const GSupplicantCallbacks callbacks = {
1123 .system_ready = system_ready,
1124 .system_killed = system_killed,
1125 .interface_added = interface_added,
1126 .interface_state = interface_state,
1127 .interface_removed = interface_removed,
1128 .scan_started = scan_started,
1129 .scan_finished = scan_finished,
1130 .network_added = network_added,
1131 .network_removed = network_removed,
1132 .network_changed = network_changed,
1137 static int tech_probe(struct connman_technology *technology)
1139 wifi_technology = technology;
1144 static void tech_remove(struct connman_technology *technology)
1146 wifi_technology = NULL;
1149 struct wifi_tethering_info {
1150 struct wifi_data *wifi;
1151 struct connman_technology *technology;
1153 GSupplicantSSID *ssid;
1156 static GSupplicantSSID *ssid_ap_init(const char *ssid, const char *passphrase)
1158 GSupplicantSSID *ap;
1160 ap = g_try_malloc0(sizeof(GSupplicantSSID));
1164 ap->mode = G_SUPPLICANT_MODE_MASTER;
1166 ap->ssid_len = strlen(ssid);
1170 if (passphrase == NULL || strlen(passphrase) == 0) {
1171 ap->security = G_SUPPLICANT_SECURITY_NONE;
1172 ap->passphrase = NULL;
1174 ap->security = G_SUPPLICANT_SECURITY_PSK;
1175 ap->protocol = G_SUPPLICANT_PROTO_RSN;
1176 ap->pairwise_cipher = G_SUPPLICANT_PAIRWISE_CCMP;
1177 ap->group_cipher = G_SUPPLICANT_GROUP_CCMP;
1178 ap->passphrase = passphrase;
1184 static void ap_start_callback(int result, GSupplicantInterface *interface,
1187 struct wifi_tethering_info *info = user_data;
1189 DBG("result %d index %d bridge %s",
1190 result, info->wifi->index, info->wifi->bridge);
1193 connman_inet_remove_from_bridge(info->wifi->index,
1194 info->wifi->bridge);
1195 connman_technology_tethering_notify(info->technology, FALSE);
1198 g_free(info->ifname);
1202 static void ap_create_callback(int result,
1203 GSupplicantInterface *interface,
1206 struct wifi_tethering_info *info = user_data;
1208 DBG("result %d ifname %s", result,
1209 g_supplicant_interface_get_ifname(interface));
1212 connman_inet_remove_from_bridge(info->wifi->index,
1213 info->wifi->bridge);
1214 connman_technology_tethering_notify(info->technology, FALSE);
1216 g_free(info->ifname);
1221 info->wifi->interface = interface;
1222 g_supplicant_interface_set_data(interface, info->wifi);
1224 if (g_supplicant_interface_set_apscan(interface, 2) < 0)
1225 connman_error("Failed to set interface ap_scan property");
1227 g_supplicant_interface_connect(interface, info->ssid,
1228 ap_start_callback, info);
1231 static void sta_remove_callback(int result,
1232 GSupplicantInterface *interface,
1235 struct wifi_tethering_info *info = user_data;
1236 const char *driver = connman_option_get_string("wifi");
1238 DBG("ifname %s result %d ", info->ifname, result);
1241 info->wifi->tethering = TRUE;
1243 g_free(info->ifname);
1248 info->wifi->interface = NULL;
1250 connman_technology_tethering_notify(info->technology, TRUE);
1252 g_supplicant_interface_create(info->ifname, driver, info->wifi->bridge,
1257 static int tech_set_tethering(struct connman_technology *technology,
1258 const char *identifier, const char *passphrase,
1259 const char *bridge, connman_bool_t enabled)
1262 GSupplicantInterface *interface;
1263 struct wifi_data *wifi;
1264 struct wifi_tethering_info *info;
1271 if (enabled == FALSE) {
1272 for (list = iface_list; list; list = list->next) {
1275 if (wifi->tethering == TRUE) {
1276 wifi->tethering = FALSE;
1278 connman_inet_remove_from_bridge(wifi->index,
1280 wifi->bridged = FALSE;
1284 connman_technology_tethering_notify(technology, FALSE);
1289 for (list = iface_list; list; list = list->next) {
1292 interface = wifi->interface;
1294 if (interface == NULL)
1297 ifname = g_supplicant_interface_get_ifname(wifi->interface);
1299 mode = g_supplicant_interface_get_mode(interface);
1300 if ((mode & G_SUPPLICANT_CAPABILITY_MODE_AP) == 0) {
1301 DBG("%s does not support AP mode", ifname);
1305 info = g_try_malloc0(sizeof(struct wifi_tethering_info));
1310 info->technology = technology;
1311 info->wifi->bridge = bridge;
1312 info->ssid = ssid_ap_init(identifier, passphrase);
1313 if (info->ssid == NULL) {
1317 info->ifname = g_strdup(ifname);
1318 if (info->ifname == NULL) {
1323 info->wifi->tethering = TRUE;
1325 err = g_supplicant_interface_remove(interface,
1326 sta_remove_callback,
1335 static void regdom_callback(void *user_data)
1337 char *alpha2 = user_data;
1341 if (wifi_technology == NULL)
1344 connman_technology_regdom_notify(wifi_technology, alpha2);
1347 static int tech_set_regdom(struct connman_technology *technology, const char *alpha2)
1349 return g_supplicant_set_country(alpha2, regdom_callback, alpha2);
1352 static struct connman_technology_driver tech_driver = {
1354 .type = CONNMAN_SERVICE_TYPE_WIFI,
1355 .probe = tech_probe,
1356 .remove = tech_remove,
1357 .set_tethering = tech_set_tethering,
1358 .set_regdom = tech_set_regdom,
1361 static int wifi_init(void)
1365 err = connman_network_driver_register(&network_driver);
1369 err = g_supplicant_register(&callbacks);
1371 connman_network_driver_unregister(&network_driver);
1375 err = connman_technology_driver_register(&tech_driver);
1377 g_supplicant_unregister(&callbacks);
1378 connman_network_driver_unregister(&network_driver);
1385 static void wifi_exit(void)
1389 connman_technology_driver_unregister(&tech_driver);
1391 g_supplicant_unregister(&callbacks);
1393 connman_network_driver_unregister(&network_driver);
1396 CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
1397 CONNMAN_PLUGIN_PRIORITY_DEFAULT, wifi_init, wifi_exit)