blob: eeae4e252ce75db2add7930e5dce2c0f64562ed7 [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
30static struct idxmap *idxmap[16];
31
Denis Vlasenko08a61182007-06-19 12:11:20 +000032static struct idxmap *find_by_index(int idx)
33{
34 struct idxmap *im;
35
36 for (im = idxmap[idx & 0xF]; im; im = im->next)
37 if (im->index == idx)
38 return im;
39 return NULL;
40}
41
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000042int ll_remember_index(const struct sockaddr_nl *who UNUSED_PARAM,
Denis Vlasenko68404f12008-03-17 09:00:54 +000043 struct nlmsghdr *n,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000044 void *arg UNUSED_PARAM)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000045{
46 int h;
47 struct ifinfomsg *ifi = NLMSG_DATA(n);
48 struct idxmap *im, **imp;
49 struct rtattr *tb[IFLA_MAX+1];
50
51 if (n->nlmsg_type != RTM_NEWLINK)
52 return 0;
53
54 if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifi)))
55 return -1;
56
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000057 memset(tb, 0, sizeof(tb));
58 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), IFLA_PAYLOAD(n));
59 if (tb[IFLA_IFNAME] == NULL)
60 return 0;
61
Denis Vlasenko08a61182007-06-19 12:11:20 +000062 h = ifi->ifi_index & 0xF;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000063
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
89const char *ll_idx_n2a(int idx, char *buf)
90{
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;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000098 snprintf(buf, 16, "if%d", idx);
99 return buf;
100}
101
102
103const char *ll_index_to_name(int idx)
104{
105 static char nbuf[16];
106
107 return ll_idx_n2a(idx, nbuf);
108}
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
124unsigned ll_index_to_flags(int idx)
125{
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
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000136int xll_name_to_index(const char *const name)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000137{
Denis Vlasenko08a61182007-06-19 12:11:20 +0000138 int ret = 0;
139 int sock_fd;
140
141/* caching is not warranted - no users which repeatedly call it */
142#ifdef UNUSED
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000143 static char ncache[16];
144 static int icache;
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000145
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000146 struct idxmap *im;
147 int i;
148
149 if (name == NULL)
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000150 goto out;
151 if (icache && strcmp(name, ncache) == 0) {
152 ret = icache;
153 goto out;
154 }
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000155 for (i = 0; i < 16; i++) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000156 for (im = idxmap[i]; im; im = im->next) {
157 if (strcmp(im->name, name) == 0) {
158 icache = im->index;
159 strcpy(ncache, name);
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000160 ret = im->index;
161 goto out;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000162 }
163 }
164 }
Denis Vlasenko7935a5a2006-09-30 00:18:16 +0000165 /* We have not found the interface in our cache, but the kernel
166 * may still know about it. One reason is that we may be using
167 * module on-demand loading, which means that the kernel will
168 * load the module and make the interface exist only when
169 * we explicitely request it (check for dev_load() in net/core/dev.c).
170 * I can think of other similar scenario, but they are less common...
171 * Jean II */
Denis Vlasenko08a61182007-06-19 12:11:20 +0000172#endif
173
Denis Vlasenko7935a5a2006-09-30 00:18:16 +0000174 sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
175 if (sock_fd) {
176 struct ifreq ifr;
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000177 int tmp;
Denis Vlasenko08a61182007-06-19 12:11:20 +0000178
Denis Vlasenko7935a5a2006-09-30 00:18:16 +0000179 strncpy(ifr.ifr_name, name, IFNAMSIZ);
180 ifr.ifr_ifindex = -1;
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000181 tmp = ioctl(sock_fd, SIOCGIFINDEX, &ifr);
Denis Vlasenko7935a5a2006-09-30 00:18:16 +0000182 close(sock_fd);
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000183 if (tmp >= 0)
Denis Vlasenko7935a5a2006-09-30 00:18:16 +0000184 /* In theory, we should redump the interface list
185 * to update our cache, this is left as an exercise
186 * to the reader... Jean II */
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000187 ret = ifr.ifr_ifindex;
Denis Vlasenko7935a5a2006-09-30 00:18:16 +0000188 }
Denis Vlasenko08a61182007-06-19 12:11:20 +0000189/* out:*/
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000190 if (ret <= 0)
191 bb_error_msg_and_die("cannot find device \"%s\"", name);
192 return ret;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000193}
194
195int ll_init_map(struct rtnl_handle *rth)
196{
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000197 xrtnl_wilddump_request(rth, AF_UNSPEC, RTM_GETLINK);
198 xrtnl_dump_filter(rth, ll_remember_index, &idxmap);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000199 return 0;
200}