Home | History | Annotate | Line # | Download | only in pppol2tp
pppol2tp.c revision 1.1.1.2
      1      1.1  christos /* pppol2tp.c - pppd plugin to implement PPPoL2TP protocol
      2      1.1  christos  *   for Linux using kernel pppol2tp support.
      3      1.1  christos  *
      4      1.1  christos  * Requires kernel pppol2tp driver which is integrated into the kernel
      5      1.1  christos  * from 2.6.23 onwards. For earlier kernels, a version can be obtained
      6      1.1  christos  * from the OpenL2TP project at
      7      1.1  christos  * http://www.sourceforge.net/projects/openl2tp/
      8      1.1  christos  *
      9      1.1  christos  * Original by Martijn van Oosterhout <kleptog (at) svana.org>
     10      1.1  christos  * Modified by jchapman (at) katalix.com
     11      1.1  christos  *
     12      1.1  christos  * Heavily based upon pppoatm.c: original notice follows
     13      1.1  christos  *
     14      1.1  christos  * Copyright 2000 Mitchell Blank Jr.
     15      1.1  christos  * Based in part on work from Jens Axboe and Paul Mackerras.
     16      1.1  christos  * Updated to ppp-2.4.1 by Bernhard Kaindl
     17      1.1  christos  *
     18      1.1  christos  *  This program is free software; you can redistribute it and/or
     19      1.1  christos  *  modify it under the terms of the GNU General Public License
     20      1.1  christos  *  as published by the Free Software Foundation; either version
     21      1.1  christos  *  2 of the License, or (at your option) any later version.
     22      1.1  christos  */
     23      1.1  christos #include <unistd.h>
     24      1.1  christos #include <string.h>
     25      1.1  christos #include <stdlib.h>
     26      1.1  christos #include <errno.h>
     27      1.1  christos #include "pppd.h"
     28      1.1  christos #include "pathnames.h"
     29      1.1  christos #include "fsm.h"
     30      1.1  christos #include "lcp.h"
     31      1.1  christos #include "ccp.h"
     32      1.1  christos #include "ipcp.h"
     33      1.1  christos #include <sys/stat.h>
     34      1.1  christos #include <net/if.h>
     35      1.1  christos #include <sys/ioctl.h>
     36      1.1  christos #include <sys/socket.h>
     37      1.1  christos #include <netinet/in.h>
     38      1.1  christos #include <signal.h>
     39      1.1  christos #include <linux/version.h>
     40      1.1  christos #include <linux/sockios.h>
     41      1.1  christos #ifndef aligned_u64
     42      1.1  christos /* should be defined in sys/types.h */
     43      1.1  christos #define aligned_u64 unsigned long long __attribute__((aligned(8)))
     44      1.1  christos #endif
     45      1.1  christos #include <linux/types.h>
     46      1.1  christos #include <linux/if_ether.h>
     47      1.1  christos #include <linux/ppp_defs.h>
     48      1.1  christos #include <linux/if_ppp.h>
     49      1.1  christos #include <linux/if_pppox.h>
     50      1.1  christos #include <linux/if_pppol2tp.h>
     51      1.1  christos 
     52      1.1  christos /* should be added to system's socket.h... */
     53      1.1  christos #ifndef SOL_PPPOL2TP
     54      1.1  christos #define SOL_PPPOL2TP	273
     55      1.1  christos #endif
     56      1.1  christos 
     57      1.1  christos const char pppd_version[] = VERSION;
     58      1.1  christos 
     59      1.1  christos static int setdevname_pppol2tp(char **argv);
     60      1.1  christos 
     61      1.1  christos static int pppol2tp_fd = -1;
     62      1.1  christos static char *pppol2tp_fd_str;
     63      1.1  christos static bool pppol2tp_lns_mode = 0;
     64      1.1  christos static bool pppol2tp_recv_seq = 0;
     65      1.1  christos static bool pppol2tp_send_seq = 0;
     66      1.1  christos static int pppol2tp_debug_mask = 0;
     67      1.1  christos static int pppol2tp_reorder_timeout = 0;
     68      1.1  christos static char pppol2tp_ifname[32] = { 0, };
     69      1.1  christos int pppol2tp_tunnel_id = 0;
     70      1.1  christos int pppol2tp_session_id = 0;
     71      1.1  christos 
     72      1.1  christos static int device_got_set = 0;
     73      1.1  christos struct channel pppol2tp_channel;
     74      1.1  christos 
     75      1.1  christos static void (*old_snoop_recv_hook)(unsigned char *p, int len) = NULL;
     76      1.1  christos static void (*old_snoop_send_hook)(unsigned char *p, int len) = NULL;
     77      1.1  christos 
     78      1.1  christos /* Hook provided to allow other plugins to handle ACCM changes */
     79      1.1  christos void (*pppol2tp_send_accm_hook)(int tunnel_id, int session_id,
     80      1.1  christos 				uint32_t send_accm, uint32_t recv_accm) = NULL;
     81      1.1  christos 
     82      1.1  christos /* Hook provided to allow other plugins to handle IP up/down */
     83      1.1  christos void (*pppol2tp_ip_updown_hook)(int tunnel_id, int session_id, int up) = NULL;
     84      1.1  christos 
     85      1.1  christos static option_t pppol2tp_options[] = {
     86      1.1  christos 	{ "pppol2tp", o_special, &setdevname_pppol2tp,
     87      1.1  christos 	  "FD for PPPoL2TP socket", OPT_DEVNAM | OPT_A2STRVAL,
     88      1.1  christos           &pppol2tp_fd_str },
     89      1.1  christos 	{ "pppol2tp_lns_mode", o_bool, &pppol2tp_lns_mode,
     90      1.1  christos 	  "PPPoL2TP LNS behavior. Default off.",
     91      1.1  christos 	  OPT_PRIO | OPRIO_CFGFILE },
     92      1.1  christos 	{ "pppol2tp_send_seq", o_bool, &pppol2tp_send_seq,
     93      1.1  christos 	  "PPPoL2TP enable sequence numbers in transmitted data packets. "
     94      1.1  christos 	  "Default off.",
     95      1.1  christos 	  OPT_PRIO | OPRIO_CFGFILE },
     96      1.1  christos 	{ "pppol2tp_recv_seq", o_bool, &pppol2tp_recv_seq,
     97      1.1  christos 	  "PPPoL2TP enforce sequence numbers in received data packets. "
     98      1.1  christos 	  "Default off.",
     99      1.1  christos 	  OPT_PRIO | OPRIO_CFGFILE },
    100      1.1  christos 	{ "pppol2tp_reorderto", o_int, &pppol2tp_reorder_timeout,
    101      1.1  christos 	  "PPPoL2TP data packet reorder timeout. Default 0 (no reordering).",
    102      1.1  christos 	  OPT_PRIO },
    103      1.1  christos 	{ "pppol2tp_debug_mask", o_int, &pppol2tp_debug_mask,
    104      1.1  christos 	  "PPPoL2TP debug mask. Default: no debug.",
    105      1.1  christos 	  OPT_PRIO },
    106      1.1  christos 	{ "pppol2tp_ifname", o_string, &pppol2tp_ifname,
    107      1.1  christos 	  "Set interface name of PPP interface",
    108      1.1  christos 	  OPT_PRIO | OPT_PRIV | OPT_STATIC, NULL, 16 },
    109      1.1  christos 	{ "pppol2tp_tunnel_id", o_int, &pppol2tp_tunnel_id,
    110      1.1  christos 	  "PPPoL2TP tunnel_id.",
    111      1.1  christos 	  OPT_PRIO },
    112      1.1  christos 	{ "pppol2tp_session_id", o_int, &pppol2tp_session_id,
    113      1.1  christos 	  "PPPoL2TP session_id.",
    114      1.1  christos 	  OPT_PRIO },
    115      1.1  christos 	{ NULL }
    116      1.1  christos };
    117      1.1  christos 
    118      1.1  christos static int setdevname_pppol2tp(char **argv)
    119      1.1  christos {
    120      1.1  christos 	union {
    121      1.1  christos 		char buffer[128];
    122      1.1  christos 		struct sockaddr pppol2tp;
    123      1.1  christos 	} s;
    124      1.1  christos 	int len = sizeof(s);
    125      1.1  christos 	char **a;
    126      1.1  christos 	int tmp;
    127      1.1  christos 	int tmp_len = sizeof(tmp);
    128      1.1  christos 
    129      1.1  christos 	if (device_got_set)
    130      1.1  christos 		return 0;
    131      1.1  christos 
    132      1.1  christos 	if (!int_option(*argv, &pppol2tp_fd))
    133      1.1  christos 		return 0;
    134      1.1  christos 
    135      1.1  christos 	if(getsockname(pppol2tp_fd, (struct sockaddr *)&s, &len) < 0) {
    136      1.1  christos 		fatal("Given FD for PPPoL2TP socket invalid (%s)",
    137      1.1  christos 		      strerror(errno));
    138      1.1  christos 	}
    139      1.1  christos 	if(s.pppol2tp.sa_family != AF_PPPOX) {
    140      1.1  christos 		fatal("Socket of not a PPPoX socket");
    141      1.1  christos 	}
    142      1.1  christos 
    143      1.1  christos 	/* Do a test getsockopt() to ensure that the kernel has the necessary
    144      1.1  christos 	 * feature available.
    145      1.1  christos 	 */
    146      1.1  christos 	if (getsockopt(pppol2tp_fd, SOL_PPPOL2TP, PPPOL2TP_SO_DEBUG,
    147      1.1  christos 		       &tmp, &tmp_len) < 0) {
    148      1.1  christos 		fatal("PPPoL2TP kernel driver not installed");
    149      1.1  christos 	}
    150      1.1  christos 
    151      1.1  christos 	/* Setup option defaults. Compression options are disabled! */
    152      1.1  christos 
    153      1.1  christos 	modem = 0;
    154      1.1  christos 
    155      1.1  christos 	lcp_allowoptions[0].neg_accompression = 1;
    156      1.1  christos 	lcp_wantoptions[0].neg_accompression = 0;
    157      1.1  christos 
    158      1.1  christos 	lcp_allowoptions[0].neg_pcompression = 1;
    159      1.1  christos 	lcp_wantoptions[0].neg_pcompression = 0;
    160      1.1  christos 
    161      1.1  christos 	ccp_allowoptions[0].deflate = 0;
    162      1.1  christos 	ccp_wantoptions[0].deflate = 0;
    163      1.1  christos 
    164      1.1  christos 	ipcp_allowoptions[0].neg_vj = 0;
    165      1.1  christos 	ipcp_wantoptions[0].neg_vj = 0;
    166      1.1  christos 
    167      1.1  christos 	ccp_allowoptions[0].bsd_compress = 0;
    168      1.1  christos 	ccp_wantoptions[0].bsd_compress = 0;
    169      1.1  christos 
    170      1.1  christos 	the_channel = &pppol2tp_channel;
    171      1.1  christos 	device_got_set = 1;
    172      1.1  christos 
    173      1.1  christos 	return 1;
    174      1.1  christos }
    175      1.1  christos 
    176      1.1  christos static int connect_pppol2tp(void)
    177      1.1  christos {
    178      1.1  christos 	if(pppol2tp_fd == -1) {
    179      1.1  christos 		fatal("No PPPoL2TP FD specified");
    180      1.1  christos 	}
    181      1.1  christos 
    182      1.1  christos 	return pppol2tp_fd;
    183      1.1  christos }
    184      1.1  christos 
    185      1.1  christos static void disconnect_pppol2tp(void)
    186      1.1  christos {
    187      1.1  christos 	if (pppol2tp_fd >= 0) {
    188      1.1  christos 		close(pppol2tp_fd);
    189      1.1  christos 		pppol2tp_fd = -1;
    190      1.1  christos 	}
    191      1.1  christos }
    192      1.1  christos 
    193      1.1  christos static void send_config_pppol2tp(int mtu,
    194      1.1  christos 			      u_int32_t asyncmap,
    195      1.1  christos 			      int pcomp,
    196      1.1  christos 			      int accomp)
    197      1.1  christos {
    198      1.1  christos 	struct ifreq ifr;
    199      1.1  christos 	int on = 1;
    200      1.1  christos 	int fd;
    201      1.1  christos 	char reorderto[16];
    202      1.1  christos 	char tid[8];
    203      1.1  christos 	char sid[8];
    204      1.1  christos 
    205      1.1  christos 	if (pppol2tp_ifname[0]) {
    206      1.1  christos 		struct ifreq ifr;
    207      1.1  christos 		int fd;
    208      1.1  christos 
    209      1.1  christos 		fd = socket(AF_INET, SOCK_DGRAM, 0);
    210      1.1  christos 		if (fd >= 0) {
    211      1.1  christos 			memset (&ifr, '\0', sizeof (ifr));
    212      1.1  christos 			strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
    213      1.1  christos 			strlcpy(ifr.ifr_newname, pppol2tp_ifname,
    214      1.1  christos 				sizeof(ifr.ifr_name));
    215      1.1  christos 			ioctl(fd, SIOCSIFNAME, (caddr_t) &ifr);
    216      1.1  christos 			strlcpy(ifname, pppol2tp_ifname, 32);
    217      1.1  christos 			if (pppol2tp_debug_mask & PPPOL2TP_MSG_CONTROL) {
    218      1.1  christos 				dbglog("ppp%d: interface name %s",
    219      1.1  christos 				       ifunit, ifname);
    220      1.1  christos 			}
    221      1.1  christos 		}
    222      1.1  christos 		close(fd);
    223      1.1  christos 	}
    224      1.1  christos 
    225      1.1  christos 	if ((lcp_allowoptions[0].mru > 0) && (mtu > lcp_allowoptions[0].mru)) {
    226      1.1  christos 		warn("Overriding mtu %d to %d", mtu, lcp_allowoptions[0].mru);
    227      1.1  christos 		mtu = lcp_allowoptions[0].mru;
    228      1.1  christos 	}
    229      1.1  christos 	netif_set_mtu(ifunit, mtu);
    230      1.1  christos 
    231      1.1  christos 	reorderto[0] = '\0';
    232      1.1  christos 	if (pppol2tp_reorder_timeout > 0)
    233      1.1  christos 		sprintf(&reorderto[0], "%d ", pppol2tp_reorder_timeout);
    234      1.1  christos 	tid[0] = '\0';
    235      1.1  christos 	if (pppol2tp_tunnel_id > 0)
    236      1.1  christos 		sprintf(&tid[0], "%hu ", pppol2tp_tunnel_id);
    237      1.1  christos 	sid[0] = '\0';
    238      1.1  christos 	if (pppol2tp_session_id > 0)
    239      1.1  christos 		sprintf(&sid[0], "%hu ", pppol2tp_session_id);
    240      1.1  christos 
    241      1.1  christos 	dbglog("PPPoL2TP options: %s%s%s%s%s%s%s%s%sdebugmask %d",
    242      1.1  christos 	       pppol2tp_recv_seq ? "recvseq " : "",
    243      1.1  christos 	       pppol2tp_send_seq ? "sendseq " : "",
    244      1.1  christos 	       pppol2tp_lns_mode ? "lnsmode " : "",
    245      1.1  christos 	       pppol2tp_reorder_timeout ? "reorderto " : "", reorderto,
    246      1.1  christos 	       pppol2tp_tunnel_id ? "tid " : "", tid,
    247      1.1  christos 	       pppol2tp_session_id ? "sid " : "", sid,
    248      1.1  christos 	       pppol2tp_debug_mask);
    249      1.1  christos 
    250      1.1  christos 	if (pppol2tp_recv_seq)
    251      1.1  christos 		if (setsockopt(pppol2tp_fd, SOL_PPPOL2TP, PPPOL2TP_SO_RECVSEQ,
    252      1.1  christos 			       &on, sizeof(on)) < 0)
    253      1.1  christos 			fatal("setsockopt(PPPOL2TP_RECVSEQ): %m");
    254      1.1  christos 	if (pppol2tp_send_seq)
    255      1.1  christos 		if (setsockopt(pppol2tp_fd, SOL_PPPOL2TP, PPPOL2TP_SO_SENDSEQ,
    256      1.1  christos 			       &on, sizeof(on)) < 0)
    257      1.1  christos 			fatal("setsockopt(PPPOL2TP_SENDSEQ): %m");
    258      1.1  christos 	if (pppol2tp_lns_mode)
    259      1.1  christos 		if (setsockopt(pppol2tp_fd, SOL_PPPOL2TP, PPPOL2TP_SO_LNSMODE,
    260      1.1  christos 			       &on, sizeof(on)) < 0)
    261      1.1  christos 			fatal("setsockopt(PPPOL2TP_LNSMODE): %m");
    262      1.1  christos 	if (pppol2tp_reorder_timeout)
    263      1.1  christos 		if (setsockopt(pppol2tp_fd, SOL_PPPOL2TP, PPPOL2TP_SO_REORDERTO,
    264      1.1  christos 			       &pppol2tp_reorder_timeout,
    265      1.1  christos 			       sizeof(pppol2tp_reorder_timeout)) < 0)
    266      1.1  christos 			fatal("setsockopt(PPPOL2TP_REORDERTO): %m");
    267      1.1  christos 	if (pppol2tp_debug_mask)
    268      1.1  christos 		if (setsockopt(pppol2tp_fd, SOL_PPPOL2TP, PPPOL2TP_SO_DEBUG,
    269      1.1  christos 			       &pppol2tp_debug_mask, sizeof(pppol2tp_debug_mask)) < 0)
    270      1.1  christos 			fatal("setsockopt(PPPOL2TP_DEBUG): %m");
    271      1.1  christos }
    272      1.1  christos 
    273      1.1  christos static void recv_config_pppol2tp(int mru,
    274      1.1  christos 			      u_int32_t asyncmap,
    275      1.1  christos 			      int pcomp,
    276      1.1  christos 			      int accomp)
    277      1.1  christos {
    278      1.1  christos 	if ((lcp_allowoptions[0].mru > 0) && (mru > lcp_allowoptions[0].mru)) {
    279      1.1  christos 		warn("Overriding mru %d to mtu value %d", mru,
    280      1.1  christos 		     lcp_allowoptions[0].mru);
    281      1.1  christos 		mru = lcp_allowoptions[0].mru;
    282      1.1  christos 	}
    283      1.1  christos 	if ((ifunit >= 0) && ioctl(pppol2tp_fd, PPPIOCSMRU, (caddr_t) &mru) < 0)
    284      1.1  christos 		error("Couldn't set PPP MRU: %m");
    285      1.1  christos }
    286      1.1  christos 
    287      1.1  christos /*****************************************************************************
    288      1.1  christos  * Snoop LCP message exchanges to capture negotiated ACCM values.
    289      1.1  christos  * When asyncmap values have been seen from both sides, give the values to
    290      1.1  christos  * L2TP.
    291      1.1  christos  * This code is derived from Roaring Penguin L2TP.
    292      1.1  christos  *****************************************************************************/
    293      1.1  christos 
    294      1.1  christos static void pppol2tp_lcp_snoop(unsigned char *buf, int len, int incoming)
    295      1.1  christos {
    296      1.1  christos 	static bool got_send_accm = 0;
    297      1.1  christos 	static bool got_recv_accm = 0;
    298      1.1  christos 	static uint32_t recv_accm = 0xffffffff;
    299      1.1  christos 	static uint32_t send_accm = 0xffffffff;
    300      1.1  christos 	static bool snooping = 1;
    301      1.1  christos 
    302      1.1  christos 	uint16_t protocol;
    303      1.1  christos 	uint16_t lcp_pkt_len;
    304      1.1  christos 	int opt, opt_len;
    305      1.1  christos 	int reject;
    306      1.1  christos 	unsigned char const *opt_data;
    307      1.1  christos 	uint32_t accm;
    308      1.1  christos 
    309      1.1  christos 	/* Skip HDLC header */
    310      1.1  christos 	buf += 2;
    311      1.1  christos 	len -= 2;
    312      1.1  christos 
    313      1.1  christos 	/* Unreasonably short frame?? */
    314      1.1  christos 	if (len <= 0) return;
    315      1.1  christos 
    316      1.1  christos 	/* Get protocol */
    317      1.1  christos 	if (buf[0] & 0x01) {
    318      1.1  christos 		/* Compressed protcol field */
    319      1.1  christos 		protocol = buf[0];
    320      1.1  christos 	} else {
    321      1.1  christos 		protocol = ((unsigned int) buf[0]) * 256 + buf[1];
    322      1.1  christos 	}
    323      1.1  christos 
    324      1.1  christos 	/* If it's a network protocol, stop snooping */
    325      1.1  christos 	if (protocol <= 0x3fff) {
    326      1.1  christos 		if (pppol2tp_debug_mask & PPPOL2TP_MSG_DEBUG) {
    327      1.1  christos 			dbglog("Turning off snooping: "
    328      1.1  christos 			       "Network protocol %04x found.",
    329      1.1  christos 			       protocol);
    330      1.1  christos 		}
    331      1.1  christos 		snooping = 0;
    332      1.1  christos 		return;
    333      1.1  christos 	}
    334      1.1  christos 
    335      1.1  christos 	/* If it's not LCP, do not snoop */
    336      1.1  christos 	if (protocol != 0xc021) {
    337      1.1  christos 		return;
    338      1.1  christos 	}
    339      1.1  christos 
    340      1.1  christos 	/* Skip protocol; go to packet data */
    341      1.1  christos 	buf += 2;
    342      1.1  christos 	len -= 2;
    343      1.1  christos 
    344      1.1  christos 	/* Unreasonably short frame?? */
    345      1.1  christos 	if (len <= 0) return;
    346      1.1  christos 
    347      1.1  christos 	/* Look for Configure-Ack or Configure-Reject code */
    348      1.1  christos 	if (buf[0] != CONFACK && buf[0] != CONFREJ) return;
    349      1.1  christos 
    350      1.1  christos 	reject = (buf[0] == CONFREJ);
    351      1.1  christos 
    352      1.1  christos 	lcp_pkt_len = ((unsigned int) buf[2]) * 256 + buf[3];
    353      1.1  christos 
    354      1.1  christos 	/* Something fishy with length field? */
    355      1.1  christos 	if (lcp_pkt_len > len) return;
    356      1.1  christos 
    357      1.1  christos 	/* Skip to options */
    358      1.1  christos 	len = lcp_pkt_len - 4;
    359      1.1  christos 	buf += 4;
    360      1.1  christos 
    361      1.1  christos 	while (len > 0) {
    362      1.1  christos 		/* Pull off an option */
    363      1.1  christos 		opt = buf[0];
    364      1.1  christos 		opt_len = buf[1];
    365      1.1  christos 		opt_data = &buf[2];
    366      1.1  christos 		if (opt_len > len || opt_len < 2) break;
    367      1.1  christos 		len -= opt_len;
    368      1.1  christos 		buf += opt_len;
    369      1.1  christos 		if (pppol2tp_debug_mask & PPPOL2TP_MSG_DEBUG) {
    370      1.1  christos 			dbglog("Found option type %02x; len %d", opt, opt_len);
    371      1.1  christos 		}
    372      1.1  christos 
    373      1.1  christos 		/* We are specifically interested in ACCM */
    374      1.1  christos 		if (opt == CI_ASYNCMAP && opt_len == 0x06) {
    375      1.1  christos 			if (reject) {
    376      1.1  christos 				/* ACCM negotiation REJECTED; use default */
    377      1.1  christos 				accm = 0xffffffff;
    378      1.1  christos 				if (pppol2tp_debug_mask & PPPOL2TP_MSG_DATA) {
    379      1.1  christos 					dbglog("Rejected ACCM negotiation; "
    380      1.1  christos 					       "defaulting (%s)",
    381      1.1  christos 					       incoming ? "incoming" : "outgoing");
    382      1.1  christos 				}
    383      1.1  christos 				recv_accm = accm;
    384      1.1  christos 				send_accm = accm;
    385      1.1  christos 				got_recv_accm = 1;
    386      1.1  christos 				got_send_accm = 1;
    387      1.1  christos 			} else {
    388      1.1  christos 				memcpy(&accm, opt_data, sizeof(accm));
    389      1.1  christos 				if (pppol2tp_debug_mask & PPPOL2TP_MSG_DATA) {
    390      1.1  christos 					dbglog("Found ACCM of %08x (%s)", accm,
    391      1.1  christos 					       incoming ? "incoming" : "outgoing");
    392      1.1  christos 				}
    393      1.1  christos 				if (incoming) {
    394      1.1  christos 					recv_accm = accm;
    395      1.1  christos 					got_recv_accm = 1;
    396      1.1  christos 				} else {
    397      1.1  christos 					send_accm = accm;
    398      1.1  christos 					got_send_accm = 1;
    399      1.1  christos 				}
    400      1.1  christos 			}
    401      1.1  christos 
    402      1.1  christos 			if (got_recv_accm && got_send_accm) {
    403      1.1  christos 				if (pppol2tp_debug_mask & PPPOL2TP_MSG_CONTROL) {
    404      1.1  christos 					dbglog("Telling L2TP: Send ACCM = %08x; "
    405      1.1  christos 					       "Receive ACCM = %08x", send_accm, recv_accm);
    406      1.1  christos 				}
    407      1.1  christos 				if (pppol2tp_send_accm_hook != NULL) {
    408      1.1  christos 					(*pppol2tp_send_accm_hook)(pppol2tp_tunnel_id,
    409      1.1  christos 								   pppol2tp_session_id,
    410      1.1  christos 								   send_accm, recv_accm);
    411      1.1  christos 				}
    412      1.1  christos 				got_recv_accm = 0;
    413      1.1  christos 				got_send_accm = 0;
    414      1.1  christos 			}
    415      1.1  christos 		}
    416      1.1  christos 	}
    417      1.1  christos }
    418      1.1  christos 
    419      1.1  christos static void pppol2tp_lcp_snoop_recv(unsigned char *p, int len)
    420      1.1  christos {
    421      1.1  christos 	if (old_snoop_recv_hook != NULL)
    422      1.1  christos 		(*old_snoop_recv_hook)(p, len);
    423      1.1  christos 	pppol2tp_lcp_snoop(p, len, 1);
    424      1.1  christos }
    425      1.1  christos 
    426      1.1  christos static void pppol2tp_lcp_snoop_send(unsigned char *p, int len)
    427      1.1  christos {
    428      1.1  christos 	if (old_snoop_send_hook != NULL)
    429      1.1  christos 		(*old_snoop_send_hook)(p, len);
    430      1.1  christos 	pppol2tp_lcp_snoop(p, len, 0);
    431      1.1  christos }
    432      1.1  christos 
    433      1.1  christos /*****************************************************************************
    434      1.1  christos  * Interface up/down events
    435      1.1  christos  *****************************************************************************/
    436      1.1  christos 
    437  1.1.1.2  christos static void pppol2tp_ip_up(void *opaque, int arg)
    438      1.1  christos {
    439  1.1.1.2  christos 	/* may get called twice (for IPv4 and IPv6) but the hook handles that well */
    440      1.1  christos 	if (pppol2tp_ip_updown_hook != NULL) {
    441      1.1  christos 		(*pppol2tp_ip_updown_hook)(pppol2tp_tunnel_id,
    442      1.1  christos 					   pppol2tp_session_id, 1);
    443      1.1  christos 	}
    444      1.1  christos }
    445      1.1  christos 
    446  1.1.1.2  christos static void pppol2tp_ip_down(void *opaque, int arg)
    447      1.1  christos {
    448  1.1.1.2  christos 	/* may get called twice (for IPv4 and IPv6) but the hook handles that well */
    449      1.1  christos 	if (pppol2tp_ip_updown_hook != NULL) {
    450      1.1  christos 		(*pppol2tp_ip_updown_hook)(pppol2tp_tunnel_id,
    451      1.1  christos 					   pppol2tp_session_id, 0);
    452      1.1  christos 	}
    453      1.1  christos }
    454      1.1  christos 
    455      1.1  christos /*****************************************************************************
    456      1.1  christos  * Application init
    457      1.1  christos  *****************************************************************************/
    458      1.1  christos 
    459      1.1  christos static void pppol2tp_check_options(void)
    460      1.1  christos {
    461      1.1  christos 	/* Enable LCP snooping for ACCM options only for LNS */
    462      1.1  christos 	if (pppol2tp_lns_mode) {
    463      1.1  christos 		if ((pppol2tp_tunnel_id == 0) || (pppol2tp_session_id == 0)) {
    464      1.1  christos 			fatal("tunnel_id/session_id values not specified");
    465      1.1  christos 		}
    466      1.1  christos 		if (pppol2tp_debug_mask & PPPOL2TP_MSG_CONTROL) {
    467      1.1  christos 			dbglog("Enabling LCP snooping");
    468      1.1  christos 		}
    469      1.1  christos 		old_snoop_recv_hook = snoop_recv_hook;
    470      1.1  christos 		old_snoop_send_hook = snoop_send_hook;
    471      1.1  christos 
    472      1.1  christos 		snoop_recv_hook = pppol2tp_lcp_snoop_recv;
    473      1.1  christos 		snoop_send_hook = pppol2tp_lcp_snoop_send;
    474      1.1  christos 	}
    475      1.1  christos }
    476      1.1  christos 
    477      1.1  christos /* Called just before pppd exits.
    478      1.1  christos  */
    479      1.1  christos static void pppol2tp_cleanup(void)
    480      1.1  christos {
    481      1.1  christos 	if (pppol2tp_debug_mask & PPPOL2TP_MSG_DEBUG) {
    482      1.1  christos 		dbglog("pppol2tp: exiting.");
    483      1.1  christos 	}
    484      1.1  christos 	disconnect_pppol2tp();
    485      1.1  christos }
    486      1.1  christos 
    487      1.1  christos void plugin_init(void)
    488      1.1  christos {
    489      1.1  christos #if defined(__linux__)
    490      1.1  christos 	extern int new_style_driver;	/* From sys-linux.c */
    491      1.1  christos 	if (!ppp_available() && !new_style_driver)
    492      1.1  christos 		fatal("Kernel doesn't support ppp_generic - "
    493      1.1  christos 		    "needed for PPPoL2TP");
    494      1.1  christos #else
    495      1.1  christos 	fatal("No PPPoL2TP support on this OS");
    496      1.1  christos #endif
    497      1.1  christos 	add_options(pppol2tp_options);
    498  1.1.1.2  christos 
    499  1.1.1.2  christos 	/* Hook up ip up/down notifiers to send indicator to openl2tpd
    500  1.1.1.2  christos 	 * that the link is up
    501  1.1.1.2  christos 	 */
    502  1.1.1.2  christos 	add_notifier(&ip_up_notifier, pppol2tp_ip_up, NULL);
    503  1.1.1.2  christos 	add_notifier(&ip_down_notifier, pppol2tp_ip_down, NULL);
    504  1.1.1.2  christos 	add_notifier(&ipv6_up_notifier, pppol2tp_ip_up, NULL);
    505  1.1.1.2  christos 	add_notifier(&ipv6_down_notifier, pppol2tp_ip_down, NULL);
    506      1.1  christos }
    507      1.1  christos 
    508      1.1  christos struct channel pppol2tp_channel = {
    509      1.1  christos     options: pppol2tp_options,
    510      1.1  christos     process_extra_options: NULL,
    511      1.1  christos     check_options: &pppol2tp_check_options,
    512      1.1  christos     connect: &connect_pppol2tp,
    513      1.1  christos     disconnect: &disconnect_pppol2tp,
    514      1.1  christos     establish_ppp: &generic_establish_ppp,
    515      1.1  christos     disestablish_ppp: &generic_disestablish_ppp,
    516      1.1  christos     send_config: &send_config_pppol2tp,
    517      1.1  christos     recv_config: &recv_config_pppol2tp,
    518      1.1  christos     close: NULL,
    519      1.1  christos     cleanup: NULL
    520      1.1  christos };
    521