natm_pcb.c revision 148125
1/*	$NetBSD: natm_pcb.c,v 1.4 1996/11/09 03:26:27 chuck Exp $	*/
2/*-
3 *
4 * Copyright (c) 1996 Charles D. Cranor and Washington University.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 *    must display the following acknowledgement:
17 *      This product includes software developed by Charles D. Cranor and
18 *      Washington University.
19 * 4. The name of the author may not be used to endorse or promote products
20 *    derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34/*
35 * atm_pcb.c: manage atm protocol control blocks and keep IP and NATM
36 * from trying to use each other's VCs.
37 */
38
39#include <sys/cdefs.h>
40__FBSDID("$FreeBSD: head/sys/netnatm/natm_pcb.c 148125 2005-07-18 16:55:46Z rwatson $");
41
42#include <sys/param.h>
43#include <sys/systm.h>
44#include <sys/malloc.h>
45#include <sys/socket.h>
46#include <sys/socketvar.h>
47
48#include <net/if.h>
49
50#include <netinet/in.h>
51
52#include <netnatm/natm.h>
53
54struct npcblist natm_pcbs;
55
56/*
57 * npcb_alloc: allocate a npcb [in the free state]
58 */
59struct natmpcb *
60npcb_alloc(int wait)
61
62{
63	struct natmpcb *npcb;
64
65	npcb = malloc(sizeof(*npcb), M_PCB, wait | M_ZERO);
66
67#ifdef DIAGNOSTIC
68	if (wait == M_WAITOK && npcb == NULL)
69		panic("npcb_alloc: malloc didn't wait");
70#endif
71
72	if (npcb != NULL)
73		npcb->npcb_flags = NPCB_FREE;
74	return (npcb);
75}
76
77
78/*
79 * npcb_free: free a npcb
80 */
81void
82npcb_free(struct natmpcb *npcb, int op)
83{
84
85	NATM_LOCK_ASSERT();
86
87	if ((npcb->npcb_flags & NPCB_FREE) == 0) {
88		LIST_REMOVE(npcb, pcblist);
89		npcb->npcb_flags = NPCB_FREE;
90	}
91	if (op == NPCB_DESTROY) {
92		if (npcb->npcb_inq) {
93			npcb->npcb_flags = NPCB_DRAIN;	/* flag for distruct. */
94		} else {
95			FREE(npcb, M_PCB);		/* kill it! */
96		}
97	}
98}
99
100
101/*
102 * npcb_add: add or remove npcb from main list
103 *   returns npcb if ok
104 */
105struct natmpcb *
106npcb_add(struct natmpcb *npcb, struct ifnet *ifp, u_int16_t vci, u_int8_t vpi)
107{
108	struct natmpcb *cpcb = NULL;		/* current pcb */
109
110	NATM_LOCK_ASSERT();
111
112	/*
113	 * lookup required
114	 */
115	LIST_FOREACH(cpcb, &natm_pcbs, pcblist)
116		if (ifp == cpcb->npcb_ifp && vci == cpcb->npcb_vci &&
117		    vpi == cpcb->npcb_vpi)
118			break;
119
120	/*
121	 * add & something already there?
122	 */
123	if (cpcb) {
124		cpcb = NULL;
125		goto done;			/* fail */
126	}
127
128	/*
129	 * need to allocate a pcb?
130	 */
131	if (npcb == NULL) {
132		/* could be called from lower half */
133		cpcb = npcb_alloc(M_NOWAIT);
134		if (cpcb == NULL)
135			goto done;			/* fail */
136	} else {
137		cpcb = npcb;
138	}
139
140	cpcb->npcb_ifp = ifp;
141	cpcb->ipaddr.s_addr = 0;
142	cpcb->npcb_vci = vci;
143	cpcb->npcb_vpi = vpi;
144	cpcb->npcb_flags = NPCB_CONNECTED;
145
146	LIST_INSERT_HEAD(&natm_pcbs, cpcb, pcblist);
147
148done:
149	return (cpcb);
150}
151
152#ifdef DDB
153
154int
155npcb_dump(void)
156{
157	struct natmpcb *cpcb;
158
159	printf("npcb dump:\n");
160	LIST_FOREACH(cpcb, &natm_pcbs, pcblist) {
161		printf("if=%s, vci=%d, vpi=%d, IP=0x%x, sock=%p, flags=0x%x, "
162		    "inq=%d\n", cpcb->npcb_ifp->if_xname, cpcb->npcb_vci,
163		    cpcb->npcb_vpi, cpcb->ipaddr.s_addr, cpcb->npcb_socket,
164		    cpcb->npcb_flags, cpcb->npcb_inq);
165	}
166	printf("done\n");
167	return (0);
168}
169#endif
170