vlan_init.c 24 KB

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