2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2020 Alexander V. Chernikov
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include "opt_route.h"
33 #include <sys/cdefs.h>
34 #include <sys/param.h>
35 #include <sys/systm.h>
37 #include <sys/rmlock.h>
38 #include <sys/rwlock.h>
39 #include <sys/malloc.h>
41 #include <sys/socket.h>
42 #include <sys/sysctl.h>
43 #include <sys/kernel.h>
46 #include <net/if_var.h>
47 #include <net/if_dl.h>
48 #include <net/route.h>
49 #include <net/route/route_ctl.h>
50 #include <net/route/route_var.h>
53 #include <netinet/in.h>
54 #include <netinet/in_var.h>
55 #include <netinet/in_fib.h>
57 #include <net/route/nhop_utils.h>
58 #include <net/route/nhop.h>
59 #include <net/route/nhop_var.h>
62 * This file contains the supporting functions for adding/deleting/updating
63 * multipath routes to the routing table.
66 SYSCTL_DECL(_net_route);
67 VNET_DEFINE(u_int, fib_hash_outbound) = 0;
68 SYSCTL_UINT(_net_route, OID_AUTO, hash_outbound, CTLFLAG_RD | CTLFLAG_VNET,
69 &VNET_NAME(fib_hash_outbound), 0,
70 "Compute flowid for locally-originated packets");
72 /* Default entropy to add to the hash calculation for the outbound connections*/
73 uint8_t mpath_entropy_key[MPATH_ENTROPY_KEY_LEN] = {
74 0x6d, 0x5a, 0x56, 0xda, 0x25, 0x5b, 0x0e, 0xc2,
75 0x41, 0x67, 0x25, 0x3d, 0x43, 0xa3, 0x8f, 0xb0,
76 0xd0, 0xca, 0x2b, 0xcb, 0xae, 0x7b, 0x30, 0xb4,
77 0x77, 0xcb, 0x2d, 0xa3, 0x80, 0x30, 0xf2, 0x0c,
78 0x6a, 0x42, 0xb7, 0x3b, 0xbe, 0xac, 0x01, 0xfa,
83 * Tries to add @rnd_add nhop to the existing set of nhops (@nh_orig) for the
84 * prefix specified by @rt.
86 * Return 0 ans consumes rt / rnd_add nhop references. @rc gets populated
87 * with the operation result.
88 * Otherwise errno is returned.
90 * caller responsibility is to unlock/free rt and
94 add_route_mpath(struct rib_head *rnh, struct rt_addrinfo *info,
95 struct rtentry *rt, struct route_nhop_data *rnd_add,
96 struct route_nhop_data *rnd_orig, struct rib_cmd_info *rc)
99 struct route_nhop_data rnd_new;
103 * It is possible that multiple rtsock speakers will try to update
104 * the same route simultaneously. Reduce the chance of failing the
105 * request by retrying the cycle multiple times.
107 for (int i = 0; i < RIB_MAX_RETRIES; i++) {
108 error = nhgrp_get_addition_group(rnh, rnd_orig, rnd_add,
115 * Group creation failed, most probably because
116 * @rnd_orig data got scheduled for deletion.
117 * Refresh @rnd_orig data and retry.
120 lookup_prefix(rnh, info, rnd_orig);
125 error = change_route_conditional(rnh, rt, info, rnd_orig,
129 RTSTAT_INC(rts_add_retry);
132 if (V_fib_hash_outbound == 0 && error == 0 &&
133 NH_IS_NHGRP(rc->rc_nh_new)) {
135 * First multipath route got installed. Enable local
136 * outbound connections hashing.
139 printf("FIB: enabled flowid calculation for locally-originated packets\n");
140 V_fib_hash_outbound = 1;
146 struct rt_match_info {
147 struct rt_addrinfo *info;
152 gw_filter_func(const struct nhop_object *nh, void *_data)
154 struct rt_match_info *ri = (struct rt_match_info *)_data;
156 return (check_info_match_nhop(ri->info, ri->rt, nh) == 0);
160 * Tries to delete matching paths from @nhg.
161 * Returns 0 on success and updates operation result in @rc.
164 del_route_mpath(struct rib_head *rh, struct rt_addrinfo *info,
165 struct rtentry *rt, struct nhgrp_object *nhg,
166 struct rib_cmd_info *rc)
168 struct route_nhop_data rnd;
169 struct rt_match_info ri = { .info = info, .rt = rt };
172 RIB_WLOCK_ASSERT(rh);
175 * Require gateway to delete multipath routes, to forbid
176 * deleting all paths at once.
177 * If the filter function is provided, skip gateway check to
178 * allow rib_walk_del() delete routes for any criteria based
179 * on provided callback.
181 if ((info->rti_info[RTAX_GATEWAY] == NULL) && (info->rti_filter == NULL))
184 error = nhgrp_get_filtered_group(rh, nhg, gw_filter_func, (void *)&ri,
187 error = change_route_nhop(rh, rt, info, &rnd, rc);