]> git.ozlabs.org Git - petitboot/blob - discover/network.c
discover/network: Ensure interfaces have device before configuring
[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 #include <i18n/i18n.h>
12
13 #include <log/log.h>
14 #include <list/list.h>
15 #include <file/file.h>
16 #include <types/types.h>
17 #include <talloc/talloc.h>
18 #include <waiter/waiter.h>
19 #include <process/process.h>
20 #include <system/system.h>
21
22 #include "network.h"
23 #include "sysinfo.h"
24 #include "platform.h"
25 #include "device-handler.h"
26
27 #define HWADDR_SIZE     6
28 #define PIDFILE_BASE    (LOCAL_STATE_DIR "/petitboot/")
29 #define INITIAL_BUFSIZE 4096
30
31 #define for_each_nlmsg(buf, nlmsg, len) \
32         for (nlmsg = (struct nlmsghdr *)buf; \
33                 NLMSG_OK(nlmsg, len) && nlmsg->nlmsg_type != NLMSG_DONE; \
34                 nlmsg = NLMSG_NEXT(nlmsg, len))
35
36 #define for_each_rta(buf, rta, attrlen) \
37         for (rta = (struct rtattr *)(buf); RTA_OK(rta, attrlen); \
38                         rta = RTA_NEXT(rta, attrlen))
39
40
41 struct interface {
42         int     ifindex;
43         char    name[IFNAMSIZ];
44         uint8_t hwaddr[HWADDR_SIZE];
45
46         enum {
47                 IFSTATE_NEW,
48                 IFSTATE_UP_WAITING_LINK,
49                 IFSTATE_CONFIGURED,
50                 IFSTATE_IGNORED,
51         } state;
52
53         struct list_item list;
54         struct process *udhcpc_process;
55         struct discover_device *dev;
56 };
57
58 struct network {
59         struct list             interfaces;
60         struct device_handler   *handler;
61         struct waiter           *waiter;
62         int                     netlink_sd;
63         void                    *netlink_buf;
64         unsigned int            netlink_buf_size;
65         bool                    manual_config;
66         bool                    dry_run;
67 };
68
69 static char *mac_bytes_to_string(void *ctx, uint8_t *addr, int len)
70 {
71         const int l = strlen("xx:");
72         char *buf;
73         int i;
74
75         if (len <= 0)
76                 return talloc_strdup(ctx, "");
77
78         buf = talloc_array(ctx, char, (len * l) + 1);
79
80         for (i = 0; i < len; i++)
81                 sprintf(buf + (l * i), "%02x:", addr[i]);
82
83         *(buf + (l * len) - 1) = '\0';
84
85         return buf;
86 }
87
88 static const struct interface_config *find_config_by_hwaddr(
89                 uint8_t *hwaddr)
90 {
91         const struct config *config;
92         unsigned int i;
93
94         config = config_get();
95         if (!config)
96                 return NULL;
97
98         for (i = 0; i < config->network.n_interfaces; i++) {
99                 struct interface_config *ifconf = config->network.interfaces[i];
100
101                 if (!memcmp(ifconf->hwaddr, hwaddr, HWADDR_SIZE))
102                         return ifconf;
103         }
104
105         return NULL;
106 }
107
108 static struct interface *find_interface_by_ifindex(struct network *network,
109                 int ifindex)
110 {
111         struct interface *interface;
112
113         list_for_each_entry(&network->interfaces, interface, list)
114                 if (interface->ifindex == ifindex)
115                         return interface;
116
117         return NULL;
118 }
119
120 static struct interface *find_interface_by_name(struct network *network,
121                 const char *name)
122 {
123         struct interface *interface;
124
125         list_for_each_entry(&network->interfaces, interface, list)
126                 if (!strcmp(interface->name, name))
127                         return interface;
128
129         return NULL;
130 }
131
132 static struct interface *find_interface_by_uuid(struct network *network,
133                 const char *uuid)
134 {
135         struct interface *interface;
136         char *mac;
137
138         list_for_each_entry(&network->interfaces, interface, list) {
139                 mac = mac_bytes_to_string(interface, interface->hwaddr,
140                                         sizeof(interface->hwaddr));
141                 if (!strcmp(mac, uuid)) {
142                         talloc_free(mac);
143                         return interface;
144                 }
145                 talloc_free(mac);
146         }
147
148         return NULL;
149 }
150
151 uint8_t *find_mac_by_name(void *ctx, struct network *network,
152                 const char *name)
153 {
154         struct interface *interface;
155
156         interface = find_interface_by_name(network, name);
157         if (!interface)
158                 return NULL;
159
160         return talloc_memdup(ctx, &interface->hwaddr,
161                              sizeof(uint8_t) * HWADDR_SIZE);
162 }
163
164 static int network_init_netlink(struct network *network)
165 {
166         struct sockaddr_nl addr;
167         int rc;
168
169         memset(&addr, 0, sizeof(addr));
170         addr.nl_family = AF_NETLINK;
171         addr.nl_groups = RTMGRP_LINK;
172
173         network->netlink_sd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
174         if (network->netlink_sd < 0) {
175                 perror("socket(AF_NETLINK)");
176                 return -1;
177         }
178
179         rc = bind(network->netlink_sd, (struct sockaddr *)&addr, sizeof(addr));
180         if (rc) {
181                 perror("bind(sockaddr_nl)");
182                 close(network->netlink_sd);
183                 return -1;
184         }
185
186         network->netlink_buf_size = INITIAL_BUFSIZE;
187         network->netlink_buf = talloc_array(network, char,
188                                 network->netlink_buf_size);
189
190         return 0;
191 }
192
193 static int network_send_link_query(struct network *network)
194 {
195         int rc;
196         struct {
197                 struct nlmsghdr nlmsg;
198                 struct rtgenmsg rtmsg;
199         } msg;
200
201         memset(&msg, 0, sizeof(msg));
202
203         msg.nlmsg.nlmsg_len = sizeof(msg);
204         msg.nlmsg.nlmsg_type = RTM_GETLINK;
205         msg.nlmsg.nlmsg_flags = NLM_F_REQUEST | NLM_F_ROOT;
206         msg.nlmsg.nlmsg_seq = 0;
207         msg.nlmsg.nlmsg_pid = 0;
208         msg.rtmsg.rtgen_family = AF_UNSPEC;
209
210         rc = send(network->netlink_sd, &msg, sizeof(msg), MSG_NOSIGNAL);
211         if (rc != sizeof(msg))
212                 return -1;
213
214         return 0;
215 }
216
217 static void create_interface_dev(struct network *network,
218                 struct interface *interface)
219 {
220         char *uuid = mac_bytes_to_string(interface, interface->hwaddr,
221                                                 sizeof(interface->hwaddr));
222
223         interface->dev = discover_device_create(network->handler, uuid,
224                                                 interface->name);
225         interface->dev->device->type = DEVICE_TYPE_NETWORK;
226         device_handler_add_device(network->handler, interface->dev);
227         talloc_free(uuid);
228 }
229
230 static void remove_interface(struct network *network,
231                 struct interface *interface)
232 {
233         if (interface->dev)
234                 device_handler_remove(network->handler, interface->dev);
235         list_remove(&interface->list);
236         talloc_free(interface);
237 }
238
239 void network_register_device(struct network *network,
240                 struct discover_device *dev)
241 {
242         struct interface *iface;
243
244         if (dev->uuid)
245                 iface = find_interface_by_uuid(network, dev->uuid);
246         else
247                 iface = find_interface_by_name(network, dev->label);
248         if (!iface)
249                 return;
250
251         iface->dev = dev;
252         dev->uuid = mac_bytes_to_string(iface->dev, iface->hwaddr,
253                         sizeof(iface->hwaddr));
254 }
255
256 void network_unregister_device(struct network *network,
257                 struct discover_device *dev)
258 {
259         struct interface *iface;
260
261         iface = find_interface_by_uuid(network, dev->uuid);
262         if (!iface)
263                 return;
264
265         iface->dev = NULL;
266 }
267
268 static int interface_change(struct interface *interface, bool up)
269 {
270         const char *statestr = up ? "up" : "down";
271         int rc;
272
273         if (!up && interface->udhcpc_process) {
274                 /* we don't care about the callback from here */
275                 interface->udhcpc_process->exit_cb = NULL;
276                 interface->udhcpc_process->data = NULL;
277                 process_stop_async(interface->udhcpc_process);
278                 process_release(interface->udhcpc_process);
279         }
280
281         if (!up) {
282                 rc = process_run_simple(interface, pb_system_apps.ip,
283                                 "address", "flush", "dev", interface->name,
284                                 NULL);
285                 if (rc)
286                         pb_log("failed to flush addresses from interface %s\n",
287                                 interface->name);
288         }
289
290         rc = process_run_simple(interface, pb_system_apps.ip,
291                         "link", "set", interface->name, statestr, NULL);
292         if (rc) {
293                 pb_log("failed to bring interface %s %s\n", interface->name,
294                                 statestr);
295                 return -1;
296         }
297         return 0;
298 }
299
300 static int interface_up(struct interface *interface)
301 {
302         return interface_change(interface, true);
303 }
304
305 static int interface_down(struct interface *interface)
306 {
307         return interface_change(interface, false);
308 }
309
310 static void udhcpc_process_exit(struct process *process)
311 {
312         struct interface *interface = process->data;
313         pb_debug("udhcp client [pid %d] for interface %s exited, rc %d\n",
314                         process->pid, interface->name, process->exit_status);
315         interface->udhcpc_process = NULL;
316         process_release(process);
317 }
318
319 static void configure_interface_dhcp(struct network *network,
320                 struct interface *interface)
321 {
322         const struct platform *platform;
323         char pidfile[256], id[10];
324         struct process *process;
325         int rc;
326         const char *argv[] = {
327                 pb_system_apps.udhcpc,
328                 "-R",
329                 "-f",
330                 "-O", "pxeconffile",
331                 "-O", "pxepathprefix",
332                 "-p", pidfile,
333                 "-i", interface->name,
334                 "-x", id, /* [11,12] - dhcp client identifier */
335                 NULL,
336         };
337
338         device_handler_status_dev_info(network->handler, interface->dev,
339                         _("Configuring with DHCP"));
340
341         snprintf(pidfile, sizeof(pidfile), "%s/udhcpc-%s.pid",
342                         PIDFILE_BASE, interface->name);
343
344         platform = platform_get();
345         if (platform && platform->dhcp_arch_id != 0xffff)
346                 snprintf(id, sizeof(id), "0x5d:%04x", platform->dhcp_arch_id);
347         else
348                 argv[11] = NULL;
349
350         process = process_create(interface);
351
352         process->path = pb_system_apps.udhcpc;
353         process->argv = argv;
354         process->exit_cb = udhcpc_process_exit;
355         process->data = interface;
356
357         rc = process_run_async(process);
358
359         if (rc)
360                 process_release(process);
361         else
362                 interface->udhcpc_process = process;
363
364         return;
365 }
366
367 static void configure_interface_static(struct network *network,
368                 struct interface *interface,
369                 const struct interface_config *config)
370 {
371         int rc;
372
373         device_handler_status_dev_info(network->handler, interface->dev,
374                         _("Configuring with static address (ip: %s)"),
375                         config->static_config.address);
376
377         rc = process_run_simple(interface, pb_system_apps.ip,
378                         "address", "add", config->static_config.address,
379                         "dev", interface->name, NULL);
380
381
382         if (rc) {
383                 pb_log("failed to add address %s to interface %s\n",
384                                 config->static_config.address,
385                                 interface->name);
386                 return;
387         }
388
389         system_info_set_interface_address(sizeof(interface->hwaddr),
390                                 interface->hwaddr,
391                                 config->static_config.address);
392
393         /* we need the interface up before we can route through it */
394         rc = interface_up(interface);
395         if (rc)
396                 return;
397
398         if (config->static_config.gateway)
399                 rc = process_run_simple(interface, pb_system_apps.ip,
400                                 "route", "add", "default",
401                                 "via", config->static_config.gateway,
402                                 NULL);
403
404         if (rc) {
405                 pb_log("failed to add default route %s on interface %s\n",
406                                 config->static_config.gateway,
407                                 interface->name);
408         }
409
410         if (config->static_config.url) {
411                 pb_log("config URL %s\n", config->static_config.url);
412                 device_handler_process_url(network->handler,
413                                 config->static_config.url,
414                                 mac_bytes_to_string(interface->dev,
415                                                 interface->hwaddr,
416                                                 sizeof(interface->hwaddr)),
417                                 config->static_config.address);
418         }
419
420         return;
421 }
422
423 static void configure_interface(struct network *network,
424                 struct interface *interface, bool up, bool link)
425 {
426         const struct interface_config *config = NULL;
427
428         if (interface->state == IFSTATE_IGNORED)
429                 return;
430
431         /* old interface? check that we're still up and running */
432         if (interface->state == IFSTATE_CONFIGURED) {
433                 if (!up)
434                         interface->state = IFSTATE_NEW;
435                 else if (!link)
436                         interface->state = IFSTATE_UP_WAITING_LINK;
437                 else {
438                         pb_debug("network: skipping configured interface %s\n",
439                                         interface->name);
440                         return;
441                 }
442         }
443
444         /* always up the lookback, no other handling required */
445         if (!strcmp(interface->name, "lo")) {
446                 if (interface->state == IFSTATE_NEW)
447                         interface_up(interface);
448                 interface->state = IFSTATE_CONFIGURED;
449                 return;
450         }
451
452         config = find_config_by_hwaddr(interface->hwaddr);
453         if (config && config->ignore) {
454                 pb_log("network: ignoring interface %s\n", interface->name);
455                 interface->state = IFSTATE_IGNORED;
456                 return;
457         }
458
459         /* if we're in manual config mode, we need an interface configuration */
460         if (network->manual_config && !config) {
461                 interface->state = IFSTATE_IGNORED;
462                 pb_log("network: skipping %s: manual config mode, "
463                                 "but no config for this interface\n",
464                                 interface->name);
465                 return;
466         }
467
468         /* new interface? bring up to the point so we can detect a link */
469         if (interface->state == IFSTATE_NEW) {
470                 if (!up) {
471                         interface_up(interface);
472                         pb_log("network: bringing up interface %s\n",
473                                         interface->name);
474                         return;
475
476                 } else if (!link) {
477                         interface->state = IFSTATE_UP_WAITING_LINK;
478                 }
479         }
480
481         /* no link? wait for a notification */
482         if (interface->state == IFSTATE_UP_WAITING_LINK && !link)
483                 return;
484
485         pb_log("network: configuring interface %s\n", interface->name);
486
487         if (!config || config->method == CONFIG_METHOD_DHCP) {
488                 configure_interface_dhcp(network, interface);
489
490         } else if (config->method == CONFIG_METHOD_STATIC) {
491                 configure_interface_static(network, interface, config);
492         }
493
494         interface->state = IFSTATE_CONFIGURED;
495 }
496
497 static int network_handle_nlmsg(struct network *network, struct nlmsghdr *nlmsg)
498 {
499         bool have_ifaddr, have_ifname;
500         struct interface *interface;
501         struct ifinfomsg *info;
502         struct rtattr *attr;
503         unsigned int mtu;
504         uint8_t ifaddr[6];
505         char ifname[IFNAMSIZ+1];
506         int attrlen, type;
507
508
509         /* we're only interested in NEWLINK messages */
510         type = nlmsg->nlmsg_type;
511         if (!(type == RTM_NEWLINK || type == RTM_DELLINK))
512                 return 0;
513
514         info = NLMSG_DATA(nlmsg);
515
516         have_ifaddr = have_ifname = false;
517         mtu = 1;
518
519         attrlen = nlmsg->nlmsg_len - sizeof(*info);
520
521         /* extract the interface name and hardware address attributes */
522         for_each_rta(info + 1, attr, attrlen) {
523                 void *data = RTA_DATA(attr);
524
525                 switch (attr->rta_type) {
526                 case IFLA_ADDRESS:
527                         memcpy(ifaddr, data, sizeof(ifaddr));
528                         have_ifaddr = true;
529                         break;
530
531                 case IFLA_IFNAME:
532                         strncpy(ifname, data, IFNAMSIZ);
533                         have_ifname = true;
534                         break;
535
536                 case IFLA_MTU:
537                         mtu = *(unsigned int *)data;
538                         break;
539                 }
540         }
541
542         if (!have_ifaddr || !have_ifname)
543                 return -1;
544
545         if (type == RTM_DELLINK || mtu == 0) {
546                 interface = find_interface_by_ifindex(network, info->ifi_index);
547                 if (!interface)
548                         return 0;
549                 pb_log("network: interface %s removed\n", interface->name);
550                 remove_interface(network, interface);
551                 return 0;
552         }
553
554         /* ignore the default tun device in some environments */
555         if (strncmp(ifname, "tun", strlen("tun")) == 0)
556                 return 0;
557
558         interface = find_interface_by_ifindex(network, info->ifi_index);
559         if (!interface) {
560                 interface = talloc_zero(network, struct interface);
561                 interface->ifindex = info->ifi_index;
562                 interface->state = IFSTATE_NEW;
563                 memcpy(interface->hwaddr, ifaddr, sizeof(interface->hwaddr));
564                 strncpy(interface->name, ifname, sizeof(interface->name) - 1);
565                 list_add(&network->interfaces, &interface->list);
566                 create_interface_dev(network, interface);
567         }
568
569         /* A repeated RTM_NEWLINK can represent an interface name change */
570         if (strncmp(interface->name, ifname, IFNAMSIZ)) {
571                 pb_debug("ifname update: %s -> %s\n", interface->name, ifname);
572                 strncpy(interface->name, ifname, sizeof(interface->name) - 1);
573                 talloc_free(interface->dev->device->id);
574                 interface->dev->device->id =
575                         talloc_strdup(interface->dev->device, ifname);
576         }
577
578         /* notify the sysinfo code about changes to this interface */
579         if (strcmp(interface->name, "lo"))
580                 system_info_register_interface(
581                                 sizeof(interface->hwaddr),
582                                 interface->hwaddr, interface->name,
583                                 info->ifi_flags & IFF_LOWER_UP);
584
585         if (!interface->dev)
586                 create_interface_dev(network, interface);
587
588         configure_interface(network, interface,
589                         info->ifi_flags & IFF_UP,
590                         info->ifi_flags & IFF_LOWER_UP);
591
592         return 0;
593 }
594
595 static int network_netlink_process(void *arg)
596 {
597         struct network *network = arg;
598         struct nlmsghdr *nlmsg;
599         struct msghdr msg;
600         struct iovec iov;
601         unsigned int len;
602         int rc, flags;
603
604         memset(&msg, 0, sizeof(msg));
605         msg.msg_iov = &iov;
606         msg.msg_iovlen = 1;
607
608         flags = MSG_PEEK;
609
610 retry:
611         iov.iov_len = network->netlink_buf_size;
612         iov.iov_base = network->netlink_buf;
613
614         rc = recvmsg(network->netlink_sd, &msg, flags);
615
616         if (rc < 0) {
617                 perror("netlink recv header");
618                 return -1;
619         }
620
621         len = rc;
622
623         /* if the netlink message was larger than our buffer, realloc
624          * before reading again */
625         if (len > network->netlink_buf_size || msg.msg_flags & MSG_TRUNC) {
626                 network->netlink_buf_size *= 2;
627                 network->netlink_buf = talloc_realloc(network,
628                                         network->netlink_buf,
629                                         char *,
630                                         network->netlink_buf_size);
631                 goto retry;
632         }
633
634         /* otherwise, we're good to read the entire message without PEEK */
635         if (flags == MSG_PEEK) {
636                 flags = 0;
637                 goto retry;
638         }
639
640         for_each_nlmsg(network->netlink_buf, nlmsg, len)
641                 network_handle_nlmsg(network, nlmsg);
642
643         return 0;
644 }
645
646 static void network_init_dns(struct network *network)
647 {
648         const struct config *config;
649         unsigned int i;
650         int rc, len;
651         bool modified;
652         char *buf;
653
654         if (network->dry_run)
655                 return;
656
657         config = config_get();
658         if (!config || !config->network.n_dns_servers)
659                 return;
660
661         rc = read_file(network, "/etc/resolv.conf", &buf, &len);
662
663         if (rc) {
664                 buf = talloc_strdup(network, "");
665                 len = 0;
666         }
667
668         modified = false;
669
670         for (i = 0; i < config->network.n_dns_servers; i++) {
671                 int dns_conf_len;
672                 char *dns_conf;
673
674                 dns_conf = talloc_asprintf(network, "nameserver %s\n",
675                                 config->network.dns_servers[i]);
676
677                 if (strstr(buf, dns_conf)) {
678                         talloc_free(dns_conf);
679                         continue;
680                 }
681
682                 dns_conf_len = strlen(dns_conf);
683                 buf = talloc_realloc(network, buf, char, len + dns_conf_len + 1);
684                 memcpy(buf + len, dns_conf, dns_conf_len);
685                 len += dns_conf_len;
686                 buf[len] = '\0';
687                 modified = true;
688
689                 talloc_free(dns_conf);
690         }
691
692         if (modified) {
693                 rc = replace_file("/etc/resolv.conf", buf, len);
694                 if (rc)
695                         pb_log("error replacing resolv.conf: %s\n",
696                                         strerror(errno));
697         }
698
699         talloc_free(buf);
700 }
701
702 struct network *network_init(struct device_handler *handler,
703                 struct waitset *waitset, bool dry_run)
704 {
705         struct network *network;
706         int rc;
707
708         network = talloc(handler, struct network);
709         list_init(&network->interfaces);
710         network->handler = handler;
711         network->dry_run = dry_run;
712         network->manual_config = config_get()->network.n_interfaces != 0;
713
714         network_init_dns(network);
715
716         rc = network_init_netlink(network);
717         if (rc)
718                 goto err;
719
720         network->waiter = waiter_register_io(waitset, network->netlink_sd,
721                         WAIT_IN, network_netlink_process, network);
722
723         if (!network->waiter)
724                 goto err;
725
726         rc = network_send_link_query(network);
727         if (rc)
728                 goto err;
729
730         return network;
731
732 err:
733         network_shutdown(network);
734         return NULL;
735 }
736
737 int network_shutdown(struct network *network)
738 {
739         struct interface *interface;
740
741         if (network->waiter)
742                 waiter_remove(network->waiter);
743
744         list_for_each_entry(&network->interfaces, interface, list) {
745                 if (interface->state == IFSTATE_IGNORED)
746                         continue;
747                 if (!strcmp(interface->name, "lo"))
748                         continue;
749                 interface_down(interface);
750         }
751
752         close(network->netlink_sd);
753         talloc_free(network);
754         return 0;
755 }