1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1988, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * @(#)rtsock.c 8.7 (Berkeley) 10/12/95
32 */
33 #include "opt_ddb.h"
34 #include "opt_route.h"
35 #include "opt_inet.h"
36 #include "opt_inet6.h"
37
38 #include <sys/param.h>
39 #include <sys/jail.h>
40 #include <sys/kernel.h>
41 #include <sys/eventhandler.h>
42 #include <sys/domain.h>
43 #include <sys/lock.h>
44 #include <sys/malloc.h>
45 #include <sys/mbuf.h>
46 #include <sys/priv.h>
47 #include <sys/proc.h>
48 #include <sys/protosw.h>
49 #include <sys/rmlock.h>
50 #include <sys/rwlock.h>
51 #include <sys/signalvar.h>
52 #include <sys/socket.h>
53 #include <sys/socketvar.h>
54 #include <sys/sysctl.h>
55 #include <sys/systm.h>
56
57 #include <net/if.h>
58 #include <net/if_var.h>
59 #include <net/if_private.h>
60 #include <net/if_dl.h>
61 #include <net/if_llatbl.h>
62 #include <net/if_types.h>
63 #include <net/netisr.h>
64 #include <net/route.h>
65 #include <net/route/route_ctl.h>
66 #include <net/route/route_var.h>
67 #include <net/vnet.h>
68
69 #include <netinet/in.h>
70 #include <netinet/if_ether.h>
71 #include <netinet/ip_carp.h>
72 #ifdef INET6
73 #include <netinet6/in6_var.h>
74 #include <netinet6/ip6_var.h>
75 #include <netinet6/scope6_var.h>
76 #endif
77 #include <net/route/nhop.h>
78
79 #define DEBUG_MOD_NAME rtsock
80 #define DEBUG_MAX_LEVEL LOG_DEBUG
81 #include <net/route/route_debug.h>
82 _DECLARE_DEBUG(LOG_INFO);
83
84 #ifdef COMPAT_FREEBSD32
85 #include <sys/mount.h>
86 #include <compat/freebsd32/freebsd32.h>
87
88 struct if_msghdr32 {
89 uint16_t ifm_msglen;
90 uint8_t ifm_version;
91 uint8_t ifm_type;
92 int32_t ifm_addrs;
93 int32_t ifm_flags;
94 uint16_t ifm_index;
95 uint16_t _ifm_spare1;
96 struct if_data ifm_data;
97 };
98
99 struct if_msghdrl32 {
100 uint16_t ifm_msglen;
101 uint8_t ifm_version;
102 uint8_t ifm_type;
103 int32_t ifm_addrs;
104 int32_t ifm_flags;
105 uint16_t ifm_index;
106 uint16_t _ifm_spare1;
107 uint16_t ifm_len;
108 uint16_t ifm_data_off;
109 uint32_t _ifm_spare2;
110 struct if_data ifm_data;
111 };
112
113 struct ifa_msghdrl32 {
114 uint16_t ifam_msglen;
115 uint8_t ifam_version;
116 uint8_t ifam_type;
117 int32_t ifam_addrs;
118 int32_t ifam_flags;
119 uint16_t ifam_index;
120 uint16_t _ifam_spare1;
121 uint16_t ifam_len;
122 uint16_t ifam_data_off;
123 int32_t ifam_metric;
124 struct if_data ifam_data;
125 };
126
127 #define SA_SIZE32(sa) \
128 ( (((struct sockaddr *)(sa))->sa_len == 0) ? \
129 sizeof(int) : \
130 1 + ( (((struct sockaddr *)(sa))->sa_len - 1) | (sizeof(int) - 1) ) )
131
132 #endif /* COMPAT_FREEBSD32 */
133
134 struct linear_buffer {
135 char *base; /* Base allocated memory pointer */
136 uint32_t offset; /* Currently used offset */
137 uint32_t size; /* Total buffer size */
138 };
139 #define SCRATCH_BUFFER_SIZE 1024
140
141 #define RTS_PID_LOG(_l, _fmt, ...) \
142 RT_LOG_##_l(_l, "PID %d: " _fmt, curproc ? curproc->p_pid : 0, \
143 ## __VA_ARGS__)
144
145 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
146
147 /* NB: these are not modified */
148 static struct sockaddr route_src = { 2, PF_ROUTE, };
149 static struct sockaddr sa_zero = { sizeof(sa_zero), AF_INET, };
150
151 /* These are external hooks for CARP. */
152 int (*carp_get_vhid_p)(struct ifaddr *);
153
154 /*
155 * Used by rtsock callback code to decide whether to filter the update
156 * notification to a socket bound to a particular FIB.
157 */
158 #define RTS_FILTER_FIB M_PROTO8
159 /*
160 * Used to store address family of the notification.
161 */
162 #define m_rtsock_family m_pkthdr.PH_loc.eight[0]
163
164 struct rcb {
165 LIST_ENTRY(rcb) list;
166 struct socket *rcb_socket;
167 sa_family_t rcb_family;
168 };
169
170 typedef struct {
171 LIST_HEAD(, rcb) cblist;
172 int ip_count; /* attached w/ AF_INET */
173 int ip6_count; /* attached w/ AF_INET6 */
174 int any_count; /* total attached */
175 } route_cb_t;
176 VNET_DEFINE_STATIC(route_cb_t, route_cb);
177 #define V_route_cb VNET(route_cb)
178
179 struct mtx rtsock_mtx;
180 MTX_SYSINIT(rtsock, &rtsock_mtx, "rtsock route_cb lock", MTX_DEF);
181
182 #define RTSOCK_LOCK() mtx_lock(&rtsock_mtx)
183 #define RTSOCK_UNLOCK() mtx_unlock(&rtsock_mtx)
184 #define RTSOCK_LOCK_ASSERT() mtx_assert(&rtsock_mtx, MA_OWNED)
185
186 SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
187
188 struct walkarg {
189 int family;
190 int w_tmemsize;
191 int w_op, w_arg;
192 caddr_t w_tmem;
193 struct sysctl_req *w_req;
194 struct sockaddr *dst;
195 struct sockaddr *mask;
196 };
197
198 static void rts_input(struct mbuf *m);
199 static struct mbuf *rtsock_msg_mbuf(int type, struct rt_addrinfo *rtinfo);
200 static int rtsock_msg_buffer(int type, struct rt_addrinfo *rtinfo,
201 struct walkarg *w, int *plen);
202 static int rt_xaddrs(caddr_t cp, caddr_t cplim,
203 struct rt_addrinfo *rtinfo);
204 static int cleanup_xaddrs(struct rt_addrinfo *info, struct linear_buffer *lb);
205 static int sysctl_dumpentry(struct rtentry *rt, void *vw);
206 static int sysctl_dumpnhop(struct rtentry *rt, struct nhop_object *nh,
207 uint32_t weight, struct walkarg *w);
208 static int sysctl_iflist(int af, struct walkarg *w);
209 static int sysctl_ifmalist(int af, struct walkarg *w);
210 static void rt_getmetrics(const struct rtentry *rt,
211 const struct nhop_object *nh, struct rt_metrics *out);
212 static void rt_dispatch(struct mbuf *, sa_family_t);
213 static void rt_ifannouncemsg(struct ifnet *ifp, int what);
214 static int handle_rtm_get(struct rt_addrinfo *info, u_int fibnum,
215 struct rt_msghdr *rtm, struct rib_cmd_info *rc);
216 static int update_rtm_from_rc(struct rt_addrinfo *info,
217 struct rt_msghdr **prtm, int alloc_len,
218 struct rib_cmd_info *rc, struct nhop_object *nh);
219 static void send_rtm_reply(struct socket *so, struct rt_msghdr *rtm,
220 struct mbuf *m, sa_family_t saf, u_int fibnum,
221 int rtm_errno);
222 static void rtsock_notify_event(uint32_t fibnum, const struct rib_cmd_info *rc);
223 static void rtsock_ifmsg(struct ifnet *ifp, int if_flags_mask);
224
225 static struct netisr_handler rtsock_nh = {
226 .nh_name = "rtsock",
227 .nh_handler = rts_input,
228 .nh_proto = NETISR_ROUTE,
229 .nh_policy = NETISR_POLICY_SOURCE,
230 };
231
232 static int
sysctl_route_netisr_maxqlen(SYSCTL_HANDLER_ARGS)233 sysctl_route_netisr_maxqlen(SYSCTL_HANDLER_ARGS)
234 {
235 int error, qlimit;
236
237 netisr_getqlimit(&rtsock_nh, &qlimit);
238 error = sysctl_handle_int(oidp, &qlimit, 0, req);
239 if (error || !req->newptr)
240 return (error);
241 if (qlimit < 1)
242 return (EINVAL);
243 return (netisr_setqlimit(&rtsock_nh, qlimit));
244 }
245 SYSCTL_PROC(_net_route, OID_AUTO, netisr_maxqlen,
246 CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NOFETCH | CTLFLAG_MPSAFE,
247 0, 0, sysctl_route_netisr_maxqlen, "I",
248 "maximum routing socket dispatch queue length");
249
250 static void
vnet_rts_init(void)251 vnet_rts_init(void)
252 {
253 int tmp;
254
255 if (IS_DEFAULT_VNET(curvnet)) {
256 if (TUNABLE_INT_FETCH("net.route.netisr_maxqlen", &tmp))
257 rtsock_nh.nh_qlimit = tmp;
258 netisr_register(&rtsock_nh);
259 }
260 #ifdef VIMAGE
261 else
262 netisr_register_vnet(&rtsock_nh);
263 #endif
264 }
265 VNET_SYSINIT(vnet_rtsock, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD,
266 vnet_rts_init, 0);
267
268 #ifdef VIMAGE
269 static void
vnet_rts_uninit(void)270 vnet_rts_uninit(void)
271 {
272
273 netisr_unregister_vnet(&rtsock_nh);
274 }
275 VNET_SYSUNINIT(vnet_rts_uninit, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD,
276 vnet_rts_uninit, 0);
277 #endif
278
279 static void
report_route_event(const struct rib_cmd_info * rc,void * _cbdata)280 report_route_event(const struct rib_cmd_info *rc, void *_cbdata)
281 {
282 uint32_t fibnum = (uint32_t)(uintptr_t)_cbdata;
283 struct nhop_object *nh;
284
285 nh = rc->rc_cmd == RTM_DELETE ? rc->rc_nh_old : rc->rc_nh_new;
286 rt_routemsg(rc->rc_cmd, rc->rc_rt, nh, fibnum);
287 }
288
289 static void
rts_handle_route_event(uint32_t fibnum,const struct rib_cmd_info * rc)290 rts_handle_route_event(uint32_t fibnum, const struct rib_cmd_info *rc)
291 {
292 #ifdef ROUTE_MPATH
293 if ((rc->rc_nh_new && NH_IS_NHGRP(rc->rc_nh_new)) ||
294 (rc->rc_nh_old && NH_IS_NHGRP(rc->rc_nh_old))) {
295 rib_decompose_notification(rc, report_route_event,
296 (void *)(uintptr_t)fibnum);
297 } else
298 #endif
299 report_route_event(rc, (void *)(uintptr_t)fibnum);
300 }
301 static struct rtbridge rtsbridge = {
302 .route_f = rts_handle_route_event,
303 .ifmsg_f = rtsock_ifmsg,
304 };
305 static struct rtbridge *rtsbridge_orig_p;
306
307 static void
rtsock_notify_event(uint32_t fibnum,const struct rib_cmd_info * rc)308 rtsock_notify_event(uint32_t fibnum, const struct rib_cmd_info *rc)
309 {
310 netlink_callback_p->route_f(fibnum, rc);
311 }
312
313 static void
rtsock_init(void)314 rtsock_init(void)
315 {
316 rtsbridge_orig_p = rtsock_callback_p;
317 rtsock_callback_p = &rtsbridge;
318 }
319 SYSINIT(rtsock_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, rtsock_init, NULL);
320
321 static void
rts_handle_ifnet_arrival(void * arg __unused,struct ifnet * ifp)322 rts_handle_ifnet_arrival(void *arg __unused, struct ifnet *ifp)
323 {
324 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
325 }
326 EVENTHANDLER_DEFINE(ifnet_arrival_event, rts_handle_ifnet_arrival, NULL, 0);
327
328 static void
rts_handle_ifnet_departure(void * arg __unused,struct ifnet * ifp)329 rts_handle_ifnet_departure(void *arg __unused, struct ifnet *ifp)
330 {
331 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
332 }
333 EVENTHANDLER_DEFINE(ifnet_departure_event, rts_handle_ifnet_departure, NULL, 0);
334
335 static void
rts_append_data(struct socket * so,struct mbuf * m)336 rts_append_data(struct socket *so, struct mbuf *m)
337 {
338
339 if (sbappendaddr(&so->so_rcv, &route_src, m, NULL) == 0) {
340 soroverflow(so);
341 m_freem(m);
342 } else
343 sorwakeup(so);
344 }
345
346 static void
rts_input(struct mbuf * m)347 rts_input(struct mbuf *m)
348 {
349 struct rcb *rcb;
350 struct socket *last;
351
352 last = NULL;
353 RTSOCK_LOCK();
354 LIST_FOREACH(rcb, &V_route_cb.cblist, list) {
355 if (rcb->rcb_family != AF_UNSPEC &&
356 rcb->rcb_family != m->m_rtsock_family)
357 continue;
358 if ((m->m_flags & RTS_FILTER_FIB) &&
359 M_GETFIB(m) != rcb->rcb_socket->so_fibnum)
360 continue;
361 if (last != NULL) {
362 struct mbuf *n;
363
364 n = m_copym(m, 0, M_COPYALL, M_NOWAIT);
365 if (n != NULL)
366 rts_append_data(last, n);
367 }
368 last = rcb->rcb_socket;
369 }
370 if (last != NULL)
371 rts_append_data(last, m);
372 else
373 m_freem(m);
374 RTSOCK_UNLOCK();
375 }
376
377 static void
rts_close(struct socket * so)378 rts_close(struct socket *so)
379 {
380
381 soisdisconnected(so);
382 }
383
384 static SYSCTL_NODE(_net, OID_AUTO, rtsock, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
385 "Routing socket infrastructure");
386 static u_long rts_sendspace = 8192;
387 SYSCTL_ULONG(_net_rtsock, OID_AUTO, sendspace, CTLFLAG_RW, &rts_sendspace, 0,
388 "Default routing socket send space");
389 static u_long rts_recvspace = 8192;
390 SYSCTL_ULONG(_net_rtsock, OID_AUTO, recvspace, CTLFLAG_RW, &rts_recvspace, 0,
391 "Default routing socket receive space");
392
393 static int
rts_attach(struct socket * so,int proto,struct thread * td)394 rts_attach(struct socket *so, int proto, struct thread *td)
395 {
396 struct rcb *rcb;
397 int error;
398
399 error = soreserve(so, rts_sendspace, rts_recvspace);
400 if (error)
401 return (error);
402
403 rcb = malloc(sizeof(*rcb), M_PCB, M_WAITOK);
404 rcb->rcb_socket = so;
405 rcb->rcb_family = proto;
406
407 so->so_pcb = rcb;
408 so->so_fibnum = td->td_proc->p_fibnum;
409 so->so_options |= SO_USELOOPBACK;
410
411 RTSOCK_LOCK();
412 LIST_INSERT_HEAD(&V_route_cb.cblist, rcb, list);
413 switch (proto) {
414 case AF_INET:
415 V_route_cb.ip_count++;
416 break;
417 case AF_INET6:
418 V_route_cb.ip6_count++;
419 break;
420 }
421 V_route_cb.any_count++;
422 RTSOCK_UNLOCK();
423 soisconnected(so);
424
425 return (0);
426 }
427
428 static void
rts_detach(struct socket * so)429 rts_detach(struct socket *so)
430 {
431 struct rcb *rcb = so->so_pcb;
432
433 RTSOCK_LOCK();
434 LIST_REMOVE(rcb, list);
435 switch(rcb->rcb_family) {
436 case AF_INET:
437 V_route_cb.ip_count--;
438 break;
439 case AF_INET6:
440 V_route_cb.ip6_count--;
441 break;
442 }
443 V_route_cb.any_count--;
444 RTSOCK_UNLOCK();
445 free(rcb, M_PCB);
446 so->so_pcb = NULL;
447 }
448
449 static int
rts_disconnect(struct socket * so)450 rts_disconnect(struct socket *so)
451 {
452
453 return (ENOTCONN);
454 }
455
456 static int
rts_shutdown(struct socket * so)457 rts_shutdown(struct socket *so)
458 {
459
460 socantsendmore(so);
461 return (0);
462 }
463
464 #ifndef _SOCKADDR_UNION_DEFINED
465 #define _SOCKADDR_UNION_DEFINED
466 /*
467 * The union of all possible address formats we handle.
468 */
469 union sockaddr_union {
470 struct sockaddr sa;
471 struct sockaddr_in sin;
472 struct sockaddr_in6 sin6;
473 };
474 #endif /* _SOCKADDR_UNION_DEFINED */
475
476 static int
rtm_get_jailed(struct rt_addrinfo * info,struct ifnet * ifp,struct nhop_object * nh,union sockaddr_union * saun,struct ucred * cred)477 rtm_get_jailed(struct rt_addrinfo *info, struct ifnet *ifp,
478 struct nhop_object *nh, union sockaddr_union *saun, struct ucred *cred)
479 {
480 #if defined(INET) || defined(INET6)
481 struct epoch_tracker et;
482 #endif
483
484 /* First, see if the returned address is part of the jail. */
485 if (prison_if(cred, nh->nh_ifa->ifa_addr) == 0) {
486 info->rti_info[RTAX_IFA] = nh->nh_ifa->ifa_addr;
487 return (0);
488 }
489
490 switch (info->rti_info[RTAX_DST]->sa_family) {
491 #ifdef INET
492 case AF_INET:
493 {
494 struct in_addr ia;
495 struct ifaddr *ifa;
496 int found;
497
498 found = 0;
499 /*
500 * Try to find an address on the given outgoing interface
501 * that belongs to the jail.
502 */
503 NET_EPOCH_ENTER(et);
504 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
505 struct sockaddr *sa;
506 sa = ifa->ifa_addr;
507 if (sa->sa_family != AF_INET)
508 continue;
509 ia = ((struct sockaddr_in *)sa)->sin_addr;
510 if (prison_check_ip4(cred, &ia) == 0) {
511 found = 1;
512 break;
513 }
514 }
515 NET_EPOCH_EXIT(et);
516 if (!found) {
517 /*
518 * As a last resort return the 'default' jail address.
519 */
520 ia = ((struct sockaddr_in *)nh->nh_ifa->ifa_addr)->
521 sin_addr;
522 if (prison_get_ip4(cred, &ia) != 0)
523 return (ESRCH);
524 }
525 bzero(&saun->sin, sizeof(struct sockaddr_in));
526 saun->sin.sin_len = sizeof(struct sockaddr_in);
527 saun->sin.sin_family = AF_INET;
528 saun->sin.sin_addr.s_addr = ia.s_addr;
529 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin;
530 break;
531 }
532 #endif
533 #ifdef INET6
534 case AF_INET6:
535 {
536 struct in6_addr ia6;
537 struct ifaddr *ifa;
538 int found;
539
540 found = 0;
541 /*
542 * Try to find an address on the given outgoing interface
543 * that belongs to the jail.
544 */
545 NET_EPOCH_ENTER(et);
546 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
547 struct sockaddr *sa;
548 sa = ifa->ifa_addr;
549 if (sa->sa_family != AF_INET6)
550 continue;
551 bcopy(&((struct sockaddr_in6 *)sa)->sin6_addr,
552 &ia6, sizeof(struct in6_addr));
553 if (prison_check_ip6(cred, &ia6) == 0) {
554 found = 1;
555 break;
556 }
557 }
558 NET_EPOCH_EXIT(et);
559 if (!found) {
560 /*
561 * As a last resort return the 'default' jail address.
562 */
563 ia6 = ((struct sockaddr_in6 *)nh->nh_ifa->ifa_addr)->
564 sin6_addr;
565 if (prison_get_ip6(cred, &ia6) != 0)
566 return (ESRCH);
567 }
568 bzero(&saun->sin6, sizeof(struct sockaddr_in6));
569 saun->sin6.sin6_len = sizeof(struct sockaddr_in6);
570 saun->sin6.sin6_family = AF_INET6;
571 bcopy(&ia6, &saun->sin6.sin6_addr, sizeof(struct in6_addr));
572 if (sa6_recoverscope(&saun->sin6) != 0)
573 return (ESRCH);
574 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin6;
575 break;
576 }
577 #endif
578 default:
579 return (ESRCH);
580 }
581 return (0);
582 }
583
584 static int
fill_blackholeinfo(struct rt_addrinfo * info,union sockaddr_union * saun)585 fill_blackholeinfo(struct rt_addrinfo *info, union sockaddr_union *saun)
586 {
587 struct ifaddr *ifa;
588 sa_family_t saf;
589
590 if (V_loif == NULL) {
591 RTS_PID_LOG(LOG_INFO, "Unable to add blackhole/reject nhop without loopback");
592 return (ENOTSUP);
593 }
594 info->rti_ifp = V_loif;
595
596 saf = info->rti_info[RTAX_DST]->sa_family;
597
598 CK_STAILQ_FOREACH(ifa, &info->rti_ifp->if_addrhead, ifa_link) {
599 if (ifa->ifa_addr->sa_family == saf) {
600 info->rti_ifa = ifa;
601 break;
602 }
603 }
604 if (info->rti_ifa == NULL) {
605 RTS_PID_LOG(LOG_INFO, "Unable to find ifa for blackhole/reject nhop");
606 return (ENOTSUP);
607 }
608
609 bzero(saun, sizeof(union sockaddr_union));
610 switch (saf) {
611 #ifdef INET
612 case AF_INET:
613 saun->sin.sin_family = AF_INET;
614 saun->sin.sin_len = sizeof(struct sockaddr_in);
615 saun->sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
616 break;
617 #endif
618 #ifdef INET6
619 case AF_INET6:
620 saun->sin6.sin6_family = AF_INET6;
621 saun->sin6.sin6_len = sizeof(struct sockaddr_in6);
622 saun->sin6.sin6_addr = in6addr_loopback;
623 break;
624 #endif
625 default:
626 RTS_PID_LOG(LOG_INFO, "unsupported family: %d", saf);
627 return (ENOTSUP);
628 }
629 info->rti_info[RTAX_GATEWAY] = &saun->sa;
630 info->rti_flags |= RTF_GATEWAY;
631
632 return (0);
633 }
634
635 /*
636 * Fills in @info based on userland-provided @rtm message.
637 *
638 * Returns 0 on success.
639 */
640 static int
fill_addrinfo(struct rt_msghdr * rtm,int len,struct linear_buffer * lb,u_int fibnum,struct rt_addrinfo * info)641 fill_addrinfo(struct rt_msghdr *rtm, int len, struct linear_buffer *lb, u_int fibnum,
642 struct rt_addrinfo *info)
643 {
644 int error;
645
646 rtm->rtm_pid = curproc->p_pid;
647 info->rti_addrs = rtm->rtm_addrs;
648
649 info->rti_mflags = rtm->rtm_inits;
650 info->rti_rmx = &rtm->rtm_rmx;
651
652 /*
653 * rt_xaddrs() performs s6_addr[2] := sin6_scope_id for AF_INET6
654 * link-local address because rtrequest requires addresses with
655 * embedded scope id.
656 */
657 if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, info))
658 return (EINVAL);
659
660 info->rti_flags = rtm->rtm_flags;
661 error = cleanup_xaddrs(info, lb);
662 if (error != 0)
663 return (error);
664 /*
665 * Verify that the caller has the appropriate privilege; RTM_GET
666 * is the only operation the non-superuser is allowed.
667 */
668 if (rtm->rtm_type != RTM_GET) {
669 error = priv_check(curthread, PRIV_NET_ROUTE);
670 if (error != 0)
671 return (error);
672 }
673
674 /*
675 * The given gateway address may be an interface address.
676 * For example, issuing a "route change" command on a route
677 * entry that was created from a tunnel, and the gateway
678 * address given is the local end point. In this case the
679 * RTF_GATEWAY flag must be cleared or the destination will
680 * not be reachable even though there is no error message.
681 */
682 if (info->rti_info[RTAX_GATEWAY] != NULL &&
683 info->rti_info[RTAX_GATEWAY]->sa_family != AF_LINK) {
684 struct nhop_object *nh;
685
686 /*
687 * A host route through the loopback interface is
688 * installed for each interface address. In pre 8.0
689 * releases the interface address of a PPP link type
690 * is not reachable locally. This behavior is fixed as
691 * part of the new L2/L3 redesign and rewrite work. The
692 * signature of this interface address route is the
693 * AF_LINK sa_family type of the gateway, and the
694 * rt_ifp has the IFF_LOOPBACK flag set.
695 */
696 nh = rib_lookup(fibnum, info->rti_info[RTAX_GATEWAY], NHR_NONE, 0);
697 if (nh != NULL && nh->gw_sa.sa_family == AF_LINK &&
698 nh->nh_ifp->if_flags & IFF_LOOPBACK) {
699 info->rti_flags &= ~RTF_GATEWAY;
700 info->rti_flags |= RTF_GWFLAG_COMPAT;
701 }
702 }
703
704 return (0);
705 }
706
707 static struct nhop_object *
select_nhop(struct nhop_object * nh,const struct sockaddr * gw)708 select_nhop(struct nhop_object *nh, const struct sockaddr *gw)
709 {
710 if (!NH_IS_NHGRP(nh))
711 return (nh);
712 #ifdef ROUTE_MPATH
713 const struct weightened_nhop *wn;
714 uint32_t num_nhops;
715 wn = nhgrp_get_nhops((struct nhgrp_object *)nh, &num_nhops);
716 if (gw == NULL)
717 return (wn[0].nh);
718 for (int i = 0; i < num_nhops; i++) {
719 if (match_nhop_gw(wn[i].nh, gw))
720 return (wn[i].nh);
721 }
722 #endif
723 return (NULL);
724 }
725
726 /*
727 * Handles RTM_GET message from routing socket, returning matching rt.
728 *
729 * Returns:
730 * 0 on success, with locked and referenced matching rt in @rt_nrt
731 * errno of failure
732 */
733 static int
handle_rtm_get(struct rt_addrinfo * info,u_int fibnum,struct rt_msghdr * rtm,struct rib_cmd_info * rc)734 handle_rtm_get(struct rt_addrinfo *info, u_int fibnum,
735 struct rt_msghdr *rtm, struct rib_cmd_info *rc)
736 {
737 RIB_RLOCK_TRACKER;
738 struct rib_head *rnh;
739 struct nhop_object *nh;
740 sa_family_t saf;
741
742 saf = info->rti_info[RTAX_DST]->sa_family;
743
744 rnh = rt_tables_get_rnh(fibnum, saf);
745 if (rnh == NULL)
746 return (EAFNOSUPPORT);
747
748 RIB_RLOCK(rnh);
749
750 /*
751 * By (implicit) convention host route (one without netmask)
752 * means longest-prefix-match request and the route with netmask
753 * means exact-match lookup.
754 * As cleanup_xaddrs() cleans up info flags&addrs for the /32,/128
755 * prefixes, use original data to check for the netmask presence.
756 */
757 if ((rtm->rtm_addrs & RTA_NETMASK) == 0) {
758 /*
759 * Provide longest prefix match for
760 * address lookup (no mask).
761 * 'route -n get addr'
762 */
763 rc->rc_rt = (struct rtentry *) rnh->rnh_matchaddr(
764 info->rti_info[RTAX_DST], &rnh->head);
765 } else
766 rc->rc_rt = (struct rtentry *) rnh->rnh_lookup(
767 info->rti_info[RTAX_DST],
768 info->rti_info[RTAX_NETMASK], &rnh->head);
769
770 if (rc->rc_rt == NULL) {
771 RIB_RUNLOCK(rnh);
772 return (ESRCH);
773 }
774
775 nh = select_nhop(rt_get_raw_nhop(rc->rc_rt), info->rti_info[RTAX_GATEWAY]);
776 if (nh == NULL) {
777 RIB_RUNLOCK(rnh);
778 return (ESRCH);
779 }
780 /*
781 * If performing proxied L2 entry insertion, and
782 * the actual PPP host entry is found, perform
783 * another search to retrieve the prefix route of
784 * the local end point of the PPP link.
785 * TODO: move this logic to userland.
786 */
787 if (rtm->rtm_flags & RTF_ANNOUNCE) {
788 struct sockaddr_storage laddr;
789
790 if (nh->nh_ifp != NULL &&
791 nh->nh_ifp->if_type == IFT_PROPVIRTUAL) {
792 struct ifaddr *ifa;
793
794 ifa = ifa_ifwithnet(info->rti_info[RTAX_DST], 1,
795 RT_ALL_FIBS);
796 if (ifa != NULL)
797 rt_maskedcopy(ifa->ifa_addr,
798 (struct sockaddr *)&laddr,
799 ifa->ifa_netmask);
800 } else
801 rt_maskedcopy(nh->nh_ifa->ifa_addr,
802 (struct sockaddr *)&laddr,
803 nh->nh_ifa->ifa_netmask);
804 /*
805 * refactor rt and no lock operation necessary
806 */
807 rc->rc_rt = (struct rtentry *)rnh->rnh_matchaddr(
808 (struct sockaddr *)&laddr, &rnh->head);
809 if (rc->rc_rt == NULL) {
810 RIB_RUNLOCK(rnh);
811 return (ESRCH);
812 }
813 nh = select_nhop(rt_get_raw_nhop(rc->rc_rt), info->rti_info[RTAX_GATEWAY]);
814 if (nh == NULL) {
815 RIB_RUNLOCK(rnh);
816 return (ESRCH);
817 }
818 }
819 rc->rc_nh_new = nh;
820 rc->rc_nh_weight = rc->rc_rt->rt_weight;
821 RIB_RUNLOCK(rnh);
822
823 return (0);
824 }
825
826 static void
init_sockaddrs_family(int family,struct sockaddr * dst,struct sockaddr * mask)827 init_sockaddrs_family(int family, struct sockaddr *dst, struct sockaddr *mask)
828 {
829 #ifdef INET
830 if (family == AF_INET) {
831 struct sockaddr_in *dst4 = (struct sockaddr_in *)dst;
832 struct sockaddr_in *mask4 = (struct sockaddr_in *)mask;
833
834 bzero(dst4, sizeof(struct sockaddr_in));
835 bzero(mask4, sizeof(struct sockaddr_in));
836
837 dst4->sin_family = AF_INET;
838 dst4->sin_len = sizeof(struct sockaddr_in);
839 mask4->sin_family = AF_INET;
840 mask4->sin_len = sizeof(struct sockaddr_in);
841 }
842 #endif
843 #ifdef INET6
844 if (family == AF_INET6) {
845 struct sockaddr_in6 *dst6 = (struct sockaddr_in6 *)dst;
846 struct sockaddr_in6 *mask6 = (struct sockaddr_in6 *)mask;
847
848 bzero(dst6, sizeof(struct sockaddr_in6));
849 bzero(mask6, sizeof(struct sockaddr_in6));
850
851 dst6->sin6_family = AF_INET6;
852 dst6->sin6_len = sizeof(struct sockaddr_in6);
853 mask6->sin6_family = AF_INET6;
854 mask6->sin6_len = sizeof(struct sockaddr_in6);
855 }
856 #endif
857 }
858
859 static void
export_rtaddrs(const struct rtentry * rt,struct sockaddr * dst,struct sockaddr * mask)860 export_rtaddrs(const struct rtentry *rt, struct sockaddr *dst,
861 struct sockaddr *mask)
862 {
863 #ifdef INET
864 if (dst->sa_family == AF_INET) {
865 struct sockaddr_in *dst4 = (struct sockaddr_in *)dst;
866 struct sockaddr_in *mask4 = (struct sockaddr_in *)mask;
867 uint32_t scopeid = 0;
868 rt_get_inet_prefix_pmask(rt, &dst4->sin_addr, &mask4->sin_addr,
869 &scopeid);
870 return;
871 }
872 #endif
873 #ifdef INET6
874 if (dst->sa_family == AF_INET6) {
875 struct sockaddr_in6 *dst6 = (struct sockaddr_in6 *)dst;
876 struct sockaddr_in6 *mask6 = (struct sockaddr_in6 *)mask;
877 uint32_t scopeid = 0;
878 rt_get_inet6_prefix_pmask(rt, &dst6->sin6_addr,
879 &mask6->sin6_addr, &scopeid);
880 dst6->sin6_scope_id = scopeid;
881 return;
882 }
883 #endif
884 }
885
886 static int
update_rtm_from_info(struct rt_addrinfo * info,struct rt_msghdr ** prtm,int alloc_len)887 update_rtm_from_info(struct rt_addrinfo *info, struct rt_msghdr **prtm,
888 int alloc_len)
889 {
890 struct rt_msghdr *rtm, *orig_rtm = NULL;
891 struct walkarg w;
892 int len;
893
894 rtm = *prtm;
895 /* Check if we need to realloc storage */
896 rtsock_msg_buffer(rtm->rtm_type, info, NULL, &len);
897 if (len > alloc_len) {
898 struct rt_msghdr *tmp_rtm;
899
900 tmp_rtm = malloc(len, M_TEMP, M_NOWAIT);
901 if (tmp_rtm == NULL)
902 return (ENOBUFS);
903 bcopy(rtm, tmp_rtm, rtm->rtm_msglen);
904 orig_rtm = rtm;
905 rtm = tmp_rtm;
906 alloc_len = len;
907
908 /*
909 * Delay freeing original rtm as info contains
910 * data referencing it.
911 */
912 }
913
914 w.w_tmem = (caddr_t)rtm;
915 w.w_tmemsize = alloc_len;
916 rtsock_msg_buffer(rtm->rtm_type, info, &w, &len);
917 rtm->rtm_addrs = info->rti_addrs;
918
919 if (orig_rtm != NULL)
920 free(orig_rtm, M_TEMP);
921 *prtm = rtm;
922 return (0);
923 }
924
925
926 /*
927 * Update sockaddrs, flags, etc in @prtm based on @rc data.
928 * rtm can be reallocated.
929 *
930 * Returns 0 on success, along with pointer to (potentially reallocated)
931 * rtm.
932 *
933 */
934 static int
update_rtm_from_rc(struct rt_addrinfo * info,struct rt_msghdr ** prtm,int alloc_len,struct rib_cmd_info * rc,struct nhop_object * nh)935 update_rtm_from_rc(struct rt_addrinfo *info, struct rt_msghdr **prtm,
936 int alloc_len, struct rib_cmd_info *rc, struct nhop_object *nh)
937 {
938 union sockaddr_union saun;
939 struct rt_msghdr *rtm;
940 struct ifnet *ifp;
941 int error;
942
943 rtm = *prtm;
944 union sockaddr_union sa_dst, sa_mask;
945 int family = info->rti_info[RTAX_DST]->sa_family;
946 init_sockaddrs_family(family, &sa_dst.sa, &sa_mask.sa);
947 export_rtaddrs(rc->rc_rt, &sa_dst.sa, &sa_mask.sa);
948
949 info->rti_info[RTAX_DST] = &sa_dst.sa;
950 info->rti_info[RTAX_NETMASK] = rt_is_host(rc->rc_rt) ? NULL : &sa_mask.sa;
951 info->rti_info[RTAX_GATEWAY] = &nh->gw_sa;
952 info->rti_info[RTAX_GENMASK] = 0;
953 ifp = nh->nh_ifp;
954 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
955 if (ifp) {
956 info->rti_info[RTAX_IFP] =
957 ifp->if_addr->ifa_addr;
958 error = rtm_get_jailed(info, ifp, nh,
959 &saun, curthread->td_ucred);
960 if (error != 0)
961 return (error);
962 if (ifp->if_flags & IFF_POINTOPOINT)
963 info->rti_info[RTAX_BRD] =
964 nh->nh_ifa->ifa_dstaddr;
965 rtm->rtm_index = ifp->if_index;
966 } else {
967 info->rti_info[RTAX_IFP] = NULL;
968 info->rti_info[RTAX_IFA] = NULL;
969 }
970 } else if (ifp != NULL)
971 rtm->rtm_index = ifp->if_index;
972
973 if ((error = update_rtm_from_info(info, prtm, alloc_len)) != 0)
974 return (error);
975
976 rtm = *prtm;
977 rtm->rtm_flags = rc->rc_rt->rte_flags | nhop_get_rtflags(nh);
978 if (rtm->rtm_flags & RTF_GWFLAG_COMPAT)
979 rtm->rtm_flags = RTF_GATEWAY |
980 (rtm->rtm_flags & ~RTF_GWFLAG_COMPAT);
981 rt_getmetrics(rc->rc_rt, nh, &rtm->rtm_rmx);
982 rtm->rtm_rmx.rmx_weight = rc->rc_nh_weight;
983
984 return (0);
985 }
986
987 #ifdef ROUTE_MPATH
988 static void
save_del_notification(const struct rib_cmd_info * rc,void * _cbdata)989 save_del_notification(const struct rib_cmd_info *rc, void *_cbdata)
990 {
991 struct rib_cmd_info *rc_new = (struct rib_cmd_info *)_cbdata;
992
993 if (rc->rc_cmd == RTM_DELETE)
994 *rc_new = *rc;
995 }
996
997 static void
save_add_notification(const struct rib_cmd_info * rc,void * _cbdata)998 save_add_notification(const struct rib_cmd_info *rc, void *_cbdata)
999 {
1000 struct rib_cmd_info *rc_new = (struct rib_cmd_info *)_cbdata;
1001
1002 if (rc->rc_cmd == RTM_ADD)
1003 *rc_new = *rc;
1004 }
1005 #endif
1006
1007 #if defined(INET6) || defined(INET)
1008 static struct sockaddr *
alloc_sockaddr_aligned(struct linear_buffer * lb,int len)1009 alloc_sockaddr_aligned(struct linear_buffer *lb, int len)
1010 {
1011 len = roundup2(len, sizeof(uint64_t));
1012 if (lb->offset + len > lb->size)
1013 return (NULL);
1014 struct sockaddr *sa = (struct sockaddr *)(lb->base + lb->offset);
1015 lb->offset += len;
1016 return (sa);
1017 }
1018 #endif
1019
1020 static int
rts_send(struct socket * so,int flags,struct mbuf * m,struct sockaddr * nam,struct mbuf * control,struct thread * td)1021 rts_send(struct socket *so, int flags, struct mbuf *m,
1022 struct sockaddr *nam, struct mbuf *control, struct thread *td)
1023 {
1024 struct rt_msghdr *rtm = NULL;
1025 struct rt_addrinfo info;
1026 struct epoch_tracker et;
1027 #ifdef INET6
1028 struct sockaddr_storage ss;
1029 struct sockaddr_in6 *sin6;
1030 int i, rti_need_deembed = 0;
1031 #endif
1032 int alloc_len = 0, len, error = 0, fibnum;
1033 sa_family_t saf = AF_UNSPEC;
1034 struct rib_cmd_info rc;
1035 struct nhop_object *nh;
1036
1037 if ((flags & PRUS_OOB) || control != NULL) {
1038 m_freem(m);
1039 if (control != NULL)
1040 m_freem(control);
1041 return (EOPNOTSUPP);
1042 }
1043
1044 fibnum = so->so_fibnum;
1045 #define senderr(e) { error = e; goto flush;}
1046 if (m == NULL || ((m->m_len < sizeof(long)) &&
1047 (m = m_pullup(m, sizeof(long))) == NULL))
1048 return (ENOBUFS);
1049 if ((m->m_flags & M_PKTHDR) == 0)
1050 panic("route_output");
1051 NET_EPOCH_ENTER(et);
1052 len = m->m_pkthdr.len;
1053 if (len < sizeof(*rtm) ||
1054 len != mtod(m, struct rt_msghdr *)->rtm_msglen)
1055 senderr(EINVAL);
1056
1057 /*
1058 * Most of current messages are in range 200-240 bytes,
1059 * minimize possible re-allocation on reply using larger size
1060 * buffer aligned on 1k boundaty.
1061 */
1062 alloc_len = roundup2(len, 1024);
1063 int total_len = alloc_len + SCRATCH_BUFFER_SIZE;
1064 if ((rtm = malloc(total_len, M_TEMP, M_NOWAIT)) == NULL)
1065 senderr(ENOBUFS);
1066
1067 m_copydata(m, 0, len, (caddr_t)rtm);
1068 bzero(&info, sizeof(info));
1069 nh = NULL;
1070 struct linear_buffer lb = {
1071 .base = (char *)rtm + alloc_len,
1072 .size = SCRATCH_BUFFER_SIZE,
1073 };
1074
1075 if (rtm->rtm_version != RTM_VERSION) {
1076 /* Do not touch message since format is unknown */
1077 free(rtm, M_TEMP);
1078 rtm = NULL;
1079 senderr(EPROTONOSUPPORT);
1080 }
1081
1082 /*
1083 * Starting from here, it is possible
1084 * to alter original message and insert
1085 * caller PID and error value.
1086 */
1087
1088 if ((error = fill_addrinfo(rtm, len, &lb, fibnum, &info)) != 0) {
1089 senderr(error);
1090 }
1091 /* fill_addringo() embeds scope into IPv6 addresses */
1092 #ifdef INET6
1093 rti_need_deembed = 1;
1094 #endif
1095
1096 saf = info.rti_info[RTAX_DST]->sa_family;
1097
1098 /* support for new ARP code */
1099 if (rtm->rtm_flags & RTF_LLDATA) {
1100 error = lla_rt_output(rtm, &info);
1101 goto flush;
1102 }
1103
1104 union sockaddr_union gw_saun;
1105 int blackhole_flags = rtm->rtm_flags & (RTF_BLACKHOLE|RTF_REJECT);
1106 if (blackhole_flags != 0) {
1107 if (blackhole_flags != (RTF_BLACKHOLE | RTF_REJECT))
1108 error = fill_blackholeinfo(&info, &gw_saun);
1109 else {
1110 RTS_PID_LOG(LOG_DEBUG, "both BLACKHOLE and REJECT flags specifiied");
1111 error = EINVAL;
1112 }
1113 if (error != 0)
1114 senderr(error);
1115 }
1116
1117 switch (rtm->rtm_type) {
1118 case RTM_ADD:
1119 case RTM_CHANGE:
1120 if (rtm->rtm_type == RTM_ADD) {
1121 if (info.rti_info[RTAX_GATEWAY] == NULL) {
1122 RTS_PID_LOG(LOG_DEBUG, "RTM_ADD w/o gateway");
1123 senderr(EINVAL);
1124 }
1125 }
1126 error = rib_action(fibnum, rtm->rtm_type, &info, &rc);
1127 if (error == 0) {
1128 rtsock_notify_event(fibnum, &rc);
1129 #ifdef ROUTE_MPATH
1130 if (NH_IS_NHGRP(rc.rc_nh_new) ||
1131 (rc.rc_nh_old && NH_IS_NHGRP(rc.rc_nh_old))) {
1132 struct rib_cmd_info rc_simple = {};
1133 rib_decompose_notification(&rc,
1134 save_add_notification, (void *)&rc_simple);
1135 rc = rc_simple;
1136 }
1137 #endif
1138 /* nh MAY be empty if RTM_CHANGE request is no-op */
1139 nh = rc.rc_nh_new;
1140 if (nh != NULL) {
1141 rtm->rtm_index = nh->nh_ifp->if_index;
1142 rtm->rtm_flags = rc.rc_rt->rte_flags | nhop_get_rtflags(nh);
1143 }
1144 }
1145 break;
1146
1147 case RTM_DELETE:
1148 error = rib_action(fibnum, RTM_DELETE, &info, &rc);
1149 if (error == 0) {
1150 rtsock_notify_event(fibnum, &rc);
1151 #ifdef ROUTE_MPATH
1152 if (NH_IS_NHGRP(rc.rc_nh_old) ||
1153 (rc.rc_nh_new && NH_IS_NHGRP(rc.rc_nh_new))) {
1154 struct rib_cmd_info rc_simple = {};
1155 rib_decompose_notification(&rc,
1156 save_del_notification, (void *)&rc_simple);
1157 rc = rc_simple;
1158 }
1159 #endif
1160 nh = rc.rc_nh_old;
1161 }
1162 break;
1163
1164 case RTM_GET:
1165 error = handle_rtm_get(&info, fibnum, rtm, &rc);
1166 if (error != 0)
1167 senderr(error);
1168 nh = rc.rc_nh_new;
1169
1170 if (!rt_is_exportable(rc.rc_rt, curthread->td_ucred))
1171 senderr(ESRCH);
1172 break;
1173
1174 default:
1175 senderr(EOPNOTSUPP);
1176 }
1177
1178 if (error == 0 && nh != NULL) {
1179 error = update_rtm_from_rc(&info, &rtm, alloc_len, &rc, nh);
1180 /*
1181 * Note that some sockaddr pointers may have changed to
1182 * point to memory outsize @rtm. Some may be pointing
1183 * to the on-stack variables.
1184 * Given that, any pointer in @info CANNOT BE USED.
1185 */
1186
1187 /*
1188 * scopeid deembedding has been performed while
1189 * writing updated rtm in rtsock_msg_buffer().
1190 * With that in mind, skip deembedding procedure below.
1191 */
1192 #ifdef INET6
1193 rti_need_deembed = 0;
1194 #endif
1195 }
1196
1197 flush:
1198 NET_EPOCH_EXIT(et);
1199
1200 #ifdef INET6
1201 if (rtm != NULL) {
1202 if (rti_need_deembed) {
1203 /* sin6_scope_id is recovered before sending rtm. */
1204 sin6 = (struct sockaddr_in6 *)&ss;
1205 for (i = 0; i < RTAX_MAX; i++) {
1206 if (info.rti_info[i] == NULL)
1207 continue;
1208 if (info.rti_info[i]->sa_family != AF_INET6)
1209 continue;
1210 bcopy(info.rti_info[i], sin6, sizeof(*sin6));
1211 if (sa6_recoverscope(sin6) == 0)
1212 bcopy(sin6, info.rti_info[i],
1213 sizeof(*sin6));
1214 }
1215 if (update_rtm_from_info(&info, &rtm, alloc_len) != 0) {
1216 if (error != 0)
1217 error = ENOBUFS;
1218 }
1219 }
1220 }
1221 #endif
1222 send_rtm_reply(so, rtm, m, saf, fibnum, error);
1223
1224 return (error);
1225 }
1226
1227 /*
1228 * Sends the prepared reply message in @rtm to all rtsock clients.
1229 * Frees @m and @rtm.
1230 *
1231 */
1232 static void
send_rtm_reply(struct socket * so,struct rt_msghdr * rtm,struct mbuf * m,sa_family_t saf,u_int fibnum,int rtm_errno)1233 send_rtm_reply(struct socket *so, struct rt_msghdr *rtm, struct mbuf *m,
1234 sa_family_t saf, u_int fibnum, int rtm_errno)
1235 {
1236 struct rcb *rcb = NULL;
1237
1238 /*
1239 * Check to see if we don't want our own messages.
1240 */
1241 if ((so->so_options & SO_USELOOPBACK) == 0) {
1242 if (V_route_cb.any_count <= 1) {
1243 if (rtm != NULL)
1244 free(rtm, M_TEMP);
1245 m_freem(m);
1246 return;
1247 }
1248 /* There is another listener, so construct message */
1249 rcb = so->so_pcb;
1250 }
1251
1252 if (rtm != NULL) {
1253 if (rtm_errno!= 0)
1254 rtm->rtm_errno = rtm_errno;
1255 else
1256 rtm->rtm_flags |= RTF_DONE;
1257
1258 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
1259 if (m->m_pkthdr.len < rtm->rtm_msglen) {
1260 m_freem(m);
1261 m = NULL;
1262 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
1263 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
1264
1265 free(rtm, M_TEMP);
1266 }
1267 if (m != NULL) {
1268 M_SETFIB(m, fibnum);
1269 m->m_flags |= RTS_FILTER_FIB;
1270 if (rcb) {
1271 /*
1272 * XXX insure we don't get a copy by
1273 * invalidating our protocol
1274 */
1275 sa_family_t family = rcb->rcb_family;
1276 rcb->rcb_family = AF_UNSPEC;
1277 rt_dispatch(m, saf);
1278 rcb->rcb_family = family;
1279 } else
1280 rt_dispatch(m, saf);
1281 }
1282 }
1283
1284 static void
rt_getmetrics(const struct rtentry * rt,const struct nhop_object * nh,struct rt_metrics * out)1285 rt_getmetrics(const struct rtentry *rt, const struct nhop_object *nh,
1286 struct rt_metrics *out)
1287 {
1288
1289 bzero(out, sizeof(*out));
1290 out->rmx_mtu = nh->nh_mtu;
1291 out->rmx_weight = rt->rt_weight;
1292 out->rmx_nhidx = nhop_get_idx(nh);
1293 /* Kernel -> userland timebase conversion. */
1294 out->rmx_expire = nhop_get_expire(nh) ?
1295 nhop_get_expire(nh) - time_uptime + time_second : 0;
1296 }
1297
1298 /*
1299 * Extract the addresses of the passed sockaddrs.
1300 * Do a little sanity checking so as to avoid bad memory references.
1301 * This data is derived straight from userland.
1302 */
1303 static int
rt_xaddrs(caddr_t cp,caddr_t cplim,struct rt_addrinfo * rtinfo)1304 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
1305 {
1306 struct sockaddr *sa;
1307 int i;
1308
1309 for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
1310 if ((rtinfo->rti_addrs & (1 << i)) == 0)
1311 continue;
1312 sa = (struct sockaddr *)cp;
1313 /*
1314 * It won't fit.
1315 */
1316 if (cp + sa->sa_len > cplim) {
1317 RTS_PID_LOG(LOG_DEBUG, "sa_len too big for sa type %d", i);
1318 return (EINVAL);
1319 }
1320 /*
1321 * there are no more.. quit now
1322 * If there are more bits, they are in error.
1323 * I've seen this. route(1) can evidently generate these.
1324 * This causes kernel to core dump.
1325 * for compatibility, If we see this, point to a safe address.
1326 */
1327 if (sa->sa_len == 0) {
1328 rtinfo->rti_info[i] = &sa_zero;
1329 return (0); /* should be EINVAL but for compat */
1330 }
1331 /* accept it */
1332 #ifdef INET6
1333 if (sa->sa_family == AF_INET6)
1334 sa6_embedscope((struct sockaddr_in6 *)sa,
1335 V_ip6_use_defzone);
1336 #endif
1337 rtinfo->rti_info[i] = sa;
1338 cp += SA_SIZE(sa);
1339 }
1340 return (0);
1341 }
1342
1343 #ifdef INET
1344 static inline void
fill_sockaddr_inet(struct sockaddr_in * sin,struct in_addr addr)1345 fill_sockaddr_inet(struct sockaddr_in *sin, struct in_addr addr)
1346 {
1347
1348 const struct sockaddr_in nsin = {
1349 .sin_family = AF_INET,
1350 .sin_len = sizeof(struct sockaddr_in),
1351 .sin_addr = addr,
1352 };
1353 *sin = nsin;
1354 }
1355 #endif
1356
1357 #ifdef INET6
1358 static inline void
fill_sockaddr_inet6(struct sockaddr_in6 * sin6,const struct in6_addr * addr6,uint32_t scopeid)1359 fill_sockaddr_inet6(struct sockaddr_in6 *sin6, const struct in6_addr *addr6,
1360 uint32_t scopeid)
1361 {
1362
1363 const struct sockaddr_in6 nsin6 = {
1364 .sin6_family = AF_INET6,
1365 .sin6_len = sizeof(struct sockaddr_in6),
1366 .sin6_addr = *addr6,
1367 .sin6_scope_id = scopeid,
1368 };
1369 *sin6 = nsin6;
1370 }
1371 #endif
1372
1373 #if defined(INET6) || defined(INET)
1374 /*
1375 * Checks if gateway is suitable for lltable operations.
1376 * Lltable code requires AF_LINK gateway with ifindex
1377 * and mac address specified.
1378 * Returns 0 on success.
1379 */
1380 static int
cleanup_xaddrs_lladdr(struct rt_addrinfo * info)1381 cleanup_xaddrs_lladdr(struct rt_addrinfo *info)
1382 {
1383 struct sockaddr_dl *sdl = (struct sockaddr_dl *)info->rti_info[RTAX_GATEWAY];
1384
1385 if (sdl->sdl_family != AF_LINK)
1386 return (EINVAL);
1387
1388 if (sdl->sdl_index == 0) {
1389 RTS_PID_LOG(LOG_DEBUG, "AF_LINK gateway w/o ifindex");
1390 return (EINVAL);
1391 }
1392
1393 if (offsetof(struct sockaddr_dl, sdl_data) + sdl->sdl_nlen + sdl->sdl_alen > sdl->sdl_len) {
1394 RTS_PID_LOG(LOG_DEBUG, "AF_LINK gw: sdl_nlen/sdl_alen too large");
1395 return (EINVAL);
1396 }
1397
1398 return (0);
1399 }
1400
1401 static int
cleanup_xaddrs_gateway(struct rt_addrinfo * info,struct linear_buffer * lb)1402 cleanup_xaddrs_gateway(struct rt_addrinfo *info, struct linear_buffer *lb)
1403 {
1404 struct sockaddr *gw = info->rti_info[RTAX_GATEWAY];
1405 struct sockaddr *sa;
1406
1407 if (info->rti_flags & RTF_LLDATA)
1408 return (cleanup_xaddrs_lladdr(info));
1409
1410 switch (gw->sa_family) {
1411 #ifdef INET
1412 case AF_INET:
1413 {
1414 struct sockaddr_in *gw_sin = (struct sockaddr_in *)gw;
1415
1416 /* Ensure reads do not go beyoud SA boundary */
1417 if (SA_SIZE(gw) < offsetof(struct sockaddr_in, sin_zero)) {
1418 RTS_PID_LOG(LOG_DEBUG, "gateway sin_len too small: %d",
1419 gw->sa_len);
1420 return (EINVAL);
1421 }
1422 sa = alloc_sockaddr_aligned(lb, sizeof(struct sockaddr_in));
1423 if (sa == NULL)
1424 return (ENOBUFS);
1425 fill_sockaddr_inet((struct sockaddr_in *)sa, gw_sin->sin_addr);
1426 info->rti_info[RTAX_GATEWAY] = sa;
1427 }
1428 break;
1429 #endif
1430 #ifdef INET6
1431 case AF_INET6:
1432 {
1433 struct sockaddr_in6 *gw_sin6 = (struct sockaddr_in6 *)gw;
1434 if (gw_sin6->sin6_len < sizeof(struct sockaddr_in6)) {
1435 RTS_PID_LOG(LOG_DEBUG, "gateway sin6_len too small: %d",
1436 gw->sa_len);
1437 return (EINVAL);
1438 }
1439 fill_sockaddr_inet6(gw_sin6, &gw_sin6->sin6_addr, 0);
1440 break;
1441 }
1442 #endif
1443 case AF_LINK:
1444 {
1445 struct sockaddr_dl *gw_sdl;
1446
1447 size_t sdl_min_len = offsetof(struct sockaddr_dl, sdl_data);
1448 gw_sdl = (struct sockaddr_dl *)gw;
1449 if (gw_sdl->sdl_len < sdl_min_len) {
1450 RTS_PID_LOG(LOG_DEBUG, "gateway sdl_len too small: %d",
1451 gw_sdl->sdl_len);
1452 return (EINVAL);
1453 }
1454 sa = alloc_sockaddr_aligned(lb, sizeof(struct sockaddr_dl_short));
1455 if (sa == NULL)
1456 return (ENOBUFS);
1457
1458 const struct sockaddr_dl_short sdl = {
1459 .sdl_family = AF_LINK,
1460 .sdl_len = sizeof(struct sockaddr_dl_short),
1461 .sdl_index = gw_sdl->sdl_index,
1462 };
1463 *((struct sockaddr_dl_short *)sa) = sdl;
1464 info->rti_info[RTAX_GATEWAY] = sa;
1465 break;
1466 }
1467 }
1468
1469 return (0);
1470 }
1471 #endif
1472
1473 static void
remove_netmask(struct rt_addrinfo * info)1474 remove_netmask(struct rt_addrinfo *info)
1475 {
1476 info->rti_info[RTAX_NETMASK] = NULL;
1477 info->rti_flags |= RTF_HOST;
1478 info->rti_addrs &= ~RTA_NETMASK;
1479 }
1480
1481 #ifdef INET
1482 static int
cleanup_xaddrs_inet(struct rt_addrinfo * info,struct linear_buffer * lb)1483 cleanup_xaddrs_inet(struct rt_addrinfo *info, struct linear_buffer *lb)
1484 {
1485 struct sockaddr_in *dst_sa, *mask_sa;
1486 const int sa_len = sizeof(struct sockaddr_in);
1487 struct in_addr dst, mask;
1488
1489 /* Check & fixup dst/netmask combination first */
1490 dst_sa = (struct sockaddr_in *)info->rti_info[RTAX_DST];
1491 mask_sa = (struct sockaddr_in *)info->rti_info[RTAX_NETMASK];
1492
1493 /* Ensure reads do not go beyound the buffer size */
1494 if (SA_SIZE(dst_sa) < offsetof(struct sockaddr_in, sin_zero)) {
1495 RTS_PID_LOG(LOG_DEBUG, "prefix dst sin_len too small: %d",
1496 dst_sa->sin_len);
1497 return (EINVAL);
1498 }
1499
1500 if ((mask_sa != NULL) && mask_sa->sin_len < sizeof(struct sockaddr_in)) {
1501 /*
1502 * Some older routing software encode mask length into the
1503 * sin_len, thus resulting in "truncated" sockaddr.
1504 */
1505 int len = mask_sa->sin_len - offsetof(struct sockaddr_in, sin_addr);
1506 if (len >= 0) {
1507 mask.s_addr = 0;
1508 if (len > sizeof(struct in_addr))
1509 len = sizeof(struct in_addr);
1510 memcpy(&mask, &mask_sa->sin_addr, len);
1511 } else {
1512 RTS_PID_LOG(LOG_DEBUG, "prefix mask sin_len too small: %d",
1513 mask_sa->sin_len);
1514 return (EINVAL);
1515 }
1516 } else
1517 mask.s_addr = mask_sa ? mask_sa->sin_addr.s_addr : INADDR_BROADCAST;
1518
1519 dst.s_addr = htonl(ntohl(dst_sa->sin_addr.s_addr) & ntohl(mask.s_addr));
1520
1521 /* Construct new "clean" dst/mask sockaddresses */
1522 if ((dst_sa = (struct sockaddr_in *)alloc_sockaddr_aligned(lb, sa_len)) == NULL)
1523 return (ENOBUFS);
1524 fill_sockaddr_inet(dst_sa, dst);
1525 info->rti_info[RTAX_DST] = (struct sockaddr *)dst_sa;
1526
1527 if (mask.s_addr != INADDR_BROADCAST) {
1528 if ((mask_sa = (struct sockaddr_in *)alloc_sockaddr_aligned(lb, sa_len)) == NULL)
1529 return (ENOBUFS);
1530 fill_sockaddr_inet(mask_sa, mask);
1531 info->rti_info[RTAX_NETMASK] = (struct sockaddr *)mask_sa;
1532 info->rti_flags &= ~RTF_HOST;
1533 } else
1534 remove_netmask(info);
1535
1536 /* Check gateway */
1537 if (info->rti_info[RTAX_GATEWAY] != NULL)
1538 return (cleanup_xaddrs_gateway(info, lb));
1539
1540 return (0);
1541 }
1542 #endif
1543
1544 #ifdef INET6
1545 static int
cleanup_xaddrs_inet6(struct rt_addrinfo * info,struct linear_buffer * lb)1546 cleanup_xaddrs_inet6(struct rt_addrinfo *info, struct linear_buffer *lb)
1547 {
1548 struct sockaddr *sa;
1549 struct sockaddr_in6 *dst_sa, *mask_sa;
1550 struct in6_addr mask, *dst;
1551 const int sa_len = sizeof(struct sockaddr_in6);
1552
1553 /* Check & fixup dst/netmask combination first */
1554 dst_sa = (struct sockaddr_in6 *)info->rti_info[RTAX_DST];
1555 mask_sa = (struct sockaddr_in6 *)info->rti_info[RTAX_NETMASK];
1556
1557 if (dst_sa->sin6_len < sizeof(struct sockaddr_in6)) {
1558 RTS_PID_LOG(LOG_DEBUG, "prefix dst sin6_len too small: %d",
1559 dst_sa->sin6_len);
1560 return (EINVAL);
1561 }
1562
1563 if (mask_sa && mask_sa->sin6_len < sizeof(struct sockaddr_in6)) {
1564 /*
1565 * Some older routing software encode mask length into the
1566 * sin6_len, thus resulting in "truncated" sockaddr.
1567 */
1568 int len = mask_sa->sin6_len - offsetof(struct sockaddr_in6, sin6_addr);
1569 if (len >= 0) {
1570 bzero(&mask, sizeof(mask));
1571 if (len > sizeof(struct in6_addr))
1572 len = sizeof(struct in6_addr);
1573 memcpy(&mask, &mask_sa->sin6_addr, len);
1574 } else {
1575 RTS_PID_LOG(LOG_DEBUG, "rtsock: prefix mask sin6_len too small: %d",
1576 mask_sa->sin6_len);
1577 return (EINVAL);
1578 }
1579 } else
1580 mask = mask_sa ? mask_sa->sin6_addr : in6mask128;
1581
1582 dst = &dst_sa->sin6_addr;
1583 IN6_MASK_ADDR(dst, &mask);
1584
1585 if ((sa = alloc_sockaddr_aligned(lb, sa_len)) == NULL)
1586 return (ENOBUFS);
1587 fill_sockaddr_inet6((struct sockaddr_in6 *)sa, dst, 0);
1588 info->rti_info[RTAX_DST] = sa;
1589
1590 if (!IN6_ARE_ADDR_EQUAL(&mask, &in6mask128)) {
1591 if ((sa = alloc_sockaddr_aligned(lb, sa_len)) == NULL)
1592 return (ENOBUFS);
1593 fill_sockaddr_inet6((struct sockaddr_in6 *)sa, &mask, 0);
1594 info->rti_info[RTAX_NETMASK] = sa;
1595 info->rti_flags &= ~RTF_HOST;
1596 } else
1597 remove_netmask(info);
1598
1599 /* Check gateway */
1600 if (info->rti_info[RTAX_GATEWAY] != NULL)
1601 return (cleanup_xaddrs_gateway(info, lb));
1602
1603 return (0);
1604 }
1605 #endif
1606
1607 static int
cleanup_xaddrs(struct rt_addrinfo * info,struct linear_buffer * lb)1608 cleanup_xaddrs(struct rt_addrinfo *info, struct linear_buffer *lb)
1609 {
1610 int error = EAFNOSUPPORT;
1611
1612 if (info->rti_info[RTAX_DST] == NULL) {
1613 RTS_PID_LOG(LOG_DEBUG, "prefix dst is not set");
1614 return (EINVAL);
1615 }
1616
1617 if (info->rti_flags & RTF_LLDATA) {
1618 /*
1619 * arp(8)/ndp(8) sends RTA_NETMASK for the associated
1620 * prefix along with the actual address in RTA_DST.
1621 * Remove netmask to avoid unnecessary address masking.
1622 */
1623 remove_netmask(info);
1624 }
1625
1626 switch (info->rti_info[RTAX_DST]->sa_family) {
1627 #ifdef INET
1628 case AF_INET:
1629 error = cleanup_xaddrs_inet(info, lb);
1630 break;
1631 #endif
1632 #ifdef INET6
1633 case AF_INET6:
1634 error = cleanup_xaddrs_inet6(info, lb);
1635 break;
1636 #endif
1637 }
1638
1639 return (error);
1640 }
1641
1642 /*
1643 * Fill in @dmask with valid netmask leaving original @smask
1644 * intact. Mostly used with radix netmasks.
1645 */
1646 struct sockaddr *
rtsock_fix_netmask(const struct sockaddr * dst,const struct sockaddr * smask,struct sockaddr_storage * dmask)1647 rtsock_fix_netmask(const struct sockaddr *dst, const struct sockaddr *smask,
1648 struct sockaddr_storage *dmask)
1649 {
1650 if (dst == NULL || smask == NULL)
1651 return (NULL);
1652
1653 memset(dmask, 0, dst->sa_len);
1654 memcpy(dmask, smask, smask->sa_len);
1655 dmask->ss_len = dst->sa_len;
1656 dmask->ss_family = dst->sa_family;
1657
1658 return ((struct sockaddr *)dmask);
1659 }
1660
1661 /*
1662 * Writes information related to @rtinfo object to newly-allocated mbuf.
1663 * Assumes MCLBYTES is enough to construct any message.
1664 * Used for OS notifications of vaious events (if/ifa announces,etc)
1665 *
1666 * Returns allocated mbuf or NULL on failure.
1667 */
1668 static struct mbuf *
rtsock_msg_mbuf(int type,struct rt_addrinfo * rtinfo)1669 rtsock_msg_mbuf(int type, struct rt_addrinfo *rtinfo)
1670 {
1671 struct sockaddr_storage ss;
1672 struct rt_msghdr *rtm;
1673 struct mbuf *m;
1674 int i;
1675 struct sockaddr *sa;
1676 #ifdef INET6
1677 struct sockaddr_in6 *sin6;
1678 #endif
1679 int len, dlen;
1680
1681 switch (type) {
1682 case RTM_DELADDR:
1683 case RTM_NEWADDR:
1684 len = sizeof(struct ifa_msghdr);
1685 break;
1686
1687 case RTM_DELMADDR:
1688 case RTM_NEWMADDR:
1689 len = sizeof(struct ifma_msghdr);
1690 break;
1691
1692 case RTM_IFINFO:
1693 len = sizeof(struct if_msghdr);
1694 break;
1695
1696 case RTM_IFANNOUNCE:
1697 case RTM_IEEE80211:
1698 len = sizeof(struct if_announcemsghdr);
1699 break;
1700
1701 default:
1702 len = sizeof(struct rt_msghdr);
1703 }
1704
1705 /* XXXGL: can we use MJUMPAGESIZE cluster here? */
1706 KASSERT(len <= MCLBYTES, ("%s: message too big", __func__));
1707 if (len > MHLEN)
1708 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1709 else
1710 m = m_gethdr(M_NOWAIT, MT_DATA);
1711 if (m == NULL)
1712 return (m);
1713
1714 m->m_pkthdr.len = m->m_len = len;
1715 rtm = mtod(m, struct rt_msghdr *);
1716 bzero((caddr_t)rtm, len);
1717 for (i = 0; i < RTAX_MAX; i++) {
1718 if ((sa = rtinfo->rti_info[i]) == NULL)
1719 continue;
1720 rtinfo->rti_addrs |= (1 << i);
1721
1722 dlen = SA_SIZE(sa);
1723 KASSERT(dlen <= sizeof(ss),
1724 ("%s: sockaddr size overflow", __func__));
1725 bzero(&ss, sizeof(ss));
1726 bcopy(sa, &ss, sa->sa_len);
1727 sa = (struct sockaddr *)&ss;
1728 #ifdef INET6
1729 if (sa->sa_family == AF_INET6) {
1730 sin6 = (struct sockaddr_in6 *)sa;
1731 (void)sa6_recoverscope(sin6);
1732 }
1733 #endif
1734 m_copyback(m, len, dlen, (caddr_t)sa);
1735 len += dlen;
1736 }
1737 if (m->m_pkthdr.len != len) {
1738 m_freem(m);
1739 return (NULL);
1740 }
1741 rtm->rtm_msglen = len;
1742 rtm->rtm_version = RTM_VERSION;
1743 rtm->rtm_type = type;
1744 return (m);
1745 }
1746
1747 /*
1748 * Writes information related to @rtinfo object to preallocated buffer.
1749 * Stores needed size in @plen. If @w is NULL, calculates size without
1750 * writing.
1751 * Used for sysctl dumps and rtsock answers (RTM_DEL/RTM_GET) generation.
1752 *
1753 * Returns 0 on success.
1754 *
1755 */
1756 static int
rtsock_msg_buffer(int type,struct rt_addrinfo * rtinfo,struct walkarg * w,int * plen)1757 rtsock_msg_buffer(int type, struct rt_addrinfo *rtinfo, struct walkarg *w, int *plen)
1758 {
1759 struct sockaddr_storage ss;
1760 int len, buflen = 0, dlen, i;
1761 caddr_t cp = NULL;
1762 struct rt_msghdr *rtm = NULL;
1763 #ifdef INET6
1764 struct sockaddr_in6 *sin6;
1765 #endif
1766 #ifdef COMPAT_FREEBSD32
1767 bool compat32 = false;
1768 #endif
1769
1770 switch (type) {
1771 case RTM_DELADDR:
1772 case RTM_NEWADDR:
1773 if (w != NULL && w->w_op == NET_RT_IFLISTL) {
1774 #ifdef COMPAT_FREEBSD32
1775 if (w->w_req->flags & SCTL_MASK32) {
1776 len = sizeof(struct ifa_msghdrl32);
1777 compat32 = true;
1778 } else
1779 #endif
1780 len = sizeof(struct ifa_msghdrl);
1781 } else
1782 len = sizeof(struct ifa_msghdr);
1783 break;
1784
1785 case RTM_IFINFO:
1786 #ifdef COMPAT_FREEBSD32
1787 if (w != NULL && w->w_req->flags & SCTL_MASK32) {
1788 if (w->w_op == NET_RT_IFLISTL)
1789 len = sizeof(struct if_msghdrl32);
1790 else
1791 len = sizeof(struct if_msghdr32);
1792 compat32 = true;
1793 break;
1794 }
1795 #endif
1796 if (w != NULL && w->w_op == NET_RT_IFLISTL)
1797 len = sizeof(struct if_msghdrl);
1798 else
1799 len = sizeof(struct if_msghdr);
1800 break;
1801
1802 case RTM_NEWMADDR:
1803 len = sizeof(struct ifma_msghdr);
1804 break;
1805
1806 default:
1807 len = sizeof(struct rt_msghdr);
1808 }
1809
1810 if (w != NULL) {
1811 rtm = (struct rt_msghdr *)w->w_tmem;
1812 buflen = w->w_tmemsize - len;
1813 cp = (caddr_t)w->w_tmem + len;
1814 }
1815
1816 rtinfo->rti_addrs = 0;
1817 for (i = 0; i < RTAX_MAX; i++) {
1818 struct sockaddr *sa;
1819
1820 if ((sa = rtinfo->rti_info[i]) == NULL)
1821 continue;
1822 rtinfo->rti_addrs |= (1 << i);
1823 #ifdef COMPAT_FREEBSD32
1824 if (compat32)
1825 dlen = SA_SIZE32(sa);
1826 else
1827 #endif
1828 dlen = SA_SIZE(sa);
1829 if (cp != NULL && buflen >= dlen) {
1830 KASSERT(dlen <= sizeof(ss),
1831 ("%s: sockaddr size overflow", __func__));
1832 bzero(&ss, sizeof(ss));
1833 bcopy(sa, &ss, sa->sa_len);
1834 sa = (struct sockaddr *)&ss;
1835 #ifdef INET6
1836 if (sa->sa_family == AF_INET6) {
1837 sin6 = (struct sockaddr_in6 *)sa;
1838 (void)sa6_recoverscope(sin6);
1839 }
1840 #endif
1841 bcopy((caddr_t)sa, cp, (unsigned)dlen);
1842 cp += dlen;
1843 buflen -= dlen;
1844 } else if (cp != NULL) {
1845 /*
1846 * Buffer too small. Count needed size
1847 * and return with error.
1848 */
1849 cp = NULL;
1850 }
1851
1852 len += dlen;
1853 }
1854
1855 if (cp != NULL) {
1856 dlen = ALIGN(len) - len;
1857 if (buflen < dlen)
1858 cp = NULL;
1859 else {
1860 bzero(cp, dlen);
1861 cp += dlen;
1862 buflen -= dlen;
1863 }
1864 }
1865 len = ALIGN(len);
1866
1867 if (cp != NULL) {
1868 /* fill header iff buffer is large enough */
1869 rtm->rtm_version = RTM_VERSION;
1870 rtm->rtm_type = type;
1871 rtm->rtm_msglen = len;
1872 }
1873
1874 *plen = len;
1875
1876 if (w != NULL && cp == NULL)
1877 return (ENOBUFS);
1878
1879 return (0);
1880 }
1881
1882 /*
1883 * This routine is called to generate a message from the routing
1884 * socket indicating that a redirect has occurred, a routing lookup
1885 * has failed, or that a protocol has detected timeouts to a particular
1886 * destination.
1887 */
1888 void
rt_missmsg_fib(int type,struct rt_addrinfo * rtinfo,int flags,int error,int fibnum)1889 rt_missmsg_fib(int type, struct rt_addrinfo *rtinfo, int flags, int error,
1890 int fibnum)
1891 {
1892 struct rt_msghdr *rtm;
1893 struct mbuf *m;
1894 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
1895
1896 if (V_route_cb.any_count == 0)
1897 return;
1898 m = rtsock_msg_mbuf(type, rtinfo);
1899 if (m == NULL)
1900 return;
1901
1902 if (fibnum != RT_ALL_FIBS) {
1903 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out "
1904 "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs));
1905 M_SETFIB(m, fibnum);
1906 m->m_flags |= RTS_FILTER_FIB;
1907 }
1908
1909 rtm = mtod(m, struct rt_msghdr *);
1910 rtm->rtm_flags = RTF_DONE | flags;
1911 rtm->rtm_errno = error;
1912 rtm->rtm_addrs = rtinfo->rti_addrs;
1913 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1914 }
1915
1916 void
rt_missmsg(int type,struct rt_addrinfo * rtinfo,int flags,int error)1917 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
1918 {
1919
1920 rt_missmsg_fib(type, rtinfo, flags, error, RT_ALL_FIBS);
1921 }
1922
1923 /*
1924 * This routine is called to generate a message from the routing
1925 * socket indicating that the status of a network interface has changed.
1926 */
1927 static void
rtsock_ifmsg(struct ifnet * ifp,int if_flags_mask __unused)1928 rtsock_ifmsg(struct ifnet *ifp, int if_flags_mask __unused)
1929 {
1930 struct if_msghdr *ifm;
1931 struct mbuf *m;
1932 struct rt_addrinfo info;
1933
1934 if (V_route_cb.any_count == 0)
1935 return;
1936 bzero((caddr_t)&info, sizeof(info));
1937 m = rtsock_msg_mbuf(RTM_IFINFO, &info);
1938 if (m == NULL)
1939 return;
1940 ifm = mtod(m, struct if_msghdr *);
1941 ifm->ifm_index = ifp->if_index;
1942 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1943 if_data_copy(ifp, &ifm->ifm_data);
1944 ifm->ifm_addrs = 0;
1945 rt_dispatch(m, AF_UNSPEC);
1946 }
1947
1948 /*
1949 * Announce interface address arrival/withdraw.
1950 * Please do not call directly, use rt_addrmsg().
1951 * Assume input data to be valid.
1952 * Returns 0 on success.
1953 */
1954 int
rtsock_addrmsg(int cmd,struct ifaddr * ifa,int fibnum)1955 rtsock_addrmsg(int cmd, struct ifaddr *ifa, int fibnum)
1956 {
1957 struct rt_addrinfo info;
1958 struct sockaddr *sa;
1959 int ncmd;
1960 struct mbuf *m;
1961 struct ifa_msghdr *ifam;
1962 struct ifnet *ifp = ifa->ifa_ifp;
1963 struct sockaddr_storage ss;
1964
1965 if (V_route_cb.any_count == 0)
1966 return (0);
1967
1968 ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
1969
1970 bzero((caddr_t)&info, sizeof(info));
1971 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1972 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1973 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(
1974 info.rti_info[RTAX_IFA], ifa->ifa_netmask, &ss);
1975 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1976 if ((m = rtsock_msg_mbuf(ncmd, &info)) == NULL)
1977 return (ENOBUFS);
1978 ifam = mtod(m, struct ifa_msghdr *);
1979 ifam->ifam_index = ifp->if_index;
1980 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1981 ifam->ifam_flags = ifa->ifa_flags;
1982 ifam->ifam_addrs = info.rti_addrs;
1983
1984 if (fibnum != RT_ALL_FIBS) {
1985 M_SETFIB(m, fibnum);
1986 m->m_flags |= RTS_FILTER_FIB;
1987 }
1988
1989 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1990
1991 return (0);
1992 }
1993
1994 /*
1995 * Announce route addition/removal to rtsock based on @rt data.
1996 * Callers are advives to use rt_routemsg() instead of using this
1997 * function directly.
1998 * Assume @rt data is consistent.
1999 *
2000 * Returns 0 on success.
2001 */
2002 int
rtsock_routemsg(int cmd,struct rtentry * rt,struct nhop_object * nh,int fibnum)2003 rtsock_routemsg(int cmd, struct rtentry *rt, struct nhop_object *nh,
2004 int fibnum)
2005 {
2006 union sockaddr_union dst, mask;
2007 struct rt_addrinfo info;
2008
2009 if (V_route_cb.any_count == 0)
2010 return (0);
2011
2012 int family = rt_get_family(rt);
2013 init_sockaddrs_family(family, &dst.sa, &mask.sa);
2014 export_rtaddrs(rt, &dst.sa, &mask.sa);
2015
2016 bzero((caddr_t)&info, sizeof(info));
2017 info.rti_info[RTAX_DST] = &dst.sa;
2018 info.rti_info[RTAX_NETMASK] = &mask.sa;
2019 info.rti_info[RTAX_GATEWAY] = &nh->gw_sa;
2020 info.rti_flags = rt->rte_flags | nhop_get_rtflags(nh);
2021 info.rti_ifp = nh->nh_ifp;
2022
2023 return (rtsock_routemsg_info(cmd, &info, fibnum));
2024 }
2025
2026 int
rtsock_routemsg_info(int cmd,struct rt_addrinfo * info,int fibnum)2027 rtsock_routemsg_info(int cmd, struct rt_addrinfo *info, int fibnum)
2028 {
2029 struct rt_msghdr *rtm;
2030 struct sockaddr *sa;
2031 struct mbuf *m;
2032
2033 if (V_route_cb.any_count == 0)
2034 return (0);
2035
2036 if (info->rti_flags & RTF_HOST)
2037 info->rti_info[RTAX_NETMASK] = NULL;
2038
2039 m = rtsock_msg_mbuf(cmd, info);
2040 if (m == NULL)
2041 return (ENOBUFS);
2042
2043 if (fibnum != RT_ALL_FIBS) {
2044 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out "
2045 "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs));
2046 M_SETFIB(m, fibnum);
2047 m->m_flags |= RTS_FILTER_FIB;
2048 }
2049
2050 rtm = mtod(m, struct rt_msghdr *);
2051 rtm->rtm_addrs = info->rti_addrs;
2052 if (info->rti_ifp != NULL)
2053 rtm->rtm_index = info->rti_ifp->if_index;
2054 /* Add RTF_DONE to indicate command 'completion' required by API */
2055 info->rti_flags |= RTF_DONE;
2056 /* Reported routes has to be up */
2057 if (cmd == RTM_ADD || cmd == RTM_CHANGE)
2058 info->rti_flags |= RTF_UP;
2059 rtm->rtm_flags = info->rti_flags;
2060
2061 sa = info->rti_info[RTAX_DST];
2062 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
2063
2064 return (0);
2065 }
2066
2067 /*
2068 * This is the analogue to the rt_newaddrmsg which performs the same
2069 * function but for multicast group memberhips. This is easier since
2070 * there is no route state to worry about.
2071 */
2072 void
rt_newmaddrmsg(int cmd,struct ifmultiaddr * ifma)2073 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
2074 {
2075 struct rt_addrinfo info;
2076 struct mbuf *m = NULL;
2077 struct ifnet *ifp = ifma->ifma_ifp;
2078 struct ifma_msghdr *ifmam;
2079
2080 if (V_route_cb.any_count == 0)
2081 return;
2082
2083 bzero((caddr_t)&info, sizeof(info));
2084 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
2085 if (ifp && ifp->if_addr)
2086 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
2087 else
2088 info.rti_info[RTAX_IFP] = NULL;
2089 /*
2090 * If a link-layer address is present, present it as a ``gateway''
2091 * (similarly to how ARP entries, e.g., are presented).
2092 */
2093 info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr;
2094 m = rtsock_msg_mbuf(cmd, &info);
2095 if (m == NULL)
2096 return;
2097 ifmam = mtod(m, struct ifma_msghdr *);
2098 KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n",
2099 __func__));
2100 ifmam->ifmam_index = ifp->if_index;
2101 ifmam->ifmam_addrs = info.rti_addrs;
2102 rt_dispatch(m, ifma->ifma_addr ? ifma->ifma_addr->sa_family : AF_UNSPEC);
2103 }
2104
2105 static struct mbuf *
rt_makeifannouncemsg(struct ifnet * ifp,int type,int what,struct rt_addrinfo * info)2106 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
2107 struct rt_addrinfo *info)
2108 {
2109 struct if_announcemsghdr *ifan;
2110 struct mbuf *m;
2111
2112 if (V_route_cb.any_count == 0)
2113 return NULL;
2114 bzero((caddr_t)info, sizeof(*info));
2115 m = rtsock_msg_mbuf(type, info);
2116 if (m != NULL) {
2117 ifan = mtod(m, struct if_announcemsghdr *);
2118 ifan->ifan_index = ifp->if_index;
2119 strlcpy(ifan->ifan_name, ifp->if_xname,
2120 sizeof(ifan->ifan_name));
2121 ifan->ifan_what = what;
2122 }
2123 return m;
2124 }
2125
2126 /*
2127 * This is called to generate routing socket messages indicating
2128 * IEEE80211 wireless events.
2129 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
2130 */
2131 void
rt_ieee80211msg(struct ifnet * ifp,int what,void * data,size_t data_len)2132 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
2133 {
2134 struct mbuf *m;
2135 struct rt_addrinfo info;
2136
2137 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
2138 if (m != NULL) {
2139 /*
2140 * Append the ieee80211 data. Try to stick it in the
2141 * mbuf containing the ifannounce msg; otherwise allocate
2142 * a new mbuf and append.
2143 *
2144 * NB: we assume m is a single mbuf.
2145 */
2146 if (data_len > M_TRAILINGSPACE(m)) {
2147 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
2148 if (n == NULL) {
2149 m_freem(m);
2150 return;
2151 }
2152 bcopy(data, mtod(n, void *), data_len);
2153 n->m_len = data_len;
2154 m->m_next = n;
2155 } else if (data_len > 0) {
2156 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
2157 m->m_len += data_len;
2158 }
2159 if (m->m_flags & M_PKTHDR)
2160 m->m_pkthdr.len += data_len;
2161 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
2162 rt_dispatch(m, AF_UNSPEC);
2163 }
2164 }
2165
2166 /*
2167 * This is called to generate routing socket messages indicating
2168 * network interface arrival and departure.
2169 */
2170 static void
rt_ifannouncemsg(struct ifnet * ifp,int what)2171 rt_ifannouncemsg(struct ifnet *ifp, int what)
2172 {
2173 struct mbuf *m;
2174 struct rt_addrinfo info;
2175
2176 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
2177 if (m != NULL)
2178 rt_dispatch(m, AF_UNSPEC);
2179 }
2180
2181 static void
rt_dispatch(struct mbuf * m,sa_family_t saf)2182 rt_dispatch(struct mbuf *m, sa_family_t saf)
2183 {
2184
2185 M_ASSERTPKTHDR(m);
2186
2187 m->m_rtsock_family = saf;
2188 if (V_loif)
2189 m->m_pkthdr.rcvif = V_loif;
2190 else {
2191 m_freem(m);
2192 return;
2193 }
2194 netisr_queue(NETISR_ROUTE, m); /* mbuf is free'd on failure. */
2195 }
2196
2197 /*
2198 * This is used in dumping the kernel table via sysctl().
2199 */
2200 static int
sysctl_dumpentry(struct rtentry * rt,void * vw)2201 sysctl_dumpentry(struct rtentry *rt, void *vw)
2202 {
2203 struct walkarg *w = vw;
2204 struct nhop_object *nh;
2205
2206 NET_EPOCH_ASSERT();
2207
2208 if (!rt_is_exportable(rt, w->w_req->td->td_ucred))
2209 return (0);
2210
2211 export_rtaddrs(rt, w->dst, w->mask);
2212 nh = rt_get_raw_nhop(rt);
2213 #ifdef ROUTE_MPATH
2214 if (NH_IS_NHGRP(nh)) {
2215 const struct weightened_nhop *wn;
2216 uint32_t num_nhops;
2217 int error;
2218 wn = nhgrp_get_nhops((struct nhgrp_object *)nh, &num_nhops);
2219 for (int i = 0; i < num_nhops; i++) {
2220 error = sysctl_dumpnhop(rt, wn[i].nh, wn[i].weight, w);
2221 if (error != 0)
2222 return (error);
2223 }
2224 } else
2225 #endif
2226 sysctl_dumpnhop(rt, nh, rt->rt_weight, w);
2227
2228 return (0);
2229 }
2230
2231
2232 static int
sysctl_dumpnhop(struct rtentry * rt,struct nhop_object * nh,uint32_t weight,struct walkarg * w)2233 sysctl_dumpnhop(struct rtentry *rt, struct nhop_object *nh, uint32_t weight,
2234 struct walkarg *w)
2235 {
2236 struct rt_addrinfo info;
2237 int error = 0, size;
2238 uint32_t rtflags;
2239
2240 rtflags = nhop_get_rtflags(nh);
2241
2242 if (w->w_op == NET_RT_FLAGS && !(rtflags & w->w_arg))
2243 return (0);
2244
2245 bzero((caddr_t)&info, sizeof(info));
2246 info.rti_info[RTAX_DST] = w->dst;
2247 info.rti_info[RTAX_GATEWAY] = &nh->gw_sa;
2248 info.rti_info[RTAX_NETMASK] = (rtflags & RTF_HOST) ? NULL : w->mask;
2249 info.rti_info[RTAX_GENMASK] = 0;
2250 if (nh->nh_ifp && !(nh->nh_ifp->if_flags & IFF_DYING)) {
2251 info.rti_info[RTAX_IFP] = nh->nh_ifp->if_addr->ifa_addr;
2252 info.rti_info[RTAX_IFA] = nh->nh_ifa->ifa_addr;
2253 if (nh->nh_ifp->if_flags & IFF_POINTOPOINT)
2254 info.rti_info[RTAX_BRD] = nh->nh_ifa->ifa_dstaddr;
2255 }
2256 if ((error = rtsock_msg_buffer(RTM_GET, &info, w, &size)) != 0)
2257 return (error);
2258 if (w->w_req && w->w_tmem) {
2259 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
2260
2261 bzero(&rtm->rtm_index,
2262 sizeof(*rtm) - offsetof(struct rt_msghdr, rtm_index));
2263
2264 /*
2265 * rte flags may consist of RTF_HOST (duplicated in nhop rtflags)
2266 * and RTF_UP (if entry is linked, which is always true here).
2267 * Given that, use nhop rtflags & add RTF_UP.
2268 */
2269 rtm->rtm_flags = rtflags | RTF_UP;
2270 if (rtm->rtm_flags & RTF_GWFLAG_COMPAT)
2271 rtm->rtm_flags = RTF_GATEWAY |
2272 (rtm->rtm_flags & ~RTF_GWFLAG_COMPAT);
2273 rt_getmetrics(rt, nh, &rtm->rtm_rmx);
2274 rtm->rtm_rmx.rmx_weight = weight;
2275 rtm->rtm_index = nh->nh_ifp->if_index;
2276 rtm->rtm_addrs = info.rti_addrs;
2277 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
2278 return (error);
2279 }
2280 return (error);
2281 }
2282
2283 static int
sysctl_iflist_ifml(struct ifnet * ifp,const struct if_data * src_ifd,struct rt_addrinfo * info,struct walkarg * w,int len)2284 sysctl_iflist_ifml(struct ifnet *ifp, const struct if_data *src_ifd,
2285 struct rt_addrinfo *info, struct walkarg *w, int len)
2286 {
2287 struct if_msghdrl *ifm;
2288 struct if_data *ifd;
2289
2290 ifm = (struct if_msghdrl *)w->w_tmem;
2291
2292 #ifdef COMPAT_FREEBSD32
2293 if (w->w_req->flags & SCTL_MASK32) {
2294 struct if_msghdrl32 *ifm32;
2295
2296 ifm32 = (struct if_msghdrl32 *)ifm;
2297 ifm32->ifm_addrs = info->rti_addrs;
2298 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
2299 ifm32->ifm_index = ifp->if_index;
2300 ifm32->_ifm_spare1 = 0;
2301 ifm32->ifm_len = sizeof(*ifm32);
2302 ifm32->ifm_data_off = offsetof(struct if_msghdrl32, ifm_data);
2303 ifm32->_ifm_spare2 = 0;
2304 ifd = &ifm32->ifm_data;
2305 } else
2306 #endif
2307 {
2308 ifm->ifm_addrs = info->rti_addrs;
2309 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
2310 ifm->ifm_index = ifp->if_index;
2311 ifm->_ifm_spare1 = 0;
2312 ifm->ifm_len = sizeof(*ifm);
2313 ifm->ifm_data_off = offsetof(struct if_msghdrl, ifm_data);
2314 ifm->_ifm_spare2 = 0;
2315 ifd = &ifm->ifm_data;
2316 }
2317
2318 memcpy(ifd, src_ifd, sizeof(*ifd));
2319
2320 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
2321 }
2322
2323 static int
sysctl_iflist_ifm(struct ifnet * ifp,const struct if_data * src_ifd,struct rt_addrinfo * info,struct walkarg * w,int len)2324 sysctl_iflist_ifm(struct ifnet *ifp, const struct if_data *src_ifd,
2325 struct rt_addrinfo *info, struct walkarg *w, int len)
2326 {
2327 struct if_msghdr *ifm;
2328 struct if_data *ifd;
2329
2330 ifm = (struct if_msghdr *)w->w_tmem;
2331
2332 #ifdef COMPAT_FREEBSD32
2333 if (w->w_req->flags & SCTL_MASK32) {
2334 struct if_msghdr32 *ifm32;
2335
2336 ifm32 = (struct if_msghdr32 *)ifm;
2337 ifm32->ifm_addrs = info->rti_addrs;
2338 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
2339 ifm32->ifm_index = ifp->if_index;
2340 ifm32->_ifm_spare1 = 0;
2341 ifd = &ifm32->ifm_data;
2342 } else
2343 #endif
2344 {
2345 ifm->ifm_addrs = info->rti_addrs;
2346 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
2347 ifm->ifm_index = ifp->if_index;
2348 ifm->_ifm_spare1 = 0;
2349 ifd = &ifm->ifm_data;
2350 }
2351
2352 memcpy(ifd, src_ifd, sizeof(*ifd));
2353
2354 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
2355 }
2356
2357 static int
sysctl_iflist_ifaml(struct ifaddr * ifa,struct rt_addrinfo * info,struct walkarg * w,int len)2358 sysctl_iflist_ifaml(struct ifaddr *ifa, struct rt_addrinfo *info,
2359 struct walkarg *w, int len)
2360 {
2361 struct ifa_msghdrl *ifam;
2362 struct if_data *ifd;
2363
2364 ifam = (struct ifa_msghdrl *)w->w_tmem;
2365
2366 #ifdef COMPAT_FREEBSD32
2367 if (w->w_req->flags & SCTL_MASK32) {
2368 struct ifa_msghdrl32 *ifam32;
2369
2370 ifam32 = (struct ifa_msghdrl32 *)ifam;
2371 ifam32->ifam_addrs = info->rti_addrs;
2372 ifam32->ifam_flags = ifa->ifa_flags;
2373 ifam32->ifam_index = ifa->ifa_ifp->if_index;
2374 ifam32->_ifam_spare1 = 0;
2375 ifam32->ifam_len = sizeof(*ifam32);
2376 ifam32->ifam_data_off =
2377 offsetof(struct ifa_msghdrl32, ifam_data);
2378 ifam32->ifam_metric = ifa->ifa_ifp->if_metric;
2379 ifd = &ifam32->ifam_data;
2380 } else
2381 #endif
2382 {
2383 ifam->ifam_addrs = info->rti_addrs;
2384 ifam->ifam_flags = ifa->ifa_flags;
2385 ifam->ifam_index = ifa->ifa_ifp->if_index;
2386 ifam->_ifam_spare1 = 0;
2387 ifam->ifam_len = sizeof(*ifam);
2388 ifam->ifam_data_off = offsetof(struct ifa_msghdrl, ifam_data);
2389 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
2390 ifd = &ifam->ifam_data;
2391 }
2392
2393 bzero(ifd, sizeof(*ifd));
2394 ifd->ifi_datalen = sizeof(struct if_data);
2395 ifd->ifi_ipackets = counter_u64_fetch(ifa->ifa_ipackets);
2396 ifd->ifi_opackets = counter_u64_fetch(ifa->ifa_opackets);
2397 ifd->ifi_ibytes = counter_u64_fetch(ifa->ifa_ibytes);
2398 ifd->ifi_obytes = counter_u64_fetch(ifa->ifa_obytes);
2399
2400 /* Fixup if_data carp(4) vhid. */
2401 if (carp_get_vhid_p != NULL)
2402 ifd->ifi_vhid = (*carp_get_vhid_p)(ifa);
2403
2404 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
2405 }
2406
2407 static int
sysctl_iflist_ifam(struct ifaddr * ifa,struct rt_addrinfo * info,struct walkarg * w,int len)2408 sysctl_iflist_ifam(struct ifaddr *ifa, struct rt_addrinfo *info,
2409 struct walkarg *w, int len)
2410 {
2411 struct ifa_msghdr *ifam;
2412
2413 ifam = (struct ifa_msghdr *)w->w_tmem;
2414 ifam->ifam_addrs = info->rti_addrs;
2415 ifam->ifam_flags = ifa->ifa_flags;
2416 ifam->ifam_index = ifa->ifa_ifp->if_index;
2417 ifam->_ifam_spare1 = 0;
2418 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
2419
2420 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
2421 }
2422
2423 static int
sysctl_iflist(int af,struct walkarg * w)2424 sysctl_iflist(int af, struct walkarg *w)
2425 {
2426 struct ifnet *ifp;
2427 struct ifaddr *ifa;
2428 struct if_data ifd;
2429 struct rt_addrinfo info;
2430 int len, error = 0;
2431 struct sockaddr_storage ss;
2432
2433 bzero((caddr_t)&info, sizeof(info));
2434 bzero(&ifd, sizeof(ifd));
2435 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2436 if (w->w_arg && w->w_arg != ifp->if_index)
2437 continue;
2438 if_data_copy(ifp, &ifd);
2439 ifa = ifp->if_addr;
2440 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
2441 error = rtsock_msg_buffer(RTM_IFINFO, &info, w, &len);
2442 if (error != 0)
2443 goto done;
2444 info.rti_info[RTAX_IFP] = NULL;
2445 if (w->w_req && w->w_tmem) {
2446 if (w->w_op == NET_RT_IFLISTL)
2447 error = sysctl_iflist_ifml(ifp, &ifd, &info, w,
2448 len);
2449 else
2450 error = sysctl_iflist_ifm(ifp, &ifd, &info, w,
2451 len);
2452 if (error)
2453 goto done;
2454 }
2455 while ((ifa = CK_STAILQ_NEXT(ifa, ifa_link)) != NULL) {
2456 if (af && af != ifa->ifa_addr->sa_family)
2457 continue;
2458 if (prison_if(w->w_req->td->td_ucred,
2459 ifa->ifa_addr) != 0)
2460 continue;
2461 info.rti_info[RTAX_IFA] = ifa->ifa_addr;
2462 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(
2463 ifa->ifa_addr, ifa->ifa_netmask, &ss);
2464 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
2465 error = rtsock_msg_buffer(RTM_NEWADDR, &info, w, &len);
2466 if (error != 0)
2467 goto done;
2468 if (w->w_req && w->w_tmem) {
2469 if (w->w_op == NET_RT_IFLISTL)
2470 error = sysctl_iflist_ifaml(ifa, &info,
2471 w, len);
2472 else
2473 error = sysctl_iflist_ifam(ifa, &info,
2474 w, len);
2475 if (error)
2476 goto done;
2477 }
2478 }
2479 info.rti_info[RTAX_IFA] = NULL;
2480 info.rti_info[RTAX_NETMASK] = NULL;
2481 info.rti_info[RTAX_BRD] = NULL;
2482 }
2483 done:
2484 return (error);
2485 }
2486
2487 static int
sysctl_ifmalist(int af,struct walkarg * w)2488 sysctl_ifmalist(int af, struct walkarg *w)
2489 {
2490 struct rt_addrinfo info;
2491 struct ifaddr *ifa;
2492 struct ifmultiaddr *ifma;
2493 struct ifnet *ifp;
2494 int error, len;
2495
2496 NET_EPOCH_ASSERT();
2497
2498 error = 0;
2499 bzero((caddr_t)&info, sizeof(info));
2500
2501 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2502 if (w->w_arg && w->w_arg != ifp->if_index)
2503 continue;
2504 ifa = ifp->if_addr;
2505 info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL;
2506 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2507 if (af && af != ifma->ifma_addr->sa_family)
2508 continue;
2509 if (prison_if(w->w_req->td->td_ucred,
2510 ifma->ifma_addr) != 0)
2511 continue;
2512 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
2513 info.rti_info[RTAX_GATEWAY] =
2514 (ifma->ifma_addr->sa_family != AF_LINK) ?
2515 ifma->ifma_lladdr : NULL;
2516 error = rtsock_msg_buffer(RTM_NEWMADDR, &info, w, &len);
2517 if (error != 0)
2518 break;
2519 if (w->w_req && w->w_tmem) {
2520 struct ifma_msghdr *ifmam;
2521
2522 ifmam = (struct ifma_msghdr *)w->w_tmem;
2523 ifmam->ifmam_index = ifma->ifma_ifp->if_index;
2524 ifmam->ifmam_flags = 0;
2525 ifmam->ifmam_addrs = info.rti_addrs;
2526 ifmam->_ifmam_spare1 = 0;
2527 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
2528 if (error != 0)
2529 break;
2530 }
2531 }
2532 if (error != 0)
2533 break;
2534 }
2535 return (error);
2536 }
2537
2538 static void
rtable_sysctl_dump(uint32_t fibnum,int family,struct walkarg * w)2539 rtable_sysctl_dump(uint32_t fibnum, int family, struct walkarg *w)
2540 {
2541 union sockaddr_union sa_dst, sa_mask;
2542
2543 w->family = family;
2544 w->dst = (struct sockaddr *)&sa_dst;
2545 w->mask = (struct sockaddr *)&sa_mask;
2546
2547 init_sockaddrs_family(family, w->dst, w->mask);
2548
2549 rib_walk(fibnum, family, false, sysctl_dumpentry, w);
2550 }
2551
2552 static int
sysctl_rtsock(SYSCTL_HANDLER_ARGS)2553 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
2554 {
2555 struct epoch_tracker et;
2556 int *name = (int *)arg1;
2557 u_int namelen = arg2;
2558 struct rib_head *rnh = NULL; /* silence compiler. */
2559 int i, lim, error = EINVAL;
2560 int fib = 0;
2561 u_char af;
2562 struct walkarg w;
2563
2564 if (namelen < 3)
2565 return (EINVAL);
2566
2567 name++;
2568 namelen--;
2569 if (req->newptr)
2570 return (EPERM);
2571 if (name[1] == NET_RT_DUMP || name[1] == NET_RT_NHOP || name[1] == NET_RT_NHGRP) {
2572 if (namelen == 3)
2573 fib = req->td->td_proc->p_fibnum;
2574 else if (namelen == 4)
2575 fib = (name[3] == RT_ALL_FIBS) ?
2576 req->td->td_proc->p_fibnum : name[3];
2577 else
2578 return ((namelen < 3) ? EISDIR : ENOTDIR);
2579 if (fib < 0 || fib >= rt_numfibs)
2580 return (EINVAL);
2581 } else if (namelen != 3)
2582 return ((namelen < 3) ? EISDIR : ENOTDIR);
2583 af = name[0];
2584 if (af > AF_MAX)
2585 return (EINVAL);
2586 bzero(&w, sizeof(w));
2587 w.w_op = name[1];
2588 w.w_arg = name[2];
2589 w.w_req = req;
2590
2591 error = sysctl_wire_old_buffer(req, 0);
2592 if (error)
2593 return (error);
2594
2595 /*
2596 * Allocate reply buffer in advance.
2597 * All rtsock messages has maximum length of u_short.
2598 */
2599 w.w_tmemsize = 65536;
2600 w.w_tmem = malloc(w.w_tmemsize, M_TEMP, M_WAITOK);
2601
2602 NET_EPOCH_ENTER(et);
2603 switch (w.w_op) {
2604 case NET_RT_DUMP:
2605 case NET_RT_FLAGS:
2606 if (af == 0) { /* dump all tables */
2607 i = 1;
2608 lim = AF_MAX;
2609 } else /* dump only one table */
2610 i = lim = af;
2611
2612 /*
2613 * take care of llinfo entries, the caller must
2614 * specify an AF
2615 */
2616 if (w.w_op == NET_RT_FLAGS &&
2617 (w.w_arg == 0 || w.w_arg & RTF_LLINFO)) {
2618 if (af != 0)
2619 error = lltable_sysctl_dumparp(af, w.w_req);
2620 else
2621 error = EINVAL;
2622 break;
2623 }
2624 /*
2625 * take care of routing entries
2626 */
2627 for (error = 0; error == 0 && i <= lim; i++) {
2628 rnh = rt_tables_get_rnh(fib, i);
2629 if (rnh != NULL) {
2630 rtable_sysctl_dump(fib, i, &w);
2631 } else if (af != 0)
2632 error = EAFNOSUPPORT;
2633 }
2634 break;
2635 case NET_RT_NHOP:
2636 case NET_RT_NHGRP:
2637 /* Allow dumping one specific af/fib at a time */
2638 if (namelen < 4) {
2639 error = EINVAL;
2640 break;
2641 }
2642 fib = name[3];
2643 if (fib < 0 || fib > rt_numfibs) {
2644 error = EINVAL;
2645 break;
2646 }
2647 rnh = rt_tables_get_rnh(fib, af);
2648 if (rnh == NULL) {
2649 error = EAFNOSUPPORT;
2650 break;
2651 }
2652 if (w.w_op == NET_RT_NHOP)
2653 error = nhops_dump_sysctl(rnh, w.w_req);
2654 else
2655 #ifdef ROUTE_MPATH
2656 error = nhgrp_dump_sysctl(rnh, w.w_req);
2657 #else
2658 error = ENOTSUP;
2659 #endif
2660 break;
2661 case NET_RT_IFLIST:
2662 case NET_RT_IFLISTL:
2663 error = sysctl_iflist(af, &w);
2664 break;
2665
2666 case NET_RT_IFMALIST:
2667 error = sysctl_ifmalist(af, &w);
2668 break;
2669 }
2670 NET_EPOCH_EXIT(et);
2671
2672 free(w.w_tmem, M_TEMP);
2673 return (error);
2674 }
2675
2676 static SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD | CTLFLAG_MPSAFE,
2677 sysctl_rtsock, "Return route tables and interface/address lists");
2678
2679 /*
2680 * Definitions of protocols supported in the ROUTE domain.
2681 */
2682
2683 static struct domain routedomain; /* or at least forward */
2684
2685 static struct protosw routesw = {
2686 .pr_type = SOCK_RAW,
2687 .pr_flags = PR_ATOMIC|PR_ADDR,
2688 .pr_abort = rts_close,
2689 .pr_attach = rts_attach,
2690 .pr_detach = rts_detach,
2691 .pr_send = rts_send,
2692 .pr_shutdown = rts_shutdown,
2693 .pr_disconnect = rts_disconnect,
2694 .pr_close = rts_close,
2695 };
2696
2697 static struct domain routedomain = {
2698 .dom_family = PF_ROUTE,
2699 .dom_name = "route",
2700 .dom_nprotosw = 1,
2701 .dom_protosw = { &routesw },
2702 };
2703
2704 DOMAIN_SET(route);
2705