1 /* vi: set sw=4 ts=4: */
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version
8 * 2 of the License, or (at your option) any later version.
10 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
14 //#include <sys/socket.h> /* socket() */
15 #include <net/if.h> /* struct ifreq and co. */
16 //#include <sys/ioctl.h> /* ioctl() & SIOCGIFINDEX */
19 #include "libnetlink.h"
28 unsigned char addr[8];
32 static struct idxmap *idxmap[16];
34 int ll_remember_index(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
37 struct ifinfomsg *ifi = NLMSG_DATA(n);
38 struct idxmap *im, **imp;
39 struct rtattr *tb[IFLA_MAX+1];
41 if (n->nlmsg_type != RTM_NEWLINK)
44 if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifi)))
48 memset(tb, 0, sizeof(tb));
49 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), IFLA_PAYLOAD(n));
50 if (tb[IFLA_IFNAME] == NULL)
53 h = ifi->ifi_index&0xF;
55 for (imp = &idxmap[h]; (im = *imp) != NULL; imp = &im->next)
56 if (im->index == ifi->ifi_index)
60 im = xmalloc(sizeof(*im));
62 im->index = ifi->ifi_index;
66 im->type = ifi->ifi_type;
67 im->flags = ifi->ifi_flags;
68 if (tb[IFLA_ADDRESS]) {
70 im->alen = alen = RTA_PAYLOAD(tb[IFLA_ADDRESS]);
71 if (alen > sizeof(im->addr))
72 alen = sizeof(im->addr);
73 memcpy(im->addr, RTA_DATA(tb[IFLA_ADDRESS]), alen);
76 memset(im->addr, 0, sizeof(im->addr));
78 strcpy(im->name, RTA_DATA(tb[IFLA_IFNAME]));
82 const char *ll_idx_n2a(int idx, char *buf)
88 for (im = idxmap[idx & 0xF]; im; im = im->next)
91 snprintf(buf, 16, "if%d", idx);
96 const char *ll_index_to_name(int idx)
100 return ll_idx_n2a(idx, nbuf);
103 int ll_index_to_type(int idx)
109 for (im = idxmap[idx & 0xF]; im; im = im->next)
110 if (im->index == idx)
115 unsigned ll_index_to_flags(int idx)
122 for (im = idxmap[idx & 0xF]; im; im = im->next)
123 if (im->index == idx)
128 // TODO: caching is not warranted - no users which repeatedly call it
129 int xll_name_to_index(const char * const name)
131 static char ncache[16];
141 if (icache && strcmp(name, ncache) == 0) {
145 for (i = 0; i < 16; i++) {
146 for (im = idxmap[i]; im; im = im->next) {
147 if (strcmp(im->name, name) == 0) {
149 strcpy(ncache, name);
155 /* We have not found the interface in our cache, but the kernel
156 * may still know about it. One reason is that we may be using
157 * module on-demand loading, which means that the kernel will
158 * load the module and make the interface exist only when
159 * we explicitely request it (check for dev_load() in net/core/dev.c).
160 * I can think of other similar scenario, but they are less common...
162 sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
166 strncpy(ifr.ifr_name, name, IFNAMSIZ);
167 ifr.ifr_ifindex = -1;
168 tmp = ioctl(sock_fd, SIOCGIFINDEX, &ifr);
171 /* In theory, we should redump the interface list
172 * to update our cache, this is left as an exercise
173 * to the reader... Jean II */
174 ret = ifr.ifr_ifindex;
178 bb_error_msg_and_die("cannot find device \"%s\"", name);
182 int ll_init_map(struct rtnl_handle *rth)
184 xrtnl_wilddump_request(rth, AF_UNSPEC, RTM_GETLINK);
185 xrtnl_dump_filter(rth, ll_remember_index, &idxmap);