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
30 #include <sys/ioctl.h>
31 #include <sys/socket.h>
32 #include <linux/if_arp.h>
33 #include <linux/wireless.h>
34 #include <net/ethernet.h>
37 #define IFF_LOWER_UP 0x10000
40 #include <dbus/dbus.h>
43 #define CONNMAN_API_SUBJECT_TO_CHANGE
44 #include <connman/plugin.h>
45 #include <connman/inet.h>
46 #include <connman/device.h>
47 #include <connman/rtnl.h>
48 #include <connman/technology.h>
49 #include <connman/log.h>
50 #include <connman/option.h>
52 #include <gsupplicant/gsupplicant.h>
54 #define CLEANUP_TIMEOUT 8 /* in seconds */
55 #define INACTIVE_TIMEOUT 12 /* in seconds */
57 struct connman_technology *wifi_technology = NULL;
61 struct connman_device *device;
62 struct connman_network *network;
63 GSupplicantInterface *interface;
64 connman_bool_t connected;
70 static int get_bssid(struct connman_device *device,
71 unsigned char *bssid, unsigned int *bssid_len)
78 ifindex = connman_device_get_index(device);
82 ifname = connman_inet_ifname(ifindex);
86 fd = socket(PF_INET, SOCK_DGRAM, 0);
92 memset(&wrq, 0, sizeof(wrq));
93 strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
95 err = ioctl(fd, SIOCGIWAP, &wrq);
103 memcpy(bssid, wrq.u.ap_addr.sa_data, ETH_ALEN);
104 *bssid_len = ETH_ALEN;
109 static void wifi_newlink(unsigned flags, unsigned change, void *user_data)
111 struct connman_device *device = user_data;
112 struct wifi_data *wifi = connman_device_get_data(device);
114 DBG("index %d flags %d change %d", wifi->index, flags, change);
119 if ((wifi->flags & IFF_UP) != (flags & IFF_UP)) {
123 DBG("interface down");
126 if ((wifi->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
127 if (flags & IFF_LOWER_UP)
136 static int wifi_probe(struct connman_device *device)
138 struct wifi_data *wifi;
140 DBG("device %p", device);
142 wifi = g_try_new0(struct wifi_data, 1);
146 wifi->connected = FALSE;
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);
160 static void wifi_remove(struct connman_device *device)
162 struct wifi_data *wifi = connman_device_get_data(device);
164 DBG("device %p", device);
166 connman_device_set_data(device, NULL);
167 connman_device_unref(wifi->device);
168 connman_rtnl_remove_watch(wifi->watch);
170 g_supplicant_interface_set_data(wifi->interface, NULL);
172 g_free(wifi->identifier);
176 static void interface_create_callback(int result,
177 GSupplicantInterface *interface,
180 struct wifi_data *wifi = user_data;
182 DBG("result %d ifname %s", result,
183 g_supplicant_interface_get_ifname(interface));
188 wifi->interface = interface;
189 g_supplicant_interface_set_data(interface, wifi);
192 static void interface_remove_callback(int result,
193 GSupplicantInterface *interface,
196 struct wifi_data *wifi = user_data;
198 DBG("result %d", result);
203 wifi->interface = NULL;
207 static int wifi_enable(struct connman_device *device)
209 struct wifi_data *wifi = connman_device_get_data(device);
210 const char *interface = connman_device_get_string(device, "Interface");
211 const char *driver = connman_option_get_string("wifi");
213 DBG("device %p %p", device, wifi);
215 return g_supplicant_interface_create(interface, driver,
216 interface_create_callback,
220 static int wifi_disable(struct connman_device *device)
222 struct wifi_data *wifi = connman_device_get_data(device);
224 DBG("device %p", device);
226 wifi->connected = FALSE;
228 return g_supplicant_interface_remove(wifi->interface,
229 interface_remove_callback,
233 static void scan_callback(int result, GSupplicantInterface *interface,
236 struct connman_device *device = user_data;
238 DBG("result %d", result);
240 connman_device_set_scanning(device, FALSE);
243 static int wifi_scan(struct connman_device *device)
245 struct wifi_data *wifi = connman_device_get_data(device);
247 DBG("device %p %p", device, wifi->interface);
249 return g_supplicant_interface_scan(wifi->interface, scan_callback,
253 static struct connman_device_driver wifi_ng_driver = {
255 .type = CONNMAN_DEVICE_TYPE_WIFI,
256 .priority = CONNMAN_DEVICE_PRIORITY_LOW,
258 .remove = wifi_remove,
259 .enable = wifi_enable,
260 .disable = wifi_disable,
264 static void system_ready(void)
268 if (connman_device_driver_register(&wifi_ng_driver) < 0)
269 connman_error("Failed to register WiFi driver");
272 static void system_killed(void)
276 connman_device_driver_unregister(&wifi_ng_driver);
279 static void interface_added(GSupplicantInterface *interface)
281 const char *ifname = g_supplicant_interface_get_ifname(interface);
282 const char *driver = g_supplicant_interface_get_driver(interface);
283 struct wifi_data *wifi;
285 wifi = g_supplicant_interface_get_data(interface);
287 DBG("ifname %s driver %s wifi %p", ifname, driver, wifi);
289 if (wifi == NULL || wifi->device == NULL) {
290 connman_error("Wrong wifi pointer");
294 connman_device_set_powered(wifi->device, TRUE);
295 wifi_scan(wifi->device);
298 static void interface_state(GSupplicantInterface *interface)
300 struct connman_network *network;
301 struct connman_device *device;
302 struct wifi_data *wifi;
303 GSupplicantState state = g_supplicant_interface_get_state(interface);
304 unsigned char bssid[ETH_ALEN];
305 unsigned int bssid_len;
307 wifi = g_supplicant_interface_get_data(interface);
309 DBG("wifi %p interface state %d", wifi, state);
314 network = wifi->network;
315 device = wifi->device;
317 if (device == NULL || network == NULL)
321 case G_SUPPLICANT_STATE_SCANNING:
322 connman_device_set_scanning(device, TRUE);
325 case G_SUPPLICANT_STATE_AUTHENTICATING:
326 case G_SUPPLICANT_STATE_ASSOCIATING:
327 connman_network_set_associating(network, TRUE);
330 case G_SUPPLICANT_STATE_COMPLETED:
331 /* reset scan trigger and schedule background scan */
332 connman_device_schedule_scan(device);
334 if (get_bssid(device, bssid, &bssid_len) == 0)
335 connman_network_set_address(network,
337 connman_network_set_connected(network, TRUE);
340 case G_SUPPLICANT_STATE_DISCONNECTED:
341 connman_network_set_associating(network, FALSE);
342 connman_network_set_connected(network, FALSE);
345 case G_SUPPLICANT_STATE_INACTIVE:
346 connman_network_set_associating(network, FALSE);
349 case G_SUPPLICANT_STATE_UNKNOWN:
350 case G_SUPPLICANT_STATE_ASSOCIATED:
351 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
352 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
359 static void interface_removed(GSupplicantInterface *interface)
361 const char *ifname = g_supplicant_interface_get_ifname(interface);
362 struct wifi_data *wifi;
364 DBG("ifname %s", ifname);
366 wifi = g_supplicant_interface_get_data(interface);
368 if (wifi == NULL || wifi->device == NULL) {
369 connman_error("Wrong wifi pointer");
373 connman_device_set_powered(wifi->device, FALSE);
376 static void scan_started(GSupplicantInterface *interface)
378 struct wifi_data *wifi;
382 wifi = g_supplicant_interface_get_data(interface);
388 connman_device_set_scanning(wifi->device, TRUE);
391 static void scan_finished(GSupplicantInterface *interface)
393 struct wifi_data *wifi;
397 wifi = g_supplicant_interface_get_data(interface);
403 static unsigned char calculate_strength(GSupplicantNetwork *supplicant_network)
405 unsigned char strength;
407 strength = 120 + g_supplicant_network_get_signal(supplicant_network);
414 static void network_added(GSupplicantNetwork *supplicant_network)
416 struct connman_network *network;
417 GSupplicantInterface *interface;
418 struct wifi_data *wifi;
419 const char *name, *identifier, *mode, *security, *group;
420 const unsigned char *ssid;
421 unsigned int ssid_len;
425 interface = g_supplicant_network_get_interface(supplicant_network);
426 wifi = g_supplicant_interface_get_data(interface);
427 name = g_supplicant_network_get_name(supplicant_network);
428 identifier = g_supplicant_network_get_identifier(supplicant_network);
429 mode = g_supplicant_network_get_mode(supplicant_network);
430 security = g_supplicant_network_get_security(supplicant_network);
431 group = g_supplicant_network_get_identifier(supplicant_network);
436 ssid = g_supplicant_network_get_ssid(supplicant_network, &ssid_len);
438 network = connman_device_get_network(wifi->device, identifier);
440 if (network == NULL) {
441 network = connman_network_create(identifier,
442 CONNMAN_NETWORK_TYPE_WIFI);
446 connman_network_set_index(network, wifi->index);
448 if (connman_device_add_network(wifi->device, network) < 0) {
449 connman_network_unref(network);
454 if (name != NULL && name[0] != '\0')
455 connman_network_set_name(network, name);
457 connman_network_set_blob(network, "WiFi.SSID",
459 connman_network_set_string(network, "WiFi.Mode", mode);
460 connman_network_set_string(network, "WiFi.Security", security);
461 connman_network_set_strength(network,
462 calculate_strength(supplicant_network));
464 connman_network_set_available(network, TRUE);
467 connman_network_set_group(network, group);
470 static void network_removed(GSupplicantNetwork *network)
472 const char *name = g_supplicant_network_get_name(network);
474 DBG("* name %s", name);
477 static void debug(const char *str)
479 if (getenv("CONNMAN_GSUPPLICANT_DEBUG"))
480 connman_debug("%s", str);
483 static const GSupplicantCallbacks callbacks = {
484 .system_ready = system_ready,
485 .system_killed = system_killed,
486 .interface_added = interface_added,
487 .interface_state = interface_state,
488 .interface_removed = interface_removed,
489 .scan_started = scan_started,
490 .scan_finished = scan_finished,
491 .network_added = network_added,
492 .network_removed = network_removed,
497 static int network_probe(struct connman_network *network)
499 DBG("network %p", network);
504 static void network_remove(struct connman_network *network)
506 DBG("network %p", network);
509 static void connect_callback(int result, GSupplicantInterface *interface,
512 connman_error("%s", __func__);
515 static void disconnect_callback(int result, GSupplicantInterface *interface,
518 struct wifi_data *wifi = user_data;
521 connman_error("%s", __func__);
525 connman_network_unref(wifi->network);
527 wifi->network = NULL;
531 static GSupplicantSecurity network_security(const char *security)
533 if (g_str_equal(security, "none") == TRUE)
534 return G_SUPPLICANT_SECURITY_NONE;
535 else if (g_str_equal(security, "wep") == TRUE)
536 return G_SUPPLICANT_SECURITY_WEP;
537 else if (g_str_equal(security, "psk") == TRUE)
538 return G_SUPPLICANT_SECURITY_PSK;
539 else if (g_str_equal(security, "wpa") == TRUE)
540 return G_SUPPLICANT_SECURITY_PSK;
541 else if (g_str_equal(security, "rsn") == TRUE)
542 return G_SUPPLICANT_SECURITY_PSK;
543 else if (g_str_equal(security, "ieee8021x") == TRUE)
544 return G_SUPPLICANT_SECURITY_IEEE8021X;
546 return G_SUPPLICANT_SECURITY_UNKNOWN;
549 static void ssid_init(GSupplicantSSID *ssid, struct connman_network *network)
551 const char *security;
553 memset(ssid, 0, sizeof(*ssid));
554 ssid->ssid = connman_network_get_blob(network, "WiFi.SSID",
556 security = connman_network_get_string(network, "WiFi.Security");
557 ssid->security = network_security(security);
558 ssid->passphrase = connman_network_get_string(network,
560 ssid->eap = connman_network_get_string(network, "WiFi.EAP");
563 * If our private key password is unset,
564 * we use the supplied passphrase. That is needed
565 * for PEAP where 2 passphrases (identity and client
566 * cert may have to be provided.
568 if (connman_network_get_string(network,
569 "WiFi.PrivateKeyPassphrase") == NULL)
570 connman_network_set_string(network,
571 "WiFi.PrivateKeyPassphrase",
573 /* We must have an identity for both PEAP and TLS */
574 ssid->identity = connman_network_get_string(network, "WiFi.Identity");
575 ssid->ca_cert_path = connman_network_get_string(network,
577 ssid->client_cert_path = connman_network_get_string(network,
578 "WiFi.ClientCertFile");
579 ssid->private_key_path = connman_network_get_string(network,
580 "WiFi.PrivateKeyFile");
581 ssid->private_key_passphrase = connman_network_get_string(network,
582 "WiFi.PrivateKeyPassphrase");
583 ssid->phase2_auth = connman_network_get_string(network, "WiFi.Phase2");
587 static int network_connect(struct connman_network *network)
589 struct connman_device *device = connman_network_get_device(network);
590 struct wifi_data *wifi;
591 GSupplicantInterface *interface;
592 GSupplicantSSID ssid;
594 DBG("network %p", network);
599 wifi = connman_device_get_data(device);
603 interface = wifi->interface;
605 ssid_init(&ssid, network);
607 wifi->network = connman_network_ref(network);
609 return g_supplicant_interface_connect(interface, &ssid,
610 connect_callback, NULL);
613 static int network_disconnect(struct connman_network *network)
615 struct connman_device *device = connman_network_get_device(network);
616 struct wifi_data *wifi;
618 DBG("network %p", network);
620 wifi = connman_device_get_data(device);
621 if (wifi == NULL || wifi->interface == NULL)
624 connman_network_set_associating(network, FALSE);
626 return g_supplicant_interface_disconnect(wifi->interface,
627 disconnect_callback, wifi);
630 static struct connman_network_driver network_driver = {
632 .type = CONNMAN_NETWORK_TYPE_WIFI,
633 .priority = CONNMAN_NETWORK_PRIORITY_LOW,
634 .probe = network_probe,
635 .remove = network_remove,
636 .connect = network_connect,
637 .disconnect = network_disconnect,
640 static int tech_probe(struct connman_technology *technology)
642 wifi_technology = technology;
647 static void tech_remove(struct connman_technology *technology)
649 wifi_technology = NULL;
652 static void regdom_callback(void *user_data)
654 char *alpha2 = user_data;
658 if (wifi_technology == NULL)
661 connman_technology_regdom_notify(wifi_technology, alpha2);
664 static int tech_set_regdom(struct connman_technology *technology, const char *alpha2)
666 return g_supplicant_set_country(alpha2, regdom_callback, alpha2);
669 static struct connman_technology_driver tech_driver = {
671 .type = CONNMAN_SERVICE_TYPE_WIFI,
673 .remove = tech_remove,
674 .set_regdom = tech_set_regdom,
677 static int wifi_init(void)
681 err = connman_network_driver_register(&network_driver);
685 err = g_supplicant_register(&callbacks);
687 connman_network_driver_unregister(&network_driver);
691 err = connman_technology_driver_register(&tech_driver);
693 g_supplicant_unregister(&callbacks);
694 connman_network_driver_unregister(&network_driver);
701 static void wifi_exit(void)
705 connman_technology_driver_unregister(&tech_driver);
707 g_supplicant_unregister(&callbacks);
709 connman_network_driver_unregister(&network_driver);
712 CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
713 CONNMAN_PLUGIN_PRIORITY_DEFAULT, wifi_init, wifi_exit)