• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/iwinfo/iwinfo_wext.c

  1 /*
  2  * iwinfo - Wireless Information Library - Linux Wireless Extension Backend
  3  *
  4  *   Copyright (C) 2009 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  * Parts of this code are derived from the Linux wireless tools, iwlib.c,
 19  * iwlist.c and iwconfig.c in particular.
 20  */
 21 
 22 #include "iwinfo.h"
 23 #include "iwinfo_wext.h"
 24 
 25 static double wext_freq2float(const struct iw_freq *in)
 26 {
 27         int             i;
 28         double  res = (double) in->m;
 29         for(i = 0; i < in->e; i++) res *= 10;
 30         return res;
 31 }
 32 
 33 static inline int wext_freq2mhz(const struct iw_freq *in)
 34 {
 35         if( in->e == 6 )
 36         {
 37                 return in->m;
 38         }
 39         else
 40         {
 41                 return (int)(wext_freq2float(in) / 1000000);
 42         }
 43 }
 44 
 45 static inline int wext_ioctl(const char *ifname, int cmd, struct iwreq *wrq)
 46 {
 47         if( !strncmp(ifname, "mon.", 4) )
 48                 strncpy(wrq->ifr_name, &ifname[4], IFNAMSIZ);
 49         else
 50                 strncpy(wrq->ifr_name, ifname, IFNAMSIZ);
 51 
 52         return iwinfo_ioctl(cmd, wrq);
 53 }
 54 
 55 
 56 static int wext_probe(const char *ifname)
 57 {
 58         struct iwreq wrq;
 59 
 60         if(wext_ioctl(ifname, SIOCGIWNAME, &wrq) >= 0)
 61                 return 1;
 62 
 63         return 0;
 64 }
 65 
 66 static void wext_close(void)
 67 {
 68         /* Nop */
 69 }
 70 
 71 static int wext_get_mode(const char *ifname, int *buf)
 72 {
 73         struct iwreq wrq;
 74 
 75         if(wext_ioctl(ifname, SIOCGIWMODE, &wrq) >= 0)
 76         {
 77                 switch(wrq.u.mode)
 78                 {
 79                         case 1:
 80                                 *buf = IWINFO_OPMODE_ADHOC;
 81                                 break;
 82 
 83                         case 2:
 84                                 *buf = IWINFO_OPMODE_CLIENT;
 85                                 break;
 86 
 87                         case 3:
 88                                 *buf = IWINFO_OPMODE_MASTER;
 89                                 break;
 90 
 91                         case 6:
 92                                 *buf = IWINFO_OPMODE_MONITOR;
 93                                 break;
 94 
 95                         default:
 96                                 *buf = IWINFO_OPMODE_UNKNOWN;
 97                                 break;
 98                 }
 99 
100                 return 0;
101         }
102 
103         return -1;
104 }
105 
106 static int wext_get_ssid(const char *ifname, char *buf)
107 {
108         struct iwreq wrq;
109 
110         wrq.u.essid.pointer = (caddr_t) buf;
111         wrq.u.essid.length  = IW_ESSID_MAX_SIZE + 1;
112         wrq.u.essid.flags   = 0;
113 
114         if(wext_ioctl(ifname, SIOCGIWESSID, &wrq) >= 0)
115                 return 0;
116 
117         return -1;
118 }
119 
120 static int wext_get_bssid(const char *ifname, char *buf)
121 {
122         struct iwreq wrq;
123 
124         if(wext_ioctl(ifname, SIOCGIWAP, &wrq) >= 0)
125         {
126                 sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
127                         (uint8_t)wrq.u.ap_addr.sa_data[0], (uint8_t)wrq.u.ap_addr.sa_data[1],
128                         (uint8_t)wrq.u.ap_addr.sa_data[2], (uint8_t)wrq.u.ap_addr.sa_data[3],
129                         (uint8_t)wrq.u.ap_addr.sa_data[4], (uint8_t)wrq.u.ap_addr.sa_data[5]);
130 
131                 return 0;
132         }
133 
134         return -1;
135 }
136 
137 static int wext_get_bitrate(const char *ifname, int *buf)
138 {
139         struct iwreq wrq;
140 
141         if(wext_ioctl(ifname, SIOCGIWRATE, &wrq) >= 0)
142         {
143                 *buf = (wrq.u.bitrate.value / 1000);
144                 return 0;
145         }
146 
147         return -1;
148 }
149 
150 static int wext_get_channel(const char *ifname, int *buf)
151 {
152         struct iwreq wrq;
153         struct iw_range range;
154         double freq;
155         int i;
156 
157         if(wext_ioctl(ifname, SIOCGIWFREQ, &wrq) >= 0)
158         {
159                 if( wrq.u.freq.m >= 1000 )
160                 {
161                         freq = wext_freq2float(&wrq.u.freq);
162                         wrq.u.data.pointer = (caddr_t) &range;
163                         wrq.u.data.length  = sizeof(struct iw_range);
164                         wrq.u.data.flags   = 0;
165 
166                         if(wext_ioctl(ifname, SIOCGIWRANGE, &wrq) >= 0)
167                         {
168                                 for(i = 0; i < range.num_frequency; i++)
169                                 {
170                                         if( wext_freq2float(&range.freq[i]) == freq )
171                                         {
172                                                 *buf = range.freq[i].i;
173                                                 return 0;
174                                         }
175                                 }
176                         }
177                 }
178                 else
179                 {
180                         *buf = wrq.u.freq.m;
181                         return 0;
182                 }
183         }
184 
185         return -1;
186 }
187 
188 static int wext_get_frequency(const char *ifname, int *buf)
189 {
190         struct iwreq wrq;
191         struct iw_range range;
192         int i, channel;
193 
194         if(wext_ioctl(ifname, SIOCGIWFREQ, &wrq) >= 0)
195         {
196                 /* We got a channel number instead ... */
197                 if( wrq.u.freq.m < 1000 )
198                 {
199                         channel = wrq.u.freq.m;
200                         wrq.u.data.pointer = (caddr_t) &range;
201                         wrq.u.data.length  = sizeof(struct iw_range);
202                         wrq.u.data.flags   = 0;
203 
204                         if(wext_ioctl(ifname, SIOCGIWRANGE, &wrq) >= 0)
205                         {
206                                 for(i = 0; i < range.num_frequency; i++)
207                                 {
208                                         if( range.freq[i].i == channel )
209                                         {
210                                                 *buf = wext_freq2mhz(&range.freq[i]);
211                                                 return 0;
212                                         }
213                                 }
214                         }
215                 }
216                 else
217                 {
218                         *buf = wext_freq2mhz(&wrq.u.freq);
219                         return 0;
220                 }
221         }
222 
223         return -1;
224 }
225 
226 static int wext_get_txpower(const char *ifname, int *buf)
227 {
228         struct iwreq wrq;
229 
230         wrq.u.txpower.flags = 0;
231 
232         if(wext_ioctl(ifname, SIOCGIWTXPOW, &wrq) >= 0)
233         {
234                 if(wrq.u.txpower.flags & IW_TXPOW_MWATT)
235                         *buf = iwinfo_mw2dbm(wrq.u.txpower.value);
236                 else
237                         *buf = wrq.u.txpower.value;
238 
239                 return 0;
240         }
241 
242         return -1;
243 }
244 
245 static int wext_get_signal(const char *ifname, int *buf)
246 {
247         struct iwreq wrq;
248         struct iw_statistics stats;
249 
250         wrq.u.data.pointer = (caddr_t) &stats;
251         wrq.u.data.length  = sizeof(struct iw_statistics);
252         wrq.u.data.flags   = 1;
253 
254         if(wext_ioctl(ifname, SIOCGIWSTATS, &wrq) >= 0)
255         {
256                 *buf = (stats.qual.updated & IW_QUAL_DBM)
257                         ? (stats.qual.level - 0x100) : stats.qual.level;
258 
259                 return 0;
260         }
261 
262         return -1;
263 }
264 
265 static int wext_get_noise(const char *ifname, int *buf)
266 {
267         struct iwreq wrq;
268         struct iw_statistics stats;
269 
270         wrq.u.data.pointer = (caddr_t) &stats;
271         wrq.u.data.length  = sizeof(struct iw_statistics);
272         wrq.u.data.flags   = 1;
273 
274         if(wext_ioctl(ifname, SIOCGIWSTATS, &wrq) >= 0)
275         {
276                 *buf = (stats.qual.updated & IW_QUAL_DBM)
277                         ? (stats.qual.noise - 0x100) : stats.qual.noise;
278 
279                 return 0;
280         }
281 
282         return -1;
283 }
284 
285 static int wext_get_quality(const char *ifname, int *buf)
286 {
287         struct iwreq wrq;
288         struct iw_statistics stats;
289 
290         wrq.u.data.pointer = (caddr_t) &stats;
291         wrq.u.data.length  = sizeof(struct iw_statistics);
292         wrq.u.data.flags   = 1;
293 
294         if(wext_ioctl(ifname, SIOCGIWSTATS, &wrq) >= 0)
295         {
296                 *buf = stats.qual.qual;
297                 return 0;
298         }
299 
300         return -1;
301 }
302 
303 static int wext_get_quality_max(const char *ifname, int *buf)
304 {
305         struct iwreq wrq;
306         struct iw_range range;
307 
308         wrq.u.data.pointer = (caddr_t) &range;
309         wrq.u.data.length  = sizeof(struct iw_range);
310         wrq.u.data.flags   = 0;
311 
312         if(wext_ioctl(ifname, SIOCGIWRANGE, &wrq) >= 0)
313         {
314                 *buf = range.max_qual.qual;
315                 return 0;
316         }
317 
318         return -1;
319 }
320 
321 static int wext_get_assoclist(const char *ifname, char *buf, int *len)
322 {
323         /* Stub */
324         return -1;
325 }
326 
327 static int wext_get_txpwrlist(const char *ifname, char *buf, int *len)
328 {
329         struct iwreq wrq;
330         struct iw_range range;
331         struct iwinfo_txpwrlist_entry entry;
332         int i;
333 
334         wrq.u.data.pointer = (caddr_t) &range;
335         wrq.u.data.length  = sizeof(struct iw_range);
336         wrq.u.data.flags   = 0;
337 
338         if( (wext_ioctl(ifname, SIOCGIWRANGE, &wrq) >= 0) &&
339             (range.num_txpower > 0) && (range.num_txpower <= IW_MAX_TXPOWER) &&
340             !(range.txpower_capa & IW_TXPOW_RELATIVE)
341         ) {
342                 for( i = 0; i < range.num_txpower; i++ )
343                 {
344                         if( range.txpower_capa & IW_TXPOW_MWATT )
345                         {
346                                 entry.dbm = iwinfo_mw2dbm(range.txpower[i]);
347                                 entry.mw  = range.txpower[i];
348                         }
349 
350                         /* Madwifi does neither set mW not dBm caps, also iwlist assumes
351                          * dBm if mW is not set, so don't check here... */
352                         else /* if( range.txpower_capa & IW_TXPOW_DBM ) */
353                         {
354                                 entry.dbm = range.txpower[i];
355                                 entry.mw  = iwinfo_dbm2mw(range.txpower[i]);
356                         }
357 
358                         memcpy(&buf[i*sizeof(entry)], &entry, sizeof(entry));
359                 }
360 
361                 *len = i * sizeof(entry);
362                 return 0;
363         }
364 
365         return -1;
366 }
367 
368 static int wext_get_freqlist(const char *ifname, char *buf, int *len)
369 {
370         struct iwreq wrq;
371         struct iw_range range;
372         struct iwinfo_freqlist_entry entry;
373         int i, bl;
374 
375         wrq.u.data.pointer = (caddr_t) &range;
376         wrq.u.data.length  = sizeof(struct iw_range);
377         wrq.u.data.flags   = 0;
378 
379         if(wext_ioctl(ifname, SIOCGIWRANGE, &wrq) >= 0)
380         {
381                 bl = 0;
382 
383                 for(i = 0; i < range.num_frequency; i++)
384                 {
385                         entry.mhz        = wext_freq2mhz(&range.freq[i]);
386                         entry.channel    = range.freq[i].i;
387                         entry.restricted = 0;
388 
389                         memcpy(&buf[bl], &entry, sizeof(struct iwinfo_freqlist_entry));
390                         bl += sizeof(struct iwinfo_freqlist_entry);
391                 }
392 
393                 *len = bl;
394                 return 0;
395         }
396 
397         return -1;
398 }
399 
400 static int wext_get_country(const char *ifname, char *buf)
401 {
402         sprintf(buf, "00");
403         return 0;
404 }
405 
406 static int wext_get_countrylist(const char *ifname, char *buf, int *len)
407 {
408         /* Stub */
409         return -1;
410 }
411 
412 static int wext_get_hwmodelist(const char *ifname, int *buf)
413 {
414         char chans[IWINFO_BUFSIZE] = { 0 };
415         struct iwinfo_freqlist_entry *e = NULL;
416         int len = 0;
417 
418         *buf = 0;
419 
420         if( !wext_get_freqlist(ifname, chans, &len) )
421         {
422                 for( e = (struct iwinfo_freqlist_entry *)chans; e->channel; e++ )
423                 {
424                         if( e->channel <= 14 )
425                         {
426                                 *buf |= IWINFO_80211_B;
427                                 *buf |= IWINFO_80211_G;
428                         }
429                         else
430                         {
431                                 *buf |= IWINFO_80211_A;
432                         }
433                 }
434 
435                 return 0;
436         }
437 
438         return -1;
439 }
440 
441 static int wext_get_htmodelist(const char *ifname, int *buf)
442 {
443         /* Stub */
444         return -1;
445 }
446 
447 static int wext_get_encryption(const char *ifname, char *buf)
448 {
449         /* No reliable crypto info in wext */
450         return -1;
451 }
452 
453 static int wext_get_phyname(const char *ifname, char *buf)
454 {
455         /* No suitable api in wext */
456         strcpy(buf, ifname);
457         return 0;
458 }
459 
460 static int wext_get_mbssid_support(const char *ifname, int *buf)
461 {
462         /* No multi bssid support atm */
463         return -1;
464 }
465 
466 static char * wext_sysfs_ifname_file(const char *ifname, const char *path)
467 {
468         FILE *f;
469         static char buf[128];
470         char *rv = NULL;
471 
472         snprintf(buf, sizeof(buf), "/sys/class/net/%s/%s", ifname, path);
473 
474         if ((f = fopen(buf, "r")) != NULL)
475         {
476                 memset(buf, 0, sizeof(buf));
477 
478                 if (fread(buf, 1, sizeof(buf), f))
479                         rv = buf;
480 
481                 fclose(f);
482         }
483 
484         return rv;
485 }
486 
487 static int wext_get_hardware_id(const char *ifname, char *buf)
488 {
489         char *data;
490         struct iwinfo_hardware_id *id = (struct iwinfo_hardware_id *)buf;
491 
492         memset(id, 0, sizeof(struct iwinfo_hardware_id));
493 
494         data = wext_sysfs_ifname_file(ifname, "device/vendor");
495         if (data)
496                 id->vendor_id = strtoul(data, NULL, 16);
497 
498         data = wext_sysfs_ifname_file(ifname, "device/device");
499         if (data)
500                 id->device_id = strtoul(data, NULL, 16);
501 
502         data = wext_sysfs_ifname_file(ifname, "device/subsystem_device");
503         if (data)
504                 id->subsystem_device_id = strtoul(data, NULL, 16);
505 
506         data = wext_sysfs_ifname_file(ifname, "device/subsystem_vendor");
507         if (data)
508                 id->subsystem_vendor_id = strtoul(data, NULL, 16);
509 
510         return (id->vendor_id > 0 && id->device_id > 0) ? 0 : -1;
511 }
512 
513 static int wext_get_hardware_name(const char *ifname, char *buf)
514 {
515         sprintf(buf, "Generic WEXT");
516         return 0;
517 }
518 
519 static int wext_get_txpower_offset(const char *ifname, int *buf)
520 {
521         /* Stub */
522         *buf = 0;
523         return -1;
524 }
525 
526 static int wext_get_frequency_offset(const char *ifname, int *buf)
527 {
528         /* Stub */
529         *buf = 0;
530         return -1;
531 }
532 
533 const struct iwinfo_ops wext_ops = {
534         .name             = "wext",
535         .probe            = wext_probe,
536         .channel          = wext_get_channel,
537         .frequency        = wext_get_frequency,
538         .frequency_offset = wext_get_frequency_offset,
539         .txpower          = wext_get_txpower,
540         .txpower_offset   = wext_get_txpower_offset,
541         .bitrate          = wext_get_bitrate,
542         .signal           = wext_get_signal,
543         .noise            = wext_get_noise,
544         .quality          = wext_get_quality,
545         .quality_max      = wext_get_quality_max,
546         .mbssid_support   = wext_get_mbssid_support,
547         .hwmodelist       = wext_get_hwmodelist,
548         .htmodelist       = wext_get_htmodelist,
549         .mode             = wext_get_mode,
550         .ssid             = wext_get_ssid,
551         .bssid            = wext_get_bssid,
552         .country          = wext_get_country,
553         .hardware_id      = wext_get_hardware_id,
554         .hardware_name    = wext_get_hardware_name,
555         .encryption       = wext_get_encryption,
556         .phyname          = wext_get_phyname,
557         .assoclist        = wext_get_assoclist,
558         .txpwrlist        = wext_get_txpwrlist,
559         .scanlist         = wext_get_scanlist,
560         .freqlist         = wext_get_freqlist,
561         .countrylist      = wext_get_countrylist,
562         .close            = wext_close
563 };
564 

This page was automatically generated by LXR 0.3.1.  •  OpenWrt