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