Home | History | Annotate | Download | only in drivers
      1 /*
      2  * WPA Supplicant - wired Ethernet driver interface
      3  * Copyright (c) 2005-2007, Jouni Malinen <j (at) w1.fi>
      4  *
      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.
      8  *
      9  * Alternatively, this software may be distributed under the terms of BSD
     10  * license.
     11  *
     12  * See README and COPYING for more details.
     13  */
     14 
     15 #include "includes.h"
     16 #include <sys/ioctl.h>
     17 #include <net/if.h>
     18 #ifdef __linux__
     19 #include <netpacket/packet.h>
     20 #endif /* __linux__ */
     21 #if defined(__FreeBSD__) || defined(__DragonFly__)
     22 #include <net/if_dl.h>
     23 #endif /* defined(__FreeBSD__) || defined(__DragonFly__) */
     24 
     25 #include "common.h"
     26 #include "driver.h"
     27 
     28 
     29 static const u8 pae_group_addr[ETH_ALEN] =
     30 { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x03 };
     31 
     32 
     33 struct wpa_driver_wired_data {
     34 	void *ctx;
     35 	int pf_sock;
     36 	char ifname[IFNAMSIZ + 1];
     37 	int membership, multi, iff_allmulti, iff_up;
     38 };
     39 
     40 
     41 static int wpa_driver_wired_get_ssid(void *priv, u8 *ssid)
     42 {
     43 	ssid[0] = 0;
     44 	return 0;
     45 }
     46 
     47 
     48 static int wpa_driver_wired_get_bssid(void *priv, u8 *bssid)
     49 {
     50 	/* Report PAE group address as the "BSSID" for wired connection. */
     51 	os_memcpy(bssid, pae_group_addr, ETH_ALEN);
     52 	return 0;
     53 }
     54 
     55 
     56 static int wpa_driver_wired_get_ifflags(const char *ifname, int *flags)
     57 {
     58 	struct ifreq ifr;
     59 	int s;
     60 
     61 	s = socket(PF_INET, SOCK_DGRAM, 0);
     62 	if (s < 0) {
     63 		perror("socket");
     64 		return -1;
     65 	}
     66 
     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]");
     71 		close(s);
     72 		return -1;
     73 	}
     74 	close(s);
     75 	*flags = ifr.ifr_flags & 0xffff;
     76 	return 0;
     77 }
     78 
     79 
     80 static int wpa_driver_wired_set_ifflags(const char *ifname, int flags)
     81 {
     82 	struct ifreq ifr;
     83 	int s;
     84 
     85 	s = socket(PF_INET, SOCK_DGRAM, 0);
     86 	if (s < 0) {
     87 		perror("socket");
     88 		return -1;
     89 	}
     90 
     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]");
     96 		close(s);
     97 		return -1;
     98 	}
     99 	close(s);
    100 	return 0;
    101 }
    102 
    103 
    104 static int wpa_driver_wired_multi(const char *ifname, const u8 *addr, int add)
    105 {
    106 	struct ifreq ifr;
    107 	int s;
    108 
    109 	s = socket(PF_INET, SOCK_DGRAM, 0);
    110 	if (s < 0) {
    111 		perror("socket");
    112 		return -1;
    113 	}
    114 
    115 	os_memset(&ifr, 0, sizeof(ifr));
    116 	os_strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
    117 #ifdef __linux__
    118 	ifr.ifr_hwaddr.sa_family = AF_UNSPEC;
    119 	os_memcpy(ifr.ifr_hwaddr.sa_data, addr, ETH_ALEN);
    120 #endif /* __linux__ */
    121 #if defined(__FreeBSD__) || defined(__DragonFly__)
    122 	{
    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;
    127 		dlp->sdl_index = 0;
    128 		dlp->sdl_nlen = 0;
    129 		dlp->sdl_alen = ETH_ALEN;
    130 		dlp->sdl_slen = 0;
    131 		os_memcpy(LLADDR(dlp), addr, ETH_ALEN);
    132 	}
    133 #endif /* defined(__FreeBSD__) || defined(__DragonFly__) */
    134 #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__APPLE__)
    135 	{
    136 		struct sockaddr *sap;
    137 		sap = (struct sockaddr *) &ifr.ifr_addr;
    138 		sap->sa_len = sizeof(struct sockaddr);
    139 		sap->sa_family = AF_UNSPEC;
    140 		os_memcpy(sap->sa_data, addr, ETH_ALEN);
    141 	}
    142 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) || defined(__APPLE__) */
    143 
    144 	if (ioctl(s, add ? SIOCADDMULTI : SIOCDELMULTI, (caddr_t) &ifr) < 0) {
    145 		perror("ioctl[SIOC{ADD/DEL}MULTI]");
    146 		close(s);
    147 		return -1;
    148 	}
    149 	close(s);
    150 	return 0;
    151 }
    152 
    153 
    154 static int wpa_driver_wired_membership(struct wpa_driver_wired_data *drv,
    155 				       const u8 *addr, int add)
    156 {
    157 #ifdef __linux__
    158 	struct packet_mreq mreq;
    159 
    160 	if (drv->pf_sock == -1)
    161 		return -1;
    162 
    163 	os_memset(&mreq, 0, sizeof(mreq));
    164 	mreq.mr_ifindex = if_nametoindex(drv->ifname);
    165 	mreq.mr_type = PACKET_MR_MULTICAST;
    166 	mreq.mr_alen = ETH_ALEN;
    167 	os_memcpy(mreq.mr_address, addr, ETH_ALEN);
    168 
    169 	if (setsockopt(drv->pf_sock, SOL_PACKET,
    170 		       add ? PACKET_ADD_MEMBERSHIP : PACKET_DROP_MEMBERSHIP,
    171 		       &mreq, sizeof(mreq)) < 0) {
    172 		perror("setsockopt");
    173 		return -1;
    174 	}
    175 	return 0;
    176 #else /* __linux__ */
    177 	return -1;
    178 #endif /* __linux__ */
    179 }
    180 
    181 
    182 static void * wpa_driver_wired_init(void *ctx, const char *ifname)
    183 {
    184 	struct wpa_driver_wired_data *drv;
    185 	int flags;
    186 
    187 	drv = os_zalloc(sizeof(*drv));
    188 	if (drv == NULL)
    189 		return NULL;
    190 	os_strlcpy(drv->ifname, ifname, sizeof(drv->ifname));
    191 	drv->ctx = ctx;
    192 
    193 #ifdef __linux__
    194 	drv->pf_sock = socket(PF_PACKET, SOCK_DGRAM, 0);
    195 	if (drv->pf_sock < 0)
    196 		perror("socket(PF_PACKET)");
    197 #else /* __linux__ */
    198 	drv->pf_sock = -1;
    199 #endif /* __linux__ */
    200 
    201 	if (wpa_driver_wired_get_ifflags(ifname, &flags) == 0 &&
    202 	    !(flags & IFF_UP) &&
    203 	    wpa_driver_wired_set_ifflags(ifname, flags | IFF_UP) == 0) {
    204 		drv->iff_up = 1;
    205 	}
    206 
    207 	if (wpa_driver_wired_membership(drv, pae_group_addr, 1) == 0) {
    208 		wpa_printf(MSG_DEBUG, "%s: Added multicast membership with "
    209 			   "packet socket", __func__);
    210 		drv->membership = 1;
    211 	} else if (wpa_driver_wired_multi(ifname, pae_group_addr, 1) == 0) {
    212 		wpa_printf(MSG_DEBUG, "%s: Added multicast membership with "
    213 			   "SIOCADDMULTI", __func__);
    214 		drv->multi = 1;
    215 	} else if (wpa_driver_wired_get_ifflags(ifname, &flags) < 0) {
    216 		wpa_printf(MSG_INFO, "%s: Could not get interface "
    217 			   "flags", __func__);
    218 		os_free(drv);
    219 		return NULL;
    220 	} else if (flags & IFF_ALLMULTI) {
    221 		wpa_printf(MSG_DEBUG, "%s: Interface is already configured "
    222 			   "for multicast", __func__);
    223 	} else if (wpa_driver_wired_set_ifflags(ifname,
    224 						flags | IFF_ALLMULTI) < 0) {
    225 		wpa_printf(MSG_INFO, "%s: Failed to enable allmulti",
    226 			   __func__);
    227 		os_free(drv);
    228 		return NULL;
    229 	} else {
    230 		wpa_printf(MSG_DEBUG, "%s: Enabled allmulti mode",
    231 			   __func__);
    232 		drv->iff_allmulti = 1;
    233 	}
    234 
    235 	return drv;
    236 }
    237 
    238 
    239 static void wpa_driver_wired_deinit(void *priv)
    240 {
    241 	struct wpa_driver_wired_data *drv = priv;
    242 	int flags;
    243 
    244 	if (drv->membership &&
    245 	    wpa_driver_wired_membership(drv, pae_group_addr, 0) < 0) {
    246 		wpa_printf(MSG_DEBUG, "%s: Failed to remove PAE multicast "
    247 			   "group (PACKET)", __func__);
    248 	}
    249 
    250 	if (drv->multi &&
    251 	    wpa_driver_wired_multi(drv->ifname, pae_group_addr, 0) < 0) {
    252 		wpa_printf(MSG_DEBUG, "%s: Failed to remove PAE multicast "
    253 			   "group (SIOCDELMULTI)", __func__);
    254 	}
    255 
    256 	if (drv->iff_allmulti &&
    257 	    (wpa_driver_wired_get_ifflags(drv->ifname, &flags) < 0 ||
    258 	     wpa_driver_wired_set_ifflags(drv->ifname,
    259 					  flags & ~IFF_ALLMULTI) < 0)) {
    260 		wpa_printf(MSG_DEBUG, "%s: Failed to disable allmulti mode",
    261 			   __func__);
    262 	}
    263 
    264 	if (drv->iff_up &&
    265 	    wpa_driver_wired_get_ifflags(drv->ifname, &flags) == 0 &&
    266 	    (flags & IFF_UP) &&
    267 	    wpa_driver_wired_set_ifflags(drv->ifname, flags & ~IFF_UP) < 0) {
    268 		wpa_printf(MSG_DEBUG, "%s: Failed to set the interface down",
    269 			   __func__);
    270 	}
    271 
    272 	if (drv->pf_sock != -1)
    273 		close(drv->pf_sock);
    274 
    275 	os_free(drv);
    276 }
    277 
    278 
    279 const struct wpa_driver_ops wpa_driver_wired_ops = {
    280 	.name = "wired",
    281 	.desc = "wpa_supplicant wired Ethernet driver",
    282 	.get_ssid = wpa_driver_wired_get_ssid,
    283 	.get_bssid = wpa_driver_wired_get_bssid,
    284 	.init = wpa_driver_wired_init,
    285 	.deinit = wpa_driver_wired_deinit,
    286 };
    287