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