]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - sys/net/rtsock.c
MFC r227061 (by mlaier):
[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 *, sa_family_t);
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         sa_family_t saf = AF_UNSPEC;
549
550 #define senderr(e) { error = e; goto flush;}
551         if (m == NULL || ((m->m_len < sizeof(long)) &&
552                        (m = m_pullup(m, sizeof(long))) == NULL))
553                 return (ENOBUFS);
554         if ((m->m_flags & M_PKTHDR) == 0)
555                 panic("route_output");
556         len = m->m_pkthdr.len;
557         if (len < sizeof(*rtm) ||
558             len != mtod(m, struct rt_msghdr *)->rtm_msglen) {
559                 info.rti_info[RTAX_DST] = NULL;
560                 senderr(EINVAL);
561         }
562         R_Malloc(rtm, struct rt_msghdr *, len);
563         if (rtm == NULL) {
564                 info.rti_info[RTAX_DST] = NULL;
565                 senderr(ENOBUFS);
566         }
567         m_copydata(m, 0, len, (caddr_t)rtm);
568         if (rtm->rtm_version != RTM_VERSION) {
569                 info.rti_info[RTAX_DST] = NULL;
570                 senderr(EPROTONOSUPPORT);
571         }
572         rtm->rtm_pid = curproc->p_pid;
573         bzero(&info, sizeof(info));
574         info.rti_addrs = rtm->rtm_addrs;
575         if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info)) {
576                 info.rti_info[RTAX_DST] = NULL;
577                 senderr(EINVAL);
578         }
579         info.rti_flags = rtm->rtm_flags;
580         if (info.rti_info[RTAX_DST] == NULL ||
581             info.rti_info[RTAX_DST]->sa_family >= AF_MAX ||
582             (info.rti_info[RTAX_GATEWAY] != NULL &&
583              info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX))
584                 senderr(EINVAL);
585         saf = info.rti_info[RTAX_DST]->sa_family;
586         /*
587          * Verify that the caller has the appropriate privilege; RTM_GET
588          * is the only operation the non-superuser is allowed.
589          */
590         if (rtm->rtm_type != RTM_GET) {
591                 error = priv_check(curthread, PRIV_NET_ROUTE);
592                 if (error)
593                         senderr(error);
594         }
595
596         /*
597          * The given gateway address may be an interface address.
598          * For example, issuing a "route change" command on a route
599          * entry that was created from a tunnel, and the gateway
600          * address given is the local end point. In this case the 
601          * RTF_GATEWAY flag must be cleared or the destination will
602          * not be reachable even though there is no error message.
603          */
604         if (info.rti_info[RTAX_GATEWAY] != NULL &&
605             info.rti_info[RTAX_GATEWAY]->sa_family != AF_LINK) {
606                 struct route gw_ro;
607
608                 bzero(&gw_ro, sizeof(gw_ro));
609                 gw_ro.ro_dst = *info.rti_info[RTAX_GATEWAY];
610                 rtalloc_ign_fib(&gw_ro, 0, so->so_fibnum);
611                 /* 
612                  * A host route through the loopback interface is 
613                  * installed for each interface adddress. In pre 8.0
614                  * releases the interface address of a PPP link type
615                  * is not reachable locally. This behavior is fixed as 
616                  * part of the new L2/L3 redesign and rewrite work. The
617                  * signature of this interface address route is the
618                  * AF_LINK sa_family type of the rt_gateway, and the
619                  * rt_ifp has the IFF_LOOPBACK flag set.
620                  */
621                 if (gw_ro.ro_rt != NULL &&
622                     gw_ro.ro_rt->rt_gateway->sa_family == AF_LINK &&
623                     gw_ro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK)
624                         info.rti_flags &= ~RTF_GATEWAY;
625                 if (gw_ro.ro_rt != NULL)
626                         RTFREE(gw_ro.ro_rt);
627         }
628
629         switch (rtm->rtm_type) {
630                 struct rtentry *saved_nrt;
631
632         case RTM_ADD:
633                 if (info.rti_info[RTAX_GATEWAY] == NULL)
634                         senderr(EINVAL);
635                 saved_nrt = NULL;
636
637                 /* support for new ARP code */
638                 if (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK &&
639                     (rtm->rtm_flags & RTF_LLDATA) != 0) {
640                         error = lla_rt_output(rtm, &info);
641                         break;
642                 }
643                 error = rtrequest1_fib(RTM_ADD, &info, &saved_nrt,
644                     so->so_fibnum);
645                 if (error == 0 && saved_nrt) {
646                         RT_LOCK(saved_nrt);
647                         rt_setmetrics(rtm->rtm_inits,
648                                 &rtm->rtm_rmx, &saved_nrt->rt_rmx);
649                         rtm->rtm_index = saved_nrt->rt_ifp->if_index;
650                         RT_REMREF(saved_nrt);
651                         RT_UNLOCK(saved_nrt);
652                 }
653                 break;
654
655         case RTM_DELETE:
656                 saved_nrt = NULL;
657                 /* support for new ARP code */
658                 if (info.rti_info[RTAX_GATEWAY] && 
659                     (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) &&
660                     (rtm->rtm_flags & RTF_LLDATA) != 0) {
661                         error = lla_rt_output(rtm, &info);
662                         break;
663                 }
664                 error = rtrequest1_fib(RTM_DELETE, &info, &saved_nrt,
665                     so->so_fibnum);
666                 if (error == 0) {
667                         RT_LOCK(saved_nrt);
668                         rt = saved_nrt;
669                         goto report;
670                 }
671                 break;
672
673         case RTM_GET:
674         case RTM_CHANGE:
675         case RTM_LOCK:
676                 rnh = rt_tables_get_rnh(so->so_fibnum,
677                     info.rti_info[RTAX_DST]->sa_family);
678                 if (rnh == NULL)
679                         senderr(EAFNOSUPPORT);
680                 RADIX_NODE_HEAD_RLOCK(rnh);
681                 rt = (struct rtentry *) rnh->rnh_lookup(info.rti_info[RTAX_DST],
682                         info.rti_info[RTAX_NETMASK], rnh);
683                 if (rt == NULL) {       /* XXX looks bogus */
684                         RADIX_NODE_HEAD_RUNLOCK(rnh);
685                         senderr(ESRCH);
686                 }
687 #ifdef RADIX_MPATH
688                 /*
689                  * for RTM_CHANGE/LOCK, if we got multipath routes,
690                  * we require users to specify a matching RTAX_GATEWAY.
691                  *
692                  * for RTM_GET, gate is optional even with multipath.
693                  * if gate == NULL the first match is returned.
694                  * (no need to call rt_mpath_matchgate if gate == NULL)
695                  */
696                 if (rn_mpath_capable(rnh) &&
697                     (rtm->rtm_type != RTM_GET || info.rti_info[RTAX_GATEWAY])) {
698                         rt = rt_mpath_matchgate(rt, info.rti_info[RTAX_GATEWAY]);
699                         if (!rt) {
700                                 RADIX_NODE_HEAD_RUNLOCK(rnh);
701                                 senderr(ESRCH);
702                         }
703                 }
704 #endif
705                 /*
706                  * If performing proxied L2 entry insertion, and
707                  * the actual PPP host entry is found, perform
708                  * another search to retrieve the prefix route of
709                  * the local end point of the PPP link.
710                  */
711                 if (rtm->rtm_flags & RTF_ANNOUNCE) {
712                         struct sockaddr laddr;
713
714                         if (rt->rt_ifp != NULL && 
715                             rt->rt_ifp->if_type == IFT_PROPVIRTUAL) {
716                                 struct ifaddr *ifa;
717
718                                 ifa = ifa_ifwithnet(info.rti_info[RTAX_DST], 1);
719                                 if (ifa != NULL)
720                                         rt_maskedcopy(ifa->ifa_addr,
721                                                       &laddr,
722                                                       ifa->ifa_netmask);
723                         } else
724                                 rt_maskedcopy(rt->rt_ifa->ifa_addr,
725                                               &laddr,
726                                               rt->rt_ifa->ifa_netmask);
727                         /* 
728                          * refactor rt and no lock operation necessary
729                          */
730                         rt = (struct rtentry *)rnh->rnh_matchaddr(&laddr, rnh);
731                         if (rt == NULL) {
732                                 RADIX_NODE_HEAD_RUNLOCK(rnh);
733                                 senderr(ESRCH);
734                         }
735                 } 
736                 RT_LOCK(rt);
737                 RT_ADDREF(rt);
738                 RADIX_NODE_HEAD_RUNLOCK(rnh);
739
740                 /* 
741                  * Fix for PR: 82974
742                  *
743                  * RTM_CHANGE/LOCK need a perfect match, rn_lookup()
744                  * returns a perfect match in case a netmask is
745                  * specified.  For host routes only a longest prefix
746                  * match is returned so it is necessary to compare the
747                  * existence of the netmask.  If both have a netmask
748                  * rnh_lookup() did a perfect match and if none of them
749                  * have a netmask both are host routes which is also a
750                  * perfect match.
751                  */
752
753                 if (rtm->rtm_type != RTM_GET && 
754                     (!rt_mask(rt) != !info.rti_info[RTAX_NETMASK])) {
755                         RT_UNLOCK(rt);
756                         senderr(ESRCH);
757                 }
758
759                 switch(rtm->rtm_type) {
760
761                 case RTM_GET:
762                 report:
763                         RT_LOCK_ASSERT(rt);
764                         if ((rt->rt_flags & RTF_HOST) == 0
765                             ? jailed_without_vnet(curthread->td_ucred)
766                             : prison_if(curthread->td_ucred,
767                             rt_key(rt)) != 0) {
768                                 RT_UNLOCK(rt);
769                                 senderr(ESRCH);
770                         }
771                         info.rti_info[RTAX_DST] = rt_key(rt);
772                         info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
773                         info.rti_info[RTAX_NETMASK] = rt_mask(rt);
774                         info.rti_info[RTAX_GENMASK] = 0;
775                         if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
776                                 ifp = rt->rt_ifp;
777                                 if (ifp) {
778                                         info.rti_info[RTAX_IFP] =
779                                             ifp->if_addr->ifa_addr;
780                                         error = rtm_get_jailed(&info, ifp, rt,
781                                             &saun, curthread->td_ucred);
782                                         if (error != 0) {
783                                                 RT_UNLOCK(rt);
784                                                 senderr(error);
785                                         }
786                                         if (ifp->if_flags & IFF_POINTOPOINT)
787                                                 info.rti_info[RTAX_BRD] =
788                                                     rt->rt_ifa->ifa_dstaddr;
789                                         rtm->rtm_index = ifp->if_index;
790                                 } else {
791                                         info.rti_info[RTAX_IFP] = NULL;
792                                         info.rti_info[RTAX_IFA] = NULL;
793                                 }
794                         } else if ((ifp = rt->rt_ifp) != NULL) {
795                                 rtm->rtm_index = ifp->if_index;
796                         }
797                         len = rt_msg2(rtm->rtm_type, &info, NULL, NULL);
798                         if (len > rtm->rtm_msglen) {
799                                 struct rt_msghdr *new_rtm;
800                                 R_Malloc(new_rtm, struct rt_msghdr *, len);
801                                 if (new_rtm == NULL) {
802                                         RT_UNLOCK(rt);
803                                         senderr(ENOBUFS);
804                                 }
805                                 bcopy(rtm, new_rtm, rtm->rtm_msglen);
806                                 Free(rtm); rtm = new_rtm;
807                         }
808                         (void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm, NULL);
809                         rtm->rtm_flags = rt->rt_flags;
810                         rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx);
811                         rtm->rtm_addrs = info.rti_addrs;
812                         break;
813
814                 case RTM_CHANGE:
815                         /*
816                          * New gateway could require new ifaddr, ifp;
817                          * flags may also be different; ifp may be specified
818                          * by ll sockaddr when protocol address is ambiguous
819                          */
820                         if (((rt->rt_flags & RTF_GATEWAY) &&
821                              info.rti_info[RTAX_GATEWAY] != NULL) ||
822                             info.rti_info[RTAX_IFP] != NULL ||
823                             (info.rti_info[RTAX_IFA] != NULL &&
824                              !sa_equal(info.rti_info[RTAX_IFA],
825                                        rt->rt_ifa->ifa_addr))) {
826                                 RT_UNLOCK(rt);
827                                 RADIX_NODE_HEAD_LOCK(rnh);
828                                 error = rt_getifa_fib(&info, rt->rt_fibnum);
829                                 /*
830                                  * XXXRW: Really we should release this
831                                  * reference later, but this maintains
832                                  * historical behavior.
833                                  */
834                                 if (info.rti_ifa != NULL)
835                                         ifa_free(info.rti_ifa);
836                                 RADIX_NODE_HEAD_UNLOCK(rnh);
837                                 if (error != 0)
838                                         senderr(error);
839                                 RT_LOCK(rt);
840                         }
841                         if (info.rti_ifa != NULL &&
842                             info.rti_ifa != rt->rt_ifa &&
843                             rt->rt_ifa != NULL &&
844                             rt->rt_ifa->ifa_rtrequest != NULL) {
845                                 rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt,
846                                     &info);
847                                 ifa_free(rt->rt_ifa);
848                         }
849                         if (info.rti_info[RTAX_GATEWAY] != NULL) {
850                                 RT_UNLOCK(rt);
851                                 RADIX_NODE_HEAD_LOCK(rnh);
852                                 RT_LOCK(rt);
853                                 
854                                 error = rt_setgate(rt, rt_key(rt),
855                                     info.rti_info[RTAX_GATEWAY]);
856                                 RADIX_NODE_HEAD_UNLOCK(rnh);
857                                 if (error != 0) {
858                                         RT_UNLOCK(rt);
859                                         senderr(error);
860                                 }
861                                 rt->rt_flags |= (RTF_GATEWAY & info.rti_flags);
862                         }
863                         if (info.rti_ifa != NULL &&
864                             info.rti_ifa != rt->rt_ifa) {
865                                 ifa_ref(info.rti_ifa);
866                                 rt->rt_ifa = info.rti_ifa;
867                                 rt->rt_ifp = info.rti_ifp;
868                         }
869                         /* Allow some flags to be toggled on change. */
870                         rt->rt_flags = (rt->rt_flags & ~RTF_FMASK) |
871                                     (rtm->rtm_flags & RTF_FMASK);
872                         rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
873                                         &rt->rt_rmx);
874                         rtm->rtm_index = rt->rt_ifp->if_index;
875                         if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
876                                rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info);
877                         /* FALLTHROUGH */
878                 case RTM_LOCK:
879                         /* We don't support locks anymore */
880                         break;
881                 }
882                 RT_UNLOCK(rt);
883                 break;
884
885         default:
886                 senderr(EOPNOTSUPP);
887         }
888
889 flush:
890         if (rtm) {
891                 if (error)
892                         rtm->rtm_errno = error;
893                 else
894                         rtm->rtm_flags |= RTF_DONE;
895         }
896         if (rt)         /* XXX can this be true? */
897                 RTFREE(rt);
898     {
899         struct rawcb *rp = NULL;
900         /*
901          * Check to see if we don't want our own messages.
902          */
903         if ((so->so_options & SO_USELOOPBACK) == 0) {
904                 if (route_cb.any_count <= 1) {
905                         if (rtm)
906                                 Free(rtm);
907                         m_freem(m);
908                         return (error);
909                 }
910                 /* There is another listener, so construct message */
911                 rp = sotorawcb(so);
912         }
913         if (rtm) {
914                 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
915                 if (m->m_pkthdr.len < rtm->rtm_msglen) {
916                         m_freem(m);
917                         m = NULL;
918                 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
919                         m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
920         }
921         if (m) {
922                 M_SETFIB(m, so->so_fibnum);
923                 m->m_flags |= RTS_FILTER_FIB;
924                 if (rp) {
925                         /*
926                          * XXX insure we don't get a copy by
927                          * invalidating our protocol
928                          */
929                         unsigned short family = rp->rcb_proto.sp_family;
930                         rp->rcb_proto.sp_family = 0;
931                         rt_dispatch(m, saf);
932                         rp->rcb_proto.sp_family = family;
933                 } else
934                         rt_dispatch(m, saf);
935         }
936         /* info.rti_info[RTAX_DST] (used above) can point inside of rtm */
937         if (rtm)
938                 Free(rtm);
939     }
940         return (error);
941 #undef  sa_equal
942 }
943
944 static void
945 rt_setmetrics(u_long which, const struct rt_metrics *in,
946         struct rt_metrics_lite *out)
947 {
948 #define metric(f, e) if (which & (f)) out->e = in->e;
949         /*
950          * Only these are stored in the routing entry since introduction
951          * of tcp hostcache. The rest is ignored.
952          */
953         metric(RTV_MTU, rmx_mtu);
954         metric(RTV_WEIGHT, rmx_weight);
955         /* Userland -> kernel timebase conversion. */
956         if (which & RTV_EXPIRE)
957                 out->rmx_expire = in->rmx_expire ?
958                     in->rmx_expire - time_second + time_uptime : 0;
959 #undef metric
960 }
961
962 static void
963 rt_getmetrics(const struct rt_metrics_lite *in, struct rt_metrics *out)
964 {
965 #define metric(e) out->e = in->e;
966         bzero(out, sizeof(*out));
967         metric(rmx_mtu);
968         metric(rmx_weight);
969         /* Kernel -> userland timebase conversion. */
970         out->rmx_expire = in->rmx_expire ?
971             in->rmx_expire - time_uptime + time_second : 0;
972 #undef metric
973 }
974
975 /*
976  * Extract the addresses of the passed sockaddrs.
977  * Do a little sanity checking so as to avoid bad memory references.
978  * This data is derived straight from userland.
979  */
980 static int
981 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
982 {
983         struct sockaddr *sa;
984         int i;
985
986         for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
987                 if ((rtinfo->rti_addrs & (1 << i)) == 0)
988                         continue;
989                 sa = (struct sockaddr *)cp;
990                 /*
991                  * It won't fit.
992                  */
993                 if (cp + sa->sa_len > cplim)
994                         return (EINVAL);
995                 /*
996                  * there are no more.. quit now
997                  * If there are more bits, they are in error.
998                  * I've seen this. route(1) can evidently generate these. 
999                  * This causes kernel to core dump.
1000                  * for compatibility, If we see this, point to a safe address.
1001                  */
1002                 if (sa->sa_len == 0) {
1003                         rtinfo->rti_info[i] = &sa_zero;
1004                         return (0); /* should be EINVAL but for compat */
1005                 }
1006                 /* accept it */
1007                 rtinfo->rti_info[i] = sa;
1008                 cp += SA_SIZE(sa);
1009         }
1010         return (0);
1011 }
1012
1013 static struct mbuf *
1014 rt_msg1(int type, struct rt_addrinfo *rtinfo)
1015 {
1016         struct rt_msghdr *rtm;
1017         struct mbuf *m;
1018         int i;
1019         struct sockaddr *sa;
1020         int len, dlen;
1021
1022         switch (type) {
1023
1024         case RTM_DELADDR:
1025         case RTM_NEWADDR:
1026                 len = sizeof(struct ifa_msghdr);
1027                 break;
1028
1029         case RTM_DELMADDR:
1030         case RTM_NEWMADDR:
1031                 len = sizeof(struct ifma_msghdr);
1032                 break;
1033
1034         case RTM_IFINFO:
1035                 len = sizeof(struct if_msghdr);
1036                 break;
1037
1038         case RTM_IFANNOUNCE:
1039         case RTM_IEEE80211:
1040                 len = sizeof(struct if_announcemsghdr);
1041                 break;
1042
1043         default:
1044                 len = sizeof(struct rt_msghdr);
1045         }
1046         if (len > MCLBYTES)
1047                 panic("rt_msg1");
1048         m = m_gethdr(M_DONTWAIT, MT_DATA);
1049         if (m && len > MHLEN) {
1050                 MCLGET(m, M_DONTWAIT);
1051                 if ((m->m_flags & M_EXT) == 0) {
1052                         m_free(m);
1053                         m = NULL;
1054                 }
1055         }
1056         if (m == NULL)
1057                 return (m);
1058         m->m_pkthdr.len = m->m_len = len;
1059         m->m_pkthdr.rcvif = NULL;
1060         rtm = mtod(m, struct rt_msghdr *);
1061         bzero((caddr_t)rtm, len);
1062         for (i = 0; i < RTAX_MAX; i++) {
1063                 if ((sa = rtinfo->rti_info[i]) == NULL)
1064                         continue;
1065                 rtinfo->rti_addrs |= (1 << i);
1066                 dlen = SA_SIZE(sa);
1067                 m_copyback(m, len, dlen, (caddr_t)sa);
1068                 len += dlen;
1069         }
1070         if (m->m_pkthdr.len != len) {
1071                 m_freem(m);
1072                 return (NULL);
1073         }
1074         rtm->rtm_msglen = len;
1075         rtm->rtm_version = RTM_VERSION;
1076         rtm->rtm_type = type;
1077         return (m);
1078 }
1079
1080 static int
1081 rt_msg2(int type, struct rt_addrinfo *rtinfo, caddr_t cp, struct walkarg *w)
1082 {
1083         int i;
1084         int len, dlen, second_time = 0;
1085         caddr_t cp0;
1086
1087         rtinfo->rti_addrs = 0;
1088 again:
1089         switch (type) {
1090
1091         case RTM_DELADDR:
1092         case RTM_NEWADDR:
1093                 len = sizeof(struct ifa_msghdr);
1094                 break;
1095
1096         case RTM_IFINFO:
1097 #ifdef COMPAT_FREEBSD32
1098                 if (w != NULL && w->w_req->flags & SCTL_MASK32) {
1099                         len = sizeof(struct if_msghdr32);
1100                         break;
1101                 }
1102 #endif
1103                 len = sizeof(struct if_msghdr);
1104                 break;
1105
1106         case RTM_NEWMADDR:
1107                 len = sizeof(struct ifma_msghdr);
1108                 break;
1109
1110         default:
1111                 len = sizeof(struct rt_msghdr);
1112         }
1113         cp0 = cp;
1114         if (cp0)
1115                 cp += len;
1116         for (i = 0; i < RTAX_MAX; i++) {
1117                 struct sockaddr *sa;
1118
1119                 if ((sa = rtinfo->rti_info[i]) == NULL)
1120                         continue;
1121                 rtinfo->rti_addrs |= (1 << i);
1122                 dlen = SA_SIZE(sa);
1123                 if (cp) {
1124                         bcopy((caddr_t)sa, cp, (unsigned)dlen);
1125                         cp += dlen;
1126                 }
1127                 len += dlen;
1128         }
1129         len = ALIGN(len);
1130         if (cp == NULL && w != NULL && !second_time) {
1131                 struct walkarg *rw = w;
1132
1133                 if (rw->w_req) {
1134                         if (rw->w_tmemsize < len) {
1135                                 if (rw->w_tmem)
1136                                         free(rw->w_tmem, M_RTABLE);
1137                                 rw->w_tmem = (caddr_t)
1138                                         malloc(len, M_RTABLE, M_NOWAIT);
1139                                 if (rw->w_tmem)
1140                                         rw->w_tmemsize = len;
1141                         }
1142                         if (rw->w_tmem) {
1143                                 cp = rw->w_tmem;
1144                                 second_time = 1;
1145                                 goto again;
1146                         }
1147                 }
1148         }
1149         if (cp) {
1150                 struct rt_msghdr *rtm = (struct rt_msghdr *)cp0;
1151
1152                 rtm->rtm_version = RTM_VERSION;
1153                 rtm->rtm_type = type;
1154                 rtm->rtm_msglen = len;
1155         }
1156         return (len);
1157 }
1158
1159 /*
1160  * This routine is called to generate a message from the routing
1161  * socket indicating that a redirect has occured, a routing lookup
1162  * has failed, or that a protocol has detected timeouts to a particular
1163  * destination.
1164  */
1165 void
1166 rt_missmsg_fib(int type, struct rt_addrinfo *rtinfo, int flags, int error,
1167     int fibnum)
1168 {
1169         struct rt_msghdr *rtm;
1170         struct mbuf *m;
1171         struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
1172
1173         if (route_cb.any_count == 0)
1174                 return;
1175         m = rt_msg1(type, rtinfo);
1176         if (m == NULL)
1177                 return;
1178
1179         if (fibnum != RTS_ALLFIBS) {
1180                 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out "
1181                     "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs));
1182                 M_SETFIB(m, fibnum);
1183                 m->m_flags |= RTS_FILTER_FIB;
1184         }
1185
1186         rtm = mtod(m, struct rt_msghdr *);
1187         rtm->rtm_flags = RTF_DONE | flags;
1188         rtm->rtm_errno = error;
1189         rtm->rtm_addrs = rtinfo->rti_addrs;
1190         rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1191 }
1192
1193 void
1194 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
1195 {
1196
1197         rt_missmsg_fib(type, rtinfo, flags, error, RTS_ALLFIBS);
1198 }
1199
1200 /*
1201  * This routine is called to generate a message from the routing
1202  * socket indicating that the status of a network interface has changed.
1203  */
1204 void
1205 rt_ifmsg(struct ifnet *ifp)
1206 {
1207         struct if_msghdr *ifm;
1208         struct mbuf *m;
1209         struct rt_addrinfo info;
1210
1211         if (route_cb.any_count == 0)
1212                 return;
1213         bzero((caddr_t)&info, sizeof(info));
1214         m = rt_msg1(RTM_IFINFO, &info);
1215         if (m == NULL)
1216                 return;
1217         ifm = mtod(m, struct if_msghdr *);
1218         ifm->ifm_index = ifp->if_index;
1219         ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1220         ifm->ifm_data = ifp->if_data;
1221         ifm->ifm_addrs = 0;
1222         rt_dispatch(m, AF_UNSPEC);
1223 }
1224
1225 /*
1226  * This is called to generate messages from the routing socket
1227  * indicating a network interface has had addresses associated with it.
1228  * if we ever reverse the logic and replace messages TO the routing
1229  * socket indicate a request to configure interfaces, then it will
1230  * be unnecessary as the routing socket will automatically generate
1231  * copies of it.
1232  */
1233 void
1234 rt_newaddrmsg_fib(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt,
1235     int fibnum)
1236 {
1237         struct rt_addrinfo info;
1238         struct sockaddr *sa = NULL;
1239         int pass;
1240         struct mbuf *m = NULL;
1241         struct ifnet *ifp = ifa->ifa_ifp;
1242
1243         KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE,
1244                 ("unexpected cmd %u", cmd));
1245 #if defined(INET) || defined(INET6)
1246 #ifdef SCTP
1247         /*
1248          * notify the SCTP stack
1249          * this will only get called when an address is added/deleted
1250          * XXX pass the ifaddr struct instead if ifa->ifa_addr...
1251          */
1252         sctp_addr_change(ifa, cmd);
1253 #endif /* SCTP */
1254 #endif
1255         if (route_cb.any_count == 0)
1256                 return;
1257         for (pass = 1; pass < 3; pass++) {
1258                 bzero((caddr_t)&info, sizeof(info));
1259                 if ((cmd == RTM_ADD && pass == 1) ||
1260                     (cmd == RTM_DELETE && pass == 2)) {
1261                         struct ifa_msghdr *ifam;
1262                         int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
1263
1264                         info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1265                         info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1266                         info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1267                         info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1268                         if ((m = rt_msg1(ncmd, &info)) == NULL)
1269                                 continue;
1270                         ifam = mtod(m, struct ifa_msghdr *);
1271                         ifam->ifam_index = ifp->if_index;
1272                         ifam->ifam_metric = ifa->ifa_metric;
1273                         ifam->ifam_flags = ifa->ifa_flags;
1274                         ifam->ifam_addrs = info.rti_addrs;
1275                 }
1276                 if ((cmd == RTM_ADD && pass == 2) ||
1277                     (cmd == RTM_DELETE && pass == 1)) {
1278                         struct rt_msghdr *rtm;
1279
1280                         if (rt == NULL)
1281                                 continue;
1282                         info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1283                         info.rti_info[RTAX_DST] = sa = rt_key(rt);
1284                         info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1285                         if ((m = rt_msg1(cmd, &info)) == NULL)
1286                                 continue;
1287                         rtm = mtod(m, struct rt_msghdr *);
1288                         rtm->rtm_index = ifp->if_index;
1289                         rtm->rtm_flags |= rt->rt_flags;
1290                         rtm->rtm_errno = error;
1291                         rtm->rtm_addrs = info.rti_addrs;
1292                 }
1293                 if (fibnum != RTS_ALLFIBS) {
1294                         KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: "
1295                             "fibnum out of range 0 <= %d < %d", __func__,
1296                              fibnum, rt_numfibs));
1297                         M_SETFIB(m, fibnum);
1298                         m->m_flags |= RTS_FILTER_FIB;
1299                 }
1300                 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1301         }
1302 }
1303
1304 void
1305 rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt)
1306 {
1307
1308         rt_newaddrmsg_fib(cmd, ifa, error, rt, RTS_ALLFIBS);
1309 }
1310
1311 /*
1312  * This is the analogue to the rt_newaddrmsg which performs the same
1313  * function but for multicast group memberhips.  This is easier since
1314  * there is no route state to worry about.
1315  */
1316 void
1317 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
1318 {
1319         struct rt_addrinfo info;
1320         struct mbuf *m = NULL;
1321         struct ifnet *ifp = ifma->ifma_ifp;
1322         struct ifma_msghdr *ifmam;
1323
1324         if (route_cb.any_count == 0)
1325                 return;
1326
1327         bzero((caddr_t)&info, sizeof(info));
1328         info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1329         info.rti_info[RTAX_IFP] = ifp ? ifp->if_addr->ifa_addr : NULL;
1330         /*
1331          * If a link-layer address is present, present it as a ``gateway''
1332          * (similarly to how ARP entries, e.g., are presented).
1333          */
1334         info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr;
1335         m = rt_msg1(cmd, &info);
1336         if (m == NULL)
1337                 return;
1338         ifmam = mtod(m, struct ifma_msghdr *);
1339         KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n",
1340             __func__));
1341         ifmam->ifmam_index = ifp->if_index;
1342         ifmam->ifmam_addrs = info.rti_addrs;
1343         rt_dispatch(m, ifma->ifma_addr ? ifma->ifma_addr->sa_family : AF_UNSPEC);
1344 }
1345
1346 static struct mbuf *
1347 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1348         struct rt_addrinfo *info)
1349 {
1350         struct if_announcemsghdr *ifan;
1351         struct mbuf *m;
1352
1353         if (route_cb.any_count == 0)
1354                 return NULL;
1355         bzero((caddr_t)info, sizeof(*info));
1356         m = rt_msg1(type, info);
1357         if (m != NULL) {
1358                 ifan = mtod(m, struct if_announcemsghdr *);
1359                 ifan->ifan_index = ifp->if_index;
1360                 strlcpy(ifan->ifan_name, ifp->if_xname,
1361                         sizeof(ifan->ifan_name));
1362                 ifan->ifan_what = what;
1363         }
1364         return m;
1365 }
1366
1367 /*
1368  * This is called to generate routing socket messages indicating
1369  * IEEE80211 wireless events.
1370  * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1371  */
1372 void
1373 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
1374 {
1375         struct mbuf *m;
1376         struct rt_addrinfo info;
1377
1378         m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1379         if (m != NULL) {
1380                 /*
1381                  * Append the ieee80211 data.  Try to stick it in the
1382                  * mbuf containing the ifannounce msg; otherwise allocate
1383                  * a new mbuf and append.
1384                  *
1385                  * NB: we assume m is a single mbuf.
1386                  */
1387                 if (data_len > M_TRAILINGSPACE(m)) {
1388                         struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
1389                         if (n == NULL) {
1390                                 m_freem(m);
1391                                 return;
1392                         }
1393                         bcopy(data, mtod(n, void *), data_len);
1394                         n->m_len = data_len;
1395                         m->m_next = n;
1396                 } else if (data_len > 0) {
1397                         bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
1398                         m->m_len += data_len;
1399                 }
1400                 if (m->m_flags & M_PKTHDR)
1401                         m->m_pkthdr.len += data_len;
1402                 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
1403                 rt_dispatch(m, AF_UNSPEC);
1404         }
1405 }
1406
1407 /*
1408  * This is called to generate routing socket messages indicating
1409  * network interface arrival and departure.
1410  */
1411 void
1412 rt_ifannouncemsg(struct ifnet *ifp, int what)
1413 {
1414         struct mbuf *m;
1415         struct rt_addrinfo info;
1416
1417         m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
1418         if (m != NULL)
1419                 rt_dispatch(m, AF_UNSPEC);
1420 }
1421
1422 static void
1423 rt_dispatch(struct mbuf *m, sa_family_t saf)
1424 {
1425         struct m_tag *tag;
1426
1427         /*
1428          * Preserve the family from the sockaddr, if any, in an m_tag for
1429          * use when injecting the mbuf into the routing socket buffer from
1430          * the netisr.
1431          */
1432         if (saf != AF_UNSPEC) {
1433                 tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short),
1434                     M_NOWAIT);
1435                 if (tag == NULL) {
1436                         m_freem(m);
1437                         return;
1438                 }
1439                 *(unsigned short *)(tag + 1) = saf;
1440                 m_tag_prepend(m, tag);
1441         }
1442 #ifdef VIMAGE
1443         if (V_loif)
1444                 m->m_pkthdr.rcvif = V_loif;
1445         else {
1446                 m_freem(m);
1447                 return;
1448         }
1449 #endif
1450         netisr_queue(NETISR_ROUTE, m);  /* mbuf is free'd on failure. */
1451 }
1452
1453 /*
1454  * This is used in dumping the kernel table via sysctl().
1455  */
1456 static int
1457 sysctl_dumpentry(struct radix_node *rn, void *vw)
1458 {
1459         struct walkarg *w = vw;
1460         struct rtentry *rt = (struct rtentry *)rn;
1461         int error = 0, size;
1462         struct rt_addrinfo info;
1463
1464         if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1465                 return 0;
1466         if ((rt->rt_flags & RTF_HOST) == 0
1467             ? jailed_without_vnet(w->w_req->td->td_ucred)
1468             : prison_if(w->w_req->td->td_ucred, rt_key(rt)) != 0)
1469                 return (0);
1470         bzero((caddr_t)&info, sizeof(info));
1471         info.rti_info[RTAX_DST] = rt_key(rt);
1472         info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1473         info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1474         info.rti_info[RTAX_GENMASK] = 0;
1475         if (rt->rt_ifp) {
1476                 info.rti_info[RTAX_IFP] = rt->rt_ifp->if_addr->ifa_addr;
1477                 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
1478                 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1479                         info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr;
1480         }
1481         size = rt_msg2(RTM_GET, &info, NULL, w);
1482         if (w->w_req && w->w_tmem) {
1483                 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
1484
1485                 rtm->rtm_flags = rt->rt_flags;
1486                 /*
1487                  * let's be honest about this being a retarded hack
1488                  */
1489                 rtm->rtm_fmask = rt->rt_rmx.rmx_pksent;
1490                 rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx);
1491                 rtm->rtm_index = rt->rt_ifp->if_index;
1492                 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
1493                 rtm->rtm_addrs = info.rti_addrs;
1494                 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
1495                 return (error);
1496         }
1497         return (error);
1498 }
1499
1500 #ifdef COMPAT_FREEBSD32
1501 static void
1502 copy_ifdata32(struct if_data *src, struct if_data32 *dst)
1503 {
1504
1505         bzero(dst, sizeof(*dst));
1506         CP(*src, *dst, ifi_type);
1507         CP(*src, *dst, ifi_physical);
1508         CP(*src, *dst, ifi_addrlen);
1509         CP(*src, *dst, ifi_hdrlen);
1510         CP(*src, *dst, ifi_link_state);
1511         dst->ifi_datalen = sizeof(struct if_data32);
1512         CP(*src, *dst, ifi_mtu);
1513         CP(*src, *dst, ifi_metric);
1514         CP(*src, *dst, ifi_baudrate);
1515         CP(*src, *dst, ifi_ipackets);
1516         CP(*src, *dst, ifi_ierrors);
1517         CP(*src, *dst, ifi_opackets);
1518         CP(*src, *dst, ifi_oerrors);
1519         CP(*src, *dst, ifi_collisions);
1520         CP(*src, *dst, ifi_ibytes);
1521         CP(*src, *dst, ifi_obytes);
1522         CP(*src, *dst, ifi_imcasts);
1523         CP(*src, *dst, ifi_omcasts);
1524         CP(*src, *dst, ifi_iqdrops);
1525         CP(*src, *dst, ifi_noproto);
1526         CP(*src, *dst, ifi_hwassist);
1527         CP(*src, *dst, ifi_epoch);
1528         TV_CP(*src, *dst, ifi_lastchange);
1529 }
1530 #endif
1531
1532 static int
1533 sysctl_iflist(int af, struct walkarg *w)
1534 {
1535         struct ifnet *ifp;
1536         struct ifaddr *ifa;
1537         struct rt_addrinfo info;
1538         int len, error = 0;
1539
1540         bzero((caddr_t)&info, sizeof(info));
1541         IFNET_RLOCK();
1542         TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1543                 if (w->w_arg && w->w_arg != ifp->if_index)
1544                         continue;
1545                 IF_ADDR_LOCK(ifp);
1546                 ifa = ifp->if_addr;
1547                 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
1548                 len = rt_msg2(RTM_IFINFO, &info, NULL, w);
1549                 info.rti_info[RTAX_IFP] = NULL;
1550                 if (w->w_req && w->w_tmem) {
1551                         struct if_msghdr *ifm;
1552
1553 #ifdef COMPAT_FREEBSD32
1554                         if (w->w_req->flags & SCTL_MASK32) {
1555                                 struct if_msghdr32 *ifm32;
1556
1557                                 ifm32 = (struct if_msghdr32 *)w->w_tmem;
1558                                 ifm32->ifm_index = ifp->if_index;
1559                                 ifm32->ifm_flags = ifp->if_flags |
1560                                     ifp->if_drv_flags;
1561                                 copy_ifdata32(&ifp->if_data, &ifm32->ifm_data);
1562                                 ifm32->ifm_addrs = info.rti_addrs;
1563                                 error = SYSCTL_OUT(w->w_req, (caddr_t)ifm32,
1564                                     len);
1565                                 goto sysctl_out;
1566                         }
1567 #endif
1568                         ifm = (struct if_msghdr *)w->w_tmem;
1569                         ifm->ifm_index = ifp->if_index;
1570                         ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1571                         ifm->ifm_data = ifp->if_data;
1572                         ifm->ifm_addrs = info.rti_addrs;
1573                         error = SYSCTL_OUT(w->w_req, (caddr_t)ifm, len);
1574 #ifdef COMPAT_FREEBSD32
1575                 sysctl_out:
1576 #endif
1577                         if (error)
1578                                 goto done;
1579                 }
1580                 while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != NULL) {
1581                         if (af && af != ifa->ifa_addr->sa_family)
1582                                 continue;
1583                         if (prison_if(w->w_req->td->td_ucred,
1584                             ifa->ifa_addr) != 0)
1585                                 continue;
1586                         info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1587                         info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1588                         info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1589                         len = rt_msg2(RTM_NEWADDR, &info, NULL, w);
1590                         if (w->w_req && w->w_tmem) {
1591                                 struct ifa_msghdr *ifam;
1592
1593                                 ifam = (struct ifa_msghdr *)w->w_tmem;
1594                                 ifam->ifam_index = ifa->ifa_ifp->if_index;
1595                                 ifam->ifam_flags = ifa->ifa_flags;
1596                                 ifam->ifam_metric = ifa->ifa_metric;
1597                                 ifam->ifam_addrs = info.rti_addrs;
1598                                 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
1599                                 if (error)
1600                                         goto done;
1601                         }
1602                 }
1603                 IF_ADDR_UNLOCK(ifp);
1604                 info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] =
1605                         info.rti_info[RTAX_BRD] = NULL;
1606         }
1607 done:
1608         if (ifp != NULL)
1609                 IF_ADDR_UNLOCK(ifp);
1610         IFNET_RUNLOCK();
1611         return (error);
1612 }
1613
1614 static int
1615 sysctl_ifmalist(int af, struct walkarg *w)
1616 {
1617         struct ifnet *ifp;
1618         struct ifmultiaddr *ifma;
1619         struct  rt_addrinfo info;
1620         int     len, error = 0;
1621         struct ifaddr *ifa;
1622
1623         bzero((caddr_t)&info, sizeof(info));
1624         IFNET_RLOCK();
1625         TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1626                 if (w->w_arg && w->w_arg != ifp->if_index)
1627                         continue;
1628                 ifa = ifp->if_addr;
1629                 info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL;
1630                 IF_ADDR_LOCK(ifp);
1631                 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1632                         if (af && af != ifma->ifma_addr->sa_family)
1633                                 continue;
1634                         if (prison_if(w->w_req->td->td_ucred,
1635                             ifma->ifma_addr) != 0)
1636                                 continue;
1637                         info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1638                         info.rti_info[RTAX_GATEWAY] =
1639                             (ifma->ifma_addr->sa_family != AF_LINK) ?
1640                             ifma->ifma_lladdr : NULL;
1641                         len = rt_msg2(RTM_NEWMADDR, &info, NULL, w);
1642                         if (w->w_req && w->w_tmem) {
1643                                 struct ifma_msghdr *ifmam;
1644
1645                                 ifmam = (struct ifma_msghdr *)w->w_tmem;
1646                                 ifmam->ifmam_index = ifma->ifma_ifp->if_index;
1647                                 ifmam->ifmam_flags = 0;
1648                                 ifmam->ifmam_addrs = info.rti_addrs;
1649                                 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
1650                                 if (error) {
1651                                         IF_ADDR_UNLOCK(ifp);
1652                                         goto done;
1653                                 }
1654                         }
1655                 }
1656                 IF_ADDR_UNLOCK(ifp);
1657         }
1658 done:
1659         IFNET_RUNLOCK();
1660         return (error);
1661 }
1662
1663 static int
1664 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
1665 {
1666         int     *name = (int *)arg1;
1667         u_int   namelen = arg2;
1668         struct radix_node_head *rnh = NULL; /* silence compiler. */
1669         int     i, lim, error = EINVAL;
1670         u_char  af;
1671         struct  walkarg w;
1672
1673         name ++;
1674         namelen--;
1675         if (req->newptr)
1676                 return (EPERM);
1677         if (namelen != 3)
1678                 return ((namelen < 3) ? EISDIR : ENOTDIR);
1679         af = name[0];
1680         if (af > AF_MAX)
1681                 return (EINVAL);
1682         bzero(&w, sizeof(w));
1683         w.w_op = name[1];
1684         w.w_arg = name[2];
1685         w.w_req = req;
1686
1687         error = sysctl_wire_old_buffer(req, 0);
1688         if (error)
1689                 return (error);
1690         switch (w.w_op) {
1691
1692         case NET_RT_DUMP:
1693         case NET_RT_FLAGS:
1694                 if (af == 0) {                  /* dump all tables */
1695                         i = 1;
1696                         lim = AF_MAX;
1697                 } else                          /* dump only one table */
1698                         i = lim = af;
1699
1700                 /*
1701                  * take care of llinfo entries, the caller must
1702                  * specify an AF
1703                  */
1704                 if (w.w_op == NET_RT_FLAGS &&
1705                     (w.w_arg == 0 || w.w_arg & RTF_LLINFO)) {
1706                         if (af != 0)
1707                                 error = lltable_sysctl_dumparp(af, w.w_req);
1708                         else
1709                                 error = EINVAL;
1710                         break;
1711                 }
1712                 /*
1713                  * take care of routing entries
1714                  */
1715                 for (error = 0; error == 0 && i <= lim; i++) {
1716                         rnh = rt_tables_get_rnh(req->td->td_proc->p_fibnum, i);
1717                         if (rnh != NULL) {
1718                                 RADIX_NODE_HEAD_LOCK(rnh); 
1719                                 error = rnh->rnh_walktree(rnh,
1720                                     sysctl_dumpentry, &w);
1721                                 RADIX_NODE_HEAD_UNLOCK(rnh);
1722                         } else if (af != 0)
1723                                 error = EAFNOSUPPORT;
1724                 }
1725                 break;
1726
1727         case NET_RT_IFLIST:
1728                 error = sysctl_iflist(af, &w);
1729                 break;
1730
1731         case NET_RT_IFMALIST:
1732                 error = sysctl_ifmalist(af, &w);
1733                 break;
1734         }
1735         if (w.w_tmem)
1736                 free(w.w_tmem, M_RTABLE);
1737         return (error);
1738 }
1739
1740 SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
1741
1742 /*
1743  * Definitions of protocols supported in the ROUTE domain.
1744  */
1745
1746 static struct domain routedomain;               /* or at least forward */
1747
1748 static struct protosw routesw[] = {
1749 {
1750         .pr_type =              SOCK_RAW,
1751         .pr_domain =            &routedomain,
1752         .pr_flags =             PR_ATOMIC|PR_ADDR,
1753         .pr_output =            route_output,
1754         .pr_ctlinput =          raw_ctlinput,
1755         .pr_init =              raw_init,
1756         .pr_usrreqs =           &route_usrreqs
1757 }
1758 };
1759
1760 static struct domain routedomain = {
1761         .dom_family =           PF_ROUTE,
1762         .dom_name =              "route",
1763         .dom_protosw =          routesw,
1764         .dom_protoswNPROTOSW =  &routesw[sizeof(routesw)/sizeof(routesw[0])]
1765 };
1766
1767 VNET_DOMAIN_SET(route);