raw_cb.c revision 195699
1193326Sed/*-
2193326Sed * Copyright (c) 1980, 1986, 1993
3193326Sed *	The Regents of the University of California.
4193326Sed * All rights reserved.
5193326Sed *
6193326Sed * Redistribution and use in source and binary forms, with or without
7193326Sed * modification, are permitted provided that the following conditions
8193326Sed * are met:
9193326Sed * 1. Redistributions of source code must retain the above copyright
10193326Sed *    notice, this list of conditions and the following disclaimer.
11193326Sed * 2. Redistributions in binary form must reproduce the above copyright
12193326Sed *    notice, this list of conditions and the following disclaimer in the
13193326Sed *    documentation and/or other materials provided with the distribution.
14193326Sed * 4. Neither the name of the University nor the names of its contributors
15193326Sed *    may be used to endorse or promote products derived from this software
16193326Sed *    without specific prior written permission.
17193326Sed *
18193326Sed * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19193326Sed * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20193326Sed * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21195341Sed * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22193326Sed * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23193326Sed * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24193326Sed * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25193326Sed * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26193326Sed * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27193326Sed * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28193326Sed * SUCH DAMAGE.
29198092Srdivacky *
30193326Sed *	@(#)raw_cb.c	8.1 (Berkeley) 6/10/93
31193326Sed * $FreeBSD: head/sys/net/raw_cb.c 195699 2009-07-14 22:48:30Z rwatson $
32193326Sed */
33198092Srdivacky
34193326Sed#include <sys/param.h>
35198092Srdivacky#include <sys/domain.h>
36193326Sed#include <sys/lock.h>
37193326Sed#include <sys/kernel.h>
38195341Sed#include <sys/malloc.h>
39195341Sed#include <sys/mutex.h>
40193326Sed#include <sys/protosw.h>
41193326Sed#include <sys/socket.h>
42193326Sed#include <sys/socketvar.h>
43#include <sys/sysctl.h>
44#include <sys/systm.h>
45#include <sys/vimage.h>
46
47#include <net/if.h>
48#include <net/raw_cb.h>
49#include <net/vnet.h>
50
51/*
52 * Routines to manage the raw protocol control blocks.
53 *
54 * TODO:
55 *	hash lookups by protocol family/protocol + address family
56 *	take care of unique address problems per AF?
57 *	redo address binding to allow wildcards
58 */
59
60struct mtx rawcb_mtx;
61VNET_DEFINE(struct rawcb_list_head, rawcb_list);
62
63SYSCTL_NODE(_net, OID_AUTO, raw, CTLFLAG_RW, 0, "Raw socket infrastructure");
64
65static u_long	raw_sendspace = RAWSNDQ;
66SYSCTL_ULONG(_net_raw, OID_AUTO, sendspace, CTLFLAG_RW, &raw_sendspace, 0,
67    "Default raw socket send space");
68
69static u_long	raw_recvspace = RAWRCVQ;
70SYSCTL_ULONG(_net_raw, OID_AUTO, recvspace, CTLFLAG_RW, &raw_recvspace, 0,
71    "Default raw socket receive space");
72
73/*
74 * Allocate a control block and a nominal amount of buffer space for the
75 * socket.
76 */
77int
78raw_attach(struct socket *so, int proto)
79{
80	struct rawcb *rp = sotorawcb(so);
81	int error;
82
83	/*
84	 * It is assumed that raw_attach is called after space has been
85	 * allocated for the rawcb; consumer protocols may simply allocate
86	 * type struct rawcb, or a wrapper data structure that begins with a
87	 * struct rawcb.
88	 */
89	KASSERT(rp != NULL, ("raw_attach: rp == NULL"));
90
91	error = soreserve(so, raw_sendspace, raw_recvspace);
92	if (error)
93		return (error);
94	rp->rcb_socket = so;
95	rp->rcb_proto.sp_family = so->so_proto->pr_domain->dom_family;
96	rp->rcb_proto.sp_protocol = proto;
97	mtx_lock(&rawcb_mtx);
98	LIST_INSERT_HEAD(&V_rawcb_list, rp, list);
99	mtx_unlock(&rawcb_mtx);
100	return (0);
101}
102
103/*
104 * Detach the raw connection block and discard socket resources.
105 */
106void
107raw_detach(struct rawcb *rp)
108{
109	struct socket *so = rp->rcb_socket;
110
111	KASSERT(so->so_pcb == rp, ("raw_detach: so_pcb != rp"));
112
113	so->so_pcb = NULL;
114	mtx_lock(&rawcb_mtx);
115	LIST_REMOVE(rp, list);
116	mtx_unlock(&rawcb_mtx);
117	free((caddr_t)(rp), M_PCB);
118}
119