1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* ll_map.c
*
* Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <net/if.h>
#include "libnetlink.h"
#include "ll_map.h"
#include "list.h"
#include "utils.h"
struct ll_cache {
struct hlist_node idx_hash;
struct hlist_node name_hash;
unsigned flags;
unsigned index;
unsigned short type;
struct list_head altnames_list;
char name[];
};
#define IDXMAP_SIZE 1024
static struct hlist_head idx_head[IDXMAP_SIZE];
static struct hlist_head name_head[IDXMAP_SIZE];
static struct ll_cache *ll_get_by_index(unsigned index)
{
struct hlist_node *n;
unsigned h = index & (IDXMAP_SIZE - 1);
hlist_for_each(n, &idx_head[h]) {
struct ll_cache *im
= container_of(n, struct ll_cache, idx_hash);
if (im->index == index)
return im;
}
return NULL;
}
unsigned namehash(const char *str)
{
unsigned hash = 5381;
while (*str)
hash = ((hash << 5) + hash) + *str++; /* hash * 33 + c */
return hash;
}
static struct ll_cache *ll_get_by_name(const char *name)
{
struct hlist_node *n;
unsigned h = namehash(name) & (IDXMAP_SIZE - 1);
hlist_for_each(n, &name_head[h]) {
struct ll_cache *im
= container_of(n, struct ll_cache, name_hash);
if (strcmp(im->name, name) == 0)
return im;
}
return NULL;
}
static struct ll_cache *ll_entry_create(struct ifinfomsg *ifi,
const char *ifname,
struct ll_cache *parent_im)
{
struct ll_cache *im;
unsigned int h;
im = malloc(sizeof(*im) + strlen(ifname) + 1);
if (!im)
return NULL;
im->index = ifi->ifi_index;
strcpy(im->name, ifname);
im->type = ifi->ifi_type;
im->flags = ifi->ifi_flags;
if (parent_im) {
list_add_tail(&im->altnames_list, &parent_im->altnames_list);
} else {
/* This is parent, insert to index hash. */
h = ifi->ifi_index & (IDXMAP_SIZE - 1);
hlist_add_head(&im->idx_hash, &idx_head[h]);
INIT_LIST_HEAD(&im->altnames_list);
}
h = namehash(ifname) & (IDXMAP_SIZE - 1);
hlist_add_head(&im->name_hash, &name_head[h]);
return im;
}
static void ll_entry_destroy(struct ll_cache *im, bool im_is_parent)
{
hlist_del(&im->name_hash);
if (im_is_parent)
hlist_del(&im->idx_hash);
else
list_del(&im->altnames_list);
free(im);
}
static void ll_entry_update(struct ll_cache *im, struct ifinfomsg *ifi,
const char *ifname)
{
unsigned int h;
im->flags = ifi->ifi_flags;
if (!strcmp(im->name, ifname))
return;
hlist_del(&im->name_hash);
h = namehash(ifname) & (IDXMAP_SIZE - 1);
hlist_add_head(&im->name_hash, &name_head[h]);
}
static void ll_altname_entries_create(struct ll_cache *parent_im,
struct ifinfomsg *ifi, struct rtattr **tb)
{
struct rtattr *i, *proplist = tb[IFLA_PROP_LIST];
int rem;
if (!proplist)
return;
rem = RTA_PAYLOAD(proplist);
for (i = RTA_DATA(proplist); RTA_OK(i, rem);
i = RTA_NEXT(i, rem)) {
if (i->rta_type != IFLA_ALT_IFNAME)
continue;
ll_entry_create(ifi, rta_getattr_str(i), parent_im);
}
}
static void ll_altname_entries_destroy(struct ll_cache *parent_im)
{
struct ll_cache *im, *tmp;
list_for_each_entry_safe(im, tmp, &parent_im->altnames_list,
altnames_list)
ll_entry_destroy(im, false);
}
static void ll_altname_entries_update(struct ll_cache *parent_im,
struct ifinfomsg *ifi, struct rtattr **tb)
{
struct rtattr *i, *proplist = tb[IFLA_PROP_LIST];
struct ll_cache *im;
int rem;
if (!proplist) {
ll_altname_entries_destroy(parent_im);
return;
}
/* Simply compare the altname list with the cached one
* and if it does not fit 1:1, recreate the cached list
* from scratch.
*/
im = list_first_entry(&parent_im->altnames_list, typeof(*im),
altnames_list);
rem = RTA_PAYLOAD(proplist);
for (i = RTA_DATA(proplist); RTA_OK(i, rem);
i = RTA_NEXT(i, rem)) {
if (i->rta_type != IFLA_ALT_IFNAME)
continue;
if (!im || strcmp(rta_getattr_str(i), im->name))
goto recreate_altname_entries;
im = list_next_entry(im, altnames_list);
}
if (list_next_entry(im, altnames_list))
goto recreate_altname_entries;
return;
recreate_altname_entries:
ll_altname_entries_destroy(parent_im);
ll_altname_entries_create(parent_im, ifi, tb);
}
static void ll_entries_create(struct ifinfomsg *ifi, struct rtattr **tb)
{
struct ll_cache *parent_im;
if (!tb[IFLA_IFNAME])
return;
parent_im = ll_entry_create(ifi, rta_getattr_str(tb[IFLA_IFNAME]),
NULL);
if (!parent_im)
return;
ll_altname_entries_create(parent_im, ifi, tb);
}
static void ll_entries_destroy(struct ll_cache *parent_im)
{
ll_altname_entries_destroy(parent_im);
ll_entry_destroy(parent_im, true);
}
static void ll_entries_update(struct ll_cache *parent_im,
struct ifinfomsg *ifi, struct rtattr **tb)
{
if (tb[IFLA_IFNAME])
ll_entry_update(parent_im, ifi,
rta_getattr_str(tb[IFLA_IFNAME]));
ll_altname_entries_update(parent_im, ifi, tb);
}
int ll_remember_index(struct nlmsghdr *n, void *arg)
{
struct ifinfomsg *ifi = NLMSG_DATA(n);
struct ll_cache *im;
struct rtattr *tb[IFLA_MAX+1];
if (n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK)
return 0;
if (n->nlmsg_len < NLMSG_LENGTH(sizeof(*ifi)))
return -1;
im = ll_get_by_index(ifi->ifi_index);
if (n->nlmsg_type == RTM_DELLINK) {
if (im)
ll_entries_destroy(im);
return 0;
}
parse_rtattr_flags(tb, IFLA_MAX, IFLA_RTA(ifi),
IFLA_PAYLOAD(n), NLA_F_NESTED);
if (im)
ll_entries_update(im, ifi, tb);
else
ll_entries_create(ifi, tb);
return 0;
}
const char *ll_idx_n2a(unsigned int idx)
{
static char buf[IFNAMSIZ];
snprintf(buf, sizeof(buf), "if%u", idx);
return buf;
}
static unsigned int ll_idx_a2n(const char *name)
{
unsigned int idx;
if (sscanf(name, "if%u", &idx) != 1)
return 0;
return idx;
}
static int ll_link_get(const char *name, int index)
{
struct {
struct nlmsghdr n;
struct ifinfomsg ifm;
char buf[1024];
} req = {
.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
.n.nlmsg_flags = NLM_F_REQUEST,
.n.nlmsg_type = RTM_GETLINK,
.ifm.ifi_index = index,
};
__u32 filt_mask = RTEXT_FILTER_VF | RTEXT_FILTER_SKIP_STATS;
struct rtnl_handle rth = {};
struct nlmsghdr *answer;
int rc = 0;
if (rtnl_open(&rth, 0) < 0)
return 0;
addattr32(&req.n, sizeof(req), IFLA_EXT_MASK, filt_mask);
if (name)
addattr_l(&req.n, sizeof(req),
!check_ifname(name) ? IFLA_IFNAME : IFLA_ALT_IFNAME,
name, strlen(name) + 1);
if (rtnl_talk_suppress_rtnl_errmsg(&rth, &req.n, &answer) < 0)
goto out;
/* add entry to cache */
rc = ll_remember_index(answer, NULL);
if (!rc) {
struct ifinfomsg *ifm = NLMSG_DATA(answer);
rc = ifm->ifi_index;
}
free(answer);
out:
rtnl_close(&rth);
return rc;
}
const char *ll_index_to_name(unsigned int idx)
{
static char buf[IFNAMSIZ];
const struct ll_cache *im;
if (idx == 0)
return "*";
im = ll_get_by_index(idx);
if (im)
return im->name;
if (ll_link_get(NULL, idx) == idx) {
im = ll_get_by_index(idx);
if (im)
return im->name;
}
if (if_indextoname(idx, buf) == NULL)
snprintf(buf, IFNAMSIZ, "if%u", idx);
return buf;
}
int ll_index_to_type(unsigned idx)
{
const struct ll_cache *im;
if (idx == 0)
return -1;
im = ll_get_by_index(idx);
return im ? im->type : -1;
}
int ll_index_to_flags(unsigned idx)
{
const struct ll_cache *im;
if (idx == 0)
return 0;
im = ll_get_by_index(idx);
return im ? im->flags : -1;
}
unsigned ll_name_to_index(const char *name)
{
const struct ll_cache *im;
unsigned idx;
if (name == NULL)
return 0;
im = ll_get_by_name(name);
if (im)
return im->index;
idx = ll_link_get(name, 0);
if (idx == 0)
idx = if_nametoindex(name);
if (idx == 0)
idx = ll_idx_a2n(name);
return idx;
}
void ll_drop_by_index(unsigned index)
{
struct ll_cache *im;
im = ll_get_by_index(index);
if (!im)
return;
hlist_del(&im->idx_hash);
hlist_del(&im->name_hash);
free(im);
}
void ll_init_map(struct rtnl_handle *rth)
{
static int initialized;
if (initialized)
return;
if (rtnl_linkdump_req(rth, AF_UNSPEC) < 0) {
perror("Cannot send dump request");
exit(1);
}
if (rtnl_dump_filter(rth, ll_remember_index, NULL) < 0) {
fprintf(stderr, "Dump terminated\n");
exit(1);
}
initialized = 1;
}
|