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