driver_roboswitch.c 12 KB

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