b16f38af29fa79d2d7203eb26bff4cabb411b0f8
[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.9 2008/01/05 14:02:40 swildner 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 #include <sys/thread2.h>
48
49 #include <net/if.h>
50
51 #include <netinet/in.h>
52
53 #include "natm.h"
54
55 struct npcblist natm_pcbs;
56
57 /*
58  * npcb_alloc: allocate a npcb [in the free state]
59  */
60
61 struct natmpcb *
62 npcb_alloc(int wait)
63 {
64   struct natmpcb *npcb;
65
66   MALLOC(npcb, struct natmpcb *, sizeof(*npcb), M_PCB, wait | M_ZERO);
67
68 #ifdef DIAGNOSTIC
69   if (wait == M_WAITOK && npcb == NULL) panic("npcb_alloc: MALLOC didn't wait");
70 #endif
71
72   if (npcb)
73     npcb->npcb_flags = NPCB_FREE;
74   return(npcb);
75 }
76
77
78 /*
79  * npcb_free: free a npcb
80  */
81
82 void
83 npcb_free(struct natmpcb *npcb, int op)
84 {
85   crit_enter();
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 distruction */
94     } else {
95       FREE(npcb, M_PCB);                /* kill it! */
96     }
97   }
98
99   crit_exit();
100 }
101
102
103 /*
104  * npcb_add: add or remove npcb from main list
105  *   returns npcb if ok
106  */
107
108 struct natmpcb *
109 npcb_add(struct natmpcb *npcb, struct ifnet *ifp, int vci, int vpi)
110 {
111   struct natmpcb *cpcb = NULL;          /* current pcb */
112
113   crit_enter();
114   /*
115    * lookup required
116    */
117
118   for (cpcb = natm_pcbs.lh_first ; cpcb != NULL ; 
119                                         cpcb = cpcb->pcblist.le_next) {
120     if (ifp == cpcb->npcb_ifp && vci == cpcb->npcb_vci && vpi == cpcb->npcb_vpi)
121       break;
122   }
123
124   /*
125    * add & something already there?
126    */
127
128   if (cpcb) {
129     cpcb = NULL;
130     goto done;                                  /* fail */
131   }
132     
133   /*
134    * need to allocate a pcb?
135    */
136
137   if (npcb == NULL) {
138     cpcb = npcb_alloc(M_NOWAIT);        /* could be called from lower half */
139     if (cpcb == NULL) 
140       goto done;                        /* fail */
141   } else {
142     cpcb = npcb;
143   }
144
145   cpcb->npcb_ifp = ifp;
146   cpcb->ipaddr.s_addr = 0;
147   cpcb->npcb_vci = vci;
148   cpcb->npcb_vpi = vpi;
149   cpcb->npcb_flags = NPCB_CONNECTED;
150
151   LIST_INSERT_HEAD(&natm_pcbs, cpcb, pcblist);
152
153 done:
154   crit_exit();
155   return(cpcb);
156 }
157
158
159
160 #ifdef DDB
161
162 int npcb_dump(void);
163
164 int
165 npcb_dump(void)
166 {
167   struct natmpcb *cpcb;
168
169   kprintf("npcb dump:\n");
170   for (cpcb = natm_pcbs.lh_first ; cpcb != NULL ; 
171                                         cpcb = cpcb->pcblist.le_next) {
172     kprintf("if=%s, vci=%d, vpi=%d, IP=0x%x, sock=%p, flags=0x%x, inq=%d\n",
173         cpcb->npcb_ifp->if_xname, cpcb->npcb_vci, cpcb->npcb_vpi,
174         cpcb->ipaddr.s_addr, cpcb->npcb_socket, 
175         cpcb->npcb_flags, cpcb->npcb_inq);
176   }
177   kprintf("done\n");
178   return(0);
179 }
180
181 #endif