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