1 |
|
|
/* $OpenBSD: control.c,v 1.44 2017/01/24 04:24:25 benno Exp $ */ |
2 |
|
|
|
3 |
|
|
/* |
4 |
|
|
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> |
5 |
|
|
* |
6 |
|
|
* Permission to use, copy, modify, and distribute this software for any |
7 |
|
|
* purpose with or without fee is hereby granted, provided that the above |
8 |
|
|
* copyright notice and this permission notice appear in all copies. |
9 |
|
|
* |
10 |
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
11 |
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
12 |
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
13 |
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
14 |
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
15 |
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
16 |
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
17 |
|
|
*/ |
18 |
|
|
|
19 |
|
|
#include <sys/types.h> |
20 |
|
|
#include <sys/stat.h> |
21 |
|
|
#include <sys/socket.h> |
22 |
|
|
#include <sys/un.h> |
23 |
|
|
#include <errno.h> |
24 |
|
|
#include <fcntl.h> |
25 |
|
|
#include <stdlib.h> |
26 |
|
|
#include <string.h> |
27 |
|
|
#include <unistd.h> |
28 |
|
|
|
29 |
|
|
#include "ospfd.h" |
30 |
|
|
#include "ospf.h" |
31 |
|
|
#include "ospfe.h" |
32 |
|
|
#include "log.h" |
33 |
|
|
#include "control.h" |
34 |
|
|
|
35 |
|
|
#define CONTROL_BACKLOG 5 |
36 |
|
|
|
37 |
|
|
struct ctl_conn *control_connbyfd(int); |
38 |
|
|
struct ctl_conn *control_connbypid(pid_t); |
39 |
|
|
void control_close(int); |
40 |
|
|
|
41 |
|
|
int |
42 |
|
|
control_init(char *path) |
43 |
|
|
{ |
44 |
|
|
struct sockaddr_un sun; |
45 |
|
|
int fd; |
46 |
|
|
mode_t old_umask; |
47 |
|
|
|
48 |
|
|
if ((fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, |
49 |
|
|
0)) == -1) { |
50 |
|
|
log_warn("control_init: socket"); |
51 |
|
|
return (-1); |
52 |
|
|
} |
53 |
|
|
|
54 |
|
|
bzero(&sun, sizeof(sun)); |
55 |
|
|
sun.sun_family = AF_UNIX; |
56 |
|
|
strlcpy(sun.sun_path, path, sizeof(sun.sun_path)); |
57 |
|
|
|
58 |
|
|
if (unlink(path) == -1) |
59 |
|
|
if (errno != ENOENT) { |
60 |
|
|
log_warn("control_init: unlink %s", path); |
61 |
|
|
close(fd); |
62 |
|
|
return (-1); |
63 |
|
|
} |
64 |
|
|
|
65 |
|
|
old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH); |
66 |
|
|
if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) { |
67 |
|
|
log_warn("control_init: bind: %s", path); |
68 |
|
|
close(fd); |
69 |
|
|
umask(old_umask); |
70 |
|
|
return (-1); |
71 |
|
|
} |
72 |
|
|
umask(old_umask); |
73 |
|
|
|
74 |
|
|
if (chmod(path, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) == -1) { |
75 |
|
|
log_warn("control_init: chmod"); |
76 |
|
|
close(fd); |
77 |
|
|
(void)unlink(path); |
78 |
|
|
return (-1); |
79 |
|
|
} |
80 |
|
|
|
81 |
|
|
control_state.fd = fd; |
82 |
|
|
|
83 |
|
|
return (0); |
84 |
|
|
} |
85 |
|
|
|
86 |
|
|
int |
87 |
|
|
control_listen(void) |
88 |
|
|
{ |
89 |
|
|
|
90 |
|
|
if (listen(control_state.fd, CONTROL_BACKLOG) == -1) { |
91 |
|
|
log_warn("control_listen: listen"); |
92 |
|
|
return (-1); |
93 |
|
|
} |
94 |
|
|
|
95 |
|
|
event_set(&control_state.ev, control_state.fd, EV_READ, |
96 |
|
|
control_accept, NULL); |
97 |
|
|
event_add(&control_state.ev, NULL); |
98 |
|
|
evtimer_set(&control_state.evt, control_accept, NULL); |
99 |
|
|
|
100 |
|
|
return (0); |
101 |
|
|
} |
102 |
|
|
|
103 |
|
|
void |
104 |
|
|
control_cleanup(char *path) |
105 |
|
|
{ |
106 |
✓✗ |
48 |
if (path == NULL) |
107 |
|
|
return; |
108 |
|
24 |
event_del(&control_state.ev); |
109 |
|
24 |
event_del(&control_state.evt); |
110 |
|
24 |
unlink(path); |
111 |
|
48 |
} |
112 |
|
|
|
113 |
|
|
/* ARGSUSED */ |
114 |
|
|
void |
115 |
|
|
control_accept(int listenfd, short event, void *bula) |
116 |
|
|
{ |
117 |
|
|
int connfd; |
118 |
|
|
socklen_t len; |
119 |
|
|
struct sockaddr_un sun; |
120 |
|
|
struct ctl_conn *c; |
121 |
|
|
|
122 |
|
|
event_add(&control_state.ev, NULL); |
123 |
|
|
if ((event & EV_TIMEOUT)) |
124 |
|
|
return; |
125 |
|
|
|
126 |
|
|
len = sizeof(sun); |
127 |
|
|
if ((connfd = accept4(listenfd, (struct sockaddr *)&sun, &len, |
128 |
|
|
SOCK_CLOEXEC | SOCK_NONBLOCK)) == -1) { |
129 |
|
|
/* |
130 |
|
|
* Pause accept if we are out of file descriptors, or |
131 |
|
|
* libevent will haunt us here too. |
132 |
|
|
*/ |
133 |
|
|
if (errno == ENFILE || errno == EMFILE) { |
134 |
|
|
struct timeval evtpause = { 1, 0 }; |
135 |
|
|
|
136 |
|
|
event_del(&control_state.ev); |
137 |
|
|
evtimer_add(&control_state.evt, &evtpause); |
138 |
|
|
} else if (errno != EWOULDBLOCK && errno != EINTR && |
139 |
|
|
errno != ECONNABORTED) |
140 |
|
|
log_warn("control_accept: accept"); |
141 |
|
|
return; |
142 |
|
|
} |
143 |
|
|
|
144 |
|
|
if ((c = calloc(1, sizeof(struct ctl_conn))) == NULL) { |
145 |
|
|
log_warn("control_accept"); |
146 |
|
|
close(connfd); |
147 |
|
|
return; |
148 |
|
|
} |
149 |
|
|
|
150 |
|
|
imsg_init(&c->iev.ibuf, connfd); |
151 |
|
|
c->iev.handler = control_dispatch_imsg; |
152 |
|
|
c->iev.events = EV_READ; |
153 |
|
|
event_set(&c->iev.ev, c->iev.ibuf.fd, c->iev.events, |
154 |
|
|
c->iev.handler, &c->iev); |
155 |
|
|
event_add(&c->iev.ev, NULL); |
156 |
|
|
|
157 |
|
|
TAILQ_INSERT_TAIL(&ctl_conns, c, entry); |
158 |
|
|
} |
159 |
|
|
|
160 |
|
|
struct ctl_conn * |
161 |
|
|
control_connbyfd(int fd) |
162 |
|
|
{ |
163 |
|
|
struct ctl_conn *c; |
164 |
|
|
|
165 |
|
|
TAILQ_FOREACH(c, &ctl_conns, entry) { |
166 |
|
|
if (c->iev.ibuf.fd == fd) |
167 |
|
|
break; |
168 |
|
|
} |
169 |
|
|
|
170 |
|
|
return (c); |
171 |
|
|
} |
172 |
|
|
|
173 |
|
|
struct ctl_conn * |
174 |
|
|
control_connbypid(pid_t pid) |
175 |
|
|
{ |
176 |
|
|
struct ctl_conn *c; |
177 |
|
|
|
178 |
|
|
TAILQ_FOREACH(c, &ctl_conns, entry) { |
179 |
|
|
if (c->iev.ibuf.pid == pid) |
180 |
|
|
break; |
181 |
|
|
} |
182 |
|
|
|
183 |
|
|
return (c); |
184 |
|
|
} |
185 |
|
|
|
186 |
|
|
void |
187 |
|
|
control_close(int fd) |
188 |
|
|
{ |
189 |
|
|
struct ctl_conn *c; |
190 |
|
|
|
191 |
|
|
if ((c = control_connbyfd(fd)) == NULL) { |
192 |
|
|
log_warn("control_close: fd %d: not found", fd); |
193 |
|
|
return; |
194 |
|
|
} |
195 |
|
|
|
196 |
|
|
msgbuf_clear(&c->iev.ibuf.w); |
197 |
|
|
TAILQ_REMOVE(&ctl_conns, c, entry); |
198 |
|
|
|
199 |
|
|
event_del(&c->iev.ev); |
200 |
|
|
close(c->iev.ibuf.fd); |
201 |
|
|
|
202 |
|
|
/* Some file descriptors are available again. */ |
203 |
|
|
if (evtimer_pending(&control_state.evt, NULL)) { |
204 |
|
|
evtimer_del(&control_state.evt); |
205 |
|
|
event_add(&control_state.ev, NULL); |
206 |
|
|
} |
207 |
|
|
|
208 |
|
|
free(c); |
209 |
|
|
} |
210 |
|
|
|
211 |
|
|
/* ARGSUSED */ |
212 |
|
|
void |
213 |
|
|
control_dispatch_imsg(int fd, short event, void *bula) |
214 |
|
|
{ |
215 |
|
|
struct ctl_conn *c; |
216 |
|
|
struct imsg imsg; |
217 |
|
|
ssize_t n; |
218 |
|
|
unsigned int ifidx; |
219 |
|
|
int verbose; |
220 |
|
|
|
221 |
|
|
if ((c = control_connbyfd(fd)) == NULL) { |
222 |
|
|
log_warn("control_dispatch_imsg: fd %d: not found", fd); |
223 |
|
|
return; |
224 |
|
|
} |
225 |
|
|
|
226 |
|
|
if (event & EV_READ) { |
227 |
|
|
if (((n = imsg_read(&c->iev.ibuf)) == -1 && errno != EAGAIN) || |
228 |
|
|
n == 0) { |
229 |
|
|
control_close(fd); |
230 |
|
|
return; |
231 |
|
|
} |
232 |
|
|
} |
233 |
|
|
if (event & EV_WRITE) { |
234 |
|
|
if (msgbuf_write(&c->iev.ibuf.w) <= 0 && errno != EAGAIN) { |
235 |
|
|
control_close(fd); |
236 |
|
|
return; |
237 |
|
|
} |
238 |
|
|
} |
239 |
|
|
|
240 |
|
|
for (;;) { |
241 |
|
|
if ((n = imsg_get(&c->iev.ibuf, &imsg)) == -1) { |
242 |
|
|
control_close(fd); |
243 |
|
|
return; |
244 |
|
|
} |
245 |
|
|
|
246 |
|
|
if (n == 0) |
247 |
|
|
break; |
248 |
|
|
|
249 |
|
|
switch (imsg.hdr.type) { |
250 |
|
|
case IMSG_CTL_FIB_COUPLE: |
251 |
|
|
case IMSG_CTL_FIB_DECOUPLE: |
252 |
|
|
ospfe_fib_update(imsg.hdr.type); |
253 |
|
|
/* FALLTHROUGH */ |
254 |
|
|
case IMSG_CTL_FIB_RELOAD: |
255 |
|
|
case IMSG_CTL_RELOAD: |
256 |
|
|
c->iev.ibuf.pid = imsg.hdr.pid; |
257 |
|
|
ospfe_imsg_compose_parent(imsg.hdr.type, 0, NULL, 0); |
258 |
|
|
break; |
259 |
|
|
case IMSG_CTL_KROUTE: |
260 |
|
|
case IMSG_CTL_KROUTE_ADDR: |
261 |
|
|
case IMSG_CTL_IFINFO: |
262 |
|
|
c->iev.ibuf.pid = imsg.hdr.pid; |
263 |
|
|
ospfe_imsg_compose_parent(imsg.hdr.type, imsg.hdr.pid, |
264 |
|
|
imsg.data, imsg.hdr.len - IMSG_HEADER_SIZE); |
265 |
|
|
break; |
266 |
|
|
case IMSG_CTL_SHOW_INTERFACE: |
267 |
|
|
if (imsg.hdr.len == IMSG_HEADER_SIZE + |
268 |
|
|
sizeof(ifidx)) { |
269 |
|
|
memcpy(&ifidx, imsg.data, sizeof(ifidx)); |
270 |
|
|
ospfe_iface_ctl(c, ifidx); |
271 |
|
|
imsg_compose_event(&c->iev, IMSG_CTL_END, 0, |
272 |
|
|
0, -1, NULL, 0); |
273 |
|
|
} |
274 |
|
|
break; |
275 |
|
|
case IMSG_CTL_SHOW_DATABASE: |
276 |
|
|
case IMSG_CTL_SHOW_DB_EXT: |
277 |
|
|
case IMSG_CTL_SHOW_DB_NET: |
278 |
|
|
case IMSG_CTL_SHOW_DB_RTR: |
279 |
|
|
case IMSG_CTL_SHOW_DB_SELF: |
280 |
|
|
case IMSG_CTL_SHOW_DB_SUM: |
281 |
|
|
case IMSG_CTL_SHOW_DB_ASBR: |
282 |
|
|
case IMSG_CTL_SHOW_DB_OPAQ: |
283 |
|
|
case IMSG_CTL_SHOW_RIB: |
284 |
|
|
case IMSG_CTL_SHOW_SUM: |
285 |
|
|
c->iev.ibuf.pid = imsg.hdr.pid; |
286 |
|
|
ospfe_imsg_compose_rde(imsg.hdr.type, 0, imsg.hdr.pid, |
287 |
|
|
imsg.data, imsg.hdr.len - IMSG_HEADER_SIZE); |
288 |
|
|
break; |
289 |
|
|
case IMSG_CTL_SHOW_NBR: |
290 |
|
|
ospfe_nbr_ctl(c); |
291 |
|
|
break; |
292 |
|
|
case IMSG_CTL_LOG_VERBOSE: |
293 |
|
|
if (imsg.hdr.len != IMSG_HEADER_SIZE + |
294 |
|
|
sizeof(verbose)) |
295 |
|
|
break; |
296 |
|
|
|
297 |
|
|
/* forward to other processes */ |
298 |
|
|
ospfe_imsg_compose_parent(imsg.hdr.type, imsg.hdr.pid, |
299 |
|
|
imsg.data, imsg.hdr.len - IMSG_HEADER_SIZE); |
300 |
|
|
ospfe_imsg_compose_rde(imsg.hdr.type, 0, imsg.hdr.pid, |
301 |
|
|
imsg.data, imsg.hdr.len - IMSG_HEADER_SIZE); |
302 |
|
|
|
303 |
|
|
memcpy(&verbose, imsg.data, sizeof(verbose)); |
304 |
|
|
log_setverbose(verbose); |
305 |
|
|
break; |
306 |
|
|
default: |
307 |
|
|
log_debug("control_dispatch_imsg: " |
308 |
|
|
"error handling imsg %d", imsg.hdr.type); |
309 |
|
|
break; |
310 |
|
|
} |
311 |
|
|
imsg_free(&imsg); |
312 |
|
|
} |
313 |
|
|
|
314 |
|
|
imsg_event_add(&c->iev); |
315 |
|
|
} |
316 |
|
|
|
317 |
|
|
int |
318 |
|
|
control_imsg_relay(struct imsg *imsg) |
319 |
|
|
{ |
320 |
|
|
struct ctl_conn *c; |
321 |
|
|
|
322 |
|
|
if ((c = control_connbypid(imsg->hdr.pid)) == NULL) |
323 |
|
|
return (0); |
324 |
|
|
|
325 |
|
|
return (imsg_compose_event(&c->iev, imsg->hdr.type, 0, imsg->hdr.pid, |
326 |
|
|
-1, imsg->data, imsg->hdr.len - IMSG_HEADER_SIZE)); |
327 |
|
|
} |