3 #include <sys/socket.h>
4 #include <netinet/in.h>
7 #include <net/if_arp.h>
8 #include <linux/netlink.h>
9 #include <linux/rtnetlink.h>
15 * Use netlink sockets to find interfaces.
16 * Thanks to Erik Quanstrom.
19 netlinkrequest(int fd, int type, int (*fn)(struct nlmsghdr *h, Ipifc**, int),
20 Ipifc **ifc, int index)
24 struct sockaddr_nl nl;
31 memset(&nl, 0, sizeof nl);
32 nl.nl_family = AF_NETLINK;
34 memset(&req, 0, sizeof req);
35 req.nlh.nlmsg_len = sizeof req;
36 req.nlh.nlmsg_type = type;
37 req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
38 req.nlh.nlmsg_pid = 0;
39 req.nlh.nlmsg_seq = 1;
40 req.g.rtgen_family = AF_NETLINK;
42 if(sendto(fd, (void*)&req, sizeof req, 0, (struct sockaddr*)&nl, sizeof nl) < 0)
45 while((n=read(fd, buf, sizeof buf)) > 0){
46 for(h=(struct nlmsghdr*)buf; NLMSG_OK(h, n); h = NLMSG_NEXT(h, n)){
47 if(h->nlmsg_type == NLMSG_DONE)
49 if(h->nlmsg_type == NLMSG_ERROR){
50 werrstr("netlink error");
53 if(fn(h, ifc, index) < 0)
57 werrstr("netlink error");
64 /* we couldn't care less which one; just want to talk to the kernel! */
65 static int dumb[3] = { AF_INET, AF_PACKET, AF_INET6 };
68 for(i=0; i<nelem(dumb); i++)
69 if((fd = socket(dumb[i], SOCK_DGRAM, 0)) >= 0)
75 parsertattr(struct rtattr **dst, int ndst, struct nlmsghdr *h, int type, int skip)
80 len = h->nlmsg_len - NLMSG_LENGTH(skip);
81 if(len < 0 || h->nlmsg_type != type){
82 werrstr("attrs too short");
85 src = (struct rtattr*)((char*)NLMSG_DATA(h) + NLMSG_ALIGN(skip));
87 memset(dst, 0, ndst*sizeof dst[0]);
88 for(; RTA_OK(src, len); src = RTA_NEXT(src, len))
89 if(src->rta_type < ndst)
90 dst[src->rta_type] = src;
95 rta2ip(int af, uchar *ip, struct rtattr *rta)
97 memset(ip, 0, IPaddrlen);
101 memmove(ip, v4prefix, IPv4off);
102 memmove(ip+IPv4off, RTA_DATA(rta), IPv4addrlen);
105 memmove(ip, RTA_DATA(rta), IPaddrlen);
111 getlink(struct nlmsghdr *h, Ipifc **ipifclist, int index)
115 struct rtattr *attr[IFLA_MAX+1];
116 struct ifinfomsg *ifi;
119 ifi = (struct ifinfomsg*)NLMSG_DATA(h);
120 if(index >= 0 && ifi->ifi_index != index)
123 ifc = mallocz(sizeof *ifc, 1);
126 ifc->index = ifi->ifi_index;
129 ipifclist = &(*ipifclist)->next;
132 if(parsertattr(attr, nelem(attr), h, RTM_NEWLINK, sizeof(struct ifinfomsg)) < 0)
135 if(attr[IFLA_IFNAME])
136 p = (char*)RTA_DATA(attr[IFLA_IFNAME]);
139 strecpy(ifc->dev, ifc->dev+sizeof ifc->dev, p);
142 ifc->mtu = *(int*)RTA_DATA(attr[IFLA_MTU]);
145 * Does not work on old Linux systems,
146 * and not really necessary for my purposes.
147 * Uncomment if you want it bad.
149 if(attr[IFLA_STATS]){
150 struct rtnl_link_stats *s;
152 s = RTA_DATA(attr[IFLA_STATS]);
153 ifc->pktin = s->rx_packets;
154 ifc->pktout = s->tx_packets;
155 ifc->errin = s->rx_errors;
156 ifc->errout = s->tx_errors;
161 if((fd = devsocket()) > 0){
164 memset(&ifr, 0, sizeof ifr);
165 strncpy(ifr.ifr_name, p, IFNAMSIZ);
167 if(ioctl(fd, SIOCGIFMTU, &ifr) >= 0)
168 ifc->rp.linkmtu = ifr.ifr_mtu;
170 memset(&ifr, 0, sizeof ifr);
171 strncpy(ifr.ifr_name, p, IFNAMSIZ);
172 if(ioctl(fd, SIOCGIFHWADDR, &ifr) >= 0
173 && ifr.ifr_hwaddr.sa_family == ARPHRD_ETHER)
174 memmove(ifc->ether, ifr.ifr_hwaddr.sa_data, 6);
182 getaddr(struct nlmsghdr *h, Ipifc **ipifclist, int index)
187 struct ifaddrmsg *ifa;
188 struct rtattr *attr[IFA_MAX+1];
192 ifa = (struct ifaddrmsg*)NLMSG_DATA(h);
193 for(ifc=*ipifclist; ifc; ifc=ifc->next)
194 if(ifc->index == ifa->ifa_index)
198 if(parsertattr(attr, nelem(attr), h, RTM_NEWADDR, sizeof(struct ifaddrmsg)) < 0)
201 lifc = mallocz(sizeof *lifc, 1);
204 for(l=&ifc->lifc; *l; l=&(*l)->next)
208 if(attr[IFA_ADDRESS] == nil)
209 attr[IFA_ADDRESS] = attr[IFA_LOCAL];
210 if(attr[IFA_ADDRESS] == nil)
213 rta2ip(ifa->ifa_family, lifc->ip, attr[IFA_ADDRESS]);
215 mask = ifa->ifa_prefixlen/8;
216 if(ifa->ifa_family == AF_INET)
218 memset(lifc->mask, 0xFF, mask);
219 memmove(lifc->net, lifc->ip, mask);
221 if(attr[IFA_CACHEINFO]){
222 struct ifa_cacheinfo *ci;
224 ci = RTA_DATA(attr[IFA_CACHEINFO]);
225 lifc->preflt = ci->ifa_prefered;
226 lifc->validlt = ci->ifa_valid;
232 readipifc(char *net, Ipifc *ifc, int index)
240 fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
244 if(netlinkrequest(fd, RTM_GETLINK, getlink, &ifc, index) < 0
245 || netlinkrequest(fd, RTM_GETADDR, getaddr, &ifc, index) < 0){