vlan_init.c 16 KB

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