Line data Source code
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 <net/if.h>
23 : #include <stdlib.h>
24 :
25 : #include "sd-netlink.h"
26 : #include "netlink-util.h"
27 : #include "missing.h"
28 : #include "loopback-setup.h"
29 :
30 0 : static int start_loopback(sd_netlink *rtnl) {
31 0 : _cleanup_netlink_message_unref_ sd_netlink_message *req = NULL;
32 : int r;
33 :
34 0 : r = sd_rtnl_message_new_link(rtnl, &req, RTM_SETLINK, LOOPBACK_IFINDEX);
35 0 : if (r < 0)
36 0 : return r;
37 :
38 0 : r = sd_rtnl_message_link_set_flags(req, IFF_UP, IFF_UP);
39 0 : if (r < 0)
40 0 : return r;
41 :
42 0 : r = sd_netlink_call(rtnl, req, 0, NULL);
43 0 : if (r < 0)
44 0 : return r;
45 :
46 0 : return 0;
47 : }
48 :
49 0 : static bool check_loopback(sd_netlink *rtnl) {
50 0 : _cleanup_netlink_message_unref_ sd_netlink_message *req = NULL, *reply = NULL;
51 : unsigned flags;
52 : int r;
53 :
54 0 : r = sd_rtnl_message_new_link(rtnl, &req, RTM_GETLINK, LOOPBACK_IFINDEX);
55 0 : if (r < 0)
56 0 : return false;
57 :
58 0 : r = sd_netlink_call(rtnl, req, 0, &reply);
59 0 : if (r < 0)
60 0 : return false;
61 :
62 0 : r = sd_rtnl_message_link_get_flags(reply, &flags);
63 0 : if (r < 0)
64 0 : return false;
65 :
66 0 : return flags & IFF_UP;
67 : }
68 :
69 0 : int loopback_setup(void) {
70 0 : _cleanup_netlink_unref_ sd_netlink *rtnl = NULL;
71 : int r;
72 :
73 0 : r = sd_netlink_open(&rtnl);
74 0 : if (r < 0)
75 0 : return r;
76 :
77 0 : r = start_loopback(rtnl);
78 0 : if (r < 0) {
79 :
80 : /* If we lack the permissions to configure the
81 : * loopback device, but we find it to be already
82 : * configured, let's exit cleanly, in order to
83 : * supported unprivileged containers. */
84 0 : if (r == -EPERM && check_loopback(rtnl))
85 0 : return 0;
86 :
87 0 : return log_warning_errno(r, "Failed to configure loopback device: %m");
88 : }
89 :
90 0 : return 0;
91 : }
|