1 /* route.c 4.19 83/04/05 */ 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 "../h/errno.h" 10 11 #include "../net/if.h" 12 #include "../net/af.h" 13 #include "../net/route.h" 14 15 int rttrash; /* routes not in table but not freed */ 16 /* 17 * Packet routing routines. 18 */ 19 rtalloc(ro) 20 register struct route *ro; 21 { 22 register struct rtentry *rt, *rtmin; 23 register struct mbuf *m; 24 register unsigned hash; 25 register int (*match)(); 26 struct afhash h; 27 struct sockaddr *dst = &ro->ro_dst; 28 u_int af = dst->sa_family; 29 30 if (ro->ro_rt && ro->ro_rt->rt_ifp) /* XXX */ 31 return; 32 if (af >= AF_MAX) 33 return; 34 (*afswitch[af].af_hash)(dst, &h); 35 rtmin = 0, hash = h.afh_hosthash; 36 for (m = rthost[hash % RTHASHSIZ]; m; m = m->m_next) { 37 rt = mtod(m, struct rtentry *); 38 if (rt->rt_hash != hash) 39 continue; 40 if ((rt->rt_flags & RTF_UP) == 0 || 41 (rt->rt_ifp->if_flags & IFF_UP) == 0) 42 continue; 43 if (bcmp((caddr_t)&rt->rt_dst, (caddr_t)dst, sizeof (*dst))) 44 continue; 45 if (rtmin == 0 || rt->rt_use < rtmin->rt_use) 46 rtmin = rt; 47 } 48 if (rtmin) 49 goto found; 50 51 hash = h.afh_nethash; 52 match = afswitch[af].af_netmatch; 53 for (m = rtnet[hash % RTHASHSIZ]; m; m = m->m_next) { 54 rt = mtod(m, struct rtentry *); 55 if (rt->rt_hash != hash) 56 continue; 57 if ((rt->rt_flags & RTF_UP) == 0 || 58 (rt->rt_ifp->if_flags & IFF_UP) == 0) 59 continue; 60 if (rt->rt_dst.sa_family != af || !(*match)(&rt->rt_dst, dst)) 61 continue; 62 if (rtmin == 0 || rt->rt_use < rtmin->rt_use) 63 rtmin = rt; 64 } 65 found: 66 ro->ro_rt = rtmin; 67 if (rtmin) 68 rtmin->rt_refcnt++; 69 } 70 71 rtfree(rt) 72 register struct rtentry *rt; 73 { 74 75 if (rt == 0) 76 panic("rtfree"); 77 rt->rt_refcnt--; 78 if (rt->rt_refcnt == 0 && (rt->rt_flags&RTF_UP) == 0) { 79 rttrash--; 80 (void) m_free(dtom(rt)); 81 } 82 } 83 84 /* 85 * Force a routing table entry to the specified 86 * destination to go through the given gateway. 87 * Normally called as a result of a routing redirect 88 * message from the network layer. 89 * 90 * N.B.: must be called at splnet or higher 91 * 92 * Should notify all parties with a reference to 93 * the route that it's changed (so, for instance, 94 * round trip time estimates may be recalculated), 95 * but we have no back pointers at the moment. 96 */ 97 rtredirect(dst, gateway) 98 struct sockaddr *dst, *gateway; 99 { 100 struct route ro; 101 register struct rtentry *rt; 102 103 /* verify the gateway is directly reachable */ 104 if (if_ifwithnet(gateway) == 0) 105 return; 106 ro.ro_dst = *dst; 107 ro.ro_rt = NULL; 108 rtalloc(&ro); 109 rt = ro.ro_rt; 110 /* 111 * Don't listen to the redirect if it's 112 * for a route to an interface. 113 * 114 * Should probably create a new entry when 115 * the lookup fails. This will be necessary 116 * when wildcard routes are added. 117 */ 118 if (rt == NULL || (rt->rt_flags & RTF_GATEWAY) == 0) 119 return; 120 rt->rt_gateway = *gateway; 121 rtfree(rt); 122 } 123 124 /* 125 * Carry out a request to change the routing table. Called by 126 * interfaces at boot time to make their ``local routes'' known 127 * and for ioctl's. 128 */ 129 rtrequest(req, entry) 130 int req; 131 register struct rtentry *entry; 132 { 133 register struct mbuf *m, **mprev; 134 register struct rtentry *rt; 135 struct afhash h; 136 int s, error = 0, hash, (*match)(); 137 u_int af; 138 struct ifnet *ifp; 139 140 af = entry->rt_dst.sa_family; 141 if (af >= AF_MAX) 142 return (EAFNOSUPPORT); 143 (*afswitch[af].af_hash)(&entry->rt_dst, &h); 144 if (entry->rt_flags & RTF_HOST) { 145 hash = h.afh_hosthash; 146 mprev = &rthost[hash % RTHASHSIZ]; 147 } else { 148 hash = h.afh_nethash; 149 mprev = &rtnet[hash % RTHASHSIZ]; 150 } 151 match = afswitch[af].af_netmatch; 152 s = splimp(); 153 for (; m = *mprev; mprev = &m->m_next) { 154 rt = mtod(m, struct rtentry *); 155 if (rt->rt_hash != hash) 156 continue; 157 if (entry->rt_flags & RTF_HOST) { 158 #define equal(a1, a2) \ 159 (bcmp((caddr_t)(a1), (caddr_t)(a2), sizeof (struct sockaddr)) == 0) 160 if (!equal(&rt->rt_dst, &entry->rt_dst)) 161 continue; 162 } else { 163 if (rt->rt_dst.sa_family != entry->rt_dst.sa_family || 164 (*match)(&rt->rt_dst, &entry->rt_dst) == 0) 165 continue; 166 } 167 if (equal(&rt->rt_gateway, &entry->rt_gateway)) 168 break; 169 } 170 switch (req) { 171 172 case SIOCDELRT: 173 if (m == 0) { 174 error = ESRCH; 175 goto bad; 176 } 177 *mprev = m->m_next; 178 if (rt->rt_refcnt > 0) { 179 rt->rt_flags &= ~RTF_UP; 180 rttrash++; 181 m->m_next = 0; 182 } else 183 (void) m_free(m); 184 break; 185 186 case SIOCADDRT: 187 if (m) { 188 error = EEXIST; 189 goto bad; 190 } 191 ifp = if_ifwithaddr(&entry->rt_gateway); 192 if (ifp == 0) { 193 ifp = if_ifwithnet(&entry->rt_gateway); 194 if (ifp == 0) { 195 error = ENETUNREACH; 196 goto bad; 197 } 198 } 199 m = m_get(M_DONTWAIT, MT_RTABLE); 200 if (m == 0) { 201 error = ENOBUFS; 202 goto bad; 203 } 204 *mprev = m; 205 m->m_off = MMINOFF; 206 m->m_len = sizeof (struct rtentry); 207 rt = mtod(m, struct rtentry *); 208 rt->rt_hash = hash; 209 rt->rt_dst = entry->rt_dst; 210 rt->rt_gateway = entry->rt_gateway; 211 rt->rt_flags = 212 RTF_UP | (entry->rt_flags & (RTF_HOST|RTF_GATEWAY)); 213 rt->rt_refcnt = 0; 214 rt->rt_use = 0; 215 rt->rt_ifp = ifp; 216 break; 217 } 218 bad: 219 splx(s); 220 return (error); 221 } 222 223 /* 224 * Set up a routing table entry, normally 225 * for an interface. 226 */ 227 rtinit(dst, gateway, flags) 228 struct sockaddr *dst, *gateway; 229 int flags; 230 { 231 struct rtentry route; 232 int cmd; 233 234 if (flags == -1) { 235 cmd = (int)SIOCDELRT; 236 flags = 0; 237 } else { 238 cmd = (int)SIOCADDRT; 239 } 240 bzero((caddr_t)&route, sizeof (route)); 241 route.rt_dst = *dst; 242 route.rt_gateway = *gateway; 243 route.rt_flags = flags; 244 (void) rtrequest(cmd, &route); 245 } 246