1 /* route.c 4.16 83/02/10 */ 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 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 * Carry out a request to change the routing table. Called by 86 * interfaces at boot time to make their ``local routes'' known 87 * and for ioctl's. 88 */ 89 rtrequest(req, entry) 90 int req; 91 register struct rtentry *entry; 92 { 93 register struct mbuf *m, **mprev; 94 register struct rtentry *rt; 95 struct afhash h; 96 int af, s, error = 0, hash, (*match)(); 97 struct ifnet *ifp; 98 99 af = entry->rt_dst.sa_family; 100 if (af >= AF_MAX) 101 return (EAFNOSUPPORT); 102 (*afswitch[af].af_hash)(&entry->rt_dst, &h); 103 if (entry->rt_flags & RTF_HOST) { 104 hash = h.afh_hosthash; 105 mprev = &rthost[hash % RTHASHSIZ]; 106 } else { 107 hash = h.afh_nethash; 108 mprev = &rtnet[hash % RTHASHSIZ]; 109 } 110 match = afswitch[af].af_netmatch; 111 s = splimp(); 112 for (; m = *mprev; mprev = &m->m_next) { 113 rt = mtod(m, struct rtentry *); 114 if (rt->rt_hash != hash) 115 continue; 116 if (entry->rt_flags & RTF_HOST) { 117 #define equal(a1, a2) \ 118 (bcmp((caddr_t)(a1), (caddr_t)(a2), sizeof (struct sockaddr)) == 0) 119 if (!equal(&rt->rt_dst, &entry->rt_dst)) 120 continue; 121 } else { 122 if (rt->rt_dst.sa_family != entry->rt_dst.sa_family || 123 (*match)(&rt->rt_dst, &entry->rt_dst) == 0) 124 continue; 125 } 126 if (equal(&rt->rt_gateway, &entry->rt_gateway)) 127 break; 128 } 129 switch (req) { 130 131 case SIOCDELRT: 132 if (m == 0) { 133 error = ESRCH; 134 goto bad; 135 } 136 *mprev = m->m_next; 137 if (rt->rt_refcnt > 0) { 138 rt->rt_flags &= ~RTF_UP; 139 rttrash++; 140 m->m_next = 0; 141 } else 142 (void) m_free(m); 143 break; 144 145 case SIOCADDRT: 146 if (m) { 147 error = EEXIST; 148 goto bad; 149 } 150 ifp = if_ifwithaddr(&entry->rt_gateway); 151 if (ifp == 0) { 152 ifp = if_ifwithnet(&entry->rt_gateway); 153 if (ifp == 0) { 154 error = ENETUNREACH; 155 goto bad; 156 } 157 } 158 m = m_get(M_DONTWAIT, MT_RTABLE); 159 if (m == 0) { 160 error = ENOBUFS; 161 goto bad; 162 } 163 *mprev = m; 164 m->m_off = MMINOFF; 165 m->m_len = sizeof (struct rtentry); 166 rt = mtod(m, struct rtentry *); 167 rt->rt_hash = hash; 168 rt->rt_dst = entry->rt_dst; 169 rt->rt_gateway = entry->rt_gateway; 170 rt->rt_flags = 171 RTF_UP | (entry->rt_flags & (RTF_HOST|RTF_GATEWAY)); 172 rt->rt_refcnt = 0; 173 rt->rt_use = 0; 174 rt->rt_ifp = ifp; 175 break; 176 } 177 bad: 178 splx(s); 179 return (error); 180 } 181 182 /* 183 * Set up a routing table entry, normally 184 * for an interface. 185 */ 186 rtinit(dst, gateway, flags) 187 struct sockaddr *dst, *gateway; 188 int flags; 189 { 190 struct rtentry route; 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((int)SIOCADDRT, &route); 197 } 198