2 * WPA Supplicant - roboswitch driver interface
3 * Copyright (c) 2008-2009 Jouke Witteveen
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
12 * See README and COPYING for more details.
16 #include <sys/ioctl.h>
18 #include <linux/sockios.h>
19 #include <linux/if_ether.h>
20 #include <linux/mii.h>
24 #include "l2_packet/l2_packet.h"
26 #define ROBO_PHY_ADDR 0x1e /* RoboSwitch PHY address */
28 /* MII access registers */
29 #define ROBO_MII_PAGE 0x10 /* MII page register */
30 #define ROBO_MII_ADDR 0x11 /* MII address register */
31 #define ROBO_MII_DATA_OFFSET 0x18 /* Start of MII data registers */
33 #define ROBO_MII_PAGE_ENABLE 0x01 /* MII page op code */
34 #define ROBO_MII_ADDR_WRITE 0x01 /* MII address write op code */
35 #define ROBO_MII_ADDR_READ 0x02 /* MII address read op code */
36 #define ROBO_MII_DATA_MAX 4 /* Consecutive MII data registers */
37 #define ROBO_MII_RETRY_MAX 10 /* Read attempts before giving up */
40 #define ROBO_ARLCTRL_PAGE 0x04 /* ARL control page */
41 #define ROBO_VLAN_PAGE 0x34 /* VLAN page */
43 /* ARL control page registers */
44 #define ROBO_ARLCTRL_CONF 0x00 /* ARL configuration register */
45 #define ROBO_ARLCTRL_ADDR_1 0x10 /* Multiport address 1 */
46 #define ROBO_ARLCTRL_VEC_1 0x16 /* Multiport vector 1 */
47 #define ROBO_ARLCTRL_ADDR_2 0x20 /* Multiport address 2 */
48 #define ROBO_ARLCTRL_VEC_2 0x26 /* Multiport vector 2 */
50 /* VLAN page registers */
51 #define ROBO_VLAN_ACCESS 0x08 /* VLAN table access register */
52 #define ROBO_VLAN_ACCESS_5350 0x06 /* VLAN table access register (5350) */
53 #define ROBO_VLAN_READ 0x0c /* VLAN read register */
54 #define ROBO_VLAN_MAX 0xff /* Maximum number of VLANs */
57 static const u8 pae_group_addr
[ETH_ALEN
] =
58 { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x03 };
61 struct wpa_driver_roboswitch_data
{
63 struct l2_packet_data
*l2
;
64 char ifname
[IFNAMSIZ
+ 1];
65 u8 own_addr
[ETH_ALEN
];
72 /* Copied from the kernel-only part of mii.h. */
73 static inline struct mii_ioctl_data
*if_mii(struct ifreq
*rq
)
75 return (struct mii_ioctl_data
*) &rq
->ifr_ifru
;
80 * RoboSwitch uses 16-bit Big Endian addresses.
81 * The ordering of the words is reversed in the MII registers.
83 static void wpa_driver_roboswitch_addr_be16(const u8 addr
[ETH_ALEN
], u16
*be
)
86 for (i
= 0; i
< ETH_ALEN
; i
+= 2)
87 be
[(ETH_ALEN
- i
) / 2 - 1] = WPA_GET_BE16(addr
+ i
);
91 static u16
wpa_driver_roboswitch_mdio_read(
92 struct wpa_driver_roboswitch_data
*drv
, u8 reg
)
94 struct mii_ioctl_data
*mii
= if_mii(&drv
->ifr
);
96 mii
->phy_id
= ROBO_PHY_ADDR
;
99 if (ioctl(drv
->fd
, SIOCGMIIREG
, &drv
->ifr
) < 0) {
100 perror("ioctl[SIOCGMIIREG]");
107 static void wpa_driver_roboswitch_mdio_write(
108 struct wpa_driver_roboswitch_data
*drv
, u8 reg
, u16 val
)
110 struct mii_ioctl_data
*mii
= if_mii(&drv
->ifr
);
112 mii
->phy_id
= ROBO_PHY_ADDR
;
116 if (ioctl(drv
->fd
, SIOCSMIIREG
, &drv
->ifr
) < 0) {
117 perror("ioctl[SIOCSMIIREG");
122 static int wpa_driver_roboswitch_reg(struct wpa_driver_roboswitch_data
*drv
,
123 u8 page
, u8 reg
, u8 op
)
127 /* set page number */
128 wpa_driver_roboswitch_mdio_write(drv
, ROBO_MII_PAGE
,
129 (page
<< 8) | ROBO_MII_PAGE_ENABLE
);
130 /* set register address */
131 wpa_driver_roboswitch_mdio_write(drv
, ROBO_MII_ADDR
, (reg
<< 8) | op
);
133 /* check if operation completed */
134 for (i
= 0; i
< ROBO_MII_RETRY_MAX
; ++i
) {
135 if ((wpa_driver_roboswitch_mdio_read(drv
, ROBO_MII_ADDR
) & 3)
144 static int wpa_driver_roboswitch_read(struct wpa_driver_roboswitch_data
*drv
,
145 u8 page
, u8 reg
, u16
*val
, int len
)
149 if (len
> ROBO_MII_DATA_MAX
||
150 wpa_driver_roboswitch_reg(drv
, page
, reg
, ROBO_MII_ADDR_READ
) < 0)
153 for (i
= 0; i
< len
; ++i
) {
154 val
[i
] = wpa_driver_roboswitch_mdio_read(
155 drv
, ROBO_MII_DATA_OFFSET
+ i
);
162 static int wpa_driver_roboswitch_write(struct wpa_driver_roboswitch_data
*drv
,
163 u8 page
, u8 reg
, u16
*val
, int len
)
167 if (len
> ROBO_MII_DATA_MAX
) return -1;
168 for (i
= 0; i
< len
; ++i
) {
169 wpa_driver_roboswitch_mdio_write(drv
, ROBO_MII_DATA_OFFSET
+ i
,
172 return wpa_driver_roboswitch_reg(drv
, page
, reg
, ROBO_MII_ADDR_WRITE
);
176 static void wpa_driver_roboswitch_receive(void *priv
, const u8
*src_addr
,
177 const u8
*buf
, size_t len
)
179 struct wpa_driver_roboswitch_data
*drv
= priv
;
181 if (len
> 14 && WPA_GET_BE16(buf
+ 12) == ETH_P_EAPOL
&&
182 os_memcmp(buf
, drv
->own_addr
, ETH_ALEN
) == 0) {
183 wpa_supplicant_rx_eapol(drv
->ctx
, src_addr
, buf
+ 14,
189 static int wpa_driver_roboswitch_get_ssid(void *priv
, u8
*ssid
)
196 static int wpa_driver_roboswitch_get_bssid(void *priv
, u8
*bssid
)
198 /* Report PAE group address as the "BSSID" for wired connection. */
199 os_memcpy(bssid
, pae_group_addr
, ETH_ALEN
);
204 static int wpa_driver_roboswitch_get_capa(void *priv
,
205 struct wpa_driver_capa
*capa
)
207 os_memset(capa
, 0, sizeof(*capa
));
208 capa
->flags
= WPA_DRIVER_FLAGS_WIRED
;
213 static int wpa_driver_roboswitch_set_param(void *priv
, const char *param
)
215 struct wpa_driver_roboswitch_data
*drv
= priv
;
218 if (param
== NULL
|| os_strstr(param
, "multicast_only=1") == NULL
) {
219 sep
= drv
->ifname
+ os_strlen(drv
->ifname
);
221 drv
->l2
= l2_packet_init(drv
->ifname
, NULL
, ETH_P_ALL
,
222 wpa_driver_roboswitch_receive
, drv
,
224 if (drv
->l2
== NULL
) {
225 wpa_printf(MSG_INFO
, "%s: Unable to listen on %s",
226 __func__
, drv
->ifname
);
230 l2_packet_get_own_addr(drv
->l2
, drv
->own_addr
);
232 wpa_printf(MSG_DEBUG
, "%s: Ignoring unicast frames", __func__
);
239 static const char * wpa_driver_roboswitch_get_ifname(void *priv
)
241 struct wpa_driver_roboswitch_data
*drv
= priv
;
246 static int wpa_driver_roboswitch_join(struct wpa_driver_roboswitch_data
*drv
,
247 u16 ports
, const u8
*addr
)
249 u16 read1
[3], read2
[3], addr_be16
[3];
251 wpa_driver_roboswitch_addr_be16(addr
, addr_be16
);
253 if (wpa_driver_roboswitch_read(drv
, ROBO_ARLCTRL_PAGE
,
254 ROBO_ARLCTRL_CONF
, read1
, 1) < 0)
256 if (!(read1
[0] & (1 << 4))) {
257 /* multiport addresses are not yet enabled */
259 wpa_driver_roboswitch_write(drv
, ROBO_ARLCTRL_PAGE
,
260 ROBO_ARLCTRL_ADDR_1
, addr_be16
, 3);
261 wpa_driver_roboswitch_write(drv
, ROBO_ARLCTRL_PAGE
,
262 ROBO_ARLCTRL_VEC_1
, &ports
, 1);
263 wpa_driver_roboswitch_write(drv
, ROBO_ARLCTRL_PAGE
,
264 ROBO_ARLCTRL_ADDR_2
, addr_be16
, 3);
265 wpa_driver_roboswitch_write(drv
, ROBO_ARLCTRL_PAGE
,
266 ROBO_ARLCTRL_VEC_2
, &ports
, 1);
267 wpa_driver_roboswitch_write(drv
, ROBO_ARLCTRL_PAGE
,
268 ROBO_ARLCTRL_CONF
, read1
, 1);
270 /* if both multiport addresses are the same we can add */
271 wpa_driver_roboswitch_read(drv
, ROBO_ARLCTRL_PAGE
,
272 ROBO_ARLCTRL_ADDR_1
, read1
, 3);
273 wpa_driver_roboswitch_read(drv
, ROBO_ARLCTRL_PAGE
,
274 ROBO_ARLCTRL_ADDR_2
, read2
, 3);
275 if (os_memcmp(read1
, read2
, 6) != 0)
277 wpa_driver_roboswitch_read(drv
, ROBO_ARLCTRL_PAGE
,
278 ROBO_ARLCTRL_VEC_1
, read1
, 1);
279 wpa_driver_roboswitch_read(drv
, ROBO_ARLCTRL_PAGE
,
280 ROBO_ARLCTRL_VEC_2
, read2
, 1);
281 if (read1
[0] != read2
[0])
283 wpa_driver_roboswitch_write(drv
, ROBO_ARLCTRL_PAGE
,
284 ROBO_ARLCTRL_ADDR_1
, addr_be16
, 3);
285 wpa_driver_roboswitch_write(drv
, ROBO_ARLCTRL_PAGE
,
286 ROBO_ARLCTRL_VEC_1
, &ports
, 1);
292 static int wpa_driver_roboswitch_leave(struct wpa_driver_roboswitch_data
*drv
,
293 u16 ports
, const u8
*addr
)
295 u16 _read
, addr_be16
[3], addr_read
[3], ports_read
;
297 wpa_driver_roboswitch_addr_be16(addr
, addr_be16
);
299 wpa_driver_roboswitch_read(drv
, ROBO_ARLCTRL_PAGE
, ROBO_ARLCTRL_CONF
,
301 /* If ARL control is disabled, there is nothing to leave. */
302 if (!(_read
& (1 << 4))) return -1;
304 wpa_driver_roboswitch_read(drv
, ROBO_ARLCTRL_PAGE
,
305 ROBO_ARLCTRL_ADDR_1
, addr_read
, 3);
306 wpa_driver_roboswitch_read(drv
, ROBO_ARLCTRL_PAGE
, ROBO_ARLCTRL_VEC_1
,
308 /* check if we occupy multiport address 1 */
309 if (os_memcmp(addr_read
, addr_be16
, 6) == 0 && ports_read
== ports
) {
310 wpa_driver_roboswitch_read(drv
, ROBO_ARLCTRL_PAGE
,
311 ROBO_ARLCTRL_ADDR_2
, addr_read
, 3);
312 wpa_driver_roboswitch_read(drv
, ROBO_ARLCTRL_PAGE
,
313 ROBO_ARLCTRL_VEC_2
, &ports_read
, 1);
314 /* and multiport address 2 */
315 if (os_memcmp(addr_read
, addr_be16
, 6) == 0 &&
316 ports_read
== ports
) {
318 wpa_driver_roboswitch_write(drv
, ROBO_ARLCTRL_PAGE
,
319 ROBO_ARLCTRL_CONF
, &_read
,
322 wpa_driver_roboswitch_read(drv
, ROBO_ARLCTRL_PAGE
,
325 wpa_driver_roboswitch_read(drv
, ROBO_ARLCTRL_PAGE
,
328 wpa_driver_roboswitch_write(drv
, ROBO_ARLCTRL_PAGE
,
331 wpa_driver_roboswitch_write(drv
, ROBO_ARLCTRL_PAGE
,
336 wpa_driver_roboswitch_read(drv
, ROBO_ARLCTRL_PAGE
,
337 ROBO_ARLCTRL_ADDR_2
, addr_read
, 3);
338 wpa_driver_roboswitch_read(drv
, ROBO_ARLCTRL_PAGE
,
339 ROBO_ARLCTRL_VEC_2
, &ports_read
, 1);
340 /* or multiport address 2 */
341 if (os_memcmp(addr_read
, addr_be16
, 6) == 0 &&
342 ports_read
== ports
) {
343 wpa_driver_roboswitch_write(drv
, ROBO_ARLCTRL_PAGE
,
346 wpa_driver_roboswitch_write(drv
, ROBO_ARLCTRL_PAGE
,
355 static void * wpa_driver_roboswitch_init(void *ctx
, const char *ifname
)
357 struct wpa_driver_roboswitch_data
*drv
;
359 u16 vlan
= 0, _read
[2];
361 drv
= os_zalloc(sizeof(*drv
));
362 if (drv
== NULL
) return NULL
;
364 drv
->own_addr
[0] = '\0';
366 /* copy ifname and take a pointer to the second to last character */
368 os_strlcpy(drv
->ifname
, ifname
, sizeof(drv
->ifname
)) - 2;
369 /* find the '.' seperating <interface> and <vlan> */
370 while (sep
> drv
->ifname
&& *sep
!= '.') sep
--;
371 if (sep
<= drv
->ifname
) {
372 wpa_printf(MSG_INFO
, "%s: No <interface>.<vlan> pair in "
373 "interface name %s", __func__
, drv
->ifname
);
379 if (*sep
< '0' || *sep
> '9') {
380 wpa_printf(MSG_INFO
, "%s: Invalid vlan specification "
381 "in interface name %s", __func__
, ifname
);
387 if (vlan
> ROBO_VLAN_MAX
) {
388 wpa_printf(MSG_INFO
, "%s: VLAN out of range in "
389 "interface name %s", __func__
, ifname
);
395 drv
->fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
397 wpa_printf(MSG_INFO
, "%s: Unable to create socket", __func__
);
402 os_memset(&drv
->ifr
, 0, sizeof(drv
->ifr
));
403 os_strlcpy(drv
->ifr
.ifr_name
, drv
->ifname
, IFNAMSIZ
);
404 if (ioctl(drv
->fd
, SIOCGMIIPHY
, &drv
->ifr
) < 0) {
405 perror("ioctl[SIOCGMIIPHY]");
409 if (if_mii(&drv
->ifr
)->phy_id
!= ROBO_PHY_ADDR
) {
410 wpa_printf(MSG_INFO
, "%s: Invalid phy address (not a "
411 "RoboSwitch?)", __func__
);
416 /* set and read back to see if the register can be used */
417 _read
[0] = ROBO_VLAN_MAX
;
418 wpa_driver_roboswitch_write(drv
, ROBO_VLAN_PAGE
, ROBO_VLAN_ACCESS_5350
,
420 wpa_driver_roboswitch_read(drv
, ROBO_VLAN_PAGE
, ROBO_VLAN_ACCESS_5350
,
422 drv
->is_5350
= _read
[0] == _read
[1];
424 /* set the read bit */
426 wpa_driver_roboswitch_write(drv
, ROBO_VLAN_PAGE
,
427 drv
->is_5350
? ROBO_VLAN_ACCESS_5350
430 wpa_driver_roboswitch_read(drv
, ROBO_VLAN_PAGE
, ROBO_VLAN_READ
, _read
,
431 drv
->is_5350
? 2 : 1);
432 if (!(drv
->is_5350
? _read
[1] & (1 << 4) : _read
[0] & (1 << 14))) {
433 wpa_printf(MSG_INFO
, "%s: Could not get port information for "
434 "VLAN %d", __func__
, vlan
& ~(1 << 13));
438 drv
->ports
= _read
[0] & 0x001F;
439 /* add the MII port */
440 drv
->ports
|= 1 << 8;
441 if (wpa_driver_roboswitch_join(drv
, drv
->ports
, pae_group_addr
) < 0) {
442 wpa_printf(MSG_INFO
, "%s: Unable to join PAE group", __func__
);
446 wpa_printf(MSG_DEBUG
, "%s: Added PAE group address to "
447 "RoboSwitch ARL", __func__
);
454 static void wpa_driver_roboswitch_deinit(void *priv
)
456 struct wpa_driver_roboswitch_data
*drv
= priv
;
459 l2_packet_deinit(drv
->l2
);
462 if (wpa_driver_roboswitch_leave(drv
, drv
->ports
, pae_group_addr
) < 0) {
463 wpa_printf(MSG_DEBUG
, "%s: Unable to leave PAE group",
472 const struct wpa_driver_ops wpa_driver_roboswitch_ops
= {
473 .name
= "roboswitch",
474 .desc
= "wpa_supplicant roboswitch driver",
475 .get_ssid
= wpa_driver_roboswitch_get_ssid
,
476 .get_bssid
= wpa_driver_roboswitch_get_bssid
,
477 .get_capa
= wpa_driver_roboswitch_get_capa
,
478 .init
= wpa_driver_roboswitch_init
,
479 .deinit
= wpa_driver_roboswitch_deinit
,
480 .set_param
= wpa_driver_roboswitch_set_param
,
481 .get_ifname
= wpa_driver_roboswitch_get_ifname
,