xref: /freebsd/contrib/wpa/src/ap/vlan_ifconfig.c (revision 780fb4a2fa9a9aee5ac48a60b790f567c0dc13e9)
1*780fb4a2SCy Schubert /*
2*780fb4a2SCy Schubert  * hostapd / VLAN ifconfig helpers
3*780fb4a2SCy Schubert  * Copyright 2003, Instant802 Networks, Inc.
4*780fb4a2SCy Schubert  * Copyright 2005-2006, Devicescape Software, Inc.
5*780fb4a2SCy Schubert  * Copyright (c) 2009, Jouni Malinen <j@w1.fi>
6*780fb4a2SCy Schubert  *
7*780fb4a2SCy Schubert  * This software may be distributed under the terms of the BSD license.
8*780fb4a2SCy Schubert  * See README for more details.
9*780fb4a2SCy Schubert  */
10*780fb4a2SCy Schubert 
11*780fb4a2SCy Schubert #include "utils/includes.h"
12*780fb4a2SCy Schubert #include <net/if.h>
13*780fb4a2SCy Schubert #include <sys/ioctl.h>
14*780fb4a2SCy Schubert 
15*780fb4a2SCy Schubert #include "utils/common.h"
16*780fb4a2SCy Schubert #include "vlan_util.h"
17*780fb4a2SCy Schubert 
18*780fb4a2SCy Schubert 
ifconfig_helper(const char * if_name,int up)19*780fb4a2SCy Schubert int ifconfig_helper(const char *if_name, int up)
20*780fb4a2SCy Schubert {
21*780fb4a2SCy Schubert 	int fd;
22*780fb4a2SCy Schubert 	struct ifreq ifr;
23*780fb4a2SCy Schubert 
24*780fb4a2SCy Schubert 	if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
25*780fb4a2SCy Schubert 		wpa_printf(MSG_ERROR, "VLAN: %s: socket(AF_INET,SOCK_STREAM) "
26*780fb4a2SCy Schubert 			   "failed: %s", __func__, strerror(errno));
27*780fb4a2SCy Schubert 		return -1;
28*780fb4a2SCy Schubert 	}
29*780fb4a2SCy Schubert 
30*780fb4a2SCy Schubert 	os_memset(&ifr, 0, sizeof(ifr));
31*780fb4a2SCy Schubert 	os_strlcpy(ifr.ifr_name, if_name, IFNAMSIZ);
32*780fb4a2SCy Schubert 
33*780fb4a2SCy Schubert 	if (ioctl(fd, SIOCGIFFLAGS, &ifr) != 0) {
34*780fb4a2SCy Schubert 		wpa_printf(MSG_ERROR, "VLAN: %s: ioctl(SIOCGIFFLAGS) failed "
35*780fb4a2SCy Schubert 			   "for interface %s: %s",
36*780fb4a2SCy Schubert 			   __func__, if_name, strerror(errno));
37*780fb4a2SCy Schubert 		close(fd);
38*780fb4a2SCy Schubert 		return -1;
39*780fb4a2SCy Schubert 	}
40*780fb4a2SCy Schubert 
41*780fb4a2SCy Schubert 	if (up)
42*780fb4a2SCy Schubert 		ifr.ifr_flags |= IFF_UP;
43*780fb4a2SCy Schubert 	else
44*780fb4a2SCy Schubert 		ifr.ifr_flags &= ~IFF_UP;
45*780fb4a2SCy Schubert 
46*780fb4a2SCy Schubert 	if (ioctl(fd, SIOCSIFFLAGS, &ifr) != 0) {
47*780fb4a2SCy Schubert 		wpa_printf(MSG_ERROR, "VLAN: %s: ioctl(SIOCSIFFLAGS) failed "
48*780fb4a2SCy Schubert 			   "for interface %s (up=%d): %s",
49*780fb4a2SCy Schubert 			   __func__, if_name, up, strerror(errno));
50*780fb4a2SCy Schubert 		close(fd);
51*780fb4a2SCy Schubert 		return -1;
52*780fb4a2SCy Schubert 	}
53*780fb4a2SCy Schubert 
54*780fb4a2SCy Schubert 	close(fd);
55*780fb4a2SCy Schubert 	return 0;
56*780fb4a2SCy Schubert }
57*780fb4a2SCy Schubert 
58*780fb4a2SCy Schubert 
ifconfig_up(const char * if_name)59*780fb4a2SCy Schubert int ifconfig_up(const char *if_name)
60*780fb4a2SCy Schubert {
61*780fb4a2SCy Schubert 	wpa_printf(MSG_DEBUG, "VLAN: Set interface %s up", if_name);
62*780fb4a2SCy Schubert 	return ifconfig_helper(if_name, 1);
63*780fb4a2SCy Schubert }
64*780fb4a2SCy Schubert 
65*780fb4a2SCy Schubert 
iface_exists(const char * ifname)66*780fb4a2SCy Schubert int iface_exists(const char *ifname)
67*780fb4a2SCy Schubert {
68*780fb4a2SCy Schubert 	return if_nametoindex(ifname);
69*780fb4a2SCy Schubert }
70