raw_cb.c revision df2be82cecfdcfe4fe66cafe9b35f2eb7121b532
1/*-
2 * Copyright (c) 1980, 1986, 1993
3 *	The Regents of the University of California.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 4. Neither the name of the University nor the names of its contributors
15 *    may be used to endorse or promote products derived from this software
16 *    without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 *
30 *	@(#)raw_cb.c	8.1 (Berkeley) 6/10/93
31 * $FreeBSD$
32 */
33
34#include "opt_route.h"
35
36#include <sys/param.h>
37#include <sys/domain.h>
38#include <sys/lock.h>
39#include <sys/kernel.h>
40#include <sys/malloc.h>
41#include <sys/mutex.h>
42#include <sys/protosw.h>
43#include <sys/socket.h>
44#include <sys/socketvar.h>
45#include <sys/sysctl.h>
46#include <sys/systm.h>
47#include <sys/vimage.h>
48
49#include <net/if.h>
50#include <net/raw_cb.h>
51#include <net/route.h>
52#include <net/vnet.h>
53
54/*
55 * Routines to manage the raw protocol control blocks.
56 *
57 * TODO:
58 *	hash lookups by protocol family/protocol + address family
59 *	take care of unique address problems per AF?
60 *	redo address binding to allow wildcards
61 */
62
63struct mtx rawcb_mtx;
64#ifdef VIMAGE_GLOBALS
65struct rawcb_list_head rawcb_list;
66#endif
67
68SYSCTL_NODE(_net, OID_AUTO, raw, CTLFLAG_RW, 0, "Raw socket infrastructure");
69
70static u_long	raw_sendspace = RAWSNDQ;
71SYSCTL_ULONG(_net_raw, OID_AUTO, sendspace, CTLFLAG_RW, &raw_sendspace, 0,
72    "Default raw socket send space");
73
74static u_long	raw_recvspace = RAWRCVQ;
75SYSCTL_ULONG(_net_raw, OID_AUTO, recvspace, CTLFLAG_RW, &raw_recvspace, 0,
76    "Default raw socket receive space");
77
78/*
79 * Allocate a control block and a nominal amount of buffer space for the
80 * socket.
81 */
82int
83raw_attach(struct socket *so, int proto)
84{
85	INIT_VNET_NET(so->so_vnet);
86	struct rawcb *rp = sotorawcb(so);
87	int error;
88
89	/*
90	 * It is assumed that raw_attach is called after space has been
91	 * allocated for the rawcb; consumer protocols may simply allocate
92	 * type struct rawcb, or a wrapper data structure that begins with a
93	 * struct rawcb.
94	 */
95	KASSERT(rp != NULL, ("raw_attach: rp == NULL"));
96
97	error = soreserve(so, raw_sendspace, raw_recvspace);
98	if (error)
99		return (error);
100	rp->rcb_socket = so;
101	rp->rcb_proto.sp_family = so->so_proto->pr_domain->dom_family;
102	rp->rcb_proto.sp_protocol = proto;
103	mtx_lock(&rawcb_mtx);
104	LIST_INSERT_HEAD(&V_rawcb_list, rp, list);
105	mtx_unlock(&rawcb_mtx);
106	return (0);
107}
108
109/*
110 * Detach the raw connection block and discard socket resources.
111 */
112void
113raw_detach(struct rawcb *rp)
114{
115	struct socket *so = rp->rcb_socket;
116
117	KASSERT(so->so_pcb == rp, ("raw_detach: so_pcb != rp"));
118
119	so->so_pcb = NULL;
120	mtx_lock(&rawcb_mtx);
121	LIST_REMOVE(rp, list);
122	mtx_unlock(&rawcb_mtx);
123	free((caddr_t)(rp), M_PCB);
124}
125