summaryrefslogtreecommitdiff
path: root/usr.sbin/npppd/l2tp/l2tp_subr.c
blob: da1a0fe8c82f975acb3e01e728d740ba55914e0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*	$OpenBSD: l2tp_subr.c,v 1.4 2012/05/08 13:15:11 yasuoka Exp $	*/

/*-
 * Copyright (c) 2009 Internet Initiative Japan Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
/* $Id: l2tp_subr.c,v 1.4 2012/05/08 13:15:11 yasuoka Exp $ */
/**@file L2TP related sub-routines */
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <stdio.h>
#include <syslog.h>
#include <string.h>
#include <event.h>

#ifdef USE_LIBSOCKUTIL
#include <seil/sockfromto.h>
#endif

#include "debugutil.h"
#include "hash.h"
#include "bytebuf.h"
#include "slist.h"
#include "l2tp.h"
#include "l2tp_subr.h"
#include "l2tp_local.h"

#ifdef	L2TP_SUBR_DEBUG
#define	L2TP_SUBR_ASSERT(x)	ASSERT(x)
#else
#define	L2TP_SUBR_ASSERT(x)
#endif

/*
 * AVP
 */
int
avp_enum(struct l2tp_avp *avp, const u_char *pkt, int pktlen, int filldata)
{
	uint16_t flags;

	L2TP_SUBR_ASSERT(pktlen >= 6);

	if (pktlen < 6)
		return -1;

	GETSHORT(flags, pkt);

	avp->is_mandatory = ((flags & 0x8000) != 0)? 1 : 0;
	avp->is_hidden = ((flags & 0x4000) != 0)? 1 : 0;
	avp->length = flags & 0x03ff;

	GETSHORT(avp->vendor_id, pkt);

	avp->attr_type = *pkt << 8;
	avp->attr_type |= *(pkt + 1);
	pkt += 2;

	if (avp->length > pktlen)
		return -1;

	if (filldata != 0)
		memcpy(avp->attr_value, pkt, avp->length - 6);

	return avp->length;
}

#define	NAME_VAL(x)	{ x, #x }
static struct _label_name {
	int		label;
	const char	*name;
}
l2tp_mes_type_names[] = {
	NAME_VAL(L2TP_AVP_MESSAGE_TYPE_SCCRQ),
	NAME_VAL(L2TP_AVP_MESSAGE_TYPE_SCCRP),
	NAME_VAL(L2TP_AVP_MESSAGE_TYPE_SCCCN),
	NAME_VAL(L2TP_AVP_MESSAGE_TYPE_StopCCN),
	NAME_VAL(L2TP_AVP_MESSAGE_TYPE_HELLO),
	NAME_VAL(L2TP_AVP_MESSAGE_TYPE_OCRQ),
	NAME_VAL(L2TP_AVP_MESSAGE_TYPE_OCRP),
	NAME_VAL(L2TP_AVP_MESSAGE_TYPE_OCCN),
	NAME_VAL(L2TP_AVP_MESSAGE_TYPE_ICRQ),
	NAME_VAL(L2TP_AVP_MESSAGE_TYPE_ICRP),
	NAME_VAL(L2TP_AVP_MESSAGE_TYPE_ICCN),
	NAME_VAL(L2TP_AVP_MESSAGE_TYPE_CDN),
},
l2tp_avp_attribute_names[] = {
	NAME_VAL(L2TP_AVP_TYPE_MESSAGE_TYPE),
	NAME_VAL(L2TP_AVP_TYPE_RESULT_CODE),
	NAME_VAL(L2TP_AVP_TYPE_PROTOCOL_VERSION),
	NAME_VAL(L2TP_AVP_TYPE_FRAMING_CAPABILITIES),
	NAME_VAL(L2TP_AVP_TYPE_BEARER_CAPABILITIES),
	NAME_VAL(L2TP_AVP_TYPE_TIE_BREAKER),
	NAME_VAL(L2TP_AVP_TYPE_FIRMWARE_REVISION),
	NAME_VAL(L2TP_AVP_TYPE_HOST_NAME),
	NAME_VAL(L2TP_AVP_TYPE_VENDOR_NAME),
	NAME_VAL(L2TP_AVP_TYPE_ASSINGED_TUNNEL_ID),
	NAME_VAL(L2TP_AVP_TYPE_RECV_WINDOW_SIZE),
	NAME_VAL(L2TP_AVP_TYPE_CHALLENGE),
	NAME_VAL(L2TP_AVP_TYPE_CAUSE_CODE),
	NAME_VAL(L2TP_AVP_TYPE_CHALLENGE_RESPONSE),
	NAME_VAL(L2TP_AVP_TYPE_ASSIGNED_SESSION_ID),
	NAME_VAL(L2TP_AVP_TYPE_CALL_SERIAL_NUMBER),
	NAME_VAL(L2TP_AVP_TYPE_MINIMUM_BPS),
	NAME_VAL(L2TP_AVP_TYPE_MAXIMUM_BPS),
	NAME_VAL(L2TP_AVP_TYPE_BEARER_TYPE),
	NAME_VAL(L2TP_AVP_TYPE_FRAMING_TYPE),
	NAME_VAL(L2TP_AVP_TYPE_CALLED_NUMBER),
	NAME_VAL(L2TP_AVP_TYPE_CALLING_NUMBER),
	NAME_VAL(L2TP_AVP_TYPE_SUB_ADDRESS),
	NAME_VAL(L2TP_AVP_TYPE_TX_CONNECT_SPEED),
	NAME_VAL(L2TP_AVP_TYPE_PHYSICAL_CHANNEL_ID),
	NAME_VAL(L2TP_AVP_TYPE_INITIAL_RECV_LCP_CONFREQ),
	NAME_VAL(L2TP_AVP_TYPE_LAST_SENT_LCP_CONFREQ),
	NAME_VAL(L2TP_AVP_TYPE_LAST_RECV_LCP_CONFREQ),
	NAME_VAL(L2TP_AVP_TYPE_PROXY_AUTHEN_TYPE),
	NAME_VAL(L2TP_AVP_TYPE_PROXY_AUTHEN_NAME),
	NAME_VAL(L2TP_AVP_TYPE_PROXY_AUTHEN_CHALLENGE),
	NAME_VAL(L2TP_AVP_TYPE_PROXY_AUTHEN_ID),
	NAME_VAL(L2TP_AVP_TYPE_PROXY_AUTHEN_RESPONSE),
	NAME_VAL(L2TP_AVP_TYPE_CALL_ERRORS),
	NAME_VAL(L2TP_AVP_TYPE_ACCM),
	NAME_VAL(L2TP_AVP_TYPE_RANDOM_VECTOR),
	NAME_VAL(L2TP_AVP_TYPE_PRIVATE_GROUP_ID),
	NAME_VAL(L2TP_AVP_TYPE_RX_CONNECT_SPEED),
	NAME_VAL(L2TP_AVP_TYPE_SEQUENCING_REQUIRED),
	NAME_VAL(L2TP_AVP_TYPE_TX_MINIMUM),
	NAME_VAL(L2TP_AVP_TYPE_CALLING_SUB_ADDRESS),
	NAME_VAL(L2TP_AVP_TYPE_PPP_DISCONNECT_CAUSE_CODE),
	NAME_VAL(L2TP_AVP_TYPE_CCDS),
	NAME_VAL(L2TP_AVP_TYPE_SDS),
	NAME_VAL(L2TP_AVP_TYPE_LCP_WANT_OPTIONS),
	NAME_VAL(L2TP_AVP_TYPE_LCP_ALLOW_OPTIONS),
	NAME_VAL(L2TP_AVP_TYPE_LNS_LAST_SENT_LCP_CONFREQ),
	NAME_VAL(L2TP_AVP_TYPE_LNS_LAST_RECV_LCP_CONFREQ),
	NAME_VAL(L2TP_AVP_TYPE_MODEM_ON_HOLD_CAPABLE),
	NAME_VAL(L2TP_AVP_TYPE_MODEM_ON_HOLD_STATUS),
	NAME_VAL(L2TP_AVP_TYPE_PPPOE_RELAY),
	NAME_VAL(L2TP_AVP_TYPE_PPPOE_RELAY_RESP_CAP),
	NAME_VAL(L2TP_AVP_TYPE_PPPOE_RELAY_FORW_CAP),
	NAME_VAL(L2TP_AVP_TYPE_EXTENDED_VENDOR_ID),
	NAME_VAL(L2TP_AVP_TYPE_PSEUDOWIRE_CAP_LIST),
	NAME_VAL(L2TP_AVP_TYPE_LOCAL_SESSION_ID),
	NAME_VAL(L2TP_AVP_TYPE_REMOTE_SESSION_ID),
	NAME_VAL(L2TP_AVP_TYPE_ASSIGNED_COOKIE),
	NAME_VAL(L2TP_AVP_TYPE_REMOTE_END_ID),
	NAME_VAL(L2TP_AVP_TYPE_APPLICATION_CODE),
	NAME_VAL(L2TP_AVP_TYPE_PSEUDOWIRE_TYPE),
	NAME_VAL(L2TP_AVP_TYPE_L2_SPECIFIC_SUBLAYER),
	NAME_VAL(L2TP_AVP_TYPE_DATA_SEQUENCING),
	NAME_VAL(L2TP_AVP_TYPE_CIRCUIT_STATUS),
	NAME_VAL(L2TP_AVP_TYPE_PREFERRED_LANGUAGE),
	NAME_VAL(L2TP_AVP_TYPE_CTRL_MSG_AUTH_NONCE),
	NAME_VAL(L2TP_AVP_TYPE_TX_CONNECT_SPEED),
	NAME_VAL(L2TP_AVP_TYPE_RX_CONNECT_SPEED),
	NAME_VAL(L2TP_AVP_TYPE_FAILOVER_CAPABILITY),
	NAME_VAL(L2TP_AVP_TYPE_TUNNEL_RECOVERY),
	NAME_VAL(L2TP_AVP_TYPE_SUGGESTED_CTRL_SEQUENCE),
	NAME_VAL(L2TP_AVP_TYPE_FAILOVER_SESSION_STATE),
	NAME_VAL(L2TP_AVP_TYPE_MULTICAST_CAPABILITY),
	NAME_VAL(L2TP_AVP_TYPE_NEW_OUTGOING_SESSIONS),
	NAME_VAL(L2TP_AVP_TYPE_NEW_OUTGOING_SESSIONS_ACK),
	NAME_VAL(L2TP_AVP_TYPE_WITHDRAW_OUTGOING_SESSIONS),
	NAME_VAL(L2TP_AVP_TYPE_MULTICAST_PACKETS_PRIORITY),
},
l2tp_stopccn_rcode_names[] = {
	NAME_VAL(L2TP_STOP_CCN_RCODE_GENERAL),
	NAME_VAL(L2TP_STOP_CCN_RCODE_GENERAL_ERROR),
	NAME_VAL(L2TP_STOP_CCN_RCODE_ALREADY_EXISTS),
	NAME_VAL(L2TP_STOP_CCN_RCODE_UNAUTHORIZED),
	NAME_VAL(L2TP_STOP_CCN_RCODE_BAD_PROTOCOL_VERSION),
	NAME_VAL(L2TP_STOP_CCN_RCODE_SHUTTING_DOWN),
	NAME_VAL(L2TP_STOP_CCN_RCODE_FSM_ERROR),
},
l2tp_cdn_rcode_names[] = {
	NAME_VAL(L2TP_CDN_RCODE_LOST_CARRIER),
	NAME_VAL(L2TP_CDN_RCODE_ERROR_CODE),
	NAME_VAL(L2TP_CDN_RCODE_ADMINISTRATIVE_REASON),
	NAME_VAL(L2TP_CDN_RCODE_TEMP_NOT_AVALIABLE),
	NAME_VAL(L2TP_CDN_RCODE_PERM_NOT_AVALIABLE),
	NAME_VAL(L2TP_CDN_RCODE_INVALID_DESTINATION),
	NAME_VAL(L2TP_CDN_RCODE_NO_CARRIER),
	NAME_VAL(L2TP_CDN_RCODE_BUSY),
	NAME_VAL(L2TP_CDN_RCODE_NO_DIALTONE),
	NAME_VAL(L2TP_CDN_RCODE_CALL_TIMEOUT_BY_LAC),
	NAME_VAL(L2TP_CDN_RCODE_NO_FRAMING_DETECTED),
},
l2tp_ecode_names[] = {
	NAME_VAL(L2TP_ECODE_NO_CONTROL_CONNECTION),
	NAME_VAL(L2TP_ECODE_WRONG_LENGTH),
	NAME_VAL(L2TP_ECODE_INVALID_MESSAGE),
	NAME_VAL(L2TP_ECODE_NO_RESOURCE),
	NAME_VAL(L2TP_ECODE_INVALID_SESSION_ID),
	NAME_VAL(L2TP_ECODE_GENERIC_ERROR),
	NAME_VAL(L2TP_ECODE_TRY_ANOTHER),
	NAME_VAL(L2TP_ECODE_UNKNOWN_MANDATORY_AVP),
};
#undef	NAME_VAL

const char *
avp_attr_type_string(int attr_type)
{
	int i;

	for (i = 0; i < countof(l2tp_avp_attribute_names); i++) {
		if (attr_type == l2tp_avp_attribute_names[i].label)
			return l2tp_avp_attribute_names[i].name + 14;
	}
	return "UNKNOWN_AVP";
}

const char *
l2tp_stopccn_rcode_string(int rcode)
{
	int i;

	for (i = 0; i < countof(l2tp_stopccn_rcode_names); i++) {
		if (rcode == l2tp_stopccn_rcode_names[i].label)
			return l2tp_stopccn_rcode_names[i].name + 20;
	}
	return "UNKNOWN";
}

const char *
l2tp_cdn_rcode_string(int rcode)
{
	int i;

	for (i = 0; i < countof(l2tp_cdn_rcode_names); i++) {
		if (rcode == l2tp_cdn_rcode_names[i].label)
			return l2tp_cdn_rcode_names[i].name + 15;
	}
	return "UNKNOWN";
}

const char *
l2tp_ecode_string(int ecode)
{
	int i;

	if (ecode == 0)
		return "none";
	for (i = 0; i < countof(l2tp_ecode_names); i++) {
		if (ecode == l2tp_ecode_names[i].label)
			return l2tp_ecode_names[i].name + 11;
	}
	return "UNKNOWN";
}

/**
 * Search the AVP that matches given vendor_id and attr_type and return it
 * In case the "fill_data" is specified (non 0 value is specified as the
 * "fill_data"), the memory space of the "avp" must be larger than or equal
 * to L2TP_AVP_MAXSIZ (1024).
 */
struct l2tp_avp *
avp_find(struct l2tp_avp *avp, const u_char *pkt, int pktlen,
    uint16_t vendor_id, uint16_t attr_type, int fill_data)
{
	int avpsz;

	while (pktlen >= 6 &&
	    (avpsz = avp_enum(avp, pkt, pktlen, fill_data)) > 0) {
		if (avp->vendor_id != vendor_id || avp->attr_type != attr_type) {
			if (avpsz < 6)
				return NULL;
			pkt += avpsz;
			pktlen -= avpsz;
			continue;
		}
		return avp;
	}

	return NULL;
}

/**
 * Search the Message-Type AVP and return it.  The memory space of the "avp"
 * must be larger than or equal to L2TP_AVP_MAXSIZ (1024).
 */
struct l2tp_avp *
avp_find_message_type_avp(struct l2tp_avp *avp, const u_char *pkt, int pktlen)
{
	return avp_find(avp, pkt, pktlen, 0, L2TP_AVP_TYPE_MESSAGE_TYPE, 1);
}

/**
 * add an AVP to bytebuffer
 */
int
bytebuf_add_avp(bytebuffer *bytebuf, struct l2tp_avp *avp, int value_len)
{
	struct l2tp_avp avp1;

	memcpy(&avp1, avp, sizeof(struct l2tp_avp));

	avp1.length = value_len + 6;
	avp1.vendor_id = htons(avp->vendor_id);
	avp1.attr_type = htons(avp->attr_type);
	*(uint16_t *)&avp1 = htons(*(uint16_t *)&avp1);

	if (bytebuffer_put(bytebuf, &avp1, 6) == NULL)
		return -1;
	if (bytebuffer_put(bytebuf, avp->attr_value, value_len) == NULL)
		return -1;

	return 0;
}

const char *
avp_mes_type_string(int mes_type)
{
	int i;

	for (i = 0; i < countof(l2tp_mes_type_names); i++) {
		if (mes_type == l2tp_mes_type_names[i].label)
			return l2tp_mes_type_names[i].name + 22;
	}
	return "Unknown";
}