xref: /freebsd/sys/net/route/route_ctl.h (revision 6be3386466ab79a84b48429ae66244f21526d3df)
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 int rib_add_route(uint32_t fibnum, struct rt_addrinfo *info,
48   struct rib_cmd_info *rc);
49 int rib_del_route(uint32_t fibnum, struct rt_addrinfo *info,
50   struct rib_cmd_info *rc);
51 int rib_change_route(uint32_t fibnum, struct rt_addrinfo *info,
52   struct rib_cmd_info *rc);
53 int rib_action(uint32_t fibnum, int action, struct rt_addrinfo *info,
54   struct rib_cmd_info *rc);
55 int rib_handle_ifaddr_info(uint32_t fibnum, int cmd, struct rt_addrinfo *info);
56 
57 typedef void route_notification_t(struct rib_cmd_info *rc, void *);
58 void rib_decompose_notification(struct rib_cmd_info *rc,
59     route_notification_t *cb, void *cbdata);
60 
61 int rib_add_redirect(u_int fibnum, struct sockaddr *dst,
62   struct sockaddr *gateway, struct sockaddr *author, struct ifnet *ifp,
63   int flags, int expire_sec);
64 
65 enum rib_walk_hook {
66 	RIB_WALK_HOOK_PRE,	/* Hook is called before iteration */
67 	RIB_WALK_HOOK_POST,	/* Hook is called after iteration */
68 };
69 typedef int rib_walktree_f_t(struct rtentry *, void *);
70 typedef void rib_walk_hook_f_t(struct rib_head *rnh, enum rib_walk_hook stage,
71     void *arg);
72 void rib_walk(uint32_t fibnum, int af, bool wlock, rib_walktree_f_t *wa_f,
73     void *arg);
74 void rib_walk_ext(uint32_t fibnum, int af, bool wlock, rib_walktree_f_t *wa_f,
75     rib_walk_hook_f_t *hook_f, void *arg);
76 void rib_walk_ext_internal(struct rib_head *rnh, bool wlock,
77     rib_walktree_f_t *wa_f, rib_walk_hook_f_t *hook_f, void *arg);
78 void rib_walk_ext_locked(struct rib_head *rnh, rib_walktree_f_t *wa_f,
79     rib_walk_hook_f_t *hook_f, void *arg);
80 
81 void rib_walk_del(u_int fibnum, int family, rib_filter_f_t *filter_f,
82     void *arg, bool report);
83 
84 void rib_foreach_table_walk(int family, bool wlock, rib_walktree_f_t *wa_f,
85     rib_walk_hook_f_t *hook_f, void *arg);
86 void rib_foreach_table_walk_del(int family, rib_filter_f_t *filter_f, void *arg);
87 
88 struct nhop_object;
89 struct nhgrp_object;
90 struct route_nhop_data {
91 	union {
92 		struct nhop_object *rnd_nhop;
93 		struct nhgrp_object *rnd_nhgrp;
94 	};
95 	uint32_t rnd_weight;
96 };
97 
98 const struct rtentry *rib_lookup_prefix(uint32_t fibnum, int family,
99     const struct sockaddr *dst, const struct sockaddr *netmask,
100     struct route_nhop_data *rnd);
101 const struct rtentry *rib_lookup_lpm(uint32_t fibnum, int family,
102     const struct sockaddr *dst, struct route_nhop_data *rnd);
103 
104 /* rtentry accessors */
105 bool rt_is_host(const struct rtentry *rt);
106 sa_family_t rt_get_family(const struct rtentry *);
107 struct nhop_object *rt_get_raw_nhop(const struct rtentry *rt);
108 #ifdef INET
109 struct in_addr;
110 void rt_get_inet_prefix_plen(const struct rtentry *rt, struct in_addr *paddr,
111     int *plen, uint32_t *pscopeid);
112 void rt_get_inet_prefix_pmask(const struct rtentry *rt, struct in_addr *paddr,
113     struct in_addr *pmask, uint32_t *pscopeid);
114 #endif
115 #ifdef INET6
116 struct in6_addr;
117 void rt_get_inet6_prefix_plen(const struct rtentry *rt, struct in6_addr *paddr,
118     int *plen, uint32_t *pscopeid);
119 void rt_get_inet6_prefix_pmask(const struct rtentry *rt, struct in6_addr *paddr,
120     struct in6_addr *pmask, uint32_t *pscopeid);
121 #endif
122 
123 /* Nexthops */
124 uint32_t nhops_get_count(struct rib_head *rh);
125 
126 /* Multipath */
127 struct weightened_nhop;
128 
129 struct weightened_nhop *nhgrp_get_nhops(struct nhgrp_object *nhg,
130     uint32_t *pnum_nhops);
131 uint32_t nhgrp_get_count(struct rib_head *rh);
132 
133 /* Route subscriptions */
134 enum rib_subscription_type {
135 	RIB_NOTIFY_IMMEDIATE,
136 	RIB_NOTIFY_DELAYED
137 };
138 
139 struct rib_subscription;
140 typedef void rib_subscription_cb_t(struct rib_head *rnh, struct rib_cmd_info *rc,
141     void *arg);
142 
143 struct rib_subscription *rib_subscribe(uint32_t fibnum, int family,
144     rib_subscription_cb_t *f, void *arg, enum rib_subscription_type type,
145     bool waitok);
146 struct rib_subscription *rib_subscribe_internal(struct rib_head *rnh,
147     rib_subscription_cb_t *f, void *arg, enum rib_subscription_type type,
148     bool waitok);
149 struct rib_subscription *rib_subscribe_locked(struct rib_head *rnh,
150     rib_subscription_cb_t *f, void *arg, enum rib_subscription_type type);
151 void rib_unsibscribe(struct rib_subscription *rs);
152 void rib_unsibscribe_locked(struct rib_subscription *rs);
153 
154 #endif
155