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