libiwinfo: do survey without bringing down the ap if possible
[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 static int nl80211_ioctlsock = -1;
33
34 static int nl80211_init(void)
35 {
36         int err, fd;
37
38         if( !nls )
39         {
40                 nl80211_ioctlsock = socket(AF_INET, SOCK_DGRAM, 0);
41                 if( nl80211_ioctlsock < 0 )
42                 {
43                         err = -ENOLINK;
44                         goto err;
45                 }
46                 else if( fcntl(nl80211_ioctlsock, F_SETFD,
47                                            fcntl(nl80211_ioctlsock, F_GETFD) | FD_CLOEXEC) < 0 )
48                 {
49                         err = -EINVAL;
50                         goto err;
51                 }
52
53                 nls = malloc(sizeof(struct nl80211_state));
54                 if( !nls ) {
55                         err = -ENOMEM;
56                         goto err;
57                 }
58
59                 nls->nl_sock = nl_socket_alloc();
60                 if( !nls->nl_sock ) {
61                         err = -ENOMEM;
62                         goto err;
63                 }
64
65                 if( genl_connect(nls->nl_sock)) {
66                         err = -ENOLINK;
67                         goto err;
68                 }
69
70                 fd = nl_socket_get_fd(nls->nl_sock);
71                 if( fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC) < 0 )
72                 {
73                         err = -EINVAL;
74                         goto err;
75                 }
76
77                 if( genl_ctrl_alloc_cache(nls->nl_sock, &nls->nl_cache)) {
78                         err = -ENOMEM;
79                         goto err;
80                 }
81
82                 nls->nl80211 = genl_ctrl_search_by_name(nls->nl_cache, "nl80211");
83                 if( !nls->nl80211 )
84                 {
85                         err = -ENOENT;
86                         goto err;
87                 }
88         }
89
90         return 0;
91
92
93 err:
94         nl80211_close();
95         return err;
96 }
97
98 static int nl80211_msg_error(struct sockaddr_nl *nla,
99         struct nlmsgerr *err, void *arg)
100 {
101         int *ret = arg;
102         *ret = err->error;
103         return NL_STOP;
104 }
105
106 static int nl80211_msg_finish(struct nl_msg *msg, void *arg)
107 {
108         int *ret = arg;
109         *ret = 0;
110         return NL_SKIP;
111 }
112
113 static int nl80211_msg_ack(struct nl_msg *msg, void *arg)
114 {
115         int *ret = arg;
116         *ret = 0;
117         return NL_STOP;
118 }
119
120 static int nl80211_msg_response(struct nl_msg *msg, void *arg)
121 {
122         struct nl80211_msg_conveyor *cv = arg;
123
124         nlmsg_get(msg);
125
126         cv->msg = msg;
127         cv->hdr = nlmsg_data(nlmsg_hdr(cv->msg));
128
129         nla_parse(cv->attr, NL80211_ATTR_MAX,
130                 genlmsg_attrdata(cv->hdr, 0),
131                 genlmsg_attrlen(cv->hdr, 0), NULL);
132
133         return NL_SKIP;
134 }
135
136 static void nl80211_free(struct nl80211_msg_conveyor *cv)
137 {
138         if( cv )
139         {
140                 if( cv->cb )
141                         nl_cb_put(cv->cb);
142
143                 if( cv->msg )
144                         nlmsg_free(cv->msg);
145
146                 cv->cb  = NULL;
147                 cv->msg = NULL;
148         }
149 }
150
151 static struct nl80211_msg_conveyor * nl80211_msg(const char *ifname, int cmd, int flags)
152 {
153         static struct nl80211_msg_conveyor cv;
154
155         int ifidx = -1, phyidx = -1;
156         struct nl_msg *req = NULL;
157         struct nl_cb *cb = NULL;
158
159         if( nl80211_init() < 0 )
160                 goto err;
161
162         if( !strncmp(ifname, "radio", 5) )
163                 phyidx = atoi(&ifname[5]);
164         else if( !strncmp(ifname, "mon.", 4) )
165                 ifidx = if_nametoindex(&ifname[4]);
166         else
167                 ifidx = if_nametoindex(ifname);
168
169         if( (ifidx < 0) && (phyidx < 0) )
170                 return NULL;
171
172         req = nlmsg_alloc();
173         if( !req )
174                 goto err;
175
176         cb = nl_cb_alloc(NL_CB_DEFAULT);
177         if( !cb )
178                 goto err;
179
180         genlmsg_put(req, 0, 0, genl_family_get_id(nls->nl80211), 0,
181                 flags, cmd, 0);
182
183         if( ifidx > -1 )
184                 NLA_PUT_U32(req, NL80211_ATTR_IFINDEX, ifidx);
185
186         if( phyidx > -1 )
187                 NLA_PUT_U32(req, NL80211_ATTR_WIPHY, phyidx);
188
189         nlmsg_get(req);
190
191         cv.msg       = req;
192         cv.cb        = cb;
193         cv.custom_cb = 0;
194
195         return &cv;
196
197 err:
198 nla_put_failure:
199         if( cb )
200                 nl_cb_put(cb);
201
202         if( req )
203                 nlmsg_free(req);
204
205         return NULL;
206 }
207
208 static void nl80211_cb(struct nl80211_msg_conveyor *cv,
209         int (*cb)(struct nl_msg *, void *), void *arg)
210 {
211         cv->custom_cb = 1;
212         nl_cb_set(cv->cb, NL_CB_VALID, NL_CB_CUSTOM, cb, arg);
213 }
214
215 static struct nl80211_msg_conveyor * nl80211_send(struct nl80211_msg_conveyor *cv)
216 {
217         static struct nl80211_msg_conveyor rcv;
218         int err = 1;
219
220         if( !cv->custom_cb )
221                 nl_cb_set(cv->cb, NL_CB_VALID, NL_CB_CUSTOM, nl80211_msg_response, &rcv);
222
223         if( nl_send_auto_complete(nls->nl_sock, cv->msg) < 0 )
224                 goto err;
225
226         nl_cb_err(cv->cb,               NL_CB_CUSTOM, nl80211_msg_error,  &err);
227         nl_cb_set(cv->cb, NL_CB_FINISH, NL_CB_CUSTOM, nl80211_msg_finish, &err);
228         nl_cb_set(cv->cb, NL_CB_ACK,    NL_CB_CUSTOM, nl80211_msg_ack,    &err);
229
230         while (err > 0)
231                 nl_recvmsgs(nls->nl_sock, cv->cb);
232
233         return &rcv;
234
235 err:
236         nl_cb_put(cv->cb);
237         nlmsg_free(cv->msg);
238
239         return NULL;
240 }
241
242 static int nl80211_freq2channel(int freq)
243 {
244     if (freq == 2484)
245         return 14;
246
247     if (freq < 2484)
248         return (freq - 2407) / 5;
249
250     return (freq / 5) - 1000;
251 }
252
253 static char * nl80211_getval(const char *ifname, const char *buf, const char *key)
254 {
255         int i, len;
256         char lkey[64] = { 0 };
257         const char *ln = buf;
258         static char lval[256] = { 0 };
259
260         int matched_if = ifname ? 0 : 1;
261
262
263         for( i = 0, len = strlen(buf); i < len; i++ )
264         {
265                 if( !lkey[0] && (buf[i] == ' ' || buf[i] == '\t') )
266                 {
267                         ln++;
268                 }
269                 else if( !lkey[0] && (buf[i] == '=') )
270                 {
271                         if( (&buf[i] - ln) > 0 )
272                                 memcpy(lkey, ln, min(sizeof(lkey) - 1, &buf[i] - ln));
273                 }
274                 else if( buf[i] == '\n' )
275                 {
276                         if( lkey[0] )
277                         {
278                                 memcpy(lval, ln + strlen(lkey) + 1,
279                                         min(sizeof(lval) - 1, &buf[i] - ln - strlen(lkey) - 1));
280
281                                 if( (ifname != NULL ) &&
282                                     (!strcmp(lkey, "interface") || !strcmp(lkey, "bss")) )
283                                 {
284                                         matched_if = !strcmp(lval, ifname);
285                                 }
286                                 else if( matched_if && !strcmp(lkey, key) )
287                                 {
288                                         return lval;
289                                 }
290                         }
291
292                         ln = &buf[i+1];
293                         memset(lkey, 0, sizeof(lkey));
294                         memset(lval, 0, sizeof(lval));
295                 }
296         }
297
298         return NULL;
299 }
300
301 static char * nl80211_ifname2phy(const char *ifname)
302 {
303         static char phy[32] = { 0 };
304         struct nl80211_msg_conveyor *req, *res;
305
306         req = nl80211_msg(ifname, NL80211_CMD_GET_WIPHY, 0);
307         if( req )
308         {
309                 res = nl80211_send(req);
310                 if( res )
311                 {
312                         if( res->attr[NL80211_ATTR_WIPHY_NAME] )
313                         {
314                                 snprintf(phy, sizeof(phy), "%s",
315                                          nla_get_string(res->attr[NL80211_ATTR_WIPHY_NAME]));
316                         }
317                         nl80211_free(res);
318                 }
319                 nl80211_free(req);
320         }
321
322         return phy[0] ? phy : NULL;
323 }
324
325 static char * nl80211_hostapd_info(const char *ifname)
326 {
327         char *phy;
328         char path[32] = { 0 };
329         static char buf[4096] = { 0 };
330         FILE *conf;
331
332         if( (phy = nl80211_ifname2phy(ifname)) != NULL )
333         {
334                 snprintf(path, sizeof(path), "/var/run/hostapd-%s.conf", phy);
335
336                 if( (conf = fopen(path, "r")) != NULL )
337                 {
338                         fread(buf, sizeof(buf) - 1, 1, conf);
339                         fclose(conf);
340
341                         return buf;
342                 }
343         }
344
345         return NULL;
346 }
347
348 static char * nl80211_wpasupp_info(const char *ifname, const char *cmd)
349 {
350         int sock = -1, len;
351         char *rv = NULL;
352         size_t remote_length, local_length;
353         static char buffer[1024] = { 0 };
354
355         struct timeval tv = { 2, 0 };
356         struct sockaddr_un local = { 0 };
357         struct sockaddr_un remote = { 0 };
358
359         fd_set rfds;
360
361         sock = socket(PF_UNIX, SOCK_DGRAM, 0);
362         if( sock < 0 )
363                 return NULL;
364
365         remote.sun_family = AF_UNIX;
366         remote_length = sizeof(remote.sun_family) + sprintf(remote.sun_path,
367                 "/var/run/wpa_supplicant-%s/%s", ifname, ifname);
368
369         if( fcntl(sock, F_SETFD, fcntl(sock, F_GETFD) | FD_CLOEXEC) < 0 )
370                 goto out;
371
372         if( connect(sock, (struct sockaddr *) &remote, remote_length) )
373                 goto out;
374
375         local.sun_family = AF_UNIX;
376         local_length = sizeof(local.sun_family) + sprintf(local.sun_path,
377                 "/var/run/iwinfo-%s-%d", ifname, getpid());
378
379         if( bind(sock, (struct sockaddr *) &local, local_length) )
380                 goto out;
381
382         send(sock, cmd, strlen(cmd), 0);
383
384         while( 1 )
385         {
386                 FD_ZERO(&rfds);
387                 FD_SET(sock, &rfds);
388
389                 if( select(sock + 1, &rfds, NULL, NULL, &tv) < 0 )
390                         goto out;
391
392                 if( !FD_ISSET(sock, &rfds) )
393                         break;
394
395                 if( (len = recv(sock, buffer, sizeof(buffer), 0)) <= 0 )
396                         goto out;
397
398                 buffer[len] = 0;
399
400                 if( buffer[0] != '<' )
401                         break;
402         }
403
404         rv = buffer;
405
406 out:
407         close(sock);
408
409         if( local.sun_family )
410                 unlink(local.sun_path);
411
412         return rv;
413 }
414
415 static char * nl80211_phy2ifname(const char *ifname)
416 {
417         int fd, phyidx = 0;
418         char buffer[64];
419         static char nif[IFNAMSIZ] = { 0 };
420
421         DIR *d;
422         struct dirent *e;
423
424         if( !strncmp(ifname, "radio", 5) )
425         {
426                 phyidx = atoi(&ifname[5]);
427
428                 if( (d = opendir("/sys/class/net")) != NULL )
429                 {
430                         while( (e = readdir(d)) != NULL )
431                         {
432                                 snprintf(buffer, sizeof(buffer),
433                                         "/sys/class/net/%s/phy80211/index", e->d_name);
434
435                                 if( (fd = open(buffer, O_RDONLY)) > 0 )
436                                 {
437                                         if( (read(fd, buffer, sizeof(buffer)) > 0) &&
438                                             (atoi(buffer) == phyidx) )
439                                         {
440                                                 strncpy(nif, e->d_name, sizeof(nif));
441                                         }
442
443                                         close(fd);
444                                 }
445
446                                 if( nif[0] )
447                                         break;
448                         }
449
450                         closedir(d);
451                 }
452         }
453
454         return nif[0] ? nif : NULL;
455 }
456
457 static char * nl80211_ifadd(const char *ifname)
458 {
459         int phyidx;
460         char *rv = NULL;
461         static char nif[IFNAMSIZ] = { 0 };
462         struct nl80211_msg_conveyor *req, *res;
463
464         req = nl80211_msg(ifname, NL80211_CMD_NEW_INTERFACE, 0);
465         if( req )
466         {
467                 snprintf(nif, sizeof(nif), "tmp.%s", ifname);
468
469                 NLA_PUT_STRING(req->msg, NL80211_ATTR_IFNAME, nif);
470                 NLA_PUT_U32(req->msg, NL80211_ATTR_IFTYPE, NL80211_IFTYPE_STATION);
471
472                 res = nl80211_send(req);
473                 if( res )
474                 {
475                         rv = nif;
476                         nl80211_free(res);
477                 }
478
479         nla_put_failure:
480                 nl80211_free(req);
481         }
482
483         return rv;
484 }
485
486 static void nl80211_ifdel(const char *ifname)
487 {
488         struct nl80211_msg_conveyor *req, *res;
489
490         req = nl80211_msg(ifname, NL80211_CMD_DEL_INTERFACE, 0);
491         if( req )
492         {
493                 NLA_PUT_STRING(req->msg, NL80211_ATTR_IFNAME, ifname);
494
495                 nl80211_free(nl80211_send(req));
496
497         nla_put_failure:
498                 nl80211_free(req);
499         }
500 }
501
502 static int nl80211_ifup(const char *ifname)
503 {
504         struct ifreq ifr;
505
506         strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
507
508         if( ioctl(nl80211_ioctlsock, SIOCGIFFLAGS, &ifr) )
509                 return 0;
510
511         ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
512
513         return !ioctl(nl80211_ioctlsock, SIOCSIFFLAGS, &ifr);
514 }
515
516 static int nl80211_ifdown(const char *ifname)
517 {
518         struct ifreq ifr;
519
520         strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
521
522         if( ioctl(nl80211_ioctlsock, SIOCGIFFLAGS, &ifr) )
523                 return 0;
524
525         ifr.ifr_flags &= ~(IFF_UP | IFF_RUNNING);
526
527         return !ioctl(nl80211_ioctlsock, SIOCSIFFLAGS, &ifr);
528 }
529
530 static int nl80211_ifmac(const char *ifname)
531 {
532         struct ifreq ifr;
533
534         strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
535
536         if( ioctl(nl80211_ioctlsock, SIOCGIFHWADDR, &ifr) )
537                 return 0;
538
539         ifr.ifr_hwaddr.sa_data[1]++;
540         ifr.ifr_hwaddr.sa_data[2]++;
541
542         return !ioctl(nl80211_ioctlsock, SIOCSIFHWADDR, &ifr);
543 }
544
545 static void nl80211_hostapd_hup(const char *ifname)
546 {
547         int fd, pid = 0;
548         char buf[32];
549         char *phy = strncmp(ifname, "phy", 3) ? nl80211_ifname2phy(ifname) : ifname;
550
551         if( phy )
552         {
553                 snprintf(buf, sizeof(buf), "/var/run/wifi-%s.pid", phy);
554                 if( (fd = open(buf, O_RDONLY)) > 0 )
555                 {
556                         if( read(fd, buf, sizeof(buf)) > 0 )
557                                 pid = atoi(buf);
558
559                         close(fd);
560                 }
561
562                 if( pid > 0 )
563                         kill(pid, 1);
564         }
565 }
566
567
568 int nl80211_probe(const char *ifname)
569 {
570         return !!nl80211_ifname2phy(ifname);
571 }
572
573 void nl80211_close(void)
574 {
575         if( nl80211_ioctlsock > -1 )
576         {
577                 close(nl80211_ioctlsock);
578         }
579
580         if( nls )
581         {
582                 if( nls->nl_sock )
583                         nl_socket_free(nls->nl_sock);
584
585                 if( nls->nl_cache )
586                         nl_cache_free(nls->nl_cache);
587
588                 free(nls);
589                 nls = NULL;
590         }
591 }
592
593 int nl80211_get_mode(const char *ifname, char *buf)
594 {
595         return wext_get_mode(ifname, buf);
596 }
597
598 int nl80211_get_ssid(const char *ifname, char *buf)
599 {
600         char *ssid;
601
602         if( !wext_get_ssid(ifname, buf) )
603         {
604                 return 0;
605         }
606         else if( (ssid = nl80211_hostapd_info(ifname)) &&
607                  (ssid = nl80211_getval(ifname, ssid, "ssid")) )
608         {
609                 memcpy(buf, ssid, strlen(ssid));
610                 return 0;
611         }
612
613         return -1;
614 }
615
616 int nl80211_get_bssid(const char *ifname, char *buf)
617 {
618         char *bssid;
619         unsigned char mac[6];
620
621         if( !wext_get_bssid(ifname, buf) )
622         {
623                 return 0;
624         }
625         else if( (bssid = nl80211_hostapd_info(ifname)) &&
626                  (bssid = nl80211_getval(ifname, bssid, "bssid")) )
627         {
628                 mac[0] = strtol(&bssid[0],  NULL, 16);
629                 mac[1] = strtol(&bssid[3],  NULL, 16);
630                 mac[2] = strtol(&bssid[6],  NULL, 16);
631                 mac[3] = strtol(&bssid[9],  NULL, 16);
632                 mac[4] = strtol(&bssid[12], NULL, 16);
633                 mac[5] = strtol(&bssid[15], NULL, 16);
634
635                 sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
636                         mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
637
638                 return 0;
639         }
640
641         return -1;
642 }
643
644 int nl80211_get_channel(const char *ifname, int *buf)
645 {
646         return wext_get_channel(ifname, buf);
647 }
648
649 int nl80211_get_frequency(const char *ifname, int *buf)
650 {
651         return wext_get_frequency(ifname, buf);
652 }
653
654 int nl80211_get_txpower(const char *ifname, int *buf)
655 {
656         return wext_get_txpower(ifname, buf);
657 }
658
659
660 static int nl80211_get_signal_cb(struct nl_msg *msg, void *arg)
661 {
662         int8_t dbm;
663         int16_t mbit;
664         struct nl80211_rssi_rate *rr = arg;
665
666         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
667         struct nlattr *attr[NL80211_ATTR_MAX + 1];
668         struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
669         struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
670
671         static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
672                 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32    },
673                 [NL80211_STA_INFO_RX_BYTES]      = { .type = NLA_U32    },
674                 [NL80211_STA_INFO_TX_BYTES]      = { .type = NLA_U32    },
675                 [NL80211_STA_INFO_RX_PACKETS]    = { .type = NLA_U32    },
676                 [NL80211_STA_INFO_TX_PACKETS]    = { .type = NLA_U32    },
677                 [NL80211_STA_INFO_SIGNAL]        = { .type = NLA_U8     },
678                 [NL80211_STA_INFO_TX_BITRATE]    = { .type = NLA_NESTED },
679                 [NL80211_STA_INFO_LLID]          = { .type = NLA_U16    },
680                 [NL80211_STA_INFO_PLID]          = { .type = NLA_U16    },
681                 [NL80211_STA_INFO_PLINK_STATE]   = { .type = NLA_U8     },
682         };
683
684         static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
685                 [NL80211_RATE_INFO_BITRATE]      = { .type = NLA_U16  },
686                 [NL80211_RATE_INFO_MCS]          = { .type = NLA_U8   },
687                 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
688                 [NL80211_RATE_INFO_SHORT_GI]     = { .type = NLA_FLAG },
689         };
690
691         nla_parse(attr, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
692                   genlmsg_attrlen(gnlh, 0), NULL);
693
694         if( attr[NL80211_ATTR_STA_INFO] )
695         {
696                 if( !nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
697                                 attr[NL80211_ATTR_STA_INFO], stats_policy) )
698                 {
699                         if( sinfo[NL80211_STA_INFO_SIGNAL] )
700                         {
701                                 dbm = nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
702                                 rr->rssi = rr->rssi ? (int8_t)((rr->rssi + dbm) / 2) : dbm;
703                         }
704
705                         if( sinfo[NL80211_STA_INFO_TX_BITRATE] )
706                         {
707                                 if( !nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
708                                                 sinfo[NL80211_STA_INFO_TX_BITRATE], rate_policy) )
709                                 {
710                                         if( rinfo[NL80211_RATE_INFO_BITRATE] )
711                                         {
712                                                 mbit = nla_get_u16(rinfo[NL80211_RATE_INFO_BITRATE]);
713                                                 rr->rate = rr->rate
714                                                         ? (int16_t)((rr->rate + mbit) / 2) : mbit;
715                                         }
716                                 }
717                         }
718                 }
719         }
720
721         return NL_SKIP;
722 }
723
724 int nl80211_get_bitrate(const char *ifname, int *buf)
725 {
726         struct nl80211_rssi_rate rr;
727         struct nl80211_msg_conveyor *req;
728
729         if( !wext_get_bitrate(ifname, buf) )
730                 return 0;
731
732         req = nl80211_msg(ifname, NL80211_CMD_GET_STATION, NLM_F_DUMP);
733         if( req )
734         {
735                 rr.rssi = 0;
736                 rr.rate = 0;
737
738                 nl80211_cb(req, nl80211_get_signal_cb, &rr);
739                 nl80211_send(req);
740                 nl80211_free(req);
741
742                 if( rr.rate )
743                 {
744                         *buf = (rr.rate * 100);
745                         return 0;
746                 }
747         }
748
749         return -1;
750 }
751
752 int nl80211_get_signal(const char *ifname, int *buf)
753 {
754         struct nl80211_rssi_rate rr;
755         struct nl80211_msg_conveyor *req;
756
757         if( !wext_get_signal(ifname, buf) )
758                 return 0;
759
760         req = nl80211_msg(ifname, NL80211_CMD_GET_STATION, NLM_F_DUMP);
761         if( req )
762         {
763                 rr.rssi = 0;
764                 rr.rate = 0;
765
766                 nl80211_cb(req, nl80211_get_signal_cb, &rr);
767                 nl80211_send(req);
768                 nl80211_free(req);
769
770                 if( rr.rssi )
771                 {
772                         *buf = rr.rssi;
773                         return 0;
774                 }
775         }
776
777         return -1;
778 }
779
780 int nl80211_get_noise(const char *ifname, int *buf)
781 {
782         int rv = -1;
783         struct nl80211_msg_conveyor *req, *res;
784         struct nlattr *si[NL80211_SURVEY_INFO_MAX + 1];
785
786         static struct nla_policy sp[NL80211_SURVEY_INFO_MAX + 1] = {
787                 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
788                 [NL80211_SURVEY_INFO_NOISE]     = { .type = NLA_U8  },
789         };
790
791         req = nl80211_msg(ifname, NL80211_CMD_GET_SURVEY, NLM_F_DUMP);
792         if( req )
793         {
794                 res = nl80211_send(req);
795                 if( res )
796                 {
797                         if( res->attr[NL80211_ATTR_SURVEY_INFO] )
798                         {
799                                 if( !nla_parse_nested(si, NL80211_SURVEY_INFO_MAX,
800                                                 res->attr[NL80211_ATTR_SURVEY_INFO], sp) &&
801                                         si[NL80211_SURVEY_INFO_NOISE] )
802                                 {
803                                         *buf = (int8_t)nla_get_u8(si[NL80211_SURVEY_INFO_NOISE]);
804                                         rv = 0;
805                                 }
806                         }
807                         nl80211_free(res);
808                 }
809                 nl80211_free(req);
810         }
811
812         return rv;
813 }
814
815 int nl80211_get_quality(const char *ifname, int *buf)
816 {
817         int signal;
818
819         if( wext_get_quality(ifname, buf) )
820         {
821                 *buf = 0;
822
823                 if( !nl80211_get_signal(ifname, &signal) )
824                 {
825                         /* A positive signal level is usually just a quality
826                          * value, pass through as-is */
827                         if( signal >= 0 )
828                         {
829                                 *buf = signal;
830                         }
831
832                         /* The cfg80211 wext compat layer assumes a signal range
833                          * of -110 dBm to -40 dBm, the quality value is derived
834                          * by adding 110 to the signal level */
835                         else
836                         {
837                                 if( signal < -110 )
838                                         signal = -110;
839                                 else if( signal > -40 )
840                                         signal = -40;
841
842                                 *buf = (signal + 110);
843                         }
844                 }
845         }
846
847         return 0;
848 }
849
850 int nl80211_get_quality_max(const char *ifname, int *buf)
851 {
852         if( wext_get_quality_max(ifname, buf) )
853                 /* The cfg80211 wext compat layer assumes a maximum
854                  * quality of 70 */
855                 *buf = 70;
856
857         return 0;
858 }
859
860 int nl80211_get_encryption(const char *ifname, char *buf)
861 {
862         int i;
863         char k[9];
864         char *val, *res;
865         struct iwinfo_crypto_entry *c = (struct iwinfo_crypto_entry *)buf;
866
867         /* Hostapd */
868         if( (res = nl80211_hostapd_info(ifname)) )
869         {
870                 if( (val = nl80211_getval(ifname, res, "auth_algs")) && (val > 0) )
871                 {
872                         c->auth_suites |= IWINFO_KMGMT_NONE;
873
874                         switch(atoi(val)) {
875                                 case 1:
876                                         c->auth_algs |= IWINFO_AUTH_OPEN;
877                                         break;
878
879                                 case 2:
880                                         c->auth_algs |= IWINFO_AUTH_SHARED;
881                                         break;
882
883                                 case 3:
884                                         c->auth_algs |= IWINFO_AUTH_OPEN;
885                                         c->auth_algs |= IWINFO_AUTH_SHARED;
886                                         break;
887
888                                 default:
889                                         break;
890                         }
891
892                         for( i = 0; i < 4; i++ )
893                         {
894                                 snprintf(k, sizeof(k), "wep_key%d", i);
895
896                                 if( (val = nl80211_getval(ifname, res, k)) )
897                                 {
898                                         if( (strlen(val) == 5) || (strlen(val) == 10) )
899                                                 c->pair_ciphers |= IWINFO_CIPHER_WEP40;
900
901                                         else if( (strlen(val) == 13) || (strlen(val) == 26) )
902                                                 c->pair_ciphers |= IWINFO_CIPHER_WEP104;
903                                 }
904                         }
905
906                         c->group_ciphers = c->pair_ciphers;
907
908                         return 0;
909                 }
910
911
912                 if( (val = nl80211_getval(ifname, res, "wpa")) != NULL )
913                         c->wpa_version = atoi(val);
914
915
916                 val = nl80211_getval(ifname, res, "wpa_key_mgmt");
917
918                 if( !val || strstr(val, "PSK") )
919                         c->auth_suites |= IWINFO_KMGMT_PSK;
920
921                 if( val && strstr(val, "EAP") )
922                         c->auth_suites |= IWINFO_KMGMT_8021x;
923
924                 if( val && strstr(val, "NONE") )
925                         c->auth_suites |= IWINFO_KMGMT_NONE;
926
927
928                 if( (val = nl80211_getval(ifname, res, "wpa_pairwise")) != NULL )
929                 {
930                         if( strstr(val, "TKIP") )
931                                 c->pair_ciphers |= IWINFO_CIPHER_TKIP;
932
933                         if( strstr(val, "CCMP") )
934                                 c->pair_ciphers |= IWINFO_CIPHER_CCMP;
935
936                         if( strstr(val, "NONE") )
937                                 c->pair_ciphers |= IWINFO_CIPHER_NONE;
938                 }
939
940
941                 c->group_ciphers = c->pair_ciphers;
942                 c->enabled = (c->auth_algs || c->auth_suites) ? 1 : 0;
943
944                 return 0;
945         }
946
947         /* WPA supplicant */
948         else if( (res = nl80211_wpasupp_info(ifname, "STATUS")) &&
949                  (val = nl80211_getval(NULL, res, "pairwise_cipher")) )
950         {
951                 /* WEP */
952                 if( strstr(val, "WEP") )
953                 {
954                         if( strstr(val, "WEP-40") )
955                                 c->pair_ciphers |= IWINFO_CIPHER_WEP40;
956
957                         else if( strstr(val, "WEP-104") )
958                                 c->pair_ciphers |= IWINFO_CIPHER_WEP104;
959
960                         c->enabled       = 1;
961                         c->group_ciphers = c->pair_ciphers;
962
963                         c->auth_suites |= IWINFO_KMGMT_NONE;
964                         c->auth_algs   |= IWINFO_AUTH_OPEN; /* XXX: assumption */
965                 }
966
967                 /* WPA */
968                 else
969                 {
970                         if( strstr(val, "TKIP") )
971                                 c->pair_ciphers |= IWINFO_CIPHER_TKIP;
972
973                         else if( strstr(val, "CCMP") )
974                                 c->pair_ciphers |= IWINFO_CIPHER_CCMP;
975
976                         else if( strstr(val, "NONE") )
977                                 c->pair_ciphers |= IWINFO_CIPHER_NONE;
978
979                         else if( strstr(val, "WEP-40") )
980                                 c->pair_ciphers |= IWINFO_CIPHER_WEP40;
981
982                         else if( strstr(val, "WEP-104") )
983                                 c->pair_ciphers |= IWINFO_CIPHER_WEP104;
984
985
986                         if( (val = nl80211_getval(NULL, res, "group_cipher")) )
987                         {
988                                 if( strstr(val, "TKIP") )
989                                         c->group_ciphers |= IWINFO_CIPHER_TKIP;
990
991                                 else if( strstr(val, "CCMP") )
992                                         c->group_ciphers |= IWINFO_CIPHER_CCMP;
993
994                                 else if( strstr(val, "NONE") )
995                                         c->group_ciphers |= IWINFO_CIPHER_NONE;
996
997                                 else if( strstr(val, "WEP-40") )
998                                         c->group_ciphers |= IWINFO_CIPHER_WEP40;
999
1000                                 else if( strstr(val, "WEP-104") )
1001                                         c->group_ciphers |= IWINFO_CIPHER_WEP104;
1002                         }
1003
1004
1005                         if( (val = nl80211_getval(NULL, res, "key_mgmt")) )
1006                         {
1007                                 if( strstr(val, "WPA2") )
1008                                         c->wpa_version = 2;
1009
1010                                 else if( strstr(val, "WPA") )
1011                                         c->wpa_version = 1;
1012
1013
1014                                 if( strstr(val, "PSK") )
1015                                         c->auth_suites |= IWINFO_KMGMT_PSK;
1016
1017                                 else if( strstr(val, "EAP") || strstr(val, "802.1X") )
1018                                         c->auth_suites |= IWINFO_KMGMT_8021x;
1019
1020                                 else if( strstr(val, "NONE") )
1021                                         c->auth_suites |= IWINFO_KMGMT_NONE;
1022                         }
1023
1024                         c->enabled = (c->wpa_version && c->auth_suites) ? 1 : 0;
1025                 }
1026
1027                 return 0;
1028         }
1029
1030         return -1;
1031 }
1032
1033
1034 static int nl80211_get_assoclist_cb(struct nl_msg *msg, void *arg)
1035 {
1036         struct nl80211_assoc_count *ac = arg;
1037         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1038         struct nlattr *attr[NL80211_ATTR_MAX + 1];
1039         struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
1040
1041         static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
1042                 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32    },
1043                 [NL80211_STA_INFO_RX_BYTES]      = { .type = NLA_U32    },
1044                 [NL80211_STA_INFO_TX_BYTES]      = { .type = NLA_U32    },
1045                 [NL80211_STA_INFO_RX_PACKETS]    = { .type = NLA_U32    },
1046                 [NL80211_STA_INFO_TX_PACKETS]    = { .type = NLA_U32    },
1047                 [NL80211_STA_INFO_SIGNAL]        = { .type = NLA_U8     },
1048                 [NL80211_STA_INFO_TX_BITRATE]    = { .type = NLA_NESTED },
1049                 [NL80211_STA_INFO_LLID]          = { .type = NLA_U16    },
1050                 [NL80211_STA_INFO_PLID]          = { .type = NLA_U16    },
1051                 [NL80211_STA_INFO_PLINK_STATE]   = { .type = NLA_U8     },
1052         };
1053
1054         nla_parse(attr, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1055                 genlmsg_attrlen(gnlh, 0), NULL);
1056
1057         if( attr[NL80211_ATTR_MAC] )
1058                 memcpy(ac->entry->mac, nla_data(attr[NL80211_ATTR_MAC]), 6);
1059
1060         if( attr[NL80211_ATTR_STA_INFO] )
1061         {
1062                 if( !nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
1063                                 attr[NL80211_ATTR_STA_INFO], stats_policy) )
1064                 {
1065                         if( sinfo[NL80211_STA_INFO_SIGNAL] )
1066                                 ac->entry->signal = nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
1067                 }
1068         }
1069
1070         ac->entry->noise = ac->noise;
1071         ac->entry++;
1072         ac->count++;
1073
1074         return NL_SKIP;
1075 }
1076
1077 int nl80211_get_assoclist(const char *ifname, char *buf, int *len)
1078 {
1079         struct nl80211_assoc_count ac;
1080         struct nl80211_msg_conveyor *req;
1081
1082         nl80211_get_noise(ifname, &ac.noise);
1083
1084         req = nl80211_msg(ifname, NL80211_CMD_GET_STATION, NLM_F_DUMP);
1085         if( req )
1086         {
1087                 ac.count = 0;
1088                 ac.entry = (struct iwinfo_assoclist_entry *)buf;
1089
1090                 nl80211_cb(req, nl80211_get_assoclist_cb, &ac);
1091                 nl80211_send(req);
1092                 nl80211_free(req);
1093
1094                 *len = (ac.count * sizeof(struct iwinfo_assoclist_entry));
1095                 return 0;
1096         }
1097
1098         return -1;
1099 }
1100
1101 int nl80211_get_txpwrlist(const char *ifname, char *buf, int *len)
1102 {
1103         int ch_cur, ch_cmp, bands_remain, freqs_remain;
1104         int dbm_max = -1, dbm_cur, dbm_cnt;
1105         struct nl80211_msg_conveyor *req, *res;
1106         struct nlattr *bands[NL80211_BAND_ATTR_MAX + 1];
1107         struct nlattr *freqs[NL80211_FREQUENCY_ATTR_MAX + 1];
1108         struct nlattr *band, *freq;
1109         struct iwinfo_txpwrlist_entry entry;
1110
1111         static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
1112                 [NL80211_FREQUENCY_ATTR_FREQ]         = { .type = NLA_U32  },
1113                 [NL80211_FREQUENCY_ATTR_DISABLED]     = { .type = NLA_FLAG },
1114                 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
1115                 [NL80211_FREQUENCY_ATTR_NO_IBSS]      = { .type = NLA_FLAG },
1116                 [NL80211_FREQUENCY_ATTR_RADAR]        = { .type = NLA_FLAG },
1117                 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32  },
1118         };
1119
1120         if( nl80211_get_channel(ifname, &ch_cur) )
1121                 ch_cur = 0;
1122
1123         req = nl80211_msg(ifname, NL80211_CMD_GET_WIPHY, 0);
1124         if( req )
1125         {
1126                 res = nl80211_send(req);
1127                 if( res )
1128                 {
1129                         nla_for_each_nested(band,
1130                                 res->attr[NL80211_ATTR_WIPHY_BANDS], bands_remain)
1131                         {
1132                                 nla_parse(bands, NL80211_BAND_ATTR_MAX, nla_data(band),
1133                                           nla_len(band), NULL);
1134
1135                                 nla_for_each_nested(freq,
1136                                         bands[NL80211_BAND_ATTR_FREQS], freqs_remain)
1137                                 {
1138                                         nla_parse(freqs, NL80211_FREQUENCY_ATTR_MAX,
1139                                                 nla_data(freq), nla_len(freq), freq_policy);
1140
1141                                         ch_cmp = nl80211_freq2channel(
1142                                                 nla_get_u32(freqs[NL80211_FREQUENCY_ATTR_FREQ]));
1143
1144                                         if( (!ch_cur || (ch_cmp == ch_cur)) &&
1145                                             freqs[NL80211_FREQUENCY_ATTR_MAX_TX_POWER] )
1146                                         {
1147                                                 dbm_max = (int)(0.01 * nla_get_u32(
1148                                                         freqs[NL80211_FREQUENCY_ATTR_MAX_TX_POWER]));
1149
1150                                                 break;
1151                                         }
1152                                 }
1153                         }
1154
1155                         nl80211_free(res);
1156                 }
1157                 nl80211_free(req);
1158         }
1159
1160         if( dbm_max > -1 )
1161         {
1162                 for( dbm_cur = 0, dbm_cnt = 0;
1163                      dbm_cur < dbm_max;
1164                      dbm_cur += 2, dbm_cnt++ )
1165                 {
1166                         entry.dbm = dbm_cur;
1167                         entry.mw  = wext_dbm2mw(dbm_cur);
1168
1169                         memcpy(&buf[dbm_cnt * sizeof(entry)], &entry, sizeof(entry));
1170                 }
1171
1172                 entry.dbm = dbm_max;
1173                 entry.mw  = wext_dbm2mw(dbm_max);
1174
1175                 memcpy(&buf[dbm_cnt * sizeof(entry)], &entry, sizeof(entry));
1176                 dbm_cnt++;
1177
1178                 *len = dbm_cnt * sizeof(entry);
1179                 return 0;
1180         }
1181
1182         return -1;
1183 }
1184
1185 static void nl80211_get_scancrypto(const char *spec,
1186         struct iwinfo_crypto_entry *c)
1187 {
1188         if( strstr(spec, "OPEN") )
1189         {
1190                 c->enabled = 0;
1191         }
1192         else
1193         {
1194                 c->enabled = 1;
1195
1196                 if( strstr(spec, "WPA2-") && strstr(spec, "WPA-") )
1197                         c->wpa_version = 3;
1198
1199                 else if( strstr(spec, "WPA2") )
1200                         c->wpa_version = 2;
1201
1202                 else if( strstr(spec, "WPA") )
1203                         c->wpa_version = 1;
1204
1205                 else if( strstr(spec, "WEP") )
1206                         c->auth_algs = IWINFO_AUTH_OPEN | IWINFO_AUTH_SHARED;
1207
1208
1209                 if( strstr(spec, "PSK") )
1210                         c->auth_suites |= IWINFO_KMGMT_PSK;
1211
1212                 if( strstr(spec, "802.1X") || strstr(spec, "EAP") )
1213                         c->auth_suites |= IWINFO_KMGMT_8021x;
1214
1215                 if( strstr(spec, "WPA-NONE") )
1216                         c->auth_suites |= IWINFO_KMGMT_NONE;
1217
1218
1219                 if( strstr(spec, "TKIP") )
1220                         c->pair_ciphers |= IWINFO_CIPHER_TKIP;
1221
1222                 if( strstr(spec, "CCMP") )
1223                         c->pair_ciphers |= IWINFO_CIPHER_CCMP;
1224
1225                 if( strstr(spec, "WEP-40") )
1226                         c->pair_ciphers |= IWINFO_CIPHER_WEP40;
1227
1228                 if( strstr(spec, "WEP-104") )
1229                         c->pair_ciphers |= IWINFO_CIPHER_WEP104;
1230
1231                 c->group_ciphers = c->pair_ciphers;
1232         }
1233 }
1234
1235 int nl80211_get_scanlist(const char *ifname, char *buf, int *len)
1236 {
1237         int freq, rssi, qmax, count;
1238         char *res;
1239         char ssid[128] = { 0 };
1240         char bssid[18] = { 0 };
1241         char cipher[256] = { 0 };
1242
1243         /* Got a radioX pseudo interface, find some interface on it or create one */
1244         if( !strncmp(ifname, "radio", 5) )
1245         {
1246                 /* Reuse existing interface */
1247                 if( (res = nl80211_phy2ifname(ifname)) != NULL )
1248                 {
1249                         return nl80211_get_scanlist(res, buf, len);
1250                 }
1251
1252                 /* Need to spawn a temporary iface for scanning */
1253                 else if( (res = nl80211_ifadd(ifname)) != NULL )
1254                 {
1255                         count = nl80211_get_scanlist(res, buf, len);
1256                         nl80211_ifdel(res);
1257                         return count;
1258                 }
1259         }
1260
1261         struct iwinfo_scanlist_entry *e = (struct iwinfo_scanlist_entry *)buf;
1262
1263         /* WPA supplicant */
1264         if( (res = nl80211_wpasupp_info(ifname, "SCAN")) &&
1265             !strcmp(res, "OK\n") )
1266         {
1267                 sleep(2);
1268
1269                 if( (res = nl80211_wpasupp_info(ifname, "SCAN_RESULTS")) )
1270                 {
1271                         nl80211_get_quality_max(ifname, &qmax);
1272
1273                         /* skip header line */
1274                         while( *res++ != '\n' );
1275
1276                         count = 0;
1277
1278                         while( sscanf(res, "%17s %d %d %255s %127[^\n]\n",
1279                                       bssid, &freq, &rssi, cipher, ssid) > 0 )
1280                         {
1281                                 /* BSSID */
1282                                 e->mac[0] = strtol(&bssid[0],  NULL, 16);
1283                                 e->mac[1] = strtol(&bssid[3],  NULL, 16);
1284                                 e->mac[2] = strtol(&bssid[6],  NULL, 16);
1285                                 e->mac[3] = strtol(&bssid[9],  NULL, 16);
1286                                 e->mac[4] = strtol(&bssid[12], NULL, 16);
1287                                 e->mac[5] = strtol(&bssid[15], NULL, 16);
1288
1289                                 /* SSID */
1290                                 memcpy(e->ssid, ssid,
1291                                         min(strlen(ssid), sizeof(e->ssid) - 1));
1292
1293                                 /* Mode (assume master) */
1294                                 sprintf((char *)e->mode, "Master");
1295
1296                                 /* Channel */
1297                                 e->channel = nl80211_freq2channel(freq);
1298
1299                                 /* Signal */
1300                                 e->signal = rssi;
1301
1302                                 /* Quality */
1303                                 if( rssi < 0 )
1304                                 {
1305                                         /* The cfg80211 wext compat layer assumes a signal range
1306                                          * of -110 dBm to -40 dBm, the quality value is derived
1307                                          * by adding 110 to the signal level */
1308                                         if( rssi < -110 )
1309                                                 rssi = -110;
1310                                         else if( rssi > -40 )
1311                                                 rssi = -40;
1312
1313                                         e->quality = (rssi + 110);
1314                                 }
1315                                 else
1316                                 {
1317                                         e->quality = rssi;
1318                                 }
1319
1320                                 /* Max. Quality */
1321                                 e->quality_max = qmax;
1322
1323                                 /* Crypto */
1324                                 nl80211_get_scancrypto(cipher, &e->crypto);
1325
1326                                 /* advance to next line */
1327                                 while( *res && *res++ != '\n' );
1328
1329                                 count++;
1330                                 e++;
1331                         }
1332
1333                         *len = count * sizeof(struct iwinfo_scanlist_entry);
1334                         return 0;
1335                 }
1336         }
1337
1338         /* AP scan */
1339         else
1340         {
1341                 /* Got a temp interface, don't create yet another one */
1342                 if( !strncmp(ifname, "tmp.", 4) )
1343                 {
1344                         if( !nl80211_ifup(ifname) )
1345                                 return -1;
1346
1347                         wext_get_scanlist(ifname, buf, len);
1348                         nl80211_ifdown(ifname);
1349                         return 0;
1350                 }
1351
1352                 /* Spawn a new scan interface */
1353                 else
1354                 {
1355                         if( !(res = nl80211_ifadd(ifname)) )
1356                                 goto out;
1357
1358                         if( !nl80211_ifmac(res) )
1359                                 goto out;
1360
1361                         /* if we can take the new interface up, the driver supports an
1362                          * additional interface and there's no need to tear down the ap */
1363                         if( nl80211_ifup(res) )
1364                         {
1365                                 wext_get_scanlist(res, buf, len);
1366                                 nl80211_ifdown(res);
1367                         }
1368
1369                         /* driver cannot create secondary interface, take down ap
1370                          * during scan */
1371                         else if( nl80211_ifdown(ifname) && nl80211_ifup(res) )
1372                         {
1373                                 wext_get_scanlist(res, buf, len);
1374                                 nl80211_ifdown(res);
1375                                 nl80211_ifup(ifname);
1376                                 nl80211_hostapd_hup(ifname);
1377                         }
1378
1379                 out:
1380                         nl80211_ifdel(res);
1381                         return 0;
1382                 }
1383         }
1384
1385         return -1;
1386 }
1387
1388 int nl80211_get_freqlist(const char *ifname, char *buf, int *len)
1389 {
1390         int count = 0, bands_remain, freqs_remain;
1391         struct nl80211_msg_conveyor *req, *res;
1392         struct nlattr *bands[NL80211_BAND_ATTR_MAX + 1];
1393         struct nlattr *freqs[NL80211_FREQUENCY_ATTR_MAX + 1];
1394         struct nlattr *band, *freq;
1395         struct iwinfo_freqlist_entry *e = (struct iwinfo_freqlist_entry *)buf;
1396
1397         req = nl80211_msg(ifname, NL80211_CMD_GET_WIPHY, 0);
1398         if( req )
1399         {
1400                 res = nl80211_send(req);
1401                 if( res )
1402                 {
1403                         nla_for_each_nested(band,
1404                                 res->attr[NL80211_ATTR_WIPHY_BANDS], bands_remain)
1405                         {
1406                                 nla_parse(bands, NL80211_BAND_ATTR_MAX, nla_data(band),
1407                                           nla_len(band), NULL);
1408
1409                                 nla_for_each_nested(freq,
1410                                         bands[NL80211_BAND_ATTR_FREQS], freqs_remain)
1411                                 {
1412                                         nla_parse(freqs, NL80211_FREQUENCY_ATTR_MAX,
1413                                                 nla_data(freq), nla_len(freq), NULL);
1414
1415                                         if( !freqs[NL80211_FREQUENCY_ATTR_FREQ] ||
1416                                             freqs[NL80211_FREQUENCY_ATTR_DISABLED] )
1417                                                 continue;
1418
1419                                         e->mhz = nla_get_u32(freqs[NL80211_FREQUENCY_ATTR_FREQ]);
1420                                         e->channel = nl80211_freq2channel(e->mhz);
1421
1422                                         e->restricted = (
1423                                                 freqs[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] ||
1424                                                 freqs[NL80211_FREQUENCY_ATTR_NO_IBSS]      ||
1425                                                 freqs[NL80211_FREQUENCY_ATTR_RADAR]
1426                                         ) ? 1 : 0;
1427
1428                                         e++;
1429                                         count++;
1430                                 }
1431                         }
1432                         nl80211_free(res);
1433                 }
1434                 nl80211_free(req);
1435         }
1436
1437         if( count > 0 )
1438         {
1439                 *len = count * sizeof(struct iwinfo_freqlist_entry);
1440                 return 0;
1441         }
1442
1443         return -1;
1444 }
1445
1446 int nl80211_get_country(const char *ifname, char *buf)
1447 {
1448         int rv = -1;
1449         struct nl80211_msg_conveyor *req, *res;
1450
1451         req = nl80211_msg(ifname, NL80211_CMD_GET_REG, 0);
1452         if( req )
1453         {
1454                 res = nl80211_send(req);
1455                 if( res )
1456                 {
1457                         if( res->attr[NL80211_ATTR_REG_ALPHA2] )
1458                         {
1459                                 memcpy(buf, nla_data(res->attr[NL80211_ATTR_REG_ALPHA2]), 2);
1460                                 rv = 0;
1461                         }
1462                         nl80211_free(res);
1463                 }
1464                 nl80211_free(req);
1465         }
1466
1467         return rv;
1468 }
1469
1470 int nl80211_get_countrylist(const char *ifname, char *buf, int *len)
1471 {
1472         int i, count;
1473         struct iwinfo_iso3166_label *l;
1474         struct iwinfo_country_entry *e = (struct iwinfo_country_entry *)buf;
1475
1476         for( l = ISO3166_Names, count = 0; l->iso3166; l++, e++, count++ )
1477         {
1478                 e->iso3166 = l->iso3166;
1479                 e->ccode[0] = (l->iso3166 / 256);
1480                 e->ccode[1] = (l->iso3166 % 256);
1481         }
1482
1483         *len = (count * sizeof(struct iwinfo_country_entry));
1484         return 0;
1485 }
1486
1487 int nl80211_get_hwmodelist(const char *ifname, int *buf)
1488 {
1489         int bands_remain, freqs_remain;
1490         struct nl80211_msg_conveyor *req, *res;
1491         struct nlattr *bands[NL80211_BAND_ATTR_MAX + 1];
1492         struct nlattr *freqs[NL80211_FREQUENCY_ATTR_MAX + 1];
1493         struct nlattr *band, *freq;
1494         uint16_t caps = 0;
1495
1496         req = nl80211_msg(ifname, NL80211_CMD_GET_WIPHY, 0);
1497         if( req )
1498         {
1499                 res = nl80211_send(req);
1500                 if( res )
1501                 {
1502                         nla_for_each_nested(band,
1503                                 res->attr[NL80211_ATTR_WIPHY_BANDS], bands_remain)
1504                         {
1505                                 nla_parse(bands, NL80211_BAND_ATTR_MAX, nla_data(band),
1506                                           nla_len(band), NULL);
1507
1508                                 if( bands[NL80211_BAND_ATTR_HT_CAPA] )
1509                                         caps = nla_get_u16(bands[NL80211_BAND_ATTR_HT_CAPA]);
1510
1511                                 /* Treat HT20/HT40 as 11n */
1512                                 if( caps & (1 << 1) )
1513                                         *buf |= IWINFO_80211_N;
1514
1515                                 nla_for_each_nested(freq,
1516                                         bands[NL80211_BAND_ATTR_FREQS], freqs_remain)
1517                                 {
1518                                         nla_parse(freqs, NL80211_FREQUENCY_ATTR_MAX,
1519                                                 nla_data(freq), nla_len(freq), NULL);
1520
1521                                         if( !freqs[NL80211_FREQUENCY_ATTR_FREQ] )
1522                                                 continue;
1523
1524                                         if( nla_get_u32(freqs[NL80211_FREQUENCY_ATTR_FREQ]) < 2485 )
1525                                         {
1526                                                 *buf |= IWINFO_80211_B;
1527                                                 *buf |= IWINFO_80211_G;
1528                                         }
1529                                         else
1530                                         {
1531                                                 *buf |= IWINFO_80211_A;
1532                                         }
1533                                 }
1534                         }
1535                         nl80211_free(res);
1536                 }
1537                 nl80211_free(req);
1538         }
1539
1540         return *buf ? 0 : -1;
1541 }
1542
1543 int nl80211_get_mbssid_support(const char *ifname, int *buf)
1544 {
1545         /* We assume that multi bssid is always possible */
1546         *buf = 1;
1547         return 0;
1548 }