1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592
|
/*
* OS dependent APIs for Linux
*
* Copyright (C) 2006-2018 Thomas d'Otreppe <tdotreppe@aircrack-ng.org>
* Copyright (C) 2004, 2005 Christophe Devine
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <netpacket/packet.h>
#include <linux/if_ether.h>
#include <linux/if.h>
#include <linux/wireless.h>
#include <netinet/in.h>
#include <linux/if_tun.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <dirent.h>
#include <sys/utsname.h>
#include <net/if_arp.h>
#include <limits.h>
#ifdef CONFIG_LIBNL
#include <linux/nl80211.h>
#include <netlink/genl/genl.h>
#include <netlink/genl/family.h>
#include <netlink/genl/ctrl.h>
#include <netlink/msg.h>
#include <netlink/attr.h>
#include <linux/genetlink.h>
#endif // CONFIG_LIBNL
#include "radiotap/radiotap.h"
#include "radiotap/radiotap_iter.h"
/* radiotap-parser defines types like u8 that
* ieee80211_radiotap.h needs
*
* we use our local copy of ieee80211_radiotap.h
*
* - since we can't support extensions we don't understand
* - since linux does not include it in userspace headers
*/
#include "osdep.h"
#include "aircrack-ng/support/pcap_local.h"
#include "crctable_osdep.h"
#include "common.h"
#include "channel.h"
#include "aircrack-ng/defs.h"
#ifdef CONFIG_LIBNL
struct nl80211_state state;
static int chan;
#endif // CONFIG_LIBNL
/* if_nametoindex is defined in net/if.h but that conflicts with linux/if.h */
extern unsigned int if_nametoindex(const char * __ifname);
extern char * if_indextoname(unsigned int __ifindex, char * __ifname);
typedef enum {
DT_NULL = 0,
DT_WLANNG,
DT_HOSTAP,
DT_MADWIFI,
DT_MADWIFING,
DT_BCM43XX,
DT_ORINOCO,
DT_ZD1211RW,
DT_ACX,
DT_MAC80211_RT,
DT_AT76USB,
DT_IPW2200
} DRIVER_TYPE;
/*
* XXX need to have a different read/write/open function for each Linux driver.
*/
struct priv_linux
{
int fd_in, arptype_in;
int fd_out, arptype_out;
int fd_main;
int fd_rtc;
DRIVER_TYPE drivertype; /* inited to DT_UNKNOWN on allocation by wi_alloc */
FILE * f_cap_in;
struct pcap_file_header pfh_in;
int sysfs_inject;
int channel;
int freq;
int rate;
int tx_power;
char * wlanctlng; /* XXX never set */
char * iwpriv;
char * iwconfig;
char * ifconfig;
char * wl;
char * main_if;
unsigned char pl_mac[6];
int inject_wlanng;
};
#ifndef ETH_P_80211_RAW
#define ETH_P_80211_RAW 25
#endif
#define ARPHRD_ETHERNET 1
#define ARPHRD_IEEE80211 801
#define ARPHRD_IEEE80211_PRISM 802
#define ARPHRD_IEEE80211_FULL 803
#ifndef NULL_MAC
#define NULL_MAC "\x00\x00\x00\x00\x00\x00"
#endif
static unsigned long calc_crc_osdep(unsigned char * buf, int len)
{
unsigned long crc = 0xFFFFFFFF;
for (; len > 0; len--, buf++)
crc = crc_tbl_osdep[(crc ^ *buf) & 0xFF] ^ (crc >> 8);
return (~crc);
}
/* CRC checksum verification routine */
static int check_crc_buf_osdep(unsigned char * buf, int len)
{
unsigned long crc;
if (len < 0) return 0;
crc = calc_crc_osdep(buf, len);
buf += len;
return (((crc) &0xFF) == buf[0] && ((crc >> 8) & 0xFF) == buf[1]
&& ((crc >> 16) & 0xFF) == buf[2]
&& ((crc >> 24) & 0xFF) == buf[3]);
}
// Check if the driver is ndiswrapper */
static int is_ndiswrapper(const char * iface, const char * path)
{
int n, pid;
if (!path || !iface || strlen(iface) >= IFNAMSIZ)
{
return 0;
}
if ((pid = fork()) == 0)
{
close(0);
close(1);
close(2);
IGNORE_NZ(chdir("/"));
execl(path, "iwpriv", iface, "ndis_reset", NULL);
exit(1);
}
waitpid(pid, &n, 0);
return ((WIFEXITED(n) && WEXITSTATUS(n) == 0));
}
/* Search a file recursively */
static char * searchInside(const char * dir, const char * filename)
{
char * ret;
char * curfile;
struct stat sb;
int len, lentot;
DIR * dp;
struct dirent * ep;
dp = opendir(dir);
if (dp == NULL)
{
return NULL;
}
len = strlen(filename);
lentot = strlen(dir) + 256 + 2;
curfile = (char *) calloc(1, lentot);
if (curfile == NULL)
{
(void) closedir(dp);
return (NULL);
}
while ((ep = readdir(dp)) != NULL)
{
memset(curfile, 0, lentot);
sprintf(curfile, "%s/%s", dir, ep->d_name);
// Checking if it's the good file
if ((int) strlen(ep->d_name) == len && !strcmp(ep->d_name, filename))
{
(void) closedir(dp);
return curfile;
}
// If it's a directory and not a link, try to go inside to search
if (lstat(curfile, &sb) == 0 && S_ISDIR(sb.st_mode)
&& !S_ISLNK(sb.st_mode))
{
// Check if the directory isn't "." or ".."
if (strcmp(".", ep->d_name) && strcmp("..", ep->d_name))
{
// Recursive call
ret = searchInside(curfile, filename);
if (ret != NULL)
{
(void) closedir(dp);
free(curfile);
return ret;
}
}
}
}
(void) closedir(dp);
free(curfile);
return NULL;
}
/* Search a wireless tool and return its path */
static char * wiToolsPath(const char * tool)
{
char * path /*, *found, *env */;
int i, nbelems;
static const char * paths[] = {"/sbin",
"/usr/sbin",
"/usr/local/sbin",
"/bin",
"/usr/bin",
"/usr/local/bin",
"/tmp"};
// Also search in other known location just in case we haven't found it yet
nbelems = sizeof(paths) / sizeof(char *);
for (i = 0; i < nbelems; i++)
{
path = searchInside(paths[i], tool);
if (path != NULL) return path;
}
return NULL;
}
/* nl80211 */
#ifdef CONFIG_LIBNL
struct nl80211_state
{
#if !defined(CONFIG_LIBNL30) && !defined(CONFIG_LIBNL20)
struct nl_handle * nl_sock;
#else
struct nl_sock * nl_sock;
#endif
struct nl_cache * nl_cache;
struct genl_family * nl80211;
};
#if !defined(CONFIG_LIBNL30) && !defined(CONFIG_LIBNL20)
static inline struct nl_handle * nl_socket_alloc(void)
{
return nl_handle_alloc();
}
static inline void nl_socket_free(struct nl_handle * h)
{
nl_handle_destroy(h);
}
static inline int __genl_ctrl_alloc_cache(struct nl_handle * h,
struct nl_cache ** cache)
{
struct nl_cache * tmp = genl_ctrl_alloc_cache(h);
if (!tmp) return -ENOMEM;
*cache = tmp;
return 0;
}
#define genl_ctrl_alloc_cache __genl_ctrl_alloc_cache
#endif
static int linux_nl80211_init(struct nl80211_state * state)
{
int err;
state->nl_sock = nl_socket_alloc();
if (!state->nl_sock)
{
fprintf(stderr, "Failed to allocate netlink socket.\n");
return -ENOMEM;
}
if (genl_connect(state->nl_sock))
{
fprintf(stderr, "Failed to connect to generic netlink.\n");
err = -ENOLINK;
goto out_handle_destroy;
}
if (genl_ctrl_alloc_cache(state->nl_sock, &state->nl_cache))
{
fprintf(stderr, "Failed to allocate generic netlink cache.\n");
err = -ENOMEM;
goto out_handle_destroy;
}
state->nl80211 = genl_ctrl_search_by_name(state->nl_cache, "nl80211");
if (!state->nl80211)
{
fprintf(stderr, "nl80211 not found.\n");
err = -ENOENT;
goto out_cache_free;
}
return 0;
out_cache_free:
nl_cache_free(state->nl_cache);
out_handle_destroy:
nl_socket_free(state->nl_sock);
return err;
}
static void nl80211_cleanup(struct nl80211_state * state)
{
genl_family_put(state->nl80211);
nl_cache_free(state->nl_cache);
nl_socket_free(state->nl_sock);
}
/* Callbacks */
/*
static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
void *arg)
{
if (nla) { }
printf("\n\n\nERROR");
int *ret = arg;
*ret = err->error;
return NL_STOP;
}
*/
/*
static void test_callback(struct nl_msg *msg, void *arg)
{
if (msg || arg) { }
}
*/
#endif /* End nl80211 */
static int linux_get_channel(struct wif * wi)
{
struct priv_linux * dev = wi_priv(wi);
struct iwreq wrq;
int fd, frequency;
int chan = 0;
memset(&wrq, 0, sizeof(struct iwreq));
if (dev->main_if)
strncpy(wrq.ifr_name, dev->main_if, IFNAMSIZ);
else
strncpy(wrq.ifr_name, wi_get_ifname(wi), IFNAMSIZ);
wrq.ifr_name[IFNAMSIZ - 1] = 0;
fd = dev->fd_in;
if (dev->drivertype == DT_IPW2200) fd = dev->fd_main;
if (ioctl(fd, SIOCGIWFREQ, &wrq) < 0) return (-1);
frequency = wrq.u.freq.m;
if (frequency > 100000000)
frequency /= 100000;
else if (frequency > 1000000)
frequency /= 1000;
if (frequency > 1000)
chan = getChannelFromFrequency(frequency);
else
chan = frequency;
return chan;
}
static int linux_get_freq(struct wif * wi)
{
struct priv_linux * dev = wi_priv(wi);
struct iwreq wrq;
int fd, frequency;
memset(&wrq, 0, sizeof(struct iwreq));
if (dev->main_if)
strncpy(wrq.ifr_name, dev->main_if, IFNAMSIZ);
else
strncpy(wrq.ifr_name, wi_get_ifname(wi), IFNAMSIZ);
wrq.ifr_name[IFNAMSIZ - 1] = 0;
fd = dev->fd_in;
if (dev->drivertype == DT_IPW2200) fd = dev->fd_main;
if (ioctl(fd, SIOCGIWFREQ, &wrq) < 0) return (-1);
frequency = wrq.u.freq.m;
if (frequency > 100000000)
frequency /= 100000;
else if (frequency > 1000000)
frequency /= 1000;
if (frequency < 500) // it's not a freq, but the actual channel
frequency = getFrequencyFromChannel(frequency);
return frequency;
}
static int linux_set_rate(struct wif * wi, int rate)
{
struct priv_linux * dev = wi_priv(wi);
struct ifreq ifr;
struct iwreq wrq;
char s[32];
int pid, status;
memset(s, 0, sizeof(s));
switch (dev->drivertype)
{
case DT_MADWIFING:
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, wi_get_ifname(wi), sizeof(ifr.ifr_name) - 1);
if (ioctl(dev->fd_in, SIOCGIFINDEX, &ifr) < 0)
{
printf("Interface %s: \n", wi_get_ifname(wi));
perror("ioctl(SIOCGIFINDEX) failed");
return (1);
}
/* Bring interface down*/
ifr.ifr_flags = 0;
if (ioctl(dev->fd_in, SIOCSIFFLAGS, &ifr) < 0)
{
perror("ioctl(SIOCSIFFLAGS) failed");
return (1);
}
usleep(100000);
snprintf(s, sizeof(s) - 1, "%.1fM", (rate / 1000000.0));
if ((pid = fork()) == 0)
{
close(0);
close(1);
close(2);
IGNORE_NZ(chdir("/"));
execlp(dev->iwconfig,
"iwconfig",
wi_get_ifname(wi),
"rate",
s,
NULL);
exit(1);
}
waitpid(pid, &status, 0);
return 0;
break;
case DT_MAC80211_RT:
dev->rate = (rate / 500000);
// return 0;
// Newer mac80211 stacks (2.6.31 and up)
// don't care about Radiotap header anymore, so ioctl below must
// also be done!
//[see Documentation/networking/mac80211-injection.txt]
break;
default:
break;
}
/* ELSE */
memset(&wrq, 0, sizeof(struct iwreq));
if (dev->main_if)
strncpy(wrq.ifr_name, dev->main_if, IFNAMSIZ);
else
strncpy(wrq.ifr_name, wi_get_ifname(wi), IFNAMSIZ);
wrq.ifr_name[IFNAMSIZ - 1] = 0;
wrq.u.bitrate.value = rate;
wrq.u.bitrate.fixed = 1;
if (ioctl(dev->fd_in, SIOCSIWRATE, &wrq) < 0)
{
return (-1);
}
return 0;
}
static int linux_get_rate(struct wif * wi)
{
struct priv_linux * dev = wi_priv(wi);
struct iwreq wrq;
memset(&wrq, 0, sizeof(struct iwreq));
if (dev->drivertype == DT_MAC80211_RT) return (dev->rate * 500000);
if (dev->main_if)
strncpy(wrq.ifr_name, dev->main_if, IFNAMSIZ);
else
strncpy(wrq.ifr_name, wi_get_ifname(wi), IFNAMSIZ);
wrq.ifr_name[IFNAMSIZ - 1] = 0;
if (ioctl(dev->fd_in, SIOCGIWRATE, &wrq) < 0)
{
return (-1);
}
return wrq.u.bitrate.value;
}
static int linux_set_mtu(struct wif * wi, int mtu)
{
struct priv_linux * dev = wi_priv(wi);
struct ifreq ifr;
memset(&ifr, 0, sizeof(struct ifreq));
if (dev->main_if)
strncpy(ifr.ifr_name, dev->main_if, sizeof(ifr.ifr_name) - 1);
else
strncpy(ifr.ifr_name, wi_get_ifname(wi), sizeof(ifr.ifr_name) - 1);
ifr.ifr_mtu = mtu;
if (ioctl(dev->fd_in, SIOCSIFMTU, &ifr) < 0)
{
return (-1);
}
return 0;
}
static int linux_get_mtu(struct wif * wi)
{
struct priv_linux * dev = wi_priv(wi);
struct ifreq ifr;
memset(&ifr, 0, sizeof(struct ifreq));
if (dev->main_if)
strncpy(ifr.ifr_name, dev->main_if, sizeof(ifr.ifr_name) - 1);
else
strncpy(ifr.ifr_name, wi_get_ifname(wi), sizeof(ifr.ifr_name) - 1);
if (ioctl(dev->fd_in, SIOCGIFMTU, &ifr) < 0)
{
return (-1);
}
return ifr.ifr_mtu;
}
static int linux_read(struct wif * wi,
struct timespec * ts,
int * dlt,
unsigned char * buf,
int count,
struct rx_info * ri)
{
struct priv_linux * dev = wi_priv(wi);
unsigned char tmpbuf[4096] __attribute__((aligned(8)));
int caplen, n, got_signal, got_noise, got_channel, fcs_removed;
n = got_signal = got_noise = got_channel = fcs_removed = 0;
if ((unsigned) count > sizeof(tmpbuf)) return (-1);
caplen = read(dev->fd_in, tmpbuf, count);
if (caplen < 0 && errno == EAGAIN)
return (-1);
else if (caplen < 0)
{
perror("read failed");
return (-1);
}
switch (dev->drivertype)
{
case DT_MADWIFI:
caplen -= 4; /* remove the FCS for madwifi-old! only (not -ng)*/
break;
default:
break;
}
if (dlt)
{
// TODO(jbenden): Future code could receive the actual linktype received.
*dlt = LINKTYPE_IEEE802_11;
}
if (ts)
{
clock_gettime(CLOCK_REALTIME, ts);
}
if (dev->arptype_in == ARPHRD_IEEE80211_PRISM)
{
/* skip the prism header */
if (tmpbuf[7] == 0x40)
{
/* prism54 uses a different format */
if (ri)
{
ri->ri_power = (int32_t) load32_le(tmpbuf + 0x33);
ri->ri_noise = (int32_t) load32_le(tmpbuf + 0x33 + 12);
ri->ri_rate = load32_le(tmpbuf + 0x33 + 24) * 500000;
got_signal = 1;
got_noise = 1;
}
n = 0x40;
}
else
{
if (ri)
{
ri->ri_mactime = load64_le(tmpbuf + 0x5C - 48);
ri->ri_channel = load32_le(tmpbuf + 0x5C - 36);
ri->ri_power = (int32_t) load32_le(tmpbuf + 0x5C);
ri->ri_noise = (int32_t) load32_le(tmpbuf + 0x5C + 12);
ri->ri_rate = load32_le(tmpbuf + 0x5C + 24) * 500000;
if (dev->drivertype == DT_MADWIFI
|| dev->drivertype == DT_MADWIFING)
ri->ri_power -= (int32_t) load32_le(tmpbuf + 0x68);
got_channel = 1;
got_signal = 1;
got_noise = 1;
}
n = load32_le(tmpbuf + 4);
}
if (n < 8 || n >= caplen) return (0);
}
if (dev->arptype_in == ARPHRD_IEEE80211_FULL)
{
struct ieee80211_radiotap_iterator iterator;
struct ieee80211_radiotap_header * rthdr;
rthdr = (struct ieee80211_radiotap_header *) tmpbuf; //-V1032
if (ieee80211_radiotap_iterator_init(&iterator, rthdr, caplen, NULL)
< 0)
return (0);
/* go through the radiotap arguments we have been given
* by the driver
*/
while (ri && (ieee80211_radiotap_iterator_next(&iterator) >= 0))
{
switch (iterator.this_arg_index)
{
case IEEE80211_RADIOTAP_TSFT:
ri->ri_mactime
= le64_to_cpu(*((uint64_t *) iterator.this_arg));
break;
case IEEE80211_RADIOTAP_DBM_ANTSIGNAL:
case IEEE80211_RADIOTAP_DB_ANTSIGNAL:
if (!got_signal)
{
if (*iterator.this_arg < 127)
ri->ri_power = *iterator.this_arg;
else
ri->ri_power = *iterator.this_arg - 255;
got_signal = 1;
}
break;
case IEEE80211_RADIOTAP_DBM_ANTNOISE:
case IEEE80211_RADIOTAP_DB_ANTNOISE:
if (!got_noise)
{
if (*iterator.this_arg < 127)
ri->ri_noise = *iterator.this_arg;
else
ri->ri_noise = *iterator.this_arg - 255;
got_noise = 1;
}
break;
case IEEE80211_RADIOTAP_ANTENNA:
ri->ri_antenna = *iterator.this_arg;
break;
case IEEE80211_RADIOTAP_CHANNEL:
ri->ri_channel = getChannelFromFrequency(
le16toh(*(uint16_t *) iterator.this_arg));
got_channel = 1;
break;
case IEEE80211_RADIOTAP_RATE:
ri->ri_rate = (*iterator.this_arg) * 500000;
break;
case IEEE80211_RADIOTAP_FLAGS:
/* is the CRC visible at the end?
* remove
*/
if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FCS)
{
fcs_removed = 1;
caplen -= 4;
}
if (*iterator.this_arg & IEEE80211_RADIOTAP_F_BADFCS)
return (0);
break;
}
}
n = le16_to_cpu(rthdr->it_len);
if (n <= 0 || n >= caplen) return (0);
}
caplen -= n;
// detect fcs at the end, even if the flag wasn't set and remove it
if (fcs_removed == 0 && check_crc_buf_osdep(tmpbuf + n, caplen - 4) == 1)
{
caplen -= 4;
}
memcpy(buf, tmpbuf + n, caplen);
if (ri && !got_channel) ri->ri_channel = wi_get_channel(wi);
return (caplen);
}
static int linux_write(struct wif * wi,
struct timespec * ts,
int dlt,
unsigned char * buf,
int count,
struct tx_info * ti)
{
struct priv_linux * dev = wi_priv(wi);
unsigned char maddr[6];
int ret, usedrtap = 0;
unsigned char tmpbuf[4096];
unsigned char rate;
unsigned short int * p_rtlen;
unsigned char u8aRadiotap[] __attribute__((aligned(8))) = {
0x00,
0x00, // <-- radiotap version
0x0c,
0x00, // <- radiotap header length
0x04,
0x80,
0x00,
0x00, // <-- bitmap
0x00, // <-- rate
0x00, // <-- padding for natural alignment
0x18,
0x00, // <-- TX flags
};
/* Pointer to the radiotap header length field for later use. */
p_rtlen = (unsigned short int *) (u8aRadiotap + 2); //-V1032
if ((unsigned) count > sizeof(tmpbuf) - 22) return -1;
/* XXX honor ti */
if (ti)
{
}
(void) ts;
(void) dlt;
rate = dev->rate;
u8aRadiotap[8] = rate;
switch (dev->drivertype)
{
case DT_MAC80211_RT:
memcpy(tmpbuf, u8aRadiotap, sizeof(u8aRadiotap));
memcpy(tmpbuf + sizeof(u8aRadiotap), buf, count);
count += sizeof(u8aRadiotap);
buf = tmpbuf;
usedrtap = 1;
break;
case DT_WLANNG:
/* Wlan-ng isn't able to inject on kernel > 2.6.11 */
if (dev->inject_wlanng == 0)
{
perror("write failed");
return (-1);
}
if (count >= 24)
{
/* for some reason, wlan-ng requires a special header */
if ((((unsigned char *) buf)[1] & 3) != 3)
{
memcpy(tmpbuf, buf, 24);
memset(tmpbuf + 24, 0, 22);
tmpbuf[30] = (count - 24) & 0xFF;
tmpbuf[31] = (count - 24) >> 8;
memcpy(tmpbuf + 46, buf + 24, count - 24);
count += 22;
}
else
{
memcpy(tmpbuf, buf, 30);
memset(tmpbuf + 30, 0, 16);
tmpbuf[30] = (count - 30) & 0xFF;
tmpbuf[31] = (count - 30) >> 8; //-V610
memcpy(tmpbuf + 46, buf + 30, count - 30);
count += 16;
}
buf = tmpbuf;
}
fallthrough;
case DT_HOSTAP:
if ((((unsigned char *) buf)[1] & 3) == 2)
{
/* Prism2 firmware swaps the dmac and smac in FromDS packets */
memcpy(maddr, buf + 4, 6);
memcpy(buf + 4, buf + 16, 6);
memcpy(buf + 16, maddr, 6);
}
break;
default:
break;
}
ret = write(dev->fd_out, buf, count);
if (ret < 0)
{
if (errno == EAGAIN || errno == EWOULDBLOCK || errno == ENOBUFS
|| errno == ENOMEM)
{
usleep(10000);
return (0);
}
perror("write failed");
return (-1);
}
/* radiotap header length is stored little endian on all systems */
if (usedrtap) ret -= letoh16(*p_rtlen);
return (ret);
}
#if defined(CONFIG_LIBNL)
static int ieee80211_channel_to_frequency(int chan)
{
if (chan < 14) return 2407 + chan * 5;
if (chan == 14) return 2484;
/* FIXME: dot11ChannelStartingFactor (802.11-2007 17.3.8.3.2) */
return (chan + 1000) * 5;
}
static int
linux_set_ht_channel_nl80211(struct wif * wi, int channel, unsigned int htval)
{
struct priv_linux * dev = wi_priv(wi);
char s[32];
int pid, status;
unsigned int devid;
struct nl_msg * msg;
unsigned int freq;
memset(s, 0, sizeof(s));
switch (dev->drivertype)
{
case DT_WLANNG:
snprintf(s, sizeof(s) - 1, "channel=%d", channel);
if ((pid = fork()) == 0)
{
close(0);
close(1);
close(2);
IGNORE_NZ(chdir("/"));
execl(dev->wlanctlng,
"wlanctl-ng",
wi_get_ifname(wi),
"lnxreq_wlansniff",
s,
NULL);
exit(1);
}
waitpid(pid, &status, 0);
if (WIFEXITED(status))
{
dev->channel = channel;
return (WEXITSTATUS(status));
}
else
return (1);
break;
case DT_ORINOCO:
snprintf(s, sizeof(s) - 1, "%d", channel);
if ((pid = fork()) == 0)
{
close(0);
close(1);
close(2);
IGNORE_NZ(chdir("/"));
execlp(dev->iwpriv,
"iwpriv",
wi_get_ifname(wi),
"monitor",
"1",
s,
NULL);
exit(1);
}
waitpid(pid, &status, 0);
dev->channel = channel;
return 0;
break; // yeah ;)
case DT_ZD1211RW:
snprintf(s, sizeof(s) - 1, "%d", channel);
if ((pid = fork()) == 0)
{
close(0);
close(1);
close(2);
IGNORE_NZ(chdir("/"));
execlp(dev->iwconfig,
"iwconfig",
wi_get_ifname(wi),
"channel",
s,
NULL);
exit(1);
}
waitpid(pid, &status, 0);
dev->channel = channel;
chan = channel;
return 0;
break; // yeah ;)
default:
break;
}
/* libnl stuff */
chan = channel;
devid = if_nametoindex(wi->wi_interface);
freq = ieee80211_channel_to_frequency(channel);
msg = nlmsg_alloc();
if (!msg)
{
fprintf(stderr, "failed to allocate netlink message\n");
return 2;
}
genlmsg_put(msg,
0,
0,
genl_family_get_id(state.nl80211),
0,
0,
NL80211_CMD_SET_WIPHY,
0);
NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, devid);
NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
unsigned ht = NL80211_CHAN_NO_HT;
switch (htval)
{
case CHANNEL_HT20:
ht = NL80211_CHAN_HT20;
break;
case CHANNEL_HT40_PLUS:
ht = NL80211_CHAN_HT40PLUS;
break;
case CHANNEL_HT40_MINUS:
ht = NL80211_CHAN_HT40MINUS;
break;
}
NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, ht);
nl_send_auto_complete(state.nl_sock, msg);
nlmsg_free(msg);
dev->channel = channel;
return (0);
nla_put_failure:
return -ENOBUFS;
}
static int linux_set_channel_nl80211(struct wif * wi, int channel)
{
return linux_set_ht_channel_nl80211(wi, channel, CHANNEL_NO_HT);
}
#else // CONFIG_LIBNL
static int linux_set_channel(struct wif * wi, int channel)
{
struct priv_linux * dev = wi_priv(wi);
char s[32];
int pid, status;
struct iwreq wrq;
memset(s, 0, sizeof(s));
switch (dev->drivertype)
{
case DT_WLANNG:
snprintf(s, sizeof(s) - 1, "channel=%d", channel);
if ((pid = fork()) == 0)
{
close(0);
close(1);
close(2);
IGNORE_NZ(chdir("/"));
execl(dev->wlanctlng,
"wlanctl-ng",
wi_get_ifname(wi),
"lnxreq_wlansniff",
s,
NULL);
exit(1);
}
waitpid(pid, &status, 0);
if (WIFEXITED(status))
{
dev->channel = channel;
return (WEXITSTATUS(status));
}
else
return (1);
break;
case DT_ORINOCO:
snprintf(s, sizeof(s) - 1, "%d", channel);
if ((pid = fork()) == 0)
{
close(0);
close(1);
close(2);
IGNORE_NZ(chdir("/"));
execlp(dev->iwpriv,
"iwpriv",
wi_get_ifname(wi),
"monitor",
"1",
s,
NULL);
exit(1);
}
waitpid(pid, &status, 0);
dev->channel = channel;
return 0;
break; // yeah ;)
case DT_ZD1211RW:
snprintf(s, sizeof(s) - 1, "%d", channel);
if ((pid = fork()) == 0)
{
close(0);
close(1);
close(2);
IGNORE_NZ(chdir("/"));
execlp(dev->iwconfig,
"iwconfig",
wi_get_ifname(wi),
"channel",
s,
NULL);
exit(1);
}
waitpid(pid, &status, 0);
dev->channel = channel;
return 0;
break; // yeah ;)
default:
break;
}
memset(&wrq, 0, sizeof(struct iwreq));
strncpy(wrq.ifr_name, wi_get_ifname(wi), IFNAMSIZ);
wrq.ifr_name[IFNAMSIZ - 1] = 0;
wrq.u.freq.m = (double) channel;
wrq.u.freq.e = (double) 0;
if (ioctl(dev->fd_in, SIOCSIWFREQ, &wrq) < 0)
{
usleep(10000); /* madwifi needs a second chance */
if (ioctl(dev->fd_in, SIOCSIWFREQ, &wrq) < 0)
{
/* perror( "ioctl(SIOCSIWFREQ) failed" ); */
return (1);
}
}
dev->channel = channel;
return (0);
}
#endif
static int linux_set_freq(struct wif * wi, int freq)
{
struct priv_linux * dev = wi_priv(wi);
char s[32];
int pid, status;
struct iwreq wrq;
memset(s, 0, sizeof(s));
switch (dev->drivertype)
{
case DT_WLANNG:
case DT_ORINOCO:
case DT_ZD1211RW:
snprintf(s, sizeof(s) - 1, "%dM", freq);
if ((pid = fork()) == 0)
{
close(0);
close(1);
close(2);
IGNORE_NZ(chdir("/"));
execlp(dev->iwconfig,
"iwconfig",
wi_get_ifname(wi),
"freq",
s,
NULL);
exit(1);
}
waitpid(pid, &status, 0);
dev->freq = freq;
return 0;
break; // yeah ;)
default:
break;
}
memset(&wrq, 0, sizeof(struct iwreq));
strncpy(wrq.ifr_name, wi_get_ifname(wi), IFNAMSIZ);
wrq.ifr_name[IFNAMSIZ - 1] = 0;
wrq.u.freq.m = (double) freq * 100000;
wrq.u.freq.e = (double) 1;
if (ioctl(dev->fd_in, SIOCSIWFREQ, &wrq) < 0)
{
usleep(10000); /* madwifi needs a second chance */
if (ioctl(dev->fd_in, SIOCSIWFREQ, &wrq) < 0)
{
/* perror( "ioctl(SIOCSIWFREQ) failed" ); */
return (1);
}
}
dev->freq = freq;
return (0);
}
static int opensysfs(struct priv_linux * dev, char * iface, int fd)
{
int fd2;
char buf[256];
if (iface == NULL || strlen(iface) >= IFNAMSIZ)
{
return 1;
}
/* ipw2200 injection */
snprintf(buf, 256, "/sys/class/net/%s/device/inject", iface);
fd2 = open(buf, O_WRONLY);
/* bcm43xx injection */
if (fd2 == -1)
{
snprintf(buf, 256, "/sys/class/net/%s/device/inject_nofcs", iface);
fd2 = open(buf, O_WRONLY);
}
if (fd2 == -1) return -1;
dup2(fd2, fd);
close(fd2);
dev->sysfs_inject = 1;
return 0;
}
static int linux_get_monitor(struct wif * wi)
{
struct priv_linux * dev = wi_priv(wi);
struct ifreq ifr;
struct iwreq wrq;
/* find the interface index */
if (dev->drivertype == DT_IPW2200) return (0);
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, wi_get_ifname(wi), sizeof(ifr.ifr_name) - 1);
// if( ioctl( fd, SIOCGIFINDEX, &ifr ) < 0 )
// {
// printf("Interface %s: \n", iface);
// perror( "ioctl(SIOCGIFINDEX) failed" );
// return( 1 );
// }
/* lookup the hardware type */
if (ioctl(wi_fd(wi), SIOCGIFHWADDR, &ifr) < 0)
{
printf("Interface %s: \n", wi_get_ifname(wi));
perror("ioctl(SIOCGIFHWADDR) failed");
return (1);
}
/* lookup iw mode */
memset(&wrq, 0, sizeof(struct iwreq));
strncpy(wrq.ifr_name, wi_get_ifname(wi), IFNAMSIZ);
wrq.ifr_name[IFNAMSIZ - 1] = 0;
if (ioctl(wi_fd(wi), SIOCGIWMODE, &wrq) < 0)
{
/* most probably not supported (ie for rtap ipw interface) *
* so just assume its correctly set... */
wrq.u.mode = IW_MODE_MONITOR;
}
if ((ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211
&& ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211_PRISM
&& ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211_FULL)
|| (wrq.u.mode != IW_MODE_MONITOR && (dev->drivertype != DT_ORINOCO)))
{
return (1);
}
return (0);
}
__attribute__((unused)) static char * get_linux_driver(const char * iface)
{
char path[PATH_MAX];
char link[PATH_MAX];
if (iface == NULL || strlen(iface) >= IFNAMSIZ)
{
return NULL;
}
// Read the link path
memset(path, 0, sizeof(path));
snprintf(path, sizeof(path), "/sys/class/net/%s/device/driver", iface);
// Read the link path
ssize_t len = readlink(path, link, sizeof(link));
if (len < 1 || len >= PATH_MAX)
{
return NULL;
}
memset(link + len, 0, sizeof(link) - len);
// Get driver name
const char * drv_idx = strrchr(link, '/');
if (drv_idx == NULL)
{
return NULL;
}
// Copy it to a new char * and return
ssize_t drv_len = len - (drv_idx - link);
if (drv_len <= 1)
{
return NULL;
}
char * ret = (char *) calloc(1, drv_len); // includes /
if (ret == NULL)
{
return NULL;
}
memcpy(ret, drv_idx + 1, drv_len - 1);
return ret;
}
static int set_monitor(struct priv_linux * dev, char * iface, int fd)
{
int pid, status;
struct iwreq wrq;
if (iface == NULL || strlen(iface) >= IFNAMSIZ)
{
return (1);
}
if (strcmp(iface, "prism0") == 0)
{
dev->wl = wiToolsPath("wl");
if ((pid = fork()) == 0)
{
close(0);
close(1);
close(2);
IGNORE_NZ(chdir("/"));
ALLEGE(dev->wl != NULL);
execl(dev->wl, "wl", "monitor", "1", NULL);
exit(1);
}
waitpid(pid, &status, 0);
if (WIFEXITED(status)) return (WEXITSTATUS(status));
return (1);
}
else if (strncmp(iface, "rtap", 4) == 0)
{
return 0;
}
else
{
switch (dev->drivertype)
{
case DT_WLANNG:
if ((pid = fork()) == 0)
{
close(0);
close(1);
close(2);
IGNORE_NZ(chdir("/"));
execl(dev->wlanctlng,
"wlanctl-ng",
iface,
"lnxreq_wlansniff",
"enable=true",
"prismheader=true",
"wlanheader=false",
"stripfcs=true",
"keepwepflags=true",
"6",
NULL);
exit(1);
}
waitpid(pid, &status, 0);
if (WIFEXITED(status)) return (WEXITSTATUS(status));
return (1);
break;
case DT_ORINOCO:
if ((pid = fork()) == 0)
{
close(0);
close(1);
close(2);
IGNORE_NZ(chdir("/"));
execlp(dev->iwpriv,
"iwpriv",
iface,
"monitor",
"1",
"1",
NULL);
exit(1);
}
waitpid(pid, &status, 0);
if (WIFEXITED(status)) return (WEXITSTATUS(status));
return 1;
break;
case DT_ACX:
if ((pid = fork()) == 0)
{
close(0);
close(1);
close(2);
IGNORE_NZ(chdir("/"));
execlp(dev->iwpriv,
"iwpriv",
iface,
"monitor",
"2",
"1",
NULL);
exit(1);
}
waitpid(pid, &status, 0);
if (WIFEXITED(status)) return (WEXITSTATUS(status));
return 1;
break;
default:
break;
}
memset(&wrq, 0, sizeof(struct iwreq));
strncpy(wrq.ifr_name, iface, IFNAMSIZ);
wrq.ifr_name[IFNAMSIZ - 1] = 0;
wrq.u.mode = IW_MODE_MONITOR;
if (ioctl(fd, SIOCSIWMODE, &wrq) < 0)
{
perror("ioctl(SIOCSIWMODE) failed");
return (1);
}
if (dev->drivertype == DT_AT76USB)
{
sleep(3);
}
}
/* couple of iwprivs to enable the prism header */
if (!fork()) /* hostap */
{
close(0);
close(1);
close(2);
IGNORE_NZ(chdir("/"));
execlp("iwpriv", "iwpriv", iface, "monitor_type", "1", NULL);
exit(1);
}
wait(NULL);
if (!fork()) /* r8180 */
{
close(0);
close(1);
close(2);
IGNORE_NZ(chdir("/"));
execlp("iwpriv", "iwpriv", iface, "prismhdr", "1", NULL);
exit(1);
}
wait(NULL);
if (!fork()) /* prism54 */
{
close(0);
close(1);
close(2);
IGNORE_NZ(chdir("/"));
execlp("iwpriv", "iwpriv", iface, "set_prismhdr", "1", NULL);
exit(1);
}
wait(NULL);
return (0);
}
static int openraw(struct priv_linux * dev,
char * iface,
int fd,
int * arptype,
unsigned char * mac)
{
REQUIRE(iface != NULL);
struct ifreq ifr;
struct ifreq ifr2;
struct iwreq wrq;
struct iwreq wrq2;
struct packet_mreq mr;
struct sockaddr_ll sll;
struct sockaddr_ll sll2;
if (strlen(iface) >= sizeof(ifr.ifr_name))
{
printf("Interface name too long: %s\n", iface);
return (1);
}
/* find the interface index */
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name) - 1);
if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0)
{
printf("Interface %s: \n", iface);
perror("ioctl(SIOCGIFINDEX) failed");
return (1);
}
memset(&sll, 0, sizeof(sll));
sll.sll_family = AF_PACKET;
sll.sll_ifindex = ifr.ifr_ifindex;
switch (dev->drivertype)
{
case DT_IPW2200:
/* find the interface index */
if (dev->main_if == NULL)
{
perror("Missing interface name");
return 1;
}
memset(&ifr2, 0, sizeof(ifr));
strncpy(ifr2.ifr_name, dev->main_if, sizeof(ifr2.ifr_name) - 1);
if (ioctl(dev->fd_main, SIOCGIFINDEX, &ifr2) < 0)
{
printf("Interface %s: \n", dev->main_if);
perror("ioctl(SIOCGIFINDEX) failed");
return (1);
}
/* set iw mode to managed on main interface */
memset(&wrq2, 0, sizeof(struct iwreq));
strncpy(wrq2.ifr_name, dev->main_if, IFNAMSIZ);
wrq2.ifr_name[IFNAMSIZ - 1] = 0;
if (ioctl(dev->fd_main, SIOCGIWMODE, &wrq2) < 0)
{
perror("SIOCGIWMODE");
return 1;
}
wrq2.u.mode = IW_MODE_INFRA;
if (ioctl(dev->fd_main, SIOCSIWMODE, &wrq2) < 0)
{
perror("SIOCSIWMODE");
return 1;
}
/* bind the raw socket to the interface */
memset(&sll2, 0, sizeof(sll2));
sll2.sll_family = AF_PACKET;
sll2.sll_ifindex = ifr2.ifr_ifindex;
sll2.sll_protocol = htons(ETH_P_ALL);
if (bind(dev->fd_main, //-V641
(struct sockaddr *) &sll2, //-V641
sizeof(sll2)) //-V641
< 0)
{
printf("Interface %s: \n", dev->main_if);
perror("bind(ETH_P_ALL) failed");
return (1);
}
opensysfs(dev, dev->main_if, dev->fd_in);
break;
case DT_BCM43XX:
opensysfs(dev, iface, dev->fd_in);
break;
case DT_WLANNG:
sll.sll_protocol = htons(ETH_P_80211_RAW);
break;
default:
sll.sll_protocol = htons(ETH_P_ALL);
break;
}
/* lookup the hardware type */
if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0)
{
printf("Interface %s: \n", iface);
perror("ioctl(SIOCGIFHWADDR) failed");
return (1);
}
/* lookup iw mode */
memset(&wrq, 0, sizeof(struct iwreq));
strncpy(wrq.ifr_name, iface, IFNAMSIZ);
wrq.ifr_name[IFNAMSIZ - 1] = 0;
if (ioctl(fd, SIOCGIWMODE, &wrq) < 0)
{
/* most probably not supported (ie for rtap ipw interface) *
* so just assume its correctly set... */
wrq.u.mode = IW_MODE_MONITOR;
}
if ((ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211
&& ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211_PRISM
&& ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211_FULL)
|| (wrq.u.mode != IW_MODE_MONITOR))
{
if (set_monitor(dev, iface, fd) && dev->drivertype != DT_ORINOCO)
{
ifr.ifr_flags &= ~(IFF_UP | IFF_BROADCAST | IFF_RUNNING);
if (ioctl(fd, SIOCSIFFLAGS, &ifr) < 0)
{
perror("ioctl(SIOCSIFFLAGS) failed");
return (1);
}
if (set_monitor(dev, iface, fd))
{
printf("Error setting monitor mode on %s\n", iface);
return (1);
}
}
}
/* Is interface st to up, broadcast & running ? */
if ((ifr.ifr_flags | IFF_UP | IFF_BROADCAST | IFF_RUNNING) != ifr.ifr_flags)
{
/* Bring interface up*/
ifr.ifr_flags |= IFF_UP | IFF_BROADCAST | IFF_RUNNING;
if (ioctl(fd, SIOCSIFFLAGS, &ifr) < 0)
{
perror("ioctl(SIOCSIFFLAGS) failed");
return (1);
}
}
/* bind the raw socket to the interface */
if (bind(fd, (struct sockaddr *) &sll, sizeof(sll)) < 0) //-V641
{
printf("Interface %s: \n", iface);
perror("bind(ETH_P_ALL) failed");
return (1);
}
/* lookup the hardware type */
if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0)
{
printf("Interface %s: \n", iface);
perror("ioctl(SIOCGIFHWADDR) failed");
return (1);
}
memcpy(mac, (unsigned char *) ifr.ifr_hwaddr.sa_data, 6); //-V512
*arptype = ifr.ifr_hwaddr.sa_family;
if (ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211
&& ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211_PRISM
&& ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211_FULL)
{
if (ifr.ifr_hwaddr.sa_family == ARPHRD_ETHERNET)
fprintf(stderr, "\nARP linktype is set to 1 (Ethernet) ");
else
fprintf(stderr,
"\nUnsupported hardware link type %4d ",
ifr.ifr_hwaddr.sa_family);
fprintf(stderr,
"- expected ARPHRD_IEEE80211,\nARPHRD_IEEE80211_"
"FULL or ARPHRD_IEEE80211_PRISM instead. Make\n"
"sure RFMON is enabled: run 'airmon-ng start %s"
" <#>'\nSysfs injection support was not found "
"either.\n\n",
iface);
return (1);
}
/* enable promiscuous mode */
memset(&mr, 0, sizeof(mr));
mr.mr_ifindex = sll.sll_ifindex;
mr.mr_type = PACKET_MR_PROMISC;
if (setsockopt(fd, SOL_PACKET, PACKET_ADD_MEMBERSHIP, &mr, sizeof(mr)) < 0)
{
perror("setsockopt(PACKET_MR_PROMISC) failed");
return (1);
}
return (0);
}
/*
* Open the interface and set mode monitor
* Return 1 on failure and 0 on success
*/
static int do_linux_open(struct wif * wi, char * iface)
{
int kver;
struct utsname checklinuxversion;
struct priv_linux * dev = wi_priv(wi);
char strbuf[512];
FILE * f;
char athXraw[] = "athXraw";
pid_t pid;
int n;
DIR * net_ifaces;
struct dirent * this_iface;
FILE * acpi = NULL;
char buf[128];
char * r_file = NULL;
struct ifreq ifr;
int iface_malloced = 0;
size_t iface_len = 0;
if (iface == NULL || strlen(iface) >= IFNAMSIZ)
{
return (1);
}
dev->inject_wlanng = 1;
dev->rate = 2; /* default to 1Mbps if nothing is set */
/* open raw socks */
if ((dev->fd_in = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) < 0)
{
perror("socket(PF_PACKET) failed");
if (getuid() != 0)
fprintf(stderr, "This program requires root privileges.\n");
return (1);
}
if ((dev->fd_main = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) < 0)
{
perror("socket(PF_PACKET) failed");
if (getuid() != 0)
fprintf(stderr, "This program requires root privileges.\n");
return (1);
}
#ifndef CONFIG_LIBNL
dev->iwpriv = wiToolsPath("iwpriv");
dev->iwconfig = wiToolsPath("iwconfig");
dev->ifconfig = wiToolsPath("ifconfig");
if (!(dev->iwpriv))
{
fprintf(stderr,
"Required wireless tools when compiled without libnl "
"could not be found, exiting.\n");
goto close_in;
}
#endif
/* Exit if ndiswrapper : check iwpriv ndis_reset */
if (is_ndiswrapper(iface, dev->iwpriv))
{
fprintf(stderr, "Ndiswrapper doesn't support monitor mode.\n");
goto close_in;
}
if ((dev->fd_out = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) < 0)
{
perror("socket(PF_PACKET) failed");
goto close_in;
}
/* figure out device type */
/* mac80211 radiotap injection
* detected based on interface called mon...
* since mac80211 allows multiple virtual interfaces
*
* note though that the virtual interfaces are ultimately using a
* single physical radio: that means for example they must all
* operate on the same channel
*/
/* mac80211 stack detection */
memset(strbuf, 0, sizeof(strbuf));
snprintf(strbuf,
sizeof(strbuf) - 1,
"ls /sys/class/net/%s/phy80211/subsystem >/dev/null 2>/dev/null",
iface);
if (system(strbuf) == 0) dev->drivertype = DT_MAC80211_RT;
/* IPW2200 detection */
memset(strbuf, 0, sizeof(strbuf));
snprintf(strbuf,
sizeof(strbuf) - 1,
"ls /sys/class/net/%s/device/inject >/dev/null 2>/dev/null",
iface);
if (system(strbuf) == 0) dev->drivertype = DT_IPW2200;
/* BCM43XX detection */
memset(strbuf, 0, sizeof(strbuf));
snprintf(strbuf,
sizeof(strbuf) - 1,
"ls /sys/class/net/%s/device/inject_nofcs >/dev/null 2>/dev/null",
iface);
if (system(strbuf) == 0) dev->drivertype = DT_BCM43XX;
/* check if wlan-ng or hostap or r8180 */
if (strlen(iface) == 5 && memcmp(iface, "wlan", 4) == 0)
{
memset(strbuf, 0, sizeof(strbuf));
snprintf(strbuf,
sizeof(strbuf) - 1,
"wlancfg show %s 2>/dev/null | "
"grep p2CnfWEPFlags >/dev/null",
iface);
if (system(strbuf) == 0)
{
if (uname(&checklinuxversion) >= 0)
{
/* uname succeeded */
if (strncmp(checklinuxversion.release, "2.6.", 4) == 0
&& strncasecmp(checklinuxversion.sysname, "linux", 5) == 0)
{
/* Linux kernel 2.6 */
kver = atoi(checklinuxversion.release + 4);
if (kver > 11)
{
/* That's a kernel > 2.6.11, cannot inject */
dev->inject_wlanng = 0;
}
}
}
dev->drivertype = DT_WLANNG;
dev->wlanctlng = wiToolsPath("wlanctl-ng");
}
memset(strbuf, 0, sizeof(strbuf));
snprintf(strbuf,
sizeof(strbuf) - 1,
"iwpriv %s 2>/dev/null | "
"grep antsel_rx >/dev/null",
iface);
if (system(strbuf) == 0) dev->drivertype = DT_HOSTAP;
memset(strbuf, 0, sizeof(strbuf));
snprintf(strbuf,
sizeof(strbuf) - 1,
"iwpriv %s 2>/dev/null | "
"grep GetAcx111Info >/dev/null",
iface);
if (system(strbuf) == 0) dev->drivertype = DT_ACX;
}
/* enable injection on ralink */
if (strcmp(iface, "ra0") == 0 || strcmp(iface, "ra1") == 0
|| strcmp(iface, "rausb0") == 0
|| strcmp(iface, "rausb1") == 0)
{
memset(strbuf, 0, sizeof(strbuf));
snprintf(strbuf,
sizeof(strbuf) - 1,
"iwpriv %s rfmontx 1 >/dev/null 2>/dev/null",
iface);
IGNORE_NZ(system(strbuf));
}
/* check if newer athXraw interface available */
if ((strlen(iface) >= 4 && strlen(iface) <= 6)
&& memcmp(iface, "ath", 3) == 0)
{
dev->drivertype = DT_MADWIFI;
memset(strbuf, 0, sizeof(strbuf));
snprintf(
strbuf, sizeof(strbuf) - 1, "/proc/sys/net/%s/%%parent", iface);
f = fopen(strbuf, "r");
if (f != NULL)
{
// It is madwifi-ng
dev->drivertype = DT_MADWIFING;
fclose(f);
/* should we force prism2 header? */
sprintf((char *) strbuf, "/proc/sys/net/%s/dev_type", iface);
f = fopen((char *) strbuf, "w");
if (f != NULL)
{
fprintf(f, "802\n");
fclose(f);
}
/* Force prism2 header on madwifi-ng */
}
else
{
// Madwifi-old
memset(strbuf, 0, sizeof(strbuf));
snprintf(strbuf,
sizeof(strbuf) - 1,
"sysctl -w dev.%s.rawdev=1 >/dev/null 2>/dev/null",
iface);
if (system(strbuf) == 0)
{
athXraw[3] = iface[3];
memset(strbuf, 0, sizeof(strbuf));
snprintf(strbuf, sizeof(strbuf) - 1, "ifconfig %s up", athXraw);
IGNORE_NZ(system(strbuf));
#if 0 /* some people reported problems when prismheader is enabled */
memset( strbuf, 0, sizeof( strbuf ) );
snprintf( strbuf, sizeof( strbuf ) - 1,
"sysctl -w dev.%s.rawdev_type=1 >/dev/null 2>/dev/null",
iface );
IGNORE_NZ(system( strbuf ));
#endif
iface = athXraw;
}
}
}
if (memcmp(iface, "eth", 3) == 0)
{
/* test if orinoco */
if ((pid = fork()) == 0)
{
close(0);
close(1);
close(2);
IGNORE_NZ(chdir("/"));
execlp("iwpriv", "iwpriv", iface, "get_port3", NULL);
exit(1);
}
waitpid(pid, &n, 0);
if (WIFEXITED(n) && WEXITSTATUS(n) == 0) dev->drivertype = DT_ORINOCO;
memset(strbuf, 0, sizeof(strbuf));
snprintf(strbuf,
sizeof(strbuf) - 1,
"iwpriv %s 2>/dev/null | "
"grep get_scan_times >/dev/null",
iface);
if (system(strbuf) == 0) dev->drivertype = DT_AT76USB;
/* test if zd1211rw */
if ((pid = fork()) == 0)
{
close(0);
close(1);
close(2);
IGNORE_NZ(chdir("/"));
execlp("iwpriv", "iwpriv", iface, "get_regdomain", NULL);
exit(1);
}
waitpid(pid, &n, 0);
if (WIFEXITED(n) && WEXITSTATUS(n) == 0) dev->drivertype = DT_ZD1211RW;
}
if (dev->drivertype == DT_IPW2200)
{
r_file = (char *) calloc(33 + strlen(iface) + 1, sizeof(char));
if (!r_file)
{
goto close_out;
}
snprintf(r_file,
33 + strlen(iface) + 1,
"/sys/class/net/%s/device/rtap_iface",
iface);
if ((acpi = fopen(r_file, "r")) == NULL) goto close_out;
memset(buf, 0, 128);
IGNORE_ZERO(fgets(buf, 128, acpi));
buf[127] = '\x00';
// rtap iface doesn't exist
if (strncmp(buf, "-1", 2) == 0)
{
// repoen for writing
fclose(acpi);
if ((acpi = fopen(r_file, "w")) == NULL) goto close_out;
fputs("1", acpi);
// reopen for reading
fclose(acpi);
if ((acpi = fopen(r_file, "r")) == NULL) goto close_out;
IGNORE_ZERO(fgets(buf, 128, acpi));
}
fclose(acpi);
acpi = NULL;
// use name in buf as new iface and set original iface as main iface
iface_len = strlen(iface) + 1;
dev->main_if = (char *) malloc(iface_len);
if (dev->main_if == NULL) goto close_out;
memset(dev->main_if, 0, iface_len);
memcpy(dev->main_if, iface, iface_len);
iface_len = strlen(buf) + 1;
iface = (char *) malloc(iface_len);
if (iface == NULL) goto close_out;
iface_malloced = 1;
memset(iface, 0, iface_len);
memcpy(iface, buf, iface_len);
}
/* test if rtap interface and try to find real interface */
if (memcmp(iface, "rtap", 4) == 0 && dev->main_if == NULL)
{
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name) - 1);
n = 0;
if (ioctl(dev->fd_out, SIOCGIFINDEX, &ifr) < 0)
{
// create rtap interface
n = 1;
}
net_ifaces = opendir("/sys/class/net");
while (net_ifaces != NULL && (this_iface = readdir(net_ifaces)) != NULL)
{
if (this_iface->d_name[0] == '.') continue;
char * new_r_file = (char *) realloc(
r_file, (33 + strlen(this_iface->d_name) + 1) * sizeof(char));
if (!new_r_file)
{
continue;
}
r_file = new_r_file;
snprintf(r_file,
33 + strlen(this_iface->d_name) + 1,
"/sys/class/net/%s/device/rtap_iface",
this_iface->d_name);
if ((acpi = fopen(r_file, "r")) == NULL) continue;
dev->drivertype = DT_IPW2200;
memset(buf, 0, 128);
IGNORE_ZERO(fgets(buf, 128, acpi));
if (n == 0) // interface exists
{
if (strncmp(buf, iface, 5) == 0)
{
fclose(acpi);
acpi = NULL;
closedir(net_ifaces);
net_ifaces = NULL;
dev->main_if
= (char *) malloc(strlen(this_iface->d_name) + 1);
if (dev->main_if == NULL) continue;
strcpy(dev->main_if, this_iface->d_name);
break;
}
}
else // need to create interface
{
if (strncmp(buf, "-1", 2) == 0)
{
// repoen for writing
fclose(acpi);
if ((acpi = fopen(r_file, "w")) == NULL) continue;
fputs("1", acpi);
// reopen for reading
fclose(acpi);
if ((acpi = fopen(r_file, "r")) == NULL) continue;
IGNORE_ZERO(fgets(buf, 128, acpi));
if (strncmp(buf, iface, 5) == 0)
{
closedir(net_ifaces);
net_ifaces = NULL;
dev->main_if
= (char *) malloc(strlen(this_iface->d_name) + 1);
if (dev->main_if == NULL) continue;
strcpy(dev->main_if, this_iface->d_name);
fclose(acpi);
acpi = NULL;
break;
}
}
}
fclose(acpi);
acpi = NULL;
}
if (net_ifaces != NULL) closedir(net_ifaces);
}
if (openraw(dev, iface, dev->fd_out, &dev->arptype_out, dev->pl_mac) != 0)
{
goto close_out;
}
/* don't use the same file descriptor for in and out on bcm43xx,
as you read from the interface, but write into a file in /sys/...
*/
if (!(dev->drivertype == DT_BCM43XX) && !(dev->drivertype == DT_IPW2200))
{
close(dev->fd_in);
dev->fd_in = dev->fd_out;
}
else
{
/* if bcm43xx or ipw2200, swap both fds */
n = dev->fd_out;
dev->fd_out = dev->fd_in;
dev->fd_in = n;
}
dev->arptype_in = dev->arptype_out;
if (iface_malloced) free(iface);
if (r_file)
{
free(r_file);
}
return 0;
close_out:
close(dev->fd_out);
if (r_file)
{
free(r_file);
}
close_in:
close(dev->fd_in);
if (acpi) fclose(acpi);
if (iface_malloced) free(iface);
return 1;
}
static void do_free(struct wif * wi)
{
struct priv_linux * pl = wi_priv(wi);
if (pl->wlanctlng) free(pl->wlanctlng);
if (pl->iwpriv) free(pl->iwpriv);
if (pl->iwconfig) free(pl->iwconfig);
if (pl->ifconfig) free(pl->ifconfig);
if (pl->wl) free(pl->wl);
if (pl->main_if) free(pl->main_if);
free(pl);
free(wi);
}
#ifndef CONFIG_LIBNL
static void linux_close(struct wif * wi)
{
struct priv_linux * pl = wi_priv(wi);
if (pl->fd_in && pl->fd_out && pl->fd_in == pl->fd_out)
{
// Only close one if both are the same
close(pl->fd_in);
}
else
{
if (pl->fd_in) close(pl->fd_in);
if (pl->fd_out) close(pl->fd_out);
}
if (pl->fd_main) close(pl->fd_main);
do_free(wi);
}
#else
static void linux_close_nl80211(struct wif * wi)
{
struct priv_linux * pl = wi_priv(wi);
nl80211_cleanup(&state);
if (pl->fd_in) close(pl->fd_in);
if (pl->fd_out) close(pl->fd_out);
do_free(wi);
}
#endif
static int linux_fd(struct wif * wi)
{
struct priv_linux * pl = wi_priv(wi);
return pl->fd_in;
}
static int linux_get_mac(struct wif * wi, unsigned char * mac)
{
struct priv_linux * pl = wi_priv(wi);
struct ifreq ifr;
int fd;
fd = wi_fd(wi);
/* find the interface index */
/* ipw2200 got a file opened as fd */
if (pl->drivertype == DT_IPW2200)
{
memcpy(mac, pl->pl_mac, 6);
return 0;
}
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, wi_get_ifname(wi), sizeof(ifr.ifr_name) - 1);
if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0)
{
printf("Interface %s: \n", wi_get_ifname(wi));
perror("ioctl(SIOCGIFINDEX) failed");
return (1);
}
if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0)
{
printf("Interface %s: \n", wi_get_ifname(wi));
perror("ioctl(SIOCGIFHWADDR) failed");
return (1);
}
memcpy(pl->pl_mac, (unsigned char *) ifr.ifr_hwaddr.sa_data, 6);
/* XXX */
memcpy(mac, pl->pl_mac, 6);
return 0;
}
static int linux_set_mac(struct wif * wi, unsigned char * mac)
{
struct priv_linux * pl = wi_priv(wi);
struct ifreq ifr;
int fd, ret;
fd = wi_fd(wi);
/* find the interface index */
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, wi_get_ifname(wi), sizeof(ifr.ifr_name) - 1);
if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0)
{
printf("Interface %s: \n", wi_get_ifname(wi));
perror("ioctl(SIOCGIFHWADDR) failed");
return (1);
}
// if down
ifr.ifr_flags &= ~(IFF_UP | IFF_BROADCAST | IFF_RUNNING);
if (ioctl(fd, SIOCSIFFLAGS, &ifr) < 0)
{
perror("ioctl(SIOCSIFFLAGS) failed");
return (1);
}
ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
memcpy(ifr.ifr_hwaddr.sa_data, mac, 6); //-V512
memcpy(pl->pl_mac, mac, 6);
// set mac
ret = ioctl(fd, SIOCSIFHWADDR, &ifr);
// if up
ifr.ifr_flags |= IFF_UP | IFF_BROADCAST | IFF_RUNNING;
if (ioctl(fd, SIOCSIFFLAGS, &ifr) < 0)
{
perror("ioctl(SIOCSIFFLAGS) failed");
return (1);
}
return ret;
}
static struct wif * linux_open(char * iface)
{
struct wif * wi;
struct priv_linux * pl;
if (iface == NULL || strlen(iface) >= IFNAMSIZ)
{
return NULL;
}
wi = wi_alloc(sizeof(*pl));
if (!wi) return NULL;
wi->wi_read = linux_read;
wi->wi_write = linux_write;
#ifdef CONFIG_LIBNL
linux_nl80211_init(&state);
wi->wi_set_ht_channel = linux_set_ht_channel_nl80211;
wi->wi_set_channel = linux_set_channel_nl80211;
#else
wi->wi_set_channel = linux_set_channel;
#endif // CONFIG_LIBNL
wi->wi_get_channel = linux_get_channel;
wi->wi_set_freq = linux_set_freq;
wi->wi_get_freq = linux_get_freq;
#ifdef CONFIG_LIBNL
wi->wi_close = linux_close_nl80211;
#else
wi->wi_close = linux_close;
#endif
wi->wi_fd = linux_fd;
wi->wi_get_mac = linux_get_mac;
wi->wi_set_mac = linux_set_mac;
wi->wi_get_monitor = linux_get_monitor;
wi->wi_get_rate = linux_get_rate;
wi->wi_set_rate = linux_set_rate;
wi->wi_get_mtu = linux_get_mtu;
wi->wi_set_mtu = linux_set_mtu;
if (do_linux_open(wi, iface))
{
do_free(wi);
return NULL;
}
return wi;
}
struct wif * wi_open_osdep(char * iface) { return linux_open(iface); }
EXPORT int get_battery_state(void)
{
char buf[128];
int batteryTime = 0;
FILE * apm;
unsigned flag;
char units[32];
int ret;
static int linux_apm = 1;
static int linux_acpi = 1;
if (linux_apm == 1)
{
char * battery_data = NULL;
if ((apm = fopen("/proc/apm", "r")) != NULL)
{
battery_data = fgets(buf, 128, apm);
fclose(apm);
}
if (battery_data != NULL)
{
unsigned charging, ac;
ret = sscanf(battery_data,
"%*s %*d.%*d %*x %x %x %x %*d%% %d %31s\n",
&ac,
&charging,
&flag,
&batteryTime,
units);
if (!ret) return 0;
if ((flag & 0x80) == 0 && charging != 0xFF && ac != 1
&& batteryTime == -1)
{
if (!strncmp(units, "min", 3)) batteryTime *= 60;
}
else
return 0;
linux_acpi = 0;
return batteryTime;
}
linux_apm = 0;
}
if (linux_acpi && !linux_apm)
{
DIR *batteries, *ac_adapters;
struct dirent *this_battery, *this_adapter;
FILE *acpi, *info;
char battery_state[28 + sizeof(this_adapter->d_name) + 1];
char battery_info[24 + sizeof(this_battery->d_name) + 1];
int rate = 1, remain = 0;
static int total_remain = 0, total_cap = 0;
int batno = 0;
static int info_timer = 0;
int batt_full_capacity[3];
linux_acpi = 1;
ac_adapters = opendir("/proc/acpi/ac_adapter");
if (ac_adapters == NULL) return 0;
while ((this_adapter = readdir(ac_adapters)) != NULL)
{
if (this_adapter->d_name[0] == '.')
{
continue;
}
/* safe overloaded use of battery_state path var */
snprintf(battery_state,
sizeof(battery_state),
"/proc/acpi/ac_adapter/%s/state",
this_adapter->d_name);
if ((acpi = fopen(battery_state, "r")) == NULL)
{
continue;
}
while (fgets(buf, 128, acpi))
{
if (strstr(buf, "on-line") != NULL)
{
fclose(acpi);
closedir(ac_adapters);
return 0;
}
}
fclose(acpi);
}
closedir(ac_adapters);
batteries = opendir("/proc/acpi/battery");
if (batteries == NULL)
{
return 0;
}
while ((this_battery = readdir(batteries)) != NULL)
{
if (this_battery->d_name[0] == '.') continue;
snprintf(battery_info,
sizeof(battery_info),
"/proc/acpi/battery/%s/info",
this_battery->d_name);
info = fopen(battery_info, "r");
batt_full_capacity[batno] = 0;
if (info != NULL)
{
while (fgets(buf, sizeof(buf), info) != NULL)
if (sscanf(buf,
"last full capacity: %d mWh",
&batt_full_capacity[batno])
== 1)
continue;
fclose(info);
}
snprintf(battery_state,
sizeof(battery_state),
"/proc/acpi/battery/%s/state",
this_battery->d_name);
if ((acpi = fopen(battery_state, "r")) == NULL) continue;
while (fgets(buf, 128, acpi))
{
if (strncmp(buf, "present:", 8) == 0)
{
/* No information for this battery */
if (strstr(buf, "no")) continue;
}
else if (strncmp(buf, "charging state:", 15) == 0)
{
/* the space makes it different than discharging */
if (strstr(buf, " charging"))
{
closedir(batteries);
fclose(acpi);
return 0;
}
}
else if (strncmp(buf, "present rate:", 13) == 0)
rate = atoi(buf + 25);
else if (strncmp(buf, "remaining capacity:", 19) == 0)
{
remain = atoi(buf + 25);
total_remain += remain;
}
}
total_cap += batt_full_capacity[batno];
fclose(acpi);
if (rate != 0)
batteryTime += (int) ((((float) remain) / rate) * 3600);
batno++;
}
info_timer++;
closedir(batteries);
}
return batteryTime;
}
|