loopback-setup.c revision f3fc48150bb443f5b4623a819ebc2b39971a4f66
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 <sys/socket.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <net/if.h>
3c0cf502796be355431d4a64d738e75f543aa51dLennart Poettering#include <asm/types.h>
3c0cf502796be355431d4a64d738e75f543aa51dLennart Poettering#include <netinet/in.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <string.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <stdlib.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include <unistd.h>
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering#include "util.h"
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering#include "macro.h"
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering#include "loopback-setup.h"
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering#include "socket-util.h"
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering#include "sd-rtnl.h"
3e684349c2cead2e6fd2f816c34eb17daba23a49Lennart Poettering#include "rtnl-util.h"
3e684349c2cead2e6fd2f816c34eb17daba23a49Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/* this is hardcoded in the kernel, so don't look it up */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#define LOOPBACK_IFINDEX 1
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersenstatic int start_loopback(sd_rtnl *rtnl) {
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL;
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering int r;
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering
3c0cf502796be355431d4a64d738e75f543aa51dLennart Poettering r = sd_rtnl_message_new_link(rtnl, &req, RTM_SETLINK, LOOPBACK_IFINDEX);
3c0cf502796be355431d4a64d738e75f543aa51dLennart Poettering if (r < 0)
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering return r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = sd_rtnl_message_link_set_flags(req, IFF_UP, IFF_UP);
3c0cf502796be355431d4a64d738e75f543aa51dLennart Poettering if (r < 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = sd_rtnl_call(rtnl, req, 0, NULL);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (r < 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return 0;
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poetteringstatic int check_loopback(void) {
3c0cf502796be355431d4a64d738e75f543aa51dLennart Poettering int r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering _cleanup_close_ int fd = -1;
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen union {
91b14d6ff362b938a72db17b095ee9903d07381bTom Gundersen struct sockaddr sa;
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering struct sockaddr_in in;
8300ba218e3cf5049496937be8bce10f22d09bbcTom Gundersen } sa = {
8300ba218e3cf5049496937be8bce10f22d09bbcTom Gundersen .in.sin_family = AF_INET,
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt .in.sin_addr.s_addr = INADDR_LOOPBACK,
};
/* If we failed to set up the loop back device, check whether
* it might already be set up */
fd = socket(AF_INET, SOCK_DGRAM|SOCK_NONBLOCK|SOCK_CLOEXEC, 0);
if (fd < 0)
return -errno;
if (bind(fd, &sa.sa, sizeof(sa.in)) >= 0)
r = 1;
else
r = errno == EADDRNOTAVAIL ? 0 : -errno;
return r;
}
int loopback_setup(void) {
_cleanup_rtnl_unref_ sd_rtnl *rtnl = NULL;
int r;
r = sd_rtnl_open(&rtnl, 0);
if (r < 0)
return r;
r = start_loopback(rtnl);
if (r == -EPERM) {
if (check_loopback() < 0) {
log_warning("Failed to configure loopback device: %s",
strerror(EPERM));
return -EPERM;
}
} else if (r < 0) {
log_warning("Failed to configure loopback device: %s",
strerror(-r));
return r;
}
return 0;
}