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