xref: /freebsd/sys/net/route/route_ctl.h (revision 2ce553854cbd04595d99e1cfbc9c12a87dde7906)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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_cmd_info {
39 	uint8_t			rc_cmd;		/* RTM_ADD|RTM_DEL|RTM_CHANGE */
40 	uint8_t			spare[3];
41 	uint32_t		rc_nh_weight;	/* new nhop weight */
42 	struct rtentry		*rc_rt;		/* Target entry */
43 	struct nhop_object	*rc_nh_old;	/* Target nhop OR mpath */
44 	struct nhop_object	*rc_nh_new;	/* Target nhop OR mpath */
45 };
46 
47 struct route_nhop_data {
48 	union {
49 		struct nhop_object *rnd_nhop;
50 		struct nhgrp_object *rnd_nhgrp;
51 	};
52 	uint32_t rnd_weight;
53 };
54 
55 int rib_add_route_px(uint32_t fibnum, struct sockaddr *dst, unsigned int plen,
56     struct route_nhop_data *rnd, int op_flags, struct rib_cmd_info *rc);
57 int rib_del_route_px(uint32_t fibnum, struct sockaddr *dst, unsigned int plen,
58     rib_filter_f_t *filter_func, void *filter_arg, int op_flags,
59     struct rib_cmd_info *rc);
60 int rib_del_route_px_gw(uint32_t fibnum, struct sockaddr *dst, unsigned int plen,
61     const struct sockaddr *gw, int op_flags, struct rib_cmd_info *rc);
62 
63 /* operation flags */
64 #define	RTM_F_CREATE	0x01
65 #define	RTM_F_EXCL	0x02
66 #define	RTM_F_REPLACE	0x04
67 #define	RTM_F_APPEND	0x08
68 #define	RTM_F_FORCE	0x10
69 
70 int rib_add_route(uint32_t fibnum, struct rt_addrinfo *info,
71   struct rib_cmd_info *rc);
72 int rib_del_route(uint32_t fibnum, struct rt_addrinfo *info,
73   struct rib_cmd_info *rc);
74 int rib_change_route(uint32_t fibnum, struct rt_addrinfo *info,
75   struct rib_cmd_info *rc);
76 int rib_action(uint32_t fibnum, int action, struct rt_addrinfo *info,
77   struct rib_cmd_info *rc);
78 int rib_handle_ifaddr_info(uint32_t fibnum, int cmd, struct rt_addrinfo *info);
79 
80 typedef void route_notification_t(struct rib_cmd_info *rc, void *);
81 void rib_decompose_notification(struct rib_cmd_info *rc,
82     route_notification_t *cb, void *cbdata);
83 
84 int rib_add_redirect(u_int fibnum, struct sockaddr *dst,
85   struct sockaddr *gateway, struct sockaddr *author, struct ifnet *ifp,
86   int flags, int expire_sec);
87 
88 /* common flags for the functions below */
89 #define	RIB_FLAG_WLOCK		0x01	/* Need exclusive rnh lock */
90 #define	RIB_FLAG_LOCKED		0x02	/* Do not explicitly acquire rnh lock */
91 
92 enum rib_walk_hook {
93 	RIB_WALK_HOOK_PRE,	/* Hook is called before iteration */
94 	RIB_WALK_HOOK_POST,	/* Hook is called after iteration */
95 };
96 typedef int rib_walktree_f_t(struct rtentry *, void *);
97 typedef void rib_walk_hook_f_t(struct rib_head *rnh, enum rib_walk_hook stage,
98     void *arg);
99 void rib_walk(uint32_t fibnum, int af, bool wlock, rib_walktree_f_t *wa_f,
100     void *arg);
101 void rib_walk_ext(uint32_t fibnum, int af, bool wlock, rib_walktree_f_t *wa_f,
102     rib_walk_hook_f_t *hook_f, void *arg);
103 void rib_walk_ext_internal(struct rib_head *rnh, bool wlock,
104     rib_walktree_f_t *wa_f, rib_walk_hook_f_t *hook_f, void *arg);
105 void rib_walk_ext_locked(struct rib_head *rnh, rib_walktree_f_t *wa_f,
106     rib_walk_hook_f_t *hook_f, void *arg);
107 void rib_walk_from(uint32_t fibnum, int family, uint32_t flags, struct sockaddr *prefix,
108     struct sockaddr *mask, rib_walktree_f_t *wa_f, void *arg);
109 
110 void rib_walk_del(u_int fibnum, int family, rib_filter_f_t *filter_f,
111     void *filter_arg, bool report);
112 
113 void rib_foreach_table_walk(int family, bool wlock, rib_walktree_f_t *wa_f,
114     rib_walk_hook_f_t *hook_f, void *arg);
115 void rib_foreach_table_walk_del(int family, rib_filter_f_t *filter_f, void *arg);
116 
117 struct nhop_object;
118 struct nhgrp_object;
119 
120 const struct rtentry *rib_lookup_prefix(uint32_t fibnum, int family,
121     const struct sockaddr *dst, const struct sockaddr *netmask,
122     struct route_nhop_data *rnd);
123 const struct rtentry *rib_lookup_lpm(uint32_t fibnum, int family,
124     const struct sockaddr *dst, struct route_nhop_data *rnd);
125 
126 /* rtentry accessors */
127 bool rt_is_host(const struct rtentry *rt);
128 sa_family_t rt_get_family(const struct rtentry *);
129 struct nhop_object *rt_get_raw_nhop(const struct rtentry *rt);
130 #ifdef INET
131 struct in_addr;
132 void rt_get_inet_prefix_plen(const struct rtentry *rt, struct in_addr *paddr,
133     int *plen, uint32_t *pscopeid);
134 void rt_get_inet_prefix_pmask(const struct rtentry *rt, struct in_addr *paddr,
135     struct in_addr *pmask, uint32_t *pscopeid);
136 struct rtentry *rt_get_inet_parent(uint32_t fibnum, struct in_addr addr, int plen);
137 #endif
138 #ifdef INET6
139 struct in6_addr;
140 void rt_get_inet6_prefix_plen(const struct rtentry *rt, struct in6_addr *paddr,
141     int *plen, uint32_t *pscopeid);
142 void rt_get_inet6_prefix_pmask(const struct rtentry *rt, struct in6_addr *paddr,
143     struct in6_addr *pmask, uint32_t *pscopeid);
144 struct rtentry *rt_get_inet6_parent(uint32_t fibnum, const struct in6_addr *paddr,
145     int plen);
146 
147 struct in6_addr;
148 void ip6_writemask(struct in6_addr *addr6, uint8_t mask);
149 #endif
150 
151 /* Nexthops */
152 uint32_t nhops_get_count(struct rib_head *rh);
153 
154 /* Multipath */
155 struct weightened_nhop;
156 
157 const struct weightened_nhop *nhgrp_get_nhops(const struct nhgrp_object *nhg,
158     uint32_t *pnum_nhops);
159 uint32_t nhgrp_get_count(struct rib_head *rh);
160 
161 /* Route subscriptions */
162 enum rib_subscription_type {
163 	RIB_NOTIFY_IMMEDIATE,
164 	RIB_NOTIFY_DELAYED
165 };
166 
167 struct rib_subscription;
168 typedef void rib_subscription_cb_t(struct rib_head *rnh, struct rib_cmd_info *rc,
169     void *arg);
170 
171 struct rib_subscription *rib_subscribe(uint32_t fibnum, int family,
172     rib_subscription_cb_t *f, void *arg, enum rib_subscription_type type,
173     bool waitok);
174 struct rib_subscription *rib_subscribe_internal(struct rib_head *rnh,
175     rib_subscription_cb_t *f, void *arg, enum rib_subscription_type type,
176     bool waitok);
177 struct rib_subscription *rib_subscribe_locked(struct rib_head *rnh,
178     rib_subscription_cb_t *f, void *arg, enum rib_subscription_type type);
179 void rib_unsubscribe(struct rib_subscription *rs);
180 void rib_unsubscribe_locked(struct rib_subscription *rs);
181 
182 #endif
183