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