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; /* treat as *idxmap[16] */
32 static struct idxmap *find_by_index(int idx)
37 for (im = idxmap[idx & 0xF]; im; im = im->next)
43 int FAST_FUNC ll_remember_index(const struct sockaddr_nl *who UNUSED_PARAM,
45 void *arg UNUSED_PARAM)
48 struct ifinfomsg *ifi = NLMSG_DATA(n);
49 struct idxmap *im, **imp;
50 struct rtattr *tb[IFLA_MAX+1];
52 if (n->nlmsg_type != RTM_NEWLINK)
55 if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifi)))
58 memset(tb, 0, sizeof(tb));
59 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), IFLA_PAYLOAD(n));
60 if (tb[IFLA_IFNAME] == NULL)
64 idxmap = xzalloc(sizeof(idxmap[0]) * 16);
66 h = ifi->ifi_index & 0xF;
67 for (imp = &idxmap[h]; (im = *imp) != NULL; imp = &im->next)
68 if (im->index == ifi->ifi_index)
71 im = xmalloc(sizeof(*im));
73 im->index = ifi->ifi_index;
76 im->type = ifi->ifi_type;
77 im->flags = ifi->ifi_flags;
78 if (tb[IFLA_ADDRESS]) {
80 im->alen = alen = RTA_PAYLOAD(tb[IFLA_ADDRESS]);
81 if (alen > (int)sizeof(im->addr))
82 alen = sizeof(im->addr);
83 memcpy(im->addr, RTA_DATA(tb[IFLA_ADDRESS]), alen);
86 memset(im->addr, 0, sizeof(im->addr));
88 strcpy(im->name, RTA_DATA(tb[IFLA_IFNAME]));
92 const char FAST_FUNC *ll_idx_n2a(int idx, char *buf)
98 im = find_by_index(idx);
101 snprintf(buf, 16, "if%d", idx);
106 const char FAST_FUNC *ll_index_to_name(int idx)
108 static char nbuf[16];
110 return ll_idx_n2a(idx, nbuf);
114 int ll_index_to_type(int idx)
120 im = find_by_index(idx);
127 unsigned FAST_FUNC ll_index_to_flags(int idx)
133 im = find_by_index(idx);
139 int FAST_FUNC xll_name_to_index(const char *name)
144 /* caching is not warranted - no users which repeatedly call it */
146 static char ncache[16];
154 if (icache && strcmp(name, ncache) == 0) {
159 for (i = 0; i < 16; i++) {
160 for (im = idxmap[i]; im; im = im->next) {
161 if (strcmp(im->name, name) == 0) {
163 strcpy(ncache, name);
170 /* We have not found the interface in our cache, but the kernel
171 * may still know about it. One reason is that we may be using
172 * module on-demand loading, which means that the kernel will
173 * load the module and make the interface exist only when
174 * we explicitely request it (check for dev_load() in net/core/dev.c).
175 * I can think of other similar scenario, but they are less common...
179 sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
184 strncpy_IFNAMSIZ(ifr.ifr_name, name);
185 ifr.ifr_ifindex = -1;
186 tmp = ioctl(sock_fd, SIOCGIFINDEX, &ifr);
189 /* In theory, we should redump the interface list
190 * to update our cache, this is left as an exercise
191 * to the reader... Jean II */
192 ret = ifr.ifr_ifindex;
196 bb_error_msg_and_die("cannot find device \"%s\"", name);
200 int FAST_FUNC ll_init_map(struct rtnl_handle *rth)
202 xrtnl_wilddump_request(rth, AF_UNSPEC, RTM_GETLINK);
203 xrtnl_dump_filter(rth, ll_remember_index, NULL);