5 #include <sys/socket.h>
6 #include <linux/if_arp.h>
16 int device_ifindex(const char *ifname
)
21 if (!strncmp("any", ifname
, strlen("any")))
24 sock
= af_socket(AF_INET
);
26 memset(&ifr
, 0, sizeof(ifr
));
27 strlcpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
29 ret
= ioctl(sock
, SIOCGIFINDEX
, &ifr
);
31 panic("Cannot get ifindex from device!\n");
33 index
= ifr
.ifr_ifindex
;
39 int device_type(const char *ifname
)
44 if (!strncmp("any", ifname
, strlen("any")))
47 sock
= af_socket(AF_INET
);
49 memset(&ifr
, 0, sizeof(ifr
));
50 strlcpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
52 ret
= ioctl(sock
, SIOCGIFHWADDR
, &ifr
);
54 panic("Cannot get iftype from device!\n");
57 type
= ifr
.ifr_hwaddr
.sa_family
;
63 static int __device_address6(const char *ifname
, struct sockaddr_storage
*ss
)
65 int ret
, family
, found
= -EINVAL
;
66 struct ifaddrs
*ifaddr
, *ifa
;
68 ret
= getifaddrs(&ifaddr
);
69 if (unlikely(ret
< 0))
70 panic("Cannot get device addresses for IPv6!\n");
72 for (ifa
= ifaddr
; ifa
!= NULL
; ifa
= ifa
->ifa_next
) {
73 family
= ifa
->ifa_addr
->sa_family
;
74 if (family
!= AF_INET6
)
76 if (strcmp(ifa
->ifa_name
, ifname
))
79 memcpy(ss
, ifa
->ifa_addr
, sizeof(*ss
));
88 int device_address(const char *ifname
, int af
, struct sockaddr_storage
*ss
)
95 if (!strncmp("any", ifname
, strlen("any")))
98 return __device_address6(ifname
, ss
);
100 sock
= af_socket(af
);
102 memset(&ifr
, 0, sizeof(ifr
));
103 strlcpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
105 ifr
.ifr_addr
.sa_family
= af
;
107 ret
= ioctl(sock
, SIOCGIFADDR
, &ifr
);
109 memcpy(ss
, &ifr
.ifr_addr
, sizeof(ifr
.ifr_addr
));
115 size_t device_mtu(const char *ifname
)
121 sock
= af_socket(AF_INET
);
123 memset(&ifr
, 0, sizeof(ifr
));
124 strlcpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
126 ret
= ioctl(sock
, SIOCGIFMTU
, &ifr
);
134 short device_get_flags(const char *ifname
)
140 sock
= af_socket(AF_INET
);
142 memset(&ifr
, 0, sizeof(ifr
));
143 strlcpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
145 ret
= ioctl(sock
, SIOCGIFFLAGS
, &ifr
);
147 flags
= ifr
.ifr_flags
;
153 void device_set_flags(const char *ifname
, const short flags
)
158 sock
= af_socket(AF_INET
);
160 memset(&ifr
, 0, sizeof(ifr
));
161 strlcpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
162 ifr
.ifr_flags
= flags
;
164 ret
= ioctl(sock
, SIOCSIFFLAGS
, &ifr
);
165 if (unlikely(ret
< 0))
166 panic("Cannot set NIC flags!\n");
171 int device_up_and_running(const char *ifname
)
175 if (!strncmp("any", ifname
, strlen("any")))
178 return (device_get_flags(ifname
) &
179 (IFF_UP
| IFF_RUNNING
)) ==
180 (IFF_UP
| IFF_RUNNING
);
183 u32
device_bitrate(const char *ifname
)
185 u32 scopper
, swireless
;
187 scopper
= ethtool_bitrate(ifname
);
188 swireless
= wireless_bitrate(ifname
);
190 return scopper
? : swireless
;
193 short device_enter_promiscuous_mode(const char *ifname
)
197 if (!strncmp("any", ifname
, strlen("any")))
200 ifflags
= device_get_flags(ifname
);
201 device_set_flags(ifname
, ifflags
| IFF_PROMISC
);
206 void device_leave_promiscuous_mode(const char *ifname
, short oldflags
)
208 if (!strncmp("any", ifname
, strlen("any")))
211 device_set_flags(ifname
, oldflags
);