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