raw_cb.c revision 180390
1139823Simp/*-
21541Srgrimes * Copyright (c) 1980, 1986, 1993
3180305Srwatson *	The Regents of the University of California.
4180305Srwatson * All rights reserved.
51541Srgrimes *
61541Srgrimes * Redistribution and use in source and binary forms, with or without
71541Srgrimes * modification, are permitted provided that the following conditions
81541Srgrimes * are met:
91541Srgrimes * 1. Redistributions of source code must retain the above copyright
101541Srgrimes *    notice, this list of conditions and the following disclaimer.
111541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
121541Srgrimes *    notice, this list of conditions and the following disclaimer in the
131541Srgrimes *    documentation and/or other materials provided with the distribution.
141541Srgrimes * 4. Neither the name of the University nor the names of its contributors
151541Srgrimes *    may be used to endorse or promote products derived from this software
161541Srgrimes *    without specific prior written permission.
171541Srgrimes *
181541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
191541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
201541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
211541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
221541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
231541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
241541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
251541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
261541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
271541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
281541Srgrimes * SUCH DAMAGE.
291541Srgrimes *
301541Srgrimes *	@(#)raw_cb.c	8.1 (Berkeley) 6/10/93
3150477Speter * $FreeBSD: head/sys/net/raw_cb.c 180390 2008-07-09 18:39:55Z rwatson $
321541Srgrimes */
331541Srgrimes
341541Srgrimes#include <sys/param.h>
3596972Stanimura#include <sys/domain.h>
36130387Srwatson#include <sys/lock.h>
37180390Srwatson#include <sys/kernel.h>
3829024Sbde#include <sys/malloc.h>
39130387Srwatson#include <sys/mutex.h>
4096972Stanimura#include <sys/protosw.h>
411541Srgrimes#include <sys/socket.h>
421541Srgrimes#include <sys/socketvar.h>
43180390Srwatson#include <sys/sysctl.h>
4497093Sbde#include <sys/systm.h>
451541Srgrimes
461541Srgrimes#include <net/raw_cb.h>
471541Srgrimes
481541Srgrimes/*
498876Srgrimes * Routines to manage the raw protocol control blocks.
501541Srgrimes *
511541Srgrimes * TODO:
521541Srgrimes *	hash lookups by protocol family/protocol + address family
531541Srgrimes *	take care of unique address problems per AF?
541541Srgrimes *	redo address binding to allow wildcards
551541Srgrimes */
561541Srgrimes
57130514Srwatsonstruct mtx rawcb_mtx;
5824936Sphkstruct rawcb_list_head rawcb_list;
5924936Sphk
60180390SrwatsonSYSCTL_NODE(_net, OID_AUTO, raw, CTLFLAG_RW, 0, "Raw socket infrastructure");
611541Srgrimes
62180390Srwatsonstatic u_long	raw_sendspace = RAWSNDQ;
63180390SrwatsonSYSCTL_ULONG(_net_raw, OID_AUTO, sendspace, CTLFLAG_RW, &raw_sendspace, 0,
64180390Srwatson    "Default raw socket send space");
65180390Srwatson
66180390Srwatsonstatic u_long	raw_recvspace = RAWRCVQ;
67180390SrwatsonSYSCTL_ULONG(_net_raw, OID_AUTO, recvspace, CTLFLAG_RW, &raw_recvspace, 0,
68180390Srwatson    "Default raw socket receive space");
69180390Srwatson
701541Srgrimes/*
71180305Srwatson * Allocate a control block and a nominal amount of buffer space for the
72180305Srwatson * socket.
731541Srgrimes */
741541Srgrimesint
75180305Srwatsonraw_attach(struct socket *so, int proto)
761541Srgrimes{
77180305Srwatson	struct rawcb *rp = sotorawcb(so);
781541Srgrimes	int error;
791541Srgrimes
801541Srgrimes	/*
811541Srgrimes	 * It is assumed that raw_attach is called
821541Srgrimes	 * after space has been allocated for the
831541Srgrimes	 * rawcb.
841541Srgrimes	 */
851541Srgrimes	if (rp == 0)
861541Srgrimes		return (ENOBUFS);
873443Sphk	error = soreserve(so, raw_sendspace, raw_recvspace);
883443Sphk	if (error)
891541Srgrimes		return (error);
901541Srgrimes	rp->rcb_socket = so;
911541Srgrimes	rp->rcb_proto.sp_family = so->so_proto->pr_domain->dom_family;
921541Srgrimes	rp->rcb_proto.sp_protocol = proto;
93130514Srwatson	mtx_lock(&rawcb_mtx);
9424936Sphk	LIST_INSERT_HEAD(&rawcb_list, rp, list);
95130514Srwatson	mtx_unlock(&rawcb_mtx);
961541Srgrimes	return (0);
971541Srgrimes}
981541Srgrimes
991541Srgrimes/*
100180305Srwatson * Detach the raw connection block and discard socket resources.
1011541Srgrimes */
1021541Srgrimesvoid
103180305Srwatsonraw_detach(struct rawcb *rp)
1041541Srgrimes{
1051541Srgrimes	struct socket *so = rp->rcb_socket;
1061541Srgrimes
107157370Srwatson	KASSERT(so->so_pcb == rp, ("raw_detach: so_pcb != rp"));
108157370Srwatson
109157370Srwatson	so->so_pcb = NULL;
110140775Srwatson	mtx_lock(&rawcb_mtx);
11124936Sphk	LIST_REMOVE(rp, list);
112140775Srwatson	mtx_unlock(&rawcb_mtx);
1131541Srgrimes	free((caddr_t)(rp), M_PCB);
1141541Srgrimes}
115