loopback-setup.c revision 5d4795f3722911ccd7953c0cf112c1f7624ea834
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/***
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering This file is part of systemd.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Copyright 2010 Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is free software; you can redistribute it and/or modify it
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering under the terms of the GNU Lesser General Public License as published by
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering (at your option) any later version.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is distributed in the hope that it will be useful, but
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Lesser General Public License for more details.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering You should have received a copy of the GNU Lesser General Public License
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering***/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <errno.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <sys/socket.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <net/if.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <asm/types.h>
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen#include <netinet/in.h>
71d35b6b5563817dfbe757ab9e3b9f018b2db491Thomas Hindoe Paaboel Andersen#include <linux/rtnetlink.h>
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering#include <string.h>
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering#include <stdlib.h>
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering#include <unistd.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "util.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "macro.h"
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering#include "loopback-setup.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "socket-util.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "sd-rtnl.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "rtnl-util.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poetteringstatic int pipe_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
b93312f5960b276bae915906ccde36f545bae3e0Zbigniew Jędrzejewski-Szmek int *counter = userdata;
b93312f5960b276bae915906ccde36f545bae3e0Zbigniew Jędrzejewski-Szmek int r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering (*counter) --;
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering r = sd_rtnl_message_get_errno(m);
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering return r == -EEXIST ? 0 : r;
23502de3b0891455c8ce499a9eb61b69d060a829Daniel Mack}
23502de3b0891455c8ce499a9eb61b69d060a829Daniel Mack
23502de3b0891455c8ce499a9eb61b69d060a829Daniel Mackstatic int add_addresses(sd_rtnl *rtnl, int if_loopback, struct in_addr *ipv4_address, int *counter) {
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *ipv4 = NULL, *ipv6 = NULL;
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering int r;
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering r = sd_rtnl_message_addr_new(RTM_NEWADDR, if_loopback, AF_INET, 8,
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering IFA_F_PERMANENT, RT_SCOPE_HOST, &ipv4);
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering if (r < 0)
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering return r;
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering r = sd_rtnl_message_append_in_addr(ipv4, IFA_LOCAL, ipv4_address);
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering if (r < 0)
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering return r;
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering r = sd_rtnl_call_async(rtnl, ipv4, &pipe_handler, counter, 0, NULL);
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering if (r < 0)
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering return r;
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering (*counter) ++;
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering if (!socket_ipv6_is_supported())
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering return 0;
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering r = sd_rtnl_message_addr_new(RTM_NEWADDR, if_loopback, AF_INET6, 128,
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering IFA_F_PERMANENT, RT_SCOPE_HOST, &ipv6);
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering if (r < 0)
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering return r;
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering r = sd_rtnl_message_append_in6_addr(ipv6, IFA_LOCAL, &in6addr_loopback);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (r < 0)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return r;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = sd_rtnl_call_async(rtnl, ipv6, &pipe_handler, counter, 0, NULL);
23502de3b0891455c8ce499a9eb61b69d060a829Daniel Mack if (r < 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering (*counter) ++;
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering return 0;
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering}
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poetteringstatic int start_interface(sd_rtnl *rtnl, int if_loopback, struct in_addr *ipv4_address, int *counter) {
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *req = NULL;
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering int r;
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering r = sd_rtnl_message_link_new(RTM_SETLINK, if_loopback, &req);
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering if (r < 0)
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering return r;
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering r = sd_rtnl_message_link_set_flags(req, IFF_UP, IFF_UP);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering if (r < 0)
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering return r;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering r = sd_rtnl_call_async(rtnl, req, &pipe_handler, counter, 0, NULL);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering if (r < 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering (*counter) ++;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering return 0;
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering}
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poetteringstatic int check_loopback(void) {
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering int r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering _cleanup_close_ int fd = -1;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering union {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering struct sockaddr sa;
f5430a3ef308f3a102899fcaf7fbece757082f2aLennart Poettering struct sockaddr_in in;
d75acfb059ece4512278b8820a9103664996f1e5Lennart Poettering } sa = {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering .in.sin_family = AF_INET,
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering .in.sin_addr.s_addr = INADDR_LOOPBACK,
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering };
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering /* If we failed to set up the loop back device, check whether
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering * it might already be set up */
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering fd = socket(AF_INET, SOCK_DGRAM|SOCK_NONBLOCK|SOCK_CLOEXEC, 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (fd < 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return -errno;
8ac4e9e1e54397f6d1745c2a7a806132418c7da2Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (bind(fd, &sa.sa, sizeof(sa.in)) >= 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = 1;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering else
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = errno == EADDRNOTAVAIL ? 0 : -errno;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return r;
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek}
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
c0eb11cfd016381fe02875a4ef29c1ade00c94e7Lennart Poetteringint loopback_setup(void) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering _cleanup_sd_rtnl_unref_ sd_rtnl *rtnl = NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering int r, if_loopback, counter = 0;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering bool eperm = false;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering struct in_addr ipv4_address;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering errno = 0;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if_loopback = (int) if_nametoindex("lo");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (if_loopback <= 0)
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering return errno ? -errno : -ENODEV;
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering ipv4_address.s_addr = htonl(INADDR_LOOPBACK);
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering r = sd_rtnl_open(0, &rtnl);
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering if (r < 0)
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering return r;
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering r = add_addresses(rtnl, if_loopback, &ipv4_address, &counter);
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering if (r < 0)
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek return r;
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek r = start_interface(rtnl, if_loopback, &ipv4_address, &counter);
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek if (r < 0)
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek return r;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek while (counter > 0) {
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek r = sd_rtnl_wait(rtnl, 0);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek if (r < 0)
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek return r;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek r = sd_rtnl_process(rtnl, 0);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek if (r < 0) {
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek if (r == -EPERM)
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek eperm = true;
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering else {
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen log_warning("Failed to configure loopback device: %s", strerror(-r));
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen return r;
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen }
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen }
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen }
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen if (eperm && check_loopback() < 0) {
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen log_warning("Failed to configure loopback device: %s", strerror(EPERM));
549c1a2564b56f2bb38f1203d59c747ea15817f3Tom Gundersen return -EPERM;
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering }
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering return 0;
549c1a2564b56f2bb38f1203d59c747ea15817f3Tom Gundersen}
549c1a2564b56f2bb38f1203d59c747ea15817f3Tom Gundersen