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/log.h>
48 #include <connman/option.h>
50 #include <gsupplicant/gsupplicant.h>
52 #define CLEANUP_TIMEOUT 8 /* in seconds */
53 #define INACTIVE_TIMEOUT 12 /* in seconds */
57 struct connman_device *device;
58 struct connman_network *network;
59 GSupplicantInterface *interface;
60 connman_bool_t connected;
66 static int get_bssid(struct connman_device *device,
67 unsigned char *bssid, unsigned int *bssid_len)
74 ifindex = connman_device_get_index(device);
78 ifname = connman_inet_ifname(ifindex);
82 fd = socket(PF_INET, SOCK_DGRAM, 0);
88 memset(&wrq, 0, sizeof(wrq));
89 strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
91 err = ioctl(fd, SIOCGIWAP, &wrq);
99 memcpy(bssid, wrq.u.ap_addr.sa_data, ETH_ALEN);
100 *bssid_len = ETH_ALEN;
105 static void wifi_newlink(unsigned flags, unsigned change, void *user_data)
107 struct connman_device *device = user_data;
108 struct wifi_data *wifi = connman_device_get_data(device);
110 DBG("index %d flags %d change %d", wifi->index, flags, change);
115 if ((wifi->flags & IFF_UP) != (flags & IFF_UP)) {
119 DBG("interface down");
122 if ((wifi->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
123 if (flags & IFF_LOWER_UP)
132 static int wifi_probe(struct connman_device *device)
134 struct wifi_data *wifi;
136 DBG("device %p", device);
138 wifi = g_try_new0(struct wifi_data, 1);
142 wifi->connected = FALSE;
144 connman_device_set_data(device, wifi);
145 wifi->device = connman_device_ref(device);
147 wifi->index = connman_device_get_index(device);
150 wifi->watch = connman_rtnl_add_newlink_watch(wifi->index,
151 wifi_newlink, device);
156 static void wifi_remove(struct connman_device *device)
158 struct wifi_data *wifi = connman_device_get_data(device);
160 DBG("device %p", device);
162 connman_device_set_data(device, NULL);
163 connman_device_unref(wifi->device);
164 connman_rtnl_remove_watch(wifi->watch);
166 g_free(wifi->identifier);
170 static void interface_create_callback(int result,
171 GSupplicantInterface *interface,
174 struct wifi_data *wifi = user_data;
176 DBG("result %d ifname %s", result,
177 g_supplicant_interface_get_ifname(interface));
182 wifi->interface = interface;
183 g_supplicant_interface_set_data(interface, wifi);
186 static void interface_remove_callback(int result,
187 GSupplicantInterface *interface,
190 struct wifi_data *wifi = user_data;
192 DBG("result %d", result);
197 wifi->interface = NULL;
201 static int wifi_enable(struct connman_device *device)
203 struct wifi_data *wifi = connman_device_get_data(device);
204 const char *interface = connman_device_get_string(device, "Interface");
205 const char *driver = connman_option_get_string("wifi");
207 DBG("device %p %p", device, wifi);
209 return g_supplicant_interface_create(interface, driver,
210 interface_create_callback,
214 static int wifi_disable(struct connman_device *device)
216 struct wifi_data *wifi = connman_device_get_data(device);
218 DBG("device %p", device);
220 wifi->connected = FALSE;
222 return g_supplicant_interface_remove(wifi->interface,
223 interface_remove_callback,
227 static void scan_callback(int result, GSupplicantInterface *interface,
230 struct connman_device *device = user_data;
232 DBG("result %d", result);
235 connman_device_set_scanning(device, FALSE);
239 connman_device_set_scanning(device, TRUE);
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, *path, *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 path = g_supplicant_network_get_path(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, path);
440 if (network == NULL) {
441 network = connman_network_create(identifier,
442 CONNMAN_NETWORK_TYPE_WIFI);
446 connman_network_set_index(network, wifi->index);
448 connman_network_set_protocol(network,
449 CONNMAN_NETWORK_PROTOCOL_IP);
451 if (connman_device_add_network(wifi->device, network) < 0) {
452 connman_network_unref(network);
457 if (name != NULL && name[0] != '\0')
458 connman_network_set_name(network, name);
460 connman_network_set_blob(network, "WiFi.SSID",
462 connman_network_set_string(network, "WiFi.Mode", mode);
463 connman_network_set_string(network, "WiFi.Security", security);
464 connman_network_set_strength(network,
465 calculate_strength(supplicant_network));
467 connman_network_set_available(network, TRUE);
470 connman_network_set_group(network, group);
473 static void network_removed(GSupplicantNetwork *network)
475 const char *name = g_supplicant_network_get_name(network);
477 DBG("* name %s", name);
480 static void debug(const char *str)
482 connman_debug("%s", str);
485 static const GSupplicantCallbacks callbacks = {
486 .system_ready = system_ready,
487 .system_killed = system_killed,
488 .interface_added = interface_added,
489 .interface_state = interface_state,
490 .interface_removed = interface_removed,
491 .scan_started = scan_started,
492 .scan_finished = scan_finished,
493 .network_added = network_added,
494 .network_removed = network_removed,
499 static int network_probe(struct connman_network *network)
501 DBG("network %p", network);
506 static void network_remove(struct connman_network *network)
508 DBG("network %p", network);
511 static void connect_callback(int result, GSupplicantInterface *interface,
514 connman_error("%s", __func__);
517 static void disconnect_callback(int result, GSupplicantInterface *interface,
520 struct wifi_data *wifi = user_data;
523 connman_error("%s", __func__);
527 connman_network_unref(wifi->network);
529 wifi->network = NULL;
533 static GSupplicantSecurity network_security(const char *security)
535 if (g_str_equal(security, "none") == TRUE)
536 return G_SUPPLICANT_SECURITY_NONE;
537 else if (g_str_equal(security, "wep") == TRUE)
538 return G_SUPPLICANT_SECURITY_WEP;
539 else if (g_str_equal(security, "psk") == TRUE)
540 return G_SUPPLICANT_SECURITY_PSK;
541 else if (g_str_equal(security, "wpa") == TRUE)
542 return G_SUPPLICANT_SECURITY_PSK;
543 else if (g_str_equal(security, "rsn") == TRUE)
544 return G_SUPPLICANT_SECURITY_PSK;
545 else if (g_str_equal(security, "ieee8021x") == TRUE)
546 return G_SUPPLICANT_SECURITY_IEEE8021X;
548 return G_SUPPLICANT_SECURITY_UNKNOWN;
551 static void ssid_init(GSupplicantSSID *ssid, struct connman_network *network)
553 const char *security;
555 memset(ssid, 0, sizeof(*ssid));
556 ssid->ssid = connman_network_get_blob(network, "WiFi.SSID",
558 security = connman_network_get_string(network, "WiFi.Security");
559 ssid->security = network_security(security);
560 ssid->passphrase = connman_network_get_string(network,
562 ssid->eap = connman_network_get_string(network, "WiFi.EAP");
565 * If our private key password is unset,
566 * we use the supplied passphrase. That is needed
567 * for PEAP where 2 passphrases (identity and client
568 * cert may have to be provided.
570 if (connman_network_get_string(network,
571 "WiFi.PrivateKeyPassphrase") == NULL)
572 connman_network_set_string(network,
573 "WiFi.PrivateKeyPassphrase",
575 /* We must have an identity for both PEAP and TLS */
576 ssid->identity = connman_network_get_string(network, "WiFi.Identity");
577 ssid->ca_cert_path = connman_network_get_string(network,
579 ssid->client_cert_path = connman_network_get_string(network,
580 "WiFi.ClientCertFile");
581 ssid->private_key_path = connman_network_get_string(network,
582 "WiFi.PrivateKeyFile");
583 ssid->private_key_passphrase = connman_network_get_string(network,
584 "WiFi.PrivateKeyPassphrase");
585 ssid->phase2_auth = connman_network_get_string(network, "WiFi.Phase2");
589 static int network_connect(struct connman_network *network)
591 struct connman_device *device = connman_network_get_device(network);
592 struct wifi_data *wifi;
593 GSupplicantInterface *interface;
594 GSupplicantSSID ssid;
596 DBG("network %p", network);
601 wifi = connman_device_get_data(device);
605 interface = wifi->interface;
607 ssid_init(&ssid, network);
609 wifi->network = connman_network_ref(network);
611 return g_supplicant_interface_connect(interface, &ssid,
612 connect_callback, NULL);
615 static int network_disconnect(struct connman_network *network)
617 struct connman_device *device = connman_network_get_device(network);
618 struct wifi_data *wifi;
620 DBG("network %p", network);
622 wifi = connman_device_get_data(device);
623 if (wifi == NULL || wifi->interface == NULL)
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 wifi_init(void)
644 err = connman_network_driver_register(&network_driver);
648 err = g_supplicant_register(&callbacks);
650 connman_network_driver_unregister(&network_driver);
657 static void wifi_exit(void)
661 g_supplicant_unregister(&callbacks);
663 connman_network_driver_unregister(&network_driver);
666 CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
667 CONNMAN_PLUGIN_PRIORITY_DEFAULT, wifi_init, wifi_exit)