xref: /openbsd/usr.sbin/ldpd/keepalive.c (revision b7b4db73)
1*b7b4db73Srenato /*	$OpenBSD: keepalive.c,v 1.13 2016/05/23 15:14:07 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 <sys/socket.h>
21ab0c2486Smichele #include <sys/uio.h>
22ab0c2486Smichele 
23ab0c2486Smichele #include <netinet/in.h>
24ab0c2486Smichele #include <netinet/ip.h>
25ab0c2486Smichele #include <arpa/inet.h>
26ab0c2486Smichele #include <net/if_dl.h>
27ab0c2486Smichele #include <unistd.h>
28ab0c2486Smichele 
29ab0c2486Smichele #include <errno.h>
30ab0c2486Smichele #include <event.h>
31ab0c2486Smichele #include <stdlib.h>
32ab0c2486Smichele #include <string.h>
33ab0c2486Smichele 
34ab0c2486Smichele #include "ldpd.h"
35ab0c2486Smichele #include "ldp.h"
36ab0c2486Smichele #include "log.h"
37ab0c2486Smichele #include "ldpe.h"
38ab0c2486Smichele 
39a6fc12d8Smichele void
40ab0c2486Smichele send_keepalive(struct nbr *nbr)
41ab0c2486Smichele {
42e39620e5Snicm 	struct ibuf	*buf;
43ab0c2486Smichele 	u_int16_t	 size;
44ab0c2486Smichele 
45e39620e5Snicm 	if ((buf = ibuf_open(LDP_MAX_LEN)) == NULL)
46*b7b4db73Srenato 		fatal(__func__);
47ab0c2486Smichele 
48ab0c2486Smichele 	size = LDP_HDR_SIZE + sizeof(struct ldp_msg);
49ab0c2486Smichele 
50122f143eSclaudio 	gen_ldp_hdr(buf, size);
51ab0c2486Smichele 
52ab0c2486Smichele 	size -= LDP_HDR_SIZE;
53ab0c2486Smichele 
54ab0c2486Smichele 	gen_msg_tlv(buf, MSG_TYPE_KEEPALIVE, size);
55ab0c2486Smichele 
56699b7d06Sclaudio 	evbuf_enqueue(&nbr->tcp->wbuf, buf);
57ab0c2486Smichele }
58ab0c2486Smichele 
59ab0c2486Smichele int
60ab0c2486Smichele recv_keepalive(struct nbr *nbr, char *buf, u_int16_t len)
61ab0c2486Smichele {
6289f23408Sclaudio 	struct ldp_msg ka;
63ab0c2486Smichele 
6489f23408Sclaudio 	bcopy(buf, &ka, sizeof(ka));
65ab0c2486Smichele 
6689f23408Sclaudio 	if (len != LDP_MSG_LEN) {
6789f23408Sclaudio 		session_shutdown(nbr, S_BAD_MSG_LEN, ka.msgid, ka.type);
68ab0c2486Smichele 		return (-1);
69ab0c2486Smichele 	}
70ab0c2486Smichele 
71ab0c2486Smichele 	if (nbr->state != NBR_STA_OPER)
72ab0c2486Smichele 		nbr_fsm(nbr, NBR_EVT_KEEPALIVE_RCVD);
73ab0c2486Smichele 
7489f23408Sclaudio 	return (ntohs(ka.length));
75ab0c2486Smichele }
76