summaryrefslogtreecommitdiffstats
path: root/gatchat/ppp_lcp.c
blob: 78407d415173cbed1d3d1ecd8e60acedbc2bb9bb (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
/*
 *
 *  PPP library with GLib integration
 *
 *  Copyright (C) 2009-2010  Intel Corporation. All rights reserved.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <termios.h>
#include <glib.h>
#include <arpa/inet.h>

#include "gatppp.h"
#include "ppp.h"

enum lcp_options {
	RESERVED 		= 0,
	MRU			= 1,
	ACCM			= 2,
	AUTH_PROTO		= 3,
	QUAL_PROTO		= 4,
	MAGIC_NUMBER		= 5,
	DEPRECATED_QUAL_PROTO	= 6,
	PFC			= 7,
	ACFC			= 8,
};

#define LCP_SUPPORTED_CODES	((1 << CONFIGURE_REQUEST) | \
				(1 << CONFIGURE_ACK) | \
				(1 << CONFIGURE_NAK) | \
				(1 << CONFIGURE_REJECT) | \
				(1 << TERMINATE_REQUEST) | \
				(1 << TERMINATE_ACK) | \
				(1 << CODE_REJECT) | \
				(1 << PROTOCOL_REJECT) | \
				(1 << ECHO_REQUEST) | \
				(1 << ECHO_REPLY) | \
				(1 << DISCARD_REQUEST))

/*
 * signal the Up event to the NCP
 */
static void lcp_up(struct pppcp_data *pppcp)
{
	ppp_generate_event(pppcp->ppp, PPP_OPENED);
}

/*
 * signal the Down event to the NCP
 */
static void lcp_down(struct pppcp_data *pppcp)
{
	/* XXX should implement a way to signal NCP */
}

/*
 * Indicate that the lower layer is now needed
 * Should trigger Up event
 */
static void lcp_started(struct pppcp_data *pppcp)
{
	ppp_generate_event(pppcp->ppp, PPP_UP);
}

/*
 * Indicate that the lower layer is not needed
 * Should trigger Down event
 */
static void lcp_finished(struct pppcp_data *pppcp)
{
	ppp_generate_event(pppcp->ppp, PPP_DOWN);
}

/*
 * Scan the option to see if it is acceptable, unacceptable, or rejected
 *
 * We need to use a default case here because this option type value
 * could be anything.
 */
static guint lcp_option_scan(struct ppp_option *option, gpointer user)
{
	switch (option->type) {
	case ACCM:
	case AUTH_PROTO:
		/* XXX check to make sure it's a proto we recognize */
	case MAGIC_NUMBER:
	case PFC:
	case ACFC:
		return OPTION_ACCEPT;
	}

	return OPTION_REJECT;
}

/*
 * act on an acceptable option
 *
 * We need to use a default case here because this option type value
 * could be anything.
 */
static void lcp_option_process(gpointer data, gpointer user)
{
	struct ppp_option *option = data;
	struct pppcp_data *pppcp = user;
	GAtPPP *ppp = pppcp->ppp;
	guint32 magic;

	switch (option->type) {
	case ACCM:
		ppp_set_recv_accm(ppp, get_host_long(option->data));
		break;
	case AUTH_PROTO:
		ppp_set_auth(ppp, option->data);
		break;
	case MAGIC_NUMBER:
		/* XXX handle loopback */
		magic = get_host_long(option->data);
		if (magic != pppcp->magic_number)
			pppcp->magic_number = magic;
		else
			g_print("looped back? I should do something\n");
		break;
	case PFC:
		ppp_set_pfc(ppp, TRUE);
		break;
	case ACFC:
		ppp_set_acfc(ppp, TRUE);
		break;
	default:
		g_printerr("unhandled option %d\n", option->type);
		break;
	}
}

static const char *lcp_option_strings[256] = {
	[0]	= "Vendor Specific",
	[1]	= "Maximum-Receive-Unit",
	[2]	= "Async-Control-Character-Map",
	[3]	= "Authentication-Protocol",
	[4]	= "Quality-Protocol",
	[5]	= "Magic-Number",
	[6]	= "Quality-Protocol (deprecated)",
	[7]	= "Protocol-Field-Compression",
	[8]	= "Address-and-Control-Field-Compression",
	[9]	= "FCS-Alternatives",
	[10]	= "Self-Describing-Pad",
	[11]	= "Numbered-Mode",
	[12]	= "Multi-Link-Procedure (deprecated)",
	[13]	= "Callback",
};

struct pppcp_action lcp_action = {
	.this_layer_up =	lcp_up,
	.this_layer_down = 	lcp_down,
	.this_layer_started = 	lcp_started,
	.this_layer_finished =	lcp_finished,
	.option_scan = 		lcp_option_scan,
	.option_process = 	lcp_option_process,
};

void lcp_open(struct pppcp_data *data)
{
	if (data == NULL)
		return;

	/* send an open event to the lcp layer */
	pppcp_generate_event(data, OPEN, NULL, 0);
}

void lcp_establish(struct pppcp_data *data)
{
	if (data == NULL)
		return;

	/* send an UP event to the lcp layer */
	pppcp_generate_event(data, UP, NULL, 0);
}

void lcp_terminate(struct pppcp_data *data)
{
	if (data == NULL)
		return;

	/* send a CLOSE event to the lcp layer */
	pppcp_generate_event(data, CLOSE, NULL, 0);
}

void lcp_free(struct pppcp_data *lcp)
{
	if (lcp == NULL)
		return;

	pppcp_free(lcp);
}

void lcp_protocol_reject(struct pppcp_data *lcp, guint8 *packet, gsize len)
{
	pppcp_send_protocol_reject(lcp, packet, len);
}

struct pppcp_data *lcp_new(GAtPPP *ppp)
{
	struct pppcp_data *pppcp;
	struct ppp_option *option;
	guint16 codes = LCP_SUPPORTED_CODES;

	pppcp = pppcp_new(ppp, LCP_PROTOCOL);
	if (!pppcp) {
		g_print("Failed to allocate PPPCP struct\n");
		return NULL;
	}
	pppcp_set_valid_codes(pppcp, codes);
	pppcp->option_strings = lcp_option_strings;
	pppcp->prefix = "lcp";
	pppcp->priv = pppcp;

	/* set the actions */
	pppcp->action = &lcp_action;

	/* add the default config options */
	option = g_try_malloc0(6);
	if (option == NULL) {
		pppcp_free(pppcp);
		return NULL;
	}
	option->type = ACCM;
	option->length = 6;
	pppcp_add_config_option(pppcp, option);

	return pppcp;
}