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