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 const struct interface_config *find_config_by_hwaddr(
71 const struct config *config;
74 config = config_get();
78 for (i = 0; i < config->network.n_interfaces; i++) {
79 struct interface_config *ifconf = config->network.interfaces[i];
81 if (!memcmp(ifconf->hwaddr, hwaddr, HWADDR_SIZE))
88 static struct interface *find_interface_by_ifindex(struct network *network,
91 struct interface *interface;
93 list_for_each_entry(&network->interfaces, interface, list)
94 if (interface->ifindex == ifindex)
100 static struct interface *find_interface_by_name(struct network *network,
103 struct interface *interface;
105 list_for_each_entry(&network->interfaces, interface, list)
106 if (!strcmp(interface->name, name))
112 uint8_t *find_mac_by_name(void *ctx, struct network *network,
115 struct interface *interface;
117 interface = find_interface_by_name(network, name);
121 return talloc_memdup(ctx, &interface->hwaddr,
122 sizeof(uint8_t) * HWADDR_SIZE);
125 static int network_init_netlink(struct network *network)
127 struct sockaddr_nl addr;
130 memset(&addr, 0, sizeof(addr));
131 addr.nl_family = AF_NETLINK;
132 addr.nl_groups = RTMGRP_LINK;
134 network->netlink_sd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
135 if (network->netlink_sd < 0) {
136 perror("socket(AF_NETLINK)");
140 rc = bind(network->netlink_sd, (struct sockaddr *)&addr, sizeof(addr));
142 perror("bind(sockaddr_nl)");
143 close(network->netlink_sd);
147 network->netlink_buf_size = INITIAL_BUFSIZE;
148 network->netlink_buf = talloc_array(network, char,
149 network->netlink_buf_size);
154 static int network_send_link_query(struct network *network)
158 struct nlmsghdr nlmsg;
159 struct rtgenmsg rtmsg;
162 memset(&msg, 0, sizeof(msg));
164 msg.nlmsg.nlmsg_len = sizeof(msg);
165 msg.nlmsg.nlmsg_type = RTM_GETLINK;
166 msg.nlmsg.nlmsg_flags = NLM_F_REQUEST | NLM_F_ROOT;
167 msg.nlmsg.nlmsg_seq = 0;
168 msg.nlmsg.nlmsg_pid = 0;
169 msg.rtmsg.rtgen_family = AF_UNSPEC;
171 rc = send(network->netlink_sd, &msg, sizeof(msg), MSG_NOSIGNAL);
172 if (rc != sizeof(msg))
178 static char *mac_bytes_to_string(void *ctx, uint8_t *addr, int len)
180 const int l = strlen("xx:");
185 return talloc_strdup(ctx, "");
187 buf = talloc_array(ctx, char, (len * l) + 1);
189 for (i = 0; i < len; i++)
190 sprintf(buf + (l * i), "%02x:", addr[i]);
192 *(buf + (l * len) - 1) = '\0';
197 static void add_interface(struct network *network,
198 struct interface *interface)
200 list_add(&network->interfaces, &interface->list);
201 interface->dev = discover_device_create(network->handler,
203 interface->dev->device->type = DEVICE_TYPE_NETWORK;
204 interface->dev->uuid = mac_bytes_to_string(interface->dev,
205 interface->hwaddr, sizeof(interface->hwaddr));
206 device_handler_add_device(network->handler, interface->dev);
209 static void remove_interface(struct network *network,
210 struct interface *interface)
213 device_handler_remove(network->handler, interface->dev);
214 list_remove(&interface->list);
215 talloc_free(interface);
218 void network_register_device(struct network *network,
219 struct discover_device *dev)
221 struct interface *iface;
223 iface = find_interface_by_name(network, dev->device->id);
228 dev->uuid = mac_bytes_to_string(iface->dev, iface->hwaddr,
229 sizeof(iface->hwaddr));
232 void network_unregister_device(struct network *network,
233 struct discover_device *dev)
235 struct interface *iface;
237 iface = find_interface_by_name(network, dev->device->id);
244 static int interface_change(struct interface *interface, bool up)
246 const char *statestr = up ? "up" : "down";
249 if (!up && interface->udhcpc_process) {
250 /* we don't care about the callback from here */
251 interface->udhcpc_process->exit_cb = NULL;
252 interface->udhcpc_process->data = NULL;
253 process_stop_async(interface->udhcpc_process);
254 process_release(interface->udhcpc_process);
258 rc = process_run_simple(interface, pb_system_apps.ip,
259 "address", "flush", "dev", interface->name,
262 pb_log("failed to flush addresses from interface %s\n",
266 rc = process_run_simple(interface, pb_system_apps.ip,
267 "link", "set", interface->name, statestr, NULL);
269 pb_log("failed to bring interface %s %s\n", interface->name,
276 static int interface_up(struct interface *interface)
278 return interface_change(interface, true);
281 static int interface_down(struct interface *interface)
283 return interface_change(interface, false);
286 static void udhcpc_process_exit(struct process *process)
288 struct interface *interface = process->data;
289 pb_debug("udhcp client [pid %d] for interface %s exited, rc %d\n",
290 process->pid, interface->name, process->exit_status);
291 interface->udhcpc_process = NULL;
292 process_release(process);
295 static void configure_interface_dhcp(struct interface *interface)
297 const struct platform *platform;
298 char pidfile[256], id[10];
299 struct process *process;
301 const char *argv[] = {
302 pb_system_apps.udhcpc,
306 "-O", "pxepathprefix",
308 "-i", interface->name,
309 "-x", id, /* [11,12] - dhcp client identifier */
313 snprintf(pidfile, sizeof(pidfile), "%s/udhcpc-%s.pid",
314 PIDFILE_BASE, interface->name);
316 platform = platform_get();
317 if (platform && platform->dhcp_arch_id != 0xffff)
318 snprintf(id, sizeof(id), "0x5d:%04x", platform->dhcp_arch_id);
322 process = process_create(interface);
324 process->path = pb_system_apps.udhcpc;
325 process->argv = argv;
326 process->exit_cb = udhcpc_process_exit;
327 process->data = interface;
329 rc = process_run_async(process);
332 process_release(process);
334 interface->udhcpc_process = process;
339 static void configure_interface_static(struct network *network,
340 struct interface *interface,
341 const struct interface_config *config)
345 rc = process_run_simple(interface, pb_system_apps.ip,
346 "address", "add", config->static_config.address,
347 "dev", interface->name, NULL);
351 pb_log("failed to add address %s to interface %s\n",
352 config->static_config.address,
357 /* we need the interface up before we can route through it */
358 rc = interface_up(interface);
362 if (config->static_config.gateway)
363 rc = process_run_simple(interface, pb_system_apps.ip,
364 "route", "add", "default",
365 "via", config->static_config.gateway,
369 pb_log("failed to add default route %s on interface %s\n",
370 config->static_config.gateway,
374 if (config->static_config.url) {
375 pb_log("config URL %s\n", config->static_config.url);
376 device_handler_process_url(network->handler,
377 config->static_config.url,
378 mac_bytes_to_string(interface->dev,
380 sizeof(interface->hwaddr)),
381 config->static_config.address);
387 static void configure_interface(struct network *network,
388 struct interface *interface, bool up, bool link)
390 const struct interface_config *config = NULL;
392 if (interface->state == IFSTATE_IGNORED)
395 /* old interface? check that we're still up and running */
396 if (interface->state == IFSTATE_CONFIGURED) {
398 interface->state = IFSTATE_NEW;
400 interface->state = IFSTATE_UP_WAITING_LINK;
402 pb_debug("network: skipping configured interface %s\n",
408 /* always up the lookback, no other handling required */
409 if (!strcmp(interface->name, "lo")) {
410 if (interface->state == IFSTATE_NEW)
411 interface_up(interface);
412 interface->state = IFSTATE_CONFIGURED;
416 config = find_config_by_hwaddr(interface->hwaddr);
417 if (config && config->ignore) {
418 pb_log("network: ignoring interface %s\n", interface->name);
419 interface->state = IFSTATE_IGNORED;
423 /* if we're in manual config mode, we need an interface configuration */
424 if (network->manual_config && !config) {
425 interface->state = IFSTATE_IGNORED;
426 pb_log("network: skipping %s: manual config mode, "
427 "but no config for this interface\n",
432 /* new interface? bring up to the point so we can detect a link */
433 if (interface->state == IFSTATE_NEW) {
435 interface_up(interface);
436 pb_log("network: bringing up interface %s\n",
441 interface->state = IFSTATE_UP_WAITING_LINK;
445 /* no link? wait for a notification */
446 if (interface->state == IFSTATE_UP_WAITING_LINK && !link)
449 pb_log("network: configuring interface %s\n", interface->name);
451 if (!config || config->method == CONFIG_METHOD_DHCP) {
452 configure_interface_dhcp(interface);
454 } else if (config->method == CONFIG_METHOD_STATIC) {
455 configure_interface_static(network, interface, config);
458 interface->state = IFSTATE_CONFIGURED;
461 static int network_handle_nlmsg(struct network *network, struct nlmsghdr *nlmsg)
463 bool have_ifaddr, have_ifname;
464 struct interface *interface;
465 struct ifinfomsg *info;
469 char ifname[IFNAMSIZ+1];
473 /* we're only interested in NEWLINK messages */
474 type = nlmsg->nlmsg_type;
475 if (!(type == RTM_NEWLINK || type == RTM_DELLINK))
478 info = NLMSG_DATA(nlmsg);
480 have_ifaddr = have_ifname = false;
483 attrlen = nlmsg->nlmsg_len - sizeof(*info);
485 /* extract the interface name and hardware address attributes */
486 for_each_rta(info + 1, attr, attrlen) {
487 void *data = RTA_DATA(attr);
489 switch (attr->rta_type) {
491 memcpy(ifaddr, data, sizeof(ifaddr));
496 strncpy(ifname, data, IFNAMSIZ);
501 mtu = *(unsigned int *)data;
506 if (!have_ifaddr || !have_ifname)
509 if (type == RTM_DELLINK || mtu == 0) {
510 interface = find_interface_by_ifindex(network, info->ifi_index);
513 pb_log("network: interface %s removed\n", interface->name);
514 remove_interface(network, interface);
518 /* ignore the default tun device in some environments */
519 if (strncmp(ifname, "tun", strlen("tun")) == 0)
522 interface = find_interface_by_ifindex(network, info->ifi_index);
524 interface = talloc_zero(network, struct interface);
525 interface->ifindex = info->ifi_index;
526 interface->state = IFSTATE_NEW;
527 memcpy(interface->hwaddr, ifaddr, sizeof(interface->hwaddr));
528 strncpy(interface->name, ifname, sizeof(interface->name) - 1);
529 add_interface(network, interface);
532 /* A repeated RTM_NEWLINK can represent an interface name change */
533 if (strncmp(interface->name, ifname, IFNAMSIZ)) {
534 pb_debug("ifname update: %s -> %s\n", interface->name, ifname);
535 strncpy(interface->name, ifname, sizeof(interface->name) - 1);
536 talloc_free(interface->dev->device->id);
537 interface->dev->device->id =
538 talloc_strdup(interface->dev->device, ifname);
541 /* notify the sysinfo code about changes to this interface */
542 if (strcmp(interface->name, "lo"))
543 system_info_register_interface(
544 sizeof(interface->hwaddr),
545 interface->hwaddr, interface->name,
546 info->ifi_flags & IFF_LOWER_UP);
548 configure_interface(network, interface,
549 info->ifi_flags & IFF_UP,
550 info->ifi_flags & IFF_LOWER_UP);
555 static int network_netlink_process(void *arg)
557 struct network *network = arg;
558 struct nlmsghdr *nlmsg;
564 memset(&msg, 0, sizeof(msg));
571 iov.iov_len = network->netlink_buf_size;
572 iov.iov_base = network->netlink_buf;
574 rc = recvmsg(network->netlink_sd, &msg, flags);
577 perror("netlink recv header");
583 /* if the netlink message was larger than our buffer, realloc
584 * before reading again */
585 if (len > network->netlink_buf_size || msg.msg_flags & MSG_TRUNC) {
586 network->netlink_buf_size *= 2;
587 network->netlink_buf = talloc_realloc(network,
588 network->netlink_buf,
590 network->netlink_buf_size);
594 /* otherwise, we're good to read the entire message without PEEK */
595 if (flags == MSG_PEEK) {
600 for_each_nlmsg(network->netlink_buf, nlmsg, len)
601 network_handle_nlmsg(network, nlmsg);
606 static void network_init_dns(struct network *network)
608 const struct config *config;
614 if (network->dry_run)
617 config = config_get();
618 if (!config || !config->network.n_dns_servers)
621 rc = read_file(network, "/etc/resolv.conf", &buf, &len);
624 buf = talloc_strdup(network, "");
630 for (i = 0; i < config->network.n_dns_servers; i++) {
634 dns_conf = talloc_asprintf(network, "nameserver %s\n",
635 config->network.dns_servers[i]);
637 if (strstr(buf, dns_conf)) {
638 talloc_free(dns_conf);
642 dns_conf_len = strlen(dns_conf);
643 buf = talloc_realloc(network, buf, char, len + dns_conf_len + 1);
644 memcpy(buf + len, dns_conf, dns_conf_len);
649 talloc_free(dns_conf);
653 rc = replace_file("/etc/resolv.conf", buf, len);
655 pb_log("error replacing resolv.conf: %s\n",
662 struct network *network_init(struct device_handler *handler,
663 struct waitset *waitset, bool dry_run)
665 struct network *network;
668 network = talloc(handler, struct network);
669 list_init(&network->interfaces);
670 network->handler = handler;
671 network->dry_run = dry_run;
672 network->manual_config = config_get()->network.n_interfaces != 0;
674 network_init_dns(network);
676 rc = network_init_netlink(network);
680 network->waiter = waiter_register_io(waitset, network->netlink_sd,
681 WAIT_IN, network_netlink_process, network);
683 if (!network->waiter)
686 rc = network_send_link_query(network);
693 network_shutdown(network);
697 int network_shutdown(struct network *network)
699 struct interface *interface;
702 waiter_remove(network->waiter);
704 list_for_each_entry(&network->interfaces, interface, list) {
705 if (interface->state == IFSTATE_IGNORED)
707 if (!strcmp(interface->name, "lo"))
709 interface_down(interface);
712 close(network->netlink_sd);
713 talloc_free(network);