xref: /onnv-gate/usr/src/uts/common/ktli/t_kbind.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate /*
31*0Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
32*0Sstevel@tonic-gate  * The Regents of the University of California
33*0Sstevel@tonic-gate  * All Rights Reserved
34*0Sstevel@tonic-gate  *
35*0Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
36*0Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
37*0Sstevel@tonic-gate  * contributors.
38*0Sstevel@tonic-gate  */
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate /*
43*0Sstevel@tonic-gate  * Kernel TLI-like function to bind a transport endpoint
44*0Sstevel@tonic-gate  * to an address.
45*0Sstevel@tonic-gate  *
46*0Sstevel@tonic-gate  * Returns 0 on success or positive error code.
47*0Sstevel@tonic-gate  */
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate #include <sys/param.h>
50*0Sstevel@tonic-gate #include <sys/types.h>
51*0Sstevel@tonic-gate #include <sys/proc.h>
52*0Sstevel@tonic-gate #include <sys/file.h>
53*0Sstevel@tonic-gate #include <sys/user.h>
54*0Sstevel@tonic-gate #include <sys/errno.h>
55*0Sstevel@tonic-gate #include <sys/stream.h>
56*0Sstevel@tonic-gate #include <sys/strsubr.h>
57*0Sstevel@tonic-gate #include <sys/ioctl.h>
58*0Sstevel@tonic-gate #include <sys/stropts.h>
59*0Sstevel@tonic-gate #include <sys/vnode.h>
60*0Sstevel@tonic-gate #include <sys/tihdr.h>
61*0Sstevel@tonic-gate #include <sys/timod.h>
62*0Sstevel@tonic-gate #include <sys/tiuser.h>
63*0Sstevel@tonic-gate #include <sys/t_kuser.h>
64*0Sstevel@tonic-gate #include <sys/kmem.h>
65*0Sstevel@tonic-gate #include <sys/sysmacros.h>
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate int
t_kbind(TIUSER * tiptr,struct t_bind * req,struct t_bind * ret)68*0Sstevel@tonic-gate t_kbind(TIUSER *tiptr, struct t_bind *req, struct t_bind *ret)
69*0Sstevel@tonic-gate {
70*0Sstevel@tonic-gate 	struct T_bind_req	*bind_req;
71*0Sstevel@tonic-gate 	struct T_bind_ack	*bind_ack;
72*0Sstevel@tonic-gate 	int 			bindsz;
73*0Sstevel@tonic-gate 	vnode_t 		*vp;
74*0Sstevel@tonic-gate 	file_t 			*fp;
75*0Sstevel@tonic-gate 	char 			*buf;
76*0Sstevel@tonic-gate 	struct strioctl 	strioc;
77*0Sstevel@tonic-gate 	int			retval;
78*0Sstevel@tonic-gate 	int			error;
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate 	error = 0;
81*0Sstevel@tonic-gate 	retval = 0;
82*0Sstevel@tonic-gate 	fp = tiptr->fp;
83*0Sstevel@tonic-gate 	vp = fp->f_vnode;
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate 	/*
86*0Sstevel@tonic-gate 	 * send the ioctl request and wait
87*0Sstevel@tonic-gate 	 * for a reply.
88*0Sstevel@tonic-gate 	 */
89*0Sstevel@tonic-gate 	bindsz = (req == NULL) ? 0 : req->addr.len;
90*0Sstevel@tonic-gate 	bindsz = MAX(bindsz, tiptr->tp_info.addr);
91*0Sstevel@tonic-gate 	bindsz += MAX(TBINDREQSZ, TBINDACKSZ);
92*0Sstevel@tonic-gate 	buf = kmem_alloc(bindsz, KM_SLEEP);
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate 	/* LINTED pointer alignment */
95*0Sstevel@tonic-gate 	bind_req = (struct T_bind_req *)buf;
96*0Sstevel@tonic-gate 	bind_req->PRIM_type = T_BIND_REQ;
97*0Sstevel@tonic-gate 	bind_req->ADDR_length = (req == NULL ? 0 : req->addr.len);
98*0Sstevel@tonic-gate 	bind_req->ADDR_offset = TBINDREQSZ;
99*0Sstevel@tonic-gate 	bind_req->CONIND_number = (req == NULL ? 0 : req->qlen);
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate 	if (bind_req->ADDR_length)
102*0Sstevel@tonic-gate 		bcopy(req->addr.buf, buf + bind_req->ADDR_offset,
103*0Sstevel@tonic-gate 		    bind_req->ADDR_length);
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate 	strioc.ic_cmd = TI_BIND;
106*0Sstevel@tonic-gate 	strioc.ic_timout = 0;
107*0Sstevel@tonic-gate 	strioc.ic_dp = buf;
108*0Sstevel@tonic-gate 	strioc.ic_len = (int)TBINDREQSZ + bind_req->ADDR_length;
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate 	/*
111*0Sstevel@tonic-gate 	 * Usually ioctl()s are performed with the credential of the caller;
112*0Sstevel@tonic-gate 	 * in this particular case we specifically use the credential of
113*0Sstevel@tonic-gate 	 * the opener as this call is typically done in the context of a user
114*0Sstevel@tonic-gate 	 * process but on behalf of the kernel, e.g., a client connection
115*0Sstevel@tonic-gate 	 * to a server which is later shared by different users.
116*0Sstevel@tonic-gate 	 * At open time, we make sure to set fp->f_cred to kcred if such is
117*0Sstevel@tonic-gate 	 * the case.
118*0Sstevel@tonic-gate 	 */
119*0Sstevel@tonic-gate 	error = strdoioctl(vp->v_stream, &strioc, FNATIVE, K_TO_K, fp->f_cred,
120*0Sstevel@tonic-gate 	    &retval);
121*0Sstevel@tonic-gate 	if (error)
122*0Sstevel@tonic-gate 		goto badbind;
123*0Sstevel@tonic-gate 	if (retval) {
124*0Sstevel@tonic-gate 		if ((retval & 0xff) == TSYSERR)
125*0Sstevel@tonic-gate 			error = (retval >> 8) & 0xff;
126*0Sstevel@tonic-gate 		else
127*0Sstevel@tonic-gate 			error = t_tlitosyserr(retval & 0xff);
128*0Sstevel@tonic-gate 		goto badbind;
129*0Sstevel@tonic-gate 	}
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate 	/* LINTED pointer alignment */
132*0Sstevel@tonic-gate 	bind_ack = (struct T_bind_ack *)strioc.ic_dp;
133*0Sstevel@tonic-gate 	if (strioc.ic_len < TBINDACKSZ || bind_ack->ADDR_length == 0) {
134*0Sstevel@tonic-gate 		error = EIO;
135*0Sstevel@tonic-gate 		goto badbind;
136*0Sstevel@tonic-gate 	}
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate 	/*
139*0Sstevel@tonic-gate 	 * copy bind data into users buffer
140*0Sstevel@tonic-gate 	 */
141*0Sstevel@tonic-gate 	if (ret) {
142*0Sstevel@tonic-gate 		if (ret->addr.maxlen > bind_ack->ADDR_length)
143*0Sstevel@tonic-gate 			ret->addr.len = bind_ack->ADDR_length;
144*0Sstevel@tonic-gate 		else
145*0Sstevel@tonic-gate 			ret->addr.len = ret->addr.maxlen;
146*0Sstevel@tonic-gate 
147*0Sstevel@tonic-gate 		bcopy(buf + bind_ack->ADDR_offset, ret->addr.buf,
148*0Sstevel@tonic-gate 		    ret->addr.len);
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate 		ret->qlen = bind_ack->CONIND_number;
151*0Sstevel@tonic-gate 	}
152*0Sstevel@tonic-gate 
153*0Sstevel@tonic-gate badbind:
154*0Sstevel@tonic-gate 	kmem_free(buf, bindsz);
155*0Sstevel@tonic-gate 	return (error);
156*0Sstevel@tonic-gate }
157