vlan_init.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  1. /*
  2. * hostapd / VLAN initialization
  3. * Copyright 2003, Instant802 Networks, Inc.
  4. * Copyright 2005-2006, Devicescape Software, Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Alternatively, this software may be distributed under the terms of BSD
  11. * license.
  12. *
  13. * See README and COPYING for more details.
  14. */
  15. #include "includes.h"
  16. #include "hostapd.h"
  17. #include "driver_i.h"
  18. #include "vlan_init.h"
  19. #ifdef CONFIG_FULL_DYNAMIC_VLAN
  20. #include <net/if.h>
  21. #include <sys/ioctl.h>
  22. #include <linux/sockios.h>
  23. #include <linux/if_vlan.h>
  24. #include <linux/if_bridge.h>
  25. #include "drivers/priv_netlink.h"
  26. #include "eloop.h"
  27. struct full_dynamic_vlan {
  28. int s; /* socket on which to listen for new/removed interfaces. */
  29. };
  30. static int ifconfig_helper(const char *if_name, int up)
  31. {
  32. int fd;
  33. struct ifreq ifr;
  34. if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  35. perror("socket[AF_INET,SOCK_STREAM]");
  36. return -1;
  37. }
  38. os_memset(&ifr, 0, sizeof(ifr));
  39. os_strlcpy(ifr.ifr_name, if_name, IFNAMSIZ);
  40. if (ioctl(fd, SIOCGIFFLAGS, &ifr) != 0) {
  41. perror("ioctl[SIOCGIFFLAGS]");
  42. close(fd);
  43. return -1;
  44. }
  45. if (up)
  46. ifr.ifr_flags |= IFF_UP;
  47. else
  48. ifr.ifr_flags &= ~IFF_UP;
  49. if (ioctl(fd, SIOCSIFFLAGS, &ifr) != 0) {
  50. perror("ioctl[SIOCSIFFLAGS]");
  51. close(fd);
  52. return -1;
  53. }
  54. close(fd);
  55. return 0;
  56. }
  57. static int ifconfig_up(const char *if_name)
  58. {
  59. return ifconfig_helper(if_name, 1);
  60. }
  61. static int ifconfig_down(const char *if_name)
  62. {
  63. return ifconfig_helper(if_name, 0);
  64. }
  65. /*
  66. * These are only available in recent linux headers (without the leading
  67. * underscore).
  68. */
  69. #define _GET_VLAN_REALDEV_NAME_CMD 8
  70. #define _GET_VLAN_VID_CMD 9
  71. /* This value should be 256 ONLY. If it is something else, then hostapd
  72. * might crash!, as this value has been hard-coded in 2.4.x kernel
  73. * bridging code.
  74. */
  75. #define MAX_BR_PORTS 256
  76. static int br_delif(const char *br_name, const char *if_name)
  77. {
  78. int fd;
  79. struct ifreq ifr;
  80. unsigned long args[2];
  81. int if_index;
  82. if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  83. perror("socket[AF_INET,SOCK_STREAM]");
  84. return -1;
  85. }
  86. if_index = if_nametoindex(if_name);
  87. if (if_index == 0) {
  88. printf("Failure determining interface index for '%s'\n",
  89. if_name);
  90. close(fd);
  91. return -1;
  92. }
  93. args[0] = BRCTL_DEL_IF;
  94. args[1] = if_index;
  95. os_strlcpy(ifr.ifr_name, br_name, sizeof(ifr.ifr_name));
  96. ifr.ifr_data = (__caddr_t) args;
  97. if (ioctl(fd, SIOCDEVPRIVATE, &ifr) < 0 && errno != EINVAL) {
  98. /* No error if interface already removed. */
  99. perror("ioctl[SIOCDEVPRIVATE,BRCTL_DEL_IF]");
  100. close(fd);
  101. return -1;
  102. }
  103. close(fd);
  104. return 0;
  105. }
  106. /*
  107. Add interface 'if_name' to the bridge 'br_name'
  108. returns -1 on error
  109. returns 1 if the interface is already part of the bridge
  110. returns 0 otherwise
  111. */
  112. static int br_addif(const char *br_name, const char *if_name)
  113. {
  114. int fd;
  115. struct ifreq ifr;
  116. unsigned long args[2];
  117. int if_index;
  118. if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  119. perror("socket[AF_INET,SOCK_STREAM]");
  120. return -1;
  121. }
  122. if_index = if_nametoindex(if_name);
  123. if (if_index == 0) {
  124. printf("Failure determining interface index for '%s'\n",
  125. if_name);
  126. close(fd);
  127. return -1;
  128. }
  129. args[0] = BRCTL_ADD_IF;
  130. args[1] = if_index;
  131. os_strlcpy(ifr.ifr_name, br_name, sizeof(ifr.ifr_name));
  132. ifr.ifr_data = (__caddr_t) args;
  133. if (ioctl(fd, SIOCDEVPRIVATE, &ifr) < 0) {
  134. if (errno == EBUSY) {
  135. /* The interface is already added. */
  136. close(fd);
  137. return 1;
  138. }
  139. perror("ioctl[SIOCDEVPRIVATE,BRCTL_ADD_IF]");
  140. close(fd);
  141. return -1;
  142. }
  143. close(fd);
  144. return 0;
  145. }
  146. static int br_delbr(const char *br_name)
  147. {
  148. int fd;
  149. unsigned long arg[2];
  150. if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  151. perror("socket[AF_INET,SOCK_STREAM]");
  152. return -1;
  153. }
  154. arg[0] = BRCTL_DEL_BRIDGE;
  155. arg[1] = (unsigned long) br_name;
  156. if (ioctl(fd, SIOCGIFBR, arg) < 0 && errno != ENXIO) {
  157. /* No error if bridge already removed. */
  158. perror("ioctl[BRCTL_DEL_BRIDGE]");
  159. close(fd);
  160. return -1;
  161. }
  162. close(fd);
  163. return 0;
  164. }
  165. /*
  166. Add a bridge with the name 'br_name'.
  167. returns -1 on error
  168. returns 1 if the bridge already exists
  169. returns 0 otherwise
  170. */
  171. static int br_addbr(const char *br_name)
  172. {
  173. int fd;
  174. unsigned long arg[2];
  175. if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  176. perror("socket[AF_INET,SOCK_STREAM]");
  177. return -1;
  178. }
  179. arg[0] = BRCTL_ADD_BRIDGE;
  180. arg[1] = (unsigned long) br_name;
  181. if (ioctl(fd, SIOCGIFBR, arg) < 0) {
  182. if (errno == EEXIST) {
  183. /* The bridge is already added. */
  184. close(fd);
  185. return 1;
  186. } else {
  187. perror("ioctl[BRCTL_ADD_BRIDGE]");
  188. close(fd);
  189. return -1;
  190. }
  191. }
  192. close(fd);
  193. return 0;
  194. }
  195. static int br_getnumports(const char *br_name)
  196. {
  197. int fd;
  198. int i;
  199. int port_cnt = 0;
  200. unsigned long arg[4];
  201. int ifindices[MAX_BR_PORTS];
  202. struct ifreq ifr;
  203. if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  204. perror("socket[AF_INET,SOCK_STREAM]");
  205. return -1;
  206. }
  207. arg[0] = BRCTL_GET_PORT_LIST;
  208. arg[1] = (unsigned long) ifindices;
  209. arg[2] = MAX_BR_PORTS;
  210. arg[3] = 0;
  211. os_memset(ifindices, 0, sizeof(ifindices));
  212. os_strlcpy(ifr.ifr_name, br_name, sizeof(ifr.ifr_name));
  213. ifr.ifr_data = (__caddr_t) arg;
  214. if (ioctl(fd, SIOCDEVPRIVATE, &ifr) < 0) {
  215. perror("ioctl[SIOCDEVPRIVATE,BRCTL_GET_PORT_LIST]");
  216. close(fd);
  217. return -1;
  218. }
  219. for (i = 1; i < MAX_BR_PORTS; i++) {
  220. if (ifindices[i] > 0) {
  221. port_cnt++;
  222. }
  223. }
  224. close(fd);
  225. return port_cnt;
  226. }
  227. static int vlan_rem(const char *if_name)
  228. {
  229. int fd;
  230. struct vlan_ioctl_args if_request;
  231. if ((os_strlen(if_name) + 1) > sizeof(if_request.device1)) {
  232. fprintf(stderr, "Interface name to long.\n");
  233. return -1;
  234. }
  235. if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  236. perror("socket[AF_INET,SOCK_STREAM]");
  237. return -1;
  238. }
  239. os_memset(&if_request, 0, sizeof(if_request));
  240. os_strlcpy(if_request.device1, if_name, sizeof(if_request.device1));
  241. if_request.cmd = DEL_VLAN_CMD;
  242. if (ioctl(fd, SIOCSIFVLAN, &if_request) < 0) {
  243. perror("ioctl[SIOCSIFVLAN,DEL_VLAN_CMD]");
  244. close(fd);
  245. return -1;
  246. }
  247. close(fd);
  248. return 0;
  249. }
  250. /*
  251. Add a vlan interface with VLAN ID 'vid' and tagged interface
  252. 'if_name'.
  253. returns -1 on error
  254. returns 1 if the interface already exists
  255. returns 0 otherwise
  256. */
  257. static int vlan_add(const char *if_name, int vid)
  258. {
  259. int fd;
  260. struct vlan_ioctl_args if_request;
  261. ifconfig_up(if_name);
  262. if ((os_strlen(if_name) + 1) > sizeof(if_request.device1)) {
  263. fprintf(stderr, "Interface name to long.\n");
  264. return -1;
  265. }
  266. if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  267. perror("socket[AF_INET,SOCK_STREAM]");
  268. return -1;
  269. }
  270. os_memset(&if_request, 0, sizeof(if_request));
  271. /* Determine if a suitable vlan device already exists. */
  272. os_snprintf(if_request.device1, sizeof(if_request.device1), "vlan%d",
  273. vid);
  274. if_request.cmd = _GET_VLAN_VID_CMD;
  275. if (ioctl(fd, SIOCSIFVLAN, &if_request) == 0) {
  276. if (if_request.u.VID == vid) {
  277. if_request.cmd = _GET_VLAN_REALDEV_NAME_CMD;
  278. if (ioctl(fd, SIOCSIFVLAN, &if_request) == 0 &&
  279. os_strncmp(if_request.u.device2, if_name,
  280. sizeof(if_request.u.device2)) == 0) {
  281. close(fd);
  282. return 1;
  283. }
  284. }
  285. }
  286. /* A suitable vlan device does not already exist, add one. */
  287. os_memset(&if_request, 0, sizeof(if_request));
  288. os_strlcpy(if_request.device1, if_name, sizeof(if_request.device1));
  289. if_request.u.VID = vid;
  290. if_request.cmd = ADD_VLAN_CMD;
  291. if (ioctl(fd, SIOCSIFVLAN, &if_request) < 0) {
  292. perror("ioctl[SIOCSIFVLAN,ADD_VLAN_CMD]");
  293. close(fd);
  294. return -1;
  295. }
  296. close(fd);
  297. return 0;
  298. }
  299. static int vlan_set_name_type(unsigned int name_type)
  300. {
  301. int fd;
  302. struct vlan_ioctl_args if_request;
  303. if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  304. perror("socket[AF_INET,SOCK_STREAM]");
  305. return -1;
  306. }
  307. os_memset(&if_request, 0, sizeof(if_request));
  308. if_request.u.name_type = name_type;
  309. if_request.cmd = SET_VLAN_NAME_TYPE_CMD;
  310. if (ioctl(fd, SIOCSIFVLAN, &if_request) < 0) {
  311. perror("ioctl[SIOCSIFVLAN,SET_VLAN_NAME_TYPE_CMD]");
  312. close(fd);
  313. return -1;
  314. }
  315. close(fd);
  316. return 0;
  317. }
  318. static void vlan_newlink(char *ifname, struct hostapd_data *hapd)
  319. {
  320. char vlan_ifname[IFNAMSIZ];
  321. char br_name[IFNAMSIZ];
  322. struct hostapd_vlan *vlan = hapd->conf->vlan;
  323. char *tagged_interface = hapd->conf->ssid.vlan_tagged_interface;
  324. while (vlan) {
  325. if (os_strcmp(ifname, vlan->ifname) == 0) {
  326. os_snprintf(br_name, sizeof(br_name), "brvlan%d",
  327. vlan->vlan_id);
  328. if (!br_addbr(br_name))
  329. vlan->clean |= DVLAN_CLEAN_BR;
  330. ifconfig_up(br_name);
  331. if (tagged_interface) {
  332. if (!vlan_add(tagged_interface, vlan->vlan_id))
  333. vlan->clean |= DVLAN_CLEAN_VLAN;
  334. os_snprintf(vlan_ifname, sizeof(vlan_ifname),
  335. "vlan%d", vlan->vlan_id);
  336. if (!br_addif(br_name, vlan_ifname))
  337. vlan->clean |= DVLAN_CLEAN_VLAN_PORT;
  338. ifconfig_up(vlan_ifname);
  339. }
  340. if (!br_addif(br_name, ifname))
  341. vlan->clean |= DVLAN_CLEAN_WLAN_PORT;
  342. ifconfig_up(ifname);
  343. break;
  344. }
  345. vlan = vlan->next;
  346. }
  347. }
  348. static void vlan_dellink(char *ifname, struct hostapd_data *hapd)
  349. {
  350. char vlan_ifname[IFNAMSIZ];
  351. char br_name[IFNAMSIZ];
  352. struct hostapd_vlan *first, *prev, *vlan = hapd->conf->vlan;
  353. char *tagged_interface = hapd->conf->ssid.vlan_tagged_interface;
  354. int numports;
  355. first = prev = vlan;
  356. while (vlan) {
  357. if (os_strcmp(ifname, vlan->ifname) == 0) {
  358. os_snprintf(br_name, sizeof(br_name), "brvlan%d",
  359. vlan->vlan_id);
  360. if (tagged_interface) {
  361. os_snprintf(vlan_ifname, sizeof(vlan_ifname),
  362. "vlan%d", vlan->vlan_id);
  363. numports = br_getnumports(br_name);
  364. if (numports == 1) {
  365. br_delif(br_name, vlan_ifname);
  366. vlan_rem(vlan_ifname);
  367. ifconfig_down(br_name);
  368. br_delbr(br_name);
  369. }
  370. }
  371. if (vlan == first) {
  372. hapd->conf->vlan = vlan->next;
  373. } else {
  374. prev->next = vlan->next;
  375. }
  376. os_free(vlan);
  377. break;
  378. }
  379. prev = vlan;
  380. vlan = vlan->next;
  381. }
  382. }
  383. static void
  384. vlan_read_ifnames(struct nlmsghdr *h, size_t len, int del,
  385. struct hostapd_data *hapd)
  386. {
  387. struct ifinfomsg *ifi;
  388. int attrlen, nlmsg_len, rta_len;
  389. struct rtattr *attr;
  390. if (len < sizeof(*ifi))
  391. return;
  392. ifi = NLMSG_DATA(h);
  393. nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg));
  394. attrlen = h->nlmsg_len - nlmsg_len;
  395. if (attrlen < 0)
  396. return;
  397. attr = (struct rtattr *) (((char *) ifi) + nlmsg_len);
  398. rta_len = RTA_ALIGN(sizeof(struct rtattr));
  399. while (RTA_OK(attr, attrlen)) {
  400. char ifname[IFNAMSIZ + 1];
  401. if (attr->rta_type == IFLA_IFNAME) {
  402. int n = attr->rta_len - rta_len;
  403. if (n < 0)
  404. break;
  405. os_memset(ifname, 0, sizeof(ifname));
  406. if ((size_t) n > sizeof(ifname))
  407. n = sizeof(ifname);
  408. os_memcpy(ifname, ((char *) attr) + rta_len, n);
  409. if (del)
  410. vlan_dellink(ifname, hapd);
  411. else
  412. vlan_newlink(ifname, hapd);
  413. }
  414. attr = RTA_NEXT(attr, attrlen);
  415. }
  416. }
  417. static void vlan_event_receive(int sock, void *eloop_ctx, void *sock_ctx)
  418. {
  419. char buf[8192];
  420. int left;
  421. struct sockaddr_nl from;
  422. socklen_t fromlen;
  423. struct nlmsghdr *h;
  424. struct hostapd_data *hapd = eloop_ctx;
  425. fromlen = sizeof(from);
  426. left = recvfrom(sock, buf, sizeof(buf), MSG_DONTWAIT,
  427. (struct sockaddr *) &from, &fromlen);
  428. if (left < 0) {
  429. if (errno != EINTR && errno != EAGAIN)
  430. perror("recvfrom(netlink)");
  431. return;
  432. }
  433. h = (struct nlmsghdr *) buf;
  434. while (left >= (int) sizeof(*h)) {
  435. int len, plen;
  436. len = h->nlmsg_len;
  437. plen = len - sizeof(*h);
  438. if (len > left || plen < 0) {
  439. printf("Malformed netlink message: "
  440. "len=%d left=%d plen=%d", len, left, plen);
  441. break;
  442. }
  443. switch (h->nlmsg_type) {
  444. case RTM_NEWLINK:
  445. vlan_read_ifnames(h, plen, 0, hapd);
  446. break;
  447. case RTM_DELLINK:
  448. vlan_read_ifnames(h, plen, 1, hapd);
  449. break;
  450. }
  451. len = NLMSG_ALIGN(len);
  452. left -= len;
  453. h = (struct nlmsghdr *) ((char *) h + len);
  454. }
  455. if (left > 0) {
  456. printf("%d extra bytes in the end of netlink message",
  457. left);
  458. }
  459. }
  460. static struct full_dynamic_vlan *
  461. full_dynamic_vlan_init(struct hostapd_data *hapd)
  462. {
  463. struct sockaddr_nl local;
  464. struct full_dynamic_vlan *priv;
  465. priv = os_zalloc(sizeof(*priv));
  466. if (priv == NULL)
  467. return NULL;
  468. vlan_set_name_type(VLAN_NAME_TYPE_PLUS_VID_NO_PAD);
  469. priv->s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  470. if (priv->s < 0) {
  471. perror("socket(PF_NETLINK,SOCK_RAW,NETLINK_ROUTE)");
  472. os_free(priv);
  473. return NULL;
  474. }
  475. os_memset(&local, 0, sizeof(local));
  476. local.nl_family = AF_NETLINK;
  477. local.nl_groups = RTMGRP_LINK;
  478. if (bind(priv->s, (struct sockaddr *) &local, sizeof(local)) < 0) {
  479. perror("bind(netlink)");
  480. close(priv->s);
  481. os_free(priv);
  482. return NULL;
  483. }
  484. if (eloop_register_read_sock(priv->s, vlan_event_receive, hapd, NULL))
  485. {
  486. close(priv->s);
  487. os_free(priv);
  488. return NULL;
  489. }
  490. return priv;
  491. }
  492. static void full_dynamic_vlan_deinit(struct full_dynamic_vlan *priv)
  493. {
  494. if (priv == NULL)
  495. return;
  496. eloop_unregister_read_sock(priv->s);
  497. close(priv->s);
  498. os_free(priv);
  499. }
  500. #endif /* CONFIG_FULL_DYNAMIC_VLAN */
  501. int vlan_setup_encryption_dyn(struct hostapd_data *hapd,
  502. struct hostapd_ssid *mssid, const char *dyn_vlan)
  503. {
  504. int i;
  505. if (dyn_vlan == NULL)
  506. return 0;
  507. /* Static WEP keys are set here; IEEE 802.1X and WPA uses their own
  508. * functions for setting up dynamic broadcast keys. */
  509. for (i = 0; i < 4; i++) {
  510. if (mssid->wep.key[i] &&
  511. hostapd_set_key(dyn_vlan, hapd, WPA_ALG_WEP, NULL, i,
  512. i == mssid->wep.idx, NULL, 0,
  513. mssid->wep.key[i], mssid->wep.len[i])) {
  514. printf("VLAN: Could not set WEP encryption for "
  515. "dynamic VLAN.\n");
  516. return -1;
  517. }
  518. }
  519. return 0;
  520. }
  521. static int vlan_dynamic_add(struct hostapd_data *hapd,
  522. struct hostapd_vlan *vlan)
  523. {
  524. while (vlan) {
  525. if (vlan->vlan_id != VLAN_ID_WILDCARD &&
  526. hostapd_if_add(hapd, HOSTAPD_IF_VLAN, vlan->ifname, NULL))
  527. {
  528. if (errno != EEXIST) {
  529. printf("Could not add VLAN iface: %s: %s\n",
  530. vlan->ifname, strerror(errno));
  531. return -1;
  532. }
  533. }
  534. vlan = vlan->next;
  535. }
  536. return 0;
  537. }
  538. static void vlan_dynamic_remove(struct hostapd_data *hapd,
  539. struct hostapd_vlan *vlan)
  540. {
  541. struct hostapd_vlan *next;
  542. while (vlan) {
  543. next = vlan->next;
  544. if (vlan->vlan_id != VLAN_ID_WILDCARD &&
  545. hostapd_if_remove(hapd, HOSTAPD_IF_VLAN, vlan->ifname,
  546. NULL)) {
  547. printf("Could not remove VLAN iface: %s: %s\n",
  548. vlan->ifname, strerror(errno));
  549. }
  550. #ifdef CONFIG_FULL_DYNAMIC_VLAN
  551. if (vlan->clean)
  552. vlan_dellink(vlan->ifname, hapd);
  553. #endif /* CONFIG_FULL_DYNAMIC_VLAN */
  554. vlan = next;
  555. }
  556. }
  557. int vlan_init(struct hostapd_data *hapd)
  558. {
  559. if (vlan_dynamic_add(hapd, hapd->conf->vlan))
  560. return -1;
  561. #ifdef CONFIG_FULL_DYNAMIC_VLAN
  562. hapd->full_dynamic_vlan = full_dynamic_vlan_init(hapd);
  563. #endif /* CONFIG_FULL_DYNAMIC_VLAN */
  564. return 0;
  565. }
  566. void vlan_deinit(struct hostapd_data *hapd)
  567. {
  568. vlan_dynamic_remove(hapd, hapd->conf->vlan);
  569. #ifdef CONFIG_FULL_DYNAMIC_VLAN
  570. full_dynamic_vlan_deinit(hapd->full_dynamic_vlan);
  571. #endif /* CONFIG_FULL_DYNAMIC_VLAN */
  572. }
  573. int vlan_reconfig(struct hostapd_data *hapd, struct hostapd_config *oldconf,
  574. struct hostapd_bss_config *oldbss)
  575. {
  576. vlan_dynamic_remove(hapd, oldbss->vlan);
  577. if (vlan_dynamic_add(hapd, hapd->conf->vlan))
  578. return -1;
  579. return 0;
  580. }
  581. struct hostapd_vlan * vlan_add_dynamic(struct hostapd_data *hapd,
  582. struct hostapd_vlan *vlan,
  583. int vlan_id)
  584. {
  585. struct hostapd_vlan *n;
  586. char *ifname, *pos;
  587. if (vlan == NULL || vlan_id <= 0 || vlan_id > MAX_VLAN_ID ||
  588. vlan->vlan_id != VLAN_ID_WILDCARD)
  589. return NULL;
  590. ifname = os_strdup(vlan->ifname);
  591. if (ifname == NULL)
  592. return NULL;
  593. pos = os_strchr(ifname, '#');
  594. if (pos == NULL) {
  595. os_free(ifname);
  596. return NULL;
  597. }
  598. *pos++ = '\0';
  599. n = os_zalloc(sizeof(*n));
  600. if (n == NULL) {
  601. os_free(ifname);
  602. return NULL;
  603. }
  604. n->vlan_id = vlan_id;
  605. n->dynamic_vlan = 1;
  606. os_snprintf(n->ifname, sizeof(n->ifname), "%s%d%s", ifname, vlan_id,
  607. pos);
  608. os_free(ifname);
  609. if (hostapd_if_add(hapd, HOSTAPD_IF_VLAN, n->ifname, NULL)) {
  610. os_free(n);
  611. return NULL;
  612. }
  613. n->next = hapd->conf->vlan;
  614. hapd->conf->vlan = n;
  615. return n;
  616. }
  617. int vlan_remove_dynamic(struct hostapd_data *hapd, int vlan_id)
  618. {
  619. struct hostapd_vlan *vlan;
  620. if (vlan_id <= 0 || vlan_id > MAX_VLAN_ID)
  621. return 1;
  622. vlan = hapd->conf->vlan;
  623. while (vlan) {
  624. if (vlan->vlan_id == vlan_id && vlan->dynamic_vlan > 0) {
  625. vlan->dynamic_vlan--;
  626. break;
  627. }
  628. vlan = vlan->next;
  629. }
  630. if (vlan == NULL)
  631. return 1;
  632. if (vlan->dynamic_vlan == 0)
  633. hostapd_if_remove(hapd, HOSTAPD_IF_VLAN, vlan->ifname, NULL);
  634. return 0;
  635. }