xref: /freebsd/contrib/wpa/src/drivers/driver_nl80211_monitor.c (revision c1d255d3ffdbe447de3ab875bf4e7d7accc5bfc5)
15b9c547cSRui Paulo /*
25b9c547cSRui Paulo  * Driver interaction with Linux nl80211/cfg80211 - AP monitor interface
35b9c547cSRui Paulo  * Copyright (c) 2002-2014, Jouni Malinen <j@w1.fi>
45b9c547cSRui Paulo  * Copyright (c) 2003-2004, Instant802 Networks, Inc.
55b9c547cSRui Paulo  * Copyright (c) 2005-2006, Devicescape Software, Inc.
65b9c547cSRui Paulo  * Copyright (c) 2007, Johannes Berg <johannes@sipsolutions.net>
75b9c547cSRui Paulo  * Copyright (c) 2009-2010, Atheros Communications
85b9c547cSRui Paulo  *
95b9c547cSRui Paulo  * This software may be distributed under the terms of the BSD license.
105b9c547cSRui Paulo  * See README for more details.
115b9c547cSRui Paulo  */
125b9c547cSRui Paulo 
135b9c547cSRui Paulo #include "includes.h"
145b9c547cSRui Paulo #include <netpacket/packet.h>
155b9c547cSRui Paulo #include <linux/filter.h>
165b9c547cSRui Paulo 
175b9c547cSRui Paulo #include "utils/common.h"
185b9c547cSRui Paulo #include "utils/eloop.h"
195b9c547cSRui Paulo #include "common/ieee802_11_defs.h"
205b9c547cSRui Paulo #include "common/ieee802_11_common.h"
215b9c547cSRui Paulo #include "linux_ioctl.h"
225b9c547cSRui Paulo #include "radiotap_iter.h"
235b9c547cSRui Paulo #include "driver_nl80211.h"
245b9c547cSRui Paulo 
255b9c547cSRui Paulo 
handle_tx_callback(void * ctx,u8 * buf,size_t len,int ok)265b9c547cSRui Paulo static void handle_tx_callback(void *ctx, u8 *buf, size_t len, int ok)
275b9c547cSRui Paulo {
285b9c547cSRui Paulo 	struct ieee80211_hdr *hdr;
295b9c547cSRui Paulo 	u16 fc;
305b9c547cSRui Paulo 	union wpa_event_data event;
315b9c547cSRui Paulo 
325b9c547cSRui Paulo 	hdr = (struct ieee80211_hdr *) buf;
335b9c547cSRui Paulo 	fc = le_to_host16(hdr->frame_control);
345b9c547cSRui Paulo 
355b9c547cSRui Paulo 	os_memset(&event, 0, sizeof(event));
365b9c547cSRui Paulo 	event.tx_status.type = WLAN_FC_GET_TYPE(fc);
375b9c547cSRui Paulo 	event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
385b9c547cSRui Paulo 	event.tx_status.dst = hdr->addr1;
395b9c547cSRui Paulo 	event.tx_status.data = buf;
405b9c547cSRui Paulo 	event.tx_status.data_len = len;
415b9c547cSRui Paulo 	event.tx_status.ack = ok;
425b9c547cSRui Paulo 	wpa_supplicant_event(ctx, EVENT_TX_STATUS, &event);
435b9c547cSRui Paulo }
445b9c547cSRui Paulo 
455b9c547cSRui Paulo 
from_unknown_sta(struct wpa_driver_nl80211_data * drv,u8 * buf,size_t len)465b9c547cSRui Paulo static void from_unknown_sta(struct wpa_driver_nl80211_data *drv,
475b9c547cSRui Paulo 			     u8 *buf, size_t len)
485b9c547cSRui Paulo {
495b9c547cSRui Paulo 	struct ieee80211_hdr *hdr = (void *)buf;
505b9c547cSRui Paulo 	u16 fc;
515b9c547cSRui Paulo 	union wpa_event_data event;
525b9c547cSRui Paulo 
535b9c547cSRui Paulo 	if (len < sizeof(*hdr))
545b9c547cSRui Paulo 		return;
555b9c547cSRui Paulo 
565b9c547cSRui Paulo 	fc = le_to_host16(hdr->frame_control);
575b9c547cSRui Paulo 
585b9c547cSRui Paulo 	os_memset(&event, 0, sizeof(event));
595b9c547cSRui Paulo 	event.rx_from_unknown.bssid = get_hdr_bssid(hdr, len);
605b9c547cSRui Paulo 	event.rx_from_unknown.addr = hdr->addr2;
615b9c547cSRui Paulo 	event.rx_from_unknown.wds = (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) ==
625b9c547cSRui Paulo 		(WLAN_FC_FROMDS | WLAN_FC_TODS);
635b9c547cSRui Paulo 	wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
645b9c547cSRui Paulo }
655b9c547cSRui Paulo 
665b9c547cSRui Paulo 
handle_frame(struct wpa_driver_nl80211_data * drv,u8 * buf,size_t len,int datarate,int ssi_signal)675b9c547cSRui Paulo static void handle_frame(struct wpa_driver_nl80211_data *drv,
685b9c547cSRui Paulo 			 u8 *buf, size_t len, int datarate, int ssi_signal)
695b9c547cSRui Paulo {
705b9c547cSRui Paulo 	struct ieee80211_hdr *hdr;
715b9c547cSRui Paulo 	u16 fc;
725b9c547cSRui Paulo 	union wpa_event_data event;
735b9c547cSRui Paulo 
74*c1d255d3SCy Schubert 	if (!drv->use_monitor)
75*c1d255d3SCy Schubert 		return;
76*c1d255d3SCy Schubert 
775b9c547cSRui Paulo 	hdr = (struct ieee80211_hdr *) buf;
785b9c547cSRui Paulo 	fc = le_to_host16(hdr->frame_control);
795b9c547cSRui Paulo 
805b9c547cSRui Paulo 	switch (WLAN_FC_GET_TYPE(fc)) {
815b9c547cSRui Paulo 	case WLAN_FC_TYPE_MGMT:
825b9c547cSRui Paulo 		os_memset(&event, 0, sizeof(event));
835b9c547cSRui Paulo 		event.rx_mgmt.frame = buf;
845b9c547cSRui Paulo 		event.rx_mgmt.frame_len = len;
855b9c547cSRui Paulo 		event.rx_mgmt.datarate = datarate;
865b9c547cSRui Paulo 		event.rx_mgmt.ssi_signal = ssi_signal;
875b9c547cSRui Paulo 		wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
885b9c547cSRui Paulo 		break;
895b9c547cSRui Paulo 	case WLAN_FC_TYPE_CTRL:
905b9c547cSRui Paulo 		/* can only get here with PS-Poll frames */
915b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "CTRL");
925b9c547cSRui Paulo 		from_unknown_sta(drv, buf, len);
935b9c547cSRui Paulo 		break;
945b9c547cSRui Paulo 	case WLAN_FC_TYPE_DATA:
955b9c547cSRui Paulo 		from_unknown_sta(drv, buf, len);
965b9c547cSRui Paulo 		break;
975b9c547cSRui Paulo 	}
985b9c547cSRui Paulo }
995b9c547cSRui Paulo 
1005b9c547cSRui Paulo 
handle_monitor_read(int sock,void * eloop_ctx,void * sock_ctx)1015b9c547cSRui Paulo static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
1025b9c547cSRui Paulo {
1035b9c547cSRui Paulo 	struct wpa_driver_nl80211_data *drv = eloop_ctx;
1045b9c547cSRui Paulo 	int len;
1055b9c547cSRui Paulo 	unsigned char buf[3000];
1065b9c547cSRui Paulo 	struct ieee80211_radiotap_iterator iter;
1075b9c547cSRui Paulo 	int ret;
1085b9c547cSRui Paulo 	int datarate = 0, ssi_signal = 0;
1095b9c547cSRui Paulo 	int injected = 0, failed = 0, rxflags = 0;
1105b9c547cSRui Paulo 
1115b9c547cSRui Paulo 	len = recv(sock, buf, sizeof(buf), 0);
1125b9c547cSRui Paulo 	if (len < 0) {
1135b9c547cSRui Paulo 		wpa_printf(MSG_ERROR, "nl80211: Monitor socket recv failed: %s",
1145b9c547cSRui Paulo 			   strerror(errno));
1155b9c547cSRui Paulo 		return;
1165b9c547cSRui Paulo 	}
1175b9c547cSRui Paulo 
1185b9c547cSRui Paulo 	if (ieee80211_radiotap_iterator_init(&iter, (void *) buf, len, NULL)) {
1195b9c547cSRui Paulo 		wpa_printf(MSG_INFO, "nl80211: received invalid radiotap frame");
1205b9c547cSRui Paulo 		return;
1215b9c547cSRui Paulo 	}
1225b9c547cSRui Paulo 
1235b9c547cSRui Paulo 	while (1) {
1245b9c547cSRui Paulo 		ret = ieee80211_radiotap_iterator_next(&iter);
1255b9c547cSRui Paulo 		if (ret == -ENOENT)
1265b9c547cSRui Paulo 			break;
1275b9c547cSRui Paulo 		if (ret) {
1285b9c547cSRui Paulo 			wpa_printf(MSG_INFO, "nl80211: received invalid radiotap frame (%d)",
1295b9c547cSRui Paulo 				   ret);
1305b9c547cSRui Paulo 			return;
1315b9c547cSRui Paulo 		}
1325b9c547cSRui Paulo 		switch (iter.this_arg_index) {
1335b9c547cSRui Paulo 		case IEEE80211_RADIOTAP_FLAGS:
1345b9c547cSRui Paulo 			if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
1355b9c547cSRui Paulo 				len -= 4;
1365b9c547cSRui Paulo 			break;
1375b9c547cSRui Paulo 		case IEEE80211_RADIOTAP_RX_FLAGS:
1385b9c547cSRui Paulo 			rxflags = 1;
1395b9c547cSRui Paulo 			break;
1405b9c547cSRui Paulo 		case IEEE80211_RADIOTAP_TX_FLAGS:
1415b9c547cSRui Paulo 			injected = 1;
142780fb4a2SCy Schubert 			failed = le_to_host16((*(le16 *) iter.this_arg)) &
1435b9c547cSRui Paulo 					IEEE80211_RADIOTAP_F_TX_FAIL;
1445b9c547cSRui Paulo 			break;
1455b9c547cSRui Paulo 		case IEEE80211_RADIOTAP_DATA_RETRIES:
1465b9c547cSRui Paulo 			break;
1475b9c547cSRui Paulo 		case IEEE80211_RADIOTAP_CHANNEL:
1485b9c547cSRui Paulo 			/* TODO: convert from freq/flags to channel number */
1495b9c547cSRui Paulo 			break;
1505b9c547cSRui Paulo 		case IEEE80211_RADIOTAP_RATE:
1515b9c547cSRui Paulo 			datarate = *iter.this_arg * 5;
1525b9c547cSRui Paulo 			break;
1535b9c547cSRui Paulo 		case IEEE80211_RADIOTAP_DBM_ANTSIGNAL:
1545b9c547cSRui Paulo 			ssi_signal = (s8) *iter.this_arg;
1555b9c547cSRui Paulo 			break;
1565b9c547cSRui Paulo 		}
1575b9c547cSRui Paulo 	}
1585b9c547cSRui Paulo 
1595b9c547cSRui Paulo 	if (rxflags && injected)
1605b9c547cSRui Paulo 		return;
1615b9c547cSRui Paulo 
1625b9c547cSRui Paulo 	if (!injected)
1635b9c547cSRui Paulo 		handle_frame(drv, buf + iter._max_length,
1645b9c547cSRui Paulo 			     len - iter._max_length, datarate, ssi_signal);
1655b9c547cSRui Paulo 	else
1665b9c547cSRui Paulo 		handle_tx_callback(drv->ctx, buf + iter._max_length,
1675b9c547cSRui Paulo 				   len - iter._max_length, !failed);
1685b9c547cSRui Paulo }
1695b9c547cSRui Paulo 
1705b9c547cSRui Paulo 
1715b9c547cSRui Paulo /*
1725b9c547cSRui Paulo  * we post-process the filter code later and rewrite
1735b9c547cSRui Paulo  * this to the offset to the last instruction
1745b9c547cSRui Paulo  */
1755b9c547cSRui Paulo #define PASS	0xFF
1765b9c547cSRui Paulo #define FAIL	0xFE
1775b9c547cSRui Paulo 
1785b9c547cSRui Paulo static struct sock_filter msock_filter_insns[] = {
1795b9c547cSRui Paulo 	/*
1805b9c547cSRui Paulo 	 * do a little-endian load of the radiotap length field
1815b9c547cSRui Paulo 	 */
1825b9c547cSRui Paulo 	/* load lower byte into A */
1835b9c547cSRui Paulo 	BPF_STMT(BPF_LD  | BPF_B | BPF_ABS, 2),
1845b9c547cSRui Paulo 	/* put it into X (== index register) */
1855b9c547cSRui Paulo 	BPF_STMT(BPF_MISC| BPF_TAX, 0),
1865b9c547cSRui Paulo 	/* load upper byte into A */
1875b9c547cSRui Paulo 	BPF_STMT(BPF_LD  | BPF_B | BPF_ABS, 3),
1885b9c547cSRui Paulo 	/* left-shift it by 8 */
1895b9c547cSRui Paulo 	BPF_STMT(BPF_ALU | BPF_LSH | BPF_K, 8),
1905b9c547cSRui Paulo 	/* or with X */
1915b9c547cSRui Paulo 	BPF_STMT(BPF_ALU | BPF_OR | BPF_X, 0),
1925b9c547cSRui Paulo 	/* put result into X */
1935b9c547cSRui Paulo 	BPF_STMT(BPF_MISC| BPF_TAX, 0),
1945b9c547cSRui Paulo 
1955b9c547cSRui Paulo 	/*
1965b9c547cSRui Paulo 	 * Allow management frames through, this also gives us those
1975b9c547cSRui Paulo 	 * management frames that we sent ourselves with status
1985b9c547cSRui Paulo 	 */
1995b9c547cSRui Paulo 	/* load the lower byte of the IEEE 802.11 frame control field */
2005b9c547cSRui Paulo 	BPF_STMT(BPF_LD  | BPF_B | BPF_IND, 0),
2015b9c547cSRui Paulo 	/* mask off frame type and version */
2025b9c547cSRui Paulo 	BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0xF),
2035b9c547cSRui Paulo 	/* accept frame if it's both 0, fall through otherwise */
2045b9c547cSRui Paulo 	BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, PASS, 0),
2055b9c547cSRui Paulo 
2065b9c547cSRui Paulo 	/*
2075b9c547cSRui Paulo 	 * TODO: add a bit to radiotap RX flags that indicates
2085b9c547cSRui Paulo 	 * that the sending station is not associated, then
2095b9c547cSRui Paulo 	 * add a filter here that filters on our DA and that flag
2105b9c547cSRui Paulo 	 * to allow us to deauth frames to that bad station.
2115b9c547cSRui Paulo 	 *
2125b9c547cSRui Paulo 	 * For now allow all To DS data frames through.
2135b9c547cSRui Paulo 	 */
2145b9c547cSRui Paulo 	/* load the IEEE 802.11 frame control field */
2155b9c547cSRui Paulo 	BPF_STMT(BPF_LD  | BPF_H | BPF_IND, 0),
2165b9c547cSRui Paulo 	/* mask off frame type, version and DS status */
2175b9c547cSRui Paulo 	BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0F03),
2185b9c547cSRui Paulo 	/* accept frame if version 0, type 2 and To DS, fall through otherwise
2195b9c547cSRui Paulo 	 */
2205b9c547cSRui Paulo 	BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0801, PASS, 0),
2215b9c547cSRui Paulo 
2225b9c547cSRui Paulo #if 0
2235b9c547cSRui Paulo 	/*
2245b9c547cSRui Paulo 	 * drop non-data frames
2255b9c547cSRui Paulo 	 */
2265b9c547cSRui Paulo 	/* load the lower byte of the frame control field */
2275b9c547cSRui Paulo 	BPF_STMT(BPF_LD   | BPF_B | BPF_IND, 0),
2285b9c547cSRui Paulo 	/* mask off QoS bit */
2295b9c547cSRui Paulo 	BPF_STMT(BPF_ALU  | BPF_AND | BPF_K, 0x0c),
2305b9c547cSRui Paulo 	/* drop non-data frames */
2315b9c547cSRui Paulo 	BPF_JUMP(BPF_JMP  | BPF_JEQ | BPF_K, 8, 0, FAIL),
2325b9c547cSRui Paulo #endif
2335b9c547cSRui Paulo 	/* load the upper byte of the frame control field */
2345b9c547cSRui Paulo 	BPF_STMT(BPF_LD   | BPF_B | BPF_IND, 1),
2355b9c547cSRui Paulo 	/* mask off toDS/fromDS */
2365b9c547cSRui Paulo 	BPF_STMT(BPF_ALU  | BPF_AND | BPF_K, 0x03),
2375b9c547cSRui Paulo 	/* accept WDS frames */
2385b9c547cSRui Paulo 	BPF_JUMP(BPF_JMP  | BPF_JEQ | BPF_K, 3, PASS, 0),
2395b9c547cSRui Paulo 
2405b9c547cSRui Paulo 	/*
2415b9c547cSRui Paulo 	 * add header length to index
2425b9c547cSRui Paulo 	 */
2435b9c547cSRui Paulo 	/* load the lower byte of the frame control field */
2445b9c547cSRui Paulo 	BPF_STMT(BPF_LD   | BPF_B | BPF_IND, 0),
2455b9c547cSRui Paulo 	/* mask off QoS bit */
2465b9c547cSRui Paulo 	BPF_STMT(BPF_ALU  | BPF_AND | BPF_K, 0x80),
2475b9c547cSRui Paulo 	/* right shift it by 6 to give 0 or 2 */
2485b9c547cSRui Paulo 	BPF_STMT(BPF_ALU  | BPF_RSH | BPF_K, 6),
2495b9c547cSRui Paulo 	/* add data frame header length */
2505b9c547cSRui Paulo 	BPF_STMT(BPF_ALU  | BPF_ADD | BPF_K, 24),
2515b9c547cSRui Paulo 	/* add index, was start of 802.11 header */
2525b9c547cSRui Paulo 	BPF_STMT(BPF_ALU  | BPF_ADD | BPF_X, 0),
2535b9c547cSRui Paulo 	/* move to index, now start of LL header */
2545b9c547cSRui Paulo 	BPF_STMT(BPF_MISC | BPF_TAX, 0),
2555b9c547cSRui Paulo 
2565b9c547cSRui Paulo 	/*
2575b9c547cSRui Paulo 	 * Accept empty data frames, we use those for
2585b9c547cSRui Paulo 	 * polling activity.
2595b9c547cSRui Paulo 	 */
2605b9c547cSRui Paulo 	BPF_STMT(BPF_LD  | BPF_W | BPF_LEN, 0),
2615b9c547cSRui Paulo 	BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_X, 0, PASS, 0),
2625b9c547cSRui Paulo 
2635b9c547cSRui Paulo 	/*
2645b9c547cSRui Paulo 	 * Accept EAPOL frames
2655b9c547cSRui Paulo 	 */
2665b9c547cSRui Paulo 	BPF_STMT(BPF_LD  | BPF_W | BPF_IND, 0),
2675b9c547cSRui Paulo 	BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xAAAA0300, 0, FAIL),
2685b9c547cSRui Paulo 	BPF_STMT(BPF_LD  | BPF_W | BPF_IND, 4),
2695b9c547cSRui Paulo 	BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0000888E, PASS, FAIL),
2705b9c547cSRui Paulo 
2715b9c547cSRui Paulo 	/* keep these last two statements or change the code below */
2725b9c547cSRui Paulo 	/* return 0 == "DROP" */
2735b9c547cSRui Paulo 	BPF_STMT(BPF_RET | BPF_K, 0),
2745b9c547cSRui Paulo 	/* return ~0 == "keep all" */
2755b9c547cSRui Paulo 	BPF_STMT(BPF_RET | BPF_K, ~0),
2765b9c547cSRui Paulo };
2775b9c547cSRui Paulo 
2785b9c547cSRui Paulo static struct sock_fprog msock_filter = {
2795b9c547cSRui Paulo 	.len = ARRAY_SIZE(msock_filter_insns),
2805b9c547cSRui Paulo 	.filter = msock_filter_insns,
2815b9c547cSRui Paulo };
2825b9c547cSRui Paulo 
2835b9c547cSRui Paulo 
add_monitor_filter(int s)2845b9c547cSRui Paulo static int add_monitor_filter(int s)
2855b9c547cSRui Paulo {
2865b9c547cSRui Paulo 	int idx;
2875b9c547cSRui Paulo 
2885b9c547cSRui Paulo 	/* rewrite all PASS/FAIL jump offsets */
2895b9c547cSRui Paulo 	for (idx = 0; idx < msock_filter.len; idx++) {
2905b9c547cSRui Paulo 		struct sock_filter *insn = &msock_filter_insns[idx];
2915b9c547cSRui Paulo 
2925b9c547cSRui Paulo 		if (BPF_CLASS(insn->code) == BPF_JMP) {
2935b9c547cSRui Paulo 			if (insn->code == (BPF_JMP|BPF_JA)) {
2945b9c547cSRui Paulo 				if (insn->k == PASS)
2955b9c547cSRui Paulo 					insn->k = msock_filter.len - idx - 2;
2965b9c547cSRui Paulo 				else if (insn->k == FAIL)
2975b9c547cSRui Paulo 					insn->k = msock_filter.len - idx - 3;
2985b9c547cSRui Paulo 			}
2995b9c547cSRui Paulo 
3005b9c547cSRui Paulo 			if (insn->jt == PASS)
3015b9c547cSRui Paulo 				insn->jt = msock_filter.len - idx - 2;
3025b9c547cSRui Paulo 			else if (insn->jt == FAIL)
3035b9c547cSRui Paulo 				insn->jt = msock_filter.len - idx - 3;
3045b9c547cSRui Paulo 
3055b9c547cSRui Paulo 			if (insn->jf == PASS)
3065b9c547cSRui Paulo 				insn->jf = msock_filter.len - idx - 2;
3075b9c547cSRui Paulo 			else if (insn->jf == FAIL)
3085b9c547cSRui Paulo 				insn->jf = msock_filter.len - idx - 3;
3095b9c547cSRui Paulo 		}
3105b9c547cSRui Paulo 	}
3115b9c547cSRui Paulo 
3125b9c547cSRui Paulo 	if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER,
3135b9c547cSRui Paulo 		       &msock_filter, sizeof(msock_filter))) {
3145b9c547cSRui Paulo 		wpa_printf(MSG_ERROR, "nl80211: setsockopt(SO_ATTACH_FILTER) failed: %s",
3155b9c547cSRui Paulo 			   strerror(errno));
3165b9c547cSRui Paulo 		return -1;
3175b9c547cSRui Paulo 	}
3185b9c547cSRui Paulo 
3195b9c547cSRui Paulo 	return 0;
3205b9c547cSRui Paulo }
3215b9c547cSRui Paulo 
3225b9c547cSRui Paulo 
nl80211_remove_monitor_interface(struct wpa_driver_nl80211_data * drv)3235b9c547cSRui Paulo void nl80211_remove_monitor_interface(struct wpa_driver_nl80211_data *drv)
3245b9c547cSRui Paulo {
3255b9c547cSRui Paulo 	if (drv->monitor_refcount > 0)
3265b9c547cSRui Paulo 		drv->monitor_refcount--;
3275b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "nl80211: Remove monitor interface: refcount=%d",
3285b9c547cSRui Paulo 		   drv->monitor_refcount);
3295b9c547cSRui Paulo 	if (drv->monitor_refcount > 0)
3305b9c547cSRui Paulo 		return;
3315b9c547cSRui Paulo 
3325b9c547cSRui Paulo 	if (drv->monitor_ifidx >= 0) {
3335b9c547cSRui Paulo 		nl80211_remove_iface(drv, drv->monitor_ifidx);
3345b9c547cSRui Paulo 		drv->monitor_ifidx = -1;
3355b9c547cSRui Paulo 	}
3365b9c547cSRui Paulo 	if (drv->monitor_sock >= 0) {
3375b9c547cSRui Paulo 		eloop_unregister_read_sock(drv->monitor_sock);
3385b9c547cSRui Paulo 		close(drv->monitor_sock);
3395b9c547cSRui Paulo 		drv->monitor_sock = -1;
3405b9c547cSRui Paulo 	}
3415b9c547cSRui Paulo }
3425b9c547cSRui Paulo 
3435b9c547cSRui Paulo 
nl80211_create_monitor_interface(struct wpa_driver_nl80211_data * drv)3445b9c547cSRui Paulo int nl80211_create_monitor_interface(struct wpa_driver_nl80211_data *drv)
3455b9c547cSRui Paulo {
3465b9c547cSRui Paulo 	char buf[IFNAMSIZ];
3475b9c547cSRui Paulo 	struct sockaddr_ll ll;
3485b9c547cSRui Paulo 	int optval;
3495b9c547cSRui Paulo 	socklen_t optlen;
3505b9c547cSRui Paulo 
3515b9c547cSRui Paulo 	if (drv->monitor_ifidx >= 0) {
3525b9c547cSRui Paulo 		drv->monitor_refcount++;
3535b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "nl80211: Re-use existing monitor interface: refcount=%d",
3545b9c547cSRui Paulo 			   drv->monitor_refcount);
3555b9c547cSRui Paulo 		return 0;
3565b9c547cSRui Paulo 	}
3575b9c547cSRui Paulo 
3585b9c547cSRui Paulo 	if (os_strncmp(drv->first_bss->ifname, "p2p-", 4) == 0) {
3595b9c547cSRui Paulo 		/*
3605b9c547cSRui Paulo 		 * P2P interface name is of the format p2p-%s-%d. For monitor
3615b9c547cSRui Paulo 		 * interface name corresponding to P2P GO, replace "p2p-" with
3625b9c547cSRui Paulo 		 * "mon-" to retain the same interface name length and to
3635b9c547cSRui Paulo 		 * indicate that it is a monitor interface.
3645b9c547cSRui Paulo 		 */
3655b9c547cSRui Paulo 		snprintf(buf, IFNAMSIZ, "mon-%s", drv->first_bss->ifname + 4);
3665b9c547cSRui Paulo 	} else {
36785732ac8SCy Schubert 		int ret;
36885732ac8SCy Schubert 
3695b9c547cSRui Paulo 		/* Non-P2P interface with AP functionality. */
37085732ac8SCy Schubert 		ret = os_snprintf(buf, IFNAMSIZ, "mon.%s",
37185732ac8SCy Schubert 				  drv->first_bss->ifname);
37285732ac8SCy Schubert 		if (ret >= (int) sizeof(buf))
37385732ac8SCy Schubert 			wpa_printf(MSG_DEBUG,
37485732ac8SCy Schubert 				   "nl80211: Monitor interface name has been truncated to %s",
37585732ac8SCy Schubert 				   buf);
37685732ac8SCy Schubert 		else if (ret < 0)
37785732ac8SCy Schubert 			return ret;
3785b9c547cSRui Paulo 	}
3795b9c547cSRui Paulo 
3805b9c547cSRui Paulo 	buf[IFNAMSIZ - 1] = '\0';
3815b9c547cSRui Paulo 
3825b9c547cSRui Paulo 	drv->monitor_ifidx =
3835b9c547cSRui Paulo 		nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL,
3845b9c547cSRui Paulo 				     0, NULL, NULL, 0);
3855b9c547cSRui Paulo 
3865b9c547cSRui Paulo 	if (drv->monitor_ifidx == -EOPNOTSUPP) {
3875b9c547cSRui Paulo 		/*
3885b9c547cSRui Paulo 		 * This is backward compatibility for a few versions of
3895b9c547cSRui Paulo 		 * the kernel only that didn't advertise the right
3905b9c547cSRui Paulo 		 * attributes for the only driver that then supported
3915b9c547cSRui Paulo 		 * AP mode w/o monitor -- ath6kl.
3925b9c547cSRui Paulo 		 */
3935b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "nl80211: Driver does not support "
3945b9c547cSRui Paulo 			   "monitor interface type - try to run without it");
3955b9c547cSRui Paulo 		drv->device_ap_sme = 1;
3965b9c547cSRui Paulo 	}
3975b9c547cSRui Paulo 
3985b9c547cSRui Paulo 	if (drv->monitor_ifidx < 0)
3995b9c547cSRui Paulo 		return -1;
4005b9c547cSRui Paulo 
4015b9c547cSRui Paulo 	if (linux_set_iface_flags(drv->global->ioctl_sock, buf, 1))
4025b9c547cSRui Paulo 		goto error;
4035b9c547cSRui Paulo 
4045b9c547cSRui Paulo 	memset(&ll, 0, sizeof(ll));
4055b9c547cSRui Paulo 	ll.sll_family = AF_PACKET;
4065b9c547cSRui Paulo 	ll.sll_ifindex = drv->monitor_ifidx;
4075b9c547cSRui Paulo 	drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
4085b9c547cSRui Paulo 	if (drv->monitor_sock < 0) {
4095b9c547cSRui Paulo 		wpa_printf(MSG_ERROR, "nl80211: socket[PF_PACKET,SOCK_RAW] failed: %s",
4105b9c547cSRui Paulo 			   strerror(errno));
4115b9c547cSRui Paulo 		goto error;
4125b9c547cSRui Paulo 	}
4135b9c547cSRui Paulo 
4145b9c547cSRui Paulo 	if (add_monitor_filter(drv->monitor_sock)) {
4155b9c547cSRui Paulo 		wpa_printf(MSG_INFO, "Failed to set socket filter for monitor "
4165b9c547cSRui Paulo 			   "interface; do filtering in user space");
4175b9c547cSRui Paulo 		/* This works, but will cost in performance. */
4185b9c547cSRui Paulo 	}
4195b9c547cSRui Paulo 
4205b9c547cSRui Paulo 	if (bind(drv->monitor_sock, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
4215b9c547cSRui Paulo 		wpa_printf(MSG_ERROR, "nl80211: monitor socket bind failed: %s",
4225b9c547cSRui Paulo 			   strerror(errno));
4235b9c547cSRui Paulo 		goto error;
4245b9c547cSRui Paulo 	}
4255b9c547cSRui Paulo 
4265b9c547cSRui Paulo 	optlen = sizeof(optval);
4275b9c547cSRui Paulo 	optval = 20;
4285b9c547cSRui Paulo 	if (setsockopt
4295b9c547cSRui Paulo 	    (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
4305b9c547cSRui Paulo 		wpa_printf(MSG_ERROR, "nl80211: Failed to set socket priority: %s",
4315b9c547cSRui Paulo 			   strerror(errno));
4325b9c547cSRui Paulo 		goto error;
4335b9c547cSRui Paulo 	}
4345b9c547cSRui Paulo 
4355b9c547cSRui Paulo 	if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
4365b9c547cSRui Paulo 				     drv, NULL)) {
4375b9c547cSRui Paulo 		wpa_printf(MSG_INFO, "nl80211: Could not register monitor read socket");
4385b9c547cSRui Paulo 		goto error;
4395b9c547cSRui Paulo 	}
4405b9c547cSRui Paulo 
4415b9c547cSRui Paulo 	drv->monitor_refcount++;
4425b9c547cSRui Paulo 	return 0;
4435b9c547cSRui Paulo  error:
4445b9c547cSRui Paulo 	nl80211_remove_monitor_interface(drv);
4455b9c547cSRui Paulo 	return -1;
4465b9c547cSRui Paulo }
4475b9c547cSRui Paulo 
4485b9c547cSRui Paulo 
nl80211_send_monitor(struct wpa_driver_nl80211_data * drv,const void * data,size_t len,int encrypt,int noack)4495b9c547cSRui Paulo int nl80211_send_monitor(struct wpa_driver_nl80211_data *drv,
4505b9c547cSRui Paulo 			 const void *data, size_t len,
4515b9c547cSRui Paulo 			 int encrypt, int noack)
4525b9c547cSRui Paulo {
4535b9c547cSRui Paulo 	__u8 rtap_hdr[] = {
4545b9c547cSRui Paulo 		0x00, 0x00, /* radiotap version */
4555b9c547cSRui Paulo 		0x0e, 0x00, /* radiotap length */
4565b9c547cSRui Paulo 		0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
4575b9c547cSRui Paulo 		IEEE80211_RADIOTAP_F_FRAG, /* F_FRAG (fragment if required) */
4585b9c547cSRui Paulo 		0x00,       /* padding */
4595b9c547cSRui Paulo 		0x00, 0x00, /* RX and TX flags to indicate that */
4605b9c547cSRui Paulo 		0x00, 0x00, /* this is the injected frame directly */
4615b9c547cSRui Paulo 	};
4625b9c547cSRui Paulo 	struct iovec iov[2] = {
4635b9c547cSRui Paulo 		{
4645b9c547cSRui Paulo 			.iov_base = &rtap_hdr,
4655b9c547cSRui Paulo 			.iov_len = sizeof(rtap_hdr),
4665b9c547cSRui Paulo 		},
4675b9c547cSRui Paulo 		{
4685b9c547cSRui Paulo 			.iov_base = (void *) data,
4695b9c547cSRui Paulo 			.iov_len = len,
4705b9c547cSRui Paulo 		}
4715b9c547cSRui Paulo 	};
4725b9c547cSRui Paulo 	struct msghdr msg = {
4735b9c547cSRui Paulo 		.msg_name = NULL,
4745b9c547cSRui Paulo 		.msg_namelen = 0,
4755b9c547cSRui Paulo 		.msg_iov = iov,
4765b9c547cSRui Paulo 		.msg_iovlen = 2,
4775b9c547cSRui Paulo 		.msg_control = NULL,
4785b9c547cSRui Paulo 		.msg_controllen = 0,
4795b9c547cSRui Paulo 		.msg_flags = 0,
4805b9c547cSRui Paulo 	};
4815b9c547cSRui Paulo 	int res;
4825b9c547cSRui Paulo 	u16 txflags = 0;
4835b9c547cSRui Paulo 
4845b9c547cSRui Paulo 	if (encrypt)
4855b9c547cSRui Paulo 		rtap_hdr[8] |= IEEE80211_RADIOTAP_F_WEP;
4865b9c547cSRui Paulo 
4875b9c547cSRui Paulo 	if (drv->monitor_sock < 0) {
4885b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "nl80211: No monitor socket available "
4895b9c547cSRui Paulo 			   "for %s", __func__);
4905b9c547cSRui Paulo 		return -1;
4915b9c547cSRui Paulo 	}
4925b9c547cSRui Paulo 
4935b9c547cSRui Paulo 	if (noack)
4945b9c547cSRui Paulo 		txflags |= IEEE80211_RADIOTAP_F_TX_NOACK;
4955b9c547cSRui Paulo 	WPA_PUT_LE16(&rtap_hdr[12], txflags);
4965b9c547cSRui Paulo 
4975b9c547cSRui Paulo 	res = sendmsg(drv->monitor_sock, &msg, 0);
4985b9c547cSRui Paulo 	if (res < 0) {
4995b9c547cSRui Paulo 		wpa_printf(MSG_INFO, "nl80211: sendmsg: %s", strerror(errno));
5005b9c547cSRui Paulo 		return -1;
5015b9c547cSRui Paulo 	}
5025b9c547cSRui Paulo 	return 0;
5035b9c547cSRui Paulo }
504