Commit | Line | Data |
---|---|---|
f4d55810 | 1 | /* route.c 4.16 83/02/10 */ |
9d03c806 SL |
2 | |
3 | #include "../h/param.h" | |
ee787340 | 4 | #include "../h/systm.h" |
9d03c806 SL |
5 | #include "../h/mbuf.h" |
6 | #include "../h/protosw.h" | |
7 | #include "../h/socket.h" | |
ee787340 | 8 | #include "../h/ioctl.h" |
f4d55810 SL |
9 | #include "../h/errno.h" |
10 | ||
ee787340 | 11 | #include "../net/if.h" |
9d03c806 SL |
12 | #include "../net/af.h" |
13 | #include "../net/route.h" | |
9d03c806 | 14 | |
a13c006d | 15 | int rttrash; /* routes not in table but not freed */ |
9d03c806 SL |
16 | /* |
17 | * Packet routing routines. | |
18 | */ | |
f6311fb6 | 19 | rtalloc(ro) |
9d03c806 SL |
20 | register struct route *ro; |
21 | { | |
22 | register struct rtentry *rt, *rtmin; | |
23 | register struct mbuf *m; | |
9ccc7370 SL |
24 | register unsigned hash; |
25 | register int (*match)(); | |
9d03c806 SL |
26 | struct afhash h; |
27 | struct sockaddr *dst = &ro->ro_dst; | |
fc74f0c9 | 28 | int af = dst->sa_family; |
9d03c806 | 29 | |
f6311fb6 | 30 | if (ro->ro_rt && ro->ro_rt->rt_ifp) /* XXX */ |
9d03c806 | 31 | return; |
e65dcd4c SL |
32 | if (af >= AF_MAX) |
33 | return; | |
9d03c806 | 34 | (*afswitch[af].af_hash)(dst, &h); |
fc74f0c9 SL |
35 | rtmin = 0, hash = h.afh_hosthash; |
36 | for (m = rthost[hash % RTHASHSIZ]; m; m = m->m_next) { | |
9d03c806 | 37 | rt = mtod(m, struct rtentry *); |
fc74f0c9 SL |
38 | if (rt->rt_hash != hash) |
39 | continue; | |
90137845 SL |
40 | if ((rt->rt_flags & RTF_UP) == 0 || |
41 | (rt->rt_ifp->if_flags & IFF_UP) == 0) | |
42 | continue; | |
fc74f0c9 | 43 | if (bcmp((caddr_t)&rt->rt_dst, (caddr_t)dst, sizeof (*dst))) |
9d03c806 | 44 | continue; |
9d03c806 SL |
45 | if (rtmin == 0 || rt->rt_use < rtmin->rt_use) |
46 | rtmin = rt; | |
47 | } | |
fc74f0c9 SL |
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; | |
90137845 SL |
57 | if ((rt->rt_flags & RTF_UP) == 0 || |
58 | (rt->rt_ifp->if_flags & IFF_UP) == 0) | |
59 | continue; | |
fc74f0c9 SL |
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; | |
9d03c806 | 64 | } |
fc74f0c9 SL |
65 | found: |
66 | ro->ro_rt = rtmin; | |
a9f3e174 SL |
67 | if (rtmin) |
68 | rtmin->rt_refcnt++; | |
9d03c806 SL |
69 | } |
70 | ||
f6311fb6 | 71 | rtfree(rt) |
c124e997 SL |
72 | register struct rtentry *rt; |
73 | { | |
f6311fb6 | 74 | |
c124e997 | 75 | if (rt == 0) |
a1edc12b | 76 | panic("rtfree"); |
c124e997 | 77 | rt->rt_refcnt--; |
a13c006d BJ |
78 | if (rt->rt_refcnt == 0 && (rt->rt_flags&RTF_UP) == 0) { |
79 | rttrash--; | |
80 | (void) m_free(dtom(rt)); | |
81 | } | |
c124e997 SL |
82 | } |
83 | ||
9d03c806 | 84 | /* |
c124e997 SL |
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. | |
9d03c806 | 88 | */ |
a13c006d | 89 | rtrequest(req, entry) |
9d03c806 | 90 | int req; |
a13c006d | 91 | register struct rtentry *entry; |
9d03c806 | 92 | { |
9d03c806 | 93 | register struct mbuf *m, **mprev; |
a13c006d | 94 | register struct rtentry *rt; |
9d03c806 | 95 | struct afhash h; |
a13c006d BJ |
96 | int af, s, error = 0, hash, (*match)(); |
97 | struct ifnet *ifp; | |
9d03c806 | 98 | |
a13c006d | 99 | af = entry->rt_dst.sa_family; |
e65dcd4c SL |
100 | if (af >= AF_MAX) |
101 | return (EAFNOSUPPORT); | |
a13c006d BJ |
102 | (*afswitch[af].af_hash)(&entry->rt_dst, &h); |
103 | if (entry->rt_flags & RTF_HOST) { | |
d7887ae9 BJ |
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; | |
c124e997 | 111 | s = splimp(); |
9d03c806 SL |
112 | for (; m = *mprev; mprev = &m->m_next) { |
113 | rt = mtod(m, struct rtentry *); | |
fc74f0c9 | 114 | if (rt->rt_hash != hash) |
9d03c806 | 115 | continue; |
a13c006d BJ |
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)) | |
9d03c806 SL |
120 | continue; |
121 | } else { | |
a13c006d BJ |
122 | if (rt->rt_dst.sa_family != entry->rt_dst.sa_family || |
123 | (*match)(&rt->rt_dst, &entry->rt_dst) == 0) | |
9d03c806 SL |
124 | continue; |
125 | } | |
a13c006d | 126 | if (equal(&rt->rt_gateway, &entry->rt_gateway)) |
d7887ae9 | 127 | break; |
c124e997 | 128 | } |
9d03c806 SL |
129 | switch (req) { |
130 | ||
131 | case SIOCDELRT: | |
d7887ae9 BJ |
132 | if (m == 0) { |
133 | error = ESRCH; | |
134 | goto bad; | |
135 | } | |
a13c006d | 136 | *mprev = m->m_next; |
d7887ae9 | 137 | if (rt->rt_refcnt > 0) { |
a13c006d BJ |
138 | rt->rt_flags &= ~RTF_UP; |
139 | rttrash++; | |
140 | m->m_next = 0; | |
141 | } else | |
142 | (void) m_free(m); | |
9d03c806 SL |
143 | break; |
144 | ||
145 | case SIOCADDRT: | |
a13c006d | 146 | if (m) { |
d7887ae9 BJ |
147 | error = EEXIST; |
148 | goto bad; | |
149 | } | |
a13c006d BJ |
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 | } | |
cce93e4b | 158 | m = m_get(M_DONTWAIT, MT_RTABLE); |
c124e997 SL |
159 | if (m == 0) { |
160 | error = ENOBUFS; | |
d7887ae9 | 161 | goto bad; |
c124e997 | 162 | } |
a13c006d | 163 | *mprev = m; |
9d03c806 | 164 | m->m_off = MMINOFF; |
f6311fb6 | 165 | m->m_len = sizeof (struct rtentry); |
9d03c806 | 166 | rt = mtod(m, struct rtentry *); |
d7887ae9 | 167 | rt->rt_hash = hash; |
a13c006d BJ |
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)); | |
fc74f0c9 | 172 | rt->rt_refcnt = 0; |
a13c006d BJ |
173 | rt->rt_use = 0; |
174 | rt->rt_ifp = ifp; | |
9d03c806 SL |
175 | break; |
176 | } | |
c124e997 SL |
177 | bad: |
178 | splx(s); | |
179 | return (error); | |
9d03c806 | 180 | } |
f6311fb6 SL |
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; | |
f6311fb6 | 191 | |
a13c006d | 192 | bzero((caddr_t)&route, sizeof (route)); |
f6311fb6 SL |
193 | route.rt_dst = *dst; |
194 | route.rt_gateway = *gateway; | |
195 | route.rt_flags = flags; | |
a1edc12b | 196 | (void) rtrequest((int)SIOCADDRT, &route); |
f6311fb6 | 197 | } |