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__ */
29 static const u8 pae_group_addr
[ETH_ALEN
] =
30 { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x03 };
33 struct wpa_driver_wired_data
{
36 char ifname
[IFNAMSIZ
+ 1];
37 int membership
, multi
, iff_allmulti
, iff_up
;
41 static int wpa_driver_wired_get_ssid(void *priv
, u8
*ssid
)
48 static int wpa_driver_wired_get_bssid(void *priv
, u8
*bssid
)
50 /* Report PAE group address as the "BSSID" for wired connection. */
51 os_memcpy(bssid
, pae_group_addr
, ETH_ALEN
);
56 static int wpa_driver_wired_get_ifflags(const char *ifname
, int *flags
)
61 s
= socket(PF_INET
, SOCK_DGRAM
, 0);
67 os_memset(&ifr
, 0, sizeof(ifr
));
68 os_strlcpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
69 if (ioctl(s
, SIOCGIFFLAGS
, (caddr_t
) &ifr
) < 0) {
70 perror("ioctl[SIOCGIFFLAGS]");
75 *flags
= ifr
.ifr_flags
& 0xffff;
80 static int wpa_driver_wired_set_ifflags(const char *ifname
, int flags
)
85 s
= socket(PF_INET
, SOCK_DGRAM
, 0);
91 os_memset(&ifr
, 0, sizeof(ifr
));
92 os_strlcpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
93 ifr
.ifr_flags
= flags
& 0xffff;
94 if (ioctl(s
, SIOCSIFFLAGS
, (caddr_t
) &ifr
) < 0) {
95 perror("ioctl[SIOCSIFFLAGS]");
104 static int wpa_driver_wired_multi(const char *ifname
, const u8
*addr
, int add
)
109 s
= socket(PF_INET
, SOCK_DGRAM
, 0);
115 os_memset(&ifr
, 0, sizeof(ifr
));
116 os_strlcpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
118 ifr
.ifr_hwaddr
.sa_family
= AF_UNSPEC
;
119 os_memcpy(ifr
.ifr_hwaddr
.sa_data
, addr
, ETH_ALEN
);
120 #endif /* __linux__ */
123 struct sockaddr_dl
*dlp
;
124 dlp
= (struct sockaddr_dl
*) &ifr
.ifr_addr
;
125 dlp
->sdl_len
= sizeof(struct sockaddr_dl
);
126 dlp
->sdl_family
= AF_LINK
;
129 dlp
->sdl_alen
= ETH_ALEN
;
131 os_memcpy(LLADDR(dlp
), addr
, ETH_ALEN
);
133 #endif /* __FreeBSD__ */
135 if (ioctl(s
, add
? SIOCADDMULTI
: SIOCDELMULTI
, (caddr_t
) &ifr
) < 0) {
136 perror("ioctl[SIOC{ADD/DEL}MULTI]");
145 static int wpa_driver_wired_membership(struct wpa_driver_wired_data
*drv
,
146 const u8
*addr
, int add
)
149 struct packet_mreq mreq
;
151 if (drv
->pf_sock
== -1)
154 os_memset(&mreq
, 0, sizeof(mreq
));
155 mreq
.mr_ifindex
= if_nametoindex(drv
->ifname
);
156 mreq
.mr_type
= PACKET_MR_MULTICAST
;
157 mreq
.mr_alen
= ETH_ALEN
;
158 os_memcpy(mreq
.mr_address
, addr
, ETH_ALEN
);
160 if (setsockopt(drv
->pf_sock
, SOL_PACKET
,
161 add
? PACKET_ADD_MEMBERSHIP
: PACKET_DROP_MEMBERSHIP
,
162 &mreq
, sizeof(mreq
)) < 0) {
163 perror("setsockopt");
167 #else /* __linux__ */
169 #endif /* __linux__ */
173 static void * wpa_driver_wired_init(void *ctx
, const char *ifname
)
175 struct wpa_driver_wired_data
*drv
;
178 drv
= os_zalloc(sizeof(*drv
));
181 os_strlcpy(drv
->ifname
, ifname
, sizeof(drv
->ifname
));
185 drv
->pf_sock
= socket(PF_PACKET
, SOCK_DGRAM
, 0);
186 if (drv
->pf_sock
< 0)
187 perror("socket(PF_PACKET)");
188 #else /* __linux__ */
190 #endif /* __linux__ */
192 if (wpa_driver_wired_get_ifflags(ifname
, &flags
) == 0 &&
194 wpa_driver_wired_set_ifflags(ifname
, flags
| IFF_UP
) == 0) {
198 if (wpa_driver_wired_membership(drv
, pae_group_addr
, 1) == 0) {
199 wpa_printf(MSG_DEBUG
, "%s: Added multicast membership with "
200 "packet socket", __func__
);
202 } else if (wpa_driver_wired_multi(ifname
, pae_group_addr
, 1) == 0) {
203 wpa_printf(MSG_DEBUG
, "%s: Added multicast membership with "
204 "SIOCADDMULTI", __func__
);
206 } else if (wpa_driver_wired_get_ifflags(ifname
, &flags
) < 0) {
207 wpa_printf(MSG_INFO
, "%s: Could not get interface "
211 } else if (flags
& IFF_ALLMULTI
) {
212 wpa_printf(MSG_DEBUG
, "%s: Interface is already configured "
213 "for multicast", __func__
);
214 } else if (wpa_driver_wired_set_ifflags(ifname
,
215 flags
| IFF_ALLMULTI
) < 0) {
216 wpa_printf(MSG_INFO
, "%s: Failed to enable allmulti",
221 wpa_printf(MSG_DEBUG
, "%s: Enabled allmulti mode",
223 drv
->iff_allmulti
= 1;
230 static void wpa_driver_wired_deinit(void *priv
)
232 struct wpa_driver_wired_data
*drv
= priv
;
235 if (drv
->membership
&&
236 wpa_driver_wired_membership(drv
, pae_group_addr
, 0) < 0) {
237 wpa_printf(MSG_DEBUG
, "%s: Failed to remove PAE multicast "
238 "group (PACKET)", __func__
);
242 wpa_driver_wired_multi(drv
->ifname
, pae_group_addr
, 0) < 0) {
243 wpa_printf(MSG_DEBUG
, "%s: Failed to remove PAE multicast "
244 "group (SIOCDELMULTI)", __func__
);
247 if (drv
->iff_allmulti
&&
248 (wpa_driver_wired_get_ifflags(drv
->ifname
, &flags
) < 0 ||
249 wpa_driver_wired_set_ifflags(drv
->ifname
,
250 flags
& ~IFF_ALLMULTI
) < 0)) {
251 wpa_printf(MSG_DEBUG
, "%s: Failed to disable allmulti mode",
256 wpa_driver_wired_get_ifflags(drv
->ifname
, &flags
) == 0 &&
258 wpa_driver_wired_set_ifflags(drv
->ifname
, flags
& ~IFF_UP
) < 0) {
259 wpa_printf(MSG_DEBUG
, "%s: Failed to set the interface down",
263 if (drv
->pf_sock
!= -1)
270 const struct wpa_driver_ops wpa_driver_wired_ops
= {
272 .desc
= "wpa_supplicant wired Ethernet driver",
273 .get_ssid
= wpa_driver_wired_get_ssid
,
274 .get_bssid
= wpa_driver_wired_get_bssid
,
275 .init
= wpa_driver_wired_init
,
276 .deinit
= wpa_driver_wired_deinit
,