loopback-setup.c revision 2c3ff76eb004d71a70cb5e253cbca7332c53ac09
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <linux/rtnetlink.h>
#include "util.h"
#include "macro.h"
#include "loopback-setup.h"
#include "socket-util.h"
#define NLMSG_TAIL(nmsg) \
static int add_rtattr(struct nlmsghdr *n, size_t max_length, int type, const void *data, size_t data_length) {
return -E2BIG;
rta = NLMSG_TAIL(n);
return 0;
}
static ssize_t sendto_loop(int fd, const void *buf, size_t buf_len, int flags, const struct sockaddr *sa, socklen_t sa_len) {
for (;;) {
ssize_t l;
if (l >= 0)
return l;
return -errno;
}
}
static ssize_t recvfrom_loop(int fd, void *buf, size_t buf_len, int flags, struct sockaddr *sa, socklen_t *sa_len) {
for (;;) {
ssize_t l;
if (l >= 0)
return l;
return -errno;
}
}
union {
struct sockaddr_nl nl;
} sa;
union {
NLMSG_ALIGN(sizeof(struct ifaddrmsg)) +
RTA_LENGTH(sizeof(struct in6_addr))];
} request;
int r;
if (r < 0)
return r;
return -errno;
(*requests)++;
if (!socket_ipv6_is_supported())
return 0;
r = add_rtattr(&request.header, sizeof(request), IFA_LOCAL, &in6addr_loopback, sizeof(in6addr_loopback));
if (r < 0)
return r;
return -errno;
(*requests)++;
return 0;
}
union {
struct sockaddr_nl nl;
} sa;
union {
NLMSG_ALIGN(sizeof(struct ifinfomsg))];
} request;
return -errno;
(*requests)++;
return 0;
}
union {
struct sockaddr_nl nl;
} sa;
union {
NLMSG_ALIGN(sizeof(struct nlmsgerr))];
} response;
ssize_t l;
if (l < 0)
return -errno;
return -EIO;
return 0;
return -EIO;
return 0;
return -EIO;
}
static int check_loopback(void) {
int r, fd;
union {
struct sockaddr_in in;
} sa;
/* If we failed to set up the loop back device, check whether
* it might already be set up */
if (fd < 0)
return -errno;
r = 1;
else
return r;
}
int loopback_setup(void) {
int r, if_loopback;
union {
struct sockaddr_nl nl;
} sa;
unsigned requests = 0, i;
int fd;
bool eperm = false;
errno = 0;
if (if_loopback <= 0)
if (fd < 0)
return -errno;
r = -errno;
goto finish;
}
if (r < 0)
goto finish;
if (r < 0)
goto finish;
for (i = 0; i < requests; i++) {
if (r == -EPERM)
eperm = true;
else if (r < 0)
goto finish;
}
if (eperm && check_loopback() < 0) {
r = -EPERM;
goto finish;
}
r = 0;
if (r < 0)
if (fd >= 0)
return r;
}