]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/net/rtsock.c
Merge r260379, r260460.
[FreeBSD/stable/10.git] / sys / net / rtsock.c
1 /*-
2  * Copyright (c) 1988, 1991, 1993
3  *      The Regents of the University of California.  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  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *      @(#)rtsock.c    8.7 (Berkeley) 10/12/95
30  * $FreeBSD$
31  */
32 #include "opt_compat.h"
33 #include "opt_sctp.h"
34 #include "opt_mpath.h"
35 #include "opt_inet.h"
36 #include "opt_inet6.h"
37
38 #include <sys/param.h>
39 #include <sys/jail.h>
40 #include <sys/kernel.h>
41 #include <sys/domain.h>
42 #include <sys/lock.h>
43 #include <sys/malloc.h>
44 #include <sys/mbuf.h>
45 #include <sys/priv.h>
46 #include <sys/proc.h>
47 #include <sys/protosw.h>
48 #include <sys/rwlock.h>
49 #include <sys/signalvar.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/sysctl.h>
53 #include <sys/systm.h>
54
55 #define _IN_NET_RTSOCK_C
56 #include <net/if.h>
57 #include <net/if_dl.h>
58 #include <net/if_llatbl.h>
59 #include <net/if_types.h>
60 #include <net/netisr.h>
61 #include <net/raw_cb.h>
62 #include <net/route.h>
63 #include <net/vnet.h>
64
65 #include <netinet/in.h>
66 #include <netinet/if_ether.h>
67 #include <netinet/ip_carp.h>
68 #ifdef INET6
69 #include <netinet6/ip6_var.h>
70 #include <netinet6/scope6_var.h>
71 #endif
72
73 #if defined(INET) || defined(INET6)
74 #ifdef SCTP
75 extern void sctp_addr_change(struct ifaddr *ifa, int cmd);
76 #endif /* SCTP */
77 #endif
78
79 #ifdef COMPAT_FREEBSD32
80 #include <sys/mount.h>
81 #include <compat/freebsd32/freebsd32.h>
82
83 struct if_data32 {
84         uint8_t ifi_type;
85         uint8_t ifi_physical;
86         uint8_t ifi_addrlen;
87         uint8_t ifi_hdrlen;
88         uint8_t ifi_link_state;
89         uint8_t ifi_vhid;
90         uint8_t ifi_baudrate_pf;
91         uint8_t ifi_datalen;
92         uint32_t ifi_mtu;
93         uint32_t ifi_metric;
94         uint32_t ifi_baudrate;
95         uint32_t ifi_ipackets;
96         uint32_t ifi_ierrors;
97         uint32_t ifi_opackets;
98         uint32_t ifi_oerrors;
99         uint32_t ifi_collisions;
100         uint32_t ifi_ibytes;
101         uint32_t ifi_obytes;
102         uint32_t ifi_imcasts;
103         uint32_t ifi_omcasts;
104         uint32_t ifi_iqdrops;
105         uint32_t ifi_noproto;
106         uint32_t ifi_hwassist;
107         int32_t ifi_epoch;
108         struct  timeval32 ifi_lastchange;
109         uint32_t ifi_oqdrops;
110 };
111
112 struct if_msghdr32 {
113         uint16_t ifm_msglen;
114         uint8_t ifm_version;
115         uint8_t ifm_type;
116         int32_t ifm_addrs;
117         int32_t ifm_flags;
118         uint16_t ifm_index;
119         struct  if_data32 ifm_data;
120 };
121
122 struct if_msghdrl32 {
123         uint16_t ifm_msglen;
124         uint8_t ifm_version;
125         uint8_t ifm_type;
126         int32_t ifm_addrs;
127         int32_t ifm_flags;
128         uint16_t ifm_index;
129         uint16_t _ifm_spare1;
130         uint16_t ifm_len;
131         uint16_t ifm_data_off;
132         struct  if_data32 ifm_data;
133 };
134
135 struct ifa_msghdrl32 {
136         uint16_t ifam_msglen;
137         uint8_t ifam_version;
138         uint8_t ifam_type;
139         int32_t ifam_addrs;
140         int32_t ifam_flags;
141         uint16_t ifam_index;
142         uint16_t _ifam_spare1;
143         uint16_t ifam_len;
144         uint16_t ifam_data_off;
145         int32_t ifam_metric;
146         struct  if_data32 ifam_data;
147 };
148 #endif /* COMPAT_FREEBSD32 */
149
150 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
151
152 /* NB: these are not modified */
153 static struct   sockaddr route_src = { 2, PF_ROUTE, };
154 static struct   sockaddr sa_zero   = { sizeof(sa_zero), AF_INET, };
155
156 /* These are external hooks for CARP. */
157 int     (*carp_get_vhid_p)(struct ifaddr *);
158
159 /*
160  * Used by rtsock/raw_input callback code to decide whether to filter the update
161  * notification to a socket bound to a particular FIB.
162  */
163 #define RTS_FILTER_FIB  M_PROTO8
164
165 static struct {
166         int     ip_count;       /* attached w/ AF_INET */
167         int     ip6_count;      /* attached w/ AF_INET6 */
168         int     ipx_count;      /* attached w/ AF_IPX */
169         int     any_count;      /* total attached */
170 } route_cb;
171
172 struct mtx rtsock_mtx;
173 MTX_SYSINIT(rtsock, &rtsock_mtx, "rtsock route_cb lock", MTX_DEF);
174
175 #define RTSOCK_LOCK()   mtx_lock(&rtsock_mtx)
176 #define RTSOCK_UNLOCK() mtx_unlock(&rtsock_mtx)
177 #define RTSOCK_LOCK_ASSERT()    mtx_assert(&rtsock_mtx, MA_OWNED)
178
179 static SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RD, 0, "");
180
181 struct walkarg {
182         int     w_tmemsize;
183         int     w_op, w_arg;
184         caddr_t w_tmem;
185         struct sysctl_req *w_req;
186 };
187
188 static void     rts_input(struct mbuf *m);
189 static struct mbuf *rt_msg1(int type, struct rt_addrinfo *rtinfo);
190 static int      rt_msg2(int type, struct rt_addrinfo *rtinfo,
191                         caddr_t cp, struct walkarg *w);
192 static int      rt_xaddrs(caddr_t cp, caddr_t cplim,
193                         struct rt_addrinfo *rtinfo);
194 static int      sysctl_dumpentry(struct radix_node *rn, void *vw);
195 static int      sysctl_iflist(int af, struct walkarg *w);
196 static int      sysctl_ifmalist(int af, struct walkarg *w);
197 static int      route_output(struct mbuf *m, struct socket *so);
198 static void     rt_setmetrics(const struct rt_msghdr *rtm, struct rtentry *rt);
199 static void     rt_getmetrics(const struct rtentry *rt, struct rt_metrics *out);
200 static void     rt_dispatch(struct mbuf *, sa_family_t);
201
202 static struct netisr_handler rtsock_nh = {
203         .nh_name = "rtsock",
204         .nh_handler = rts_input,
205         .nh_proto = NETISR_ROUTE,
206         .nh_policy = NETISR_POLICY_SOURCE,
207 };
208
209 static int
210 sysctl_route_netisr_maxqlen(SYSCTL_HANDLER_ARGS)
211 {
212         int error, qlimit;
213
214         netisr_getqlimit(&rtsock_nh, &qlimit);
215         error = sysctl_handle_int(oidp, &qlimit, 0, req);
216         if (error || !req->newptr)
217                 return (error);
218         if (qlimit < 1)
219                 return (EINVAL);
220         return (netisr_setqlimit(&rtsock_nh, qlimit));
221 }
222 SYSCTL_PROC(_net_route, OID_AUTO, netisr_maxqlen, CTLTYPE_INT|CTLFLAG_RW,
223     0, 0, sysctl_route_netisr_maxqlen, "I",
224     "maximum routing socket dispatch queue length");
225
226 static void
227 rts_init(void)
228 {
229         int tmp;
230
231         if (TUNABLE_INT_FETCH("net.route.netisr_maxqlen", &tmp))
232                 rtsock_nh.nh_qlimit = tmp;
233         netisr_register(&rtsock_nh);
234 }
235 SYSINIT(rtsock, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, rts_init, 0);
236
237 static int
238 raw_input_rts_cb(struct mbuf *m, struct sockproto *proto, struct sockaddr *src,
239     struct rawcb *rp)
240 {
241         int fibnum;
242
243         KASSERT(m != NULL, ("%s: m is NULL", __func__));
244         KASSERT(proto != NULL, ("%s: proto is NULL", __func__));
245         KASSERT(rp != NULL, ("%s: rp is NULL", __func__));
246
247         /* No filtering requested. */
248         if ((m->m_flags & RTS_FILTER_FIB) == 0)
249                 return (0);
250
251         /* Check if it is a rts and the fib matches the one of the socket. */
252         fibnum = M_GETFIB(m);
253         if (proto->sp_family != PF_ROUTE ||
254             rp->rcb_socket == NULL ||
255             rp->rcb_socket->so_fibnum == fibnum)
256                 return (0);
257
258         /* Filtering requested and no match, the socket shall be skipped. */
259         return (1);
260 }
261
262 static void
263 rts_input(struct mbuf *m)
264 {
265         struct sockproto route_proto;
266         unsigned short *family;
267         struct m_tag *tag;
268
269         route_proto.sp_family = PF_ROUTE;
270         tag = m_tag_find(m, PACKET_TAG_RTSOCKFAM, NULL);
271         if (tag != NULL) {
272                 family = (unsigned short *)(tag + 1);
273                 route_proto.sp_protocol = *family;
274                 m_tag_delete(m, tag);
275         } else
276                 route_proto.sp_protocol = 0;
277
278         raw_input_ext(m, &route_proto, &route_src, raw_input_rts_cb);
279 }
280
281 /*
282  * It really doesn't make any sense at all for this code to share much
283  * with raw_usrreq.c, since its functionality is so restricted.  XXX
284  */
285 static void
286 rts_abort(struct socket *so)
287 {
288
289         raw_usrreqs.pru_abort(so);
290 }
291
292 static void
293 rts_close(struct socket *so)
294 {
295
296         raw_usrreqs.pru_close(so);
297 }
298
299 /* pru_accept is EOPNOTSUPP */
300
301 static int
302 rts_attach(struct socket *so, int proto, struct thread *td)
303 {
304         struct rawcb *rp;
305         int error;
306
307         KASSERT(so->so_pcb == NULL, ("rts_attach: so_pcb != NULL"));
308
309         /* XXX */
310         rp = malloc(sizeof *rp, M_PCB, M_WAITOK | M_ZERO);
311         if (rp == NULL)
312                 return ENOBUFS;
313
314         so->so_pcb = (caddr_t)rp;
315         so->so_fibnum = td->td_proc->p_fibnum;
316         error = raw_attach(so, proto);
317         rp = sotorawcb(so);
318         if (error) {
319                 so->so_pcb = NULL;
320                 free(rp, M_PCB);
321                 return error;
322         }
323         RTSOCK_LOCK();
324         switch(rp->rcb_proto.sp_protocol) {
325         case AF_INET:
326                 route_cb.ip_count++;
327                 break;
328         case AF_INET6:
329                 route_cb.ip6_count++;
330                 break;
331         case AF_IPX:
332                 route_cb.ipx_count++;
333                 break;
334         }
335         route_cb.any_count++;
336         RTSOCK_UNLOCK();
337         soisconnected(so);
338         so->so_options |= SO_USELOOPBACK;
339         return 0;
340 }
341
342 static int
343 rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
344 {
345
346         return (raw_usrreqs.pru_bind(so, nam, td)); /* xxx just EINVAL */
347 }
348
349 static int
350 rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
351 {
352
353         return (raw_usrreqs.pru_connect(so, nam, td)); /* XXX just EINVAL */
354 }
355
356 /* pru_connect2 is EOPNOTSUPP */
357 /* pru_control is EOPNOTSUPP */
358
359 static void
360 rts_detach(struct socket *so)
361 {
362         struct rawcb *rp = sotorawcb(so);
363
364         KASSERT(rp != NULL, ("rts_detach: rp == NULL"));
365
366         RTSOCK_LOCK();
367         switch(rp->rcb_proto.sp_protocol) {
368         case AF_INET:
369                 route_cb.ip_count--;
370                 break;
371         case AF_INET6:
372                 route_cb.ip6_count--;
373                 break;
374         case AF_IPX:
375                 route_cb.ipx_count--;
376                 break;
377         }
378         route_cb.any_count--;
379         RTSOCK_UNLOCK();
380         raw_usrreqs.pru_detach(so);
381 }
382
383 static int
384 rts_disconnect(struct socket *so)
385 {
386
387         return (raw_usrreqs.pru_disconnect(so));
388 }
389
390 /* pru_listen is EOPNOTSUPP */
391
392 static int
393 rts_peeraddr(struct socket *so, struct sockaddr **nam)
394 {
395
396         return (raw_usrreqs.pru_peeraddr(so, nam));
397 }
398
399 /* pru_rcvd is EOPNOTSUPP */
400 /* pru_rcvoob is EOPNOTSUPP */
401
402 static int
403 rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
404          struct mbuf *control, struct thread *td)
405 {
406
407         return (raw_usrreqs.pru_send(so, flags, m, nam, control, td));
408 }
409
410 /* pru_sense is null */
411
412 static int
413 rts_shutdown(struct socket *so)
414 {
415
416         return (raw_usrreqs.pru_shutdown(so));
417 }
418
419 static int
420 rts_sockaddr(struct socket *so, struct sockaddr **nam)
421 {
422
423         return (raw_usrreqs.pru_sockaddr(so, nam));
424 }
425
426 static struct pr_usrreqs route_usrreqs = {
427         .pru_abort =            rts_abort,
428         .pru_attach =           rts_attach,
429         .pru_bind =             rts_bind,
430         .pru_connect =          rts_connect,
431         .pru_detach =           rts_detach,
432         .pru_disconnect =       rts_disconnect,
433         .pru_peeraddr =         rts_peeraddr,
434         .pru_send =             rts_send,
435         .pru_shutdown =         rts_shutdown,
436         .pru_sockaddr =         rts_sockaddr,
437         .pru_close =            rts_close,
438 };
439
440 #ifndef _SOCKADDR_UNION_DEFINED
441 #define _SOCKADDR_UNION_DEFINED
442 /*
443  * The union of all possible address formats we handle.
444  */
445 union sockaddr_union {
446         struct sockaddr         sa;
447         struct sockaddr_in      sin;
448         struct sockaddr_in6     sin6;
449 };
450 #endif /* _SOCKADDR_UNION_DEFINED */
451
452 static int
453 rtm_get_jailed(struct rt_addrinfo *info, struct ifnet *ifp,
454     struct rtentry *rt, union sockaddr_union *saun, struct ucred *cred)
455 {
456
457         /* First, see if the returned address is part of the jail. */
458         if (prison_if(cred, rt->rt_ifa->ifa_addr) == 0) {
459                 info->rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
460                 return (0);
461         }
462
463         switch (info->rti_info[RTAX_DST]->sa_family) {
464 #ifdef INET
465         case AF_INET:
466         {
467                 struct in_addr ia;
468                 struct ifaddr *ifa;
469                 int found;
470
471                 found = 0;
472                 /*
473                  * Try to find an address on the given outgoing interface
474                  * that belongs to the jail.
475                  */
476                 IF_ADDR_RLOCK(ifp);
477                 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
478                         struct sockaddr *sa;
479                         sa = ifa->ifa_addr;
480                         if (sa->sa_family != AF_INET)
481                                 continue;
482                         ia = ((struct sockaddr_in *)sa)->sin_addr;
483                         if (prison_check_ip4(cred, &ia) == 0) {
484                                 found = 1;
485                                 break;
486                         }
487                 }
488                 IF_ADDR_RUNLOCK(ifp);
489                 if (!found) {
490                         /*
491                          * As a last resort return the 'default' jail address.
492                          */
493                         ia = ((struct sockaddr_in *)rt->rt_ifa->ifa_addr)->
494                             sin_addr;
495                         if (prison_get_ip4(cred, &ia) != 0)
496                                 return (ESRCH);
497                 }
498                 bzero(&saun->sin, sizeof(struct sockaddr_in));
499                 saun->sin.sin_len = sizeof(struct sockaddr_in);
500                 saun->sin.sin_family = AF_INET;
501                 saun->sin.sin_addr.s_addr = ia.s_addr;
502                 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin;
503                 break;
504         }
505 #endif
506 #ifdef INET6
507         case AF_INET6:
508         {
509                 struct in6_addr ia6;
510                 struct ifaddr *ifa;
511                 int found;
512
513                 found = 0;
514                 /*
515                  * Try to find an address on the given outgoing interface
516                  * that belongs to the jail.
517                  */
518                 IF_ADDR_RLOCK(ifp);
519                 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
520                         struct sockaddr *sa;
521                         sa = ifa->ifa_addr;
522                         if (sa->sa_family != AF_INET6)
523                                 continue;
524                         bcopy(&((struct sockaddr_in6 *)sa)->sin6_addr,
525                             &ia6, sizeof(struct in6_addr));
526                         if (prison_check_ip6(cred, &ia6) == 0) {
527                                 found = 1;
528                                 break;
529                         }
530                 }
531                 IF_ADDR_RUNLOCK(ifp);
532                 if (!found) {
533                         /*
534                          * As a last resort return the 'default' jail address.
535                          */
536                         ia6 = ((struct sockaddr_in6 *)rt->rt_ifa->ifa_addr)->
537                             sin6_addr;
538                         if (prison_get_ip6(cred, &ia6) != 0)
539                                 return (ESRCH);
540                 }
541                 bzero(&saun->sin6, sizeof(struct sockaddr_in6));
542                 saun->sin6.sin6_len = sizeof(struct sockaddr_in6);
543                 saun->sin6.sin6_family = AF_INET6;
544                 bcopy(&ia6, &saun->sin6.sin6_addr, sizeof(struct in6_addr));
545                 if (sa6_recoverscope(&saun->sin6) != 0)
546                         return (ESRCH);
547                 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin6;
548                 break;
549         }
550 #endif
551         default:
552                 return (ESRCH);
553         }
554         return (0);
555 }
556
557 /*ARGSUSED*/
558 static int
559 route_output(struct mbuf *m, struct socket *so)
560 {
561 #define sa_equal(a1, a2) (bcmp((a1), (a2), (a1)->sa_len) == 0)
562         struct rt_msghdr *rtm = NULL;
563         struct rtentry *rt = NULL;
564         struct radix_node_head *rnh;
565         struct rt_addrinfo info;
566 #ifdef INET6
567         struct sockaddr_storage ss;
568         struct sockaddr_in6 *sin6;
569         int i, rti_need_deembed = 0;
570 #endif
571         int len, error = 0;
572         struct ifnet *ifp = NULL;
573         union sockaddr_union saun;
574         sa_family_t saf = AF_UNSPEC;
575
576 #define senderr(e) { error = e; goto flush;}
577         if (m == NULL || ((m->m_len < sizeof(long)) &&
578                        (m = m_pullup(m, sizeof(long))) == NULL))
579                 return (ENOBUFS);
580         if ((m->m_flags & M_PKTHDR) == 0)
581                 panic("route_output");
582         len = m->m_pkthdr.len;
583         if (len < sizeof(*rtm) ||
584             len != mtod(m, struct rt_msghdr *)->rtm_msglen) {
585                 info.rti_info[RTAX_DST] = NULL;
586                 senderr(EINVAL);
587         }
588         R_Malloc(rtm, struct rt_msghdr *, len);
589         if (rtm == NULL) {
590                 info.rti_info[RTAX_DST] = NULL;
591                 senderr(ENOBUFS);
592         }
593         m_copydata(m, 0, len, (caddr_t)rtm);
594         if (rtm->rtm_version != RTM_VERSION) {
595                 info.rti_info[RTAX_DST] = NULL;
596                 senderr(EPROTONOSUPPORT);
597         }
598         rtm->rtm_pid = curproc->p_pid;
599         bzero(&info, sizeof(info));
600         info.rti_addrs = rtm->rtm_addrs;
601         /*
602          * rt_xaddrs() performs s6_addr[2] := sin6_scope_id for AF_INET6
603          * link-local address because rtrequest requires addresses with
604          * embedded scope id.
605          */
606         if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info)) {
607                 info.rti_info[RTAX_DST] = NULL;
608                 senderr(EINVAL);
609         }
610         info.rti_flags = rtm->rtm_flags;
611         if (info.rti_info[RTAX_DST] == NULL ||
612             info.rti_info[RTAX_DST]->sa_family >= AF_MAX ||
613             (info.rti_info[RTAX_GATEWAY] != NULL &&
614              info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX))
615                 senderr(EINVAL);
616         saf = info.rti_info[RTAX_DST]->sa_family;
617         /*
618          * Verify that the caller has the appropriate privilege; RTM_GET
619          * is the only operation the non-superuser is allowed.
620          */
621         if (rtm->rtm_type != RTM_GET) {
622                 error = priv_check(curthread, PRIV_NET_ROUTE);
623                 if (error)
624                         senderr(error);
625         }
626
627         /*
628          * The given gateway address may be an interface address.
629          * For example, issuing a "route change" command on a route
630          * entry that was created from a tunnel, and the gateway
631          * address given is the local end point. In this case the 
632          * RTF_GATEWAY flag must be cleared or the destination will
633          * not be reachable even though there is no error message.
634          */
635         if (info.rti_info[RTAX_GATEWAY] != NULL &&
636             info.rti_info[RTAX_GATEWAY]->sa_family != AF_LINK) {
637                 struct route gw_ro;
638
639                 bzero(&gw_ro, sizeof(gw_ro));
640                 gw_ro.ro_dst = *info.rti_info[RTAX_GATEWAY];
641                 rtalloc_ign_fib(&gw_ro, 0, so->so_fibnum);
642                 /* 
643                  * A host route through the loopback interface is 
644                  * installed for each interface adddress. In pre 8.0
645                  * releases the interface address of a PPP link type
646                  * is not reachable locally. This behavior is fixed as 
647                  * part of the new L2/L3 redesign and rewrite work. The
648                  * signature of this interface address route is the
649                  * AF_LINK sa_family type of the rt_gateway, and the
650                  * rt_ifp has the IFF_LOOPBACK flag set.
651                  */
652                 if (gw_ro.ro_rt != NULL &&
653                     gw_ro.ro_rt->rt_gateway->sa_family == AF_LINK &&
654                     gw_ro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) {
655                         info.rti_flags &= ~RTF_GATEWAY;
656                         info.rti_flags |= RTF_GWFLAG_COMPAT;
657                 }
658                 if (gw_ro.ro_rt != NULL)
659                         RTFREE(gw_ro.ro_rt);
660         }
661
662         switch (rtm->rtm_type) {
663                 struct rtentry *saved_nrt;
664
665         case RTM_ADD:
666                 if (info.rti_info[RTAX_GATEWAY] == NULL)
667                         senderr(EINVAL);
668                 saved_nrt = NULL;
669
670                 /* support for new ARP code */
671                 if (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK &&
672                     (rtm->rtm_flags & RTF_LLDATA) != 0) {
673                         error = lla_rt_output(rtm, &info);
674 #ifdef INET6
675                         if (error == 0)
676                                 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
677 #endif
678                         break;
679                 }
680                 error = rtrequest1_fib(RTM_ADD, &info, &saved_nrt,
681                     so->so_fibnum);
682                 if (error == 0 && saved_nrt) {
683 #ifdef INET6
684                         rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
685 #endif
686                         RT_LOCK(saved_nrt);
687                         rt_setmetrics(rtm, saved_nrt);
688                         rtm->rtm_index = saved_nrt->rt_ifp->if_index;
689                         RT_REMREF(saved_nrt);
690                         RT_UNLOCK(saved_nrt);
691                 }
692                 break;
693
694         case RTM_DELETE:
695                 saved_nrt = NULL;
696                 /* support for new ARP code */
697                 if (info.rti_info[RTAX_GATEWAY] && 
698                     (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) &&
699                     (rtm->rtm_flags & RTF_LLDATA) != 0) {
700                         error = lla_rt_output(rtm, &info);
701 #ifdef INET6
702                         if (error == 0)
703                                 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
704 #endif
705                         break;
706                 }
707                 error = rtrequest1_fib(RTM_DELETE, &info, &saved_nrt,
708                     so->so_fibnum);
709                 if (error == 0) {
710                         RT_LOCK(saved_nrt);
711                         rt = saved_nrt;
712                         goto report;
713                 }
714 #ifdef INET6
715                 /* rt_msg2() will not be used when RTM_DELETE fails. */
716                 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
717 #endif
718                 break;
719
720         case RTM_GET:
721         case RTM_CHANGE:
722         case RTM_LOCK:
723                 rnh = rt_tables_get_rnh(so->so_fibnum,
724                     info.rti_info[RTAX_DST]->sa_family);
725                 if (rnh == NULL)
726                         senderr(EAFNOSUPPORT);
727
728                 RADIX_NODE_HEAD_RLOCK(rnh);
729
730                 if (info.rti_info[RTAX_NETMASK] == NULL &&
731                     rtm->rtm_type == RTM_GET) {
732                         /*
733                          * Provide logest prefix match for
734                          * address lookup (no mask).
735                          * 'route -n get addr'
736                          */
737                         rt = (struct rtentry *) rnh->rnh_matchaddr(
738                             info.rti_info[RTAX_DST], rnh);
739                 } else
740                         rt = (struct rtentry *) rnh->rnh_lookup(
741                             info.rti_info[RTAX_DST],
742                             info.rti_info[RTAX_NETMASK], rnh);
743
744                 if (rt == NULL) {
745                         RADIX_NODE_HEAD_RUNLOCK(rnh);
746                         senderr(ESRCH);
747                 }
748 #ifdef RADIX_MPATH
749                 /*
750                  * for RTM_CHANGE/LOCK, if we got multipath routes,
751                  * we require users to specify a matching RTAX_GATEWAY.
752                  *
753                  * for RTM_GET, gate is optional even with multipath.
754                  * if gate == NULL the first match is returned.
755                  * (no need to call rt_mpath_matchgate if gate == NULL)
756                  */
757                 if (rn_mpath_capable(rnh) &&
758                     (rtm->rtm_type != RTM_GET || info.rti_info[RTAX_GATEWAY])) {
759                         rt = rt_mpath_matchgate(rt, info.rti_info[RTAX_GATEWAY]);
760                         if (!rt) {
761                                 RADIX_NODE_HEAD_RUNLOCK(rnh);
762                                 senderr(ESRCH);
763                         }
764                 }
765 #endif
766                 /*
767                  * If performing proxied L2 entry insertion, and
768                  * the actual PPP host entry is found, perform
769                  * another search to retrieve the prefix route of
770                  * the local end point of the PPP link.
771                  */
772                 if (rtm->rtm_flags & RTF_ANNOUNCE) {
773                         struct sockaddr laddr;
774
775                         if (rt->rt_ifp != NULL && 
776                             rt->rt_ifp->if_type == IFT_PROPVIRTUAL) {
777                                 struct ifaddr *ifa;
778
779                                 ifa = ifa_ifwithnet(info.rti_info[RTAX_DST], 1);
780                                 if (ifa != NULL)
781                                         rt_maskedcopy(ifa->ifa_addr,
782                                                       &laddr,
783                                                       ifa->ifa_netmask);
784                         } else
785                                 rt_maskedcopy(rt->rt_ifa->ifa_addr,
786                                               &laddr,
787                                               rt->rt_ifa->ifa_netmask);
788                         /* 
789                          * refactor rt and no lock operation necessary
790                          */
791                         rt = (struct rtentry *)rnh->rnh_matchaddr(&laddr, rnh);
792                         if (rt == NULL) {
793                                 RADIX_NODE_HEAD_RUNLOCK(rnh);
794                                 senderr(ESRCH);
795                         }
796                 } 
797                 RT_LOCK(rt);
798                 RT_ADDREF(rt);
799                 RADIX_NODE_HEAD_RUNLOCK(rnh);
800
801                 switch(rtm->rtm_type) {
802
803                 case RTM_GET:
804                 report:
805                         RT_LOCK_ASSERT(rt);
806                         if ((rt->rt_flags & RTF_HOST) == 0
807                             ? jailed_without_vnet(curthread->td_ucred)
808                             : prison_if(curthread->td_ucred,
809                             rt_key(rt)) != 0) {
810                                 RT_UNLOCK(rt);
811                                 senderr(ESRCH);
812                         }
813                         info.rti_info[RTAX_DST] = rt_key(rt);
814                         info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
815                         info.rti_info[RTAX_NETMASK] = rt_mask(rt);
816                         info.rti_info[RTAX_GENMASK] = 0;
817                         if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
818                                 ifp = rt->rt_ifp;
819                                 if (ifp) {
820                                         info.rti_info[RTAX_IFP] =
821                                             ifp->if_addr->ifa_addr;
822                                         error = rtm_get_jailed(&info, ifp, rt,
823                                             &saun, curthread->td_ucred);
824                                         if (error != 0) {
825                                                 RT_UNLOCK(rt);
826                                                 senderr(error);
827                                         }
828                                         if (ifp->if_flags & IFF_POINTOPOINT)
829                                                 info.rti_info[RTAX_BRD] =
830                                                     rt->rt_ifa->ifa_dstaddr;
831                                         rtm->rtm_index = ifp->if_index;
832                                 } else {
833                                         info.rti_info[RTAX_IFP] = NULL;
834                                         info.rti_info[RTAX_IFA] = NULL;
835                                 }
836                         } else if ((ifp = rt->rt_ifp) != NULL) {
837                                 rtm->rtm_index = ifp->if_index;
838                         }
839                         len = rt_msg2(rtm->rtm_type, &info, NULL, NULL);
840                         if (len > rtm->rtm_msglen) {
841                                 struct rt_msghdr *new_rtm;
842                                 R_Malloc(new_rtm, struct rt_msghdr *, len);
843                                 if (new_rtm == NULL) {
844                                         RT_UNLOCK(rt);
845                                         senderr(ENOBUFS);
846                                 }
847                                 bcopy(rtm, new_rtm, rtm->rtm_msglen);
848                                 Free(rtm); rtm = new_rtm;
849                         }
850                         (void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm, NULL);
851                         if (rt->rt_flags & RTF_GWFLAG_COMPAT)
852                                 rtm->rtm_flags = RTF_GATEWAY | 
853                                         (rt->rt_flags & ~RTF_GWFLAG_COMPAT);
854                         else
855                                 rtm->rtm_flags = rt->rt_flags;
856                         rt_getmetrics(rt, &rtm->rtm_rmx);
857                         rtm->rtm_addrs = info.rti_addrs;
858                         break;
859
860                 case RTM_CHANGE:
861                         /*
862                          * New gateway could require new ifaddr, ifp;
863                          * flags may also be different; ifp may be specified
864                          * by ll sockaddr when protocol address is ambiguous
865                          */
866                         if (((rt->rt_flags & RTF_GATEWAY) &&
867                              info.rti_info[RTAX_GATEWAY] != NULL) ||
868                             info.rti_info[RTAX_IFP] != NULL ||
869                             (info.rti_info[RTAX_IFA] != NULL &&
870                              !sa_equal(info.rti_info[RTAX_IFA],
871                                        rt->rt_ifa->ifa_addr))) {
872                                 RT_UNLOCK(rt);
873                                 RADIX_NODE_HEAD_LOCK(rnh);
874                                 error = rt_getifa_fib(&info, rt->rt_fibnum);
875                                 /*
876                                  * XXXRW: Really we should release this
877                                  * reference later, but this maintains
878                                  * historical behavior.
879                                  */
880                                 if (info.rti_ifa != NULL)
881                                         ifa_free(info.rti_ifa);
882                                 RADIX_NODE_HEAD_UNLOCK(rnh);
883                                 if (error != 0)
884                                         senderr(error);
885                                 RT_LOCK(rt);
886                         }
887                         if (info.rti_ifa != NULL &&
888                             info.rti_ifa != rt->rt_ifa &&
889                             rt->rt_ifa != NULL &&
890                             rt->rt_ifa->ifa_rtrequest != NULL) {
891                                 rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt,
892                                     &info);
893                                 ifa_free(rt->rt_ifa);
894                         }
895                         if (info.rti_info[RTAX_GATEWAY] != NULL) {
896                                 RT_UNLOCK(rt);
897                                 RADIX_NODE_HEAD_LOCK(rnh);
898                                 RT_LOCK(rt);
899                                 
900                                 error = rt_setgate(rt, rt_key(rt),
901                                     info.rti_info[RTAX_GATEWAY]);
902                                 RADIX_NODE_HEAD_UNLOCK(rnh);
903                                 if (error != 0) {
904                                         RT_UNLOCK(rt);
905                                         senderr(error);
906                                 }
907                                 rt->rt_flags &= ~RTF_GATEWAY;
908                                 rt->rt_flags |= (RTF_GATEWAY & info.rti_flags);
909                         }
910                         if (info.rti_ifa != NULL &&
911                             info.rti_ifa != rt->rt_ifa) {
912                                 ifa_ref(info.rti_ifa);
913                                 rt->rt_ifa = info.rti_ifa;
914                                 rt->rt_ifp = info.rti_ifp;
915                         }
916                         /* Allow some flags to be toggled on change. */
917                         rt->rt_flags = (rt->rt_flags & ~RTF_FMASK) |
918                                     (rtm->rtm_flags & RTF_FMASK);
919                         rt_setmetrics(rtm, rt);
920                         rtm->rtm_index = rt->rt_ifp->if_index;
921                         if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
922                                rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info);
923                         /* FALLTHROUGH */
924                 case RTM_LOCK:
925                         /* We don't support locks anymore */
926                         break;
927                 }
928                 RT_UNLOCK(rt);
929                 break;
930
931         default:
932                 senderr(EOPNOTSUPP);
933         }
934
935 flush:
936         if (rtm) {
937                 if (error)
938                         rtm->rtm_errno = error;
939                 else
940                         rtm->rtm_flags |= RTF_DONE;
941         }
942         if (rt)         /* XXX can this be true? */
943                 RTFREE(rt);
944     {
945         struct rawcb *rp = NULL;
946         /*
947          * Check to see if we don't want our own messages.
948          */
949         if ((so->so_options & SO_USELOOPBACK) == 0) {
950                 if (route_cb.any_count <= 1) {
951                         if (rtm)
952                                 Free(rtm);
953                         m_freem(m);
954                         return (error);
955                 }
956                 /* There is another listener, so construct message */
957                 rp = sotorawcb(so);
958         }
959         if (rtm) {
960 #ifdef INET6
961                 if (rti_need_deembed) {
962                         /* sin6_scope_id is recovered before sending rtm. */
963                         sin6 = (struct sockaddr_in6 *)&ss;
964                         for (i = 0; i < RTAX_MAX; i++) {
965                                 if (info.rti_info[i] == NULL)
966                                         continue;
967                                 if (info.rti_info[i]->sa_family != AF_INET6)
968                                         continue;
969                                 bcopy(info.rti_info[i], sin6, sizeof(*sin6));
970                                 if (sa6_recoverscope(sin6) == 0)
971                                         bcopy(sin6, info.rti_info[i],
972                                                     sizeof(*sin6));
973                         }
974                 }
975 #endif
976                 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
977                 if (m->m_pkthdr.len < rtm->rtm_msglen) {
978                         m_freem(m);
979                         m = NULL;
980                 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
981                         m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
982         }
983         if (m) {
984                 M_SETFIB(m, so->so_fibnum);
985                 m->m_flags |= RTS_FILTER_FIB;
986                 if (rp) {
987                         /*
988                          * XXX insure we don't get a copy by
989                          * invalidating our protocol
990                          */
991                         unsigned short family = rp->rcb_proto.sp_family;
992                         rp->rcb_proto.sp_family = 0;
993                         rt_dispatch(m, saf);
994                         rp->rcb_proto.sp_family = family;
995                 } else
996                         rt_dispatch(m, saf);
997         }
998         /* info.rti_info[RTAX_DST] (used above) can point inside of rtm */
999         if (rtm)
1000                 Free(rtm);
1001     }
1002         return (error);
1003 #undef  sa_equal
1004 }
1005
1006 static void
1007 rt_setmetrics(const struct rt_msghdr *rtm, struct rtentry *rt)
1008 {
1009
1010         if (rtm->rtm_inits & RTV_MTU)
1011                 rt->rt_mtu = rtm->rtm_rmx.rmx_mtu;
1012         if (rtm->rtm_inits & RTV_WEIGHT)
1013                 rt->rt_weight = rtm->rtm_rmx.rmx_weight;
1014         /* Kernel -> userland timebase conversion. */
1015         if (rtm->rtm_inits & RTV_EXPIRE)
1016                 rt->rt_expire = rtm->rtm_rmx.rmx_expire ?
1017                     rtm->rtm_rmx.rmx_expire - time_second + time_uptime : 0;
1018 }
1019
1020 static void
1021 rt_getmetrics(const struct rtentry *rt, struct rt_metrics *out)
1022 {
1023
1024         bzero(out, sizeof(*out));
1025         out->rmx_mtu = rt->rt_mtu;
1026         out->rmx_weight = rt->rt_weight;
1027         out->rmx_pksent = counter_u64_fetch(rt->rt_pksent);
1028         /* Kernel -> userland timebase conversion. */
1029         out->rmx_expire = rt->rt_expire ?
1030             rt->rt_expire - time_uptime + time_second : 0;
1031 }
1032
1033 /*
1034  * Extract the addresses of the passed sockaddrs.
1035  * Do a little sanity checking so as to avoid bad memory references.
1036  * This data is derived straight from userland.
1037  */
1038 static int
1039 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
1040 {
1041         struct sockaddr *sa;
1042         int i;
1043
1044         for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
1045                 if ((rtinfo->rti_addrs & (1 << i)) == 0)
1046                         continue;
1047                 sa = (struct sockaddr *)cp;
1048                 /*
1049                  * It won't fit.
1050                  */
1051                 if (cp + sa->sa_len > cplim)
1052                         return (EINVAL);
1053                 /*
1054                  * there are no more.. quit now
1055                  * If there are more bits, they are in error.
1056                  * I've seen this. route(1) can evidently generate these. 
1057                  * This causes kernel to core dump.
1058                  * for compatibility, If we see this, point to a safe address.
1059                  */
1060                 if (sa->sa_len == 0) {
1061                         rtinfo->rti_info[i] = &sa_zero;
1062                         return (0); /* should be EINVAL but for compat */
1063                 }
1064                 /* accept it */
1065 #ifdef INET6
1066                 if (sa->sa_family == AF_INET6)
1067                         sa6_embedscope((struct sockaddr_in6 *)sa,
1068                             V_ip6_use_defzone);
1069 #endif
1070                 rtinfo->rti_info[i] = sa;
1071                 cp += SA_SIZE(sa);
1072         }
1073         return (0);
1074 }
1075
1076 /*
1077  * Used by the routing socket.
1078  */
1079 static struct mbuf *
1080 rt_msg1(int type, struct rt_addrinfo *rtinfo)
1081 {
1082         struct rt_msghdr *rtm;
1083         struct mbuf *m;
1084         int i;
1085         struct sockaddr *sa;
1086 #ifdef INET6
1087         struct sockaddr_storage ss;
1088         struct sockaddr_in6 *sin6;
1089 #endif
1090         int len, dlen;
1091
1092         switch (type) {
1093
1094         case RTM_DELADDR:
1095         case RTM_NEWADDR:
1096                 len = sizeof(struct ifa_msghdr);
1097                 break;
1098
1099         case RTM_DELMADDR:
1100         case RTM_NEWMADDR:
1101                 len = sizeof(struct ifma_msghdr);
1102                 break;
1103
1104         case RTM_IFINFO:
1105                 len = sizeof(struct if_msghdr);
1106                 break;
1107
1108         case RTM_IFANNOUNCE:
1109         case RTM_IEEE80211:
1110                 len = sizeof(struct if_announcemsghdr);
1111                 break;
1112
1113         default:
1114                 len = sizeof(struct rt_msghdr);
1115         }
1116
1117         /* XXXGL: can we use MJUMPAGESIZE cluster here? */
1118         KASSERT(len <= MCLBYTES, ("%s: message too big", __func__));
1119         if (len > MHLEN)
1120                 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1121         else
1122                 m = m_gethdr(M_NOWAIT, MT_DATA);
1123         if (m == NULL)
1124                 return (m);
1125
1126         m->m_pkthdr.len = m->m_len = len;
1127         rtm = mtod(m, struct rt_msghdr *);
1128         bzero((caddr_t)rtm, len);
1129         for (i = 0; i < RTAX_MAX; i++) {
1130                 if ((sa = rtinfo->rti_info[i]) == NULL)
1131                         continue;
1132                 rtinfo->rti_addrs |= (1 << i);
1133                 dlen = SA_SIZE(sa);
1134 #ifdef INET6
1135                 if (V_deembed_scopeid && sa->sa_family == AF_INET6) {
1136                         sin6 = (struct sockaddr_in6 *)&ss;
1137                         bcopy(sa, sin6, sizeof(*sin6));
1138                         if (sa6_recoverscope(sin6) == 0)
1139                                 sa = (struct sockaddr *)sin6;
1140                 }
1141 #endif
1142                 m_copyback(m, len, dlen, (caddr_t)sa);
1143                 len += dlen;
1144         }
1145         if (m->m_pkthdr.len != len) {
1146                 m_freem(m);
1147                 return (NULL);
1148         }
1149         rtm->rtm_msglen = len;
1150         rtm->rtm_version = RTM_VERSION;
1151         rtm->rtm_type = type;
1152         return (m);
1153 }
1154
1155 /*
1156  * Used by the sysctl code and routing socket.
1157  */
1158 static int
1159 rt_msg2(int type, struct rt_addrinfo *rtinfo, caddr_t cp, struct walkarg *w)
1160 {
1161         int i;
1162         int len, dlen, second_time = 0;
1163         caddr_t cp0;
1164 #ifdef INET6
1165         struct sockaddr_storage ss;
1166         struct sockaddr_in6 *sin6;
1167 #endif
1168
1169         rtinfo->rti_addrs = 0;
1170 again:
1171         switch (type) {
1172
1173         case RTM_DELADDR:
1174         case RTM_NEWADDR:
1175                 if (w != NULL && w->w_op == NET_RT_IFLISTL) {
1176 #ifdef COMPAT_FREEBSD32
1177                         if (w->w_req->flags & SCTL_MASK32)
1178                                 len = sizeof(struct ifa_msghdrl32);
1179                         else
1180 #endif
1181                                 len = sizeof(struct ifa_msghdrl);
1182                 } else
1183                         len = sizeof(struct ifa_msghdr);
1184                 break;
1185
1186         case RTM_IFINFO:
1187 #ifdef COMPAT_FREEBSD32
1188                 if (w != NULL && w->w_req->flags & SCTL_MASK32) {
1189                         if (w->w_op == NET_RT_IFLISTL)
1190                                 len = sizeof(struct if_msghdrl32);
1191                         else
1192                                 len = sizeof(struct if_msghdr32);
1193                         break;
1194                 }
1195 #endif
1196                 if (w != NULL && w->w_op == NET_RT_IFLISTL)
1197                         len = sizeof(struct if_msghdrl);
1198                 else
1199                         len = sizeof(struct if_msghdr);
1200                 break;
1201
1202         case RTM_NEWMADDR:
1203                 len = sizeof(struct ifma_msghdr);
1204                 break;
1205
1206         default:
1207                 len = sizeof(struct rt_msghdr);
1208         }
1209         cp0 = cp;
1210         if (cp0)
1211                 cp += len;
1212         for (i = 0; i < RTAX_MAX; i++) {
1213                 struct sockaddr *sa;
1214
1215                 if ((sa = rtinfo->rti_info[i]) == NULL)
1216                         continue;
1217                 rtinfo->rti_addrs |= (1 << i);
1218                 dlen = SA_SIZE(sa);
1219                 if (cp) {
1220 #ifdef INET6
1221                         if (V_deembed_scopeid && sa->sa_family == AF_INET6) {
1222                                 sin6 = (struct sockaddr_in6 *)&ss;
1223                                 bcopy(sa, sin6, sizeof(*sin6));
1224                                 if (sa6_recoverscope(sin6) == 0)
1225                                         sa = (struct sockaddr *)sin6;
1226                         }
1227 #endif
1228                         bcopy((caddr_t)sa, cp, (unsigned)dlen);
1229                         cp += dlen;
1230                 }
1231                 len += dlen;
1232         }
1233         len = ALIGN(len);
1234         if (cp == NULL && w != NULL && !second_time) {
1235                 struct walkarg *rw = w;
1236
1237                 if (rw->w_req) {
1238                         if (rw->w_tmemsize < len) {
1239                                 if (rw->w_tmem)
1240                                         free(rw->w_tmem, M_RTABLE);
1241                                 rw->w_tmem = (caddr_t)
1242                                         malloc(len, M_RTABLE, M_NOWAIT);
1243                                 if (rw->w_tmem)
1244                                         rw->w_tmemsize = len;
1245                         }
1246                         if (rw->w_tmem) {
1247                                 cp = rw->w_tmem;
1248                                 second_time = 1;
1249                                 goto again;
1250                         }
1251                 }
1252         }
1253         if (cp) {
1254                 struct rt_msghdr *rtm = (struct rt_msghdr *)cp0;
1255
1256                 rtm->rtm_version = RTM_VERSION;
1257                 rtm->rtm_type = type;
1258                 rtm->rtm_msglen = len;
1259         }
1260         return (len);
1261 }
1262
1263 /*
1264  * This routine is called to generate a message from the routing
1265  * socket indicating that a redirect has occured, a routing lookup
1266  * has failed, or that a protocol has detected timeouts to a particular
1267  * destination.
1268  */
1269 void
1270 rt_missmsg_fib(int type, struct rt_addrinfo *rtinfo, int flags, int error,
1271     int fibnum)
1272 {
1273         struct rt_msghdr *rtm;
1274         struct mbuf *m;
1275         struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
1276
1277         if (route_cb.any_count == 0)
1278                 return;
1279         m = rt_msg1(type, rtinfo);
1280         if (m == NULL)
1281                 return;
1282
1283         if (fibnum != RT_ALL_FIBS) {
1284                 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out "
1285                     "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs));
1286                 M_SETFIB(m, fibnum);
1287                 m->m_flags |= RTS_FILTER_FIB;
1288         }
1289
1290         rtm = mtod(m, struct rt_msghdr *);
1291         rtm->rtm_flags = RTF_DONE | flags;
1292         rtm->rtm_errno = error;
1293         rtm->rtm_addrs = rtinfo->rti_addrs;
1294         rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1295 }
1296
1297 void
1298 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
1299 {
1300
1301         rt_missmsg_fib(type, rtinfo, flags, error, RT_ALL_FIBS);
1302 }
1303
1304 /*
1305  * This routine is called to generate a message from the routing
1306  * socket indicating that the status of a network interface has changed.
1307  */
1308 void
1309 rt_ifmsg(struct ifnet *ifp)
1310 {
1311         struct if_msghdr *ifm;
1312         struct mbuf *m;
1313         struct rt_addrinfo info;
1314
1315         if (route_cb.any_count == 0)
1316                 return;
1317         bzero((caddr_t)&info, sizeof(info));
1318         m = rt_msg1(RTM_IFINFO, &info);
1319         if (m == NULL)
1320                 return;
1321         ifm = mtod(m, struct if_msghdr *);
1322         ifm->ifm_index = ifp->if_index;
1323         ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1324         ifm->ifm_data = ifp->if_data;
1325         ifm->ifm_addrs = 0;
1326         rt_dispatch(m, AF_UNSPEC);
1327 }
1328
1329 /*
1330  * This is called to generate messages from the routing socket
1331  * indicating a network interface has had addresses associated with it.
1332  * if we ever reverse the logic and replace messages TO the routing
1333  * socket indicate a request to configure interfaces, then it will
1334  * be unnecessary as the routing socket will automatically generate
1335  * copies of it.
1336  */
1337 void
1338 rt_newaddrmsg_fib(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt,
1339     int fibnum)
1340 {
1341         struct rt_addrinfo info;
1342         struct sockaddr *sa = NULL;
1343         int pass;
1344         struct mbuf *m = NULL;
1345         struct ifnet *ifp = ifa->ifa_ifp;
1346
1347         KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE,
1348                 ("unexpected cmd %u", cmd));
1349 #if defined(INET) || defined(INET6)
1350 #ifdef SCTP
1351         /*
1352          * notify the SCTP stack
1353          * this will only get called when an address is added/deleted
1354          * XXX pass the ifaddr struct instead if ifa->ifa_addr...
1355          */
1356         sctp_addr_change(ifa, cmd);
1357 #endif /* SCTP */
1358 #endif
1359         if (route_cb.any_count == 0)
1360                 return;
1361         for (pass = 1; pass < 3; pass++) {
1362                 bzero((caddr_t)&info, sizeof(info));
1363                 if ((cmd == RTM_ADD && pass == 1) ||
1364                     (cmd == RTM_DELETE && pass == 2)) {
1365                         struct ifa_msghdr *ifam;
1366                         int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
1367
1368                         info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1369                         info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1370                         info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1371                         info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1372                         if ((m = rt_msg1(ncmd, &info)) == NULL)
1373                                 continue;
1374                         ifam = mtod(m, struct ifa_msghdr *);
1375                         ifam->ifam_index = ifp->if_index;
1376                         ifam->ifam_metric = ifa->ifa_metric;
1377                         ifam->ifam_flags = ifa->ifa_flags;
1378                         ifam->ifam_addrs = info.rti_addrs;
1379                 }
1380                 if ((cmd == RTM_ADD && pass == 2) ||
1381                     (cmd == RTM_DELETE && pass == 1)) {
1382                         struct rt_msghdr *rtm;
1383
1384                         if (rt == NULL)
1385                                 continue;
1386                         info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1387                         info.rti_info[RTAX_DST] = sa = rt_key(rt);
1388                         info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1389                         if ((m = rt_msg1(cmd, &info)) == NULL)
1390                                 continue;
1391                         rtm = mtod(m, struct rt_msghdr *);
1392                         rtm->rtm_index = ifp->if_index;
1393                         rtm->rtm_flags |= rt->rt_flags;
1394                         rtm->rtm_errno = error;
1395                         rtm->rtm_addrs = info.rti_addrs;
1396                 }
1397                 if (fibnum != RT_ALL_FIBS) {
1398                         KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: "
1399                             "fibnum out of range 0 <= %d < %d", __func__,
1400                              fibnum, rt_numfibs));
1401                         M_SETFIB(m, fibnum);
1402                         m->m_flags |= RTS_FILTER_FIB;
1403                 }
1404                 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1405         }
1406 }
1407
1408 void
1409 rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt)
1410 {
1411
1412         rt_newaddrmsg_fib(cmd, ifa, error, rt, RT_ALL_FIBS);
1413 }
1414
1415 /*
1416  * This is the analogue to the rt_newaddrmsg which performs the same
1417  * function but for multicast group memberhips.  This is easier since
1418  * there is no route state to worry about.
1419  */
1420 void
1421 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
1422 {
1423         struct rt_addrinfo info;
1424         struct mbuf *m = NULL;
1425         struct ifnet *ifp = ifma->ifma_ifp;
1426         struct ifma_msghdr *ifmam;
1427
1428         if (route_cb.any_count == 0)
1429                 return;
1430
1431         bzero((caddr_t)&info, sizeof(info));
1432         info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1433         info.rti_info[RTAX_IFP] = ifp ? ifp->if_addr->ifa_addr : NULL;
1434         /*
1435          * If a link-layer address is present, present it as a ``gateway''
1436          * (similarly to how ARP entries, e.g., are presented).
1437          */
1438         info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr;
1439         m = rt_msg1(cmd, &info);
1440         if (m == NULL)
1441                 return;
1442         ifmam = mtod(m, struct ifma_msghdr *);
1443         KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n",
1444             __func__));
1445         ifmam->ifmam_index = ifp->if_index;
1446         ifmam->ifmam_addrs = info.rti_addrs;
1447         rt_dispatch(m, ifma->ifma_addr ? ifma->ifma_addr->sa_family : AF_UNSPEC);
1448 }
1449
1450 static struct mbuf *
1451 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1452         struct rt_addrinfo *info)
1453 {
1454         struct if_announcemsghdr *ifan;
1455         struct mbuf *m;
1456
1457         if (route_cb.any_count == 0)
1458                 return NULL;
1459         bzero((caddr_t)info, sizeof(*info));
1460         m = rt_msg1(type, info);
1461         if (m != NULL) {
1462                 ifan = mtod(m, struct if_announcemsghdr *);
1463                 ifan->ifan_index = ifp->if_index;
1464                 strlcpy(ifan->ifan_name, ifp->if_xname,
1465                         sizeof(ifan->ifan_name));
1466                 ifan->ifan_what = what;
1467         }
1468         return m;
1469 }
1470
1471 /*
1472  * This is called to generate routing socket messages indicating
1473  * IEEE80211 wireless events.
1474  * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1475  */
1476 void
1477 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
1478 {
1479         struct mbuf *m;
1480         struct rt_addrinfo info;
1481
1482         m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1483         if (m != NULL) {
1484                 /*
1485                  * Append the ieee80211 data.  Try to stick it in the
1486                  * mbuf containing the ifannounce msg; otherwise allocate
1487                  * a new mbuf and append.
1488                  *
1489                  * NB: we assume m is a single mbuf.
1490                  */
1491                 if (data_len > M_TRAILINGSPACE(m)) {
1492                         struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
1493                         if (n == NULL) {
1494                                 m_freem(m);
1495                                 return;
1496                         }
1497                         bcopy(data, mtod(n, void *), data_len);
1498                         n->m_len = data_len;
1499                         m->m_next = n;
1500                 } else if (data_len > 0) {
1501                         bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
1502                         m->m_len += data_len;
1503                 }
1504                 if (m->m_flags & M_PKTHDR)
1505                         m->m_pkthdr.len += data_len;
1506                 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
1507                 rt_dispatch(m, AF_UNSPEC);
1508         }
1509 }
1510
1511 /*
1512  * This is called to generate routing socket messages indicating
1513  * network interface arrival and departure.
1514  */
1515 void
1516 rt_ifannouncemsg(struct ifnet *ifp, int what)
1517 {
1518         struct mbuf *m;
1519         struct rt_addrinfo info;
1520
1521         m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
1522         if (m != NULL)
1523                 rt_dispatch(m, AF_UNSPEC);
1524 }
1525
1526 static void
1527 rt_dispatch(struct mbuf *m, sa_family_t saf)
1528 {
1529         struct m_tag *tag;
1530
1531         /*
1532          * Preserve the family from the sockaddr, if any, in an m_tag for
1533          * use when injecting the mbuf into the routing socket buffer from
1534          * the netisr.
1535          */
1536         if (saf != AF_UNSPEC) {
1537                 tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short),
1538                     M_NOWAIT);
1539                 if (tag == NULL) {
1540                         m_freem(m);
1541                         return;
1542                 }
1543                 *(unsigned short *)(tag + 1) = saf;
1544                 m_tag_prepend(m, tag);
1545         }
1546 #ifdef VIMAGE
1547         if (V_loif)
1548                 m->m_pkthdr.rcvif = V_loif;
1549         else {
1550                 m_freem(m);
1551                 return;
1552         }
1553 #endif
1554         netisr_queue(NETISR_ROUTE, m);  /* mbuf is free'd on failure. */
1555 }
1556
1557 /*
1558  * This is used in dumping the kernel table via sysctl().
1559  */
1560 static int
1561 sysctl_dumpentry(struct radix_node *rn, void *vw)
1562 {
1563         struct walkarg *w = vw;
1564         struct rtentry *rt = (struct rtentry *)rn;
1565         int error = 0, size;
1566         struct rt_addrinfo info;
1567
1568         if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1569                 return 0;
1570         if ((rt->rt_flags & RTF_HOST) == 0
1571             ? jailed_without_vnet(w->w_req->td->td_ucred)
1572             : prison_if(w->w_req->td->td_ucred, rt_key(rt)) != 0)
1573                 return (0);
1574         bzero((caddr_t)&info, sizeof(info));
1575         info.rti_info[RTAX_DST] = rt_key(rt);
1576         info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1577         info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1578         info.rti_info[RTAX_GENMASK] = 0;
1579         if (rt->rt_ifp) {
1580                 info.rti_info[RTAX_IFP] = rt->rt_ifp->if_addr->ifa_addr;
1581                 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
1582                 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1583                         info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr;
1584         }
1585         size = rt_msg2(RTM_GET, &info, NULL, w);
1586         if (w->w_req && w->w_tmem) {
1587                 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
1588
1589                 if (rt->rt_flags & RTF_GWFLAG_COMPAT)
1590                         rtm->rtm_flags = RTF_GATEWAY | 
1591                                 (rt->rt_flags & ~RTF_GWFLAG_COMPAT);
1592                 else
1593                         rtm->rtm_flags = rt->rt_flags;
1594                 rt_getmetrics(rt, &rtm->rtm_rmx);
1595                 rtm->rtm_index = rt->rt_ifp->if_index;
1596                 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
1597                 rtm->rtm_addrs = info.rti_addrs;
1598                 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
1599                 return (error);
1600         }
1601         return (error);
1602 }
1603
1604 #ifdef COMPAT_FREEBSD32
1605 static void
1606 copy_ifdata32(struct if_data *src, struct if_data32 *dst)
1607 {
1608
1609         bzero(dst, sizeof(*dst));
1610         CP(*src, *dst, ifi_type);
1611         CP(*src, *dst, ifi_physical);
1612         CP(*src, *dst, ifi_addrlen);
1613         CP(*src, *dst, ifi_hdrlen);
1614         CP(*src, *dst, ifi_link_state);
1615         CP(*src, *dst, ifi_vhid);
1616         CP(*src, *dst, ifi_baudrate_pf);
1617         dst->ifi_datalen = sizeof(struct if_data32);
1618         CP(*src, *dst, ifi_mtu);
1619         CP(*src, *dst, ifi_metric);
1620         CP(*src, *dst, ifi_baudrate);
1621         CP(*src, *dst, ifi_ipackets);
1622         CP(*src, *dst, ifi_ierrors);
1623         CP(*src, *dst, ifi_opackets);
1624         CP(*src, *dst, ifi_oerrors);
1625         CP(*src, *dst, ifi_collisions);
1626         CP(*src, *dst, ifi_ibytes);
1627         CP(*src, *dst, ifi_obytes);
1628         CP(*src, *dst, ifi_imcasts);
1629         CP(*src, *dst, ifi_omcasts);
1630         CP(*src, *dst, ifi_iqdrops);
1631         CP(*src, *dst, ifi_noproto);
1632         CP(*src, *dst, ifi_hwassist);
1633         CP(*src, *dst, ifi_epoch);
1634         TV_CP(*src, *dst, ifi_lastchange);
1635 }
1636 #endif
1637
1638 static int
1639 sysctl_iflist_ifml(struct ifnet *ifp, struct rt_addrinfo *info,
1640     struct walkarg *w, int len)
1641 {
1642         struct if_msghdrl *ifm;
1643
1644 #ifdef COMPAT_FREEBSD32
1645         if (w->w_req->flags & SCTL_MASK32) {
1646                 struct if_msghdrl32 *ifm32;
1647
1648                 ifm32 = (struct if_msghdrl32 *)w->w_tmem;
1649                 ifm32->ifm_addrs = info->rti_addrs;
1650                 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1651                 ifm32->ifm_index = ifp->if_index;
1652                 ifm32->_ifm_spare1 = 0;
1653                 ifm32->ifm_len = sizeof(*ifm32);
1654                 ifm32->ifm_data_off = offsetof(struct if_msghdrl32, ifm_data);
1655
1656                 copy_ifdata32(&ifp->if_data, &ifm32->ifm_data);
1657                 /* Fixup if_data carp(4) vhid. */
1658                 if (carp_get_vhid_p != NULL)
1659                         ifm32->ifm_data.ifi_vhid =
1660                             (*carp_get_vhid_p)(ifp->if_addr);
1661                 ifm32->ifm_data.ifi_oqdrops = ifp->if_snd.ifq_drops;
1662
1663                 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm32, len));
1664         }
1665 #endif
1666         ifm = (struct if_msghdrl *)w->w_tmem;
1667         ifm->ifm_addrs = info->rti_addrs;
1668         ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1669         ifm->ifm_index = ifp->if_index;
1670         ifm->_ifm_spare1 = 0;
1671         ifm->ifm_len = sizeof(*ifm);
1672         ifm->ifm_data_off = offsetof(struct if_msghdrl, ifm_data);
1673
1674         ifm->ifm_data = ifp->if_data;
1675         /* Fixup if_data carp(4) vhid. */
1676         if (carp_get_vhid_p != NULL)
1677                 ifm->ifm_data.ifi_vhid = (*carp_get_vhid_p)(ifp->if_addr);
1678
1679         ifm->ifm_data.ifi_datalen += sizeof(u_long);
1680         ifm->ifi_oqdrops = ifp->if_snd.ifq_drops;
1681
1682         return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1683 }
1684
1685 static int
1686 sysctl_iflist_ifm(struct ifnet *ifp, struct rt_addrinfo *info,
1687     struct walkarg *w, int len)
1688 {
1689         struct if_msghdr *ifm;
1690
1691 #ifdef COMPAT_FREEBSD32
1692         if (w->w_req->flags & SCTL_MASK32) {
1693                 struct if_msghdr32 *ifm32;
1694
1695                 ifm32 = (struct if_msghdr32 *)w->w_tmem;
1696                 ifm32->ifm_addrs = info->rti_addrs;
1697                 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1698                 ifm32->ifm_index = ifp->if_index;
1699
1700                 copy_ifdata32(&ifp->if_data, &ifm32->ifm_data);
1701                 /* Fixup if_data carp(4) vhid. */
1702                 if (carp_get_vhid_p != NULL)
1703                         ifm32->ifm_data.ifi_vhid =
1704                             (*carp_get_vhid_p)(ifp->if_addr);
1705
1706                 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm32, len));
1707         }
1708 #endif
1709         ifm = (struct if_msghdr *)w->w_tmem;
1710         ifm->ifm_addrs = info->rti_addrs;
1711         ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1712         ifm->ifm_index = ifp->if_index;
1713
1714         ifm->ifm_data = ifp->if_data;
1715         /* Fixup if_data carp(4) vhid. */
1716         if (carp_get_vhid_p != NULL)
1717                 ifm->ifm_data.ifi_vhid = (*carp_get_vhid_p)(ifp->if_addr);
1718
1719         return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1720 }
1721
1722 static int
1723 sysctl_iflist_ifaml(struct ifaddr *ifa, struct rt_addrinfo *info,
1724     struct walkarg *w, int len)
1725 {
1726         struct ifa_msghdrl *ifam;
1727
1728 #ifdef COMPAT_FREEBSD32
1729         if (w->w_req->flags & SCTL_MASK32) {
1730                 struct ifa_msghdrl32 *ifam32;
1731
1732                 ifam32 = (struct ifa_msghdrl32 *)w->w_tmem;
1733                 ifam32->ifam_addrs = info->rti_addrs;
1734                 ifam32->ifam_flags = ifa->ifa_flags;
1735                 ifam32->ifam_index = ifa->ifa_ifp->if_index;
1736                 ifam32->_ifam_spare1 = 0;
1737                 ifam32->ifam_len = sizeof(*ifam32);
1738                 ifam32->ifam_data_off =
1739                     offsetof(struct ifa_msghdrl32, ifam_data);
1740                 ifam32->ifam_metric = ifa->ifa_metric;
1741
1742                 copy_ifdata32(&ifa->ifa_ifp->if_data, &ifam32->ifam_data);
1743                 /* Fixup if_data carp(4) vhid. */
1744                 if (carp_get_vhid_p != NULL)
1745                         ifam32->ifam_data.ifi_vhid = (*carp_get_vhid_p)(ifa);
1746
1747                 return (SYSCTL_OUT(w->w_req, (caddr_t)ifam32, len));
1748         }
1749 #endif
1750
1751         ifam = (struct ifa_msghdrl *)w->w_tmem;
1752         ifam->ifam_addrs = info->rti_addrs;
1753         ifam->ifam_flags = ifa->ifa_flags;
1754         ifam->ifam_index = ifa->ifa_ifp->if_index;
1755         ifam->_ifam_spare1 = 0;
1756         ifam->ifam_len = sizeof(*ifam);
1757         ifam->ifam_data_off = offsetof(struct ifa_msghdrl, ifam_data);
1758         ifam->ifam_metric = ifa->ifa_metric;
1759
1760         ifam->ifam_data = ifa->if_data;
1761         /* Fixup if_data carp(4) vhid. */
1762         if (carp_get_vhid_p != NULL)
1763                 ifam->ifam_data.ifi_vhid = (*carp_get_vhid_p)(ifa);
1764
1765         return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1766 }
1767
1768 static int
1769 sysctl_iflist_ifam(struct ifaddr *ifa, struct rt_addrinfo *info,
1770     struct walkarg *w, int len)
1771 {
1772         struct ifa_msghdr *ifam;
1773
1774         ifam = (struct ifa_msghdr *)w->w_tmem;
1775         ifam->ifam_addrs = info->rti_addrs;
1776         ifam->ifam_flags = ifa->ifa_flags;
1777         ifam->ifam_index = ifa->ifa_ifp->if_index;
1778         ifam->ifam_metric = ifa->ifa_metric;
1779
1780         return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1781 }
1782
1783 static int
1784 sysctl_iflist(int af, struct walkarg *w)
1785 {
1786         struct ifnet *ifp;
1787         struct ifaddr *ifa;
1788         struct rt_addrinfo info;
1789         int len, error = 0;
1790
1791         bzero((caddr_t)&info, sizeof(info));
1792         IFNET_RLOCK_NOSLEEP();
1793         TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1794                 if (w->w_arg && w->w_arg != ifp->if_index)
1795                         continue;
1796                 IF_ADDR_RLOCK(ifp);
1797                 ifa = ifp->if_addr;
1798                 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
1799                 len = rt_msg2(RTM_IFINFO, &info, NULL, w);
1800                 info.rti_info[RTAX_IFP] = NULL;
1801                 if (w->w_req && w->w_tmem) {
1802                         if (w->w_op == NET_RT_IFLISTL)
1803                                 error = sysctl_iflist_ifml(ifp, &info, w, len);
1804                         else
1805                                 error = sysctl_iflist_ifm(ifp, &info, w, len);
1806                         if (error)
1807                                 goto done;
1808                 }
1809                 while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != NULL) {
1810                         if (af && af != ifa->ifa_addr->sa_family)
1811                                 continue;
1812                         if (prison_if(w->w_req->td->td_ucred,
1813                             ifa->ifa_addr) != 0)
1814                                 continue;
1815                         info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1816                         info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1817                         info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1818                         len = rt_msg2(RTM_NEWADDR, &info, NULL, w);
1819                         if (w->w_req && w->w_tmem) {
1820                                 if (w->w_op == NET_RT_IFLISTL)
1821                                         error = sysctl_iflist_ifaml(ifa, &info,
1822                                             w, len);
1823                                 else
1824                                         error = sysctl_iflist_ifam(ifa, &info,
1825                                             w, len);
1826                                 if (error)
1827                                         goto done;
1828                         }
1829                 }
1830                 IF_ADDR_RUNLOCK(ifp);
1831                 info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] =
1832                         info.rti_info[RTAX_BRD] = NULL;
1833         }
1834 done:
1835         if (ifp != NULL)
1836                 IF_ADDR_RUNLOCK(ifp);
1837         IFNET_RUNLOCK_NOSLEEP();
1838         return (error);
1839 }
1840
1841 static int
1842 sysctl_ifmalist(int af, struct walkarg *w)
1843 {
1844         struct ifnet *ifp;
1845         struct ifmultiaddr *ifma;
1846         struct  rt_addrinfo info;
1847         int     len, error = 0;
1848         struct ifaddr *ifa;
1849
1850         bzero((caddr_t)&info, sizeof(info));
1851         IFNET_RLOCK_NOSLEEP();
1852         TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1853                 if (w->w_arg && w->w_arg != ifp->if_index)
1854                         continue;
1855                 ifa = ifp->if_addr;
1856                 info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL;
1857                 IF_ADDR_RLOCK(ifp);
1858                 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1859                         if (af && af != ifma->ifma_addr->sa_family)
1860                                 continue;
1861                         if (prison_if(w->w_req->td->td_ucred,
1862                             ifma->ifma_addr) != 0)
1863                                 continue;
1864                         info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1865                         info.rti_info[RTAX_GATEWAY] =
1866                             (ifma->ifma_addr->sa_family != AF_LINK) ?
1867                             ifma->ifma_lladdr : NULL;
1868                         len = rt_msg2(RTM_NEWMADDR, &info, NULL, w);
1869                         if (w->w_req && w->w_tmem) {
1870                                 struct ifma_msghdr *ifmam;
1871
1872                                 ifmam = (struct ifma_msghdr *)w->w_tmem;
1873                                 ifmam->ifmam_index = ifma->ifma_ifp->if_index;
1874                                 ifmam->ifmam_flags = 0;
1875                                 ifmam->ifmam_addrs = info.rti_addrs;
1876                                 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
1877                                 if (error) {
1878                                         IF_ADDR_RUNLOCK(ifp);
1879                                         goto done;
1880                                 }
1881                         }
1882                 }
1883                 IF_ADDR_RUNLOCK(ifp);
1884         }
1885 done:
1886         IFNET_RUNLOCK_NOSLEEP();
1887         return (error);
1888 }
1889
1890 static int
1891 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
1892 {
1893         int     *name = (int *)arg1;
1894         u_int   namelen = arg2;
1895         struct radix_node_head *rnh = NULL; /* silence compiler. */
1896         int     i, lim, error = EINVAL;
1897         int     fib = 0;
1898         u_char  af;
1899         struct  walkarg w;
1900
1901         name ++;
1902         namelen--;
1903         if (req->newptr)
1904                 return (EPERM);
1905         if (name[1] == NET_RT_DUMP) {
1906                 if (namelen == 3)
1907                         fib = req->td->td_proc->p_fibnum;
1908                 else if (namelen == 4)
1909                         fib = (name[3] == RT_ALL_FIBS) ?
1910                             req->td->td_proc->p_fibnum : name[3];
1911                 else
1912                         return ((namelen < 3) ? EISDIR : ENOTDIR);
1913                 if (fib < 0 || fib >= rt_numfibs)
1914                         return (EINVAL);
1915         } else if (namelen != 3)
1916                 return ((namelen < 3) ? EISDIR : ENOTDIR);
1917         af = name[0];
1918         if (af > AF_MAX)
1919                 return (EINVAL);
1920         bzero(&w, sizeof(w));
1921         w.w_op = name[1];
1922         w.w_arg = name[2];
1923         w.w_req = req;
1924
1925         error = sysctl_wire_old_buffer(req, 0);
1926         if (error)
1927                 return (error);
1928         switch (w.w_op) {
1929
1930         case NET_RT_DUMP:
1931         case NET_RT_FLAGS:
1932                 if (af == 0) {                  /* dump all tables */
1933                         i = 1;
1934                         lim = AF_MAX;
1935                 } else                          /* dump only one table */
1936                         i = lim = af;
1937
1938                 /*
1939                  * take care of llinfo entries, the caller must
1940                  * specify an AF
1941                  */
1942                 if (w.w_op == NET_RT_FLAGS &&
1943                     (w.w_arg == 0 || w.w_arg & RTF_LLINFO)) {
1944                         if (af != 0)
1945                                 error = lltable_sysctl_dumparp(af, w.w_req);
1946                         else
1947                                 error = EINVAL;
1948                         break;
1949                 }
1950                 /*
1951                  * take care of routing entries
1952                  */
1953                 for (error = 0; error == 0 && i <= lim; i++) {
1954                         rnh = rt_tables_get_rnh(fib, i);
1955                         if (rnh != NULL) {
1956                                 RADIX_NODE_HEAD_RLOCK(rnh); 
1957                                 error = rnh->rnh_walktree(rnh,
1958                                     sysctl_dumpentry, &w);
1959                                 RADIX_NODE_HEAD_RUNLOCK(rnh);
1960                         } else if (af != 0)
1961                                 error = EAFNOSUPPORT;
1962                 }
1963                 break;
1964
1965         case NET_RT_IFLIST:
1966         case NET_RT_IFLISTL:
1967                 error = sysctl_iflist(af, &w);
1968                 break;
1969
1970         case NET_RT_IFMALIST:
1971                 error = sysctl_ifmalist(af, &w);
1972                 break;
1973         }
1974         if (w.w_tmem)
1975                 free(w.w_tmem, M_RTABLE);
1976         return (error);
1977 }
1978
1979 static SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
1980
1981 /*
1982  * Definitions of protocols supported in the ROUTE domain.
1983  */
1984
1985 static struct domain routedomain;               /* or at least forward */
1986
1987 static struct protosw routesw[] = {
1988 {
1989         .pr_type =              SOCK_RAW,
1990         .pr_domain =            &routedomain,
1991         .pr_flags =             PR_ATOMIC|PR_ADDR,
1992         .pr_output =            route_output,
1993         .pr_ctlinput =          raw_ctlinput,
1994         .pr_init =              raw_init,
1995         .pr_usrreqs =           &route_usrreqs
1996 }
1997 };
1998
1999 static struct domain routedomain = {
2000         .dom_family =           PF_ROUTE,
2001         .dom_name =              "route",
2002         .dom_protosw =          routesw,
2003         .dom_protoswNPROTOSW =  &routesw[sizeof(routesw)/sizeof(routesw[0])]
2004 };
2005
2006 VNET_DOMAIN_SET(route);