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