blob: af9eb46f7e542d704fda89cc8a7a31e51b8e7bab [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00002/*
Denys Vlasenkofb132e42010-10-29 11:46:52 +02003 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU General Public License
5 * as published by the Free Software Foundation; either version
6 * 2 of the License, or (at your option) any later version.
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00007 *
Denys Vlasenkofb132e42010-10-29 11:46:52 +02008 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00009 */
10
Denys Vlasenkofb132e42010-10-29 11:46:52 +020011#include <net/if.h> /* struct ifreq and co. */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000012
Denis Vlasenko9a7d38f2007-05-31 22:42:12 +000013#include "libbb.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000014#include "libnetlink.h"
Bernhard Reutner-Fischer1d62d3b2005-10-08 20:47:15 +000015#include "ll_map.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000016
Denis Vlasenko7935a5a2006-09-30 00:18:16 +000017struct idxmap {
Denis Vlasenko08a61182007-06-19 12:11:20 +000018 struct idxmap *next;
19 int index;
20 int type;
21 int alen;
22 unsigned flags;
23 unsigned char addr[8];
24 char name[16];
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000025};
26
Denys Vlasenko94466b82009-10-13 16:27:11 +020027static struct idxmap **idxmap; /* treat as *idxmap[16] */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000028
Denis Vlasenko08a61182007-06-19 12:11:20 +000029static struct idxmap *find_by_index(int idx)
30{
31 struct idxmap *im;
32
Denys Vlasenko94466b82009-10-13 16:27:11 +020033 if (idxmap)
34 for (im = idxmap[idx & 0xF]; im; im = im->next)
35 if (im->index == idx)
36 return im;
Denis Vlasenko08a61182007-06-19 12:11:20 +000037 return NULL;
38}
39
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +020040int FAST_FUNC ll_remember_index(const struct sockaddr_nl *who UNUSED_PARAM,
Denis Vlasenko68404f12008-03-17 09:00:54 +000041 struct nlmsghdr *n,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000042 void *arg UNUSED_PARAM)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000043{
44 int h;
45 struct ifinfomsg *ifi = NLMSG_DATA(n);
46 struct idxmap *im, **imp;
47 struct rtattr *tb[IFLA_MAX+1];
48
49 if (n->nlmsg_type != RTM_NEWLINK)
50 return 0;
51
52 if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifi)))
53 return -1;
54
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000055 memset(tb, 0, sizeof(tb));
56 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), IFLA_PAYLOAD(n));
57 if (tb[IFLA_IFNAME] == NULL)
58 return 0;
59
Denys Vlasenko94466b82009-10-13 16:27:11 +020060 if (!idxmap)
61 idxmap = xzalloc(sizeof(idxmap[0]) * 16);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000062
Denys Vlasenko94466b82009-10-13 16:27:11 +020063 h = ifi->ifi_index & 0xF;
Denis Vlasenko540a2a12007-04-07 01:14:45 +000064 for (imp = &idxmap[h]; (im = *imp) != NULL; imp = &im->next)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000065 if (im->index == ifi->ifi_index)
Denis Vlasenko08a61182007-06-19 12:11:20 +000066 goto found;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000067
Denis Vlasenko08a61182007-06-19 12:11:20 +000068 im = xmalloc(sizeof(*im));
69 im->next = *imp;
70 im->index = ifi->ifi_index;
71 *imp = im;
72 found:
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000073 im->type = ifi->ifi_type;
74 im->flags = ifi->ifi_flags;
75 if (tb[IFLA_ADDRESS]) {
76 int alen;
77 im->alen = alen = RTA_PAYLOAD(tb[IFLA_ADDRESS]);
Denis Vlasenko6b06cb82008-05-15 21:30:45 +000078 if (alen > (int)sizeof(im->addr))
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000079 alen = sizeof(im->addr);
80 memcpy(im->addr, RTA_DATA(tb[IFLA_ADDRESS]), alen);
81 } else {
82 im->alen = 0;
83 memset(im->addr, 0, sizeof(im->addr));
84 }
85 strcpy(im->name, RTA_DATA(tb[IFLA_IFNAME]));
86 return 0;
87}
88
Denys Vlasenkoe52da552015-10-09 17:59:56 +020089static
90const char FAST_FUNC *ll_idx_n2a(int idx/*, char *buf*/)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000091{
92 struct idxmap *im;
93
94 if (idx == 0)
95 return "*";
Denis Vlasenko08a61182007-06-19 12:11:20 +000096 im = find_by_index(idx);
97 if (im)
98 return im->name;
Denys Vlasenkoe52da552015-10-09 17:59:56 +020099 //snprintf(buf, 16, "if%d", idx);
100 //return buf;
101 return auto_string(xasprintf("if%d", idx));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000102}
103
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200104const char FAST_FUNC *ll_index_to_name(int idx)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000105{
Denys Vlasenkoe52da552015-10-09 17:59:56 +0200106 //static char nbuf[16];
107 return ll_idx_n2a(idx/*, nbuf*/);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000108}
109
Denis Vlasenko08a61182007-06-19 12:11:20 +0000110#ifdef UNUSED
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000111int ll_index_to_type(int idx)
112{
113 struct idxmap *im;
114
115 if (idx == 0)
116 return -1;
Denis Vlasenko08a61182007-06-19 12:11:20 +0000117 im = find_by_index(idx);
118 if (im)
119 return im->type;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000120 return -1;
121}
Denis Vlasenko08a61182007-06-19 12:11:20 +0000122#endif
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000123
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200124unsigned FAST_FUNC ll_index_to_flags(int idx)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000125{
126 struct idxmap *im;
127
128 if (idx == 0)
129 return 0;
Denis Vlasenko08a61182007-06-19 12:11:20 +0000130 im = find_by_index(idx);
131 if (im)
132 return im->flags;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000133 return 0;
134}
135
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200136int FAST_FUNC xll_name_to_index(const char *name)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000137{
Denis Vlasenko08a61182007-06-19 12:11:20 +0000138 int ret = 0;
Denis Vlasenko08a61182007-06-19 12:11:20 +0000139
140/* caching is not warranted - no users which repeatedly call it */
141#ifdef UNUSED
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000142 static char ncache[16];
143 static int icache;
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000144
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000145 struct idxmap *im;
146 int i;
147
148 if (name == NULL)
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000149 goto out;
150 if (icache && strcmp(name, ncache) == 0) {
151 ret = icache;
152 goto out;
153 }
Denys Vlasenko94466b82009-10-13 16:27:11 +0200154 if (idxmap) {
155 for (i = 0; i < 16; i++) {
156 for (im = idxmap[i]; im; im = im->next) {
157 if (strcmp(im->name, name) == 0) {
158 icache = im->index;
159 strcpy(ncache, name);
160 ret = im->index;
161 goto out;
162 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000163 }
164 }
165 }
Denis Vlasenko08a61182007-06-19 12:11:20 +0000166#endif
Ron Yorston88144312015-10-21 16:57:25 +0100167 ret = if_nametoindex(name);
Denis Vlasenko08a61182007-06-19 12:11:20 +0000168/* out:*/
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000169 if (ret <= 0)
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100170 bb_error_msg_and_die("can't find device '%s'", name);
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000171 return ret;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000172}
173
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200174int FAST_FUNC ll_init_map(struct rtnl_handle *rth)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000175{
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000176 xrtnl_wilddump_request(rth, AF_UNSPEC, RTM_GETLINK);
Denys Vlasenko94466b82009-10-13 16:27:11 +0200177 xrtnl_dump_filter(rth, ll_remember_index, NULL);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000178 return 0;
179}