xref: /freebsd/contrib/wpa/src/drivers/driver_wired.c (revision e28a4053b110e06768631ac8401ed4a3c05e68a5)
13157ba21SRui Paulo /*
2*e28a4053SRui Paulo  * Wired Ethernet driver interface
3*e28a4053SRui Paulo  * Copyright (c) 2005-2009, Jouni Malinen <j@w1.fi>
4*e28a4053SRui Paulo  * Copyright (c) 2004, Gunter Burchardt <tira@isx.de>
53157ba21SRui Paulo  *
63157ba21SRui Paulo  * This program is free software; you can redistribute it and/or modify
73157ba21SRui Paulo  * it under the terms of the GNU General Public License version 2 as
83157ba21SRui Paulo  * published by the Free Software Foundation.
93157ba21SRui Paulo  *
103157ba21SRui Paulo  * Alternatively, this software may be distributed under the terms of BSD
113157ba21SRui Paulo  * license.
123157ba21SRui Paulo  *
133157ba21SRui Paulo  * See README and COPYING for more details.
143157ba21SRui Paulo  */
153157ba21SRui Paulo 
163157ba21SRui Paulo #include "includes.h"
173157ba21SRui Paulo #include <sys/ioctl.h>
183157ba21SRui Paulo #include <net/if.h>
193157ba21SRui Paulo #ifdef __linux__
203157ba21SRui Paulo #include <netpacket/packet.h>
21*e28a4053SRui Paulo #include <net/if_arp.h>
22*e28a4053SRui Paulo #include <net/if.h>
233157ba21SRui Paulo #endif /* __linux__ */
24*e28a4053SRui Paulo #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
253157ba21SRui Paulo #include <net/if_dl.h>
26*e28a4053SRui Paulo #endif /* defined(__FreeBSD__) || defined(__DragonFly__) || defined(__FreeBSD_kernel__) */
273157ba21SRui Paulo 
283157ba21SRui Paulo #include "common.h"
29*e28a4053SRui Paulo #include "eloop.h"
303157ba21SRui Paulo #include "driver.h"
313157ba21SRui Paulo 
32*e28a4053SRui Paulo #ifdef _MSC_VER
33*e28a4053SRui Paulo #pragma pack(push, 1)
34*e28a4053SRui Paulo #endif /* _MSC_VER */
35*e28a4053SRui Paulo 
36*e28a4053SRui Paulo struct ieee8023_hdr {
37*e28a4053SRui Paulo 	u8 dest[6];
38*e28a4053SRui Paulo 	u8 src[6];
39*e28a4053SRui Paulo 	u16 ethertype;
40*e28a4053SRui Paulo } STRUCT_PACKED;
41*e28a4053SRui Paulo 
42*e28a4053SRui Paulo #ifdef _MSC_VER
43*e28a4053SRui Paulo #pragma pack(pop)
44*e28a4053SRui Paulo #endif /* _MSC_VER */
453157ba21SRui Paulo 
463157ba21SRui Paulo static const u8 pae_group_addr[ETH_ALEN] =
473157ba21SRui Paulo { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x03 };
483157ba21SRui Paulo 
493157ba21SRui Paulo 
503157ba21SRui Paulo struct wpa_driver_wired_data {
513157ba21SRui Paulo 	char ifname[IFNAMSIZ + 1];
52*e28a4053SRui Paulo 	void *ctx;
53*e28a4053SRui Paulo 
54*e28a4053SRui Paulo 	int sock; /* raw packet socket for driver access */
55*e28a4053SRui Paulo 	int dhcp_sock; /* socket for dhcp packets */
56*e28a4053SRui Paulo 	int use_pae_group_addr;
57*e28a4053SRui Paulo 
58*e28a4053SRui Paulo 	int pf_sock;
593157ba21SRui Paulo 	int membership, multi, iff_allmulti, iff_up;
603157ba21SRui Paulo };
613157ba21SRui Paulo 
623157ba21SRui Paulo 
63*e28a4053SRui Paulo /* TODO: detecting new devices should eventually be changed from using DHCP
64*e28a4053SRui Paulo  * snooping to trigger on any packet from a new layer 2 MAC address, e.g.,
65*e28a4053SRui Paulo  * based on ebtables, etc. */
66*e28a4053SRui Paulo 
67*e28a4053SRui Paulo struct dhcp_message {
68*e28a4053SRui Paulo 	u_int8_t op;
69*e28a4053SRui Paulo 	u_int8_t htype;
70*e28a4053SRui Paulo 	u_int8_t hlen;
71*e28a4053SRui Paulo 	u_int8_t hops;
72*e28a4053SRui Paulo 	u_int32_t xid;
73*e28a4053SRui Paulo 	u_int16_t secs;
74*e28a4053SRui Paulo 	u_int16_t flags;
75*e28a4053SRui Paulo 	u_int32_t ciaddr;
76*e28a4053SRui Paulo 	u_int32_t yiaddr;
77*e28a4053SRui Paulo 	u_int32_t siaddr;
78*e28a4053SRui Paulo 	u_int32_t giaddr;
79*e28a4053SRui Paulo 	u_int8_t chaddr[16];
80*e28a4053SRui Paulo 	u_int8_t sname[64];
81*e28a4053SRui Paulo 	u_int8_t file[128];
82*e28a4053SRui Paulo 	u_int32_t cookie;
83*e28a4053SRui Paulo 	u_int8_t options[308]; /* 312 - cookie */
84*e28a4053SRui Paulo };
85*e28a4053SRui Paulo 
86*e28a4053SRui Paulo 
87*e28a4053SRui Paulo static int wired_multicast_membership(int sock, int ifindex,
88*e28a4053SRui Paulo 				      const u8 *addr, int add)
89*e28a4053SRui Paulo {
90*e28a4053SRui Paulo #ifdef __linux__
91*e28a4053SRui Paulo 	struct packet_mreq mreq;
92*e28a4053SRui Paulo 
93*e28a4053SRui Paulo 	if (sock < 0)
94*e28a4053SRui Paulo 		return -1;
95*e28a4053SRui Paulo 
96*e28a4053SRui Paulo 	os_memset(&mreq, 0, sizeof(mreq));
97*e28a4053SRui Paulo 	mreq.mr_ifindex = ifindex;
98*e28a4053SRui Paulo 	mreq.mr_type = PACKET_MR_MULTICAST;
99*e28a4053SRui Paulo 	mreq.mr_alen = ETH_ALEN;
100*e28a4053SRui Paulo 	os_memcpy(mreq.mr_address, addr, ETH_ALEN);
101*e28a4053SRui Paulo 
102*e28a4053SRui Paulo 	if (setsockopt(sock, SOL_PACKET,
103*e28a4053SRui Paulo 		       add ? PACKET_ADD_MEMBERSHIP : PACKET_DROP_MEMBERSHIP,
104*e28a4053SRui Paulo 		       &mreq, sizeof(mreq)) < 0) {
105*e28a4053SRui Paulo 		perror("setsockopt");
106*e28a4053SRui Paulo 		return -1;
107*e28a4053SRui Paulo 	}
108*e28a4053SRui Paulo 	return 0;
109*e28a4053SRui Paulo #else /* __linux__ */
110*e28a4053SRui Paulo 	return -1;
111*e28a4053SRui Paulo #endif /* __linux__ */
112*e28a4053SRui Paulo }
113*e28a4053SRui Paulo 
114*e28a4053SRui Paulo 
115*e28a4053SRui Paulo #ifdef __linux__
116*e28a4053SRui Paulo static void handle_data(void *ctx, unsigned char *buf, size_t len)
117*e28a4053SRui Paulo {
118*e28a4053SRui Paulo #ifdef HOSTAPD
119*e28a4053SRui Paulo 	struct ieee8023_hdr *hdr;
120*e28a4053SRui Paulo 	u8 *pos, *sa;
121*e28a4053SRui Paulo 	size_t left;
122*e28a4053SRui Paulo 	union wpa_event_data event;
123*e28a4053SRui Paulo 
124*e28a4053SRui Paulo 	/* must contain at least ieee8023_hdr 6 byte source, 6 byte dest,
125*e28a4053SRui Paulo 	 * 2 byte ethertype */
126*e28a4053SRui Paulo 	if (len < 14) {
127*e28a4053SRui Paulo 		wpa_printf(MSG_MSGDUMP, "handle_data: too short (%lu)",
128*e28a4053SRui Paulo 			   (unsigned long) len);
129*e28a4053SRui Paulo 		return;
130*e28a4053SRui Paulo 	}
131*e28a4053SRui Paulo 
132*e28a4053SRui Paulo 	hdr = (struct ieee8023_hdr *) buf;
133*e28a4053SRui Paulo 
134*e28a4053SRui Paulo 	switch (ntohs(hdr->ethertype)) {
135*e28a4053SRui Paulo 		case ETH_P_PAE:
136*e28a4053SRui Paulo 			wpa_printf(MSG_MSGDUMP, "Received EAPOL packet");
137*e28a4053SRui Paulo 			sa = hdr->src;
138*e28a4053SRui Paulo 			os_memset(&event, 0, sizeof(event));
139*e28a4053SRui Paulo 			event.new_sta.addr = sa;
140*e28a4053SRui Paulo 			wpa_supplicant_event(ctx, EVENT_NEW_STA, &event);
141*e28a4053SRui Paulo 
142*e28a4053SRui Paulo 			pos = (u8 *) (hdr + 1);
143*e28a4053SRui Paulo 			left = len - sizeof(*hdr);
144*e28a4053SRui Paulo 			drv_event_eapol_rx(ctx, sa, pos, left);
145*e28a4053SRui Paulo 		break;
146*e28a4053SRui Paulo 
147*e28a4053SRui Paulo 	default:
148*e28a4053SRui Paulo 		wpa_printf(MSG_DEBUG, "Unknown ethertype 0x%04x in data frame",
149*e28a4053SRui Paulo 			   ntohs(hdr->ethertype));
150*e28a4053SRui Paulo 		break;
151*e28a4053SRui Paulo 	}
152*e28a4053SRui Paulo #endif /* HOSTAPD */
153*e28a4053SRui Paulo }
154*e28a4053SRui Paulo 
155*e28a4053SRui Paulo 
156*e28a4053SRui Paulo static void handle_read(int sock, void *eloop_ctx, void *sock_ctx)
157*e28a4053SRui Paulo {
158*e28a4053SRui Paulo 	int len;
159*e28a4053SRui Paulo 	unsigned char buf[3000];
160*e28a4053SRui Paulo 
161*e28a4053SRui Paulo 	len = recv(sock, buf, sizeof(buf), 0);
162*e28a4053SRui Paulo 	if (len < 0) {
163*e28a4053SRui Paulo 		perror("recv");
164*e28a4053SRui Paulo 		return;
165*e28a4053SRui Paulo 	}
166*e28a4053SRui Paulo 
167*e28a4053SRui Paulo 	handle_data(eloop_ctx, buf, len);
168*e28a4053SRui Paulo }
169*e28a4053SRui Paulo 
170*e28a4053SRui Paulo 
171*e28a4053SRui Paulo static void handle_dhcp(int sock, void *eloop_ctx, void *sock_ctx)
172*e28a4053SRui Paulo {
173*e28a4053SRui Paulo 	int len;
174*e28a4053SRui Paulo 	unsigned char buf[3000];
175*e28a4053SRui Paulo 	struct dhcp_message *msg;
176*e28a4053SRui Paulo 	u8 *mac_address;
177*e28a4053SRui Paulo 	union wpa_event_data event;
178*e28a4053SRui Paulo 
179*e28a4053SRui Paulo 	len = recv(sock, buf, sizeof(buf), 0);
180*e28a4053SRui Paulo 	if (len < 0) {
181*e28a4053SRui Paulo 		perror("recv");
182*e28a4053SRui Paulo 		return;
183*e28a4053SRui Paulo 	}
184*e28a4053SRui Paulo 
185*e28a4053SRui Paulo 	/* must contain at least dhcp_message->chaddr */
186*e28a4053SRui Paulo 	if (len < 44) {
187*e28a4053SRui Paulo 		wpa_printf(MSG_MSGDUMP, "handle_dhcp: too short (%d)", len);
188*e28a4053SRui Paulo 		return;
189*e28a4053SRui Paulo 	}
190*e28a4053SRui Paulo 
191*e28a4053SRui Paulo 	msg = (struct dhcp_message *) buf;
192*e28a4053SRui Paulo 	mac_address = (u8 *) &(msg->chaddr);
193*e28a4053SRui Paulo 
194*e28a4053SRui Paulo 	wpa_printf(MSG_MSGDUMP, "Got DHCP broadcast packet from " MACSTR,
195*e28a4053SRui Paulo 		   MAC2STR(mac_address));
196*e28a4053SRui Paulo 
197*e28a4053SRui Paulo 	os_memset(&event, 0, sizeof(event));
198*e28a4053SRui Paulo 	event.new_sta.addr = mac_address;
199*e28a4053SRui Paulo 	wpa_supplicant_event(eloop_ctx, EVENT_NEW_STA, &event);
200*e28a4053SRui Paulo }
201*e28a4053SRui Paulo #endif /* __linux__ */
202*e28a4053SRui Paulo 
203*e28a4053SRui Paulo 
204*e28a4053SRui Paulo static int wired_init_sockets(struct wpa_driver_wired_data *drv, u8 *own_addr)
205*e28a4053SRui Paulo {
206*e28a4053SRui Paulo #ifdef __linux__
207*e28a4053SRui Paulo 	struct ifreq ifr;
208*e28a4053SRui Paulo 	struct sockaddr_ll addr;
209*e28a4053SRui Paulo 	struct sockaddr_in addr2;
210*e28a4053SRui Paulo 	int n = 1;
211*e28a4053SRui Paulo 
212*e28a4053SRui Paulo 	drv->sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_PAE));
213*e28a4053SRui Paulo 	if (drv->sock < 0) {
214*e28a4053SRui Paulo 		perror("socket[PF_PACKET,SOCK_RAW]");
215*e28a4053SRui Paulo 		return -1;
216*e28a4053SRui Paulo 	}
217*e28a4053SRui Paulo 
218*e28a4053SRui Paulo 	if (eloop_register_read_sock(drv->sock, handle_read, drv->ctx, NULL)) {
219*e28a4053SRui Paulo 		printf("Could not register read socket\n");
220*e28a4053SRui Paulo 		return -1;
221*e28a4053SRui Paulo 	}
222*e28a4053SRui Paulo 
223*e28a4053SRui Paulo 	os_memset(&ifr, 0, sizeof(ifr));
224*e28a4053SRui Paulo 	os_strlcpy(ifr.ifr_name, drv->ifname, sizeof(ifr.ifr_name));
225*e28a4053SRui Paulo 	if (ioctl(drv->sock, SIOCGIFINDEX, &ifr) != 0) {
226*e28a4053SRui Paulo 		perror("ioctl(SIOCGIFINDEX)");
227*e28a4053SRui Paulo 		return -1;
228*e28a4053SRui Paulo 	}
229*e28a4053SRui Paulo 
230*e28a4053SRui Paulo 	os_memset(&addr, 0, sizeof(addr));
231*e28a4053SRui Paulo 	addr.sll_family = AF_PACKET;
232*e28a4053SRui Paulo 	addr.sll_ifindex = ifr.ifr_ifindex;
233*e28a4053SRui Paulo 	wpa_printf(MSG_DEBUG, "Opening raw packet socket for ifindex %d",
234*e28a4053SRui Paulo 		   addr.sll_ifindex);
235*e28a4053SRui Paulo 
236*e28a4053SRui Paulo 	if (bind(drv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
237*e28a4053SRui Paulo 		perror("bind");
238*e28a4053SRui Paulo 		return -1;
239*e28a4053SRui Paulo 	}
240*e28a4053SRui Paulo 
241*e28a4053SRui Paulo 	/* filter multicast address */
242*e28a4053SRui Paulo 	if (wired_multicast_membership(drv->sock, ifr.ifr_ifindex,
243*e28a4053SRui Paulo 				       pae_group_addr, 1) < 0) {
244*e28a4053SRui Paulo 		wpa_printf(MSG_ERROR, "wired: Failed to add multicast group "
245*e28a4053SRui Paulo 			   "membership");
246*e28a4053SRui Paulo 		return -1;
247*e28a4053SRui Paulo 	}
248*e28a4053SRui Paulo 
249*e28a4053SRui Paulo 	os_memset(&ifr, 0, sizeof(ifr));
250*e28a4053SRui Paulo 	os_strlcpy(ifr.ifr_name, drv->ifname, sizeof(ifr.ifr_name));
251*e28a4053SRui Paulo 	if (ioctl(drv->sock, SIOCGIFHWADDR, &ifr) != 0) {
252*e28a4053SRui Paulo 		perror("ioctl(SIOCGIFHWADDR)");
253*e28a4053SRui Paulo 		return -1;
254*e28a4053SRui Paulo 	}
255*e28a4053SRui Paulo 
256*e28a4053SRui Paulo 	if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
257*e28a4053SRui Paulo 		printf("Invalid HW-addr family 0x%04x\n",
258*e28a4053SRui Paulo 		       ifr.ifr_hwaddr.sa_family);
259*e28a4053SRui Paulo 		return -1;
260*e28a4053SRui Paulo 	}
261*e28a4053SRui Paulo 	os_memcpy(own_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
262*e28a4053SRui Paulo 
263*e28a4053SRui Paulo 	/* setup dhcp listen socket for sta detection */
264*e28a4053SRui Paulo 	if ((drv->dhcp_sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
265*e28a4053SRui Paulo 		perror("socket call failed for dhcp");
266*e28a4053SRui Paulo 		return -1;
267*e28a4053SRui Paulo 	}
268*e28a4053SRui Paulo 
269*e28a4053SRui Paulo 	if (eloop_register_read_sock(drv->dhcp_sock, handle_dhcp, drv->ctx,
270*e28a4053SRui Paulo 				     NULL)) {
271*e28a4053SRui Paulo 		printf("Could not register read socket\n");
272*e28a4053SRui Paulo 		return -1;
273*e28a4053SRui Paulo 	}
274*e28a4053SRui Paulo 
275*e28a4053SRui Paulo 	os_memset(&addr2, 0, sizeof(addr2));
276*e28a4053SRui Paulo 	addr2.sin_family = AF_INET;
277*e28a4053SRui Paulo 	addr2.sin_port = htons(67);
278*e28a4053SRui Paulo 	addr2.sin_addr.s_addr = INADDR_ANY;
279*e28a4053SRui Paulo 
280*e28a4053SRui Paulo 	if (setsockopt(drv->dhcp_sock, SOL_SOCKET, SO_REUSEADDR, (char *) &n,
281*e28a4053SRui Paulo 		       sizeof(n)) == -1) {
282*e28a4053SRui Paulo 		perror("setsockopt[SOL_SOCKET,SO_REUSEADDR]");
283*e28a4053SRui Paulo 		return -1;
284*e28a4053SRui Paulo 	}
285*e28a4053SRui Paulo 	if (setsockopt(drv->dhcp_sock, SOL_SOCKET, SO_BROADCAST, (char *) &n,
286*e28a4053SRui Paulo 		       sizeof(n)) == -1) {
287*e28a4053SRui Paulo 		perror("setsockopt[SOL_SOCKET,SO_BROADCAST]");
288*e28a4053SRui Paulo 		return -1;
289*e28a4053SRui Paulo 	}
290*e28a4053SRui Paulo 
291*e28a4053SRui Paulo 	os_memset(&ifr, 0, sizeof(ifr));
292*e28a4053SRui Paulo 	os_strlcpy(ifr.ifr_ifrn.ifrn_name, drv->ifname, IFNAMSIZ);
293*e28a4053SRui Paulo 	if (setsockopt(drv->dhcp_sock, SOL_SOCKET, SO_BINDTODEVICE,
294*e28a4053SRui Paulo 		       (char *) &ifr, sizeof(ifr)) < 0) {
295*e28a4053SRui Paulo 		perror("setsockopt[SOL_SOCKET,SO_BINDTODEVICE]");
296*e28a4053SRui Paulo 		return -1;
297*e28a4053SRui Paulo 	}
298*e28a4053SRui Paulo 
299*e28a4053SRui Paulo 	if (bind(drv->dhcp_sock, (struct sockaddr *) &addr2,
300*e28a4053SRui Paulo 		 sizeof(struct sockaddr)) == -1) {
301*e28a4053SRui Paulo 		perror("bind");
302*e28a4053SRui Paulo 		return -1;
303*e28a4053SRui Paulo 	}
304*e28a4053SRui Paulo 
305*e28a4053SRui Paulo 	return 0;
306*e28a4053SRui Paulo #else /* __linux__ */
307*e28a4053SRui Paulo 	return -1;
308*e28a4053SRui Paulo #endif /* __linux__ */
309*e28a4053SRui Paulo }
310*e28a4053SRui Paulo 
311*e28a4053SRui Paulo 
312*e28a4053SRui Paulo static int wired_send_eapol(void *priv, const u8 *addr,
313*e28a4053SRui Paulo 			    const u8 *data, size_t data_len, int encrypt,
314*e28a4053SRui Paulo 			    const u8 *own_addr)
315*e28a4053SRui Paulo {
316*e28a4053SRui Paulo 	struct wpa_driver_wired_data *drv = priv;
317*e28a4053SRui Paulo 	struct ieee8023_hdr *hdr;
318*e28a4053SRui Paulo 	size_t len;
319*e28a4053SRui Paulo 	u8 *pos;
320*e28a4053SRui Paulo 	int res;
321*e28a4053SRui Paulo 
322*e28a4053SRui Paulo 	len = sizeof(*hdr) + data_len;
323*e28a4053SRui Paulo 	hdr = os_zalloc(len);
324*e28a4053SRui Paulo 	if (hdr == NULL) {
325*e28a4053SRui Paulo 		printf("malloc() failed for wired_send_eapol(len=%lu)\n",
326*e28a4053SRui Paulo 		       (unsigned long) len);
327*e28a4053SRui Paulo 		return -1;
328*e28a4053SRui Paulo 	}
329*e28a4053SRui Paulo 
330*e28a4053SRui Paulo 	os_memcpy(hdr->dest, drv->use_pae_group_addr ? pae_group_addr : addr,
331*e28a4053SRui Paulo 		  ETH_ALEN);
332*e28a4053SRui Paulo 	os_memcpy(hdr->src, own_addr, ETH_ALEN);
333*e28a4053SRui Paulo 	hdr->ethertype = htons(ETH_P_PAE);
334*e28a4053SRui Paulo 
335*e28a4053SRui Paulo 	pos = (u8 *) (hdr + 1);
336*e28a4053SRui Paulo 	os_memcpy(pos, data, data_len);
337*e28a4053SRui Paulo 
338*e28a4053SRui Paulo 	res = send(drv->sock, (u8 *) hdr, len, 0);
339*e28a4053SRui Paulo 	os_free(hdr);
340*e28a4053SRui Paulo 
341*e28a4053SRui Paulo 	if (res < 0) {
342*e28a4053SRui Paulo 		perror("wired_send_eapol: send");
343*e28a4053SRui Paulo 		printf("wired_send_eapol - packet len: %lu - failed\n",
344*e28a4053SRui Paulo 		       (unsigned long) len);
345*e28a4053SRui Paulo 	}
346*e28a4053SRui Paulo 
347*e28a4053SRui Paulo 	return res;
348*e28a4053SRui Paulo }
349*e28a4053SRui Paulo 
350*e28a4053SRui Paulo 
351*e28a4053SRui Paulo static void * wired_driver_hapd_init(struct hostapd_data *hapd,
352*e28a4053SRui Paulo 				     struct wpa_init_params *params)
353*e28a4053SRui Paulo {
354*e28a4053SRui Paulo 	struct wpa_driver_wired_data *drv;
355*e28a4053SRui Paulo 
356*e28a4053SRui Paulo 	drv = os_zalloc(sizeof(struct wpa_driver_wired_data));
357*e28a4053SRui Paulo 	if (drv == NULL) {
358*e28a4053SRui Paulo 		printf("Could not allocate memory for wired driver data\n");
359*e28a4053SRui Paulo 		return NULL;
360*e28a4053SRui Paulo 	}
361*e28a4053SRui Paulo 
362*e28a4053SRui Paulo 	drv->ctx = hapd;
363*e28a4053SRui Paulo 	os_strlcpy(drv->ifname, params->ifname, sizeof(drv->ifname));
364*e28a4053SRui Paulo 	drv->use_pae_group_addr = params->use_pae_group_addr;
365*e28a4053SRui Paulo 
366*e28a4053SRui Paulo 	if (wired_init_sockets(drv, params->own_addr)) {
367*e28a4053SRui Paulo 		os_free(drv);
368*e28a4053SRui Paulo 		return NULL;
369*e28a4053SRui Paulo 	}
370*e28a4053SRui Paulo 
371*e28a4053SRui Paulo 	return drv;
372*e28a4053SRui Paulo }
373*e28a4053SRui Paulo 
374*e28a4053SRui Paulo 
375*e28a4053SRui Paulo static void wired_driver_hapd_deinit(void *priv)
376*e28a4053SRui Paulo {
377*e28a4053SRui Paulo 	struct wpa_driver_wired_data *drv = priv;
378*e28a4053SRui Paulo 
379*e28a4053SRui Paulo 	if (drv->sock >= 0)
380*e28a4053SRui Paulo 		close(drv->sock);
381*e28a4053SRui Paulo 
382*e28a4053SRui Paulo 	if (drv->dhcp_sock >= 0)
383*e28a4053SRui Paulo 		close(drv->dhcp_sock);
384*e28a4053SRui Paulo 
385*e28a4053SRui Paulo 	os_free(drv);
386*e28a4053SRui Paulo }
387*e28a4053SRui Paulo 
388*e28a4053SRui Paulo 
3893157ba21SRui Paulo static int wpa_driver_wired_get_ssid(void *priv, u8 *ssid)
3903157ba21SRui Paulo {
3913157ba21SRui Paulo 	ssid[0] = 0;
3923157ba21SRui Paulo 	return 0;
3933157ba21SRui Paulo }
3943157ba21SRui Paulo 
3953157ba21SRui Paulo 
3963157ba21SRui Paulo static int wpa_driver_wired_get_bssid(void *priv, u8 *bssid)
3973157ba21SRui Paulo {
3983157ba21SRui Paulo 	/* Report PAE group address as the "BSSID" for wired connection. */
3993157ba21SRui Paulo 	os_memcpy(bssid, pae_group_addr, ETH_ALEN);
4003157ba21SRui Paulo 	return 0;
4013157ba21SRui Paulo }
4023157ba21SRui Paulo 
4033157ba21SRui Paulo 
404*e28a4053SRui Paulo static int wpa_driver_wired_get_capa(void *priv, struct wpa_driver_capa *capa)
405*e28a4053SRui Paulo {
406*e28a4053SRui Paulo 	os_memset(capa, 0, sizeof(*capa));
407*e28a4053SRui Paulo 	capa->flags = WPA_DRIVER_FLAGS_WIRED;
408*e28a4053SRui Paulo 	return 0;
409*e28a4053SRui Paulo }
410*e28a4053SRui Paulo 
411*e28a4053SRui Paulo 
4123157ba21SRui Paulo static int wpa_driver_wired_get_ifflags(const char *ifname, int *flags)
4133157ba21SRui Paulo {
4143157ba21SRui Paulo 	struct ifreq ifr;
4153157ba21SRui Paulo 	int s;
4163157ba21SRui Paulo 
4173157ba21SRui Paulo 	s = socket(PF_INET, SOCK_DGRAM, 0);
4183157ba21SRui Paulo 	if (s < 0) {
4193157ba21SRui Paulo 		perror("socket");
4203157ba21SRui Paulo 		return -1;
4213157ba21SRui Paulo 	}
4223157ba21SRui Paulo 
4233157ba21SRui Paulo 	os_memset(&ifr, 0, sizeof(ifr));
4243157ba21SRui Paulo 	os_strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
4253157ba21SRui Paulo 	if (ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
4263157ba21SRui Paulo 		perror("ioctl[SIOCGIFFLAGS]");
4273157ba21SRui Paulo 		close(s);
4283157ba21SRui Paulo 		return -1;
4293157ba21SRui Paulo 	}
4303157ba21SRui Paulo 	close(s);
4313157ba21SRui Paulo 	*flags = ifr.ifr_flags & 0xffff;
4323157ba21SRui Paulo 	return 0;
4333157ba21SRui Paulo }
4343157ba21SRui Paulo 
4353157ba21SRui Paulo 
4363157ba21SRui Paulo static int wpa_driver_wired_set_ifflags(const char *ifname, int flags)
4373157ba21SRui Paulo {
4383157ba21SRui Paulo 	struct ifreq ifr;
4393157ba21SRui Paulo 	int s;
4403157ba21SRui Paulo 
4413157ba21SRui Paulo 	s = socket(PF_INET, SOCK_DGRAM, 0);
4423157ba21SRui Paulo 	if (s < 0) {
4433157ba21SRui Paulo 		perror("socket");
4443157ba21SRui Paulo 		return -1;
4453157ba21SRui Paulo 	}
4463157ba21SRui Paulo 
4473157ba21SRui Paulo 	os_memset(&ifr, 0, sizeof(ifr));
4483157ba21SRui Paulo 	os_strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
4493157ba21SRui Paulo 	ifr.ifr_flags = flags & 0xffff;
4503157ba21SRui Paulo 	if (ioctl(s, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
4513157ba21SRui Paulo 		perror("ioctl[SIOCSIFFLAGS]");
4523157ba21SRui Paulo 		close(s);
4533157ba21SRui Paulo 		return -1;
4543157ba21SRui Paulo 	}
4553157ba21SRui Paulo 	close(s);
4563157ba21SRui Paulo 	return 0;
4573157ba21SRui Paulo }
4583157ba21SRui Paulo 
4593157ba21SRui Paulo 
4603157ba21SRui Paulo static int wpa_driver_wired_multi(const char *ifname, const u8 *addr, int add)
4613157ba21SRui Paulo {
4623157ba21SRui Paulo 	struct ifreq ifr;
4633157ba21SRui Paulo 	int s;
4643157ba21SRui Paulo 
4653157ba21SRui Paulo 	s = socket(PF_INET, SOCK_DGRAM, 0);
4663157ba21SRui Paulo 	if (s < 0) {
4673157ba21SRui Paulo 		perror("socket");
4683157ba21SRui Paulo 		return -1;
4693157ba21SRui Paulo 	}
4703157ba21SRui Paulo 
4713157ba21SRui Paulo 	os_memset(&ifr, 0, sizeof(ifr));
4723157ba21SRui Paulo 	os_strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
4733157ba21SRui Paulo #ifdef __linux__
4743157ba21SRui Paulo 	ifr.ifr_hwaddr.sa_family = AF_UNSPEC;
4753157ba21SRui Paulo 	os_memcpy(ifr.ifr_hwaddr.sa_data, addr, ETH_ALEN);
4763157ba21SRui Paulo #endif /* __linux__ */
477*e28a4053SRui Paulo #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
4783157ba21SRui Paulo 	{
4793157ba21SRui Paulo 		struct sockaddr_dl *dlp;
4803157ba21SRui Paulo 		dlp = (struct sockaddr_dl *) &ifr.ifr_addr;
4813157ba21SRui Paulo 		dlp->sdl_len = sizeof(struct sockaddr_dl);
4823157ba21SRui Paulo 		dlp->sdl_family = AF_LINK;
4833157ba21SRui Paulo 		dlp->sdl_index = 0;
4843157ba21SRui Paulo 		dlp->sdl_nlen = 0;
4853157ba21SRui Paulo 		dlp->sdl_alen = ETH_ALEN;
4863157ba21SRui Paulo 		dlp->sdl_slen = 0;
4873157ba21SRui Paulo 		os_memcpy(LLADDR(dlp), addr, ETH_ALEN);
4883157ba21SRui Paulo 	}
489*e28a4053SRui Paulo #endif /* defined(__FreeBSD__) || defined(__DragonFly__) || defined(FreeBSD_kernel__) */
4903157ba21SRui Paulo #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__APPLE__)
4913157ba21SRui Paulo 	{
4923157ba21SRui Paulo 		struct sockaddr *sap;
4933157ba21SRui Paulo 		sap = (struct sockaddr *) &ifr.ifr_addr;
4943157ba21SRui Paulo 		sap->sa_len = sizeof(struct sockaddr);
4953157ba21SRui Paulo 		sap->sa_family = AF_UNSPEC;
4963157ba21SRui Paulo 		os_memcpy(sap->sa_data, addr, ETH_ALEN);
4973157ba21SRui Paulo 	}
4983157ba21SRui Paulo #endif /* defined(__NetBSD__) || defined(__OpenBSD__) || defined(__APPLE__) */
4993157ba21SRui Paulo 
5003157ba21SRui Paulo 	if (ioctl(s, add ? SIOCADDMULTI : SIOCDELMULTI, (caddr_t) &ifr) < 0) {
5013157ba21SRui Paulo 		perror("ioctl[SIOC{ADD/DEL}MULTI]");
5023157ba21SRui Paulo 		close(s);
5033157ba21SRui Paulo 		return -1;
5043157ba21SRui Paulo 	}
5053157ba21SRui Paulo 	close(s);
5063157ba21SRui Paulo 	return 0;
5073157ba21SRui Paulo }
5083157ba21SRui Paulo 
5093157ba21SRui Paulo 
5103157ba21SRui Paulo static void * wpa_driver_wired_init(void *ctx, const char *ifname)
5113157ba21SRui Paulo {
5123157ba21SRui Paulo 	struct wpa_driver_wired_data *drv;
5133157ba21SRui Paulo 	int flags;
5143157ba21SRui Paulo 
5153157ba21SRui Paulo 	drv = os_zalloc(sizeof(*drv));
5163157ba21SRui Paulo 	if (drv == NULL)
5173157ba21SRui Paulo 		return NULL;
5183157ba21SRui Paulo 	os_strlcpy(drv->ifname, ifname, sizeof(drv->ifname));
5193157ba21SRui Paulo 	drv->ctx = ctx;
5203157ba21SRui Paulo 
5213157ba21SRui Paulo #ifdef __linux__
5223157ba21SRui Paulo 	drv->pf_sock = socket(PF_PACKET, SOCK_DGRAM, 0);
5233157ba21SRui Paulo 	if (drv->pf_sock < 0)
5243157ba21SRui Paulo 		perror("socket(PF_PACKET)");
5253157ba21SRui Paulo #else /* __linux__ */
5263157ba21SRui Paulo 	drv->pf_sock = -1;
5273157ba21SRui Paulo #endif /* __linux__ */
5283157ba21SRui Paulo 
5293157ba21SRui Paulo 	if (wpa_driver_wired_get_ifflags(ifname, &flags) == 0 &&
5303157ba21SRui Paulo 	    !(flags & IFF_UP) &&
5313157ba21SRui Paulo 	    wpa_driver_wired_set_ifflags(ifname, flags | IFF_UP) == 0) {
5323157ba21SRui Paulo 		drv->iff_up = 1;
5333157ba21SRui Paulo 	}
5343157ba21SRui Paulo 
535*e28a4053SRui Paulo 	if (wired_multicast_membership(drv->pf_sock,
536*e28a4053SRui Paulo 				       if_nametoindex(drv->ifname),
537*e28a4053SRui Paulo 				       pae_group_addr, 1) == 0) {
5383157ba21SRui Paulo 		wpa_printf(MSG_DEBUG, "%s: Added multicast membership with "
5393157ba21SRui Paulo 			   "packet socket", __func__);
5403157ba21SRui Paulo 		drv->membership = 1;
5413157ba21SRui Paulo 	} else if (wpa_driver_wired_multi(ifname, pae_group_addr, 1) == 0) {
5423157ba21SRui Paulo 		wpa_printf(MSG_DEBUG, "%s: Added multicast membership with "
5433157ba21SRui Paulo 			   "SIOCADDMULTI", __func__);
5443157ba21SRui Paulo 		drv->multi = 1;
5453157ba21SRui Paulo 	} else if (wpa_driver_wired_get_ifflags(ifname, &flags) < 0) {
5463157ba21SRui Paulo 		wpa_printf(MSG_INFO, "%s: Could not get interface "
5473157ba21SRui Paulo 			   "flags", __func__);
5483157ba21SRui Paulo 		os_free(drv);
5493157ba21SRui Paulo 		return NULL;
5503157ba21SRui Paulo 	} else if (flags & IFF_ALLMULTI) {
5513157ba21SRui Paulo 		wpa_printf(MSG_DEBUG, "%s: Interface is already configured "
5523157ba21SRui Paulo 			   "for multicast", __func__);
5533157ba21SRui Paulo 	} else if (wpa_driver_wired_set_ifflags(ifname,
5543157ba21SRui Paulo 						flags | IFF_ALLMULTI) < 0) {
5553157ba21SRui Paulo 		wpa_printf(MSG_INFO, "%s: Failed to enable allmulti",
5563157ba21SRui Paulo 			   __func__);
5573157ba21SRui Paulo 		os_free(drv);
5583157ba21SRui Paulo 		return NULL;
5593157ba21SRui Paulo 	} else {
5603157ba21SRui Paulo 		wpa_printf(MSG_DEBUG, "%s: Enabled allmulti mode",
5613157ba21SRui Paulo 			   __func__);
5623157ba21SRui Paulo 		drv->iff_allmulti = 1;
5633157ba21SRui Paulo 	}
5643157ba21SRui Paulo 
5653157ba21SRui Paulo 	return drv;
5663157ba21SRui Paulo }
5673157ba21SRui Paulo 
5683157ba21SRui Paulo 
5693157ba21SRui Paulo static void wpa_driver_wired_deinit(void *priv)
5703157ba21SRui Paulo {
5713157ba21SRui Paulo 	struct wpa_driver_wired_data *drv = priv;
5723157ba21SRui Paulo 	int flags;
5733157ba21SRui Paulo 
5743157ba21SRui Paulo 	if (drv->membership &&
575*e28a4053SRui Paulo 	    wired_multicast_membership(drv->pf_sock,
576*e28a4053SRui Paulo 				       if_nametoindex(drv->ifname),
577*e28a4053SRui Paulo 				       pae_group_addr, 0) < 0) {
5783157ba21SRui Paulo 		wpa_printf(MSG_DEBUG, "%s: Failed to remove PAE multicast "
5793157ba21SRui Paulo 			   "group (PACKET)", __func__);
5803157ba21SRui Paulo 	}
5813157ba21SRui Paulo 
5823157ba21SRui Paulo 	if (drv->multi &&
5833157ba21SRui Paulo 	    wpa_driver_wired_multi(drv->ifname, pae_group_addr, 0) < 0) {
5843157ba21SRui Paulo 		wpa_printf(MSG_DEBUG, "%s: Failed to remove PAE multicast "
5853157ba21SRui Paulo 			   "group (SIOCDELMULTI)", __func__);
5863157ba21SRui Paulo 	}
5873157ba21SRui Paulo 
5883157ba21SRui Paulo 	if (drv->iff_allmulti &&
5893157ba21SRui Paulo 	    (wpa_driver_wired_get_ifflags(drv->ifname, &flags) < 0 ||
5903157ba21SRui Paulo 	     wpa_driver_wired_set_ifflags(drv->ifname,
5913157ba21SRui Paulo 					  flags & ~IFF_ALLMULTI) < 0)) {
5923157ba21SRui Paulo 		wpa_printf(MSG_DEBUG, "%s: Failed to disable allmulti mode",
5933157ba21SRui Paulo 			   __func__);
5943157ba21SRui Paulo 	}
5953157ba21SRui Paulo 
5963157ba21SRui Paulo 	if (drv->iff_up &&
5973157ba21SRui Paulo 	    wpa_driver_wired_get_ifflags(drv->ifname, &flags) == 0 &&
5983157ba21SRui Paulo 	    (flags & IFF_UP) &&
5993157ba21SRui Paulo 	    wpa_driver_wired_set_ifflags(drv->ifname, flags & ~IFF_UP) < 0) {
6003157ba21SRui Paulo 		wpa_printf(MSG_DEBUG, "%s: Failed to set the interface down",
6013157ba21SRui Paulo 			   __func__);
6023157ba21SRui Paulo 	}
6033157ba21SRui Paulo 
6043157ba21SRui Paulo 	if (drv->pf_sock != -1)
6053157ba21SRui Paulo 		close(drv->pf_sock);
6063157ba21SRui Paulo 
6073157ba21SRui Paulo 	os_free(drv);
6083157ba21SRui Paulo }
6093157ba21SRui Paulo 
6103157ba21SRui Paulo 
6113157ba21SRui Paulo const struct wpa_driver_ops wpa_driver_wired_ops = {
6123157ba21SRui Paulo 	.name = "wired",
613*e28a4053SRui Paulo 	.desc = "Wired Ethernet driver",
614*e28a4053SRui Paulo 	.hapd_init = wired_driver_hapd_init,
615*e28a4053SRui Paulo 	.hapd_deinit = wired_driver_hapd_deinit,
616*e28a4053SRui Paulo 	.hapd_send_eapol = wired_send_eapol,
6173157ba21SRui Paulo 	.get_ssid = wpa_driver_wired_get_ssid,
6183157ba21SRui Paulo 	.get_bssid = wpa_driver_wired_get_bssid,
619*e28a4053SRui Paulo 	.get_capa = wpa_driver_wired_get_capa,
6203157ba21SRui Paulo 	.init = wpa_driver_wired_init,
6213157ba21SRui Paulo 	.deinit = wpa_driver_wired_deinit,
6223157ba21SRui Paulo };
623