Home | History | Annotate | Line # | Download | only in common
      1 /*	$NetBSD: kern_uipc_socket_50.c,v 1.4 2019/12/12 02:15:42 pgoyette Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2002, 2007, 2008, 2009 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of Wasabi Systems, Inc, and by Andrew Doran.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Copyright (c) 2004 The FreeBSD Foundation
     34  * Copyright (c) 2004 Robert Watson
     35  * Copyright (c) 1982, 1986, 1988, 1990, 1993
     36  *     The Regents of the University of California.  All rights reserved.
     37  *
     38  * Redistribution and use in source and binary forms, with or without
     39  * modification, are permitted provided that the following conditions
     40  * are met:
     41  * 1. Redistributions of source code must retain the above copyright
     42  *    notice, this list of conditions and the following disclaimer.
     43  * 2. Redistributions in binary form must reproduce the above copyright
     44  *    notice, this list of conditions and the following disclaimer in the
     45  *    documentation and/or other materials provided with the distribution.
     46  * 3. Neither the name of the University nor the names of its contributors
     47  *    may be used to endorse or promote products derived from this software
     48  *    without specific prior written permission.
     49  *
     50  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     51  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     52  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     53  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     54  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     55  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     56  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     57  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     58  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     59  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     60  * SUCH DAMAGE.
     61  *
     62  *     @(#)uipc_socket.c       8.6 (Berkeley) 5/2/95
     63  */
     64 
     65 /*
     66  * Copyright (c) 1988 University of Utah.
     67  * Copyright (c) 1990, 1993
     68  *	The Regents of the University of California.  All rights reserved.
     69  *
     70  * This code is derived from software contributed to Berkeley by
     71  * the Systems Programming Group of the University of Utah Computer
     72  * Science Department.
     73  *
     74  * Redistribution and use in source and binary forms, with or without
     75  * modification, are permitted provided that the following conditions
     76  * are met:
     77  * 1. Redistributions of source code must retain the above copyright
     78  *    notice, this list of conditions and the following disclaimer.
     79  * 2. Redistributions in binary form must reproduce the above copyright
     80  *    notice, this list of conditions and the following disclaimer in the
     81  *    documentation and/or other materials provided with the distribution.
     82  * 3. Neither the name of the University nor the names of its contributors
     83  *    may be used to endorse or promote products derived from this software
     84  *    without specific prior written permission.
     85  *
     86  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     87  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     88  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     89  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     90  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     91  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     92  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     93  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     94  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     95  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     96  * SUCH DAMAGE.
     97  *
     98  * from: Utah $Hdr: vn.c 1.13 94/04/02$
     99  *
    100  *	@(#)vn.c	8.9 (Berkeley) 5/14/95
    101  */
    102 
    103 #include <sys/cdefs.h>
    104 __KERNEL_RCSID(0, "$NetBSD: kern_uipc_socket_50.c,v 1.4 2019/12/12 02:15:42 pgoyette Exp $");
    105 
    106 #if defined(_KERNEL_OPT)
    107 #include "opt_compat_netbsd.h"
    108 #endif
    109 
    110 #include <sys/param.h>
    111 #include <sys/systm.h>
    112 #include <sys/kernel.h>
    113 #include <sys/proc.h>
    114 #include <sys/file.h>
    115 #include <sys/compat_stub.h>
    116 #include <sys/socketvar.h>
    117 
    118 #include <compat/sys/time.h>
    119 #include <compat/sys/socket.h>
    120 
    121 #include <compat/common/compat_mod.h>
    122 
    123 static int
    124 uipc_socket_50_getopt1(int opt, struct socket *so, struct sockopt *sopt)
    125 {
    126 	int optval, error;
    127 	struct timeval50 otv;
    128 
    129 	switch (opt) {
    130 
    131 	case SO_OSNDTIMEO:
    132 	case SO_ORCVTIMEO:
    133 		optval = (opt == SO_OSNDTIMEO ?
    134 		    so->so_snd.sb_timeo : so->so_rcv.sb_timeo);
    135 
    136 		otv.tv_sec = optval / hz;
    137 		otv.tv_usec = (optval % hz) * tick;
    138 
    139 		error = sockopt_set(sopt, &otv, sizeof(otv));
    140 		break;
    141 
    142 	case SO_OTIMESTAMP:
    143 		error = sockopt_setint(sopt, (so->so_options & opt) ? 1 : 0);
    144 		break;
    145 
    146 	default:
    147 		error = EPASSTHROUGH;
    148 	}
    149 	return error;
    150 }
    151 
    152 static int
    153 uipc_socket_50_setopt1(int opt, struct socket *so, const struct sockopt *sopt)
    154 {
    155 	int optval, error;
    156 	struct timeval50 otv;
    157 	struct timeval tv;
    158 
    159 	switch (opt) {
    160 
    161 	case SO_OSNDTIMEO:
    162 	case SO_ORCVTIMEO:
    163 		solock(so);
    164 
    165 		error = sockopt_get(sopt, &otv, sizeof(otv));
    166 		if (error)
    167 			break;
    168 
    169 		timeval50_to_timeval(&otv, &tv);
    170 
    171 		/* Code duplicated from sys/kern/uipc_socket.c */
    172 		if (tv.tv_sec < 0 || tv.tv_usec < 0 || tv.tv_usec >= 1000000) {
    173 			error = EDOM;
    174 			break;
    175 		}
    176 		if (tv.tv_sec > (INT_MAX - tv.tv_usec / tick) / hz) {
    177 			error = EDOM;
    178 			break;
    179 		}
    180 
    181 		optval = tv.tv_sec * hz + tv.tv_usec / tick;
    182 		if (optval == 0 && tv.tv_usec != 0)
    183 			optval = 1;
    184 
    185 		switch (opt) {
    186 		case SO_OSNDTIMEO:
    187 			so->so_snd.sb_timeo = optval;
    188 			break;
    189 		case SO_ORCVTIMEO:
    190 			so->so_rcv.sb_timeo = optval;
    191 			break;
    192 		}
    193 		break;
    194 
    195 	case SO_OTIMESTAMP:
    196 		error = sockopt_getint(sopt, &optval);
    197 		solock(so);
    198 		if (error)
    199 			break;
    200 		if (optval)
    201 			so->so_options |= opt;
    202 		else
    203 			so->so_options &= ~opt;
    204 		break;
    205 
    206 	default:
    207 		error = EPASSTHROUGH;
    208 	}
    209 	return error;
    210 }
    211 
    212 static int
    213 uipc_socket_50_sbts(int opt, struct mbuf ***mp)
    214 {
    215 	struct timeval50 tv50;
    216 	struct timeval tv;
    217 
    218 	microtime(&tv);
    219 
    220 	if (opt & SO_OTIMESTAMP) {
    221 
    222 		timeval_to_timeval50(&tv, &tv50);
    223 		**mp = sbcreatecontrol(&tv50, sizeof(tv50), SCM_OTIMESTAMP,
    224 		    SOL_SOCKET);
    225 		if (**mp)
    226 			*mp = &(**mp)->m_next;
    227 		return 0;
    228 	} else
    229 		return EPASSTHROUGH;
    230 }
    231 
    232 void
    233 kern_uipc_socket_50_init(void)
    234 {
    235 
    236 	MODULE_HOOK_SET(uipc_socket_50_setopt1_hook, uipc_socket_50_setopt1);
    237 	MODULE_HOOK_SET(uipc_socket_50_getopt1_hook, uipc_socket_50_getopt1);
    238 	MODULE_HOOK_SET(uipc_socket_50_sbts_hook, uipc_socket_50_sbts);
    239 }
    240 
    241 void
    242 kern_uipc_socket_50_fini(void)
    243 {
    244 
    245 	MODULE_HOOK_UNSET(uipc_socket_50_setopt1_hook);
    246 	MODULE_HOOK_UNSET(uipc_socket_50_getopt1_hook);
    247 	MODULE_HOOK_UNSET(uipc_socket_50_sbts_hook);
    248 }
    249