1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2020 Alexander V. Chernikov 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 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. 14 * 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 25 * SUCH DAMAGE. 26 */ 27 28 /* 29 * This header file contains public functions and structures used for 30 * routing table manipulations. 31 */ 32 33 #ifndef _NET_ROUTE_ROUTE_CTL_H_ 34 #define _NET_ROUTE_ROUTE_CTL_H_ 35 36 struct rib_cmd_info { 37 uint8_t rc_cmd; /* RTM_ADD|RTM_DEL|RTM_CHANGE */ 38 uint8_t spare[3]; 39 uint32_t rc_nh_weight; /* new nhop weight */ 40 struct rtentry *rc_rt; /* Target entry */ 41 struct nhop_object *rc_nh_old; /* Target nhop OR mpath */ 42 struct nhop_object *rc_nh_new; /* Target nhop OR mpath */ 43 }; 44 45 struct route_nhop_data { 46 union { 47 struct nhop_object *rnd_nhop; 48 struct nhgrp_object *rnd_nhgrp; 49 }; 50 uint32_t rnd_weight; 51 }; 52 53 int rib_add_route_px(uint32_t fibnum, struct sockaddr *dst, int plen, 54 struct route_nhop_data *rnd, int op_flags, struct rib_cmd_info *rc); 55 int rib_del_route_px(uint32_t fibnum, struct sockaddr *dst, int plen, 56 rib_filter_f_t *filter_func, void *filter_arg, int op_flags, 57 struct rib_cmd_info *rc); 58 int rib_del_route_px_gw(uint32_t fibnum, struct sockaddr *dst, int plen, 59 const struct sockaddr *gw, int op_flags, struct rib_cmd_info *rc); 60 61 /* operation flags */ 62 #define RTM_F_CREATE 0x01 /* Create object if not exists */ 63 #define RTM_F_EXCL 0x02 /* (Deprecated) Do not replace or append if exists */ 64 #define RTM_F_REPLACE 0x04 /* Replace if route (even multipath) if exists */ 65 #define RTM_F_APPEND 0x08 /* Append path to the route */ 66 #define RTM_F_FORCE 0x10 /* Bump operation priority to highest */ 67 68 int rib_add_route(uint32_t fibnum, struct rt_addrinfo *info, 69 struct rib_cmd_info *rc); 70 int rib_del_route(uint32_t fibnum, struct rt_addrinfo *info, 71 struct rib_cmd_info *rc); 72 int rib_change_route(uint32_t fibnum, struct rt_addrinfo *info, 73 struct rib_cmd_info *rc); 74 int rib_action(uint32_t fibnum, int action, struct rt_addrinfo *info, 75 struct rib_cmd_info *rc); 76 int rib_match_gw(const struct rtentry *rt, const struct nhop_object *nh, 77 void *_data); 78 int rib_handle_ifaddr_info(uint32_t fibnum, int cmd, struct rt_addrinfo *info); 79 80 int rib_add_default_route(uint32_t fibnum, int family, struct ifnet *ifp, 81 struct sockaddr *gw, struct rib_cmd_info *rc); 82 83 typedef void route_notification_t(const struct rib_cmd_info *rc, void *); 84 void rib_decompose_notification(const struct rib_cmd_info *rc, 85 route_notification_t *cb, void *cbdata); 86 87 int rib_add_redirect(u_int fibnum, struct sockaddr *dst, 88 struct sockaddr *gateway, struct sockaddr *author, struct ifnet *ifp, 89 int flags, int expire_sec); 90 91 /* common flags for the functions below */ 92 #define RIB_FLAG_WLOCK 0x01 /* Need exclusive rnh lock */ 93 #define RIB_FLAG_LOCKED 0x02 /* Do not explicitly acquire rnh lock */ 94 95 enum rib_walk_hook { 96 RIB_WALK_HOOK_PRE, /* Hook is called before iteration */ 97 RIB_WALK_HOOK_POST, /* Hook is called after iteration */ 98 }; 99 typedef int rib_walktree_f_t(struct rtentry *, void *); 100 typedef void rib_walk_hook_f_t(struct rib_head *rnh, enum rib_walk_hook stage, 101 void *arg); 102 void rib_walk(uint32_t fibnum, int af, bool wlock, rib_walktree_f_t *wa_f, 103 void *arg); 104 void rib_walk_ext(uint32_t fibnum, int af, bool wlock, rib_walktree_f_t *wa_f, 105 rib_walk_hook_f_t *hook_f, void *arg); 106 void rib_walk_ext_internal(struct rib_head *rnh, bool wlock, 107 rib_walktree_f_t *wa_f, rib_walk_hook_f_t *hook_f, void *arg); 108 void rib_walk_ext_locked(struct rib_head *rnh, rib_walktree_f_t *wa_f, 109 rib_walk_hook_f_t *hook_f, void *arg); 110 void rib_walk_from(uint32_t fibnum, int family, uint32_t flags, struct sockaddr *prefix, 111 struct sockaddr *mask, rib_walktree_f_t *wa_f, void *arg); 112 113 void rib_walk_del(u_int fibnum, int family, rib_filter_f_t *filter_f, 114 void *filter_arg, bool report); 115 116 void rib_foreach_table_walk(int family, bool wlock, rib_walktree_f_t *wa_f, 117 rib_walk_hook_f_t *hook_f, void *arg); 118 void rib_foreach_table_walk_del(int family, rib_filter_f_t *filter_f, void *arg); 119 120 struct nhop_object; 121 struct nhgrp_object; 122 struct ucred; 123 124 const struct rtentry *rib_lookup_prefix(uint32_t fibnum, int family, 125 const struct sockaddr *dst, const struct sockaddr *netmask, 126 struct route_nhop_data *rnd); 127 const struct rtentry *rib_lookup_lpm(uint32_t fibnum, int family, 128 const struct sockaddr *dst, struct route_nhop_data *rnd); 129 130 /* rtentry accessors */ 131 bool rt_is_host(const struct rtentry *rt); 132 sa_family_t rt_get_family(const struct rtentry *); 133 struct nhop_object *rt_get_raw_nhop(const struct rtentry *rt); 134 void rt_get_rnd(const struct rtentry *rt, struct route_nhop_data *rnd); 135 bool rt_is_exportable(const struct rtentry *rt, struct ucred *cred); 136 #ifdef INET 137 struct in_addr; 138 void rt_get_inet_prefix_plen(const struct rtentry *rt, struct in_addr *paddr, 139 int *plen, uint32_t *pscopeid); 140 void rt_get_inet_prefix_pmask(const struct rtentry *rt, struct in_addr *paddr, 141 struct in_addr *pmask, uint32_t *pscopeid); 142 struct rtentry *rt_get_inet_parent(uint32_t fibnum, struct in_addr addr, int plen); 143 #endif 144 #ifdef INET6 145 struct in6_addr; 146 void rt_get_inet6_prefix_plen(const struct rtentry *rt, struct in6_addr *paddr, 147 int *plen, uint32_t *pscopeid); 148 void rt_get_inet6_prefix_pmask(const struct rtentry *rt, struct in6_addr *paddr, 149 struct in6_addr *pmask, uint32_t *pscopeid); 150 struct rtentry *rt_get_inet6_parent(uint32_t fibnum, const struct in6_addr *paddr, 151 int plen); 152 153 struct in6_addr; 154 void ip6_writemask(struct in6_addr *addr6, uint8_t mask); 155 #endif 156 157 /* Nexthops */ 158 uint32_t nhops_get_count(struct rib_head *rh); 159 160 /* Multipath */ 161 struct weightened_nhop; 162 163 const struct weightened_nhop *nhgrp_get_nhops(const struct nhgrp_object *nhg, 164 uint32_t *pnum_nhops); 165 uint32_t nhgrp_get_count(struct rib_head *rh); 166 int nhgrp_get_group(struct rib_head *rh, struct weightened_nhop *wn, int num_nhops, 167 uint32_t uidx, struct nhgrp_object **pnhg); 168 169 /* Route subscriptions */ 170 enum rib_subscription_type { 171 RIB_NOTIFY_IMMEDIATE, 172 RIB_NOTIFY_DELAYED 173 }; 174 175 struct rib_subscription; 176 typedef void rib_subscription_cb_t(struct rib_head *rnh, struct rib_cmd_info *rc, 177 void *arg); 178 179 struct rib_subscription *rib_subscribe(uint32_t fibnum, int family, 180 rib_subscription_cb_t *f, void *arg, enum rib_subscription_type type, 181 bool waitok); 182 struct rib_subscription *rib_subscribe_internal(struct rib_head *rnh, 183 rib_subscription_cb_t *f, void *arg, enum rib_subscription_type type, 184 bool waitok); 185 struct rib_subscription *rib_subscribe_locked(struct rib_head *rnh, 186 rib_subscription_cb_t *f, void *arg, enum rib_subscription_type type); 187 void rib_unsubscribe(struct rib_subscription *rs); 188 void rib_unsubscribe_locked(struct rib_subscription *rs); 189 void rib_notify(struct rib_head *rnh, enum rib_subscription_type type, 190 struct rib_cmd_info *rc); 191 192 /* Event bridge */ 193 typedef void route_event_f(uint32_t fibnum, const struct rib_cmd_info *rc); 194 typedef void ifmsg_event_f(struct ifnet *ifp, int if_flags_mask); 195 struct rtbridge{ 196 route_event_f *route_f; 197 ifmsg_event_f *ifmsg_f; 198 }; 199 extern struct rtbridge *rtsock_callback_p; 200 extern struct rtbridge *netlink_callback_p; 201 #endif 202