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>
17 #include "libnetlink.h"
20 #include <sys/socket.h> /* socket() */
21 #include <net/if.h> /* struct ifreq and co. */
22 #include <sys/ioctl.h> /* ioctl() & SIOCGIFINDEX */
30 unsigned char addr[8];
34 static struct idxmap *idxmap[16];
36 int ll_remember_index(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
39 struct ifinfomsg *ifi = NLMSG_DATA(n);
40 struct idxmap *im, **imp;
41 struct rtattr *tb[IFLA_MAX+1];
43 if (n->nlmsg_type != RTM_NEWLINK)
46 if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifi)))
50 memset(tb, 0, sizeof(tb));
51 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), IFLA_PAYLOAD(n));
52 if (tb[IFLA_IFNAME] == NULL)
55 h = ifi->ifi_index&0xF;
57 for (imp = &idxmap[h]; (im = *imp) != NULL; imp = &im->next)
58 if (im->index == ifi->ifi_index)
62 im = xmalloc(sizeof(*im));
64 im->index = ifi->ifi_index;
68 im->type = ifi->ifi_type;
69 im->flags = ifi->ifi_flags;
70 if (tb[IFLA_ADDRESS]) {
72 im->alen = alen = RTA_PAYLOAD(tb[IFLA_ADDRESS]);
73 if (alen > sizeof(im->addr))
74 alen = sizeof(im->addr);
75 memcpy(im->addr, RTA_DATA(tb[IFLA_ADDRESS]), alen);
78 memset(im->addr, 0, sizeof(im->addr));
80 strcpy(im->name, RTA_DATA(tb[IFLA_IFNAME]));
84 const char *ll_idx_n2a(int idx, char *buf)
90 for (im = idxmap[idx & 0xF]; im; im = im->next)
93 snprintf(buf, 16, "if%d", idx);
98 const char *ll_index_to_name(int idx)
100 static char nbuf[16];
102 return ll_idx_n2a(idx, nbuf);
105 int ll_index_to_type(int idx)
111 for (im = idxmap[idx & 0xF]; im; im = im->next)
112 if (im->index == idx)
117 unsigned ll_index_to_flags(int idx)
124 for (im = idxmap[idx & 0xF]; im; im = im->next)
125 if (im->index == idx)
130 // TODO: caching is not warranted - no users which repeatedly call it
131 int ll_name_to_index(char *name)
133 static char ncache[16];
142 if (icache && strcmp(name, ncache) == 0)
144 for (i = 0; i < 16; i++) {
145 for (im = idxmap[i]; im; im = im->next) {
146 if (strcmp(im->name, name) == 0) {
148 strcpy(ncache, name);
153 /* We have not found the interface in our cache, but the kernel
154 * may still know about it. One reason is that we may be using
155 * module on-demand loading, which means that the kernel will
156 * load the module and make the interface exist only when
157 * we explicitely request it (check for dev_load() in net/core/dev.c).
158 * I can think of other similar scenario, but they are less common...
160 sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
164 strncpy(ifr.ifr_name, name, IFNAMSIZ);
165 ifr.ifr_ifindex = -1;
166 ret = ioctl(sock_fd, SIOCGIFINDEX, &ifr);
169 /* In theory, we should redump the interface list
170 * to update our cache, this is left as an exercise
171 * to the reader... Jean II */
172 return ifr.ifr_ifindex;
178 int ll_init_map(struct rtnl_handle *rth)
180 if (rtnl_wilddump_request(rth, AF_UNSPEC, RTM_GETLINK) < 0) {
181 bb_perror_msg_and_die("cannot send dump request");
184 if (rtnl_dump_filter(rth, ll_remember_index, &idxmap, NULL, NULL) < 0) {
185 bb_error_msg_and_die("dump terminated");