summaryrefslogtreecommitdiff
path: root/pppd/plugins/pppol2tp/pppol2tp.c
blob: 1be0ad25b67cb1e0dca3a6ce4832c766ecd996fe (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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
/* pppol2tp.c - pppd plugin to implement PPPoL2TP protocol
 *   for Linux using kernel pppol2tp support.
 *
 * Requires kernel pppol2tp driver which is integrated into the kernel
 * from 2.6.23 onwards. For earlier kernels, a version can be obtained
 * from the OpenL2TP project at
 * http://www.sourceforge.net/projects/openl2tp/
 *
 * Original by Martijn van Oosterhout <kleptog@svana.org>
 * Modified by jchapman@katalix.com
 *
 * Heavily based upon pppoatm.c: original notice follows
 *
 * Copyright 2000 Mitchell Blank Jr.
 * Based in part on work from Jens Axboe and Paul Mackerras.
 * Updated to ppp-2.4.1 by Bernhard Kaindl
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version
 *  2 of the License, or (at your option) any later version.
 */
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include "pppd.h"
#include "pathnames.h"
#include "fsm.h"
#include "lcp.h"
#include "ccp.h"
#include "ipcp.h"
#include <sys/stat.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <signal.h>
#include <linux/version.h>
#include <linux/sockios.h>
#ifndef aligned_u64
/* should be defined in sys/types.h */
#define aligned_u64 unsigned long long __attribute__((aligned(8)))
#endif
#include <linux/types.h>
#include <linux/if_ether.h>
#include <linux/ppp_defs.h>
#include <linux/if_ppp.h>
#include <linux/if_pppox.h>
#include <linux/if_pppol2tp.h>

/* should be added to system's socket.h... */
#ifndef SOL_PPPOL2TP
#define SOL_PPPOL2TP	273
#endif

const char pppd_version[] = VERSION;

static int setdevname_pppol2tp(char **argv);

static int pppol2tp_fd = -1;
static char *pppol2tp_fd_str;
static bool pppol2tp_lns_mode = 0;
static bool pppol2tp_recv_seq = 0;
static bool pppol2tp_send_seq = 0;
static int pppol2tp_debug_mask = 0;
static int pppol2tp_reorder_timeout = 0;
static char pppol2tp_ifname[32] = { 0, };
int pppol2tp_tunnel_id = 0;
int pppol2tp_session_id = 0;

static int device_got_set = 0;
struct channel pppol2tp_channel;

static void (*old_snoop_recv_hook)(unsigned char *p, int len) = NULL;
static void (*old_snoop_send_hook)(unsigned char *p, int len) = NULL;

/* Hook provided to allow other plugins to handle ACCM changes */
void (*pppol2tp_send_accm_hook)(int tunnel_id, int session_id,
				uint32_t send_accm, uint32_t recv_accm) = NULL;

/* Hook provided to allow other plugins to handle IP up/down */
void (*pppol2tp_ip_updown_hook)(int tunnel_id, int session_id, int up) = NULL;

static option_t pppol2tp_options[] = {
	{ "pppol2tp", o_special, &setdevname_pppol2tp,
	  "FD for PPPoL2TP socket", OPT_DEVNAM | OPT_A2STRVAL,
          &pppol2tp_fd_str },
	{ "pppol2tp_lns_mode", o_bool, &pppol2tp_lns_mode,
	  "PPPoL2TP LNS behavior. Default off.",
	  OPT_PRIO | OPRIO_CFGFILE },
	{ "pppol2tp_send_seq", o_bool, &pppol2tp_send_seq,
	  "PPPoL2TP enable sequence numbers in transmitted data packets. "
	  "Default off.",
	  OPT_PRIO | OPRIO_CFGFILE },
	{ "pppol2tp_recv_seq", o_bool, &pppol2tp_recv_seq,
	  "PPPoL2TP enforce sequence numbers in received data packets. "
	  "Default off.",
	  OPT_PRIO | OPRIO_CFGFILE },
	{ "pppol2tp_reorderto", o_int, &pppol2tp_reorder_timeout,
	  "PPPoL2TP data packet reorder timeout. Default 0 (no reordering).",
	  OPT_PRIO },
	{ "pppol2tp_debug_mask", o_int, &pppol2tp_debug_mask,
	  "PPPoL2TP debug mask. Default: no debug.",
	  OPT_PRIO },
	{ "pppol2tp_ifname", o_string, &pppol2tp_ifname,
	  "Set interface name of PPP interface",
	  OPT_PRIO | OPT_PRIV | OPT_STATIC, NULL, 16 },
	{ "pppol2tp_tunnel_id", o_int, &pppol2tp_tunnel_id,
	  "PPPoL2TP tunnel_id.",
	  OPT_PRIO },
	{ "pppol2tp_session_id", o_int, &pppol2tp_session_id,
	  "PPPoL2TP session_id.",
	  OPT_PRIO },
	{ NULL }
};

static int setdevname_pppol2tp(char **argv)
{
	union {
		char buffer[128];
		struct sockaddr pppol2tp;
	} s;
	int len = sizeof(s);
	char **a;
	int tmp;
	int tmp_len = sizeof(tmp);

	if (device_got_set)
		return 0;

	if (!int_option(*argv, &pppol2tp_fd))
		return 0;

	if(getsockname(pppol2tp_fd, (struct sockaddr *)&s, &len) < 0) {
		fatal("Given FD for PPPoL2TP socket invalid (%s)",
		      strerror(errno));
	}
	if(s.pppol2tp.sa_family != AF_PPPOX) {
		fatal("Socket of not a PPPoX socket");
	}

	/* Do a test getsockopt() to ensure that the kernel has the necessary
	 * feature available.
	 */
	if (getsockopt(pppol2tp_fd, SOL_PPPOL2TP, PPPOL2TP_SO_DEBUG,
		       &tmp, &tmp_len) < 0) {
		fatal("PPPoL2TP kernel driver not installed");
	}

	pppol2tp_fd_str = strdup(*argv);
	if (pppol2tp_fd_str == NULL)
		novm("PPPoL2TP FD");

	/* Setup option defaults. Compression options are disabled! */

	modem = 0;

	lcp_allowoptions[0].neg_accompression = 1;
	lcp_wantoptions[0].neg_accompression = 0;

	lcp_allowoptions[0].neg_pcompression = 1;
	lcp_wantoptions[0].neg_pcompression = 0;

	ccp_allowoptions[0].deflate = 0;
	ccp_wantoptions[0].deflate = 0;

	ipcp_allowoptions[0].neg_vj = 0;
	ipcp_wantoptions[0].neg_vj = 0;

	ccp_allowoptions[0].bsd_compress = 0;
	ccp_wantoptions[0].bsd_compress = 0;

	the_channel = &pppol2tp_channel;
	device_got_set = 1;

	return 1;
}

static int connect_pppol2tp(void)
{
	if(pppol2tp_fd == -1) {
		fatal("No PPPoL2TP FD specified");
	}

	return pppol2tp_fd;
}

static void disconnect_pppol2tp(void)
{
	if (pppol2tp_fd >= 0) {
		close(pppol2tp_fd);
		pppol2tp_fd = -1;
	}
}

static void send_config_pppol2tp(int mtu,
			      u_int32_t asyncmap,
			      int pcomp,
			      int accomp)
{
	struct ifreq ifr;
	int on = 1;
	int fd;
	char reorderto[16];
	char tid[8];
	char sid[8];

	if (pppol2tp_ifname[0]) {
		struct ifreq ifr;
		int fd;

		fd = socket(AF_INET, SOCK_DGRAM, 0);
		if (fd >= 0) {
			memset (&ifr, '\0', sizeof (ifr));
			strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
			strlcpy(ifr.ifr_newname, pppol2tp_ifname,
				sizeof(ifr.ifr_name));
			ioctl(fd, SIOCSIFNAME, (caddr_t) &ifr);
			strlcpy(ifname, pppol2tp_ifname, 32);
			if (pppol2tp_debug_mask & PPPOL2TP_MSG_CONTROL) {
				dbglog("ppp%d: interface name %s",
				       ifunit, ifname);
			}
		}
		close(fd);
	}

	if ((lcp_allowoptions[0].mru > 0) && (mtu > lcp_allowoptions[0].mru)) {
		warn("Overriding mtu %d to %d", mtu, lcp_allowoptions[0].mru);
		mtu = lcp_allowoptions[0].mru;
	}
	netif_set_mtu(ifunit, mtu);

	reorderto[0] = '\0';
	if (pppol2tp_reorder_timeout > 0)
		sprintf(&reorderto[0], "%d ", pppol2tp_reorder_timeout);
	tid[0] = '\0';
	if (pppol2tp_tunnel_id > 0)
		sprintf(&tid[0], "%hu ", pppol2tp_tunnel_id);
	sid[0] = '\0';
	if (pppol2tp_session_id > 0)
		sprintf(&sid[0], "%hu ", pppol2tp_session_id);

	dbglog("PPPoL2TP options: %s%s%s%s%s%s%s%s%sdebugmask %d",
	       pppol2tp_recv_seq ? "recvseq " : "",
	       pppol2tp_send_seq ? "sendseq " : "",
	       pppol2tp_lns_mode ? "lnsmode " : "",
	       pppol2tp_reorder_timeout ? "reorderto " : "", reorderto,
	       pppol2tp_tunnel_id ? "tid " : "", tid,
	       pppol2tp_session_id ? "sid " : "", sid,
	       pppol2tp_debug_mask);

	if (pppol2tp_recv_seq)
		if (setsockopt(pppol2tp_fd, SOL_PPPOL2TP, PPPOL2TP_SO_RECVSEQ,
			       &on, sizeof(on)) < 0)
			fatal("setsockopt(PPPOL2TP_RECVSEQ): %m");
	if (pppol2tp_send_seq)
		if (setsockopt(pppol2tp_fd, SOL_PPPOL2TP, PPPOL2TP_SO_SENDSEQ,
			       &on, sizeof(on)) < 0)
			fatal("setsockopt(PPPOL2TP_SENDSEQ): %m");
	if (pppol2tp_lns_mode)
		if (setsockopt(pppol2tp_fd, SOL_PPPOL2TP, PPPOL2TP_SO_LNSMODE,
			       &on, sizeof(on)) < 0)
			fatal("setsockopt(PPPOL2TP_LNSMODE): %m");
	if (pppol2tp_reorder_timeout)
		if (setsockopt(pppol2tp_fd, SOL_PPPOL2TP, PPPOL2TP_SO_REORDERTO,
			       &pppol2tp_reorder_timeout,
			       sizeof(pppol2tp_reorder_timeout)) < 0)
			fatal("setsockopt(PPPOL2TP_REORDERTO): %m");
	if (pppol2tp_debug_mask)
		if (setsockopt(pppol2tp_fd, SOL_PPPOL2TP, PPPOL2TP_SO_DEBUG,
			       &pppol2tp_debug_mask, sizeof(pppol2tp_debug_mask)) < 0)
			fatal("setsockopt(PPPOL2TP_DEBUG): %m");
}

static void recv_config_pppol2tp(int mru,
			      u_int32_t asyncmap,
			      int pcomp,
			      int accomp)
{
	if ((lcp_allowoptions[0].mru > 0) && (mru > lcp_allowoptions[0].mru)) {
		warn("Overriding mru %d to mtu value %d", mru,
		     lcp_allowoptions[0].mru);
		mru = lcp_allowoptions[0].mru;
	}
	if ((ifunit >= 0) && ioctl(pppol2tp_fd, PPPIOCSMRU, (caddr_t) &mru) < 0)
		error("Couldn't set PPP MRU: %m");
}

/*****************************************************************************
 * Snoop LCP message exchanges to capture negotiated ACCM values.
 * When asyncmap values have been seen from both sides, give the values to
 * L2TP.
 * This code is derived from Roaring Penguin L2TP.
 *****************************************************************************/

static void pppol2tp_lcp_snoop(unsigned char *buf, int len, int incoming)
{
	static bool got_send_accm = 0;
	static bool got_recv_accm = 0;
	static uint32_t recv_accm = 0xffffffff;
	static uint32_t send_accm = 0xffffffff;
	static bool snooping = 1;

	uint16_t protocol;
	uint16_t lcp_pkt_len;
	int opt, opt_len;
	int reject;
	unsigned char const *opt_data;
	uint32_t accm;

	/* Skip HDLC header */
	buf += 2;
	len -= 2;

	/* Unreasonably short frame?? */
	if (len <= 0) return;

	/* Get protocol */
	if (buf[0] & 0x01) {
		/* Compressed protcol field */
		protocol = buf[0];
	} else {
		protocol = ((unsigned int) buf[0]) * 256 + buf[1];
	}

	/* If it's a network protocol, stop snooping */
	if (protocol <= 0x3fff) {
		if (pppol2tp_debug_mask & PPPOL2TP_MSG_DEBUG) {
			dbglog("Turning off snooping: "
			       "Network protocol %04x found.",
			       protocol);
		}
		snooping = 0;
		return;
	}

	/* If it's not LCP, do not snoop */
	if (protocol != 0xc021) {
		return;
	}

	/* Skip protocol; go to packet data */
	buf += 2;
	len -= 2;

	/* Unreasonably short frame?? */
	if (len <= 0) return;

	/* Look for Configure-Ack or Configure-Reject code */
	if (buf[0] != CONFACK && buf[0] != CONFREJ) return;

	reject = (buf[0] == CONFREJ);

	lcp_pkt_len = ((unsigned int) buf[2]) * 256 + buf[3];

	/* Something fishy with length field? */
	if (lcp_pkt_len > len) return;

	/* Skip to options */
	len = lcp_pkt_len - 4;
	buf += 4;

	while (len > 0) {
		/* Pull off an option */
		opt = buf[0];
		opt_len = buf[1];
		opt_data = &buf[2];
		if (opt_len > len || opt_len < 2) break;
		len -= opt_len;
		buf += opt_len;
		if (pppol2tp_debug_mask & PPPOL2TP_MSG_DEBUG) {
			dbglog("Found option type %02x; len %d", opt, opt_len);
		}

		/* We are specifically interested in ACCM */
		if (opt == CI_ASYNCMAP && opt_len == 0x06) {
			if (reject) {
				/* ACCM negotiation REJECTED; use default */
				accm = 0xffffffff;
				if (pppol2tp_debug_mask & PPPOL2TP_MSG_DATA) {
					dbglog("Rejected ACCM negotiation; "
					       "defaulting (%s)",
					       incoming ? "incoming" : "outgoing");
				}
				recv_accm = accm;
				send_accm = accm;
				got_recv_accm = 1;
				got_send_accm = 1;
			} else {
				memcpy(&accm, opt_data, sizeof(accm));
				if (pppol2tp_debug_mask & PPPOL2TP_MSG_DATA) {
					dbglog("Found ACCM of %08x (%s)", accm,
					       incoming ? "incoming" : "outgoing");
				}
				if (incoming) {
					recv_accm = accm;
					got_recv_accm = 1;
				} else {
					send_accm = accm;
					got_send_accm = 1;
				}
			}

			if (got_recv_accm && got_send_accm) {
				if (pppol2tp_debug_mask & PPPOL2TP_MSG_CONTROL) {
					dbglog("Telling L2TP: Send ACCM = %08x; "
					       "Receive ACCM = %08x", send_accm, recv_accm);
				}
				if (pppol2tp_send_accm_hook != NULL) {
					(*pppol2tp_send_accm_hook)(pppol2tp_tunnel_id,
								   pppol2tp_session_id,
								   send_accm, recv_accm);
				}
				got_recv_accm = 0;
				got_send_accm = 0;
			}
		}
	}
}

static void pppol2tp_lcp_snoop_recv(unsigned char *p, int len)
{
	if (old_snoop_recv_hook != NULL)
		(*old_snoop_recv_hook)(p, len);
	pppol2tp_lcp_snoop(p, len, 1);
}

static void pppol2tp_lcp_snoop_send(unsigned char *p, int len)
{
	if (old_snoop_send_hook != NULL)
		(*old_snoop_send_hook)(p, len);
	pppol2tp_lcp_snoop(p, len, 0);
}

/*****************************************************************************
 * Interface up/down events
 *****************************************************************************/

static void pppol2tp_ip_up(void *opaque, int arg)
{
	/* may get called twice (for IPv4 and IPv6) but the hook handles that well */
	if (pppol2tp_ip_updown_hook != NULL) {
		(*pppol2tp_ip_updown_hook)(pppol2tp_tunnel_id,
					   pppol2tp_session_id, 1);
	}
}

static void pppol2tp_ip_down(void *opaque, int arg)
{
	/* may get called twice (for IPv4 and IPv6) but the hook handles that well */
	if (pppol2tp_ip_updown_hook != NULL) {
		(*pppol2tp_ip_updown_hook)(pppol2tp_tunnel_id,
					   pppol2tp_session_id, 0);
	}
}

/*****************************************************************************
 * Application init
 *****************************************************************************/

static void pppol2tp_check_options(void)
{
	/* Enable LCP snooping for ACCM options only for LNS */
	if (pppol2tp_lns_mode) {
		if ((pppol2tp_tunnel_id == 0) || (pppol2tp_session_id == 0)) {
			fatal("tunnel_id/session_id values not specified");
		}
		if (pppol2tp_debug_mask & PPPOL2TP_MSG_CONTROL) {
			dbglog("Enabling LCP snooping");
		}
		old_snoop_recv_hook = snoop_recv_hook;
		old_snoop_send_hook = snoop_send_hook;

		snoop_recv_hook = pppol2tp_lcp_snoop_recv;
		snoop_send_hook = pppol2tp_lcp_snoop_send;
	}
}

/* Called just before pppd exits.
 */
static void pppol2tp_cleanup(void)
{
	if (pppol2tp_debug_mask & PPPOL2TP_MSG_DEBUG) {
		dbglog("pppol2tp: exiting.");
	}
	disconnect_pppol2tp();
}

void plugin_init(void)
{
#if defined(__linux__)
	extern int new_style_driver;	/* From sys-linux.c */
	if (!ppp_available() && !new_style_driver)
		fatal("Kernel doesn't support ppp_generic - "
		    "needed for PPPoL2TP");
#else
	fatal("No PPPoL2TP support on this OS");
#endif
	add_options(pppol2tp_options);

	/* Hook up ip up/down notifiers to send indicator to openl2tpd
	 * that the link is up
	 */
	add_notifier(&ip_up_notifier, pppol2tp_ip_up, NULL);
	add_notifier(&ip_down_notifier, pppol2tp_ip_down, NULL);
	add_notifier(&ipv6_up_notifier, pppol2tp_ip_up, NULL);
	add_notifier(&ipv6_down_notifier, pppol2tp_ip_down, NULL);
}

struct channel pppol2tp_channel = {
    options: pppol2tp_options,
    process_extra_options: NULL,
    check_options: &pppol2tp_check_options,
    connect: &connect_pppol2tp,
    disconnect: &disconnect_pppol2tp,
    establish_ppp: &generic_establish_ppp,
    disestablish_ppp: &generic_disestablish_ppp,
    send_config: &send_config_pppol2tp,
    recv_config: &recv_config_pppol2tp,
    close: NULL,
    cleanup: NULL
};