chiark / gitweb /
0d7d00cfc00eea8109e215c01e028b7aba3160ae
[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 "util.h"
31 #include "macro.h"
32 #include "loopback-setup.h"
33 #include "socket-util.h"
34 #include "sd-rtnl.h"
35 #include "rtnl-util.h"
36
37 /* this is hardcoded in the kernel, so don't look it up */
38 #define LOOPBACK_IFINDEX 1
39
40 static int start_loopback(sd_rtnl *rtnl) {
41         _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL;
42         int r;
43
44         r = sd_rtnl_message_new_link(rtnl, &req, RTM_SETLINK, LOOPBACK_IFINDEX);
45         if (r < 0)
46                 return r;
47
48         r = sd_rtnl_message_link_set_flags(req, IFF_UP, IFF_UP);
49         if (r < 0)
50                 return r;
51
52         r = sd_rtnl_call(rtnl, req, 0, NULL);
53         if (r < 0)
54                 return r;
55
56         return 0;
57 }
58
59 static bool check_loopback(sd_rtnl *rtnl) {
60         _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL, *reply = NULL;
61         unsigned flags;
62         int r;
63
64         r = sd_rtnl_message_new_link(rtnl, &req, RTM_GETLINK, LOOPBACK_IFINDEX);
65         if (r < 0)
66                 return r;
67
68         r = sd_rtnl_call(rtnl, req, 0, &reply);
69         if (r < 0)
70                 return r;
71
72         r = sd_rtnl_message_link_get_flags(reply, &flags);
73         if (r < 0)
74                 return r;
75
76         return flags & IFF_UP;
77 }
78
79 int loopback_setup(void) {
80         _cleanup_rtnl_unref_ sd_rtnl *rtnl = NULL;
81         int r;
82
83         r = sd_rtnl_open(&rtnl, 0);
84         if (r < 0)
85                 return r;
86
87         r = start_loopback(rtnl);
88         if (r == -EPERM) {
89                 if (!check_loopback(rtnl))
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");
93
94
95         return 0;
96 }