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 */
58 struct connman_device *device;
59 struct connman_network *network;
60 GSupplicantInterface *interface;
61 connman_bool_t connected;
67 static int get_bssid(struct connman_device *device,
68 unsigned char *bssid, unsigned int *bssid_len)
75 ifindex = connman_device_get_index(device);
79 ifname = connman_inet_ifname(ifindex);
83 fd = socket(PF_INET, SOCK_DGRAM, 0);
89 memset(&wrq, 0, sizeof(wrq));
90 strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
92 err = ioctl(fd, SIOCGIWAP, &wrq);
100 memcpy(bssid, wrq.u.ap_addr.sa_data, ETH_ALEN);
101 *bssid_len = ETH_ALEN;
106 static void wifi_newlink(unsigned flags, unsigned change, void *user_data)
108 struct connman_device *device = user_data;
109 struct wifi_data *wifi = connman_device_get_data(device);
111 DBG("index %d flags %d change %d", wifi->index, flags, change);
116 if ((wifi->flags & IFF_UP) != (flags & IFF_UP)) {
120 DBG("interface down");
123 if ((wifi->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
124 if (flags & IFF_LOWER_UP)
133 static int wifi_probe(struct connman_device *device)
135 struct wifi_data *wifi;
137 DBG("device %p", device);
139 wifi = g_try_new0(struct wifi_data, 1);
143 wifi->connected = FALSE;
145 connman_device_set_data(device, wifi);
146 wifi->device = connman_device_ref(device);
148 wifi->index = connman_device_get_index(device);
151 wifi->watch = connman_rtnl_add_newlink_watch(wifi->index,
152 wifi_newlink, device);
157 static void wifi_remove(struct connman_device *device)
159 struct wifi_data *wifi = connman_device_get_data(device);
161 DBG("device %p", device);
163 connman_device_set_data(device, NULL);
164 connman_device_unref(wifi->device);
165 connman_rtnl_remove_watch(wifi->watch);
167 g_supplicant_interface_set_data(wifi->interface, NULL);
169 g_free(wifi->identifier);
173 static void interface_create_callback(int result,
174 GSupplicantInterface *interface,
177 struct wifi_data *wifi = user_data;
179 DBG("result %d ifname %s", result,
180 g_supplicant_interface_get_ifname(interface));
185 wifi->interface = interface;
186 g_supplicant_interface_set_data(interface, wifi);
189 static void interface_remove_callback(int result,
190 GSupplicantInterface *interface,
193 struct wifi_data *wifi = user_data;
195 DBG("result %d", result);
200 wifi->interface = NULL;
204 static int wifi_enable(struct connman_device *device)
206 struct wifi_data *wifi = connman_device_get_data(device);
207 const char *interface = connman_device_get_string(device, "Interface");
208 const char *driver = connman_option_get_string("wifi");
210 DBG("device %p %p", device, wifi);
212 return g_supplicant_interface_create(interface, driver,
213 interface_create_callback,
217 static int wifi_disable(struct connman_device *device)
219 struct wifi_data *wifi = connman_device_get_data(device);
221 DBG("device %p", device);
223 wifi->connected = FALSE;
225 return g_supplicant_interface_remove(wifi->interface,
226 interface_remove_callback,
230 static void scan_callback(int result, GSupplicantInterface *interface,
233 struct connman_device *device = user_data;
235 DBG("result %d", result);
237 connman_device_set_scanning(device, FALSE);
240 static int wifi_scan(struct connman_device *device)
242 struct wifi_data *wifi = connman_device_get_data(device);
244 DBG("device %p %p", device, wifi->interface);
246 return g_supplicant_interface_scan(wifi->interface, scan_callback,
250 static struct connman_device_driver wifi_ng_driver = {
252 .type = CONNMAN_DEVICE_TYPE_WIFI,
253 .priority = CONNMAN_DEVICE_PRIORITY_LOW,
255 .remove = wifi_remove,
256 .enable = wifi_enable,
257 .disable = wifi_disable,
261 static void system_ready(void)
265 if (connman_device_driver_register(&wifi_ng_driver) < 0)
266 connman_error("Failed to register WiFi driver");
269 static void system_killed(void)
273 connman_device_driver_unregister(&wifi_ng_driver);
276 static void interface_added(GSupplicantInterface *interface)
278 const char *ifname = g_supplicant_interface_get_ifname(interface);
279 const char *driver = g_supplicant_interface_get_driver(interface);
280 struct wifi_data *wifi;
282 wifi = g_supplicant_interface_get_data(interface);
284 DBG("ifname %s driver %s wifi %p", ifname, driver, wifi);
286 if (wifi == NULL || wifi->device == NULL) {
287 connman_error("Wrong wifi pointer");
291 connman_device_set_powered(wifi->device, TRUE);
292 wifi_scan(wifi->device);
295 static void interface_state(GSupplicantInterface *interface)
297 struct connman_network *network;
298 struct connman_device *device;
299 struct wifi_data *wifi;
300 GSupplicantState state = g_supplicant_interface_get_state(interface);
301 unsigned char bssid[ETH_ALEN];
302 unsigned int bssid_len;
304 wifi = g_supplicant_interface_get_data(interface);
306 DBG("wifi %p interface state %d", wifi, state);
311 network = wifi->network;
312 device = wifi->device;
314 if (device == NULL || network == NULL)
318 case G_SUPPLICANT_STATE_SCANNING:
319 connman_device_set_scanning(device, TRUE);
322 case G_SUPPLICANT_STATE_AUTHENTICATING:
323 case G_SUPPLICANT_STATE_ASSOCIATING:
324 connman_network_set_associating(network, TRUE);
327 case G_SUPPLICANT_STATE_COMPLETED:
328 /* reset scan trigger and schedule background scan */
329 connman_device_schedule_scan(device);
331 if (get_bssid(device, bssid, &bssid_len) == 0)
332 connman_network_set_address(network,
334 connman_network_set_connected(network, TRUE);
337 case G_SUPPLICANT_STATE_DISCONNECTED:
338 connman_network_set_associating(network, FALSE);
339 connman_network_set_connected(network, FALSE);
342 case G_SUPPLICANT_STATE_INACTIVE:
343 connman_network_set_associating(network, FALSE);
346 case G_SUPPLICANT_STATE_UNKNOWN:
347 case G_SUPPLICANT_STATE_ASSOCIATED:
348 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
349 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
356 static void interface_removed(GSupplicantInterface *interface)
358 const char *ifname = g_supplicant_interface_get_ifname(interface);
359 struct wifi_data *wifi;
361 DBG("ifname %s", ifname);
363 wifi = g_supplicant_interface_get_data(interface);
365 if (wifi == NULL || wifi->device == NULL) {
366 connman_error("Wrong wifi pointer");
370 connman_device_set_powered(wifi->device, FALSE);
373 static void scan_started(GSupplicantInterface *interface)
375 struct wifi_data *wifi;
379 wifi = g_supplicant_interface_get_data(interface);
385 connman_device_set_scanning(wifi->device, TRUE);
388 static void scan_finished(GSupplicantInterface *interface)
390 struct wifi_data *wifi;
394 wifi = g_supplicant_interface_get_data(interface);
400 static unsigned char calculate_strength(GSupplicantNetwork *supplicant_network)
402 unsigned char strength;
404 strength = 120 + g_supplicant_network_get_signal(supplicant_network);
411 static void network_added(GSupplicantNetwork *supplicant_network)
413 struct connman_network *network;
414 GSupplicantInterface *interface;
415 struct wifi_data *wifi;
416 const char *name, *identifier, *mode, *security, *group;
417 const unsigned char *ssid;
418 unsigned int ssid_len;
422 interface = g_supplicant_network_get_interface(supplicant_network);
423 wifi = g_supplicant_interface_get_data(interface);
424 name = g_supplicant_network_get_name(supplicant_network);
425 identifier = g_supplicant_network_get_identifier(supplicant_network);
426 mode = g_supplicant_network_get_mode(supplicant_network);
427 security = g_supplicant_network_get_security(supplicant_network);
428 group = g_supplicant_network_get_identifier(supplicant_network);
433 ssid = g_supplicant_network_get_ssid(supplicant_network, &ssid_len);
435 network = connman_device_get_network(wifi->device, identifier);
437 if (network == NULL) {
438 network = connman_network_create(identifier,
439 CONNMAN_NETWORK_TYPE_WIFI);
443 connman_network_set_index(network, wifi->index);
445 if (connman_device_add_network(wifi->device, network) < 0) {
446 connman_network_unref(network);
451 if (name != NULL && name[0] != '\0')
452 connman_network_set_name(network, name);
454 connman_network_set_blob(network, "WiFi.SSID",
456 connman_network_set_string(network, "WiFi.Mode", mode);
457 connman_network_set_string(network, "WiFi.Security", security);
458 connman_network_set_strength(network,
459 calculate_strength(supplicant_network));
461 connman_network_set_available(network, TRUE);
464 connman_network_set_group(network, group);
467 static void network_removed(GSupplicantNetwork *network)
469 const char *name = g_supplicant_network_get_name(network);
471 DBG("* name %s", name);
474 static void debug(const char *str)
476 connman_debug("%s", str);
479 static const GSupplicantCallbacks callbacks = {
480 .system_ready = system_ready,
481 .system_killed = system_killed,
482 .interface_added = interface_added,
483 .interface_state = interface_state,
484 .interface_removed = interface_removed,
485 .scan_started = scan_started,
486 .scan_finished = scan_finished,
487 .network_added = network_added,
488 .network_removed = network_removed,
493 static int network_probe(struct connman_network *network)
495 DBG("network %p", network);
500 static void network_remove(struct connman_network *network)
502 DBG("network %p", network);
505 static void connect_callback(int result, GSupplicantInterface *interface,
508 connman_error("%s", __func__);
511 static void disconnect_callback(int result, GSupplicantInterface *interface,
514 struct wifi_data *wifi = user_data;
517 connman_error("%s", __func__);
521 connman_network_unref(wifi->network);
523 wifi->network = NULL;
527 static GSupplicantSecurity network_security(const char *security)
529 if (g_str_equal(security, "none") == TRUE)
530 return G_SUPPLICANT_SECURITY_NONE;
531 else if (g_str_equal(security, "wep") == TRUE)
532 return G_SUPPLICANT_SECURITY_WEP;
533 else if (g_str_equal(security, "psk") == TRUE)
534 return G_SUPPLICANT_SECURITY_PSK;
535 else if (g_str_equal(security, "wpa") == TRUE)
536 return G_SUPPLICANT_SECURITY_PSK;
537 else if (g_str_equal(security, "rsn") == TRUE)
538 return G_SUPPLICANT_SECURITY_PSK;
539 else if (g_str_equal(security, "ieee8021x") == TRUE)
540 return G_SUPPLICANT_SECURITY_IEEE8021X;
542 return G_SUPPLICANT_SECURITY_UNKNOWN;
545 static void ssid_init(GSupplicantSSID *ssid, struct connman_network *network)
547 const char *security;
549 memset(ssid, 0, sizeof(*ssid));
550 ssid->ssid = connman_network_get_blob(network, "WiFi.SSID",
552 security = connman_network_get_string(network, "WiFi.Security");
553 ssid->security = network_security(security);
554 ssid->passphrase = connman_network_get_string(network,
556 ssid->eap = connman_network_get_string(network, "WiFi.EAP");
559 * If our private key password is unset,
560 * we use the supplied passphrase. That is needed
561 * for PEAP where 2 passphrases (identity and client
562 * cert may have to be provided.
564 if (connman_network_get_string(network,
565 "WiFi.PrivateKeyPassphrase") == NULL)
566 connman_network_set_string(network,
567 "WiFi.PrivateKeyPassphrase",
569 /* We must have an identity for both PEAP and TLS */
570 ssid->identity = connman_network_get_string(network, "WiFi.Identity");
571 ssid->ca_cert_path = connman_network_get_string(network,
573 ssid->client_cert_path = connman_network_get_string(network,
574 "WiFi.ClientCertFile");
575 ssid->private_key_path = connman_network_get_string(network,
576 "WiFi.PrivateKeyFile");
577 ssid->private_key_passphrase = connman_network_get_string(network,
578 "WiFi.PrivateKeyPassphrase");
579 ssid->phase2_auth = connman_network_get_string(network, "WiFi.Phase2");
583 static int network_connect(struct connman_network *network)
585 struct connman_device *device = connman_network_get_device(network);
586 struct wifi_data *wifi;
587 GSupplicantInterface *interface;
588 GSupplicantSSID ssid;
590 DBG("network %p", network);
595 wifi = connman_device_get_data(device);
599 interface = wifi->interface;
601 ssid_init(&ssid, network);
603 wifi->network = connman_network_ref(network);
605 return g_supplicant_interface_connect(interface, &ssid,
606 connect_callback, NULL);
609 static int network_disconnect(struct connman_network *network)
611 struct connman_device *device = connman_network_get_device(network);
612 struct wifi_data *wifi;
614 DBG("network %p", network);
616 wifi = connman_device_get_data(device);
617 if (wifi == NULL || wifi->interface == NULL)
620 connman_network_set_associating(network, FALSE);
622 return g_supplicant_interface_disconnect(wifi->interface,
623 disconnect_callback, wifi);
626 static struct connman_network_driver network_driver = {
628 .type = CONNMAN_NETWORK_TYPE_WIFI,
629 .priority = CONNMAN_NETWORK_PRIORITY_LOW,
630 .probe = network_probe,
631 .remove = network_remove,
632 .connect = network_connect,
633 .disconnect = network_disconnect,
636 static int tech_probe(struct connman_technology *technology)
641 static void tech_remove(struct connman_technology *technology)
645 static int tech_set_regdom(struct connman_technology *technology, const char *alpha2)
650 static struct connman_technology_driver tech_driver = {
652 .type = CONNMAN_SERVICE_TYPE_BLUETOOTH,
654 .remove = tech_remove,
655 .set_regdom = tech_set_regdom,
658 static int wifi_init(void)
662 err = connman_network_driver_register(&network_driver);
666 err = g_supplicant_register(&callbacks);
668 connman_network_driver_unregister(&network_driver);
672 err = connman_technology_driver_register(&tech_driver);
674 g_supplicant_unregister(&callbacks);
675 connman_network_driver_unregister(&network_driver);
682 static void wifi_exit(void)
686 connman_technology_driver_unregister(&tech_driver);
688 g_supplicant_unregister(&callbacks);
690 connman_network_driver_unregister(&network_driver);
693 CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
694 CONNMAN_PLUGIN_PRIORITY_DEFAULT, wifi_init, wifi_exit)