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);
234 connman_device_set_scanning(device, FALSE);
237 static int wifi_scan(struct connman_device *device)
239 struct wifi_data *wifi = connman_device_get_data(device);
241 DBG("device %p %p", device, wifi->interface);
243 return g_supplicant_interface_scan(wifi->interface, scan_callback,
247 static struct connman_device_driver wifi_ng_driver = {
249 .type = CONNMAN_DEVICE_TYPE_WIFI,
250 .priority = CONNMAN_DEVICE_PRIORITY_LOW,
252 .remove = wifi_remove,
253 .enable = wifi_enable,
254 .disable = wifi_disable,
258 static void system_ready(void)
262 if (connman_device_driver_register(&wifi_ng_driver) < 0)
263 connman_error("Failed to register WiFi driver");
266 static void system_killed(void)
270 connman_device_driver_unregister(&wifi_ng_driver);
273 static void interface_added(GSupplicantInterface *interface)
275 const char *ifname = g_supplicant_interface_get_ifname(interface);
276 const char *driver = g_supplicant_interface_get_driver(interface);
277 struct wifi_data *wifi;
279 wifi = g_supplicant_interface_get_data(interface);
281 DBG("ifname %s driver %s wifi %p", ifname, driver, wifi);
283 if (wifi == NULL || wifi->device == NULL) {
284 connman_error("Wrong wifi pointer");
288 connman_device_set_powered(wifi->device, TRUE);
289 wifi_scan(wifi->device);
292 static void interface_state(GSupplicantInterface *interface)
294 struct connman_network *network;
295 struct connman_device *device;
296 struct wifi_data *wifi;
297 GSupplicantState state = g_supplicant_interface_get_state(interface);
298 unsigned char bssid[ETH_ALEN];
299 unsigned int bssid_len;
301 wifi = g_supplicant_interface_get_data(interface);
303 DBG("wifi %p interface state %d", wifi, state);
308 network = wifi->network;
309 device = wifi->device;
311 if (device == NULL || network == NULL)
315 case G_SUPPLICANT_STATE_SCANNING:
316 connman_device_set_scanning(device, TRUE);
319 case G_SUPPLICANT_STATE_AUTHENTICATING:
320 case G_SUPPLICANT_STATE_ASSOCIATING:
321 connman_network_set_associating(network, TRUE);
324 case G_SUPPLICANT_STATE_COMPLETED:
325 /* reset scan trigger and schedule background scan */
326 connman_device_schedule_scan(device);
328 if (get_bssid(device, bssid, &bssid_len) == 0)
329 connman_network_set_address(network,
331 connman_network_set_connected(network, TRUE);
334 case G_SUPPLICANT_STATE_DISCONNECTED:
335 connman_network_set_associating(network, FALSE);
336 connman_network_set_connected(network, FALSE);
339 case G_SUPPLICANT_STATE_INACTIVE:
340 connman_network_set_associating(network, FALSE);
343 case G_SUPPLICANT_STATE_UNKNOWN:
344 case G_SUPPLICANT_STATE_ASSOCIATED:
345 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
346 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
353 static void interface_removed(GSupplicantInterface *interface)
355 const char *ifname = g_supplicant_interface_get_ifname(interface);
356 struct wifi_data *wifi;
358 DBG("ifname %s", ifname);
360 wifi = g_supplicant_interface_get_data(interface);
362 if (wifi == NULL || wifi->device == NULL) {
363 connman_error("Wrong wifi pointer");
367 connman_device_set_powered(wifi->device, FALSE);
370 static void scan_started(GSupplicantInterface *interface)
372 struct wifi_data *wifi;
376 wifi = g_supplicant_interface_get_data(interface);
382 connman_device_set_scanning(wifi->device, TRUE);
385 static void scan_finished(GSupplicantInterface *interface)
387 struct wifi_data *wifi;
391 wifi = g_supplicant_interface_get_data(interface);
397 static unsigned char calculate_strength(GSupplicantNetwork *supplicant_network)
399 unsigned char strength;
401 strength = 120 + g_supplicant_network_get_signal(supplicant_network);
408 static void network_added(GSupplicantNetwork *supplicant_network)
410 struct connman_network *network;
411 GSupplicantInterface *interface;
412 struct wifi_data *wifi;
413 const char *name, *path, *identifier, *mode, *security, *group;
414 const unsigned char *ssid;
415 unsigned int ssid_len;
419 interface = g_supplicant_network_get_interface(supplicant_network);
420 wifi = g_supplicant_interface_get_data(interface);
421 name = g_supplicant_network_get_name(supplicant_network);
422 path = g_supplicant_network_get_path(supplicant_network);
423 identifier = g_supplicant_network_get_identifier(supplicant_network);
424 mode = g_supplicant_network_get_mode(supplicant_network);
425 security = g_supplicant_network_get_security(supplicant_network);
426 group = g_supplicant_network_get_identifier(supplicant_network);
431 ssid = g_supplicant_network_get_ssid(supplicant_network, &ssid_len);
433 network = connman_device_get_network(wifi->device, path);
435 if (network == NULL) {
436 network = connman_network_create(identifier,
437 CONNMAN_NETWORK_TYPE_WIFI);
441 connman_network_set_index(network, wifi->index);
443 connman_network_set_protocol(network,
444 CONNMAN_NETWORK_PROTOCOL_IP);
446 if (connman_device_add_network(wifi->device, network) < 0) {
447 connman_network_unref(network);
452 if (name != NULL && name[0] != '\0')
453 connman_network_set_name(network, name);
455 connman_network_set_blob(network, "WiFi.SSID",
457 connman_network_set_string(network, "WiFi.Mode", mode);
458 connman_network_set_string(network, "WiFi.Security", security);
459 connman_network_set_strength(network,
460 calculate_strength(supplicant_network));
462 connman_network_set_available(network, TRUE);
465 connman_network_set_group(network, group);
468 static void network_removed(GSupplicantNetwork *network)
470 const char *name = g_supplicant_network_get_name(network);
472 DBG("* name %s", name);
475 static void debug(const char *str)
477 connman_debug("%s", str);
480 static const GSupplicantCallbacks callbacks = {
481 .system_ready = system_ready,
482 .system_killed = system_killed,
483 .interface_added = interface_added,
484 .interface_state = interface_state,
485 .interface_removed = interface_removed,
486 .scan_started = scan_started,
487 .scan_finished = scan_finished,
488 .network_added = network_added,
489 .network_removed = network_removed,
494 static int network_probe(struct connman_network *network)
496 DBG("network %p", network);
501 static void network_remove(struct connman_network *network)
503 DBG("network %p", network);
506 static void connect_callback(int result, GSupplicantInterface *interface,
509 connman_error("%s", __func__);
512 static void disconnect_callback(int result, GSupplicantInterface *interface,
515 struct wifi_data *wifi = user_data;
518 connman_error("%s", __func__);
522 connman_network_unref(wifi->network);
524 wifi->network = NULL;
528 static GSupplicantSecurity network_security(const char *security)
530 if (g_str_equal(security, "none") == TRUE)
531 return G_SUPPLICANT_SECURITY_NONE;
532 else if (g_str_equal(security, "wep") == TRUE)
533 return G_SUPPLICANT_SECURITY_WEP;
534 else if (g_str_equal(security, "psk") == TRUE)
535 return G_SUPPLICANT_SECURITY_PSK;
536 else if (g_str_equal(security, "wpa") == TRUE)
537 return G_SUPPLICANT_SECURITY_PSK;
538 else if (g_str_equal(security, "rsn") == TRUE)
539 return G_SUPPLICANT_SECURITY_PSK;
540 else if (g_str_equal(security, "ieee8021x") == TRUE)
541 return G_SUPPLICANT_SECURITY_IEEE8021X;
543 return G_SUPPLICANT_SECURITY_UNKNOWN;
546 static void ssid_init(GSupplicantSSID *ssid, struct connman_network *network)
548 const char *security;
550 memset(ssid, 0, sizeof(*ssid));
551 ssid->ssid = connman_network_get_blob(network, "WiFi.SSID",
553 security = connman_network_get_string(network, "WiFi.Security");
554 ssid->security = network_security(security);
555 ssid->passphrase = connman_network_get_string(network,
557 ssid->eap = connman_network_get_string(network, "WiFi.EAP");
560 * If our private key password is unset,
561 * we use the supplied passphrase. That is needed
562 * for PEAP where 2 passphrases (identity and client
563 * cert may have to be provided.
565 if (connman_network_get_string(network,
566 "WiFi.PrivateKeyPassphrase") == NULL)
567 connman_network_set_string(network,
568 "WiFi.PrivateKeyPassphrase",
570 /* We must have an identity for both PEAP and TLS */
571 ssid->identity = connman_network_get_string(network, "WiFi.Identity");
572 ssid->ca_cert_path = connman_network_get_string(network,
574 ssid->client_cert_path = connman_network_get_string(network,
575 "WiFi.ClientCertFile");
576 ssid->private_key_path = connman_network_get_string(network,
577 "WiFi.PrivateKeyFile");
578 ssid->private_key_passphrase = connman_network_get_string(network,
579 "WiFi.PrivateKeyPassphrase");
580 ssid->phase2_auth = connman_network_get_string(network, "WiFi.Phase2");
584 static int network_connect(struct connman_network *network)
586 struct connman_device *device = connman_network_get_device(network);
587 struct wifi_data *wifi;
588 GSupplicantInterface *interface;
589 GSupplicantSSID ssid;
591 DBG("network %p", network);
596 wifi = connman_device_get_data(device);
600 interface = wifi->interface;
602 ssid_init(&ssid, network);
604 wifi->network = connman_network_ref(network);
606 return g_supplicant_interface_connect(interface, &ssid,
607 connect_callback, NULL);
610 static int network_disconnect(struct connman_network *network)
612 struct connman_device *device = connman_network_get_device(network);
613 struct wifi_data *wifi;
615 DBG("network %p", network);
617 wifi = connman_device_get_data(device);
618 if (wifi == NULL || wifi->interface == NULL)
621 return g_supplicant_interface_disconnect(wifi->interface,
622 disconnect_callback, wifi);
625 static struct connman_network_driver network_driver = {
627 .type = CONNMAN_NETWORK_TYPE_WIFI,
628 .priority = CONNMAN_NETWORK_PRIORITY_LOW,
629 .probe = network_probe,
630 .remove = network_remove,
631 .connect = network_connect,
632 .disconnect = network_disconnect,
635 static int wifi_init(void)
639 err = connman_network_driver_register(&network_driver);
643 err = g_supplicant_register(&callbacks);
645 connman_network_driver_unregister(&network_driver);
652 static void wifi_exit(void)
656 g_supplicant_unregister(&callbacks);
658 connman_network_driver_unregister(&network_driver);
661 CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
662 CONNMAN_PLUGIN_PRIORITY_DEFAULT, wifi_init, wifi_exit)