1 /* 2 * Copyright (c) 1992 Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 * 7 * @(#)tuba_table.c 7.3 (Berkeley) 10/15/92 8 */ 9 #include <sys/param.h> 10 #include <sys/systm.h> 11 #include <sys/proc.h> 12 #include <sys/mbuf.h> 13 #include <sys/socket.h> 14 #include <sys/socketvar.h> 15 #include <sys/domain.h> 16 #include <sys/protosw.h> 17 #include <sys/ioctl.h> 18 #include <sys/time.h> 19 #include <sys/kernel.h> 20 21 #include <net/if.h> 22 #include <net/af.h> 23 #include <net/radix.h> 24 25 #include <netiso/iso.h> 26 #include <netiso/tuba_addr.h> 27 28 int tuba_table_size; 29 struct tuba_cache **tuba_table; 30 struct radix_node_head *tuba_tree; 31 extern int arpt_keep, arpt_prune; /* use same values as arp cache */ 32 33 void 34 tuba_timer() 35 { 36 int s = splnet(); 37 int i; 38 register struct tuba_cache *tc; 39 long timelimit = time.tv_sec - arpt_keep; 40 41 timeout(tuba_timer, (caddr_t)0, arpt_prune * hz); 42 for (i = tuba_table_size; i > 0; i--) 43 if ((tc = tuba_table[i]) && (tc->tc_refcnt == 0) && 44 (tc->tc_time < timelimit)) { 45 tuba_table[i] = 0; 46 rn_delete((caddr_t)&tc->tc_addr, (caddr_t)0, tuba_tree); 47 free((caddr_t)tc, M_RTABLE); 48 } 49 splx(s); 50 } 51 52 tuba_table_init() 53 { 54 rn_inithead((void **)&tuba_tree, 40); 55 timeout(tuba_timer, (caddr_t)0, arpt_prune * hz); 56 } 57 58 int 59 tuba_lookup(isoa, flags, wait) 60 register struct iso_addr *isoa; 61 int flags; 62 { 63 struct radix_node *rn, *rn_match(); 64 register struct tuba_cache *tc; 65 struct tuba_cache **new; 66 int dupentry = 0, sum_even = 0, sum_odd = 0, old_size, i; 67 68 if (rn = rn_match(tuba_tree, (caddr_t)isoa)) { 69 tc = (struct tuba_cache *)rn; 70 tc->tc_time = time.tv_sec; 71 return (tc->tc_index); 72 } 73 if ((tc = (struct tuba_cache *)malloc(sizeof(*tc), M_RTABLE, wait)) 74 == NULL) 75 return (0); 76 bzero((caddr_t)tc, sizeof (*tc)); 77 bcopy((caddr_t)isoa, (caddr_t)&tc->tc_addr, 1 + isoa->isoa_len); 78 rn_insert((caddr_t)&tc->tc_addr, tuba_tree, &dupentry, tc->tc_nodes); 79 if (dupentry) 80 panic("tuba_lookup 1"); 81 tc->tc_time = time.tv_sec; 82 tc->tc_flags = flags; 83 for (i = isoa->isoa_len; --i >= 0; ) 84 (i & 1 ? sum_even : sum_odd) += isoa->isoa_genaddr[i]; 85 ICKSUM(tc->tc_sum_in, (sum_even << 8) + sum_odd); 86 ICKSUM(tc->tc_sum_out, tc->tc_sum_in + 0x1fffe - tc->tc_index); 87 for (i = tuba_table_size; i > 0; i--) 88 if (tuba_table[i] == 0) 89 break; 90 if (i) { 91 tc->tc_index = 1; 92 tuba_table[i] = tc; 93 return (i); 94 } 95 if (tuba_table_size == 0) 96 tuba_table_size = 15; 97 if (tuba_table_size > 0x7fff) 98 return (0); 99 tuba_table_size = 1 + 2 * tuba_table_size; 100 i = (tuba_table_size + 1) * sizeof(tc); 101 new = (struct tuba_cache **)malloc((unsigned)i, M_RTABLE, wait); 102 if (new == 0) { 103 tuba_table_size = old_size; 104 rn_delete((caddr_t)&tc->tc_addr, (caddr_t)0, tuba_tree); 105 free((caddr_t)tc, M_RTABLE); 106 return (0); 107 } 108 bzero((caddr_t)new, (unsigned)i); 109 if (tuba_table) 110 bcopy((caddr_t)tuba_table, (caddr_t)new, i >> 1); 111 tuba_table[tc->tc_index = tuba_table_size] = tc; 112 return (tc->tc_index); 113 } 114