vlan_init.c 21 KB

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