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