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