7 #include <sys/socket.h>
9 #include <linux/netlink.h>
10 #include <linux/rtnetlink.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>
24 #include "device-handler.h"
27 #define PIDFILE_BASE (LOCAL_STATE_DIR "/petitboot/")
28 #define INITIAL_BUFSIZE 4096
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))
35 #define for_each_rta(buf, rta, attrlen) \
36 for (rta = (struct rtattr *)(buf); RTA_OK(rta, attrlen); \
37 rta = RTA_NEXT(rta, attrlen))
43 uint8_t hwaddr[HWADDR_SIZE];
47 IFSTATE_UP_WAITING_LINK,
52 struct list_item list;
53 struct process *udhcpc_process;
54 struct discover_device *dev;
58 struct list interfaces;
59 struct device_handler *handler;
60 struct waiter *waiter;
63 unsigned int netlink_buf_size;
68 static char *mac_bytes_to_string(void *ctx, uint8_t *addr, int len)
70 const int l = strlen("xx:");
75 return talloc_strdup(ctx, "");
77 buf = talloc_array(ctx, char, (len * l) + 1);
79 for (i = 0; i < len; i++)
80 sprintf(buf + (l * i), "%02x:", addr[i]);
82 *(buf + (l * len) - 1) = '\0';
87 static const struct interface_config *find_config_by_hwaddr(
90 const struct config *config;
93 config = config_get();
97 for (i = 0; i < config->network.n_interfaces; i++) {
98 struct interface_config *ifconf = config->network.interfaces[i];
100 if (!memcmp(ifconf->hwaddr, hwaddr, HWADDR_SIZE))
107 static struct interface *find_interface_by_ifindex(struct network *network,
110 struct interface *interface;
112 list_for_each_entry(&network->interfaces, interface, list)
113 if (interface->ifindex == ifindex)
119 static struct interface *find_interface_by_name(struct network *network,
122 struct interface *interface;
124 list_for_each_entry(&network->interfaces, interface, list)
125 if (!strcmp(interface->name, name))
131 static struct interface *find_interface_by_uuid(struct network *network,
134 struct interface *interface;
137 list_for_each_entry(&network->interfaces, interface, list) {
138 mac = mac_bytes_to_string(interface, interface->hwaddr,
139 sizeof(interface->hwaddr));
140 if (!strcmp(mac, uuid)) {
150 uint8_t *find_mac_by_name(void *ctx, struct network *network,
153 struct interface *interface;
155 interface = find_interface_by_name(network, name);
159 return talloc_memdup(ctx, &interface->hwaddr,
160 sizeof(uint8_t) * HWADDR_SIZE);
163 static int network_init_netlink(struct network *network)
165 struct sockaddr_nl addr;
168 memset(&addr, 0, sizeof(addr));
169 addr.nl_family = AF_NETLINK;
170 addr.nl_groups = RTMGRP_LINK;
172 network->netlink_sd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
173 if (network->netlink_sd < 0) {
174 perror("socket(AF_NETLINK)");
178 rc = bind(network->netlink_sd, (struct sockaddr *)&addr, sizeof(addr));
180 perror("bind(sockaddr_nl)");
181 close(network->netlink_sd);
185 network->netlink_buf_size = INITIAL_BUFSIZE;
186 network->netlink_buf = talloc_array(network, char,
187 network->netlink_buf_size);
192 static int network_send_link_query(struct network *network)
196 struct nlmsghdr nlmsg;
197 struct rtgenmsg rtmsg;
200 memset(&msg, 0, sizeof(msg));
202 msg.nlmsg.nlmsg_len = sizeof(msg);
203 msg.nlmsg.nlmsg_type = RTM_GETLINK;
204 msg.nlmsg.nlmsg_flags = NLM_F_REQUEST | NLM_F_ROOT;
205 msg.nlmsg.nlmsg_seq = 0;
206 msg.nlmsg.nlmsg_pid = 0;
207 msg.rtmsg.rtgen_family = AF_UNSPEC;
209 rc = send(network->netlink_sd, &msg, sizeof(msg), MSG_NOSIGNAL);
210 if (rc != sizeof(msg))
216 static void add_interface(struct network *network,
217 struct interface *interface)
219 char *uuid = mac_bytes_to_string(interface, interface->hwaddr,
220 sizeof(interface->hwaddr));
222 list_add(&network->interfaces, &interface->list);
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;
244 iface = find_interface_by_uuid(network, dev->uuid);
249 dev->uuid = mac_bytes_to_string(iface->dev, iface->hwaddr,
250 sizeof(iface->hwaddr));
253 void network_unregister_device(struct network *network,
254 struct discover_device *dev)
256 struct interface *iface;
258 iface = find_interface_by_uuid(network, dev->uuid);
265 static int interface_change(struct interface *interface, bool up)
267 const char *statestr = up ? "up" : "down";
270 if (!up && interface->udhcpc_process) {
271 /* we don't care about the callback from here */
272 interface->udhcpc_process->exit_cb = NULL;
273 interface->udhcpc_process->data = NULL;
274 process_stop_async(interface->udhcpc_process);
275 process_release(interface->udhcpc_process);
279 rc = process_run_simple(interface, pb_system_apps.ip,
280 "address", "flush", "dev", interface->name,
283 pb_log("failed to flush addresses from interface %s\n",
287 rc = process_run_simple(interface, pb_system_apps.ip,
288 "link", "set", interface->name, statestr, NULL);
290 pb_log("failed to bring interface %s %s\n", interface->name,
297 static int interface_up(struct interface *interface)
299 return interface_change(interface, true);
302 static int interface_down(struct interface *interface)
304 return interface_change(interface, false);
307 static void udhcpc_process_exit(struct process *process)
309 struct interface *interface = process->data;
310 pb_debug("udhcp client [pid %d] for interface %s exited, rc %d\n",
311 process->pid, interface->name, process->exit_status);
312 interface->udhcpc_process = NULL;
313 process_release(process);
316 static void configure_interface_dhcp(struct interface *interface)
318 const struct platform *platform;
319 char pidfile[256], id[10];
320 struct process *process;
322 const char *argv[] = {
323 pb_system_apps.udhcpc,
327 "-O", "pxepathprefix",
329 "-i", interface->name,
330 "-x", id, /* [11,12] - dhcp client identifier */
334 snprintf(pidfile, sizeof(pidfile), "%s/udhcpc-%s.pid",
335 PIDFILE_BASE, interface->name);
337 platform = platform_get();
338 if (platform && platform->dhcp_arch_id != 0xffff)
339 snprintf(id, sizeof(id), "0x5d:%04x", platform->dhcp_arch_id);
343 process = process_create(interface);
345 process->path = pb_system_apps.udhcpc;
346 process->argv = argv;
347 process->exit_cb = udhcpc_process_exit;
348 process->data = interface;
350 rc = process_run_async(process);
353 process_release(process);
355 interface->udhcpc_process = process;
360 static void configure_interface_static(struct network *network,
361 struct interface *interface,
362 const struct interface_config *config)
366 rc = process_run_simple(interface, pb_system_apps.ip,
367 "address", "add", config->static_config.address,
368 "dev", interface->name, NULL);
372 pb_log("failed to add address %s to interface %s\n",
373 config->static_config.address,
378 /* we need the interface up before we can route through it */
379 rc = interface_up(interface);
383 if (config->static_config.gateway)
384 rc = process_run_simple(interface, pb_system_apps.ip,
385 "route", "add", "default",
386 "via", config->static_config.gateway,
390 pb_log("failed to add default route %s on interface %s\n",
391 config->static_config.gateway,
395 if (config->static_config.url) {
396 pb_log("config URL %s\n", config->static_config.url);
397 device_handler_process_url(network->handler,
398 config->static_config.url,
399 mac_bytes_to_string(interface->dev,
401 sizeof(interface->hwaddr)),
402 config->static_config.address);
408 static void configure_interface(struct network *network,
409 struct interface *interface, bool up, bool link)
411 const struct interface_config *config = NULL;
413 if (interface->state == IFSTATE_IGNORED)
416 /* old interface? check that we're still up and running */
417 if (interface->state == IFSTATE_CONFIGURED) {
419 interface->state = IFSTATE_NEW;
421 interface->state = IFSTATE_UP_WAITING_LINK;
423 pb_debug("network: skipping configured interface %s\n",
429 /* always up the lookback, no other handling required */
430 if (!strcmp(interface->name, "lo")) {
431 if (interface->state == IFSTATE_NEW)
432 interface_up(interface);
433 interface->state = IFSTATE_CONFIGURED;
437 config = find_config_by_hwaddr(interface->hwaddr);
438 if (config && config->ignore) {
439 pb_log("network: ignoring interface %s\n", interface->name);
440 interface->state = IFSTATE_IGNORED;
444 /* if we're in manual config mode, we need an interface configuration */
445 if (network->manual_config && !config) {
446 interface->state = IFSTATE_IGNORED;
447 pb_log("network: skipping %s: manual config mode, "
448 "but no config for this interface\n",
453 /* new interface? bring up to the point so we can detect a link */
454 if (interface->state == IFSTATE_NEW) {
456 interface_up(interface);
457 pb_log("network: bringing up interface %s\n",
462 interface->state = IFSTATE_UP_WAITING_LINK;
466 /* no link? wait for a notification */
467 if (interface->state == IFSTATE_UP_WAITING_LINK && !link)
470 pb_log("network: configuring interface %s\n", interface->name);
472 if (!config || config->method == CONFIG_METHOD_DHCP) {
473 configure_interface_dhcp(interface);
475 } else if (config->method == CONFIG_METHOD_STATIC) {
476 configure_interface_static(network, interface, config);
479 interface->state = IFSTATE_CONFIGURED;
482 static int network_handle_nlmsg(struct network *network, struct nlmsghdr *nlmsg)
484 bool have_ifaddr, have_ifname;
485 struct interface *interface;
486 struct ifinfomsg *info;
490 char ifname[IFNAMSIZ+1];
494 /* we're only interested in NEWLINK messages */
495 type = nlmsg->nlmsg_type;
496 if (!(type == RTM_NEWLINK || type == RTM_DELLINK))
499 info = NLMSG_DATA(nlmsg);
501 have_ifaddr = have_ifname = false;
504 attrlen = nlmsg->nlmsg_len - sizeof(*info);
506 /* extract the interface name and hardware address attributes */
507 for_each_rta(info + 1, attr, attrlen) {
508 void *data = RTA_DATA(attr);
510 switch (attr->rta_type) {
512 memcpy(ifaddr, data, sizeof(ifaddr));
517 strncpy(ifname, data, IFNAMSIZ);
522 mtu = *(unsigned int *)data;
527 if (!have_ifaddr || !have_ifname)
530 if (type == RTM_DELLINK || mtu == 0) {
531 interface = find_interface_by_ifindex(network, info->ifi_index);
534 pb_log("network: interface %s removed\n", interface->name);
535 remove_interface(network, interface);
539 /* ignore the default tun device in some environments */
540 if (strncmp(ifname, "tun", strlen("tun")) == 0)
543 interface = find_interface_by_ifindex(network, info->ifi_index);
545 interface = talloc_zero(network, struct interface);
546 interface->ifindex = info->ifi_index;
547 interface->state = IFSTATE_NEW;
548 memcpy(interface->hwaddr, ifaddr, sizeof(interface->hwaddr));
549 strncpy(interface->name, ifname, sizeof(interface->name) - 1);
550 add_interface(network, interface);
553 /* A repeated RTM_NEWLINK can represent an interface name change */
554 if (strncmp(interface->name, ifname, IFNAMSIZ)) {
555 pb_debug("ifname update: %s -> %s\n", interface->name, ifname);
556 strncpy(interface->name, ifname, sizeof(interface->name) - 1);
557 talloc_free(interface->dev->device->id);
558 interface->dev->device->id =
559 talloc_strdup(interface->dev->device, ifname);
562 /* notify the sysinfo code about changes to this interface */
563 if (strcmp(interface->name, "lo"))
564 system_info_register_interface(
565 sizeof(interface->hwaddr),
566 interface->hwaddr, interface->name,
567 info->ifi_flags & IFF_LOWER_UP);
569 configure_interface(network, interface,
570 info->ifi_flags & IFF_UP,
571 info->ifi_flags & IFF_LOWER_UP);
576 static int network_netlink_process(void *arg)
578 struct network *network = arg;
579 struct nlmsghdr *nlmsg;
585 memset(&msg, 0, sizeof(msg));
592 iov.iov_len = network->netlink_buf_size;
593 iov.iov_base = network->netlink_buf;
595 rc = recvmsg(network->netlink_sd, &msg, flags);
598 perror("netlink recv header");
604 /* if the netlink message was larger than our buffer, realloc
605 * before reading again */
606 if (len > network->netlink_buf_size || msg.msg_flags & MSG_TRUNC) {
607 network->netlink_buf_size *= 2;
608 network->netlink_buf = talloc_realloc(network,
609 network->netlink_buf,
611 network->netlink_buf_size);
615 /* otherwise, we're good to read the entire message without PEEK */
616 if (flags == MSG_PEEK) {
621 for_each_nlmsg(network->netlink_buf, nlmsg, len)
622 network_handle_nlmsg(network, nlmsg);
627 static void network_init_dns(struct network *network)
629 const struct config *config;
635 if (network->dry_run)
638 config = config_get();
639 if (!config || !config->network.n_dns_servers)
642 rc = read_file(network, "/etc/resolv.conf", &buf, &len);
645 buf = talloc_strdup(network, "");
651 for (i = 0; i < config->network.n_dns_servers; i++) {
655 dns_conf = talloc_asprintf(network, "nameserver %s\n",
656 config->network.dns_servers[i]);
658 if (strstr(buf, dns_conf)) {
659 talloc_free(dns_conf);
663 dns_conf_len = strlen(dns_conf);
664 buf = talloc_realloc(network, buf, char, len + dns_conf_len + 1);
665 memcpy(buf + len, dns_conf, dns_conf_len);
670 talloc_free(dns_conf);
674 rc = replace_file("/etc/resolv.conf", buf, len);
676 pb_log("error replacing resolv.conf: %s\n",
683 struct network *network_init(struct device_handler *handler,
684 struct waitset *waitset, bool dry_run)
686 struct network *network;
689 network = talloc(handler, struct network);
690 list_init(&network->interfaces);
691 network->handler = handler;
692 network->dry_run = dry_run;
693 network->manual_config = config_get()->network.n_interfaces != 0;
695 network_init_dns(network);
697 rc = network_init_netlink(network);
701 network->waiter = waiter_register_io(waitset, network->netlink_sd,
702 WAIT_IN, network_netlink_process, network);
704 if (!network->waiter)
707 rc = network_send_link_query(network);
714 network_shutdown(network);
718 int network_shutdown(struct network *network)
720 struct interface *interface;
723 waiter_remove(network->waiter);
725 list_for_each_entry(&network->interfaces, interface, list) {
726 if (interface->state == IFSTATE_IGNORED)
728 if (!strcmp(interface->name, "lo"))
730 interface_down(interface);
733 close(network->netlink_sd);
734 talloc_free(network);