1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 
3 #include <net/if.h>
4 #include <netinet/ether.h>
5 #include <netinet/in.h>
6 #include <linux/fou.h>
7 #include <linux/genetlink.h>
8 #include <linux/if_macsec.h>
9 #include <linux/l2tp.h>
10 #include <linux/nl80211.h>
11 
12 #include "sd-netlink.h"
13 
14 #include "alloc-util.h"
15 #include "ether-addr-util.h"
16 #include "macro.h"
17 #include "netlink-genl.h"
18 #include "netlink-internal.h"
19 #include "netlink-util.h"
20 #include "socket-util.h"
21 #include "stdio-util.h"
22 #include "string-util.h"
23 #include "strv.h"
24 #include "tests.h"
25 
test_message_link_bridge(sd_netlink * rtnl)26 static void test_message_link_bridge(sd_netlink *rtnl) {
27         _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *message = NULL;
28         uint32_t cost;
29 
30         log_debug("/* %s */", __func__);
31 
32         assert_se(sd_rtnl_message_new_link(rtnl, &message, RTM_NEWLINK, 1) >= 0);
33         assert_se(sd_rtnl_message_link_set_family(message, AF_BRIDGE) >= 0);
34         assert_se(sd_netlink_message_open_container(message, IFLA_PROTINFO) >= 0);
35         assert_se(sd_netlink_message_append_u32(message, IFLA_BRPORT_COST, 10) >= 0);
36         assert_se(sd_netlink_message_close_container(message) >= 0);
37 
38         assert_se(sd_netlink_message_rewind(message, rtnl) >= 0);
39 
40         assert_se(sd_netlink_message_enter_container(message, IFLA_PROTINFO) >= 0);
41         assert_se(sd_netlink_message_read_u32(message, IFLA_BRPORT_COST, &cost) >= 0);
42         assert_se(cost == 10);
43         assert_se(sd_netlink_message_exit_container(message) >= 0);
44 }
45 
test_link_configure(sd_netlink * rtnl,int ifindex)46 static void test_link_configure(sd_netlink *rtnl, int ifindex) {
47         _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *message = NULL, *reply = NULL;
48         uint32_t mtu_out;
49         const char *name_out;
50         struct ether_addr mac_out;
51 
52         log_debug("/* %s */", __func__);
53 
54         /* we'd really like to test NEWLINK, but let's not mess with the running kernel */
55         assert_se(sd_rtnl_message_new_link(rtnl, &message, RTM_GETLINK, ifindex) >= 0);
56 
57         assert_se(sd_netlink_call(rtnl, message, 0, &reply) == 1);
58 
59         assert_se(sd_netlink_message_read_string(reply, IFLA_IFNAME, &name_out) >= 0);
60         assert_se(sd_netlink_message_read_ether_addr(reply, IFLA_ADDRESS, &mac_out) >= 0);
61         assert_se(sd_netlink_message_read_u32(reply, IFLA_MTU, &mtu_out) >= 0);
62 }
63 
test_link_get(sd_netlink * rtnl,int ifindex)64 static void test_link_get(sd_netlink *rtnl, int ifindex) {
65         _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL, *r = NULL;
66         const char *str_data;
67         uint8_t u8_data;
68         uint32_t u32_data;
69         struct ether_addr eth_data;
70 
71         log_debug("/* %s */", __func__);
72 
73         assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_GETLINK, ifindex) >= 0);
74         assert_se(m);
75 
76         assert_se(sd_netlink_call(rtnl, m, 0, &r) == 1);
77 
78         assert_se(sd_netlink_message_read_string(r, IFLA_IFNAME, &str_data) == 0);
79 
80         assert_se(sd_netlink_message_read_u8(r, IFLA_CARRIER, &u8_data) == 0);
81         assert_se(sd_netlink_message_read_u8(r, IFLA_OPERSTATE, &u8_data) == 0);
82         assert_se(sd_netlink_message_read_u8(r, IFLA_LINKMODE, &u8_data) == 0);
83 
84         assert_se(sd_netlink_message_read_u32(r, IFLA_MTU, &u32_data) == 0);
85         assert_se(sd_netlink_message_read_u32(r, IFLA_GROUP, &u32_data) == 0);
86         assert_se(sd_netlink_message_read_u32(r, IFLA_TXQLEN, &u32_data) == 0);
87         assert_se(sd_netlink_message_read_u32(r, IFLA_NUM_TX_QUEUES, &u32_data) == 0);
88         assert_se(sd_netlink_message_read_u32(r, IFLA_NUM_RX_QUEUES, &u32_data) == 0);
89 
90         assert_se(sd_netlink_message_read_ether_addr(r, IFLA_ADDRESS, &eth_data) == 0);
91 }
92 
test_address_get(sd_netlink * rtnl,int ifindex)93 static void test_address_get(sd_netlink *rtnl, int ifindex) {
94         _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL, *r = NULL;
95         struct in_addr in_data;
96         struct ifa_cacheinfo cache;
97         const char *label;
98 
99         log_debug("/* %s */", __func__);
100 
101         assert_se(sd_rtnl_message_new_addr(rtnl, &m, RTM_GETADDR, ifindex, AF_INET) >= 0);
102         assert_se(m);
103         assert_se(sd_netlink_message_request_dump(m, true) >= 0);
104         assert_se(sd_netlink_call(rtnl, m, -1, &r) == 1);
105 
106         assert_se(sd_netlink_message_read_in_addr(r, IFA_LOCAL, &in_data) == 0);
107         assert_se(sd_netlink_message_read_in_addr(r, IFA_ADDRESS, &in_data) == 0);
108         assert_se(sd_netlink_message_read_string(r, IFA_LABEL, &label) == 0);
109         assert_se(sd_netlink_message_read_cache_info(r, IFA_CACHEINFO, &cache) == 0);
110 }
111 
test_route(sd_netlink * rtnl)112 static void test_route(sd_netlink *rtnl) {
113         _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
114         struct in_addr addr, addr_data;
115         uint32_t index = 2, u32_data;
116         int r;
117 
118         log_debug("/* %s */", __func__);
119 
120         r = sd_rtnl_message_new_route(rtnl, &req, RTM_NEWROUTE, AF_INET, RTPROT_STATIC);
121         if (r < 0) {
122                 log_error_errno(r, "Could not create RTM_NEWROUTE message: %m");
123                 return;
124         }
125 
126         addr.s_addr = htobe32(INADDR_LOOPBACK);
127 
128         r = sd_netlink_message_append_in_addr(req, RTA_GATEWAY, &addr);
129         if (r < 0) {
130                 log_error_errno(r, "Could not append RTA_GATEWAY attribute: %m");
131                 return;
132         }
133 
134         r = sd_netlink_message_append_u32(req, RTA_OIF, index);
135         if (r < 0) {
136                 log_error_errno(r, "Could not append RTA_OIF attribute: %m");
137                 return;
138         }
139 
140         assert_se(sd_netlink_message_rewind(req, rtnl) >= 0);
141 
142         assert_se(sd_netlink_message_read_in_addr(req, RTA_GATEWAY, &addr_data) >= 0);
143         assert_se(addr_data.s_addr == addr.s_addr);
144 
145         assert_se(sd_netlink_message_read_u32(req, RTA_OIF, &u32_data) >= 0);
146         assert_se(u32_data == index);
147 
148         assert_se((req = sd_netlink_message_unref(req)) == NULL);
149 }
150 
test_multiple(void)151 static void test_multiple(void) {
152         sd_netlink *rtnl1, *rtnl2;
153 
154         log_debug("/* %s */", __func__);
155 
156         assert_se(sd_netlink_open(&rtnl1) >= 0);
157         assert_se(sd_netlink_open(&rtnl2) >= 0);
158 
159         rtnl1 = sd_netlink_unref(rtnl1);
160         rtnl2 = sd_netlink_unref(rtnl2);
161 }
162 
link_handler(sd_netlink * rtnl,sd_netlink_message * m,void * userdata)163 static int link_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) {
164         char *ifname = userdata;
165         const char *data;
166 
167         assert_se(rtnl);
168         assert_se(m);
169         assert_se(userdata);
170 
171         log_info("%s: got link info about %s", __func__, ifname);
172         free(ifname);
173 
174         assert_se(sd_netlink_message_read_string(m, IFLA_IFNAME, &data) >= 0);
175         assert_se(streq(data, "lo"));
176 
177         return 1;
178 }
179 
test_event_loop(int ifindex)180 static void test_event_loop(int ifindex) {
181         _cleanup_(sd_event_unrefp) sd_event *event = NULL;
182         _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
183         _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
184         char *ifname;
185 
186         log_debug("/* %s */", __func__);
187 
188         ifname = strdup("lo2");
189         assert_se(ifname);
190 
191         assert_se(sd_netlink_open(&rtnl) >= 0);
192         assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_GETLINK, ifindex) >= 0);
193 
194         assert_se(sd_netlink_call_async(rtnl, NULL, m, link_handler, NULL, ifname, 0, NULL) >= 0);
195 
196         assert_se(sd_event_default(&event) >= 0);
197 
198         assert_se(sd_netlink_attach_event(rtnl, event, 0) >= 0);
199 
200         assert_se(sd_event_run(event, 0) >= 0);
201 
202         assert_se(sd_netlink_detach_event(rtnl) >= 0);
203 
204         assert_se((rtnl = sd_netlink_unref(rtnl)) == NULL);
205 }
206 
test_async_destroy(void * userdata)207 static void test_async_destroy(void *userdata) {
208 }
209 
test_async(int ifindex)210 static void test_async(int ifindex) {
211         _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
212         _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL, *r = NULL;
213         _cleanup_(sd_netlink_slot_unrefp) sd_netlink_slot *slot = NULL;
214         sd_netlink_destroy_t destroy_callback;
215         const char *description;
216         char *ifname;
217 
218         log_debug("/* %s */", __func__);
219 
220         ifname = strdup("lo");
221         assert_se(ifname);
222 
223         assert_se(sd_netlink_open(&rtnl) >= 0);
224 
225         assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_GETLINK, ifindex) >= 0);
226 
227         assert_se(sd_netlink_call_async(rtnl, &slot, m, link_handler, test_async_destroy, ifname, 0, "hogehoge") >= 0);
228 
229         assert_se(sd_netlink_slot_get_netlink(slot) == rtnl);
230         assert_se(sd_netlink_slot_get_userdata(slot) == ifname);
231         assert_se(sd_netlink_slot_get_destroy_callback(slot, &destroy_callback) == 1);
232         assert_se(destroy_callback == test_async_destroy);
233         assert_se(sd_netlink_slot_get_floating(slot) == 0);
234         assert_se(sd_netlink_slot_get_description(slot, &description) == 1);
235         assert_se(streq(description, "hogehoge"));
236 
237         assert_se(sd_netlink_wait(rtnl, 0) >= 0);
238         assert_se(sd_netlink_process(rtnl, &r) >= 0);
239 
240         assert_se((rtnl = sd_netlink_unref(rtnl)) == NULL);
241 }
242 
test_slot_set(int ifindex)243 static void test_slot_set(int ifindex) {
244         _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
245         _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL, *r = NULL;
246         _cleanup_(sd_netlink_slot_unrefp) sd_netlink_slot *slot = NULL;
247         sd_netlink_destroy_t destroy_callback;
248         const char *description;
249         char *ifname;
250 
251         log_debug("/* %s */", __func__);
252 
253         ifname = strdup("lo");
254         assert_se(ifname);
255 
256         assert_se(sd_netlink_open(&rtnl) >= 0);
257 
258         assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_GETLINK, ifindex) >= 0);
259 
260         assert_se(sd_netlink_call_async(rtnl, &slot, m, link_handler, NULL, NULL, 0, NULL) >= 0);
261 
262         assert_se(sd_netlink_slot_get_netlink(slot) == rtnl);
263         assert_se(!sd_netlink_slot_get_userdata(slot));
264         assert_se(!sd_netlink_slot_set_userdata(slot, ifname));
265         assert_se(sd_netlink_slot_get_userdata(slot) == ifname);
266         assert_se(sd_netlink_slot_get_destroy_callback(slot, NULL) == 0);
267         assert_se(sd_netlink_slot_set_destroy_callback(slot, test_async_destroy) >= 0);
268         assert_se(sd_netlink_slot_get_destroy_callback(slot, &destroy_callback) == 1);
269         assert_se(destroy_callback == test_async_destroy);
270         assert_se(sd_netlink_slot_get_floating(slot) == 0);
271         assert_se(sd_netlink_slot_set_floating(slot, 1) == 1);
272         assert_se(sd_netlink_slot_get_floating(slot) == 1);
273         assert_se(sd_netlink_slot_get_description(slot, NULL) == 0);
274         assert_se(sd_netlink_slot_set_description(slot, "hogehoge") >= 0);
275         assert_se(sd_netlink_slot_get_description(slot, &description) == 1);
276         assert_se(streq(description, "hogehoge"));
277 
278         assert_se(sd_netlink_wait(rtnl, 0) >= 0);
279         assert_se(sd_netlink_process(rtnl, &r) >= 0);
280 
281         assert_se((rtnl = sd_netlink_unref(rtnl)) == NULL);
282 }
283 
284 struct test_async_object {
285         unsigned n_ref;
286         char *ifname;
287 };
288 
test_async_object_free(struct test_async_object * t)289 static struct test_async_object *test_async_object_free(struct test_async_object *t) {
290         assert_se(t);
291 
292         free(t->ifname);
293         return mfree(t);
294 }
295 
296 DEFINE_PRIVATE_TRIVIAL_REF_UNREF_FUNC(struct test_async_object, test_async_object, test_async_object_free);
297 DEFINE_TRIVIAL_CLEANUP_FUNC(struct test_async_object *, test_async_object_unref);
298 
link_handler2(sd_netlink * rtnl,sd_netlink_message * m,void * userdata)299 static int link_handler2(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) {
300         struct test_async_object *t = userdata;
301         const char *data;
302 
303         assert_se(rtnl);
304         assert_se(m);
305         assert_se(userdata);
306 
307         log_info("%s: got link info about %s", __func__, t->ifname);
308 
309         assert_se(sd_netlink_message_read_string(m, IFLA_IFNAME, &data) >= 0);
310         assert_se(streq(data, "lo"));
311 
312         return 1;
313 }
314 
test_async_object_destroy(void * userdata)315 static void test_async_object_destroy(void *userdata) {
316         struct test_async_object *t = userdata;
317 
318         assert_se(userdata);
319 
320         log_info("%s: n_ref=%u", __func__, t->n_ref);
321         test_async_object_unref(t);
322 }
323 
test_async_destroy_callback(int ifindex)324 static void test_async_destroy_callback(int ifindex) {
325         _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
326         _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL, *r = NULL;
327         _cleanup_(test_async_object_unrefp) struct test_async_object *t = NULL;
328         _cleanup_(sd_netlink_slot_unrefp) sd_netlink_slot *slot = NULL;
329         char *ifname;
330 
331         log_debug("/* %s */", __func__);
332 
333         assert_se(t = new(struct test_async_object, 1));
334         assert_se(ifname = strdup("lo"));
335         *t = (struct test_async_object) {
336                 .n_ref = 1,
337                 .ifname = ifname,
338         };
339 
340         assert_se(sd_netlink_open(&rtnl) >= 0);
341 
342         /* destroy callback is called after processing message */
343         assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_GETLINK, ifindex) >= 0);
344         assert_se(sd_netlink_call_async(rtnl, NULL, m, link_handler2, test_async_object_destroy, t, 0, NULL) >= 0);
345 
346         assert_se(t->n_ref == 1);
347         assert_se(test_async_object_ref(t));
348         assert_se(t->n_ref == 2);
349 
350         assert_se(sd_netlink_wait(rtnl, 0) >= 0);
351         assert_se(sd_netlink_process(rtnl, &r) == 1);
352         assert_se(t->n_ref == 1);
353 
354         assert_se(!sd_netlink_message_unref(m));
355 
356         /* destroy callback is called when asynchronous call is cancelled, that is, slot is freed. */
357         assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_GETLINK, ifindex) >= 0);
358         assert_se(sd_netlink_call_async(rtnl, &slot, m, link_handler2, test_async_object_destroy, t, 0, NULL) >= 0);
359 
360         assert_se(t->n_ref == 1);
361         assert_se(test_async_object_ref(t));
362         assert_se(t->n_ref == 2);
363 
364         assert_se(!(slot = sd_netlink_slot_unref(slot)));
365         assert_se(t->n_ref == 1);
366 
367         assert_se(!sd_netlink_message_unref(m));
368 
369         /* destroy callback is also called by sd_netlink_unref() */
370         assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_GETLINK, ifindex) >= 0);
371         assert_se(sd_netlink_call_async(rtnl, NULL, m, link_handler2, test_async_object_destroy, t, 0, NULL) >= 0);
372 
373         assert_se(t->n_ref == 1);
374         assert_se(test_async_object_ref(t));
375         assert_se(t->n_ref == 2);
376 
377         assert_se((rtnl = sd_netlink_unref(rtnl)) == NULL);
378         assert_se(t->n_ref == 1);
379 }
380 
pipe_handler(sd_netlink * rtnl,sd_netlink_message * m,void * userdata)381 static int pipe_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) {
382         int *counter = userdata;
383         int r;
384 
385         (*counter)--;
386 
387         r = sd_netlink_message_get_errno(m);
388 
389         log_info_errno(r, "%d left in pipe. got reply: %m", *counter);
390 
391         assert_se(r >= 0);
392 
393         return 1;
394 }
395 
test_pipe(int ifindex)396 static void test_pipe(int ifindex) {
397         _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
398         _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m1 = NULL, *m2 = NULL;
399         int counter = 0;
400 
401         log_debug("/* %s */", __func__);
402 
403         assert_se(sd_netlink_open(&rtnl) >= 0);
404 
405         assert_se(sd_rtnl_message_new_link(rtnl, &m1, RTM_GETLINK, ifindex) >= 0);
406         assert_se(sd_rtnl_message_new_link(rtnl, &m2, RTM_GETLINK, ifindex) >= 0);
407 
408         counter++;
409         assert_se(sd_netlink_call_async(rtnl, NULL, m1, pipe_handler, NULL, &counter, 0, NULL) >= 0);
410 
411         counter++;
412         assert_se(sd_netlink_call_async(rtnl, NULL, m2, pipe_handler, NULL, &counter, 0, NULL) >= 0);
413 
414         while (counter > 0) {
415                 assert_se(sd_netlink_wait(rtnl, 0) >= 0);
416                 assert_se(sd_netlink_process(rtnl, NULL) >= 0);
417         }
418 
419         assert_se((rtnl = sd_netlink_unref(rtnl)) == NULL);
420 }
421 
test_container(sd_netlink * rtnl)422 static void test_container(sd_netlink *rtnl) {
423         _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
424         uint16_t u16_data;
425         uint32_t u32_data;
426         const char *string_data;
427 
428         log_debug("/* %s */", __func__);
429 
430         assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_NEWLINK, 0) >= 0);
431 
432         assert_se(sd_netlink_message_open_container(m, IFLA_LINKINFO) >= 0);
433         assert_se(sd_netlink_message_open_container_union(m, IFLA_INFO_DATA, "vlan") >= 0);
434         assert_se(sd_netlink_message_append_u16(m, IFLA_VLAN_ID, 100) >= 0);
435         assert_se(sd_netlink_message_close_container(m) >= 0);
436         assert_se(sd_netlink_message_append_string(m, IFLA_INFO_KIND, "vlan") >= 0);
437         assert_se(sd_netlink_message_close_container(m) >= 0);
438         assert_se(sd_netlink_message_close_container(m) == -EINVAL);
439 
440         assert_se(sd_netlink_message_rewind(m, rtnl) >= 0);
441 
442         assert_se(sd_netlink_message_enter_container(m, IFLA_LINKINFO) >= 0);
443         assert_se(sd_netlink_message_read_string(m, IFLA_INFO_KIND, &string_data) >= 0);
444         assert_se(streq("vlan", string_data));
445 
446         assert_se(sd_netlink_message_enter_container(m, IFLA_INFO_DATA) >= 0);
447         assert_se(sd_netlink_message_read_u16(m, IFLA_VLAN_ID, &u16_data) >= 0);
448         assert_se(sd_netlink_message_exit_container(m) >= 0);
449 
450         assert_se(sd_netlink_message_read_string(m, IFLA_INFO_KIND, &string_data) >= 0);
451         assert_se(streq("vlan", string_data));
452         assert_se(sd_netlink_message_exit_container(m) >= 0);
453 
454         assert_se(sd_netlink_message_read_u32(m, IFLA_LINKINFO, &u32_data) < 0);
455 
456         assert_se(sd_netlink_message_exit_container(m) == -EINVAL);
457 }
458 
test_match(void)459 static void test_match(void) {
460         _cleanup_(sd_netlink_slot_unrefp) sd_netlink_slot *s1 = NULL, *s2 = NULL;
461         _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
462 
463         log_debug("/* %s */", __func__);
464 
465         assert_se(sd_netlink_open(&rtnl) >= 0);
466 
467         assert_se(sd_netlink_add_match(rtnl, &s1, RTM_NEWLINK, link_handler, NULL, NULL, NULL) >= 0);
468         assert_se(sd_netlink_add_match(rtnl, &s2, RTM_NEWLINK, link_handler, NULL, NULL, NULL) >= 0);
469         assert_se(sd_netlink_add_match(rtnl, NULL, RTM_NEWLINK, link_handler, NULL, NULL, NULL) >= 0);
470 
471         assert_se(!(s1 = sd_netlink_slot_unref(s1)));
472         assert_se(!(s2 = sd_netlink_slot_unref(s2)));
473 
474         assert_se((rtnl = sd_netlink_unref(rtnl)) == NULL);
475 }
476 
test_get_addresses(sd_netlink * rtnl)477 static void test_get_addresses(sd_netlink *rtnl) {
478         _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
479         sd_netlink_message *m;
480 
481         log_debug("/* %s */", __func__);
482 
483         assert_se(sd_rtnl_message_new_addr(rtnl, &req, RTM_GETADDR, 0, AF_UNSPEC) >= 0);
484         assert_se(sd_netlink_message_request_dump(req, true) >= 0);
485         assert_se(sd_netlink_call(rtnl, req, 0, &reply) >= 0);
486 
487         for (m = reply; m; m = sd_netlink_message_next(m)) {
488                 uint16_t type;
489                 unsigned char scope, flags;
490                 int family, ifindex;
491 
492                 assert_se(sd_netlink_message_get_type(m, &type) >= 0);
493                 assert_se(type == RTM_NEWADDR);
494 
495                 assert_se(sd_rtnl_message_addr_get_ifindex(m, &ifindex) >= 0);
496                 assert_se(sd_rtnl_message_addr_get_family(m, &family) >= 0);
497                 assert_se(sd_rtnl_message_addr_get_scope(m, &scope) >= 0);
498                 assert_se(sd_rtnl_message_addr_get_flags(m, &flags) >= 0);
499 
500                 assert_se(ifindex > 0);
501                 assert_se(IN_SET(family, AF_INET, AF_INET6));
502 
503                 log_info("got IPv%u address on ifindex %i", family == AF_INET ? 4: 6, ifindex);
504         }
505 }
506 
test_message(sd_netlink * rtnl)507 static void test_message(sd_netlink *rtnl) {
508         _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
509 
510         log_debug("/* %s */", __func__);
511 
512         assert_se(message_new_synthetic_error(rtnl, -ETIMEDOUT, 1, &m) >= 0);
513         assert_se(sd_netlink_message_get_errno(m) == -ETIMEDOUT);
514 }
515 
test_array(void)516 static void test_array(void) {
517         _cleanup_(sd_netlink_unrefp) sd_netlink *genl = NULL;
518         _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
519 
520         log_debug("/* %s */", __func__);
521 
522         assert_se(sd_genl_socket_open(&genl) >= 0);
523         assert_se(sd_genl_message_new(genl, CTRL_GENL_NAME, CTRL_CMD_GETFAMILY, &m) >= 0);
524 
525         assert_se(sd_netlink_message_open_container(m, CTRL_ATTR_MCAST_GROUPS) >= 0);
526         for (unsigned i = 0; i < 10; i++) {
527                 char name[STRLEN("hoge") + DECIMAL_STR_MAX(uint32_t)];
528                 uint32_t id = i + 1000;
529 
530                 xsprintf(name, "hoge%" PRIu32, id);
531                 assert_se(sd_netlink_message_open_array(m, i + 1) >= 0);
532                 assert_se(sd_netlink_message_append_u32(m, CTRL_ATTR_MCAST_GRP_ID, id) >= 0);
533                 assert_se(sd_netlink_message_append_string(m, CTRL_ATTR_MCAST_GRP_NAME, name) >= 0);
534                 assert_se(sd_netlink_message_close_container(m) >= 0);
535         }
536         assert_se(sd_netlink_message_close_container(m) >= 0);
537 
538         message_seal(m);
539         assert_se(sd_netlink_message_rewind(m, genl) >= 0);
540 
541         assert_se(sd_netlink_message_enter_container(m, CTRL_ATTR_MCAST_GROUPS) >= 0);
542         for (unsigned i = 0; i < 10; i++) {
543                 char expected[STRLEN("hoge") + DECIMAL_STR_MAX(uint32_t)];
544                 const char *name;
545                 uint32_t id;
546 
547                 assert_se(sd_netlink_message_enter_array(m, i + 1) >= 0);
548                 assert_se(sd_netlink_message_read_u32(m, CTRL_ATTR_MCAST_GRP_ID, &id) >= 0);
549                 assert_se(sd_netlink_message_read_string(m, CTRL_ATTR_MCAST_GRP_NAME, &name) >= 0);
550                 assert_se(sd_netlink_message_exit_container(m) >= 0);
551 
552                 assert_se(id == i + 1000);
553                 xsprintf(expected, "hoge%" PRIu32, id);
554                 assert_se(streq(name, expected));
555         }
556         assert_se(sd_netlink_message_exit_container(m) >= 0);
557 }
558 
test_strv(sd_netlink * rtnl)559 static void test_strv(sd_netlink *rtnl) {
560         _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
561         _cleanup_strv_free_ char **names_in = NULL, **names_out;
562         const char *p;
563 
564         log_debug("/* %s */", __func__);
565 
566         assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_NEWLINKPROP, 1) >= 0);
567 
568         for (unsigned i = 0; i < 10; i++) {
569                 char name[STRLEN("hoge") + DECIMAL_STR_MAX(uint32_t)];
570 
571                 xsprintf(name, "hoge%" PRIu32, i + 1000);
572                 assert_se(strv_extend(&names_in, name) >= 0);
573         }
574 
575         assert_se(sd_netlink_message_open_container(m, IFLA_PROP_LIST) >= 0);
576         assert_se(sd_netlink_message_append_strv(m, IFLA_ALT_IFNAME, names_in) >= 0);
577         assert_se(sd_netlink_message_close_container(m) >= 0);
578 
579         message_seal(m);
580         assert_se(sd_netlink_message_rewind(m, rtnl) >= 0);
581 
582         assert_se(sd_netlink_message_read_strv(m, IFLA_PROP_LIST, IFLA_ALT_IFNAME, &names_out) >= 0);
583         assert_se(strv_equal(names_in, names_out));
584 
585         assert_se(sd_netlink_message_enter_container(m, IFLA_PROP_LIST) >= 0);
586         assert_se(sd_netlink_message_read_string(m, IFLA_ALT_IFNAME, &p) >= 0);
587         assert_se(streq(p, "hoge1009"));
588         assert_se(sd_netlink_message_exit_container(m) >= 0);
589 }
590 
genl_ctrl_match_callback(sd_netlink * genl,sd_netlink_message * m,void * userdata)591 static int genl_ctrl_match_callback(sd_netlink *genl, sd_netlink_message *m, void *userdata) {
592         const char *name;
593         uint16_t id;
594         uint8_t cmd;
595 
596         assert_se(genl);
597         assert_se(m);
598 
599         assert_se(sd_genl_message_get_family_name(genl, m, &name) >= 0);
600         assert_se(streq(name, CTRL_GENL_NAME));
601 
602         assert_se(sd_genl_message_get_command(genl, m, &cmd) >= 0);
603 
604         switch (cmd) {
605         case CTRL_CMD_NEWFAMILY:
606         case CTRL_CMD_DELFAMILY:
607                 assert_se(sd_netlink_message_read_string(m, CTRL_ATTR_FAMILY_NAME, &name) >= 0);
608                 assert_se(sd_netlink_message_read_u16(m, CTRL_ATTR_FAMILY_ID, &id) >= 0);
609                 log_debug("%s: %s (id=%"PRIu16") family is %s.",
610                           __func__, name, id, cmd == CTRL_CMD_NEWFAMILY ? "added" : "removed");
611                 break;
612         case CTRL_CMD_NEWMCAST_GRP:
613         case CTRL_CMD_DELMCAST_GRP:
614                 assert_se(sd_netlink_message_read_string(m, CTRL_ATTR_FAMILY_NAME, &name) >= 0);
615                 assert_se(sd_netlink_message_read_u16(m, CTRL_ATTR_FAMILY_ID, &id) >= 0);
616                 log_debug("%s: multicast group for %s (id=%"PRIu16") family is %s.",
617                           __func__, name, id, cmd == CTRL_CMD_NEWMCAST_GRP ? "added" : "removed");
618                 break;
619         default:
620                 log_debug("%s: received nlctrl message with unknown command '%"PRIu8"'.", __func__, cmd);
621         }
622 
623         return 0;
624 }
625 
test_genl(void)626 static void test_genl(void) {
627         _cleanup_(sd_event_unrefp) sd_event *event = NULL;
628         _cleanup_(sd_netlink_unrefp) sd_netlink *genl = NULL;
629         _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
630         const char *name;
631         uint8_t cmd;
632         int r;
633 
634         log_debug("/* %s */", __func__);
635 
636         assert_se(sd_genl_socket_open(&genl) >= 0);
637         assert_se(sd_event_default(&event) >= 0);
638         assert_se(sd_netlink_attach_event(genl, event, 0) >= 0);
639 
640         assert_se(sd_genl_message_new(genl, CTRL_GENL_NAME, CTRL_CMD_GETFAMILY, &m) >= 0);
641         assert_se(sd_genl_message_get_family_name(genl, m, &name) >= 0);
642         assert_se(streq(name, CTRL_GENL_NAME));
643         assert_se(sd_genl_message_get_command(genl, m, &cmd) >= 0);
644         assert_se(cmd == CTRL_CMD_GETFAMILY);
645 
646         assert_se(sd_genl_add_match(genl, NULL, CTRL_GENL_NAME, "notify", 0, genl_ctrl_match_callback, NULL, NULL, "genl-ctrl-notify") >= 0);
647 
648         m = sd_netlink_message_unref(m);
649         assert_se(sd_genl_message_new(genl, "should-not-exist", CTRL_CMD_GETFAMILY, &m) < 0);
650         assert_se(sd_genl_message_new(genl, "should-not-exist", CTRL_CMD_GETFAMILY, &m) == -EOPNOTSUPP);
651 
652         /* These families may not be supported by kernel. Hence, ignore results. */
653         (void) sd_genl_message_new(genl, FOU_GENL_NAME, 0, &m);
654         m = sd_netlink_message_unref(m);
655         (void) sd_genl_message_new(genl, L2TP_GENL_NAME, 0, &m);
656         m = sd_netlink_message_unref(m);
657         (void) sd_genl_message_new(genl, MACSEC_GENL_NAME, 0, &m);
658         m = sd_netlink_message_unref(m);
659         (void) sd_genl_message_new(genl, NL80211_GENL_NAME, 0, &m);
660 
661         for (;;) {
662                 r = sd_event_run(event, 500 * USEC_PER_MSEC);
663                 assert_se(r >= 0);
664                 if (r == 0)
665                         return;
666         }
667 }
668 
main(void)669 int main(void) {
670         sd_netlink *rtnl;
671         sd_netlink_message *m;
672         sd_netlink_message *r;
673         const char *string_data;
674         int if_loopback;
675         uint16_t type;
676 
677         test_setup_logging(LOG_DEBUG);
678 
679         test_match();
680         test_multiple();
681 
682         assert_se(sd_netlink_open(&rtnl) >= 0);
683         assert_se(rtnl);
684 
685         test_route(rtnl);
686         test_message(rtnl);
687         test_container(rtnl);
688         test_array();
689         test_strv(rtnl);
690 
691         if_loopback = (int) if_nametoindex("lo");
692         assert_se(if_loopback > 0);
693 
694         test_async(if_loopback);
695         test_slot_set(if_loopback);
696         test_async_destroy_callback(if_loopback);
697         test_pipe(if_loopback);
698         test_event_loop(if_loopback);
699         test_link_configure(rtnl, if_loopback);
700 
701         test_get_addresses(rtnl);
702         test_message_link_bridge(rtnl);
703 
704         assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_GETLINK, if_loopback) >= 0);
705         assert_se(m);
706 
707         assert_se(sd_netlink_message_get_type(m, &type) >= 0);
708         assert_se(type == RTM_GETLINK);
709 
710         assert_se(sd_netlink_message_read_string(m, IFLA_IFNAME, &string_data) == -EPERM);
711 
712         assert_se(sd_netlink_call(rtnl, m, 0, &r) == 1);
713         assert_se(sd_netlink_message_get_type(r, &type) >= 0);
714         assert_se(type == RTM_NEWLINK);
715 
716         assert_se((r = sd_netlink_message_unref(r)) == NULL);
717 
718         assert_se(sd_netlink_call(rtnl, m, -1, &r) == -EPERM);
719         assert_se((m = sd_netlink_message_unref(m)) == NULL);
720         assert_se((r = sd_netlink_message_unref(r)) == NULL);
721 
722         test_link_get(rtnl, if_loopback);
723         test_address_get(rtnl, if_loopback);
724 
725         assert_se((m = sd_netlink_message_unref(m)) == NULL);
726         assert_se((r = sd_netlink_message_unref(r)) == NULL);
727         assert_se((rtnl = sd_netlink_unref(rtnl)) == NULL);
728 
729         test_genl();
730 
731         return EXIT_SUCCESS;
732 }
733