xref: /haiku/src/system/kernel/fs/socket.cpp (revision b671e9bbdbd10268a042b4f4cc4317ccd03d105e)
1 /*
2  * Copyright 2009, Axel Dörfler, axeld@pinc-software.de.
3  * Copyright 2008, Ingo Weinhold, ingo_weinhold@gmx.de.
4  *
5  * Distributed under the terms of the MIT License.
6  */
7 
8 #include <sys/socket.h>
9 
10 #include <errno.h>
11 #include <limits.h>
12 
13 #include <module.h>
14 
15 #include <AutoDeleter.h>
16 
17 #include <syscall_utils.h>
18 
19 #include <fd.h>
20 #include <kernel.h>
21 #include <lock.h>
22 #include <syscall_restart.h>
23 #include <util/AutoLock.h>
24 #include <vfs.h>
25 
26 #include <net_stack_interface.h>
27 #include <net_stat.h>
28 
29 
30 #define MAX_SOCKET_ADDRESS_LENGTH	(sizeof(sockaddr_storage))
31 #define MAX_SOCKET_OPTION_LEN	128
32 #define MAX_ANCILLARY_DATA_LEN	1024
33 
34 #define GET_SOCKET_FD_OR_RETURN(fd, kernel, descriptor)	\
35 	do {												\
36 		status_t getError = get_socket_descriptor(fd, kernel, descriptor); \
37 		if (getError != B_OK)							\
38 			return getError;							\
39 	} while (false)
40 
41 
42 static net_stack_interface_module_info* sStackInterface = NULL;
43 static vint32 sStackInterfaceInitialized = 0;
44 static mutex sLock = MUTEX_INITIALIZER("stack interface");
45 
46 
47 struct FDPutter {
48 	FDPutter(file_descriptor* descriptor)
49 		: descriptor(descriptor)
50 	{
51 	}
52 
53 	~FDPutter()
54 	{
55 		if (descriptor != NULL)
56 			put_fd(descriptor);
57 	}
58 
59 	file_descriptor*	descriptor;
60 };
61 
62 
63 static net_stack_interface_module_info*
64 get_stack_interface_module()
65 {
66 	MutexLocker _(sLock);
67 
68 	if (sStackInterfaceInitialized++ == 0) {
69 		// load module
70 		net_stack_interface_module_info* module;
71 		// TODO: Add driver settings option to load the userland net stack.
72 		status_t error = get_module(NET_STACK_INTERFACE_MODULE_NAME,
73 			(module_info**)&module);
74 		if (error == B_OK)
75 			sStackInterface = module;
76 		else
77 			sStackInterface = NULL;
78 	}
79 
80 	return sStackInterface;
81 }
82 
83 
84 static void
85 put_stack_interface_module()
86 {
87 	MutexLocker _(sLock);
88 
89 	if (sStackInterfaceInitialized-- == 1)
90 		put_module(NET_STACK_INTERFACE_MODULE_NAME);
91 }
92 
93 
94 static status_t
95 prepare_userland_address_result(struct sockaddr* userAddress,
96 	socklen_t* _addressLength, socklen_t& addressLength, bool addressRequired)
97 {
98 	// check parameters
99 	if (_addressLength == NULL)
100 		return B_BAD_VALUE;
101 	if (userAddress == NULL) {
102 		if (addressRequired)
103 			return B_BAD_VALUE;
104 	} else if (!IS_USER_ADDRESS(userAddress)
105 			|| !IS_USER_ADDRESS(_addressLength)) {
106 		return B_BAD_ADDRESS;
107 	}
108 
109 	// copy the buffer size from userland
110 	addressLength = 0;
111 	if (userAddress != NULL
112 			&& user_memcpy(&addressLength, _addressLength, sizeof(socklen_t))
113 				!= B_OK) {
114 		return B_BAD_ADDRESS;
115 	}
116 
117 	if (addressLength > MAX_SOCKET_ADDRESS_LENGTH)
118 		addressLength = MAX_SOCKET_ADDRESS_LENGTH;
119 
120 	return B_OK;
121 }
122 
123 
124 static status_t
125 copy_address_to_userland(const void* address, socklen_t addressLength,
126 	sockaddr* userAddress, socklen_t userAddressBufferSize,
127 	socklen_t* userAddressLength)
128 {
129 	// copy address size and address back to userland
130 	if (user_memcpy(userAddressLength, &addressLength,
131 			sizeof(socklen_t)) != B_OK
132 		|| (userAddress != NULL
133 			&& user_memcpy(userAddress, address,
134 				min_c(addressLength, userAddressBufferSize)) != B_OK)) {
135 		return B_BAD_ADDRESS;
136 	}
137 
138 	return B_OK;
139 }
140 
141 
142 static status_t
143 prepare_userland_msghdr(const msghdr* userMessage, msghdr& message,
144 	iovec*& userVecs, MemoryDeleter& vecsDeleter, void*& userAddress,
145 	char* address)
146 {
147 	if (userMessage == NULL)
148 		return B_BAD_VALUE;
149 
150 	// copy message from userland
151 	if (!IS_USER_ADDRESS(userMessage)
152 			|| user_memcpy(&message, userMessage, sizeof(msghdr)) != B_OK) {
153 		return B_BAD_ADDRESS;
154 	}
155 
156 	userVecs = message.msg_iov;
157 	userAddress = message.msg_name;
158 
159 	// copy iovecs from userland
160 	if (message.msg_iovlen < 0 || message.msg_iovlen > IOV_MAX)
161 		return EMSGSIZE;
162 	if (userVecs != NULL && message.msg_iovlen > 0) {
163 		iovec* vecs = (iovec*)malloc(sizeof(iovec) * message.msg_iovlen);
164 		if (vecs == NULL)
165 			return B_NO_MEMORY;
166 		vecsDeleter.SetTo(vecs);
167 
168 		if (!IS_USER_ADDRESS(message.msg_iov)
169 				|| user_memcpy(vecs, message.msg_iov,
170 					message.msg_iovlen * sizeof(iovec)) != B_OK) {
171 			return B_BAD_ADDRESS;
172 		}
173 
174 		for (int i = 0; i < message.msg_iovlen; i++) {
175 			if (!IS_USER_ADDRESS(vecs[i].iov_base))
176 				return B_BAD_ADDRESS;
177 		}
178 
179 		message.msg_iov = vecs;
180 	} else {
181 		message.msg_iov = NULL;
182 		message.msg_iovlen = 0;
183 	}
184 
185 	// prepare the address field
186 	userAddress = message.msg_name;
187 	if (userAddress != NULL) {
188 		if (!IS_USER_ADDRESS(message.msg_name))
189 			return B_BAD_ADDRESS;
190 		if (message.msg_namelen > MAX_SOCKET_ADDRESS_LENGTH)
191 			message.msg_namelen = MAX_SOCKET_ADDRESS_LENGTH;
192 
193 		message.msg_name = address;
194 	}
195 
196 	return B_OK;
197 }
198 
199 
200 static status_t
201 get_socket_descriptor(int fd, bool kernel, file_descriptor*& descriptor)
202 {
203 	if (fd < 0)
204 		return EBADF;
205 
206 	descriptor = get_fd(get_current_io_context(kernel), fd);
207 	if (descriptor == NULL)
208 		return EBADF;
209 
210 	if (descriptor->type != FDTYPE_SOCKET) {
211 		put_fd(descriptor);
212 		return ENOTSOCK;
213 	}
214 
215 	return B_OK;
216 }
217 
218 
219 // #pragma mark - socket file descriptor
220 
221 
222 static status_t
223 socket_read(struct file_descriptor *descriptor, off_t pos, void *buffer,
224 	size_t *_length)
225 {
226 	ssize_t bytesRead = sStackInterface->recv(descriptor->u.socket, buffer,
227 		*_length, 0);
228 	*_length = bytesRead >= 0 ? bytesRead : 0;
229 	return bytesRead >= 0 ? B_OK : bytesRead;
230 }
231 
232 
233 static status_t
234 socket_write(struct file_descriptor *descriptor, off_t pos, const void *buffer,
235 	size_t *_length)
236 {
237 	ssize_t bytesWritten = sStackInterface->send(descriptor->u.socket, buffer,
238 		*_length, 0);
239 	*_length = bytesWritten >= 0 ? bytesWritten : 0;
240 	return bytesWritten >= 0 ? B_OK : bytesWritten;
241 }
242 
243 
244 static status_t
245 socket_ioctl(struct file_descriptor *descriptor, ulong op, void *buffer,
246 	size_t length)
247 {
248 	return sStackInterface->ioctl(descriptor->u.socket, op, buffer, length);
249 }
250 
251 
252 static status_t
253 socket_set_flags(struct file_descriptor *descriptor, int flags)
254 {
255 	// we ignore O_APPEND, but O_NONBLOCK we need to translate
256 	uint32 op = (flags & O_NONBLOCK) != 0
257 		? B_SET_NONBLOCKING_IO : B_SET_BLOCKING_IO;
258 
259 	return sStackInterface->ioctl(descriptor->u.socket, op, NULL, 0);
260 }
261 
262 
263 static status_t
264 socket_select(struct file_descriptor *descriptor, uint8 event,
265 	struct selectsync *sync)
266 {
267 	return sStackInterface->select(descriptor->u.socket, event, sync);
268 }
269 
270 
271 static status_t
272 socket_deselect(struct file_descriptor *descriptor, uint8 event,
273 	struct selectsync *sync)
274 {
275 	return sStackInterface->deselect(descriptor->u.socket, event, sync);
276 }
277 
278 
279 static status_t
280 socket_read_stat(struct file_descriptor *descriptor, struct stat *st)
281 {
282 	st->st_dev = 0;
283 	st->st_ino = (addr_t)descriptor->u.socket;
284 	st->st_mode = S_IFSOCK | 0666;
285 	st->st_nlink = 1;
286 	st->st_uid = 0;
287 	st->st_gid = 0;
288 	st->st_size = 0;
289 	st->st_rdev = 0;
290 	st->st_blksize = 1024;	// use MTU for datagram sockets?
291 	st->st_type = 0;
292 
293 	timespec now;
294 	now.tv_sec = time(NULL);
295 	now.tv_nsec = 0;
296 
297 	st->st_atim = now;
298 	st->st_mtim = now;
299 	st->st_ctim = now;
300 	st->st_crtim = now;
301 
302 	return B_OK;
303 }
304 
305 
306 static status_t
307 socket_close(struct file_descriptor *descriptor)
308 {
309 	return sStackInterface->close(descriptor->u.socket);
310 }
311 
312 
313 static void
314 socket_free(struct file_descriptor *descriptor)
315 {
316 	sStackInterface->free(descriptor->u.socket);
317 	put_stack_interface_module();
318 }
319 
320 
321 static struct fd_ops sSocketFDOps = {
322 	&socket_read,
323 	&socket_write,
324 	NULL,	// fd_seek
325 	&socket_ioctl,
326 	&socket_set_flags,
327 	&socket_select,
328 	&socket_deselect,
329 	NULL,	// fd_read_dir
330 	NULL,	// fd_rewind_dir
331 	&socket_read_stat,
332 	NULL,	// fd_write_stat
333 	&socket_close,
334 	&socket_free
335 };
336 
337 
338 static int
339 create_socket_fd(net_socket* socket, bool kernel)
340 {
341 	// Get the socket's non-blocking flag, so we can set the respective
342 	// open mode flag.
343 	int32 nonBlock;
344 	socklen_t nonBlockLen = sizeof(int32);
345 	status_t error = sStackInterface->getsockopt(socket, SOL_SOCKET,
346 		SO_NONBLOCK, &nonBlock, &nonBlockLen);
347 	if (error != B_OK)
348 		return error;
349 
350 	// allocate a file descriptor
351 	file_descriptor* descriptor = alloc_fd();
352 	if (descriptor == NULL)
353 		return B_NO_MEMORY;
354 
355 	// init it
356 	descriptor->type = FDTYPE_SOCKET;
357 	descriptor->ops = &sSocketFDOps;
358 	descriptor->u.socket = socket;
359 	descriptor->open_mode = O_RDWR | (nonBlock ? O_NONBLOCK : 0);
360 
361 	// publish it
362 	int fd = new_fd(get_current_io_context(kernel), descriptor);
363 	if (fd < 0)
364 		free(descriptor);
365 
366 	return fd;
367 }
368 
369 
370 // #pragma mark - common sockets API implementation
371 
372 
373 static int
374 common_socket(int family, int type, int protocol, bool kernel)
375 {
376 	if (!get_stack_interface_module())
377 		return B_UNSUPPORTED;
378 
379 	// create the socket
380 	net_socket* socket;
381 	status_t error = sStackInterface->open(family, type, protocol, &socket);
382 	if (error != B_OK) {
383 		put_stack_interface_module();
384 		return error;
385 	}
386 
387 	// allocate the FD
388 	int fd = create_socket_fd(socket, kernel);
389 	if (fd < 0) {
390 		sStackInterface->close(socket);
391 		sStackInterface->free(socket);
392 		put_stack_interface_module();
393 	}
394 
395 	return fd;
396 }
397 
398 
399 static status_t
400 common_bind(int fd, const struct sockaddr *address, socklen_t addressLength,
401 	bool kernel)
402 {
403 	file_descriptor* descriptor;
404 	GET_SOCKET_FD_OR_RETURN(fd, kernel, descriptor);
405 	FDPutter _(descriptor);
406 
407 	return sStackInterface->bind(descriptor->u.socket, address, addressLength);
408 }
409 
410 
411 static status_t
412 common_shutdown(int fd, int how, bool kernel)
413 {
414 	file_descriptor* descriptor;
415 	GET_SOCKET_FD_OR_RETURN(fd, kernel, descriptor);
416 	FDPutter _(descriptor);
417 
418 	return sStackInterface->shutdown(descriptor->u.socket, how);
419 }
420 
421 
422 static status_t
423 common_connect(int fd, const struct sockaddr *address,
424 	socklen_t addressLength, bool kernel)
425 {
426 	file_descriptor* descriptor;
427 	GET_SOCKET_FD_OR_RETURN(fd, kernel, descriptor);
428 	FDPutter _(descriptor);
429 
430 	return sStackInterface->connect(descriptor->u.socket, address,
431 		addressLength);
432 }
433 
434 
435 static status_t
436 common_listen(int fd, int backlog, bool kernel)
437 {
438 	file_descriptor* descriptor;
439 	GET_SOCKET_FD_OR_RETURN(fd, kernel, descriptor);
440 	FDPutter _(descriptor);
441 
442 	return sStackInterface->listen(descriptor->u.socket, backlog);
443 }
444 
445 
446 static int
447 common_accept(int fd, struct sockaddr *address, socklen_t *_addressLength,
448 	bool kernel)
449 {
450 	file_descriptor* descriptor;
451 	GET_SOCKET_FD_OR_RETURN(fd, kernel, descriptor);
452 	FDPutter _(descriptor);
453 
454 	net_socket* acceptedSocket;
455 	status_t error = sStackInterface->accept(descriptor->u.socket, address,
456 		_addressLength, &acceptedSocket);
457 	if (error != B_OK)
458 		return error;
459 
460 	// allocate the FD
461 	int acceptedFD = create_socket_fd(acceptedSocket, kernel);
462 	if (acceptedFD < 0) {
463 		sStackInterface->close(acceptedSocket);
464 		sStackInterface->free(acceptedSocket);
465 	} else {
466 		// we need a reference for the new FD
467 		get_stack_interface_module();
468 	}
469 
470 	return acceptedFD;
471 }
472 
473 
474 static ssize_t
475 common_recv(int fd, void *data, size_t length, int flags, bool kernel)
476 {
477 	file_descriptor* descriptor;
478 	GET_SOCKET_FD_OR_RETURN(fd, kernel, descriptor);
479 	FDPutter _(descriptor);
480 
481 	return sStackInterface->recv(descriptor->u.socket, data, length, flags);
482 }
483 
484 
485 static ssize_t
486 common_recvfrom(int fd, void *data, size_t length, int flags,
487 	struct sockaddr *address, socklen_t *_addressLength, bool kernel)
488 {
489 	file_descriptor* descriptor;
490 	GET_SOCKET_FD_OR_RETURN(fd, kernel, descriptor);
491 	FDPutter _(descriptor);
492 
493 	return sStackInterface->recvfrom(descriptor->u.socket, data, length,
494 		flags, address, _addressLength);
495 }
496 
497 
498 static ssize_t
499 common_recvmsg(int fd, struct msghdr *message, int flags, bool kernel)
500 {
501 	file_descriptor* descriptor;
502 	GET_SOCKET_FD_OR_RETURN(fd, kernel, descriptor);
503 	FDPutter _(descriptor);
504 
505 	return sStackInterface->recvmsg(descriptor->u.socket, message, flags);
506 }
507 
508 
509 static ssize_t
510 common_send(int fd, const void *data, size_t length, int flags, bool kernel)
511 {
512 	file_descriptor* descriptor;
513 	GET_SOCKET_FD_OR_RETURN(fd, kernel, descriptor);
514 	FDPutter _(descriptor);
515 
516 	return sStackInterface->send(descriptor->u.socket, data, length, flags);
517 }
518 
519 
520 static ssize_t
521 common_sendto(int fd, const void *data, size_t length, int flags,
522 	const struct sockaddr *address, socklen_t addressLength, bool kernel)
523 {
524 	file_descriptor* descriptor;
525 	GET_SOCKET_FD_OR_RETURN(fd, kernel, descriptor);
526 	FDPutter _(descriptor);
527 
528 	return sStackInterface->sendto(descriptor->u.socket, data, length, flags,
529 		address, addressLength);
530 }
531 
532 
533 static ssize_t
534 common_sendmsg(int fd, const struct msghdr *message, int flags, bool kernel)
535 {
536 	file_descriptor* descriptor;
537 	GET_SOCKET_FD_OR_RETURN(fd, kernel, descriptor);
538 	FDPutter _(descriptor);
539 
540 	return sStackInterface->sendmsg(descriptor->u.socket, message, flags);
541 }
542 
543 
544 static status_t
545 common_getsockopt(int fd, int level, int option, void *value,
546 	socklen_t *_length, bool kernel)
547 {
548 	file_descriptor* descriptor;
549 	GET_SOCKET_FD_OR_RETURN(fd, kernel, descriptor);
550 	FDPutter _(descriptor);
551 
552 	return sStackInterface->getsockopt(descriptor->u.socket, level, option,
553 		value, _length);
554 }
555 
556 
557 static status_t
558 common_setsockopt(int fd, int level, int option, const void *value,
559 	socklen_t length, bool kernel)
560 {
561 	file_descriptor* descriptor;
562 	GET_SOCKET_FD_OR_RETURN(fd, kernel, descriptor);
563 	FDPutter _(descriptor);
564 
565 	return sStackInterface->setsockopt(descriptor->u.socket, level, option,
566 		value, length);
567 }
568 
569 
570 static status_t
571 common_getpeername(int fd, struct sockaddr *address,
572 	socklen_t *_addressLength, bool kernel)
573 {
574 	file_descriptor* descriptor;
575 	GET_SOCKET_FD_OR_RETURN(fd, kernel, descriptor);
576 	FDPutter _(descriptor);
577 
578 	return sStackInterface->getpeername(descriptor->u.socket, address,
579 		_addressLength);
580 }
581 
582 
583 static status_t
584 common_getsockname(int fd, struct sockaddr *address,
585 	socklen_t *_addressLength, bool kernel)
586 {
587 	file_descriptor* descriptor;
588 	GET_SOCKET_FD_OR_RETURN(fd, kernel, descriptor);
589 	FDPutter _(descriptor);
590 
591 	return sStackInterface->getsockname(descriptor->u.socket, address,
592 		_addressLength);
593 }
594 
595 
596 static int
597 common_sockatmark(int fd, bool kernel)
598 {
599 	file_descriptor* descriptor;
600 	GET_SOCKET_FD_OR_RETURN(fd, kernel, descriptor);
601 	FDPutter _(descriptor);
602 
603 	return sStackInterface->sockatmark(descriptor->u.socket);
604 }
605 
606 
607 static status_t
608 common_socketpair(int family, int type, int protocol, int fds[2], bool kernel)
609 {
610 	if (!get_stack_interface_module())
611 		return B_UNSUPPORTED;
612 
613 	net_socket* sockets[2];
614 	status_t error = sStackInterface->socketpair(family, type, protocol,
615 		sockets);
616 	if (error != B_OK) {
617 		put_stack_interface_module();
618 		return error;
619 	}
620 
621 	// allocate the FDs
622 	for (int i = 0; i < 2; i++) {
623 		fds[i] = create_socket_fd(sockets[i], kernel);
624 		if (fds[i] < 0) {
625 			sStackInterface->close(sockets[i]);
626 			sStackInterface->free(sockets[i]);
627 			put_stack_interface_module();
628 			return fds[i];
629 		}
630 	}
631 
632 	// We need another reference for the second socket
633 	get_stack_interface_module();
634 	return B_OK;
635 }
636 
637 
638 static status_t
639 common_get_next_socket_stat(int family, uint32 *cookie, struct net_stat *stat)
640 {
641 	if (!get_stack_interface_module())
642 		return B_UNSUPPORTED;
643 
644 	status_t status = sStackInterface->get_next_socket_stat(family, cookie,
645 		stat);
646 
647 	put_stack_interface_module();
648 	return status;
649 }
650 
651 
652 // #pragma mark - kernel sockets API
653 
654 
655 int
656 socket(int family, int type, int protocol)
657 {
658 	SyscallFlagUnsetter _;
659 	RETURN_AND_SET_ERRNO(common_socket(family, type, protocol, true));
660 }
661 
662 
663 int
664 bind(int socket, const struct sockaddr *address, socklen_t addressLength)
665 {
666 	SyscallFlagUnsetter _;
667 	RETURN_AND_SET_ERRNO(common_bind(socket, address, addressLength, true));
668 }
669 
670 
671 int
672 shutdown(int socket, int how)
673 {
674 	SyscallFlagUnsetter _;
675 	RETURN_AND_SET_ERRNO(common_shutdown(socket, how, true));
676 }
677 
678 
679 int
680 connect(int socket, const struct sockaddr *address, socklen_t addressLength)
681 {
682 	SyscallFlagUnsetter _;
683 	RETURN_AND_SET_ERRNO(common_connect(socket, address, addressLength, true));
684 }
685 
686 
687 int
688 listen(int socket, int backlog)
689 {
690 	SyscallFlagUnsetter _;
691 	RETURN_AND_SET_ERRNO(common_listen(socket, backlog, true));
692 }
693 
694 
695 int
696 accept(int socket, struct sockaddr *address, socklen_t *_addressLength)
697 {
698 	SyscallFlagUnsetter _;
699 	RETURN_AND_SET_ERRNO(common_accept(socket, address, _addressLength, true));
700 }
701 
702 
703 ssize_t
704 recv(int socket, void *data, size_t length, int flags)
705 {
706 	SyscallFlagUnsetter _;
707 	RETURN_AND_SET_ERRNO(common_recv(socket, data, length, flags, true));
708 }
709 
710 
711 ssize_t
712 recvfrom(int socket, void *data, size_t length, int flags,
713 	struct sockaddr *address, socklen_t *_addressLength)
714 {
715 	SyscallFlagUnsetter _;
716 	RETURN_AND_SET_ERRNO(common_recvfrom(socket, data, length, flags, address,
717 		_addressLength, true));
718 }
719 
720 
721 ssize_t
722 recvmsg(int socket, struct msghdr *message, int flags)
723 {
724 	SyscallFlagUnsetter _;
725 	RETURN_AND_SET_ERRNO(common_recvmsg(socket, message, flags, true));
726 }
727 
728 
729 ssize_t
730 send(int socket, const void *data, size_t length, int flags)
731 {
732 	SyscallFlagUnsetter _;
733 	RETURN_AND_SET_ERRNO(common_send(socket, data, length, flags, true));
734 }
735 
736 
737 ssize_t
738 sendto(int socket, const void *data, size_t length, int flags,
739 	const struct sockaddr *address, socklen_t addressLength)
740 {
741 	SyscallFlagUnsetter _;
742 	RETURN_AND_SET_ERRNO(common_sendto(socket, data, length, flags, address,
743 		addressLength, true));
744 }
745 
746 
747 ssize_t
748 sendmsg(int socket, const struct msghdr *message, int flags)
749 {
750 	SyscallFlagUnsetter _;
751 	RETURN_AND_SET_ERRNO(common_sendmsg(socket, message, flags, true));
752 }
753 
754 
755 int
756 getsockopt(int socket, int level, int option, void *value, socklen_t *_length)
757 {
758 	SyscallFlagUnsetter _;
759 	RETURN_AND_SET_ERRNO(common_getsockopt(socket, level, option, value,
760 		_length, true));
761 }
762 
763 
764 int
765 setsockopt(int socket, int level, int option, const void *value,
766 	socklen_t length)
767 {
768 	SyscallFlagUnsetter _;
769 	RETURN_AND_SET_ERRNO(common_setsockopt(socket, level, option, value,
770 		length, true));
771 }
772 
773 
774 int
775 getpeername(int socket, struct sockaddr *address, socklen_t *_addressLength)
776 {
777 	SyscallFlagUnsetter _;
778 	RETURN_AND_SET_ERRNO(common_getpeername(socket, address, _addressLength,
779 		true));
780 }
781 
782 
783 int
784 getsockname(int socket, struct sockaddr *address, socklen_t *_addressLength)
785 {
786 	SyscallFlagUnsetter _;
787 	RETURN_AND_SET_ERRNO(common_getsockname(socket, address, _addressLength,
788 		true));
789 }
790 
791 
792 int
793 sockatmark(int socket)
794 {
795 	SyscallFlagUnsetter _;
796 	RETURN_AND_SET_ERRNO(common_sockatmark(socket, true));
797 }
798 
799 
800 int
801 socketpair(int family, int type, int protocol, int socketVector[2])
802 {
803 	SyscallFlagUnsetter _;
804 	RETURN_AND_SET_ERRNO(common_socketpair(family, type, protocol,
805 		socketVector, true));
806 }
807 
808 
809 // #pragma mark - syscalls
810 
811 
812 int
813 _user_socket(int family, int type, int protocol)
814 {
815 	SyscallRestartWrapper<int> result;
816 	return result = common_socket(family, type, protocol, false);
817 }
818 
819 
820 status_t
821 _user_bind(int socket, const struct sockaddr *userAddress,
822 	socklen_t addressLength)
823 {
824 	// check parameters and copy address from userland
825 	if (userAddress == NULL || addressLength > MAX_SOCKET_ADDRESS_LENGTH)
826 		return B_BAD_VALUE;
827 
828 	sockaddr_storage address;
829 	if (!IS_USER_ADDRESS(userAddress)
830 			|| user_memcpy(&address, userAddress, addressLength) != B_OK) {
831 		return B_BAD_ADDRESS;
832 	}
833 
834 	address.ss_len = addressLength;
835 		// make sure the sa_len field is set correctly
836 
837 	SyscallRestartWrapper<status_t> error;
838 	return error = common_bind(socket, (sockaddr*)&address, addressLength,
839 		false);
840 }
841 
842 
843 status_t
844 _user_shutdown_socket(int socket, int how)
845 {
846 	SyscallRestartWrapper<status_t> error;
847 	return error = common_shutdown(socket, how, false);
848 }
849 
850 
851 status_t
852 _user_connect(int socket, const struct sockaddr *userAddress,
853 	socklen_t addressLength)
854 {
855 	// check parameters and copy address from userland
856 	if (userAddress == NULL || addressLength > MAX_SOCKET_ADDRESS_LENGTH)
857 		return B_BAD_VALUE;
858 
859 	sockaddr_storage address;
860 	if (!IS_USER_ADDRESS(userAddress)
861 			|| user_memcpy(&address, userAddress, addressLength) != B_OK) {
862 		return B_BAD_ADDRESS;
863 	}
864 
865 	address.ss_len = addressLength;
866 		// make sure the sa_len field is set correctly
867 
868 	SyscallRestartWrapper<status_t> error;
869 
870 	return error = common_connect(socket, (sockaddr*)&address, addressLength,
871 		false);
872 }
873 
874 
875 status_t
876 _user_listen(int socket, int backlog)
877 {
878 	SyscallRestartWrapper<status_t> error;
879 	return error = common_listen(socket, backlog, false);
880 }
881 
882 
883 int
884 _user_accept(int socket, struct sockaddr *userAddress,
885 	socklen_t *_addressLength)
886 {
887 	// check parameters
888 	socklen_t addressLength = 0;
889 	status_t error = prepare_userland_address_result(userAddress,
890 		_addressLength, addressLength, false);
891 	if (error != B_OK)
892 		return error;
893 
894 	// accept()
895 	SyscallRestartWrapper<int> result;
896 
897 	char address[MAX_SOCKET_ADDRESS_LENGTH];
898 	socklen_t userAddressBufferSize = addressLength;
899 	result = common_accept(socket,
900 		userAddress != NULL ? (sockaddr*)address : NULL, &addressLength, false);
901 
902 	// copy address size and address back to userland
903 	if (copy_address_to_userland(address, addressLength, userAddress,
904 			userAddressBufferSize, _addressLength) != B_OK) {
905 		_user_close(result);
906 		return B_BAD_ADDRESS;
907 	}
908 
909 	return result;
910 }
911 
912 
913 ssize_t
914 _user_recv(int socket, void *data, size_t length, int flags)
915 {
916 	SyscallRestartWrapper<ssize_t> result;
917 	return result = common_recv(socket, data, length, flags, false);
918 }
919 
920 
921 ssize_t
922 _user_recvfrom(int socket, void *data, size_t length, int flags,
923 	struct sockaddr *userAddress, socklen_t *_addressLength)
924 {
925 	// check parameters
926 	socklen_t addressLength = 0;
927 	status_t error = prepare_userland_address_result(userAddress,
928 		_addressLength, addressLength, false);
929 	if (error != B_OK)
930 		return error;
931 
932 	// recvfrom()
933 	SyscallRestartWrapper<ssize_t> result;
934 
935 	char address[MAX_SOCKET_ADDRESS_LENGTH];
936 	socklen_t userAddressBufferSize = addressLength;
937 	result = common_recvfrom(socket, data, length, flags,
938 		userAddress != NULL ? (sockaddr*)address : NULL, &addressLength, false);
939 	if (result < 0)
940 		return result;
941 
942 	// copy address size and address back to userland
943 	if (copy_address_to_userland(address, addressLength, userAddress,
944 			userAddressBufferSize, _addressLength) != B_OK) {
945 		return B_BAD_ADDRESS;
946 	}
947 
948 	return result;
949 }
950 
951 
952 ssize_t
953 _user_recvmsg(int socket, struct msghdr *userMessage, int flags)
954 {
955 	// copy message from userland
956 	msghdr message;
957 	iovec* userVecs;
958 	MemoryDeleter vecsDeleter;
959 	void* userAddress;
960 	char address[MAX_SOCKET_ADDRESS_LENGTH];
961 
962 	status_t error = prepare_userland_msghdr(userMessage, message, userVecs,
963 		vecsDeleter, userAddress, address);
964 	if (error != B_OK)
965 		return error;
966 
967 	// prepare a buffer for ancillary data
968 	MemoryDeleter ancillaryDeleter;
969 	void* ancillary = NULL;
970 	void* userAncillary = message.msg_control;
971 	if (userAncillary != NULL) {
972 		if (!IS_USER_ADDRESS(userAncillary))
973 			return B_BAD_ADDRESS;
974 		if (message.msg_controllen < 0)
975 			return B_BAD_VALUE;
976 		if (message.msg_controllen > MAX_ANCILLARY_DATA_LEN)
977 			message.msg_controllen = MAX_ANCILLARY_DATA_LEN;
978 
979 		message.msg_control = ancillary = malloc(message.msg_controllen);
980 		if (message.msg_control == NULL)
981 			return B_NO_MEMORY;
982 
983 		ancillaryDeleter.SetTo(ancillary);
984 	}
985 
986 	// recvmsg()
987 	SyscallRestartWrapper<ssize_t> result;
988 
989 	result = common_recvmsg(socket, &message, flags, false);
990 	if (result < 0)
991 		return result;
992 
993 	// copy the address, the ancillary data, and the message header back to
994 	// userland
995 	message.msg_name = userAddress;
996 	message.msg_iov = userVecs;
997 	message.msg_control = userAncillary;
998 	if ((userAddress != NULL && user_memcpy(userAddress, address,
999 				message.msg_namelen) != B_OK)
1000 		|| (userAncillary != NULL && user_memcpy(userAncillary, ancillary,
1001 				message.msg_controllen) != B_OK)
1002 		|| user_memcpy(userMessage, &message, sizeof(msghdr)) != B_OK) {
1003 		return B_BAD_ADDRESS;
1004 	}
1005 
1006 	return result;
1007 }
1008 
1009 
1010 ssize_t
1011 _user_send(int socket, const void *data, size_t length, int flags)
1012 {
1013 	SyscallRestartWrapper<ssize_t> result;
1014 	return result = common_send(socket, data, length, flags, false);
1015 }
1016 
1017 
1018 ssize_t
1019 _user_sendto(int socket, const void *data, size_t length, int flags,
1020 	const struct sockaddr *userAddress, socklen_t addressLength)
1021 {
1022 // TODO: If this is a connection-mode socket, the address parameter is
1023 // supposed to be ignored.
1024 	if (userAddress == NULL || addressLength <= 0
1025 			|| addressLength > MAX_SOCKET_ADDRESS_LENGTH) {
1026 		return B_BAD_VALUE;
1027 	}
1028 
1029 	// copy address from userland
1030 	char address[MAX_SOCKET_ADDRESS_LENGTH];
1031 	if (!IS_USER_ADDRESS(userAddress)
1032 			|| user_memcpy(address, userAddress, addressLength) != B_OK) {
1033 		return B_BAD_ADDRESS;
1034 	}
1035 
1036 	// sendto()
1037 	SyscallRestartWrapper<ssize_t> result;
1038 
1039 	return result = common_sendto(socket, data, length, flags,
1040 		(sockaddr*)address, addressLength, false);
1041 }
1042 
1043 
1044 ssize_t
1045 _user_sendmsg(int socket, const struct msghdr *userMessage, int flags)
1046 {
1047 	// copy message from userland
1048 	msghdr message;
1049 	iovec* userVecs;
1050 	MemoryDeleter vecsDeleter;
1051 	void* userAddress;
1052 	char address[MAX_SOCKET_ADDRESS_LENGTH];
1053 
1054 	status_t error = prepare_userland_msghdr(userMessage, message, userVecs,
1055 		vecsDeleter, userAddress, address);
1056 	if (error != B_OK)
1057 		return error;
1058 
1059 	// copy the address from userland
1060 	if (userAddress != NULL
1061 			&& user_memcpy(address, userAddress, message.msg_namelen) != B_OK) {
1062 		return B_BAD_ADDRESS;
1063 	}
1064 
1065 	// copy ancillary data from userland
1066 	MemoryDeleter ancillaryDeleter;
1067 	void* userAncillary = message.msg_control;
1068 	if (userAncillary != NULL) {
1069 		if (!IS_USER_ADDRESS(userAncillary))
1070 			return B_BAD_ADDRESS;
1071 		if (message.msg_controllen < 0
1072 				|| message.msg_controllen > MAX_ANCILLARY_DATA_LEN) {
1073 			return B_BAD_VALUE;
1074 		}
1075 
1076 		message.msg_control = malloc(message.msg_controllen);
1077 		if (message.msg_control == NULL)
1078 			return B_NO_MEMORY;
1079 		ancillaryDeleter.SetTo(message.msg_control);
1080 
1081 		if (user_memcpy(message.msg_control, userAncillary,
1082 				message.msg_controllen) != B_OK) {
1083 			return B_BAD_ADDRESS;
1084 		}
1085 	}
1086 
1087 	// sendmsg()
1088 	SyscallRestartWrapper<ssize_t> result;
1089 
1090 	return result = common_sendmsg(socket, &message, flags, false);
1091 }
1092 
1093 
1094 status_t
1095 _user_getsockopt(int socket, int level, int option, void *userValue,
1096 	socklen_t *_length)
1097 {
1098 	// check params
1099 	if (userValue == NULL || _length == NULL)
1100 		return B_BAD_VALUE;
1101 	if (!IS_USER_ADDRESS(userValue) || !IS_USER_ADDRESS(_length))
1102 		return B_BAD_ADDRESS;
1103 
1104 	// copy length from userland
1105 	socklen_t length;
1106 	if (user_memcpy(&length, _length, sizeof(socklen_t)) != B_OK)
1107 		return B_BAD_ADDRESS;
1108 
1109 	if (length > MAX_SOCKET_OPTION_LEN)
1110 		return B_BAD_VALUE;
1111 
1112 	// getsockopt()
1113 	char value[MAX_SOCKET_OPTION_LEN];
1114 	SyscallRestartWrapper<status_t> error;
1115 	error = common_getsockopt(socket, level, option, value, &length,
1116 		false);
1117 	if (error != B_OK)
1118 		return error;
1119 
1120 	// copy value back to userland
1121 	if (user_memcpy(userValue, value, length) != B_OK)
1122 		return B_BAD_ADDRESS;
1123 
1124 	return B_OK;
1125 }
1126 
1127 
1128 status_t
1129 _user_setsockopt(int socket, int level, int option, const void *userValue,
1130 	socklen_t length)
1131 {
1132 	// check params
1133 	if (userValue == NULL || length > MAX_SOCKET_OPTION_LEN)
1134 		return B_BAD_VALUE;
1135 
1136 	// copy value from userland
1137 	char value[MAX_SOCKET_OPTION_LEN];
1138 	if (!IS_USER_ADDRESS(userValue)
1139 			|| user_memcpy(value, userValue, length) != B_OK) {
1140 		return B_BAD_ADDRESS;
1141 	}
1142 
1143 	// setsockopt();
1144 	SyscallRestartWrapper<status_t> error;
1145 	return error = common_setsockopt(socket, level, option, value, length,
1146 		false);
1147 }
1148 
1149 
1150 status_t
1151 _user_getpeername(int socket, struct sockaddr *userAddress,
1152 	socklen_t *_addressLength)
1153 {
1154 	// check parameters
1155 	socklen_t addressLength = 0;
1156 	SyscallRestartWrapper<status_t> error;
1157 	error = prepare_userland_address_result(userAddress, _addressLength,
1158 		addressLength, true);
1159 	if (error != B_OK)
1160 		return error;
1161 
1162 	// getpeername()
1163 	char address[MAX_SOCKET_ADDRESS_LENGTH];
1164 	socklen_t userAddressBufferSize = addressLength;
1165 	error = common_getpeername(socket, (sockaddr*)address, &addressLength,
1166 		false);
1167 	if (error != B_OK)
1168 		return error;
1169 
1170 	// copy address size and address back to userland
1171 	if (copy_address_to_userland(address, addressLength, userAddress,
1172 			userAddressBufferSize, _addressLength) != B_OK) {
1173 		return B_BAD_ADDRESS;
1174 	}
1175 
1176 	return B_OK;
1177 }
1178 
1179 
1180 status_t
1181 _user_getsockname(int socket, struct sockaddr *userAddress,
1182 	socklen_t *_addressLength)
1183 {
1184 	// check parameters
1185 	socklen_t addressLength = 0;
1186 	SyscallRestartWrapper<status_t> error;
1187 	error = prepare_userland_address_result(userAddress, _addressLength,
1188 		addressLength, true);
1189 	if (error != B_OK)
1190 		return error;
1191 
1192 	// getsockname()
1193 	char address[MAX_SOCKET_ADDRESS_LENGTH];
1194 	socklen_t userAddressBufferSize = addressLength;
1195 	error = common_getsockname(socket, (sockaddr*)address, &addressLength,
1196 		false);
1197 	if (error != B_OK)
1198 		return error;
1199 
1200 	// copy address size and address back to userland
1201 	if (copy_address_to_userland(address, addressLength, userAddress,
1202 			userAddressBufferSize, _addressLength) != B_OK) {
1203 		return B_BAD_ADDRESS;
1204 	}
1205 
1206 	return B_OK;
1207 }
1208 
1209 
1210 int
1211 _user_sockatmark(int socket)
1212 {
1213 	SyscallRestartWrapper<status_t> error;
1214 	return error = common_sockatmark(socket, false);
1215 }
1216 
1217 
1218 status_t
1219 _user_socketpair(int family, int type, int protocol, int *userSocketVector)
1220 {
1221 	// check parameters
1222 	if (userSocketVector == NULL)
1223 		return B_BAD_VALUE;
1224 	if (!IS_USER_ADDRESS(userSocketVector))
1225 		return B_BAD_ADDRESS;
1226 
1227 	// socketpair()
1228 	int socketVector[2];
1229 	SyscallRestartWrapper<status_t> error;
1230 	error = common_socketpair(family, type, protocol, socketVector, false);
1231 	if (error != B_OK)
1232 		return error;
1233 
1234 	// copy FDs back to userland
1235 	if (user_memcpy(userSocketVector, socketVector,
1236 			sizeof(socketVector)) != B_OK) {
1237 		_user_close(socketVector[0]);
1238 		_user_close(socketVector[1]);
1239 		return B_BAD_ADDRESS;
1240 	}
1241 
1242 	return B_OK;
1243 }
1244 
1245 
1246 status_t
1247 _user_get_next_socket_stat(int family, uint32 *_cookie, struct net_stat *_stat)
1248 {
1249 	// check parameters and copy cookie from userland
1250 	if (_cookie == NULL || _stat == NULL)
1251 		return B_BAD_VALUE;
1252 
1253 	uint32 cookie;
1254 	if (!IS_USER_ADDRESS(_stat) || !IS_USER_ADDRESS(_cookie)
1255 		|| user_memcpy(&cookie, _cookie, sizeof(cookie)) != B_OK) {
1256 		return B_BAD_ADDRESS;
1257 	}
1258 
1259 	net_stat stat;
1260 	SyscallRestartWrapper<status_t> error;
1261 	error = common_get_next_socket_stat(family, &cookie, &stat);
1262 	if (error != B_OK)
1263 		return error;
1264 
1265 	// copy cookie and data back to userland
1266 	if (user_memcpy(_cookie, &cookie, sizeof(cookie)) != B_OK
1267 		|| user_memcpy(_stat, &stat, sizeof(net_stat)) != B_OK) {
1268 		return B_BAD_ADDRESS;
1269 	}
1270 
1271 	return B_OK;
1272 }
1273