1 /* $OpenBSD: notification.c,v 1.16 2013/10/15 20:31:13 renato 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/in_systm.h> 25 #include <netinet/ip.h> 26 #include <arpa/inet.h> 27 #include <net/if_dl.h> 28 #include <unistd.h> 29 30 #include <errno.h> 31 #include <event.h> 32 #include <stdlib.h> 33 #include <string.h> 34 35 #include "ldpd.h" 36 #include "ldp.h" 37 #include "log.h" 38 #include "ldpe.h" 39 40 int gen_status_tlv(struct ibuf *, u_int32_t, u_int32_t, u_int32_t); 41 42 void 43 send_notification_nbr(struct nbr *nbr, u_int32_t status, u_int32_t msgid, 44 u_int32_t type) 45 { 46 log_debug("send_notification_nbr: nbr ID %s, status %s", 47 inet_ntoa(nbr->id), notification_name(status)); 48 send_notification(status, nbr->tcp, msgid, type); 49 nbr_fsm(nbr, NBR_EVT_PDU_SENT); 50 } 51 52 void 53 send_notification(u_int32_t status, struct tcp_conn *tcp, u_int32_t msgid, 54 u_int32_t type) 55 { 56 struct ibuf *buf; 57 u_int16_t size; 58 59 if ((buf = ibuf_open(LDP_MAX_LEN)) == NULL) 60 fatal("send_notification"); 61 62 size = LDP_HDR_SIZE + sizeof(struct ldp_msg) + STATUS_SIZE; 63 64 gen_ldp_hdr(buf, size); 65 66 size -= LDP_HDR_SIZE; 67 68 gen_msg_tlv(buf, MSG_TYPE_NOTIFICATION, size); 69 70 size -= sizeof(struct ldp_msg); 71 72 gen_status_tlv(buf, status, msgid, type); 73 74 evbuf_enqueue(&tcp->wbuf, buf); 75 } 76 77 int 78 recv_notification(struct nbr *nbr, char *buf, u_int16_t len) 79 { 80 struct ldp_msg not; 81 struct status_tlv st; 82 83 log_debug("recv_notification: neighbor ID %s", inet_ntoa(nbr->id)); 84 85 bcopy(buf, ¬, sizeof(not)); 86 87 buf += sizeof(struct ldp_msg); 88 len -= sizeof(struct ldp_msg); 89 90 if (len < STATUS_SIZE) { 91 session_shutdown(nbr, S_BAD_MSG_LEN, not.msgid, not.type); 92 return (-1); 93 } 94 bcopy(buf, &st, sizeof(st)); 95 96 if (ntohs(st.length) > STATUS_SIZE - TLV_HDR_LEN || 97 ntohs(st.length) > len - TLV_HDR_LEN) { 98 session_shutdown(nbr, S_BAD_TLV_LEN, not.msgid, not.type); 99 return (-1); 100 } 101 102 /* TODO optional parameters: ext status, returned PDU and msg */ 103 104 if (st.status_code & htonl(STATUS_FATAL)) 105 log_warnx("received notification from neighbor %s: %s", 106 inet_ntoa(nbr->id), 107 notification_name(ntohl(st.status_code))); 108 else 109 log_debug("received non-fatal notification from neighbor " 110 "%s: %s", inet_ntoa(nbr->id), 111 notification_name(ntohl(st.status_code))); 112 113 if (st.status_code & htonl(STATUS_FATAL)) { 114 if (st.status_code == htonl(S_NO_HELLO) || 115 st.status_code == htonl(S_PARM_ADV_MODE) || 116 st.status_code == htonl(S_MAX_PDU_LEN) || 117 st.status_code == htonl(S_PARM_L_RANGE) || 118 st.status_code == htonl(S_KEEPALIVE_BAD)) 119 nbr_start_idtimer(nbr); 120 121 nbr_fsm(nbr, NBR_EVT_CLOSE_SESSION); 122 return (-1); 123 } 124 /* XXX in some cases we should inform the RDE about non-fatal ones */ 125 126 return (ntohs(not.length)); 127 } 128 129 int 130 gen_status_tlv(struct ibuf *buf, u_int32_t status, u_int32_t msgid, 131 u_int32_t type) 132 { 133 struct status_tlv st; 134 135 bzero(&st, sizeof(st)); 136 137 st.type = htons(TLV_TYPE_STATUS); 138 st.length = htons(STATUS_TLV_LEN); 139 st.status_code = htonl(status); 140 141 st.msg_id = msgid; 142 st.msg_type = type; 143 144 return (ibuf_add(buf, &st, STATUS_SIZE)); 145 } 146