Commit | Line | Data |
---|---|---|
e831d540 KS |
1 | /* |
2 | * Copyright (c) 1988 Regents of the University of California. | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms are permitted | |
6 | * provided that the above copyright notice and this paragraph are | |
7 | * duplicated in all such forms and that any documentation, | |
8 | * advertising materials, and other materials related to such | |
9 | * distribution and use acknowledge that the software was developed | |
10 | * by the University of California, Berkeley. The name of the | |
11 | * University may not be used to endorse or promote products derived | |
12 | * from this software without specific prior written permission. | |
13 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR | |
14 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED | |
15 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. | |
16 | * | |
2e5be88b | 17 | * @(#)rtsock.c 7.9 (Berkeley) %G% |
e831d540 KS |
18 | */ |
19 | ||
e831d540 KS |
20 | #include "param.h" |
21 | #include "mbuf.h" | |
e831d540 KS |
22 | #include "user.h" |
23 | #include "proc.h" | |
24 | #include "socket.h" | |
25 | #include "socketvar.h" | |
26 | #include "domain.h" | |
27 | #include "protosw.h" | |
28 | #include "errno.h" | |
29 | ||
30 | #include "af.h" | |
a0d75f26 | 31 | #include "if.h" |
e831d540 KS |
32 | #include "route.h" |
33 | #include "raw_cb.h" | |
34 | ||
d301d150 | 35 | #include "machine/mtpr.h" |
e831d540 | 36 | |
b72a6efb KS |
37 | struct sockaddr route_dst = { 0, PF_ROUTE, }; |
38 | struct sockaddr route_src = { 0, PF_ROUTE, }; | |
39 | struct sockproto route_proto = { PF_ROUTE, }; | |
40 | ||
e831d540 | 41 | /*ARGSUSED*/ |
a0d75f26 | 42 | route_usrreq(so, req, m, nam, control) |
e831d540 KS |
43 | register struct socket *so; |
44 | int req; | |
a0d75f26 | 45 | struct mbuf *m, *nam, *control; |
e831d540 KS |
46 | { |
47 | register int error = 0; | |
48 | register struct rawcb *rp = sotorawcb(so); | |
b72a6efb KS |
49 | if (req == PRU_ATTACH) { |
50 | MALLOC(rp, struct rawcb *, sizeof(*rp), M_PCB, M_WAITOK); | |
51 | if (so->so_pcb = (caddr_t)rp) | |
52 | bzero(so->so_pcb, sizeof(*rp)); | |
53 | ||
54 | } | |
e831d540 KS |
55 | if (req == PRU_DETACH && rp) { |
56 | int af = rp->rcb_proto.sp_protocol; | |
57 | if (af == AF_INET) | |
58 | route_cb.ip_count--; | |
59 | else if (af == AF_NS) | |
60 | route_cb.ns_count--; | |
e831d540 KS |
61 | else if (af == AF_ISO) |
62 | route_cb.iso_count--; | |
e831d540 KS |
63 | route_cb.any_count--; |
64 | } | |
a0d75f26 | 65 | error = raw_usrreq(so, req, m, nam, control); |
e831d540 | 66 | rp = sotorawcb(so); |
b72a6efb | 67 | if (req == PRU_ATTACH && rp) { |
e831d540 | 68 | int af = rp->rcb_proto.sp_protocol; |
b72a6efb KS |
69 | if (error) { |
70 | free((caddr_t)rp, M_PCB); | |
71 | return (error); | |
72 | } | |
e831d540 KS |
73 | if (af == AF_INET) |
74 | route_cb.ip_count++; | |
75 | else if (af == AF_NS) | |
76 | route_cb.ns_count++; | |
e831d540 KS |
77 | else if (af == AF_ISO) |
78 | route_cb.iso_count++; | |
b72a6efb | 79 | rp->rcb_faddr = &route_src; |
e831d540 KS |
80 | route_cb.any_count++; |
81 | soisconnected(so); | |
2e5be88b | 82 | so->so_options |= SO_USELOOPBACK; |
e831d540 KS |
83 | } |
84 | return (error); | |
85 | } | |
86 | #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1))) | |
87 | ||
b72a6efb | 88 | /*ARGSUSED*/ |
e831d540 KS |
89 | route_output(m, so) |
90 | register struct mbuf *m; | |
91 | struct socket *so; | |
92 | { | |
93 | register struct rt_msghdr *rtm = 0; | |
94 | register struct rtentry *rt = 0; | |
a0d75f26 | 95 | struct rtentry *saved_nrt = 0; |
79e0ea5d KS |
96 | struct sockaddr *dst = 0, *gate = 0, *netmask = 0, *genmask = 0; |
97 | caddr_t cp, lim; | |
b72a6efb | 98 | int len, error = 0; |
e831d540 | 99 | |
a0d75f26 | 100 | #define senderr(e) { error = e; goto flush;} |
79e0ea5d KS |
101 | if (m == 0 || m->m_len < sizeof(long)) |
102 | return (ENOBUFS); | |
103 | if ((m = m_pullup(m, sizeof(long))) == 0) | |
32dfe9ab | 104 | return (ENOBUFS); |
e831d540 | 105 | if ((m->m_flags & M_PKTHDR) == 0) |
32dfe9ab | 106 | panic("route_output"); |
e831d540 KS |
107 | len = m->m_pkthdr.len; |
108 | rtm = mtod(m, struct rt_msghdr *); | |
109 | if (len < rtm->rtm_msglen) | |
a0d75f26 | 110 | senderr(EINVAL); |
e831d540 KS |
111 | R_Malloc(rtm, struct rt_msghdr *, len); |
112 | if (rtm == 0) | |
a0d75f26 | 113 | senderr(ENOBUFS); |
e831d540 | 114 | m_copydata(m, 0, len, (caddr_t)rtm); |
79e0ea5d | 115 | if (rtm->rtm_version != RTM_VERSION) |
a0d75f26 | 116 | senderr(EPROTONOSUPPORT); |
e831d540 | 117 | rtm->rtm_pid = u.u_procp->p_pid; |
79e0ea5d | 118 | lim = len + (caddr_t) rtm; |
e831d540 | 119 | cp = (caddr_t) (rtm + 1); |
79e0ea5d | 120 | if (rtm->rtm_addrs & RTA_DST) { |
e831d540 KS |
121 | dst = (struct sockaddr *)cp; |
122 | cp += ROUNDUP(dst->sa_len); | |
79e0ea5d KS |
123 | } else |
124 | senderr(EINVAL); | |
125 | if ((rtm->rtm_addrs & RTA_GATEWAY) && cp < lim) { | |
e831d540 KS |
126 | gate = (struct sockaddr *)cp; |
127 | cp += ROUNDUP(gate->sa_len); | |
128 | } | |
79e0ea5d | 129 | if ((rtm->rtm_addrs & RTA_NETMASK) && cp < lim) { |
e831d540 | 130 | netmask = (struct sockaddr *)cp; |
b72a6efb KS |
131 | if (*cp) |
132 | cp += ROUNDUP(netmask->sa_len); | |
133 | else | |
134 | cp += sizeof(long); | |
135 | ||
e831d540 | 136 | } |
79e0ea5d KS |
137 | if ((rtm->rtm_addrs & RTA_GENMASK) && cp < lim) { |
138 | genmask = (struct sockaddr *)cp; | |
e831d540 KS |
139 | } |
140 | switch (rtm->rtm_type) { | |
141 | case RTM_ADD: | |
79e0ea5d KS |
142 | if (gate == 0) |
143 | senderr(EINVAL); | |
e831d540 KS |
144 | error = rtrequest(RTM_ADD, dst, gate, netmask, |
145 | rtm->rtm_flags, &saved_nrt); | |
79e0ea5d KS |
146 | if (error == 0 && saved_nrt) { |
147 | rt_setmetrics(rtm->rtm_inits, | |
148 | &rtm->rtm_rmx, &saved_nrt->rt_rmx); | |
a0d75f26 | 149 | saved_nrt->rt_refcnt--; |
79e0ea5d | 150 | } |
e831d540 KS |
151 | break; |
152 | ||
153 | case RTM_DELETE: | |
154 | error = rtrequest(RTM_DELETE, dst, gate, netmask, | |
b72a6efb | 155 | rtm->rtm_flags, (struct rtentry **)0); |
e831d540 KS |
156 | break; |
157 | ||
158 | case RTM_GET: | |
159 | case RTM_CHANGE: | |
160 | case RTM_LOCK: | |
161 | rt = rtalloc1(dst, 0); | |
162 | if (rt == 0) | |
a0d75f26 | 163 | senderr(ESRCH); |
e831d540 KS |
164 | switch(rtm->rtm_type) { |
165 | struct sockaddr *outmask; | |
166 | ||
167 | case RTM_GET: | |
168 | netmask = rt_mask(rt); | |
79e0ea5d KS |
169 | len = sizeof(*rtm) + ROUNDUP(rt_key(rt)->sa_len); |
170 | rtm->rtm_addrs = RTA_DST; | |
171 | if (rt->rt_gateway) { | |
172 | len += ROUNDUP(rt->rt_gateway->sa_len); | |
173 | rtm->rtm_addrs |= RTA_GATEWAY; | |
174 | } | |
175 | if (netmask) { | |
b72a6efb | 176 | len += netmask->sa_len; |
79e0ea5d KS |
177 | rtm->rtm_addrs |= RTA_NETMASK; |
178 | } | |
e831d540 KS |
179 | if (len > rtm->rtm_msglen) { |
180 | struct rt_msghdr *new_rtm; | |
181 | R_Malloc(new_rtm, struct rt_msghdr *, len); | |
182 | if (new_rtm == 0) | |
a0d75f26 | 183 | senderr(ENOBUFS); |
e831d540 KS |
184 | Bcopy(rtm, new_rtm, rtm->rtm_msglen); |
185 | Free(rtm); rtm = new_rtm; | |
186 | gate = (struct sockaddr *) | |
187 | (ROUNDUP(rt->rt_gateway->sa_len) | |
188 | + (char *)dst); | |
189 | Bcopy(&rt->rt_gateway, gate, | |
190 | rt->rt_gateway->sa_len); | |
191 | rtm->rtm_flags = rt->rt_flags; | |
e831d540 KS |
192 | if (netmask) { |
193 | outmask = (struct sockaddr *) | |
194 | (ROUNDUP(netmask->sa_len)+(char *)gate); | |
195 | Bcopy(netmask, outmask, netmask->sa_len); | |
e831d540 KS |
196 | } |
197 | } | |
198 | break; | |
199 | ||
200 | case RTM_CHANGE: | |
79e0ea5d KS |
201 | if (gate == 0) |
202 | senderr(EINVAL); | |
e831d540 | 203 | if (gate->sa_len > (len = rt->rt_gateway->sa_len)) |
a0d75f26 KS |
204 | senderr(EDQUOT); |
205 | if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest) | |
206 | rt->rt_ifa->ifa_rtrequest(RTM_CHANGE, rt, gate); | |
e831d540 KS |
207 | Bcopy(gate, rt->rt_gateway, len); |
208 | rt->rt_gateway->sa_len = len; | |
209 | ||
79e0ea5d KS |
210 | rt_setmetrics(rtm->rtm_inits, |
211 | &rtm->rtm_rmx, &rt->rt_rmx); | |
e831d540 KS |
212 | /* |
213 | * Fall into | |
214 | */ | |
79e0ea5d KS |
215 | case RTM_LOCK: |
216 | rt->rt_rmx.rmx_locks |= | |
217 | (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks); | |
218 | rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits); | |
e831d540 | 219 | break; |
e831d540 KS |
220 | } |
221 | goto cleanup; | |
222 | ||
223 | default: | |
a0d75f26 | 224 | senderr(EOPNOTSUPP); |
e831d540 KS |
225 | } |
226 | ||
227 | flush: | |
228 | if (rtm) { | |
229 | if (error) | |
230 | rtm->rtm_errno = error; | |
231 | else | |
232 | rtm->rtm_flags |= RTF_DONE; | |
233 | } | |
234 | cleanup: | |
235 | if (rt) | |
236 | rtfree(rt); | |
2e5be88b KS |
237 | { |
238 | register struct rawcb *rp = 0; | |
239 | /* | |
240 | * Check to see if we don't want our own messages. | |
241 | */ | |
242 | if ((so->so_options & SO_USELOOPBACK) == 0) { | |
243 | if (route_cb.any_count <= 1) { | |
244 | if (rtm) | |
245 | Free(rtm); | |
246 | m_freem(m); | |
247 | return (error); | |
248 | } | |
249 | /* There is another listener, so construct message */ | |
250 | rp = sotorawcb(so); | |
251 | } | |
32dfe9ab KS |
252 | if (cp = (caddr_t)rtm) { |
253 | m_copyback(m, 0, len, cp); | |
254 | Free(rtm); | |
255 | } | |
2e5be88b KS |
256 | if (rp) |
257 | rp->rcb_proto.sp_family = 0; /* Avoid us */ | |
e831d540 | 258 | route_proto.sp_protocol = dst->sa_family; |
32dfe9ab | 259 | raw_input(m, &route_proto, &route_src, &route_dst); |
2e5be88b KS |
260 | if (rp) |
261 | rp->rcb_proto.sp_family = PF_ROUTE; | |
262 | } | |
e831d540 KS |
263 | return (error); |
264 | } | |
265 | ||
79e0ea5d KS |
266 | static rt_setmetrics(which, in, out) |
267 | u_long which; | |
268 | register struct rt_metrics *in, *out; | |
269 | { | |
270 | #define metric(f, e) if (which & (f)) out->e = in->e; | |
271 | metric(RTV_RPIPE, rmx_recvpipe); | |
272 | metric(RTV_SPIPE, rmx_sendpipe); | |
273 | metric(RTV_SSTHRESH, rmx_ssthresh); | |
274 | metric(RTV_RTT, rmx_rtt); | |
275 | metric(RTV_RTTVAR, rmx_rttvar); | |
276 | metric(RTV_HOPCOUNT, rmx_hopcount); | |
277 | metric(RTV_MTU, rmx_mtu); | |
278 | #undef metric | |
279 | } | |
280 | ||
e831d540 KS |
281 | /* |
282 | * Copy data from a buffer back into the indicated mbuf chain, | |
283 | * starting "off" bytes from the beginning, extending the mbuf | |
284 | * chain if necessary. | |
285 | */ | |
286 | m_copyback(m0, off, len, cp) | |
287 | struct mbuf *m0; | |
288 | register int off; | |
289 | register int len; | |
290 | caddr_t cp; | |
291 | ||
292 | { | |
293 | register int mlen; | |
294 | register struct mbuf *m = m0, *n; | |
295 | int totlen = 0; | |
296 | ||
297 | if (m0 == 0) | |
298 | return; | |
299 | while (off >= (mlen = m->m_len)) { | |
300 | off -= mlen; | |
301 | totlen += mlen; | |
302 | if (m->m_next == 0) { | |
303 | n = m_getclr(M_DONTWAIT, m->m_type); | |
304 | if (n == 0) | |
305 | goto out; | |
306 | n->m_len = min(MLEN, len + off); | |
307 | m->m_next = n; | |
308 | } | |
309 | m = m->m_next; | |
310 | } | |
311 | while (len > 0) { | |
312 | mlen = min (m->m_len - off, len); | |
b72a6efb | 313 | bcopy(cp, off + mtod(m, caddr_t), (unsigned)mlen); |
e831d540 KS |
314 | cp += mlen; |
315 | len -= mlen; | |
316 | mlen += off; | |
317 | off = 0; | |
318 | totlen += mlen; | |
319 | if (len == 0) | |
320 | break; | |
321 | if (m->m_next == 0) { | |
322 | n = m_get(M_DONTWAIT, m->m_type); | |
323 | if (n == 0) | |
324 | break; | |
325 | n->m_len = min(MLEN, len); | |
326 | m->m_next = n; | |
327 | } | |
328 | m = m->m_next; | |
329 | } | |
330 | out: if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen)) | |
331 | m->m_pkthdr.len = totlen; | |
332 | } | |
333 | ||
334 | /* | |
335 | * The miss message and losing message are very similar. | |
336 | */ | |
337 | ||
b72a6efb | 338 | rt_missmsg(type, dst, gate, mask, src, flags, error) |
e831d540 KS |
339 | register struct sockaddr *dst; |
340 | struct sockaddr *gate, *mask, *src; | |
341 | { | |
342 | register struct rt_msghdr *rtm; | |
343 | register struct mbuf *m; | |
344 | int dlen = ROUNDUP(dst->sa_len); | |
345 | int len = dlen + sizeof(*rtm); | |
e831d540 KS |
346 | |
347 | if (route_cb.any_count == 0) | |
348 | return; | |
349 | m = m_gethdr(M_DONTWAIT, MT_DATA); | |
350 | if (m == 0) | |
351 | return; | |
352 | m->m_pkthdr.len = m->m_len = min(len, MHLEN); | |
353 | m->m_pkthdr.rcvif = 0; | |
354 | rtm = mtod(m, struct rt_msghdr *); | |
355 | bzero((caddr_t)rtm, sizeof(*rtm)); /*XXX assumes sizeof(*rtm) < MHLEN*/ | |
356 | rtm->rtm_flags = RTF_DONE | flags; | |
357 | rtm->rtm_msglen = len; | |
79e0ea5d | 358 | rtm->rtm_version = RTM_VERSION; |
e831d540 | 359 | rtm->rtm_type = type; |
79e0ea5d | 360 | rtm->rtm_addrs = RTA_DST; |
e831d540 | 361 | if (type == RTM_OLDADD || type == RTM_OLDDEL) { |
e831d540 | 362 | rtm->rtm_pid = u.u_procp->p_pid; |
e831d540 KS |
363 | } |
364 | m_copyback(m, sizeof (*rtm), dlen, (caddr_t)dst); | |
365 | if (gate) { | |
366 | dlen = ROUNDUP(gate->sa_len); | |
367 | m_copyback(m, len , dlen, (caddr_t)gate); | |
368 | len += dlen; | |
79e0ea5d | 369 | rtm->rtm_addrs |= RTA_GATEWAY; |
e831d540 KS |
370 | } |
371 | if (mask) { | |
b72a6efb KS |
372 | if (mask->sa_len) |
373 | dlen = ROUNDUP(mask->sa_len); | |
374 | else | |
375 | dlen = sizeof(long); | |
e831d540 KS |
376 | m_copyback(m, len , dlen, (caddr_t)mask); |
377 | len += dlen; | |
79e0ea5d | 378 | rtm->rtm_addrs |= RTA_NETMASK; |
e831d540 KS |
379 | } |
380 | if (src) { | |
381 | dlen = ROUNDUP(src->sa_len); | |
382 | m_copyback(m, len , dlen, (caddr_t)src); | |
383 | len += dlen; | |
79e0ea5d | 384 | rtm->rtm_addrs |= RTA_AUTHOR; |
e831d540 KS |
385 | } |
386 | if (m->m_pkthdr.len != len) { | |
387 | m_freem(m); | |
388 | return; | |
389 | } | |
b72a6efb | 390 | rtm->rtm_errno = error; |
e831d540 KS |
391 | rtm->rtm_msglen = len; |
392 | route_proto.sp_protocol = dst->sa_family; | |
393 | raw_input(m, &route_proto, &route_src, &route_dst); | |
394 | } | |
395 | ||
a0d75f26 | 396 | #include "kinfo.h" |
a0d75f26 KS |
397 | struct walkarg { |
398 | int w_op, w_arg; | |
399 | int w_given, w_needed; | |
400 | caddr_t w_where; | |
401 | struct { | |
402 | struct rt_msghdr m_rtm; | |
403 | char m_sabuf[128]; | |
404 | } w_m; | |
405 | #define w_rtm w_m.m_rtm | |
406 | }; | |
407 | /* | |
408 | * This is used in dumping the kernel table via getkinfo(). | |
409 | */ | |
410 | rt_dumpentry(rn, w) | |
411 | struct radix_node *rn; | |
412 | register struct walkarg *w; | |
413 | { | |
414 | register struct sockaddr *sa; | |
415 | int n, error; | |
416 | ||
2e5be88b | 417 | for (; rn; rn = rn->rn_dupedkey) { |
a0d75f26 KS |
418 | int count = 0, size = sizeof(w->w_rtm); |
419 | register struct rtentry *rt = (struct rtentry *)rn; | |
420 | ||
2e5be88b KS |
421 | if (rn->rn_flags & RNF_ROOT) |
422 | continue; | |
a0d75f26 KS |
423 | if (w->w_op == KINFO_RT_FLAGS && !(rt->rt_flags & w->w_arg)) |
424 | continue; | |
79e0ea5d KS |
425 | #define next(a, l) {size += (l); w->w_rtm.rtm_addrs |= (a); } |
426 | w->w_rtm.rtm_addrs = 0; | |
a0d75f26 | 427 | if (sa = rt_key(rt)) |
79e0ea5d | 428 | next(RTA_DST, ROUNDUP(sa->sa_len)); |
a0d75f26 | 429 | if (sa = rt->rt_gateway) |
79e0ea5d KS |
430 | next(RTA_GATEWAY, ROUNDUP(sa->sa_len)); |
431 | if (sa = rt_mask(rt)) | |
432 | next(RTA_NETMASK, | |
433 | sa->sa_len ? ROUNDUP(sa->sa_len) : sizeof(long)); | |
a0d75f26 | 434 | if (sa = rt->rt_genmask) |
79e0ea5d | 435 | next(RTA_GENMASK, ROUNDUP(sa->sa_len)); |
a0d75f26 KS |
436 | w->w_needed += size; |
437 | if (w->w_where == NULL || w->w_needed > 0) | |
438 | continue; | |
439 | w->w_rtm.rtm_msglen = size; | |
a0d75f26 | 440 | w->w_rtm.rtm_flags = rt->rt_flags; |
79e0ea5d KS |
441 | w->w_rtm.rtm_use = rt->rt_use; |
442 | w->w_rtm.rtm_rmx = rt->rt_rmx; | |
443 | w->w_rtm.rtm_index = rt->rt_ifp->if_index; | |
444 | #undef next | |
445 | #define next(l) {n = (l); Bcopy(sa, cp, n); cp += n;} | |
a0d75f26 KS |
446 | if (size <= sizeof(w->w_m)) { |
447 | register caddr_t cp = (caddr_t)(w->w_m.m_sabuf); | |
a0d75f26 | 448 | if (sa = rt_key(rt)) |
79e0ea5d | 449 | next(ROUNDUP(sa->sa_len)); |
a0d75f26 | 450 | if (sa = rt->rt_gateway) |
79e0ea5d KS |
451 | next(ROUNDUP(sa->sa_len)); |
452 | if (sa = rt_mask(rt)) | |
453 | next(sa->sa_len ? ROUNDUP(sa->sa_len) : sizeof(long)); | |
a0d75f26 | 454 | if (sa = rt->rt_genmask) |
79e0ea5d | 455 | next(ROUNDUP(sa->sa_len)); |
a0d75f26 | 456 | #undef next |
79e0ea5d KS |
457 | #define next(s, l) {n = (l); \ |
458 | if (error = copyout((caddr_t)(s), w->w_where, n)) return (error); \ | |
459 | w->w_where += n;} | |
a0d75f26 KS |
460 | |
461 | next(&w->w_m, size); /* Copy rtmsg and sockaddrs back */ | |
462 | continue; | |
463 | } | |
464 | next(&w->w_rtm, sizeof(w->w_rtm)); | |
465 | if (sa = rt_key(rt)) | |
466 | next(sa, ROUNDUP(sa->sa_len)); | |
467 | if (sa = rt->rt_gateway) | |
468 | next(sa, ROUNDUP(sa->sa_len)); | |
79e0ea5d KS |
469 | if (sa = rt_mask(rt)) |
470 | next(sa, sa->sa_len ? ROUNDUP(sa->sa_len) : sizeof(long)); | |
a0d75f26 KS |
471 | if (sa = rt->rt_genmask) |
472 | next(sa, ROUNDUP(sa->sa_len)); | |
473 | } | |
474 | return (0); | |
475 | #undef next | |
476 | } | |
477 | ||
478 | kinfo_rtable(op, where, given, arg, needed) | |
479 | int op, arg; | |
480 | caddr_t where; | |
481 | int *given, *needed; | |
482 | { | |
483 | register struct radix_node_head *rnh; | |
484 | int s, error = 0; | |
485 | u_char af = ki_af(op); | |
486 | struct walkarg w; | |
487 | ||
488 | op &= 0xffff; | |
489 | if (op != KINFO_RT_DUMP && op != KINFO_RT_FLAGS) | |
490 | return (EINVAL); | |
491 | ||
492 | Bzero(&w, sizeof(w)); | |
493 | if ((w.w_where = where) && given) | |
494 | w.w_given = *given; | |
495 | w.w_needed = 0 - w.w_given; | |
496 | w.w_arg = arg; | |
497 | w.w_op = op; | |
79e0ea5d | 498 | w.w_rtm.rtm_version = RTM_VERSION; |
a0d75f26 KS |
499 | w.w_rtm.rtm_type = RTM_GET; |
500 | ||
501 | s = splnet(); | |
502 | for (rnh = radix_node_head; rnh; rnh = rnh->rnh_next) { | |
503 | if (rnh->rnh_af == 0) | |
504 | continue; | |
505 | if (af && af != rnh->rnh_af) | |
506 | continue; | |
507 | error = rt_walk(rnh->rnh_treetop, rt_dumpentry, &w); | |
508 | if (error) | |
509 | break; | |
510 | } | |
511 | w.w_needed += w.w_given; | |
512 | if (where && given) | |
513 | *given = w.w_where - where; | |
514 | else | |
515 | w.w_needed = (11 * w.w_needed) / 10; | |
516 | *needed = w.w_needed; | |
517 | splx(s); | |
518 | return (error); | |
519 | } | |
520 | ||
521 | rt_walk(rn, f, w) | |
522 | register struct radix_node *rn; | |
523 | register int (*f)(); | |
524 | struct walkarg *w; | |
525 | { | |
526 | int error; | |
527 | for (;;) { | |
528 | while (rn->rn_b >= 0) | |
529 | rn = rn->rn_l; /* First time through node, go left */ | |
530 | if (error = (*f)(rn, w)) | |
531 | return (error); /* Process Leaf */ | |
532 | while (rn->rn_p->rn_r == rn) { /* if coming back from right */ | |
533 | rn = rn->rn_p; /* go back up */ | |
534 | if (rn->rn_flags & RNF_ROOT) | |
535 | return 0; | |
536 | } | |
537 | rn = rn->rn_p->rn_r; /* otherwise, go right*/ | |
538 | } | |
539 | } | |
540 | ||
e831d540 KS |
541 | /* |
542 | * Definitions of protocols supported in the ROUTE domain. | |
543 | */ | |
544 | ||
e831d540 KS |
545 | int raw_init(),raw_usrreq(),raw_input(),raw_ctlinput(); |
546 | extern struct domain routedomain; /* or at least forward */ | |
547 | ||
548 | struct protosw routesw[] = { | |
549 | { SOCK_RAW, &routedomain, 0, PR_ATOMIC|PR_ADDR, | |
550 | raw_input, route_output, raw_ctlinput, 0, | |
551 | route_usrreq, | |
552 | raw_init, 0, 0, 0, | |
553 | }, | |
554 | { 0, 0, 0, 0, | |
555 | raw_input, 0, raw_ctlinput, 0, | |
556 | raw_usrreq, | |
557 | raw_init, 0, 0, 0, | |
558 | } | |
559 | }; | |
560 | ||
561 | int unp_externalize(), unp_dispose(); | |
562 | ||
563 | struct domain routedomain = | |
564 | { PF_ROUTE, "route", 0, 0, 0, | |
565 | routesw, &routesw[sizeof(routesw)/sizeof(routesw[0])] }; |