main.c (20788B)
1 /* 2 * 3 * Wireless daemon for Linux 4 * 5 * Copyright (C) 2013-2019 Intel Corporation. All rights reserved. 6 * 7 * This library is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU Lesser General Public 9 * License as published by the Free Software Foundation; either 10 * version 2.1 of the License, or (at your option) any later version. 11 * 12 * This library is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * Lesser General Public License for more details. 16 * 17 * You should have received a copy of the GNU Lesser General Public 18 * License along with this library; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 20 * 21 */ 22 23 #ifdef HAVE_CONFIG_H 24 #include <config.h> 25 #endif 26 27 #define _GNU_SOURCE 28 #include <stdio.h> 29 #include <errno.h> 30 #include <ctype.h> 31 #include <stdlib.h> 32 #include <string.h> 33 #include <getopt.h> 34 #include <signal.h> 35 #include <unistd.h> 36 #include <arpa/inet.h> 37 #include <sys/socket.h> 38 #include <linux/genetlink.h> 39 #include <linux/rtnetlink.h> 40 #include <linux/if_arp.h> 41 #include <linux/filter.h> 42 #include <sys/ioctl.h> 43 #include <ell/ell.h> 44 45 #ifndef ARPHRD_NETLINK 46 #define ARPHRD_NETLINK 824 47 #endif 48 49 #include "linux/nl80211.h" 50 #include "monitor/nlmon.h" 51 #include "monitor/pcap.h" 52 #include "monitor/display.h" 53 54 #define MAX_SNAPLEN (1024 * 16) 55 56 static struct nlmon *nlmon = NULL; 57 static const char *writer_path = NULL; 58 static struct l_timeout *timeout = NULL; 59 static struct nlmon_config config; 60 61 #define NLA_OK(nla,len) ((len) >= (int) sizeof(struct nlattr) && \ 62 (nla)->nla_len >= sizeof(struct nlattr) && \ 63 (nla)->nla_len <= (len)) 64 #define NLA_NEXT(nla,attrlen) ((attrlen) -= NLA_ALIGN((nla)->nla_len), \ 65 (struct nlattr*)(((char*)(nla)) + \ 66 NLA_ALIGN((nla)->nla_len))) 67 68 #define NLA_LENGTH(len) (NLA_ALIGN(sizeof(struct nlattr)) + (len)) 69 #define NLA_DATA(nla) ((void*)(((char*)(nla)) + NLA_LENGTH(0))) 70 #define NLA_PAYLOAD(nla) ((int)((nla)->nla_len - NLA_LENGTH(0))) 71 72 #define NLMON_TYPE "nlmon" 73 #define NLMON_LEN 5 74 75 static bool nlmon_receive(struct l_io *io, void *user_data) 76 { 77 struct nlmon *nlmon = user_data; 78 struct msghdr msg; 79 struct sockaddr_ll sll; 80 struct iovec iov; 81 struct cmsghdr *cmsg; 82 struct timeval copy_tv; 83 const struct timeval *tv = NULL; 84 uint16_t proto_type; 85 unsigned char buf[8192]; 86 unsigned char control[32]; 87 ssize_t bytes_read; 88 int fd; 89 90 fd = l_io_get_fd(io); 91 if (fd < 0) 92 return false; 93 94 memset(&sll, 0, sizeof(sll)); 95 96 memset(&iov, 0, sizeof(iov)); 97 iov.iov_base = buf; 98 iov.iov_len = sizeof(buf); 99 100 memset(&msg, 0, sizeof(msg)); 101 msg.msg_name = &sll; 102 msg.msg_namelen = sizeof(sll); 103 msg.msg_iov = &iov; 104 msg.msg_iovlen = 1; 105 msg.msg_control = control; 106 msg.msg_controllen = sizeof(control); 107 108 bytes_read = recvmsg(fd, &msg, 0); 109 if (bytes_read < 0) { 110 if (errno != EAGAIN && errno != EINTR) 111 return false; 112 113 return true; 114 } 115 116 if (sll.sll_hatype != ARPHRD_NETLINK) 117 return true; 118 119 proto_type = ntohs(sll.sll_protocol); 120 121 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; 122 cmsg = CMSG_NXTHDR(&msg, cmsg)) { 123 if (cmsg->cmsg_level == SOL_SOCKET && 124 cmsg->cmsg_type == SCM_TIMESTAMP) { 125 memcpy(©_tv, CMSG_DATA(cmsg), sizeof(copy_tv)); 126 tv = ©_tv; 127 } 128 } 129 130 switch (proto_type) { 131 case NETLINK_ROUTE: 132 nlmon_print_rtnl(nlmon, tv, iov.iov_base, bytes_read); 133 break; 134 case NETLINK_GENERIC: 135 nlmon_print_genl(nlmon, tv, iov.iov_base, bytes_read); 136 break; 137 } 138 139 return true; 140 } 141 142 /* 143 * BPF filter to match skb->dev->type == 824 (ARPHRD_NETLINK) and 144 * either match skb->protocol == 0x0000 (NETLINK_ROUTE) or match 145 * skb->protocol == 0x0010 (NETLINK_GENERIC). 146 */ 147 static struct sock_filter mon_filter[] = { 148 { 0x28, 0, 0, 0xfffff01c }, /* ldh #hatype */ 149 { 0x15, 0, 3, 0x00000338 }, /* jne #824, drop */ 150 { 0x28, 0, 0, 0xfffff000 }, /* ldh #proto */ 151 { 0x15, 2, 0, 0000000000 }, /* jeq #0x0000, pass */ 152 { 0x15, 1, 0, 0x00000010 }, /* jeq #0x0010, pass */ 153 { 0x06, 0, 0, 0000000000 }, /* drop: ret #0 */ 154 { 0x06, 0, 0, 0xffffffff }, /* pass: ret #-1 */ 155 }; 156 157 static const struct sock_fprog mon_fprog = { .len = 7, .filter = mon_filter }; 158 159 static struct l_io *open_packet(const char *name) 160 { 161 struct l_io *io; 162 struct sockaddr_ll sll; 163 struct packet_mreq mr; 164 struct ifreq ifr; 165 int fd, opt = 1; 166 167 fd = socket(PF_PACKET, SOCK_RAW | SOCK_CLOEXEC | SOCK_NONBLOCK, 0); 168 if (fd < 0) { 169 perror("Failed to create packet socket"); 170 return NULL; 171 } 172 173 strncpy(ifr.ifr_name, name, IFNAMSIZ - 1); 174 175 if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0) { 176 perror("Failed to get monitor index"); 177 close(fd); 178 return NULL; 179 } 180 181 memset(&sll, 0, sizeof(sll)); 182 sll.sll_family = AF_PACKET; 183 sll.sll_protocol = htons(ETH_P_ALL); 184 sll.sll_ifindex = ifr.ifr_ifindex; 185 186 if (bind(fd, (struct sockaddr *) &sll, sizeof(sll)) < 0) { 187 perror("Failed to bind packet socket"); 188 close(fd); 189 return NULL; 190 } 191 192 memset(&mr, 0, sizeof(mr)); 193 mr.mr_ifindex = ifr.ifr_ifindex; 194 mr.mr_type = PACKET_MR_ALLMULTI; 195 196 if (setsockopt(fd, SOL_PACKET, PACKET_ADD_MEMBERSHIP, 197 &mr, sizeof(mr)) < 0) { 198 perror("Failed to enable all multicast"); 199 close(fd); 200 return NULL; 201 } 202 203 if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, 204 &mon_fprog, sizeof(mon_fprog)) < 0) { 205 perror("Failed to enable monitor filter"); 206 close(fd); 207 return NULL; 208 } 209 210 if (setsockopt(fd, SOL_SOCKET, SO_TIMESTAMP, &opt, sizeof(opt)) < 0) { 211 perror("Failed to enable monitor timestamps"); 212 close(fd); 213 return NULL; 214 } 215 216 io = l_io_new(fd); 217 218 l_io_set_close_on_destroy(io, true); 219 220 return io; 221 } 222 223 struct iwmon_interface { 224 char *ifname; 225 bool exists; 226 struct l_netlink *rtnl; 227 struct l_genl *genl; 228 struct l_io *io; 229 }; 230 231 static struct iwmon_interface monitor_interface = { }; 232 233 static void nl80211_appeared(const struct l_genl_family_info *info, 234 void *user_data) 235 { 236 const char *ifname = user_data; 237 238 monitor_interface.io = open_packet(ifname); 239 if (!monitor_interface.io) 240 goto failed; 241 242 nlmon = nlmon_open(l_genl_family_info_get_id(info), 243 writer_path, &config); 244 if (!nlmon) 245 goto failed; 246 247 l_io_set_read_handler(monitor_interface.io, nlmon_receive, nlmon, NULL); 248 return; 249 250 failed: 251 l_main_quit(); 252 } 253 254 static struct l_genl *genl_lookup(const char *ifname) 255 { 256 struct l_genl *genl = l_genl_new(); 257 258 l_genl_request_family(genl, NL80211_GENL_NAME, nl80211_appeared, 259 (char *) ifname, NULL); 260 return genl; 261 } 262 263 static bool rta_linkinfo_kind(struct rtattr *rta, unsigned short len, 264 const char* kind) 265 { 266 for (; RTA_OK(rta, len); rta = RTA_NEXT(rta, len)) { 267 if (rta->rta_type != IFLA_INFO_KIND) 268 continue; 269 270 if (rta->rta_len < NLMON_LEN) 271 continue; 272 273 if (memcmp(RTA_DATA(rta), kind, strlen(kind))) 274 continue; 275 276 return true; 277 } 278 279 return false; 280 } 281 282 static struct l_netlink *rtm_interface_send_message(struct l_netlink *rtnl, 283 const char *ifname, 284 uint16_t rtm_msg_type, 285 l_netlink_command_func_t callback, 286 void *user_data, 287 l_netlink_destroy_func_t destroy) 288 { 289 size_t nlmon_type_len = strlen(NLMON_TYPE); 290 unsigned short ifname_len = 0; 291 struct l_netlink_message *nlm; 292 struct ifinfomsg ifi; 293 uint16_t flags = 0; 294 295 if (ifname) { 296 ifname_len = strlen(ifname) + 1; 297 298 if (ifname_len < 2 || ifname_len > IFNAMSIZ) 299 return NULL; 300 } 301 302 if (!L_IN_SET(rtm_msg_type, RTM_NEWLINK, RTM_DELLINK, RTM_GETLINK)) 303 return NULL; 304 305 if (!rtnl) 306 rtnl = l_netlink_new(NETLINK_ROUTE); 307 308 if (!rtnl) 309 return NULL; 310 311 memset(&ifi, 0, sizeof(ifi)); 312 ifi.ifi_family = AF_UNSPEC; 313 ifi.ifi_change = ~0; 314 315 switch (rtm_msg_type) { 316 case RTM_NEWLINK: 317 ifi.ifi_flags = IFF_UP | IFF_ALLMULTI | IFF_NOARP; 318 flags = NLM_F_CREATE | NLM_F_EXCL; 319 break; 320 case RTM_GETLINK: 321 flags = NLM_F_DUMP; 322 break; 323 } 324 325 nlm = l_netlink_message_new(rtm_msg_type, flags);; 326 l_netlink_message_add_header(nlm, &ifi, sizeof(ifi)); 327 328 if (ifname) 329 l_netlink_message_append(nlm, IFLA_IFNAME, ifname, ifname_len); 330 331 l_netlink_message_enter_nested(nlm, IFLA_LINKINFO); 332 l_netlink_message_append(nlm, IFLA_INFO_KIND, 333 NLMON_TYPE, nlmon_type_len); 334 l_netlink_message_leave_nested(nlm); 335 336 l_netlink_send(rtnl, nlm, callback, user_data, destroy); 337 338 return rtnl; 339 } 340 341 static struct l_netlink *iwmon_interface_disable( 342 struct iwmon_interface *monitor_interface) 343 { 344 if (!monitor_interface->exists) 345 return rtm_interface_send_message(monitor_interface->rtnl, 346 monitor_interface->ifname, 347 RTM_DELLINK, NULL, NULL, NULL); 348 349 return monitor_interface->rtnl; 350 } 351 352 static void iwmon_interface_enable_callback(int error, uint16_t type, 353 const void *data, uint32_t len, 354 void *user_data) 355 { 356 struct iwmon_interface *monitor_interface = user_data; 357 358 if (error) { 359 fprintf(stderr, "Failed to create monitor interface %s: %s\n", 360 monitor_interface->ifname, strerror(error)); 361 362 l_main_quit(); 363 return; 364 } 365 366 printf("Created interface %s\n", monitor_interface->ifname); 367 368 monitor_interface->genl = genl_lookup(monitor_interface->ifname); 369 } 370 371 static struct l_netlink *iwmon_interface_enable( 372 struct iwmon_interface *monitor_interface) 373 { 374 return rtm_interface_send_message(monitor_interface->rtnl, 375 monitor_interface->ifname, 376 RTM_NEWLINK, 377 iwmon_interface_enable_callback, 378 monitor_interface, NULL); 379 } 380 381 static void iwmon_interface_lookup_done(void *user_data) 382 { 383 struct iwmon_interface *monitor_interface = user_data; 384 385 if (monitor_interface->exists && monitor_interface->ifname) { 386 printf("Using %s as Monitor interface\n", 387 monitor_interface->ifname); 388 389 monitor_interface->genl = 390 genl_lookup(monitor_interface->ifname); 391 392 return; 393 } 394 395 if (!monitor_interface->ifname) 396 monitor_interface->ifname = l_strdup(NLMON_TYPE); 397 398 iwmon_interface_enable(monitor_interface); 399 } 400 401 static void iwmon_interface_lookup_callback(int error, uint16_t type, 402 const void *data, uint32_t len, 403 void *user_data) 404 { 405 const struct ifinfomsg *rtmmsg = data; 406 struct rtattr *rta; 407 struct iwmon_interface *monitor_interface = user_data; 408 const char *ifname = NULL; 409 unsigned short ifname_len = 0; 410 bool nlmon = false; 411 412 if (error) 413 return; 414 415 if (type != RTM_NEWLINK) 416 return; 417 418 for (rta = (struct rtattr *)(rtmmsg + 1); RTA_OK(rta, len); 419 rta = RTA_NEXT(rta, len)) { 420 switch(rta->rta_type) { 421 case IFLA_IFNAME: 422 ifname = RTA_DATA(rta); 423 ifname_len = rta->rta_len; 424 break; 425 426 case IFLA_LINKINFO: 427 nlmon = rta_linkinfo_kind(RTA_DATA(rta), rta->rta_len, 428 NLMON_TYPE); 429 break; 430 431 default: 432 break; 433 } 434 } 435 436 if (!ifname) 437 return; 438 439 if (!nlmon) 440 return; 441 442 if ((rtmmsg->ifi_flags & (IFF_UP | IFF_ALLMULTI | IFF_NOARP)) != 443 (IFF_UP | IFF_ALLMULTI | IFF_NOARP)) 444 return; 445 446 l_free(monitor_interface->ifname); 447 monitor_interface->ifname = l_strndup(ifname, ifname_len); 448 monitor_interface->exists = true; 449 } 450 451 static void iwmon_interface_lookup(struct iwmon_interface *monitor_interface) 452 { 453 monitor_interface->rtnl = 454 rtm_interface_send_message(monitor_interface->rtnl, NULL, 455 RTM_GETLINK, 456 iwmon_interface_lookup_callback, 457 monitor_interface, 458 iwmon_interface_lookup_done); 459 } 460 461 static int analyze_pcap(const char *pathname) 462 { 463 struct l_queue *genl_list; 464 const struct l_queue_entry *genl_entry; 465 struct pcap *pcap; 466 struct timeval tv; 467 void *buf; 468 uint32_t snaplen, len, real_len; 469 int exit_status; 470 unsigned long pkt_count = 0; 471 unsigned long pkt_short = 0; 472 unsigned long pkt_trunc = 0; 473 unsigned long pkt_ether = 0; 474 unsigned long pkt_pae = 0; 475 unsigned long pkt_netlink = 0; 476 unsigned long pkt_rtnl = 0; 477 unsigned long pkt_genl = 0; 478 unsigned long msg_netlink = 0; 479 unsigned long msg_rtnl = 0; 480 unsigned long msg_genl = 0; 481 bool first; 482 483 pcap = pcap_open(pathname); 484 if (!pcap) 485 return EXIT_FAILURE; 486 487 if (pcap_get_type(pcap) != PCAP_TYPE_LINUX_SLL) { 488 fprintf(stderr, "Invalid packet format\n"); 489 exit_status = EXIT_FAILURE; 490 goto done; 491 } 492 493 snaplen = pcap_get_snaplen(pcap); 494 if (snaplen > MAX_SNAPLEN) 495 snaplen = MAX_SNAPLEN; 496 497 buf = malloc(snaplen); 498 if (!buf) { 499 fprintf(stderr, "Failed to allocate packet buffer\n"); 500 exit_status = EXIT_FAILURE; 501 goto done; 502 } 503 504 genl_list = l_queue_new(); 505 506 while (pcap_read(pcap, &tv, buf, snaplen, &len, &real_len)) { 507 struct nlmsghdr *nlmsg; 508 int64_t aligned_len; 509 uint16_t arphrd_type; 510 uint16_t proto_type; 511 512 pkt_count++; 513 514 if (len < 16) { 515 pkt_short++; 516 continue; 517 } 518 519 arphrd_type = l_get_be16(buf + 2); 520 proto_type = l_get_be16(buf + 14); 521 522 switch (arphrd_type) { 523 case ARPHRD_ETHER: 524 pkt_ether++; 525 switch (proto_type) { 526 case ETH_P_PAE: 527 pkt_pae++; 528 break; 529 } 530 break; 531 case ARPHRD_NETLINK: 532 pkt_netlink++; 533 switch (proto_type) { 534 case NETLINK_ROUTE: 535 pkt_rtnl++; 536 break; 537 case NETLINK_GENERIC: 538 pkt_genl++; 539 break; 540 } 541 break; 542 } 543 544 if (len < real_len) { 545 pkt_trunc++; 546 continue; 547 } 548 549 if (arphrd_type != ARPHRD_NETLINK) 550 continue; 551 552 aligned_len = NLMSG_ALIGN(len - 16); 553 554 for (nlmsg = buf + 16; NLMSG_OK(nlmsg, aligned_len); 555 nlmsg = NLMSG_NEXT(nlmsg, aligned_len)) { 556 uint16_t type = nlmsg->nlmsg_type; 557 558 msg_netlink++; 559 switch (proto_type) { 560 case NETLINK_ROUTE: 561 msg_rtnl++; 562 break; 563 case NETLINK_GENERIC: 564 if (type >= NLMSG_MIN_TYPE) { 565 l_queue_remove(genl_list, 566 L_UINT_TO_PTR(type)); 567 l_queue_push_tail(genl_list, 568 L_UINT_TO_PTR(type)); 569 } 570 msg_genl++; 571 break; 572 } 573 } 574 } 575 576 printf("\n"); 577 printf(" Analyzed file: %s\n", pathname); 578 printf("\n"); 579 printf(" Number of packets: %lu\n", pkt_count); 580 printf(" Short packets: %lu\n", pkt_short); 581 printf(" Truncated packets: %lu\n", pkt_trunc); 582 printf("\n"); 583 printf(" Ethernet packets: %lu\n", pkt_ether); 584 printf(" PAE packets: %lu\n", pkt_pae); 585 printf("\n"); 586 printf(" Netlink packets: %lu\n", pkt_netlink); 587 printf(" RTNL packets: %lu\n", pkt_rtnl); 588 printf(" GENL packets: %lu\n", pkt_genl); 589 printf("\n"); 590 printf(" Netlink messages: %lu\n", msg_netlink); 591 printf(" RTNL messages: %lu\n", msg_rtnl); 592 printf(" GENL messages: %lu\n", msg_genl); 593 printf("\n"); 594 for (genl_entry = l_queue_get_entries(genl_list), first = true; 595 genl_entry; 596 genl_entry = genl_entry->next, first = false) { 597 uint16_t family = L_PTR_TO_UINT(genl_entry->data); 598 const char *label, *desc; 599 600 if (first) 601 label = " GENL families:"; 602 else 603 label = " "; 604 605 if (family == GENL_ID_CTRL) 606 desc = "nlctrl"; 607 else 608 desc = ""; 609 610 printf("%s 0x%02x (%u) %s\n", label, family, family, desc); 611 } 612 printf("\n"); 613 614 l_queue_destroy(genl_list, NULL); 615 616 free(buf); 617 618 exit_status = EXIT_SUCCESS; 619 620 done: 621 pcap_close(pcap); 622 623 return exit_status; 624 } 625 626 static int process_pcap(struct pcap *pcap, const struct nlmon_config *config) 627 { 628 struct nlmon *nlmon = NULL; 629 struct timeval tv; 630 uint8_t *buf; 631 uint32_t snaplen, len, real_len; 632 633 snaplen = pcap_get_snaplen(pcap); 634 if (snaplen > MAX_SNAPLEN) 635 snaplen = MAX_SNAPLEN; 636 637 buf = malloc(snaplen); 638 if (!buf) { 639 fprintf(stderr, "Failed to allocate packet buffer\n"); 640 return EXIT_FAILURE; 641 } 642 643 nlmon = nlmon_create(0, config); 644 645 while (pcap_read(pcap, &tv, buf, snaplen, &len, &real_len)) { 646 uint16_t arphrd_type; 647 uint16_t proto_type; 648 uint16_t pkt_type; 649 650 if (len < 16) { 651 printf("Too short packet\n"); 652 continue; 653 } 654 655 if (len < real_len) { 656 printf("Packet truncated from %u\n", real_len); 657 continue; 658 } 659 660 pkt_type = l_get_be16(buf); 661 arphrd_type = l_get_be16(buf + 2); 662 proto_type = l_get_be16(buf + 14); 663 664 switch (arphrd_type) { 665 case ARPHRD_ETHER: 666 switch (proto_type) { 667 case ETH_P_PAE: 668 nlmon_print_pae(nlmon, &tv, pkt_type, -1, 669 buf + 16, len - 16); 670 break; 671 } 672 break; 673 case ARPHRD_NETLINK: 674 switch (proto_type) { 675 case NETLINK_ROUTE: 676 nlmon_print_rtnl(nlmon, &tv, 677 buf + 16, len - 16); 678 break; 679 case NETLINK_GENERIC: 680 nlmon_print_genl(nlmon, &tv, 681 buf + 16, len - 16); 682 break; 683 } 684 break; 685 default: 686 printf("Unsupported ARPHRD %u\n", arphrd_type); 687 break; 688 } 689 } 690 691 nlmon_destroy(nlmon); 692 693 free(buf); 694 695 return EXIT_SUCCESS; 696 } 697 698 static void main_loop_quit(struct l_timeout *timeout, void *user_data) 699 { 700 l_main_quit(); 701 } 702 703 static void signal_handler(uint32_t signo, void *user_data) 704 { 705 switch (signo) { 706 case SIGINT: 707 case SIGTERM: 708 iwmon_interface_disable(&monitor_interface); 709 710 timeout = l_timeout_create(1, main_loop_quit, NULL, NULL); 711 break; 712 } 713 } 714 715 static void usage(void) 716 { 717 printf("iwmon - Wireless monitor\n" 718 "Usage:\n"); 719 printf("\tiwmon [options]\n"); 720 printf("Options:\n" 721 "\t-r, --read <file> Read netlink PCAP trace file\n" 722 "\t-w, --write <file> Write netlink PCAP trace file\n" 723 "\t-a, --analyze <file> Analyze netlink PCAP trace file\n" 724 "\t-i, --interface <dev> Use specified netlink monitor\n" 725 "\t-n, --nortnl Don't show RTNL output\n" 726 "\t-y, --nowiphy Don't show 'New Wiphy' output\n" 727 "\t-s, --noscan Don't show scan result output\n" 728 "\t-e, --noies Don't show IEs except SSID\n" 729 "\t-t, --time-format <format> Time format to display. Either\n" 730 "\t\t\t\t 'delta' or 'utc'.\n" 731 "\t-W,--pcap-count Maximum number of PCAP files\n" 732 "\t-C,--pcap-size Maximum size (MB) of PCAP files\n" 733 "\t-h, --help Show help options\n"); 734 } 735 736 static const struct option main_options[] = { 737 { "read", required_argument, NULL, 'r' }, 738 { "write", required_argument, NULL, 'w' }, 739 { "analyze", required_argument, NULL, 'a' }, 740 { "nl80211", required_argument, NULL, 'F' }, 741 { "interface", required_argument, NULL, 'i' }, 742 { "nortnl", no_argument, NULL, 'n' }, 743 { "nowiphy", no_argument, NULL, 'y' }, 744 { "noscan", no_argument, NULL, 's' }, 745 { "noies", no_argument, NULL, 'e' }, 746 { "time-format", required_argument, NULL, 't' }, 747 { "pcap-count", required_argument, NULL, 'W' }, 748 { "pcap-size", required_argument, NULL, 'C' }, 749 { "version", no_argument, NULL, 'v' }, 750 { "help", no_argument, NULL, 'h' }, 751 { } 752 }; 753 754 int main(int argc, char *argv[]) 755 { 756 const char *reader_path = NULL; 757 const char *analyze_path = NULL; 758 const char *ifname = NULL; 759 int exit_status; 760 761 for (;;) { 762 int opt; 763 764 opt = getopt_long(argc, argv, "r:w:a:i:t:W:C:nvhyse", 765 main_options, NULL); 766 if (opt < 0) 767 break; 768 769 switch (opt) { 770 case 'r': 771 reader_path = optarg; 772 config.read_only = true; 773 break; 774 case 'w': 775 writer_path = optarg; 776 break; 777 case 'a': 778 analyze_path = optarg; 779 break; 780 case 'i': 781 ifname = optarg; 782 break; 783 case 'n': 784 config.nortnl = true; 785 break; 786 case 'y': 787 config.nowiphy = true; 788 break; 789 case 's': 790 config.noscan = true; 791 break; 792 case 'e': 793 config.noies = true; 794 break; 795 case 't': 796 if (!strcmp(optarg, "delta")) 797 config.time_format = TIME_FORMAT_DELTA; 798 else if (!strcmp(optarg, "utc")) 799 config.time_format = TIME_FORMAT_UTC; 800 else { 801 printf("Invalid time format '%s'", optarg); 802 return EXIT_FAILURE; 803 } 804 805 break; 806 case 'W': 807 if (l_safe_atou32(optarg, 808 &config.pcap_file_count) < 0 || 809 config.pcap_file_count == 0) { 810 printf("Invalid file count '%s'\n", optarg); 811 return EXIT_FAILURE; 812 } 813 814 break; 815 case 'C': 816 if (l_safe_atou32(optarg, 817 &config.pcap_file_size) < 0 || 818 config.pcap_file_size == 0) { 819 printf("Invalid file size '%s'\n", optarg); 820 return EXIT_FAILURE; 821 } 822 823 break; 824 case 'v': 825 printf("%s\n", VERSION); 826 return EXIT_SUCCESS; 827 case 'h': 828 usage(); 829 return EXIT_SUCCESS; 830 default: 831 return EXIT_FAILURE; 832 } 833 } 834 835 if (argc - optind > 0) { 836 fprintf(stderr, "Invalid command line parameters\n"); 837 return EXIT_FAILURE; 838 } 839 840 if (reader_path && analyze_path) { 841 fprintf(stderr, "Display and analyze can't be combined\n"); 842 return EXIT_FAILURE; 843 } 844 845 if (!l_main_init()) 846 return EXIT_FAILURE; 847 848 printf("Wireless monitor ver %s\n", VERSION); 849 850 if (analyze_path) { 851 exit_status = analyze_pcap(analyze_path); 852 goto done; 853 } 854 855 if (reader_path) { 856 struct pcap *pcap; 857 858 open_pager(); 859 860 pcap = pcap_open(reader_path); 861 if (!pcap) { 862 exit_status = EXIT_FAILURE; 863 goto done; 864 } 865 866 if (pcap_get_type(pcap) != PCAP_TYPE_LINUX_SLL) { 867 fprintf(stderr, "Invalid packet format\n"); 868 exit_status = EXIT_FAILURE; 869 } else 870 exit_status = process_pcap(pcap, &config); 871 872 pcap_close(pcap); 873 874 close_pager(); 875 goto done; 876 } 877 878 monitor_interface.ifname = l_strdup(ifname); 879 iwmon_interface_lookup(&monitor_interface); 880 881 exit_status = l_main_run_with_signal(signal_handler, NULL); 882 883 l_io_destroy(monitor_interface.io); 884 l_netlink_destroy(monitor_interface.rtnl); 885 l_genl_unref(monitor_interface.genl); 886 l_free(monitor_interface.ifname); 887 888 nlmon_close(nlmon); 889 890 done: 891 l_timeout_remove(timeout); 892 893 l_main_exit(); 894 895 return exit_status; 896 }