]> git.saurik.com Git - apple/xnu.git/blob - bsd/netiso/tuba_table.c
3d8a005c2651a7af57614d2a98f8923dd7f2a5f8
[apple/xnu.git] / bsd / netiso / tuba_table.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /*
23 * Copyright (c) 1992, 1993
24 * The Regents of the University of California. All rights reserved.
25 *
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
28 * are met:
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. All advertising materials mentioning features or use of this software
35 * must display the following acknowledgement:
36 * This product includes software developed by the University of
37 * California, Berkeley and its contributors.
38 * 4. Neither the name of the University nor the names of its contributors
39 * may be used to endorse or promote products derived from this software
40 * without specific prior written permission.
41 *
42 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * SUCH DAMAGE.
53 *
54 * @(#)tuba_table.c 8.2 (Berkeley) 11/15/93
55 */
56 #include <sys/param.h>
57 #include <sys/systm.h>
58 #include <sys/proc.h>
59 #include <sys/mbuf.h>
60 #include <sys/socket.h>
61 #include <sys/socketvar.h>
62 #include <sys/domain.h>
63 #include <sys/protosw.h>
64 #include <sys/ioctl.h>
65 #include <sys/time.h>
66 #include <sys/kernel.h>
67
68 #include <net/if.h>
69 #include <net/radix.h>
70
71 #include <netiso/iso.h>
72 #include <netiso/tuba_table.h>
73
74 int tuba_table_size;
75 struct tuba_cache **tuba_table;
76 struct radix_node_head *tuba_tree;
77 extern int arpt_keep, arpt_prune; /* use same values as arp cache */
78
79 void
80 tuba_timer()
81 {
82 int s = splnet();
83 int i;
84 register struct tuba_cache *tc;
85 long timelimit = time.tv_sec - arpt_keep;
86
87 timeout(tuba_timer, (caddr_t)0, arpt_prune * hz);
88 for (i = tuba_table_size; i > 0; i--)
89 if ((tc = tuba_table[i]) && (tc->tc_refcnt == 0) &&
90 (tc->tc_time < timelimit)) {
91 tuba_table[i] = 0;
92 rn_delete(&tc->tc_siso.siso_addr, NULL, tuba_tree);
93 FREE((caddr_t)tc, M_RTABLE);
94 }
95 splx(s);
96 }
97
98 tuba_table_init()
99 {
100 rn_inithead((void **)&tuba_tree, 40);
101 timeout(tuba_timer, (caddr_t)0, arpt_prune * hz);
102 }
103
104 int
105 tuba_lookup(siso, wait)
106 register struct sockaddr_iso *siso;
107 {
108 struct radix_node *rn, *rn_match();
109 register struct tuba_cache *tc;
110 struct tuba_cache **new;
111 int dupentry = 0, sum_a = 0, sum_b = 0, old_size, i;
112
113 if ((rn = rn_match((caddr_t)&siso->siso_addr, tuba_tree->rnh_treetop))
114 && ((rn->rn_flags & RNF_ROOT) == 0)) {
115 tc = (struct tuba_cache *)rn;
116 tc->tc_time = time.tv_sec;
117 return (tc->tc_index);
118 }
119 // if ((tc = (struct tuba_cache *)malloc(sizeof(*tc), M_RTABLE, wait))
120 // == NULL)
121 if (wait == M_DONTWAIT)
122 MALLOC(tc, struct tuba_cache *, sizeof(*tc), M_RTABLE, M_NOWAIT);
123 else
124 MALLOC(tc, struct tuba_cache *, sizeof(*tc), M_RTABLE, M_WAITOK);
125 if (tc == NULL)
126 return (0);
127 bzero((caddr_t)tc, sizeof (*tc));
128 bcopy(siso->siso_data, tc->tc_siso.siso_data,
129 tc->tc_siso.siso_nlen = siso->siso_nlen);
130 rn_insert(&tc->tc_siso.siso_addr, tuba_tree, &dupentry, tc->tc_nodes);
131 if (dupentry)
132 panic("tuba_lookup 1");
133 tc->tc_siso.siso_family = AF_ISO;
134 tc->tc_siso.siso_len = sizeof(tc->tc_siso);
135 tc->tc_time = time.tv_sec;
136 for (i = sum_a = tc->tc_siso.siso_nlen; --i >= 0; )
137 (i & 1 ? sum_a : sum_b) += (u_char)tc->tc_siso.siso_data[i];
138 REDUCE(tc->tc_sum, (sum_a << 8) + sum_b);
139 HTONS(tc->tc_sum);
140 SWAB(tc->tc_ssum, tc->tc_sum);
141 for (i = tuba_table_size; i > 0; i--)
142 if (tuba_table[i] == 0)
143 goto fixup;
144 old_size = tuba_table_size;
145 if (tuba_table_size == 0)
146 tuba_table_size = 15;
147 if (tuba_table_size > 0x7fff)
148 return (0);
149 tuba_table_size = 1 + 2 * tuba_table_size;
150 i = (tuba_table_size + 1) * sizeof(tc);
151 // new = (struct tuba_cache **)malloc((unsigned)i, M_RTABLE, wait);
152 if (wait == M_DONTWAIT)
153 MALLOC(new, struct tuba_cache **, i, M_RTABLE, M_NOWAIT);
154 else
155 MALLOC(new, struct tuba_cache **, i, M_RTABLE, M_WAITOK);
156 if (new == 0) {
157 tuba_table_size = old_size;
158 rn_delete(&tc->tc_siso.siso_addr, NULL, tuba_tree);
159 FREE((caddr_t)tc, M_RTABLE);
160 return (0);
161 }
162 bzero((caddr_t)new, (unsigned)i);
163 if (tuba_table) {
164 bcopy((caddr_t)tuba_table, (caddr_t)new, i >> 1);
165 FREE((caddr_t)tuba_table, M_RTABLE);
166 }
167 tuba_table = new;
168 i = tuba_table_size;
169 fixup:
170 tuba_table[i] = tc;
171 tc->tc_index = i;
172 return (tc->tc_index);
173 }