Deleted Added
full compact
rtsock.c (266860) rtsock.c (269243)
1/*-
2 * Copyright (c) 1988, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * @(#)rtsock.c 8.7 (Berkeley) 10/12/95
1/*-
2 * Copyright (c) 1988, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * @(#)rtsock.c 8.7 (Berkeley) 10/12/95
30 * $FreeBSD: head/sys/net/rtsock.c 266860 2014-05-29 21:03:49Z asomers $
30 * $FreeBSD: head/sys/net/rtsock.c 269243 2014-07-29 15:01:29Z glebius $
31 */
32#include "opt_compat.h"
33#include "opt_mpath.h"
34#include "opt_inet.h"
35#include "opt_inet6.h"
36
37#include <sys/param.h>
38#include <sys/jail.h>
39#include <sys/kernel.h>
40#include <sys/domain.h>
41#include <sys/lock.h>
42#include <sys/malloc.h>
43#include <sys/mbuf.h>
44#include <sys/priv.h>
45#include <sys/proc.h>
46#include <sys/protosw.h>
47#include <sys/rwlock.h>
48#include <sys/signalvar.h>
49#include <sys/socket.h>
50#include <sys/socketvar.h>
51#include <sys/sysctl.h>
52#include <sys/systm.h>
53
54#include <net/if.h>
55#include <net/if_var.h>
56#include <net/if_dl.h>
57#include <net/if_llatbl.h>
58#include <net/if_types.h>
59#include <net/netisr.h>
60#include <net/raw_cb.h>
61#include <net/route.h>
62#include <net/vnet.h>
63
64#include <netinet/in.h>
65#include <netinet/if_ether.h>
66#include <netinet/ip_carp.h>
67#ifdef INET6
68#include <netinet6/ip6_var.h>
69#include <netinet6/scope6_var.h>
70#endif
71
72#ifdef COMPAT_FREEBSD32
73#include <sys/mount.h>
74#include <compat/freebsd32/freebsd32.h>
75
76struct if_msghdr32 {
77 uint16_t ifm_msglen;
78 uint8_t ifm_version;
79 uint8_t ifm_type;
80 int32_t ifm_addrs;
81 int32_t ifm_flags;
82 uint16_t ifm_index;
83 struct if_data ifm_data;
84};
85
86struct if_msghdrl32 {
87 uint16_t ifm_msglen;
88 uint8_t ifm_version;
89 uint8_t ifm_type;
90 int32_t ifm_addrs;
91 int32_t ifm_flags;
92 uint16_t ifm_index;
93 uint16_t _ifm_spare1;
94 uint16_t ifm_len;
95 uint16_t ifm_data_off;
96 struct if_data ifm_data;
97};
98
99struct ifa_msghdrl32 {
100 uint16_t ifam_msglen;
101 uint8_t ifam_version;
102 uint8_t ifam_type;
103 int32_t ifam_addrs;
104 int32_t ifam_flags;
105 uint16_t ifam_index;
106 uint16_t _ifam_spare1;
107 uint16_t ifam_len;
108 uint16_t ifam_data_off;
109 int32_t ifam_metric;
110 struct if_data ifam_data;
111};
112#endif /* COMPAT_FREEBSD32 */
113
114MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
115
116/* NB: these are not modified */
117static struct sockaddr route_src = { 2, PF_ROUTE, };
118static struct sockaddr sa_zero = { sizeof(sa_zero), AF_INET, };
119
120/* These are external hooks for CARP. */
121int (*carp_get_vhid_p)(struct ifaddr *);
122
123/*
124 * Used by rtsock/raw_input callback code to decide whether to filter the update
125 * notification to a socket bound to a particular FIB.
126 */
127#define RTS_FILTER_FIB M_PROTO8
128
129typedef struct {
130 int ip_count; /* attached w/ AF_INET */
131 int ip6_count; /* attached w/ AF_INET6 */
132 int any_count; /* total attached */
133} route_cb_t;
134static VNET_DEFINE(route_cb_t, route_cb);
135#define V_route_cb VNET(route_cb)
136
137struct mtx rtsock_mtx;
138MTX_SYSINIT(rtsock, &rtsock_mtx, "rtsock route_cb lock", MTX_DEF);
139
140#define RTSOCK_LOCK() mtx_lock(&rtsock_mtx)
141#define RTSOCK_UNLOCK() mtx_unlock(&rtsock_mtx)
142#define RTSOCK_LOCK_ASSERT() mtx_assert(&rtsock_mtx, MA_OWNED)
143
144static SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RD, 0, "");
145
146struct walkarg {
147 int w_tmemsize;
148 int w_op, w_arg;
149 caddr_t w_tmem;
150 struct sysctl_req *w_req;
151};
152
153static void rts_input(struct mbuf *m);
154static struct mbuf *rtsock_msg_mbuf(int type, struct rt_addrinfo *rtinfo);
155static int rtsock_msg_buffer(int type, struct rt_addrinfo *rtinfo,
156 struct walkarg *w, int *plen);
157static int rt_xaddrs(caddr_t cp, caddr_t cplim,
158 struct rt_addrinfo *rtinfo);
159static int sysctl_dumpentry(struct radix_node *rn, void *vw);
160static int sysctl_iflist(int af, struct walkarg *w);
161static int sysctl_ifmalist(int af, struct walkarg *w);
162static int route_output(struct mbuf *m, struct socket *so);
163static void rt_getmetrics(const struct rtentry *rt, struct rt_metrics *out);
164static void rt_dispatch(struct mbuf *, sa_family_t);
165static struct sockaddr *rtsock_fix_netmask(struct sockaddr *dst,
166 struct sockaddr *smask, struct sockaddr_storage *dmask);
167
168static struct netisr_handler rtsock_nh = {
169 .nh_name = "rtsock",
170 .nh_handler = rts_input,
171 .nh_proto = NETISR_ROUTE,
172 .nh_policy = NETISR_POLICY_SOURCE,
173};
174
175static int
176sysctl_route_netisr_maxqlen(SYSCTL_HANDLER_ARGS)
177{
178 int error, qlimit;
179
180 netisr_getqlimit(&rtsock_nh, &qlimit);
181 error = sysctl_handle_int(oidp, &qlimit, 0, req);
182 if (error || !req->newptr)
183 return (error);
184 if (qlimit < 1)
185 return (EINVAL);
186 return (netisr_setqlimit(&rtsock_nh, qlimit));
187}
188SYSCTL_PROC(_net_route, OID_AUTO, netisr_maxqlen, CTLTYPE_INT|CTLFLAG_RW,
189 0, 0, sysctl_route_netisr_maxqlen, "I",
190 "maximum routing socket dispatch queue length");
191
192static void
193rts_init(void)
194{
195 int tmp;
196
197 if (TUNABLE_INT_FETCH("net.route.netisr_maxqlen", &tmp))
198 rtsock_nh.nh_qlimit = tmp;
199 netisr_register(&rtsock_nh);
200}
201SYSINIT(rtsock, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, rts_init, 0);
202
203static int
204raw_input_rts_cb(struct mbuf *m, struct sockproto *proto, struct sockaddr *src,
205 struct rawcb *rp)
206{
207 int fibnum;
208
209 KASSERT(m != NULL, ("%s: m is NULL", __func__));
210 KASSERT(proto != NULL, ("%s: proto is NULL", __func__));
211 KASSERT(rp != NULL, ("%s: rp is NULL", __func__));
212
213 /* No filtering requested. */
214 if ((m->m_flags & RTS_FILTER_FIB) == 0)
215 return (0);
216
217 /* Check if it is a rts and the fib matches the one of the socket. */
218 fibnum = M_GETFIB(m);
219 if (proto->sp_family != PF_ROUTE ||
220 rp->rcb_socket == NULL ||
221 rp->rcb_socket->so_fibnum == fibnum)
222 return (0);
223
224 /* Filtering requested and no match, the socket shall be skipped. */
225 return (1);
226}
227
228static void
229rts_input(struct mbuf *m)
230{
231 struct sockproto route_proto;
232 unsigned short *family;
233 struct m_tag *tag;
234
235 route_proto.sp_family = PF_ROUTE;
236 tag = m_tag_find(m, PACKET_TAG_RTSOCKFAM, NULL);
237 if (tag != NULL) {
238 family = (unsigned short *)(tag + 1);
239 route_proto.sp_protocol = *family;
240 m_tag_delete(m, tag);
241 } else
242 route_proto.sp_protocol = 0;
243
244 raw_input_ext(m, &route_proto, &route_src, raw_input_rts_cb);
245}
246
247/*
248 * It really doesn't make any sense at all for this code to share much
249 * with raw_usrreq.c, since its functionality is so restricted. XXX
250 */
251static void
252rts_abort(struct socket *so)
253{
254
255 raw_usrreqs.pru_abort(so);
256}
257
258static void
259rts_close(struct socket *so)
260{
261
262 raw_usrreqs.pru_close(so);
263}
264
265/* pru_accept is EOPNOTSUPP */
266
267static int
268rts_attach(struct socket *so, int proto, struct thread *td)
269{
270 struct rawcb *rp;
271 int error;
272
273 KASSERT(so->so_pcb == NULL, ("rts_attach: so_pcb != NULL"));
274
275 /* XXX */
276 rp = malloc(sizeof *rp, M_PCB, M_WAITOK | M_ZERO);
277 if (rp == NULL)
278 return ENOBUFS;
279
280 so->so_pcb = (caddr_t)rp;
281 so->so_fibnum = td->td_proc->p_fibnum;
282 error = raw_attach(so, proto);
283 rp = sotorawcb(so);
284 if (error) {
285 so->so_pcb = NULL;
286 free(rp, M_PCB);
287 return error;
288 }
289 RTSOCK_LOCK();
290 switch(rp->rcb_proto.sp_protocol) {
291 case AF_INET:
292 V_route_cb.ip_count++;
293 break;
294 case AF_INET6:
295 V_route_cb.ip6_count++;
296 break;
297 }
298 V_route_cb.any_count++;
299 RTSOCK_UNLOCK();
300 soisconnected(so);
301 so->so_options |= SO_USELOOPBACK;
302 return 0;
303}
304
305static int
306rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
307{
308
309 return (raw_usrreqs.pru_bind(so, nam, td)); /* xxx just EINVAL */
310}
311
312static int
313rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
314{
315
316 return (raw_usrreqs.pru_connect(so, nam, td)); /* XXX just EINVAL */
317}
318
319/* pru_connect2 is EOPNOTSUPP */
320/* pru_control is EOPNOTSUPP */
321
322static void
323rts_detach(struct socket *so)
324{
325 struct rawcb *rp = sotorawcb(so);
326
327 KASSERT(rp != NULL, ("rts_detach: rp == NULL"));
328
329 RTSOCK_LOCK();
330 switch(rp->rcb_proto.sp_protocol) {
331 case AF_INET:
332 V_route_cb.ip_count--;
333 break;
334 case AF_INET6:
335 V_route_cb.ip6_count--;
336 break;
337 }
338 V_route_cb.any_count--;
339 RTSOCK_UNLOCK();
340 raw_usrreqs.pru_detach(so);
341}
342
343static int
344rts_disconnect(struct socket *so)
345{
346
347 return (raw_usrreqs.pru_disconnect(so));
348}
349
350/* pru_listen is EOPNOTSUPP */
351
352static int
353rts_peeraddr(struct socket *so, struct sockaddr **nam)
354{
355
356 return (raw_usrreqs.pru_peeraddr(so, nam));
357}
358
359/* pru_rcvd is EOPNOTSUPP */
360/* pru_rcvoob is EOPNOTSUPP */
361
362static int
363rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
364 struct mbuf *control, struct thread *td)
365{
366
367 return (raw_usrreqs.pru_send(so, flags, m, nam, control, td));
368}
369
370/* pru_sense is null */
371
372static int
373rts_shutdown(struct socket *so)
374{
375
376 return (raw_usrreqs.pru_shutdown(so));
377}
378
379static int
380rts_sockaddr(struct socket *so, struct sockaddr **nam)
381{
382
383 return (raw_usrreqs.pru_sockaddr(so, nam));
384}
385
386static struct pr_usrreqs route_usrreqs = {
387 .pru_abort = rts_abort,
388 .pru_attach = rts_attach,
389 .pru_bind = rts_bind,
390 .pru_connect = rts_connect,
391 .pru_detach = rts_detach,
392 .pru_disconnect = rts_disconnect,
393 .pru_peeraddr = rts_peeraddr,
394 .pru_send = rts_send,
395 .pru_shutdown = rts_shutdown,
396 .pru_sockaddr = rts_sockaddr,
397 .pru_close = rts_close,
398};
399
400#ifndef _SOCKADDR_UNION_DEFINED
401#define _SOCKADDR_UNION_DEFINED
402/*
403 * The union of all possible address formats we handle.
404 */
405union sockaddr_union {
406 struct sockaddr sa;
407 struct sockaddr_in sin;
408 struct sockaddr_in6 sin6;
409};
410#endif /* _SOCKADDR_UNION_DEFINED */
411
412static int
413rtm_get_jailed(struct rt_addrinfo *info, struct ifnet *ifp,
414 struct rtentry *rt, union sockaddr_union *saun, struct ucred *cred)
415{
416
417 /* First, see if the returned address is part of the jail. */
418 if (prison_if(cred, rt->rt_ifa->ifa_addr) == 0) {
419 info->rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
420 return (0);
421 }
422
423 switch (info->rti_info[RTAX_DST]->sa_family) {
424#ifdef INET
425 case AF_INET:
426 {
427 struct in_addr ia;
428 struct ifaddr *ifa;
429 int found;
430
431 found = 0;
432 /*
433 * Try to find an address on the given outgoing interface
434 * that belongs to the jail.
435 */
436 IF_ADDR_RLOCK(ifp);
437 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
438 struct sockaddr *sa;
439 sa = ifa->ifa_addr;
440 if (sa->sa_family != AF_INET)
441 continue;
442 ia = ((struct sockaddr_in *)sa)->sin_addr;
443 if (prison_check_ip4(cred, &ia) == 0) {
444 found = 1;
445 break;
446 }
447 }
448 IF_ADDR_RUNLOCK(ifp);
449 if (!found) {
450 /*
451 * As a last resort return the 'default' jail address.
452 */
453 ia = ((struct sockaddr_in *)rt->rt_ifa->ifa_addr)->
454 sin_addr;
455 if (prison_get_ip4(cred, &ia) != 0)
456 return (ESRCH);
457 }
458 bzero(&saun->sin, sizeof(struct sockaddr_in));
459 saun->sin.sin_len = sizeof(struct sockaddr_in);
460 saun->sin.sin_family = AF_INET;
461 saun->sin.sin_addr.s_addr = ia.s_addr;
462 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin;
463 break;
464 }
465#endif
466#ifdef INET6
467 case AF_INET6:
468 {
469 struct in6_addr ia6;
470 struct ifaddr *ifa;
471 int found;
472
473 found = 0;
474 /*
475 * Try to find an address on the given outgoing interface
476 * that belongs to the jail.
477 */
478 IF_ADDR_RLOCK(ifp);
479 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
480 struct sockaddr *sa;
481 sa = ifa->ifa_addr;
482 if (sa->sa_family != AF_INET6)
483 continue;
484 bcopy(&((struct sockaddr_in6 *)sa)->sin6_addr,
485 &ia6, sizeof(struct in6_addr));
486 if (prison_check_ip6(cred, &ia6) == 0) {
487 found = 1;
488 break;
489 }
490 }
491 IF_ADDR_RUNLOCK(ifp);
492 if (!found) {
493 /*
494 * As a last resort return the 'default' jail address.
495 */
496 ia6 = ((struct sockaddr_in6 *)rt->rt_ifa->ifa_addr)->
497 sin6_addr;
498 if (prison_get_ip6(cred, &ia6) != 0)
499 return (ESRCH);
500 }
501 bzero(&saun->sin6, sizeof(struct sockaddr_in6));
502 saun->sin6.sin6_len = sizeof(struct sockaddr_in6);
503 saun->sin6.sin6_family = AF_INET6;
504 bcopy(&ia6, &saun->sin6.sin6_addr, sizeof(struct in6_addr));
505 if (sa6_recoverscope(&saun->sin6) != 0)
506 return (ESRCH);
507 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin6;
508 break;
509 }
510#endif
511 default:
512 return (ESRCH);
513 }
514 return (0);
515}
516
517/*ARGSUSED*/
518static int
519route_output(struct mbuf *m, struct socket *so)
520{
521 struct rt_msghdr *rtm = NULL;
522 struct rtentry *rt = NULL;
523 struct radix_node_head *rnh;
524 struct rt_addrinfo info;
525 struct sockaddr_storage ss;
526#ifdef INET6
527 struct sockaddr_in6 *sin6;
528 int i, rti_need_deembed = 0;
529#endif
530 int alloc_len = 0, len, error = 0, fibnum;
531 struct ifnet *ifp = NULL;
532 union sockaddr_union saun;
533 sa_family_t saf = AF_UNSPEC;
534 struct rawcb *rp = NULL;
535 struct walkarg w;
536
537 fibnum = so->so_fibnum;
538
539#define senderr(e) { error = e; goto flush;}
540 if (m == NULL || ((m->m_len < sizeof(long)) &&
541 (m = m_pullup(m, sizeof(long))) == NULL))
542 return (ENOBUFS);
543 if ((m->m_flags & M_PKTHDR) == 0)
544 panic("route_output");
545 len = m->m_pkthdr.len;
546 if (len < sizeof(*rtm) ||
547 len != mtod(m, struct rt_msghdr *)->rtm_msglen)
548 senderr(EINVAL);
549
550 /*
551 * Most of current messages are in range 200-240 bytes,
552 * minimize possible re-allocation on reply using larger size
553 * buffer aligned on 1k boundaty.
554 */
555 alloc_len = roundup2(len, 1024);
556 if ((rtm = malloc(alloc_len, M_TEMP, M_NOWAIT)) == NULL)
557 senderr(ENOBUFS);
558
559 m_copydata(m, 0, len, (caddr_t)rtm);
560 bzero(&info, sizeof(info));
561 bzero(&w, sizeof(w));
562
563 if (rtm->rtm_version != RTM_VERSION) {
564 /* Do not touch message since format is unknown */
565 free(rtm, M_TEMP);
566 rtm = NULL;
567 senderr(EPROTONOSUPPORT);
568 }
569
570 /*
571 * Starting from here, it is possible
572 * to alter original message and insert
573 * caller PID and error value.
574 */
575
576 rtm->rtm_pid = curproc->p_pid;
577 info.rti_addrs = rtm->rtm_addrs;
578
579 info.rti_mflags = rtm->rtm_inits;
580 info.rti_rmx = &rtm->rtm_rmx;
581
582 /*
583 * rt_xaddrs() performs s6_addr[2] := sin6_scope_id for AF_INET6
584 * link-local address because rtrequest requires addresses with
585 * embedded scope id.
586 */
587 if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info))
588 senderr(EINVAL);
589
590 info.rti_flags = rtm->rtm_flags;
591 if (info.rti_info[RTAX_DST] == NULL ||
592 info.rti_info[RTAX_DST]->sa_family >= AF_MAX ||
593 (info.rti_info[RTAX_GATEWAY] != NULL &&
594 info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX))
595 senderr(EINVAL);
596 saf = info.rti_info[RTAX_DST]->sa_family;
597 /*
598 * Verify that the caller has the appropriate privilege; RTM_GET
599 * is the only operation the non-superuser is allowed.
600 */
601 if (rtm->rtm_type != RTM_GET) {
602 error = priv_check(curthread, PRIV_NET_ROUTE);
603 if (error)
604 senderr(error);
605 }
606
607 /*
608 * The given gateway address may be an interface address.
609 * For example, issuing a "route change" command on a route
610 * entry that was created from a tunnel, and the gateway
611 * address given is the local end point. In this case the
612 * RTF_GATEWAY flag must be cleared or the destination will
613 * not be reachable even though there is no error message.
614 */
615 if (info.rti_info[RTAX_GATEWAY] != NULL &&
616 info.rti_info[RTAX_GATEWAY]->sa_family != AF_LINK) {
617 struct route gw_ro;
618
619 bzero(&gw_ro, sizeof(gw_ro));
620 gw_ro.ro_dst = *info.rti_info[RTAX_GATEWAY];
621 rtalloc_ign_fib(&gw_ro, 0, fibnum);
622 /*
623 * A host route through the loopback interface is
624 * installed for each interface adddress. In pre 8.0
625 * releases the interface address of a PPP link type
626 * is not reachable locally. This behavior is fixed as
627 * part of the new L2/L3 redesign and rewrite work. The
628 * signature of this interface address route is the
629 * AF_LINK sa_family type of the rt_gateway, and the
630 * rt_ifp has the IFF_LOOPBACK flag set.
631 */
632 if (gw_ro.ro_rt != NULL &&
633 gw_ro.ro_rt->rt_gateway->sa_family == AF_LINK &&
634 gw_ro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) {
635 info.rti_flags &= ~RTF_GATEWAY;
636 info.rti_flags |= RTF_GWFLAG_COMPAT;
637 }
638 if (gw_ro.ro_rt != NULL)
639 RTFREE(gw_ro.ro_rt);
640 }
641
642 switch (rtm->rtm_type) {
643 struct rtentry *saved_nrt;
644
645 case RTM_ADD:
646 case RTM_CHANGE:
647 if (info.rti_info[RTAX_GATEWAY] == NULL)
648 senderr(EINVAL);
649 saved_nrt = NULL;
650
651 /* support for new ARP code */
652 if (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK &&
653 (rtm->rtm_flags & RTF_LLDATA) != 0) {
654 error = lla_rt_output(rtm, &info);
655#ifdef INET6
656 if (error == 0)
657 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
658#endif
659 break;
660 }
661 error = rtrequest1_fib(rtm->rtm_type, &info, &saved_nrt,
662 fibnum);
663 if (error == 0 && saved_nrt != NULL) {
664#ifdef INET6
665 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
666#endif
667 RT_LOCK(saved_nrt);
668 rtm->rtm_index = saved_nrt->rt_ifp->if_index;
669 RT_REMREF(saved_nrt);
670 RT_UNLOCK(saved_nrt);
671 }
672 break;
673
674 case RTM_DELETE:
675 saved_nrt = NULL;
676 /* support for new ARP code */
677 if (info.rti_info[RTAX_GATEWAY] &&
678 (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) &&
679 (rtm->rtm_flags & RTF_LLDATA) != 0) {
680 error = lla_rt_output(rtm, &info);
681#ifdef INET6
682 if (error == 0)
683 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
684#endif
685 break;
686 }
687 error = rtrequest1_fib(RTM_DELETE, &info, &saved_nrt, fibnum);
688 if (error == 0) {
689 RT_LOCK(saved_nrt);
690 rt = saved_nrt;
691 goto report;
692 }
693#ifdef INET6
694 /* rt_msg2() will not be used when RTM_DELETE fails. */
695 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
696#endif
697 break;
698
699 case RTM_GET:
700 rnh = rt_tables_get_rnh(fibnum, saf);
701 if (rnh == NULL)
702 senderr(EAFNOSUPPORT);
703
704 RADIX_NODE_HEAD_RLOCK(rnh);
705
706 if (info.rti_info[RTAX_NETMASK] == NULL &&
707 rtm->rtm_type == RTM_GET) {
708 /*
709 * Provide logest prefix match for
710 * address lookup (no mask).
711 * 'route -n get addr'
712 */
713 rt = (struct rtentry *) rnh->rnh_matchaddr(
714 info.rti_info[RTAX_DST], rnh);
715 } else
716 rt = (struct rtentry *) rnh->rnh_lookup(
717 info.rti_info[RTAX_DST],
718 info.rti_info[RTAX_NETMASK], rnh);
719
720 if (rt == NULL) {
721 RADIX_NODE_HEAD_RUNLOCK(rnh);
722 senderr(ESRCH);
723 }
724#ifdef RADIX_MPATH
725 /*
726 * for RTM_CHANGE/LOCK, if we got multipath routes,
727 * we require users to specify a matching RTAX_GATEWAY.
728 *
729 * for RTM_GET, gate is optional even with multipath.
730 * if gate == NULL the first match is returned.
731 * (no need to call rt_mpath_matchgate if gate == NULL)
732 */
733 if (rn_mpath_capable(rnh) &&
734 (rtm->rtm_type != RTM_GET || info.rti_info[RTAX_GATEWAY])) {
735 rt = rt_mpath_matchgate(rt, info.rti_info[RTAX_GATEWAY]);
736 if (!rt) {
737 RADIX_NODE_HEAD_RUNLOCK(rnh);
738 senderr(ESRCH);
739 }
740 }
741#endif
742 /*
743 * If performing proxied L2 entry insertion, and
744 * the actual PPP host entry is found, perform
745 * another search to retrieve the prefix route of
746 * the local end point of the PPP link.
747 */
748 if (rtm->rtm_flags & RTF_ANNOUNCE) {
749 struct sockaddr laddr;
750
751 if (rt->rt_ifp != NULL &&
752 rt->rt_ifp->if_type == IFT_PROPVIRTUAL) {
753 struct ifaddr *ifa;
754
755 ifa = ifa_ifwithnet(info.rti_info[RTAX_DST], 1);
756 if (ifa != NULL)
757 rt_maskedcopy(ifa->ifa_addr,
758 &laddr,
759 ifa->ifa_netmask);
760 } else
761 rt_maskedcopy(rt->rt_ifa->ifa_addr,
762 &laddr,
763 rt->rt_ifa->ifa_netmask);
764 /*
765 * refactor rt and no lock operation necessary
766 */
767 rt = (struct rtentry *)rnh->rnh_matchaddr(&laddr, rnh);
768 if (rt == NULL) {
769 RADIX_NODE_HEAD_RUNLOCK(rnh);
770 senderr(ESRCH);
771 }
772 }
773 RT_LOCK(rt);
774 RT_ADDREF(rt);
775 RADIX_NODE_HEAD_RUNLOCK(rnh);
776
777report:
778 RT_LOCK_ASSERT(rt);
779 if ((rt->rt_flags & RTF_HOST) == 0
780 ? jailed_without_vnet(curthread->td_ucred)
781 : prison_if(curthread->td_ucred,
782 rt_key(rt)) != 0) {
783 RT_UNLOCK(rt);
784 senderr(ESRCH);
785 }
786 info.rti_info[RTAX_DST] = rt_key(rt);
787 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
788 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(rt_key(rt),
789 rt_mask(rt), &ss);
790 info.rti_info[RTAX_GENMASK] = 0;
791 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
792 ifp = rt->rt_ifp;
793 if (ifp) {
794 info.rti_info[RTAX_IFP] =
795 ifp->if_addr->ifa_addr;
796 error = rtm_get_jailed(&info, ifp, rt,
797 &saun, curthread->td_ucred);
798 if (error != 0) {
799 RT_UNLOCK(rt);
800 senderr(error);
801 }
802 if (ifp->if_flags & IFF_POINTOPOINT)
803 info.rti_info[RTAX_BRD] =
804 rt->rt_ifa->ifa_dstaddr;
805 rtm->rtm_index = ifp->if_index;
806 } else {
807 info.rti_info[RTAX_IFP] = NULL;
808 info.rti_info[RTAX_IFA] = NULL;
809 }
810 } else if ((ifp = rt->rt_ifp) != NULL) {
811 rtm->rtm_index = ifp->if_index;
812 }
813
814 /* Check if we need to realloc storage */
815 rtsock_msg_buffer(rtm->rtm_type, &info, NULL, &len);
816 if (len > alloc_len) {
817 struct rt_msghdr *new_rtm;
818 new_rtm = malloc(len, M_TEMP, M_NOWAIT);
819 if (new_rtm == NULL) {
820 RT_UNLOCK(rt);
821 senderr(ENOBUFS);
822 }
823 bcopy(rtm, new_rtm, rtm->rtm_msglen);
824 free(rtm, M_TEMP);
825 rtm = new_rtm;
826 alloc_len = len;
827 }
828
829 w.w_tmem = (caddr_t)rtm;
830 w.w_tmemsize = alloc_len;
831 rtsock_msg_buffer(rtm->rtm_type, &info, &w, &len);
832
833 if (rt->rt_flags & RTF_GWFLAG_COMPAT)
834 rtm->rtm_flags = RTF_GATEWAY |
835 (rt->rt_flags & ~RTF_GWFLAG_COMPAT);
836 else
837 rtm->rtm_flags = rt->rt_flags;
838 rt_getmetrics(rt, &rtm->rtm_rmx);
839 rtm->rtm_addrs = info.rti_addrs;
840
841 RT_UNLOCK(rt);
842 break;
843
844 default:
845 senderr(EOPNOTSUPP);
846 }
847
848flush:
849 if (rt != NULL)
850 RTFREE(rt);
851 /*
852 * Check to see if we don't want our own messages.
853 */
854 if ((so->so_options & SO_USELOOPBACK) == 0) {
855 if (V_route_cb.any_count <= 1) {
856 if (rtm != NULL)
857 free(rtm, M_TEMP);
858 m_freem(m);
859 return (error);
860 }
861 /* There is another listener, so construct message */
862 rp = sotorawcb(so);
863 }
864
865 if (rtm != NULL) {
866#ifdef INET6
867 if (rti_need_deembed) {
868 /* sin6_scope_id is recovered before sending rtm. */
869 sin6 = (struct sockaddr_in6 *)&ss;
870 for (i = 0; i < RTAX_MAX; i++) {
871 if (info.rti_info[i] == NULL)
872 continue;
873 if (info.rti_info[i]->sa_family != AF_INET6)
874 continue;
875 bcopy(info.rti_info[i], sin6, sizeof(*sin6));
876 if (sa6_recoverscope(sin6) == 0)
877 bcopy(sin6, info.rti_info[i],
878 sizeof(*sin6));
879 }
880 }
881#endif
882 if (error != 0)
883 rtm->rtm_errno = error;
884 else
885 rtm->rtm_flags |= RTF_DONE;
886
887 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
888 if (m->m_pkthdr.len < rtm->rtm_msglen) {
889 m_freem(m);
890 m = NULL;
891 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
892 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
893
894 free(rtm, M_TEMP);
895 }
896 if (m != NULL) {
897 M_SETFIB(m, fibnum);
898 m->m_flags |= RTS_FILTER_FIB;
899 if (rp) {
900 /*
901 * XXX insure we don't get a copy by
902 * invalidating our protocol
903 */
904 unsigned short family = rp->rcb_proto.sp_family;
905 rp->rcb_proto.sp_family = 0;
906 rt_dispatch(m, saf);
907 rp->rcb_proto.sp_family = family;
908 } else
909 rt_dispatch(m, saf);
910 }
911
912 return (error);
913}
914
915static void
916rt_getmetrics(const struct rtentry *rt, struct rt_metrics *out)
917{
918
919 bzero(out, sizeof(*out));
920 out->rmx_mtu = rt->rt_mtu;
921 out->rmx_weight = rt->rt_weight;
922 out->rmx_pksent = counter_u64_fetch(rt->rt_pksent);
923 /* Kernel -> userland timebase conversion. */
924 out->rmx_expire = rt->rt_expire ?
925 rt->rt_expire - time_uptime + time_second : 0;
926}
927
928/*
929 * Extract the addresses of the passed sockaddrs.
930 * Do a little sanity checking so as to avoid bad memory references.
931 * This data is derived straight from userland.
932 */
933static int
934rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
935{
936 struct sockaddr *sa;
937 int i;
938
939 for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
940 if ((rtinfo->rti_addrs & (1 << i)) == 0)
941 continue;
942 sa = (struct sockaddr *)cp;
943 /*
944 * It won't fit.
945 */
946 if (cp + sa->sa_len > cplim)
947 return (EINVAL);
948 /*
949 * there are no more.. quit now
950 * If there are more bits, they are in error.
951 * I've seen this. route(1) can evidently generate these.
952 * This causes kernel to core dump.
953 * for compatibility, If we see this, point to a safe address.
954 */
955 if (sa->sa_len == 0) {
956 rtinfo->rti_info[i] = &sa_zero;
957 return (0); /* should be EINVAL but for compat */
958 }
959 /* accept it */
960#ifdef INET6
961 if (sa->sa_family == AF_INET6)
962 sa6_embedscope((struct sockaddr_in6 *)sa,
963 V_ip6_use_defzone);
964#endif
965 rtinfo->rti_info[i] = sa;
966 cp += SA_SIZE(sa);
967 }
968 return (0);
969}
970
971/*
972 * Fill in @dmask with valid netmask leaving original @smask
973 * intact. Mostly used with radix netmasks.
974 */
975static struct sockaddr *
976rtsock_fix_netmask(struct sockaddr *dst, struct sockaddr *smask,
977 struct sockaddr_storage *dmask)
978{
979 if (dst == NULL || smask == NULL)
980 return (NULL);
981
982 memset(dmask, 0, dst->sa_len);
983 memcpy(dmask, smask, smask->sa_len);
984 dmask->ss_len = dst->sa_len;
985 dmask->ss_family = dst->sa_family;
986
987 return ((struct sockaddr *)dmask);
988}
989
990/*
991 * Writes information related to @rtinfo object to newly-allocated mbuf.
992 * Assumes MCLBYTES is enough to construct any message.
993 * Used for OS notifications of vaious events (if/ifa announces,etc)
994 *
995 * Returns allocated mbuf or NULL on failure.
996 */
997static struct mbuf *
998rtsock_msg_mbuf(int type, struct rt_addrinfo *rtinfo)
999{
1000 struct rt_msghdr *rtm;
1001 struct mbuf *m;
1002 int i;
1003 struct sockaddr *sa;
1004#ifdef INET6
1005 struct sockaddr_storage ss;
1006 struct sockaddr_in6 *sin6;
1007#endif
1008 int len, dlen;
1009
1010 switch (type) {
1011
1012 case RTM_DELADDR:
1013 case RTM_NEWADDR:
1014 len = sizeof(struct ifa_msghdr);
1015 break;
1016
1017 case RTM_DELMADDR:
1018 case RTM_NEWMADDR:
1019 len = sizeof(struct ifma_msghdr);
1020 break;
1021
1022 case RTM_IFINFO:
1023 len = sizeof(struct if_msghdr);
1024 break;
1025
1026 case RTM_IFANNOUNCE:
1027 case RTM_IEEE80211:
1028 len = sizeof(struct if_announcemsghdr);
1029 break;
1030
1031 default:
1032 len = sizeof(struct rt_msghdr);
1033 }
1034
1035 /* XXXGL: can we use MJUMPAGESIZE cluster here? */
1036 KASSERT(len <= MCLBYTES, ("%s: message too big", __func__));
1037 if (len > MHLEN)
1038 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1039 else
1040 m = m_gethdr(M_NOWAIT, MT_DATA);
1041 if (m == NULL)
1042 return (m);
1043
1044 m->m_pkthdr.len = m->m_len = len;
1045 rtm = mtod(m, struct rt_msghdr *);
1046 bzero((caddr_t)rtm, len);
1047 for (i = 0; i < RTAX_MAX; i++) {
1048 if ((sa = rtinfo->rti_info[i]) == NULL)
1049 continue;
1050 rtinfo->rti_addrs |= (1 << i);
1051 dlen = SA_SIZE(sa);
1052#ifdef INET6
1053 if (V_deembed_scopeid && sa->sa_family == AF_INET6) {
1054 sin6 = (struct sockaddr_in6 *)&ss;
1055 bcopy(sa, sin6, sizeof(*sin6));
1056 if (sa6_recoverscope(sin6) == 0)
1057 sa = (struct sockaddr *)sin6;
1058 }
1059#endif
1060 m_copyback(m, len, dlen, (caddr_t)sa);
1061 len += dlen;
1062 }
1063 if (m->m_pkthdr.len != len) {
1064 m_freem(m);
1065 return (NULL);
1066 }
1067 rtm->rtm_msglen = len;
1068 rtm->rtm_version = RTM_VERSION;
1069 rtm->rtm_type = type;
1070 return (m);
1071}
1072
1073/*
1074 * Writes information related to @rtinfo object to preallocated buffer.
1075 * Stores needed size in @plen. If @w is NULL, calculates size without
1076 * writing.
1077 * Used for sysctl dumps and rtsock answers (RTM_DEL/RTM_GET) generation.
1078 *
1079 * Returns 0 on success.
1080 *
1081 */
1082static int
1083rtsock_msg_buffer(int type, struct rt_addrinfo *rtinfo, struct walkarg *w, int *plen)
1084{
1085 int i;
1086 int len, buflen = 0, dlen;
1087 caddr_t cp = NULL;
1088 struct rt_msghdr *rtm = NULL;
1089#ifdef INET6
1090 struct sockaddr_storage ss;
1091 struct sockaddr_in6 *sin6;
1092#endif
1093
1094 switch (type) {
1095
1096 case RTM_DELADDR:
1097 case RTM_NEWADDR:
1098 if (w != NULL && w->w_op == NET_RT_IFLISTL) {
1099#ifdef COMPAT_FREEBSD32
1100 if (w->w_req->flags & SCTL_MASK32)
1101 len = sizeof(struct ifa_msghdrl32);
1102 else
1103#endif
1104 len = sizeof(struct ifa_msghdrl);
1105 } else
1106 len = sizeof(struct ifa_msghdr);
1107 break;
1108
1109 case RTM_IFINFO:
1110#ifdef COMPAT_FREEBSD32
1111 if (w != NULL && w->w_req->flags & SCTL_MASK32) {
1112 if (w->w_op == NET_RT_IFLISTL)
1113 len = sizeof(struct if_msghdrl32);
1114 else
1115 len = sizeof(struct if_msghdr32);
1116 break;
1117 }
1118#endif
1119 if (w != NULL && w->w_op == NET_RT_IFLISTL)
1120 len = sizeof(struct if_msghdrl);
1121 else
1122 len = sizeof(struct if_msghdr);
1123 break;
1124
1125 case RTM_NEWMADDR:
1126 len = sizeof(struct ifma_msghdr);
1127 break;
1128
1129 default:
1130 len = sizeof(struct rt_msghdr);
1131 }
1132
1133 if (w != NULL) {
1134 rtm = (struct rt_msghdr *)w->w_tmem;
1135 buflen = w->w_tmemsize - len;
1136 cp = (caddr_t)w->w_tmem + len;
1137 }
1138
1139 rtinfo->rti_addrs = 0;
1140 for (i = 0; i < RTAX_MAX; i++) {
1141 struct sockaddr *sa;
1142
1143 if ((sa = rtinfo->rti_info[i]) == NULL)
1144 continue;
1145 rtinfo->rti_addrs |= (1 << i);
1146 dlen = SA_SIZE(sa);
1147 if (cp != NULL && buflen >= dlen) {
1148#ifdef INET6
1149 if (V_deembed_scopeid && sa->sa_family == AF_INET6) {
1150 sin6 = (struct sockaddr_in6 *)&ss;
1151 bcopy(sa, sin6, sizeof(*sin6));
1152 if (sa6_recoverscope(sin6) == 0)
1153 sa = (struct sockaddr *)sin6;
1154 }
1155#endif
1156 bcopy((caddr_t)sa, cp, (unsigned)dlen);
1157 cp += dlen;
1158 buflen -= dlen;
1159 } else if (cp != NULL) {
1160 /*
1161 * Buffer too small. Count needed size
1162 * and return with error.
1163 */
1164 cp = NULL;
1165 }
1166
1167 len += dlen;
1168 }
1169
1170 if (cp != NULL) {
1171 dlen = ALIGN(len) - len;
1172 if (buflen < dlen)
1173 cp = NULL;
1174 else
1175 buflen -= dlen;
1176 }
1177 len = ALIGN(len);
1178
1179 if (cp != NULL) {
1180 /* fill header iff buffer is large enough */
1181 rtm->rtm_version = RTM_VERSION;
1182 rtm->rtm_type = type;
1183 rtm->rtm_msglen = len;
1184 }
1185
1186 *plen = len;
1187
1188 if (w != NULL && cp == NULL)
1189 return (ENOBUFS);
1190
1191 return (0);
1192}
1193
1194/*
1195 * This routine is called to generate a message from the routing
1196 * socket indicating that a redirect has occured, a routing lookup
1197 * has failed, or that a protocol has detected timeouts to a particular
1198 * destination.
1199 */
1200void
1201rt_missmsg_fib(int type, struct rt_addrinfo *rtinfo, int flags, int error,
1202 int fibnum)
1203{
1204 struct rt_msghdr *rtm;
1205 struct mbuf *m;
1206 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
1207
1208 if (V_route_cb.any_count == 0)
1209 return;
1210 m = rtsock_msg_mbuf(type, rtinfo);
1211 if (m == NULL)
1212 return;
1213
1214 if (fibnum != RT_ALL_FIBS) {
1215 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out "
1216 "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs));
1217 M_SETFIB(m, fibnum);
1218 m->m_flags |= RTS_FILTER_FIB;
1219 }
1220
1221 rtm = mtod(m, struct rt_msghdr *);
1222 rtm->rtm_flags = RTF_DONE | flags;
1223 rtm->rtm_errno = error;
1224 rtm->rtm_addrs = rtinfo->rti_addrs;
1225 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1226}
1227
1228void
1229rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
1230{
1231
1232 rt_missmsg_fib(type, rtinfo, flags, error, RT_ALL_FIBS);
1233}
1234
1235/*
1236 * This routine is called to generate a message from the routing
1237 * socket indicating that the status of a network interface has changed.
1238 */
1239void
1240rt_ifmsg(struct ifnet *ifp)
1241{
1242 struct if_msghdr *ifm;
1243 struct mbuf *m;
1244 struct rt_addrinfo info;
1245
1246 if (V_route_cb.any_count == 0)
1247 return;
1248 bzero((caddr_t)&info, sizeof(info));
1249 m = rtsock_msg_mbuf(RTM_IFINFO, &info);
1250 if (m == NULL)
1251 return;
1252 ifm = mtod(m, struct if_msghdr *);
1253 ifm->ifm_index = ifp->if_index;
1254 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1255 ifm->ifm_data = ifp->if_data;
1256 ifm->ifm_addrs = 0;
1257 rt_dispatch(m, AF_UNSPEC);
1258}
1259
1260/*
1261 * Announce interface address arrival/withdraw.
1262 * Please do not call directly, use rt_addrmsg().
1263 * Assume input data to be valid.
1264 * Returns 0 on success.
1265 */
1266int
1267rtsock_addrmsg(int cmd, struct ifaddr *ifa, int fibnum)
1268{
1269 struct rt_addrinfo info;
1270 struct sockaddr *sa;
1271 int ncmd;
1272 struct mbuf *m;
1273 struct ifa_msghdr *ifam;
1274 struct ifnet *ifp = ifa->ifa_ifp;
1275 struct sockaddr_storage ss;
1276
1277 if (V_route_cb.any_count == 0)
1278 return (0);
1279
1280 ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
1281
1282 bzero((caddr_t)&info, sizeof(info));
1283 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1284 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1285 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(
1286 info.rti_info[RTAX_IFP], ifa->ifa_netmask, &ss);
1287 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1288 if ((m = rtsock_msg_mbuf(ncmd, &info)) == NULL)
1289 return (ENOBUFS);
1290 ifam = mtod(m, struct ifa_msghdr *);
1291 ifam->ifam_index = ifp->if_index;
31 */
32#include "opt_compat.h"
33#include "opt_mpath.h"
34#include "opt_inet.h"
35#include "opt_inet6.h"
36
37#include <sys/param.h>
38#include <sys/jail.h>
39#include <sys/kernel.h>
40#include <sys/domain.h>
41#include <sys/lock.h>
42#include <sys/malloc.h>
43#include <sys/mbuf.h>
44#include <sys/priv.h>
45#include <sys/proc.h>
46#include <sys/protosw.h>
47#include <sys/rwlock.h>
48#include <sys/signalvar.h>
49#include <sys/socket.h>
50#include <sys/socketvar.h>
51#include <sys/sysctl.h>
52#include <sys/systm.h>
53
54#include <net/if.h>
55#include <net/if_var.h>
56#include <net/if_dl.h>
57#include <net/if_llatbl.h>
58#include <net/if_types.h>
59#include <net/netisr.h>
60#include <net/raw_cb.h>
61#include <net/route.h>
62#include <net/vnet.h>
63
64#include <netinet/in.h>
65#include <netinet/if_ether.h>
66#include <netinet/ip_carp.h>
67#ifdef INET6
68#include <netinet6/ip6_var.h>
69#include <netinet6/scope6_var.h>
70#endif
71
72#ifdef COMPAT_FREEBSD32
73#include <sys/mount.h>
74#include <compat/freebsd32/freebsd32.h>
75
76struct if_msghdr32 {
77 uint16_t ifm_msglen;
78 uint8_t ifm_version;
79 uint8_t ifm_type;
80 int32_t ifm_addrs;
81 int32_t ifm_flags;
82 uint16_t ifm_index;
83 struct if_data ifm_data;
84};
85
86struct if_msghdrl32 {
87 uint16_t ifm_msglen;
88 uint8_t ifm_version;
89 uint8_t ifm_type;
90 int32_t ifm_addrs;
91 int32_t ifm_flags;
92 uint16_t ifm_index;
93 uint16_t _ifm_spare1;
94 uint16_t ifm_len;
95 uint16_t ifm_data_off;
96 struct if_data ifm_data;
97};
98
99struct ifa_msghdrl32 {
100 uint16_t ifam_msglen;
101 uint8_t ifam_version;
102 uint8_t ifam_type;
103 int32_t ifam_addrs;
104 int32_t ifam_flags;
105 uint16_t ifam_index;
106 uint16_t _ifam_spare1;
107 uint16_t ifam_len;
108 uint16_t ifam_data_off;
109 int32_t ifam_metric;
110 struct if_data ifam_data;
111};
112#endif /* COMPAT_FREEBSD32 */
113
114MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
115
116/* NB: these are not modified */
117static struct sockaddr route_src = { 2, PF_ROUTE, };
118static struct sockaddr sa_zero = { sizeof(sa_zero), AF_INET, };
119
120/* These are external hooks for CARP. */
121int (*carp_get_vhid_p)(struct ifaddr *);
122
123/*
124 * Used by rtsock/raw_input callback code to decide whether to filter the update
125 * notification to a socket bound to a particular FIB.
126 */
127#define RTS_FILTER_FIB M_PROTO8
128
129typedef struct {
130 int ip_count; /* attached w/ AF_INET */
131 int ip6_count; /* attached w/ AF_INET6 */
132 int any_count; /* total attached */
133} route_cb_t;
134static VNET_DEFINE(route_cb_t, route_cb);
135#define V_route_cb VNET(route_cb)
136
137struct mtx rtsock_mtx;
138MTX_SYSINIT(rtsock, &rtsock_mtx, "rtsock route_cb lock", MTX_DEF);
139
140#define RTSOCK_LOCK() mtx_lock(&rtsock_mtx)
141#define RTSOCK_UNLOCK() mtx_unlock(&rtsock_mtx)
142#define RTSOCK_LOCK_ASSERT() mtx_assert(&rtsock_mtx, MA_OWNED)
143
144static SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RD, 0, "");
145
146struct walkarg {
147 int w_tmemsize;
148 int w_op, w_arg;
149 caddr_t w_tmem;
150 struct sysctl_req *w_req;
151};
152
153static void rts_input(struct mbuf *m);
154static struct mbuf *rtsock_msg_mbuf(int type, struct rt_addrinfo *rtinfo);
155static int rtsock_msg_buffer(int type, struct rt_addrinfo *rtinfo,
156 struct walkarg *w, int *plen);
157static int rt_xaddrs(caddr_t cp, caddr_t cplim,
158 struct rt_addrinfo *rtinfo);
159static int sysctl_dumpentry(struct radix_node *rn, void *vw);
160static int sysctl_iflist(int af, struct walkarg *w);
161static int sysctl_ifmalist(int af, struct walkarg *w);
162static int route_output(struct mbuf *m, struct socket *so);
163static void rt_getmetrics(const struct rtentry *rt, struct rt_metrics *out);
164static void rt_dispatch(struct mbuf *, sa_family_t);
165static struct sockaddr *rtsock_fix_netmask(struct sockaddr *dst,
166 struct sockaddr *smask, struct sockaddr_storage *dmask);
167
168static struct netisr_handler rtsock_nh = {
169 .nh_name = "rtsock",
170 .nh_handler = rts_input,
171 .nh_proto = NETISR_ROUTE,
172 .nh_policy = NETISR_POLICY_SOURCE,
173};
174
175static int
176sysctl_route_netisr_maxqlen(SYSCTL_HANDLER_ARGS)
177{
178 int error, qlimit;
179
180 netisr_getqlimit(&rtsock_nh, &qlimit);
181 error = sysctl_handle_int(oidp, &qlimit, 0, req);
182 if (error || !req->newptr)
183 return (error);
184 if (qlimit < 1)
185 return (EINVAL);
186 return (netisr_setqlimit(&rtsock_nh, qlimit));
187}
188SYSCTL_PROC(_net_route, OID_AUTO, netisr_maxqlen, CTLTYPE_INT|CTLFLAG_RW,
189 0, 0, sysctl_route_netisr_maxqlen, "I",
190 "maximum routing socket dispatch queue length");
191
192static void
193rts_init(void)
194{
195 int tmp;
196
197 if (TUNABLE_INT_FETCH("net.route.netisr_maxqlen", &tmp))
198 rtsock_nh.nh_qlimit = tmp;
199 netisr_register(&rtsock_nh);
200}
201SYSINIT(rtsock, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, rts_init, 0);
202
203static int
204raw_input_rts_cb(struct mbuf *m, struct sockproto *proto, struct sockaddr *src,
205 struct rawcb *rp)
206{
207 int fibnum;
208
209 KASSERT(m != NULL, ("%s: m is NULL", __func__));
210 KASSERT(proto != NULL, ("%s: proto is NULL", __func__));
211 KASSERT(rp != NULL, ("%s: rp is NULL", __func__));
212
213 /* No filtering requested. */
214 if ((m->m_flags & RTS_FILTER_FIB) == 0)
215 return (0);
216
217 /* Check if it is a rts and the fib matches the one of the socket. */
218 fibnum = M_GETFIB(m);
219 if (proto->sp_family != PF_ROUTE ||
220 rp->rcb_socket == NULL ||
221 rp->rcb_socket->so_fibnum == fibnum)
222 return (0);
223
224 /* Filtering requested and no match, the socket shall be skipped. */
225 return (1);
226}
227
228static void
229rts_input(struct mbuf *m)
230{
231 struct sockproto route_proto;
232 unsigned short *family;
233 struct m_tag *tag;
234
235 route_proto.sp_family = PF_ROUTE;
236 tag = m_tag_find(m, PACKET_TAG_RTSOCKFAM, NULL);
237 if (tag != NULL) {
238 family = (unsigned short *)(tag + 1);
239 route_proto.sp_protocol = *family;
240 m_tag_delete(m, tag);
241 } else
242 route_proto.sp_protocol = 0;
243
244 raw_input_ext(m, &route_proto, &route_src, raw_input_rts_cb);
245}
246
247/*
248 * It really doesn't make any sense at all for this code to share much
249 * with raw_usrreq.c, since its functionality is so restricted. XXX
250 */
251static void
252rts_abort(struct socket *so)
253{
254
255 raw_usrreqs.pru_abort(so);
256}
257
258static void
259rts_close(struct socket *so)
260{
261
262 raw_usrreqs.pru_close(so);
263}
264
265/* pru_accept is EOPNOTSUPP */
266
267static int
268rts_attach(struct socket *so, int proto, struct thread *td)
269{
270 struct rawcb *rp;
271 int error;
272
273 KASSERT(so->so_pcb == NULL, ("rts_attach: so_pcb != NULL"));
274
275 /* XXX */
276 rp = malloc(sizeof *rp, M_PCB, M_WAITOK | M_ZERO);
277 if (rp == NULL)
278 return ENOBUFS;
279
280 so->so_pcb = (caddr_t)rp;
281 so->so_fibnum = td->td_proc->p_fibnum;
282 error = raw_attach(so, proto);
283 rp = sotorawcb(so);
284 if (error) {
285 so->so_pcb = NULL;
286 free(rp, M_PCB);
287 return error;
288 }
289 RTSOCK_LOCK();
290 switch(rp->rcb_proto.sp_protocol) {
291 case AF_INET:
292 V_route_cb.ip_count++;
293 break;
294 case AF_INET6:
295 V_route_cb.ip6_count++;
296 break;
297 }
298 V_route_cb.any_count++;
299 RTSOCK_UNLOCK();
300 soisconnected(so);
301 so->so_options |= SO_USELOOPBACK;
302 return 0;
303}
304
305static int
306rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
307{
308
309 return (raw_usrreqs.pru_bind(so, nam, td)); /* xxx just EINVAL */
310}
311
312static int
313rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
314{
315
316 return (raw_usrreqs.pru_connect(so, nam, td)); /* XXX just EINVAL */
317}
318
319/* pru_connect2 is EOPNOTSUPP */
320/* pru_control is EOPNOTSUPP */
321
322static void
323rts_detach(struct socket *so)
324{
325 struct rawcb *rp = sotorawcb(so);
326
327 KASSERT(rp != NULL, ("rts_detach: rp == NULL"));
328
329 RTSOCK_LOCK();
330 switch(rp->rcb_proto.sp_protocol) {
331 case AF_INET:
332 V_route_cb.ip_count--;
333 break;
334 case AF_INET6:
335 V_route_cb.ip6_count--;
336 break;
337 }
338 V_route_cb.any_count--;
339 RTSOCK_UNLOCK();
340 raw_usrreqs.pru_detach(so);
341}
342
343static int
344rts_disconnect(struct socket *so)
345{
346
347 return (raw_usrreqs.pru_disconnect(so));
348}
349
350/* pru_listen is EOPNOTSUPP */
351
352static int
353rts_peeraddr(struct socket *so, struct sockaddr **nam)
354{
355
356 return (raw_usrreqs.pru_peeraddr(so, nam));
357}
358
359/* pru_rcvd is EOPNOTSUPP */
360/* pru_rcvoob is EOPNOTSUPP */
361
362static int
363rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
364 struct mbuf *control, struct thread *td)
365{
366
367 return (raw_usrreqs.pru_send(so, flags, m, nam, control, td));
368}
369
370/* pru_sense is null */
371
372static int
373rts_shutdown(struct socket *so)
374{
375
376 return (raw_usrreqs.pru_shutdown(so));
377}
378
379static int
380rts_sockaddr(struct socket *so, struct sockaddr **nam)
381{
382
383 return (raw_usrreqs.pru_sockaddr(so, nam));
384}
385
386static struct pr_usrreqs route_usrreqs = {
387 .pru_abort = rts_abort,
388 .pru_attach = rts_attach,
389 .pru_bind = rts_bind,
390 .pru_connect = rts_connect,
391 .pru_detach = rts_detach,
392 .pru_disconnect = rts_disconnect,
393 .pru_peeraddr = rts_peeraddr,
394 .pru_send = rts_send,
395 .pru_shutdown = rts_shutdown,
396 .pru_sockaddr = rts_sockaddr,
397 .pru_close = rts_close,
398};
399
400#ifndef _SOCKADDR_UNION_DEFINED
401#define _SOCKADDR_UNION_DEFINED
402/*
403 * The union of all possible address formats we handle.
404 */
405union sockaddr_union {
406 struct sockaddr sa;
407 struct sockaddr_in sin;
408 struct sockaddr_in6 sin6;
409};
410#endif /* _SOCKADDR_UNION_DEFINED */
411
412static int
413rtm_get_jailed(struct rt_addrinfo *info, struct ifnet *ifp,
414 struct rtentry *rt, union sockaddr_union *saun, struct ucred *cred)
415{
416
417 /* First, see if the returned address is part of the jail. */
418 if (prison_if(cred, rt->rt_ifa->ifa_addr) == 0) {
419 info->rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
420 return (0);
421 }
422
423 switch (info->rti_info[RTAX_DST]->sa_family) {
424#ifdef INET
425 case AF_INET:
426 {
427 struct in_addr ia;
428 struct ifaddr *ifa;
429 int found;
430
431 found = 0;
432 /*
433 * Try to find an address on the given outgoing interface
434 * that belongs to the jail.
435 */
436 IF_ADDR_RLOCK(ifp);
437 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
438 struct sockaddr *sa;
439 sa = ifa->ifa_addr;
440 if (sa->sa_family != AF_INET)
441 continue;
442 ia = ((struct sockaddr_in *)sa)->sin_addr;
443 if (prison_check_ip4(cred, &ia) == 0) {
444 found = 1;
445 break;
446 }
447 }
448 IF_ADDR_RUNLOCK(ifp);
449 if (!found) {
450 /*
451 * As a last resort return the 'default' jail address.
452 */
453 ia = ((struct sockaddr_in *)rt->rt_ifa->ifa_addr)->
454 sin_addr;
455 if (prison_get_ip4(cred, &ia) != 0)
456 return (ESRCH);
457 }
458 bzero(&saun->sin, sizeof(struct sockaddr_in));
459 saun->sin.sin_len = sizeof(struct sockaddr_in);
460 saun->sin.sin_family = AF_INET;
461 saun->sin.sin_addr.s_addr = ia.s_addr;
462 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin;
463 break;
464 }
465#endif
466#ifdef INET6
467 case AF_INET6:
468 {
469 struct in6_addr ia6;
470 struct ifaddr *ifa;
471 int found;
472
473 found = 0;
474 /*
475 * Try to find an address on the given outgoing interface
476 * that belongs to the jail.
477 */
478 IF_ADDR_RLOCK(ifp);
479 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
480 struct sockaddr *sa;
481 sa = ifa->ifa_addr;
482 if (sa->sa_family != AF_INET6)
483 continue;
484 bcopy(&((struct sockaddr_in6 *)sa)->sin6_addr,
485 &ia6, sizeof(struct in6_addr));
486 if (prison_check_ip6(cred, &ia6) == 0) {
487 found = 1;
488 break;
489 }
490 }
491 IF_ADDR_RUNLOCK(ifp);
492 if (!found) {
493 /*
494 * As a last resort return the 'default' jail address.
495 */
496 ia6 = ((struct sockaddr_in6 *)rt->rt_ifa->ifa_addr)->
497 sin6_addr;
498 if (prison_get_ip6(cred, &ia6) != 0)
499 return (ESRCH);
500 }
501 bzero(&saun->sin6, sizeof(struct sockaddr_in6));
502 saun->sin6.sin6_len = sizeof(struct sockaddr_in6);
503 saun->sin6.sin6_family = AF_INET6;
504 bcopy(&ia6, &saun->sin6.sin6_addr, sizeof(struct in6_addr));
505 if (sa6_recoverscope(&saun->sin6) != 0)
506 return (ESRCH);
507 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin6;
508 break;
509 }
510#endif
511 default:
512 return (ESRCH);
513 }
514 return (0);
515}
516
517/*ARGSUSED*/
518static int
519route_output(struct mbuf *m, struct socket *so)
520{
521 struct rt_msghdr *rtm = NULL;
522 struct rtentry *rt = NULL;
523 struct radix_node_head *rnh;
524 struct rt_addrinfo info;
525 struct sockaddr_storage ss;
526#ifdef INET6
527 struct sockaddr_in6 *sin6;
528 int i, rti_need_deembed = 0;
529#endif
530 int alloc_len = 0, len, error = 0, fibnum;
531 struct ifnet *ifp = NULL;
532 union sockaddr_union saun;
533 sa_family_t saf = AF_UNSPEC;
534 struct rawcb *rp = NULL;
535 struct walkarg w;
536
537 fibnum = so->so_fibnum;
538
539#define senderr(e) { error = e; goto flush;}
540 if (m == NULL || ((m->m_len < sizeof(long)) &&
541 (m = m_pullup(m, sizeof(long))) == NULL))
542 return (ENOBUFS);
543 if ((m->m_flags & M_PKTHDR) == 0)
544 panic("route_output");
545 len = m->m_pkthdr.len;
546 if (len < sizeof(*rtm) ||
547 len != mtod(m, struct rt_msghdr *)->rtm_msglen)
548 senderr(EINVAL);
549
550 /*
551 * Most of current messages are in range 200-240 bytes,
552 * minimize possible re-allocation on reply using larger size
553 * buffer aligned on 1k boundaty.
554 */
555 alloc_len = roundup2(len, 1024);
556 if ((rtm = malloc(alloc_len, M_TEMP, M_NOWAIT)) == NULL)
557 senderr(ENOBUFS);
558
559 m_copydata(m, 0, len, (caddr_t)rtm);
560 bzero(&info, sizeof(info));
561 bzero(&w, sizeof(w));
562
563 if (rtm->rtm_version != RTM_VERSION) {
564 /* Do not touch message since format is unknown */
565 free(rtm, M_TEMP);
566 rtm = NULL;
567 senderr(EPROTONOSUPPORT);
568 }
569
570 /*
571 * Starting from here, it is possible
572 * to alter original message and insert
573 * caller PID and error value.
574 */
575
576 rtm->rtm_pid = curproc->p_pid;
577 info.rti_addrs = rtm->rtm_addrs;
578
579 info.rti_mflags = rtm->rtm_inits;
580 info.rti_rmx = &rtm->rtm_rmx;
581
582 /*
583 * rt_xaddrs() performs s6_addr[2] := sin6_scope_id for AF_INET6
584 * link-local address because rtrequest requires addresses with
585 * embedded scope id.
586 */
587 if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info))
588 senderr(EINVAL);
589
590 info.rti_flags = rtm->rtm_flags;
591 if (info.rti_info[RTAX_DST] == NULL ||
592 info.rti_info[RTAX_DST]->sa_family >= AF_MAX ||
593 (info.rti_info[RTAX_GATEWAY] != NULL &&
594 info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX))
595 senderr(EINVAL);
596 saf = info.rti_info[RTAX_DST]->sa_family;
597 /*
598 * Verify that the caller has the appropriate privilege; RTM_GET
599 * is the only operation the non-superuser is allowed.
600 */
601 if (rtm->rtm_type != RTM_GET) {
602 error = priv_check(curthread, PRIV_NET_ROUTE);
603 if (error)
604 senderr(error);
605 }
606
607 /*
608 * The given gateway address may be an interface address.
609 * For example, issuing a "route change" command on a route
610 * entry that was created from a tunnel, and the gateway
611 * address given is the local end point. In this case the
612 * RTF_GATEWAY flag must be cleared or the destination will
613 * not be reachable even though there is no error message.
614 */
615 if (info.rti_info[RTAX_GATEWAY] != NULL &&
616 info.rti_info[RTAX_GATEWAY]->sa_family != AF_LINK) {
617 struct route gw_ro;
618
619 bzero(&gw_ro, sizeof(gw_ro));
620 gw_ro.ro_dst = *info.rti_info[RTAX_GATEWAY];
621 rtalloc_ign_fib(&gw_ro, 0, fibnum);
622 /*
623 * A host route through the loopback interface is
624 * installed for each interface adddress. In pre 8.0
625 * releases the interface address of a PPP link type
626 * is not reachable locally. This behavior is fixed as
627 * part of the new L2/L3 redesign and rewrite work. The
628 * signature of this interface address route is the
629 * AF_LINK sa_family type of the rt_gateway, and the
630 * rt_ifp has the IFF_LOOPBACK flag set.
631 */
632 if (gw_ro.ro_rt != NULL &&
633 gw_ro.ro_rt->rt_gateway->sa_family == AF_LINK &&
634 gw_ro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) {
635 info.rti_flags &= ~RTF_GATEWAY;
636 info.rti_flags |= RTF_GWFLAG_COMPAT;
637 }
638 if (gw_ro.ro_rt != NULL)
639 RTFREE(gw_ro.ro_rt);
640 }
641
642 switch (rtm->rtm_type) {
643 struct rtentry *saved_nrt;
644
645 case RTM_ADD:
646 case RTM_CHANGE:
647 if (info.rti_info[RTAX_GATEWAY] == NULL)
648 senderr(EINVAL);
649 saved_nrt = NULL;
650
651 /* support for new ARP code */
652 if (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK &&
653 (rtm->rtm_flags & RTF_LLDATA) != 0) {
654 error = lla_rt_output(rtm, &info);
655#ifdef INET6
656 if (error == 0)
657 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
658#endif
659 break;
660 }
661 error = rtrequest1_fib(rtm->rtm_type, &info, &saved_nrt,
662 fibnum);
663 if (error == 0 && saved_nrt != NULL) {
664#ifdef INET6
665 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
666#endif
667 RT_LOCK(saved_nrt);
668 rtm->rtm_index = saved_nrt->rt_ifp->if_index;
669 RT_REMREF(saved_nrt);
670 RT_UNLOCK(saved_nrt);
671 }
672 break;
673
674 case RTM_DELETE:
675 saved_nrt = NULL;
676 /* support for new ARP code */
677 if (info.rti_info[RTAX_GATEWAY] &&
678 (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) &&
679 (rtm->rtm_flags & RTF_LLDATA) != 0) {
680 error = lla_rt_output(rtm, &info);
681#ifdef INET6
682 if (error == 0)
683 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
684#endif
685 break;
686 }
687 error = rtrequest1_fib(RTM_DELETE, &info, &saved_nrt, fibnum);
688 if (error == 0) {
689 RT_LOCK(saved_nrt);
690 rt = saved_nrt;
691 goto report;
692 }
693#ifdef INET6
694 /* rt_msg2() will not be used when RTM_DELETE fails. */
695 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
696#endif
697 break;
698
699 case RTM_GET:
700 rnh = rt_tables_get_rnh(fibnum, saf);
701 if (rnh == NULL)
702 senderr(EAFNOSUPPORT);
703
704 RADIX_NODE_HEAD_RLOCK(rnh);
705
706 if (info.rti_info[RTAX_NETMASK] == NULL &&
707 rtm->rtm_type == RTM_GET) {
708 /*
709 * Provide logest prefix match for
710 * address lookup (no mask).
711 * 'route -n get addr'
712 */
713 rt = (struct rtentry *) rnh->rnh_matchaddr(
714 info.rti_info[RTAX_DST], rnh);
715 } else
716 rt = (struct rtentry *) rnh->rnh_lookup(
717 info.rti_info[RTAX_DST],
718 info.rti_info[RTAX_NETMASK], rnh);
719
720 if (rt == NULL) {
721 RADIX_NODE_HEAD_RUNLOCK(rnh);
722 senderr(ESRCH);
723 }
724#ifdef RADIX_MPATH
725 /*
726 * for RTM_CHANGE/LOCK, if we got multipath routes,
727 * we require users to specify a matching RTAX_GATEWAY.
728 *
729 * for RTM_GET, gate is optional even with multipath.
730 * if gate == NULL the first match is returned.
731 * (no need to call rt_mpath_matchgate if gate == NULL)
732 */
733 if (rn_mpath_capable(rnh) &&
734 (rtm->rtm_type != RTM_GET || info.rti_info[RTAX_GATEWAY])) {
735 rt = rt_mpath_matchgate(rt, info.rti_info[RTAX_GATEWAY]);
736 if (!rt) {
737 RADIX_NODE_HEAD_RUNLOCK(rnh);
738 senderr(ESRCH);
739 }
740 }
741#endif
742 /*
743 * If performing proxied L2 entry insertion, and
744 * the actual PPP host entry is found, perform
745 * another search to retrieve the prefix route of
746 * the local end point of the PPP link.
747 */
748 if (rtm->rtm_flags & RTF_ANNOUNCE) {
749 struct sockaddr laddr;
750
751 if (rt->rt_ifp != NULL &&
752 rt->rt_ifp->if_type == IFT_PROPVIRTUAL) {
753 struct ifaddr *ifa;
754
755 ifa = ifa_ifwithnet(info.rti_info[RTAX_DST], 1);
756 if (ifa != NULL)
757 rt_maskedcopy(ifa->ifa_addr,
758 &laddr,
759 ifa->ifa_netmask);
760 } else
761 rt_maskedcopy(rt->rt_ifa->ifa_addr,
762 &laddr,
763 rt->rt_ifa->ifa_netmask);
764 /*
765 * refactor rt and no lock operation necessary
766 */
767 rt = (struct rtentry *)rnh->rnh_matchaddr(&laddr, rnh);
768 if (rt == NULL) {
769 RADIX_NODE_HEAD_RUNLOCK(rnh);
770 senderr(ESRCH);
771 }
772 }
773 RT_LOCK(rt);
774 RT_ADDREF(rt);
775 RADIX_NODE_HEAD_RUNLOCK(rnh);
776
777report:
778 RT_LOCK_ASSERT(rt);
779 if ((rt->rt_flags & RTF_HOST) == 0
780 ? jailed_without_vnet(curthread->td_ucred)
781 : prison_if(curthread->td_ucred,
782 rt_key(rt)) != 0) {
783 RT_UNLOCK(rt);
784 senderr(ESRCH);
785 }
786 info.rti_info[RTAX_DST] = rt_key(rt);
787 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
788 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(rt_key(rt),
789 rt_mask(rt), &ss);
790 info.rti_info[RTAX_GENMASK] = 0;
791 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
792 ifp = rt->rt_ifp;
793 if (ifp) {
794 info.rti_info[RTAX_IFP] =
795 ifp->if_addr->ifa_addr;
796 error = rtm_get_jailed(&info, ifp, rt,
797 &saun, curthread->td_ucred);
798 if (error != 0) {
799 RT_UNLOCK(rt);
800 senderr(error);
801 }
802 if (ifp->if_flags & IFF_POINTOPOINT)
803 info.rti_info[RTAX_BRD] =
804 rt->rt_ifa->ifa_dstaddr;
805 rtm->rtm_index = ifp->if_index;
806 } else {
807 info.rti_info[RTAX_IFP] = NULL;
808 info.rti_info[RTAX_IFA] = NULL;
809 }
810 } else if ((ifp = rt->rt_ifp) != NULL) {
811 rtm->rtm_index = ifp->if_index;
812 }
813
814 /* Check if we need to realloc storage */
815 rtsock_msg_buffer(rtm->rtm_type, &info, NULL, &len);
816 if (len > alloc_len) {
817 struct rt_msghdr *new_rtm;
818 new_rtm = malloc(len, M_TEMP, M_NOWAIT);
819 if (new_rtm == NULL) {
820 RT_UNLOCK(rt);
821 senderr(ENOBUFS);
822 }
823 bcopy(rtm, new_rtm, rtm->rtm_msglen);
824 free(rtm, M_TEMP);
825 rtm = new_rtm;
826 alloc_len = len;
827 }
828
829 w.w_tmem = (caddr_t)rtm;
830 w.w_tmemsize = alloc_len;
831 rtsock_msg_buffer(rtm->rtm_type, &info, &w, &len);
832
833 if (rt->rt_flags & RTF_GWFLAG_COMPAT)
834 rtm->rtm_flags = RTF_GATEWAY |
835 (rt->rt_flags & ~RTF_GWFLAG_COMPAT);
836 else
837 rtm->rtm_flags = rt->rt_flags;
838 rt_getmetrics(rt, &rtm->rtm_rmx);
839 rtm->rtm_addrs = info.rti_addrs;
840
841 RT_UNLOCK(rt);
842 break;
843
844 default:
845 senderr(EOPNOTSUPP);
846 }
847
848flush:
849 if (rt != NULL)
850 RTFREE(rt);
851 /*
852 * Check to see if we don't want our own messages.
853 */
854 if ((so->so_options & SO_USELOOPBACK) == 0) {
855 if (V_route_cb.any_count <= 1) {
856 if (rtm != NULL)
857 free(rtm, M_TEMP);
858 m_freem(m);
859 return (error);
860 }
861 /* There is another listener, so construct message */
862 rp = sotorawcb(so);
863 }
864
865 if (rtm != NULL) {
866#ifdef INET6
867 if (rti_need_deembed) {
868 /* sin6_scope_id is recovered before sending rtm. */
869 sin6 = (struct sockaddr_in6 *)&ss;
870 for (i = 0; i < RTAX_MAX; i++) {
871 if (info.rti_info[i] == NULL)
872 continue;
873 if (info.rti_info[i]->sa_family != AF_INET6)
874 continue;
875 bcopy(info.rti_info[i], sin6, sizeof(*sin6));
876 if (sa6_recoverscope(sin6) == 0)
877 bcopy(sin6, info.rti_info[i],
878 sizeof(*sin6));
879 }
880 }
881#endif
882 if (error != 0)
883 rtm->rtm_errno = error;
884 else
885 rtm->rtm_flags |= RTF_DONE;
886
887 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
888 if (m->m_pkthdr.len < rtm->rtm_msglen) {
889 m_freem(m);
890 m = NULL;
891 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
892 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
893
894 free(rtm, M_TEMP);
895 }
896 if (m != NULL) {
897 M_SETFIB(m, fibnum);
898 m->m_flags |= RTS_FILTER_FIB;
899 if (rp) {
900 /*
901 * XXX insure we don't get a copy by
902 * invalidating our protocol
903 */
904 unsigned short family = rp->rcb_proto.sp_family;
905 rp->rcb_proto.sp_family = 0;
906 rt_dispatch(m, saf);
907 rp->rcb_proto.sp_family = family;
908 } else
909 rt_dispatch(m, saf);
910 }
911
912 return (error);
913}
914
915static void
916rt_getmetrics(const struct rtentry *rt, struct rt_metrics *out)
917{
918
919 bzero(out, sizeof(*out));
920 out->rmx_mtu = rt->rt_mtu;
921 out->rmx_weight = rt->rt_weight;
922 out->rmx_pksent = counter_u64_fetch(rt->rt_pksent);
923 /* Kernel -> userland timebase conversion. */
924 out->rmx_expire = rt->rt_expire ?
925 rt->rt_expire - time_uptime + time_second : 0;
926}
927
928/*
929 * Extract the addresses of the passed sockaddrs.
930 * Do a little sanity checking so as to avoid bad memory references.
931 * This data is derived straight from userland.
932 */
933static int
934rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
935{
936 struct sockaddr *sa;
937 int i;
938
939 for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
940 if ((rtinfo->rti_addrs & (1 << i)) == 0)
941 continue;
942 sa = (struct sockaddr *)cp;
943 /*
944 * It won't fit.
945 */
946 if (cp + sa->sa_len > cplim)
947 return (EINVAL);
948 /*
949 * there are no more.. quit now
950 * If there are more bits, they are in error.
951 * I've seen this. route(1) can evidently generate these.
952 * This causes kernel to core dump.
953 * for compatibility, If we see this, point to a safe address.
954 */
955 if (sa->sa_len == 0) {
956 rtinfo->rti_info[i] = &sa_zero;
957 return (0); /* should be EINVAL but for compat */
958 }
959 /* accept it */
960#ifdef INET6
961 if (sa->sa_family == AF_INET6)
962 sa6_embedscope((struct sockaddr_in6 *)sa,
963 V_ip6_use_defzone);
964#endif
965 rtinfo->rti_info[i] = sa;
966 cp += SA_SIZE(sa);
967 }
968 return (0);
969}
970
971/*
972 * Fill in @dmask with valid netmask leaving original @smask
973 * intact. Mostly used with radix netmasks.
974 */
975static struct sockaddr *
976rtsock_fix_netmask(struct sockaddr *dst, struct sockaddr *smask,
977 struct sockaddr_storage *dmask)
978{
979 if (dst == NULL || smask == NULL)
980 return (NULL);
981
982 memset(dmask, 0, dst->sa_len);
983 memcpy(dmask, smask, smask->sa_len);
984 dmask->ss_len = dst->sa_len;
985 dmask->ss_family = dst->sa_family;
986
987 return ((struct sockaddr *)dmask);
988}
989
990/*
991 * Writes information related to @rtinfo object to newly-allocated mbuf.
992 * Assumes MCLBYTES is enough to construct any message.
993 * Used for OS notifications of vaious events (if/ifa announces,etc)
994 *
995 * Returns allocated mbuf or NULL on failure.
996 */
997static struct mbuf *
998rtsock_msg_mbuf(int type, struct rt_addrinfo *rtinfo)
999{
1000 struct rt_msghdr *rtm;
1001 struct mbuf *m;
1002 int i;
1003 struct sockaddr *sa;
1004#ifdef INET6
1005 struct sockaddr_storage ss;
1006 struct sockaddr_in6 *sin6;
1007#endif
1008 int len, dlen;
1009
1010 switch (type) {
1011
1012 case RTM_DELADDR:
1013 case RTM_NEWADDR:
1014 len = sizeof(struct ifa_msghdr);
1015 break;
1016
1017 case RTM_DELMADDR:
1018 case RTM_NEWMADDR:
1019 len = sizeof(struct ifma_msghdr);
1020 break;
1021
1022 case RTM_IFINFO:
1023 len = sizeof(struct if_msghdr);
1024 break;
1025
1026 case RTM_IFANNOUNCE:
1027 case RTM_IEEE80211:
1028 len = sizeof(struct if_announcemsghdr);
1029 break;
1030
1031 default:
1032 len = sizeof(struct rt_msghdr);
1033 }
1034
1035 /* XXXGL: can we use MJUMPAGESIZE cluster here? */
1036 KASSERT(len <= MCLBYTES, ("%s: message too big", __func__));
1037 if (len > MHLEN)
1038 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1039 else
1040 m = m_gethdr(M_NOWAIT, MT_DATA);
1041 if (m == NULL)
1042 return (m);
1043
1044 m->m_pkthdr.len = m->m_len = len;
1045 rtm = mtod(m, struct rt_msghdr *);
1046 bzero((caddr_t)rtm, len);
1047 for (i = 0; i < RTAX_MAX; i++) {
1048 if ((sa = rtinfo->rti_info[i]) == NULL)
1049 continue;
1050 rtinfo->rti_addrs |= (1 << i);
1051 dlen = SA_SIZE(sa);
1052#ifdef INET6
1053 if (V_deembed_scopeid && sa->sa_family == AF_INET6) {
1054 sin6 = (struct sockaddr_in6 *)&ss;
1055 bcopy(sa, sin6, sizeof(*sin6));
1056 if (sa6_recoverscope(sin6) == 0)
1057 sa = (struct sockaddr *)sin6;
1058 }
1059#endif
1060 m_copyback(m, len, dlen, (caddr_t)sa);
1061 len += dlen;
1062 }
1063 if (m->m_pkthdr.len != len) {
1064 m_freem(m);
1065 return (NULL);
1066 }
1067 rtm->rtm_msglen = len;
1068 rtm->rtm_version = RTM_VERSION;
1069 rtm->rtm_type = type;
1070 return (m);
1071}
1072
1073/*
1074 * Writes information related to @rtinfo object to preallocated buffer.
1075 * Stores needed size in @plen. If @w is NULL, calculates size without
1076 * writing.
1077 * Used for sysctl dumps and rtsock answers (RTM_DEL/RTM_GET) generation.
1078 *
1079 * Returns 0 on success.
1080 *
1081 */
1082static int
1083rtsock_msg_buffer(int type, struct rt_addrinfo *rtinfo, struct walkarg *w, int *plen)
1084{
1085 int i;
1086 int len, buflen = 0, dlen;
1087 caddr_t cp = NULL;
1088 struct rt_msghdr *rtm = NULL;
1089#ifdef INET6
1090 struct sockaddr_storage ss;
1091 struct sockaddr_in6 *sin6;
1092#endif
1093
1094 switch (type) {
1095
1096 case RTM_DELADDR:
1097 case RTM_NEWADDR:
1098 if (w != NULL && w->w_op == NET_RT_IFLISTL) {
1099#ifdef COMPAT_FREEBSD32
1100 if (w->w_req->flags & SCTL_MASK32)
1101 len = sizeof(struct ifa_msghdrl32);
1102 else
1103#endif
1104 len = sizeof(struct ifa_msghdrl);
1105 } else
1106 len = sizeof(struct ifa_msghdr);
1107 break;
1108
1109 case RTM_IFINFO:
1110#ifdef COMPAT_FREEBSD32
1111 if (w != NULL && w->w_req->flags & SCTL_MASK32) {
1112 if (w->w_op == NET_RT_IFLISTL)
1113 len = sizeof(struct if_msghdrl32);
1114 else
1115 len = sizeof(struct if_msghdr32);
1116 break;
1117 }
1118#endif
1119 if (w != NULL && w->w_op == NET_RT_IFLISTL)
1120 len = sizeof(struct if_msghdrl);
1121 else
1122 len = sizeof(struct if_msghdr);
1123 break;
1124
1125 case RTM_NEWMADDR:
1126 len = sizeof(struct ifma_msghdr);
1127 break;
1128
1129 default:
1130 len = sizeof(struct rt_msghdr);
1131 }
1132
1133 if (w != NULL) {
1134 rtm = (struct rt_msghdr *)w->w_tmem;
1135 buflen = w->w_tmemsize - len;
1136 cp = (caddr_t)w->w_tmem + len;
1137 }
1138
1139 rtinfo->rti_addrs = 0;
1140 for (i = 0; i < RTAX_MAX; i++) {
1141 struct sockaddr *sa;
1142
1143 if ((sa = rtinfo->rti_info[i]) == NULL)
1144 continue;
1145 rtinfo->rti_addrs |= (1 << i);
1146 dlen = SA_SIZE(sa);
1147 if (cp != NULL && buflen >= dlen) {
1148#ifdef INET6
1149 if (V_deembed_scopeid && sa->sa_family == AF_INET6) {
1150 sin6 = (struct sockaddr_in6 *)&ss;
1151 bcopy(sa, sin6, sizeof(*sin6));
1152 if (sa6_recoverscope(sin6) == 0)
1153 sa = (struct sockaddr *)sin6;
1154 }
1155#endif
1156 bcopy((caddr_t)sa, cp, (unsigned)dlen);
1157 cp += dlen;
1158 buflen -= dlen;
1159 } else if (cp != NULL) {
1160 /*
1161 * Buffer too small. Count needed size
1162 * and return with error.
1163 */
1164 cp = NULL;
1165 }
1166
1167 len += dlen;
1168 }
1169
1170 if (cp != NULL) {
1171 dlen = ALIGN(len) - len;
1172 if (buflen < dlen)
1173 cp = NULL;
1174 else
1175 buflen -= dlen;
1176 }
1177 len = ALIGN(len);
1178
1179 if (cp != NULL) {
1180 /* fill header iff buffer is large enough */
1181 rtm->rtm_version = RTM_VERSION;
1182 rtm->rtm_type = type;
1183 rtm->rtm_msglen = len;
1184 }
1185
1186 *plen = len;
1187
1188 if (w != NULL && cp == NULL)
1189 return (ENOBUFS);
1190
1191 return (0);
1192}
1193
1194/*
1195 * This routine is called to generate a message from the routing
1196 * socket indicating that a redirect has occured, a routing lookup
1197 * has failed, or that a protocol has detected timeouts to a particular
1198 * destination.
1199 */
1200void
1201rt_missmsg_fib(int type, struct rt_addrinfo *rtinfo, int flags, int error,
1202 int fibnum)
1203{
1204 struct rt_msghdr *rtm;
1205 struct mbuf *m;
1206 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
1207
1208 if (V_route_cb.any_count == 0)
1209 return;
1210 m = rtsock_msg_mbuf(type, rtinfo);
1211 if (m == NULL)
1212 return;
1213
1214 if (fibnum != RT_ALL_FIBS) {
1215 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out "
1216 "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs));
1217 M_SETFIB(m, fibnum);
1218 m->m_flags |= RTS_FILTER_FIB;
1219 }
1220
1221 rtm = mtod(m, struct rt_msghdr *);
1222 rtm->rtm_flags = RTF_DONE | flags;
1223 rtm->rtm_errno = error;
1224 rtm->rtm_addrs = rtinfo->rti_addrs;
1225 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1226}
1227
1228void
1229rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
1230{
1231
1232 rt_missmsg_fib(type, rtinfo, flags, error, RT_ALL_FIBS);
1233}
1234
1235/*
1236 * This routine is called to generate a message from the routing
1237 * socket indicating that the status of a network interface has changed.
1238 */
1239void
1240rt_ifmsg(struct ifnet *ifp)
1241{
1242 struct if_msghdr *ifm;
1243 struct mbuf *m;
1244 struct rt_addrinfo info;
1245
1246 if (V_route_cb.any_count == 0)
1247 return;
1248 bzero((caddr_t)&info, sizeof(info));
1249 m = rtsock_msg_mbuf(RTM_IFINFO, &info);
1250 if (m == NULL)
1251 return;
1252 ifm = mtod(m, struct if_msghdr *);
1253 ifm->ifm_index = ifp->if_index;
1254 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1255 ifm->ifm_data = ifp->if_data;
1256 ifm->ifm_addrs = 0;
1257 rt_dispatch(m, AF_UNSPEC);
1258}
1259
1260/*
1261 * Announce interface address arrival/withdraw.
1262 * Please do not call directly, use rt_addrmsg().
1263 * Assume input data to be valid.
1264 * Returns 0 on success.
1265 */
1266int
1267rtsock_addrmsg(int cmd, struct ifaddr *ifa, int fibnum)
1268{
1269 struct rt_addrinfo info;
1270 struct sockaddr *sa;
1271 int ncmd;
1272 struct mbuf *m;
1273 struct ifa_msghdr *ifam;
1274 struct ifnet *ifp = ifa->ifa_ifp;
1275 struct sockaddr_storage ss;
1276
1277 if (V_route_cb.any_count == 0)
1278 return (0);
1279
1280 ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
1281
1282 bzero((caddr_t)&info, sizeof(info));
1283 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1284 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1285 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(
1286 info.rti_info[RTAX_IFP], ifa->ifa_netmask, &ss);
1287 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1288 if ((m = rtsock_msg_mbuf(ncmd, &info)) == NULL)
1289 return (ENOBUFS);
1290 ifam = mtod(m, struct ifa_msghdr *);
1291 ifam->ifam_index = ifp->if_index;
1292 ifam->ifam_metric = ifa->ifa_metric;
1292 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1293 ifam->ifam_flags = ifa->ifa_flags;
1294 ifam->ifam_addrs = info.rti_addrs;
1295
1296 if (fibnum != RT_ALL_FIBS) {
1297 M_SETFIB(m, fibnum);
1298 m->m_flags |= RTS_FILTER_FIB;
1299 }
1300
1301 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1302
1303 return (0);
1304}
1305
1306/*
1307 * Announce route addition/removal.
1308 * Please do not call directly, use rt_routemsg().
1309 * Note that @rt data MAY be inconsistent/invalid:
1310 * if some userland app sends us "invalid" route message (invalid mask,
1311 * no dst, wrong address families, etc...) we need to pass it back
1312 * to app (and any other rtsock consumers) with rtm_errno field set to
1313 * non-zero value.
1314 *
1315 * Returns 0 on success.
1316 */
1317int
1318rtsock_routemsg(int cmd, struct ifnet *ifp, int error, struct rtentry *rt,
1319 int fibnum)
1320{
1321 struct rt_addrinfo info;
1322 struct sockaddr *sa;
1323 struct mbuf *m;
1324 struct rt_msghdr *rtm;
1325 struct sockaddr_storage ss;
1326
1327 if (V_route_cb.any_count == 0)
1328 return (0);
1329
1330 bzero((caddr_t)&info, sizeof(info));
1331 info.rti_info[RTAX_DST] = sa = rt_key(rt);
1332 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(sa, rt_mask(rt), &ss);
1333 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1334 if ((m = rtsock_msg_mbuf(cmd, &info)) == NULL)
1335 return (ENOBUFS);
1336 rtm = mtod(m, struct rt_msghdr *);
1337 rtm->rtm_index = ifp->if_index;
1338 rtm->rtm_flags |= rt->rt_flags;
1339 rtm->rtm_errno = error;
1340 rtm->rtm_addrs = info.rti_addrs;
1341
1342 if (fibnum != RT_ALL_FIBS) {
1343 M_SETFIB(m, fibnum);
1344 m->m_flags |= RTS_FILTER_FIB;
1345 }
1346
1347 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1348
1349 return (0);
1350}
1351
1352/*
1353 * This is the analogue to the rt_newaddrmsg which performs the same
1354 * function but for multicast group memberhips. This is easier since
1355 * there is no route state to worry about.
1356 */
1357void
1358rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
1359{
1360 struct rt_addrinfo info;
1361 struct mbuf *m = NULL;
1362 struct ifnet *ifp = ifma->ifma_ifp;
1363 struct ifma_msghdr *ifmam;
1364
1365 if (V_route_cb.any_count == 0)
1366 return;
1367
1368 bzero((caddr_t)&info, sizeof(info));
1369 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1370 info.rti_info[RTAX_IFP] = ifp ? ifp->if_addr->ifa_addr : NULL;
1371 /*
1372 * If a link-layer address is present, present it as a ``gateway''
1373 * (similarly to how ARP entries, e.g., are presented).
1374 */
1375 info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr;
1376 m = rtsock_msg_mbuf(cmd, &info);
1377 if (m == NULL)
1378 return;
1379 ifmam = mtod(m, struct ifma_msghdr *);
1380 KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n",
1381 __func__));
1382 ifmam->ifmam_index = ifp->if_index;
1383 ifmam->ifmam_addrs = info.rti_addrs;
1384 rt_dispatch(m, ifma->ifma_addr ? ifma->ifma_addr->sa_family : AF_UNSPEC);
1385}
1386
1387static struct mbuf *
1388rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1389 struct rt_addrinfo *info)
1390{
1391 struct if_announcemsghdr *ifan;
1392 struct mbuf *m;
1393
1394 if (V_route_cb.any_count == 0)
1395 return NULL;
1396 bzero((caddr_t)info, sizeof(*info));
1397 m = rtsock_msg_mbuf(type, info);
1398 if (m != NULL) {
1399 ifan = mtod(m, struct if_announcemsghdr *);
1400 ifan->ifan_index = ifp->if_index;
1401 strlcpy(ifan->ifan_name, ifp->if_xname,
1402 sizeof(ifan->ifan_name));
1403 ifan->ifan_what = what;
1404 }
1405 return m;
1406}
1407
1408/*
1409 * This is called to generate routing socket messages indicating
1410 * IEEE80211 wireless events.
1411 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1412 */
1413void
1414rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
1415{
1416 struct mbuf *m;
1417 struct rt_addrinfo info;
1418
1419 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1420 if (m != NULL) {
1421 /*
1422 * Append the ieee80211 data. Try to stick it in the
1423 * mbuf containing the ifannounce msg; otherwise allocate
1424 * a new mbuf and append.
1425 *
1426 * NB: we assume m is a single mbuf.
1427 */
1428 if (data_len > M_TRAILINGSPACE(m)) {
1429 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
1430 if (n == NULL) {
1431 m_freem(m);
1432 return;
1433 }
1434 bcopy(data, mtod(n, void *), data_len);
1435 n->m_len = data_len;
1436 m->m_next = n;
1437 } else if (data_len > 0) {
1438 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
1439 m->m_len += data_len;
1440 }
1441 if (m->m_flags & M_PKTHDR)
1442 m->m_pkthdr.len += data_len;
1443 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
1444 rt_dispatch(m, AF_UNSPEC);
1445 }
1446}
1447
1448/*
1449 * This is called to generate routing socket messages indicating
1450 * network interface arrival and departure.
1451 */
1452void
1453rt_ifannouncemsg(struct ifnet *ifp, int what)
1454{
1455 struct mbuf *m;
1456 struct rt_addrinfo info;
1457
1458 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
1459 if (m != NULL)
1460 rt_dispatch(m, AF_UNSPEC);
1461}
1462
1463static void
1464rt_dispatch(struct mbuf *m, sa_family_t saf)
1465{
1466 struct m_tag *tag;
1467
1468 /*
1469 * Preserve the family from the sockaddr, if any, in an m_tag for
1470 * use when injecting the mbuf into the routing socket buffer from
1471 * the netisr.
1472 */
1473 if (saf != AF_UNSPEC) {
1474 tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short),
1475 M_NOWAIT);
1476 if (tag == NULL) {
1477 m_freem(m);
1478 return;
1479 }
1480 *(unsigned short *)(tag + 1) = saf;
1481 m_tag_prepend(m, tag);
1482 }
1483#ifdef VIMAGE
1484 if (V_loif)
1485 m->m_pkthdr.rcvif = V_loif;
1486 else {
1487 m_freem(m);
1488 return;
1489 }
1490#endif
1491 netisr_queue(NETISR_ROUTE, m); /* mbuf is free'd on failure. */
1492}
1493
1494/*
1495 * This is used in dumping the kernel table via sysctl().
1496 */
1497static int
1498sysctl_dumpentry(struct radix_node *rn, void *vw)
1499{
1500 struct walkarg *w = vw;
1501 struct rtentry *rt = (struct rtentry *)rn;
1502 int error = 0, size;
1503 struct rt_addrinfo info;
1504 struct sockaddr_storage ss;
1505
1506 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1507 return 0;
1508 if ((rt->rt_flags & RTF_HOST) == 0
1509 ? jailed_without_vnet(w->w_req->td->td_ucred)
1510 : prison_if(w->w_req->td->td_ucred, rt_key(rt)) != 0)
1511 return (0);
1512 bzero((caddr_t)&info, sizeof(info));
1513 info.rti_info[RTAX_DST] = rt_key(rt);
1514 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1515 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(rt_key(rt),
1516 rt_mask(rt), &ss);
1517 info.rti_info[RTAX_GENMASK] = 0;
1518 if (rt->rt_ifp) {
1519 info.rti_info[RTAX_IFP] = rt->rt_ifp->if_addr->ifa_addr;
1520 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
1521 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1522 info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr;
1523 }
1524 if ((error = rtsock_msg_buffer(RTM_GET, &info, w, &size)) != 0)
1525 return (error);
1526 if (w->w_req && w->w_tmem) {
1527 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
1528
1529 if (rt->rt_flags & RTF_GWFLAG_COMPAT)
1530 rtm->rtm_flags = RTF_GATEWAY |
1531 (rt->rt_flags & ~RTF_GWFLAG_COMPAT);
1532 else
1533 rtm->rtm_flags = rt->rt_flags;
1534 rt_getmetrics(rt, &rtm->rtm_rmx);
1535 rtm->rtm_index = rt->rt_ifp->if_index;
1536 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
1537 rtm->rtm_addrs = info.rti_addrs;
1538 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
1539 return (error);
1540 }
1541 return (error);
1542}
1543
1544static int
1545sysctl_iflist_ifml(struct ifnet *ifp, struct rt_addrinfo *info,
1546 struct walkarg *w, int len)
1547{
1548 struct if_msghdrl *ifm;
1549 struct if_data *ifd;
1550
1551 ifm = (struct if_msghdrl *)w->w_tmem;
1552
1553#ifdef COMPAT_FREEBSD32
1554 if (w->w_req->flags & SCTL_MASK32) {
1555 struct if_msghdrl32 *ifm32;
1556
1557 ifm32 = (struct if_msghdrl32 *)ifm;
1558 ifm32->ifm_addrs = info->rti_addrs;
1559 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1560 ifm32->ifm_index = ifp->if_index;
1561 ifm32->_ifm_spare1 = 0;
1562 ifm32->ifm_len = sizeof(*ifm32);
1563 ifm32->ifm_data_off = offsetof(struct if_msghdrl32, ifm_data);
1564 ifd = &ifm32->ifm_data;
1565 } else
1566#endif
1567 {
1568 ifm->ifm_addrs = info->rti_addrs;
1569 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1570 ifm->ifm_index = ifp->if_index;
1571 ifm->_ifm_spare1 = 0;
1572 ifm->ifm_len = sizeof(*ifm);
1573 ifm->ifm_data_off = offsetof(struct if_msghdrl, ifm_data);
1574 ifd = &ifm->ifm_data;
1575 }
1576
1577 *ifd = ifp->if_data;
1578
1579 /* Some drivers still use ifqueue(9), add its stats. */
1580 ifd->ifi_oqdrops += ifp->if_snd.ifq_drops;
1581
1582 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1583}
1584
1585static int
1586sysctl_iflist_ifm(struct ifnet *ifp, struct rt_addrinfo *info,
1587 struct walkarg *w, int len)
1588{
1589 struct if_msghdr *ifm;
1590 struct if_data *ifd;
1591
1592 ifm = (struct if_msghdr *)w->w_tmem;
1593
1594#ifdef COMPAT_FREEBSD32
1595 if (w->w_req->flags & SCTL_MASK32) {
1596 struct if_msghdr32 *ifm32;
1597
1598 ifm32 = (struct if_msghdr32 *)ifm;
1599 ifm32->ifm_addrs = info->rti_addrs;
1600 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1601 ifm32->ifm_index = ifp->if_index;
1602 ifd = &ifm32->ifm_data;
1603 } else
1604#endif
1605 {
1606 ifm->ifm_addrs = info->rti_addrs;
1607 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1608 ifm->ifm_index = ifp->if_index;
1609 ifd = &ifm->ifm_data;
1610 }
1611
1612 *ifd = ifp->if_data;
1613
1614 /* Some drivers still use ifqueue(9), add its stats. */
1615 ifd->ifi_oqdrops += ifp->if_snd.ifq_drops;
1616
1617 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1618}
1619
1620static int
1621sysctl_iflist_ifaml(struct ifaddr *ifa, struct rt_addrinfo *info,
1622 struct walkarg *w, int len)
1623{
1624 struct ifa_msghdrl *ifam;
1625 struct if_data *ifd;
1626
1627 ifam = (struct ifa_msghdrl *)w->w_tmem;
1628
1629#ifdef COMPAT_FREEBSD32
1630 if (w->w_req->flags & SCTL_MASK32) {
1631 struct ifa_msghdrl32 *ifam32;
1632
1633 ifam32 = (struct ifa_msghdrl32 *)ifam;
1634 ifam32->ifam_addrs = info->rti_addrs;
1635 ifam32->ifam_flags = ifa->ifa_flags;
1636 ifam32->ifam_index = ifa->ifa_ifp->if_index;
1637 ifam32->_ifam_spare1 = 0;
1638 ifam32->ifam_len = sizeof(*ifam32);
1639 ifam32->ifam_data_off =
1640 offsetof(struct ifa_msghdrl32, ifam_data);
1293 ifam->ifam_flags = ifa->ifa_flags;
1294 ifam->ifam_addrs = info.rti_addrs;
1295
1296 if (fibnum != RT_ALL_FIBS) {
1297 M_SETFIB(m, fibnum);
1298 m->m_flags |= RTS_FILTER_FIB;
1299 }
1300
1301 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1302
1303 return (0);
1304}
1305
1306/*
1307 * Announce route addition/removal.
1308 * Please do not call directly, use rt_routemsg().
1309 * Note that @rt data MAY be inconsistent/invalid:
1310 * if some userland app sends us "invalid" route message (invalid mask,
1311 * no dst, wrong address families, etc...) we need to pass it back
1312 * to app (and any other rtsock consumers) with rtm_errno field set to
1313 * non-zero value.
1314 *
1315 * Returns 0 on success.
1316 */
1317int
1318rtsock_routemsg(int cmd, struct ifnet *ifp, int error, struct rtentry *rt,
1319 int fibnum)
1320{
1321 struct rt_addrinfo info;
1322 struct sockaddr *sa;
1323 struct mbuf *m;
1324 struct rt_msghdr *rtm;
1325 struct sockaddr_storage ss;
1326
1327 if (V_route_cb.any_count == 0)
1328 return (0);
1329
1330 bzero((caddr_t)&info, sizeof(info));
1331 info.rti_info[RTAX_DST] = sa = rt_key(rt);
1332 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(sa, rt_mask(rt), &ss);
1333 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1334 if ((m = rtsock_msg_mbuf(cmd, &info)) == NULL)
1335 return (ENOBUFS);
1336 rtm = mtod(m, struct rt_msghdr *);
1337 rtm->rtm_index = ifp->if_index;
1338 rtm->rtm_flags |= rt->rt_flags;
1339 rtm->rtm_errno = error;
1340 rtm->rtm_addrs = info.rti_addrs;
1341
1342 if (fibnum != RT_ALL_FIBS) {
1343 M_SETFIB(m, fibnum);
1344 m->m_flags |= RTS_FILTER_FIB;
1345 }
1346
1347 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1348
1349 return (0);
1350}
1351
1352/*
1353 * This is the analogue to the rt_newaddrmsg which performs the same
1354 * function but for multicast group memberhips. This is easier since
1355 * there is no route state to worry about.
1356 */
1357void
1358rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
1359{
1360 struct rt_addrinfo info;
1361 struct mbuf *m = NULL;
1362 struct ifnet *ifp = ifma->ifma_ifp;
1363 struct ifma_msghdr *ifmam;
1364
1365 if (V_route_cb.any_count == 0)
1366 return;
1367
1368 bzero((caddr_t)&info, sizeof(info));
1369 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1370 info.rti_info[RTAX_IFP] = ifp ? ifp->if_addr->ifa_addr : NULL;
1371 /*
1372 * If a link-layer address is present, present it as a ``gateway''
1373 * (similarly to how ARP entries, e.g., are presented).
1374 */
1375 info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr;
1376 m = rtsock_msg_mbuf(cmd, &info);
1377 if (m == NULL)
1378 return;
1379 ifmam = mtod(m, struct ifma_msghdr *);
1380 KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n",
1381 __func__));
1382 ifmam->ifmam_index = ifp->if_index;
1383 ifmam->ifmam_addrs = info.rti_addrs;
1384 rt_dispatch(m, ifma->ifma_addr ? ifma->ifma_addr->sa_family : AF_UNSPEC);
1385}
1386
1387static struct mbuf *
1388rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1389 struct rt_addrinfo *info)
1390{
1391 struct if_announcemsghdr *ifan;
1392 struct mbuf *m;
1393
1394 if (V_route_cb.any_count == 0)
1395 return NULL;
1396 bzero((caddr_t)info, sizeof(*info));
1397 m = rtsock_msg_mbuf(type, info);
1398 if (m != NULL) {
1399 ifan = mtod(m, struct if_announcemsghdr *);
1400 ifan->ifan_index = ifp->if_index;
1401 strlcpy(ifan->ifan_name, ifp->if_xname,
1402 sizeof(ifan->ifan_name));
1403 ifan->ifan_what = what;
1404 }
1405 return m;
1406}
1407
1408/*
1409 * This is called to generate routing socket messages indicating
1410 * IEEE80211 wireless events.
1411 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1412 */
1413void
1414rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
1415{
1416 struct mbuf *m;
1417 struct rt_addrinfo info;
1418
1419 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1420 if (m != NULL) {
1421 /*
1422 * Append the ieee80211 data. Try to stick it in the
1423 * mbuf containing the ifannounce msg; otherwise allocate
1424 * a new mbuf and append.
1425 *
1426 * NB: we assume m is a single mbuf.
1427 */
1428 if (data_len > M_TRAILINGSPACE(m)) {
1429 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
1430 if (n == NULL) {
1431 m_freem(m);
1432 return;
1433 }
1434 bcopy(data, mtod(n, void *), data_len);
1435 n->m_len = data_len;
1436 m->m_next = n;
1437 } else if (data_len > 0) {
1438 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
1439 m->m_len += data_len;
1440 }
1441 if (m->m_flags & M_PKTHDR)
1442 m->m_pkthdr.len += data_len;
1443 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
1444 rt_dispatch(m, AF_UNSPEC);
1445 }
1446}
1447
1448/*
1449 * This is called to generate routing socket messages indicating
1450 * network interface arrival and departure.
1451 */
1452void
1453rt_ifannouncemsg(struct ifnet *ifp, int what)
1454{
1455 struct mbuf *m;
1456 struct rt_addrinfo info;
1457
1458 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
1459 if (m != NULL)
1460 rt_dispatch(m, AF_UNSPEC);
1461}
1462
1463static void
1464rt_dispatch(struct mbuf *m, sa_family_t saf)
1465{
1466 struct m_tag *tag;
1467
1468 /*
1469 * Preserve the family from the sockaddr, if any, in an m_tag for
1470 * use when injecting the mbuf into the routing socket buffer from
1471 * the netisr.
1472 */
1473 if (saf != AF_UNSPEC) {
1474 tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short),
1475 M_NOWAIT);
1476 if (tag == NULL) {
1477 m_freem(m);
1478 return;
1479 }
1480 *(unsigned short *)(tag + 1) = saf;
1481 m_tag_prepend(m, tag);
1482 }
1483#ifdef VIMAGE
1484 if (V_loif)
1485 m->m_pkthdr.rcvif = V_loif;
1486 else {
1487 m_freem(m);
1488 return;
1489 }
1490#endif
1491 netisr_queue(NETISR_ROUTE, m); /* mbuf is free'd on failure. */
1492}
1493
1494/*
1495 * This is used in dumping the kernel table via sysctl().
1496 */
1497static int
1498sysctl_dumpentry(struct radix_node *rn, void *vw)
1499{
1500 struct walkarg *w = vw;
1501 struct rtentry *rt = (struct rtentry *)rn;
1502 int error = 0, size;
1503 struct rt_addrinfo info;
1504 struct sockaddr_storage ss;
1505
1506 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1507 return 0;
1508 if ((rt->rt_flags & RTF_HOST) == 0
1509 ? jailed_without_vnet(w->w_req->td->td_ucred)
1510 : prison_if(w->w_req->td->td_ucred, rt_key(rt)) != 0)
1511 return (0);
1512 bzero((caddr_t)&info, sizeof(info));
1513 info.rti_info[RTAX_DST] = rt_key(rt);
1514 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1515 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(rt_key(rt),
1516 rt_mask(rt), &ss);
1517 info.rti_info[RTAX_GENMASK] = 0;
1518 if (rt->rt_ifp) {
1519 info.rti_info[RTAX_IFP] = rt->rt_ifp->if_addr->ifa_addr;
1520 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
1521 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1522 info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr;
1523 }
1524 if ((error = rtsock_msg_buffer(RTM_GET, &info, w, &size)) != 0)
1525 return (error);
1526 if (w->w_req && w->w_tmem) {
1527 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
1528
1529 if (rt->rt_flags & RTF_GWFLAG_COMPAT)
1530 rtm->rtm_flags = RTF_GATEWAY |
1531 (rt->rt_flags & ~RTF_GWFLAG_COMPAT);
1532 else
1533 rtm->rtm_flags = rt->rt_flags;
1534 rt_getmetrics(rt, &rtm->rtm_rmx);
1535 rtm->rtm_index = rt->rt_ifp->if_index;
1536 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
1537 rtm->rtm_addrs = info.rti_addrs;
1538 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
1539 return (error);
1540 }
1541 return (error);
1542}
1543
1544static int
1545sysctl_iflist_ifml(struct ifnet *ifp, struct rt_addrinfo *info,
1546 struct walkarg *w, int len)
1547{
1548 struct if_msghdrl *ifm;
1549 struct if_data *ifd;
1550
1551 ifm = (struct if_msghdrl *)w->w_tmem;
1552
1553#ifdef COMPAT_FREEBSD32
1554 if (w->w_req->flags & SCTL_MASK32) {
1555 struct if_msghdrl32 *ifm32;
1556
1557 ifm32 = (struct if_msghdrl32 *)ifm;
1558 ifm32->ifm_addrs = info->rti_addrs;
1559 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1560 ifm32->ifm_index = ifp->if_index;
1561 ifm32->_ifm_spare1 = 0;
1562 ifm32->ifm_len = sizeof(*ifm32);
1563 ifm32->ifm_data_off = offsetof(struct if_msghdrl32, ifm_data);
1564 ifd = &ifm32->ifm_data;
1565 } else
1566#endif
1567 {
1568 ifm->ifm_addrs = info->rti_addrs;
1569 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1570 ifm->ifm_index = ifp->if_index;
1571 ifm->_ifm_spare1 = 0;
1572 ifm->ifm_len = sizeof(*ifm);
1573 ifm->ifm_data_off = offsetof(struct if_msghdrl, ifm_data);
1574 ifd = &ifm->ifm_data;
1575 }
1576
1577 *ifd = ifp->if_data;
1578
1579 /* Some drivers still use ifqueue(9), add its stats. */
1580 ifd->ifi_oqdrops += ifp->if_snd.ifq_drops;
1581
1582 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1583}
1584
1585static int
1586sysctl_iflist_ifm(struct ifnet *ifp, struct rt_addrinfo *info,
1587 struct walkarg *w, int len)
1588{
1589 struct if_msghdr *ifm;
1590 struct if_data *ifd;
1591
1592 ifm = (struct if_msghdr *)w->w_tmem;
1593
1594#ifdef COMPAT_FREEBSD32
1595 if (w->w_req->flags & SCTL_MASK32) {
1596 struct if_msghdr32 *ifm32;
1597
1598 ifm32 = (struct if_msghdr32 *)ifm;
1599 ifm32->ifm_addrs = info->rti_addrs;
1600 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1601 ifm32->ifm_index = ifp->if_index;
1602 ifd = &ifm32->ifm_data;
1603 } else
1604#endif
1605 {
1606 ifm->ifm_addrs = info->rti_addrs;
1607 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1608 ifm->ifm_index = ifp->if_index;
1609 ifd = &ifm->ifm_data;
1610 }
1611
1612 *ifd = ifp->if_data;
1613
1614 /* Some drivers still use ifqueue(9), add its stats. */
1615 ifd->ifi_oqdrops += ifp->if_snd.ifq_drops;
1616
1617 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1618}
1619
1620static int
1621sysctl_iflist_ifaml(struct ifaddr *ifa, struct rt_addrinfo *info,
1622 struct walkarg *w, int len)
1623{
1624 struct ifa_msghdrl *ifam;
1625 struct if_data *ifd;
1626
1627 ifam = (struct ifa_msghdrl *)w->w_tmem;
1628
1629#ifdef COMPAT_FREEBSD32
1630 if (w->w_req->flags & SCTL_MASK32) {
1631 struct ifa_msghdrl32 *ifam32;
1632
1633 ifam32 = (struct ifa_msghdrl32 *)ifam;
1634 ifam32->ifam_addrs = info->rti_addrs;
1635 ifam32->ifam_flags = ifa->ifa_flags;
1636 ifam32->ifam_index = ifa->ifa_ifp->if_index;
1637 ifam32->_ifam_spare1 = 0;
1638 ifam32->ifam_len = sizeof(*ifam32);
1639 ifam32->ifam_data_off =
1640 offsetof(struct ifa_msghdrl32, ifam_data);
1641 ifam32->ifam_metric = ifa->ifa_metric;
1641 ifam32->ifam_metric = ifa->ifa_ifp->if_metric;
1642 ifd = &ifam32->ifam_data;
1643 } else
1644#endif
1645 {
1646 ifam->ifam_addrs = info->rti_addrs;
1647 ifam->ifam_flags = ifa->ifa_flags;
1648 ifam->ifam_index = ifa->ifa_ifp->if_index;
1649 ifam->_ifam_spare1 = 0;
1650 ifam->ifam_len = sizeof(*ifam);
1651 ifam->ifam_data_off = offsetof(struct ifa_msghdrl, ifam_data);
1642 ifd = &ifam32->ifam_data;
1643 } else
1644#endif
1645 {
1646 ifam->ifam_addrs = info->rti_addrs;
1647 ifam->ifam_flags = ifa->ifa_flags;
1648 ifam->ifam_index = ifa->ifa_ifp->if_index;
1649 ifam->_ifam_spare1 = 0;
1650 ifam->ifam_len = sizeof(*ifam);
1651 ifam->ifam_data_off = offsetof(struct ifa_msghdrl, ifam_data);
1652 ifam->ifam_metric = ifa->ifa_metric;
1652 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1653 ifd = &ifam->ifam_data;
1654 }
1655
1656 bzero(ifd, sizeof(*ifd));
1657 ifd->ifi_datalen = sizeof(struct if_data);
1658 ifd->ifi_ipackets = counter_u64_fetch(ifa->ifa_ipackets);
1659 ifd->ifi_opackets = counter_u64_fetch(ifa->ifa_opackets);
1660 ifd->ifi_ibytes = counter_u64_fetch(ifa->ifa_ibytes);
1661 ifd->ifi_obytes = counter_u64_fetch(ifa->ifa_obytes);
1662
1663 /* Fixup if_data carp(4) vhid. */
1664 if (carp_get_vhid_p != NULL)
1665 ifd->ifi_vhid = (*carp_get_vhid_p)(ifa);
1666
1667 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1668}
1669
1670static int
1671sysctl_iflist_ifam(struct ifaddr *ifa, struct rt_addrinfo *info,
1672 struct walkarg *w, int len)
1673{
1674 struct ifa_msghdr *ifam;
1675
1676 ifam = (struct ifa_msghdr *)w->w_tmem;
1677 ifam->ifam_addrs = info->rti_addrs;
1678 ifam->ifam_flags = ifa->ifa_flags;
1679 ifam->ifam_index = ifa->ifa_ifp->if_index;
1653 ifd = &ifam->ifam_data;
1654 }
1655
1656 bzero(ifd, sizeof(*ifd));
1657 ifd->ifi_datalen = sizeof(struct if_data);
1658 ifd->ifi_ipackets = counter_u64_fetch(ifa->ifa_ipackets);
1659 ifd->ifi_opackets = counter_u64_fetch(ifa->ifa_opackets);
1660 ifd->ifi_ibytes = counter_u64_fetch(ifa->ifa_ibytes);
1661 ifd->ifi_obytes = counter_u64_fetch(ifa->ifa_obytes);
1662
1663 /* Fixup if_data carp(4) vhid. */
1664 if (carp_get_vhid_p != NULL)
1665 ifd->ifi_vhid = (*carp_get_vhid_p)(ifa);
1666
1667 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1668}
1669
1670static int
1671sysctl_iflist_ifam(struct ifaddr *ifa, struct rt_addrinfo *info,
1672 struct walkarg *w, int len)
1673{
1674 struct ifa_msghdr *ifam;
1675
1676 ifam = (struct ifa_msghdr *)w->w_tmem;
1677 ifam->ifam_addrs = info->rti_addrs;
1678 ifam->ifam_flags = ifa->ifa_flags;
1679 ifam->ifam_index = ifa->ifa_ifp->if_index;
1680 ifam->ifam_metric = ifa->ifa_metric;
1680 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1681
1682 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1683}
1684
1685static int
1686sysctl_iflist(int af, struct walkarg *w)
1687{
1688 struct ifnet *ifp;
1689 struct ifaddr *ifa;
1690 struct rt_addrinfo info;
1691 int len, error = 0;
1692 struct sockaddr_storage ss;
1693
1694 bzero((caddr_t)&info, sizeof(info));
1695 IFNET_RLOCK_NOSLEEP();
1696 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1697 if (w->w_arg && w->w_arg != ifp->if_index)
1698 continue;
1699 IF_ADDR_RLOCK(ifp);
1700 ifa = ifp->if_addr;
1701 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
1702 error = rtsock_msg_buffer(RTM_IFINFO, &info, w, &len);
1703 if (error != 0)
1704 goto done;
1705 info.rti_info[RTAX_IFP] = NULL;
1706 if (w->w_req && w->w_tmem) {
1707 if (w->w_op == NET_RT_IFLISTL)
1708 error = sysctl_iflist_ifml(ifp, &info, w, len);
1709 else
1710 error = sysctl_iflist_ifm(ifp, &info, w, len);
1711 if (error)
1712 goto done;
1713 }
1714 while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != NULL) {
1715 if (af && af != ifa->ifa_addr->sa_family)
1716 continue;
1717 if (prison_if(w->w_req->td->td_ucred,
1718 ifa->ifa_addr) != 0)
1719 continue;
1720 info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1721 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(
1722 ifa->ifa_addr, ifa->ifa_netmask, &ss);
1723 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1724 error = rtsock_msg_buffer(RTM_NEWADDR, &info, w, &len);
1725 if (error != 0)
1726 goto done;
1727 if (w->w_req && w->w_tmem) {
1728 if (w->w_op == NET_RT_IFLISTL)
1729 error = sysctl_iflist_ifaml(ifa, &info,
1730 w, len);
1731 else
1732 error = sysctl_iflist_ifam(ifa, &info,
1733 w, len);
1734 if (error)
1735 goto done;
1736 }
1737 }
1738 IF_ADDR_RUNLOCK(ifp);
1739 info.rti_info[RTAX_IFA] = NULL;
1740 info.rti_info[RTAX_NETMASK] = NULL;
1741 info.rti_info[RTAX_BRD] = NULL;
1742 }
1743done:
1744 if (ifp != NULL)
1745 IF_ADDR_RUNLOCK(ifp);
1746 IFNET_RUNLOCK_NOSLEEP();
1747 return (error);
1748}
1749
1750static int
1751sysctl_ifmalist(int af, struct walkarg *w)
1752{
1753 struct ifnet *ifp;
1754 struct ifmultiaddr *ifma;
1755 struct rt_addrinfo info;
1756 int len, error = 0;
1757 struct ifaddr *ifa;
1758
1759 bzero((caddr_t)&info, sizeof(info));
1760 IFNET_RLOCK_NOSLEEP();
1761 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1762 if (w->w_arg && w->w_arg != ifp->if_index)
1763 continue;
1764 ifa = ifp->if_addr;
1765 info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL;
1766 IF_ADDR_RLOCK(ifp);
1767 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1768 if (af && af != ifma->ifma_addr->sa_family)
1769 continue;
1770 if (prison_if(w->w_req->td->td_ucred,
1771 ifma->ifma_addr) != 0)
1772 continue;
1773 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1774 info.rti_info[RTAX_GATEWAY] =
1775 (ifma->ifma_addr->sa_family != AF_LINK) ?
1776 ifma->ifma_lladdr : NULL;
1777 error = rtsock_msg_buffer(RTM_NEWMADDR, &info, w, &len);
1778 if (error != 0)
1779 goto done;
1780 if (w->w_req && w->w_tmem) {
1781 struct ifma_msghdr *ifmam;
1782
1783 ifmam = (struct ifma_msghdr *)w->w_tmem;
1784 ifmam->ifmam_index = ifma->ifma_ifp->if_index;
1785 ifmam->ifmam_flags = 0;
1786 ifmam->ifmam_addrs = info.rti_addrs;
1787 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
1788 if (error) {
1789 IF_ADDR_RUNLOCK(ifp);
1790 goto done;
1791 }
1792 }
1793 }
1794 IF_ADDR_RUNLOCK(ifp);
1795 }
1796done:
1797 IFNET_RUNLOCK_NOSLEEP();
1798 return (error);
1799}
1800
1801static int
1802sysctl_rtsock(SYSCTL_HANDLER_ARGS)
1803{
1804 int *name = (int *)arg1;
1805 u_int namelen = arg2;
1806 struct radix_node_head *rnh = NULL; /* silence compiler. */
1807 int i, lim, error = EINVAL;
1808 int fib = 0;
1809 u_char af;
1810 struct walkarg w;
1811
1812 name ++;
1813 namelen--;
1814 if (req->newptr)
1815 return (EPERM);
1816 if (name[1] == NET_RT_DUMP) {
1817 if (namelen == 3)
1818 fib = req->td->td_proc->p_fibnum;
1819 else if (namelen == 4)
1820 fib = (name[3] == RT_ALL_FIBS) ?
1821 req->td->td_proc->p_fibnum : name[3];
1822 else
1823 return ((namelen < 3) ? EISDIR : ENOTDIR);
1824 if (fib < 0 || fib >= rt_numfibs)
1825 return (EINVAL);
1826 } else if (namelen != 3)
1827 return ((namelen < 3) ? EISDIR : ENOTDIR);
1828 af = name[0];
1829 if (af > AF_MAX)
1830 return (EINVAL);
1831 bzero(&w, sizeof(w));
1832 w.w_op = name[1];
1833 w.w_arg = name[2];
1834 w.w_req = req;
1835
1836 error = sysctl_wire_old_buffer(req, 0);
1837 if (error)
1838 return (error);
1839
1840 /*
1841 * Allocate reply buffer in advance.
1842 * All rtsock messages has maximum length of u_short.
1843 */
1844 w.w_tmemsize = 65536;
1845 w.w_tmem = malloc(w.w_tmemsize, M_TEMP, M_WAITOK);
1846
1847 switch (w.w_op) {
1848
1849 case NET_RT_DUMP:
1850 case NET_RT_FLAGS:
1851 if (af == 0) { /* dump all tables */
1852 i = 1;
1853 lim = AF_MAX;
1854 } else /* dump only one table */
1855 i = lim = af;
1856
1857 /*
1858 * take care of llinfo entries, the caller must
1859 * specify an AF
1860 */
1861 if (w.w_op == NET_RT_FLAGS &&
1862 (w.w_arg == 0 || w.w_arg & RTF_LLINFO)) {
1863 if (af != 0)
1864 error = lltable_sysctl_dumparp(af, w.w_req);
1865 else
1866 error = EINVAL;
1867 break;
1868 }
1869 /*
1870 * take care of routing entries
1871 */
1872 for (error = 0; error == 0 && i <= lim; i++) {
1873 rnh = rt_tables_get_rnh(fib, i);
1874 if (rnh != NULL) {
1875 RADIX_NODE_HEAD_RLOCK(rnh);
1876 error = rnh->rnh_walktree(rnh,
1877 sysctl_dumpentry, &w);
1878 RADIX_NODE_HEAD_RUNLOCK(rnh);
1879 } else if (af != 0)
1880 error = EAFNOSUPPORT;
1881 }
1882 break;
1883
1884 case NET_RT_IFLIST:
1885 case NET_RT_IFLISTL:
1886 error = sysctl_iflist(af, &w);
1887 break;
1888
1889 case NET_RT_IFMALIST:
1890 error = sysctl_ifmalist(af, &w);
1891 break;
1892 }
1893
1894 free(w.w_tmem, M_TEMP);
1895 return (error);
1896}
1897
1898static SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
1899
1900/*
1901 * Definitions of protocols supported in the ROUTE domain.
1902 */
1903
1904static struct domain routedomain; /* or at least forward */
1905
1906static struct protosw routesw[] = {
1907{
1908 .pr_type = SOCK_RAW,
1909 .pr_domain = &routedomain,
1910 .pr_flags = PR_ATOMIC|PR_ADDR,
1911 .pr_output = route_output,
1912 .pr_ctlinput = raw_ctlinput,
1913 .pr_init = raw_init,
1914 .pr_usrreqs = &route_usrreqs
1915}
1916};
1917
1918static struct domain routedomain = {
1919 .dom_family = PF_ROUTE,
1920 .dom_name = "route",
1921 .dom_protosw = routesw,
1922 .dom_protoswNPROTOSW = &routesw[sizeof(routesw)/sizeof(routesw[0])]
1923};
1924
1925VNET_DOMAIN_SET(route);
1681
1682 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1683}
1684
1685static int
1686sysctl_iflist(int af, struct walkarg *w)
1687{
1688 struct ifnet *ifp;
1689 struct ifaddr *ifa;
1690 struct rt_addrinfo info;
1691 int len, error = 0;
1692 struct sockaddr_storage ss;
1693
1694 bzero((caddr_t)&info, sizeof(info));
1695 IFNET_RLOCK_NOSLEEP();
1696 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1697 if (w->w_arg && w->w_arg != ifp->if_index)
1698 continue;
1699 IF_ADDR_RLOCK(ifp);
1700 ifa = ifp->if_addr;
1701 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
1702 error = rtsock_msg_buffer(RTM_IFINFO, &info, w, &len);
1703 if (error != 0)
1704 goto done;
1705 info.rti_info[RTAX_IFP] = NULL;
1706 if (w->w_req && w->w_tmem) {
1707 if (w->w_op == NET_RT_IFLISTL)
1708 error = sysctl_iflist_ifml(ifp, &info, w, len);
1709 else
1710 error = sysctl_iflist_ifm(ifp, &info, w, len);
1711 if (error)
1712 goto done;
1713 }
1714 while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != NULL) {
1715 if (af && af != ifa->ifa_addr->sa_family)
1716 continue;
1717 if (prison_if(w->w_req->td->td_ucred,
1718 ifa->ifa_addr) != 0)
1719 continue;
1720 info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1721 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(
1722 ifa->ifa_addr, ifa->ifa_netmask, &ss);
1723 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1724 error = rtsock_msg_buffer(RTM_NEWADDR, &info, w, &len);
1725 if (error != 0)
1726 goto done;
1727 if (w->w_req && w->w_tmem) {
1728 if (w->w_op == NET_RT_IFLISTL)
1729 error = sysctl_iflist_ifaml(ifa, &info,
1730 w, len);
1731 else
1732 error = sysctl_iflist_ifam(ifa, &info,
1733 w, len);
1734 if (error)
1735 goto done;
1736 }
1737 }
1738 IF_ADDR_RUNLOCK(ifp);
1739 info.rti_info[RTAX_IFA] = NULL;
1740 info.rti_info[RTAX_NETMASK] = NULL;
1741 info.rti_info[RTAX_BRD] = NULL;
1742 }
1743done:
1744 if (ifp != NULL)
1745 IF_ADDR_RUNLOCK(ifp);
1746 IFNET_RUNLOCK_NOSLEEP();
1747 return (error);
1748}
1749
1750static int
1751sysctl_ifmalist(int af, struct walkarg *w)
1752{
1753 struct ifnet *ifp;
1754 struct ifmultiaddr *ifma;
1755 struct rt_addrinfo info;
1756 int len, error = 0;
1757 struct ifaddr *ifa;
1758
1759 bzero((caddr_t)&info, sizeof(info));
1760 IFNET_RLOCK_NOSLEEP();
1761 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1762 if (w->w_arg && w->w_arg != ifp->if_index)
1763 continue;
1764 ifa = ifp->if_addr;
1765 info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL;
1766 IF_ADDR_RLOCK(ifp);
1767 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1768 if (af && af != ifma->ifma_addr->sa_family)
1769 continue;
1770 if (prison_if(w->w_req->td->td_ucred,
1771 ifma->ifma_addr) != 0)
1772 continue;
1773 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1774 info.rti_info[RTAX_GATEWAY] =
1775 (ifma->ifma_addr->sa_family != AF_LINK) ?
1776 ifma->ifma_lladdr : NULL;
1777 error = rtsock_msg_buffer(RTM_NEWMADDR, &info, w, &len);
1778 if (error != 0)
1779 goto done;
1780 if (w->w_req && w->w_tmem) {
1781 struct ifma_msghdr *ifmam;
1782
1783 ifmam = (struct ifma_msghdr *)w->w_tmem;
1784 ifmam->ifmam_index = ifma->ifma_ifp->if_index;
1785 ifmam->ifmam_flags = 0;
1786 ifmam->ifmam_addrs = info.rti_addrs;
1787 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
1788 if (error) {
1789 IF_ADDR_RUNLOCK(ifp);
1790 goto done;
1791 }
1792 }
1793 }
1794 IF_ADDR_RUNLOCK(ifp);
1795 }
1796done:
1797 IFNET_RUNLOCK_NOSLEEP();
1798 return (error);
1799}
1800
1801static int
1802sysctl_rtsock(SYSCTL_HANDLER_ARGS)
1803{
1804 int *name = (int *)arg1;
1805 u_int namelen = arg2;
1806 struct radix_node_head *rnh = NULL; /* silence compiler. */
1807 int i, lim, error = EINVAL;
1808 int fib = 0;
1809 u_char af;
1810 struct walkarg w;
1811
1812 name ++;
1813 namelen--;
1814 if (req->newptr)
1815 return (EPERM);
1816 if (name[1] == NET_RT_DUMP) {
1817 if (namelen == 3)
1818 fib = req->td->td_proc->p_fibnum;
1819 else if (namelen == 4)
1820 fib = (name[3] == RT_ALL_FIBS) ?
1821 req->td->td_proc->p_fibnum : name[3];
1822 else
1823 return ((namelen < 3) ? EISDIR : ENOTDIR);
1824 if (fib < 0 || fib >= rt_numfibs)
1825 return (EINVAL);
1826 } else if (namelen != 3)
1827 return ((namelen < 3) ? EISDIR : ENOTDIR);
1828 af = name[0];
1829 if (af > AF_MAX)
1830 return (EINVAL);
1831 bzero(&w, sizeof(w));
1832 w.w_op = name[1];
1833 w.w_arg = name[2];
1834 w.w_req = req;
1835
1836 error = sysctl_wire_old_buffer(req, 0);
1837 if (error)
1838 return (error);
1839
1840 /*
1841 * Allocate reply buffer in advance.
1842 * All rtsock messages has maximum length of u_short.
1843 */
1844 w.w_tmemsize = 65536;
1845 w.w_tmem = malloc(w.w_tmemsize, M_TEMP, M_WAITOK);
1846
1847 switch (w.w_op) {
1848
1849 case NET_RT_DUMP:
1850 case NET_RT_FLAGS:
1851 if (af == 0) { /* dump all tables */
1852 i = 1;
1853 lim = AF_MAX;
1854 } else /* dump only one table */
1855 i = lim = af;
1856
1857 /*
1858 * take care of llinfo entries, the caller must
1859 * specify an AF
1860 */
1861 if (w.w_op == NET_RT_FLAGS &&
1862 (w.w_arg == 0 || w.w_arg & RTF_LLINFO)) {
1863 if (af != 0)
1864 error = lltable_sysctl_dumparp(af, w.w_req);
1865 else
1866 error = EINVAL;
1867 break;
1868 }
1869 /*
1870 * take care of routing entries
1871 */
1872 for (error = 0; error == 0 && i <= lim; i++) {
1873 rnh = rt_tables_get_rnh(fib, i);
1874 if (rnh != NULL) {
1875 RADIX_NODE_HEAD_RLOCK(rnh);
1876 error = rnh->rnh_walktree(rnh,
1877 sysctl_dumpentry, &w);
1878 RADIX_NODE_HEAD_RUNLOCK(rnh);
1879 } else if (af != 0)
1880 error = EAFNOSUPPORT;
1881 }
1882 break;
1883
1884 case NET_RT_IFLIST:
1885 case NET_RT_IFLISTL:
1886 error = sysctl_iflist(af, &w);
1887 break;
1888
1889 case NET_RT_IFMALIST:
1890 error = sysctl_ifmalist(af, &w);
1891 break;
1892 }
1893
1894 free(w.w_tmem, M_TEMP);
1895 return (error);
1896}
1897
1898static SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
1899
1900/*
1901 * Definitions of protocols supported in the ROUTE domain.
1902 */
1903
1904static struct domain routedomain; /* or at least forward */
1905
1906static struct protosw routesw[] = {
1907{
1908 .pr_type = SOCK_RAW,
1909 .pr_domain = &routedomain,
1910 .pr_flags = PR_ATOMIC|PR_ADDR,
1911 .pr_output = route_output,
1912 .pr_ctlinput = raw_ctlinput,
1913 .pr_init = raw_init,
1914 .pr_usrreqs = &route_usrreqs
1915}
1916};
1917
1918static struct domain routedomain = {
1919 .dom_family = PF_ROUTE,
1920 .dom_name = "route",
1921 .dom_protosw = routesw,
1922 .dom_protoswNPROTOSW = &routesw[sizeof(routesw)/sizeof(routesw[0])]
1923};
1924
1925VNET_DOMAIN_SET(route);