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 | * | |
4e9d5936 | 17 | * @(#)rtsock.c 7.11 (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 | { | |
d6bc9b28 | 93 | register struct rt_msghdr *rtm; |
e831d540 | 94 | register struct rtentry *rt = 0; |
a0d75f26 | 95 | struct rtentry *saved_nrt = 0; |
79e0ea5d | 96 | struct sockaddr *dst = 0, *gate = 0, *netmask = 0, *genmask = 0; |
d6bc9b28 | 97 | struct sockaddr *ifpaddr = 0; |
79e0ea5d | 98 | caddr_t cp, lim; |
b72a6efb | 99 | int len, error = 0; |
d6bc9b28 KS |
100 | struct ifnet *ifp = 0; |
101 | struct ifaddr *ifa; | |
102 | extern struct ifaddr *ifaof_ifpforaddr(), *ifa_ifwithroute(); | |
e831d540 | 103 | |
a0d75f26 | 104 | #define senderr(e) { error = e; goto flush;} |
79e0ea5d KS |
105 | if (m == 0 || m->m_len < sizeof(long)) |
106 | return (ENOBUFS); | |
107 | if ((m = m_pullup(m, sizeof(long))) == 0) | |
32dfe9ab | 108 | return (ENOBUFS); |
e831d540 | 109 | if ((m->m_flags & M_PKTHDR) == 0) |
32dfe9ab | 110 | panic("route_output"); |
e831d540 KS |
111 | len = m->m_pkthdr.len; |
112 | rtm = mtod(m, struct rt_msghdr *); | |
113 | if (len < rtm->rtm_msglen) | |
a0d75f26 | 114 | senderr(EINVAL); |
e831d540 KS |
115 | R_Malloc(rtm, struct rt_msghdr *, len); |
116 | if (rtm == 0) | |
a0d75f26 | 117 | senderr(ENOBUFS); |
e831d540 | 118 | m_copydata(m, 0, len, (caddr_t)rtm); |
79e0ea5d | 119 | if (rtm->rtm_version != RTM_VERSION) |
a0d75f26 | 120 | senderr(EPROTONOSUPPORT); |
e831d540 | 121 | rtm->rtm_pid = u.u_procp->p_pid; |
79e0ea5d | 122 | lim = len + (caddr_t) rtm; |
e831d540 | 123 | cp = (caddr_t) (rtm + 1); |
79e0ea5d | 124 | if (rtm->rtm_addrs & RTA_DST) { |
e831d540 KS |
125 | dst = (struct sockaddr *)cp; |
126 | cp += ROUNDUP(dst->sa_len); | |
79e0ea5d KS |
127 | } else |
128 | senderr(EINVAL); | |
129 | if ((rtm->rtm_addrs & RTA_GATEWAY) && cp < lim) { | |
e831d540 KS |
130 | gate = (struct sockaddr *)cp; |
131 | cp += ROUNDUP(gate->sa_len); | |
132 | } | |
79e0ea5d | 133 | if ((rtm->rtm_addrs & RTA_NETMASK) && cp < lim) { |
e831d540 | 134 | netmask = (struct sockaddr *)cp; |
b72a6efb KS |
135 | if (*cp) |
136 | cp += ROUNDUP(netmask->sa_len); | |
137 | else | |
138 | cp += sizeof(long); | |
139 | ||
e831d540 | 140 | } |
79e0ea5d | 141 | if ((rtm->rtm_addrs & RTA_GENMASK) && cp < lim) { |
d6bc9b28 | 142 | struct radix_node *t, *rn_addmask(); |
79e0ea5d | 143 | genmask = (struct sockaddr *)cp; |
d6bc9b28 KS |
144 | if (*cp) |
145 | cp += ROUNDUP(netmask->sa_len); | |
146 | else | |
147 | cp += sizeof(long); | |
148 | t = rn_addmask(genmask, 1, 2); | |
149 | if (t && Bcmp(genmask, t->rn_key, *(u_char *)genmask) == 0) | |
150 | genmask = (struct sockaddr *)(t->rn_key); | |
151 | else | |
152 | senderr(ENOBUFS); | |
153 | } | |
154 | if ((rtm->rtm_addrs & RTA_IFP) && cp < lim) { | |
155 | ifpaddr = (struct sockaddr *)cp; | |
e831d540 KS |
156 | } |
157 | switch (rtm->rtm_type) { | |
158 | case RTM_ADD: | |
79e0ea5d KS |
159 | if (gate == 0) |
160 | senderr(EINVAL); | |
e831d540 KS |
161 | error = rtrequest(RTM_ADD, dst, gate, netmask, |
162 | rtm->rtm_flags, &saved_nrt); | |
79e0ea5d KS |
163 | if (error == 0 && saved_nrt) { |
164 | rt_setmetrics(rtm->rtm_inits, | |
165 | &rtm->rtm_rmx, &saved_nrt->rt_rmx); | |
a0d75f26 | 166 | saved_nrt->rt_refcnt--; |
d6bc9b28 | 167 | saved_nrt->rt_genmask = genmask; |
79e0ea5d | 168 | } |
e831d540 KS |
169 | break; |
170 | ||
171 | case RTM_DELETE: | |
172 | error = rtrequest(RTM_DELETE, dst, gate, netmask, | |
b72a6efb | 173 | rtm->rtm_flags, (struct rtentry **)0); |
e831d540 KS |
174 | break; |
175 | ||
176 | case RTM_GET: | |
177 | case RTM_CHANGE: | |
178 | case RTM_LOCK: | |
179 | rt = rtalloc1(dst, 0); | |
180 | if (rt == 0) | |
a0d75f26 | 181 | senderr(ESRCH); |
e831d540 KS |
182 | switch(rtm->rtm_type) { |
183 | struct sockaddr *outmask; | |
184 | ||
185 | case RTM_GET: | |
186 | netmask = rt_mask(rt); | |
79e0ea5d KS |
187 | len = sizeof(*rtm) + ROUNDUP(rt_key(rt)->sa_len); |
188 | rtm->rtm_addrs = RTA_DST; | |
189 | if (rt->rt_gateway) { | |
190 | len += ROUNDUP(rt->rt_gateway->sa_len); | |
191 | rtm->rtm_addrs |= RTA_GATEWAY; | |
192 | } | |
193 | if (netmask) { | |
b72a6efb | 194 | len += netmask->sa_len; |
79e0ea5d KS |
195 | rtm->rtm_addrs |= RTA_NETMASK; |
196 | } | |
e831d540 KS |
197 | if (len > rtm->rtm_msglen) { |
198 | struct rt_msghdr *new_rtm; | |
199 | R_Malloc(new_rtm, struct rt_msghdr *, len); | |
200 | if (new_rtm == 0) | |
a0d75f26 | 201 | senderr(ENOBUFS); |
e831d540 KS |
202 | Bcopy(rtm, new_rtm, rtm->rtm_msglen); |
203 | Free(rtm); rtm = new_rtm; | |
204 | gate = (struct sockaddr *) | |
205 | (ROUNDUP(rt->rt_gateway->sa_len) | |
206 | + (char *)dst); | |
207 | Bcopy(&rt->rt_gateway, gate, | |
208 | rt->rt_gateway->sa_len); | |
209 | rtm->rtm_flags = rt->rt_flags; | |
e831d540 KS |
210 | if (netmask) { |
211 | outmask = (struct sockaddr *) | |
212 | (ROUNDUP(netmask->sa_len)+(char *)gate); | |
213 | Bcopy(netmask, outmask, netmask->sa_len); | |
e831d540 KS |
214 | } |
215 | } | |
216 | break; | |
217 | ||
218 | case RTM_CHANGE: | |
d6bc9b28 | 219 | if (gate == 0 || netmask != 0) |
79e0ea5d | 220 | senderr(EINVAL); |
e831d540 | 221 | if (gate->sa_len > (len = rt->rt_gateway->sa_len)) |
a0d75f26 KS |
222 | senderr(EDQUOT); |
223 | if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest) | |
d6bc9b28 KS |
224 | rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt, gate); |
225 | /* new gateway could require new ifaddr, ifp; | |
226 | flags may also be different; ifp may be specified | |
227 | by ll sockaddr when protocol address is ambiguous */ | |
228 | if (ifpaddr && | |
229 | (ifa = ifa_ifwithnet(ifpaddr)) && | |
230 | (ifp = ifa->ifa_ifp) && | |
231 | (ifa = ifaof_ifpforaddr(gate, ifp))) { | |
232 | /* We got it */ | |
233 | } else { | |
234 | ifa = 0; ifp = 0; | |
235 | } | |
e831d540 KS |
236 | Bcopy(gate, rt->rt_gateway, len); |
237 | rt->rt_gateway->sa_len = len; | |
79e0ea5d KS |
238 | rt_setmetrics(rtm->rtm_inits, |
239 | &rtm->rtm_rmx, &rt->rt_rmx); | |
d6bc9b28 KS |
240 | if (ifa == 0) |
241 | ifa = ifa_ifwithroute(rt->rt_flags, rt_key(rt), | |
242 | gate); | |
243 | if (ifa) { | |
244 | if (rt->rt_ifa != ifa) { | |
245 | rt->rt_ifa = ifa; | |
246 | rt->rt_ifp = ifa->ifa_ifp; | |
247 | } | |
248 | } | |
249 | if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest) | |
250 | rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, gate); | |
251 | if (genmask) | |
252 | rt->rt_genmask = genmask; | |
e831d540 KS |
253 | /* |
254 | * Fall into | |
255 | */ | |
79e0ea5d KS |
256 | case RTM_LOCK: |
257 | rt->rt_rmx.rmx_locks |= | |
258 | (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks); | |
259 | rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits); | |
e831d540 | 260 | break; |
e831d540 KS |
261 | } |
262 | goto cleanup; | |
263 | ||
264 | default: | |
a0d75f26 | 265 | senderr(EOPNOTSUPP); |
e831d540 KS |
266 | } |
267 | ||
268 | flush: | |
269 | if (rtm) { | |
270 | if (error) | |
271 | rtm->rtm_errno = error; | |
272 | else | |
273 | rtm->rtm_flags |= RTF_DONE; | |
274 | } | |
275 | cleanup: | |
276 | if (rt) | |
277 | rtfree(rt); | |
2e5be88b KS |
278 | { |
279 | register struct rawcb *rp = 0; | |
280 | /* | |
281 | * Check to see if we don't want our own messages. | |
282 | */ | |
283 | if ((so->so_options & SO_USELOOPBACK) == 0) { | |
284 | if (route_cb.any_count <= 1) { | |
285 | if (rtm) | |
286 | Free(rtm); | |
287 | m_freem(m); | |
288 | return (error); | |
289 | } | |
290 | /* There is another listener, so construct message */ | |
291 | rp = sotorawcb(so); | |
292 | } | |
32dfe9ab KS |
293 | if (cp = (caddr_t)rtm) { |
294 | m_copyback(m, 0, len, cp); | |
295 | Free(rtm); | |
296 | } | |
2e5be88b KS |
297 | if (rp) |
298 | rp->rcb_proto.sp_family = 0; /* Avoid us */ | |
e831d540 | 299 | route_proto.sp_protocol = dst->sa_family; |
32dfe9ab | 300 | raw_input(m, &route_proto, &route_src, &route_dst); |
2e5be88b KS |
301 | if (rp) |
302 | rp->rcb_proto.sp_family = PF_ROUTE; | |
303 | } | |
e831d540 KS |
304 | return (error); |
305 | } | |
306 | ||
d6bc9b28 | 307 | rt_setmetrics(which, in, out) |
79e0ea5d KS |
308 | u_long which; |
309 | register struct rt_metrics *in, *out; | |
310 | { | |
311 | #define metric(f, e) if (which & (f)) out->e = in->e; | |
312 | metric(RTV_RPIPE, rmx_recvpipe); | |
313 | metric(RTV_SPIPE, rmx_sendpipe); | |
314 | metric(RTV_SSTHRESH, rmx_ssthresh); | |
315 | metric(RTV_RTT, rmx_rtt); | |
316 | metric(RTV_RTTVAR, rmx_rttvar); | |
317 | metric(RTV_HOPCOUNT, rmx_hopcount); | |
318 | metric(RTV_MTU, rmx_mtu); | |
319 | #undef metric | |
320 | } | |
321 | ||
e831d540 KS |
322 | /* |
323 | * Copy data from a buffer back into the indicated mbuf chain, | |
324 | * starting "off" bytes from the beginning, extending the mbuf | |
325 | * chain if necessary. | |
326 | */ | |
327 | m_copyback(m0, off, len, cp) | |
328 | struct mbuf *m0; | |
329 | register int off; | |
330 | register int len; | |
331 | caddr_t cp; | |
332 | ||
333 | { | |
334 | register int mlen; | |
335 | register struct mbuf *m = m0, *n; | |
336 | int totlen = 0; | |
337 | ||
338 | if (m0 == 0) | |
339 | return; | |
340 | while (off >= (mlen = m->m_len)) { | |
341 | off -= mlen; | |
342 | totlen += mlen; | |
343 | if (m->m_next == 0) { | |
344 | n = m_getclr(M_DONTWAIT, m->m_type); | |
345 | if (n == 0) | |
346 | goto out; | |
347 | n->m_len = min(MLEN, len + off); | |
348 | m->m_next = n; | |
349 | } | |
350 | m = m->m_next; | |
351 | } | |
352 | while (len > 0) { | |
353 | mlen = min (m->m_len - off, len); | |
b72a6efb | 354 | bcopy(cp, off + mtod(m, caddr_t), (unsigned)mlen); |
e831d540 KS |
355 | cp += mlen; |
356 | len -= mlen; | |
357 | mlen += off; | |
358 | off = 0; | |
359 | totlen += mlen; | |
360 | if (len == 0) | |
361 | break; | |
362 | if (m->m_next == 0) { | |
363 | n = m_get(M_DONTWAIT, m->m_type); | |
364 | if (n == 0) | |
365 | break; | |
366 | n->m_len = min(MLEN, len); | |
367 | m->m_next = n; | |
368 | } | |
369 | m = m->m_next; | |
370 | } | |
371 | out: if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen)) | |
372 | m->m_pkthdr.len = totlen; | |
373 | } | |
374 | ||
375 | /* | |
376 | * The miss message and losing message are very similar. | |
377 | */ | |
378 | ||
b72a6efb | 379 | rt_missmsg(type, dst, gate, mask, src, flags, error) |
e831d540 KS |
380 | register struct sockaddr *dst; |
381 | struct sockaddr *gate, *mask, *src; | |
382 | { | |
383 | register struct rt_msghdr *rtm; | |
384 | register struct mbuf *m; | |
385 | int dlen = ROUNDUP(dst->sa_len); | |
386 | int len = dlen + sizeof(*rtm); | |
e831d540 KS |
387 | |
388 | if (route_cb.any_count == 0) | |
389 | return; | |
390 | m = m_gethdr(M_DONTWAIT, MT_DATA); | |
391 | if (m == 0) | |
392 | return; | |
393 | m->m_pkthdr.len = m->m_len = min(len, MHLEN); | |
394 | m->m_pkthdr.rcvif = 0; | |
395 | rtm = mtod(m, struct rt_msghdr *); | |
396 | bzero((caddr_t)rtm, sizeof(*rtm)); /*XXX assumes sizeof(*rtm) < MHLEN*/ | |
397 | rtm->rtm_flags = RTF_DONE | flags; | |
398 | rtm->rtm_msglen = len; | |
79e0ea5d | 399 | rtm->rtm_version = RTM_VERSION; |
e831d540 | 400 | rtm->rtm_type = type; |
79e0ea5d | 401 | rtm->rtm_addrs = RTA_DST; |
e831d540 | 402 | if (type == RTM_OLDADD || type == RTM_OLDDEL) { |
e831d540 | 403 | rtm->rtm_pid = u.u_procp->p_pid; |
e831d540 KS |
404 | } |
405 | m_copyback(m, sizeof (*rtm), dlen, (caddr_t)dst); | |
406 | if (gate) { | |
407 | dlen = ROUNDUP(gate->sa_len); | |
408 | m_copyback(m, len , dlen, (caddr_t)gate); | |
409 | len += dlen; | |
79e0ea5d | 410 | rtm->rtm_addrs |= RTA_GATEWAY; |
e831d540 KS |
411 | } |
412 | if (mask) { | |
b72a6efb KS |
413 | if (mask->sa_len) |
414 | dlen = ROUNDUP(mask->sa_len); | |
415 | else | |
416 | dlen = sizeof(long); | |
e831d540 KS |
417 | m_copyback(m, len , dlen, (caddr_t)mask); |
418 | len += dlen; | |
79e0ea5d | 419 | rtm->rtm_addrs |= RTA_NETMASK; |
e831d540 KS |
420 | } |
421 | if (src) { | |
422 | dlen = ROUNDUP(src->sa_len); | |
423 | m_copyback(m, len , dlen, (caddr_t)src); | |
424 | len += dlen; | |
79e0ea5d | 425 | rtm->rtm_addrs |= RTA_AUTHOR; |
e831d540 KS |
426 | } |
427 | if (m->m_pkthdr.len != len) { | |
428 | m_freem(m); | |
429 | return; | |
430 | } | |
b72a6efb | 431 | rtm->rtm_errno = error; |
e831d540 KS |
432 | rtm->rtm_msglen = len; |
433 | route_proto.sp_protocol = dst->sa_family; | |
434 | raw_input(m, &route_proto, &route_src, &route_dst); | |
435 | } | |
436 | ||
a0d75f26 | 437 | #include "kinfo.h" |
a0d75f26 KS |
438 | struct walkarg { |
439 | int w_op, w_arg; | |
440 | int w_given, w_needed; | |
441 | caddr_t w_where; | |
442 | struct { | |
443 | struct rt_msghdr m_rtm; | |
444 | char m_sabuf[128]; | |
445 | } w_m; | |
446 | #define w_rtm w_m.m_rtm | |
447 | }; | |
448 | /* | |
449 | * This is used in dumping the kernel table via getkinfo(). | |
450 | */ | |
451 | rt_dumpentry(rn, w) | |
452 | struct radix_node *rn; | |
453 | register struct walkarg *w; | |
454 | { | |
455 | register struct sockaddr *sa; | |
456 | int n, error; | |
457 | ||
2e5be88b | 458 | for (; rn; rn = rn->rn_dupedkey) { |
a0d75f26 KS |
459 | int count = 0, size = sizeof(w->w_rtm); |
460 | register struct rtentry *rt = (struct rtentry *)rn; | |
461 | ||
2e5be88b KS |
462 | if (rn->rn_flags & RNF_ROOT) |
463 | continue; | |
a0d75f26 KS |
464 | if (w->w_op == KINFO_RT_FLAGS && !(rt->rt_flags & w->w_arg)) |
465 | continue; | |
79e0ea5d KS |
466 | #define next(a, l) {size += (l); w->w_rtm.rtm_addrs |= (a); } |
467 | w->w_rtm.rtm_addrs = 0; | |
a0d75f26 | 468 | if (sa = rt_key(rt)) |
79e0ea5d | 469 | next(RTA_DST, ROUNDUP(sa->sa_len)); |
a0d75f26 | 470 | if (sa = rt->rt_gateway) |
79e0ea5d KS |
471 | next(RTA_GATEWAY, ROUNDUP(sa->sa_len)); |
472 | if (sa = rt_mask(rt)) | |
473 | next(RTA_NETMASK, | |
474 | sa->sa_len ? ROUNDUP(sa->sa_len) : sizeof(long)); | |
a0d75f26 | 475 | if (sa = rt->rt_genmask) |
79e0ea5d | 476 | next(RTA_GENMASK, ROUNDUP(sa->sa_len)); |
a0d75f26 KS |
477 | w->w_needed += size; |
478 | if (w->w_where == NULL || w->w_needed > 0) | |
479 | continue; | |
480 | w->w_rtm.rtm_msglen = size; | |
a0d75f26 | 481 | w->w_rtm.rtm_flags = rt->rt_flags; |
79e0ea5d KS |
482 | w->w_rtm.rtm_use = rt->rt_use; |
483 | w->w_rtm.rtm_rmx = rt->rt_rmx; | |
484 | w->w_rtm.rtm_index = rt->rt_ifp->if_index; | |
485 | #undef next | |
486 | #define next(l) {n = (l); Bcopy(sa, cp, n); cp += n;} | |
a0d75f26 KS |
487 | if (size <= sizeof(w->w_m)) { |
488 | register caddr_t cp = (caddr_t)(w->w_m.m_sabuf); | |
a0d75f26 | 489 | if (sa = rt_key(rt)) |
79e0ea5d | 490 | next(ROUNDUP(sa->sa_len)); |
a0d75f26 | 491 | if (sa = rt->rt_gateway) |
79e0ea5d KS |
492 | next(ROUNDUP(sa->sa_len)); |
493 | if (sa = rt_mask(rt)) | |
494 | next(sa->sa_len ? ROUNDUP(sa->sa_len) : sizeof(long)); | |
a0d75f26 | 495 | if (sa = rt->rt_genmask) |
79e0ea5d | 496 | next(ROUNDUP(sa->sa_len)); |
a0d75f26 | 497 | #undef next |
79e0ea5d KS |
498 | #define next(s, l) {n = (l); \ |
499 | if (error = copyout((caddr_t)(s), w->w_where, n)) return (error); \ | |
500 | w->w_where += n;} | |
a0d75f26 KS |
501 | |
502 | next(&w->w_m, size); /* Copy rtmsg and sockaddrs back */ | |
503 | continue; | |
504 | } | |
505 | next(&w->w_rtm, sizeof(w->w_rtm)); | |
506 | if (sa = rt_key(rt)) | |
507 | next(sa, ROUNDUP(sa->sa_len)); | |
508 | if (sa = rt->rt_gateway) | |
509 | next(sa, ROUNDUP(sa->sa_len)); | |
79e0ea5d KS |
510 | if (sa = rt_mask(rt)) |
511 | next(sa, sa->sa_len ? ROUNDUP(sa->sa_len) : sizeof(long)); | |
a0d75f26 KS |
512 | if (sa = rt->rt_genmask) |
513 | next(sa, ROUNDUP(sa->sa_len)); | |
514 | } | |
515 | return (0); | |
516 | #undef next | |
517 | } | |
518 | ||
519 | kinfo_rtable(op, where, given, arg, needed) | |
520 | int op, arg; | |
521 | caddr_t where; | |
522 | int *given, *needed; | |
523 | { | |
524 | register struct radix_node_head *rnh; | |
525 | int s, error = 0; | |
526 | u_char af = ki_af(op); | |
527 | struct walkarg w; | |
528 | ||
529 | op &= 0xffff; | |
530 | if (op != KINFO_RT_DUMP && op != KINFO_RT_FLAGS) | |
531 | return (EINVAL); | |
532 | ||
533 | Bzero(&w, sizeof(w)); | |
534 | if ((w.w_where = where) && given) | |
535 | w.w_given = *given; | |
536 | w.w_needed = 0 - w.w_given; | |
537 | w.w_arg = arg; | |
538 | w.w_op = op; | |
79e0ea5d | 539 | w.w_rtm.rtm_version = RTM_VERSION; |
a0d75f26 KS |
540 | w.w_rtm.rtm_type = RTM_GET; |
541 | ||
542 | s = splnet(); | |
543 | for (rnh = radix_node_head; rnh; rnh = rnh->rnh_next) { | |
544 | if (rnh->rnh_af == 0) | |
545 | continue; | |
546 | if (af && af != rnh->rnh_af) | |
547 | continue; | |
548 | error = rt_walk(rnh->rnh_treetop, rt_dumpentry, &w); | |
549 | if (error) | |
550 | break; | |
551 | } | |
552 | w.w_needed += w.w_given; | |
553 | if (where && given) | |
554 | *given = w.w_where - where; | |
555 | else | |
556 | w.w_needed = (11 * w.w_needed) / 10; | |
557 | *needed = w.w_needed; | |
558 | splx(s); | |
559 | return (error); | |
560 | } | |
561 | ||
562 | rt_walk(rn, f, w) | |
563 | register struct radix_node *rn; | |
564 | register int (*f)(); | |
565 | struct walkarg *w; | |
566 | { | |
567 | int error; | |
568 | for (;;) { | |
569 | while (rn->rn_b >= 0) | |
570 | rn = rn->rn_l; /* First time through node, go left */ | |
571 | if (error = (*f)(rn, w)) | |
572 | return (error); /* Process Leaf */ | |
573 | while (rn->rn_p->rn_r == rn) { /* if coming back from right */ | |
574 | rn = rn->rn_p; /* go back up */ | |
575 | if (rn->rn_flags & RNF_ROOT) | |
576 | return 0; | |
577 | } | |
578 | rn = rn->rn_p->rn_r; /* otherwise, go right*/ | |
579 | } | |
580 | } | |
581 | ||
e831d540 KS |
582 | /* |
583 | * Definitions of protocols supported in the ROUTE domain. | |
584 | */ | |
585 | ||
e831d540 KS |
586 | int raw_init(),raw_usrreq(),raw_input(),raw_ctlinput(); |
587 | extern struct domain routedomain; /* or at least forward */ | |
588 | ||
589 | struct protosw routesw[] = { | |
590 | { SOCK_RAW, &routedomain, 0, PR_ATOMIC|PR_ADDR, | |
591 | raw_input, route_output, raw_ctlinput, 0, | |
592 | route_usrreq, | |
593 | raw_init, 0, 0, 0, | |
594 | }, | |
595 | { 0, 0, 0, 0, | |
596 | raw_input, 0, raw_ctlinput, 0, | |
597 | raw_usrreq, | |
598 | raw_init, 0, 0, 0, | |
599 | } | |
600 | }; | |
601 | ||
602 | int unp_externalize(), unp_dispose(); | |
603 | ||
604 | struct domain routedomain = | |
605 | { PF_ROUTE, "route", 0, 0, 0, | |
606 | routesw, &routesw[sizeof(routesw)/sizeof(routesw[0])] }; |