forked from adamlaska/systemd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnetworkd-route.c
More file actions
2898 lines (2337 loc) · 97.3 KB
/
networkd-route.c
File metadata and controls
2898 lines (2337 loc) · 97.3 KB
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <linux/icmpv6.h>
#include <linux/ipv6_route.h>
#include <linux/nexthop.h>
#include "alloc-util.h"
#include "event-util.h"
#include "netlink-util.h"
#include "networkd-address.h"
#include "networkd-ipv4ll.h"
#include "networkd-manager.h"
#include "networkd-network.h"
#include "networkd-nexthop.h"
#include "networkd-queue.h"
#include "networkd-route-util.h"
#include "networkd-route.h"
#include "parse-util.h"
#include "string-util.h"
#include "strv.h"
#include "vrf.h"
#include "wireguard.h"
int route_new(Route **ret) {
_cleanup_(route_freep) Route *route = NULL;
route = new(Route, 1);
if (!route)
return -ENOMEM;
*route = (Route) {
.family = AF_UNSPEC,
.scope = RT_SCOPE_UNIVERSE,
.protocol = RTPROT_UNSPEC,
.type = RTN_UNICAST,
.table = RT_TABLE_MAIN,
.lifetime_usec = USEC_INFINITY,
.quickack = -1,
.fast_open_no_cookie = -1,
.gateway_onlink = -1,
.ttl_propagate = -1,
};
*ret = TAKE_PTR(route);
return 0;
}
static int route_new_static(Network *network, const char *filename, unsigned section_line, Route **ret) {
_cleanup_(config_section_freep) ConfigSection *n = NULL;
_cleanup_(route_freep) Route *route = NULL;
int r;
assert(network);
assert(ret);
assert(filename);
assert(section_line > 0);
r = config_section_new(filename, section_line, &n);
if (r < 0)
return r;
route = hashmap_get(network->routes_by_section, n);
if (route) {
*ret = TAKE_PTR(route);
return 0;
}
if (hashmap_size(network->routes_by_section) >= routes_max())
return -E2BIG;
r = route_new(&route);
if (r < 0)
return r;
route->protocol = RTPROT_STATIC;
route->network = network;
route->section = TAKE_PTR(n);
route->source = NETWORK_CONFIG_SOURCE_STATIC;
r = hashmap_ensure_put(&network->routes_by_section, &config_section_hash_ops, route->section, route);
if (r < 0)
return r;
*ret = TAKE_PTR(route);
return 0;
}
Route *route_free(Route *route) {
if (!route)
return NULL;
if (route->network) {
assert(route->section);
hashmap_remove(route->network->routes_by_section, route->section);
}
config_section_free(route->section);
if (route->link)
set_remove(route->link->routes, route);
if (route->manager)
set_remove(route->manager->routes, route);
ordered_set_free_with_destructor(route->multipath_routes, multipath_route_free);
sd_event_source_disable_unref(route->expire);
return mfree(route);
}
static void route_hash_func(const Route *route, struct siphash *state) {
assert(route);
siphash24_compress(&route->family, sizeof(route->family), state);
switch (route->family) {
case AF_INET:
case AF_INET6:
siphash24_compress(&route->dst_prefixlen, sizeof(route->dst_prefixlen), state);
siphash24_compress(&route->dst, FAMILY_ADDRESS_SIZE(route->family), state);
siphash24_compress(&route->src_prefixlen, sizeof(route->src_prefixlen), state);
siphash24_compress(&route->src, FAMILY_ADDRESS_SIZE(route->family), state);
siphash24_compress(&route->gw_family, sizeof(route->gw_family), state);
if (IN_SET(route->gw_family, AF_INET, AF_INET6)) {
siphash24_compress(&route->gw, FAMILY_ADDRESS_SIZE(route->gw_family), state);
siphash24_compress(&route->gw_weight, sizeof(route->gw_weight), state);
}
siphash24_compress(&route->prefsrc, FAMILY_ADDRESS_SIZE(route->family), state);
siphash24_compress(&route->tos, sizeof(route->tos), state);
siphash24_compress(&route->priority, sizeof(route->priority), state);
siphash24_compress(&route->table, sizeof(route->table), state);
siphash24_compress(&route->protocol, sizeof(route->protocol), state);
siphash24_compress(&route->scope, sizeof(route->scope), state);
siphash24_compress(&route->type, sizeof(route->type), state);
siphash24_compress(&route->initcwnd, sizeof(route->initcwnd), state);
siphash24_compress(&route->initrwnd, sizeof(route->initrwnd), state);
siphash24_compress(&route->advmss, sizeof(route->advmss), state);
siphash24_compress(&route->nexthop_id, sizeof(route->nexthop_id), state);
break;
default:
/* treat any other address family as AF_UNSPEC */
break;
}
}
static int route_compare_func(const Route *a, const Route *b) {
int r;
r = CMP(a->family, b->family);
if (r != 0)
return r;
switch (a->family) {
case AF_INET:
case AF_INET6:
r = CMP(a->dst_prefixlen, b->dst_prefixlen);
if (r != 0)
return r;
r = memcmp(&a->dst, &b->dst, FAMILY_ADDRESS_SIZE(a->family));
if (r != 0)
return r;
r = CMP(a->src_prefixlen, b->src_prefixlen);
if (r != 0)
return r;
r = memcmp(&a->src, &b->src, FAMILY_ADDRESS_SIZE(a->family));
if (r != 0)
return r;
r = CMP(a->gw_family, b->gw_family);
if (r != 0)
return r;
if (IN_SET(a->gw_family, AF_INET, AF_INET6)) {
r = memcmp(&a->gw, &b->gw, FAMILY_ADDRESS_SIZE(a->family));
if (r != 0)
return r;
r = CMP(a->gw_weight, b->gw_weight);
if (r != 0)
return r;
}
r = memcmp(&a->prefsrc, &b->prefsrc, FAMILY_ADDRESS_SIZE(a->family));
if (r != 0)
return r;
r = CMP(a->tos, b->tos);
if (r != 0)
return r;
r = CMP(a->priority, b->priority);
if (r != 0)
return r;
r = CMP(a->table, b->table);
if (r != 0)
return r;
r = CMP(a->protocol, b->protocol);
if (r != 0)
return r;
r = CMP(a->scope, b->scope);
if (r != 0)
return r;
r = CMP(a->type, b->type);
if (r != 0)
return r;
r = CMP(a->initcwnd, b->initcwnd);
if (r != 0)
return r;
r = CMP(a->initrwnd, b->initrwnd);
if (r != 0)
return r;
r = CMP(a->advmss, b->advmss);
if (r != 0)
return r;
r = CMP(a->nexthop_id, b->nexthop_id);
if (r != 0)
return r;
return 0;
default:
/* treat any other address family as AF_UNSPEC */
return 0;
}
}
DEFINE_HASH_OPS_WITH_KEY_DESTRUCTOR(
route_hash_ops,
Route,
route_hash_func,
route_compare_func,
route_free);
static bool route_type_is_reject(const Route *route) {
assert(route);
return IN_SET(route->type, RTN_UNREACHABLE, RTN_PROHIBIT, RTN_BLACKHOLE, RTN_THROW);
}
static bool route_needs_convert(const Route *route) {
assert(route);
return route->nexthop_id > 0 || !ordered_set_isempty(route->multipath_routes);
}
static int route_add(Manager *manager, Link *link, Route *route) {
int r;
assert(route);
if (route_type_is_reject(route)) {
assert(manager);
r = set_ensure_put(&manager->routes, &route_hash_ops, route);
if (r < 0)
return r;
if (r == 0)
return -EEXIST;
route->manager = manager;
} else {
assert(link);
r = set_ensure_put(&link->routes, &route_hash_ops, route);
if (r < 0)
return r;
if (r == 0)
return -EEXIST;
route->link = link;
}
return 0;
}
int route_get(Manager *manager, Link *link, const Route *in, Route **ret) {
Route *route;
assert(in);
if (route_type_is_reject(in)) {
if (!manager)
return -ENOENT;
route = set_get(manager->routes, in);
} else {
if (!link)
return -ENOENT;
route = set_get(link->routes, in);
}
if (!route)
return -ENOENT;
if (ret)
*ret = route;
return 0;
}
int route_dup(const Route *src, Route **ret) {
_cleanup_(route_freep) Route *dest = NULL;
/* This does not copy mulipath routes. */
assert(src);
assert(ret);
dest = newdup(Route, src, 1);
if (!dest)
return -ENOMEM;
/* Unset all pointers */
dest->network = NULL;
dest->section = NULL;
dest->link = NULL;
dest->manager = NULL;
dest->multipath_routes = NULL;
dest->expire = NULL;
*ret = TAKE_PTR(dest);
return 0;
}
static void route_apply_nexthop(Route *route, const NextHop *nh, uint8_t nh_weight) {
assert(route);
assert(nh);
assert(hashmap_isempty(nh->group));
route->gw_family = nh->family;
route->gw = nh->gw;
if (nh_weight != UINT8_MAX)
route->gw_weight = nh_weight;
if (nh->blackhole)
route->type = RTN_BLACKHOLE;
}
static void route_apply_multipath_route(Route *route, const MultipathRoute *m) {
assert(route);
assert(m);
route->gw_family = m->gateway.family;
route->gw = m->gateway.address;
route->gw_weight = m->weight;
}
static int multipath_route_get_link(Manager *manager, const MultipathRoute *m, Link **ret) {
int r;
assert(manager);
assert(m);
if (m->ifname) {
r = link_get_by_name(manager, m->ifname, ret);
return r < 0 ? r : 1;
} else if (m->ifindex > 0) { /* Always ignore ifindex if ifname is set. */
r = link_get_by_index(manager, m->ifindex, ret);
return r < 0 ? r : 1;
}
if (ret)
*ret = NULL;
return 0;
}
typedef struct ConvertedRoutes {
size_t n;
Route **routes;
Link **links;
} ConvertedRoutes;
static ConvertedRoutes *converted_routes_free(ConvertedRoutes *c) {
if (!c)
return NULL;
for (size_t i = 0; i < c->n; i++)
route_free(c->routes[i]);
free(c->routes);
free(c->links);
return mfree(c);
}
DEFINE_TRIVIAL_CLEANUP_FUNC(ConvertedRoutes*, converted_routes_free);
static int converted_routes_new(size_t n, ConvertedRoutes **ret) {
_cleanup_(converted_routes_freep) ConvertedRoutes *c = NULL;
_cleanup_free_ Route **routes = NULL;
_cleanup_free_ Link **links = NULL;
assert(n > 0);
assert(ret);
routes = new0(Route*, n);
if (!routes)
return -ENOMEM;
links = new0(Link*, n);
if (!links)
return -ENOMEM;
c = new(ConvertedRoutes, 1);
if (!c)
return -ENOMEM;
*c = (ConvertedRoutes) {
.n = n,
.routes = TAKE_PTR(routes),
.links = TAKE_PTR(links),
};
*ret = TAKE_PTR(c);
return 0;
}
static int route_convert(Manager *manager, const Route *route, ConvertedRoutes **ret) {
_cleanup_(converted_routes_freep) ConvertedRoutes *c = NULL;
int r;
assert(manager);
assert(route);
assert(ret);
if (!route_needs_convert(route)) {
*ret = NULL;
return 0;
}
if (route->nexthop_id > 0) {
struct nexthop_grp *nhg;
NextHop *nh;
r = manager_get_nexthop_by_id(manager, route->nexthop_id, &nh);
if (r < 0)
return r;
if (hashmap_isempty(nh->group)) {
r = converted_routes_new(1, &c);
if (r < 0)
return r;
r = route_dup(route, &c->routes[0]);
if (r < 0)
return r;
route_apply_nexthop(c->routes[0], nh, UINT8_MAX);
c->links[0] = nh->link;
*ret = TAKE_PTR(c);
return 1;
}
r = converted_routes_new(hashmap_size(nh->group), &c);
if (r < 0)
return r;
size_t i = 0;
HASHMAP_FOREACH(nhg, nh->group) {
NextHop *h;
r = manager_get_nexthop_by_id(manager, nhg->id, &h);
if (r < 0)
return r;
r = route_dup(route, &c->routes[i]);
if (r < 0)
return r;
route_apply_nexthop(c->routes[i], h, nhg->weight);
c->links[i] = h->link;
i++;
}
*ret = TAKE_PTR(c);
return 1;
}
assert(!ordered_set_isempty(route->multipath_routes));
r = converted_routes_new(ordered_set_size(route->multipath_routes), &c);
if (r < 0)
return r;
size_t i = 0;
MultipathRoute *m;
ORDERED_SET_FOREACH(m, route->multipath_routes) {
r = route_dup(route, &c->routes[i]);
if (r < 0)
return r;
route_apply_multipath_route(c->routes[i], m);
r = multipath_route_get_link(manager, m, &c->links[i]);
if (r < 0)
return r;
i++;
}
*ret = TAKE_PTR(c);
return 1;
}
void link_mark_routes(Link *link, NetworkConfigSource source, const struct in6_addr *router) {
Route *route;
assert(link);
SET_FOREACH(route, link->routes) {
if (route->source != source)
continue;
if (source == NETWORK_CONFIG_SOURCE_NDISC &&
router && !in6_addr_equal(router, &route->provider.in6))
continue;
route_mark(route);
}
}
static void log_route_debug(const Route *route, const char *str, const Link *link, const Manager *manager) {
_cleanup_free_ char *state = NULL, *dst = NULL, *src = NULL, *gw_alloc = NULL, *prefsrc = NULL,
*table = NULL, *scope = NULL, *proto = NULL, *flags = NULL;
const char *gw = NULL;
assert(route);
assert(str);
assert(manager);
/* link may be NULL. */
if (!DEBUG_LOGGING)
return;
(void) network_config_state_to_string_alloc(route->state, &state);
if (in_addr_is_set(route->family, &route->dst) || route->dst_prefixlen > 0)
(void) in_addr_prefix_to_string(route->family, &route->dst, route->dst_prefixlen, &dst);
if (in_addr_is_set(route->family, &route->src) || route->src_prefixlen > 0)
(void) in_addr_prefix_to_string(route->family, &route->src, route->src_prefixlen, &src);
if (in_addr_is_set(route->gw_family, &route->gw)) {
(void) in_addr_to_string(route->gw_family, &route->gw, &gw_alloc);
gw = gw_alloc;
} else if (route->gateway_from_dhcp_or_ra) {
if (route->gw_family == AF_INET)
gw = "_dhcp4";
else if (route->gw_family == AF_INET6)
gw = "_ipv6ra";
} else {
MultipathRoute *m;
ORDERED_SET_FOREACH(m, route->multipath_routes) {
_cleanup_free_ char *buf = NULL;
union in_addr_union a = m->gateway.address;
(void) in_addr_to_string(m->gateway.family, &a, &buf);
(void) strextend_with_separator(&gw_alloc, ",", strna(buf));
if (m->ifname)
(void) strextend(&gw_alloc, "@", m->ifname);
else if (m->ifindex > 0)
(void) strextendf(&gw_alloc, "@%"PRIu32, m->ifindex);
/* See comments in config_parse_multipath_route(). */
(void) strextendf(&gw_alloc, ":%"PRIu32, m->weight + 1);
}
gw = gw_alloc;
}
if (in_addr_is_set(route->family, &route->prefsrc))
(void) in_addr_to_string(route->family, &route->prefsrc, &prefsrc);
(void) route_scope_to_string_alloc(route->scope, &scope);
(void) manager_get_route_table_to_string(manager, route->table, &table);
(void) route_protocol_full_to_string_alloc(route->protocol, &proto);
(void) route_flags_to_string_alloc(route->flags, &flags);
log_link_debug(link,
"%s %s route (%s): dst: %s, src: %s, gw: %s, prefsrc: %s, scope: %s, table: %s, "
"proto: %s, type: %s, nexthop: %"PRIu32", priority: %"PRIu32", flags: %s",
str, strna(network_config_source_to_string(route->source)), strna(state),
strna(dst), strna(src), strna(gw), strna(prefsrc),
strna(scope), strna(table), strna(proto),
strna(route_type_to_string(route->type)),
route->nexthop_id, route->priority, strna(flags));
}
static int route_set_netlink_message(const Route *route, sd_netlink_message *req, Link *link) {
int r;
assert(route);
assert(req);
/* link may be NULL */
if (in_addr_is_set(route->gw_family, &route->gw) && route->nexthop_id == 0) {
if (route->gw_family == route->family) {
r = netlink_message_append_in_addr_union(req, RTA_GATEWAY, route->gw_family, &route->gw);
if (r < 0)
return r;
} else {
RouteVia rtvia = {
.family = route->gw_family,
.address = route->gw,
};
r = sd_netlink_message_append_data(req, RTA_VIA, &rtvia, sizeof(rtvia));
if (r < 0)
return r;
}
}
if (route->dst_prefixlen > 0) {
r = netlink_message_append_in_addr_union(req, RTA_DST, route->family, &route->dst);
if (r < 0)
return r;
r = sd_rtnl_message_route_set_dst_prefixlen(req, route->dst_prefixlen);
if (r < 0)
return r;
}
if (route->src_prefixlen > 0) {
r = netlink_message_append_in_addr_union(req, RTA_SRC, route->family, &route->src);
if (r < 0)
return r;
r = sd_rtnl_message_route_set_src_prefixlen(req, route->src_prefixlen);
if (r < 0)
return r;
}
if (in_addr_is_set(route->family, &route->prefsrc)) {
r = netlink_message_append_in_addr_union(req, RTA_PREFSRC, route->family, &route->prefsrc);
if (r < 0)
return r;
}
r = sd_rtnl_message_route_set_scope(req, route->scope);
if (r < 0)
return r;
r = sd_rtnl_message_route_set_flags(req, route->flags & RTNH_F_ONLINK);
if (r < 0)
return r;
if (route->table < 256) {
r = sd_rtnl_message_route_set_table(req, route->table);
if (r < 0)
return r;
} else {
r = sd_rtnl_message_route_set_table(req, RT_TABLE_UNSPEC);
if (r < 0)
return r;
/* Table attribute to allow more than 256. */
r = sd_netlink_message_append_u32(req, RTA_TABLE, route->table);
if (r < 0)
return r;
}
if (!route_type_is_reject(route) &&
route->nexthop_id == 0 &&
ordered_set_isempty(route->multipath_routes)) {
assert(link); /* Those routes must be attached to a specific link */
r = sd_netlink_message_append_u32(req, RTA_OIF, link->ifindex);
if (r < 0)
return r;
}
if (route->nexthop_id > 0) {
r = sd_netlink_message_append_u32(req, RTA_NH_ID, route->nexthop_id);
if (r < 0)
return r;
}
r = sd_netlink_message_append_u8(req, RTA_PREF, route->pref);
if (r < 0)
return r;
r = sd_netlink_message_append_u32(req, RTA_PRIORITY, route->priority);
if (r < 0)
return r;
return 0;
}
static int route_remove_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
int r;
assert(m);
/* link may be NULL. */
if (link && IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
return 0;
r = sd_netlink_message_get_errno(m);
if (r < 0 && r != -ESRCH)
log_link_message_warning_errno(link, m, r, "Could not drop route, ignoring");
return 1;
}
int route_remove(Route *route) {
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
unsigned char type;
Manager *manager;
Link *link;
int r;
assert(route);
assert(route->manager || (route->link && route->link->manager));
assert(IN_SET(route->family, AF_INET, AF_INET6));
link = route->link;
manager = route->manager ?: link->manager;
log_route_debug(route, "Removing", link, manager);
r = sd_rtnl_message_new_route(manager->rtnl, &req,
RTM_DELROUTE, route->family,
route->protocol);
if (r < 0)
return log_link_error_errno(link, r, "Could not create netlink message: %m");
if (route->family == AF_INET && route->nexthop_id > 0 && route->type == RTN_BLACKHOLE)
/* When IPv4 route has nexthop id and the nexthop type is blackhole, even though kernel
* sends RTM_NEWROUTE netlink message with blackhole type, kernel's internal route type
* fib_rt_info::type may not be blackhole. Thus, we cannot know the internal value.
* Moreover, on route removal, the matching is done with the hidden value if we set
* non-zero type in RTM_DELROUTE message. Note, sd_rtnl_message_new_route() sets
* RTN_UNICAST by default. So, we need to clear the type here. */
type = RTN_UNSPEC;
else
type = route->type;
r = sd_rtnl_message_route_set_type(req, type);
if (r < 0)
return log_link_error_errno(link, r, "Could not set route type: %m");
r = route_set_netlink_message(route, req, link);
if (r < 0)
return log_error_errno(r, "Could not fill netlink message: %m");
r = netlink_call_async(manager->rtnl, NULL, req, route_remove_handler,
link ? link_netlink_destroy_callback : NULL, link);
if (r < 0)
return log_link_error_errno(link, r, "Could not send netlink message: %m");
link_ref(link);
route_enter_removing(route);
return 0;
}
static void manager_mark_routes(Manager *manager, bool foreign, const Link *except) {
Route *route;
Link *link;
int r;
assert(manager);
/* First, mark all routes. */
SET_FOREACH(route, manager->routes) {
/* Do not touch routes managed by the kernel. */
if (route->protocol == RTPROT_KERNEL)
continue;
/* When 'foreign' is true, mark only foreign routes, and vice versa. */
if (foreign != (route->source == NETWORK_CONFIG_SOURCE_FOREIGN))
continue;
/* Do not touch dynamic routes. They will removed by dhcp_pd_prefix_lost() */
if (IN_SET(route->source, NETWORK_CONFIG_SOURCE_DHCP4, NETWORK_CONFIG_SOURCE_DHCP6))
continue;
/* Ignore routes not assigned yet or already removed. */
if (!route_exists(route))
continue;
route_mark(route);
}
/* Then, unmark all routes requested by active links. */
HASHMAP_FOREACH(link, manager->links_by_index) {
if (link == except)
continue;
if (!link->network)
continue;
if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED))
continue;
HASHMAP_FOREACH(route, link->network->routes_by_section) {
_cleanup_(converted_routes_freep) ConvertedRoutes *converted = NULL;
Route *existing;
r = route_convert(manager, route, &converted);
if (r < 0)
continue;
if (r == 0) {
if (route_get(manager, NULL, route, &existing) >= 0)
route_unmark(existing);
continue;
}
for (size_t i = 0; i < converted->n; i++)
if (route_get(manager, NULL, converted->routes[i], &existing) >= 0)
route_unmark(existing);
}
}
}
static int manager_drop_marked_routes(Manager *manager) {
Route *route;
int k, r = 0;
assert(manager);
SET_FOREACH(route, manager->routes) {
if (!route_is_marked(route))
continue;
k = route_remove(route);
if (k < 0 && r >= 0)
r = k;
}
return r;
}
static bool route_by_kernel(const Route *route) {
assert(route);
if (route->protocol == RTPROT_KERNEL)
return true;
/* The kernels older than a826b04303a40d52439aa141035fca5654ccaccd (v5.11) create the IPv6
* multicast with RTPROT_BOOT. Do not touch it. */
if (route->protocol == RTPROT_BOOT &&
route->family == AF_INET6 &&
route->dst_prefixlen == 8 &&
in6_addr_equal(&route->dst.in6, & (struct in6_addr) {{{ 0xff,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0 }}}))
return true;
return false;
}
static void link_unmark_wireguard_routes(Link *link) {
Route *route, *existing;
Wireguard *w;
assert(link);
w = WIREGUARD(link->netdev);
if (!w)
return;
SET_FOREACH(route, w->routes)
if (route_get(NULL, link, route, &existing) >= 0)
route_unmark(existing);
}
int link_drop_foreign_routes(Link *link) {
Route *route;
int k, r;
assert(link);
assert(link->manager);
assert(link->network);
SET_FOREACH(route, link->routes) {
/* do not touch routes managed by the kernel */
if (route_by_kernel(route))
continue;
/* Do not remove routes we configured. */
if (route->source != NETWORK_CONFIG_SOURCE_FOREIGN)
continue;
/* Ignore routes not assigned yet or already removed. */
if (!route_exists(route))
continue;
if (route->protocol == RTPROT_STATIC &&
FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_STATIC))
continue;
if (route->protocol == RTPROT_DHCP &&
FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP))
continue;
route_mark(route);
}
HASHMAP_FOREACH(route, link->network->routes_by_section) {
_cleanup_(converted_routes_freep) ConvertedRoutes *converted = NULL;
Route *existing;
r = route_convert(link->manager, route, &converted);
if (r < 0)
continue;
if (r == 0) {
if (route_get(NULL, link, route, &existing) >= 0)
route_unmark(existing);
continue;
}
for (size_t i = 0; i < converted->n; i++)
if (route_get(NULL, link, converted->routes[i], &existing) >= 0)
route_unmark(existing);
}
link_unmark_wireguard_routes(link);
r = 0;
SET_FOREACH(route, link->routes) {
if (!route_is_marked(route))
continue;
k = route_remove(route);
if (k < 0 && r >= 0)
r = k;
}
manager_mark_routes(link->manager, /* foreign = */ true, NULL);
k = manager_drop_marked_routes(link->manager);
if (k < 0 && r >= 0)
r = k;
return r;
}
int link_drop_managed_routes(Link *link) {
Route *route;
int k, r = 0;
assert(link);
SET_FOREACH(route, link->routes) {
/* do not touch routes managed by the kernel */
if (route_by_kernel(route))
continue;
/* Do not touch routes managed by kernel or other tools. */
if (route->source == NETWORK_CONFIG_SOURCE_FOREIGN)
continue;
if (!route_exists(route))
continue;
k = route_remove(route);
if (k < 0 && r >= 0)
r = k;
}
manager_mark_routes(link->manager, /* foreign = */ false, link);
k = manager_drop_marked_routes(link->manager);
if (k < 0 && r >= 0)
r = k;
return r;
}
void link_foreignize_routes(Link *link) {
Route *route;
assert(link);
SET_FOREACH(route, link->routes)
route->source = NETWORK_CONFIG_SOURCE_FOREIGN;
manager_mark_routes(link->manager, /* foreign = */ false, link);
SET_FOREACH(route, link->manager->routes) {