summaryrefslogtreecommitdiff
path: root/src/nm-l3-config-data.h
blob: 59fbffce66c0fba9b9d5c0c278d4c2138b85dba9 (plain)
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
// SPDX-License-Identifier: LGPL-2.1+

#ifndef __NM_L3_CONFIG_DATA_H__
#define __NM_L3_CONFIG_DATA_H__

#include "nm-glib-aux/nm-dedup-multi.h"
#include "nm-setting-connection.h"
#include "nm-setting-ip6-config.h"
#include "platform/nm-platform.h"

typedef enum {
	NM_L3_CONFIG_DAT_FLAGS_NONE                           = 0,

	/* if set, then the merge flag NM_L3_CONFIG_MERGE_FLAGS_NO_DEFAULT_ROUTES gets
	 * ignored during merge. */
	NM_L3_CONFIG_DAT_FLAGS_IGNORE_MERGE_NO_DEFAULT_ROUTES = (1ull << 0),

	NM_L3_CONFIG_DAT_FLAGS_HAS_DNS_PRIORITY_4             = (1ull << 1),
	NM_L3_CONFIG_DAT_FLAGS_HAS_DNS_PRIORITY_6             = (1ull << 2),
#define NM_L3_CONFIG_DAT_FLAGS_HAS_DNS_PRIORITY(is_ipv4) (  (is_ipv4) \
                                                          ? NM_L3_CONFIG_DAT_FLAGS_HAS_DNS_PRIORITY_4 \
                                                          : NM_L3_CONFIG_DAT_FLAGS_HAS_DNS_PRIORITY_6)
	NM_L3_CONFIG_DAT_FLAGS_HAS_MTU                        = (1ull << 3),

} NML3ConfigDatFlags;

typedef enum {
	NM_L3_CONFIG_ADD_FLAGS_NONE         = 0,

	/* If the object does not yet exist, it will be added. If it already exists,
	 * by default the object will be replaced. With this flag, the new object will
	 * be merged with the existing one. */
	NM_L3_CONFIG_ADD_FLAGS_MERGE        = (1ull << 0),

	/* If the object does not yet exist, it will be added. If it already exists,
	 * by default the object will be replaced. With this flag, the add will have
	 * no effect and the existing object will be kept. */
	NM_L3_CONFIG_ADD_FLAGS_EXCLUSIVE    = (1ull << 1),

	/* A new object gets appended by default. If the object already exists,
	 * by default it will not be moved. With APPEND-FORCE, we will always move
	 * an existing object to the end of the list. */
	NM_L3_CONFIG_ADD_FLAGS_APPEND_FORCE = (1ull << 2),
} NML3ConfigAddFlags;

/**
 * NML3ConfigMergeFlags:
 * @NM_L3_CONFIG_MERGE_FLAGS_NONE: no flags set
 * @NM_L3_CONFIG_MERGE_FLAGS_NO_ROUTES: don't merge routes
 * @NM_L3_CONFIG_MERGE_FLAGS_NO_DEFAULT_ROUTES: don't merge default routes.
 *   Note that if the respective NML3ConfigData has NM_L3_CONFIG_DAT_FLAGS_IGNORE_MERGE_NO_DEFAULT_ROUTES
 *   set, this flag gets ignored during merge.
 * @NM_L3_CONFIG_MERGE_FLAGS_NO_DNS: don't merge DNS information
 * @NM_L3_CONFIG_MERGE_FLAGS_EXTERNAL: mark new addresses as external
 */
typedef enum {
	NM_L3_CONFIG_MERGE_FLAGS_NONE              = 0,
	NM_L3_CONFIG_MERGE_FLAGS_NO_ROUTES         = (1LL << 0),
	NM_L3_CONFIG_MERGE_FLAGS_NO_DEFAULT_ROUTES = (1LL << 1),
	NM_L3_CONFIG_MERGE_FLAGS_NO_DNS            = (1LL << 2),
	NM_L3_CONFIG_MERGE_FLAGS_EXTERNAL          = (1LL << 3),
} NML3ConfigMergeFlags;

/*****************************************************************************/

typedef struct _NML3ConfigData NML3ConfigData;

typedef struct {
	const NML3ConfigData *l3cfg;
	NML3ConfigMergeFlags merge_flags;
	union {
		struct {
			guint32 default_route_penalty_6;
			guint32 default_route_penalty_4;
		};
		guint32 default_route_penalty_x[2];
	};
} NML3ConfigDatMergeInfo;

NML3ConfigData *nm_l3_config_data_new (NMDedupMultiIndex *multi_idx,
                                       int ifindex);
const NML3ConfigData *nm_l3_config_data_ref (const NML3ConfigData *self);
const NML3ConfigData *nm_l3_config_data_ref_and_seal (const NML3ConfigData *self);
const NML3ConfigData *nm_l3_config_data_seal (const NML3ConfigData *self);
void nm_l3_config_data_unref (const NML3ConfigData *self);

NM_AUTO_DEFINE_FCN0 (const NML3ConfigData *, _nm_auto_unref_l3cfg, nm_l3_config_data_unref);
#define nm_auto_unref_l3cfg nm_auto (_nm_auto_unref_l3cfg)

NM_AUTO_DEFINE_FCN0 (NML3ConfigData *, _nm_auto_unref_l3cfg_init, nm_l3_config_data_unref);
#define nm_auto_unref_l3cfg_init nm_auto (_nm_auto_unref_l3cfg_init)

gboolean nm_l3_config_data_is_sealed (const NML3ConfigData *self);

NML3ConfigData *nm_l3_config_data_new_clone (const NML3ConfigData *src,
                                             int ifindex);

NML3ConfigData *nm_l3_config_data_new_from_connection (NMDedupMultiIndex *multi_idx,
                                                       int ifindex,
                                                       NMConnection *connection,
                                                       guint32 route_table,
                                                       guint32 route_metric);

NML3ConfigData *nm_l3_config_data_new_from_platform (NMDedupMultiIndex *multi_idx,
                                                     int ifindex,
                                                     NMPlatform *platform,
                                                     NMSettingIP6ConfigPrivacy ipv6_privacy_rfc4941);

NML3ConfigData *nm_l3_config_data_new_combined (NMDedupMultiIndex *multi_idx,
                                                int ifindex,
                                                const NML3ConfigDatMergeInfo *const*merge_infos,
                                                guint merge_infos_len);

void nm_l3_config_data_add_dependent_routes (NML3ConfigData *self,
                                             int addr_family,
                                             guint32 route_table,
                                             guint32 route_metric,
                                             gboolean is_vrf,
                                             GPtrArray **out_ip4_dev_route_blacklist);

/*****************************************************************************/

int nm_l3_config_data_get_ifindex (const NML3ConfigData *self);

NMDedupMultiIndex *nm_l3_config_data_get_multi_idx (const NML3ConfigData *self);

static inline gboolean
NM_IS_L3_CONFIG_DATA (const NML3ConfigData *self)
{
	/* NML3ConfigData is not an NMObject, so we cannot ask which type it has.
	 * This check here is really only useful for assertions, and there it is
	 * enough to check whether the pointer is not NULL.
	 *
	 * Additionally, also call nm_l3_config_data_get_ifindex(), which does more
	 * checks during nm_assert(). */
	nm_assert (nm_l3_config_data_get_ifindex (self) > 0);
	return !!self;
}

/*****************************************************************************/

int nm_l3_config_data_cmp (const NML3ConfigData *a, const NML3ConfigData *b);

static inline gboolean
nm_l3_config_data_equal (const NML3ConfigData *a, const NML3ConfigData *b)
{
	return nm_l3_config_data_cmp (a, b) == 0;
}

/*****************************************************************************/

const NMDedupMultiIdxType *nm_l3_config_data_lookup_index (const NML3ConfigData *self,
                                                           NMPObjectType obj_type);

const NMDedupMultiEntry *nm_l3_config_data_lookup_obj (const NML3ConfigData *self,
                                                       const NMPObject *obj);

const NMDedupMultiEntry *nm_l3_config_data_lookup_route_obj (const NML3ConfigData *self,
                                                             const NMPObject *needle);

const NMDedupMultiEntry *nm_l3_config_data_lookup_route (const NML3ConfigData *self,
                                                         int addr_family,
                                                         const NMPlatformIPRoute *needle);

const NMDedupMultiHeadEntry *nm_l3_config_data_lookup_objs (const NML3ConfigData *self, NMPObjectType obj_type);

static inline const NMDedupMultiHeadEntry *
nm_l3_config_data_lookup_addresses (const NML3ConfigData *self, int addr_family)
{
	nm_assert_addr_family (addr_family);

	return nm_l3_config_data_lookup_objs (self,
	                                        addr_family == AF_INET
	                                      ? NMP_OBJECT_TYPE_IP4_ADDRESS
	                                      : NMP_OBJECT_TYPE_IP6_ADDRESS);
}

static inline const NMDedupMultiHeadEntry *
nm_l3_config_data_lookup_routes (const NML3ConfigData *self, int addr_family)
{
	nm_assert_addr_family (addr_family);

	return nm_l3_config_data_lookup_objs (self,
	                                        addr_family == AF_INET
	                                      ? NMP_OBJECT_TYPE_IP4_ROUTE
	                                      : NMP_OBJECT_TYPE_IP6_ROUTE);
}

#define nm_l3_config_data_iter_obj_for_each(iter, self, obj, type) \
    for (nm_dedup_multi_iter_init (&(iter), nm_l3_config_data_lookup_objs ((self), (type))); \
         nm_platform_dedup_multi_iter_next_obj (&(iter), &(obj), (type)); \
         )

#define nm_l3_config_data_iter_ip4_address_for_each(iter, self, address) \
    for (nm_dedup_multi_iter_init (&(iter), nm_l3_config_data_lookup_addresses ((self), AF_INET)); \
         nm_platform_dedup_multi_iter_next_ip4_address (&(iter), &(address)); \
         )

#define nm_l3_config_data_iter_ip6_address_for_each(iter, self, address) \
    for (nm_dedup_multi_iter_init (&(iter), nm_l3_config_data_lookup_addresses ((self), AF_INET6)); \
         nm_platform_dedup_multi_iter_next_ip6_address (&(iter), &(address)); \
         )

#define nm_l3_config_data_iter_ip4_route_for_each(iter, self, route) \
    for (nm_dedup_multi_iter_init (&(iter), nm_l3_config_data_lookup_routes ((self), AF_INET)); \
         nm_platform_dedup_multi_iter_next_ip4_route  (&(iter), &(route)); \
         )

#define nm_l3_config_data_iter_ip6_route_for_each(iter, self, route) \
    for (nm_dedup_multi_iter_init (&(iter), nm_l3_config_data_lookup_routes ((self), AF_INET6)); \
         nm_platform_dedup_multi_iter_next_ip6_route  (&(iter), &(route)); \
         )

/*****************************************************************************/

NML3ConfigDatFlags nm_l3_config_data_get_flags (const NML3ConfigData *self);

void nm_l3_config_data_set_flags_full (NML3ConfigData *self,
                                       NML3ConfigDatFlags flags,
                                       NML3ConfigDatFlags mask);

static inline void
nm_l3_config_data_set_flags (NML3ConfigData *self,
                             NML3ConfigDatFlags flags)
{
	nm_l3_config_data_set_flags_full (self, flags, flags);
}

static inline void
nm_l3_config_data_unset_flags (NML3ConfigData *self,
                               NML3ConfigDatFlags flags)
{
	nm_l3_config_data_set_flags_full (self, NM_L3_CONFIG_DAT_FLAGS_NONE, flags);
}

/*****************************************************************************/

gboolean nm_l3_config_data_add_address_full (NML3ConfigData *self,
                                             int addr_family,
                                             const NMPObject *obj_new,
                                             const NMPlatformIPAddress *pl_new,
                                             NML3ConfigAddFlags add_flags,
                                             const NMPObject **out_obj_new);

static inline gboolean
nm_l3_config_data_add_address (NML3ConfigData *self,
                               int addr_family,
                               const NMPObject *obj_new,
                               const NMPlatformIPAddress *pl_new)
{
	return nm_l3_config_data_add_address_full (self,
	                                           addr_family,
	                                           obj_new,
	                                           pl_new,
	                                           NM_L3_CONFIG_ADD_FLAGS_MERGE,
	                                           NULL);
}

static inline gboolean
nm_l3_config_data_add_address_4 (NML3ConfigData *self, const NMPlatformIP4Address *addr)
{
	return nm_l3_config_data_add_address (self, AF_INET, NULL, NM_PLATFORM_IP_ADDRESS_CAST (addr));
}

static inline gboolean
nm_l3_config_data_add_address_6 (NML3ConfigData *self, const NMPlatformIP6Address *addr)
{
	return nm_l3_config_data_add_address (self, AF_INET6, NULL, NM_PLATFORM_IP_ADDRESS_CAST (addr));
}

gboolean nm_l3_config_data_add_route_full (NML3ConfigData *self,
                                           int addr_family,
                                           const NMPObject *obj_new,
                                           const NMPlatformIPRoute *pl_new,
                                           NML3ConfigAddFlags add_flags,
                                           const NMPObject **out_obj_new,
                                           gboolean *out_changed_best_default_route);

static inline gboolean
nm_l3_config_data_add_route (NML3ConfigData *self,
                             int addr_family,
                             const NMPObject *obj_new,
                             const NMPlatformIPRoute *pl_new)
{
	return nm_l3_config_data_add_route_full (self,
	                                         addr_family,
	                                         obj_new,
	                                         pl_new,
	                                         NM_L3_CONFIG_ADD_FLAGS_MERGE,
	                                         NULL,
	                                         NULL);
}

static inline gboolean
nm_l3_config_data_add_route_4 (NML3ConfigData *self, const NMPlatformIP4Route *rt)
{
	return nm_l3_config_data_add_route (self, AF_INET, NULL, NM_PLATFORM_IP_ROUTE_CAST (rt));
}

static inline gboolean
nm_l3_config_data_add_route_6 (NML3ConfigData *self, const NMPlatformIP6Route *rt)
{
	return nm_l3_config_data_add_route (self, AF_INET6, NULL, NM_PLATFORM_IP_ROUTE_CAST (rt));
}

gboolean nm_l3_config_data_set_mdns (NML3ConfigData *self,
                                     NMSettingConnectionMdns mdns);

gboolean nm_l3_config_data_set_llmnr (NML3ConfigData *self,
                                      NMSettingConnectionLlmnr llmnr);

NMIPRouteTableSyncMode nm_l3_config_data_get_route_table_sync (const NML3ConfigData *self,
                                                               int addr_family);

gboolean nm_l3_config_data_set_route_table_sync (NML3ConfigData *self,
                                                 int addr_family,
                                                 NMIPRouteTableSyncMode route_table_sync);

gboolean nm_l3_config_data_set_metered (NML3ConfigData *self,
                                        NMTernary metered);

gboolean nm_l3_config_data_set_mtu (NML3ConfigData *self,
                                    guint32 mtu);

gboolean nm_l3_config_data_add_nameserver (NML3ConfigData *self,
                                           int addr_family,
                                           gconstpointer /* (const NMIPAddr *) */ nameserver);

gboolean nm_l3_config_data_add_wins (NML3ConfigData *self,
                                     in_addr_t wins);

gboolean nm_l3_config_data_add_nis_server (NML3ConfigData *self,
                                           in_addr_t nis_server);

gboolean nm_l3_config_data_set_nis_domain (NML3ConfigData *self,
                                           const char *nis_domain);

gboolean nm_l3_config_data_add_domain (NML3ConfigData *self,
                                       int addr_family,
                                       const char *domain);

gboolean nm_l3_config_data_add_search (NML3ConfigData *self,
                                       int addr_family,
                                       const char *search);

gboolean nm_l3_config_data_add_dns_option (NML3ConfigData *self,
                                           int addr_family,
                                           const char *dns_option);

gboolean nm_l3_config_data_set_dns_priority (NML3ConfigData *self,
                                             int addr_family,
                                             int dns_priority);

#endif /* __NM_L3_CONFIG_DATA_H__ */