discover/discover-server: Restrict clients based on uid
[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         if (interface->udhcpc6_process) {
567                 interface->udhcpc6_process->exit_cb = NULL;
568                 interface->udhcpc6_process->data = NULL;
569                 process_stop_async(interface->udhcpc6_process);
570                 process_release(interface->udhcpc6_process);
571         }
572
573         config = find_config_by_hwaddr(interface->hwaddr);
574
575         if (config && config->ignore)
576                 return;
577
578         if (!config || config->method == CONFIG_METHOD_DHCP) {
579                 /* Restart DHCP. Once we acquire a lease, we'll re-start
580                  * the requery timeout (based on any reboottime DHCP option)
581                  */
582                 configure_interface_dhcp(network, interface);
583
584         } else if (config->method == CONFIG_METHOD_STATIC &&
585                         config->static_config.url) {
586                 /* Redownload statically-provided URL, and manually restart
587                  * requery timeout */
588                 device_handler_process_url(network->handler,
589                                 config->static_config.url,
590                                 mac_bytes_to_string(interface->dev,
591                                                 interface->hwaddr,
592                                                 sizeof(interface->hwaddr)),
593                                 config->static_config.address);
594                 device_handler_start_requery_timeout(network->handler,
595                                 dev, -1);
596         }
597 }
598
599 static int network_handle_nlmsg(struct network *network, struct nlmsghdr *nlmsg)
600 {
601         bool have_ifaddr, have_ifname;
602         struct interface *interface, *tmp;
603         struct ifinfomsg *info;
604         struct rtattr *attr;
605         unsigned int mtu;
606         uint8_t ifaddr[6];
607         char ifname[IFNAMSIZ];
608         int attrlen, type;
609
610
611         /* we're only interested in NEWLINK messages */
612         type = nlmsg->nlmsg_type;
613         if (!(type == RTM_NEWLINK || type == RTM_DELLINK))
614                 return 0;
615
616         info = NLMSG_DATA(nlmsg);
617
618         have_ifaddr = have_ifname = false;
619         mtu = 1;
620
621         attrlen = nlmsg->nlmsg_len - sizeof(*info);
622
623         /* extract the interface name and hardware address attributes */
624         for_each_rta(info + 1, attr, attrlen) {
625                 void *data = RTA_DATA(attr);
626
627                 switch (attr->rta_type) {
628                 case IFLA_ADDRESS:
629                         memcpy(ifaddr, data, sizeof(ifaddr));
630                         have_ifaddr = true;
631                         break;
632
633                 case IFLA_IFNAME:
634                         strncpy(ifname, data, IFNAMSIZ);
635                         ifname[IFNAMSIZ - 1] = '\0';
636                         have_ifname = true;
637                         break;
638
639                 case IFLA_MTU:
640                         mtu = *(unsigned int *)data;
641                         break;
642                 }
643         }
644
645         if (!have_ifaddr || !have_ifname)
646                 return -1;
647
648         if (type == RTM_DELLINK || mtu == 0) {
649                 interface = find_interface_by_ifindex(network, info->ifi_index);
650                 if (!interface)
651                         return 0;
652                 pb_log("network: interface %s removed\n", interface->name);
653                 remove_interface(network, interface);
654                 return 0;
655         }
656
657         /* ignore the default tun device in some environments */
658         if (strncmp(ifname, "tun", strlen("tun")) == 0)
659                 return 0;
660
661         interface = find_interface_by_ifindex(network, info->ifi_index);
662         if (!interface) {
663                 interface = talloc_zero(network, struct interface);
664                 interface->ifindex = info->ifi_index;
665                 interface->state = IFSTATE_NEW;
666                 memcpy(interface->hwaddr, ifaddr, sizeof(interface->hwaddr));
667                 strncpy(interface->name, ifname, sizeof(interface->name));
668
669                 list_for_each_entry(&network->interfaces, tmp, list)
670                         if (memcmp(interface->hwaddr, tmp->hwaddr,
671                                    sizeof(interface->hwaddr)) == 0) {
672                                 pb_log("%s: %s has duplicate MAC address, ignoring\n",
673                                        __func__, interface->name);
674                                 talloc_free(interface);
675                                 return -1;
676                         }
677
678                 list_add(&network->interfaces, &interface->list);
679                 create_interface_dev(network, interface);
680         }
681
682         /* A repeated RTM_NEWLINK can represent an interface name change */
683         if (strncmp(interface->name, ifname, IFNAMSIZ)) {
684                 pb_debug("ifname update: %s -> %s\n", interface->name, ifname);
685                 strncpy(interface->name, ifname, sizeof(interface->name));
686                 talloc_free(interface->dev->device->id);
687                 interface->dev->device->id =
688                         talloc_strdup(interface->dev->device, ifname);
689         }
690
691         /* notify the sysinfo code about changes to this interface */
692         if (strcmp(interface->name, "lo"))
693                 system_info_register_interface(
694                                 sizeof(interface->hwaddr),
695                                 interface->hwaddr, interface->name,
696                                 info->ifi_flags & IFF_LOWER_UP);
697
698         if (!interface->dev)
699                 create_interface_dev(network, interface);
700
701         if (!interface->ready && strncmp(interface->name, "lo", strlen("lo"))) {
702                 pb_log("%s not marked ready yet\n", interface->name);
703                 return 0;
704         }
705
706         configure_interface(network, interface,
707                         info->ifi_flags & IFF_UP,
708                         info->ifi_flags & IFF_LOWER_UP);
709
710         return 0;
711 }
712
713 void network_mark_interface_ready(struct device_handler *handler,
714                 int ifindex, const char *ifname, uint8_t *mac, int hwsize)
715 {
716         struct network *network = device_handler_get_network(handler);
717         struct interface *interface, *tmp = NULL;
718         char *macstr;
719
720         if (!network) {
721                 pb_log("Network not ready - can not mark interface ready\n");
722                 return;
723         }
724
725         if (hwsize != HWADDR_SIZE)
726                 return;
727
728         if (strncmp(ifname, "lo", strlen("lo")) == 0)
729                 return;
730
731         interface = find_interface_by_ifindex(network, ifindex);
732         if (!interface) {
733                 pb_debug("Creating ready interface %d - %s\n",
734                                 ifindex, ifname);
735                 interface = talloc_zero(network, struct interface);
736                 interface->ifindex = ifindex;
737                 interface->state = IFSTATE_NEW;
738                 memcpy(interface->hwaddr, mac, HWADDR_SIZE);
739                 strncpy(interface->name, ifname, sizeof(interface->name) - 1);
740
741                 list_for_each_entry(&network->interfaces, tmp, list)
742                         if (memcmp(interface->hwaddr, tmp->hwaddr,
743                                    sizeof(interface->hwaddr)) == 0) {
744                                 pb_log("%s: %s has duplicate MAC address, ignoring\n",
745                                        __func__, interface->name);
746                                 talloc_free(interface);
747                                 return;
748                         }
749
750                 list_add(&network->interfaces, &interface->list);
751                 create_interface_dev(network, interface);
752         }
753
754         if (interface->ready) {
755                 pb_log("%s already ready\n", interface->name);
756                 return;
757         }
758
759         if (strncmp(interface->name, ifname, strlen(ifname)) != 0) {
760                 pb_debug("ifname update from udev: %s -> %s\n", interface->name, ifname);
761                 strncpy(interface->name, ifname, sizeof(interface->name) - 1);
762                 talloc_free(interface->dev->device->id);
763                 interface->dev->device->id =
764                         talloc_strdup(interface->dev->device, ifname);
765         }
766
767         if (memcmp(interface->hwaddr, mac, HWADDR_SIZE) != 0) {
768                 macstr = mac_bytes_to_string(interface, mac, hwsize);
769                 pb_log("Warning - new MAC for interface %d does not match: %s\n",
770                                 ifindex, macstr);
771                 talloc_free(macstr);
772         }
773
774         pb_log("Interface %s ready\n", ifname);
775         interface->ready = true;
776         configure_interface(network, interface, false, false);
777 }
778
779 static int network_netlink_process(void *arg)
780 {
781         struct network *network = arg;
782         struct nlmsghdr *nlmsg;
783         struct msghdr msg;
784         struct iovec iov;
785         unsigned int len;
786         int rc, flags;
787
788         memset(&msg, 0, sizeof(msg));
789         msg.msg_iov = &iov;
790         msg.msg_iovlen = 1;
791
792         flags = MSG_PEEK;
793
794 retry:
795         iov.iov_len = network->netlink_buf_size;
796         iov.iov_base = network->netlink_buf;
797
798         rc = recvmsg(network->netlink_sd, &msg, flags);
799
800         if (rc < 0) {
801                 perror("netlink recv header");
802                 return -1;
803         }
804
805         len = rc;
806
807         /* if the netlink message was larger than our buffer, realloc
808          * before reading again */
809         if (len > network->netlink_buf_size || msg.msg_flags & MSG_TRUNC) {
810                 network->netlink_buf_size *= 2;
811                 network->netlink_buf = talloc_realloc(network,
812                                         network->netlink_buf,
813                                         char *,
814                                         network->netlink_buf_size);
815                 goto retry;
816         }
817
818         /* otherwise, we're good to read the entire message without PEEK */
819         if (flags == MSG_PEEK) {
820                 flags = 0;
821                 goto retry;
822         }
823
824         for_each_nlmsg(network->netlink_buf, nlmsg, len)
825                 network_handle_nlmsg(network, nlmsg);
826
827         return 0;
828 }
829
830 static void network_init_dns(struct network *network)
831 {
832         const struct config *config;
833         unsigned int i;
834         int rc, len;
835         bool modified;
836         char *buf;
837
838         if (network->dry_run)
839                 return;
840
841         config = config_get();
842         if (!config || !config->network.n_dns_servers)
843                 return;
844
845         rc = read_file(network, "/etc/resolv.conf", &buf, &len);
846
847         if (rc) {
848                 buf = talloc_strdup(network, "");
849                 len = 0;
850         }
851
852         modified = false;
853
854         for (i = 0; i < config->network.n_dns_servers; i++) {
855                 int dns_conf_len;
856                 char *dns_conf;
857
858                 dns_conf = talloc_asprintf(network, "nameserver %s\n",
859                                 config->network.dns_servers[i]);
860
861                 if (strstr(buf, dns_conf)) {
862                         talloc_free(dns_conf);
863                         continue;
864                 }
865
866                 dns_conf_len = strlen(dns_conf);
867                 buf = talloc_realloc(network, buf, char, len + dns_conf_len + 1);
868                 memcpy(buf + len, dns_conf, dns_conf_len);
869                 len += dns_conf_len;
870                 buf[len] = '\0';
871                 modified = true;
872
873                 talloc_free(dns_conf);
874         }
875
876         if (modified) {
877                 rc = replace_file("/etc/resolv.conf", buf, len);
878                 if (rc)
879                         pb_log("error replacing resolv.conf: %s\n",
880                                         strerror(errno));
881         }
882
883         talloc_free(buf);
884 }
885
886 struct network *network_init(struct device_handler *handler,
887                 struct waitset *waitset, bool dry_run)
888 {
889         struct network *network;
890         int rc;
891
892         network = talloc(handler, struct network);
893         list_init(&network->interfaces);
894         network->handler = handler;
895         network->dry_run = dry_run;
896         network->manual_config = config_get()->network.n_interfaces != 0;
897
898         network_init_dns(network);
899
900         rc = network_init_netlink(network);
901         if (rc)
902                 goto err;
903
904         network->waiter = waiter_register_io(waitset, network->netlink_sd,
905                         WAIT_IN, network_netlink_process, network);
906
907         if (!network->waiter)
908                 goto err;
909
910         rc = network_send_link_query(network);
911         if (rc)
912                 goto err;
913
914         return network;
915
916 err:
917         network_shutdown(network);
918         return NULL;
919 }
920
921 int network_shutdown(struct network *network)
922 {
923         struct interface *interface;
924
925         if (network->waiter)
926                 waiter_remove(network->waiter);
927
928         list_for_each_entry(&network->interfaces, interface, list) {
929                 if (interface->state == IFSTATE_IGNORED)
930                         continue;
931                 if (!strcmp(interface->name, "lo"))
932                         continue;
933                 interface_down(interface);
934         }
935
936         close(network->netlink_sd);
937         talloc_free(network);
938         return 0;
939 }