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