xref: /openbsd-src/usr.sbin/ripd/ripd.h (revision 2b0358df1d88d06ef4139321dd05bd5e05d91eaf)
1 /*	$OpenBSD: ripd.h,v 1.12 2009/03/24 19:26:13 michele 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 #define	CONF_FILE		"/etc/ripd.conf"
32 #define	RIPD_SOCKET		"/var/run/ripd.sock"
33 #define	RIPD_USER		"_ripd"
34 
35 #define	NBR_HASHSIZE		128
36 #define	NBR_IDSELF		1
37 #define	NBR_CNTSTART		(NBR_IDSELF + 1)
38 
39 #define	ROUTE_TIMEOUT		180
40 #define ROUTE_GARBAGE		120
41 
42 #define	NBR_TIMEOUT		180
43 
44 #define	READ_BUF_SIZE		65535
45 #define RT_BUF_SIZE		16384
46 #define MAX_RTSOCK_BUF		128 * 1024
47 
48 #define	RIPD_FLAG_NO_FIB_UPDATE	0x0001
49 
50 #define	F_RIPD_INSERTED		0x0001
51 #define	F_KERNEL		0x0002
52 #define	F_BGPD_INSERTED		0x0004
53 #define	F_CONNECTED		0x0008
54 #define	F_DOWN			0x0010
55 #define	F_STATIC		0x0020
56 #define	F_DYNAMIC		0x0040
57 #define	F_OSPFD_INSERTED	0x0080
58 #define	F_REDISTRIBUTED		0x0100
59 
60 #define REDISTRIBUTE_ON		0x01
61 
62 #define	OPT_SPLIT_HORIZON	0x01
63 #define	OPT_SPLIT_POISONED	0x02
64 #define	OPT_TRIGGERED_UPDATES	0x04
65 
66 /* buffer */
67 struct buf {
68 	TAILQ_ENTRY(buf)	 entry;
69 	u_char			*buf;
70 	size_t			 size;
71 	size_t			 max;
72 	size_t			 wpos;
73 	size_t			 rpos;
74 };
75 
76 struct msgbuf {
77 	TAILQ_HEAD(, buf)	 bufs;
78 	u_int32_t		 queued;
79 	int			 fd;
80 };
81 
82 #define IMSG_HEADER_SIZE	sizeof(struct imsg_hdr)
83 #define MAX_IMSGSIZE		8192
84 
85 struct buf_read {
86 	u_char			 buf[READ_BUF_SIZE];
87 	u_char			*rptr;
88 	size_t			 wpos;
89 };
90 
91 struct imsgbuf {
92 	TAILQ_HEAD(, imsg_fd)	fds;
93 	struct buf_read		r;
94 	struct msgbuf		w;
95 	struct event		ev;
96 	void			(*handler)(int, short, void *);
97 	int			fd;
98 	pid_t			pid;
99 	short			events;
100 };
101 
102 enum imsg_type {
103 	IMSG_NONE,
104 	IMSG_CTL_END,
105 	IMSG_CTL_RELOAD,
106 	IMSG_CTL_IFINFO,
107 	IMSG_IFINFO,
108 	IMSG_CTL_FIB_COUPLE,
109 	IMSG_CTL_FIB_DECOUPLE,
110 	IMSG_CTL_KROUTE,
111 	IMSG_CTL_KROUTE_ADDR,
112 	IMSG_CTL_SHOW_INTERFACE,
113 	IMSG_CTL_SHOW_IFACE,
114 	IMSG_CTL_SHOW_NBR,
115 	IMSG_CTL_SHOW_RIB,
116 	IMSG_KROUTE_CHANGE,
117 	IMSG_KROUTE_DELETE,
118 	IMSG_KROUTE_GET,
119 	IMSG_NETWORK_ADD,
120 	IMSG_NETWORK_DEL,
121 	IMSG_ROUTE_FEED,
122 	IMSG_RESPONSE_ADD,
123 	IMSG_SEND_RESPONSE,
124 	IMSG_FULL_RESPONSE,
125 	IMSG_ROUTE_REQUEST,
126 	IMSG_ROUTE_REQUEST_END,
127 	IMSG_FULL_REQUEST,
128 	IMSG_REQUEST_ADD,
129 	IMSG_SEND_REQUEST,
130 	IMSG_SEND_TRIGGERED_UPDATE,
131 	IMSG_DEMOTE
132 };
133 
134 struct imsg_hdr {
135 	enum imsg_type	type;
136 	u_int16_t	len;
137 	u_int32_t	peerid;
138 	pid_t		pid;
139 };
140 
141 struct imsg {
142 	struct imsg_hdr	 hdr;
143 	void		*data;
144 };
145 
146 struct imsg_fd {
147 	TAILQ_ENTRY(imsg_fd)	entry;
148 	int			fd;
149 };
150 
151 /* interface states */
152 #define IF_STA_DOWN		0x01
153 #define IF_STA_ACTIVE		(~IF_STA_DOWN)
154 #define IF_STA_ANY		0x7f
155 
156 /* interface events */
157 enum iface_event {
158 	IF_EVT_NOTHING,
159 	IF_EVT_UP,
160 	IF_EVT_DOWN
161 };
162 
163 /* interface actions */
164 enum iface_action {
165 	IF_ACT_NOTHING,
166 	IF_ACT_STRT,
167 	IF_ACT_RST
168 };
169 
170 /* interface types */
171 enum iface_type {
172 	IF_TYPE_POINTOPOINT,
173 	IF_TYPE_BROADCAST,
174 	IF_TYPE_NBMA,
175 	IF_TYPE_POINTOMULTIPOINT
176 };
177 
178 /* neighbor states */
179 #define NBR_STA_DOWN		0x01
180 #define	NBR_STA_REQ_RCVD	0x02
181 #define NBR_STA_ACTIVE		(~NBR_STA_DOWN)
182 #define NBR_STA_ANY		0xff
183 
184 struct auth_md {
185 	TAILQ_ENTRY(auth_md)	 entry;
186 	u_int32_t		 seq_modulator;
187 	u_int8_t		 key[MD5_DIGEST_LENGTH];
188 	u_int8_t		 keyid;
189 };
190 
191 #define MAX_SIMPLE_AUTH_LEN	16
192 
193 /* auth types */
194 enum auth_type {
195 	AUTH_NONE = 1,
196 	AUTH_SIMPLE,
197 	AUTH_CRYPT
198 };
199 
200 TAILQ_HEAD(auth_md_head, auth_md);
201 TAILQ_HEAD(packet_head, packet_entry);
202 
203 struct iface {
204 	LIST_ENTRY(iface)	 entry;
205 	LIST_HEAD(, nbr)	 nbr_list;
206 	LIST_HEAD(, nbr_failed)	 failed_nbr_list;
207 	char			 name[IF_NAMESIZE];
208 	char			 demote_group[IFNAMSIZ];
209 	u_int8_t		 auth_key[MAX_SIMPLE_AUTH_LEN];
210 	struct in_addr		 addr;
211 	struct in_addr		 dst;
212 	struct in_addr		 mask;
213 	struct packet_head	 rq_list;
214 	struct packet_head	 rp_list;
215 	struct auth_md_head	 auth_md_list;
216 
217 	u_int64_t		 baudrate;
218 	time_t			 uptime;
219 	u_int			 mtu;
220 	int			 fd; /* XXX */
221 	int			 state;
222 	u_short			 ifindex;
223 	u_int16_t		 cost;
224 	u_int16_t		 flags;
225 	enum iface_type		 type;
226 	enum auth_type		 auth_type;
227 	u_int8_t		 linktype;
228 	u_int8_t		 media_type;
229 	u_int8_t		 passive;
230 	u_int8_t		 linkstate;
231 	u_int8_t		 auth_keyid;
232 };
233 
234 struct rip_route {
235 	struct in_addr		 address;
236 	struct in_addr		 mask;
237 	struct in_addr		 nexthop;
238 	int			 refcount;
239 	u_short			 ifindex;
240 	u_int8_t		 metric;
241 };
242 
243 struct packet_entry {
244 	TAILQ_ENTRY(packet_entry)	 entry;
245 	struct rip_route		*rr;
246 };
247 
248 enum {
249 	PROC_MAIN,
250 	PROC_RIP_ENGINE,
251 	PROC_RDE_ENGINE
252 } ripd_process;
253 
254 #define	REDIST_CONNECTED	0x01
255 #define	REDIST_STATIC		0x02
256 #define	REDIST_LABEL		0x04
257 #define	REDIST_ADDR		0x08
258 #define	REDIST_DEFAULT		0x10
259 #define	REDIST_NO		0x20
260 
261 struct redistribute {
262 	SIMPLEQ_ENTRY(redistribute)	entry;
263 	struct in_addr			addr;
264 	struct in_addr			mask;
265 	u_int32_t			metric;
266 	u_int16_t			label;
267 	u_int16_t			type;
268 };
269 
270 struct ripd_conf {
271 	struct event		 ev;
272 	struct event		 report_timer;
273 	LIST_HEAD(, iface)	 iface_list;
274 	SIMPLEQ_HEAD(, redistribute) redist_list;
275 
276 	u_int32_t		 opts;
277 #define RIPD_OPT_VERBOSE	0x00000001
278 #define	RIPD_OPT_VERBOSE2	0x00000002
279 #define	RIPD_OPT_NOACTION	0x00000004
280 #define	RIPD_OPT_FORCE_DEMOTE	0x00000008
281 	int			 flags;
282 	int			 options;
283 	int			 rip_socket;
284 	int			 redistribute;
285 };
286 
287 /* kroute */
288 struct kroute {
289 	struct in_addr	prefix;
290 	struct in_addr	netmask;
291 	struct in_addr	nexthop;
292 	u_int16_t	flags;
293 	u_int16_t	rtlabel;
294 	u_short		ifindex;
295 	u_int8_t	metric;
296 };
297 
298 struct kif {
299 	char		 ifname[IF_NAMESIZE];
300 	u_int64_t	 baudrate;
301 	int		 flags;
302 	int		 mtu;
303 	u_short		 ifindex;
304 	u_int8_t	 media_type;
305 	u_int8_t	 link_state;
306 	u_int8_t	 nh_reachable;	/* for nexthop verification */
307 };
308 
309 /* control data structures */
310 struct ctl_iface {
311 	char			 name[IF_NAMESIZE];
312 	struct in_addr		 addr;
313 	struct in_addr		 mask;
314 
315 	time_t			 uptime;
316 	time_t			 report_timer;
317 
318 	u_int64_t		 baudrate;
319 	unsigned int		 ifindex;
320 	int			 state;
321 	int			 mtu;
322 
323 	u_int16_t		 flags;
324 	u_int16_t		 metric;
325 	enum iface_type		 type;
326 	u_int8_t		 linkstate;
327 	u_int8_t		 mediatype;
328 	u_int8_t		 passive;
329 };
330 
331 struct ctl_rt {
332 	struct in_addr		 prefix;
333 	struct in_addr		 netmask;
334 	struct in_addr		 nexthop;
335 	time_t			 uptime;
336 	time_t			 expire;
337 	u_int32_t		 metric;
338 	u_int16_t		 flags;
339 };
340 
341 struct ctl_nbr {
342 	char			 name[IF_NAMESIZE];
343 	struct in_addr		 id;
344 	struct in_addr		 addr;
345 	time_t			 dead_timer;
346 	time_t			 uptime;
347 	int			 nbr_state;
348 	int			 iface_state;
349 };
350 
351 struct demote_msg {
352 	char			 demote_group[IF_NAMESIZE];
353 	int			 level;
354 };
355 
356 int		 kif_init(void);
357 int		 kr_init(int);
358 int		 kr_change(struct kroute *);
359 int		 kr_delete(struct kroute *);
360 void		 kr_shutdown(void);
361 void		 kr_fib_couple(void);
362 void		 kr_fib_decouple(void);
363 void		 kr_dispatch_msg(int, short, void *);
364 void		 kr_show_route(struct imsg *);
365 void		 kr_ifinfo(char *, pid_t);
366 struct kif	*kif_findname(char *);
367 
368 in_addr_t	 prefixlen2mask(u_int8_t);
369 u_int8_t	 mask2prefixlen(in_addr_t);
370 
371 /* ripd.c */
372 void		 main_imsg_compose_ripe(int, pid_t, void *, u_int16_t);
373 void		 main_imsg_compose_rde(int, pid_t, void *, u_int16_t);
374 int		 rip_redistribute(struct kroute *);
375 
376 /* parse.y */
377 struct ripd_conf	*parse_config(char *, int);
378 int			 cmdline_symset(char *);
379 
380 /* buffer.c */
381 struct buf	*buf_open(size_t);
382 struct buf	*buf_dynamic(size_t, size_t);
383 int		 buf_add(struct buf *, void *, size_t);
384 void		*buf_reserve(struct buf *, size_t);
385 void		*buf_seek(struct buf *, size_t, size_t);
386 int		 buf_close(struct msgbuf *, struct buf *);
387 void		 buf_free(struct buf *);
388 void		 msgbuf_init(struct msgbuf *);
389 void		 msgbuf_clear(struct msgbuf *);
390 int		 msgbuf_write(struct msgbuf *);
391 
392 /* carp.c */
393 int		 carp_demote_init(char *, int);
394 void		 carp_demote_shutdown(void);
395 int		 carp_demote_get(char *);
396 int		 carp_demote_set(char *, int);
397 
398 /* imsg.c */
399 void		 imsg_init(struct imsgbuf *, int, void (*)(int, short, void *));
400 ssize_t		 imsg_read(struct imsgbuf *);
401 ssize_t		 imsg_get(struct imsgbuf *, struct imsg *);
402 int		 imsg_compose(struct imsgbuf *, enum imsg_type, u_int32_t,
403 		    pid_t, void *, u_int16_t);
404 struct buf	*imsg_create(struct imsgbuf *, enum imsg_type, u_int32_t, pid_t,
405 		    u_int16_t);
406 int		 imsg_add(struct buf *, void *, u_int16_t);
407 int		 imsg_close(struct imsgbuf *, struct buf *);
408 void		 imsg_free(struct imsg *);
409 void		 imsg_event_add(struct imsgbuf *);
410 
411 /* printconf.c */
412 void		 print_config(struct ripd_conf *);
413 
414 /* log.c */
415 const char	*if_state_name(int);
416 const char	*if_auth_name(enum auth_type);
417 const char	*nbr_state_name(int);
418 
419 /* interface.c */
420 struct iface	*if_find_index(u_short);
421 
422 /* name2id.c */
423 u_int16_t	 rtlabel_name2id(const char *);
424 const char	*rtlabel_id2name(u_int16_t);
425 void		 rtlabel_unref(u_int16_t);
426 
427 #endif /* _RIPD_H_ */
428