raw_cb.c revision 3443
1295373Sdteske/*
2295373Sdteske * Copyright (c) 1980, 1986, 1993
3295373Sdteske *	The Regents of the University of California.  All rights reserved.
4295373Sdteske *
5295373Sdteske * Redistribution and use in source and binary forms, with or without
6295373Sdteske * modification, are permitted provided that the following conditions
7295373Sdteske * are met:
8295373Sdteske * 1. Redistributions of source code must retain the above copyright
9295373Sdteske *    notice, this list of conditions and the following disclaimer.
10295373Sdteske * 2. Redistributions in binary form must reproduce the above copyright
11295373Sdteske *    notice, this list of conditions and the following disclaimer in the
12295373Sdteske *    documentation and/or other materials provided with the distribution.
13295373Sdteske * 3. All advertising materials mentioning features or use of this software
14295373Sdteske *    must display the following acknowledgement:
15295373Sdteske *	This product includes software developed by the University of
16295373Sdteske *	California, Berkeley and its contributors.
17295373Sdteske * 4. Neither the name of the University nor the names of its contributors
18295373Sdteske *    may be used to endorse or promote products derived from this software
19295373Sdteske *    without specific prior written permission.
20295373Sdteske *
21295373Sdteske * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22295373Sdteske * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23295373Sdteske * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24295373Sdteske * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25295373Sdteske * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26295373Sdteske * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27295373Sdteske * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28295373Sdteske * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29295373Sdteske * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30295373Sdteske * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31295373Sdteske * SUCH DAMAGE.
32295373Sdteske *
33295373Sdteske *	@(#)raw_cb.c	8.1 (Berkeley) 6/10/93
34295373Sdteske * $Id: raw_cb.c,v 1.2 1994/08/02 07:46:34 davidg Exp $
35295373Sdteske */
36295400Sdteske
37295400Sdteske#include <sys/param.h>
38295400Sdteske#include <sys/systm.h>
39295373Sdteske#include <sys/mbuf.h>
40295400Sdteske#include <sys/socket.h>
41295400Sdteske#include <sys/socketvar.h>
42295373Sdteske#include <sys/domain.h>
43295373Sdteske#include <sys/protosw.h>
44295373Sdteske#include <sys/errno.h>
45295373Sdteske
46295373Sdteske#include <net/if.h>
47295373Sdteske#include <net/route.h>
48295373Sdteske#include <net/raw_cb.h>
49295373Sdteske#include <netinet/in.h>
50295373Sdteske
51295373Sdteske/*
52295373Sdteske * Routines to manage the raw protocol control blocks.
53295373Sdteske *
54295373Sdteske * TODO:
55295373Sdteske *	hash lookups by protocol family/protocol + address family
56295373Sdteske *	take care of unique address problems per AF?
57295373Sdteske *	redo address binding to allow wildcards
58295373Sdteske */
59295373Sdteske
60295373Sdteskeu_long	raw_sendspace = RAWSNDQ;
61295373Sdteskeu_long	raw_recvspace = RAWRCVQ;
62295373Sdteske
63295373Sdteske/*
64295373Sdteske * Allocate a control block and a nominal amount
65295373Sdteske * of buffer space for the socket.
66295373Sdteske */
67295373Sdteskeint
68295373Sdteskeraw_attach(so, proto)
69295373Sdteske	register struct socket *so;
70295373Sdteske	int proto;
71295373Sdteske{
72295373Sdteske	register struct rawcb *rp = sotorawcb(so);
73295373Sdteske	int error;
74295373Sdteske
75295373Sdteske	/*
76295373Sdteske	 * It is assumed that raw_attach is called
77295373Sdteske	 * after space has been allocated for the
78295373Sdteske	 * rawcb.
79295373Sdteske	 */
80295373Sdteske	if (rp == 0)
81295373Sdteske		return (ENOBUFS);
82295373Sdteske	error = soreserve(so, raw_sendspace, raw_recvspace);
83295373Sdteske	if (error)
84295373Sdteske		return (error);
85295373Sdteske	rp->rcb_socket = so;
86295373Sdteske	rp->rcb_proto.sp_family = so->so_proto->pr_domain->dom_family;
87295373Sdteske	rp->rcb_proto.sp_protocol = proto;
88295373Sdteske	insque(rp, &rawcb);
89295373Sdteske	return (0);
90295373Sdteske}
91295373Sdteske
92295373Sdteske/*
93295373Sdteske * Detach the raw connection block and discard
94295373Sdteske * socket resources.
95295373Sdteske */
96295373Sdteskevoid
97295373Sdteskeraw_detach(rp)
98295373Sdteske	register struct rawcb *rp;
99295373Sdteske{
100295373Sdteske	struct socket *so = rp->rcb_socket;
101295373Sdteske
102295373Sdteske	so->so_pcb = 0;
103295373Sdteske	sofree(so);
104295373Sdteske	remque(rp);
105295373Sdteske#ifdef notdef
106295373Sdteske	if (rp->rcb_laddr)
107295373Sdteske		m_freem(dtom(rp->rcb_laddr));
108295373Sdteske	rp->rcb_laddr = 0;
109295373Sdteske#endif
110295373Sdteske	free((caddr_t)(rp), M_PCB);
111295373Sdteske}
112295373Sdteske
113295373Sdteske/*
114295373Sdteske * Disconnect and possibly release resources.
115295373Sdteske */
116295373Sdteskevoid
117295373Sdteskeraw_disconnect(rp)
118295373Sdteske	struct rawcb *rp;
119295373Sdteske{
120295373Sdteske
121295373Sdteske#ifdef notdef
122295373Sdteske	if (rp->rcb_faddr)
123295373Sdteske		m_freem(dtom(rp->rcb_faddr));
124295373Sdteske	rp->rcb_faddr = 0;
125295441Sdteske#endif
126295373Sdteske	if (rp->rcb_socket->so_state & SS_NOFDREF)
127295373Sdteske		raw_detach(rp);
128295373Sdteske}
129295373Sdteske
130295373Sdteske#ifdef notdef
131295373Sdteskeint
132295373Sdteskeraw_bind(so, nam)
133295373Sdteske	register struct socket *so;
134295373Sdteske	struct mbuf *nam;
135295373Sdteske{
136295373Sdteske	struct sockaddr *addr = mtod(nam, struct sockaddr *);
137295373Sdteske	register struct rawcb *rp;
138295373Sdteske
139295373Sdteske	if (ifnet == 0)
140295373Sdteske		return (EADDRNOTAVAIL);
141295373Sdteske	rp = sotorawcb(so);
142295373Sdteske	nam = m_copym(nam, 0, M_COPYALL, M_WAITOK);
143295373Sdteske	rp->rcb_laddr = mtod(nam, struct sockaddr *);
144295373Sdteske	return (0);
145295373Sdteske}
146295373Sdteske#endif
147295373Sdteske