added L2TPv2 (PPP over L2TP, a.k.a. UDP tunnels) support

Supported:
 - L2TPv2 (PPP over L2TP, a.k.a. UDP tunnels)
 - LAC

 Not supported:
 - LNS (require PPP server support)
 - L2TPv3 ethernet pseudowires
 - L2TPv3 VLAN pseudowire
 - L2TPv3 PPP pseudowires
 - L2TPv3 IP encapsulation
 - L2TPv3 IP pseudowire
 - L2TP tunnel switching - http://tools.ietf.org/html/draft-ietf-l2tpext-tunnel-switching-08
 - Multiple tunnels per UDP socket, as well as multiple sessions per tunnel
 - Hidden AVPs
This commit is contained in:
Sylvain Rochet 2012-07-09 23:25:32 +02:00
parent 0797ab6bc6
commit f534e80c71
11 changed files with 1505 additions and 15 deletions

View File

@ -142,6 +142,47 @@ int pppapi_over_ethernet_open(ppp_pcb *pcb, struct netif *ethif, const char *ser
#endif /* PPPOE_SUPPORT */
#if PPPOL2TP_SUPPORT
/**
* Call ppp_over_l2tp_open() inside the tcpip_thread context.
*/
static void pppapi_do_ppp_over_l2tp_open(struct pppapi_msg_msg *msg) {
msg->err = ppp_over_l2tp_open(msg->ppp, msg->msg.l2tpopen.ipaddr,
msg->msg.l2tpopen.port,
#if PPPOL2TP_AUTH_SUPPORT
msg->msg.l2tpopen.secret,
msg->msg.l2tpopen.secret_len,
#else /* PPPOL2TP_AUTH_SUPPORT */
NULL,
#endif /* PPPOL2TP_AUTH_SUPPORT */
msg->msg.l2tpopen.link_status_cb, msg->msg.l2tpopen.link_status_ctx);
TCPIP_PPPAPI_ACK(msg);
}
/**
* Call ppp_over_l2tp_open() in a thread-safe way by running that function inside the
* tcpip_thread context.
*/
int pppapi_over_l2tp_open(ppp_pcb *pcb, ip_addr_t *ipaddr, u16_t port, u8_t *secret, u8_t secret_len,
ppp_link_status_cb_fn link_status_cb, void *link_status_ctx) {
struct pppapi_msg msg;
msg.function = pppapi_do_ppp_over_l2tp_open;
msg.msg.ppp = pcb;
msg.msg.msg.l2tpopen.ipaddr = ipaddr;
msg.msg.msg.l2tpopen.port = port;
#if PPPOL2TP_AUTH_SUPPORT
msg.msg.msg.l2tpopen.secret = secret;
msg.msg.msg.l2tpopen.secret_len = secret_len;
#endif /* PPPOL2TP_AUTH_SUPPORT */
msg.msg.msg.l2tpopen.link_status_cb = link_status_cb;
msg.msg.msg.l2tpopen.link_status_ctx = link_status_ctx;
TCPIP_PPPAPI(&msg);
return msg.msg.err;
}
#endif /* PPPOL2TP_SUPPORT */
/**
* Call ppp_close() inside the tcpip_thread context.
*/

View File

@ -58,6 +58,7 @@
#include "lwip/snmp_msg.h"
#include "lwip/dns.h"
#include "netif/ppp_oe.h"
#include "netif/pppol2tp.h"
#include "lwip/nd6.h"
#include "lwip/ip6_frag.h"
#include "lwip/mld6.h"

View File

@ -89,11 +89,14 @@ LWIP_MEMPOOL(LOCALHOSTLIST, MEMP_NUM_LOCALHOSTLIST, LOCALHOSTLIST_ELEM_SIZE,
#endif /* LWIP_DNS && DNS_LOCAL_HOSTLIST && DNS_LOCAL_HOSTLIST_IS_DYNAMIC */
#if PPP_SUPPORT
LWIP_MEMPOOL(PPP_PCB, MEMP_NUM_PPP_PCB, sizeof(ppp_pcb), "PPP_PCB")
#endif /* PPP_SUPPORT && PPPOE_SUPPORT */
#if PPP_SUPPORT && PPPOE_SUPPORT
LWIP_MEMPOOL(PPPOE_IF, MEMP_NUM_PPPOE_INTERFACES, sizeof(struct pppoe_softc), "PPPOE_IF")
#endif /* PPP_SUPPORT && PPPOE_SUPPORT */
LWIP_MEMPOOL(PPP_PCB, MEMP_NUM_PPP_PCB, sizeof(ppp_pcb), "PPP_PCB")
#if PPPOE_SUPPORT
LWIP_MEMPOOL(PPPOE_IF, MEMP_NUM_PPPOE_INTERFACES, sizeof(struct pppoe_softc), "PPPOE_IF")
#endif /* PPPOE_SUPPORT */
#if PPPOL2TP_SUPPORT
LWIP_MEMPOOL(PPPOL2TP_PCB, MEMP_NUM_PPPOL2TP_INTERFACES, sizeof(pppol2tp_pcb), "PPPOL2TP_PCB")
#endif /* PPPOL2TP_SUPPORT */
#endif /* PPP_SUPPORT */
#if LWIP_IPV6 && LWIP_ND6_QUEUEING
LWIP_MEMPOOL(ND6_QUEUE, MEMP_NUM_ND6_QUEUE, sizeof(struct nd6_q_entry), "ND6_QUEUE")

View File

@ -415,6 +415,14 @@
#define MEMP_NUM_PPPOE_INTERFACES 1
#endif
/**
* MEMP_NUM_PPPOL2TP_INTERFACES: the number of concurrently active PPPoL2TP
* interfaces (only used with PPPOL2TP_SUPPORT==1)
*/
#ifndef MEMP_NUM_PPPOL2TP_INTERFACES
#define MEMP_NUM_PPPOL2TP_INTERFACES 1
#endif
/**
* PBUF_POOL_SIZE: the number of buffers in the pbuf pool.
*/
@ -1694,6 +1702,20 @@
#define PPPOE_SUPPORT 0
#endif
/**
* PPPOL2TP_SUPPORT==1: Enable PPP Over L2TP
*/
#ifndef PPPOL2TP_SUPPORT
#define PPPOL2TP_SUPPORT 0
#endif
/**
* PPPOL2TP_AUTH_SUPPORT==1: Enable PPP Over L2TP Auth (enable MD5 support)
*/
#ifndef PPPOL2TP_AUTH_SUPPORT
#define PPPOL2TP_AUTH_SUPPORT PPPOL2TP_SUPPORT
#endif
/**
* PPPOS_SUPPORT==1: Enable PPP Over Serial
*/
@ -1782,15 +1804,15 @@
#endif /* !PPPOS_SUPPORT */
/**
* PPP_MD5_RANDM==1: Use MD5 for better randomness. Automatically enabled if CHAP support is enabled.
* PPP_MD5_RANDM==1: Use MD5 for better randomness. Automatically enabled if CHAP or L2TP AUTH support is enabled.
*/
#ifndef PPP_MD5_RANDM
#define PPP_MD5_RANDM 0
#endif
#if CHAP_SUPPORT
#if CHAP_SUPPORT || PPPOL2TP_AUTH_SUPPORT
#undef PPP_MD5_RANDM
#define PPP_MD5_RANDM 1 /* MD5 Random is required for CHAP */
#endif /* CHAP_SUPPORT */
#define PPP_MD5_RANDM 1 /* MD5 Random is required for CHAP and L2TP AUTH */
#endif /* CHAP_SUPPORT || PPPOL2TP_AUTH_SUPPORT */
/**
* PolarSSL library, used if necessary and not previously disabled
@ -1825,11 +1847,11 @@
* using our cleaned PolarSSL library.
*/
#if CHAP_SUPPORT || EAP_SUPPORT || PPP_MD5_RANDM
#if CHAP_SUPPORT || EAP_SUPPORT || PPPOL2TP_AUTH_SUPPORT || PPP_MD5_RANDM
#ifndef LWIP_INCLUDED_POLARSSL_MD5
#define LWIP_INCLUDED_POLARSSL_MD5 1 /* CHAP, EAP and MD5 Random require MD5 support */
#define LWIP_INCLUDED_POLARSSL_MD5 1 /* CHAP, EAP, L2TP AUTH and MD5 Random require MD5 support */
#endif /* LWIP_INCLUDED_POLARSSL_MD5 */
#endif /* CHAP_SUPPORT || EAP_SUPPORT || PPP_MD5_RANDM */
#endif /* CHAP_SUPPORT || EAP_SUPPORT || PPPOL2TP_AUTH_SUPPORT || PPP_MD5_RANDM */
#if MSCHAP_SUPPORT
#ifndef LWIP_INCLUDED_POLARSSL_MD4

View File

@ -67,6 +67,18 @@ struct pppapi_msg_msg {
void *link_status_ctx;
} ethernetopen;
#endif /* PPPOE_SUPPORT */
#if PPPOL2TP_SUPPORT
struct {
ip_addr_t *ipaddr;
u16_t port;
#if PPPOL2TP_AUTH_SUPPORT
u8_t *secret;
u8_t secret_len;
#endif /* PPPOL2TP_AUTH_SUPPORT */
ppp_link_status_cb_fn link_status_cb;
void *link_status_ctx;
} l2tpopen;
#endif /* PPPOL2TP_SUPPORT */
struct {
int cmd;
void *arg;
@ -106,6 +118,10 @@ int pppapi_over_ethernet_open(ppp_pcb *pcb, struct netif *ethif, const char *ser
const char *concentrator_name, ppp_link_status_cb_fn link_status_cb,
void *link_status_ctx);
#endif /* PPPOE_SUPPORT */
#if PPPOL2TP_SUPPORT
int pppapi_over_l2tp_open(ppp_pcb *pcb, ip_addr_t *ipaddr, u16_t port, u8_t *secret, u8_t secret_len,
ppp_link_status_cb_fn link_status_cb, void *link_status_ctx);
#endif /* PPPOL2TP_SUPPORT */
int pppapi_close(ppp_pcb *pcb);
void pppapi_sighup(ppp_pcb *pcb);
int pppapi_ioctl(ppp_pcb *pcb, int cmd, void *arg);

View File

@ -73,8 +73,7 @@
#ifndef PPP_OE_H
#define PPP_OE_H
#include "ppp_impl.h"
#include "ppp.h"
#include "netif/etharp.h"
#ifdef PACK_STRUCT_USE_INCLUDES

View File

@ -0,0 +1,219 @@
/**
* @file
* Network Point to Point Protocol over Layer 2 Tunneling Protocol header file.
*
*/
/*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*
* This file is part of the lwIP TCP/IP stack.
*
*/
#include "lwip/opt.h"
#if PPP_SUPPORT && PPPOL2TP_SUPPORT /* don't build if not configured for use in lwipopts.h */
#ifndef PPPOL2TP_H_
#define PPPOL2TP_H_
#include "ppp.h"
/* Timeout */
#define PPPOL2TP_CONTROL_TIMEOUT (5*1000) /* base for quick timeout calculation */
#define PPPOL2TP_SLOW_RETRY (60*1000) /* persistent retry interval */
#define PPPOL2TP_MAXSCCRQ 4 /* retry SCCRQ four times (quickly) */
#define PPPOL2TP_MAXICRQ 4 /* retry IRCQ four times */
#define PPPOL2TP_MAXICCN 4 /* retry ICCN four times */
/* L2TP header flags */
#define PPPOL2TP_HEADERFLAG_CONTROL 0x8000
#define PPPOL2TP_HEADERFLAG_LENGTH 0x4000
#define PPPOL2TP_HEADERFLAG_SEQUENCE 0x0800
#define PPPOL2TP_HEADERFLAG_OFFSET 0x0200
#define PPPOL2TP_HEADERFLAG_PRIORITY 0x0100
#define PPPOL2TP_HEADERFLAG_VERSION 0x0002
/* Mandatory bits for control: Control, Length, Sequence, Version 2 */
#define PPPOL2TP_HEADERFLAG_CONTROL_MANDATORY (PPPOL2TP_HEADERFLAG_CONTROL|PPPOL2TP_HEADERFLAG_LENGTH|PPPOL2TP_HEADERFLAG_SEQUENCE|PPPOL2TP_HEADERFLAG_VERSION)
/* Forbidden bits for control: Offset, Priority */
#define PPPOL2TP_HEADERFLAG_CONTROL_FORBIDDEN (PPPOL2TP_HEADERFLAG_OFFSET|PPPOL2TP_HEADERFLAG_PRIORITY)
/* Mandatory bits for data: Version 2 */
#define PPPOL2TP_HEADERFLAG_DATA_MANDATORY (PPPOL2TP_HEADERFLAG_VERSION)
/* AVP (Attribute Value Pair) header */
#define PPPOL2TP_AVPHEADERFLAG_MANDATORY 0x8000
#define PPPOL2TP_AVPHEADERFLAG_HIDDEN 0x4000
#define PPPOL2TP_AVPHEADERFLAG_LENGTHMASK 0x03ff
/* -- AVP - Message type */
#define PPPOL2TP_AVPTYPE_MESSAGE 0 /* Message type */
/* Control Connection Management */
#define PPPOL2TP_MESSAGETYPE_SCCRQ 1 /* Start Control Connection Request */
#define PPPOL2TP_MESSAGETYPE_SCCRP 2 /* Start Control Connection Reply */
#define PPPOL2TP_MESSAGETYPE_SCCCN 3 /* Start Control Connection Connected */
#define PPPOL2TP_MESSAGETYPE_STOPCCN 4 /* Stop Control Connection Notification */
#define PPPOL2TP_MESSAGETYPE_HELLO 6 /* Hello */
/* Call Management */
#define PPPOL2TP_MESSAGETYPE_OCRQ 7 /* Outgoing Call Request */
#define PPPOL2TP_MESSAGETYPE_OCRP 8 /* Outgoing Call Reply */
#define PPPOL2TP_MESSAGETYPE_OCCN 9 /* Outgoing Call Connected */
#define PPPOL2TP_MESSAGETYPE_ICRQ 10 /* Incoming Call Request */
#define PPPOL2TP_MESSAGETYPE_ICRP 11 /* Incoming Call Reply */
#define PPPOL2TP_MESSAGETYPE_ICCN 12 /* Incoming Call Connected */
#define PPPOL2TP_MESSAGETYPE_CDN 14 /* Call Disconnect Notify */
/* Error reporting */
#define PPPOL2TP_MESSAGETYPE_WEN 15 /* WAN Error Notify */
/* PPP Session Control */
#define PPPOL2TP_MESSAGETYPE_SLI 16 /* Set Link Info */
/* -- AVP - Result code */
#define PPPOL2TP_AVPTYPE_RESULTCODE 1 /* Result code */
#define PPPOL2TP_RESULTCODE 1 /* General request to clear control connection */
/* -- AVP - Protocol version (!= L2TP Header version) */
#define PPPOL2TP_AVPTYPE_VERSION 2
#define PPPOL2TP_VERSION 0x0100 /* L2TP Protocol version 1, revision 0 */
/* -- AVP - Framing capabilities */
#define PPPOL2TP_AVPTYPE_FRAMINGCAPABILITIES 3 /* Bearer capabilities */
#define PPPOL2TP_FRAMINGCAPABILITIES 0x00000003 /* Async + Sync framing */
/* -- AVP - Bearer capabilities */
#define PPPOL2TP_AVPTYPE_BEARERCAPABILITIES 4 /* Bearer capabilities */
#define PPPOL2TP_BEARERCAPABILITIES 0x00000003 /* Analog + Digital Access */
/* -- AVP - Tie breaker */
#define PPPOL2TP_AVPTYPE_TIEBREAKER 5
/* -- AVP - Host name */
#define PPPOL2TP_AVPTYPE_HOSTNAME 7 /* Host name */
#define PPPOL2TP_HOSTNAME "lwIP" /* FIXME: make it configurable */
/* -- AVP - Vendor name */
#define PPPOL2TP_AVPTYPE_VENDORNAME 8 /* Vendor name */
#define PPPOL2TP_VENDORNAME "lwIP" /* FIXME: make it configurable */
/* -- AVP - Assign tunnel ID */
#define PPPOL2TP_AVPTYPE_TUNNELID 9 /* Assign Tunnel ID */
/* -- AVP - Receive window size */
#define PPPOL2TP_AVPTYPE_RECEIVEWINDOWSIZE 10 /* Receive window size */
#define PPPOL2TP_RECEIVEWINDOWSIZE 8 /* FIXME: make it configurable */
/* -- AVP - Challenge */
#define PPPOL2TP_AVPTYPE_CHALLENGE 11 /* Challenge */
/* -- AVP - Cause code */
#define PPPOL2TP_AVPTYPE_CAUSECODE 12 /* Cause code*/
/* -- AVP - Challenge response */
#define PPPOL2TP_AVPTYPE_CHALLENGERESPONSE 13 /* Challenge response */
#define PPPOL2TP_AVPTYPE_CHALLENGERESPONSE_SIZE 16
/* -- AVP - Assign session ID */
#define PPPOL2TP_AVPTYPE_SESSIONID 14 /* Assign Session ID */
/* -- AVP - Call serial number */
#define PPPOL2TP_AVPTYPE_CALLSERIALNUMBER 15 /* Call Serial Number */
/* -- AVP - Framing type */
#define PPPOL2TP_AVPTYPE_FRAMINGTYPE 19 /* Framing Type */
#define PPPOL2TP_FRAMINGTYPE 0x00000001 /* Sync framing */
/* -- AVP - TX Connect Speed */
#define PPPOL2TP_AVPTYPE_TXCONNECTSPEED 24 /* TX Connect Speed */
#define PPPOL2TP_TXCONNECTSPEED 100000000 /* Connect speed: 100 Mbits/s */
/* L2TP Session state */
#define PPPOL2TP_STATE_INITIAL 0
#define PPPOL2TP_STATE_SCCRQ_SENT 1
#define PPPOL2TP_STATE_ICRQ_SENT 2
#define PPPOL2TP_STATE_ICCN_SENT 3
#define PPPOL2TP_STATE_DATA 4
#define PPPOL2TP_STATE_CLOSING 5
#define PPPOL2TP_CB_STATE_UP 0 /* PPPoL2TP link is UP */
#define PPPOL2TP_CB_STATE_DOWN 1 /* PPPo2TP link is DOWN - normal condition */
#define PPPOL2TP_CB_STATE_FAILED 2 /* Failed to setup PPPo2TP link */
#define PPPOL2TP_OUTPUT_DATA_HEADER_LEN 6 /* Our data header len */
/*
* PPPoL2TP interface control block.
*/
typedef struct pppol2tp_pcb_s pppol2tp_pcb;
struct pppol2tp_pcb_s {
ppp_pcb *ppp; /* PPP PCB */
u8_t phase; /* L2TP phase */
void (*link_status_cb)(ppp_pcb *pcb, int status);
struct udp_pcb *udp; /* UDP L2TP Socket */
ip_addr_t remote_ip; /* LNS IP Address */
u16_t remote_port; /* LNS port */
#if PPPOL2TP_AUTH_SUPPORT
u8_t *secret; /* Secret string */
u8_t secret_len; /* Secret string length */
u8_t secret_rv[16]; /* Random vector */
u8_t challenge_hash[16]; /* Challenge response */
u8_t send_challenge; /* Boolean whether the next sent packet should contains a challenge response */
#endif /* PPPOL2TP_AUTH_SUPPORT */
u16_t tunnel_port; /* Tunnel port */
u16_t our_ns; /* NS to peer */
u16_t peer_nr; /* NR from peer */
u16_t peer_ns; /* NS from peer */
u16_t source_tunnel_id; /* Tunnel ID assigned by peer */
u16_t remote_tunnel_id; /* Tunnel ID assigned to peer */
u16_t source_session_id; /* Session ID assigned by peer */
u16_t remote_session_id; /* Session ID assigned to peer */
u8_t sccrq_retried; /* number of SCCRQ retries already done */
u8_t icrq_retried; /* number of ICRQ retries already done */
u8_t iccn_retried; /* number of ICCN retries already done */
};
/* Create a new L2TP session. */
err_t pppol2tp_create(ppp_pcb *ppp, void (*link_status_cb)(ppp_pcb *pcb, int status),
pppol2tp_pcb **l2tpptr, u8_t *secret, u8_t secret_len);
/* Destroy a L2TP control block */
err_t pppol2tp_destroy(pppol2tp_pcb *l2tp);
/* Be a LAC, connect to a LNS. */
err_t pppol2tp_connect(pppol2tp_pcb *l2tp, ip_addr_t *ipaddr, u16_t port);
/* Disconnect */
void pppol2tp_disconnect(pppol2tp_pcb *l2tp);
/* Reconnect to a LNS, using previously set L2TP server IP address and port. */
err_t pppol2tp_reconnect(pppol2tp_pcb *l2tp);
/* Data packet from PPP to L2TP */
err_t pppol2tp_xmit(pppol2tp_pcb *l2tp, struct pbuf *pb);
#endif /* PPPOL2TP_H_ */
#endif /* PPP_SUPPORT && PPPOL2TP_SUPPORT */

View File

@ -124,6 +124,9 @@
#if PPPOE_SUPPORT
#include "netif/ppp_oe.h"
#endif /* PPPOE_SUPPORT */
#if PPPOL2TP_SUPPORT
#include "netif/pppol2tp.h"
#endif /* PPPOL2TP_SUPPORT */
/* Global variables */
@ -219,6 +222,12 @@ static err_t ppp_netif_output_over_ethernet(ppp_pcb *pcb, struct pbuf *p, u_shor
static int ppp_write_over_ethernet(ppp_pcb *pcb, struct pbuf *p);
#endif /* PPPOE_SUPPORT */
#if PPPOL2TP_SUPPORT
static err_t ppp_netif_output_over_l2tp(ppp_pcb *pcb, struct pbuf *p, u_short protocol);
/* function called by ppp_write() */
static int ppp_write_over_l2tp(ppp_pcb *pcb, struct pbuf *p);
#endif /* PPPOL2TP_SUPPORT */
static void ppp_destroy(ppp_pcb *pcb);
/***********************************/
@ -415,6 +424,44 @@ int ppp_over_ethernet_open(ppp_pcb *pcb, struct netif *ethif, const char *servic
}
#endif /* PPPOE_SUPPORT */
#if PPPOL2TP_SUPPORT
static void ppp_over_l2tp_link_status_cb(ppp_pcb *pcb, int state);
int ppp_over_l2tp_open(ppp_pcb *pcb, ip_addr_t *ipaddr, u16_t port, u8_t *secret, u8_t secret_len,
ppp_link_status_cb_fn link_status_cb, void *link_status_ctx) {
lcp_options *wo = &pcb->lcp_wantoptions;
lcp_options *ao = &pcb->lcp_allowoptions;
/* PPP is single-threaded: without a callback,
* there is no way to know when the link is up. */
if (link_status_cb == NULL)
return PPPERR_PARAM;
pcb->link_status_cb = link_status_cb;
pcb->link_status_ctx = link_status_ctx;
wo->mru = 1500; /* FIXME: MTU depends we support IP fragmentation or not */
wo->neg_asyncmap = 0;
wo->neg_pcompression = 0;
wo->neg_accompression = 0;
ao->mru = 1500; /* FIXME: MTU depends we support IP fragmentation or not */
ao->neg_asyncmap = 0;
ao->neg_pcompression = 0;
ao->neg_accompression = 0;
if(pppol2tp_create(pcb, ppp_over_l2tp_link_status_cb, &pcb->l2tp_pcb, secret, secret_len) != ERR_OK) {
return PPPERR_OPEN;
}
new_phase(pcb, PHASE_INITIALIZE);
if(!pppol2tp_connect(pcb->l2tp_pcb, ipaddr, port) != ERR_OK) {
return PPPERR_OPEN;
}
return PPPERR_NONE;
}
#endif /* PPPOL2TP_SUPPORT */
/* Close a PPP connection and release the descriptor.
* Any outstanding packets in the queues are dropped.
@ -983,6 +1030,12 @@ static err_t ppp_netif_output(struct netif *netif, struct pbuf *pb, u_short prot
}
#endif /* PPPOE_SUPPORT */
#if PPPOL2TP_SUPPORT
if(pcb->l2tp_pcb) {
return ppp_netif_output_over_l2tp(pcb, pb, protocol);
}
#endif /* PPPOL2TP_SUPPORT */
#if PPPOS_SUPPORT
/* Grab an output buffer. */
head = pbuf_alloc(PBUF_RAW, 0, PBUF_POOL);
@ -1133,6 +1186,47 @@ static err_t ppp_netif_output_over_ethernet(ppp_pcb *pcb, struct pbuf *p, u_shor
#endif /* PPPOE_SUPPORT */
#if PPPOL2TP_SUPPORT
static err_t ppp_netif_output_over_l2tp(ppp_pcb *pcb, struct pbuf *p, u_short protocol) {
struct pbuf *pb;
int i=0;
u16_t tot_len;
/* @todo: try to use pbuf_header() here! */
pb = pbuf_alloc(PBUF_TRANSPORT, PPPOL2TP_OUTPUT_DATA_HEADER_LEN + sizeof(protocol), PBUF_RAM);
if(!pb) {
LINK_STATS_INC(link.memerr);
LINK_STATS_INC(link.proterr);
snmp_inc_ifoutdiscards(&pcb->netif);
return ERR_MEM;
}
pbuf_header(pb, -(s16_t)PPPOL2TP_OUTPUT_DATA_HEADER_LEN);
pcb->last_xmit = sys_jiffies();
if (!pcb->pcomp || protocol > 0xFF) {
*((u_char*)pb->payload + i++) = (protocol >> 8) & 0xFF;
}
*((u_char*)pb->payload + i) = protocol & 0xFF;
pbuf_chain(pb, p);
tot_len = pb->tot_len;
if(pppol2tp_xmit(pcb->l2tp_pcb, pb) != ERR_OK) {
LINK_STATS_INC(link.err);
snmp_inc_ifoutdiscards(&pcb->netif);
return PPPERR_DEVICE;
}
snmp_add_ifoutoctets(&pcb->netif, tot_len);
snmp_inc_ifoutucastpkts(&pcb->netif);
LINK_STATS_INC(link.xmit);
return ERR_OK;
}
#endif /* PPPOL2TP_SUPPORT */
/* Get and set parameters for the given connection.
* Return 0 on success, an error code on failure. */
int
@ -1214,6 +1308,12 @@ int ppp_write(ppp_pcb *pcb, struct pbuf *p) {
}
#endif /* PPPOE_SUPPORT */
#if PPPOL2TP_SUPPORT
if(pcb->l2tp_pcb) {
return ppp_write_over_l2tp(pcb, p);
}
#endif /* PPPOL2TP_SUPPORT */
#if PPPOS_SUPPORT
head = pbuf_alloc(PBUF_RAW, 0, PBUF_POOL);
if (head == NULL) {
@ -1311,6 +1411,38 @@ static int ppp_write_over_ethernet(ppp_pcb *pcb, struct pbuf *p) {
}
#endif /* PPPOE_SUPPORT */
#if PPPOL2TP_SUPPORT
static int ppp_write_over_l2tp(ppp_pcb *pcb, struct pbuf *p) {
struct pbuf *ph; /* UDP + L2TP header */
u16_t tot_len;
ph = pbuf_alloc(PBUF_TRANSPORT, (u16_t)(PPPOL2TP_OUTPUT_DATA_HEADER_LEN), PBUF_RAM);
if(!ph) {
LINK_STATS_INC(link.memerr);
LINK_STATS_INC(link.proterr);
snmp_inc_ifoutdiscards(&pcb->netif);
pbuf_free(p);
return PPPERR_ALLOC;
}
pbuf_header(ph, -(s16_t)PPPOL2TP_OUTPUT_DATA_HEADER_LEN); /* hide L2TP header */
pbuf_cat(ph, p);
tot_len = ph->tot_len;
pcb->last_xmit = sys_jiffies();
if(pppol2tp_xmit(pcb->l2tp_pcb, ph) != ERR_OK) {
LINK_STATS_INC(link.err);
snmp_inc_ifoutdiscards(&pcb->netif);
return PPPERR_DEVICE;
}
snmp_add_ifoutoctets(&pcb->netif, (u16_t)tot_len);
snmp_inc_ifoutucastpkts(&pcb->netif);
LINK_STATS_INC(link.xmit);
return PPPERR_NONE;
}
#endif /* PPPOL2TP_SUPPORT */
#if PPPOS_SUPPORT
/*
@ -1689,6 +1821,49 @@ static void ppp_over_ethernet_link_status_cb(ppp_pcb *pcb, int state) {
}
#endif /* PPPOE_SUPPORT */
#if PPPOL2TP_SUPPORT
static void ppp_over_l2tp_link_status_cb(ppp_pcb *pcb, int state) {
int pppol2tp_err_code = PPPERR_NONE;
switch(state) {
/* PPPoL2TP link is established, starting PPP negotiation */
case PPPOL2TP_CB_STATE_UP:
PPPDEBUG(LOG_INFO, ("ppp_over_l2tp_link_status_cb: unit %d: UP, connecting\n", pcb->num));
ppp_start(pcb);
return;
/* PPPoL2TP link normally down (i.e. asked to do so) */
case PPPOL2TP_CB_STATE_DOWN:
PPPDEBUG(LOG_INFO, ("ppp_over_l2tp_link_status_cb: unit %d: DOWN, disconnected\n", pcb->num));
pppol2tp_err_code = PPPERR_CONNECT;
break;
/* PPPoL2TP link failed to setup (i.e. L2TP timeout) */
case PPPOL2TP_CB_STATE_FAILED:
PPPDEBUG(LOG_INFO, ("ppp_over_l2tp_link_status_cb: unit %d: FAILED, aborting\n", pcb->num));
pppol2tp_err_code = PPPERR_OPEN;
break;
}
/* Reconnect if persist mode is enabled */
if(pcb->settings.persist) {
if(pcb->link_status_cb)
pcb->link_status_cb(pcb, pcb->err_code ? pcb->err_code : pppol2tp_err_code, pcb->link_status_ctx);
new_phase(pcb, PHASE_INITIALIZE);
pppol2tp_reconnect(pcb->l2tp_pcb);
return;
}
ppp_hup(pcb);
ppp_stop(pcb);
pppol2tp_destroy(pcb->l2tp_pcb);
if(pcb->link_status_cb)
pcb->link_status_cb(pcb, pcb->err_code ? pcb->err_code : pppol2tp_err_code, pcb->link_status_ctx);
ppp_destroy(pcb);
}
#endif /* PPPOL2TP_SUPPORT */
void ppp_link_down(ppp_pcb *pcb) {
PPPDEBUG(LOG_DEBUG, ("ppp_link_down: unit %d\n", pcb->num));
@ -1705,6 +1880,11 @@ void ppp_link_terminated(ppp_pcb *pcb) {
pppoe_disconnect(pcb->pppoe_sc);
} else
#endif /* PPPOE_SUPPORT */
#if PPPOL2TP_SUPPORT
if (pcb->l2tp_pcb) {
pppol2tp_disconnect(pcb->l2tp_pcb);
} else
#endif /* PPPOL2TP_SUPPORT */
{
#if PPPOS_SUPPORT
#if PPP_INPROC_OWNTHREAD

View File

@ -144,6 +144,13 @@ typedef struct ppp_pcb_s ppp_pcb;
#include "eap.h"
#endif /* EAP_SUPPORT */
#if PPPOE_SUPPORT
#include "netif/ppp_oe.h"
#endif /* PPPOE_SUPPORT */
#if PPPOL2TP_SUPPORT
#include "netif/pppol2tp.h"
#endif /* PPPOL2TP_SUPPORT */
/*
* PPP configuration.
*/
@ -325,6 +332,10 @@ struct ppp_pcb_s {
struct pppoe_softc *pppoe_sc;
#endif /* PPPOE_SUPPORT */
#if PPPOL2TP_SUPPORT
pppol2tp_pcb *l2tp_pcb;
#endif /* PPPOL2TP_SUPPORT */
u8_t phase; /* where the link is at */
u8_t err_code; /* Code indicating why interface is down. */
@ -464,6 +475,14 @@ int ppp_over_ethernet_open(ppp_pcb *pcb, struct netif *ethif, const char *servic
ppp_link_status_cb_fn link_status_cb, void *link_status_ctx);
#endif /* PPPOE_SUPPORT */
#if PPPOL2TP_SUPPORT
/*
* Open a new PPP Over L2TP (PPPoL2TP) connection.
*/
int ppp_over_l2tp_open(ppp_pcb *pcb, ip_addr_t *ipaddr, u16_t port, u8_t *secret, u8_t secret_len,
ppp_link_status_cb_fn link_status_cb, void *link_status_ctx);
#endif /* PPPOL2TP_SUPPORT */
/*
* Close a PPP connection and release the control block.
* Any outstanding packets in the queues are dropped.

View File

@ -81,7 +81,6 @@
#include "lwip/stats.h"
#include "ppp_impl.h"
#include "netif/ppp_oe.h"
/* Add a 16 bit unsigned value to a buffer pointed to by PTR */
@ -683,6 +682,7 @@ pppoe_send_padi(struct pppoe_softc *sc)
LWIP_ASSERT("sizeof(struct eth_hdr) + PPPOE_HEADERLEN + len <= 0xffff",
sizeof(struct eth_hdr) + PPPOE_HEADERLEN + len <= 0xffff);
/* FIXME: PBUF_LINK already allocate a eth_hdr */
/* allocate a buffer */
pb = pbuf_alloc(PBUF_LINK, (u16_t)(sizeof(struct eth_hdr) + PPPOE_HEADERLEN + len), PBUF_RAM);
if (!pb) {

990
src/netif/ppp/pppol2tp.c Normal file
View File

@ -0,0 +1,990 @@
/**
* @file
* Network Point to Point Protocol over Layer 2 Tunneling Protocol program file.
*
*/
/*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*
* This file is part of the lwIP TCP/IP stack.
*
*/
/*
* L2TP Support status:
*
* Supported:
* - L2TPv2 (PPP over L2TP, a.k.a. UDP tunnels)
* - LAC
*
* Not supported:
* - LNS (require PPP server support)
* - L2TPv3 ethernet pseudowires
* - L2TPv3 VLAN pseudowire
* - L2TPv3 PPP pseudowires
* - L2TPv3 IP encapsulation
* - L2TPv3 IP pseudowire
* - L2TP tunnel switching - http://tools.ietf.org/html/draft-ietf-l2tpext-tunnel-switching-08
* - Multiple tunnels per UDP socket, as well as multiple sessions per tunnel
* - Hidden AVPs
*/
#include "lwip/opt.h"
#if PPP_SUPPORT && PPPOL2TP_SUPPORT /* don't build if not configured for use in lwipopts.h */
#include "lwip/err.h"
#include "lwip/memp.h"
#include "lwip/udp.h"
#include "ppp_impl.h"
#include "netif/pppol2tp.h"
#include "magic.h"
#if PPPOL2TP_AUTH_SUPPORT
#if LWIP_INCLUDED_POLARSSL_MD5
#include "polarssl/lwip_md5.h"
#else
#include "polarssl/md5.h"
#endif
#endif /* PPPOL2TP_AUTH_SUPPORT */
/* Prototypes for procedures local to this file. */
static void pppol2tp_do_disconnect(pppol2tp_pcb *l2tp);
static void pppol2tp_input(void *arg, struct udp_pcb *pcb, struct pbuf *p, struct ip_addr *addr, u16_t port);
static void pppol2tp_dispatch_control_packet(pppol2tp_pcb *l2tp, struct ip_addr *addr, u16_t port,
struct pbuf *p, u16_t len, u16_t tunnel_id, u16_t session_id, u16_t ns, u16_t nr);
static void pppol2tp_timeout(void *arg);
static void pppol2tp_abort_connect(pppol2tp_pcb *l2tp);
static void pppol2tp_clear(pppol2tp_pcb *l2tp);
static err_t pppol2tp_send_sccrq(pppol2tp_pcb *l2tp);
static err_t pppol2tp_send_scccn(pppol2tp_pcb *l2tp, u16_t ns);
static err_t pppol2tp_send_icrq(pppol2tp_pcb *l2tp, u16_t ns);
static err_t pppol2tp_send_iccn(pppol2tp_pcb *l2tp, u16_t ns);
static err_t pppol2tp_send_zlb(pppol2tp_pcb *l2tp, u16_t ns);
static err_t pppol2tp_send_stopccn(pppol2tp_pcb *l2tp, u16_t ns);
/* Create a new L2TP session. */
err_t pppol2tp_create(ppp_pcb *ppp, void (*link_status_cb)(ppp_pcb *pcb, int status),
pppol2tp_pcb **l2tpptr, u8_t *secret, u8_t secret_len) {
pppol2tp_pcb *l2tp;
struct udp_pcb *udp;
l2tp = (pppol2tp_pcb *)memp_malloc(MEMP_PPPOL2TP_PCB);
if (l2tp == NULL) {
*l2tpptr = NULL;
return ERR_MEM;
}
udp = udp_new();
if (udp == NULL) {
memp_free(MEMP_PPPOL2TP_PCB, l2tp);
*l2tpptr = NULL;
return ERR_MEM;
}
memset(l2tp, 0, sizeof(pppol2tp_pcb));
l2tp->phase = PPPOL2TP_STATE_INITIAL;
l2tp->ppp = ppp;
l2tp->udp = udp;
l2tp->link_status_cb = link_status_cb;
#if PPPOL2TP_AUTH_SUPPORT
l2tp->secret = secret;
l2tp->secret_len = secret_len;
#endif /* PPPOL2TP_AUTH_SUPPORT */
*l2tpptr = l2tp;
return ERR_OK;
}
/* Destroy a L2TP control block */
err_t pppol2tp_destroy(pppol2tp_pcb *l2tp) {
sys_untimeout(pppol2tp_timeout, l2tp);
if (l2tp->udp != NULL) {
udp_remove(l2tp->udp);
}
memp_free(MEMP_PPPOL2TP_PCB, l2tp);
return ERR_OK;
}
/* Be a LAC, connect to a LNS. */
err_t pppol2tp_connect(pppol2tp_pcb *l2tp, ip_addr_t *ipaddr, u16_t port) {
err_t err;
if (l2tp->phase != PPPOL2TP_STATE_INITIAL) {
return ERR_VAL;
}
ip_addr_set(&l2tp->remote_ip, ipaddr);
l2tp->remote_port = l2tp->tunnel_port = port;
/* Listen to a random source port, we need to do that instead of using udp_connect()
* because the L2TP LNS might answer with its own random source port (!= 1701)
*/
udp_bind(l2tp->udp, IP_ADDR_ANY, 0);
udp_recv(l2tp->udp, pppol2tp_input, l2tp);
#if PPPOL2TP_AUTH_SUPPORT
/* Generate random vector */
if (l2tp->secret != NULL) {
random_bytes(l2tp->secret_rv, sizeof(l2tp->secret_rv));
}
#endif /* PPPOL2TP_AUTH_SUPPORT */
do {
l2tp->remote_tunnel_id = magic();
} while(l2tp->remote_tunnel_id == 0);
/* save state, in case we fail to send SCCRQ */
l2tp->sccrq_retried = 0;
l2tp->phase = PPPOL2TP_STATE_SCCRQ_SENT;
if ((err = pppol2tp_send_sccrq(l2tp)) != 0) {
PPPDEBUG(LOG_DEBUG, ("pppol2tp: failed to send SCCRQ, error=%d\n", err));
}
sys_timeout(PPPOL2TP_CONTROL_TIMEOUT, pppol2tp_timeout, l2tp);
return err;
}
/* Reconnect to a LNS, using previously set L2TP server IP address and port. */
err_t pppol2tp_reconnect(pppol2tp_pcb *l2tp) {
err_t err;
if (l2tp->phase != PPPOL2TP_STATE_INITIAL) {
return ERR_VAL;
}
pppol2tp_clear(l2tp);
do {
l2tp->remote_tunnel_id = magic();
} while(l2tp->remote_tunnel_id == 0);
/* save state, in case we fail to send SCCRQ */
l2tp->sccrq_retried = 0;
l2tp->phase = PPPOL2TP_STATE_SCCRQ_SENT;
if ((err = pppol2tp_send_sccrq(l2tp)) != 0) {
PPPDEBUG(LOG_DEBUG, ("pppol2tp: failed to send SCCRQ, error=%d\n", err));
}
sys_timeout(PPPOL2TP_CONTROL_TIMEOUT, pppol2tp_timeout, l2tp);
return err;
}
/* Disconnect */
void pppol2tp_disconnect(pppol2tp_pcb *l2tp) {
if (l2tp->phase == PPPOL2TP_STATE_CLOSING) {
return;
}
l2tp->our_ns++;
pppol2tp_send_stopccn(l2tp, l2tp->our_ns);
/*
* Do not call pppol2tp_disconnect here, the upper layer state
* machine gets confused by this. We must return from this
* function and defer disconnecting to the timeout handler.
*/
l2tp->phase = PPPOL2TP_STATE_CLOSING;
sys_timeout(20, pppol2tp_timeout, l2tp);
}
static void pppol2tp_do_disconnect(pppol2tp_pcb *l2tp) {
pppol2tp_clear(l2tp);
l2tp->link_status_cb(l2tp->ppp, PPPOL2TP_CB_STATE_DOWN); /* notify upper layers */
}
/* UDP Callback for incoming L2TP frames */
static void pppol2tp_input(void *arg, struct udp_pcb *pcb, struct pbuf *p, struct ip_addr *addr, u16_t port) {
pppol2tp_pcb *l2tp = (pppol2tp_pcb*)arg;
u16_t hflags, hlen, len=0, tunnel_id=0, session_id=0, ns=0, nr=0, offset=0;
u8_t *inp;
printf("-----------\nL2TP INPUT, %d\n", p->len);
p = ppp_singlebuf(p);
/* L2TP header */
if (p->len < sizeof(hflags) + sizeof(tunnel_id) + sizeof(session_id) ) {
goto packet_too_short;
}
inp = p->payload;
GETSHORT(hflags, inp);
if (hflags & PPPOL2TP_HEADERFLAG_CONTROL) {
/* check mandatory flags for a control packet */
if ( (hflags & PPPOL2TP_HEADERFLAG_CONTROL_MANDATORY) != PPPOL2TP_HEADERFLAG_CONTROL_MANDATORY ) {
PPPDEBUG(LOG_DEBUG, ("pppol2tp: mandatory header flags for control packet not set\n"));
goto free_and_return;
}
/* check forbidden flags for a control packet */
if (hflags & PPPOL2TP_HEADERFLAG_CONTROL_FORBIDDEN) {
PPPDEBUG(LOG_DEBUG, ("pppol2tp: forbidden header flags for control packet found\n"));
goto free_and_return;
}
} else {
/* check mandatory flags for a data packet */
if ( (hflags & PPPOL2TP_HEADERFLAG_DATA_MANDATORY) != PPPOL2TP_HEADERFLAG_DATA_MANDATORY) {
PPPDEBUG(LOG_DEBUG, ("pppol2tp: mandatory header flags for data packet not set\n"));
goto free_and_return;
}
}
/* Expected header size */
hlen = sizeof(hflags) + sizeof(tunnel_id) + sizeof(session_id);
if (hflags & PPPOL2TP_HEADERFLAG_LENGTH) {
hlen += sizeof(len);
}
if (hflags & PPPOL2TP_HEADERFLAG_SEQUENCE) {
hlen += sizeof(ns) + sizeof(nr);
}
if (hflags & PPPOL2TP_HEADERFLAG_OFFSET) {
hlen += sizeof(offset);
}
if (p->len < hlen) {
goto packet_too_short;
}
if (hflags & PPPOL2TP_HEADERFLAG_LENGTH) {
GETSHORT(len, inp);
if (p->len < len || len < hlen) {
goto packet_too_short;
}
}
GETSHORT(tunnel_id, inp);
GETSHORT(session_id, inp);
if (hflags & PPPOL2TP_HEADERFLAG_SEQUENCE) {
GETSHORT(ns, inp);
GETSHORT(nr, inp);
}
if (hflags & PPPOL2TP_HEADERFLAG_OFFSET) {
GETSHORT(offset, inp)
if (offset > 4096) { /* don't be fooled with large offset which might overflow hlen */
PPPDEBUG(LOG_DEBUG, ("pppol2tp: strange packet received, offset=%d\n", offset));
goto free_and_return;
}
hlen += offset;
if (p->len < hlen) {
goto packet_too_short;
}
INCPTR(offset, inp);
}
printf("HLEN = %d\n", hlen);
/* skip L2TP header */
if (pbuf_header(p, -hlen) != 0) {
goto free_and_return;
}
printf("LEN=%d, TUNNEL_ID=%d, SESSION_ID=%d, NS=%d, NR=%d, OFFSET=%d\n", len, tunnel_id, session_id, ns, nr, offset);
/* Control packet */
if (hflags & PPPOL2TP_HEADERFLAG_CONTROL) {
pppol2tp_dispatch_control_packet(l2tp, addr, port, p, len, tunnel_id, session_id, ns, nr);
goto free_and_return;
}
/* Data packet */
if(l2tp->phase != PPPOL2TP_STATE_DATA) {
goto free_and_return;
}
if(tunnel_id != l2tp->remote_tunnel_id) {
PPPDEBUG(LOG_DEBUG, ("pppol2tp: tunnel ID mismatch, assigned=%d, received=%d\n", l2tp->remote_tunnel_id, tunnel_id));
goto free_and_return;
}
if(session_id != l2tp->remote_session_id) {
PPPDEBUG(LOG_DEBUG, ("pppol2tp: session ID mismatch, assigned=%d, received=%d\n", l2tp->remote_session_id, session_id));
goto free_and_return;
}
/* skip address & flags */
pbuf_header(p, -(s16_t)2);
/* Dispatch the packet thereby consuming it. */
ppp_input(l2tp->ppp, p);
return;
packet_too_short:
PPPDEBUG(LOG_DEBUG, ("pppol2tp: packet too short: %d\n", p->len));
free_and_return:
pbuf_free(p);
}
/* L2TP Control packet entry point */
static void pppol2tp_dispatch_control_packet(pppol2tp_pcb *l2tp, struct ip_addr *addr, u16_t port,
struct pbuf *p, u16_t len, u16_t tunnel_id, u16_t session_id, u16_t ns, u16_t nr) {
u8_t *inp;
u16_t avplen, avpflags, vendorid, attributetype, messagetype=0;
err_t err;
#if PPPOL2TP_AUTH_SUPPORT
md5_context md5_context;
u8_t md5_hash[16];
u8_t challenge_id = 0;
#endif /* PPPOL2TP_AUTH_SUPPORT */
l2tp->peer_nr = nr;
l2tp->peer_ns = ns;
printf("L2TP CTRL INPUT, ns=%d, nr=%d, len=%d\n", ns, nr, p->len);
/* Handle the special case of the ICCN acknowledge */
if (l2tp->phase == PPPOL2TP_STATE_ICCN_SENT && l2tp->peer_nr > l2tp->our_ns) {
l2tp->phase = PPPOL2TP_STATE_DATA;
}
/* ZLB packets */
if (p->len == 0) {
return;
}
inp = p->payload;
/* Decode AVPs */
while (p->len > 0) {
if (p->len < sizeof(avpflags) + sizeof(vendorid) + sizeof(attributetype) ) {
goto packet_too_short;
}
GETSHORT(avpflags, inp);
avplen = avpflags & PPPOL2TP_AVPHEADERFLAG_LENGTHMASK;
printf("AVPLEN = %d\n", avplen);
if (p->len < avplen || avplen < sizeof(avpflags) + sizeof(vendorid) + sizeof(attributetype)) {
goto packet_too_short;
}
GETSHORT(vendorid, inp);
GETSHORT(attributetype, inp);
avplen -= sizeof(avpflags) + sizeof(vendorid) + sizeof(attributetype);
/* Message type must be the first AVP */
if (messagetype == 0) {
if (attributetype != 0 || vendorid != 0 || avplen != sizeof(messagetype) ) {
PPPDEBUG(LOG_DEBUG, ("pppol2tp: message type must be the first AVP\n"));
return;
}
GETSHORT(messagetype, inp);
printf("Message type = %d\n", messagetype);
switch(messagetype) {
/* Start Control Connection Reply */
case PPPOL2TP_MESSAGETYPE_SCCRP:
/* Only accept SCCRP packet if we sent a SCCRQ */
if (l2tp->phase != PPPOL2TP_STATE_SCCRQ_SENT) {
goto send_zlb;
}
break;
/* Incoming Call Reply */
case PPPOL2TP_MESSAGETYPE_ICRP:
/* Only accept ICRP packet if we sent a IRCQ */
if (l2tp->phase != PPPOL2TP_STATE_ICRQ_SENT) {
goto send_zlb;
}
break;
/* Stop Control Connection Notification */
case PPPOL2TP_MESSAGETYPE_STOPCCN:
if (l2tp->phase < PPPOL2TP_STATE_DATA) {
pppol2tp_abort_connect(l2tp);
} else if (l2tp->phase == PPPOL2TP_STATE_DATA) {
pppol2tp_disconnect(l2tp);
}
return;
}
goto nextavp;
}
/* Skip proprietary L2TP extensions */
if (vendorid != 0) {
goto skipavp;
}
switch (messagetype) {
/* Start Control Connection Reply */
case PPPOL2TP_MESSAGETYPE_SCCRP:
switch (attributetype) {
case PPPOL2TP_AVPTYPE_TUNNELID:
if (avplen != sizeof(l2tp->source_tunnel_id) ) {
PPPDEBUG(LOG_DEBUG, ("pppol2tp: AVP Assign tunnel ID length check failed\n"));
return;
}
GETSHORT(l2tp->source_tunnel_id, inp);
printf("Assigned tunnel %d\n", l2tp->source_tunnel_id);
goto nextavp;
#if PPPOL2TP_AUTH_SUPPORT
case PPPOL2TP_AVPTYPE_CHALLENGE:
if (avplen == 0) {
PPPDEBUG(LOG_DEBUG, ("pppol2tp: Challenge length check failed\n"));
return;
}
if (l2tp->secret == NULL) {
PPPDEBUG(LOG_DEBUG, ("pppol2tp: Received challenge from peer and no secret key available\n"));
pppol2tp_abort_connect(l2tp);
return;
}
/* Generate hash of ID, secret, challenge */
md5_starts(&md5_context);
challenge_id = PPPOL2TP_MESSAGETYPE_SCCCN;
md5_update(&md5_context, &challenge_id, 1);
md5_update(&md5_context, l2tp->secret, l2tp->secret_len);
md5_update(&md5_context, inp, avplen);
md5_finish(&md5_context, l2tp->challenge_hash);
l2tp->send_challenge = 1;
goto skipavp;
case PPPOL2TP_AVPTYPE_CHALLENGERESPONSE:
if (avplen != PPPOL2TP_AVPTYPE_CHALLENGERESPONSE_SIZE) {
PPPDEBUG(LOG_DEBUG, ("pppol2tp: AVP Challenge Response length check failed\n"));
return;
}
/* Generate hash of ID, secret, challenge */
md5_starts(&md5_context);
challenge_id = PPPOL2TP_MESSAGETYPE_SCCRP;
md5_update(&md5_context, &challenge_id, 1);
md5_update(&md5_context, l2tp->secret, l2tp->secret_len);
md5_update(&md5_context, l2tp->secret_rv, sizeof(l2tp->secret_rv));
md5_finish(&md5_context, md5_hash);
if ( memcmp(inp, md5_hash, sizeof(md5_hash)) ) {
PPPDEBUG(LOG_DEBUG, ("pppol2tp: Received challenge response from peer and secret key do not match\n"));
pppol2tp_abort_connect(l2tp);
return;
}
goto skipavp;
#endif /* PPPOL2TP_AUTH_SUPPORT */
}
break;
/* Incoming Call Reply */
case PPPOL2TP_MESSAGETYPE_ICRP:
switch (attributetype) {
case PPPOL2TP_AVPTYPE_SESSIONID:
if (avplen != sizeof(l2tp->source_session_id) ) {
PPPDEBUG(LOG_DEBUG, ("pppol2tp: AVP Assign session ID length check failed\n"));
return;
}
GETSHORT(l2tp->source_session_id, inp);
printf("Assigned session %d\n", l2tp->source_session_id);
goto nextavp;
}
break;
}
skipavp:
INCPTR(avplen, inp);
nextavp:
printf("AVP Found, vendor=%d, attribute=%d, len=%d\n", vendorid, attributetype, avplen);
/* next AVP */
if (pbuf_header(p, -avplen - sizeof(avpflags) - sizeof(vendorid) - sizeof(attributetype) ) != 0) {
return;
}
}
switch(messagetype) {
/* Start Control Connection Reply */
case PPPOL2TP_MESSAGETYPE_SCCRP:
do {
l2tp->remote_session_id = magic();
} while(l2tp->remote_session_id == 0);
l2tp->tunnel_port = port; /* LNS server might have chosen its own local port */
l2tp->icrq_retried = 0;
l2tp->phase = PPPOL2TP_STATE_ICRQ_SENT;
l2tp->our_ns++;
if ((err = pppol2tp_send_scccn(l2tp, l2tp->our_ns)) != 0) {
PPPDEBUG(LOG_DEBUG, ("pppol2tp: failed to send SCCCN, error=%d\n", err));
}
l2tp->our_ns++;
if ((err = pppol2tp_send_icrq(l2tp, l2tp->our_ns)) != 0) {
PPPDEBUG(LOG_DEBUG, ("pppol2tp: failed to send ICRQ, error=%d\n", err));
}
sys_untimeout(pppol2tp_timeout, l2tp);
sys_timeout(PPPOL2TP_CONTROL_TIMEOUT, pppol2tp_timeout, l2tp);
break;
/* Incoming Call Reply */
case PPPOL2TP_MESSAGETYPE_ICRP:
l2tp->iccn_retried = 0;
l2tp->phase = PPPOL2TP_STATE_ICCN_SENT;
l2tp->our_ns++;
l2tp->link_status_cb(l2tp->ppp, PPPOL2TP_CB_STATE_UP); /* notify upper layers */
if ((err = pppol2tp_send_iccn(l2tp, l2tp->our_ns)) != 0) {
PPPDEBUG(LOG_DEBUG, ("pppol2tp: failed to send ICCN, error=%d\n", err));
}
sys_untimeout(pppol2tp_timeout, l2tp);
sys_timeout(PPPOL2TP_CONTROL_TIMEOUT, pppol2tp_timeout, l2tp);
break;
/* Unhandled packet, send ZLB ACK */
default:
goto send_zlb;
}
return;
send_zlb:
pppol2tp_send_zlb(l2tp, l2tp->our_ns);
return;
packet_too_short:
PPPDEBUG(LOG_DEBUG, ("pppol2tp: packet too short: %d\n", p->len));
}
/* L2TP Timeout handler */
static void pppol2tp_timeout(void *arg) {
pppol2tp_pcb *l2tp = (pppol2tp_pcb*)arg;
err_t err;
u32_t retry_wait;
PPPDEBUG(LOG_DEBUG, ("pppol2tp: timeout\n"));
switch (l2tp->phase) {
case PPPOL2TP_STATE_SCCRQ_SENT:
/* backoff wait */
if (l2tp->sccrq_retried < UCHAR_MAX) {
l2tp->sccrq_retried++;
}
if (!l2tp->ppp->settings.persist && l2tp->sccrq_retried >= PPPOL2TP_MAXSCCRQ) {
pppol2tp_abort_connect(l2tp);
return;
}
retry_wait = LWIP_MIN(PPPOL2TP_CONTROL_TIMEOUT * l2tp->sccrq_retried, PPPOL2TP_SLOW_RETRY);
PPPDEBUG(LOG_DEBUG, ("pppol2tp: sccrq_retried=%d\n", l2tp->sccrq_retried));
if ((err = pppol2tp_send_sccrq(l2tp)) != 0) {
l2tp->sccrq_retried--;
PPPDEBUG(LOG_DEBUG, ("pppol2tp: failed to send SCCRQ, error=%d\n", err));
}
sys_timeout(retry_wait, pppol2tp_timeout, l2tp);
break;
case PPPOL2TP_STATE_ICRQ_SENT:
l2tp->icrq_retried++;
if (l2tp->icrq_retried >= PPPOL2TP_MAXICRQ) {
pppol2tp_abort_connect(l2tp);
return;
}
PPPDEBUG(LOG_DEBUG, ("pppol2tp: icrq_retried=%d\n", l2tp->icrq_retried));
if (l2tp->peer_nr <= l2tp->our_ns -1) { /* the SCCCN was not acknowledged */
if ((err = pppol2tp_send_scccn(l2tp, l2tp->our_ns -1)) != 0) {
l2tp->icrq_retried--;
PPPDEBUG(LOG_DEBUG, ("pppol2tp: failed to send SCCCN, error=%d\n", err));
sys_timeout(PPPOL2TP_CONTROL_TIMEOUT, pppol2tp_timeout, l2tp);
break;
}
}
if ((err = pppol2tp_send_icrq(l2tp, l2tp->our_ns)) != 0) {
l2tp->icrq_retried--;
PPPDEBUG(LOG_DEBUG, ("pppol2tp: failed to send ICRQ, error=%d\n", err));
}
sys_timeout(PPPOL2TP_CONTROL_TIMEOUT, pppol2tp_timeout, l2tp);
break;
case PPPOL2TP_STATE_ICCN_SENT:
l2tp->iccn_retried++;
if (l2tp->iccn_retried >= PPPOL2TP_MAXICCN) {
pppol2tp_abort_connect(l2tp);
return;
}
PPPDEBUG(LOG_DEBUG, ("pppol2tp: iccn_retried=%d\n", l2tp->iccn_retried));
if ((err = pppol2tp_send_iccn(l2tp, l2tp->our_ns)) != 0) {
l2tp->iccn_retried--;
PPPDEBUG(LOG_DEBUG, ("pppol2tp: failed to send ICCN, error=%d\n", err));
}
sys_timeout(PPPOL2TP_CONTROL_TIMEOUT, pppol2tp_timeout, l2tp);
break;
case PPPOL2TP_STATE_CLOSING:
pppol2tp_do_disconnect(l2tp);
break;
default:
return; /* all done, work in peace */
}
}
/* Connection attempt aborted */
static void pppol2tp_abort_connect(pppol2tp_pcb *l2tp) {
PPPDEBUG(LOG_DEBUG, ("pppol2tp: could not establish connection\n"));
pppol2tp_clear(l2tp);
l2tp->link_status_cb(l2tp->ppp, PPPOL2TP_CB_STATE_FAILED); /* notify upper layers */
}
/* Reset L2TP control block to its initial state */
static void pppol2tp_clear(pppol2tp_pcb *l2tp) {
/* stop any timer */
sys_untimeout(pppol2tp_timeout, l2tp);
l2tp->phase = PPPOL2TP_STATE_INITIAL;
l2tp->tunnel_port = l2tp->remote_port;
l2tp->our_ns = 0;
l2tp->peer_nr = 0;
l2tp->peer_ns = 0;
l2tp->source_tunnel_id = 0;
l2tp->remote_tunnel_id = 0;
l2tp->source_session_id = 0;
l2tp->remote_session_id = 0;
/* l2tp->*_retried are cleared when used */
}
/* Initiate a new tunnel */
static err_t pppol2tp_send_sccrq(pppol2tp_pcb *l2tp) {
struct pbuf *pb;
u8_t *p;
u16_t len;
/* calculate UDP packet length */
len = 12 +8 +8 +10 +10 +6+sizeof(PPPOL2TP_HOSTNAME)-1 +6+sizeof(PPPOL2TP_VENDORNAME)-1 +8 +8;
#if PPPOL2TP_AUTH_SUPPORT
if (l2tp->secret != NULL) {
len += 6 + sizeof(l2tp->secret_rv);
}
#endif /* PPPOL2TP_AUTH_SUPPORT */
/* allocate a buffer */
pb = pbuf_alloc(PBUF_TRANSPORT, len, PBUF_RAM);
if (pb == NULL) {
return ERR_MEM;
}
LWIP_ASSERT("pb->tot_len == pb->len", pb->tot_len == pb->len);
p = (u8_t*)pb->payload;
/* fill in pkt */
/* L2TP control header */
PUTSHORT(PPPOL2TP_HEADERFLAG_CONTROL_MANDATORY, p);
PUTSHORT(len, p); /* Length */
PUTSHORT(0, p); /* Tunnel Id */
PUTSHORT(0, p); /* Session Id */
PUTSHORT(0, p); /* NS Sequence number - to peer */
PUTSHORT(0, p); /* NR Sequence number - expected for peer */
/* AVP - Message type */
PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 8, p); /* Mandatory flag + len field */
PUTSHORT(0, p); /* Vendor ID */
PUTSHORT(PPPOL2TP_AVPTYPE_MESSAGE, p); /* Attribute type: Message Type */
PUTSHORT(PPPOL2TP_MESSAGETYPE_SCCRQ, p); /* Attribute value: Message type: SCCRQ */
/* AVP - L2TP Version */
PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 8, p); /* Mandatory flag + len field */
PUTSHORT(0, p); /* Vendor ID */
PUTSHORT(PPPOL2TP_AVPTYPE_VERSION, p); /* Attribute type: Version */
PUTSHORT(PPPOL2TP_VERSION, p); /* Attribute value: L2TP Version */
/* AVP - Framing capabilities */
PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 10, p); /* Mandatory flag + len field */
PUTSHORT(0, p); /* Vendor ID */
PUTSHORT(PPPOL2TP_AVPTYPE_FRAMINGCAPABILITIES, p); /* Attribute type: Framing capabilities */
PUTLONG(PPPOL2TP_FRAMINGCAPABILITIES, p); /* Attribute value: Framing capabilities */
/* AVP - Bearer capabilities */
PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 10, p); /* Mandatory flag + len field */
PUTSHORT(0, p); /* Vendor ID */
PUTSHORT(PPPOL2TP_AVPTYPE_BEARERCAPABILITIES, p); /* Attribute type: Bearer capabilities */
PUTLONG(PPPOL2TP_BEARERCAPABILITIES, p); /* Attribute value: Bearer capabilities */
/* AVP - Host name */
PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 6+sizeof(PPPOL2TP_HOSTNAME)-1, p); /* Mandatory flag + len field */
PUTSHORT(0, p); /* Vendor ID */
PUTSHORT(PPPOL2TP_AVPTYPE_HOSTNAME, p); /* Attribute type: Hostname */
MEMCPY(p, PPPOL2TP_HOSTNAME, sizeof(PPPOL2TP_HOSTNAME)-1); /* Attribute value: Hostname */
INCPTR(sizeof(PPPOL2TP_HOSTNAME)-1, p);
/* AVP - Vendor name */
PUTSHORT(6+sizeof(PPPOL2TP_VENDORNAME)-1, p); /* len field */
PUTSHORT(0, p); /* Vendor ID */
PUTSHORT(PPPOL2TP_AVPTYPE_VENDORNAME, p); /* Attribute type: Vendor name */
MEMCPY(p, PPPOL2TP_VENDORNAME, sizeof(PPPOL2TP_VENDORNAME)-1); /* Attribute value: Vendor name */
INCPTR(sizeof(PPPOL2TP_VENDORNAME)-1, p);
/* AVP - Assign tunnel ID */
PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 8, p); /* Mandatory flag + len field */
PUTSHORT(0, p); /* Vendor ID */
PUTSHORT(PPPOL2TP_AVPTYPE_TUNNELID, p); /* Attribute type: Tunnel ID */
PUTSHORT(l2tp->remote_tunnel_id, p); /* Attribute value: Tunnel ID */
/* AVP - Receive window size */
PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 8, p); /* Mandatory flag + len field */
PUTSHORT(0, p); /* Vendor ID */
PUTSHORT(PPPOL2TP_AVPTYPE_RECEIVEWINDOWSIZE, p); /* Attribute type: Receive window size */
PUTSHORT(PPPOL2TP_RECEIVEWINDOWSIZE, p); /* Attribute value: Receive window size */
#if PPPOL2TP_AUTH_SUPPORT
/* AVP - Challenge */
if (l2tp->secret != NULL) {
PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 6 + sizeof(l2tp->secret_rv), p); /* Mandatory flag + len field */
PUTSHORT(0, p); /* Vendor ID */
PUTSHORT(PPPOL2TP_AVPTYPE_CHALLENGE, p); /* Attribute type: Challenge */
MEMCPY(p, l2tp->secret_rv, sizeof(l2tp->secret_rv)); /* Attribute value: Random vector */
INCPTR(sizeof(l2tp->secret_rv), p);
}
#endif /* PPPOL2TP_AUTH_SUPPORT */
udp_sendto(l2tp->udp, pb, &l2tp->remote_ip, l2tp->remote_port);
pbuf_free(pb);
return ERR_OK;
}
/* Complete tunnel establishment */
static err_t pppol2tp_send_scccn(pppol2tp_pcb *l2tp, u16_t ns) {
struct pbuf *pb;
u8_t *p;
u16_t len;
/* calculate UDP packet length */
len = 12 +8;
#if PPPOL2TP_AUTH_SUPPORT
if (l2tp->send_challenge) {
len += 6 + sizeof(l2tp->challenge_hash);
}
#endif /* PPPOL2TP_AUTH_SUPPORT */
/* allocate a buffer */
pb = pbuf_alloc(PBUF_TRANSPORT, len, PBUF_RAM);
if (pb == NULL) {
return ERR_MEM;
}
LWIP_ASSERT("pb->tot_len == pb->len", pb->tot_len == pb->len);
p = (u8_t*)pb->payload;
/* fill in pkt */
/* L2TP control header */
PUTSHORT(PPPOL2TP_HEADERFLAG_CONTROL_MANDATORY, p);
PUTSHORT(len, p); /* Length */
PUTSHORT(l2tp->source_tunnel_id, p); /* Tunnel Id */
PUTSHORT(0, p); /* Session Id */
PUTSHORT(ns, p); /* NS Sequence number - to peer */
PUTSHORT(l2tp->peer_ns+1, p); /* NR Sequence number - expected for peer */
/* AVP - Message type */
PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 8, p); /* Mandatory flag + len field */
PUTSHORT(0, p); /* Vendor ID */
PUTSHORT(PPPOL2TP_AVPTYPE_MESSAGE, p); /* Attribute type: Message Type */
PUTSHORT(PPPOL2TP_MESSAGETYPE_SCCCN, p); /* Attribute value: Message type: SCCCN */
#if PPPOL2TP_AUTH_SUPPORT
/* AVP - Challenge response */
if (l2tp->send_challenge) {
PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 6 + sizeof(l2tp->challenge_hash), p); /* Mandatory flag + len field */
PUTSHORT(0, p); /* Vendor ID */
PUTSHORT(PPPOL2TP_AVPTYPE_CHALLENGERESPONSE, p); /* Attribute type: Challenge response */
MEMCPY(p, l2tp->challenge_hash, sizeof(l2tp->challenge_hash)); /* Attribute value: Computed challenge */
INCPTR(sizeof(l2tp->challenge_hash), p);
}
#endif /* PPPOL2TP_AUTH_SUPPORT */
udp_sendto(l2tp->udp, pb, &l2tp->remote_ip, l2tp->tunnel_port);
pbuf_free(pb);
return ERR_OK;
}
/* Initiate a new session */
static err_t pppol2tp_send_icrq(pppol2tp_pcb *l2tp, u16_t ns) {
struct pbuf *pb;
u8_t *p;
u16_t len;
u32_t serialnumber;
/* calculate UDP packet length */
len = 12 +8 +8 +10;
/* allocate a buffer */
pb = pbuf_alloc(PBUF_TRANSPORT, len, PBUF_RAM);
if (pb == NULL) {
return ERR_MEM;
}
LWIP_ASSERT("pb->tot_len == pb->len", pb->tot_len == pb->len);
p = (u8_t*)pb->payload;
/* fill in pkt */
/* L2TP control header */
PUTSHORT(PPPOL2TP_HEADERFLAG_CONTROL_MANDATORY, p);
PUTSHORT(len, p); /* Length */
PUTSHORT(l2tp->source_tunnel_id, p); /* Tunnel Id */
PUTSHORT(0, p); /* Session Id */
PUTSHORT(ns, p); /* NS Sequence number - to peer */
PUTSHORT(l2tp->peer_ns+1, p); /* NR Sequence number - expected for peer */
/* AVP - Message type */
PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 8, p); /* Mandatory flag + len field */
PUTSHORT(0, p); /* Vendor ID */
PUTSHORT(PPPOL2TP_AVPTYPE_MESSAGE, p); /* Attribute type: Message Type */
PUTSHORT(PPPOL2TP_MESSAGETYPE_ICRQ, p); /* Attribute value: Message type: ICRQ */
/* AVP - Assign session ID */
PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 8, p); /* Mandatory flag + len field */
PUTSHORT(0, p); /* Vendor ID */
PUTSHORT(PPPOL2TP_AVPTYPE_SESSIONID, p); /* Attribute type: Session ID */
PUTSHORT(l2tp->remote_session_id, p); /* Attribute value: Session ID */
/* AVP - Call Serial Number */
PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 10, p); /* Mandatory flag + len field */
PUTSHORT(0, p); /* Vendor ID */
PUTSHORT(PPPOL2TP_AVPTYPE_CALLSERIALNUMBER, p); /* Attribute type: Serial number */
serialnumber = magic();
PUTLONG(serialnumber, p); /* Attribute value: Serial number */
udp_sendto(l2tp->udp, pb, &l2tp->remote_ip, l2tp->tunnel_port);
pbuf_free(pb);
return ERR_OK;
}
/* Complete tunnel establishment */
static err_t pppol2tp_send_iccn(pppol2tp_pcb *l2tp, u16_t ns) {
struct pbuf *pb;
u8_t *p;
u16_t len;
/* calculate UDP packet length */
len = 12 +8 +10 +10;
/* allocate a buffer */
pb = pbuf_alloc(PBUF_TRANSPORT, len, PBUF_RAM);
if (pb == NULL) {
return ERR_MEM;
}
LWIP_ASSERT("pb->tot_len == pb->len", pb->tot_len == pb->len);
p = (u8_t*)pb->payload;
/* fill in pkt */
/* L2TP control header */
PUTSHORT(PPPOL2TP_HEADERFLAG_CONTROL_MANDATORY, p);
PUTSHORT(len, p); /* Length */
PUTSHORT(l2tp->source_tunnel_id, p); /* Tunnel Id */
PUTSHORT(l2tp->source_session_id, p); /* Session Id */
PUTSHORT(ns, p); /* NS Sequence number - to peer */
PUTSHORT(l2tp->peer_ns+1, p); /* NR Sequence number - expected for peer */
/* AVP - Message type */
PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 8, p); /* Mandatory flag + len field */
PUTSHORT(0, p); /* Vendor ID */
PUTSHORT(PPPOL2TP_AVPTYPE_MESSAGE, p); /* Attribute type: Message Type */
PUTSHORT(PPPOL2TP_MESSAGETYPE_ICCN, p); /* Attribute value: Message type: ICCN */
/* AVP - Framing type */
PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 10, p); /* Mandatory flag + len field */
PUTSHORT(0, p); /* Vendor ID */
PUTSHORT(PPPOL2TP_AVPTYPE_FRAMINGTYPE, p); /* Attribute type: Framing type */
PUTLONG(PPPOL2TP_FRAMINGTYPE, p); /* Attribute value: Framing type */
/* AVP - TX Connect speed */
PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 10, p); /* Mandatory flag + len field */
PUTSHORT(0, p); /* Vendor ID */
PUTSHORT(PPPOL2TP_AVPTYPE_TXCONNECTSPEED, p); /* Attribute type: TX Connect speed */
PUTLONG(PPPOL2TP_TXCONNECTSPEED, p); /* Attribute value: TX Connect speed */
udp_sendto(l2tp->udp, pb, &l2tp->remote_ip, l2tp->tunnel_port);
pbuf_free(pb);
return ERR_OK;
}
/* Send a ZLB ACK packet */
static err_t pppol2tp_send_zlb(pppol2tp_pcb *l2tp, u16_t ns) {
struct pbuf *pb;
u8_t *p;
u16_t len;
/* calculate UDP packet length */
len = 12;
/* allocate a buffer */
pb = pbuf_alloc(PBUF_TRANSPORT, len, PBUF_RAM);
if (pb == NULL) {
return ERR_MEM;
}
LWIP_ASSERT("pb->tot_len == pb->len", pb->tot_len == pb->len);
p = (u8_t*)pb->payload;
/* fill in pkt */
/* L2TP control header */
PUTSHORT(PPPOL2TP_HEADERFLAG_CONTROL_MANDATORY, p);
PUTSHORT(len, p); /* Length */
PUTSHORT(l2tp->source_tunnel_id, p); /* Tunnel Id */
PUTSHORT(0, p); /* Session Id */
PUTSHORT(ns, p); /* NS Sequence number - to peer */
PUTSHORT(l2tp->peer_ns+1, p); /* NR Sequence number - expected for peer */
udp_sendto(l2tp->udp, pb, &l2tp->remote_ip, l2tp->tunnel_port);
pbuf_free(pb);
return ERR_OK;
}
/* Send a StopCCN packet */
static err_t pppol2tp_send_stopccn(pppol2tp_pcb *l2tp, u16_t ns) {
struct pbuf *pb;
u8_t *p;
u16_t len;
/* calculate UDP packet length */
len = 12 +8 +8 +8;
/* allocate a buffer */
pb = pbuf_alloc(PBUF_TRANSPORT, len, PBUF_RAM);
if (pb == NULL) {
return ERR_MEM;
}
LWIP_ASSERT("pb->tot_len == pb->len", pb->tot_len == pb->len);
p = (u8_t*)pb->payload;
/* fill in pkt */
/* L2TP control header */
PUTSHORT(PPPOL2TP_HEADERFLAG_CONTROL_MANDATORY, p);
PUTSHORT(len, p); /* Length */
PUTSHORT(l2tp->source_tunnel_id, p); /* Tunnel Id */
PUTSHORT(0, p); /* Session Id */
PUTSHORT(ns, p); /* NS Sequence number - to peer */
PUTSHORT(l2tp->peer_ns+1, p); /* NR Sequence number - expected for peer */
/* AVP - Message type */
PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 8, p); /* Mandatory flag + len field */
PUTSHORT(0, p); /* Vendor ID */
PUTSHORT(PPPOL2TP_AVPTYPE_MESSAGE, p); /* Attribute type: Message Type */
PUTSHORT(PPPOL2TP_MESSAGETYPE_STOPCCN, p); /* Attribute value: Message type: StopCCN */
/* AVP - Assign tunnel ID */
PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 8, p); /* Mandatory flag + len field */
PUTSHORT(0, p); /* Vendor ID */
PUTSHORT(PPPOL2TP_AVPTYPE_TUNNELID, p); /* Attribute type: Tunnel ID */
PUTSHORT(l2tp->remote_tunnel_id, p); /* Attribute value: Tunnel ID */
/* AVP - Result code */
PUTSHORT(PPPOL2TP_AVPHEADERFLAG_MANDATORY + 8, p); /* Mandatory flag + len field */
PUTSHORT(0, p); /* Vendor ID */
PUTSHORT(PPPOL2TP_AVPTYPE_RESULTCODE, p); /* Attribute type: Result code */
PUTSHORT(PPPOL2TP_RESULTCODE, p); /* Attribute value: Result code */
udp_sendto(l2tp->udp, pb, &l2tp->remote_ip, l2tp->tunnel_port);
pbuf_free(pb);
return ERR_OK;
}
err_t pppol2tp_xmit(pppol2tp_pcb *l2tp, struct pbuf *pb) {
u8_t *p;
/* are we ready to process data yet? */
if (l2tp->phase < PPPOL2TP_STATE_DATA) {
pbuf_free(pb);
return ERR_CONN;
}
/* make room for L2TP header - should not fail */
if (pbuf_header(pb, PPPOL2TP_OUTPUT_DATA_HEADER_LEN) != 0) {
/* bail out */
PPPDEBUG(LOG_ERR, ("pppol2tp: pppol2tp_pcb: could not allocate room for L2TP header\n"));
LINK_STATS_INC(link.lenerr);
pbuf_free(pb);
return ERR_BUF;
}
p = pb->payload;
PUTSHORT(PPPOL2TP_HEADERFLAG_DATA_MANDATORY, p);
PUTSHORT(l2tp->source_tunnel_id, p); /* Tunnel Id */
PUTSHORT(l2tp->source_session_id, p); /* Session Id */
udp_sendto(l2tp->udp, pb, &l2tp->remote_ip, l2tp->tunnel_port);
pbuf_free(pb);
return ERR_OK;
}
#endif /* PPP_SUPPORT && PPPOL2TP_SUPPORT */