2 * WPA Supplicant - wired Ethernet driver interface
3 * Copyright (c) 2005-2007, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
16 #include <sys/ioctl.h>
19 #include <netpacket/packet.h>
20 #endif /* __linux__ */
22 #include <net/if_dl.h>
23 #endif /* __FreeBSD__ */
27 #include "wpa_supplicant.h"
30 static const u8 pae_group_addr
[ETH_ALEN
] =
31 { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x03 };
34 struct wpa_driver_wired_data
{
37 char ifname
[IFNAMSIZ
+ 1];
38 int membership
, multi
, iff_allmulti
, iff_up
;
42 static int wpa_driver_wired_get_ssid(void *priv
, u8
*ssid
)
49 static int wpa_driver_wired_get_bssid(void *priv
, u8
*bssid
)
51 /* Report PAE group address as the "BSSID" for wired connection. */
52 os_memcpy(bssid
, pae_group_addr
, ETH_ALEN
);
57 static int wpa_driver_wired_get_ifflags(const char *ifname
, int *flags
)
62 s
= socket(PF_INET
, SOCK_DGRAM
, 0);
68 os_memset(&ifr
, 0, sizeof(ifr
));
69 os_strncpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
70 if (ioctl(s
, SIOCGIFFLAGS
, (caddr_t
) &ifr
) < 0) {
71 perror("ioctl[SIOCGIFFLAGS]");
76 *flags
= ifr
.ifr_flags
& 0xffff;
81 static int wpa_driver_wired_set_ifflags(const char *ifname
, int flags
)
86 s
= socket(PF_INET
, SOCK_DGRAM
, 0);
92 os_memset(&ifr
, 0, sizeof(ifr
));
93 os_strncpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
94 ifr
.ifr_flags
= flags
& 0xffff;
95 if (ioctl(s
, SIOCSIFFLAGS
, (caddr_t
) &ifr
) < 0) {
96 perror("ioctl[SIOCSIFFLAGS]");
105 static int wpa_driver_wired_multi(const char *ifname
, const u8
*addr
, int add
)
110 s
= socket(PF_INET
, SOCK_DGRAM
, 0);
116 os_memset(&ifr
, 0, sizeof(ifr
));
117 os_strncpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
119 ifr
.ifr_hwaddr
.sa_family
= AF_UNSPEC
;
120 os_memcpy(ifr
.ifr_hwaddr
.sa_data
, addr
, ETH_ALEN
);
121 #endif /* __linux__ */
124 struct sockaddr_dl
*dlp
;
125 dlp
= (struct sockaddr_dl
*) &ifr
.ifr_addr
;
126 dlp
->sdl_len
= sizeof(struct sockaddr_dl
);
127 dlp
->sdl_family
= AF_LINK
;
130 dlp
->sdl_alen
= ETH_ALEN
;
132 os_memcpy(LLADDR(dlp
), addr
, ETH_ALEN
);
134 #endif /* __FreeBSD__ */
136 if (ioctl(s
, add
? SIOCADDMULTI
: SIOCDELMULTI
, (caddr_t
) &ifr
) < 0) {
137 perror("ioctl[SIOC{ADD/DEL}MULTI]");
146 static int wpa_driver_wired_membership(struct wpa_driver_wired_data
*drv
,
147 const u8
*addr
, int add
)
150 struct packet_mreq mreq
;
152 if (drv
->pf_sock
== -1)
155 os_memset(&mreq
, 0, sizeof(mreq
));
156 mreq
.mr_ifindex
= if_nametoindex(drv
->ifname
);
157 mreq
.mr_type
= PACKET_MR_MULTICAST
;
158 mreq
.mr_alen
= ETH_ALEN
;
159 os_memcpy(mreq
.mr_address
, addr
, ETH_ALEN
);
161 if (setsockopt(drv
->pf_sock
, SOL_PACKET
,
162 add
? PACKET_ADD_MEMBERSHIP
: PACKET_DROP_MEMBERSHIP
,
163 &mreq
, sizeof(mreq
)) < 0) {
164 perror("setsockopt");
168 #else /* __linux__ */
170 #endif /* __linux__ */
174 static void * wpa_driver_wired_init(void *ctx
, const char *ifname
)
176 struct wpa_driver_wired_data
*drv
;
179 drv
= os_zalloc(sizeof(*drv
));
182 os_strncpy(drv
->ifname
, ifname
, sizeof(drv
->ifname
));
186 drv
->pf_sock
= socket(PF_PACKET
, SOCK_DGRAM
, 0);
187 if (drv
->pf_sock
< 0)
188 perror("socket(PF_PACKET)");
189 #else /* __linux__ */
191 #endif /* __linux__ */
193 if (wpa_driver_wired_get_ifflags(ifname
, &flags
) == 0 &&
195 wpa_driver_wired_set_ifflags(ifname
, flags
| IFF_UP
) == 0) {
199 if (wpa_driver_wired_membership(drv
, pae_group_addr
, 1) == 0) {
200 wpa_printf(MSG_DEBUG
, "%s: Added multicast membership with "
201 "packet socket", __func__
);
203 } else if (wpa_driver_wired_multi(ifname
, pae_group_addr
, 1) == 0) {
204 wpa_printf(MSG_DEBUG
, "%s: Added multicast membership with "
205 "SIOCADDMULTI", __func__
);
207 } else if (wpa_driver_wired_get_ifflags(ifname
, &flags
) < 0) {
208 wpa_printf(MSG_INFO
, "%s: Could not get interface "
212 } else if (flags
& IFF_ALLMULTI
) {
213 wpa_printf(MSG_DEBUG
, "%s: Interface is already configured "
214 "for multicast", __func__
);
215 } else if (wpa_driver_wired_set_ifflags(ifname
,
216 flags
| IFF_ALLMULTI
) < 0) {
217 wpa_printf(MSG_INFO
, "%s: Failed to enable allmulti",
222 wpa_printf(MSG_DEBUG
, "%s: Enabled allmulti mode",
224 drv
->iff_allmulti
= 1;
231 static void wpa_driver_wired_deinit(void *priv
)
233 struct wpa_driver_wired_data
*drv
= priv
;
236 if (drv
->membership
&&
237 wpa_driver_wired_membership(drv
, pae_group_addr
, 0) < 0) {
238 wpa_printf(MSG_DEBUG
, "%s: Failed to remove PAE multicast "
239 "group (PACKET)", __func__
);
243 wpa_driver_wired_multi(drv
->ifname
, pae_group_addr
, 0) < 0) {
244 wpa_printf(MSG_DEBUG
, "%s: Failed to remove PAE multicast "
245 "group (SIOCDELMULTI)", __func__
);
248 if (drv
->iff_allmulti
&&
249 (wpa_driver_wired_get_ifflags(drv
->ifname
, &flags
) < 0 ||
250 wpa_driver_wired_set_ifflags(drv
->ifname
,
251 flags
& ~IFF_ALLMULTI
) < 0)) {
252 wpa_printf(MSG_DEBUG
, "%s: Failed to disable allmulti mode",
257 wpa_driver_wired_get_ifflags(drv
->ifname
, &flags
) == 0 &&
259 wpa_driver_wired_set_ifflags(drv
->ifname
, flags
& ~IFF_UP
) < 0) {
260 wpa_printf(MSG_DEBUG
, "%s: Failed to set the interface down",
264 if (drv
->pf_sock
!= -1)
271 const struct wpa_driver_ops wpa_driver_wired_ops
= {
273 .desc
= "wpa_supplicant wired Ethernet driver",
274 .get_ssid
= wpa_driver_wired_get_ssid
,
275 .get_bssid
= wpa_driver_wired_get_bssid
,
276 .init
= wpa_driver_wired_init
,
277 .deinit
= wpa_driver_wired_deinit
,