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