1/*-
2 * Copyright (c) 2009-2010 The FreeBSD Foundation
3 * Copyright (c) 2011 Pawel Jakub Dawidek <pawel@dawidek.net>
4 * All rights reserved.
5 *
6 * This software was developed by Pawel Jakub Dawidek under sponsorship from
7 * the FreeBSD Foundation.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 *
30 * $P4: //depot/projects/trustedbsd/openbsm/bin/auditdistd/proto_common.c#1 $
31 */
32
33#include <sys/types.h>
34#include <sys/socket.h>
35
36#include <errno.h>
37#include <fcntl.h>
38#include <stdbool.h>
39#include <stdlib.h>
40#include <strings.h>
41#include <unistd.h>
42
43#include <compat/compat.h>
44
45#include "pjdlog.h"
46#include "proto_impl.h"
47
48/* Maximum size of packet we want to use when sending data. */
49#ifndef MAX_SEND_SIZE
50#define	MAX_SEND_SIZE	32768
51#endif
52
53static bool
54blocking_socket(int sock)
55{
56	int flags;
57
58	flags = fcntl(sock, F_GETFL);
59	PJDLOG_ASSERT(flags >= 0);
60	return ((flags & O_NONBLOCK) == 0);
61}
62
63static int
64proto_descriptor_send(int sock, int fd)
65{
66	unsigned char ctrl[CMSG_SPACE(sizeof(fd))];
67	struct msghdr msg;
68	struct cmsghdr *cmsg;
69
70	PJDLOG_ASSERT(sock >= 0);
71	PJDLOG_ASSERT(fd >= 0);
72
73	bzero(&msg, sizeof(msg));
74	bzero(&ctrl, sizeof(ctrl));
75
76	msg.msg_iov = NULL;
77	msg.msg_iovlen = 0;
78	msg.msg_control = ctrl;
79	msg.msg_controllen = sizeof(ctrl);
80
81	cmsg = CMSG_FIRSTHDR(&msg);
82	cmsg->cmsg_level = SOL_SOCKET;
83	cmsg->cmsg_type = SCM_RIGHTS;
84	cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
85	bcopy(&fd, CMSG_DATA(cmsg), sizeof(fd));
86
87	if (sendmsg(sock, &msg, 0) == -1)
88		return (errno);
89
90	return (0);
91}
92
93int
94proto_common_send(int sock, const unsigned char *data, size_t size, int fd)
95{
96	ssize_t done;
97	size_t sendsize;
98	int errcount = 0;
99
100	PJDLOG_ASSERT(sock >= 0);
101
102	if (data == NULL) {
103		/* The caller is just trying to decide about direction. */
104
105		PJDLOG_ASSERT(size == 0);
106
107		if (shutdown(sock, SHUT_RD) == -1)
108			return (errno);
109		return (0);
110	}
111
112	PJDLOG_ASSERT(data != NULL);
113	PJDLOG_ASSERT(size > 0);
114
115	do {
116		sendsize = size < MAX_SEND_SIZE ? size : MAX_SEND_SIZE;
117		done = send(sock, data, sendsize, MSG_NOSIGNAL);
118		if (done == 0) {
119			return (ENOTCONN);
120		} else if (done < 0) {
121			if (errno == EINTR)
122				continue;
123			if (errno == ENOBUFS) {
124				/*
125				 * If there are no buffers we retry.
126				 * After each try we increase delay before the
127				 * next one and we give up after fifteen times.
128				 * This gives 11s of total wait time.
129				 */
130				if (errcount == 15) {
131					pjdlog_warning("Getting ENOBUFS errors for 11s on send(), giving up.");
132				} else {
133					if (errcount == 0)
134						pjdlog_warning("Got ENOBUFS error on send(), retrying for a bit.");
135					errcount++;
136					usleep(100000 * errcount);
137					continue;
138				}
139			}
140			/*
141			 * If this is blocking socket and we got EAGAIN, this
142			 * means the request timed out. Translate errno to
143			 * ETIMEDOUT, to give administrator a hint to
144			 * eventually increase timeout.
145			 */
146			if (errno == EAGAIN && blocking_socket(sock))
147				errno = ETIMEDOUT;
148			return (errno);
149		}
150		data += done;
151		size -= done;
152	} while (size > 0);
153	if (errcount > 0) {
154		pjdlog_info("Data sent successfully after %d ENOBUFS error%s.",
155		    errcount, errcount == 1 ? "" : "s");
156	}
157
158	if (fd == -1)
159		return (0);
160	return (proto_descriptor_send(sock, fd));
161}
162
163static int
164proto_descriptor_recv(int sock, int *fdp)
165{
166	unsigned char ctrl[CMSG_SPACE(sizeof(*fdp))];
167	struct msghdr msg;
168	struct cmsghdr *cmsg;
169
170	PJDLOG_ASSERT(sock >= 0);
171	PJDLOG_ASSERT(fdp != NULL);
172
173	bzero(&msg, sizeof(msg));
174	bzero(&ctrl, sizeof(ctrl));
175
176	msg.msg_iov = NULL;
177	msg.msg_iovlen = 0;
178	msg.msg_control = ctrl;
179	msg.msg_controllen = sizeof(ctrl);
180
181	if (recvmsg(sock, &msg, 0) == -1)
182		return (errno);
183
184	cmsg = CMSG_FIRSTHDR(&msg);
185	if (cmsg->cmsg_level != SOL_SOCKET ||
186	    cmsg->cmsg_type != SCM_RIGHTS) {
187		return (EINVAL);
188	}
189	bcopy(CMSG_DATA(cmsg), fdp, sizeof(*fdp));
190
191	return (0);
192}
193
194int
195proto_common_recv(int sock, unsigned char *data, size_t size, int *fdp)
196{
197	ssize_t done;
198
199	PJDLOG_ASSERT(sock >= 0);
200
201	if (data == NULL) {
202		/* The caller is just trying to decide about direction. */
203
204		PJDLOG_ASSERT(size == 0);
205
206		if (shutdown(sock, SHUT_WR) == -1)
207			return (errno);
208		return (0);
209	}
210
211	PJDLOG_ASSERT(data != NULL);
212	PJDLOG_ASSERT(size > 0);
213
214	do {
215		done = recv(sock, data, size, MSG_WAITALL);
216	} while (done == -1 && errno == EINTR);
217	if (done == 0) {
218		return (ENOTCONN);
219	} else if (done < 0) {
220		/*
221		 * If this is blocking socket and we got EAGAIN, this
222		 * means the request timed out. Translate errno to
223		 * ETIMEDOUT, to give administrator a hint to
224		 * eventually increase timeout.
225		 */
226		if (errno == EAGAIN && blocking_socket(sock))
227			errno = ETIMEDOUT;
228		return (errno);
229	}
230	if (fdp == NULL)
231		return (0);
232	return (proto_descriptor_recv(sock, fdp));
233}
234