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
51static const u8 pae_group_addr[ETH_ALEN] =
52{ 0x01, 0x80, 0xc2, 0x00, 0x00, 0x03 };
53
54
55struct 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. */
67static 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 */
77static 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
85static 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
102static 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
118static 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
140static 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
158static 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
172static 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
183static int wpa_driver_roboswitch_get_ssid(void *priv, u8 *ssid)
184{
185	ssid[0] = 0;
186	return 0;
187}
188
189
190static 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
198static 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
207static 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
233static 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
240static 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
286static 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	wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE, ROBO_ARLCTRL_CONF,
294				   &_read, 1);
295	/* If ARL control is disabled, there is nothing to leave. */
296	if (!(_read & (1 << 4))) return -1;
297
298	wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE,
299				   ROBO_ARLCTRL_ADDR_1, addr_read, 3);
300	wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE, ROBO_ARLCTRL_VEC_1,
301				   &ports_read, 1);
302	/* check if we occupy multiport address 1 */
303	if (os_memcmp(addr_read, addr_be16, 6) == 0 && ports_read == ports) {
304		wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE,
305					   ROBO_ARLCTRL_ADDR_2, addr_read, 3);
306		wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE,
307					   ROBO_ARLCTRL_VEC_2, &ports_read, 1);
308		/* and multiport address 2 */
309		if (os_memcmp(addr_read, addr_be16, 6) == 0 &&
310		    ports_read == ports) {
311			_read &= ~(1 << 4);
312			wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
313						    ROBO_ARLCTRL_CONF, &_read,
314						    1);
315		} else {
316			wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE,
317						   ROBO_ARLCTRL_ADDR_1,
318						   addr_read, 3);
319			wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE,
320						   ROBO_ARLCTRL_VEC_1,
321						   &ports_read, 1);
322			wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
323						    ROBO_ARLCTRL_ADDR_2,
324						    addr_read, 3);
325			wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
326						    ROBO_ARLCTRL_VEC_2,
327						    &ports_read, 1);
328		}
329	} else {
330		wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE,
331					   ROBO_ARLCTRL_ADDR_2, addr_read, 3);
332		wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE,
333					   ROBO_ARLCTRL_VEC_2, &ports_read, 1);
334		/* or multiport address 2 */
335		if (os_memcmp(addr_read, addr_be16, 6) == 0 &&
336		    ports_read == ports) {
337			wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
338						    ROBO_ARLCTRL_ADDR_1,
339						    addr_read, 3);
340			wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
341						    ROBO_ARLCTRL_VEC_1,
342						    &ports_read, 1);
343		} else return -1;
344	}
345	return 0;
346}
347
348
349static void * wpa_driver_roboswitch_init(void *ctx, const char *ifname)
350{
351	struct wpa_driver_roboswitch_data *drv;
352	char *sep;
353	u16 vlan = 0, _read[2];
354
355	drv = os_zalloc(sizeof(*drv));
356	if (drv == NULL) return NULL;
357	drv->ctx = ctx;
358	drv->own_addr[0] = '\0';
359
360	/* copy ifname and take a pointer to the second to last character */
361	sep = drv->ifname +
362	      os_strlcpy(drv->ifname, ifname, sizeof(drv->ifname)) - 2;
363	/* find the '.' separating <interface> and <vlan> */
364	while (sep > drv->ifname && *sep != '.') sep--;
365	if (sep <= drv->ifname) {
366		wpa_printf(MSG_INFO, "%s: No <interface>.<vlan> pair in "
367			   "interface name %s", __func__, drv->ifname);
368		os_free(drv);
369		return NULL;
370	}
371	*sep = '\0';
372	while (*++sep) {
373		if (*sep < '0' || *sep > '9') {
374			wpa_printf(MSG_INFO, "%s: Invalid vlan specification "
375				   "in interface name %s", __func__, ifname);
376			os_free(drv);
377			return NULL;
378		}
379		vlan *= 10;
380		vlan += *sep - '0';
381		if (vlan > ROBO_VLAN_MAX) {
382			wpa_printf(MSG_INFO, "%s: VLAN out of range in "
383				   "interface name %s", __func__, ifname);
384			os_free(drv);
385			return NULL;
386		}
387	}
388
389	drv->fd = socket(PF_INET, SOCK_DGRAM, 0);
390	if (drv->fd < 0) {
391		wpa_printf(MSG_INFO, "%s: Unable to create socket", __func__);
392		os_free(drv);
393		return NULL;
394	}
395
396	os_memset(&drv->ifr, 0, sizeof(drv->ifr));
397	os_strlcpy(drv->ifr.ifr_name, drv->ifname, IFNAMSIZ);
398	if (ioctl(drv->fd, SIOCGMIIPHY, &drv->ifr) < 0) {
399		wpa_printf(MSG_ERROR, "ioctl[SIOCGMIIPHY]: %s",
400			   strerror(errno));
401		os_free(drv);
402		return NULL;
403	}
404	/* BCM63xx devices provide 0 here */
405	if (if_mii(&drv->ifr)->phy_id != ROBO_PHY_ADDR &&
406	    if_mii(&drv->ifr)->phy_id != 0) {
407		wpa_printf(MSG_INFO, "%s: Invalid phy address (not a "
408			   "RoboSwitch?)", __func__);
409		os_free(drv);
410		return NULL;
411	}
412
413	/* set and read back to see if the register can be used */
414	_read[0] = ROBO_VLAN_MAX;
415	wpa_driver_roboswitch_write(drv, ROBO_VLAN_PAGE, ROBO_VLAN_ACCESS_5350,
416				    _read, 1);
417	wpa_driver_roboswitch_read(drv, ROBO_VLAN_PAGE, ROBO_VLAN_ACCESS_5350,
418				   _read + 1, 1);
419	drv->is_5350 = _read[0] == _read[1];
420
421	/* set the read bit */
422	vlan |= 1 << 13;
423	wpa_driver_roboswitch_write(drv, ROBO_VLAN_PAGE,
424				    drv->is_5350 ? ROBO_VLAN_ACCESS_5350
425						 : ROBO_VLAN_ACCESS,
426				    &vlan, 1);
427	wpa_driver_roboswitch_read(drv, ROBO_VLAN_PAGE, ROBO_VLAN_READ, _read,
428				   drv->is_5350 ? 2 : 1);
429	if (!(drv->is_5350 ? _read[1] & (1 << 4) : _read[0] & (1 << 14))) {
430		wpa_printf(MSG_INFO, "%s: Could not get port information for "
431				     "VLAN %d", __func__, vlan & ~(1 << 13));
432		os_free(drv);
433		return NULL;
434	}
435	drv->ports = _read[0] & 0x001F;
436	/* add the MII port */
437	drv->ports |= 1 << 8;
438	if (wpa_driver_roboswitch_join(drv, drv->ports, pae_group_addr) < 0) {
439		wpa_printf(MSG_INFO, "%s: Unable to join PAE group", __func__);
440		os_free(drv);
441		return NULL;
442	} else {
443		wpa_printf(MSG_DEBUG, "%s: Added PAE group address to "
444			   "RoboSwitch ARL", __func__);
445	}
446
447	return drv;
448}
449
450
451static void wpa_driver_roboswitch_deinit(void *priv)
452{
453	struct wpa_driver_roboswitch_data *drv = priv;
454
455	if (drv->l2) {
456		l2_packet_deinit(drv->l2);
457		drv->l2 = NULL;
458	}
459	if (wpa_driver_roboswitch_leave(drv, drv->ports, pae_group_addr) < 0) {
460		wpa_printf(MSG_DEBUG, "%s: Unable to leave PAE group",
461			   __func__);
462	}
463
464	close(drv->fd);
465	os_free(drv);
466}
467
468
469const struct wpa_driver_ops wpa_driver_roboswitch_ops = {
470	.name = "roboswitch",
471	.desc = "wpa_supplicant roboswitch driver",
472	.get_ssid = wpa_driver_roboswitch_get_ssid,
473	.get_bssid = wpa_driver_roboswitch_get_bssid,
474	.get_capa = wpa_driver_roboswitch_get_capa,
475	.init = wpa_driver_roboswitch_init,
476	.deinit = wpa_driver_roboswitch_deinit,
477	.set_param = wpa_driver_roboswitch_set_param,
478	.get_ifname = wpa_driver_roboswitch_get_ifname,
479};
480