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