7 #include <sys/socket.h>
9 #include <linux/netlink.h>
10 #include <linux/rtnetlink.h>
13 #include <list/list.h>
14 #include <types/types.h>
15 #include <talloc/talloc.h>
16 #include <waiter/waiter.h>
17 #include <process/process.h>
18 #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 static int network_init_netlink(struct network *network)
114 struct sockaddr_nl addr;
117 memset(&addr, 0, sizeof(addr));
118 addr.nl_family = AF_NETLINK;
119 addr.nl_groups = RTMGRP_LINK;
121 network->netlink_sd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
122 if (network->netlink_sd < 0) {
123 perror("socket(AF_NETLINK)");
127 rc = bind(network->netlink_sd, (struct sockaddr *)&addr, sizeof(addr));
129 perror("bind(sockaddr_nl)");
130 close(network->netlink_sd);
134 network->netlink_buf_size = INITIAL_BUFSIZE;
135 network->netlink_buf = talloc_array(network, char,
136 network->netlink_buf_size);
141 static int network_send_link_query(struct network *network)
145 struct nlmsghdr nlmsg;
146 struct rtgenmsg rtmsg;
149 memset(&msg, 0, sizeof(msg));
151 msg.nlmsg.nlmsg_len = sizeof(msg);
152 msg.nlmsg.nlmsg_type = RTM_GETLINK;
153 msg.nlmsg.nlmsg_flags = NLM_F_REQUEST | NLM_F_ROOT;
154 msg.nlmsg.nlmsg_seq = 0;
155 msg.nlmsg.nlmsg_pid = 0;
156 msg.rtmsg.rtgen_family = AF_UNSPEC;
158 rc = send(network->netlink_sd, &msg, sizeof(msg), MSG_NOSIGNAL);
159 if (rc != sizeof(msg))
165 static char *mac_bytes_to_string(void *ctx, uint8_t *addr, int len)
167 const int l = strlen("xx:");
172 return talloc_strdup(ctx, "");
174 buf = talloc_array(ctx, char, (len * l) + 1);
176 for (i = 0; i < len; i++)
177 sprintf(buf + (l * i), "%02x:", addr[i]);
179 *(buf + (l * len) - 1) = '\0';
184 static void add_interface(struct network *network,
185 struct interface *interface)
187 list_add(&network->interfaces, &interface->list);
188 interface->dev = discover_device_create(network->handler,
190 interface->dev->device->type = DEVICE_TYPE_NETWORK;
191 interface->dev->uuid = mac_bytes_to_string(interface->dev,
192 interface->hwaddr, sizeof(interface->hwaddr));
193 device_handler_add_device(network->handler, interface->dev);
196 static void remove_interface(struct network *network,
197 struct interface *interface)
200 device_handler_remove(network->handler, interface->dev);
201 list_remove(&interface->list);
202 talloc_free(interface);
205 void network_register_device(struct network *network,
206 struct discover_device *dev)
208 struct interface *iface;
210 iface = find_interface_by_name(network, dev->device->id);
215 dev->uuid = mac_bytes_to_string(iface->dev, iface->hwaddr,
216 sizeof(iface->hwaddr));
219 void network_unregister_device(struct network *network,
220 struct discover_device *dev)
222 struct interface *iface;
224 iface = find_interface_by_name(network, dev->device->id);
231 static int interface_change(struct interface *interface, bool up)
233 const char *statestr = up ? "up" : "down";
236 if (!up && interface->udhcpc_process) {
237 /* we don't care about the callback from here */
238 interface->udhcpc_process->exit_cb = NULL;
239 interface->udhcpc_process->data = NULL;
240 process_stop_async(interface->udhcpc_process);
241 process_release(interface->udhcpc_process);
245 rc = process_run_simple(interface, pb_system_apps.ip,
246 "address", "flush", "dev", interface->name,
249 pb_log("failed to flush addresses from interface %s\n",
253 rc = process_run_simple(interface, pb_system_apps.ip,
254 "link", "set", interface->name, statestr, NULL);
256 pb_log("failed to bring interface %s %s\n", interface->name,
263 static int interface_up(struct interface *interface)
265 return interface_change(interface, true);
268 static int interface_down(struct interface *interface)
270 return interface_change(interface, false);
273 static void udhcpc_process_exit(struct process *process)
275 struct interface *interface = process->data;
276 pb_debug("udhcp client [pid %d] for interface %s exited, rc %d\n",
277 process->pid, interface->name, process->exit_status);
278 interface->udhcpc_process = NULL;
279 process_release(process);
282 static void configure_interface_dhcp(struct interface *interface)
284 const struct platform *platform;
285 char pidfile[256], id[10];
286 struct process *process;
288 const char *argv[] = {
289 pb_system_apps.udhcpc,
293 "-O", "pxepathprefix",
295 "-i", interface->name,
296 "-x", id, /* [11,12] - dhcp client identifier */
300 snprintf(pidfile, sizeof(pidfile), "%s/udhcpc-%s.pid",
301 PIDFILE_BASE, interface->name);
303 platform = platform_get();
304 if (platform && platform->dhcp_arch_id != 0xffff)
305 snprintf(id, sizeof(id), "0x5d:%04x", platform->dhcp_arch_id);
309 process = process_create(interface);
311 process->path = pb_system_apps.udhcpc;
312 process->argv = argv;
313 process->exit_cb = udhcpc_process_exit;
314 process->data = interface;
316 rc = process_run_async(process);
319 process_release(process);
321 interface->udhcpc_process = process;
326 static void configure_interface_static(struct interface *interface,
327 const struct interface_config *config)
331 rc = process_run_simple(interface, pb_system_apps.ip,
332 "address", "add", config->static_config.address,
333 "dev", interface->name, NULL);
337 pb_log("failed to add address %s to interface %s\n",
338 config->static_config.address,
343 /* we need the interface up before we can route through it */
344 rc = interface_up(interface);
348 if (config->static_config.gateway)
349 rc = process_run_simple(interface, pb_system_apps.ip,
350 "route", "add", "default",
351 "via", config->static_config.gateway,
355 pb_log("failed to add default route %s on interface %s\n",
356 config->static_config.gateway,
363 static void configure_interface(struct network *network,
364 struct interface *interface, bool up, bool link)
366 const struct interface_config *config = NULL;
368 if (interface->state == IFSTATE_IGNORED)
371 /* old interface? check that we're still up and running */
372 if (interface->state == IFSTATE_CONFIGURED) {
374 interface->state = IFSTATE_NEW;
376 interface->state = IFSTATE_UP_WAITING_LINK;
381 /* always up the lookback, no other handling required */
382 if (!strcmp(interface->name, "lo")) {
383 if (interface->state == IFSTATE_NEW)
384 interface_up(interface);
385 interface->state = IFSTATE_CONFIGURED;
389 config = find_config_by_hwaddr(interface->hwaddr);
390 if (config && config->ignore) {
391 pb_log("network: ignoring interface %s\n", interface->name);
392 interface->state = IFSTATE_IGNORED;
396 /* if we're in manual config mode, we need an interface configuration */
397 if (network->manual_config && !config) {
398 interface->state = IFSTATE_IGNORED;
399 pb_log("network: skipping %s: manual config mode, "
400 "but no config for this interface\n",
405 /* new interface? bring up to the point so we can detect a link */
406 if (interface->state == IFSTATE_NEW) {
408 interface_up(interface);
409 pb_log("network: bringing up interface %s\n",
414 interface->state = IFSTATE_UP_WAITING_LINK;
418 /* no link? wait for a notification */
419 if (interface->state == IFSTATE_UP_WAITING_LINK && !link)
422 pb_log("network: configuring interface %s\n", interface->name);
424 if (!config || config->method == CONFIG_METHOD_DHCP) {
425 configure_interface_dhcp(interface);
427 } else if (config->method == CONFIG_METHOD_STATIC) {
428 configure_interface_static(interface, config);
432 static int network_handle_nlmsg(struct network *network, struct nlmsghdr *nlmsg)
434 bool have_ifaddr, have_ifname;
435 struct interface *interface;
436 struct ifinfomsg *info;
440 char ifname[IFNAMSIZ+1];
444 /* we're only interested in NEWLINK messages */
445 type = nlmsg->nlmsg_type;
446 if (!(type == RTM_NEWLINK || type == RTM_DELLINK))
449 info = NLMSG_DATA(nlmsg);
451 have_ifaddr = have_ifname = false;
454 attrlen = nlmsg->nlmsg_len - sizeof(*info);
456 /* extract the interface name and hardware address attributes */
457 for_each_rta(info + 1, attr, attrlen) {
458 void *data = RTA_DATA(attr);
460 switch (attr->rta_type) {
462 memcpy(ifaddr, data, sizeof(ifaddr));
467 strncpy(ifname, data, IFNAMSIZ);
472 mtu = *(unsigned int *)data;
477 if (!have_ifaddr || !have_ifname)
480 if (type == RTM_DELLINK || mtu == 0) {
481 interface = find_interface_by_ifindex(network, info->ifi_index);
484 pb_log("network: interface %s removed\n", interface->name);
485 remove_interface(network, interface);
490 interface = find_interface_by_ifindex(network, info->ifi_index);
492 interface = talloc_zero(network, struct interface);
493 interface->ifindex = info->ifi_index;
494 interface->state = IFSTATE_NEW;
495 memcpy(interface->hwaddr, ifaddr, sizeof(interface->hwaddr));
496 strncpy(interface->name, ifname, sizeof(interface->name) - 1);
497 add_interface(network, interface);
500 /* notify the sysinfo code about changes to this interface */
501 if (strcmp(interface->name, "lo"))
502 system_info_register_interface(
503 sizeof(interface->hwaddr),
504 interface->hwaddr, interface->name,
505 info->ifi_flags & IFF_LOWER_UP);
507 configure_interface(network, interface,
508 info->ifi_flags & IFF_UP,
509 info->ifi_flags & IFF_LOWER_UP);
514 static int network_netlink_process(void *arg)
516 struct network *network = arg;
517 struct nlmsghdr *nlmsg;
523 memset(&msg, 0, sizeof(msg));
530 iov.iov_len = network->netlink_buf_size;
531 iov.iov_base = network->netlink_buf;
533 rc = recvmsg(network->netlink_sd, &msg, flags);
536 perror("netlink recv header");
542 /* if the netlink message was larger than our buffer, realloc
543 * before reading again */
544 if (len > network->netlink_buf_size || msg.msg_flags & MSG_TRUNC) {
545 network->netlink_buf_size *= 2;
546 network->netlink_buf = talloc_realloc(network,
547 network->netlink_buf,
549 network->netlink_buf_size);
553 /* otherwise, we're good to read the entire message without PEEK */
554 if (flags == MSG_PEEK) {
559 for_each_nlmsg(network->netlink_buf, nlmsg, len)
560 network_handle_nlmsg(network, nlmsg);
565 static void network_init_dns(struct network *network)
567 const struct config *config;
573 if (network->dry_run)
576 config = config_get();
577 if (!config || !config->network.n_dns_servers)
580 rc = read_file(network, "/etc/resolv.conf", &buf, &len);
583 buf = talloc_strdup(network, "");
589 for (i = 0; i < config->network.n_dns_servers; i++) {
593 dns_conf = talloc_asprintf(network, "nameserver %s\n",
594 config->network.dns_servers[i]);
596 if (strstr(buf, dns_conf)) {
597 talloc_free(dns_conf);
601 dns_conf_len = strlen(dns_conf);
602 buf = talloc_realloc(network, buf, char, len + dns_conf_len + 1);
603 memcpy(buf + len, dns_conf, dns_conf_len);
608 talloc_free(dns_conf);
612 rc = replace_file("/etc/resolv.conf", buf, len);
614 pb_log("error replacing resolv.conf: %s\n",
621 struct network *network_init(struct device_handler *handler,
622 struct waitset *waitset, bool dry_run)
624 struct network *network;
627 network = talloc(handler, struct network);
628 list_init(&network->interfaces);
629 network->handler = handler;
630 network->dry_run = dry_run;
631 network->manual_config = config_get()->network.n_interfaces != 0;
633 network_init_dns(network);
635 rc = network_init_netlink(network);
639 network->waiter = waiter_register_io(waitset, network->netlink_sd,
640 WAIT_IN, network_netlink_process, network);
642 if (!network->waiter)
645 rc = network_send_link_query(network);
652 network_shutdown(network);
656 int network_shutdown(struct network *network)
658 struct interface *interface;
661 waiter_remove(network->waiter);
663 list_for_each_entry(&network->interfaces, interface, list) {
664 if (interface->state == IFSTATE_IGNORED)
666 if (!strcmp(interface->name, "lo"))
668 interface_down(interface);
671 close(network->netlink_sd);
672 talloc_free(network);