vlan_init.c 20 KB

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