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