xref: /openbsd/usr.sbin/ldpd/ldpe.h (revision 26889e1d)
1 /*	$OpenBSD: ldpe.h,v 1.80 2024/11/21 13:27:13 claudio Exp $ */
2 
3 /*
4  * Copyright (c) 2013, 2016 Renato Westphal <renato@openbsd.org>
5  * Copyright (c) 2009 Michele Marchetto <michele@openbsd.org>
6  * Copyright (c) 2004, 2005, 2008 Esben Norby <norby@openbsd.org>
7  *
8  * Permission to use, copy, modify, and distribute this software for any
9  * purpose with or without fee is hereby granted, provided that the above
10  * copyright notice and this permission notice appear in all copies.
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19  */
20 
21 #ifndef _LDPE_H_
22 #define _LDPE_H_
23 
24 #include <sys/types.h>
25 #include <sys/queue.h>
26 #include <sys/tree.h>
27 #include <net/pfkeyv2.h>
28 
29 #include "ldpd.h"
30 
31 #define min(x,y) ((x) <= (y) ? (x) : (y))
32 #define max(x,y) ((x) > (y) ? (x) : (y))
33 
34 struct hello_source {
35 	enum hello_type		 type;
36 	struct in_addr		 lsr_id;
37 	struct {
38 		struct iface_af	*ia;
39 		union ldpd_addr	 src_addr;
40 	} link;
41 	struct tnbr		*target;
42 };
43 
44 struct adj {
45 	LIST_ENTRY(adj)		 global_entry;
46 	LIST_ENTRY(adj)		 nbr_entry;
47 	LIST_ENTRY(adj)		 ia_entry;
48 	struct in_addr		 lsr_id;
49 	struct nbr		*nbr;
50 	int			 ds_tlv;
51 	struct hello_source	 source;
52 	struct event		 inactivity_timer;
53 	uint16_t		 holdtime;
54 	union ldpd_addr		 trans_addr;
55 };
56 
57 struct buf_read {
58 	uint8_t			 buf[IBUF_READ_SIZE];
59 	size_t			 wpos;
60 };
61 
62 struct tcp_conn {
63 	struct nbr		*nbr;
64 	int			 fd;
65 	struct buf_read		*rbuf;
66 	struct evbuf		 wbuf;
67 	struct event		 rev;
68 };
69 
70 struct nbr {
71 	RB_ENTRY(nbr)		 id_tree, addr_tree, pid_tree;
72 	struct tcp_conn		*tcp;
73 	LIST_HEAD(, adj)	 adj_list;	/* adjacencies */
74 	struct event		 ev_connect;
75 	struct event		 keepalive_timer;
76 	struct event		 keepalive_timeout;
77 	struct event		 init_timeout;
78 	struct event		 initdelay_timer;
79 
80 	struct mapping_head	 mapping_list;
81 	struct mapping_head	 withdraw_list;
82 	struct mapping_head	 request_list;
83 	struct mapping_head	 release_list;
84 	struct mapping_head	 abortreq_list;
85 
86 	uint32_t		 peerid;	/* unique ID in DB */
87 	int			 af;
88 	int			 ds_tlv;
89 	int			 v4_enabled;	/* announce/process v4 msgs */
90 	int			 v6_enabled;	/* announce/process v6 msgs */
91 	struct in_addr		 id;		/* lsr id */
92 	union ldpd_addr		 laddr;		/* local address */
93 	union ldpd_addr		 raddr;		/* remote address */
94 	uint32_t		 raddr_scope;	/* remote address scope (v6) */
95 	time_t			 uptime;
96 	int			 fd;
97 	int			 state;
98 	uint32_t		 conf_seqnum;
99 	int			 idtimer_cnt;
100 	uint16_t		 keepalive;
101 	uint16_t		 max_pdu_len;
102 
103 	uint32_t		 auth_spi_in;
104 	uint32_t		 auth_spi_out;
105 	int			 auth_established;
106 
107 	int			 flags;
108 };
109 #define F_NBR_GTSM_NEGOTIATED	 0x01
110 #define F_NBR_CAP_DYNAMIC	 0x02
111 #define F_NBR_CAP_TWCARD	 0x04
112 #define F_NBR_CAP_UNOTIF	 0x08
113 
114 RB_HEAD(nbr_id_head, nbr);
115 RB_PROTOTYPE(nbr_id_head, nbr, id_tree, nbr_id_compare)
116 RB_HEAD(nbr_addr_head, nbr);
117 RB_PROTOTYPE(nbr_addr_head, nbr, addr_tree, nbr_addr_compare)
118 RB_HEAD(nbr_pid_head, nbr);
119 RB_PROTOTYPE(nbr_pid_head, nbr, pid_tree, nbr_pid_compare)
120 
121 struct pending_conn {
122 	TAILQ_ENTRY(pending_conn)	 entry;
123 	int				 fd;
124 	int				 af;
125 	union ldpd_addr			 addr;
126 	struct event			 ev_timeout;
127 };
128 #define PENDING_CONN_TIMEOUT	5
129 
130 struct mapping_entry {
131 	TAILQ_ENTRY(mapping_entry)	entry;
132 	struct map			map;
133 };
134 
135 struct ctl_conn;
136 
137 struct ldpd_sysdep {
138 	uint8_t		no_pfkey;
139 	uint8_t		no_md5sig;
140 };
141 
142 extern struct ldpd_conf		*leconf;
143 extern struct ldpd_sysdep	 sysdep;
144 extern struct nbr_id_head	 nbrs_by_id;
145 extern struct nbr_addr_head	 nbrs_by_addr;
146 extern struct nbr_pid_head	 nbrs_by_pid;
147 
148 /* accept.c */
149 void	accept_init(void);
150 int	accept_add(int, void (*)(int, short, void *), void *);
151 void	accept_del(int);
152 void	accept_pause(void);
153 void	accept_unpause(void);
154 
155 /* hello.c */
156 int	 send_hello(enum hello_type, struct iface_af *, struct tnbr *);
157 void	 recv_hello(struct in_addr, struct ldp_msg *, int, union ldpd_addr *,
158 	    struct iface *, int, char *, uint16_t);
159 
160 /* init.c */
161 void	 send_init(struct nbr *);
162 int	 recv_init(struct nbr *, char *, uint16_t);
163 void	 send_capability(struct nbr *, uint16_t, int);
164 int	 recv_capability(struct nbr *, char *, uint16_t);
165 
166 /* keepalive.c */
167 void	 send_keepalive(struct nbr *);
168 int	 recv_keepalive(struct nbr *, char *, uint16_t);
169 
170 /* notification.c */
171 void	 send_notification_full(struct tcp_conn *, struct notify_msg *);
172 void	 send_notification(struct tcp_conn *, uint32_t, uint32_t, uint16_t);
173 void	 send_notification_rtlvs(struct nbr *, uint32_t, uint32_t, uint16_t,
174 	    uint16_t, uint16_t, char *);
175 int	 recv_notification(struct nbr *, char *, uint16_t);
176 int	 gen_status_tlv(struct ibuf *, uint32_t, uint32_t, uint16_t);
177 
178 /* address.c */
179 void	 send_address_single(struct nbr *, struct if_addr *, int);
180 void	 send_address_all(struct nbr *, int);
181 void	 send_mac_withdrawal(struct nbr *, struct map *, uint8_t *);
182 int	 recv_address(struct nbr *, char *, uint16_t);
183 
184 /* labelmapping.c */
185 #define PREFIX_SIZE(x)	(((x) + 7) / 8)
186 void	 send_labelmessage(struct nbr *, uint16_t, struct mapping_head *);
187 int	 recv_labelmessage(struct nbr *, char *, uint16_t, uint16_t);
188 int	 gen_pw_status_tlv(struct ibuf *, uint32_t);
189 uint16_t len_fec_tlv(struct map *);
190 int	 gen_fec_tlv(struct ibuf *, struct map *);
191 int	 tlv_decode_fec_elm(struct nbr *, struct ldp_msg *, char *,
192 	    uint16_t, struct map *);
193 
194 /* ldpe.c */
195 void		 ldpe(int, int, char *);
196 int		 ldpe_imsg_compose_parent(int, pid_t, void *,
197 		    uint16_t);
198 int		 ldpe_imsg_compose_lde(int, uint32_t, pid_t, void *,
199 		    uint16_t);
200 void		 ldpe_reset_nbrs(int);
201 void		 ldpe_reset_ds_nbrs(void);
202 void		 ldpe_remove_dynamic_tnbrs(int);
203 void		 ldpe_stop_init_backoff(int);
204 void		 ldpe_iface_ctl(struct ctl_conn *, unsigned int);
205 void		 ldpe_adj_ctl(struct ctl_conn *);
206 void		 ldpe_nbr_ctl(struct ctl_conn *);
207 void		 mapping_list_add(struct mapping_head *, struct map *);
208 void		 mapping_list_clr(struct mapping_head *);
209 
210 /* interface.c */
211 struct iface	*if_new(struct kif *);
212 void		 if_exit(struct iface *);
213 struct iface	*if_lookup(struct ldpd_conf *, unsigned short);
214 struct iface_af *iface_af_get(struct iface *, int);
215 void		 if_addr_add(struct kaddr *);
216 void		 if_addr_del(struct kaddr *);
217 void		 if_update(struct iface *, int);
218 void		 if_update_all(int);
219 struct ctl_iface *if_to_ctl(struct iface_af *);
220 in_addr_t	 if_get_ipv4_addr(struct iface *);
221 
222 /* adjacency.c */
223 struct adj	*adj_new(struct in_addr, struct hello_source *,
224 		    union ldpd_addr *);
225 void		 adj_del(struct adj *, uint32_t);
226 struct adj	*adj_find(struct hello_source *);
227 int		 adj_get_af(struct adj *adj);
228 void		 adj_start_itimer(struct adj *);
229 void		 adj_stop_itimer(struct adj *);
230 struct tnbr	*tnbr_new(struct ldpd_conf *, int, union ldpd_addr *);
231 struct tnbr	*tnbr_find(struct ldpd_conf *, int, union ldpd_addr *);
232 struct tnbr	*tnbr_check(struct tnbr *);
233 void		 tnbr_update(struct tnbr *);
234 void		 tnbr_update_all(int);
235 struct ctl_adj	*adj_to_ctl(struct adj *);
236 
237 /* neighbor.c */
238 int			 nbr_fsm(struct nbr *, enum nbr_event);
239 struct nbr		*nbr_new(struct in_addr, int, int, union ldpd_addr *,
240 			    uint32_t);
241 void			 nbr_del(struct nbr *);
242 struct nbr		*nbr_find_ldpid(uint32_t);
243 struct nbr		*nbr_find_addr(int, union ldpd_addr *);
244 struct nbr		*nbr_find_peerid(uint32_t);
245 int			 nbr_adj_count(struct nbr *, int);
246 int			 nbr_session_active_role(struct nbr *);
247 void			 nbr_stop_ktimer(struct nbr *);
248 void			 nbr_stop_ktimeout(struct nbr *);
249 void			 nbr_stop_itimeout(struct nbr *);
250 void			 nbr_start_idtimer(struct nbr *);
251 void			 nbr_stop_idtimer(struct nbr *);
252 int			 nbr_pending_idtimer(struct nbr *);
253 int			 nbr_pending_connect(struct nbr *);
254 int			 nbr_establish_connection(struct nbr *);
255 int			 nbr_gtsm_enabled(struct nbr *, struct nbr_params *);
256 int			 nbr_gtsm_setup(int, int, struct nbr_params *);
257 int			 nbr_gtsm_check(int, struct nbr *, struct nbr_params *);
258 struct nbr_params	*nbr_params_new(struct in_addr);
259 struct nbr_params	*nbr_params_find(struct ldpd_conf *, struct in_addr);
260 uint16_t		 nbr_get_keepalive(int, struct in_addr);
261 struct ctl_nbr		*nbr_to_ctl(struct nbr *);
262 void			 nbr_clear_ctl(struct ctl_nbr *);
263 
264 /* packet.c */
265 int			 gen_ldp_hdr(struct ibuf *, uint16_t);
266 int			 gen_msg_hdr(struct ibuf *, uint16_t, uint16_t);
267 int			 send_packet(int, int, union ldpd_addr *,
268 			    struct iface_af *, void *, size_t);
269 void			 disc_recv_packet(int, short, void *);
270 void			 session_accept(int, short, void *);
271 void			 session_accept_nbr(struct nbr *, int);
272 void			 session_shutdown(struct nbr *, uint32_t, uint32_t,
273 			    uint32_t);
274 void			 session_close(struct nbr *);
275 struct tcp_conn		*tcp_new(int, struct nbr *);
276 void			 pending_conn_del(struct pending_conn *);
277 struct pending_conn	*pending_conn_find(int, union ldpd_addr *);
278 
279 /* pfkey.c */
280 int	pfkey_read(int, struct sadb_msg *);
281 int	pfkey_establish(struct ldpd_conf *, struct nbr *);
282 int	pfkey_remove(struct nbr *);
283 int	pfkey_init(void);
284 
285 /* l2vpn.c */
286 void	ldpe_l2vpn_init(struct l2vpn *);
287 void	ldpe_l2vpn_exit(struct l2vpn *);
288 void	ldpe_l2vpn_pw_init(struct l2vpn_pw *);
289 void	ldpe_l2vpn_pw_exit(struct l2vpn_pw *);
290 
291 #endif	/* _LDPE_H_ */
292