xref: /openbsd/usr.sbin/ldpd/notification.c (revision a6445c1d)
1 /*	$OpenBSD: notification.c,v 1.17 2014/10/25 03:23:49 lteo Exp $ */
2 
3 /*
4  * Copyright (c) 2009 Michele Marchetto <michele@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <sys/uio.h>
22 
23 #include <netinet/in.h>
24 #include <netinet/ip.h>
25 #include <arpa/inet.h>
26 #include <net/if_dl.h>
27 #include <unistd.h>
28 
29 #include <errno.h>
30 #include <event.h>
31 #include <stdlib.h>
32 #include <string.h>
33 
34 #include "ldpd.h"
35 #include "ldp.h"
36 #include "log.h"
37 #include "ldpe.h"
38 
39 int	gen_status_tlv(struct ibuf *, u_int32_t, u_int32_t, u_int32_t);
40 
41 void
42 send_notification_nbr(struct nbr *nbr, u_int32_t status, u_int32_t msgid,
43     u_int32_t type)
44 {
45 	log_debug("send_notification_nbr: nbr ID %s, status %s",
46 	    inet_ntoa(nbr->id), notification_name(status));
47 	send_notification(status, nbr->tcp, msgid, type);
48 	nbr_fsm(nbr, NBR_EVT_PDU_SENT);
49 }
50 
51 void
52 send_notification(u_int32_t status, struct tcp_conn *tcp, u_int32_t msgid,
53     u_int32_t type)
54 {
55 	struct ibuf	*buf;
56 	u_int16_t	 size;
57 
58 	if ((buf = ibuf_open(LDP_MAX_LEN)) == NULL)
59 		fatal("send_notification");
60 
61 	size = LDP_HDR_SIZE + sizeof(struct ldp_msg) + STATUS_SIZE;
62 
63 	gen_ldp_hdr(buf, size);
64 
65 	size -= LDP_HDR_SIZE;
66 
67 	gen_msg_tlv(buf, MSG_TYPE_NOTIFICATION, size);
68 
69 	size -= sizeof(struct ldp_msg);
70 
71 	gen_status_tlv(buf, status, msgid, type);
72 
73 	evbuf_enqueue(&tcp->wbuf, buf);
74 }
75 
76 int
77 recv_notification(struct nbr *nbr, char *buf, u_int16_t len)
78 {
79 	struct ldp_msg		not;
80 	struct status_tlv	st;
81 
82 	log_debug("recv_notification: neighbor ID %s", inet_ntoa(nbr->id));
83 
84 	bcopy(buf, &not, sizeof(not));
85 
86 	buf += sizeof(struct ldp_msg);
87 	len -= sizeof(struct ldp_msg);
88 
89 	if (len < STATUS_SIZE) {
90 		session_shutdown(nbr, S_BAD_MSG_LEN, not.msgid, not.type);
91 		return (-1);
92 	}
93 	bcopy(buf, &st, sizeof(st));
94 
95 	if (ntohs(st.length) > STATUS_SIZE - TLV_HDR_LEN ||
96 	    ntohs(st.length) > len - TLV_HDR_LEN) {
97 		session_shutdown(nbr, S_BAD_TLV_LEN, not.msgid, not.type);
98 		return (-1);
99 	}
100 
101 	/* TODO optional parameters: ext status, returned PDU and msg */
102 
103 	if (st.status_code & htonl(STATUS_FATAL))
104 		log_warnx("received notification from neighbor %s: %s",
105 		    inet_ntoa(nbr->id),
106 		    notification_name(ntohl(st.status_code)));
107 	else
108 		log_debug("received non-fatal notification from neighbor "
109 		    "%s: %s", inet_ntoa(nbr->id),
110 		    notification_name(ntohl(st.status_code)));
111 
112 	if (st.status_code & htonl(STATUS_FATAL)) {
113 		if (st.status_code == htonl(S_NO_HELLO) ||
114 		    st.status_code == htonl(S_PARM_ADV_MODE) ||
115 		    st.status_code == htonl(S_MAX_PDU_LEN) ||
116 		    st.status_code == htonl(S_PARM_L_RANGE) ||
117 		    st.status_code == htonl(S_KEEPALIVE_BAD))
118 			nbr_start_idtimer(nbr);
119 
120 		nbr_fsm(nbr, NBR_EVT_CLOSE_SESSION);
121 		return (-1);
122 	}
123 	/* XXX in some cases we should inform the RDE about non-fatal ones */
124 
125 	return (ntohs(not.length));
126 }
127 
128 int
129 gen_status_tlv(struct ibuf *buf, u_int32_t status, u_int32_t msgid,
130     u_int32_t type)
131 {
132 	struct status_tlv	st;
133 
134 	bzero(&st, sizeof(st));
135 
136 	st.type = htons(TLV_TYPE_STATUS);
137 	st.length = htons(STATUS_TLV_LEN);
138 	st.status_code = htonl(status);
139 
140 	st.msg_id = msgid;
141 	st.msg_type = type;
142 
143 	return (ibuf_add(buf, &st, STATUS_SIZE));
144 }
145