1c1d255d3SCy Schubert /*
2c1d255d3SCy Schubert * WPA Supplicant - Layer2 packet handling with Linux packet sockets
3c1d255d3SCy Schubert * Copyright (c) 2003-2015, Jouni Malinen <j@w1.fi>
4c1d255d3SCy Schubert *
5c1d255d3SCy Schubert * This software may be distributed under the terms of the BSD license.
6c1d255d3SCy Schubert * See README for more details.
7c1d255d3SCy Schubert */
8c1d255d3SCy Schubert
9c1d255d3SCy Schubert #include "includes.h"
10c1d255d3SCy Schubert #include <sys/ioctl.h>
11c1d255d3SCy Schubert #include <netpacket/packet.h>
12c1d255d3SCy Schubert #include <net/if.h>
13c1d255d3SCy Schubert #include <linux/filter.h>
14c1d255d3SCy Schubert
15c1d255d3SCy Schubert #include "common.h"
16c1d255d3SCy Schubert #include "eloop.h"
17c1d255d3SCy Schubert #include "crypto/sha1.h"
18c1d255d3SCy Schubert #include "crypto/crypto.h"
19c1d255d3SCy Schubert #include "l2_packet.h"
20c1d255d3SCy Schubert
21c1d255d3SCy Schubert
22c1d255d3SCy Schubert struct l2_packet_data {
23c1d255d3SCy Schubert int fd; /* packet socket for EAPOL frames */
24c1d255d3SCy Schubert char ifname[IFNAMSIZ + 1];
25c1d255d3SCy Schubert int ifindex;
26c1d255d3SCy Schubert u8 own_addr[ETH_ALEN];
27c1d255d3SCy Schubert void (*rx_callback)(void *ctx, const u8 *src_addr,
28c1d255d3SCy Schubert const u8 *buf, size_t len);
29c1d255d3SCy Schubert void *rx_callback_ctx;
30c1d255d3SCy Schubert int l2_hdr; /* whether to include layer 2 (Ethernet) header data
31c1d255d3SCy Schubert * buffers */
32c1d255d3SCy Schubert
33c1d255d3SCy Schubert #ifndef CONFIG_NO_LINUX_PACKET_SOCKET_WAR
34c1d255d3SCy Schubert /* For working around Linux packet socket behavior and regression. */
35c1d255d3SCy Schubert int fd_br_rx;
36c1d255d3SCy Schubert int last_from_br, last_from_br_prev;
37c1d255d3SCy Schubert u8 last_hash[SHA1_MAC_LEN];
38c1d255d3SCy Schubert u8 last_hash_prev[SHA1_MAC_LEN];
39c1d255d3SCy Schubert unsigned int num_rx_br;
40c1d255d3SCy Schubert #endif /* CONFIG_NO_LINUX_PACKET_SOCKET_WAR */
41c1d255d3SCy Schubert };
42c1d255d3SCy Schubert
43c1d255d3SCy Schubert /* Generated by 'sudo tcpdump -s 3000 -dd greater 278 and ip and udp and
44c1d255d3SCy Schubert * src port bootps and dst port bootpc'
45c1d255d3SCy Schubert */
46c1d255d3SCy Schubert static struct sock_filter dhcp_sock_filter_insns[] = {
47c1d255d3SCy Schubert { 0x80, 0, 0, 0x00000000 },
48c1d255d3SCy Schubert { 0x35, 0, 12, 0x00000116 },
49c1d255d3SCy Schubert { 0x28, 0, 0, 0x0000000c },
50c1d255d3SCy Schubert { 0x15, 0, 10, 0x00000800 },
51c1d255d3SCy Schubert { 0x30, 0, 0, 0x00000017 },
52c1d255d3SCy Schubert { 0x15, 0, 8, 0x00000011 },
53c1d255d3SCy Schubert { 0x28, 0, 0, 0x00000014 },
54c1d255d3SCy Schubert { 0x45, 6, 0, 0x00001fff },
55c1d255d3SCy Schubert { 0xb1, 0, 0, 0x0000000e },
56c1d255d3SCy Schubert { 0x48, 0, 0, 0x0000000e },
57c1d255d3SCy Schubert { 0x15, 0, 3, 0x00000043 },
58c1d255d3SCy Schubert { 0x48, 0, 0, 0x00000010 },
59c1d255d3SCy Schubert { 0x15, 0, 1, 0x00000044 },
60c1d255d3SCy Schubert { 0x6, 0, 0, 0x00000bb8 },
61c1d255d3SCy Schubert { 0x6, 0, 0, 0x00000000 },
62c1d255d3SCy Schubert };
63c1d255d3SCy Schubert
64c1d255d3SCy Schubert static const struct sock_fprog dhcp_sock_filter = {
65c1d255d3SCy Schubert .len = ARRAY_SIZE(dhcp_sock_filter_insns),
66c1d255d3SCy Schubert .filter = dhcp_sock_filter_insns,
67c1d255d3SCy Schubert };
68c1d255d3SCy Schubert
69c1d255d3SCy Schubert
70c1d255d3SCy Schubert /* Generated by 'sudo tcpdump -dd -s 1500 multicast and ip6[6]=58' */
71c1d255d3SCy Schubert static struct sock_filter ndisc_sock_filter_insns[] = {
72c1d255d3SCy Schubert { 0x30, 0, 0, 0x00000000 },
73c1d255d3SCy Schubert { 0x45, 0, 5, 0x00000001 },
74c1d255d3SCy Schubert { 0x28, 0, 0, 0x0000000c },
75c1d255d3SCy Schubert { 0x15, 0, 3, 0x000086dd },
76c1d255d3SCy Schubert { 0x30, 0, 0, 0x00000014 },
77c1d255d3SCy Schubert { 0x15, 0, 1, 0x0000003a },
78c1d255d3SCy Schubert { 0x6, 0, 0, 0x000005dc },
79c1d255d3SCy Schubert { 0x6, 0, 0, 0x00000000 },
80c1d255d3SCy Schubert };
81c1d255d3SCy Schubert
82c1d255d3SCy Schubert static const struct sock_fprog ndisc_sock_filter = {
83c1d255d3SCy Schubert .len = ARRAY_SIZE(ndisc_sock_filter_insns),
84c1d255d3SCy Schubert .filter = ndisc_sock_filter_insns,
85c1d255d3SCy Schubert };
86c1d255d3SCy Schubert
87c1d255d3SCy Schubert /* drop packet if skb->pkt_type is PACKET_OTHERHOST (0x03). Generated by:
88c1d255d3SCy Schubert * $ bpfc - <<EOF
89c1d255d3SCy Schubert * > ldb #type
90c1d255d3SCy Schubert * > jeq #0x03, drop
91c1d255d3SCy Schubert * > pass: ret #-1
92c1d255d3SCy Schubert * > drop: ret #0
93c1d255d3SCy Schubert * > EOF
94c1d255d3SCy Schubert */
95c1d255d3SCy Schubert static struct sock_filter pkt_type_filter_insns[] = {
96c1d255d3SCy Schubert { 0x30, 0, 0, 0xfffff004 },
97c1d255d3SCy Schubert { 0x15, 1, 0, 0x00000003 },
98c1d255d3SCy Schubert { 0x6, 0, 0, 0xffffffff },
99c1d255d3SCy Schubert { 0x6, 0, 0, 0x00000000 },
100c1d255d3SCy Schubert };
101c1d255d3SCy Schubert
102c1d255d3SCy Schubert static const struct sock_fprog pkt_type_sock_filter = {
103c1d255d3SCy Schubert .len = ARRAY_SIZE(pkt_type_filter_insns),
104c1d255d3SCy Schubert .filter = pkt_type_filter_insns,
105c1d255d3SCy Schubert };
106c1d255d3SCy Schubert
107c1d255d3SCy Schubert
l2_packet_get_own_addr(struct l2_packet_data * l2,u8 * addr)108c1d255d3SCy Schubert int l2_packet_get_own_addr(struct l2_packet_data *l2, u8 *addr)
109c1d255d3SCy Schubert {
110c1d255d3SCy Schubert os_memcpy(addr, l2->own_addr, ETH_ALEN);
111c1d255d3SCy Schubert return 0;
112c1d255d3SCy Schubert }
113c1d255d3SCy Schubert
114c1d255d3SCy Schubert
l2_packet_send(struct l2_packet_data * l2,const u8 * dst_addr,u16 proto,const u8 * buf,size_t len)115c1d255d3SCy Schubert int l2_packet_send(struct l2_packet_data *l2, const u8 *dst_addr, u16 proto,
116c1d255d3SCy Schubert const u8 *buf, size_t len)
117c1d255d3SCy Schubert {
118c1d255d3SCy Schubert int ret;
119c1d255d3SCy Schubert
120c1d255d3SCy Schubert if (TEST_FAIL())
121c1d255d3SCy Schubert return -1;
122c1d255d3SCy Schubert if (l2 == NULL)
123c1d255d3SCy Schubert return -1;
124c1d255d3SCy Schubert if (l2->l2_hdr) {
125c1d255d3SCy Schubert ret = send(l2->fd, buf, len, 0);
126c1d255d3SCy Schubert if (ret < 0)
127c1d255d3SCy Schubert wpa_printf(MSG_ERROR, "l2_packet_send - send: %s",
128c1d255d3SCy Schubert strerror(errno));
129c1d255d3SCy Schubert } else {
130c1d255d3SCy Schubert struct sockaddr_ll ll;
131c1d255d3SCy Schubert os_memset(&ll, 0, sizeof(ll));
132c1d255d3SCy Schubert ll.sll_family = AF_PACKET;
133c1d255d3SCy Schubert ll.sll_ifindex = l2->ifindex;
134c1d255d3SCy Schubert ll.sll_protocol = htons(proto);
135c1d255d3SCy Schubert ll.sll_halen = ETH_ALEN;
136c1d255d3SCy Schubert os_memcpy(ll.sll_addr, dst_addr, ETH_ALEN);
137c1d255d3SCy Schubert ret = sendto(l2->fd, buf, len, 0, (struct sockaddr *) &ll,
138c1d255d3SCy Schubert sizeof(ll));
139c1d255d3SCy Schubert if (ret < 0) {
140c1d255d3SCy Schubert wpa_printf(MSG_ERROR, "l2_packet_send - sendto: %s",
141c1d255d3SCy Schubert strerror(errno));
142c1d255d3SCy Schubert }
143c1d255d3SCy Schubert }
144c1d255d3SCy Schubert return ret;
145c1d255d3SCy Schubert }
146c1d255d3SCy Schubert
147c1d255d3SCy Schubert
l2_packet_receive(int sock,void * eloop_ctx,void * sock_ctx)148c1d255d3SCy Schubert static void l2_packet_receive(int sock, void *eloop_ctx, void *sock_ctx)
149c1d255d3SCy Schubert {
150c1d255d3SCy Schubert struct l2_packet_data *l2 = eloop_ctx;
151c1d255d3SCy Schubert u8 buf[2300];
152c1d255d3SCy Schubert int res;
153c1d255d3SCy Schubert struct sockaddr_ll ll;
154c1d255d3SCy Schubert socklen_t fromlen;
155c1d255d3SCy Schubert
156c1d255d3SCy Schubert os_memset(&ll, 0, sizeof(ll));
157c1d255d3SCy Schubert fromlen = sizeof(ll);
158c1d255d3SCy Schubert res = recvfrom(sock, buf, sizeof(buf), 0, (struct sockaddr *) &ll,
159c1d255d3SCy Schubert &fromlen);
160c1d255d3SCy Schubert if (res < 0) {
161c1d255d3SCy Schubert wpa_printf(MSG_DEBUG, "l2_packet_receive - recvfrom: %s",
162c1d255d3SCy Schubert strerror(errno));
163c1d255d3SCy Schubert return;
164c1d255d3SCy Schubert }
165c1d255d3SCy Schubert
166c1d255d3SCy Schubert wpa_printf(MSG_DEBUG, "%s: src=" MACSTR " len=%d",
167c1d255d3SCy Schubert __func__, MAC2STR(ll.sll_addr), (int) res);
168c1d255d3SCy Schubert
169c1d255d3SCy Schubert #ifndef CONFIG_NO_LINUX_PACKET_SOCKET_WAR
170c1d255d3SCy Schubert if (l2->fd_br_rx >= 0) {
171c1d255d3SCy Schubert u8 hash[SHA1_MAC_LEN];
172c1d255d3SCy Schubert const u8 *addr[1];
173c1d255d3SCy Schubert size_t len[1];
174c1d255d3SCy Schubert const struct l2_ethhdr *eth = (const struct l2_ethhdr *) buf;
175c1d255d3SCy Schubert
176c1d255d3SCy Schubert /*
177c1d255d3SCy Schubert * Close the workaround socket if the kernel version seems to be
178c1d255d3SCy Schubert * able to deliver packets through the packet socket before
179c1d255d3SCy Schubert * authorization has been completed (in dormant state).
180c1d255d3SCy Schubert */
181c1d255d3SCy Schubert if (l2->num_rx_br <= 1 &&
182*a90b9d01SCy Schubert (ether_addr_equal(eth->h_dest, l2->own_addr) ||
183c1d255d3SCy Schubert is_multicast_ether_addr(eth->h_dest))) {
184c1d255d3SCy Schubert wpa_printf(MSG_DEBUG,
185c1d255d3SCy Schubert "l2_packet_receive: Main packet socket for %s seems to have working RX - close workaround bridge socket",
186c1d255d3SCy Schubert l2->ifname);
187c1d255d3SCy Schubert eloop_unregister_read_sock(l2->fd_br_rx);
188c1d255d3SCy Schubert close(l2->fd_br_rx);
189c1d255d3SCy Schubert l2->fd_br_rx = -1;
190c1d255d3SCy Schubert }
191c1d255d3SCy Schubert
192c1d255d3SCy Schubert addr[0] = buf;
193c1d255d3SCy Schubert len[0] = res;
194c1d255d3SCy Schubert sha1_vector(1, addr, len, hash);
195c1d255d3SCy Schubert if (l2->last_from_br &&
196c1d255d3SCy Schubert os_memcmp(hash, l2->last_hash, SHA1_MAC_LEN) == 0) {
197c1d255d3SCy Schubert wpa_printf(MSG_DEBUG, "%s: Drop duplicate RX",
198c1d255d3SCy Schubert __func__);
199c1d255d3SCy Schubert return;
200c1d255d3SCy Schubert }
201c1d255d3SCy Schubert if (l2->last_from_br_prev &&
202c1d255d3SCy Schubert os_memcmp(hash, l2->last_hash_prev, SHA1_MAC_LEN) == 0) {
203c1d255d3SCy Schubert wpa_printf(MSG_DEBUG, "%s: Drop duplicate RX(prev)",
204c1d255d3SCy Schubert __func__);
205c1d255d3SCy Schubert return;
206c1d255d3SCy Schubert }
207c1d255d3SCy Schubert os_memcpy(l2->last_hash_prev, l2->last_hash, SHA1_MAC_LEN);
208c1d255d3SCy Schubert l2->last_from_br_prev = l2->last_from_br;
209c1d255d3SCy Schubert os_memcpy(l2->last_hash, hash, SHA1_MAC_LEN);
210c1d255d3SCy Schubert }
211c1d255d3SCy Schubert
212c1d255d3SCy Schubert l2->last_from_br = 0;
213c1d255d3SCy Schubert #endif /* CONFIG_NO_LINUX_PACKET_SOCKET_WAR */
214c1d255d3SCy Schubert l2->rx_callback(l2->rx_callback_ctx, ll.sll_addr, buf, res);
215c1d255d3SCy Schubert }
216c1d255d3SCy Schubert
217c1d255d3SCy Schubert
218c1d255d3SCy Schubert #ifndef CONFIG_NO_LINUX_PACKET_SOCKET_WAR
l2_packet_receive_br(int sock,void * eloop_ctx,void * sock_ctx)219c1d255d3SCy Schubert static void l2_packet_receive_br(int sock, void *eloop_ctx, void *sock_ctx)
220c1d255d3SCy Schubert {
221c1d255d3SCy Schubert struct l2_packet_data *l2 = eloop_ctx;
222c1d255d3SCy Schubert u8 buf[2300];
223c1d255d3SCy Schubert int res;
224c1d255d3SCy Schubert struct sockaddr_ll ll;
225c1d255d3SCy Schubert socklen_t fromlen;
226c1d255d3SCy Schubert u8 hash[SHA1_MAC_LEN];
227c1d255d3SCy Schubert const u8 *addr[1];
228c1d255d3SCy Schubert size_t len[1];
229c1d255d3SCy Schubert
230c1d255d3SCy Schubert l2->num_rx_br++;
231c1d255d3SCy Schubert os_memset(&ll, 0, sizeof(ll));
232c1d255d3SCy Schubert fromlen = sizeof(ll);
233c1d255d3SCy Schubert res = recvfrom(sock, buf, sizeof(buf), 0, (struct sockaddr *) &ll,
234c1d255d3SCy Schubert &fromlen);
235c1d255d3SCy Schubert if (res < 0) {
236c1d255d3SCy Schubert wpa_printf(MSG_DEBUG, "l2_packet_receive_br - recvfrom: %s",
237c1d255d3SCy Schubert strerror(errno));
238c1d255d3SCy Schubert return;
239c1d255d3SCy Schubert }
240c1d255d3SCy Schubert
241c1d255d3SCy Schubert wpa_printf(MSG_DEBUG, "%s: src=" MACSTR " len=%d",
242c1d255d3SCy Schubert __func__, MAC2STR(ll.sll_addr), (int) res);
243c1d255d3SCy Schubert
244*a90b9d01SCy Schubert if (ether_addr_equal(ll.sll_addr, l2->own_addr)) {
245c1d255d3SCy Schubert wpa_printf(MSG_DEBUG, "%s: Drop RX of own frame", __func__);
246c1d255d3SCy Schubert return;
247c1d255d3SCy Schubert }
248c1d255d3SCy Schubert
249c1d255d3SCy Schubert addr[0] = buf;
250c1d255d3SCy Schubert len[0] = res;
251c1d255d3SCy Schubert sha1_vector(1, addr, len, hash);
252c1d255d3SCy Schubert if (!l2->last_from_br &&
253c1d255d3SCy Schubert os_memcmp(hash, l2->last_hash, SHA1_MAC_LEN) == 0) {
254c1d255d3SCy Schubert wpa_printf(MSG_DEBUG, "%s: Drop duplicate RX", __func__);
255c1d255d3SCy Schubert return;
256c1d255d3SCy Schubert }
257c1d255d3SCy Schubert if (!l2->last_from_br_prev &&
258c1d255d3SCy Schubert os_memcmp(hash, l2->last_hash_prev, SHA1_MAC_LEN) == 0) {
259c1d255d3SCy Schubert wpa_printf(MSG_DEBUG, "%s: Drop duplicate RX(prev)", __func__);
260c1d255d3SCy Schubert return;
261c1d255d3SCy Schubert }
262c1d255d3SCy Schubert os_memcpy(l2->last_hash_prev, l2->last_hash, SHA1_MAC_LEN);
263c1d255d3SCy Schubert l2->last_from_br_prev = l2->last_from_br;
264c1d255d3SCy Schubert l2->last_from_br = 1;
265c1d255d3SCy Schubert os_memcpy(l2->last_hash, hash, SHA1_MAC_LEN);
266c1d255d3SCy Schubert l2->rx_callback(l2->rx_callback_ctx, ll.sll_addr, buf, res);
267c1d255d3SCy Schubert }
268c1d255d3SCy Schubert #endif /* CONFIG_NO_LINUX_PACKET_SOCKET_WAR */
269c1d255d3SCy Schubert
270c1d255d3SCy Schubert
l2_packet_init(const char * ifname,const u8 * own_addr,unsigned short protocol,void (* rx_callback)(void * ctx,const u8 * src_addr,const u8 * buf,size_t len),void * rx_callback_ctx,int l2_hdr)271c1d255d3SCy Schubert struct l2_packet_data * l2_packet_init(
272c1d255d3SCy Schubert const char *ifname, const u8 *own_addr, unsigned short protocol,
273c1d255d3SCy Schubert void (*rx_callback)(void *ctx, const u8 *src_addr,
274c1d255d3SCy Schubert const u8 *buf, size_t len),
275c1d255d3SCy Schubert void *rx_callback_ctx, int l2_hdr)
276c1d255d3SCy Schubert {
277c1d255d3SCy Schubert struct l2_packet_data *l2;
278c1d255d3SCy Schubert struct ifreq ifr;
279c1d255d3SCy Schubert struct sockaddr_ll ll;
280c1d255d3SCy Schubert
281c1d255d3SCy Schubert l2 = os_zalloc(sizeof(struct l2_packet_data));
282c1d255d3SCy Schubert if (l2 == NULL)
283c1d255d3SCy Schubert return NULL;
284c1d255d3SCy Schubert os_strlcpy(l2->ifname, ifname, sizeof(l2->ifname));
285c1d255d3SCy Schubert l2->rx_callback = rx_callback;
286c1d255d3SCy Schubert l2->rx_callback_ctx = rx_callback_ctx;
287c1d255d3SCy Schubert l2->l2_hdr = l2_hdr;
288c1d255d3SCy Schubert #ifndef CONFIG_NO_LINUX_PACKET_SOCKET_WAR
289c1d255d3SCy Schubert l2->fd_br_rx = -1;
290c1d255d3SCy Schubert #endif /* CONFIG_NO_LINUX_PACKET_SOCKET_WAR */
291c1d255d3SCy Schubert
292c1d255d3SCy Schubert l2->fd = socket(PF_PACKET, l2_hdr ? SOCK_RAW : SOCK_DGRAM,
293c1d255d3SCy Schubert htons(protocol));
294c1d255d3SCy Schubert if (l2->fd < 0) {
295c1d255d3SCy Schubert wpa_printf(MSG_ERROR, "%s: socket(PF_PACKET): %s",
296c1d255d3SCy Schubert __func__, strerror(errno));
297c1d255d3SCy Schubert os_free(l2);
298c1d255d3SCy Schubert return NULL;
299c1d255d3SCy Schubert }
300c1d255d3SCy Schubert os_memset(&ifr, 0, sizeof(ifr));
301c1d255d3SCy Schubert os_strlcpy(ifr.ifr_name, l2->ifname, sizeof(ifr.ifr_name));
302c1d255d3SCy Schubert if (ioctl(l2->fd, SIOCGIFINDEX, &ifr) < 0) {
303c1d255d3SCy Schubert wpa_printf(MSG_ERROR, "%s: ioctl[SIOCGIFINDEX]: %s",
304c1d255d3SCy Schubert __func__, strerror(errno));
305c1d255d3SCy Schubert close(l2->fd);
306c1d255d3SCy Schubert os_free(l2);
307c1d255d3SCy Schubert return NULL;
308c1d255d3SCy Schubert }
309c1d255d3SCy Schubert l2->ifindex = ifr.ifr_ifindex;
310c1d255d3SCy Schubert
311c1d255d3SCy Schubert os_memset(&ll, 0, sizeof(ll));
312c1d255d3SCy Schubert ll.sll_family = PF_PACKET;
313c1d255d3SCy Schubert ll.sll_ifindex = ifr.ifr_ifindex;
314c1d255d3SCy Schubert ll.sll_protocol = htons(protocol);
315c1d255d3SCy Schubert if (rx_callback &&
316c1d255d3SCy Schubert bind(l2->fd, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
317c1d255d3SCy Schubert wpa_printf(MSG_ERROR, "%s: bind[PF_PACKET]: %s",
318c1d255d3SCy Schubert __func__, strerror(errno));
319c1d255d3SCy Schubert close(l2->fd);
320c1d255d3SCy Schubert os_free(l2);
321c1d255d3SCy Schubert return NULL;
322c1d255d3SCy Schubert }
323c1d255d3SCy Schubert
324c1d255d3SCy Schubert if (ioctl(l2->fd, SIOCGIFHWADDR, &ifr) < 0) {
325c1d255d3SCy Schubert wpa_printf(MSG_ERROR, "%s: ioctl[SIOCGIFHWADDR]: %s",
326c1d255d3SCy Schubert __func__, strerror(errno));
327c1d255d3SCy Schubert close(l2->fd);
328c1d255d3SCy Schubert os_free(l2);
329c1d255d3SCy Schubert return NULL;
330c1d255d3SCy Schubert }
331c1d255d3SCy Schubert os_memcpy(l2->own_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
332c1d255d3SCy Schubert
333c1d255d3SCy Schubert if (rx_callback)
334c1d255d3SCy Schubert eloop_register_read_sock(l2->fd, l2_packet_receive, l2, NULL);
335c1d255d3SCy Schubert
336c1d255d3SCy Schubert return l2;
337c1d255d3SCy Schubert }
338c1d255d3SCy Schubert
339c1d255d3SCy Schubert
l2_packet_init_bridge(const char * br_ifname,const char * ifname,const u8 * own_addr,unsigned short protocol,void (* rx_callback)(void * ctx,const u8 * src_addr,const u8 * buf,size_t len),void * rx_callback_ctx,int l2_hdr)340c1d255d3SCy Schubert struct l2_packet_data * l2_packet_init_bridge(
341c1d255d3SCy Schubert const char *br_ifname, const char *ifname, const u8 *own_addr,
342c1d255d3SCy Schubert unsigned short protocol,
343c1d255d3SCy Schubert void (*rx_callback)(void *ctx, const u8 *src_addr,
344c1d255d3SCy Schubert const u8 *buf, size_t len),
345c1d255d3SCy Schubert void *rx_callback_ctx, int l2_hdr)
346c1d255d3SCy Schubert {
347c1d255d3SCy Schubert struct l2_packet_data *l2;
348c1d255d3SCy Schubert #ifndef CONFIG_NO_LINUX_PACKET_SOCKET_WAR
349c1d255d3SCy Schubert struct sock_filter ethertype_sock_filter_insns[] = {
350c1d255d3SCy Schubert /* Load ethertype */
351c1d255d3SCy Schubert BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 2 * ETH_ALEN),
352c1d255d3SCy Schubert /* Jump over next statement if ethertype does not match */
353c1d255d3SCy Schubert BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, protocol, 0, 1),
354c1d255d3SCy Schubert /* Ethertype match - return all */
355c1d255d3SCy Schubert BPF_STMT(BPF_RET | BPF_K, ~0),
356c1d255d3SCy Schubert /* No match - drop */
357c1d255d3SCy Schubert BPF_STMT(BPF_RET | BPF_K, 0)
358c1d255d3SCy Schubert };
359c1d255d3SCy Schubert const struct sock_fprog ethertype_sock_filter = {
360c1d255d3SCy Schubert .len = ARRAY_SIZE(ethertype_sock_filter_insns),
361c1d255d3SCy Schubert .filter = ethertype_sock_filter_insns,
362c1d255d3SCy Schubert };
363c1d255d3SCy Schubert struct sockaddr_ll ll;
364c1d255d3SCy Schubert #endif /* CONFIG_NO_LINUX_PACKET_SOCKET_WAR */
365c1d255d3SCy Schubert
366c1d255d3SCy Schubert l2 = l2_packet_init(br_ifname, own_addr, protocol, rx_callback,
367c1d255d3SCy Schubert rx_callback_ctx, l2_hdr);
368c1d255d3SCy Schubert if (!l2)
369c1d255d3SCy Schubert return NULL;
370c1d255d3SCy Schubert
371c1d255d3SCy Schubert #ifndef CONFIG_NO_LINUX_PACKET_SOCKET_WAR
372c1d255d3SCy Schubert /*
373c1d255d3SCy Schubert * The Linux packet socket behavior has changed over the years and there
374c1d255d3SCy Schubert * is an inconvenient regression in it that breaks RX for a specific
375c1d255d3SCy Schubert * protocol from interfaces in a bridge when that interface is not in
376c1d255d3SCy Schubert * fully operation state (i.e., when in station mode and not completed
377c1d255d3SCy Schubert * authorization). To work around this, register ETH_P_ALL version of
378c1d255d3SCy Schubert * the packet socket bound to the real netdev and use socket filter to
379c1d255d3SCy Schubert * match the ethertype value. This version is less efficient, but
380c1d255d3SCy Schubert * required for functionality with many kernel version. If the main
381c1d255d3SCy Schubert * packet socket is found to be working, this less efficient version
382c1d255d3SCy Schubert * gets closed automatically.
383c1d255d3SCy Schubert */
384c1d255d3SCy Schubert
385c1d255d3SCy Schubert l2->fd_br_rx = socket(PF_PACKET, l2_hdr ? SOCK_RAW : SOCK_DGRAM,
386c1d255d3SCy Schubert htons(ETH_P_ALL));
387c1d255d3SCy Schubert if (l2->fd_br_rx < 0) {
388c1d255d3SCy Schubert wpa_printf(MSG_DEBUG, "%s: socket(PF_PACKET-fd_br_rx): %s",
389c1d255d3SCy Schubert __func__, strerror(errno));
390c1d255d3SCy Schubert /* try to continue without the workaround RX socket */
391c1d255d3SCy Schubert return l2;
392c1d255d3SCy Schubert }
393c1d255d3SCy Schubert
394c1d255d3SCy Schubert os_memset(&ll, 0, sizeof(ll));
395c1d255d3SCy Schubert ll.sll_family = PF_PACKET;
396c1d255d3SCy Schubert ll.sll_ifindex = if_nametoindex(ifname);
397c1d255d3SCy Schubert ll.sll_protocol = htons(ETH_P_ALL);
398c1d255d3SCy Schubert if (bind(l2->fd_br_rx, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
399c1d255d3SCy Schubert wpa_printf(MSG_DEBUG, "%s: bind[PF_PACKET-fd_br_rx]: %s",
400c1d255d3SCy Schubert __func__, strerror(errno));
401c1d255d3SCy Schubert /* try to continue without the workaround RX socket */
402c1d255d3SCy Schubert close(l2->fd_br_rx);
403c1d255d3SCy Schubert l2->fd_br_rx = -1;
404c1d255d3SCy Schubert return l2;
405c1d255d3SCy Schubert }
406c1d255d3SCy Schubert
407c1d255d3SCy Schubert if (setsockopt(l2->fd_br_rx, SOL_SOCKET, SO_ATTACH_FILTER,
408c1d255d3SCy Schubert ðertype_sock_filter, sizeof(struct sock_fprog))) {
409c1d255d3SCy Schubert wpa_printf(MSG_DEBUG,
410c1d255d3SCy Schubert "l2_packet_linux: setsockopt(SO_ATTACH_FILTER) failed: %s",
411c1d255d3SCy Schubert strerror(errno));
412c1d255d3SCy Schubert /* try to continue without the workaround RX socket */
413c1d255d3SCy Schubert close(l2->fd_br_rx);
414c1d255d3SCy Schubert l2->fd_br_rx = -1;
415c1d255d3SCy Schubert return l2;
416c1d255d3SCy Schubert }
417c1d255d3SCy Schubert
418c1d255d3SCy Schubert eloop_register_read_sock(l2->fd_br_rx, l2_packet_receive_br, l2, NULL);
419c1d255d3SCy Schubert #endif /* CONFIG_NO_LINUX_PACKET_SOCKET_WAR */
420c1d255d3SCy Schubert
421c1d255d3SCy Schubert return l2;
422c1d255d3SCy Schubert }
423c1d255d3SCy Schubert
424c1d255d3SCy Schubert
l2_packet_deinit(struct l2_packet_data * l2)425c1d255d3SCy Schubert void l2_packet_deinit(struct l2_packet_data *l2)
426c1d255d3SCy Schubert {
427c1d255d3SCy Schubert if (l2 == NULL)
428c1d255d3SCy Schubert return;
429c1d255d3SCy Schubert
430c1d255d3SCy Schubert if (l2->fd >= 0) {
431c1d255d3SCy Schubert eloop_unregister_read_sock(l2->fd);
432c1d255d3SCy Schubert close(l2->fd);
433c1d255d3SCy Schubert }
434c1d255d3SCy Schubert
435c1d255d3SCy Schubert #ifndef CONFIG_NO_LINUX_PACKET_SOCKET_WAR
436c1d255d3SCy Schubert if (l2->fd_br_rx >= 0) {
437c1d255d3SCy Schubert eloop_unregister_read_sock(l2->fd_br_rx);
438c1d255d3SCy Schubert close(l2->fd_br_rx);
439c1d255d3SCy Schubert }
440c1d255d3SCy Schubert #endif /* CONFIG_NO_LINUX_PACKET_SOCKET_WAR */
441c1d255d3SCy Schubert
442c1d255d3SCy Schubert os_free(l2);
443c1d255d3SCy Schubert }
444c1d255d3SCy Schubert
445c1d255d3SCy Schubert
l2_packet_get_ip_addr(struct l2_packet_data * l2,char * buf,size_t len)446c1d255d3SCy Schubert int l2_packet_get_ip_addr(struct l2_packet_data *l2, char *buf, size_t len)
447c1d255d3SCy Schubert {
448c1d255d3SCy Schubert int s;
449c1d255d3SCy Schubert struct ifreq ifr;
450c1d255d3SCy Schubert struct sockaddr_in *saddr;
451c1d255d3SCy Schubert size_t res;
452c1d255d3SCy Schubert
453c1d255d3SCy Schubert s = socket(PF_INET, SOCK_DGRAM, 0);
454c1d255d3SCy Schubert if (s < 0) {
455c1d255d3SCy Schubert wpa_printf(MSG_ERROR, "%s: socket: %s",
456c1d255d3SCy Schubert __func__, strerror(errno));
457c1d255d3SCy Schubert return -1;
458c1d255d3SCy Schubert }
459c1d255d3SCy Schubert os_memset(&ifr, 0, sizeof(ifr));
460c1d255d3SCy Schubert os_strlcpy(ifr.ifr_name, l2->ifname, sizeof(ifr.ifr_name));
461c1d255d3SCy Schubert if (ioctl(s, SIOCGIFADDR, &ifr) < 0) {
462c1d255d3SCy Schubert if (errno != EADDRNOTAVAIL)
463c1d255d3SCy Schubert wpa_printf(MSG_ERROR, "%s: ioctl[SIOCGIFADDR]: %s",
464c1d255d3SCy Schubert __func__, strerror(errno));
465c1d255d3SCy Schubert close(s);
466c1d255d3SCy Schubert return -1;
467c1d255d3SCy Schubert }
468c1d255d3SCy Schubert close(s);
469c1d255d3SCy Schubert saddr = aliasing_hide_typecast(&ifr.ifr_addr, struct sockaddr_in);
470c1d255d3SCy Schubert if (saddr->sin_family != AF_INET)
471c1d255d3SCy Schubert return -1;
472c1d255d3SCy Schubert res = os_strlcpy(buf, inet_ntoa(saddr->sin_addr), len);
473c1d255d3SCy Schubert if (res >= len)
474c1d255d3SCy Schubert return -1;
475c1d255d3SCy Schubert return 0;
476c1d255d3SCy Schubert }
477c1d255d3SCy Schubert
478c1d255d3SCy Schubert
l2_packet_notify_auth_start(struct l2_packet_data * l2)479c1d255d3SCy Schubert void l2_packet_notify_auth_start(struct l2_packet_data *l2)
480c1d255d3SCy Schubert {
481c1d255d3SCy Schubert }
482c1d255d3SCy Schubert
483c1d255d3SCy Schubert
l2_packet_set_packet_filter(struct l2_packet_data * l2,enum l2_packet_filter_type type)484c1d255d3SCy Schubert int l2_packet_set_packet_filter(struct l2_packet_data *l2,
485c1d255d3SCy Schubert enum l2_packet_filter_type type)
486c1d255d3SCy Schubert {
487c1d255d3SCy Schubert const struct sock_fprog *sock_filter;
488c1d255d3SCy Schubert
489c1d255d3SCy Schubert if (TEST_FAIL())
490c1d255d3SCy Schubert return -1;
491c1d255d3SCy Schubert
492c1d255d3SCy Schubert switch (type) {
493c1d255d3SCy Schubert case L2_PACKET_FILTER_DHCP:
494c1d255d3SCy Schubert sock_filter = &dhcp_sock_filter;
495c1d255d3SCy Schubert break;
496c1d255d3SCy Schubert case L2_PACKET_FILTER_NDISC:
497c1d255d3SCy Schubert sock_filter = &ndisc_sock_filter;
498c1d255d3SCy Schubert break;
499c1d255d3SCy Schubert case L2_PACKET_FILTER_PKTTYPE:
500c1d255d3SCy Schubert sock_filter = &pkt_type_sock_filter;
501c1d255d3SCy Schubert break;
502c1d255d3SCy Schubert default:
503c1d255d3SCy Schubert return -1;
504c1d255d3SCy Schubert }
505c1d255d3SCy Schubert
506c1d255d3SCy Schubert if (setsockopt(l2->fd, SOL_SOCKET, SO_ATTACH_FILTER,
507c1d255d3SCy Schubert sock_filter, sizeof(struct sock_fprog))) {
508c1d255d3SCy Schubert wpa_printf(MSG_ERROR,
509c1d255d3SCy Schubert "l2_packet_linux: setsockopt(SO_ATTACH_FILTER) failed: %s",
510c1d255d3SCy Schubert strerror(errno));
511c1d255d3SCy Schubert return -1;
512c1d255d3SCy Schubert }
513c1d255d3SCy Schubert
514c1d255d3SCy Schubert return 0;
515c1d255d3SCy Schubert }
516