xref: /openbsd/usr.sbin/ripd/ripd.h (revision d200470e)
1 /*	$OpenBSD: ripd.h,v 1.28 2021/01/19 10:28:07 claudio Exp $ */
2 
3 /*
4  * Copyright (c) 2004 Esben Norby <norby@openbsd.org>
5  * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #ifndef RIPD_H
21 #define RIPD_H
22 
23 #include <sys/queue.h>
24 #include <sys/socket.h>
25 #include <sys/tree.h>
26 #include <md5.h>
27 #include <net/if.h>
28 #include <netinet/in.h>
29 #include <event.h>
30 
31 #include <imsg.h>
32 
33 #define	CONF_FILE		"/etc/ripd.conf"
34 #define	RIPD_SOCKET		"/var/run/ripd.sock"
35 #define	RIPD_USER		"_ripd"
36 
37 #define	NBR_HASHSIZE		128
38 #define	NBR_IDSELF		1
39 #define	NBR_CNTSTART		(NBR_IDSELF + 1)
40 
41 #define	ROUTE_TIMEOUT		180
42 #define ROUTE_GARBAGE		120
43 
44 #define	NBR_TIMEOUT		180
45 
46 #define READ_BUF_SIZE		65535
47 #define RT_BUF_SIZE		16384
48 #define RT_BUF_SIZE		16384
49 #define MAX_RTSOCK_BUF		(2 * 1024 * 1024)
50 
51 #define	RIPD_FLAG_NO_FIB_UPDATE	0x0001
52 
53 #define	F_RIPD_INSERTED		0x0001
54 #define	F_KERNEL		0x0002
55 #define	F_CONNECTED		0x0008
56 #define	F_DOWN			0x0010
57 #define	F_STATIC		0x0020
58 #define	F_DYNAMIC		0x0040
59 #define	F_REDISTRIBUTED		0x0100
60 #define	F_REJECT		0x0200
61 #define	F_BLACKHOLE		0x0400
62 
63 #define REDISTRIBUTE_ON		0x01
64 
65 #define	OPT_SPLIT_HORIZON	0x01
66 #define	OPT_SPLIT_POISONED	0x02
67 #define	OPT_TRIGGERED_UPDATES	0x04
68 
69 enum imsg_type {
70 	IMSG_NONE,
71 	IMSG_CTL_END,
72 	IMSG_CTL_RELOAD,
73 	IMSG_CTL_IFINFO,
74 	IMSG_IFINFO,
75 	IMSG_CTL_FIB_COUPLE,
76 	IMSG_CTL_FIB_DECOUPLE,
77 	IMSG_CTL_KROUTE,
78 	IMSG_CTL_KROUTE_ADDR,
79 	IMSG_CTL_SHOW_INTERFACE,
80 	IMSG_CTL_SHOW_IFACE,
81 	IMSG_CTL_SHOW_NBR,
82 	IMSG_CTL_SHOW_RIB,
83 	IMSG_CTL_LOG_VERBOSE,
84 	IMSG_KROUTE_CHANGE,
85 	IMSG_KROUTE_DELETE,
86 	IMSG_NETWORK_ADD,
87 	IMSG_NETWORK_DEL,
88 	IMSG_ROUTE_FEED,
89 	IMSG_RESPONSE_ADD,
90 	IMSG_SEND_RESPONSE,
91 	IMSG_FULL_RESPONSE,
92 	IMSG_ROUTE_REQUEST,
93 	IMSG_ROUTE_REQUEST_END,
94 	IMSG_FULL_REQUEST,
95 	IMSG_REQUEST_ADD,
96 	IMSG_SEND_REQUEST,
97 	IMSG_SEND_TRIGGERED_UPDATE,
98 	IMSG_DEMOTE
99 };
100 
101 struct imsgev {
102 	struct imsgbuf		 ibuf;
103 	void			(*handler)(int, short, void *);
104 	struct event		 ev;
105 	void			*data;
106 	short			 events;
107 };
108 
109 /* interface states */
110 #define IF_STA_DOWN		0x01
111 #define IF_STA_ACTIVE		(~IF_STA_DOWN)
112 #define IF_STA_ANY		0x7f
113 
114 /* interface events */
115 enum iface_event {
116 	IF_EVT_NOTHING,
117 	IF_EVT_UP,
118 	IF_EVT_DOWN
119 };
120 
121 /* interface actions */
122 enum iface_action {
123 	IF_ACT_NOTHING,
124 	IF_ACT_STRT,
125 	IF_ACT_RST
126 };
127 
128 /* interface types */
129 enum iface_type {
130 	IF_TYPE_POINTOPOINT,
131 	IF_TYPE_BROADCAST,
132 	IF_TYPE_NBMA,
133 	IF_TYPE_POINTOMULTIPOINT
134 };
135 
136 /* neighbor states */
137 #define NBR_STA_DOWN		0x01
138 #define	NBR_STA_REQ_RCVD	0x02
139 #define NBR_STA_ACTIVE		(~NBR_STA_DOWN)
140 #define NBR_STA_ANY		0xff
141 
142 struct auth_md {
143 	TAILQ_ENTRY(auth_md)	 entry;
144 	u_int32_t		 seq_modulator;
145 	u_int8_t		 key[MD5_DIGEST_LENGTH];
146 	u_int8_t		 keyid;
147 };
148 
149 #define MAX_SIMPLE_AUTH_LEN	16
150 
151 /* auth types */
152 enum auth_type {
153 	AUTH_NONE = 1,
154 	AUTH_SIMPLE,
155 	AUTH_CRYPT
156 };
157 
158 TAILQ_HEAD(auth_md_head, auth_md);
159 TAILQ_HEAD(packet_head, packet_entry);
160 
161 struct iface {
162 	LIST_ENTRY(iface)	 entry;
163 	LIST_HEAD(, nbr)	 nbr_list;
164 	LIST_HEAD(, nbr_failed)	 failed_nbr_list;
165 	char			 name[IF_NAMESIZE];
166 	char			 demote_group[IFNAMSIZ];
167 	u_int8_t		 auth_key[MAX_SIMPLE_AUTH_LEN];
168 	struct in_addr		 addr;
169 	struct in_addr		 dst;
170 	struct in_addr		 mask;
171 	struct packet_head	 rq_list;
172 	struct packet_head	 rp_list;
173 	struct auth_md_head	 auth_md_list;
174 
175 	u_int64_t		 baudrate;
176 	time_t			 uptime;
177 	u_int			 mtu;
178 	int			 fd; /* XXX */
179 	int			 state;
180 	u_short			 ifindex;
181 	u_int16_t		 cost;
182 	u_int16_t		 flags;
183 	enum iface_type		 type;
184 	enum auth_type		 auth_type;
185 	u_int8_t		 linktype;
186 	u_int8_t		 if_type;
187 	u_int8_t		 passive;
188 	u_int8_t		 linkstate;
189 	u_int8_t		 auth_keyid;
190 };
191 
192 struct rip_route {
193 	struct in_addr		 address;
194 	struct in_addr		 mask;
195 	struct in_addr		 nexthop;
196 	int			 refcount;
197 	u_short			 ifindex;
198 	u_int8_t		 metric;
199 };
200 
201 struct packet_entry {
202 	TAILQ_ENTRY(packet_entry)	 entry;
203 	struct rip_route		*rr;
204 };
205 
206 #define	REDIST_CONNECTED	0x01
207 #define	REDIST_STATIC		0x02
208 #define	REDIST_LABEL		0x04
209 #define	REDIST_ADDR		0x08
210 #define	REDIST_DEFAULT		0x10
211 #define	REDIST_NO		0x20
212 
213 struct redistribute {
214 	SIMPLEQ_ENTRY(redistribute)	entry;
215 	struct in_addr			addr;
216 	struct in_addr			mask;
217 	u_int32_t			metric;
218 	u_int16_t			label;
219 	u_int16_t			type;
220 };
221 
222 struct ripd_conf {
223 	struct event		 ev;
224 	struct event		 report_timer;
225 	LIST_HEAD(, iface)	 iface_list;
226 	SIMPLEQ_HEAD(, redistribute) redist_list;
227 
228 	u_int32_t		 opts;
229 #define RIPD_OPT_VERBOSE	0x00000001
230 #define	RIPD_OPT_VERBOSE2	0x00000002
231 #define	RIPD_OPT_NOACTION	0x00000004
232 #define	RIPD_OPT_FORCE_DEMOTE	0x00000008
233 	int			 flags;
234 	int			 options;
235 	int			 rip_socket;
236 	int			 redistribute;
237 	u_int8_t		 fib_priority;
238 	u_int			 rdomain;
239 	char			*csock;
240 };
241 
242 /* kroute */
243 struct kroute {
244 	struct in_addr	prefix;
245 	struct in_addr	netmask;
246 	struct in_addr	nexthop;
247 	u_int16_t	flags;
248 	u_int16_t	rtlabel;
249 	u_short		ifindex;
250 	u_int8_t	metric;
251 	u_int8_t	priority;
252 };
253 
254 struct kif {
255 	char		 ifname[IF_NAMESIZE];
256 	u_int64_t	 baudrate;
257 	int		 flags;
258 	int		 mtu;
259 	u_short		 ifindex;
260 	u_int8_t	 if_type;
261 	u_int8_t	 link_state;
262 	u_int8_t	 nh_reachable;	/* for nexthop verification */
263 };
264 
265 /* control data structures */
266 struct ctl_iface {
267 	char			 name[IF_NAMESIZE];
268 	struct in_addr		 addr;
269 	struct in_addr		 mask;
270 
271 	time_t			 uptime;
272 	time_t			 report_timer;
273 
274 	u_int64_t		 baudrate;
275 	unsigned int		 ifindex;
276 	int			 state;
277 	int			 mtu;
278 
279 	u_int16_t		 flags;
280 	u_int16_t		 metric;
281 	enum iface_type		 type;
282 	u_int8_t		 linkstate;
283 	u_int8_t		 if_type;
284 	u_int8_t		 passive;
285 };
286 
287 struct ctl_rt {
288 	struct in_addr		 prefix;
289 	struct in_addr		 netmask;
290 	struct in_addr		 nexthop;
291 	time_t			 uptime;
292 	time_t			 expire;
293 	u_int32_t		 metric;
294 	u_int16_t		 flags;
295 };
296 
297 struct ctl_nbr {
298 	char			 name[IF_NAMESIZE];
299 	struct in_addr		 id;
300 	struct in_addr		 addr;
301 	time_t			 dead_timer;
302 	time_t			 uptime;
303 	int			 nbr_state;
304 	int			 iface_state;
305 };
306 
307 struct demote_msg {
308 	char			 demote_group[IF_NAMESIZE];
309 	int			 level;
310 };
311 
312 int		 kif_init(void);
313 int		 kr_init(int, u_int, u_int8_t);
314 int		 kr_change(struct kroute *);
315 int		 kr_delete(struct kroute *);
316 void		 kr_shutdown(void);
317 void		 kr_fib_couple(void);
318 void		 kr_fib_decouple(void);
319 void		 kr_dispatch_msg(int, short, void *);
320 void		 kr_show_route(struct imsg *);
321 void		 kr_ifinfo(char *, pid_t);
322 struct kif	*kif_findname(char *);
323 
324 in_addr_t	 prefixlen2mask(u_int8_t);
325 u_int8_t	 mask2prefixlen(in_addr_t);
326 
327 /* ripd.c */
328 void		 main_imsg_compose_ripe(int, pid_t, void *, u_int16_t);
329 void		 main_imsg_compose_rde(int, pid_t, void *, u_int16_t);
330 int		 rip_redistribute(struct kroute *);
331 void		 imsg_event_add(struct imsgev *);
332 int		 imsg_compose_event(struct imsgev *, u_int16_t, u_int32_t,
333 		    pid_t, int, void *, u_int16_t);
334 
335 /* parse.y */
336 struct ripd_conf	*parse_config(char *, int);
337 int			 cmdline_symset(char *);
338 
339 /* carp.c */
340 int		 carp_demote_init(char *, int);
341 void		 carp_demote_shutdown(void);
342 int		 carp_demote_get(char *);
343 int		 carp_demote_set(char *, int);
344 
345 /* printconf.c */
346 void		 print_config(struct ripd_conf *);
347 
348 /* logmsg.c */
349 const char	*nbr_state_name(int);
350 const char	*if_type_name(enum iface_type);
351 const char	*if_auth_name(enum auth_type);
352 const char	*if_state_name(int);
353 
354 /* interface.c */
355 struct iface	*if_find_index(u_short);
356 
357 /* name2id.c */
358 u_int16_t	 rtlabel_name2id(const char *);
359 const char	*rtlabel_id2name(u_int16_t);
360 void		 rtlabel_unref(u_int16_t);
361 
362 #endif /* RIPD_H */
363