File: interface.c

package info (click to toggle)
ifupdown-ng 0.12.1-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 964 kB
  • sloc: ansic: 3,572; sh: 980; makefile: 233
file content (371 lines) | stat: -rw-r--r-- 8,950 bytes parent folder | download | duplicates (4)
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
/*
 * libifupdown/interface.c
 * Purpose: interface management
 *
 * Copyright (c) 2020 Ariadne Conill <ariadne@dereferenced.org>
 * Copyright (c) 2020 Maximilian Wilhelm <max@sdn.clinic>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * This software is provided 'as is' and without any warranty, express or
 * implied.  In no event shall the authors be liable for any damages arising
 * from the use of this software.
 */

#include <stdio.h>
#include <string.h>
#include <sys/utsname.h>
#include "libifupdown/interface.h"
#include "libifupdown/config-file.h"

bool
lif_address_parse(struct lif_address *address, const char *presentation)
{
	char buf[512], *netmask_p;

	strlcpy(buf, presentation, sizeof buf);

	address->domain = strchr(buf, ':') != NULL ? AF_INET6 : AF_INET;

	netmask_p = strrchr(buf, '/');
	if (netmask_p != NULL)
	{
		*netmask_p++ = '\0';
		address->netmask = strtol(netmask_p, NULL, 10);
	}
	else
		address->netmask = 0;

	return !!inet_pton(address->domain, buf, address->addr_buf);
}

bool
lif_address_unparse(const struct lif_address *address, char *buf, size_t buflen, bool with_netmask)
{
	char workbuf[512] = {};

	if (!inet_ntop(address->domain, address->addr_buf, workbuf, sizeof workbuf))
		return false;

	if (!with_netmask || !address->netmask)
	{
		strlcpy(buf, workbuf, buflen);
		return true;
	}

	snprintf(buf, buflen, "%s/%zu", workbuf, address->netmask);
	return true;
}

static inline size_t
count_set_bits(const char *netmask)
{
        /* netmask set to CIDR length */
	if (strchr(netmask, '.') == NULL)
		return strtol(netmask, NULL, 10);

	size_t r = 0;
	struct in_addr in;

	if (inet_pton(AF_INET, netmask, &in) == 0)
		return r;

	/* take the IP, put it in host endian order, and
	 * flip it so that all the set bits are set to the right.
	 * then we can simply count down from 32 and right-shift
	 * until the bit field is all zero.
	 */
	unsigned int bits = htonl(in.s_addr);
	for (bits = ~bits, r = 32; bits; bits >>= 1, r--)
		;

	return r;
}

static inline size_t
determine_interface_netmask(const struct lif_interface *iface, const struct lif_address *addr)
{
	/* if netmask is not set, default to /24 or /64, ifupdown does so too */
	size_t netmask = addr->domain == AF_INET6 ? 64 : 24;

	struct lif_dict_entry *entry = lif_dict_find(&iface->vars, "netmask");
	if (entry != NULL)
		netmask = count_set_bits(entry->data);

	return netmask;
}

bool
lif_address_format_cidr(const struct lif_interface *iface, struct lif_dict_entry *entry, char *buf, size_t buflen)
{
	struct lif_address *addr = entry->data;
	size_t orig_netmask = addr->netmask;

	if (!addr->netmask)
		addr->netmask = determine_interface_netmask(iface, addr);

	if (!lif_address_unparse(addr, buf, buflen, true))
	{
		addr->netmask = orig_netmask;
		return false;
	}

	addr->netmask = orig_netmask;
	return true;
}

void
lif_interface_init(struct lif_interface *interface, const char *ifname)
{
	memset(interface, '\0', sizeof *interface);

	interface->ifname = strdup(ifname);

	lif_interface_use_executor(interface, "link");

	/* keep the 'vlan' executor as a config hint for backwards compatibility */
	if (strchr(ifname, '.') != NULL)
		lif_interface_use_executor(interface, "vlan");
}

bool
lif_interface_address_add(struct lif_interface *interface, const char *address)
{
	struct lif_address *addr = calloc(1, sizeof *addr);

	if (!lif_address_parse(addr, address))
	{
		free(addr);
		return false;
	}

	lif_interface_use_executor(interface, "static");

	lif_dict_add(&interface->vars, "address", addr);

	return true;
}

void
lif_interface_address_delete(struct lif_interface *interface, const char *address)
{
	struct lif_node *iter, *iter_next;
	struct lif_address addr;

	if (!lif_address_parse(&addr, address))
		return;

	LIF_DICT_FOREACH_SAFE(iter, iter_next, &interface->vars)
	{
		struct lif_dict_entry *entry = iter->data;

		if (strcmp(entry->key, "address"))
			continue;

		struct lif_address *entry_addr = entry->data;
		char addr_buf[512] = {};

		if (!lif_address_unparse(entry_addr, addr_buf, sizeof addr_buf, addr.netmask != 0))
			continue;

		if (strcmp(addr_buf, address))
			continue;

		lif_dict_delete_entry(&interface->vars, entry);
		free(entry_addr);
	}
}

void
lif_interface_fini(struct lif_interface *interface)
{
	struct lif_node *iter, *iter_next;

	LIF_DICT_FOREACH_SAFE(iter, iter_next, &interface->vars)
	{
		struct lif_dict_entry *entry = iter->data;

		free(entry->data);
		lif_dict_delete_entry(&interface->vars, entry);
	}

	free(interface->ifname);
}

void
lif_interface_use_executor(struct lif_interface *interface, const char *executor)
{
	char *exec_addon = strdup(executor);

	if (lif_dict_add_once(&interface->vars, "use", exec_addon, (lif_dict_cmp_t) strcmp) == NULL)
		free(exec_addon);

	/* pass requires as compatibility env vars to appropriate executors (bridge, bond) */
	if (!strcmp(executor, "bridge"))
		interface->is_bridge = true;
	else if (!strcmp(executor, "bond"))
		interface->is_bond = true;

	if (strcmp(executor, "dhcp") || !lif_config.use_hostname_for_dhcp)
		return;

	/* learn a reasonable default hostname */
	struct utsname un;
	if (uname(&un) < 0)
		return;

	lif_dict_add(&interface->vars, "dhcp-hostname", strdup(un.nodename));
}

void
lif_interface_finalize(struct lif_interface *interface)
{
	struct lif_node *iter;

	/* convert all addresses to CIDR notation. */
	LIF_DICT_FOREACH(iter, &interface->vars)
	{
		struct lif_dict_entry *entry = iter->data;

		if (strcmp(entry->key, "address"))
			continue;

		struct lif_address *addr = entry->data;

		if (!addr->netmask)
			addr->netmask = determine_interface_netmask(interface, addr);
	}

	/* with all addresses converted to CIDR, netmask property is no longer needed. */
	struct lif_dict_entry *entry = lif_dict_find(&interface->vars, "netmask");

	if (entry != NULL)
	{
		free(entry->data);

		lif_dict_delete_entry(&interface->vars, entry);
	}
}

void
lif_interface_collection_init(struct lif_dict *collection)
{
	struct lif_interface *if_lo;

	memset(collection, '\0', sizeof *collection);

	/* always enable loopback interface as part of a collection */
	if_lo = lif_interface_collection_find(collection, "lo");
	if_lo->is_auto = true;
	if_lo->is_explicit = true;
	lif_interface_use_executor(if_lo, "loopback");
}

void
lif_interface_collection_fini(struct lif_dict *collection)
{
	struct lif_node *iter, *iter_next;

	LIF_DICT_FOREACH_SAFE(iter, iter_next, collection)
	{
		struct lif_dict_entry *entry = iter->data;
		struct lif_interface *iface = entry->data;

		lif_interface_fini(iface);
		free(iface);

		lif_dict_delete_entry(collection, entry);
	}
}

struct lif_interface *
lif_interface_collection_find(struct lif_dict *collection, const char *ifname)
{
	struct lif_dict_entry *entry = lif_dict_find(collection, ifname);

	if (entry == NULL)
	{
		struct lif_interface *iface = calloc(1, sizeof *iface);

		lif_interface_init(iface, ifname);
		lif_dict_add(collection, ifname, iface);

		return iface;
	}

	return entry->data;
}

struct lif_interface *
lif_interface_collection_upsert(struct lif_dict *collection, struct lif_interface *interface)
{
	struct lif_dict_entry *entry = lif_dict_find(collection, interface->ifname);

	if (entry == NULL)
	{
		lif_dict_add(collection, interface->ifname, interface);
		return interface;
	}

	if (entry->data == interface)
		return interface;

	lif_interface_collection_delete(collection, entry->data);
	lif_dict_add(collection, interface->ifname, interface);

	return interface;
}

void
lif_interface_collection_delete(struct lif_dict *collection, struct lif_interface *interface)
{
	struct lif_dict_entry *entry = lif_dict_find(collection, interface->ifname);

	if (entry == NULL)
		return;

	lif_interface_fini(interface);
	free(interface);

	lif_dict_delete_entry(collection, entry);
}

bool
lif_interface_collection_inherit(struct lif_interface *interface, struct lif_interface *parent)
{
	/* maybe convert any interface we are inheriting from into a template */
	if (lif_config.implicit_template_conversion)
		parent->is_template = true;

	lif_dict_add(&interface->vars, "inherit", strdup(parent->ifname));
	interface->is_bond = parent->is_bond;
	interface->is_bridge = parent->is_bridge;

	/* copy the variables */
	struct lif_node *iter;
	LIF_DICT_FOREACH(iter, &parent->vars)
	{
		struct lif_dict_entry *entry = iter->data;

		if (!strcmp(entry->key, "address"))
		{
			struct lif_address *addr = calloc(1, sizeof *addr);
			struct lif_address *other_addr = entry->data;

			memcpy(addr, other_addr, sizeof *addr);

			lif_dict_add(&interface->vars, entry->key, addr);
		}
		else
		{
			char *value = strdup(entry->data);

			if (lif_dict_add_once(&interface->vars, entry->key, value, (lif_dict_cmp_t) strcmp) == NULL)
				free(value);
		}
	}

	return true;
}