blob: baabbe33b76f61b5f4bbe467a58fff432cefe4d8 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
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 *
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007 * This software may be distributed under the terms of the BSD license.
8 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009 */
10
11#include "utils/includes.h"
12
13#include "utils/common.h"
14#include "hostapd.h"
15#include "ap_config.h"
16#include "ap_drv_ops.h"
17#include "vlan_init.h"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070018#include "vlan_util.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070019
20
21#ifdef CONFIG_FULL_DYNAMIC_VLAN
22
23#include <net/if.h>
24#include <sys/ioctl.h>
25#include <linux/sockios.h>
26#include <linux/if_vlan.h>
27#include <linux/if_bridge.h>
28
29#include "drivers/priv_netlink.h"
30#include "utils/eloop.h"
31
32
33struct full_dynamic_vlan {
34 int s; /* socket on which to listen for new/removed interfaces. */
35};
36
37
38static int ifconfig_helper(const char *if_name, int up)
39{
40 int fd;
41 struct ifreq ifr;
42
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));
46 return -1;
47 }
48
49 os_memset(&ifr, 0, sizeof(ifr));
50 os_strlcpy(ifr.ifr_name, if_name, IFNAMSIZ);
51
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));
56 close(fd);
57 return -1;
58 }
59
60 if (up)
61 ifr.ifr_flags |= IFF_UP;
62 else
63 ifr.ifr_flags &= ~IFF_UP;
64
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));
69 close(fd);
70 return -1;
71 }
72
73 close(fd);
74 return 0;
75}
76
77
78static int ifconfig_up(const char *if_name)
79{
80 wpa_printf(MSG_DEBUG, "VLAN: Set interface %s up", if_name);
81 return ifconfig_helper(if_name, 1);
82}
83
84
85static int ifconfig_down(const char *if_name)
86{
87 wpa_printf(MSG_DEBUG, "VLAN: Set interface %s down", if_name);
88 return ifconfig_helper(if_name, 0);
89}
90
91
92/*
93 * These are only available in recent linux headers (without the leading
94 * underscore).
95 */
96#define _GET_VLAN_REALDEV_NAME_CMD 8
97#define _GET_VLAN_VID_CMD 9
98
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
101 * bridging code.
102 */
103#define MAX_BR_PORTS 256
104
105static int br_delif(const char *br_name, const char *if_name)
106{
107 int fd;
108 struct ifreq ifr;
109 unsigned long args[2];
110 int if_index;
111
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
119 if_index = if_nametoindex(if_name);
120
121 if (if_index == 0) {
122 wpa_printf(MSG_ERROR, "VLAN: %s: Failure determining "
123 "interface index for '%s'",
124 __func__, if_name);
125 close(fd);
126 return -1;
127 }
128
129 args[0] = BRCTL_DEL_IF;
130 args[1] = if_index;
131
132 os_strlcpy(ifr.ifr_name, br_name, sizeof(ifr.ifr_name));
133 ifr.ifr_data = (__caddr_t) args;
134
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));
140 close(fd);
141 return -1;
142 }
143
144 close(fd);
145 return 0;
146}
147
148
149/*
150 Add interface 'if_name' to the bridge 'br_name'
151
152 returns -1 on error
153 returns 1 if the interface is already part of the bridge
154 returns 0 otherwise
155*/
156static int br_addif(const char *br_name, const char *if_name)
157{
158 int fd;
159 struct ifreq ifr;
160 unsigned long args[2];
161 int if_index;
162
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));
167 return -1;
168 }
169
170 if_index = if_nametoindex(if_name);
171
172 if (if_index == 0) {
173 wpa_printf(MSG_ERROR, "VLAN: %s: Failure determining "
174 "interface index for '%s'",
175 __func__, if_name);
176 close(fd);
177 return -1;
178 }
179
180 args[0] = BRCTL_ADD_IF;
181 args[1] = if_index;
182
183 os_strlcpy(ifr.ifr_name, br_name, sizeof(ifr.ifr_name));
184 ifr.ifr_data = (__caddr_t) args;
185
186 if (ioctl(fd, SIOCDEVPRIVATE, &ifr) < 0) {
187 if (errno == EBUSY) {
188 /* The interface is already added. */
189 close(fd);
190 return 1;
191 }
192
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));
196 close(fd);
197 return -1;
198 }
199
200 close(fd);
201 return 0;
202}
203
204
205static int br_delbr(const char *br_name)
206{
207 int fd;
208 unsigned long arg[2];
209
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));
214 return -1;
215 }
216
217 arg[0] = BRCTL_DEL_BRIDGE;
218 arg[1] = (unsigned long) br_name;
219
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));
224 close(fd);
225 return -1;
226 }
227
228 close(fd);
229 return 0;
230}
231
232
233/*
234 Add a bridge with the name 'br_name'.
235
236 returns -1 on error
237 returns 1 if the bridge already exists
238 returns 0 otherwise
239*/
240static int br_addbr(const char *br_name)
241{
242 int fd;
243 unsigned long arg[4];
244 struct ifreq ifr;
245
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));
250 return -1;
251 }
252
253 arg[0] = BRCTL_ADD_BRIDGE;
254 arg[1] = (unsigned long) br_name;
255
256 if (ioctl(fd, SIOCGIFBR, arg) < 0) {
257 if (errno == EEXIST) {
258 /* The bridge is already added. */
259 close(fd);
260 return 1;
261 } else {
262 wpa_printf(MSG_ERROR, "VLAN: %s: BRCTL_ADD_BRIDGE "
263 "failed for %s: %s",
264 __func__, br_name, strerror(errno));
265 close(fd);
266 return -1;
267 }
268 }
269
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;
274 arg[1] = 1;
275 arg[2] = 0;
276 arg[3] = 0;
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 */
283 }
284
285 close(fd);
286 return 0;
287}
288
289
290static int br_getnumports(const char *br_name)
291{
292 int fd;
293 int i;
294 int port_cnt = 0;
295 unsigned long arg[4];
296 int ifindices[MAX_BR_PORTS];
297 struct ifreq ifr;
298
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));
302 return -1;
303 }
304
305 arg[0] = BRCTL_GET_PORT_LIST;
306 arg[1] = (unsigned long) ifindices;
307 arg[2] = MAX_BR_PORTS;
308 arg[3] = 0;
309
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;
313
314 if (ioctl(fd, SIOCDEVPRIVATE, &ifr) < 0) {
315 wpa_printf(MSG_ERROR, "VLAN: %s: BRCTL_GET_PORT_LIST "
316 "failed for %s: %s",
317 __func__, br_name, strerror(errno));
318 close(fd);
319 return -1;
320 }
321
322 for (i = 1; i < MAX_BR_PORTS; i++) {
323 if (ifindices[i] > 0) {
324 port_cnt++;
325 }
326 }
327
328 close(fd);
329 return port_cnt;
330}
331
332
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700333#ifndef CONFIG_VLAN_NETLINK
334
335int vlan_rem(const char *if_name)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700336{
337 int fd;
338 struct vlan_ioctl_args if_request;
339
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'",
343 if_name);
344 return -1;
345 }
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
353 os_memset(&if_request, 0, sizeof(if_request));
354
355 os_strlcpy(if_request.device1, if_name, sizeof(if_request.device1));
356 if_request.cmd = DEL_VLAN_CMD;
357
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));
361 close(fd);
362 return -1;
363 }
364
365 close(fd);
366 return 0;
367}
368
369
370/*
371 Add a vlan interface with VLAN ID 'vid' and tagged interface
372 'if_name'.
373
374 returns -1 on error
375 returns 1 if the interface already exists
376 returns 0 otherwise
377*/
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700378int vlan_add(const char *if_name, int vid, const char *vlan_if_name)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700379{
380 int fd;
381 struct vlan_ioctl_args if_request;
382
383 wpa_printf(MSG_DEBUG, "VLAN: vlan_add(if_name=%s, vid=%d)",
384 if_name, vid);
385 ifconfig_up(if_name);
386
387 if ((os_strlen(if_name) + 1) > sizeof(if_request.device1)) {
388 wpa_printf(MSG_ERROR, "VLAN: Interface name too long: '%s'",
389 if_name);
390 return -1;
391 }
392
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));
396 return -1;
397 }
398
399 os_memset(&if_request, 0, sizeof(if_request));
400
401 /* Determine if a suitable vlan device already exists. */
402
403 os_snprintf(if_request.device1, sizeof(if_request.device1), "vlan%d",
404 vid);
405
406 if_request.cmd = _GET_VLAN_VID_CMD;
407
408 if (ioctl(fd, SIOCSIFVLAN, &if_request) == 0) {
409
410 if (if_request.u.VID == vid) {
411 if_request.cmd = _GET_VLAN_REALDEV_NAME_CMD;
412
413 if (ioctl(fd, SIOCSIFVLAN, &if_request) == 0 &&
414 os_strncmp(if_request.u.device2, if_name,
415 sizeof(if_request.u.device2)) == 0) {
416 close(fd);
417 wpa_printf(MSG_DEBUG, "VLAN: vlan_add: "
418 "if_name %s exists already",
419 if_request.device1);
420 return 1;
421 }
422 }
423 }
424
425 /* A suitable vlan device does not already exist, add one. */
426
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;
431
432 if (ioctl(fd, SIOCSIFVLAN, &if_request) < 0) {
433 wpa_printf(MSG_ERROR, "VLAN: %s: ADD_VLAN_CMD failed for %s: "
434 "%s",
435 __func__, if_request.device1, strerror(errno));
436 close(fd);
437 return -1;
438 }
439
440 close(fd);
441 return 0;
442}
443
444
445static int vlan_set_name_type(unsigned int name_type)
446{
447 int fd;
448 struct vlan_ioctl_args if_request;
449
450 wpa_printf(MSG_DEBUG, "VLAN: vlan_set_name_type(name_type=%u)",
451 name_type);
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));
455 return -1;
456 }
457
458 os_memset(&if_request, 0, sizeof(if_request));
459
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));
466 close(fd);
467 return -1;
468 }
469
470 close(fd);
471 return 0;
472}
473
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700474#endif /* CONFIG_VLAN_NETLINK */
475
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700476
477static void vlan_newlink(char *ifname, struct hostapd_data *hapd)
478{
479 char vlan_ifname[IFNAMSIZ];
480 char br_name[IFNAMSIZ];
481 struct hostapd_vlan *vlan = hapd->conf->vlan;
482 char *tagged_interface = hapd->conf->ssid.vlan_tagged_interface;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700483 int vlan_naming = hapd->conf->ssid.vlan_naming;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700484
485 wpa_printf(MSG_DEBUG, "VLAN: vlan_newlink(%s)", ifname);
486
487 while (vlan) {
Dmitry Shmidt83474442015-04-15 13:47:09 -0700488 if (os_strcmp(ifname, vlan->ifname) == 0 && !vlan->configured) {
489 vlan->configured = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700490
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700491 if (hapd->conf->vlan_bridge[0]) {
492 os_snprintf(br_name, sizeof(br_name), "%s%d",
493 hapd->conf->vlan_bridge,
494 vlan->vlan_id);
495 } else if (tagged_interface) {
496 os_snprintf(br_name, sizeof(br_name),
497 "br%s.%d", tagged_interface,
498 vlan->vlan_id);
499 } else {
500 os_snprintf(br_name, sizeof(br_name),
501 "brvlan%d", vlan->vlan_id);
502 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700503
Dmitry Shmidt818ea482014-03-10 13:15:21 -0700504 if (!br_addbr(br_name))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700505 vlan->clean |= DVLAN_CLEAN_BR;
506
507 ifconfig_up(br_name);
508
509 if (tagged_interface) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700510 if (vlan_naming ==
511 DYNAMIC_VLAN_NAMING_WITH_DEVICE)
512 os_snprintf(vlan_ifname,
513 sizeof(vlan_ifname),
514 "%s.%d", tagged_interface,
515 vlan->vlan_id);
516 else
517 os_snprintf(vlan_ifname,
518 sizeof(vlan_ifname),
519 "vlan%d", vlan->vlan_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700520
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700521 ifconfig_up(tagged_interface);
Dmitry Shmidt818ea482014-03-10 13:15:21 -0700522 if (!vlan_add(tagged_interface, vlan->vlan_id,
523 vlan_ifname))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700524 vlan->clean |= DVLAN_CLEAN_VLAN;
525
Dmitry Shmidt818ea482014-03-10 13:15:21 -0700526 if (!br_addif(br_name, vlan_ifname))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700527 vlan->clean |= DVLAN_CLEAN_VLAN_PORT;
528
529 ifconfig_up(vlan_ifname);
530 }
531
Dmitry Shmidt818ea482014-03-10 13:15:21 -0700532 if (!br_addif(br_name, ifname))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700533 vlan->clean |= DVLAN_CLEAN_WLAN_PORT;
534
535 ifconfig_up(ifname);
536
537 break;
538 }
539 vlan = vlan->next;
540 }
541}
542
543
544static void vlan_dellink(char *ifname, struct hostapd_data *hapd)
545{
546 char vlan_ifname[IFNAMSIZ];
547 char br_name[IFNAMSIZ];
548 struct hostapd_vlan *first, *prev, *vlan = hapd->conf->vlan;
549 char *tagged_interface = hapd->conf->ssid.vlan_tagged_interface;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700550 int vlan_naming = hapd->conf->ssid.vlan_naming;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700551
552 wpa_printf(MSG_DEBUG, "VLAN: vlan_dellink(%s)", ifname);
553
554 first = prev = vlan;
555
556 while (vlan) {
557 if (os_strcmp(ifname, vlan->ifname) == 0) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700558 if (hapd->conf->vlan_bridge[0]) {
559 os_snprintf(br_name, sizeof(br_name), "%s%d",
560 hapd->conf->vlan_bridge,
561 vlan->vlan_id);
562 } else if (tagged_interface) {
563 os_snprintf(br_name, sizeof(br_name),
564 "br%s.%d", tagged_interface,
565 vlan->vlan_id);
566 } else {
567 os_snprintf(br_name, sizeof(br_name),
568 "brvlan%d", vlan->vlan_id);
569 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700570
Dmitry Shmidt818ea482014-03-10 13:15:21 -0700571 if (vlan->clean & DVLAN_CLEAN_WLAN_PORT)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700572 br_delif(br_name, vlan->ifname);
573
574 if (tagged_interface) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700575 if (vlan_naming ==
576 DYNAMIC_VLAN_NAMING_WITH_DEVICE)
577 os_snprintf(vlan_ifname,
578 sizeof(vlan_ifname),
579 "%s.%d", tagged_interface,
580 vlan->vlan_id);
581 else
582 os_snprintf(vlan_ifname,
583 sizeof(vlan_ifname),
584 "vlan%d", vlan->vlan_id);
Dmitry Shmidt818ea482014-03-10 13:15:21 -0700585 if (vlan->clean & DVLAN_CLEAN_VLAN_PORT)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700586 br_delif(br_name, vlan_ifname);
587 ifconfig_down(vlan_ifname);
588
Dmitry Shmidt818ea482014-03-10 13:15:21 -0700589 if (vlan->clean & DVLAN_CLEAN_VLAN)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700590 vlan_rem(vlan_ifname);
591 }
592
593 if ((vlan->clean & DVLAN_CLEAN_BR) &&
594 br_getnumports(br_name) == 0) {
595 ifconfig_down(br_name);
596 br_delbr(br_name);
597 }
598
599 if (vlan == first) {
600 hapd->conf->vlan = vlan->next;
601 } else {
602 prev->next = vlan->next;
603 }
604 os_free(vlan);
605
606 break;
607 }
608 prev = vlan;
609 vlan = vlan->next;
610 }
611}
612
613
614static void
615vlan_read_ifnames(struct nlmsghdr *h, size_t len, int del,
616 struct hostapd_data *hapd)
617{
618 struct ifinfomsg *ifi;
619 int attrlen, nlmsg_len, rta_len;
620 struct rtattr *attr;
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800621 char ifname[IFNAMSIZ + 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700622
623 if (len < sizeof(*ifi))
624 return;
625
626 ifi = NLMSG_DATA(h);
627
628 nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg));
629
630 attrlen = h->nlmsg_len - nlmsg_len;
631 if (attrlen < 0)
632 return;
633
634 attr = (struct rtattr *) (((char *) ifi) + nlmsg_len);
635
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800636 os_memset(ifname, 0, sizeof(ifname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700637 rta_len = RTA_ALIGN(sizeof(struct rtattr));
638 while (RTA_OK(attr, attrlen)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700639 if (attr->rta_type == IFLA_IFNAME) {
640 int n = attr->rta_len - rta_len;
641 if (n < 0)
642 break;
643
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800644 if ((size_t) n >= sizeof(ifname))
645 n = sizeof(ifname) - 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700646 os_memcpy(ifname, ((char *) attr) + rta_len, n);
647
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700648 }
649
650 attr = RTA_NEXT(attr, attrlen);
651 }
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800652
653 if (!ifname[0])
654 return;
Dmitry Shmidt83474442015-04-15 13:47:09 -0700655 if (del && if_nametoindex(ifname)) {
656 /* interface still exists, race condition ->
657 * iface has just been recreated */
658 return;
659 }
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800660
661 wpa_printf(MSG_DEBUG,
662 "VLAN: RTM_%sLINK: ifi_index=%d ifname=%s ifi_family=%d ifi_flags=0x%x (%s%s%s%s)",
663 del ? "DEL" : "NEW",
664 ifi->ifi_index, ifname, ifi->ifi_family, ifi->ifi_flags,
665 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
666 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
667 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
668 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
669
670 if (del)
671 vlan_dellink(ifname, hapd);
672 else
673 vlan_newlink(ifname, hapd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700674}
675
676
677static void vlan_event_receive(int sock, void *eloop_ctx, void *sock_ctx)
678{
679 char buf[8192];
680 int left;
681 struct sockaddr_nl from;
682 socklen_t fromlen;
683 struct nlmsghdr *h;
684 struct hostapd_data *hapd = eloop_ctx;
685
686 fromlen = sizeof(from);
687 left = recvfrom(sock, buf, sizeof(buf), MSG_DONTWAIT,
688 (struct sockaddr *) &from, &fromlen);
689 if (left < 0) {
690 if (errno != EINTR && errno != EAGAIN)
691 wpa_printf(MSG_ERROR, "VLAN: %s: recvfrom failed: %s",
692 __func__, strerror(errno));
693 return;
694 }
695
696 h = (struct nlmsghdr *) buf;
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800697 while (NLMSG_OK(h, left)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700698 int len, plen;
699
700 len = h->nlmsg_len;
701 plen = len - sizeof(*h);
702 if (len > left || plen < 0) {
703 wpa_printf(MSG_DEBUG, "VLAN: Malformed netlink "
704 "message: len=%d left=%d plen=%d",
705 len, left, plen);
706 break;
707 }
708
709 switch (h->nlmsg_type) {
710 case RTM_NEWLINK:
711 vlan_read_ifnames(h, plen, 0, hapd);
712 break;
713 case RTM_DELLINK:
714 vlan_read_ifnames(h, plen, 1, hapd);
715 break;
716 }
717
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800718 h = NLMSG_NEXT(h, left);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700719 }
720
721 if (left > 0) {
722 wpa_printf(MSG_DEBUG, "VLAN: %s: %d extra bytes in the end of "
723 "netlink message", __func__, left);
724 }
725}
726
727
728static struct full_dynamic_vlan *
729full_dynamic_vlan_init(struct hostapd_data *hapd)
730{
731 struct sockaddr_nl local;
732 struct full_dynamic_vlan *priv;
733
734 priv = os_zalloc(sizeof(*priv));
735 if (priv == NULL)
736 return NULL;
737
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700738#ifndef CONFIG_VLAN_NETLINK
739 vlan_set_name_type(hapd->conf->ssid.vlan_naming ==
740 DYNAMIC_VLAN_NAMING_WITH_DEVICE ?
741 VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD :
742 VLAN_NAME_TYPE_PLUS_VID_NO_PAD);
743#endif /* CONFIG_VLAN_NETLINK */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700744
745 priv->s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
746 if (priv->s < 0) {
747 wpa_printf(MSG_ERROR, "VLAN: %s: socket(PF_NETLINK,SOCK_RAW,"
748 "NETLINK_ROUTE) failed: %s",
749 __func__, strerror(errno));
750 os_free(priv);
751 return NULL;
752 }
753
754 os_memset(&local, 0, sizeof(local));
755 local.nl_family = AF_NETLINK;
756 local.nl_groups = RTMGRP_LINK;
757 if (bind(priv->s, (struct sockaddr *) &local, sizeof(local)) < 0) {
758 wpa_printf(MSG_ERROR, "VLAN: %s: bind(netlink) failed: %s",
759 __func__, strerror(errno));
760 close(priv->s);
761 os_free(priv);
762 return NULL;
763 }
764
765 if (eloop_register_read_sock(priv->s, vlan_event_receive, hapd, NULL))
766 {
767 close(priv->s);
768 os_free(priv);
769 return NULL;
770 }
771
772 return priv;
773}
774
775
776static void full_dynamic_vlan_deinit(struct full_dynamic_vlan *priv)
777{
778 if (priv == NULL)
779 return;
780 eloop_unregister_read_sock(priv->s);
781 close(priv->s);
782 os_free(priv);
783}
784#endif /* CONFIG_FULL_DYNAMIC_VLAN */
785
786
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -0700787int vlan_setup_encryption_dyn(struct hostapd_data *hapd, const char *dyn_vlan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700788{
789 int i;
790
791 if (dyn_vlan == NULL)
792 return 0;
793
794 /* Static WEP keys are set here; IEEE 802.1X and WPA uses their own
795 * functions for setting up dynamic broadcast keys. */
796 for (i = 0; i < 4; i++) {
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -0700797 if (hapd->conf->ssid.wep.key[i] &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700798 hostapd_drv_set_key(dyn_vlan, hapd, WPA_ALG_WEP, NULL, i,
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -0700799 i == hapd->conf->ssid.wep.idx, NULL, 0,
800 hapd->conf->ssid.wep.key[i],
801 hapd->conf->ssid.wep.len[i]))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700802 {
803 wpa_printf(MSG_ERROR, "VLAN: Could not set WEP "
804 "encryption for dynamic VLAN");
805 return -1;
806 }
807 }
808
809 return 0;
810}
811
812
813static int vlan_dynamic_add(struct hostapd_data *hapd,
814 struct hostapd_vlan *vlan)
815{
816 while (vlan) {
817 if (vlan->vlan_id != VLAN_ID_WILDCARD) {
818 if (hostapd_vlan_if_add(hapd, vlan->ifname)) {
819 if (errno != EEXIST) {
820 wpa_printf(MSG_ERROR, "VLAN: Could "
821 "not add VLAN %s: %s",
822 vlan->ifname,
823 strerror(errno));
824 return -1;
825 }
826 }
827#ifdef CONFIG_FULL_DYNAMIC_VLAN
828 ifconfig_up(vlan->ifname);
829#endif /* CONFIG_FULL_DYNAMIC_VLAN */
830 }
831
832 vlan = vlan->next;
833 }
834
835 return 0;
836}
837
838
839static void vlan_dynamic_remove(struct hostapd_data *hapd,
840 struct hostapd_vlan *vlan)
841{
842 struct hostapd_vlan *next;
843
844 while (vlan) {
845 next = vlan->next;
846
847 if (vlan->vlan_id != VLAN_ID_WILDCARD &&
848 hostapd_vlan_if_remove(hapd, vlan->ifname)) {
849 wpa_printf(MSG_ERROR, "VLAN: Could not remove VLAN "
850 "iface: %s: %s",
851 vlan->ifname, strerror(errno));
852 }
853#ifdef CONFIG_FULL_DYNAMIC_VLAN
854 if (vlan->clean)
855 vlan_dellink(vlan->ifname, hapd);
856#endif /* CONFIG_FULL_DYNAMIC_VLAN */
857
858 vlan = next;
859 }
860}
861
862
863int vlan_init(struct hostapd_data *hapd)
864{
865#ifdef CONFIG_FULL_DYNAMIC_VLAN
866 hapd->full_dynamic_vlan = full_dynamic_vlan_init(hapd);
867#endif /* CONFIG_FULL_DYNAMIC_VLAN */
868
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700869 if (hapd->conf->ssid.dynamic_vlan != DYNAMIC_VLAN_DISABLED &&
870 !hapd->conf->vlan) {
871 /* dynamic vlans enabled but no (or empty) vlan_file given */
872 struct hostapd_vlan *vlan;
873 vlan = os_zalloc(sizeof(*vlan));
874 if (vlan == NULL) {
875 wpa_printf(MSG_ERROR, "Out of memory while assigning "
876 "VLAN interfaces");
877 return -1;
878 }
879
880 vlan->vlan_id = VLAN_ID_WILDCARD;
881 os_snprintf(vlan->ifname, sizeof(vlan->ifname), "%s.#",
882 hapd->conf->iface);
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -0700883 vlan->next = hapd->conf->vlan;
884 hapd->conf->vlan = vlan;
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700885 }
886
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700887 if (vlan_dynamic_add(hapd, hapd->conf->vlan))
888 return -1;
889
890 return 0;
891}
892
893
894void vlan_deinit(struct hostapd_data *hapd)
895{
896 vlan_dynamic_remove(hapd, hapd->conf->vlan);
897
898#ifdef CONFIG_FULL_DYNAMIC_VLAN
899 full_dynamic_vlan_deinit(hapd->full_dynamic_vlan);
Dmitry Shmidt56052862013-10-04 10:23:25 -0700900 hapd->full_dynamic_vlan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700901#endif /* CONFIG_FULL_DYNAMIC_VLAN */
902}
903
904
905struct hostapd_vlan * vlan_add_dynamic(struct hostapd_data *hapd,
906 struct hostapd_vlan *vlan,
907 int vlan_id)
908{
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800909 struct hostapd_vlan *n = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700910 char *ifname, *pos;
911
912 if (vlan == NULL || vlan_id <= 0 || vlan_id > MAX_VLAN_ID ||
913 vlan->vlan_id != VLAN_ID_WILDCARD)
914 return NULL;
915
916 wpa_printf(MSG_DEBUG, "VLAN: %s(vlan_id=%d ifname=%s)",
917 __func__, vlan_id, vlan->ifname);
918 ifname = os_strdup(vlan->ifname);
919 if (ifname == NULL)
920 return NULL;
921 pos = os_strchr(ifname, '#');
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800922 if (pos == NULL)
923 goto free_ifname;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700924 *pos++ = '\0';
925
926 n = os_zalloc(sizeof(*n));
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800927 if (n == NULL)
928 goto free_ifname;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700929
930 n->vlan_id = vlan_id;
931 n->dynamic_vlan = 1;
932
933 os_snprintf(n->ifname, sizeof(n->ifname), "%s%d%s", ifname, vlan_id,
934 pos);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700935
936 if (hostapd_vlan_if_add(hapd, n->ifname)) {
937 os_free(n);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800938 n = NULL;
939 goto free_ifname;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700940 }
941
942 n->next = hapd->conf->vlan;
943 hapd->conf->vlan = n;
944
945#ifdef CONFIG_FULL_DYNAMIC_VLAN
946 ifconfig_up(n->ifname);
947#endif /* CONFIG_FULL_DYNAMIC_VLAN */
948
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800949free_ifname:
950 os_free(ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700951 return n;
952}
953
954
955int vlan_remove_dynamic(struct hostapd_data *hapd, int vlan_id)
956{
957 struct hostapd_vlan *vlan;
958
959 if (vlan_id <= 0 || vlan_id > MAX_VLAN_ID)
960 return 1;
961
Dmitry Shmidt83474442015-04-15 13:47:09 -0700962 wpa_printf(MSG_DEBUG, "VLAN: %s(ifname=%s vlan_id=%d)",
963 __func__, hapd->conf->iface, vlan_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700964
965 vlan = hapd->conf->vlan;
966 while (vlan) {
967 if (vlan->vlan_id == vlan_id && vlan->dynamic_vlan > 0) {
968 vlan->dynamic_vlan--;
969 break;
970 }
971 vlan = vlan->next;
972 }
973
974 if (vlan == NULL)
975 return 1;
976
977 if (vlan->dynamic_vlan == 0)
978 hostapd_vlan_if_remove(hapd, vlan->ifname);
979
980 return 0;
981}