xref: /openbsd/usr.sbin/ldpd/ldpe.h (revision 4ff910d1)
1 /*	$OpenBSD: ldpe.h,v 1.79 2021/01/19 15:59:25 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 tcp_conn {
58 	struct nbr		*nbr;
59 	int			 fd;
60 	struct ibuf_read	*rbuf;
61 	struct evbuf		 wbuf;
62 	struct event		 rev;
63 };
64 
65 struct nbr {
66 	RB_ENTRY(nbr)		 id_tree, addr_tree, pid_tree;
67 	struct tcp_conn		*tcp;
68 	LIST_HEAD(, adj)	 adj_list;	/* adjacencies */
69 	struct event		 ev_connect;
70 	struct event		 keepalive_timer;
71 	struct event		 keepalive_timeout;
72 	struct event		 init_timeout;
73 	struct event		 initdelay_timer;
74 
75 	struct mapping_head	 mapping_list;
76 	struct mapping_head	 withdraw_list;
77 	struct mapping_head	 request_list;
78 	struct mapping_head	 release_list;
79 	struct mapping_head	 abortreq_list;
80 
81 	uint32_t		 peerid;	/* unique ID in DB */
82 	int			 af;
83 	int			 ds_tlv;
84 	int			 v4_enabled;	/* announce/process v4 msgs */
85 	int			 v6_enabled;	/* announce/process v6 msgs */
86 	struct in_addr		 id;		/* lsr id */
87 	union ldpd_addr		 laddr;		/* local address */
88 	union ldpd_addr		 raddr;		/* remote address */
89 	uint32_t		 raddr_scope;	/* remote address scope (v6) */
90 	time_t			 uptime;
91 	int			 fd;
92 	int			 state;
93 	uint32_t		 conf_seqnum;
94 	int			 idtimer_cnt;
95 	uint16_t		 keepalive;
96 	uint16_t		 max_pdu_len;
97 
98 	uint32_t		 auth_spi_in;
99 	uint32_t		 auth_spi_out;
100 	int			 auth_established;
101 
102 	int			 flags;
103 };
104 #define F_NBR_GTSM_NEGOTIATED	 0x01
105 #define F_NBR_CAP_DYNAMIC	 0x02
106 #define F_NBR_CAP_TWCARD	 0x04
107 #define F_NBR_CAP_UNOTIF	 0x08
108 
109 RB_HEAD(nbr_id_head, nbr);
110 RB_PROTOTYPE(nbr_id_head, nbr, id_tree, nbr_id_compare)
111 RB_HEAD(nbr_addr_head, nbr);
112 RB_PROTOTYPE(nbr_addr_head, nbr, addr_tree, nbr_addr_compare)
113 RB_HEAD(nbr_pid_head, nbr);
114 RB_PROTOTYPE(nbr_pid_head, nbr, pid_tree, nbr_pid_compare)
115 
116 struct pending_conn {
117 	TAILQ_ENTRY(pending_conn)	 entry;
118 	int				 fd;
119 	int				 af;
120 	union ldpd_addr			 addr;
121 	struct event			 ev_timeout;
122 };
123 #define PENDING_CONN_TIMEOUT	5
124 
125 struct mapping_entry {
126 	TAILQ_ENTRY(mapping_entry)	entry;
127 	struct map			map;
128 };
129 
130 struct ctl_conn;
131 
132 struct ldpd_sysdep {
133 	uint8_t		no_pfkey;
134 	uint8_t		no_md5sig;
135 };
136 
137 extern struct ldpd_conf		*leconf;
138 extern struct ldpd_sysdep	 sysdep;
139 extern struct nbr_id_head	 nbrs_by_id;
140 extern struct nbr_addr_head	 nbrs_by_addr;
141 extern struct nbr_pid_head	 nbrs_by_pid;
142 
143 /* accept.c */
144 void	accept_init(void);
145 int	accept_add(int, void (*)(int, short, void *), void *);
146 void	accept_del(int);
147 void	accept_pause(void);
148 void	accept_unpause(void);
149 
150 /* hello.c */
151 int	 send_hello(enum hello_type, struct iface_af *, struct tnbr *);
152 void	 recv_hello(struct in_addr, struct ldp_msg *, int, union ldpd_addr *,
153 	    struct iface *, int, char *, uint16_t);
154 
155 /* init.c */
156 void	 send_init(struct nbr *);
157 int	 recv_init(struct nbr *, char *, uint16_t);
158 void	 send_capability(struct nbr *, uint16_t, int);
159 int	 recv_capability(struct nbr *, char *, uint16_t);
160 
161 /* keepalive.c */
162 void	 send_keepalive(struct nbr *);
163 int	 recv_keepalive(struct nbr *, char *, uint16_t);
164 
165 /* notification.c */
166 void	 send_notification_full(struct tcp_conn *, struct notify_msg *);
167 void	 send_notification(struct tcp_conn *, uint32_t, uint32_t, uint16_t);
168 void	 send_notification_rtlvs(struct nbr *, uint32_t, uint32_t, uint16_t,
169 	    uint16_t, uint16_t, char *);
170 int	 recv_notification(struct nbr *, char *, uint16_t);
171 int	 gen_status_tlv(struct ibuf *, uint32_t, uint32_t, uint16_t);
172 
173 /* address.c */
174 void	 send_address_single(struct nbr *, struct if_addr *, int);
175 void	 send_address_all(struct nbr *, int);
176 void	 send_mac_withdrawal(struct nbr *, struct map *, uint8_t *);
177 int	 recv_address(struct nbr *, char *, uint16_t);
178 
179 /* labelmapping.c */
180 #define PREFIX_SIZE(x)	(((x) + 7) / 8)
181 void	 send_labelmessage(struct nbr *, uint16_t, struct mapping_head *);
182 int	 recv_labelmessage(struct nbr *, char *, uint16_t, uint16_t);
183 int	 gen_pw_status_tlv(struct ibuf *, uint32_t);
184 uint16_t len_fec_tlv(struct map *);
185 int	 gen_fec_tlv(struct ibuf *, struct map *);
186 int	 tlv_decode_fec_elm(struct nbr *, struct ldp_msg *, char *,
187 	    uint16_t, struct map *);
188 
189 /* ldpe.c */
190 void		 ldpe(int, int, char *);
191 int		 ldpe_imsg_compose_parent(int, pid_t, void *,
192 		    uint16_t);
193 int		 ldpe_imsg_compose_lde(int, uint32_t, pid_t, void *,
194 		    uint16_t);
195 void		 ldpe_reset_nbrs(int);
196 void		 ldpe_reset_ds_nbrs(void);
197 void		 ldpe_remove_dynamic_tnbrs(int);
198 void		 ldpe_stop_init_backoff(int);
199 void		 ldpe_iface_ctl(struct ctl_conn *, unsigned int);
200 void		 ldpe_adj_ctl(struct ctl_conn *);
201 void		 ldpe_nbr_ctl(struct ctl_conn *);
202 void		 mapping_list_add(struct mapping_head *, struct map *);
203 void		 mapping_list_clr(struct mapping_head *);
204 
205 /* interface.c */
206 struct iface	*if_new(struct kif *);
207 void		 if_exit(struct iface *);
208 struct iface	*if_lookup(struct ldpd_conf *, unsigned short);
209 struct iface_af *iface_af_get(struct iface *, int);
210 void		 if_addr_add(struct kaddr *);
211 void		 if_addr_del(struct kaddr *);
212 void		 if_update(struct iface *, int);
213 void		 if_update_all(int);
214 struct ctl_iface *if_to_ctl(struct iface_af *);
215 in_addr_t	 if_get_ipv4_addr(struct iface *);
216 
217 /* adjacency.c */
218 struct adj	*adj_new(struct in_addr, struct hello_source *,
219 		    union ldpd_addr *);
220 void		 adj_del(struct adj *, uint32_t);
221 struct adj	*adj_find(struct hello_source *);
222 int		 adj_get_af(struct adj *adj);
223 void		 adj_start_itimer(struct adj *);
224 void		 adj_stop_itimer(struct adj *);
225 struct tnbr	*tnbr_new(struct ldpd_conf *, int, union ldpd_addr *);
226 struct tnbr	*tnbr_find(struct ldpd_conf *, int, union ldpd_addr *);
227 struct tnbr	*tnbr_check(struct tnbr *);
228 void		 tnbr_update(struct tnbr *);
229 void		 tnbr_update_all(int);
230 struct ctl_adj	*adj_to_ctl(struct adj *);
231 
232 /* neighbor.c */
233 int			 nbr_fsm(struct nbr *, enum nbr_event);
234 struct nbr		*nbr_new(struct in_addr, int, int, union ldpd_addr *,
235 			    uint32_t);
236 void			 nbr_del(struct nbr *);
237 struct nbr		*nbr_find_ldpid(uint32_t);
238 struct nbr		*nbr_find_addr(int, union ldpd_addr *);
239 struct nbr		*nbr_find_peerid(uint32_t);
240 int			 nbr_adj_count(struct nbr *, int);
241 int			 nbr_session_active_role(struct nbr *);
242 void			 nbr_stop_ktimer(struct nbr *);
243 void			 nbr_stop_ktimeout(struct nbr *);
244 void			 nbr_stop_itimeout(struct nbr *);
245 void			 nbr_start_idtimer(struct nbr *);
246 void			 nbr_stop_idtimer(struct nbr *);
247 int			 nbr_pending_idtimer(struct nbr *);
248 int			 nbr_pending_connect(struct nbr *);
249 int			 nbr_establish_connection(struct nbr *);
250 int			 nbr_gtsm_enabled(struct nbr *, struct nbr_params *);
251 int			 nbr_gtsm_setup(int, int, struct nbr_params *);
252 int			 nbr_gtsm_check(int, struct nbr *, struct nbr_params *);
253 struct nbr_params	*nbr_params_new(struct in_addr);
254 struct nbr_params	*nbr_params_find(struct ldpd_conf *, struct in_addr);
255 uint16_t		 nbr_get_keepalive(int, struct in_addr);
256 struct ctl_nbr		*nbr_to_ctl(struct nbr *);
257 void			 nbr_clear_ctl(struct ctl_nbr *);
258 
259 /* packet.c */
260 int			 gen_ldp_hdr(struct ibuf *, uint16_t);
261 int			 gen_msg_hdr(struct ibuf *, uint16_t, uint16_t);
262 int			 send_packet(int, int, union ldpd_addr *,
263 			    struct iface_af *, void *, size_t);
264 void			 disc_recv_packet(int, short, void *);
265 void			 session_accept(int, short, void *);
266 void			 session_accept_nbr(struct nbr *, int);
267 void			 session_shutdown(struct nbr *, uint32_t, uint32_t,
268 			    uint32_t);
269 void			 session_close(struct nbr *);
270 struct tcp_conn		*tcp_new(int, struct nbr *);
271 void			 pending_conn_del(struct pending_conn *);
272 struct pending_conn	*pending_conn_find(int, union ldpd_addr *);
273 
274 /* pfkey.c */
275 int	pfkey_read(int, struct sadb_msg *);
276 int	pfkey_establish(struct ldpd_conf *, struct nbr *);
277 int	pfkey_remove(struct nbr *);
278 int	pfkey_init(void);
279 
280 /* l2vpn.c */
281 void	ldpe_l2vpn_init(struct l2vpn *);
282 void	ldpe_l2vpn_exit(struct l2vpn *);
283 void	ldpe_l2vpn_pw_init(struct l2vpn_pw *);
284 void	ldpe_l2vpn_pw_exit(struct l2vpn_pw *);
285 
286 #endif	/* _LDPE_H_ */
287