]> git.ozlabs.org Git - petitboot/blob - discover/network.c
69223b149adacd0c133beb06de88d06300e324a4
[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 add_interface(struct network *network,
218                 struct interface *interface)
219 {
220         char *uuid = mac_bytes_to_string(interface, interface->hwaddr,
221                                                 sizeof(interface->hwaddr));
222
223         list_add(&network->interfaces, &interface->list);
224         interface->dev = discover_device_create(network->handler, uuid,
225                                                 interface->name);
226         interface->dev->device->type = DEVICE_TYPE_NETWORK;
227         device_handler_add_device(network->handler, interface->dev);
228         talloc_free(uuid);
229 }
230
231 static void remove_interface(struct network *network,
232                 struct interface *interface)
233 {
234         if (interface->dev)
235                 device_handler_remove(network->handler, interface->dev);
236         list_remove(&interface->list);
237         talloc_free(interface);
238 }
239
240 void network_register_device(struct network *network,
241                 struct discover_device *dev)
242 {
243         struct interface *iface;
244
245         iface = find_interface_by_uuid(network, dev->uuid);
246         if (!iface)
247                 return;
248
249         iface->dev = dev;
250         dev->uuid = mac_bytes_to_string(iface->dev, iface->hwaddr,
251                         sizeof(iface->hwaddr));
252 }
253
254 void network_unregister_device(struct network *network,
255                 struct discover_device *dev)
256 {
257         struct interface *iface;
258
259         iface = find_interface_by_uuid(network, dev->uuid);
260         if (!iface)
261                 return;
262
263         iface->dev = NULL;
264 }
265
266 static int interface_change(struct interface *interface, bool up)
267 {
268         const char *statestr = up ? "up" : "down";
269         int rc;
270
271         if (!up && interface->udhcpc_process) {
272                 /* we don't care about the callback from here */
273                 interface->udhcpc_process->exit_cb = NULL;
274                 interface->udhcpc_process->data = NULL;
275                 process_stop_async(interface->udhcpc_process);
276                 process_release(interface->udhcpc_process);
277         }
278
279         if (!up) {
280                 rc = process_run_simple(interface, pb_system_apps.ip,
281                                 "address", "flush", "dev", interface->name,
282                                 NULL);
283                 if (rc)
284                         pb_log("failed to flush addresses from interface %s\n",
285                                 interface->name);
286         }
287
288         rc = process_run_simple(interface, pb_system_apps.ip,
289                         "link", "set", interface->name, statestr, NULL);
290         if (rc) {
291                 pb_log("failed to bring interface %s %s\n", interface->name,
292                                 statestr);
293                 return -1;
294         }
295         return 0;
296 }
297
298 static int interface_up(struct interface *interface)
299 {
300         return interface_change(interface, true);
301 }
302
303 static int interface_down(struct interface *interface)
304 {
305         return interface_change(interface, false);
306 }
307
308 static void udhcpc_process_exit(struct process *process)
309 {
310         struct interface *interface = process->data;
311         pb_debug("udhcp client [pid %d] for interface %s exited, rc %d\n",
312                         process->pid, interface->name, process->exit_status);
313         interface->udhcpc_process = NULL;
314         process_release(process);
315 }
316
317 static void configure_interface_dhcp(struct network *network,
318                 struct interface *interface)
319 {
320         const struct platform *platform;
321         char pidfile[256], id[10];
322         struct process *process;
323         int rc;
324         const char *argv[] = {
325                 pb_system_apps.udhcpc,
326                 "-R",
327                 "-f",
328                 "-O", "pxeconffile",
329                 "-O", "pxepathprefix",
330                 "-p", pidfile,
331                 "-i", interface->name,
332                 "-x", id, /* [11,12] - dhcp client identifier */
333                 NULL,
334         };
335
336         device_handler_status_dev_info(network->handler, interface->dev,
337                         _("Configuring with DHCP"));
338
339         snprintf(pidfile, sizeof(pidfile), "%s/udhcpc-%s.pid",
340                         PIDFILE_BASE, interface->name);
341
342         platform = platform_get();
343         if (platform && platform->dhcp_arch_id != 0xffff)
344                 snprintf(id, sizeof(id), "0x5d:%04x", platform->dhcp_arch_id);
345         else
346                 argv[11] = NULL;
347
348         process = process_create(interface);
349
350         process->path = pb_system_apps.udhcpc;
351         process->argv = argv;
352         process->exit_cb = udhcpc_process_exit;
353         process->data = interface;
354
355         rc = process_run_async(process);
356
357         if (rc)
358                 process_release(process);
359         else
360                 interface->udhcpc_process = process;
361
362         return;
363 }
364
365 static void configure_interface_static(struct network *network,
366                 struct interface *interface,
367                 const struct interface_config *config)
368 {
369         int rc;
370
371         device_handler_status_dev_info(network->handler, interface->dev,
372                         _("Configuring with static address (ip: %s)"),
373                         config->static_config.address);
374
375         rc = process_run_simple(interface, pb_system_apps.ip,
376                         "address", "add", config->static_config.address,
377                         "dev", interface->name, NULL);
378
379
380         if (rc) {
381                 pb_log("failed to add address %s to interface %s\n",
382                                 config->static_config.address,
383                                 interface->name);
384                 return;
385         }
386
387         system_info_set_interface_address(sizeof(interface->hwaddr),
388                                 interface->hwaddr,
389                                 config->static_config.address);
390
391         /* we need the interface up before we can route through it */
392         rc = interface_up(interface);
393         if (rc)
394                 return;
395
396         if (config->static_config.gateway)
397                 rc = process_run_simple(interface, pb_system_apps.ip,
398                                 "route", "add", "default",
399                                 "via", config->static_config.gateway,
400                                 NULL);
401
402         if (rc) {
403                 pb_log("failed to add default route %s on interface %s\n",
404                                 config->static_config.gateway,
405                                 interface->name);
406         }
407
408         if (config->static_config.url) {
409                 pb_log("config URL %s\n", config->static_config.url);
410                 device_handler_process_url(network->handler,
411                                 config->static_config.url,
412                                 mac_bytes_to_string(interface->dev,
413                                                 interface->hwaddr,
414                                                 sizeof(interface->hwaddr)),
415                                 config->static_config.address);
416         }
417
418         return;
419 }
420
421 static void configure_interface(struct network *network,
422                 struct interface *interface, bool up, bool link)
423 {
424         const struct interface_config *config = NULL;
425
426         if (interface->state == IFSTATE_IGNORED)
427                 return;
428
429         /* old interface? check that we're still up and running */
430         if (interface->state == IFSTATE_CONFIGURED) {
431                 if (!up)
432                         interface->state = IFSTATE_NEW;
433                 else if (!link)
434                         interface->state = IFSTATE_UP_WAITING_LINK;
435                 else {
436                         pb_debug("network: skipping configured interface %s\n",
437                                         interface->name);
438                         return;
439                 }
440         }
441
442         /* always up the lookback, no other handling required */
443         if (!strcmp(interface->name, "lo")) {
444                 if (interface->state == IFSTATE_NEW)
445                         interface_up(interface);
446                 interface->state = IFSTATE_CONFIGURED;
447                 return;
448         }
449
450         config = find_config_by_hwaddr(interface->hwaddr);
451         if (config && config->ignore) {
452                 pb_log("network: ignoring interface %s\n", interface->name);
453                 interface->state = IFSTATE_IGNORED;
454                 return;
455         }
456
457         /* if we're in manual config mode, we need an interface configuration */
458         if (network->manual_config && !config) {
459                 interface->state = IFSTATE_IGNORED;
460                 pb_log("network: skipping %s: manual config mode, "
461                                 "but no config for this interface\n",
462                                 interface->name);
463                 return;
464         }
465
466         /* new interface? bring up to the point so we can detect a link */
467         if (interface->state == IFSTATE_NEW) {
468                 if (!up) {
469                         interface_up(interface);
470                         pb_log("network: bringing up interface %s\n",
471                                         interface->name);
472                         return;
473
474                 } else if (!link) {
475                         interface->state = IFSTATE_UP_WAITING_LINK;
476                 }
477         }
478
479         /* no link? wait for a notification */
480         if (interface->state == IFSTATE_UP_WAITING_LINK && !link)
481                 return;
482
483         pb_log("network: configuring interface %s\n", interface->name);
484
485         if (!config || config->method == CONFIG_METHOD_DHCP) {
486                 configure_interface_dhcp(network, interface);
487
488         } else if (config->method == CONFIG_METHOD_STATIC) {
489                 configure_interface_static(network, interface, config);
490         }
491
492         interface->state = IFSTATE_CONFIGURED;
493 }
494
495 static int network_handle_nlmsg(struct network *network, struct nlmsghdr *nlmsg)
496 {
497         bool have_ifaddr, have_ifname;
498         struct interface *interface;
499         struct ifinfomsg *info;
500         struct rtattr *attr;
501         unsigned int mtu;
502         uint8_t ifaddr[6];
503         char ifname[IFNAMSIZ+1];
504         int attrlen, type;
505
506
507         /* we're only interested in NEWLINK messages */
508         type = nlmsg->nlmsg_type;
509         if (!(type == RTM_NEWLINK || type == RTM_DELLINK))
510                 return 0;
511
512         info = NLMSG_DATA(nlmsg);
513
514         have_ifaddr = have_ifname = false;
515         mtu = 1;
516
517         attrlen = nlmsg->nlmsg_len - sizeof(*info);
518
519         /* extract the interface name and hardware address attributes */
520         for_each_rta(info + 1, attr, attrlen) {
521                 void *data = RTA_DATA(attr);
522
523                 switch (attr->rta_type) {
524                 case IFLA_ADDRESS:
525                         memcpy(ifaddr, data, sizeof(ifaddr));
526                         have_ifaddr = true;
527                         break;
528
529                 case IFLA_IFNAME:
530                         strncpy(ifname, data, IFNAMSIZ);
531                         have_ifname = true;
532                         break;
533
534                 case IFLA_MTU:
535                         mtu = *(unsigned int *)data;
536                         break;
537                 }
538         }
539
540         if (!have_ifaddr || !have_ifname)
541                 return -1;
542
543         if (type == RTM_DELLINK || mtu == 0) {
544                 interface = find_interface_by_ifindex(network, info->ifi_index);
545                 if (!interface)
546                         return 0;
547                 pb_log("network: interface %s removed\n", interface->name);
548                 remove_interface(network, interface);
549                 return 0;
550         }
551
552         /* ignore the default tun device in some environments */
553         if (strncmp(ifname, "tun", strlen("tun")) == 0)
554                 return 0;
555
556         interface = find_interface_by_ifindex(network, info->ifi_index);
557         if (!interface) {
558                 interface = talloc_zero(network, struct interface);
559                 interface->ifindex = info->ifi_index;
560                 interface->state = IFSTATE_NEW;
561                 memcpy(interface->hwaddr, ifaddr, sizeof(interface->hwaddr));
562                 strncpy(interface->name, ifname, sizeof(interface->name) - 1);
563                 add_interface(network, interface);
564         }
565
566         /* A repeated RTM_NEWLINK can represent an interface name change */
567         if (strncmp(interface->name, ifname, IFNAMSIZ)) {
568                 pb_debug("ifname update: %s -> %s\n", interface->name, ifname);
569                 strncpy(interface->name, ifname, sizeof(interface->name) - 1);
570                 talloc_free(interface->dev->device->id);
571                 interface->dev->device->id =
572                         talloc_strdup(interface->dev->device, ifname);
573         }
574
575         /* notify the sysinfo code about changes to this interface */
576         if (strcmp(interface->name, "lo"))
577                 system_info_register_interface(
578                                 sizeof(interface->hwaddr),
579                                 interface->hwaddr, interface->name,
580                                 info->ifi_flags & IFF_LOWER_UP);
581
582         configure_interface(network, interface,
583                         info->ifi_flags & IFF_UP,
584                         info->ifi_flags & IFF_LOWER_UP);
585
586         return 0;
587 }
588
589 static int network_netlink_process(void *arg)
590 {
591         struct network *network = arg;
592         struct nlmsghdr *nlmsg;
593         struct msghdr msg;
594         struct iovec iov;
595         unsigned int len;
596         int rc, flags;
597
598         memset(&msg, 0, sizeof(msg));
599         msg.msg_iov = &iov;
600         msg.msg_iovlen = 1;
601
602         flags = MSG_PEEK;
603
604 retry:
605         iov.iov_len = network->netlink_buf_size;
606         iov.iov_base = network->netlink_buf;
607
608         rc = recvmsg(network->netlink_sd, &msg, flags);
609
610         if (rc < 0) {
611                 perror("netlink recv header");
612                 return -1;
613         }
614
615         len = rc;
616
617         /* if the netlink message was larger than our buffer, realloc
618          * before reading again */
619         if (len > network->netlink_buf_size || msg.msg_flags & MSG_TRUNC) {
620                 network->netlink_buf_size *= 2;
621                 network->netlink_buf = talloc_realloc(network,
622                                         network->netlink_buf,
623                                         char *,
624                                         network->netlink_buf_size);
625                 goto retry;
626         }
627
628         /* otherwise, we're good to read the entire message without PEEK */
629         if (flags == MSG_PEEK) {
630                 flags = 0;
631                 goto retry;
632         }
633
634         for_each_nlmsg(network->netlink_buf, nlmsg, len)
635                 network_handle_nlmsg(network, nlmsg);
636
637         return 0;
638 }
639
640 static void network_init_dns(struct network *network)
641 {
642         const struct config *config;
643         unsigned int i;
644         int rc, len;
645         bool modified;
646         char *buf;
647
648         if (network->dry_run)
649                 return;
650
651         config = config_get();
652         if (!config || !config->network.n_dns_servers)
653                 return;
654
655         rc = read_file(network, "/etc/resolv.conf", &buf, &len);
656
657         if (rc) {
658                 buf = talloc_strdup(network, "");
659                 len = 0;
660         }
661
662         modified = false;
663
664         for (i = 0; i < config->network.n_dns_servers; i++) {
665                 int dns_conf_len;
666                 char *dns_conf;
667
668                 dns_conf = talloc_asprintf(network, "nameserver %s\n",
669                                 config->network.dns_servers[i]);
670
671                 if (strstr(buf, dns_conf)) {
672                         talloc_free(dns_conf);
673                         continue;
674                 }
675
676                 dns_conf_len = strlen(dns_conf);
677                 buf = talloc_realloc(network, buf, char, len + dns_conf_len + 1);
678                 memcpy(buf + len, dns_conf, dns_conf_len);
679                 len += dns_conf_len;
680                 buf[len] = '\0';
681                 modified = true;
682
683                 talloc_free(dns_conf);
684         }
685
686         if (modified) {
687                 rc = replace_file("/etc/resolv.conf", buf, len);
688                 if (rc)
689                         pb_log("error replacing resolv.conf: %s\n",
690                                         strerror(errno));
691         }
692
693         talloc_free(buf);
694 }
695
696 struct network *network_init(struct device_handler *handler,
697                 struct waitset *waitset, bool dry_run)
698 {
699         struct network *network;
700         int rc;
701
702         network = talloc(handler, struct network);
703         list_init(&network->interfaces);
704         network->handler = handler;
705         network->dry_run = dry_run;
706         network->manual_config = config_get()->network.n_interfaces != 0;
707
708         network_init_dns(network);
709
710         rc = network_init_netlink(network);
711         if (rc)
712                 goto err;
713
714         network->waiter = waiter_register_io(waitset, network->netlink_sd,
715                         WAIT_IN, network_netlink_process, network);
716
717         if (!network->waiter)
718                 goto err;
719
720         rc = network_send_link_query(network);
721         if (rc)
722                 goto err;
723
724         return network;
725
726 err:
727         network_shutdown(network);
728         return NULL;
729 }
730
731 int network_shutdown(struct network *network)
732 {
733         struct interface *interface;
734
735         if (network->waiter)
736                 waiter_remove(network->waiter);
737
738         list_for_each_entry(&network->interfaces, interface, list) {
739                 if (interface->state == IFSTATE_IGNORED)
740                         continue;
741                 if (!strcmp(interface->name, "lo"))
742                         continue;
743                 interface_down(interface);
744         }
745
746         close(network->netlink_sd);
747         talloc_free(network);
748         return 0;
749 }