[contrib] introduce libiwinfo, splitted apart from LuCI for general usage
[project/luci.git] / contrib / package / iwinfo / src / iwinfo_nl80211.c
1 /*
2  * iwinfo - Wireless Information Library - NL80211 Backend
3  *
4  *   Copyright (C) 2010 Jo-Philipp Wich <xm@subsignal.org>
5  *
6  * The iwinfo library is free software: you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License version 2
8  * as published by the Free Software Foundation.
9  *
10  * The iwinfo library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with the iwinfo library. If not, see http://www.gnu.org/licenses/.
17  *
18  * The signal handling code is derived from the official madwifi tools,
19  * wlanconfig.c in particular. The encryption property handling was
20  * inspired by the hostapd madwifi driver.
21  *
22  * Parts of this code are derived from the Linux iw utility.
23  */
24
25 #include "iwinfo_nl80211.h"
26 #include "iwinfo_wext.h"
27
28 #define min(x, y) ((x) < (y)) ? (x) : (y)
29
30 static struct nl80211_state *nls = NULL;
31
32 static int nl80211_init(void)
33 {
34         int err;
35
36         if( !nls )
37         {
38                 nls = malloc(sizeof(struct nl80211_state));
39                 if( !nls ) {
40                         err = -ENOMEM;
41                         goto err;
42                 }
43
44                 nls->nl_sock = nl_socket_alloc();
45                 if( !nls->nl_sock ) {
46                         err = -ENOMEM;
47                         goto err;
48                 }
49
50                 if( genl_connect(nls->nl_sock)) {
51                         err = -ENOLINK;
52                         goto err;
53                 }
54
55                 if( genl_ctrl_alloc_cache(nls->nl_sock, &nls->nl_cache)) {
56                         err = -ENOMEM;
57                         goto err;
58                 }
59
60                 nls->nl80211 = genl_ctrl_search_by_name(nls->nl_cache, "nl80211");
61                 if( !nls->nl80211 )
62                 {
63                         err = -ENOENT;
64                         goto err;
65                 }
66         }
67
68         return 0;
69
70
71 err:
72         if( nls && nls->nl_sock )
73                 nl_socket_free(nls->nl_sock);
74
75         if( nls && nls->nl_cache )
76                 nl_cache_free(nls->nl_cache);
77
78         if( nls )
79                 free(nls);
80
81         nls = NULL;
82
83         return err;
84 }
85
86 static int nl80211_msg_error(struct sockaddr_nl *nla,
87         struct nlmsgerr *err, void *arg)
88 {
89         int *ret = arg;
90         *ret = err->error;
91         return NL_STOP;
92 }
93
94 static int nl80211_msg_finish(struct nl_msg *msg, void *arg)
95 {
96         int *ret = arg;
97         *ret = 0;
98         return NL_SKIP;
99 }
100
101 static int nl80211_msg_ack(struct nl_msg *msg, void *arg)
102 {
103         int *ret = arg;
104         *ret = 0;
105         return NL_STOP;
106 }
107
108 static int nl80211_msg_response(struct nl_msg *msg, void *arg)
109 {
110         struct nl80211_msg_conveyor *cv = arg;
111
112         nlmsg_get(msg);
113
114         cv->msg = msg;
115         cv->hdr = nlmsg_data(nlmsg_hdr(cv->msg));
116
117         nla_parse(cv->attr, NL80211_ATTR_MAX,
118                 genlmsg_attrdata(cv->hdr, 0),
119                 genlmsg_attrlen(cv->hdr, 0), NULL);
120
121         return NL_SKIP;
122 }
123
124 static void nl80211_free(struct nl80211_msg_conveyor *cv)
125 {
126         if( cv && cv->cb )
127                 nl_cb_put(cv->cb);
128
129         if( cv && cv->msg )
130                 nlmsg_free(cv->msg);
131 }
132
133 static struct nl80211_msg_conveyor * nl80211_msg(const char *ifname, int cmd, int flags)
134 {
135         static struct nl80211_msg_conveyor cv;
136
137         int ifidx;
138         struct nl_msg *req = NULL;
139         struct nl_cb *cb = NULL;
140
141         if( nl80211_init() < 0 )
142                 goto err;
143
144         if( !strncmp(ifname, "mon.", 4) )
145                 ifidx = if_nametoindex(&ifname[4]);
146         else
147                 ifidx = if_nametoindex(ifname);
148
149         if( ifidx < 0 )
150                 return NULL;
151
152         req = nlmsg_alloc();
153         if( !req )
154                 goto err;
155
156         cb = nl_cb_alloc(NL_CB_DEFAULT);
157         if( !cb )
158                 goto err;
159
160         genlmsg_put(req, 0, 0, genl_family_get_id(nls->nl80211), 0,
161                 flags, cmd, 0);
162
163         NLA_PUT_U32(req, NL80211_ATTR_IFINDEX, ifidx);
164
165         nlmsg_get(req);
166
167         cv.msg       = req;
168         cv.cb        = cb;
169         cv.custom_cb = 0;
170
171         return &cv;
172
173 err:
174 nla_put_failure:
175         if( cb )
176                 nl_cb_put(cb);
177
178         if( req )
179                 nlmsg_free(req);
180
181         return NULL;
182 }
183
184 static void nl80211_cb(struct nl80211_msg_conveyor *cv,
185         int (*cb)(struct nl_msg *, void *), void *arg)
186 {
187         cv->custom_cb = 1;
188         nl_cb_set(cv->cb, NL_CB_VALID, NL_CB_CUSTOM, cb, arg);
189 }
190
191 static struct nl80211_msg_conveyor * nl80211_send(struct nl80211_msg_conveyor *cv)
192 {
193         static struct nl80211_msg_conveyor rcv;
194         int err = 1;
195
196         if( !cv->custom_cb )
197                 nl_cb_set(cv->cb, NL_CB_VALID, NL_CB_CUSTOM, nl80211_msg_response, &rcv);
198
199         if( nl_send_auto_complete(nls->nl_sock, cv->msg) < 0 )
200                 goto err;
201
202         nl_cb_err(cv->cb,               NL_CB_CUSTOM, nl80211_msg_error,  &err);
203         nl_cb_set(cv->cb, NL_CB_FINISH, NL_CB_CUSTOM, nl80211_msg_finish, &err);
204         nl_cb_set(cv->cb, NL_CB_ACK,    NL_CB_CUSTOM, nl80211_msg_ack,    &err);
205
206         while (err > 0)
207                 nl_recvmsgs(nls->nl_sock, cv->cb);
208
209         return &rcv;
210
211 err:
212         nl_cb_put(cv->cb);
213         nlmsg_free(cv->msg);
214
215         return NULL;
216 }
217
218 static int nl80211_freq2channel(int freq)
219 {
220     if (freq == 2484)
221         return 14;
222
223     if (freq < 2484)
224         return (freq - 2407) / 5;
225
226     return (freq / 5) - 1000;
227 }
228
229 static char * nl80211_getval(const char *buf, const char *key)
230 {
231         int i, len;
232         char lkey[64] = { 0 };
233         const char *ln = buf;
234         static char lval[256] = { 0 };
235
236         for( i = 0, len = strlen(buf); i < len; i++ )
237         {
238                 if( !lkey[0] && (buf[i] == ' ' || buf[i] == '\t') )
239                 {
240                         ln++;
241                 }
242                 else if( !lkey[0] && (buf[i] == '=') )
243                 {
244                         if( (&buf[i] - ln) > 0 )
245                                 memcpy(lkey, ln, min(sizeof(lkey) - 1, &buf[i] - ln));
246                 }
247                 else if( buf[i] == '\n' )
248                 {
249                         if( lkey[0] && !strcmp(lkey, key) )
250                         {
251                                 memcpy(lval, ln + strlen(lkey) + 1,
252                                         min(sizeof(lval) - 1, &buf[i] - ln - strlen(lkey) - 1));
253
254                                 return lval;
255                         }
256
257                         ln = &buf[i+1];
258                         memset(lkey, 0, sizeof(lkey));
259                         memset(lval, 0, sizeof(lval));
260                 }
261         }
262
263         return NULL;
264 }
265
266 static char * nl80211_ifname2phy(const char *ifname)
267 {
268         static char phy[32] = { 0 };
269         struct nl80211_msg_conveyor *req, *res;
270
271         req = nl80211_msg(ifname, NL80211_CMD_GET_WIPHY, 0);
272         if( req )
273         {
274                 res = nl80211_send(req);
275                 if( res )
276                 {
277                         if( res->attr[NL80211_ATTR_WIPHY_NAME] )
278                         {
279                                 snprintf(phy, sizeof(phy), "%s",
280                                          nla_get_string(res->attr[NL80211_ATTR_WIPHY_NAME]));
281                         }
282                         nl80211_free(res);
283                 }
284                 nl80211_free(req);
285         }
286
287         return phy[0] ? phy : NULL;
288 }
289
290 static char * nl80211_hostapd_info(const char *ifname)
291 {
292         char *phy;
293         char path[32] = { 0 };
294         static char buf[4096] = { 0 };
295         FILE *conf;
296
297         if( (phy = nl80211_ifname2phy(ifname)) != NULL )
298         {
299                 snprintf(path, sizeof(path), "/var/run/hostapd-%s.conf", phy);
300
301                 if( (conf = fopen(path, "r")) != NULL )
302                 {
303                         fread(buf, sizeof(buf) - 1, 1, conf);
304                         fclose(conf);
305
306                         return buf;
307                 }
308         }
309
310         return NULL;
311 }
312
313 static char * nl80211_wpasupp_info(const char *ifname, const char *cmd)
314 {
315         int sock = -1, len;
316         char *rv = NULL;
317         size_t remote_length, local_length;
318         static char buffer[1024] = { 0 };
319
320         struct timeval tv = { 2, 0 };
321         struct sockaddr_un local = { 0 };
322         struct sockaddr_un remote = { 0 };
323
324         fd_set rfds;
325
326         sock = socket(PF_UNIX, SOCK_DGRAM, 0);
327         if( sock < 0 )
328                 return NULL;
329
330         remote.sun_family = AF_UNIX;
331         remote_length = sizeof(remote.sun_family) + sprintf(remote.sun_path,
332                 "/var/run/wpa_supplicant-%s/%s", ifname, ifname);
333
334         if( connect(sock, (struct sockaddr *) &remote, remote_length) )
335                 goto out;
336
337         local.sun_family = AF_UNIX;
338         local_length = sizeof(local.sun_family) + sprintf(local.sun_path,
339                 "/var/run/iwinfo-%s-%d", ifname, getpid());
340
341         if( bind(sock, (struct sockaddr *) &local, local_length) )
342                 goto out;
343
344         send(sock, cmd, strlen(cmd), 0);
345
346         while( 1 )
347         {
348                 FD_ZERO(&rfds);
349                 FD_SET(sock, &rfds);
350
351                 if( select(sock + 1, &rfds, NULL, NULL, &tv) < 0 )
352                         goto out;
353
354                 if( !FD_ISSET(sock, &rfds) )
355                         break;
356
357                 if( (len = recv(sock, buffer, sizeof(buffer), 0)) <= 0 )
358                         goto out;
359
360                 buffer[len] = 0;
361
362                 if( buffer[0] != '<' )
363                         break;
364         }
365
366         rv = buffer;
367
368 out:
369         close(sock);
370         unlink(local.sun_path);
371
372         return rv;
373 }
374
375
376 int nl80211_probe(const char *ifname)
377 {
378         return !!nl80211_ifname2phy(ifname);
379 }
380
381 int nl80211_get_mode(const char *ifname, char *buf)
382 {
383         return wext_get_mode(ifname, buf);
384 }
385
386 int nl80211_get_ssid(const char *ifname, char *buf)
387 {
388         char *ssid;
389
390         if( !wext_get_ssid(ifname, buf) )
391         {
392                 return 0;
393         }
394         else if( (ssid = nl80211_hostapd_info(ifname)) &&
395                  (ssid = nl80211_getval(ssid, "ssid")) )
396         {
397                 memcpy(buf, ssid, strlen(ssid));
398                 return 0;
399         }
400
401         return -1;
402 }
403
404 int nl80211_get_bssid(const char *ifname, char *buf)
405 {
406         char *bssid;
407         unsigned char mac[6];
408
409         if( !wext_get_bssid(ifname, buf) )
410         {
411                 return 0;
412         }
413         else if( (bssid = nl80211_hostapd_info(ifname)) &&
414                  (bssid = nl80211_getval(bssid, "bssid")) )
415         {
416                 mac[0] = strtol(&bssid[0],  NULL, 16);
417                 mac[1] = strtol(&bssid[3],  NULL, 16);
418                 mac[2] = strtol(&bssid[6],  NULL, 16);
419                 mac[3] = strtol(&bssid[9],  NULL, 16);
420                 mac[4] = strtol(&bssid[12], NULL, 16);
421                 mac[5] = strtol(&bssid[15], NULL, 16);
422
423                 sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
424                         mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
425
426                 return 0;
427         }
428
429         return -1;
430 }
431
432 int nl80211_get_channel(const char *ifname, int *buf)
433 {
434         return wext_get_channel(ifname, buf);
435 }
436
437 int nl80211_get_frequency(const char *ifname, int *buf)
438 {
439         return wext_get_frequency(ifname, buf);
440 }
441
442 int nl80211_get_txpower(const char *ifname, int *buf)
443 {
444         return wext_get_txpower(ifname, buf);
445 }
446
447
448 static int nl80211_get_signal_cb(struct nl_msg *msg, void *arg)
449 {
450         int8_t dbm;
451         int16_t mbit;
452         struct nl80211_rssi_rate *rr = arg;
453
454         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
455         struct nlattr *attr[NL80211_ATTR_MAX + 1];
456         struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
457         struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
458
459         static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
460                 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32    },
461                 [NL80211_STA_INFO_RX_BYTES]      = { .type = NLA_U32    },
462                 [NL80211_STA_INFO_TX_BYTES]      = { .type = NLA_U32    },
463                 [NL80211_STA_INFO_RX_PACKETS]    = { .type = NLA_U32    },
464                 [NL80211_STA_INFO_TX_PACKETS]    = { .type = NLA_U32    },
465                 [NL80211_STA_INFO_SIGNAL]        = { .type = NLA_U8     },
466                 [NL80211_STA_INFO_TX_BITRATE]    = { .type = NLA_NESTED },
467                 [NL80211_STA_INFO_LLID]          = { .type = NLA_U16    },
468                 [NL80211_STA_INFO_PLID]          = { .type = NLA_U16    },
469                 [NL80211_STA_INFO_PLINK_STATE]   = { .type = NLA_U8     },
470         };
471
472         static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
473                 [NL80211_RATE_INFO_BITRATE]      = { .type = NLA_U16  },
474                 [NL80211_RATE_INFO_MCS]          = { .type = NLA_U8   },
475                 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
476                 [NL80211_RATE_INFO_SHORT_GI]     = { .type = NLA_FLAG },
477         };
478
479         nla_parse(attr, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
480                   genlmsg_attrlen(gnlh, 0), NULL);
481
482         if( attr[NL80211_ATTR_STA_INFO] )
483         {
484                 if( !nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
485                                 attr[NL80211_ATTR_STA_INFO], stats_policy) )
486                 {
487                         if( sinfo[NL80211_STA_INFO_SIGNAL] )
488                         {
489                                 dbm = nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
490                                 rr->rssi = rr->rssi ? (int8_t)((rr->rssi + dbm) / 2) : dbm;
491                         }
492
493                         if( sinfo[NL80211_STA_INFO_TX_BITRATE] )
494                         {
495                                 if( !nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
496                                                 sinfo[NL80211_STA_INFO_TX_BITRATE], rate_policy) )
497                                 {
498                                         if( rinfo[NL80211_RATE_INFO_BITRATE] )
499                                         {
500                                                 mbit = nla_get_u16(rinfo[NL80211_RATE_INFO_BITRATE]);
501                                                 rr->rate = rr->rate
502                                                         ? (int16_t)((rr->rate + mbit) / 2) : mbit;
503                                         }
504                                 }
505                         }
506                 }
507         }
508
509         return NL_SKIP;
510 }
511
512 int nl80211_get_bitrate(const char *ifname, int *buf)
513 {
514         struct nl80211_rssi_rate rr;
515         struct nl80211_msg_conveyor *req;
516
517         if( !wext_get_bitrate(ifname, buf) )
518                 return 0;
519
520         req = nl80211_msg(ifname, NL80211_CMD_GET_STATION, NLM_F_DUMP);
521         if( req )
522         {
523                 rr.rssi = 0;
524                 rr.rate = 0;
525
526                 nl80211_cb(req, nl80211_get_signal_cb, &rr);
527                 nl80211_send(req);
528                 nl80211_free(req);
529
530                 if( rr.rate )
531                 {
532                         *buf = (rr.rate * 100);
533                         return 0;
534                 }
535         }
536
537         return -1;
538 }
539
540 int nl80211_get_signal(const char *ifname, int *buf)
541 {
542         struct nl80211_rssi_rate rr;
543         struct nl80211_msg_conveyor *req;
544
545         if( !wext_get_signal(ifname, buf) )
546                 return 0;
547
548         req = nl80211_msg(ifname, NL80211_CMD_GET_STATION, NLM_F_DUMP);
549         if( req )
550         {
551                 rr.rssi = 0;
552                 rr.rate = 0;
553
554                 nl80211_cb(req, nl80211_get_signal_cb, &rr);
555                 nl80211_send(req);
556                 nl80211_free(req);
557
558                 if( rr.rssi )
559                 {
560                         *buf = rr.rssi;
561                         return 0;
562                 }
563         }
564
565         return -1;
566 }
567
568 int nl80211_get_noise(const char *ifname, int *buf)
569 {
570         struct nl80211_msg_conveyor *req, *res;
571         struct nlattr *si[NL80211_SURVEY_INFO_MAX + 1];
572
573         static struct nla_policy sp[NL80211_SURVEY_INFO_MAX + 1] = {
574                 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
575                 [NL80211_SURVEY_INFO_NOISE]     = { .type = NLA_U8  },
576         };
577
578         req = nl80211_msg(ifname, NL80211_CMD_GET_SURVEY, NLM_F_DUMP);
579         if( req )
580         {
581                 res = nl80211_send(req);
582                 if( res )
583                 {
584                         if( res->attr[NL80211_ATTR_SURVEY_INFO] )
585                         {
586                                 if( !nla_parse_nested(si, NL80211_SURVEY_INFO_MAX,
587                                                 res->attr[NL80211_ATTR_SURVEY_INFO], sp) )
588                                 {
589                                         *buf = (int8_t)nla_get_u8(si[NL80211_SURVEY_INFO_NOISE]);
590                                         return 0;
591                                 }
592                         }
593                         nl80211_free(res);
594                 }
595                 nl80211_free(req);
596         }
597
598         return -1;
599 }
600
601 int nl80211_get_quality(const char *ifname, int *buf)
602 {
603         int signal;
604
605         if( wext_get_quality(ifname, buf) )
606         {
607                 *buf = 0;
608
609                 if( !nl80211_get_signal(ifname, &signal) )
610                 {
611                         /* A positive signal level is usually just a quality
612                          * value, pass through as-is */
613                         if( signal >= 0 )
614                         {
615                                 *buf = signal;
616                         }
617
618                         /* The cfg80211 wext compat layer assumes a signal range
619                          * of -110 dBm to -40 dBm, the quality value is derived
620                          * by adding 110 to the signal level */
621                         else
622                         {
623                                 if( signal < -110 )
624                                         signal = -110;
625                                 else if( signal > -40 )
626                                         signal = -40;
627
628                                 *buf = (signal + 110);
629                         }
630                 }
631         }
632
633         return 0;
634 }
635
636 int nl80211_get_quality_max(const char *ifname, int *buf)
637 {
638         if( wext_get_quality_max(ifname, buf) )
639                 /* The cfg80211 wext compat layer assumes a maximum
640                  * quality of 70 */
641                 *buf = 70;
642
643         return 0;
644 }
645
646 int nl80211_get_encryption(const char *ifname, char *buf)
647 {
648         int i;
649         char k[9];
650         char *val, *res;
651         struct iwinfo_crypto_entry *c = (struct iwinfo_crypto_entry *)buf;
652
653         /* Hostapd */
654         if( (res = nl80211_hostapd_info(ifname)) &&
655             nl80211_getval(res, "interface") )
656         {
657                 if( (val = nl80211_getval(res, "auth_algs")) && (val > 0) )
658                 {
659                         c->auth_suites |= IWINFO_KMGMT_NONE;
660
661                         switch(atoi(val)) {
662                                 case 1:
663                                         c->auth_algs |= IWINFO_AUTH_OPEN;
664                                         break;
665
666                                 case 2:
667                                         c->auth_algs |= IWINFO_AUTH_SHARED;
668                                         break;
669
670                                 case 3:
671                                         c->auth_algs |= IWINFO_AUTH_OPEN;
672                                         c->auth_algs |= IWINFO_AUTH_SHARED;
673                                         break;
674
675                                 default:
676                                         break;
677                         }
678
679                         for( i = 0; i < 4; i++ )
680                         {
681                                 snprintf(k, sizeof(k), "wep_key%d", i);
682
683                                 if( (val = nl80211_getval(res, k)) )
684                                 {
685                                         if( (strlen(val) == 5) || (strlen(val) == 10) )
686                                                 c->pair_ciphers |= IWINFO_CIPHER_WEP40;
687
688                                         else if( (strlen(val) == 13) || (strlen(val) == 26) )
689                                                 c->pair_ciphers |= IWINFO_CIPHER_WEP104;
690                                 }
691                         }
692
693                         c->group_ciphers = c->pair_ciphers;
694
695                         return 0;
696                 }
697
698
699                 if( (val = nl80211_getval(res, "wpa")) != NULL )
700                         c->wpa_version = atoi(val);
701
702
703                 val = nl80211_getval(res, "wpa_key_mgmt");
704
705                 if( !val || strstr(val, "PSK") )
706                         c->auth_suites |= IWINFO_KMGMT_PSK;
707
708                 if( val && strstr(val, "EAP") )
709                         c->auth_suites |= IWINFO_KMGMT_8021x;
710
711                 if( val && strstr(val, "NONE") )
712                         c->auth_suites |= IWINFO_KMGMT_NONE;
713
714
715                 if( (val = nl80211_getval(res, "wpa_pairwise")) != NULL )
716                 {
717                         if( strstr(val, "TKIP") )
718                                 c->pair_ciphers |= IWINFO_CIPHER_TKIP;
719
720                         if( strstr(val, "CCMP") )
721                                 c->pair_ciphers |= IWINFO_CIPHER_CCMP;
722
723                         if( strstr(val, "NONE") )
724                                 c->pair_ciphers |= IWINFO_CIPHER_NONE;
725                 }
726
727
728                 c->group_ciphers = c->pair_ciphers;
729                 c->enabled = (c->auth_algs || c->auth_suites) ? 1 : 0;
730
731                 return 0;
732         }
733
734         /* WPA supplicant */
735         else if( (res = nl80211_wpasupp_info(ifname, "STATUS")) &&
736                  (val = nl80211_getval(res, "pairwise_cipher")) )
737         {
738                 /* WEP */
739                 if( strstr(val, "WEP") )
740                 {
741                         if( strstr(val, "WEP-40") )
742                                 c->pair_ciphers |= IWINFO_CIPHER_WEP40;
743
744                         else if( strstr(val, "WEP-104") )
745                                 c->pair_ciphers |= IWINFO_CIPHER_WEP104;
746
747                         c->enabled       = 1;
748                         c->group_ciphers = c->pair_ciphers;
749
750                         c->auth_suites |= IWINFO_KMGMT_NONE;
751                         c->auth_algs   |= IWINFO_AUTH_OPEN; /* XXX: assumption */
752                 }
753
754                 /* WPA */
755                 else
756                 {
757                         if( strstr(val, "TKIP") )
758                                 c->pair_ciphers |= IWINFO_CIPHER_TKIP;
759
760                         else if( strstr(val, "CCMP") )
761                                 c->pair_ciphers |= IWINFO_CIPHER_CCMP;
762
763                         else if( strstr(val, "NONE") )
764                                 c->pair_ciphers |= IWINFO_CIPHER_NONE;
765
766                         else if( strstr(val, "WEP-40") )
767                                 c->pair_ciphers |= IWINFO_CIPHER_WEP40;
768
769                         else if( strstr(val, "WEP-104") )
770                                 c->pair_ciphers |= IWINFO_CIPHER_WEP104;
771
772
773                         if( (val = nl80211_getval(res, "group_cipher")) )
774                         {
775                                 if( strstr(val, "TKIP") )
776                                         c->group_ciphers |= IWINFO_CIPHER_TKIP;
777
778                                 else if( strstr(val, "CCMP") )
779                                         c->group_ciphers |= IWINFO_CIPHER_CCMP;
780
781                                 else if( strstr(val, "NONE") )
782                                         c->group_ciphers |= IWINFO_CIPHER_NONE;
783
784                                 else if( strstr(val, "WEP-40") )
785                                         c->group_ciphers |= IWINFO_CIPHER_WEP40;
786
787                                 else if( strstr(val, "WEP-104") )
788                                         c->group_ciphers |= IWINFO_CIPHER_WEP104;
789                         }
790
791
792                         if( (val = nl80211_getval(res, "key_mgmt")) )
793                         {
794                                 if( strstr(val, "WPA2") )
795                                         c->wpa_version = 2;
796
797                                 else if( strstr(val, "WPA") )
798                                         c->wpa_version = 1;
799
800
801                                 if( strstr(val, "PSK") )
802                                         c->auth_suites |= IWINFO_KMGMT_PSK;
803
804                                 else if( strstr(val, "EAP") || strstr(val, "802.1X") )
805                                         c->auth_suites |= IWINFO_KMGMT_8021x;
806
807                                 else if( strstr(val, "NONE") )
808                                         c->auth_suites |= IWINFO_KMGMT_NONE;
809                         }
810
811                         c->enabled = (c->wpa_version && c->auth_suites) ? 1 : 0;
812                 }
813
814                 return 0;
815         }
816
817         return -1;
818 }
819
820
821 static int nl80211_get_assoclist_cb(struct nl_msg *msg, void *arg)
822 {
823         struct nl80211_assoc_count *ac = arg;
824         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
825         struct nlattr *attr[NL80211_ATTR_MAX + 1];
826         struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
827
828         static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
829                 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32    },
830                 [NL80211_STA_INFO_RX_BYTES]      = { .type = NLA_U32    },
831                 [NL80211_STA_INFO_TX_BYTES]      = { .type = NLA_U32    },
832                 [NL80211_STA_INFO_RX_PACKETS]    = { .type = NLA_U32    },
833                 [NL80211_STA_INFO_TX_PACKETS]    = { .type = NLA_U32    },
834                 [NL80211_STA_INFO_SIGNAL]        = { .type = NLA_U8     },
835                 [NL80211_STA_INFO_TX_BITRATE]    = { .type = NLA_NESTED },
836                 [NL80211_STA_INFO_LLID]          = { .type = NLA_U16    },
837                 [NL80211_STA_INFO_PLID]          = { .type = NLA_U16    },
838                 [NL80211_STA_INFO_PLINK_STATE]   = { .type = NLA_U8     },
839         };
840
841         nla_parse(attr, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
842                 genlmsg_attrlen(gnlh, 0), NULL);
843
844         if( attr[NL80211_ATTR_MAC] )
845                 memcpy(ac->entry->mac, nla_data(attr[NL80211_ATTR_MAC]), 6);
846
847         if( attr[NL80211_ATTR_STA_INFO] )
848         {
849                 if( !nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
850                                 attr[NL80211_ATTR_STA_INFO], stats_policy) )
851                 {
852                         if( sinfo[NL80211_STA_INFO_SIGNAL] )
853                                 ac->entry->signal = nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
854                 }
855         }
856
857         ac->entry->noise = ac->noise;
858         ac->entry++;
859         ac->count++;
860
861         return NL_SKIP;
862 }
863
864 int nl80211_get_assoclist(const char *ifname, char *buf, int *len)
865 {
866         struct nl80211_assoc_count ac;
867         struct nl80211_msg_conveyor *req;
868
869         nl80211_get_noise(ifname, &ac.noise);
870
871         req = nl80211_msg(ifname, NL80211_CMD_GET_STATION, NLM_F_DUMP);
872         if( req )
873         {
874                 ac.count = 0;
875                 ac.entry = (struct iwinfo_assoclist_entry *)buf;
876
877                 nl80211_cb(req, nl80211_get_assoclist_cb, &ac);
878                 nl80211_send(req);
879                 nl80211_free(req);
880
881                 *len = (ac.count * sizeof(struct iwinfo_assoclist_entry));
882                 return 0;
883         }
884
885         return -1;
886 }
887
888 int nl80211_get_txpwrlist(const char *ifname, char *buf, int *len)
889 {
890         int ch_cur, ch_cmp, bands_remain, freqs_remain;
891         int dbm_max = -1, dbm_cur, dbm_cnt;
892         struct nl80211_msg_conveyor *req, *res;
893         struct nlattr *bands[NL80211_BAND_ATTR_MAX + 1];
894         struct nlattr *freqs[NL80211_FREQUENCY_ATTR_MAX + 1];
895         struct nlattr *band, *freq;
896         struct iwinfo_txpwrlist_entry entry;
897
898         static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
899                 [NL80211_FREQUENCY_ATTR_FREQ]         = { .type = NLA_U32  },
900                 [NL80211_FREQUENCY_ATTR_DISABLED]     = { .type = NLA_FLAG },
901                 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
902                 [NL80211_FREQUENCY_ATTR_NO_IBSS]      = { .type = NLA_FLAG },
903                 [NL80211_FREQUENCY_ATTR_RADAR]        = { .type = NLA_FLAG },
904                 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32  },
905         };
906
907         if( nl80211_get_channel(ifname, &ch_cur) )
908                 return -1;
909
910         req = nl80211_msg(ifname, NL80211_CMD_GET_WIPHY, 0);
911         if( req )
912         {
913                 res = nl80211_send(req);
914                 if( res )
915                 {
916                         nla_for_each_nested(band,
917                                 res->attr[NL80211_ATTR_WIPHY_BANDS], bands_remain)
918                         {
919                                 nla_parse(bands, NL80211_BAND_ATTR_MAX, nla_data(band),
920                                           nla_len(band), NULL);
921
922                                 nla_for_each_nested(freq,
923                                         bands[NL80211_BAND_ATTR_FREQS], freqs_remain)
924                                 {
925                                         nla_parse(freqs, NL80211_FREQUENCY_ATTR_MAX,
926                                                 nla_data(freq), nla_len(freq), freq_policy);
927
928                                         ch_cmp = nl80211_freq2channel(
929                                                 nla_get_u32(freqs[NL80211_FREQUENCY_ATTR_FREQ]));
930
931                                         if( (ch_cmp == ch_cur) &&
932                                             freqs[NL80211_FREQUENCY_ATTR_MAX_TX_POWER] )
933                                         {
934                                                 dbm_max = (int)(0.01 * nla_get_u32(
935                                                         freqs[NL80211_FREQUENCY_ATTR_MAX_TX_POWER]));
936
937                                                 break;
938                                         }
939                                 }
940                         }                       
941
942                         nl80211_free(res);
943                 }
944                 nl80211_free(req);
945         }
946
947         if( dbm_max > -1 )
948         {
949                 for( dbm_cur = 0, dbm_cnt = 0;
950                      dbm_cur < dbm_max;
951                      dbm_cur += 2, dbm_cnt++ )
952                 {
953                         entry.dbm = dbm_cur;
954                         entry.mw  = wext_dbm2mw(dbm_cur);
955
956                         memcpy(&buf[dbm_cnt * sizeof(entry)], &entry, sizeof(entry));
957                 }
958
959                 entry.dbm = dbm_max;
960                 entry.mw  = wext_dbm2mw(dbm_max);
961
962                 memcpy(&buf[dbm_cnt * sizeof(entry)], &entry, sizeof(entry));
963                 dbm_cnt++;
964
965                 *len = dbm_cnt * sizeof(entry);
966                 return 0;
967         }
968
969         return -1;
970 }
971
972 static void nl80211_get_scancrypto(const char *spec,
973         struct iwinfo_crypto_entry *c)
974 {
975         if( strstr(spec, "OPEN") )
976         {
977                 c->enabled = 0;
978         }
979         else
980         {
981                 c->enabled = 1;
982
983                 if( strstr(spec, "WPA2-") && strstr(spec, "WPA-") )
984                         c->wpa_version = 3;
985
986                 else if( strstr(spec, "WPA2") )
987                         c->wpa_version = 2;
988
989                 else if( strstr(spec, "WPA") )
990                         c->wpa_version = 1;
991
992                 else if( strstr(spec, "WEP") )
993                         c->auth_algs = IWINFO_AUTH_OPEN | IWINFO_AUTH_SHARED;
994
995
996                 if( strstr(spec, "PSK") )
997                         c->auth_suites |= IWINFO_KMGMT_PSK;
998
999                 if( strstr(spec, "802.1X") || strstr(spec, "EAP") )
1000                         c->auth_suites |= IWINFO_KMGMT_8021x;
1001
1002                 if( strstr(spec, "WPA-NONE") )
1003                         c->auth_suites |= IWINFO_KMGMT_NONE;
1004
1005
1006                 if( strstr(spec, "TKIP") )
1007                         c->pair_ciphers |= IWINFO_CIPHER_TKIP;
1008
1009                 if( strstr(spec, "CCMP") )
1010                         c->pair_ciphers |= IWINFO_CIPHER_CCMP;
1011
1012                 if( strstr(spec, "WEP-40") )
1013                         c->pair_ciphers |= IWINFO_CIPHER_WEP40;
1014
1015                 if( strstr(spec, "WEP-104") )
1016                         c->pair_ciphers |= IWINFO_CIPHER_WEP104;
1017
1018                 c->group_ciphers = c->pair_ciphers;
1019         }
1020 }
1021
1022 int nl80211_get_scanlist(const char *ifname, char *buf, int *len)
1023 {
1024         int freq, rssi, qmax, count;
1025         char *res;
1026         char cmd[256];
1027         char ssid[128] = { 0 };
1028         char bssid[18] = { 0 };
1029         char cipher[256] = { 0 };
1030
1031         struct iwinfo_scanlist_entry *e = (struct iwinfo_scanlist_entry *)buf;
1032
1033         /* WPA supplicant */
1034         if( (res = nl80211_wpasupp_info(ifname, "SCAN")) &&
1035             !strcmp(res, "OK\n") )
1036         {
1037                 sleep(2);
1038
1039                 if( (res = nl80211_wpasupp_info(ifname, "SCAN_RESULTS")) )
1040                 {
1041                         nl80211_get_quality_max(ifname, &qmax);
1042
1043                         /* skip header line */
1044                         while( *res++ != '\n' );
1045
1046                         count = 0;
1047
1048                         while( sscanf(res, "%17s %d %d %255s %127[^\n]\n",
1049                                       bssid, &freq, &rssi, cipher, ssid) > 0 )
1050                         {
1051                                 /* BSSID */
1052                                 e->mac[0] = strtol(&bssid[0],  NULL, 16);
1053                                 e->mac[1] = strtol(&bssid[3],  NULL, 16);
1054                                 e->mac[2] = strtol(&bssid[6],  NULL, 16);
1055                                 e->mac[3] = strtol(&bssid[9],  NULL, 16);
1056                                 e->mac[4] = strtol(&bssid[12], NULL, 16);
1057                                 e->mac[5] = strtol(&bssid[15], NULL, 16);
1058
1059                                 /* SSID */
1060                                 memcpy(e->ssid, ssid,
1061                                         min(strlen(ssid), sizeof(e->ssid) - 1));
1062
1063                                 /* Mode (assume master) */
1064                                 sprintf((char *)e->mode, "Master");
1065
1066                                 /* Channel */
1067                                 e->channel = nl80211_freq2channel(freq);
1068
1069                                 /* Signal */
1070                                 e->signal = rssi;
1071
1072                                 /* Quality */
1073                                 if( rssi < 0 )
1074                                 {
1075                                         /* The cfg80211 wext compat layer assumes a signal range
1076                                          * of -110 dBm to -40 dBm, the quality value is derived
1077                                          * by adding 110 to the signal level */
1078                                         if( rssi < -110 )
1079                                                 rssi = -110;
1080                                         else if( rssi > -40 )
1081                                                 rssi = -40;
1082
1083                                         e->quality = (rssi + 110);
1084                                 }
1085                                 else
1086                                 {
1087                                         e->quality = rssi;
1088                                 }
1089
1090                                 /* Max. Quality */
1091                                 e->quality_max = qmax;
1092
1093                                 /* Crypto */
1094                                 nl80211_get_scancrypto(cipher, &e->crypto);
1095
1096                                 /* advance to next line */
1097                                 while( *res && *res++ != '\n' );
1098
1099                                 count++;
1100                                 e++;
1101                         }
1102
1103                         *len = count * sizeof(struct iwinfo_scanlist_entry);
1104                         return 0;
1105                 }
1106         }
1107
1108         /* AP scan */
1109         else
1110         {
1111                 if( (res = nl80211_ifname2phy(ifname)) != NULL )
1112                 {
1113                         /*
1114                          * This is a big ugly hack, just look away.
1115                          */
1116
1117                         sprintf(cmd, "ifconfig %s down 2>/dev/null", ifname);
1118                         if( WEXITSTATUS(system(cmd)) )
1119                                 goto out;
1120
1121                         sprintf(cmd, "iw phy %s interface add scan.%s "
1122                                 "type station 2>/dev/null", res, ifname);
1123                         if( WEXITSTATUS(system(cmd)) )
1124                                 goto out;
1125
1126                         sprintf(cmd, "ifconfig scan.%s up 2>/dev/null", ifname);
1127                         if( WEXITSTATUS(system(cmd)) )
1128                                 goto out;
1129
1130                         sprintf(cmd, "scan.%s", ifname);
1131                         wext_get_scanlist(cmd, buf, len);
1132
1133         out:
1134                         sprintf(cmd, "ifconfig scan.%s down 2>/dev/null", ifname);
1135                         (void) WEXITSTATUS(system(cmd));
1136
1137                         sprintf(cmd, "iw dev scan.%s del 2>/dev/null", ifname);
1138                         (void) WEXITSTATUS(system(cmd));
1139
1140                         sprintf(cmd, "ifconfig %s up 2>/dev/null", ifname);
1141                         (void) WEXITSTATUS(system(cmd));
1142
1143                         sprintf(cmd, "killall -HUP hostapd 2>/dev/null");
1144                         (void) WEXITSTATUS(system(cmd));
1145
1146                         return 0;
1147                 }
1148         }
1149
1150         return -1;
1151 }
1152
1153 int nl80211_get_freqlist(const char *ifname, char *buf, int *len)
1154 {
1155         return wext_get_freqlist(ifname, buf, len);
1156 }
1157
1158 int nl80211_get_mbssid_support(const char *ifname, int *buf)
1159 {
1160         /* We assume that multi bssid is always possible */
1161         *buf = 1;
1162         return 0;
1163 }
1164