1801710773b975106e43d6592de4a85d8ad55322
[petitboot] / discover / network.c
1
2 #include <stdbool.h>
3 #include <stdint.h>
4 #include <string.h>
5 #include <stdlib.h>
6 #include <errno.h>
7 #include <sys/socket.h>
8 #include <linux/if.h>
9 #include <linux/netlink.h>
10 #include <linux/rtnetlink.h>
11
12 #include <log/log.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>
19
20 #include "file.h"
21 #include "network.h"
22 #include "sysinfo.h"
23 #include "platform.h"
24 #include "device-handler.h"
25
26 #define HWADDR_SIZE     6
27 #define PIDFILE_BASE    (LOCAL_STATE_DIR "/petitboot/")
28 #define INITIAL_BUFSIZE 4096
29
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))
34
35 #define for_each_rta(buf, rta, attrlen) \
36         for (rta = (struct rtattr *)(buf); RTA_OK(rta, attrlen); \
37                         rta = RTA_NEXT(rta, attrlen))
38
39
40 struct interface {
41         int     ifindex;
42         char    name[IFNAMSIZ];
43         uint8_t hwaddr[HWADDR_SIZE];
44
45         enum {
46                 IFSTATE_NEW,
47                 IFSTATE_UP_WAITING_LINK,
48                 IFSTATE_CONFIGURED,
49                 IFSTATE_IGNORED,
50         } state;
51
52         struct list_item list;
53         struct process *udhcpc_process;
54         struct discover_device *dev;
55 };
56
57 struct network {
58         struct list             interfaces;
59         struct device_handler   *handler;
60         struct waiter           *waiter;
61         int                     netlink_sd;
62         void                    *netlink_buf;
63         unsigned int            netlink_buf_size;
64         bool                    manual_config;
65         bool                    dry_run;
66 };
67
68 static const struct interface_config *find_config_by_hwaddr(
69                 uint8_t *hwaddr)
70 {
71         const struct config *config;
72         unsigned int i;
73
74         config = config_get();
75         if (!config)
76                 return NULL;
77
78         for (i = 0; i < config->network.n_interfaces; i++) {
79                 struct interface_config *ifconf = config->network.interfaces[i];
80
81                 if (!memcmp(ifconf->hwaddr, hwaddr, HWADDR_SIZE))
82                         return ifconf;
83         }
84
85         return NULL;
86 }
87
88 static struct interface *find_interface_by_ifindex(struct network *network,
89                 int ifindex)
90 {
91         struct interface *interface;
92
93         list_for_each_entry(&network->interfaces, interface, list)
94                 if (interface->ifindex == ifindex)
95                         return interface;
96
97         return NULL;
98 }
99
100 static int network_init_netlink(struct network *network)
101 {
102         struct sockaddr_nl addr;
103         int rc;
104
105         memset(&addr, 0, sizeof(addr));
106         addr.nl_family = AF_NETLINK;
107         addr.nl_groups = RTMGRP_LINK;
108
109         network->netlink_sd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
110         if (network->netlink_sd < 0) {
111                 perror("socket(AF_NETLINK)");
112                 return -1;
113         }
114
115         rc = bind(network->netlink_sd, (struct sockaddr *)&addr, sizeof(addr));
116         if (rc) {
117                 perror("bind(sockaddr_nl)");
118                 close(network->netlink_sd);
119                 return -1;
120         }
121
122         network->netlink_buf_size = INITIAL_BUFSIZE;
123         network->netlink_buf = talloc_array(network, char,
124                                 network->netlink_buf_size);
125
126         return 0;
127 }
128
129 static int network_send_link_query(struct network *network)
130 {
131         int rc;
132         struct {
133                 struct nlmsghdr nlmsg;
134                 struct rtgenmsg rtmsg;
135         } msg;
136
137         memset(&msg, 0, sizeof(msg));
138
139         msg.nlmsg.nlmsg_len = sizeof(msg);
140         msg.nlmsg.nlmsg_type = RTM_GETLINK;
141         msg.nlmsg.nlmsg_flags = NLM_F_REQUEST | NLM_F_ROOT;
142         msg.nlmsg.nlmsg_seq = 0;
143         msg.nlmsg.nlmsg_pid = 0;
144         msg.rtmsg.rtgen_family = AF_UNSPEC;
145
146         rc = send(network->netlink_sd, &msg, sizeof(msg), MSG_NOSIGNAL);
147         if (rc != sizeof(msg))
148                 return -1;
149
150         return 0;
151 }
152
153 static void add_interface(struct network *network,
154                 struct interface *interface)
155 {
156         list_add(&network->interfaces, &interface->list);
157         interface->dev = discover_device_create(network->handler,
158                                         interface->name);
159         interface->dev->device->type = DEVICE_TYPE_NETWORK;
160         device_handler_add_device(network->handler, interface->dev);
161 }
162
163 static void remove_interface(struct network *network,
164                 struct interface *interface)
165 {
166         device_handler_remove(network->handler, interface->dev);
167         list_remove(&interface->list);
168         talloc_free(interface);
169 }
170
171 static int interface_change(struct interface *interface, bool up)
172 {
173         const char *statestr = up ? "up" : "down";
174         int rc;
175
176         if (!up && interface->udhcpc_process) {
177                 /* we don't care about the callback from here */
178                 interface->udhcpc_process->exit_cb = NULL;
179                 interface->udhcpc_process->data = NULL;
180                 process_stop_async(interface->udhcpc_process);
181                 process_release(interface->udhcpc_process);
182         }
183
184         if (!up) {
185                 rc = process_run_simple(interface, pb_system_apps.ip,
186                                 "address", "flush", "dev", interface->name,
187                                 NULL);
188                 if (rc)
189                         pb_log("failed to flush addresses from interface %s\n",
190                                 interface->name);
191         }
192
193         rc = process_run_simple(interface, pb_system_apps.ip,
194                         "link", "set", interface->name, statestr, NULL);
195         if (rc) {
196                 pb_log("failed to bring interface %s %s\n", interface->name,
197                                 statestr);
198                 return -1;
199         }
200         return 0;
201 }
202
203 static int interface_up(struct interface *interface)
204 {
205         return interface_change(interface, true);
206 }
207
208 static int interface_down(struct interface *interface)
209 {
210         return interface_change(interface, false);
211 }
212
213 static void udhcpc_process_exit(struct process *process)
214 {
215         struct interface *interface = process->data;
216         pb_debug("udhcp client [pid %d] for interface %s exited, rc %d\n",
217                         process->pid, interface->name, process->exit_status);
218         interface->udhcpc_process = NULL;
219         process_release(process);
220 }
221
222 static void configure_interface_dhcp(struct interface *interface)
223 {
224         const struct platform *platform;
225         char pidfile[256], id[10];
226         struct process *process;
227         int rc;
228         const char *argv[] = {
229                 pb_system_apps.udhcpc,
230                 "-R",
231                 "-f",
232                 "-O", "pxeconffile",
233                 "-O", "pxepathprefix",
234                 "-p", pidfile,
235                 "-i", interface->name,
236                 "-x", id, /* [11,12] - dhcp client identifier */
237                 NULL,
238         };
239
240         snprintf(pidfile, sizeof(pidfile), "%s/udhcpc-%s.pid",
241                         PIDFILE_BASE, interface->name);
242
243         platform = platform_get();
244         if (platform && platform->dhcp_arch_id != 0xffff)
245                 snprintf(id, sizeof(id), "0x5d:%04x", platform->dhcp_arch_id);
246         else
247                 argv[11] = NULL;
248
249         process = process_create(interface);
250
251         process->path = pb_system_apps.udhcpc;
252         process->argv = argv;
253         process->exit_cb = udhcpc_process_exit;
254         process->data = interface;
255
256         rc = process_run_async(process);
257
258         if (rc)
259                 process_release(process);
260         else
261                 interface->udhcpc_process = process;
262
263         return;
264 }
265
266 static void configure_interface_static(struct interface *interface,
267                 const struct interface_config *config)
268 {
269         int rc;
270
271         rc = process_run_simple(interface, pb_system_apps.ip,
272                         "address", "add", config->static_config.address,
273                         "dev", interface->name, NULL);
274
275
276         if (rc) {
277                 pb_log("failed to add address %s to interface %s\n",
278                                 config->static_config.address,
279                                 interface->name);
280                 return;
281         }
282
283         /* we need the interface up before we can route through it */
284         rc = interface_up(interface);
285         if (rc)
286                 return;
287
288         if (config->static_config.gateway)
289                 rc = process_run_simple(interface, pb_system_apps.ip,
290                                 "route", "add", "default",
291                                 "via", config->static_config.gateway,
292                                 NULL);
293
294         if (rc) {
295                 pb_log("failed to add default route %s on interface %s\n",
296                                 config->static_config.gateway,
297                                 interface->name);
298         }
299
300         return;
301 }
302
303 static void configure_interface(struct network *network,
304                 struct interface *interface, bool up, bool link)
305 {
306         const struct interface_config *config = NULL;
307
308         if (interface->state == IFSTATE_IGNORED)
309                 return;
310
311         /* old interface? check that we're still up and running */
312         if (interface->state == IFSTATE_CONFIGURED) {
313                 if (!up)
314                         interface->state = IFSTATE_NEW;
315                 else if (!link)
316                         interface->state = IFSTATE_UP_WAITING_LINK;
317                 else
318                         return;
319         }
320
321         /* always up the lookback, no other handling required */
322         if (!strcmp(interface->name, "lo")) {
323                 if (interface->state == IFSTATE_NEW)
324                         interface_up(interface);
325                 interface->state = IFSTATE_CONFIGURED;
326                 return;
327         }
328
329         config = find_config_by_hwaddr(interface->hwaddr);
330         if (config && config->ignore) {
331                 pb_log("network: ignoring interface %s\n", interface->name);
332                 interface->state = IFSTATE_IGNORED;
333                 return;
334         }
335
336         /* if we're in manual config mode, we need an interface configuration */
337         if (network->manual_config && !config) {
338                 interface->state = IFSTATE_IGNORED;
339                 pb_log("network: skipping %s: manual config mode, "
340                                 "but no config for this interface\n",
341                                 interface->name);
342                 return;
343         }
344
345         /* new interface? bring up to the point so we can detect a link */
346         if (interface->state == IFSTATE_NEW) {
347                 if (!up) {
348                         interface_up(interface);
349                         pb_log("network: bringing up interface %s\n",
350                                         interface->name);
351                         return;
352
353                 } else if (!link) {
354                         interface->state = IFSTATE_UP_WAITING_LINK;
355                 }
356         }
357
358         /* no link? wait for a notification */
359         if (interface->state == IFSTATE_UP_WAITING_LINK && !link)
360                 return;
361
362         pb_log("network: configuring interface %s\n", interface->name);
363
364         if (!config || config->method == CONFIG_METHOD_DHCP) {
365                 configure_interface_dhcp(interface);
366
367         } else if (config->method == CONFIG_METHOD_STATIC) {
368                 configure_interface_static(interface, config);
369         }
370 }
371
372 static int network_handle_nlmsg(struct network *network, struct nlmsghdr *nlmsg)
373 {
374         bool have_ifaddr, have_ifname;
375         struct interface *interface;
376         struct ifinfomsg *info;
377         struct rtattr *attr;
378         unsigned int mtu;
379         uint8_t ifaddr[6];
380         char ifname[IFNAMSIZ+1];
381         int attrlen, type;
382
383
384         /* we're only interested in NEWLINK messages */
385         type = nlmsg->nlmsg_type;
386         if (!(type == RTM_NEWLINK || type == RTM_DELLINK))
387                 return 0;
388
389         info = NLMSG_DATA(nlmsg);
390
391         have_ifaddr = have_ifname = false;
392         mtu = 1;
393
394         attrlen = nlmsg->nlmsg_len - sizeof(*info);
395
396         /* extract the interface name and hardware address attributes */
397         for_each_rta(info + 1, attr, attrlen) {
398                 void *data = RTA_DATA(attr);
399
400                 switch (attr->rta_type) {
401                 case IFLA_ADDRESS:
402                         memcpy(ifaddr, data, sizeof(ifaddr));
403                         have_ifaddr = true;
404                         break;
405
406                 case IFLA_IFNAME:
407                         strncpy(ifname, data, IFNAMSIZ);
408                         have_ifname = true;
409                         break;
410
411                 case IFLA_MTU:
412                         mtu = *(unsigned int *)data;
413                         break;
414                 }
415         }
416
417         if (!have_ifaddr || !have_ifname)
418                 return -1;
419
420         if (type == RTM_DELLINK || mtu == 0) {
421                 interface = find_interface_by_ifindex(network, info->ifi_index);
422                 if (!interface)
423                         return 0;
424                 pb_log("network: interface %s removed\n", interface->name);
425                 remove_interface(network, interface);
426                 return 0;
427         }
428
429
430         interface = find_interface_by_ifindex(network, info->ifi_index);
431         if (!interface) {
432                 interface = talloc_zero(network, struct interface);
433                 interface->ifindex = info->ifi_index;
434                 interface->state = IFSTATE_NEW;
435                 memcpy(interface->hwaddr, ifaddr, sizeof(interface->hwaddr));
436                 strncpy(interface->name, ifname, sizeof(interface->name) - 1);
437                 add_interface(network, interface);
438         }
439
440         /* notify the sysinfo code about changes to this interface */
441         if (strcmp(interface->name, "lo"))
442                 system_info_register_interface(
443                                 sizeof(interface->hwaddr),
444                                 interface->hwaddr, interface->name,
445                                 info->ifi_flags & IFF_LOWER_UP);
446
447         configure_interface(network, interface,
448                         info->ifi_flags & IFF_UP,
449                         info->ifi_flags & IFF_LOWER_UP);
450
451         return 0;
452 }
453
454 static int network_netlink_process(void *arg)
455 {
456         struct network *network = arg;
457         struct nlmsghdr *nlmsg;
458         struct msghdr msg;
459         struct iovec iov;
460         unsigned int len;
461         int rc, flags;
462
463         memset(&msg, 0, sizeof(msg));
464         msg.msg_iov = &iov;
465         msg.msg_iovlen = 1;
466
467         flags = MSG_PEEK;
468
469 retry:
470         iov.iov_len = network->netlink_buf_size;
471         iov.iov_base = network->netlink_buf;
472
473         rc = recvmsg(network->netlink_sd, &msg, flags);
474
475         if (rc < 0) {
476                 perror("netlink recv header");
477                 return -1;
478         }
479
480         len = rc;
481
482         /* if the netlink message was larger than our buffer, realloc
483          * before reading again */
484         if (len > network->netlink_buf_size || msg.msg_flags & MSG_TRUNC) {
485                 network->netlink_buf_size *= 2;
486                 network->netlink_buf = talloc_realloc(network,
487                                         network->netlink_buf,
488                                         char *,
489                                         network->netlink_buf_size);
490                 goto retry;
491         }
492
493         /* otherwise, we're good to read the entire message without PEEK */
494         if (flags == MSG_PEEK) {
495                 flags = 0;
496                 goto retry;
497         }
498
499         for_each_nlmsg(network->netlink_buf, nlmsg, len)
500                 network_handle_nlmsg(network, nlmsg);
501
502         return 0;
503 }
504
505 static void network_init_dns(struct network *network)
506 {
507         const struct config *config;
508         unsigned int i;
509         int rc, len;
510         bool modified;
511         char *buf;
512
513         if (network->dry_run)
514                 return;
515
516         config = config_get();
517         if (!config || !config->network.n_dns_servers)
518                 return;
519
520         rc = read_file(network, "/etc/resolv.conf", &buf, &len);
521
522         if (rc) {
523                 buf = talloc_strdup(network, "");
524                 len = 0;
525         }
526
527         modified = false;
528
529         for (i = 0; i < config->network.n_dns_servers; i++) {
530                 int dns_conf_len;
531                 char *dns_conf;
532
533                 dns_conf = talloc_asprintf(network, "nameserver %s\n",
534                                 config->network.dns_servers[i]);
535
536                 if (strstr(buf, dns_conf)) {
537                         talloc_free(dns_conf);
538                         continue;
539                 }
540
541                 dns_conf_len = strlen(dns_conf);
542                 buf = talloc_realloc(network, buf, char, len + dns_conf_len + 1);
543                 memcpy(buf + len, dns_conf, dns_conf_len);
544                 len += dns_conf_len;
545                 buf[len] = '\0';
546                 modified = true;
547
548                 talloc_free(dns_conf);
549         }
550
551         if (modified) {
552                 rc = replace_file("/etc/resolv.conf", buf, len);
553                 if (rc)
554                         pb_log("error replacing resolv.conf: %s\n",
555                                         strerror(errno));
556         }
557
558         talloc_free(buf);
559 }
560
561 struct network *network_init(struct device_handler *handler,
562                 struct waitset *waitset, bool dry_run)
563 {
564         struct network *network;
565         int rc;
566
567         network = talloc(handler, struct network);
568         list_init(&network->interfaces);
569         network->handler = handler;
570         network->dry_run = dry_run;
571         network->manual_config = config_get()->network.n_interfaces != 0;
572
573         network_init_dns(network);
574
575         rc = network_init_netlink(network);
576         if (rc)
577                 goto err;
578
579         network->waiter = waiter_register_io(waitset, network->netlink_sd,
580                         WAIT_IN, network_netlink_process, network);
581
582         if (!network->waiter)
583                 goto err;
584
585         rc = network_send_link_query(network);
586         if (rc)
587                 goto err;
588
589         return network;
590
591 err:
592         network_shutdown(network);
593         return NULL;
594 }
595
596 int network_shutdown(struct network *network)
597 {
598         struct interface *interface;
599
600         if (network->waiter)
601                 waiter_remove(network->waiter);
602
603         list_for_each_entry(&network->interfaces, interface, list) {
604                 if (interface->state == IFSTATE_IGNORED)
605                         continue;
606                 if (!strcmp(interface->name, "lo"))
607                         continue;
608                 interface_down(interface);
609         }
610
611         close(network->netlink_sd);
612         talloc_free(network);
613         return 0;
614 }