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