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.
28 #include "pathnames.h"
35 #include <sys/ioctl.h>
36 #include <sys/socket.h>
37 #include <netinet/in.h>
39 #include <linux/version.h>
40 #include <linux/sockios.h>
42 /* should be defined in sys/types.h */
43 #define aligned_u64 unsigned long long __attribute__((aligned(8)))
45 #include <linux/types.h>
46 #include <linux/if_ether.h>
47 #include <linux/ppp_defs.h>
48 #include <linux/if_ppp.h>
49 #include <linux/if_pppox.h>
50 #include <linux/if_pppol2tp.h>
52 /* should be added to system's socket.h... */
54 #define SOL_PPPOL2TP 273
57 const char pppd_version[] = VERSION;
59 static int setdevname_pppol2tp(char **argv);
61 static int pppol2tp_fd = -1;
62 static char *pppol2tp_fd_str;
63 static bool pppol2tp_lns_mode = 0;
64 static bool pppol2tp_recv_seq = 0;
65 static bool pppol2tp_send_seq = 0;
66 static int pppol2tp_debug_mask = 0;
67 static int pppol2tp_reorder_timeout = 0;
68 static char pppol2tp_ifname[32] = { 0, };
69 int pppol2tp_tunnel_id = 0;
70 int pppol2tp_session_id = 0;
72 static int device_got_set = 0;
73 struct channel pppol2tp_channel;
75 static void (*old_snoop_recv_hook)(unsigned char *p, int len) = NULL;
76 static void (*old_snoop_send_hook)(unsigned char *p, int len) = NULL;
78 /* Hook provided to allow other plugins to handle ACCM changes */
79 void (*pppol2tp_send_accm_hook)(int tunnel_id, int session_id,
80 uint32_t send_accm, uint32_t recv_accm) = NULL;
82 /* Hook provided to allow other plugins to handle IP up/down */
83 void (*pppol2tp_ip_updown_hook)(int tunnel_id, int session_id, int up) = NULL;
85 static option_t pppol2tp_options[] = {
86 { "pppol2tp", o_special, &setdevname_pppol2tp,
87 "FD for PPPoL2TP socket", OPT_DEVNAM | OPT_A2STRVAL,
89 { "pppol2tp_lns_mode", o_bool, &pppol2tp_lns_mode,
90 "PPPoL2TP LNS behavior. Default off.",
91 OPT_PRIO | OPRIO_CFGFILE },
92 { "pppol2tp_send_seq", o_bool, &pppol2tp_send_seq,
93 "PPPoL2TP enable sequence numbers in transmitted data packets. "
95 OPT_PRIO | OPRIO_CFGFILE },
96 { "pppol2tp_recv_seq", o_bool, &pppol2tp_recv_seq,
97 "PPPoL2TP enforce sequence numbers in received data packets. "
99 OPT_PRIO | OPRIO_CFGFILE },
100 { "pppol2tp_reorderto", o_int, &pppol2tp_reorder_timeout,
101 "PPPoL2TP data packet reorder timeout. Default 0 (no reordering).",
103 { "pppol2tp_debug_mask", o_int, &pppol2tp_debug_mask,
104 "PPPoL2TP debug mask. Default: no debug.",
106 { "pppol2tp_ifname", o_string, &pppol2tp_ifname,
107 "Set interface name of PPP interface",
108 OPT_PRIO | OPT_PRIV | OPT_STATIC, NULL, 16 },
109 { "pppol2tp_tunnel_id", o_int, &pppol2tp_tunnel_id,
110 "PPPoL2TP tunnel_id.",
112 { "pppol2tp_session_id", o_int, &pppol2tp_session_id,
113 "PPPoL2TP session_id.",
118 static int setdevname_pppol2tp(char **argv)
122 struct sockaddr pppol2tp;
127 int tmp_len = sizeof(tmp);
132 if (!int_option(*argv, &pppol2tp_fd))
135 if(getsockname(pppol2tp_fd, (struct sockaddr *)&s, &len) < 0) {
136 fatal("Given FD for PPPoL2TP socket invalid (%s)",
139 if(s.pppol2tp.sa_family != AF_PPPOX) {
140 fatal("Socket of not a PPPoX socket");
143 /* Do a test getsockopt() to ensure that the kernel has the necessary
146 if (getsockopt(pppol2tp_fd, SOL_PPPOL2TP, PPPOL2TP_SO_DEBUG,
147 &tmp, &tmp_len) < 0) {
148 fatal("PPPoL2TP kernel driver not installed");
151 /* Setup option defaults. Compression options are disabled! */
155 lcp_allowoptions[0].neg_accompression = 1;
156 lcp_wantoptions[0].neg_accompression = 0;
158 lcp_allowoptions[0].neg_pcompression = 1;
159 lcp_wantoptions[0].neg_pcompression = 0;
161 ccp_allowoptions[0].deflate = 0;
162 ccp_wantoptions[0].deflate = 0;
164 ipcp_allowoptions[0].neg_vj = 0;
165 ipcp_wantoptions[0].neg_vj = 0;
167 ccp_allowoptions[0].bsd_compress = 0;
168 ccp_wantoptions[0].bsd_compress = 0;
170 the_channel = &pppol2tp_channel;
176 static int connect_pppol2tp(void)
178 if(pppol2tp_fd == -1) {
179 fatal("No PPPoL2TP FD specified");
185 static void disconnect_pppol2tp(void)
187 if (pppol2tp_fd >= 0) {
193 static void send_config_pppol2tp(int mtu,
205 if (pppol2tp_ifname[0]) {
209 fd = socket(AF_INET, SOCK_DGRAM, 0);
211 memset (&ifr, '\0', sizeof (ifr));
212 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
213 strlcpy(ifr.ifr_newname, pppol2tp_ifname,
214 sizeof(ifr.ifr_name));
215 ioctl(fd, SIOCSIFNAME, (caddr_t) &ifr);
216 strlcpy(ifname, pppol2tp_ifname, 32);
217 if (pppol2tp_debug_mask & PPPOL2TP_MSG_CONTROL) {
218 dbglog("ppp%d: interface name %s",
225 if ((lcp_allowoptions[0].mru > 0) && (mtu > lcp_allowoptions[0].mru)) {
226 warn("Overriding mtu %d to %d", mtu, lcp_allowoptions[0].mru);
227 mtu = lcp_allowoptions[0].mru;
229 netif_set_mtu(ifunit, mtu);
232 if (pppol2tp_reorder_timeout > 0)
233 sprintf(&reorderto[0], "%d ", pppol2tp_reorder_timeout);
235 if (pppol2tp_tunnel_id > 0)
236 sprintf(&tid[0], "%hu ", pppol2tp_tunnel_id);
238 if (pppol2tp_session_id > 0)
239 sprintf(&sid[0], "%hu ", pppol2tp_session_id);
241 dbglog("PPPoL2TP options: %s%s%s%s%s%s%s%s%sdebugmask %d",
242 pppol2tp_recv_seq ? "recvseq " : "",
243 pppol2tp_send_seq ? "sendseq " : "",
244 pppol2tp_lns_mode ? "lnsmode " : "",
245 pppol2tp_reorder_timeout ? "reorderto " : "", reorderto,
246 pppol2tp_tunnel_id ? "tid " : "", tid,
247 pppol2tp_session_id ? "sid " : "", sid,
248 pppol2tp_debug_mask);
250 if (pppol2tp_recv_seq)
251 if (setsockopt(pppol2tp_fd, SOL_PPPOL2TP, PPPOL2TP_SO_RECVSEQ,
252 &on, sizeof(on)) < 0)
253 fatal("setsockopt(PPPOL2TP_RECVSEQ): %m");
254 if (pppol2tp_send_seq)
255 if (setsockopt(pppol2tp_fd, SOL_PPPOL2TP, PPPOL2TP_SO_SENDSEQ,
256 &on, sizeof(on)) < 0)
257 fatal("setsockopt(PPPOL2TP_SENDSEQ): %m");
258 if (pppol2tp_lns_mode)
259 if (setsockopt(pppol2tp_fd, SOL_PPPOL2TP, PPPOL2TP_SO_LNSMODE,
260 &on, sizeof(on)) < 0)
261 fatal("setsockopt(PPPOL2TP_LNSMODE): %m");
262 if (pppol2tp_reorder_timeout)
263 if (setsockopt(pppol2tp_fd, SOL_PPPOL2TP, PPPOL2TP_SO_REORDERTO,
264 &pppol2tp_reorder_timeout,
265 sizeof(pppol2tp_reorder_timeout)) < 0)
266 fatal("setsockopt(PPPOL2TP_REORDERTO): %m");
267 if (pppol2tp_debug_mask)
268 if (setsockopt(pppol2tp_fd, SOL_PPPOL2TP, PPPOL2TP_SO_DEBUG,
269 &pppol2tp_debug_mask, sizeof(pppol2tp_debug_mask)) < 0)
270 fatal("setsockopt(PPPOL2TP_DEBUG): %m");
273 static void recv_config_pppol2tp(int mru,
278 if ((lcp_allowoptions[0].mru > 0) && (mru > lcp_allowoptions[0].mru)) {
279 warn("Overriding mru %d to mtu value %d", mru,
280 lcp_allowoptions[0].mru);
281 mru = lcp_allowoptions[0].mru;
283 if ((ifunit >= 0) && ioctl(pppol2tp_fd, PPPIOCSMRU, (caddr_t) &mru) < 0)
284 error("Couldn't set PPP MRU: %m");
287 /*****************************************************************************
288 * Snoop LCP message exchanges to capture negotiated ACCM values.
289 * When asyncmap values have been seen from both sides, give the values to
291 * This code is derived from Roaring Penguin L2TP.
292 *****************************************************************************/
294 static void pppol2tp_lcp_snoop(unsigned char *buf, int len, int incoming)
296 static bool got_send_accm = 0;
297 static bool got_recv_accm = 0;
298 static uint32_t recv_accm = 0xffffffff;
299 static uint32_t send_accm = 0xffffffff;
300 static bool snooping = 1;
303 uint16_t lcp_pkt_len;
306 unsigned char const *opt_data;
309 /* Skip HDLC header */
313 /* Unreasonably short frame?? */
314 if (len <= 0) return;
318 /* Compressed protcol field */
321 protocol = ((unsigned int) buf[0]) * 256 + buf[1];
324 /* If it's a network protocol, stop snooping */
325 if (protocol <= 0x3fff) {
326 if (pppol2tp_debug_mask & PPPOL2TP_MSG_DEBUG) {
327 dbglog("Turning off snooping: "
328 "Network protocol %04x found.",
335 /* If it's not LCP, do not snoop */
336 if (protocol != 0xc021) {
340 /* Skip protocol; go to packet data */
344 /* Unreasonably short frame?? */
345 if (len <= 0) return;
347 /* Look for Configure-Ack or Configure-Reject code */
348 if (buf[0] != CONFACK && buf[0] != CONFREJ) return;
350 reject = (buf[0] == CONFREJ);
352 lcp_pkt_len = ((unsigned int) buf[2]) * 256 + buf[3];
354 /* Something fishy with length field? */
355 if (lcp_pkt_len > len) return;
357 /* Skip to options */
358 len = lcp_pkt_len - 4;
362 /* Pull off an option */
366 if (opt_len > len || opt_len < 2) break;
369 if (pppol2tp_debug_mask & PPPOL2TP_MSG_DEBUG) {
370 dbglog("Found option type %02x; len %d", opt, opt_len);
373 /* We are specifically interested in ACCM */
374 if (opt == CI_ASYNCMAP && opt_len == 0x06) {
376 /* ACCM negotiation REJECTED; use default */
378 if (pppol2tp_debug_mask & PPPOL2TP_MSG_DATA) {
379 dbglog("Rejected ACCM negotiation; "
381 incoming ? "incoming" : "outgoing");
388 memcpy(&accm, opt_data, sizeof(accm));
389 if (pppol2tp_debug_mask & PPPOL2TP_MSG_DATA) {
390 dbglog("Found ACCM of %08x (%s)", accm,
391 incoming ? "incoming" : "outgoing");
402 if (got_recv_accm && got_send_accm) {
403 if (pppol2tp_debug_mask & PPPOL2TP_MSG_CONTROL) {
404 dbglog("Telling L2TP: Send ACCM = %08x; "
405 "Receive ACCM = %08x", send_accm, recv_accm);
407 if (pppol2tp_send_accm_hook != NULL) {
408 (*pppol2tp_send_accm_hook)(pppol2tp_tunnel_id,
410 send_accm, recv_accm);
419 static void pppol2tp_lcp_snoop_recv(unsigned char *p, int len)
421 if (old_snoop_recv_hook != NULL)
422 (*old_snoop_recv_hook)(p, len);
423 pppol2tp_lcp_snoop(p, len, 1);
426 static void pppol2tp_lcp_snoop_send(unsigned char *p, int len)
428 if (old_snoop_send_hook != NULL)
429 (*old_snoop_send_hook)(p, len);
430 pppol2tp_lcp_snoop(p, len, 0);
433 /*****************************************************************************
434 * Interface up/down events
435 *****************************************************************************/
437 static void pppol2tp_ip_up(void *opaque, int arg)
439 /* may get called twice (for IPv4 and IPv6) but the hook handles that well */
440 if (pppol2tp_ip_updown_hook != NULL) {
441 (*pppol2tp_ip_updown_hook)(pppol2tp_tunnel_id,
442 pppol2tp_session_id, 1);
446 static void pppol2tp_ip_down(void *opaque, int arg)
448 /* may get called twice (for IPv4 and IPv6) but the hook handles that well */
449 if (pppol2tp_ip_updown_hook != NULL) {
450 (*pppol2tp_ip_updown_hook)(pppol2tp_tunnel_id,
451 pppol2tp_session_id, 0);
455 /*****************************************************************************
457 *****************************************************************************/
459 static void pppol2tp_check_options(void)
461 /* Enable LCP snooping for ACCM options only for LNS */
462 if (pppol2tp_lns_mode) {
463 if ((pppol2tp_tunnel_id == 0) || (pppol2tp_session_id == 0)) {
464 fatal("tunnel_id/session_id values not specified");
466 if (pppol2tp_debug_mask & PPPOL2TP_MSG_CONTROL) {
467 dbglog("Enabling LCP snooping");
469 old_snoop_recv_hook = snoop_recv_hook;
470 old_snoop_send_hook = snoop_send_hook;
472 snoop_recv_hook = pppol2tp_lcp_snoop_recv;
473 snoop_send_hook = pppol2tp_lcp_snoop_send;
477 /* Called just before pppd exits.
479 static void pppol2tp_cleanup(void)
481 if (pppol2tp_debug_mask & PPPOL2TP_MSG_DEBUG) {
482 dbglog("pppol2tp: exiting.");
484 disconnect_pppol2tp();
487 void plugin_init(void)
489 #if defined(__linux__)
490 extern int new_style_driver; /* From sys-linux.c */
491 if (!ppp_available() && !new_style_driver)
492 fatal("Kernel doesn't support ppp_generic - "
493 "needed for PPPoL2TP");
495 fatal("No PPPoL2TP support on this OS");
497 add_options(pppol2tp_options);
499 /* Hook up ip up/down notifiers to send indicator to openl2tpd
500 * that the link is up
502 add_notifier(&ip_up_notifier, pppol2tp_ip_up, NULL);
503 add_notifier(&ip_down_notifier, pppol2tp_ip_down, NULL);
504 add_notifier(&ipv6_up_notifier, pppol2tp_ip_up, NULL);
505 add_notifier(&ipv6_down_notifier, pppol2tp_ip_down, NULL);
508 struct channel pppol2tp_channel = {
509 options: pppol2tp_options,
510 process_extra_options: NULL,
511 check_options: &pppol2tp_check_options,
512 connect: &connect_pppol2tp,
513 disconnect: &disconnect_pppol2tp,
514 establish_ppp: &generic_establish_ppp,
515 disestablish_ppp: &generic_disestablish_ppp,
516 send_config: &send_config_pppol2tp,
517 recv_config: &recv_config_pppol2tp,