blob: a8fcd7f888062e845bdb970062a493696de7ccda [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
Rob Landleyecae66a2006-06-02 20:53:38 +000014#include "libbb.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000015#include <string.h>
16
17#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 +000020#include <sys/socket.h> /* socket() */
21#include <net/if.h> /* struct ifreq and co. */
22#include <sys/ioctl.h> /* ioctl() & SIOCGIFINDEX */
23
24struct idxmap {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000025 struct idxmap * next;
26 int index;
27 int type;
28 int alen;
29 unsigned flags;
30 unsigned char addr[8];
31 char name[16];
32};
33
34static struct idxmap *idxmap[16];
35
36int ll_remember_index(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
37{
38 int h;
39 struct ifinfomsg *ifi = NLMSG_DATA(n);
40 struct idxmap *im, **imp;
41 struct rtattr *tb[IFLA_MAX+1];
42
43 if (n->nlmsg_type != RTM_NEWLINK)
44 return 0;
45
46 if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifi)))
47 return -1;
48
49
50 memset(tb, 0, sizeof(tb));
51 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), IFLA_PAYLOAD(n));
52 if (tb[IFLA_IFNAME] == NULL)
53 return 0;
54
55 h = ifi->ifi_index&0xF;
56
Denis Vlasenko540a2a12007-04-07 01:14:45 +000057 for (imp = &idxmap[h]; (im = *imp) != NULL; imp = &im->next)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000058 if (im->index == ifi->ifi_index)
59 break;
60
61 if (im == NULL) {
Rob Landleya6e131d2006-05-29 06:43:55 +000062 im = xmalloc(sizeof(*im));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000063 im->next = *imp;
64 im->index = ifi->ifi_index;
65 *imp = im;
66 }
67
68 im->type = ifi->ifi_type;
69 im->flags = ifi->ifi_flags;
70 if (tb[IFLA_ADDRESS]) {
71 int alen;
72 im->alen = alen = RTA_PAYLOAD(tb[IFLA_ADDRESS]);
73 if (alen > sizeof(im->addr))
74 alen = sizeof(im->addr);
75 memcpy(im->addr, RTA_DATA(tb[IFLA_ADDRESS]), alen);
76 } else {
77 im->alen = 0;
78 memset(im->addr, 0, sizeof(im->addr));
79 }
80 strcpy(im->name, RTA_DATA(tb[IFLA_IFNAME]));
81 return 0;
82}
83
84const char *ll_idx_n2a(int idx, char *buf)
85{
86 struct idxmap *im;
87
88 if (idx == 0)
89 return "*";
Denis Vlasenko540a2a12007-04-07 01:14:45 +000090 for (im = idxmap[idx & 0xF]; im; im = im->next)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000091 if (im->index == idx)
92 return im->name;
93 snprintf(buf, 16, "if%d", idx);
94 return buf;
95}
96
97
98const char *ll_index_to_name(int idx)
99{
100 static char nbuf[16];
101
102 return ll_idx_n2a(idx, nbuf);
103}
104
105int ll_index_to_type(int idx)
106{
107 struct idxmap *im;
108
109 if (idx == 0)
110 return -1;
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000111 for (im = idxmap[idx & 0xF]; im; im = im->next)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000112 if (im->index == idx)
113 return im->type;
114 return -1;
115}
116
117unsigned ll_index_to_flags(int idx)
118{
119 struct idxmap *im;
120
121 if (idx == 0)
122 return 0;
123
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000124 for (im = idxmap[idx & 0xF]; im; im = im->next)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000125 if (im->index == idx)
126 return im->flags;
127 return 0;
128}
129
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000130// TODO: caching is not warranted - no users which repeatedly call it
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000131int ll_name_to_index(char *name)
132{
133 static char ncache[16];
134 static int icache;
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000135
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000136 struct idxmap *im;
Denis Vlasenko7935a5a2006-09-30 00:18:16 +0000137 int sock_fd;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000138 int i;
139
140 if (name == NULL)
141 return 0;
142 if (icache && strcmp(name, ncache) == 0)
143 return icache;
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000144 for (i = 0; i < 16; i++) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000145 for (im = idxmap[i]; im; im = im->next) {
146 if (strcmp(im->name, name) == 0) {
147 icache = im->index;
148 strcpy(ncache, name);
149 return im->index;
150 }
151 }
152 }
Denis Vlasenko7935a5a2006-09-30 00:18:16 +0000153 /* We have not found the interface in our cache, but the kernel
154 * may still know about it. One reason is that we may be using
155 * module on-demand loading, which means that the kernel will
156 * load the module and make the interface exist only when
157 * we explicitely request it (check for dev_load() in net/core/dev.c).
158 * I can think of other similar scenario, but they are less common...
159 * Jean II */
160 sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
161 if (sock_fd) {
162 struct ifreq ifr;
163 int ret;
164 strncpy(ifr.ifr_name, name, IFNAMSIZ);
165 ifr.ifr_ifindex = -1;
166 ret = ioctl(sock_fd, SIOCGIFINDEX, &ifr);
167 close(sock_fd);
168 if (ret >= 0)
169 /* In theory, we should redump the interface list
170 * to update our cache, this is left as an exercise
171 * to the reader... Jean II */
172 return ifr.ifr_ifindex;
173 }
Denis Vlasenkof7996f32007-01-11 17:20:00 +0000174
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000175 return 0;
176}
177
178int ll_init_map(struct rtnl_handle *rth)
179{
180 if (rtnl_wilddump_request(rth, AF_UNSPEC, RTM_GETLINK) < 0) {
Denis Vlasenko7935a5a2006-09-30 00:18:16 +0000181 bb_perror_msg_and_die("cannot send dump request");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000182 }
183
184 if (rtnl_dump_filter(rth, ll_remember_index, &idxmap, NULL, NULL) < 0) {
Denis Vlasenko7935a5a2006-09-30 00:18:16 +0000185 bb_error_msg_and_die("dump terminated");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000186 }
187 return 0;
188}