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