]> git.ipfire.org Git - people/ms/systemd.git/blame - loopback-setup.c
device: allow easy identification of network interfaces without their full sysfs...
[people/ms/systemd.git] / loopback-setup.c
CommitLineData
af5bc85d
LP
1/*-*- Mode: C; c-basic-offset: 8 -*-*/
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 General Public License as published by
10 the Free Software Foundation; either version 2 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 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
22#include <errno.h>
23#include <sys/socket.h>
24#include <net/if.h>
25#include <asm/types.h>
26#include <netinet/in.h>
27#include <string.h>
28#include <stdlib.h>
29#include <unistd.h>
30#include <linux/netlink.h>
31#include <linux/rtnetlink.h>
32
33#include "util.h"
34#include "macro.h"
35#include "loopback-setup.h"
36
37enum {
38 REQUEST_NONE = 0,
39 REQUEST_ADDRESS_IPV4 = 1,
40 REQUEST_ADDRESS_IPV6 = 2,
41 REQUEST_FLAGS = 4,
42 REQUEST_ALL = 7
43};
44
45#define NLMSG_TAIL(nmsg) \
46 ((struct rtattr *) (((uint8_t*) (nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len)))
47
48static int add_rtattr(struct nlmsghdr *n, size_t max_length, int type, const void *data, size_t data_length) {
49 size_t length;
50 struct rtattr *rta;
51
52 length = RTA_LENGTH(data_length);
53
54 if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(length) > max_length)
55 return -E2BIG;
56
57 rta = NLMSG_TAIL(n);
58 rta->rta_type = type;
59 rta->rta_len = length;
60 memcpy(RTA_DATA(rta), data, data_length);
61 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(length);
62
63 return 0;
64}
65
66static ssize_t sendto_loop(int fd, const void *buf, size_t buf_len, int flags, const struct sockaddr *sa, socklen_t sa_len) {
67
68 for (;;) {
69 ssize_t l;
70
71 if ((l = sendto(fd, buf, buf_len, flags, sa, sa_len)) >= 0)
72 return l;
73
74 if (errno != EINTR)
75 return -errno;
76 }
77}
78
79static ssize_t recvfrom_loop(int fd, void *buf, size_t buf_len, int flags, struct sockaddr *sa, socklen_t *sa_len) {
80
81 for (;;) {
82 ssize_t l;
83
84 if ((l = recvfrom(fd, buf, buf_len, flags, sa, sa_len)) >= 0)
85 return l;
86
87 if (errno != EINTR)
88 return -errno;
89 }
90}
91
92static int add_adresses(int fd, int if_loopback) {
93 union {
94 struct sockaddr sa;
95 struct sockaddr_nl nl;
96 } sa;
97 union {
98 struct nlmsghdr header;
99 uint8_t buf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) +
100 NLMSG_ALIGN(sizeof(struct ifaddrmsg)) +
101 RTA_LENGTH(sizeof(struct in6_addr))];
102 } request;
103
104 struct ifaddrmsg *ifaddrmsg;
105 uint32_t ipv4_address = htonl(INADDR_LOOPBACK);
106 int r;
107
108 zero(request);
109
110 request.header.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
111 request.header.nlmsg_type = RTM_NEWADDR;
112 request.header.nlmsg_flags = NLM_F_REQUEST|NLM_F_CREATE|NLM_F_ACK;
113 request.header.nlmsg_seq = REQUEST_ADDRESS_IPV4;
114
115 ifaddrmsg = NLMSG_DATA(&request.header);
116 ifaddrmsg->ifa_family = AF_INET;
117 ifaddrmsg->ifa_prefixlen = 8;
118 ifaddrmsg->ifa_flags = IFA_F_PERMANENT;
119 ifaddrmsg->ifa_scope = RT_SCOPE_HOST;
120 ifaddrmsg->ifa_index = if_loopback;
121
122 if ((r = add_rtattr(&request.header, sizeof(request), IFA_LOCAL, &ipv4_address, sizeof(ipv4_address))) < 0)
123 return r;
124
125 zero(sa);
126 sa.nl.nl_family = AF_NETLINK;
127
128 if (sendto_loop(fd, &request, request.header.nlmsg_len, 0, &sa.sa, sizeof(sa)) < 0)
129 return -errno;
130
131 request.header.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
132 request.header.nlmsg_seq = REQUEST_ADDRESS_IPV6;
133
134 ifaddrmsg->ifa_family = AF_INET6;
135 ifaddrmsg->ifa_prefixlen = 128;
136
137 if ((r = add_rtattr(&request.header, sizeof(request), IFA_LOCAL, &in6addr_loopback, sizeof(in6addr_loopback))) < 0)
138 return r;
139
140 if (sendto_loop(fd, &request, request.header.nlmsg_len, 0, &sa.sa, sizeof(sa)) < 0)
141 return -errno;
142
143 return 0;
144}
145
146static int start_interface(int fd, int if_loopback) {
147 union {
148 struct sockaddr sa;
149 struct sockaddr_nl nl;
150 } sa;
151 union {
152 struct nlmsghdr header;
153 uint8_t buf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) +
154 NLMSG_ALIGN(sizeof(struct ifinfomsg))];
155 } request;
156
157 struct ifinfomsg *ifinfomsg;
158
159 zero(request);
160
161 request.header.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
162 request.header.nlmsg_type = RTM_NEWLINK;
163 request.header.nlmsg_flags = NLM_F_REQUEST|NLM_F_ACK;
164 request.header.nlmsg_seq = REQUEST_FLAGS;
165
166 ifinfomsg = NLMSG_DATA(&request.header);
167 ifinfomsg->ifi_family = AF_UNSPEC;
168 ifinfomsg->ifi_index = if_loopback;
169 ifinfomsg->ifi_flags = IFF_UP;
170 ifinfomsg->ifi_change = IFF_UP;
171
172 zero(sa);
173 sa.nl.nl_family = AF_NETLINK;
174
175 if (sendto_loop(fd, &request, request.header.nlmsg_len, 0, &sa.sa, sizeof(sa)) < 0)
176 return -errno;
177
178 return 0;
179}
180
181static int read_response(int fd) {
182 union {
183 struct sockaddr sa;
184 struct sockaddr_nl nl;
185 } sa;
186 union {
187 struct nlmsghdr header;
188 uint8_t buf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) +
189 NLMSG_ALIGN(sizeof(struct nlmsgerr))];
190 } response;
191
192 ssize_t l;
193 socklen_t sa_len = sizeof(sa);
194 struct nlmsgerr *nlmsgerr;
195
196 if ((l = recvfrom_loop(fd, &response, sizeof(response), 0, &sa.sa, &sa_len)) < 0)
197 return -errno;
198
199 if (sa_len != sizeof(sa.nl) ||
200 sa.nl.nl_family != AF_NETLINK)
201 return -EIO;
202
203 if (sa.nl.nl_pid != 0)
204 return 0;
205
206 if ((size_t) l < sizeof(struct nlmsghdr))
207 return -EIO;
208
209 if (response.header.nlmsg_type != NLMSG_ERROR ||
210 (pid_t) response.header.nlmsg_pid != getpid() ||
211 response.header.nlmsg_seq >= REQUEST_ALL)
212 return 0;
213
214 if ((size_t) l < NLMSG_LENGTH(sizeof(struct nlmsgerr)) ||
215 response.header.nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr)))
216 return -EIO;
217
218 nlmsgerr = NLMSG_DATA(&response.header);
219
220 if (nlmsgerr->error < 0 && nlmsgerr->error != -EEXIST)
221 return nlmsgerr->error;
222
223 return response.header.nlmsg_seq;
224}
225
226int loopback_setup(void) {
227 int r, if_loopback;
228 union {
229 struct sockaddr sa;
230 struct sockaddr_nl nl;
231 struct sockaddr_storage storage;
232 } sa;
233 int requests = REQUEST_NONE;
234
235 int fd;
236
237 errno = 0;
238 if ((if_loopback = (int) if_nametoindex("lo")) <= 0)
239 return errno ? -errno : -ENODEV;
240
241 if ((fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) < 0)
242 return -errno;
243
244 zero(sa);
245 sa.nl.nl_family = AF_NETLINK;
246
247 if (bind(fd, &sa.sa, sizeof(sa)) < 0) {
248 r = -errno;
249 goto finish;
250 }
251
252 if ((r = add_adresses(fd, if_loopback)) < 0)
253 goto finish;
254
255 if ((r = start_interface(fd, if_loopback)) < 0)
256 goto finish;
257
258 do {
259 if ((r = read_response(fd)) < 0)
260 goto finish;
261
262 requests |= r;
263
264 } while (requests != REQUEST_ALL);
265
266 r = 0;
267
268finish:
269 if (r < 0)
270 log_error("Failed to configure loopback device: %s", strerror(-r));
271
272 if (fd >= 0)
273 close_nointr_nofail(fd);
274
275 return r;
276}