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>
7 * This software may be distributed under the terms of the BSD license.
8 * See README for more details.
11 #include "utils/includes.h"
13 #include "utils/common.h"
15 #include "ap_config.h"
16 #include "ap_drv_ops.h"
17 #include "vlan_init.h"
18 #include "vlan_util.h"
21 #ifdef CONFIG_FULL_DYNAMIC_VLAN
24 #include <sys/ioctl.h>
25 #include <linux/sockios.h>
26 #include <linux/if_vlan.h>
27 #include <linux/if_bridge.h>
29 #include "drivers/priv_netlink.h"
30 #include "utils/eloop.h"
33 struct full_dynamic_vlan {
34 int s; /* socket on which to listen for new/removed interfaces. */
38 static int ifconfig_helper(const char *if_name, int up)
43 if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
44 wpa_printf(MSG_ERROR, "VLAN: %s: socket(AF_INET,SOCK_STREAM) "
45 "failed: %s", __func__, strerror(errno));
49 os_memset(&ifr, 0, sizeof(ifr));
50 os_strlcpy(ifr.ifr_name, if_name, IFNAMSIZ);
52 if (ioctl(fd, SIOCGIFFLAGS, &ifr) != 0) {
53 wpa_printf(MSG_ERROR, "VLAN: %s: ioctl(SIOCGIFFLAGS) failed "
54 "for interface %s: %s",
55 __func__, if_name, strerror(errno));
61 ifr.ifr_flags |= IFF_UP;
63 ifr.ifr_flags &= ~IFF_UP;
65 if (ioctl(fd, SIOCSIFFLAGS, &ifr) != 0) {
66 wpa_printf(MSG_ERROR, "VLAN: %s: ioctl(SIOCSIFFLAGS) failed "
67 "for interface %s (up=%d): %s",
68 __func__, if_name, up, strerror(errno));
78 static int ifconfig_up(const char *if_name)
80 wpa_printf(MSG_DEBUG, "VLAN: Set interface %s up", if_name);
81 return ifconfig_helper(if_name, 1);
85 static int ifconfig_down(const char *if_name)
87 wpa_printf(MSG_DEBUG, "VLAN: Set interface %s down", if_name);
88 return ifconfig_helper(if_name, 0);
93 * These are only available in recent linux headers (without the leading
96 #define _GET_VLAN_REALDEV_NAME_CMD 8
97 #define _GET_VLAN_VID_CMD 9
99 /* This value should be 256 ONLY. If it is something else, then hostapd
100 * might crash!, as this value has been hard-coded in 2.4.x kernel
103 #define MAX_BR_PORTS 256
105 static int br_delif(const char *br_name, const char *if_name)
109 unsigned long args[2];
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));
119 if_index = if_nametoindex(if_name);
122 wpa_printf(MSG_ERROR, "VLAN: %s: Failure determining "
123 "interface index for '%s'",
129 args[0] = BRCTL_DEL_IF;
132 os_strlcpy(ifr.ifr_name, br_name, sizeof(ifr.ifr_name));
133 ifr.ifr_data = (__caddr_t) args;
135 if (ioctl(fd, SIOCDEVPRIVATE, &ifr) < 0 && errno != EINVAL) {
136 /* No error if interface already removed. */
137 wpa_printf(MSG_ERROR, "VLAN: %s: ioctl[SIOCDEVPRIVATE,"
138 "BRCTL_DEL_IF] failed for br_name=%s if_name=%s: "
139 "%s", __func__, br_name, if_name, strerror(errno));
150 Add interface 'if_name' to the bridge 'br_name'
153 returns 1 if the interface is already part of the bridge
156 static int br_addif(const char *br_name, const char *if_name)
160 unsigned long args[2];
163 wpa_printf(MSG_DEBUG, "VLAN: br_addif(%s, %s)", br_name, if_name);
164 if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
165 wpa_printf(MSG_ERROR, "VLAN: %s: socket(AF_INET,SOCK_STREAM) "
166 "failed: %s", __func__, strerror(errno));
170 if_index = if_nametoindex(if_name);
173 wpa_printf(MSG_ERROR, "VLAN: %s: Failure determining "
174 "interface index for '%s'",
180 args[0] = BRCTL_ADD_IF;
183 os_strlcpy(ifr.ifr_name, br_name, sizeof(ifr.ifr_name));
184 ifr.ifr_data = (__caddr_t) args;
186 if (ioctl(fd, SIOCDEVPRIVATE, &ifr) < 0) {
187 if (errno == EBUSY) {
188 /* The interface is already added. */
193 wpa_printf(MSG_ERROR, "VLAN: %s: ioctl[SIOCDEVPRIVATE,"
194 "BRCTL_ADD_IF] failed for br_name=%s if_name=%s: "
195 "%s", __func__, br_name, if_name, strerror(errno));
205 static int br_delbr(const char *br_name)
208 unsigned long arg[2];
210 wpa_printf(MSG_DEBUG, "VLAN: br_delbr(%s)", br_name);
211 if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
212 wpa_printf(MSG_ERROR, "VLAN: %s: socket(AF_INET,SOCK_STREAM) "
213 "failed: %s", __func__, strerror(errno));
217 arg[0] = BRCTL_DEL_BRIDGE;
218 arg[1] = (unsigned long) br_name;
220 if (ioctl(fd, SIOCGIFBR, arg) < 0 && errno != ENXIO) {
221 /* No error if bridge already removed. */
222 wpa_printf(MSG_ERROR, "VLAN: %s: BRCTL_DEL_BRIDGE failed for "
223 "%s: %s", __func__, br_name, strerror(errno));
234 Add a bridge with the name 'br_name'.
237 returns 1 if the bridge already exists
240 static int br_addbr(const char *br_name)
243 unsigned long arg[4];
246 wpa_printf(MSG_DEBUG, "VLAN: br_addbr(%s)", br_name);
247 if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
248 wpa_printf(MSG_ERROR, "VLAN: %s: socket(AF_INET,SOCK_STREAM) "
249 "failed: %s", __func__, strerror(errno));
253 arg[0] = BRCTL_ADD_BRIDGE;
254 arg[1] = (unsigned long) br_name;
256 if (ioctl(fd, SIOCGIFBR, arg) < 0) {
257 if (errno == EEXIST) {
258 /* The bridge is already added. */
262 wpa_printf(MSG_ERROR, "VLAN: %s: BRCTL_ADD_BRIDGE "
264 __func__, br_name, strerror(errno));
270 /* Decrease forwarding delay to avoid EAPOL timeouts. */
271 os_memset(&ifr, 0, sizeof(ifr));
272 os_strlcpy(ifr.ifr_name, br_name, IFNAMSIZ);
273 arg[0] = BRCTL_SET_BRIDGE_FORWARD_DELAY;
277 ifr.ifr_data = (char *) &arg;
278 if (ioctl(fd, SIOCDEVPRIVATE, &ifr) < 0) {
279 wpa_printf(MSG_ERROR, "VLAN: %s: "
280 "BRCTL_SET_BRIDGE_FORWARD_DELAY (1 sec) failed for "
281 "%s: %s", __func__, br_name, strerror(errno));
282 /* Continue anyway */
290 static int br_getnumports(const char *br_name)
295 unsigned long arg[4];
296 int ifindices[MAX_BR_PORTS];
299 if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
300 wpa_printf(MSG_ERROR, "VLAN: %s: socket(AF_INET,SOCK_STREAM) "
301 "failed: %s", __func__, strerror(errno));
305 arg[0] = BRCTL_GET_PORT_LIST;
306 arg[1] = (unsigned long) ifindices;
307 arg[2] = MAX_BR_PORTS;
310 os_memset(ifindices, 0, sizeof(ifindices));
311 os_strlcpy(ifr.ifr_name, br_name, sizeof(ifr.ifr_name));
312 ifr.ifr_data = (__caddr_t) arg;
314 if (ioctl(fd, SIOCDEVPRIVATE, &ifr) < 0) {
315 wpa_printf(MSG_ERROR, "VLAN: %s: BRCTL_GET_PORT_LIST "
317 __func__, br_name, strerror(errno));
322 for (i = 1; i < MAX_BR_PORTS; i++) {
323 if (ifindices[i] > 0) {
333 #ifndef CONFIG_VLAN_NETLINK
335 int vlan_rem(const char *if_name)
338 struct vlan_ioctl_args if_request;
340 wpa_printf(MSG_DEBUG, "VLAN: vlan_rem(%s)", if_name);
341 if ((os_strlen(if_name) + 1) > sizeof(if_request.device1)) {
342 wpa_printf(MSG_ERROR, "VLAN: Interface name too long: '%s'",
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));
353 os_memset(&if_request, 0, sizeof(if_request));
355 os_strlcpy(if_request.device1, if_name, sizeof(if_request.device1));
356 if_request.cmd = DEL_VLAN_CMD;
358 if (ioctl(fd, SIOCSIFVLAN, &if_request) < 0) {
359 wpa_printf(MSG_ERROR, "VLAN: %s: DEL_VLAN_CMD failed for %s: "
360 "%s", __func__, if_name, strerror(errno));
371 Add a vlan interface with VLAN ID 'vid' and tagged interface
375 returns 1 if the interface already exists
378 int vlan_add(const char *if_name, int vid, const char *vlan_if_name)
381 struct vlan_ioctl_args if_request;
383 wpa_printf(MSG_DEBUG, "VLAN: vlan_add(if_name=%s, vid=%d)",
385 ifconfig_up(if_name);
387 if ((os_strlen(if_name) + 1) > sizeof(if_request.device1)) {
388 wpa_printf(MSG_ERROR, "VLAN: Interface name too long: '%s'",
393 if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
394 wpa_printf(MSG_ERROR, "VLAN: %s: socket(AF_INET,SOCK_STREAM) "
395 "failed: %s", __func__, strerror(errno));
399 os_memset(&if_request, 0, sizeof(if_request));
401 /* Determine if a suitable vlan device already exists. */
403 os_snprintf(if_request.device1, sizeof(if_request.device1), "vlan%d",
406 if_request.cmd = _GET_VLAN_VID_CMD;
408 if (ioctl(fd, SIOCSIFVLAN, &if_request) == 0) {
410 if (if_request.u.VID == vid) {
411 if_request.cmd = _GET_VLAN_REALDEV_NAME_CMD;
413 if (ioctl(fd, SIOCSIFVLAN, &if_request) == 0 &&
414 os_strncmp(if_request.u.device2, if_name,
415 sizeof(if_request.u.device2)) == 0) {
417 wpa_printf(MSG_DEBUG, "VLAN: vlan_add: "
418 "if_name %s exists already",
425 /* A suitable vlan device does not already exist, add one. */
427 os_memset(&if_request, 0, sizeof(if_request));
428 os_strlcpy(if_request.device1, if_name, sizeof(if_request.device1));
429 if_request.u.VID = vid;
430 if_request.cmd = ADD_VLAN_CMD;
432 if (ioctl(fd, SIOCSIFVLAN, &if_request) < 0) {
433 wpa_printf(MSG_ERROR, "VLAN: %s: ADD_VLAN_CMD failed for %s: "
435 __func__, if_request.device1, strerror(errno));
445 static int vlan_set_name_type(unsigned int name_type)
448 struct vlan_ioctl_args if_request;
450 wpa_printf(MSG_DEBUG, "VLAN: vlan_set_name_type(name_type=%u)",
452 if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
453 wpa_printf(MSG_ERROR, "VLAN: %s: socket(AF_INET,SOCK_STREAM) "
454 "failed: %s", __func__, strerror(errno));
458 os_memset(&if_request, 0, sizeof(if_request));
460 if_request.u.name_type = name_type;
461 if_request.cmd = SET_VLAN_NAME_TYPE_CMD;
462 if (ioctl(fd, SIOCSIFVLAN, &if_request) < 0) {
463 wpa_printf(MSG_ERROR, "VLAN: %s: SET_VLAN_NAME_TYPE_CMD "
464 "name_type=%u failed: %s",
465 __func__, name_type, strerror(errno));
474 #endif /* CONFIG_VLAN_NETLINK */
478 * Increase the usage counter for given parent/ifname combination.
479 * If create is set, then this iface is added to the global list.
482 * 0 if iface is not in list
483 * 1 if iface is in list (was there or has been added)
485 static int hapd_get_dynamic_iface(const char *parent, const char *ifname,
486 int create, struct hostapd_data *hapd)
489 struct hostapd_dynamic_iface *j = NULL, **tmp;
490 struct hapd_interfaces *hapd_global = hapd->iface->interfaces;
495 for (i = 0; i < hapd_global->count_dynamic; i++) {
496 j = hapd_global->dynamic_iface[i];
497 if (os_strncmp(j->iface, ifname, sizeof(j->iface)) == 0 &&
498 os_strncmp(j->parent, parent, sizeof(j->parent)) == 0)
501 if (i < hapd_global->count_dynamic) {
506 /* new entry required */
510 j = os_zalloc(sizeof(*j));
513 os_strlcpy(j->iface, ifname, sizeof(j->iface));
514 os_strlcpy(j->parent, parent, sizeof(j->parent));
516 tmp = os_realloc_array(hapd_global->dynamic_iface, i + 1,
517 sizeof(*hapd_global->dynamic_iface));
519 wpa_printf(MSG_ERROR, "VLAN: Failed to allocate memory in %s",
523 hapd_global->count_dynamic++;
524 hapd_global->dynamic_iface = tmp;
525 hapd_global->dynamic_iface[i] = j;
532 * Decrease the usage counter for given ifname.
534 * -1 on error or if iface was not found
535 * 0 if iface was found and is still present
536 * 1 if iface was removed from global list
538 static int hapd_put_dynamic_iface(const char *parent, const char *ifname,
539 struct hostapd_data *hapd)
542 struct hostapd_dynamic_iface *j = NULL, **tmp;
543 struct hapd_interfaces *hapd_glob = hapd->iface->interfaces;
548 for (i = 0; i < hapd_glob->count_dynamic; i++) {
549 j = hapd_glob->dynamic_iface[i];
550 if (os_strncmp(j->iface, ifname, sizeof(j->iface)) == 0 &&
551 os_strncmp(j->parent, parent, sizeof(j->parent)) == 0)
555 if (i == hapd_glob->count_dynamic) {
557 * Interface not in global list. This can happen if alloc in
569 for (; i < hapd_glob->count_dynamic - 1; i++)
570 hapd_glob->dynamic_iface[i] = hapd_glob->dynamic_iface[i + 1];
571 hapd_glob->dynamic_iface[hapd_glob->count_dynamic - 1] = NULL;
572 hapd_glob->count_dynamic--;
574 if (hapd_glob->count_dynamic == 0) {
575 os_free(hapd_glob->dynamic_iface);
576 hapd_glob->dynamic_iface = NULL;
580 tmp = os_realloc_array(hapd_glob->dynamic_iface,
581 hapd_glob->count_dynamic,
582 sizeof(*hapd_glob->dynamic_iface));
584 wpa_printf(MSG_ERROR, "VLAN: Failed to release memory in %s",
588 hapd_glob->dynamic_iface = tmp;
594 static void vlan_newlink(char *ifname, struct hostapd_data *hapd)
596 char vlan_ifname[IFNAMSIZ];
597 char br_name[IFNAMSIZ];
598 struct hostapd_vlan *vlan = hapd->conf->vlan;
599 char *tagged_interface = hapd->conf->ssid.vlan_tagged_interface;
600 int vlan_naming = hapd->conf->ssid.vlan_naming;
603 wpa_printf(MSG_DEBUG, "VLAN: vlan_newlink(%s)", ifname);
606 if (os_strcmp(ifname, vlan->ifname) == 0) {
608 if (hapd->conf->vlan_bridge[0]) {
609 os_snprintf(br_name, sizeof(br_name), "%s%d",
610 hapd->conf->vlan_bridge,
612 } else if (tagged_interface) {
613 os_snprintf(br_name, sizeof(br_name),
614 "br%s.%d", tagged_interface,
617 os_snprintf(br_name, sizeof(br_name),
618 "brvlan%d", vlan->vlan_id);
621 ret = br_addbr(br_name);
622 if (hapd_get_dynamic_iface(NULL, br_name, ret == 0,
624 vlan->clean |= DVLAN_CLEAN_BR;
626 ifconfig_up(br_name);
628 if (tagged_interface) {
630 DYNAMIC_VLAN_NAMING_WITH_DEVICE)
631 os_snprintf(vlan_ifname,
633 "%s.%d", tagged_interface,
636 os_snprintf(vlan_ifname,
638 "vlan%d", vlan->vlan_id);
640 ifconfig_up(tagged_interface);
641 ret = vlan_add(tagged_interface, vlan->vlan_id,
643 if (hapd_get_dynamic_iface(NULL, vlan_ifname,
645 vlan->clean |= DVLAN_CLEAN_VLAN;
647 ret = br_addif(br_name, vlan_ifname);
648 if (hapd_get_dynamic_iface(br_name,
651 vlan->clean |= DVLAN_CLEAN_VLAN_PORT;
653 ifconfig_up(vlan_ifname);
656 ret = br_addif(br_name, ifname);
657 if (hapd_get_dynamic_iface(br_name, ifname, ret == 0,
659 vlan->clean |= DVLAN_CLEAN_WLAN_PORT;
670 static void vlan_dellink(char *ifname, struct hostapd_data *hapd)
672 char vlan_ifname[IFNAMSIZ];
673 char br_name[IFNAMSIZ];
674 struct hostapd_vlan *first, *prev, *vlan = hapd->conf->vlan;
675 char *tagged_interface = hapd->conf->ssid.vlan_tagged_interface;
676 int vlan_naming = hapd->conf->ssid.vlan_naming;
678 wpa_printf(MSG_DEBUG, "VLAN: vlan_dellink(%s)", ifname);
683 if (os_strcmp(ifname, vlan->ifname) == 0) {
684 if (hapd->conf->vlan_bridge[0]) {
685 os_snprintf(br_name, sizeof(br_name), "%s%d",
686 hapd->conf->vlan_bridge,
688 } else if (tagged_interface) {
689 os_snprintf(br_name, sizeof(br_name),
690 "br%s.%d", tagged_interface,
693 os_snprintf(br_name, sizeof(br_name),
694 "brvlan%d", vlan->vlan_id);
697 if ((vlan->clean & DVLAN_CLEAN_WLAN_PORT) &&
698 hapd_put_dynamic_iface(br_name, vlan->ifname, hapd))
699 br_delif(br_name, vlan->ifname);
701 if (tagged_interface) {
703 DYNAMIC_VLAN_NAMING_WITH_DEVICE)
704 os_snprintf(vlan_ifname,
706 "%s.%d", tagged_interface,
709 os_snprintf(vlan_ifname,
711 "vlan%d", vlan->vlan_id);
712 if ((vlan->clean & DVLAN_CLEAN_VLAN_PORT) &&
713 hapd_put_dynamic_iface(br_name, vlan_ifname,
715 br_delif(br_name, vlan_ifname);
716 ifconfig_down(vlan_ifname);
718 if ((vlan->clean & DVLAN_CLEAN_VLAN) &&
719 hapd_put_dynamic_iface(NULL, vlan_ifname,
721 vlan_rem(vlan_ifname);
724 if ((vlan->clean & DVLAN_CLEAN_BR) &&
725 hapd_put_dynamic_iface(NULL, br_name, hapd) &&
726 br_getnumports(br_name) == 0) {
727 ifconfig_down(br_name);
732 hapd->conf->vlan = vlan->next;
734 prev->next = vlan->next;
747 vlan_read_ifnames(struct nlmsghdr *h, size_t len, int del,
748 struct hostapd_data *hapd)
750 struct ifinfomsg *ifi;
751 int attrlen, nlmsg_len, rta_len;
754 if (len < sizeof(*ifi))
759 nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg));
761 attrlen = h->nlmsg_len - nlmsg_len;
765 attr = (struct rtattr *) (((char *) ifi) + nlmsg_len);
767 rta_len = RTA_ALIGN(sizeof(struct rtattr));
768 while (RTA_OK(attr, attrlen)) {
769 char ifname[IFNAMSIZ + 1];
771 if (attr->rta_type == IFLA_IFNAME) {
772 int n = attr->rta_len - rta_len;
776 os_memset(ifname, 0, sizeof(ifname));
778 if ((size_t) n > sizeof(ifname))
780 os_memcpy(ifname, ((char *) attr) + rta_len, n);
783 vlan_dellink(ifname, hapd);
785 vlan_newlink(ifname, hapd);
788 attr = RTA_NEXT(attr, attrlen);
793 static void vlan_event_receive(int sock, void *eloop_ctx, void *sock_ctx)
797 struct sockaddr_nl from;
800 struct hostapd_data *hapd = eloop_ctx;
802 fromlen = sizeof(from);
803 left = recvfrom(sock, buf, sizeof(buf), MSG_DONTWAIT,
804 (struct sockaddr *) &from, &fromlen);
806 if (errno != EINTR && errno != EAGAIN)
807 wpa_printf(MSG_ERROR, "VLAN: %s: recvfrom failed: %s",
808 __func__, strerror(errno));
812 h = (struct nlmsghdr *) buf;
813 while (left >= (int) sizeof(*h)) {
817 plen = len - sizeof(*h);
818 if (len > left || plen < 0) {
819 wpa_printf(MSG_DEBUG, "VLAN: Malformed netlink "
820 "message: len=%d left=%d plen=%d",
825 switch (h->nlmsg_type) {
827 vlan_read_ifnames(h, plen, 0, hapd);
830 vlan_read_ifnames(h, plen, 1, hapd);
834 len = NLMSG_ALIGN(len);
836 h = (struct nlmsghdr *) ((char *) h + len);
840 wpa_printf(MSG_DEBUG, "VLAN: %s: %d extra bytes in the end of "
841 "netlink message", __func__, left);
846 static struct full_dynamic_vlan *
847 full_dynamic_vlan_init(struct hostapd_data *hapd)
849 struct sockaddr_nl local;
850 struct full_dynamic_vlan *priv;
852 priv = os_zalloc(sizeof(*priv));
856 #ifndef CONFIG_VLAN_NETLINK
857 vlan_set_name_type(hapd->conf->ssid.vlan_naming ==
858 DYNAMIC_VLAN_NAMING_WITH_DEVICE ?
859 VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD :
860 VLAN_NAME_TYPE_PLUS_VID_NO_PAD);
861 #endif /* CONFIG_VLAN_NETLINK */
863 priv->s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
865 wpa_printf(MSG_ERROR, "VLAN: %s: socket(PF_NETLINK,SOCK_RAW,"
866 "NETLINK_ROUTE) failed: %s",
867 __func__, strerror(errno));
872 os_memset(&local, 0, sizeof(local));
873 local.nl_family = AF_NETLINK;
874 local.nl_groups = RTMGRP_LINK;
875 if (bind(priv->s, (struct sockaddr *) &local, sizeof(local)) < 0) {
876 wpa_printf(MSG_ERROR, "VLAN: %s: bind(netlink) failed: %s",
877 __func__, strerror(errno));
883 if (eloop_register_read_sock(priv->s, vlan_event_receive, hapd, NULL))
894 static void full_dynamic_vlan_deinit(struct full_dynamic_vlan *priv)
898 eloop_unregister_read_sock(priv->s);
902 #endif /* CONFIG_FULL_DYNAMIC_VLAN */
905 int vlan_setup_encryption_dyn(struct hostapd_data *hapd,
906 struct hostapd_ssid *mssid, const char *dyn_vlan)
910 if (dyn_vlan == NULL)
913 /* Static WEP keys are set here; IEEE 802.1X and WPA uses their own
914 * functions for setting up dynamic broadcast keys. */
915 for (i = 0; i < 4; i++) {
916 if (mssid->wep.key[i] &&
917 hostapd_drv_set_key(dyn_vlan, hapd, WPA_ALG_WEP, NULL, i,
918 i == mssid->wep.idx, NULL, 0,
919 mssid->wep.key[i], mssid->wep.len[i]))
921 wpa_printf(MSG_ERROR, "VLAN: Could not set WEP "
922 "encryption for dynamic VLAN");
931 static int vlan_dynamic_add(struct hostapd_data *hapd,
932 struct hostapd_vlan *vlan)
935 if (vlan->vlan_id != VLAN_ID_WILDCARD) {
936 if (hostapd_vlan_if_add(hapd, vlan->ifname)) {
937 if (errno != EEXIST) {
938 wpa_printf(MSG_ERROR, "VLAN: Could "
939 "not add VLAN %s: %s",
945 #ifdef CONFIG_FULL_DYNAMIC_VLAN
946 ifconfig_up(vlan->ifname);
947 #endif /* CONFIG_FULL_DYNAMIC_VLAN */
957 static void vlan_dynamic_remove(struct hostapd_data *hapd,
958 struct hostapd_vlan *vlan)
960 struct hostapd_vlan *next;
965 if (vlan->vlan_id != VLAN_ID_WILDCARD &&
966 hostapd_vlan_if_remove(hapd, vlan->ifname)) {
967 wpa_printf(MSG_ERROR, "VLAN: Could not remove VLAN "
969 vlan->ifname, strerror(errno));
971 #ifdef CONFIG_FULL_DYNAMIC_VLAN
973 vlan_dellink(vlan->ifname, hapd);
974 #endif /* CONFIG_FULL_DYNAMIC_VLAN */
981 int vlan_init(struct hostapd_data *hapd)
983 #ifdef CONFIG_FULL_DYNAMIC_VLAN
984 hapd->full_dynamic_vlan = full_dynamic_vlan_init(hapd);
985 #endif /* CONFIG_FULL_DYNAMIC_VLAN */
987 if (hapd->conf->ssid.dynamic_vlan != DYNAMIC_VLAN_DISABLED &&
989 /* dynamic vlans enabled but no (or empty) vlan_file given */
990 struct hostapd_vlan *vlan;
991 vlan = os_zalloc(sizeof(*vlan));
993 wpa_printf(MSG_ERROR, "Out of memory while assigning "
998 vlan->vlan_id = VLAN_ID_WILDCARD;
999 os_snprintf(vlan->ifname, sizeof(vlan->ifname), "%s.#",
1001 vlan->next = hapd->conf->vlan;
1002 hapd->conf->vlan = vlan;
1005 if (vlan_dynamic_add(hapd, hapd->conf->vlan))
1012 void vlan_deinit(struct hostapd_data *hapd)
1014 vlan_dynamic_remove(hapd, hapd->conf->vlan);
1016 #ifdef CONFIG_FULL_DYNAMIC_VLAN
1017 full_dynamic_vlan_deinit(hapd->full_dynamic_vlan);
1018 hapd->full_dynamic_vlan = NULL;
1019 #endif /* CONFIG_FULL_DYNAMIC_VLAN */
1023 struct hostapd_vlan * vlan_add_dynamic(struct hostapd_data *hapd,
1024 struct hostapd_vlan *vlan,
1027 struct hostapd_vlan *n;
1030 if (vlan == NULL || vlan_id <= 0 || vlan_id > MAX_VLAN_ID ||
1031 vlan->vlan_id != VLAN_ID_WILDCARD)
1034 wpa_printf(MSG_DEBUG, "VLAN: %s(vlan_id=%d ifname=%s)",
1035 __func__, vlan_id, vlan->ifname);
1036 ifname = os_strdup(vlan->ifname);
1039 pos = os_strchr(ifname, '#');
1046 n = os_zalloc(sizeof(*n));
1052 n->vlan_id = vlan_id;
1053 n->dynamic_vlan = 1;
1055 os_snprintf(n->ifname, sizeof(n->ifname), "%s%d%s", ifname, vlan_id,
1059 if (hostapd_vlan_if_add(hapd, n->ifname)) {
1064 n->next = hapd->conf->vlan;
1065 hapd->conf->vlan = n;
1067 #ifdef CONFIG_FULL_DYNAMIC_VLAN
1068 ifconfig_up(n->ifname);
1069 #endif /* CONFIG_FULL_DYNAMIC_VLAN */
1075 int vlan_remove_dynamic(struct hostapd_data *hapd, int vlan_id)
1077 struct hostapd_vlan *vlan;
1079 if (vlan_id <= 0 || vlan_id > MAX_VLAN_ID)
1082 wpa_printf(MSG_DEBUG, "VLAN: %s(vlan_id=%d)", __func__, vlan_id);
1084 vlan = hapd->conf->vlan;
1086 if (vlan->vlan_id == vlan_id && vlan->dynamic_vlan > 0) {
1087 vlan->dynamic_vlan--;
1096 if (vlan->dynamic_vlan == 0)
1097 hostapd_vlan_if_remove(hapd, vlan->ifname);