Commit | Line | Data |
---|---|---|
15637ed4 RG |
1 | /* |
2 | * Copyright (c) 1988, 1991 Regents of the University of California. | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * 2. Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in the | |
12 | * documentation and/or other materials provided with the distribution. | |
13 | * 3. All advertising materials mentioning features or use of this software | |
14 | * must display the following acknowledgement: | |
15 | * This product includes software developed by the University of | |
16 | * California, Berkeley and its contributors. | |
17 | * 4. Neither the name of the University nor the names of its contributors | |
18 | * may be used to endorse or promote products derived from this software | |
19 | * without specific prior written permission. | |
20 | * | |
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
31 | * SUCH DAMAGE. | |
32 | * | |
78ed81a3 | 33 | * from: @(#)rtsock.c 7.18 (Berkeley) 6/27/91 |
34 | * $Id$ | |
15637ed4 RG |
35 | */ |
36 | ||
37 | #include "param.h" | |
78ed81a3 | 38 | #include "systm.h" |
15637ed4 RG |
39 | #include "mbuf.h" |
40 | #include "proc.h" | |
41 | #include "socket.h" | |
42 | #include "socketvar.h" | |
43 | #include "domain.h" | |
44 | #include "protosw.h" | |
45 | ||
46 | #include "af.h" | |
47 | #include "if.h" | |
48 | #include "route.h" | |
49 | #include "raw_cb.h" | |
50 | ||
51 | #include "machine/mtpr.h" | |
52 | ||
53 | struct sockaddr route_dst = { 2, PF_ROUTE, }; | |
54 | struct sockaddr route_src = { 2, PF_ROUTE, }; | |
55 | struct sockproto route_proto = { PF_ROUTE, }; | |
56 | ||
57 | /*ARGSUSED*/ | |
58 | route_usrreq(so, req, m, nam, control) | |
59 | register struct socket *so; | |
60 | int req; | |
61 | struct mbuf *m, *nam, *control; | |
62 | { | |
63 | register int error = 0; | |
64 | register struct rawcb *rp = sotorawcb(so); | |
65 | int s; | |
66 | if (req == PRU_ATTACH) { | |
67 | MALLOC(rp, struct rawcb *, sizeof(*rp), M_PCB, M_WAITOK); | |
68 | if (so->so_pcb = (caddr_t)rp) | |
69 | bzero(so->so_pcb, sizeof(*rp)); | |
70 | ||
71 | } | |
72 | if (req == PRU_DETACH && rp) { | |
73 | int af = rp->rcb_proto.sp_protocol; | |
74 | if (af == AF_INET) | |
75 | route_cb.ip_count--; | |
76 | else if (af == AF_NS) | |
77 | route_cb.ns_count--; | |
78 | else if (af == AF_ISO) | |
79 | route_cb.iso_count--; | |
80 | route_cb.any_count--; | |
81 | } | |
82 | s = splnet(); | |
83 | error = raw_usrreq(so, req, m, nam, control); | |
84 | rp = sotorawcb(so); | |
85 | if (req == PRU_ATTACH && rp) { | |
86 | int af = rp->rcb_proto.sp_protocol; | |
87 | if (error) { | |
88 | free((caddr_t)rp, M_PCB); | |
89 | splx(s); | |
90 | return (error); | |
91 | } | |
92 | if (af == AF_INET) | |
93 | route_cb.ip_count++; | |
94 | else if (af == AF_NS) | |
95 | route_cb.ns_count++; | |
96 | else if (af == AF_ISO) | |
97 | route_cb.iso_count++; | |
98 | rp->rcb_faddr = &route_src; | |
99 | route_cb.any_count++; | |
100 | soisconnected(so); | |
101 | so->so_options |= SO_USELOOPBACK; | |
102 | } | |
103 | splx(s); | |
104 | return (error); | |
105 | } | |
106 | #define ROUNDUP(a) \ | |
107 | ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) | |
108 | #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len)) | |
109 | ||
110 | /*ARGSUSED*/ | |
111 | route_output(m, so) | |
112 | register struct mbuf *m; | |
113 | struct socket *so; | |
114 | { | |
115 | register struct rt_msghdr *rtm = 0; | |
116 | register struct rtentry *rt = 0; | |
117 | struct rtentry *saved_nrt = 0; | |
118 | struct sockaddr *dst = 0, *gate = 0, *netmask = 0, *genmask = 0; | |
119 | struct sockaddr *ifpaddr = 0, *ifaaddr = 0; | |
120 | caddr_t cp, lim; | |
121 | int len, error = 0; | |
122 | struct ifnet *ifp = 0; | |
123 | struct ifaddr *ifa = 0; | |
124 | struct ifaddr *ifaof_ifpforaddr(), *ifa_ifwithroute(); | |
125 | ||
126 | #define senderr(e) { error = e; goto flush;} | |
127 | if (m == 0 || m->m_len < sizeof(long)) | |
128 | return (ENOBUFS); | |
129 | if ((m = m_pullup(m, sizeof(long))) == 0) | |
130 | return (ENOBUFS); | |
131 | if ((m->m_flags & M_PKTHDR) == 0) | |
132 | panic("route_output"); | |
133 | len = m->m_pkthdr.len; | |
134 | if (len < sizeof(*rtm) || | |
135 | len != mtod(m, struct rt_msghdr *)->rtm_msglen) | |
136 | senderr(EINVAL); | |
137 | R_Malloc(rtm, struct rt_msghdr *, len); | |
138 | if (rtm == 0) | |
139 | senderr(ENOBUFS); | |
140 | m_copydata(m, 0, len, (caddr_t)rtm); | |
141 | if (rtm->rtm_version != RTM_VERSION) | |
142 | senderr(EPROTONOSUPPORT); | |
143 | rtm->rtm_pid = curproc->p_pid; | |
144 | lim = len + (caddr_t) rtm; | |
145 | cp = (caddr_t) (rtm + 1); | |
146 | if (rtm->rtm_addrs & RTA_DST) { | |
147 | dst = (struct sockaddr *)cp; | |
148 | ADVANCE(cp, dst); | |
149 | } else | |
150 | senderr(EINVAL); | |
151 | if ((rtm->rtm_addrs & RTA_GATEWAY) && cp < lim) { | |
152 | gate = (struct sockaddr *)cp; | |
153 | ADVANCE(cp, gate); | |
154 | } | |
155 | if ((rtm->rtm_addrs & RTA_NETMASK) && cp < lim) { | |
156 | netmask = (struct sockaddr *)cp; | |
157 | ADVANCE(cp, netmask); | |
158 | } | |
159 | if ((rtm->rtm_addrs & RTA_GENMASK) && cp < lim) { | |
160 | struct radix_node *t, *rn_addmask(); | |
161 | genmask = (struct sockaddr *)cp; | |
162 | ADVANCE(cp, genmask); | |
163 | t = rn_addmask(genmask, 1, 2); | |
164 | if (t && Bcmp(genmask, t->rn_key, *(u_char *)genmask) == 0) | |
165 | genmask = (struct sockaddr *)(t->rn_key); | |
166 | else | |
167 | senderr(ENOBUFS); | |
168 | } | |
169 | if ((rtm->rtm_addrs & RTA_IFP) && cp < lim) { | |
170 | ifpaddr = (struct sockaddr *)cp; | |
171 | ADVANCE(cp, ifpaddr); | |
172 | } | |
173 | if ((rtm->rtm_addrs & RTA_IFA) && cp < lim) { | |
174 | ifaaddr = (struct sockaddr *)cp; | |
175 | } | |
176 | switch (rtm->rtm_type) { | |
177 | case RTM_ADD: | |
178 | if (gate == 0) | |
179 | senderr(EINVAL); | |
180 | error = rtrequest(RTM_ADD, dst, gate, netmask, | |
181 | rtm->rtm_flags, &saved_nrt); | |
182 | if (error == 0 && saved_nrt) { | |
183 | rt_setmetrics(rtm->rtm_inits, | |
184 | &rtm->rtm_rmx, &saved_nrt->rt_rmx); | |
185 | saved_nrt->rt_refcnt--; | |
186 | saved_nrt->rt_genmask = genmask; | |
187 | } | |
188 | break; | |
189 | ||
190 | case RTM_DELETE: | |
191 | error = rtrequest(RTM_DELETE, dst, gate, netmask, | |
192 | rtm->rtm_flags, (struct rtentry **)0); | |
193 | break; | |
194 | ||
195 | case RTM_GET: | |
196 | case RTM_CHANGE: | |
197 | case RTM_LOCK: | |
198 | rt = rtalloc1(dst, 0); | |
199 | if (rt == 0) | |
200 | senderr(ESRCH); | |
201 | if (rtm->rtm_type != RTM_GET) { | |
202 | if (Bcmp(dst, rt_key(rt), dst->sa_len) != 0) | |
203 | senderr(ESRCH); | |
204 | if (rt->rt_nodes->rn_dupedkey && | |
205 | (netmask == 0 || | |
206 | Bcmp(netmask, rt_mask(rt), netmask->sa_len))) | |
207 | senderr(ETOOMANYREFS); | |
208 | } | |
209 | switch(rtm->rtm_type) { | |
210 | ||
211 | case RTM_GET: | |
212 | dst = rt_key(rt); len = sizeof(*rtm); | |
213 | ADVANCE(len, dst); | |
214 | rtm->rtm_addrs |= RTA_DST; | |
215 | if (gate = rt->rt_gateway) { | |
216 | ADVANCE(len, gate); | |
217 | rtm->rtm_addrs |= RTA_GATEWAY; | |
218 | } else | |
219 | rtm->rtm_addrs &= ~RTA_GATEWAY; | |
220 | if (netmask = rt_mask(rt)) { | |
221 | ADVANCE(len, netmask); | |
222 | rtm->rtm_addrs |= RTA_NETMASK; | |
223 | } else | |
224 | rtm->rtm_addrs &= ~RTA_NETMASK; | |
225 | if (genmask = rt->rt_genmask) { | |
226 | ADVANCE(len, genmask); | |
227 | rtm->rtm_addrs |= RTA_GENMASK; | |
228 | } else | |
229 | rtm->rtm_addrs &= ~RTA_GENMASK; | |
230 | if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) { | |
231 | if (rt->rt_ifp == 0) | |
232 | goto badif; | |
233 | for (ifa = rt->rt_ifp->if_addrlist; | |
234 | ifa && ifa->ifa_addr->sa_family != AF_LINK; | |
235 | ifa = ifa->ifa_next){} | |
236 | if (ifa && rt->rt_ifa) { | |
237 | ifpaddr = ifa->ifa_addr; | |
238 | ADVANCE(len, ifpaddr); | |
239 | ifaaddr = rt->rt_ifa->ifa_addr; | |
240 | ADVANCE(len, ifaaddr); | |
241 | rtm->rtm_addrs |= RTA_IFP | RTA_IFA; | |
242 | } else { | |
243 | badif: ifpaddr = 0; | |
244 | rtm->rtm_addrs &= ~(RTA_IFP | RTA_IFA); | |
245 | } | |
246 | } | |
247 | if (len > rtm->rtm_msglen) { | |
248 | struct rt_msghdr *new_rtm; | |
249 | R_Malloc(new_rtm, struct rt_msghdr *, len); | |
250 | if (new_rtm == 0) | |
251 | senderr(ENOBUFS); | |
252 | Bcopy(rtm, new_rtm, rtm->rtm_msglen); | |
253 | Free(rtm); rtm = new_rtm; | |
254 | } | |
255 | rtm->rtm_msglen = len; | |
256 | rtm->rtm_flags = rt->rt_flags; | |
257 | rtm->rtm_rmx = rt->rt_rmx; | |
258 | cp = (caddr_t) (1 + rtm); | |
259 | len = ROUNDUP(dst->sa_len); | |
260 | Bcopy(dst, cp, len); cp += len; | |
261 | if (gate) { | |
262 | len = ROUNDUP(gate->sa_len); | |
263 | Bcopy(gate, cp, len); cp += len; | |
264 | } | |
265 | if (netmask) { | |
266 | len = ROUNDUP(netmask->sa_len); | |
267 | Bcopy(netmask, cp, len); cp += len; | |
268 | } | |
269 | if (genmask) { | |
270 | len = ROUNDUP(genmask->sa_len); | |
271 | Bcopy(genmask, cp, len); cp += len; | |
272 | } | |
273 | if (ifpaddr) { | |
274 | len = ROUNDUP(ifpaddr->sa_len); | |
275 | Bcopy(ifpaddr, cp, len); cp += len; | |
276 | len = ROUNDUP(ifaaddr->sa_len); | |
277 | Bcopy(ifaaddr, cp, len); cp += len; | |
278 | } | |
279 | break; | |
280 | ||
281 | case RTM_CHANGE: | |
282 | if (gate && | |
283 | (gate->sa_len > (len = rt->rt_gateway->sa_len))) | |
284 | senderr(EDQUOT); | |
285 | /* new gateway could require new ifaddr, ifp; | |
286 | flags may also be different; ifp may be specified | |
287 | by ll sockaddr when protocol address is ambiguous */ | |
288 | if (ifpaddr && (ifa = ifa_ifwithnet(ifpaddr)) && | |
289 | (ifp = ifa->ifa_ifp)) | |
290 | ifa = ifaof_ifpforaddr(ifaaddr ? ifaaddr : gate, | |
291 | ifp); | |
292 | else if ((ifaaddr && (ifa = ifa_ifwithaddr(ifaaddr))) || | |
293 | (ifa = ifa_ifwithroute(rt->rt_flags, | |
294 | rt_key(rt), gate))) | |
295 | ifp = ifa->ifa_ifp; | |
296 | if (ifa) { | |
297 | register struct ifaddr *oifa = rt->rt_ifa; | |
298 | if (oifa != ifa) { | |
299 | if (oifa && oifa->ifa_rtrequest) | |
300 | oifa->ifa_rtrequest(RTM_DELETE, | |
301 | rt, gate); | |
302 | rt->rt_ifa = ifa; | |
303 | rt->rt_ifp = ifp; | |
304 | } | |
305 | } | |
306 | if (gate) | |
307 | Bcopy(gate, rt->rt_gateway, len); | |
308 | rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx, | |
309 | &rt->rt_rmx); | |
310 | if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest) | |
311 | rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, gate); | |
312 | if (genmask) | |
313 | rt->rt_genmask = genmask; | |
314 | /* | |
315 | * Fall into | |
316 | */ | |
317 | case RTM_LOCK: | |
318 | rt->rt_rmx.rmx_locks |= | |
319 | (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks); | |
320 | rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits); | |
321 | break; | |
322 | } | |
323 | goto cleanup; | |
324 | ||
325 | default: | |
326 | senderr(EOPNOTSUPP); | |
327 | } | |
328 | ||
329 | flush: | |
330 | if (rtm) { | |
331 | if (error) | |
332 | rtm->rtm_errno = error; | |
333 | else | |
334 | rtm->rtm_flags |= RTF_DONE; | |
335 | } | |
336 | cleanup: | |
337 | if (rt) | |
338 | rtfree(rt); | |
339 | { | |
340 | register struct rawcb *rp = 0; | |
341 | /* | |
342 | * Check to see if we don't want our own messages. | |
343 | */ | |
344 | if ((so->so_options & SO_USELOOPBACK) == 0) { | |
345 | if (route_cb.any_count <= 1) { | |
346 | if (rtm) | |
347 | Free(rtm); | |
348 | m_freem(m); | |
349 | return (error); | |
350 | } | |
351 | /* There is another listener, so construct message */ | |
352 | rp = sotorawcb(so); | |
353 | } | |
354 | if (rtm) { | |
355 | m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm); | |
356 | Free(rtm); | |
357 | } | |
358 | if (rp) | |
359 | rp->rcb_proto.sp_family = 0; /* Avoid us */ | |
360 | if (dst) | |
361 | route_proto.sp_protocol = dst->sa_family; | |
362 | raw_input(m, &route_proto, &route_src, &route_dst); | |
363 | if (rp) | |
364 | rp->rcb_proto.sp_family = PF_ROUTE; | |
365 | } | |
366 | return (error); | |
367 | } | |
368 | ||
369 | rt_setmetrics(which, in, out) | |
370 | u_long which; | |
371 | register struct rt_metrics *in, *out; | |
372 | { | |
373 | #define metric(f, e) if (which & (f)) out->e = in->e; | |
374 | metric(RTV_RPIPE, rmx_recvpipe); | |
375 | metric(RTV_SPIPE, rmx_sendpipe); | |
376 | metric(RTV_SSTHRESH, rmx_ssthresh); | |
377 | metric(RTV_RTT, rmx_rtt); | |
378 | metric(RTV_RTTVAR, rmx_rttvar); | |
379 | metric(RTV_HOPCOUNT, rmx_hopcount); | |
380 | metric(RTV_MTU, rmx_mtu); | |
381 | metric(RTV_EXPIRE, rmx_expire); | |
382 | #undef metric | |
383 | } | |
384 | ||
385 | /* | |
386 | * Copy data from a buffer back into the indicated mbuf chain, | |
387 | * starting "off" bytes from the beginning, extending the mbuf | |
388 | * chain if necessary. | |
389 | */ | |
390 | m_copyback(m0, off, len, cp) | |
391 | struct mbuf *m0; | |
392 | register int off; | |
393 | register int len; | |
394 | caddr_t cp; | |
395 | ||
396 | { | |
397 | register int mlen; | |
398 | register struct mbuf *m = m0, *n; | |
399 | int totlen = 0; | |
400 | ||
401 | if (m0 == 0) | |
402 | return; | |
403 | while (off > (mlen = m->m_len)) { | |
404 | off -= mlen; | |
405 | totlen += mlen; | |
406 | if (m->m_next == 0) { | |
407 | n = m_getclr(M_DONTWAIT, m->m_type); | |
408 | if (n == 0) | |
409 | goto out; | |
410 | n->m_len = min(MLEN, len + off); | |
411 | m->m_next = n; | |
412 | } | |
413 | m = m->m_next; | |
414 | } | |
415 | while (len > 0) { | |
416 | mlen = min (m->m_len - off, len); | |
417 | bcopy(cp, off + mtod(m, caddr_t), (unsigned)mlen); | |
418 | cp += mlen; | |
419 | len -= mlen; | |
420 | mlen += off; | |
421 | off = 0; | |
422 | totlen += mlen; | |
423 | if (len == 0) | |
424 | break; | |
425 | if (m->m_next == 0) { | |
426 | n = m_get(M_DONTWAIT, m->m_type); | |
427 | if (n == 0) | |
428 | break; | |
429 | n->m_len = min(MLEN, len); | |
430 | m->m_next = n; | |
431 | } | |
432 | m = m->m_next; | |
433 | } | |
434 | out: if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen)) | |
435 | m->m_pkthdr.len = totlen; | |
436 | } | |
437 | ||
438 | /* | |
439 | * The miss message and losing message are very similar. | |
440 | */ | |
441 | ||
442 | rt_missmsg(type, dst, gate, mask, src, flags, error) | |
443 | register struct sockaddr *dst; | |
444 | struct sockaddr *gate, *mask, *src; | |
445 | { | |
446 | register struct rt_msghdr *rtm; | |
447 | register struct mbuf *m; | |
448 | int dlen = ROUNDUP(dst->sa_len); | |
449 | int len = dlen + sizeof(*rtm); | |
450 | ||
451 | if (route_cb.any_count == 0) | |
452 | return; | |
453 | m = m_gethdr(M_DONTWAIT, MT_DATA); | |
454 | if (m == 0) | |
455 | return; | |
456 | m->m_pkthdr.len = m->m_len = min(len, MHLEN); | |
457 | m->m_pkthdr.rcvif = 0; | |
458 | rtm = mtod(m, struct rt_msghdr *); | |
459 | bzero((caddr_t)rtm, sizeof(*rtm)); /*XXX assumes sizeof(*rtm) < MHLEN*/ | |
460 | rtm->rtm_flags = RTF_DONE | flags; | |
461 | rtm->rtm_msglen = len; | |
462 | rtm->rtm_version = RTM_VERSION; | |
463 | rtm->rtm_type = type; | |
464 | rtm->rtm_addrs = RTA_DST; | |
465 | if (type == RTM_OLDADD || type == RTM_OLDDEL) { | |
466 | rtm->rtm_pid = curproc->p_pid; | |
467 | } | |
468 | m_copyback(m, sizeof (*rtm), dlen, (caddr_t)dst); | |
469 | if (gate) { | |
470 | dlen = ROUNDUP(gate->sa_len); | |
471 | m_copyback(m, len , dlen, (caddr_t)gate); | |
472 | len += dlen; | |
473 | rtm->rtm_addrs |= RTA_GATEWAY; | |
474 | } | |
475 | if (mask) { | |
476 | dlen = ROUNDUP(mask->sa_len); | |
477 | m_copyback(m, len , dlen, (caddr_t)mask); | |
478 | len += dlen; | |
479 | rtm->rtm_addrs |= RTA_NETMASK; | |
480 | } | |
481 | if (src) { | |
482 | dlen = ROUNDUP(src->sa_len); | |
483 | m_copyback(m, len , dlen, (caddr_t)src); | |
484 | len += dlen; | |
485 | rtm->rtm_addrs |= RTA_AUTHOR; | |
486 | } | |
487 | if (m->m_pkthdr.len != len) { | |
488 | m_freem(m); | |
489 | return; | |
490 | } | |
491 | rtm->rtm_errno = error; | |
492 | rtm->rtm_msglen = len; | |
493 | route_proto.sp_protocol = dst->sa_family; | |
494 | raw_input(m, &route_proto, &route_src, &route_dst); | |
495 | } | |
496 | ||
497 | #include "kinfo.h" | |
498 | struct walkarg { | |
499 | int w_op, w_arg; | |
500 | int w_given, w_needed; | |
501 | caddr_t w_where; | |
502 | struct { | |
503 | struct rt_msghdr m_rtm; | |
504 | char m_sabuf[128]; | |
505 | } w_m; | |
506 | #define w_rtm w_m.m_rtm | |
507 | }; | |
508 | /* | |
509 | * This is used in dumping the kernel table via getkinfo(). | |
510 | */ | |
511 | rt_dumpentry(rn, w) | |
512 | struct radix_node *rn; | |
513 | register struct walkarg *w; | |
514 | { | |
515 | register struct sockaddr *sa; | |
516 | int n, error; | |
517 | ||
518 | for (; rn; rn = rn->rn_dupedkey) { | |
519 | int count = 0, size = sizeof(w->w_rtm); | |
520 | register struct rtentry *rt = (struct rtentry *)rn; | |
521 | ||
522 | if (rn->rn_flags & RNF_ROOT) | |
523 | continue; | |
524 | if (w->w_op == KINFO_RT_FLAGS && !(rt->rt_flags & w->w_arg)) | |
525 | continue; | |
526 | #define next(a, l) {size += (l); w->w_rtm.rtm_addrs |= (a); } | |
527 | w->w_rtm.rtm_addrs = 0; | |
528 | if (sa = rt_key(rt)) | |
529 | next(RTA_DST, ROUNDUP(sa->sa_len)); | |
530 | if (sa = rt->rt_gateway) | |
531 | next(RTA_GATEWAY, ROUNDUP(sa->sa_len)); | |
532 | if (sa = rt_mask(rt)) | |
533 | next(RTA_NETMASK, ROUNDUP(sa->sa_len)); | |
534 | if (sa = rt->rt_genmask) | |
535 | next(RTA_GENMASK, ROUNDUP(sa->sa_len)); | |
536 | w->w_needed += size; | |
537 | if (w->w_where == NULL || w->w_needed > 0) | |
538 | continue; | |
539 | w->w_rtm.rtm_msglen = size; | |
540 | w->w_rtm.rtm_flags = rt->rt_flags; | |
541 | w->w_rtm.rtm_use = rt->rt_use; | |
542 | w->w_rtm.rtm_rmx = rt->rt_rmx; | |
543 | w->w_rtm.rtm_index = rt->rt_ifp->if_index; | |
544 | #undef next | |
545 | #define next(l) {n = (l); Bcopy(sa, cp, n); cp += n;} | |
546 | if (size <= sizeof(w->w_m)) { | |
547 | register caddr_t cp = (caddr_t)(w->w_m.m_sabuf); | |
548 | if (sa = rt_key(rt)) | |
549 | next(ROUNDUP(sa->sa_len)); | |
550 | if (sa = rt->rt_gateway) | |
551 | next(ROUNDUP(sa->sa_len)); | |
552 | if (sa = rt_mask(rt)) | |
553 | next(ROUNDUP(sa->sa_len)); | |
554 | if (sa = rt->rt_genmask) | |
555 | next(ROUNDUP(sa->sa_len)); | |
556 | #undef next | |
557 | #define next(s, l) {n = (l); \ | |
558 | if (error = copyout((caddr_t)(s), w->w_where, n)) return (error); \ | |
559 | w->w_where += n;} | |
560 | ||
561 | next(&w->w_m, size); /* Copy rtmsg and sockaddrs back */ | |
562 | continue; | |
563 | } | |
564 | next(&w->w_rtm, sizeof(w->w_rtm)); | |
565 | if (sa = rt_key(rt)) | |
566 | next(sa, ROUNDUP(sa->sa_len)); | |
567 | if (sa = rt->rt_gateway) | |
568 | next(sa, ROUNDUP(sa->sa_len)); | |
569 | if (sa = rt_mask(rt)) | |
570 | next(sa, ROUNDUP(sa->sa_len)); | |
571 | if (sa = rt->rt_genmask) | |
572 | next(sa, ROUNDUP(sa->sa_len)); | |
573 | } | |
574 | return (0); | |
575 | #undef next | |
576 | } | |
577 | ||
578 | kinfo_rtable(op, where, given, arg, needed) | |
579 | int op, arg; | |
580 | caddr_t where; | |
581 | int *given, *needed; | |
582 | { | |
583 | register struct radix_node_head *rnh; | |
584 | int s, error = 0; | |
585 | u_char af = ki_af(op); | |
586 | struct walkarg w; | |
587 | ||
588 | op &= 0xffff; | |
589 | if (op != KINFO_RT_DUMP && op != KINFO_RT_FLAGS) | |
590 | return (EINVAL); | |
591 | ||
592 | Bzero(&w, sizeof(w)); | |
593 | if ((w.w_where = where) && given) | |
594 | w.w_given = *given; | |
595 | w.w_needed = 0 - w.w_given; | |
596 | w.w_arg = arg; | |
597 | w.w_op = op; | |
598 | w.w_rtm.rtm_version = RTM_VERSION; | |
599 | w.w_rtm.rtm_type = RTM_GET; | |
600 | ||
601 | s = splnet(); | |
602 | for (rnh = radix_node_head; rnh; rnh = rnh->rnh_next) { | |
603 | if (rnh->rnh_af == 0) | |
604 | continue; | |
605 | if (af && af != rnh->rnh_af) | |
606 | continue; | |
607 | error = rt_walk(rnh->rnh_treetop, rt_dumpentry, &w); | |
608 | if (error) | |
609 | break; | |
610 | } | |
611 | w.w_needed += w.w_given; | |
612 | if (where && given) | |
613 | *given = w.w_where - where; | |
614 | else | |
615 | w.w_needed = (11 * w.w_needed) / 10; | |
616 | *needed = w.w_needed; | |
617 | splx(s); | |
618 | return (error); | |
619 | } | |
620 | ||
621 | rt_walk(rn, f, w) | |
622 | register struct radix_node *rn; | |
623 | register int (*f)(); | |
624 | struct walkarg *w; | |
625 | { | |
626 | int error; | |
627 | for (;;) { | |
628 | while (rn->rn_b >= 0) | |
629 | rn = rn->rn_l; /* First time through node, go left */ | |
630 | if (error = (*f)(rn, w)) | |
631 | return (error); /* Process Leaf */ | |
632 | while (rn->rn_p->rn_r == rn) { /* if coming back from right */ | |
633 | rn = rn->rn_p; /* go back up */ | |
634 | if (rn->rn_flags & RNF_ROOT) | |
635 | return 0; | |
636 | } | |
637 | rn = rn->rn_p->rn_r; /* otherwise, go right*/ | |
638 | } | |
639 | } | |
640 | ||
641 | /* | |
642 | * Definitions of protocols supported in the ROUTE domain. | |
643 | */ | |
644 | ||
645 | int raw_init(),raw_usrreq(),raw_input(),raw_ctlinput(); | |
646 | extern struct domain routedomain; /* or at least forward */ | |
647 | ||
648 | struct protosw routesw[] = { | |
649 | { SOCK_RAW, &routedomain, 0, PR_ATOMIC|PR_ADDR, | |
650 | raw_input, route_output, raw_ctlinput, 0, | |
651 | route_usrreq, | |
652 | raw_init, 0, 0, 0, | |
653 | } | |
654 | }; | |
655 | ||
656 | int unp_externalize(), unp_dispose(); | |
657 | ||
658 | struct domain routedomain = | |
659 | { PF_ROUTE, "route", 0, 0, 0, | |
660 | routesw, &routesw[sizeof(routesw)/sizeof(routesw[0])] }; |