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"
28 #define PIDFILE_BASE (LOCAL_STATE_DIR "/petitboot/")
29 #define INITIAL_BUFSIZE 4096
31 #define for_each_nlmsg(buf, nlmsg, len) \
32 for (nlmsg = (struct nlmsghdr *)buf; \
33 NLMSG_OK(nlmsg, len) && nlmsg->nlmsg_type != NLMSG_DONE; \
34 nlmsg = NLMSG_NEXT(nlmsg, len))
36 #define for_each_rta(buf, rta, attrlen) \
37 for (rta = (struct rtattr *)(buf); RTA_OK(rta, attrlen); \
38 rta = RTA_NEXT(rta, attrlen))
44 uint8_t hwaddr[HWADDR_SIZE];
48 IFSTATE_UP_WAITING_LINK,
53 struct list_item list;
54 struct process *udhcpc_process;
55 struct discover_device *dev;
59 struct list interfaces;
60 struct device_handler *handler;
61 struct waiter *waiter;
64 unsigned int netlink_buf_size;
69 static char *mac_bytes_to_string(void *ctx, uint8_t *addr, int len)
71 const int l = strlen("xx:");
76 return talloc_strdup(ctx, "");
78 buf = talloc_array(ctx, char, (len * l) + 1);
80 for (i = 0; i < len; i++)
81 sprintf(buf + (l * i), "%02x:", addr[i]);
83 *(buf + (l * len) - 1) = '\0';
88 static const struct interface_config *find_config_by_hwaddr(
91 const struct config *config;
94 config = config_get();
98 for (i = 0; i < config->network.n_interfaces; i++) {
99 struct interface_config *ifconf = config->network.interfaces[i];
101 if (!memcmp(ifconf->hwaddr, hwaddr, HWADDR_SIZE))
108 static struct interface *find_interface_by_ifindex(struct network *network,
111 struct interface *interface;
113 list_for_each_entry(&network->interfaces, interface, list)
114 if (interface->ifindex == ifindex)
120 static struct interface *find_interface_by_name(struct network *network,
123 struct interface *interface;
125 list_for_each_entry(&network->interfaces, interface, list)
126 if (!strcmp(interface->name, name))
132 static struct interface *find_interface_by_uuid(struct network *network,
135 struct interface *interface;
138 list_for_each_entry(&network->interfaces, interface, list) {
139 mac = mac_bytes_to_string(interface, interface->hwaddr,
140 sizeof(interface->hwaddr));
141 if (!strcmp(mac, uuid)) {
151 uint8_t *find_mac_by_name(void *ctx, struct network *network,
154 struct interface *interface;
156 interface = find_interface_by_name(network, name);
160 return talloc_memdup(ctx, &interface->hwaddr,
161 sizeof(uint8_t) * HWADDR_SIZE);
164 static int network_init_netlink(struct network *network)
166 struct sockaddr_nl addr;
169 memset(&addr, 0, sizeof(addr));
170 addr.nl_family = AF_NETLINK;
171 addr.nl_groups = RTMGRP_LINK;
173 network->netlink_sd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
174 if (network->netlink_sd < 0) {
175 perror("socket(AF_NETLINK)");
179 rc = bind(network->netlink_sd, (struct sockaddr *)&addr, sizeof(addr));
181 perror("bind(sockaddr_nl)");
182 close(network->netlink_sd);
186 network->netlink_buf_size = INITIAL_BUFSIZE;
187 network->netlink_buf = talloc_array(network, char,
188 network->netlink_buf_size);
193 static int network_send_link_query(struct network *network)
197 struct nlmsghdr nlmsg;
198 struct rtgenmsg rtmsg;
201 memset(&msg, 0, sizeof(msg));
203 msg.nlmsg.nlmsg_len = sizeof(msg);
204 msg.nlmsg.nlmsg_type = RTM_GETLINK;
205 msg.nlmsg.nlmsg_flags = NLM_F_REQUEST | NLM_F_ROOT;
206 msg.nlmsg.nlmsg_seq = 0;
207 msg.nlmsg.nlmsg_pid = 0;
208 msg.rtmsg.rtgen_family = AF_UNSPEC;
210 rc = send(network->netlink_sd, &msg, sizeof(msg), MSG_NOSIGNAL);
211 if (rc != sizeof(msg))
217 static void create_interface_dev(struct network *network,
218 struct interface *interface)
220 char *uuid = mac_bytes_to_string(interface, interface->hwaddr,
221 sizeof(interface->hwaddr));
223 interface->dev = discover_device_create(network->handler, uuid,
225 interface->dev->device->type = DEVICE_TYPE_NETWORK;
226 device_handler_add_device(network->handler, interface->dev);
230 static void remove_interface(struct network *network,
231 struct interface *interface)
234 device_handler_remove(network->handler, interface->dev);
235 list_remove(&interface->list);
236 talloc_free(interface);
239 void network_register_device(struct network *network,
240 struct discover_device *dev)
242 struct interface *iface;
245 iface = find_interface_by_uuid(network, dev->uuid);
247 iface = find_interface_by_name(network, dev->label);
252 dev->uuid = mac_bytes_to_string(iface->dev, iface->hwaddr,
253 sizeof(iface->hwaddr));
256 void network_unregister_device(struct network *network,
257 struct discover_device *dev)
259 struct interface *iface;
261 iface = find_interface_by_uuid(network, dev->uuid);
268 static int interface_change(struct interface *interface, bool up)
270 const char *statestr = up ? "up" : "down";
273 if (!up && interface->udhcpc_process) {
274 /* we don't care about the callback from here */
275 interface->udhcpc_process->exit_cb = NULL;
276 interface->udhcpc_process->data = NULL;
277 process_stop_async(interface->udhcpc_process);
278 process_release(interface->udhcpc_process);
282 rc = process_run_simple(interface, pb_system_apps.ip,
283 "address", "flush", "dev", interface->name,
286 pb_log("failed to flush addresses from interface %s\n",
290 rc = process_run_simple(interface, pb_system_apps.ip,
291 "link", "set", interface->name, statestr, NULL);
293 pb_log("failed to bring interface %s %s\n", interface->name,
300 static int interface_up(struct interface *interface)
302 return interface_change(interface, true);
305 static int interface_down(struct interface *interface)
307 return interface_change(interface, false);
310 static void udhcpc_process_exit(struct process *process)
312 struct interface *interface = process->data;
313 pb_debug("udhcp client [pid %d] for interface %s exited, rc %d\n",
314 process->pid, interface->name, process->exit_status);
315 interface->udhcpc_process = NULL;
316 process_release(process);
319 static void configure_interface_dhcp(struct network *network,
320 struct interface *interface)
322 const struct platform *platform;
323 char pidfile[256], id[10];
324 struct process *process;
326 const char *argv[] = {
327 pb_system_apps.udhcpc,
331 "-O", "pxepathprefix",
333 "-i", interface->name,
334 "-x", id, /* [11,12] - dhcp client identifier */
338 device_handler_status_dev_info(network->handler, interface->dev,
339 _("Configuring with DHCP"));
341 snprintf(pidfile, sizeof(pidfile), "%s/udhcpc-%s.pid",
342 PIDFILE_BASE, interface->name);
344 platform = platform_get();
345 if (platform && platform->dhcp_arch_id != 0xffff)
346 snprintf(id, sizeof(id), "0x5d:%04x", platform->dhcp_arch_id);
350 process = process_create(interface);
352 process->path = pb_system_apps.udhcpc;
353 process->argv = argv;
354 process->exit_cb = udhcpc_process_exit;
355 process->data = interface;
357 rc = process_run_async(process);
360 process_release(process);
362 interface->udhcpc_process = process;
367 static void configure_interface_static(struct network *network,
368 struct interface *interface,
369 const struct interface_config *config)
373 device_handler_status_dev_info(network->handler, interface->dev,
374 _("Configuring with static address (ip: %s)"),
375 config->static_config.address);
377 rc = process_run_simple(interface, pb_system_apps.ip,
378 "address", "add", config->static_config.address,
379 "dev", interface->name, NULL);
383 pb_log("failed to add address %s to interface %s\n",
384 config->static_config.address,
389 system_info_set_interface_address(sizeof(interface->hwaddr),
391 config->static_config.address);
393 /* we need the interface up before we can route through it */
394 rc = interface_up(interface);
398 if (config->static_config.gateway)
399 rc = process_run_simple(interface, pb_system_apps.ip,
400 "route", "add", "default",
401 "via", config->static_config.gateway,
405 pb_log("failed to add default route %s on interface %s\n",
406 config->static_config.gateway,
410 if (config->static_config.url) {
411 pb_log("config URL %s\n", config->static_config.url);
412 device_handler_process_url(network->handler,
413 config->static_config.url,
414 mac_bytes_to_string(interface->dev,
416 sizeof(interface->hwaddr)),
417 config->static_config.address);
423 static void configure_interface(struct network *network,
424 struct interface *interface, bool up, bool link)
426 const struct interface_config *config = NULL;
428 if (interface->state == IFSTATE_IGNORED)
431 /* old interface? check that we're still up and running */
432 if (interface->state == IFSTATE_CONFIGURED) {
434 interface->state = IFSTATE_NEW;
436 interface->state = IFSTATE_UP_WAITING_LINK;
438 pb_debug("network: skipping configured interface %s\n",
444 /* always up the lookback, no other handling required */
445 if (!strcmp(interface->name, "lo")) {
446 if (interface->state == IFSTATE_NEW)
447 interface_up(interface);
448 interface->state = IFSTATE_CONFIGURED;
452 config = find_config_by_hwaddr(interface->hwaddr);
453 if (config && config->ignore) {
454 pb_log("network: ignoring interface %s\n", interface->name);
455 interface->state = IFSTATE_IGNORED;
459 /* if we're in manual config mode, we need an interface configuration */
460 if (network->manual_config && !config) {
461 interface->state = IFSTATE_IGNORED;
462 pb_log("network: skipping %s: manual config mode, "
463 "but no config for this interface\n",
468 /* new interface? bring up to the point so we can detect a link */
469 if (interface->state == IFSTATE_NEW) {
471 interface_up(interface);
472 pb_log("network: bringing up interface %s\n",
477 interface->state = IFSTATE_UP_WAITING_LINK;
481 /* no link? wait for a notification */
482 if (interface->state == IFSTATE_UP_WAITING_LINK && !link)
485 pb_log("network: configuring interface %s\n", interface->name);
487 if (!config || config->method == CONFIG_METHOD_DHCP) {
488 configure_interface_dhcp(network, interface);
490 } else if (config->method == CONFIG_METHOD_STATIC) {
491 configure_interface_static(network, interface, config);
494 interface->state = IFSTATE_CONFIGURED;
497 static int network_handle_nlmsg(struct network *network, struct nlmsghdr *nlmsg)
499 bool have_ifaddr, have_ifname;
500 struct interface *interface, *tmp;
501 struct ifinfomsg *info;
505 char ifname[IFNAMSIZ+1];
509 /* we're only interested in NEWLINK messages */
510 type = nlmsg->nlmsg_type;
511 if (!(type == RTM_NEWLINK || type == RTM_DELLINK))
514 info = NLMSG_DATA(nlmsg);
516 have_ifaddr = have_ifname = false;
519 attrlen = nlmsg->nlmsg_len - sizeof(*info);
521 /* extract the interface name and hardware address attributes */
522 for_each_rta(info + 1, attr, attrlen) {
523 void *data = RTA_DATA(attr);
525 switch (attr->rta_type) {
527 memcpy(ifaddr, data, sizeof(ifaddr));
532 strncpy(ifname, data, IFNAMSIZ);
537 mtu = *(unsigned int *)data;
542 if (!have_ifaddr || !have_ifname)
545 if (type == RTM_DELLINK || mtu == 0) {
546 interface = find_interface_by_ifindex(network, info->ifi_index);
549 pb_log("network: interface %s removed\n", interface->name);
550 remove_interface(network, interface);
554 /* ignore the default tun device in some environments */
555 if (strncmp(ifname, "tun", strlen("tun")) == 0)
558 interface = find_interface_by_ifindex(network, info->ifi_index);
560 interface = talloc_zero(network, struct interface);
561 interface->ifindex = info->ifi_index;
562 interface->state = IFSTATE_NEW;
563 memcpy(interface->hwaddr, ifaddr, sizeof(interface->hwaddr));
564 strncpy(interface->name, ifname, sizeof(interface->name) - 1);
566 list_for_each_entry(&network->interfaces, tmp, list)
567 if (memcmp(interface->hwaddr, tmp->hwaddr,
568 sizeof(interface->hwaddr)) == 0) {
569 pb_log("%s: %s has duplicate MAC address, ignoring\n",
570 __func__, interface->name);
571 talloc_free(interface);
575 list_add(&network->interfaces, &interface->list);
576 create_interface_dev(network, interface);
579 /* A repeated RTM_NEWLINK can represent an interface name change */
580 if (strncmp(interface->name, ifname, IFNAMSIZ)) {
581 pb_debug("ifname update: %s -> %s\n", interface->name, ifname);
582 strncpy(interface->name, ifname, sizeof(interface->name) - 1);
583 talloc_free(interface->dev->device->id);
584 interface->dev->device->id =
585 talloc_strdup(interface->dev->device, ifname);
588 /* notify the sysinfo code about changes to this interface */
589 if (strcmp(interface->name, "lo"))
590 system_info_register_interface(
591 sizeof(interface->hwaddr),
592 interface->hwaddr, interface->name,
593 info->ifi_flags & IFF_LOWER_UP);
596 create_interface_dev(network, interface);
598 configure_interface(network, interface,
599 info->ifi_flags & IFF_UP,
600 info->ifi_flags & IFF_LOWER_UP);
605 static int network_netlink_process(void *arg)
607 struct network *network = arg;
608 struct nlmsghdr *nlmsg;
614 memset(&msg, 0, sizeof(msg));
621 iov.iov_len = network->netlink_buf_size;
622 iov.iov_base = network->netlink_buf;
624 rc = recvmsg(network->netlink_sd, &msg, flags);
627 perror("netlink recv header");
633 /* if the netlink message was larger than our buffer, realloc
634 * before reading again */
635 if (len > network->netlink_buf_size || msg.msg_flags & MSG_TRUNC) {
636 network->netlink_buf_size *= 2;
637 network->netlink_buf = talloc_realloc(network,
638 network->netlink_buf,
640 network->netlink_buf_size);
644 /* otherwise, we're good to read the entire message without PEEK */
645 if (flags == MSG_PEEK) {
650 for_each_nlmsg(network->netlink_buf, nlmsg, len)
651 network_handle_nlmsg(network, nlmsg);
656 static void network_init_dns(struct network *network)
658 const struct config *config;
664 if (network->dry_run)
667 config = config_get();
668 if (!config || !config->network.n_dns_servers)
671 rc = read_file(network, "/etc/resolv.conf", &buf, &len);
674 buf = talloc_strdup(network, "");
680 for (i = 0; i < config->network.n_dns_servers; i++) {
684 dns_conf = talloc_asprintf(network, "nameserver %s\n",
685 config->network.dns_servers[i]);
687 if (strstr(buf, dns_conf)) {
688 talloc_free(dns_conf);
692 dns_conf_len = strlen(dns_conf);
693 buf = talloc_realloc(network, buf, char, len + dns_conf_len + 1);
694 memcpy(buf + len, dns_conf, dns_conf_len);
699 talloc_free(dns_conf);
703 rc = replace_file("/etc/resolv.conf", buf, len);
705 pb_log("error replacing resolv.conf: %s\n",
712 struct network *network_init(struct device_handler *handler,
713 struct waitset *waitset, bool dry_run)
715 struct network *network;
718 network = talloc(handler, struct network);
719 list_init(&network->interfaces);
720 network->handler = handler;
721 network->dry_run = dry_run;
722 network->manual_config = config_get()->network.n_interfaces != 0;
724 network_init_dns(network);
726 rc = network_init_netlink(network);
730 network->waiter = waiter_register_io(waitset, network->netlink_sd,
731 WAIT_IN, network_netlink_process, network);
733 if (!network->waiter)
736 rc = network_send_link_query(network);
743 network_shutdown(network);
747 int network_shutdown(struct network *network)
749 struct interface *interface;
752 waiter_remove(network->waiter);
754 list_for_each_entry(&network->interfaces, interface, list) {
755 if (interface->state == IFSTATE_IGNORED)
757 if (!strcmp(interface->name, "lo"))
759 interface_down(interface);
762 close(network->netlink_sd);
763 talloc_free(network);