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