7 #include <sys/socket.h>
9 #include <linux/netlink.h>
10 #include <linux/rtnetlink.h>
11 #include <i18n/i18n.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>
25 #include "device-handler.h"
29 #define PIDFILE_BASE (LOCAL_STATE_DIR "/petitboot/")
30 #define INITIAL_BUFSIZE 4096
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))
37 #define for_each_rta(buf, rta, attrlen) \
38 for (rta = (struct rtattr *)(buf); RTA_OK(rta, attrlen); \
39 rta = RTA_NEXT(rta, attrlen))
45 uint8_t hwaddr[HWADDR_SIZE];
49 IFSTATE_UP_WAITING_LINK,
54 struct list_item list;
55 struct process *udhcpc_process;
56 struct process *udhcpc6_process;
57 struct discover_device *dev;
62 struct list interfaces;
63 struct device_handler *handler;
64 struct waiter *waiter;
67 unsigned int netlink_buf_size;
72 static char *mac_bytes_to_string(void *ctx, uint8_t *addr, int len)
74 const int l = strlen("xx:");
79 return talloc_strdup(ctx, "");
81 buf = talloc_array(ctx, char, (len * l) + 1);
83 for (i = 0; i < len; i++)
84 sprintf(buf + (l * i), "%02x:", addr[i]);
86 *(buf + (l * len) - 1) = '\0';
91 static const struct interface_config *find_config_by_hwaddr(
94 const struct config *config;
97 config = config_get();
101 for (i = 0; i < config->network.n_interfaces; i++) {
102 struct interface_config *ifconf = config->network.interfaces[i];
104 if (!memcmp(ifconf->hwaddr, hwaddr, HWADDR_SIZE))
111 static struct interface *find_interface_by_ifindex(struct network *network,
114 struct interface *interface;
116 list_for_each_entry(&network->interfaces, interface, list)
117 if (interface->ifindex == ifindex)
123 static struct interface *find_interface_by_name(struct network *network,
126 struct interface *interface;
128 list_for_each_entry(&network->interfaces, interface, list)
129 if (!strcmp(interface->name, name))
135 static struct interface *find_interface_by_uuid(struct network *network,
138 struct interface *interface;
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)) {
154 uint8_t *find_mac_by_name(void *ctx, struct network *network,
157 struct interface *interface;
159 interface = find_interface_by_name(network, name);
163 return talloc_memdup(ctx, &interface->hwaddr,
164 sizeof(uint8_t) * HWADDR_SIZE);
167 static int network_init_netlink(struct network *network)
169 struct sockaddr_nl addr;
172 memset(&addr, 0, sizeof(addr));
173 addr.nl_family = AF_NETLINK;
174 addr.nl_groups = RTMGRP_LINK;
176 network->netlink_sd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
177 if (network->netlink_sd < 0) {
178 perror("socket(AF_NETLINK)");
182 rc = bind(network->netlink_sd, (struct sockaddr *)&addr, sizeof(addr));
184 perror("bind(sockaddr_nl)");
185 close(network->netlink_sd);
189 network->netlink_buf_size = INITIAL_BUFSIZE;
190 network->netlink_buf = talloc_array(network, char,
191 network->netlink_buf_size);
196 static int network_send_link_query(struct network *network)
200 struct nlmsghdr nlmsg;
201 struct rtgenmsg rtmsg;
204 memset(&msg, 0, sizeof(msg));
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;
213 rc = send(network->netlink_sd, &msg, sizeof(msg), MSG_NOSIGNAL);
214 if (rc != sizeof(msg))
220 static void create_interface_dev(struct network *network,
221 struct interface *interface)
223 char *uuid = mac_bytes_to_string(interface, interface->hwaddr,
224 sizeof(interface->hwaddr));
226 interface->dev = discover_device_create(network->handler, uuid,
228 interface->dev->device->type = DEVICE_TYPE_NETWORK;
229 device_handler_add_device(network->handler, interface->dev);
233 static void remove_interface(struct network *network,
234 struct interface *interface)
237 device_handler_remove(network->handler, interface->dev);
238 list_remove(&interface->list);
239 talloc_free(interface);
242 void network_register_device(struct network *network,
243 struct discover_device *dev)
245 struct interface *iface;
248 iface = find_interface_by_uuid(network, dev->uuid);
250 iface = find_interface_by_name(network, dev->label);
255 dev->uuid = mac_bytes_to_string(iface->dev, iface->hwaddr,
256 sizeof(iface->hwaddr));
259 void network_unregister_device(struct network *network,
260 struct discover_device *dev)
262 struct interface *iface;
264 iface = find_interface_by_uuid(network, dev->uuid);
271 static int interface_change(struct interface *interface, bool up)
273 const char *statestr = up ? "up" : "down";
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);
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);
292 rc = process_run_simple(interface, pb_system_apps.ip,
293 "address", "flush", "dev", interface->name,
296 pb_log("failed to flush addresses from interface %s\n",
300 rc = process_run_simple(interface, pb_system_apps.ip,
301 "link", "set", interface->name, statestr, NULL);
303 pb_log("failed to bring interface %s %s\n", interface->name,
310 static int interface_up(struct interface *interface)
312 return interface_change(interface, true);
315 static int interface_down(struct interface *interface)
317 return interface_change(interface, false);
320 static void udhcpc_process_exit(struct process *process)
322 struct interface *interface = process->data;
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;
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;
334 process_release(process);
337 static void configure_interface_dhcp(struct network *network,
338 struct interface *interface)
340 const struct platform *platform;
341 char pidfile[256], idv4[10], idv6[10];
342 struct process *p_v4, *p_v6;
344 const char *argv_ipv4[] = {
345 pb_system_apps.udhcpc,
349 "-O", "pxepathprefix",
352 "-i", interface->name,
353 "-x", idv4, /* [11,12] - dhcp client identifier */
357 const char *argv_ipv6[] = {
358 pb_system_apps.udhcpc6,
361 "-O", "bootfile_url",
362 "-O", "bootfile_param",
364 "-O", "pxepathprefix",
366 "-i", interface->name,
367 "-x", idv6, /* [15,16] - dhcp client identifier */
371 device_handler_status_dev_info(network->handler, interface->dev,
372 _("Configuring with DHCP"));
374 snprintf(pidfile, sizeof(pidfile), "%s/udhcpc-%s.pid",
375 PIDFILE_BASE, interface->name);
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);
384 argv_ipv4[11] = argv_ipv6[15] = NULL;
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;
393 pb_log("Running DHCPv4 client\n");
394 rc = process_run_async(p_v4);
396 process_release(p_v4);
398 interface->udhcpc_process = p_v4;
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;
407 rc = process_run_async(p_v6);
409 process_release(p_v6);
411 interface->udhcpc6_process = p_v6;
416 static void configure_interface_static(struct network *network,
417 struct interface *interface,
418 const struct interface_config *config)
422 device_handler_status_dev_info(network->handler, interface->dev,
423 _("Configuring with static address (ip: %s)"),
424 config->static_config.address);
426 rc = process_run_simple(interface, pb_system_apps.ip,
427 "address", "add", config->static_config.address,
428 "dev", interface->name, NULL);
432 pb_log("failed to add address %s to interface %s\n",
433 config->static_config.address,
438 system_info_set_interface_address(sizeof(interface->hwaddr),
440 config->static_config.address);
442 /* we need the interface up before we can route through it */
443 rc = interface_up(interface);
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,
454 pb_log("failed to add default route %s on interface %s\n",
455 config->static_config.gateway,
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,
465 sizeof(interface->hwaddr)),
466 config->static_config.address);
467 device_handler_start_requery_timeout(network->handler,
474 static void configure_interface(struct network *network,
475 struct interface *interface, bool up, bool link)
477 const struct interface_config *config = NULL;
479 if (interface->state == IFSTATE_IGNORED)
482 /* old interface? check that we're still up and running */
483 if (interface->state == IFSTATE_CONFIGURED) {
485 interface->state = IFSTATE_NEW;
487 interface->state = IFSTATE_UP_WAITING_LINK;
489 pb_debug("network: skipping configured interface %s\n",
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;
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;
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",
519 /* new interface? bring up to the point so we can detect a link */
520 if (interface->state == IFSTATE_NEW) {
522 interface_up(interface);
523 pb_log("network: bringing up interface %s\n",
528 interface->state = IFSTATE_UP_WAITING_LINK;
532 /* no link? wait for a notification */
533 if (interface->state == IFSTATE_UP_WAITING_LINK && !link)
536 pb_log("network: configuring interface %s\n", interface->name);
538 if (!config || config->method == CONFIG_METHOD_DHCP) {
539 configure_interface_dhcp(network, interface);
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();
547 interface->state = IFSTATE_CONFIGURED;
550 void network_requery_device(struct network *network,
551 struct discover_device *dev)
553 const struct interface_config *config;
554 struct interface *interface;
556 interface = find_interface_by_uuid(network, dev->uuid);
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);
567 config = find_config_by_hwaddr(interface->hwaddr);
569 if (config && config->ignore)
572 if (!config || config->method == CONFIG_METHOD_DHCP) {
573 /* Restart DHCP. Once we acquire a lease, we'll re-start
574 * the requery timeout (based on any reboottime DHCP option)
576 configure_interface_dhcp(network, interface);
578 } else if (config->method == CONFIG_METHOD_STATIC &&
579 config->static_config.url) {
580 /* Redownload statically-provided URL, and manually restart
582 device_handler_process_url(network->handler,
583 config->static_config.url,
584 mac_bytes_to_string(interface->dev,
586 sizeof(interface->hwaddr)),
587 config->static_config.address);
588 device_handler_start_requery_timeout(network->handler,
593 static int network_handle_nlmsg(struct network *network, struct nlmsghdr *nlmsg)
595 bool have_ifaddr, have_ifname;
596 struct interface *interface, *tmp;
597 struct ifinfomsg *info;
601 char ifname[IFNAMSIZ];
605 /* we're only interested in NEWLINK messages */
606 type = nlmsg->nlmsg_type;
607 if (!(type == RTM_NEWLINK || type == RTM_DELLINK))
610 info = NLMSG_DATA(nlmsg);
612 have_ifaddr = have_ifname = false;
615 attrlen = nlmsg->nlmsg_len - sizeof(*info);
617 /* extract the interface name and hardware address attributes */
618 for_each_rta(info + 1, attr, attrlen) {
619 void *data = RTA_DATA(attr);
621 switch (attr->rta_type) {
623 memcpy(ifaddr, data, sizeof(ifaddr));
628 strncpy(ifname, data, IFNAMSIZ);
629 ifname[IFNAMSIZ - 1] = '\0';
634 mtu = *(unsigned int *)data;
639 if (!have_ifaddr || !have_ifname)
642 if (type == RTM_DELLINK || mtu == 0) {
643 interface = find_interface_by_ifindex(network, info->ifi_index);
646 pb_log("network: interface %s removed\n", interface->name);
647 remove_interface(network, interface);
651 /* ignore the default tun device in some environments */
652 if (strncmp(ifname, "tun", strlen("tun")) == 0)
655 interface = find_interface_by_ifindex(network, info->ifi_index);
657 interface = talloc_zero(network, struct interface);
658 interface->ifindex = info->ifi_index;
659 interface->state = IFSTATE_NEW;
660 memcpy(interface->hwaddr, ifaddr, sizeof(interface->hwaddr));
661 strncpy(interface->name, ifname, sizeof(interface->name));
663 list_for_each_entry(&network->interfaces, tmp, list)
664 if (memcmp(interface->hwaddr, tmp->hwaddr,
665 sizeof(interface->hwaddr)) == 0) {
666 pb_log("%s: %s has duplicate MAC address, ignoring\n",
667 __func__, interface->name);
668 talloc_free(interface);
672 list_add(&network->interfaces, &interface->list);
673 create_interface_dev(network, interface);
676 /* A repeated RTM_NEWLINK can represent an interface name change */
677 if (strncmp(interface->name, ifname, IFNAMSIZ)) {
678 pb_debug("ifname update: %s -> %s\n", interface->name, ifname);
679 strncpy(interface->name, ifname, sizeof(interface->name));
680 talloc_free(interface->dev->device->id);
681 interface->dev->device->id =
682 talloc_strdup(interface->dev->device, ifname);
685 /* notify the sysinfo code about changes to this interface */
686 if (strcmp(interface->name, "lo"))
687 system_info_register_interface(
688 sizeof(interface->hwaddr),
689 interface->hwaddr, interface->name,
690 info->ifi_flags & IFF_LOWER_UP);
693 create_interface_dev(network, interface);
695 if (!interface->ready && strncmp(interface->name, "lo", strlen("lo"))) {
696 pb_log("%s not marked ready yet\n", interface->name);
700 configure_interface(network, interface,
701 info->ifi_flags & IFF_UP,
702 info->ifi_flags & IFF_LOWER_UP);
707 void network_mark_interface_ready(struct device_handler *handler,
708 int ifindex, const char *ifname, uint8_t *mac, int hwsize)
710 struct network *network = device_handler_get_network(handler);
711 struct interface *interface, *tmp = NULL;
715 pb_log("Network not ready - can not mark interface ready\n");
719 if (hwsize != HWADDR_SIZE)
722 if (strncmp(ifname, "lo", strlen("lo")) == 0)
725 interface = find_interface_by_ifindex(network, ifindex);
727 pb_debug("Creating ready interface %d - %s\n",
729 interface = talloc_zero(network, struct interface);
730 interface->ifindex = ifindex;
731 interface->state = IFSTATE_NEW;
732 memcpy(interface->hwaddr, mac, HWADDR_SIZE);
733 strncpy(interface->name, ifname, sizeof(interface->name) - 1);
735 list_for_each_entry(&network->interfaces, tmp, list)
736 if (memcmp(interface->hwaddr, tmp->hwaddr,
737 sizeof(interface->hwaddr)) == 0) {
738 pb_log("%s: %s has duplicate MAC address, ignoring\n",
739 __func__, interface->name);
740 talloc_free(interface);
744 list_add(&network->interfaces, &interface->list);
745 create_interface_dev(network, interface);
748 if (interface->ready) {
749 pb_log("%s already ready\n", interface->name);
753 if (strncmp(interface->name, ifname, strlen(ifname)) != 0) {
754 pb_debug("ifname update from udev: %s -> %s\n", interface->name, ifname);
755 strncpy(interface->name, ifname, sizeof(interface->name) - 1);
756 talloc_free(interface->dev->device->id);
757 interface->dev->device->id =
758 talloc_strdup(interface->dev->device, ifname);
761 if (memcmp(interface->hwaddr, mac, HWADDR_SIZE) != 0) {
762 macstr = mac_bytes_to_string(interface, mac, hwsize);
763 pb_log("Warning - new MAC for interface %d does not match: %s\n",
768 pb_log("Interface %s ready\n", ifname);
769 interface->ready = true;
770 configure_interface(network, interface, false, false);
773 static int network_netlink_process(void *arg)
775 struct network *network = arg;
776 struct nlmsghdr *nlmsg;
782 memset(&msg, 0, sizeof(msg));
789 iov.iov_len = network->netlink_buf_size;
790 iov.iov_base = network->netlink_buf;
792 rc = recvmsg(network->netlink_sd, &msg, flags);
795 perror("netlink recv header");
801 /* if the netlink message was larger than our buffer, realloc
802 * before reading again */
803 if (len > network->netlink_buf_size || msg.msg_flags & MSG_TRUNC) {
804 network->netlink_buf_size *= 2;
805 network->netlink_buf = talloc_realloc(network,
806 network->netlink_buf,
808 network->netlink_buf_size);
812 /* otherwise, we're good to read the entire message without PEEK */
813 if (flags == MSG_PEEK) {
818 for_each_nlmsg(network->netlink_buf, nlmsg, len)
819 network_handle_nlmsg(network, nlmsg);
824 static void network_init_dns(struct network *network)
826 const struct config *config;
832 if (network->dry_run)
835 config = config_get();
836 if (!config || !config->network.n_dns_servers)
839 rc = read_file(network, "/etc/resolv.conf", &buf, &len);
842 buf = talloc_strdup(network, "");
848 for (i = 0; i < config->network.n_dns_servers; i++) {
852 dns_conf = talloc_asprintf(network, "nameserver %s\n",
853 config->network.dns_servers[i]);
855 if (strstr(buf, dns_conf)) {
856 talloc_free(dns_conf);
860 dns_conf_len = strlen(dns_conf);
861 buf = talloc_realloc(network, buf, char, len + dns_conf_len + 1);
862 memcpy(buf + len, dns_conf, dns_conf_len);
867 talloc_free(dns_conf);
871 rc = replace_file("/etc/resolv.conf", buf, len);
873 pb_log("error replacing resolv.conf: %s\n",
880 struct network *network_init(struct device_handler *handler,
881 struct waitset *waitset, bool dry_run)
883 struct network *network;
886 network = talloc(handler, struct network);
887 list_init(&network->interfaces);
888 network->handler = handler;
889 network->dry_run = dry_run;
890 network->manual_config = config_get()->network.n_interfaces != 0;
892 network_init_dns(network);
894 rc = network_init_netlink(network);
898 network->waiter = waiter_register_io(waitset, network->netlink_sd,
899 WAIT_IN, network_netlink_process, network);
901 if (!network->waiter)
904 rc = network_send_link_query(network);
911 network_shutdown(network);
915 int network_shutdown(struct network *network)
917 struct interface *interface;
920 waiter_remove(network->waiter);
922 list_for_each_entry(&network->interfaces, interface, list) {
923 if (interface->state == IFSTATE_IGNORED)
925 if (!strcmp(interface->name, "lo"))
927 interface_down(interface);
930 close(network->netlink_sd);
931 talloc_free(network);