5 * Copyright (C) 2007 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
31 #include <sys/ioctl.h>
32 #include <sys/socket.h>
33 #include <arpa/inet.h>
35 #include <net/ethernet.h>
36 #include <linux/wireless.h>
40 #include <connman/plugin.h>
41 #include <connman/iface.h>
42 #include <connman/log.h>
44 #include "supplicant.h"
54 unsigned char wpa_ie[40];
56 unsigned char rsn_ie[40];
65 char ifname[IFNAMSIZ];
72 static void report_station(struct connman_iface *iface,
73 struct station_data *station)
80 if (station->name == NULL)
90 connman_iface_indicate_station(iface, station->name,
91 station->qual, security);
94 static struct station_data *create_station(struct iface_data *iface,
97 struct station_data *station;
100 for (list = iface->stations; list; list = list->next) {
101 station = list->data;
103 if (g_ascii_strcasecmp(station->address, address) == 0)
107 station = g_try_new0(struct station_data, 1);
111 station->address = g_strdup(address);
112 if (station->address == NULL) {
117 iface->stations = g_slist_append(iface->stations, station);
122 static void load_stations(struct iface_data *iface)
125 gchar **groups, **group;
128 keyfile = g_key_file_new();
130 if (g_key_file_load_from_file(keyfile, "/tmp/stations.list",
131 G_KEY_FILE_KEEP_COMMENTS, NULL) == FALSE)
134 groups = g_key_file_get_groups(keyfile, &length);
136 for (group = groups; *group; group++) {
137 struct station_data *station;
139 station = create_station(iface, *group);
143 station->name = g_key_file_get_string(keyfile,
144 *group, "Name", NULL);
146 station->mode = g_key_file_get_integer(keyfile,
147 *group, "Mode", NULL);
153 g_key_file_free(keyfile);
155 printf("[802.11] loaded %d stations\n",
156 g_slist_length(iface->stations));
159 static void print_stations(struct iface_data *iface)
166 keyfile = g_key_file_new();
168 for (list = iface->stations; list; list = list->next) {
169 struct station_data *station = list->data;
171 //printf("Address:%s Mode:%d ESSID:\"%s\" Quality:%d/100\n",
172 // station->address, station->mode,
173 // station->name, station->qual);
175 if (station->name == NULL)
178 g_key_file_set_string(keyfile, station->address,
179 "Name", station->name);
181 g_key_file_set_integer(keyfile, station->address,
182 "Mode", station->mode);
185 data = g_key_file_to_data(keyfile, &length, NULL);
187 g_file_set_contents("/tmp/stations.list", data, length, NULL);
189 g_key_file_free(keyfile);
192 static int wifi_probe(struct connman_iface *iface)
194 struct iface_data *data;
198 sk = socket(PF_INET, SOCK_DGRAM, 0);
202 memset(&ifr, 0, sizeof(ifr));
203 ifr.ifr_ifindex = iface->index;
205 err = ioctl(sk, SIOCGIFNAME, &ifr);
212 DBG("iface %p %s", iface, ifr.ifr_name);
214 data = malloc(sizeof(*data));
218 memset(data, 0, sizeof(*data));
220 memcpy(data->ifname, ifr.ifr_name, IFNAMSIZ);
222 iface->type = CONNMAN_IFACE_TYPE_80211;
224 iface->flags = CONNMAN_IFACE_FLAG_RTNL |
225 CONNMAN_IFACE_FLAG_IPV4 |
226 CONNMAN_IFACE_FLAG_SCANNING;
228 connman_iface_set_data(iface, data);
235 static void wifi_remove(struct connman_iface *iface)
237 struct iface_data *data = connman_iface_get_data(iface);
239 DBG("iface %p %s", iface, data->ifname);
241 __supplicant_stop(iface);
243 connman_iface_set_data(iface, NULL);
245 g_free(data->network);
246 g_free(data->passphrase);
251 static int wifi_scan(struct connman_iface *iface)
253 struct iface_data *data = connman_iface_get_data(iface);
255 struct iw_scan_req iws;
258 DBG("iface %p %s", iface, data->ifname);
260 sk = socket(PF_INET, SOCK_DGRAM, 0);
264 memset(&iws, 0, sizeof(iws));
265 iws.scan_type = IW_SCAN_TYPE_PASSIVE;
266 //iws.scan_type = IW_SCAN_TYPE_ACTIVE;
268 memset(&iwr, 0, sizeof(iwr));
269 strncpy(iwr.ifr_name, data->ifname, IFNAMSIZ);
271 iwr.u.data.pointer = (caddr_t ) &iws;
272 iwr.u.data.length = sizeof(iws);
273 iwr.u.data.flags = IW_SCAN_DEFAULT;
275 err = ioctl(sk, SIOCSIWSCAN, &iwr);
280 connman_error("%s: scan initiate error %d",
281 data->ifname, errno);
286 static int wifi_connect(struct connman_iface *iface,
287 struct connman_network *network)
289 struct iface_data *data = connman_iface_get_data(iface);
291 DBG("iface %p %s", iface, data->ifname);
293 __supplicant_start(iface);
295 if (data->network != NULL)
296 __supplicant_connect(iface, data->network, data->passphrase);
301 static int wifi_disconnect(struct connman_iface *iface)
303 struct iface_data *data = connman_iface_get_data(iface);
305 DBG("iface %p %s", iface, data->ifname);
307 if (data->network != NULL)
308 __supplicant_disconnect(iface);
310 __supplicant_stop(iface);
315 static void wifi_set_network(struct connman_iface *iface,
318 struct iface_data *data = connman_iface_get_data(iface);
320 DBG("iface %p %s", iface, data->ifname);
322 g_free(data->network);
324 data->network = g_strdup(network);
327 static void wifi_set_passphrase(struct connman_iface *iface,
328 const char *passphrase)
330 struct iface_data *data = connman_iface_get_data(iface);
332 DBG("iface %p %s", iface, data->ifname);
334 g_free(data->passphrase);
336 data->passphrase = g_strdup(passphrase);
339 static void parse_genie(struct station_data *station,
340 unsigned char *data, int len)
344 while (offset <= len - 2) {
347 switch (data[offset]) {
348 case 0xdd: /* WPA1 (and other) */
349 station->has_wpa = 1;
351 case 0x30: /* WPA2 (RSN) */
352 station->has_rsn = 1;
358 //for (i = 0; i < len; i++)
359 // printf(" %02x", data[i]);
362 offset += data[offset + 1] + 2;
366 static void parse_scan_results(struct connman_iface *iface,
367 unsigned char *data, int len)
369 unsigned char *ptr = data;
370 struct station_data *station = NULL;
371 struct ether_addr *eth;
375 while (len > IW_EV_LCP_PK_LEN) {
376 struct iw_event *event = (void *) ptr;
378 switch (event->cmd) {
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),
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);
408 station->mode = event->u.mode;
413 if (station != NULL) {
414 if (!event->u.data.pointer)
415 event->u.data.flags |= IW_ENCODE_NOKEY;
417 if (!(event->u.data.flags & IW_ENCODE_DISABLED))
418 station->has_wep = 1;
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;
432 parse_genie(station, ptr + 8, event->len - 8);
437 printf("[802.11] scan element 0x%04x (len %d)\n",
438 event->cmd, event->len);
448 report_station(iface, station);
450 printf("[802.11] found %d networks\n", num);
453 static void scan_results(struct connman_iface *iface)
455 struct iface_data *data = connman_iface_get_data(iface);
459 int sk, err, done = 0;
464 memset(&iwr, 0, sizeof(iwr));
465 memcpy(iwr.ifr_name, data->ifname, IFNAMSIZ);
467 sk = socket(PF_INET, SOCK_DGRAM, 0);
477 newbuf = g_realloc(buf, size);
478 if (newbuf == NULL) {
484 iwr.u.data.pointer = buf;
485 iwr.u.data.length = size;
486 iwr.u.data.flags = 0;
488 err = ioctl(sk, SIOCGIWSCAN, &iwr);
495 parse_scan_results(iface, iwr.u.data.pointer,
505 print_stations(data);
508 static void wifi_wireless(struct connman_iface *iface,
509 void *data, unsigned short len)
511 struct iw_event *event = data;
512 struct iw_point point;
513 struct ether_addr *eth;
516 switch (event->cmd) {
518 printf("[802.11] Set Frequency (flags %d)\n",
519 event->u.freq.flags);
522 printf("[802.11] Set Mode (mode %d)\n", event->u.mode);
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);
534 printf("[802.11] Set Encryption key (flags %d)\n",
535 event->u.data.flags);
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);
553 printf("[802.11] Wireless event (cmd 0x%04x len %d)\n",
554 event->cmd, event->len);
559 static struct connman_iface_driver wifi_driver = {
561 .capability = "net.80211",
563 .remove = wifi_remove,
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,
572 static int wifi_init(void)
574 return connman_iface_register(&wifi_driver);
577 static void wifi_exit(void)
579 connman_iface_unregister(&wifi_driver);
582 CONNMAN_PLUGIN_DEFINE("80211", "IEEE 802.11 interface plugin", VERSION,
583 wifi_init, wifi_exit)