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