2014-08-03 06:03:53 +02:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Wireless daemon for Linux
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013-2014 Intel Corporation. All rights reserved.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; 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 <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2014-08-05 21:39:54 +02:00
|
|
|
#include <getopt.h>
|
2014-08-05 22:39:33 +02:00
|
|
|
#include <sys/socket.h>
|
2014-08-03 06:03:53 +02:00
|
|
|
#include <linux/genetlink.h>
|
2014-08-05 22:39:33 +02:00
|
|
|
#include <linux/if_arp.h>
|
2014-08-03 06:03:53 +02:00
|
|
|
#include <ell/ell.h>
|
|
|
|
|
|
|
|
#include "linux/nl80211.h"
|
|
|
|
#include "monitor/nlmon.h"
|
2014-08-05 22:39:33 +02:00
|
|
|
#include "monitor/pcap.h"
|
2014-08-03 06:03:53 +02:00
|
|
|
|
|
|
|
static struct nlmon *nlmon = NULL;
|
|
|
|
|
|
|
|
#define NLA_OK(nla,len) ((len) >= (int) sizeof(struct nlattr) && \
|
|
|
|
(nla)->nla_len >= sizeof(struct nlattr) && \
|
|
|
|
(nla)->nla_len <= (len))
|
|
|
|
#define NLA_NEXT(nla,attrlen) ((attrlen) -= NLA_ALIGN((nla)->nla_len), \
|
|
|
|
(struct nlattr*)(((char*)(nla)) + \
|
|
|
|
NLA_ALIGN((nla)->nla_len)))
|
|
|
|
|
|
|
|
#define NLA_LENGTH(len) (NLA_ALIGN(sizeof(struct nlattr)) + (len))
|
|
|
|
#define NLA_DATA(nla) ((void*)(((char*)(nla)) + NLA_LENGTH(0)))
|
|
|
|
#define NLA_PAYLOAD(nla) ((int)((nla)->nla_len - NLA_LENGTH(0)))
|
|
|
|
|
2014-08-05 21:39:54 +02:00
|
|
|
static void genl_parse(uint16_t type, const void *data, uint32_t len,
|
|
|
|
const char *ifname)
|
2014-08-03 06:03:53 +02:00
|
|
|
{
|
|
|
|
const struct genlmsghdr *genlmsg = data;
|
|
|
|
const struct nlattr *nla;
|
|
|
|
char name[GENL_NAMSIZ];
|
|
|
|
uint16_t id = GENL_ID_GENERATE;
|
|
|
|
|
|
|
|
if (nlmon)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (type != GENL_ID_CTRL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (genlmsg->cmd != CTRL_CMD_NEWFAMILY)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (nla = data + GENL_HDRLEN; NLA_OK(nla, len);
|
|
|
|
nla = NLA_NEXT(nla, len)) {
|
|
|
|
switch (nla->nla_type & NLA_TYPE_MASK) {
|
|
|
|
case CTRL_ATTR_FAMILY_ID:
|
|
|
|
id = *((uint16_t *) NLA_DATA(nla));
|
|
|
|
break;
|
|
|
|
case CTRL_ATTR_FAMILY_NAME:
|
|
|
|
strncpy(name, NLA_DATA(nla), GENL_NAMSIZ);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (id == GENL_ID_GENERATE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!strcmp(name, NL80211_GENL_NAME))
|
2014-08-05 21:39:54 +02:00
|
|
|
nlmon = nlmon_open(ifname, id);
|
2014-08-03 06:03:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void genl_notify(uint16_t type, const void *data,
|
|
|
|
uint32_t len, void *user_data)
|
|
|
|
{
|
2014-08-05 21:39:54 +02:00
|
|
|
const char *ifname = user_data;
|
|
|
|
|
|
|
|
genl_parse(type, data, len, ifname);
|
2014-08-03 06:03:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void genl_callback(int error, uint16_t type, const void *data,
|
|
|
|
uint32_t len, void *user_data)
|
|
|
|
{
|
2014-08-05 21:39:54 +02:00
|
|
|
const char *ifname = user_data;
|
|
|
|
|
2014-08-03 06:03:53 +02:00
|
|
|
if (error < 0) {
|
|
|
|
fprintf(stderr, "Failed to lookup nl80211 family\n");
|
|
|
|
l_main_quit();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-08-05 21:39:54 +02:00
|
|
|
genl_parse(type, data, len, ifname);
|
2014-08-03 06:03:53 +02:00
|
|
|
}
|
|
|
|
|
2014-08-05 21:39:54 +02:00
|
|
|
static struct l_netlink *genl_lookup(const char *ifname)
|
2014-08-03 06:03:53 +02:00
|
|
|
{
|
|
|
|
struct l_netlink *genl;
|
|
|
|
char buf[GENL_HDRLEN + NLA_HDRLEN + GENL_NAMSIZ];
|
|
|
|
struct genlmsghdr *genlmsg;
|
|
|
|
struct nlattr *nla;
|
|
|
|
|
|
|
|
genl = l_netlink_new(NETLINK_GENERIC);
|
|
|
|
|
|
|
|
l_netlink_register(genl, GENL_ID_CTRL, genl_notify, NULL, NULL);
|
|
|
|
|
|
|
|
genlmsg = (struct genlmsghdr *) buf;
|
|
|
|
genlmsg->cmd = CTRL_CMD_GETFAMILY;
|
|
|
|
genlmsg->version = 0;
|
|
|
|
genlmsg->reserved = 0;
|
|
|
|
|
|
|
|
nla = (struct nlattr *) (buf + GENL_HDRLEN);
|
|
|
|
nla->nla_len = NLA_HDRLEN + GENL_NAMSIZ;
|
|
|
|
nla->nla_type = CTRL_ATTR_FAMILY_NAME;
|
|
|
|
strncpy(buf + GENL_HDRLEN + NLA_HDRLEN,
|
|
|
|
NL80211_GENL_NAME, GENL_NAMSIZ);
|
|
|
|
|
|
|
|
l_netlink_send(genl, GENL_ID_CTRL, 0, buf, sizeof(buf),
|
2014-08-05 21:39:54 +02:00
|
|
|
genl_callback, (char *) ifname, NULL);
|
2014-08-03 06:03:53 +02:00
|
|
|
|
|
|
|
return genl;
|
|
|
|
}
|
|
|
|
|
2014-08-05 22:39:33 +02:00
|
|
|
static int process_pcap(struct pcap *pcap)
|
|
|
|
{
|
|
|
|
struct nlmon *nlmon = NULL;
|
|
|
|
uint8_t buf[8192];
|
|
|
|
uint32_t len;
|
|
|
|
|
|
|
|
nlmon = nlmon_create();
|
|
|
|
|
|
|
|
while (pcap_read(pcap, NULL, buf, sizeof(buf), &len)) {
|
|
|
|
uint16_t arphrd_type;
|
2014-08-06 00:12:20 +02:00
|
|
|
uint16_t proto_type;
|
2014-08-05 22:39:33 +02:00
|
|
|
|
|
|
|
if (len < 16) {
|
2014-08-06 06:44:15 +02:00
|
|
|
printf("Too short packet\n");
|
|
|
|
continue;
|
2014-08-05 22:39:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
arphrd_type = L_GET_UNALIGNED((const uint16_t *) (buf + 2));
|
|
|
|
|
|
|
|
if (L_BE16_TO_CPU(arphrd_type) != ARPHRD_NETLINK) {
|
2014-08-06 06:44:15 +02:00
|
|
|
printf("Unsupported ARPHRD %u\n",
|
|
|
|
L_BE16_TO_CPU(arphrd_type));
|
|
|
|
continue;
|
2014-08-05 22:39:33 +02:00
|
|
|
}
|
|
|
|
|
2014-08-06 00:12:20 +02:00
|
|
|
proto_type = L_GET_UNALIGNED((const uint16_t *) (buf + 14));
|
|
|
|
|
|
|
|
switch (L_BE16_TO_CPU(proto_type)) {
|
2014-08-06 06:44:15 +02:00
|
|
|
case NETLINK_ROUTE:
|
|
|
|
printf("RTNL packet\n");
|
|
|
|
break;
|
2014-08-06 00:12:20 +02:00
|
|
|
case NETLINK_GENERIC:
|
|
|
|
nlmon_print(nlmon, buf + 16, len - 16);
|
|
|
|
break;
|
2014-08-06 06:44:15 +02:00
|
|
|
default:
|
|
|
|
printf("Other protocol %u\n",
|
|
|
|
L_BE16_TO_CPU(proto_type));
|
|
|
|
break;
|
2014-08-06 00:12:20 +02:00
|
|
|
}
|
2014-08-05 22:39:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
nlmon_destroy(nlmon);
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2014-08-03 06:03:53 +02:00
|
|
|
static void signal_handler(struct l_signal *signal, uint32_t signo,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
switch (signo) {
|
|
|
|
case SIGINT:
|
|
|
|
case SIGTERM:
|
|
|
|
l_main_quit();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-05 21:39:54 +02:00
|
|
|
static void usage(void)
|
|
|
|
{
|
|
|
|
printf("iwmon - Wireless monitor\n"
|
|
|
|
"Usage:\n");
|
|
|
|
printf("\tiwmon [options]\n");
|
|
|
|
printf("options:\n"
|
2014-08-05 22:39:33 +02:00
|
|
|
"\t-r, --read <file> Read netlink PCAP trace files\n"
|
2014-08-05 21:39:54 +02:00
|
|
|
"\t-i, --interface <dev> Use specified netlink monitor\n"
|
|
|
|
"\t-h, --help Show help options\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct option main_options[] = {
|
2014-08-05 22:39:33 +02:00
|
|
|
{ "read", required_argument, NULL, 'r' },
|
2014-08-05 21:39:54 +02:00
|
|
|
{ "interface", required_argument, NULL, 'i' },
|
|
|
|
{ "version", no_argument, NULL, 'v' },
|
|
|
|
{ "help", no_argument, NULL, 'h' },
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
2014-08-03 06:03:53 +02:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2014-08-05 22:39:33 +02:00
|
|
|
const char *reader_path = NULL;
|
2014-08-05 21:39:54 +02:00
|
|
|
const char *ifname = "nlmon";
|
2014-08-03 06:03:53 +02:00
|
|
|
struct l_signal *signal;
|
|
|
|
struct l_netlink *genl;
|
|
|
|
sigset_t mask;
|
|
|
|
int exit_status;
|
|
|
|
|
2014-08-05 21:39:54 +02:00
|
|
|
for (;;) {
|
|
|
|
int opt;
|
|
|
|
|
2014-08-05 22:39:33 +02:00
|
|
|
opt = getopt_long(argc, argv, "r:i:vh", main_options, NULL);
|
2014-08-05 21:39:54 +02:00
|
|
|
if (opt < 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
switch (opt) {
|
2014-08-05 22:39:33 +02:00
|
|
|
case 'r':
|
|
|
|
reader_path = optarg;
|
|
|
|
break;
|
2014-08-05 21:39:54 +02:00
|
|
|
case 'i':
|
|
|
|
ifname = optarg;
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
printf("%s\n", VERSION);
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
case 'h':
|
|
|
|
usage();
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
default:
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc - optind > 0) {
|
|
|
|
fprintf(stderr, "Invalid command line parameters\n");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2014-08-03 06:03:53 +02:00
|
|
|
sigemptyset(&mask);
|
|
|
|
sigaddset(&mask, SIGINT);
|
|
|
|
sigaddset(&mask, SIGTERM);
|
|
|
|
|
|
|
|
signal = l_signal_create(&mask, signal_handler, NULL, NULL);
|
|
|
|
|
2014-08-07 01:01:28 +02:00
|
|
|
printf("Wireless monitor ver %s\n", VERSION);
|
|
|
|
|
2014-08-05 22:39:33 +02:00
|
|
|
if (reader_path) {
|
|
|
|
struct pcap *pcap;
|
|
|
|
|
|
|
|
pcap = pcap_open(reader_path);
|
|
|
|
if (!pcap) {
|
|
|
|
exit_status = EXIT_FAILURE;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pcap_get_type(pcap) != PCAP_TYPE_LINUX_SLL) {
|
|
|
|
fprintf(stderr, "Invalid packet format\n");
|
|
|
|
exit_status = EXIT_FAILURE;
|
|
|
|
} else
|
|
|
|
exit_status = process_pcap(pcap);
|
|
|
|
|
|
|
|
pcap_close(pcap);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2014-08-05 21:39:54 +02:00
|
|
|
genl = genl_lookup(ifname);
|
2014-08-03 06:03:53 +02:00
|
|
|
|
|
|
|
l_main_run();
|
|
|
|
|
|
|
|
l_netlink_destroy(genl);
|
|
|
|
nlmon_close(nlmon);
|
|
|
|
|
|
|
|
exit_status = EXIT_SUCCESS;
|
|
|
|
|
2014-08-05 22:39:33 +02:00
|
|
|
done:
|
2014-08-03 06:03:53 +02:00
|
|
|
l_signal_remove(signal);
|
|
|
|
|
|
|
|
return exit_status;
|
|
|
|
}
|