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 <pb-config/pb-config.h>
18 #include <process/process.h>
19 #include <system/system.h>
23 #include "device-handler.h"
26 #define PIDFILE_BASE (LOCAL_STATE_DIR "/petitboot/")
28 #define for_each_nlmsg(buf, nlmsg, len) \
29 for (nlmsg = (struct nlmsghdr *)buf; \
30 NLMSG_OK(nlmsg, len) && nlmsg->nlmsg_type != NLMSG_DONE; \
31 nlmsg = NLMSG_NEXT(nlmsg, len))
33 #define for_each_rta(buf, rta, attrlen) \
34 for (rta = (struct rtattr *)(buf); RTA_OK(rta, attrlen); \
35 rta = RTA_NEXT(rta, attrlen))
41 uint8_t hwaddr[HWADDR_SIZE];
45 IFSTATE_UP_WAITING_LINK,
50 struct list_item list;
51 struct process *udhcpc_process;
52 struct discover_device *dev;
56 struct list interfaces;
57 struct device_handler *handler;
58 struct waiter *waiter;
64 static const struct interface_config *find_config_by_hwaddr(
67 const struct config *config;
70 config = config_get();
74 for (i = 0; i < config->network.n_interfaces; i++) {
75 struct interface_config *ifconf = config->network.interfaces[i];
77 if (!memcmp(ifconf->hwaddr, hwaddr, HWADDR_SIZE))
84 static struct interface *find_interface_by_ifindex(struct network *network,
87 struct interface *interface;
89 list_for_each_entry(&network->interfaces, interface, list)
90 if (interface->ifindex == ifindex)
96 static int network_init_netlink(struct network *network)
98 struct sockaddr_nl addr;
101 memset(&addr, 0, sizeof(addr));
102 addr.nl_family = AF_NETLINK;
103 addr.nl_groups = RTMGRP_LINK;
105 network->netlink_sd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
106 if (network->netlink_sd < 0) {
107 perror("socket(AF_NETLINK)");
111 rc = bind(network->netlink_sd, (struct sockaddr *)&addr, sizeof(addr));
113 perror("bind(sockaddr_nl)");
114 close(network->netlink_sd);
121 static int network_send_link_query(struct network *network)
125 struct nlmsghdr nlmsg;
126 struct rtgenmsg rtmsg;
129 memset(&msg, 0, sizeof(msg));
131 msg.nlmsg.nlmsg_len = sizeof(msg);
132 msg.nlmsg.nlmsg_type = RTM_GETLINK;
133 msg.nlmsg.nlmsg_flags = NLM_F_REQUEST | NLM_F_ROOT;
134 msg.nlmsg.nlmsg_seq = 0;
135 msg.nlmsg.nlmsg_pid = 0;
136 msg.rtmsg.rtgen_family = AF_UNSPEC;
138 rc = send(network->netlink_sd, &msg, sizeof(msg), MSG_NOSIGNAL);
139 if (rc != sizeof(msg))
145 static void add_interface(struct network *network,
146 struct interface *interface)
148 list_add(&network->interfaces, &interface->list);
149 interface->dev = discover_device_create(network->handler,
151 interface->dev->device->type = DEVICE_TYPE_NETWORK;
152 device_handler_add_device(network->handler, interface->dev);
155 static void remove_interface(struct network *network,
156 struct interface *interface)
158 device_handler_remove(network->handler, interface->dev);
159 list_remove(&interface->list);
160 talloc_free(interface);
163 static int interface_change(struct interface *interface, bool up)
165 const char *statestr = up ? "up" : "down";
168 if (!up && interface->udhcpc_process) {
169 /* we don't care about the callback from here */
170 interface->udhcpc_process->exit_cb = NULL;
171 interface->udhcpc_process->data = NULL;
172 process_stop_async(interface->udhcpc_process);
173 process_release(interface->udhcpc_process);
176 rc = process_run_simple(interface, pb_system_apps.ip,
177 "link", "set", interface->name, statestr, NULL);
179 pb_log("failed to bring interface %s %s\n", interface->name,
186 static int interface_up(struct interface *interface)
188 return interface_change(interface, true);
191 static int interface_down(struct interface *interface)
193 return interface_change(interface, false);
196 static void udhcpc_process_exit(struct process *process)
198 struct interface *interface = process->data;
199 pb_log("udhcp client [pid %d] for interface %s exited, rc %d\n",
200 process->pid, interface->name, process->exit_status);
201 interface->udhcpc_process = NULL;
202 process_release(process);
205 static void configure_interface_dhcp(struct interface *interface)
207 struct process *process;
210 const char *argv[] = {
211 pb_system_apps.udhcpc,
215 "-i", interface->name,
218 snprintf(pidfile, sizeof(pidfile), "%s/udhcpc-%s.pid",
219 PIDFILE_BASE, interface->name);
221 process = process_create(interface);
223 process->path = pb_system_apps.udhcpc;
224 process->argv = argv;
225 process->exit_cb = udhcpc_process_exit;
226 process->data = interface;
228 rc = process_run_async(process);
231 process_release(process);
233 interface->udhcpc_process = process;
238 static void configure_interface_static(struct interface *interface,
239 const struct interface_config *config)
243 rc = process_run_simple(interface, pb_system_apps.ip,
244 "address", "add", config->static_config.address,
245 "dev", interface->name, NULL);
249 pb_log("failed to add address %s to interface %s\n",
250 config->static_config.address,
255 /* we need the interface up before we can route through it */
256 rc = interface_up(interface);
260 if (config->static_config.gateway)
261 rc = process_run_simple(interface, pb_system_apps.ip,
262 "route", "add", "default",
263 "via", config->static_config.gateway,
267 pb_log("failed to add default route %s on interface %s\n",
268 config->static_config.gateway,
275 static void configure_interface(struct network *network,
276 struct interface *interface, bool up, bool link)
278 const struct interface_config *config = NULL;
280 if (interface->state == IFSTATE_IGNORED)
283 /* old interface? check that we're still up and running */
284 if (interface->state == IFSTATE_CONFIGURED) {
286 interface->state = IFSTATE_NEW;
288 interface->state = IFSTATE_UP_WAITING_LINK;
293 /* always up the lookback, no other handling required */
294 if (!strcmp(interface->name, "lo")) {
295 if (interface->state == IFSTATE_NEW)
296 interface_up(interface);
297 interface->state = IFSTATE_CONFIGURED;
301 config = find_config_by_hwaddr(interface->hwaddr);
302 if (config && config->ignore) {
303 pb_log("network: ignoring interface %s\n", interface->name);
304 interface->state = IFSTATE_IGNORED;
308 /* if we're in manual config mode, we need an interface configuration */
309 if (network->manual_config && !config) {
310 interface->state = IFSTATE_IGNORED;
311 pb_log("network: skipping %s: manual config mode, "
312 "but no config for this interface\n",
317 /* new interface? bring up to the point so we can detect a link */
318 if (interface->state == IFSTATE_NEW) {
320 interface_up(interface);
321 pb_log("network: bringing up interface %s\n",
326 interface->state = IFSTATE_UP_WAITING_LINK;
330 /* no link? wait for a notification */
331 if (interface->state == IFSTATE_UP_WAITING_LINK && !link)
334 pb_log("network: configuring interface %s\n", interface->name);
336 if (!config || config->method == CONFIG_METHOD_DHCP) {
337 configure_interface_dhcp(interface);
339 } else if (config->method == CONFIG_METHOD_STATIC) {
340 configure_interface_static(interface, config);
344 static int network_handle_nlmsg(struct network *network, struct nlmsghdr *nlmsg)
346 bool have_ifaddr, have_ifname;
347 struct interface *interface;
348 struct ifinfomsg *info;
352 char ifname[IFNAMSIZ+1];
356 /* we're only interested in NEWLINK messages */
357 type = nlmsg->nlmsg_type;
358 if (!(type == RTM_NEWLINK || type == RTM_DELLINK))
361 info = NLMSG_DATA(nlmsg);
363 have_ifaddr = have_ifname = false;
365 attrlen = nlmsg->nlmsg_len - sizeof(*info);
367 /* extract the interface name and hardware address attributes */
368 for_each_rta(info + 1, attr, attrlen) {
369 void *data = RTA_DATA(attr);
371 switch (attr->rta_type) {
373 memcpy(ifaddr, data, sizeof(ifaddr));
378 strncpy(ifname, data, IFNAMSIZ);
383 mtu = *(unsigned int *)data;
388 if (!have_ifaddr || !have_ifname)
391 if (type == RTM_DELLINK || mtu == 0) {
392 interface = find_interface_by_ifindex(network, info->ifi_index);
395 pb_log("network: interface %s removed\n", interface->name);
396 remove_interface(network, interface);
401 interface = find_interface_by_ifindex(network, info->ifi_index);
403 interface = talloc_zero(network, struct interface);
404 interface->ifindex = info->ifi_index;
405 interface->state = IFSTATE_NEW;
406 memcpy(interface->hwaddr, ifaddr, sizeof(interface->hwaddr));
407 strncpy(interface->name, ifname, sizeof(interface->name) - 1);
408 add_interface(network, interface);
411 configure_interface(network, interface,
412 info->ifi_flags & IFF_UP,
413 info->ifi_flags & IFF_LOWER_UP);
418 static int network_netlink_process(void *arg)
420 struct network *network = arg;
421 struct nlmsghdr *nlmsg;
426 rc = recv(network->netlink_sd, buf, sizeof(buf), 0);
428 perror("netlink recv");
434 for_each_nlmsg(buf, nlmsg, len)
435 network_handle_nlmsg(network, nlmsg);
440 static void network_init_dns(struct network *network)
442 const struct config *config;
447 if (network->dry_run)
450 config = config_get();
451 if (!config || !config->network.n_dns_servers)
454 rc = read_file(network, "/etc/resolv.conf", &buf, &len);
457 buf = talloc_strdup(network, "");
463 for (i = 0; i < config->network.n_dns_servers; i++) {
467 dns_conf = talloc_asprintf(network, "nameserver %s\n",
468 config->network.dns_servers[i]);
470 if (strstr(buf, dns_conf)) {
471 talloc_free(dns_conf);
475 dns_conf_len = strlen(dns_conf);
476 buf = talloc_realloc(network, buf, char, len + dns_conf_len);
477 memcpy(buf + len, dns_conf, dns_conf_len);
481 talloc_free(dns_conf);
485 rc = replace_file("/etc/resolv.conf", buf, len);
487 pb_log("error replacing resolv.conf: %s\n",
494 struct network *network_init(struct device_handler *handler,
495 struct waitset *waitset, bool dry_run)
497 struct network *network;
500 network = talloc(handler, struct network);
501 list_init(&network->interfaces);
502 network->handler = handler;
503 network->manual_config = false;
504 network->dry_run = dry_run;
506 network_init_dns(network);
508 rc = network_init_netlink(network);
512 network->waiter = waiter_register_io(waitset, network->netlink_sd,
513 WAIT_IN, network_netlink_process, network);
515 if (!network->waiter)
518 rc = network_send_link_query(network);
525 network_shutdown(network);
530 int network_shutdown(struct network *network)
532 struct interface *interface;
535 waiter_remove(network->waiter);
537 list_for_each_entry(&network->interfaces, interface, list)
538 interface_down(interface);
540 close(network->netlink_sd);
541 talloc_free(network);