1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
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.
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.
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/>.
23 #include <sys/socket.h>
25 #include <asm/types.h>
26 #include <netinet/in.h>
33 #include "loopback-setup.h"
34 #include "socket-util.h"
36 #include "rtnl-util.h"
38 static int pipe_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
39 int *counter = userdata;
44 r = sd_rtnl_message_get_errno(m);
46 return r == -EEXIST ? 0 : r;
49 static int add_addresses(sd_rtnl *rtnl, int if_loopback, struct in_addr *ipv4_address, int *counter) {
50 _cleanup_rtnl_message_unref_ sd_rtnl_message *ipv4 = NULL, *ipv6 = NULL;
53 r = sd_rtnl_message_new_addr(RTM_NEWADDR, if_loopback, AF_INET, &ipv4);
57 r = sd_rtnl_message_addr_set_prefixlen(ipv4, 8);
61 r = sd_rtnl_message_addr_set_flags(ipv4, IFA_F_PERMANENT);
65 r = sd_rtnl_message_addr_set_scope(ipv4, RT_SCOPE_HOST);
69 r = sd_rtnl_message_append_in_addr(ipv4, IFA_LOCAL, ipv4_address);
73 r = sd_rtnl_call_async(rtnl, ipv4, &pipe_handler, counter, 0, NULL);
79 if (!socket_ipv6_is_supported())
82 r = sd_rtnl_message_new_addr(RTM_NEWADDR, if_loopback, AF_INET6, &ipv6);
86 r = sd_rtnl_message_addr_set_prefixlen(ipv6, 128);
90 r = sd_rtnl_message_addr_set_flags(ipv6, IFA_F_PERMANENT);
94 r = sd_rtnl_message_addr_set_scope(ipv6, RT_SCOPE_HOST);
98 r = sd_rtnl_message_append_in6_addr(ipv6, IFA_LOCAL, &in6addr_loopback);
102 r = sd_rtnl_call_async(rtnl, ipv6, &pipe_handler, counter, 0, NULL);
111 static int start_interface(sd_rtnl *rtnl, int if_loopback, struct in_addr *ipv4_address, int *counter) {
112 _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL;
115 r = sd_rtnl_message_new_link(RTM_SETLINK, if_loopback, &req);
119 r = sd_rtnl_message_link_set_flags(req, IFF_UP, IFF_UP);
123 r = sd_rtnl_call_async(rtnl, req, &pipe_handler, counter, 0, NULL);
132 static int check_loopback(void) {
134 _cleanup_close_ int fd = -1;
137 struct sockaddr_in in;
139 .in.sin_family = AF_INET,
140 .in.sin_addr.s_addr = INADDR_LOOPBACK,
143 /* If we failed to set up the loop back device, check whether
144 * it might already be set up */
146 fd = socket(AF_INET, SOCK_DGRAM|SOCK_NONBLOCK|SOCK_CLOEXEC, 0);
150 if (bind(fd, &sa.sa, sizeof(sa.in)) >= 0)
153 r = errno == EADDRNOTAVAIL ? 0 : -errno;
158 int loopback_setup(void) {
159 _cleanup_rtnl_unref_ sd_rtnl *rtnl = NULL;
160 int r, if_loopback, counter = 0;
162 struct in_addr ipv4_address;
165 if_loopback = (int) if_nametoindex("lo");
166 if (if_loopback <= 0)
167 return errno ? -errno : -ENODEV;
169 ipv4_address.s_addr = htonl(INADDR_LOOPBACK);
171 r = sd_rtnl_open(0, &rtnl);
175 r = add_addresses(rtnl, if_loopback, &ipv4_address, &counter);
179 r = start_interface(rtnl, if_loopback, &ipv4_address, &counter);
183 while (counter > 0) {
184 r = sd_rtnl_wait(rtnl, 0);
188 r = sd_rtnl_process(rtnl, 0);
193 log_warning("Failed to configure loopback device: %s", strerror(-r));
199 if (eperm && check_loopback() < 0) {
200 log_warning("Failed to configure loopback device: %s", strerror(EPERM));