utils/pb-udhcpc: Fix incorrect reference to $interface
[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 <pb-config/pb-config.h>
18 #include <process/process.h>
19 #include <system/system.h>
20
21 #include "file.h"
22 #include "network.h"
23 #include "sysinfo.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         rc = process_run_simple(interface, pb_system_apps.ip,
185                         "link", "set", interface->name, statestr, NULL);
186         if (rc) {
187                 pb_log("failed to bring interface %s %s\n", interface->name,
188                                 statestr);
189                 return -1;
190         }
191         return 0;
192 }
193
194 static int interface_up(struct interface *interface)
195 {
196         return interface_change(interface, true);
197 }
198
199 static int interface_down(struct interface *interface)
200 {
201         return interface_change(interface, false);
202 }
203
204 static void udhcpc_process_exit(struct process *process)
205 {
206         struct interface *interface = process->data;
207         pb_debug("udhcp client [pid %d] for interface %s exited, rc %d\n",
208                         process->pid, interface->name, process->exit_status);
209         interface->udhcpc_process = NULL;
210         process_release(process);
211 }
212
213 static void configure_interface_dhcp(struct interface *interface)
214 {
215         struct process *process;
216         char pidfile[256];
217         int rc;
218         const char *argv[] = {
219                 pb_system_apps.udhcpc,
220                 "-R",
221                 "-n",
222                 "-O", "pxeconffile",
223                 "-O", "pxepathprefix",
224                 "-x", "0x5d:000a",
225                 "-p", pidfile,
226                 "-i", interface->name,
227                 NULL,
228         };
229         snprintf(pidfile, sizeof(pidfile), "%s/udhcpc-%s.pid",
230                         PIDFILE_BASE, interface->name);
231
232         process = process_create(interface);
233
234         process->path = pb_system_apps.udhcpc;
235         process->argv = argv;
236         process->exit_cb = udhcpc_process_exit;
237         process->data = interface;
238
239         rc = process_run_async(process);
240
241         if (rc)
242                 process_release(process);
243         else
244                 interface->udhcpc_process = process;
245
246         return;
247 }
248
249 static void configure_interface_static(struct interface *interface,
250                 const struct interface_config *config)
251 {
252         int rc;
253
254         rc = process_run_simple(interface, pb_system_apps.ip,
255                         "address", "add", config->static_config.address,
256                         "dev", interface->name, NULL);
257
258
259         if (rc) {
260                 pb_log("failed to add address %s to interface %s\n",
261                                 config->static_config.address,
262                                 interface->name);
263                 return;
264         }
265
266         /* we need the interface up before we can route through it */
267         rc = interface_up(interface);
268         if (rc)
269                 return;
270
271         if (config->static_config.gateway)
272                 rc = process_run_simple(interface, pb_system_apps.ip,
273                                 "route", "add", "default",
274                                 "via", config->static_config.gateway,
275                                 NULL);
276
277         if (rc) {
278                 pb_log("failed to add default route %s on interface %s\n",
279                                 config->static_config.gateway,
280                                 interface->name);
281         }
282
283         return;
284 }
285
286 static void configure_interface(struct network *network,
287                 struct interface *interface, bool up, bool link)
288 {
289         const struct interface_config *config = NULL;
290
291         if (interface->state == IFSTATE_IGNORED)
292                 return;
293
294         /* old interface? check that we're still up and running */
295         if (interface->state == IFSTATE_CONFIGURED) {
296                 if (!up)
297                         interface->state = IFSTATE_NEW;
298                 else if (!link)
299                         interface->state = IFSTATE_UP_WAITING_LINK;
300                 else
301                         return;
302         }
303
304         /* always up the lookback, no other handling required */
305         if (!strcmp(interface->name, "lo")) {
306                 if (interface->state == IFSTATE_NEW)
307                         interface_up(interface);
308                 interface->state = IFSTATE_CONFIGURED;
309                 return;
310         }
311
312         config = find_config_by_hwaddr(interface->hwaddr);
313         if (config && config->ignore) {
314                 pb_log("network: ignoring interface %s\n", interface->name);
315                 interface->state = IFSTATE_IGNORED;
316                 return;
317         }
318
319         /* if we're in manual config mode, we need an interface configuration */
320         if (network->manual_config && !config) {
321                 interface->state = IFSTATE_IGNORED;
322                 pb_log("network: skipping %s: manual config mode, "
323                                 "but no config for this interface\n",
324                                 interface->name);
325                 return;
326         }
327
328         /* new interface? bring up to the point so we can detect a link */
329         if (interface->state == IFSTATE_NEW) {
330                 if (!up) {
331                         interface_up(interface);
332                         pb_log("network: bringing up interface %s\n",
333                                         interface->name);
334                         return;
335
336                 } else if (!link) {
337                         interface->state = IFSTATE_UP_WAITING_LINK;
338                 }
339         }
340
341         /* no link? wait for a notification */
342         if (interface->state == IFSTATE_UP_WAITING_LINK && !link)
343                 return;
344
345         pb_log("network: configuring interface %s\n", interface->name);
346
347         if (!config || config->method == CONFIG_METHOD_DHCP) {
348                 configure_interface_dhcp(interface);
349
350         } else if (config->method == CONFIG_METHOD_STATIC) {
351                 configure_interface_static(interface, config);
352         }
353 }
354
355 static int network_handle_nlmsg(struct network *network, struct nlmsghdr *nlmsg)
356 {
357         bool have_ifaddr, have_ifname;
358         struct interface *interface;
359         struct ifinfomsg *info;
360         struct rtattr *attr;
361         unsigned int mtu;
362         uint8_t ifaddr[6];
363         char ifname[IFNAMSIZ+1];
364         int attrlen, type;
365
366
367         /* we're only interested in NEWLINK messages */
368         type = nlmsg->nlmsg_type;
369         if (!(type == RTM_NEWLINK || type == RTM_DELLINK))
370                 return 0;
371
372         info = NLMSG_DATA(nlmsg);
373
374         have_ifaddr = have_ifname = false;
375         mtu = 1;
376
377         attrlen = nlmsg->nlmsg_len - sizeof(*info);
378
379         /* extract the interface name and hardware address attributes */
380         for_each_rta(info + 1, attr, attrlen) {
381                 void *data = RTA_DATA(attr);
382
383                 switch (attr->rta_type) {
384                 case IFLA_ADDRESS:
385                         memcpy(ifaddr, data, sizeof(ifaddr));
386                         have_ifaddr = true;
387                         break;
388
389                 case IFLA_IFNAME:
390                         strncpy(ifname, data, IFNAMSIZ);
391                         have_ifname = true;
392                         break;
393
394                 case IFLA_MTU:
395                         mtu = *(unsigned int *)data;
396                         break;
397                 }
398         }
399
400         if (!have_ifaddr || !have_ifname)
401                 return -1;
402
403         if (type == RTM_DELLINK || mtu == 0) {
404                 interface = find_interface_by_ifindex(network, info->ifi_index);
405                 if (!interface)
406                         return 0;
407                 pb_log("network: interface %s removed\n", interface->name);
408                 remove_interface(network, interface);
409                 return 0;
410         }
411
412
413         interface = find_interface_by_ifindex(network, info->ifi_index);
414         if (!interface) {
415                 interface = talloc_zero(network, struct interface);
416                 interface->ifindex = info->ifi_index;
417                 interface->state = IFSTATE_NEW;
418                 memcpy(interface->hwaddr, ifaddr, sizeof(interface->hwaddr));
419                 strncpy(interface->name, ifname, sizeof(interface->name) - 1);
420                 add_interface(network, interface);
421         }
422
423         /* notify the sysinfo code about changes to this interface */
424         if (strcmp(interface->name, "lo"))
425                 system_info_register_interface(
426                                 sizeof(interface->hwaddr),
427                                 interface->hwaddr, interface->name,
428                                 info->ifi_flags & IFF_LOWER_UP);
429
430         configure_interface(network, interface,
431                         info->ifi_flags & IFF_UP,
432                         info->ifi_flags & IFF_LOWER_UP);
433
434         return 0;
435 }
436
437 static int network_netlink_process(void *arg)
438 {
439         struct network *network = arg;
440         struct nlmsghdr *nlmsg;
441         struct msghdr msg;
442         struct iovec iov;
443         unsigned int len;
444         int rc, flags;
445
446         memset(&msg, 0, sizeof(msg));
447         msg.msg_iov = &iov;
448         msg.msg_iovlen = 1;
449
450         flags = MSG_PEEK;
451
452 retry:
453         iov.iov_len = network->netlink_buf_size;
454         iov.iov_base = network->netlink_buf;
455
456         rc = recvmsg(network->netlink_sd, &msg, flags);
457
458         if (rc < 0) {
459                 perror("netlink recv header");
460                 return -1;
461         }
462
463         len = rc;
464
465         /* if the netlink message was larger than our buffer, realloc
466          * before reading again */
467         if (len > network->netlink_buf_size || msg.msg_flags & MSG_TRUNC) {
468                 network->netlink_buf_size *= 2;
469                 network->netlink_buf = talloc_realloc(network,
470                                         network->netlink_buf,
471                                         char *,
472                                         network->netlink_buf_size);
473                 goto retry;
474         }
475
476         /* otherwise, we're good to read the entire message without PEEK */
477         if (flags == MSG_PEEK) {
478                 flags = 0;
479                 goto retry;
480         }
481
482         for_each_nlmsg(network->netlink_buf, nlmsg, len)
483                 network_handle_nlmsg(network, nlmsg);
484
485         return 0;
486 }
487
488 static void network_init_dns(struct network *network)
489 {
490         const struct config *config;
491         unsigned int i;
492         int rc, len;
493         bool modified;
494         char *buf;
495
496         if (network->dry_run)
497                 return;
498
499         config = config_get();
500         if (!config || !config->network.n_dns_servers)
501                 return;
502
503         rc = read_file(network, "/etc/resolv.conf", &buf, &len);
504
505         if (rc) {
506                 buf = talloc_strdup(network, "");
507                 len = 0;
508         }
509
510         modified = false;
511
512         for (i = 0; i < config->network.n_dns_servers; i++) {
513                 int dns_conf_len;
514                 char *dns_conf;
515
516                 dns_conf = talloc_asprintf(network, "nameserver %s\n",
517                                 config->network.dns_servers[i]);
518
519                 if (strstr(buf, dns_conf)) {
520                         talloc_free(dns_conf);
521                         continue;
522                 }
523
524                 dns_conf_len = strlen(dns_conf);
525                 buf = talloc_realloc(network, buf, char, len + dns_conf_len + 1);
526                 memcpy(buf + len, dns_conf, dns_conf_len);
527                 len += dns_conf_len;
528                 buf[len - 1] = '\0';
529                 modified = true;
530
531                 talloc_free(dns_conf);
532         }
533
534         if (modified) {
535                 rc = replace_file("/etc/resolv.conf", buf, len);
536                 if (rc)
537                         pb_log("error replacing resolv.conf: %s\n",
538                                         strerror(errno));
539         }
540
541         talloc_free(buf);
542 }
543
544 struct network *network_init(struct device_handler *handler,
545                 struct waitset *waitset, bool dry_run)
546 {
547         struct network *network;
548         int rc;
549
550         network = talloc(handler, struct network);
551         list_init(&network->interfaces);
552         network->handler = handler;
553         network->dry_run = dry_run;
554         network->manual_config = config_get()->network.n_interfaces != 0;
555
556         network_init_dns(network);
557
558         rc = network_init_netlink(network);
559         if (rc)
560                 goto err;
561
562         network->waiter = waiter_register_io(waitset, network->netlink_sd,
563                         WAIT_IN, network_netlink_process, network);
564
565         if (!network->waiter)
566                 goto err;
567
568         rc = network_send_link_query(network);
569         if (rc)
570                 goto err;
571
572         return network;
573
574 err:
575         network_shutdown(network);
576         return NULL;
577 }
578
579
580 int network_shutdown(struct network *network)
581 {
582         struct interface *interface;
583
584         if (network->waiter)
585                 waiter_remove(network->waiter);
586
587         list_for_each_entry(&network->interfaces, interface, list) {
588                 if (interface->state == IFSTATE_IGNORED)
589                         continue;
590                 if (!strcmp(interface->name, "lo"))
591                         continue;
592                 interface_down(interface);
593         }
594
595         close(network->netlink_sd);
596         talloc_free(network);
597         return 0;
598 }