summaryrefslogtreecommitdiffstats
path: root/discover/network.c
blob: 4a4f93252f5a1f02693222e2ca1d6560d2a0995b (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
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

#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <linux/if.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>

#include <log/log.h>
#include <list/list.h>
#include <talloc/talloc.h>
#include <waiter/waiter.h>
#include <pb-config/pb-config.h>
#include <system/system.h>

#include "network.h"

#define HWADDR_SIZE	6
#define PIDFILE_BASE	(LOCAL_STATE_DIR "/petitboot/")

#define for_each_nlmsg(buf, nlmsg, len) \
	for (nlmsg = (struct nlmsghdr *)buf; \
		NLMSG_OK(nlmsg, len) && nlmsg->nlmsg_type != NLMSG_DONE; \
		nlmsg = NLMSG_NEXT(nlmsg, len))

#define for_each_rta(buf, rta, attrlen) \
	for (rta = (struct rtattr *)(buf); RTA_OK(rta, attrlen); \
			rta = RTA_NEXT(rta, attrlen))


struct interface {
	int	ifindex;
	char	name[IFNAMSIZ];
	uint8_t	hwaddr[HWADDR_SIZE];

	enum {
		IFSTATE_NEW,
		IFSTATE_UP_WAITING_LINK,
		IFSTATE_CONFIGURED,
		IFSTATE_IGNORED,
	} state;

	struct list_item list;
};

struct network {
	struct list	interfaces;
	struct waiter	*waiter;
	int		netlink_sd;
	bool		manual_config;
	bool		dry_run;
};

static const struct network_config *find_config_by_hwaddr(
		uint8_t *hwaddr)
{
	const struct config *config;
	int i;

	config = config_get();
	if (!config)
		return NULL;

	for (i = 0; i < config->n_network_configs; i++) {
		struct network_config *netconf = config->network_configs[i];

		if (!memcmp(netconf->hwaddr, hwaddr, HWADDR_SIZE))
			return netconf;
	}

	return NULL;
}

static struct interface *find_interface_by_ifindex(struct network *network,
		int ifindex)
{
	struct interface *interface;

	list_for_each_entry(&network->interfaces, interface, list)
		if (interface->ifindex == ifindex)
			return interface;

	return NULL;
}

static int network_init_netlink(struct network *network)
{
	struct sockaddr_nl addr;
	int rc;

	memset(&addr, 0, sizeof(addr));
	addr.nl_family = AF_NETLINK;
	addr.nl_groups = RTMGRP_LINK;

	network->netlink_sd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
	if (network->netlink_sd < 0) {
		perror("socket(AF_NETLINK)");
		return -1;
	}

	rc = bind(network->netlink_sd, (struct sockaddr *)&addr, sizeof(addr));
	if (rc) {
		perror("bind(sockaddr_nl)");
		close(network->netlink_sd);
		return -1;
	}

	return 0;
}

static int network_send_link_query(struct network *network)
{
	int rc;
	struct {
		struct nlmsghdr nlmsg;
		struct rtgenmsg rtmsg;
	} msg;

	memset(&msg, 0, sizeof(msg));

	msg.nlmsg.nlmsg_len = sizeof(msg);
	msg.nlmsg.nlmsg_type = RTM_GETLINK;
	msg.nlmsg.nlmsg_flags = NLM_F_REQUEST | NLM_F_ROOT;
	msg.nlmsg.nlmsg_seq = 0;
	msg.nlmsg.nlmsg_pid = 0;
	msg.rtmsg.rtgen_family = AF_UNSPEC;

	rc = send(network->netlink_sd, &msg, sizeof(msg), MSG_NOSIGNAL);
	if (rc != sizeof(msg))
		return -1;

	return 0;
}

static int interface_up(struct network *network, struct interface *interface)
{
	int rc;
	const char *argv[] = {
		pb_system_apps.ip,
		"link",
		"set",
		interface->name,
		"up",
		NULL,
	};

	rc = pb_run_cmd(argv, 1, network->dry_run);
	if (rc) {
		pb_log("failed to bring interface %s up\n", interface->name);
		return -1;
	}
	return 0;
}

static void configure_interface_dhcp(struct network *network,
		struct interface *interface)
{
	char pidfile[256];
	const char *argv[] = {
		pb_system_apps.udhcpc,
		"-R",
		"-n",
		"-p", pidfile,
		"-i", interface->name,
		NULL,
	};
	snprintf(pidfile, sizeof(pidfile), "%s/udhcpc-%s.pid",
			PIDFILE_BASE, interface->name);

	pb_run_cmd(argv, 0, network->dry_run);
	return;
}

static void configure_interface_static(struct network *network,
		struct interface *interface,
		const struct network_config *config)
{
	const char *addr_argv[] = {
		pb_system_apps.ip,
		"address",
		"add",
		config->static_config.address,
		"dev",
		interface->name,
		NULL,
	};
	const char *route_argv[] = {
		pb_system_apps.ip,
		"route",
		"add",
		"default",
		"via",
		config->static_config.gateway,
		NULL,
	};
	int rc;


	rc = pb_run_cmd(addr_argv, 1, network->dry_run);
	if (rc) {
		pb_log("failed to add address %s to interface %s\n",
				config->static_config.address,
				interface->name);
		return;
	}

	/* we need the interface up before we can route through it */
	rc = interface_up(network, interface);
	if (rc)
		return;

	if (config->static_config.gateway)
		rc = pb_run_cmd(route_argv, 1, network->dry_run);

	if (rc) {
		pb_log("failed to add default route %s on interface %s\n",
				config->static_config.gateway,
				interface->name);
	}

	return;
}

static void configure_interface(struct network *network,
		struct interface *interface, bool up, bool link)
{
	const struct network_config *config = NULL;

	if (interface->state == IFSTATE_IGNORED)
		return;

	/* old interface? check that we're still up and running */
	if (interface->state == IFSTATE_CONFIGURED) {
		if (!up)
			interface->state = IFSTATE_NEW;
		else if (!link)
			interface->state = IFSTATE_UP_WAITING_LINK;
		else
			return;
	}

	/* always up the lookback, no other handling required */
	if (!strcmp(interface->name, "lo")) {
		if (interface->state == IFSTATE_NEW)
			interface_up(network, interface);
		interface->state = IFSTATE_CONFIGURED;
		return;
	}

	config = find_config_by_hwaddr(interface->hwaddr);
	if (config && config->ignore) {
		pb_log("network: ignoring interface %s\n", interface->name);
		interface->state = IFSTATE_IGNORED;
		return;
	}

	/* if we're in manual config mode, we need an interface configuration */
	if (network->manual_config && !config) {
		interface->state = IFSTATE_IGNORED;
		pb_log("network: skipping %s: manual config mode, "
				"but no config for this interface\n",
				interface->name);
		return;
	}

	/* new interface? bring up to the point so we can detect a link */
	if (interface->state == IFSTATE_NEW) {
		if (!up) {
			interface_up(network, interface);
			pb_log("network: bringing up interface %s\n",
					interface->name);
			return;

		} else if (!link) {
			interface->state = IFSTATE_UP_WAITING_LINK;
		}
	}

	/* no link? wait for a notification */
	if (interface->state == IFSTATE_UP_WAITING_LINK && !link)
		return;

	pb_log("network: configuring interface %s\n", interface->name);

	if (!config || config->method == CONFIG_METHOD_DHCP) {
		configure_interface_dhcp(network, interface);

	} else if (config->method == CONFIG_METHOD_STATIC) {
		configure_interface_static(network, interface, config);
	}
}

static int network_handle_nlmsg(struct network *network, struct nlmsghdr *nlmsg)
{
	bool have_ifaddr, have_ifname;
	struct interface *interface;
	struct ifinfomsg *info;
	struct rtattr *attr;
	uint8_t ifaddr[6];
	char ifname[IFNAMSIZ+1];
	int attrlen, type;


	/* we're only interested in NEWLINK messages */
	type = nlmsg->nlmsg_type;
	if (!(type == RTM_NEWLINK || type == RTM_DELLINK))
		return 0;

	info = NLMSG_DATA(nlmsg);

	have_ifaddr = have_ifname = false;

	attrlen = nlmsg->nlmsg_len - sizeof(*info);

	/* extract the interface name and hardware address attributes */
	for_each_rta(info + 1, attr, attrlen) {
		void *data = RTA_DATA(attr);

		switch (attr->rta_type) {
		case IFLA_ADDRESS:
			memcpy(ifaddr, data, sizeof(ifaddr));
			have_ifaddr = true;
			break;

		case IFLA_IFNAME:
			strncpy(ifname, data, IFNAMSIZ);
			have_ifname = true;
			break;
		}
	}

	if (!have_ifaddr || !have_ifname)
		return -1;

	if (type == RTM_DELLINK) {
		interface = find_interface_by_ifindex(network, info->ifi_index);
		if (!interface)
			return 0;
		pb_log("network: interface %s removed\n", interface->name);
		list_remove(&interface->list);
		talloc_free(interface);
		return 0;
	}


	interface = find_interface_by_ifindex(network, info->ifi_index);
	if (!interface) {
		interface = talloc_zero(network, struct interface);
		interface->ifindex = info->ifi_index;
		interface->state = IFSTATE_NEW;
		memcpy(interface->hwaddr, ifaddr, sizeof(interface->hwaddr));
		strncpy(interface->name, ifname, sizeof(interface->name) - 1);
	}

	configure_interface(network, interface,
			info->ifi_flags & IFF_UP,
			info->ifi_flags & IFF_LOWER_UP);

	return 0;
}

static int network_netlink_process(void *arg)
{
	struct network *network = arg;
	struct nlmsghdr *nlmsg;
	unsigned int len;
	char buf[4096];
	int rc;

	rc = recv(network->netlink_sd, buf, sizeof(buf), 0);
	if (rc < 0) {
		perror("netlink recv");
		return -1;
	}

	len = rc;

	for_each_nlmsg(buf, nlmsg, len)
		network_handle_nlmsg(network, nlmsg);

	return 0;
}

struct network *network_init(void *ctx, struct waitset *waitset, bool dry_run)
{
	struct network *network;
	int rc;

	network = talloc(ctx, struct network);
	list_init(&network->interfaces);
	network->manual_config = false;
	network->dry_run = dry_run;

	rc = network_init_netlink(network);
	if (rc)
		goto err;

	network->waiter = waiter_register_io(waitset, network->netlink_sd,
			WAIT_IN, network_netlink_process, network);

	if (!network->waiter)
		goto err;

	rc = network_send_link_query(network);
	if (rc)
		goto err;

	return network;

err:
	network_shutdown(network);
	return NULL;
}


int network_shutdown(struct network *network)
{
	if (network->waiter)
		waiter_remove(network->waiter);

	close(network->netlink_sd);
	talloc_free(network);
	return 0;
}
OpenPOWER on IntegriCloud