2006-07-03 01:17:05 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
2002-11-10 07:03:55 +05:30
|
|
|
/*
|
2010-10-29 15:16:52 +05:30
|
|
|
* 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.
|
2002-11-10 07:03:55 +05:30
|
|
|
*
|
2010-10-29 15:16:52 +05:30
|
|
|
* Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
|
2002-11-10 07:03:55 +05:30
|
|
|
*/
|
2010-10-29 15:16:52 +05:30
|
|
|
#include <net/if.h> /* struct ifreq and co. */
|
2002-11-10 07:03:55 +05:30
|
|
|
|
2007-06-01 04:12:12 +05:30
|
|
|
#include "libbb.h"
|
2002-11-10 07:03:55 +05:30
|
|
|
#include "libnetlink.h"
|
2005-10-09 02:17:15 +05:30
|
|
|
#include "ll_map.h"
|
2002-11-10 07:03:55 +05:30
|
|
|
|
2006-09-30 05:48:16 +05:30
|
|
|
struct idxmap {
|
2007-06-19 17:41:20 +05:30
|
|
|
struct idxmap *next;
|
|
|
|
int index;
|
|
|
|
int type;
|
|
|
|
int alen;
|
|
|
|
unsigned flags;
|
|
|
|
unsigned char addr[8];
|
|
|
|
char name[16];
|
2002-11-10 07:03:55 +05:30
|
|
|
};
|
|
|
|
|
2009-10-13 19:57:11 +05:30
|
|
|
static struct idxmap **idxmap; /* treat as *idxmap[16] */
|
2002-11-10 07:03:55 +05:30
|
|
|
|
2007-06-19 17:41:20 +05:30
|
|
|
static struct idxmap *find_by_index(int idx)
|
|
|
|
{
|
|
|
|
struct idxmap *im;
|
|
|
|
|
2009-10-13 19:57:11 +05:30
|
|
|
if (idxmap)
|
|
|
|
for (im = idxmap[idx & 0xF]; im; im = im->next)
|
|
|
|
if (im->index == idx)
|
|
|
|
return im;
|
2007-06-19 17:41:20 +05:30
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-06-05 15:36:05 +05:30
|
|
|
int FAST_FUNC ll_remember_index(const struct sockaddr_nl *who UNUSED_PARAM,
|
2008-03-17 14:30:54 +05:30
|
|
|
struct nlmsghdr *n,
|
2008-07-05 14:48:54 +05:30
|
|
|
void *arg UNUSED_PARAM)
|
2002-11-10 07:03:55 +05:30
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2018-02-08 13:12:37 +05:30
|
|
|
//memset(tb, 0, sizeof(tb)); - parse_rtattr does this
|
2002-11-10 07:03:55 +05:30
|
|
|
parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), IFLA_PAYLOAD(n));
|
|
|
|
if (tb[IFLA_IFNAME] == NULL)
|
|
|
|
return 0;
|
|
|
|
|
2009-10-13 19:57:11 +05:30
|
|
|
if (!idxmap)
|
|
|
|
idxmap = xzalloc(sizeof(idxmap[0]) * 16);
|
2002-11-10 07:03:55 +05:30
|
|
|
|
2009-10-13 19:57:11 +05:30
|
|
|
h = ifi->ifi_index & 0xF;
|
2007-04-07 06:44:45 +05:30
|
|
|
for (imp = &idxmap[h]; (im = *imp) != NULL; imp = &im->next)
|
2002-11-10 07:03:55 +05:30
|
|
|
if (im->index == ifi->ifi_index)
|
2007-06-19 17:41:20 +05:30
|
|
|
goto found;
|
2002-11-10 07:03:55 +05:30
|
|
|
|
2007-06-19 17:41:20 +05:30
|
|
|
im = xmalloc(sizeof(*im));
|
|
|
|
im->next = *imp;
|
|
|
|
im->index = ifi->ifi_index;
|
|
|
|
*imp = im;
|
|
|
|
found:
|
2002-11-10 07:03:55 +05:30
|
|
|
im->type = ifi->ifi_type;
|
|
|
|
im->flags = ifi->ifi_flags;
|
|
|
|
if (tb[IFLA_ADDRESS]) {
|
|
|
|
int alen;
|
|
|
|
im->alen = alen = RTA_PAYLOAD(tb[IFLA_ADDRESS]);
|
2008-05-16 03:00:45 +05:30
|
|
|
if (alen > (int)sizeof(im->addr))
|
2002-11-10 07:03:55 +05:30
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-10-09 21:29:56 +05:30
|
|
|
static
|
|
|
|
const char FAST_FUNC *ll_idx_n2a(int idx/*, char *buf*/)
|
2002-11-10 07:03:55 +05:30
|
|
|
{
|
|
|
|
struct idxmap *im;
|
|
|
|
|
|
|
|
if (idx == 0)
|
|
|
|
return "*";
|
2007-06-19 17:41:20 +05:30
|
|
|
im = find_by_index(idx);
|
|
|
|
if (im)
|
|
|
|
return im->name;
|
2015-10-09 21:29:56 +05:30
|
|
|
//snprintf(buf, 16, "if%d", idx);
|
|
|
|
//return buf;
|
|
|
|
return auto_string(xasprintf("if%d", idx));
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
|
2009-06-05 15:36:05 +05:30
|
|
|
const char FAST_FUNC *ll_index_to_name(int idx)
|
2002-11-10 07:03:55 +05:30
|
|
|
{
|
2015-10-09 21:29:56 +05:30
|
|
|
//static char nbuf[16];
|
|
|
|
return ll_idx_n2a(idx/*, nbuf*/);
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
|
2007-06-19 17:41:20 +05:30
|
|
|
#ifdef UNUSED
|
2002-11-10 07:03:55 +05:30
|
|
|
int ll_index_to_type(int idx)
|
|
|
|
{
|
|
|
|
struct idxmap *im;
|
|
|
|
|
|
|
|
if (idx == 0)
|
|
|
|
return -1;
|
2007-06-19 17:41:20 +05:30
|
|
|
im = find_by_index(idx);
|
|
|
|
if (im)
|
|
|
|
return im->type;
|
2002-11-10 07:03:55 +05:30
|
|
|
return -1;
|
|
|
|
}
|
2007-06-19 17:41:20 +05:30
|
|
|
#endif
|
2002-11-10 07:03:55 +05:30
|
|
|
|
2009-06-05 15:36:05 +05:30
|
|
|
unsigned FAST_FUNC ll_index_to_flags(int idx)
|
2002-11-10 07:03:55 +05:30
|
|
|
{
|
|
|
|
struct idxmap *im;
|
|
|
|
|
|
|
|
if (idx == 0)
|
|
|
|
return 0;
|
2007-06-19 17:41:20 +05:30
|
|
|
im = find_by_index(idx);
|
|
|
|
if (im)
|
|
|
|
return im->flags;
|
2002-11-10 07:03:55 +05:30
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-06-05 15:36:05 +05:30
|
|
|
int FAST_FUNC xll_name_to_index(const char *name)
|
2002-11-10 07:03:55 +05:30
|
|
|
{
|
2007-06-19 17:41:20 +05:30
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
/* caching is not warranted - no users which repeatedly call it */
|
|
|
|
#ifdef UNUSED
|
2002-11-10 07:03:55 +05:30
|
|
|
static char ncache[16];
|
|
|
|
static int icache;
|
2007-04-07 06:44:45 +05:30
|
|
|
|
2002-11-10 07:03:55 +05:30
|
|
|
struct idxmap *im;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (name == NULL)
|
2007-04-12 17:04:39 +05:30
|
|
|
goto out;
|
|
|
|
if (icache && strcmp(name, ncache) == 0) {
|
|
|
|
ret = icache;
|
|
|
|
goto out;
|
|
|
|
}
|
2009-10-13 19:57:11 +05:30
|
|
|
if (idxmap) {
|
|
|
|
for (i = 0; i < 16; i++) {
|
|
|
|
for (im = idxmap[i]; im; im = im->next) {
|
|
|
|
if (strcmp(im->name, name) == 0) {
|
|
|
|
icache = im->index;
|
|
|
|
strcpy(ncache, name);
|
|
|
|
ret = im->index;
|
|
|
|
goto out;
|
|
|
|
}
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-06-19 17:41:20 +05:30
|
|
|
#endif
|
2015-10-21 21:27:25 +05:30
|
|
|
ret = if_nametoindex(name);
|
2007-06-19 17:41:20 +05:30
|
|
|
/* out:*/
|
2007-04-12 17:04:39 +05:30
|
|
|
if (ret <= 0)
|
2009-11-13 13:38:27 +05:30
|
|
|
bb_error_msg_and_die("can't find device '%s'", name);
|
2007-04-12 17:04:39 +05:30
|
|
|
return ret;
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
|
2009-06-05 15:36:05 +05:30
|
|
|
int FAST_FUNC ll_init_map(struct rtnl_handle *rth)
|
2002-11-10 07:03:55 +05:30
|
|
|
{
|
2007-04-12 17:04:39 +05:30
|
|
|
xrtnl_wilddump_request(rth, AF_UNSPEC, RTM_GETLINK);
|
2009-10-13 19:57:11 +05:30
|
|
|
xrtnl_dump_filter(rth, ll_remember_index, NULL);
|
2002-11-10 07:03:55 +05:30
|
|
|
return 0;
|
|
|
|
}
|