summaryrefslogtreecommitdiff
path: root/accel-pppd/ctrl/l2tp
diff options
context:
space:
mode:
Diffstat (limited to 'accel-pppd/ctrl/l2tp')
-rw-r--r--accel-pppd/ctrl/l2tp/CMakeLists.txt17
-rw-r--r--accel-pppd/ctrl/l2tp/attr_defs.h73
-rw-r--r--accel-pppd/ctrl/l2tp/dict.c271
-rw-r--r--accel-pppd/ctrl/l2tp/dict/dictionary2
-rw-r--r--accel-pppd/ctrl/l2tp/dict/dictionary.rfc266155
-rw-r--r--accel-pppd/ctrl/l2tp/dict/dictionary.rfc393115
-rw-r--r--accel-pppd/ctrl/l2tp/dict2c.py20
-rw-r--r--accel-pppd/ctrl/l2tp/l2tp.c1141
-rw-r--r--accel-pppd/ctrl/l2tp/l2tp.h88
-rw-r--r--accel-pppd/ctrl/l2tp/l2tp_kernel.h163
-rw-r--r--accel-pppd/ctrl/l2tp/l2tp_prot.h55
-rw-r--r--accel-pppd/ctrl/l2tp/netlink.c94
-rw-r--r--accel-pppd/ctrl/l2tp/packet.c494
13 files changed, 2488 insertions, 0 deletions
diff --git a/accel-pppd/ctrl/l2tp/CMakeLists.txt b/accel-pppd/ctrl/l2tp/CMakeLists.txt
new file mode 100644
index 00000000..b2952899
--- /dev/null
+++ b/accel-pppd/ctrl/l2tp/CMakeLists.txt
@@ -0,0 +1,17 @@
+INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})
+
+ADD_DEFINITIONS(-DDICTIONARY="${CMAKE_INSTALL_PREFIX}/share/accel-ppp/l2tp/dictionary")
+
+ADD_LIBRARY(l2tp SHARED
+ l2tp.c
+ dict.c
+ packet.c
+ # netlink.c
+)
+#TARGET_LINK_LIBRARIES(l2tp nl nl-genl)
+
+INSTALL(TARGETS l2tp LIBRARY DESTINATION lib/accel-ppp)
+
+FILE(GLOB dict "${CMAKE_CURRENT_SOURCE_DIR}/dict/*")
+INSTALL(FILES ${dict} DESTINATION share/accel-ppp/l2tp)
+
diff --git a/accel-pppd/ctrl/l2tp/attr_defs.h b/accel-pppd/ctrl/l2tp/attr_defs.h
new file mode 100644
index 00000000..e7991574
--- /dev/null
+++ b/accel-pppd/ctrl/l2tp/attr_defs.h
@@ -0,0 +1,73 @@
+#ifndef __ATTR_DEFS_H
+#define __ATTR_DEFS_H
+
+#define Message_Type 0
+#define Random_Vector 36
+#define Result_Code 1
+#define Protocol_Version 2
+#define Framing_Capabilities 3
+#define Bearer_Capabilities 4
+#define Tie_Breaker 5
+#define Firmware_Revision 6
+#define Host_Name 7
+#define Vendor_Name 8
+#define Assigned_Tunnel_ID 9
+#define Recv_Window_Size 10
+#define Challenge 11
+#define Challenge_Response 13
+#define Cause_Code 12
+#define Assigned_Session_ID 14
+#define Call_Serial_Number 15
+#define Minimum_BPS 16
+#define Maximum_BPS 17
+#define Bearer_Type 18
+#define Framing_Type 19
+#define Called_Number 21
+#define Calling_Number 22
+#define Sub_Address 23
+#define TX_Speed 24
+#define RX_Speed 38
+#define Physical_Channel_ID 25
+#define Private_Group_ID 37
+#define Sequencing_Required 39
+#define Init_Recv_LCP 26
+#define Last_Sent_LCP 27
+#define Last_Recv_LCP 28
+#define Proxy_Authen_Type 29
+#define Proxy_Authen_Name 30
+#define Proxy_Authen_Challenge 31
+#define Proxy_Authen_ID 32
+#define Proxy_Authen_Response 33
+#define Call_Errors 34
+#define ACCM 35
+#define Message_Type_Start_Ctrl_Conn_Request 1
+#define Message_Type_Start_Ctrl_Conn_Reply 2
+#define Message_Type_Start_Ctrl_Conn_Connected 3
+#define Message_Type_Stop_Ctrl_Conn_Notify 4
+#define Message_Type_Hello 6
+#define Message_Type_Outgoing_Call_Request 7
+#define Message_Type_Outgoing_Call_Reply 8
+#define Message_Type_Outgoing_Call_Connected 9
+#define Message_Type_Incoming_Call_Request 10
+#define Message_Type_Incoming_Call_Reply 11
+#define Message_Type_Incoming_Call_Connected 12
+#define Message_Type_Call_Disconnect_Notify 14
+#define Message_Type_WAN_Error_Notify 15
+#define Message_Type_Set_Link_Info 16
+#define Message_Digest 59
+#define Router_ID 60
+#define Assigned_Connection_ID 61
+#define Pseudowire_Capabilities 62
+#define Prefered_Language 72
+#define Local_Session_ID 63
+#define Remote_Session_ID 64
+#define Assigned_Cookie 65
+#define Remote_End_ID 66
+#define Pseudowire_Type 68
+#define L2_Specific_Sublayer 69
+#define Data_Sequencing 70
+#define TX_Connect_Speeed 74
+#define RX_Connect_Speeed 75
+#define Circuit_Status 71
+
+#endif
diff --git a/accel-pppd/ctrl/l2tp/dict.c b/accel-pppd/ctrl/l2tp/dict.c
new file mode 100644
index 00000000..c93ccd48
--- /dev/null
+++ b/accel-pppd/ctrl/l2tp/dict.c
@@ -0,0 +1,271 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+#include <limits.h>
+#include <unistd.h>
+
+#include "list.h"
+#include "l2tp.h"
+#include "log.h"
+#include "triton.h"
+
+#include "memdebug.h"
+
+struct l2tp_dict_t
+{
+ struct list_head items;
+};
+
+static struct l2tp_dict_t *dict;
+
+#define BUF_SIZE 1024
+static char *path, *fname1, *buf;
+
+struct l2tp_dict_attr_t *l2tp_dict_find_attr_by_name(const char *name)
+{
+ struct l2tp_dict_attr_t *attr;
+
+ list_for_each_entry(attr, &dict->items, entry) {
+ if (!strcmp(attr->name, name))
+ return attr;
+ }
+
+ return NULL;
+}
+
+struct l2tp_dict_attr_t *l2tp_dict_find_attr_by_id(int id)
+{
+ struct l2tp_dict_attr_t *attr;
+
+ list_for_each_entry(attr, &dict->items, entry) {
+ if (attr->id == id)
+ return attr;
+ }
+
+ return NULL;
+}
+
+struct l2tp_dict_value_t *l2tp_dict_find_value(struct l2tp_dict_attr_t *attr, l2tp_value_t val)
+{
+ struct l2tp_dict_value_t *v;
+
+ list_for_each_entry(v, &attr->values, entry) {
+ switch (attr->type) {
+ case ATTR_TYPE_INT16:
+ if (v->val.int16 == val.int16)
+ return v;
+ break;
+ case ATTR_TYPE_INT32:
+ if (v->val.int32 == val.int32)
+ return v;
+ break;
+ }
+ }
+
+ return NULL;
+}
+
+static char *skip_word(char *ptr)
+{
+ for(; *ptr; ptr++)
+ if (*ptr == ' ' || *ptr == '\t' || *ptr == '\n')
+ break;
+ return ptr;
+}
+
+static char *skip_space(char *ptr)
+{
+ for(; *ptr; ptr++)
+ if (*ptr != ' ' && *ptr != '\t')
+ break;
+ return ptr;
+}
+
+static int split(char *buf, char **ptr)
+{
+ int i;
+
+ for (i = 0; i < 6; i++) {
+ buf = skip_word(buf);
+ if (!*buf)
+ return i;
+
+ *buf = 0;
+
+ buf = skip_space(buf + 1);
+ if (!*buf)
+ return i;
+
+ ptr[i] = buf;
+ }
+
+ buf = skip_word(buf);
+ //if (*buf == '\n')
+ *buf = 0;
+ //else if (*buf)
+ // return -1;
+
+ return i;
+}
+
+
+static int dict_load(const char *fname)
+{
+ FILE *f;
+ char *ptr[6], *endptr;
+ struct l2tp_dict_attr_t *attr;
+ struct l2tp_dict_value_t *value;
+ struct list_head *items;
+ int i, r, n = 0;
+
+ f = fopen(fname, "r");
+ if (!f) {
+ log_emerg("l2tp: open '%s': %s\n", fname, strerror(errno));
+ return -1;
+ }
+
+ items = &dict->items;
+ while (fgets(buf, BUF_SIZE, f)) {
+ n++;
+ if (buf[0] == '#' || buf[0] == '\n' || buf[0] == 0)
+ continue;
+
+ r = split(buf, ptr);
+
+ if (!strcmp(buf, "$INCLUDE")) {
+ if (r != 1)
+ goto out_syntax;
+
+ for (i = strlen(path) - 1; i; i--) {
+ if (path[i] == '/') {
+ path[i + 1] = 0;
+ break;
+ }
+ }
+
+ strcpy(fname1, path);
+ strcat(fname1, ptr[0]);
+
+ if (dict_load(fname1))
+ goto out_err;
+ } else if (!strcmp(buf, "ATTRIBUTE")) {
+ if (r < 3)
+ goto out_syntax;
+
+ attr = malloc(sizeof(*attr));
+ memset(attr, 0, sizeof(*attr));
+ list_add_tail(&attr->entry, items);
+ INIT_LIST_HEAD(&attr->values);
+
+ attr->name = strdup(ptr[0]);
+ attr->id = strtol(ptr[1], &endptr, 10);
+ if (*endptr != 0)
+ goto out_syntax;
+
+ if (!strcmp(ptr[2], "none"))
+ attr->type = ATTR_TYPE_NONE;
+ else if (!strcmp(ptr[2], "int16"))
+ attr->type = ATTR_TYPE_INT16;
+ else if (!strcmp(ptr[2], "int32"))
+ attr->type = ATTR_TYPE_INT32;
+ else if (!strcmp(ptr[2], "int64"))
+ attr->type = ATTR_TYPE_INT64;
+ else if (!strcmp(ptr[2], "octets"))
+ attr->type = ATTR_TYPE_OCTETS;
+ else if (!strcmp(ptr[2], "string"))
+ attr->type = ATTR_TYPE_STRING;
+ else
+ goto out_syntax;
+
+ attr->M = -1;
+ attr->H = -1;
+
+ for (i = 3; i < r; i++) {
+ if (!strcmp(ptr[i], "M=0"))
+ attr->M = 0;
+ else if (!strcmp(ptr[i], "M=1"))
+ attr->M = 1;
+ else if (!strcmp(ptr[i], "H=0"))
+ attr->H = 0;
+ else if (!strcmp(ptr[i], "H=1"))
+ attr->H = 1;
+ else
+ goto out_syntax;
+ }
+ } else if (!strcmp(buf, "VALUE")) {
+ if (r != 3)
+ goto out_syntax;
+
+ attr = l2tp_dict_find_attr_by_name(ptr[0]);
+ if (!attr) {
+ log_emerg("l2tp:%s:%i: attribute not found\n", fname, n);
+ goto out_err;
+ }
+
+ value = malloc(sizeof(*value));
+ memset(value, 0, sizeof(*value));
+ list_add_tail(&value->entry, &attr->values);
+
+ value->name = strdup(ptr[1]);
+ switch (attr->type) {
+ case ATTR_TYPE_INT16:
+ case ATTR_TYPE_INT32:
+ value->val.int16 = strtol(ptr[2], &endptr, 10);
+ if (*endptr != 0)
+ goto out_syntax;
+ break;
+ case ATTR_TYPE_STRING:
+ value->val.string = strdup(ptr[2]);
+ break;
+ }
+ } else
+ goto out_syntax;
+ }
+
+ fclose(f);
+
+ return 0;
+
+out_syntax:
+ log_emerg("l2tp:%s:%i: syntaxis error\n", fname, n);
+out_err:
+ fclose(f);
+ return -1;
+}
+
+static int l2tp_dict_load(const char *fname)
+{
+ int r;
+
+ dict = _malloc(sizeof(*dict));
+ memset(dict, 0, sizeof(*dict));
+ INIT_LIST_HEAD(&dict->items);
+
+ path = _malloc(PATH_MAX);
+ fname1 = _malloc(PATH_MAX);
+ buf = _malloc(BUF_SIZE);
+
+ strcpy(path, fname);
+
+ r = dict_load(fname);
+
+ _free(buf);
+ _free(fname1);
+ _free(path);
+
+ return r;
+}
+
+static void __init dict_init(void)
+{
+ char *opt;
+
+ opt = conf_get_opt("l2tp", "dictionary");
+ if (!opt)
+ opt = DICTIONARY;
+
+ if (l2tp_dict_load(opt))
+ _exit(EXIT_FAILURE);
+}
+
diff --git a/accel-pppd/ctrl/l2tp/dict/dictionary b/accel-pppd/ctrl/l2tp/dict/dictionary
new file mode 100644
index 00000000..f5d7c423
--- /dev/null
+++ b/accel-pppd/ctrl/l2tp/dict/dictionary
@@ -0,0 +1,2 @@
+$INCLUDE dictionary.rfc2661
+$INCLUDE dictionary.rfc3931
diff --git a/accel-pppd/ctrl/l2tp/dict/dictionary.rfc2661 b/accel-pppd/ctrl/l2tp/dict/dictionary.rfc2661
new file mode 100644
index 00000000..3fd9614e
--- /dev/null
+++ b/accel-pppd/ctrl/l2tp/dict/dictionary.rfc2661
@@ -0,0 +1,55 @@
+ATTRIBUTE Message-Type 0 int16 H=0
+ATTRIBUTE Random-Vector 36 octets M=1 H=0
+ATTRIBUTE Result-Code 1 octets M=1 H=0
+ATTRIBUTE Protocol-Version 2 int16 M=1 H=0
+ATTRIBUTE Framing-Capabilities 3 int32 M=1
+ATTRIBUTE Bearer-Capabilities 4 int32 M=1
+ATTRIBUTE Tie-Breaker 5 int64 M=0
+ATTRIBUTE Firmware-Revision 6 int16 M=0
+ATTRIBUTE Host-Name 7 string M=1
+ATTRIBUTE Vendor-Name 8 string M=0
+ATTRIBUTE Assigned-Tunnel-ID 9 int16 M=1
+ATTRIBUTE Recv-Window-Size 10 int16 M=1
+ATTRIBUTE Challenge 11 octets M=1
+ATTRIBUTE Challenge-Response 13 octets M=1
+ATTRIBUTE Cause-Code 12 int32 M=1 H=0
+ATTRIBUTE Assigned-Session-ID 14 int16 M=1
+ATTRIBUTE Call-Serial-Number 15 int32 M=1
+ATTRIBUTE Minimum-BPS 16 int32 M=1
+ATTRIBUTE Maximum-BPS 17 int32 M=1
+ATTRIBUTE Bearer-Type 18 int32 M=1
+ATTRIBUTE Framing-Type 19 int32 M=1
+ATTRIBUTE Called-Number 21 string M=1
+ATTRIBUTE Calling-Number 22 string M=1
+ATTRIBUTE Sub-Address 23 string M=1
+ATTRIBUTE TX-Speed 24 int32 M=1
+ATTRIBUTE RX-Speed 38 int32 M=1
+ATTRIBUTE Physical-Channel-ID 25 int32 M=0
+ATTRIBUTE Private-Group-ID 37 int32 M=0
+ATTRIBUTE Sequencing-Required 39 none M=1 H=0
+ATTRIBUTE Init-Recv-LCP 26 octets M=0
+ATTRIBUTE Last-Sent-LCP 27 octets M=0
+ATTRIBUTE Last-Recv-LCP 28 octets M=0
+ATTRIBUTE Proxy-Authen-Type 29 int16 M=0
+ATTRIBUTE Proxy-Authen-Name 30 string M=0
+ATTRIBUTE Proxy-Authen-Challenge 31 octets M=0
+ATTRIBUTE Proxy-Authen-ID 32 int16 M=0
+ATTRIBUTE Proxy-Authen-Response 33 octets M=0
+ATTRIBUTE Call-Errors 34 octets M=1
+ATTRIBUTE ACCM 35 octets M=1
+
+VALUE Message-Type Start-Ctrl-Conn-Request 1
+VALUE Message-Type Start-Ctrl-Conn-Reply 2
+VALUE Message-Type Start-Ctrl-Conn-Connected 3
+VALUE Message-Type Stop-Ctrl-Conn-Notify 4
+VALUE Message-Type Hello 6
+VALUE Message-Type Outgoing-Call-Request 7
+VALUE Message-Type Outgoing-Call-Reply 8
+VALUE Message-Type Outgoing-Call-Connected 9
+VALUE Message-Type Incoming-Call-Request 10
+VALUE Message-Type Incoming-Call-Reply 11
+VALUE Message-Type Incoming-Call-Connected 12
+VALUE Message-Type Call-Disconnect-Notify 14
+VALUE Message-Type WAN-Error-Notify 15
+VALUE Message-Type Set-Link-Info 16
+
diff --git a/accel-pppd/ctrl/l2tp/dict/dictionary.rfc3931 b/accel-pppd/ctrl/l2tp/dict/dictionary.rfc3931
new file mode 100644
index 00000000..d6ad410b
--- /dev/null
+++ b/accel-pppd/ctrl/l2tp/dict/dictionary.rfc3931
@@ -0,0 +1,15 @@
+ATTRIBUTE Message-Digest 59 octets M=1 H=0
+ATTRIBUTE Router-ID 60 int32 H=0
+ATTRIBUTE Assigned-Connection-ID 61 int32
+ATTRIBUTE Pseudowire-Capabilities 62 octets
+ATTRIBUTE Prefered-Language 72 octets
+ATTRIBUTE Local-Session-ID 63 int32
+ATTRIBUTE Remote-Session-ID 64 int32
+ATTRIBUTE Assigned-Cookie 65 octets
+ATTRIBUTE Remote-End-ID 66 octets
+ATTRIBUTE Pseudowire-Type 68 int16
+ATTRIBUTE L2-Specific-Sublayer 69 int16
+ATTRIBUTE Data-Sequencing 70 int16
+ATTRIBUTE TX-Connect-Speeed 74 int64
+ATTRIBUTE RX-Connect-Speeed 75 int64
+ATTRIBUTE Circuit-Status 71 int16
diff --git a/accel-pppd/ctrl/l2tp/dict2c.py b/accel-pppd/ctrl/l2tp/dict2c.py
new file mode 100644
index 00000000..ff0961e7
--- /dev/null
+++ b/accel-pppd/ctrl/l2tp/dict2c.py
@@ -0,0 +1,20 @@
+import sys,re
+
+hdr = file(sys.argv[2],'w')
+
+def process(fname, hdr):
+ for line in file(fname):
+ if line[:-1].strip() == '':
+ continue
+ if line[0] == '#':
+ continue
+ f = re.compile('[$.a-zA-Z0-9\-]+').findall(line)
+ if f[0] == 'ATTRIBUTE' or f[0] == 'VENDOR':
+ hdr.write('#define {0} {1}\n'.format(f[1].replace('-','_').replace('.','_'), f[2]))
+ elif f[0] == 'VALUE':
+ hdr.write('#define {0}_{1} {2}\n'.format(f[1].replace('-','_').replace('.','_'), f[2].replace('-','_'),f[3]))
+ elif f[0] == '$INCLUDE':
+ process(f[1], hdr)
+
+if __name__ == '__main__':
+ process(sys.argv[1], hdr)
diff --git a/accel-pppd/ctrl/l2tp/l2tp.c b/accel-pppd/ctrl/l2tp/l2tp.c
new file mode 100644
index 00000000..ca56051b
--- /dev/null
+++ b/accel-pppd/ctrl/l2tp/l2tp.c
@@ -0,0 +1,1141 @@
+#include <unistd.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdarg.h>
+#include <errno.h>
+#include <string.h>
+#include <fcntl.h>
+#include <time.h>
+#include <pthread.h>
+#include <arpa/inet.h>
+#include <netinet/in.h>
+#include <sys/socket.h>
+#include <linux/socket.h>
+#include <linux/if.h>
+#include <linux/if_ether.h>
+#include <linux/if_pppox.h>
+
+#include "triton.h"
+#include "mempool.h"
+#include "log.h"
+#include "ppp.h"
+#include "events.h"
+#include "utils.h"
+#include "iprange.h"
+#include "cli.h"
+
+#include "memdebug.h"
+
+#include "l2tp.h"
+#include "attr_defs.h"
+
+#ifndef SOL_PPPOL2TP
+#define SOL_PPPOL2TP 273
+#endif
+
+#define STATE_WAIT_SCCCN 1
+#define STATE_WAIT_ICRQ 2
+#define STATE_WAIT_ICCN 3
+#define STATE_WAIT_OCRP 4
+#define STATE_WAIT_OCCN 5
+#define STATE_ESTB 6
+#define STATE_PPP 7
+#define STATE_FIN 8
+#define STATE_CLOSE 0
+
+int conf_verbose = 0;
+int conf_timeout = 60;
+int conf_rtimeout = 5;
+int conf_retransmit = 5;
+int conf_hello_interval = 60;
+char *conf_host_name = NULL;
+
+static unsigned int stat_active;
+static unsigned int stat_starting;
+
+struct l2tp_serv_t
+{
+ struct triton_context_t ctx;
+ struct triton_md_handler_t hnd;
+ struct sockaddr_in addr;
+};
+
+struct l2tp_conn_t
+{
+ struct triton_context_t ctx;
+ struct triton_md_handler_t hnd;
+ struct triton_timer_t timeout_timer;
+ struct triton_timer_t rtimeout_timer;
+ struct triton_timer_t hello_timer;
+
+ int tunnel_fd;
+
+ struct sockaddr_in addr;
+ uint16_t tid;
+ uint16_t sid;
+ uint16_t peer_tid;
+ uint16_t peer_sid;
+ uint32_t framing_cap;
+
+ int retransmit;
+ uint16_t Ns, Nr;
+ struct list_head send_queue;
+
+ int state;
+ int state1;
+ int state2;
+
+ struct ppp_ctrl_t ctrl;
+ struct ppp_t ppp;
+};
+
+static pthread_mutex_t l2tp_lock = PTHREAD_MUTEX_INITIALIZER;
+static struct l2tp_conn_t **l2tp_conn;
+static uint16_t l2tp_tid;
+
+static mempool_t l2tp_conn_pool;
+
+static void l2tp_timeout(struct triton_timer_t *t);
+static void l2tp_rtimeout(struct triton_timer_t *t);
+static void l2tp_send_HELLO(struct triton_timer_t *t);
+static void l2tp_send_SCCRP(struct l2tp_conn_t *conn);
+static int l2tp_send(struct l2tp_conn_t *conn, struct l2tp_packet_t *pack, int log_debug);
+static int l2tp_conn_read(struct triton_md_handler_t *);
+
+static void l2tp_disconnect(struct l2tp_conn_t *conn)
+{
+ struct l2tp_packet_t *pack;
+
+ triton_md_unregister_handler(&conn->hnd);
+ close(conn->hnd.fd);
+
+ if (conn->timeout_timer.tpd)
+ triton_timer_del(&conn->timeout_timer);
+
+ if (conn->rtimeout_timer.tpd)
+ triton_timer_del(&conn->rtimeout_timer);
+
+ if (conn->hello_timer.tpd)
+ triton_timer_del(&conn->hello_timer);
+
+ if (conn->state == STATE_PPP) {
+ __sync_sub_and_fetch(&stat_active, 1);
+ conn->state = STATE_FIN;
+ ppp_terminate(&conn->ppp, TERM_USER_REQUEST, 1);
+ } else if (conn->state != STATE_FIN)
+ __sync_sub_and_fetch(&stat_starting, 1);
+
+ pthread_mutex_lock(&l2tp_lock);
+ l2tp_conn[conn->tid] = NULL;
+ pthread_mutex_unlock(&l2tp_lock);
+
+ if (conn->ppp.fd != -1)
+ close(conn->ppp.fd);
+
+ if (conn->tunnel_fd != -1)
+ close(conn->tunnel_fd);
+
+ triton_event_fire(EV_CTRL_FINISHED, &conn->ppp);
+
+ log_ppp_info1("disconnected\n");
+
+ triton_context_unregister(&conn->ctx);
+
+ while (!list_empty(&conn->send_queue)) {
+ pack = list_entry(conn->send_queue.next, typeof(*pack), entry);
+ list_del(&pack->entry);
+ l2tp_packet_free(pack);
+ }
+
+ if (conn->ppp.chan_name)
+ _free(conn->ppp.chan_name);
+
+ _free(conn->ctrl.calling_station_id);
+ _free(conn->ctrl.called_station_id);
+
+ mempool_free(conn);
+}
+
+static int l2tp_terminate(struct l2tp_conn_t *conn, int res, int err)
+{
+ struct l2tp_packet_t *pack;
+ struct l2tp_avp_result_code rc = {res, err};
+
+ log_ppp_debug("l2tp: terminate (%i, %i)\n", res, err);
+
+ pack = l2tp_packet_alloc(2, Message_Type_Stop_Ctrl_Conn_Notify, &conn->addr);
+ if (!pack)
+ return -1;
+
+ if (l2tp_packet_add_int16(pack, Assigned_Tunnel_ID, conn->tid, 1))
+ goto out_err;
+ if (l2tp_packet_add_octets(pack, Result_Code, (uint8_t *)&rc, sizeof(rc), 0))
+ goto out_err;
+
+ l2tp_send(conn, pack, 0);
+
+ conn->state = STATE_FIN;
+
+ return 0;
+
+out_err:
+ l2tp_packet_free(pack);
+ return -1;
+}
+
+static void l2tp_ppp_started(struct ppp_t *ppp)
+{
+ struct l2tp_conn_t *conn = container_of(ppp, typeof(*conn), ppp);
+
+ log_ppp_debug("l2tp: ppp started\n");
+
+ if (conf_hello_interval)
+ triton_timer_add(&conn->ctx, &conn->hello_timer, 0);
+}
+
+static void l2tp_ppp_finished(struct ppp_t *ppp)
+{
+ struct l2tp_conn_t *conn = container_of(ppp, typeof(*conn), ppp);
+
+ log_ppp_debug("l2tp: ppp finished\n");
+
+ if (conn->state != STATE_FIN) {
+ __sync_sub_and_fetch(&stat_active, 1);
+ if (l2tp_terminate(conn, 0, 0))
+ triton_context_call(&conn->ctx, (triton_event_func)l2tp_disconnect, conn);
+ }
+}
+
+static void l2tp_conn_close(struct triton_context_t *ctx)
+{
+ struct l2tp_conn_t *conn = container_of(ctx, typeof(*conn), ctx);
+
+ if (conn->state == STATE_PPP) {
+ __sync_sub_and_fetch(&stat_active, 1);
+ conn->state = STATE_FIN;
+ ppp_terminate(&conn->ppp, TERM_ADMIN_RESET, 1);
+ }
+
+ if (l2tp_terminate(conn, 0, 0))
+ l2tp_disconnect(conn);
+}
+
+static int l2tp_tunnel_alloc(struct l2tp_serv_t *serv, struct l2tp_packet_t *pack, struct in_pktinfo *pkt_info, struct l2tp_attr_t *assigned_tid, struct l2tp_attr_t *framing_cap)
+{
+ struct l2tp_conn_t *conn;
+ struct sockaddr_in addr;
+ uint16_t tid;
+ //char *opt;
+ int flag = 1;
+
+ conn = mempool_alloc(l2tp_conn_pool);
+ if (!conn) {
+ log_emerg("l2tp: out of memory\n");
+ return -1;
+ }
+
+ memset(conn, 0, sizeof(*conn));
+ INIT_LIST_HEAD(&conn->send_queue);
+
+ conn->hnd.fd = socket(PF_INET, SOCK_DGRAM, 0);
+ if (conn->hnd.fd < 0) {
+ log_error("l2tp: socket: %s\n", strerror(errno));
+ mempool_free(conn);
+ return -1;
+ }
+
+ memset(&addr, 0, sizeof(addr));
+ addr.sin_family = AF_INET;
+ addr.sin_addr = pkt_info->ipi_addr;
+ addr.sin_port = htons(L2TP_PORT);
+
+ setsockopt(conn->hnd.fd, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag));
+ if (bind(conn->hnd.fd, &addr, sizeof(addr))) {
+ log_error("l2tp: bind: %s\n", strerror(errno));
+ goto out_err;
+ }
+
+ if (connect(conn->hnd.fd, (struct sockaddr *)&pack->addr, sizeof(addr))) {
+ log_error("l2tp: connect: %s\n", strerror(errno));
+ goto out_err;
+ }
+
+ if (fcntl(conn->hnd.fd, F_SETFL, O_NONBLOCK)) {
+ log_emerg("l2tp: failed to set nonblocking mode: %s\n", strerror(errno));
+ goto out_err;
+ }
+
+ pthread_mutex_lock(&l2tp_lock);
+ for (tid = l2tp_tid + 1; tid != l2tp_tid; tid++) {
+ if (tid == L2TP_MAX_TID)
+ tid = 1;
+ if (!l2tp_conn[tid]) {
+ l2tp_conn[tid] = conn;
+ conn->tid = tid;
+ break;
+ }
+ }
+ pthread_mutex_unlock(&l2tp_lock);
+
+ if (!conn->tid) {
+ if (conf_verbose)
+ log_warn("l2tp: no free tid available\n");
+ mempool_free(conn);
+ return -1;
+ }
+
+ conn->sid = 1;
+
+ memcpy(&conn->addr, &pack->addr, sizeof(pack->addr));
+ conn->peer_tid = assigned_tid->val.uint16;
+ conn->framing_cap = framing_cap->val.uint32;
+
+ conn->ctx.before_switch = log_switch;
+ conn->ctx.close = l2tp_conn_close;
+ conn->hnd.read = l2tp_conn_read;
+ conn->timeout_timer.expire = l2tp_timeout;
+ conn->timeout_timer.period = conf_timeout * 1000;
+ conn->rtimeout_timer.expire = l2tp_rtimeout;
+ conn->rtimeout_timer.period = conf_rtimeout * 1000;
+ conn->hello_timer.expire = l2tp_send_HELLO;
+ conn->hello_timer.period = conf_hello_interval * 1000;
+ conn->ctrl.ctx = &conn->ctx;
+ conn->ctrl.name = "l2tp";
+ conn->ctrl.started = l2tp_ppp_started;
+ conn->ctrl.finished = l2tp_ppp_finished;
+ conn->ctrl.max_mtu = 1420;
+
+ conn->ctrl.calling_station_id = _malloc(17);
+ conn->ctrl.called_station_id = _malloc(17);
+ u_inet_ntoa(conn->addr.sin_addr.s_addr, conn->ctrl.calling_station_id);
+ u_inet_ntoa(addr.sin_addr.s_addr, conn->ctrl.called_station_id);
+
+ ppp_init(&conn->ppp);
+ conn->ppp.ctrl = &conn->ctrl;
+ conn->ppp.fd = -1;
+ conn->tunnel_fd = -1;
+
+ triton_context_register(&conn->ctx, &conn->ppp);
+ triton_md_register_handler(&conn->ctx, &conn->hnd);
+ triton_md_enable_handler(&conn->hnd, MD_MODE_READ);
+ triton_context_wakeup(&conn->ctx);
+
+ if (conf_verbose) {
+ log_switch(&conn->ctx, &conn->ppp);
+ log_ppp_info2("recv ");
+ l2tp_packet_print(pack, log_ppp_info2);
+ }
+
+ triton_context_call(&conn->ctx, (triton_event_func)l2tp_send_SCCRP, conn);
+
+ __sync_add_and_fetch(&stat_starting, 1);
+
+ return 0;
+
+out_err:
+ close(conn->hnd.fd);
+ mempool_free(conn);
+ return -1;
+}
+
+static int l2tp_connect(struct l2tp_conn_t *conn)
+{
+ struct sockaddr_pppol2tp pppox_addr;
+ int arg = 1;
+
+ memset(&pppox_addr, 0, sizeof(pppox_addr));
+ pppox_addr.sa_family = AF_PPPOX;
+ pppox_addr.sa_protocol = PX_PROTO_OL2TP;
+ pppox_addr.pppol2tp.fd = conn->hnd.fd;
+ memcpy(&pppox_addr.pppol2tp.addr, &conn->addr, sizeof(conn->addr));
+ pppox_addr.pppol2tp.s_tunnel = conn->tid;
+ pppox_addr.pppol2tp.d_tunnel = conn->peer_tid;
+
+ conn->tunnel_fd = socket(AF_PPPOX, SOCK_DGRAM, PX_PROTO_OL2TP);
+ if (!conn->ppp.fd) {
+ log_ppp_error("l2tp: socket(AF_PPPOX): %s\n", strerror(errno));
+ return -1;
+ }
+
+ conn->ppp.fd = socket(AF_PPPOX, SOCK_DGRAM, PX_PROTO_OL2TP);
+ if (!conn->ppp.fd) {
+ close(conn->tunnel_fd);
+ conn->tunnel_fd = -1;
+ log_ppp_error("l2tp: socket(AF_PPPOX): %s\n", strerror(errno));
+ return -1;
+ }
+
+ if (connect(conn->tunnel_fd, (struct sockaddr *)&pppox_addr, sizeof(pppox_addr)) < 0) {
+ log_ppp_error("l2tp: connect(tunnel): %s\n", strerror(errno));
+ return -1;
+ }
+
+ pppox_addr.pppol2tp.s_session = conn->sid;
+ pppox_addr.pppol2tp.d_session = conn->peer_sid;
+
+ if (connect(conn->ppp.fd, (struct sockaddr *)&pppox_addr, sizeof(pppox_addr)) < 0) {
+ log_ppp_error("l2tp: connect(session): %s\n", strerror(errno));
+ return -1;
+ }
+
+ if (setsockopt(conn->ppp.fd, SOL_PPPOL2TP, PPPOL2TP_SO_LNSMODE, &arg, sizeof(arg))) {
+ log_ppp_error("l2tp: setsockopt: %s\n", strerror(errno));
+ return -1;
+ }
+
+ conn->ppp.chan_name = _strdup(inet_ntoa(conn->addr.sin_addr));
+
+ triton_event_fire(EV_CTRL_STARTED, &conn->ppp);
+
+ if (establish_ppp(&conn->ppp))
+ return -1;
+
+ __sync_sub_and_fetch(&stat_starting, 1);
+ __sync_add_and_fetch(&stat_active, 1);
+
+ conn->state = STATE_PPP;
+
+ return 0;
+}
+
+static void l2tp_rtimeout(struct triton_timer_t *t)
+{
+ struct l2tp_conn_t *conn = container_of(t, typeof(*conn), rtimeout_timer);
+ struct l2tp_packet_t *pack;
+
+ if (!list_empty(&conn->send_queue)) {
+ log_ppp_debug("l2tp: retransmit (%i)\n", conn->retransmit);
+ if (++conn->retransmit <= conf_retransmit) {
+ pack = list_entry(conn->send_queue.next, typeof(*pack), entry);
+ pack->hdr.Nr = htons(conn->Nr + 1);
+ if (conf_verbose) {
+ log_ppp_debug("send ");
+ l2tp_packet_print(pack, log_ppp_debug);
+ }
+ if (l2tp_packet_send(conn->hnd.fd, pack) == 0)
+ return;
+ } else
+ l2tp_disconnect(conn);
+ }
+}
+
+static void l2tp_timeout(struct triton_timer_t *t)
+{
+ struct l2tp_conn_t *conn = container_of(t, typeof(*conn), timeout_timer);
+ log_ppp_debug("l2tp: timeout\n");
+ l2tp_disconnect(conn);
+}
+
+static int l2tp_send(struct l2tp_conn_t *conn, struct l2tp_packet_t *pack, int log_debug)
+{
+ conn->retransmit = 0;
+
+ pack->hdr.tid = htons(conn->peer_tid);
+ //pack->hdr.sid = htons(conn->peer_sid);
+ pack->hdr.Nr = htons(conn->Nr + 1);
+ pack->hdr.Ns = htons(conn->Ns);
+
+ if (!list_empty(&pack->attrs))
+ conn->Ns++;
+
+ if (conf_verbose) {
+ if (log_debug) {
+ log_ppp_debug("send ");
+ l2tp_packet_print(pack, log_ppp_debug);
+ } else {
+ log_ppp_info2("send ");
+ l2tp_packet_print(pack, log_ppp_info2);
+ }
+ }
+
+ if (l2tp_packet_send(conn->hnd.fd, pack))
+ goto out_err;
+
+ if (!list_empty(&pack->attrs)) {
+ list_add_tail(&pack->entry, &conn->send_queue);
+ if (!conn->rtimeout_timer.tpd)
+ triton_timer_add(&conn->ctx, &conn->rtimeout_timer, 0);
+ } else
+ l2tp_packet_free(pack);
+
+ return 0;
+
+out_err:
+ l2tp_packet_free(pack);
+ return -1;
+}
+
+static int l2tp_send_ZLB(struct l2tp_conn_t *conn)
+{
+ struct l2tp_packet_t *pack;
+
+ pack = l2tp_packet_alloc(2, 0, &conn->addr);
+ if (!pack)
+ return -1;
+
+ if (l2tp_send(conn, pack, 1))
+ return -1;
+
+ return 0;
+}
+
+static void l2tp_send_HELLO(struct triton_timer_t *t)
+{
+ struct l2tp_conn_t *conn = container_of(t, typeof(*conn), hello_timer);
+ struct l2tp_packet_t *pack;
+
+ pack = l2tp_packet_alloc(2, Message_Type_Hello, &conn->addr);
+ if (!pack) {
+ l2tp_disconnect(conn);
+ return;
+ }
+
+ if (l2tp_send(conn, pack, 1))
+ l2tp_disconnect(conn);
+}
+
+static void l2tp_send_SCCRP(struct l2tp_conn_t *conn)
+{
+ struct l2tp_packet_t *pack;
+
+ pack = l2tp_packet_alloc(2, Message_Type_Start_Ctrl_Conn_Reply, &conn->addr);
+ if (!pack)
+ goto out;
+
+ if (l2tp_packet_add_int16(pack, Protocol_Version, L2TP_V2_PROTOCOL_VERSION, 1))
+ goto out_err;
+ if (conf_host_name && l2tp_packet_add_string(pack, Host_Name, conf_host_name, 1))
+ goto out_err;
+ if (l2tp_packet_add_int32(pack, Framing_Capabilities, conn->framing_cap, 1))
+ goto out_err;
+ if (l2tp_packet_add_int16(pack, Assigned_Tunnel_ID, conn->tid, 1))
+ goto out_err;
+
+ if (l2tp_send(conn, pack, 0))
+ goto out;
+
+ if (!conn->timeout_timer.tpd)
+ triton_timer_add(&conn->ctx, &conn->timeout_timer, 0);
+ else
+ triton_timer_mod(&conn->timeout_timer, 0);
+
+ conn->state = STATE_WAIT_SCCCN;
+
+ return;
+
+out_err:
+ l2tp_packet_free(pack);
+out:
+ l2tp_disconnect(conn);
+}
+
+static int l2tp_send_ICRP(struct l2tp_conn_t *conn)
+{
+ struct l2tp_packet_t *pack;
+
+ pack = l2tp_packet_alloc(2, Message_Type_Incoming_Call_Reply, &conn->addr);
+ if (!pack)
+ return -1;
+
+ pack->hdr.sid = htons(conn->peer_sid);
+
+ if (l2tp_packet_add_int16(pack, Assigned_Session_ID, conn->sid, 1))
+ goto out_err;
+
+ l2tp_send(conn, pack, 0);
+
+ if (!conn->timeout_timer.tpd)
+ triton_timer_add(&conn->ctx, &conn->timeout_timer, 0);
+ else
+ triton_timer_mod(&conn->timeout_timer, 0);
+
+ conn->state1 = STATE_WAIT_ICCN;
+
+ return 0;
+
+out_err:
+ l2tp_packet_free(pack);
+ return -1;
+}
+
+static int l2tp_send_OCRQ(struct l2tp_conn_t *conn)
+{
+ struct l2tp_packet_t *pack;
+
+ pack = l2tp_packet_alloc(2, Message_Type_Outgoing_Call_Request, &conn->addr);
+ if (!pack)
+ return -1;
+
+ pack->hdr.sid = htons(conn->peer_sid);
+
+ if (l2tp_packet_add_int16(pack, Assigned_Session_ID, conn->sid, 1))
+ goto out_err;
+ if (l2tp_packet_add_int32(pack, Call_Serial_Number, 0, 1))
+ goto out_err;
+ if (l2tp_packet_add_int32(pack, Minimum_BPS, 100, 1))
+ goto out_err;
+ if (l2tp_packet_add_int32(pack, Maximum_BPS, 100000, 1))
+ goto out_err;
+ if (l2tp_packet_add_int32(pack, Bearer_Type, 3, 1))
+ goto out_err;
+ if (l2tp_packet_add_int32(pack, Framing_Type, 3, 1))
+ goto out_err;
+ if (l2tp_packet_add_string(pack, Called_Number, "", 1))
+ goto out_err;
+
+ if (l2tp_send(conn, pack, 0))
+ return -1;
+
+ if (!conn->timeout_timer.tpd)
+ triton_timer_add(&conn->ctx, &conn->timeout_timer, 0);
+ else
+ triton_timer_mod(&conn->timeout_timer, 0);
+
+ conn->state2 = STATE_WAIT_OCRP;
+
+ return 0;
+
+out_err:
+ l2tp_packet_free(pack);
+ return -1;
+}
+
+
+static int l2tp_recv_SCCRQ(struct l2tp_serv_t *serv, struct l2tp_packet_t *pack, struct in_pktinfo *pkt_info)
+{
+ struct l2tp_attr_t *attr;
+ struct l2tp_attr_t *protocol_version = NULL;
+ struct l2tp_attr_t *assigned_tid = NULL;
+ struct l2tp_attr_t *assigned_cid = NULL;
+ struct l2tp_attr_t *framing_cap = NULL;
+ struct l2tp_attr_t *router_id = NULL;
+
+ if (ppp_shutdown)
+ return 0;
+
+ list_for_each_entry(attr, &pack->attrs, entry) {
+ switch (attr->attr->id) {
+ case Protocol_Version:
+ protocol_version = attr;
+ break;
+ case Framing_Capabilities:
+ framing_cap = attr;
+ break;
+ case Assigned_Tunnel_ID:
+ assigned_tid = attr;
+ break;
+ case Challenge:
+ if (conf_verbose)
+ log_warn("l2tp: Challenge in SCCRQ is not supported\n");
+ return -1;
+ case Assigned_Connection_ID:
+ assigned_cid = attr;
+ break;
+ case Router_ID:
+ router_id = attr;
+ break;
+ case Message_Digest:
+ if (conf_verbose)
+ log_warn("l2tp: Message-Digest is not supported\n");
+ return -1;
+ }
+ }
+
+ if (assigned_tid) {
+ if (!protocol_version) {
+ if (conf_verbose)
+ log_warn("l2tp: SCCRQ: no Protocol-Version present in message\n");
+ return -1;
+ }
+ if (protocol_version->val.uint16 != L2TP_V2_PROTOCOL_VERSION) {
+ if (conf_verbose)
+ log_warn("l2tp: protocol version %02x is not supported\n", protocol_version->val.uint16);
+ return -1;
+ }
+ if (!framing_cap) {
+ if (conf_verbose)
+ log_warn("l2tp: SCCRQ: no Framing-Capabilities present in message\n");
+ return -1;
+ }
+
+ if (l2tp_tunnel_alloc(serv, pack, pkt_info, assigned_tid, framing_cap))
+ return -1;
+
+ } else if (assigned_cid) {
+ // not yet implemented
+ return 0;
+ } else {
+ if (conf_verbose)
+ log_warn("l2tp: SCCRQ: no Assigned-Tunnel-ID or Assigned-Connection-ID present in message\n");
+ return -1;
+ }
+
+ return 0;
+}
+
+static int l2tp_recv_SCCCN(struct l2tp_conn_t *conn, struct l2tp_packet_t *pack)
+{
+ if (conn->state == STATE_WAIT_SCCCN) {
+ triton_timer_mod(&conn->timeout_timer, 0);
+ conn->state = STATE_ESTB;
+ conn->state1 = STATE_WAIT_ICRQ;
+ }
+ else
+ log_ppp_warn("l2tp: unexpected SCCCN\n");
+
+ return 0;
+}
+
+static int l2tp_recv_StopCCN(struct l2tp_conn_t *conn, struct l2tp_packet_t *pack)
+{
+ l2tp_send_ZLB(conn);
+ return -1;
+}
+
+static int l2tp_recv_HELLO(struct l2tp_conn_t *conn, struct l2tp_packet_t *pack)
+{
+ if (l2tp_send_ZLB(conn))
+ return -1;
+
+ return 0;
+}
+
+static int l2tp_recv_ICRQ(struct l2tp_conn_t *conn, struct l2tp_packet_t *pack)
+{
+ struct l2tp_attr_t *attr;
+ struct l2tp_attr_t *assigned_sid = NULL;
+
+ if (conn->state1 != STATE_WAIT_ICRQ) {
+ log_ppp_warn("l2tp: unexpected ICRQ\n");
+ return 0;
+ }
+
+ list_for_each_entry(attr, &pack->attrs, entry) {
+ switch(attr->attr->id) {
+ case Assigned_Session_ID:
+ assigned_sid = attr;
+ break;
+ case Message_Type:
+ case Call_Serial_Number:
+ case Bearer_Type:
+ case Calling_Number:
+ case Called_Number:
+ case Sub_Address:
+ case Physical_Channel_ID:
+ break;
+ default:
+ if (attr->M) {
+ if (conf_verbose) {
+ log_ppp_warn("l2tp: ICRQ: unknown attribute %i\n", attr->attr->id);
+ if (l2tp_terminate(conn, 2, 8))
+ return -1;
+ return 0;
+ }
+ }
+ }
+ }
+
+ if (!assigned_sid) {
+ if (conf_verbose)
+ log_ppp_warn("l2tp: ICRQ: no Assigned-Session-ID attribute present in message\n");
+ if (l2tp_terminate(conn, 2, 0))
+ return -1;
+ }
+
+ conn->peer_sid = assigned_sid->val.uint16;
+
+ if (l2tp_send_ICRP(conn))
+ return -1;
+
+ if (l2tp_send_OCRQ(conn))
+ return -1;
+
+ return 0;
+}
+
+static int l2tp_recv_ICCN(struct l2tp_conn_t *conn, struct l2tp_packet_t *pack)
+{
+ if (conn->state1 != STATE_WAIT_ICCN) {
+ log_ppp_warn("l2tp: unexpected ICCN\n");
+ return 0;
+ }
+
+ conn->state1 = STATE_ESTB;
+
+ if (l2tp_connect(conn)) {
+ if (l2tp_terminate(conn, 2, 0))
+ return -1;
+ return 0;
+ }
+
+ if (l2tp_send_ZLB(conn))
+ return -1;
+
+ triton_timer_del(&conn->timeout_timer);
+
+ return 0;
+}
+
+static int l2tp_recv_OCRP(struct l2tp_conn_t *conn, struct l2tp_packet_t *pack)
+{
+ if (conn->state2 != STATE_WAIT_OCRP) {
+ log_ppp_warn("l2tp: unexpected OCRP\n");
+ return 0;
+ }
+
+ conn->state2 = STATE_WAIT_OCCN;
+
+ return 0;
+}
+
+static int l2tp_recv_OCCN(struct l2tp_conn_t *conn, struct l2tp_packet_t *pack)
+{
+ if (conn->state2 != STATE_WAIT_OCCN) {
+ log_ppp_warn("l2tp: unexpected OCCN\n");
+ return 0;
+ }
+
+ conn->state2 = STATE_ESTB;
+
+ return 0;
+}
+
+static int l2tp_recv_CDN(struct l2tp_conn_t *conn, struct l2tp_packet_t *pack)
+{
+ if (ntohs(pack->hdr.sid) != conn->sid) {
+ if (conf_verbose)
+ log_warn("l2tp: sid %i is incorrect\n", ntohs(pack->hdr.sid));
+ return 0;
+ }
+
+ if (conn->state == STATE_PPP) {
+ __sync_sub_and_fetch(&stat_active, 1);
+ conn->state = STATE_FIN;
+ ppp_terminate(&conn->ppp, TERM_USER_REQUEST, 1);
+ }
+
+ if (l2tp_terminate(conn, 0, 0))
+ return -1;
+
+ return 0;
+}
+
+static int l2tp_recv_SLI(struct l2tp_conn_t *conn, struct l2tp_packet_t *pack)
+{
+ return 0;
+}
+
+static int l2tp_conn_read(struct triton_md_handler_t *h)
+{
+ struct l2tp_conn_t *conn = container_of(h, typeof(*conn), hnd);
+ struct l2tp_packet_t *pack, *p;
+ struct l2tp_attr_t *msg_type;
+
+ while (1) {
+ if (l2tp_recv(h->fd, &pack, NULL))
+ return 0;
+
+ if (!pack)
+ continue;
+
+ if (ntohs(pack->hdr.tid) != conn->tid) {
+ if (conf_verbose)
+ log_warn("l2tp: incorrect tid %i in tunnel %i\n", ntohs(pack->hdr.tid), conn->tid);
+ l2tp_packet_free(pack);
+ continue;
+ }
+
+ if (ntohs(pack->hdr.Ns) == conn->Nr + 1) {
+ if (!list_empty(&pack->attrs))
+ conn->Nr++;
+ if (!list_empty(&conn->send_queue)) {
+ p = list_entry(conn->send_queue.next, typeof(*pack), entry);
+ list_del(&p->entry);
+ l2tp_packet_free(p);
+ conn->retransmit = 0;
+ }
+ if (!list_empty(&conn->send_queue))
+ triton_timer_mod(&conn->rtimeout_timer, 0);
+ else {
+ if (conn->rtimeout_timer.tpd)
+ triton_timer_del(&conn->rtimeout_timer);
+ if (conn->state == STATE_FIN)
+ goto drop;
+ }
+ } else {
+ if (ntohs(pack->hdr.Ns) < conn->Nr + 1 || (ntohs(pack->hdr.Ns > 32767 && conn->Nr + 1 < 32767))) {
+ log_ppp_debug("duplicate packet\n");
+ if (l2tp_send_ZLB(conn))
+ goto drop;
+ } else
+ log_ppp_debug("reordered packet\n");
+ l2tp_packet_free(pack);
+ continue;
+ }
+
+ if (list_empty(&pack->attrs)) {
+ l2tp_packet_free(pack);
+ continue;
+ }
+
+ msg_type = list_entry(pack->attrs.next, typeof(*msg_type), entry);
+
+ if (msg_type->attr->id != Message_Type) {
+ if (conf_verbose)
+ log_ppp_error("l2tp: first attribute is not Message-Type, dropping connection...\n");
+ goto drop;
+ }
+
+ if (conf_verbose) {
+ if (msg_type->val.uint16 == Message_Type_Hello) {
+ log_ppp_debug("recv ");
+ l2tp_packet_print(pack, log_ppp_debug);
+ } else {
+ log_ppp_info2("recv ");
+ l2tp_packet_print(pack, log_ppp_info2);
+ }
+ }
+
+ switch (msg_type->val.uint16) {
+ case Message_Type_Start_Ctrl_Conn_Connected:
+ if (l2tp_recv_SCCCN(conn, pack))
+ goto drop;
+ break;
+ case Message_Type_Stop_Ctrl_Conn_Notify:
+ if (l2tp_recv_StopCCN(conn, pack))
+ goto drop;
+ break;
+ case Message_Type_Hello:
+ if (l2tp_recv_HELLO(conn, pack))
+ goto drop;
+ break;
+ case Message_Type_Incoming_Call_Request:
+ if (l2tp_recv_ICRQ(conn, pack))
+ goto drop;
+ break;
+ case Message_Type_Incoming_Call_Connected:
+ if (l2tp_recv_ICCN(conn, pack))
+ goto drop;
+ break;
+ case Message_Type_Outgoing_Call_Reply:
+ if (l2tp_recv_OCRP(conn, pack))
+ goto drop;
+ break;
+ case Message_Type_Outgoing_Call_Connected:
+ if (l2tp_recv_OCCN(conn, pack))
+ goto drop;
+ break;
+ case Message_Type_Call_Disconnect_Notify:
+ if (l2tp_recv_CDN(conn, pack))
+ goto drop;
+ break;
+ case Message_Type_Set_Link_Info:
+ if (l2tp_recv_SLI(conn, pack))
+ goto drop;
+ break;
+ case Message_Type_Start_Ctrl_Conn_Request:
+ case Message_Type_Start_Ctrl_Conn_Reply:
+ case Message_Type_Outgoing_Call_Request:
+ case Message_Type_Incoming_Call_Reply:
+ case Message_Type_WAN_Error_Notify:
+ if (conf_verbose)
+ log_warn("l2tp: unexpected Message-Type %i\n", msg_type->val.uint16);
+ break;
+ default:
+ if (conf_verbose)
+ log_warn("l2tp: unknown Message-Type %i\n", msg_type->val.uint16);
+ if (msg_type->M) {
+ if (l2tp_terminate(conn, 2, 8))
+ goto drop;
+ }
+ }
+
+ l2tp_packet_free(pack);
+ }
+
+drop:
+ l2tp_packet_free(pack);
+ l2tp_disconnect(conn);
+ return -1;
+}
+
+static int l2tp_udp_read(struct triton_md_handler_t *h)
+{
+ struct l2tp_serv_t *serv = container_of(h, typeof(*serv), hnd);
+ struct l2tp_packet_t *pack;
+ struct l2tp_attr_t *msg_type;
+ struct in_pktinfo pkt_info;
+
+ while (1) {
+ if (l2tp_recv(h->fd, &pack, &pkt_info))
+ break;
+
+ if (!pack)
+ continue;
+
+ if (iprange_client_check(pack->addr.sin_addr.s_addr)) {
+ log_warn("l2tp: IP is out of client-ip-range, droping connection...\n");
+ goto skip;
+ }
+
+ if (pack->hdr.tid)
+ goto skip;
+
+ if (list_empty(&pack->attrs)) {
+ if (conf_verbose)
+ log_warn("l2tp: to Message-Type attribute present\n");
+ goto skip;
+ }
+
+ msg_type = list_entry(pack->attrs.next, typeof(*msg_type), entry);
+ if (msg_type->attr->id != Message_Type) {
+ if (conf_verbose)
+ log_warn("l2tp: first attribute is not Message-Type\n");
+ goto skip;
+ }
+
+ if (msg_type->val.uint16 == Message_Type_Start_Ctrl_Conn_Request)
+ l2tp_recv_SCCRQ(serv, pack, &pkt_info);
+ else {
+ if (conf_verbose) {
+ log_warn("recv (unexpected) ");
+ l2tp_packet_print(pack, log_ppp_warn);
+ }
+ }
+skip:
+ l2tp_packet_free(pack);
+ }
+
+ return 0;
+}
+
+static void l2tp_udp_close(struct triton_context_t *ctx)
+{
+ struct l2tp_serv_t *serv = container_of(ctx, typeof(*serv), ctx);
+ triton_md_unregister_handler(&serv->hnd);
+ close(serv->hnd.fd);
+ triton_context_unregister(&serv->ctx);
+}
+
+static struct l2tp_serv_t udp_serv =
+{
+ .hnd.read = l2tp_udp_read,
+ .ctx.close = l2tp_udp_close,
+ .ctx.before_switch = log_switch,
+};
+
+/*static struct l2tp_serv_t ip_serv =
+{
+ .hnd.read=l2t_ip_read,
+ .ctx.close=l2tp_ip_close,
+};*/
+
+static void start_udp_server(void)
+{
+ struct sockaddr_in addr;
+ char *opt;
+ int flag = 1;
+
+ udp_serv.hnd.fd = socket(PF_INET, SOCK_DGRAM, 0);
+ if (udp_serv.hnd.fd < 0) {
+ log_emerg("l2tp: socket: %s\n", strerror(errno));
+ return;
+ }
+
+ memset(&addr, 0, sizeof(addr));
+ addr.sin_family = AF_INET;
+ addr.sin_port = htons(L2TP_PORT);
+
+ opt = conf_get_opt("l2tp", "bind");
+ if (opt)
+ addr.sin_addr.s_addr = inet_addr(opt);
+ else
+ addr.sin_addr.s_addr = htonl(INADDR_ANY);
+
+ setsockopt(udp_serv.hnd.fd, SOL_SOCKET, SO_REUSEADDR, &udp_serv.hnd.fd, sizeof(udp_serv.hnd.fd));
+ setsockopt(udp_serv.hnd.fd, SOL_SOCKET, SO_NO_CHECK, &udp_serv.hnd.fd, sizeof(udp_serv.hnd.fd));
+
+ if (bind (udp_serv.hnd.fd, (struct sockaddr *) &addr, sizeof (addr)) < 0) {
+ log_emerg("l2tp: bind: %s\n", strerror(errno));
+ close(udp_serv.hnd.fd);
+ return;
+ }
+
+ if (fcntl(udp_serv.hnd.fd, F_SETFL, O_NONBLOCK)) {
+ log_emerg("l2tp: failed to set nonblocking mode: %s\n", strerror(errno));
+ close(udp_serv.hnd.fd);
+ return;
+ }
+
+ if (setsockopt(udp_serv.hnd.fd, IPPROTO_IP, IP_PKTINFO, &flag, sizeof(flag))) {
+ log_emerg("l2tp: setsockopt(IP_PKTINFO): %s\n", strerror(errno));
+ close(udp_serv.hnd.fd);
+ return;
+ }
+
+ memcpy(&udp_serv.addr, &addr, sizeof(addr));
+
+ triton_context_register(&udp_serv.ctx, NULL);
+ triton_md_register_handler(&udp_serv.ctx, &udp_serv.hnd);
+ triton_md_enable_handler(&udp_serv.hnd, MD_MODE_READ);
+ triton_context_wakeup(&udp_serv.ctx);
+}
+
+static int show_stat_exec(const char *cmd, char * const *fields, int fields_cnt, void *client)
+{
+ cli_send(client, "l2tp:\r\n");
+ cli_sendv(client, " starting: %u\r\n", stat_starting);
+ cli_sendv(client, " active: %u\r\n", stat_active);
+
+ return CLI_CMD_OK;
+}
+
+static void load_config(void)
+{
+ char *opt;
+
+ opt = conf_get_opt("l2tp", "verbose");
+ if (opt && atoi(opt) > 0)
+ conf_verbose = 1;
+
+ opt = conf_get_opt("l2tp", "hello-interval");
+ if (opt && atoi(opt) > 0)
+ conf_hello_interval = atoi(opt);
+
+ opt = conf_get_opt("l2tp", "timeout");
+ if (opt && atoi(opt) > 0)
+ conf_timeout = atoi(opt);
+
+ opt = conf_get_opt("l2tp", "rtimeout");
+ if (opt && atoi(opt) > 0)
+ conf_rtimeout = atoi(opt);
+
+ opt = conf_get_opt("l2tp", "retransmit");
+ if (opt && atoi(opt) > 0)
+ conf_retransmit = atoi(opt);
+
+ if (conf_host_name)
+ _free(conf_host_name);
+ opt = conf_get_opt("l2tp", "host-name");
+ if (opt)
+ conf_host_name = _strdup(opt);
+ else
+ conf_host_name = NULL;
+}
+
+static void __init l2tp_init(void)
+{
+ l2tp_conn = malloc(L2TP_MAX_TID * sizeof(void *));
+ memset(l2tp_conn, 0, L2TP_MAX_TID * sizeof(void *));
+
+ l2tp_conn_pool = mempool_create(sizeof(struct l2tp_conn_t));
+
+ load_config();
+
+ start_udp_server();
+
+ cli_register_simple_cmd2(&show_stat_exec, NULL, 2, "show", "stat");
+
+ triton_event_register_handler(EV_CONFIG_RELOAD, (triton_event_func)load_config);
+}
+
diff --git a/accel-pppd/ctrl/l2tp/l2tp.h b/accel-pppd/ctrl/l2tp/l2tp.h
new file mode 100644
index 00000000..ac9b8e0b
--- /dev/null
+++ b/accel-pppd/ctrl/l2tp/l2tp.h
@@ -0,0 +1,88 @@
+#ifndef __L2TP_H
+#define __L2TP_H
+
+#include <netinet/in.h>
+
+#include "list.h"
+#include "l2tp_prot.h"
+
+#define ATTR_TYPE_NONE 0
+#define ATTR_TYPE_INT16 1
+#define ATTR_TYPE_INT32 2
+#define ATTR_TYPE_INT64 3
+#define ATTR_TYPE_OCTETS 4
+#define ATTR_TYPE_STRING 5
+
+#define L2TP_MAX_PACKET_SIZE 65536
+#define L2TP_MAX_TID 65534
+
+#define L2TP_V2_PROTOCOL_VERSION ( 1 << 8 | 0 )
+
+typedef union
+{
+ uint32_t uint32;
+ int32_t int32;
+ uint16_t uint16;
+ int16_t int16;
+ uint64_t uint64;
+ uint8_t *octets;
+ char *string;
+} l2tp_value_t;
+
+struct l2tp_dict_attr_t
+{
+ struct list_head entry;
+ const char *name;
+ int id;
+ int type;
+ int M;
+ int H;
+ struct list_head values;
+};
+
+struct l2tp_dict_value_t
+{
+ struct list_head entry;
+ const char *name;
+ l2tp_value_t val;
+};
+
+struct l2tp_attr_t
+{
+ struct list_head entry;
+ struct l2tp_dict_attr_t *attr;
+ int M:1;
+ int H:1;
+ int length;
+ l2tp_value_t val;
+};
+
+struct l2tp_packet_t
+{
+ struct list_head entry;
+ struct sockaddr_in addr;
+ struct l2tp_hdr_t hdr;
+ struct list_head attrs;
+};
+
+extern int conf_verbose;
+
+struct l2tp_dict_attr_t *l2tp_dict_find_attr_by_name(const char *name);
+struct l2tp_dict_attr_t *l2tp_dict_find_attr_by_id(int id);
+struct l2tp_dict_value_t *l2tp_dict_find_value(struct l2tp_dict_attr_t *attr, l2tp_value_t val);
+
+int l2tp_recv(int fd, struct l2tp_packet_t **, struct in_pktinfo *);
+void l2tp_packet_free(struct l2tp_packet_t *);
+void l2tp_packet_print(struct l2tp_packet_t *, void (*print)(const char *fmt, ...));
+struct l2tp_packet_t *l2tp_packet_alloc(int ver, int msg_type, struct sockaddr_in *addr);
+int l2tp_packet_send(int sock, struct l2tp_packet_t *);
+int l2tp_packet_add_int16(struct l2tp_packet_t *pack, int id, int16_t val, int M);
+int l2tp_packet_add_int32(struct l2tp_packet_t *pack, int id, int32_t val, int M);
+int l2tp_packet_add_string(struct l2tp_packet_t *pack, int id, const char *val, int M);
+int l2tp_packet_add_octets(struct l2tp_packet_t *pack, int id, const uint8_t *val, int size, int M);
+
+void l2tp_nl_create_tunnel(int fd, int tid, int peer_tid);
+void l2tp_nl_create_session(int tid, int sid, int peer_sid);
+void l2tp_nl_delete_tunnel(int tid);
+
+#endif
diff --git a/accel-pppd/ctrl/l2tp/l2tp_kernel.h b/accel-pppd/ctrl/l2tp/l2tp_kernel.h
new file mode 100644
index 00000000..4bdb31df
--- /dev/null
+++ b/accel-pppd/ctrl/l2tp/l2tp_kernel.h
@@ -0,0 +1,163 @@
+/*
+ * L2TP-over-IP socket for L2TPv3.
+ *
+ * Author: James Chapman <jchapman@katalix.com>
+ */
+
+#ifndef _LINUX_L2TP_H_
+#define _LINUX_L2TP_H_
+
+#include <linux/types.h>
+#ifdef __KERNEL__
+#include <linux/socket.h>
+#include <linux/in.h>
+#else
+#include <netinet/in.h>
+#endif
+
+#define IPPROTO_L2TP 115
+
+/**
+ * struct sockaddr_l2tpip - the sockaddr structure for L2TP-over-IP sockets
+ * @l2tp_family: address family number AF_L2TPIP.
+ * @l2tp_addr: protocol specific address information
+ * @l2tp_conn_id: connection id of tunnel
+ */
+#define __SOCK_SIZE__ 16 /* sizeof(struct sockaddr) */
+struct sockaddr_l2tpip {
+ /* The first fields must match struct sockaddr_in */
+ sa_family_t l2tp_family; /* AF_INET */
+ __be16 l2tp_unused; /* INET port number (unused) */
+ struct in_addr l2tp_addr; /* Internet address */
+
+ __u32 l2tp_conn_id; /* Connection ID of tunnel */
+
+ /* Pad to size of `struct sockaddr'. */
+ unsigned char __pad[sizeof(struct sockaddr) - sizeof(sa_family_t) -
+ sizeof(__be16) - sizeof(struct in_addr) -
+ sizeof(__u32)];
+};
+
+/*****************************************************************************
+ * NETLINK_GENERIC netlink family.
+ *****************************************************************************/
+
+/*
+ * Commands.
+ * Valid TLVs of each command are:-
+ * TUNNEL_CREATE - CONN_ID, pw_type, netns, ifname, ipinfo, udpinfo, udpcsum, vlanid
+ * TUNNEL_DELETE - CONN_ID
+ * TUNNEL_MODIFY - CONN_ID, udpcsum
+ * TUNNEL_GETSTATS - CONN_ID, (stats)
+ * TUNNEL_GET - CONN_ID, (...)
+ * SESSION_CREATE - SESSION_ID, PW_TYPE, offset, data_seq, cookie, peer_cookie, offset, l2spec
+ * SESSION_DELETE - SESSION_ID
+ * SESSION_MODIFY - SESSION_ID, data_seq
+ * SESSION_GET - SESSION_ID, (...)
+ * SESSION_GETSTATS - SESSION_ID, (stats)
+ *
+ */
+enum {
+ L2TP_CMD_NOOP,
+ L2TP_CMD_TUNNEL_CREATE,
+ L2TP_CMD_TUNNEL_DELETE,
+ L2TP_CMD_TUNNEL_MODIFY,
+ L2TP_CMD_TUNNEL_GET,
+ L2TP_CMD_SESSION_CREATE,
+ L2TP_CMD_SESSION_DELETE,
+ L2TP_CMD_SESSION_MODIFY,
+ L2TP_CMD_SESSION_GET,
+ __L2TP_CMD_MAX,
+};
+
+#define L2TP_CMD_MAX (__L2TP_CMD_MAX - 1)
+
+/*
+ * ATTR types defined for L2TP
+ */
+enum {
+ L2TP_ATTR_NONE, /* no data */
+ L2TP_ATTR_PW_TYPE, /* u16, enum l2tp_pwtype */
+ L2TP_ATTR_ENCAP_TYPE, /* u16, enum l2tp_encap_type */
+ L2TP_ATTR_OFFSET, /* u16 */
+ L2TP_ATTR_DATA_SEQ, /* u16 */
+ L2TP_ATTR_L2SPEC_TYPE, /* u8, enum l2tp_l2spec_type */
+ L2TP_ATTR_L2SPEC_LEN, /* u8, enum l2tp_l2spec_type */
+ L2TP_ATTR_PROTO_VERSION, /* u8 */
+ L2TP_ATTR_IFNAME, /* string */
+ L2TP_ATTR_CONN_ID, /* u32 */
+ L2TP_ATTR_PEER_CONN_ID, /* u32 */
+ L2TP_ATTR_SESSION_ID, /* u32 */
+ L2TP_ATTR_PEER_SESSION_ID, /* u32 */
+ L2TP_ATTR_UDP_CSUM, /* u8 */
+ L2TP_ATTR_VLAN_ID, /* u16 */
+ L2TP_ATTR_COOKIE, /* 0, 4 or 8 bytes */
+ L2TP_ATTR_PEER_COOKIE, /* 0, 4 or 8 bytes */
+ L2TP_ATTR_DEBUG, /* u32 */
+ L2TP_ATTR_RECV_SEQ, /* u8 */
+ L2TP_ATTR_SEND_SEQ, /* u8 */
+ L2TP_ATTR_LNS_MODE, /* u8 */
+ L2TP_ATTR_USING_IPSEC, /* u8 */
+ L2TP_ATTR_RECV_TIMEOUT, /* msec */
+ L2TP_ATTR_FD, /* int */
+ L2TP_ATTR_IP_SADDR, /* u32 */
+ L2TP_ATTR_IP_DADDR, /* u32 */
+ L2TP_ATTR_UDP_SPORT, /* u16 */
+ L2TP_ATTR_UDP_DPORT, /* u16 */
+ L2TP_ATTR_MTU, /* u16 */
+ L2TP_ATTR_MRU, /* u16 */
+ L2TP_ATTR_STATS, /* nested */
+ __L2TP_ATTR_MAX,
+};
+
+#define L2TP_ATTR_MAX (__L2TP_ATTR_MAX - 1)
+
+/* Nested in L2TP_ATTR_STATS */
+enum {
+ L2TP_ATTR_STATS_NONE, /* no data */
+ L2TP_ATTR_TX_PACKETS, /* u64 */
+ L2TP_ATTR_TX_BYTES, /* u64 */
+ L2TP_ATTR_TX_ERRORS, /* u64 */
+ L2TP_ATTR_RX_PACKETS, /* u64 */
+ L2TP_ATTR_RX_BYTES, /* u64 */
+ L2TP_ATTR_RX_SEQ_DISCARDS, /* u64 */
+ L2TP_ATTR_RX_OOS_PACKETS, /* u64 */
+ L2TP_ATTR_RX_ERRORS, /* u64 */
+ __L2TP_ATTR_STATS_MAX,
+};
+
+#define L2TP_ATTR_STATS_MAX (__L2TP_ATTR_STATS_MAX - 1)
+
+enum l2tp_pwtype {
+ L2TP_PWTYPE_NONE = 0x0000,
+ L2TP_PWTYPE_ETH_VLAN = 0x0004,
+ L2TP_PWTYPE_ETH = 0x0005,
+ L2TP_PWTYPE_PPP = 0x0007,
+ L2TP_PWTYPE_PPP_AC = 0x0008,
+ L2TP_PWTYPE_IP = 0x000b,
+ __L2TP_PWTYPE_MAX
+};
+
+enum l2tp_l2spec_type {
+ L2TP_L2SPECTYPE_NONE,
+ L2TP_L2SPECTYPE_DEFAULT,
+};
+
+enum l2tp_encap_type {
+ L2TP_ENCAPTYPE_UDP,
+ L2TP_ENCAPTYPE_IP,
+};
+
+enum l2tp_seqmode {
+ L2TP_SEQ_NONE = 0,
+ L2TP_SEQ_IP = 1,
+ L2TP_SEQ_ALL = 2,
+};
+
+/*
+ * NETLINK_GENERIC related info
+ */
+#define L2TP_GENL_NAME "l2tp"
+#define L2TP_GENL_VERSION 0x1
+
+#endif
diff --git a/accel-pppd/ctrl/l2tp/l2tp_prot.h b/accel-pppd/ctrl/l2tp/l2tp_prot.h
new file mode 100644
index 00000000..1c1d9dae
--- /dev/null
+++ b/accel-pppd/ctrl/l2tp/l2tp_prot.h
@@ -0,0 +1,55 @@
+#ifndef __L2TP_PROT_H
+#define __L2TP_PROT_H
+
+#include <stdint.h>
+
+#define L2TP_PORT 1701
+
+struct l2tp_hdr_t
+{
+ uint8_t P:1;
+ uint8_t O:1;
+ uint8_t reserved2:1;
+ uint8_t S:1;
+ uint8_t reserved1:2;
+ uint8_t L:1;
+ uint8_t T:1;
+ uint8_t ver:4;
+ uint8_t reserved3:4;
+ uint16_t length;
+ union {
+ struct {
+ uint16_t tid;
+ uint16_t sid;
+ };
+ uint32_t cid;
+ };
+ uint16_t Ns;
+ uint16_t Nr;
+} __attribute__((packed));
+
+/*#define L2TP_T(hdr) (hdr->flags >> 15)
+#define L2TP_L(hdr) ((hdr->flags >> 14) & 1)
+#define L2TP_S(hdr) ((hdr->flags >> 10) & 1)
+#define L2TP_O(hdr) ((hdr->flags >> 8) & 1)
+#define L2TP_VER(hdr) (hdr->flags & 0xf)*/
+
+struct l2tp_avp_t
+{
+ uint16_t length:10;
+ uint16_t reserved:4;
+ uint16_t H:1;
+ uint16_t M:1;
+ uint16_t vendor;
+ uint16_t type;
+ uint8_t val[0];
+} __attribute__((packed));
+
+struct l2tp_avp_result_code
+{
+ uint16_t result_code;
+ uint16_t error_code;
+} __attribute__((packed));
+
+#endif
+
diff --git a/accel-pppd/ctrl/l2tp/netlink.c b/accel-pppd/ctrl/l2tp/netlink.c
new file mode 100644
index 00000000..7c8c79a6
--- /dev/null
+++ b/accel-pppd/ctrl/l2tp/netlink.c
@@ -0,0 +1,94 @@
+#include <netlink/netlink.h>
+#include <netlink/genl/genl.h>
+#include <netlink/genl/ctrl.h>
+
+#include "l2tp_kernel.h"
+#include "triton.h"
+
+static int family;
+
+void l2tp_nl_delete_tunnel(int tid)
+{
+ struct nl_sock *nl_sock;
+ struct nl_msg *msg;
+
+ nl_sock = nl_socket_alloc();
+ msg = nlmsg_alloc();
+
+ genl_connect(nl_sock);
+
+ genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, family, 0, NLM_F_REQUEST, L2TP_CMD_TUNNEL_DELETE, L2TP_GENL_VERSION);
+ nla_put_u32(msg, L2TP_ATTR_CONN_ID, tid);
+
+ nl_send_auto_complete(nl_sock, msg);
+ nl_recvmsgs_default(nl_sock);
+
+ nlmsg_free(msg);
+ nl_close(nl_sock);
+ nl_socket_free(nl_sock);
+}
+
+void l2tp_nl_create_tunnel(int fd, int tid, int peer_tid)
+{
+ struct nl_sock *nl_sock;
+ struct nl_msg *msg;
+
+ nl_sock = nl_socket_alloc();
+ msg = nlmsg_alloc();
+
+ genl_connect(nl_sock);
+
+ genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, family, 0, NLM_F_REQUEST, L2TP_CMD_TUNNEL_CREATE, L2TP_GENL_VERSION);
+ nla_put_u16(msg, L2TP_ATTR_ENCAP_TYPE, L2TP_ENCAPTYPE_UDP);
+ nla_put_u8(msg, L2TP_ATTR_PROTO_VERSION, 2);
+ nla_put_u32(msg, L2TP_ATTR_CONN_ID, tid);
+ nla_put_u32(msg, L2TP_ATTR_PEER_CONN_ID, peer_tid);
+ nla_put_u32(msg, L2TP_ATTR_FD, fd);
+ //nla_put_u32(msg, L2TP_ATTR_DEBUG, 0xffffffff);
+
+ nl_send_auto_complete(nl_sock, msg);
+ nl_recvmsgs_default(nl_sock);
+
+ nlmsg_free(msg);
+ nl_close(nl_sock);
+ nl_socket_free(nl_sock);
+}
+
+void l2tp_nl_create_session(int tid, int sid, int peer_sid)
+{
+ struct nl_sock *nl_sock;
+ struct nl_msg *msg;
+
+ nl_sock = nl_socket_alloc();
+ msg = nlmsg_alloc();
+
+ genl_connect(nl_sock);
+
+ genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, family, 0, NLM_F_REQUEST, L2TP_CMD_SESSION_CREATE, L2TP_GENL_VERSION);
+ nla_put_u32(msg, L2TP_ATTR_CONN_ID, tid);
+ nla_put_u32(msg, L2TP_ATTR_SESSION_ID, sid);
+ nla_put_u32(msg, L2TP_ATTR_PEER_SESSION_ID, peer_sid);
+ nla_put_u16(msg, L2TP_ATTR_PW_TYPE, L2TP_PWTYPE_PPP);
+ nla_put_u8(msg, L2TP_ATTR_LNS_MODE, 1);
+ //nla_put_u32(msg, L2TP_ATTR_DEBUG, 0xffffffff);
+
+ nl_send_auto_complete(nl_sock, msg);
+ nl_recvmsgs_default(nl_sock);
+
+ nlmsg_free(msg);
+ nl_close(nl_sock);
+ nl_socket_free(nl_sock);
+}
+
+static void __init init(void)
+{
+ struct nl_sock *nl_sock = nl_socket_alloc();
+
+ genl_connect(nl_sock);
+
+ family = genl_ctrl_resolve(nl_sock, L2TP_GENL_NAME);
+
+ nl_close(nl_sock);
+ nl_socket_free(nl_sock);
+}
+
diff --git a/accel-pppd/ctrl/l2tp/packet.c b/accel-pppd/ctrl/l2tp/packet.c
new file mode 100644
index 00000000..e3f68965
--- /dev/null
+++ b/accel-pppd/ctrl/l2tp/packet.c
@@ -0,0 +1,494 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <arpa/inet.h>
+
+#include "triton.h"
+#include "log.h"
+#include "mempool.h"
+#include "memdebug.h"
+
+#include "l2tp.h"
+#include "attr_defs.h"
+
+static mempool_t attr_pool;
+static mempool_t pack_pool;
+static mempool_t buf_pool;
+
+void l2tp_packet_print(struct l2tp_packet_t *pack, void (*print)(const char *fmt, ...))
+{
+ struct l2tp_attr_t *attr;
+ struct l2tp_dict_value_t *val;
+
+ if (pack->hdr.ver == 2) {
+ print("[L2TP tid=%i sid=%i", ntohs(pack->hdr.tid), ntohs(pack->hdr.sid));
+ log_ppp_debug(" Ns=%i Nr=%i", ntohs(pack->hdr.Ns), ntohs(pack->hdr.Nr));
+ } else {
+ print("[L2TP cid=%u", pack->hdr.cid);
+ log_ppp_debug(" Ns=%i Nr=%i", ntohs(pack->hdr.Ns), ntohs(pack->hdr.Nr));
+ }
+
+ list_for_each_entry(attr, &pack->attrs, entry) {
+ print(" <%s", attr->attr->name);
+ val = l2tp_dict_find_value(attr->attr, attr->val);
+ if (val)
+ print(" %s", val->name);
+ else {
+ switch (attr->attr->type) {
+ case ATTR_TYPE_INT16:
+ print(" %i", attr->val.int16);
+ break;
+ case ATTR_TYPE_INT32:
+ print(" %i", attr->val.int32);
+ break;
+ case ATTR_TYPE_STRING:
+ print(" %s", attr->val.string);
+ break;
+ }
+ }
+ print(">");
+ }
+
+ print("]\n");
+}
+
+struct l2tp_packet_t *l2tp_packet_alloc(int ver, int msg_type, struct sockaddr_in *addr)
+{
+ struct l2tp_packet_t *pack = mempool_alloc(pack_pool);
+ if (!pack)
+ return NULL;
+
+ memset(pack, 0, sizeof(*pack));
+ INIT_LIST_HEAD(&pack->attrs);
+ pack->hdr.ver = ver;
+ pack->hdr.T = 1;
+ pack->hdr.L = 1;
+ pack->hdr.S = 1;
+ memcpy(&pack->addr, addr, sizeof(*addr));
+
+ if (msg_type) {
+ if (l2tp_packet_add_int16(pack, Message_Type, msg_type, 1)) {
+ mempool_free(pack);
+ return NULL;
+ }
+ }
+
+ return pack;
+}
+
+void l2tp_packet_free(struct l2tp_packet_t *pack)
+{
+ struct l2tp_attr_t *attr;
+
+ while (!list_empty(&pack->attrs)) {
+ attr = list_entry(pack->attrs.next, typeof(*attr), entry);
+ if (attr->attr->type == ATTR_TYPE_OCTETS || attr->attr->type == ATTR_TYPE_STRING)
+ _free(attr->val.octets);
+ list_del(&attr->entry);
+ mempool_free(attr);
+ }
+
+ mempool_free(pack);
+}
+
+int l2tp_recv(int fd, struct l2tp_packet_t **p, struct in_pktinfo *pkt_info)
+{
+ int n, length;
+ uint8_t *buf;
+ struct l2tp_hdr_t *hdr;
+ struct l2tp_avp_t *avp;
+ struct l2tp_dict_attr_t *da;
+ struct l2tp_attr_t *attr, *RV = NULL;
+ uint8_t *ptr;
+ struct l2tp_packet_t *pack;
+ struct sockaddr_in addr;
+ socklen_t len = sizeof(addr);
+ struct msghdr msg;
+ char msg_control[128];
+ struct cmsghdr *cmsg;
+
+ *p = NULL;
+
+ if (pkt_info) {
+ memset(&msg, 0, sizeof(msg));
+ msg.msg_control = msg_control;
+ msg.msg_controllen = 128;
+
+ n = recvmsg(fd, &msg, MSG_PEEK);
+
+ if (n < 0) {
+ if (errno == EAGAIN)
+ return -1;
+ log_error("l2tp: recvmsg: %s\n", strerror(errno));
+ return 0;
+ }
+
+ for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
+ if (cmsg->cmsg_level == IPPROTO_IP && cmsg->cmsg_type == IP_PKTINFO) {
+ memcpy(pkt_info, CMSG_DATA(cmsg), sizeof(*pkt_info));
+ break;
+ }
+ }
+ }
+
+ buf = mempool_alloc(buf_pool);
+ if (!buf) {
+ log_emerg("l2tp: out of memory\n");
+ return 0;
+ }
+ hdr = (struct l2tp_hdr_t *)buf;
+ ptr = (uint8_t *)(hdr + 1);
+
+ n = recvfrom(fd, buf, L2TP_MAX_PACKET_SIZE, 0, &addr, &len);
+
+ if (n < 0) {
+ mempool_free(buf);
+ if (errno == EAGAIN)
+ return -1;
+ log_error("l2tp: recv: %s\n", strerror(errno));
+ return 0;
+ }
+
+ if (n < sizeof(*hdr)) {
+ if (conf_verbose)
+ log_warn("l2tp: short packet received (%i/%i)\n", n, sizeof(*hdr));
+ goto out_err_hdr;
+ }
+
+ if (n < ntohs(hdr->length)) {
+ if (conf_verbose)
+ log_warn("l2tp: short packet received (%i/%i)\n", n, ntohs(hdr->length));
+ goto out_err_hdr;
+ }
+
+ if (hdr->T == 0)
+ goto out_err_hdr;
+
+ if (hdr->ver == 2) {
+ if (hdr->L == 0) {
+ if (conf_verbose)
+ log_warn("l2tp: incorrect message received (L=0)\n");
+ goto out_err_hdr;
+ }
+
+ if (hdr->S == 0) {
+ if (conf_verbose)
+ log_warn("l2tp: incorrect message received (S=0)\n");
+ goto out_err_hdr;
+ }
+
+ if (hdr->O == 1) {
+ if (conf_verbose)
+ log_warn("l2tp: incorrect message received (O=1)\n");
+ goto out_err_hdr;
+ }
+ } else if (hdr->ver != 3) {
+ if (conf_verbose)
+ log_warn("l2tp: protocol version %i is not supported\n", hdr->ver);
+ goto out_err_hdr;
+ }
+
+ pack = mempool_alloc(pack_pool);
+ if (!pack) {
+ log_emerg("l2tp: out of memory\n");
+ goto out_err_hdr;
+ }
+
+ memset(pack, 0, sizeof(*pack));
+ INIT_LIST_HEAD(&pack->attrs);
+
+ memcpy(&pack->addr, &addr, sizeof(addr));
+ memcpy(&pack->hdr, hdr, sizeof(*hdr));
+ length = ntohs(hdr->length) - sizeof(*hdr);
+
+ while (length) {
+ *(uint16_t *)ptr = ntohs(*(uint16_t *)ptr);
+ avp = (struct l2tp_avp_t *)ptr;
+
+ if (avp->length > length) {
+ if (conf_verbose)
+ log_warn("l2tp: incorrect avp received (exceeds message length)\n");
+ goto out_err;
+ }
+
+ if (avp->vendor)
+ goto skip;
+
+ da = l2tp_dict_find_attr_by_id(ntohs(avp->type));
+ if (!da) {
+ if (conf_verbose)
+ log_warn("l2tp: unknown avp received (type=%i, M=%u)\n", ntohs(avp->type), avp->M);
+ if (avp->M)
+ goto out_err;
+ } else {
+ if (da->M != -1 && da->M != avp->M) {
+ if (conf_verbose)
+ log_warn("l2tp: incorrect avp received (type=%i, M=%i, must be %i)\n", ntohs(avp->type), avp->M, da->M);
+ goto out_err;
+ }
+
+ if (da->H != -1 && da->H != avp->H) {
+ if (conf_verbose)
+ log_warn("l2tp: incorrect avp received (type=%i, H=%i, must be %i)\n", ntohs(avp->type), avp->H, da->H);
+ goto out_err;
+ }
+
+ if (avp->H) {
+ if (!RV) {
+ if (conf_verbose)
+ log_warn("l2tp: incorrect avp received (type=%i, H=1, but Random-Vector is not received)\n", ntohs(avp->type));
+ goto out_err;
+ } else {
+ if (conf_verbose)
+ log_warn("l2tp: hidden avp received (type=%i)\n", ntohs(avp->type));
+ }
+ }
+
+ attr = mempool_alloc(attr_pool);
+ memset(attr, 0, sizeof(*attr));
+ list_add_tail(&attr->entry, &pack->attrs);
+
+ attr->attr = da;
+ attr->M = avp->M;
+ attr->H = avp->H;
+ attr->length = avp->length - sizeof(*avp);
+
+ if (attr->attr->id == Random_Vector)
+ RV = attr;
+
+ switch (da->type) {
+ case ATTR_TYPE_INT16:
+ if (avp->length != sizeof(*avp) + 2)
+ goto out_err_len;
+ attr->val.uint16 = ntohs(*(uint16_t *)avp->val);
+ break;
+ case ATTR_TYPE_INT32:
+ if (avp->length != sizeof(*avp) + 4)
+ goto out_err_len;
+ attr->val.uint32 = ntohl(*(uint32_t *)avp->val);
+ break;
+ case ATTR_TYPE_INT64:
+ if (avp->length != sizeof(*avp) + 8)
+ goto out_err_len;
+ attr->val.uint64 = *(uint64_t *)avp->val;
+ break;
+ case ATTR_TYPE_OCTETS:
+ attr->val.octets = _malloc(attr->length);
+ if (!attr->val.octets)
+ goto out_err_mem;
+ memcpy(attr->val.octets, avp->val, attr->length);
+ break;
+ case ATTR_TYPE_STRING:
+ attr->val.string = _malloc(attr->length + 1);
+ if (!attr->val.string)
+ goto out_err_mem;
+ memcpy(attr->val.string, avp->val, attr->length);
+ attr->val.string[attr->length] = 0;
+ break;
+ }
+ }
+skip:
+ ptr += avp->length;
+ length -= avp->length;
+ }
+
+ *p = pack;
+
+ mempool_free(buf);
+
+ return 0;
+
+out_err:
+ l2tp_packet_free(pack);
+out_err_hdr:
+ mempool_free(buf);
+ return 0;
+out_err_len:
+ if (conf_verbose)
+ log_warn("l2tp: incorrect avp received (type=%i, incorrect length %i)\n", ntohs(avp->type), avp->length);
+ goto out_err;
+out_err_mem:
+ log_emerg("l2tp: out of memory\n");
+ goto out_err;
+}
+
+int l2tp_packet_send(int sock, struct l2tp_packet_t *pack)
+{
+ uint8_t *buf = mempool_alloc(buf_pool);
+ struct l2tp_avp_t *avp;
+ struct l2tp_attr_t *attr;
+ uint8_t *ptr;
+ int n;
+ int len = sizeof(pack->hdr);
+
+ if (!buf) {
+ log_emerg("l2tp: out of memory\n");
+ return -1;
+ }
+
+ memset(buf, 0, L2TP_MAX_PACKET_SIZE);
+
+ ptr = buf + sizeof(pack->hdr);
+
+ list_for_each_entry(attr, &pack->attrs, entry) {
+ if (len + sizeof(*avp) + attr->length >= L2TP_MAX_PACKET_SIZE) {
+ log_error("l2tp: cann't send packet (exceeds maximum size)\n");
+ mempool_free(buf);
+ return -1;
+ }
+ avp = (struct l2tp_avp_t *)ptr;
+ avp->type = htons(attr->attr->id);
+ avp->M = attr->M;
+ avp->H = attr->H;
+ avp->length = sizeof(*avp) + attr->length;
+ *(uint16_t *)ptr = htons(*(uint16_t *)ptr);
+ switch (attr->attr->type) {
+ case ATTR_TYPE_INT16:
+ *(int16_t *)avp->val = htons(attr->val.int16);
+ break;
+ case ATTR_TYPE_INT32:
+ *(int32_t *)avp->val = htonl(attr->val.int32);
+ break;
+ case ATTR_TYPE_STRING:
+ case ATTR_TYPE_OCTETS:
+ memcpy(avp->val, attr->val.string, attr->length);
+ break;
+ }
+
+ ptr += sizeof(*avp) + attr->length;
+ len += sizeof(*avp) + attr->length;
+ }
+
+ pack->hdr.length = htons(len);
+ memcpy(buf, &pack->hdr, sizeof(pack->hdr));
+
+ n = write(sock, buf, ntohs(pack->hdr.length));
+
+ mempool_free(buf);
+
+ if (n < 0) {
+ if (errno == EAGAIN) {
+ if (conf_verbose)
+ log_warn("l2tp: buffer overflow (packet lost)\n");
+ } else {
+ if (conf_verbose)
+ log_warn("l2tp: sendto: %s\n", strerror(errno));
+ return -1;
+ }
+ }
+
+ if (n != ntohs(pack->hdr.length)) {
+ if (conf_verbose)
+ log_warn("l2tp: short write (%i/%i)\n", n, ntohs(pack->hdr.length));
+ }
+
+ return 0;
+}
+
+static struct l2tp_attr_t *attr_alloc(int id, int M)
+{
+ struct l2tp_attr_t *attr;
+ struct l2tp_dict_attr_t *da;
+
+ da = l2tp_dict_find_attr_by_id(id);
+ if (!da)
+ return NULL;
+
+ attr = mempool_alloc(attr_pool);
+ if (!attr) {
+ log_emerg("l2tp: out of memory\n");
+ return NULL;
+ }
+
+ memset(attr, 0, sizeof(*attr));
+
+ attr->attr = da;
+
+ if (da->M != -1)
+ attr->M = da->M;
+ else
+ attr->M = M;
+
+ //if (da->H != -1)
+ //attr->H = da->H;
+
+ return attr;
+}
+
+int l2tp_packet_add_int16(struct l2tp_packet_t *pack, int id, int16_t val, int M)
+{
+ struct l2tp_attr_t *attr = attr_alloc(id, M);
+
+ if (!attr)
+ return -1;
+
+ attr->length = 2;
+ attr->val.int16 = val;
+ list_add_tail(&attr->entry, &pack->attrs);
+
+ return 0;
+}
+int l2tp_packet_add_int32(struct l2tp_packet_t *pack, int id, int32_t val, int M)
+{
+ struct l2tp_attr_t *attr = attr_alloc(id, M);
+
+ if (!attr)
+ return -1;
+
+ attr->length = 4;
+ attr->val.int32 = val;
+ list_add_tail(&attr->entry, &pack->attrs);
+
+ return 0;
+}
+int l2tp_packet_add_string(struct l2tp_packet_t *pack, int id, const char *val, int M)
+{
+ struct l2tp_attr_t *attr = attr_alloc(id, M);
+
+ if (!attr)
+ return -1;
+
+ attr->length = strlen(val);
+ attr->val.string = _strdup(val);
+ if (!attr->val.string) {
+ log_emerg("l2tp: out of memory\n");
+ mempool_free(attr);
+ return -1;
+ }
+ memcpy(attr->val.string, val, attr->length);
+ list_add_tail(&attr->entry, &pack->attrs);
+
+ return 0;
+}
+
+int l2tp_packet_add_octets(struct l2tp_packet_t *pack, int id, const uint8_t *val, int size, int M)
+{
+ struct l2tp_attr_t *attr = attr_alloc(id, M);
+
+ if (!attr)
+ return -1;
+
+ attr->length = size;
+ attr->val.octets = _malloc(size);
+ if (!attr->val.string) {
+ log_emerg("l2tp: out of memory\n");
+ mempool_free(attr);
+ return -1;
+ }
+ memcpy(attr->val.octets, val, attr->length);
+ list_add_tail(&attr->entry, &pack->attrs);
+
+ return 0;
+}
+
+static void __init init(void)
+{
+ attr_pool = mempool_create(sizeof(struct l2tp_attr_t));
+ pack_pool = mempool_create(sizeof(struct l2tp_packet_t));
+ buf_pool = mempool_create(L2TP_MAX_PACKET_SIZE);
+}
+