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