2 * Linux packet socket monitor
3 * Copyright (c) 2010, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
15 #include "utils/includes.h"
17 #include <netpacket/packet.h>
19 #include "utils/common.h"
20 #include "utils/eloop.h"
24 static void monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
26 struct wlantest *wt = eloop_ctx;
30 len = recv(sock, buf, sizeof(buf), 0);
32 wpa_printf(MSG_INFO, "recv(PACKET): %s", strerror(errno));
36 wlantest_process(wt, buf, len);
40 int monitor_init(struct wlantest *wt, const char *ifname)
42 struct sockaddr_ll ll;
44 os_memset(&ll, 0, sizeof(ll));
45 ll.sll_family = AF_PACKET;
46 ll.sll_ifindex = if_nametoindex(ifname);
47 if (ll.sll_ifindex == 0) {
48 wpa_printf(MSG_ERROR, "Monitor interface '%s' does not exist",
53 wt->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
54 if (wt->monitor_sock < 0) {
55 wpa_printf(MSG_ERROR, "socket(PF_PACKET,SOCK_RAW): %s",
60 if (bind(wt->monitor_sock, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
61 wpa_printf(MSG_ERROR, "bind(PACKET): %s", strerror(errno));
62 close(wt->monitor_sock);
63 wt->monitor_sock = -1;
67 if (eloop_register_read_sock(wt->monitor_sock, monitor_read, wt, NULL))
69 wpa_printf(MSG_ERROR, "Could not register monitor read "
71 close(wt->monitor_sock);
72 wt->monitor_sock = -1;
80 void monitor_deinit(struct wlantest *wt)
82 if (wt->monitor_sock >= 0) {
83 eloop_unregister_read_sock(wt->monitor_sock);
84 close(wt->monitor_sock);
85 wt->monitor_sock = -1;