f71cd6a9ea89f139b350b629ef3ef26c289b844d
[framework/connectivity/connman.git] / plugins / 80211.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007  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 <stdio.h>
27 #include <errno.h>
28 #include <unistd.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <sys/ioctl.h>
32 #include <sys/socket.h>
33 #include <arpa/inet.h>
34 #include <net/if.h>
35 #include <net/ethernet.h>
36 #include <linux/wireless.h>
37
38 #include <glib.h>
39
40 #include <connman/plugin.h>
41 #include <connman/iface.h>
42 #include <connman/log.h>
43
44 #include "supplicant.h"
45
46 struct station_data {
47         char *address;
48         char *name;
49         int mode;
50         int qual;
51         int noise;
52         int level;
53
54         unsigned char wpa_ie[40];
55         int wpa_ie_len;
56         unsigned char rsn_ie[40];
57         int rsn_ie_len;
58
59         int has_wep;
60         int has_wpa;
61         int has_rsn;
62 };
63
64 struct iface_data {
65         char ifname[IFNAMSIZ];
66         GSList *stations;
67
68         gchar *network;
69         gchar *passphrase;
70 };
71
72 static void report_station(struct connman_iface *iface,
73                                                 struct station_data *station)
74 {
75         int security = 0;
76
77         if (station == NULL)
78                 return;
79
80         if (station->name == NULL)
81                 return;
82
83         if (station->has_wep)
84                 security |= 0x01;
85         if (station->has_wpa)
86                 security |= 0x02;
87         if (station->has_rsn)
88                 security |= 0x04;
89
90         connman_iface_indicate_station(iface, station->name,
91                                                 station->qual, security);
92 }
93
94 static struct station_data *create_station(struct iface_data *iface,
95                                                         const char *address)
96 {
97         struct station_data *station;
98         GSList *list;
99
100         for (list = iface->stations; list; list = list->next) {
101                 station = list->data;
102
103                 if (g_ascii_strcasecmp(station->address, address) == 0)
104                         return station;
105         }
106
107         station = g_try_new0(struct station_data, 1);
108         if (station == NULL)
109                 return NULL;
110
111         station->address = g_strdup(address);
112         if (station->address == NULL) {
113                 g_free(station);
114                 return NULL;
115         }
116
117         iface->stations = g_slist_append(iface->stations, station);
118
119         return station;
120 }
121
122 static void load_stations(struct iface_data *iface)
123 {
124         GKeyFile *keyfile;
125         gchar **groups, **group;
126         gsize length;
127
128         keyfile = g_key_file_new();
129
130         if (g_key_file_load_from_file(keyfile, "/tmp/stations.list",
131                                 G_KEY_FILE_KEEP_COMMENTS, NULL) == FALSE)
132                 goto done;
133
134         groups = g_key_file_get_groups(keyfile, &length);
135
136         for (group = groups; *group; group++) {
137                 struct station_data *station;
138
139                 station = create_station(iface, *group);
140                 if (station == NULL)
141                         continue;
142
143                 station->name = g_key_file_get_string(keyfile,
144                                                 *group, "Name", NULL);
145         
146                 station->mode = g_key_file_get_integer(keyfile,
147                                                 *group, "Mode", NULL);
148         }
149
150         g_strfreev(groups);
151
152 done:
153         g_key_file_free(keyfile);
154
155         printf("[802.11] loaded %d stations\n",
156                                 g_slist_length(iface->stations));
157 }
158
159 static void print_stations(struct iface_data *iface)
160 {
161         GKeyFile *keyfile;
162         gchar *data;
163         gsize length;
164         GSList *list;
165
166         keyfile = g_key_file_new();
167
168         for (list = iface->stations; list; list = list->next) {
169                 struct station_data *station = list->data;
170
171                 //printf("Address:%s Mode:%d ESSID:\"%s\" Quality:%d/100\n",
172                 //                      station->address, station->mode,
173                 //                              station->name, station->qual);
174
175                 if (station->name == NULL)
176                         continue;
177
178                 g_key_file_set_string(keyfile, station->address,
179                                                 "Name", station->name);
180
181                 g_key_file_set_integer(keyfile, station->address,
182                                                 "Mode", station->mode);
183         }
184
185         data = g_key_file_to_data(keyfile, &length, NULL);
186
187         g_file_set_contents("/tmp/stations.list", data, length, NULL);
188
189         g_key_file_free(keyfile);
190 }
191
192 static int wifi_probe(struct connman_iface *iface)
193 {
194         struct iface_data *data;
195         struct ifreq ifr;
196         int sk, err;
197
198         sk = socket(PF_INET, SOCK_DGRAM, 0);
199         if (sk < 0)
200                 return -EIO;
201
202         memset(&ifr, 0, sizeof(ifr));
203         ifr.ifr_ifindex = iface->index;
204
205         err = ioctl(sk, SIOCGIFNAME, &ifr);
206
207         close(sk);
208
209         if (err < 0)
210                 return -EIO;
211
212         DBG("iface %p %s", iface, ifr.ifr_name);
213
214         data = malloc(sizeof(*data));
215         if (data == NULL)
216                 return -ENOMEM;
217
218         memset(data, 0, sizeof(*data));
219
220         memcpy(data->ifname, ifr.ifr_name, IFNAMSIZ);
221
222         iface->type = CONNMAN_IFACE_TYPE_80211;
223
224         iface->flags = CONNMAN_IFACE_FLAG_RTNL |
225                                 CONNMAN_IFACE_FLAG_IPV4 |
226                                 CONNMAN_IFACE_FLAG_SCANNING;
227
228         connman_iface_set_data(iface, data);
229
230         load_stations(data);
231
232         return 0;
233 }
234
235 static void wifi_remove(struct connman_iface *iface)
236 {
237         struct iface_data *data = connman_iface_get_data(iface);
238
239         DBG("iface %p %s", iface, data->ifname);
240
241         __supplicant_stop(iface);
242
243         connman_iface_set_data(iface, NULL);
244
245         g_free(data->network);
246         g_free(data->passphrase);
247
248         free(data);
249 }
250
251 static int wifi_scan(struct connman_iface *iface)
252 {
253         struct iface_data *data = connman_iface_get_data(iface);
254         struct iwreq iwr;
255         struct iw_scan_req iws;
256         int sk, err;
257
258         DBG("iface %p %s", iface, data->ifname);
259
260         sk = socket(PF_INET, SOCK_DGRAM, 0);
261         if (sk < 0)
262                 return -EIO;
263
264         memset(&iws, 0, sizeof(iws));
265         iws.scan_type = IW_SCAN_TYPE_PASSIVE;
266         //iws.scan_type = IW_SCAN_TYPE_ACTIVE;
267
268         memset(&iwr, 0, sizeof(iwr));
269         strncpy(iwr.ifr_name, data->ifname, IFNAMSIZ);
270
271         iwr.u.data.pointer = (caddr_t ) &iws;
272         iwr.u.data.length = sizeof(iws);
273         iwr.u.data.flags = IW_SCAN_DEFAULT;
274
275         err = ioctl(sk, SIOCSIWSCAN, &iwr);
276
277         close(sk);
278
279         if (err < 0)
280                 connman_error("%s: scan initiate error %d",
281                                                 data->ifname, errno);
282
283         return err;
284 }
285
286 static int wifi_connect(struct connman_iface *iface,
287                                         struct connman_network *network)
288 {
289         struct iface_data *data = connman_iface_get_data(iface);
290
291         DBG("iface %p %s", iface, data->ifname);
292
293         __supplicant_start(iface);
294
295         if (data->network != NULL)
296                 __supplicant_connect(iface, data->network, data->passphrase);
297
298         return 0;
299 }
300
301 static int wifi_disconnect(struct connman_iface *iface)
302 {
303         struct iface_data *data = connman_iface_get_data(iface);
304
305         DBG("iface %p %s", iface, data->ifname);
306
307         if (data->network != NULL)
308                 __supplicant_disconnect(iface);
309
310         __supplicant_stop(iface);
311
312         return 0;
313 }
314
315 static void wifi_set_network(struct connman_iface *iface,
316                                                 const char *network)
317 {
318         struct iface_data *data = connman_iface_get_data(iface);
319
320         DBG("iface %p %s", iface, data->ifname);
321
322         g_free(data->network);
323
324         data->network = g_strdup(network);
325 }
326
327 static void wifi_set_passphrase(struct connman_iface *iface,
328                                                 const char *passphrase)
329 {
330         struct iface_data *data = connman_iface_get_data(iface);
331
332         DBG("iface %p %s", iface, data->ifname);
333
334         g_free(data->passphrase);
335
336         data->passphrase = g_strdup(passphrase);
337 }
338
339 static void parse_genie(struct station_data *station,
340                                         unsigned char *data, int len)
341 {
342         int offset = 0;
343
344         while (offset <= len - 2) {
345                 //int i;
346
347                 switch (data[offset]) {
348                 case 0xdd:      /* WPA1 (and other) */
349                         station->has_wpa = 1;
350                         break;
351                 case 0x30:      /* WPA2 (RSN) */
352                         station->has_rsn = 1;
353                         break;
354                 default:
355                         break;
356                 }
357
358                 //for (i = 0; i < len; i++)
359                 //      printf(" %02x", data[i]);
360                 //printf("\n");
361
362                 offset += data[offset + 1] + 2;
363         }
364 }
365
366 static void parse_scan_results(struct connman_iface *iface,
367                                         unsigned char *data, int len)
368 {
369         unsigned char *ptr = data;
370         struct station_data *station = NULL;
371         struct ether_addr *eth;
372         char addr[18];
373         int num = 0;
374
375         while (len > IW_EV_LCP_PK_LEN) {
376                 struct iw_event *event = (void *) ptr;
377
378                 switch (event->cmd) {
379                 case SIOCGIWAP:
380                         report_station(iface, station);
381                         eth = (void *) &event->u.ap_addr.sa_data;
382                         sprintf(addr, "%02X:%02X:%02X:%02X:%02X:%02X",
383                                                 eth->ether_addr_octet[0],
384                                                 eth->ether_addr_octet[1],
385                                                 eth->ether_addr_octet[2],
386                                                 eth->ether_addr_octet[3],
387                                                 eth->ether_addr_octet[4],
388                                                 eth->ether_addr_octet[5]);
389                         station = create_station(connman_iface_get_data(iface),
390                                                                         addr);
391                         num++;
392                         break;
393                 case SIOCGIWESSID:
394                         if (station != NULL) {
395                                 station->name = malloc(event->len - IW_EV_POINT_LEN + 1);
396                                 if (station->name != NULL) {
397                                         memset(station->name, 0,
398                                                 event->len - IW_EV_POINT_LEN + 1);
399                                         memcpy(station->name, ptr + IW_EV_POINT_LEN,
400                                                 event->len - IW_EV_POINT_LEN);
401                                 }
402                         }
403                         break;
404                 case SIOCGIWNAME:
405                         break;
406                 case SIOCGIWMODE:
407                         if (station != NULL)
408                                 station->mode = event->u.mode;
409                         break;
410                 case SIOCGIWFREQ:
411                         break;
412                 case SIOCGIWENCODE:
413                         if (station != NULL) {
414                                 if (!event->u.data.pointer)
415                                         event->u.data.flags |= IW_ENCODE_NOKEY;
416
417                                 if (!(event->u.data.flags & IW_ENCODE_DISABLED))
418                                         station->has_wep = 1;
419                         }
420                         break;
421                 case SIOCGIWRATE:
422                         break;
423                 case IWEVQUAL:
424                         if (station != NULL) {
425                                 station->qual = event->u.qual.qual;
426                                 station->noise = event->u.qual.noise;
427                                 station->level = event->u.qual.level;
428                         }
429                         break;
430                 case IWEVGENIE:
431                         if (station != NULL)
432                                 parse_genie(station, ptr + 8, event->len - 8);
433                         break;
434                 case IWEVCUSTOM:
435                         break;
436                 default:
437                         printf("[802.11] scan element 0x%04x (len %d)\n",
438                                                 event->cmd, event->len);
439                         if (event->len == 0)
440                                 len = 0;
441                         break;
442                 }
443
444                 ptr += event->len;
445                 len -= event->len;
446         }
447
448         report_station(iface, station);
449
450         printf("[802.11] found %d networks\n", num);
451 }
452
453 static void scan_results(struct connman_iface *iface)
454 {
455         struct iface_data *data = connman_iface_get_data(iface);
456         struct iwreq iwr;
457         void *buf;
458         size_t size;
459         int sk, err, done = 0;
460
461         if (data == NULL)
462                 return;
463
464         memset(&iwr, 0, sizeof(iwr));
465         memcpy(iwr.ifr_name, data->ifname, IFNAMSIZ);
466
467         sk = socket(PF_INET, SOCK_DGRAM, 0);
468         if (sk < 0)
469                 return;
470
471         buf = NULL;
472         size = 1024;
473
474         while (!done) {
475                 void *newbuf;
476
477                 newbuf = g_realloc(buf, size);
478                 if (newbuf == NULL) {
479                         close(sk);
480                         return;
481                 }
482
483                 buf = newbuf;
484                 iwr.u.data.pointer = buf;
485                 iwr.u.data.length = size;
486                 iwr.u.data.flags = 0;
487
488                 err = ioctl(sk, SIOCGIWSCAN, &iwr);
489                 if (err < 0) {
490                         if (errno == E2BIG)
491                                 size *= 2;
492                         else
493                                 done = 1;
494                 } else {
495                         parse_scan_results(iface, iwr.u.data.pointer,
496                                                         iwr.u.data.length);
497                         done = 1;
498                 }
499         }
500
501         g_free(buf);
502
503         close(sk);
504
505         print_stations(data);
506 }
507
508 static void wifi_wireless(struct connman_iface *iface,
509                                         void *data, unsigned short len)
510 {
511         struct iw_event *event = data;
512         struct iw_point point;
513         struct ether_addr *eth;
514         char addr[18];
515
516         switch (event->cmd) {
517         case SIOCSIWFREQ:
518                 printf("[802.11] Set Frequency (flags %d)\n",
519                                                         event->u.freq.flags);
520                 break;
521         case SIOCSIWMODE:
522                 printf("[802.11] Set Mode (mode %d)\n", event->u.mode);
523                 break;
524         case SIOCSIWESSID:
525                 memcpy(&point, data + IW_EV_LCP_LEN -
526                                         IW_EV_POINT_OFF, sizeof(point));
527                 point.pointer = data + IW_EV_LCP_LEN +
528                                         sizeof(point) - IW_EV_POINT_OFF;
529                 printf("[802.11] Set ESSID (length %d flags %d) \"%s\"\n",
530                                         point.length, point.flags,
531                                                 (char *) point.pointer);
532                 break;
533         case SIOCSIWENCODE:
534                 printf("[802.11] Set Encryption key (flags %d)\n",
535                                                         event->u.data.flags);
536                 break;
537
538         case SIOCGIWAP:
539                 eth = (void *) &event->u.ap_addr.sa_data;
540                 sprintf(addr, "%02X:%02X:%02X:%02X:%02X:%02X",
541                                                 eth->ether_addr_octet[0],
542                                                 eth->ether_addr_octet[1],
543                                                 eth->ether_addr_octet[2],
544                                                 eth->ether_addr_octet[3],
545                                                 eth->ether_addr_octet[4],
546                                                 eth->ether_addr_octet[5]);
547                 printf("[802.11] New Access Point %s\n", addr);
548                 break;
549         case SIOCGIWSCAN:
550                 scan_results(iface);
551                 break;
552         default:
553                 printf("[802.11] Wireless event (cmd 0x%04x len %d)\n",
554                                                 event->cmd, event->len);
555                 break;
556         }
557 }
558
559 static struct connman_iface_driver wifi_driver = {
560         .name           = "80211",
561         .capability     = "net.80211",
562         .probe          = wifi_probe,
563         .remove         = wifi_remove,
564         .scan           = wifi_scan,
565         .connect        = wifi_connect,
566         .disconnect     = wifi_disconnect,
567         .set_network    = wifi_set_network,
568         .set_passphrase = wifi_set_passphrase,
569         .rtnl_wireless  = wifi_wireless,
570 };
571
572 static int wifi_init(void)
573 {
574         return connman_iface_register(&wifi_driver);
575 }
576
577 static void wifi_exit(void)
578 {
579         connman_iface_unregister(&wifi_driver);
580 }
581
582 CONNMAN_PLUGIN_DEFINE("80211", "IEEE 802.11 interface plugin", VERSION,
583                                                         wifi_init, wifi_exit)