driver_roboswitch.c 13 KB

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