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$
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
46183550Szec#include <net/if.h>
471541Srgrimes#include <net/raw_cb.h>
48185571Sbz#include <net/vnet.h>
491541Srgrimes
501541Srgrimes/*
518876Srgrimes * Routines to manage the raw protocol control blocks.
521541Srgrimes *
531541Srgrimes * TODO:
541541Srgrimes *	hash lookups by protocol family/protocol + address family
551541Srgrimes *	take care of unique address problems per AF?
561541Srgrimes *	redo address binding to allow wildcards
571541Srgrimes */
581541Srgrimes
59130514Srwatsonstruct mtx rawcb_mtx;
60195699SrwatsonVNET_DEFINE(struct rawcb_list_head, rawcb_list);
6124936Sphk
62227309Sedstatic SYSCTL_NODE(_net, OID_AUTO, raw, CTLFLAG_RW, 0,
63227309Sed    "Raw socket infrastructure");
641541Srgrimes
65180390Srwatsonstatic u_long	raw_sendspace = RAWSNDQ;
66180390SrwatsonSYSCTL_ULONG(_net_raw, OID_AUTO, sendspace, CTLFLAG_RW, &raw_sendspace, 0,
67180390Srwatson    "Default raw socket send space");
68180390Srwatson
69180390Srwatsonstatic u_long	raw_recvspace = RAWRCVQ;
70180390SrwatsonSYSCTL_ULONG(_net_raw, OID_AUTO, recvspace, CTLFLAG_RW, &raw_recvspace, 0,
71180390Srwatson    "Default raw socket receive space");
72180390Srwatson
731541Srgrimes/*
74180305Srwatson * Allocate a control block and a nominal amount of buffer space for the
75180305Srwatson * socket.
761541Srgrimes */
771541Srgrimesint
78180305Srwatsonraw_attach(struct socket *so, int proto)
791541Srgrimes{
80180305Srwatson	struct rawcb *rp = sotorawcb(so);
811541Srgrimes	int error;
821541Srgrimes
831541Srgrimes	/*
84180391Srwatson	 * It is assumed that raw_attach is called after space has been
85180391Srwatson	 * allocated for the rawcb; consumer protocols may simply allocate
86180391Srwatson	 * type struct rawcb, or a wrapper data structure that begins with a
87180391Srwatson	 * struct rawcb.
881541Srgrimes	 */
89180391Srwatson	KASSERT(rp != NULL, ("raw_attach: rp == NULL"));
90180391Srwatson
913443Sphk	error = soreserve(so, raw_sendspace, raw_recvspace);
923443Sphk	if (error)
931541Srgrimes		return (error);
941541Srgrimes	rp->rcb_socket = so;
951541Srgrimes	rp->rcb_proto.sp_family = so->so_proto->pr_domain->dom_family;
961541Srgrimes	rp->rcb_proto.sp_protocol = proto;
97130514Srwatson	mtx_lock(&rawcb_mtx);
98181803Sbz	LIST_INSERT_HEAD(&V_rawcb_list, rp, list);
99130514Srwatson	mtx_unlock(&rawcb_mtx);
1001541Srgrimes	return (0);
1011541Srgrimes}
1021541Srgrimes
1031541Srgrimes/*
104180305Srwatson * Detach the raw connection block and discard socket resources.
1051541Srgrimes */
1061541Srgrimesvoid
107180305Srwatsonraw_detach(struct rawcb *rp)
1081541Srgrimes{
1091541Srgrimes	struct socket *so = rp->rcb_socket;
1101541Srgrimes
111157370Srwatson	KASSERT(so->so_pcb == rp, ("raw_detach: so_pcb != rp"));
112157370Srwatson
113157370Srwatson	so->so_pcb = NULL;
114140775Srwatson	mtx_lock(&rawcb_mtx);
11524936Sphk	LIST_REMOVE(rp, list);
116140775Srwatson	mtx_unlock(&rawcb_mtx);
1171541Srgrimes	free((caddr_t)(rp), M_PCB);
1181541Srgrimes}
119