Add RoboSwitch driver interface for wpa_supplicant
[libeap.git] / src / drivers / driver_roboswitch.c
1 /*
2  * WPA Supplicant - roboswitch driver interface
3  * Copyright (c) 2008 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_READ          0x0C    /* VLAN read register */
51
52
53 static const u8 pae_group_addr[ETH_ALEN] =
54 { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x03 };
55
56
57 struct wpa_driver_roboswitch_data {
58         void *ctx;
59         char ifname[IFNAMSIZ + 1];
60         struct ifreq ifr;
61         int fd;
62         u16 ports;
63 };
64
65
66 /* Copied from the kernel-only part of mii.h. */
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 static u16 wpa_driver_roboswitch_mdio_read(
74                 struct wpa_driver_roboswitch_data *drv, u8 reg)
75 {
76         struct mii_ioctl_data *mii = if_mii(&drv->ifr);
77
78         mii->phy_id = ROBO_PHY_ADDR;
79         mii->reg_num = reg;
80
81         if (ioctl(drv->fd, SIOCGMIIREG, &drv->ifr) < 0) {
82                 perror("ioctl[SIOCGMIIREG]");
83                 return 0x00;
84         }
85         return mii->val_out;
86 }
87
88
89 static void wpa_driver_roboswitch_mdio_write(
90                 struct wpa_driver_roboswitch_data *drv, u8 reg, u16 val)
91 {
92         struct mii_ioctl_data *mii = if_mii(&drv->ifr);
93
94         mii->phy_id = ROBO_PHY_ADDR;
95         mii->reg_num = reg;
96         mii->val_in = val;
97
98         if (ioctl(drv->fd, SIOCSMIIREG, &drv->ifr) < 0) {
99                 perror("ioctl[SIOCSMIIREG");
100         }
101 }
102
103
104 static int wpa_driver_roboswitch_reg(struct wpa_driver_roboswitch_data *drv,
105                                      u8 page, u8 reg, u8 op)
106 {
107         int i;
108
109         /* set page number */
110         wpa_driver_roboswitch_mdio_write(drv, ROBO_MII_PAGE,
111                                          (page << 8) | ROBO_MII_PAGE_ENABLE);
112         /* set register address */
113         wpa_driver_roboswitch_mdio_write(drv, ROBO_MII_ADDR, (reg << 8) | op);
114
115         /* check if operation completed */
116         for (i = 0; i < ROBO_MII_RETRY_MAX; ++i) {
117                 if ((wpa_driver_roboswitch_mdio_read(drv, ROBO_MII_ADDR) & 3) ==
118                     0) {
119                         return 0;
120                 }
121         }
122         /* timeout */
123         return -1;
124 }
125
126
127 static int wpa_driver_roboswitch_read(struct wpa_driver_roboswitch_data *drv,
128                                       u8 page, u8 reg, u16 *val, int len)
129 {
130         int i;
131
132         if (len > ROBO_MII_DATA_MAX ||
133             wpa_driver_roboswitch_reg(drv, page, reg, ROBO_MII_ADDR_READ) < 0) {
134                 return -1;
135         }
136         for (i = 0; i < len; ++i) {
137                 val[i] = wpa_driver_roboswitch_mdio_read(drv,
138                                                       ROBO_MII_DATA_OFFSET + i);
139         }
140         return 0;
141 }
142
143
144 static int wpa_driver_roboswitch_write(struct wpa_driver_roboswitch_data *drv,
145                                        u8 page, u8 reg, u16 *val, int len)
146 {
147         int i;
148
149         if (len > ROBO_MII_DATA_MAX) return -1;
150         for (i = 0; i < len; ++i) {
151                 wpa_driver_roboswitch_mdio_write(drv, ROBO_MII_DATA_OFFSET + i,
152                                                  val[i]);
153         }
154         return wpa_driver_roboswitch_reg(drv, page, reg, ROBO_MII_ADDR_WRITE);
155 }
156
157
158 static int wpa_driver_roboswitch_get_ssid(void *priv, u8 *ssid)
159 {
160         ssid[0] = 0;
161         return 0;
162 }
163
164
165 static int wpa_driver_roboswitch_get_bssid(void *priv, u8 *bssid)
166 {
167         /* Report PAE group address as the "BSSID" for wired connection. */
168         os_memcpy(bssid, pae_group_addr, ETH_ALEN);
169         return 0;
170 }
171
172
173 static const char * wpa_driver_roboswitch_get_ifname(void *priv)
174 {
175         struct wpa_driver_roboswitch_data *drv = priv;
176         return drv->ifname;
177 }
178
179
180 static int wpa_driver_roboswitch_join(struct wpa_driver_roboswitch_data *drv,
181                                       const u8 *addr)
182 {
183         int i;
184         u16 _read, zero = 0;
185         /* For reasons of simplicity we assume ETH_ALEN is even. */
186         u16 addr_word[ETH_ALEN/2];
187         /* RoboSwitch uses 16-bit Big Endian addresses.                 */
188         /* The ordering of the words is reversed in the MII registers.  */
189         for (i = 0; i < ETH_ALEN; i += 2) {
190                 addr_word[(ETH_ALEN - i) / 2 - 1] = WPA_GET_BE16(addr + i);
191         }
192
193         /* check if multiport addresses are not yet enabled */
194         if (wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE,
195                                        ROBO_ARLCTRL_CONF, &_read, 1) < 0) {
196                 return -1;
197         }
198         if (!(_read & (1 << 4))){
199                 _read |= 1 << 4;
200                 wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
201                                             ROBO_ARLCTRL_ADDR_1, addr_word, 3);
202                 wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
203                                             ROBO_ARLCTRL_VEC_1, &drv->ports, 1);
204                 wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
205                                             ROBO_ARLCTRL_VEC_2, &zero, 1);
206                 wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
207                                             ROBO_ARLCTRL_CONF, &_read, 1);
208                 return 0;
209         }
210         /* check if multiport address 1 is free */
211         wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE, ROBO_ARLCTRL_VEC_1,
212                                    &_read, 1);
213         if (_read == 0) {
214                 wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
215                                             ROBO_ARLCTRL_ADDR_1, addr_word, 3);
216                 wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
217                                             ROBO_ARLCTRL_VEC_1, &drv->ports, 1);
218                 return 0;
219         }
220         /* check if multiport address 2 is free */
221         wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE, ROBO_ARLCTRL_VEC_2,
222                                    &_read, 1);
223         if (_read == 0) {
224                 wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
225                                             ROBO_ARLCTRL_ADDR_2, addr_word, 3);
226                 wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
227                                             ROBO_ARLCTRL_VEC_2, &drv->ports, 1);
228                 return 0;
229         }
230         /* out of free multiport addresses */
231         return -1;
232 }
233
234
235 static int wpa_driver_roboswitch_leave(struct wpa_driver_roboswitch_data *drv,
236                                        const u8 *addr)
237 {
238         int i;
239         u8 mport[4] = { ROBO_ARLCTRL_VEC_1, ROBO_ARLCTRL_ADDR_1,
240                         ROBO_ARLCTRL_VEC_2, ROBO_ARLCTRL_ADDR_2 };
241         u16 _read[3], zero = 0;
242         /* same as at join */
243         u16 addr_word[ETH_ALEN/2];
244         for (i = 0; i < ETH_ALEN; i += 2) {
245                 addr_word[(ETH_ALEN - i) / 2 - 1] = WPA_GET_BE16(addr + i);
246         }
247
248         /* find our address/vector pair */
249         for (i = 0; i < 4; i += 2) {
250                 wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE, mport[i],
251                                            _read, 1);
252                 if (_read[0] == drv->ports) {
253                         wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE,
254                                                    mport[i + 1], _read, 3);
255                         if (os_memcmp(read, addr_word, 6) == 0)
256                                 break;
257                 }
258         }
259         /* check if we found our address/vector pair and deactivate it */
260         if (i == 4) return -1;
261         wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE, mport[i], &zero, 1);
262
263         /* leave the multiport registers in a sane state */
264         wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE, ROBO_ARLCTRL_VEC_1,
265                                    _read, 1);
266         if (_read[0] == 0) {
267                 wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE,
268                                            ROBO_ARLCTRL_VEC_2, _read, 1);
269                 if (_read[0] == 0) {
270                         wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE,
271                                                    ROBO_ARLCTRL_CONF, _read, 1);
272                         _read[0] &= ~(1 << 4);
273                         wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
274                                                     ROBO_ARLCTRL_CONF, _read, 1);
275                 } else {
276                         wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE,
277                                                    ROBO_ARLCTRL_ADDR_2, _read,
278                                                    3);
279                         wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
280                                                     ROBO_ARLCTRL_ADDR_1, _read,
281                                                     3);
282                         wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE,
283                                                    ROBO_ARLCTRL_VEC_2, _read, 1);
284                         wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
285                                                     ROBO_ARLCTRL_VEC_1, _read,
286                                                     1);
287                         wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
288                                                     ROBO_ARLCTRL_VEC_2, &zero,
289                                                     1);
290                 }
291         }
292         return 0;
293 }
294
295
296 static void * wpa_driver_roboswitch_init(void *ctx, const char *ifname)
297 {
298         struct wpa_driver_roboswitch_data *drv;
299         int len = -1, sep = -1;
300         u16 vlan = 0, vlan_read[2];
301
302         drv = os_zalloc(sizeof(*drv));
303         if (drv == NULL) return NULL;
304         drv->ctx = ctx;
305
306         while (ifname[++len]) if (ifname[len] == '.') sep = len;
307         if (sep < 0 || sep >= len - 1) {
308                 wpa_printf(MSG_INFO, "%s: No <interface>.<vlan> pair in "
309                            "interfacename %s", __func__, ifname);
310                 os_free(drv);
311                 return NULL;
312         }
313         if (sep > IFNAMSIZ) {
314                 wpa_printf(MSG_INFO, "%s: Interfacename %s is too long",
315                            __func__, ifname);
316                 os_free(drv);
317                 return NULL;
318         }
319         os_memcpy(drv->ifname, ifname, sep);
320         drv->ifname[sep] = '\0';
321         while (++sep < len) {
322                 if (ifname[sep] < '0' || ifname[sep] > '9') {
323                         wpa_printf(MSG_INFO, "%s: Invalid vlan specification "
324                                    "in interfacename %s", __func__, ifname);
325                         os_free(drv);
326                         return NULL;
327                 }
328                 vlan *= 10;
329                 vlan += ifname[sep] - '0';
330                 if (vlan > 255) {
331                         wpa_printf(MSG_INFO, "%s: VLAN out of range in "
332                                    "interfacename %s", __func__, ifname);
333                         os_free(drv);
334                         return NULL;
335                 }
336         }
337
338         drv->fd = socket(PF_INET, SOCK_DGRAM, 0);
339         if (drv->fd < 0) {
340                 wpa_printf(MSG_INFO, "%s: Unable to create socket", __func__);
341                 os_free(drv);
342                 return NULL;
343         }
344
345         os_memset(&drv->ifr, 0, sizeof(drv->ifr));
346         os_strlcpy(drv->ifr.ifr_name, drv->ifname, IFNAMSIZ);
347         if (ioctl(drv->fd, SIOCGMIIPHY, &drv->ifr) < 0) {
348                 perror("ioctl[SIOCGMIIPHY]");
349                 os_free(drv);
350                 return NULL;
351         }
352         if (if_mii(&drv->ifr)->phy_id != ROBO_PHY_ADDR) {
353                 wpa_printf(MSG_INFO, "%s: Invalid phy address (not a "
354                            "RoboSwitch?)", __func__);
355                 os_free(drv);
356                 return NULL;
357         }
358
359         vlan |= 1 << 13;
360         /* The BCM5365 uses a different register and is not accounted for. */
361         wpa_driver_roboswitch_write(drv, ROBO_VLAN_PAGE, ROBO_VLAN_ACCESS,
362                                     &vlan, 1);
363         wpa_driver_roboswitch_read(drv, ROBO_VLAN_PAGE, ROBO_VLAN_READ,
364                                    vlan_read, 2);
365         if (!(vlan_read[1] & (1 << 4))) {
366                 wpa_printf(MSG_INFO, "%s: Could not get port information for "
367                                      "VLAN %d", __func__, vlan & ~(1 << 13));
368                 os_free(drv);
369                 return NULL;
370         }
371         drv->ports = vlan_read[0] & 0x001F;
372         /* add the MII port */
373         drv->ports |= 1 << 8;
374         if (wpa_driver_roboswitch_join(drv, pae_group_addr) < 0) {
375                 wpa_printf(MSG_INFO, "%s: Unable to join PAE group", __func__);
376                 os_free(drv);
377                 return NULL;
378         } else {
379                 wpa_printf(MSG_DEBUG, "%s: Added PAE group address to "
380                                       "RoboSwitch ARL", __func__);
381         }
382
383         return drv;
384 }
385
386
387 static void wpa_driver_roboswitch_deinit(void *priv)
388 {
389         struct wpa_driver_roboswitch_data *drv = priv;
390
391         if (wpa_driver_roboswitch_leave(drv, pae_group_addr) < 0) {
392                 wpa_printf(MSG_DEBUG, "%s: Unable to leave PAE group",
393                            __func__);
394         }
395
396         close(drv->fd);
397         os_free(drv);
398 }
399
400
401 const struct wpa_driver_ops wpa_driver_roboswitch_ops = {
402         .name = "roboswitch",
403         .desc = "wpa_supplicant roboswitch driver",
404         .get_ssid = wpa_driver_roboswitch_get_ssid,
405         .get_bssid = wpa_driver_roboswitch_get_bssid,
406         .init = wpa_driver_roboswitch_init,
407         .deinit = wpa_driver_roboswitch_deinit,
408         .get_ifname = wpa_driver_roboswitch_get_ifname,
409 };