vlan_init.c 16 KB

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