2006-07-02 19:47:05 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
2002-11-10 01:33:55 +00:00
|
|
|
/*
|
|
|
|
* ll_map.c
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version
|
|
|
|
* 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2007-05-31 22:42:12 +00:00
|
|
|
#include <net/if.h> /* struct ifreq and co. */
|
2002-11-10 01:33:55 +00:00
|
|
|
|
2007-05-31 22:42:12 +00:00
|
|
|
#include "libbb.h"
|
2002-11-10 01:33:55 +00:00
|
|
|
#include "libnetlink.h"
|
2005-10-08 20:47:15 +00:00
|
|
|
#include "ll_map.h"
|
2002-11-10 01:33:55 +00:00
|
|
|
|
2006-09-30 00:18:16 +00:00
|
|
|
struct idxmap {
|
2007-06-19 12:11:20 +00:00
|
|
|
struct idxmap *next;
|
|
|
|
int index;
|
|
|
|
int type;
|
|
|
|
int alen;
|
|
|
|
unsigned flags;
|
|
|
|
unsigned char addr[8];
|
|
|
|
char name[16];
|
2002-11-10 01:33:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct idxmap *idxmap[16];
|
|
|
|
|
2007-06-19 12:11:20 +00:00
|
|
|
static struct idxmap *find_by_index(int idx)
|
|
|
|
{
|
|
|
|
struct idxmap *im;
|
|
|
|
|
|
|
|
for (im = idxmap[idx & 0xF]; im; im = im->next)
|
|
|
|
if (im->index == idx)
|
|
|
|
return im;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-03-17 09:00:54 +00:00
|
|
|
int ll_remember_index(struct sockaddr_nl *who ATTRIBUTE_UNUSED,
|
|
|
|
struct nlmsghdr *n,
|
|
|
|
void *arg ATTRIBUTE_UNUSED)
|
2002-11-10 01:33:55 +00:00
|
|
|
{
|
|
|
|
int h;
|
|
|
|
struct ifinfomsg *ifi = NLMSG_DATA(n);
|
|
|
|
struct idxmap *im, **imp;
|
|
|
|
struct rtattr *tb[IFLA_MAX+1];
|
|
|
|
|
|
|
|
if (n->nlmsg_type != RTM_NEWLINK)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifi)))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
memset(tb, 0, sizeof(tb));
|
|
|
|
parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), IFLA_PAYLOAD(n));
|
|
|
|
if (tb[IFLA_IFNAME] == NULL)
|
|
|
|
return 0;
|
|
|
|
|
2007-06-19 12:11:20 +00:00
|
|
|
h = ifi->ifi_index & 0xF;
|
2002-11-10 01:33:55 +00:00
|
|
|
|
2007-04-07 01:14:45 +00:00
|
|
|
for (imp = &idxmap[h]; (im = *imp) != NULL; imp = &im->next)
|
2002-11-10 01:33:55 +00:00
|
|
|
if (im->index == ifi->ifi_index)
|
2007-06-19 12:11:20 +00:00
|
|
|
goto found;
|
2002-11-10 01:33:55 +00:00
|
|
|
|
2007-06-19 12:11:20 +00:00
|
|
|
im = xmalloc(sizeof(*im));
|
|
|
|
im->next = *imp;
|
|
|
|
im->index = ifi->ifi_index;
|
|
|
|
*imp = im;
|
|
|
|
found:
|
2002-11-10 01:33:55 +00:00
|
|
|
im->type = ifi->ifi_type;
|
|
|
|
im->flags = ifi->ifi_flags;
|
|
|
|
if (tb[IFLA_ADDRESS]) {
|
|
|
|
int alen;
|
|
|
|
im->alen = alen = RTA_PAYLOAD(tb[IFLA_ADDRESS]);
|
|
|
|
if (alen > sizeof(im->addr))
|
|
|
|
alen = sizeof(im->addr);
|
|
|
|
memcpy(im->addr, RTA_DATA(tb[IFLA_ADDRESS]), alen);
|
|
|
|
} else {
|
|
|
|
im->alen = 0;
|
|
|
|
memset(im->addr, 0, sizeof(im->addr));
|
|
|
|
}
|
|
|
|
strcpy(im->name, RTA_DATA(tb[IFLA_IFNAME]));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *ll_idx_n2a(int idx, char *buf)
|
|
|
|
{
|
|
|
|
struct idxmap *im;
|
|
|
|
|
|
|
|
if (idx == 0)
|
|
|
|
return "*";
|
2007-06-19 12:11:20 +00:00
|
|
|
im = find_by_index(idx);
|
|
|
|
if (im)
|
|
|
|
return im->name;
|
2002-11-10 01:33:55 +00:00
|
|
|
snprintf(buf, 16, "if%d", idx);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const char *ll_index_to_name(int idx)
|
|
|
|
{
|
|
|
|
static char nbuf[16];
|
|
|
|
|
|
|
|
return ll_idx_n2a(idx, nbuf);
|
|
|
|
}
|
|
|
|
|
2007-06-19 12:11:20 +00:00
|
|
|
#ifdef UNUSED
|
2002-11-10 01:33:55 +00:00
|
|
|
int ll_index_to_type(int idx)
|
|
|
|
{
|
|
|
|
struct idxmap *im;
|
|
|
|
|
|
|
|
if (idx == 0)
|
|
|
|
return -1;
|
2007-06-19 12:11:20 +00:00
|
|
|
im = find_by_index(idx);
|
|
|
|
if (im)
|
|
|
|
return im->type;
|
2002-11-10 01:33:55 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2007-06-19 12:11:20 +00:00
|
|
|
#endif
|
2002-11-10 01:33:55 +00:00
|
|
|
|
|
|
|
unsigned ll_index_to_flags(int idx)
|
|
|
|
{
|
|
|
|
struct idxmap *im;
|
|
|
|
|
|
|
|
if (idx == 0)
|
|
|
|
return 0;
|
2007-06-19 12:11:20 +00:00
|
|
|
im = find_by_index(idx);
|
|
|
|
if (im)
|
|
|
|
return im->flags;
|
2002-11-10 01:33:55 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-08-12 20:58:27 +00:00
|
|
|
int xll_name_to_index(const char *const name)
|
2002-11-10 01:33:55 +00:00
|
|
|
{
|
2007-06-19 12:11:20 +00:00
|
|
|
int ret = 0;
|
|
|
|
int sock_fd;
|
|
|
|
|
|
|
|
/* caching is not warranted - no users which repeatedly call it */
|
|
|
|
#ifdef UNUSED
|
2002-11-10 01:33:55 +00:00
|
|
|
static char ncache[16];
|
|
|
|
static int icache;
|
2007-04-07 01:14:45 +00:00
|
|
|
|
2002-11-10 01:33:55 +00:00
|
|
|
struct idxmap *im;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (name == NULL)
|
2007-04-12 11:34:39 +00:00
|
|
|
goto out;
|
|
|
|
if (icache && strcmp(name, ncache) == 0) {
|
|
|
|
ret = icache;
|
|
|
|
goto out;
|
|
|
|
}
|
2007-04-07 01:14:45 +00:00
|
|
|
for (i = 0; i < 16; i++) {
|
2002-11-10 01:33:55 +00:00
|
|
|
for (im = idxmap[i]; im; im = im->next) {
|
|
|
|
if (strcmp(im->name, name) == 0) {
|
|
|
|
icache = im->index;
|
|
|
|
strcpy(ncache, name);
|
2007-04-12 11:34:39 +00:00
|
|
|
ret = im->index;
|
|
|
|
goto out;
|
2002-11-10 01:33:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-09-30 00:18:16 +00:00
|
|
|
/* We have not found the interface in our cache, but the kernel
|
|
|
|
* may still know about it. One reason is that we may be using
|
|
|
|
* module on-demand loading, which means that the kernel will
|
|
|
|
* load the module and make the interface exist only when
|
|
|
|
* we explicitely request it (check for dev_load() in net/core/dev.c).
|
|
|
|
* I can think of other similar scenario, but they are less common...
|
|
|
|
* Jean II */
|
2007-06-19 12:11:20 +00:00
|
|
|
#endif
|
|
|
|
|
2006-09-30 00:18:16 +00:00
|
|
|
sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (sock_fd) {
|
|
|
|
struct ifreq ifr;
|
2007-04-12 11:34:39 +00:00
|
|
|
int tmp;
|
2007-06-19 12:11:20 +00:00
|
|
|
|
2006-09-30 00:18:16 +00:00
|
|
|
strncpy(ifr.ifr_name, name, IFNAMSIZ);
|
|
|
|
ifr.ifr_ifindex = -1;
|
2007-04-12 11:34:39 +00:00
|
|
|
tmp = ioctl(sock_fd, SIOCGIFINDEX, &ifr);
|
2006-09-30 00:18:16 +00:00
|
|
|
close(sock_fd);
|
2007-04-12 11:34:39 +00:00
|
|
|
if (tmp >= 0)
|
2006-09-30 00:18:16 +00:00
|
|
|
/* In theory, we should redump the interface list
|
|
|
|
* to update our cache, this is left as an exercise
|
|
|
|
* to the reader... Jean II */
|
2007-04-12 11:34:39 +00:00
|
|
|
ret = ifr.ifr_ifindex;
|
2006-09-30 00:18:16 +00:00
|
|
|
}
|
2007-06-19 12:11:20 +00:00
|
|
|
/* out:*/
|
2007-04-12 11:34:39 +00:00
|
|
|
if (ret <= 0)
|
|
|
|
bb_error_msg_and_die("cannot find device \"%s\"", name);
|
|
|
|
return ret;
|
2002-11-10 01:33:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int ll_init_map(struct rtnl_handle *rth)
|
|
|
|
{
|
2007-04-12 11:34:39 +00:00
|
|
|
xrtnl_wilddump_request(rth, AF_UNSPEC, RTM_GETLINK);
|
|
|
|
xrtnl_dump_filter(rth, ll_remember_index, &idxmap);
|
2002-11-10 01:33:55 +00:00
|
|
|
return 0;
|
|
|
|
}
|