driver_roboswitch.c
Go to the documentation of this file.
00001 /*
00002  * WPA Supplicant - roboswitch driver interface
00003  * Copyright (c) 2008-2009 Jouke Witteveen
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License version 2 as
00007  * published by the Free Software Foundation.
00008  *
00009  * Alternatively, this software may be distributed under the terms of BSD
00010  * license.
00011  *
00012  * See README and COPYING for more details.
00013  */
00014 
00015 #include "includes.h"
00016 #include <sys/ioctl.h>
00017 #include <linux/if.h>
00018 #include <linux/sockios.h>
00019 #include <linux/if_ether.h>
00020 #include <linux/mii.h>
00021 
00022 #include "common.h"
00023 #include "driver.h"
00024 #include "l2_packet/l2_packet.h"
00025 
00026 #define ROBO_PHY_ADDR           0x1e    /* RoboSwitch PHY address */
00027 
00028 /* MII access registers */
00029 #define ROBO_MII_PAGE           0x10    /* MII page register */
00030 #define ROBO_MII_ADDR           0x11    /* MII address register */
00031 #define ROBO_MII_DATA_OFFSET    0x18    /* Start of MII data registers */
00032 
00033 #define ROBO_MII_PAGE_ENABLE    0x01    /* MII page op code */
00034 #define ROBO_MII_ADDR_WRITE     0x01    /* MII address write op code */
00035 #define ROBO_MII_ADDR_READ      0x02    /* MII address read op code */
00036 #define ROBO_MII_DATA_MAX          4    /* Consecutive MII data registers */
00037 #define ROBO_MII_RETRY_MAX        10    /* Read attempts before giving up */
00038 
00039 /* Page numbers */
00040 #define ROBO_ARLCTRL_PAGE       0x04    /* ARL control page */
00041 #define ROBO_VLAN_PAGE          0x34    /* VLAN page */
00042 
00043 /* ARL control page registers */
00044 #define ROBO_ARLCTRL_CONF       0x00    /* ARL configuration register */
00045 #define ROBO_ARLCTRL_ADDR_1     0x10    /* Multiport address 1 */
00046 #define ROBO_ARLCTRL_VEC_1      0x16    /* Multiport vector 1 */
00047 #define ROBO_ARLCTRL_ADDR_2     0x20    /* Multiport address 2 */
00048 #define ROBO_ARLCTRL_VEC_2      0x26    /* Multiport vector 2 */
00049 
00050 /* VLAN page registers */
00051 #define ROBO_VLAN_ACCESS        0x08    /* VLAN table access register */
00052 #define ROBO_VLAN_ACCESS_5350   0x06    /* VLAN table access register (5350) */
00053 #define ROBO_VLAN_READ          0x0c    /* VLAN read register */
00054 #define ROBO_VLAN_MAX           0xff    /* Maximum number of VLANs */
00055 
00056 
00057 static const u8 pae_group_addr[ETH_ALEN] =
00058 { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x03 };
00059 
00060 
00061 struct wpa_driver_roboswitch_data {
00062         void *ctx;
00063         struct l2_packet_data *l2;
00064         char ifname[IFNAMSIZ + 1];
00065         u8 own_addr[ETH_ALEN];
00066         struct ifreq ifr;
00067         int fd, is_5350;
00068         u16 ports;
00069 };
00070 
00071 
00072 /* Copied from the kernel-only part of mii.h. */
00073 static inline struct mii_ioctl_data *if_mii(struct ifreq *rq)
00074 {
00075         return (struct mii_ioctl_data *) &rq->ifr_ifru;
00076 }
00077 
00078 
00079 /*
00080  * RoboSwitch uses 16-bit Big Endian addresses.
00081  * The ordering of the words is reversed in the MII registers.
00082  */
00083 static void wpa_driver_roboswitch_addr_be16(const u8 addr[ETH_ALEN], u16 *be)
00084 {
00085         int i;
00086         for (i = 0; i < ETH_ALEN; i += 2)
00087                 be[(ETH_ALEN - i) / 2 - 1] = WPA_GET_BE16(addr + i);
00088 }
00089 
00090 
00091 static u16 wpa_driver_roboswitch_mdio_read(
00092         struct wpa_driver_roboswitch_data *drv, u8 reg)
00093 {
00094         struct mii_ioctl_data *mii = if_mii(&drv->ifr);
00095 
00096         mii->phy_id = ROBO_PHY_ADDR;
00097         mii->reg_num = reg;
00098 
00099         if (ioctl(drv->fd, SIOCGMIIREG, &drv->ifr) < 0) {
00100                 perror("ioctl[SIOCGMIIREG]");
00101                 return 0x00;
00102         }
00103         return mii->val_out;
00104 }
00105 
00106 
00107 static void wpa_driver_roboswitch_mdio_write(
00108         struct wpa_driver_roboswitch_data *drv, u8 reg, u16 val)
00109 {
00110         struct mii_ioctl_data *mii = if_mii(&drv->ifr);
00111 
00112         mii->phy_id = ROBO_PHY_ADDR;
00113         mii->reg_num = reg;
00114         mii->val_in = val;
00115 
00116         if (ioctl(drv->fd, SIOCSMIIREG, &drv->ifr) < 0) {
00117                 perror("ioctl[SIOCSMIIREG");
00118         }
00119 }
00120 
00121 
00122 static int wpa_driver_roboswitch_reg(struct wpa_driver_roboswitch_data *drv,
00123                                      u8 page, u8 reg, u8 op)
00124 {
00125         int i;
00126 
00127         /* set page number */
00128         wpa_driver_roboswitch_mdio_write(drv, ROBO_MII_PAGE,
00129                                          (page << 8) | ROBO_MII_PAGE_ENABLE);
00130         /* set register address */
00131         wpa_driver_roboswitch_mdio_write(drv, ROBO_MII_ADDR, (reg << 8) | op);
00132 
00133         /* check if operation completed */
00134         for (i = 0; i < ROBO_MII_RETRY_MAX; ++i) {
00135                 if ((wpa_driver_roboswitch_mdio_read(drv, ROBO_MII_ADDR) & 3)
00136                     == 0)
00137                         return 0;
00138         }
00139         /* timeout */
00140         return -1;
00141 }
00142 
00143 
00144 static int wpa_driver_roboswitch_read(struct wpa_driver_roboswitch_data *drv,
00145                                       u8 page, u8 reg, u16 *val, int len)
00146 {
00147         int i;
00148 
00149         if (len > ROBO_MII_DATA_MAX ||
00150             wpa_driver_roboswitch_reg(drv, page, reg, ROBO_MII_ADDR_READ) < 0)
00151                 return -1;
00152 
00153         for (i = 0; i < len; ++i) {
00154                 val[i] = wpa_driver_roboswitch_mdio_read(
00155                         drv, ROBO_MII_DATA_OFFSET + i);
00156         }
00157 
00158         return 0;
00159 }
00160 
00161 
00162 static int wpa_driver_roboswitch_write(struct wpa_driver_roboswitch_data *drv,
00163                                        u8 page, u8 reg, u16 *val, int len)
00164 {
00165         int i;
00166 
00167         if (len > ROBO_MII_DATA_MAX) return -1;
00168         for (i = 0; i < len; ++i) {
00169                 wpa_driver_roboswitch_mdio_write(drv, ROBO_MII_DATA_OFFSET + i,
00170                                                  val[i]);
00171         }
00172         return wpa_driver_roboswitch_reg(drv, page, reg, ROBO_MII_ADDR_WRITE);
00173 }
00174 
00175 
00176 static void wpa_driver_roboswitch_receive(void *priv, const u8 *src_addr,
00177                                           const u8 *buf, size_t len)
00178 {
00179         struct wpa_driver_roboswitch_data *drv = priv;
00180 
00181         if (len > 14 && WPA_GET_BE16(buf + 12) == ETH_P_EAPOL &&
00182             os_memcmp(buf, drv->own_addr, ETH_ALEN) == 0)
00183                 drv_event_eapol_rx(drv->ctx, src_addr, buf + 14, len - 14);
00184 }
00185 
00186 
00187 static int wpa_driver_roboswitch_get_ssid(void *priv, u8 *ssid)
00188 {
00189         ssid[0] = 0;
00190         return 0;
00191 }
00192 
00193 
00194 static int wpa_driver_roboswitch_get_bssid(void *priv, u8 *bssid)
00195 {
00196         /* Report PAE group address as the "BSSID" for wired connection. */
00197         os_memcpy(bssid, pae_group_addr, ETH_ALEN);
00198         return 0;
00199 }
00200 
00201 
00202 static int wpa_driver_roboswitch_get_capa(void *priv,
00203                                           struct wpa_driver_capa *capa)
00204 {
00205         os_memset(capa, 0, sizeof(*capa));
00206         capa->flags = WPA_DRIVER_FLAGS_WIRED;
00207         return 0;
00208 }
00209 
00210 
00211 static int wpa_driver_roboswitch_set_param(void *priv, const char *param)
00212 {
00213         struct wpa_driver_roboswitch_data *drv = priv;
00214         char *sep;
00215 
00216         if (param == NULL || os_strstr(param, "multicast_only=1") == NULL) {
00217                 sep = drv->ifname + os_strlen(drv->ifname);
00218                 *sep = '.';
00219                 drv->l2 = l2_packet_init(drv->ifname, NULL, ETH_P_ALL,
00220                                          wpa_driver_roboswitch_receive, drv,
00221                                          1);
00222                 if (drv->l2 == NULL) {
00223                         wpa_printf(MSG_INFO, "%s: Unable to listen on %s",
00224                                    __func__, drv->ifname);
00225                         return -1;
00226                 }
00227                 *sep = '\0';
00228                 l2_packet_get_own_addr(drv->l2, drv->own_addr);
00229         } else {
00230                 wpa_printf(MSG_DEBUG, "%s: Ignoring unicast frames", __func__);
00231                 drv->l2 = NULL;
00232         }
00233         return 0;
00234 }
00235 
00236 
00237 static const char * wpa_driver_roboswitch_get_ifname(void *priv)
00238 {
00239         struct wpa_driver_roboswitch_data *drv = priv;
00240         return drv->ifname;
00241 }
00242 
00243 
00244 static int wpa_driver_roboswitch_join(struct wpa_driver_roboswitch_data *drv,
00245                                       u16 ports, const u8 *addr)
00246 {
00247         u16 read1[3], read2[3], addr_be16[3];
00248 
00249         wpa_driver_roboswitch_addr_be16(addr, addr_be16);
00250 
00251         if (wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE,
00252                                        ROBO_ARLCTRL_CONF, read1, 1) < 0)
00253                 return -1;
00254         if (!(read1[0] & (1 << 4))) {
00255                 /* multiport addresses are not yet enabled */
00256                 read1[0] |= 1 << 4;
00257                 wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
00258                                             ROBO_ARLCTRL_ADDR_1, addr_be16, 3);
00259                 wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
00260                                             ROBO_ARLCTRL_VEC_1, &ports, 1);
00261                 wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
00262                                             ROBO_ARLCTRL_ADDR_2, addr_be16, 3);
00263                 wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
00264                                             ROBO_ARLCTRL_VEC_2, &ports, 1);
00265                 wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
00266                                             ROBO_ARLCTRL_CONF, read1, 1);
00267         } else {
00268                 /* if both multiport addresses are the same we can add */
00269                 wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE,
00270                                            ROBO_ARLCTRL_ADDR_1, read1, 3);
00271                 wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE,
00272                                            ROBO_ARLCTRL_ADDR_2, read2, 3);
00273                 if (os_memcmp(read1, read2, 6) != 0)
00274                         return -1;
00275                 wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE,
00276                                            ROBO_ARLCTRL_VEC_1, read1, 1);
00277                 wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE,
00278                                            ROBO_ARLCTRL_VEC_2, read2, 1);
00279                 if (read1[0] != read2[0])
00280                         return -1;
00281                 wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
00282                                             ROBO_ARLCTRL_ADDR_1, addr_be16, 3);
00283                 wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
00284                                             ROBO_ARLCTRL_VEC_1, &ports, 1);
00285         }
00286         return 0;
00287 }
00288 
00289 
00290 static int wpa_driver_roboswitch_leave(struct wpa_driver_roboswitch_data *drv,
00291                                        u16 ports, const u8 *addr)
00292 {
00293         u16 _read, addr_be16[3], addr_read[3], ports_read;
00294 
00295         wpa_driver_roboswitch_addr_be16(addr, addr_be16);
00296 
00297         wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE, ROBO_ARLCTRL_CONF,
00298                                    &_read, 1);
00299         /* If ARL control is disabled, there is nothing to leave. */
00300         if (!(_read & (1 << 4))) return -1;
00301 
00302         wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE,
00303                                    ROBO_ARLCTRL_ADDR_1, addr_read, 3);
00304         wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE, ROBO_ARLCTRL_VEC_1,
00305                                    &ports_read, 1);
00306         /* check if we occupy multiport address 1 */
00307         if (os_memcmp(addr_read, addr_be16, 6) == 0 && ports_read == ports) {
00308                 wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE,
00309                                            ROBO_ARLCTRL_ADDR_2, addr_read, 3);
00310                 wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE,
00311                                            ROBO_ARLCTRL_VEC_2, &ports_read, 1);
00312                 /* and multiport address 2 */
00313                 if (os_memcmp(addr_read, addr_be16, 6) == 0 &&
00314                     ports_read == ports) {
00315                         _read &= ~(1 << 4);
00316                         wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
00317                                                     ROBO_ARLCTRL_CONF, &_read,
00318                                                     1);
00319                 } else {
00320                         wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE,
00321                                                    ROBO_ARLCTRL_ADDR_1,
00322                                                    addr_read, 3);
00323                         wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE,
00324                                                    ROBO_ARLCTRL_VEC_1,
00325                                                    &ports_read, 1);
00326                         wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
00327                                                     ROBO_ARLCTRL_ADDR_2,
00328                                                     addr_read, 3);
00329                         wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
00330                                                     ROBO_ARLCTRL_VEC_2,
00331                                                     &ports_read, 1);
00332                 }
00333         } else {
00334                 wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE,
00335                                            ROBO_ARLCTRL_ADDR_2, addr_read, 3);
00336                 wpa_driver_roboswitch_read(drv, ROBO_ARLCTRL_PAGE,
00337                                            ROBO_ARLCTRL_VEC_2, &ports_read, 1);
00338                 /* or multiport address 2 */
00339                 if (os_memcmp(addr_read, addr_be16, 6) == 0 &&
00340                     ports_read == ports) {
00341                         wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
00342                                                     ROBO_ARLCTRL_ADDR_1,
00343                                                     addr_read, 3);
00344                         wpa_driver_roboswitch_write(drv, ROBO_ARLCTRL_PAGE,
00345                                                     ROBO_ARLCTRL_VEC_1,
00346                                                     &ports_read, 1);
00347                 } else return -1;
00348         }
00349         return 0;
00350 }
00351 
00352 
00353 static void * wpa_driver_roboswitch_init(void *ctx, const char *ifname)
00354 {
00355         struct wpa_driver_roboswitch_data *drv;
00356         char *sep;
00357         u16 vlan = 0, _read[2];
00358 
00359         drv = os_zalloc(sizeof(*drv));
00360         if (drv == NULL) return NULL;
00361         drv->ctx = ctx;
00362         drv->own_addr[0] = '\0';
00363 
00364         /* copy ifname and take a pointer to the second to last character */
00365         sep = drv->ifname +
00366               os_strlcpy(drv->ifname, ifname, sizeof(drv->ifname)) - 2;
00367         /* find the '.' seperating <interface> and <vlan> */
00368         while (sep > drv->ifname && *sep != '.') sep--;
00369         if (sep <= drv->ifname) {
00370                 wpa_printf(MSG_INFO, "%s: No <interface>.<vlan> pair in "
00371                            "interface name %s", __func__, drv->ifname);
00372                 os_free(drv);
00373                 return NULL;
00374         }
00375         *sep = '\0';
00376         while (*++sep) {
00377                 if (*sep < '0' || *sep > '9') {
00378                         wpa_printf(MSG_INFO, "%s: Invalid vlan specification "
00379                                    "in interface name %s", __func__, ifname);
00380                         os_free(drv);
00381                         return NULL;
00382                 }
00383                 vlan *= 10;
00384                 vlan += *sep - '0';
00385                 if (vlan > ROBO_VLAN_MAX) {
00386                         wpa_printf(MSG_INFO, "%s: VLAN out of range in "
00387                                    "interface name %s", __func__, ifname);
00388                         os_free(drv);
00389                         return NULL;
00390                 }
00391         }
00392 
00393         drv->fd = socket(PF_INET, SOCK_DGRAM, 0);
00394         if (drv->fd < 0) {
00395                 wpa_printf(MSG_INFO, "%s: Unable to create socket", __func__);
00396                 os_free(drv);
00397                 return NULL;
00398         }
00399 
00400         os_memset(&drv->ifr, 0, sizeof(drv->ifr));
00401         os_strlcpy(drv->ifr.ifr_name, drv->ifname, IFNAMSIZ);
00402         if (ioctl(drv->fd, SIOCGMIIPHY, &drv->ifr) < 0) {
00403                 perror("ioctl[SIOCGMIIPHY]");
00404                 os_free(drv);
00405                 return NULL;
00406         }
00407         if (if_mii(&drv->ifr)->phy_id != ROBO_PHY_ADDR) {
00408                 wpa_printf(MSG_INFO, "%s: Invalid phy address (not a "
00409                            "RoboSwitch?)", __func__);
00410                 os_free(drv);
00411                 return NULL;
00412         }
00413 
00414         /* set and read back to see if the register can be used */
00415         _read[0] = ROBO_VLAN_MAX;
00416         wpa_driver_roboswitch_write(drv, ROBO_VLAN_PAGE, ROBO_VLAN_ACCESS_5350,
00417                                     _read, 1);
00418         wpa_driver_roboswitch_read(drv, ROBO_VLAN_PAGE, ROBO_VLAN_ACCESS_5350,
00419                                    _read + 1, 1);
00420         drv->is_5350 = _read[0] == _read[1];
00421 
00422         /* set the read bit */
00423         vlan |= 1 << 13;
00424         wpa_driver_roboswitch_write(drv, ROBO_VLAN_PAGE,
00425                                     drv->is_5350 ? ROBO_VLAN_ACCESS_5350
00426                                                  : ROBO_VLAN_ACCESS,
00427                                     &vlan, 1);
00428         wpa_driver_roboswitch_read(drv, ROBO_VLAN_PAGE, ROBO_VLAN_READ, _read,
00429                                    drv->is_5350 ? 2 : 1);
00430         if (!(drv->is_5350 ? _read[1] & (1 << 4) : _read[0] & (1 << 14))) {
00431                 wpa_printf(MSG_INFO, "%s: Could not get port information for "
00432                                      "VLAN %d", __func__, vlan & ~(1 << 13));
00433                 os_free(drv);
00434                 return NULL;
00435         }
00436         drv->ports = _read[0] & 0x001F;
00437         /* add the MII port */
00438         drv->ports |= 1 << 8;
00439         if (wpa_driver_roboswitch_join(drv, drv->ports, pae_group_addr) < 0) {
00440                 wpa_printf(MSG_INFO, "%s: Unable to join PAE group", __func__);
00441                 os_free(drv);
00442                 return NULL;
00443         } else {
00444                 wpa_printf(MSG_DEBUG, "%s: Added PAE group address to "
00445                            "RoboSwitch ARL", __func__);
00446         }
00447 
00448         return drv;
00449 }
00450 
00451 
00452 static void wpa_driver_roboswitch_deinit(void *priv)
00453 {
00454         struct wpa_driver_roboswitch_data *drv = priv;
00455 
00456         if (drv->l2) {
00457                 l2_packet_deinit(drv->l2);
00458                 drv->l2 = NULL;
00459         }
00460         if (wpa_driver_roboswitch_leave(drv, drv->ports, pae_group_addr) < 0) {
00461                 wpa_printf(MSG_DEBUG, "%s: Unable to leave PAE group",
00462                            __func__);
00463         }
00464 
00465         close(drv->fd);
00466         os_free(drv);
00467 }
00468 
00469 
00470 const struct wpa_driver_ops wpa_driver_roboswitch_ops = {
00471         .name = "roboswitch",
00472         .desc = "wpa_supplicant roboswitch driver",
00473         .get_ssid = wpa_driver_roboswitch_get_ssid,
00474         .get_bssid = wpa_driver_roboswitch_get_bssid,
00475         .get_capa = wpa_driver_roboswitch_get_capa,
00476         .init = wpa_driver_roboswitch_init,
00477         .deinit = wpa_driver_roboswitch_deinit,
00478         .set_param = wpa_driver_roboswitch_set_param,
00479         .get_ifname = wpa_driver_roboswitch_get_ifname,
00480 };


wpa_supplicant
Author(s): Package maintained by Blaise Gassend
autogenerated on Thu Apr 24 2014 15:34:34