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