1 /* pppol2tp.c - pppd plugin to implement PPPoL2TP protocol
2 * for Linux using kernel pppol2tp support.
4 * Requires kernel pppol2tp driver which is integrated into the kernel
5 * from 2.6.23 onwards. For earlier kernels, a version can be obtained
6 * from the OpenL2TP project at
7 * http://www.sourceforge.net/projects/openl2tp/
9 * Original by Martijn van Oosterhout <kleptog@svana.org>
10 * Modified by jchapman@katalix.com
12 * Heavily based upon pppoatm.c: original notice follows
14 * Copyright 2000 Mitchell Blank Jr.
15 * Based in part on work from Jens Axboe and Paul Mackerras.
16 * Updated to ppp-2.4.1 by Bernhard Kaindl
18 * This program is free software; you can redistribute it and/or
19 * modify it under the terms of the GNU General Public License
20 * as published by the Free Software Foundation; either version
21 * 2 of the License, or (at your option) any later version.
32 #include "pathnames.h"
39 #include <sys/ioctl.h>
40 #include <sys/socket.h>
41 #include <netinet/in.h>
43 #include <linux/version.h>
44 #include <linux/sockios.h>
46 /* should be defined in sys/types.h */
47 #define aligned_u64 unsigned long long __attribute__((aligned(8)))
49 #include <linux/types.h>
50 #include <linux/if_ether.h>
51 #include <linux/ppp_defs.h>
52 #include <linux/if_ppp.h>
53 #include <linux/if_pppox.h>
54 #include <linux/if_pppol2tp.h>
56 /* should be added to system's socket.h... */
58 #define SOL_PPPOL2TP 273
61 const char pppd_version[] = VERSION;
63 static int setdevname_pppol2tp(char **argv);
65 static int pppol2tp_fd = -1;
66 static char *pppol2tp_fd_str;
67 static bool pppol2tp_lns_mode = 0;
68 static bool pppol2tp_recv_seq = 0;
69 static bool pppol2tp_send_seq = 0;
70 static int pppol2tp_debug_mask = 0;
71 static int pppol2tp_reorder_timeout = 0;
72 static char pppol2tp_ifname[32] = { 0, };
73 int pppol2tp_tunnel_id = 0;
74 int pppol2tp_session_id = 0;
76 static int device_got_set = 0;
77 struct channel pppol2tp_channel;
79 static void (*old_snoop_recv_hook)(unsigned char *p, int len) = NULL;
80 static void (*old_snoop_send_hook)(unsigned char *p, int len) = NULL;
82 /* Hook provided to allow other plugins to handle ACCM changes */
83 void (*pppol2tp_send_accm_hook)(int tunnel_id, int session_id,
84 uint32_t send_accm, uint32_t recv_accm) = NULL;
86 /* Hook provided to allow other plugins to handle IP up/down */
87 void (*pppol2tp_ip_updown_hook)(int tunnel_id, int session_id, int up) = NULL;
89 static option_t pppol2tp_options[] = {
90 { "pppol2tp", o_special, &setdevname_pppol2tp,
91 "FD for PPPoL2TP socket", OPT_DEVNAM | OPT_A2STRVAL,
93 { "pppol2tp_lns_mode", o_bool, &pppol2tp_lns_mode,
94 "PPPoL2TP LNS behavior. Default off.",
95 OPT_PRIO | OPRIO_CFGFILE },
96 { "pppol2tp_send_seq", o_bool, &pppol2tp_send_seq,
97 "PPPoL2TP enable sequence numbers in transmitted data packets. "
99 OPT_PRIO | OPRIO_CFGFILE },
100 { "pppol2tp_recv_seq", o_bool, &pppol2tp_recv_seq,
101 "PPPoL2TP enforce sequence numbers in received data packets. "
103 OPT_PRIO | OPRIO_CFGFILE },
104 { "pppol2tp_reorderto", o_int, &pppol2tp_reorder_timeout,
105 "PPPoL2TP data packet reorder timeout. Default 0 (no reordering).",
107 { "pppol2tp_debug_mask", o_int, &pppol2tp_debug_mask,
108 "PPPoL2TP debug mask. Default: no debug.",
110 { "pppol2tp_ifname", o_string, &pppol2tp_ifname,
111 "Set interface name of PPP interface",
112 OPT_PRIO | OPT_PRIV | OPT_STATIC, NULL, 16 },
113 { "pppol2tp_tunnel_id", o_int, &pppol2tp_tunnel_id,
114 "PPPoL2TP tunnel_id.",
116 { "pppol2tp_session_id", o_int, &pppol2tp_session_id,
117 "PPPoL2TP session_id.",
122 static int setdevname_pppol2tp(char **argv)
126 struct sockaddr pppol2tp;
131 int tmp_len = sizeof(tmp);
136 if (!int_option(*argv, &pppol2tp_fd))
139 if(getsockname(pppol2tp_fd, (struct sockaddr *)&s, &len) < 0) {
140 fatal("Given FD for PPPoL2TP socket invalid (%s)",
143 if(s.pppol2tp.sa_family != AF_PPPOX) {
144 fatal("Socket of not a PPPoX socket");
147 /* Do a test getsockopt() to ensure that the kernel has the necessary
150 if (getsockopt(pppol2tp_fd, SOL_PPPOL2TP, PPPOL2TP_SO_DEBUG,
151 &tmp, &tmp_len) < 0) {
152 fatal("PPPoL2TP kernel driver not installed");
155 pppol2tp_fd_str = strdup(*argv);
156 if (pppol2tp_fd_str == NULL)
159 /* Setup option defaults. Compression options are disabled! */
163 lcp_allowoptions[0].neg_accompression = 1;
164 lcp_wantoptions[0].neg_accompression = 0;
166 lcp_allowoptions[0].neg_pcompression = 1;
167 lcp_wantoptions[0].neg_pcompression = 0;
169 ccp_allowoptions[0].deflate = 0;
170 ccp_wantoptions[0].deflate = 0;
172 ipcp_allowoptions[0].neg_vj = 0;
173 ipcp_wantoptions[0].neg_vj = 0;
175 ccp_allowoptions[0].bsd_compress = 0;
176 ccp_wantoptions[0].bsd_compress = 0;
178 the_channel = &pppol2tp_channel;
184 static int connect_pppol2tp(void)
186 if(pppol2tp_fd == -1) {
187 fatal("No PPPoL2TP FD specified");
193 static void disconnect_pppol2tp(void)
195 if (pppol2tp_fd >= 0) {
201 static void send_config_pppol2tp(int mtu,
213 if (pppol2tp_ifname[0]) {
217 fd = socket(AF_INET, SOCK_DGRAM, 0);
219 memset (&ifr, '\0', sizeof (ifr));
220 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
221 strlcpy(ifr.ifr_newname, pppol2tp_ifname,
222 sizeof(ifr.ifr_name));
223 ioctl(fd, SIOCSIFNAME, (caddr_t) &ifr);
224 strlcpy(ifname, pppol2tp_ifname, 32);
225 if (pppol2tp_debug_mask & PPPOL2TP_MSG_CONTROL) {
226 dbglog("ppp%d: interface name %s",
233 if ((lcp_allowoptions[0].mru > 0) && (mtu > lcp_allowoptions[0].mru)) {
234 warn("Overriding mtu %d to %d", mtu, lcp_allowoptions[0].mru);
235 mtu = lcp_allowoptions[0].mru;
237 netif_set_mtu(ifunit, mtu);
240 if (pppol2tp_reorder_timeout > 0)
241 sprintf(&reorderto[0], "%d ", pppol2tp_reorder_timeout);
243 if (pppol2tp_tunnel_id > 0)
244 sprintf(&tid[0], "%hu ", pppol2tp_tunnel_id);
246 if (pppol2tp_session_id > 0)
247 sprintf(&sid[0], "%hu ", pppol2tp_session_id);
249 dbglog("PPPoL2TP options: %s%s%s%s%s%s%s%s%sdebugmask %d",
250 pppol2tp_recv_seq ? "recvseq " : "",
251 pppol2tp_send_seq ? "sendseq " : "",
252 pppol2tp_lns_mode ? "lnsmode " : "",
253 pppol2tp_reorder_timeout ? "reorderto " : "", reorderto,
254 pppol2tp_tunnel_id ? "tid " : "", tid,
255 pppol2tp_session_id ? "sid " : "", sid,
256 pppol2tp_debug_mask);
258 if (pppol2tp_recv_seq)
259 if (setsockopt(pppol2tp_fd, SOL_PPPOL2TP, PPPOL2TP_SO_RECVSEQ,
260 &on, sizeof(on)) < 0)
261 fatal("setsockopt(PPPOL2TP_RECVSEQ): %m");
262 if (pppol2tp_send_seq)
263 if (setsockopt(pppol2tp_fd, SOL_PPPOL2TP, PPPOL2TP_SO_SENDSEQ,
264 &on, sizeof(on)) < 0)
265 fatal("setsockopt(PPPOL2TP_SENDSEQ): %m");
266 if (pppol2tp_lns_mode)
267 if (setsockopt(pppol2tp_fd, SOL_PPPOL2TP, PPPOL2TP_SO_LNSMODE,
268 &on, sizeof(on)) < 0)
269 fatal("setsockopt(PPPOL2TP_LNSMODE): %m");
270 if (pppol2tp_reorder_timeout)
271 if (setsockopt(pppol2tp_fd, SOL_PPPOL2TP, PPPOL2TP_SO_REORDERTO,
272 &pppol2tp_reorder_timeout,
273 sizeof(pppol2tp_reorder_timeout)) < 0)
274 fatal("setsockopt(PPPOL2TP_REORDERTO): %m");
275 if (pppol2tp_debug_mask)
276 if (setsockopt(pppol2tp_fd, SOL_PPPOL2TP, PPPOL2TP_SO_DEBUG,
277 &pppol2tp_debug_mask, sizeof(pppol2tp_debug_mask)) < 0)
278 fatal("setsockopt(PPPOL2TP_DEBUG): %m");
281 static void recv_config_pppol2tp(int mru,
286 if ((lcp_allowoptions[0].mru > 0) && (mru > lcp_allowoptions[0].mru)) {
287 warn("Overriding mru %d to mtu value %d", mru,
288 lcp_allowoptions[0].mru);
289 mru = lcp_allowoptions[0].mru;
291 if ((ifunit >= 0) && ioctl(pppol2tp_fd, PPPIOCSMRU, (caddr_t) &mru) < 0)
292 error("Couldn't set PPP MRU: %m");
295 /*****************************************************************************
296 * Snoop LCP message exchanges to capture negotiated ACCM values.
297 * When asyncmap values have been seen from both sides, give the values to
299 * This code is derived from Roaring Penguin L2TP.
300 *****************************************************************************/
302 static void pppol2tp_lcp_snoop(unsigned char *buf, int len, int incoming)
304 static bool got_send_accm = 0;
305 static bool got_recv_accm = 0;
306 static uint32_t recv_accm = 0xffffffff;
307 static uint32_t send_accm = 0xffffffff;
308 static bool snooping = 1;
311 uint16_t lcp_pkt_len;
314 unsigned char const *opt_data;
317 /* Skip HDLC header */
321 /* Unreasonably short frame?? */
322 if (len <= 0) return;
326 /* Compressed protcol field */
329 protocol = ((unsigned int) buf[0]) * 256 + buf[1];
332 /* If it's a network protocol, stop snooping */
333 if (protocol <= 0x3fff) {
334 if (pppol2tp_debug_mask & PPPOL2TP_MSG_DEBUG) {
335 dbglog("Turning off snooping: "
336 "Network protocol %04x found.",
343 /* If it's not LCP, do not snoop */
344 if (protocol != 0xc021) {
348 /* Skip protocol; go to packet data */
352 /* Unreasonably short frame?? */
353 if (len <= 0) return;
355 /* Look for Configure-Ack or Configure-Reject code */
356 if (buf[0] != CONFACK && buf[0] != CONFREJ) return;
358 reject = (buf[0] == CONFREJ);
360 lcp_pkt_len = ((unsigned int) buf[2]) * 256 + buf[3];
362 /* Something fishy with length field? */
363 if (lcp_pkt_len > len) return;
365 /* Skip to options */
366 len = lcp_pkt_len - 4;
370 /* Pull off an option */
374 if (opt_len > len || opt_len < 2) break;
377 if (pppol2tp_debug_mask & PPPOL2TP_MSG_DEBUG) {
378 dbglog("Found option type %02x; len %d", opt, opt_len);
381 /* We are specifically interested in ACCM */
382 if (opt == CI_ASYNCMAP && opt_len == 0x06) {
384 /* ACCM negotiation REJECTED; use default */
386 if (pppol2tp_debug_mask & PPPOL2TP_MSG_DATA) {
387 dbglog("Rejected ACCM negotiation; "
389 incoming ? "incoming" : "outgoing");
396 memcpy(&accm, opt_data, sizeof(accm));
397 if (pppol2tp_debug_mask & PPPOL2TP_MSG_DATA) {
398 dbglog("Found ACCM of %08x (%s)", accm,
399 incoming ? "incoming" : "outgoing");
410 if (got_recv_accm && got_send_accm) {
411 if (pppol2tp_debug_mask & PPPOL2TP_MSG_CONTROL) {
412 dbglog("Telling L2TP: Send ACCM = %08x; "
413 "Receive ACCM = %08x", send_accm, recv_accm);
415 if (pppol2tp_send_accm_hook != NULL) {
416 (*pppol2tp_send_accm_hook)(pppol2tp_tunnel_id,
418 send_accm, recv_accm);
427 static void pppol2tp_lcp_snoop_recv(unsigned char *p, int len)
429 if (old_snoop_recv_hook != NULL)
430 (*old_snoop_recv_hook)(p, len);
431 pppol2tp_lcp_snoop(p, len, 1);
434 static void pppol2tp_lcp_snoop_send(unsigned char *p, int len)
436 if (old_snoop_send_hook != NULL)
437 (*old_snoop_send_hook)(p, len);
438 pppol2tp_lcp_snoop(p, len, 0);
441 /*****************************************************************************
442 * Interface up/down events
443 *****************************************************************************/
445 static void pppol2tp_ip_up(void *opaque, int arg)
447 /* may get called twice (for IPv4 and IPv6) but the hook handles that well */
448 if (pppol2tp_ip_updown_hook != NULL) {
449 (*pppol2tp_ip_updown_hook)(pppol2tp_tunnel_id,
450 pppol2tp_session_id, 1);
454 static void pppol2tp_ip_down(void *opaque, int arg)
456 /* may get called twice (for IPv4 and IPv6) but the hook handles that well */
457 if (pppol2tp_ip_updown_hook != NULL) {
458 (*pppol2tp_ip_updown_hook)(pppol2tp_tunnel_id,
459 pppol2tp_session_id, 0);
463 /*****************************************************************************
465 *****************************************************************************/
467 static void pppol2tp_check_options(void)
469 /* Enable LCP snooping for ACCM options only for LNS */
470 if (pppol2tp_lns_mode) {
471 if ((pppol2tp_tunnel_id == 0) || (pppol2tp_session_id == 0)) {
472 fatal("tunnel_id/session_id values not specified");
474 if (pppol2tp_debug_mask & PPPOL2TP_MSG_CONTROL) {
475 dbglog("Enabling LCP snooping");
477 old_snoop_recv_hook = snoop_recv_hook;
478 old_snoop_send_hook = snoop_send_hook;
480 snoop_recv_hook = pppol2tp_lcp_snoop_recv;
481 snoop_send_hook = pppol2tp_lcp_snoop_send;
485 /* Called just before pppd exits.
487 static void pppol2tp_cleanup(void)
489 if (pppol2tp_debug_mask & PPPOL2TP_MSG_DEBUG) {
490 dbglog("pppol2tp: exiting.");
492 disconnect_pppol2tp();
495 void plugin_init(void)
497 #if defined(__linux__)
498 extern int new_style_driver; /* From sys-linux.c */
499 if (!ppp_available() && !new_style_driver)
500 fatal("Kernel doesn't support ppp_generic - "
501 "needed for PPPoL2TP");
503 fatal("No PPPoL2TP support on this OS");
505 add_options(pppol2tp_options);
507 /* Hook up ip up/down notifiers to send indicator to openl2tpd
508 * that the link is up
510 add_notifier(&ip_up_notifier, pppol2tp_ip_up, NULL);
511 add_notifier(&ip_down_notifier, pppol2tp_ip_down, NULL);
512 add_notifier(&ipv6_up_notifier, pppol2tp_ip_up, NULL);
513 add_notifier(&ipv6_down_notifier, pppol2tp_ip_down, NULL);
516 struct channel pppol2tp_channel = {
517 options: pppol2tp_options,
518 process_extra_options: NULL,
519 check_options: &pppol2tp_check_options,
520 connect: &connect_pppol2tp,
521 disconnect: &disconnect_pppol2tp,
522 establish_ppp: &generic_establish_ppp,
523 disestablish_ppp: &generic_disestablish_ppp,
524 send_config: &send_config_pppol2tp,
525 recv_config: &recv_config_pppol2tp,