]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netnatm/natm_pcb.c
This commit was generated by cvs2svn to compensate for changes in r48743,
[FreeBSD/FreeBSD.git] / sys / netnatm / natm_pcb.c
1 /*      $NetBSD: natm_pcb.c,v 1.4 1996/11/09 03:26:27 chuck 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/malloc.h>
43 #include <sys/socket.h>
44 #include <sys/socketvar.h>
45
46 #include <net/if.h>
47
48 #include <netinet/in.h>
49
50 #include <netnatm/natm.h>
51
52 struct npcblist natm_pcbs;
53
54 /*
55  * npcb_alloc: allocate a npcb [in the free state]
56  */
57
58 struct natmpcb *npcb_alloc(wait)
59
60 int wait;
61
62 {
63   struct natmpcb *npcb;
64
65   MALLOC(npcb, struct natmpcb *, sizeof(*npcb), M_PCB, wait);
66
67 #ifdef DIAGNOSTIC
68   if (wait == M_WAITOK && npcb == NULL) panic("npcb_alloc: malloc didn't wait");
69 #endif
70
71   if (npcb) {
72     bzero(npcb, sizeof(*npcb));
73     npcb->npcb_flags = NPCB_FREE;
74   }
75   return(npcb);
76 }
77
78
79 /*
80  * npcb_free: free a npcb
81  */
82
83 void npcb_free(npcb, op)
84
85 struct natmpcb *npcb;
86 int op;
87
88 {
89   int s = splimp();
90
91   if ((npcb->npcb_flags & NPCB_FREE) == 0) {
92     LIST_REMOVE(npcb, pcblist);
93     npcb->npcb_flags = NPCB_FREE;
94   }
95   if (op == NPCB_DESTROY) {
96     if (npcb->npcb_inq) {
97       npcb->npcb_flags = NPCB_DRAIN;    /* flag for distruction */
98     } else {
99       FREE(npcb, M_PCB);                /* kill it! */
100     }
101   }
102
103   splx(s);
104 }
105
106
107 /*
108  * npcb_add: add or remove npcb from main list
109  *   returns npcb if ok
110  */
111
112 struct natmpcb *npcb_add(npcb, ifp, vci, vpi)
113
114 struct natmpcb *npcb;
115 struct ifnet *ifp;
116 u_int16_t vci;
117 u_int8_t vpi;
118
119 {
120   struct natmpcb *cpcb = NULL;          /* current pcb */
121   int s = splimp();
122
123
124   /*
125    * lookup required
126    */
127
128   for (cpcb = natm_pcbs.lh_first ; cpcb != NULL ; 
129                                         cpcb = cpcb->pcblist.le_next) {
130     if (ifp == cpcb->npcb_ifp && vci == cpcb->npcb_vci && vpi == cpcb->npcb_vpi)
131       break;
132   }
133
134   /*
135    * add & something already there?
136    */
137
138   if (cpcb) {
139     cpcb = NULL;
140     goto done;                                  /* fail */
141   }
142     
143   /*
144    * need to allocate a pcb?
145    */
146
147   if (npcb == NULL) {
148     cpcb = npcb_alloc(M_NOWAIT);        /* could be called from lower half */
149     if (cpcb == NULL) 
150       goto done;                        /* fail */
151   } else {
152     cpcb = npcb;
153   }
154
155   cpcb->npcb_ifp = ifp;
156   cpcb->ipaddr.s_addr = 0;
157   cpcb->npcb_vci = vci;
158   cpcb->npcb_vpi = vpi;
159   cpcb->npcb_flags = NPCB_CONNECTED;
160
161   LIST_INSERT_HEAD(&natm_pcbs, cpcb, pcblist);
162
163 done:
164   splx(s);
165   return(cpcb);
166 }
167
168
169
170 #ifdef DDB
171
172 int npcb_dump __P((void));
173
174 int npcb_dump()
175
176 {
177   struct natmpcb *cpcb;
178
179   printf("npcb dump:\n");
180   for (cpcb = natm_pcbs.lh_first ; cpcb != NULL ; 
181                                         cpcb = cpcb->pcblist.le_next) {
182     printf("if=%s, vci=%d, vpi=%d, IP=0x%x, sock=%p, flags=0x%x, inq=%d\n",
183         cpcb->npcb_ifp->if_xname, cpcb->npcb_vci, cpcb->npcb_vpi,
184         cpcb->ipaddr.s_addr, cpcb->npcb_socket, 
185         cpcb->npcb_flags, cpcb->npcb_inq);
186   }
187   printf("done\n");
188   return(0);
189 }
190
191 #endif