]>
Commit | Line | Data |
---|---|---|
af5bc85d LP |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2010 Lennart Poettering | |
5 | ||
6 | systemd is free software; you can redistribute it and/or modify it | |
5430f7f2 LP |
7 | under the terms of the GNU Lesser General Public License as published by |
8 | the Free Software Foundation; either version 2.1 of the License, or | |
af5bc85d LP |
9 | (at your option) any later version. |
10 | ||
11 | systemd is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
5430f7f2 | 14 | Lesser General Public License for more details. |
af5bc85d | 15 | |
5430f7f2 | 16 | You should have received a copy of the GNU Lesser General Public License |
af5bc85d LP |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. |
18 | ***/ | |
19 | ||
af5bc85d | 20 | #include <net/if.h> |
af5bc85d | 21 | #include <stdlib.h> |
af5bc85d | 22 | |
1c4baffc | 23 | #include "sd-netlink.h" |
07630cea | 24 | |
9ca903cc | 25 | #include "loopback-setup.h" |
07630cea LP |
26 | #include "missing.h" |
27 | #include "netlink-util.h" | |
5a723174 | 28 | |
1c4baffc | 29 | static int start_loopback(sd_netlink *rtnl) { |
4afd3348 | 30 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
81eca919 | 31 | int r; |
af5bc85d | 32 | |
f3fc4815 | 33 | r = sd_rtnl_message_new_link(rtnl, &req, RTM_SETLINK, LOOPBACK_IFINDEX); |
fc25d7f8 TG |
34 | if (r < 0) |
35 | return r; | |
36 | ||
5d4795f3 | 37 | r = sd_rtnl_message_link_set_flags(req, IFF_UP, IFF_UP); |
81eca919 TG |
38 | if (r < 0) |
39 | return r; | |
af5bc85d | 40 | |
1c4baffc | 41 | r = sd_netlink_call(rtnl, req, 0, NULL); |
81eca919 TG |
42 | if (r < 0) |
43 | return r; | |
af5bc85d LP |
44 | |
45 | return 0; | |
46 | } | |
47 | ||
1c4baffc | 48 | static bool check_loopback(sd_netlink *rtnl) { |
4afd3348 | 49 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL; |
e95e909d | 50 | unsigned flags; |
e62d8c39 | 51 | int r; |
e95e909d TG |
52 | |
53 | r = sd_rtnl_message_new_link(rtnl, &req, RTM_GETLINK, LOOPBACK_IFINDEX); | |
54 | if (r < 0) | |
2f0af4e1 | 55 | return false; |
e95e909d | 56 | |
1c4baffc | 57 | r = sd_netlink_call(rtnl, req, 0, &reply); |
e95e909d | 58 | if (r < 0) |
2f0af4e1 | 59 | return false; |
e95e909d TG |
60 | |
61 | r = sd_rtnl_message_link_get_flags(reply, &flags); | |
62 | if (r < 0) | |
2f0af4e1 | 63 | return false; |
e95e909d TG |
64 | |
65 | return flags & IFF_UP; | |
2c3ff76e LP |
66 | } |
67 | ||
af5bc85d | 68 | int loopback_setup(void) { |
4afd3348 | 69 | _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; |
f3fc4815 | 70 | int r; |
0a0dc69b | 71 | |
1c4baffc | 72 | r = sd_netlink_open(&rtnl); |
2c3ff76e | 73 | if (r < 0) |
81eca919 | 74 | return r; |
af5bc85d | 75 | |
f3fc4815 | 76 | r = start_loopback(rtnl); |
8f084002 LP |
77 | if (r < 0) { |
78 | ||
79 | /* If we lack the permissions to configure the | |
80 | * loopback device, but we find it to be already | |
81 | * configured, let's exit cleanly, in order to | |
82 | * supported unprivileged containers. */ | |
83 | if (r == -EPERM && check_loopback(rtnl)) | |
84 | return 0; | |
af5bc85d | 85 | |
8f084002 LP |
86 | return log_warning_errno(r, "Failed to configure loopback device: %m"); |
87 | } | |
2c3ff76e | 88 | |
e62d8c39 | 89 | return 0; |
af5bc85d | 90 | } |