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