natm_pcb.c revision 116189
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: head/sys/netnatm/natm_pcb.c 116189 2003-06-11 05:37:42Z obrien $"); 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 54struct npcblist natm_pcbs; 55 56/* 57 * npcb_alloc: allocate a npcb [in the free state] 58 */ 59 60struct natmpcb *npcb_alloc(wait) 61 62int wait; 63 64{ 65 struct natmpcb *npcb; 66 67 MALLOC(npcb, struct natmpcb *, sizeof(*npcb), M_PCB, wait | M_ZERO); 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 npcb->npcb_flags = NPCB_FREE; 75 76 return(npcb); 77} 78 79 80/* 81 * npcb_free: free a npcb 82 */ 83 84void npcb_free(npcb, op) 85 86struct natmpcb *npcb; 87int op; 88 89{ 90 int s = splimp(); 91 92 if ((npcb->npcb_flags & NPCB_FREE) == 0) { 93 LIST_REMOVE(npcb, pcblist); 94 npcb->npcb_flags = NPCB_FREE; 95 } 96 if (op == NPCB_DESTROY) { 97 if (npcb->npcb_inq) { 98 npcb->npcb_flags = NPCB_DRAIN; /* flag for distruction */ 99 } else { 100 FREE(npcb, M_PCB); /* kill it! */ 101 } 102 } 103 104 splx(s); 105} 106 107 108/* 109 * npcb_add: add or remove npcb from main list 110 * returns npcb if ok 111 */ 112 113struct natmpcb *npcb_add(npcb, ifp, vci, vpi) 114 115struct natmpcb *npcb; 116struct ifnet *ifp; 117u_int16_t vci; 118u_int8_t vpi; 119 120{ 121 struct natmpcb *cpcb = NULL; /* current pcb */ 122 int s = splimp(); 123 124 125 /* 126 * lookup required 127 */ 128 129 for (cpcb = LIST_FIRST(&natm_pcbs) ; cpcb != NULL ; 130 cpcb = LIST_NEXT(cpcb, pcblist)) { 131 if (ifp == cpcb->npcb_ifp && vci == cpcb->npcb_vci && vpi == cpcb->npcb_vpi) 132 break; 133 } 134 135 /* 136 * add & something already there? 137 */ 138 139 if (cpcb) { 140 cpcb = NULL; 141 goto done; /* fail */ 142 } 143 144 /* 145 * need to allocate a pcb? 146 */ 147 148 if (npcb == NULL) { 149 cpcb = npcb_alloc(M_NOWAIT); /* could be called from lower half */ 150 if (cpcb == NULL) 151 goto done; /* fail */ 152 } else { 153 cpcb = npcb; 154 } 155 156 cpcb->npcb_ifp = ifp; 157 cpcb->ipaddr.s_addr = 0; 158 cpcb->npcb_vci = vci; 159 cpcb->npcb_vpi = vpi; 160 cpcb->npcb_flags = NPCB_CONNECTED; 161 162 LIST_INSERT_HEAD(&natm_pcbs, cpcb, pcblist); 163 164done: 165 splx(s); 166 return(cpcb); 167} 168 169 170 171#ifdef DDB 172 173int npcb_dump(void); 174 175int npcb_dump() 176 177{ 178 struct natmpcb *cpcb; 179 180 printf("npcb dump:\n"); 181 for (cpcb = LIST_FIRST(&natm_pcbs) ; cpcb != NULL ; 182 cpcb = LIST_NEXT(cpcb, pcblist)) { 183 printf("if=%s, vci=%d, vpi=%d, IP=0x%x, sock=%p, flags=0x%x, inq=%d\n", 184 cpcb->npcb_ifp->if_xname, cpcb->npcb_vci, cpcb->npcb_vpi, 185 cpcb->ipaddr.s_addr, cpcb->npcb_socket, 186 cpcb->npcb_flags, cpcb->npcb_inq); 187 } 188 printf("done\n"); 189 return(0); 190} 191 192#endif 193