blob: 66401da77fac47ac8172e7bc5a6ca871856c00e7 [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 */
Denys Vlasenkofb132e42010-10-29 11:46:52 +020010#include <net/if.h> /* struct ifreq and co. */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000011
Denis Vlasenko9a7d38f2007-05-31 22:42:12 +000012#include "libbb.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000013#include "libnetlink.h"
Bernhard Reutner-Fischer1d62d3b2005-10-08 20:47:15 +000014#include "ll_map.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000015
Denis Vlasenko7935a5a2006-09-30 00:18:16 +000016struct idxmap {
Denis Vlasenko08a61182007-06-19 12:11:20 +000017 struct idxmap *next;
18 int index;
19 int type;
20 int alen;
21 unsigned flags;
22 unsigned char addr[8];
23 char name[16];
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000024};
25
Denys Vlasenko94466b82009-10-13 16:27:11 +020026static struct idxmap **idxmap; /* treat as *idxmap[16] */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000027
Denis Vlasenko08a61182007-06-19 12:11:20 +000028static struct idxmap *find_by_index(int idx)
29{
30 struct idxmap *im;
31
Denys Vlasenko94466b82009-10-13 16:27:11 +020032 if (idxmap)
33 for (im = idxmap[idx & 0xF]; im; im = im->next)
34 if (im->index == idx)
35 return im;
Denis Vlasenko08a61182007-06-19 12:11:20 +000036 return NULL;
37}
38
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +020039int FAST_FUNC ll_remember_index(const struct sockaddr_nl *who UNUSED_PARAM,
Denis Vlasenko68404f12008-03-17 09:00:54 +000040 struct nlmsghdr *n,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000041 void *arg UNUSED_PARAM)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000042{
43 int h;
44 struct ifinfomsg *ifi = NLMSG_DATA(n);
45 struct idxmap *im, **imp;
46 struct rtattr *tb[IFLA_MAX+1];
47
48 if (n->nlmsg_type != RTM_NEWLINK)
49 return 0;
50
51 if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifi)))
52 return -1;
53
Denys Vlasenko68ae5422018-02-08 08:42:37 +010054 //memset(tb, 0, sizeof(tb)); - parse_rtattr does this
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000055 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), IFLA_PAYLOAD(n));
56 if (tb[IFLA_IFNAME] == NULL)
57 return 0;
58
Denys Vlasenko94466b82009-10-13 16:27:11 +020059 if (!idxmap)
60 idxmap = xzalloc(sizeof(idxmap[0]) * 16);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000061
Denys Vlasenko94466b82009-10-13 16:27:11 +020062 h = ifi->ifi_index & 0xF;
Denis Vlasenko540a2a12007-04-07 01:14:45 +000063 for (imp = &idxmap[h]; (im = *imp) != NULL; imp = &im->next)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000064 if (im->index == ifi->ifi_index)
Denis Vlasenko08a61182007-06-19 12:11:20 +000065 goto found;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000066
Denis Vlasenko08a61182007-06-19 12:11:20 +000067 im = xmalloc(sizeof(*im));
68 im->next = *imp;
69 im->index = ifi->ifi_index;
70 *imp = im;
71 found:
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000072 im->type = ifi->ifi_type;
73 im->flags = ifi->ifi_flags;
74 if (tb[IFLA_ADDRESS]) {
75 int alen;
76 im->alen = alen = RTA_PAYLOAD(tb[IFLA_ADDRESS]);
Denis Vlasenko6b06cb82008-05-15 21:30:45 +000077 if (alen > (int)sizeof(im->addr))
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000078 alen = sizeof(im->addr);
79 memcpy(im->addr, RTA_DATA(tb[IFLA_ADDRESS]), alen);
80 } else {
81 im->alen = 0;
82 memset(im->addr, 0, sizeof(im->addr));
83 }
84 strcpy(im->name, RTA_DATA(tb[IFLA_IFNAME]));
85 return 0;
86}
87
Denys Vlasenkoe52da552015-10-09 17:59:56 +020088static
89const char FAST_FUNC *ll_idx_n2a(int idx/*, char *buf*/)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000090{
91 struct idxmap *im;
92
93 if (idx == 0)
94 return "*";
Denis Vlasenko08a61182007-06-19 12:11:20 +000095 im = find_by_index(idx);
96 if (im)
97 return im->name;
Denys Vlasenkoe52da552015-10-09 17:59:56 +020098 //snprintf(buf, 16, "if%d", idx);
99 //return buf;
100 return auto_string(xasprintf("if%d", idx));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000101}
102
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200103const char FAST_FUNC *ll_index_to_name(int idx)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000104{
Denys Vlasenkoe52da552015-10-09 17:59:56 +0200105 //static char nbuf[16];
106 return ll_idx_n2a(idx/*, nbuf*/);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000107}
108
Denis Vlasenko08a61182007-06-19 12:11:20 +0000109#ifdef UNUSED
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000110int ll_index_to_type(int idx)
111{
112 struct idxmap *im;
113
114 if (idx == 0)
115 return -1;
Denis Vlasenko08a61182007-06-19 12:11:20 +0000116 im = find_by_index(idx);
117 if (im)
118 return im->type;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000119 return -1;
120}
Denis Vlasenko08a61182007-06-19 12:11:20 +0000121#endif
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000122
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200123unsigned FAST_FUNC ll_index_to_flags(int idx)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000124{
125 struct idxmap *im;
126
127 if (idx == 0)
128 return 0;
Denis Vlasenko08a61182007-06-19 12:11:20 +0000129 im = find_by_index(idx);
130 if (im)
131 return im->flags;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000132 return 0;
133}
134
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200135int FAST_FUNC xll_name_to_index(const char *name)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000136{
Denis Vlasenko08a61182007-06-19 12:11:20 +0000137 int ret = 0;
Denis Vlasenko08a61182007-06-19 12:11:20 +0000138
139/* caching is not warranted - no users which repeatedly call it */
140#ifdef UNUSED
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000141 static char ncache[16];
142 static int icache;
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000143
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000144 struct idxmap *im;
145 int i;
146
147 if (name == NULL)
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000148 goto out;
149 if (icache && strcmp(name, ncache) == 0) {
150 ret = icache;
151 goto out;
152 }
Denys Vlasenko94466b82009-10-13 16:27:11 +0200153 if (idxmap) {
154 for (i = 0; i < 16; i++) {
155 for (im = idxmap[i]; im; im = im->next) {
156 if (strcmp(im->name, name) == 0) {
157 icache = im->index;
158 strcpy(ncache, name);
159 ret = im->index;
160 goto out;
161 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000162 }
163 }
164 }
Denis Vlasenko08a61182007-06-19 12:11:20 +0000165#endif
Ron Yorston88144312015-10-21 16:57:25 +0100166 ret = if_nametoindex(name);
Denis Vlasenko08a61182007-06-19 12:11:20 +0000167/* out:*/
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000168 if (ret <= 0)
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100169 bb_error_msg_and_die("can't find device '%s'", name);
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000170 return ret;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000171}
172
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200173int FAST_FUNC ll_init_map(struct rtnl_handle *rth)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000174{
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000175 xrtnl_wilddump_request(rth, AF_UNSPEC, RTM_GETLINK);
Denys Vlasenko94466b82009-10-13 16:27:11 +0200176 xrtnl_dump_filter(rth, ll_remember_index, NULL);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000177 return 0;
178}