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