Switch to gcc41 as default compiler.
[dragonfly.git] / contrib / wpa_supplicant-0.4.9 / driver_wired.c
blob359a21b481bd32dcbf560cd7e824edd256e692a0
1 /*
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
10 * license.
12 * See README and COPYING for more details.
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <string.h>
18 #include <unistd.h>
19 #include <sys/ioctl.h>
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <netpacket/packet.h>
23 #include <net/if.h>
25 #include "common.h"
26 #include "driver.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 {
35 void *ctx;
36 int pf_sock;
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)
44 return 0;
48 static int wpa_driver_wired_get_ssid(void *priv, u8 *ssid)
50 ssid[0] = 0;
51 return 0;
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);
59 return 0;
63 static int wpa_driver_wired_get_ifflags(const char *ifname, int *flags)
65 struct ifreq ifr;
66 int s;
68 s = socket(PF_INET, SOCK_DGRAM, 0);
69 if (s < 0) {
70 perror("socket");
71 return -1;
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]");
78 close(s);
79 return -1;
81 close(s);
82 *flags = ifr.ifr_flags & 0xffff;
83 return 0;
87 static int wpa_driver_wired_set_ifflags(const char *ifname, int flags)
89 struct ifreq ifr;
90 int s;
92 s = socket(PF_INET, SOCK_DGRAM, 0);
93 if (s < 0) {
94 perror("socket");
95 return -1;
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]");
103 close(s);
104 return -1;
106 close(s);
107 return 0;
111 static int wpa_driver_wired_multi(const char *ifname, const u8 *addr, int add)
113 struct ifreq ifr;
114 int s;
116 s = socket(PF_INET, SOCK_DGRAM, 0);
117 if (s < 0) {
118 perror("socket");
119 return -1;
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]");
129 close(s);
130 return -1;
132 close(s);
133 return 0;
137 static int wpa_driver_wired_membership(struct wpa_driver_wired_data *drv,
138 const u8 *addr, int add)
140 #ifdef __linux__
141 struct packet_mreq mreq;
143 if (drv->pf_sock == -1)
144 return -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");
156 return -1;
158 return 0;
159 #else /* __linux__ */
160 return -1;
161 #endif /* __linux__ */
165 static void * wpa_driver_wired_init(void *ctx, const char *ifname)
167 struct wpa_driver_wired_data *drv;
168 int flags;
170 drv = malloc(sizeof(*drv));
171 if (drv == NULL)
172 return NULL;
173 memset(drv, 0, sizeof(*drv));
174 strncpy(drv->ifname, ifname, sizeof(drv->ifname));
175 drv->ctx = ctx;
177 #ifdef __linux__
178 drv->pf_sock = socket(PF_PACKET, SOCK_DGRAM, 0);
179 if (drv->pf_sock < 0)
180 perror("socket(PF_PACKET)");
181 #else
182 drv->pf_sock = -1;
183 #endif
185 if (wpa_driver_wired_get_ifflags(ifname, &flags) == 0 &&
186 !(flags & IFF_UP) &&
187 wpa_driver_wired_set_ifflags(ifname, flags | IFF_UP) == 0) {
188 drv->iff_up = 1;
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__);
194 drv->membership = 1;
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__);
198 drv->multi = 1;
199 } else if (wpa_driver_wired_get_ifflags(ifname, &flags) < 0) {
200 wpa_printf(MSG_INFO, "%s: Could not get interface "
201 "flags", __func__);
202 free(drv);
203 return NULL;
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",
210 __func__);
211 free(drv);
212 return NULL;
213 } else {
214 wpa_printf(MSG_DEBUG, "%s: Enabled allmulti mode",
215 __func__);
216 drv->iff_allmulti = 1;
219 return drv;
223 static void wpa_driver_wired_deinit(void *priv)
225 struct wpa_driver_wired_data *drv = priv;
226 int flags;
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__);
234 if (drv->multi &&
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",
245 __func__);
248 if (drv->iff_up &&
249 wpa_driver_wired_get_ifflags(drv->ifname, &flags) == 0 &&
250 (flags & IFF_UP) &&
251 wpa_driver_wired_set_ifflags(drv->ifname, flags & ~IFF_UP) < 0) {
252 wpa_printf(MSG_DEBUG, "%s: Failed to set the interface down",
253 __func__);
256 if (drv->pf_sock != -1)
257 close(drv->pf_sock);
259 free(drv);
263 const struct wpa_driver_ops wpa_driver_wired_ops = {
264 .name = "wired",
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,