chiark / gitweb /
67ce160c1982f9cb7084cc3e9b7898cc6d4c7c64
[elogind.git] / src / core / loopback-setup.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2010 Lennart Poettering
7
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.
12
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.
17
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/>.
20 ***/
21
22 #include <sys/socket.h>
23 #include <net/if.h>
24 #include <asm/types.h>
25 #include <netinet/in.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29
30 #include "sd-rtnl.h"
31 #include "util.h"
32 #include "macro.h"
33 #include "socket-util.h"
34 #include "rtnl-util.h"
35 #include "missing.h"
36 #include "loopback-setup.h"
37
38 static int start_loopback(sd_rtnl *rtnl) {
39         _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL;
40         int r;
41
42         r = sd_rtnl_message_new_link(rtnl, &req, RTM_SETLINK, LOOPBACK_IFINDEX);
43         if (r < 0)
44                 return r;
45
46         r = sd_rtnl_message_link_set_flags(req, IFF_UP, IFF_UP);
47         if (r < 0)
48                 return r;
49
50         r = sd_rtnl_call(rtnl, req, 0, NULL);
51         if (r < 0)
52                 return r;
53
54         return 0;
55 }
56
57 static bool check_loopback(sd_rtnl *rtnl) {
58         _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL, *reply = NULL;
59         unsigned flags;
60         int r;
61
62         r = sd_rtnl_message_new_link(rtnl, &req, RTM_GETLINK, LOOPBACK_IFINDEX);
63         if (r < 0)
64                 return false;
65
66         r = sd_rtnl_call(rtnl, req, 0, &reply);
67         if (r < 0)
68                 return false;
69
70         r = sd_rtnl_message_link_get_flags(reply, &flags);
71         if (r < 0)
72                 return false;
73
74         return flags & IFF_UP;
75 }
76
77 int loopback_setup(void) {
78         _cleanup_rtnl_unref_ sd_rtnl *rtnl = NULL;
79         int r;
80
81         r = sd_rtnl_open(&rtnl, 0);
82         if (r < 0)
83                 return r;
84
85         r = start_loopback(rtnl);
86         if (r < 0) {
87
88                 /* If we lack the permissions to configure the
89                  * loopback device, but we find it to be already
90                  * configured, let's exit cleanly, in order to
91                  * supported unprivileged containers. */
92                 if (r == -EPERM && check_loopback(rtnl))
93                         return 0;
94
95                 return log_warning_errno(r, "Failed to configure loopback device: %m");
96         }
97
98         return 0;
99 }