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