vlan_full.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. /*
  2. * hostapd / VLAN initialization - full dynamic VLAN
  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. #include <net/if.h>
  12. /* Avoid conflicts due to NetBSD net/if.h if_type define with driver.h */
  13. #undef if_type
  14. #include <sys/ioctl.h>
  15. #include "utils/common.h"
  16. #include "drivers/priv_netlink.h"
  17. #include "common/linux_bridge.h"
  18. #include "common/linux_vlan.h"
  19. #include "utils/eloop.h"
  20. #include "hostapd.h"
  21. #include "ap_config.h"
  22. #include "ap_drv_ops.h"
  23. #include "wpa_auth.h"
  24. #include "vlan_init.h"
  25. #include "vlan_util.h"
  26. struct full_dynamic_vlan {
  27. int s; /* socket on which to listen for new/removed interfaces. */
  28. };
  29. #define DVLAN_CLEAN_BR 0x1
  30. #define DVLAN_CLEAN_VLAN 0x2
  31. #define DVLAN_CLEAN_VLAN_PORT 0x4
  32. struct dynamic_iface {
  33. char ifname[IFNAMSIZ + 1];
  34. int usage;
  35. int clean;
  36. struct dynamic_iface *next;
  37. };
  38. /* Increment ref counter for ifname and add clean flag.
  39. * If not in list, add it only if some flags are given.
  40. */
  41. static void dyn_iface_get(struct hostapd_data *hapd, const char *ifname,
  42. int clean)
  43. {
  44. struct dynamic_iface *next, **dynamic_ifaces;
  45. struct hapd_interfaces *interfaces;
  46. interfaces = hapd->iface->interfaces;
  47. dynamic_ifaces = &interfaces->vlan_priv;
  48. for (next = *dynamic_ifaces; next; next = next->next) {
  49. if (os_strcmp(ifname, next->ifname) == 0)
  50. break;
  51. }
  52. if (next) {
  53. next->usage++;
  54. next->clean |= clean;
  55. return;
  56. }
  57. if (!clean)
  58. return;
  59. next = os_zalloc(sizeof(*next));
  60. if (!next)
  61. return;
  62. os_strlcpy(next->ifname, ifname, sizeof(next->ifname));
  63. next->usage = 1;
  64. next->clean = clean;
  65. next->next = *dynamic_ifaces;
  66. *dynamic_ifaces = next;
  67. }
  68. /* Decrement reference counter for given ifname.
  69. * Return clean flag iff reference counter was decreased to zero, else zero
  70. */
  71. static int dyn_iface_put(struct hostapd_data *hapd, const char *ifname)
  72. {
  73. struct dynamic_iface *next, *prev = NULL, **dynamic_ifaces;
  74. struct hapd_interfaces *interfaces;
  75. int clean;
  76. interfaces = hapd->iface->interfaces;
  77. dynamic_ifaces = &interfaces->vlan_priv;
  78. for (next = *dynamic_ifaces; next; next = next->next) {
  79. if (os_strcmp(ifname, next->ifname) == 0)
  80. break;
  81. prev = next;
  82. }
  83. if (!next)
  84. return 0;
  85. next->usage--;
  86. if (next->usage)
  87. return 0;
  88. if (prev)
  89. prev->next = next->next;
  90. else
  91. *dynamic_ifaces = next->next;
  92. clean = next->clean;
  93. os_free(next);
  94. return clean;
  95. }
  96. static int ifconfig_down(const char *if_name)
  97. {
  98. wpa_printf(MSG_DEBUG, "VLAN: Set interface %s down", if_name);
  99. return ifconfig_helper(if_name, 0);
  100. }
  101. /* This value should be 256 ONLY. If it is something else, then hostapd
  102. * might crash!, as this value has been hard-coded in 2.4.x kernel
  103. * bridging code.
  104. */
  105. #define MAX_BR_PORTS 256
  106. static int br_delif(const char *br_name, const char *if_name)
  107. {
  108. int fd;
  109. struct ifreq ifr;
  110. unsigned long args[2];
  111. int if_index;
  112. wpa_printf(MSG_DEBUG, "VLAN: br_delif(%s, %s)", br_name, if_name);
  113. if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  114. wpa_printf(MSG_ERROR, "VLAN: %s: socket(AF_INET,SOCK_STREAM) "
  115. "failed: %s", __func__, strerror(errno));
  116. return -1;
  117. }
  118. if_index = if_nametoindex(if_name);
  119. if (if_index == 0) {
  120. wpa_printf(MSG_ERROR, "VLAN: %s: Failure determining "
  121. "interface index for '%s'",
  122. __func__, if_name);
  123. close(fd);
  124. return -1;
  125. }
  126. args[0] = BRCTL_DEL_IF;
  127. args[1] = if_index;
  128. os_strlcpy(ifr.ifr_name, br_name, sizeof(ifr.ifr_name));
  129. ifr.ifr_data = (void *) args;
  130. if (ioctl(fd, SIOCDEVPRIVATE, &ifr) < 0 && errno != EINVAL) {
  131. /* No error if interface already removed. */
  132. wpa_printf(MSG_ERROR, "VLAN: %s: ioctl[SIOCDEVPRIVATE,"
  133. "BRCTL_DEL_IF] failed for br_name=%s if_name=%s: "
  134. "%s", __func__, br_name, if_name, strerror(errno));
  135. close(fd);
  136. return -1;
  137. }
  138. close(fd);
  139. return 0;
  140. }
  141. /*
  142. Add interface 'if_name' to the bridge 'br_name'
  143. returns -1 on error
  144. returns 1 if the interface is already part of the bridge
  145. returns 0 otherwise
  146. */
  147. static int br_addif(const char *br_name, const char *if_name)
  148. {
  149. int fd;
  150. struct ifreq ifr;
  151. unsigned long args[2];
  152. int if_index;
  153. wpa_printf(MSG_DEBUG, "VLAN: br_addif(%s, %s)", br_name, if_name);
  154. if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  155. wpa_printf(MSG_ERROR, "VLAN: %s: socket(AF_INET,SOCK_STREAM) "
  156. "failed: %s", __func__, strerror(errno));
  157. return -1;
  158. }
  159. if_index = if_nametoindex(if_name);
  160. if (if_index == 0) {
  161. wpa_printf(MSG_ERROR, "VLAN: %s: Failure determining "
  162. "interface index for '%s'",
  163. __func__, if_name);
  164. close(fd);
  165. return -1;
  166. }
  167. args[0] = BRCTL_ADD_IF;
  168. args[1] = if_index;
  169. os_strlcpy(ifr.ifr_name, br_name, sizeof(ifr.ifr_name));
  170. ifr.ifr_data = (void *) args;
  171. if (ioctl(fd, SIOCDEVPRIVATE, &ifr) < 0) {
  172. if (errno == EBUSY) {
  173. /* The interface is already added. */
  174. close(fd);
  175. return 1;
  176. }
  177. wpa_printf(MSG_ERROR, "VLAN: %s: ioctl[SIOCDEVPRIVATE,"
  178. "BRCTL_ADD_IF] failed for br_name=%s if_name=%s: "
  179. "%s", __func__, br_name, if_name, strerror(errno));
  180. close(fd);
  181. return -1;
  182. }
  183. close(fd);
  184. return 0;
  185. }
  186. static int br_delbr(const char *br_name)
  187. {
  188. int fd;
  189. unsigned long arg[2];
  190. wpa_printf(MSG_DEBUG, "VLAN: br_delbr(%s)", br_name);
  191. if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  192. wpa_printf(MSG_ERROR, "VLAN: %s: socket(AF_INET,SOCK_STREAM) "
  193. "failed: %s", __func__, strerror(errno));
  194. return -1;
  195. }
  196. arg[0] = BRCTL_DEL_BRIDGE;
  197. arg[1] = (unsigned long) br_name;
  198. if (ioctl(fd, SIOCGIFBR, arg) < 0 && errno != ENXIO) {
  199. /* No error if bridge already removed. */
  200. wpa_printf(MSG_ERROR, "VLAN: %s: BRCTL_DEL_BRIDGE failed for "
  201. "%s: %s", __func__, br_name, strerror(errno));
  202. close(fd);
  203. return -1;
  204. }
  205. close(fd);
  206. return 0;
  207. }
  208. /*
  209. Add a bridge with the name 'br_name'.
  210. returns -1 on error
  211. returns 1 if the bridge already exists
  212. returns 0 otherwise
  213. */
  214. static int br_addbr(const char *br_name)
  215. {
  216. int fd;
  217. unsigned long arg[4];
  218. struct ifreq ifr;
  219. wpa_printf(MSG_DEBUG, "VLAN: br_addbr(%s)", br_name);
  220. if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  221. wpa_printf(MSG_ERROR, "VLAN: %s: socket(AF_INET,SOCK_STREAM) "
  222. "failed: %s", __func__, strerror(errno));
  223. return -1;
  224. }
  225. arg[0] = BRCTL_ADD_BRIDGE;
  226. arg[1] = (unsigned long) br_name;
  227. if (ioctl(fd, SIOCGIFBR, arg) < 0) {
  228. if (errno == EEXIST) {
  229. /* The bridge is already added. */
  230. close(fd);
  231. return 1;
  232. } else {
  233. wpa_printf(MSG_ERROR, "VLAN: %s: BRCTL_ADD_BRIDGE "
  234. "failed for %s: %s",
  235. __func__, br_name, strerror(errno));
  236. close(fd);
  237. return -1;
  238. }
  239. }
  240. /* Decrease forwarding delay to avoid EAPOL timeouts. */
  241. os_memset(&ifr, 0, sizeof(ifr));
  242. os_strlcpy(ifr.ifr_name, br_name, IFNAMSIZ);
  243. arg[0] = BRCTL_SET_BRIDGE_FORWARD_DELAY;
  244. arg[1] = 1;
  245. arg[2] = 0;
  246. arg[3] = 0;
  247. ifr.ifr_data = (char *) &arg;
  248. if (ioctl(fd, SIOCDEVPRIVATE, &ifr) < 0) {
  249. wpa_printf(MSG_ERROR, "VLAN: %s: "
  250. "BRCTL_SET_BRIDGE_FORWARD_DELAY (1 sec) failed for "
  251. "%s: %s", __func__, br_name, strerror(errno));
  252. /* Continue anyway */
  253. }
  254. close(fd);
  255. return 0;
  256. }
  257. static int br_getnumports(const char *br_name)
  258. {
  259. int fd;
  260. int i;
  261. int port_cnt = 0;
  262. unsigned long arg[4];
  263. int ifindices[MAX_BR_PORTS];
  264. struct ifreq ifr;
  265. if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  266. wpa_printf(MSG_ERROR, "VLAN: %s: socket(AF_INET,SOCK_STREAM) "
  267. "failed: %s", __func__, strerror(errno));
  268. return -1;
  269. }
  270. arg[0] = BRCTL_GET_PORT_LIST;
  271. arg[1] = (unsigned long) ifindices;
  272. arg[2] = MAX_BR_PORTS;
  273. arg[3] = 0;
  274. os_memset(ifindices, 0, sizeof(ifindices));
  275. os_strlcpy(ifr.ifr_name, br_name, sizeof(ifr.ifr_name));
  276. ifr.ifr_data = (void *) arg;
  277. if (ioctl(fd, SIOCDEVPRIVATE, &ifr) < 0) {
  278. wpa_printf(MSG_ERROR, "VLAN: %s: BRCTL_GET_PORT_LIST "
  279. "failed for %s: %s",
  280. __func__, br_name, strerror(errno));
  281. close(fd);
  282. return -1;
  283. }
  284. for (i = 1; i < MAX_BR_PORTS; i++) {
  285. if (ifindices[i] > 0) {
  286. port_cnt++;
  287. }
  288. }
  289. close(fd);
  290. return port_cnt;
  291. }
  292. static void vlan_newlink_tagged(int vlan_naming, const char *tagged_interface,
  293. const char *br_name, int vid,
  294. struct hostapd_data *hapd)
  295. {
  296. char vlan_ifname[IFNAMSIZ];
  297. int clean;
  298. if (vlan_naming == DYNAMIC_VLAN_NAMING_WITH_DEVICE)
  299. os_snprintf(vlan_ifname, sizeof(vlan_ifname), "%s.%d",
  300. tagged_interface, vid);
  301. else
  302. os_snprintf(vlan_ifname, sizeof(vlan_ifname), "vlan%d", vid);
  303. clean = 0;
  304. ifconfig_up(tagged_interface);
  305. if (!vlan_add(tagged_interface, vid, vlan_ifname))
  306. clean |= DVLAN_CLEAN_VLAN;
  307. if (!br_addif(br_name, vlan_ifname))
  308. clean |= DVLAN_CLEAN_VLAN_PORT;
  309. dyn_iface_get(hapd, vlan_ifname, clean);
  310. ifconfig_up(vlan_ifname);
  311. }
  312. static void vlan_bridge_name(char *br_name, struct hostapd_data *hapd, int vid)
  313. {
  314. char *tagged_interface = hapd->conf->ssid.vlan_tagged_interface;
  315. if (hapd->conf->vlan_bridge[0]) {
  316. os_snprintf(br_name, IFNAMSIZ, "%s%d",
  317. hapd->conf->vlan_bridge, vid);
  318. } else if (tagged_interface) {
  319. os_snprintf(br_name, IFNAMSIZ, "br%s.%d",
  320. tagged_interface, vid);
  321. } else {
  322. os_snprintf(br_name, IFNAMSIZ, "brvlan%d", vid);
  323. }
  324. }
  325. static void vlan_get_bridge(const char *br_name, struct hostapd_data *hapd,
  326. int vid)
  327. {
  328. char *tagged_interface = hapd->conf->ssid.vlan_tagged_interface;
  329. int vlan_naming = hapd->conf->ssid.vlan_naming;
  330. dyn_iface_get(hapd, br_name, br_addbr(br_name) ? 0 : DVLAN_CLEAN_BR);
  331. ifconfig_up(br_name);
  332. if (tagged_interface)
  333. vlan_newlink_tagged(vlan_naming, tagged_interface, br_name,
  334. vid, hapd);
  335. }
  336. void vlan_newlink(const char *ifname, struct hostapd_data *hapd)
  337. {
  338. char br_name[IFNAMSIZ];
  339. struct hostapd_vlan *vlan;
  340. int untagged, *tagged, i, notempty;
  341. wpa_printf(MSG_DEBUG, "VLAN: vlan_newlink(%s)", ifname);
  342. for (vlan = hapd->conf->vlan; vlan; vlan = vlan->next) {
  343. if (vlan->configured ||
  344. os_strcmp(ifname, vlan->ifname) != 0)
  345. continue;
  346. break;
  347. }
  348. if (!vlan)
  349. return;
  350. vlan->configured = 1;
  351. notempty = vlan->vlan_desc.notempty;
  352. untagged = vlan->vlan_desc.untagged;
  353. tagged = vlan->vlan_desc.tagged;
  354. if (!notempty) {
  355. /* Non-VLAN STA */
  356. if (hapd->conf->bridge[0] &&
  357. !br_addif(hapd->conf->bridge, ifname))
  358. vlan->clean |= DVLAN_CLEAN_WLAN_PORT;
  359. } else if (untagged > 0 && untagged <= MAX_VLAN_ID) {
  360. vlan_bridge_name(br_name, hapd, untagged);
  361. vlan_get_bridge(br_name, hapd, untagged);
  362. if (!br_addif(br_name, ifname))
  363. vlan->clean |= DVLAN_CLEAN_WLAN_PORT;
  364. }
  365. for (i = 0; i < MAX_NUM_TAGGED_VLAN && tagged[i]; i++) {
  366. if (tagged[i] == untagged ||
  367. tagged[i] <= 0 || tagged[i] > MAX_VLAN_ID ||
  368. (i > 0 && tagged[i] == tagged[i - 1]))
  369. continue;
  370. vlan_bridge_name(br_name, hapd, tagged[i]);
  371. vlan_get_bridge(br_name, hapd, tagged[i]);
  372. vlan_newlink_tagged(DYNAMIC_VLAN_NAMING_WITH_DEVICE,
  373. ifname, br_name, tagged[i], hapd);
  374. }
  375. ifconfig_up(ifname);
  376. }
  377. static void vlan_dellink_tagged(int vlan_naming, const char *tagged_interface,
  378. const char *br_name, int vid,
  379. struct hostapd_data *hapd)
  380. {
  381. char vlan_ifname[IFNAMSIZ];
  382. int clean;
  383. if (vlan_naming == DYNAMIC_VLAN_NAMING_WITH_DEVICE)
  384. os_snprintf(vlan_ifname, sizeof(vlan_ifname), "%s.%d",
  385. tagged_interface, vid);
  386. else
  387. os_snprintf(vlan_ifname, sizeof(vlan_ifname), "vlan%d", vid);
  388. clean = dyn_iface_put(hapd, vlan_ifname);
  389. if (clean & DVLAN_CLEAN_VLAN_PORT)
  390. br_delif(br_name, vlan_ifname);
  391. if (clean & DVLAN_CLEAN_VLAN) {
  392. ifconfig_down(vlan_ifname);
  393. vlan_rem(vlan_ifname);
  394. }
  395. }
  396. static void vlan_put_bridge(const char *br_name, struct hostapd_data *hapd,
  397. int vid)
  398. {
  399. int clean;
  400. char *tagged_interface = hapd->conf->ssid.vlan_tagged_interface;
  401. int vlan_naming = hapd->conf->ssid.vlan_naming;
  402. if (tagged_interface)
  403. vlan_dellink_tagged(vlan_naming, tagged_interface, br_name,
  404. vid, hapd);
  405. clean = dyn_iface_put(hapd, br_name);
  406. if ((clean & DVLAN_CLEAN_BR) && br_getnumports(br_name) == 0) {
  407. ifconfig_down(br_name);
  408. br_delbr(br_name);
  409. }
  410. }
  411. void vlan_dellink(const char *ifname, struct hostapd_data *hapd)
  412. {
  413. struct hostapd_vlan *first, *prev, *vlan = hapd->conf->vlan;
  414. wpa_printf(MSG_DEBUG, "VLAN: vlan_dellink(%s)", ifname);
  415. first = prev = vlan;
  416. while (vlan) {
  417. if (os_strcmp(ifname, vlan->ifname) != 0) {
  418. prev = vlan;
  419. vlan = vlan->next;
  420. continue;
  421. }
  422. break;
  423. }
  424. if (!vlan)
  425. return;
  426. if (vlan->configured) {
  427. int notempty = vlan->vlan_desc.notempty;
  428. int untagged = vlan->vlan_desc.untagged;
  429. int *tagged = vlan->vlan_desc.tagged;
  430. char br_name[IFNAMSIZ];
  431. int i;
  432. for (i = 0; i < MAX_NUM_TAGGED_VLAN && tagged[i]; i++) {
  433. if (tagged[i] == untagged ||
  434. tagged[i] <= 0 || tagged[i] > MAX_VLAN_ID ||
  435. (i > 0 && tagged[i] == tagged[i - 1]))
  436. continue;
  437. vlan_bridge_name(br_name, hapd, tagged[i]);
  438. vlan_dellink_tagged(DYNAMIC_VLAN_NAMING_WITH_DEVICE,
  439. ifname, br_name, tagged[i], hapd);
  440. vlan_put_bridge(br_name, hapd, tagged[i]);
  441. }
  442. if (!notempty) {
  443. /* Non-VLAN STA */
  444. if (hapd->conf->bridge[0] &&
  445. (vlan->clean & DVLAN_CLEAN_WLAN_PORT))
  446. br_delif(hapd->conf->bridge, ifname);
  447. } else if (untagged > 0 && untagged <= MAX_VLAN_ID) {
  448. vlan_bridge_name(br_name, hapd, untagged);
  449. if (vlan->clean & DVLAN_CLEAN_WLAN_PORT)
  450. br_delif(br_name, vlan->ifname);
  451. vlan_put_bridge(br_name, hapd, untagged);
  452. }
  453. }
  454. /*
  455. * Ensure this VLAN interface is actually removed even if
  456. * NEWLINK message is only received later.
  457. */
  458. if (if_nametoindex(vlan->ifname) && vlan_if_remove(hapd, vlan))
  459. wpa_printf(MSG_ERROR,
  460. "VLAN: Could not remove VLAN iface: %s: %s",
  461. vlan->ifname, strerror(errno));
  462. if (vlan == first)
  463. hapd->conf->vlan = vlan->next;
  464. else
  465. prev->next = vlan->next;
  466. os_free(vlan);
  467. }
  468. static void
  469. vlan_read_ifnames(struct nlmsghdr *h, size_t len, int del,
  470. struct hostapd_data *hapd)
  471. {
  472. struct ifinfomsg *ifi;
  473. int attrlen, nlmsg_len, rta_len;
  474. struct rtattr *attr;
  475. char ifname[IFNAMSIZ + 1];
  476. if (len < sizeof(*ifi))
  477. return;
  478. ifi = NLMSG_DATA(h);
  479. nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg));
  480. attrlen = h->nlmsg_len - nlmsg_len;
  481. if (attrlen < 0)
  482. return;
  483. attr = (struct rtattr *) (((char *) ifi) + nlmsg_len);
  484. os_memset(ifname, 0, sizeof(ifname));
  485. rta_len = RTA_ALIGN(sizeof(struct rtattr));
  486. while (RTA_OK(attr, attrlen)) {
  487. if (attr->rta_type == IFLA_IFNAME) {
  488. int n = attr->rta_len - rta_len;
  489. if (n < 0)
  490. break;
  491. if ((size_t) n >= sizeof(ifname))
  492. n = sizeof(ifname) - 1;
  493. os_memcpy(ifname, ((char *) attr) + rta_len, n);
  494. }
  495. attr = RTA_NEXT(attr, attrlen);
  496. }
  497. if (!ifname[0])
  498. return;
  499. if (del && if_nametoindex(ifname)) {
  500. /* interface still exists, race condition ->
  501. * iface has just been recreated */
  502. return;
  503. }
  504. wpa_printf(MSG_DEBUG,
  505. "VLAN: RTM_%sLINK: ifi_index=%d ifname=%s ifi_family=%d ifi_flags=0x%x (%s%s%s%s)",
  506. del ? "DEL" : "NEW",
  507. ifi->ifi_index, ifname, ifi->ifi_family, ifi->ifi_flags,
  508. (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
  509. (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
  510. (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
  511. (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
  512. if (del)
  513. vlan_dellink(ifname, hapd);
  514. else
  515. vlan_newlink(ifname, hapd);
  516. }
  517. static void vlan_event_receive(int sock, void *eloop_ctx, void *sock_ctx)
  518. {
  519. char buf[8192];
  520. int left;
  521. struct sockaddr_nl from;
  522. socklen_t fromlen;
  523. struct nlmsghdr *h;
  524. struct hostapd_data *hapd = eloop_ctx;
  525. fromlen = sizeof(from);
  526. left = recvfrom(sock, buf, sizeof(buf), MSG_DONTWAIT,
  527. (struct sockaddr *) &from, &fromlen);
  528. if (left < 0) {
  529. if (errno != EINTR && errno != EAGAIN)
  530. wpa_printf(MSG_ERROR, "VLAN: %s: recvfrom failed: %s",
  531. __func__, strerror(errno));
  532. return;
  533. }
  534. h = (struct nlmsghdr *) buf;
  535. while (NLMSG_OK(h, left)) {
  536. int len, plen;
  537. len = h->nlmsg_len;
  538. plen = len - sizeof(*h);
  539. if (len > left || plen < 0) {
  540. wpa_printf(MSG_DEBUG, "VLAN: Malformed netlink "
  541. "message: len=%d left=%d plen=%d",
  542. len, left, plen);
  543. break;
  544. }
  545. switch (h->nlmsg_type) {
  546. case RTM_NEWLINK:
  547. vlan_read_ifnames(h, plen, 0, hapd);
  548. break;
  549. case RTM_DELLINK:
  550. vlan_read_ifnames(h, plen, 1, hapd);
  551. break;
  552. }
  553. h = NLMSG_NEXT(h, left);
  554. }
  555. if (left > 0) {
  556. wpa_printf(MSG_DEBUG, "VLAN: %s: %d extra bytes in the end of "
  557. "netlink message", __func__, left);
  558. }
  559. }
  560. struct full_dynamic_vlan *
  561. full_dynamic_vlan_init(struct hostapd_data *hapd)
  562. {
  563. struct sockaddr_nl local;
  564. struct full_dynamic_vlan *priv;
  565. priv = os_zalloc(sizeof(*priv));
  566. if (priv == NULL)
  567. return NULL;
  568. vlan_set_name_type(hapd->conf->ssid.vlan_naming ==
  569. DYNAMIC_VLAN_NAMING_WITH_DEVICE ?
  570. VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD :
  571. VLAN_NAME_TYPE_PLUS_VID_NO_PAD);
  572. priv->s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  573. if (priv->s < 0) {
  574. wpa_printf(MSG_ERROR, "VLAN: %s: socket(PF_NETLINK,SOCK_RAW,"
  575. "NETLINK_ROUTE) failed: %s",
  576. __func__, strerror(errno));
  577. os_free(priv);
  578. return NULL;
  579. }
  580. os_memset(&local, 0, sizeof(local));
  581. local.nl_family = AF_NETLINK;
  582. local.nl_groups = RTMGRP_LINK;
  583. if (bind(priv->s, (struct sockaddr *) &local, sizeof(local)) < 0) {
  584. wpa_printf(MSG_ERROR, "VLAN: %s: bind(netlink) failed: %s",
  585. __func__, strerror(errno));
  586. close(priv->s);
  587. os_free(priv);
  588. return NULL;
  589. }
  590. if (eloop_register_read_sock(priv->s, vlan_event_receive, hapd, NULL))
  591. {
  592. close(priv->s);
  593. os_free(priv);
  594. return NULL;
  595. }
  596. return priv;
  597. }
  598. void full_dynamic_vlan_deinit(struct full_dynamic_vlan *priv)
  599. {
  600. if (priv == NULL)
  601. return;
  602. eloop_unregister_read_sock(priv->s);
  603. close(priv->s);
  604. os_free(priv);
  605. }