xref: /openbsd/usr.sbin/ldpd/keepalive.c (revision 60e1e0e7)
1*60e1e0e7Srenato /*	$OpenBSD: keepalive.c,v 1.17 2016/07/01 23:36:38 renato Exp $ */
2ab0c2486Smichele 
3ab0c2486Smichele /*
4ab0c2486Smichele  * Copyright (c) 2009 Michele Marchetto <michele@openbsd.org>
5ab0c2486Smichele  *
6ab0c2486Smichele  * Permission to use, copy, modify, and distribute this software for any
7ab0c2486Smichele  * purpose with or without fee is hereby granted, provided that the above
8ab0c2486Smichele  * copyright notice and this permission notice appear in all copies.
9ab0c2486Smichele  *
10ab0c2486Smichele  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11ab0c2486Smichele  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12ab0c2486Smichele  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13ab0c2486Smichele  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14ab0c2486Smichele  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15ab0c2486Smichele  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16ab0c2486Smichele  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17ab0c2486Smichele  */
18ab0c2486Smichele 
19ab0c2486Smichele #include <sys/types.h>
20ab0c2486Smichele #include <string.h>
21ab0c2486Smichele 
22ab0c2486Smichele #include "ldpd.h"
23ab0c2486Smichele #include "ldpe.h"
245411bbb6Srenato #include "log.h"
25ab0c2486Smichele 
26a6fc12d8Smichele void
send_keepalive(struct nbr * nbr)27ab0c2486Smichele send_keepalive(struct nbr *nbr)
28ab0c2486Smichele {
29e39620e5Snicm 	struct ibuf	*buf;
303de94509Srenato 	uint16_t	 size;
31ab0c2486Smichele 
329277622bSrenato 	size = LDP_HDR_SIZE + LDP_MSG_SIZE;
339277622bSrenato 	if ((buf = ibuf_open(size)) == NULL)
34b7b4db73Srenato 		fatal(__func__);
35ab0c2486Smichele 
36122f143eSclaudio 	gen_ldp_hdr(buf, size);
37ab0c2486Smichele 	size -= LDP_HDR_SIZE;
389277622bSrenato 	gen_msg_hdr(buf, MSG_TYPE_KEEPALIVE, size);
39ab0c2486Smichele 
40699b7d06Sclaudio 	evbuf_enqueue(&nbr->tcp->wbuf, buf);
41ab0c2486Smichele }
42ab0c2486Smichele 
43ab0c2486Smichele int
recv_keepalive(struct nbr * nbr,char * buf,uint16_t len)443de94509Srenato recv_keepalive(struct nbr *nbr, char *buf, uint16_t len)
45ab0c2486Smichele {
46*60e1e0e7Srenato 	struct ldp_msg msg;
47ab0c2486Smichele 
48*60e1e0e7Srenato 	memcpy(&msg, buf, sizeof(msg));
493de94509Srenato 	if (len != LDP_MSG_SIZE) {
50*60e1e0e7Srenato 		session_shutdown(nbr, S_BAD_MSG_LEN, msg.id, msg.type);
51ab0c2486Smichele 		return (-1);
52ab0c2486Smichele 	}
53ab0c2486Smichele 
54ab0c2486Smichele 	if (nbr->state != NBR_STA_OPER)
55ab0c2486Smichele 		nbr_fsm(nbr, NBR_EVT_KEEPALIVE_RCVD);
56ab0c2486Smichele 
579277622bSrenato 	return (0);
58ab0c2486Smichele }
59