chiark / gitweb /
bus: when connecting to a container's kdbus instance, enter namespace first
[elogind.git] / src / core / loopback-setup.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2010 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <errno.h>
23 #include <sys/socket.h>
24 #include <net/if.h>
25 #include <asm/types.h>
26 #include <netinet/in.h>
27 #include <linux/rtnetlink.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31
32 #include "util.h"
33 #include "macro.h"
34 #include "loopback-setup.h"
35 #include "socket-util.h"
36 #include "sd-rtnl.h"
37 #include "rtnl-util.h"
38
39 static int pipe_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
40         int *counter = userdata;
41         int r;
42
43         (*counter) --;
44
45         r = sd_rtnl_message_get_errno(m);
46
47         return r == -EEXIST ? 0 : r;
48 }
49
50 static int add_adresses(sd_rtnl *rtnl, int if_loopback, uint32_t ipv4_address, int *counter) {
51         _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *ipv4 = NULL, *ipv6 = NULL;
52         int r;
53
54         r = sd_rtnl_message_addr_new(RTM_NEWADDR, if_loopback, AF_INET, 8, IFA_F_PERMANENT, RT_SCOPE_HOST, &ipv4);
55         if (r < 0)
56                 return r;
57
58         r = sd_rtnl_message_append(ipv4, IFA_LOCAL, &ipv4_address);
59         if (r < 0)
60                 return r;
61
62         r = sd_rtnl_call_async(rtnl, ipv4, &pipe_handler, counter, 0, NULL);
63         if (r < 0)
64                 return r;
65
66         (*counter) ++;
67
68         if (!socket_ipv6_is_supported())
69                 return 0;
70
71         r = sd_rtnl_message_addr_new(RTM_NEWADDR, if_loopback, AF_INET6, 128, 0, 0, &ipv6);
72         if (r < 0)
73                 return r;
74
75         r = sd_rtnl_message_append(ipv6, IFA_LOCAL, &in6addr_loopback);
76         if (r < 0)
77                 return r;
78
79         r = sd_rtnl_call_async(rtnl, ipv6, &pipe_handler, counter, 0, NULL);
80         if (r < 0)
81                 return r;
82
83         (*counter) ++;
84
85         return 0;
86 }
87
88 static int start_interface(sd_rtnl *rtnl, int if_loopback, uint32_t ipv4_address, int *counter) {
89         _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *req = NULL;
90         int r;
91
92         r = sd_rtnl_message_link_new(RTM_NEWLINK, if_loopback, 0, IFF_UP, &req);
93         if (r < 0)
94                 return r;
95
96         r = sd_rtnl_message_append(req, IFA_LOCAL, &ipv4_address);
97         if (r < 0)
98                 return r;
99
100         r = sd_rtnl_call_async(rtnl, req, &pipe_handler, counter, 0, NULL);
101         if (r < 0)
102                 return r;
103
104         (*counter) ++;
105
106         return 0;
107 }
108
109 static int check_loopback(void) {
110         int r;
111         _cleanup_close_ int fd = -1;
112         union {
113                 struct sockaddr sa;
114                 struct sockaddr_in in;
115         } sa = {
116                 .in.sin_family = AF_INET,
117                 .in.sin_addr.s_addr = INADDR_LOOPBACK,
118         };
119
120         /* If we failed to set up the loop back device, check whether
121          * it might already be set up */
122
123         fd = socket(AF_INET, SOCK_DGRAM|SOCK_NONBLOCK|SOCK_CLOEXEC, 0);
124         if (fd < 0)
125                 return -errno;
126
127         if (bind(fd, &sa.sa, sizeof(sa.in)) >= 0)
128                 r = 1;
129         else
130                 r = errno == EADDRNOTAVAIL ? 0 : -errno;
131
132         return r;
133 }
134
135 int loopback_setup(void) {
136         _cleanup_sd_rtnl_unref_ sd_rtnl *rtnl = NULL;
137         int r, if_loopback, counter = 0;
138         bool eperm = false;
139         uint32_t ipv4_address = htonl(INADDR_LOOPBACK);
140
141         errno = 0;
142         if_loopback = (int) if_nametoindex("lo");
143         if (if_loopback <= 0)
144                 return errno ? -errno : -ENODEV;
145
146         r = sd_rtnl_open(0, &rtnl);
147         if (r < 0)
148                 return r;
149
150         r = add_adresses(rtnl, if_loopback, ipv4_address, &counter);
151         if (r < 0)
152                 return r;
153
154         r = start_interface(rtnl, if_loopback, ipv4_address, &counter);
155         if (r < 0)
156                 return r;
157
158         while (counter > 0) {
159                 r = sd_rtnl_wait(rtnl, 0);
160                 if (r < 0)
161                         return r;
162
163                 r = sd_rtnl_process(rtnl, 0);
164                 if (r < 0) {
165                         if (r == -EPERM)
166                                 eperm = true;
167                         else {
168                                 log_warning("Failed to configure loopback device: %s", strerror(-r));
169                                 return r;
170                         }
171                 }
172         }
173
174         if (eperm && check_loopback() < 0) {
175                 log_warning("Failed to configure loopback device: %s", strerror(EPERM));
176                 return -EPERM;
177         }
178
179         return 0;
180 }