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