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