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