libiwinfo: support txpwrlist(), freqlist() and countrylist() for radioX pseudodevices...
[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
389 int nl80211_probe(const char *ifname)
390 {
391         return !!nl80211_ifname2phy(ifname);
392 }
393
394 void nl80211_close(void)
395 {
396         if( nls )
397         {
398                 if( nls->nl_sock )
399                         nl_socket_free(nls->nl_sock);
400
401                 if( nls->nl_cache )
402                         nl_cache_free(nls->nl_cache);
403
404                 free(nls);
405                 nls = NULL;
406         }
407 }
408
409 int nl80211_get_mode(const char *ifname, char *buf)
410 {
411         return wext_get_mode(ifname, buf);
412 }
413
414 int nl80211_get_ssid(const char *ifname, char *buf)
415 {
416         char *ssid;
417
418         if( !wext_get_ssid(ifname, buf) )
419         {
420                 return 0;
421         }
422         else if( (ssid = nl80211_hostapd_info(ifname)) &&
423                  (ssid = nl80211_getval(ssid, "ssid")) )
424         {
425                 memcpy(buf, ssid, strlen(ssid));
426                 return 0;
427         }
428
429         return -1;
430 }
431
432 int nl80211_get_bssid(const char *ifname, char *buf)
433 {
434         char *bssid;
435         unsigned char mac[6];
436
437         if( !wext_get_bssid(ifname, buf) )
438         {
439                 return 0;
440         }
441         else if( (bssid = nl80211_hostapd_info(ifname)) &&
442                  (bssid = nl80211_getval(bssid, "bssid")) )
443         {
444                 mac[0] = strtol(&bssid[0],  NULL, 16);
445                 mac[1] = strtol(&bssid[3],  NULL, 16);
446                 mac[2] = strtol(&bssid[6],  NULL, 16);
447                 mac[3] = strtol(&bssid[9],  NULL, 16);
448                 mac[4] = strtol(&bssid[12], NULL, 16);
449                 mac[5] = strtol(&bssid[15], NULL, 16);
450
451                 sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
452                         mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
453
454                 return 0;
455         }
456
457         return -1;
458 }
459
460 int nl80211_get_channel(const char *ifname, int *buf)
461 {
462         return wext_get_channel(ifname, buf);
463 }
464
465 int nl80211_get_frequency(const char *ifname, int *buf)
466 {
467         return wext_get_frequency(ifname, buf);
468 }
469
470 int nl80211_get_txpower(const char *ifname, int *buf)
471 {
472         return wext_get_txpower(ifname, buf);
473 }
474
475
476 static int nl80211_get_signal_cb(struct nl_msg *msg, void *arg)
477 {
478         int8_t dbm;
479         int16_t mbit;
480         struct nl80211_rssi_rate *rr = arg;
481
482         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
483         struct nlattr *attr[NL80211_ATTR_MAX + 1];
484         struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
485         struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
486
487         static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
488                 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32    },
489                 [NL80211_STA_INFO_RX_BYTES]      = { .type = NLA_U32    },
490                 [NL80211_STA_INFO_TX_BYTES]      = { .type = NLA_U32    },
491                 [NL80211_STA_INFO_RX_PACKETS]    = { .type = NLA_U32    },
492                 [NL80211_STA_INFO_TX_PACKETS]    = { .type = NLA_U32    },
493                 [NL80211_STA_INFO_SIGNAL]        = { .type = NLA_U8     },
494                 [NL80211_STA_INFO_TX_BITRATE]    = { .type = NLA_NESTED },
495                 [NL80211_STA_INFO_LLID]          = { .type = NLA_U16    },
496                 [NL80211_STA_INFO_PLID]          = { .type = NLA_U16    },
497                 [NL80211_STA_INFO_PLINK_STATE]   = { .type = NLA_U8     },
498         };
499
500         static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
501                 [NL80211_RATE_INFO_BITRATE]      = { .type = NLA_U16  },
502                 [NL80211_RATE_INFO_MCS]          = { .type = NLA_U8   },
503                 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
504                 [NL80211_RATE_INFO_SHORT_GI]     = { .type = NLA_FLAG },
505         };
506
507         nla_parse(attr, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
508                   genlmsg_attrlen(gnlh, 0), NULL);
509
510         if( attr[NL80211_ATTR_STA_INFO] )
511         {
512                 if( !nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
513                                 attr[NL80211_ATTR_STA_INFO], stats_policy) )
514                 {
515                         if( sinfo[NL80211_STA_INFO_SIGNAL] )
516                         {
517                                 dbm = nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
518                                 rr->rssi = rr->rssi ? (int8_t)((rr->rssi + dbm) / 2) : dbm;
519                         }
520
521                         if( sinfo[NL80211_STA_INFO_TX_BITRATE] )
522                         {
523                                 if( !nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
524                                                 sinfo[NL80211_STA_INFO_TX_BITRATE], rate_policy) )
525                                 {
526                                         if( rinfo[NL80211_RATE_INFO_BITRATE] )
527                                         {
528                                                 mbit = nla_get_u16(rinfo[NL80211_RATE_INFO_BITRATE]);
529                                                 rr->rate = rr->rate
530                                                         ? (int16_t)((rr->rate + mbit) / 2) : mbit;
531                                         }
532                                 }
533                         }
534                 }
535         }
536
537         return NL_SKIP;
538 }
539
540 int nl80211_get_bitrate(const char *ifname, int *buf)
541 {
542         struct nl80211_rssi_rate rr;
543         struct nl80211_msg_conveyor *req;
544
545         if( !wext_get_bitrate(ifname, buf) )
546                 return 0;
547
548         req = nl80211_msg(ifname, NL80211_CMD_GET_STATION, NLM_F_DUMP);
549         if( req )
550         {
551                 rr.rssi = 0;
552                 rr.rate = 0;
553
554                 nl80211_cb(req, nl80211_get_signal_cb, &rr);
555                 nl80211_send(req);
556                 nl80211_free(req);
557
558                 if( rr.rate )
559                 {
560                         *buf = (rr.rate * 100);
561                         return 0;
562                 }
563         }
564
565         return -1;
566 }
567
568 int nl80211_get_signal(const char *ifname, int *buf)
569 {
570         struct nl80211_rssi_rate rr;
571         struct nl80211_msg_conveyor *req;
572
573         if( !wext_get_signal(ifname, buf) )
574                 return 0;
575
576         req = nl80211_msg(ifname, NL80211_CMD_GET_STATION, NLM_F_DUMP);
577         if( req )
578         {
579                 rr.rssi = 0;
580                 rr.rate = 0;
581
582                 nl80211_cb(req, nl80211_get_signal_cb, &rr);
583                 nl80211_send(req);
584                 nl80211_free(req);
585
586                 if( rr.rssi )
587                 {
588                         *buf = rr.rssi;
589                         return 0;
590                 }
591         }
592
593         return -1;
594 }
595
596 int nl80211_get_noise(const char *ifname, int *buf)
597 {
598         int rv = -1;
599         struct nl80211_msg_conveyor *req, *res;
600         struct nlattr *si[NL80211_SURVEY_INFO_MAX + 1];
601
602         static struct nla_policy sp[NL80211_SURVEY_INFO_MAX + 1] = {
603                 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
604                 [NL80211_SURVEY_INFO_NOISE]     = { .type = NLA_U8  },
605         };
606
607         req = nl80211_msg(ifname, NL80211_CMD_GET_SURVEY, NLM_F_DUMP);
608         if( req )
609         {
610                 res = nl80211_send(req);
611                 if( res )
612                 {
613                         if( res->attr[NL80211_ATTR_SURVEY_INFO] )
614                         {
615                                 if( !nla_parse_nested(si, NL80211_SURVEY_INFO_MAX,
616                                                 res->attr[NL80211_ATTR_SURVEY_INFO], sp) &&
617                                         si[NL80211_SURVEY_INFO_NOISE] )
618                                 {
619                                         *buf = (int8_t)nla_get_u8(si[NL80211_SURVEY_INFO_NOISE]);
620                                         rv = 0;
621                                 }
622                         }
623                         nl80211_free(res);
624                 }
625                 nl80211_free(req);
626         }
627
628         return rv;
629 }
630
631 int nl80211_get_quality(const char *ifname, int *buf)
632 {
633         int signal;
634
635         if( wext_get_quality(ifname, buf) )
636         {
637                 *buf = 0;
638
639                 if( !nl80211_get_signal(ifname, &signal) )
640                 {
641                         /* A positive signal level is usually just a quality
642                          * value, pass through as-is */
643                         if( signal >= 0 )
644                         {
645                                 *buf = signal;
646                         }
647
648                         /* The cfg80211 wext compat layer assumes a signal range
649                          * of -110 dBm to -40 dBm, the quality value is derived
650                          * by adding 110 to the signal level */
651                         else
652                         {
653                                 if( signal < -110 )
654                                         signal = -110;
655                                 else if( signal > -40 )
656                                         signal = -40;
657
658                                 *buf = (signal + 110);
659                         }
660                 }
661         }
662
663         return 0;
664 }
665
666 int nl80211_get_quality_max(const char *ifname, int *buf)
667 {
668         if( wext_get_quality_max(ifname, buf) )
669                 /* The cfg80211 wext compat layer assumes a maximum
670                  * quality of 70 */
671                 *buf = 70;
672
673         return 0;
674 }
675
676 int nl80211_get_encryption(const char *ifname, char *buf)
677 {
678         int i;
679         char k[9];
680         char *val, *res;
681         struct iwinfo_crypto_entry *c = (struct iwinfo_crypto_entry *)buf;
682
683         /* Hostapd */
684         if( (res = nl80211_hostapd_info(ifname)) &&
685             nl80211_getval(res, "interface") )
686         {
687                 if( (val = nl80211_getval(res, "auth_algs")) && (val > 0) )
688                 {
689                         c->auth_suites |= IWINFO_KMGMT_NONE;
690
691                         switch(atoi(val)) {
692                                 case 1:
693                                         c->auth_algs |= IWINFO_AUTH_OPEN;
694                                         break;
695
696                                 case 2:
697                                         c->auth_algs |= IWINFO_AUTH_SHARED;
698                                         break;
699
700                                 case 3:
701                                         c->auth_algs |= IWINFO_AUTH_OPEN;
702                                         c->auth_algs |= IWINFO_AUTH_SHARED;
703                                         break;
704
705                                 default:
706                                         break;
707                         }
708
709                         for( i = 0; i < 4; i++ )
710                         {
711                                 snprintf(k, sizeof(k), "wep_key%d", i);
712
713                                 if( (val = nl80211_getval(res, k)) )
714                                 {
715                                         if( (strlen(val) == 5) || (strlen(val) == 10) )
716                                                 c->pair_ciphers |= IWINFO_CIPHER_WEP40;
717
718                                         else if( (strlen(val) == 13) || (strlen(val) == 26) )
719                                                 c->pair_ciphers |= IWINFO_CIPHER_WEP104;
720                                 }
721                         }
722
723                         c->group_ciphers = c->pair_ciphers;
724
725                         return 0;
726                 }
727
728
729                 if( (val = nl80211_getval(res, "wpa")) != NULL )
730                         c->wpa_version = atoi(val);
731
732
733                 val = nl80211_getval(res, "wpa_key_mgmt");
734
735                 if( !val || strstr(val, "PSK") )
736                         c->auth_suites |= IWINFO_KMGMT_PSK;
737
738                 if( val && strstr(val, "EAP") )
739                         c->auth_suites |= IWINFO_KMGMT_8021x;
740
741                 if( val && strstr(val, "NONE") )
742                         c->auth_suites |= IWINFO_KMGMT_NONE;
743
744
745                 if( (val = nl80211_getval(res, "wpa_pairwise")) != NULL )
746                 {
747                         if( strstr(val, "TKIP") )
748                                 c->pair_ciphers |= IWINFO_CIPHER_TKIP;
749
750                         if( strstr(val, "CCMP") )
751                                 c->pair_ciphers |= IWINFO_CIPHER_CCMP;
752
753                         if( strstr(val, "NONE") )
754                                 c->pair_ciphers |= IWINFO_CIPHER_NONE;
755                 }
756
757
758                 c->group_ciphers = c->pair_ciphers;
759                 c->enabled = (c->auth_algs || c->auth_suites) ? 1 : 0;
760
761                 return 0;
762         }
763
764         /* WPA supplicant */
765         else if( (res = nl80211_wpasupp_info(ifname, "STATUS")) &&
766                  (val = nl80211_getval(res, "pairwise_cipher")) )
767         {
768                 /* WEP */
769                 if( strstr(val, "WEP") )
770                 {
771                         if( strstr(val, "WEP-40") )
772                                 c->pair_ciphers |= IWINFO_CIPHER_WEP40;
773
774                         else if( strstr(val, "WEP-104") )
775                                 c->pair_ciphers |= IWINFO_CIPHER_WEP104;
776
777                         c->enabled       = 1;
778                         c->group_ciphers = c->pair_ciphers;
779
780                         c->auth_suites |= IWINFO_KMGMT_NONE;
781                         c->auth_algs   |= IWINFO_AUTH_OPEN; /* XXX: assumption */
782                 }
783
784                 /* WPA */
785                 else
786                 {
787                         if( strstr(val, "TKIP") )
788                                 c->pair_ciphers |= IWINFO_CIPHER_TKIP;
789
790                         else if( strstr(val, "CCMP") )
791                                 c->pair_ciphers |= IWINFO_CIPHER_CCMP;
792
793                         else if( strstr(val, "NONE") )
794                                 c->pair_ciphers |= IWINFO_CIPHER_NONE;
795
796                         else if( strstr(val, "WEP-40") )
797                                 c->pair_ciphers |= IWINFO_CIPHER_WEP40;
798
799                         else if( strstr(val, "WEP-104") )
800                                 c->pair_ciphers |= IWINFO_CIPHER_WEP104;
801
802
803                         if( (val = nl80211_getval(res, "group_cipher")) )
804                         {
805                                 if( strstr(val, "TKIP") )
806                                         c->group_ciphers |= IWINFO_CIPHER_TKIP;
807
808                                 else if( strstr(val, "CCMP") )
809                                         c->group_ciphers |= IWINFO_CIPHER_CCMP;
810
811                                 else if( strstr(val, "NONE") )
812                                         c->group_ciphers |= IWINFO_CIPHER_NONE;
813
814                                 else if( strstr(val, "WEP-40") )
815                                         c->group_ciphers |= IWINFO_CIPHER_WEP40;
816
817                                 else if( strstr(val, "WEP-104") )
818                                         c->group_ciphers |= IWINFO_CIPHER_WEP104;
819                         }
820
821
822                         if( (val = nl80211_getval(res, "key_mgmt")) )
823                         {
824                                 if( strstr(val, "WPA2") )
825                                         c->wpa_version = 2;
826
827                                 else if( strstr(val, "WPA") )
828                                         c->wpa_version = 1;
829
830
831                                 if( strstr(val, "PSK") )
832                                         c->auth_suites |= IWINFO_KMGMT_PSK;
833
834                                 else if( strstr(val, "EAP") || strstr(val, "802.1X") )
835                                         c->auth_suites |= IWINFO_KMGMT_8021x;
836
837                                 else if( strstr(val, "NONE") )
838                                         c->auth_suites |= IWINFO_KMGMT_NONE;
839                         }
840
841                         c->enabled = (c->wpa_version && c->auth_suites) ? 1 : 0;
842                 }
843
844                 return 0;
845         }
846
847         return -1;
848 }
849
850
851 static int nl80211_get_assoclist_cb(struct nl_msg *msg, void *arg)
852 {
853         struct nl80211_assoc_count *ac = arg;
854         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
855         struct nlattr *attr[NL80211_ATTR_MAX + 1];
856         struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
857
858         static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
859                 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32    },
860                 [NL80211_STA_INFO_RX_BYTES]      = { .type = NLA_U32    },
861                 [NL80211_STA_INFO_TX_BYTES]      = { .type = NLA_U32    },
862                 [NL80211_STA_INFO_RX_PACKETS]    = { .type = NLA_U32    },
863                 [NL80211_STA_INFO_TX_PACKETS]    = { .type = NLA_U32    },
864                 [NL80211_STA_INFO_SIGNAL]        = { .type = NLA_U8     },
865                 [NL80211_STA_INFO_TX_BITRATE]    = { .type = NLA_NESTED },
866                 [NL80211_STA_INFO_LLID]          = { .type = NLA_U16    },
867                 [NL80211_STA_INFO_PLID]          = { .type = NLA_U16    },
868                 [NL80211_STA_INFO_PLINK_STATE]   = { .type = NLA_U8     },
869         };
870
871         nla_parse(attr, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
872                 genlmsg_attrlen(gnlh, 0), NULL);
873
874         if( attr[NL80211_ATTR_MAC] )
875                 memcpy(ac->entry->mac, nla_data(attr[NL80211_ATTR_MAC]), 6);
876
877         if( attr[NL80211_ATTR_STA_INFO] )
878         {
879                 if( !nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
880                                 attr[NL80211_ATTR_STA_INFO], stats_policy) )
881                 {
882                         if( sinfo[NL80211_STA_INFO_SIGNAL] )
883                                 ac->entry->signal = nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
884                 }
885         }
886
887         ac->entry->noise = ac->noise;
888         ac->entry++;
889         ac->count++;
890
891         return NL_SKIP;
892 }
893
894 int nl80211_get_assoclist(const char *ifname, char *buf, int *len)
895 {
896         struct nl80211_assoc_count ac;
897         struct nl80211_msg_conveyor *req;
898
899         nl80211_get_noise(ifname, &ac.noise);
900
901         req = nl80211_msg(ifname, NL80211_CMD_GET_STATION, NLM_F_DUMP);
902         if( req )
903         {
904                 ac.count = 0;
905                 ac.entry = (struct iwinfo_assoclist_entry *)buf;
906
907                 nl80211_cb(req, nl80211_get_assoclist_cb, &ac);
908                 nl80211_send(req);
909                 nl80211_free(req);
910
911                 *len = (ac.count * sizeof(struct iwinfo_assoclist_entry));
912                 return 0;
913         }
914
915         return -1;
916 }
917
918 int nl80211_get_txpwrlist(const char *ifname, char *buf, int *len)
919 {
920         int ch_cur, ch_cmp, bands_remain, freqs_remain;
921         int dbm_max = -1, dbm_cur, dbm_cnt;
922         struct nl80211_msg_conveyor *req, *res;
923         struct nlattr *bands[NL80211_BAND_ATTR_MAX + 1];
924         struct nlattr *freqs[NL80211_FREQUENCY_ATTR_MAX + 1];
925         struct nlattr *band, *freq;
926         struct iwinfo_txpwrlist_entry entry;
927
928         static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
929                 [NL80211_FREQUENCY_ATTR_FREQ]         = { .type = NLA_U32  },
930                 [NL80211_FREQUENCY_ATTR_DISABLED]     = { .type = NLA_FLAG },
931                 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
932                 [NL80211_FREQUENCY_ATTR_NO_IBSS]      = { .type = NLA_FLAG },
933                 [NL80211_FREQUENCY_ATTR_RADAR]        = { .type = NLA_FLAG },
934                 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32  },
935         };
936
937         if( nl80211_get_channel(ifname, &ch_cur) )
938                 ch_cur = 0;
939
940         req = nl80211_msg(ifname, NL80211_CMD_GET_WIPHY, 0);
941         if( req )
942         {
943                 res = nl80211_send(req);
944                 if( res )
945                 {
946                         nla_for_each_nested(band,
947                                 res->attr[NL80211_ATTR_WIPHY_BANDS], bands_remain)
948                         {
949                                 nla_parse(bands, NL80211_BAND_ATTR_MAX, nla_data(band),
950                                           nla_len(band), NULL);
951
952                                 nla_for_each_nested(freq,
953                                         bands[NL80211_BAND_ATTR_FREQS], freqs_remain)
954                                 {
955                                         nla_parse(freqs, NL80211_FREQUENCY_ATTR_MAX,
956                                                 nla_data(freq), nla_len(freq), freq_policy);
957
958                                         ch_cmp = nl80211_freq2channel(
959                                                 nla_get_u32(freqs[NL80211_FREQUENCY_ATTR_FREQ]));
960
961                                         if( (!ch_cur || (ch_cmp == ch_cur)) &&
962                                             freqs[NL80211_FREQUENCY_ATTR_MAX_TX_POWER] )
963                                         {
964                                                 dbm_max = (int)(0.01 * nla_get_u32(
965                                                         freqs[NL80211_FREQUENCY_ATTR_MAX_TX_POWER]));
966
967                                                 break;
968                                         }
969                                 }
970                         }
971
972                         nl80211_free(res);
973                 }
974                 nl80211_free(req);
975         }
976
977         if( dbm_max > -1 )
978         {
979                 for( dbm_cur = 0, dbm_cnt = 0;
980                      dbm_cur < dbm_max;
981                      dbm_cur += 2, dbm_cnt++ )
982                 {
983                         entry.dbm = dbm_cur;
984                         entry.mw  = wext_dbm2mw(dbm_cur);
985
986                         memcpy(&buf[dbm_cnt * sizeof(entry)], &entry, sizeof(entry));
987                 }
988
989                 entry.dbm = dbm_max;
990                 entry.mw  = wext_dbm2mw(dbm_max);
991
992                 memcpy(&buf[dbm_cnt * sizeof(entry)], &entry, sizeof(entry));
993                 dbm_cnt++;
994
995                 *len = dbm_cnt * sizeof(entry);
996                 return 0;
997         }
998
999         return -1;
1000 }
1001
1002 static void nl80211_get_scancrypto(const char *spec,
1003         struct iwinfo_crypto_entry *c)
1004 {
1005         if( strstr(spec, "OPEN") )
1006         {
1007                 c->enabled = 0;
1008         }
1009         else
1010         {
1011                 c->enabled = 1;
1012
1013                 if( strstr(spec, "WPA2-") && strstr(spec, "WPA-") )
1014                         c->wpa_version = 3;
1015
1016                 else if( strstr(spec, "WPA2") )
1017                         c->wpa_version = 2;
1018
1019                 else if( strstr(spec, "WPA") )
1020                         c->wpa_version = 1;
1021
1022                 else if( strstr(spec, "WEP") )
1023                         c->auth_algs = IWINFO_AUTH_OPEN | IWINFO_AUTH_SHARED;
1024
1025
1026                 if( strstr(spec, "PSK") )
1027                         c->auth_suites |= IWINFO_KMGMT_PSK;
1028
1029                 if( strstr(spec, "802.1X") || strstr(spec, "EAP") )
1030                         c->auth_suites |= IWINFO_KMGMT_8021x;
1031
1032                 if( strstr(spec, "WPA-NONE") )
1033                         c->auth_suites |= IWINFO_KMGMT_NONE;
1034
1035
1036                 if( strstr(spec, "TKIP") )
1037                         c->pair_ciphers |= IWINFO_CIPHER_TKIP;
1038
1039                 if( strstr(spec, "CCMP") )
1040                         c->pair_ciphers |= IWINFO_CIPHER_CCMP;
1041
1042                 if( strstr(spec, "WEP-40") )
1043                         c->pair_ciphers |= IWINFO_CIPHER_WEP40;
1044
1045                 if( strstr(spec, "WEP-104") )
1046                         c->pair_ciphers |= IWINFO_CIPHER_WEP104;
1047
1048                 c->group_ciphers = c->pair_ciphers;
1049         }
1050 }
1051
1052 int nl80211_get_scanlist(const char *ifname, char *buf, int *len)
1053 {
1054         int freq, rssi, qmax, count;
1055         char *res;
1056         char cmd[256];
1057         char ssid[128] = { 0 };
1058         char bssid[18] = { 0 };
1059         char cipher[256] = { 0 };
1060
1061         struct iwinfo_scanlist_entry *e = (struct iwinfo_scanlist_entry *)buf;
1062
1063         /* WPA supplicant */
1064         if( (res = nl80211_wpasupp_info(ifname, "SCAN")) &&
1065             !strcmp(res, "OK\n") )
1066         {
1067                 sleep(2);
1068
1069                 if( (res = nl80211_wpasupp_info(ifname, "SCAN_RESULTS")) )
1070                 {
1071                         nl80211_get_quality_max(ifname, &qmax);
1072
1073                         /* skip header line */
1074                         while( *res++ != '\n' );
1075
1076                         count = 0;
1077
1078                         while( sscanf(res, "%17s %d %d %255s %127[^\n]\n",
1079                                       bssid, &freq, &rssi, cipher, ssid) > 0 )
1080                         {
1081                                 /* BSSID */
1082                                 e->mac[0] = strtol(&bssid[0],  NULL, 16);
1083                                 e->mac[1] = strtol(&bssid[3],  NULL, 16);
1084                                 e->mac[2] = strtol(&bssid[6],  NULL, 16);
1085                                 e->mac[3] = strtol(&bssid[9],  NULL, 16);
1086                                 e->mac[4] = strtol(&bssid[12], NULL, 16);
1087                                 e->mac[5] = strtol(&bssid[15], NULL, 16);
1088
1089                                 /* SSID */
1090                                 memcpy(e->ssid, ssid,
1091                                         min(strlen(ssid), sizeof(e->ssid) - 1));
1092
1093                                 /* Mode (assume master) */
1094                                 sprintf((char *)e->mode, "Master");
1095
1096                                 /* Channel */
1097                                 e->channel = nl80211_freq2channel(freq);
1098
1099                                 /* Signal */
1100                                 e->signal = rssi;
1101
1102                                 /* Quality */
1103                                 if( rssi < 0 )
1104                                 {
1105                                         /* The cfg80211 wext compat layer assumes a signal range
1106                                          * of -110 dBm to -40 dBm, the quality value is derived
1107                                          * by adding 110 to the signal level */
1108                                         if( rssi < -110 )
1109                                                 rssi = -110;
1110                                         else if( rssi > -40 )
1111                                                 rssi = -40;
1112
1113                                         e->quality = (rssi + 110);
1114                                 }
1115                                 else
1116                                 {
1117                                         e->quality = rssi;
1118                                 }
1119
1120                                 /* Max. Quality */
1121                                 e->quality_max = qmax;
1122
1123                                 /* Crypto */
1124                                 nl80211_get_scancrypto(cipher, &e->crypto);
1125
1126                                 /* advance to next line */
1127                                 while( *res && *res++ != '\n' );
1128
1129                                 count++;
1130                                 e++;
1131                         }
1132
1133                         *len = count * sizeof(struct iwinfo_scanlist_entry);
1134                         return 0;
1135                 }
1136         }
1137
1138         /* AP scan */
1139         else
1140         {
1141                 if( (res = nl80211_ifname2phy(ifname)) != NULL )
1142                 {
1143                         /*
1144                          * This is a big ugly hack, just look away.
1145                          */
1146
1147                         sprintf(cmd, "ifconfig %s down 2>/dev/null", ifname);
1148                         if( WEXITSTATUS(system(cmd)) )
1149                                 goto out;
1150
1151                         sprintf(cmd, "iw phy %s interface add scan.%s "
1152                                 "type station 2>/dev/null", res, ifname);
1153                         if( WEXITSTATUS(system(cmd)) )
1154                                 goto out;
1155
1156                         sprintf(cmd, "ifconfig scan.%s up 2>/dev/null", ifname);
1157                         if( WEXITSTATUS(system(cmd)) )
1158                                 goto out;
1159
1160                         sprintf(cmd, "scan.%s", ifname);
1161                         wext_get_scanlist(cmd, buf, len);
1162
1163         out:
1164                         sprintf(cmd, "ifconfig scan.%s down 2>/dev/null", ifname);
1165                         (void) WEXITSTATUS(system(cmd));
1166
1167                         sprintf(cmd, "iw dev scan.%s del 2>/dev/null", ifname);
1168                         (void) WEXITSTATUS(system(cmd));
1169
1170                         sprintf(cmd, "ifconfig %s up 2>/dev/null", ifname);
1171                         (void) WEXITSTATUS(system(cmd));
1172
1173                         sprintf(cmd, "killall -HUP hostapd 2>/dev/null");
1174                         (void) WEXITSTATUS(system(cmd));
1175
1176                         return 0;
1177                 }
1178         }
1179
1180         return -1;
1181 }
1182
1183 int nl80211_get_freqlist(const char *ifname, char *buf, int *len)
1184 {
1185         char *phy;
1186         int count = 0, bands_remain, freqs_remain;
1187         struct nl80211_msg_conveyor *req, *res;
1188         struct nlattr *bands[NL80211_BAND_ATTR_MAX + 1];
1189         struct nlattr *freqs[NL80211_FREQUENCY_ATTR_MAX + 1];
1190         struct nlattr *band, *freq;
1191         struct iwinfo_freqlist_entry *e = (struct iwinfo_freqlist_entry *)buf;
1192
1193         static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
1194                 [NL80211_FREQUENCY_ATTR_FREQ]         = { .type = NLA_U32  },
1195                 [NL80211_FREQUENCY_ATTR_DISABLED]     = { .type = NLA_FLAG },
1196                 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
1197                 [NL80211_FREQUENCY_ATTR_NO_IBSS]      = { .type = NLA_FLAG },
1198                 [NL80211_FREQUENCY_ATTR_RADAR]        = { .type = NLA_FLAG },
1199                 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32  },
1200         };
1201
1202         if( !wext_get_freqlist(ifname, buf, len) )
1203                 return 0;
1204
1205         req = nl80211_msg(ifname, NL80211_CMD_GET_WIPHY, 0);
1206         if( req )
1207         {
1208                 res = nl80211_send(req);
1209                 if( res )
1210                 {
1211                         nla_for_each_nested(band,
1212                                 res->attr[NL80211_ATTR_WIPHY_BANDS], bands_remain)
1213                         {
1214                                 nla_parse(bands, NL80211_BAND_ATTR_MAX, nla_data(band),
1215                                           nla_len(band), NULL);
1216
1217                                 nla_for_each_nested(freq,
1218                                         bands[NL80211_BAND_ATTR_FREQS], freqs_remain)
1219                                 {
1220                                         nla_parse(freqs, NL80211_FREQUENCY_ATTR_MAX,
1221                                                 nla_data(freq), nla_len(freq), freq_policy);
1222
1223                                         e->mhz = nla_get_u32(freqs[NL80211_FREQUENCY_ATTR_FREQ]);
1224                                         e->channel = nl80211_freq2channel(e->mhz);
1225
1226                                         e++;
1227                                         count++;
1228                                 }
1229                         }
1230                         nl80211_free(res);
1231                 }
1232                 nl80211_free(req);
1233         }
1234
1235         if( count > 0 )
1236         {
1237                 *len = count * sizeof(struct iwinfo_freqlist_entry);
1238                 return 0;
1239         }
1240
1241         return -1;
1242 }
1243
1244 int nl80211_get_country(const char *ifname, char *buf)
1245 {
1246         int rv = -1;
1247         struct nl80211_msg_conveyor *req, *res;
1248
1249         req = nl80211_msg(ifname, NL80211_CMD_GET_REG, 0);
1250         if( req )
1251         {
1252                 res = nl80211_send(req);
1253                 if( res )
1254                 {
1255                         if( res->attr[NL80211_ATTR_REG_ALPHA2] )
1256                         {
1257                                 memcpy(buf, nla_data(res->attr[NL80211_ATTR_REG_ALPHA2]), 2);
1258                                 rv = 0;
1259                         }
1260                         nl80211_free(res);
1261                 }
1262                 nl80211_free(req);
1263         }
1264
1265         return rv;
1266 }
1267
1268 int nl80211_get_countrylist(const char *ifname, char *buf, int *len)
1269 {
1270         int i, count;
1271         struct iwinfo_iso3166_label *l;
1272         struct iwinfo_country_entry *e = (struct iwinfo_country_entry *)buf;
1273
1274         for( l = ISO3166_Names, count = 0; l->iso3166; l++, e++, count++ )
1275         {
1276                 e->iso3166 = l->iso3166;
1277                 e->ccode[0] = (l->iso3166 / 256);
1278                 e->ccode[1] = (l->iso3166 % 256);
1279         }
1280
1281         *len = (count * sizeof(struct iwinfo_country_entry));
1282         return 0;
1283 }
1284
1285 int nl80211_get_mbssid_support(const char *ifname, int *buf)
1286 {
1287         /* We assume that multi bssid is always possible */
1288         *buf = 1;
1289         return 0;
1290 }