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