Merge branch 'vendor/FILE'
[dragonfly.git] / sys / net / rtsock.c
1 /*
2  * Copyright (c) 2004, 2005 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Jeffrey M. Hsu.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of The DragonFly Project nor the names of its
16  *    contributors may be used to endorse or promote products derived
17  *    from this software without specific, prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
23  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32
33 /*
34  * Copyright (c) 1988, 1991, 1993
35  *      The Regents of the University of California.  All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  * 3. Neither the name of the University nor the names of its contributors
46  *    may be used to endorse or promote products derived from this software
47  *    without specific prior written permission.
48  *
49  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59  * SUCH DAMAGE.
60  *
61  *      @(#)rtsock.c    8.7 (Berkeley) 10/12/95
62  * $FreeBSD: src/sys/net/rtsock.c,v 1.44.2.11 2002/12/04 14:05:41 ru Exp $
63  */
64
65 #include <sys/param.h>
66 #include <sys/systm.h>
67 #include <sys/kernel.h>
68 #include <sys/sysctl.h>
69 #include <sys/proc.h>
70 #include <sys/priv.h>
71 #include <sys/malloc.h>
72 #include <sys/mbuf.h>
73 #include <sys/protosw.h>
74 #include <sys/socket.h>
75 #include <sys/socketvar.h>
76 #include <sys/domain.h>
77 #include <sys/jail.h>
78
79 #include <sys/thread2.h>
80 #include <sys/socketvar2.h>
81
82 #include <net/if.h>
83 #include <net/if_var.h>
84 #include <net/route.h>
85 #include <net/raw_cb.h>
86 #include <net/netmsg2.h>
87 #include <net/netisr2.h>
88
89 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
90
91 static struct route_cb {
92         int     ip_count;
93         int     ip6_count;
94         int     ns_count;
95         int     any_count;
96 } route_cb;
97
98 static const struct sockaddr route_src = { 2, PF_ROUTE, };
99
100 struct walkarg {
101         int     w_tmemsize;
102         int     w_op, w_arg;
103         void    *w_tmem;
104         struct sysctl_req *w_req;
105 };
106
107 #ifndef RTTABLE_DUMP_MSGCNT_MAX
108 /* Should be large enough for dupkeys */
109 #define RTTABLE_DUMP_MSGCNT_MAX         64
110 #endif
111
112 struct rttable_walkarg {
113         int     w_op;
114         int     w_arg;
115         int     w_bufsz;
116         void    *w_buf;
117
118         int     w_buflen;
119
120         const char *w_key;
121         const char *w_mask;
122
123         struct sockaddr_storage w_key0;
124         struct sockaddr_storage w_mask0;
125 };
126
127 struct netmsg_rttable_walk {
128         struct netmsg_base      base;
129         int                     af;
130         struct rttable_walkarg  *w;
131 };
132
133 struct routecb {
134         struct rawcb    rocb_rcb;
135         unsigned int    rocb_msgfilter;
136         char            *rocb_missfilter;
137         size_t          rocb_missfilterlen;
138 };
139 #define sotoroutecb(so) ((struct routecb *)(so)->so_pcb)
140
141 static struct mbuf *
142                 rt_msg_mbuf (int, struct rt_addrinfo *);
143 static void     rt_msg_buffer (int, struct rt_addrinfo *, void *buf, int len);
144 static int      rt_msgsize(int type, const struct rt_addrinfo *rtinfo);
145 static int      rt_xaddrs (char *, char *, struct rt_addrinfo *);
146 static int      sysctl_rttable(int af, struct sysctl_req *req, int op, int arg);
147 static int      if_addrflags(const struct ifaddr *ifa);
148 static int      sysctl_iflist (int af, struct walkarg *w);
149 static int      route_output(struct mbuf *, struct socket *, ...);
150 static void     rt_setmetrics (u_long, struct rt_metrics *,
151                                struct rt_metrics *);
152
153 /*
154  * It really doesn't make any sense at all for this code to share much
155  * with raw_usrreq.c, since its functionality is so restricted.  XXX
156  */
157 static void
158 rts_abort(netmsg_t msg)
159 {
160         crit_enter();
161         raw_usrreqs.pru_abort(msg);
162         /* msg invalid now */
163         crit_exit();
164 }
165
166 static int
167 rts_filter(struct mbuf *m, const struct sockproto *proto,
168         const struct rawcb *rp)
169 {
170         const struct routecb *rop = (const struct routecb *)rp;
171         const struct rt_msghdr *rtm;
172
173         KKASSERT(m != NULL);
174         KKASSERT(proto != NULL);
175         KKASSERT(rp != NULL);
176
177         /* Wrong family for this socket. */
178         if (proto->sp_family != PF_ROUTE)
179                 return ENOPROTOOPT;
180
181         /* If no filter set, just return. */
182         if (rop->rocb_msgfilter == 0 && rop->rocb_missfilterlen == 0)
183                 return 0;
184
185         /* Ensure we can access rtm_type */
186         if (m->m_len <
187             offsetof(struct rt_msghdr, rtm_type) + sizeof(rtm->rtm_type))
188                 return EINVAL;
189
190         rtm = mtod(m, const struct rt_msghdr *);
191         /* If the rtm type is filtered out, return a positive. */
192         if (rop->rocb_msgfilter != 0 &&
193             !(rop->rocb_msgfilter & ROUTE_FILTER(rtm->rtm_type)))
194                 return EEXIST;
195
196         if (rop->rocb_missfilterlen != 0 && rtm->rtm_type == RTM_MISS) {
197                 CTASSERT(RTAX_DST == 0);
198                 struct sockaddr *sa;
199                 struct sockaddr_storage ss;
200                 struct sockaddr *dst = (struct sockaddr *)&ss;
201                 char *cp = rop->rocb_missfilter;
202                 char *ep = cp + rop->rocb_missfilterlen;
203
204                 /* Ensure we can access sa_len */
205                 if (m->m_pkthdr.len < sizeof(*rtm) +
206                     offsetof(struct sockaddr, sa_len) + sizeof(dst->sa_len))
207                         return EINVAL;
208                 m_copydata(m, sizeof(*rtm) + offsetof(struct sockaddr, sa_len),
209                     sizeof(ss.ss_len), (caddr_t)&ss);
210                 if (m->m_pkthdr.len < sizeof(*rtm) + ss.ss_len)
211                         return EINVAL;
212                 /* Copy out the destination sockaddr */
213                 m_copydata(m, sizeof(*rtm), ss.ss_len, (caddr_t)&ss);
214
215                 /* Find a matching sockaddr in the filter */
216                 while (cp < ep) {
217                         sa = (struct sockaddr *)cp;
218                         if (sa->sa_len == dst->sa_len &&
219                             memcmp(sa, dst, sa->sa_len) == 0)
220                                 break;
221                         cp += RT_ROUNDUP(sa->sa_len);
222                 }
223                 if (cp == ep)
224                         return EEXIST;
225         }
226
227         /* Passed the filter. */
228         return 0;
229 }
230
231
232 /* pru_accept is EOPNOTSUPP */
233
234 static void
235 rts_attach(netmsg_t msg)
236 {
237         struct socket *so = msg->base.nm_so;
238         struct pru_attach_info *ai = msg->attach.nm_ai;
239         struct rawcb *rp;
240         struct routecb *rop;
241         int proto = msg->attach.nm_proto;
242         int error;
243
244         crit_enter();
245         if (sotorawcb(so) != NULL) {
246                 error = EISCONN;
247                 goto done;
248         }
249
250         rop = kmalloc(sizeof *rop, M_PCB, M_WAITOK | M_ZERO);
251         rp = &rop->rocb_rcb;
252
253         /*
254          * The critical section is necessary to block protocols from sending
255          * error notifications (like RTM_REDIRECT or RTM_LOSING) while
256          * this PCB is extant but incompletely initialized.
257          * Probably we should try to do more of this work beforehand and
258          * eliminate the critical section.
259          */
260         so->so_pcb = rp;
261         soreference(so);        /* so_pcb assignment */
262         error = raw_attach(so, proto, ai->sb_rlimit);
263         rp = sotorawcb(so);
264         if (error) {
265                 kfree(rop, M_PCB);
266                 goto done;
267         }
268         switch(rp->rcb_proto.sp_protocol) {
269         case AF_INET:
270                 route_cb.ip_count++;
271                 break;
272         case AF_INET6:
273                 route_cb.ip6_count++;
274                 break;
275         }
276         rp->rcb_faddr = &route_src;
277         rp->rcb_filter = rts_filter;
278         route_cb.any_count++;
279         soisconnected(so);
280         so->so_options |= SO_USELOOPBACK;
281         error = 0;
282 done:
283         crit_exit();
284         lwkt_replymsg(&msg->lmsg, error);
285 }
286
287 static void
288 rts_bind(netmsg_t msg)
289 {
290         crit_enter();
291         raw_usrreqs.pru_bind(msg); /* xxx just EINVAL */
292         /* msg invalid now */
293         crit_exit();
294 }
295
296 static void
297 rts_connect(netmsg_t msg)
298 {
299         crit_enter();
300         raw_usrreqs.pru_connect(msg); /* XXX just EINVAL */
301         /* msg invalid now */
302         crit_exit();
303 }
304
305 /* pru_connect2 is EOPNOTSUPP */
306 /* pru_control is EOPNOTSUPP */
307
308 static void
309 rts_detach(netmsg_t msg)
310 {
311         struct socket *so = msg->base.nm_so;
312         struct rawcb *rp = sotorawcb(so);
313         struct routecb *rop = (struct routecb *)rp;
314
315         crit_enter();
316         if (rop->rocb_missfilterlen != 0)
317                 kfree(rop->rocb_missfilter, M_PCB);
318         if (rp != NULL) {
319                 switch(rp->rcb_proto.sp_protocol) {
320                 case AF_INET:
321                         route_cb.ip_count--;
322                         break;
323                 case AF_INET6:
324                         route_cb.ip6_count--;
325                         break;
326                 }
327                 route_cb.any_count--;
328         }
329         raw_usrreqs.pru_detach(msg);
330         /* msg invalid now */
331         crit_exit();
332 }
333
334 static void
335 rts_disconnect(netmsg_t msg)
336 {
337         crit_enter();
338         raw_usrreqs.pru_disconnect(msg);
339         /* msg invalid now */
340         crit_exit();
341 }
342
343 /* pru_listen is EOPNOTSUPP */
344
345 static void
346 rts_peeraddr(netmsg_t msg)
347 {
348         crit_enter();
349         raw_usrreqs.pru_peeraddr(msg);
350         /* msg invalid now */
351         crit_exit();
352 }
353
354 /* pru_rcvd is EOPNOTSUPP */
355 /* pru_rcvoob is EOPNOTSUPP */
356
357 static void
358 rts_send(netmsg_t msg)
359 {
360         crit_enter();
361         raw_usrreqs.pru_send(msg);
362         /* msg invalid now */
363         crit_exit();
364 }
365
366 /* pru_sense is null */
367
368 static void
369 rts_shutdown(netmsg_t msg)
370 {
371         crit_enter();
372         raw_usrreqs.pru_shutdown(msg);
373         /* msg invalid now */
374         crit_exit();
375 }
376
377 static void
378 rts_sockaddr(netmsg_t msg)
379 {
380         crit_enter();
381         raw_usrreqs.pru_sockaddr(msg);
382         /* msg invalid now */
383         crit_exit();
384 }
385
386 static struct pr_usrreqs route_usrreqs = {
387         .pru_abort = rts_abort,
388         .pru_accept = pr_generic_notsupp,
389         .pru_attach = rts_attach,
390         .pru_bind = rts_bind,
391         .pru_connect = rts_connect,
392         .pru_connect2 = pr_generic_notsupp,
393         .pru_control = pr_generic_notsupp,
394         .pru_detach = rts_detach,
395         .pru_disconnect = rts_disconnect,
396         .pru_listen = pr_generic_notsupp,
397         .pru_peeraddr = rts_peeraddr,
398         .pru_rcvd = pr_generic_notsupp,
399         .pru_rcvoob = pr_generic_notsupp,
400         .pru_send = rts_send,
401         .pru_sense = pru_sense_null,
402         .pru_shutdown = rts_shutdown,
403         .pru_sockaddr = rts_sockaddr,
404         .pru_sosend = sosend,
405         .pru_soreceive = soreceive
406 };
407
408 static __inline sa_family_t
409 familyof(struct sockaddr *sa)
410 {
411         return (sa != NULL ? sa->sa_family : 0);
412 }
413
414 /*
415  * Routing socket input function.  The packet must be serialized onto cpu 0.
416  * We use the cpu0_soport() netisr processing loop to handle it.
417  *
418  * This looks messy but it means that anyone, including interrupt code,
419  * can send a message to the routing socket.
420  */
421 static void
422 rts_input_handler(netmsg_t msg)
423 {
424         static const struct sockaddr route_dst = { 2, PF_ROUTE, };
425         struct sockproto route_proto;
426         struct netmsg_packet *pmsg = &msg->packet;
427         struct mbuf *m;
428         sa_family_t family;
429         struct rawcb *skip;
430
431         family = pmsg->base.lmsg.u.ms_result;
432         route_proto.sp_family = PF_ROUTE;
433         route_proto.sp_protocol = family;
434
435         m = pmsg->nm_packet;
436         M_ASSERTPKTHDR(m);
437
438         skip = m->m_pkthdr.header;
439         m->m_pkthdr.header = NULL;
440
441         raw_input(m, &route_proto, &route_src, &route_dst, skip);
442 }
443
444 static void
445 rts_input_skip(struct mbuf *m, sa_family_t family, struct rawcb *skip)
446 {
447         struct netmsg_packet *pmsg;
448         lwkt_port_t port;
449
450         M_ASSERTPKTHDR(m);
451
452         port = netisr_cpuport(0);       /* XXX same as for routing socket */
453         pmsg = &m->m_hdr.mh_netmsg;
454         netmsg_init(&pmsg->base, NULL, &netisr_apanic_rport,
455                     0, rts_input_handler);
456         pmsg->nm_packet = m;
457         pmsg->base.lmsg.u.ms_result = family;
458         m->m_pkthdr.header = skip; /* XXX steal field in pkthdr */
459         lwkt_sendmsg(port, &pmsg->base.lmsg);
460 }
461
462 static __inline void
463 rts_input(struct mbuf *m, sa_family_t family)
464 {
465         rts_input_skip(m, family, NULL);
466 }
467
468 static void
469 route_ctloutput(netmsg_t msg)
470 {
471         struct socket *so = msg->ctloutput.base.nm_so;
472         struct sockopt *sopt = msg->ctloutput.nm_sopt;
473         struct routecb *rop = sotoroutecb(so);
474         int error;
475         unsigned int msgfilter;
476         unsigned char *cp, *ep;
477         size_t len;
478         struct sockaddr *sa;
479
480         if (sopt->sopt_level != AF_ROUTE) {
481                 error = EINVAL;
482                 goto out;
483         }
484
485         error = 0;
486
487         switch (sopt->sopt_dir) {
488         case SOPT_SET:
489                 switch (sopt->sopt_name) {
490                 case ROUTE_MSGFILTER:
491                         error = soopt_to_kbuf(sopt, &msgfilter,
492                             sizeof(msgfilter), sizeof(msgfilter));
493                         if (error == 0)
494                                 rop->rocb_msgfilter = msgfilter;
495                         break;
496                 case RO_MISSFILTER:
497                         /* Validate the data */
498                         len = 0;
499                         cp = sopt->sopt_val;
500                         ep = cp + sopt->sopt_valsize;
501                         while (cp < ep) {
502                                 if (ep - cp <
503                                     offsetof(struct sockaddr, sa_len) +
504                                     sizeof(sa->sa_len))
505                                         break;
506                                 if (++len > RO_FILTSA_MAX) {
507                                         error = ENOBUFS;
508                                         break;
509                                 }
510                                 sa = (struct sockaddr *)cp;
511                                 cp += RT_ROUNDUP(sa->sa_len);
512                         }
513                         if (cp != ep) {
514                                 if (error == 0)
515                                         error = EINVAL;
516                                 break;
517                         }
518                         if (rop->rocb_missfilterlen != 0)
519                                 kfree(rop->rocb_missfilter, M_PCB);
520                         if (sopt->sopt_valsize != 0) {
521                                 rop->rocb_missfilter =
522                                     kmalloc(sopt->sopt_valsize,
523                                             M_PCB, M_WAITOK | M_NULLOK);
524                                 if (rop->rocb_missfilter == NULL) {
525                                         rop->rocb_missfilterlen = 0;
526                                         error = ENOBUFS;
527                                         break;
528                                 }
529                         } else
530                                 rop->rocb_missfilter = NULL;
531                         rop->rocb_missfilterlen = sopt->sopt_valsize;
532                         if (rop->rocb_missfilterlen != 0)
533                                 memcpy(rop->rocb_missfilter, sopt->sopt_val,
534                                     rop->rocb_missfilterlen);
535                         break;
536                 default:
537                         error = ENOPROTOOPT;
538                         break;
539                 }
540                 break;
541         case SOPT_GET:
542                 switch (sopt->sopt_name) {
543                 case ROUTE_MSGFILTER:
544                         msgfilter = rop->rocb_msgfilter;
545                         soopt_from_kbuf(sopt, &msgfilter, sizeof(msgfilter));
546                         break;
547                 case RO_MISSFILTER:
548                         soopt_from_kbuf(sopt, rop->rocb_missfilter,
549                             rop->rocb_missfilterlen);
550                         break;
551                 default:
552                         error = ENOPROTOOPT;
553                         break;
554                 }
555         }
556 out:
557         lwkt_replymsg(&msg->ctloutput.base.lmsg, error);
558 }
559
560
561
562 static void *
563 reallocbuf_nofree(void *ptr, size_t len, size_t olen)
564 {
565         void *newptr;
566
567         newptr = kmalloc(len, M_RTABLE, M_INTWAIT | M_NULLOK);
568         if (newptr == NULL)
569                 return NULL;
570         bcopy(ptr, newptr, olen);
571         if (olen < len)
572                 bzero((char *)newptr + olen, len - olen);
573
574         return (newptr);
575 }
576
577 /*
578  * Internal helper routine for route_output().
579  */
580 static int
581 _fillrtmsg(struct rt_msghdr **prtm, struct rtentry *rt,
582            struct rt_addrinfo *rtinfo)
583 {
584         int msglen;
585         struct rt_msghdr *rtm = *prtm;
586
587         /* Fill in rt_addrinfo for call to rt_msg_buffer(). */
588         rtinfo->rti_dst = rt_key(rt);
589         rtinfo->rti_gateway = rt->rt_gateway;
590         rtinfo->rti_netmask = rt_mask(rt);              /* might be NULL */
591         rtinfo->rti_genmask = rt->rt_genmask;           /* might be NULL */
592         if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
593                 if (rt->rt_ifp != NULL) {
594                         rtinfo->rti_ifpaddr =
595                             TAILQ_FIRST(&rt->rt_ifp->if_addrheads[mycpuid])
596                             ->ifa->ifa_addr;
597                         rtinfo->rti_ifaaddr = rt->rt_ifa->ifa_addr;
598                         if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
599                                 rtinfo->rti_bcastaddr = rt->rt_ifa->ifa_dstaddr;
600                         rtm->rtm_index = rt->rt_ifp->if_index;
601                 } else {
602                         rtinfo->rti_ifpaddr = NULL;
603                         rtinfo->rti_ifaaddr = NULL;
604                 }
605         } else if (rt->rt_ifp != NULL) {
606                 rtm->rtm_index = rt->rt_ifp->if_index;
607         }
608
609         msglen = rt_msgsize(rtm->rtm_type, rtinfo);
610         if (rtm->rtm_msglen < msglen) {
611                 /* NOTE: Caller will free the old rtm accordingly */
612                 rtm = reallocbuf_nofree(rtm, msglen, rtm->rtm_msglen);
613                 if (rtm == NULL)
614                         return (ENOBUFS);
615                 *prtm = rtm;
616         }
617         rt_msg_buffer(rtm->rtm_type, rtinfo, rtm, msglen);
618
619         rtm->rtm_flags = rt->rt_flags;
620         rtm->rtm_rmx = rt->rt_rmx;
621         rtm->rtm_addrs = rtinfo->rti_addrs;
622
623         return (0);
624 }
625
626 struct rtm_arg {
627         struct rt_msghdr        *bak_rtm;
628         struct rt_msghdr        *new_rtm;
629 };
630
631 static int
632 fillrtmsg(struct rtm_arg *arg, struct rtentry *rt,
633           struct rt_addrinfo *rtinfo)
634 {
635         struct rt_msghdr *rtm = arg->new_rtm;
636         int error;
637
638         error = _fillrtmsg(&rtm, rt, rtinfo);
639         if (!error) {
640                 if (arg->new_rtm != rtm) {
641                         /*
642                          * _fillrtmsg() just allocated a new rtm;
643                          * if the previously allocated rtm is not
644                          * the backing rtm, it should be freed.
645                          */
646                         if (arg->new_rtm != arg->bak_rtm)
647                                 kfree(arg->new_rtm, M_RTABLE);
648                         arg->new_rtm = rtm;
649                 }
650         }
651         return error;
652 }
653
654 static void route_output_add_callback(int, int, struct rt_addrinfo *,
655                                         struct rtentry *, void *);
656 static void route_output_delete_callback(int, int, struct rt_addrinfo *,
657                                         struct rtentry *, void *);
658 static int route_output_get_callback(int, struct rt_addrinfo *,
659                                      struct rtentry *, void *, int);
660 static int route_output_change_callback(int, struct rt_addrinfo *,
661                                         struct rtentry *, void *, int);
662 static int route_output_lock_callback(int, struct rt_addrinfo *,
663                                       struct rtentry *, void *, int);
664
665 /*ARGSUSED*/
666 static int
667 route_output(struct mbuf *m, struct socket *so, ...)
668 {
669         struct rtm_arg arg;
670         struct rt_msghdr *rtm = NULL;
671         struct rawcb *rp = NULL;
672         struct pr_output_info *oi;
673         struct rt_addrinfo rtinfo;
674         sa_family_t family;
675         int len, error = 0;
676         __va_list ap;
677
678         M_ASSERTPKTHDR(m);
679
680         __va_start(ap, so);
681         oi = __va_arg(ap, struct pr_output_info *);
682         __va_end(ap);
683
684         family = familyof(NULL);
685
686 #define gotoerr(e) { error = e; goto flush;}
687
688         if (m == NULL ||
689             (m->m_len < sizeof(long) &&
690              (m = m_pullup(m, sizeof(long))) == NULL))
691                 return (ENOBUFS);
692         len = m->m_pkthdr.len;
693         if (len < sizeof(struct rt_msghdr) ||
694             len != mtod(m, struct rt_msghdr *)->rtm_msglen)
695                 gotoerr(EINVAL);
696
697         rtm = kmalloc(len, M_RTABLE, M_INTWAIT | M_NULLOK);
698         if (rtm == NULL)
699                 gotoerr(ENOBUFS);
700
701         m_copydata(m, 0, len, (caddr_t)rtm);
702         if (rtm->rtm_version != RTM_VERSION)
703                 gotoerr(EPROTONOSUPPORT);
704
705         rtm->rtm_pid = oi->p_pid;
706         bzero(&rtinfo, sizeof(struct rt_addrinfo));
707         rtinfo.rti_addrs = rtm->rtm_addrs;
708         if (rt_xaddrs((char *)(rtm + 1), (char *)rtm + len, &rtinfo) != 0)
709                 gotoerr(EINVAL);
710
711         rtinfo.rti_flags = rtm->rtm_flags;
712         if (rtinfo.rti_dst == NULL || rtinfo.rti_dst->sa_family >= AF_MAX ||
713             (rtinfo.rti_gateway && rtinfo.rti_gateway->sa_family >= AF_MAX))
714                 gotoerr(EINVAL);
715
716         family = familyof(rtinfo.rti_dst);
717
718         /*
719          * Verify that the caller has the appropriate privilege; RTM_GET
720          * is the only operation the non-superuser is allowed.
721          */
722         if (rtm->rtm_type != RTM_GET &&
723             priv_check_cred(so->so_cred, PRIV_ROOT, 0) != 0)
724                 gotoerr(EPERM);
725
726         if (rtinfo.rti_genmask != NULL) {
727                 error = rtmask_add_global(rtinfo.rti_genmask,
728                     rtm->rtm_type != RTM_GET ?
729                     RTREQ_PRIO_HIGH : RTREQ_PRIO_NORM);
730                 if (error)
731                         goto flush;
732         }
733
734         switch (rtm->rtm_type) {
735         case RTM_ADD:
736                 if (rtinfo.rti_gateway == NULL) {
737                         error = EINVAL;
738                 } else {
739                         error = rtrequest1_global(RTM_ADD, &rtinfo,
740                             route_output_add_callback, rtm, RTREQ_PRIO_HIGH);
741                 }
742                 break;
743         case RTM_DELETE:
744                 /*
745                  * Backing rtm (bak_rtm) could _not_ be freed during
746                  * rtrequest1_global or rtsearch_global, even if the
747                  * callback reallocates the rtm due to its size changes,
748                  * since rtinfo points to the backing rtm's memory area.
749                  * After rtrequest1_global or rtsearch_global returns,
750                  * it is safe to free the backing rtm, since rtinfo will
751                  * not be used anymore.
752                  *
753                  * new_rtm will be used to save the new rtm allocated
754                  * by rtrequest1_global or rtsearch_global.
755                  */
756                 arg.bak_rtm = rtm;
757                 arg.new_rtm = rtm;
758                 error = rtrequest1_global(RTM_DELETE, &rtinfo,
759                     route_output_delete_callback, &arg, RTREQ_PRIO_HIGH);
760                 rtm = arg.new_rtm;
761                 if (rtm != arg.bak_rtm)
762                         kfree(arg.bak_rtm, M_RTABLE);
763                 break;
764         case RTM_GET:
765                 /* See the comment in RTM_DELETE */
766                 arg.bak_rtm = rtm;
767                 arg.new_rtm = rtm;
768                 error = rtsearch_global(RTM_GET, &rtinfo,
769                     route_output_get_callback, &arg, RTS_NOEXACTMATCH,
770                     RTREQ_PRIO_NORM);
771                 rtm = arg.new_rtm;
772                 if (rtm != arg.bak_rtm)
773                         kfree(arg.bak_rtm, M_RTABLE);
774                 break;
775         case RTM_CHANGE:
776                 error = rtsearch_global(RTM_CHANGE, &rtinfo,
777                     route_output_change_callback, rtm, RTS_EXACTMATCH,
778                     RTREQ_PRIO_HIGH);
779                 break;
780         case RTM_LOCK:
781                 error = rtsearch_global(RTM_LOCK, &rtinfo,
782                     route_output_lock_callback, rtm, RTS_EXACTMATCH,
783                     RTREQ_PRIO_HIGH);
784                 break;
785         default:
786                 error = EOPNOTSUPP;
787                 break;
788         }
789 flush:
790         if (rtm != NULL) {
791                 if (error != 0)
792                         rtm->rtm_errno = error;
793                 else
794                         rtm->rtm_flags |= RTF_DONE;
795         }
796
797         /*
798          * Check to see if we don't want our own messages.
799          */
800         if (!(so->so_options & SO_USELOOPBACK)) {
801                 if (route_cb.any_count <= 1) {
802                         if (rtm != NULL)
803                                 kfree(rtm, M_RTABLE);
804                         m_freem(m);
805                         return (error);
806                 }
807                 /* There is another listener, so construct message */
808                 rp = sotorawcb(so);
809         }
810         if (rtm != NULL) {
811                 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
812                 if (m->m_pkthdr.len < rtm->rtm_msglen) {
813                         m_freem(m);
814                         m = NULL;
815                 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
816                         m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
817                 kfree(rtm, M_RTABLE);
818         }
819         if (m != NULL)
820                 rts_input_skip(m, family, rp);
821         return (error);
822 }
823
824 static void
825 route_output_add_callback(int cmd, int error, struct rt_addrinfo *rtinfo,
826                           struct rtentry *rt, void *arg)
827 {
828         struct rt_msghdr *rtm = arg;
829
830         if (error == 0 && rt != NULL) {
831                 rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
832                     &rt->rt_rmx);
833                 rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
834                 rt->rt_rmx.rmx_locks |=
835                     (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
836                 if (rtinfo->rti_genmask != NULL) {
837                         rt->rt_genmask = rtmask_purelookup(rtinfo->rti_genmask);
838                         if (rt->rt_genmask == NULL) {
839                                 /*
840                                  * This should not happen, since we
841                                  * have already installed genmask
842                                  * on each CPU before we reach here.
843                                  */
844                                 panic("genmask is gone!?");
845                         }
846                 } else {
847                         rt->rt_genmask = NULL;
848                 }
849                 rtm->rtm_index = rt->rt_ifp->if_index;
850         }
851 }
852
853 static void
854 route_output_delete_callback(int cmd, int error, struct rt_addrinfo *rtinfo,
855                           struct rtentry *rt, void *arg)
856 {
857         if (error == 0 && rt) {
858                 ++rt->rt_refcnt;
859                 if (fillrtmsg(arg, rt, rtinfo) != 0) {
860                         error = ENOBUFS;
861                         /* XXX no way to return the error */
862                 }
863                 --rt->rt_refcnt;
864         }
865         if (rt && rt->rt_refcnt == 0) {
866                 ++rt->rt_refcnt;
867                 rtfree(rt);
868         }
869 }
870
871 static int
872 route_output_get_callback(int cmd, struct rt_addrinfo *rtinfo,
873                           struct rtentry *rt, void *arg, int found_cnt)
874 {
875         int error, found = 0;
876
877         if (((rtinfo->rti_flags ^ rt->rt_flags) & RTF_HOST) == 0)
878                 found = 1;
879
880         error = fillrtmsg(arg, rt, rtinfo);
881         if (!error && found) {
882                 /* Got the exact match, we could return now! */
883                 error = EJUSTRETURN;
884         }
885         return error;
886 }
887
888 static int
889 route_output_change_callback(int cmd, struct rt_addrinfo *rtinfo,
890                              struct rtentry *rt, void *arg, int found_cnt)
891 {
892         struct rt_msghdr *rtm = arg;
893         struct ifaddr *ifa;
894         int error = 0;
895
896         /*
897          * new gateway could require new ifaddr, ifp;
898          * flags may also be different; ifp may be specified
899          * by ll sockaddr when protocol address is ambiguous
900          */
901         if (((rt->rt_flags & RTF_GATEWAY) && rtinfo->rti_gateway != NULL) ||
902             rtinfo->rti_ifpaddr != NULL ||
903             (rtinfo->rti_ifaaddr != NULL &&
904              !sa_equal(rtinfo->rti_ifaaddr, rt->rt_ifa->ifa_addr))) {
905                 error = rt_getifa(rtinfo);
906                 if (error != 0)
907                         goto done;
908         }
909         if (rtinfo->rti_gateway != NULL) {
910                 /*
911                  * We only need to generate rtmsg upon the
912                  * first route to be changed.
913                  */
914                 error = rt_setgate(rt, rt_key(rt), rtinfo->rti_gateway);
915                 if (error != 0)
916                         goto done;
917         }
918         if ((ifa = rtinfo->rti_ifa) != NULL) {
919                 struct ifaddr *oifa = rt->rt_ifa;
920
921                 if (oifa != ifa) {
922                         if (oifa && oifa->ifa_rtrequest)
923                                 oifa->ifa_rtrequest(RTM_DELETE, rt);
924                         IFAFREE(rt->rt_ifa);
925                         IFAREF(ifa);
926                         rt->rt_ifa = ifa;
927                         rt->rt_ifp = rtinfo->rti_ifp;
928                 }
929         }
930         rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx, &rt->rt_rmx);
931         if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
932                 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt);
933         if (rtinfo->rti_genmask != NULL) {
934                 rt->rt_genmask = rtmask_purelookup(rtinfo->rti_genmask);
935                 if (rt->rt_genmask == NULL) {
936                         /*
937                          * This should not happen, since we
938                          * have already installed genmask
939                          * on each CPU before we reach here.
940                          */
941                         panic("genmask is gone!?");
942                 }
943         }
944         rtm->rtm_index = rt->rt_ifp->if_index;
945         if (found_cnt == 1)
946                 rt_rtmsg(RTM_CHANGE, rt, rt->rt_ifp, 0);
947 done:
948         return error;
949 }
950
951 static int
952 route_output_lock_callback(int cmd, struct rt_addrinfo *rtinfo,
953                            struct rtentry *rt, void *arg,
954                            int found_cnt __unused)
955 {
956         struct rt_msghdr *rtm = arg;
957
958         rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
959         rt->rt_rmx.rmx_locks |=
960                 (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
961         return 0;
962 }
963
964 static void
965 rt_setmetrics(u_long which, struct rt_metrics *in, struct rt_metrics *out)
966 {
967 #define setmetric(flag, elt) if (which & (flag)) out->elt = in->elt;
968         setmetric(RTV_RPIPE, rmx_recvpipe);
969         setmetric(RTV_SPIPE, rmx_sendpipe);
970         setmetric(RTV_SSTHRESH, rmx_ssthresh);
971         setmetric(RTV_RTT, rmx_rtt);
972         setmetric(RTV_RTTVAR, rmx_rttvar);
973         setmetric(RTV_HOPCOUNT, rmx_hopcount);
974         setmetric(RTV_MTU, rmx_mtu);
975         setmetric(RTV_EXPIRE, rmx_expire);
976         setmetric(RTV_MSL, rmx_msl);
977         setmetric(RTV_IWMAXSEGS, rmx_iwmaxsegs);
978         setmetric(RTV_IWCAPSEGS, rmx_iwcapsegs);
979 #undef setmetric
980 }
981
982 /*
983  * Extract the addresses of the passed sockaddrs.
984  * Do a little sanity checking so as to avoid bad memory references.
985  * This data is derived straight from userland.
986  */
987 static int
988 rt_xaddrs(char *cp, char *cplim, struct rt_addrinfo *rtinfo)
989 {
990         struct sockaddr *sa;
991         int i;
992
993         for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
994                 if ((rtinfo->rti_addrs & (1 << i)) == 0)
995                         continue;
996                 sa = (struct sockaddr *)cp;
997                 /*
998                  * It won't fit.
999                  */
1000                 if ((cp + sa->sa_len) > cplim) {
1001                         return (EINVAL);
1002                 }
1003
1004                 /*
1005                  * There are no more...  Quit now.
1006                  * If there are more bits, they are in error.
1007                  * I've seen this.  route(1) can evidently generate these. 
1008                  * This causes kernel to core dump.
1009                  * For compatibility, if we see this, point to a safe address.
1010                  */
1011                 if (sa->sa_len == 0) {
1012                         static struct sockaddr sa_zero = {
1013                                 sizeof sa_zero, AF_INET,
1014                         };
1015
1016                         rtinfo->rti_info[i] = &sa_zero;
1017                         kprintf("rtsock: received more addr bits than sockaddrs.\n");
1018                         return (0); /* should be EINVAL but for compat */
1019                 }
1020
1021                 /* Accept the sockaddr. */
1022                 rtinfo->rti_info[i] = sa;
1023                 cp += RT_ROUNDUP(sa->sa_len);
1024         }
1025         return (0);
1026 }
1027
1028 static int
1029 rt_msghdrsize(int type)
1030 {
1031         switch (type) {
1032         case RTM_DELADDR:
1033         case RTM_NEWADDR:
1034                 return sizeof(struct ifa_msghdr);
1035         case RTM_DELMADDR:
1036         case RTM_NEWMADDR:
1037                 return sizeof(struct ifma_msghdr);
1038         case RTM_IFINFO:
1039                 return sizeof(struct if_msghdr);
1040         case RTM_IFANNOUNCE:
1041         case RTM_IEEE80211:
1042                 return sizeof(struct if_announcemsghdr);
1043         default:
1044                 return sizeof(struct rt_msghdr);
1045         }
1046 }
1047
1048 static int
1049 rt_msgsize(int type, const struct rt_addrinfo *rtinfo)
1050 {
1051         int len, i;
1052
1053         len = rt_msghdrsize(type);
1054         for (i = 0; i < RTAX_MAX; i++) {
1055                 if (rtinfo->rti_info[i] != NULL)
1056                         len += RT_ROUNDUP(rtinfo->rti_info[i]->sa_len);
1057         }
1058         len = ALIGN(len);
1059         return len;
1060 }
1061
1062 /*
1063  * Build a routing message in a buffer.
1064  * Copy the addresses in the rtinfo->rti_info[] sockaddr array
1065  * to the end of the buffer after the message header.
1066  *
1067  * Set the rtinfo->rti_addrs bitmask of addresses present in rtinfo->rti_info[].
1068  * This side-effect can be avoided if we reorder the addrs bitmask field in all
1069  * the route messages to line up so we can set it here instead of back in the
1070  * calling routine.
1071  *
1072  * NOTE! The buffer may already contain a partially filled-out rtm via
1073  *       _fillrtmsg().
1074  */
1075 static void
1076 rt_msg_buffer(int type, struct rt_addrinfo *rtinfo, void *buf, int msglen)
1077 {
1078         struct rt_msghdr *rtm;
1079         char *cp;
1080         int dlen, i;
1081
1082         rtm = (struct rt_msghdr *) buf;
1083         rtm->rtm_version = RTM_VERSION;
1084         rtm->rtm_type = type;
1085         rtm->rtm_msglen = msglen;
1086
1087         cp = (char *)buf + rt_msghdrsize(type);
1088         rtinfo->rti_addrs = 0;
1089         for (i = 0; i < RTAX_MAX; i++) {
1090                 struct sockaddr *sa;
1091
1092                 if ((sa = rtinfo->rti_info[i]) == NULL)
1093                         continue;
1094                 rtinfo->rti_addrs |= (1 << i);
1095                 dlen = RT_ROUNDUP(sa->sa_len);
1096                 bcopy(sa, cp, dlen);
1097                 cp += dlen;
1098         }
1099 }
1100
1101 /*
1102  * Build a routing message in a mbuf chain.
1103  * Copy the addresses in the rtinfo->rti_info[] sockaddr array
1104  * to the end of the mbuf after the message header.
1105  *
1106  * Set the rtinfo->rti_addrs bitmask of addresses present in rtinfo->rti_info[].
1107  * This side-effect can be avoided if we reorder the addrs bitmask field in all
1108  * the route messages to line up so we can set it here instead of back in the
1109  * calling routine.
1110  */
1111 static struct mbuf *
1112 rt_msg_mbuf(int type, struct rt_addrinfo *rtinfo)
1113 {
1114         struct mbuf *m;
1115         struct rt_msghdr *rtm;
1116         int hlen, len;
1117         int i;
1118
1119         hlen = rt_msghdrsize(type);
1120         KASSERT(hlen <= MCLBYTES, ("rt_msg_mbuf: hlen %d doesn't fit", hlen));
1121
1122         m = m_getl(hlen, M_NOWAIT, MT_DATA, M_PKTHDR, NULL);
1123         if (m == NULL)
1124                 return (NULL);
1125         mbuftrackid(m, 32);
1126         m->m_pkthdr.len = m->m_len = hlen;
1127         m->m_pkthdr.rcvif = NULL;
1128         rtinfo->rti_addrs = 0;
1129         len = hlen;
1130         for (i = 0; i < RTAX_MAX; i++) {
1131                 struct sockaddr *sa;
1132                 int dlen;
1133
1134                 if ((sa = rtinfo->rti_info[i]) == NULL)
1135                         continue;
1136                 rtinfo->rti_addrs |= (1 << i);
1137                 dlen = RT_ROUNDUP(sa->sa_len);
1138                 m_copyback(m, len, dlen, (caddr_t)sa); /* can grow mbuf chain */
1139                 len += dlen;
1140         }
1141         if (m->m_pkthdr.len != len) { /* one of the m_copyback() calls failed */
1142                 m_freem(m);
1143                 return (NULL);
1144         }
1145         rtm = mtod(m, struct rt_msghdr *);
1146         bzero(rtm, hlen);
1147         rtm->rtm_msglen = len;
1148         rtm->rtm_version = RTM_VERSION;
1149         rtm->rtm_type = type;
1150         return (m);
1151 }
1152
1153 /*
1154  * This routine is called to generate a message from the routing
1155  * socket indicating that a redirect has occurred, a routing lookup
1156  * has failed, or that a protocol has detected timeouts to a particular
1157  * destination.
1158  */
1159 void
1160 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
1161 {
1162         struct sockaddr *dst = rtinfo->rti_info[RTAX_DST];
1163         struct rt_msghdr *rtm;
1164         struct mbuf *m;
1165
1166         if (route_cb.any_count == 0)
1167                 return;
1168         m = rt_msg_mbuf(type, rtinfo);
1169         if (m == NULL)
1170                 return;
1171         rtm = mtod(m, struct rt_msghdr *);
1172         rtm->rtm_flags = RTF_DONE | flags;
1173         rtm->rtm_errno = error;
1174         rtm->rtm_addrs = rtinfo->rti_addrs;
1175         rts_input(m, familyof(dst));
1176 }
1177
1178 void
1179 rt_dstmsg(int type, struct sockaddr *dst, int error)
1180 {
1181         struct rt_msghdr *rtm;
1182         struct rt_addrinfo addrs;
1183         struct mbuf *m;
1184
1185         if (route_cb.any_count == 0)
1186                 return;
1187         bzero(&addrs, sizeof(struct rt_addrinfo));
1188         addrs.rti_info[RTAX_DST] = dst;
1189         m = rt_msg_mbuf(type, &addrs);
1190         if (m == NULL)
1191                 return;
1192         rtm = mtod(m, struct rt_msghdr *);
1193         rtm->rtm_flags = RTF_DONE;
1194         rtm->rtm_errno = error;
1195         rtm->rtm_addrs = addrs.rti_addrs;
1196         rts_input(m, familyof(dst));
1197 }
1198
1199 /*
1200  * This routine is called to generate a message from the routing
1201  * socket indicating that the status of a network interface has changed.
1202  */
1203 void
1204 rt_ifmsg(struct ifnet *ifp)
1205 {
1206         struct if_msghdr *ifm;
1207         struct mbuf *m;
1208         struct rt_addrinfo rtinfo;
1209
1210         if (route_cb.any_count == 0)
1211                 return;
1212         bzero(&rtinfo, sizeof(struct rt_addrinfo));
1213         m = rt_msg_mbuf(RTM_IFINFO, &rtinfo);
1214         if (m == NULL)
1215                 return;
1216         ifm = mtod(m, struct if_msghdr *);
1217         ifm->ifm_index = ifp->if_index;
1218         ifm->ifm_flags = ifp->if_flags;
1219         ifm->ifm_data = ifp->if_data;
1220         ifm->ifm_addrs = 0;
1221         rts_input(m, 0);
1222 }
1223
1224 static void
1225 rt_ifamsg(int cmd, struct ifaddr *ifa)
1226 {
1227         struct ifa_msghdr *ifam;
1228         struct rt_addrinfo rtinfo;
1229         struct mbuf *m;
1230         struct ifnet *ifp = ifa->ifa_ifp;
1231
1232         bzero(&rtinfo, sizeof(struct rt_addrinfo));
1233         rtinfo.rti_ifaaddr = ifa->ifa_addr;
1234         rtinfo.rti_ifpaddr =
1235                 TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa->ifa_addr;
1236         rtinfo.rti_netmask = ifa->ifa_netmask;
1237         rtinfo.rti_bcastaddr = ifa->ifa_dstaddr;
1238
1239         m = rt_msg_mbuf(cmd, &rtinfo);
1240         if (m == NULL)
1241                 return;
1242
1243         ifam = mtod(m, struct ifa_msghdr *);
1244         ifam->ifam_index = ifp->if_index;
1245         ifam->ifam_flags = ifa->ifa_flags;
1246         ifam->ifam_addrs = rtinfo.rti_addrs;
1247         ifam->ifam_addrflags = if_addrflags(ifa);
1248         ifam->ifam_metric = ifa->ifa_metric;
1249
1250         rts_input(m, familyof(ifa->ifa_addr));
1251 }
1252
1253 void
1254 rt_rtmsg(int cmd, struct rtentry *rt, struct ifnet *ifp, int error)
1255 {
1256         struct rt_msghdr *rtm;
1257         struct rt_addrinfo rtinfo;
1258         struct mbuf *m;
1259         struct sockaddr *dst;
1260
1261         if (rt == NULL)
1262                 return;
1263
1264         bzero(&rtinfo, sizeof(struct rt_addrinfo));
1265         rtinfo.rti_dst = dst = rt_key(rt);
1266         rtinfo.rti_gateway = rt->rt_gateway;
1267         rtinfo.rti_netmask = rt_mask(rt);
1268         if (ifp != NULL) {
1269                 rtinfo.rti_ifpaddr =
1270                 TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa->ifa_addr;
1271         }
1272         if (rt->rt_ifa != NULL)
1273                 rtinfo.rti_ifaaddr = rt->rt_ifa->ifa_addr;
1274
1275         m = rt_msg_mbuf(cmd, &rtinfo);
1276         if (m == NULL)
1277                 return;
1278
1279         rtm = mtod(m, struct rt_msghdr *);
1280         if (ifp != NULL)
1281                 rtm->rtm_index = ifp->if_index;
1282         rtm->rtm_flags |= rt->rt_flags;
1283         rtm->rtm_errno = error;
1284         rtm->rtm_addrs = rtinfo.rti_addrs;
1285
1286         rts_input(m, familyof(dst));
1287 }
1288
1289 /*
1290  * This is called to generate messages from the routing socket
1291  * indicating a network interface has had addresses associated with it.
1292  * if we ever reverse the logic and replace messages TO the routing
1293  * socket indicate a request to configure interfaces, then it will
1294  * be unnecessary as the routing socket will automatically generate
1295  * copies of it.
1296  */
1297 void
1298 rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt)
1299 {
1300         if (route_cb.any_count == 0)
1301                 return;
1302
1303         if (cmd == RTM_ADD) {
1304                 rt_ifamsg(RTM_NEWADDR, ifa);
1305                 rt_rtmsg(RTM_ADD, rt, ifa->ifa_ifp, error);
1306         } else {
1307                 KASSERT((cmd == RTM_DELETE), ("unknown cmd %d", cmd));
1308                 rt_rtmsg(RTM_DELETE, rt, ifa->ifa_ifp, error);
1309                 rt_ifamsg(RTM_DELADDR, ifa);
1310         }
1311 }
1312
1313 /*
1314  * This is the analogue to the rt_newaddrmsg which performs the same
1315  * function but for multicast group memberhips.  This is easier since
1316  * there is no route state to worry about.
1317  */
1318 void
1319 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
1320 {
1321         struct rt_addrinfo rtinfo;
1322         struct mbuf *m = NULL;
1323         struct ifnet *ifp = ifma->ifma_ifp;
1324         struct ifma_msghdr *ifmam;
1325
1326         if (route_cb.any_count == 0)
1327                 return;
1328
1329         bzero(&rtinfo, sizeof(struct rt_addrinfo));
1330         rtinfo.rti_ifaaddr = ifma->ifma_addr;
1331         if (ifp != NULL && !TAILQ_EMPTY(&ifp->if_addrheads[mycpuid])) {
1332                 rtinfo.rti_ifpaddr =
1333                 TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa->ifa_addr;
1334         }
1335         /*
1336          * If a link-layer address is present, present it as a ``gateway''
1337          * (similarly to how ARP entries, e.g., are presented).
1338          */
1339         rtinfo.rti_gateway = ifma->ifma_lladdr;
1340
1341         m = rt_msg_mbuf(cmd, &rtinfo);
1342         if (m == NULL)
1343                 return;
1344
1345         ifmam = mtod(m, struct ifma_msghdr *);
1346         ifmam->ifmam_index = ifp->if_index;
1347         ifmam->ifmam_addrs = rtinfo.rti_addrs;
1348
1349         rts_input(m, familyof(ifma->ifma_addr));
1350 }
1351
1352 static struct mbuf *
1353 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1354                      struct rt_addrinfo *info)
1355 {
1356         struct if_announcemsghdr *ifan;
1357         struct mbuf *m;
1358
1359         if (route_cb.any_count == 0)
1360                 return NULL;
1361
1362         bzero(info, sizeof(*info));
1363         m = rt_msg_mbuf(type, info);
1364         if (m == NULL)
1365                 return NULL;
1366
1367         ifan = mtod(m, struct if_announcemsghdr *);
1368         ifan->ifan_index = ifp->if_index;
1369         strlcpy(ifan->ifan_name, ifp->if_xname, sizeof ifan->ifan_name);
1370         ifan->ifan_what = what;
1371         return m;
1372 }
1373
1374 /*
1375  * This is called to generate routing socket messages indicating
1376  * IEEE80211 wireless events.
1377  * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1378  */
1379 void
1380 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
1381 {
1382         struct rt_addrinfo info;
1383         struct mbuf *m;
1384
1385         m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1386         if (m == NULL)
1387                 return;
1388
1389         /*
1390          * Append the ieee80211 data.  Try to stick it in the
1391          * mbuf containing the ifannounce msg; otherwise allocate
1392          * a new mbuf and append.
1393          *
1394          * NB: we assume m is a single mbuf.
1395          */
1396         if (data_len > M_TRAILINGSPACE(m)) {
1397                 /* XXX use m_getb(data_len, M_NOWAIT, MT_DATA, 0); */
1398                 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
1399                 if (n == NULL) {
1400                         m_freem(m);
1401                         return;
1402                 }
1403                 KKASSERT(data_len <= M_TRAILINGSPACE(n));
1404                 bcopy(data, mtod(n, void *), data_len);
1405                 n->m_len = data_len;
1406                 m->m_next = n;
1407         } else if (data_len > 0) {
1408                 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
1409                 m->m_len += data_len;
1410         }
1411         mbuftrackid(m, 33);
1412         if (m->m_flags & M_PKTHDR)
1413                 m->m_pkthdr.len += data_len;
1414         mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
1415         rts_input(m, 0);
1416 }
1417
1418 /*
1419  * This is called to generate routing socket messages indicating
1420  * network interface arrival and departure.
1421  */
1422 void
1423 rt_ifannouncemsg(struct ifnet *ifp, int what)
1424 {
1425         struct rt_addrinfo addrinfo;
1426         struct mbuf *m;
1427
1428         m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &addrinfo);
1429         if (m != NULL)
1430                 rts_input(m, 0);
1431 }
1432
1433 static int
1434 resizewalkarg(struct walkarg *w, int len)
1435 {
1436         void *newptr;
1437
1438         newptr = kmalloc(len, M_RTABLE, M_INTWAIT | M_NULLOK);
1439         if (newptr == NULL)
1440                 return (ENOMEM);
1441         if (w->w_tmem != NULL)
1442                 kfree(w->w_tmem, M_RTABLE);
1443         w->w_tmem = newptr;
1444         w->w_tmemsize = len;
1445         bzero(newptr, len);
1446
1447         return (0);
1448 }
1449
1450 static void
1451 ifnet_compute_stats(struct ifnet *ifp)
1452 {
1453         IFNET_STAT_GET(ifp, ipackets, ifp->if_ipackets);
1454         IFNET_STAT_GET(ifp, ierrors, ifp->if_ierrors);
1455         IFNET_STAT_GET(ifp, opackets, ifp->if_opackets);
1456         IFNET_STAT_GET(ifp, collisions, ifp->if_collisions);
1457         IFNET_STAT_GET(ifp, ibytes, ifp->if_ibytes);
1458         IFNET_STAT_GET(ifp, obytes, ifp->if_obytes);
1459         IFNET_STAT_GET(ifp, imcasts, ifp->if_imcasts);
1460         IFNET_STAT_GET(ifp, omcasts, ifp->if_omcasts);
1461         IFNET_STAT_GET(ifp, iqdrops, ifp->if_iqdrops);
1462         IFNET_STAT_GET(ifp, noproto, ifp->if_noproto);
1463         IFNET_STAT_GET(ifp, oqdrops, ifp->if_oqdrops);
1464 }
1465
1466 static int
1467 if_addrflags(const struct ifaddr *ifa)
1468 {
1469         switch (ifa->ifa_addr->sa_family) {
1470 #ifdef INET6
1471         case AF_INET6:
1472                 return ((const struct in6_ifaddr *)ifa)->ia6_flags;
1473 #endif
1474         default:
1475                 return 0;
1476         }
1477 }
1478
1479 static int
1480 sysctl_iflist(int af, struct walkarg *w)
1481 {
1482         struct ifnet *ifp;
1483         struct rt_addrinfo rtinfo;
1484         int msglen, error;
1485
1486         bzero(&rtinfo, sizeof(struct rt_addrinfo));
1487
1488         ifnet_lock();
1489         TAILQ_FOREACH(ifp, &ifnetlist, if_link) {
1490                 struct ifaddr_container *ifac, *ifac_mark;
1491                 struct ifaddr_marker mark;
1492                 struct ifaddrhead *head;
1493                 struct ifaddr *ifa;
1494
1495                 if (w->w_arg && w->w_arg != ifp->if_index)
1496                         continue;
1497                 head = &ifp->if_addrheads[mycpuid];
1498                 /*
1499                  * There is no need to reference the first ifaddr
1500                  * even if the following resizewalkarg() blocks,
1501                  * since the first ifaddr will not be destroyed
1502                  * when the ifnet lock is held.
1503                  */
1504                 ifac = TAILQ_FIRST(head);
1505                 ifa = ifac->ifa;
1506                 rtinfo.rti_ifpaddr = ifa->ifa_addr;
1507                 msglen = rt_msgsize(RTM_IFINFO, &rtinfo);
1508                 if (w->w_tmemsize < msglen && resizewalkarg(w, msglen) != 0) {
1509                         ifnet_unlock();
1510                         return (ENOMEM);
1511                 }
1512                 rt_msg_buffer(RTM_IFINFO, &rtinfo, w->w_tmem, msglen);
1513                 rtinfo.rti_ifpaddr = NULL;
1514                 if (w->w_req != NULL && w->w_tmem != NULL) {
1515                         struct if_msghdr *ifm = w->w_tmem;
1516
1517                         ifm->ifm_index = ifp->if_index;
1518                         ifm->ifm_flags = ifp->if_flags;
1519                         ifnet_compute_stats(ifp);
1520                         ifm->ifm_data = ifp->if_data;
1521                         ifm->ifm_addrs = rtinfo.rti_addrs;
1522                         error = SYSCTL_OUT(w->w_req, ifm, msglen);
1523                         if (error) {
1524                                 ifnet_unlock();
1525                                 return (error);
1526                         }
1527                 }
1528                 /*
1529                  * Add a marker, since SYSCTL_OUT() could block and during
1530                  * that period the list could be changed.
1531                  */
1532                 ifa_marker_init(&mark, ifp);
1533                 ifac_mark = &mark.ifac;
1534                 TAILQ_INSERT_AFTER(head, ifac, ifac_mark, ifa_link);
1535                 while ((ifac = TAILQ_NEXT(ifac_mark, ifa_link)) != NULL) {
1536                         TAILQ_REMOVE(head, ifac_mark, ifa_link);
1537                         TAILQ_INSERT_AFTER(head, ifac, ifac_mark, ifa_link);
1538
1539                         ifa = ifac->ifa;
1540
1541                         /* Ignore marker */
1542                         if (ifa->ifa_addr->sa_family == AF_UNSPEC)
1543                                 continue;
1544
1545                         if (af && af != ifa->ifa_addr->sa_family)
1546                                 continue;
1547                         if (curproc->p_ucred->cr_prison &&
1548                             prison_if(curproc->p_ucred, ifa->ifa_addr))
1549                                 continue;
1550                         rtinfo.rti_ifaaddr = ifa->ifa_addr;
1551                         rtinfo.rti_netmask = ifa->ifa_netmask;
1552                         rtinfo.rti_bcastaddr = ifa->ifa_dstaddr;
1553                         msglen = rt_msgsize(RTM_NEWADDR, &rtinfo);
1554                         /*
1555                          * Keep a reference on this ifaddr, so that it will
1556                          * not be destroyed if the following resizewalkarg()
1557                          * blocks.
1558                          */
1559                         IFAREF(ifa);
1560                         if (w->w_tmemsize < msglen &&
1561                             resizewalkarg(w, msglen) != 0) {
1562                                 IFAFREE(ifa);
1563                                 TAILQ_REMOVE(head, ifac_mark, ifa_link);
1564                                 ifnet_unlock();
1565                                 return (ENOMEM);
1566                         }
1567                         rt_msg_buffer(RTM_NEWADDR, &rtinfo, w->w_tmem, msglen);
1568                         if (w->w_req != NULL) {
1569                                 struct ifa_msghdr *ifam = w->w_tmem;
1570
1571                                 ifam->ifam_index = ifa->ifa_ifp->if_index;
1572                                 ifam->ifam_flags = ifa->ifa_flags;
1573                                 ifam->ifam_addrs = rtinfo.rti_addrs;
1574                                 ifam->ifam_addrflags = if_addrflags(ifa);
1575                                 ifam->ifam_metric = ifa->ifa_metric;
1576                                 error = SYSCTL_OUT(w->w_req, w->w_tmem, msglen);
1577                                 if (error) {
1578                                         IFAFREE(ifa);
1579                                         TAILQ_REMOVE(head, ifac_mark, ifa_link);
1580                                         ifnet_unlock();
1581                                         return (error);
1582                                 }
1583                         }
1584                         IFAFREE(ifa);
1585                 }
1586                 TAILQ_REMOVE(head, ifac_mark, ifa_link);
1587                 rtinfo.rti_netmask = NULL;
1588                 rtinfo.rti_ifaaddr = NULL;
1589                 rtinfo.rti_bcastaddr = NULL;
1590         }
1591         ifnet_unlock();
1592         return (0);
1593 }
1594
1595 static int
1596 rttable_walkarg_create(struct rttable_walkarg *w, int op, int arg)
1597 {
1598         struct rt_addrinfo rtinfo;
1599         struct sockaddr_storage ss;
1600         int i, msglen;
1601
1602         memset(w, 0, sizeof(*w));
1603         w->w_op = op;
1604         w->w_arg = arg;
1605
1606         memset(&ss, 0, sizeof(ss));
1607         ss.ss_len = sizeof(ss);
1608
1609         memset(&rtinfo, 0, sizeof(rtinfo));
1610         for (i = 0; i < RTAX_MAX; ++i)
1611                 rtinfo.rti_info[i] = (struct sockaddr *)&ss;
1612         msglen = rt_msgsize(RTM_GET, &rtinfo);
1613
1614         w->w_bufsz = msglen * RTTABLE_DUMP_MSGCNT_MAX;
1615         w->w_buf = kmalloc(w->w_bufsz, M_TEMP, M_WAITOK | M_NULLOK);
1616         if (w->w_buf == NULL)
1617                 return ENOMEM;
1618         return 0;
1619 }
1620
1621 static void
1622 rttable_walkarg_destroy(struct rttable_walkarg *w)
1623 {
1624         kfree(w->w_buf, M_TEMP);
1625 }
1626
1627 static void
1628 rttable_entry_rtinfo(struct rt_addrinfo *rtinfo, struct radix_node *rn)
1629 {
1630         struct rtentry *rt = (struct rtentry *)rn;
1631
1632         bzero(rtinfo, sizeof(*rtinfo));
1633         rtinfo->rti_dst = rt_key(rt);
1634         rtinfo->rti_gateway = rt->rt_gateway;
1635         rtinfo->rti_netmask = rt_mask(rt);
1636         rtinfo->rti_genmask = rt->rt_genmask;
1637         if (rt->rt_ifp != NULL) {
1638                 rtinfo->rti_ifpaddr =
1639                 TAILQ_FIRST(&rt->rt_ifp->if_addrheads[mycpuid])->ifa->ifa_addr;
1640                 rtinfo->rti_ifaaddr = rt->rt_ifa->ifa_addr;
1641                 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1642                         rtinfo->rti_bcastaddr = rt->rt_ifa->ifa_dstaddr;
1643         }
1644 }
1645
1646 static int
1647 rttable_walk_entry(struct radix_node *rn, void *xw)
1648 {
1649         struct rttable_walkarg *w = xw;
1650         struct rtentry *rt = (struct rtentry *)rn;
1651         struct rt_addrinfo rtinfo;
1652         struct rt_msghdr *rtm;
1653         boolean_t save = FALSE;
1654         int msglen, w_bufleft;
1655         void *ptr;
1656
1657         rttable_entry_rtinfo(&rtinfo, rn);
1658         msglen = rt_msgsize(RTM_GET, &rtinfo);
1659
1660         w_bufleft = w->w_bufsz - w->w_buflen;
1661
1662         if (rn->rn_dupedkey != NULL) {
1663                 struct radix_node *rn1 = rn;
1664                 int total_msglen = msglen;
1665
1666                 /*
1667                  * Make sure that we have enough space left for all
1668                  * dupedkeys, since rn_walktree_at always starts
1669                  * from the first dupedkey.
1670                  */
1671                 while ((rn1 = rn1->rn_dupedkey) != NULL) {
1672                         struct rt_addrinfo rtinfo1;
1673                         int msglen1;
1674
1675                         if (rn1->rn_flags & RNF_ROOT)
1676                                 continue;
1677
1678                         rttable_entry_rtinfo(&rtinfo1, rn1);
1679                         msglen1 = rt_msgsize(RTM_GET, &rtinfo1);
1680                         total_msglen += msglen1;
1681                 }
1682
1683                 if (total_msglen > w_bufleft) {
1684                         if (total_msglen > w->w_bufsz) {
1685                                 static int logged = 0;
1686
1687                                 if (!logged) {
1688                                         kprintf("buffer is too small for "
1689                                             "all dupedkeys, increase "
1690                                             "RTTABLE_DUMP_MSGCNT_MAX\n");
1691                                         logged = 1;
1692                                 }
1693                                 return ENOMEM;
1694                         }
1695                         save = TRUE;
1696                 }
1697         } else if (msglen > w_bufleft) {
1698                 save = TRUE;
1699         }
1700
1701         if (save) {
1702                 /*
1703                  * Not enough buffer left; remember the position
1704                  * to start from upon next round.
1705                  */
1706                 KASSERT(msglen <= w->w_bufsz, ("msg too long %d", msglen));
1707
1708                 KASSERT(rtinfo.rti_dst->sa_len <= sizeof(w->w_key0),
1709                     ("key too long %d", rtinfo.rti_dst->sa_len));
1710                 memset(&w->w_key0, 0, sizeof(w->w_key0));
1711                 memcpy(&w->w_key0, rtinfo.rti_dst, rtinfo.rti_dst->sa_len);
1712                 w->w_key = (const char *)&w->w_key0;
1713
1714                 if (rtinfo.rti_netmask != NULL) {
1715                         KASSERT(
1716                             rtinfo.rti_netmask->sa_len <= sizeof(w->w_mask0),
1717                             ("mask too long %d", rtinfo.rti_netmask->sa_len));
1718                         memset(&w->w_mask0, 0, sizeof(w->w_mask0));
1719                         memcpy(&w->w_mask0, rtinfo.rti_netmask,
1720                             rtinfo.rti_netmask->sa_len);
1721                         w->w_mask = (const char *)&w->w_mask0;
1722                 } else {
1723                         w->w_mask = NULL;
1724                 }
1725                 return EJUSTRETURN;
1726         }
1727
1728         if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1729                 return 0;
1730
1731         ptr = ((uint8_t *)w->w_buf) + w->w_buflen;
1732         rt_msg_buffer(RTM_GET, &rtinfo, ptr, msglen);
1733
1734         rtm = (struct rt_msghdr *)ptr;
1735         rtm->rtm_flags = rt->rt_flags;
1736         rtm->rtm_use = rt->rt_use;
1737         rtm->rtm_rmx = rt->rt_rmx;
1738         rtm->rtm_index = rt->rt_ifp->if_index;
1739         rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
1740         rtm->rtm_addrs = rtinfo.rti_addrs;
1741
1742         w->w_buflen += msglen;
1743
1744         return 0;
1745 }
1746
1747 static void
1748 rttable_walk_dispatch(netmsg_t msg)
1749 {
1750         struct netmsg_rttable_walk *nmsg = (struct netmsg_rttable_walk *)msg;
1751         struct radix_node_head *rnh = rt_tables[mycpuid][nmsg->af];
1752         struct rttable_walkarg *w = nmsg->w;
1753         int error;
1754
1755         error = rnh->rnh_walktree_at(rnh, w->w_key, w->w_mask,
1756             rttable_walk_entry, w);
1757         lwkt_replymsg(&nmsg->base.lmsg, error);
1758 }
1759
1760 static int
1761 sysctl_rttable(int af, struct sysctl_req *req, int op, int arg)
1762 {
1763         struct rttable_walkarg w;
1764         int error, i;
1765
1766         error = rttable_walkarg_create(&w, op, arg);
1767         if (error)
1768                 return error;
1769
1770         error = EINVAL;
1771         for (i = 1; i <= AF_MAX; i++) {
1772                 if (rt_tables[mycpuid][i] != NULL && (af == 0 || af == i)) {
1773                         w.w_key = NULL;
1774                         w.w_mask = NULL;
1775                         for (;;) {
1776                                 struct netmsg_rttable_walk nmsg;
1777
1778                                 netmsg_init(&nmsg.base, NULL,
1779                                     &curthread->td_msgport, 0,
1780                                     rttable_walk_dispatch);
1781                                 nmsg.af = i;
1782                                 nmsg.w = &w;
1783
1784                                 w.w_buflen = 0;
1785
1786                                 error = lwkt_domsg(netisr_cpuport(mycpuid),
1787                                     &nmsg.base.lmsg, 0);
1788                                 if (error && error != EJUSTRETURN)
1789                                         goto done;
1790
1791                                 if (req != NULL && w.w_buflen > 0) {
1792                                         int error1;
1793
1794                                         error1 = SYSCTL_OUT(req, w.w_buf,
1795                                             w.w_buflen);
1796                                         if (error1) {
1797                                                 error = error1;
1798                                                 goto done;
1799                                         }
1800                                 }
1801                                 if (error == 0) /* done */
1802                                         break;
1803                         }
1804                 }
1805         }
1806 done:
1807         rttable_walkarg_destroy(&w);
1808         return error;
1809 }
1810
1811 static int
1812 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
1813 {
1814         int     *name = (int *)arg1;
1815         u_int   namelen = arg2;
1816         int     error = EINVAL;
1817         int     origcpu, cpu;
1818         u_char  af;
1819         struct  walkarg w;
1820
1821         name ++;
1822         namelen--;
1823         if (req->newptr)
1824                 return (EPERM);
1825         if (namelen != 3 && namelen != 4)
1826                 return (EINVAL);
1827         af = name[0];
1828         bzero(&w, sizeof w);
1829         w.w_op = name[1];
1830         w.w_arg = name[2];
1831         w.w_req = req;
1832
1833         /*
1834          * Optional third argument specifies cpu, used primarily for
1835          * debugging the route table.
1836          */
1837         if (namelen == 4) {
1838                 if (name[3] < 0 || name[3] >= netisr_ncpus)
1839                         return (EINVAL);
1840                 cpu = name[3];
1841         } else {
1842                 /*
1843                  * Target cpu is not specified, use cpu0 then, so that
1844                  * the result set will be relatively stable.
1845                  */
1846                 cpu = 0;
1847         }
1848         origcpu = mycpuid;
1849         lwkt_migratecpu(cpu);
1850
1851         switch (w.w_op) {
1852         case NET_RT_DUMP:
1853         case NET_RT_FLAGS:
1854                 error = sysctl_rttable(af, w.w_req, w.w_op, w.w_arg);
1855                 break;
1856
1857         case NET_RT_IFLIST:
1858                 error = sysctl_iflist(af, &w);
1859                 break;
1860         }
1861         if (w.w_tmem != NULL)
1862                 kfree(w.w_tmem, M_RTABLE);
1863
1864         lwkt_migratecpu(origcpu);
1865         return (error);
1866 }
1867
1868 SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
1869
1870 /*
1871  * Definitions of protocols supported in the ROUTE domain.
1872  */
1873
1874 static struct domain routedomain;               /* or at least forward */
1875
1876 static struct protosw routesw[] = {
1877     {
1878         .pr_type = SOCK_RAW,
1879         .pr_domain = &routedomain,
1880         .pr_protocol = 0,
1881         .pr_flags = PR_ATOMIC|PR_ADDR,
1882         .pr_input = NULL,
1883         .pr_output = route_output,
1884         .pr_ctlinput = raw_ctlinput,
1885         .pr_ctloutput = route_ctloutput,
1886         .pr_ctlport = cpu0_ctlport,
1887
1888         .pr_init = raw_init,
1889         .pr_usrreqs = &route_usrreqs
1890     }
1891 };
1892
1893 static struct domain routedomain = {
1894         .dom_family             = AF_ROUTE,
1895         .dom_name               = "route",
1896         .dom_init               = NULL,
1897         .dom_externalize        = NULL,
1898         .dom_dispose            = NULL,
1899         .dom_protosw            = routesw,
1900         .dom_protoswNPROTOSW    = &routesw[(sizeof routesw)/(sizeof routesw[0])],
1901         .dom_next               = SLIST_ENTRY_INITIALIZER,
1902         .dom_rtattach           = NULL,
1903         .dom_rtoffset           = 0,
1904         .dom_maxrtkey           = 0,
1905         .dom_ifattach           = NULL,
1906         .dom_ifdetach           = NULL
1907 };
1908
1909 DOMAIN_SET(route);
1910