2 * WPA Supplicant - wired Ethernet driver interface
3 * Copyright (c) 2005, Jouni Malinen <jkmaline@cc.hut.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.
19 #include <sys/ioctl.h>
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <netpacket/packet.h>
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_set_wpa(void *priv
, int enabled
)
48 static int wpa_driver_wired_get_ssid(void *priv
, u8
*ssid
)
55 static int wpa_driver_wired_get_bssid(void *priv
, u8
*bssid
)
57 /* Report PAE group address as the "BSSID" for wired connection. */
58 memcpy(bssid
, pae_group_addr
, ETH_ALEN
);
63 static int wpa_driver_wired_get_ifflags(const char *ifname
, int *flags
)
68 s
= socket(PF_INET
, SOCK_DGRAM
, 0);
74 memset(&ifr
, 0, sizeof(ifr
));
75 strncpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
76 if (ioctl(s
, SIOCGIFFLAGS
, (caddr_t
) &ifr
) < 0) {
77 perror("ioctl[SIOCGIFFLAGS]");
82 *flags
= ifr
.ifr_flags
& 0xffff;
87 static int wpa_driver_wired_set_ifflags(const char *ifname
, int flags
)
92 s
= socket(PF_INET
, SOCK_DGRAM
, 0);
98 memset(&ifr
, 0, sizeof(ifr
));
99 strncpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
100 ifr
.ifr_flags
= flags
& 0xffff;
101 if (ioctl(s
, SIOCSIFFLAGS
, (caddr_t
) &ifr
) < 0) {
102 perror("ioctl[SIOCSIFFLAGS]");
111 static int wpa_driver_wired_multi(const char *ifname
, const u8
*addr
, int add
)
116 s
= socket(PF_INET
, SOCK_DGRAM
, 0);
122 memset(&ifr
, 0, sizeof(ifr
));
123 strncpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
124 ifr
.ifr_hwaddr
.sa_family
= AF_UNSPEC
;
125 memcpy(ifr
.ifr_hwaddr
.sa_data
, addr
, ETH_ALEN
);
127 if (ioctl(s
, add
? SIOCADDMULTI
: SIOCDELMULTI
, (caddr_t
) &ifr
) < 0) {
128 perror("ioctl[SIOC{ADD/DEL}MULTI]");
137 static int wpa_driver_wired_membership(struct wpa_driver_wired_data
*drv
,
138 const u8
*addr
, int add
)
141 struct packet_mreq mreq
;
143 if (drv
->pf_sock
== -1)
146 memset(&mreq
, 0, sizeof(mreq
));
147 mreq
.mr_ifindex
= if_nametoindex(drv
->ifname
);
148 mreq
.mr_type
= PACKET_MR_MULTICAST
;
149 mreq
.mr_alen
= ETH_ALEN
;
150 memcpy(mreq
.mr_address
, addr
, ETH_ALEN
);
152 if (setsockopt(drv
->pf_sock
, SOL_PACKET
,
153 add
? PACKET_ADD_MEMBERSHIP
: PACKET_DROP_MEMBERSHIP
,
154 &mreq
, sizeof(mreq
)) < 0) {
155 perror("setsockopt");
159 #else /* __linux__ */
161 #endif /* __linux__ */
165 static void * wpa_driver_wired_init(void *ctx
, const char *ifname
)
167 struct wpa_driver_wired_data
*drv
;
170 drv
= malloc(sizeof(*drv
));
173 memset(drv
, 0, sizeof(*drv
));
174 strncpy(drv
->ifname
, ifname
, sizeof(drv
->ifname
));
178 drv
->pf_sock
= socket(PF_PACKET
, SOCK_DGRAM
, 0);
179 if (drv
->pf_sock
< 0)
180 perror("socket(PF_PACKET)");
185 if (wpa_driver_wired_get_ifflags(ifname
, &flags
) == 0 &&
187 wpa_driver_wired_set_ifflags(ifname
, flags
| IFF_UP
) == 0) {
191 if (wpa_driver_wired_membership(drv
, pae_group_addr
, 1) == 0) {
192 wpa_printf(MSG_DEBUG
, "%s: Added multicast membership with "
193 "packet socket", __func__
);
195 } else if (wpa_driver_wired_multi(ifname
, pae_group_addr
, 1) == 0) {
196 wpa_printf(MSG_DEBUG
, "%s: Added multicast membership with "
197 "SIOCADDMULTI", __func__
);
199 } else if (wpa_driver_wired_get_ifflags(ifname
, &flags
) < 0) {
200 wpa_printf(MSG_INFO
, "%s: Could not get interface "
204 } else if (flags
& IFF_ALLMULTI
) {
205 wpa_printf(MSG_DEBUG
, "%s: Interface is already configured "
206 "for multicast", __func__
);
207 } else if (wpa_driver_wired_set_ifflags(ifname
,
208 flags
| IFF_ALLMULTI
) < 0) {
209 wpa_printf(MSG_INFO
, "%s: Failed to enable allmulti",
214 wpa_printf(MSG_DEBUG
, "%s: Enabled allmulti mode",
216 drv
->iff_allmulti
= 1;
223 static void wpa_driver_wired_deinit(void *priv
)
225 struct wpa_driver_wired_data
*drv
= priv
;
228 if (drv
->membership
&&
229 wpa_driver_wired_membership(drv
, pae_group_addr
, 0) < 0) {
230 wpa_printf(MSG_DEBUG
, "%s: Failed to remove PAE multicast "
231 "group (PACKET)", __func__
);
235 wpa_driver_wired_multi(drv
->ifname
, pae_group_addr
, 0) < 0) {
236 wpa_printf(MSG_DEBUG
, "%s: Failed to remove PAE multicast "
237 "group (SIOCDELMULTI)", __func__
);
240 if (drv
->iff_allmulti
&&
241 (wpa_driver_wired_get_ifflags(drv
->ifname
, &flags
) < 0 ||
242 wpa_driver_wired_set_ifflags(drv
->ifname
,
243 flags
& ~IFF_ALLMULTI
) < 0)) {
244 wpa_printf(MSG_DEBUG
, "%s: Failed to disable allmulti mode",
249 wpa_driver_wired_get_ifflags(drv
->ifname
, &flags
) == 0 &&
251 wpa_driver_wired_set_ifflags(drv
->ifname
, flags
& ~IFF_UP
) < 0) {
252 wpa_printf(MSG_DEBUG
, "%s: Failed to set the interface down",
256 if (drv
->pf_sock
!= -1)
263 const struct wpa_driver_ops wpa_driver_wired_ops
= {
265 .desc
= "wpa_supplicant wired Ethernet driver",
266 .set_wpa
= wpa_driver_wired_set_wpa
,
267 .get_ssid
= wpa_driver_wired_get_ssid
,
268 .get_bssid
= wpa_driver_wired_get_bssid
,
269 .init
= wpa_driver_wired_init
,
270 .deinit
= wpa_driver_wired_deinit
,