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