Line data Source code
1 : /*
2 : * hostapd / VLAN ifconfig helpers
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 :
11 : #include "utils/includes.h"
12 : #include <net/if.h>
13 : #include <sys/ioctl.h>
14 :
15 : #include "utils/common.h"
16 : #include "vlan_util.h"
17 :
18 :
19 209 : int ifconfig_helper(const char *if_name, int up)
20 : {
21 : int fd;
22 : struct ifreq ifr;
23 :
24 209 : if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
25 0 : wpa_printf(MSG_ERROR, "VLAN: %s: socket(AF_INET,SOCK_STREAM) "
26 0 : "failed: %s", __func__, strerror(errno));
27 0 : return -1;
28 : }
29 :
30 209 : os_memset(&ifr, 0, sizeof(ifr));
31 209 : os_strlcpy(ifr.ifr_name, if_name, IFNAMSIZ);
32 :
33 209 : if (ioctl(fd, SIOCGIFFLAGS, &ifr) != 0) {
34 7 : wpa_printf(MSG_ERROR, "VLAN: %s: ioctl(SIOCGIFFLAGS) failed "
35 : "for interface %s: %s",
36 7 : __func__, if_name, strerror(errno));
37 7 : close(fd);
38 7 : return -1;
39 : }
40 :
41 202 : if (up)
42 170 : ifr.ifr_flags |= IFF_UP;
43 : else
44 32 : ifr.ifr_flags &= ~IFF_UP;
45 :
46 202 : if (ioctl(fd, SIOCSIFFLAGS, &ifr) != 0) {
47 0 : wpa_printf(MSG_ERROR, "VLAN: %s: ioctl(SIOCSIFFLAGS) failed "
48 : "for interface %s (up=%d): %s",
49 0 : __func__, if_name, up, strerror(errno));
50 0 : close(fd);
51 0 : return -1;
52 : }
53 :
54 202 : close(fd);
55 202 : return 0;
56 : }
57 :
58 :
59 172 : int ifconfig_up(const char *if_name)
60 : {
61 172 : wpa_printf(MSG_DEBUG, "VLAN: Set interface %s up", if_name);
62 172 : return ifconfig_helper(if_name, 1);
63 : }
64 :
65 :
66 51 : int iface_exists(const char *ifname)
67 : {
68 51 : return if_nametoindex(ifname);
69 : }
|