Use common driver code for Linux hwaddr get/set
[libeap.git] / src / drivers / linux_ioctl.c
1 /*
2  * Linux ioctl helper functions for driver wrappers
3  * Copyright (c) 2002-2010, Jouni Malinen <j@w1.fi>
4  *
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.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include "utils/includes.h"
16 #include <sys/ioctl.h>
17 #include <net/if.h>
18 #include <net/if_arp.h>
19
20 #include "utils/common.h"
21 #include "linux_ioctl.h"
22
23
24 int linux_set_iface_flags(int sock, const char *ifname, int dev_up)
25 {
26         struct ifreq ifr;
27
28         if (sock < 0)
29                 return -1;
30
31         os_memset(&ifr, 0, sizeof(ifr));
32         os_strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
33
34         if (ioctl(sock, SIOCGIFFLAGS, &ifr) != 0) {
35                 wpa_printf(MSG_ERROR, "Could not read interface %s flags: %s",
36                            ifname, strerror(errno));
37                 return -1;
38         }
39
40         if (dev_up) {
41                 if (ifr.ifr_flags & IFF_UP)
42                         return 0;
43                 ifr.ifr_flags |= IFF_UP;
44         } else {
45                 if (!(ifr.ifr_flags & IFF_UP))
46                         return 0;
47                 ifr.ifr_flags &= ~IFF_UP;
48         }
49
50         if (ioctl(sock, SIOCSIFFLAGS, &ifr) != 0) {
51                 wpa_printf(MSG_ERROR, "Could not set interface %s flags: %s",
52                            ifname, strerror(errno));
53                 return -1;
54         }
55
56         return 0;
57 }
58
59
60 int linux_get_ifhwaddr(int sock, const char *ifname, u8 *addr)
61 {
62         struct ifreq ifr;
63
64         os_memset(&ifr, 0, sizeof(ifr));
65         os_strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
66         if (ioctl(sock, SIOCGIFHWADDR, &ifr)) {
67                 wpa_printf(MSG_ERROR, "Could not get interface %s hwaddr: %s",
68                            ifname, strerror(errno));
69                 return -1;
70         }
71
72         if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
73                 wpa_printf(MSG_ERROR, "%s: Invalid HW-addr family 0x%04x",
74                            ifname, ifr.ifr_hwaddr.sa_family);
75                 return -1;
76         }
77         os_memcpy(addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
78
79         return 0;
80 }
81
82
83 int linux_set_ifhwaddr(int sock, const char *ifname, const u8 *addr)
84 {
85         struct ifreq ifr;
86
87         os_memset(&ifr, 0, sizeof(ifr));
88         os_strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
89         os_memcpy(ifr.ifr_hwaddr.sa_data, addr, ETH_ALEN);
90         ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
91
92         if (ioctl(sock, SIOCSIFHWADDR, &ifr)) {
93                 wpa_printf(MSG_DEBUG, "Could not set interface %s hwaddr: %s",
94                            ifname, strerror(errno));
95                 return -1;
96         }
97
98         return 0;
99 }