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);
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);
573 config = find_config_by_hwaddr(interface->hwaddr);
575 if (config && config->ignore)
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)
582 configure_interface_dhcp(network, interface);
584 } else if (config->method == CONFIG_METHOD_STATIC &&
585 config->static_config.url) {
586 /* Redownload statically-provided URL, and manually restart
588 device_handler_process_url(network->handler,
589 config->static_config.url,
590 mac_bytes_to_string(interface->dev,
592 sizeof(interface->hwaddr)),
593 config->static_config.address);
594 device_handler_start_requery_timeout(network->handler,
599 static int network_handle_nlmsg(struct network *network, struct nlmsghdr *nlmsg)
601 bool have_ifaddr, have_ifname;
602 struct interface *interface, *tmp;
603 struct ifinfomsg *info;
607 char ifname[IFNAMSIZ];
611 /* we're only interested in NEWLINK messages */
612 type = nlmsg->nlmsg_type;
613 if (!(type == RTM_NEWLINK || type == RTM_DELLINK))
616 info = NLMSG_DATA(nlmsg);
618 have_ifaddr = have_ifname = false;
621 attrlen = nlmsg->nlmsg_len - sizeof(*info);
623 /* extract the interface name and hardware address attributes */
624 for_each_rta(info + 1, attr, attrlen) {
625 void *data = RTA_DATA(attr);
627 switch (attr->rta_type) {
629 memcpy(ifaddr, data, sizeof(ifaddr));
634 strncpy(ifname, data, IFNAMSIZ);
635 ifname[IFNAMSIZ - 1] = '\0';
640 mtu = *(unsigned int *)data;
645 if (!have_ifaddr || !have_ifname)
648 if (type == RTM_DELLINK || mtu == 0) {
649 interface = find_interface_by_ifindex(network, info->ifi_index);
652 pb_log("network: interface %s removed\n", interface->name);
653 remove_interface(network, interface);
657 /* ignore the default tun device in some environments */
658 if (strncmp(ifname, "tun", strlen("tun")) == 0)
661 interface = find_interface_by_ifindex(network, info->ifi_index);
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));
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);
678 list_add(&network->interfaces, &interface->list);
679 create_interface_dev(network, interface);
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);
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);
699 create_interface_dev(network, interface);
701 if (!interface->ready && strncmp(interface->name, "lo", strlen("lo"))) {
702 pb_log("%s not marked ready yet\n", interface->name);
706 configure_interface(network, interface,
707 info->ifi_flags & IFF_UP,
708 info->ifi_flags & IFF_LOWER_UP);
713 void network_mark_interface_ready(struct device_handler *handler,
714 int ifindex, const char *ifname, uint8_t *mac, int hwsize)
716 struct network *network = device_handler_get_network(handler);
717 struct interface *interface, *tmp = NULL;
721 pb_log("Network not ready - can not mark interface ready\n");
725 if (hwsize != HWADDR_SIZE)
728 if (strncmp(ifname, "lo", strlen("lo")) == 0)
731 interface = find_interface_by_ifindex(network, ifindex);
733 pb_debug("Creating ready interface %d - %s\n",
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);
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);
750 list_add(&network->interfaces, &interface->list);
751 create_interface_dev(network, interface);
754 if (interface->ready) {
755 pb_log("%s already ready\n", interface->name);
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);
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",
774 pb_log("Interface %s ready\n", ifname);
775 interface->ready = true;
776 configure_interface(network, interface, false, false);
779 static int network_netlink_process(void *arg)
781 struct network *network = arg;
782 struct nlmsghdr *nlmsg;
788 memset(&msg, 0, sizeof(msg));
795 iov.iov_len = network->netlink_buf_size;
796 iov.iov_base = network->netlink_buf;
798 rc = recvmsg(network->netlink_sd, &msg, flags);
801 perror("netlink recv header");
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,
814 network->netlink_buf_size);
818 /* otherwise, we're good to read the entire message without PEEK */
819 if (flags == MSG_PEEK) {
824 for_each_nlmsg(network->netlink_buf, nlmsg, len)
825 network_handle_nlmsg(network, nlmsg);
830 static void network_init_dns(struct network *network)
832 const struct config *config;
838 if (network->dry_run)
841 config = config_get();
842 if (!config || !config->network.n_dns_servers)
845 rc = read_file(network, "/etc/resolv.conf", &buf, &len);
848 buf = talloc_strdup(network, "");
854 for (i = 0; i < config->network.n_dns_servers; i++) {
858 dns_conf = talloc_asprintf(network, "nameserver %s\n",
859 config->network.dns_servers[i]);
861 if (strstr(buf, dns_conf)) {
862 talloc_free(dns_conf);
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);
873 talloc_free(dns_conf);
877 rc = replace_file("/etc/resolv.conf", buf, len);
879 pb_log("error replacing resolv.conf: %s\n",
886 struct network *network_init(struct device_handler *handler,
887 struct waitset *waitset, bool dry_run)
889 struct network *network;
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;
898 network_init_dns(network);
900 rc = network_init_netlink(network);
904 network->waiter = waiter_register_io(waitset, network->netlink_sd,
905 WAIT_IN, network_netlink_process, network);
907 if (!network->waiter)
910 rc = network_send_link_query(network);
917 network_shutdown(network);
921 int network_shutdown(struct network *network)
923 struct interface *interface;
926 waiter_remove(network->waiter);
928 list_for_each_entry(&network->interfaces, interface, list) {
929 if (interface->state == IFSTATE_IGNORED)
931 if (!strcmp(interface->name, "lo"))
933 interface_down(interface);
936 close(network->netlink_sd);
937 talloc_free(network);