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$
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 if (rtm->rtm_flags & RTF_RNH_LOCKED)
619 senderr(EINVAL);
620 info.rti_flags = rtm->rtm_flags;
621 if (info.rti_info[RTAX_DST] == NULL ||
622 info.rti_info[RTAX_DST]->sa_family >= AF_MAX ||
623 (info.rti_info[RTAX_GATEWAY] != NULL &&
624 info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX))
625 senderr(EINVAL);
626 saf = info.rti_info[RTAX_DST]->sa_family;
627 /*
628 * Verify that the caller has the appropriate privilege; RTM_GET
629 * is the only operation the non-superuser is allowed.
630 */
631 if (rtm->rtm_type != RTM_GET) {
632 error = priv_check(curthread, PRIV_NET_ROUTE);
633 if (error)
634 senderr(error);
635 }
636
637 /*
638 * The given gateway address may be an interface address.
639 * For example, issuing a "route change" command on a route
640 * entry that was created from a tunnel, and the gateway
641 * address given is the local end point. In this case the
642 * RTF_GATEWAY flag must be cleared or the destination will
643 * not be reachable even though there is no error message.
644 */
645 if (info.rti_info[RTAX_GATEWAY] != NULL &&
646 info.rti_info[RTAX_GATEWAY]->sa_family != AF_LINK) {
647 struct rt_addrinfo ginfo;
648 struct sockaddr *gdst;
649
650 bzero(&ginfo, sizeof(ginfo));
651 bzero(&ss, sizeof(ss));
652 ss.ss_len = sizeof(ss);
653
654 ginfo.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&ss;
655 gdst = info.rti_info[RTAX_GATEWAY];
656
657 /*
658 * A host route through the loopback interface is
659 * installed for each interface adddress. In pre 8.0
660 * releases the interface address of a PPP link type
661 * is not reachable locally. This behavior is fixed as
662 * part of the new L2/L3 redesign and rewrite work. The
663 * signature of this interface address route is the
664 * AF_LINK sa_family type of the rt_gateway, and the
665 * rt_ifp has the IFF_LOOPBACK flag set.
666 */
667 if (rib_lookup_info(fibnum, gdst, NHR_REF, 0, &ginfo) == 0) {
668 if (ss.ss_family == AF_LINK &&
669 ginfo.rti_ifp->if_flags & IFF_LOOPBACK) {
670 info.rti_flags &= ~RTF_GATEWAY;
671 info.rti_flags |= RTF_GWFLAG_COMPAT;
672 }
673 rib_free_info(&ginfo);
674 }
675 }
676
677 switch (rtm->rtm_type) {
678 struct rtentry *saved_nrt;
679
680 case RTM_ADD:
681 case RTM_CHANGE:
682 if (rtm->rtm_type == RTM_ADD) {
683 if (info.rti_info[RTAX_GATEWAY] == NULL)
684 senderr(EINVAL);
685 }
686 saved_nrt = NULL;
687
688 /* support for new ARP code */
689 if (info.rti_info[RTAX_GATEWAY] != NULL &&
690 info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK &&
691 (rtm->rtm_flags & RTF_LLDATA) != 0) {
692 error = lla_rt_output(rtm, &info);
693 #ifdef INET6
694 if (error == 0)
695 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
696 #endif
697 break;
698 }
699 error = rtrequest1_fib(rtm->rtm_type, &info, &saved_nrt,
700 fibnum);
701 if (error == 0 && saved_nrt != NULL) {
702 #ifdef INET6
703 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
704 #endif
705 RT_LOCK(saved_nrt);
706 rtm->rtm_index = saved_nrt->rt_ifp->if_index;
707 RT_REMREF(saved_nrt);
708 RT_UNLOCK(saved_nrt);
709 }
710 break;
711
712 case RTM_DELETE:
713 saved_nrt = NULL;
714 /* support for new ARP code */
715 if (info.rti_info[RTAX_GATEWAY] &&
716 (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) &&
717 (rtm->rtm_flags & RTF_LLDATA) != 0) {
718 error = lla_rt_output(rtm, &info);
719 #ifdef INET6
720 if (error == 0)
721 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
722 #endif
723 break;
724 }
725 error = rtrequest1_fib(RTM_DELETE, &info, &saved_nrt, fibnum);
726 if (error == 0) {
727 RT_LOCK(saved_nrt);
728 rt = saved_nrt;
729 goto report;
730 }
731 #ifdef INET6
732 /* rt_msg2() will not be used when RTM_DELETE fails. */
733 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
734 #endif
735 break;
736
737 case RTM_GET:
738 rnh = rt_tables_get_rnh(fibnum, saf);
739 if (rnh == NULL)
740 senderr(EAFNOSUPPORT);
741
742 RIB_RLOCK(rnh);
743
744 if (info.rti_info[RTAX_NETMASK] == NULL &&
745 rtm->rtm_type == RTM_GET) {
746 /*
747 * Provide longest prefix match for
748 * address lookup (no mask).
749 * 'route -n get addr'
750 */
751 rt = (struct rtentry *) rnh->rnh_matchaddr(
752 info.rti_info[RTAX_DST], &rnh->head);
753 } else
754 rt = (struct rtentry *) rnh->rnh_lookup(
755 info.rti_info[RTAX_DST],
756 info.rti_info[RTAX_NETMASK], &rnh->head);
757
758 if (rt == NULL) {
759 RIB_RUNLOCK(rnh);
760 senderr(ESRCH);
761 }
762 #ifdef RADIX_MPATH
763 /*
764 * for RTM_CHANGE/LOCK, if we got multipath routes,
765 * we require users to specify a matching RTAX_GATEWAY.
766 *
767 * for RTM_GET, gate is optional even with multipath.
768 * if gate == NULL the first match is returned.
769 * (no need to call rt_mpath_matchgate if gate == NULL)
770 */
771 if (rt_mpath_capable(rnh) &&
772 (rtm->rtm_type != RTM_GET || info.rti_info[RTAX_GATEWAY])) {
773 rt = rt_mpath_matchgate(rt, info.rti_info[RTAX_GATEWAY]);
774 if (!rt) {
775 RIB_RUNLOCK(rnh);
776 senderr(ESRCH);
777 }
778 }
779 #endif
780 /*
781 * If performing proxied L2 entry insertion, and
782 * the actual PPP host entry is found, perform
783 * another search to retrieve the prefix route of
784 * the local end point of the PPP link.
785 */
786 if (rtm->rtm_flags & RTF_ANNOUNCE) {
787 struct sockaddr laddr;
788
789 if (rt->rt_ifp != NULL &&
790 rt->rt_ifp->if_type == IFT_PROPVIRTUAL) {
791 struct ifaddr *ifa;
792
793 NET_EPOCH_ENTER();
794 ifa = ifa_ifwithnet(info.rti_info[RTAX_DST], 1,
795 RT_ALL_FIBS);
796 if (ifa != NULL)
797 rt_maskedcopy(ifa->ifa_addr,
798 &laddr,
799 ifa->ifa_netmask);
800 NET_EPOCH_EXIT();
801 } else
802 rt_maskedcopy(rt->rt_ifa->ifa_addr,
803 &laddr,
804 rt->rt_ifa->ifa_netmask);
805 /*
806 * refactor rt and no lock operation necessary
807 */
808 rt = (struct rtentry *)rnh->rnh_matchaddr(&laddr,
809 &rnh->head);
810 if (rt == NULL) {
811 RIB_RUNLOCK(rnh);
812 senderr(ESRCH);
813 }
814 }
815 RT_LOCK(rt);
816 RT_ADDREF(rt);
817 RIB_RUNLOCK(rnh);
818
819 report:
820 RT_LOCK_ASSERT(rt);
821 if ((rt->rt_flags & RTF_HOST) == 0
822 ? jailed_without_vnet(curthread->td_ucred)
823 : prison_if(curthread->td_ucred,
824 rt_key(rt)) != 0) {
825 RT_UNLOCK(rt);
826 senderr(ESRCH);
827 }
828 info.rti_info[RTAX_DST] = rt_key(rt);
829 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
830 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(rt_key(rt),
831 rt_mask(rt), &ss);
832 info.rti_info[RTAX_GENMASK] = 0;
833 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
834 ifp = rt->rt_ifp;
835 if (ifp) {
836 info.rti_info[RTAX_IFP] =
837 ifp->if_addr->ifa_addr;
838 error = rtm_get_jailed(&info, ifp, rt,
839 &saun, curthread->td_ucred);
840 if (error != 0) {
841 RT_UNLOCK(rt);
842 senderr(error);
843 }
844 if (ifp->if_flags & IFF_POINTOPOINT)
845 info.rti_info[RTAX_BRD] =
846 rt->rt_ifa->ifa_dstaddr;
847 rtm->rtm_index = ifp->if_index;
848 } else {
849 info.rti_info[RTAX_IFP] = NULL;
850 info.rti_info[RTAX_IFA] = NULL;
851 }
852 } else if ((ifp = rt->rt_ifp) != NULL) {
853 rtm->rtm_index = ifp->if_index;
854 }
855
856 /* Check if we need to realloc storage */
857 rtsock_msg_buffer(rtm->rtm_type, &info, NULL, &len);
858 if (len > alloc_len) {
859 struct rt_msghdr *new_rtm;
860 new_rtm = malloc(len, M_TEMP, M_NOWAIT);
861 if (new_rtm == NULL) {
862 RT_UNLOCK(rt);
863 senderr(ENOBUFS);
864 }
865 bcopy(rtm, new_rtm, rtm->rtm_msglen);
866 free(rtm, M_TEMP);
867 rtm = new_rtm;
868 alloc_len = len;
869 }
870
871 w.w_tmem = (caddr_t)rtm;
872 w.w_tmemsize = alloc_len;
873 rtsock_msg_buffer(rtm->rtm_type, &info, &w, &len);
874
875 if (rt->rt_flags & RTF_GWFLAG_COMPAT)
876 rtm->rtm_flags = RTF_GATEWAY |
877 (rt->rt_flags & ~RTF_GWFLAG_COMPAT);
878 else
879 rtm->rtm_flags = rt->rt_flags;
880 rt_getmetrics(rt, &rtm->rtm_rmx);
881 rtm->rtm_addrs = info.rti_addrs;
882
883 RT_UNLOCK(rt);
884 break;
885
886 default:
887 senderr(EOPNOTSUPP);
888 }
889
890 flush:
891 if (rt != NULL)
892 RTFREE(rt);
893 /*
894 * Check to see if we don't want our own messages.
895 */
896 if ((so->so_options & SO_USELOOPBACK) == 0) {
897 if (V_route_cb.any_count <= 1) {
898 if (rtm != NULL)
899 free(rtm, M_TEMP);
900 m_freem(m);
901 return (error);
902 }
903 /* There is another listener, so construct message */
904 rp = sotorawcb(so);
905 }
906
907 if (rtm != NULL) {
908 #ifdef INET6
909 if (rti_need_deembed) {
910 /* sin6_scope_id is recovered before sending rtm. */
911 sin6 = (struct sockaddr_in6 *)&ss;
912 for (i = 0; i < RTAX_MAX; i++) {
913 if (info.rti_info[i] == NULL)
914 continue;
915 if (info.rti_info[i]->sa_family != AF_INET6)
916 continue;
917 bcopy(info.rti_info[i], sin6, sizeof(*sin6));
918 if (sa6_recoverscope(sin6) == 0)
919 bcopy(sin6, info.rti_info[i],
920 sizeof(*sin6));
921 }
922 }
923 #endif
924 if (error != 0)
925 rtm->rtm_errno = error;
926 else
927 rtm->rtm_flags |= RTF_DONE;
928
929 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
930 if (m->m_pkthdr.len < rtm->rtm_msglen) {
931 m_freem(m);
932 m = NULL;
933 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
934 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
935
936 free(rtm, M_TEMP);
937 }
938 if (m != NULL) {
939 M_SETFIB(m, fibnum);
940 m->m_flags |= RTS_FILTER_FIB;
941 if (rp) {
942 /*
943 * XXX insure we don't get a copy by
944 * invalidating our protocol
945 */
946 unsigned short family = rp->rcb_proto.sp_family;
947 rp->rcb_proto.sp_family = 0;
948 rt_dispatch(m, saf);
949 rp->rcb_proto.sp_family = family;
950 } else
951 rt_dispatch(m, saf);
952 }
953
954 return (error);
955 }
956
957 static void
958 rt_getmetrics(const struct rtentry *rt, struct rt_metrics *out)
959 {
960
961 bzero(out, sizeof(*out));
962 out->rmx_mtu = rt->rt_mtu;
963 out->rmx_weight = rt->rt_weight;
964 out->rmx_pksent = counter_u64_fetch(rt->rt_pksent);
965 /* Kernel -> userland timebase conversion. */
966 out->rmx_expire = rt->rt_expire ?
967 rt->rt_expire - time_uptime + time_second : 0;
968 }
969
970 /*
971 * Extract the addresses of the passed sockaddrs.
972 * Do a little sanity checking so as to avoid bad memory references.
973 * This data is derived straight from userland.
974 */
975 static int
976 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
977 {
978 struct sockaddr *sa;
979 int i;
980
981 for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
982 if ((rtinfo->rti_addrs & (1 << i)) == 0)
983 continue;
984 sa = (struct sockaddr *)cp;
985 /*
986 * It won't fit.
987 */
988 if (cp + sa->sa_len > cplim)
989 return (EINVAL);
990 /*
991 * there are no more.. quit now
992 * If there are more bits, they are in error.
993 * I've seen this. route(1) can evidently generate these.
994 * This causes kernel to core dump.
995 * for compatibility, If we see this, point to a safe address.
996 */
997 if (sa->sa_len == 0) {
998 rtinfo->rti_info[i] = &sa_zero;
999 return (0); /* should be EINVAL but for compat */
1000 }
1001 /* accept it */
1002 #ifdef INET6
1003 if (sa->sa_family == AF_INET6)
1004 sa6_embedscope((struct sockaddr_in6 *)sa,
1005 V_ip6_use_defzone);
1006 #endif
1007 rtinfo->rti_info[i] = sa;
1008 cp += SA_SIZE(sa);
1009 }
1010 return (0);
1011 }
1012
1013 /*
1014 * Fill in @dmask with valid netmask leaving original @smask
1015 * intact. Mostly used with radix netmasks.
1016 */
1017 static struct sockaddr *
1018 rtsock_fix_netmask(struct sockaddr *dst, struct sockaddr *smask,
1019 struct sockaddr_storage *dmask)
1020 {
1021 if (dst == NULL || smask == NULL)
1022 return (NULL);
1023
1024 memset(dmask, 0, dst->sa_len);
1025 memcpy(dmask, smask, smask->sa_len);
1026 dmask->ss_len = dst->sa_len;
1027 dmask->ss_family = dst->sa_family;
1028
1029 return ((struct sockaddr *)dmask);
1030 }
1031
1032 /*
1033 * Writes information related to @rtinfo object to newly-allocated mbuf.
1034 * Assumes MCLBYTES is enough to construct any message.
1035 * Used for OS notifications of vaious events (if/ifa announces,etc)
1036 *
1037 * Returns allocated mbuf or NULL on failure.
1038 */
1039 static struct mbuf *
1040 rtsock_msg_mbuf(int type, struct rt_addrinfo *rtinfo)
1041 {
1042 struct rt_msghdr *rtm;
1043 struct mbuf *m;
1044 int i;
1045 struct sockaddr *sa;
1046 #ifdef INET6
1047 struct sockaddr_storage ss;
1048 struct sockaddr_in6 *sin6;
1049 #endif
1050 int len, dlen;
1051
1052 switch (type) {
1053
1054 case RTM_DELADDR:
1055 case RTM_NEWADDR:
1056 len = sizeof(struct ifa_msghdr);
1057 break;
1058
1059 case RTM_DELMADDR:
1060 case RTM_NEWMADDR:
1061 len = sizeof(struct ifma_msghdr);
1062 break;
1063
1064 case RTM_IFINFO:
1065 len = sizeof(struct if_msghdr);
1066 break;
1067
1068 case RTM_IFANNOUNCE:
1069 case RTM_IEEE80211:
1070 len = sizeof(struct if_announcemsghdr);
1071 break;
1072
1073 default:
1074 len = sizeof(struct rt_msghdr);
1075 }
1076
1077 /* XXXGL: can we use MJUMPAGESIZE cluster here? */
1078 KASSERT(len <= MCLBYTES, ("%s: message too big", __func__));
1079 if (len > MHLEN)
1080 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1081 else
1082 m = m_gethdr(M_NOWAIT, MT_DATA);
1083 if (m == NULL)
1084 return (m);
1085
1086 m->m_pkthdr.len = m->m_len = len;
1087 rtm = mtod(m, struct rt_msghdr *);
1088 bzero((caddr_t)rtm, len);
1089 for (i = 0; i < RTAX_MAX; i++) {
1090 if ((sa = rtinfo->rti_info[i]) == NULL)
1091 continue;
1092 rtinfo->rti_addrs |= (1 << i);
1093 dlen = SA_SIZE(sa);
1094 #ifdef INET6
1095 if (V_deembed_scopeid && sa->sa_family == AF_INET6) {
1096 sin6 = (struct sockaddr_in6 *)&ss;
1097 bcopy(sa, sin6, sizeof(*sin6));
1098 if (sa6_recoverscope(sin6) == 0)
1099 sa = (struct sockaddr *)sin6;
1100 }
1101 #endif
1102 m_copyback(m, len, dlen, (caddr_t)sa);
1103 len += dlen;
1104 }
1105 if (m->m_pkthdr.len != len) {
1106 m_freem(m);
1107 return (NULL);
1108 }
1109 rtm->rtm_msglen = len;
1110 rtm->rtm_version = RTM_VERSION;
1111 rtm->rtm_type = type;
1112 return (m);
1113 }
1114
1115 /*
1116 * Writes information related to @rtinfo object to preallocated buffer.
1117 * Stores needed size in @plen. If @w is NULL, calculates size without
1118 * writing.
1119 * Used for sysctl dumps and rtsock answers (RTM_DEL/RTM_GET) generation.
1120 *
1121 * Returns 0 on success.
1122 *
1123 */
1124 static int
1125 rtsock_msg_buffer(int type, struct rt_addrinfo *rtinfo, struct walkarg *w, int *plen)
1126 {
1127 int i;
1128 int len, buflen = 0, dlen;
1129 caddr_t cp = NULL;
1130 struct rt_msghdr *rtm = NULL;
1131 #ifdef INET6
1132 struct sockaddr_storage ss;
1133 struct sockaddr_in6 *sin6;
1134 #endif
1135 #ifdef COMPAT_FREEBSD32
1136 bool compat32 = false;
1137 #endif
1138
1139 switch (type) {
1140
1141 case RTM_DELADDR:
1142 case RTM_NEWADDR:
1143 if (w != NULL && w->w_op == NET_RT_IFLISTL) {
1144 #ifdef COMPAT_FREEBSD32
1145 if (w->w_req->flags & SCTL_MASK32) {
1146 len = sizeof(struct ifa_msghdrl32);
1147 compat32 = true;
1148 } else
1149 #endif
1150 len = sizeof(struct ifa_msghdrl);
1151 } else
1152 len = sizeof(struct ifa_msghdr);
1153 break;
1154
1155 case RTM_IFINFO:
1156 #ifdef COMPAT_FREEBSD32
1157 if (w != NULL && w->w_req->flags & SCTL_MASK32) {
1158 if (w->w_op == NET_RT_IFLISTL)
1159 len = sizeof(struct if_msghdrl32);
1160 else
1161 len = sizeof(struct if_msghdr32);
1162 compat32 = true;
1163 break;
1164 }
1165 #endif
1166 if (w != NULL && w->w_op == NET_RT_IFLISTL)
1167 len = sizeof(struct if_msghdrl);
1168 else
1169 len = sizeof(struct if_msghdr);
1170 break;
1171
1172 case RTM_NEWMADDR:
1173 len = sizeof(struct ifma_msghdr);
1174 break;
1175
1176 default:
1177 len = sizeof(struct rt_msghdr);
1178 }
1179
1180 if (w != NULL) {
1181 rtm = (struct rt_msghdr *)w->w_tmem;
1182 buflen = w->w_tmemsize - len;
1183 cp = (caddr_t)w->w_tmem + len;
1184 }
1185
1186 rtinfo->rti_addrs = 0;
1187 for (i = 0; i < RTAX_MAX; i++) {
1188 struct sockaddr *sa;
1189
1190 if ((sa = rtinfo->rti_info[i]) == NULL)
1191 continue;
1192 rtinfo->rti_addrs |= (1 << i);
1193 #ifdef COMPAT_FREEBSD32
1194 if (compat32)
1195 dlen = SA_SIZE32(sa);
1196 else
1197 #endif
1198 dlen = SA_SIZE(sa);
1199 if (cp != NULL && buflen >= dlen) {
1200 #ifdef INET6
1201 if (V_deembed_scopeid && sa->sa_family == AF_INET6) {
1202 sin6 = (struct sockaddr_in6 *)&ss;
1203 bcopy(sa, sin6, sizeof(*sin6));
1204 if (sa6_recoverscope(sin6) == 0)
1205 sa = (struct sockaddr *)sin6;
1206 }
1207 #endif
1208 bcopy((caddr_t)sa, cp, (unsigned)dlen);
1209 cp += dlen;
1210 buflen -= dlen;
1211 } else if (cp != NULL) {
1212 /*
1213 * Buffer too small. Count needed size
1214 * and return with error.
1215 */
1216 cp = NULL;
1217 }
1218
1219 len += dlen;
1220 }
1221
1222 if (cp != NULL) {
1223 dlen = ALIGN(len) - len;
1224 if (buflen < dlen)
1225 cp = NULL;
1226 else {
1227 bzero(cp, dlen);
1228 cp += dlen;
1229 buflen -= dlen;
1230 }
1231 }
1232 len = ALIGN(len);
1233
1234 if (cp != NULL) {
1235 /* fill header iff buffer is large enough */
1236 rtm->rtm_version = RTM_VERSION;
1237 rtm->rtm_type = type;
1238 rtm->rtm_msglen = len;
1239 }
1240
1241 *plen = len;
1242
1243 if (w != NULL && cp == NULL)
1244 return (ENOBUFS);
1245
1246 return (0);
1247 }
1248
1249 /*
1250 * This routine is called to generate a message from the routing
1251 * socket indicating that a redirect has occurred, a routing lookup
1252 * has failed, or that a protocol has detected timeouts to a particular
1253 * destination.
1254 */
1255 void
1256 rt_missmsg_fib(int type, struct rt_addrinfo *rtinfo, int flags, int error,
1257 int fibnum)
1258 {
1259 struct rt_msghdr *rtm;
1260 struct mbuf *m;
1261 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
1262
1263 if (V_route_cb.any_count == 0)
1264 return;
1265 m = rtsock_msg_mbuf(type, rtinfo);
1266 if (m == NULL)
1267 return;
1268
1269 if (fibnum != RT_ALL_FIBS) {
1270 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out "
1271 "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs));
1272 M_SETFIB(m, fibnum);
1273 m->m_flags |= RTS_FILTER_FIB;
1274 }
1275
1276 rtm = mtod(m, struct rt_msghdr *);
1277 rtm->rtm_flags = RTF_DONE | flags;
1278 rtm->rtm_errno = error;
1279 rtm->rtm_addrs = rtinfo->rti_addrs;
1280 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1281 }
1282
1283 void
1284 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
1285 {
1286
1287 rt_missmsg_fib(type, rtinfo, flags, error, RT_ALL_FIBS);
1288 }
1289
1290 /*
1291 * This routine is called to generate a message from the routing
1292 * socket indicating that the status of a network interface has changed.
1293 */
1294 void
1295 rt_ifmsg(struct ifnet *ifp)
1296 {
1297 struct if_msghdr *ifm;
1298 struct mbuf *m;
1299 struct rt_addrinfo info;
1300
1301 if (V_route_cb.any_count == 0)
1302 return;
1303 bzero((caddr_t)&info, sizeof(info));
1304 m = rtsock_msg_mbuf(RTM_IFINFO, &info);
1305 if (m == NULL)
1306 return;
1307 ifm = mtod(m, struct if_msghdr *);
1308 ifm->ifm_index = ifp->if_index;
1309 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1310 if_data_copy(ifp, &ifm->ifm_data);
1311 ifm->ifm_addrs = 0;
1312 rt_dispatch(m, AF_UNSPEC);
1313 }
1314
1315 /*
1316 * Announce interface address arrival/withdraw.
1317 * Please do not call directly, use rt_addrmsg().
1318 * Assume input data to be valid.
1319 * Returns 0 on success.
1320 */
1321 int
1322 rtsock_addrmsg(int cmd, struct ifaddr *ifa, int fibnum)
1323 {
1324 struct rt_addrinfo info;
1325 struct sockaddr *sa;
1326 int ncmd;
1327 struct mbuf *m;
1328 struct ifa_msghdr *ifam;
1329 struct ifnet *ifp = ifa->ifa_ifp;
1330 struct sockaddr_storage ss;
1331
1332 if (V_route_cb.any_count == 0)
1333 return (0);
1334
1335 ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
1336
1337 bzero((caddr_t)&info, sizeof(info));
1338 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1339 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1340 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(
1341 info.rti_info[RTAX_IFP], ifa->ifa_netmask, &ss);
1342 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1343 if ((m = rtsock_msg_mbuf(ncmd, &info)) == NULL)
1344 return (ENOBUFS);
1345 ifam = mtod(m, struct ifa_msghdr *);
1346 ifam->ifam_index = ifp->if_index;
1347 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1348 ifam->ifam_flags = ifa->ifa_flags;
1349 ifam->ifam_addrs = info.rti_addrs;
1350
1351 if (fibnum != RT_ALL_FIBS) {
1352 M_SETFIB(m, fibnum);
1353 m->m_flags |= RTS_FILTER_FIB;
1354 }
1355
1356 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1357
1358 return (0);
1359 }
1360
1361 /*
1362 * Announce route addition/removal.
1363 * Please do not call directly, use rt_routemsg().
1364 * Note that @rt data MAY be inconsistent/invalid:
1365 * if some userland app sends us "invalid" route message (invalid mask,
1366 * no dst, wrong address families, etc...) we need to pass it back
1367 * to app (and any other rtsock consumers) with rtm_errno field set to
1368 * non-zero value.
1369 *
1370 * Returns 0 on success.
1371 */
1372 int
1373 rtsock_routemsg(int cmd, struct ifnet *ifp, int error, struct rtentry *rt,
1374 int fibnum)
1375 {
1376 struct rt_addrinfo info;
1377 struct sockaddr *sa;
1378 struct mbuf *m;
1379 struct rt_msghdr *rtm;
1380 struct sockaddr_storage ss;
1381
1382 if (V_route_cb.any_count == 0)
1383 return (0);
1384
1385 bzero((caddr_t)&info, sizeof(info));
1386 info.rti_info[RTAX_DST] = sa = rt_key(rt);
1387 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(sa, rt_mask(rt), &ss);
1388 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1389 if ((m = rtsock_msg_mbuf(cmd, &info)) == NULL)
1390 return (ENOBUFS);
1391 rtm = mtod(m, struct rt_msghdr *);
1392 rtm->rtm_index = ifp->if_index;
1393 rtm->rtm_flags |= rt->rt_flags;
1394 rtm->rtm_errno = error;
1395 rtm->rtm_addrs = info.rti_addrs;
1396
1397 if (fibnum != RT_ALL_FIBS) {
1398 M_SETFIB(m, fibnum);
1399 m->m_flags |= RTS_FILTER_FIB;
1400 }
1401
1402 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1403
1404 return (0);
1405 }
1406
1407 /*
1408 * This is the analogue to the rt_newaddrmsg which performs the same
1409 * function but for multicast group memberhips. This is easier since
1410 * there is no route state to worry about.
1411 */
1412 void
1413 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
1414 {
1415 struct rt_addrinfo info;
1416 struct mbuf *m = NULL;
1417 struct ifnet *ifp = ifma->ifma_ifp;
1418 struct ifma_msghdr *ifmam;
1419
1420 if (V_route_cb.any_count == 0)
1421 return;
1422
1423 bzero((caddr_t)&info, sizeof(info));
1424 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1425 if (ifp && ifp->if_addr)
1426 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1427 else
1428 info.rti_info[RTAX_IFP] = NULL;
1429 /*
1430 * If a link-layer address is present, present it as a ``gateway''
1431 * (similarly to how ARP entries, e.g., are presented).
1432 */
1433 info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr;
1434 m = rtsock_msg_mbuf(cmd, &info);
1435 if (m == NULL)
1436 return;
1437 ifmam = mtod(m, struct ifma_msghdr *);
1438 KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n",
1439 __func__));
1440 ifmam->ifmam_index = ifp->if_index;
1441 ifmam->ifmam_addrs = info.rti_addrs;
1442 rt_dispatch(m, ifma->ifma_addr ? ifma->ifma_addr->sa_family : AF_UNSPEC);
1443 }
1444
1445 static struct mbuf *
1446 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1447 struct rt_addrinfo *info)
1448 {
1449 struct if_announcemsghdr *ifan;
1450 struct mbuf *m;
1451
1452 if (V_route_cb.any_count == 0)
1453 return NULL;
1454 bzero((caddr_t)info, sizeof(*info));
1455 m = rtsock_msg_mbuf(type, info);
1456 if (m != NULL) {
1457 ifan = mtod(m, struct if_announcemsghdr *);
1458 ifan->ifan_index = ifp->if_index;
1459 strlcpy(ifan->ifan_name, ifp->if_xname,
1460 sizeof(ifan->ifan_name));
1461 ifan->ifan_what = what;
1462 }
1463 return m;
1464 }
1465
1466 /*
1467 * This is called to generate routing socket messages indicating
1468 * IEEE80211 wireless events.
1469 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1470 */
1471 void
1472 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
1473 {
1474 struct mbuf *m;
1475 struct rt_addrinfo info;
1476
1477 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1478 if (m != NULL) {
1479 /*
1480 * Append the ieee80211 data. Try to stick it in the
1481 * mbuf containing the ifannounce msg; otherwise allocate
1482 * a new mbuf and append.
1483 *
1484 * NB: we assume m is a single mbuf.
1485 */
1486 if (data_len > M_TRAILINGSPACE(m)) {
1487 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
1488 if (n == NULL) {
1489 m_freem(m);
1490 return;
1491 }
1492 bcopy(data, mtod(n, void *), data_len);
1493 n->m_len = data_len;
1494 m->m_next = n;
1495 } else if (data_len > 0) {
1496 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
1497 m->m_len += data_len;
1498 }
1499 if (m->m_flags & M_PKTHDR)
1500 m->m_pkthdr.len += data_len;
1501 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
1502 rt_dispatch(m, AF_UNSPEC);
1503 }
1504 }
1505
1506 /*
1507 * This is called to generate routing socket messages indicating
1508 * network interface arrival and departure.
1509 */
1510 void
1511 rt_ifannouncemsg(struct ifnet *ifp, int what)
1512 {
1513 struct mbuf *m;
1514 struct rt_addrinfo info;
1515
1516 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
1517 if (m != NULL)
1518 rt_dispatch(m, AF_UNSPEC);
1519 }
1520
1521 static void
1522 rt_dispatch(struct mbuf *m, sa_family_t saf)
1523 {
1524 struct m_tag *tag;
1525
1526 /*
1527 * Preserve the family from the sockaddr, if any, in an m_tag for
1528 * use when injecting the mbuf into the routing socket buffer from
1529 * the netisr.
1530 */
1531 if (saf != AF_UNSPEC) {
1532 tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short),
1533 M_NOWAIT);
1534 if (tag == NULL) {
1535 m_freem(m);
1536 return;
1537 }
1538 *(unsigned short *)(tag + 1) = saf;
1539 m_tag_prepend(m, tag);
1540 }
1541 #ifdef VIMAGE
1542 if (V_loif)
1543 m->m_pkthdr.rcvif = V_loif;
1544 else {
1545 m_freem(m);
1546 return;
1547 }
1548 #endif
1549 netisr_queue(NETISR_ROUTE, m); /* mbuf is free'd on failure. */
1550 }
1551
1552 /*
1553 * This is used in dumping the kernel table via sysctl().
1554 */
1555 static int
1556 sysctl_dumpentry(struct radix_node *rn, void *vw)
1557 {
1558 struct walkarg *w = vw;
1559 struct rtentry *rt = (struct rtentry *)rn;
1560 int error = 0, size;
1561 struct rt_addrinfo info;
1562 struct sockaddr_storage ss;
1563
1564 IFNET_RLOCK_NOSLEEP_ASSERT();
1565
1566 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1567 return 0;
1568 if ((rt->rt_flags & RTF_HOST) == 0
1569 ? jailed_without_vnet(w->w_req->td->td_ucred)
1570 : prison_if(w->w_req->td->td_ucred, rt_key(rt)) != 0)
1571 return (0);
1572 bzero((caddr_t)&info, sizeof(info));
1573 info.rti_info[RTAX_DST] = rt_key(rt);
1574 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1575 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(rt_key(rt),
1576 rt_mask(rt), &ss);
1577 info.rti_info[RTAX_GENMASK] = 0;
1578 if (rt->rt_ifp && !(rt->rt_ifp->if_flags & IFF_DYING)) {
1579 info.rti_info[RTAX_IFP] = rt->rt_ifp->if_addr->ifa_addr;
1580 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
1581 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1582 info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr;
1583 }
1584 if ((error = rtsock_msg_buffer(RTM_GET, &info, w, &size)) != 0)
1585 return (error);
1586 if (w->w_req && w->w_tmem) {
1587 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
1588
1589 bzero(&rtm->rtm_index,
1590 sizeof(*rtm) - offsetof(struct rt_msghdr, rtm_index));
1591 if (rt->rt_flags & RTF_GWFLAG_COMPAT)
1592 rtm->rtm_flags = RTF_GATEWAY |
1593 (rt->rt_flags & ~RTF_GWFLAG_COMPAT);
1594 else
1595 rtm->rtm_flags = rt->rt_flags;
1596 rt_getmetrics(rt, &rtm->rtm_rmx);
1597 rtm->rtm_index = rt->rt_ifp->if_index;
1598 rtm->rtm_addrs = info.rti_addrs;
1599 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
1600 return (error);
1601 }
1602 return (error);
1603 }
1604
1605 static int
1606 sysctl_iflist_ifml(struct ifnet *ifp, const struct if_data *src_ifd,
1607 struct rt_addrinfo *info, struct walkarg *w, int len)
1608 {
1609 struct if_msghdrl *ifm;
1610 struct if_data *ifd;
1611
1612 ifm = (struct if_msghdrl *)w->w_tmem;
1613
1614 #ifdef COMPAT_FREEBSD32
1615 if (w->w_req->flags & SCTL_MASK32) {
1616 struct if_msghdrl32 *ifm32;
1617
1618 ifm32 = (struct if_msghdrl32 *)ifm;
1619 ifm32->ifm_addrs = info->rti_addrs;
1620 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1621 ifm32->ifm_index = ifp->if_index;
1622 ifm32->_ifm_spare1 = 0;
1623 ifm32->ifm_len = sizeof(*ifm32);
1624 ifm32->ifm_data_off = offsetof(struct if_msghdrl32, ifm_data);
1625 ifm32->_ifm_spare2 = 0;
1626 ifd = &ifm32->ifm_data;
1627 } else
1628 #endif
1629 {
1630 ifm->ifm_addrs = info->rti_addrs;
1631 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1632 ifm->ifm_index = ifp->if_index;
1633 ifm->_ifm_spare1 = 0;
1634 ifm->ifm_len = sizeof(*ifm);
1635 ifm->ifm_data_off = offsetof(struct if_msghdrl, ifm_data);
1636 ifm->_ifm_spare2 = 0;
1637 ifd = &ifm->ifm_data;
1638 }
1639
1640 memcpy(ifd, src_ifd, sizeof(*ifd));
1641
1642 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1643 }
1644
1645 static int
1646 sysctl_iflist_ifm(struct ifnet *ifp, const struct if_data *src_ifd,
1647 struct rt_addrinfo *info, struct walkarg *w, int len)
1648 {
1649 struct if_msghdr *ifm;
1650 struct if_data *ifd;
1651
1652 ifm = (struct if_msghdr *)w->w_tmem;
1653
1654 #ifdef COMPAT_FREEBSD32
1655 if (w->w_req->flags & SCTL_MASK32) {
1656 struct if_msghdr32 *ifm32;
1657
1658 ifm32 = (struct if_msghdr32 *)ifm;
1659 ifm32->ifm_addrs = info->rti_addrs;
1660 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1661 ifm32->ifm_index = ifp->if_index;
1662 ifm32->_ifm_spare1 = 0;
1663 ifd = &ifm32->ifm_data;
1664 } else
1665 #endif
1666 {
1667 ifm->ifm_addrs = info->rti_addrs;
1668 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1669 ifm->ifm_index = ifp->if_index;
1670 ifm->_ifm_spare1 = 0;
1671 ifd = &ifm->ifm_data;
1672 }
1673
1674 memcpy(ifd, src_ifd, sizeof(*ifd));
1675
1676 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1677 }
1678
1679 static int
1680 sysctl_iflist_ifaml(struct ifaddr *ifa, struct rt_addrinfo *info,
1681 struct walkarg *w, int len)
1682 {
1683 struct ifa_msghdrl *ifam;
1684 struct if_data *ifd;
1685
1686 ifam = (struct ifa_msghdrl *)w->w_tmem;
1687
1688 #ifdef COMPAT_FREEBSD32
1689 if (w->w_req->flags & SCTL_MASK32) {
1690 struct ifa_msghdrl32 *ifam32;
1691
1692 ifam32 = (struct ifa_msghdrl32 *)ifam;
1693 ifam32->ifam_addrs = info->rti_addrs;
1694 ifam32->ifam_flags = ifa->ifa_flags;
1695 ifam32->ifam_index = ifa->ifa_ifp->if_index;
1696 ifam32->_ifam_spare1 = 0;
1697 ifam32->ifam_len = sizeof(*ifam32);
1698 ifam32->ifam_data_off =
1699 offsetof(struct ifa_msghdrl32, ifam_data);
1700 ifam32->ifam_metric = ifa->ifa_ifp->if_metric;
1701 ifd = &ifam32->ifam_data;
1702 } else
1703 #endif
1704 {
1705 ifam->ifam_addrs = info->rti_addrs;
1706 ifam->ifam_flags = ifa->ifa_flags;
1707 ifam->ifam_index = ifa->ifa_ifp->if_index;
1708 ifam->_ifam_spare1 = 0;
1709 ifam->ifam_len = sizeof(*ifam);
1710 ifam->ifam_data_off = offsetof(struct ifa_msghdrl, ifam_data);
1711 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1712 ifd = &ifam->ifam_data;
1713 }
1714
1715 bzero(ifd, sizeof(*ifd));
1716 ifd->ifi_datalen = sizeof(struct if_data);
1717 ifd->ifi_ipackets = counter_u64_fetch(ifa->ifa_ipackets);
1718 ifd->ifi_opackets = counter_u64_fetch(ifa->ifa_opackets);
1719 ifd->ifi_ibytes = counter_u64_fetch(ifa->ifa_ibytes);
1720 ifd->ifi_obytes = counter_u64_fetch(ifa->ifa_obytes);
1721
1722 /* Fixup if_data carp(4) vhid. */
1723 if (carp_get_vhid_p != NULL)
1724 ifd->ifi_vhid = (*carp_get_vhid_p)(ifa);
1725
1726 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1727 }
1728
1729 static int
1730 sysctl_iflist_ifam(struct ifaddr *ifa, struct rt_addrinfo *info,
1731 struct walkarg *w, int len)
1732 {
1733 struct ifa_msghdr *ifam;
1734
1735 ifam = (struct ifa_msghdr *)w->w_tmem;
1736 ifam->ifam_addrs = info->rti_addrs;
1737 ifam->ifam_flags = ifa->ifa_flags;
1738 ifam->ifam_index = ifa->ifa_ifp->if_index;
1739 ifam->_ifam_spare1 = 0;
1740 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1741
1742 return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1743 }
1744
1745 static int
1746 sysctl_iflist(int af, struct walkarg *w)
1747 {
1748 struct ifnet *ifp;
1749 struct ifaddr *ifa;
1750 struct if_data ifd;
1751 struct rt_addrinfo info;
1752 int len, error = 0;
1753 struct sockaddr_storage ss;
1754 struct epoch_tracker et;
1755
1756 bzero((caddr_t)&info, sizeof(info));
1757 bzero(&ifd, sizeof(ifd));
1758 NET_EPOCH_ENTER_ET(et);
1759 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1760 if (w->w_arg && w->w_arg != ifp->if_index)
1761 continue;
1762 if_data_copy(ifp, &ifd);
1763 ifa = ifp->if_addr;
1764 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
1765 error = rtsock_msg_buffer(RTM_IFINFO, &info, w, &len);
1766 if (error != 0)
1767 goto done;
1768 info.rti_info[RTAX_IFP] = NULL;
1769 if (w->w_req && w->w_tmem) {
1770 if (w->w_op == NET_RT_IFLISTL)
1771 error = sysctl_iflist_ifml(ifp, &ifd, &info, w,
1772 len);
1773 else
1774 error = sysctl_iflist_ifm(ifp, &ifd, &info, w,
1775 len);
1776 if (error)
1777 goto done;
1778 }
1779 while ((ifa = CK_STAILQ_NEXT(ifa, ifa_link)) != NULL) {
1780 if (af && af != ifa->ifa_addr->sa_family)
1781 continue;
1782 if (prison_if(w->w_req->td->td_ucred,
1783 ifa->ifa_addr) != 0)
1784 continue;
1785 info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1786 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(
1787 ifa->ifa_addr, ifa->ifa_netmask, &ss);
1788 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1789 error = rtsock_msg_buffer(RTM_NEWADDR, &info, w, &len);
1790 if (error != 0)
1791 goto done;
1792 if (w->w_req && w->w_tmem) {
1793 if (w->w_op == NET_RT_IFLISTL)
1794 error = sysctl_iflist_ifaml(ifa, &info,
1795 w, len);
1796 else
1797 error = sysctl_iflist_ifam(ifa, &info,
1798 w, len);
1799 if (error)
1800 goto done;
1801 }
1802 }
1803 info.rti_info[RTAX_IFA] = NULL;
1804 info.rti_info[RTAX_NETMASK] = NULL;
1805 info.rti_info[RTAX_BRD] = NULL;
1806 }
1807 done:
1808 NET_EPOCH_EXIT_ET(et);
1809 return (error);
1810 }
1811
1812 static int
1813 sysctl_ifmalist(int af, struct walkarg *w)
1814 {
1815 struct rt_addrinfo info;
1816 struct ifaddr *ifa;
1817 struct ifmultiaddr *ifma;
1818 struct ifnet *ifp;
1819 int error, len;
1820
1821 error = 0;
1822 bzero((caddr_t)&info, sizeof(info));
1823
1824 IFNET_RLOCK_NOSLEEP();
1825 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1826 if (w->w_arg && w->w_arg != ifp->if_index)
1827 continue;
1828 ifa = ifp->if_addr;
1829 info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL;
1830 IF_ADDR_RLOCK(ifp);
1831 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1832 if (af && af != ifma->ifma_addr->sa_family)
1833 continue;
1834 if (prison_if(w->w_req->td->td_ucred,
1835 ifma->ifma_addr) != 0)
1836 continue;
1837 info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1838 info.rti_info[RTAX_GATEWAY] =
1839 (ifma->ifma_addr->sa_family != AF_LINK) ?
1840 ifma->ifma_lladdr : NULL;
1841 error = rtsock_msg_buffer(RTM_NEWMADDR, &info, w, &len);
1842 if (error != 0)
1843 break;
1844 if (w->w_req && w->w_tmem) {
1845 struct ifma_msghdr *ifmam;
1846
1847 ifmam = (struct ifma_msghdr *)w->w_tmem;
1848 ifmam->ifmam_index = ifma->ifma_ifp->if_index;
1849 ifmam->ifmam_flags = 0;
1850 ifmam->ifmam_addrs = info.rti_addrs;
1851 ifmam->_ifmam_spare1 = 0;
1852 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
1853 if (error != 0)
1854 break;
1855 }
1856 }
1857 IF_ADDR_RUNLOCK(ifp);
1858 if (error != 0)
1859 break;
1860 }
1861 IFNET_RUNLOCK_NOSLEEP();
1862 return (error);
1863 }
1864
1865 static int
1866 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
1867 {
1868 RIB_RLOCK_TRACKER;
1869 int *name = (int *)arg1;
1870 u_int namelen = arg2;
1871 struct rib_head *rnh = NULL; /* silence compiler. */
1872 int i, lim, error = EINVAL;
1873 int fib = 0;
1874 u_char af;
1875 struct walkarg w;
1876
1877 name ++;
1878 namelen--;
1879 if (req->newptr)
1880 return (EPERM);
1881 if (name[1] == NET_RT_DUMP) {
1882 if (namelen == 3)
1883 fib = req->td->td_proc->p_fibnum;
1884 else if (namelen == 4)
1885 fib = (name[3] == RT_ALL_FIBS) ?
1886 req->td->td_proc->p_fibnum : name[3];
1887 else
1888 return ((namelen < 3) ? EISDIR : ENOTDIR);
1889 if (fib < 0 || fib >= rt_numfibs)
1890 return (EINVAL);
1891 } else if (namelen != 3)
1892 return ((namelen < 3) ? EISDIR : ENOTDIR);
1893 af = name[0];
1894 if (af > AF_MAX)
1895 return (EINVAL);
1896 bzero(&w, sizeof(w));
1897 w.w_op = name[1];
1898 w.w_arg = name[2];
1899 w.w_req = req;
1900
1901 error = sysctl_wire_old_buffer(req, 0);
1902 if (error)
1903 return (error);
1904
1905 /*
1906 * Allocate reply buffer in advance.
1907 * All rtsock messages has maximum length of u_short.
1908 */
1909 w.w_tmemsize = 65536;
1910 w.w_tmem = malloc(w.w_tmemsize, M_TEMP, M_WAITOK);
1911
1912 switch (w.w_op) {
1913
1914 case NET_RT_DUMP:
1915 case NET_RT_FLAGS:
1916 if (af == 0) { /* dump all tables */
1917 i = 1;
1918 lim = AF_MAX;
1919 } else /* dump only one table */
1920 i = lim = af;
1921
1922 /*
1923 * take care of llinfo entries, the caller must
1924 * specify an AF
1925 */
1926 if (w.w_op == NET_RT_FLAGS &&
1927 (w.w_arg == 0 || w.w_arg & RTF_LLINFO)) {
1928 if (af != 0)
1929 error = lltable_sysctl_dumparp(af, w.w_req);
1930 else
1931 error = EINVAL;
1932 break;
1933 }
1934 /*
1935 * take care of routing entries
1936 */
1937 for (error = 0; error == 0 && i <= lim; i++) {
1938 rnh = rt_tables_get_rnh(fib, i);
1939 if (rnh != NULL) {
1940 RIB_RLOCK(rnh);
1941 IFNET_RLOCK_NOSLEEP();
1942 error = rnh->rnh_walktree(&rnh->head,
1943 sysctl_dumpentry, &w);
1944 IFNET_RUNLOCK_NOSLEEP();
1945 RIB_RUNLOCK(rnh);
1946 } else if (af != 0)
1947 error = EAFNOSUPPORT;
1948 }
1949 break;
1950
1951 case NET_RT_IFLIST:
1952 case NET_RT_IFLISTL:
1953 error = sysctl_iflist(af, &w);
1954 break;
1955
1956 case NET_RT_IFMALIST:
1957 error = sysctl_ifmalist(af, &w);
1958 break;
1959 }
1960
1961 free(w.w_tmem, M_TEMP);
1962 return (error);
1963 }
1964
1965 static SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
1966
1967 /*
1968 * Definitions of protocols supported in the ROUTE domain.
1969 */
1970
1971 static struct domain routedomain; /* or at least forward */
1972
1973 static struct protosw routesw[] = {
1974 {
1975 .pr_type = SOCK_RAW,
1976 .pr_domain = &routedomain,
1977 .pr_flags = PR_ATOMIC|PR_ADDR,
1978 .pr_output = route_output,
1979 .pr_ctlinput = raw_ctlinput,
1980 .pr_init = raw_init,
1981 .pr_usrreqs = &route_usrreqs
1982 }
1983 };
1984
1985 static struct domain routedomain = {
1986 .dom_family = PF_ROUTE,
1987 .dom_name = "route",
1988 .dom_protosw = routesw,
1989 .dom_protoswNPROTOSW = &routesw[nitems(routesw)]
1990 };
1991
1992 VNET_DOMAIN_SET(route);
Cache object: a679de13641c6e39bbf175f5b757451d
|