wifi: Regulatory domain setting implementation
[framework/connectivity/connman.git] / plugins / wifi.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
6  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <unistd.h>
27 #include <errno.h>
28 #include <string.h>
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>
34
35 #ifndef IFF_LOWER_UP
36 #define IFF_LOWER_UP    0x10000
37 #endif
38
39 #include <dbus/dbus.h>
40 #include <glib.h>
41
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>
50
51 #include <gsupplicant/gsupplicant.h>
52
53 #define CLEANUP_TIMEOUT   8     /* in seconds */
54 #define INACTIVE_TIMEOUT  12    /* in seconds */
55
56 struct connman_technology *wifi_technology = NULL;
57
58 struct wifi_data {
59         char *identifier;
60         struct connman_device *device;
61         struct connman_network *network;
62         GSupplicantInterface *interface;
63         connman_bool_t connected;
64         int index;
65         unsigned flags;
66         unsigned int watch;
67 };
68
69 static int get_bssid(struct connman_device *device,
70                                 unsigned char *bssid, unsigned int *bssid_len)
71 {
72         struct iwreq wrq;
73         char *ifname;
74         int ifindex;
75         int fd, err;
76
77         ifindex = connman_device_get_index(device);
78         if (ifindex < 0)
79                 return -EINVAL;
80
81         ifname = connman_inet_ifname(ifindex);
82         if (ifname == NULL)
83                 return -EINVAL;
84
85         fd = socket(PF_INET, SOCK_DGRAM, 0);
86         if (fd < 0) {
87                 g_free(ifname);
88                 return -EINVAL;
89         }
90
91         memset(&wrq, 0, sizeof(wrq));
92         strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
93
94         err = ioctl(fd, SIOCGIWAP, &wrq);
95
96         g_free(ifname);
97         close(fd);
98
99         if (err < 0)
100                 return -EIO;
101
102         memcpy(bssid, wrq.u.ap_addr.sa_data, ETH_ALEN);
103         *bssid_len = ETH_ALEN;
104
105         return 0;
106 }
107
108 static void wifi_newlink(unsigned flags, unsigned change, void *user_data)
109 {
110         struct connman_device *device = user_data;
111         struct wifi_data *wifi = connman_device_get_data(device);
112
113         DBG("index %d flags %d change %d", wifi->index, flags, change);
114
115         if (!change)
116                 return;
117
118         if ((wifi->flags & IFF_UP) != (flags & IFF_UP)) {
119                 if (flags & IFF_UP)
120                         DBG("interface up");
121                 else
122                         DBG("interface down");
123         }
124
125         if ((wifi->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
126                 if (flags & IFF_LOWER_UP)
127                         DBG("carrier on");
128                 else
129                         DBG("carrier off");
130         }
131
132         wifi->flags = flags;
133 }
134
135 static int wifi_probe(struct connman_device *device)
136 {
137         struct wifi_data *wifi;
138
139         DBG("device %p", device);
140
141         wifi = g_try_new0(struct wifi_data, 1);
142         if (wifi == NULL)
143                 return -ENOMEM;
144
145         wifi->connected = FALSE;
146
147         connman_device_set_data(device, wifi);
148         wifi->device = connman_device_ref(device);
149
150         wifi->index = connman_device_get_index(device);
151         wifi->flags = 0;
152
153         wifi->watch = connman_rtnl_add_newlink_watch(wifi->index,
154                                                         wifi_newlink, device);
155
156         return 0;
157 }
158
159 static void wifi_remove(struct connman_device *device)
160 {
161         struct wifi_data *wifi = connman_device_get_data(device);
162
163         DBG("device %p", device);
164
165         connman_device_set_data(device, NULL);
166         connman_device_unref(wifi->device);
167         connman_rtnl_remove_watch(wifi->watch);
168
169         g_supplicant_interface_set_data(wifi->interface, NULL);
170
171         g_free(wifi->identifier);
172         g_free(wifi);
173 }
174
175 static void interface_create_callback(int result,
176                                         GSupplicantInterface *interface,
177                                                         void *user_data)
178 {
179         struct wifi_data *wifi = user_data;
180
181         DBG("result %d ifname %s", result,
182                                 g_supplicant_interface_get_ifname(interface));
183
184         if (result < 0)
185                 return;
186
187         wifi->interface = interface;
188         g_supplicant_interface_set_data(interface, wifi);
189 }
190
191 static void interface_remove_callback(int result,
192                                         GSupplicantInterface *interface,
193                                                         void *user_data)
194 {
195         struct wifi_data *wifi = user_data;
196
197         DBG("result %d", result);
198
199         if (result < 0)
200                 return;
201
202         wifi->interface = NULL;
203 }
204
205
206 static int wifi_enable(struct connman_device *device)
207 {
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");
211
212         DBG("device %p %p", device, wifi);
213
214         return g_supplicant_interface_create(interface, driver,
215                                                 interface_create_callback,
216                                                         wifi);
217 }
218
219 static int wifi_disable(struct connman_device *device)
220 {
221         struct wifi_data *wifi = connman_device_get_data(device);
222
223         DBG("device %p", device);
224
225         wifi->connected = FALSE;
226
227         return g_supplicant_interface_remove(wifi->interface,
228                                                 interface_remove_callback,
229                                                         wifi);
230 }
231
232 static void scan_callback(int result, GSupplicantInterface *interface,
233                                                 void *user_data)
234 {
235         struct connman_device *device = user_data;
236
237         DBG("result %d", result);
238
239         connman_device_set_scanning(device, FALSE);
240 }
241
242 static int wifi_scan(struct connman_device *device)
243 {
244         struct wifi_data *wifi = connman_device_get_data(device);
245
246         DBG("device %p %p", device, wifi->interface);
247
248         return g_supplicant_interface_scan(wifi->interface, scan_callback,
249                                                                 device);
250 }
251
252 static struct connman_device_driver wifi_ng_driver = {
253         .name           = "wifi",
254         .type           = CONNMAN_DEVICE_TYPE_WIFI,
255         .priority       = CONNMAN_DEVICE_PRIORITY_LOW,
256         .probe          = wifi_probe,
257         .remove         = wifi_remove,
258         .enable         = wifi_enable,
259         .disable        = wifi_disable,
260         .scan           = wifi_scan,
261 };
262
263 static void system_ready(void)
264 {
265         DBG("");
266
267         if (connman_device_driver_register(&wifi_ng_driver) < 0)
268                 connman_error("Failed to register WiFi driver");
269 }
270
271 static void system_killed(void)
272 {
273         DBG("");
274
275         connman_device_driver_unregister(&wifi_ng_driver);
276 }
277
278 static void interface_added(GSupplicantInterface *interface)
279 {
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;
283
284         wifi = g_supplicant_interface_get_data(interface);
285
286         DBG("ifname %s driver %s wifi %p", ifname, driver, wifi);
287
288         if (wifi == NULL || wifi->device == NULL) {
289                 connman_error("Wrong wifi pointer");
290                 return;
291         }
292
293         connman_device_set_powered(wifi->device, TRUE);
294         wifi_scan(wifi->device);
295 }
296
297 static void interface_state(GSupplicantInterface *interface)
298 {
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;
305
306         wifi = g_supplicant_interface_get_data(interface);
307
308         DBG("wifi %p interface state %d", wifi, state);
309
310         if (wifi == NULL)
311                 return;
312
313         network = wifi->network;
314         device = wifi->device;
315
316         if (device == NULL || network == NULL)
317                 return;
318
319         switch (state) {
320         case G_SUPPLICANT_STATE_SCANNING:
321                 connman_device_set_scanning(device, TRUE);
322                 break;
323
324         case G_SUPPLICANT_STATE_AUTHENTICATING:
325         case G_SUPPLICANT_STATE_ASSOCIATING:
326                 connman_network_set_associating(network, TRUE);
327                 break;
328
329         case G_SUPPLICANT_STATE_COMPLETED:
330                 /* reset scan trigger and schedule background scan */
331                 connman_device_schedule_scan(device);
332
333                 if (get_bssid(device, bssid, &bssid_len) == 0)
334                         connman_network_set_address(network,
335                                                         bssid, bssid_len);
336                 connman_network_set_connected(network, TRUE);
337                 break;
338
339         case G_SUPPLICANT_STATE_DISCONNECTED:
340                 connman_network_set_associating(network, FALSE);
341                 connman_network_set_connected(network, FALSE);
342                 break;
343
344         case G_SUPPLICANT_STATE_INACTIVE:
345                 connman_network_set_associating(network, FALSE);
346                 break;
347
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:
352                 break;
353         }
354
355         DBG("DONE");
356 }
357
358 static void interface_removed(GSupplicantInterface *interface)
359 {
360         const char *ifname = g_supplicant_interface_get_ifname(interface);
361         struct wifi_data *wifi;
362
363         DBG("ifname %s", ifname);
364
365         wifi = g_supplicant_interface_get_data(interface);
366
367         if (wifi == NULL || wifi->device == NULL) {
368                 connman_error("Wrong wifi pointer");
369                 return;
370         }
371
372         connman_device_set_powered(wifi->device, FALSE);
373 }
374
375 static void scan_started(GSupplicantInterface *interface)
376 {
377         struct wifi_data *wifi;
378
379         DBG("");
380
381         wifi = g_supplicant_interface_get_data(interface);
382
383         if (wifi == NULL)
384                 return;
385
386         if (wifi->device)
387                 connman_device_set_scanning(wifi->device, TRUE);
388 }
389
390 static void scan_finished(GSupplicantInterface *interface)
391 {
392         struct wifi_data *wifi;
393
394         DBG("");
395
396         wifi = g_supplicant_interface_get_data(interface);
397
398         if (wifi == NULL)
399                 return;
400 }
401
402 static unsigned char calculate_strength(GSupplicantNetwork *supplicant_network)
403 {
404         unsigned char strength;
405
406         strength = 120 + g_supplicant_network_get_signal(supplicant_network);
407         if (strength > 100)
408                 strength = 100;
409
410         return strength;
411 }
412
413 static void network_added(GSupplicantNetwork *supplicant_network)
414 {
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;
421
422         DBG("");
423
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);
431
432         if (wifi == NULL)
433                 return;
434
435         ssid = g_supplicant_network_get_ssid(supplicant_network, &ssid_len);
436
437         network = connman_device_get_network(wifi->device, identifier);
438
439         if (network == NULL) {
440                 network = connman_network_create(identifier,
441                                                 CONNMAN_NETWORK_TYPE_WIFI);
442                 if (network == NULL)
443                         return;
444
445                 connman_network_set_index(network, wifi->index);
446
447                 if (connman_device_add_network(wifi->device, network) < 0) {
448                         connman_network_unref(network);
449                         return;
450                 }
451         }
452
453         if (name != NULL && name[0] != '\0')
454                 connman_network_set_name(network, name);
455
456         connman_network_set_blob(network, "WiFi.SSID",
457                                                 ssid, ssid_len);
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));
462
463         connman_network_set_available(network, TRUE);
464
465         if (ssid != NULL)
466                 connman_network_set_group(network, group);
467 }
468
469 static void network_removed(GSupplicantNetwork *network)
470 {
471         const char *name = g_supplicant_network_get_name(network);
472
473         DBG("* name %s", name);
474 }
475
476 static void debug(const char *str)
477 {
478         connman_debug("%s", str);
479 }
480
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,
491         .debug                  = debug,
492 };
493
494
495 static int network_probe(struct connman_network *network)
496 {
497         DBG("network %p", network);
498
499         return 0;
500 }
501
502 static void network_remove(struct connman_network *network)
503 {
504         DBG("network %p", network);
505 }
506
507 static void connect_callback(int result, GSupplicantInterface *interface,
508                                                         void *user_data)
509 {
510         connman_error("%s", __func__);
511 }
512
513 static void disconnect_callback(int result, GSupplicantInterface *interface,
514                                                         void *user_data)
515 {
516         struct wifi_data *wifi = user_data;
517
518         if (result < 0) {
519                 connman_error("%s", __func__);
520                 return;
521         }
522
523         connman_network_unref(wifi->network);
524
525         wifi->network = NULL;
526 }
527
528
529 static GSupplicantSecurity network_security(const char *security)
530 {
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;
543
544         return G_SUPPLICANT_SECURITY_UNKNOWN;
545 }
546
547 static void ssid_init(GSupplicantSSID *ssid, struct connman_network *network)
548 {
549         const char *security;
550
551         memset(ssid, 0, sizeof(*ssid));
552         ssid->ssid = connman_network_get_blob(network, "WiFi.SSID",
553                                                 &ssid->ssid_len);
554         security = connman_network_get_string(network, "WiFi.Security");
555         ssid->security = network_security(security);
556         ssid->passphrase = connman_network_get_string(network,
557                                                         "WiFi.Passphrase");
558         ssid->eap = connman_network_get_string(network, "WiFi.EAP");
559
560         /*
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.
565          */
566         if (connman_network_get_string(network,
567                                         "WiFi.PrivateKeyPassphrase") == NULL)
568                 connman_network_set_string(network,
569                                                 "WiFi.PrivateKeyPassphrase",
570                                                 ssid->passphrase);
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,
574                                                         "WiFi.CACertFile");
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");
582
583 }
584
585 static int network_connect(struct connman_network *network)
586 {
587         struct connman_device *device = connman_network_get_device(network);
588         struct wifi_data *wifi;
589         GSupplicantInterface *interface;
590         GSupplicantSSID ssid;
591
592         DBG("network %p", network);
593
594         if (device == NULL)
595                 return -ENODEV;
596
597         wifi = connman_device_get_data(device);
598         if (wifi == NULL)
599                 return -ENODEV;
600
601         interface = wifi->interface;
602
603         ssid_init(&ssid, network);
604
605         wifi->network = connman_network_ref(network);
606
607         return g_supplicant_interface_connect(interface, &ssid,
608                                                 connect_callback, NULL);
609 }
610
611 static int network_disconnect(struct connman_network *network)
612 {
613         struct connman_device *device = connman_network_get_device(network);
614         struct wifi_data *wifi;
615
616         DBG("network %p", network);
617
618         wifi = connman_device_get_data(device);
619         if (wifi == NULL || wifi->interface == NULL)
620                 return -ENODEV;
621
622         connman_network_set_associating(network, FALSE);
623
624         return g_supplicant_interface_disconnect(wifi->interface,
625                                                 disconnect_callback, wifi);
626 }
627
628 static struct connman_network_driver network_driver = {
629         .name           = "wifi",
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,
636 };
637
638 static int tech_probe(struct connman_technology *technology)
639 {
640         wifi_technology = technology;
641
642         return 0;
643 }
644
645 static void tech_remove(struct connman_technology *technology)
646 {
647         wifi_technology = NULL;
648 }
649
650 static void regdom_callback(void *user_data)
651 {
652         char *alpha2 = user_data;
653
654         DBG("");
655
656         if (wifi_technology == NULL)
657                 return;
658
659         connman_technology_regdom_notify(wifi_technology, alpha2);
660 }
661
662 static int tech_set_regdom(struct connman_technology *technology, const char *alpha2)
663 {
664         return g_supplicant_set_country(alpha2, regdom_callback, alpha2);
665 }
666
667 static struct connman_technology_driver tech_driver = {
668         .name           = "wifi",
669         .type           = CONNMAN_SERVICE_TYPE_WIFI,
670         .probe          = tech_probe,
671         .remove         = tech_remove,
672         .set_regdom     = tech_set_regdom,
673 };
674
675 static int wifi_init(void)
676 {
677         int err;
678
679         err = connman_network_driver_register(&network_driver);
680         if (err < 0)
681                 return err;
682
683         err = g_supplicant_register(&callbacks);
684         if (err < 0) {
685                 connman_network_driver_unregister(&network_driver);
686                 return err;
687         }
688
689         err = connman_technology_driver_register(&tech_driver);
690         if (err < 0) {
691                 g_supplicant_unregister(&callbacks);
692                 connman_network_driver_unregister(&network_driver);
693                 return err;
694         }
695
696         return 0;
697 }
698
699 static void wifi_exit(void)
700 {
701         DBG();
702
703         connman_technology_driver_unregister(&tech_driver);
704
705         g_supplicant_unregister(&callbacks);
706
707         connman_network_driver_unregister(&network_driver);
708 }
709
710 CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
711                 CONNMAN_PLUGIN_PRIORITY_DEFAULT, wifi_init, wifi_exit)