loopback-setup.c revision 5d4795f3722911ccd7953c0cf112c1f7624ea834
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering This file is part of systemd.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Copyright 2010 Lennart 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 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 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/>.
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poetteringstatic int pipe_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering return r == -EEXIST ? 0 : r;
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 r = sd_rtnl_message_addr_new(RTM_NEWADDR, if_loopback, AF_INET, 8,
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering r = sd_rtnl_message_append_in_addr(ipv4, IFA_LOCAL, ipv4_address);
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering r = sd_rtnl_call_async(rtnl, ipv4, &pipe_handler, counter, 0, NULL);
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering r = sd_rtnl_message_addr_new(RTM_NEWADDR, if_loopback, AF_INET6, 128,
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering r = sd_rtnl_message_append_in6_addr(ipv6, IFA_LOCAL, &in6addr_loopback);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = sd_rtnl_call_async(rtnl, ipv6, &pipe_handler, counter, 0, NULL);
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 r = sd_rtnl_message_link_new(RTM_SETLINK, if_loopback, &req);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering r = sd_rtnl_message_link_set_flags(req, IFF_UP, IFF_UP);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering r = sd_rtnl_call_async(rtnl, req, &pipe_handler, counter, 0, NULL);
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poetteringstatic int check_loopback(void) {
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering /* If we failed to set up the loop back device, check whether
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering * it might already be set up */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering fd = socket(AF_INET, SOCK_DGRAM|SOCK_NONBLOCK|SOCK_CLOEXEC, 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (bind(fd, &sa.sa, sizeof(sa.in)) >= 0)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering _cleanup_sd_rtnl_unref_ sd_rtnl *rtnl = NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if_loopback = (int) if_nametoindex("lo");
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering ipv4_address.s_addr = htonl(INADDR_LOOPBACK);
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering r = add_addresses(rtnl, if_loopback, &ipv4_address, &counter);
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek r = start_interface(rtnl, if_loopback, &ipv4_address, &counter);
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen log_warning("Failed to configure loopback device: %s", strerror(-r));
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen log_warning("Failed to configure loopback device: %s", strerror(EPERM));