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