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 <net/if.h> /* struct ifreq and co. */
17 #include "libnetlink.h"
26 unsigned char addr[8];
30 static struct idxmap *idxmap[16];
32 static struct idxmap *find_by_index(int idx)
36 for (im = idxmap[idx & 0xF]; im; im = im->next)
42 int ll_remember_index(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
45 struct ifinfomsg *ifi = NLMSG_DATA(n);
46 struct idxmap *im, **imp;
47 struct rtattr *tb[IFLA_MAX+1];
49 if (n->nlmsg_type != RTM_NEWLINK)
52 if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifi)))
55 memset(tb, 0, sizeof(tb));
56 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), IFLA_PAYLOAD(n));
57 if (tb[IFLA_IFNAME] == NULL)
60 h = ifi->ifi_index & 0xF;
62 for (imp = &idxmap[h]; (im = *imp) != NULL; imp = &im->next)
63 if (im->index == ifi->ifi_index)
66 im = xmalloc(sizeof(*im));
68 im->index = ifi->ifi_index;
71 im->type = ifi->ifi_type;
72 im->flags = ifi->ifi_flags;
73 if (tb[IFLA_ADDRESS]) {
75 im->alen = alen = RTA_PAYLOAD(tb[IFLA_ADDRESS]);
76 if (alen > sizeof(im->addr))
77 alen = sizeof(im->addr);
78 memcpy(im->addr, RTA_DATA(tb[IFLA_ADDRESS]), alen);
81 memset(im->addr, 0, sizeof(im->addr));
83 strcpy(im->name, RTA_DATA(tb[IFLA_IFNAME]));
87 const char *ll_idx_n2a(int idx, char *buf)
93 im = find_by_index(idx);
96 snprintf(buf, 16, "if%d", idx);
101 const char *ll_index_to_name(int idx)
103 static char nbuf[16];
105 return ll_idx_n2a(idx, nbuf);
109 int ll_index_to_type(int idx)
115 im = find_by_index(idx);
122 unsigned ll_index_to_flags(int idx)
128 im = find_by_index(idx);
134 int xll_name_to_index(const char *const name)
139 /* caching is not warranted - no users which repeatedly call it */
141 static char ncache[16];
149 if (icache && strcmp(name, ncache) == 0) {
153 for (i = 0; i < 16; i++) {
154 for (im = idxmap[i]; im; im = im->next) {
155 if (strcmp(im->name, name) == 0) {
157 strcpy(ncache, name);
163 /* We have not found the interface in our cache, but the kernel
164 * may still know about it. One reason is that we may be using
165 * module on-demand loading, which means that the kernel will
166 * load the module and make the interface exist only when
167 * we explicitely request it (check for dev_load() in net/core/dev.c).
168 * I can think of other similar scenario, but they are less common...
172 sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
177 strncpy(ifr.ifr_name, name, IFNAMSIZ);
178 ifr.ifr_ifindex = -1;
179 tmp = ioctl(sock_fd, SIOCGIFINDEX, &ifr);
182 /* In theory, we should redump the interface list
183 * to update our cache, this is left as an exercise
184 * to the reader... Jean II */
185 ret = ifr.ifr_ifindex;
189 bb_error_msg_and_die("cannot find device \"%s\"", name);
193 int ll_init_map(struct rtnl_handle *rth)
195 xrtnl_wilddump_request(rth, AF_UNSPEC, RTM_GETLINK);
196 xrtnl_dump_filter(rth, ll_remember_index, &idxmap);