]>
Commit | Line | Data |
---|---|---|
d6c9574f | 1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
af5bc85d LP |
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 | |
5430f7f2 LP |
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 | |
af5bc85d LP |
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 | |
5430f7f2 | 16 | Lesser General Public License for more details. |
af5bc85d | 17 | |
5430f7f2 | 18 | You should have received a copy of the GNU Lesser General Public License |
af5bc85d LP |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. |
20 | ***/ | |
21 | ||
af5bc85d LP |
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> | |
af5bc85d LP |
29 | |
30 | #include "util.h" | |
31 | #include "macro.h" | |
32 | #include "loopback-setup.h" | |
5bfcc1c6 | 33 | #include "socket-util.h" |
81eca919 TG |
34 | #include "sd-rtnl.h" |
35 | #include "rtnl-util.h" | |
af5bc85d | 36 | |
f3fc4815 TG |
37 | /* this is hardcoded in the kernel, so don't look it up */ |
38 | #define LOOPBACK_IFINDEX 1 | |
5a723174 | 39 | |
f3fc4815 | 40 | static int start_loopback(sd_rtnl *rtnl) { |
cf6a8911 | 41 | _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL; |
81eca919 | 42 | int r; |
af5bc85d | 43 | |
f3fc4815 | 44 | r = sd_rtnl_message_new_link(rtnl, &req, RTM_SETLINK, LOOPBACK_IFINDEX); |
fc25d7f8 TG |
45 | if (r < 0) |
46 | return r; | |
47 | ||
5d4795f3 | 48 | r = sd_rtnl_message_link_set_flags(req, IFF_UP, IFF_UP); |
81eca919 TG |
49 | if (r < 0) |
50 | return r; | |
af5bc85d | 51 | |
f3fc4815 | 52 | r = sd_rtnl_call(rtnl, req, 0, NULL); |
81eca919 TG |
53 | if (r < 0) |
54 | return r; | |
af5bc85d LP |
55 | |
56 | return 0; | |
57 | } | |
58 | ||
e95e909d TG |
59 | static bool check_loopback(sd_rtnl *rtnl) { |
60 | _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL, *reply = NULL; | |
61 | unsigned flags; | |
e62d8c39 | 62 | int r; |
e95e909d TG |
63 | |
64 | r = sd_rtnl_message_new_link(rtnl, &req, RTM_GETLINK, LOOPBACK_IFINDEX); | |
65 | if (r < 0) | |
2f0af4e1 | 66 | return false; |
e95e909d TG |
67 | |
68 | r = sd_rtnl_call(rtnl, req, 0, &reply); | |
69 | if (r < 0) | |
2f0af4e1 | 70 | return false; |
e95e909d TG |
71 | |
72 | r = sd_rtnl_message_link_get_flags(reply, &flags); | |
73 | if (r < 0) | |
2f0af4e1 | 74 | return false; |
e95e909d TG |
75 | |
76 | return flags & IFF_UP; | |
2c3ff76e LP |
77 | } |
78 | ||
af5bc85d | 79 | int loopback_setup(void) { |
cf6a8911 | 80 | _cleanup_rtnl_unref_ sd_rtnl *rtnl = NULL; |
f3fc4815 | 81 | int r; |
0a0dc69b | 82 | |
151b9b96 | 83 | r = sd_rtnl_open(&rtnl, 0); |
2c3ff76e | 84 | if (r < 0) |
81eca919 | 85 | return r; |
af5bc85d | 86 | |
f3fc4815 TG |
87 | r = start_loopback(rtnl); |
88 | if (r == -EPERM) { | |
e95e909d | 89 | if (!check_loopback(rtnl)) |
23bbb0de MS |
90 | return log_warning_errno(EPERM, "Failed to configure loopback device: %m"); |
91 | } else if (r < 0) | |
92 | return log_warning_errno(r, "Failed to configure loopback device: %m"); | |
af5bc85d | 93 | |
2c3ff76e | 94 | |
e62d8c39 | 95 | return 0; |
af5bc85d | 96 | } |