xref: /openbsd-src/usr.sbin/ldpd/control.c (revision a0747c9f67a4ae71ccb71e62a28d1ea19e06a63c)
1 /*	$OpenBSD: control.c,v 1.31 2021/01/19 15:14:35 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 #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 
32 TAILQ_HEAD(ctl_conns, ctl_conn) ctl_conns = TAILQ_HEAD_INITIALIZER(ctl_conns);
33 
34 #define	CONTROL_BACKLOG	5
35 
36 static void		 control_accept(int, short, void *);
37 static struct ctl_conn	*control_connbyfd(int);
38 static struct ctl_conn	*control_connbypid(pid_t);
39 static void		 control_close(int);
40 static void		 control_dispatch_imsg(int, short, void *);
41 
42 static int		 control_fd;
43 
44 int
45 control_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 
89 int
90 control_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 
100 void
101 control_cleanup(void)
102 {
103 	accept_del(control_fd);
104 	close(control_fd);
105 }
106 
107 /* ARGSUSED */
108 static void
109 control_accept(int listenfd, short event, void *bula)
110 {
111 	int			 connfd;
112 	socklen_t		 len;
113 	struct sockaddr_un	 sun;
114 	struct ctl_conn		*c;
115 
116 	len = sizeof(sun);
117 	if ((connfd = accept4(listenfd, (struct sockaddr *)&sun, &len,
118 	    SOCK_NONBLOCK | SOCK_CLOEXEC)) == -1) {
119 		/*
120 		 * Pause accept if we are out of file descriptors, or
121 		 * libevent will haunt us here too.
122 		 */
123 		if (errno == ENFILE || errno == EMFILE)
124 			accept_pause();
125 		else if (errno != EWOULDBLOCK && errno != EINTR &&
126 		    errno != ECONNABORTED)
127 			log_warn("%s: accept4", __func__);
128 		return;
129 	}
130 
131 	if ((c = calloc(1, sizeof(struct ctl_conn))) == NULL) {
132 		log_warn(__func__);
133 		close(connfd);
134 		return;
135 	}
136 
137 	imsg_init(&c->iev.ibuf, connfd);
138 	c->iev.handler = control_dispatch_imsg;
139 	c->iev.events = EV_READ;
140 	event_set(&c->iev.ev, c->iev.ibuf.fd, c->iev.events,
141 	    c->iev.handler, &c->iev);
142 	event_add(&c->iev.ev, NULL);
143 
144 	TAILQ_INSERT_TAIL(&ctl_conns, c, entry);
145 }
146 
147 static struct ctl_conn *
148 control_connbyfd(int fd)
149 {
150 	struct ctl_conn	*c;
151 
152 	TAILQ_FOREACH(c, &ctl_conns, entry) {
153 		if (c->iev.ibuf.fd == fd)
154 			break;
155 	}
156 
157 	return (c);
158 }
159 
160 static struct ctl_conn *
161 control_connbypid(pid_t pid)
162 {
163 	struct ctl_conn	*c;
164 
165 	TAILQ_FOREACH(c, &ctl_conns, entry) {
166 		if (c->iev.ibuf.pid == pid)
167 			break;
168 	}
169 
170 	return (c);
171 }
172 
173 static void
174 control_close(int fd)
175 {
176 	struct ctl_conn	*c;
177 
178 	if ((c = control_connbyfd(fd)) == NULL) {
179 		log_warnx("%s: fd %d: not found", __func__, fd);
180 		return;
181 	}
182 
183 	msgbuf_clear(&c->iev.ibuf.w);
184 	TAILQ_REMOVE(&ctl_conns, c, entry);
185 
186 	event_del(&c->iev.ev);
187 	close(c->iev.ibuf.fd);
188 	accept_unpause();
189 	free(c);
190 }
191 
192 /* ARGSUSED */
193 static void
194 control_dispatch_imsg(int fd, short event, void *bula)
195 {
196 	struct ctl_conn	*c;
197 	struct imsg	 imsg;
198 	ssize_t		 n;
199 	unsigned int	 ifidx;
200 	int		 verbose;
201 
202 	if ((c = control_connbyfd(fd)) == NULL) {
203 		log_warnx("%s: fd %d: not found", __func__, fd);
204 		return;
205 	}
206 
207 	if (event & EV_READ) {
208 		if (((n = imsg_read(&c->iev.ibuf)) == -1 && errno != EAGAIN) ||
209 		    n == 0) {
210 			control_close(fd);
211 			return;
212 		}
213 	}
214 	if (event & EV_WRITE) {
215 		if (msgbuf_write(&c->iev.ibuf.w) <= 0 && errno != EAGAIN) {
216 			control_close(fd);
217 			return;
218 		}
219 	}
220 
221 	for (;;) {
222 		if ((n = imsg_get(&c->iev.ibuf, &imsg)) == -1) {
223 			control_close(fd);
224 			return;
225 		}
226 
227 		if (n == 0)
228 			break;
229 
230 		switch (imsg.hdr.type) {
231 		case IMSG_CTL_FIB_COUPLE:
232 		case IMSG_CTL_FIB_DECOUPLE:
233 		case IMSG_CTL_RELOAD:
234 			c->iev.ibuf.pid = imsg.hdr.pid;
235 			ldpe_imsg_compose_parent(imsg.hdr.type, 0, NULL, 0);
236 			break;
237 		case IMSG_CTL_KROUTE:
238 		case IMSG_CTL_KROUTE_ADDR:
239 		case IMSG_CTL_IFINFO:
240 			c->iev.ibuf.pid = imsg.hdr.pid;
241 			ldpe_imsg_compose_parent(imsg.hdr.type,
242 			    imsg.hdr.pid, imsg.data,
243 			    imsg.hdr.len - IMSG_HEADER_SIZE);
244 			break;
245 		case IMSG_CTL_SHOW_INTERFACE:
246 			if (imsg.hdr.len == IMSG_HEADER_SIZE +
247 			    sizeof(ifidx)) {
248 				memcpy(&ifidx, imsg.data, sizeof(ifidx));
249 				ldpe_iface_ctl(c, ifidx);
250 				imsg_compose_event(&c->iev, IMSG_CTL_END, 0,
251 				    0, -1, NULL, 0);
252 			}
253 			break;
254 		case IMSG_CTL_SHOW_DISCOVERY:
255 			ldpe_adj_ctl(c);
256 			break;
257 		case IMSG_CTL_SHOW_LIB:
258 		case IMSG_CTL_SHOW_L2VPN_PW:
259 		case IMSG_CTL_SHOW_L2VPN_BINDING:
260 			c->iev.ibuf.pid = imsg.hdr.pid;
261 			ldpe_imsg_compose_lde(imsg.hdr.type, 0, imsg.hdr.pid,
262 			    imsg.data, imsg.hdr.len - IMSG_HEADER_SIZE);
263 			break;
264 		case IMSG_CTL_SHOW_NBR:
265 			ldpe_nbr_ctl(c);
266 			break;
267 		case IMSG_CTL_CLEAR_NBR:
268 			if (imsg.hdr.len != IMSG_HEADER_SIZE +
269 			    sizeof(struct ctl_nbr))
270 				break;
271 
272 			nbr_clear_ctl(imsg.data);
273 			break;
274 		case IMSG_CTL_LOG_VERBOSE:
275 			if (imsg.hdr.len != IMSG_HEADER_SIZE +
276 			    sizeof(verbose))
277 				break;
278 
279 			/* forward to other processes */
280 			ldpe_imsg_compose_parent(imsg.hdr.type, imsg.hdr.pid,
281 			    imsg.data, imsg.hdr.len - IMSG_HEADER_SIZE);
282 			ldpe_imsg_compose_lde(imsg.hdr.type, 0, imsg.hdr.pid,
283 			    imsg.data, imsg.hdr.len - IMSG_HEADER_SIZE);
284 
285 			memcpy(&verbose, imsg.data, sizeof(verbose));
286 			log_verbose(verbose);
287 			break;
288 		default:
289 			log_debug("%s: error handling imsg %d", __func__,
290 			    imsg.hdr.type);
291 			break;
292 		}
293 		imsg_free(&imsg);
294 	}
295 
296 	imsg_event_add(&c->iev);
297 }
298 
299 int
300 control_imsg_relay(struct imsg *imsg)
301 {
302 	struct ctl_conn	*c;
303 
304 	if ((c = control_connbypid(imsg->hdr.pid)) == NULL)
305 		return (0);
306 
307 	return (imsg_compose_event(&c->iev, imsg->hdr.type, 0, imsg->hdr.pid,
308 	    -1, imsg->data, imsg->hdr.len - IMSG_HEADER_SIZE));
309 }
310