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