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