Sync zoneinfo database with tzdata2006p from elsie.
[dragonfly.git] / sys / net / rtsock.c
CommitLineData
f3ed2586
JH
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) 2004, 2005 Jeffrey M. Hsu. All rights reserved.
35 *
36 * License terms: all terms for the DragonFly license above plus the following:
37 *
38 * 4. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 *
41 * This product includes software developed by Jeffrey M. Hsu
42 * for the DragonFly Project.
43 *
44 * This requirement may be waived with permission from Jeffrey Hsu.
45 * Permission will be granted to any DragonFly user for free.
46 * This requirement will sunset and may be removed on Jan 31, 2006,
47 * after which the standard DragonFly license (as shown above) will
48 * apply.
49 */
50
984263bc
MD
51/*
52 * Copyright (c) 1988, 1991, 1993
53 * The Regents of the University of California. All rights reserved.
54 *
55 * Redistribution and use in source and binary forms, with or without
56 * modification, are permitted provided that the following conditions
57 * are met:
58 * 1. Redistributions of source code must retain the above copyright
59 * notice, this list of conditions and the following disclaimer.
60 * 2. Redistributions in binary form must reproduce the above copyright
61 * notice, this list of conditions and the following disclaimer in the
62 * documentation and/or other materials provided with the distribution.
63 * 3. All advertising materials mentioning features or use of this software
64 * must display the following acknowledgement:
65 * This product includes software developed by the University of
66 * California, Berkeley and its contributors.
67 * 4. Neither the name of the University nor the names of its contributors
68 * may be used to endorse or promote products derived from this software
69 * without specific prior written permission.
70 *
71 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
72 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
73 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
74 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
75 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
76 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
77 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
78 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
79 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
80 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
81 * SUCH DAMAGE.
82 *
83 * @(#)rtsock.c 8.7 (Berkeley) 10/12/95
84 * $FreeBSD: src/sys/net/rtsock.c,v 1.44.2.11 2002/12/04 14:05:41 ru Exp $
ba4257b8 85 * $DragonFly: src/sys/net/rtsock.c,v 1.35 2006/10/23 09:27:37 swildner Exp $
984263bc
MD
86 */
87
78812139
EN
88#include "opt_sctp.h"
89
984263bc
MD
90#include <sys/param.h>
91#include <sys/systm.h>
92#include <sys/kernel.h>
93#include <sys/sysctl.h>
94#include <sys/proc.h>
95#include <sys/malloc.h>
96#include <sys/mbuf.h>
47e78ae8 97#include <sys/protosw.h>
984263bc
MD
98#include <sys/socket.h>
99#include <sys/socketvar.h>
100#include <sys/domain.h>
a49aa710 101#include <sys/thread2.h>
deffea2e 102
984263bc
MD
103#include <net/if.h>
104#include <net/route.h>
105#include <net/raw_cb.h>
106
78812139
EN
107#ifdef SCTP
108extern void sctp_add_ip_address(struct ifaddr *ifa);
109extern void sctp_delete_ip_address(struct ifaddr *ifa);
110#endif /* SCTP */
111
984263bc
MD
112MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
113
f23061d4
JH
114static struct route_cb {
115 int ip_count;
116 int ip6_count;
117 int ipx_count;
118 int ns_count;
119 int any_count;
120} route_cb;
121
590b8cd4 122static const struct sockaddr route_src = { 2, PF_ROUTE, };
984263bc
MD
123
124struct walkarg {
125 int w_tmemsize;
126 int w_op, w_arg;
8e63efec 127 void *w_tmem;
984263bc
MD
128 struct sysctl_req *w_req;
129};
130
131static struct mbuf *
590b8cd4
JH
132 rt_msg_mbuf (int, struct rt_addrinfo *);
133static void rt_msg_buffer (int, struct rt_addrinfo *, void *buf, int len);
134static int rt_msgsize (int type, struct rt_addrinfo *rtinfo);
ef87f48d 135static int rt_xaddrs (char *, char *, struct rt_addrinfo *);
158abb01
RG
136static int sysctl_dumpentry (struct radix_node *rn, void *vw);
137static int sysctl_iflist (int af, struct walkarg *w);
deffea2e 138static int route_output(struct mbuf *, struct socket *, ...);
47e78ae8 139static void rt_setmetrics (u_long, struct rt_metrics *,
f23061d4 140 struct rt_metrics *);
984263bc 141
984263bc
MD
142/*
143 * It really doesn't make any sense at all for this code to share much
144 * with raw_usrreq.c, since its functionality is so restricted. XXX
145 */
146static int
147rts_abort(struct socket *so)
148{
a49aa710 149 int error;
f23061d4 150
a49aa710 151 crit_enter();
984263bc 152 error = raw_usrreqs.pru_abort(so);
a49aa710 153 crit_exit();
984263bc
MD
154 return error;
155}
156
157/* pru_accept is EOPNOTSUPP */
158
159static int
e4700d00 160rts_attach(struct socket *so, int proto, struct pru_attach_info *ai)
984263bc
MD
161{
162 struct rawcb *rp;
a49aa710 163 int error;
984263bc 164
ef87f48d 165 if (sotorawcb(so) != NULL)
984263bc 166 return EISCONN; /* XXX panic? */
f23061d4 167
efda3bd0 168 rp = kmalloc(sizeof *rp, M_PCB, M_WAITOK | M_ZERO);
ef87f48d 169 if (rp == NULL)
984263bc
MD
170 return ENOBUFS;
171
172 /*
a49aa710 173 * The critical section is necessary to block protocols from sending
984263bc
MD
174 * error notifications (like RTM_REDIRECT or RTM_LOSING) while
175 * this PCB is extant but incompletely initialized.
176 * Probably we should try to do more of this work beforehand and
a49aa710 177 * eliminate the critical section.
984263bc 178 */
a49aa710 179 crit_enter();
ef87f48d 180 so->so_pcb = rp;
e4700d00 181 error = raw_attach(so, proto, ai->sb_rlimit);
984263bc
MD
182 rp = sotorawcb(so);
183 if (error) {
a49aa710 184 crit_exit();
efda3bd0 185 kfree(rp, M_PCB);
984263bc
MD
186 return error;
187 }
188 switch(rp->rcb_proto.sp_protocol) {
189 case AF_INET:
190 route_cb.ip_count++;
191 break;
192 case AF_INET6:
193 route_cb.ip6_count++;
194 break;
195 case AF_IPX:
196 route_cb.ipx_count++;
197 break;
198 case AF_NS:
199 route_cb.ns_count++;
200 break;
201 }
202 rp->rcb_faddr = &route_src;
203 route_cb.any_count++;
204 soisconnected(so);
205 so->so_options |= SO_USELOOPBACK;
a49aa710 206 crit_exit();
984263bc
MD
207 return 0;
208}
209
210static int
dadab5e9 211rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
984263bc 212{
a49aa710 213 int error;
f23061d4 214
a49aa710 215 crit_enter();
dadab5e9 216 error = raw_usrreqs.pru_bind(so, nam, td); /* xxx just EINVAL */
a49aa710 217 crit_exit();
984263bc
MD
218 return error;
219}
220
221static int
dadab5e9 222rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
984263bc 223{
a49aa710 224 int error;
f23061d4 225
a49aa710 226 crit_enter();
dadab5e9 227 error = raw_usrreqs.pru_connect(so, nam, td); /* XXX just EINVAL */
a49aa710 228 crit_exit();
984263bc
MD
229 return error;
230}
231
232/* pru_connect2 is EOPNOTSUPP */
233/* pru_control is EOPNOTSUPP */
234
235static int
236rts_detach(struct socket *so)
237{
238 struct rawcb *rp = sotorawcb(so);
a49aa710 239 int error;
984263bc 240
a49aa710 241 crit_enter();
ef87f48d 242 if (rp != NULL) {
984263bc
MD
243 switch(rp->rcb_proto.sp_protocol) {
244 case AF_INET:
245 route_cb.ip_count--;
246 break;
247 case AF_INET6:
248 route_cb.ip6_count--;
249 break;
250 case AF_IPX:
251 route_cb.ipx_count--;
252 break;
253 case AF_NS:
254 route_cb.ns_count--;
255 break;
256 }
257 route_cb.any_count--;
258 }
259 error = raw_usrreqs.pru_detach(so);
a49aa710 260 crit_exit();
984263bc
MD
261 return error;
262}
263
264static int
265rts_disconnect(struct socket *so)
266{
a49aa710 267 int error;
f23061d4 268
a49aa710 269 crit_enter();
984263bc 270 error = raw_usrreqs.pru_disconnect(so);
a49aa710 271 crit_exit();
984263bc
MD
272 return error;
273}
274
275/* pru_listen is EOPNOTSUPP */
276
277static int
278rts_peeraddr(struct socket *so, struct sockaddr **nam)
279{
a49aa710 280 int error;
f23061d4 281
a49aa710 282 crit_enter();
984263bc 283 error = raw_usrreqs.pru_peeraddr(so, nam);
a49aa710 284 crit_exit();
984263bc
MD
285 return error;
286}
287
288/* pru_rcvd is EOPNOTSUPP */
289/* pru_rcvoob is EOPNOTSUPP */
290
291static int
292rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
dadab5e9 293 struct mbuf *control, struct thread *td)
984263bc 294{
a49aa710 295 int error;
f23061d4 296
a49aa710 297 crit_enter();
dadab5e9 298 error = raw_usrreqs.pru_send(so, flags, m, nam, control, td);
a49aa710 299 crit_exit();
984263bc
MD
300 return error;
301}
302
303/* pru_sense is null */
304
305static int
306rts_shutdown(struct socket *so)
307{
a49aa710 308 int error;
f23061d4 309
a49aa710 310 crit_enter();
984263bc 311 error = raw_usrreqs.pru_shutdown(so);
a49aa710 312 crit_exit();
984263bc
MD
313 return error;
314}
315
316static int
317rts_sockaddr(struct socket *so, struct sockaddr **nam)
318{
a49aa710 319 int error;
f23061d4 320
a49aa710 321 crit_enter();
984263bc 322 error = raw_usrreqs.pru_sockaddr(so, nam);
a49aa710 323 crit_exit();
984263bc
MD
324 return error;
325}
326
327static struct pr_usrreqs route_usrreqs = {
328 rts_abort, pru_accept_notsupp, rts_attach, rts_bind, rts_connect,
329 pru_connect2_notsupp, pru_control_notsupp, rts_detach, rts_disconnect,
330 pru_listen_notsupp, rts_peeraddr, pru_rcvd_notsupp, pru_rcvoob_notsupp,
331 rts_send, pru_sense_null, rts_shutdown, rts_sockaddr,
332 sosend, soreceive, sopoll
333};
334
590b8cd4
JH
335static __inline sa_family_t
336familyof(struct sockaddr *sa)
337{
338 return (sa != NULL ? sa->sa_family : 0);
339}
340
341static void
342rts_input(struct mbuf *m, sa_family_t family)
343{
344 static const struct sockaddr route_dst = { 2, PF_ROUTE, };
345 struct sockproto route_proto = { PF_ROUTE, family };
346
347 raw_input(m, &route_proto, &route_src, &route_dst);
348}
349
350static void *
351reallocbuf(void *ptr, size_t len, size_t olen)
352{
353 void *newptr;
354
efda3bd0 355 newptr = kmalloc(len, M_RTABLE, M_INTWAIT | M_NULLOK);
590b8cd4
JH
356 if (newptr == NULL)
357 return NULL;
358 bcopy(ptr, newptr, olen);
efda3bd0 359 kfree(ptr, M_RTABLE);
590b8cd4
JH
360 return (newptr);
361}
362
8e63efec
JH
363/*
364 * Internal helper routine for route_output().
365 */
590b8cd4
JH
366static int
367fillrtmsg(struct rt_msghdr **prtm, struct rtentry *rt,
368 struct rt_addrinfo *rtinfo)
369{
370 int msglen;
371 struct rt_msghdr *rtm = *prtm;
372
373 /* Fill in rt_addrinfo for call to rt_msg_buffer(). */
374 rtinfo->rti_dst = rt_key(rt);
375 rtinfo->rti_gateway = rt->rt_gateway;
376 rtinfo->rti_netmask = rt_mask(rt); /* might be NULL */
377 rtinfo->rti_genmask = rt->rt_genmask; /* might be NULL */
378 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
379 if (rt->rt_ifp != NULL) {
380 rtinfo->rti_ifpaddr =
381 TAILQ_FIRST(&rt->rt_ifp->if_addrhead)->ifa_addr;
382 rtinfo->rti_ifaaddr = rt->rt_ifa->ifa_addr;
383 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
384 rtinfo->rti_bcastaddr = rt->rt_ifa->ifa_dstaddr;
385 rtm->rtm_index = rt->rt_ifp->if_index;
386 } else {
387 rtinfo->rti_ifpaddr = NULL;
388 rtinfo->rti_ifaaddr = NULL;
389 }
390 }
391
392 msglen = rt_msgsize(rtm->rtm_type, rtinfo);
393 if (rtm->rtm_msglen < msglen) {
394 rtm = reallocbuf(rtm, msglen, rtm->rtm_msglen);
395 if (rtm == NULL)
396 return (ENOBUFS);
397 *prtm = rtm;
398 }
399 rt_msg_buffer(rtm->rtm_type, rtinfo, rtm, msglen);
400
401 rtm->rtm_flags = rt->rt_flags;
402 rtm->rtm_rmx = rt->rt_rmx;
403 rtm->rtm_addrs = rtinfo->rti_addrs;
404
405 return (0);
406}
407
ecdefdda
MD
408static void route_output_add_callback(int, int, struct rt_addrinfo *,
409 struct rtentry *, void *);
410static void route_output_delete_callback(int, int, struct rt_addrinfo *,
411 struct rtentry *, void *);
412static void route_output_change_callback(int, int, struct rt_addrinfo *,
413 struct rtentry *, void *);
414static void route_output_lock_callback(int, int, struct rt_addrinfo *,
415 struct rtentry *, void *);
416
984263bc
MD
417/*ARGSUSED*/
418static int
deffea2e 419route_output(struct mbuf *m, struct socket *so, ...)
984263bc 420{
2e9572df 421 struct rt_msghdr *rtm = NULL;
ecdefdda 422 struct rtentry *rt;
984263bc 423 struct radix_node_head *rnh;
f23061d4 424 struct rawcb *rp = NULL;
deffea2e 425 struct pr_output_info *oi;
590b8cd4 426 struct rt_addrinfo rtinfo;
ef87f48d 427 int len, error = 0;
deffea2e
JS
428 __va_list ap;
429
430 __va_start(ap, so);
431 oi = __va_arg(ap, struct pr_output_info *);
432 __va_end(ap);
984263bc 433
ef87f48d 434#define gotoerr(e) { error = e; goto flush;}
590b8cd4
JH
435
436 if (m == NULL ||
437 (m->m_len < sizeof(long) &&
438 (m = m_pullup(m, sizeof(long))) == NULL))
984263bc 439 return (ENOBUFS);
ef87f48d 440 if (!(m->m_flags & M_PKTHDR))
984263bc
MD
441 panic("route_output");
442 len = m->m_pkthdr.len;
590b8cd4 443 if (len < sizeof(struct rt_msghdr) ||
984263bc 444 len != mtod(m, struct rt_msghdr *)->rtm_msglen) {
590b8cd4 445 rtinfo.rti_dst = NULL;
ef87f48d 446 gotoerr(EINVAL);
984263bc 447 }
efda3bd0 448 rtm = kmalloc(len, M_RTABLE, M_INTWAIT | M_NULLOK);
ef87f48d 449 if (rtm == NULL) {
590b8cd4 450 rtinfo.rti_dst = NULL;
ef87f48d 451 gotoerr(ENOBUFS);
984263bc
MD
452 }
453 m_copydata(m, 0, len, (caddr_t)rtm);
454 if (rtm->rtm_version != RTM_VERSION) {
590b8cd4 455 rtinfo.rti_dst = NULL;
ef87f48d 456 gotoerr(EPROTONOSUPPORT);
984263bc 457 }
47e78ae8 458 rtm->rtm_pid = oi->p_pid;
590b8cd4
JH
459 bzero(&rtinfo, sizeof(struct rt_addrinfo));
460 rtinfo.rti_addrs = rtm->rtm_addrs;
461 if (rt_xaddrs((char *)(rtm + 1), (char *)rtm + len, &rtinfo) != 0) {
462 rtinfo.rti_dst = NULL;
ef87f48d 463 gotoerr(EINVAL);
984263bc 464 }
590b8cd4
JH
465 rtinfo.rti_flags = rtm->rtm_flags;
466 if (rtinfo.rti_dst == NULL || rtinfo.rti_dst->sa_family >= AF_MAX ||
467 (rtinfo.rti_gateway && rtinfo.rti_gateway->sa_family >= AF_MAX))
ef87f48d
JH
468 gotoerr(EINVAL);
469
590b8cd4
JH
470 if (rtinfo.rti_genmask != NULL) {
471 struct radix_node *n;
ef87f48d 472
590b8cd4
JH
473#define clen(s) (*(u_char *)(s))
474 n = rn_addmask((char *)rtinfo.rti_genmask, TRUE, 1);
475 if (n != NULL &&
476 rtinfo.rti_genmask->sa_len >= clen(n->rn_key) &&
477 bcmp((char *)rtinfo.rti_genmask + 1,
478 (char *)n->rn_key + 1, clen(n->rn_key) - 1) == 0)
479 rtinfo.rti_genmask = (struct sockaddr *)n->rn_key;
984263bc 480 else
ef87f48d 481 gotoerr(ENOBUFS);
984263bc
MD
482 }
483
484 /*
485 * Verify that the caller has the appropriate privilege; RTM_GET
486 * is the only operation the non-superuser is allowed.
487 */
dadab5e9 488 if (rtm->rtm_type != RTM_GET && suser_cred(so->so_cred, 0) != 0)
ef87f48d 489 gotoerr(EPERM);
984263bc
MD
490
491 switch (rtm->rtm_type) {
984263bc 492 case RTM_ADD:
ecdefdda
MD
493 if (rtinfo.rti_gateway == NULL) {
494 error = EINVAL;
495 } else {
496 error = rtrequest1_global(RTM_ADD, &rtinfo,
497 route_output_add_callback, rtm);
984263bc
MD
498 }
499 break;
984263bc 500 case RTM_DELETE:
ecdefdda
MD
501 /*
502 * note: &rtm passed as argument so 'rtm' can be replaced.
503 */
504 error = rtrequest1_global(RTM_DELETE, &rtinfo,
505 route_output_delete_callback, &rtm);
984263bc 506 break;
984263bc 507 case RTM_GET:
ecdefdda
MD
508 rnh = rt_tables[mycpuid][rtinfo.rti_dst->sa_family];
509 if (rnh == NULL) {
510 error = EAFNOSUPPORT;
511 break;
512 }
590b8cd4
JH
513 rt = (struct rtentry *)
514 rnh->rnh_lookup((char *)rtinfo.rti_dst,
515 (char *)rtinfo.rti_netmask, rnh);
ecdefdda
MD
516 if (rt == NULL) {
517 error = ESRCH;
984263bc
MD
518 break;
519 }
ecdefdda
MD
520 rt->rt_refcnt++;
521 if (fillrtmsg(&rtm, rt, &rtinfo) != 0)
522 gotoerr(ENOBUFS);
523 --rt->rt_refcnt;
524 break;
525 case RTM_CHANGE:
526 error = rtrequest1_global(RTM_GET, &rtinfo,
527 route_output_change_callback, rtm);
528 break;
529 case RTM_LOCK:
530 error = rtrequest1_global(RTM_GET, &rtinfo,
531 route_output_lock_callback, rtm);
590b8cd4 532 break;
984263bc 533 default:
ecdefdda
MD
534 error = EOPNOTSUPP;
535 break;
984263bc
MD
536 }
537
538flush:
590b8cd4
JH
539 if (rtm != NULL) {
540 if (error != 0)
984263bc
MD
541 rtm->rtm_errno = error;
542 else
543 rtm->rtm_flags |= RTF_DONE;
544 }
ecdefdda 545
984263bc
MD
546 /*
547 * Check to see if we don't want our own messages.
548 */
ef87f48d 549 if (!(so->so_options & SO_USELOOPBACK)) {
984263bc 550 if (route_cb.any_count <= 1) {
590b8cd4 551 if (rtm != NULL)
efda3bd0 552 kfree(rtm, M_RTABLE);
984263bc
MD
553 m_freem(m);
554 return (error);
555 }
556 /* There is another listener, so construct message */
557 rp = sotorawcb(so);
558 }
590b8cd4 559 if (rtm != NULL) {
984263bc
MD
560 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
561 if (m->m_pkthdr.len < rtm->rtm_msglen) {
562 m_freem(m);
563 m = NULL;
564 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
565 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
efda3bd0 566 kfree(rtm, M_RTABLE);
984263bc 567 }
ef87f48d 568 if (rp != NULL)
984263bc 569 rp->rcb_proto.sp_family = 0; /* Avoid us */
ef87f48d 570 if (m != NULL)
590b8cd4 571 rts_input(m, familyof(rtinfo.rti_dst));
ef87f48d 572 if (rp != NULL)
984263bc 573 rp->rcb_proto.sp_family = PF_ROUTE;
984263bc
MD
574 return (error);
575}
576
ecdefdda
MD
577static void
578route_output_add_callback(int cmd, int error, struct rt_addrinfo *rtinfo,
579 struct rtentry *rt, void *arg)
580{
581 struct rt_msghdr *rtm = arg;
582
583 if (error == 0 && rt != NULL) {
584 rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
585 &rt->rt_rmx);
586 rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
587 rt->rt_rmx.rmx_locks |=
588 (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
589 rt->rt_genmask = rtinfo->rti_genmask;
590 }
591}
592
593static void
594route_output_delete_callback(int cmd, int error, struct rt_addrinfo *rtinfo,
595 struct rtentry *rt, void *arg)
596{
597 struct rt_msghdr **rtm = arg;
598
599 if (error == 0 && rt) {
600 ++rt->rt_refcnt;
601 if (fillrtmsg(rtm, rt, rtinfo) != 0) {
602 error = ENOBUFS;
603 /* XXX no way to return the error */
604 }
605 --rt->rt_refcnt;
606 }
607}
608
609static void
610route_output_change_callback(int cmd, int error, struct rt_addrinfo *rtinfo,
611 struct rtentry *rt, void *arg)
612{
613 struct rt_msghdr *rtm = arg;
614 struct ifaddr *ifa;
615
616 if (error)
617 goto done;
618
619 /*
620 * new gateway could require new ifaddr, ifp;
621 * flags may also be different; ifp may be specified
622 * by ll sockaddr when protocol address is ambiguous
623 */
624 if (((rt->rt_flags & RTF_GATEWAY) && rtinfo->rti_gateway != NULL) ||
625 rtinfo->rti_ifpaddr != NULL || (rtinfo->rti_ifaaddr != NULL &&
626 sa_equal(rtinfo->rti_ifaaddr, rt->rt_ifa->ifa_addr))
627 ) {
628 error = rt_getifa(rtinfo);
629 if (error != 0)
630 goto done;
631 }
632 if (rtinfo->rti_gateway != NULL) {
633 error = rt_setgate(rt, rt_key(rt), rtinfo->rti_gateway);
634 if (error != 0)
635 goto done;
636 }
637 if ((ifa = rtinfo->rti_ifa) != NULL) {
638 struct ifaddr *oifa = rt->rt_ifa;
639
640 if (oifa != ifa) {
641 if (oifa && oifa->ifa_rtrequest)
642 oifa->ifa_rtrequest(RTM_DELETE, rt, rtinfo);
643 IFAFREE(rt->rt_ifa);
644 IFAREF(ifa);
645 rt->rt_ifa = ifa;
646 rt->rt_ifp = rtinfo->rti_ifp;
647 }
648 }
649 rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx, &rt->rt_rmx);
650 if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
651 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, rtinfo);
652 if (rtinfo->rti_genmask != NULL)
653 rt->rt_genmask = rtinfo->rti_genmask;
654done:
655 /* XXX no way to return error */
656 ;
657}
658
659static void
660route_output_lock_callback(int cmd, int error, struct rt_addrinfo *rtinfo,
661 struct rtentry *rt, void *arg)
662{
663 struct rt_msghdr *rtm = arg;
664
665 rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
666 rt->rt_rmx.rmx_locks |=
667 (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
668}
669
984263bc 670static void
ef87f48d 671rt_setmetrics(u_long which, struct rt_metrics *in, struct rt_metrics *out)
984263bc 672{
2e9572df
JH
673#define setmetric(flag, elt) if (which & (flag)) out->elt = in->elt;
674 setmetric(RTV_RPIPE, rmx_recvpipe);
675 setmetric(RTV_SPIPE, rmx_sendpipe);
676 setmetric(RTV_SSTHRESH, rmx_ssthresh);
677 setmetric(RTV_RTT, rmx_rtt);
678 setmetric(RTV_RTTVAR, rmx_rttvar);
679 setmetric(RTV_HOPCOUNT, rmx_hopcount);
680 setmetric(RTV_MTU, rmx_mtu);
681 setmetric(RTV_EXPIRE, rmx_expire);
682#undef setmetric
984263bc
MD
683}
684
685#define ROUNDUP(a) \
686 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
984263bc 687
984263bc
MD
688/*
689 * Extract the addresses of the passed sockaddrs.
690 * Do a little sanity checking so as to avoid bad memory references.
691 * This data is derived straight from userland.
692 */
693static int
ef87f48d 694rt_xaddrs(char *cp, char *cplim, struct rt_addrinfo *rtinfo)
984263bc 695{
82ed7fc2
RG
696 struct sockaddr *sa;
697 int i;
984263bc
MD
698
699 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
700 if ((rtinfo->rti_addrs & (1 << i)) == 0)
701 continue;
702 sa = (struct sockaddr *)cp;
703 /*
704 * It won't fit.
705 */
0c3c561c 706 if ((cp + sa->sa_len) > cplim) {
984263bc
MD
707 return (EINVAL);
708 }
709
710 /*
ef87f48d 711 * There are no more... Quit now.
984263bc 712 * If there are more bits, they are in error.
ef87f48d 713 * I've seen this. route(1) can evidently generate these.
984263bc 714 * This causes kernel to core dump.
ef87f48d 715 * For compatibility, if we see this, point to a safe address.
984263bc
MD
716 */
717 if (sa->sa_len == 0) {
590b8cd4
JH
718 static struct sockaddr sa_zero = {
719 sizeof sa_zero, AF_INET,
720 };
721
984263bc
MD
722 rtinfo->rti_info[i] = &sa_zero;
723 return (0); /* should be EINVAL but for compat */
724 }
725
ef87f48d 726 /* Accept the sockaddr. */
984263bc 727 rtinfo->rti_info[i] = sa;
590b8cd4 728 cp += ROUNDUP(sa->sa_len);
984263bc
MD
729 }
730 return (0);
731}
732
590b8cd4
JH
733static int
734rt_msghdrsize(int type)
984263bc 735{
984263bc 736 switch (type) {
984263bc
MD
737 case RTM_DELADDR:
738 case RTM_NEWADDR:
590b8cd4 739 return sizeof(struct ifa_msghdr);
984263bc
MD
740 case RTM_DELMADDR:
741 case RTM_NEWMADDR:
590b8cd4 742 return sizeof(struct ifma_msghdr);
984263bc 743 case RTM_IFINFO:
590b8cd4 744 return sizeof(struct if_msghdr);
984263bc 745 case RTM_IFANNOUNCE:
841ab66c 746 case RTM_IEEE80211:
590b8cd4 747 return sizeof(struct if_announcemsghdr);
984263bc 748 default:
590b8cd4 749 return sizeof(struct rt_msghdr);
984263bc 750 }
590b8cd4
JH
751}
752
753static int
754rt_msgsize(int type, struct rt_addrinfo *rtinfo)
755{
756 int len, i;
757
758 len = rt_msghdrsize(type);
759 for (i = 0; i < RTAX_MAX; i++) {
760 if (rtinfo->rti_info[i] != NULL)
761 len += ROUNDUP(rtinfo->rti_info[i]->sa_len);
984263bc 762 }
590b8cd4
JH
763 len = ALIGN(len);
764 return len;
765}
766
767/*
768 * Build a routing message in a buffer.
769 * Copy the addresses in the rtinfo->rti_info[] sockaddr array
770 * to the end of the buffer after the message header.
771 *
772 * Set the rtinfo->rti_addrs bitmask of addresses present in rtinfo->rti_info[].
773 * This side-effect can be avoided if we reorder the addrs bitmask field in all
774 * the route messages to line up so we can set it here instead of back in the
775 * calling routine.
776 */
777static void
778rt_msg_buffer(int type, struct rt_addrinfo *rtinfo, void *buf, int msglen)
779{
780 struct rt_msghdr *rtm;
781 char *cp;
782 int dlen, i;
783
784 rtm = (struct rt_msghdr *) buf;
785 rtm->rtm_version = RTM_VERSION;
786 rtm->rtm_type = type;
787 rtm->rtm_msglen = msglen;
788
789 cp = (char *)buf + rt_msghdrsize(type);
790 rtinfo->rti_addrs = 0;
984263bc 791 for (i = 0; i < RTAX_MAX; i++) {
590b8cd4
JH
792 struct sockaddr *sa;
793
984263bc
MD
794 if ((sa = rtinfo->rti_info[i]) == NULL)
795 continue;
796 rtinfo->rti_addrs |= (1 << i);
797 dlen = ROUNDUP(sa->sa_len);
590b8cd4
JH
798 bcopy(sa, cp, dlen);
799 cp += dlen;
984263bc 800 }
984263bc
MD
801}
802
590b8cd4
JH
803/*
804 * Build a routing message in a mbuf chain.
805 * Copy the addresses in the rtinfo->rti_info[] sockaddr array
806 * to the end of the mbuf after the message header.
807 *
808 * Set the rtinfo->rti_addrs bitmask of addresses present in rtinfo->rti_info[].
809 * This side-effect can be avoided if we reorder the addrs bitmask field in all
810 * the route messages to line up so we can set it here instead of back in the
811 * calling routine.
812 */
813static struct mbuf *
814rt_msg_mbuf(int type, struct rt_addrinfo *rtinfo)
984263bc 815{
590b8cd4
JH
816 struct mbuf *m;
817 struct rt_msghdr *rtm;
818 int hlen, len;
82ed7fc2 819 int i;
984263bc 820
590b8cd4
JH
821 hlen = rt_msghdrsize(type);
822 KASSERT(hlen <= MCLBYTES, ("rt_msg_mbuf: hlen %d doesn't fit", hlen));
984263bc 823
50503f0f 824 m = m_getl(hlen, MB_DONTWAIT, MT_DATA, M_PKTHDR, NULL);
590b8cd4
JH
825 if (m == NULL)
826 return (NULL);
590b8cd4
JH
827 m->m_pkthdr.len = m->m_len = hlen;
828 m->m_pkthdr.rcvif = NULL;
829 rtinfo->rti_addrs = 0;
830 len = hlen;
984263bc 831 for (i = 0; i < RTAX_MAX; i++) {
82ed7fc2 832 struct sockaddr *sa;
590b8cd4 833 int dlen;
984263bc 834
ef87f48d 835 if ((sa = rtinfo->rti_info[i]) == NULL)
984263bc
MD
836 continue;
837 rtinfo->rti_addrs |= (1 << i);
838 dlen = ROUNDUP(sa->sa_len);
590b8cd4 839 m_copyback(m, len, dlen, (caddr_t)sa); /* can grow mbuf chain */
984263bc
MD
840 len += dlen;
841 }
590b8cd4
JH
842 if (m->m_pkthdr.len != len) { /* one of the m_copyback() calls failed */
843 m_freem(m);
844 return (NULL);
984263bc 845 }
590b8cd4
JH
846 rtm = mtod(m, struct rt_msghdr *);
847 bzero(rtm, hlen);
848 rtm->rtm_msglen = len;
849 rtm->rtm_version = RTM_VERSION;
850 rtm->rtm_type = type;
851 return (m);
984263bc
MD
852}
853
854/*
855 * This routine is called to generate a message from the routing
f23061d4 856 * socket indicating that a redirect has occurred, a routing lookup
984263bc
MD
857 * has failed, or that a protocol has detected timeouts to a particular
858 * destination.
859 */
860void
f23061d4 861rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
984263bc 862{
0c3c561c 863 struct sockaddr *dst = rtinfo->rti_info[RTAX_DST];
82ed7fc2
RG
864 struct rt_msghdr *rtm;
865 struct mbuf *m;
984263bc
MD
866
867 if (route_cb.any_count == 0)
868 return;
590b8cd4 869 m = rt_msg_mbuf(type, rtinfo);
ef87f48d 870 if (m == NULL)
984263bc
MD
871 return;
872 rtm = mtod(m, struct rt_msghdr *);
873 rtm->rtm_flags = RTF_DONE | flags;
874 rtm->rtm_errno = error;
875 rtm->rtm_addrs = rtinfo->rti_addrs;
590b8cd4 876 rts_input(m, familyof(dst));
984263bc
MD
877}
878
f3ed2586
JH
879void
880rt_dstmsg(int type, struct sockaddr *dst, int error)
881{
882 struct rt_msghdr *rtm;
883 struct rt_addrinfo addrs;
884 struct mbuf *m;
885
886 if (route_cb.any_count == 0)
887 return;
888 bzero(&addrs, sizeof(struct rt_addrinfo));
889 addrs.rti_info[RTAX_DST] = dst;
590b8cd4 890 m = rt_msg_mbuf(type, &addrs);
f3ed2586
JH
891 if (m == NULL)
892 return;
893 rtm = mtod(m, struct rt_msghdr *);
894 rtm->rtm_flags = RTF_DONE;
895 rtm->rtm_errno = error;
896 rtm->rtm_addrs = addrs.rti_addrs;
590b8cd4 897 rts_input(m, familyof(dst));
f3ed2586
JH
898}
899
984263bc
MD
900/*
901 * This routine is called to generate a message from the routing
902 * socket indicating that the status of a network interface has changed.
903 */
904void
0c3c561c 905rt_ifmsg(struct ifnet *ifp)
984263bc 906{
82ed7fc2 907 struct if_msghdr *ifm;
984263bc 908 struct mbuf *m;
590b8cd4 909 struct rt_addrinfo rtinfo;
984263bc
MD
910
911 if (route_cb.any_count == 0)
912 return;
590b8cd4
JH
913 bzero(&rtinfo, sizeof(struct rt_addrinfo));
914 m = rt_msg_mbuf(RTM_IFINFO, &rtinfo);
ef87f48d 915 if (m == NULL)
984263bc
MD
916 return;
917 ifm = mtod(m, struct if_msghdr *);
918 ifm->ifm_index = ifp->if_index;
9c095379 919 ifm->ifm_flags = ifp->if_flags;
984263bc 920 ifm->ifm_data = ifp->if_data;
590b8cd4
JH
921 ifm->ifm_addrs = 0;
922 rts_input(m, 0);
984263bc
MD
923}
924
372316d9
JH
925static void
926rt_ifamsg(int cmd, struct ifaddr *ifa)
927{
928 struct ifa_msghdr *ifam;
590b8cd4 929 struct rt_addrinfo rtinfo;
372316d9 930 struct mbuf *m;
372316d9
JH
931 struct ifnet *ifp = ifa->ifa_ifp;
932
590b8cd4
JH
933 bzero(&rtinfo, sizeof(struct rt_addrinfo));
934 rtinfo.rti_ifaaddr = ifa->ifa_addr;
935 rtinfo.rti_ifpaddr = TAILQ_FIRST(&ifp->if_addrhead)->ifa_addr;
936 rtinfo.rti_netmask = ifa->ifa_netmask;
937 rtinfo.rti_bcastaddr = ifa->ifa_dstaddr;
f23061d4 938
590b8cd4 939 m = rt_msg_mbuf(cmd, &rtinfo);
f23061d4 940 if (m == NULL)
372316d9 941 return;
f23061d4 942
372316d9
JH
943 ifam = mtod(m, struct ifa_msghdr *);
944 ifam->ifam_index = ifp->if_index;
945 ifam->ifam_metric = ifa->ifa_metric;
946 ifam->ifam_flags = ifa->ifa_flags;
590b8cd4 947 ifam->ifam_addrs = rtinfo.rti_addrs;
f23061d4 948
590b8cd4 949 rts_input(m, familyof(ifa->ifa_addr));
372316d9
JH
950}
951
f3ed2586
JH
952void
953rt_rtmsg(int cmd, struct rtentry *rt, struct ifnet *ifp, int error)
372316d9
JH
954{
955 struct rt_msghdr *rtm;
590b8cd4 956 struct rt_addrinfo rtinfo;
372316d9 957 struct mbuf *m;
f3ed2586 958 struct sockaddr *dst;
372316d9
JH
959
960 if (rt == NULL)
961 return;
f23061d4 962
590b8cd4
JH
963 bzero(&rtinfo, sizeof(struct rt_addrinfo));
964 rtinfo.rti_dst = dst = rt_key(rt);
965 rtinfo.rti_gateway = rt->rt_gateway;
966 rtinfo.rti_netmask = rt_mask(rt);
f3ed2586 967 if (ifp != NULL)
590b8cd4
JH
968 rtinfo.rti_ifpaddr = TAILQ_FIRST(&ifp->if_addrhead)->ifa_addr;
969 rtinfo.rti_ifaaddr = rt->rt_ifa->ifa_addr;
f23061d4 970
590b8cd4 971 m = rt_msg_mbuf(cmd, &rtinfo);
f23061d4 972 if (m == NULL)
372316d9 973 return;
f23061d4 974
372316d9 975 rtm = mtod(m, struct rt_msghdr *);
f3ed2586
JH
976 if (ifp != NULL)
977 rtm->rtm_index = ifp->if_index;
372316d9
JH
978 rtm->rtm_flags |= rt->rt_flags;
979 rtm->rtm_errno = error;
590b8cd4 980 rtm->rtm_addrs = rtinfo.rti_addrs;
f23061d4 981
590b8cd4 982 rts_input(m, familyof(dst));
372316d9
JH
983}
984
984263bc
MD
985/*
986 * This is called to generate messages from the routing socket
987 * indicating a network interface has had addresses associated with it.
988 * if we ever reverse the logic and replace messages TO the routing
989 * socket indicate a request to configure interfaces, then it will
990 * be unnecessary as the routing socket will automatically generate
991 * copies of it.
992 */
993void
0c3c561c 994rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt)
984263bc 995{
78812139
EN
996#ifdef SCTP
997 /*
998 * notify the SCTP stack
999 * this will only get called when an address is added/deleted
1000 * XXX pass the ifaddr struct instead if ifa->ifa_addr...
1001 */
1002 if (cmd == RTM_ADD)
1003 sctp_add_ip_address(ifa);
1004 else if (cmd == RTM_DELETE)
1005 sctp_delete_ip_address(ifa);
1006#endif /* SCTP */
1007
984263bc
MD
1008 if (route_cb.any_count == 0)
1009 return;
984263bc 1010
372316d9
JH
1011 if (cmd == RTM_ADD) {
1012 rt_ifamsg(RTM_NEWADDR, ifa);
f3ed2586 1013 rt_rtmsg(RTM_ADD, rt, ifa->ifa_ifp, error);
372316d9
JH
1014 } else {
1015 KASSERT((cmd == RTM_DELETE), ("unknown cmd %d", cmd));
f3ed2586 1016 rt_rtmsg(RTM_DELETE, rt, ifa->ifa_ifp, error);
372316d9 1017 rt_ifamsg(RTM_DELADDR, ifa);
984263bc
MD
1018 }
1019}
1020
1021/*
1022 * This is the analogue to the rt_newaddrmsg which performs the same
1023 * function but for multicast group memberhips. This is easier since
1024 * there is no route state to worry about.
1025 */
1026void
0c3c561c 1027rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
984263bc 1028{
590b8cd4 1029 struct rt_addrinfo rtinfo;
ef87f48d 1030 struct mbuf *m = NULL;
984263bc
MD
1031 struct ifnet *ifp = ifma->ifma_ifp;
1032 struct ifma_msghdr *ifmam;
1033
1034 if (route_cb.any_count == 0)
1035 return;
1036
590b8cd4
JH
1037 bzero(&rtinfo, sizeof(struct rt_addrinfo));
1038 rtinfo.rti_ifaaddr = ifma->ifma_addr;
1782116d 1039 if (ifp != NULL && !TAILQ_EMPTY(&ifp->if_addrhead))
590b8cd4 1040 rtinfo.rti_ifpaddr = TAILQ_FIRST(&ifp->if_addrhead)->ifa_addr;
984263bc
MD
1041 /*
1042 * If a link-layer address is present, present it as a ``gateway''
1043 * (similarly to how ARP entries, e.g., are presented).
1044 */
590b8cd4 1045 rtinfo.rti_gateway = ifma->ifma_lladdr;
f23061d4 1046
590b8cd4 1047 m = rt_msg_mbuf(cmd, &rtinfo);
f23061d4 1048 if (m == NULL)
984263bc 1049 return;
f23061d4 1050
984263bc
MD
1051 ifmam = mtod(m, struct ifma_msghdr *);
1052 ifmam->ifmam_index = ifp->if_index;
590b8cd4 1053 ifmam->ifmam_addrs = rtinfo.rti_addrs;
f23061d4 1054
590b8cd4 1055 rts_input(m, familyof(ifma->ifma_addr));
984263bc
MD
1056}
1057
841ab66c
SZ
1058static struct mbuf *
1059rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1060 struct rt_addrinfo *info)
984263bc 1061{
590b8cd4 1062 struct if_announcemsghdr *ifan;
841ab66c 1063 struct mbuf *m;
984263bc
MD
1064
1065 if (route_cb.any_count == 0)
841ab66c 1066 return NULL;
f23061d4 1067
841ab66c
SZ
1068 bzero(info, sizeof(*info));
1069 m = rt_msg_mbuf(type, info);
984263bc 1070 if (m == NULL)
841ab66c 1071 return NULL;
f23061d4 1072
984263bc
MD
1073 ifan = mtod(m, struct if_announcemsghdr *);
1074 ifan->ifan_index = ifp->if_index;
5fe66e68 1075 strlcpy(ifan->ifan_name, ifp->if_xname, sizeof ifan->ifan_name);
984263bc 1076 ifan->ifan_what = what;
841ab66c
SZ
1077 return m;
1078}
1079
1080/*
1081 * This is called to generate routing socket messages indicating
1082 * IEEE80211 wireless events.
1083 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1084 */
1085void
1086rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
1087{
1088 struct rt_addrinfo info;
1089 struct mbuf *m;
f23061d4 1090
841ab66c
SZ
1091 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1092 if (m == NULL)
1093 return;
1094
1095 /*
1096 * Append the ieee80211 data. Try to stick it in the
1097 * mbuf containing the ifannounce msg; otherwise allocate
1098 * a new mbuf and append.
1099 *
1100 * NB: we assume m is a single mbuf.
1101 */
1102 if (data_len > M_TRAILINGSPACE(m)) {
1103 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
1104 if (n == NULL) {
1105 m_freem(m);
1106 return;
1107 }
1108 bcopy(data, mtod(n, void *), data_len);
1109 n->m_len = data_len;
1110 m->m_next = n;
1111 } else if (data_len > 0) {
1112 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
1113 m->m_len += data_len;
1114 }
1115 if (m->m_flags & M_PKTHDR)
1116 m->m_pkthdr.len += data_len;
1117 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
590b8cd4 1118 rts_input(m, 0);
841ab66c
SZ
1119}
1120
1121/*
1122 * This is called to generate routing socket messages indicating
1123 * network interface arrival and departure.
1124 */
1125void
1126rt_ifannouncemsg(struct ifnet *ifp, int what)
1127{
1128 struct rt_addrinfo addrinfo;
1129 struct mbuf *m;
1130
1131 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &addrinfo);
1132 if (m != NULL)
1133 rts_input(m, 0);
590b8cd4 1134}
f23061d4 1135
590b8cd4
JH
1136static int
1137resizewalkarg(struct walkarg *w, int len)
1138{
1139 void *newptr;
1140
efda3bd0 1141 newptr = kmalloc(len, M_RTABLE, M_INTWAIT | M_NULLOK);
590b8cd4
JH
1142 if (newptr == NULL)
1143 return (ENOMEM);
1144 if (w->w_tmem != NULL)
efda3bd0 1145 kfree(w->w_tmem, M_RTABLE);
590b8cd4
JH
1146 w->w_tmem = newptr;
1147 w->w_tmemsize = len;
1148 return (0);
1149}
984263bc
MD
1150
1151/*
1152 * This is used in dumping the kernel table via sysctl().
1153 */
1154int
590b8cd4 1155sysctl_dumpentry(struct radix_node *rn, void *vw)
984263bc 1156{
82ed7fc2
RG
1157 struct walkarg *w = vw;
1158 struct rtentry *rt = (struct rtentry *)rn;
590b8cd4
JH
1159 struct rt_addrinfo rtinfo;
1160 int error, msglen;
984263bc
MD
1161
1162 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1163 return 0;
f23061d4 1164
590b8cd4
JH
1165 bzero(&rtinfo, sizeof(struct rt_addrinfo));
1166 rtinfo.rti_dst = rt_key(rt);
1167 rtinfo.rti_gateway = rt->rt_gateway;
1168 rtinfo.rti_netmask = rt_mask(rt);
1169 rtinfo.rti_genmask = rt->rt_genmask;
ef87f48d 1170 if (rt->rt_ifp != NULL) {
590b8cd4 1171 rtinfo.rti_ifpaddr =
ef87f48d 1172 TAILQ_FIRST(&rt->rt_ifp->if_addrhead)->ifa_addr;
590b8cd4 1173 rtinfo.rti_ifaaddr = rt->rt_ifa->ifa_addr;
984263bc 1174 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
590b8cd4 1175 rtinfo.rti_bcastaddr = rt->rt_ifa->ifa_dstaddr;
984263bc 1176 }
590b8cd4
JH
1177 msglen = rt_msgsize(RTM_GET, &rtinfo);
1178 if (w->w_tmemsize < msglen && resizewalkarg(w, msglen) != 0)
1179 return (ENOMEM);
1180 rt_msg_buffer(RTM_GET, &rtinfo, w->w_tmem, msglen);
1181 if (w->w_req != NULL) {
8e63efec 1182 struct rt_msghdr *rtm = w->w_tmem;
984263bc
MD
1183
1184 rtm->rtm_flags = rt->rt_flags;
1185 rtm->rtm_use = rt->rt_use;
1186 rtm->rtm_rmx = rt->rt_rmx;
1187 rtm->rtm_index = rt->rt_ifp->if_index;
1188 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
590b8cd4
JH
1189 rtm->rtm_addrs = rtinfo.rti_addrs;
1190 error = SYSCTL_OUT(w->w_req, rtm, msglen);
984263bc
MD
1191 return (error);
1192 }
590b8cd4 1193 return (0);
984263bc
MD
1194}
1195
590b8cd4
JH
1196static int
1197sysctl_iflist(int af, struct walkarg *w)
984263bc 1198{
82ed7fc2
RG
1199 struct ifnet *ifp;
1200 struct ifaddr *ifa;
590b8cd4
JH
1201 struct rt_addrinfo rtinfo;
1202 int msglen, error;
984263bc 1203
590b8cd4 1204 bzero(&rtinfo, sizeof(struct rt_addrinfo));
984263bc
MD
1205 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1206 if (w->w_arg && w->w_arg != ifp->if_index)
1207 continue;
1208 ifa = TAILQ_FIRST(&ifp->if_addrhead);
590b8cd4
JH
1209 rtinfo.rti_ifpaddr = ifa->ifa_addr;
1210 msglen = rt_msgsize(RTM_IFINFO, &rtinfo);
1211 if (w->w_tmemsize < msglen && resizewalkarg(w, msglen) != 0)
1212 return (ENOMEM);
1213 rt_msg_buffer(RTM_IFINFO, &rtinfo, w->w_tmem, msglen);
1214 rtinfo.rti_ifpaddr = NULL;
ef87f48d 1215 if (w->w_req != NULL && w->w_tmem != NULL) {
8e63efec 1216 struct if_msghdr *ifm = w->w_tmem;
984263bc 1217
984263bc 1218 ifm->ifm_index = ifp->if_index;
9c095379 1219 ifm->ifm_flags = ifp->if_flags;
984263bc 1220 ifm->ifm_data = ifp->if_data;
590b8cd4
JH
1221 ifm->ifm_addrs = rtinfo.rti_addrs;
1222 error = SYSCTL_OUT(w->w_req, ifm, msglen);
984263bc
MD
1223 if (error)
1224 return (error);
1225 }
ef87f48d 1226 while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != NULL) {
984263bc
MD
1227 if (af && af != ifa->ifa_addr->sa_family)
1228 continue;
590b8cd4 1229 if (curproc->p_ucred->cr_prison &&
87de5057 1230 prison_if(curproc->p_ucred, ifa->ifa_addr))
984263bc 1231 continue;
590b8cd4
JH
1232 rtinfo.rti_ifaaddr = ifa->ifa_addr;
1233 rtinfo.rti_netmask = ifa->ifa_netmask;
1234 rtinfo.rti_bcastaddr = ifa->ifa_dstaddr;
1235 msglen = rt_msgsize(RTM_NEWADDR, &rtinfo);
1236 if (w->w_tmemsize < msglen &&
1237 resizewalkarg(w, msglen) != 0)
1238 return (ENOMEM);
1239 rt_msg_buffer(RTM_NEWADDR, &rtinfo, w->w_tmem, msglen);
1240 if (w->w_req != NULL) {
8e63efec 1241 struct ifa_msghdr *ifam = w->w_tmem;
984263bc 1242
984263bc
MD
1243 ifam->ifam_index = ifa->ifa_ifp->if_index;
1244 ifam->ifam_flags = ifa->ifa_flags;
1245 ifam->ifam_metric = ifa->ifa_metric;
590b8cd4
JH
1246 ifam->ifam_addrs = rtinfo.rti_addrs;
1247 error = SYSCTL_OUT(w->w_req, w->w_tmem, msglen);
984263bc
MD
1248 if (error)
1249 return (error);
1250 }
1251 }
590b8cd4
JH
1252 rtinfo.rti_netmask = NULL;
1253 rtinfo.rti_ifaaddr = NULL;
1254 rtinfo.rti_bcastaddr = NULL;
984263bc
MD
1255 }
1256 return (0);
1257}
1258
1259static int
1260sysctl_rtsock(SYSCTL_HANDLER_ARGS)
1261{
1262 int *name = (int *)arg1;
1263 u_int namelen = arg2;
82ed7fc2 1264 struct radix_node_head *rnh;
a49aa710 1265 int i, error = EINVAL;
ecdefdda 1266 int origcpu;
984263bc
MD
1267 u_char af;
1268 struct walkarg w;
1269
1270 name ++;
1271 namelen--;
1272 if (req->newptr)
1273 return (EPERM);
ecdefdda 1274 if (namelen != 3 && namelen != 4)
984263bc
MD
1275 return (EINVAL);
1276 af = name[0];
5fe66e68 1277 bzero(&w, sizeof w);
984263bc
MD
1278 w.w_op = name[1];
1279 w.w_arg = name[2];
1280 w.w_req = req;
1281
ecdefdda
MD
1282 /*
1283 * Optional third argument specifies cpu, used primarily for
1284 * debugging the route table.
1285 */
1286 if (namelen == 4) {
1287 if (name[3] < 0 || name[3] >= ncpus)
1288 return (EINVAL);
1289 origcpu = mycpuid;
1290 lwkt_migratecpu(name[3]);
1291 } else {
1292 origcpu = -1;
1293 }
a49aa710 1294 crit_enter();
984263bc 1295 switch (w.w_op) {
984263bc
MD
1296 case NET_RT_DUMP:
1297 case NET_RT_FLAGS:
1298 for (i = 1; i <= AF_MAX; i++)
ecdefdda
MD
1299 if ((rnh = rt_tables[mycpuid][i]) &&
1300 (af == 0 || af == i) &&
984263bc 1301 (error = rnh->rnh_walktree(rnh,
f23061d4 1302 sysctl_dumpentry, &w)))
984263bc
MD
1303 break;
1304 break;
1305
1306 case NET_RT_IFLIST:
1307 error = sysctl_iflist(af, &w);
1308 }
a49aa710 1309 crit_exit();
590b8cd4 1310 if (w.w_tmem != NULL)
efda3bd0 1311 kfree(w.w_tmem, M_RTABLE);
ecdefdda
MD
1312 if (origcpu >= 0)
1313 lwkt_migratecpu(origcpu);
984263bc
MD
1314 return (error);
1315}
1316
1317SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
1318
1319/*
1320 * Definitions of protocols supported in the ROUTE domain.
1321 */
1322
ba4257b8 1323static struct domain routedomain; /* or at least forward */
984263bc
MD
1324
1325static struct protosw routesw[] = {
1326{ SOCK_RAW, &routedomain, 0, PR_ATOMIC|PR_ADDR,
1327 0, route_output, raw_ctlinput, 0,
9eeaa8a9 1328 cpu0_soport,
984263bc
MD
1329 raw_init, 0, 0, 0,
1330 &route_usrreqs
1331}
1332};
1333
ba4257b8 1334static struct domain routedomain = {
9c70fe43
JH
1335 PF_ROUTE, "route", NULL, NULL, NULL,
1336 routesw, &routesw[(sizeof routesw)/(sizeof routesw[0])],
1337};
984263bc
MD
1338
1339DOMAIN_SET(route);