1 /*	$OpenBSD: natm_pcb.c,v 1.5 2002/03/14 01:27:12 millert 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 
npcb_alloc(wait)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 
npcb_free(npcb,op)82 void npcb_free(npcb, op)
83 
84 struct natmpcb *npcb;
85 int op;
86 
87 {
88   int s = splimp();
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 
npcb_add(npcb,ifp,vci,vpi)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 = splimp();
121 
122 
123   /*
124    * lookup required
125    */
126 
127   for (cpcb = natm_pcbs.lh_first ; cpcb != NULL ;
128 					cpcb = cpcb->pcblist.le_next) {
129     if (ifp == cpcb->npcb_ifp && vci == cpcb->npcb_vci && vpi == cpcb->npcb_vpi)
130       break;
131   }
132 
133   /*
134    * add & something already there?
135    */
136 
137   if (cpcb) {
138     cpcb = NULL;
139     goto done;					/* fail */
140   }
141 
142   /*
143    * need to allocate a pcb?
144    */
145 
146   if (npcb == NULL) {
147     cpcb = npcb_alloc(M_NOWAIT);	/* could be called from lower half */
148     if (cpcb == NULL)
149       goto done;			/* fail */
150   } else {
151     cpcb = npcb;
152   }
153 
154   cpcb->npcb_ifp = ifp;
155   cpcb->ipaddr.s_addr = 0;
156   cpcb->npcb_vci = vci;
157   cpcb->npcb_vpi = vpi;
158   cpcb->npcb_flags = NPCB_CONNECTED;
159 
160   LIST_INSERT_HEAD(&natm_pcbs, cpcb, pcblist);
161 
162 done:
163   splx(s);
164   return(cpcb);
165 }
166 
167 
168 
169 #ifdef DDB
170 
171 int npcb_dump(void);
172 
npcb_dump()173 int npcb_dump()
174 
175 {
176   struct natmpcb *cpcb;
177 
178   printf("npcb dump:\n");
179   for (cpcb = natm_pcbs.lh_first ; cpcb != NULL ;
180 					cpcb = cpcb->pcblist.le_next) {
181     printf("if=%s, vci=%d, vpi=%d, IP=0x%x, sock=%p, flags=0x%x, inq=%d\n",
182 	cpcb->npcb_ifp->if_xname, cpcb->npcb_vci, cpcb->npcb_vpi,
183 	cpcb->ipaddr.s_addr, cpcb->npcb_socket,
184 	cpcb->npcb_flags, cpcb->npcb_inq);
185   }
186   printf("done\n");
187   return(0);
188 }
189 
190 #endif
191