xref: /xnu-8792.81.2/bsd/net/raw_cb.c (revision 19c3b8c28c31cb8130e034cfb5df6bf9ba342d90)
1*19c3b8c2SApple OSS Distributions /*
2*19c3b8c2SApple OSS Distributions  * Copyright (c) 2000-2012 Apple Inc. All rights reserved.
3*19c3b8c2SApple OSS Distributions  *
4*19c3b8c2SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*19c3b8c2SApple OSS Distributions  *
6*19c3b8c2SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*19c3b8c2SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*19c3b8c2SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*19c3b8c2SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*19c3b8c2SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*19c3b8c2SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*19c3b8c2SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*19c3b8c2SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*19c3b8c2SApple OSS Distributions  *
15*19c3b8c2SApple OSS Distributions  * Please obtain a copy of the License at
16*19c3b8c2SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*19c3b8c2SApple OSS Distributions  *
18*19c3b8c2SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*19c3b8c2SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*19c3b8c2SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*19c3b8c2SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*19c3b8c2SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*19c3b8c2SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*19c3b8c2SApple OSS Distributions  * limitations under the License.
25*19c3b8c2SApple OSS Distributions  *
26*19c3b8c2SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*19c3b8c2SApple OSS Distributions  */
28*19c3b8c2SApple OSS Distributions /*
29*19c3b8c2SApple OSS Distributions  * Copyright (c) 1980, 1986, 1993
30*19c3b8c2SApple OSS Distributions  *	The Regents of the University of California.  All rights reserved.
31*19c3b8c2SApple OSS Distributions  *
32*19c3b8c2SApple OSS Distributions  * Redistribution and use in source and binary forms, with or without
33*19c3b8c2SApple OSS Distributions  * modification, are permitted provided that the following conditions
34*19c3b8c2SApple OSS Distributions  * are met:
35*19c3b8c2SApple OSS Distributions  * 1. Redistributions of source code must retain the above copyright
36*19c3b8c2SApple OSS Distributions  *    notice, this list of conditions and the following disclaimer.
37*19c3b8c2SApple OSS Distributions  * 2. Redistributions in binary form must reproduce the above copyright
38*19c3b8c2SApple OSS Distributions  *    notice, this list of conditions and the following disclaimer in the
39*19c3b8c2SApple OSS Distributions  *    documentation and/or other materials provided with the distribution.
40*19c3b8c2SApple OSS Distributions  * 3. All advertising materials mentioning features or use of this software
41*19c3b8c2SApple OSS Distributions  *    must display the following acknowledgement:
42*19c3b8c2SApple OSS Distributions  *	This product includes software developed by the University of
43*19c3b8c2SApple OSS Distributions  *	California, Berkeley and its contributors.
44*19c3b8c2SApple OSS Distributions  * 4. Neither the name of the University nor the names of its contributors
45*19c3b8c2SApple OSS Distributions  *    may be used to endorse or promote products derived from this software
46*19c3b8c2SApple OSS Distributions  *    without specific prior written permission.
47*19c3b8c2SApple OSS Distributions  *
48*19c3b8c2SApple OSS Distributions  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49*19c3b8c2SApple OSS Distributions  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50*19c3b8c2SApple OSS Distributions  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51*19c3b8c2SApple OSS Distributions  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52*19c3b8c2SApple OSS Distributions  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53*19c3b8c2SApple OSS Distributions  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54*19c3b8c2SApple OSS Distributions  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55*19c3b8c2SApple OSS Distributions  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56*19c3b8c2SApple OSS Distributions  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57*19c3b8c2SApple OSS Distributions  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58*19c3b8c2SApple OSS Distributions  * SUCH DAMAGE.
59*19c3b8c2SApple OSS Distributions  *
60*19c3b8c2SApple OSS Distributions  *	@(#)raw_cb.c	8.1 (Berkeley) 6/10/93
61*19c3b8c2SApple OSS Distributions  */
62*19c3b8c2SApple OSS Distributions 
63*19c3b8c2SApple OSS Distributions #include <sys/param.h>
64*19c3b8c2SApple OSS Distributions #include <sys/malloc.h>
65*19c3b8c2SApple OSS Distributions #include <sys/socket.h>
66*19c3b8c2SApple OSS Distributions #include <sys/socketvar.h>
67*19c3b8c2SApple OSS Distributions #include <sys/domain.h>
68*19c3b8c2SApple OSS Distributions #include <sys/protosw.h>
69*19c3b8c2SApple OSS Distributions #include <kern/locks.h>
70*19c3b8c2SApple OSS Distributions 
71*19c3b8c2SApple OSS Distributions #include <net/raw_cb.h>
72*19c3b8c2SApple OSS Distributions 
73*19c3b8c2SApple OSS Distributions /*
74*19c3b8c2SApple OSS Distributions  * Routines to manage the raw protocol control blocks.
75*19c3b8c2SApple OSS Distributions  *
76*19c3b8c2SApple OSS Distributions  * TODO:
77*19c3b8c2SApple OSS Distributions  *	hash lookups by protocol family/protocol + address family
78*19c3b8c2SApple OSS Distributions  *	take care of unique address problems per AF?
79*19c3b8c2SApple OSS Distributions  *	redo address binding to allow wildcards
80*19c3b8c2SApple OSS Distributions  */
81*19c3b8c2SApple OSS Distributions 
82*19c3b8c2SApple OSS Distributions struct rawcb_list_head rawcb_list = LIST_HEAD_INITIALIZER(rawcb_list);
83*19c3b8c2SApple OSS Distributions 
84*19c3b8c2SApple OSS Distributions static uint32_t raw_sendspace = RAWSNDQ;
85*19c3b8c2SApple OSS Distributions static uint32_t raw_recvspace = RAWRCVQ;
86*19c3b8c2SApple OSS Distributions extern lck_mtx_t        raw_mtx;       /*### global raw cb mutex for now */
87*19c3b8c2SApple OSS Distributions 
88*19c3b8c2SApple OSS Distributions /*
89*19c3b8c2SApple OSS Distributions  * Allocate a control block and a nominal amount
90*19c3b8c2SApple OSS Distributions  * of buffer space for the socket.
91*19c3b8c2SApple OSS Distributions  */
92*19c3b8c2SApple OSS Distributions int
raw_attach(struct socket * so,int proto)93*19c3b8c2SApple OSS Distributions raw_attach(struct socket *so, int proto)
94*19c3b8c2SApple OSS Distributions {
95*19c3b8c2SApple OSS Distributions 	struct rawcb *rp = sotorawcb(so);
96*19c3b8c2SApple OSS Distributions 	int error;
97*19c3b8c2SApple OSS Distributions 
98*19c3b8c2SApple OSS Distributions 	/*
99*19c3b8c2SApple OSS Distributions 	 * It is assumed that raw_attach is called
100*19c3b8c2SApple OSS Distributions 	 * after space has been allocated for the
101*19c3b8c2SApple OSS Distributions 	 * rawcb.
102*19c3b8c2SApple OSS Distributions 	 */
103*19c3b8c2SApple OSS Distributions 	if (rp == 0) {
104*19c3b8c2SApple OSS Distributions 		return ENOBUFS;
105*19c3b8c2SApple OSS Distributions 	}
106*19c3b8c2SApple OSS Distributions 	error = soreserve(so, raw_sendspace, raw_recvspace);
107*19c3b8c2SApple OSS Distributions 	if (error) {
108*19c3b8c2SApple OSS Distributions 		return error;
109*19c3b8c2SApple OSS Distributions 	}
110*19c3b8c2SApple OSS Distributions 	rp->rcb_socket = so;
111*19c3b8c2SApple OSS Distributions 	rp->rcb_proto.sp_family = (uint16_t)SOCK_DOM(so);
112*19c3b8c2SApple OSS Distributions 	rp->rcb_proto.sp_protocol = (uint16_t)proto;
113*19c3b8c2SApple OSS Distributions 	lck_mtx_lock(&raw_mtx);
114*19c3b8c2SApple OSS Distributions 	LIST_INSERT_HEAD(&rawcb_list, rp, list);
115*19c3b8c2SApple OSS Distributions 	lck_mtx_unlock(&raw_mtx);
116*19c3b8c2SApple OSS Distributions 	return 0;
117*19c3b8c2SApple OSS Distributions }
118*19c3b8c2SApple OSS Distributions 
119*19c3b8c2SApple OSS Distributions /*
120*19c3b8c2SApple OSS Distributions  * Detach the raw connection block and discard
121*19c3b8c2SApple OSS Distributions  * socket resources.
122*19c3b8c2SApple OSS Distributions  */
123*19c3b8c2SApple OSS Distributions void
raw_detach_nofree(struct rawcb * rp)124*19c3b8c2SApple OSS Distributions raw_detach_nofree(struct rawcb *rp)
125*19c3b8c2SApple OSS Distributions {
126*19c3b8c2SApple OSS Distributions 	struct socket *so = rp->rcb_socket;
127*19c3b8c2SApple OSS Distributions 
128*19c3b8c2SApple OSS Distributions 	so->so_pcb = 0;
129*19c3b8c2SApple OSS Distributions 	so->so_flags |= SOF_PCBCLEARING;
130*19c3b8c2SApple OSS Distributions 	sofree(so);
131*19c3b8c2SApple OSS Distributions 	if (!lck_mtx_try_lock(&raw_mtx)) {
132*19c3b8c2SApple OSS Distributions 		socket_unlock(so, 0);
133*19c3b8c2SApple OSS Distributions 		lck_mtx_lock(&raw_mtx);
134*19c3b8c2SApple OSS Distributions 		socket_lock(so, 0);
135*19c3b8c2SApple OSS Distributions 	}
136*19c3b8c2SApple OSS Distributions 	LIST_REMOVE(rp, list);
137*19c3b8c2SApple OSS Distributions 	lck_mtx_unlock(&raw_mtx);
138*19c3b8c2SApple OSS Distributions #ifdef notdef
139*19c3b8c2SApple OSS Distributions 	if (rp->rcb_laddr) {
140*19c3b8c2SApple OSS Distributions 		m_freem(dtom(rp->rcb_laddr));
141*19c3b8c2SApple OSS Distributions 	}
142*19c3b8c2SApple OSS Distributions 	rp->rcb_laddr = 0;
143*19c3b8c2SApple OSS Distributions #endif
144*19c3b8c2SApple OSS Distributions 	rp->rcb_socket = NULL;
145*19c3b8c2SApple OSS Distributions }
146*19c3b8c2SApple OSS Distributions 
147*19c3b8c2SApple OSS Distributions /*
148*19c3b8c2SApple OSS Distributions  * Disconnect and possibly release resources.
149*19c3b8c2SApple OSS Distributions  */
150*19c3b8c2SApple OSS Distributions void
raw_disconnect(struct rawcb * rp)151*19c3b8c2SApple OSS Distributions raw_disconnect(struct rawcb *rp)
152*19c3b8c2SApple OSS Distributions {
153*19c3b8c2SApple OSS Distributions 	struct socket *so = rp->rcb_socket;
154*19c3b8c2SApple OSS Distributions 
155*19c3b8c2SApple OSS Distributions #ifdef notdef
156*19c3b8c2SApple OSS Distributions 	if (rp->rcb_faddr) {
157*19c3b8c2SApple OSS Distributions 		m_freem(dtom(rp->rcb_faddr));
158*19c3b8c2SApple OSS Distributions 	}
159*19c3b8c2SApple OSS Distributions 	rp->rcb_faddr = 0;
160*19c3b8c2SApple OSS Distributions #endif
161*19c3b8c2SApple OSS Distributions 	/*
162*19c3b8c2SApple OSS Distributions 	 * A multipath subflow socket would have its SS_NOFDREF set by default,
163*19c3b8c2SApple OSS Distributions 	 * so check for SOF_MP_SUBFLOW socket flag before detaching the PCB;
164*19c3b8c2SApple OSS Distributions 	 * when the socket is closed for real, SOF_MP_SUBFLOW would be cleared.
165*19c3b8c2SApple OSS Distributions 	 */
166*19c3b8c2SApple OSS Distributions 	if (!(so->so_flags & SOF_MP_SUBFLOW) && (so->so_state & SS_NOFDREF)) {
167*19c3b8c2SApple OSS Distributions 		raw_detach_nofree(rp);
168*19c3b8c2SApple OSS Distributions 		kfree_type(struct rawcb, rp);
169*19c3b8c2SApple OSS Distributions 	}
170*19c3b8c2SApple OSS Distributions }
171*19c3b8c2SApple OSS Distributions 
172*19c3b8c2SApple OSS Distributions #ifdef notdef
173*19c3b8c2SApple OSS Distributions #include <sys/mbuf.h>
174*19c3b8c2SApple OSS Distributions 
175*19c3b8c2SApple OSS Distributions int
raw_bind(struct socket * so,struct mbuf * nam)176*19c3b8c2SApple OSS Distributions raw_bind(struct socket *so, struct mbuf *nam)
177*19c3b8c2SApple OSS Distributions {
178*19c3b8c2SApple OSS Distributions 	struct sockaddr *addr = mtod(nam, struct sockaddr *);
179*19c3b8c2SApple OSS Distributions 	struct rawcb *rp;
180*19c3b8c2SApple OSS Distributions 
181*19c3b8c2SApple OSS Distributions 	if (ifnet == 0) {
182*19c3b8c2SApple OSS Distributions 		return EADDRNOTAVAIL;
183*19c3b8c2SApple OSS Distributions 	}
184*19c3b8c2SApple OSS Distributions 	rp = sotorawcb(so);
185*19c3b8c2SApple OSS Distributions 	nam = m_copym(nam, 0, M_COPYALL, M_WAITOK);
186*19c3b8c2SApple OSS Distributions 	if (nam == NULL) {
187*19c3b8c2SApple OSS Distributions 		return ENOBUFS;
188*19c3b8c2SApple OSS Distributions 	}
189*19c3b8c2SApple OSS Distributions 	rp->rcb_laddr = mtod(nam, struct sockaddr *);
190*19c3b8c2SApple OSS Distributions 	return 0;
191*19c3b8c2SApple OSS Distributions }
192*19c3b8c2SApple OSS Distributions #endif
193