xref: /netbsd-src/sys/compat/common/kern_uipc_socket_50.c (revision 8a031a1d1e0aac23d43015c71ae6350b917c8347)
1*8a031a1dSpgoyette /*	$NetBSD: kern_uipc_socket_50.c,v 1.4 2019/12/12 02:15:42 pgoyette Exp $	*/
268c01b4bSpgoyette 
368c01b4bSpgoyette /*
468c01b4bSpgoyette  * Copyright (c) 2002, 2007, 2008, 2009 The NetBSD Foundation, Inc.
568c01b4bSpgoyette  * All rights reserved.
668c01b4bSpgoyette  *
768c01b4bSpgoyette  * This code is derived from software contributed to The NetBSD Foundation
868c01b4bSpgoyette  * by Jason R. Thorpe of Wasabi Systems, Inc, and by Andrew Doran.
968c01b4bSpgoyette  *
1068c01b4bSpgoyette  * Redistribution and use in source and binary forms, with or without
1168c01b4bSpgoyette  * modification, are permitted provided that the following conditions
1268c01b4bSpgoyette  * are met:
1368c01b4bSpgoyette  * 1. Redistributions of source code must retain the above copyright
1468c01b4bSpgoyette  *    notice, this list of conditions and the following disclaimer.
1568c01b4bSpgoyette  * 2. Redistributions in binary form must reproduce the above copyright
1668c01b4bSpgoyette  *    notice, this list of conditions and the following disclaimer in the
1768c01b4bSpgoyette  *    documentation and/or other materials provided with the distribution.
1868c01b4bSpgoyette  *
1968c01b4bSpgoyette  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2068c01b4bSpgoyette  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2168c01b4bSpgoyette  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2268c01b4bSpgoyette  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2368c01b4bSpgoyette  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2468c01b4bSpgoyette  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2568c01b4bSpgoyette  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2668c01b4bSpgoyette  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2768c01b4bSpgoyette  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2868c01b4bSpgoyette  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2968c01b4bSpgoyette  * POSSIBILITY OF SUCH DAMAGE.
3068c01b4bSpgoyette  */
3168c01b4bSpgoyette 
3268c01b4bSpgoyette /*
3368c01b4bSpgoyette  * Copyright (c) 2004 The FreeBSD Foundation
3468c01b4bSpgoyette  * Copyright (c) 2004 Robert Watson
3568c01b4bSpgoyette  * Copyright (c) 1982, 1986, 1988, 1990, 1993
3668c01b4bSpgoyette  *     The Regents of the University of California.  All rights reserved.
3768c01b4bSpgoyette  *
3868c01b4bSpgoyette  * Redistribution and use in source and binary forms, with or without
3968c01b4bSpgoyette  * modification, are permitted provided that the following conditions
4068c01b4bSpgoyette  * are met:
4168c01b4bSpgoyette  * 1. Redistributions of source code must retain the above copyright
4268c01b4bSpgoyette  *    notice, this list of conditions and the following disclaimer.
4368c01b4bSpgoyette  * 2. Redistributions in binary form must reproduce the above copyright
4468c01b4bSpgoyette  *    notice, this list of conditions and the following disclaimer in the
4568c01b4bSpgoyette  *    documentation and/or other materials provided with the distribution.
4668c01b4bSpgoyette  * 3. Neither the name of the University nor the names of its contributors
4768c01b4bSpgoyette  *    may be used to endorse or promote products derived from this software
4868c01b4bSpgoyette  *    without specific prior written permission.
4968c01b4bSpgoyette  *
5068c01b4bSpgoyette  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
5168c01b4bSpgoyette  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
5268c01b4bSpgoyette  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
5368c01b4bSpgoyette  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
5468c01b4bSpgoyette  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5568c01b4bSpgoyette  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
5668c01b4bSpgoyette  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
5768c01b4bSpgoyette  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
5868c01b4bSpgoyette  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5968c01b4bSpgoyette  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
6068c01b4bSpgoyette  * SUCH DAMAGE.
6168c01b4bSpgoyette  *
6268c01b4bSpgoyette  *     @(#)uipc_socket.c       8.6 (Berkeley) 5/2/95
6368c01b4bSpgoyette  */
6468c01b4bSpgoyette 
6568c01b4bSpgoyette /*
6668c01b4bSpgoyette  * Copyright (c) 1988 University of Utah.
6768c01b4bSpgoyette  * Copyright (c) 1990, 1993
6868c01b4bSpgoyette  *	The Regents of the University of California.  All rights reserved.
6968c01b4bSpgoyette  *
7068c01b4bSpgoyette  * This code is derived from software contributed to Berkeley by
7168c01b4bSpgoyette  * the Systems Programming Group of the University of Utah Computer
7268c01b4bSpgoyette  * Science Department.
7368c01b4bSpgoyette  *
7468c01b4bSpgoyette  * Redistribution and use in source and binary forms, with or without
7568c01b4bSpgoyette  * modification, are permitted provided that the following conditions
7668c01b4bSpgoyette  * are met:
7768c01b4bSpgoyette  * 1. Redistributions of source code must retain the above copyright
7868c01b4bSpgoyette  *    notice, this list of conditions and the following disclaimer.
7968c01b4bSpgoyette  * 2. Redistributions in binary form must reproduce the above copyright
8068c01b4bSpgoyette  *    notice, this list of conditions and the following disclaimer in the
8168c01b4bSpgoyette  *    documentation and/or other materials provided with the distribution.
8268c01b4bSpgoyette  * 3. Neither the name of the University nor the names of its contributors
8368c01b4bSpgoyette  *    may be used to endorse or promote products derived from this software
8468c01b4bSpgoyette  *    without specific prior written permission.
8568c01b4bSpgoyette  *
8668c01b4bSpgoyette  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
8768c01b4bSpgoyette  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
8868c01b4bSpgoyette  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
8968c01b4bSpgoyette  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
9068c01b4bSpgoyette  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
9168c01b4bSpgoyette  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
9268c01b4bSpgoyette  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
9368c01b4bSpgoyette  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
9468c01b4bSpgoyette  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
9568c01b4bSpgoyette  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
9668c01b4bSpgoyette  * SUCH DAMAGE.
9768c01b4bSpgoyette  *
9868c01b4bSpgoyette  * from: Utah $Hdr: vn.c 1.13 94/04/02$
9968c01b4bSpgoyette  *
10068c01b4bSpgoyette  *	@(#)vn.c	8.9 (Berkeley) 5/14/95
10168c01b4bSpgoyette  */
10268c01b4bSpgoyette 
10368c01b4bSpgoyette #include <sys/cdefs.h>
104*8a031a1dSpgoyette __KERNEL_RCSID(0, "$NetBSD: kern_uipc_socket_50.c,v 1.4 2019/12/12 02:15:42 pgoyette Exp $");
10568c01b4bSpgoyette 
10668c01b4bSpgoyette #if defined(_KERNEL_OPT)
10768c01b4bSpgoyette #include "opt_compat_netbsd.h"
10868c01b4bSpgoyette #endif
10968c01b4bSpgoyette 
11068c01b4bSpgoyette #include <sys/param.h>
11168c01b4bSpgoyette #include <sys/systm.h>
11268c01b4bSpgoyette #include <sys/kernel.h>
11368c01b4bSpgoyette #include <sys/proc.h>
11468c01b4bSpgoyette #include <sys/file.h>
11568c01b4bSpgoyette #include <sys/compat_stub.h>
11668c01b4bSpgoyette #include <sys/socketvar.h>
11768c01b4bSpgoyette 
11868c01b4bSpgoyette #include <compat/sys/time.h>
11968c01b4bSpgoyette #include <compat/sys/socket.h>
12068c01b4bSpgoyette 
12168c01b4bSpgoyette #include <compat/common/compat_mod.h>
12268c01b4bSpgoyette 
12368c01b4bSpgoyette static int
uipc_socket_50_getopt1(int opt,struct socket * so,struct sockopt * sopt)124a17761b2Spgoyette uipc_socket_50_getopt1(int opt, struct socket *so, struct sockopt *sopt)
12568c01b4bSpgoyette {
12668c01b4bSpgoyette 	int optval, error;
12768c01b4bSpgoyette 	struct timeval50 otv;
12868c01b4bSpgoyette 
12968c01b4bSpgoyette 	switch (opt) {
13068c01b4bSpgoyette 
13168c01b4bSpgoyette 	case SO_OSNDTIMEO:
13268c01b4bSpgoyette 	case SO_ORCVTIMEO:
13368c01b4bSpgoyette 		optval = (opt == SO_OSNDTIMEO ?
13468c01b4bSpgoyette 		    so->so_snd.sb_timeo : so->so_rcv.sb_timeo);
13568c01b4bSpgoyette 
13668c01b4bSpgoyette 		otv.tv_sec = optval / hz;
13768c01b4bSpgoyette 		otv.tv_usec = (optval % hz) * tick;
13868c01b4bSpgoyette 
13968c01b4bSpgoyette 		error = sockopt_set(sopt, &otv, sizeof(otv));
14068c01b4bSpgoyette 		break;
14168c01b4bSpgoyette 
14268c01b4bSpgoyette 	case SO_OTIMESTAMP:
14368c01b4bSpgoyette 		error = sockopt_setint(sopt, (so->so_options & opt) ? 1 : 0);
14468c01b4bSpgoyette 		break;
14568c01b4bSpgoyette 
14668c01b4bSpgoyette 	default:
14768c01b4bSpgoyette 		error = EPASSTHROUGH;
14868c01b4bSpgoyette 	}
14968c01b4bSpgoyette 	return error;
15068c01b4bSpgoyette }
15168c01b4bSpgoyette 
15268c01b4bSpgoyette static int
uipc_socket_50_setopt1(int opt,struct socket * so,const struct sockopt * sopt)153a17761b2Spgoyette uipc_socket_50_setopt1(int opt, struct socket *so, const struct sockopt *sopt)
15468c01b4bSpgoyette {
15568c01b4bSpgoyette 	int optval, error;
15668c01b4bSpgoyette 	struct timeval50 otv;
157a17761b2Spgoyette 	struct timeval tv;
15868c01b4bSpgoyette 
15968c01b4bSpgoyette 	switch (opt) {
16068c01b4bSpgoyette 
16168c01b4bSpgoyette 	case SO_OSNDTIMEO:
16268c01b4bSpgoyette 	case SO_ORCVTIMEO:
16368c01b4bSpgoyette 		solock(so);
164a17761b2Spgoyette 
165a17761b2Spgoyette 		error = sockopt_get(sopt, &otv, sizeof(otv));
166a17761b2Spgoyette 		if (error)
167a17761b2Spgoyette 			break;
168a17761b2Spgoyette 
169a17761b2Spgoyette 		timeval50_to_timeval(&otv, &tv);
170a17761b2Spgoyette 
171a17761b2Spgoyette 		/* Code duplicated from sys/kern/uipc_socket.c */
172a17761b2Spgoyette 		if (tv.tv_sec < 0 || tv.tv_usec < 0 || tv.tv_usec >= 1000000) {
173a17761b2Spgoyette 			error = EDOM;
17468c01b4bSpgoyette 			break;
17568c01b4bSpgoyette 		}
176a17761b2Spgoyette 		if (tv.tv_sec > (INT_MAX - tv.tv_usec / tick) / hz) {
177a17761b2Spgoyette 			error = EDOM;
178a17761b2Spgoyette 			break;
179a17761b2Spgoyette 		}
180a17761b2Spgoyette 
181a17761b2Spgoyette 		optval = tv.tv_sec * hz + tv.tv_usec / tick;
182a17761b2Spgoyette 		if (optval == 0 && tv.tv_usec != 0)
183a17761b2Spgoyette 			optval = 1;
184a17761b2Spgoyette 
185a17761b2Spgoyette 		switch (opt) {
186a17761b2Spgoyette 		case SO_OSNDTIMEO:
187a17761b2Spgoyette 			so->so_snd.sb_timeo = optval;
188a17761b2Spgoyette 			break;
189a17761b2Spgoyette 		case SO_ORCVTIMEO:
190a17761b2Spgoyette 			so->so_rcv.sb_timeo = optval;
191a17761b2Spgoyette 			break;
192a17761b2Spgoyette 		}
19368c01b4bSpgoyette 		break;
19468c01b4bSpgoyette 
19568c01b4bSpgoyette 	case SO_OTIMESTAMP:
19668c01b4bSpgoyette 		error = sockopt_getint(sopt, &optval);
19768c01b4bSpgoyette 		solock(so);
19868c01b4bSpgoyette 		if (error)
19968c01b4bSpgoyette 			break;
20068c01b4bSpgoyette 		if (optval)
20168c01b4bSpgoyette 			so->so_options |= opt;
20268c01b4bSpgoyette 		else
20368c01b4bSpgoyette 			so->so_options &= ~opt;
20468c01b4bSpgoyette 		break;
20568c01b4bSpgoyette 
20668c01b4bSpgoyette 	default:
20768c01b4bSpgoyette 		error = EPASSTHROUGH;
20868c01b4bSpgoyette 	}
20968c01b4bSpgoyette 	return error;
21068c01b4bSpgoyette }
21168c01b4bSpgoyette 
21268c01b4bSpgoyette static int
uipc_socket_50_sbts(int opt,struct mbuf *** mp)213ccc3f35bSpgoyette uipc_socket_50_sbts(int opt, struct mbuf ***mp)
21468c01b4bSpgoyette {
21568c01b4bSpgoyette 	struct timeval50 tv50;
21668c01b4bSpgoyette 	struct timeval tv;
21768c01b4bSpgoyette 
21868c01b4bSpgoyette 	microtime(&tv);
21968c01b4bSpgoyette 
22068c01b4bSpgoyette 	if (opt & SO_OTIMESTAMP) {
22168c01b4bSpgoyette 
22268c01b4bSpgoyette 		timeval_to_timeval50(&tv, &tv50);
223ccc3f35bSpgoyette 		**mp = sbcreatecontrol(&tv50, sizeof(tv50), SCM_OTIMESTAMP,
22468c01b4bSpgoyette 		    SOL_SOCKET);
225ccc3f35bSpgoyette 		if (**mp)
226ccc3f35bSpgoyette 			*mp = &(**mp)->m_next;
22768c01b4bSpgoyette 		return 0;
22868c01b4bSpgoyette 	} else
22968c01b4bSpgoyette 		return EPASSTHROUGH;
23068c01b4bSpgoyette }
23168c01b4bSpgoyette 
23268c01b4bSpgoyette void
kern_uipc_socket_50_init(void)23368c01b4bSpgoyette kern_uipc_socket_50_init(void)
23468c01b4bSpgoyette {
23568c01b4bSpgoyette 
236*8a031a1dSpgoyette 	MODULE_HOOK_SET(uipc_socket_50_setopt1_hook, uipc_socket_50_setopt1);
237*8a031a1dSpgoyette 	MODULE_HOOK_SET(uipc_socket_50_getopt1_hook, uipc_socket_50_getopt1);
238*8a031a1dSpgoyette 	MODULE_HOOK_SET(uipc_socket_50_sbts_hook, uipc_socket_50_sbts);
23968c01b4bSpgoyette }
24068c01b4bSpgoyette 
24168c01b4bSpgoyette void
kern_uipc_socket_50_fini(void)24268c01b4bSpgoyette kern_uipc_socket_50_fini(void)
24368c01b4bSpgoyette {
24468c01b4bSpgoyette 
24568c01b4bSpgoyette 	MODULE_HOOK_UNSET(uipc_socket_50_setopt1_hook);
24668c01b4bSpgoyette 	MODULE_HOOK_UNSET(uipc_socket_50_getopt1_hook);
24768c01b4bSpgoyette 	MODULE_HOOK_UNSET(uipc_socket_50_sbts_hook);
24868c01b4bSpgoyette }
249