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"
27 #define ETH_P_EAPOL 0x888e
30 #define ROBO_PHY_ADDR 0x1e /* RoboSwitch PHY address */
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 */
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 */
44 #define ROBO_ARLCTRL_PAGE 0x04 /* ARL control page */
45 #define ROBO_VLAN_PAGE 0x34 /* VLAN page */
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 */
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 */
61 static const u8 pae_group_addr
[ETH_ALEN
] =
62 { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x03 };
65 struct wpa_driver_roboswitch_data
{
67 struct l2_packet_data
*l2
;
68 char ifname
[IFNAMSIZ
+ 1];
69 u8 own_addr
[ETH_ALEN
];
76 /* Copied from the kernel-only part of mii.h. */
77 static inline struct mii_ioctl_data
*if_mii(struct ifreq
*rq
)
79 return (struct mii_ioctl_data
*) &rq
->ifr_ifru
;
84 * RoboSwitch uses 16-bit Big Endian addresses.
85 * The ordering of the words is reversed in the MII registers.
87 static void wpa_driver_roboswitch_addr_be16(const u8 addr
[ETH_ALEN
], u16
*be
)
90 for (i
= 0; i
< ETH_ALEN
; i
+= 2)
91 be
[(ETH_ALEN
- i
) / 2 - 1] = WPA_GET_BE16(addr
+ i
);
95 static u16
wpa_driver_roboswitch_mdio_read(
96 struct wpa_driver_roboswitch_data
*drv
, u8 reg
)
98 struct mii_ioctl_data
*mii
= if_mii(&drv
->ifr
);
100 mii
->phy_id
= ROBO_PHY_ADDR
;
103 if (ioctl(drv
->fd
, SIOCGMIIREG
, &drv
->ifr
) < 0) {
104 perror("ioctl[SIOCGMIIREG]");
111 static void wpa_driver_roboswitch_mdio_write(
112 struct wpa_driver_roboswitch_data
*drv
, u8 reg
, u16 val
)
114 struct mii_ioctl_data
*mii
= if_mii(&drv
->ifr
);
116 mii
->phy_id
= ROBO_PHY_ADDR
;
120 if (ioctl(drv
->fd
, SIOCSMIIREG
, &drv
->ifr
) < 0) {
121 perror("ioctl[SIOCSMIIREG");
126 static int wpa_driver_roboswitch_reg(struct wpa_driver_roboswitch_data
*drv
,
127 u8 page
, u8 reg
, u8 op
)
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
);
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)
148 static int wpa_driver_roboswitch_read(struct wpa_driver_roboswitch_data
*drv
,
149 u8 page
, u8 reg
, u16
*val
, int len
)
153 if (len
> ROBO_MII_DATA_MAX
||
154 wpa_driver_roboswitch_reg(drv
, page
, reg
, ROBO_MII_ADDR_READ
) < 0)
157 for (i
= 0; i
< len
; ++i
) {
158 val
[i
] = wpa_driver_roboswitch_mdio_read(
159 drv
, ROBO_MII_DATA_OFFSET
+ i
);
166 static int wpa_driver_roboswitch_write(struct wpa_driver_roboswitch_data
*drv
,
167 u8 page
, u8 reg
, u16
*val
, int len
)
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
,
176 return wpa_driver_roboswitch_reg(drv
, page
, reg
, ROBO_MII_ADDR_WRITE
);
180 static void wpa_driver_roboswitch_receive(void *priv
, const u8
*src_addr
,
181 const u8
*buf
, size_t len
)
183 struct wpa_driver_roboswitch_data
*drv
= priv
;
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,
193 static int wpa_driver_roboswitch_get_ssid(void *priv
, u8
*ssid
)
200 static int wpa_driver_roboswitch_get_bssid(void *priv
, u8
*bssid
)
202 /* Report PAE group address as the "BSSID" for wired connection. */
203 os_memcpy(bssid
, pae_group_addr
, ETH_ALEN
);
208 static int wpa_driver_roboswitch_set_param(void *priv
, const char *param
)
210 struct wpa_driver_roboswitch_data
*drv
= priv
;
213 if (param
== NULL
|| os_strstr(param
, "multicast_only=1") == NULL
) {
214 sep
= drv
->ifname
+ os_strlen(drv
->ifname
);
216 drv
->l2
= l2_packet_init(drv
->ifname
, NULL
, ETH_P_ALL
,
217 wpa_driver_roboswitch_receive
, drv
,
219 if (drv
->l2
== NULL
) {
220 wpa_printf(MSG_INFO
, "%s: Unable to listen on %s",
221 __func__
, drv
->ifname
);
225 l2_packet_get_own_addr(drv
->l2
, drv
->own_addr
);
227 wpa_printf(MSG_DEBUG
, "%s: Ignoring unicast frames", __func__
);
234 static const char * wpa_driver_roboswitch_get_ifname(void *priv
)
236 struct wpa_driver_roboswitch_data
*drv
= priv
;
241 static int wpa_driver_roboswitch_join(struct wpa_driver_roboswitch_data
*drv
,
242 u16 ports
, const u8
*addr
)
244 u16 read1
[3], read2
[3], addr_be16
[3];
246 wpa_driver_roboswitch_addr_be16(addr
, addr_be16
);
248 if (wpa_driver_roboswitch_read(drv
, ROBO_ARLCTRL_PAGE
,
249 ROBO_ARLCTRL_CONF
, read1
, 1) < 0)
251 if (!(read1
[0] & (1 << 4))) {
252 /* multiport addresses are not yet enabled */
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);
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)
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])
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);
287 static int wpa_driver_roboswitch_leave(struct wpa_driver_roboswitch_data
*drv
,
288 u16 ports
, const u8
*addr
)
290 u16 _read
, addr_be16
[3], addr_read
[3], ports_read
;
292 wpa_driver_roboswitch_addr_be16(addr
, addr_be16
);
294 wpa_driver_roboswitch_read(drv
, ROBO_ARLCTRL_PAGE
, ROBO_ARLCTRL_CONF
,
296 /* If ARL control is disabled, there is nothing to leave. */
297 if (!(_read
& (1 << 4))) return -1;
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
,
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
) {
313 wpa_driver_roboswitch_write(drv
, ROBO_ARLCTRL_PAGE
,
314 ROBO_ARLCTRL_CONF
, &_read
,
317 wpa_driver_roboswitch_read(drv
, ROBO_ARLCTRL_PAGE
,
320 wpa_driver_roboswitch_read(drv
, ROBO_ARLCTRL_PAGE
,
323 wpa_driver_roboswitch_write(drv
, ROBO_ARLCTRL_PAGE
,
326 wpa_driver_roboswitch_write(drv
, ROBO_ARLCTRL_PAGE
,
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
,
341 wpa_driver_roboswitch_write(drv
, ROBO_ARLCTRL_PAGE
,
350 static void * wpa_driver_roboswitch_init(void *ctx
, const char *ifname
)
352 struct wpa_driver_roboswitch_data
*drv
;
354 u16 vlan
= 0, _read
[2];
356 drv
= os_zalloc(sizeof(*drv
));
357 if (drv
== NULL
) return NULL
;
359 drv
->own_addr
[0] = '\0';
361 /* copy ifname and take a pointer to the second to last character */
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
);
374 if (*sep
< '0' || *sep
> '9') {
375 wpa_printf(MSG_INFO
, "%s: Invalid vlan specification "
376 "in interface name %s", __func__
, ifname
);
382 if (vlan
> ROBO_VLAN_MAX
) {
383 wpa_printf(MSG_INFO
, "%s: VLAN out of range in "
384 "interface name %s", __func__
, ifname
);
390 drv
->fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
392 wpa_printf(MSG_INFO
, "%s: Unable to create socket", __func__
);
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]");
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__
);
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
,
415 wpa_driver_roboswitch_read(drv
, ROBO_VLAN_PAGE
, ROBO_VLAN_ACCESS_5350
,
417 drv
->is_5350
= _read
[0] == _read
[1];
419 /* set the read bit */
421 wpa_driver_roboswitch_write(drv
, ROBO_VLAN_PAGE
,
422 drv
->is_5350
? ROBO_VLAN_ACCESS_5350
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));
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__
);
441 wpa_printf(MSG_DEBUG
, "%s: Added PAE group address to "
442 "RoboSwitch ARL", __func__
);
449 static void wpa_driver_roboswitch_deinit(void *priv
)
451 struct wpa_driver_roboswitch_data
*drv
= priv
;
454 l2_packet_deinit(drv
->l2
);
457 if (wpa_driver_roboswitch_leave(drv
, drv
->ports
, pae_group_addr
) < 0) {
458 wpa_printf(MSG_DEBUG
, "%s: Unable to leave PAE group",
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
,