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 discover_device *dev;
61 struct list interfaces;
62 struct device_handler *handler;
63 struct waiter *waiter;
66 unsigned int netlink_buf_size;
71 static char *mac_bytes_to_string(void *ctx, uint8_t *addr, int len)
73 const int l = strlen("xx:");
78 return talloc_strdup(ctx, "");
80 buf = talloc_array(ctx, char, (len * l) + 1);
82 for (i = 0; i < len; i++)
83 sprintf(buf + (l * i), "%02x:", addr[i]);
85 *(buf + (l * len) - 1) = '\0';
90 static const struct interface_config *find_config_by_hwaddr(
93 const struct config *config;
96 config = config_get();
100 for (i = 0; i < config->network.n_interfaces; i++) {
101 struct interface_config *ifconf = config->network.interfaces[i];
103 if (!memcmp(ifconf->hwaddr, hwaddr, HWADDR_SIZE))
110 static struct interface *find_interface_by_ifindex(struct network *network,
113 struct interface *interface;
115 list_for_each_entry(&network->interfaces, interface, list)
116 if (interface->ifindex == ifindex)
122 static struct interface *find_interface_by_name(struct network *network,
125 struct interface *interface;
127 list_for_each_entry(&network->interfaces, interface, list)
128 if (!strcmp(interface->name, name))
134 static struct interface *find_interface_by_uuid(struct network *network,
137 struct interface *interface;
140 list_for_each_entry(&network->interfaces, interface, list) {
141 mac = mac_bytes_to_string(interface, interface->hwaddr,
142 sizeof(interface->hwaddr));
143 if (!strcmp(mac, uuid)) {
153 uint8_t *find_mac_by_name(void *ctx, struct network *network,
156 struct interface *interface;
158 interface = find_interface_by_name(network, name);
162 return talloc_memdup(ctx, &interface->hwaddr,
163 sizeof(uint8_t) * HWADDR_SIZE);
166 static int network_init_netlink(struct network *network)
168 struct sockaddr_nl addr;
171 memset(&addr, 0, sizeof(addr));
172 addr.nl_family = AF_NETLINK;
173 addr.nl_groups = RTMGRP_LINK;
175 network->netlink_sd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
176 if (network->netlink_sd < 0) {
177 perror("socket(AF_NETLINK)");
181 rc = bind(network->netlink_sd, (struct sockaddr *)&addr, sizeof(addr));
183 perror("bind(sockaddr_nl)");
184 close(network->netlink_sd);
188 network->netlink_buf_size = INITIAL_BUFSIZE;
189 network->netlink_buf = talloc_array(network, char,
190 network->netlink_buf_size);
195 static int network_send_link_query(struct network *network)
199 struct nlmsghdr nlmsg;
200 struct rtgenmsg rtmsg;
203 memset(&msg, 0, sizeof(msg));
205 msg.nlmsg.nlmsg_len = sizeof(msg);
206 msg.nlmsg.nlmsg_type = RTM_GETLINK;
207 msg.nlmsg.nlmsg_flags = NLM_F_REQUEST | NLM_F_ROOT;
208 msg.nlmsg.nlmsg_seq = 0;
209 msg.nlmsg.nlmsg_pid = 0;
210 msg.rtmsg.rtgen_family = AF_UNSPEC;
212 rc = send(network->netlink_sd, &msg, sizeof(msg), MSG_NOSIGNAL);
213 if (rc != sizeof(msg))
219 static void create_interface_dev(struct network *network,
220 struct interface *interface)
222 char *uuid = mac_bytes_to_string(interface, interface->hwaddr,
223 sizeof(interface->hwaddr));
225 interface->dev = discover_device_create(network->handler, uuid,
227 interface->dev->device->type = DEVICE_TYPE_NETWORK;
228 device_handler_add_device(network->handler, interface->dev);
232 static void remove_interface(struct network *network,
233 struct interface *interface)
236 device_handler_remove(network->handler, interface->dev);
237 list_remove(&interface->list);
238 talloc_free(interface);
241 void network_register_device(struct network *network,
242 struct discover_device *dev)
244 struct interface *iface;
247 iface = find_interface_by_uuid(network, dev->uuid);
249 iface = find_interface_by_name(network, dev->label);
254 dev->uuid = mac_bytes_to_string(iface->dev, iface->hwaddr,
255 sizeof(iface->hwaddr));
258 void network_unregister_device(struct network *network,
259 struct discover_device *dev)
261 struct interface *iface;
263 iface = find_interface_by_uuid(network, dev->uuid);
270 static int interface_change(struct interface *interface, bool up)
272 const char *statestr = up ? "up" : "down";
275 if (!up && interface->udhcpc_process) {
276 /* we don't care about the callback from here */
277 interface->udhcpc_process->exit_cb = NULL;
278 interface->udhcpc_process->data = NULL;
279 process_stop_async(interface->udhcpc_process);
280 process_release(interface->udhcpc_process);
284 rc = process_run_simple(interface, pb_system_apps.ip,
285 "address", "flush", "dev", interface->name,
288 pb_log("failed to flush addresses from interface %s\n",
292 rc = process_run_simple(interface, pb_system_apps.ip,
293 "link", "set", interface->name, statestr, NULL);
295 pb_log("failed to bring interface %s %s\n", interface->name,
302 static int interface_up(struct interface *interface)
304 return interface_change(interface, true);
307 static int interface_down(struct interface *interface)
309 return interface_change(interface, false);
312 static void udhcpc_process_exit(struct process *process)
314 struct interface *interface = process->data;
315 pb_debug("udhcp client [pid %d] for interface %s exited, rc %d\n",
316 process->pid, interface->name, process->exit_status);
317 interface->udhcpc_process = NULL;
318 process_release(process);
321 static void configure_interface_dhcp(struct network *network,
322 struct interface *interface)
324 const struct platform *platform;
325 char pidfile[256], id[10];
326 struct process *process;
328 const char *argv[] = {
329 pb_system_apps.udhcpc,
333 "-O", "pxepathprefix",
336 "-i", interface->name,
337 "-x", id, /* [11,12] - dhcp client identifier */
341 device_handler_status_dev_info(network->handler, interface->dev,
342 _("Configuring with DHCP"));
344 snprintf(pidfile, sizeof(pidfile), "%s/udhcpc-%s.pid",
345 PIDFILE_BASE, interface->name);
347 platform = platform_get();
348 if (platform && platform->dhcp_arch_id != 0xffff)
349 snprintf(id, sizeof(id), "0x5d:%04x", platform->dhcp_arch_id);
353 process = process_create(interface);
355 process->path = pb_system_apps.udhcpc;
356 process->argv = argv;
357 process->exit_cb = udhcpc_process_exit;
358 process->data = interface;
360 rc = process_run_async(process);
363 process_release(process);
365 interface->udhcpc_process = process;
370 static void configure_interface_static(struct network *network,
371 struct interface *interface,
372 const struct interface_config *config)
376 device_handler_status_dev_info(network->handler, interface->dev,
377 _("Configuring with static address (ip: %s)"),
378 config->static_config.address);
380 rc = process_run_simple(interface, pb_system_apps.ip,
381 "address", "add", config->static_config.address,
382 "dev", interface->name, NULL);
386 pb_log("failed to add address %s to interface %s\n",
387 config->static_config.address,
392 system_info_set_interface_address(sizeof(interface->hwaddr),
394 config->static_config.address);
396 /* we need the interface up before we can route through it */
397 rc = interface_up(interface);
401 if (config->static_config.gateway)
402 rc = process_run_simple(interface, pb_system_apps.ip,
403 "route", "add", "default",
404 "via", config->static_config.gateway,
408 pb_log("failed to add default route %s on interface %s\n",
409 config->static_config.gateway,
413 if (config->static_config.url) {
414 pb_log("config URL %s\n", config->static_config.url);
415 device_handler_process_url(network->handler,
416 config->static_config.url,
417 mac_bytes_to_string(interface->dev,
419 sizeof(interface->hwaddr)),
420 config->static_config.address);
421 device_handler_start_requery_timeout(network->handler,
428 static void configure_interface(struct network *network,
429 struct interface *interface, bool up, bool link)
431 const struct interface_config *config = NULL;
433 if (interface->state == IFSTATE_IGNORED)
436 /* old interface? check that we're still up and running */
437 if (interface->state == IFSTATE_CONFIGURED) {
439 interface->state = IFSTATE_NEW;
441 interface->state = IFSTATE_UP_WAITING_LINK;
443 pb_debug("network: skipping configured interface %s\n",
449 /* always up the lookback, no other handling required */
450 if (!strcmp(interface->name, "lo")) {
451 if (interface->state == IFSTATE_NEW)
452 interface_up(interface);
453 interface->state = IFSTATE_CONFIGURED;
457 config = find_config_by_hwaddr(interface->hwaddr);
458 if (config && config->ignore) {
459 pb_log("network: ignoring interface %s\n", interface->name);
460 interface->state = IFSTATE_IGNORED;
464 /* if we're in manual config mode, we need an interface configuration */
465 if (network->manual_config && !config) {
466 interface->state = IFSTATE_IGNORED;
467 pb_log("network: skipping %s: manual config mode, "
468 "but no config for this interface\n",
473 /* new interface? bring up to the point so we can detect a link */
474 if (interface->state == IFSTATE_NEW) {
476 interface_up(interface);
477 pb_log("network: bringing up interface %s\n",
482 interface->state = IFSTATE_UP_WAITING_LINK;
486 /* no link? wait for a notification */
487 if (interface->state == IFSTATE_UP_WAITING_LINK && !link)
490 pb_log("network: configuring interface %s\n", interface->name);
492 if (!config || config->method == CONFIG_METHOD_DHCP) {
493 configure_interface_dhcp(network, interface);
495 } else if (config->method == CONFIG_METHOD_STATIC) {
496 configure_interface_static(network, interface, config);
497 /* Nothing left to do for static interfaces */
498 pending_network_jobs_start();
501 interface->state = IFSTATE_CONFIGURED;
504 void network_requery_device(struct network *network,
505 struct discover_device *dev)
507 const struct interface_config *config;
508 struct interface *interface;
510 interface = find_interface_by_uuid(network, dev->uuid);
514 if (interface->udhcpc_process) {
515 interface->udhcpc_process->exit_cb = NULL;
516 interface->udhcpc_process->data = NULL;
517 process_stop_async(interface->udhcpc_process);
518 process_release(interface->udhcpc_process);
521 config = find_config_by_hwaddr(interface->hwaddr);
523 if (config && config->ignore)
526 if (!config || config->method == CONFIG_METHOD_DHCP) {
527 /* Restart DHCP. Once we acquire a lease, we'll re-start
528 * the requery timeout (based on any reboottime DHCP option)
530 configure_interface_dhcp(network, interface);
532 } else if (config->method == CONFIG_METHOD_STATIC &&
533 config->static_config.url) {
534 /* Redownload statically-provided URL, and manually restart
536 device_handler_process_url(network->handler,
537 config->static_config.url,
538 mac_bytes_to_string(interface->dev,
540 sizeof(interface->hwaddr)),
541 config->static_config.address);
542 device_handler_start_requery_timeout(network->handler,
547 static int network_handle_nlmsg(struct network *network, struct nlmsghdr *nlmsg)
549 bool have_ifaddr, have_ifname;
550 struct interface *interface, *tmp;
551 struct ifinfomsg *info;
555 char ifname[IFNAMSIZ];
559 /* we're only interested in NEWLINK messages */
560 type = nlmsg->nlmsg_type;
561 if (!(type == RTM_NEWLINK || type == RTM_DELLINK))
564 info = NLMSG_DATA(nlmsg);
566 have_ifaddr = have_ifname = false;
569 attrlen = nlmsg->nlmsg_len - sizeof(*info);
571 /* extract the interface name and hardware address attributes */
572 for_each_rta(info + 1, attr, attrlen) {
573 void *data = RTA_DATA(attr);
575 switch (attr->rta_type) {
577 memcpy(ifaddr, data, sizeof(ifaddr));
582 strncpy(ifname, data, IFNAMSIZ);
583 ifname[IFNAMSIZ - 1] = '\0';
588 mtu = *(unsigned int *)data;
593 if (!have_ifaddr || !have_ifname)
596 if (type == RTM_DELLINK || mtu == 0) {
597 interface = find_interface_by_ifindex(network, info->ifi_index);
600 pb_log("network: interface %s removed\n", interface->name);
601 remove_interface(network, interface);
605 /* ignore the default tun device in some environments */
606 if (strncmp(ifname, "tun", strlen("tun")) == 0)
609 interface = find_interface_by_ifindex(network, info->ifi_index);
611 interface = talloc_zero(network, struct interface);
612 interface->ifindex = info->ifi_index;
613 interface->state = IFSTATE_NEW;
614 memcpy(interface->hwaddr, ifaddr, sizeof(interface->hwaddr));
615 strncpy(interface->name, ifname, sizeof(interface->name));
617 list_for_each_entry(&network->interfaces, tmp, list)
618 if (memcmp(interface->hwaddr, tmp->hwaddr,
619 sizeof(interface->hwaddr)) == 0) {
620 pb_log("%s: %s has duplicate MAC address, ignoring\n",
621 __func__, interface->name);
622 talloc_free(interface);
626 list_add(&network->interfaces, &interface->list);
627 create_interface_dev(network, interface);
630 /* A repeated RTM_NEWLINK can represent an interface name change */
631 if (strncmp(interface->name, ifname, IFNAMSIZ)) {
632 pb_debug("ifname update: %s -> %s\n", interface->name, ifname);
633 strncpy(interface->name, ifname, sizeof(interface->name));
634 talloc_free(interface->dev->device->id);
635 interface->dev->device->id =
636 talloc_strdup(interface->dev->device, ifname);
639 /* notify the sysinfo code about changes to this interface */
640 if (strcmp(interface->name, "lo"))
641 system_info_register_interface(
642 sizeof(interface->hwaddr),
643 interface->hwaddr, interface->name,
644 info->ifi_flags & IFF_LOWER_UP);
647 create_interface_dev(network, interface);
649 if (!interface->ready && strncmp(interface->name, "lo", strlen("lo"))) {
650 pb_log("%s not marked ready yet\n", interface->name);
654 configure_interface(network, interface,
655 info->ifi_flags & IFF_UP,
656 info->ifi_flags & IFF_LOWER_UP);
661 void network_mark_interface_ready(struct device_handler *handler,
662 int ifindex, const char *ifname, uint8_t *mac, int hwsize)
664 struct network *network = device_handler_get_network(handler);
665 struct interface *interface, *tmp = NULL;
669 pb_log("Network not ready - can not mark interface ready\n");
673 if (hwsize != HWADDR_SIZE)
676 if (strncmp(ifname, "lo", strlen("lo")) == 0)
679 interface = find_interface_by_ifindex(network, ifindex);
681 pb_debug("Creating ready interface %d - %s\n",
683 interface = talloc_zero(network, struct interface);
684 interface->ifindex = ifindex;
685 interface->state = IFSTATE_NEW;
686 memcpy(interface->hwaddr, mac, HWADDR_SIZE);
687 strncpy(interface->name, ifname, sizeof(interface->name) - 1);
689 list_for_each_entry(&network->interfaces, tmp, list)
690 if (memcmp(interface->hwaddr, tmp->hwaddr,
691 sizeof(interface->hwaddr)) == 0) {
692 pb_log("%s: %s has duplicate MAC address, ignoring\n",
693 __func__, interface->name);
694 talloc_free(interface);
698 list_add(&network->interfaces, &interface->list);
699 create_interface_dev(network, interface);
702 if (interface->ready) {
703 pb_log("%s already ready\n", interface->name);
707 if (strncmp(interface->name, ifname, strlen(ifname)) != 0) {
708 pb_debug("ifname update from udev: %s -> %s\n", interface->name, ifname);
709 strncpy(interface->name, ifname, sizeof(interface->name) - 1);
710 talloc_free(interface->dev->device->id);
711 interface->dev->device->id =
712 talloc_strdup(interface->dev->device, ifname);
715 if (memcmp(interface->hwaddr, mac, HWADDR_SIZE) != 0) {
716 macstr = mac_bytes_to_string(interface, mac, hwsize);
717 pb_log("Warning - new MAC for interface %d does not match: %s\n",
722 pb_log("Interface %s ready\n", ifname);
723 interface->ready = true;
724 configure_interface(network, interface, false, false);
727 static int network_netlink_process(void *arg)
729 struct network *network = arg;
730 struct nlmsghdr *nlmsg;
736 memset(&msg, 0, sizeof(msg));
743 iov.iov_len = network->netlink_buf_size;
744 iov.iov_base = network->netlink_buf;
746 rc = recvmsg(network->netlink_sd, &msg, flags);
749 perror("netlink recv header");
755 /* if the netlink message was larger than our buffer, realloc
756 * before reading again */
757 if (len > network->netlink_buf_size || msg.msg_flags & MSG_TRUNC) {
758 network->netlink_buf_size *= 2;
759 network->netlink_buf = talloc_realloc(network,
760 network->netlink_buf,
762 network->netlink_buf_size);
766 /* otherwise, we're good to read the entire message without PEEK */
767 if (flags == MSG_PEEK) {
772 for_each_nlmsg(network->netlink_buf, nlmsg, len)
773 network_handle_nlmsg(network, nlmsg);
778 static void network_init_dns(struct network *network)
780 const struct config *config;
786 if (network->dry_run)
789 config = config_get();
790 if (!config || !config->network.n_dns_servers)
793 rc = read_file(network, "/etc/resolv.conf", &buf, &len);
796 buf = talloc_strdup(network, "");
802 for (i = 0; i < config->network.n_dns_servers; i++) {
806 dns_conf = talloc_asprintf(network, "nameserver %s\n",
807 config->network.dns_servers[i]);
809 if (strstr(buf, dns_conf)) {
810 talloc_free(dns_conf);
814 dns_conf_len = strlen(dns_conf);
815 buf = talloc_realloc(network, buf, char, len + dns_conf_len + 1);
816 memcpy(buf + len, dns_conf, dns_conf_len);
821 talloc_free(dns_conf);
825 rc = replace_file("/etc/resolv.conf", buf, len);
827 pb_log("error replacing resolv.conf: %s\n",
834 struct network *network_init(struct device_handler *handler,
835 struct waitset *waitset, bool dry_run)
837 struct network *network;
840 network = talloc(handler, struct network);
841 list_init(&network->interfaces);
842 network->handler = handler;
843 network->dry_run = dry_run;
844 network->manual_config = config_get()->network.n_interfaces != 0;
846 network_init_dns(network);
848 rc = network_init_netlink(network);
852 network->waiter = waiter_register_io(waitset, network->netlink_sd,
853 WAIT_IN, network_netlink_process, network);
855 if (!network->waiter)
858 rc = network_send_link_query(network);
865 network_shutdown(network);
869 int network_shutdown(struct network *network)
871 struct interface *interface;
874 waiter_remove(network->waiter);
876 list_for_each_entry(&network->interfaces, interface, list) {
877 if (interface->state == IFSTATE_IGNORED)
879 if (!strcmp(interface->name, "lo"))
881 interface_down(interface);
884 close(network->netlink_sd);
885 talloc_free(network);