rtsock.c revision 1.100.2.2 1 /* $NetBSD: rtsock.c,v 1.100.2.2 2008/03/29 20:47:02 christos Exp $ */
2
3 /*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 1988, 1991, 1993
34 * The Regents of the University of California. All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 * @(#)rtsock.c 8.7 (Berkeley) 10/12/95
61 */
62
63 #include <sys/cdefs.h>
64 __KERNEL_RCSID(0, "$NetBSD: rtsock.c,v 1.100.2.2 2008/03/29 20:47:02 christos Exp $");
65
66 #include "opt_inet.h"
67 #include "opt_compat_netbsd.h"
68
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/proc.h>
72 #include <sys/mbuf.h>
73 #include <sys/socket.h>
74 #include <sys/socketvar.h>
75 #include <sys/domain.h>
76 #include <sys/protosw.h>
77 #include <sys/sysctl.h>
78 #include <sys/kauth.h>
79 #include <sys/intr.h>
80 #ifdef RTSOCK_DEBUG
81 #include <netinet/in.h>
82 #endif /* RTSOCK_DEBUG */
83
84 #include <net/if.h>
85 #include <net/route.h>
86 #include <net/raw_cb.h>
87
88 #if defined(COMPAT_14) || defined(COMPAT_50)
89 #include <compat/net/if.h>
90 #endif
91
92 #include <machine/stdarg.h>
93
94 DOMAIN_DEFINE(routedomain); /* forward declare and add to link set */
95
96 struct sockaddr route_dst = { .sa_len = 2, .sa_family = PF_ROUTE, };
97 struct sockaddr route_src = { .sa_len = 2, .sa_family = PF_ROUTE, };
98
99 int route_maxqlen = IFQ_MAXLEN;
100 static struct ifqueue route_intrq;
101 static void *route_sih;
102
103 static int rt_msg2(int, struct rt_addrinfo *, void *, struct rt_walkarg *, int *);
104 static int rt_xaddrs(u_char, const char *, const char *, struct rt_addrinfo *);
105 static struct mbuf *rt_makeifannouncemsg(struct ifnet *, int, int,
106 struct rt_addrinfo *);
107 static int sysctl_dumpentry(struct rtentry *, void *);
108 static int sysctl_iflist(int, struct rt_walkarg *, int);
109 static int sysctl_rtable(SYSCTLFN_PROTO);
110 static inline void rt_adjustcount(int, int);
111
112 /* Sleazy use of local variables throughout file, warning!!!! */
113 #define dst info.rti_info[RTAX_DST]
114 #define gate info.rti_info[RTAX_GATEWAY]
115 #define netmask info.rti_info[RTAX_NETMASK]
116 #define ifpaddr info.rti_info[RTAX_IFP]
117 #define ifaaddr info.rti_info[RTAX_IFA]
118 #define brdaddr info.rti_info[RTAX_BRD]
119
120 static inline void
121 rt_adjustcount(int af, int cnt)
122 {
123 route_cb.any_count += cnt;
124 switch (af) {
125 case AF_INET:
126 route_cb.ip_count += cnt;
127 return;
128 #ifdef INET6
129 case AF_INET6:
130 route_cb.ip6_count += cnt;
131 return;
132 #endif
133 case AF_IPX:
134 route_cb.ipx_count += cnt;
135 return;
136 case AF_NS:
137 route_cb.ns_count += cnt;
138 return;
139 case AF_ISO:
140 route_cb.iso_count += cnt;
141 return;
142 }
143 }
144 static inline void
145 cvtmetrics(struct ort_metrics *ortm, const struct rt_metrics *rtm)
146 {
147 ortm->rmx_locks = rtm->rmx_locks;
148 ortm->rmx_mtu = rtm->rmx_mtu;
149 ortm->rmx_hopcount = rtm->rmx_hopcount;
150 ortm->rmx_expire = rtm->rmx_expire;
151 ortm->rmx_recvpipe = rtm->rmx_recvpipe;
152 ortm->rmx_sendpipe = rtm->rmx_sendpipe;
153 ortm->rmx_ssthresh = rtm->rmx_ssthresh;
154 ortm->rmx_rtt = rtm->rmx_rtt;
155 ortm->rmx_rttvar = rtm->rmx_rttvar;
156 ortm->rmx_pksent = rtm->rmx_pksent;
157 }
158
159 /*ARGSUSED*/
160 int
161 route_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
162 struct mbuf *control, struct lwp *l)
163 {
164 int error = 0;
165 struct rawcb *rp = sotorawcb(so);
166 int s;
167
168 if (req == PRU_ATTACH) {
169 MALLOC(rp, struct rawcb *, sizeof(*rp), M_PCB, M_WAITOK);
170 if ((so->so_pcb = rp) != NULL)
171 memset(so->so_pcb, 0, sizeof(*rp));
172
173 }
174 if (req == PRU_DETACH && rp)
175 rt_adjustcount(rp->rcb_proto.sp_protocol, -1);
176 s = splsoftnet();
177
178 /*
179 * Don't call raw_usrreq() in the attach case, because
180 * we want to allow non-privileged processes to listen on
181 * and send "safe" commands to the routing socket.
182 */
183 if (req == PRU_ATTACH) {
184 if (l == NULL)
185 error = EACCES;
186 else
187 error = raw_attach(so, (int)(long)nam);
188 } else
189 error = raw_usrreq(so, req, m, nam, control, l);
190
191 rp = sotorawcb(so);
192 if (req == PRU_ATTACH && rp) {
193 if (error) {
194 free((void *)rp, M_PCB);
195 splx(s);
196 return error;
197 }
198 rt_adjustcount(rp->rcb_proto.sp_protocol, 1);
199 rp->rcb_laddr = &route_src;
200 rp->rcb_faddr = &route_dst;
201 soisconnected(so);
202 so->so_options |= SO_USELOOPBACK;
203 }
204 splx(s);
205 return error;
206 }
207
208 static const struct sockaddr *
209 intern_netmask(const struct sockaddr *mask)
210 {
211 struct radix_node *rn;
212 extern struct radix_node_head *mask_rnhead;
213
214 if (mask != NULL &&
215 (rn = rn_search(mask, mask_rnhead->rnh_treetop)))
216 mask = (const struct sockaddr *)rn->rn_key;
217
218 return mask;
219 }
220
221 /*ARGSUSED*/
222 int
223 route_output(struct mbuf *m, ...)
224 {
225 struct sockproto proto = { .sp_family = PF_ROUTE, };
226 struct rt_msghdr *rtm = NULL;
227 struct rtentry *rt = NULL;
228 struct rtentry *saved_nrt = NULL;
229 struct rt_addrinfo info;
230 int len, error = 0;
231 struct ifnet *ifp = NULL;
232 struct ifaddr *ifa = NULL;
233 struct socket *so;
234 va_list ap;
235 sa_family_t family;
236
237 va_start(ap, m);
238 so = va_arg(ap, struct socket *);
239 va_end(ap);
240
241 #define senderr(e) do { error = e; goto flush;} while (/*CONSTCOND*/ 0)
242 if (m == NULL || ((m->m_len < sizeof(int32_t)) &&
243 (m = m_pullup(m, sizeof(int32_t))) == NULL))
244 return ENOBUFS;
245 if ((m->m_flags & M_PKTHDR) == 0)
246 panic("route_output");
247 len = m->m_pkthdr.len;
248 if (len < sizeof(*rtm) ||
249 len != mtod(m, struct rt_msghdr *)->rtm_msglen) {
250 dst = NULL;
251 senderr(EINVAL);
252 }
253 R_Malloc(rtm, struct rt_msghdr *, len);
254 if (rtm == NULL) {
255 dst = NULL;
256 senderr(ENOBUFS);
257 }
258 m_copydata(m, 0, len, (void *)rtm);
259 if (rtm->rtm_version != RTM_VERSION) {
260 dst = NULL;
261 senderr(EPROTONOSUPPORT);
262 }
263 rtm->rtm_pid = curproc->p_pid;
264 memset(&info, 0, sizeof(info));
265 info.rti_addrs = rtm->rtm_addrs;
266 if (rt_xaddrs(rtm->rtm_type, (void *)(rtm + 1), len + (char *)rtm, &info))
267 senderr(EINVAL);
268 info.rti_flags = rtm->rtm_flags;
269 #ifdef RTSOCK_DEBUG
270 if (dst->sa_family == AF_INET) {
271 printf("%s: extracted dst %s\n", __func__,
272 inet_ntoa(((const struct sockaddr_in *)dst)->sin_addr));
273 }
274 #endif /* RTSOCK_DEBUG */
275 if (dst == NULL || (dst->sa_family >= AF_MAX))
276 senderr(EINVAL);
277 if (gate != NULL && (gate->sa_family >= AF_MAX))
278 senderr(EINVAL);
279
280 /*
281 * Verify that the caller has the appropriate privilege; RTM_GET
282 * is the only operation the non-superuser is allowed.
283 */
284 if (kauth_authorize_network(curlwp->l_cred, KAUTH_NETWORK_ROUTE,
285 0, rtm, NULL, NULL) != 0)
286 senderr(EACCES);
287
288 switch (rtm->rtm_type) {
289
290 case RTM_ADD:
291 if (gate == NULL)
292 senderr(EINVAL);
293 error = rtrequest1(rtm->rtm_type, &info, &saved_nrt);
294 if (error == 0 && saved_nrt) {
295 rt_setmetrics(rtm->rtm_inits,
296 &rtm->rtm_rmx, &saved_nrt->rt_rmx);
297 saved_nrt->rt_refcnt--;
298 }
299 break;
300
301 case RTM_DELETE:
302 error = rtrequest1(rtm->rtm_type, &info, &saved_nrt);
303 if (error == 0) {
304 (rt = saved_nrt)->rt_refcnt++;
305 goto report;
306 }
307 break;
308
309 case RTM_GET:
310 case RTM_CHANGE:
311 case RTM_LOCK:
312 /* XXX This will mask dst with netmask before
313 * searching. It did not used to do that. --dyoung
314 */
315 error = rtrequest(RTM_GET, dst, gate, netmask, 0, &rt);
316 if (error != 0)
317 senderr(error);
318 if (rtm->rtm_type != RTM_GET) {/* XXX: too grotty */
319 struct radix_node *rn;
320
321 if (memcmp(dst, rt_getkey(rt), dst->sa_len) != 0)
322 senderr(ESRCH);
323 netmask = intern_netmask(netmask);
324 for (rn = rt->rt_nodes; rn; rn = rn->rn_dupedkey)
325 if (netmask == (const struct sockaddr *)rn->rn_mask)
326 break;
327 if (rn == NULL)
328 senderr(ETOOMANYREFS);
329 rt = (struct rtentry *)rn;
330 }
331
332 switch (rtm->rtm_type) {
333 case RTM_GET:
334 report:
335 dst = rt_getkey(rt);
336 gate = rt->rt_gateway;
337 netmask = rt_mask(rt);
338 if ((rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) == 0)
339 ;
340 else if ((ifp = rt->rt_ifp) != NULL) {
341 const struct ifaddr *rtifa;
342 ifpaddr = ifp->if_dl->ifa_addr;
343 /* rtifa used to be simply rt->rt_ifa.
344 * If rt->rt_ifa != NULL, then
345 * rt_get_ifa() != NULL. So this
346 * ought to still be safe. --dyoung
347 */
348 rtifa = rt_get_ifa(rt);
349 ifaaddr = rtifa->ifa_addr;
350 #ifdef RTSOCK_DEBUG
351 if (ifaaddr->sa_family == AF_INET) {
352 printf("%s: copying out RTAX_IFA %s ",
353 __func__,
354 inet_ntoa(((const struct sockaddr_in *)ifaaddr)->sin_addr));
355 printf("for dst %s ifa_getifa %p ifa_seqno %p\n",
356 inet_ntoa(((const struct sockaddr_in *)dst)->sin_addr),
357 (void *)rtifa->ifa_getifa, rtifa->ifa_seqno);
358 }
359 #endif /* RTSOCK_DEBUG */
360 if (ifp->if_flags & IFF_POINTOPOINT)
361 brdaddr = rtifa->ifa_dstaddr;
362 else
363 brdaddr = NULL;
364 rtm->rtm_index = ifp->if_index;
365 } else {
366 ifpaddr = NULL;
367 ifaaddr = NULL;
368 }
369 (void)rt_msg2(rtm->rtm_type, &info, NULL, NULL, &len);
370 if (len > rtm->rtm_msglen) {
371 struct rt_msghdr *new_rtm;
372 R_Malloc(new_rtm, struct rt_msghdr *, len);
373 if (new_rtm == NULL)
374 senderr(ENOBUFS);
375 Bcopy(rtm, new_rtm, rtm->rtm_msglen);
376 Free(rtm); rtm = new_rtm;
377 }
378 (void)rt_msg2(rtm->rtm_type, &info, (void *)rtm,
379 NULL, 0);
380 rtm->rtm_flags = rt->rt_flags;
381 cvtmetrics(&rtm->rtm_rmx, &rt->rt_rmx);
382 rtm->rtm_addrs = info.rti_addrs;
383 break;
384
385 case RTM_CHANGE:
386 /*
387 * new gateway could require new ifaddr, ifp;
388 * flags may also be different; ifp may be specified
389 * by ll sockaddr when protocol address is ambiguous
390 */
391 if ((error = rt_getifa(&info)) != 0)
392 senderr(error);
393 if (gate && rt_setgate(rt, gate))
394 senderr(EDQUOT);
395 /* new gateway could require new ifaddr, ifp;
396 flags may also be different; ifp may be specified
397 by ll sockaddr when protocol address is ambiguous */
398 if (ifpaddr && (ifa = ifa_ifwithnet(ifpaddr)) &&
399 (ifp = ifa->ifa_ifp) && (ifaaddr || gate))
400 ifa = ifaof_ifpforaddr(ifaaddr ? ifaaddr : gate,
401 ifp);
402 else if ((ifaaddr && (ifa = ifa_ifwithaddr(ifaaddr))) ||
403 (gate && (ifa = ifa_ifwithroute(rt->rt_flags,
404 rt_getkey(rt), gate))))
405 ifp = ifa->ifa_ifp;
406 if (ifa) {
407 struct ifaddr *oifa = rt->rt_ifa;
408 if (oifa != ifa) {
409 if (oifa && oifa->ifa_rtrequest) {
410 oifa->ifa_rtrequest(RTM_DELETE,
411 rt, &info);
412 }
413 rt_replace_ifa(rt, ifa);
414 rt->rt_ifp = ifp;
415 }
416 }
417 rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
418 &rt->rt_rmx);
419 if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
420 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info);
421 /*
422 * Fall into
423 */
424 case RTM_LOCK:
425 rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
426 rt->rt_rmx.rmx_locks |=
427 (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
428 break;
429 }
430 break;
431
432 default:
433 senderr(EOPNOTSUPP);
434 }
435
436 flush:
437 if (rtm) {
438 if (error)
439 rtm->rtm_errno = error;
440 else
441 rtm->rtm_flags |= RTF_DONE;
442 }
443 family = dst ? dst->sa_family : 0;
444 if (rt)
445 rtfree(rt);
446 {
447 struct rawcb *rp = NULL;
448 /*
449 * Check to see if we don't want our own messages.
450 */
451 if ((so->so_options & SO_USELOOPBACK) == 0) {
452 if (route_cb.any_count <= 1) {
453 if (rtm)
454 Free(rtm);
455 m_freem(m);
456 return error;
457 }
458 /* There is another listener, so construct message */
459 rp = sotorawcb(so);
460 }
461 if (rtm) {
462 m_copyback(m, 0, rtm->rtm_msglen, (void *)rtm);
463 if (m->m_pkthdr.len < rtm->rtm_msglen) {
464 m_freem(m);
465 m = NULL;
466 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
467 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
468 Free(rtm);
469 }
470 if (rp)
471 rp->rcb_proto.sp_family = 0; /* Avoid us */
472 if (family)
473 proto.sp_protocol = family;
474 if (m)
475 raw_input(m, &proto, &route_src, &route_dst);
476 if (rp)
477 rp->rcb_proto.sp_family = PF_ROUTE;
478 }
479 return error;
480 }
481
482 void
483 rt_setmetrics(u_long which, const struct ort_metrics *in, struct rt_metrics *out)
484 {
485 #define metric(f, e) if (which & (f)) out->e = in->e;
486 metric(RTV_RPIPE, rmx_recvpipe);
487 metric(RTV_SPIPE, rmx_sendpipe);
488 metric(RTV_SSTHRESH, rmx_ssthresh);
489 metric(RTV_RTT, rmx_rtt);
490 metric(RTV_RTTVAR, rmx_rttvar);
491 metric(RTV_HOPCOUNT, rmx_hopcount);
492 metric(RTV_MTU, rmx_mtu);
493 /* XXX time_t: Will not work after 2038 */
494 metric(RTV_EXPIRE, rmx_expire);
495 #undef metric
496 }
497
498 #define ROUNDUP(a) \
499 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
500 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
501
502 static int
503 rt_xaddrs(u_char rtmtype, const char *cp, const char *cplim, struct rt_addrinfo *rtinfo)
504 {
505 const struct sockaddr *sa = NULL; /* Quell compiler warning */
506 int i;
507
508 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
509 if ((rtinfo->rti_addrs & (1 << i)) == 0)
510 continue;
511 rtinfo->rti_info[i] = sa = (const struct sockaddr *)cp;
512 ADVANCE(cp, sa);
513 }
514
515 /* Check for extra addresses specified, except RTM_GET asking for interface info. */
516 if (rtmtype == RTM_GET) {
517 if (((rtinfo->rti_addrs & (~((1 << RTAX_IFP) | (1 << RTAX_IFA)))) & (~0 << i)) != 0)
518 return 1;
519 } else {
520 if ((rtinfo->rti_addrs & (~0 << i)) != 0)
521 return 1;
522 }
523 /* Check for bad data length. */
524 if (cp != cplim) {
525 if (i == RTAX_NETMASK + 1 && sa &&
526 cp - ROUNDUP(sa->sa_len) + sa->sa_len == cplim)
527 /*
528 * The last sockaddr was netmask.
529 * We accept this for now for the sake of old
530 * binaries or third party softwares.
531 */
532 ;
533 else
534 return 1;
535 }
536 return 0;
537 }
538
539 struct mbuf *
540 rt_msg1(int type, struct rt_addrinfo *rtinfo, void *data, int datalen)
541 {
542 struct rt_msghdr *rtm;
543 struct mbuf *m;
544 int i;
545 const struct sockaddr *sa;
546 int len, dlen;
547
548 m = m_gethdr(M_DONTWAIT, MT_DATA);
549 if (m == NULL)
550 return m;
551 MCLAIM(m, &routedomain.dom_mowner);
552 switch (type) {
553
554 case RTM_DELADDR:
555 case RTM_NEWADDR:
556 len = sizeof(struct ifa_msghdr);
557 break;
558
559 #ifdef COMPAT_14
560 case RTM_OOIFINFO:
561 len = sizeof(struct if_msghdr14);
562 break;
563 #endif
564 #ifdef COMPAT_50
565 case RTM_OIFINFO:
566 len = sizeof(struct if_msghdr50);
567 break;
568 #endif
569
570 case RTM_IFINFO:
571 len = sizeof(struct if_msghdr);
572 break;
573
574 case RTM_IFANNOUNCE:
575 case RTM_IEEE80211:
576 len = sizeof(struct if_announcemsghdr);
577 break;
578
579 default:
580 len = sizeof(struct rt_msghdr);
581 }
582 if (len > MHLEN + MLEN)
583 panic("rt_msg1: message too long");
584 else if (len > MHLEN) {
585 m->m_next = m_get(M_DONTWAIT, MT_DATA);
586 if (m->m_next == NULL) {
587 m_freem(m);
588 return NULL;
589 }
590 MCLAIM(m->m_next, m->m_owner);
591 m->m_pkthdr.len = len;
592 m->m_len = MHLEN;
593 m->m_next->m_len = len - MHLEN;
594 } else {
595 m->m_pkthdr.len = m->m_len = len;
596 }
597 m->m_pkthdr.rcvif = NULL;
598 m_copyback(m, 0, datalen, data);
599 rtm = mtod(m, struct rt_msghdr *);
600 for (i = 0; i < RTAX_MAX; i++) {
601 if ((sa = rtinfo->rti_info[i]) == NULL)
602 continue;
603 rtinfo->rti_addrs |= (1 << i);
604 dlen = ROUNDUP(sa->sa_len);
605 m_copyback(m, len, dlen, sa);
606 len += dlen;
607 }
608 if (m->m_pkthdr.len != len) {
609 m_freem(m);
610 return NULL;
611 }
612 rtm->rtm_msglen = len;
613 rtm->rtm_version = RTM_VERSION;
614 rtm->rtm_type = type;
615 return m;
616 }
617
618 /*
619 * rt_msg2
620 *
621 * fills 'cp' or 'w'.w_tmem with the routing socket message and
622 * returns the length of the message in 'lenp'.
623 *
624 * if walkarg is 0, cp is expected to be 0 or a buffer large enough to hold
625 * the message
626 * otherwise walkarg's w_needed is updated and if the user buffer is
627 * specified and w_needed indicates space exists the information is copied
628 * into the temp space (w_tmem). w_tmem is [re]allocated if necessary,
629 * if the allocation fails ENOBUFS is returned.
630 */
631 static int
632 rt_msg2(int type, struct rt_addrinfo *rtinfo, void *cpv, struct rt_walkarg *w,
633 int *lenp)
634 {
635 int i;
636 int len, dlen, second_time = 0;
637 char *cp0, *cp = cpv;
638
639 rtinfo->rti_addrs = 0;
640 again:
641 switch (type) {
642
643 case RTM_DELADDR:
644 case RTM_NEWADDR:
645 len = sizeof(struct ifa_msghdr);
646 break;
647 #ifdef COMPAT_14
648 case RTM_OOIFINFO:
649 len = sizeof(struct if_msghdr14);
650 break;
651 #endif
652 #ifdef COMPAT_50
653 case RTM_OIFINFO:
654 len = sizeof(struct if_msghdr50);
655 break;
656 #endif
657
658 case RTM_IFINFO:
659 len = sizeof(struct if_msghdr);
660 break;
661
662 default:
663 len = sizeof(struct rt_msghdr);
664 }
665 if ((cp0 = cp) != NULL)
666 cp += len;
667 for (i = 0; i < RTAX_MAX; i++) {
668 const struct sockaddr *sa;
669
670 if ((sa = rtinfo->rti_info[i]) == NULL)
671 continue;
672 rtinfo->rti_addrs |= (1 << i);
673 dlen = ROUNDUP(sa->sa_len);
674 if (cp) {
675 bcopy(sa, cp, (unsigned)dlen);
676 cp += dlen;
677 }
678 len += dlen;
679 }
680 if (cp == NULL && w != NULL && !second_time) {
681 struct rt_walkarg *rw = w;
682
683 rw->w_needed += len;
684 if (rw->w_needed <= 0 && rw->w_where) {
685 if (rw->w_tmemsize < len) {
686 if (rw->w_tmem)
687 free(rw->w_tmem, M_RTABLE);
688 rw->w_tmem = (void *) malloc(len, M_RTABLE,
689 M_NOWAIT);
690 if (rw->w_tmem)
691 rw->w_tmemsize = len;
692 }
693 if (rw->w_tmem) {
694 cp = rw->w_tmem;
695 second_time = 1;
696 goto again;
697 } else {
698 rw->w_tmemneeded = len;
699 return ENOBUFS;
700 }
701 }
702 }
703 if (cp) {
704 struct rt_msghdr *rtm = (struct rt_msghdr *)cp0;
705
706 rtm->rtm_version = RTM_VERSION;
707 rtm->rtm_type = type;
708 rtm->rtm_msglen = len;
709 }
710 if (lenp)
711 *lenp = len;
712 return 0;
713 }
714
715 /*
716 * This routine is called to generate a message from the routing
717 * socket indicating that a redirect has occurred, a routing lookup
718 * has failed, or that a protocol has detected timeouts to a particular
719 * destination.
720 */
721 void
722 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
723 {
724 struct rt_msghdr rtm;
725 struct mbuf *m;
726 const struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
727
728 if (route_cb.any_count == 0)
729 return;
730 memset(&rtm, 0, sizeof(rtm));
731 rtm.rtm_flags = RTF_DONE | flags;
732 rtm.rtm_errno = error;
733 m = rt_msg1(type, rtinfo, (void *)&rtm, sizeof(rtm));
734 if (m == NULL)
735 return;
736 mtod(m, struct rt_msghdr *)->rtm_addrs = rtinfo->rti_addrs;
737 route_enqueue(m, sa ? sa->sa_family : 0);
738 }
739
740 /*
741 * This routine is called to generate a message from the routing
742 * socket indicating that the status of a network interface has changed.
743 */
744 void
745 rt_ifmsg(struct ifnet *ifp)
746 {
747 struct if_msghdr ifm;
748 struct mbuf *m;
749 struct rt_addrinfo info;
750
751 if (route_cb.any_count == 0)
752 return;
753 (void)memset(&info, 0, sizeof(info));
754 (void)memset(&ifm, 0, sizeof(ifm));
755 ifm.ifm_index = ifp->if_index;
756 ifm.ifm_flags = ifp->if_flags;
757 ifm.ifm_data = ifp->if_data;
758 ifm.ifm_addrs = 0;
759 m = rt_msg1(RTM_IFINFO, &info, (void *)&ifm, sizeof(ifm));
760 if (m == NULL)
761 return;
762 route_enqueue(m, 0);
763 #ifdef COMPAT_14
764 compat_14_rt_ifmsg(ifp, &ifm);
765 #endif
766 #ifdef COMPAT_50
767 compat_50_rt_ifmsg(ifp, &ifm);
768 #endif
769 }
770
771
772 /*
773 * This is called to generate messages from the routing socket
774 * indicating a network interface has had addresses associated with it.
775 * if we ever reverse the logic and replace messages TO the routing
776 * socket indicate a request to configure interfaces, then it will
777 * be unnecessary as the routing socket will automatically generate
778 * copies of it.
779 */
780 void
781 rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt)
782 {
783 struct rt_addrinfo info;
784 const struct sockaddr *sa = NULL;
785 int pass;
786 struct mbuf *m = NULL;
787 struct ifnet *ifp = ifa->ifa_ifp;
788
789 if (route_cb.any_count == 0)
790 return;
791 for (pass = 1; pass < 3; pass++) {
792 memset(&info, 0, sizeof(info));
793 if ((cmd == RTM_ADD && pass == 1) ||
794 (cmd == RTM_DELETE && pass == 2)) {
795 struct ifa_msghdr ifam;
796 int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
797
798 ifaaddr = sa = ifa->ifa_addr;
799 ifpaddr = ifp->if_dl->ifa_addr;
800 netmask = ifa->ifa_netmask;
801 brdaddr = ifa->ifa_dstaddr;
802 memset(&ifam, 0, sizeof(ifam));
803 ifam.ifam_index = ifp->if_index;
804 ifam.ifam_metric = ifa->ifa_metric;
805 ifam.ifam_flags = ifa->ifa_flags;
806 m = rt_msg1(ncmd, &info, (void *)&ifam, sizeof(ifam));
807 if (m == NULL)
808 continue;
809 mtod(m, struct ifa_msghdr *)->ifam_addrs =
810 info.rti_addrs;
811 }
812 if ((cmd == RTM_ADD && pass == 2) ||
813 (cmd == RTM_DELETE && pass == 1)) {
814 struct rt_msghdr rtm;
815
816 if (rt == NULL)
817 continue;
818 netmask = rt_mask(rt);
819 dst = sa = rt_getkey(rt);
820 gate = rt->rt_gateway;
821 memset(&rtm, 0, sizeof(rtm));
822 rtm.rtm_index = ifp->if_index;
823 rtm.rtm_flags |= rt->rt_flags;
824 rtm.rtm_errno = error;
825 m = rt_msg1(cmd, &info, (void *)&rtm, sizeof(rtm));
826 if (m == NULL)
827 continue;
828 mtod(m, struct rt_msghdr *)->rtm_addrs = info.rti_addrs;
829 }
830 route_enqueue(m, sa ? sa->sa_family : 0);
831 }
832 }
833
834 static struct mbuf *
835 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
836 struct rt_addrinfo *info)
837 {
838 struct if_announcemsghdr ifan;
839
840 memset(info, 0, sizeof(*info));
841 memset(&ifan, 0, sizeof(ifan));
842 ifan.ifan_index = ifp->if_index;
843 strlcpy(ifan.ifan_name, ifp->if_xname, sizeof(ifan.ifan_name));
844 ifan.ifan_what = what;
845 return rt_msg1(type, info, (void *)&ifan, sizeof(ifan));
846 }
847
848 /*
849 * This is called to generate routing socket messages indicating
850 * network interface arrival and departure.
851 */
852 void
853 rt_ifannouncemsg(struct ifnet *ifp, int what)
854 {
855 struct mbuf *m;
856 struct rt_addrinfo info;
857
858 if (route_cb.any_count == 0)
859 return;
860 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
861 if (m == NULL)
862 return;
863 route_enqueue(m, 0);
864 }
865
866 /*
867 * This is called to generate routing socket messages indicating
868 * IEEE80211 wireless events.
869 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
870 */
871 void
872 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
873 {
874 struct mbuf *m;
875 struct rt_addrinfo info;
876
877 if (route_cb.any_count == 0)
878 return;
879 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
880 if (m == NULL)
881 return;
882 /*
883 * Append the ieee80211 data. Try to stick it in the
884 * mbuf containing the ifannounce msg; otherwise allocate
885 * a new mbuf and append.
886 *
887 * NB: we assume m is a single mbuf.
888 */
889 if (data_len > M_TRAILINGSPACE(m)) {
890 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
891 if (n == NULL) {
892 m_freem(m);
893 return;
894 }
895 (void)memcpy(mtod(n, void *), data, data_len);
896 n->m_len = data_len;
897 m->m_next = n;
898 } else if (data_len > 0) {
899 (void)memcpy(mtod(m, uint8_t *) + m->m_len, data, data_len);
900 m->m_len += data_len;
901 }
902 if (m->m_flags & M_PKTHDR)
903 m->m_pkthdr.len += data_len;
904 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
905 route_enqueue(m, 0);
906 }
907
908 /*
909 * This is used in dumping the kernel table via sysctl().
910 */
911 static int
912 sysctl_dumpentry(struct rtentry *rt, void *v)
913 {
914 struct rt_walkarg *w = v;
915 int error = 0, size;
916 struct rt_addrinfo info;
917
918 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
919 return 0;
920 memset(&info, 0, sizeof(info));
921 dst = rt_getkey(rt);
922 gate = rt->rt_gateway;
923 netmask = rt_mask(rt);
924 if (rt->rt_ifp) {
925 const struct ifaddr *rtifa;
926 ifpaddr = rt->rt_ifp->if_dl->ifa_addr;
927 /* rtifa used to be simply rt->rt_ifa. If rt->rt_ifa != NULL,
928 * then rt_get_ifa() != NULL. So this ought to still be safe.
929 * --dyoung
930 */
931 rtifa = rt_get_ifa(rt);
932 ifaaddr = rtifa->ifa_addr;
933 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
934 brdaddr = rtifa->ifa_dstaddr;
935 }
936 if ((error = rt_msg2(RTM_GET, &info, 0, w, &size)))
937 return error;
938 if (w->w_where && w->w_tmem && w->w_needed <= 0) {
939 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
940
941 rtm->rtm_flags = rt->rt_flags;
942 rtm->rtm_use = rt->rt_use;
943 cvtmetrics(&rtm->rtm_rmx, &rt->rt_rmx);
944 KASSERT(rt->rt_ifp != NULL);
945 rtm->rtm_index = rt->rt_ifp->if_index;
946 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
947 rtm->rtm_addrs = info.rti_addrs;
948 if ((error = copyout(rtm, w->w_where, size)) != 0)
949 w->w_where = NULL;
950 else
951 w->w_where = (char *)w->w_where + size;
952 }
953 return error;
954 }
955
956 static int
957 sysctl_iflist(int af, struct rt_walkarg *w, int type)
958 {
959 struct ifnet *ifp;
960 struct ifaddr *ifa;
961 struct rt_addrinfo info;
962 int len, error = 0;
963
964 memset(&info, 0, sizeof(info));
965 IFNET_FOREACH(ifp) {
966 if (w->w_arg && w->w_arg != ifp->if_index)
967 continue;
968 if (IFADDR_EMPTY(ifp))
969 continue;
970 ifpaddr = ifp->if_dl->ifa_addr;
971 switch (type) {
972 case NET_RT_IFLIST:
973 error = rt_msg2(RTM_IFINFO, &info, NULL, w, &len);
974 break;
975 #ifdef COMPAT_14
976 case NET_RT_OOIFLIST:
977 error = rt_msg2(RTM_OOIFINFO, &info, NULL, w, &len);
978 break;
979 #endif
980 #ifdef COMPAT_50
981 case NET_RT_OIFLIST:
982 error = rt_msg2(RTM_OIFINFO, &info, NULL, w, &len);
983 break;
984 #endif
985 default:
986 panic("sysctl_iflist(1)");
987 }
988 if (error)
989 return error;
990 ifpaddr = NULL;
991 if (w->w_where && w->w_tmem && w->w_needed <= 0) {
992 switch (type) {
993 case NET_RT_IFLIST: {
994 struct if_msghdr *ifm;
995
996 ifm = (struct if_msghdr *)w->w_tmem;
997 ifm->ifm_index = ifp->if_index;
998 ifm->ifm_flags = ifp->if_flags;
999 ifm->ifm_data = ifp->if_data;
1000 ifm->ifm_addrs = info.rti_addrs;
1001 error = copyout(ifm, w->w_where, len);
1002 if (error)
1003 return error;
1004 w->w_where = (char *)w->w_where + len;
1005 break;
1006 }
1007
1008 #ifdef COMPAT_14
1009 case NET_RT_OOIFLIST:
1010 error = compat_14_iflist(ifp, w, &info, len);
1011 if (error)
1012 return error;
1013 break;
1014 #endif
1015 #ifdef COMPAT_50
1016 case NET_RT_OIFLIST:
1017 error = compat_50_iflist(ifp, w, &info, len);
1018 if (error)
1019 return error;
1020 break;
1021 #endif
1022 default:
1023 panic("sysctl_iflist(2)");
1024 }
1025 }
1026 IFADDR_FOREACH(ifa, ifp) {
1027 if (af && af != ifa->ifa_addr->sa_family)
1028 continue;
1029 ifaaddr = ifa->ifa_addr;
1030 netmask = ifa->ifa_netmask;
1031 brdaddr = ifa->ifa_dstaddr;
1032 if ((error = rt_msg2(RTM_NEWADDR, &info, 0, w, &len)))
1033 return error;
1034 if (w->w_where && w->w_tmem && w->w_needed <= 0) {
1035 struct ifa_msghdr *ifam;
1036
1037 ifam = (struct ifa_msghdr *)w->w_tmem;
1038 ifam->ifam_index = ifa->ifa_ifp->if_index;
1039 ifam->ifam_flags = ifa->ifa_flags;
1040 ifam->ifam_metric = ifa->ifa_metric;
1041 ifam->ifam_addrs = info.rti_addrs;
1042 error = copyout(w->w_tmem, w->w_where, len);
1043 if (error)
1044 return error;
1045 w->w_where = (char *)w->w_where + len;
1046 }
1047 }
1048 ifaaddr = netmask = brdaddr = NULL;
1049 }
1050 return 0;
1051 }
1052
1053 static int
1054 sysctl_rtable(SYSCTLFN_ARGS)
1055 {
1056 void *where = oldp;
1057 size_t *given = oldlenp;
1058 const void *new = newp;
1059 int i, s, error = EINVAL;
1060 u_char af;
1061 struct rt_walkarg w;
1062
1063 if (namelen == 1 && name[0] == CTL_QUERY)
1064 return sysctl_query(SYSCTLFN_CALL(rnode));
1065
1066 if (new)
1067 return EPERM;
1068 if (namelen != 3)
1069 return EINVAL;
1070 af = name[0];
1071 w.w_tmemneeded = 0;
1072 w.w_tmemsize = 0;
1073 w.w_tmem = NULL;
1074 again:
1075 /* we may return here if a later [re]alloc of the t_mem buffer fails */
1076 if (w.w_tmemneeded) {
1077 w.w_tmem = (void *) malloc(w.w_tmemneeded, M_RTABLE, M_WAITOK);
1078 w.w_tmemsize = w.w_tmemneeded;
1079 w.w_tmemneeded = 0;
1080 }
1081 w.w_op = name[1];
1082 w.w_arg = name[2];
1083 w.w_given = *given;
1084 w.w_needed = 0 - w.w_given;
1085 w.w_where = where;
1086
1087 s = splsoftnet();
1088 switch (w.w_op) {
1089
1090 case NET_RT_DUMP:
1091 case NET_RT_FLAGS:
1092 for (i = 1; i <= AF_MAX; i++)
1093 if ((af == 0 || af == i) &&
1094 (error = rt_walktree(i, sysctl_dumpentry, &w)))
1095 break;
1096 break;
1097
1098 #ifdef COMPAT_14
1099 case NET_RT_OOIFLIST:
1100 error = sysctl_iflist(af, &w, w.w_op);
1101 break;
1102 #endif
1103 #ifdef COMPAT_50
1104 case NET_RT_OIFLIST:
1105 error = sysctl_iflist(af, &w, w.w_op);
1106 break;
1107 #endif
1108
1109 case NET_RT_IFLIST:
1110 error = sysctl_iflist(af, &w, w.w_op);
1111 }
1112 splx(s);
1113
1114 /* check to see if we couldn't allocate memory with NOWAIT */
1115 if (error == ENOBUFS && w.w_tmem == 0 && w.w_tmemneeded)
1116 goto again;
1117
1118 if (w.w_tmem)
1119 free(w.w_tmem, M_RTABLE);
1120 w.w_needed += w.w_given;
1121 if (where) {
1122 *given = (char *)w.w_where - (char *)where;
1123 if (*given < w.w_needed)
1124 return ENOMEM;
1125 } else {
1126 *given = (11 * w.w_needed) / 10;
1127 }
1128 return error;
1129 }
1130
1131 /*
1132 * Routing message software interrupt routine
1133 */
1134 static void
1135 route_intr(void *cookie)
1136 {
1137 struct sockproto proto = { .sp_family = PF_ROUTE, };
1138 struct mbuf *m;
1139 int s;
1140
1141 while (!IF_IS_EMPTY(&route_intrq)) {
1142 s = splnet();
1143 IF_DEQUEUE(&route_intrq, m);
1144 splx(s);
1145 if (m == NULL)
1146 break;
1147 proto.sp_protocol = M_GETCTX(m, uintptr_t);
1148 raw_input(m, &proto, &route_src, &route_dst);
1149 }
1150 }
1151
1152 /*
1153 * Enqueue a message to the software interrupt routine.
1154 */
1155 void
1156 route_enqueue(struct mbuf *m, int family)
1157 {
1158 int s, wasempty;
1159
1160 s = splnet();
1161 if (IF_QFULL(&route_intrq)) {
1162 IF_DROP(&route_intrq);
1163 m_freem(m);
1164 } else {
1165 wasempty = IF_IS_EMPTY(&route_intrq);
1166 M_SETCTX(m, (uintptr_t)family);
1167 IF_ENQUEUE(&route_intrq, m);
1168 if (wasempty)
1169 softint_schedule(route_sih);
1170 }
1171 splx(s);
1172 }
1173
1174 void
1175 rt_init(void)
1176 {
1177
1178 route_intrq.ifq_maxlen = route_maxqlen;
1179 route_sih = softint_establish(SOFTINT_NET, route_intr, NULL);
1180 }
1181
1182 /*
1183 * Definitions of protocols supported in the ROUTE domain.
1184 */
1185
1186 const struct protosw routesw[] = {
1187 {
1188 .pr_type = SOCK_RAW,
1189 .pr_domain = &routedomain,
1190 .pr_flags = PR_ATOMIC|PR_ADDR,
1191 .pr_input = raw_input,
1192 .pr_output = route_output,
1193 .pr_ctlinput = raw_ctlinput,
1194 .pr_usrreq = route_usrreq,
1195 .pr_init = raw_init,
1196 },
1197 };
1198
1199 struct domain routedomain = {
1200 .dom_family = PF_ROUTE,
1201 .dom_name = "route",
1202 .dom_init = route_init,
1203 .dom_protosw = routesw,
1204 .dom_protoswNPROTOSW = &routesw[__arraycount(routesw)],
1205 };
1206
1207 SYSCTL_SETUP(sysctl_net_route_setup, "sysctl net.route subtree setup")
1208 {
1209 const struct sysctlnode *rnode = NULL;
1210
1211 sysctl_createv(clog, 0, NULL, NULL,
1212 CTLFLAG_PERMANENT,
1213 CTLTYPE_NODE, "net", NULL,
1214 NULL, 0, NULL, 0,
1215 CTL_NET, CTL_EOL);
1216
1217 sysctl_createv(clog, 0, NULL, &rnode,
1218 CTLFLAG_PERMANENT,
1219 CTLTYPE_NODE, "route",
1220 SYSCTL_DESCR("PF_ROUTE information"),
1221 NULL, 0, NULL, 0,
1222 CTL_NET, PF_ROUTE, CTL_EOL);
1223 sysctl_createv(clog, 0, NULL, NULL,
1224 CTLFLAG_PERMANENT,
1225 CTLTYPE_NODE, "rtable",
1226 SYSCTL_DESCR("Routing table information"),
1227 sysctl_rtable, 0, NULL, 0,
1228 CTL_NET, PF_ROUTE, 0 /* any protocol */, CTL_EOL);
1229 sysctl_createv(clog, 0, &rnode, NULL,
1230 CTLFLAG_PERMANENT,
1231 CTLTYPE_STRUCT, "stats",
1232 SYSCTL_DESCR("Routing statistics"),
1233 NULL, 0, &rtstat, sizeof(rtstat),
1234 CTL_CREATE, CTL_EOL);
1235 }
1236