raw_cb.c revision 195699
1139826Simp/*-
253541Sshin * Copyright (c) 1980, 1986, 1993
353541Sshin *	The Regents of the University of California.
453541Sshin * All rights reserved.
553541Sshin *
653541Sshin * Redistribution and use in source and binary forms, with or without
753541Sshin * modification, are permitted provided that the following conditions
853541Sshin * are met:
953541Sshin * 1. Redistributions of source code must retain the above copyright
1053541Sshin *    notice, this list of conditions and the following disclaimer.
1153541Sshin * 2. Redistributions in binary form must reproduce the above copyright
1253541Sshin *    notice, this list of conditions and the following disclaimer in the
1353541Sshin *    documentation and/or other materials provided with the distribution.
1453541Sshin * 4. Neither the name of the University nor the names of its contributors
1553541Sshin *    may be used to endorse or promote products derived from this software
1653541Sshin *    without specific prior written permission.
1753541Sshin *
1853541Sshin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1953541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2053541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2153541Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2253541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2353541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2453541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2553541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2653541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2753541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2853541Sshin * SUCH DAMAGE.
2953541Sshin *
3053541Sshin *	@(#)raw_cb.c	8.1 (Berkeley) 6/10/93
3153541Sshin * $FreeBSD: head/sys/net/raw_cb.c 195699 2009-07-14 22:48:30Z rwatson $
32139826Simp */
3353541Sshin
3453541Sshin#include <sys/param.h>
3553541Sshin#include <sys/domain.h>
3653541Sshin#include <sys/lock.h>
3753541Sshin#include <sys/kernel.h>
3853541Sshin#include <sys/malloc.h>
3953541Sshin#include <sys/mutex.h>
4053541Sshin#include <sys/protosw.h>
4153541Sshin#include <sys/socket.h>
4253541Sshin#include <sys/socketvar.h>
4353541Sshin#include <sys/sysctl.h>
4453541Sshin#include <sys/systm.h>
4553541Sshin#include <sys/vimage.h>
4653541Sshin
4753541Sshin#include <net/if.h>
4853541Sshin#include <net/raw_cb.h>
4953541Sshin#include <net/vnet.h>
5053541Sshin
5153541Sshin/*
5253541Sshin * Routines to manage the raw protocol control blocks.
5353541Sshin *
5453541Sshin * TODO:
5553541Sshin *	hash lookups by protocol family/protocol + address family
5653541Sshin *	take care of unique address problems per AF?
5753541Sshin *	redo address binding to allow wildcards
5853541Sshin */
5953541Sshin
6053541Sshinstruct mtx rawcb_mtx;
6153541SshinVNET_DEFINE(struct rawcb_list_head, rawcb_list);
6253541Sshin
6355009SshinSYSCTL_NODE(_net, OID_AUTO, raw, CTLFLAG_RW, 0, "Raw socket infrastructure");
6478064Sume
6555009Sshinstatic u_long	raw_sendspace = RAWSNDQ;
6653541SshinSYSCTL_ULONG(_net_raw, OID_AUTO, sendspace, CTLFLAG_RW, &raw_sendspace, 0,
6795759Stanimura    "Default raw socket send space");
6895759Stanimura
6953541Sshinstatic u_long	raw_recvspace = RAWRCVQ;
7095759StanimuraSYSCTL_ULONG(_net_raw, OID_AUTO, recvspace, CTLFLAG_RW, &raw_recvspace, 0,
7153541Sshin    "Default raw socket receive space");
7295759Stanimura
7395759Stanimura/*
7453541Sshin * Allocate a control block and a nominal amount of buffer space for the
7553541Sshin * socket.
7695759Stanimura */
7753541Sshinint
78148385Sumeraw_attach(struct socket *so, int proto)
7953541Sshin{
8053541Sshin	struct rawcb *rp = sotorawcb(so);
8195759Stanimura	int error;
8253541Sshin
8353541Sshin	/*
8453541Sshin	 * It is assumed that raw_attach is called after space has been
8553541Sshin	 * allocated for the rawcb; consumer protocols may simply allocate
8653541Sshin	 * type struct rawcb, or a wrapper data structure that begins with a
8795759Stanimura	 * struct rawcb.
8895759Stanimura	 */
8962587Sitojun	KASSERT(rp != NULL, ("raw_attach: rp == NULL"));
9095759Stanimura
9156723Sshin	error = soreserve(so, raw_sendspace, raw_recvspace);
9253541Sshin	if (error)
9395759Stanimura		return (error);
9453541Sshin	rp->rcb_socket = so;
9595759Stanimura	rp->rcb_proto.sp_family = so->so_proto->pr_domain->dom_family;
9662587Sitojun	rp->rcb_proto.sp_protocol = proto;
9753541Sshin	mtx_lock(&rawcb_mtx);
9853541Sshin	LIST_INSERT_HEAD(&V_rawcb_list, rp, list);
9953541Sshin	mtx_unlock(&rawcb_mtx);
10053541Sshin	return (0);
10153541Sshin}
10253541Sshin
103105199Ssam/*
104105199Ssam * Detach the raw connection block and discard socket resources.
105105199Ssam */
106105199Ssamvoid
107105199Ssamraw_detach(struct rawcb *rp)
10853541Sshin{
10953541Sshin	struct socket *so = rp->rcb_socket;
11053541Sshin
11153541Sshin	KASSERT(so->so_pcb == rp, ("raw_detach: so_pcb != rp"));
11253541Sshin
11353541Sshin	so->so_pcb = NULL;
11453541Sshin	mtx_lock(&rawcb_mtx);
11553541Sshin	LIST_REMOVE(rp, list);
11653541Sshin	mtx_unlock(&rawcb_mtx);
11753541Sshin	free((caddr_t)(rp), M_PCB);
11853541Sshin}
11953541Sshin