1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1980, 1986, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * @(#)route.c 8.3.1.1 (Berkeley) 2/23/95
32 */
33 /************************************************************************
34 * Note: In this file a 'fib' is a "forwarding information base" *
35 * Which is the new name for an in kernel routing (next hop) table. *
36 ***********************************************************************/
37
38 #include "opt_inet.h"
39 #include "opt_inet6.h"
40 #include "opt_mrouting.h"
41 #include "opt_route.h"
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/malloc.h>
46 #include <sys/mbuf.h>
47 #include <sys/socket.h>
48 #include <sys/sysctl.h>
49 #include <sys/syslog.h>
50 #include <sys/sysproto.h>
51 #include <sys/proc.h>
52 #include <sys/domain.h>
53 #include <sys/eventhandler.h>
54 #include <sys/kernel.h>
55 #include <sys/lock.h>
56 #include <sys/rmlock.h>
57
58 #include <net/if.h>
59 #include <net/if_var.h>
60 #include <net/if_dl.h>
61 #include <net/route.h>
62 #include <net/route/route_ctl.h>
63 #include <net/route/route_var.h>
64 #include <net/route/nhop.h>
65 #include <net/vnet.h>
66
67 #include <netinet/in.h>
68 #include <netinet/ip_mroute.h>
69
70 VNET_PCPUSTAT_DEFINE(struct rtstat, rtstat);
71
72 VNET_PCPUSTAT_SYSINIT(rtstat);
73 #ifdef VIMAGE
74 VNET_PCPUSTAT_SYSUNINIT(rtstat);
75 #endif
76
77 EVENTHANDLER_LIST_DEFINE(rt_addrmsg);
78
79 static int rt_ifdelroute(const struct rtentry *rt, const struct nhop_object *,
80 void *arg);
81 static int rt_exportinfo(struct rtentry *rt, struct nhop_object *nh,
82 struct rt_addrinfo *info, int flags);
83
84 /*
85 * route initialization must occur before ip6_init2(), which happenas at
86 * SI_ORDER_MIDDLE.
87 */
88 static void
route_init(void)89 route_init(void)
90 {
91
92 nhops_init();
93 }
94 SYSINIT(route_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, route_init, NULL);
95
96 struct rib_head *
rt_table_init(int offset,int family,u_int fibnum)97 rt_table_init(int offset, int family, u_int fibnum)
98 {
99 struct rib_head *rh;
100
101 rh = malloc(sizeof(struct rib_head), M_RTABLE, M_WAITOK | M_ZERO);
102
103 /* TODO: These details should be hidded inside radix.c */
104 /* Init masks tree */
105 rn_inithead_internal(&rh->head, rh->rnh_nodes, offset);
106 rn_inithead_internal(&rh->rmhead.head, rh->rmhead.mask_nodes, 0);
107 rh->head.rnh_masks = &rh->rmhead;
108
109 /* Save metadata associated with this routing table. */
110 rh->rib_family = family;
111 rh->rib_fibnum = fibnum;
112 #ifdef VIMAGE
113 rh->rib_vnet = curvnet;
114 #endif
115
116 tmproutes_init(rh);
117
118 /* Init locks */
119 RIB_LOCK_INIT(rh);
120
121 nhops_init_rib(rh);
122
123 /* Init subscription system */
124 rib_init_subscriptions(rh);
125
126 /* Finally, set base callbacks */
127 rh->rnh_addaddr = rn_addroute;
128 rh->rnh_deladdr = rn_delete;
129 rh->rnh_matchaddr = rn_match;
130 rh->rnh_lookup = rn_lookup;
131 rh->rnh_walktree = rn_walktree;
132 rh->rnh_walktree_from = rn_walktree_from;
133
134 return (rh);
135 }
136
137 static int
rt_freeentry(struct radix_node * rn,void * arg)138 rt_freeentry(struct radix_node *rn, void *arg)
139 {
140 struct radix_head * const rnh = arg;
141 struct radix_node *x;
142
143 x = (struct radix_node *)rn_delete(rn + 2, NULL, rnh);
144 if (x != NULL)
145 R_Free(x);
146 return (0);
147 }
148
149 void
rt_table_destroy(struct rib_head * rh)150 rt_table_destroy(struct rib_head *rh)
151 {
152
153 RIB_WLOCK(rh);
154 rh->rib_dying = true;
155 RIB_WUNLOCK(rh);
156
157 #ifdef FIB_ALGO
158 fib_destroy_rib(rh);
159 #endif
160
161 tmproutes_destroy(rh);
162
163 rn_walktree(&rh->rmhead.head, rt_freeentry, &rh->rmhead.head);
164
165 nhops_destroy_rib(rh);
166
167 rib_destroy_subscriptions(rh);
168
169 /* Assume table is already empty */
170 RIB_LOCK_DESTROY(rh);
171 free(rh, M_RTABLE);
172 }
173
174 /*
175 * Adds a temporal redirect entry to the routing table.
176 * @fibnum: fib number
177 * @dst: destination to install redirect to
178 * @gateway: gateway to go via
179 * @author: sockaddr of originating router, can be NULL
180 * @ifp: interface to use for the redirected route
181 * @flags: set of flags to add. Allowed: RTF_GATEWAY
182 * @lifetime_sec: time in seconds to expire this redirect.
183 *
184 * Retuns 0 on success, errno otherwise.
185 */
186 int
rib_add_redirect(u_int fibnum,struct sockaddr * dst,struct sockaddr * gateway,struct sockaddr * author,struct ifnet * ifp,int flags,int lifetime_sec)187 rib_add_redirect(u_int fibnum, struct sockaddr *dst, struct sockaddr *gateway,
188 struct sockaddr *author, struct ifnet *ifp, int flags, int lifetime_sec)
189 {
190 struct route_nhop_data rnd = { .rnd_weight = RT_DEFAULT_WEIGHT };
191 struct rib_cmd_info rc;
192 struct ifaddr *ifa;
193 int error;
194
195 NET_EPOCH_ASSERT();
196
197 if (rt_tables_get_rnh(fibnum, dst->sa_family) == NULL)
198 return (EAFNOSUPPORT);
199
200 /* Verify the allowed flag mask. */
201 KASSERT(((flags & ~(RTF_GATEWAY)) == 0),
202 ("invalid redirect flags: %x", flags));
203 flags |= RTF_HOST | RTF_DYNAMIC;
204
205 /* Get the best ifa for the given interface and gateway. */
206 if ((ifa = ifaof_ifpforaddr(gateway, ifp)) == NULL)
207 return (ENETUNREACH);
208
209 struct nhop_object *nh = nhop_alloc(fibnum, dst->sa_family);
210 if (nh == NULL)
211 return (ENOMEM);
212
213 nhop_set_gw(nh, gateway, flags & RTF_GATEWAY);
214 nhop_set_transmit_ifp(nh, ifp);
215 nhop_set_src(nh, ifa);
216 nhop_set_pxtype_flag(nh, NHF_HOST);
217 nhop_set_expire(nh, lifetime_sec + time_uptime);
218 nhop_set_redirect(nh, true);
219 nhop_set_origin(nh, NH_ORIGIN_REDIRECT);
220 rnd.rnd_nhop = nhop_get_nhop(nh, &error);
221 if (error == 0) {
222 error = rib_add_route_px(fibnum, dst, -1,
223 &rnd, RTM_F_CREATE, &rc);
224 }
225
226 if (error != 0) {
227 /* TODO: add per-fib redirect stats. */
228 return (error);
229 }
230
231 RTSTAT_INC(rts_dynamic);
232
233 /* Send notification of a route addition to userland. */
234 struct rt_addrinfo info = {
235 .rti_info[RTAX_DST] = dst,
236 .rti_info[RTAX_GATEWAY] = gateway,
237 .rti_info[RTAX_AUTHOR] = author,
238 };
239 rt_missmsg_fib(RTM_REDIRECT, &info, flags | RTF_UP, error, fibnum);
240
241 return (0);
242 }
243
244 /*
245 * Routing table ioctl interface.
246 */
247 int
rtioctl_fib(u_long req,caddr_t data,u_int fibnum)248 rtioctl_fib(u_long req, caddr_t data, u_int fibnum)
249 {
250
251 /*
252 * If more ioctl commands are added here, make sure the proper
253 * super-user checks are being performed because it is possible for
254 * prison-root to make it this far if raw sockets have been enabled
255 * in jails.
256 */
257 #ifdef INET
258 /* Multicast goop, grrr... */
259 return mrt_ioctl ? mrt_ioctl(req, data, fibnum) : EOPNOTSUPP;
260 #else /* INET */
261 return ENXIO;
262 #endif /* INET */
263 }
264
265 struct ifaddr *
ifa_ifwithroute(int flags,const struct sockaddr * dst,const struct sockaddr * gateway,u_int fibnum)266 ifa_ifwithroute(int flags, const struct sockaddr *dst,
267 const struct sockaddr *gateway, u_int fibnum)
268 {
269 struct ifaddr *ifa;
270
271 NET_EPOCH_ASSERT();
272 if ((flags & RTF_GATEWAY) == 0) {
273 /*
274 * If we are adding a route to an interface,
275 * and the interface is a pt to pt link
276 * we should search for the destination
277 * as our clue to the interface. Otherwise
278 * we can use the local address.
279 */
280 ifa = NULL;
281 if (flags & RTF_HOST)
282 ifa = ifa_ifwithdstaddr(dst, fibnum);
283 if (ifa == NULL)
284 ifa = ifa_ifwithaddr(gateway);
285 } else {
286 /*
287 * If we are adding a route to a remote net
288 * or host, the gateway may still be on the
289 * other end of a pt to pt link.
290 */
291 ifa = ifa_ifwithdstaddr(gateway, fibnum);
292 }
293 if (ifa == NULL)
294 ifa = ifa_ifwithnet(gateway, 0, fibnum);
295 if (ifa == NULL) {
296 struct nhop_object *nh;
297
298 nh = rib_lookup(fibnum, gateway, NHR_NONE, 0);
299
300 /*
301 * dismiss a gateway that is reachable only
302 * through the default router
303 */
304 if ((nh == NULL) || (nh->nh_flags & NHF_DEFAULT))
305 return (NULL);
306 ifa = nh->nh_ifa;
307 }
308 if (ifa->ifa_addr->sa_family != dst->sa_family) {
309 struct ifaddr *oifa = ifa;
310 ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp);
311 if (ifa == NULL)
312 ifa = oifa;
313 }
314
315 return (ifa);
316 }
317
318 /*
319 * Copy most of @rt data into @info.
320 *
321 * If @flags contains NHR_COPY, copies dst,netmask and gw to the
322 * pointers specified by @info structure. Assume such pointers
323 * are zeroed sockaddr-like structures with sa_len field initialized
324 * to reflect size of the provided buffer. if no NHR_COPY is specified,
325 * point dst,netmask and gw @info fields to appropriate @rt values.
326 *
327 * if @flags contains NHR_REF, do refcouting on rt_ifp and rt_ifa.
328 *
329 * Returns 0 on success.
330 */
331 static int
rt_exportinfo(struct rtentry * rt,struct nhop_object * nh,struct rt_addrinfo * info,int flags)332 rt_exportinfo(struct rtentry *rt, struct nhop_object *nh,
333 struct rt_addrinfo *info, int flags)
334 {
335 struct rt_metrics *rmx;
336 struct sockaddr *src, *dst;
337 int sa_len;
338
339 if (flags & NHR_COPY) {
340 /* Copy destination if dst is non-zero */
341 src = rt_key(rt);
342 dst = info->rti_info[RTAX_DST];
343 sa_len = src->sa_len;
344 if (dst != NULL) {
345 if (src->sa_len > dst->sa_len)
346 return (ENOMEM);
347 memcpy(dst, src, src->sa_len);
348 info->rti_addrs |= RTA_DST;
349 }
350
351 /* Copy mask if set && dst is non-zero */
352 src = rt_mask(rt);
353 dst = info->rti_info[RTAX_NETMASK];
354 if (src != NULL && dst != NULL) {
355 /*
356 * Radix stores different value in sa_len,
357 * assume rt_mask() to have the same length
358 * as rt_key()
359 */
360 if (sa_len > dst->sa_len)
361 return (ENOMEM);
362 memcpy(dst, src, src->sa_len);
363 info->rti_addrs |= RTA_NETMASK;
364 }
365
366 /* Copy gateway is set && dst is non-zero */
367 src = &nh->gw_sa;
368 dst = info->rti_info[RTAX_GATEWAY];
369 if ((nhop_get_rtflags(nh) & RTF_GATEWAY) &&
370 src != NULL && dst != NULL) {
371 if (src->sa_len > dst->sa_len)
372 return (ENOMEM);
373 memcpy(dst, src, src->sa_len);
374 info->rti_addrs |= RTA_GATEWAY;
375 }
376 } else {
377 info->rti_info[RTAX_DST] = rt_key(rt);
378 info->rti_addrs |= RTA_DST;
379 if (rt_mask(rt) != NULL) {
380 info->rti_info[RTAX_NETMASK] = rt_mask(rt);
381 info->rti_addrs |= RTA_NETMASK;
382 }
383 if (nhop_get_rtflags(nh) & RTF_GATEWAY) {
384 info->rti_info[RTAX_GATEWAY] = &nh->gw_sa;
385 info->rti_addrs |= RTA_GATEWAY;
386 }
387 }
388
389 rmx = info->rti_rmx;
390 if (rmx != NULL) {
391 info->rti_mflags |= RTV_MTU;
392 rmx->rmx_mtu = nh->nh_mtu;
393 }
394
395 info->rti_flags = rt->rte_flags | nhop_get_rtflags(nh);
396 info->rti_ifp = nh->nh_ifp;
397 info->rti_ifa = nh->nh_ifa;
398 if (flags & NHR_REF) {
399 if_ref(info->rti_ifp);
400 ifa_ref(info->rti_ifa);
401 }
402
403 return (0);
404 }
405
406 /*
407 * Lookups up route entry for @dst in RIB database for fib @fibnum.
408 * Exports entry data to @info using rt_exportinfo().
409 *
410 * If @flags contains NHR_REF, refcouting is performed on rt_ifp and rt_ifa.
411 * All references can be released later by calling rib_free_info().
412 *
413 * Returns 0 on success.
414 * Returns ENOENT for lookup failure, ENOMEM for export failure.
415 */
416 int
rib_lookup_info(uint32_t fibnum,const struct sockaddr * dst,uint32_t flags,uint32_t flowid,struct rt_addrinfo * info)417 rib_lookup_info(uint32_t fibnum, const struct sockaddr *dst, uint32_t flags,
418 uint32_t flowid, struct rt_addrinfo *info)
419 {
420 RIB_RLOCK_TRACKER;
421 struct rib_head *rh;
422 struct radix_node *rn;
423 struct rtentry *rt;
424 struct nhop_object *nh;
425 int error;
426
427 KASSERT((fibnum < rt_numfibs), ("rib_lookup_rte: bad fibnum"));
428 rh = rt_tables_get_rnh(fibnum, dst->sa_family);
429 if (rh == NULL)
430 return (ENOENT);
431
432 RIB_RLOCK(rh);
433 rn = rh->rnh_matchaddr(__DECONST(void *, dst), &rh->head);
434 if (rn != NULL && ((rn->rn_flags & RNF_ROOT) == 0)) {
435 rt = RNTORT(rn);
436 nh = nhop_select(rt->rt_nhop, flowid);
437 /* Ensure route & ifp is UP */
438 if (RT_LINK_IS_UP(nh->nh_ifp)) {
439 flags = (flags & NHR_REF) | NHR_COPY;
440 error = rt_exportinfo(rt, nh, info, flags);
441 RIB_RUNLOCK(rh);
442
443 return (error);
444 }
445 }
446 RIB_RUNLOCK(rh);
447
448 return (ENOENT);
449 }
450
451 /*
452 * Releases all references acquired by rib_lookup_info() when
453 * called with NHR_REF flags.
454 */
455 void
rib_free_info(struct rt_addrinfo * info)456 rib_free_info(struct rt_addrinfo *info)
457 {
458
459 ifa_free(info->rti_ifa);
460 if_rele(info->rti_ifp);
461 }
462
463 /*
464 * Delete Routes for a Network Interface
465 *
466 * Called for each routing entry via the rnh->rnh_walktree() call above
467 * to delete all route entries referencing a detaching network interface.
468 *
469 * Arguments:
470 * rt pointer to rtentry
471 * nh pointer to nhop
472 * arg argument passed to rnh->rnh_walktree() - detaching interface
473 *
474 * Returns:
475 * 0 successful
476 * errno failed - reason indicated
477 */
478 static int
rt_ifdelroute(const struct rtentry * rt,const struct nhop_object * nh,void * arg)479 rt_ifdelroute(const struct rtentry *rt, const struct nhop_object *nh, void *arg)
480 {
481 struct ifnet *ifp = arg;
482
483 if (nh->nh_ifp != ifp)
484 return (0);
485
486 /*
487 * Protect (sorta) against walktree recursion problems
488 * with cloned routes
489 */
490 if ((rt->rte_flags & RTF_UP) == 0)
491 return (0);
492
493 return (1);
494 }
495
496 void
rt_flushifroutes(struct ifnet * ifp)497 rt_flushifroutes(struct ifnet *ifp)
498 {
499
500 rib_foreach_table_walk_del(AF_UNSPEC, rt_ifdelroute, ifp);
501 }
502
503 /*
504 * Tries to extract interface from RTAX_IFP passed in rt_addrinfo.
505 * Interface can be specified ether as interface index (sdl_index) or
506 * the interface name (sdl_data).
507 *
508 * Returns found ifp or NULL
509 */
510 static struct ifnet *
info_get_ifp(struct rt_addrinfo * info)511 info_get_ifp(struct rt_addrinfo *info)
512 {
513 const struct sockaddr_dl *sdl;
514
515 sdl = (const struct sockaddr_dl *)info->rti_info[RTAX_IFP];
516 if (sdl->sdl_family != AF_LINK)
517 return (NULL);
518
519 if (sdl->sdl_index != 0)
520 return (ifnet_byindex(sdl->sdl_index));
521 if (sdl->sdl_nlen > 0) {
522 char if_name[IF_NAMESIZE];
523 if (sdl->sdl_nlen + offsetof(struct sockaddr_dl, sdl_data) > sdl->sdl_len)
524 return (NULL);
525 if (sdl->sdl_nlen >= IF_NAMESIZE)
526 return (NULL);
527 bzero(if_name, sizeof(if_name));
528 memcpy(if_name, sdl->sdl_data, sdl->sdl_nlen);
529 return (ifunit(if_name));
530 }
531
532 return (NULL);
533 }
534
535 /*
536 * Calculates proper ifa/ifp for the cases when gateway AF is different
537 * from dst AF.
538 *
539 * Returns 0 on success.
540 */
541 __noinline static int
rt_getifa_family(struct rt_addrinfo * info,uint32_t fibnum)542 rt_getifa_family(struct rt_addrinfo *info, uint32_t fibnum)
543 {
544 if (info->rti_ifp == NULL) {
545 struct ifaddr *ifa = NULL;
546 /*
547 * No transmit interface specified. Guess it by checking gw sa.
548 */
549 const struct sockaddr *gw = info->rti_info[RTAX_GATEWAY];
550 ifa = ifa_ifwithroute(RTF_GATEWAY, gw, gw, fibnum);
551 if (ifa == NULL)
552 return (ENETUNREACH);
553 info->rti_ifp = ifa->ifa_ifp;
554 }
555
556 /* Prefer address from outgoing interface */
557 info->rti_ifa = ifaof_ifpforaddr(info->rti_info[RTAX_DST], info->rti_ifp);
558 #ifdef INET
559 if (info->rti_ifa == NULL) {
560 /* Use first found IPv4 address */
561 bool loopback_ok = info->rti_ifp->if_flags & IFF_LOOPBACK;
562 info->rti_ifa = (struct ifaddr *)in_findlocal(fibnum, loopback_ok);
563 }
564 #endif
565 if (info->rti_ifa == NULL)
566 return (ENETUNREACH);
567 return (0);
568 }
569
570 /*
571 * Fills in rti_ifp and rti_ifa for the provided fib.
572 *
573 * Assume basic consistency checks are executed by callers:
574 * RTAX_DST exists, if RTF_GATEWAY is set, RTAX_GATEWAY exists as well.
575 */
576 int
rt_getifa_fib(struct rt_addrinfo * info,u_int fibnum)577 rt_getifa_fib(struct rt_addrinfo *info, u_int fibnum)
578 {
579 const struct sockaddr *dst, *gateway, *ifaaddr;
580 int error, flags;
581
582 dst = info->rti_info[RTAX_DST];
583 gateway = info->rti_info[RTAX_GATEWAY];
584 ifaaddr = info->rti_info[RTAX_IFA];
585 flags = info->rti_flags;
586
587 /*
588 * ifp may be specified by sockaddr_dl
589 * when protocol address is ambiguous.
590 */
591 error = 0;
592
593 /* If we have interface specified by RTAX_IFP address, try to use it */
594 if ((info->rti_ifp == NULL) && (info->rti_info[RTAX_IFP] != NULL))
595 info->rti_ifp = info_get_ifp(info);
596 /*
597 * If we have source address specified, try to find it
598 * TODO: avoid enumerating all ifas on all interfaces.
599 */
600 if (info->rti_ifa == NULL && ifaaddr != NULL)
601 info->rti_ifa = ifa_ifwithaddr(ifaaddr);
602 if ((info->rti_ifa == NULL) && ((info->rti_flags & RTF_GATEWAY) != 0) &&
603 (gateway->sa_family != dst->sa_family))
604 return (rt_getifa_family(info, fibnum));
605 if (info->rti_ifa == NULL) {
606 const struct sockaddr *sa;
607
608 /*
609 * Most common use case for the userland-supplied routes.
610 *
611 * Choose sockaddr to select ifa.
612 * -- if ifp is set --
613 * Order of preference:
614 * 1) IFA address
615 * 2) gateway address
616 * Note: for interface routes link-level gateway address
617 * is specified to indicate the interface index without
618 * specifying RTF_GATEWAY. In this case, ignore gateway
619 * Note: gateway AF may be different from dst AF. In this case,
620 * ignore gateway
621 * 3) final destination.
622 * 4) if all of these fails, try to get at least link-level ifa.
623 * -- else --
624 * try to lookup gateway or dst in the routing table to get ifa
625 */
626 if (info->rti_info[RTAX_IFA] != NULL)
627 sa = info->rti_info[RTAX_IFA];
628 else if ((info->rti_flags & RTF_GATEWAY) != 0 &&
629 gateway->sa_family == dst->sa_family)
630 sa = gateway;
631 else
632 sa = dst;
633 if (info->rti_ifp != NULL) {
634 info->rti_ifa = ifaof_ifpforaddr(sa, info->rti_ifp);
635 /* Case 4 */
636 if (info->rti_ifa == NULL && gateway != NULL)
637 info->rti_ifa = ifaof_ifpforaddr(gateway, info->rti_ifp);
638 } else if (dst != NULL && gateway != NULL)
639 info->rti_ifa = ifa_ifwithroute(flags, dst, gateway,
640 fibnum);
641 else if (sa != NULL)
642 info->rti_ifa = ifa_ifwithroute(flags, sa, sa,
643 fibnum);
644 }
645 if (info->rti_ifa != NULL) {
646 if (info->rti_ifp == NULL)
647 info->rti_ifp = info->rti_ifa->ifa_ifp;
648 } else
649 error = ENETUNREACH;
650 return (error);
651 }
652
653 void
rt_updatemtu(struct ifnet * ifp)654 rt_updatemtu(struct ifnet *ifp)
655 {
656 struct rib_head *rnh;
657 int mtu;
658 int i, j;
659
660 /*
661 * Try to update rt_mtu for all routes using this interface
662 * Unfortunately the only way to do this is to traverse all
663 * routing tables in all fibs/domains.
664 */
665 for (i = 1; i <= AF_MAX; i++) {
666 mtu = if_getmtu_family(ifp, i);
667 for (j = 0; j < rt_numfibs; j++) {
668 rnh = rt_tables_get_rnh(j, i);
669 if (rnh == NULL)
670 continue;
671 nhops_update_ifmtu(rnh, ifp, mtu);
672 }
673 }
674 }
675
676 #if 0
677 int p_sockaddr(char *buf, int buflen, struct sockaddr *s);
678 int rt_print(char *buf, int buflen, struct rtentry *rt);
679
680 int
681 p_sockaddr(char *buf, int buflen, struct sockaddr *s)
682 {
683 void *paddr = NULL;
684
685 switch (s->sa_family) {
686 case AF_INET:
687 paddr = &((struct sockaddr_in *)s)->sin_addr;
688 break;
689 case AF_INET6:
690 paddr = &((struct sockaddr_in6 *)s)->sin6_addr;
691 break;
692 }
693
694 if (paddr == NULL)
695 return (0);
696
697 if (inet_ntop(s->sa_family, paddr, buf, buflen) == NULL)
698 return (0);
699
700 return (strlen(buf));
701 }
702
703 int
704 rt_print(char *buf, int buflen, struct rtentry *rt)
705 {
706 struct sockaddr *addr, *mask;
707 int i = 0;
708
709 addr = rt_key(rt);
710 mask = rt_mask(rt);
711
712 i = p_sockaddr(buf, buflen, addr);
713 if (!(rt->rt_flags & RTF_HOST)) {
714 buf[i++] = '/';
715 i += p_sockaddr(buf + i, buflen - i, mask);
716 }
717
718 if (rt->rt_flags & RTF_GATEWAY) {
719 buf[i++] = '>';
720 i += p_sockaddr(buf + i, buflen - i, &rt->rt_nhop->gw_sa);
721 }
722
723 return (i);
724 }
725 #endif
726
727 void
rt_maskedcopy(const struct sockaddr * src,struct sockaddr * dst,const struct sockaddr * netmask)728 rt_maskedcopy(const struct sockaddr *src, struct sockaddr *dst,
729 const struct sockaddr *netmask)
730 {
731 const u_char *cp1 = (const u_char *)src;
732 u_char *cp2 = (u_char *)dst;
733 const u_char *cp3 = (const u_char *)netmask;
734 u_char *cplim = cp2 + *cp3;
735 u_char *cplim2 = cp2 + *cp1;
736
737 *cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */
738 cp3 += 2;
739 if (cplim > cplim2)
740 cplim = cplim2;
741 while (cp2 < cplim)
742 *cp2++ = *cp1++ & *cp3++;
743 if (cp2 < cplim2)
744 bzero((caddr_t)cp2, (unsigned)(cplim2 - cp2));
745 }
746
747 /*
748 * Announce interface address arrival/withdraw
749 * Returns 0 on success.
750 */
751 int
rt_addrmsg(int cmd,struct ifaddr * ifa,int fibnum)752 rt_addrmsg(int cmd, struct ifaddr *ifa, int fibnum)
753 {
754
755 KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE,
756 ("unexpected cmd %d", cmd));
757 KASSERT((fibnum >= 0 && fibnum < rt_numfibs),
758 ("%s: fib out of range 0 <=%d<%d", __func__, fibnum, rt_numfibs));
759
760 EVENTHANDLER_DIRECT_INVOKE(rt_addrmsg, ifa, cmd);
761
762 if (V_rt_add_addr_allfibs)
763 fibnum = RT_ALL_FIBS;
764 return (rtsock_addrmsg(cmd, ifa, fibnum));
765 }
766
767 /*
768 * Announce kernel-originated route addition/removal to rtsock based on @rt data.
769 * cmd: RTM_ cmd
770 * @rt: valid rtentry
771 * @nh: nhop object to announce
772 * @fibnum: fib id or RT_ALL_FIBS
773 *
774 * Returns 0 on success.
775 */
776 int
rt_routemsg(int cmd,struct rtentry * rt,struct nhop_object * nh,int fibnum)777 rt_routemsg(int cmd, struct rtentry *rt, struct nhop_object *nh,
778 int fibnum)
779 {
780
781 KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE || cmd == RTM_CHANGE,
782 ("unexpected cmd %d", cmd));
783
784 KASSERT(fibnum == RT_ALL_FIBS || (fibnum >= 0 && fibnum < rt_numfibs),
785 ("%s: fib out of range 0 <=%d<%d", __func__, fibnum, rt_numfibs));
786
787 KASSERT(rt_key(rt) != NULL, (":%s: rt_key must be supplied", __func__));
788
789 return (rtsock_routemsg(cmd, rt, nh, fibnum));
790 }
791
792 /*
793 * Announce kernel-originated route addition/removal to rtsock based on @rt data.
794 * cmd: RTM_ cmd
795 * @info: addrinfo structure with valid data.
796 * @fibnum: fib id or RT_ALL_FIBS
797 *
798 * Returns 0 on success.
799 */
800 int
rt_routemsg_info(int cmd,struct rt_addrinfo * info,int fibnum)801 rt_routemsg_info(int cmd, struct rt_addrinfo *info, int fibnum)
802 {
803
804 KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE || cmd == RTM_CHANGE,
805 ("unexpected cmd %d", cmd));
806
807 KASSERT(fibnum == RT_ALL_FIBS || (fibnum >= 0 && fibnum < rt_numfibs),
808 ("%s: fib out of range 0 <=%d<%d", __func__, fibnum, rt_numfibs));
809
810 KASSERT(info->rti_info[RTAX_DST] != NULL, (":%s: RTAX_DST must be supplied", __func__));
811
812 return (rtsock_routemsg_info(cmd, info, fibnum));
813 }
814
815 void
rt_ifmsg(struct ifnet * ifp)816 rt_ifmsg(struct ifnet *ifp)
817 {
818 rt_ifmsg_14(ifp, 0);
819 }
820
821 void
rt_ifmsg_14(struct ifnet * ifp,int if_flags_mask)822 rt_ifmsg_14(struct ifnet *ifp, int if_flags_mask)
823 {
824 rtsock_callback_p->ifmsg_f(ifp, if_flags_mask);
825 netlink_callback_p->ifmsg_f(ifp, if_flags_mask);
826 }
827
828 /* Netlink-related callbacks needed to glue rtsock, netlink and linuxolator */
829 static void
ignore_route_event(uint32_t fibnum,const struct rib_cmd_info * rc)830 ignore_route_event(uint32_t fibnum, const struct rib_cmd_info *rc)
831 {
832 }
833
834 static void
ignore_ifmsg_event(struct ifnet * ifp,int if_flags_mask)835 ignore_ifmsg_event(struct ifnet *ifp, int if_flags_mask)
836 {
837 }
838
839 static struct rtbridge ignore_cb = {
840 .route_f = ignore_route_event,
841 .ifmsg_f = ignore_ifmsg_event,
842 };
843
844 void *linux_netlink_p = NULL; /* Callback pointer for Linux translator functions */
845 struct rtbridge *rtsock_callback_p = &ignore_cb;
846 struct rtbridge *netlink_callback_p = &ignore_cb;
847