1 /*****************************************************************************
2 Copyright (c) 2006 EMC Corporation.
3 Copyright (c) 2011 Factor-SPE
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the Free
7 Software Foundation; either version 2 of the License, or (at your option)
10 This program is distributed in the hope that it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 You should have received a copy of the GNU General Public License along with
16 this program; if not, write to the Free Software Foundation, Inc., 59
17 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 The full GNU General Public License is included in this distribution in the
22 Authors: Srinivas Aji <Aji_Srinivas@emc.com>
23 Authors: Vitalii Demianets <dvitasgs@gmail.com>
25 ******************************************************************************/
33 #include <sys/ioctl.h>
34 #include <sys/types.h>
36 #include <linux/if_ether.h>
37 #include <linux/ethtool.h>
38 #include <linux/sockios.h>
40 #include "netif_utils.h"
43 #ifndef SYSFS_CLASS_NET
44 #define SYSFS_CLASS_NET "/sys/class/net"
47 static int netsock
= -1;
49 int netsock_init(void)
51 netsock
= socket(AF_INET
, SOCK_DGRAM
, 0);
54 ERROR("Couldn't open inet socket for ioctls: %m\n");
60 int get_hwaddr(char *ifname
, __u8
*hwaddr
)
63 memset(&ifr
, 0, sizeof(ifr
));
64 strncpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
- 1);
65 if(0 > ioctl(netsock
, SIOCGIFHWADDR
, &ifr
))
67 ERROR("%s: get hw address failed: %m", ifname
);
70 memcpy(hwaddr
, ifr
.ifr_hwaddr
.sa_data
, ETH_ALEN
);
74 int get_flags(char *ifname
)
77 memset(&ifr
, 0, sizeof(ifr
));
78 strncpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
- 1);
79 if(0 > ioctl(netsock
, SIOCGIFFLAGS
, &ifr
))
81 ERROR("%s: get interface flags failed: %m", ifname
);
87 int if_shutdown(char *ifname
)
91 memset(&ifr
, 0, sizeof(ifr
));
92 /* TODO: Let's hope -1 is not a valid flag combination */
93 if(-1 == (ifr
.ifr_flags
= get_flags(ifname
)))
97 ifr
.ifr_flags
&= ~IFF_UP
;
98 strncpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
- 1);
99 if(0 > ioctl(netsock
, SIOCSIFFLAGS
, &ifr
))
101 ERROR("%s: set if_down flag failed: %m", ifname
);
107 int ethtool_get_speed_duplex(char *ifname
, int *speed
, int *duplex
)
110 memset(&ifr
, 0, sizeof(ifr
));
111 strncpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
- 1);
112 struct ethtool_cmd ecmd
;
114 ecmd
.cmd
= ETHTOOL_GSET
;
115 ifr
.ifr_data
= (caddr_t
)&ecmd
;
116 if(0 > ioctl(netsock
, SIOCETHTOOL
, &ifr
))
118 INFO("Cannot get speed/duplex for %s: %m\n", ifname
);
121 *speed
= ecmd
.speed
; /* Ethtool speed is in Mbps */
122 *duplex
= ecmd
.duplex
; /* We have same convention as ethtool.
123 0 = half, 1 = full */
127 /********* Sysfs based utility functions *************/
129 /* This sysfs stuff might break with interface renames */
130 bool is_bridge(char *if_name
)
132 char path
[32 + IFNAMSIZ
];
133 sprintf(path
, SYSFS_CLASS_NET
"/%s/bridge", if_name
);
134 return (0 == access(path
, R_OK
));
137 static int get_port_file(const char *if_name
, const char *file
)
139 char path
[32 + IFNAMSIZ
];
140 sprintf(path
, SYSFS_CLASS_NET
"/%s/brport/%s", if_name
, file
);
144 TSTM((fd
= open(path
, O_RDONLY
)) >= 0, -1, "%m");
146 TSTM((l
= read(fd
, buf
, sizeof(buf
) - 1)) >= 0, -1, "%m");
149 ERROR("Empty %s file", file
);
152 else if((sizeof(buf
) - 1) == l
)
154 ERROR("%s file too long", file
);
158 if('\n' == buf
[l
- 1])
161 res
= strtoul(buf
, &end
, 0);
162 if(0 != *end
|| INT_MAX
< res
)
164 ERROR("Invalid %s %s", file
, buf
);
172 int get_bpdu_filter(char *if_name
)
174 return get_port_file(if_name
, "bpdu_filter");
177 int get_bridge_portno(char *if_name
)
179 return get_port_file(if_name
, "port_no");