1 /* route.c 4.5 82/03/30 */ 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 } 61 62 rtfree(rt) 63 register struct rtentry *rt; 64 { 65 66 COUNT(FREEROUTE); 67 if (rt == 0) 68 panic("freeroute"); 69 rt->rt_refcnt--; 70 /* on refcnt == 0 reclaim? notify someone? */ 71 } 72 73 #define equal(a1, a2) \ 74 (bcmp((caddr_t)(a1), (caddr_t)(a2), sizeof (struct sockaddr)) == 0) 75 /* 76 * Carry out a request to change the routing table. Called by 77 * interfaces at boot time to make their ``local routes'' known 78 * and for ioctl's. 79 */ 80 rtrequest(req, new) 81 int req; 82 register struct rtentry *new; 83 { 84 register struct rtentry *rt; 85 register struct mbuf *m, **mprev; 86 register int hash, (*match)(); 87 register struct sockaddr *sa = &new->rt_dst; 88 register struct sockaddr *gate = &new->rt_gateway; 89 struct afhash h; 90 int af = sa->sa_family, doinghost, s, error = 0; 91 92 COUNT(RTREQUEST); 93 (*afswitch[af].af_hash)(sa, &h); 94 hash = h.afh_hosthash; 95 mprev = &rthost[hash % RTHASHSIZ]; 96 doinghost = 1; 97 s = splimp(); 98 again: 99 for (; m = *mprev; mprev = &m->m_next) { 100 rt = mtod(m, struct rtentry *); 101 if (rt->rt_hash != hash) 102 continue; 103 if (doinghost) { 104 if (!equal(&rt->rt_dst, sa)) 105 continue; 106 } else { 107 if (rt->rt_dst.sa_family != sa->sa_family || 108 (*match)(&rt->rt_dst, sa) == 0) 109 continue; 110 } 111 /* require full match on deletions */ 112 if (req == SIOCDELRT && !equal(&rt->rt_gateway, gate)) 113 continue; 114 /* don't keep multiple identical entries */ 115 if (req == SIOCADDRT && equal(&rt->rt_gateway, gate)) { 116 error = EEXIST; 117 goto bad; 118 } 119 break; 120 } 121 if (m == 0 && doinghost) { 122 hash = h.afh_nethash; 123 mprev = &rtnet[hash % RTHASHSIZ]; 124 match = afswitch[af].af_netmatch; 125 doinghost = 0; 126 goto again; 127 } 128 129 if (m == 0 && req != SIOCADDRT) { 130 error = ESRCH; 131 goto bad; 132 } 133 found: 134 switch (req) { 135 136 case SIOCDELRT: 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 rt->rt_flags = new->rt_flags; 146 if (rt->rt_refcnt > 0) 147 error = EBUSY; 148 else if (!equal(&rt->rt_gateway, gate)) 149 goto newneighbor; 150 break; 151 152 case SIOCADDRT: 153 m = m_get(M_DONTWAIT); 154 if (m == 0) { 155 error = ENOBUFS; 156 break; 157 } 158 m->m_off = MMINOFF; 159 m->m_len = sizeof (struct rtentry); 160 *mprev = m; 161 rt = mtod(m, struct rtentry *); 162 *rt = *new; 163 rt->rt_hash = new->rt_flags & RTF_HOST ? 164 h.afh_hosthash : h.afh_nethash; 165 rt->rt_use = 0; 166 rt->rt_refcnt = 0; 167 newneighbor: 168 rt->rt_ifp = if_ifwithnet(gate); 169 if (rt->rt_ifp == 0) 170 rt->rt_flags &= ~RTF_UP; 171 break; 172 } 173 bad: 174 splx(s); 175 return (error); 176 } 177 178 /* 179 * Set up a routing table entry, normally 180 * for an interface. 181 */ 182 rtinit(dst, gateway, flags) 183 struct sockaddr *dst, *gateway; 184 int flags; 185 { 186 struct rtentry route; 187 struct route ro; 188 189 route.rt_dst = *dst; 190 route.rt_gateway = *gateway; 191 route.rt_flags = flags; 192 route.rt_use = 0; 193 (void) rtrequest(SIOCADDRT, &route); 194 ro.ro_rt = 0; 195 ro.ro_dst = *dst; 196 rtalloc(&ro); 197 } 198