BSD 4_4_Lite2 development
[unix-history] / .ref-BSD-4_4_Lite1 / usr / src / sys / netiso / tuba_table.c
CommitLineData
1067fd0f 1/*
ad787160
C
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
1067fd0f 4 *
ad787160
C
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
1067fd0f 20 *
ad787160
C
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
ed554bc5 33 * @(#)tuba_table.c 8.2 (Berkeley) 11/15/93
1067fd0f 34 */
21715d8e
KS
35#include <sys/param.h>
36#include <sys/systm.h>
37#include <sys/proc.h>
38#include <sys/mbuf.h>
39#include <sys/socket.h>
40#include <sys/socketvar.h>
41#include <sys/domain.h>
42#include <sys/protosw.h>
43#include <sys/ioctl.h>
44#include <sys/time.h>
45#include <sys/kernel.h>
1067fd0f 46
21715d8e 47#include <net/if.h>
21715d8e 48#include <net/radix.h>
1067fd0f 49
21715d8e 50#include <netiso/iso.h>
77db2796 51#include <netiso/tuba_table.h>
1067fd0f
KS
52
53int tuba_table_size;
54struct tuba_cache **tuba_table;
55struct radix_node_head *tuba_tree;
56extern int arpt_keep, arpt_prune; /* use same values as arp cache */
57
58void
59tuba_timer()
60{
61 int s = splnet();
62 int i;
21715d8e 63 register struct tuba_cache *tc;
1067fd0f
KS
64 long timelimit = time.tv_sec - arpt_keep;
65
66 timeout(tuba_timer, (caddr_t)0, arpt_prune * hz);
67 for (i = tuba_table_size; i > 0; i--)
68 if ((tc = tuba_table[i]) && (tc->tc_refcnt == 0) &&
69 (tc->tc_time < timelimit)) {
70 tuba_table[i] = 0;
d32cd81f 71 rn_delete(&tc->tc_siso.siso_addr, NULL, tuba_tree);
1067fd0f
KS
72 free((caddr_t)tc, M_RTABLE);
73 }
74 splx(s);
75}
76
21715d8e 77tuba_table_init()
1067fd0f 78{
0ef321d4 79 rn_inithead((void **)&tuba_tree, 40);
1067fd0f
KS
80 timeout(tuba_timer, (caddr_t)0, arpt_prune * hz);
81}
82
83int
7cfe65b5
KS
84tuba_lookup(siso, wait)
85 register struct sockaddr_iso *siso;
1067fd0f 86{
21715d8e 87 struct radix_node *rn, *rn_match();
1067fd0f 88 register struct tuba_cache *tc;
21715d8e 89 struct tuba_cache **new;
77db2796 90 int dupentry = 0, sum_a = 0, sum_b = 0, old_size, i;
1067fd0f 91
7cfe65b5
KS
92 if ((rn = rn_match((caddr_t)&siso->siso_addr, tuba_tree->rnh_treetop))
93 && ((rn->rn_flags & RNF_ROOT) == 0)) {
1067fd0f
KS
94 tc = (struct tuba_cache *)rn;
95 tc->tc_time = time.tv_sec;
96 return (tc->tc_index);
97 }
98 if ((tc = (struct tuba_cache *)malloc(sizeof(*tc), M_RTABLE, wait))
99 == NULL)
100 return (0);
21715d8e 101 bzero((caddr_t)tc, sizeof (*tc));
7cfe65b5
KS
102 bcopy(siso->siso_data, tc->tc_siso.siso_data,
103 tc->tc_siso.siso_nlen = siso->siso_nlen);
d32cd81f 104 rn_insert(&tc->tc_siso.siso_addr, tuba_tree, &dupentry, tc->tc_nodes);
8c53a772
KS
105 if (dupentry)
106 panic("tuba_lookup 1");
ff38aee7
KS
107 tc->tc_siso.siso_family = AF_ISO;
108 tc->tc_siso.siso_len = sizeof(tc->tc_siso);
1067fd0f 109 tc->tc_time = time.tv_sec;
7cfe65b5 110 for (i = sum_a = tc->tc_siso.siso_nlen; --i >= 0; )
1770ae00 111 (i & 1 ? sum_a : sum_b) += (u_char)tc->tc_siso.siso_data[i];
8c53a772
KS
112 REDUCE(tc->tc_sum, (sum_a << 8) + sum_b);
113 HTONS(tc->tc_sum);
315cfecc 114 SWAB(tc->tc_ssum, tc->tc_sum);
1067fd0f
KS
115 for (i = tuba_table_size; i > 0; i--)
116 if (tuba_table[i] == 0)
8c53a772 117 goto fixup;
77db2796 118 old_size = tuba_table_size;
1067fd0f
KS
119 if (tuba_table_size == 0)
120 tuba_table_size = 15;
121 if (tuba_table_size > 0x7fff)
122 return (0);
123 tuba_table_size = 1 + 2 * tuba_table_size;
124 i = (tuba_table_size + 1) * sizeof(tc);
125 new = (struct tuba_cache **)malloc((unsigned)i, M_RTABLE, wait);
126 if (new == 0) {
127 tuba_table_size = old_size;
d32cd81f 128 rn_delete(&tc->tc_siso.siso_addr, NULL, tuba_tree);
1067fd0f
KS
129 free((caddr_t)tc, M_RTABLE);
130 return (0);
131 }
132 bzero((caddr_t)new, (unsigned)i);
77db2796 133 if (tuba_table) {
1067fd0f 134 bcopy((caddr_t)tuba_table, (caddr_t)new, i >> 1);
77db2796
KS
135 free((caddr_t)tuba_table, M_RTABLE);
136 }
137 tuba_table = new;
8c53a772
KS
138 i = tuba_table_size;
139fixup:
140 tuba_table[i] = tc;
141 tc->tc_index = i;
1067fd0f
KS
142 return (tc->tc_index);
143}