1 /*
2 * WPA Supplicant - roboswitch driver interface
3 * Copyright (c) 2008-2009 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 perror("ioctl[SIOCGMIIREG]");
95 return 0x00;
96 }
97 return mii->val_out;
98 }
99
100
wpa_driver_roboswitch_mdio_write(struct wpa_driver_roboswitch_data * drv,u8 reg,u16 val)101 static void wpa_driver_roboswitch_mdio_write(
102 struct wpa_driver_roboswitch_data *drv, u8 reg, u16 val)
103 {
104 struct mii_ioctl_data *mii = if_mii(&drv->ifr);
105
106 mii->phy_id = ROBO_PHY_ADDR;
107 mii->reg_num = reg;
108 mii->val_in = val;
109
110 if (ioctl(drv->fd, SIOCSMIIREG, &drv->ifr) < 0) {
111 perror("ioctl[SIOCSMIIREG");
112 }
113 }
114
115
wpa_driver_roboswitch_reg(struct wpa_driver_roboswitch_data * drv,u8 page,u8 reg,u8 op)116 static int wpa_driver_roboswitch_reg(struct wpa_driver_roboswitch_data *drv,
117 u8 page, u8 reg, u8 op)
118 {
119 int i;
120
121 /* set page number */
122 wpa_driver_roboswitch_mdio_write(drv, ROBO_MII_PAGE,
123 (page << 8) | ROBO_MII_PAGE_ENABLE);
124 /* set register address */
125 wpa_driver_roboswitch_mdio_write(drv, ROBO_MII_ADDR, (reg << 8) | op);
126
127 /* check if operation completed */
128 for (i = 0; i < ROBO_MII_RETRY_MAX; ++i) {
129 if ((wpa_driver_roboswitch_mdio_read(drv, ROBO_MII_ADDR) & 3)
130 == 0)
131 return 0;
132 }
133 /* timeout */
134 return -1;
135 }
136
137
wpa_driver_roboswitch_read(struct wpa_driver_roboswitch_data * drv,u8 page,u8 reg,u16 * val,int len)138 static int wpa_driver_roboswitch_read(struct wpa_driver_roboswitch_data *drv,
139 u8 page, u8 reg, u16 *val, int len)
140 {
141 int i;
142
143 if (len > ROBO_MII_DATA_MAX ||
144 wpa_driver_roboswitch_reg(drv, page, reg, ROBO_MII_ADDR_READ) < 0)
145 return -1;
146
147 for (i = 0; i < len; ++i) {
148 val[i] = wpa_driver_roboswitch_mdio_read(
149 drv, ROBO_MII_DATA_OFFSET + i);
150 }
151
152 return 0;
153 }
154
155
wpa_driver_roboswitch_write(struct wpa_driver_roboswitch_data * drv,u8 page,u8 reg,u16 * val,int len)156 static int wpa_driver_roboswitch_write(struct wpa_driver_roboswitch_data *drv,
157 u8 page, u8 reg, u16 *val, int len)
158 {
159 int i;
160
161 if (len > ROBO_MII_DATA_MAX) return -1;
162 for (i = 0; i < len; ++i) {
163 wpa_driver_roboswitch_mdio_write(drv, ROBO_MII_DATA_OFFSET + i,
164 val[i]);
165 }
166 return wpa_driver_roboswitch_reg(drv, page, reg, ROBO_MII_ADDR_WRITE);
167 }
168
169
wpa_driver_roboswitch_receive(void * priv,const u8 * src_addr,const u8 * buf,size_t len)170 static void wpa_driver_roboswitch_receive(void *priv, const u8 *src_addr,
171 const u8 *buf, size_t len)
172 {
173 struct wpa_driver_roboswitch_data *drv = priv;
174
175 if (len > 14 && WPA_GET_BE16(buf + 12) == ETH_P_EAPOL &&
176 os_memcmp(buf, drv->own_addr, ETH_ALEN) == 0)
177 drv_event_eapol_rx(drv->ctx, src_addr, buf + 14, len - 14);
178 }
179
180
wpa_driver_roboswitch_get_ssid(void * priv,u8 * ssid)181 static int wpa_driver_roboswitch_get_ssid(void *priv, u8 *ssid)
182 {
183 ssid[0] = 0;
184 return 0;
185 }
186
187
wpa_driver_roboswitch_get_bssid(void * priv,u8 * bssid)188 static int wpa_driver_roboswitch_get_bssid(void *priv, u8 *bssid)
189 {
190 /* Report PAE group address as the "BSSID" for wired connection. */
191 os_memcpy(bssid, pae_group_addr, ETH_ALEN);
192 return 0;
193 }
194
195
wpa_driver_roboswitch_get_capa(void * priv,struct wpa_driver_capa * capa)196 static int wpa_driver_roboswitch_get_capa(void *priv,
197 struct wpa_driver_capa *capa)
198 {
199 os_memset(capa, 0, sizeof(*capa));
200 capa->flags = WPA_DRIVER_FLAGS_WIRED;
201 return 0;
202 }
203
204
wpa_driver_roboswitch_set_param(void * priv,const char * param)205 static int wpa_driver_roboswitch_set_param(void *priv, const char *param)
206 {
207 struct wpa_driver_roboswitch_data *drv = priv;
208 char *sep;
209
210 if (param == NULL || os_strstr(param, "multicast_only=1") == NULL) {
211 sep = drv->ifname + os_strlen(drv->ifname);
212 *sep = '.';
213 drv->l2 = l2_packet_init(drv->ifname, NULL, ETH_P_ALL,
214 wpa_driver_roboswitch_receive, drv,
215 1);
216 if (drv->l2 == NULL) {
217 wpa_printf(MSG_INFO, "%s: Unable to listen on %s",
218 __func__, drv->ifname);
219 return -1;
220 }
221 *sep = '\0';
222 l2_packet_get_own_addr(drv->l2, drv->own_addr);
223 } else {
224 wpa_printf(MSG_DEBUG, "%s: Ignoring unicast frames", __func__);
225 drv->l2 = NULL;
226 }
227 return 0;
228 }
229
230
wpa_driver_roboswitch_get_ifname(void * priv)231 static const char * wpa_driver_roboswitch_get_ifname(void *priv)
232 {
233 struct wpa_driver_roboswitch_data *drv = priv;
234 return drv->ifname;
235 }
236
237
wpa_driver_roboswitch_join(struct wpa_driver_roboswitch_data * drv,u16 ports,const u8 * addr)238 static int wpa_driver_roboswitch_join(struct wpa_driver_roboswitch_data *drv,
239 u16 ports, const u8 *addr)
240 {
241 u16 read1[3], read2[3], addr_be16[3];
242
243 wpa_driver_roboswitch_addr_be16(addr, addr_be16);
244
245 if (wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE,
246 ROBO_ARLCTRL_CONF, read1, 1) < 0)
247 return -1;
248 if (!(read1[0] & (1 << 4))) {
249 /* multiport addresses are not yet enabled */
250 read1[0] |= 1 << 4;
251 wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
252 ROBO_ARLCTRL_ADDR_1, addr_be16, 3);
253 wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
254 ROBO_ARLCTRL_VEC_1, &ports, 1);
255 wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
256 ROBO_ARLCTRL_ADDR_2, addr_be16, 3);
257 wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
258 ROBO_ARLCTRL_VEC_2, &ports, 1);
259 wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
260 ROBO_ARLCTRL_CONF, read1, 1);
261 } else {
262 /* if both multiport addresses are the same we can add */
263 wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE,
264 ROBO_ARLCTRL_ADDR_1, read1, 3);
265 wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE,
266 ROBO_ARLCTRL_ADDR_2, read2, 3);
267 if (os_memcmp(read1, read2, 6) != 0)
268 return -1;
269 wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE,
270 ROBO_ARLCTRL_VEC_1, read1, 1);
271 wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE,
272 ROBO_ARLCTRL_VEC_2, read2, 1);
273 if (read1[0] != read2[0])
274 return -1;
275 wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
276 ROBO_ARLCTRL_ADDR_1, addr_be16, 3);
277 wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
278 ROBO_ARLCTRL_VEC_1, &ports, 1);
279 }
280 return 0;
281 }
282
283
wpa_driver_roboswitch_leave(struct wpa_driver_roboswitch_data * drv,u16 ports,const u8 * addr)284 static int wpa_driver_roboswitch_leave(struct wpa_driver_roboswitch_data *drv,
285 u16 ports, const u8 *addr)
286 {
287 u16 _read, addr_be16[3], addr_read[3], ports_read;
288
289 wpa_driver_roboswitch_addr_be16(addr, addr_be16);
290
291 wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE, ROBO_ARLCTRL_CONF,
292 &_read, 1);
293 /* If ARL control is disabled, there is nothing to leave. */
294 if (!(_read & (1 << 4))) return -1;
295
296 wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE,
297 ROBO_ARLCTRL_ADDR_1, addr_read, 3);
298 wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE, ROBO_ARLCTRL_VEC_1,
299 &ports_read, 1);
300 /* check if we occupy multiport address 1 */
301 if (os_memcmp(addr_read, addr_be16, 6) == 0 && ports_read == ports) {
302 wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE,
303 ROBO_ARLCTRL_ADDR_2, addr_read, 3);
304 wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE,
305 ROBO_ARLCTRL_VEC_2, &ports_read, 1);
306 /* and multiport address 2 */
307 if (os_memcmp(addr_read, addr_be16, 6) == 0 &&
308 ports_read == ports) {
309 _read &= ~(1 << 4);
310 wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
311 ROBO_ARLCTRL_CONF, &_read,
312 1);
313 } else {
314 wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE,
315 ROBO_ARLCTRL_ADDR_1,
316 addr_read, 3);
317 wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE,
318 ROBO_ARLCTRL_VEC_1,
319 &ports_read, 1);
320 wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
321 ROBO_ARLCTRL_ADDR_2,
322 addr_read, 3);
323 wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
324 ROBO_ARLCTRL_VEC_2,
325 &ports_read, 1);
326 }
327 } else {
328 wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE,
329 ROBO_ARLCTRL_ADDR_2, addr_read, 3);
330 wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE,
331 ROBO_ARLCTRL_VEC_2, &ports_read, 1);
332 /* or multiport address 2 */
333 if (os_memcmp(addr_read, addr_be16, 6) == 0 &&
334 ports_read == ports) {
335 wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
336 ROBO_ARLCTRL_ADDR_1,
337 addr_read, 3);
338 wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
339 ROBO_ARLCTRL_VEC_1,
340 &ports_read, 1);
341 } else return -1;
342 }
343 return 0;
344 }
345
346
wpa_driver_roboswitch_init(void * ctx,const char * ifname)347 static void * wpa_driver_roboswitch_init(void *ctx, const char *ifname)
348 {
349 struct wpa_driver_roboswitch_data *drv;
350 char *sep;
351 u16 vlan = 0, _read[2];
352
353 drv = os_zalloc(sizeof(*drv));
354 if (drv == NULL) return NULL;
355 drv->ctx = ctx;
356 drv->own_addr[0] = '\0';
357
358 /* copy ifname and take a pointer to the second to last character */
359 sep = drv->ifname +
360 os_strlcpy(drv->ifname, ifname, sizeof(drv->ifname)) - 2;
361 /* find the '.' separating <interface> and <vlan> */
362 while (sep > drv->ifname && *sep != '.') sep--;
363 if (sep <= drv->ifname) {
364 wpa_printf(MSG_INFO, "%s: No <interface>.<vlan> pair in "
365 "interface name %s", __func__, drv->ifname);
366 os_free(drv);
367 return NULL;
368 }
369 *sep = '\0';
370 while (*++sep) {
371 if (*sep < '0' || *sep > '9') {
372 wpa_printf(MSG_INFO, "%s: Invalid vlan specification "
373 "in interface name %s", __func__, ifname);
374 os_free(drv);
375 return NULL;
376 }
377 vlan *= 10;
378 vlan += *sep - '0';
379 if (vlan > ROBO_VLAN_MAX) {
380 wpa_printf(MSG_INFO, "%s: VLAN out of range in "
381 "interface name %s", __func__, ifname);
382 os_free(drv);
383 return NULL;
384 }
385 }
386
387 drv->fd = socket(PF_INET, SOCK_DGRAM, 0);
388 if (drv->fd < 0) {
389 wpa_printf(MSG_INFO, "%s: Unable to create socket", __func__);
390 os_free(drv);
391 return NULL;
392 }
393
394 os_memset(&drv->ifr, 0, sizeof(drv->ifr));
395 os_strlcpy(drv->ifr.ifr_name, drv->ifname, IFNAMSIZ);
396 if (ioctl(drv->fd, SIOCGMIIPHY, &drv->ifr) < 0) {
397 perror("ioctl[SIOCGMIIPHY]");
398 os_free(drv);
399 return NULL;
400 }
401 if (if_mii(&drv->ifr)->phy_id != ROBO_PHY_ADDR) {
402 wpa_printf(MSG_INFO, "%s: Invalid phy address (not a "
403 "RoboSwitch?)", __func__);
404 os_free(drv);
405 return NULL;
406 }
407
408 /* set and read back to see if the register can be used */
409 _read[0] = ROBO_VLAN_MAX;
410 wpa_driver_roboswitch_write(drv, ROBO_VLAN_PAGE, ROBO_VLAN_ACCESS_5350,
411 _read, 1);
412 wpa_driver_roboswitch_read(drv, ROBO_VLAN_PAGE, ROBO_VLAN_ACCESS_5350,
413 _read + 1, 1);
414 drv->is_5350 = _read[0] == _read[1];
415
416 /* set the read bit */
417 vlan |= 1 << 13;
418 wpa_driver_roboswitch_write(drv, ROBO_VLAN_PAGE,
419 drv->is_5350 ? ROBO_VLAN_ACCESS_5350
420 : ROBO_VLAN_ACCESS,
421 &vlan, 1);
422 wpa_driver_roboswitch_read(drv, ROBO_VLAN_PAGE, ROBO_VLAN_READ, _read,
423 drv->is_5350 ? 2 : 1);
424 if (!(drv->is_5350 ? _read[1] & (1 << 4) : _read[0] & (1 << 14))) {
425 wpa_printf(MSG_INFO, "%s: Could not get port information for "
426 "VLAN %d", __func__, vlan & ~(1 << 13));
427 os_free(drv);
428 return NULL;
429 }
430 drv->ports = _read[0] & 0x001F;
431 /* add the MII port */
432 drv->ports |= 1 << 8;
433 if (wpa_driver_roboswitch_join(drv, drv->ports, pae_group_addr) < 0) {
434 wpa_printf(MSG_INFO, "%s: Unable to join PAE group", __func__);
435 os_free(drv);
436 return NULL;
437 } else {
438 wpa_printf(MSG_DEBUG, "%s: Added PAE group address to "
439 "RoboSwitch ARL", __func__);
440 }
441
442 return drv;
443 }
444
445
wpa_driver_roboswitch_deinit(void * priv)446 static void wpa_driver_roboswitch_deinit(void *priv)
447 {
448 struct wpa_driver_roboswitch_data *drv = priv;
449
450 if (drv->l2) {
451 l2_packet_deinit(drv->l2);
452 drv->l2 = NULL;
453 }
454 if (wpa_driver_roboswitch_leave(drv, drv->ports, pae_group_addr) < 0) {
455 wpa_printf(MSG_DEBUG, "%s: Unable to leave PAE group",
456 __func__);
457 }
458
459 close(drv->fd);
460 os_free(drv);
461 }
462
463
464 const struct wpa_driver_ops wpa_driver_roboswitch_ops = {
465 .name = "roboswitch",
466 .desc = "wpa_supplicant roboswitch driver",
467 .get_ssid = wpa_driver_roboswitch_get_ssid,
468 .get_bssid = wpa_driver_roboswitch_get_bssid,
469 .get_capa = wpa_driver_roboswitch_get_capa,
470 .init = wpa_driver_roboswitch_init,
471 .deinit = wpa_driver_roboswitch_deinit,
472 .set_param = wpa_driver_roboswitch_set_param,
473 .get_ifname = wpa_driver_roboswitch_get_ifname,
474 };
475