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
29 #include <sys/ioctl.h>
30 #include <sys/socket.h>
31 #include <linux/if_arp.h>
32 #include <linux/wireless.h>
33 #include <net/ethernet.h>
36 #define IFF_LOWER_UP 0x10000
39 #include <dbus/dbus.h>
42 #define CONNMAN_API_SUBJECT_TO_CHANGE
43 #include <connman/plugin.h>
44 #include <connman/inet.h>
45 #include <connman/device.h>
46 #include <connman/rtnl.h>
47 #include <connman/technology.h>
48 #include <connman/log.h>
49 #include <connman/option.h>
51 #include <gsupplicant/gsupplicant.h>
53 #define CLEANUP_TIMEOUT 8 /* in seconds */
54 #define INACTIVE_TIMEOUT 12 /* in seconds */
56 struct connman_technology *wifi_technology = NULL;
60 struct connman_device *device;
61 struct connman_network *network;
62 GSupplicantInterface *interface;
63 connman_bool_t connected;
69 static int get_bssid(struct connman_device *device,
70 unsigned char *bssid, unsigned int *bssid_len)
77 ifindex = connman_device_get_index(device);
81 ifname = connman_inet_ifname(ifindex);
85 fd = socket(PF_INET, SOCK_DGRAM, 0);
91 memset(&wrq, 0, sizeof(wrq));
92 strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
94 err = ioctl(fd, SIOCGIWAP, &wrq);
102 memcpy(bssid, wrq.u.ap_addr.sa_data, ETH_ALEN);
103 *bssid_len = ETH_ALEN;
108 static void wifi_newlink(unsigned flags, unsigned change, void *user_data)
110 struct connman_device *device = user_data;
111 struct wifi_data *wifi = connman_device_get_data(device);
113 DBG("index %d flags %d change %d", wifi->index, flags, change);
118 if ((wifi->flags & IFF_UP) != (flags & IFF_UP)) {
122 DBG("interface down");
125 if ((wifi->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
126 if (flags & IFF_LOWER_UP)
135 static int wifi_probe(struct connman_device *device)
137 struct wifi_data *wifi;
139 DBG("device %p", device);
141 wifi = g_try_new0(struct wifi_data, 1);
145 wifi->connected = FALSE;
147 connman_device_set_data(device, wifi);
148 wifi->device = connman_device_ref(device);
150 wifi->index = connman_device_get_index(device);
153 wifi->watch = connman_rtnl_add_newlink_watch(wifi->index,
154 wifi_newlink, device);
159 static void wifi_remove(struct connman_device *device)
161 struct wifi_data *wifi = connman_device_get_data(device);
163 DBG("device %p", device);
165 connman_device_set_data(device, NULL);
166 connman_device_unref(wifi->device);
167 connman_rtnl_remove_watch(wifi->watch);
169 g_supplicant_interface_set_data(wifi->interface, NULL);
171 g_free(wifi->identifier);
175 static void interface_create_callback(int result,
176 GSupplicantInterface *interface,
179 struct wifi_data *wifi = user_data;
181 DBG("result %d ifname %s", result,
182 g_supplicant_interface_get_ifname(interface));
187 wifi->interface = interface;
188 g_supplicant_interface_set_data(interface, wifi);
191 static void interface_remove_callback(int result,
192 GSupplicantInterface *interface,
195 struct wifi_data *wifi = user_data;
197 DBG("result %d", result);
202 wifi->interface = NULL;
206 static int wifi_enable(struct connman_device *device)
208 struct wifi_data *wifi = connman_device_get_data(device);
209 const char *interface = connman_device_get_string(device, "Interface");
210 const char *driver = connman_option_get_string("wifi");
212 DBG("device %p %p", device, wifi);
214 return g_supplicant_interface_create(interface, driver,
215 interface_create_callback,
219 static int wifi_disable(struct connman_device *device)
221 struct wifi_data *wifi = connman_device_get_data(device);
223 DBG("device %p", device);
225 wifi->connected = FALSE;
227 return g_supplicant_interface_remove(wifi->interface,
228 interface_remove_callback,
232 static void scan_callback(int result, GSupplicantInterface *interface,
235 struct connman_device *device = user_data;
237 DBG("result %d", result);
239 connman_device_set_scanning(device, FALSE);
242 static int wifi_scan(struct connman_device *device)
244 struct wifi_data *wifi = connman_device_get_data(device);
246 DBG("device %p %p", device, wifi->interface);
248 return g_supplicant_interface_scan(wifi->interface, scan_callback,
252 static struct connman_device_driver wifi_ng_driver = {
254 .type = CONNMAN_DEVICE_TYPE_WIFI,
255 .priority = CONNMAN_DEVICE_PRIORITY_LOW,
257 .remove = wifi_remove,
258 .enable = wifi_enable,
259 .disable = wifi_disable,
263 static void system_ready(void)
267 if (connman_device_driver_register(&wifi_ng_driver) < 0)
268 connman_error("Failed to register WiFi driver");
271 static void system_killed(void)
275 connman_device_driver_unregister(&wifi_ng_driver);
278 static void interface_added(GSupplicantInterface *interface)
280 const char *ifname = g_supplicant_interface_get_ifname(interface);
281 const char *driver = g_supplicant_interface_get_driver(interface);
282 struct wifi_data *wifi;
284 wifi = g_supplicant_interface_get_data(interface);
286 DBG("ifname %s driver %s wifi %p", ifname, driver, wifi);
288 if (wifi == NULL || wifi->device == NULL) {
289 connman_error("Wrong wifi pointer");
293 connman_device_set_powered(wifi->device, TRUE);
294 wifi_scan(wifi->device);
297 static void interface_state(GSupplicantInterface *interface)
299 struct connman_network *network;
300 struct connman_device *device;
301 struct wifi_data *wifi;
302 GSupplicantState state = g_supplicant_interface_get_state(interface);
303 unsigned char bssid[ETH_ALEN];
304 unsigned int bssid_len;
306 wifi = g_supplicant_interface_get_data(interface);
308 DBG("wifi %p interface state %d", wifi, state);
313 network = wifi->network;
314 device = wifi->device;
316 if (device == NULL || network == NULL)
320 case G_SUPPLICANT_STATE_SCANNING:
321 connman_device_set_scanning(device, TRUE);
324 case G_SUPPLICANT_STATE_AUTHENTICATING:
325 case G_SUPPLICANT_STATE_ASSOCIATING:
326 connman_network_set_associating(network, TRUE);
329 case G_SUPPLICANT_STATE_COMPLETED:
330 /* reset scan trigger and schedule background scan */
331 connman_device_schedule_scan(device);
333 if (get_bssid(device, bssid, &bssid_len) == 0)
334 connman_network_set_address(network,
336 connman_network_set_connected(network, TRUE);
339 case G_SUPPLICANT_STATE_DISCONNECTED:
340 connman_network_set_associating(network, FALSE);
341 connman_network_set_connected(network, FALSE);
344 case G_SUPPLICANT_STATE_INACTIVE:
345 connman_network_set_associating(network, FALSE);
348 case G_SUPPLICANT_STATE_UNKNOWN:
349 case G_SUPPLICANT_STATE_ASSOCIATED:
350 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
351 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
358 static void interface_removed(GSupplicantInterface *interface)
360 const char *ifname = g_supplicant_interface_get_ifname(interface);
361 struct wifi_data *wifi;
363 DBG("ifname %s", ifname);
365 wifi = g_supplicant_interface_get_data(interface);
367 if (wifi == NULL || wifi->device == NULL) {
368 connman_error("Wrong wifi pointer");
372 connman_device_set_powered(wifi->device, FALSE);
375 static void scan_started(GSupplicantInterface *interface)
377 struct wifi_data *wifi;
381 wifi = g_supplicant_interface_get_data(interface);
387 connman_device_set_scanning(wifi->device, TRUE);
390 static void scan_finished(GSupplicantInterface *interface)
392 struct wifi_data *wifi;
396 wifi = g_supplicant_interface_get_data(interface);
402 static unsigned char calculate_strength(GSupplicantNetwork *supplicant_network)
404 unsigned char strength;
406 strength = 120 + g_supplicant_network_get_signal(supplicant_network);
413 static void network_added(GSupplicantNetwork *supplicant_network)
415 struct connman_network *network;
416 GSupplicantInterface *interface;
417 struct wifi_data *wifi;
418 const char *name, *identifier, *mode, *security, *group;
419 const unsigned char *ssid;
420 unsigned int ssid_len;
424 interface = g_supplicant_network_get_interface(supplicant_network);
425 wifi = g_supplicant_interface_get_data(interface);
426 name = g_supplicant_network_get_name(supplicant_network);
427 identifier = g_supplicant_network_get_identifier(supplicant_network);
428 mode = g_supplicant_network_get_mode(supplicant_network);
429 security = g_supplicant_network_get_security(supplicant_network);
430 group = g_supplicant_network_get_identifier(supplicant_network);
435 ssid = g_supplicant_network_get_ssid(supplicant_network, &ssid_len);
437 network = connman_device_get_network(wifi->device, identifier);
439 if (network == NULL) {
440 network = connman_network_create(identifier,
441 CONNMAN_NETWORK_TYPE_WIFI);
445 connman_network_set_index(network, wifi->index);
447 if (connman_device_add_network(wifi->device, network) < 0) {
448 connman_network_unref(network);
453 if (name != NULL && name[0] != '\0')
454 connman_network_set_name(network, name);
456 connman_network_set_blob(network, "WiFi.SSID",
458 connman_network_set_string(network, "WiFi.Mode", mode);
459 connman_network_set_string(network, "WiFi.Security", security);
460 connman_network_set_strength(network,
461 calculate_strength(supplicant_network));
463 connman_network_set_available(network, TRUE);
466 connman_network_set_group(network, group);
469 static void network_removed(GSupplicantNetwork *network)
471 const char *name = g_supplicant_network_get_name(network);
473 DBG("* name %s", name);
476 static void debug(const char *str)
478 connman_debug("%s", str);
481 static const GSupplicantCallbacks callbacks = {
482 .system_ready = system_ready,
483 .system_killed = system_killed,
484 .interface_added = interface_added,
485 .interface_state = interface_state,
486 .interface_removed = interface_removed,
487 .scan_started = scan_started,
488 .scan_finished = scan_finished,
489 .network_added = network_added,
490 .network_removed = network_removed,
495 static int network_probe(struct connman_network *network)
497 DBG("network %p", network);
502 static void network_remove(struct connman_network *network)
504 DBG("network %p", network);
507 static void connect_callback(int result, GSupplicantInterface *interface,
510 connman_error("%s", __func__);
513 static void disconnect_callback(int result, GSupplicantInterface *interface,
516 struct wifi_data *wifi = user_data;
519 connman_error("%s", __func__);
523 connman_network_unref(wifi->network);
525 wifi->network = NULL;
529 static GSupplicantSecurity network_security(const char *security)
531 if (g_str_equal(security, "none") == TRUE)
532 return G_SUPPLICANT_SECURITY_NONE;
533 else if (g_str_equal(security, "wep") == TRUE)
534 return G_SUPPLICANT_SECURITY_WEP;
535 else if (g_str_equal(security, "psk") == TRUE)
536 return G_SUPPLICANT_SECURITY_PSK;
537 else if (g_str_equal(security, "wpa") == TRUE)
538 return G_SUPPLICANT_SECURITY_PSK;
539 else if (g_str_equal(security, "rsn") == TRUE)
540 return G_SUPPLICANT_SECURITY_PSK;
541 else if (g_str_equal(security, "ieee8021x") == TRUE)
542 return G_SUPPLICANT_SECURITY_IEEE8021X;
544 return G_SUPPLICANT_SECURITY_UNKNOWN;
547 static void ssid_init(GSupplicantSSID *ssid, struct connman_network *network)
549 const char *security;
551 memset(ssid, 0, sizeof(*ssid));
552 ssid->ssid = connman_network_get_blob(network, "WiFi.SSID",
554 security = connman_network_get_string(network, "WiFi.Security");
555 ssid->security = network_security(security);
556 ssid->passphrase = connman_network_get_string(network,
558 ssid->eap = connman_network_get_string(network, "WiFi.EAP");
561 * If our private key password is unset,
562 * we use the supplied passphrase. That is needed
563 * for PEAP where 2 passphrases (identity and client
564 * cert may have to be provided.
566 if (connman_network_get_string(network,
567 "WiFi.PrivateKeyPassphrase") == NULL)
568 connman_network_set_string(network,
569 "WiFi.PrivateKeyPassphrase",
571 /* We must have an identity for both PEAP and TLS */
572 ssid->identity = connman_network_get_string(network, "WiFi.Identity");
573 ssid->ca_cert_path = connman_network_get_string(network,
575 ssid->client_cert_path = connman_network_get_string(network,
576 "WiFi.ClientCertFile");
577 ssid->private_key_path = connman_network_get_string(network,
578 "WiFi.PrivateKeyFile");
579 ssid->private_key_passphrase = connman_network_get_string(network,
580 "WiFi.PrivateKeyPassphrase");
581 ssid->phase2_auth = connman_network_get_string(network, "WiFi.Phase2");
585 static int network_connect(struct connman_network *network)
587 struct connman_device *device = connman_network_get_device(network);
588 struct wifi_data *wifi;
589 GSupplicantInterface *interface;
590 GSupplicantSSID ssid;
592 DBG("network %p", network);
597 wifi = connman_device_get_data(device);
601 interface = wifi->interface;
603 ssid_init(&ssid, network);
605 wifi->network = connman_network_ref(network);
607 return g_supplicant_interface_connect(interface, &ssid,
608 connect_callback, NULL);
611 static int network_disconnect(struct connman_network *network)
613 struct connman_device *device = connman_network_get_device(network);
614 struct wifi_data *wifi;
616 DBG("network %p", network);
618 wifi = connman_device_get_data(device);
619 if (wifi == NULL || wifi->interface == NULL)
622 connman_network_set_associating(network, FALSE);
624 return g_supplicant_interface_disconnect(wifi->interface,
625 disconnect_callback, wifi);
628 static struct connman_network_driver network_driver = {
630 .type = CONNMAN_NETWORK_TYPE_WIFI,
631 .priority = CONNMAN_NETWORK_PRIORITY_LOW,
632 .probe = network_probe,
633 .remove = network_remove,
634 .connect = network_connect,
635 .disconnect = network_disconnect,
638 static int tech_probe(struct connman_technology *technology)
640 wifi_technology = technology;
645 static void tech_remove(struct connman_technology *technology)
647 wifi_technology = NULL;
650 static void regdom_callback(void *user_data)
652 char *alpha2 = user_data;
656 if (wifi_technology == NULL)
659 connman_technology_regdom_notify(wifi_technology, alpha2);
662 static int tech_set_regdom(struct connman_technology *technology, const char *alpha2)
664 return g_supplicant_set_country(alpha2, regdom_callback, alpha2);
667 static struct connman_technology_driver tech_driver = {
669 .type = CONNMAN_SERVICE_TYPE_WIFI,
671 .remove = tech_remove,
672 .set_regdom = tech_set_regdom,
675 static int wifi_init(void)
679 err = connman_network_driver_register(&network_driver);
683 err = g_supplicant_register(&callbacks);
685 connman_network_driver_unregister(&network_driver);
689 err = connman_technology_driver_register(&tech_driver);
691 g_supplicant_unregister(&callbacks);
692 connman_network_driver_unregister(&network_driver);
699 static void wifi_exit(void)
703 connman_technology_driver_unregister(&tech_driver);
705 g_supplicant_unregister(&callbacks);
707 connman_network_driver_unregister(&network_driver);
710 CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
711 CONNMAN_PLUGIN_PRIORITY_DEFAULT, wifi_init, wifi_exit)