Debian Codegraph @

lib/ll_map.c

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