raw_cb.c revision 157370
1234285Sdim/*-
2234285Sdim * Copyright (c) 1980, 1986, 1993
3234285Sdim *	The Regents of the University of California.  All rights reserved.
4234285Sdim *
5234285Sdim * Redistribution and use in source and binary forms, with or without
6234285Sdim * modification, are permitted provided that the following conditions
7234285Sdim * are met:
8234285Sdim * 1. Redistributions of source code must retain the above copyright
9234285Sdim *    notice, this list of conditions and the following disclaimer.
10234285Sdim * 2. Redistributions in binary form must reproduce the above copyright
11234285Sdim *    notice, this list of conditions and the following disclaimer in the
12234285Sdim *    documentation and/or other materials provided with the distribution.
13234285Sdim * 4. Neither the name of the University nor the names of its contributors
14234285Sdim *    may be used to endorse or promote products derived from this software
15341825Sdim *    without specific prior written permission.
16321369Sdim *
17234285Sdim * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18341825Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19234285Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20280031Sdim * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21280031Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22276479Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23249423Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24261991Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25288943Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26234285Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27280031Sdim * SUCH DAMAGE.
28234285Sdim *
29234285Sdim *	@(#)raw_cb.c	8.1 (Berkeley) 6/10/93
30234285Sdim * $FreeBSD: head/sys/net/raw_cb.c 157370 2006-04-01 15:42:02Z rwatson $
31234285Sdim */
32234285Sdim
33280031Sdim#include <sys/param.h>
34234285Sdim#include <sys/domain.h>
35276479Sdim#include <sys/lock.h>
36276479Sdim#include <sys/malloc.h>
37234285Sdim#include <sys/mutex.h>
38234285Sdim#include <sys/protosw.h>
39234285Sdim#include <sys/socket.h>
40280031Sdim#include <sys/socketvar.h>
41234285Sdim#include <sys/systm.h>
42234285Sdim
43280031Sdim#include <net/raw_cb.h>
44234285Sdim
45234285Sdim/*
46280031Sdim * Routines to manage the raw protocol control blocks.
47280031Sdim *
48234285Sdim * TODO:
49234285Sdim *	hash lookups by protocol family/protocol + address family
50234285Sdim *	take care of unique address problems per AF?
51234285Sdim *	redo address binding to allow wildcards
52234285Sdim */
53280031Sdim
54280031Sdimstruct mtx rawcb_mtx;
55234285Sdimstruct rawcb_list_head rawcb_list;
56280031Sdim
57234285Sdimconst static u_long	raw_sendspace = RAWSNDQ;
58234285Sdimconst static u_long	raw_recvspace = RAWRCVQ;
59234285Sdim
60280031Sdim/*
61234285Sdim * Allocate a control block and a nominal amount
62341825Sdim * of buffer space for the socket.
63341825Sdim */
64234285Sdimint
65341825Sdimraw_attach(so, proto)
66234285Sdim	register struct socket *so;
67234285Sdim	int proto;
68234285Sdim{
69234285Sdim	register struct rawcb *rp = sotorawcb(so);
70280031Sdim	int error;
71280031Sdim
72234285Sdim	/*
73341825Sdim	 * It is assumed that raw_attach is called
74341825Sdim	 * after space has been allocated for the
75234285Sdim	 * rawcb.
76341825Sdim	 */
77234285Sdim	if (rp == 0)
78234285Sdim		return (ENOBUFS);
79234285Sdim	error = soreserve(so, raw_sendspace, raw_recvspace);
80234285Sdim	if (error)
81280031Sdim		return (error);
82280031Sdim	rp->rcb_socket = so;
83280031Sdim	rp->rcb_proto.sp_family = so->so_proto->pr_domain->dom_family;
84280031Sdim	rp->rcb_proto.sp_protocol = proto;
85234285Sdim	mtx_lock(&rawcb_mtx);
86234285Sdim	LIST_INSERT_HEAD(&rawcb_list, rp, list);
87234285Sdim	mtx_unlock(&rawcb_mtx);
88280031Sdim	return (0);
89280031Sdim}
90341825Sdim
91234285Sdim/*
92261991Sdim * Detach the raw connection block and discard
93288943Sdim * socket resources.
94288943Sdim */
95314564Sdimvoid
96288943Sdimraw_detach(rp)
97261991Sdim	register struct rawcb *rp;
98314564Sdim{
99314564Sdim	struct socket *so = rp->rcb_socket;
100288943Sdim
101288943Sdim	KASSERT(so->so_pcb == rp, ("raw_detach: so_pcb != rp"));
102314564Sdim
103314564Sdim	so->so_pcb = NULL;
104288943Sdim	mtx_lock(&rawcb_mtx);
105288943Sdim	LIST_REMOVE(rp, list);
106288943Sdim	mtx_unlock(&rawcb_mtx);
107288943Sdim#ifdef notdef
108288943Sdim	if (rp->rcb_laddr)
109288943Sdim		m_freem(dtom(rp->rcb_laddr));
110341825Sdim	rp->rcb_laddr = 0;
111341825Sdim#endif
112341825Sdim	free((caddr_t)(rp), M_PCB);
113288943Sdim}
114261991Sdim
115341825Sdim/*
116341825Sdim * Disconnect and possibly release resources.
117341825Sdim */
118341825Sdimvoid
119341825Sdimraw_disconnect(rp)
120341825Sdim	struct rawcb *rp;
121341825Sdim{
122341825Sdim
123341825Sdim#ifdef notdef
124341825Sdim	if (rp->rcb_faddr)
125341825Sdim		m_freem(dtom(rp->rcb_faddr));
126341825Sdim	rp->rcb_faddr = 0;
127341825Sdim#endif
128341825Sdim	if (rp->rcb_socket->so_state & SS_NOFDREF)
129341825Sdim		raw_detach(rp);
130341825Sdim}
131341825Sdim
132341825Sdim#ifdef notdef
133341825Sdim#include <sys/mbuf.h>
134341825Sdim
135341825Sdimint
136341825Sdimraw_bind(so, nam)
137341825Sdim	register struct socket *so;
138261991Sdim	struct mbuf *nam;
139280031Sdim{
140280031Sdim	struct sockaddr *addr = mtod(nam, struct sockaddr *);
141261991Sdim	register struct rawcb *rp;
142261991Sdim
143280031Sdim	if (ifnet == 0)
144280031Sdim		return (EADDRNOTAVAIL);
145261991Sdim	rp = sotorawcb(so);
146280031Sdim	nam = m_copym(nam, 0, M_COPYALL, M_TRYWAIT);
147280031Sdim	rp->rcb_laddr = mtod(nam, struct sockaddr *);
148280031Sdim	return (0);
149280031Sdim}
150261991Sdim#endif
151280031Sdim