1 /* route.c 4.9 82/06/06 */ 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 if (af >= AF_MAX) 34 return; 35 (*afswitch[af].af_hash)(dst, &h); 36 rtmin = 0, hash = h.afh_hosthash; 37 for (m = rthost[hash % RTHASHSIZ]; m; m = m->m_next) { 38 rt = mtod(m, struct rtentry *); 39 if (rt->rt_hash != hash) 40 continue; 41 if ((rt->rt_flags & RTF_UP) == 0 || 42 (rt->rt_ifp->if_flags & IFF_UP) == 0) 43 continue; 44 if (bcmp((caddr_t)&rt->rt_dst, (caddr_t)dst, sizeof (*dst))) 45 continue; 46 if (rtmin == 0 || rt->rt_use < rtmin->rt_use) 47 rtmin = rt; 48 } 49 if (rtmin) 50 goto found; 51 52 hash = h.afh_nethash; 53 match = afswitch[af].af_netmatch; 54 for (m = rtnet[hash % RTHASHSIZ]; m; m = m->m_next) { 55 rt = mtod(m, struct rtentry *); 56 if (rt->rt_hash != hash) 57 continue; 58 if ((rt->rt_flags & RTF_UP) == 0 || 59 (rt->rt_ifp->if_flags & IFF_UP) == 0) 60 continue; 61 if (rt->rt_dst.sa_family != af || !(*match)(&rt->rt_dst, dst)) 62 continue; 63 if (rtmin == 0 || rt->rt_use < rtmin->rt_use) 64 rtmin = rt; 65 } 66 found: 67 ro->ro_rt = rtmin; 68 if (rtmin) 69 rtmin->rt_refcnt++; 70 } 71 72 rtfree(rt) 73 register struct rtentry *rt; 74 { 75 76 if (rt == 0) 77 panic("freeroute"); 78 rt->rt_refcnt--; 79 /* on refcnt == 0 reclaim? notify someone? */ 80 } 81 82 #define equal(a1, a2) \ 83 (bcmp((caddr_t)(a1), (caddr_t)(a2), sizeof (struct sockaddr)) == 0) 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, new) 90 int req; 91 struct rtentry *new; 92 { 93 register struct rtentry *rt; 94 register struct mbuf *m, **mprev; 95 register struct sockaddr *sa = &new->rt_dst; 96 register struct sockaddr *gate = &new->rt_gateway; 97 struct afhash h; 98 int af = sa->sa_family, doinghost, s, error = 0; 99 int hash, (*match)(); 100 101 COUNT(RTREQUEST); 102 if (af >= AF_MAX) 103 return (EAFNOSUPPORT); 104 (*afswitch[af].af_hash)(sa, &h); 105 doinghost = new->rt_flags & RTF_HOST; 106 if (doinghost) { 107 hash = h.afh_hosthash; 108 mprev = &rthost[hash % RTHASHSIZ]; 109 } else { 110 hash = h.afh_nethash; 111 mprev = &rtnet[hash % RTHASHSIZ]; 112 } 113 match = afswitch[af].af_netmatch; 114 s = splimp(); 115 for (; m = *mprev; mprev = &m->m_next) { 116 rt = mtod(m, struct rtentry *); 117 if (rt->rt_hash != hash) 118 continue; 119 if (doinghost) { 120 if (!equal(&rt->rt_dst, sa)) 121 continue; 122 } else { 123 if (rt->rt_dst.sa_family != sa->sa_family || 124 (*match)(&rt->rt_dst, sa) == 0) 125 continue; 126 } 127 if (equal(&rt->rt_gateway, gate)) 128 break; 129 } 130 switch (req) { 131 132 case SIOCDELRT: 133 if (m == 0) { 134 error = ESRCH; 135 goto bad; 136 } 137 rt->rt_flags &= ~RTF_UP; 138 if (rt->rt_refcnt > 0) /* should we notify protocols? */ 139 error = EBUSY; 140 else 141 *mprev = m_free(m); 142 break; 143 144 case SIOCCHGRT: 145 if (m == 0) { 146 error = ESRCH; 147 goto bad; 148 } 149 rt->rt_flags = new->rt_flags; 150 if (rt->rt_refcnt > 0) { 151 error = EBUSY; 152 goto bad; 153 } 154 if (!equal(&rt->rt_gateway, gate)) 155 goto changerouter; 156 break; 157 158 case SIOCADDRT: 159 if (m != 0) { 160 error = EEXIST; 161 goto bad; 162 } 163 m = m_get(M_DONTWAIT); 164 if (m == 0) { 165 error = ENOBUFS; 166 goto bad; 167 } 168 m->m_off = MMINOFF; 169 m->m_len = sizeof (struct rtentry); 170 rt = mtod(m, struct rtentry *); 171 *rt = *new; 172 rt->rt_hash = hash; 173 *mprev = m; 174 rt->rt_use = 0; 175 rt->rt_refcnt = 0; 176 changerouter: 177 rt->rt_ifp = if_ifwithnet(gate); 178 if (rt->rt_ifp == 0) 179 rt->rt_flags &= ~RTF_UP; 180 break; 181 } 182 bad: 183 splx(s); 184 return (error); 185 } 186 187 /* 188 * Set up a routing table entry, normally 189 * for an interface. 190 */ 191 rtinit(dst, gateway, flags) 192 struct sockaddr *dst, *gateway; 193 int flags; 194 { 195 struct rtentry route; 196 struct route ro; 197 198 route.rt_dst = *dst; 199 route.rt_gateway = *gateway; 200 route.rt_flags = flags; 201 route.rt_use = 0; 202 (void) rtrequest(SIOCADDRT, &route); 203 ro.ro_rt = 0; 204 ro.ro_dst = *dst; 205 rtalloc(&ro); 206 } 207