1 /* route.c 4.6 82/03/31 */ 2 3 #include "../h/param.h" 4 #include "../h/systm.h" 5 #include "../h/mbuf.h" 6 #include "../h/protosw.h" 7 #include "../h/socket.h" 8 #include "../h/ioctl.h" 9 #include "../net/in.h" 10 #include "../net/in_systm.h" 11 #include "../net/if.h" 12 #include "../net/af.h" 13 #include "../net/route.h" 14 #include <errno.h> 15 16 /* 17 * Packet routing routines. 18 */ 19 20 rtalloc(ro) 21 register struct route *ro; 22 { 23 register struct rtentry *rt, *rtmin; 24 register struct mbuf *m; 25 register int hash, (*match)(); 26 struct afhash h; 27 struct sockaddr *dst = &ro->ro_dst; 28 int af = dst->sa_family; 29 30 COUNT(RTALLOC); 31 if (ro->ro_rt && ro->ro_rt->rt_ifp) /* XXX */ 32 return; 33 (*afswitch[af].af_hash)(dst, &h); 34 rtmin = 0, hash = h.afh_hosthash; 35 for (m = rthost[hash % RTHASHSIZ]; m; m = m->m_next) { 36 rt = mtod(m, struct rtentry *); 37 if (rt->rt_hash != hash) 38 continue; 39 if (bcmp((caddr_t)&rt->rt_dst, (caddr_t)dst, sizeof (*dst))) 40 continue; 41 if (rtmin == 0 || rt->rt_use < rtmin->rt_use) 42 rtmin = rt; 43 } 44 if (rtmin) 45 goto found; 46 47 hash = h.afh_nethash; 48 match = afswitch[af].af_netmatch; 49 for (m = rtnet[hash % RTHASHSIZ]; m; m = m->m_next) { 50 rt = mtod(m, struct rtentry *); 51 if (rt->rt_hash != hash) 52 continue; 53 if (rt->rt_dst.sa_family != af || !(*match)(&rt->rt_dst, dst)) 54 continue; 55 if (rtmin == 0 || rt->rt_use < rtmin->rt_use) 56 rtmin = rt; 57 } 58 found: 59 ro->ro_rt = rtmin; 60 if (rtmin) 61 rtmin->rt_refcnt++; 62 } 63 64 rtfree(rt) 65 register struct rtentry *rt; 66 { 67 68 if (rt == 0) 69 panic("freeroute"); 70 rt->rt_refcnt--; 71 /* on refcnt == 0 reclaim? notify someone? */ 72 } 73 74 #define equal(a1, a2) \ 75 (bcmp((caddr_t)(a1), (caddr_t)(a2), sizeof (struct sockaddr)) == 0) 76 /* 77 * Carry out a request to change the routing table. Called by 78 * interfaces at boot time to make their ``local routes'' known 79 * and for ioctl's. 80 */ 81 rtrequest(req, new) 82 int req; 83 register struct rtentry *new; 84 { 85 register struct rtentry *rt; 86 register struct mbuf *m, **mprev; 87 register int hash, (*match)(); 88 register struct sockaddr *sa = &new->rt_dst; 89 register struct sockaddr *gate = &new->rt_gateway; 90 struct afhash h; 91 struct mbuf **oldmprev; 92 int af = sa->sa_family, doinghost, s, error = 0; 93 94 COUNT(RTREQUEST); 95 (*afswitch[af].af_hash)(sa, &h); 96 hash = h.afh_hosthash; 97 mprev = &rthost[hash % RTHASHSIZ]; 98 doinghost = 1; 99 s = splimp(); 100 again: 101 for (; m = *mprev; mprev = &m->m_next) { 102 rt = mtod(m, struct rtentry *); 103 if (rt->rt_hash != hash) 104 continue; 105 if (doinghost) { 106 if (!equal(&rt->rt_dst, sa)) 107 continue; 108 } else { 109 if (rt->rt_dst.sa_family != sa->sa_family || 110 (*match)(&rt->rt_dst, sa) == 0) 111 continue; 112 } 113 /* require full match on deletions */ 114 if (req == SIOCDELRT && !equal(&rt->rt_gateway, gate)) 115 continue; 116 /* don't keep multiple identical entries */ 117 if (req == SIOCADDRT && equal(&rt->rt_gateway, gate)) { 118 error = EEXIST; 119 goto bad; 120 } 121 break; 122 } 123 if (m == 0 && doinghost) { 124 hash = h.afh_nethash; 125 oldmprev = mprev; 126 mprev = &rtnet[hash % RTHASHSIZ]; 127 match = afswitch[af].af_netmatch; 128 doinghost = 0; 129 goto again; 130 } 131 132 if (m == 0 && req != SIOCADDRT) { 133 error = ESRCH; 134 goto bad; 135 } 136 found: 137 switch (req) { 138 139 case SIOCDELRT: 140 rt->rt_flags &= ~RTF_UP; 141 if (rt->rt_refcnt > 0) /* should we notify protocols? */ 142 error = EBUSY; 143 else 144 *mprev = m_free(m); 145 break; 146 147 case SIOCCHGRT: 148 rt->rt_flags = new->rt_flags; 149 if (rt->rt_refcnt > 0) 150 error = EBUSY; 151 else if (!equal(&rt->rt_gateway, gate)) 152 goto newneighbor; 153 break; 154 155 case SIOCADDRT: 156 m = m_get(M_DONTWAIT); 157 if (m == 0) { 158 error = ENOBUFS; 159 break; 160 } 161 m->m_off = MMINOFF; 162 m->m_len = sizeof (struct rtentry); 163 rt = mtod(m, struct rtentry *); 164 *rt = *new; 165 if (new->rt_flags & RTF_HOST) { 166 rt->rt_hash = h.afh_hosthash; 167 *oldmprev = m; 168 } else { 169 rt->rt_hash = h.afh_nethash; 170 *mprev = m; 171 } 172 rt->rt_use = 0; 173 rt->rt_refcnt = 0; 174 newneighbor: 175 rt->rt_ifp = if_ifwithnet(gate); 176 if (rt->rt_ifp == 0) 177 rt->rt_flags &= ~RTF_UP; 178 break; 179 } 180 bad: 181 splx(s); 182 return (error); 183 } 184 185 /* 186 * Set up a routing table entry, normally 187 * for an interface. 188 */ 189 rtinit(dst, gateway, flags) 190 struct sockaddr *dst, *gateway; 191 int flags; 192 { 193 struct rtentry route; 194 struct route ro; 195 196 route.rt_dst = *dst; 197 route.rt_gateway = *gateway; 198 route.rt_flags = flags; 199 route.rt_use = 0; 200 (void) rtrequest(SIOCADDRT, &route); 201 ro.ro_rt = 0; 202 ro.ro_dst = *dst; 203 rtalloc(&ro); 204 } 205