Home | History | Annotate | Download | only in drivers
      1 /*
      2  * WPA Supplicant - roboswitch driver interface
      3  * Copyright (c) 2008-2009 Jouke Witteveen
      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 <linux/if.h>
     18 #include <linux/sockios.h>
     19 #include <linux/if_ether.h>
     20 #include <linux/mii.h>
     21 
     22 #include "common.h"
     23 #include "driver.h"
     24 #include "l2_packet/l2_packet.h"
     25 
     26 #ifndef ETH_P_EAPOL
     27 #define ETH_P_EAPOL		0x888e
     28 #endif
     29 
     30 #define ROBO_PHY_ADDR		0x1e	/* RoboSwitch PHY address */
     31 
     32 /* MII access registers */
     33 #define ROBO_MII_PAGE		0x10	/* MII page register */
     34 #define ROBO_MII_ADDR		0x11	/* MII address register */
     35 #define ROBO_MII_DATA_OFFSET	0x18	/* Start of MII data registers */
     36 
     37 #define ROBO_MII_PAGE_ENABLE	0x01	/* MII page op code */
     38 #define ROBO_MII_ADDR_WRITE	0x01	/* MII address write op code */
     39 #define ROBO_MII_ADDR_READ	0x02	/* MII address read op code */
     40 #define ROBO_MII_DATA_MAX	   4	/* Consecutive MII data registers */
     41 #define ROBO_MII_RETRY_MAX	  10	/* Read attempts before giving up */
     42 
     43 /* Page numbers */
     44 #define ROBO_ARLCTRL_PAGE	0x04	/* ARL control page */
     45 #define ROBO_VLAN_PAGE		0x34	/* VLAN page */
     46 
     47 /* ARL control page registers */
     48 #define ROBO_ARLCTRL_CONF	0x00	/* ARL configuration register */
     49 #define ROBO_ARLCTRL_ADDR_1	0x10	/* Multiport address 1 */
     50 #define ROBO_ARLCTRL_VEC_1	0x16	/* Multiport vector 1 */
     51 #define ROBO_ARLCTRL_ADDR_2	0x20	/* Multiport address 2 */
     52 #define ROBO_ARLCTRL_VEC_2	0x26	/* Multiport vector 2 */
     53 
     54 /* VLAN page registers */
     55 #define ROBO_VLAN_ACCESS	0x08	/* VLAN table access register */
     56 #define ROBO_VLAN_ACCESS_5350	0x06	/* VLAN table access register (5350) */
     57 #define ROBO_VLAN_READ		0x0c	/* VLAN read register */
     58 #define ROBO_VLAN_MAX		0xff	/* Maximum number of VLANs */
     59 
     60 
     61 static const u8 pae_group_addr[ETH_ALEN] =
     62 { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x03 };
     63 
     64 
     65 struct wpa_driver_roboswitch_data {
     66 	void *ctx;
     67 	struct l2_packet_data *l2;
     68 	char ifname[IFNAMSIZ + 1];
     69 	u8 own_addr[ETH_ALEN];
     70 	struct ifreq ifr;
     71 	int fd, is_5350;
     72 	u16 ports;
     73 };
     74 
     75 
     76 /* Copied from the kernel-only part of mii.h. */
     77 static inline struct mii_ioctl_data *if_mii(struct ifreq *rq)
     78 {
     79 	return (struct mii_ioctl_data *) &rq->ifr_ifru;
     80 }
     81 
     82 
     83 /*
     84  * RoboSwitch uses 16-bit Big Endian addresses.
     85  * The ordering of the words is reversed in the MII registers.
     86  */
     87 static void wpa_driver_roboswitch_addr_be16(const u8 addr[ETH_ALEN], u16 *be)
     88 {
     89 	int i;
     90 	for (i = 0; i < ETH_ALEN; i += 2)
     91 		be[(ETH_ALEN - i) / 2 - 1] = WPA_GET_BE16(addr + i);
     92 }
     93 
     94 
     95 static u16 wpa_driver_roboswitch_mdio_read(
     96 	struct wpa_driver_roboswitch_data *drv, u8 reg)
     97 {
     98 	struct mii_ioctl_data *mii = if_mii(&drv->ifr);
     99 
    100 	mii->phy_id = ROBO_PHY_ADDR;
    101 	mii->reg_num = reg;
    102 
    103 	if (ioctl(drv->fd, SIOCGMIIREG, &drv->ifr) < 0) {
    104 		perror("ioctl[SIOCGMIIREG]");
    105 		return 0x00;
    106 	}
    107 	return mii->val_out;
    108 }
    109 
    110 
    111 static void wpa_driver_roboswitch_mdio_write(
    112 	struct wpa_driver_roboswitch_data *drv, u8 reg, u16 val)
    113 {
    114 	struct mii_ioctl_data *mii = if_mii(&drv->ifr);
    115 
    116 	mii->phy_id = ROBO_PHY_ADDR;
    117 	mii->reg_num = reg;
    118 	mii->val_in = val;
    119 
    120 	if (ioctl(drv->fd, SIOCSMIIREG, &drv->ifr) < 0) {
    121 		perror("ioctl[SIOCSMIIREG");
    122 	}
    123 }
    124 
    125 
    126 static int wpa_driver_roboswitch_reg(struct wpa_driver_roboswitch_data *drv,
    127 				     u8 page, u8 reg, u8 op)
    128 {
    129 	int i;
    130 
    131 	/* set page number */
    132 	wpa_driver_roboswitch_mdio_write(drv, ROBO_MII_PAGE,
    133 					 (page << 8) | ROBO_MII_PAGE_ENABLE);
    134 	/* set register address */
    135 	wpa_driver_roboswitch_mdio_write(drv, ROBO_MII_ADDR, (reg << 8) | op);
    136 
    137 	/* check if operation completed */
    138 	for (i = 0; i < ROBO_MII_RETRY_MAX; ++i) {
    139 		if ((wpa_driver_roboswitch_mdio_read(drv, ROBO_MII_ADDR) & 3)
    140 		    == 0)
    141 			return 0;
    142 	}
    143 	/* timeout */
    144 	return -1;
    145 }
    146 
    147 
    148 static int wpa_driver_roboswitch_read(struct wpa_driver_roboswitch_data *drv,
    149 				      u8 page, u8 reg, u16 *val, int len)
    150 {
    151 	int i;
    152 
    153 	if (len > ROBO_MII_DATA_MAX ||
    154 	    wpa_driver_roboswitch_reg(drv, page, reg, ROBO_MII_ADDR_READ) < 0)
    155 		return -1;
    156 
    157 	for (i = 0; i < len; ++i) {
    158 		val[i] = wpa_driver_roboswitch_mdio_read(
    159 			drv, ROBO_MII_DATA_OFFSET + i);
    160 	}
    161 
    162 	return 0;
    163 }
    164 
    165 
    166 static int wpa_driver_roboswitch_write(struct wpa_driver_roboswitch_data *drv,
    167 				       u8 page, u8 reg, u16 *val, int len)
    168 {
    169 	int i;
    170 
    171 	if (len > ROBO_MII_DATA_MAX) return -1;
    172 	for (i = 0; i < len; ++i) {
    173 		wpa_driver_roboswitch_mdio_write(drv, ROBO_MII_DATA_OFFSET + i,
    174 						 val[i]);
    175 	}
    176 	return wpa_driver_roboswitch_reg(drv, page, reg, ROBO_MII_ADDR_WRITE);
    177 }
    178 
    179 
    180 static void wpa_driver_roboswitch_receive(void *priv, const u8 *src_addr,
    181 					  const u8 *buf, size_t len)
    182 {
    183 	struct wpa_driver_roboswitch_data *drv = priv;
    184 
    185 	if (len > 14 && WPA_GET_BE16(buf + 12) == ETH_P_EAPOL &&
    186 	    os_memcmp(buf, drv->own_addr, ETH_ALEN) == 0) {
    187 		wpa_supplicant_rx_eapol(drv->ctx, src_addr, buf + 14,
    188 					len - 14);
    189 	}
    190 }
    191 
    192 
    193 static int wpa_driver_roboswitch_get_ssid(void *priv, u8 *ssid)
    194 {
    195 	ssid[0] = 0;
    196 	return 0;
    197 }
    198 
    199 
    200 static int wpa_driver_roboswitch_get_bssid(void *priv, u8 *bssid)
    201 {
    202 	/* Report PAE group address as the "BSSID" for wired connection. */
    203 	os_memcpy(bssid, pae_group_addr, ETH_ALEN);
    204 	return 0;
    205 }
    206 
    207 
    208 static int wpa_driver_roboswitch_set_param(void *priv, const char *param)
    209 {
    210 	struct wpa_driver_roboswitch_data *drv = priv;
    211 	char *sep;
    212 
    213 	if (param == NULL || os_strstr(param, "multicast_only=1") == NULL) {
    214 		sep = drv->ifname + os_strlen(drv->ifname);
    215 		*sep = '.';
    216 		drv->l2 = l2_packet_init(drv->ifname, NULL, ETH_P_ALL,
    217 					 wpa_driver_roboswitch_receive, drv,
    218 					 1);
    219 		if (drv->l2 == NULL) {
    220 			wpa_printf(MSG_INFO, "%s: Unable to listen on %s",
    221 				   __func__, drv->ifname);
    222 			return -1;
    223 		}
    224 		*sep = '\0';
    225 		l2_packet_get_own_addr(drv->l2, drv->own_addr);
    226 	} else {
    227 		wpa_printf(MSG_DEBUG, "%s: Ignoring unicast frames", __func__);
    228 		drv->l2 = NULL;
    229 	}
    230 	return 0;
    231 }
    232 
    233 
    234 static const char * wpa_driver_roboswitch_get_ifname(void *priv)
    235 {
    236 	struct wpa_driver_roboswitch_data *drv = priv;
    237 	return drv->ifname;
    238 }
    239 
    240 
    241 static int wpa_driver_roboswitch_join(struct wpa_driver_roboswitch_data *drv,
    242 				      u16 ports, const u8 *addr)
    243 {
    244 	u16 read1[3], read2[3], addr_be16[3];
    245 
    246 	wpa_driver_roboswitch_addr_be16(addr, addr_be16);
    247 
    248 	if (wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE,
    249 				       ROBO_ARLCTRL_CONF, read1, 1) < 0)
    250 		return -1;
    251 	if (!(read1[0] & (1 << 4))) {
    252 		/* multiport addresses are not yet enabled */
    253 		read1[0] |= 1 << 4;
    254 		wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
    255 					    ROBO_ARLCTRL_ADDR_1, addr_be16, 3);
    256 		wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
    257 					    ROBO_ARLCTRL_VEC_1, &ports, 1);
    258 		wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
    259 					    ROBO_ARLCTRL_ADDR_2, addr_be16, 3);
    260 		wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
    261 					    ROBO_ARLCTRL_VEC_2, &ports, 1);
    262 		wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
    263 					    ROBO_ARLCTRL_CONF, read1, 1);
    264 	} else {
    265 		/* if both multiport addresses are the same we can add */
    266 		wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE,
    267 					   ROBO_ARLCTRL_ADDR_1, read1, 3);
    268 		wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE,
    269 					   ROBO_ARLCTRL_ADDR_2, read2, 3);
    270 		if (os_memcmp(read1, read2, 6) != 0)
    271 			return -1;
    272 		wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE,
    273 					   ROBO_ARLCTRL_VEC_1, read1, 1);
    274 		wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE,
    275 					   ROBO_ARLCTRL_VEC_2, read2, 1);
    276 		if (read1[0] != read2[0])
    277 			return -1;
    278 		wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
    279 					    ROBO_ARLCTRL_ADDR_1, addr_be16, 3);
    280 		wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
    281 					    ROBO_ARLCTRL_VEC_1, &ports, 1);
    282 	}
    283 	return 0;
    284 }
    285 
    286 
    287 static int wpa_driver_roboswitch_leave(struct wpa_driver_roboswitch_data *drv,
    288 				       u16 ports, const u8 *addr)
    289 {
    290 	u16 _read, addr_be16[3], addr_read[3], ports_read;
    291 
    292 	wpa_driver_roboswitch_addr_be16(addr, addr_be16);
    293 
    294 	wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE, ROBO_ARLCTRL_CONF,
    295 				   &_read, 1);
    296 	/* If ARL control is disabled, there is nothing to leave. */
    297 	if (!(_read & (1 << 4))) return -1;
    298 
    299 	wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE,
    300 				   ROBO_ARLCTRL_ADDR_1, addr_read, 3);
    301 	wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE, ROBO_ARLCTRL_VEC_1,
    302 				   &ports_read, 1);
    303 	/* check if we occupy multiport address 1 */
    304 	if (os_memcmp(addr_read, addr_be16, 6) == 0 && ports_read == ports) {
    305 		wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE,
    306 					   ROBO_ARLCTRL_ADDR_2, addr_read, 3);
    307 		wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE,
    308 					   ROBO_ARLCTRL_VEC_2, &ports_read, 1);
    309 		/* and multiport address 2 */
    310 		if (os_memcmp(addr_read, addr_be16, 6) == 0 &&
    311 		    ports_read == ports) {
    312 			_read &= ~(1 << 4);
    313 			wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
    314 						    ROBO_ARLCTRL_CONF, &_read,
    315 						    1);
    316 		} else {
    317 			wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE,
    318 						   ROBO_ARLCTRL_ADDR_1,
    319 						   addr_read, 3);
    320 			wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE,
    321 						   ROBO_ARLCTRL_VEC_1,
    322 						   &ports_read, 1);
    323 			wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
    324 						    ROBO_ARLCTRL_ADDR_2,
    325 						    addr_read, 3);
    326 			wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
    327 						    ROBO_ARLCTRL_VEC_2,
    328 						    &ports_read, 1);
    329 		}
    330 	} else {
    331 		wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE,
    332 					   ROBO_ARLCTRL_ADDR_2, addr_read, 3);
    333 		wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE,
    334 					   ROBO_ARLCTRL_VEC_2, &ports_read, 1);
    335 		/* or multiport address 2 */
    336 		if (os_memcmp(addr_read, addr_be16, 6) == 0 &&
    337 		    ports_read == ports) {
    338 			wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
    339 						    ROBO_ARLCTRL_ADDR_1,
    340 						    addr_read, 3);
    341 			wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
    342 						    ROBO_ARLCTRL_VEC_1,
    343 						    &ports_read, 1);
    344 		} else return -1;
    345 	}
    346 	return 0;
    347 }
    348 
    349 
    350 static void * wpa_driver_roboswitch_init(void *ctx, const char *ifname)
    351 {
    352 	struct wpa_driver_roboswitch_data *drv;
    353 	char *sep;
    354 	u16 vlan = 0, _read[2];
    355 
    356 	drv = os_zalloc(sizeof(*drv));
    357 	if (drv == NULL) return NULL;
    358 	drv->ctx = ctx;
    359 	drv->own_addr[0] = '\0';
    360 
    361 	/* copy ifname and take a pointer to the second to last character */
    362 	sep = drv->ifname +
    363 	      os_strlcpy(drv->ifname, ifname, sizeof(drv->ifname)) - 2;
    364 	/* find the '.' seperating <interface> and <vlan> */
    365 	while (sep > drv->ifname && *sep != '.') sep--;
    366 	if (sep <= drv->ifname) {
    367 		wpa_printf(MSG_INFO, "%s: No <interface>.<vlan> pair in "
    368 			   "interface name %s", __func__, drv->ifname);
    369 		os_free(drv);
    370 		return NULL;
    371 	}
    372 	*sep = '\0';
    373 	while (*++sep) {
    374 		if (*sep < '0' || *sep > '9') {
    375 			wpa_printf(MSG_INFO, "%s: Invalid vlan specification "
    376 				   "in interface name %s", __func__, ifname);
    377 			os_free(drv);
    378 			return NULL;
    379 		}
    380 		vlan *= 10;
    381 		vlan += *sep - '0';
    382 		if (vlan > ROBO_VLAN_MAX) {
    383 			wpa_printf(MSG_INFO, "%s: VLAN out of range in "
    384 				   "interface name %s", __func__, ifname);
    385 			os_free(drv);
    386 			return NULL;
    387 		}
    388 	}
    389 
    390 	drv->fd = socket(PF_INET, SOCK_DGRAM, 0);
    391 	if (drv->fd < 0) {
    392 		wpa_printf(MSG_INFO, "%s: Unable to create socket", __func__);
    393 		os_free(drv);
    394 		return NULL;
    395 	}
    396 
    397 	os_memset(&drv->ifr, 0, sizeof(drv->ifr));
    398 	os_strlcpy(drv->ifr.ifr_name, drv->ifname, IFNAMSIZ);
    399 	if (ioctl(drv->fd, SIOCGMIIPHY, &drv->ifr) < 0) {
    400 		perror("ioctl[SIOCGMIIPHY]");
    401 		os_free(drv);
    402 		return NULL;
    403 	}
    404 	if (if_mii(&drv->ifr)->phy_id != ROBO_PHY_ADDR) {
    405 		wpa_printf(MSG_INFO, "%s: Invalid phy address (not a "
    406 			   "RoboSwitch?)", __func__);
    407 		os_free(drv);
    408 		return NULL;
    409 	}
    410 
    411 	/* set and read back to see if the register can be used */
    412 	_read[0] = ROBO_VLAN_MAX;
    413 	wpa_driver_roboswitch_write(drv, ROBO_VLAN_PAGE, ROBO_VLAN_ACCESS_5350,
    414 				    _read, 1);
    415 	wpa_driver_roboswitch_read(drv, ROBO_VLAN_PAGE, ROBO_VLAN_ACCESS_5350,
    416 				   _read + 1, 1);
    417 	drv->is_5350 = _read[0] == _read[1];
    418 
    419 	/* set the read bit */
    420 	vlan |= 1 << 13;
    421 	wpa_driver_roboswitch_write(drv, ROBO_VLAN_PAGE,
    422 				    drv->is_5350 ? ROBO_VLAN_ACCESS_5350
    423 						 : ROBO_VLAN_ACCESS,
    424 				    &vlan, 1);
    425 	wpa_driver_roboswitch_read(drv, ROBO_VLAN_PAGE, ROBO_VLAN_READ, _read,
    426 				   drv->is_5350 ? 2 : 1);
    427 	if (!(drv->is_5350 ? _read[1] & (1 << 4) : _read[0] & (1 << 14))) {
    428 		wpa_printf(MSG_INFO, "%s: Could not get port information for "
    429 				     "VLAN %d", __func__, vlan & ~(1 << 13));
    430 		os_free(drv);
    431 		return NULL;
    432 	}
    433 	drv->ports = _read[0] & 0x001F;
    434 	/* add the MII port */
    435 	drv->ports |= 1 << 8;
    436 	if (wpa_driver_roboswitch_join(drv, drv->ports, pae_group_addr) < 0) {
    437 		wpa_printf(MSG_INFO, "%s: Unable to join PAE group", __func__);
    438 		os_free(drv);
    439 		return NULL;
    440 	} else {
    441 		wpa_printf(MSG_DEBUG, "%s: Added PAE group address to "
    442 			   "RoboSwitch ARL", __func__);
    443 	}
    444 
    445 	return drv;
    446 }
    447 
    448 
    449 static void wpa_driver_roboswitch_deinit(void *priv)
    450 {
    451 	struct wpa_driver_roboswitch_data *drv = priv;
    452 
    453 	if (drv->l2) {
    454 		l2_packet_deinit(drv->l2);
    455 		drv->l2 = NULL;
    456 	}
    457 	if (wpa_driver_roboswitch_leave(drv, drv->ports, pae_group_addr) < 0) {
    458 		wpa_printf(MSG_DEBUG, "%s: Unable to leave PAE group",
    459 			   __func__);
    460 	}
    461 
    462 	close(drv->fd);
    463 	os_free(drv);
    464 }
    465 
    466 
    467 const struct wpa_driver_ops wpa_driver_roboswitch_ops = {
    468 	.name = "roboswitch",
    469 	.desc = "wpa_supplicant roboswitch driver",
    470 	.get_ssid = wpa_driver_roboswitch_get_ssid,
    471 	.get_bssid = wpa_driver_roboswitch_get_bssid,
    472 	.init = wpa_driver_roboswitch_init,
    473 	.deinit = wpa_driver_roboswitch_deinit,
    474 	.set_param = wpa_driver_roboswitch_set_param,
    475 	.get_ifname = wpa_driver_roboswitch_get_ifname,
    476 };
    477