707116928c2cac8d93756fdf03c2104156fd3ecd
2 * Licensed under the Apache License, Version 2.0 (the "License");
3 * you may not use this file except in compliance with the License.
4 * You may obtain a copy of the License at
6 * http://www.apache.org/licenses/LICENSE-2.0
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and
12 * limitations under the License.
14 * Copyright (C) 2008 John Crispin <blogic@openwrt.org>
15 * Copyright (C) 2008 Steven Barth <steven@midlink.org>
16 * Copyright (C) 2009 Jo-Philipp Wich <xm@subsignal.org>
20 #include <net/if_arp.h>
21 #include <net/route.h>
25 #include <linux/sockios.h>
34 static int sock_iwconfig
= 0;
36 typedef struct iwscan_state
39 int ap_num
; /* Access Point number 1->N */
40 int val_index
; /* Value in table 0->(N-1) */
46 sock_iwconfig
= iw_sockets_open();
50 void iwc_shutdown(void)
54 iw_sockets_close(sock_iwconfig
);
58 /* taken from wireless tools */
60 get_info(char * ifname
, struct wireless_info
* info
)
64 memset((char*) info
, 0, sizeof(struct wireless_info
));
66 /* Get basic information */
67 if(iw_get_basic_config(sock_iwconfig
, ifname
, &(info
->b
)) < 0)
69 /* If no wireless name : no wireless extensions */
70 /* But let's check if the interface exists at all */
73 strncpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
74 if(ioctl(sock_iwconfig
, SIOCGIFFLAGS
, &ifr
) < 0)
81 if(iw_get_range_info(sock_iwconfig
, ifname
, &(info
->range
)) >= 0)
85 if(iw_get_ext(sock_iwconfig
, ifname
, SIOCGIWAP
, &wrq
) >= 0)
87 info
->has_ap_addr
= 1;
88 memcpy(&(info
->ap_addr
), &(wrq
.u
.ap_addr
), sizeof (sockaddr
));
92 if(iw_get_ext(sock_iwconfig
, ifname
, SIOCGIWRATE
, &wrq
) >= 0)
94 info
->has_bitrate
= 1;
95 memcpy(&(info
->bitrate
), &(wrq
.u
.bitrate
), sizeof(iwparam
));
98 /* Get Power Management settings */
99 wrq
.u
.power
.flags
= 0;
100 if(iw_get_ext(sock_iwconfig
, ifname
, SIOCGIWPOWER
, &wrq
) >= 0)
103 memcpy(&(info
->power
), &(wrq
.u
.power
), sizeof(iwparam
));
107 if(iw_get_stats(sock_iwconfig
, ifname
, &(info
->stats
),
108 &info
->range
, info
->has_range
) >= 0)
114 wrq
.u
.essid
.pointer
= (caddr_t
) info
->nickname
;
115 wrq
.u
.essid
.length
= IW_ESSID_MAX_SIZE
+ 1;
116 wrq
.u
.essid
.flags
= 0;
117 if(iw_get_ext(sock_iwconfig
, ifname
, SIOCGIWNICKN
, &wrq
) >= 0)
118 if(wrq
.u
.data
.length
> 1)
119 info
->has_nickname
= 1;
121 if((info
->has_range
) && (info
->range
.we_version_compiled
> 9))
123 /* Get Transmit Power */
124 if(iw_get_ext(sock_iwconfig
, ifname
, SIOCGIWTXPOW
, &wrq
) >= 0)
126 info
->has_txpower
= 1;
127 memcpy(&(info
->txpower
), &(wrq
.u
.txpower
), sizeof(iwparam
));
131 /* Get sensitivity */
132 if(iw_get_ext(sock_iwconfig
, ifname
, SIOCGIWSENS
, &wrq
) >= 0)
135 memcpy(&(info
->sens
), &(wrq
.u
.sens
), sizeof(iwparam
));
138 if((info
->has_range
) && (info
->range
.we_version_compiled
> 10))
140 /* Get retry limit/lifetime */
141 if(iw_get_ext(sock_iwconfig
, ifname
, SIOCGIWRETRY
, &wrq
) >= 0)
144 memcpy(&(info
->retry
), &(wrq
.u
.retry
), sizeof(iwparam
));
148 /* Get RTS threshold */
149 if(iw_get_ext(sock_iwconfig
, ifname
, SIOCGIWRTS
, &wrq
) >= 0)
152 memcpy(&(info
->rts
), &(wrq
.u
.rts
), sizeof(iwparam
));
155 /* Get fragmentation threshold */
156 if(iw_get_ext(sock_iwconfig
, ifname
, SIOCGIWFRAG
, &wrq
) >= 0)
159 memcpy(&(info
->frag
), &(wrq
.u
.frag
), sizeof(iwparam
));
165 void iwc_get(lua_State
*L
, char *ifname
)
167 struct wireless_info info
;
168 int rc
= get_info(ifname
, &info
);
173 lua_pushstring(L
, ifname
);
179 add_table_entry(L
, "essid", info
.b
.essid
);
181 add_table_entry(L
, "essid", "off");
185 add_table_entry(L
, "mode", iw_operation_mode
[info
.b
.mode
]);
189 double freq
= info
.b
.freq
; /* Frequency/channel */
190 int channel
= -1; /* Converted to channel */
192 if(info
.has_range
&& (freq
< KILO
))
193 channel
= iw_channel_to_freq((int) freq
, &freq
, &info
.range
);
194 iw_print_freq(buffer
, sizeof(buffer
), freq
, -1, info
.b
.freq_flags
);
195 snprintf(tmp
, 4, "%d", channel
);
196 add_table_entry(L
, "channel", tmp
);
197 add_table_entry(L
, "freq", buffer
);
201 add_table_entry(L
, "macap", iw_sawap_ntop(&info
.ap_addr
, buffer
));
205 iw_print_bitrate(buffer
, sizeof(buffer
), info
.bitrate
.value
);
206 add_table_entry(L
, "bitrate", buffer
);
211 iw_print_txpower(buffer
, sizeof(buffer
), &info
.txpower
);
212 add_table_entry(L
, "txpower", buffer
);
217 int iwc_getall(lua_State
*L
)
222 fp
= fopen("/proc/net/wireless", "r");
225 fgets(buffer
, 128, fp
);
226 fgets(buffer
, 128, fp
);
228 while(fgets(buffer
, 128, fp
))
242 /* taken from wireless tools */
243 int iwc_set_essid(lua_State
*L
)
247 char essid
[IW_ESSID_MAX_SIZE
+ 1];
248 int we_kernel_version
;
250 if(lua_gettop(L
) != 2)
252 lua_pushstring(L
, "invalid arg list");
256 ifname
= (char *)lua_tostring (L
, 1);
257 e
= (char *)lua_tostring (L
, 2);
259 if((!strcasecmp(e
, "off")) | (!strcasecmp(e
, "any")))
261 wrq
.u
.essid
.flags
= 0;
263 } else if(!strcasecmp(e
, "on"))
266 memset(essid
, '\0', sizeof(essid
));
267 wrq
.u
.essid
.pointer
= (caddr_t
) essid
;
268 wrq
.u
.essid
.length
= IW_ESSID_MAX_SIZE
+ 1;
269 wrq
.u
.essid
.flags
= 0;
270 if(iw_get_ext(sock_iwconfig
, ifname
, SIOCGIWESSID
, &wrq
) < 0)
272 wrq
.u
.essid
.flags
= 1;
274 wrq
.u
.essid
.flags
= 1;
275 strcpy(essid
, e
); /* Size checked, all clear */
279 /* Get version from kernel, device may not have range... */
280 we_kernel_version
= iw_get_kernel_we_version();
282 /* Finally set the ESSID value */
283 wrq
.u
.essid
.pointer
= (caddr_t
) essid
;
284 wrq
.u
.essid
.length
= strlen(essid
);
285 if(we_kernel_version
< 21)
286 wrq
.u
.essid
.length
++;
288 if(!iw_set_ext(sock_iwconfig
, ifname
, SIOCSIWESSID
, &wrq
))
289 lua_pushboolean(L
, 1);
291 lua_pushboolean(L
, 0);
295 /* taken from wireless tools */
296 int iwc_set_mode(lua_State
*L
)
299 unsigned int k
; /* Must be unsigned */
302 if(lua_gettop(L
) != 2)
304 lua_pushstring(L
, "invalid arg list");
308 ifname
= (char *)lua_tostring (L
, 1);
309 mode
= (char *)lua_tostring (L
, 2);
311 /* Check if it is a uint, otherwise get is as a string */
312 if(sscanf(mode
, "%ui", &k
) != 1)
315 while((k
< IW_NUM_OPER_MODE
) && strncasecmp(mode
, iw_operation_mode
[k
], 3))
318 if(k
>= IW_NUM_OPER_MODE
)
322 if(!iw_set_ext(sock_iwconfig
, ifname
, SIOCSIWMODE
, &wrq
))
323 lua_pushboolean(L
, 1);
325 lua_pushboolean(L
, 0);
329 int iwc_set_channel(lua_State
*L
)
334 if(lua_gettop(L
) != 2)
336 lua_pushstring(L
, "invalid arg list");
340 ifname
= (char *)lua_tostring (L
, 1);
341 channel
= (int)lua_tointeger(L
, 2);
347 wrq
.u
.freq
.flags
= 0;
349 iw_float2freq(channel
, &wrq
.u
.freq
);
350 wrq
.u
.freq
.flags
= IW_FREQ_FIXED
;
352 if(!iw_set_ext(sock_iwconfig
, ifname
, SIOCSIWFREQ
, &wrq
))
353 lua_pushboolean(L
, 1);
355 lua_pushboolean(L
, 0);
359 static const char * iw_ie_cypher_name
[] = {
367 #define IW_ARRAY_LEN(x) (sizeof(x)/sizeof((x)[0]))
368 #define IW_IE_CYPHER_NUM IW_ARRAY_LEN(iw_ie_cypher_name)
370 static const char * iw_ie_key_mgmt_name
[] = {
375 #define IW_IE_KEY_MGMT_NUM IW_ARRAY_LEN(iw_ie_key_mgmt_name)
377 static inline void iw_print_ie_wpa(lua_State
*L
, unsigned char * iebuf
, int buflen
)
379 int ielen
= iebuf
[1] + 2;
380 int offset
= 2; /* Skip the IE id, and the length. */
381 unsigned char wpa1_oui
[3] = {0x00, 0x50, 0xf2};
382 unsigned char wpa2_oui
[3] = {0x00, 0x0f, 0xac};
383 unsigned char *wpa_oui
;
387 int wpa1
= 0, wpa2
= 0;
394 case 0x30: /* WPA2 */
395 /* Check if we have enough data */
401 case 0xdd: /* WPA or else */
403 /* Not all IEs that start with 0xdd are WPA.
404 * * So check that the OUI is valid. */
406 || ((memcmp(&iebuf
[offset
], wpa_oui
, 3) != 0)
407 && (iebuf
[offset
+3] == 0x01)))
419 /* Pick version number (little endian) */
420 ver
= iebuf
[offset
] | (iebuf
[offset
+ 1] << 8);
428 if(ielen
< (offset
+ 4))
432 add_table_entry(L
, "wpa1gcipher", "TKIP");
433 add_table_entry(L
, "wpa1pcipher", "TKIP");
435 add_table_entry(L
, "wpa2gcipher", "TKIP");
436 add_table_entry(L
, "wpa2pcipher", "TKIP");
441 if(memcmp(&iebuf
[offset
], wpa_oui
, 3) != 0)
444 add_table_entry(L
, "wpa1gcipher", "Proprietary");
446 add_table_entry(L
, "wpa2gcipher", "Proprietary");
449 add_table_entry(L
, "wpa1gcipher", iebuf
[offset
+3][iw_ie_cypher_name
]);
451 add_table_entry(L
, "wpa2gcipher", iebuf
[offset
+3][iw_ie_cypher_name
]);
455 if(ielen
< (offset
+ 2))
458 add_table_entry(L
, "wpa1pcipher", "TKIP");
460 add_table_entry(L
, "wpa2pcipher", "TKIP");
463 /* Otherwise, we have some number of pairwise ciphers. */
464 cnt
= iebuf
[offset
] | (iebuf
[offset
+ 1] << 8);
466 if(ielen
< (offset
+ 4*cnt
))
469 for(i
= 0; i
< cnt
; i
++)
472 strncat(buf
, " ", 256);
473 if(memcmp(&iebuf
[offset
], wpa_oui
, 3) != 0)
475 strncat(buf
, "Proprietary", 256);
477 if(iebuf
[offset
+3] <= IW_IE_CYPHER_NUM
)
478 strncat(buf
, iw_ie_cypher_name
[iebuf
[offset
+3]], 256);
480 strncat(buf
, "unknown", 256);
485 add_table_entry(L
, "wpa1pcipher", buf
);
487 add_table_entry(L
, "wpa2pcipher", buf
);
489 /* Check if we are done */
490 if(ielen
< (offset
+ 2))
493 /* Now, we have authentication suites. */
494 cnt
= iebuf
[offset
] | (iebuf
[offset
+ 1] << 8);
497 if(ielen
< (offset
+ 4*cnt
))
500 for(i
= 0; i
< cnt
; i
++)
503 strncat(buf
, " ", 256);
504 if(memcmp(&iebuf
[offset
], wpa_oui
, 3) != 0)
506 strncat(buf
, "Proprietary", 256);
508 if(iebuf
[offset
+3] <= IW_IE_KEY_MGMT_NUM
)
509 strncat(buf
, iw_ie_key_mgmt_name
[iebuf
[offset
+3]], 256);
511 strncat(buf
, "unknown", 256);
516 add_table_entry(L
, "wpa1auth", buf
);
518 add_table_entry(L
, "wpa2auth", buf
);
519 /* Check if we are done */
520 if(ielen
< (offset
+ 1))
524 static inline void print_scanning_token(lua_State
*L
, struct stream_descr
*stream
,
525 struct iw_event
*event
, struct iwscan_state
*state
, struct iw_range
*iw_range
, int has_range
)
527 char buffer
[128]; /* Temporary buffer */
529 /* Now, let's decode the event */
533 add_table_entry(L
, "addr", iw_saether_ntop(&event
->u
.ap_addr
, buffer
));
538 double freq
; /* Frequency/channel */
539 int channel
= -1; /* Converted to channel */
540 freq
= iw_freq2float(&(event
->u
.freq
));
541 /* Convert to channel if possible */
543 channel
= iw_freq_to_channel(freq
, iw_range
);
544 snprintf(buffer
, 128, "%1.3f", freq
);
545 add_table_entry(L
, "frequency", buffer
);
546 snprintf(buffer
, 128, "%d", channel
);
547 add_table_entry(L
, "channel", buffer
);
548 //iw_print_freq(buffer, sizeof(buffer), freq, channel, event->u.freq.flags);
549 //printf(" %s\n", buffer);
553 /* Note : event->u.mode is unsigned, no need to check <= 0 */
554 if(event
->u
.mode
>= IW_NUM_OPER_MODE
)
555 event
->u
.mode
= IW_NUM_OPER_MODE
;
556 add_table_entry(L
, "mode", iw_operation_mode
[event
->u
.mode
]);
560 char essid
[IW_ESSID_MAX_SIZE
+1];
561 memset(essid
, '\0', sizeof(essid
));
562 if((event
->u
.essid
.pointer
) && (event
->u
.essid
.length
))
563 memcpy(essid
, event
->u
.essid
.pointer
, event
->u
.essid
.length
);
564 if(event
->u
.essid
.flags
)
565 add_table_entry(L
, "essid", essid
);
567 add_table_entry(L
, "essid", "off/any/hidden");
572 unsigned char key
[IW_ENCODING_TOKEN_MAX
];
573 if(event
->u
.data
.pointer
)
574 memcpy(key
, event
->u
.data
.pointer
, event
->u
.data
.length
);
576 event
->u
.data
.flags
|= IW_ENCODE_NOKEY
;
577 if(event
->u
.data
.flags
& IW_ENCODE_DISABLED
)
579 add_table_entry(L
, "key", "off");
581 iw_print_key(buffer
, sizeof(buffer
), key
, event
->u
.data
.length
,
582 event
->u
.data
.flags
);
583 add_table_entry(L
, "key", buffer
);
588 if(state
->val_index
== 0)
590 lua_pushstring(L
, "bitrates");
593 //iw_print_bitrate(buffer, sizeof(buffer), event->u.bitrate.value);
594 snprintf(buffer
, sizeof(buffer
), "%d", event
->u
.bitrate
.value
);
595 lua_pushinteger(L
, state
->val_index
+ 1);
596 lua_pushstring(L
, buffer
);
599 /* Check for termination */
600 if(stream
->value
== NULL
)
603 state
->val_index
= 0;
610 unsigned char *buffer
= event
->u
.data
.pointer
;
611 int buflen
= event
->u
.data
.length
;
612 while(offset
<= (buflen
- 2))
614 switch(buffer
[offset
])
616 case 0xdd: /* WPA1 (and other) */
617 case 0x30: /* WPA2 */
618 iw_print_ie_wpa(L
, buffer
+ offset
, buflen
);
623 offset
+= buffer
[offset
+1] + 2;
629 } /* switch(event->cmd) */
632 int iwc_scan(lua_State
*L
)
635 struct iw_scan_req scanopt
; /* Options for 'set' */
636 int scanflags
= 0; /* Flags for scan */
637 unsigned char *buffer
= NULL
; /* Results */
638 int buflen
= IW_SCAN_MAX_DATA
; /* Min for compat WE<17 */
639 struct iw_range range
;
641 struct timeval tv
; /* Select timeout */
642 int timeout
= 15000000; /* 15s */
644 if(lua_gettop(L
) != 1)
646 lua_pushstring(L
, "invalid arg list");
650 ifname
= (char *)lua_tostring (L
, 1);
652 /* Debugging stuff */
653 if((IW_EV_LCP_PK2_LEN
!= IW_EV_LCP_PK_LEN
) || (IW_EV_POINT_PK2_LEN
!= IW_EV_POINT_PK_LEN
))
655 fprintf(stderr
, "*** Please report to jt@hpl.hp.com your platform details\n");
656 fprintf(stderr
, "*** and the following line :\n");
657 fprintf(stderr
, "*** IW_EV_LCP_PK2_LEN = %zu ; IW_EV_POINT_PK2_LEN = %zu\n\n",
658 IW_EV_LCP_PK2_LEN
, IW_EV_POINT_PK2_LEN
);
661 /* Get range stuff */
662 has_range
= (iw_get_range_info(sock_iwconfig
, ifname
, &range
) >= 0);
664 /* Check if the interface could support scanning. */
665 if((!has_range
) || (range
.we_version_compiled
< 14))
667 lua_pushstring(L
, "interface does not support scanning");
672 /* Init timeout value -> 250ms between set and first get */
676 /* Clean up set args */
677 memset(&scanopt
, 0, sizeof(scanopt
));
679 wrq
.u
.data
.pointer
= NULL
;
680 wrq
.u
.data
.flags
= 0;
681 wrq
.u
.data
.length
= 0;
683 /* Initiate Scanning */
684 if(iw_set_ext(sock_iwconfig
, ifname
, SIOCSIWSCAN
, &wrq
) < 0)
686 if((errno
!= EPERM
) || (scanflags
!= 0))
688 lua_pushstring(L
, "interface does not support scanning");
692 /* If we don't have the permission to initiate the scan, we may
693 * * still have permission to read left-over results.
694 * * But, don't wait !!! */
696 /* Not cool, it display for non wireless interfaces... */
697 fprintf(stderr
, "%-8.16s (Could not trigger scanning, just reading left-over results)\n", ifname
);
701 timeout
-= tv
.tv_usec
;
706 fd_set rfds
; /* File descriptors for select */
707 int last_fd
; /* Last fd */
710 /* Guess what ? We must re-generate rfds each time */
713 /* In here, add the rtnetlink fd in the list */
715 /* Wait until something happens */
716 ret
= select(last_fd
+ 1, &rfds
, NULL
, NULL
, &tv
);
718 /* Check if there was an error */
721 if(errno
== EAGAIN
|| errno
== EINTR
)
723 lua_pushstring(L
, "unhandled signal");
728 /* Check if there was a timeout */
731 unsigned char * newbuf
;
734 /* (Re)allocate the buffer - realloc(NULL, len) == malloc(len) */
735 newbuf
= realloc(buffer
, buflen
);
740 fprintf(stderr
, "%s: Allocation failed\n", __FUNCTION__
);
745 /* Try to read the results */
746 wrq
.u
.data
.pointer
= buffer
;
747 wrq
.u
.data
.flags
= 0;
748 wrq
.u
.data
.length
= buflen
;
749 if(iw_get_ext(sock_iwconfig
, ifname
, SIOCGIWSCAN
, &wrq
) < 0)
751 /* Check if buffer was too small (WE-17 only) */
752 if((errno
== E2BIG
) && (range
.we_version_compiled
> 16))
754 /* Some driver may return very large scan results, either
755 * because there are many cells, or because they have many
756 * large elements in cells (like IWEVCUSTOM). Most will
757 * only need the regular sized buffer. We now use a dynamic
758 * allocation of the buffer to satisfy everybody. Of course,
759 * as we don't know in advance the size of the array, we try
760 * various increasing sizes. Jean II */
762 /* Check if the driver gave us any hints. */
763 if(wrq
.u
.data
.length
> buflen
)
764 buflen
= wrq
.u
.data
.length
;
772 /* Check if results not available yet */
775 /* Restart timer for only 100ms*/
778 timeout
-= tv
.tv_usec
;
780 continue; /* Try again later */
785 fprintf(stderr
, "%-8.16s Failed to read scan data : %s\n\n",
786 ifname
, strerror(errno
));
789 /* We have the results, go to process them */
793 /* In here, check if event and event type
794 * * if scan event, read results. All errors bad & no reset timeout */
797 if(wrq
.u
.data
.length
)
800 struct stream_descr stream
;
801 struct iwscan_state state
= { .ap_num
= 1, .val_index
= 0 };
804 iw_init_event_stream(&stream
, (char *) buffer
, wrq
.u
.data
.length
);
808 /* Extract an event and print it */
809 ret
= iw_extract_event_stream(&stream
, &iwe
,
810 range
.we_version_compiled
);
813 if(iwe
.cmd
== SIOCGIWAP
)
818 lua_pushinteger(L
, state
.ap_num
);
821 print_scanning_token(L
, &stream
, &iwe
, &state
, &range
, has_range
);
832 int iwc_frequencies(lua_State
*L
)
837 struct iw_range range
;
839 if(lua_gettop(L
) != 1)
841 lua_pushstring(L
, "invalid arg list");
846 ifname
= (char *)lua_tostring (L
, 1);
848 /* Get range stuff */
849 has_range
= (iw_get_range_info(sock_iwconfig
, ifname
, &range
) >= 0);
851 /* Check if the interface could support scanning. */
852 if((!has_range
) || (range
.we_version_compiled
< 14))
854 lua_pushstring(L
, "interface does not support frequency enumeration");
861 for(i
= 0; i
< range
.num_frequency
; i
++)
863 lua_pushnumber(L
, i
+ 1);
866 lua_pushinteger(L
, 1);
867 lua_pushinteger(L
, (int)range
.freq
[i
].i
);
870 lua_pushinteger(L
, 2);
871 lua_pushnumber(L
, iw_freq2float(&(range
.freq
[i
])));