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