GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.sbin/switchd/control.c Lines: 0 164 0.0 %
Date: 2017-11-13 Branches: 0 96 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: control.c,v 1.8 2017/01/17 22:10:56 krw Exp $	*/
2
3
/*
4
 * Copyright (c) 2010-2016 Reyk Floeter <reyk@openbsd.org>
5
 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
6
 *
7
 * Permission to use, copy, modify, and distribute this software for any
8
 * purpose with or without fee is hereby granted, provided that the above
9
 * copyright notice and this permission notice appear in all copies.
10
 *
11
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18
 */
19
20
#include <sys/queue.h>
21
#include <sys/stat.h>
22
#include <sys/socket.h>
23
#include <sys/un.h>
24
#include <sys/tree.h>
25
26
#include <net/if.h>
27
28
#include <errno.h>
29
#include <event.h>
30
#include <fcntl.h>
31
#include <stdlib.h>
32
#include <string.h>
33
#include <unistd.h>
34
#include <signal.h>
35
#include <imsg.h>
36
37
#include "proc.h"
38
#include "switchd.h"
39
40
#define	CONTROL_BACKLOG	5
41
42
struct ctl_connlist ctl_conns;
43
44
void
45
	 control_accept(int, short, void *);
46
struct ctl_conn
47
	*control_connbyfd(int);
48
void	 control_close(int, struct control_sock *);
49
void	 control_dispatch_imsg(int, short, void *);
50
void	 control_imsg_forward(struct imsg *);
51
void	 control_run(struct privsep *, struct privsep_proc *, void *);
52
53
int	 control_dispatch_ofp(int, struct privsep_proc *, struct imsg *);
54
55
static struct privsep_proc procs[] = {
56
	{ "ofp",	PROC_OFP,	control_dispatch_ofp },
57
	{ "parent",	PROC_PARENT,	NULL },
58
	{ "ofcconn",	PROC_OFCCONN,	NULL }
59
};
60
61
void
62
control(struct privsep *ps, struct privsep_proc *p)
63
{
64
	proc_run(ps, p, procs, nitems(procs), control_run, NULL);
65
}
66
67
void
68
control_run(struct privsep *ps, struct privsep_proc *p, void *arg)
69
{
70
	/*
71
	 * pledge in the control process:
72
	 * stdio - for malloc and basic I/O including events.
73
	 * cpath - for managing the control socket.
74
	 * unix - for the control socket.
75
	 * recvfd - for the proc fd exchange.
76
	 */
77
	if (pledge("stdio cpath unix recvfd flock rpath wpath", NULL) == -1)
78
		fatal("pledge");
79
}
80
81
int
82
control_dispatch_ofp(int fd, struct privsep_proc *p, struct imsg *imsg)
83
{
84
	int		 cfd;
85
	struct ctl_conn	*c;
86
	uint8_t		*d = imsg->data;
87
	size_t		 s;
88
89
	switch (imsg->hdr.type) {
90
	case IMSG_CTL_SWITCH:
91
	case IMSG_CTL_MAC:
92
		IMSG_SIZE_CHECK(imsg, &cfd);
93
		memcpy(&cfd, d, sizeof(cfd));
94
95
		if ((c = control_connbyfd(cfd)) == NULL)
96
			fatalx("invalid control connection");
97
98
		s = IMSG_DATA_SIZE(imsg) - sizeof(cfd);
99
		d += sizeof(cfd);
100
		imsg_compose_event(&c->iev, imsg->hdr.type, 0, 0, -1, d, s);
101
		return (0);
102
	case IMSG_CTL_END:
103
		IMSG_SIZE_CHECK(imsg, &cfd);
104
		memcpy(&cfd, d, sizeof(cfd));
105
106
		if ((c = control_connbyfd(cfd)) == NULL)
107
			fatalx("invalid control connection");
108
109
		imsg_compose_event(&c->iev, IMSG_CTL_END, 0, 0, -1, NULL, 0);
110
		return (0);
111
112
	default:
113
		break;
114
	}
115
116
	return (-1);
117
}
118
119
int
120
control_init(struct privsep *ps, struct control_sock *cs)
121
{
122
	struct switchd		*env = ps->ps_env;
123
	struct sockaddr_un	 sun;
124
	int			 fd;
125
	mode_t			 old_umask, mode;
126
127
	if (cs->cs_name == NULL)
128
		return (0);
129
130
	if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
131
		log_warn("%s: socket", __func__);
132
		return (-1);
133
	}
134
135
	sun.sun_family = AF_UNIX;
136
	if (strlcpy(sun.sun_path, cs->cs_name,
137
	    sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
138
		log_warn("%s: %s name too long", __func__, cs->cs_name);
139
		close(fd);
140
		return (-1);
141
	}
142
143
	if (unlink(cs->cs_name) == -1)
144
		if (errno != ENOENT) {
145
			log_warn("%s: unlink %s", __func__, cs->cs_name);
146
			close(fd);
147
			return (-1);
148
		}
149
150
	if (cs->cs_restricted) {
151
		old_umask = umask(S_IXUSR|S_IXGRP|S_IXOTH);
152
		mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
153
	} else {
154
		old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
155
		mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP;
156
	}
157
158
	if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
159
		log_warn("%s: bind: %s", __func__, cs->cs_name);
160
		close(fd);
161
		(void)umask(old_umask);
162
		return (-1);
163
	}
164
	(void)umask(old_umask);
165
166
	if (chmod(cs->cs_name, mode) == -1) {
167
		log_warn("%s: chmod", __func__);
168
		close(fd);
169
		(void)unlink(cs->cs_name);
170
		return (-1);
171
	}
172
173
	socket_set_blockmode(fd, BM_NONBLOCK);
174
	cs->cs_fd = fd;
175
	cs->cs_env = env;
176
177
	return (0);
178
}
179
180
int
181
control_listen(struct control_sock *cs)
182
{
183
	if (cs->cs_name == NULL)
184
		return (0);
185
186
	if (listen(cs->cs_fd, CONTROL_BACKLOG) == -1) {
187
		log_warn("%s: listen", __func__);
188
		return (-1);
189
	}
190
191
	event_set(&cs->cs_ev, cs->cs_fd, EV_READ,
192
	    control_accept, cs);
193
	event_add(&cs->cs_ev, NULL);
194
	evtimer_set(&cs->cs_evt, control_accept, cs);
195
196
	return (0);
197
}
198
199
void
200
control_cleanup(struct control_sock *cs)
201
{
202
	if (cs->cs_name == NULL)
203
		return;
204
	event_del(&cs->cs_ev);
205
	event_del(&cs->cs_evt);
206
	(void)unlink(cs->cs_name);
207
}
208
209
/* ARGSUSED */
210
void
211
control_accept(int listenfd, short event, void *arg)
212
{
213
	struct control_sock	*cs = arg;
214
	int			 connfd;
215
	socklen_t		 len;
216
	struct sockaddr_un	 sun;
217
	struct ctl_conn		*c;
218
219
	event_add(&cs->cs_ev, NULL);
220
	if ((event & EV_TIMEOUT))
221
		return;
222
223
	len = sizeof(sun);
224
	if ((connfd = accept(listenfd,
225
	    (struct sockaddr *)&sun, &len)) == -1) {
226
		/*
227
		 * Pause accept if we are out of file descriptors, or
228
		 * libevent will haunt us here too.
229
		 */
230
		if (errno == ENFILE || errno == EMFILE) {
231
			struct timeval evtpause = { 1, 0 };
232
233
			event_del(&cs->cs_ev);
234
			evtimer_add(&cs->cs_evt, &evtpause);
235
		} else if (errno != EWOULDBLOCK && errno != EINTR &&
236
		    errno != ECONNABORTED)
237
			log_warn("%s: accept", __func__);
238
		return;
239
	}
240
241
	socket_set_blockmode(connfd, BM_NONBLOCK);
242
243
	if ((c = calloc(1, sizeof(struct ctl_conn))) == NULL) {
244
		log_warn("%s", __func__);
245
		close(connfd);
246
		return;
247
	}
248
249
	imsg_init(&c->iev.ibuf, connfd);
250
	c->iev.handler = control_dispatch_imsg;
251
	c->iev.events = EV_READ;
252
	c->iev.data = cs;
253
	event_set(&c->iev.ev, c->iev.ibuf.fd, c->iev.events,
254
	    c->iev.handler, c->iev.data);
255
	event_add(&c->iev.ev, NULL);
256
257
	TAILQ_INSERT_TAIL(&ctl_conns, c, entry);
258
}
259
260
struct ctl_conn *
261
control_connbyfd(int fd)
262
{
263
	struct ctl_conn	*c;
264
265
	TAILQ_FOREACH(c, &ctl_conns, entry) {
266
		if (c->iev.ibuf.fd == fd)
267
			break;
268
	}
269
270
	return (c);
271
}
272
273
void
274
control_close(int fd, struct control_sock *cs)
275
{
276
	struct ctl_conn	*c;
277
278
	if ((c = control_connbyfd(fd)) == NULL) {
279
		log_warn("%s: fd %d: not found", __func__, fd);
280
		return;
281
	}
282
283
	msgbuf_clear(&c->iev.ibuf.w);
284
	TAILQ_REMOVE(&ctl_conns, c, entry);
285
286
	event_del(&c->iev.ev);
287
	close(c->iev.ibuf.fd);
288
289
	/* Some file descriptors are available again. */
290
	if (evtimer_pending(&cs->cs_evt, NULL)) {
291
		evtimer_del(&cs->cs_evt);
292
		event_add(&cs->cs_ev, NULL);
293
	}
294
295
	free(c);
296
}
297
298
/* ARGSUSED */
299
void
300
control_dispatch_imsg(int fd, short event, void *arg)
301
{
302
	struct control_sock	*cs = arg;
303
	struct switchd		*env = cs->cs_env;
304
	struct ctl_conn		*c;
305
	struct imsg		 imsg;
306
	int			 n, v;
307
308
	if ((c = control_connbyfd(fd)) == NULL) {
309
		log_warn("%s: fd %d: not found", __func__, fd);
310
		return;
311
	}
312
313
	if (event & EV_READ) {
314
		if (((n = imsg_read(&c->iev.ibuf)) == -1 && errno != EAGAIN) ||
315
		    n == 0) {
316
			control_close(fd, cs);
317
			return;
318
		}
319
	}
320
	if (event & EV_WRITE) {
321
		if (msgbuf_write(&c->iev.ibuf.w) <= 0 && errno != EAGAIN) {
322
			control_close(fd, cs);
323
			return;
324
		}
325
	}
326
327
	for (;;) {
328
		if ((n = imsg_get(&c->iev.ibuf, &imsg)) == -1) {
329
			control_close(fd, cs);
330
			return;
331
		}
332
333
		if (n == 0)
334
			break;
335
336
		control_imsg_forward(&imsg);
337
338
		switch (imsg.hdr.type) {
339
		case IMSG_CTL_SHOW_SUM:
340
			/* Forward request and use control fd as _id_ */
341
			proc_compose(&env->sc_ps, PROC_OFP,
342
			    imsg.hdr.type, &fd, sizeof(fd));
343
			break;
344
		case IMSG_CTL_CONNECT:
345
		case IMSG_CTL_DISCONNECT:
346
			proc_compose(&env->sc_ps, PROC_PARENT,
347
			    imsg.hdr.type, imsg.data, IMSG_DATA_SIZE(&imsg));
348
			break;
349
		case IMSG_CTL_NOTIFY:
350
			if (c->flags & CTL_CONN_NOTIFY) {
351
				log_debug("%s: "
352
				    "client requested notify more than once",
353
				    __func__);
354
				imsg_compose_event(&c->iev, IMSG_CTL_FAIL,
355
				    0, 0, -1, NULL, 0);
356
				break;
357
			}
358
			c->flags |= CTL_CONN_NOTIFY;
359
			break;
360
		case IMSG_CTL_VERBOSE:
361
			IMSG_SIZE_CHECK(&imsg, &v);
362
363
			memcpy(&v, imsg.data, sizeof(v));
364
			log_setverbose(v);
365
366
			proc_forward_imsg(&env->sc_ps, &imsg, PROC_PARENT, -1);
367
			proc_forward_imsg(&env->sc_ps, &imsg, PROC_OFP, -1);
368
			break;
369
		default:
370
			log_debug("%s: error handling imsg %d",
371
			    __func__, imsg.hdr.type);
372
			break;
373
		}
374
		imsg_free(&imsg);
375
	}
376
377
	imsg_event_add(&c->iev);
378
}
379
380
void
381
control_imsg_forward(struct imsg *imsg)
382
{
383
	struct ctl_conn *c;
384
385
	TAILQ_FOREACH(c, &ctl_conns, entry)
386
		if (c->flags & CTL_CONN_NOTIFY)
387
			imsg_compose(&c->iev.ibuf, imsg->hdr.type,
388
			    0, imsg->hdr.pid, -1, imsg->data,
389
			    imsg->hdr.len - IMSG_HEADER_SIZE);
390
}