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