1323124Sdes/*-
276259Sgreen * Copyright (c) 1980, 1986, 1993
376259Sgreen *	The Regents of the University of California.
476259Sgreen * All rights reserved.
576259Sgreen *
676259Sgreen * Redistribution and use in source and binary forms, with or without
776259Sgreen * modification, are permitted provided that the following conditions
876259Sgreen * are met:
976259Sgreen * 1. Redistributions of source code must retain the above copyright
1076259Sgreen *    notice, this list of conditions and the following disclaimer.
1176259Sgreen * 2. Redistributions in binary form must reproduce the above copyright
1276259Sgreen *    notice, this list of conditions and the following disclaimer in the
1376259Sgreen *    documentation and/or other materials provided with the distribution.
1476259Sgreen * 4. Neither the name of the University nor the names of its contributors
1576259Sgreen *    may be used to endorse or promote products derived from this software
1676259Sgreen *    without specific prior written permission.
1776259Sgreen *
1876259Sgreen * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1976259Sgreen * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2076259Sgreen * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2176259Sgreen * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2276259Sgreen * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2376259Sgreen * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2476259Sgreen * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25323124Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2676259Sgreen * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2776259Sgreen * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2876259Sgreen * SUCH DAMAGE.
2976259Sgreen *
30124208Sdes *	@(#)raw_cb.c	8.1 (Berkeley) 6/10/93
3176259Sgreen * $FreeBSD$
3276259Sgreen */
33113908Sdes
34262566Sdes#include <sys/param.h>
35126274Sdes#include <sys/domain.h>
3676259Sgreen#include <sys/lock.h>
37204917Sdes#include <sys/kernel.h>
38204917Sdes#include <sys/malloc.h>
39126274Sdes#include <sys/mutex.h>
40204917Sdes#include <sys/protosw.h>
41221420Sdes#include <sys/socket.h>
4276259Sgreen#include <sys/socketvar.h>
43204917Sdes#include <sys/sysctl.h>
4492555Sdes#include <sys/systm.h>
4592555Sdes
46126274Sdes#include <net/if.h>
4792555Sdes#include <net/raw_cb.h>
48113908Sdes#include <net/vnet.h>
4976259Sgreen
50192595Sdes/*
51192595Sdes * Routines to manage the raw protocol control blocks.
5276259Sgreen *
53221420Sdes * TODO:
54221420Sdes *	hash lookups by protocol family/protocol + address family
55113908Sdes *	take care of unique address problems per AF?
56221420Sdes *	redo address binding to allow wildcards
57221420Sdes */
58124208Sdes
59124208Sdesstruct mtx rawcb_mtx;
60124208SdesVNET_DEFINE(struct rawcb_list_head, rawcb_list);
6176259Sgreen
6276259Sgreenstatic SYSCTL_NODE(_net, OID_AUTO, raw, CTLFLAG_RW, 0,
6376259Sgreen    "Raw socket infrastructure");
6476259Sgreen
6576259Sgreenstatic u_long	raw_sendspace = RAWSNDQ;
6676259SgreenSYSCTL_ULONG(_net_raw, OID_AUTO, sendspace, CTLFLAG_RW, &raw_sendspace, 0,
6776259Sgreen    "Default raw socket send space");
6876259Sgreen
6976259Sgreenstatic u_long	raw_recvspace = RAWRCVQ;
7076259SgreenSYSCTL_ULONG(_net_raw, OID_AUTO, recvspace, CTLFLAG_RW, &raw_recvspace, 0,
7176259Sgreen    "Default raw socket receive space");
7292555Sdes
7376259Sgreen/*
7476259Sgreen * Allocate a control block and a nominal amount of buffer space for the
7592555Sdes * socket.
7692555Sdes */
7792555Sdesint
7876259Sgreenraw_attach(struct socket *so, int proto)
79146998Sdes{
80146998Sdes	struct rawcb *rp = sotorawcb(so);
81146998Sdes	int error;
8276259Sgreen
83124208Sdes	/*
84124208Sdes	 * It is assumed that raw_attach is called after space has been
85124208Sdes	 * allocated for the rawcb; consumer protocols may simply allocate
86157016Sdes	 * type struct rawcb, or a wrapper data structure that begins with a
87124208Sdes	 * struct rawcb.
88124208Sdes	 */
89124208Sdes	KASSERT(rp != NULL, ("raw_attach: rp == NULL"));
90124208Sdes
91124208Sdes	error = soreserve(so, raw_sendspace, raw_recvspace);
92226046Sdes	if (error)
93226046Sdes		return (error);
94226046Sdes	rp->rcb_socket = so;
95226046Sdes	rp->rcb_proto.sp_family = so->so_proto->pr_domain->dom_family;
9676259Sgreen	rp->rcb_proto.sp_protocol = proto;
9776259Sgreen	mtx_lock(&rawcb_mtx);
98126274Sdes	LIST_INSERT_HEAD(&V_rawcb_list, rp, list);
99126274Sdes	mtx_unlock(&rawcb_mtx);
100204917Sdes	return (0);
101204917Sdes}
102204917Sdes
103204917Sdes/*
104204917Sdes * Detach the raw connection block and discard socket resources.
105204917Sdes */
106204917Sdesvoid
107204917Sdesraw_detach(struct rawcb *rp)
108204917Sdes{
109204917Sdes	struct socket *so = rp->rcb_socket;
110262566Sdes
111295367Sdes	KASSERT(so->so_pcb == rp, ("raw_detach: so_pcb != rp"));
112295367Sdes
113295367Sdes	so->so_pcb = NULL;
114295367Sdes	mtx_lock(&rawcb_mtx);
115126274Sdes	LIST_REMOVE(rp, list);
116126274Sdes	mtx_unlock(&rawcb_mtx);
117126274Sdes	free((caddr_t)(rp), M_PCB);
118126274Sdes}
119126274Sdes