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