Share a single Linux ioctl helper fo setting interface up/down
[hostap-gosc2009.git] / src / drivers / linux_ioctl.c
blob733bdb2519fbdeae4594b596d6dfe76db1b90e04
1 /*
2 * Linux ioctl helper functions for driver wrappers
3 * Copyright (c) 2002-2010, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
12 * See README and COPYING for more details.
15 #include "utils/includes.h"
16 #include <sys/ioctl.h>
17 #include <net/if.h>
19 #include "utils/common.h"
20 #include "linux_ioctl.h"
23 int linux_set_iface_flags(int sock, const char *ifname, int dev_up)
25 struct ifreq ifr;
27 if (sock < 0)
28 return -1;
30 os_memset(&ifr, 0, sizeof(ifr));
31 os_strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
33 if (ioctl(sock, SIOCGIFFLAGS, &ifr) != 0) {
34 wpa_printf(MSG_ERROR, "Could not read interface %s flags: %s",
35 ifname, strerror(errno));
36 return -1;
39 if (dev_up) {
40 if (ifr.ifr_flags & IFF_UP)
41 return 0;
42 ifr.ifr_flags |= IFF_UP;
43 } else {
44 if (!(ifr.ifr_flags & IFF_UP))
45 return 0;
46 ifr.ifr_flags &= ~IFF_UP;
49 if (ioctl(sock, SIOCSIFFLAGS, &ifr) != 0) {
50 wpa_printf(MSG_ERROR, "Could not set interface %s flags: %s",
51 ifname, strerror(errno));
52 return -1;
55 return 0;