]> git.ozlabs.org Git - petitboot/blob - discover/network.c
eaf6d1b4e3e9d6b4283ac6a534692f8098bd69d1
[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 void add_interface(struct network *network,
142                 struct interface *interface)
143 {
144         list_add(&network->interfaces, &interface->list);
145 }
146
147 static void remove_interface(struct interface *interface)
148 {
149         list_remove(&interface->list);
150         talloc_free(interface);
151 }
152
153 static int interface_change(struct interface *interface, bool up)
154 {
155         const char *statestr = up ? "up" : "down";
156         int rc;
157
158         if (!up && interface->udhcpc_process) {
159                 /* we don't care about the callback from here */
160                 interface->udhcpc_process->exit_cb = NULL;
161                 interface->udhcpc_process->data = NULL;
162                 process_stop_async(interface->udhcpc_process);
163                 process_release(interface->udhcpc_process);
164         }
165
166         rc = process_run_simple(interface, pb_system_apps.ip,
167                         "link", "set", interface->name, statestr, NULL);
168         if (rc) {
169                 pb_log("failed to bring interface %s %s\n", interface->name,
170                                 statestr);
171                 return -1;
172         }
173         return 0;
174 }
175
176 static int interface_up(struct interface *interface)
177 {
178         return interface_change(interface, true);
179 }
180
181 static int interface_down(struct interface *interface)
182 {
183         return interface_change(interface, false);
184 }
185
186 static void udhcpc_process_exit(struct process *process)
187 {
188         struct interface *interface = process->data;
189         pb_log("udhcp client [pid %d] for interface %s exited, rc %d\n",
190                         process->pid, interface->name, process->exit_status);
191         interface->udhcpc_process = NULL;
192         process_release(process);
193 }
194
195 static void configure_interface_dhcp(struct interface *interface)
196 {
197         struct process *process;
198         char pidfile[256];
199         int rc;
200         const char *argv[] = {
201                 pb_system_apps.udhcpc,
202                 "-R",
203                 "-n",
204                 "-p", pidfile,
205                 "-i", interface->name,
206                 NULL,
207         };
208         snprintf(pidfile, sizeof(pidfile), "%s/udhcpc-%s.pid",
209                         PIDFILE_BASE, interface->name);
210
211         process = process_create(interface);
212
213         process->path = pb_system_apps.udhcpc;
214         process->argv = argv;
215         process->exit_cb = udhcpc_process_exit;
216         process->data = interface;
217
218         rc = process_run_async(process);
219
220         if (rc)
221                 process_release(process);
222         else
223                 interface->udhcpc_process = process;
224
225         return;
226 }
227
228 static void configure_interface_static(struct interface *interface,
229                 const struct interface_config *config)
230 {
231         int rc;
232
233         rc = process_run_simple(interface, pb_system_apps.ip,
234                         "address", "add", config->static_config.address,
235                         "dev", interface->name, NULL);
236
237
238         if (rc) {
239                 pb_log("failed to add address %s to interface %s\n",
240                                 config->static_config.address,
241                                 interface->name);
242                 return;
243         }
244
245         /* we need the interface up before we can route through it */
246         rc = interface_up(interface);
247         if (rc)
248                 return;
249
250         if (config->static_config.gateway)
251                 rc = process_run_simple(interface, pb_system_apps.ip,
252                                 "route", "add", "default",
253                                 "via", config->static_config.gateway,
254                                 NULL);
255
256         if (rc) {
257                 pb_log("failed to add default route %s on interface %s\n",
258                                 config->static_config.gateway,
259                                 interface->name);
260         }
261
262         return;
263 }
264
265 static void configure_interface(struct network *network,
266                 struct interface *interface, bool up, bool link)
267 {
268         const struct interface_config *config = NULL;
269
270         if (interface->state == IFSTATE_IGNORED)
271                 return;
272
273         /* old interface? check that we're still up and running */
274         if (interface->state == IFSTATE_CONFIGURED) {
275                 if (!up)
276                         interface->state = IFSTATE_NEW;
277                 else if (!link)
278                         interface->state = IFSTATE_UP_WAITING_LINK;
279                 else
280                         return;
281         }
282
283         /* always up the lookback, no other handling required */
284         if (!strcmp(interface->name, "lo")) {
285                 if (interface->state == IFSTATE_NEW)
286                         interface_up(interface);
287                 interface->state = IFSTATE_CONFIGURED;
288                 return;
289         }
290
291         config = find_config_by_hwaddr(interface->hwaddr);
292         if (config && config->ignore) {
293                 pb_log("network: ignoring interface %s\n", interface->name);
294                 interface->state = IFSTATE_IGNORED;
295                 return;
296         }
297
298         /* if we're in manual config mode, we need an interface configuration */
299         if (network->manual_config && !config) {
300                 interface->state = IFSTATE_IGNORED;
301                 pb_log("network: skipping %s: manual config mode, "
302                                 "but no config for this interface\n",
303                                 interface->name);
304                 return;
305         }
306
307         /* new interface? bring up to the point so we can detect a link */
308         if (interface->state == IFSTATE_NEW) {
309                 if (!up) {
310                         interface_up(interface);
311                         pb_log("network: bringing up interface %s\n",
312                                         interface->name);
313                         return;
314
315                 } else if (!link) {
316                         interface->state = IFSTATE_UP_WAITING_LINK;
317                 }
318         }
319
320         /* no link? wait for a notification */
321         if (interface->state == IFSTATE_UP_WAITING_LINK && !link)
322                 return;
323
324         pb_log("network: configuring interface %s\n", interface->name);
325
326         if (!config || config->method == CONFIG_METHOD_DHCP) {
327                 configure_interface_dhcp(interface);
328
329         } else if (config->method == CONFIG_METHOD_STATIC) {
330                 configure_interface_static(interface, config);
331         }
332 }
333
334 static int network_handle_nlmsg(struct network *network, struct nlmsghdr *nlmsg)
335 {
336         bool have_ifaddr, have_ifname;
337         struct interface *interface;
338         struct ifinfomsg *info;
339         struct rtattr *attr;
340         unsigned int mtu;
341         uint8_t ifaddr[6];
342         char ifname[IFNAMSIZ+1];
343         int attrlen, type;
344
345
346         /* we're only interested in NEWLINK messages */
347         type = nlmsg->nlmsg_type;
348         if (!(type == RTM_NEWLINK || type == RTM_DELLINK))
349                 return 0;
350
351         info = NLMSG_DATA(nlmsg);
352
353         have_ifaddr = have_ifname = false;
354
355         attrlen = nlmsg->nlmsg_len - sizeof(*info);
356
357         /* extract the interface name and hardware address attributes */
358         for_each_rta(info + 1, attr, attrlen) {
359                 void *data = RTA_DATA(attr);
360
361                 switch (attr->rta_type) {
362                 case IFLA_ADDRESS:
363                         memcpy(ifaddr, data, sizeof(ifaddr));
364                         have_ifaddr = true;
365                         break;
366
367                 case IFLA_IFNAME:
368                         strncpy(ifname, data, IFNAMSIZ);
369                         have_ifname = true;
370                         break;
371
372                 case IFLA_MTU:
373                         mtu = *(unsigned int *)data;
374                         break;
375                 }
376         }
377
378         if (!have_ifaddr || !have_ifname)
379                 return -1;
380
381         if (type == RTM_DELLINK || mtu == 0) {
382                 interface = find_interface_by_ifindex(network, info->ifi_index);
383                 if (!interface)
384                         return 0;
385                 pb_log("network: interface %s removed\n", interface->name);
386                 remove_interface(interface);
387                 return 0;
388         }
389
390
391         interface = find_interface_by_ifindex(network, info->ifi_index);
392         if (!interface) {
393                 interface = talloc_zero(network, struct interface);
394                 interface->ifindex = info->ifi_index;
395                 interface->state = IFSTATE_NEW;
396                 memcpy(interface->hwaddr, ifaddr, sizeof(interface->hwaddr));
397                 strncpy(interface->name, ifname, sizeof(interface->name) - 1);
398                 add_interface(network, interface);
399         }
400
401         configure_interface(network, interface,
402                         info->ifi_flags & IFF_UP,
403                         info->ifi_flags & IFF_LOWER_UP);
404
405         return 0;
406 }
407
408 static int network_netlink_process(void *arg)
409 {
410         struct network *network = arg;
411         struct nlmsghdr *nlmsg;
412         unsigned int len;
413         char buf[4096];
414         int rc;
415
416         rc = recv(network->netlink_sd, buf, sizeof(buf), 0);
417         if (rc < 0) {
418                 perror("netlink recv");
419                 return -1;
420         }
421
422         len = rc;
423
424         for_each_nlmsg(buf, nlmsg, len)
425                 network_handle_nlmsg(network, nlmsg);
426
427         return 0;
428 }
429
430 static void network_init_dns(struct network *network)
431 {
432         const struct config *config;
433         int i, rc, len;
434         bool modified;
435         char *buf;
436
437         if (network->dry_run)
438                 return;
439
440         config = config_get();
441         if (!config || !config->network.n_dns_servers)
442                 return;
443
444         rc = read_file(network, "/etc/resolv.conf", &buf, &len);
445
446         if (rc) {
447                 buf = talloc_strdup(network, "");
448                 len = 0;
449         }
450
451         modified = false;
452
453         for (i = 0; i < config->network.n_dns_servers; i++) {
454                 int dns_conf_len;
455                 char *dns_conf;
456
457                 dns_conf = talloc_asprintf(network, "nameserver %s\n",
458                                 config->network.dns_servers[i]);
459
460                 if (strstr(buf, dns_conf)) {
461                         talloc_free(dns_conf);
462                         continue;
463                 }
464
465                 dns_conf_len = strlen(dns_conf);
466                 buf = talloc_realloc(network, buf, char, len + dns_conf_len);
467                 memcpy(buf + len, dns_conf, dns_conf_len);
468                 len += dns_conf_len;
469                 modified = true;
470
471                 talloc_free(dns_conf);
472         }
473
474         if (modified) {
475                 rc = replace_file("/etc/resolv.conf", buf, len);
476                 if (rc)
477                         pb_log("error replacing resolv.conf: %s\n",
478                                         strerror(errno));
479         }
480
481         talloc_free(buf);
482 }
483
484 struct network *network_init(void *ctx, struct waitset *waitset, bool dry_run)
485 {
486         struct network *network;
487         int rc;
488
489         network = talloc(ctx, struct network);
490         list_init(&network->interfaces);
491         network->manual_config = false;
492         network->dry_run = dry_run;
493
494         network_init_dns(network);
495
496         rc = network_init_netlink(network);
497         if (rc)
498                 goto err;
499
500         network->waiter = waiter_register_io(waitset, network->netlink_sd,
501                         WAIT_IN, network_netlink_process, network);
502
503         if (!network->waiter)
504                 goto err;
505
506         rc = network_send_link_query(network);
507         if (rc)
508                 goto err;
509
510         return network;
511
512 err:
513         network_shutdown(network);
514         return NULL;
515 }
516
517
518 int network_shutdown(struct network *network)
519 {
520         struct interface *interface;
521
522         if (network->waiter)
523                 waiter_remove(network->waiter);
524
525         list_for_each_entry(&network->interfaces, interface, list)
526                 interface_down(interface);
527
528         close(network->netlink_sd);
529         talloc_free(network);
530         return 0;
531 }