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 
ifconfig_helper(const char * if_name,int up)19 int ifconfig_helper(const char *if_name, int up)
20 {
21 	int fd;
22 	struct ifreq ifr;
23 
24 	if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
25 		wpa_printf(MSG_ERROR, "VLAN: %s: socket(AF_INET,SOCK_STREAM) "
26 			   "failed: %s", __func__, strerror(errno));
27 		return -1;
28 	}
29 
30 	os_memset(&ifr, 0, sizeof(ifr));
31 	os_strlcpy(ifr.ifr_name, if_name, IFNAMSIZ);
32 
33 	if (ioctl(fd, SIOCGIFFLAGS, &ifr) != 0) {
34 		wpa_printf(MSG_ERROR, "VLAN: %s: ioctl(SIOCGIFFLAGS) failed "
35 			   "for interface %s: %s",
36 			   __func__, if_name, strerror(errno));
37 		close(fd);
38 		return -1;
39 	}
40 
41 	if (up)
42 		ifr.ifr_flags |= IFF_UP;
43 	else
44 		ifr.ifr_flags &= ~IFF_UP;
45 
46 	if (ioctl(fd, SIOCSIFFLAGS, &ifr) != 0) {
47 		wpa_printf(MSG_ERROR, "VLAN: %s: ioctl(SIOCSIFFLAGS) failed "
48 			   "for interface %s (up=%d): %s",
49 			   __func__, if_name, up, strerror(errno));
50 		close(fd);
51 		return -1;
52 	}
53 
54 	close(fd);
55 	return 0;
56 }
57 
58 
ifconfig_up(const char * if_name)59 int ifconfig_up(const char *if_name)
60 {
61 	wpa_printf(MSG_DEBUG, "VLAN: Set interface %s up", if_name);
62 	return ifconfig_helper(if_name, 1);
63 }
64 
65 
iface_exists(const char * ifname)66 int iface_exists(const char *ifname)
67 {
68 	return if_nametoindex(ifname);
69 }
70