lib/process: replace pb_run_cmd
[petitboot] / discover / network.c
1
2 #include <stdbool.h>
3 #include <stdint.h>
4 #include <string.h>
5 #include <stdlib.h>
6 #include <errno.h>
7 #include <sys/socket.h>
8 #include <linux/if.h>
9 #include <linux/netlink.h>
10 #include <linux/rtnetlink.h>
11
12 #include <log/log.h>
13 #include <list/list.h>
14 #include <talloc/talloc.h>
15 #include <waiter/waiter.h>
16 #include <pb-config/pb-config.h>
17 #include <process/process.h>
18 #include <system/system.h>
19
20 #include "file.h"
21 #include "network.h"
22
23 #define HWADDR_SIZE     6
24 #define PIDFILE_BASE    (LOCAL_STATE_DIR "/petitboot/")
25
26 #define for_each_nlmsg(buf, nlmsg, len) \
27         for (nlmsg = (struct nlmsghdr *)buf; \
28                 NLMSG_OK(nlmsg, len) && nlmsg->nlmsg_type != NLMSG_DONE; \
29                 nlmsg = NLMSG_NEXT(nlmsg, len))
30
31 #define for_each_rta(buf, rta, attrlen) \
32         for (rta = (struct rtattr *)(buf); RTA_OK(rta, attrlen); \
33                         rta = RTA_NEXT(rta, attrlen))
34
35
36 struct interface {
37         int     ifindex;
38         char    name[IFNAMSIZ];
39         uint8_t hwaddr[HWADDR_SIZE];
40
41         enum {
42                 IFSTATE_NEW,
43                 IFSTATE_UP_WAITING_LINK,
44                 IFSTATE_CONFIGURED,
45                 IFSTATE_IGNORED,
46         } state;
47
48         struct list_item list;
49         struct process *udhcpc_process;
50 };
51
52 struct network {
53         struct list     interfaces;
54         struct waiter   *waiter;
55         int             netlink_sd;
56         bool            manual_config;
57         bool            dry_run;
58 };
59
60 static const struct interface_config *find_config_by_hwaddr(
61                 uint8_t *hwaddr)
62 {
63         const struct config *config;
64         int i;
65
66         config = config_get();
67         if (!config)
68                 return NULL;
69
70         for (i = 0; i < config->network.n_interfaces; i++) {
71                 struct interface_config *ifconf = config->network.interfaces[i];
72
73                 if (!memcmp(ifconf->hwaddr, hwaddr, HWADDR_SIZE))
74                         return ifconf;
75         }
76
77         return NULL;
78 }
79
80 static struct interface *find_interface_by_ifindex(struct network *network,
81                 int ifindex)
82 {
83         struct interface *interface;
84
85         list_for_each_entry(&network->interfaces, interface, list)
86                 if (interface->ifindex == ifindex)
87                         return interface;
88
89         return NULL;
90 }
91
92 static int network_init_netlink(struct network *network)
93 {
94         struct sockaddr_nl addr;
95         int rc;
96
97         memset(&addr, 0, sizeof(addr));
98         addr.nl_family = AF_NETLINK;
99         addr.nl_groups = RTMGRP_LINK;
100
101         network->netlink_sd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
102         if (network->netlink_sd < 0) {
103                 perror("socket(AF_NETLINK)");
104                 return -1;
105         }
106
107         rc = bind(network->netlink_sd, (struct sockaddr *)&addr, sizeof(addr));
108         if (rc) {
109                 perror("bind(sockaddr_nl)");
110                 close(network->netlink_sd);
111                 return -1;
112         }
113
114         return 0;
115 }
116
117 static int network_send_link_query(struct network *network)
118 {
119         int rc;
120         struct {
121                 struct nlmsghdr nlmsg;
122                 struct rtgenmsg rtmsg;
123         } msg;
124
125         memset(&msg, 0, sizeof(msg));
126
127         msg.nlmsg.nlmsg_len = sizeof(msg);
128         msg.nlmsg.nlmsg_type = RTM_GETLINK;
129         msg.nlmsg.nlmsg_flags = NLM_F_REQUEST | NLM_F_ROOT;
130         msg.nlmsg.nlmsg_seq = 0;
131         msg.nlmsg.nlmsg_pid = 0;
132         msg.rtmsg.rtgen_family = AF_UNSPEC;
133
134         rc = send(network->netlink_sd, &msg, sizeof(msg), MSG_NOSIGNAL);
135         if (rc != sizeof(msg))
136                 return -1;
137
138         return 0;
139 }
140
141 static int interface_change(struct interface *interface, bool up)
142 {
143         const char *statestr = up ? "up" : "down";
144         int rc;
145
146         if (!up && interface->udhcpc_process) {
147                 /* we don't care about the callback from here */
148                 interface->udhcpc_process->exit_cb = NULL;
149                 interface->udhcpc_process->data = NULL;
150                 process_stop_async(interface->udhcpc_process);
151                 process_release(interface->udhcpc_process);
152         }
153
154         rc = process_run_simple(interface, pb_system_apps.ip,
155                         "link", "set", interface->name, statestr, NULL);
156         if (rc) {
157                 pb_log("failed to bring interface %s %s\n", interface->name,
158                                 statestr);
159                 return -1;
160         }
161         return 0;
162 }
163
164 static int interface_up(struct interface *interface)
165 {
166         return interface_change(interface, true);
167 }
168
169 static int interface_down(struct interface *interface)
170 {
171         return interface_change(interface, false);
172 }
173
174 static void udhcpc_process_exit(struct process *process)
175 {
176         struct interface *interface = process->data;
177         pb_log("udhcp client [pid %d] for interface %s exited, rc %d\n",
178                         process->pid, interface->name, process->exit_status);
179         interface->udhcpc_process = NULL;
180         process_release(process);
181 }
182
183 static void configure_interface_dhcp(struct interface *interface)
184 {
185         struct process *process;
186         char pidfile[256];
187         int rc;
188         const char *argv[] = {
189                 pb_system_apps.udhcpc,
190                 "-R",
191                 "-n",
192                 "-p", pidfile,
193                 "-i", interface->name,
194                 NULL,
195         };
196         snprintf(pidfile, sizeof(pidfile), "%s/udhcpc-%s.pid",
197                         PIDFILE_BASE, interface->name);
198
199         process = process_create(interface);
200
201         process->path = pb_system_apps.udhcpc;
202         process->argv = argv;
203         process->exit_cb = udhcpc_process_exit;
204         process->data = interface;
205
206         rc = process_run_async(process);
207
208         if (rc)
209                 process_release(process);
210         else
211                 interface->udhcpc_process = process;
212
213         return;
214 }
215
216 static void configure_interface_static(struct interface *interface,
217                 const struct interface_config *config)
218 {
219         int rc;
220
221         rc = process_run_simple(interface, pb_system_apps.ip,
222                         "address", "add", config->static_config.address,
223                         "dev", interface->name, NULL);
224
225
226         if (rc) {
227                 pb_log("failed to add address %s to interface %s\n",
228                                 config->static_config.address,
229                                 interface->name);
230                 return;
231         }
232
233         /* we need the interface up before we can route through it */
234         rc = interface_up(interface);
235         if (rc)
236                 return;
237
238         if (config->static_config.gateway)
239                 rc = process_run_simple(interface, pb_system_apps.ip,
240                                 "route", "add", "default",
241                                 "via", config->static_config.gateway,
242                                 NULL);
243
244         if (rc) {
245                 pb_log("failed to add default route %s on interface %s\n",
246                                 config->static_config.gateway,
247                                 interface->name);
248         }
249
250         return;
251 }
252
253 static void configure_interface(struct network *network,
254                 struct interface *interface, bool up, bool link)
255 {
256         const struct interface_config *config = NULL;
257
258         if (interface->state == IFSTATE_IGNORED)
259                 return;
260
261         /* old interface? check that we're still up and running */
262         if (interface->state == IFSTATE_CONFIGURED) {
263                 if (!up)
264                         interface->state = IFSTATE_NEW;
265                 else if (!link)
266                         interface->state = IFSTATE_UP_WAITING_LINK;
267                 else
268                         return;
269         }
270
271         /* always up the lookback, no other handling required */
272         if (!strcmp(interface->name, "lo")) {
273                 if (interface->state == IFSTATE_NEW)
274                         interface_up(interface);
275                 interface->state = IFSTATE_CONFIGURED;
276                 return;
277         }
278
279         config = find_config_by_hwaddr(interface->hwaddr);
280         if (config && config->ignore) {
281                 pb_log("network: ignoring interface %s\n", interface->name);
282                 interface->state = IFSTATE_IGNORED;
283                 return;
284         }
285
286         /* if we're in manual config mode, we need an interface configuration */
287         if (network->manual_config && !config) {
288                 interface->state = IFSTATE_IGNORED;
289                 pb_log("network: skipping %s: manual config mode, "
290                                 "but no config for this interface\n",
291                                 interface->name);
292                 return;
293         }
294
295         /* new interface? bring up to the point so we can detect a link */
296         if (interface->state == IFSTATE_NEW) {
297                 if (!up) {
298                         interface_up(interface);
299                         pb_log("network: bringing up interface %s\n",
300                                         interface->name);
301                         return;
302
303                 } else if (!link) {
304                         interface->state = IFSTATE_UP_WAITING_LINK;
305                 }
306         }
307
308         /* no link? wait for a notification */
309         if (interface->state == IFSTATE_UP_WAITING_LINK && !link)
310                 return;
311
312         pb_log("network: configuring interface %s\n", interface->name);
313
314         if (!config || config->method == CONFIG_METHOD_DHCP) {
315                 configure_interface_dhcp(interface);
316
317         } else if (config->method == CONFIG_METHOD_STATIC) {
318                 configure_interface_static(interface, config);
319         }
320 }
321
322 static int network_handle_nlmsg(struct network *network, struct nlmsghdr *nlmsg)
323 {
324         bool have_ifaddr, have_ifname;
325         struct interface *interface;
326         struct ifinfomsg *info;
327         struct rtattr *attr;
328         uint8_t ifaddr[6];
329         char ifname[IFNAMSIZ+1];
330         int attrlen, type;
331
332
333         /* we're only interested in NEWLINK messages */
334         type = nlmsg->nlmsg_type;
335         if (!(type == RTM_NEWLINK || type == RTM_DELLINK))
336                 return 0;
337
338         info = NLMSG_DATA(nlmsg);
339
340         have_ifaddr = have_ifname = false;
341
342         attrlen = nlmsg->nlmsg_len - sizeof(*info);
343
344         /* extract the interface name and hardware address attributes */
345         for_each_rta(info + 1, attr, attrlen) {
346                 void *data = RTA_DATA(attr);
347
348                 switch (attr->rta_type) {
349                 case IFLA_ADDRESS:
350                         memcpy(ifaddr, data, sizeof(ifaddr));
351                         have_ifaddr = true;
352                         break;
353
354                 case IFLA_IFNAME:
355                         strncpy(ifname, data, IFNAMSIZ);
356                         have_ifname = true;
357                         break;
358                 }
359         }
360
361         if (!have_ifaddr || !have_ifname)
362                 return -1;
363
364         if (type == RTM_DELLINK) {
365                 interface = find_interface_by_ifindex(network, info->ifi_index);
366                 if (!interface)
367                         return 0;
368                 pb_log("network: interface %s removed\n", interface->name);
369                 list_remove(&interface->list);
370                 talloc_free(interface);
371                 return 0;
372         }
373
374
375         interface = find_interface_by_ifindex(network, info->ifi_index);
376         if (!interface) {
377                 interface = talloc_zero(network, struct interface);
378                 interface->ifindex = info->ifi_index;
379                 interface->state = IFSTATE_NEW;
380                 memcpy(interface->hwaddr, ifaddr, sizeof(interface->hwaddr));
381                 strncpy(interface->name, ifname, sizeof(interface->name) - 1);
382         }
383
384         configure_interface(network, interface,
385                         info->ifi_flags & IFF_UP,
386                         info->ifi_flags & IFF_LOWER_UP);
387
388         return 0;
389 }
390
391 static int network_netlink_process(void *arg)
392 {
393         struct network *network = arg;
394         struct nlmsghdr *nlmsg;
395         unsigned int len;
396         char buf[4096];
397         int rc;
398
399         rc = recv(network->netlink_sd, buf, sizeof(buf), 0);
400         if (rc < 0) {
401                 perror("netlink recv");
402                 return -1;
403         }
404
405         len = rc;
406
407         for_each_nlmsg(buf, nlmsg, len)
408                 network_handle_nlmsg(network, nlmsg);
409
410         return 0;
411 }
412
413 static void network_init_dns(struct network *network)
414 {
415         const struct config *config;
416         int i, rc, len;
417         bool modified;
418         char *buf;
419
420         if (network->dry_run)
421                 return;
422
423         config = config_get();
424         if (!config || !config->network.n_dns_servers)
425                 return;
426
427         rc = read_file(network, "/etc/resolv.conf", &buf, &len);
428
429         if (rc) {
430                 buf = talloc_strdup(network, "");
431                 len = 0;
432         }
433
434         modified = false;
435
436         for (i = 0; i < config->network.n_dns_servers; i++) {
437                 int dns_conf_len;
438                 char *dns_conf;
439
440                 dns_conf = talloc_asprintf(network, "nameserver %s\n",
441                                 config->network.dns_servers[i]);
442
443                 if (strstr(buf, dns_conf)) {
444                         talloc_free(dns_conf);
445                         continue;
446                 }
447
448                 dns_conf_len = strlen(dns_conf);
449                 buf = talloc_realloc(network, buf, char, len + dns_conf_len);
450                 memcpy(buf + len, dns_conf, dns_conf_len);
451                 len += dns_conf_len;
452                 modified = true;
453
454                 talloc_free(dns_conf);
455         }
456
457         if (modified) {
458                 rc = replace_file("/etc/resolv.conf", buf, len);
459                 if (rc)
460                         pb_log("error replacing resolv.conf: %s\n",
461                                         strerror(errno));
462         }
463
464         talloc_free(buf);
465 }
466
467 struct network *network_init(void *ctx, struct waitset *waitset, bool dry_run)
468 {
469         struct network *network;
470         int rc;
471
472         network = talloc(ctx, struct network);
473         list_init(&network->interfaces);
474         network->manual_config = false;
475         network->dry_run = dry_run;
476
477         network_init_dns(network);
478
479         rc = network_init_netlink(network);
480         if (rc)
481                 goto err;
482
483         network->waiter = waiter_register_io(waitset, network->netlink_sd,
484                         WAIT_IN, network_netlink_process, network);
485
486         if (!network->waiter)
487                 goto err;
488
489         rc = network_send_link_query(network);
490         if (rc)
491                 goto err;
492
493         return network;
494
495 err:
496         network_shutdown(network);
497         return NULL;
498 }
499
500
501 int network_shutdown(struct network *network)
502 {
503         struct interface *interface;
504
505         if (network->waiter)
506                 waiter_remove(network->waiter);
507
508         list_for_each_entry(&network->interfaces, interface, list)
509                 interface_down(interface);
510
511         close(network->netlink_sd);
512         talloc_free(network);
513         return 0;
514 }