forked from mstpd/mstpd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
netif_utils.c
179 lines (157 loc) · 4.56 KB
/
netif_utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*****************************************************************************
Copyright (c) 2006 EMC Corporation.
Copyright (c) 2011 Factor-SPE
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your option)
any later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59
Temple Place - Suite 330, Boston, MA 02111-1307, USA.
The full GNU General Public License is included in this distribution in the
file called LICENSE.
Authors: Srinivas Aji <[email protected]>
Authors: Vitalii Demianets <[email protected]>
******************************************************************************/
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdbool.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <net/if.h>
#include <linux/if_ether.h>
#include <linux/ethtool.h>
#include <linux/sockios.h>
#include "log.h"
#ifndef SYSFS_CLASS_NET
#define SYSFS_CLASS_NET "/sys/class/net"
#endif
static int netsock = -1;
int netsock_init(void)
{
netsock = socket(AF_INET, SOCK_DGRAM, 0);
if(0 > netsock)
{
ERROR("Couldn't open inet socket for ioctls: %m\n");
return -1;
}
return 0;
}
int get_hwaddr(char *ifname, __u8 *hwaddr)
{
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, ifname, IFNAMSIZ - 1);
if(0 > ioctl(netsock, SIOCGIFHWADDR, &ifr))
{
ERROR("%s: get hw address failed: %m", ifname);
return -1;
}
memcpy(hwaddr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
return 0;
}
int get_flags(char *ifname)
{
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, ifname, IFNAMSIZ - 1);
if(0 > ioctl(netsock, SIOCGIFFLAGS, &ifr))
{
ERROR("%s: get interface flags failed: %m", ifname);
return -1;
}
return ifr.ifr_flags;
}
int if_shutdown(char *ifname)
{
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
/* TODO: Let's hope -1 is not a valid flag combination */
if(-1 == (ifr.ifr_flags = get_flags(ifname)))
{
return -1;
}
ifr.ifr_flags &= ~IFF_UP;
strncpy(ifr.ifr_name, ifname, IFNAMSIZ - 1);
if(0 > ioctl(netsock, SIOCSIFFLAGS, &ifr))
{
ERROR("%s: set if_down flag failed: %m", ifname);
return -1;
}
return 0;
}
int ethtool_get_speed_duplex(char *ifname, int *speed, int *duplex)
{
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, ifname, IFNAMSIZ - 1);
struct ethtool_cmd ecmd;
ecmd.cmd = ETHTOOL_GSET;
ifr.ifr_data = (caddr_t)&ecmd;
if(0 > ioctl(netsock, SIOCETHTOOL, &ifr))
{
ERROR("Cannot get speed/duplex for %s: %m\n", ifname);
return -1;
}
*speed = ecmd.speed; /* Ethtool speed is in Mbps */
*duplex = ecmd.duplex; /* We have same convention as ethtool.
0 = half, 1 = full */
return 0;
}
char *index_to_name(int index, char *name)
{
return if_indextoname(index, name);
}
char *index_to_port_name(int index, char *name)
{
return if_indextoname(index, name);
}
/********* Sysfs based utility functions *************/
/* This sysfs stuff might break with interface renames */
bool is_bridge(char *if_name)
{
char path[32 + IFNAMSIZ];
sprintf(path, SYSFS_CLASS_NET "/%s/bridge", if_name);
return (0 == access(path, R_OK));
}
int get_bridge_portno(char *if_name)
{
char path[32 + IFNAMSIZ];
sprintf(path, SYSFS_CLASS_NET "/%s/brport/port_no", if_name);
char buf[128];
int fd;
long res = -1;
TSTM((fd = open(path, O_RDONLY)) >= 0, -1, "%m");
int l;
TSTM((l = read(fd, buf, sizeof(buf) - 1)) >= 0, -1, "%m");
if(0 == l)
{
ERROR("Empty port index file");
goto out;
}
else if((sizeof(buf) - 1) == l)
{
ERROR("port_index file too long");
goto out;
}
buf[l] = 0;
if('\n' == buf[l - 1])
buf[l - 1] = 0;
char *end;
res = strtoul(buf, &end, 0);
if(0 != *end || INT_MAX < res)
{
ERROR("Invalid port index %s", buf);
res = -1;
}
out:
close(fd);
return res;
}