1*0a73ee0aSchristos /*
2*0a73ee0aSchristos * Common functions for Wired Ethernet driver interfaces
3*0a73ee0aSchristos * Copyright (c) 2005-2009, Jouni Malinen <j@w1.fi>
4*0a73ee0aSchristos * Copyright (c) 2004, Gunter Burchardt <tira@isx.de>
5*0a73ee0aSchristos *
6*0a73ee0aSchristos * This software may be distributed under the terms of the BSD license.
7*0a73ee0aSchristos * See README for more details.
8*0a73ee0aSchristos */
9*0a73ee0aSchristos
10*0a73ee0aSchristos #include "includes.h"
11*0a73ee0aSchristos
12*0a73ee0aSchristos #include "common.h"
13*0a73ee0aSchristos #include "eloop.h"
14*0a73ee0aSchristos #include "driver.h"
15*0a73ee0aSchristos #include "driver_wired_common.h"
16*0a73ee0aSchristos
17*0a73ee0aSchristos #include <sys/ioctl.h>
18*0a73ee0aSchristos #include <net/if.h>
19*0a73ee0aSchristos #ifdef __linux__
20*0a73ee0aSchristos #include <netpacket/packet.h>
21*0a73ee0aSchristos #include <net/if_arp.h>
22*0a73ee0aSchristos #include <net/if.h>
23*0a73ee0aSchristos #endif /* __linux__ */
24*0a73ee0aSchristos #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
25*0a73ee0aSchristos #include <net/if_dl.h>
26*0a73ee0aSchristos #include <net/if_media.h>
27*0a73ee0aSchristos #endif /* defined(__FreeBSD__) || defined(__DragonFly__) || defined(__FreeBSD_kernel__) */
28*0a73ee0aSchristos #ifdef __sun__
29*0a73ee0aSchristos #include <sys/sockio.h>
30*0a73ee0aSchristos #endif /* __sun__ */
31*0a73ee0aSchristos
32*0a73ee0aSchristos
driver_wired_get_ifflags(const char * ifname,int * flags)33*0a73ee0aSchristos static int driver_wired_get_ifflags(const char *ifname, int *flags)
34*0a73ee0aSchristos {
35*0a73ee0aSchristos struct ifreq ifr;
36*0a73ee0aSchristos int s;
37*0a73ee0aSchristos
38*0a73ee0aSchristos s = socket(PF_INET, SOCK_DGRAM, 0);
39*0a73ee0aSchristos if (s < 0) {
40*0a73ee0aSchristos wpa_printf(MSG_ERROR, "socket: %s", strerror(errno));
41*0a73ee0aSchristos return -1;
42*0a73ee0aSchristos }
43*0a73ee0aSchristos
44*0a73ee0aSchristos os_memset(&ifr, 0, sizeof(ifr));
45*0a73ee0aSchristos os_strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
46*0a73ee0aSchristos if (ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
47*0a73ee0aSchristos wpa_printf(MSG_ERROR, "ioctl[SIOCGIFFLAGS]: %s",
48*0a73ee0aSchristos strerror(errno));
49*0a73ee0aSchristos close(s);
50*0a73ee0aSchristos return -1;
51*0a73ee0aSchristos }
52*0a73ee0aSchristos close(s);
53*0a73ee0aSchristos *flags = ifr.ifr_flags & 0xffff;
54*0a73ee0aSchristos return 0;
55*0a73ee0aSchristos }
56*0a73ee0aSchristos
57*0a73ee0aSchristos
driver_wired_set_ifflags(const char * ifname,int flags)58*0a73ee0aSchristos static int driver_wired_set_ifflags(const char *ifname, int flags)
59*0a73ee0aSchristos {
60*0a73ee0aSchristos struct ifreq ifr;
61*0a73ee0aSchristos int s;
62*0a73ee0aSchristos
63*0a73ee0aSchristos s = socket(PF_INET, SOCK_DGRAM, 0);
64*0a73ee0aSchristos if (s < 0) {
65*0a73ee0aSchristos wpa_printf(MSG_ERROR, "socket: %s", strerror(errno));
66*0a73ee0aSchristos return -1;
67*0a73ee0aSchristos }
68*0a73ee0aSchristos
69*0a73ee0aSchristos os_memset(&ifr, 0, sizeof(ifr));
70*0a73ee0aSchristos os_strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
71*0a73ee0aSchristos ifr.ifr_flags = flags & 0xffff;
72*0a73ee0aSchristos if (ioctl(s, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
73*0a73ee0aSchristos wpa_printf(MSG_ERROR, "ioctl[SIOCSIFFLAGS]: %s",
74*0a73ee0aSchristos strerror(errno));
75*0a73ee0aSchristos close(s);
76*0a73ee0aSchristos return -1;
77*0a73ee0aSchristos }
78*0a73ee0aSchristos close(s);
79*0a73ee0aSchristos return 0;
80*0a73ee0aSchristos }
81*0a73ee0aSchristos
82*0a73ee0aSchristos
driver_wired_multi(const char * ifname,const u8 * addr,int add)83*0a73ee0aSchristos static int driver_wired_multi(const char *ifname, const u8 *addr, int add)
84*0a73ee0aSchristos {
85*0a73ee0aSchristos struct ifreq ifr;
86*0a73ee0aSchristos int s;
87*0a73ee0aSchristos
88*0a73ee0aSchristos #ifdef __sun__
89*0a73ee0aSchristos return -1;
90*0a73ee0aSchristos #endif /* __sun__ */
91*0a73ee0aSchristos
92*0a73ee0aSchristos s = socket(PF_INET, SOCK_DGRAM, 0);
93*0a73ee0aSchristos if (s < 0) {
94*0a73ee0aSchristos wpa_printf(MSG_ERROR, "socket: %s", strerror(errno));
95*0a73ee0aSchristos return -1;
96*0a73ee0aSchristos }
97*0a73ee0aSchristos
98*0a73ee0aSchristos os_memset(&ifr, 0, sizeof(ifr));
99*0a73ee0aSchristos os_strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
100*0a73ee0aSchristos #ifdef __linux__
101*0a73ee0aSchristos ifr.ifr_hwaddr.sa_family = AF_UNSPEC;
102*0a73ee0aSchristos os_memcpy(ifr.ifr_hwaddr.sa_data, addr, ETH_ALEN);
103*0a73ee0aSchristos #endif /* __linux__ */
104*0a73ee0aSchristos #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
105*0a73ee0aSchristos {
106*0a73ee0aSchristos struct sockaddr_dl *dlp;
107*0a73ee0aSchristos
108*0a73ee0aSchristos dlp = (struct sockaddr_dl *) &ifr.ifr_addr;
109*0a73ee0aSchristos dlp->sdl_len = sizeof(struct sockaddr_dl);
110*0a73ee0aSchristos dlp->sdl_family = AF_LINK;
111*0a73ee0aSchristos dlp->sdl_index = 0;
112*0a73ee0aSchristos dlp->sdl_nlen = 0;
113*0a73ee0aSchristos dlp->sdl_alen = ETH_ALEN;
114*0a73ee0aSchristos dlp->sdl_slen = 0;
115*0a73ee0aSchristos os_memcpy(LLADDR(dlp), addr, ETH_ALEN);
116*0a73ee0aSchristos }
117*0a73ee0aSchristos #endif /* defined(__FreeBSD__) || defined(__DragonFly__) || defined(FreeBSD_kernel__) */
118*0a73ee0aSchristos #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__APPLE__)
119*0a73ee0aSchristos {
120*0a73ee0aSchristos struct sockaddr *sap;
121*0a73ee0aSchristos
122*0a73ee0aSchristos sap = (struct sockaddr *) &ifr.ifr_addr;
123*0a73ee0aSchristos sap->sa_len = sizeof(struct sockaddr);
124*0a73ee0aSchristos sap->sa_family = AF_UNSPEC;
125*0a73ee0aSchristos os_memcpy(sap->sa_data, addr, ETH_ALEN);
126*0a73ee0aSchristos }
127*0a73ee0aSchristos #endif /* defined(__NetBSD__) || defined(__OpenBSD__) || defined(__APPLE__) */
128*0a73ee0aSchristos
129*0a73ee0aSchristos if (ioctl(s, add ? SIOCADDMULTI : SIOCDELMULTI, (caddr_t) &ifr) < 0) {
130*0a73ee0aSchristos wpa_printf(MSG_ERROR, "ioctl[SIOC{ADD/DEL}MULTI]: %s",
131*0a73ee0aSchristos strerror(errno));
132*0a73ee0aSchristos close(s);
133*0a73ee0aSchristos return -1;
134*0a73ee0aSchristos }
135*0a73ee0aSchristos close(s);
136*0a73ee0aSchristos return 0;
137*0a73ee0aSchristos }
138*0a73ee0aSchristos
139*0a73ee0aSchristos
wired_multicast_membership(int sock,int ifindex,const u8 * addr,int add)140*0a73ee0aSchristos int wired_multicast_membership(int sock, int ifindex, const u8 *addr, int add)
141*0a73ee0aSchristos {
142*0a73ee0aSchristos #ifdef __linux__
143*0a73ee0aSchristos struct packet_mreq mreq;
144*0a73ee0aSchristos
145*0a73ee0aSchristos if (sock < 0)
146*0a73ee0aSchristos return -1;
147*0a73ee0aSchristos
148*0a73ee0aSchristos os_memset(&mreq, 0, sizeof(mreq));
149*0a73ee0aSchristos mreq.mr_ifindex = ifindex;
150*0a73ee0aSchristos mreq.mr_type = PACKET_MR_MULTICAST;
151*0a73ee0aSchristos mreq.mr_alen = ETH_ALEN;
152*0a73ee0aSchristos os_memcpy(mreq.mr_address, addr, ETH_ALEN);
153*0a73ee0aSchristos
154*0a73ee0aSchristos if (setsockopt(sock, SOL_PACKET,
155*0a73ee0aSchristos add ? PACKET_ADD_MEMBERSHIP : PACKET_DROP_MEMBERSHIP,
156*0a73ee0aSchristos &mreq, sizeof(mreq)) < 0) {
157*0a73ee0aSchristos wpa_printf(MSG_ERROR, "setsockopt: %s", strerror(errno));
158*0a73ee0aSchristos return -1;
159*0a73ee0aSchristos }
160*0a73ee0aSchristos return 0;
161*0a73ee0aSchristos #else /* __linux__ */
162*0a73ee0aSchristos return -1;
163*0a73ee0aSchristos #endif /* __linux__ */
164*0a73ee0aSchristos }
165*0a73ee0aSchristos
166*0a73ee0aSchristos
driver_wired_get_ssid(void * priv,u8 * ssid)167*0a73ee0aSchristos int driver_wired_get_ssid(void *priv, u8 *ssid)
168*0a73ee0aSchristos {
169*0a73ee0aSchristos ssid[0] = 0;
170*0a73ee0aSchristos return 0;
171*0a73ee0aSchristos }
172*0a73ee0aSchristos
173*0a73ee0aSchristos
driver_wired_get_bssid(void * priv,u8 * bssid)174*0a73ee0aSchristos int driver_wired_get_bssid(void *priv, u8 *bssid)
175*0a73ee0aSchristos {
176*0a73ee0aSchristos /* Report PAE group address as the "BSSID" for wired connection. */
177*0a73ee0aSchristos os_memcpy(bssid, pae_group_addr, ETH_ALEN);
178*0a73ee0aSchristos return 0;
179*0a73ee0aSchristos }
180*0a73ee0aSchristos
181*0a73ee0aSchristos
driver_wired_get_capa(void * priv,struct wpa_driver_capa * capa)182*0a73ee0aSchristos int driver_wired_get_capa(void *priv, struct wpa_driver_capa *capa)
183*0a73ee0aSchristos {
184*0a73ee0aSchristos os_memset(capa, 0, sizeof(*capa));
185*0a73ee0aSchristos capa->flags = WPA_DRIVER_FLAGS_WIRED;
186*0a73ee0aSchristos return 0;
187*0a73ee0aSchristos }
188*0a73ee0aSchristos
189*0a73ee0aSchristos
190*0a73ee0aSchristos #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
driver_wired_get_ifstatus(const char * ifname,int * status)191*0a73ee0aSchristos static int driver_wired_get_ifstatus(const char *ifname, int *status)
192*0a73ee0aSchristos {
193*0a73ee0aSchristos struct ifmediareq ifmr;
194*0a73ee0aSchristos int s;
195*0a73ee0aSchristos
196*0a73ee0aSchristos s = socket(PF_INET, SOCK_DGRAM, 0);
197*0a73ee0aSchristos if (s < 0) {
198*0a73ee0aSchristos wpa_printf(MSG_ERROR, "socket: %s", strerror(errno));
199*0a73ee0aSchristos return -1;
200*0a73ee0aSchristos }
201*0a73ee0aSchristos
202*0a73ee0aSchristos os_memset(&ifmr, 0, sizeof(ifmr));
203*0a73ee0aSchristos os_strlcpy(ifmr.ifm_name, ifname, IFNAMSIZ);
204*0a73ee0aSchristos if (ioctl(s, SIOCGIFMEDIA, (caddr_t) &ifmr) < 0) {
205*0a73ee0aSchristos wpa_printf(MSG_ERROR, "ioctl[SIOCGIFMEDIA]: %s",
206*0a73ee0aSchristos strerror(errno));
207*0a73ee0aSchristos close(s);
208*0a73ee0aSchristos return -1;
209*0a73ee0aSchristos }
210*0a73ee0aSchristos close(s);
211*0a73ee0aSchristos *status = (ifmr.ifm_status & (IFM_ACTIVE | IFM_AVALID)) ==
212*0a73ee0aSchristos (IFM_ACTIVE | IFM_AVALID);
213*0a73ee0aSchristos
214*0a73ee0aSchristos return 0;
215*0a73ee0aSchristos }
216*0a73ee0aSchristos #endif /* defined(__FreeBSD__) || defined(__DragonFly__) || defined(FreeBSD_kernel__) */
217*0a73ee0aSchristos
218*0a73ee0aSchristos
driver_wired_init_common(struct driver_wired_common_data * common,const char * ifname,void * ctx)219*0a73ee0aSchristos int driver_wired_init_common(struct driver_wired_common_data *common,
220*0a73ee0aSchristos const char *ifname, void *ctx)
221*0a73ee0aSchristos {
222*0a73ee0aSchristos int flags;
223*0a73ee0aSchristos
224*0a73ee0aSchristos os_strlcpy(common->ifname, ifname, sizeof(common->ifname));
225*0a73ee0aSchristos common->ctx = ctx;
226*0a73ee0aSchristos
227*0a73ee0aSchristos #ifdef __linux__
228*0a73ee0aSchristos common->pf_sock = socket(PF_PACKET, SOCK_DGRAM, 0);
229*0a73ee0aSchristos if (common->pf_sock < 0)
230*0a73ee0aSchristos wpa_printf(MSG_ERROR, "socket(PF_PACKET): %s", strerror(errno));
231*0a73ee0aSchristos #else /* __linux__ */
232*0a73ee0aSchristos common->pf_sock = -1;
233*0a73ee0aSchristos #endif /* __linux__ */
234*0a73ee0aSchristos
235*0a73ee0aSchristos if (driver_wired_get_ifflags(ifname, &flags) == 0 &&
236*0a73ee0aSchristos !(flags & IFF_UP) &&
237*0a73ee0aSchristos driver_wired_set_ifflags(ifname, flags | IFF_UP) == 0)
238*0a73ee0aSchristos common->iff_up = 1;
239*0a73ee0aSchristos
240*0a73ee0aSchristos if (wired_multicast_membership(common->pf_sock,
241*0a73ee0aSchristos if_nametoindex(common->ifname),
242*0a73ee0aSchristos pae_group_addr, 1) == 0) {
243*0a73ee0aSchristos wpa_printf(MSG_DEBUG,
244*0a73ee0aSchristos "%s: Added multicast membership with packet socket",
245*0a73ee0aSchristos __func__);
246*0a73ee0aSchristos common->membership = 1;
247*0a73ee0aSchristos } else if (driver_wired_multi(ifname, pae_group_addr, 1) == 0) {
248*0a73ee0aSchristos wpa_printf(MSG_DEBUG,
249*0a73ee0aSchristos "%s: Added multicast membership with SIOCADDMULTI",
250*0a73ee0aSchristos __func__);
251*0a73ee0aSchristos common->multi = 1;
252*0a73ee0aSchristos } else if (driver_wired_get_ifflags(ifname, &flags) < 0) {
253*0a73ee0aSchristos wpa_printf(MSG_INFO, "%s: Could not get interface flags",
254*0a73ee0aSchristos __func__);
255*0a73ee0aSchristos return -1;
256*0a73ee0aSchristos } else if (flags & IFF_ALLMULTI) {
257*0a73ee0aSchristos wpa_printf(MSG_DEBUG,
258*0a73ee0aSchristos "%s: Interface is already configured for multicast",
259*0a73ee0aSchristos __func__);
260*0a73ee0aSchristos } else if (driver_wired_set_ifflags(ifname,
261*0a73ee0aSchristos flags | IFF_ALLMULTI) < 0) {
262*0a73ee0aSchristos wpa_printf(MSG_INFO, "%s: Failed to enable allmulti", __func__);
263*0a73ee0aSchristos return -1;
264*0a73ee0aSchristos } else {
265*0a73ee0aSchristos wpa_printf(MSG_DEBUG, "%s: Enabled allmulti mode", __func__);
266*0a73ee0aSchristos common->iff_allmulti = 1;
267*0a73ee0aSchristos }
268*0a73ee0aSchristos #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
269*0a73ee0aSchristos {
270*0a73ee0aSchristos int status;
271*0a73ee0aSchristos
272*0a73ee0aSchristos wpa_printf(MSG_DEBUG, "%s: waiting for link to become active",
273*0a73ee0aSchristos __func__);
274*0a73ee0aSchristos while (driver_wired_get_ifstatus(ifname, &status) == 0 &&
275*0a73ee0aSchristos status == 0)
276*0a73ee0aSchristos sleep(1);
277*0a73ee0aSchristos }
278*0a73ee0aSchristos #endif /* defined(__FreeBSD__) || defined(__DragonFly__) || defined(FreeBSD_kernel__) */
279*0a73ee0aSchristos
280*0a73ee0aSchristos return 0;
281*0a73ee0aSchristos }
282*0a73ee0aSchristos
283*0a73ee0aSchristos
driver_wired_deinit_common(struct driver_wired_common_data * common)284*0a73ee0aSchristos void driver_wired_deinit_common(struct driver_wired_common_data *common)
285*0a73ee0aSchristos {
286*0a73ee0aSchristos int flags;
287*0a73ee0aSchristos
288*0a73ee0aSchristos if (common->membership &&
289*0a73ee0aSchristos wired_multicast_membership(common->pf_sock,
290*0a73ee0aSchristos if_nametoindex(common->ifname),
291*0a73ee0aSchristos pae_group_addr, 0) < 0) {
292*0a73ee0aSchristos wpa_printf(MSG_DEBUG,
293*0a73ee0aSchristos "%s: Failed to remove PAE multicast group (PACKET)",
294*0a73ee0aSchristos __func__);
295*0a73ee0aSchristos }
296*0a73ee0aSchristos
297*0a73ee0aSchristos if (common->multi &&
298*0a73ee0aSchristos driver_wired_multi(common->ifname, pae_group_addr, 0) < 0) {
299*0a73ee0aSchristos wpa_printf(MSG_DEBUG,
300*0a73ee0aSchristos "%s: Failed to remove PAE multicast group (SIOCDELMULTI)",
301*0a73ee0aSchristos __func__);
302*0a73ee0aSchristos }
303*0a73ee0aSchristos
304*0a73ee0aSchristos if (common->iff_allmulti &&
305*0a73ee0aSchristos (driver_wired_get_ifflags(common->ifname, &flags) < 0 ||
306*0a73ee0aSchristos driver_wired_set_ifflags(common->ifname,
307*0a73ee0aSchristos flags & ~IFF_ALLMULTI) < 0)) {
308*0a73ee0aSchristos wpa_printf(MSG_DEBUG, "%s: Failed to disable allmulti mode",
309*0a73ee0aSchristos __func__);
310*0a73ee0aSchristos }
311*0a73ee0aSchristos
312*0a73ee0aSchristos if (common->iff_up &&
313*0a73ee0aSchristos driver_wired_get_ifflags(common->ifname, &flags) == 0 &&
314*0a73ee0aSchristos (flags & IFF_UP) &&
315*0a73ee0aSchristos driver_wired_set_ifflags(common->ifname, flags & ~IFF_UP) < 0) {
316*0a73ee0aSchristos wpa_printf(MSG_DEBUG, "%s: Failed to set the interface down",
317*0a73ee0aSchristos __func__);
318*0a73ee0aSchristos }
319*0a73ee0aSchristos
320*0a73ee0aSchristos if (common->pf_sock != -1)
321*0a73ee0aSchristos close(common->pf_sock);
322*0a73ee0aSchristos }
323