blob: 3c4ef2c321d47f9064e60b3dff0cd6a5a38ffb06 [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/*
3 * ll_map.c
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.
9 *
10 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
11 *
12 */
13
Denis Vlasenko9a7d38f2007-05-31 22:42:12 +000014#include <net/if.h> /* struct ifreq and co. */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000015
Denis Vlasenko9a7d38f2007-05-31 22:42:12 +000016#include "libbb.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000017#include "libnetlink.h"
Bernhard Reutner-Fischer1d62d3b2005-10-08 20:47:15 +000018#include "ll_map.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000019
Denis Vlasenko7935a5a2006-09-30 00:18:16 +000020struct idxmap {
Denis Vlasenko08a61182007-06-19 12:11:20 +000021 struct idxmap *next;
22 int index;
23 int type;
24 int alen;
25 unsigned flags;
26 unsigned char addr[8];
27 char name[16];
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000028};
29
Denys Vlasenko94466b82009-10-13 16:27:11 +020030static struct idxmap **idxmap; /* treat as *idxmap[16] */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000031
Denis Vlasenko08a61182007-06-19 12:11:20 +000032static struct idxmap *find_by_index(int idx)
33{
34 struct idxmap *im;
35
Denys Vlasenko94466b82009-10-13 16:27:11 +020036 if (idxmap)
37 for (im = idxmap[idx & 0xF]; im; im = im->next)
38 if (im->index == idx)
39 return im;
Denis Vlasenko08a61182007-06-19 12:11:20 +000040 return NULL;
41}
42
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +020043int FAST_FUNC ll_remember_index(const struct sockaddr_nl *who UNUSED_PARAM,
Denis Vlasenko68404f12008-03-17 09:00:54 +000044 struct nlmsghdr *n,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000045 void *arg UNUSED_PARAM)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000046{
47 int h;
48 struct ifinfomsg *ifi = NLMSG_DATA(n);
49 struct idxmap *im, **imp;
50 struct rtattr *tb[IFLA_MAX+1];
51
52 if (n->nlmsg_type != RTM_NEWLINK)
53 return 0;
54
55 if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifi)))
56 return -1;
57
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000058 memset(tb, 0, sizeof(tb));
59 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), IFLA_PAYLOAD(n));
60 if (tb[IFLA_IFNAME] == NULL)
61 return 0;
62
Denys Vlasenko94466b82009-10-13 16:27:11 +020063 if (!idxmap)
64 idxmap = xzalloc(sizeof(idxmap[0]) * 16);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000065
Denys Vlasenko94466b82009-10-13 16:27:11 +020066 h = ifi->ifi_index & 0xF;
Denis Vlasenko540a2a12007-04-07 01:14:45 +000067 for (imp = &idxmap[h]; (im = *imp) != NULL; imp = &im->next)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000068 if (im->index == ifi->ifi_index)
Denis Vlasenko08a61182007-06-19 12:11:20 +000069 goto found;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000070
Denis Vlasenko08a61182007-06-19 12:11:20 +000071 im = xmalloc(sizeof(*im));
72 im->next = *imp;
73 im->index = ifi->ifi_index;
74 *imp = im;
75 found:
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000076 im->type = ifi->ifi_type;
77 im->flags = ifi->ifi_flags;
78 if (tb[IFLA_ADDRESS]) {
79 int alen;
80 im->alen = alen = RTA_PAYLOAD(tb[IFLA_ADDRESS]);
Denis Vlasenko6b06cb82008-05-15 21:30:45 +000081 if (alen > (int)sizeof(im->addr))
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000082 alen = sizeof(im->addr);
83 memcpy(im->addr, RTA_DATA(tb[IFLA_ADDRESS]), alen);
84 } else {
85 im->alen = 0;
86 memset(im->addr, 0, sizeof(im->addr));
87 }
88 strcpy(im->name, RTA_DATA(tb[IFLA_IFNAME]));
89 return 0;
90}
91
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +020092const char FAST_FUNC *ll_idx_n2a(int idx, char *buf)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000093{
94 struct idxmap *im;
95
96 if (idx == 0)
97 return "*";
Denis Vlasenko08a61182007-06-19 12:11:20 +000098 im = find_by_index(idx);
99 if (im)
100 return im->name;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000101 snprintf(buf, 16, "if%d", idx);
102 return buf;
103}
104
105
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200106const char FAST_FUNC *ll_index_to_name(int idx)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000107{
108 static char nbuf[16];
109
110 return ll_idx_n2a(idx, nbuf);
111}
112
Denis Vlasenko08a61182007-06-19 12:11:20 +0000113#ifdef UNUSED
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000114int ll_index_to_type(int idx)
115{
116 struct idxmap *im;
117
118 if (idx == 0)
119 return -1;
Denis Vlasenko08a61182007-06-19 12:11:20 +0000120 im = find_by_index(idx);
121 if (im)
122 return im->type;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000123 return -1;
124}
Denis Vlasenko08a61182007-06-19 12:11:20 +0000125#endif
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000126
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200127unsigned FAST_FUNC ll_index_to_flags(int idx)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000128{
129 struct idxmap *im;
130
131 if (idx == 0)
132 return 0;
Denis Vlasenko08a61182007-06-19 12:11:20 +0000133 im = find_by_index(idx);
134 if (im)
135 return im->flags;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000136 return 0;
137}
138
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200139int FAST_FUNC xll_name_to_index(const char *name)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000140{
Denis Vlasenko08a61182007-06-19 12:11:20 +0000141 int ret = 0;
142 int sock_fd;
143
144/* caching is not warranted - no users which repeatedly call it */
145#ifdef UNUSED
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000146 static char ncache[16];
147 static int icache;
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000148
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000149 struct idxmap *im;
150 int i;
151
152 if (name == NULL)
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000153 goto out;
154 if (icache && strcmp(name, ncache) == 0) {
155 ret = icache;
156 goto out;
157 }
Denys Vlasenko94466b82009-10-13 16:27:11 +0200158 if (idxmap) {
159 for (i = 0; i < 16; i++) {
160 for (im = idxmap[i]; im; im = im->next) {
161 if (strcmp(im->name, name) == 0) {
162 icache = im->index;
163 strcpy(ncache, name);
164 ret = im->index;
165 goto out;
166 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000167 }
168 }
169 }
Denis Vlasenko7935a5a2006-09-30 00:18:16 +0000170 /* We have not found the interface in our cache, but the kernel
171 * may still know about it. One reason is that we may be using
172 * module on-demand loading, which means that the kernel will
173 * load the module and make the interface exist only when
174 * we explicitely request it (check for dev_load() in net/core/dev.c).
175 * I can think of other similar scenario, but they are less common...
176 * Jean II */
Denis Vlasenko08a61182007-06-19 12:11:20 +0000177#endif
178
Denis Vlasenko7935a5a2006-09-30 00:18:16 +0000179 sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
Denis Vlasenko12abcb32008-12-10 14:14:09 +0000180 if (sock_fd >= 0) {
Denis Vlasenko7935a5a2006-09-30 00:18:16 +0000181 struct ifreq ifr;
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000182 int tmp;
Denis Vlasenko08a61182007-06-19 12:11:20 +0000183
Denis Vlasenko360d9662008-12-02 18:18:50 +0000184 strncpy_IFNAMSIZ(ifr.ifr_name, name);
Denis Vlasenko7935a5a2006-09-30 00:18:16 +0000185 ifr.ifr_ifindex = -1;
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000186 tmp = ioctl(sock_fd, SIOCGIFINDEX, &ifr);
Denis Vlasenko7935a5a2006-09-30 00:18:16 +0000187 close(sock_fd);
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000188 if (tmp >= 0)
Denis Vlasenko7935a5a2006-09-30 00:18:16 +0000189 /* In theory, we should redump the interface list
190 * to update our cache, this is left as an exercise
191 * to the reader... Jean II */
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000192 ret = ifr.ifr_ifindex;
Denis Vlasenko7935a5a2006-09-30 00:18:16 +0000193 }
Denis Vlasenko08a61182007-06-19 12:11:20 +0000194/* out:*/
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000195 if (ret <= 0)
196 bb_error_msg_and_die("cannot find device \"%s\"", name);
197 return ret;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000198}
199
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200200int FAST_FUNC ll_init_map(struct rtnl_handle *rth)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000201{
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000202 xrtnl_wilddump_request(rth, AF_UNSPEC, RTM_GETLINK);
Denys Vlasenko94466b82009-10-13 16:27:11 +0200203 xrtnl_dump_filter(rth, ll_remember_index, NULL);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000204 return 0;
205}