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