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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include "libnm-glib-aux/nm-default-glib-i18n-lib.h"
#include "nmp-global-tracker.h"
#include <linux/fib_rules.h>
#include <linux/rtnetlink.h>
#include "libnm-log-core/nm-logging.h"
#include "libnm-std-aux/c-list-util.h"
#include "nmp-object.h"
/* This limit comes from kernel, and it limits the number of MPTCP addresses
* we can configure. */
#define MPTCP_PM_ADDR_MAX 8
/*****************************************************************************/
/* NMPGlobalTracker tracks certain objects for the entire network namespace and can
* commit them.
*
* We tend to configure things per-interface and per-profile. In many cases,
* we thereby only need to care about the things for that interface. For example,
* we can configure IP addresses and (unicast) routes without having a system wide
* view. That is mainly, because such objects are themselves tied to an ifindex.
*
* However, for certain objects that's not the case. For example, policy routing
* rules, certain route types (blackhole, unreachable, prohibit, throw) and MPTCP
* endpoints require a holistic view of the system. That is, because rules and
* these route types have no ifindex. For MPTCP endpoints, they have an ifindex,
* however we can only configure a small number of them at a time, so we need a
* central (global) instance that can track which endpoints to configure.
*
* In general, the NMPGlobalTracker tracks objects for the entire namespace, and
* it's sync() method will figure out how to configure them.
*
* Since the users of NMPGloablTracker (NML3Cfg, NMDevice) themselves don't
* have this holistic view, the API of NMPGlobalTracker allows them to track
* individual objects independently (they register their objects for a private
* user-tag). If multiple such independent users track the same object, the tracking
* priority (track_priority_val) determines which one wins.
*
* NMPGlobalTracker can not only track whether an object should be present,
* it also can track whether it should be absent. See track_priority_present.
*/
/*****************************************************************************/
struct _NMPGlobalTracker {
NMPlatform *platform;
GHashTable *by_obj;
GHashTable *by_user_tag;
GHashTable *by_data;
CList by_obj_lst_heads[4];
guint ref_count;
};
/*****************************************************************************/
#define _NMLOG_DOMAIN LOGD_PLATFORM
#define _NMLOG_PREFIX_NAME "global-tracker"
#define _NMLOG(level, ...) __NMLOG_DEFAULT(level, LOGD_PLATFORM, _NMLOG_PREFIX_NAME, __VA_ARGS__)
/*****************************************************************************/
static gboolean
NMP_IS_GLOBAL_TRACKER(gpointer self)
{
return self && ((NMPGlobalTracker *) self)->ref_count > 0
&& NM_IS_PLATFORM(((NMPGlobalTracker *) self)->platform);
}
/*****************************************************************************/
typedef struct {
const NMPObject *obj;
gconstpointer user_tag;
CList obj_lst;
CList user_tag_lst;
/* @track_priority_val zero is special: those are weakly tracked objects.
* That means: NetworkManager will restore them only if it removed them earlier.
* But it will not remove or add them otherwise.
*
* Otherwise, @track_priority_val goes together with @track_priority_present.
* In case of one object being tracked multiple times (with different priorities),
* the one with higher priority wins. See _track_obj_data_get_best_data().
* Then, the winning present state either enforces that the rule is present
* or absent.
*
* If an object is not tracked at all, it is ignored by NetworkManager (except
* for MPTCP endpoints for the tracked interface). Assuming that it was added
* externally by the user. But unlike weakly tracked rules, NM will *not* restore
* such rules if NetworkManager themself removed them. */
guint32 track_priority_val;
bool track_priority_present : 1;
/* Calling nmp_global_tracker_track() will ensure that the tracked entry is
* non-dirty. Together with nmp_global_tracker_set_dirty() and nmp_global_tracker_untrack_all()'s
* @all parameter, this can be used to remove stale entries. */
bool dirty : 1;
} TrackData;
typedef enum {
CONFIG_STATE_NONE = 0,
CONFIG_STATE_ADDED_BY_US = 1,
CONFIG_STATE_REMOVED_BY_US = 2,
/* ConfigState encodes whether the object was touched by us at all (CONFIG_STATE_NONE).
*
* Maybe we would only need to track whether we touched the object at all. But we
* track it more in detail what we did: did we add it (CONFIG_STATE_ADDED_BY_US)
* or did we remove it (CONFIG_STATE_REMOVED_BY_US)?
* Finally, we need CONFIG_STATE_OWNED_BY_US, which means that we didn't actively
* add/remove it, but whenever we are about to undo the add/remove, we need to do it.
* In that sense, CONFIG_STATE_OWNED_BY_US is really just a flag that we unconditionally
* force the state next time when necessary. */
CONFIG_STATE_OWNED_BY_US = 3,
} ConfigState;
typedef struct {
const NMPObject *obj;
CList obj_lst_head;
CList by_obj_lst;
/* indicates whether we configured/removed the object (during sync()). We need that, so
* if the object gets untracked, that we know to remove/restore it.
*
* This makes NMPGlobalTracker stateful (beyond the configuration that indicates
* which objects are tracked).
* After a restart, NetworkManager would no longer remember which objects were added
* by us.
*
* That is partially fixed by NetworkManager taking over the objects that it
* actively configures (see %NMP_GLOBAL_TRACKER_EXTERN_WEAKLY_TRACKED_USER_TAG). */
ConfigState config_state;
} TrackObjData;
typedef struct {
gconstpointer user_tag;
CList user_tag_lst_head;
} TrackUserTagData;
/*****************************************************************************/
static void _track_data_untrack(NMPGlobalTracker *self,
TrackData *track_data,
gboolean remove_user_tag_data,
gboolean make_owned_by_us);
/*****************************************************************************/
static CList *
_by_obj_lst_head(NMPGlobalTracker *self, NMPObjectType obj_type)
{
G_STATIC_ASSERT(G_N_ELEMENTS(self->by_obj_lst_heads) == 4);
switch (obj_type) {
case NMP_OBJECT_TYPE_IP4_ROUTE:
return &self->by_obj_lst_heads[0];
case NMP_OBJECT_TYPE_IP6_ROUTE:
return &self->by_obj_lst_heads[1];
case NMP_OBJECT_TYPE_ROUTING_RULE:
return &self->by_obj_lst_heads[2];
case NMP_OBJECT_TYPE_MPTCP_ADDR:
return &self->by_obj_lst_heads[3];
default:
return nm_assert_unreachable_val(NULL);
}
}
/*****************************************************************************/
static void
_track_data_assert(const TrackData *track_data, gboolean linked)
{
nm_assert(track_data);
nm_assert(NM_IN_SET(NMP_OBJECT_GET_TYPE(track_data->obj),
NMP_OBJECT_TYPE_IP4_ROUTE,
NMP_OBJECT_TYPE_IP6_ROUTE,
NMP_OBJECT_TYPE_ROUTING_RULE,
NMP_OBJECT_TYPE_MPTCP_ADDR));
nm_assert(nmp_object_is_visible(track_data->obj));
nm_assert(track_data->user_tag);
nm_assert(!linked || !c_list_is_empty(&track_data->obj_lst));
nm_assert(!linked || !c_list_is_empty(&track_data->user_tag_lst));
}
static guint
_track_data_hash(gconstpointer data)
{
const TrackData *track_data = data;
NMHashState h;
_track_data_assert(track_data, FALSE);
nm_hash_init(&h, 269297543u);
nmp_object_hash_update(track_data->obj, &h);
nm_hash_update_val(&h, track_data->user_tag);
return nm_hash_complete(&h);
}
static gboolean
_track_data_equal(gconstpointer data_a, gconstpointer data_b)
{
const TrackData *track_data_a = data_a;
const TrackData *track_data_b = data_b;
_track_data_assert(track_data_a, FALSE);
_track_data_assert(track_data_b, FALSE);
return track_data_a->user_tag == track_data_b->user_tag
&& nmp_object_equal(track_data_a->obj, track_data_b->obj);
}
static void
_track_data_destroy(gpointer data)
{
TrackData *track_data = data;
_track_data_assert(track_data, FALSE);
c_list_unlink_stale(&track_data->obj_lst);
c_list_unlink_stale(&track_data->user_tag_lst);
nmp_object_unref(track_data->obj);
nm_g_slice_free(track_data);
}
static const TrackData *
_track_obj_data_get_best_data(TrackObjData *obj_data)
{
TrackData *track_data;
const TrackData *td_best = NULL;
c_list_for_each_entry (track_data, &obj_data->obj_lst_head, obj_lst) {
_track_data_assert(track_data, TRUE);
if (td_best) {
if (td_best->track_priority_val > track_data->track_priority_val)
continue;
if (td_best->track_priority_val == track_data->track_priority_val) {
if (td_best->track_priority_present || !track_data->track_priority_present) {
/* if the priorities are identical, then "present" wins over
* "!present" (absent). */
continue;
}
}
}
td_best = track_data;
}
if (!td_best)
return NULL;
/* Always copy the object from the best TrackData to the TrackObjData. It is
* a bit odd that this getter modifies TrackObjData. However, it gives the
* nice property that after calling _track_obj_data_get_best_data() you can
* use obj_data->obj (and get the same as td_best->obj).
*
* This is actually important, because the previous obj_data->obj will have
* the same ID, but it might have minor differences to td_best->obj.
*
* Note that at this point obj_data->obj also might be an object that is no longer
* tracked. Updating the reference will ensure that we don't have such old references
* around and update to use the most appropriate one. */
nmp_object_ref_set(&obj_data->obj, td_best->obj);
return td_best;
}
static guint
_track_obj_data_hash(gconstpointer data)
{
const TrackObjData *obj_data = data;
return nmp_object_id_hash(obj_data->obj);
}
static gboolean
_track_obj_data_equal(gconstpointer data_a, gconstpointer data_b)
{
const TrackObjData *obj_data_a = data_a;
const TrackObjData *obj_data_b = data_b;
return nmp_object_id_equal(obj_data_a->obj, obj_data_b->obj);
}
static void
_track_obj_data_destroy(gpointer data)
{
TrackObjData *obj_data = data;
c_list_unlink_stale(&obj_data->obj_lst_head);
c_list_unlink_stale(&obj_data->by_obj_lst);
nmp_object_unref(obj_data->obj);
nm_g_slice_free(obj_data);
}
static void
_track_user_tag_data_destroy(gpointer data)
{
TrackUserTagData *user_tag_data = data;
c_list_unlink_stale(&user_tag_data->user_tag_lst_head);
nm_g_slice_free(user_tag_data);
}
static TrackData *
_track_data_lookup(GHashTable *by_data, const NMPObject *obj, gconstpointer user_tag)
{
TrackData track_data_needle = {
.obj = obj,
.user_tag = user_tag,
};
return g_hash_table_lookup(by_data, &track_data_needle);
}
/*****************************************************************************/
static const NMPObject *
_obj_stackinit(NMPObject *obj_stack, NMPObjectType obj_type, gconstpointer obj)
{
nmp_object_stackinit(obj_stack, obj_type, obj);
if (NM_MORE_ASSERTS > 10) {
if (obj_type == NMP_OBJECT_TYPE_MPTCP_ADDR) {
NMPlatformMptcpAddr *m = NMP_OBJECT_CAST_MPTCP_ADDR(obj_stack);
NMPlatformMptcpAddr m_dummy;
/* Only certain MPTCP addresses can be added. */
nm_assert(m->ifindex > 0);
if (nm_platform_mptcp_addr_cmp(
nmp_global_tracker_mptcp_addr_init_for_ifindex(&m_dummy, m->ifindex),
m)
== 0) {
/* This is a dummy instance. We are good. */
} else {
nm_assert_addr_family(m->addr_family);
nm_assert(m->port == 0);
nm_assert(m->id == 0);
}
}
}
nm_assert(nmp_object_is_visible(obj_stack));
return obj_stack;
}
/**
* nmp_global_tracker_track:
* @self: the #NMPGlobalTracker instance
* @obj_type: the NMPObjectType of @obj that we are tracking.
* @obj: the NMPlatformObject (of type NMPObjectType) to track. Usually
* a #NMPlatformRoutingRule, #NMPlatformIP4Route, #NMPlatformIP6Route
* or #NMPlatformMptcpAddr pointer.
* @track_priority: the priority for tracking the rule. Note that
* negative values indicate a forced absence of the rule. Priorities
* are compared with their absolute values (with higher absolute
* value being more important). For example, if you track the same
* rule twice, once with priority -5 and +10, then the rule is
* present (because the positive number is more important).
* The special value 0 indicates weakly-tracked rules.
* @user_tag: the tag associated with tracking this rule. The same tag
* must be used to untrack the rule later.
* @user_tag_untrack: if not %NULL, at the same time untrack this user-tag
* for the same rule. Note that this is different from a plain nmp_global_tracker_untrack_rule(),
* because it enforces ownership of the now tracked rule. On the other hand,
* a plain nmp_global_tracker_untrack_rule() merely forgets about the tracking.
* The purpose here is to set this to %NMP_GLOBAL_TRACKER_EXTERN_WEAKLY_TRACKED_USER_TAG.
*
* Returns: %TRUE, if something changed.
*/
gboolean
nmp_global_tracker_track(NMPGlobalTracker *self,
NMPObjectType obj_type,
gconstpointer obj,
gint32 track_priority,
gconstpointer user_tag,
gconstpointer user_tag_untrack)
{
NMPObject obj_stack;
const NMPObject *p_obj_stack;
TrackData *track_data;
TrackObjData *obj_data;
TrackUserTagData *user_tag_data;
gboolean changed = FALSE;
gboolean changed_untrack = FALSE;
guint32 track_priority_val;
gboolean track_priority_present;
g_return_val_if_fail(NMP_IS_GLOBAL_TRACKER(self), FALSE);
g_return_val_if_fail(obj, FALSE);
g_return_val_if_fail(user_tag, FALSE);
/* The route must not be tied to an interface. We can only handle here
* blackhole/unreachable/prohibit route types. */
g_return_val_if_fail(
NM_IN_SET(obj_type, NMP_OBJECT_TYPE_ROUTING_RULE, NMP_OBJECT_TYPE_MPTCP_ADDR)
|| (NM_IN_SET(obj_type, NMP_OBJECT_TYPE_IP4_ROUTE, NMP_OBJECT_TYPE_IP6_ROUTE)
&& ((const NMPlatformIPRoute *) obj)->ifindex == 0),
FALSE);
/* only positive track priorities are implemented for MPTCP addrs. */
nm_assert(obj_type != NMP_OBJECT_TYPE_MPTCP_ADDR || track_priority > 0);
p_obj_stack = _obj_stackinit(&obj_stack, obj_type, obj);
if (track_priority >= 0) {
track_priority_val = track_priority;
track_priority_present = TRUE;
} else {
track_priority_val = -track_priority;
track_priority_present = FALSE;
}
track_data = _track_data_lookup(self->by_data, p_obj_stack, user_tag);
if (!track_data) {
track_data = g_slice_new(TrackData);
*track_data = (TrackData) {
.obj = nm_dedup_multi_index_obj_intern(nm_platform_get_multi_idx(self->platform),
p_obj_stack),
.user_tag = user_tag,
.track_priority_val = track_priority_val,
.track_priority_present = track_priority_present,
.dirty = FALSE,
};
g_hash_table_add(self->by_data, track_data);
obj_data = g_hash_table_lookup(self->by_obj, &track_data->obj);
if (!obj_data) {
obj_data = g_slice_new(TrackObjData);
*obj_data = (TrackObjData) {
.obj = nmp_object_ref(track_data->obj),
.obj_lst_head = C_LIST_INIT(obj_data->obj_lst_head),
.config_state = CONFIG_STATE_NONE,
};
g_hash_table_add(self->by_obj, obj_data);
c_list_link_tail(_by_obj_lst_head(self, obj_type), &obj_data->by_obj_lst);
}
c_list_link_tail(&obj_data->obj_lst_head, &track_data->obj_lst);
user_tag_data = g_hash_table_lookup(self->by_user_tag, &track_data->user_tag);
if (!user_tag_data) {
user_tag_data = g_slice_new(TrackUserTagData);
*user_tag_data = (TrackUserTagData) {
.user_tag = user_tag,
.user_tag_lst_head = C_LIST_INIT(user_tag_data->user_tag_lst_head),
};
g_hash_table_add(self->by_user_tag, user_tag_data);
}
c_list_link_tail(&user_tag_data->user_tag_lst_head, &track_data->user_tag_lst);
changed = TRUE;
} else {
track_data->dirty = FALSE;
if (track_data->track_priority_val != track_priority_val
|| track_data->track_priority_present != track_priority_present) {
track_data->track_priority_val = track_priority_val;
track_data->track_priority_present = track_priority_present;
changed = TRUE;
}
}
if (user_tag_untrack) {
if (user_tag != user_tag_untrack) {
TrackData *track_data_untrack;
track_data_untrack = _track_data_lookup(self->by_data, p_obj_stack, user_tag_untrack);
if (track_data_untrack) {
_track_data_untrack(self, track_data_untrack, FALSE, TRUE);
changed_untrack = TRUE;
}
} else
nm_assert_not_reached();
}
_track_data_assert(track_data, TRUE);
if (changed) {
char sbuf[NM_UTILS_TO_STRING_BUFFER_SIZE];
_LOGD(
"track [" NM_HASH_OBFUSCATE_PTR_FMT ",%s%u] %s \"%s\"",
NM_HASH_OBFUSCATE_PTR(track_data->user_tag),
(track_data->track_priority_val == 0
? ""
: (track_data->track_priority_present ? "+" : "-")),
(guint) track_data->track_priority_val,
NMP_OBJECT_GET_CLASS(track_data->obj)->obj_type_name,
nmp_object_to_string(track_data->obj, NMP_OBJECT_TO_STRING_PUBLIC, sbuf, sizeof(sbuf)));
}
return changed || changed_untrack;
}
static void
_track_data_untrack(NMPGlobalTracker *self,
TrackData *track_data,
gboolean remove_user_tag_data,
gboolean make_owned_by_us)
{
char sbuf[NM_UTILS_TO_STRING_BUFFER_SIZE];
TrackObjData *obj_data;
nm_assert(NMP_IS_GLOBAL_TRACKER(self));
_track_data_assert(track_data, TRUE);
nm_assert(self->by_data);
nm_assert(g_hash_table_lookup(self->by_data, track_data) == track_data);
_LOGD("untrack [" NM_HASH_OBFUSCATE_PTR_FMT "] %s \"%s\"",
NM_HASH_OBFUSCATE_PTR(track_data->user_tag),
NMP_OBJECT_GET_CLASS(track_data->obj)->obj_type_name,
nmp_object_to_string(track_data->obj, NMP_OBJECT_TO_STRING_PUBLIC, sbuf, sizeof(sbuf)));
#if NM_MORE_ASSERTS
{
TrackUserTagData *user_tag_data;
user_tag_data = g_hash_table_lookup(self->by_user_tag, &track_data->user_tag);
nm_assert(user_tag_data);
nm_assert(c_list_contains(&user_tag_data->user_tag_lst_head, &track_data->user_tag_lst));
}
#endif
nm_assert(!c_list_is_empty(&track_data->user_tag_lst));
obj_data = g_hash_table_lookup(self->by_obj, &track_data->obj);
nm_assert(obj_data);
nm_assert(c_list_contains(&obj_data->obj_lst_head, &track_data->obj_lst));
nm_assert(obj_data == g_hash_table_lookup(self->by_obj, &track_data->obj));
if (make_owned_by_us) {
if (obj_data->config_state == CONFIG_STATE_NONE) {
/* we need to mark this entry that it requires a touch on the next
* sync. */
obj_data->config_state = CONFIG_STATE_OWNED_BY_US;
}
} else if (remove_user_tag_data && c_list_is_empty_or_single(&track_data->user_tag_lst))
g_hash_table_remove(self->by_user_tag, &track_data->user_tag);
/* if obj_data is marked to be "added_by_us" or "removed_by_us", we need to keep this entry
* around for the next sync -- so that we can undo what we did earlier. */
if (obj_data->config_state == CONFIG_STATE_NONE
&& c_list_is_empty_or_single(&track_data->obj_lst))
g_hash_table_remove(self->by_obj, &track_data->obj);
g_hash_table_remove(self->by_data, track_data);
}
gboolean
nmp_global_tracker_untrack(NMPGlobalTracker *self,
NMPObjectType obj_type,
gconstpointer obj,
gconstpointer user_tag)
{
NMPObject obj_stack;
const NMPObject *p_obj_stack;
TrackData *track_data;
gboolean changed = FALSE;
g_return_val_if_fail(NMP_IS_GLOBAL_TRACKER(self), FALSE);
nm_assert(NM_IN_SET(obj_type,
NMP_OBJECT_TYPE_IP4_ROUTE,
NMP_OBJECT_TYPE_IP6_ROUTE,
NMP_OBJECT_TYPE_ROUTING_RULE,
NMP_OBJECT_TYPE_MPTCP_ADDR));
g_return_val_if_fail(obj, FALSE);
g_return_val_if_fail(user_tag, FALSE);
p_obj_stack = _obj_stackinit(&obj_stack, obj_type, obj);
track_data = _track_data_lookup(self->by_data, p_obj_stack, user_tag);
if (track_data) {
_track_data_untrack(self, track_data, TRUE, FALSE);
changed = TRUE;
}
return changed;
}
void
nmp_global_tracker_set_dirty(NMPGlobalTracker *self, gconstpointer user_tag)
{
TrackData *track_data;
TrackUserTagData *user_tag_data;
g_return_if_fail(NMP_IS_GLOBAL_TRACKER(self));
g_return_if_fail(user_tag);
user_tag_data = g_hash_table_lookup(self->by_user_tag, &user_tag);
if (!user_tag_data)
return;
c_list_for_each_entry (track_data, &user_tag_data->user_tag_lst_head, user_tag_lst)
track_data->dirty = TRUE;
}
gboolean
nmp_global_tracker_untrack_all(NMPGlobalTracker *self,
gconstpointer user_tag,
gboolean all /* or only dirty */,
gboolean make_survivors_dirty)
{
TrackData *track_data;
TrackData *track_data_safe;
TrackUserTagData *user_tag_data;
gboolean changed = FALSE;
g_return_val_if_fail(NMP_IS_GLOBAL_TRACKER(self), FALSE);
g_return_val_if_fail(user_tag, FALSE);
user_tag_data = g_hash_table_lookup(self->by_user_tag, &user_tag);
if (!user_tag_data)
return FALSE;
c_list_for_each_entry_safe (track_data,
track_data_safe,
&user_tag_data->user_tag_lst_head,
user_tag_lst) {
if (all || track_data->dirty) {
_track_data_untrack(self, track_data, FALSE, FALSE);
changed = TRUE;
continue;
}
if (make_survivors_dirty)
track_data->dirty = TRUE;
}
if (c_list_is_empty(&user_tag_data->user_tag_lst_head))
g_hash_table_remove(self->by_user_tag, user_tag_data);
return changed;
}
/*****************************************************************************/
/* Usually, we track NMPlatformMptcpAddr instances with an ifindex set.
* If we have *any* such instance, we know that the ifindex is fully
* synched (meaning, we will delete all unknown endpoints for that interface).
* However, if we don't have an endpoint on the interface, we may still
* want to track that a certain ifindex is fully managed.
*
* This initializes a dummy instance for exactly that purpose. */
const NMPlatformMptcpAddr *
nmp_global_tracker_mptcp_addr_init_for_ifindex(NMPlatformMptcpAddr *addr, int ifindex)
{
nm_assert(addr);
nm_assert(ifindex > 0);
*addr = (NMPlatformMptcpAddr) {
.ifindex = ifindex,
.addr_family = AF_UNSPEC,
};
return addr;
}
/*****************************************************************************/
typedef struct {
TrackObjData *obj_data;
const TrackData *td_best;
} MptcpSyncData;
static int
_mptcp_entries_cmp(gconstpointer a, gconstpointer b, gpointer user_data)
{
const MptcpSyncData *d_a = a;
const MptcpSyncData *d_b = b;
/* 1) prefer addresses based on the priority (highest priority
* sorted first). */
NM_CMP_FIELD(d_b->td_best, d_a->td_best, track_priority_val);
/* Finally, we only care about the order in which they were tracked.
* Rely on the stable sort to get that right. */
return 0;
}
void
nmp_global_tracker_sync_mptcp_addrs(NMPGlobalTracker *self, gboolean reapply)
{
char sbuf[64 + NM_UTILS_TO_STRING_BUFFER_SIZE];
gs_unref_ptrarray GPtrArray *kaddrs_arr = NULL;
gs_unref_hashtable GHashTable *kaddrs_idx = NULL;
TrackObjData *obj_data;
TrackObjData *obj_data_safe;
CList *by_obj_lst_head;
const TrackData *td_best;
gs_unref_hashtable GHashTable *handled_ifindexes = NULL;
gs_unref_array GArray *entries = NULL;
gs_unref_hashtable GHashTable *entries_hash_by_addr = NULL;
gs_unref_hashtable GHashTable *entries_to_delete = NULL;
guint i;
guint j;
g_return_if_fail(NMP_IS_GLOBAL_TRACKER(self));
_LOGD("sync mptcp-addr%s", reapply ? " (reapply)" : "");
/* Iterate over the tracked objects and construct @handled_ifindexes, @entries
* and @entries_to_delete.
* - @handled_ifindexes is a hash with all managed interfaces (their ifindex).
* - @entries are the MptcpSyncData instances for the tracked objects.
* - @entries_to_delete are the NMPObject which we added earlier, but now not
* anymore (and which we shall delete). */
by_obj_lst_head = _by_obj_lst_head(self, NMP_OBJECT_TYPE_MPTCP_ADDR);
c_list_for_each_entry_safe (obj_data, obj_data_safe, by_obj_lst_head, by_obj_lst) {
const NMPlatformMptcpAddr *mptcp_addr = NMP_OBJECT_CAST_MPTCP_ADDR(obj_data->obj);
NMPlatformMptcpAddr xtst;
nm_assert(mptcp_addr->port == 0);
nm_assert(mptcp_addr->ifindex > 0);
nm_assert(mptcp_addr->id == 0);
nm_assert_addr_family_or_unspec(mptcp_addr->addr_family);
/* AF_UNSPEC means this is the dummy object. We only care about it to make the
* ifindex as managed via @handled_ifindexes. */
nm_assert(
(mptcp_addr->addr_family == AF_UNSPEC)
== (nm_platform_mptcp_addr_cmp(
mptcp_addr,
nmp_global_tracker_mptcp_addr_init_for_ifindex(&xtst, mptcp_addr->ifindex))
== 0));
/* We need to know which ifindexes are managed/handled by us. Build an index
* for that. */
if (!handled_ifindexes)
handled_ifindexes = g_hash_table_new(nm_direct_hash, NULL);
g_hash_table_add(handled_ifindexes, GINT_TO_POINTER(mptcp_addr->ifindex));
td_best = _track_obj_data_get_best_data(obj_data);
if (!td_best) {
nm_assert(obj_data->config_state == CONFIG_STATE_ADDED_BY_US);
/* This entry is a tombstone, that tells us that added the object earlier.
* We can delete the MPTCP address (if it's still configured).
*
* Then we can drop the tombstone. */
if (mptcp_addr->addr_family != AF_UNSPEC) {
if (!reapply) {
if (!entries_to_delete) {
entries_to_delete = g_hash_table_new_full((GHashFunc) nmp_object_id_hash,
(GEqualFunc) nmp_object_id_equal,
(GDestroyNotify) nmp_object_unref,
NULL);
}
g_hash_table_add(entries_to_delete, (gpointer) nmp_object_ref(obj_data->obj));
}
}
/* We can forget about this entry now. */
g_hash_table_remove(self->by_obj, obj_data);
continue;
}
/* negative and zero track priorities are not implemented (and make no sense?). */
nm_assert(td_best->track_priority_val > 0);
nm_assert(td_best->track_priority_present);
if (mptcp_addr->addr_family == AF_UNSPEC) {
/* This is a nmp_global_tracker_mptcp_addr_init_for_ifindex() dummy entry.
* It only exists so we can add the @handled_ifindexes entry above
* and handle addresses on this interface. */
obj_data->config_state = CONFIG_STATE_ADDED_BY_US;
continue;
}
if (!entries)
entries = g_array_new(FALSE, FALSE, sizeof(MptcpSyncData));
g_array_append_val(entries,
((const MptcpSyncData) {
.obj_data = obj_data,
.td_best = td_best,
}));
}
/* We collected all the entires we want to configure. Now, sort them by
* priority, and drop all the duplicates (preferring the entries that
* appear first, where first means "older"). In kernel, we can only configure an IP
* address (without port) as endpoint once. If two interfaces provide the same IP
* address, we can only configure one. We need to select one and filter out duplicates.
* While there is no solution, the idea is to select the preferred address
* somewhat consistently.
*
* Also, create a lookup index @entries_hash_by_addr to lookup by address. */
if (entries) {
/* First we sort the entries by priority, to prefer the ones with higher
* priority. In case of equal priority, we rely on the stable sort to
* preserve the order in which things got tracked. */
g_array_sort_with_data(entries, _mptcp_entries_cmp, NULL);
entries_hash_by_addr = g_hash_table_new(nm_platform_mptcp_addr_index_addr_cmp,
nm_platform_mptcp_addr_index_addr_equal);
/* Now, drop all duplicates addresses. Only keep the first one. */
for (i = 0, j = 0; i < entries->len; i++) {
const MptcpSyncData *d = &nm_g_array_index(entries, MptcpSyncData, i);
const NMPlatformMptcpAddr *mptcp_addr = NMP_OBJECT_CAST_MPTCP_ADDR(d->obj_data->obj);
obj_data = g_hash_table_lookup(entries_hash_by_addr, (gpointer) mptcp_addr);
if (obj_data) {
/* This object is shadowed. We ignore it.
*
* However, we first propagate the config_state. For MPTCP addrs, it can only be
* NONE or ADDED_BY_US. */
nm_assert(NM_IN_SET(d->obj_data->config_state,
CONFIG_STATE_NONE,
CONFIG_STATE_ADDED_BY_US));
nm_assert(
NM_IN_SET(obj_data->config_state, CONFIG_STATE_NONE, CONFIG_STATE_ADDED_BY_US));
if (d->obj_data->config_state == CONFIG_STATE_ADDED_BY_US) {
obj_data->config_state = CONFIG_STATE_ADDED_BY_US;
d->obj_data->config_state = CONFIG_STATE_NONE;
}
continue;
}
if (!g_hash_table_insert(entries_hash_by_addr, (gpointer) mptcp_addr, d->obj_data))
nm_assert_not_reached();
if (i != j)
(nm_g_array_index(entries, MptcpSyncData, j)) = *d;
j++;
if (j >= MPTCP_PM_ADDR_MAX) {
/* Kernel limits the number of addresses we can configure.
* It's hard-coded here, taken from current kernel. Hopefully
* it matches the running kernel.
*
* It's worse. There might be other addresses already configured
* on other interfaces (or with a port). Our sync method will leave
* them alone, as they were not added by us. So the actual limit
* is possibly smaller, and kernel fails with EINVAL.
*
* Still, we definitely need to truncate the list here. Imagine
* during an earlier sync we added MAX addresses on one interface.
* Now, another interface activates, and wants to configure one
* address. That address will get a higher priority (chosen by NML3Cfg),
* so that part is good. However, it means we must drop the last from
* the other MAX addresses. We achieve that by truncating the list
* to MPTCP_PM_ADDR_MAX.
*/
break;
}
}
g_array_set_size(entries, j);
}
/* Get the list of currently (in kernel) configured MPTCP endpoints. */
kaddrs_arr = nm_platform_mptcp_addrs_dump(self->platform);
/* First, delete all kaddrs which we no longer want... */
if (kaddrs_arr) {
for (i = 0; i < kaddrs_arr->len; i++) {
const NMPObject *obj = kaddrs_arr->pdata[i];
const NMPlatformMptcpAddr *mptcp_addr = NMP_OBJECT_CAST_MPTCP_ADDR(obj);
gboolean add_to_kaddr_idx = FALSE;
if (mptcp_addr->port != 0 || mptcp_addr->ifindex <= 0) {
/* We ignore all endpoints that have a port or no ifindex.
* Those were never created by us, let the user who created
* them handle them. */
goto keep_and_next;
}
if (!nm_g_hash_table_contains(handled_ifindexes,
GINT_TO_POINTER(mptcp_addr->ifindex))) {
/* This endpoint is on an interface we don't manage. Ignore (and keep) it. */
goto keep_and_next;
}
/* We have the object in the delete-list. However, we might still also want
* to add it back. Check for that too. */
obj_data = nm_g_hash_table_lookup(entries_hash_by_addr, mptcp_addr);
if (obj_data) {
const NMPlatformMptcpAddr *mptcp_addr2 = NMP_OBJECT_CAST_MPTCP_ADDR(obj_data->obj);
if (mptcp_addr->flags == mptcp_addr2->flags
&& mptcp_addr->ifindex == mptcp_addr2->ifindex) {
/* We want to add this address and it's already configured. Keep it
* and remember that we already have it. */
add_to_kaddr_idx = TRUE;
goto keep_and_next;
}
/* We want to configure a similar address mptcp_addr2) as the one that is already configured
* (mptcp_addr). However, the ifindex or flag differs. Delete this one to add the
* right one blow. */
} else {
/* We don't want to configure this address (anymore). */
if (reapply) {
/* in reapply mode, we delete the extra address. */
} else {
/* Otherwise, we only delete it, if we remember that we added this one
* before. */
if (!nm_g_hash_table_contains(entries_to_delete, obj)) {
/* This address was not added by us. Keep it. */
goto keep_and_next;
}
}
}
if (!nm_platform_object_delete(self->platform, obj)) {
/* We failed to delete it. It's unclear what is the matter with this
* object. Ignore the failure. */
}
continue;
keep_and_next:
_LOGt("keep: %s \"%s\"%s",
NMP_OBJECT_GET_CLASS(obj)->obj_type_name,
nmp_object_to_string(obj, NMP_OBJECT_TO_STRING_PUBLIC, sbuf, sizeof(sbuf)),
add_to_kaddr_idx ? " (index)" : "");
if (add_to_kaddr_idx) {
if (!kaddrs_idx) {
kaddrs_idx = g_hash_table_new((GHashFunc) nmp_object_id_hash,
(GEqualFunc) nmp_object_id_equal);
}
g_hash_table_add(kaddrs_idx, (gpointer) obj);
}
}
}
if (entries) {
for (i = 0; i < entries->len; i++) {
const MptcpSyncData *d = &nm_g_array_index(entries, MptcpSyncData, i);
const NMPlatformMptcpAddr *mptcp_addr = NMP_OBJECT_CAST_MPTCP_ADDR(d->obj_data->obj);
const NMPObject *kobj;
nm_assert(mptcp_addr->port == 0);
nm_assert(mptcp_addr->ifindex > 0);
d->obj_data->config_state = CONFIG_STATE_ADDED_BY_US;
kobj = nm_g_hash_table_lookup(kaddrs_idx, d->obj_data->obj);
if (kobj && kobj->mptcp_addr.flags == mptcp_addr->flags) {
/* This address is already added with the right flags. We can
* skip it. */
continue;
}
/* Kernel actually only allows us to add a small number of addresses.
* Also, if we have a conflicting address on another interface, the
* request will be rejected.
*
* Don't try to handle that. Just attempt to add the address, and if
* we fail, there is nothing we can do about it. */
nm_platform_mptcp_addr_update(self->platform, TRUE, mptcp_addr);
}
}
}
void
nmp_global_tracker_sync(NMPGlobalTracker *self, NMPObjectType obj_type, gboolean keep_deleted)
{
char sbuf[NM_UTILS_TO_STRING_BUFFER_SIZE];
const NMDedupMultiHeadEntry *pl_head_entry;
const NMPObject *plobj;
gs_unref_ptrarray GPtrArray *objs_to_delete = NULL;
TrackObjData *obj_data;
TrackObjData *obj_data_safe;
CList *by_obj_lst_head;
guint i;
const TrackData *td_best;
g_return_if_fail(NMP_IS_GLOBAL_TRACKER(self));
g_return_if_fail(NM_IN_SET(obj_type,
NMP_OBJECT_TYPE_IP4_ROUTE,
NMP_OBJECT_TYPE_IP6_ROUTE,
NMP_OBJECT_TYPE_ROUTING_RULE));
_LOGD("sync %s%s",
nmp_class_from_type(obj_type)->obj_type_name,
keep_deleted ? " (don't remove any)" : "");
if (obj_type == NMP_OBJECT_TYPE_ROUTING_RULE)
pl_head_entry = nm_platform_lookup_obj_type(self->platform, obj_type);
else
pl_head_entry = nm_platform_lookup_object(self->platform, obj_type, 0);
if (pl_head_entry) {
NMDedupMultiIter pl_iter;
nmp_cache_iter_for_each (&pl_iter, pl_head_entry, &plobj) {
obj_data = g_hash_table_lookup(self->by_obj, &plobj);
if (!obj_data) {
/* this obj is not tracked. It was externally added, hence we
* ignore it. */
continue;
}
td_best = _track_obj_data_get_best_data(obj_data);
if (td_best) {
if (td_best->track_priority_present) {
if (obj_data->config_state == CONFIG_STATE_OWNED_BY_US)
obj_data->config_state = CONFIG_STATE_ADDED_BY_US;
continue;
}
if (td_best->track_priority_val == 0) {
if (!NM_IN_SET(obj_data->config_state,
CONFIG_STATE_ADDED_BY_US,
CONFIG_STATE_OWNED_BY_US)) {
obj_data->config_state = CONFIG_STATE_NONE;
continue;
}
obj_data->config_state = CONFIG_STATE_NONE;
}
}
if (keep_deleted) {
_LOGD("forget/leak object added by us: %s \"%s\"",
NMP_OBJECT_GET_CLASS(plobj)->obj_type_name,
nmp_object_to_string(plobj, NMP_OBJECT_TO_STRING_PUBLIC, sbuf, sizeof(sbuf)));
continue;
}
if (!objs_to_delete)
objs_to_delete = g_ptr_array_new_with_free_func((GDestroyNotify) nmp_object_unref);
g_ptr_array_add(objs_to_delete, (gpointer) nmp_object_ref(plobj));
obj_data->config_state = CONFIG_STATE_REMOVED_BY_US;
}
}
if (objs_to_delete) {
for (i = 0; i < objs_to_delete->len; i++)
nm_platform_object_delete(self->platform, objs_to_delete->pdata[i]);
}
by_obj_lst_head = _by_obj_lst_head(self, obj_type);
c_list_for_each_entry_safe (obj_data, obj_data_safe, by_obj_lst_head, by_obj_lst) {
nm_assert(NMP_OBJECT_GET_TYPE(obj_data->obj) == obj_type);
td_best = _track_obj_data_get_best_data(obj_data);
if (!td_best) {
g_hash_table_remove(self->by_obj, obj_data);
continue;
}
if (!td_best->track_priority_present) {
if (obj_data->config_state == CONFIG_STATE_OWNED_BY_US)
obj_data->config_state = CONFIG_STATE_REMOVED_BY_US;
continue;
}
if (td_best->track_priority_val == 0) {
if (!NM_IN_SET(obj_data->config_state,
CONFIG_STATE_REMOVED_BY_US,
CONFIG_STATE_OWNED_BY_US)) {
obj_data->config_state = CONFIG_STATE_NONE;
continue;
}
obj_data->config_state = CONFIG_STATE_NONE;
}
plobj =
nm_platform_lookup_obj(self->platform, NMP_CACHE_ID_TYPE_OBJECT_TYPE, obj_data->obj);
if (plobj) {
int c;
switch (obj_type) {
case NMP_OBJECT_TYPE_ROUTING_RULE:
c = nm_platform_routing_rule_cmp(NMP_OBJECT_CAST_ROUTING_RULE(obj_data->obj),
NMP_OBJECT_CAST_ROUTING_RULE(plobj),
NM_PLATFORM_ROUTING_RULE_CMP_TYPE_SEMANTICALLY);
break;
case NMP_OBJECT_TYPE_IP4_ROUTE:
c = nm_platform_ip4_route_cmp(NMP_OBJECT_CAST_IP4_ROUTE(obj_data->obj),
NMP_OBJECT_CAST_IP4_ROUTE(plobj),
NM_PLATFORM_IP_ROUTE_CMP_TYPE_SEMANTICALLY);
break;
case NMP_OBJECT_TYPE_IP6_ROUTE:
c = nm_platform_ip6_route_cmp(NMP_OBJECT_CAST_IP6_ROUTE(obj_data->obj),
NMP_OBJECT_CAST_IP6_ROUTE(plobj),
NM_PLATFORM_IP_ROUTE_CMP_TYPE_SEMANTICALLY);
break;
default:
c = nm_assert_unreachable_val(0);
break;
}
if (c == 0)
continue;
nm_platform_object_delete(self->platform, plobj);
}
obj_data->config_state = CONFIG_STATE_ADDED_BY_US;
if (obj_type == NMP_OBJECT_TYPE_ROUTING_RULE) {
nm_platform_routing_rule_add(self->platform,
NMP_NLM_FLAG_ADD,
NMP_OBJECT_CAST_ROUTING_RULE(obj_data->obj));
} else
nm_platform_ip_route_add(self->platform, NMP_NLM_FLAG_APPEND, obj_data->obj, NULL);
}
}
/*****************************************************************************/
void
nmp_global_tracker_track_rule_from_platform(NMPGlobalTracker *self,
NMPlatform *platform,
int addr_family,
gint32 tracking_priority,
gconstpointer user_tag)
{
NMPLookup lookup;
const NMDedupMultiHeadEntry *head_entry;
NMDedupMultiIter iter;
const NMPObject *o;
g_return_if_fail(NMP_IS_GLOBAL_TRACKER(self));
if (!platform)
platform = self->platform;
else
g_return_if_fail(NM_IS_PLATFORM(platform));
nm_assert(NM_IN_SET(addr_family, AF_UNSPEC, AF_INET, AF_INET6));
nmp_lookup_init_obj_type(&lookup, NMP_OBJECT_TYPE_ROUTING_RULE);
head_entry = nm_platform_lookup(platform, &lookup);
nmp_cache_iter_for_each (&iter, head_entry, &o) {
const NMPlatformRoutingRule *rr = NMP_OBJECT_CAST_ROUTING_RULE(o);
if (addr_family != AF_UNSPEC && rr->addr_family != addr_family)
continue;
nmp_global_tracker_track_rule(self, rr, tracking_priority, user_tag, NULL);
}
}
/*****************************************************************************/
void
nmp_global_tracker_track_rule_default(NMPGlobalTracker *self,
int addr_family,
gint32 track_priority,
gconstpointer user_tag)
{
g_return_if_fail(NMP_IS_GLOBAL_TRACKER(self));
nm_assert(NM_IN_SET(addr_family, AF_UNSPEC, AF_INET, AF_INET6));
/* track the default rules. See also `man ip-rule`. */
if (NM_IN_SET(addr_family, AF_UNSPEC, AF_INET)) {
nmp_global_tracker_track_local_rule(self, addr_family, track_priority, user_tag, NULL);
nmp_global_tracker_track_rule(self,
&((NMPlatformRoutingRule) {
.addr_family = AF_INET,
.priority = 32766,
.table = RT_TABLE_MAIN,
.action = FR_ACT_TO_TBL,
.protocol = RTPROT_KERNEL,
}),
track_priority,
user_tag,
NULL);
nmp_global_tracker_track_rule(self,
&((NMPlatformRoutingRule) {
.addr_family = AF_INET,
.priority = 32767,
.table = RT_TABLE_DEFAULT,
.action = FR_ACT_TO_TBL,
.protocol = RTPROT_KERNEL,
}),
track_priority,
user_tag,
NULL);
}
if (NM_IN_SET(addr_family, AF_UNSPEC, AF_INET6)) {
nmp_global_tracker_track_local_rule(self, addr_family, track_priority, user_tag, NULL);
nmp_global_tracker_track_rule(self,
&((NMPlatformRoutingRule) {
.addr_family = AF_INET6,
.priority = 32766,
.table = RT_TABLE_MAIN,
.action = FR_ACT_TO_TBL,
.protocol = RTPROT_KERNEL,
}),
track_priority,
user_tag,
NULL);
}
}
void
nmp_global_tracker_track_local_rule(NMPGlobalTracker *self,
int addr_family,
gint32 track_priority,
gconstpointer user_tag,
gconstpointer user_tag_untrack)
{
g_return_if_fail(NMP_IS_GLOBAL_TRACKER(self));
nm_assert(NM_IN_SET(addr_family, AF_UNSPEC, AF_INET, AF_INET6));
if (NM_IN_SET(addr_family, AF_UNSPEC, AF_INET)) {
nmp_global_tracker_track_rule(self,
&((NMPlatformRoutingRule) {
.addr_family = AF_INET,
.priority = 0,
.table = RT_TABLE_LOCAL,
.action = FR_ACT_TO_TBL,
.protocol = RTPROT_KERNEL,
}),
track_priority,
user_tag,
user_tag_untrack);
}
if (NM_IN_SET(addr_family, AF_UNSPEC, AF_INET6)) {
nmp_global_tracker_track_rule(self,
&((NMPlatformRoutingRule) {
.addr_family = AF_INET6,
.priority = 0,
.table = RT_TABLE_LOCAL,
.action = FR_ACT_TO_TBL,
.protocol = RTPROT_KERNEL,
}),
track_priority,
user_tag,
user_tag_untrack);
}
}
/*****************************************************************************/
NMPGlobalTracker *
nmp_global_tracker_new(NMPlatform *platform)
{
NMPGlobalTracker *self;
g_return_val_if_fail(NM_IS_PLATFORM(platform), NULL);
G_STATIC_ASSERT_EXPR(G_STRUCT_OFFSET(TrackUserTagData, user_tag) == 0);
self = g_slice_new(NMPGlobalTracker);
*self = (NMPGlobalTracker) {
.ref_count = 1,
.platform = g_object_ref(platform),
.by_data =
g_hash_table_new_full(_track_data_hash, _track_data_equal, NULL, _track_data_destroy),
.by_obj = g_hash_table_new_full(_track_obj_data_hash,
_track_obj_data_equal,
NULL,
_track_obj_data_destroy),
.by_user_tag = g_hash_table_new_full(nm_pdirect_hash,
nm_pdirect_equal,
NULL,
_track_user_tag_data_destroy),
.by_obj_lst_heads[0] = C_LIST_INIT(self->by_obj_lst_heads[0]),
.by_obj_lst_heads[1] = C_LIST_INIT(self->by_obj_lst_heads[1]),
.by_obj_lst_heads[2] = C_LIST_INIT(self->by_obj_lst_heads[2]),
.by_obj_lst_heads[3] = C_LIST_INIT(self->by_obj_lst_heads[3]),
};
return self;
}
NMPGlobalTracker *
nmp_global_tracker_ref(NMPGlobalTracker *self)
{
g_return_val_if_fail(NMP_IS_GLOBAL_TRACKER(self), NULL);
self->ref_count++;
return self;
}
void
nmp_global_tracker_unref(NMPGlobalTracker *self)
{
g_return_if_fail(NMP_IS_GLOBAL_TRACKER(self));
if (--self->ref_count > 0)
return;
g_hash_table_destroy(self->by_user_tag);
g_hash_table_destroy(self->by_obj);
g_hash_table_destroy(self->by_data);
nm_assert(c_list_is_empty(&self->by_obj_lst_heads[0]));
nm_assert(c_list_is_empty(&self->by_obj_lst_heads[1]));
nm_assert(c_list_is_empty(&self->by_obj_lst_heads[2]));
nm_assert(c_list_is_empty(&self->by_obj_lst_heads[3]));
g_object_unref(self->platform);
nm_g_slice_free(self);
}
|