jcs's openbsd hax
openbsd
1/* $OpenBSD: control.c,v 1.16 2024/11/21 13:38:14 claudio 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/un.h>
22
23#include <errno.h>
24#include <stdlib.h>
25#include <string.h>
26#include <unistd.h>
27
28#include "eigrpd.h"
29#include "eigrpe.h"
30#include "control.h"
31#include "log.h"
32
33TAILQ_HEAD(ctl_conns, ctl_conn) ctl_conns = TAILQ_HEAD_INITIALIZER(ctl_conns);
34
35#define CONTROL_BACKLOG 5
36
37static void control_accept(int, short, void *);
38static struct ctl_conn *control_connbyfd(int);
39static struct ctl_conn *control_connbypid(pid_t);
40static void control_close(int);
41static void control_dispatch_imsg(int, short, void *);
42
43struct {
44 struct event ev;
45 struct event evt;
46 int fd;
47} control_state;
48
49
50int
51control_init(char *path)
52{
53 struct sockaddr_un sun;
54 int fd;
55 mode_t old_umask;
56
57 if ((fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
58 0)) == -1) {
59 log_warn("%s: socket", __func__);
60 return (-1);
61 }
62
63 memset(&sun, 0, sizeof(sun));
64 sun.sun_family = AF_UNIX;
65 strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
66
67 if (unlink(path) == -1)
68 if (errno != ENOENT) {
69 log_warn("%s: unlink %s", __func__, path);
70 close(fd);
71 return (-1);
72 }
73
74 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
75 if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
76 log_warn("%s: bind: %s", __func__, path);
77 close(fd);
78 umask(old_umask);
79 return (-1);
80 }
81 umask(old_umask);
82
83 if (chmod(path, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) == -1) {
84 log_warn("%s: chmod", __func__);
85 close(fd);
86 (void)unlink(path);
87 return (-1);
88 }
89
90 control_state.fd = fd;
91
92 return (0);
93}
94
95int
96control_listen(void)
97{
98 if (listen(control_state.fd, CONTROL_BACKLOG) == -1) {
99 log_warn("%s: listen", __func__);
100 return (-1);
101 }
102
103 event_set(&control_state.ev, control_state.fd, EV_READ,
104 control_accept, NULL);
105 event_add(&control_state.ev, NULL);
106 evtimer_set(&control_state.evt, control_accept, NULL);
107
108 return (0);
109}
110
111static void
112control_accept(int listenfd, short event, void *bula)
113{
114 int connfd;
115 socklen_t len;
116 struct sockaddr_un sun;
117 struct ctl_conn *c;
118
119 event_add(&control_state.ev, NULL);
120 if ((event & EV_TIMEOUT))
121 return;
122
123 len = sizeof(sun);
124 if ((connfd = accept4(listenfd, (struct sockaddr *)&sun, &len,
125 SOCK_CLOEXEC | SOCK_NONBLOCK)) == -1) {
126 /*
127 * Pause accept if we are out of file descriptors, or
128 * libevent will haunt us here too.
129 */
130 if (errno == ENFILE || errno == EMFILE) {
131 struct timeval evtpause = { 1, 0 };
132
133 event_del(&control_state.ev);
134 evtimer_add(&control_state.evt, &evtpause);
135 } else if (errno != EWOULDBLOCK && errno != EINTR &&
136 errno != ECONNABORTED)
137 log_warn("%s: accept4", __func__);
138 return;
139 }
140
141 if ((c = calloc(1, sizeof(struct ctl_conn))) == NULL) {
142 log_warn("%s: calloc", __func__);
143 close(connfd);
144 return;
145 }
146
147 if (imsgbuf_init(&c->iev.ibuf, connfd) == -1) {
148 log_warn("%s: imsgbuf_init", __func__);
149 close(connfd);
150 free(c);
151 return;
152 }
153 c->iev.handler = control_dispatch_imsg;
154 c->iev.events = EV_READ;
155 event_set(&c->iev.ev, c->iev.ibuf.fd, c->iev.events,
156 c->iev.handler, &c->iev);
157 event_add(&c->iev.ev, NULL);
158
159 TAILQ_INSERT_TAIL(&ctl_conns, c, entry);
160}
161
162static struct ctl_conn *
163control_connbyfd(int fd)
164{
165 struct ctl_conn *c;
166
167 TAILQ_FOREACH(c, &ctl_conns, entry) {
168 if (c->iev.ibuf.fd == fd)
169 break;
170 }
171
172 return (c);
173}
174
175static struct ctl_conn *
176control_connbypid(pid_t pid)
177{
178 struct ctl_conn *c;
179
180 TAILQ_FOREACH(c, &ctl_conns, entry) {
181 if (c->iev.ibuf.pid == pid)
182 break;
183 }
184
185 return (c);
186}
187
188static void
189control_close(int fd)
190{
191 struct ctl_conn *c;
192
193 if ((c = control_connbyfd(fd)) == NULL) {
194 log_warnx("%s: fd %d: not found", __func__, fd);
195 return;
196 }
197
198 imsgbuf_clear(&c->iev.ibuf);
199 TAILQ_REMOVE(&ctl_conns, c, entry);
200
201 event_del(&c->iev.ev);
202 close(c->iev.ibuf.fd);
203
204 /* Some file descriptors are available again. */
205 if (evtimer_pending(&control_state.evt, NULL)) {
206 evtimer_del(&control_state.evt);
207 event_add(&control_state.ev, NULL);
208 }
209
210 free(c);
211}
212
213static void
214control_dispatch_imsg(int fd, short event, void *bula)
215{
216 struct ctl_conn *c;
217 struct imsg imsg;
218 ssize_t n;
219 unsigned int ifidx;
220 int verbose;
221
222 if ((c = control_connbyfd(fd)) == NULL) {
223 log_warnx("%s: fd %d: not found", __func__, fd);
224 return;
225 }
226
227 if (event & EV_READ) {
228 if (imsgbuf_read(&c->iev.ibuf) != 1) {
229 control_close(fd);
230 return;
231 }
232 }
233 if (event & EV_WRITE) {
234 if (imsgbuf_write(&c->iev.ibuf) == -1) {
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 case IMSG_CTL_RELOAD:
253 c->iev.ibuf.pid = imsg.hdr.pid;
254 eigrpe_imsg_compose_parent(imsg.hdr.type, 0, NULL, 0);
255 break;
256 case IMSG_CTL_KROUTE:
257 case IMSG_CTL_IFINFO:
258 c->iev.ibuf.pid = imsg.hdr.pid;
259 eigrpe_imsg_compose_parent(imsg.hdr.type, imsg.hdr.pid,
260 imsg.data, imsg.hdr.len - IMSG_HEADER_SIZE);
261 break;
262 case IMSG_CTL_SHOW_INTERFACE:
263 if (imsg.hdr.len != IMSG_HEADER_SIZE +
264 sizeof(ifidx))
265 break;
266
267 memcpy(&ifidx, imsg.data, sizeof(ifidx));
268 eigrpe_iface_ctl(c, ifidx);
269 imsg_compose_event(&c->iev, IMSG_CTL_END, 0,
270 0, -1, NULL, 0);
271 break;
272 case IMSG_CTL_SHOW_TOPOLOGY:
273 if (imsg.hdr.len != IMSG_HEADER_SIZE +
274 sizeof(struct ctl_show_topology_req))
275 break;
276
277 c->iev.ibuf.pid = imsg.hdr.pid;
278 eigrpe_imsg_compose_rde(imsg.hdr.type, 0, imsg.hdr.pid,
279 imsg.data, imsg.hdr.len - IMSG_HEADER_SIZE);
280 break;
281 case IMSG_CTL_SHOW_NBR:
282 eigrpe_nbr_ctl(c);
283 break;
284 case IMSG_CTL_SHOW_STATS:
285 eigrpe_stats_ctl(c);
286 break;
287 case IMSG_CTL_CLEAR_NBR:
288 if (imsg.hdr.len != IMSG_HEADER_SIZE +
289 sizeof(struct ctl_nbr))
290 break;
291
292 nbr_clear_ctl(imsg.data);
293 break;
294 case IMSG_CTL_LOG_VERBOSE:
295 if (imsg.hdr.len != IMSG_HEADER_SIZE +
296 sizeof(verbose))
297 break;
298
299 /* forward to other processes */
300 eigrpe_imsg_compose_parent(imsg.hdr.type, imsg.hdr.pid,
301 imsg.data, imsg.hdr.len - IMSG_HEADER_SIZE);
302 eigrpe_imsg_compose_rde(imsg.hdr.type, 0, imsg.hdr.pid,
303 imsg.data, imsg.hdr.len - IMSG_HEADER_SIZE);
304
305 memcpy(&verbose, imsg.data, sizeof(verbose));
306 log_verbose(verbose);
307 break;
308 default:
309 log_debug("%s: error handling imsg %d", __func__,
310 imsg.hdr.type);
311 break;
312 }
313 imsg_free(&imsg);
314 }
315
316 imsg_event_add(&c->iev);
317}
318
319int
320control_imsg_relay(struct imsg *imsg)
321{
322 struct ctl_conn *c;
323
324 if ((c = control_connbypid(imsg->hdr.pid)) == NULL)
325 return (0);
326
327 return (imsg_compose_event(&c->iev, imsg->hdr.type, 0, imsg->hdr.pid,
328 -1, imsg->data, imsg->hdr.len - IMSG_HEADER_SIZE));
329}