Merge from vendor branch BINUTILS:
[dragonfly.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  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *      @(#)rtsock.c    8.7 (Berkeley) 10/12/95
34  * $FreeBSD: src/sys/net/rtsock.c,v 1.44.2.11 2002/12/04 14:05:41 ru Exp $
35  * $DragonFly: src/sys/net/rtsock.c,v 1.8 2004/01/06 03:17:25 dillon Exp $
36  */
37
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/sysctl.h>
43 #include <sys/proc.h>
44 #include <sys/malloc.h>
45 #include <sys/mbuf.h>
46 #include <sys/socket.h>
47 #include <sys/socketvar.h>
48 #include <sys/domain.h>
49 #include <sys/protosw.h>
50
51 #include <net/if.h>
52 #include <net/route.h>
53 #include <net/raw_cb.h>
54
55 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
56
57 static struct   sockaddr route_dst = { 2, PF_ROUTE, };
58 static struct   sockaddr route_src = { 2, PF_ROUTE, };
59 static struct   sockaddr sa_zero   = { sizeof(sa_zero), AF_INET, };
60 static struct   sockproto route_proto = { PF_ROUTE, };
61
62 struct walkarg {
63         int     w_tmemsize;
64         int     w_op, w_arg;
65         caddr_t w_tmem;
66         struct sysctl_req *w_req;
67 };
68
69 static struct mbuf *
70                 rt_msg1 (int, struct rt_addrinfo *);
71 static int      rt_msg2 (int,
72                     struct rt_addrinfo *, caddr_t, struct walkarg *);
73 static int      rt_xaddrs (caddr_t, caddr_t, struct rt_addrinfo *);
74 static int      sysctl_dumpentry (struct radix_node *rn, void *vw);
75 static int      sysctl_iflist (int af, struct walkarg *w);
76 static int       route_output (struct mbuf *, struct socket *);
77 static void      rt_setmetrics (u_long, struct rt_metrics *, struct rt_metrics *);
78
79 /* Sleazy use of local variables throughout file, warning!!!! */
80 #define dst     info.rti_info[RTAX_DST]
81 #define gate    info.rti_info[RTAX_GATEWAY]
82 #define netmask info.rti_info[RTAX_NETMASK]
83 #define genmask info.rti_info[RTAX_GENMASK]
84 #define ifpaddr info.rti_info[RTAX_IFP]
85 #define ifaaddr info.rti_info[RTAX_IFA]
86 #define brdaddr info.rti_info[RTAX_BRD]
87
88 /*
89  * It really doesn't make any sense at all for this code to share much
90  * with raw_usrreq.c, since its functionality is so restricted.  XXX
91  */
92 static int
93 rts_abort(struct socket *so)
94 {
95         int s, error;
96         s = splnet();
97         error = raw_usrreqs.pru_abort(so);
98         splx(s);
99         return error;
100 }
101
102 /* pru_accept is EOPNOTSUPP */
103
104 static int
105 rts_attach(struct socket *so, int proto, struct thread *td)
106 {
107         struct rawcb *rp;
108         int s, error;
109
110         if (sotorawcb(so) != 0)
111                 return EISCONN; /* XXX panic? */
112         MALLOC(rp, struct rawcb *, sizeof *rp, M_PCB, M_WAITOK|M_ZERO);
113         if (rp == 0)
114                 return ENOBUFS;
115
116         /*
117          * The splnet() is necessary to block protocols from sending
118          * error notifications (like RTM_REDIRECT or RTM_LOSING) while
119          * this PCB is extant but incompletely initialized.
120          * Probably we should try to do more of this work beforehand and
121          * eliminate the spl.
122          */
123         s = splnet();
124         so->so_pcb = (caddr_t)rp;
125         error = raw_attach(so, proto);
126         rp = sotorawcb(so);
127         if (error) {
128                 splx(s);
129                 free(rp, M_PCB);
130                 return error;
131         }
132         switch(rp->rcb_proto.sp_protocol) {
133         case AF_INET:
134                 route_cb.ip_count++;
135                 break;
136         case AF_INET6:
137                 route_cb.ip6_count++;
138                 break;
139         case AF_IPX:
140                 route_cb.ipx_count++;
141                 break;
142         case AF_NS:
143                 route_cb.ns_count++;
144                 break;
145         }
146         rp->rcb_faddr = &route_src;
147         route_cb.any_count++;
148         soisconnected(so);
149         so->so_options |= SO_USELOOPBACK;
150         splx(s);
151         return 0;
152 }
153
154 static int
155 rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
156 {
157         int s, error;
158         s = splnet();
159         error = raw_usrreqs.pru_bind(so, nam, td); /* xxx just EINVAL */
160         splx(s);
161         return error;
162 }
163
164 static int
165 rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
166 {
167         int s, error;
168         s = splnet();
169         error = raw_usrreqs.pru_connect(so, nam, td); /* XXX just EINVAL */
170         splx(s);
171         return error;
172 }
173
174 /* pru_connect2 is EOPNOTSUPP */
175 /* pru_control is EOPNOTSUPP */
176
177 static int
178 rts_detach(struct socket *so)
179 {
180         struct rawcb *rp = sotorawcb(so);
181         int s, error;
182
183         s = splnet();
184         if (rp != 0) {
185                 switch(rp->rcb_proto.sp_protocol) {
186                 case AF_INET:
187                         route_cb.ip_count--;
188                         break;
189                 case AF_INET6:
190                         route_cb.ip6_count--;
191                         break;
192                 case AF_IPX:
193                         route_cb.ipx_count--;
194                         break;
195                 case AF_NS:
196                         route_cb.ns_count--;
197                         break;
198                 }
199                 route_cb.any_count--;
200         }
201         error = raw_usrreqs.pru_detach(so);
202         splx(s);
203         return error;
204 }
205
206 static int
207 rts_disconnect(struct socket *so)
208 {
209         int s, error;
210         s = splnet();
211         error = raw_usrreqs.pru_disconnect(so);
212         splx(s);
213         return error;
214 }
215
216 /* pru_listen is EOPNOTSUPP */
217
218 static int
219 rts_peeraddr(struct socket *so, struct sockaddr **nam)
220 {
221         int s, error;
222         s = splnet();
223         error = raw_usrreqs.pru_peeraddr(so, nam);
224         splx(s);
225         return error;
226 }
227
228 /* pru_rcvd is EOPNOTSUPP */
229 /* pru_rcvoob is EOPNOTSUPP */
230
231 static int
232 rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
233          struct mbuf *control, struct thread *td)
234 {
235         int s, error;
236         s = splnet();
237         error = raw_usrreqs.pru_send(so, flags, m, nam, control, td);
238         splx(s);
239         return error;
240 }
241
242 /* pru_sense is null */
243
244 static int
245 rts_shutdown(struct socket *so)
246 {
247         int s, error;
248         s = splnet();
249         error = raw_usrreqs.pru_shutdown(so);
250         splx(s);
251         return error;
252 }
253
254 static int
255 rts_sockaddr(struct socket *so, struct sockaddr **nam)
256 {
257         int s, error;
258         s = splnet();
259         error = raw_usrreqs.pru_sockaddr(so, nam);
260         splx(s);
261         return error;
262 }
263
264 static struct pr_usrreqs route_usrreqs = {
265         rts_abort, pru_accept_notsupp, rts_attach, rts_bind, rts_connect,
266         pru_connect2_notsupp, pru_control_notsupp, rts_detach, rts_disconnect,
267         pru_listen_notsupp, rts_peeraddr, pru_rcvd_notsupp, pru_rcvoob_notsupp,
268         rts_send, pru_sense_null, rts_shutdown, rts_sockaddr,
269         sosend, soreceive, sopoll
270 };
271
272 /*ARGSUSED*/
273 static int
274 route_output(m, so)
275         struct mbuf *m;
276         struct socket *so;
277 {
278         struct rt_msghdr *rtm = 0;
279         struct rtentry *rt = 0;
280         struct rtentry *saved_nrt = 0;
281         struct radix_node_head *rnh;
282         struct rt_addrinfo info;
283         int len, error = 0;
284         struct ifnet *ifp = 0;
285         struct ifaddr *ifa = 0;
286
287 #define senderr(e) { error = e; goto flush;}
288         if (m == 0 || ((m->m_len < sizeof(long)) &&
289                        (m = m_pullup(m, sizeof(long))) == 0))
290                 return (ENOBUFS);
291         if ((m->m_flags & M_PKTHDR) == 0)
292                 panic("route_output");
293         len = m->m_pkthdr.len;
294         if (len < sizeof(*rtm) ||
295             len != mtod(m, struct rt_msghdr *)->rtm_msglen) {
296                 dst = 0;
297                 senderr(EINVAL);
298         }
299         R_Malloc(rtm, struct rt_msghdr *, len);
300         if (rtm == 0) {
301                 dst = 0;
302                 senderr(ENOBUFS);
303         }
304         m_copydata(m, 0, len, (caddr_t)rtm);
305         if (rtm->rtm_version != RTM_VERSION) {
306                 dst = 0;
307                 senderr(EPROTONOSUPPORT);
308         }
309         rtm->rtm_pid = curproc->p_pid;
310         bzero(&info, sizeof(info));
311         info.rti_addrs = rtm->rtm_addrs;
312         if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info)) {
313                 dst = 0;
314                 senderr(EINVAL);
315         }
316         info.rti_flags = rtm->rtm_flags;
317         if (dst == 0 || (dst->sa_family >= AF_MAX)
318             || (gate != 0 && (gate->sa_family >= AF_MAX)))
319                 senderr(EINVAL);
320         if (genmask) {
321                 struct radix_node *t;
322                 t = rn_addmask((caddr_t)genmask, 0, 1);
323                 if (t && Bcmp((caddr_t *)genmask + 1, (caddr_t *)t->rn_key + 1,
324                               *(u_char *)t->rn_key - 1) == 0)
325                         genmask = (struct sockaddr *)(t->rn_key);
326                 else
327                         senderr(ENOBUFS);
328         }
329
330         /*
331          * Verify that the caller has the appropriate privilege; RTM_GET
332          * is the only operation the non-superuser is allowed.
333          */
334         if (rtm->rtm_type != RTM_GET && suser_cred(so->so_cred, 0) != 0)
335                 senderr(EPERM);
336
337         switch (rtm->rtm_type) {
338
339         case RTM_ADD:
340                 if (gate == 0)
341                         senderr(EINVAL);
342                 error = rtrequest1(RTM_ADD, &info, &saved_nrt);
343                 if (error == 0 && saved_nrt) {
344                         rt_setmetrics(rtm->rtm_inits,
345                                 &rtm->rtm_rmx, &saved_nrt->rt_rmx);
346                         saved_nrt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
347                         saved_nrt->rt_rmx.rmx_locks |=
348                                 (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
349                         saved_nrt->rt_refcnt--;
350                         saved_nrt->rt_genmask = genmask;
351                 }
352                 break;
353
354         case RTM_DELETE:
355                 error = rtrequest1(RTM_DELETE, &info, &saved_nrt);
356                 if (error == 0) {
357                         if ((rt = saved_nrt))
358                                 rt->rt_refcnt++;
359                         goto report;
360                 }
361                 break;
362
363         case RTM_GET:
364         case RTM_CHANGE:
365         case RTM_LOCK:
366                 if ((rnh = rt_tables[dst->sa_family]) == 0) {
367                         senderr(EAFNOSUPPORT);
368                 } else if ((rt = (struct rtentry *)
369                                 rnh->rnh_lookup(dst, netmask, rnh)) != NULL)
370                         rt->rt_refcnt++;
371                 else
372                         senderr(ESRCH);
373                 switch(rtm->rtm_type) {
374
375                 case RTM_GET:
376                 report:
377                         dst = rt_key(rt);
378                         gate = rt->rt_gateway;
379                         netmask = rt_mask(rt);
380                         genmask = rt->rt_genmask;
381                         if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
382                                 ifp = rt->rt_ifp;
383                                 if (ifp) {
384                                         ifpaddr = TAILQ_FIRST(&ifp->if_addrhead)->ifa_addr;
385                                         ifaaddr = rt->rt_ifa->ifa_addr;
386                                         if (ifp->if_flags & IFF_POINTOPOINT)
387                                                 brdaddr = rt->rt_ifa->ifa_dstaddr;
388                                         rtm->rtm_index = ifp->if_index;
389                                 } else {
390                                         ifpaddr = 0;
391                                         ifaaddr = 0;
392                             }
393                         }
394                         len = rt_msg2(rtm->rtm_type, &info, (caddr_t)0,
395                                 (struct walkarg *)0);
396                         if (len > rtm->rtm_msglen) {
397                                 struct rt_msghdr *new_rtm;
398                                 R_Malloc(new_rtm, struct rt_msghdr *, len);
399                                 if (new_rtm == 0)
400                                         senderr(ENOBUFS);
401                                 Bcopy(rtm, new_rtm, rtm->rtm_msglen);
402                                 Free(rtm); rtm = new_rtm;
403                         }
404                         (void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm,
405                                 (struct walkarg *)0);
406                         rtm->rtm_flags = rt->rt_flags;
407                         rtm->rtm_rmx = rt->rt_rmx;
408                         rtm->rtm_addrs = info.rti_addrs;
409                         break;
410
411                 case RTM_CHANGE:
412                         /* new gateway could require new ifaddr, ifp;
413                            flags may also be different; ifp may be specified
414                            by ll sockaddr when protocol address is ambiguous */
415 #define equal(a1, a2) (bcmp((caddr_t)(a1), (caddr_t)(a2), (a1)->sa_len) == 0)
416                         if ((rt->rt_flags & RTF_GATEWAY && gate != NULL) ||
417                             ifpaddr != NULL ||
418                             (ifaaddr != NULL &&
419                             !equal(ifaaddr, rt->rt_ifa->ifa_addr))) {
420                                 if ((error = rt_getifa(&info)) != 0)
421                                         senderr(error);
422                         }
423                         if (gate != NULL &&
424                             (error = rt_setgate(rt, rt_key(rt), gate)) != 0)
425                                 senderr(error);
426                         if ((ifa = info.rti_ifa) != NULL) {
427                                 struct ifaddr *oifa = rt->rt_ifa;
428                                 if (oifa != ifa) {
429                                     if (oifa && oifa->ifa_rtrequest)
430                                         oifa->ifa_rtrequest(RTM_DELETE, rt,
431                                             &info);
432                                     IFAFREE(rt->rt_ifa);
433                                     rt->rt_ifa = ifa;
434                                     ifa->ifa_refcnt++;
435                                     rt->rt_ifp = info.rti_ifp;
436                                 }
437                         }
438                         rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
439                                         &rt->rt_rmx);
440                         if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
441                                rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info);
442                         if (genmask)
443                                 rt->rt_genmask = genmask;
444                         /*
445                          * Fall into
446                          */
447                 case RTM_LOCK:
448                         rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
449                         rt->rt_rmx.rmx_locks |=
450                                 (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
451                         break;
452                 }
453                 break;
454
455         default:
456                 senderr(EOPNOTSUPP);
457         }
458
459 flush:
460         if (rtm) {
461                 if (error)
462                         rtm->rtm_errno = error;
463                 else
464                         rtm->rtm_flags |= RTF_DONE;
465         }
466         if (rt)
467                 rtfree(rt);
468     {
469         struct rawcb *rp = 0;
470         /*
471          * Check to see if we don't want our own messages.
472          */
473         if ((so->so_options & SO_USELOOPBACK) == 0) {
474                 if (route_cb.any_count <= 1) {
475                         if (rtm)
476                                 Free(rtm);
477                         m_freem(m);
478                         return (error);
479                 }
480                 /* There is another listener, so construct message */
481                 rp = sotorawcb(so);
482         }
483         if (rtm) {
484                 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
485                 if (m->m_pkthdr.len < rtm->rtm_msglen) {
486                         m_freem(m);
487                         m = NULL;
488                 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
489                         m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
490                 Free(rtm);
491         }
492         if (rp)
493                 rp->rcb_proto.sp_family = 0; /* Avoid us */
494         if (dst)
495                 route_proto.sp_protocol = dst->sa_family;
496         if (m)
497                 raw_input(m, &route_proto, &route_src, &route_dst);
498         if (rp)
499                 rp->rcb_proto.sp_family = PF_ROUTE;
500     }
501         return (error);
502 }
503
504 static void
505 rt_setmetrics(which, in, out)
506         u_long which;
507         struct rt_metrics *in, *out;
508 {
509 #define metric(f, e) if (which & (f)) out->e = in->e;
510         metric(RTV_RPIPE, rmx_recvpipe);
511         metric(RTV_SPIPE, rmx_sendpipe);
512         metric(RTV_SSTHRESH, rmx_ssthresh);
513         metric(RTV_RTT, rmx_rtt);
514         metric(RTV_RTTVAR, rmx_rttvar);
515         metric(RTV_HOPCOUNT, rmx_hopcount);
516         metric(RTV_MTU, rmx_mtu);
517         metric(RTV_EXPIRE, rmx_expire);
518 #undef metric
519 }
520
521 #define ROUNDUP(a) \
522         ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
523 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
524
525
526 /*
527  * Extract the addresses of the passed sockaddrs.
528  * Do a little sanity checking so as to avoid bad memory references.
529  * This data is derived straight from userland.
530  */
531 static int
532 rt_xaddrs(cp, cplim, rtinfo)
533         caddr_t cp, cplim;
534         struct rt_addrinfo *rtinfo;
535 {
536         struct sockaddr *sa;
537         int i;
538
539         for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
540                 if ((rtinfo->rti_addrs & (1 << i)) == 0)
541                         continue;
542                 sa = (struct sockaddr *)cp;
543                 /*
544                  * It won't fit.
545                  */
546                 if ( (cp + sa->sa_len) > cplim ) {
547                         return (EINVAL);
548                 }
549
550                 /*
551                  * there are no more.. quit now
552                  * If there are more bits, they are in error.
553                  * I've seen this. route(1) can evidently generate these. 
554                  * This causes kernel to core dump.
555                  * for compatibility, If we see this, point to a safe address.
556                  */
557                 if (sa->sa_len == 0) {
558                         rtinfo->rti_info[i] = &sa_zero;
559                         return (0); /* should be EINVAL but for compat */
560                 }
561
562                 /* accept it */
563                 rtinfo->rti_info[i] = sa;
564                 ADVANCE(cp, sa);
565         }
566         return (0);
567 }
568
569 static struct mbuf *
570 rt_msg1(type, rtinfo)
571         int type;
572         struct rt_addrinfo *rtinfo;
573 {
574         struct rt_msghdr *rtm;
575         struct mbuf *m;
576         int i;
577         struct sockaddr *sa;
578         int len, dlen;
579
580         switch (type) {
581
582         case RTM_DELADDR:
583         case RTM_NEWADDR:
584                 len = sizeof(struct ifa_msghdr);
585                 break;
586
587         case RTM_DELMADDR:
588         case RTM_NEWMADDR:
589                 len = sizeof(struct ifma_msghdr);
590                 break;
591
592         case RTM_IFINFO:
593                 len = sizeof(struct if_msghdr);
594                 break;
595
596         case RTM_IFANNOUNCE:
597                 len = sizeof(struct if_announcemsghdr);
598                 break;
599
600         default:
601                 len = sizeof(struct rt_msghdr);
602         }
603         if (len > MCLBYTES)
604                 panic("rt_msg1");
605         m = m_gethdr(M_DONTWAIT, MT_DATA);
606         if (m && len > MHLEN) {
607                 MCLGET(m, M_DONTWAIT);
608                 if ((m->m_flags & M_EXT) == 0) {
609                         m_free(m);
610                         m = NULL;
611                 }
612         }
613         if (m == 0)
614                 return (m);
615         m->m_pkthdr.len = m->m_len = len;
616         m->m_pkthdr.rcvif = 0;
617         rtm = mtod(m, struct rt_msghdr *);
618         bzero((caddr_t)rtm, len);
619         for (i = 0; i < RTAX_MAX; i++) {
620                 if ((sa = rtinfo->rti_info[i]) == NULL)
621                         continue;
622                 rtinfo->rti_addrs |= (1 << i);
623                 dlen = ROUNDUP(sa->sa_len);
624                 m_copyback(m, len, dlen, (caddr_t)sa);
625                 len += dlen;
626         }
627         if (m->m_pkthdr.len != len) {
628                 m_freem(m);
629                 return (NULL);
630         }
631         rtm->rtm_msglen = len;
632         rtm->rtm_version = RTM_VERSION;
633         rtm->rtm_type = type;
634         return (m);
635 }
636
637 static int
638 rt_msg2(type, rtinfo, cp, w)
639         int type;
640         struct rt_addrinfo *rtinfo;
641         caddr_t cp;
642         struct walkarg *w;
643 {
644         int i;
645         int len, dlen, second_time = 0;
646         caddr_t cp0;
647
648         rtinfo->rti_addrs = 0;
649 again:
650         switch (type) {
651
652         case RTM_DELADDR:
653         case RTM_NEWADDR:
654                 len = sizeof(struct ifa_msghdr);
655                 break;
656
657         case RTM_IFINFO:
658                 len = sizeof(struct if_msghdr);
659                 break;
660
661         default:
662                 len = sizeof(struct rt_msghdr);
663         }
664         cp0 = cp;
665         if (cp0)
666                 cp += len;
667         for (i = 0; i < RTAX_MAX; i++) {
668                 struct sockaddr *sa;
669
670                 if ((sa = rtinfo->rti_info[i]) == 0)
671                         continue;
672                 rtinfo->rti_addrs |= (1 << i);
673                 dlen = ROUNDUP(sa->sa_len);
674                 if (cp) {
675                         bcopy((caddr_t)sa, cp, (unsigned)dlen);
676                         cp += dlen;
677                 }
678                 len += dlen;
679         }
680         len = ALIGN(len);
681         if (cp == 0 && w != NULL && !second_time) {
682                 struct walkarg *rw = w;
683
684                 if (rw->w_req) {
685                         if (rw->w_tmemsize < len) {
686                                 if (rw->w_tmem)
687                                         free(rw->w_tmem, M_RTABLE);
688                                 rw->w_tmem = (caddr_t)
689                                         malloc(len, M_RTABLE, M_NOWAIT);
690                                 if (rw->w_tmem)
691                                         rw->w_tmemsize = len;
692                         }
693                         if (rw->w_tmem) {
694                                 cp = rw->w_tmem;
695                                 second_time = 1;
696                                 goto again;
697                         }
698                 }
699         }
700         if (cp) {
701                 struct rt_msghdr *rtm = (struct rt_msghdr *)cp0;
702
703                 rtm->rtm_version = RTM_VERSION;
704                 rtm->rtm_type = type;
705                 rtm->rtm_msglen = len;
706         }
707         return (len);
708 }
709
710 /*
711  * This routine is called to generate a message from the routing
712  * socket indicating that a redirect has occured, a routing lookup
713  * has failed, or that a protocol has detected timeouts to a particular
714  * destination.
715  */
716 void
717 rt_missmsg(type, rtinfo, flags, error)
718         int type, flags, error;
719         struct rt_addrinfo *rtinfo;
720 {
721         struct rt_msghdr *rtm;
722         struct mbuf *m;
723         struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
724
725         if (route_cb.any_count == 0)
726                 return;
727         m = rt_msg1(type, rtinfo);
728         if (m == 0)
729                 return;
730         rtm = mtod(m, struct rt_msghdr *);
731         rtm->rtm_flags = RTF_DONE | flags;
732         rtm->rtm_errno = error;
733         rtm->rtm_addrs = rtinfo->rti_addrs;
734         route_proto.sp_protocol = sa ? sa->sa_family : 0;
735         raw_input(m, &route_proto, &route_src, &route_dst);
736 }
737
738 /*
739  * This routine is called to generate a message from the routing
740  * socket indicating that the status of a network interface has changed.
741  */
742 void
743 rt_ifmsg(ifp)
744         struct ifnet *ifp;
745 {
746         struct if_msghdr *ifm;
747         struct mbuf *m;
748         struct rt_addrinfo info;
749
750         if (route_cb.any_count == 0)
751                 return;
752         bzero((caddr_t)&info, sizeof(info));
753         m = rt_msg1(RTM_IFINFO, &info);
754         if (m == 0)
755                 return;
756         ifm = mtod(m, struct if_msghdr *);
757         ifm->ifm_index = ifp->if_index;
758         ifm->ifm_flags = (u_short)ifp->if_flags;
759         ifm->ifm_data = ifp->if_data;
760         ifm->ifm_addrs = 0;
761         route_proto.sp_protocol = 0;
762         raw_input(m, &route_proto, &route_src, &route_dst);
763 }
764
765 static void
766 rt_ifamsg(int cmd, struct ifaddr *ifa)
767 {
768         struct ifa_msghdr *ifam;
769         struct rt_addrinfo info;
770         struct mbuf *m;
771         struct sockaddr *sa;
772         struct ifnet *ifp = ifa->ifa_ifp;
773
774         bzero(&info, sizeof(info));
775         ifaaddr = sa = ifa->ifa_addr;
776         ifpaddr = TAILQ_FIRST(&ifp->if_addrhead)->ifa_addr;
777         netmask = ifa->ifa_netmask;
778         brdaddr = ifa->ifa_dstaddr;
779         if ((m = rt_msg1(cmd, &info)) == NULL)
780                 return;
781         ifam = mtod(m, struct ifa_msghdr *);
782         ifam->ifam_index = ifp->if_index;
783         ifam->ifam_metric = ifa->ifa_metric;
784         ifam->ifam_flags = ifa->ifa_flags;
785         ifam->ifam_addrs = info.rti_addrs;
786
787         route_proto.sp_protocol = sa ? sa->sa_family : 0;
788         raw_input(m, &route_proto, &route_src, &route_dst);
789 }
790
791 static void
792 rt_rtmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt)
793 {
794         struct rt_msghdr *rtm;
795         struct rt_addrinfo info;
796         struct mbuf *m;
797         struct sockaddr *sa;
798         struct ifnet *ifp = ifa->ifa_ifp;
799
800         if (rt == NULL)
801                 return;
802         bzero(&info, sizeof(info));
803         netmask = rt_mask(rt);
804         dst = sa = rt_key(rt);
805         gate = rt->rt_gateway;
806         if ((m = rt_msg1(cmd, &info)) == NULL)
807                 return;
808         rtm = mtod(m, struct rt_msghdr *);
809         rtm->rtm_index = ifp->if_index;
810         rtm->rtm_flags |= rt->rt_flags;
811         rtm->rtm_errno = error;
812         rtm->rtm_addrs = info.rti_addrs;
813
814         route_proto.sp_protocol = sa ? sa->sa_family : 0;
815         raw_input(m, &route_proto, &route_src, &route_dst);
816 }
817
818 /*
819  * This is called to generate messages from the routing socket
820  * indicating a network interface has had addresses associated with it.
821  * if we ever reverse the logic and replace messages TO the routing
822  * socket indicate a request to configure interfaces, then it will
823  * be unnecessary as the routing socket will automatically generate
824  * copies of it.
825  */
826 void
827 rt_newaddrmsg(cmd, ifa, error, rt)
828         int cmd, error;
829         struct ifaddr *ifa;
830         struct rtentry *rt;
831 {
832         if (route_cb.any_count == 0)
833                 return;
834
835         if (cmd == RTM_ADD) {
836                 rt_ifamsg(RTM_NEWADDR, ifa);
837                 rt_rtmsg(RTM_ADD, ifa, error, rt);
838         } else {
839                 KASSERT((cmd == RTM_DELETE), ("unknown cmd %d", cmd));
840                 rt_rtmsg(RTM_DELETE, ifa, error, rt);
841                 rt_ifamsg(RTM_DELADDR, ifa);
842         }
843 }
844
845 /*
846  * This is the analogue to the rt_newaddrmsg which performs the same
847  * function but for multicast group memberhips.  This is easier since
848  * there is no route state to worry about.
849  */
850 void
851 rt_newmaddrmsg(cmd, ifma)
852         int cmd;
853         struct ifmultiaddr *ifma;
854 {
855         struct rt_addrinfo info;
856         struct mbuf *m = 0;
857         struct ifnet *ifp = ifma->ifma_ifp;
858         struct ifma_msghdr *ifmam;
859
860         if (route_cb.any_count == 0)
861                 return;
862
863         bzero((caddr_t)&info, sizeof(info));
864         ifaaddr = ifma->ifma_addr;
865         if (ifp && TAILQ_FIRST(&ifp->if_addrhead))
866                 ifpaddr = TAILQ_FIRST(&ifp->if_addrhead)->ifa_addr;
867         else
868                 ifpaddr = NULL;
869         /*
870          * If a link-layer address is present, present it as a ``gateway''
871          * (similarly to how ARP entries, e.g., are presented).
872          */
873         gate = ifma->ifma_lladdr;
874         if ((m = rt_msg1(cmd, &info)) == NULL)
875                 return;
876         ifmam = mtod(m, struct ifma_msghdr *);
877         ifmam->ifmam_index = ifp->if_index;
878         ifmam->ifmam_addrs = info.rti_addrs;
879         route_proto.sp_protocol = ifma->ifma_addr->sa_family;
880         raw_input(m, &route_proto, &route_src, &route_dst);
881 }
882
883 /*
884  * This is called to generate routing socket messages indicating
885  * network interface arrival and departure.
886  */
887 void
888 rt_ifannouncemsg(ifp, what)
889         struct ifnet *ifp;
890         int what;
891 {
892         struct if_announcemsghdr *ifan;
893         struct mbuf *m;
894         struct rt_addrinfo info;
895
896         if (route_cb.any_count == 0)
897                 return;
898         bzero((caddr_t)&info, sizeof(info));
899         m = rt_msg1(RTM_IFANNOUNCE, &info);
900         if (m == NULL)
901                 return;
902         ifan = mtod(m, struct if_announcemsghdr *);
903         ifan->ifan_index = ifp->if_index;
904         strlcpy(ifan->ifan_name, ifp->if_xname, sizeof(ifan->ifan_name));
905         ifan->ifan_what = what;
906         route_proto.sp_protocol = 0;
907         raw_input(m, &route_proto, &route_src, &route_dst);
908  }
909
910 /*
911  * This is used in dumping the kernel table via sysctl().
912  */
913 int
914 sysctl_dumpentry(rn, vw)
915         struct radix_node *rn;
916         void *vw;
917 {
918         struct walkarg *w = vw;
919         struct rtentry *rt = (struct rtentry *)rn;
920         int error = 0, size;
921         struct rt_addrinfo info;
922
923         if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
924                 return 0;
925         bzero((caddr_t)&info, sizeof(info));
926         dst = rt_key(rt);
927         gate = rt->rt_gateway;
928         netmask = rt_mask(rt);
929         genmask = rt->rt_genmask;
930         if (rt->rt_ifp) {
931                 ifpaddr = TAILQ_FIRST(&rt->rt_ifp->if_addrhead)->ifa_addr;
932                 ifaaddr = rt->rt_ifa->ifa_addr;
933                 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
934                         brdaddr = rt->rt_ifa->ifa_dstaddr;
935         }
936         size = rt_msg2(RTM_GET, &info, 0, w);
937         if (w->w_req && w->w_tmem) {
938                 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
939
940                 rtm->rtm_flags = rt->rt_flags;
941                 rtm->rtm_use = rt->rt_use;
942                 rtm->rtm_rmx = rt->rt_rmx;
943                 rtm->rtm_index = rt->rt_ifp->if_index;
944                 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
945                 rtm->rtm_addrs = info.rti_addrs;
946                 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
947                 return (error);
948         }
949         return (error);
950 }
951
952 int
953 sysctl_iflist(af, w)
954         int     af;
955         struct  walkarg *w;
956 {
957         struct ifnet *ifp;
958         struct ifaddr *ifa;
959         struct  rt_addrinfo info;
960         int     len, error = 0;
961
962         bzero((caddr_t)&info, sizeof(info));
963         TAILQ_FOREACH(ifp, &ifnet, if_link) {
964                 if (w->w_arg && w->w_arg != ifp->if_index)
965                         continue;
966                 ifa = TAILQ_FIRST(&ifp->if_addrhead);
967                 ifpaddr = ifa->ifa_addr;
968                 len = rt_msg2(RTM_IFINFO, &info, (caddr_t)0, w);
969                 ifpaddr = 0;
970                 if (w->w_req && w->w_tmem) {
971                         struct if_msghdr *ifm;
972
973                         ifm = (struct if_msghdr *)w->w_tmem;
974                         ifm->ifm_index = ifp->if_index;
975                         ifm->ifm_flags = (u_short)ifp->if_flags;
976                         ifm->ifm_data = ifp->if_data;
977                         ifm->ifm_addrs = info.rti_addrs;
978                         error = SYSCTL_OUT(w->w_req,(caddr_t)ifm, len);
979                         if (error)
980                                 return (error);
981                 }
982                 while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != 0) {
983                         if (af && af != ifa->ifa_addr->sa_family)
984                                 continue;
985                         if (curproc->p_ucred->cr_prison && prison_if(curthread, ifa->ifa_addr))
986                                 continue;
987                         ifaaddr = ifa->ifa_addr;
988                         netmask = ifa->ifa_netmask;
989                         brdaddr = ifa->ifa_dstaddr;
990                         len = rt_msg2(RTM_NEWADDR, &info, 0, w);
991                         if (w->w_req && w->w_tmem) {
992                                 struct ifa_msghdr *ifam;
993
994                                 ifam = (struct ifa_msghdr *)w->w_tmem;
995                                 ifam->ifam_index = ifa->ifa_ifp->if_index;
996                                 ifam->ifam_flags = ifa->ifa_flags;
997                                 ifam->ifam_metric = ifa->ifa_metric;
998                                 ifam->ifam_addrs = info.rti_addrs;
999                                 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
1000                                 if (error)
1001                                         return (error);
1002                         }
1003                 }
1004                 ifaaddr = netmask = brdaddr = 0;
1005         }
1006         return (0);
1007 }
1008
1009 static int
1010 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
1011 {
1012         int     *name = (int *)arg1;
1013         u_int   namelen = arg2;
1014         struct radix_node_head *rnh;
1015         int     i, s, error = EINVAL;
1016         u_char  af;
1017         struct  walkarg w;
1018
1019         name ++;
1020         namelen--;
1021         if (req->newptr)
1022                 return (EPERM);
1023         if (namelen != 3)
1024                 return (EINVAL);
1025         af = name[0];
1026         Bzero(&w, sizeof(w));
1027         w.w_op = name[1];
1028         w.w_arg = name[2];
1029         w.w_req = req;
1030
1031         s = splnet();
1032         switch (w.w_op) {
1033
1034         case NET_RT_DUMP:
1035         case NET_RT_FLAGS:
1036                 for (i = 1; i <= AF_MAX; i++)
1037                         if ((rnh = rt_tables[i]) && (af == 0 || af == i) &&
1038                             (error = rnh->rnh_walktree(rnh,
1039                                                         sysctl_dumpentry, &w)))
1040                                 break;
1041                 break;
1042
1043         case NET_RT_IFLIST:
1044                 error = sysctl_iflist(af, &w);
1045         }
1046         splx(s);
1047         if (w.w_tmem)
1048                 free(w.w_tmem, M_RTABLE);
1049         return (error);
1050 }
1051
1052 SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
1053
1054 /*
1055  * Definitions of protocols supported in the ROUTE domain.
1056  */
1057
1058 extern struct domain routedomain;               /* or at least forward */
1059
1060 static struct protosw routesw[] = {
1061 { SOCK_RAW,     &routedomain,   0,              PR_ATOMIC|PR_ADDR,
1062   0,            route_output,   raw_ctlinput,   0,
1063   0,
1064   raw_init,     0,              0,              0,
1065   &route_usrreqs
1066 }
1067 };
1068
1069 static struct domain routedomain =
1070     { PF_ROUTE, "route", 0, 0, 0,
1071       routesw, &routesw[sizeof(routesw)/sizeof(routesw[0])] };
1072
1073 DOMAIN_SET(route);