GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.sbin/bgpd/logmsg.c Lines: 0 100 0.0 %
Date: 2017-11-13 Branches: 0 69 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: logmsg.c,v 1.3 2017/05/28 20:14:15 claudio Exp $ */
2
3
/*
4
 * Copyright (c) 2003, 2004 Henning Brauer <henning@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 <errno.h>
20
#include <stdarg.h>
21
#include <stdio.h>
22
#include <stdlib.h>
23
#include <string.h>
24
#include <sys/types.h>
25
#include <syslog.h>
26
27
#include "bgpd.h"
28
#include "session.h"
29
#include "log.h"
30
31
char *
32
log_fmt_peer(const struct peer_config *peer)
33
{
34
	const char	*ip;
35
	char		*pfmt, *p;
36
37
	ip = log_addr(&peer->remote_addr);
38
	if ((peer->remote_addr.aid == AID_INET && peer->remote_masklen != 32) ||
39
	    (peer->remote_addr.aid == AID_INET6 &&
40
	    peer->remote_masklen != 128)) {
41
		if (asprintf(&p, "%s/%u", ip, peer->remote_masklen) == -1)
42
			fatal(NULL);
43
	} else {
44
		if ((p = strdup(ip)) == NULL)
45
			fatal(NULL);
46
	}
47
48
	if (peer->descr[0]) {
49
		if (asprintf(&pfmt, "neighbor %s (%s)", p, peer->descr) ==
50
		    -1)
51
			fatal(NULL);
52
	} else {
53
		if (asprintf(&pfmt, "neighbor %s", p) == -1)
54
			fatal(NULL);
55
	}
56
	free(p);
57
	return (pfmt);
58
}
59
60
void
61
log_peer_info(const struct peer_config *peer, const char *emsg, ...)
62
{
63
	char	*p, *nfmt;
64
	va_list	 ap;
65
66
	p = log_fmt_peer(peer);
67
	if (asprintf(&nfmt, "%s: %s", p, emsg) == -1)
68
		fatal(NULL);
69
	va_start(ap, emsg);
70
	vlog(LOG_INFO, nfmt, ap);
71
	va_end(ap);
72
	free(p);
73
	free(nfmt);
74
}
75
76
void
77
log_peer_warn(const struct peer_config *peer, const char *emsg, ...)
78
{
79
	char	*p, *nfmt;
80
	va_list	 ap;
81
82
	p = log_fmt_peer(peer);
83
	if (emsg == NULL) {
84
		if (asprintf(&nfmt, "%s: %s", p, strerror(errno)) == -1)
85
			fatal(NULL);
86
	} else {
87
		if (asprintf(&nfmt, "%s: %s: %s", p, emsg, strerror(errno)) ==
88
		    -1)
89
			fatal(NULL);
90
	}
91
	va_start(ap, emsg);
92
	vlog(LOG_ERR, nfmt, ap);
93
	va_end(ap);
94
	free(p);
95
	free(nfmt);
96
}
97
98
void
99
log_peer_warnx(const struct peer_config *peer, const char *emsg, ...)
100
{
101
	char	*p, *nfmt;
102
	va_list	 ap;
103
104
	p = log_fmt_peer(peer);
105
	if (asprintf(&nfmt, "%s: %s", p, emsg) == -1)
106
		fatal(NULL);
107
	va_start(ap, emsg);
108
	vlog(LOG_ERR, nfmt, ap);
109
	va_end(ap);
110
	free(p);
111
	free(nfmt);
112
}
113
114
void
115
log_statechange(struct peer *peer, enum session_state nstate,
116
    enum session_events event)
117
{
118
	char	*p;
119
120
	/* don't clutter the logs with constant Connect -> Active -> Connect */
121
	if (nstate == STATE_CONNECT && peer->state == STATE_ACTIVE &&
122
	    peer->prev_state == STATE_CONNECT)
123
		return;
124
	if (nstate == STATE_ACTIVE && peer->state == STATE_CONNECT &&
125
	    peer->prev_state == STATE_ACTIVE)
126
		return;
127
128
	peer->lasterr = 0;
129
	p = log_fmt_peer(&peer->conf);
130
	logit(LOG_INFO, "%s: state change %s -> %s, reason: %s",
131
	    p, statenames[peer->state], statenames[nstate], eventnames[event]);
132
	free(p);
133
}
134
135
void
136
log_notification(const struct peer *peer, u_int8_t errcode, u_int8_t subcode,
137
    u_char *data, u_int16_t datalen, const char *dir)
138
{
139
	char		*p;
140
	const char	*suberrname = NULL;
141
	int		 uk = 0;
142
143
	p = log_fmt_peer(&peer->conf);
144
	switch (errcode) {
145
	case ERR_HEADER:
146
		if (subcode >= sizeof(suberr_header_names)/sizeof(char *))
147
			uk = 1;
148
		else
149
			suberrname = suberr_header_names[subcode];
150
		break;
151
	case ERR_OPEN:
152
		if (subcode >= sizeof(suberr_open_names)/sizeof(char *))
153
			uk = 1;
154
		else
155
			suberrname = suberr_open_names[subcode];
156
		break;
157
	case ERR_UPDATE:
158
		if (subcode >= sizeof(suberr_update_names)/sizeof(char *))
159
			uk = 1;
160
		else
161
			suberrname = suberr_update_names[subcode];
162
		break;
163
	case ERR_CEASE:
164
		if (subcode >= sizeof(suberr_cease_names)/sizeof(char *))
165
			uk = 1;
166
		else
167
			suberrname = suberr_cease_names[subcode];
168
		break;
169
	case ERR_HOLDTIMEREXPIRED:
170
		if (subcode != 0)
171
			uk = 1;
172
		break;
173
	case ERR_FSM:
174
		if (subcode >= sizeof(suberr_fsm_names)/sizeof(char *))
175
			uk = 1;
176
		else
177
			suberrname = suberr_fsm_names[subcode];
178
		break;
179
	default:
180
		logit(LOG_ERR, "%s: %s notification, unknown errcode "
181
		    "%u, subcode %u", p, dir, errcode, subcode);
182
		free(p);
183
		return;
184
	}
185
186
	if (uk)
187
		logit(LOG_ERR, "%s: %s notification: %s, unknown subcode %u",
188
		    p, dir, errnames[errcode], subcode);
189
	else {
190
		if (suberrname == NULL)
191
			logit(LOG_ERR, "%s: %s notification: %s", p,
192
			    dir, errnames[errcode]);
193
		else
194
			logit(LOG_ERR, "%s: %s notification: %s, %s",
195
			    p, dir, errnames[errcode], suberrname);
196
	}
197
	free(p);
198
}
199
200
void
201
log_conn_attempt(const struct peer *peer, struct sockaddr *sa)
202
{
203
	char		*p;
204
	const char	*b;
205
206
	if (peer == NULL) {	/* connection from non-peer, drop */
207
		b = log_sockaddr(sa);
208
		logit(LOG_INFO, "connection from non-peer %s refused", b);
209
	} else {
210
		/* only log if there is a chance that the session may come up */
211
		if (peer->conf.down && peer->state == STATE_IDLE)
212
			return;
213
		p = log_fmt_peer(&peer->conf);
214
		logit(LOG_INFO, "Connection attempt from %s while session is "
215
		    "in state %s", p, statenames[peer->state]);
216
		free(p);
217
	}
218
}