From e8aa3a1457295f70f8ccc9cd7f2f9073f01a5e2e Mon Sep 17 00:00:00 2001 From: Kozlov Dmitry Date: Fri, 3 Sep 2010 18:58:53 +0400 Subject: project restructured --- accel-pptpd/CMakeLists.txt | 42 ++- accel-pptpd/accel-pptpd.conf | 6 +- accel-pptpd/auth/CMakeLists.txt | 5 + accel-pptpd/auth/auth_chap_md5.c | 294 +++++++++++++++++ accel-pptpd/auth/auth_mschap_v1.c | 358 +++++++++++++++++++++ accel-pptpd/auth/auth_mschap_v2.c | 446 ++++++++++++++++++++++++++ accel-pptpd/auth/auth_pap.c | 214 +++++++++++++ accel-pptpd/auth_chap_md5.c | 294 ----------------- accel-pptpd/auth_mschap_v1.c | 358 --------------------- accel-pptpd/auth_mschap_v2.c | 446 -------------------------- accel-pptpd/auth_pap.c | 214 ------------- accel-pptpd/ctrl/CMakeLists.txt | 4 + accel-pptpd/ctrl/pptp.c | 562 ++++++++++++++++++++++++++++++++ accel-pptpd/ctrl/pptp_prot.h | 297 +++++++++++++++++ accel-pptpd/if_pppox.h | 227 ------------- accel-pptpd/include/if_pppox.h | 227 +++++++++++++ accel-pptpd/include/ipdb.h | 1 + accel-pptpd/include/list.h | 1 + accel-pptpd/include/log.h | 1 + accel-pptpd/include/ppp.h | 1 + accel-pptpd/include/ppp_auth.h | 1 + accel-pptpd/include/ppp_fsm.h | 1 + accel-pptpd/include/ppp_lcp.h | 1 + accel-pptpd/include/pwdb.h | 1 + accel-pptpd/include/triton.h | 1 + accel-pptpd/ipcp_opt_dns.c | 146 --------- accel-pptpd/ipcp_opt_ipaddr.c | 147 --------- accel-pptpd/ipdb.c | 3 +- accel-pptpd/lcp_opt_accomp.c | 92 ------ accel-pptpd/lcp_opt_magic.c | 83 ----- accel-pptpd/lcp_opt_mru.c | 123 ------- accel-pptpd/lcp_opt_pcomp.c | 92 ------ accel-pptpd/list.h | 252 --------------- accel-pptpd/log.c | 14 +- accel-pptpd/main.c | 2 +- accel-pptpd/ppp.c | 492 ---------------------------- accel-pptpd/ppp.h | 138 -------- accel-pptpd/ppp/CMakeLists.txt | 19 ++ accel-pptpd/ppp/ipcp_opt_dns.c | 146 +++++++++ accel-pptpd/ppp/ipcp_opt_ipaddr.c | 147 +++++++++ accel-pptpd/ppp/lcp_opt_accomp.c | 92 ++++++ accel-pptpd/ppp/lcp_opt_magic.c | 83 +++++ accel-pptpd/ppp/lcp_opt_mru.c | 123 +++++++ accel-pptpd/ppp/lcp_opt_pcomp.c | 92 ++++++ accel-pptpd/ppp/ppp.c | 492 ++++++++++++++++++++++++++++ accel-pptpd/ppp/ppp.h | 138 ++++++++ accel-pptpd/ppp/ppp_auth.c | 320 ++++++++++++++++++ accel-pptpd/ppp/ppp_auth.h | 34 ++ accel-pptpd/ppp/ppp_ccp.c | 577 +++++++++++++++++++++++++++++++++ accel-pptpd/ppp/ppp_ccp.h | 94 ++++++ accel-pptpd/ppp/ppp_fsm.c | 534 ++++++++++++++++++++++++++++++ accel-pptpd/ppp/ppp_fsm.h | 65 ++++ accel-pptpd/ppp/ppp_ipcp.c | 568 ++++++++++++++++++++++++++++++++ accel-pptpd/ppp/ppp_ipcp.h | 94 ++++++ accel-pptpd/ppp/ppp_lcp.c | 659 ++++++++++++++++++++++++++++++++++++++ accel-pptpd/ppp/ppp_lcp.h | 134 ++++++++ accel-pptpd/ppp_auth.c | 320 ------------------ accel-pptpd/ppp_auth.h | 34 -- accel-pptpd/ppp_ccp.c | 577 --------------------------------- accel-pptpd/ppp_ccp.h | 94 ------ accel-pptpd/ppp_fsm.c | 534 ------------------------------ accel-pptpd/ppp_fsm.h | 65 ---- accel-pptpd/ppp_ipcp.c | 568 -------------------------------- accel-pptpd/ppp_ipcp.h | 94 ------ accel-pptpd/ppp_lcp.c | 658 ------------------------------------- accel-pptpd/ppp_lcp.h | 134 -------- accel-pptpd/pptp.c | 563 -------------------------------- accel-pptpd/pptp_prot.h | 297 ----------------- accel-pptpd/pptpd.h | 26 -- accel-pptpd/pwdb.c | 4 +- accel-pptpd/triton/loader.c | 40 ++- accel-pptpd/triton/triton.c | 7 +- accel-pptpd/triton/triton.h | 2 +- accel-pptpd/triton/triton_p.h | 1 + 74 files changed, 6907 insertions(+), 7109 deletions(-) create mode 100644 accel-pptpd/auth/CMakeLists.txt create mode 100644 accel-pptpd/auth/auth_chap_md5.c create mode 100644 accel-pptpd/auth/auth_mschap_v1.c create mode 100644 accel-pptpd/auth/auth_mschap_v2.c create mode 100644 accel-pptpd/auth/auth_pap.c delete mode 100644 accel-pptpd/auth_chap_md5.c delete mode 100644 accel-pptpd/auth_mschap_v1.c delete mode 100644 accel-pptpd/auth_mschap_v2.c delete mode 100644 accel-pptpd/auth_pap.c create mode 100644 accel-pptpd/ctrl/CMakeLists.txt create mode 100644 accel-pptpd/ctrl/pptp.c create mode 100644 accel-pptpd/ctrl/pptp_prot.h delete mode 100644 accel-pptpd/if_pppox.h create mode 100644 accel-pptpd/include/if_pppox.h create mode 120000 accel-pptpd/include/ipdb.h create mode 120000 accel-pptpd/include/list.h create mode 120000 accel-pptpd/include/log.h create mode 120000 accel-pptpd/include/ppp.h create mode 120000 accel-pptpd/include/ppp_auth.h create mode 120000 accel-pptpd/include/ppp_fsm.h create mode 120000 accel-pptpd/include/ppp_lcp.h create mode 120000 accel-pptpd/include/pwdb.h create mode 120000 accel-pptpd/include/triton.h delete mode 100644 accel-pptpd/ipcp_opt_dns.c delete mode 100644 accel-pptpd/ipcp_opt_ipaddr.c delete mode 100644 accel-pptpd/lcp_opt_accomp.c delete mode 100644 accel-pptpd/lcp_opt_magic.c delete mode 100644 accel-pptpd/lcp_opt_mru.c delete mode 100644 accel-pptpd/lcp_opt_pcomp.c delete mode 100644 accel-pptpd/list.h delete mode 100644 accel-pptpd/ppp.c delete mode 100644 accel-pptpd/ppp.h create mode 100644 accel-pptpd/ppp/CMakeLists.txt create mode 100644 accel-pptpd/ppp/ipcp_opt_dns.c create mode 100644 accel-pptpd/ppp/ipcp_opt_ipaddr.c create mode 100644 accel-pptpd/ppp/lcp_opt_accomp.c create mode 100644 accel-pptpd/ppp/lcp_opt_magic.c create mode 100644 accel-pptpd/ppp/lcp_opt_mru.c create mode 100644 accel-pptpd/ppp/lcp_opt_pcomp.c create mode 100644 accel-pptpd/ppp/ppp.c create mode 100644 accel-pptpd/ppp/ppp.h create mode 100644 accel-pptpd/ppp/ppp_auth.c create mode 100644 accel-pptpd/ppp/ppp_auth.h create mode 100644 accel-pptpd/ppp/ppp_ccp.c create mode 100644 accel-pptpd/ppp/ppp_ccp.h create mode 100644 accel-pptpd/ppp/ppp_fsm.c create mode 100644 accel-pptpd/ppp/ppp_fsm.h create mode 100644 accel-pptpd/ppp/ppp_ipcp.c create mode 100644 accel-pptpd/ppp/ppp_ipcp.h create mode 100644 accel-pptpd/ppp/ppp_lcp.c create mode 100644 accel-pptpd/ppp/ppp_lcp.h delete mode 100644 accel-pptpd/ppp_auth.c delete mode 100644 accel-pptpd/ppp_auth.h delete mode 100644 accel-pptpd/ppp_ccp.c delete mode 100644 accel-pptpd/ppp_ccp.h delete mode 100644 accel-pptpd/ppp_fsm.c delete mode 100644 accel-pptpd/ppp_fsm.h delete mode 100644 accel-pptpd/ppp_ipcp.c delete mode 100644 accel-pptpd/ppp_ipcp.h delete mode 100644 accel-pptpd/ppp_lcp.c delete mode 100644 accel-pptpd/ppp_lcp.h delete mode 100644 accel-pptpd/pptp.c delete mode 100644 accel-pptpd/pptp_prot.h delete mode 100644 accel-pptpd/pptpd.h (limited to 'accel-pptpd') diff --git a/accel-pptpd/CMakeLists.txt b/accel-pptpd/CMakeLists.txt index fc6ac0e..64f351f 100644 --- a/accel-pptpd/CMakeLists.txt +++ b/accel-pptpd/CMakeLists.txt @@ -3,35 +3,31 @@ cmake_minimum_required(VERSION 2.6) SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -fvisibility=hidden -D_GNU_SOURCE") +INCLUDE_DIRECTORIES(include) + ADD_SUBDIRECTORY(triton) +ADD_SUBDIRECTORY(ctrl) +ADD_SUBDIRECTORY(auth) ADD_EXECUTABLE(pptpd - pptp.c + ppp/ppp.c + ppp/ppp_fsm.c + ppp/ppp_lcp.c + ppp/lcp_opt_mru.c + ppp/lcp_opt_magic.c + ppp/lcp_opt_pcomp.c + ppp/lcp_opt_accomp.c + ppp/ppp_auth.c + ppp/ppp_ipcp.c + ppp/ipcp_opt_ipaddr.c + ppp/ipcp_opt_dns.c + ppp/ppp_ccp.c + log.c - ppp.c - ppp_fsm.c - - ppp_lcp.c - lcp_opt_mru.c - lcp_opt_magic.c - lcp_opt_pcomp.c - lcp_opt_accomp.c - - ppp_auth.c - auth_pap.c - auth_chap_md5.c - auth_mschap_v1.c - auth_mschap_v2.c - - ppp_ipcp.c - ipcp_opt_ipaddr.c - ipcp_opt_dns.c - - ppp_ccp.c - pwdb.c ipdb.c main.c ) -TARGET_LINK_LIBRARIES(pptpd pthread triton ssl rt) +TARGET_LINK_LIBRARIES(pptpd triton rt pthread ssl) + diff --git a/accel-pptpd/accel-pptpd.conf b/accel-pptpd/accel-pptpd.conf index 736f71a..fa2f128 100644 --- a/accel-pptpd/accel-pptpd.conf +++ b/accel-pptpd/accel-pptpd.conf @@ -1,8 +1,6 @@ [modules] -libpap.so -libmschap-v1.so -libmschap-v2.so - +./libpptp.so +./libauth_mschap_v2.so [core] log-error=/dev/stderr diff --git a/accel-pptpd/auth/CMakeLists.txt b/accel-pptpd/auth/CMakeLists.txt new file mode 100644 index 0000000..f0e6465 --- /dev/null +++ b/accel-pptpd/auth/CMakeLists.txt @@ -0,0 +1,5 @@ +ADD_LIBRARY(auth_pap SHARED auth_pap.c) +ADD_LIBRARY(auth_chap_md5 SHARED auth_chap_md5.c) +ADD_LIBRARY(auth_mschap_v1 SHARED auth_mschap_v1.c) +ADD_LIBRARY(auth_mschap_v2 SHARED auth_mschap_v2.c) + diff --git a/accel-pptpd/auth/auth_chap_md5.c b/accel-pptpd/auth/auth_chap_md5.c new file mode 100644 index 0000000..5577794 --- /dev/null +++ b/accel-pptpd/auth/auth_chap_md5.c @@ -0,0 +1,294 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "log.h" +#include "ppp.h" +#include "ppp_auth.h" +#include "ppp_lcp.h" +#include "pwdb.h" + +#define CHAP_CHALLENGE 1 +#define CHAP_RESPONSE 2 +#define CHAP_SUCCESS 3 +#define CHAP_FAILURE 4 + +#define VALUE_SIZE 16 + +#define MSG_FAILURE "Authentication failed" +#define MSG_SUCCESS "Authentication successed" + +#define HDR_LEN (sizeof(struct chap_hdr_t)-2) + +static int urandom_fd; + +struct chap_hdr_t +{ + uint16_t proto; + uint8_t code; + uint8_t id; + uint16_t len; +} __attribute__((packed)); + +struct chap_challenge_t +{ + struct chap_hdr_t hdr; + uint8_t val_size; + uint8_t val[VALUE_SIZE]; + char name[0]; +} __attribute__((packed)); + +struct chap_failure_t +{ + struct chap_hdr_t hdr; + char message[sizeof(MSG_FAILURE)]; +} __attribute__((packed)); + +struct chap_success_t +{ + struct chap_hdr_t hdr; + char message[sizeof(MSG_SUCCESS)]; +} __attribute__((packed)); + + +struct chap_auth_data_t +{ + struct auth_data_t auth; + struct ppp_handler_t h; + struct ppp_t *ppp; + int id; + uint8_t val[VALUE_SIZE]; +}; + +static void chap_send_challenge(struct chap_auth_data_t *ad); +static void chap_recv(struct ppp_handler_t *h); + +static void print_buf(const uint8_t *buf,int size) +{ + int i; + for(i=0;iauth.proto=PPP_CHAP; + d->ppp=ppp; + + return &d->auth; +} + +static void auth_data_free(struct ppp_t *ppp,struct auth_data_t *auth) +{ + struct chap_auth_data_t *d=container_of(auth,typeof(*d),auth); + + free(d); +} + +static int chap_start(struct ppp_t *ppp, struct auth_data_t *auth) +{ + struct chap_auth_data_t *d=container_of(auth,typeof(*d),auth); + + d->h.proto=PPP_CHAP; + d->h.recv=chap_recv; + + ppp_register_chan_handler(ppp,&d->h); + + chap_send_challenge(d); + + return 0; +} + +static int chap_finish(struct ppp_t *ppp, struct auth_data_t *auth) +{ + struct chap_auth_data_t *d=container_of(auth,typeof(*d),auth); + + ppp_unregister_handler(ppp,&d->h); + + return 0; +} + +static int lcp_send_conf_req(struct ppp_t *ppp, struct auth_data_t *d, uint8_t *ptr) +{ + *ptr=5; + return 1; +} + +static int lcp_recv_conf_req(struct ppp_t *ppp, struct auth_data_t *d, uint8_t *ptr) +{ + if (*ptr==5) + return LCP_OPT_ACK; + return LCP_OPT_NAK; +} + +static void chap_send_failure(struct chap_auth_data_t *ad) +{ + struct chap_failure_t msg= + { + .hdr.proto=htons(PPP_CHAP), + .hdr.code=CHAP_FAILURE, + .hdr.id=ad->id, + .hdr.len=htons(sizeof(msg)-1-2), + .message=MSG_FAILURE, + }; + + log_debug("send [CHAP Failure id=%x \"%s\"]\n",msg.hdr.id,MSG_FAILURE); + + ppp_chan_send(ad->ppp,&msg,ntohs(msg.hdr.len)+2); +} + +static void chap_send_success(struct chap_auth_data_t *ad) +{ + struct chap_success_t msg= + { + .hdr.proto=htons(PPP_CHAP), + .hdr.code=CHAP_SUCCESS, + .hdr.id=ad->id, + .hdr.len=htons(sizeof(msg)-1-2), + .message=MSG_SUCCESS, + }; + + log_debug("send [CHAP Success id=%x \"%s\"]\n",msg.hdr.id,MSG_SUCCESS); + + ppp_chan_send(ad->ppp,&msg,ntohs(msg.hdr.len)+2); +} + +static void chap_send_challenge(struct chap_auth_data_t *ad) +{ + struct chap_challenge_t msg= + { + .hdr.proto=htons(PPP_CHAP), + .hdr.code=CHAP_CHALLENGE, + .hdr.id=++ad->id, + .hdr.len=htons(sizeof(msg)-2), + .val_size=VALUE_SIZE, + }; + + read(urandom_fd,ad->val,VALUE_SIZE); + memcpy(msg.val,ad->val,VALUE_SIZE); + + log_debug("send [CHAP Challenge id=%x <",msg.hdr.id); + print_buf(msg.val,VALUE_SIZE); + log_debug(">]\n"); + + ppp_chan_send(ad->ppp,&msg,ntohs(msg.hdr.len)+2); +} + +static void chap_recv_response(struct chap_auth_data_t *ad, struct chap_hdr_t *hdr) +{ + MD5_CTX md5_ctx; + uint8_t md5[MD5_DIGEST_LENGTH]; + char *passwd; + char *name; + struct chap_challenge_t *msg=(struct chap_challenge_t*)hdr; + + log_debug("recv [CHAP Response id=%x <", msg->hdr.id); + print_buf(msg->val,msg->val_size); + log_debug(">, name=\""); + print_str(msg->name,ntohs(msg->hdr.len)-sizeof(*msg)+2); + log_debug("\"]\n"); + + if (msg->hdr.id!=ad->id) + { + log_error("chap-md5: id mismatch\n"); + chap_send_failure(ad); + ppp_terminate(ad->ppp, 0); + } + + if (msg->val_size!=VALUE_SIZE) + { + log_error("chap-md5: value-size should be %i, expected %i\n",VALUE_SIZE,msg->val_size); + chap_send_failure(ad); + ppp_terminate(ad->ppp, 0); + } + + name=strndup(msg->name,ntohs(msg->hdr.len)-sizeof(*msg)+2); + passwd=pwdb_get_passwd(ad->ppp,name); + if (!passwd) + { + free(name); + log_debug("chap-md5: user not found\n"); + chap_send_failure(ad); + return; + } + + MD5_Init(&md5_ctx); + MD5_Update(&md5_ctx,&msg->hdr.id,1); + MD5_Update(&md5_ctx,passwd,strlen(passwd)); + MD5_Update(&md5_ctx,ad->val,VALUE_SIZE); + MD5_Final(md5,&md5_ctx); + + if (memcmp(md5,msg->val,sizeof(md5))) + { + log_debug("chap-md5: challenge response mismatch\n"); + chap_send_failure(ad); + auth_failed(ad->ppp); + }else + { + chap_send_success(ad); + auth_successed(ad->ppp); + } + + free(name); + free(passwd); +} + +static struct ppp_auth_handler_t chap= +{ + .name="CHAP-md5", + .init=auth_data_init, + .free=auth_data_free, + .send_conf_req=lcp_send_conf_req, + .recv_conf_req=lcp_recv_conf_req, + .start=chap_start, + .finish=chap_finish, +}; + +static void chap_recv(struct ppp_handler_t *h) +{ + struct chap_auth_data_t *d=container_of(h,typeof(*d),h); + struct chap_hdr_t *hdr=(struct chap_hdr_t *)d->ppp->chan_buf; + + if (d->ppp->chan_buf_sizelen)len)ppp->chan_buf_size-2) + { + log_warn("CHAP: short packet received\n"); + return; + } + + if (hdr->code==CHAP_RESPONSE) chap_recv_response(d,hdr); + else + { + log_warn("CHAP: unknown code received %x\n",hdr->code); + } +} + +static void __init auth_chap_md5_init() +{ + urandom_fd=open("/dev/urandom",O_RDONLY); + if (urandom_fd<0) + { + log_error("chap-md5: failed to open /dev/urandom: %s\n",strerror(errno)); + return; + } + if (ppp_auth_register_handler(&chap)) + log_error("chap-md5: failed to register handler\n"); +} + diff --git a/accel-pptpd/auth/auth_mschap_v1.c b/accel-pptpd/auth/auth_mschap_v1.c new file mode 100644 index 0000000..1cf5eb8 --- /dev/null +++ b/accel-pptpd/auth/auth_mschap_v1.c @@ -0,0 +1,358 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "log.h" +#include "ppp.h" +#include "ppp_auth.h" +#include "ppp_lcp.h" +#include "pwdb.h" + +#define CHAP_CHALLENGE 1 +#define CHAP_RESPONSE 2 +#define CHAP_SUCCESS 3 +#define CHAP_FAILURE 4 + +#define VALUE_SIZE 8 +#define RESPONSE_VALUE_SIZE (24+24+1) + +#define MSG_FAILURE "E=691 R=0" +#define MSG_SUCCESS "Authentication successed" + +#define HDR_LEN (sizeof(struct chap_hdr_t)-2) + +static int urandom_fd; + +struct chap_hdr_t +{ + uint16_t proto; + uint8_t code; + uint8_t id; + uint16_t len; +} __attribute__((packed)); + +struct chap_challenge_t +{ + struct chap_hdr_t hdr; + uint8_t val_size; + uint8_t val[VALUE_SIZE]; + char name[0]; +} __attribute__((packed)); + +struct chap_response_t +{ + struct chap_hdr_t hdr; + uint8_t val_size; + uint8_t lm_hash[24]; + uint8_t nt_hash[24]; + uint8_t use_nt_hash; + char name[0]; +} __attribute__((packed)); + +struct chap_failure_t +{ + struct chap_hdr_t hdr; + char message[sizeof(MSG_FAILURE)]; +} __attribute__((packed)); + +struct chap_success_t +{ + struct chap_hdr_t hdr; + char message[sizeof(MSG_SUCCESS)]; +} __attribute__((packed)); + + +struct chap_auth_data_t +{ + struct auth_data_t auth; + struct ppp_handler_t h; + struct ppp_t *ppp; + int id; + uint8_t val[VALUE_SIZE]; +}; + +static void chap_send_challenge(struct chap_auth_data_t *ad); +static void chap_recv(struct ppp_handler_t *h); +static int chap_check_response(struct chap_auth_data_t *ad, struct chap_response_t *res); + +static void print_buf(const uint8_t *buf,int size) +{ + int i; + for(i=0;iauth.proto=PPP_CHAP; + d->ppp=ppp; + + return &d->auth; +} + +static void auth_data_free(struct ppp_t *ppp,struct auth_data_t *auth) +{ + struct chap_auth_data_t *d=container_of(auth,typeof(*d),auth); + + free(d); +} + +static int chap_start(struct ppp_t *ppp, struct auth_data_t *auth) +{ + struct chap_auth_data_t *d=container_of(auth,typeof(*d),auth); + + d->h.proto=PPP_CHAP; + d->h.recv=chap_recv; + + ppp_register_chan_handler(ppp,&d->h); + + chap_send_challenge(d); + + return 0; +} + +static int chap_finish(struct ppp_t *ppp, struct auth_data_t *auth) +{ + struct chap_auth_data_t *d=container_of(auth,typeof(*d),auth); + + ppp_unregister_handler(ppp,&d->h); + + return 0; +} + +static int lcp_send_conf_req(struct ppp_t *ppp, struct auth_data_t *d, uint8_t *ptr) +{ + *ptr=0x80; + return 1; +} + +static int lcp_recv_conf_req(struct ppp_t *ppp, struct auth_data_t *d, uint8_t *ptr) +{ + if (*ptr==0x80) + return LCP_OPT_ACK; + return LCP_OPT_NAK; +} + +static void chap_send_failure(struct chap_auth_data_t *ad) +{ + struct chap_failure_t msg= + { + .hdr.proto=htons(PPP_CHAP), + .hdr.code=CHAP_FAILURE, + .hdr.id=ad->id, + .hdr.len=htons(sizeof(msg)-1-2), + .message=MSG_FAILURE, + }; + + log_debug("send [MSCHAP-v1 Failure id=%x \"%s\"]\n",msg.hdr.id,MSG_FAILURE); + + ppp_chan_send(ad->ppp,&msg,ntohs(msg.hdr.len)+2); +} + +static void chap_send_success(struct chap_auth_data_t *ad) +{ + struct chap_success_t msg= + { + .hdr.proto=htons(PPP_CHAP), + .hdr.code=CHAP_SUCCESS, + .hdr.id=ad->id, + .hdr.len=htons(sizeof(msg)-1-2), + .message=MSG_SUCCESS, + }; + + log_debug("send [MSCHAP-v1 Success id=%x \"%s\"]\n",msg.hdr.id,MSG_SUCCESS); + + ppp_chan_send(ad->ppp,&msg,ntohs(msg.hdr.len)+2); +} + +static void chap_send_challenge(struct chap_auth_data_t *ad) +{ + struct chap_challenge_t msg= + { + .hdr.proto=htons(PPP_CHAP), + .hdr.code=CHAP_CHALLENGE, + .hdr.id=++ad->id, + .hdr.len=htons(sizeof(msg)-2), + .val_size=VALUE_SIZE, + }; + + read(urandom_fd,ad->val,VALUE_SIZE); + memcpy(msg.val,ad->val,VALUE_SIZE); + + log_debug("send [MSCHAP-v1 Challenge id=%x <",msg.hdr.id); + print_buf(msg.val,VALUE_SIZE); + log_debug(">]\n"); + + ppp_chan_send(ad->ppp,&msg,ntohs(msg.hdr.len)+2); +} + +static void chap_recv_response(struct chap_auth_data_t *ad, struct chap_hdr_t *hdr) +{ + struct chap_response_t *msg=(struct chap_response_t*)hdr; + + log_debug("recv [MSCHAP-v1 Response id=%x <", msg->hdr.id); + print_buf(msg->lm_hash,24); + log_debug(">, <"); + print_buf(msg->nt_hash,24); + log_debug(">, F=%i, name=\"",msg->use_nt_hash); + print_str(msg->name,ntohs(msg->hdr.len)-sizeof(*msg)+2); + log_debug("\"]\n"); + + if (msg->hdr.id!=ad->id) + { + log_error("mschap-v1: id mismatch\n"); + chap_send_failure(ad); + ppp_terminate(ad->ppp, 0); + } + + if (msg->val_size!=RESPONSE_VALUE_SIZE) + { + log_error("mschap-v1: value-size should be %i, expected %i\n",RESPONSE_VALUE_SIZE,msg->val_size); + chap_send_failure(ad); + ppp_terminate(ad->ppp, 0); + } + + if (chap_check_response(ad,msg)) + { + chap_send_failure(ad); + auth_failed(ad->ppp); + }else + { + chap_send_success(ad); + auth_successed(ad->ppp); + } +} + +static void des_encrypt(const uint8_t *input, const uint8_t *key, uint8_t *output) +{ + int i,j,parity; + union + { + uint64_t u64; + uint8_t buf[8]; + } p_key; + DES_cblock cb; + DES_cblock res; + DES_key_schedule ks; + + memcpy(p_key.buf,key,7); + p_key.u64=bswap_64(p_key.u64); + + for(i=0;i<8;i++) + { + cb[i]=(((p_key.u64<<(7*i))>>56)&0xfe); + for(j=0, parity=0; j<7; j++) + if ((cb[i]>>(j+1))&1) parity++; + cb[i]|=(~parity)&1; + } + + DES_set_key_checked(&cb, &ks); + memcpy(cb,input,8); + DES_ecb_encrypt(&cb,&res,&ks,DES_ENCRYPT); + memcpy(output,res,8); +} + +static int chap_check_response(struct chap_auth_data_t *ad, struct chap_response_t *msg) +{ + MD4_CTX md4_ctx; + uint8_t z_hash[21]; + uint8_t nt_hash[24]; + char *passwd; + char *u_passwd; + char *name; + int i; + + name=strndup(msg->name,ntohs(msg->hdr.len)-sizeof(*msg)+2); + passwd=pwdb_get_passwd(ad->ppp,name); + if (!passwd) + { + free(name); + log_debug("mschap-v1: user not found\n"); + chap_send_failure(ad); + return -1; + } + + u_passwd=malloc(strlen(passwd)*2); + for(i=0; ival,z_hash,nt_hash); + des_encrypt(ad->val,z_hash+7,nt_hash+8); + des_encrypt(ad->val,z_hash+14,nt_hash+16); + + free(name); + free(passwd); + free(u_passwd); + + return memcmp(nt_hash,msg->nt_hash,24); +} + +static struct ppp_auth_handler_t chap= +{ + .name="MSCHAP-v1", + .init=auth_data_init, + .free=auth_data_free, + .send_conf_req=lcp_send_conf_req, + .recv_conf_req=lcp_recv_conf_req, + .start=chap_start, + .finish=chap_finish, +}; + +static void chap_recv(struct ppp_handler_t *h) +{ + struct chap_auth_data_t *d=container_of(h,typeof(*d),h); + struct chap_hdr_t *hdr=(struct chap_hdr_t *)d->ppp->chan_buf; + + if (d->ppp->chan_buf_sizelen)len)ppp->chan_buf_size-2) + { + log_warn("mschap-v1: short packet received\n"); + return; + } + + if (hdr->code==CHAP_RESPONSE) chap_recv_response(d,hdr); + else + { + log_warn("mschap-v1: unknown code received %x\n",hdr->code); + } +} + +static void __init auth_mschap_v1_init() +{ + urandom_fd=open("/dev/urandom",O_RDONLY); + if (urandom_fd<0) + { + log_error("mschap-v1: failed to open /dev/urandom: %s\n",strerror(errno)); + return; + } + if (ppp_auth_register_handler(&chap)) + log_error("mschap-v1: failed to register handler\n"); +} + diff --git a/accel-pptpd/auth/auth_mschap_v2.c b/accel-pptpd/auth/auth_mschap_v2.c new file mode 100644 index 0000000..502f368 --- /dev/null +++ b/accel-pptpd/auth/auth_mschap_v2.c @@ -0,0 +1,446 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include "log.h" +#include "ppp.h" +#include "ppp_auth.h" +#include "ppp_lcp.h" +#include "pwdb.h" + +#define CHAP_CHALLENGE 1 +#define CHAP_RESPONSE 2 +#define CHAP_SUCCESS 3 +#define CHAP_FAILURE 4 + +#define VALUE_SIZE 16 +#define RESPONSE_VALUE_SIZE (16+8+24+1) + +#define MSG_FAILURE "E=691 R=0 C=cccccccccccccccccccccccccccccccc V=3 M=Authentication failure" +#define MSG_SUCCESS "S=cccccccccccccccccccccccccccccccccccccccc M=Authentication successed" + +#define HDR_LEN (sizeof(struct chap_hdr_t)-2) + +static int urandom_fd; +static uint8_t magic1[39] = + {0x4D, 0x61, 0x67, 0x69, 0x63, 0x20, 0x73, 0x65, 0x72, 0x76, + 0x65, 0x72, 0x20, 0x74, 0x6F, 0x20, 0x63, 0x6C, 0x69, 0x65, + 0x6E, 0x74, 0x20, 0x73, 0x69, 0x67, 0x6E, 0x69, 0x6E, 0x67, + 0x20, 0x63, 0x6F, 0x6E, 0x73, 0x74, 0x61, 0x6E, 0x74}; +static uint8_t magic2[41] = + {0x50, 0x61, 0x64, 0x20, 0x74, 0x6F, 0x20, 0x6D, 0x61, 0x6B, + 0x65, 0x20, 0x69, 0x74, 0x20, 0x64, 0x6F, 0x20, 0x6D, 0x6F, + 0x72, 0x65, 0x20, 0x74, 0x68, 0x61, 0x6E, 0x20, 0x6F, 0x6E, + 0x65, 0x20, 0x69, 0x74, 0x65, 0x72, 0x61, 0x74, 0x69, 0x6F, + 0x6E}; + +struct chap_hdr_t +{ + uint16_t proto; + uint8_t code; + uint8_t id; + uint16_t len; +} __attribute__((packed)); + +struct chap_challenge_t +{ + struct chap_hdr_t hdr; + uint8_t val_size; + uint8_t val[VALUE_SIZE]; + char name[0]; +} __attribute__((packed)); + +struct chap_response_t +{ + struct chap_hdr_t hdr; + uint8_t val_size; + uint8_t peer_challenge[16]; + uint8_t reserved[8]; + uint8_t nt_hash[24]; + uint8_t flags; + char name[0]; +} __attribute__((packed)); + +struct chap_failure_t +{ + struct chap_hdr_t hdr; + char message[sizeof(MSG_FAILURE)]; +} __attribute__((packed)); + +struct chap_success_t +{ + struct chap_hdr_t hdr; + char message[sizeof(MSG_SUCCESS)]; +} __attribute__((packed)); + + +struct chap_auth_data_t +{ + struct auth_data_t auth; + struct ppp_handler_t h; + struct ppp_t *ppp; + int id; + uint8_t val[VALUE_SIZE]; +}; + +static void chap_send_challenge(struct chap_auth_data_t *ad); +static void chap_recv(struct ppp_handler_t *h); +static int chap_check_response(struct chap_auth_data_t *ad, struct chap_response_t *res); + +static void print_buf(const uint8_t *buf,int size) +{ + int i; + for(i=0;iauth.proto=PPP_CHAP; + d->ppp=ppp; + + return &d->auth; +} + +static void auth_data_free(struct ppp_t *ppp,struct auth_data_t *auth) +{ + struct chap_auth_data_t *d=container_of(auth,typeof(*d),auth); + + free(d); +} + +static int chap_start(struct ppp_t *ppp, struct auth_data_t *auth) +{ + struct chap_auth_data_t *d=container_of(auth,typeof(*d),auth); + + d->h.proto=PPP_CHAP; + d->h.recv=chap_recv; + + ppp_register_chan_handler(ppp,&d->h); + + chap_send_challenge(d); + + return 0; +} + +static int chap_finish(struct ppp_t *ppp, struct auth_data_t *auth) +{ + struct chap_auth_data_t *d=container_of(auth,typeof(*d),auth); + + ppp_unregister_handler(ppp,&d->h); + + return 0; +} + +static int lcp_send_conf_req(struct ppp_t *ppp, struct auth_data_t *d, uint8_t *ptr) +{ + *ptr=0x81; + return 1; +} + +static int lcp_recv_conf_req(struct ppp_t *ppp, struct auth_data_t *d, uint8_t *ptr) +{ + if (*ptr==0x81) + return LCP_OPT_ACK; + return LCP_OPT_NAK; +} + +static void chap_send_failure(struct chap_auth_data_t *ad) +{ + struct chap_failure_t msg= + { + .hdr.proto=htons(PPP_CHAP), + .hdr.code=CHAP_FAILURE, + .hdr.id=ad->id, + .hdr.len=htons(sizeof(msg)-1-2), + .message=MSG_FAILURE, + }; + + log_debug("send [MSCHAP-v2 Failure id=%x \"%s\"]\n",msg.hdr.id,MSG_FAILURE); + + ppp_chan_send(ad->ppp,&msg,ntohs(msg.hdr.len)+2); +} + +static int generate_response(struct chap_auth_data_t *ad, struct chap_response_t *msg, uint8_t *response) +{ + MD4_CTX md4_ctx; + SHA_CTX sha_ctx; + char *passwd; + char *u_passwd; + char *name; + uint8_t pw_hash[MD4_DIGEST_LENGTH]; + uint8_t c_hash[SHA_DIGEST_LENGTH]; + int i; + + name=strndup(msg->name,ntohs(msg->hdr.len)-sizeof(*msg)+2); + passwd=pwdb_get_passwd(ad->ppp,name); + if (!passwd) + { + free(name); + return -1; + } + + u_passwd=malloc(strlen(passwd)*2); + for(i=0; int_hash,24); + SHA1_Update(&sha_ctx,magic1,39); + SHA1_Final(response,&sha_ctx); + + SHA1_Init(&sha_ctx); + SHA1_Update(&sha_ctx,msg->peer_challenge,16); + SHA1_Update(&sha_ctx,ad->val,16); + SHA1_Update(&sha_ctx,name,strlen(name)); + SHA1_Final(c_hash,&sha_ctx); + + SHA1_Init(&sha_ctx); + SHA1_Update(&sha_ctx,response,20); + SHA1_Update(&sha_ctx,c_hash,8); + SHA1_Update(&sha_ctx,magic2,41); + SHA1_Final(response,&sha_ctx); + + free(name); + free(passwd); + free(u_passwd); + + return 0; +} + +static void chap_send_success(struct chap_auth_data_t *ad, struct chap_response_t *res_msg) +{ + struct chap_success_t msg= + { + .hdr.proto=htons(PPP_CHAP), + .hdr.code=CHAP_SUCCESS, + .hdr.id=ad->id, + .hdr.len=htons(sizeof(msg)-1-2), + .message=MSG_SUCCESS, + }; + uint8_t response[20]; + int i; + + if (generate_response(ad,res_msg,response)) + return; + for(i=0; i<20; i++) + sprintf(msg.message+2+i*2,"%02X",response[i]); + msg.message[2+i*2]=' '; + + log_debug("send [MSCHAP-v2 Success id=%x \"%s\"]\n",msg.hdr.id,msg.message); + + ppp_chan_send(ad->ppp,&msg,ntohs(msg.hdr.len)+2); +} + +static void chap_send_challenge(struct chap_auth_data_t *ad) +{ + struct chap_challenge_t msg= + { + .hdr.proto=htons(PPP_CHAP), + .hdr.code=CHAP_CHALLENGE, + .hdr.id=++ad->id, + .hdr.len=htons(sizeof(msg)-2), + .val_size=VALUE_SIZE, + }; + + read(urandom_fd,ad->val,VALUE_SIZE); + memcpy(msg.val,ad->val,VALUE_SIZE); + + log_debug("send [MSCHAP-v2 Challenge id=%x <",msg.hdr.id); + print_buf(msg.val,VALUE_SIZE); + log_debug(">]\n"); + + ppp_chan_send(ad->ppp,&msg,ntohs(msg.hdr.len)+2); +} + +static void chap_recv_response(struct chap_auth_data_t *ad, struct chap_hdr_t *hdr) +{ + struct chap_response_t *msg=(struct chap_response_t*)hdr; + + log_debug("recv [MSCHAP-v2 Response id=%x <", msg->hdr.id); + print_buf(msg->peer_challenge,16); + log_debug(">, <"); + print_buf(msg->nt_hash,24); + log_debug(">, F=%i, name=\"",msg->flags); + print_str(msg->name,ntohs(msg->hdr.len)-sizeof(*msg)+2); + log_debug("\"]\n"); + + if (msg->hdr.id!=ad->id) + { + log_error("mschap-v2: id mismatch\n"); + chap_send_failure(ad); + ppp_terminate(ad->ppp, 0); + } + + if (msg->val_size!=RESPONSE_VALUE_SIZE) + { + log_error("mschap-v2: value-size should be %i, expected %i\n",RESPONSE_VALUE_SIZE,msg->val_size); + chap_send_failure(ad); + ppp_terminate(ad->ppp, 0); + } + + if (chap_check_response(ad,msg)) + { + chap_send_failure(ad); + auth_failed(ad->ppp); + }else + { + chap_send_success(ad,msg); + auth_successed(ad->ppp); + } +} + +static void des_encrypt(const uint8_t *input, const uint8_t *key, uint8_t *output) +{ + int i,j,parity; + union + { + uint64_t u64; + uint8_t buf[8]; + } p_key; + DES_cblock cb; + DES_cblock res; + DES_key_schedule ks; + + memcpy(p_key.buf,key,7); + p_key.u64=bswap_64(p_key.u64); + + for(i=0;i<8;i++) + { + cb[i]=(((p_key.u64<<(7*i))>>56)&0xfe); + for(j=0, parity=0; j<7; j++) + if ((cb[i]>>(j+1))&1) parity++; + cb[i]|=(~parity)&1; + } + + DES_set_key_checked(&cb, &ks); + memcpy(cb,input,8); + DES_ecb_encrypt(&cb,&res,&ks,DES_ENCRYPT); + memcpy(output,res,8); +} + +static int chap_check_response(struct chap_auth_data_t *ad, struct chap_response_t *msg) +{ + MD4_CTX md4_ctx; + SHA_CTX sha_ctx; + uint8_t z_hash[21]; + uint8_t c_hash[SHA_DIGEST_LENGTH]; + uint8_t nt_hash[24]; + char *passwd; + char *u_passwd; + char *name; + int i; + + name=strndup(msg->name,ntohs(msg->hdr.len)-sizeof(*msg)+2); + passwd=pwdb_get_passwd(ad->ppp,name); + if (!passwd) + { + free(name); + log_debug("mschap-v2: user not found\n"); + chap_send_failure(ad); + return -1; + } + + u_passwd=malloc(strlen(passwd)*2); + for(i=0; ipeer_challenge,16); + SHA1_Update(&sha_ctx,ad->val,16); + SHA1_Update(&sha_ctx,name,strlen(name)); + SHA1_Final(c_hash,&sha_ctx); + + memset(z_hash,0,sizeof(z_hash)); + MD4_Init(&md4_ctx); + MD4_Update(&md4_ctx,u_passwd,strlen(passwd)*2); + MD4_Final(z_hash,&md4_ctx); + + des_encrypt(c_hash,z_hash,nt_hash); + des_encrypt(c_hash,z_hash+7,nt_hash+8); + des_encrypt(c_hash,z_hash+14,nt_hash+16); + + free(name); + free(passwd); + free(u_passwd); + + return memcmp(nt_hash,msg->nt_hash,24); +} + +static struct ppp_auth_handler_t chap= +{ + .name="MSCHAP-v2", + .init=auth_data_init, + .free=auth_data_free, + .send_conf_req=lcp_send_conf_req, + .recv_conf_req=lcp_recv_conf_req, + .start=chap_start, + .finish=chap_finish, +}; + +static void chap_recv(struct ppp_handler_t *h) +{ + struct chap_auth_data_t *d=container_of(h,typeof(*d),h); + struct chap_hdr_t *hdr=(struct chap_hdr_t *)d->ppp->chan_buf; + + if (d->ppp->chan_buf_sizelen)len)ppp->chan_buf_size-2) + { + log_warn("mschap-v2: short packet received\n"); + return; + } + + if (hdr->code==CHAP_RESPONSE) chap_recv_response(d,hdr); + else + { + log_warn("mschap-v2: unknown code received %x\n",hdr->code); + } +} + +static void __init auth_mschap_v2_init() +{ + urandom_fd=open("/dev/urandom",O_RDONLY); + if (urandom_fd<0) + { + log_error("mschap-v2: failed to open /dev/urandom: %s\n",strerror(errno)); + return; + } + if (ppp_auth_register_handler(&chap)) + log_error("mschap-v2: failed to register handler\n"); +} + diff --git a/accel-pptpd/auth/auth_pap.c b/accel-pptpd/auth/auth_pap.c new file mode 100644 index 0000000..92ddef5 --- /dev/null +++ b/accel-pptpd/auth/auth_pap.c @@ -0,0 +1,214 @@ +#include +#include +#include +#include + +#include "log.h" +#include "ppp.h" +#include "ppp_auth.h" +#include "ppp_lcp.h" +#include "pwdb.h" + +#define MSG_FAILED "Authentication failed" +#define MSG_SUCCESSED "Authentication successed" + +#define HDR_LEN (sizeof(struct pap_hdr_t)-2) + +#define PAP_REQ 1 +#define PAP_ACK 2 +#define PAP_NAK 3 + +char *strndup(const char *s, size_t n); + +static struct auth_data_t* auth_data_init(struct ppp_t *ppp); +static void auth_data_free(struct ppp_t*, struct auth_data_t*); +static int lcp_send_conf_req(struct ppp_t*, struct auth_data_t*, uint8_t*); +static int lcp_recv_conf_req(struct ppp_t*, struct auth_data_t*, uint8_t*); +static int pap_start(struct ppp_t*, struct auth_data_t*); +static int pap_finish(struct ppp_t*, struct auth_data_t*); +static void pap_recv(struct ppp_handler_t*h); + +struct pap_auth_data_t +{ + struct auth_data_t auth; + struct ppp_handler_t h; + struct ppp_t *ppp; +}; + +struct pap_hdr_t +{ + uint16_t proto; + uint8_t code; + uint8_t id; + uint16_t len; +} __attribute__((packed)); + +struct pap_ack_t +{ + struct pap_hdr_t hdr; + uint8_t msg_len; + char msg[0]; +} __attribute__((packed)); + +static struct ppp_auth_handler_t pap= +{ + .name="PAP", + .init=auth_data_init, + .free=auth_data_free, + .send_conf_req=lcp_send_conf_req, + .recv_conf_req=lcp_recv_conf_req, + .start=pap_start, + .finish=pap_finish, +}; + +static struct auth_data_t* auth_data_init(struct ppp_t *ppp) +{ + struct pap_auth_data_t *d=malloc(sizeof(*d)); + + memset(d,0,sizeof(*d)); + d->auth.proto=PPP_PAP; + d->ppp=ppp; + + return &d->auth; +} + +static void auth_data_free(struct ppp_t *ppp,struct auth_data_t *auth) +{ + struct pap_auth_data_t *d=container_of(auth,typeof(*d),auth); + + free(d); +} + +static int pap_start(struct ppp_t *ppp, struct auth_data_t *auth) +{ + struct pap_auth_data_t *d=container_of(auth,typeof(*d),auth); + + d->h.proto=PPP_PAP; + d->h.recv=pap_recv; + + ppp_register_chan_handler(ppp,&d->h); + + return 0; +} +static int pap_finish(struct ppp_t *ppp, struct auth_data_t *auth) +{ + struct pap_auth_data_t *d=container_of(auth,typeof(*d),auth); + + ppp_unregister_handler(ppp,&d->h); + + return 0; +} + +static int lcp_send_conf_req(struct ppp_t *ppp, struct auth_data_t *d, uint8_t *ptr) +{ + return 0; +} + +static int lcp_recv_conf_req(struct ppp_t *ppp, struct auth_data_t *d, uint8_t *ptr) +{ + return LCP_OPT_ACK; +} + +static void pap_send_ack(struct pap_auth_data_t *p, int id) +{ + uint8_t buf[128]; + struct pap_ack_t *msg=(struct pap_ack_t*)buf; + msg->hdr.proto=htons(PPP_PAP); + msg->hdr.code=PAP_ACK; + msg->hdr.id=id; + msg->hdr.len=htons(HDR_LEN+1+sizeof(MSG_SUCCESSED)-1); + msg->msg_len=sizeof(MSG_SUCCESSED)-1; + memcpy(msg->msg,MSG_SUCCESSED,sizeof(MSG_SUCCESSED)); + + log_debug("send [PAP AuthAck id=%x \"%s\"]\n",id,MSG_SUCCESSED); + + ppp_chan_send(p->ppp,msg,ntohs(msg->hdr.len)+2); +} + +static void pap_send_nak(struct pap_auth_data_t *p,int id) +{ + uint8_t buf[128]; + struct pap_ack_t *msg=(struct pap_ack_t*)buf; + msg->hdr.proto=htons(PPP_PAP); + msg->hdr.code=PAP_NAK; + msg->hdr.id=id; + msg->hdr.len=htons(HDR_LEN+1+sizeof(MSG_FAILED)-1); + msg->msg_len=sizeof(MSG_FAILED)-1; + memcpy(msg->msg,MSG_FAILED,sizeof(MSG_FAILED)); + + log_debug("send [PAP AuthNak id=%x \"%s\"]\n",id,MSG_FAILED); + + ppp_chan_send(p->ppp,msg,ntohs(msg->hdr.len)+2); +} + +static int pap_recv_req(struct pap_auth_data_t *p,struct pap_hdr_t *hdr) +{ + int ret; + char *peer_id; + char *passwd; + int peer_id_len; + int passwd_len; + uint8_t *ptr=(uint8_t*)(hdr+1); + + log_debug("recv [PAP AuthReq id=%x]\n",hdr->id); + + peer_id_len=*(uint8_t*)ptr; ptr++; + if (peer_id_len>ntohs(hdr->len)-sizeof(*hdr)+2-1) + { + log_warn("PAP: short packet received\n"); + return -1; + } + peer_id=(char*)ptr; ptr+=peer_id_len; + + passwd_len=*(uint8_t*)ptr; ptr++; + if (passwd_len>ntohs(hdr->len)-sizeof(*hdr)+2-2-peer_id_len) + { + log_warn("PAP: short packet received\n"); + return -1; + } + + peer_id=strndup((const char*)peer_id,peer_id_len); + passwd=strndup((const char*)ptr,passwd_len); + + if (pwdb_check(p->ppp,peer_id,passwd)) + { + log_warn("PAP: authentication error\n"); + pap_send_nak(p,hdr->id); + auth_failed(p->ppp); + ret=-1; + }else + { + pap_send_ack(p,hdr->id); + auth_successed(p->ppp); + ret=0; + } + + free(peer_id); + free(passwd); + + return ret; +} + +static void pap_recv(struct ppp_handler_t *h) +{ + struct pap_auth_data_t *d=container_of(h,typeof(*d),h); + struct pap_hdr_t *hdr=(struct pap_hdr_t *)d->ppp->chan_buf; + + if (d->ppp->chan_buf_sizelen)len)ppp->chan_buf_size-2) + { + log_warn("PAP: short packet received\n"); + return; + } + + if (hdr->code==PAP_REQ) pap_recv_req(d,hdr); + else + { + log_warn("PAP: unknown code received %x\n",hdr->code); + } +} + +static void __init auth_pap_init() +{ + ppp_auth_register_handler(&pap); +} + diff --git a/accel-pptpd/auth_chap_md5.c b/accel-pptpd/auth_chap_md5.c deleted file mode 100644 index 5577794..0000000 --- a/accel-pptpd/auth_chap_md5.c +++ /dev/null @@ -1,294 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -#include "log.h" -#include "ppp.h" -#include "ppp_auth.h" -#include "ppp_lcp.h" -#include "pwdb.h" - -#define CHAP_CHALLENGE 1 -#define CHAP_RESPONSE 2 -#define CHAP_SUCCESS 3 -#define CHAP_FAILURE 4 - -#define VALUE_SIZE 16 - -#define MSG_FAILURE "Authentication failed" -#define MSG_SUCCESS "Authentication successed" - -#define HDR_LEN (sizeof(struct chap_hdr_t)-2) - -static int urandom_fd; - -struct chap_hdr_t -{ - uint16_t proto; - uint8_t code; - uint8_t id; - uint16_t len; -} __attribute__((packed)); - -struct chap_challenge_t -{ - struct chap_hdr_t hdr; - uint8_t val_size; - uint8_t val[VALUE_SIZE]; - char name[0]; -} __attribute__((packed)); - -struct chap_failure_t -{ - struct chap_hdr_t hdr; - char message[sizeof(MSG_FAILURE)]; -} __attribute__((packed)); - -struct chap_success_t -{ - struct chap_hdr_t hdr; - char message[sizeof(MSG_SUCCESS)]; -} __attribute__((packed)); - - -struct chap_auth_data_t -{ - struct auth_data_t auth; - struct ppp_handler_t h; - struct ppp_t *ppp; - int id; - uint8_t val[VALUE_SIZE]; -}; - -static void chap_send_challenge(struct chap_auth_data_t *ad); -static void chap_recv(struct ppp_handler_t *h); - -static void print_buf(const uint8_t *buf,int size) -{ - int i; - for(i=0;iauth.proto=PPP_CHAP; - d->ppp=ppp; - - return &d->auth; -} - -static void auth_data_free(struct ppp_t *ppp,struct auth_data_t *auth) -{ - struct chap_auth_data_t *d=container_of(auth,typeof(*d),auth); - - free(d); -} - -static int chap_start(struct ppp_t *ppp, struct auth_data_t *auth) -{ - struct chap_auth_data_t *d=container_of(auth,typeof(*d),auth); - - d->h.proto=PPP_CHAP; - d->h.recv=chap_recv; - - ppp_register_chan_handler(ppp,&d->h); - - chap_send_challenge(d); - - return 0; -} - -static int chap_finish(struct ppp_t *ppp, struct auth_data_t *auth) -{ - struct chap_auth_data_t *d=container_of(auth,typeof(*d),auth); - - ppp_unregister_handler(ppp,&d->h); - - return 0; -} - -static int lcp_send_conf_req(struct ppp_t *ppp, struct auth_data_t *d, uint8_t *ptr) -{ - *ptr=5; - return 1; -} - -static int lcp_recv_conf_req(struct ppp_t *ppp, struct auth_data_t *d, uint8_t *ptr) -{ - if (*ptr==5) - return LCP_OPT_ACK; - return LCP_OPT_NAK; -} - -static void chap_send_failure(struct chap_auth_data_t *ad) -{ - struct chap_failure_t msg= - { - .hdr.proto=htons(PPP_CHAP), - .hdr.code=CHAP_FAILURE, - .hdr.id=ad->id, - .hdr.len=htons(sizeof(msg)-1-2), - .message=MSG_FAILURE, - }; - - log_debug("send [CHAP Failure id=%x \"%s\"]\n",msg.hdr.id,MSG_FAILURE); - - ppp_chan_send(ad->ppp,&msg,ntohs(msg.hdr.len)+2); -} - -static void chap_send_success(struct chap_auth_data_t *ad) -{ - struct chap_success_t msg= - { - .hdr.proto=htons(PPP_CHAP), - .hdr.code=CHAP_SUCCESS, - .hdr.id=ad->id, - .hdr.len=htons(sizeof(msg)-1-2), - .message=MSG_SUCCESS, - }; - - log_debug("send [CHAP Success id=%x \"%s\"]\n",msg.hdr.id,MSG_SUCCESS); - - ppp_chan_send(ad->ppp,&msg,ntohs(msg.hdr.len)+2); -} - -static void chap_send_challenge(struct chap_auth_data_t *ad) -{ - struct chap_challenge_t msg= - { - .hdr.proto=htons(PPP_CHAP), - .hdr.code=CHAP_CHALLENGE, - .hdr.id=++ad->id, - .hdr.len=htons(sizeof(msg)-2), - .val_size=VALUE_SIZE, - }; - - read(urandom_fd,ad->val,VALUE_SIZE); - memcpy(msg.val,ad->val,VALUE_SIZE); - - log_debug("send [CHAP Challenge id=%x <",msg.hdr.id); - print_buf(msg.val,VALUE_SIZE); - log_debug(">]\n"); - - ppp_chan_send(ad->ppp,&msg,ntohs(msg.hdr.len)+2); -} - -static void chap_recv_response(struct chap_auth_data_t *ad, struct chap_hdr_t *hdr) -{ - MD5_CTX md5_ctx; - uint8_t md5[MD5_DIGEST_LENGTH]; - char *passwd; - char *name; - struct chap_challenge_t *msg=(struct chap_challenge_t*)hdr; - - log_debug("recv [CHAP Response id=%x <", msg->hdr.id); - print_buf(msg->val,msg->val_size); - log_debug(">, name=\""); - print_str(msg->name,ntohs(msg->hdr.len)-sizeof(*msg)+2); - log_debug("\"]\n"); - - if (msg->hdr.id!=ad->id) - { - log_error("chap-md5: id mismatch\n"); - chap_send_failure(ad); - ppp_terminate(ad->ppp, 0); - } - - if (msg->val_size!=VALUE_SIZE) - { - log_error("chap-md5: value-size should be %i, expected %i\n",VALUE_SIZE,msg->val_size); - chap_send_failure(ad); - ppp_terminate(ad->ppp, 0); - } - - name=strndup(msg->name,ntohs(msg->hdr.len)-sizeof(*msg)+2); - passwd=pwdb_get_passwd(ad->ppp,name); - if (!passwd) - { - free(name); - log_debug("chap-md5: user not found\n"); - chap_send_failure(ad); - return; - } - - MD5_Init(&md5_ctx); - MD5_Update(&md5_ctx,&msg->hdr.id,1); - MD5_Update(&md5_ctx,passwd,strlen(passwd)); - MD5_Update(&md5_ctx,ad->val,VALUE_SIZE); - MD5_Final(md5,&md5_ctx); - - if (memcmp(md5,msg->val,sizeof(md5))) - { - log_debug("chap-md5: challenge response mismatch\n"); - chap_send_failure(ad); - auth_failed(ad->ppp); - }else - { - chap_send_success(ad); - auth_successed(ad->ppp); - } - - free(name); - free(passwd); -} - -static struct ppp_auth_handler_t chap= -{ - .name="CHAP-md5", - .init=auth_data_init, - .free=auth_data_free, - .send_conf_req=lcp_send_conf_req, - .recv_conf_req=lcp_recv_conf_req, - .start=chap_start, - .finish=chap_finish, -}; - -static void chap_recv(struct ppp_handler_t *h) -{ - struct chap_auth_data_t *d=container_of(h,typeof(*d),h); - struct chap_hdr_t *hdr=(struct chap_hdr_t *)d->ppp->chan_buf; - - if (d->ppp->chan_buf_sizelen)len)ppp->chan_buf_size-2) - { - log_warn("CHAP: short packet received\n"); - return; - } - - if (hdr->code==CHAP_RESPONSE) chap_recv_response(d,hdr); - else - { - log_warn("CHAP: unknown code received %x\n",hdr->code); - } -} - -static void __init auth_chap_md5_init() -{ - urandom_fd=open("/dev/urandom",O_RDONLY); - if (urandom_fd<0) - { - log_error("chap-md5: failed to open /dev/urandom: %s\n",strerror(errno)); - return; - } - if (ppp_auth_register_handler(&chap)) - log_error("chap-md5: failed to register handler\n"); -} - diff --git a/accel-pptpd/auth_mschap_v1.c b/accel-pptpd/auth_mschap_v1.c deleted file mode 100644 index 1cf5eb8..0000000 --- a/accel-pptpd/auth_mschap_v1.c +++ /dev/null @@ -1,358 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -#include "log.h" -#include "ppp.h" -#include "ppp_auth.h" -#include "ppp_lcp.h" -#include "pwdb.h" - -#define CHAP_CHALLENGE 1 -#define CHAP_RESPONSE 2 -#define CHAP_SUCCESS 3 -#define CHAP_FAILURE 4 - -#define VALUE_SIZE 8 -#define RESPONSE_VALUE_SIZE (24+24+1) - -#define MSG_FAILURE "E=691 R=0" -#define MSG_SUCCESS "Authentication successed" - -#define HDR_LEN (sizeof(struct chap_hdr_t)-2) - -static int urandom_fd; - -struct chap_hdr_t -{ - uint16_t proto; - uint8_t code; - uint8_t id; - uint16_t len; -} __attribute__((packed)); - -struct chap_challenge_t -{ - struct chap_hdr_t hdr; - uint8_t val_size; - uint8_t val[VALUE_SIZE]; - char name[0]; -} __attribute__((packed)); - -struct chap_response_t -{ - struct chap_hdr_t hdr; - uint8_t val_size; - uint8_t lm_hash[24]; - uint8_t nt_hash[24]; - uint8_t use_nt_hash; - char name[0]; -} __attribute__((packed)); - -struct chap_failure_t -{ - struct chap_hdr_t hdr; - char message[sizeof(MSG_FAILURE)]; -} __attribute__((packed)); - -struct chap_success_t -{ - struct chap_hdr_t hdr; - char message[sizeof(MSG_SUCCESS)]; -} __attribute__((packed)); - - -struct chap_auth_data_t -{ - struct auth_data_t auth; - struct ppp_handler_t h; - struct ppp_t *ppp; - int id; - uint8_t val[VALUE_SIZE]; -}; - -static void chap_send_challenge(struct chap_auth_data_t *ad); -static void chap_recv(struct ppp_handler_t *h); -static int chap_check_response(struct chap_auth_data_t *ad, struct chap_response_t *res); - -static void print_buf(const uint8_t *buf,int size) -{ - int i; - for(i=0;iauth.proto=PPP_CHAP; - d->ppp=ppp; - - return &d->auth; -} - -static void auth_data_free(struct ppp_t *ppp,struct auth_data_t *auth) -{ - struct chap_auth_data_t *d=container_of(auth,typeof(*d),auth); - - free(d); -} - -static int chap_start(struct ppp_t *ppp, struct auth_data_t *auth) -{ - struct chap_auth_data_t *d=container_of(auth,typeof(*d),auth); - - d->h.proto=PPP_CHAP; - d->h.recv=chap_recv; - - ppp_register_chan_handler(ppp,&d->h); - - chap_send_challenge(d); - - return 0; -} - -static int chap_finish(struct ppp_t *ppp, struct auth_data_t *auth) -{ - struct chap_auth_data_t *d=container_of(auth,typeof(*d),auth); - - ppp_unregister_handler(ppp,&d->h); - - return 0; -} - -static int lcp_send_conf_req(struct ppp_t *ppp, struct auth_data_t *d, uint8_t *ptr) -{ - *ptr=0x80; - return 1; -} - -static int lcp_recv_conf_req(struct ppp_t *ppp, struct auth_data_t *d, uint8_t *ptr) -{ - if (*ptr==0x80) - return LCP_OPT_ACK; - return LCP_OPT_NAK; -} - -static void chap_send_failure(struct chap_auth_data_t *ad) -{ - struct chap_failure_t msg= - { - .hdr.proto=htons(PPP_CHAP), - .hdr.code=CHAP_FAILURE, - .hdr.id=ad->id, - .hdr.len=htons(sizeof(msg)-1-2), - .message=MSG_FAILURE, - }; - - log_debug("send [MSCHAP-v1 Failure id=%x \"%s\"]\n",msg.hdr.id,MSG_FAILURE); - - ppp_chan_send(ad->ppp,&msg,ntohs(msg.hdr.len)+2); -} - -static void chap_send_success(struct chap_auth_data_t *ad) -{ - struct chap_success_t msg= - { - .hdr.proto=htons(PPP_CHAP), - .hdr.code=CHAP_SUCCESS, - .hdr.id=ad->id, - .hdr.len=htons(sizeof(msg)-1-2), - .message=MSG_SUCCESS, - }; - - log_debug("send [MSCHAP-v1 Success id=%x \"%s\"]\n",msg.hdr.id,MSG_SUCCESS); - - ppp_chan_send(ad->ppp,&msg,ntohs(msg.hdr.len)+2); -} - -static void chap_send_challenge(struct chap_auth_data_t *ad) -{ - struct chap_challenge_t msg= - { - .hdr.proto=htons(PPP_CHAP), - .hdr.code=CHAP_CHALLENGE, - .hdr.id=++ad->id, - .hdr.len=htons(sizeof(msg)-2), - .val_size=VALUE_SIZE, - }; - - read(urandom_fd,ad->val,VALUE_SIZE); - memcpy(msg.val,ad->val,VALUE_SIZE); - - log_debug("send [MSCHAP-v1 Challenge id=%x <",msg.hdr.id); - print_buf(msg.val,VALUE_SIZE); - log_debug(">]\n"); - - ppp_chan_send(ad->ppp,&msg,ntohs(msg.hdr.len)+2); -} - -static void chap_recv_response(struct chap_auth_data_t *ad, struct chap_hdr_t *hdr) -{ - struct chap_response_t *msg=(struct chap_response_t*)hdr; - - log_debug("recv [MSCHAP-v1 Response id=%x <", msg->hdr.id); - print_buf(msg->lm_hash,24); - log_debug(">, <"); - print_buf(msg->nt_hash,24); - log_debug(">, F=%i, name=\"",msg->use_nt_hash); - print_str(msg->name,ntohs(msg->hdr.len)-sizeof(*msg)+2); - log_debug("\"]\n"); - - if (msg->hdr.id!=ad->id) - { - log_error("mschap-v1: id mismatch\n"); - chap_send_failure(ad); - ppp_terminate(ad->ppp, 0); - } - - if (msg->val_size!=RESPONSE_VALUE_SIZE) - { - log_error("mschap-v1: value-size should be %i, expected %i\n",RESPONSE_VALUE_SIZE,msg->val_size); - chap_send_failure(ad); - ppp_terminate(ad->ppp, 0); - } - - if (chap_check_response(ad,msg)) - { - chap_send_failure(ad); - auth_failed(ad->ppp); - }else - { - chap_send_success(ad); - auth_successed(ad->ppp); - } -} - -static void des_encrypt(const uint8_t *input, const uint8_t *key, uint8_t *output) -{ - int i,j,parity; - union - { - uint64_t u64; - uint8_t buf[8]; - } p_key; - DES_cblock cb; - DES_cblock res; - DES_key_schedule ks; - - memcpy(p_key.buf,key,7); - p_key.u64=bswap_64(p_key.u64); - - for(i=0;i<8;i++) - { - cb[i]=(((p_key.u64<<(7*i))>>56)&0xfe); - for(j=0, parity=0; j<7; j++) - if ((cb[i]>>(j+1))&1) parity++; - cb[i]|=(~parity)&1; - } - - DES_set_key_checked(&cb, &ks); - memcpy(cb,input,8); - DES_ecb_encrypt(&cb,&res,&ks,DES_ENCRYPT); - memcpy(output,res,8); -} - -static int chap_check_response(struct chap_auth_data_t *ad, struct chap_response_t *msg) -{ - MD4_CTX md4_ctx; - uint8_t z_hash[21]; - uint8_t nt_hash[24]; - char *passwd; - char *u_passwd; - char *name; - int i; - - name=strndup(msg->name,ntohs(msg->hdr.len)-sizeof(*msg)+2); - passwd=pwdb_get_passwd(ad->ppp,name); - if (!passwd) - { - free(name); - log_debug("mschap-v1: user not found\n"); - chap_send_failure(ad); - return -1; - } - - u_passwd=malloc(strlen(passwd)*2); - for(i=0; ival,z_hash,nt_hash); - des_encrypt(ad->val,z_hash+7,nt_hash+8); - des_encrypt(ad->val,z_hash+14,nt_hash+16); - - free(name); - free(passwd); - free(u_passwd); - - return memcmp(nt_hash,msg->nt_hash,24); -} - -static struct ppp_auth_handler_t chap= -{ - .name="MSCHAP-v1", - .init=auth_data_init, - .free=auth_data_free, - .send_conf_req=lcp_send_conf_req, - .recv_conf_req=lcp_recv_conf_req, - .start=chap_start, - .finish=chap_finish, -}; - -static void chap_recv(struct ppp_handler_t *h) -{ - struct chap_auth_data_t *d=container_of(h,typeof(*d),h); - struct chap_hdr_t *hdr=(struct chap_hdr_t *)d->ppp->chan_buf; - - if (d->ppp->chan_buf_sizelen)len)ppp->chan_buf_size-2) - { - log_warn("mschap-v1: short packet received\n"); - return; - } - - if (hdr->code==CHAP_RESPONSE) chap_recv_response(d,hdr); - else - { - log_warn("mschap-v1: unknown code received %x\n",hdr->code); - } -} - -static void __init auth_mschap_v1_init() -{ - urandom_fd=open("/dev/urandom",O_RDONLY); - if (urandom_fd<0) - { - log_error("mschap-v1: failed to open /dev/urandom: %s\n",strerror(errno)); - return; - } - if (ppp_auth_register_handler(&chap)) - log_error("mschap-v1: failed to register handler\n"); -} - diff --git a/accel-pptpd/auth_mschap_v2.c b/accel-pptpd/auth_mschap_v2.c deleted file mode 100644 index 502f368..0000000 --- a/accel-pptpd/auth_mschap_v2.c +++ /dev/null @@ -1,446 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include - -#include "log.h" -#include "ppp.h" -#include "ppp_auth.h" -#include "ppp_lcp.h" -#include "pwdb.h" - -#define CHAP_CHALLENGE 1 -#define CHAP_RESPONSE 2 -#define CHAP_SUCCESS 3 -#define CHAP_FAILURE 4 - -#define VALUE_SIZE 16 -#define RESPONSE_VALUE_SIZE (16+8+24+1) - -#define MSG_FAILURE "E=691 R=0 C=cccccccccccccccccccccccccccccccc V=3 M=Authentication failure" -#define MSG_SUCCESS "S=cccccccccccccccccccccccccccccccccccccccc M=Authentication successed" - -#define HDR_LEN (sizeof(struct chap_hdr_t)-2) - -static int urandom_fd; -static uint8_t magic1[39] = - {0x4D, 0x61, 0x67, 0x69, 0x63, 0x20, 0x73, 0x65, 0x72, 0x76, - 0x65, 0x72, 0x20, 0x74, 0x6F, 0x20, 0x63, 0x6C, 0x69, 0x65, - 0x6E, 0x74, 0x20, 0x73, 0x69, 0x67, 0x6E, 0x69, 0x6E, 0x67, - 0x20, 0x63, 0x6F, 0x6E, 0x73, 0x74, 0x61, 0x6E, 0x74}; -static uint8_t magic2[41] = - {0x50, 0x61, 0x64, 0x20, 0x74, 0x6F, 0x20, 0x6D, 0x61, 0x6B, - 0x65, 0x20, 0x69, 0x74, 0x20, 0x64, 0x6F, 0x20, 0x6D, 0x6F, - 0x72, 0x65, 0x20, 0x74, 0x68, 0x61, 0x6E, 0x20, 0x6F, 0x6E, - 0x65, 0x20, 0x69, 0x74, 0x65, 0x72, 0x61, 0x74, 0x69, 0x6F, - 0x6E}; - -struct chap_hdr_t -{ - uint16_t proto; - uint8_t code; - uint8_t id; - uint16_t len; -} __attribute__((packed)); - -struct chap_challenge_t -{ - struct chap_hdr_t hdr; - uint8_t val_size; - uint8_t val[VALUE_SIZE]; - char name[0]; -} __attribute__((packed)); - -struct chap_response_t -{ - struct chap_hdr_t hdr; - uint8_t val_size; - uint8_t peer_challenge[16]; - uint8_t reserved[8]; - uint8_t nt_hash[24]; - uint8_t flags; - char name[0]; -} __attribute__((packed)); - -struct chap_failure_t -{ - struct chap_hdr_t hdr; - char message[sizeof(MSG_FAILURE)]; -} __attribute__((packed)); - -struct chap_success_t -{ - struct chap_hdr_t hdr; - char message[sizeof(MSG_SUCCESS)]; -} __attribute__((packed)); - - -struct chap_auth_data_t -{ - struct auth_data_t auth; - struct ppp_handler_t h; - struct ppp_t *ppp; - int id; - uint8_t val[VALUE_SIZE]; -}; - -static void chap_send_challenge(struct chap_auth_data_t *ad); -static void chap_recv(struct ppp_handler_t *h); -static int chap_check_response(struct chap_auth_data_t *ad, struct chap_response_t *res); - -static void print_buf(const uint8_t *buf,int size) -{ - int i; - for(i=0;iauth.proto=PPP_CHAP; - d->ppp=ppp; - - return &d->auth; -} - -static void auth_data_free(struct ppp_t *ppp,struct auth_data_t *auth) -{ - struct chap_auth_data_t *d=container_of(auth,typeof(*d),auth); - - free(d); -} - -static int chap_start(struct ppp_t *ppp, struct auth_data_t *auth) -{ - struct chap_auth_data_t *d=container_of(auth,typeof(*d),auth); - - d->h.proto=PPP_CHAP; - d->h.recv=chap_recv; - - ppp_register_chan_handler(ppp,&d->h); - - chap_send_challenge(d); - - return 0; -} - -static int chap_finish(struct ppp_t *ppp, struct auth_data_t *auth) -{ - struct chap_auth_data_t *d=container_of(auth,typeof(*d),auth); - - ppp_unregister_handler(ppp,&d->h); - - return 0; -} - -static int lcp_send_conf_req(struct ppp_t *ppp, struct auth_data_t *d, uint8_t *ptr) -{ - *ptr=0x81; - return 1; -} - -static int lcp_recv_conf_req(struct ppp_t *ppp, struct auth_data_t *d, uint8_t *ptr) -{ - if (*ptr==0x81) - return LCP_OPT_ACK; - return LCP_OPT_NAK; -} - -static void chap_send_failure(struct chap_auth_data_t *ad) -{ - struct chap_failure_t msg= - { - .hdr.proto=htons(PPP_CHAP), - .hdr.code=CHAP_FAILURE, - .hdr.id=ad->id, - .hdr.len=htons(sizeof(msg)-1-2), - .message=MSG_FAILURE, - }; - - log_debug("send [MSCHAP-v2 Failure id=%x \"%s\"]\n",msg.hdr.id,MSG_FAILURE); - - ppp_chan_send(ad->ppp,&msg,ntohs(msg.hdr.len)+2); -} - -static int generate_response(struct chap_auth_data_t *ad, struct chap_response_t *msg, uint8_t *response) -{ - MD4_CTX md4_ctx; - SHA_CTX sha_ctx; - char *passwd; - char *u_passwd; - char *name; - uint8_t pw_hash[MD4_DIGEST_LENGTH]; - uint8_t c_hash[SHA_DIGEST_LENGTH]; - int i; - - name=strndup(msg->name,ntohs(msg->hdr.len)-sizeof(*msg)+2); - passwd=pwdb_get_passwd(ad->ppp,name); - if (!passwd) - { - free(name); - return -1; - } - - u_passwd=malloc(strlen(passwd)*2); - for(i=0; int_hash,24); - SHA1_Update(&sha_ctx,magic1,39); - SHA1_Final(response,&sha_ctx); - - SHA1_Init(&sha_ctx); - SHA1_Update(&sha_ctx,msg->peer_challenge,16); - SHA1_Update(&sha_ctx,ad->val,16); - SHA1_Update(&sha_ctx,name,strlen(name)); - SHA1_Final(c_hash,&sha_ctx); - - SHA1_Init(&sha_ctx); - SHA1_Update(&sha_ctx,response,20); - SHA1_Update(&sha_ctx,c_hash,8); - SHA1_Update(&sha_ctx,magic2,41); - SHA1_Final(response,&sha_ctx); - - free(name); - free(passwd); - free(u_passwd); - - return 0; -} - -static void chap_send_success(struct chap_auth_data_t *ad, struct chap_response_t *res_msg) -{ - struct chap_success_t msg= - { - .hdr.proto=htons(PPP_CHAP), - .hdr.code=CHAP_SUCCESS, - .hdr.id=ad->id, - .hdr.len=htons(sizeof(msg)-1-2), - .message=MSG_SUCCESS, - }; - uint8_t response[20]; - int i; - - if (generate_response(ad,res_msg,response)) - return; - for(i=0; i<20; i++) - sprintf(msg.message+2+i*2,"%02X",response[i]); - msg.message[2+i*2]=' '; - - log_debug("send [MSCHAP-v2 Success id=%x \"%s\"]\n",msg.hdr.id,msg.message); - - ppp_chan_send(ad->ppp,&msg,ntohs(msg.hdr.len)+2); -} - -static void chap_send_challenge(struct chap_auth_data_t *ad) -{ - struct chap_challenge_t msg= - { - .hdr.proto=htons(PPP_CHAP), - .hdr.code=CHAP_CHALLENGE, - .hdr.id=++ad->id, - .hdr.len=htons(sizeof(msg)-2), - .val_size=VALUE_SIZE, - }; - - read(urandom_fd,ad->val,VALUE_SIZE); - memcpy(msg.val,ad->val,VALUE_SIZE); - - log_debug("send [MSCHAP-v2 Challenge id=%x <",msg.hdr.id); - print_buf(msg.val,VALUE_SIZE); - log_debug(">]\n"); - - ppp_chan_send(ad->ppp,&msg,ntohs(msg.hdr.len)+2); -} - -static void chap_recv_response(struct chap_auth_data_t *ad, struct chap_hdr_t *hdr) -{ - struct chap_response_t *msg=(struct chap_response_t*)hdr; - - log_debug("recv [MSCHAP-v2 Response id=%x <", msg->hdr.id); - print_buf(msg->peer_challenge,16); - log_debug(">, <"); - print_buf(msg->nt_hash,24); - log_debug(">, F=%i, name=\"",msg->flags); - print_str(msg->name,ntohs(msg->hdr.len)-sizeof(*msg)+2); - log_debug("\"]\n"); - - if (msg->hdr.id!=ad->id) - { - log_error("mschap-v2: id mismatch\n"); - chap_send_failure(ad); - ppp_terminate(ad->ppp, 0); - } - - if (msg->val_size!=RESPONSE_VALUE_SIZE) - { - log_error("mschap-v2: value-size should be %i, expected %i\n",RESPONSE_VALUE_SIZE,msg->val_size); - chap_send_failure(ad); - ppp_terminate(ad->ppp, 0); - } - - if (chap_check_response(ad,msg)) - { - chap_send_failure(ad); - auth_failed(ad->ppp); - }else - { - chap_send_success(ad,msg); - auth_successed(ad->ppp); - } -} - -static void des_encrypt(const uint8_t *input, const uint8_t *key, uint8_t *output) -{ - int i,j,parity; - union - { - uint64_t u64; - uint8_t buf[8]; - } p_key; - DES_cblock cb; - DES_cblock res; - DES_key_schedule ks; - - memcpy(p_key.buf,key,7); - p_key.u64=bswap_64(p_key.u64); - - for(i=0;i<8;i++) - { - cb[i]=(((p_key.u64<<(7*i))>>56)&0xfe); - for(j=0, parity=0; j<7; j++) - if ((cb[i]>>(j+1))&1) parity++; - cb[i]|=(~parity)&1; - } - - DES_set_key_checked(&cb, &ks); - memcpy(cb,input,8); - DES_ecb_encrypt(&cb,&res,&ks,DES_ENCRYPT); - memcpy(output,res,8); -} - -static int chap_check_response(struct chap_auth_data_t *ad, struct chap_response_t *msg) -{ - MD4_CTX md4_ctx; - SHA_CTX sha_ctx; - uint8_t z_hash[21]; - uint8_t c_hash[SHA_DIGEST_LENGTH]; - uint8_t nt_hash[24]; - char *passwd; - char *u_passwd; - char *name; - int i; - - name=strndup(msg->name,ntohs(msg->hdr.len)-sizeof(*msg)+2); - passwd=pwdb_get_passwd(ad->ppp,name); - if (!passwd) - { - free(name); - log_debug("mschap-v2: user not found\n"); - chap_send_failure(ad); - return -1; - } - - u_passwd=malloc(strlen(passwd)*2); - for(i=0; ipeer_challenge,16); - SHA1_Update(&sha_ctx,ad->val,16); - SHA1_Update(&sha_ctx,name,strlen(name)); - SHA1_Final(c_hash,&sha_ctx); - - memset(z_hash,0,sizeof(z_hash)); - MD4_Init(&md4_ctx); - MD4_Update(&md4_ctx,u_passwd,strlen(passwd)*2); - MD4_Final(z_hash,&md4_ctx); - - des_encrypt(c_hash,z_hash,nt_hash); - des_encrypt(c_hash,z_hash+7,nt_hash+8); - des_encrypt(c_hash,z_hash+14,nt_hash+16); - - free(name); - free(passwd); - free(u_passwd); - - return memcmp(nt_hash,msg->nt_hash,24); -} - -static struct ppp_auth_handler_t chap= -{ - .name="MSCHAP-v2", - .init=auth_data_init, - .free=auth_data_free, - .send_conf_req=lcp_send_conf_req, - .recv_conf_req=lcp_recv_conf_req, - .start=chap_start, - .finish=chap_finish, -}; - -static void chap_recv(struct ppp_handler_t *h) -{ - struct chap_auth_data_t *d=container_of(h,typeof(*d),h); - struct chap_hdr_t *hdr=(struct chap_hdr_t *)d->ppp->chan_buf; - - if (d->ppp->chan_buf_sizelen)len)ppp->chan_buf_size-2) - { - log_warn("mschap-v2: short packet received\n"); - return; - } - - if (hdr->code==CHAP_RESPONSE) chap_recv_response(d,hdr); - else - { - log_warn("mschap-v2: unknown code received %x\n",hdr->code); - } -} - -static void __init auth_mschap_v2_init() -{ - urandom_fd=open("/dev/urandom",O_RDONLY); - if (urandom_fd<0) - { - log_error("mschap-v2: failed to open /dev/urandom: %s\n",strerror(errno)); - return; - } - if (ppp_auth_register_handler(&chap)) - log_error("mschap-v2: failed to register handler\n"); -} - diff --git a/accel-pptpd/auth_pap.c b/accel-pptpd/auth_pap.c deleted file mode 100644 index 92ddef5..0000000 --- a/accel-pptpd/auth_pap.c +++ /dev/null @@ -1,214 +0,0 @@ -#include -#include -#include -#include - -#include "log.h" -#include "ppp.h" -#include "ppp_auth.h" -#include "ppp_lcp.h" -#include "pwdb.h" - -#define MSG_FAILED "Authentication failed" -#define MSG_SUCCESSED "Authentication successed" - -#define HDR_LEN (sizeof(struct pap_hdr_t)-2) - -#define PAP_REQ 1 -#define PAP_ACK 2 -#define PAP_NAK 3 - -char *strndup(const char *s, size_t n); - -static struct auth_data_t* auth_data_init(struct ppp_t *ppp); -static void auth_data_free(struct ppp_t*, struct auth_data_t*); -static int lcp_send_conf_req(struct ppp_t*, struct auth_data_t*, uint8_t*); -static int lcp_recv_conf_req(struct ppp_t*, struct auth_data_t*, uint8_t*); -static int pap_start(struct ppp_t*, struct auth_data_t*); -static int pap_finish(struct ppp_t*, struct auth_data_t*); -static void pap_recv(struct ppp_handler_t*h); - -struct pap_auth_data_t -{ - struct auth_data_t auth; - struct ppp_handler_t h; - struct ppp_t *ppp; -}; - -struct pap_hdr_t -{ - uint16_t proto; - uint8_t code; - uint8_t id; - uint16_t len; -} __attribute__((packed)); - -struct pap_ack_t -{ - struct pap_hdr_t hdr; - uint8_t msg_len; - char msg[0]; -} __attribute__((packed)); - -static struct ppp_auth_handler_t pap= -{ - .name="PAP", - .init=auth_data_init, - .free=auth_data_free, - .send_conf_req=lcp_send_conf_req, - .recv_conf_req=lcp_recv_conf_req, - .start=pap_start, - .finish=pap_finish, -}; - -static struct auth_data_t* auth_data_init(struct ppp_t *ppp) -{ - struct pap_auth_data_t *d=malloc(sizeof(*d)); - - memset(d,0,sizeof(*d)); - d->auth.proto=PPP_PAP; - d->ppp=ppp; - - return &d->auth; -} - -static void auth_data_free(struct ppp_t *ppp,struct auth_data_t *auth) -{ - struct pap_auth_data_t *d=container_of(auth,typeof(*d),auth); - - free(d); -} - -static int pap_start(struct ppp_t *ppp, struct auth_data_t *auth) -{ - struct pap_auth_data_t *d=container_of(auth,typeof(*d),auth); - - d->h.proto=PPP_PAP; - d->h.recv=pap_recv; - - ppp_register_chan_handler(ppp,&d->h); - - return 0; -} -static int pap_finish(struct ppp_t *ppp, struct auth_data_t *auth) -{ - struct pap_auth_data_t *d=container_of(auth,typeof(*d),auth); - - ppp_unregister_handler(ppp,&d->h); - - return 0; -} - -static int lcp_send_conf_req(struct ppp_t *ppp, struct auth_data_t *d, uint8_t *ptr) -{ - return 0; -} - -static int lcp_recv_conf_req(struct ppp_t *ppp, struct auth_data_t *d, uint8_t *ptr) -{ - return LCP_OPT_ACK; -} - -static void pap_send_ack(struct pap_auth_data_t *p, int id) -{ - uint8_t buf[128]; - struct pap_ack_t *msg=(struct pap_ack_t*)buf; - msg->hdr.proto=htons(PPP_PAP); - msg->hdr.code=PAP_ACK; - msg->hdr.id=id; - msg->hdr.len=htons(HDR_LEN+1+sizeof(MSG_SUCCESSED)-1); - msg->msg_len=sizeof(MSG_SUCCESSED)-1; - memcpy(msg->msg,MSG_SUCCESSED,sizeof(MSG_SUCCESSED)); - - log_debug("send [PAP AuthAck id=%x \"%s\"]\n",id,MSG_SUCCESSED); - - ppp_chan_send(p->ppp,msg,ntohs(msg->hdr.len)+2); -} - -static void pap_send_nak(struct pap_auth_data_t *p,int id) -{ - uint8_t buf[128]; - struct pap_ack_t *msg=(struct pap_ack_t*)buf; - msg->hdr.proto=htons(PPP_PAP); - msg->hdr.code=PAP_NAK; - msg->hdr.id=id; - msg->hdr.len=htons(HDR_LEN+1+sizeof(MSG_FAILED)-1); - msg->msg_len=sizeof(MSG_FAILED)-1; - memcpy(msg->msg,MSG_FAILED,sizeof(MSG_FAILED)); - - log_debug("send [PAP AuthNak id=%x \"%s\"]\n",id,MSG_FAILED); - - ppp_chan_send(p->ppp,msg,ntohs(msg->hdr.len)+2); -} - -static int pap_recv_req(struct pap_auth_data_t *p,struct pap_hdr_t *hdr) -{ - int ret; - char *peer_id; - char *passwd; - int peer_id_len; - int passwd_len; - uint8_t *ptr=(uint8_t*)(hdr+1); - - log_debug("recv [PAP AuthReq id=%x]\n",hdr->id); - - peer_id_len=*(uint8_t*)ptr; ptr++; - if (peer_id_len>ntohs(hdr->len)-sizeof(*hdr)+2-1) - { - log_warn("PAP: short packet received\n"); - return -1; - } - peer_id=(char*)ptr; ptr+=peer_id_len; - - passwd_len=*(uint8_t*)ptr; ptr++; - if (passwd_len>ntohs(hdr->len)-sizeof(*hdr)+2-2-peer_id_len) - { - log_warn("PAP: short packet received\n"); - return -1; - } - - peer_id=strndup((const char*)peer_id,peer_id_len); - passwd=strndup((const char*)ptr,passwd_len); - - if (pwdb_check(p->ppp,peer_id,passwd)) - { - log_warn("PAP: authentication error\n"); - pap_send_nak(p,hdr->id); - auth_failed(p->ppp); - ret=-1; - }else - { - pap_send_ack(p,hdr->id); - auth_successed(p->ppp); - ret=0; - } - - free(peer_id); - free(passwd); - - return ret; -} - -static void pap_recv(struct ppp_handler_t *h) -{ - struct pap_auth_data_t *d=container_of(h,typeof(*d),h); - struct pap_hdr_t *hdr=(struct pap_hdr_t *)d->ppp->chan_buf; - - if (d->ppp->chan_buf_sizelen)len)ppp->chan_buf_size-2) - { - log_warn("PAP: short packet received\n"); - return; - } - - if (hdr->code==PAP_REQ) pap_recv_req(d,hdr); - else - { - log_warn("PAP: unknown code received %x\n",hdr->code); - } -} - -static void __init auth_pap_init() -{ - ppp_auth_register_handler(&pap); -} - diff --git a/accel-pptpd/ctrl/CMakeLists.txt b/accel-pptpd/ctrl/CMakeLists.txt new file mode 100644 index 0000000..97f8e0d --- /dev/null +++ b/accel-pptpd/ctrl/CMakeLists.txt @@ -0,0 +1,4 @@ +INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}) + +ADD_LIBRARY(pptp SHARED pptp.c) + diff --git a/accel-pptpd/ctrl/pptp.c b/accel-pptpd/ctrl/pptp.c new file mode 100644 index 0000000..b503d85 --- /dev/null +++ b/accel-pptpd/ctrl/pptp.c @@ -0,0 +1,562 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "if_pppox.h" + +#include "list.h" +#include "pptp_prot.h" +#include "triton.h" +#include "log.h" +#include "ppp.h" + + +#define STATE_IDLE 0 +#define STATE_ESTB 1 +#define STATE_PPP 2 +#define STATE_FIN 3 +#define STATE_CLOSE 4 + +struct pptp_conn_t +{ + struct triton_ctx_t ctx; + struct triton_md_handler_t hnd; + struct triton_timer_t timeout_timer; + struct triton_timer_t echo_timer; + int state; + int echo_sent; + + uint8_t *in_buf; + int in_size; + uint8_t *out_buf; + int out_size; + int out_pos; + + struct ppp_ctrl_t ctrl; + struct ppp_t ppp; +}; + +static int conf_timeout = 3; +static int conf_echo_interval = 0; + +static int pptp_read(struct triton_md_handler_t *h); +static int pptp_write(struct triton_md_handler_t *h); +static void pptp_timeout(struct triton_timer_t *); +static void ppp_started(struct ppp_t *); +static void ppp_finished(struct ppp_t *); + +static void disconnect(struct pptp_conn_t *conn) +{ + triton_md_unregister_handler(&conn->hnd); + close(conn->hnd.fd); + + if (conn->timeout_timer.period) { + triton_timer_del(&conn->timeout_timer); + conn->timeout_timer.period = 0; + } + + if (conn->echo_timer.period) { + triton_timer_del(&conn->echo_timer); + conn->echo_timer.period = 0; + } + + if (conn->state == STATE_PPP) { + conn->state = STATE_CLOSE; + ppp_terminate(&conn->ppp, 1); + } + + triton_unregister_ctx(&conn->ctx); + + free(conn->in_buf); + free(conn->out_buf); + free(conn); +} + +static int post_msg(struct pptp_conn_t *conn, void *buf, int size) +{ + int n; + if (conn->out_size) { + log_debug("post_msg: buffer is not empty\n"); + return -1; + } + + n=write(conn->hnd.fd, buf, size); + if (n < 0) { + if (errno == EINTR || errno == EAGAIN) + n = 0; + else { + if (errno != EPIPE) + log_debug("post_msg: failed to write socket %i\n",errno); + return -1; + } + } + + if ( nout_buf, buf + n, size - n); + triton_md_enable_handler(&conn->hnd, MD_MODE_WRITE); + } + + return 0; +} + +static int send_pptp_stop_ctrl_conn_rqst(struct pptp_conn_t *conn, int reason, int err_code) +{ + struct pptp_stop_ctrl_conn msg = { + .header = PPTP_HEADER_CTRL(PPTP_STOP_CTRL_CONN_RQST), + .reason_result = hton8(reason), + .error_code = hton8(err_code), + }; + + return post_msg(conn, &msg, sizeof(msg)); +} + +static int send_pptp_stop_ctrl_conn_rply(struct pptp_conn_t *conn, int reason, int err_code) +{ + struct pptp_stop_ctrl_conn msg = { + .header = PPTP_HEADER_CTRL(PPTP_STOP_CTRL_CONN_RPLY), + .reason_result = hton8(reason), + .error_code = hton8(err_code), + }; + + return post_msg(conn, &msg, sizeof(msg)); +} +static int pptp_stop_ctrl_conn_rqst(struct pptp_conn_t *conn) +{ + struct pptp_stop_ctrl_conn *msg = (struct pptp_stop_ctrl_conn *)conn->in_buf; + log_info("PPTP_STOP_CTRL_CONN_RQST reason=%i error_code=%i\n",msg->reason_result, msg->error_code); + + if (conn->state == STATE_PPP) { + conn->state = STATE_FIN; + ppp_terminate(&conn->ppp, 0); + } + + send_pptp_stop_ctrl_conn_rply(conn, PPTP_CONN_STOP_OK, 0); + return -1; +} + +static int send_pptp_start_ctrl_conn_rply(struct pptp_conn_t *conn, int res_code, int err_code) +{ + struct pptp_start_ctrl_conn msg = { + .header = PPTP_HEADER_CTRL(PPTP_START_CTRL_CONN_RPLY), + .version = htons(PPTP_VERSION), + .result_code = res_code, + .error_code = err_code, + .framing_cap = htonl(PPTP_FRAME_SYNC), + .bearer_cap = htonl(0), + .max_channels = htons(1), + .firmware_rev = htons(PPTP_FIRMWARE_VERSION), + }; + + memset(msg.hostname, 0, sizeof(msg.hostname)); + strcpy((char*)msg.hostname, PPTP_HOSTNAME); + + memset(msg.vendor, 0, sizeof(msg.vendor)); + strcpy((char*)msg.vendor, PPTP_VENDOR); + + return post_msg(conn, &msg, sizeof(msg)); +} +static int pptp_start_ctrl_conn_rqst(struct pptp_conn_t *conn) +{ + struct pptp_start_ctrl_conn *msg = (struct pptp_start_ctrl_conn *)conn->in_buf; + + if (conn->state != STATE_IDLE) { + log_info("unexpected PPTP_START_CTRL_CONN_RQST\n"); + if (send_pptp_start_ctrl_conn_rply(conn, PPTP_CONN_RES_EXISTS, 0)) + return -1; + return 0; + } + + if (msg->version != htons(PPTP_VERSION)) { + log_info("PPTP version mismatch: expecting %x, received %s\n", PPTP_VERSION, msg->version); + if (send_pptp_start_ctrl_conn_rply(conn, PPTP_CONN_RES_PROTOCOL, 0)) + return -1; + return 0; + } + if (!(ntohl(msg->framing_cap) & PPTP_FRAME_SYNC)) { + log_info("connection does not supports sync mode\n"); + if (send_pptp_start_ctrl_conn_rply(conn, PPTP_CONN_RES_GE, 0)) + return -1; + return 0; + } + if (send_pptp_start_ctrl_conn_rply(conn, PPTP_CONN_RES_SUCCESS, 0)) + return -1; + + triton_timer_mod(&conn->timeout_timer, 0); + + conn->state = STATE_ESTB; + + return 0; +} + +static int send_pptp_out_call_rply(struct pptp_conn_t *conn, struct pptp_out_call_rqst *rqst, int call_id, int res_code, int err_code) +{ + struct pptp_out_call_rply msg = { + .header = PPTP_HEADER_CTRL(PPTP_OUT_CALL_RPLY), + .call_id = htons(call_id), + .call_id_peer = rqst->call_id, + .result_code = res_code, + .error_code = err_code, + .cause_code = 0, + .speed = rqst->bps_max, + .recv_size = rqst->recv_size, + .delay = 0, + .channel = 0, + }; + + return post_msg(conn, &msg, sizeof(msg)); +} + +static int pptp_out_call_rqst(struct pptp_conn_t *conn) +{ + struct pptp_out_call_rqst *msg = (struct pptp_out_call_rqst *)conn->in_buf; + struct sockaddr_pppox src_addr, dst_addr; + struct sockaddr_in addr; + socklen_t addrlen; + int pptp_sock; + + if (conn->state != STATE_ESTB) { + log_error("unexpected PPTP_OUT_CALL_RQST\n"); + if (send_pptp_out_call_rply(conn, msg, 0, PPTP_CALL_RES_GE, PPTP_GE_NOCONN)) + return -1; + return 0; + } + + src_addr.sa_family = AF_PPPOX; + src_addr.sa_protocol = PX_PROTO_PPTP; + src_addr.sa_addr.pptp.call_id = 0; + addrlen = sizeof(addr); + getsockname(conn->hnd.fd, (struct sockaddr*)&addr, &addrlen); + src_addr.sa_addr.pptp.sin_addr = addr.sin_addr; + + dst_addr.sa_family = AF_PPPOX; + dst_addr.sa_protocol = PX_PROTO_PPTP; + dst_addr.sa_addr.pptp.call_id = htons(msg->call_id); + addrlen = sizeof(addr); + getpeername(conn->hnd.fd, (struct sockaddr*)&addr, &addrlen); + dst_addr.sa_addr.pptp.sin_addr = addr.sin_addr; + + pptp_sock = socket(AF_PPPOX, SOCK_STREAM, PX_PROTO_PPTP); + if (pptp_sock < 0) { + log_error("failed to create PPTP socket (%s)\n", strerror(errno)); + return -1; + } + if (bind(pptp_sock, (struct sockaddr*)&src_addr, sizeof(src_addr))) { + log_error("failed to bind PPTP socket (%s)\n", strerror(errno)); + close(pptp_sock); + return -1; + } + addrlen = sizeof(src_addr); + getsockname(pptp_sock, (struct sockaddr*)&src_addr, &addrlen); + + if (connect(pptp_sock, (struct sockaddr*)&dst_addr, sizeof(dst_addr))) { + log_error("failed to connect PPTP socket (%s)\n", strerror(errno)); + close(pptp_sock); + return -1; + } + + if (send_pptp_out_call_rply(conn, msg, src_addr.sa_addr.pptp.call_id, PPTP_CALL_RES_OK, 0)) + return -1; + + conn->ppp.fd = pptp_sock; + conn->ppp.chan_name = strdup(inet_ntoa(dst_addr.sa_addr.pptp.sin_addr)); + conn->ppp.ctrl = &conn->ctrl; + conn->ctrl.ctx = &conn->ctx; + conn->ctrl.started = ppp_started; + conn->ctrl.finished = ppp_finished; + if (establish_ppp(&conn->ppp)) { + close(pptp_sock); + //if (send_pptp_stop_ctrl_conn_rqst(conn, 0, 0)) + conn->state = STATE_FIN; + return -1; + } + conn->state = STATE_PPP; + + triton_timer_del(&conn->timeout_timer); + conn->timeout_timer.period = 0; + + if (conf_echo_interval) { + conn->echo_timer.period = conf_echo_interval * 1000; + triton_timer_add(&conn->ctx, &conn->echo_timer, 0); + } + + return 0; +} + +static int pptp_echo_rqst(struct pptp_conn_t *conn) +{ + struct pptp_echo_rqst *in_msg = (struct pptp_echo_rqst *)conn->in_buf; + struct pptp_echo_rply out_msg = { + .header = PPTP_HEADER_CTRL(PPTP_ECHO_RQST), + .identifier = in_msg->identifier, + .result_code = 1, + }; + + return post_msg(conn, &out_msg, sizeof(out_msg)); +} + +static int pptp_echo_rply(struct pptp_conn_t *conn) +{ + struct pptp_echo_rply *msg = (struct pptp_echo_rply *)conn->in_buf; + if (msg->identifier != conn->echo_sent) { + log_error("pptp:echo: identifier mismatch\n"); + return -1; + } + conn->echo_sent = 0; + return 0; +} +static void pptp_send_echo(struct triton_timer_t *t) +{ + struct pptp_conn_t *conn = container_of(t, typeof(*conn), echo_timer); + struct pptp_echo_rqst msg = { + .header = PPTP_HEADER_CTRL(PPTP_ECHO_RQST), + }; + + if (conn->echo_sent) { + log_warn("pptp: no echo reply\n"); + disconnect(conn); + return; + } + + conn->echo_sent = random(); + msg.identifier = conn->echo_sent; + + if (post_msg(conn, &msg, sizeof(msg))) + disconnect(conn); +} + +static int process_packet(struct pptp_conn_t *conn) +{ + struct pptp_header *hdr = (struct pptp_header *)conn->in_buf; + switch(ntohs(hdr->ctrl_type)) + { + case PPTP_START_CTRL_CONN_RQST: + return pptp_start_ctrl_conn_rqst(conn); + case PPTP_STOP_CTRL_CONN_RQST: + return pptp_stop_ctrl_conn_rqst(conn); + case PPTP_OUT_CALL_RQST: + return pptp_out_call_rqst(conn); + case PPTP_ECHO_RQST: + return pptp_echo_rqst(conn); + case PPTP_ECHO_RPLY: + return pptp_echo_rply(conn); + } + return 0; +} + +static int pptp_read(struct triton_md_handler_t *h) +{ + struct pptp_conn_t *conn=container_of(h,typeof(*conn),hnd); + struct pptp_header *hdr=(struct pptp_header *)conn->in_buf; + int n; + + while(1) { + n = read(h->fd,conn->in_buf,PPTP_CTRL_SIZE_MAX-conn->in_size); + if (n < 0) { + if (errno == EINTR) + continue; + if (errno == EAGAIN) + return 0; + log_error("pptp: read: %s\n",strerror(errno)); + goto drop; + } + if (n == 0) + goto drop; + conn->in_size += n; + if (conn->in_size >= sizeof(*hdr)) { + if (hdr->magic != htonl(PPTP_MAGIC)) + goto drop; + if (ntohs(hdr->length) >= PPTP_CTRL_SIZE_MAX) + goto drop; + if (ntohs(hdr->length) > conn->in_size) + goto drop; + if (ntohs(hdr->length) == conn->in_size) { + if (ntohs(hdr->length) != PPTP_CTRL_SIZE(ntohs(hdr->ctrl_type))) + goto drop; + if (process_packet(conn)) + goto drop; + conn->in_size = 0; + } + } + } +drop: + disconnect(conn); + return 1; +} +static int pptp_write(struct triton_md_handler_t *h) +{ + struct pptp_conn_t *conn = container_of(h, typeof(*conn), hnd); + int n; + + while (1) { + n = write(h->fd, conn->out_buf+conn->out_pos, conn->out_size-conn->out_pos); + + if (n < 0) { + if (errno == EINTR) + continue; + if (errno == EAGAIN) + n = 0; + else { + if (errno != EPIPE) + log_error("pptp:post_msg: %s\n", strerror(errno)); + disconnect(conn); + return 1; + } + } + + conn->out_pos += n; + if (conn->out_pos == conn->out_size) { + conn->out_pos = 0; + conn->out_size = 0; + triton_md_disable_handler(h, MD_MODE_WRITE); + return 0; + } + } +} +static void pptp_timeout(struct triton_timer_t *t) +{ + struct pptp_conn_t *conn = container_of(t, typeof(*conn), timeout_timer); + disconnect(conn); +} +static void pptp_close(struct triton_ctx_t *ctx) +{ + struct pptp_conn_t *conn = container_of(ctx, typeof(*conn), ctx); + if (conn->state == STATE_PPP) { + conn->state = STATE_FIN; + ppp_terminate(&conn->ppp, 0); + } else + disconnect(conn); +} +static void ppp_started(struct ppp_t *ppp) +{ + log_msg("ppp_started\n"); +} +static void ppp_finished(struct ppp_t *ppp) +{ + struct pptp_conn_t *conn = container_of(ppp, typeof(*conn), ppp); + + log_msg("ppp_finished\n"); + close(conn->ppp.fd); + //send_pptp_stop_ctrl_conn_rqst(conn, 0, 0); + if (conn->state != STATE_CLOSE) { + conn->state = STATE_CLOSE; + disconnect(conn); + } +} + +//================================== + +struct pptp_serv_t +{ + struct triton_ctx_t ctx; + struct triton_md_handler_t hnd; +}; + +static int pptp_connect(struct triton_md_handler_t *h) +{ + struct sockaddr_in addr; + socklen_t size = sizeof(addr); + int sock; + struct pptp_conn_t *conn; + + while(1) { + sock = accept(h->fd, (struct sockaddr *)&addr, &size); + if (sock < 0) { + if (errno == EAGAIN) + return 0; + log_error("pptp: accept failed: %s\n", strerror(errno)); + continue; + } + + log_info("pptp: new connection from %s\n", inet_ntoa(addr.sin_addr)); + + if (fcntl(sock, F_SETFL, O_NONBLOCK)) { + log_error("pptp: failed to set nonblocking mode: %s, closing connection...\n", strerror(errno)); + close(sock); + continue; + } + + conn = malloc(sizeof(*conn)); + memset(conn, 0, sizeof(*conn)); + conn->hnd.fd = sock; + conn->hnd.read = pptp_read; + conn->hnd.write = pptp_write; + conn->ctx.close = pptp_close; + conn->in_buf = malloc(PPTP_CTRL_SIZE_MAX); + conn->out_buf = malloc(PPTP_CTRL_SIZE_MAX); + conn->timeout_timer.expire = pptp_timeout; + conn->timeout_timer.period = conf_timeout * 1000; + conn->echo_timer.expire = pptp_send_echo; + + triton_register_ctx(&conn->ctx); + triton_md_register_handler(&conn->ctx, &conn->hnd); + triton_md_enable_handler(&conn->hnd,MD_MODE_READ); + triton_timer_add(&conn->ctx, &conn->timeout_timer, 0); + } + return 0; +} +static void pptp_serv_close(struct triton_ctx_t *ctx) +{ + struct pptp_serv_t *s=container_of(ctx,typeof(*s),ctx); + triton_md_unregister_handler(&s->hnd); + close(s->hnd.fd); +} + +static struct pptp_serv_t serv= +{ + .hnd.read=pptp_connect, + .ctx.close=pptp_serv_close, +}; + +static void __init pptp_init(void) +{ + struct sockaddr_in addr; + char *opt; + + serv.hnd.fd = socket (PF_INET, SOCK_STREAM, 0); + if (serv.hnd.fd < 0) { + log_error("pptp: failed to create server socket: %s\n", strerror(errno)); + return; + } + addr.sin_family = AF_INET; + addr.sin_port = htons (PPTP_PORT); + addr.sin_addr.s_addr = htonl (INADDR_ANY); + if (bind (serv.hnd.fd, (struct sockaddr *) &addr, sizeof (addr)) < 0) { + perror("pptp: bind"); + log_error("pptp: failed to bind socket: %s\n", strerror(errno)); + close(serv.hnd.fd); + return; + } + + if (listen (serv.hnd.fd, 100) < 0) { + log_error("pptp: failed to listen socket: %s\n", strerror(errno)); + close(serv.hnd.fd); + return; + } + + if (fcntl(serv.hnd.fd, F_SETFL, O_NONBLOCK)) { + log_error("pptp: failed to set nonblocking mode: %s\n", strerror(errno)); + close(serv.hnd.fd); + return; + } + + triton_register_ctx(&serv.ctx); + triton_md_register_handler(&serv.ctx, &serv.hnd); + triton_md_enable_handler(&serv.hnd, MD_MODE_READ); + + opt = conf_get_opt("pptp", "timeout"); + if (opt && atoi(opt) > 0) + conf_timeout = atoi(opt); + + opt = conf_get_opt("pptp", "echo-interval"); + if (opt && atoi(opt) > 0) + conf_echo_interval = atoi(opt); +} + diff --git a/accel-pptpd/ctrl/pptp_prot.h b/accel-pptpd/ctrl/pptp_prot.h new file mode 100644 index 0000000..7e3ebdd --- /dev/null +++ b/accel-pptpd/ctrl/pptp_prot.h @@ -0,0 +1,297 @@ +#ifndef PPTP_PROT_H +#define PPTP_PROT_H + +#include + +#define hton8(x) (x) +#define ntoh8(x) (x) +#define hton16(x) htons(x) +#define ntoh16(x) ntohs(x) +#define hton32(x) htonl(x) +#define ntoh32(x) ntohl(x) + +/* PPTP magic numbers: ----------------------------------------- */ + +#define PPTP_MAGIC 0x1A2B3C4D /* Magic cookie for PPTP datagrams */ +#define PPTP_PORT 1723 /* PPTP TCP port number */ +#define PPTP_PROTO 47 /* PPTP IP protocol number */ + +/* PPTP result codes:---------------------------------------- */ +#define PPTP_CONN_RES_SUCCESS 1 +#define PPTP_CONN_RES_GE 2 +#define PPTP_CONN_RES_EXISTS 3 +#define PPTP_CONN_RES_AUTH 4 +#define PPTP_CONN_RES_PROTOCOL 5 + +#define PPTP_CONN_STOP_OK 1 +#define PPTP_CONN_STOP_GE 2 + +#define PPTP_CALL_RES_OK 1 +#define PPTP_CALL_RES_GE 2 + +#define PPTP_GE_NOCONN 1 + +/* Control Connection Message Types: --------------------------- */ + +#define PPTP_MESSAGE_CONTROL 1 +#define PPTP_MESSAGE_MANAGE 2 + +/* Control Message Types: -------------------------------------- */ + +/* (Control Connection Management) */ +#define PPTP_START_CTRL_CONN_RQST 1 +#define PPTP_START_CTRL_CONN_RPLY 2 +#define PPTP_STOP_CTRL_CONN_RQST 3 +#define PPTP_STOP_CTRL_CONN_RPLY 4 +#define PPTP_ECHO_RQST 5 +#define PPTP_ECHO_RPLY 6 + +/* (Call Management) */ +#define PPTP_OUT_CALL_RQST 7 +#define PPTP_OUT_CALL_RPLY 8 +#define PPTP_IN_CALL_RQST 9 +#define PPTP_IN_CALL_RPLY 10 +#define PPTP_IN_CALL_CONNECT 11 +#define PPTP_CALL_CLEAR_RQST 12 +#define PPTP_CALL_CLEAR_NTFY 13 + +/* (Error Reporting) */ +#define PPTP_WAN_ERR_NTFY 14 + +/* (PPP Session Control) */ +#define PPTP_SET_LINK_INFO 15 + +/* PPTP version information: --------------------------------------*/ +#define PPTP_VERSION_STRING "1.00" +#define PPTP_VERSION 0x100 +#define PPTP_FIRMWARE_STRING "0.01" +#define PPTP_FIRMWARE_VERSION 0x001 + +#define PPTP_HOSTNAME "local" +#define PPTP_VENDOR "cananian" + +/* PPTP capabilities: ---------------------------------------------*/ + +/* (Framing capabilities for msg sender) */ +#define PPTP_FRAME_ASYNC 1 +#define PPTP_FRAME_SYNC 2 +#define PPTP_FRAME_ANY 3 + +/* (Bearer capabilities for msg sender) */ +#define PPTP_BEARER_ANALOG 1 +#define PPTP_BEARER_DIGITAL 2 +#define PPTP_BEARER_ANY 3 + +#define PPTP_RESULT_GENERAL_ERROR 2 + +/* (Reasons to close a connection) */ +#define PPTP_STOP_NONE 1 /* no good reason */ +#define PPTP_STOP_PROTOCOL 2 /* can't support peer's protocol version */ +#define PPTP_STOP_LOCAL_SHUTDOWN 3 /* requester is being shut down */ + +/* PPTP datagram structures (all data in network byte order): ----------*/ + +struct pptp_header +{ + uint16_t length; /* message length in octets, including header */ + uint16_t pptp_type; /* PPTP message type. 1 for control message. */ + uint32_t magic; /* this should be PPTP_MAGIC. */ + uint16_t ctrl_type; /* Control message type (0-15) */ + uint16_t reserved0; /* reserved. MUST BE ZERO. */ +}__attribute__((packed)); + +struct pptp_start_ctrl_conn /* for control message types 1 and 2 */ +{ + struct pptp_header header; + + uint16_t version; /* PPTP protocol version. = PPTP_VERSION */ + uint8_t result_code; /* these two fields should be zero on rqst msg*/ + uint8_t error_code; /* 0 unless result_code==2 (General Error) */ + uint32_t framing_cap; /* Framing capabilities */ + uint32_t bearer_cap; /* Bearer Capabilities */ + uint16_t max_channels; /* Maximum Channels (=0 for PNS, PAC ignores) */ + uint16_t firmware_rev; /* Firmware or Software Revision */ + uint8_t hostname[64]; /* Host Name (64 octets, zero terminated) */ + uint8_t vendor[64]; /* Vendor string (64 octets, zero term.) */ +}__attribute__((packed)); + +struct pptp_stop_ctrl_conn /* for control message types 3 and 4 */ +{ + struct pptp_header header; + + uint8_t reason_result; /* reason for rqst, result for rply */ + uint8_t error_code; /* MUST be 0, unless rply result==2 (general err)*/ + uint16_t reserved1; /* MUST be 0 */ +}__attribute__((packed)); + +struct pptp_echo_rqst /* for control message type 5 */ +{ + struct pptp_header header; + uint32_t identifier; /* arbitrary value set by sender which is used */ + /* to match up reply and request */ +}__attribute__((packed)); + +struct pptp_echo_rply /* for control message type 6 */ +{ + struct pptp_header header; + uint32_t identifier; /* should correspond to id of rqst */ + uint8_t result_code; + uint8_t error_code; /* =0, unless result_code==2 (general error) */ + uint16_t reserved1; /* MUST BE ZERO */ +}__attribute__((packed)); + +struct pptp_out_call_rqst /* for control message type 7 */ +{ + struct pptp_header header; + uint16_t call_id; /* Call ID (unique id used to multiplex data) */ + uint16_t call_sernum; /* Call Serial Number (used for logging) */ + uint32_t bps_min; /* Minimum BPS (lowest acceptable line speed) */ + uint32_t bps_max; /* Maximum BPS (highest acceptable line speed) */ + uint32_t bearer; /* Bearer type */ + uint32_t framing; /* Framing type */ + uint16_t recv_size; /* Recv. Window Size (no. of buffered packets) */ + uint16_t delay; /* Packet Processing Delay (in 1/10 sec) */ + uint16_t phone_len; /* Phone Number Length (num. of valid digits) */ + uint16_t reserved1; /* MUST BE ZERO */ + uint8_t phone_num[64]; /* Phone Number (64 octets, null term.) */ + uint8_t subaddress[64]; /* Subaddress (64 octets, null term.) */ +}__attribute__((packed)); + +struct pptp_out_call_rply /* for control message type 8 */ +{ + struct pptp_header header; + uint16_t call_id; /* Call ID (used to multiplex data over tunnel)*/ + uint16_t call_id_peer; /* Peer's Call ID (call_id of pptp_out_call_rqst)*/ + uint8_t result_code; /* Result Code (1 is no errors) */ + uint8_t error_code; /* Error Code (=0 unless result_code==2) */ + uint16_t cause_code; /* Cause Code (addt'l failure information) */ + uint32_t speed; /* Connect Speed (in BPS) */ + uint16_t recv_size; /* Recv. Window Size (no. of buffered packets) */ + uint16_t delay; /* Packet Processing Delay (in 1/10 sec) */ + uint32_t channel; /* Physical Channel ID (for logging) */ +}__attribute__((packed)); + +struct pptp_in_call_rqst /* for control message type 9 */ +{ + struct pptp_header header; + uint16_t call_id; /* Call ID (unique id used to multiplex data) */ + uint16_t call_sernum; /* Call Serial Number (used for logging) */ + uint32_t bearer; /* Bearer type */ + uint32_t channel; /* Physical Channel ID (for logging) */ + uint16_t dialed_len; /* Dialed Number Length (# of valid digits) */ + uint16_t dialing_len; /* Dialing Number Length (# of valid digits) */ + uint8_t dialed_num[64]; /* Dialed Number (64 octets, zero term.) */ + uint8_t dialing_num[64]; /* Dialing Number (64 octets, zero term.) */ + uint8_t subaddress[64]; /* Subaddress (64 octets, zero term.) */ +}__attribute__((packed)); + +struct pptp_in_call_rply /* for control message type 10 */ +{ + struct pptp_header header; + uint16_t call_id; /* Call ID (used to multiplex data over tunnel)*/ + uint16_t call_id_peer; /* Peer's Call ID (call_id of pptp_out_call_rqst)*/ + uint8_t result_code; /* Result Code (1 is no errors) */ + uint8_t error_code; /* Error Code (=0 unless result_code==2) */ + uint16_t recv_size; /* Recv. Window Size (no. of buffered packets) */ + uint16_t delay; /* Packet Processing Delay (in 1/10 sec) */ + uint16_t reserved1; /* MUST BE ZERO */ +}__attribute__((packed)); + +struct pptp_in_call_connect /* for control message type 11 */ +{ + struct pptp_header header; + uint16_t call_id_peer; /* Peer's Call ID (call_id of pptp_out_call_rqst)*/ + uint16_t reserved1; /* MUST BE ZERO */ + uint32_t speed; /* Connect Speed (in BPS) */ + uint16_t recv_size; /* Recv. Window Size (no. of buffered packets) */ + uint16_t delay; /* Packet Processing Delay (in 1/10 sec) */ + uint32_t framing; /* Framing type */ +}__attribute__((packed)); + +struct pptp_call_clear_rqst /* for control message type 12 */ +{ + struct pptp_header header; + uint16_t call_id; /* Call ID (used to multiplex data over tunnel)*/ + uint16_t reserved1; /* MUST BE ZERO */ +}__attribute__((packed)); + +struct pptp_call_clear_ntfy /* for control message type 13 */ +{ + struct pptp_header header; + uint16_t call_id; /* Call ID (used to multiplex data over tunnel)*/ + uint8_t result_code; /* Result Code */ + uint8_t error_code; /* Error Code (=0 unless result_code==2) */ + uint16_t cause_code; /* Cause Code (for ISDN, is Q.931 cause code) */ + uint16_t reserved1; /* MUST BE ZERO */ + uint8_t call_stats[128]; /* Call Statistics: 128 octets, ascii, 0-term */ +}__attribute__((packed)); + +struct pptp_wan_err_ntfy /* for control message type 14 */ +{ + struct pptp_header header; + uint16_t call_id_peer; /* Peer's Call ID (call_id of pptp_out_call_rqst)*/ + uint16_t reserved1; /* MUST BE ZERO */ + uint32_t crc_errors; /* CRC errors */ + uint32_t frame_errors; /* Framing errors */ + uint32_t hard_errors; /* Hardware overruns */ + uint32_t buff_errors; /* Buffer overruns */ + uint32_t time_errors; /* Time-out errors */ + uint32_t align_errors; /* Alignment errors */ +}__attribute__((packed)); + +struct pptp_set_link_info /* for control message type 15 */ +{ + struct pptp_header header; + uint16_t call_id_peer; /* Peer's Call ID (call_id of pptp_out_call_rqst) */ + uint16_t reserved1; /* MUST BE ZERO */ + uint32_t send_accm; /* Send ACCM (for PPP packets; default 0xFFFFFFFF)*/ + uint32_t recv_accm; /* Receive ACCM (for PPP pack.;default 0xFFFFFFFF)*/ +}__attribute__((packed)); + +/* helpful #defines: -------------------------------------------- */ +#define pptp_isvalid_ctrl(header, type, length) \ + (!( ( ntoh16(((struct pptp_header *)header)->length) < (length) ) || \ + ( ntoh16(((struct pptp_header *)header)->pptp_type) !=(type) ) || \ + ( ntoh32(((struct pptp_header *)header)->magic) !=PPTP_MAGIC) || \ + ( ntoh16(((struct pptp_header *)header)->ctrl_type) > PPTP_SET_LINK_INFO) || \ + ( ntoh16(((struct pptp_header *)header)->reserved0) !=0 ) )) + +#define PPTP_HEADER_CTRL(type) \ +{ hton16(PPTP_CTRL_SIZE(type)), \ + hton16(PPTP_MESSAGE_CONTROL), \ + hton32(PPTP_MAGIC), \ + hton16(type), 0 } + +#define PPTP_CTRL_SIZE(type) ( \ +(type==PPTP_START_CTRL_CONN_RQST)?sizeof(struct pptp_start_ctrl_conn): \ +(type==PPTP_START_CTRL_CONN_RPLY)?sizeof(struct pptp_start_ctrl_conn): \ +(type==PPTP_STOP_CTRL_CONN_RQST )?sizeof(struct pptp_stop_ctrl_conn): \ +(type==PPTP_STOP_CTRL_CONN_RPLY )?sizeof(struct pptp_stop_ctrl_conn): \ +(type==PPTP_ECHO_RQST )?sizeof(struct pptp_echo_rqst): \ +(type==PPTP_ECHO_RPLY )?sizeof(struct pptp_echo_rply): \ +(type==PPTP_OUT_CALL_RQST )?sizeof(struct pptp_out_call_rqst): \ +(type==PPTP_OUT_CALL_RPLY )?sizeof(struct pptp_out_call_rply): \ +(type==PPTP_IN_CALL_RQST )?sizeof(struct pptp_in_call_rqst): \ +(type==PPTP_IN_CALL_RPLY )?sizeof(struct pptp_in_call_rply): \ +(type==PPTP_IN_CALL_CONNECT )?sizeof(struct pptp_in_call_connect): \ +(type==PPTP_CALL_CLEAR_RQST )?sizeof(struct pptp_call_clear_rqst): \ +(type==PPTP_CALL_CLEAR_NTFY )?sizeof(struct pptp_call_clear_ntfy): \ +(type==PPTP_WAN_ERR_NTFY )?sizeof(struct pptp_wan_err_ntfy): \ +(type==PPTP_SET_LINK_INFO )?sizeof(struct pptp_set_link_info): \ +0) +#define max(a,b) (((a)>(b))?(a):(b)) +#define PPTP_CTRL_SIZE_MAX ( \ +max(sizeof(struct pptp_start_ctrl_conn), \ +max(sizeof(struct pptp_echo_rqst), \ +max(sizeof(struct pptp_echo_rply), \ +max(sizeof(struct pptp_out_call_rqst), \ +max(sizeof(struct pptp_out_call_rply), \ +max(sizeof(struct pptp_in_call_rqst), \ +max(sizeof(struct pptp_in_call_rply), \ +max(sizeof(struct pptp_in_call_connect), \ +max(sizeof(struct pptp_call_clear_rqst), \ +max(sizeof(struct pptp_call_clear_ntfy), \ +max(sizeof(struct pptp_wan_err_ntfy), \ +max(sizeof(struct pptp_set_link_info), 0))))))))))))) + +#endif diff --git a/accel-pptpd/if_pppox.h b/accel-pptpd/if_pppox.h deleted file mode 100644 index da327a1..0000000 --- a/accel-pptpd/if_pppox.h +++ /dev/null @@ -1,227 +0,0 @@ -/*************************************************************************** - * Linux PPP over X - Generic PPP transport layer sockets - * Linux PPP over Ethernet (PPPoE) Socket Implementation (RFC 2516) - * - * This file supplies definitions required by the PPP over Ethernet driver - * (pppox.c). All version information wrt this file is located in pppox.c - * - * License: - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version - * 2 of the License, or (at your option) any later version. - * - */ - -#ifndef __LINUX_IF_PPPOX_H -#define __LINUX_IF_PPPOX_H - - -#include -#include -#include - -#ifdef __KERNEL__ -#include -#include -#include -#include -#include -#endif /* __KERNEL__ */ - -/* For user-space programs to pick up these definitions - * which they wouldn't get otherwise without defining __KERNEL__ - */ -#ifndef AF_PPPOX -#define AF_PPPOX 24 -#define PF_PPPOX AF_PPPOX -#endif /* !(AF_PPPOX) */ - -struct pptp_addr{ - __u16 call_id; - struct in_addr sin_addr; -}; -/************************************************************************ - * Protocols supported by AF_PPPOX - */ -#define PX_PROTO_OE 0 /* Currently just PPPoE */ -#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,22) -#define PX_PROTO_PPTP 1 -#define PX_MAX_PROTO 2 -#else -#define PX_PROTO_PPTP 2 -#define PX_MAX_PROTO 3 -#endif - -struct sockaddr_pppox { - sa_family_t sa_family; /* address family, AF_PPPOX */ - unsigned int sa_protocol; /* protocol identifier */ - union{ - struct pptp_addr pptp; - }sa_addr; -}__attribute__ ((packed)); - - -/********************************************************************* - * - * ioctl interface for defining forwarding of connections - * - ********************************************************************/ - -#define PPPOEIOCSFWD _IOW(0xB1 ,0, size_t) -#define PPPOEIOCDFWD _IO(0xB1 ,1) -/*#define PPPOEIOCGFWD _IOWR(0xB1,2, size_t)*/ - -/* Codes to identify message types */ -#define PADI_CODE 0x09 -#define PADO_CODE 0x07 -#define PADR_CODE 0x19 -#define PADS_CODE 0x65 -#define PADT_CODE 0xa7 -struct pppoe_tag { - __u16 tag_type; - __u16 tag_len; - char tag_data[0]; -} __attribute ((packed)); - -/* Tag identifiers */ -#define PTT_EOL __constant_htons(0x0000) -#define PTT_SRV_NAME __constant_htons(0x0101) -#define PTT_AC_NAME __constant_htons(0x0102) -#define PTT_HOST_UNIQ __constant_htons(0x0103) -#define PTT_AC_COOKIE __constant_htons(0x0104) -#define PTT_VENDOR __constant_htons(0x0105) -#define PTT_RELAY_SID __constant_htons(0x0110) -#define PTT_SRV_ERR __constant_htons(0x0201) -#define PTT_SYS_ERR __constant_htons(0x0202) -#define PTT_GEN_ERR __constant_htons(0x0203) - -struct pppoe_hdr { -#if defined(__LITTLE_ENDIAN_BITFIELD) - __u8 ver : 4; - __u8 type : 4; -#elif defined(__BIG_ENDIAN_BITFIELD) - __u8 type : 4; - __u8 ver : 4; -#else -#error "Please fix " -#endif - __u8 code; - __u16 sid; - __u16 length; - struct pppoe_tag tag[0]; -} __attribute__ ((packed)); - - -/* Socket options */ -#define PPTP_SO_TIMEOUT 1 - - -#ifdef __KERNEL__ -#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0) -struct pppoe_opt { - struct net_device *dev; /* device associated with socket*/ - struct pppoe_addr pa; /* what this socket is bound to*/ - struct sockaddr_pppox relay; /* what socket data will be - relayed to (PPPoE relaying) */ -}; -#endif -struct pptp_opt { - struct pptp_addr src_addr; - struct pptp_addr dst_addr; - int timeout; - __u32 ack_sent, ack_recv; - __u32 seq_sent, seq_recv; - int ppp_flags; - int flags; - struct sk_buff_head skb_buf; - #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0) - struct tq_struct buf_work; //check bufferd packets work - struct timer_list buf_timer; - #else - #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20) - struct delayed_work buf_work; //check bufferd packets work - #else - struct work_struct buf_work; //check bufferd packets work - #endif - #endif - struct gre_statistics *stat; - spinlock_t xmit_lock; - spinlock_t rcv_lock; -}; -#define PPTP_FLAG_PAUSE 0 -#define PPTP_FLAG_PROC 1 - -#include - -struct pppox_sock { - /* struct sock must be the first member of pppox_sock */ - #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0) - struct ppp_channel chan; - struct sock *sk; - #else - struct sock sk; - struct ppp_channel chan; - #endif - struct pppox_sock *next; /* for hash table */ - union { - struct pppoe_opt pppoe; - struct pptp_opt pptp; - } proto; - unsigned short num; -}; -#define pppoe_dev proto.pppoe.dev -#define pppoe_pa proto.pppoe.pa -#define pppoe_relay proto.pppoe.relay - -static inline struct pppox_sock *pppox_sk(struct sock *sk) -{ - #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0) - return (struct pppox_sock *)sk->protinfo.pppox; - #else - return (struct pppox_sock *)sk; - #endif -} - -static inline struct sock *sk_pppox(struct pppox_sock *po) -{ - #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0) - return po->sk; - #else - return (struct sock *)po; - #endif -} - -struct module; - -struct pppox_proto { - #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24) - int (*create)(struct socket *sock); - #else - int (*create)(struct net *net, struct socket *sock); - #endif - int (*ioctl)(struct socket *sock, unsigned int cmd, - unsigned long arg); - #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,15) - struct module *owner; - #endif -}; - -extern int register_pppox_proto(int proto_num, struct pppox_proto *pp); -extern void unregister_pppox_proto(int proto_num); -extern void pppox_unbind_sock(struct sock *sk);/* delete ppp-channel binding */ -extern int pppox_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg); - -/* PPPoX socket states */ -enum { - PPPOX_NONE = 0, /* initial state */ - PPPOX_CONNECTED = 1, /* connection established ==TCP_ESTABLISHED */ - PPPOX_BOUND = 2, /* bound to ppp device */ - PPPOX_RELAY = 4, /* forwarding is enabled */ - PPPOX_ZOMBIE = 8, /* dead, but still bound to ppp device */ - PPPOX_DEAD = 16 /* dead, useless, please clean me up!*/ -}; - -#endif /* __KERNEL__ */ - -#endif /* !(__LINUX_IF_PPPOX_H) */ diff --git a/accel-pptpd/include/if_pppox.h b/accel-pptpd/include/if_pppox.h new file mode 100644 index 0000000..da327a1 --- /dev/null +++ b/accel-pptpd/include/if_pppox.h @@ -0,0 +1,227 @@ +/*************************************************************************** + * Linux PPP over X - Generic PPP transport layer sockets + * Linux PPP over Ethernet (PPPoE) Socket Implementation (RFC 2516) + * + * This file supplies definitions required by the PPP over Ethernet driver + * (pppox.c). All version information wrt this file is located in pppox.c + * + * License: + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version + * 2 of the License, or (at your option) any later version. + * + */ + +#ifndef __LINUX_IF_PPPOX_H +#define __LINUX_IF_PPPOX_H + + +#include +#include +#include + +#ifdef __KERNEL__ +#include +#include +#include +#include +#include +#endif /* __KERNEL__ */ + +/* For user-space programs to pick up these definitions + * which they wouldn't get otherwise without defining __KERNEL__ + */ +#ifndef AF_PPPOX +#define AF_PPPOX 24 +#define PF_PPPOX AF_PPPOX +#endif /* !(AF_PPPOX) */ + +struct pptp_addr{ + __u16 call_id; + struct in_addr sin_addr; +}; +/************************************************************************ + * Protocols supported by AF_PPPOX + */ +#define PX_PROTO_OE 0 /* Currently just PPPoE */ +#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,22) +#define PX_PROTO_PPTP 1 +#define PX_MAX_PROTO 2 +#else +#define PX_PROTO_PPTP 2 +#define PX_MAX_PROTO 3 +#endif + +struct sockaddr_pppox { + sa_family_t sa_family; /* address family, AF_PPPOX */ + unsigned int sa_protocol; /* protocol identifier */ + union{ + struct pptp_addr pptp; + }sa_addr; +}__attribute__ ((packed)); + + +/********************************************************************* + * + * ioctl interface for defining forwarding of connections + * + ********************************************************************/ + +#define PPPOEIOCSFWD _IOW(0xB1 ,0, size_t) +#define PPPOEIOCDFWD _IO(0xB1 ,1) +/*#define PPPOEIOCGFWD _IOWR(0xB1,2, size_t)*/ + +/* Codes to identify message types */ +#define PADI_CODE 0x09 +#define PADO_CODE 0x07 +#define PADR_CODE 0x19 +#define PADS_CODE 0x65 +#define PADT_CODE 0xa7 +struct pppoe_tag { + __u16 tag_type; + __u16 tag_len; + char tag_data[0]; +} __attribute ((packed)); + +/* Tag identifiers */ +#define PTT_EOL __constant_htons(0x0000) +#define PTT_SRV_NAME __constant_htons(0x0101) +#define PTT_AC_NAME __constant_htons(0x0102) +#define PTT_HOST_UNIQ __constant_htons(0x0103) +#define PTT_AC_COOKIE __constant_htons(0x0104) +#define PTT_VENDOR __constant_htons(0x0105) +#define PTT_RELAY_SID __constant_htons(0x0110) +#define PTT_SRV_ERR __constant_htons(0x0201) +#define PTT_SYS_ERR __constant_htons(0x0202) +#define PTT_GEN_ERR __constant_htons(0x0203) + +struct pppoe_hdr { +#if defined(__LITTLE_ENDIAN_BITFIELD) + __u8 ver : 4; + __u8 type : 4; +#elif defined(__BIG_ENDIAN_BITFIELD) + __u8 type : 4; + __u8 ver : 4; +#else +#error "Please fix " +#endif + __u8 code; + __u16 sid; + __u16 length; + struct pppoe_tag tag[0]; +} __attribute__ ((packed)); + + +/* Socket options */ +#define PPTP_SO_TIMEOUT 1 + + +#ifdef __KERNEL__ +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0) +struct pppoe_opt { + struct net_device *dev; /* device associated with socket*/ + struct pppoe_addr pa; /* what this socket is bound to*/ + struct sockaddr_pppox relay; /* what socket data will be + relayed to (PPPoE relaying) */ +}; +#endif +struct pptp_opt { + struct pptp_addr src_addr; + struct pptp_addr dst_addr; + int timeout; + __u32 ack_sent, ack_recv; + __u32 seq_sent, seq_recv; + int ppp_flags; + int flags; + struct sk_buff_head skb_buf; + #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0) + struct tq_struct buf_work; //check bufferd packets work + struct timer_list buf_timer; + #else + #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20) + struct delayed_work buf_work; //check bufferd packets work + #else + struct work_struct buf_work; //check bufferd packets work + #endif + #endif + struct gre_statistics *stat; + spinlock_t xmit_lock; + spinlock_t rcv_lock; +}; +#define PPTP_FLAG_PAUSE 0 +#define PPTP_FLAG_PROC 1 + +#include + +struct pppox_sock { + /* struct sock must be the first member of pppox_sock */ + #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0) + struct ppp_channel chan; + struct sock *sk; + #else + struct sock sk; + struct ppp_channel chan; + #endif + struct pppox_sock *next; /* for hash table */ + union { + struct pppoe_opt pppoe; + struct pptp_opt pptp; + } proto; + unsigned short num; +}; +#define pppoe_dev proto.pppoe.dev +#define pppoe_pa proto.pppoe.pa +#define pppoe_relay proto.pppoe.relay + +static inline struct pppox_sock *pppox_sk(struct sock *sk) +{ + #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0) + return (struct pppox_sock *)sk->protinfo.pppox; + #else + return (struct pppox_sock *)sk; + #endif +} + +static inline struct sock *sk_pppox(struct pppox_sock *po) +{ + #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0) + return po->sk; + #else + return (struct sock *)po; + #endif +} + +struct module; + +struct pppox_proto { + #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24) + int (*create)(struct socket *sock); + #else + int (*create)(struct net *net, struct socket *sock); + #endif + int (*ioctl)(struct socket *sock, unsigned int cmd, + unsigned long arg); + #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,15) + struct module *owner; + #endif +}; + +extern int register_pppox_proto(int proto_num, struct pppox_proto *pp); +extern void unregister_pppox_proto(int proto_num); +extern void pppox_unbind_sock(struct sock *sk);/* delete ppp-channel binding */ +extern int pppox_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg); + +/* PPPoX socket states */ +enum { + PPPOX_NONE = 0, /* initial state */ + PPPOX_CONNECTED = 1, /* connection established ==TCP_ESTABLISHED */ + PPPOX_BOUND = 2, /* bound to ppp device */ + PPPOX_RELAY = 4, /* forwarding is enabled */ + PPPOX_ZOMBIE = 8, /* dead, but still bound to ppp device */ + PPPOX_DEAD = 16 /* dead, useless, please clean me up!*/ +}; + +#endif /* __KERNEL__ */ + +#endif /* !(__LINUX_IF_PPPOX_H) */ diff --git a/accel-pptpd/include/ipdb.h b/accel-pptpd/include/ipdb.h new file mode 120000 index 0000000..77ab85b --- /dev/null +++ b/accel-pptpd/include/ipdb.h @@ -0,0 +1 @@ +../ipdb.h \ No newline at end of file diff --git a/accel-pptpd/include/list.h b/accel-pptpd/include/list.h new file mode 120000 index 0000000..4b6d9a9 --- /dev/null +++ b/accel-pptpd/include/list.h @@ -0,0 +1 @@ +../triton/list.h \ No newline at end of file diff --git a/accel-pptpd/include/log.h b/accel-pptpd/include/log.h new file mode 120000 index 0000000..49a04dd --- /dev/null +++ b/accel-pptpd/include/log.h @@ -0,0 +1 @@ +../log.h \ No newline at end of file diff --git a/accel-pptpd/include/ppp.h b/accel-pptpd/include/ppp.h new file mode 120000 index 0000000..ae3fa2e --- /dev/null +++ b/accel-pptpd/include/ppp.h @@ -0,0 +1 @@ +../ppp/ppp.h \ No newline at end of file diff --git a/accel-pptpd/include/ppp_auth.h b/accel-pptpd/include/ppp_auth.h new file mode 120000 index 0000000..527c853 --- /dev/null +++ b/accel-pptpd/include/ppp_auth.h @@ -0,0 +1 @@ +../ppp/ppp_auth.h \ No newline at end of file diff --git a/accel-pptpd/include/ppp_fsm.h b/accel-pptpd/include/ppp_fsm.h new file mode 120000 index 0000000..5c6b7f4 --- /dev/null +++ b/accel-pptpd/include/ppp_fsm.h @@ -0,0 +1 @@ +../ppp/ppp_fsm.h \ No newline at end of file diff --git a/accel-pptpd/include/ppp_lcp.h b/accel-pptpd/include/ppp_lcp.h new file mode 120000 index 0000000..4902752 --- /dev/null +++ b/accel-pptpd/include/ppp_lcp.h @@ -0,0 +1 @@ +../ppp/ppp_lcp.h \ No newline at end of file diff --git a/accel-pptpd/include/pwdb.h b/accel-pptpd/include/pwdb.h new file mode 120000 index 0000000..360efe4 --- /dev/null +++ b/accel-pptpd/include/pwdb.h @@ -0,0 +1 @@ +../pwdb.h \ No newline at end of file diff --git a/accel-pptpd/include/triton.h b/accel-pptpd/include/triton.h new file mode 120000 index 0000000..6f35a2f --- /dev/null +++ b/accel-pptpd/include/triton.h @@ -0,0 +1 @@ +../triton/triton.h \ No newline at end of file diff --git a/accel-pptpd/ipcp_opt_dns.c b/accel-pptpd/ipcp_opt_dns.c deleted file mode 100644 index b741798..0000000 --- a/accel-pptpd/ipcp_opt_dns.c +++ /dev/null @@ -1,146 +0,0 @@ -#include -#include -#include - -#include "ppp.h" -#include "ppp_ipcp.h" -#include "log.h" -#include "ipdb.h" - -static struct ipcp_option_t *dns1_init(struct ppp_ipcp_t *ipcp); -static struct ipcp_option_t *dns2_init(struct ppp_ipcp_t *ipcp); -static void dns_free(struct ppp_ipcp_t *ipcp, struct ipcp_option_t *opt); -static int dns_send_conf_req(struct ppp_ipcp_t *ipcp, struct ipcp_option_t *opt, uint8_t *ptr); -static int dns_send_conf_nak(struct ppp_ipcp_t *ipcp, struct ipcp_option_t *opt, uint8_t *ptr); -static int dns_recv_conf_req(struct ppp_ipcp_t *ipcp, struct ipcp_option_t *opt, uint8_t *ptr); -static void dns1_print(void (*print)(const char *fmt,...),struct ipcp_option_t*, uint8_t *ptr); -static void dns2_print(void (*print)(const char *fmt,...),struct ipcp_option_t*, uint8_t *ptr); - -struct dns_option_t -{ - struct ipcp_option_t opt; - in_addr_t addr; -}; - -static struct ipcp_option_handler_t dns1_opt_hnd= -{ - .init=dns1_init, - .send_conf_req=dns_send_conf_req, - .send_conf_nak=dns_send_conf_nak, - .recv_conf_req=dns_recv_conf_req, - .free=dns_free, - .print=dns1_print, -}; -static struct ipcp_option_handler_t dns2_opt_hnd= -{ - .init=dns2_init, - .send_conf_req=dns_send_conf_req, - .send_conf_nak=dns_send_conf_nak, - .recv_conf_req=dns_recv_conf_req, - .free=dns_free, - .print=dns2_print, -}; - -static struct ipcp_option_t *dns1_init(struct ppp_ipcp_t *ipcp) -{ - struct dns_option_t *dns_opt=malloc(sizeof(*dns_opt)); - memset(dns_opt,0,sizeof(*dns_opt)); - dns_opt->opt.id=CI_DNS1; - dns_opt->opt.len=6; - - return &dns_opt->opt; -} - -static struct ipcp_option_t *dns2_init(struct ppp_ipcp_t *ipcp) -{ - struct dns_option_t *dns_opt=malloc(sizeof(*dns_opt)); - memset(dns_opt,0,sizeof(*dns_opt)); - dns_opt->opt.id=CI_DNS2; - dns_opt->opt.len=6; - - return &dns_opt->opt; -} - -static void dns_free(struct ppp_ipcp_t *ipcp, struct ipcp_option_t *opt) -{ - struct dns_option_t *dns_opt=container_of(opt,typeof(*dns_opt),opt); - - free(dns_opt); -} - -static int dns_send_conf_req(struct ppp_ipcp_t *ipcp, struct ipcp_option_t *opt, uint8_t *ptr) -{ - struct dns_option_t *dns_opt=container_of(opt,typeof(*dns_opt),opt); - struct ipcp_opt32_t *opt32=(struct ipcp_opt32_t*)ptr; - - if (!dns_opt->addr) - return 0; - opt32->hdr.id=dns_opt->opt.id; - opt32->hdr.len=6; - opt32->val=dns_opt->addr; - return 6; -} - -static int dns_send_conf_nak(struct ppp_ipcp_t *ipcp, struct ipcp_option_t *opt, uint8_t *ptr) -{ - struct dns_option_t *dns_opt=container_of(opt,typeof(*dns_opt),opt); - struct ipcp_opt32_t *opt32=(struct ipcp_opt32_t*)ptr; - opt32->hdr.id=dns_opt->opt.id; - opt32->hdr.len=6; - opt32->val=dns_opt->addr; - return 6; -} - -static int dns_recv_conf_req(struct ppp_ipcp_t *ipcp, struct ipcp_option_t *opt, uint8_t *ptr) -{ - struct dns_option_t *dns_opt=container_of(opt,typeof(*dns_opt),opt); - struct ipcp_opt32_t *opt32=(struct ipcp_opt32_t*)ptr; - - if (!dns_opt->addr) - { - if (dns_opt->opt.id==CI_DNS1) dns_opt->addr=inet_addr("10.0.0.1"); - else dns_opt->addr=inet_addr("10.0.0.2"); - - if (!dns_opt->addr) - { - dns_opt->addr=opt32->val; - return IPCP_OPT_ACK; - } - } - - if (dns_opt->addr==opt32->val) - return IPCP_OPT_ACK; - - return IPCP_OPT_NAK; -} - -static void dns1_print(void (*print)(const char *fmt,...),struct ipcp_option_t *opt, uint8_t *ptr) -{ - struct dns_option_t *dns_opt=container_of(opt,typeof(*dns_opt),opt); - struct ipcp_opt32_t *opt32=(struct ipcp_opt32_t*)ptr; - struct in_addr in; - - if (ptr) in.s_addr=opt32->val; - else in.s_addr=dns_opt->addr; - - print("",inet_ntoa(in)); -} - -static void dns2_print(void (*print)(const char *fmt,...),struct ipcp_option_t *opt, uint8_t *ptr) -{ - struct dns_option_t *dns_opt=container_of(opt,typeof(*dns_opt),opt); - struct ipcp_opt32_t *opt32=(struct ipcp_opt32_t*)ptr; - struct in_addr in; - - if (ptr) in.s_addr=opt32->val; - else in.s_addr=dns_opt->addr; - - print("",inet_ntoa(in)); -} - -static void __init dns_opt_init() -{ - ipcp_option_register(&dns1_opt_hnd); - ipcp_option_register(&dns2_opt_hnd); -} - diff --git a/accel-pptpd/ipcp_opt_ipaddr.c b/accel-pptpd/ipcp_opt_ipaddr.c deleted file mode 100644 index 2fa9251..0000000 --- a/accel-pptpd/ipcp_opt_ipaddr.c +++ /dev/null @@ -1,147 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include - -#include "ppp.h" -#include "ppp_ipcp.h" -#include "log.h" -#include "ipdb.h" - -static struct ipcp_option_t *ipaddr_init(struct ppp_ipcp_t *ipcp); -static void ipaddr_free(struct ppp_ipcp_t *ipcp, struct ipcp_option_t *opt); -static int ipaddr_send_conf_req(struct ppp_ipcp_t *ipcp, struct ipcp_option_t *opt, uint8_t *ptr); -static int ipaddr_send_conf_nak(struct ppp_ipcp_t *ipcp, struct ipcp_option_t *opt, uint8_t *ptr); -static int ipaddr_recv_conf_req(struct ppp_ipcp_t *ipcp, struct ipcp_option_t *opt, uint8_t *ptr); -//static int ipaddr_recv_conf_ack(struct ppp_ipcp_t *ipcp, struct ipcp_option_t *opt, uint8_t *ptr); -static void ipaddr_print(void (*print)(const char *fmt,...),struct ipcp_option_t*, uint8_t *ptr); - -struct ipaddr_option_t -{ - struct ipcp_option_t opt; - in_addr_t addr; - in_addr_t peer_addr; -}; - -static struct ipcp_option_handler_t ipaddr_opt_hnd= -{ - .init=ipaddr_init, - .send_conf_req=ipaddr_send_conf_req, - .send_conf_nak=ipaddr_send_conf_nak, - .recv_conf_req=ipaddr_recv_conf_req, - .free=ipaddr_free, - .print=ipaddr_print, -}; - -static struct ipcp_option_t *ipaddr_init(struct ppp_ipcp_t *ipcp) -{ - struct ipaddr_option_t *ipaddr_opt=malloc(sizeof(*ipaddr_opt)); - memset(ipaddr_opt,0,sizeof(*ipaddr_opt)); - ipdb_get(&ipaddr_opt->addr,&ipaddr_opt->peer_addr); - ipaddr_opt->opt.id=CI_ADDR; - ipaddr_opt->opt.len=6; - - return &ipaddr_opt->opt; -} - -static void ipaddr_free(struct ppp_ipcp_t *ipcp, struct ipcp_option_t *opt) -{ - struct ipaddr_option_t *ipaddr_opt=container_of(opt,typeof(*ipaddr_opt),opt); - - free(ipaddr_opt); -} - -static int ipaddr_send_conf_req(struct ppp_ipcp_t *ipcp, struct ipcp_option_t *opt, uint8_t *ptr) -{ - struct ipaddr_option_t *ipaddr_opt=container_of(opt,typeof(*ipaddr_opt),opt); - struct ipcp_opt32_t *opt32=(struct ipcp_opt32_t*)ptr; - opt32->hdr.id=CI_ADDR; - opt32->hdr.len=6; - opt32->val=ipaddr_opt->addr; - return 6; -} - -static int ipaddr_send_conf_nak(struct ppp_ipcp_t *ipcp, struct ipcp_option_t *opt, uint8_t *ptr) -{ - struct ipaddr_option_t *ipaddr_opt=container_of(opt,typeof(*ipaddr_opt),opt); - struct ipcp_opt32_t *opt32=(struct ipcp_opt32_t*)ptr; - opt32->hdr.id=CI_ADDR; - opt32->hdr.len=6; - opt32->val=ipaddr_opt->peer_addr; - return 6; -} - -static int ipaddr_recv_conf_req(struct ppp_ipcp_t *ipcp, struct ipcp_option_t *opt, uint8_t *ptr) -{ - struct ipaddr_option_t *ipaddr_opt = container_of(opt,typeof(*ipaddr_opt), opt); - struct ipcp_opt32_t *opt32 = (struct ipcp_opt32_t*)ptr; - struct ifreq ifr; - struct sockaddr_in addr; - struct npioctl np; - - if (ipaddr_opt->peer_addr == opt32->val) - goto ack; - - if (!ipaddr_opt->peer_addr) { - ipaddr_opt->peer_addr = opt32->val; - goto ack; - } - - return IPCP_OPT_NAK; - -ack: - memset(&ifr, 0, sizeof(ifr)); - memset(&addr, 0, sizeof(addr)); - - sprintf(ifr.ifr_name,"ppp%i",ipcp->ppp->unit_idx); - - addr.sin_family = AF_INET; - addr.sin_addr.s_addr = ipaddr_opt->addr; - memcpy(&ifr.ifr_addr,&addr,sizeof(addr)); - - if (ioctl(sock_fd, SIOCSIFADDR, &ifr)) - log_error("\nipcp: failed to set PA address: %s\n", strerror(errno)); - - addr.sin_addr.s_addr = ipaddr_opt->peer_addr; - memcpy(&ifr.ifr_dstaddr,&addr,sizeof(addr)); - - if (ioctl(sock_fd, SIOCSIFDSTADDR, &ifr)) - log_error("\nipcp: failed to set remote PA address: %s\n", strerror(errno)); - - if (ioctl(sock_fd, SIOCGIFFLAGS, &ifr)) - log_error("\nipcp: failed to get interface flags: %s\n", strerror(errno)); - - ifr.ifr_flags |= IFF_UP | IFF_POINTOPOINT; - - if (ioctl(sock_fd, SIOCSIFFLAGS, &ifr)) - log_error("\nipcp: failed to set interface flags: %s\n", strerror(errno)); - - np.protocol = PPP_IP; - np.mode = NPMODE_PASS; - - if (ioctl(ipcp->ppp->unit_fd, PPPIOCSNPMODE, &np)) - log_error("\nipcp: failed to set NP mode: %s\n", strerror(errno)); - - return IPCP_OPT_ACK; -} - -static void ipaddr_print(void (*print)(const char *fmt,...),struct ipcp_option_t *opt, uint8_t *ptr) -{ - struct ipaddr_option_t *ipaddr_opt=container_of(opt,typeof(*ipaddr_opt),opt); - struct ipcp_opt32_t *opt32=(struct ipcp_opt32_t*)ptr; - struct in_addr in; - - if (ptr) in.s_addr=opt32->val; - else in.s_addr=ipaddr_opt->addr; - - print("",inet_ntoa(in)); -} - -static void __init ipaddr_opt_init() -{ - ipcp_option_register(&ipaddr_opt_hnd); -} - diff --git a/accel-pptpd/ipdb.c b/accel-pptpd/ipdb.c index fb13971..8ac86cb 100644 --- a/accel-pptpd/ipdb.c +++ b/accel-pptpd/ipdb.c @@ -1,6 +1,7 @@ +#include "triton.h" #include "ipdb.h" -int ipdb_get(in_addr_t *addr, in_addr_t *peer_addr) +int __export ipdb_get(in_addr_t *addr, in_addr_t *peer_addr) { *addr=inet_addr("192.168.200.100"); *peer_addr=inet_addr("192.168.200.200"); diff --git a/accel-pptpd/lcp_opt_accomp.c b/accel-pptpd/lcp_opt_accomp.c deleted file mode 100644 index c4c221c..0000000 --- a/accel-pptpd/lcp_opt_accomp.c +++ /dev/null @@ -1,92 +0,0 @@ -#include -#include -#include - -#include "ppp.h" -#include "ppp_lcp.h" -#include "log.h" - -static struct lcp_option_t *accomp_init(struct ppp_lcp_t *lcp); -static void accomp_free(struct ppp_lcp_t *lcp, struct lcp_option_t *opt); -static int accomp_send_conf_req(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr); -static int accomp_send_conf_nak(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr); -static int accomp_recv_conf_req(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr); -static void accomp_print(void (*print)(const char *fmt,...),struct lcp_option_t*, uint8_t *ptr); - -struct accomp_option_t -{ - struct lcp_option_t opt; - int accomp; // 0 - disabled, 1 - enabled, 2 - allow,disabled, 3 - allow,enabled -}; - -static struct lcp_option_handler_t accomp_opt_hnd= -{ - .init=accomp_init, - .send_conf_req=accomp_send_conf_req, - .send_conf_nak=accomp_send_conf_nak, - .recv_conf_req=accomp_recv_conf_req, - .free=accomp_free, - .print=accomp_print, -}; - -static struct lcp_option_t *accomp_init(struct ppp_lcp_t *lcp) -{ - struct accomp_option_t *accomp_opt=malloc(sizeof(*accomp_opt)); - memset(accomp_opt,0,sizeof(*accomp_opt)); - accomp_opt->accomp=0; - accomp_opt->opt.id=CI_ACCOMP; - accomp_opt->opt.len=2; - - return &accomp_opt->opt; -} - -static void accomp_free(struct ppp_lcp_t *lcp, struct lcp_option_t *opt) -{ - struct accomp_option_t *accomp_opt=container_of(opt,typeof(*accomp_opt),opt); - - free(accomp_opt); -} - -static int accomp_send_conf_req(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr) -{ - struct accomp_option_t *accomp_opt=container_of(opt,typeof(*accomp_opt),opt); - struct lcp_opt_hdr_t *opt0=(struct lcp_opt_hdr_t*)ptr; - if (accomp_opt->accomp==1 || accomp_opt->accomp==3) - { - opt0->id=CI_ACCOMP; - opt0->len=2; - return 2; - } - return 0; -} - -static int accomp_send_conf_nak(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr) -{ - struct accomp_option_t *accomp_opt=container_of(opt,typeof(*accomp_opt),opt); - struct lcp_opt_hdr_t *opt0=(struct lcp_opt_hdr_t*)ptr; - opt0->id=CI_ACCOMP; - opt0->len=2; - return 2; -} - -static int accomp_recv_conf_req(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr) -{ - struct accomp_option_t *accomp_opt=container_of(opt,typeof(*accomp_opt),opt); - - if (accomp_opt->accomp>0) - { - accomp_opt->accomp=1; - return LCP_OPT_ACK; - }else return LCP_OPT_REJ; -} - -static void accomp_print(void (*print)(const char *fmt,...),struct lcp_option_t *opt, uint8_t *ptr) -{ - print(""); -} - -static void __init accomp_opt_init() -{ - lcp_option_register(&accomp_opt_hnd); -} - diff --git a/accel-pptpd/lcp_opt_magic.c b/accel-pptpd/lcp_opt_magic.c deleted file mode 100644 index dc94ac6..0000000 --- a/accel-pptpd/lcp_opt_magic.c +++ /dev/null @@ -1,83 +0,0 @@ -#include -#include -#include - -#include "ppp.h" -#include "ppp_lcp.h" -#include "log.h" - -static struct lcp_option_t *magic_init(struct ppp_lcp_t *lcp); -static void magic_free(struct ppp_lcp_t *lcp, struct lcp_option_t *opt); -static int magic_send_conf_req(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr); -static int magic_recv_conf_req(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr); -static void magic_print(void (*print)(const char *fmt,...),struct lcp_option_t*, uint8_t *ptr); - -struct magic_option_t -{ - struct lcp_option_t opt; - int magic; -}; - -static struct lcp_option_handler_t magic_opt_hnd= -{ - .init=magic_init, - .send_conf_req=magic_send_conf_req, - .recv_conf_req=magic_recv_conf_req, - .free=magic_free, - .print=magic_print, -}; - -static struct lcp_option_t *magic_init(struct ppp_lcp_t *lcp) -{ - struct magic_option_t *magic_opt=malloc(sizeof(*magic_opt)); - memset(magic_opt,0,sizeof(*magic_opt)); - magic_opt->magic=random(); - magic_opt->opt.id=CI_MAGIC; - magic_opt->opt.len=6; - - lcp->magic = magic_opt->magic; - - return &magic_opt->opt; -} - -static void magic_free(struct ppp_lcp_t *lcp, struct lcp_option_t *opt) -{ - struct magic_option_t *magic_opt=container_of(opt,typeof(*magic_opt),opt); - - free(magic_opt); -} - -static int magic_send_conf_req(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr) -{ - struct magic_option_t *magic_opt=container_of(opt,typeof(*magic_opt),opt); - struct lcp_opt32_t *opt32=(struct lcp_opt32_t*)ptr; - opt32->hdr.id=CI_MAGIC; - opt32->hdr.len=6; - opt32->val=htonl(magic_opt->magic); - return 6; -} - -static int magic_recv_conf_req(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr) -{ - struct magic_option_t *magic_opt=container_of(opt,typeof(*magic_opt),opt); - struct lcp_opt32_t *opt32=(struct lcp_opt32_t*)ptr; - - if (magic_opt->magic==ntohl(opt32->val)) - { - log_error("loop detected"); - return -1; - } - return LCP_OPT_ACK; -} - -static void magic_print(void (*print)(const char *fmt,...),struct lcp_option_t *opt, uint8_t *ptr) -{ - struct magic_option_t *magic_opt=container_of(opt,typeof(*magic_opt),opt); - - print("",magic_opt->magic); -} - -static void __init magic_opt_init() -{ - lcp_option_register(&magic_opt_hnd); -} diff --git a/accel-pptpd/lcp_opt_mru.c b/accel-pptpd/lcp_opt_mru.c deleted file mode 100644 index e1c13b4..0000000 --- a/accel-pptpd/lcp_opt_mru.c +++ /dev/null @@ -1,123 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include - -#include "ppp.h" -#include "ppp_lcp.h" -#include "log.h" - -#define MAX_MTU 1436 - -static struct lcp_option_t *mru_init(struct ppp_lcp_t *lcp); -static void mru_free(struct ppp_lcp_t *lcp, struct lcp_option_t *opt); -static int mru_send_conf_req(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr); -static int mru_send_conf_nak(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr); -static int mru_recv_conf_req(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr); -static int mru_recv_conf_ack(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr); -static void mru_print(void (*print)(const char *fmt,...),struct lcp_option_t*, uint8_t *ptr); - -struct mru_option_t -{ - struct lcp_option_t opt; - int mru; - int mtu; -}; - -static struct lcp_option_handler_t mru_opt_hnd= -{ - .init=mru_init, - .send_conf_req=mru_send_conf_req, - .send_conf_nak=mru_send_conf_nak, - .recv_conf_req=mru_recv_conf_req, - .recv_conf_ack=mru_recv_conf_ack, - .free=mru_free, - .print=mru_print, -}; - -static struct lcp_option_t *mru_init(struct ppp_lcp_t *lcp) -{ - struct mru_option_t *mru_opt=malloc(sizeof(*mru_opt)); - memset(mru_opt,0,sizeof(*mru_opt)); - mru_opt->mtu=0; - mru_opt->mru=MAX_MTU; - mru_opt->opt.id=CI_MRU; - mru_opt->opt.len=4; - - return &mru_opt->opt; -} - -static void mru_free(struct ppp_lcp_t *lcp, struct lcp_option_t *opt) -{ - struct mru_option_t *mru_opt=container_of(opt,typeof(*mru_opt),opt); - - free(mru_opt); -} - -static int mru_send_conf_req(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr) -{ - struct mru_option_t *mru_opt=container_of(opt,typeof(*mru_opt),opt); - struct lcp_opt16_t *opt16=(struct lcp_opt16_t*)ptr; - opt16->hdr.id=CI_MRU; - opt16->hdr.len=4; - opt16->val=htons(mru_opt->mru); - return 4; -} - -static int mru_send_conf_nak(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr) -{ - struct mru_option_t *mru_opt=container_of(opt,typeof(*mru_opt),opt); - struct lcp_opt16_t *opt16=(struct lcp_opt16_t*)ptr; - opt16->hdr.id=CI_MRU; - opt16->hdr.len=4; - opt16->val=htons(mru_opt->mtu); - return 4; -} - -static int mru_recv_conf_req(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr) -{ - struct mru_option_t *mru_opt=container_of(opt,typeof(*mru_opt),opt); - struct lcp_opt16_t *opt16=(struct lcp_opt16_t*)ptr; - - if (!mru_opt->mtu || mru_opt->mtu==ntohs(opt16->val)) - { - mru_opt->mtu=ntohs(opt16->val); - return LCP_OPT_ACK; - }else return LCP_OPT_NAK; -} - -static int mru_recv_conf_ack(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr) -{ - struct mru_option_t *mru_opt = container_of(opt,typeof(*mru_opt), opt); - struct ifreq ifr = { - .ifr_mtu = mru_opt->mtu, - }; - - sprintf(ifr.ifr_name,"ppp%i",lcp->ppp->unit_idx); - - if (ioctl(lcp->ppp->unit_fd, PPPIOCSMRU, &mru_opt->mru)) - log_error("\nlcp:mru: failed to set MRU: %s\n", strerror(errno)); - - if (ioctl(sock_fd, SIOCSIFMTU, &ifr)) - log_error("\nlcp:mru: failed to set MTU: %s\n", strerror(errno)); - - return 0; -} - -static void mru_print(void (*print)(const char *fmt,...),struct lcp_option_t *opt, uint8_t *ptr) -{ - struct mru_option_t *mru_opt=container_of(opt,typeof(*mru_opt),opt); - struct lcp_opt16_t *opt16=(struct lcp_opt16_t*)ptr; - - if (ptr) print("",ntohs(opt16->val)); - else print("",mru_opt->mru); -} - -static void __init mru_opt_init() -{ - lcp_option_register(&mru_opt_hnd); -} - diff --git a/accel-pptpd/lcp_opt_pcomp.c b/accel-pptpd/lcp_opt_pcomp.c deleted file mode 100644 index 8700bf4..0000000 --- a/accel-pptpd/lcp_opt_pcomp.c +++ /dev/null @@ -1,92 +0,0 @@ -#include -#include -#include - -#include "ppp.h" -#include "ppp_lcp.h" -#include "log.h" - -static struct lcp_option_t *pcomp_init(struct ppp_lcp_t *lcp); -static void pcomp_free(struct ppp_lcp_t *lcp, struct lcp_option_t *opt); -static int pcomp_send_conf_req(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr); -static int pcomp_send_conf_nak(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr); -static int pcomp_recv_conf_req(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr); -static void pcomp_print(void (*print)(const char *fmt,...),struct lcp_option_t*, uint8_t *ptr); - -struct pcomp_option_t -{ - struct lcp_option_t opt; - int pcomp; // 0 - disabled, 1 - enabled, 2 - allow,disabled, 3 - allow,enabled -}; - -static struct lcp_option_handler_t pcomp_opt_hnd= -{ - .init=pcomp_init, - .send_conf_req=pcomp_send_conf_req, - .send_conf_nak=pcomp_send_conf_nak, - .recv_conf_req=pcomp_recv_conf_req, - .free=pcomp_free, - .print=pcomp_print, -}; - -static struct lcp_option_t *pcomp_init(struct ppp_lcp_t *lcp) -{ - struct pcomp_option_t *pcomp_opt=malloc(sizeof(*pcomp_opt)); - memset(pcomp_opt,0,sizeof(*pcomp_opt)); - pcomp_opt->pcomp=0; - pcomp_opt->opt.id=CI_PCOMP; - pcomp_opt->opt.len=2; - - return &pcomp_opt->opt; -} - -static void pcomp_free(struct ppp_lcp_t *lcp, struct lcp_option_t *opt) -{ - struct pcomp_option_t *pcomp_opt=container_of(opt,typeof(*pcomp_opt),opt); - - free(pcomp_opt); -} - -static int pcomp_send_conf_req(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr) -{ - struct pcomp_option_t *pcomp_opt=container_of(opt,typeof(*pcomp_opt),opt); - struct lcp_opt_hdr_t *opt0=(struct lcp_opt_hdr_t*)ptr; - if (pcomp_opt->pcomp==1 || pcomp_opt->pcomp==3) - { - opt0->id=CI_PCOMP; - opt0->len=2; - return 2; - } - return 0; -} - -static int pcomp_send_conf_nak(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr) -{ - struct pcomp_option_t *pcomp_opt=container_of(opt,typeof(*pcomp_opt),opt); - struct lcp_opt_hdr_t *opt0=(struct lcp_opt_hdr_t*)ptr; - opt0->id=CI_PCOMP; - opt0->len=2; - return 2; -} - -static int pcomp_recv_conf_req(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr) -{ - struct pcomp_option_t *pcomp_opt=container_of(opt,typeof(*pcomp_opt),opt); - - if (pcomp_opt->pcomp>0) - { - pcomp_opt->pcomp=1; - return LCP_OPT_ACK; - }else return LCP_OPT_REJ; -} - -static void pcomp_print(void (*print)(const char *fmt,...),struct lcp_option_t *opt, uint8_t *ptr) -{ - print(""); -} - -static void __init pcomp_opt_init() -{ - lcp_option_register(&pcomp_opt_hnd); -} - diff --git a/accel-pptpd/list.h b/accel-pptpd/list.h deleted file mode 100644 index 49045b7..0000000 --- a/accel-pptpd/list.h +++ /dev/null @@ -1,252 +0,0 @@ -#ifndef _LINUX_LIST_H -#define _LINUX_LIST_H - -//#if defined(__KERNEL__) || defined(_LVM_H_INCLUDE) - -//#include - -/* - * Simple doubly linked list implementation. - * - * Some of the internal functions ("__xxx") are useful when - * manipulating whole lists rather than single entries, as - * sometimes we already know the next/prev entries and we can - * generate better code by using them directly rather than - * using the generic single-entry routines. - */ - -#ifndef NULL -#define NULL 0 -#endif - -void static inline prefetch(void* p){} - -typedef struct list_head { - struct list_head *next, *prev; -} list_t; - -#define LIST_HEAD_INIT(name) { &(name), &(name) } - -#define LIST_HEAD(name) \ - struct list_head name = LIST_HEAD_INIT(name) - -#define INIT_LIST_HEAD(ptr) do { \ - (ptr)->next = (ptr); (ptr)->prev = (ptr); \ -} while (0) - -/* - * Insert a _new entry between two known consecutive entries. - * - * This is only for internal list manipulation where we know - * the prev/next entries already! - */ -static inline void __list_add(struct list_head *_new, - struct list_head *prev, - struct list_head *next) -{ - next->prev = _new; - _new->next = next; - _new->prev = prev; - prev->next = _new; -} - -/** - * list_add - add a _new entry - * @_new: _new entry to be added - * @head: list head to add it after - * - * Insert a _new entry after the specified head. - * This is good for implementing stacks. - */ -static inline void list_add(struct list_head *_new, struct list_head *head) -{ - __list_add(_new, head, head->next); -} - -/** - * list_add_tail - add a _new entry - * @_new: _new entry to be added - * @head: list head to add it before - * - * Insert a _new entry before the specified head. - * This is useful for implementing queues. - */ -static inline void list_add_tail(struct list_head *_new, struct list_head *head) -{ - __list_add(_new, head->prev, head); -} - -/* - * Delete a list entry by making the prev/next entries - * point to each other. - * - * This is only for internal list manipulation where we know - * the prev/next entries already! - */ -static inline void __list_del(struct list_head *prev, struct list_head *next) -{ - next->prev = prev; - prev->next = next; -} - -/** - * list_del - deletes entry from list. - * @entry: the element to delete from the list. - * Note: list_empty on entry does not return true after this, the entry is in an undefined state. - */ -static inline void list_del(struct list_head *entry) -{ - __list_del(entry->prev, entry->next); - entry->next = NULL;//(void *) 0; - entry->prev = NULL;//(void *) 0; -} - -/** - * list_del_init - deletes entry from list and reinitialize it. - * @entry: the element to delete from the list. - */ -static inline void list_del_init(struct list_head *entry) -{ - __list_del(entry->prev, entry->next); - INIT_LIST_HEAD(entry); -} - -/** - * list_move - delete from one list and add as another's head - * @list: the entry to move - * @head: the head that will precede our entry - */ -static inline void list_move(struct list_head *list, struct list_head *head) -{ - __list_del(list->prev, list->next); - list_add(list, head); -} - -/** - * list_move_tail - delete from one list and add as another's tail - * @list: the entry to move - * @head: the head that will follow our entry - */ -static inline void list_move_tail(struct list_head *list, - struct list_head *head) -{ - __list_del(list->prev, list->next); - list_add_tail(list, head); -} - -/** - * list_empty - tests whether a list is empty - * @head: the list to test. - */ -static inline int list_empty(struct list_head *head) -{ - return head->next == head; -} - -static inline void __list_splice(struct list_head *list, - struct list_head *head) -{ - struct list_head *first = list->next; - struct list_head *last = list->prev; - struct list_head *at = head->next; - - first->prev = head; - head->next = first; - - last->next = at; - at->prev = last; -} - -/** - * list_splice - join two lists - * @list: the _new list to add. - * @head: the place to add it in the first list. - */ -static inline void list_splice(struct list_head *list, struct list_head *head) -{ - if (!list_empty(list)) - __list_splice(list, head); -} - -/** - * list_splice_init - join two lists and reinitialise the emptied list. - * @list: the _new list to add. - * @head: the place to add it in the first list. - * - * The list at @list is reinitialised - */ -static inline void list_splice_init(struct list_head *list, - struct list_head *head) -{ - if (!list_empty(list)) { - __list_splice(list, head); - INIT_LIST_HEAD(list); - } -} - -/** - * list_entry - get the struct for this entry - * @ptr: the &struct list_head pointer. - * @type: the type of the struct this is embedded in. - * @member: the name of the list_struct within the struct. - */ -#define list_entry(ptr, type, member) \ - ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member))) - -/** - * list_for_each - iterate over a list - * @pos: the &struct list_head to use as a loop counter. - * @head: the head for your list. - */ -#define list_for_each(pos, head) \ - for (pos = (head)->next, prefetch(pos->next); pos != (head); \ - pos = pos->next, prefetch(pos->next)) - -/** - * __list_for_each - iterate over a list - * @pos: the &struct list_head to use as a loop counter. - * @head: the head for your list. - * - * This variant differs from list_for_each() in that it's the - * simplest possible list iteration code, no prefetching is done. - * Use this for code that knows the list to be very short (empty - * or 1 entry) most of the time. - */ -#define __list_for_each(pos, head) \ - for (pos = (head)->next; pos != (head); pos = pos->next) - -/** - * list_for_each_prev - iterate over a list backwards - * @pos: the &struct list_head to use as a loop counter. - * @head: the head for your list. - */ -#define list_for_each_prev(pos, head) \ - for (pos = (head)->prev, prefetch(pos->prev); pos != (head); \ - pos = pos->prev, prefetch(pos->prev)) - -/** - * list_for_each_safe - iterate over a list safe against removal of list entry - * @pos: the &struct list_head to use as a loop counter. - * @n: another &struct list_head to use as temporary storage - * @head: the head for your list. - */ -#define list_for_each_safe(pos, n, head) \ - for (pos = (head)->next, n = pos->next; pos != (head); \ - pos = n, n = pos->next) - -/** - * list_for_each_entry - iterate over list of given type - * @pos: the type * to use as a loop counter. - * @head: the head for your list. - * @member: the name of the list_struct within the struct. - */ -#define list_for_each_entry(pos, head, member) \ - for (pos = list_entry((head)->next, typeof(*pos), member), \ - prefetch(pos->member.next); \ - &pos->member != (head); \ - pos = list_entry(pos->member.next, typeof(*pos), member), \ - prefetch(pos->member.next)) - -//#endif /* __KERNEL__ || _LVM_H_INCLUDE */ - -#endif diff --git a/accel-pptpd/log.c b/accel-pptpd/log.c index 992f24d..a29ce84 100644 --- a/accel-pptpd/log.c +++ b/accel-pptpd/log.c @@ -20,6 +20,7 @@ #include #include +#include "triton/triton.h" #include "log.h" #define RED_COLOR "\033[1;31m" @@ -61,7 +62,7 @@ static void do_log(int level,const char *fmt,va_list ap) msg_completed=fmt[strlen(fmt)-1]=='\n'; //if (msg_completed) pthread_mutex_unlock(&lock); } -void log_error(const char *fmt,...) +void __export log_error(const char *fmt,...) { if (log_level>=1) { @@ -70,7 +71,7 @@ void log_error(const char *fmt,...) do_log(LOG_ERROR,fmt,ap); } } -void log_warn(const char *fmt,...) +void __export log_warn(const char *fmt,...) { if (log_level>=2) { @@ -79,7 +80,7 @@ void log_warn(const char *fmt,...) do_log(LOG_WARN,fmt,ap); } } -void log_info(const char *fmt,...) +void __export log_info(const char *fmt,...) { if (log_level>=3) { @@ -88,7 +89,7 @@ void log_info(const char *fmt,...) do_log(LOG_INFO,fmt,ap); } } -void log_debug(const char *fmt,...) +void __export log_debug(const char *fmt,...) { if (log_level>=4) { @@ -98,7 +99,7 @@ void log_debug(const char *fmt,...) } } -void log_msg(const char *fmt,...) +void __export log_msg(const char *fmt,...) { va_list ap; if (msg_completed) return; @@ -108,9 +109,10 @@ void log_msg(const char *fmt,...) if (msg_completed) pthread_mutex_unlock(&lock); } -void log_init(FILE *f,int level,int color) +void __export log_init(FILE *f,int level,int color) { log_file=f; log_level=level; log_color=color; } + diff --git a/accel-pptpd/main.c b/accel-pptpd/main.c index 0503625..c60d35f 100644 --- a/accel-pptpd/main.c +++ b/accel-pptpd/main.c @@ -69,7 +69,7 @@ static void __init __main(void) if (!conf_file) goto usage; - if (triton_init(conf_file)) + if (triton_init(conf_file, "modules")) _exit(EXIT_FAILURE); return; diff --git a/accel-pptpd/ppp.c b/accel-pptpd/ppp.c deleted file mode 100644 index d1b3883..0000000 --- a/accel-pptpd/ppp.c +++ /dev/null @@ -1,492 +0,0 @@ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "triton/triton.h" - -#include "ppp.h" -#include "ppp_fsm.h" -#include "log.h" - -static LIST_HEAD(layers); - -struct layer_node_t -{ - struct list_head entry; - int order; - struct list_head items; -}; - -static int ppp_chan_read(struct triton_md_handler_t*); -static int ppp_unit_read(struct triton_md_handler_t*); -static void init_layers(struct ppp_t *); -static void free_layers(struct ppp_t *); -static void start_first_layer(struct ppp_t *); - -struct ppp_t *init_ppp(void) -{ - struct ppp_t *ppp=malloc(sizeof(*ppp)); - memset(ppp,0,sizeof(*ppp)); - return ppp; -} - -static void free_ppp(struct ppp_t *ppp) -{ - free(ppp->chan_buf); - free(ppp->unit_buf); -} - -int establish_ppp(struct ppp_t *ppp) -{ - /* Open an instance of /dev/ppp and connect the channel to it */ - if (ioctl(ppp->fd, PPPIOCGCHAN, &ppp->chan_idx)==-1) - { - log_error("Couldn't get channel number\n"); - return -1; - } - - ppp->chan_fd=open("/dev/ppp", O_RDWR); - if (ppp->chan_fd<0) - { - log_error("Couldn't reopen /dev/ppp\n"); - return -1; - } - - if (ioctl(ppp->chan_fd, PPPIOCATTCHAN, &ppp->chan_idx)<0) - { - log_error("Couldn't attach to channel %d\n", ppp->chan_idx); - goto exit_close_chan; - } - - ppp->unit_fd=open("/dev/ppp", O_RDWR); - if (ppp->unit_fd<0) - { - log_error("Couldn't reopen /dev/ppp\n"); - goto exit_close_chan; - } - - ppp->unit_idx=-1; - if (ioctl(ppp->unit_fd, PPPIOCNEWUNIT, &ppp->unit_idx)<0) - { - log_error("Couldn't create new ppp unit\n"); - goto exit_close_unit; - } - - if (ioctl(ppp->chan_fd, PPPIOCCONNECT, &ppp->unit_idx)<0) - { - log_error("Couldn't attach to PPP unit %d\n", ppp->unit_idx); - goto exit_close_unit; - } - - log_info("connect: ppp%i <--> pptp(%s)\n",ppp->unit_idx,ppp->chan_name); - - ppp->chan_buf=malloc(PPP_MRU); - ppp->unit_buf=malloc(PPP_MRU); - - INIT_LIST_HEAD(&ppp->chan_handlers); - INIT_LIST_HEAD(&ppp->unit_handlers); - - init_layers(ppp); - - if (list_empty(&ppp->layers)) - { - log_error("no layers to start\n"); - goto exit_close_unit; - } - - if (fcntl(ppp->chan_fd, F_SETFL, O_NONBLOCK)) { - log_error("ppp: cann't to set nonblocking mode: %s\n", strerror(errno)); - goto exit_close_unit; - } - - if (fcntl(ppp->unit_fd, F_SETFL, O_NONBLOCK)) { - log_error("ppp: cann't to set nonblocking mode: %s\n", strerror(errno)); - goto exit_close_unit; - } - - ppp->chan_hnd.fd=ppp->chan_fd; - ppp->chan_hnd.read=ppp_chan_read; - //ppp->chan_hnd.twait=-1; - ppp->unit_hnd.fd=ppp->unit_fd; - ppp->unit_hnd.read=ppp_unit_read; - //ppp->unit_hnd.twait=-1; - triton_md_register_handler(ppp->ctrl->ctx, &ppp->chan_hnd); - triton_md_register_handler(ppp->ctrl->ctx, &ppp->unit_hnd); - - triton_md_enable_handler(&ppp->chan_hnd,MD_MODE_READ); - triton_md_enable_handler(&ppp->unit_hnd,MD_MODE_READ); - - log_debug("ppp established\n"); - - start_first_layer(ppp); - - return 0; - -exit_close_unit: - close(ppp->unit_fd); -exit_close_chan: - close(ppp->chan_fd); - - free_ppp(ppp); - - return -1; -} - -void destablish_ppp(struct ppp_t *ppp) -{ - triton_md_unregister_handler(&ppp->chan_hnd); - triton_md_unregister_handler(&ppp->unit_hnd); - - close(ppp->unit_fd); - close(ppp->chan_fd); - - ppp->unit_fd = -1; - ppp->chan_fd = -1; - - free(ppp->unit_buf); - free(ppp->chan_buf); - - free_layers(ppp); - - log_debug("ppp destablished\n"); - - ppp->ctrl->finished(ppp); -} - -void print_buf(uint8_t *buf,int size) -{ - int i; - for(i=0;ichan_fd,data,size); - if (nunit_fd,data,size); - if (nchan_buf_size = read(h->fd, ppp->chan_buf, PPP_MRU); - if (ppp->chan_buf_size < 0) { - if (errno == EINTR) - continue; - if (errno == EAGAIN) - return 0; - log_error("ppp_chan_read: %s\n",strerror(errno)); - return 0; - } - - //printf("ppp_chan_read: "); - //print_buf(ppp->chan_buf,ppp->chan_buf_size); - - if (ppp->chan_buf_size < 2) { - log_error("ppp_chan_read: short read %i\n", ppp->chan_buf_size); - continue; - } - - proto = ntohs(*(uint16_t*)ppp->chan_buf); - list_for_each_entry(ppp_h, &ppp->chan_handlers, entry) { - if (ppp_h->proto == proto) { - ppp_h->recv(ppp_h); - if (ppp->chan_fd == -1) { - ppp->ctrl->finished(ppp); - return 1; - } - goto cont; - } - } - - log_warn("ppp_chan_read: discarding unknown packet %x\n", proto); - } -} - -static int ppp_unit_read(struct triton_md_handler_t *h) -{ - struct ppp_t *ppp = container_of(h, typeof(*ppp), unit_hnd); - struct ppp_handler_t *ppp_h; - uint16_t proto; - - while (1) { -cont: - ppp->unit_buf_size = read(h->fd, ppp->unit_buf, PPP_MRU); - if (ppp->unit_buf_size < 0) { - if (errno == EINTR) - continue; - if (errno == EAGAIN) - return 0; - log_error("ppp_chan_read: %s\n",strerror(errno)); - return 0; - } - - //printf("ppp_unit_read: "); - //print_buf(ppp->unit_buf,ppp->unit_buf_size); - - if (ppp->unit_buf_size < 2) { - log_error("ppp_chan_read: short read %i\n", ppp->unit_buf_size); - continue; - } - - proto=ntohs(*(uint16_t*)ppp->unit_buf); - list_for_each_entry(ppp_h, &ppp->unit_handlers, entry) { - if (ppp_h->proto == proto) { - ppp_h->recv(ppp_h); - if (ppp->unit_fd == -1) { - ppp->ctrl->finished(ppp); - return 1; - } - goto cont; - } - } - - log_warn("ppp_unit_read: discarding unknown packet %x\n",proto); - } -} - -void ppp_layer_started(struct ppp_t *ppp, struct ppp_layer_data_t *d) -{ - struct layer_node_t *n=d->node; - - d->started=1; - - list_for_each_entry(d,&n->items,entry) - if (!d->started) return; - - if (n->entry.next==&ppp->layers) - { - ppp->ctrl->started(ppp); - }else - { - n=list_entry(n->entry.next,typeof(*n),entry); - list_for_each_entry(d,&n->items,entry) - { - d->starting=1; - d->layer->start(d); - } - } -} - -void ppp_layer_finished(struct ppp_t *ppp, struct ppp_layer_data_t *d) -{ - struct layer_node_t *n=d->node; - - d->starting=0; - d->started=0; - - list_for_each_entry(n,&ppp->layers,entry) - { - list_for_each_entry(d,&n->items,entry) - { - if (d->starting) - return; - } - } - - destablish_ppp(ppp); -} - -void ppp_terminate(struct ppp_t *ppp, int hard) -{ - struct layer_node_t *n; - struct ppp_layer_data_t *d; - int s = 0; - - log_debug("ppp_terminate\n"); - - if (hard) { - destablish_ppp(ppp); - return; - } - - list_for_each_entry(n,&ppp->layers,entry) { - list_for_each_entry(d,&n->items,entry) { - if (d->starting) { - s = 1; - d->layer->finish(d); - } - } - } - if (s) - return; - destablish_ppp(ppp); -} - -void ppp_register_chan_handler(struct ppp_t *ppp,struct ppp_handler_t *h) -{ - list_add_tail(&h->entry,&ppp->chan_handlers); -} -void ppp_register_unit_handler(struct ppp_t *ppp,struct ppp_handler_t *h) -{ - list_add_tail(&h->entry,&ppp->unit_handlers); -} -void ppp_unregister_handler(struct ppp_t *ppp,struct ppp_handler_t *h) -{ - list_del(&h->entry); -} - -static int get_layer_order(const char *name) -{ - if (!strcmp(name,"lcp")) return 0; - if (!strcmp(name,"auth")) return 1; - if (!strcmp(name,"ipcp")) return 2; - if (!strcmp(name,"ccp")) return 2; - return -1; -} - -int ppp_register_layer(const char *name, struct ppp_layer_t *layer) -{ - int order; - struct layer_node_t *n,*n1; - - order=get_layer_order(name); - - if (order<0) - return order; - - list_for_each_entry(n,&layers,entry) - { - if (order>n->order) - continue; - if (orderorder) - { - n1=malloc(sizeof(*n1)); - memset(n1,0,sizeof(*n1)); - n1->order=order; - INIT_LIST_HEAD(&n1->items); - list_add_tail(&n1->entry,&n->entry); - n=n1; - } - goto insert; - } - n1=malloc(sizeof(*n1)); - memset(n1,0,sizeof(*n1)); - n1->order=order; - INIT_LIST_HEAD(&n1->items); - list_add_tail(&n1->entry,&layers); - n=n1; -insert: - list_add_tail(&layer->entry,&n->items); - - return 0; -} -void ppp_unregister_layer(struct ppp_layer_t *layer) -{ - list_del(&layer->entry); -} - -static void init_layers(struct ppp_t *ppp) -{ - struct layer_node_t *n, *n1; - struct ppp_layer_t *l; - struct ppp_layer_data_t *d; - - INIT_LIST_HEAD(&ppp->layers); - - list_for_each_entry(n,&layers,entry) { - n1 = (struct layer_node_t*)malloc(sizeof(*n1)); - memset(n1, 0, sizeof(*n1)); - INIT_LIST_HEAD(&n1->items); - list_add_tail(&n1->entry, &ppp->layers); - list_for_each_entry(l, &n->items, entry) { - d = l->init(ppp); - d->layer = l; - d->started = 0; - d->node = n1; - list_add_tail(&d->entry, &n1->items); - } - } -} - -static void free_layers(struct ppp_t *ppp) -{ - struct layer_node_t *n; - struct ppp_layer_data_t *d; - - while (!list_empty(&ppp->layers)) { - n = list_entry(ppp->layers.next, typeof(*n), entry); - while (!list_empty(&n->items)) { - d = list_entry(n->items.next, typeof(*d), entry); - list_del(&d->entry); - d->layer->free(d); - } - list_del(&n->entry); - free(n); - } -} - -static void start_first_layer(struct ppp_t *ppp) -{ - struct layer_node_t *n; - struct ppp_layer_data_t *d; - - n=list_entry(ppp->layers.next,typeof(*n),entry); - list_for_each_entry(d,&n->items,entry) - { - d->starting=1; - d->layer->start(d); - } -} - -struct ppp_layer_data_t *ppp_find_layer_data(struct ppp_t *ppp, struct ppp_layer_t *layer) -{ - struct layer_node_t *n; - struct ppp_layer_data_t *d; - - list_for_each_entry(n,&ppp->layers,entry) - { - list_for_each_entry(d,&n->items,entry) - { - if (d->layer==layer) - return d; - } - } - - return NULL; -} - -int sock_fd; -static void __init ppp_init(void) -{ - sock_fd = socket(AF_INET, SOCK_DGRAM, 0); - if (sock_fd < 0) { - perror("socket"); - _exit(EXIT_FAILURE); - } -} diff --git a/accel-pptpd/ppp.h b/accel-pptpd/ppp.h deleted file mode 100644 index b3b1be8..0000000 --- a/accel-pptpd/ppp.h +++ /dev/null @@ -1,138 +0,0 @@ -#ifndef PPP_H -#define PPP_H - -#include - -#include "triton/triton.h" -#include "list.h" - -/* - * Packet header = Code, id, length. - */ -#define PPP_HEADERLEN 4 -#define PPP_MTU 1500 - -/* - * Timeouts. - */ -#define DEFTIMEOUT 3 /* Timeout time in seconds */ -#define DEFMAXTERMREQS 2 /* Maximum Terminate-Request transmissions */ -#define DEFMAXCONFREQS 10 /* Maximum Configure-Request transmissions */ -#define DEFMAXNAKLOOPS 5 /* Maximum number of nak loops */ - -/* - * Protocol field values. - */ -#define PPP_IP 0x21 /* Internet Protocol */ -#define PPP_AT 0x29 /* AppleTalk Protocol */ -#define PPP_IPX 0x2b /* IPX protocol */ -#define PPP_VJC_COMP 0x2d /* VJ compressed TCP */ -#define PPP_VJC_UNCOMP 0x2f /* VJ uncompressed TCP */ -#define PPP_IPV6 0x57 /* Internet Protocol Version 6 */ -#define PPP_COMP 0xfd /* compressed packet */ -#define PPP_IPCP 0x8021 /* IP Control Protocol */ -#define PPP_ATCP 0x8029 /* AppleTalk Control Protocol */ -#define PPP_IPXCP 0x802b /* IPX Control Protocol */ -#define PPP_IPV6CP 0x8057 /* IPv6 Control Protocol */ -#define PPP_CCP 0x80fd /* Compression Control Protocol */ -#define PPP_ECP 0x8053 /* Encryption Control Protocol */ -#define PPP_LCP 0xc021 /* Link Control Protocol */ -#define PPP_PAP 0xc023 /* Password Authentication Protocol */ -#define PPP_LQR 0xc025 /* Link Quality Report protocol */ -#define PPP_CHAP 0xc223 /* Cryptographic Handshake Auth. Protocol */ -#define PPP_CBCP 0xc029 /* Callback Control Protocol */ -#define PPP_EAP 0xc227 /* Extensible Authentication Protocol */ - -#define PPP_LAYER_LCP 1 -#define PPP_LAYER_AUTH 2 -#define PPP_LAYER_CCP 3 -#define PPP_LAYER_IPCP 4 - -#define AUTH_MAX 3 - -struct ppp_t; - -struct ppp_ctrl_t -{ - struct triton_ctx_t *ctx; - void (*started)(struct ppp_t*); - void (*finished)(struct ppp_t*); -}; - -struct ppp_t -{ - struct triton_md_handler_t chan_hnd; - struct triton_md_handler_t unit_hnd; - int fd; - int chan_fd; - int unit_fd; - - int chan_idx; - int unit_idx; - - char *chan_name; - - struct ppp_ctrl_t *ctrl; - - int log:1; - - void *chan_buf; - int chan_buf_size; - void *unit_buf; - int unit_buf_size; - - struct list_head chan_handlers; - struct list_head unit_handlers; - - struct list_head layers; - - struct ppp_lcp_t *lcp; -}; - -struct ppp_layer_t; -struct layer_node_t; -struct ppp_layer_data_t -{ - struct list_head entry; - struct ppp_layer_t *layer; - struct layer_node_t *node; - int starting:1; - int started:1; -}; - -struct ppp_layer_t -{ - struct list_head entry; - struct ppp_layer_data_t *(*init)(struct ppp_t *); - void (*start)(struct ppp_layer_data_t*); - void (*finish)(struct ppp_layer_data_t*); - void (*free)(struct ppp_layer_data_t *); -}; - -struct ppp_handler_t -{ - struct list_head entry; - int proto; - void (*recv)(struct ppp_handler_t*); -}; - -struct ppp_t *alloc_ppp(void); -int establish_ppp(struct ppp_t *ppp); -int ppp_chan_send(struct ppp_t *ppp, void *data, int size); -int ppp_unit_send(struct ppp_t *ppp, void *data, int size); - -struct ppp_fsm_t* ppp_lcp_init(struct ppp_t *ppp); -void ppp_layer_started(struct ppp_t *ppp,struct ppp_layer_data_t*); -void ppp_layer_finished(struct ppp_t *ppp,struct ppp_layer_data_t*); -void ppp_terminate(struct ppp_t *ppp, int hard); - -void ppp_register_chan_handler(struct ppp_t *, struct ppp_handler_t *); -void ppp_register_unit_handler(struct ppp_t * ,struct ppp_handler_t *); -void ppp_unregister_handler(struct ppp_t *, struct ppp_handler_t *); - -int ppp_register_layer(const char *name, struct ppp_layer_t *); -void ppp_unregister_layer(struct ppp_layer_t *); -struct ppp_layer_data_t *ppp_find_layer_data(struct ppp_t *, struct ppp_layer_t *); - -extern int sock_fd; // internet socket for ioctls -#endif diff --git a/accel-pptpd/ppp/CMakeLists.txt b/accel-pptpd/ppp/CMakeLists.txt new file mode 100644 index 0000000..f4c0f04 --- /dev/null +++ b/accel-pptpd/ppp/CMakeLists.txt @@ -0,0 +1,19 @@ +SET(target ppp) +SET(sources_c + ppp.c + ppp_fsm.c + ppp_lcp.c + lcp_opt_mru.c + lcp_opt_magic.c + lcp_opt_pcomp.c + lcp_opt_accomp.c + ppp_auth.c + ppp_ipcp.c + ipcp_opt_ipaddr.c + ipcp_opt_dns.c + ppp_ccp.c +) +INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}) + +ADD_LIBRARY(${target} SHARED ${sources_c}) + diff --git a/accel-pptpd/ppp/ipcp_opt_dns.c b/accel-pptpd/ppp/ipcp_opt_dns.c new file mode 100644 index 0000000..b741798 --- /dev/null +++ b/accel-pptpd/ppp/ipcp_opt_dns.c @@ -0,0 +1,146 @@ +#include +#include +#include + +#include "ppp.h" +#include "ppp_ipcp.h" +#include "log.h" +#include "ipdb.h" + +static struct ipcp_option_t *dns1_init(struct ppp_ipcp_t *ipcp); +static struct ipcp_option_t *dns2_init(struct ppp_ipcp_t *ipcp); +static void dns_free(struct ppp_ipcp_t *ipcp, struct ipcp_option_t *opt); +static int dns_send_conf_req(struct ppp_ipcp_t *ipcp, struct ipcp_option_t *opt, uint8_t *ptr); +static int dns_send_conf_nak(struct ppp_ipcp_t *ipcp, struct ipcp_option_t *opt, uint8_t *ptr); +static int dns_recv_conf_req(struct ppp_ipcp_t *ipcp, struct ipcp_option_t *opt, uint8_t *ptr); +static void dns1_print(void (*print)(const char *fmt,...),struct ipcp_option_t*, uint8_t *ptr); +static void dns2_print(void (*print)(const char *fmt,...),struct ipcp_option_t*, uint8_t *ptr); + +struct dns_option_t +{ + struct ipcp_option_t opt; + in_addr_t addr; +}; + +static struct ipcp_option_handler_t dns1_opt_hnd= +{ + .init=dns1_init, + .send_conf_req=dns_send_conf_req, + .send_conf_nak=dns_send_conf_nak, + .recv_conf_req=dns_recv_conf_req, + .free=dns_free, + .print=dns1_print, +}; +static struct ipcp_option_handler_t dns2_opt_hnd= +{ + .init=dns2_init, + .send_conf_req=dns_send_conf_req, + .send_conf_nak=dns_send_conf_nak, + .recv_conf_req=dns_recv_conf_req, + .free=dns_free, + .print=dns2_print, +}; + +static struct ipcp_option_t *dns1_init(struct ppp_ipcp_t *ipcp) +{ + struct dns_option_t *dns_opt=malloc(sizeof(*dns_opt)); + memset(dns_opt,0,sizeof(*dns_opt)); + dns_opt->opt.id=CI_DNS1; + dns_opt->opt.len=6; + + return &dns_opt->opt; +} + +static struct ipcp_option_t *dns2_init(struct ppp_ipcp_t *ipcp) +{ + struct dns_option_t *dns_opt=malloc(sizeof(*dns_opt)); + memset(dns_opt,0,sizeof(*dns_opt)); + dns_opt->opt.id=CI_DNS2; + dns_opt->opt.len=6; + + return &dns_opt->opt; +} + +static void dns_free(struct ppp_ipcp_t *ipcp, struct ipcp_option_t *opt) +{ + struct dns_option_t *dns_opt=container_of(opt,typeof(*dns_opt),opt); + + free(dns_opt); +} + +static int dns_send_conf_req(struct ppp_ipcp_t *ipcp, struct ipcp_option_t *opt, uint8_t *ptr) +{ + struct dns_option_t *dns_opt=container_of(opt,typeof(*dns_opt),opt); + struct ipcp_opt32_t *opt32=(struct ipcp_opt32_t*)ptr; + + if (!dns_opt->addr) + return 0; + opt32->hdr.id=dns_opt->opt.id; + opt32->hdr.len=6; + opt32->val=dns_opt->addr; + return 6; +} + +static int dns_send_conf_nak(struct ppp_ipcp_t *ipcp, struct ipcp_option_t *opt, uint8_t *ptr) +{ + struct dns_option_t *dns_opt=container_of(opt,typeof(*dns_opt),opt); + struct ipcp_opt32_t *opt32=(struct ipcp_opt32_t*)ptr; + opt32->hdr.id=dns_opt->opt.id; + opt32->hdr.len=6; + opt32->val=dns_opt->addr; + return 6; +} + +static int dns_recv_conf_req(struct ppp_ipcp_t *ipcp, struct ipcp_option_t *opt, uint8_t *ptr) +{ + struct dns_option_t *dns_opt=container_of(opt,typeof(*dns_opt),opt); + struct ipcp_opt32_t *opt32=(struct ipcp_opt32_t*)ptr; + + if (!dns_opt->addr) + { + if (dns_opt->opt.id==CI_DNS1) dns_opt->addr=inet_addr("10.0.0.1"); + else dns_opt->addr=inet_addr("10.0.0.2"); + + if (!dns_opt->addr) + { + dns_opt->addr=opt32->val; + return IPCP_OPT_ACK; + } + } + + if (dns_opt->addr==opt32->val) + return IPCP_OPT_ACK; + + return IPCP_OPT_NAK; +} + +static void dns1_print(void (*print)(const char *fmt,...),struct ipcp_option_t *opt, uint8_t *ptr) +{ + struct dns_option_t *dns_opt=container_of(opt,typeof(*dns_opt),opt); + struct ipcp_opt32_t *opt32=(struct ipcp_opt32_t*)ptr; + struct in_addr in; + + if (ptr) in.s_addr=opt32->val; + else in.s_addr=dns_opt->addr; + + print("",inet_ntoa(in)); +} + +static void dns2_print(void (*print)(const char *fmt,...),struct ipcp_option_t *opt, uint8_t *ptr) +{ + struct dns_option_t *dns_opt=container_of(opt,typeof(*dns_opt),opt); + struct ipcp_opt32_t *opt32=(struct ipcp_opt32_t*)ptr; + struct in_addr in; + + if (ptr) in.s_addr=opt32->val; + else in.s_addr=dns_opt->addr; + + print("",inet_ntoa(in)); +} + +static void __init dns_opt_init() +{ + ipcp_option_register(&dns1_opt_hnd); + ipcp_option_register(&dns2_opt_hnd); +} + diff --git a/accel-pptpd/ppp/ipcp_opt_ipaddr.c b/accel-pptpd/ppp/ipcp_opt_ipaddr.c new file mode 100644 index 0000000..2fa9251 --- /dev/null +++ b/accel-pptpd/ppp/ipcp_opt_ipaddr.c @@ -0,0 +1,147 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "ppp.h" +#include "ppp_ipcp.h" +#include "log.h" +#include "ipdb.h" + +static struct ipcp_option_t *ipaddr_init(struct ppp_ipcp_t *ipcp); +static void ipaddr_free(struct ppp_ipcp_t *ipcp, struct ipcp_option_t *opt); +static int ipaddr_send_conf_req(struct ppp_ipcp_t *ipcp, struct ipcp_option_t *opt, uint8_t *ptr); +static int ipaddr_send_conf_nak(struct ppp_ipcp_t *ipcp, struct ipcp_option_t *opt, uint8_t *ptr); +static int ipaddr_recv_conf_req(struct ppp_ipcp_t *ipcp, struct ipcp_option_t *opt, uint8_t *ptr); +//static int ipaddr_recv_conf_ack(struct ppp_ipcp_t *ipcp, struct ipcp_option_t *opt, uint8_t *ptr); +static void ipaddr_print(void (*print)(const char *fmt,...),struct ipcp_option_t*, uint8_t *ptr); + +struct ipaddr_option_t +{ + struct ipcp_option_t opt; + in_addr_t addr; + in_addr_t peer_addr; +}; + +static struct ipcp_option_handler_t ipaddr_opt_hnd= +{ + .init=ipaddr_init, + .send_conf_req=ipaddr_send_conf_req, + .send_conf_nak=ipaddr_send_conf_nak, + .recv_conf_req=ipaddr_recv_conf_req, + .free=ipaddr_free, + .print=ipaddr_print, +}; + +static struct ipcp_option_t *ipaddr_init(struct ppp_ipcp_t *ipcp) +{ + struct ipaddr_option_t *ipaddr_opt=malloc(sizeof(*ipaddr_opt)); + memset(ipaddr_opt,0,sizeof(*ipaddr_opt)); + ipdb_get(&ipaddr_opt->addr,&ipaddr_opt->peer_addr); + ipaddr_opt->opt.id=CI_ADDR; + ipaddr_opt->opt.len=6; + + return &ipaddr_opt->opt; +} + +static void ipaddr_free(struct ppp_ipcp_t *ipcp, struct ipcp_option_t *opt) +{ + struct ipaddr_option_t *ipaddr_opt=container_of(opt,typeof(*ipaddr_opt),opt); + + free(ipaddr_opt); +} + +static int ipaddr_send_conf_req(struct ppp_ipcp_t *ipcp, struct ipcp_option_t *opt, uint8_t *ptr) +{ + struct ipaddr_option_t *ipaddr_opt=container_of(opt,typeof(*ipaddr_opt),opt); + struct ipcp_opt32_t *opt32=(struct ipcp_opt32_t*)ptr; + opt32->hdr.id=CI_ADDR; + opt32->hdr.len=6; + opt32->val=ipaddr_opt->addr; + return 6; +} + +static int ipaddr_send_conf_nak(struct ppp_ipcp_t *ipcp, struct ipcp_option_t *opt, uint8_t *ptr) +{ + struct ipaddr_option_t *ipaddr_opt=container_of(opt,typeof(*ipaddr_opt),opt); + struct ipcp_opt32_t *opt32=(struct ipcp_opt32_t*)ptr; + opt32->hdr.id=CI_ADDR; + opt32->hdr.len=6; + opt32->val=ipaddr_opt->peer_addr; + return 6; +} + +static int ipaddr_recv_conf_req(struct ppp_ipcp_t *ipcp, struct ipcp_option_t *opt, uint8_t *ptr) +{ + struct ipaddr_option_t *ipaddr_opt = container_of(opt,typeof(*ipaddr_opt), opt); + struct ipcp_opt32_t *opt32 = (struct ipcp_opt32_t*)ptr; + struct ifreq ifr; + struct sockaddr_in addr; + struct npioctl np; + + if (ipaddr_opt->peer_addr == opt32->val) + goto ack; + + if (!ipaddr_opt->peer_addr) { + ipaddr_opt->peer_addr = opt32->val; + goto ack; + } + + return IPCP_OPT_NAK; + +ack: + memset(&ifr, 0, sizeof(ifr)); + memset(&addr, 0, sizeof(addr)); + + sprintf(ifr.ifr_name,"ppp%i",ipcp->ppp->unit_idx); + + addr.sin_family = AF_INET; + addr.sin_addr.s_addr = ipaddr_opt->addr; + memcpy(&ifr.ifr_addr,&addr,sizeof(addr)); + + if (ioctl(sock_fd, SIOCSIFADDR, &ifr)) + log_error("\nipcp: failed to set PA address: %s\n", strerror(errno)); + + addr.sin_addr.s_addr = ipaddr_opt->peer_addr; + memcpy(&ifr.ifr_dstaddr,&addr,sizeof(addr)); + + if (ioctl(sock_fd, SIOCSIFDSTADDR, &ifr)) + log_error("\nipcp: failed to set remote PA address: %s\n", strerror(errno)); + + if (ioctl(sock_fd, SIOCGIFFLAGS, &ifr)) + log_error("\nipcp: failed to get interface flags: %s\n", strerror(errno)); + + ifr.ifr_flags |= IFF_UP | IFF_POINTOPOINT; + + if (ioctl(sock_fd, SIOCSIFFLAGS, &ifr)) + log_error("\nipcp: failed to set interface flags: %s\n", strerror(errno)); + + np.protocol = PPP_IP; + np.mode = NPMODE_PASS; + + if (ioctl(ipcp->ppp->unit_fd, PPPIOCSNPMODE, &np)) + log_error("\nipcp: failed to set NP mode: %s\n", strerror(errno)); + + return IPCP_OPT_ACK; +} + +static void ipaddr_print(void (*print)(const char *fmt,...),struct ipcp_option_t *opt, uint8_t *ptr) +{ + struct ipaddr_option_t *ipaddr_opt=container_of(opt,typeof(*ipaddr_opt),opt); + struct ipcp_opt32_t *opt32=(struct ipcp_opt32_t*)ptr; + struct in_addr in; + + if (ptr) in.s_addr=opt32->val; + else in.s_addr=ipaddr_opt->addr; + + print("",inet_ntoa(in)); +} + +static void __init ipaddr_opt_init() +{ + ipcp_option_register(&ipaddr_opt_hnd); +} + diff --git a/accel-pptpd/ppp/lcp_opt_accomp.c b/accel-pptpd/ppp/lcp_opt_accomp.c new file mode 100644 index 0000000..c4c221c --- /dev/null +++ b/accel-pptpd/ppp/lcp_opt_accomp.c @@ -0,0 +1,92 @@ +#include +#include +#include + +#include "ppp.h" +#include "ppp_lcp.h" +#include "log.h" + +static struct lcp_option_t *accomp_init(struct ppp_lcp_t *lcp); +static void accomp_free(struct ppp_lcp_t *lcp, struct lcp_option_t *opt); +static int accomp_send_conf_req(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr); +static int accomp_send_conf_nak(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr); +static int accomp_recv_conf_req(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr); +static void accomp_print(void (*print)(const char *fmt,...),struct lcp_option_t*, uint8_t *ptr); + +struct accomp_option_t +{ + struct lcp_option_t opt; + int accomp; // 0 - disabled, 1 - enabled, 2 - allow,disabled, 3 - allow,enabled +}; + +static struct lcp_option_handler_t accomp_opt_hnd= +{ + .init=accomp_init, + .send_conf_req=accomp_send_conf_req, + .send_conf_nak=accomp_send_conf_nak, + .recv_conf_req=accomp_recv_conf_req, + .free=accomp_free, + .print=accomp_print, +}; + +static struct lcp_option_t *accomp_init(struct ppp_lcp_t *lcp) +{ + struct accomp_option_t *accomp_opt=malloc(sizeof(*accomp_opt)); + memset(accomp_opt,0,sizeof(*accomp_opt)); + accomp_opt->accomp=0; + accomp_opt->opt.id=CI_ACCOMP; + accomp_opt->opt.len=2; + + return &accomp_opt->opt; +} + +static void accomp_free(struct ppp_lcp_t *lcp, struct lcp_option_t *opt) +{ + struct accomp_option_t *accomp_opt=container_of(opt,typeof(*accomp_opt),opt); + + free(accomp_opt); +} + +static int accomp_send_conf_req(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr) +{ + struct accomp_option_t *accomp_opt=container_of(opt,typeof(*accomp_opt),opt); + struct lcp_opt_hdr_t *opt0=(struct lcp_opt_hdr_t*)ptr; + if (accomp_opt->accomp==1 || accomp_opt->accomp==3) + { + opt0->id=CI_ACCOMP; + opt0->len=2; + return 2; + } + return 0; +} + +static int accomp_send_conf_nak(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr) +{ + struct accomp_option_t *accomp_opt=container_of(opt,typeof(*accomp_opt),opt); + struct lcp_opt_hdr_t *opt0=(struct lcp_opt_hdr_t*)ptr; + opt0->id=CI_ACCOMP; + opt0->len=2; + return 2; +} + +static int accomp_recv_conf_req(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr) +{ + struct accomp_option_t *accomp_opt=container_of(opt,typeof(*accomp_opt),opt); + + if (accomp_opt->accomp>0) + { + accomp_opt->accomp=1; + return LCP_OPT_ACK; + }else return LCP_OPT_REJ; +} + +static void accomp_print(void (*print)(const char *fmt,...),struct lcp_option_t *opt, uint8_t *ptr) +{ + print(""); +} + +static void __init accomp_opt_init() +{ + lcp_option_register(&accomp_opt_hnd); +} + diff --git a/accel-pptpd/ppp/lcp_opt_magic.c b/accel-pptpd/ppp/lcp_opt_magic.c new file mode 100644 index 0000000..dc94ac6 --- /dev/null +++ b/accel-pptpd/ppp/lcp_opt_magic.c @@ -0,0 +1,83 @@ +#include +#include +#include + +#include "ppp.h" +#include "ppp_lcp.h" +#include "log.h" + +static struct lcp_option_t *magic_init(struct ppp_lcp_t *lcp); +static void magic_free(struct ppp_lcp_t *lcp, struct lcp_option_t *opt); +static int magic_send_conf_req(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr); +static int magic_recv_conf_req(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr); +static void magic_print(void (*print)(const char *fmt,...),struct lcp_option_t*, uint8_t *ptr); + +struct magic_option_t +{ + struct lcp_option_t opt; + int magic; +}; + +static struct lcp_option_handler_t magic_opt_hnd= +{ + .init=magic_init, + .send_conf_req=magic_send_conf_req, + .recv_conf_req=magic_recv_conf_req, + .free=magic_free, + .print=magic_print, +}; + +static struct lcp_option_t *magic_init(struct ppp_lcp_t *lcp) +{ + struct magic_option_t *magic_opt=malloc(sizeof(*magic_opt)); + memset(magic_opt,0,sizeof(*magic_opt)); + magic_opt->magic=random(); + magic_opt->opt.id=CI_MAGIC; + magic_opt->opt.len=6; + + lcp->magic = magic_opt->magic; + + return &magic_opt->opt; +} + +static void magic_free(struct ppp_lcp_t *lcp, struct lcp_option_t *opt) +{ + struct magic_option_t *magic_opt=container_of(opt,typeof(*magic_opt),opt); + + free(magic_opt); +} + +static int magic_send_conf_req(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr) +{ + struct magic_option_t *magic_opt=container_of(opt,typeof(*magic_opt),opt); + struct lcp_opt32_t *opt32=(struct lcp_opt32_t*)ptr; + opt32->hdr.id=CI_MAGIC; + opt32->hdr.len=6; + opt32->val=htonl(magic_opt->magic); + return 6; +} + +static int magic_recv_conf_req(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr) +{ + struct magic_option_t *magic_opt=container_of(opt,typeof(*magic_opt),opt); + struct lcp_opt32_t *opt32=(struct lcp_opt32_t*)ptr; + + if (magic_opt->magic==ntohl(opt32->val)) + { + log_error("loop detected"); + return -1; + } + return LCP_OPT_ACK; +} + +static void magic_print(void (*print)(const char *fmt,...),struct lcp_option_t *opt, uint8_t *ptr) +{ + struct magic_option_t *magic_opt=container_of(opt,typeof(*magic_opt),opt); + + print("",magic_opt->magic); +} + +static void __init magic_opt_init() +{ + lcp_option_register(&magic_opt_hnd); +} diff --git a/accel-pptpd/ppp/lcp_opt_mru.c b/accel-pptpd/ppp/lcp_opt_mru.c new file mode 100644 index 0000000..e1c13b4 --- /dev/null +++ b/accel-pptpd/ppp/lcp_opt_mru.c @@ -0,0 +1,123 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "ppp.h" +#include "ppp_lcp.h" +#include "log.h" + +#define MAX_MTU 1436 + +static struct lcp_option_t *mru_init(struct ppp_lcp_t *lcp); +static void mru_free(struct ppp_lcp_t *lcp, struct lcp_option_t *opt); +static int mru_send_conf_req(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr); +static int mru_send_conf_nak(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr); +static int mru_recv_conf_req(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr); +static int mru_recv_conf_ack(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr); +static void mru_print(void (*print)(const char *fmt,...),struct lcp_option_t*, uint8_t *ptr); + +struct mru_option_t +{ + struct lcp_option_t opt; + int mru; + int mtu; +}; + +static struct lcp_option_handler_t mru_opt_hnd= +{ + .init=mru_init, + .send_conf_req=mru_send_conf_req, + .send_conf_nak=mru_send_conf_nak, + .recv_conf_req=mru_recv_conf_req, + .recv_conf_ack=mru_recv_conf_ack, + .free=mru_free, + .print=mru_print, +}; + +static struct lcp_option_t *mru_init(struct ppp_lcp_t *lcp) +{ + struct mru_option_t *mru_opt=malloc(sizeof(*mru_opt)); + memset(mru_opt,0,sizeof(*mru_opt)); + mru_opt->mtu=0; + mru_opt->mru=MAX_MTU; + mru_opt->opt.id=CI_MRU; + mru_opt->opt.len=4; + + return &mru_opt->opt; +} + +static void mru_free(struct ppp_lcp_t *lcp, struct lcp_option_t *opt) +{ + struct mru_option_t *mru_opt=container_of(opt,typeof(*mru_opt),opt); + + free(mru_opt); +} + +static int mru_send_conf_req(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr) +{ + struct mru_option_t *mru_opt=container_of(opt,typeof(*mru_opt),opt); + struct lcp_opt16_t *opt16=(struct lcp_opt16_t*)ptr; + opt16->hdr.id=CI_MRU; + opt16->hdr.len=4; + opt16->val=htons(mru_opt->mru); + return 4; +} + +static int mru_send_conf_nak(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr) +{ + struct mru_option_t *mru_opt=container_of(opt,typeof(*mru_opt),opt); + struct lcp_opt16_t *opt16=(struct lcp_opt16_t*)ptr; + opt16->hdr.id=CI_MRU; + opt16->hdr.len=4; + opt16->val=htons(mru_opt->mtu); + return 4; +} + +static int mru_recv_conf_req(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr) +{ + struct mru_option_t *mru_opt=container_of(opt,typeof(*mru_opt),opt); + struct lcp_opt16_t *opt16=(struct lcp_opt16_t*)ptr; + + if (!mru_opt->mtu || mru_opt->mtu==ntohs(opt16->val)) + { + mru_opt->mtu=ntohs(opt16->val); + return LCP_OPT_ACK; + }else return LCP_OPT_NAK; +} + +static int mru_recv_conf_ack(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr) +{ + struct mru_option_t *mru_opt = container_of(opt,typeof(*mru_opt), opt); + struct ifreq ifr = { + .ifr_mtu = mru_opt->mtu, + }; + + sprintf(ifr.ifr_name,"ppp%i",lcp->ppp->unit_idx); + + if (ioctl(lcp->ppp->unit_fd, PPPIOCSMRU, &mru_opt->mru)) + log_error("\nlcp:mru: failed to set MRU: %s\n", strerror(errno)); + + if (ioctl(sock_fd, SIOCSIFMTU, &ifr)) + log_error("\nlcp:mru: failed to set MTU: %s\n", strerror(errno)); + + return 0; +} + +static void mru_print(void (*print)(const char *fmt,...),struct lcp_option_t *opt, uint8_t *ptr) +{ + struct mru_option_t *mru_opt=container_of(opt,typeof(*mru_opt),opt); + struct lcp_opt16_t *opt16=(struct lcp_opt16_t*)ptr; + + if (ptr) print("",ntohs(opt16->val)); + else print("",mru_opt->mru); +} + +static void __init mru_opt_init() +{ + lcp_option_register(&mru_opt_hnd); +} + diff --git a/accel-pptpd/ppp/lcp_opt_pcomp.c b/accel-pptpd/ppp/lcp_opt_pcomp.c new file mode 100644 index 0000000..8700bf4 --- /dev/null +++ b/accel-pptpd/ppp/lcp_opt_pcomp.c @@ -0,0 +1,92 @@ +#include +#include +#include + +#include "ppp.h" +#include "ppp_lcp.h" +#include "log.h" + +static struct lcp_option_t *pcomp_init(struct ppp_lcp_t *lcp); +static void pcomp_free(struct ppp_lcp_t *lcp, struct lcp_option_t *opt); +static int pcomp_send_conf_req(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr); +static int pcomp_send_conf_nak(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr); +static int pcomp_recv_conf_req(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr); +static void pcomp_print(void (*print)(const char *fmt,...),struct lcp_option_t*, uint8_t *ptr); + +struct pcomp_option_t +{ + struct lcp_option_t opt; + int pcomp; // 0 - disabled, 1 - enabled, 2 - allow,disabled, 3 - allow,enabled +}; + +static struct lcp_option_handler_t pcomp_opt_hnd= +{ + .init=pcomp_init, + .send_conf_req=pcomp_send_conf_req, + .send_conf_nak=pcomp_send_conf_nak, + .recv_conf_req=pcomp_recv_conf_req, + .free=pcomp_free, + .print=pcomp_print, +}; + +static struct lcp_option_t *pcomp_init(struct ppp_lcp_t *lcp) +{ + struct pcomp_option_t *pcomp_opt=malloc(sizeof(*pcomp_opt)); + memset(pcomp_opt,0,sizeof(*pcomp_opt)); + pcomp_opt->pcomp=0; + pcomp_opt->opt.id=CI_PCOMP; + pcomp_opt->opt.len=2; + + return &pcomp_opt->opt; +} + +static void pcomp_free(struct ppp_lcp_t *lcp, struct lcp_option_t *opt) +{ + struct pcomp_option_t *pcomp_opt=container_of(opt,typeof(*pcomp_opt),opt); + + free(pcomp_opt); +} + +static int pcomp_send_conf_req(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr) +{ + struct pcomp_option_t *pcomp_opt=container_of(opt,typeof(*pcomp_opt),opt); + struct lcp_opt_hdr_t *opt0=(struct lcp_opt_hdr_t*)ptr; + if (pcomp_opt->pcomp==1 || pcomp_opt->pcomp==3) + { + opt0->id=CI_PCOMP; + opt0->len=2; + return 2; + } + return 0; +} + +static int pcomp_send_conf_nak(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr) +{ + struct pcomp_option_t *pcomp_opt=container_of(opt,typeof(*pcomp_opt),opt); + struct lcp_opt_hdr_t *opt0=(struct lcp_opt_hdr_t*)ptr; + opt0->id=CI_PCOMP; + opt0->len=2; + return 2; +} + +static int pcomp_recv_conf_req(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr) +{ + struct pcomp_option_t *pcomp_opt=container_of(opt,typeof(*pcomp_opt),opt); + + if (pcomp_opt->pcomp>0) + { + pcomp_opt->pcomp=1; + return LCP_OPT_ACK; + }else return LCP_OPT_REJ; +} + +static void pcomp_print(void (*print)(const char *fmt,...),struct lcp_option_t *opt, uint8_t *ptr) +{ + print(""); +} + +static void __init pcomp_opt_init() +{ + lcp_option_register(&pcomp_opt_hnd); +} + diff --git a/accel-pptpd/ppp/ppp.c b/accel-pptpd/ppp/ppp.c new file mode 100644 index 0000000..5c2eb91 --- /dev/null +++ b/accel-pptpd/ppp/ppp.c @@ -0,0 +1,492 @@ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "triton.h" + +#include "ppp.h" +#include "ppp_fsm.h" +#include "log.h" + +static LIST_HEAD(layers); + +struct layer_node_t +{ + struct list_head entry; + int order; + struct list_head items; +}; + +static int ppp_chan_read(struct triton_md_handler_t*); +static int ppp_unit_read(struct triton_md_handler_t*); +static void init_layers(struct ppp_t *); +static void free_layers(struct ppp_t *); +static void start_first_layer(struct ppp_t *); + +struct ppp_t *init_ppp(void) +{ + struct ppp_t *ppp=malloc(sizeof(*ppp)); + memset(ppp,0,sizeof(*ppp)); + return ppp; +} + +static void free_ppp(struct ppp_t *ppp) +{ + free(ppp->chan_buf); + free(ppp->unit_buf); +} + +int __export establish_ppp(struct ppp_t *ppp) +{ + /* Open an instance of /dev/ppp and connect the channel to it */ + if (ioctl(ppp->fd, PPPIOCGCHAN, &ppp->chan_idx)==-1) + { + log_error("Couldn't get channel number\n"); + return -1; + } + + ppp->chan_fd=open("/dev/ppp", O_RDWR); + if (ppp->chan_fd<0) + { + log_error("Couldn't reopen /dev/ppp\n"); + return -1; + } + + if (ioctl(ppp->chan_fd, PPPIOCATTCHAN, &ppp->chan_idx)<0) + { + log_error("Couldn't attach to channel %d\n", ppp->chan_idx); + goto exit_close_chan; + } + + ppp->unit_fd=open("/dev/ppp", O_RDWR); + if (ppp->unit_fd<0) + { + log_error("Couldn't reopen /dev/ppp\n"); + goto exit_close_chan; + } + + ppp->unit_idx=-1; + if (ioctl(ppp->unit_fd, PPPIOCNEWUNIT, &ppp->unit_idx)<0) + { + log_error("Couldn't create new ppp unit\n"); + goto exit_close_unit; + } + + if (ioctl(ppp->chan_fd, PPPIOCCONNECT, &ppp->unit_idx)<0) + { + log_error("Couldn't attach to PPP unit %d\n", ppp->unit_idx); + goto exit_close_unit; + } + + log_info("connect: ppp%i <--> pptp(%s)\n",ppp->unit_idx,ppp->chan_name); + + ppp->chan_buf=malloc(PPP_MRU); + ppp->unit_buf=malloc(PPP_MRU); + + INIT_LIST_HEAD(&ppp->chan_handlers); + INIT_LIST_HEAD(&ppp->unit_handlers); + + init_layers(ppp); + + if (list_empty(&ppp->layers)) + { + log_error("no layers to start\n"); + goto exit_close_unit; + } + + if (fcntl(ppp->chan_fd, F_SETFL, O_NONBLOCK)) { + log_error("ppp: cann't to set nonblocking mode: %s\n", strerror(errno)); + goto exit_close_unit; + } + + if (fcntl(ppp->unit_fd, F_SETFL, O_NONBLOCK)) { + log_error("ppp: cann't to set nonblocking mode: %s\n", strerror(errno)); + goto exit_close_unit; + } + + ppp->chan_hnd.fd=ppp->chan_fd; + ppp->chan_hnd.read=ppp_chan_read; + //ppp->chan_hnd.twait=-1; + ppp->unit_hnd.fd=ppp->unit_fd; + ppp->unit_hnd.read=ppp_unit_read; + //ppp->unit_hnd.twait=-1; + triton_md_register_handler(ppp->ctrl->ctx, &ppp->chan_hnd); + triton_md_register_handler(ppp->ctrl->ctx, &ppp->unit_hnd); + + triton_md_enable_handler(&ppp->chan_hnd,MD_MODE_READ); + triton_md_enable_handler(&ppp->unit_hnd,MD_MODE_READ); + + log_debug("ppp established\n"); + + start_first_layer(ppp); + + return 0; + +exit_close_unit: + close(ppp->unit_fd); +exit_close_chan: + close(ppp->chan_fd); + + free_ppp(ppp); + + return -1; +} + +static void destablish_ppp(struct ppp_t *ppp) +{ + triton_md_unregister_handler(&ppp->chan_hnd); + triton_md_unregister_handler(&ppp->unit_hnd); + + close(ppp->unit_fd); + close(ppp->chan_fd); + + ppp->unit_fd = -1; + ppp->chan_fd = -1; + + free(ppp->unit_buf); + free(ppp->chan_buf); + + free_layers(ppp); + + log_debug("ppp destablished\n"); + + ppp->ctrl->finished(ppp); +} + +void print_buf(uint8_t *buf,int size) +{ + int i; + for(i=0;ichan_fd,data,size); + if (nunit_fd,data,size); + if (nchan_buf_size = read(h->fd, ppp->chan_buf, PPP_MRU); + if (ppp->chan_buf_size < 0) { + if (errno == EINTR) + continue; + if (errno == EAGAIN) + return 0; + log_error("ppp_chan_read: %s\n",strerror(errno)); + return 0; + } + + //printf("ppp_chan_read: "); + //print_buf(ppp->chan_buf,ppp->chan_buf_size); + + if (ppp->chan_buf_size < 2) { + log_error("ppp_chan_read: short read %i\n", ppp->chan_buf_size); + continue; + } + + proto = ntohs(*(uint16_t*)ppp->chan_buf); + list_for_each_entry(ppp_h, &ppp->chan_handlers, entry) { + if (ppp_h->proto == proto) { + ppp_h->recv(ppp_h); + if (ppp->chan_fd == -1) { + ppp->ctrl->finished(ppp); + return 1; + } + goto cont; + } + } + + log_warn("ppp_chan_read: discarding unknown packet %x\n", proto); + } +} + +static int ppp_unit_read(struct triton_md_handler_t *h) +{ + struct ppp_t *ppp = container_of(h, typeof(*ppp), unit_hnd); + struct ppp_handler_t *ppp_h; + uint16_t proto; + + while (1) { +cont: + ppp->unit_buf_size = read(h->fd, ppp->unit_buf, PPP_MRU); + if (ppp->unit_buf_size < 0) { + if (errno == EINTR) + continue; + if (errno == EAGAIN) + return 0; + log_error("ppp_chan_read: %s\n",strerror(errno)); + return 0; + } + + //printf("ppp_unit_read: "); + //print_buf(ppp->unit_buf,ppp->unit_buf_size); + + if (ppp->unit_buf_size < 2) { + log_error("ppp_chan_read: short read %i\n", ppp->unit_buf_size); + continue; + } + + proto=ntohs(*(uint16_t*)ppp->unit_buf); + list_for_each_entry(ppp_h, &ppp->unit_handlers, entry) { + if (ppp_h->proto == proto) { + ppp_h->recv(ppp_h); + if (ppp->unit_fd == -1) { + ppp->ctrl->finished(ppp); + return 1; + } + goto cont; + } + } + + log_warn("ppp_unit_read: discarding unknown packet %x\n",proto); + } +} + +void __export ppp_layer_started(struct ppp_t *ppp, struct ppp_layer_data_t *d) +{ + struct layer_node_t *n=d->node; + + d->started=1; + + list_for_each_entry(d,&n->items,entry) + if (!d->started) return; + + if (n->entry.next==&ppp->layers) + { + ppp->ctrl->started(ppp); + }else + { + n=list_entry(n->entry.next,typeof(*n),entry); + list_for_each_entry(d,&n->items,entry) + { + d->starting=1; + d->layer->start(d); + } + } +} + +void __export ppp_layer_finished(struct ppp_t *ppp, struct ppp_layer_data_t *d) +{ + struct layer_node_t *n=d->node; + + d->starting=0; + d->started=0; + + list_for_each_entry(n,&ppp->layers,entry) + { + list_for_each_entry(d,&n->items,entry) + { + if (d->starting) + return; + } + } + + destablish_ppp(ppp); +} + +void __export ppp_terminate(struct ppp_t *ppp, int hard) +{ + struct layer_node_t *n; + struct ppp_layer_data_t *d; + int s = 0; + + log_debug("ppp_terminate\n"); + + if (hard) { + destablish_ppp(ppp); + return; + } + + list_for_each_entry(n,&ppp->layers,entry) { + list_for_each_entry(d,&n->items,entry) { + if (d->starting) { + s = 1; + d->layer->finish(d); + } + } + } + if (s) + return; + destablish_ppp(ppp); +} + +void __export ppp_register_chan_handler(struct ppp_t *ppp,struct ppp_handler_t *h) +{ + list_add_tail(&h->entry,&ppp->chan_handlers); +} +void __export ppp_register_unit_handler(struct ppp_t *ppp,struct ppp_handler_t *h) +{ + list_add_tail(&h->entry,&ppp->unit_handlers); +} +void __export ppp_unregister_handler(struct ppp_t *ppp,struct ppp_handler_t *h) +{ + list_del(&h->entry); +} + +static int get_layer_order(const char *name) +{ + if (!strcmp(name,"lcp")) return 0; + if (!strcmp(name,"auth")) return 1; + if (!strcmp(name,"ipcp")) return 2; + if (!strcmp(name,"ccp")) return 2; + return -1; +} + +int __export ppp_register_layer(const char *name, struct ppp_layer_t *layer) +{ + int order; + struct layer_node_t *n,*n1; + + order=get_layer_order(name); + + if (order<0) + return order; + + list_for_each_entry(n,&layers,entry) + { + if (order>n->order) + continue; + if (orderorder) + { + n1=malloc(sizeof(*n1)); + memset(n1,0,sizeof(*n1)); + n1->order=order; + INIT_LIST_HEAD(&n1->items); + list_add_tail(&n1->entry,&n->entry); + n=n1; + } + goto insert; + } + n1=malloc(sizeof(*n1)); + memset(n1,0,sizeof(*n1)); + n1->order=order; + INIT_LIST_HEAD(&n1->items); + list_add_tail(&n1->entry,&layers); + n=n1; +insert: + list_add_tail(&layer->entry,&n->items); + + return 0; +} +void __export ppp_unregister_layer(struct ppp_layer_t *layer) +{ + list_del(&layer->entry); +} + +static void init_layers(struct ppp_t *ppp) +{ + struct layer_node_t *n, *n1; + struct ppp_layer_t *l; + struct ppp_layer_data_t *d; + + INIT_LIST_HEAD(&ppp->layers); + + list_for_each_entry(n,&layers,entry) { + n1 = (struct layer_node_t*)malloc(sizeof(*n1)); + memset(n1, 0, sizeof(*n1)); + INIT_LIST_HEAD(&n1->items); + list_add_tail(&n1->entry, &ppp->layers); + list_for_each_entry(l, &n->items, entry) { + d = l->init(ppp); + d->layer = l; + d->started = 0; + d->node = n1; + list_add_tail(&d->entry, &n1->items); + } + } +} + +static void free_layers(struct ppp_t *ppp) +{ + struct layer_node_t *n; + struct ppp_layer_data_t *d; + + while (!list_empty(&ppp->layers)) { + n = list_entry(ppp->layers.next, typeof(*n), entry); + while (!list_empty(&n->items)) { + d = list_entry(n->items.next, typeof(*d), entry); + list_del(&d->entry); + d->layer->free(d); + } + list_del(&n->entry); + free(n); + } +} + +static void start_first_layer(struct ppp_t *ppp) +{ + struct layer_node_t *n; + struct ppp_layer_data_t *d; + + n=list_entry(ppp->layers.next,typeof(*n),entry); + list_for_each_entry(d,&n->items,entry) + { + d->starting=1; + d->layer->start(d); + } +} + +struct ppp_layer_data_t *ppp_find_layer_data(struct ppp_t *ppp, struct ppp_layer_t *layer) +{ + struct layer_node_t *n; + struct ppp_layer_data_t *d; + + list_for_each_entry(n,&ppp->layers,entry) + { + list_for_each_entry(d,&n->items,entry) + { + if (d->layer==layer) + return d; + } + } + + return NULL; +} + +int sock_fd; +static void __init ppp_init(void) +{ + sock_fd = socket(AF_INET, SOCK_DGRAM, 0); + if (sock_fd < 0) { + perror("socket"); + _exit(EXIT_FAILURE); + } +} diff --git a/accel-pptpd/ppp/ppp.h b/accel-pptpd/ppp/ppp.h new file mode 100644 index 0000000..d880fc2 --- /dev/null +++ b/accel-pptpd/ppp/ppp.h @@ -0,0 +1,138 @@ +#ifndef PPP_H +#define PPP_H + +#include + +#include "triton.h" +#include "list.h" + +/* + * Packet header = Code, id, length. + */ +#define PPP_HEADERLEN 4 +#define PPP_MTU 1500 + +/* + * Timeouts. + */ +#define DEFTIMEOUT 3 /* Timeout time in seconds */ +#define DEFMAXTERMREQS 2 /* Maximum Terminate-Request transmissions */ +#define DEFMAXCONFREQS 10 /* Maximum Configure-Request transmissions */ +#define DEFMAXNAKLOOPS 5 /* Maximum number of nak loops */ + +/* + * Protocol field values. + */ +#define PPP_IP 0x21 /* Internet Protocol */ +#define PPP_AT 0x29 /* AppleTalk Protocol */ +#define PPP_IPX 0x2b /* IPX protocol */ +#define PPP_VJC_COMP 0x2d /* VJ compressed TCP */ +#define PPP_VJC_UNCOMP 0x2f /* VJ uncompressed TCP */ +#define PPP_IPV6 0x57 /* Internet Protocol Version 6 */ +#define PPP_COMP 0xfd /* compressed packet */ +#define PPP_IPCP 0x8021 /* IP Control Protocol */ +#define PPP_ATCP 0x8029 /* AppleTalk Control Protocol */ +#define PPP_IPXCP 0x802b /* IPX Control Protocol */ +#define PPP_IPV6CP 0x8057 /* IPv6 Control Protocol */ +#define PPP_CCP 0x80fd /* Compression Control Protocol */ +#define PPP_ECP 0x8053 /* Encryption Control Protocol */ +#define PPP_LCP 0xc021 /* Link Control Protocol */ +#define PPP_PAP 0xc023 /* Password Authentication Protocol */ +#define PPP_LQR 0xc025 /* Link Quality Report protocol */ +#define PPP_CHAP 0xc223 /* Cryptographic Handshake Auth. Protocol */ +#define PPP_CBCP 0xc029 /* Callback Control Protocol */ +#define PPP_EAP 0xc227 /* Extensible Authentication Protocol */ + +#define PPP_LAYER_LCP 1 +#define PPP_LAYER_AUTH 2 +#define PPP_LAYER_CCP 3 +#define PPP_LAYER_IPCP 4 + +#define AUTH_MAX 3 + +struct ppp_t; + +struct ppp_ctrl_t +{ + struct triton_ctx_t *ctx; + void (*started)(struct ppp_t*); + void (*finished)(struct ppp_t*); +}; + +struct ppp_t +{ + struct triton_md_handler_t chan_hnd; + struct triton_md_handler_t unit_hnd; + int fd; + int chan_fd; + int unit_fd; + + int chan_idx; + int unit_idx; + + char *chan_name; + + struct ppp_ctrl_t *ctrl; + + int log:1; + + void *chan_buf; + int chan_buf_size; + void *unit_buf; + int unit_buf_size; + + struct list_head chan_handlers; + struct list_head unit_handlers; + + struct list_head layers; + + struct ppp_lcp_t *lcp; +}; + +struct ppp_layer_t; +struct layer_node_t; +struct ppp_layer_data_t +{ + struct list_head entry; + struct ppp_layer_t *layer; + struct layer_node_t *node; + int starting:1; + int started:1; +}; + +struct ppp_layer_t +{ + struct list_head entry; + struct ppp_layer_data_t *(*init)(struct ppp_t *); + void (*start)(struct ppp_layer_data_t*); + void (*finish)(struct ppp_layer_data_t*); + void (*free)(struct ppp_layer_data_t *); +}; + +struct ppp_handler_t +{ + struct list_head entry; + int proto; + void (*recv)(struct ppp_handler_t*); +}; + +struct ppp_t *alloc_ppp(void); +int establish_ppp(struct ppp_t *ppp); +int ppp_chan_send(struct ppp_t *ppp, void *data, int size); +int ppp_unit_send(struct ppp_t *ppp, void *data, int size); + +struct ppp_fsm_t* ppp_lcp_init(struct ppp_t *ppp); +void ppp_layer_started(struct ppp_t *ppp,struct ppp_layer_data_t*); +void ppp_layer_finished(struct ppp_t *ppp,struct ppp_layer_data_t*); +void ppp_terminate(struct ppp_t *ppp, int hard); + +void ppp_register_chan_handler(struct ppp_t *, struct ppp_handler_t *); +void ppp_register_unit_handler(struct ppp_t * ,struct ppp_handler_t *); +void ppp_unregister_handler(struct ppp_t *, struct ppp_handler_t *); + +int ppp_register_layer(const char *name, struct ppp_layer_t *); +void ppp_unregister_layer(struct ppp_layer_t *); +struct ppp_layer_data_t *ppp_find_layer_data(struct ppp_t *, struct ppp_layer_t *); + +extern int sock_fd; // internet socket for ioctls +#endif diff --git a/accel-pptpd/ppp/ppp_auth.c b/accel-pptpd/ppp/ppp_auth.c new file mode 100644 index 0000000..32f63d5 --- /dev/null +++ b/accel-pptpd/ppp/ppp_auth.c @@ -0,0 +1,320 @@ +#include +#include +#include + +#include "ppp.h" +#include "ppp_lcp.h" +#include "log.h" + +#include "ppp_auth.h" + + +static LIST_HEAD(auth_handlers); +static int extra_opt_len=0; + +static struct lcp_option_t *auth_init(struct ppp_lcp_t *lcp); +static void auth_free(struct ppp_lcp_t *lcp, struct lcp_option_t *opt); +static int auth_send_conf_req(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr); +static int auth_recv_conf_req(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr); +static int auth_recv_conf_nak(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr); +static int auth_recv_conf_rej(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr); +static int auth_recv_conf_ack(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr); +static void auth_print(void (*print)(const char *fmt,...),struct lcp_option_t*, uint8_t *ptr); + +static struct ppp_layer_data_t *auth_layer_init(struct ppp_t*); +static void auth_layer_start(struct ppp_layer_data_t *); +static void auth_layer_finish(struct ppp_layer_data_t *); +static void auth_layer_free(struct ppp_layer_data_t *); + +struct auth_option_t +{ + struct lcp_option_t opt; + struct list_head auth_list; + struct auth_data_t *auth; + struct auth_data_t *peer_auth; +}; + +struct auth_layer_data_t +{ + struct ppp_layer_data_t ld; + struct auth_option_t auth_opt; + struct ppp_t *ppp; +}; + +static struct lcp_option_handler_t auth_opt_hnd= +{ + .init=auth_init, + .send_conf_req=auth_send_conf_req, + .send_conf_nak=auth_send_conf_req, + .recv_conf_req=auth_recv_conf_req, + .recv_conf_nak=auth_recv_conf_nak, + .recv_conf_rej=auth_recv_conf_rej, + .recv_conf_ack=auth_recv_conf_ack, + .free=auth_free, + .print=auth_print, +}; + +static struct ppp_layer_t auth_layer= +{ + .init=auth_layer_init, + .start=auth_layer_start, + .finish=auth_layer_finish, + .free=auth_layer_free, +}; + +static struct lcp_option_t *auth_init(struct ppp_lcp_t *lcp) +{ + struct ppp_auth_handler_t *h; + struct auth_data_t *d; + struct auth_layer_data_t *ad; + + ad=container_of(ppp_find_layer_data(lcp->ppp,&auth_layer),typeof(*ad),ld); + + ad->auth_opt.opt.id=CI_AUTH; + ad->auth_opt.opt.len=4+extra_opt_len; + + INIT_LIST_HEAD(&ad->auth_opt.auth_list); + + list_for_each_entry(h,&auth_handlers,entry) + { + d=h->init(lcp->ppp); + d->h=h; + list_add_tail(&d->entry,&ad->auth_opt.auth_list); + } + + return &ad->auth_opt.opt; +} + +static void auth_free(struct ppp_lcp_t *lcp, struct lcp_option_t *opt) +{ + struct auth_option_t *auth_opt=container_of(opt,typeof(*auth_opt),opt); + struct auth_data_t *d; + + while(!list_empty(&auth_opt->auth_list)) + { + d=list_entry(auth_opt->auth_list.next,typeof(*d),entry); + list_del(&d->entry); + d->h->free(lcp->ppp,d); + } +} + +static int auth_send_conf_req(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr) +{ + struct auth_option_t *auth_opt=container_of(opt,typeof(*auth_opt),opt); + struct lcp_opt16_t *opt16=(struct lcp_opt16_t*)ptr; + struct auth_data_t *d; + int n; + + if (list_empty(&auth_opt->auth_list)) return 0; + + if (!auth_opt->auth || auth_opt->auth->state==LCP_OPT_NAK) + { + list_for_each_entry(d,&auth_opt->auth_list,entry) + { + if (d->state==LCP_OPT_NAK || d->state==LCP_OPT_REJ) + continue; + auth_opt->auth=d; + break; + } + } + + opt16->hdr.id=CI_AUTH; + opt16->val=htons(auth_opt->auth->proto); + n=auth_opt->auth->h->send_conf_req(lcp->ppp,auth_opt->auth,(uint8_t*)(opt16+1)); + opt16->hdr.len=4+n; + + return 4+n; +} + +static int auth_recv_conf_req(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr) +{ + struct auth_option_t *auth_opt=container_of(opt,typeof(*auth_opt),opt); + struct lcp_opt16_t *opt16=(struct lcp_opt16_t*)ptr; + struct auth_data_t *d; + int r; + + if (list_empty(&auth_opt->auth_list)) + return LCP_OPT_REJ; + + list_for_each_entry(d,&auth_opt->auth_list,entry) + { + if (d->proto==ntohs(opt16->val)) + { + r=d->h->recv_conf_req(lcp->ppp,d,(uint8_t*)(opt16+1)); + if (r==LCP_OPT_FAIL) + return LCP_OPT_FAIL; + if (r==LCP_OPT_REJ) + break; + auth_opt->peer_auth=d; + return r; + } + } + + list_for_each_entry(d,&auth_opt->auth_list,entry) + { + if (d->state!=LCP_OPT_NAK) + { + auth_opt->peer_auth=d; + return LCP_OPT_NAK; + } + } + + log_msg("cann't negotiate authentication type\n"); + return LCP_OPT_FAIL; +} + +static int auth_recv_conf_ack(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr) +{ + struct auth_option_t *auth_opt=container_of(opt,typeof(*auth_opt),opt); + + auth_opt->peer_auth=NULL; + + return 0; +} + +static int auth_recv_conf_nak(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr) +{ + struct auth_option_t *auth_opt=container_of(opt,typeof(*auth_opt),opt); + struct auth_data_t *d; + + if (!auth_opt->auth) + { + log_error("auth: unexcepcted configure-nak\n"); + return -1; + } + auth_opt->auth->state=LCP_OPT_NAK; + if (auth_opt->peer_auth) + auth_opt->auth=auth_opt->peer_auth; + + list_for_each_entry(d,&auth_opt->auth_list,entry) + { + if (d->state!=LCP_OPT_NAK) + return 0; + } + + log_msg("cann't negotiate authentication type\n"); + return -1; +} + +static int auth_recv_conf_rej(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr) +{ + struct auth_option_t *auth_opt=container_of(opt,typeof(*auth_opt),opt); + struct auth_data_t *d; + + if (!auth_opt->auth) + { + log_error("auth: unexcepcted configure-reject\n"); + return -1; + } + auth_opt->auth->state=LCP_OPT_NAK; + if (auth_opt->peer_auth) + auth_opt->auth=auth_opt->peer_auth; + + list_for_each_entry(d,&auth_opt->auth_list,entry) + { + if (d->state!=LCP_OPT_NAK) + return 0; + } + + log_msg("cann't negotiate authentication type\n"); + return -1; +} + +static void auth_print(void (*print)(const char *fmt,...),struct lcp_option_t *opt, uint8_t *ptr) +{ + struct auth_option_t *auth_opt=container_of(opt,typeof(*auth_opt),opt); + struct lcp_opt16_t *opt16=(struct lcp_opt16_t*)ptr; + struct auth_data_t *d; + + if (ptr) + { + list_for_each_entry(d,&auth_opt->auth_list,entry) + { + if (d->proto==ntohs(opt16->val)) + goto print_d; + } + + print("",ntohs(opt16->val)); + return; + } + else if (auth_opt->auth) d=auth_opt->auth; + else return; + +print_d: + print("",d->h->name); +} + +static struct ppp_layer_data_t *auth_layer_init(struct ppp_t *ppp) +{ + struct auth_layer_data_t *ad=(struct auth_layer_data_t*)malloc(sizeof(*ad)); + + log_debug("auth_layer_init\n"); + + memset(ad,0,sizeof(*ad)); + + ad->ppp=ppp; + + return &ad->ld; +} + +static void auth_layer_start(struct ppp_layer_data_t *ld) +{ + struct auth_layer_data_t *ad=container_of(ld,typeof(*ad),ld); + + log_debug("auth_layer_start\n"); + + if (ad->auth_opt.auth) + ad->auth_opt.auth->h->start(ad->ppp,ad->auth_opt.auth); + else + { + log_debug("auth_layer_started\n"); + ppp_layer_started(ad->ppp,ld); + } +} + +static void auth_layer_finish(struct ppp_layer_data_t *ld) +{ + struct auth_layer_data_t *ad=container_of(ld,typeof(*ad),ld); + + log_debug("auth_layer_finish\n"); + + if (ad->auth_opt.auth) + ad->auth_opt.auth->h->finish(ad->ppp,ad->auth_opt.auth); + + log_debug("auth_layer_finished\n"); + ppp_layer_finished(ad->ppp,ld); +} + +static void auth_layer_free(struct ppp_layer_data_t *ld) +{ + struct auth_layer_data_t *ad=container_of(ld,typeof(*ad),ld); + + log_debug("auth_layer_free\n"); + + free(ad); +} + +void __export auth_successed(struct ppp_t *ppp) +{ + struct auth_layer_data_t *ad=container_of(ppp_find_layer_data(ppp,&auth_layer),typeof(*ad),ld); + log_debug("auth_layer_started\n"); + ppp_layer_started(ppp,&ad->ld); +} + +void __export auth_failed(struct ppp_t *ppp) +{ + ppp_terminate(ppp, 0); +} + +int __export ppp_auth_register_handler(struct ppp_auth_handler_t *h) +{ + list_add_tail(&h->entry,&auth_handlers); + return 0; +} + +static void __init ppp_auth_init() +{ + ppp_register_layer("auth",&auth_layer); + lcp_option_register(&auth_opt_hnd); +} + diff --git a/accel-pptpd/ppp/ppp_auth.h b/accel-pptpd/ppp/ppp_auth.h new file mode 100644 index 0000000..f1880d5 --- /dev/null +++ b/accel-pptpd/ppp/ppp_auth.h @@ -0,0 +1,34 @@ +#ifndef PPP_AUTH_H +#define PPP_AUTH_H + +#include "list.h" + +struct ppp_auth_handler_t; + +struct auth_data_t +{ + struct list_head entry; + int proto; + int state; + struct ppp_auth_handler_t *h; +}; + +struct ppp_auth_handler_t +{ + struct list_head entry; + const char *name; + struct auth_data_t* (*init)(struct ppp_t*); + int (*send_conf_req)(struct ppp_t*, struct auth_data_t*, uint8_t*); + int (*recv_conf_req)(struct ppp_t*, struct auth_data_t*, uint8_t*); + int (*start)(struct ppp_t*, struct auth_data_t*); + int (*finish)(struct ppp_t*, struct auth_data_t*); + void (*free)(struct ppp_t*,struct auth_data_t*); +}; + +int ppp_auth_register_handler(struct ppp_auth_handler_t*); + +void auth_successed(struct ppp_t *ppp); +void auth_failed(struct ppp_t *ppp); + +#endif + diff --git a/accel-pptpd/ppp/ppp_ccp.c b/accel-pptpd/ppp/ppp_ccp.c new file mode 100644 index 0000000..fe10886 --- /dev/null +++ b/accel-pptpd/ppp/ppp_ccp.c @@ -0,0 +1,577 @@ +#include +#include +#include +#include +#include + +#include "triton.h" + +#include "log.h" + +#include "ppp.h" +#include "ppp_ccp.h" + +struct recv_opt_t +{ + struct list_head entry; + struct ccp_opt_hdr_t *hdr; + int len; + int state; + struct ccp_option_t *lopt; +}; + +static LIST_HEAD(option_handlers); + +static void ccp_layer_up(struct ppp_fsm_t*); +static void ccp_layer_down(struct ppp_fsm_t*); +static void send_conf_req(struct ppp_fsm_t*); +static void send_conf_ack(struct ppp_fsm_t*); +static void send_conf_nak(struct ppp_fsm_t*); +static void send_conf_rej(struct ppp_fsm_t*); +static void ccp_recv(struct ppp_handler_t*); + +static void ccp_options_init(struct ppp_ccp_t *ccp) +{ + struct ccp_option_t *lopt; + struct ccp_option_handler_t *h; + + INIT_LIST_HEAD(&ccp->options); + + list_for_each_entry(h,&option_handlers,entry) + { + lopt=h->init(ccp); + if (lopt) + { + lopt->h=h; + list_add_tail(&lopt->entry,&ccp->options); + ccp->conf_req_len+=lopt->len; + } + } +} + +static void ccp_options_free(struct ppp_ccp_t *ccp) +{ + struct ccp_option_t *lopt; + + while(!list_empty(&ccp->options)) + { + lopt=list_entry(ccp->options.next,typeof(*lopt),entry); + list_del(&lopt->entry); + lopt->h->free(ccp,lopt); + } +} + +static struct ppp_layer_data_t *ccp_layer_init(struct ppp_t *ppp) +{ + struct ppp_ccp_t *ccp=malloc(sizeof(*ccp)); + memset(ccp,0,sizeof(*ccp)); + + log_debug("ccp_layer_init\n"); + + ccp->ppp=ppp; + ccp->fsm.ppp=ppp; + + ccp->hnd.proto=PPP_CCP; + ccp->hnd.recv=ccp_recv; + + ppp_register_unit_handler(ppp,&ccp->hnd); + + ppp_fsm_init(&ccp->fsm); + + ccp->fsm.layer_up=ccp_layer_up; + ccp->fsm.layer_finished=ccp_layer_down; + ccp->fsm.send_conf_req=send_conf_req; + ccp->fsm.send_conf_ack=send_conf_ack; + ccp->fsm.send_conf_nak=send_conf_nak; + ccp->fsm.send_conf_rej=send_conf_rej; + + INIT_LIST_HEAD(&ccp->ropt_list); + + return &ccp->ld; +} + +void ccp_layer_start(struct ppp_layer_data_t *ld) +{ + struct ppp_ccp_t *ccp=container_of(ld,typeof(*ccp),ld); + + log_debug("ccp_layer_start\n"); + + ccp_options_init(ccp); + ppp_fsm_lower_up(&ccp->fsm); + ppp_fsm_open(&ccp->fsm); +} + +void ccp_layer_finish(struct ppp_layer_data_t *ld) +{ + struct ppp_ccp_t *ccp=container_of(ld,typeof(*ccp),ld); + + log_debug("ccp_layer_finish\n"); + + ppp_fsm_lower_down(&ccp->fsm); + + ppp_unregister_handler(ccp->ppp,&ccp->hnd); + ccp_options_free(ccp); + + ppp_layer_finished(ccp->ppp,ld); +} + +void ccp_layer_free(struct ppp_layer_data_t *ld) +{ + struct ppp_ccp_t *ccp=container_of(ld,typeof(*ccp),ld); + + log_debug("ccp_layer_free\n"); + + free(ccp); +} + +static void ccp_layer_up(struct ppp_fsm_t *fsm) +{ + struct ppp_ccp_t *ccp=container_of(fsm,typeof(*ccp),fsm); + log_debug("ccp_layer_started\n"); + ppp_layer_started(ccp->ppp,&ccp->ld); +} + +static void ccp_layer_down(struct ppp_fsm_t *fsm) +{ + struct ppp_ccp_t *ccp=container_of(fsm,typeof(*ccp),fsm); + log_debug("ccp_layer_finished\n"); + ppp_layer_finished(ccp->ppp,&ccp->ld); +} + +static void print_ropt(struct recv_opt_t *ropt) +{ + int i; + uint8_t *ptr=(uint8_t*)ropt->hdr; + + log_debug(" <"); + for(i=0; ilen; i++) + { + log_debug(" %x",ptr[i]); + } + log_debug(" >"); +} + +static void send_conf_req(struct ppp_fsm_t *fsm) +{ + struct ppp_ccp_t *ccp=container_of(fsm,typeof(*ccp),fsm); + uint8_t *buf=malloc(ccp->conf_req_len), *ptr=buf; + struct ccp_hdr_t *ccp_hdr=(struct ccp_hdr_t*)ptr; + struct ccp_option_t *lopt; + int n; + + log_debug("send [CCP ConfReq"); + ccp_hdr->proto=htons(PPP_CCP); + ccp_hdr->code=CONFREQ; + ccp_hdr->id=++ccp->fsm.id; + ccp_hdr->len=0; + log_debug(" id=%x",ccp_hdr->id); + + ptr+=sizeof(*ccp_hdr); + + list_for_each_entry(lopt,&ccp->options,entry) + { + n=lopt->h->send_conf_req(ccp,lopt,ptr); + if (n) + { + log_debug(" "); + lopt->h->print(log_debug,lopt,NULL); + ptr+=n; + } + } + + log_debug("]\n"); + + ccp_hdr->len=htons((ptr-buf)-2); + ppp_unit_send(ccp->ppp,ccp_hdr,ptr-buf); +} + +static void send_conf_ack(struct ppp_fsm_t *fsm) +{ + struct ppp_ccp_t *ccp=container_of(fsm,typeof(*ccp),fsm); + struct ccp_hdr_t *hdr=(struct ccp_hdr_t*)ccp->ppp->unit_buf; + + hdr->code=CONFACK; + log_debug("send [CCP ConfAck id=%x ]\n",ccp->fsm.recv_id); + + ppp_unit_send(ccp->ppp,hdr,ntohs(hdr->len)+2); +} + +static void send_conf_nak(struct ppp_fsm_t *fsm) +{ + struct ppp_ccp_t *ccp=container_of(fsm,typeof(*ccp),fsm); + uint8_t *buf=malloc(ccp->conf_req_len), *ptr=buf; + struct ccp_hdr_t *ccp_hdr=(struct ccp_hdr_t*)ptr; + struct recv_opt_t *ropt; + + log_debug("send [CCP ConfNak id=%x",ccp->fsm.recv_id); + + ccp_hdr->proto=htons(PPP_CCP); + ccp_hdr->code=CONFNAK; + ccp_hdr->id=ccp->fsm.recv_id; + ccp_hdr->len=0; + + ptr+=sizeof(*ccp_hdr); + + list_for_each_entry(ropt,&ccp->ropt_list,entry) + { + if (ropt->state==CCP_OPT_NAK) + { + log_debug(" "); + ropt->lopt->h->print(log_debug,ropt->lopt,NULL); + ptr+=ropt->lopt->h->send_conf_nak(ccp,ropt->lopt,ptr); + } + } + + log_debug("]\n"); + + ccp_hdr->len=htons((ptr-buf)-2); + ppp_unit_send(ccp->ppp,ccp_hdr,ptr-buf); +} + +static void send_conf_rej(struct ppp_fsm_t *fsm) +{ + struct ppp_ccp_t *ccp=container_of(fsm,typeof(*ccp),fsm); + uint8_t *buf=malloc(ccp->ropt_len), *ptr=buf; + struct ccp_hdr_t *ccp_hdr=(struct ccp_hdr_t*)ptr; + struct recv_opt_t *ropt; + + log_debug("send [CCP ConfRej id=%x ",ccp->fsm.recv_id); + + ccp_hdr->proto=htons(PPP_CCP); + ccp_hdr->code=CONFREJ; + ccp_hdr->id=ccp->fsm.recv_id; + ccp_hdr->len=0; + + ptr+=sizeof(*ccp_hdr); + + list_for_each_entry(ropt,&ccp->ropt_list,entry) + { + if (ropt->state==CCP_OPT_REJ) + { + log_debug(" "); + if (ropt->lopt) ropt->lopt->h->print(log_debug,ropt->lopt,(uint8_t*)ropt->hdr); + else print_ropt(ropt); + memcpy(ptr,ropt->hdr,ropt->len); + ptr+=ropt->len; + } + } + + log_debug("]\n"); + + ccp_hdr->len=htons((ptr-buf)-2); + ppp_unit_send(ccp->ppp,ccp_hdr,ptr-buf); +} + +static int ccp_recv_conf_req(struct ppp_ccp_t *ccp,uint8_t *data,int size) +{ + struct ccp_opt_hdr_t *hdr; + struct recv_opt_t *ropt; + struct ccp_option_t *lopt; + int r,ret=1,ack=0; + + ccp->ropt_len=size; + + while(size>0) + { + hdr=(struct ccp_opt_hdr_t *)data; + + ropt=malloc(sizeof(*ropt)); + memset(ropt,0,sizeof(*ropt)); + if (hdr->len>size) ropt->len=size; + else ropt->len=hdr->len; + ropt->hdr=hdr; + ropt->state=CCP_OPT_NONE; + list_add_tail(&ropt->entry,&ccp->ropt_list); + + data+=ropt->len; + size-=ropt->len; + } + + list_for_each_entry(lopt,&ccp->options,entry) + lopt->state=CCP_OPT_NONE; + + log_debug("recv [CCP ConfReq id=%x",ccp->fsm.recv_id); + list_for_each_entry(ropt,&ccp->ropt_list,entry) + { + list_for_each_entry(lopt,&ccp->options,entry) + { + if (lopt->id==ropt->hdr->id) + { + log_debug(" "); + lopt->h->print(log_debug,lopt,(uint8_t*)ropt->hdr); + r=lopt->h->recv_conf_req(ccp,lopt,(uint8_t*)ropt->hdr); + if (ack) + { + lopt->state=CCP_OPT_REJ; + ropt->state=CCP_OPT_REJ; + }else + { + lopt->state=r; + ropt->state=r; + } + ropt->lopt=lopt; + if (rstate==CCP_OPT_ACK || ropt->state==CCP_OPT_NAK) + ack=1; + else if (!ropt->lopt) + { + log_debug(" "); + print_ropt(ropt); + ropt->state=CCP_OPT_REJ; + ret=CCP_OPT_REJ; + } + } + log_debug("]\n"); + + /*list_for_each_entry(lopt,&ccp->options,entry) + { + if (lopt->state==CCP_OPT_NONE) + { + r=lopt->h->recv_conf_req(ccp,lopt,NULL); + lopt->state=r; + if (rropt_list)) + { + ropt=list_entry(ccp->ropt_list.next,typeof(*ropt),entry); + list_del(&ropt->entry); + free(ropt); + } +} + +static int ccp_recv_conf_rej(struct ppp_ccp_t *ccp,uint8_t *data,int size) +{ + struct ccp_opt_hdr_t *hdr; + struct ccp_option_t *lopt; + int res=0; + + log_debug("recv [CCP ConfRej id=%x",ccp->fsm.recv_id); + + if (ccp->fsm.recv_id!=ccp->fsm.id) + { + log_debug(": id mismatch ]\n"); + return 0; + } + + while(size>0) + { + hdr=(struct ccp_opt_hdr_t *)data; + + list_for_each_entry(lopt,&ccp->options,entry) + { + if (lopt->id==hdr->id) + { + if (!lopt->h->recv_conf_rej) + res=-1; + else if (lopt->h->recv_conf_rej(ccp,lopt,data)) + res=-1; + break; + } + } + + data+=hdr->len; + size-=hdr->len; + } + log_debug("]\n"); + return res; +} + +static int ccp_recv_conf_nak(struct ppp_ccp_t *ccp,uint8_t *data,int size) +{ + struct ccp_opt_hdr_t *hdr; + struct ccp_option_t *lopt; + int res=0; + + log_debug("recv [CCP ConfNak id=%x",ccp->fsm.recv_id); + + if (ccp->fsm.recv_id!=ccp->fsm.id) + { + log_debug(": id mismatch ]\n"); + return 0; + } + + while(size>0) + { + hdr=(struct ccp_opt_hdr_t *)data; + + list_for_each_entry(lopt,&ccp->options,entry) + { + if (lopt->id==hdr->id) + { + log_debug(" "); + lopt->h->print(log_debug,lopt,data); + if (lopt->h->recv_conf_nak(ccp,lopt,data)) + res=-1; + break; + } + } + + data+=hdr->len; + size-=hdr->len; + } + log_debug("]\n"); + return res; +} + +static int ccp_recv_conf_ack(struct ppp_ccp_t *ccp,uint8_t *data,int size) +{ + struct ccp_opt_hdr_t *hdr; + struct ccp_option_t *lopt; + int res=0; + + log_debug("recv [CCP ConfAck id=%x",ccp->fsm.recv_id); + + if (ccp->fsm.recv_id!=ccp->fsm.id) + { + log_debug(": id mismatch ]\n"); + return 0; + } + + while(size>0) + { + hdr=(struct ccp_opt_hdr_t *)data; + + list_for_each_entry(lopt,&ccp->options,entry) + { + if (lopt->id==hdr->id) + { + log_debug(" "); + lopt->h->print(log_debug,lopt,data); + if (!lopt->h->recv_conf_ack) + break; + if (lopt->h->recv_conf_ack(ccp,lopt,data)) + res=-1; + break; + } + } + + data+=hdr->len; + size-=hdr->len; + } + log_debug("]\n"); + return res; +} + +static void ccp_recv(struct ppp_handler_t*h) +{ + struct ccp_hdr_t *hdr; + struct ppp_ccp_t *ccp=container_of(h,typeof(*ccp),hnd); + int r; + char *term_msg; + + if (ccp->fsm.fsm_state==FSM_Initial || ccp->fsm.fsm_state==FSM_Closed) + { + log_error("CCP: discaring packet\n"); + return; + } + + if (ccp->ppp->unit_buf_sizeppp->unit_buf; + if (ntohs(hdr->len)fsm.recv_id=hdr->id; + switch(hdr->code) + { + case CONFREQ: + r=ccp_recv_conf_req(ccp,(uint8_t*)(hdr+1),ntohs(hdr->len)-PPP_HDRLEN); + switch(r) + { + case CCP_OPT_ACK: + ppp_fsm_recv_conf_req_ack(&ccp->fsm); + break; + case CCP_OPT_NAK: + ppp_fsm_recv_conf_req_nak(&ccp->fsm); + break; + case CCP_OPT_REJ: + ppp_fsm_recv_conf_req_rej(&ccp->fsm); + break; + } + ccp_free_conf_req(ccp); + if (r==CCP_OPT_FAIL) + ppp_terminate(ccp->ppp, 0); + break; + case CONFACK: + if (ccp_recv_conf_ack(ccp,(uint8_t*)(hdr+1),ntohs(hdr->len)-PPP_HDRLEN)) + ppp_terminate(ccp->ppp, 0); + else + ppp_fsm_recv_conf_ack(&ccp->fsm); + break; + case CONFNAK: + ccp_recv_conf_nak(ccp,(uint8_t*)(hdr+1),ntohs(hdr->len)-PPP_HDRLEN); + ppp_fsm_recv_conf_rej(&ccp->fsm); + break; + case CONFREJ: + if (ccp_recv_conf_rej(ccp,(uint8_t*)(hdr+1),ntohs(hdr->len)-PPP_HDRLEN)) + ppp_terminate(ccp->ppp, 0); + else + ppp_fsm_recv_conf_rej(&ccp->fsm); + break; + case TERMREQ: + term_msg=strndup((char*)(hdr+1),ntohs(hdr->len)); + log_debug("recv [CCP TermReq id=%x \"%s\"]\n",hdr->id,term_msg); + free(term_msg); + ppp_fsm_recv_term_req(&ccp->fsm); + ppp_terminate(ccp->ppp, 0); + break; + case TERMACK: + term_msg=strndup((char*)(hdr+1),ntohs(hdr->len)); + log_debug("recv [CCP TermAck id=%x \"%s\"]\n",hdr->id,term_msg); + free(term_msg); + ppp_fsm_recv_term_ack(&ccp->fsm); + break; + case CODEREJ: + log_debug("recv [CCP CodeRej id=%x]\n",hdr->id); + ppp_fsm_recv_code_rej_bad(&ccp->fsm); + break; + default: + ppp_fsm_recv_unk(&ccp->fsm); + break; + } +} + +int ccp_option_register(struct ccp_option_handler_t *h) +{ + /*struct ccp_option_drv_t *p; + + list_for_each_entry(p,option_drv_list,entry) + if (p->id==h->id) + return -1;*/ + + list_add_tail(&h->entry,&option_handlers); + + return 0; +} + +static struct ppp_layer_t ccp_layer= +{ + .init=ccp_layer_init, + .start=ccp_layer_start, + .finish=ccp_layer_finish, + .free=ccp_layer_free, +}; + +static void __init ccp_init(void) +{ + ppp_register_layer("ccp",&ccp_layer); +} diff --git a/accel-pptpd/ppp/ppp_ccp.h b/accel-pptpd/ppp/ppp_ccp.h new file mode 100644 index 0000000..3e45969 --- /dev/null +++ b/accel-pptpd/ppp/ppp_ccp.h @@ -0,0 +1,94 @@ +#ifndef PPP_CCP_H +#define PPP_CCP_H + +#include + +#include "triton.h" +#include "ppp_fsm.h" +/* + * Options. + */ +#define CI_COMP 2 /* IP-Compress-Protocol */ +#define CI_ADDR 3 /* IP-Address */ +#define CI_DNS1 129 /* Primary-DNS-Address */ +#define CI_DNS2 131 /* Secondary-DNS-Address */ + +struct ccp_hdr_t +{ + uint16_t proto; + uint8_t code; + uint8_t id; + uint16_t len; +} __attribute__((packed)); +struct ccp_opt_hdr_t +{ + uint8_t id; + uint8_t len; +} __attribute__((packed)); +struct ccp_opt8_t +{ + struct ccp_opt_hdr_t hdr; + uint8_t val; +} __attribute__((packed)); +struct ccp_opt16_t +{ + struct ccp_opt_hdr_t hdr; + uint16_t val; +} __attribute__((packed)); +struct ccp_opt32_t +{ + struct ccp_opt_hdr_t hdr; + uint32_t val; +} __attribute__((packed)); + +#define CCP_OPT_NONE 0 +#define CCP_OPT_ACK 1 +#define CCP_OPT_NAK -1 +#define CCP_OPT_REJ -2 +#define CCP_OPT_FAIL -3 + +struct ppp_ccp_t; +struct ccp_option_handler_t; + +struct ccp_option_t +{ + struct list_head entry; + int id; + int len; + int state; + struct ccp_option_handler_t *h; +}; + +struct ccp_option_handler_t +{ + struct list_head entry; + struct ccp_option_t* (*init)(struct ppp_ccp_t*); + int (*send_conf_req)(struct ppp_ccp_t*,struct ccp_option_t*,uint8_t*); + int (*send_conf_rej)(struct ppp_ccp_t*,struct ccp_option_t*,uint8_t*); + int (*send_conf_nak)(struct ppp_ccp_t*,struct ccp_option_t*,uint8_t*); + int (*recv_conf_req)(struct ppp_ccp_t*,struct ccp_option_t*,uint8_t*); + int (*recv_conf_rej)(struct ppp_ccp_t*,struct ccp_option_t*,uint8_t*); + int (*recv_conf_nak)(struct ppp_ccp_t*,struct ccp_option_t*,uint8_t*); + int (*recv_conf_ack)(struct ppp_ccp_t*,struct ccp_option_t*,uint8_t*); + void (*free)(struct ppp_ccp_t*,struct ccp_option_t*); + void (*print)(void (*print)(const char *fmt,...), struct ccp_option_t*,uint8_t*); +}; + +struct ppp_ccp_t +{ + struct ppp_layer_data_t ld; + struct ppp_handler_t hnd; + struct ppp_fsm_t fsm; + struct ppp_t *ppp; + struct list_head options; + + struct list_head ropt_list; // last received ConfReq + int ropt_len; + + int conf_req_len; +}; + +int ccp_option_register(struct ccp_option_handler_t *h); + +#endif + diff --git a/accel-pptpd/ppp/ppp_fsm.c b/accel-pptpd/ppp/ppp_fsm.c new file mode 100644 index 0000000..85cf15a --- /dev/null +++ b/accel-pptpd/ppp/ppp_fsm.c @@ -0,0 +1,534 @@ +#include +#include + +#include "triton.h" + +#include "ppp.h" +#include "ppp_fsm.h" +#include "ppp_lcp.h" +#include "log.h" + +static int conf_max_terminate = 2; +static int conf_max_configure = 5; +static int conf_max_failure = 5; +static int conf_timeout = 3; + +void send_term_req(struct ppp_fsm_t *layer); +void send_term_ack(struct ppp_fsm_t *layer); +void send_echo_reply(struct ppp_fsm_t *layer); + +static void init_req_counter(struct ppp_fsm_t *layer,int timeout); +static void zero_req_counter(struct ppp_fsm_t *layer); +static void restart_timer_func(struct triton_timer_t *t); +static void stop_timer(struct ppp_fsm_t *fsm); + +void ppp_fsm_init(struct ppp_fsm_t *layer) +{ + layer->fsm_state = FSM_Initial; + layer->restart_timer.expire = restart_timer_func; + layer->restart_counter = 0; + + layer->max_terminate = conf_max_terminate; + layer->max_configure = conf_max_configure; + layer->max_failure = conf_max_failure; + layer->timeout = conf_timeout; +} +void ppp_fsm_free(struct ppp_fsm_t *layer) +{ + stop_timer(layer); +} + +void ppp_fsm_lower_up(struct ppp_fsm_t *layer) +{ + switch(layer->fsm_state) + { + case FSM_Initial: + layer->fsm_state=FSM_Closed; + break; + case FSM_Starting: + //if (layer->init_req_cnt) layer->init_req_cnt(layer); + init_req_counter(layer,layer->max_configure); + --layer->restart_counter; + if (layer->send_conf_req) layer->send_conf_req(layer); + layer->fsm_state=FSM_Req_Sent; + break; + default: + break; + } +} + +void ppp_fsm_lower_down(struct ppp_fsm_t *layer) +{ + switch(layer->fsm_state) + { + case FSM_Closed: + case FSM_Closing: + layer->fsm_state=FSM_Initial; + break; + case FSM_Stopped: + if (layer->layer_started) layer->layer_started(layer); + layer->fsm_state=FSM_Starting; + break; + case FSM_Stopping: + case FSM_Req_Sent: + case FSM_Ack_Rcvd: + case FSM_Ack_Sent: + layer->fsm_state=FSM_Starting; + break; + case FSM_Opened: + if (layer->layer_down) layer->layer_down(layer); + layer->fsm_state=FSM_Starting; + break; + default: + break; + } +} + +void ppp_fsm_open(struct ppp_fsm_t *layer) +{ + switch(layer->fsm_state) + { + case FSM_Initial: + if (layer->layer_started) layer->layer_started(layer); + layer->fsm_state=FSM_Starting; + break; + case FSM_Starting: + break; + case FSM_Closed: + //if (layer->init_req_cnt) layer->init_req_cnt(layer); + init_req_counter(layer,layer->max_configure); + --layer->restart_counter; + if (layer->send_conf_req) layer->send_conf_req(layer); + layer->fsm_state=FSM_Req_Sent; + break; + case FSM_Closing: + case FSM_Stopping: + case FSM_Stopped: + case FSM_Opened: + ppp_fsm_lower_down(layer); + ppp_fsm_lower_up(layer); + break; + default: + break; + } +} + +void ppp_fsm_close(struct ppp_fsm_t *layer) +{ + switch(layer->fsm_state) + { + case FSM_Starting: + if (layer->layer_finished) layer->layer_finished(layer); + layer->fsm_state=FSM_Initial; + break; + case FSM_Stopped: + layer->fsm_state=FSM_Closed; + break; + case FSM_Stopping: + layer->fsm_state=FSM_Closing; + break; + case FSM_Opened: + if (layer->layer_down) layer->layer_down(layer); + case FSM_Req_Sent: + case FSM_Ack_Rcvd: + case FSM_Ack_Sent: + //if (layer->init_req_cnt) layer->init_req_cnt(layer); + init_req_counter(layer,layer->max_terminate); + send_term_req(layer); + layer->fsm_state=FSM_Closing; + break; + default: + break; + } +} + +void ppp_fsm_timeout0(struct ppp_fsm_t *layer) +{ + switch(layer->fsm_state) + { + case FSM_Closing: + case FSM_Stopping: + send_term_req(layer); + break; + case FSM_Ack_Rcvd: + layer->fsm_state=FSM_Req_Sent; + case FSM_Req_Sent: + case FSM_Ack_Sent: + --layer->restart_counter; + if (layer->send_conf_req) layer->send_conf_req(layer); + break; + default: + break; + } +} + +void ppp_fsm_timeout1(struct ppp_fsm_t *layer) +{ + switch(layer->fsm_state) + { + case FSM_Closing: + stop_timer(layer); + if (layer->layer_finished) layer->layer_finished(layer); + layer->fsm_state=FSM_Closed; + break; + case FSM_Stopping: + stop_timer(layer); + if (layer->layer_finished) layer->layer_finished(layer); + layer->fsm_state=FSM_Stopped; + break; + case FSM_Ack_Rcvd: + case FSM_Req_Sent: + case FSM_Ack_Sent: + stop_timer(layer); + if (layer->layer_finished) layer->layer_finished(layer); + layer->fsm_state=FSM_Stopped; + break; + default: + break; + } +} + +void ppp_fsm_recv_conf_req_ack(struct ppp_fsm_t *layer) +{ + switch(layer->fsm_state) + { + case FSM_Closed: + send_term_ack(layer); + break; + case FSM_Stopped: + //if (layer->init_req_cnt) layer->init_req_cnt(layer); + init_req_counter(layer,layer->max_configure); + --layer->restart_counter; + if (layer->send_conf_req) layer->send_conf_req(layer); + case FSM_Req_Sent: + case FSM_Ack_Sent: + if (layer->send_conf_ack) layer->send_conf_ack(layer); + layer->fsm_state=FSM_Ack_Sent; + break; + case FSM_Ack_Rcvd: + if (layer->send_conf_ack) layer->send_conf_ack(layer); + stop_timer(layer); + if (layer->layer_up) layer->layer_up(layer); + layer->fsm_state=FSM_Opened; + break; + case FSM_Opened: + if (layer->layer_down) layer->layer_down(layer); + --layer->restart_counter; + if (layer->send_conf_req) layer->send_conf_req(layer); + if (layer->send_conf_ack) layer->send_conf_ack(layer); + layer->fsm_state=FSM_Ack_Sent; + break; + default: + break; + } +} + +void ppp_fsm_recv_conf_req_nak(struct ppp_fsm_t *layer) +{ + switch(layer->fsm_state) + { + case FSM_Closed: + send_term_ack(layer); + break; + case FSM_Stopped: + //if (layer->init_req_cnt) layer->init_req_cnt(layer); + init_req_counter(layer,layer->max_configure); + --layer->restart_counter; + if (layer->send_conf_req) layer->send_conf_req(layer); + case FSM_Ack_Sent: + if (layer->send_conf_nak) layer->send_conf_nak(layer); + layer->fsm_state=FSM_Req_Sent; + break; + case FSM_Req_Sent: + case FSM_Ack_Rcvd: + if (layer->send_conf_nak) layer->send_conf_nak(layer); + break; + case FSM_Opened: + if (layer->layer_down) layer->layer_down(layer); + --layer->restart_counter; + if (layer->send_conf_req) layer->send_conf_req(layer); + if (layer->send_conf_nak) layer->send_conf_nak(layer); + layer->fsm_state=FSM_Req_Sent; + break; + default: + break; + } +} + +void ppp_fsm_recv_conf_req_rej(struct ppp_fsm_t *layer) +{ + switch(layer->fsm_state) + { + case FSM_Closed: + send_term_ack(layer); + break; + case FSM_Stopped: + //if (layer->init_req_cnt) layer->init_req_cnt(layer); + init_req_counter(layer,layer->max_configure); + --layer->restart_counter; + if (layer->send_conf_req) layer->send_conf_req(layer); + case FSM_Ack_Sent: + if (layer->send_conf_rej) layer->send_conf_rej(layer); + layer->fsm_state=FSM_Req_Sent; + break; + case FSM_Req_Sent: + case FSM_Ack_Rcvd: + if (layer->send_conf_rej) layer->send_conf_rej(layer); + break; + case FSM_Opened: + if (layer->layer_down) layer->layer_down(layer); + --layer->restart_counter; + if (layer->send_conf_req) layer->send_conf_req(layer); + if (layer->send_conf_rej) layer->send_conf_rej(layer); + layer->fsm_state=FSM_Req_Sent; + break; + default: + break; + } +} + +void ppp_fsm_recv_conf_ack(struct ppp_fsm_t *layer) +{ + switch(layer->fsm_state) + { + case FSM_Closed: + case FSM_Stopped: + send_term_ack(layer); + break; + case FSM_Req_Sent: + //if (layer->init_req_cnt) layer->init_req_cnt(layer); + init_req_counter(layer,layer->max_configure); + layer->fsm_state=FSM_Ack_Rcvd; + break; + case FSM_Ack_Rcvd: + --layer->restart_counter; + if (layer->send_conf_req) layer->send_conf_req(layer); + layer->fsm_state=FSM_Req_Sent; + break; + case FSM_Ack_Sent: + //if (layer->init_req_cnt) layer->init_req_cnt(layer); + //init_req_counter(layer,layer->max_configure); + //tlu + stop_timer(layer); + if (layer->layer_up) layer->layer_up(layer); + layer->fsm_state=FSM_Opened; + break; + case FSM_Opened: + if (layer->layer_down) layer->layer_down(layer); + --layer->restart_counter; + if (layer->send_conf_req) layer->send_conf_req(layer); + layer->fsm_state=FSM_Req_Sent; + default: + break; + } +} + +void ppp_fsm_recv_conf_rej(struct ppp_fsm_t *layer) +{ + switch(layer->fsm_state) + { + case FSM_Closed: + case FSM_Stopped: + send_term_ack(layer); + break; + case FSM_Req_Sent: + //if (layer->init_req_cnt) layer->init_req_cnt(layer); + init_req_counter(layer,layer->max_failure); + --layer->restart_counter; + if (layer->send_conf_req) layer->send_conf_req(layer); + break; + case FSM_Ack_Rcvd: + --layer->restart_counter; + if (layer->send_conf_req) layer->send_conf_req(layer); + layer->fsm_state=FSM_Req_Sent; + break; + case FSM_Ack_Sent: + //if (layer->init_req_cnt) layer->init_req_cnt(layer); + init_req_counter(layer,layer->max_configure); + --layer->restart_counter; + if (layer->send_conf_req) layer->send_conf_req(layer); + break; + case FSM_Opened: + if (layer->layer_down) layer->layer_down(layer); + --layer->restart_counter; + if (layer->send_conf_req) layer->send_conf_req(layer); + layer->fsm_state=FSM_Req_Sent; + break; + default: + break; + } +} + +void ppp_fsm_recv_term_req(struct ppp_fsm_t *layer) +{ + switch(layer->fsm_state) + { + case FSM_Opened: + if (layer->layer_down) layer->layer_down(layer); + send_term_req(layer); + send_term_ack(layer); + //if (layer->zero_req_cnt) layer->zero_req_cnt(layer); + zero_req_counter(layer); + layer->fsm_state=FSM_Stopping; + break; + case FSM_Req_Sent: + case FSM_Ack_Rcvd: + case FSM_Ack_Sent: + send_term_req(layer); + layer->fsm_state=FSM_Req_Sent; + break; + default: + send_term_req(layer); + break; + } +} + +void ppp_fsm_recv_term_ack(struct ppp_fsm_t *layer) +{ + switch(layer->fsm_state) + { + case FSM_Closing: + if (layer->layer_finished) layer->layer_finished(layer); + layer->fsm_state=FSM_Closed; + break; + case FSM_Stopping: + if (layer->layer_finished) layer->layer_finished(layer); + layer->fsm_state=FSM_Stopped; + break; + case FSM_Ack_Rcvd: + layer->fsm_state=FSM_Req_Sent; + break; + case FSM_Opened: + if (layer->layer_down) layer->layer_down(layer); + --layer->restart_counter; + if (layer->send_conf_req) layer->send_conf_req(layer); + layer->fsm_state=FSM_Req_Sent; + break; + default: + break; + } +} + +void ppp_fsm_recv_unk(struct ppp_fsm_t *layer) +{ + if (layer->send_conf_rej) layer->send_conf_rej(layer); +} + +void ppp_fsm_recv_code_rej_perm(struct ppp_fsm_t *layer) +{ + switch(layer->fsm_state) + { + case FSM_Ack_Rcvd: + layer->fsm_state=FSM_Req_Sent; + break; + default: + break; + } +} + +void ppp_fsm_recv_code_rej_bad(struct ppp_fsm_t *layer) +{ + switch(layer->fsm_state) + { + case FSM_Opened: + if (layer->layer_down) layer->layer_down(layer); + send_term_req(layer); + layer->fsm_state=FSM_Stopping; + break; + case FSM_Closing: + if (layer->layer_finished) layer->layer_finished(layer); + layer->fsm_state=FSM_Closed; + break; + case FSM_Stopping: + case FSM_Req_Sent: + case FSM_Ack_Rcvd: + case FSM_Ack_Sent: + if (layer->layer_finished) layer->layer_finished(layer); + layer->fsm_state=FSM_Stopped; + break; + default: + break; + } +} + +void send_term_req(struct ppp_fsm_t *layer) +{ + struct lcp_hdr_t hdr = { + .proto = htons(PPP_LCP), + .code = TERMREQ, + .id = ++layer->id, + .len = htons(4), + }; + + log_debug("send [LCP TermReq id=%i \"\"]\n",hdr.id); + + --layer->restart_counter; + ppp_chan_send(layer->ppp, &hdr, 6); +} +void send_term_ack(struct ppp_fsm_t *layer) +{ + struct lcp_hdr_t hdr = { + .proto = htons(PPP_LCP), + .code = TERMACK, + .id = layer->recv_id, + .len = htons(4), + }; + + log_debug("send [LCP TermAck id=%i \"\"]\n", hdr.id); + + ppp_chan_send(layer->ppp, &hdr, 6); +} + +static void stop_timer(struct ppp_fsm_t *fsm) +{ + if (fsm->restart_timer.period) { + fsm->restart_timer.period = 0; + triton_timer_del(&fsm->restart_timer); + } +} +static void init_req_counter(struct ppp_fsm_t *layer,int timeout) +{ + layer->restart_timer.expire_tv.tv_sec=0; + layer->restart_counter = timeout; + + if (!layer->restart_timer.period) { + layer->restart_timer.period = layer->timeout * 1000; + triton_timer_add(layer->ppp->ctrl->ctx, &layer->restart_timer, 0); + } +} +static void zero_req_counter(struct ppp_fsm_t *layer) +{ + layer->restart_timer.expire_tv.tv_sec=0; + layer->restart_counter=0; +} + +static void restart_timer_func(struct triton_timer_t *t) +{ + struct ppp_fsm_t *layer = container_of(t, typeof(*layer), restart_timer); + + if (layer->restart_counter>0) + ppp_fsm_timeout0(layer); + else + ppp_fsm_timeout1(layer); +} + +void __init fsm_init(void) +{ + char *opt; + + opt = conf_get_opt("lcp", "max-terminate"); + if (opt && atoi(opt) > 0) + conf_max_terminate = atoi(opt); + + opt = conf_get_opt("lcp", "max-configure"); + if (opt && atoi(opt) > 0) + conf_max_configure = atoi(opt); + + opt = conf_get_opt("lcp", "max-failure"); + if (opt && atoi(opt) > 0) + conf_max_failure = atoi(opt); + + opt = conf_get_opt("lcp", "timeout"); + if (opt && atoi(opt) > 0) + conf_timeout = atoi(opt); +} diff --git a/accel-pptpd/ppp/ppp_fsm.h b/accel-pptpd/ppp/ppp_fsm.h new file mode 100644 index 0000000..bc958fe --- /dev/null +++ b/accel-pptpd/ppp/ppp_fsm.h @@ -0,0 +1,65 @@ +#ifndef PPP_FSM_H +#define PPP_FSM_H + +typedef enum {FSM_Initial=0,FSM_Starting,FSM_Closed,FSM_Stopped,FSM_Closing,FSM_Stopping,FSM_Req_Sent,FSM_Ack_Rcvd,FSM_Ack_Sent,FSM_Opened} FSM_STATE; +/* + * CP (LCP, IPCP, etc.) codes. + */ +#define CONFREQ 1 /* Configuration Request */ +#define CONFACK 2 /* Configuration Ack */ +#define CONFNAK 3 /* Configuration Nak */ +#define CONFREJ 4 /* Configuration Reject */ +#define TERMREQ 5 /* Termination Request */ +#define TERMACK 6 /* Termination Ack */ +#define CODEREJ 7 /* Code Reject */ +#define ECHOREQ 9 /* Echo Request */ +#define ECHOREP 10 /* Echo Reply */ + +struct ppp_t; + +struct ppp_fsm_t +{ + struct ppp_t *ppp; + FSM_STATE fsm_state; + + struct triton_timer_t restart_timer; + int restart_counter; + int max_terminate; + int max_configure; + int max_failure; + int timeout; + + int id; + int recv_id; + + //fsm handling + void (*layer_up)(struct ppp_fsm_t*); + void (*layer_down)(struct ppp_fsm_t*); + void (*layer_started)(struct ppp_fsm_t*); + void (*layer_finished)(struct ppp_fsm_t*); + void (*send_conf_req)(struct ppp_fsm_t*); + void (*send_conf_ack)(struct ppp_fsm_t*); + void (*send_conf_nak)(struct ppp_fsm_t*); + void (*send_conf_rej)(struct ppp_fsm_t*); +}; + +void ppp_fsm_init(struct ppp_fsm_t*); +void ppp_fsm_free(struct ppp_fsm_t*); + +void ppp_fsm_lower_up(struct ppp_fsm_t*); +void ppp_fsm_lower_down(struct ppp_fsm_t*); +void ppp_fsm_open(struct ppp_fsm_t*); +void ppp_fsm_close(struct ppp_fsm_t*); +void ppp_fsm_timeout0(struct ppp_fsm_t *layer); +void ppp_fsm_timeout1(struct ppp_fsm_t *layer); +void ppp_fsm_recv_conf_req_ack(struct ppp_fsm_t *layer); +void ppp_fsm_recv_conf_req_nak(struct ppp_fsm_t *layer); +void ppp_fsm_recv_conf_req_rej(struct ppp_fsm_t *layer); +void ppp_fsm_recv_conf_ack(struct ppp_fsm_t *layer); +void ppp_fsm_recv_conf_rej(struct ppp_fsm_t *layer); +void ppp_fsm_recv_term_req(struct ppp_fsm_t *layer); +void ppp_fsm_recv_term_ack(struct ppp_fsm_t *layer); +void ppp_fsm_recv_unk(struct ppp_fsm_t *layer); +void ppp_fsm_recv_code_rej_bad(struct ppp_fsm_t *layer); + +#endif diff --git a/accel-pptpd/ppp/ppp_ipcp.c b/accel-pptpd/ppp/ppp_ipcp.c new file mode 100644 index 0000000..6ac145b --- /dev/null +++ b/accel-pptpd/ppp/ppp_ipcp.c @@ -0,0 +1,568 @@ +#include +#include +#include +#include +#include + +#include "triton.h" + +#include "log.h" + +#include "ppp.h" +#include "ppp_ipcp.h" + +struct recv_opt_t +{ + struct list_head entry; + struct ipcp_opt_hdr_t *hdr; + int len; + int state; + struct ipcp_option_t *lopt; +}; + +static LIST_HEAD(option_handlers); + +static void ipcp_layer_up(struct ppp_fsm_t*); +static void ipcp_layer_down(struct ppp_fsm_t*); +static void send_conf_req(struct ppp_fsm_t*); +static void send_conf_ack(struct ppp_fsm_t*); +static void send_conf_nak(struct ppp_fsm_t*); +static void send_conf_rej(struct ppp_fsm_t*); +static void ipcp_recv(struct ppp_handler_t*); + +static void ipcp_options_init(struct ppp_ipcp_t *ipcp) +{ + struct ipcp_option_t *lopt; + struct ipcp_option_handler_t *h; + + INIT_LIST_HEAD(&ipcp->options); + + list_for_each_entry(h,&option_handlers,entry) + { + lopt=h->init(ipcp); + if (lopt) + { + lopt->h=h; + list_add_tail(&lopt->entry,&ipcp->options); + ipcp->conf_req_len+=lopt->len; + } + } +} + +static void ipcp_options_free(struct ppp_ipcp_t *ipcp) +{ + struct ipcp_option_t *lopt; + + while(!list_empty(&ipcp->options)) + { + lopt=list_entry(ipcp->options.next,typeof(*lopt),entry); + list_del(&lopt->entry); + lopt->h->free(ipcp,lopt); + } +} + +static struct ppp_layer_data_t *ipcp_layer_init(struct ppp_t *ppp) +{ + struct ppp_ipcp_t *ipcp=malloc(sizeof(*ipcp)); + memset(ipcp,0,sizeof(*ipcp)); + + log_debug("ipcp_layer_init\n"); + + ipcp->ppp=ppp; + ipcp->fsm.ppp=ppp; + + ipcp->hnd.proto=PPP_IPCP; + ipcp->hnd.recv=ipcp_recv; + + ppp_register_unit_handler(ppp,&ipcp->hnd); + + ppp_fsm_init(&ipcp->fsm); + + ipcp->fsm.layer_up=ipcp_layer_up; + ipcp->fsm.layer_finished=ipcp_layer_down; + ipcp->fsm.send_conf_req=send_conf_req; + ipcp->fsm.send_conf_ack=send_conf_ack; + ipcp->fsm.send_conf_nak=send_conf_nak; + ipcp->fsm.send_conf_rej=send_conf_rej; + + INIT_LIST_HEAD(&ipcp->ropt_list); + + return &ipcp->ld; +} + +void ipcp_layer_start(struct ppp_layer_data_t *ld) +{ + struct ppp_ipcp_t *ipcp=container_of(ld,typeof(*ipcp),ld); + + log_debug("ipcp_layer_start\n"); + + ipcp_options_init(ipcp); + ppp_fsm_lower_up(&ipcp->fsm); + ppp_fsm_open(&ipcp->fsm); +} + +void ipcp_layer_finish(struct ppp_layer_data_t *ld) +{ + struct ppp_ipcp_t *ipcp=container_of(ld,typeof(*ipcp),ld); + + log_debug("ipcp_layer_finish\n"); + + ppp_fsm_lower_down(&ipcp->fsm); + + ppp_unregister_handler(ipcp->ppp,&ipcp->hnd); + ipcp_options_free(ipcp); + + ppp_layer_finished(ipcp->ppp,ld); +} + +void ipcp_layer_free(struct ppp_layer_data_t *ld) +{ + struct ppp_ipcp_t *ipcp=container_of(ld,typeof(*ipcp),ld); + + log_debug("ipcp_layer_free\n"); + + free(ipcp); +} + +static void ipcp_layer_up(struct ppp_fsm_t *fsm) +{ + struct ppp_ipcp_t *ipcp=container_of(fsm,typeof(*ipcp),fsm); + log_debug("ipcp_layer_started\n"); + ppp_layer_started(ipcp->ppp,&ipcp->ld); +} + +static void ipcp_layer_down(struct ppp_fsm_t *fsm) +{ + struct ppp_ipcp_t *ipcp=container_of(fsm,typeof(*ipcp),fsm); + log_debug("ipcp_layer_finished\n"); + ppp_layer_finished(ipcp->ppp,&ipcp->ld); +} + +static void print_ropt(struct recv_opt_t *ropt) +{ + int i; + uint8_t *ptr=(uint8_t*)ropt->hdr; + + log_debug(" <"); + for(i=0; ilen; i++) + { + log_debug(" %x",ptr[i]); + } + log_debug(" >"); +} + +static void send_conf_req(struct ppp_fsm_t *fsm) +{ + struct ppp_ipcp_t *ipcp=container_of(fsm,typeof(*ipcp),fsm); + uint8_t *buf=malloc(ipcp->conf_req_len), *ptr=buf; + struct ipcp_hdr_t *ipcp_hdr=(struct ipcp_hdr_t*)ptr; + struct ipcp_option_t *lopt; + int n; + + log_debug("send [IPCP ConfReq"); + ipcp_hdr->proto=htons(PPP_IPCP); + ipcp_hdr->code=CONFREQ; + ipcp_hdr->id=++ipcp->fsm.id; + ipcp_hdr->len=0; + log_debug(" id=%x",ipcp_hdr->id); + + ptr+=sizeof(*ipcp_hdr); + + list_for_each_entry(lopt,&ipcp->options,entry) + { + n=lopt->h->send_conf_req(ipcp,lopt,ptr); + if (n) + { + log_debug(" "); + lopt->h->print(log_debug,lopt,NULL); + ptr+=n; + } + } + + log_debug("]\n"); + + ipcp_hdr->len=htons((ptr-buf)-2); + ppp_unit_send(ipcp->ppp,ipcp_hdr,ptr-buf); +} + +static void send_conf_ack(struct ppp_fsm_t *fsm) +{ + struct ppp_ipcp_t *ipcp=container_of(fsm,typeof(*ipcp),fsm); + struct ipcp_hdr_t *hdr=(struct ipcp_hdr_t*)ipcp->ppp->unit_buf; + + hdr->code=CONFACK; + log_debug("send [IPCP ConfAck id=%x ]\n",ipcp->fsm.recv_id); + + ppp_unit_send(ipcp->ppp,hdr,ntohs(hdr->len)+2); +} + +static void send_conf_nak(struct ppp_fsm_t *fsm) +{ + struct ppp_ipcp_t *ipcp=container_of(fsm,typeof(*ipcp),fsm); + uint8_t *buf=malloc(ipcp->conf_req_len), *ptr=buf; + struct ipcp_hdr_t *ipcp_hdr=(struct ipcp_hdr_t*)ptr; + struct recv_opt_t *ropt; + + log_debug("send [IPCP ConfNak id=%x",ipcp->fsm.recv_id); + + ipcp_hdr->proto=htons(PPP_IPCP); + ipcp_hdr->code=CONFNAK; + ipcp_hdr->id=ipcp->fsm.recv_id; + ipcp_hdr->len=0; + + ptr+=sizeof(*ipcp_hdr); + + list_for_each_entry(ropt,&ipcp->ropt_list,entry) + { + if (ropt->state==IPCP_OPT_NAK) + { + log_debug(" "); + ropt->lopt->h->print(log_debug,ropt->lopt,NULL); + ptr+=ropt->lopt->h->send_conf_nak(ipcp,ropt->lopt,ptr); + } + } + + log_debug("]\n"); + + ipcp_hdr->len=htons((ptr-buf)-2); + ppp_unit_send(ipcp->ppp,ipcp_hdr,ptr-buf); +} + +static void send_conf_rej(struct ppp_fsm_t *fsm) +{ + struct ppp_ipcp_t *ipcp=container_of(fsm,typeof(*ipcp),fsm); + uint8_t *buf=malloc(ipcp->ropt_len), *ptr=buf; + struct ipcp_hdr_t *ipcp_hdr=(struct ipcp_hdr_t*)ptr; + struct recv_opt_t *ropt; + + log_debug("send [IPCP ConfRej id=%x ",ipcp->fsm.recv_id); + + ipcp_hdr->proto=htons(PPP_IPCP); + ipcp_hdr->code=CONFREJ; + ipcp_hdr->id=ipcp->fsm.recv_id; + ipcp_hdr->len=0; + + ptr+=sizeof(*ipcp_hdr); + + list_for_each_entry(ropt,&ipcp->ropt_list,entry) + { + if (ropt->state==IPCP_OPT_REJ) + { + log_debug(" "); + if (ropt->lopt) ropt->lopt->h->print(log_debug,ropt->lopt,(uint8_t*)ropt->hdr); + else print_ropt(ropt); + memcpy(ptr,ropt->hdr,ropt->len); + ptr+=ropt->len; + } + } + + log_debug("]\n"); + + ipcp_hdr->len=htons((ptr-buf)-2); + ppp_unit_send(ipcp->ppp,ipcp_hdr,ptr-buf); +} + +static int ipcp_recv_conf_req(struct ppp_ipcp_t *ipcp,uint8_t *data,int size) +{ + struct ipcp_opt_hdr_t *hdr; + struct recv_opt_t *ropt; + struct ipcp_option_t *lopt; + int r,ret=1; + + ipcp->ropt_len=size; + + while(size>0) + { + hdr=(struct ipcp_opt_hdr_t *)data; + + ropt=malloc(sizeof(*ropt)); + memset(ropt,0,sizeof(*ropt)); + if (hdr->len>size) ropt->len=size; + else ropt->len=hdr->len; + ropt->hdr=hdr; + ropt->state=IPCP_OPT_NONE; + list_add_tail(&ropt->entry,&ipcp->ropt_list); + + data+=ropt->len; + size-=ropt->len; + } + + list_for_each_entry(lopt,&ipcp->options,entry) + lopt->state=IPCP_OPT_NONE; + + log_debug("recv [IPCP ConfReq id=%x",ipcp->fsm.recv_id); + list_for_each_entry(ropt,&ipcp->ropt_list,entry) + { + list_for_each_entry(lopt,&ipcp->options,entry) + { + if (lopt->id==ropt->hdr->id) + { + log_debug(" "); + lopt->h->print(log_debug,lopt,(uint8_t*)ropt->hdr); + r=lopt->h->recv_conf_req(ipcp,lopt,(uint8_t*)ropt->hdr); + lopt->state=r; + ropt->state=r; + ropt->lopt=lopt; + if (rlopt) + { + log_debug(" "); + print_ropt(ropt); + ropt->state=IPCP_OPT_REJ; + ret=IPCP_OPT_REJ; + } + } + log_debug("]\n"); + + /*list_for_each_entry(lopt,&ipcp->options,entry) + { + if (lopt->state==IPCP_OPT_NONE) + { + r=lopt->h->recv_conf_req(ipcp,lopt,NULL); + lopt->state=r; + if (rropt_list)) + { + ropt=list_entry(ipcp->ropt_list.next,typeof(*ropt),entry); + list_del(&ropt->entry); + free(ropt); + } +} + +static int ipcp_recv_conf_rej(struct ppp_ipcp_t *ipcp,uint8_t *data,int size) +{ + struct ipcp_opt_hdr_t *hdr; + struct ipcp_option_t *lopt; + int res=0; + + log_debug("recv [IPCP ConfRej id=%x",ipcp->fsm.recv_id); + + if (ipcp->fsm.recv_id!=ipcp->fsm.id) + { + log_debug(": id mismatch ]\n"); + return 0; + } + + while(size>0) + { + hdr=(struct ipcp_opt_hdr_t *)data; + + list_for_each_entry(lopt,&ipcp->options,entry) + { + if (lopt->id==hdr->id) + { + if (!lopt->h->recv_conf_rej) + res=-1; + else if (lopt->h->recv_conf_rej(ipcp,lopt,data)) + res=-1; + break; + } + } + + data+=hdr->len; + size-=hdr->len; + } + log_debug("]\n"); + return res; +} + +static int ipcp_recv_conf_nak(struct ppp_ipcp_t *ipcp,uint8_t *data,int size) +{ + struct ipcp_opt_hdr_t *hdr; + struct ipcp_option_t *lopt; + int res=0; + + log_debug("recv [IPCP ConfNak id=%x",ipcp->fsm.recv_id); + + if (ipcp->fsm.recv_id!=ipcp->fsm.id) + { + log_debug(": id mismatch ]\n"); + return 0; + } + + while(size>0) + { + hdr=(struct ipcp_opt_hdr_t *)data; + + list_for_each_entry(lopt,&ipcp->options,entry) + { + if (lopt->id==hdr->id) + { + log_debug(" "); + lopt->h->print(log_debug,lopt,data); + if (lopt->h->recv_conf_nak(ipcp,lopt,data)) + res=-1; + break; + } + } + + data+=hdr->len; + size-=hdr->len; + } + log_debug("]\n"); + return res; +} + +static int ipcp_recv_conf_ack(struct ppp_ipcp_t *ipcp,uint8_t *data,int size) +{ + struct ipcp_opt_hdr_t *hdr; + struct ipcp_option_t *lopt; + int res=0; + + log_debug("recv [IPCP ConfAck id=%x",ipcp->fsm.recv_id); + + if (ipcp->fsm.recv_id!=ipcp->fsm.id) + { + log_debug(": id mismatch ]\n"); + return 0; + } + + while(size>0) + { + hdr=(struct ipcp_opt_hdr_t *)data; + + list_for_each_entry(lopt,&ipcp->options,entry) + { + if (lopt->id==hdr->id) + { + log_debug(" "); + lopt->h->print(log_debug,lopt,data); + if (!lopt->h->recv_conf_ack) + break; + if (lopt->h->recv_conf_ack(ipcp,lopt,data)) + res=-1; + break; + } + } + + data+=hdr->len; + size-=hdr->len; + } + log_debug("]\n"); + return res; +} + +static void ipcp_recv(struct ppp_handler_t*h) +{ + struct ipcp_hdr_t *hdr; + struct ppp_ipcp_t *ipcp=container_of(h,typeof(*ipcp),hnd); + int r; + char *term_msg; + + if (ipcp->fsm.fsm_state==FSM_Initial || ipcp->fsm.fsm_state==FSM_Closed) + { + log_error("IPCP: discaring packet\n"); + return; + } + + if (ipcp->ppp->unit_buf_sizeppp->unit_buf; + if (ntohs(hdr->len)fsm.recv_id=hdr->id; + switch(hdr->code) + { + case CONFREQ: + r=ipcp_recv_conf_req(ipcp,(uint8_t*)(hdr+1),ntohs(hdr->len)-PPP_HDRLEN); + switch(r) + { + case IPCP_OPT_ACK: + ppp_fsm_recv_conf_req_ack(&ipcp->fsm); + break; + case IPCP_OPT_NAK: + ppp_fsm_recv_conf_req_nak(&ipcp->fsm); + break; + case IPCP_OPT_REJ: + ppp_fsm_recv_conf_req_rej(&ipcp->fsm); + break; + } + ipcp_free_conf_req(ipcp); + if (r==IPCP_OPT_FAIL) + ppp_terminate(ipcp->ppp, 0); + break; + case CONFACK: + if (ipcp_recv_conf_ack(ipcp,(uint8_t*)(hdr+1),ntohs(hdr->len)-PPP_HDRLEN)) + ppp_terminate(ipcp->ppp, 0); + else + ppp_fsm_recv_conf_ack(&ipcp->fsm); + break; + case CONFNAK: + ipcp_recv_conf_nak(ipcp,(uint8_t*)(hdr+1),ntohs(hdr->len)-PPP_HDRLEN); + ppp_fsm_recv_conf_rej(&ipcp->fsm); + break; + case CONFREJ: + if (ipcp_recv_conf_rej(ipcp,(uint8_t*)(hdr+1),ntohs(hdr->len)-PPP_HDRLEN)) + ppp_terminate(ipcp->ppp, 0); + else + ppp_fsm_recv_conf_rej(&ipcp->fsm); + break; + case TERMREQ: + term_msg=strndup((char*)(hdr+1),ntohs(hdr->len)); + log_debug("recv [IPCP TermReq id=%x \"%s\"]\n",hdr->id,term_msg); + free(term_msg); + ppp_fsm_recv_term_req(&ipcp->fsm); + ppp_terminate(ipcp->ppp, 0); + break; + case TERMACK: + term_msg=strndup((char*)(hdr+1),ntohs(hdr->len)); + log_debug("recv [IPCP TermAck id=%x \"%s\"]\n",hdr->id,term_msg); + free(term_msg); + ppp_fsm_recv_term_ack(&ipcp->fsm); + break; + case CODEREJ: + log_debug("recv [IPCP CodeRej id=%x]\n",hdr->id); + ppp_fsm_recv_code_rej_bad(&ipcp->fsm); + break; + default: + ppp_fsm_recv_unk(&ipcp->fsm); + break; + } +} + +int ipcp_option_register(struct ipcp_option_handler_t *h) +{ + /*struct ipcp_option_drv_t *p; + + list_for_each_entry(p,option_drv_list,entry) + if (p->id==h->id) + return -1;*/ + + list_add_tail(&h->entry,&option_handlers); + + return 0; +} + +static struct ppp_layer_t ipcp_layer= +{ + .init=ipcp_layer_init, + .start=ipcp_layer_start, + .finish=ipcp_layer_finish, + .free=ipcp_layer_free, +}; + +static void __init ipcp_init(void) +{ + ppp_register_layer("ipcp",&ipcp_layer); +} diff --git a/accel-pptpd/ppp/ppp_ipcp.h b/accel-pptpd/ppp/ppp_ipcp.h new file mode 100644 index 0000000..9e0c99e --- /dev/null +++ b/accel-pptpd/ppp/ppp_ipcp.h @@ -0,0 +1,94 @@ +#ifndef PPP_IPCP_H +#define PPP_IPCP_H + +#include + +#include "triton.h" +#include "ppp_fsm.h" +/* + * Options. + */ +#define CI_COMP 2 /* IP-Compress-Protocol */ +#define CI_ADDR 3 /* IP-Address */ +#define CI_DNS1 129 /* Primary-DNS-Address */ +#define CI_DNS2 131 /* Secondary-DNS-Address */ + +struct ipcp_hdr_t +{ + uint16_t proto; + uint8_t code; + uint8_t id; + uint16_t len; +} __attribute__((packed)); +struct ipcp_opt_hdr_t +{ + uint8_t id; + uint8_t len; +} __attribute__((packed)); +struct ipcp_opt8_t +{ + struct ipcp_opt_hdr_t hdr; + uint8_t val; +} __attribute__((packed)); +struct ipcp_opt16_t +{ + struct ipcp_opt_hdr_t hdr; + uint16_t val; +} __attribute__((packed)); +struct ipcp_opt32_t +{ + struct ipcp_opt_hdr_t hdr; + uint32_t val; +} __attribute__((packed)); + +#define IPCP_OPT_NONE 0 +#define IPCP_OPT_ACK 1 +#define IPCP_OPT_NAK -1 +#define IPCP_OPT_REJ -2 +#define IPCP_OPT_FAIL -3 + +struct ppp_ipcp_t; +struct ipcp_option_handler_t; + +struct ipcp_option_t +{ + struct list_head entry; + int id; + int len; + int state; + struct ipcp_option_handler_t *h; +}; + +struct ipcp_option_handler_t +{ + struct list_head entry; + struct ipcp_option_t* (*init)(struct ppp_ipcp_t*); + int (*send_conf_req)(struct ppp_ipcp_t*,struct ipcp_option_t*,uint8_t*); + int (*send_conf_rej)(struct ppp_ipcp_t*,struct ipcp_option_t*,uint8_t*); + int (*send_conf_nak)(struct ppp_ipcp_t*,struct ipcp_option_t*,uint8_t*); + int (*recv_conf_req)(struct ppp_ipcp_t*,struct ipcp_option_t*,uint8_t*); + int (*recv_conf_rej)(struct ppp_ipcp_t*,struct ipcp_option_t*,uint8_t*); + int (*recv_conf_nak)(struct ppp_ipcp_t*,struct ipcp_option_t*,uint8_t*); + int (*recv_conf_ack)(struct ppp_ipcp_t*,struct ipcp_option_t*,uint8_t*); + void (*free)(struct ppp_ipcp_t*,struct ipcp_option_t*); + void (*print)(void (*print)(const char *fmt,...), struct ipcp_option_t*,uint8_t*); +}; + +struct ppp_ipcp_t +{ + struct ppp_layer_data_t ld; + struct ppp_handler_t hnd; + struct ppp_fsm_t fsm; + struct ppp_t *ppp; + struct list_head options; + + struct list_head ropt_list; // last received ConfReq + int ropt_len; + + int conf_req_len; +}; + +int ipcp_option_register(struct ipcp_option_handler_t *h); + +#endif + diff --git a/accel-pptpd/ppp/ppp_lcp.c b/accel-pptpd/ppp/ppp_lcp.c new file mode 100644 index 0000000..afcfc10 --- /dev/null +++ b/accel-pptpd/ppp/ppp_lcp.c @@ -0,0 +1,659 @@ +#include +#include +#include +#include +#include + +#include "triton.h" + +#include "log.h" + +#include "ppp.h" +#include "ppp_lcp.h" + +struct recv_opt_t +{ + struct list_head entry; + struct lcp_opt_hdr_t *hdr; + int len; + int state; + struct lcp_option_t *lopt; +}; + +static int conf_echo_interval = 0; +static int conf_echo_failure = 3; + +static LIST_HEAD(option_handlers); + +static void lcp_layer_up(struct ppp_fsm_t*); +static void lcp_layer_down(struct ppp_fsm_t*); +static void send_conf_req(struct ppp_fsm_t*); +static void send_conf_ack(struct ppp_fsm_t*); +static void send_conf_nak(struct ppp_fsm_t*); +static void send_conf_rej(struct ppp_fsm_t*); +static void lcp_recv(struct ppp_handler_t*); +static void start_echo(struct ppp_lcp_t *lcp); +static void stop_echo(struct ppp_lcp_t *lcp); + +static void lcp_options_init(struct ppp_lcp_t *lcp) +{ + struct lcp_option_t *lopt; + struct lcp_option_handler_t *h; + + INIT_LIST_HEAD(&lcp->options); + + list_for_each_entry(h,&option_handlers,entry) + { + lopt=h->init(lcp); + if (lopt) + { + lopt->h=h; + list_add_tail(&lopt->entry,&lcp->options); + lcp->conf_req_len+=lopt->len; + } + } +} + +static void lcp_options_free(struct ppp_lcp_t *lcp) +{ + struct lcp_option_t *lopt; + + while(!list_empty(&lcp->options)) + { + lopt=list_entry(lcp->options.next,typeof(*lopt),entry); + list_del(&lopt->entry); + lopt->h->free(lcp,lopt); + } +} + +static struct ppp_layer_data_t *lcp_layer_init(struct ppp_t *ppp) +{ + struct ppp_lcp_t *lcp=malloc(sizeof(*lcp)); + memset(lcp,0,sizeof(*lcp)); + + log_debug("lcp_layer_init\n"); + + lcp->ppp=ppp; + lcp->fsm.ppp=ppp; + + lcp->hnd.proto=PPP_LCP; + lcp->hnd.recv=lcp_recv; + + ppp_register_chan_handler(ppp,&lcp->hnd); + + ppp_fsm_init(&lcp->fsm); + + lcp->fsm.layer_up=lcp_layer_up; + lcp->fsm.layer_finished=lcp_layer_down; + lcp->fsm.send_conf_req=send_conf_req; + lcp->fsm.send_conf_ack=send_conf_ack; + lcp->fsm.send_conf_nak=send_conf_nak; + lcp->fsm.send_conf_rej=send_conf_rej; + + INIT_LIST_HEAD(&lcp->ropt_list); + + return &lcp->ld; +} + +void lcp_layer_start(struct ppp_layer_data_t *ld) +{ + struct ppp_lcp_t *lcp=container_of(ld,typeof(*lcp),ld); + + log_debug("lcp_layer_start\n"); + + lcp_options_init(lcp); + ppp_fsm_lower_up(&lcp->fsm); + ppp_fsm_open(&lcp->fsm); +} + +void lcp_layer_finish(struct ppp_layer_data_t *ld) +{ + struct ppp_lcp_t *lcp=container_of(ld,typeof(*lcp),ld); + + log_debug("lcp_layer_finish\n"); + + stop_echo(lcp); + + ppp_fsm_close(&lcp->fsm); +} + +void lcp_layer_free(struct ppp_layer_data_t *ld) +{ + struct ppp_lcp_t *lcp=container_of(ld,typeof(*lcp),ld); + + log_debug("lcp_layer_free\n"); + + stop_echo(lcp); + ppp_unregister_handler(lcp->ppp,&lcp->hnd); + lcp_options_free(lcp); + ppp_fsm_free(&lcp->fsm); + + free(lcp); +} + +static void lcp_layer_up(struct ppp_fsm_t *fsm) +{ + struct ppp_lcp_t *lcp=container_of(fsm,typeof(*lcp),fsm); + log_debug("lcp_layer_started\n"); + ppp_layer_started(lcp->ppp,&lcp->ld); + + start_echo(lcp); +} + +static void lcp_layer_down(struct ppp_fsm_t *fsm) +{ + struct ppp_lcp_t *lcp=container_of(fsm,typeof(*lcp),fsm); + log_debug("lcp_layer_finished\n"); + stop_echo(lcp); + ppp_layer_finished(lcp->ppp,&lcp->ld); +} + +static void print_ropt(struct recv_opt_t *ropt) +{ + int i; + uint8_t *ptr=(uint8_t*)ropt->hdr; + + log_debug(" <"); + for(i=0; ilen; i++) + { + log_debug(" %x",ptr[i]); + } + log_debug(" >"); +} + +static void send_conf_req(struct ppp_fsm_t *fsm) +{ + struct ppp_lcp_t *lcp=container_of(fsm,typeof(*lcp),fsm); + uint8_t *buf=malloc(lcp->conf_req_len), *ptr=buf; + struct lcp_hdr_t *lcp_hdr=(struct lcp_hdr_t*)ptr; + struct lcp_option_t *lopt; + int n; + + log_debug("send [LCP ConfReq"); + lcp_hdr->proto=htons(PPP_LCP); + lcp_hdr->code=CONFREQ; + lcp_hdr->id=++lcp->fsm.id; + lcp_hdr->len=0; + log_debug(" id=%x",lcp_hdr->id); + + ptr+=sizeof(*lcp_hdr); + + list_for_each_entry(lopt,&lcp->options,entry) + { + n=lopt->h->send_conf_req(lcp,lopt,ptr); + if (n) + { + log_debug(" "); + lopt->h->print(log_debug,lopt,NULL); + ptr+=n; + } + } + + log_debug("]\n"); + + lcp_hdr->len=htons((ptr-buf)-2); + ppp_chan_send(lcp->ppp,lcp_hdr,ptr-buf); +} + +static void send_conf_ack(struct ppp_fsm_t *fsm) +{ + struct ppp_lcp_t *lcp=container_of(fsm,typeof(*lcp),fsm); + struct lcp_hdr_t *hdr=(struct lcp_hdr_t*)lcp->ppp->chan_buf; + + hdr->code=CONFACK; + log_debug("send [LCP ConfAck id=%x ]\n",lcp->fsm.recv_id); + + ppp_chan_send(lcp->ppp,hdr,ntohs(hdr->len)+2); +} + +static void send_conf_nak(struct ppp_fsm_t *fsm) +{ + struct ppp_lcp_t *lcp=container_of(fsm,typeof(*lcp),fsm); + uint8_t *buf=malloc(lcp->conf_req_len), *ptr=buf; + struct lcp_hdr_t *lcp_hdr=(struct lcp_hdr_t*)ptr; + struct recv_opt_t *ropt; + + log_debug("send [LCP ConfNak id=%x",lcp->fsm.recv_id); + + lcp_hdr->proto=htons(PPP_LCP); + lcp_hdr->code=CONFNAK; + lcp_hdr->id=lcp->fsm.recv_id; + lcp_hdr->len=0; + + ptr+=sizeof(*lcp_hdr); + + list_for_each_entry(ropt,&lcp->ropt_list,entry) + { + if (ropt->state==LCP_OPT_NAK) + { + log_debug(" "); + ropt->lopt->h->print(log_debug,ropt->lopt,NULL); + ptr+=ropt->lopt->h->send_conf_nak(lcp,ropt->lopt,ptr); + } + } + + log_debug("]\n"); + + lcp_hdr->len=htons((ptr-buf)-2); + ppp_chan_send(lcp->ppp,lcp_hdr,ptr-buf); +} + +static void send_conf_rej(struct ppp_fsm_t *fsm) +{ + struct ppp_lcp_t *lcp=container_of(fsm,typeof(*lcp),fsm); + uint8_t *buf=malloc(lcp->ropt_len), *ptr=buf; + struct lcp_hdr_t *lcp_hdr=(struct lcp_hdr_t*)ptr; + struct recv_opt_t *ropt; + + log_debug("send [LCP ConfRej id=%x ",lcp->fsm.recv_id); + + lcp_hdr->proto=htons(PPP_LCP); + lcp_hdr->code=CONFREJ; + lcp_hdr->id=lcp->fsm.recv_id; + lcp_hdr->len=0; + + ptr+=sizeof(*lcp_hdr); + + list_for_each_entry(ropt,&lcp->ropt_list,entry) + { + if (ropt->state==LCP_OPT_REJ) + { + log_debug(" "); + if (ropt->lopt) ropt->lopt->h->print(log_debug,ropt->lopt,(uint8_t*)ropt->hdr); + else print_ropt(ropt); + memcpy(ptr,ropt->hdr,ropt->len); + ptr+=ropt->len; + } + } + + log_debug("]\n"); + + lcp_hdr->len=htons((ptr-buf)-2); + ppp_chan_send(lcp->ppp,lcp_hdr,ptr-buf); +} + +static int lcp_recv_conf_req(struct ppp_lcp_t *lcp,uint8_t *data,int size) +{ + struct lcp_opt_hdr_t *hdr; + struct recv_opt_t *ropt; + struct lcp_option_t *lopt; + int r,ret=1; + + lcp->ropt_len=size; + + while(size>0) + { + hdr=(struct lcp_opt_hdr_t *)data; + + ropt=malloc(sizeof(*ropt)); + if (hdr->len>size) ropt->len=size; + else ropt->len=hdr->len; + ropt->hdr=hdr; + ropt->state=LCP_OPT_NONE; + list_add_tail(&ropt->entry,&lcp->ropt_list); + + data+=ropt->len; + size-=ropt->len; + } + + list_for_each_entry(lopt,&lcp->options,entry) + lopt->state=LCP_OPT_NONE; + + log_debug("recv [LCP ConfReq id=%x",lcp->fsm.recv_id); + list_for_each_entry(ropt,&lcp->ropt_list,entry) + { + list_for_each_entry(lopt,&lcp->options,entry) + { + if (lopt->id==ropt->hdr->id) + { + log_debug(" "); + lopt->h->print(log_debug,lopt,(uint8_t*)ropt->hdr); + r=lopt->h->recv_conf_req(lcp,lopt,(uint8_t*)ropt->hdr); + lopt->state=r; + ropt->state=r; + ropt->lopt=lopt; + if (rlopt) + { + log_debug(" "); + print_ropt(ropt); + ropt->state=LCP_OPT_REJ; + ret=LCP_OPT_REJ; + } + } + log_debug("]\n"); + + /*list_for_each_entry(lopt,&lcp->options,entry) + { + if (lopt->state==LCP_OPT_NONE) + { + r=lopt->h->recv_conf_req(lcp,lopt,NULL); + lopt->state=r; + if (rropt_list)) + { + ropt=list_entry(lcp->ropt_list.next,typeof(*ropt),entry); + list_del(&ropt->entry); + free(ropt); + } +} + +static int lcp_recv_conf_rej(struct ppp_lcp_t *lcp,uint8_t *data,int size) +{ + struct lcp_opt_hdr_t *hdr; + struct lcp_option_t *lopt; + int res=0; + + log_debug("recv [LCP ConfRej id=%x",lcp->fsm.recv_id); + + if (lcp->fsm.recv_id!=lcp->fsm.id) + { + log_debug(": id mismatch ]\n"); + return 0; + } + + while(size>0) + { + hdr=(struct lcp_opt_hdr_t *)data; + + list_for_each_entry(lopt,&lcp->options,entry) + { + if (lopt->id==hdr->id) + { + if (!lopt->h->recv_conf_rej) + res=-1; + else if (lopt->h->recv_conf_rej(lcp,lopt,data)) + res=-1; + break; + } + } + + data+=hdr->len; + size-=hdr->len; + } + log_debug("]\n"); + return res; +} + +static int lcp_recv_conf_nak(struct ppp_lcp_t *lcp,uint8_t *data,int size) +{ + struct lcp_opt_hdr_t *hdr; + struct lcp_option_t *lopt; + int res=0; + + log_debug("recv [LCP ConfNak id=%x",lcp->fsm.recv_id); + + if (lcp->fsm.recv_id!=lcp->fsm.id) + { + log_debug(": id mismatch ]\n"); + return 0; + } + + while(size>0) + { + hdr=(struct lcp_opt_hdr_t *)data; + + list_for_each_entry(lopt,&lcp->options,entry) + { + if (lopt->id==hdr->id) + { + log_debug(" "); + lopt->h->print(log_debug,lopt,data); + if (lopt->h->recv_conf_nak(lcp,lopt,data)) + res=-1; + break; + } + } + + data+=hdr->len; + size-=hdr->len; + } + log_debug("]\n"); + return res; +} + +static int lcp_recv_conf_ack(struct ppp_lcp_t *lcp,uint8_t *data,int size) +{ + struct lcp_opt_hdr_t *hdr; + struct lcp_option_t *lopt; + int res=0; + + log_debug("recv [LCP ConfAck id=%x",lcp->fsm.recv_id); + + if (lcp->fsm.recv_id!=lcp->fsm.id) + { + log_debug(": id mismatch ]\n"); + return 0; + } + + while(size>0) + { + hdr=(struct lcp_opt_hdr_t *)data; + + list_for_each_entry(lopt,&lcp->options,entry) + { + if (lopt->id==hdr->id) + { + log_debug(" "); + lopt->h->print(log_debug,lopt,data); + if (!lopt->h->recv_conf_ack) + break; + if (lopt->h->recv_conf_ack(lcp,lopt,data)) + res=-1; + break; + } + } + + data+=hdr->len; + size-=hdr->len; + } + log_debug("]\n"); + return res; +} + +static void lcp_recv_echo_repl(struct ppp_lcp_t *lcp,uint8_t *data,int size) +{ + uint32_t magic = *(uint32_t *)data; + + if (size != 4) { + log_error("lcp:echo: magic number size mismatch\n"); + ppp_terminate(lcp->ppp, 0); + } + + log_debug("recv [LCP EchoRep id=%x ]\n",lcp->fsm.recv_id,magic); + + if (magic == lcp->magic) { + log_error("lcp:echo: loop-back detected\n"); + ppp_terminate(lcp->ppp, 0); + } + + lcp->echo_sent = 0; +} + +static void send_echo_reply(struct ppp_lcp_t *lcp) +{ + struct lcp_hdr_t *hdr=(struct lcp_hdr_t*)lcp->ppp->chan_buf; + uint32_t magic = *(uint32_t *)(hdr+1); + + hdr->code=ECHOREP; + log_debug("send [LCP EchoRep id=%x ]\n", hdr->id, magic); + + ppp_chan_send(lcp->ppp,hdr,ntohs(hdr->len)+2); +} +static void send_echo_request(struct triton_timer_t *t) +{ + struct ppp_lcp_t *lcp = container_of(t, typeof(*lcp), echo_timer); + struct lcp_echo_req_t + { + struct lcp_hdr_t hdr; + uint32_t magic; + } __attribute__((packed)) msg = { + .hdr.proto = htons(PPP_LCP), + .hdr.code = ECHOREQ, + .hdr.id = ++lcp->fsm.id, + .hdr.len = htons(8), + .magic = lcp->magic, + }; + + if (++lcp->echo_sent > lcp->echo_failure) { + log_warn("lcp: no echo reply\n"); + ppp_terminate(lcp->ppp, 0); + } else { + log_debug("send [LCP EchoReq id=%x ]\n", msg.hdr.id, msg.magic); + ppp_chan_send(lcp->ppp,&msg,ntohs(msg.hdr.len)+2); + } +} + +static void start_echo(struct ppp_lcp_t *lcp) +{ + lcp->echo_interval = conf_echo_interval; + lcp->echo_failure = conf_echo_failure; + + lcp->echo_timer.period = lcp->echo_interval * 1000; + lcp->echo_timer.expire = send_echo_request; + if (lcp->echo_timer.period) + triton_timer_add(lcp->ppp->ctrl->ctx, &lcp->echo_timer, 0); +} +static void stop_echo(struct ppp_lcp_t *lcp) +{ + if (lcp->echo_interval) { + triton_timer_del(&lcp->echo_timer); + lcp->echo_interval = 0; + } +} + +static void lcp_recv(struct ppp_handler_t*h) +{ + struct lcp_hdr_t *hdr; + struct ppp_lcp_t *lcp=container_of(h,typeof(*lcp),hnd); + int r; + char *term_msg; + + if (lcp->ppp->chan_buf_sizeppp->chan_buf; + if (ntohs(hdr->len)fsm.recv_id=hdr->id; + switch(hdr->code) + { + case CONFREQ: + r=lcp_recv_conf_req(lcp,(uint8_t*)(hdr+1),ntohs(hdr->len)-PPP_HDRLEN); + switch(r) + { + case LCP_OPT_ACK: + ppp_fsm_recv_conf_req_ack(&lcp->fsm); + break; + case LCP_OPT_NAK: + ppp_fsm_recv_conf_req_nak(&lcp->fsm); + break; + case LCP_OPT_REJ: + ppp_fsm_recv_conf_req_rej(&lcp->fsm); + break; + } + lcp_free_conf_req(lcp); + if (r==LCP_OPT_FAIL) + ppp_terminate(lcp->ppp, 0); + break; + case CONFACK: + if (lcp_recv_conf_ack(lcp,(uint8_t*)(hdr+1),ntohs(hdr->len)-PPP_HDRLEN)) + ppp_terminate(lcp->ppp, 0); + else + ppp_fsm_recv_conf_ack(&lcp->fsm); + break; + case CONFNAK: + lcp_recv_conf_nak(lcp,(uint8_t*)(hdr+1),ntohs(hdr->len)-PPP_HDRLEN); + ppp_fsm_recv_conf_rej(&lcp->fsm); + break; + case CONFREJ: + if (lcp_recv_conf_rej(lcp,(uint8_t*)(hdr+1),ntohs(hdr->len)-PPP_HDRLEN)) + ppp_terminate(lcp->ppp, 0); + else + ppp_fsm_recv_conf_rej(&lcp->fsm); + break; + case TERMREQ: + term_msg=strndup((char*)(hdr+1),ntohs(hdr->len)); + log_debug("recv [LCP TermReq id=%x \"%s\"]\n",hdr->id,term_msg); + free(term_msg); + ppp_fsm_recv_term_req(&lcp->fsm); + ppp_terminate(lcp->ppp, 0); + break; + case TERMACK: + term_msg=strndup((char*)(hdr+1),ntohs(hdr->len)); + log_debug("recv [LCP TermAck id=%x \"%s\"]\n",hdr->id,term_msg); + free(term_msg); + ppp_fsm_recv_term_ack(&lcp->fsm); + break; + case CODEREJ: + log_debug("recv [LCP CodeRej id=%x]\n",hdr->id); + ppp_fsm_recv_code_rej_bad(&lcp->fsm); + break; + case ECHOREQ: + send_echo_reply(lcp); + break; + case ECHOREP: + lcp_recv_echo_repl(lcp,(uint8_t*)(hdr+1),ntohs(hdr->len)-PPP_HDRLEN); + break; + default: + ppp_fsm_recv_unk(&lcp->fsm); + break; + } +} + +int lcp_option_register(struct lcp_option_handler_t *h) +{ + /*struct lcp_option_drv_t *p; + + list_for_each_entry(p,option_drv_list,entry) + if (p->id==h->id) + return -1;*/ + + list_add_tail(&h->entry,&option_handlers); + + return 0; +} + +static struct ppp_layer_t lcp_layer= +{ + .init=lcp_layer_init, + .start=lcp_layer_start, + .finish=lcp_layer_finish, + .free=lcp_layer_free, +}; + +static void __init lcp_init(void) +{ + char *opt; + + ppp_register_layer("lcp",&lcp_layer); + + opt = conf_get_opt("lcp", "echo-interval"); + if (opt && atoi(opt) > 0) + conf_echo_interval = atoi(opt); + + opt = conf_get_opt("lcp", "echo-failure"); + if (opt && atoi(opt) > 0) + conf_echo_failure = atoi(opt); + +} diff --git a/accel-pptpd/ppp/ppp_lcp.h b/accel-pptpd/ppp/ppp_lcp.h new file mode 100644 index 0000000..46bc17b --- /dev/null +++ b/accel-pptpd/ppp/ppp_lcp.h @@ -0,0 +1,134 @@ +#ifndef PPP_LCP_H +#define PPP_LCP_H + +#include + +#include "triton.h" +#include "ppp_fsm.h" +/* + * Options. + */ +#define CI_VENDOR 0 /* Vendor Specific */ +#define CI_MRU 1 /* Maximum Receive Unit */ +#define CI_ASYNCMAP 2 /* Async Control Character Map */ +#define CI_AUTH 3 /* Authentication Type */ +#define CI_QUALITY 4 /* Quality Protocol */ +#define CI_MAGIC 5 /* Magic Number */ +#define CI_PCOMP 7 /* Protocol Field Compression */ +#define CI_ACCOMP 8 /* Address/Control Field Compression */ +#define CI_FCSALTERN 9 /* FCS-Alternatives */ +#define CI_SDP 10 /* Self-Describing-Pad */ +#define CI_NUMBERED 11 /* Numbered-Mode */ +#define CI_CALLBACK 13 /* callback */ +#define CI_MRRU 17 /* max reconstructed receive unit; multilink */ +#define CI_SSNHF 18 /* short sequence numbers for multilink */ +#define CI_EPDISC 19 /* endpoint discriminator */ +#define CI_MPPLUS 22 /* Multi-Link-Plus-Procedure */ +#define CI_LDISC 23 /* Link-Discriminator */ +#define CI_LCPAUTH 24 /* LCP Authentication */ +#define CI_COBS 25 /* Consistent Overhead Byte Stuffing */ +#define CI_PREFELIS 26 /* Prefix Elision */ +#define CI_MPHDRFMT 27 /* MP Header Format */ +#define CI_I18N 28 /* Internationalization */ +#define CI_SDL 29 /* Simple Data Link */ + +struct lcp_hdr_t +{ + uint16_t proto; + uint8_t code; + uint8_t id; + uint16_t len; +} __attribute__((packed)); +struct lcp_opt_hdr_t +{ + uint8_t id; + uint8_t len; +} __attribute__((packed)); +struct lcp_opt8_t +{ + struct lcp_opt_hdr_t hdr; + uint8_t val; +} __attribute__((packed)); +struct lcp_opt16_t +{ + struct lcp_opt_hdr_t hdr; + uint16_t val; +} __attribute__((packed)); +struct lcp_opt32_t +{ + struct lcp_opt_hdr_t hdr; + uint32_t val; +} __attribute__((packed)); + +/*struct lcp_options_t +{ + int magic; + int mtu; + int mru; + int accomp; // 0 - disabled, 1 - enable, 2 - allow, disabled, 3 - allow,enabled + int pcomp; // 0 - disabled, 1 - enable, 2 - allow, disabled, 3 - allow,enabled + // negotiated options; + int neg_mru; + int neg_mtu; + int neg_accomp; // -1 - rejected + int neg_pcomp; + int neg_auth[AUTH_MAX]; +};*/ + +#define LCP_OPT_NONE 0 +#define LCP_OPT_ACK 1 +#define LCP_OPT_NAK -1 +#define LCP_OPT_REJ -2 +#define LCP_OPT_FAIL -3 + +struct ppp_lcp_t; +struct lcp_option_handler_t; + +struct lcp_option_t +{ + struct list_head entry; + int id; + int len; + int state; + struct lcp_option_handler_t *h; +}; + +struct lcp_option_handler_t +{ + struct list_head entry; + struct lcp_option_t* (*init)(struct ppp_lcp_t*); + int (*send_conf_req)(struct ppp_lcp_t*,struct lcp_option_t*,uint8_t*); + int (*send_conf_rej)(struct ppp_lcp_t*,struct lcp_option_t*,uint8_t*); + int (*send_conf_nak)(struct ppp_lcp_t*,struct lcp_option_t*,uint8_t*); + int (*recv_conf_req)(struct ppp_lcp_t*,struct lcp_option_t*,uint8_t*); + int (*recv_conf_rej)(struct ppp_lcp_t*,struct lcp_option_t*,uint8_t*); + int (*recv_conf_nak)(struct ppp_lcp_t*,struct lcp_option_t*,uint8_t*); + int (*recv_conf_ack)(struct ppp_lcp_t*,struct lcp_option_t*,uint8_t*); + void (*free)(struct ppp_lcp_t*,struct lcp_option_t*); + void (*print)(void (*print)(const char *fmt,...), struct lcp_option_t*,uint8_t*); +}; + +struct ppp_lcp_t +{ + struct ppp_layer_data_t ld; + struct ppp_handler_t hnd; + struct ppp_fsm_t fsm; + struct ppp_t *ppp; + struct list_head options; + + struct triton_timer_t echo_timer; + int echo_interval; + int echo_failure; + int echo_sent; + int magic; + + struct list_head ropt_list; // last received ConfReq + int ropt_len; + + int conf_req_len; +}; + +int lcp_option_register(struct lcp_option_handler_t *h); + +#endif + diff --git a/accel-pptpd/ppp_auth.c b/accel-pptpd/ppp_auth.c deleted file mode 100644 index 3208eda..0000000 --- a/accel-pptpd/ppp_auth.c +++ /dev/null @@ -1,320 +0,0 @@ -#include -#include -#include - -#include "ppp.h" -#include "ppp_lcp.h" -#include "log.h" - -#include "ppp_auth.h" - - -static LIST_HEAD(auth_handlers); -static int extra_opt_len=0; - -static struct lcp_option_t *auth_init(struct ppp_lcp_t *lcp); -static void auth_free(struct ppp_lcp_t *lcp, struct lcp_option_t *opt); -static int auth_send_conf_req(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr); -static int auth_recv_conf_req(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr); -static int auth_recv_conf_nak(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr); -static int auth_recv_conf_rej(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr); -static int auth_recv_conf_ack(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr); -static void auth_print(void (*print)(const char *fmt,...),struct lcp_option_t*, uint8_t *ptr); - -static struct ppp_layer_data_t *auth_layer_init(struct ppp_t*); -static void auth_layer_start(struct ppp_layer_data_t *); -static void auth_layer_finish(struct ppp_layer_data_t *); -static void auth_layer_free(struct ppp_layer_data_t *); - -struct auth_option_t -{ - struct lcp_option_t opt; - struct list_head auth_list; - struct auth_data_t *auth; - struct auth_data_t *peer_auth; -}; - -struct auth_layer_data_t -{ - struct ppp_layer_data_t ld; - struct auth_option_t auth_opt; - struct ppp_t *ppp; -}; - -static struct lcp_option_handler_t auth_opt_hnd= -{ - .init=auth_init, - .send_conf_req=auth_send_conf_req, - .send_conf_nak=auth_send_conf_req, - .recv_conf_req=auth_recv_conf_req, - .recv_conf_nak=auth_recv_conf_nak, - .recv_conf_rej=auth_recv_conf_rej, - .recv_conf_ack=auth_recv_conf_ack, - .free=auth_free, - .print=auth_print, -}; - -static struct ppp_layer_t auth_layer= -{ - .init=auth_layer_init, - .start=auth_layer_start, - .finish=auth_layer_finish, - .free=auth_layer_free, -}; - -static struct lcp_option_t *auth_init(struct ppp_lcp_t *lcp) -{ - struct ppp_auth_handler_t *h; - struct auth_data_t *d; - struct auth_layer_data_t *ad; - - ad=container_of(ppp_find_layer_data(lcp->ppp,&auth_layer),typeof(*ad),ld); - - ad->auth_opt.opt.id=CI_AUTH; - ad->auth_opt.opt.len=4+extra_opt_len; - - INIT_LIST_HEAD(&ad->auth_opt.auth_list); - - list_for_each_entry(h,&auth_handlers,entry) - { - d=h->init(lcp->ppp); - d->h=h; - list_add_tail(&d->entry,&ad->auth_opt.auth_list); - } - - return &ad->auth_opt.opt; -} - -static void auth_free(struct ppp_lcp_t *lcp, struct lcp_option_t *opt) -{ - struct auth_option_t *auth_opt=container_of(opt,typeof(*auth_opt),opt); - struct auth_data_t *d; - - while(!list_empty(&auth_opt->auth_list)) - { - d=list_entry(auth_opt->auth_list.next,typeof(*d),entry); - list_del(&d->entry); - d->h->free(lcp->ppp,d); - } -} - -static int auth_send_conf_req(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr) -{ - struct auth_option_t *auth_opt=container_of(opt,typeof(*auth_opt),opt); - struct lcp_opt16_t *opt16=(struct lcp_opt16_t*)ptr; - struct auth_data_t *d; - int n; - - if (list_empty(&auth_opt->auth_list)) return 0; - - if (!auth_opt->auth || auth_opt->auth->state==LCP_OPT_NAK) - { - list_for_each_entry(d,&auth_opt->auth_list,entry) - { - if (d->state==LCP_OPT_NAK || d->state==LCP_OPT_REJ) - continue; - auth_opt->auth=d; - break; - } - } - - opt16->hdr.id=CI_AUTH; - opt16->val=htons(auth_opt->auth->proto); - n=auth_opt->auth->h->send_conf_req(lcp->ppp,auth_opt->auth,(uint8_t*)(opt16+1)); - opt16->hdr.len=4+n; - - return 4+n; -} - -static int auth_recv_conf_req(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr) -{ - struct auth_option_t *auth_opt=container_of(opt,typeof(*auth_opt),opt); - struct lcp_opt16_t *opt16=(struct lcp_opt16_t*)ptr; - struct auth_data_t *d; - int r; - - if (list_empty(&auth_opt->auth_list)) - return LCP_OPT_REJ; - - list_for_each_entry(d,&auth_opt->auth_list,entry) - { - if (d->proto==ntohs(opt16->val)) - { - r=d->h->recv_conf_req(lcp->ppp,d,(uint8_t*)(opt16+1)); - if (r==LCP_OPT_FAIL) - return LCP_OPT_FAIL; - if (r==LCP_OPT_REJ) - break; - auth_opt->peer_auth=d; - return r; - } - } - - list_for_each_entry(d,&auth_opt->auth_list,entry) - { - if (d->state!=LCP_OPT_NAK) - { - auth_opt->peer_auth=d; - return LCP_OPT_NAK; - } - } - - log_msg("cann't negotiate authentication type\n"); - return LCP_OPT_FAIL; -} - -static int auth_recv_conf_ack(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr) -{ - struct auth_option_t *auth_opt=container_of(opt,typeof(*auth_opt),opt); - - auth_opt->peer_auth=NULL; - - return 0; -} - -static int auth_recv_conf_nak(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr) -{ - struct auth_option_t *auth_opt=container_of(opt,typeof(*auth_opt),opt); - struct auth_data_t *d; - - if (!auth_opt->auth) - { - log_error("auth: unexcepcted configure-nak\n"); - return -1; - } - auth_opt->auth->state=LCP_OPT_NAK; - if (auth_opt->peer_auth) - auth_opt->auth=auth_opt->peer_auth; - - list_for_each_entry(d,&auth_opt->auth_list,entry) - { - if (d->state!=LCP_OPT_NAK) - return 0; - } - - log_msg("cann't negotiate authentication type\n"); - return -1; -} - -static int auth_recv_conf_rej(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr) -{ - struct auth_option_t *auth_opt=container_of(opt,typeof(*auth_opt),opt); - struct auth_data_t *d; - - if (!auth_opt->auth) - { - log_error("auth: unexcepcted configure-reject\n"); - return -1; - } - auth_opt->auth->state=LCP_OPT_NAK; - if (auth_opt->peer_auth) - auth_opt->auth=auth_opt->peer_auth; - - list_for_each_entry(d,&auth_opt->auth_list,entry) - { - if (d->state!=LCP_OPT_NAK) - return 0; - } - - log_msg("cann't negotiate authentication type\n"); - return -1; -} - -static void auth_print(void (*print)(const char *fmt,...),struct lcp_option_t *opt, uint8_t *ptr) -{ - struct auth_option_t *auth_opt=container_of(opt,typeof(*auth_opt),opt); - struct lcp_opt16_t *opt16=(struct lcp_opt16_t*)ptr; - struct auth_data_t *d; - - if (ptr) - { - list_for_each_entry(d,&auth_opt->auth_list,entry) - { - if (d->proto==ntohs(opt16->val)) - goto print_d; - } - - print("",ntohs(opt16->val)); - return; - } - else if (auth_opt->auth) d=auth_opt->auth; - else return; - -print_d: - print("",d->h->name); -} - -static struct ppp_layer_data_t *auth_layer_init(struct ppp_t *ppp) -{ - struct auth_layer_data_t *ad=(struct auth_layer_data_t*)malloc(sizeof(*ad)); - - log_debug("auth_layer_init\n"); - - memset(ad,0,sizeof(*ad)); - - ad->ppp=ppp; - - return &ad->ld; -} - -static void auth_layer_start(struct ppp_layer_data_t *ld) -{ - struct auth_layer_data_t *ad=container_of(ld,typeof(*ad),ld); - - log_debug("auth_layer_start\n"); - - if (ad->auth_opt.auth) - ad->auth_opt.auth->h->start(ad->ppp,ad->auth_opt.auth); - else - { - log_debug("auth_layer_started\n"); - ppp_layer_started(ad->ppp,ld); - } -} - -static void auth_layer_finish(struct ppp_layer_data_t *ld) -{ - struct auth_layer_data_t *ad=container_of(ld,typeof(*ad),ld); - - log_debug("auth_layer_finish\n"); - - if (ad->auth_opt.auth) - ad->auth_opt.auth->h->finish(ad->ppp,ad->auth_opt.auth); - - log_debug("auth_layer_finished\n"); - ppp_layer_finished(ad->ppp,ld); -} - -static void auth_layer_free(struct ppp_layer_data_t *ld) -{ - struct auth_layer_data_t *ad=container_of(ld,typeof(*ad),ld); - - log_debug("auth_layer_free\n"); - - free(ad); -} - -void auth_successed(struct ppp_t *ppp) -{ - struct auth_layer_data_t *ad=container_of(ppp_find_layer_data(ppp,&auth_layer),typeof(*ad),ld); - log_debug("auth_layer_started\n"); - ppp_layer_started(ppp,&ad->ld); -} - -void auth_failed(struct ppp_t *ppp) -{ - ppp_terminate(ppp, 0); -} - -int ppp_auth_register_handler(struct ppp_auth_handler_t *h) -{ - list_add_tail(&h->entry,&auth_handlers); - return 0; -} - -static void __init ppp_auth_init() -{ - ppp_register_layer("auth",&auth_layer); - lcp_option_register(&auth_opt_hnd); -} - diff --git a/accel-pptpd/ppp_auth.h b/accel-pptpd/ppp_auth.h deleted file mode 100644 index f1880d5..0000000 --- a/accel-pptpd/ppp_auth.h +++ /dev/null @@ -1,34 +0,0 @@ -#ifndef PPP_AUTH_H -#define PPP_AUTH_H - -#include "list.h" - -struct ppp_auth_handler_t; - -struct auth_data_t -{ - struct list_head entry; - int proto; - int state; - struct ppp_auth_handler_t *h; -}; - -struct ppp_auth_handler_t -{ - struct list_head entry; - const char *name; - struct auth_data_t* (*init)(struct ppp_t*); - int (*send_conf_req)(struct ppp_t*, struct auth_data_t*, uint8_t*); - int (*recv_conf_req)(struct ppp_t*, struct auth_data_t*, uint8_t*); - int (*start)(struct ppp_t*, struct auth_data_t*); - int (*finish)(struct ppp_t*, struct auth_data_t*); - void (*free)(struct ppp_t*,struct auth_data_t*); -}; - -int ppp_auth_register_handler(struct ppp_auth_handler_t*); - -void auth_successed(struct ppp_t *ppp); -void auth_failed(struct ppp_t *ppp); - -#endif - diff --git a/accel-pptpd/ppp_ccp.c b/accel-pptpd/ppp_ccp.c deleted file mode 100644 index 924df7d..0000000 --- a/accel-pptpd/ppp_ccp.c +++ /dev/null @@ -1,577 +0,0 @@ -#include -#include -#include -#include -#include - -#include "triton/triton.h" - -#include "log.h" - -#include "ppp.h" -#include "ppp_ccp.h" - -struct recv_opt_t -{ - struct list_head entry; - struct ccp_opt_hdr_t *hdr; - int len; - int state; - struct ccp_option_t *lopt; -}; - -static LIST_HEAD(option_handlers); - -static void ccp_layer_up(struct ppp_fsm_t*); -static void ccp_layer_down(struct ppp_fsm_t*); -static void send_conf_req(struct ppp_fsm_t*); -static void send_conf_ack(struct ppp_fsm_t*); -static void send_conf_nak(struct ppp_fsm_t*); -static void send_conf_rej(struct ppp_fsm_t*); -static void ccp_recv(struct ppp_handler_t*); - -static void ccp_options_init(struct ppp_ccp_t *ccp) -{ - struct ccp_option_t *lopt; - struct ccp_option_handler_t *h; - - INIT_LIST_HEAD(&ccp->options); - - list_for_each_entry(h,&option_handlers,entry) - { - lopt=h->init(ccp); - if (lopt) - { - lopt->h=h; - list_add_tail(&lopt->entry,&ccp->options); - ccp->conf_req_len+=lopt->len; - } - } -} - -static void ccp_options_free(struct ppp_ccp_t *ccp) -{ - struct ccp_option_t *lopt; - - while(!list_empty(&ccp->options)) - { - lopt=list_entry(ccp->options.next,typeof(*lopt),entry); - list_del(&lopt->entry); - lopt->h->free(ccp,lopt); - } -} - -static struct ppp_layer_data_t *ccp_layer_init(struct ppp_t *ppp) -{ - struct ppp_ccp_t *ccp=malloc(sizeof(*ccp)); - memset(ccp,0,sizeof(*ccp)); - - log_debug("ccp_layer_init\n"); - - ccp->ppp=ppp; - ccp->fsm.ppp=ppp; - - ccp->hnd.proto=PPP_CCP; - ccp->hnd.recv=ccp_recv; - - ppp_register_unit_handler(ppp,&ccp->hnd); - - ppp_fsm_init(&ccp->fsm); - - ccp->fsm.layer_up=ccp_layer_up; - ccp->fsm.layer_finished=ccp_layer_down; - ccp->fsm.send_conf_req=send_conf_req; - ccp->fsm.send_conf_ack=send_conf_ack; - ccp->fsm.send_conf_nak=send_conf_nak; - ccp->fsm.send_conf_rej=send_conf_rej; - - INIT_LIST_HEAD(&ccp->ropt_list); - - return &ccp->ld; -} - -void ccp_layer_start(struct ppp_layer_data_t *ld) -{ - struct ppp_ccp_t *ccp=container_of(ld,typeof(*ccp),ld); - - log_debug("ccp_layer_start\n"); - - ccp_options_init(ccp); - ppp_fsm_lower_up(&ccp->fsm); - ppp_fsm_open(&ccp->fsm); -} - -void ccp_layer_finish(struct ppp_layer_data_t *ld) -{ - struct ppp_ccp_t *ccp=container_of(ld,typeof(*ccp),ld); - - log_debug("ccp_layer_finish\n"); - - ppp_fsm_lower_down(&ccp->fsm); - - ppp_unregister_handler(ccp->ppp,&ccp->hnd); - ccp_options_free(ccp); - - ppp_layer_finished(ccp->ppp,ld); -} - -void ccp_layer_free(struct ppp_layer_data_t *ld) -{ - struct ppp_ccp_t *ccp=container_of(ld,typeof(*ccp),ld); - - log_debug("ccp_layer_free\n"); - - free(ccp); -} - -static void ccp_layer_up(struct ppp_fsm_t *fsm) -{ - struct ppp_ccp_t *ccp=container_of(fsm,typeof(*ccp),fsm); - log_debug("ccp_layer_started\n"); - ppp_layer_started(ccp->ppp,&ccp->ld); -} - -static void ccp_layer_down(struct ppp_fsm_t *fsm) -{ - struct ppp_ccp_t *ccp=container_of(fsm,typeof(*ccp),fsm); - log_debug("ccp_layer_finished\n"); - ppp_layer_finished(ccp->ppp,&ccp->ld); -} - -static void print_ropt(struct recv_opt_t *ropt) -{ - int i; - uint8_t *ptr=(uint8_t*)ropt->hdr; - - log_debug(" <"); - for(i=0; ilen; i++) - { - log_debug(" %x",ptr[i]); - } - log_debug(" >"); -} - -static void send_conf_req(struct ppp_fsm_t *fsm) -{ - struct ppp_ccp_t *ccp=container_of(fsm,typeof(*ccp),fsm); - uint8_t *buf=malloc(ccp->conf_req_len), *ptr=buf; - struct ccp_hdr_t *ccp_hdr=(struct ccp_hdr_t*)ptr; - struct ccp_option_t *lopt; - int n; - - log_debug("send [CCP ConfReq"); - ccp_hdr->proto=htons(PPP_CCP); - ccp_hdr->code=CONFREQ; - ccp_hdr->id=++ccp->fsm.id; - ccp_hdr->len=0; - log_debug(" id=%x",ccp_hdr->id); - - ptr+=sizeof(*ccp_hdr); - - list_for_each_entry(lopt,&ccp->options,entry) - { - n=lopt->h->send_conf_req(ccp,lopt,ptr); - if (n) - { - log_debug(" "); - lopt->h->print(log_debug,lopt,NULL); - ptr+=n; - } - } - - log_debug("]\n"); - - ccp_hdr->len=htons((ptr-buf)-2); - ppp_unit_send(ccp->ppp,ccp_hdr,ptr-buf); -} - -static void send_conf_ack(struct ppp_fsm_t *fsm) -{ - struct ppp_ccp_t *ccp=container_of(fsm,typeof(*ccp),fsm); - struct ccp_hdr_t *hdr=(struct ccp_hdr_t*)ccp->ppp->unit_buf; - - hdr->code=CONFACK; - log_debug("send [CCP ConfAck id=%x ]\n",ccp->fsm.recv_id); - - ppp_unit_send(ccp->ppp,hdr,ntohs(hdr->len)+2); -} - -static void send_conf_nak(struct ppp_fsm_t *fsm) -{ - struct ppp_ccp_t *ccp=container_of(fsm,typeof(*ccp),fsm); - uint8_t *buf=malloc(ccp->conf_req_len), *ptr=buf; - struct ccp_hdr_t *ccp_hdr=(struct ccp_hdr_t*)ptr; - struct recv_opt_t *ropt; - - log_debug("send [CCP ConfNak id=%x",ccp->fsm.recv_id); - - ccp_hdr->proto=htons(PPP_CCP); - ccp_hdr->code=CONFNAK; - ccp_hdr->id=ccp->fsm.recv_id; - ccp_hdr->len=0; - - ptr+=sizeof(*ccp_hdr); - - list_for_each_entry(ropt,&ccp->ropt_list,entry) - { - if (ropt->state==CCP_OPT_NAK) - { - log_debug(" "); - ropt->lopt->h->print(log_debug,ropt->lopt,NULL); - ptr+=ropt->lopt->h->send_conf_nak(ccp,ropt->lopt,ptr); - } - } - - log_debug("]\n"); - - ccp_hdr->len=htons((ptr-buf)-2); - ppp_unit_send(ccp->ppp,ccp_hdr,ptr-buf); -} - -static void send_conf_rej(struct ppp_fsm_t *fsm) -{ - struct ppp_ccp_t *ccp=container_of(fsm,typeof(*ccp),fsm); - uint8_t *buf=malloc(ccp->ropt_len), *ptr=buf; - struct ccp_hdr_t *ccp_hdr=(struct ccp_hdr_t*)ptr; - struct recv_opt_t *ropt; - - log_debug("send [CCP ConfRej id=%x ",ccp->fsm.recv_id); - - ccp_hdr->proto=htons(PPP_CCP); - ccp_hdr->code=CONFREJ; - ccp_hdr->id=ccp->fsm.recv_id; - ccp_hdr->len=0; - - ptr+=sizeof(*ccp_hdr); - - list_for_each_entry(ropt,&ccp->ropt_list,entry) - { - if (ropt->state==CCP_OPT_REJ) - { - log_debug(" "); - if (ropt->lopt) ropt->lopt->h->print(log_debug,ropt->lopt,(uint8_t*)ropt->hdr); - else print_ropt(ropt); - memcpy(ptr,ropt->hdr,ropt->len); - ptr+=ropt->len; - } - } - - log_debug("]\n"); - - ccp_hdr->len=htons((ptr-buf)-2); - ppp_unit_send(ccp->ppp,ccp_hdr,ptr-buf); -} - -static int ccp_recv_conf_req(struct ppp_ccp_t *ccp,uint8_t *data,int size) -{ - struct ccp_opt_hdr_t *hdr; - struct recv_opt_t *ropt; - struct ccp_option_t *lopt; - int r,ret=1,ack=0; - - ccp->ropt_len=size; - - while(size>0) - { - hdr=(struct ccp_opt_hdr_t *)data; - - ropt=malloc(sizeof(*ropt)); - memset(ropt,0,sizeof(*ropt)); - if (hdr->len>size) ropt->len=size; - else ropt->len=hdr->len; - ropt->hdr=hdr; - ropt->state=CCP_OPT_NONE; - list_add_tail(&ropt->entry,&ccp->ropt_list); - - data+=ropt->len; - size-=ropt->len; - } - - list_for_each_entry(lopt,&ccp->options,entry) - lopt->state=CCP_OPT_NONE; - - log_debug("recv [CCP ConfReq id=%x",ccp->fsm.recv_id); - list_for_each_entry(ropt,&ccp->ropt_list,entry) - { - list_for_each_entry(lopt,&ccp->options,entry) - { - if (lopt->id==ropt->hdr->id) - { - log_debug(" "); - lopt->h->print(log_debug,lopt,(uint8_t*)ropt->hdr); - r=lopt->h->recv_conf_req(ccp,lopt,(uint8_t*)ropt->hdr); - if (ack) - { - lopt->state=CCP_OPT_REJ; - ropt->state=CCP_OPT_REJ; - }else - { - lopt->state=r; - ropt->state=r; - } - ropt->lopt=lopt; - if (rstate==CCP_OPT_ACK || ropt->state==CCP_OPT_NAK) - ack=1; - else if (!ropt->lopt) - { - log_debug(" "); - print_ropt(ropt); - ropt->state=CCP_OPT_REJ; - ret=CCP_OPT_REJ; - } - } - log_debug("]\n"); - - /*list_for_each_entry(lopt,&ccp->options,entry) - { - if (lopt->state==CCP_OPT_NONE) - { - r=lopt->h->recv_conf_req(ccp,lopt,NULL); - lopt->state=r; - if (rropt_list)) - { - ropt=list_entry(ccp->ropt_list.next,typeof(*ropt),entry); - list_del(&ropt->entry); - free(ropt); - } -} - -static int ccp_recv_conf_rej(struct ppp_ccp_t *ccp,uint8_t *data,int size) -{ - struct ccp_opt_hdr_t *hdr; - struct ccp_option_t *lopt; - int res=0; - - log_debug("recv [CCP ConfRej id=%x",ccp->fsm.recv_id); - - if (ccp->fsm.recv_id!=ccp->fsm.id) - { - log_debug(": id mismatch ]\n"); - return 0; - } - - while(size>0) - { - hdr=(struct ccp_opt_hdr_t *)data; - - list_for_each_entry(lopt,&ccp->options,entry) - { - if (lopt->id==hdr->id) - { - if (!lopt->h->recv_conf_rej) - res=-1; - else if (lopt->h->recv_conf_rej(ccp,lopt,data)) - res=-1; - break; - } - } - - data+=hdr->len; - size-=hdr->len; - } - log_debug("]\n"); - return res; -} - -static int ccp_recv_conf_nak(struct ppp_ccp_t *ccp,uint8_t *data,int size) -{ - struct ccp_opt_hdr_t *hdr; - struct ccp_option_t *lopt; - int res=0; - - log_debug("recv [CCP ConfNak id=%x",ccp->fsm.recv_id); - - if (ccp->fsm.recv_id!=ccp->fsm.id) - { - log_debug(": id mismatch ]\n"); - return 0; - } - - while(size>0) - { - hdr=(struct ccp_opt_hdr_t *)data; - - list_for_each_entry(lopt,&ccp->options,entry) - { - if (lopt->id==hdr->id) - { - log_debug(" "); - lopt->h->print(log_debug,lopt,data); - if (lopt->h->recv_conf_nak(ccp,lopt,data)) - res=-1; - break; - } - } - - data+=hdr->len; - size-=hdr->len; - } - log_debug("]\n"); - return res; -} - -static int ccp_recv_conf_ack(struct ppp_ccp_t *ccp,uint8_t *data,int size) -{ - struct ccp_opt_hdr_t *hdr; - struct ccp_option_t *lopt; - int res=0; - - log_debug("recv [CCP ConfAck id=%x",ccp->fsm.recv_id); - - if (ccp->fsm.recv_id!=ccp->fsm.id) - { - log_debug(": id mismatch ]\n"); - return 0; - } - - while(size>0) - { - hdr=(struct ccp_opt_hdr_t *)data; - - list_for_each_entry(lopt,&ccp->options,entry) - { - if (lopt->id==hdr->id) - { - log_debug(" "); - lopt->h->print(log_debug,lopt,data); - if (!lopt->h->recv_conf_ack) - break; - if (lopt->h->recv_conf_ack(ccp,lopt,data)) - res=-1; - break; - } - } - - data+=hdr->len; - size-=hdr->len; - } - log_debug("]\n"); - return res; -} - -static void ccp_recv(struct ppp_handler_t*h) -{ - struct ccp_hdr_t *hdr; - struct ppp_ccp_t *ccp=container_of(h,typeof(*ccp),hnd); - int r; - char *term_msg; - - if (ccp->fsm.fsm_state==FSM_Initial || ccp->fsm.fsm_state==FSM_Closed) - { - log_error("CCP: discaring packet\n"); - return; - } - - if (ccp->ppp->unit_buf_sizeppp->unit_buf; - if (ntohs(hdr->len)fsm.recv_id=hdr->id; - switch(hdr->code) - { - case CONFREQ: - r=ccp_recv_conf_req(ccp,(uint8_t*)(hdr+1),ntohs(hdr->len)-PPP_HDRLEN); - switch(r) - { - case CCP_OPT_ACK: - ppp_fsm_recv_conf_req_ack(&ccp->fsm); - break; - case CCP_OPT_NAK: - ppp_fsm_recv_conf_req_nak(&ccp->fsm); - break; - case CCP_OPT_REJ: - ppp_fsm_recv_conf_req_rej(&ccp->fsm); - break; - } - ccp_free_conf_req(ccp); - if (r==CCP_OPT_FAIL) - ppp_terminate(ccp->ppp, 0); - break; - case CONFACK: - if (ccp_recv_conf_ack(ccp,(uint8_t*)(hdr+1),ntohs(hdr->len)-PPP_HDRLEN)) - ppp_terminate(ccp->ppp, 0); - else - ppp_fsm_recv_conf_ack(&ccp->fsm); - break; - case CONFNAK: - ccp_recv_conf_nak(ccp,(uint8_t*)(hdr+1),ntohs(hdr->len)-PPP_HDRLEN); - ppp_fsm_recv_conf_rej(&ccp->fsm); - break; - case CONFREJ: - if (ccp_recv_conf_rej(ccp,(uint8_t*)(hdr+1),ntohs(hdr->len)-PPP_HDRLEN)) - ppp_terminate(ccp->ppp, 0); - else - ppp_fsm_recv_conf_rej(&ccp->fsm); - break; - case TERMREQ: - term_msg=strndup((char*)(hdr+1),ntohs(hdr->len)); - log_debug("recv [CCP TermReq id=%x \"%s\"]\n",hdr->id,term_msg); - free(term_msg); - ppp_fsm_recv_term_req(&ccp->fsm); - ppp_terminate(ccp->ppp, 0); - break; - case TERMACK: - term_msg=strndup((char*)(hdr+1),ntohs(hdr->len)); - log_debug("recv [CCP TermAck id=%x \"%s\"]\n",hdr->id,term_msg); - free(term_msg); - ppp_fsm_recv_term_ack(&ccp->fsm); - break; - case CODEREJ: - log_debug("recv [CCP CodeRej id=%x]\n",hdr->id); - ppp_fsm_recv_code_rej_bad(&ccp->fsm); - break; - default: - ppp_fsm_recv_unk(&ccp->fsm); - break; - } -} - -int ccp_option_register(struct ccp_option_handler_t *h) -{ - /*struct ccp_option_drv_t *p; - - list_for_each_entry(p,option_drv_list,entry) - if (p->id==h->id) - return -1;*/ - - list_add_tail(&h->entry,&option_handlers); - - return 0; -} - -static struct ppp_layer_t ccp_layer= -{ - .init=ccp_layer_init, - .start=ccp_layer_start, - .finish=ccp_layer_finish, - .free=ccp_layer_free, -}; - -static void __init ccp_init(void) -{ - ppp_register_layer("ccp",&ccp_layer); -} diff --git a/accel-pptpd/ppp_ccp.h b/accel-pptpd/ppp_ccp.h deleted file mode 100644 index 6aca0ab..0000000 --- a/accel-pptpd/ppp_ccp.h +++ /dev/null @@ -1,94 +0,0 @@ -#ifndef PPP_CCP_H -#define PPP_CCP_H - -#include - -#include "triton/triton.h" -#include "ppp_fsm.h" -/* - * Options. - */ -#define CI_COMP 2 /* IP-Compress-Protocol */ -#define CI_ADDR 3 /* IP-Address */ -#define CI_DNS1 129 /* Primary-DNS-Address */ -#define CI_DNS2 131 /* Secondary-DNS-Address */ - -struct ccp_hdr_t -{ - uint16_t proto; - uint8_t code; - uint8_t id; - uint16_t len; -} __attribute__((packed)); -struct ccp_opt_hdr_t -{ - uint8_t id; - uint8_t len; -} __attribute__((packed)); -struct ccp_opt8_t -{ - struct ccp_opt_hdr_t hdr; - uint8_t val; -} __attribute__((packed)); -struct ccp_opt16_t -{ - struct ccp_opt_hdr_t hdr; - uint16_t val; -} __attribute__((packed)); -struct ccp_opt32_t -{ - struct ccp_opt_hdr_t hdr; - uint32_t val; -} __attribute__((packed)); - -#define CCP_OPT_NONE 0 -#define CCP_OPT_ACK 1 -#define CCP_OPT_NAK -1 -#define CCP_OPT_REJ -2 -#define CCP_OPT_FAIL -3 - -struct ppp_ccp_t; -struct ccp_option_handler_t; - -struct ccp_option_t -{ - struct list_head entry; - int id; - int len; - int state; - struct ccp_option_handler_t *h; -}; - -struct ccp_option_handler_t -{ - struct list_head entry; - struct ccp_option_t* (*init)(struct ppp_ccp_t*); - int (*send_conf_req)(struct ppp_ccp_t*,struct ccp_option_t*,uint8_t*); - int (*send_conf_rej)(struct ppp_ccp_t*,struct ccp_option_t*,uint8_t*); - int (*send_conf_nak)(struct ppp_ccp_t*,struct ccp_option_t*,uint8_t*); - int (*recv_conf_req)(struct ppp_ccp_t*,struct ccp_option_t*,uint8_t*); - int (*recv_conf_rej)(struct ppp_ccp_t*,struct ccp_option_t*,uint8_t*); - int (*recv_conf_nak)(struct ppp_ccp_t*,struct ccp_option_t*,uint8_t*); - int (*recv_conf_ack)(struct ppp_ccp_t*,struct ccp_option_t*,uint8_t*); - void (*free)(struct ppp_ccp_t*,struct ccp_option_t*); - void (*print)(void (*print)(const char *fmt,...), struct ccp_option_t*,uint8_t*); -}; - -struct ppp_ccp_t -{ - struct ppp_layer_data_t ld; - struct ppp_handler_t hnd; - struct ppp_fsm_t fsm; - struct ppp_t *ppp; - struct list_head options; - - struct list_head ropt_list; // last received ConfReq - int ropt_len; - - int conf_req_len; -}; - -int ccp_option_register(struct ccp_option_handler_t *h); - -#endif - diff --git a/accel-pptpd/ppp_fsm.c b/accel-pptpd/ppp_fsm.c deleted file mode 100644 index c2d00dc..0000000 --- a/accel-pptpd/ppp_fsm.c +++ /dev/null @@ -1,534 +0,0 @@ -#include -#include - -#include "triton/triton.h" - -#include "ppp.h" -#include "ppp_fsm.h" -#include "ppp_lcp.h" -#include "log.h" - -static int conf_max_terminate = 2; -static int conf_max_configure = 5; -static int conf_max_failure = 5; -static int conf_timeout = 3; - -void send_term_req(struct ppp_fsm_t *layer); -void send_term_ack(struct ppp_fsm_t *layer); -void send_echo_reply(struct ppp_fsm_t *layer); - -static void init_req_counter(struct ppp_fsm_t *layer,int timeout); -static void zero_req_counter(struct ppp_fsm_t *layer); -static void restart_timer_func(struct triton_timer_t *t); -static void stop_timer(struct ppp_fsm_t *fsm); - -void ppp_fsm_init(struct ppp_fsm_t *layer) -{ - layer->fsm_state = FSM_Initial; - layer->restart_timer.expire = restart_timer_func; - layer->restart_counter = 0; - - layer->max_terminate = conf_max_terminate; - layer->max_configure = conf_max_configure; - layer->max_failure = conf_max_failure; - layer->timeout = conf_timeout; -} -void ppp_fsm_free(struct ppp_fsm_t *layer) -{ - stop_timer(layer); -} - -void ppp_fsm_lower_up(struct ppp_fsm_t *layer) -{ - switch(layer->fsm_state) - { - case FSM_Initial: - layer->fsm_state=FSM_Closed; - break; - case FSM_Starting: - //if (layer->init_req_cnt) layer->init_req_cnt(layer); - init_req_counter(layer,layer->max_configure); - --layer->restart_counter; - if (layer->send_conf_req) layer->send_conf_req(layer); - layer->fsm_state=FSM_Req_Sent; - break; - default: - break; - } -} - -void ppp_fsm_lower_down(struct ppp_fsm_t *layer) -{ - switch(layer->fsm_state) - { - case FSM_Closed: - case FSM_Closing: - layer->fsm_state=FSM_Initial; - break; - case FSM_Stopped: - if (layer->layer_started) layer->layer_started(layer); - layer->fsm_state=FSM_Starting; - break; - case FSM_Stopping: - case FSM_Req_Sent: - case FSM_Ack_Rcvd: - case FSM_Ack_Sent: - layer->fsm_state=FSM_Starting; - break; - case FSM_Opened: - if (layer->layer_down) layer->layer_down(layer); - layer->fsm_state=FSM_Starting; - break; - default: - break; - } -} - -void ppp_fsm_open(struct ppp_fsm_t *layer) -{ - switch(layer->fsm_state) - { - case FSM_Initial: - if (layer->layer_started) layer->layer_started(layer); - layer->fsm_state=FSM_Starting; - break; - case FSM_Starting: - break; - case FSM_Closed: - //if (layer->init_req_cnt) layer->init_req_cnt(layer); - init_req_counter(layer,layer->max_configure); - --layer->restart_counter; - if (layer->send_conf_req) layer->send_conf_req(layer); - layer->fsm_state=FSM_Req_Sent; - break; - case FSM_Closing: - case FSM_Stopping: - case FSM_Stopped: - case FSM_Opened: - ppp_fsm_lower_down(layer); - ppp_fsm_lower_up(layer); - break; - default: - break; - } -} - -void ppp_fsm_close(struct ppp_fsm_t *layer) -{ - switch(layer->fsm_state) - { - case FSM_Starting: - if (layer->layer_finished) layer->layer_finished(layer); - layer->fsm_state=FSM_Initial; - break; - case FSM_Stopped: - layer->fsm_state=FSM_Closed; - break; - case FSM_Stopping: - layer->fsm_state=FSM_Closing; - break; - case FSM_Opened: - if (layer->layer_down) layer->layer_down(layer); - case FSM_Req_Sent: - case FSM_Ack_Rcvd: - case FSM_Ack_Sent: - //if (layer->init_req_cnt) layer->init_req_cnt(layer); - init_req_counter(layer,layer->max_terminate); - send_term_req(layer); - layer->fsm_state=FSM_Closing; - break; - default: - break; - } -} - -void ppp_fsm_timeout0(struct ppp_fsm_t *layer) -{ - switch(layer->fsm_state) - { - case FSM_Closing: - case FSM_Stopping: - send_term_req(layer); - break; - case FSM_Ack_Rcvd: - layer->fsm_state=FSM_Req_Sent; - case FSM_Req_Sent: - case FSM_Ack_Sent: - --layer->restart_counter; - if (layer->send_conf_req) layer->send_conf_req(layer); - break; - default: - break; - } -} - -void ppp_fsm_timeout1(struct ppp_fsm_t *layer) -{ - switch(layer->fsm_state) - { - case FSM_Closing: - stop_timer(layer); - if (layer->layer_finished) layer->layer_finished(layer); - layer->fsm_state=FSM_Closed; - break; - case FSM_Stopping: - stop_timer(layer); - if (layer->layer_finished) layer->layer_finished(layer); - layer->fsm_state=FSM_Stopped; - break; - case FSM_Ack_Rcvd: - case FSM_Req_Sent: - case FSM_Ack_Sent: - stop_timer(layer); - if (layer->layer_finished) layer->layer_finished(layer); - layer->fsm_state=FSM_Stopped; - break; - default: - break; - } -} - -void ppp_fsm_recv_conf_req_ack(struct ppp_fsm_t *layer) -{ - switch(layer->fsm_state) - { - case FSM_Closed: - send_term_ack(layer); - break; - case FSM_Stopped: - //if (layer->init_req_cnt) layer->init_req_cnt(layer); - init_req_counter(layer,layer->max_configure); - --layer->restart_counter; - if (layer->send_conf_req) layer->send_conf_req(layer); - case FSM_Req_Sent: - case FSM_Ack_Sent: - if (layer->send_conf_ack) layer->send_conf_ack(layer); - layer->fsm_state=FSM_Ack_Sent; - break; - case FSM_Ack_Rcvd: - if (layer->send_conf_ack) layer->send_conf_ack(layer); - stop_timer(layer); - if (layer->layer_up) layer->layer_up(layer); - layer->fsm_state=FSM_Opened; - break; - case FSM_Opened: - if (layer->layer_down) layer->layer_down(layer); - --layer->restart_counter; - if (layer->send_conf_req) layer->send_conf_req(layer); - if (layer->send_conf_ack) layer->send_conf_ack(layer); - layer->fsm_state=FSM_Ack_Sent; - break; - default: - break; - } -} - -void ppp_fsm_recv_conf_req_nak(struct ppp_fsm_t *layer) -{ - switch(layer->fsm_state) - { - case FSM_Closed: - send_term_ack(layer); - break; - case FSM_Stopped: - //if (layer->init_req_cnt) layer->init_req_cnt(layer); - init_req_counter(layer,layer->max_configure); - --layer->restart_counter; - if (layer->send_conf_req) layer->send_conf_req(layer); - case FSM_Ack_Sent: - if (layer->send_conf_nak) layer->send_conf_nak(layer); - layer->fsm_state=FSM_Req_Sent; - break; - case FSM_Req_Sent: - case FSM_Ack_Rcvd: - if (layer->send_conf_nak) layer->send_conf_nak(layer); - break; - case FSM_Opened: - if (layer->layer_down) layer->layer_down(layer); - --layer->restart_counter; - if (layer->send_conf_req) layer->send_conf_req(layer); - if (layer->send_conf_nak) layer->send_conf_nak(layer); - layer->fsm_state=FSM_Req_Sent; - break; - default: - break; - } -} - -void ppp_fsm_recv_conf_req_rej(struct ppp_fsm_t *layer) -{ - switch(layer->fsm_state) - { - case FSM_Closed: - send_term_ack(layer); - break; - case FSM_Stopped: - //if (layer->init_req_cnt) layer->init_req_cnt(layer); - init_req_counter(layer,layer->max_configure); - --layer->restart_counter; - if (layer->send_conf_req) layer->send_conf_req(layer); - case FSM_Ack_Sent: - if (layer->send_conf_rej) layer->send_conf_rej(layer); - layer->fsm_state=FSM_Req_Sent; - break; - case FSM_Req_Sent: - case FSM_Ack_Rcvd: - if (layer->send_conf_rej) layer->send_conf_rej(layer); - break; - case FSM_Opened: - if (layer->layer_down) layer->layer_down(layer); - --layer->restart_counter; - if (layer->send_conf_req) layer->send_conf_req(layer); - if (layer->send_conf_rej) layer->send_conf_rej(layer); - layer->fsm_state=FSM_Req_Sent; - break; - default: - break; - } -} - -void ppp_fsm_recv_conf_ack(struct ppp_fsm_t *layer) -{ - switch(layer->fsm_state) - { - case FSM_Closed: - case FSM_Stopped: - send_term_ack(layer); - break; - case FSM_Req_Sent: - //if (layer->init_req_cnt) layer->init_req_cnt(layer); - init_req_counter(layer,layer->max_configure); - layer->fsm_state=FSM_Ack_Rcvd; - break; - case FSM_Ack_Rcvd: - --layer->restart_counter; - if (layer->send_conf_req) layer->send_conf_req(layer); - layer->fsm_state=FSM_Req_Sent; - break; - case FSM_Ack_Sent: - //if (layer->init_req_cnt) layer->init_req_cnt(layer); - //init_req_counter(layer,layer->max_configure); - //tlu - stop_timer(layer); - if (layer->layer_up) layer->layer_up(layer); - layer->fsm_state=FSM_Opened; - break; - case FSM_Opened: - if (layer->layer_down) layer->layer_down(layer); - --layer->restart_counter; - if (layer->send_conf_req) layer->send_conf_req(layer); - layer->fsm_state=FSM_Req_Sent; - default: - break; - } -} - -void ppp_fsm_recv_conf_rej(struct ppp_fsm_t *layer) -{ - switch(layer->fsm_state) - { - case FSM_Closed: - case FSM_Stopped: - send_term_ack(layer); - break; - case FSM_Req_Sent: - //if (layer->init_req_cnt) layer->init_req_cnt(layer); - init_req_counter(layer,layer->max_failure); - --layer->restart_counter; - if (layer->send_conf_req) layer->send_conf_req(layer); - break; - case FSM_Ack_Rcvd: - --layer->restart_counter; - if (layer->send_conf_req) layer->send_conf_req(layer); - layer->fsm_state=FSM_Req_Sent; - break; - case FSM_Ack_Sent: - //if (layer->init_req_cnt) layer->init_req_cnt(layer); - init_req_counter(layer,layer->max_configure); - --layer->restart_counter; - if (layer->send_conf_req) layer->send_conf_req(layer); - break; - case FSM_Opened: - if (layer->layer_down) layer->layer_down(layer); - --layer->restart_counter; - if (layer->send_conf_req) layer->send_conf_req(layer); - layer->fsm_state=FSM_Req_Sent; - break; - default: - break; - } -} - -void ppp_fsm_recv_term_req(struct ppp_fsm_t *layer) -{ - switch(layer->fsm_state) - { - case FSM_Opened: - if (layer->layer_down) layer->layer_down(layer); - send_term_req(layer); - send_term_ack(layer); - //if (layer->zero_req_cnt) layer->zero_req_cnt(layer); - zero_req_counter(layer); - layer->fsm_state=FSM_Stopping; - break; - case FSM_Req_Sent: - case FSM_Ack_Rcvd: - case FSM_Ack_Sent: - send_term_req(layer); - layer->fsm_state=FSM_Req_Sent; - break; - default: - send_term_req(layer); - break; - } -} - -void ppp_fsm_recv_term_ack(struct ppp_fsm_t *layer) -{ - switch(layer->fsm_state) - { - case FSM_Closing: - if (layer->layer_finished) layer->layer_finished(layer); - layer->fsm_state=FSM_Closed; - break; - case FSM_Stopping: - if (layer->layer_finished) layer->layer_finished(layer); - layer->fsm_state=FSM_Stopped; - break; - case FSM_Ack_Rcvd: - layer->fsm_state=FSM_Req_Sent; - break; - case FSM_Opened: - if (layer->layer_down) layer->layer_down(layer); - --layer->restart_counter; - if (layer->send_conf_req) layer->send_conf_req(layer); - layer->fsm_state=FSM_Req_Sent; - break; - default: - break; - } -} - -void ppp_fsm_recv_unk(struct ppp_fsm_t *layer) -{ - if (layer->send_conf_rej) layer->send_conf_rej(layer); -} - -void ppp_fsm_recv_code_rej_perm(struct ppp_fsm_t *layer) -{ - switch(layer->fsm_state) - { - case FSM_Ack_Rcvd: - layer->fsm_state=FSM_Req_Sent; - break; - default: - break; - } -} - -void ppp_fsm_recv_code_rej_bad(struct ppp_fsm_t *layer) -{ - switch(layer->fsm_state) - { - case FSM_Opened: - if (layer->layer_down) layer->layer_down(layer); - send_term_req(layer); - layer->fsm_state=FSM_Stopping; - break; - case FSM_Closing: - if (layer->layer_finished) layer->layer_finished(layer); - layer->fsm_state=FSM_Closed; - break; - case FSM_Stopping: - case FSM_Req_Sent: - case FSM_Ack_Rcvd: - case FSM_Ack_Sent: - if (layer->layer_finished) layer->layer_finished(layer); - layer->fsm_state=FSM_Stopped; - break; - default: - break; - } -} - -void send_term_req(struct ppp_fsm_t *layer) -{ - struct lcp_hdr_t hdr = { - .proto = htons(PPP_LCP), - .code = TERMREQ, - .id = ++layer->id, - .len = htons(4), - }; - - log_debug("send [LCP TermReq id=%i \"\"]\n",hdr.id); - - --layer->restart_counter; - ppp_chan_send(layer->ppp, &hdr, 6); -} -void send_term_ack(struct ppp_fsm_t *layer) -{ - struct lcp_hdr_t hdr = { - .proto = htons(PPP_LCP), - .code = TERMACK, - .id = layer->recv_id, - .len = htons(4), - }; - - log_debug("send [LCP TermAck id=%i \"\"]\n", hdr.id); - - ppp_chan_send(layer->ppp, &hdr, 6); -} - -static void stop_timer(struct ppp_fsm_t *fsm) -{ - if (fsm->restart_timer.period) { - fsm->restart_timer.period = 0; - triton_timer_del(&fsm->restart_timer); - } -} -static void init_req_counter(struct ppp_fsm_t *layer,int timeout) -{ - layer->restart_timer.expire_tv.tv_sec=0; - layer->restart_counter = timeout; - - if (!layer->restart_timer.period) { - layer->restart_timer.period = layer->timeout * 1000; - triton_timer_add(layer->ppp->ctrl->ctx, &layer->restart_timer, 0); - } -} -static void zero_req_counter(struct ppp_fsm_t *layer) -{ - layer->restart_timer.expire_tv.tv_sec=0; - layer->restart_counter=0; -} - -static void restart_timer_func(struct triton_timer_t *t) -{ - struct ppp_fsm_t *layer = container_of(t, typeof(*layer), restart_timer); - - if (layer->restart_counter>0) - ppp_fsm_timeout0(layer); - else - ppp_fsm_timeout1(layer); -} - -void __init fsm_init(void) -{ - char *opt; - - opt = conf_get_opt("lcp", "max-terminate"); - if (opt && atoi(opt) > 0) - conf_max_terminate = atoi(opt); - - opt = conf_get_opt("lcp", "max-configure"); - if (opt && atoi(opt) > 0) - conf_max_configure = atoi(opt); - - opt = conf_get_opt("lcp", "max-failure"); - if (opt && atoi(opt) > 0) - conf_max_failure = atoi(opt); - - opt = conf_get_opt("lcp", "timeout"); - if (opt && atoi(opt) > 0) - conf_timeout = atoi(opt); -} diff --git a/accel-pptpd/ppp_fsm.h b/accel-pptpd/ppp_fsm.h deleted file mode 100644 index bc958fe..0000000 --- a/accel-pptpd/ppp_fsm.h +++ /dev/null @@ -1,65 +0,0 @@ -#ifndef PPP_FSM_H -#define PPP_FSM_H - -typedef enum {FSM_Initial=0,FSM_Starting,FSM_Closed,FSM_Stopped,FSM_Closing,FSM_Stopping,FSM_Req_Sent,FSM_Ack_Rcvd,FSM_Ack_Sent,FSM_Opened} FSM_STATE; -/* - * CP (LCP, IPCP, etc.) codes. - */ -#define CONFREQ 1 /* Configuration Request */ -#define CONFACK 2 /* Configuration Ack */ -#define CONFNAK 3 /* Configuration Nak */ -#define CONFREJ 4 /* Configuration Reject */ -#define TERMREQ 5 /* Termination Request */ -#define TERMACK 6 /* Termination Ack */ -#define CODEREJ 7 /* Code Reject */ -#define ECHOREQ 9 /* Echo Request */ -#define ECHOREP 10 /* Echo Reply */ - -struct ppp_t; - -struct ppp_fsm_t -{ - struct ppp_t *ppp; - FSM_STATE fsm_state; - - struct triton_timer_t restart_timer; - int restart_counter; - int max_terminate; - int max_configure; - int max_failure; - int timeout; - - int id; - int recv_id; - - //fsm handling - void (*layer_up)(struct ppp_fsm_t*); - void (*layer_down)(struct ppp_fsm_t*); - void (*layer_started)(struct ppp_fsm_t*); - void (*layer_finished)(struct ppp_fsm_t*); - void (*send_conf_req)(struct ppp_fsm_t*); - void (*send_conf_ack)(struct ppp_fsm_t*); - void (*send_conf_nak)(struct ppp_fsm_t*); - void (*send_conf_rej)(struct ppp_fsm_t*); -}; - -void ppp_fsm_init(struct ppp_fsm_t*); -void ppp_fsm_free(struct ppp_fsm_t*); - -void ppp_fsm_lower_up(struct ppp_fsm_t*); -void ppp_fsm_lower_down(struct ppp_fsm_t*); -void ppp_fsm_open(struct ppp_fsm_t*); -void ppp_fsm_close(struct ppp_fsm_t*); -void ppp_fsm_timeout0(struct ppp_fsm_t *layer); -void ppp_fsm_timeout1(struct ppp_fsm_t *layer); -void ppp_fsm_recv_conf_req_ack(struct ppp_fsm_t *layer); -void ppp_fsm_recv_conf_req_nak(struct ppp_fsm_t *layer); -void ppp_fsm_recv_conf_req_rej(struct ppp_fsm_t *layer); -void ppp_fsm_recv_conf_ack(struct ppp_fsm_t *layer); -void ppp_fsm_recv_conf_rej(struct ppp_fsm_t *layer); -void ppp_fsm_recv_term_req(struct ppp_fsm_t *layer); -void ppp_fsm_recv_term_ack(struct ppp_fsm_t *layer); -void ppp_fsm_recv_unk(struct ppp_fsm_t *layer); -void ppp_fsm_recv_code_rej_bad(struct ppp_fsm_t *layer); - -#endif diff --git a/accel-pptpd/ppp_ipcp.c b/accel-pptpd/ppp_ipcp.c deleted file mode 100644 index 4acf694..0000000 --- a/accel-pptpd/ppp_ipcp.c +++ /dev/null @@ -1,568 +0,0 @@ -#include -#include -#include -#include -#include - -#include "triton/triton.h" - -#include "log.h" - -#include "ppp.h" -#include "ppp_ipcp.h" - -struct recv_opt_t -{ - struct list_head entry; - struct ipcp_opt_hdr_t *hdr; - int len; - int state; - struct ipcp_option_t *lopt; -}; - -static LIST_HEAD(option_handlers); - -static void ipcp_layer_up(struct ppp_fsm_t*); -static void ipcp_layer_down(struct ppp_fsm_t*); -static void send_conf_req(struct ppp_fsm_t*); -static void send_conf_ack(struct ppp_fsm_t*); -static void send_conf_nak(struct ppp_fsm_t*); -static void send_conf_rej(struct ppp_fsm_t*); -static void ipcp_recv(struct ppp_handler_t*); - -static void ipcp_options_init(struct ppp_ipcp_t *ipcp) -{ - struct ipcp_option_t *lopt; - struct ipcp_option_handler_t *h; - - INIT_LIST_HEAD(&ipcp->options); - - list_for_each_entry(h,&option_handlers,entry) - { - lopt=h->init(ipcp); - if (lopt) - { - lopt->h=h; - list_add_tail(&lopt->entry,&ipcp->options); - ipcp->conf_req_len+=lopt->len; - } - } -} - -static void ipcp_options_free(struct ppp_ipcp_t *ipcp) -{ - struct ipcp_option_t *lopt; - - while(!list_empty(&ipcp->options)) - { - lopt=list_entry(ipcp->options.next,typeof(*lopt),entry); - list_del(&lopt->entry); - lopt->h->free(ipcp,lopt); - } -} - -static struct ppp_layer_data_t *ipcp_layer_init(struct ppp_t *ppp) -{ - struct ppp_ipcp_t *ipcp=malloc(sizeof(*ipcp)); - memset(ipcp,0,sizeof(*ipcp)); - - log_debug("ipcp_layer_init\n"); - - ipcp->ppp=ppp; - ipcp->fsm.ppp=ppp; - - ipcp->hnd.proto=PPP_IPCP; - ipcp->hnd.recv=ipcp_recv; - - ppp_register_unit_handler(ppp,&ipcp->hnd); - - ppp_fsm_init(&ipcp->fsm); - - ipcp->fsm.layer_up=ipcp_layer_up; - ipcp->fsm.layer_finished=ipcp_layer_down; - ipcp->fsm.send_conf_req=send_conf_req; - ipcp->fsm.send_conf_ack=send_conf_ack; - ipcp->fsm.send_conf_nak=send_conf_nak; - ipcp->fsm.send_conf_rej=send_conf_rej; - - INIT_LIST_HEAD(&ipcp->ropt_list); - - return &ipcp->ld; -} - -void ipcp_layer_start(struct ppp_layer_data_t *ld) -{ - struct ppp_ipcp_t *ipcp=container_of(ld,typeof(*ipcp),ld); - - log_debug("ipcp_layer_start\n"); - - ipcp_options_init(ipcp); - ppp_fsm_lower_up(&ipcp->fsm); - ppp_fsm_open(&ipcp->fsm); -} - -void ipcp_layer_finish(struct ppp_layer_data_t *ld) -{ - struct ppp_ipcp_t *ipcp=container_of(ld,typeof(*ipcp),ld); - - log_debug("ipcp_layer_finish\n"); - - ppp_fsm_lower_down(&ipcp->fsm); - - ppp_unregister_handler(ipcp->ppp,&ipcp->hnd); - ipcp_options_free(ipcp); - - ppp_layer_finished(ipcp->ppp,ld); -} - -void ipcp_layer_free(struct ppp_layer_data_t *ld) -{ - struct ppp_ipcp_t *ipcp=container_of(ld,typeof(*ipcp),ld); - - log_debug("ipcp_layer_free\n"); - - free(ipcp); -} - -static void ipcp_layer_up(struct ppp_fsm_t *fsm) -{ - struct ppp_ipcp_t *ipcp=container_of(fsm,typeof(*ipcp),fsm); - log_debug("ipcp_layer_started\n"); - ppp_layer_started(ipcp->ppp,&ipcp->ld); -} - -static void ipcp_layer_down(struct ppp_fsm_t *fsm) -{ - struct ppp_ipcp_t *ipcp=container_of(fsm,typeof(*ipcp),fsm); - log_debug("ipcp_layer_finished\n"); - ppp_layer_finished(ipcp->ppp,&ipcp->ld); -} - -static void print_ropt(struct recv_opt_t *ropt) -{ - int i; - uint8_t *ptr=(uint8_t*)ropt->hdr; - - log_debug(" <"); - for(i=0; ilen; i++) - { - log_debug(" %x",ptr[i]); - } - log_debug(" >"); -} - -static void send_conf_req(struct ppp_fsm_t *fsm) -{ - struct ppp_ipcp_t *ipcp=container_of(fsm,typeof(*ipcp),fsm); - uint8_t *buf=malloc(ipcp->conf_req_len), *ptr=buf; - struct ipcp_hdr_t *ipcp_hdr=(struct ipcp_hdr_t*)ptr; - struct ipcp_option_t *lopt; - int n; - - log_debug("send [IPCP ConfReq"); - ipcp_hdr->proto=htons(PPP_IPCP); - ipcp_hdr->code=CONFREQ; - ipcp_hdr->id=++ipcp->fsm.id; - ipcp_hdr->len=0; - log_debug(" id=%x",ipcp_hdr->id); - - ptr+=sizeof(*ipcp_hdr); - - list_for_each_entry(lopt,&ipcp->options,entry) - { - n=lopt->h->send_conf_req(ipcp,lopt,ptr); - if (n) - { - log_debug(" "); - lopt->h->print(log_debug,lopt,NULL); - ptr+=n; - } - } - - log_debug("]\n"); - - ipcp_hdr->len=htons((ptr-buf)-2); - ppp_unit_send(ipcp->ppp,ipcp_hdr,ptr-buf); -} - -static void send_conf_ack(struct ppp_fsm_t *fsm) -{ - struct ppp_ipcp_t *ipcp=container_of(fsm,typeof(*ipcp),fsm); - struct ipcp_hdr_t *hdr=(struct ipcp_hdr_t*)ipcp->ppp->unit_buf; - - hdr->code=CONFACK; - log_debug("send [IPCP ConfAck id=%x ]\n",ipcp->fsm.recv_id); - - ppp_unit_send(ipcp->ppp,hdr,ntohs(hdr->len)+2); -} - -static void send_conf_nak(struct ppp_fsm_t *fsm) -{ - struct ppp_ipcp_t *ipcp=container_of(fsm,typeof(*ipcp),fsm); - uint8_t *buf=malloc(ipcp->conf_req_len), *ptr=buf; - struct ipcp_hdr_t *ipcp_hdr=(struct ipcp_hdr_t*)ptr; - struct recv_opt_t *ropt; - - log_debug("send [IPCP ConfNak id=%x",ipcp->fsm.recv_id); - - ipcp_hdr->proto=htons(PPP_IPCP); - ipcp_hdr->code=CONFNAK; - ipcp_hdr->id=ipcp->fsm.recv_id; - ipcp_hdr->len=0; - - ptr+=sizeof(*ipcp_hdr); - - list_for_each_entry(ropt,&ipcp->ropt_list,entry) - { - if (ropt->state==IPCP_OPT_NAK) - { - log_debug(" "); - ropt->lopt->h->print(log_debug,ropt->lopt,NULL); - ptr+=ropt->lopt->h->send_conf_nak(ipcp,ropt->lopt,ptr); - } - } - - log_debug("]\n"); - - ipcp_hdr->len=htons((ptr-buf)-2); - ppp_unit_send(ipcp->ppp,ipcp_hdr,ptr-buf); -} - -static void send_conf_rej(struct ppp_fsm_t *fsm) -{ - struct ppp_ipcp_t *ipcp=container_of(fsm,typeof(*ipcp),fsm); - uint8_t *buf=malloc(ipcp->ropt_len), *ptr=buf; - struct ipcp_hdr_t *ipcp_hdr=(struct ipcp_hdr_t*)ptr; - struct recv_opt_t *ropt; - - log_debug("send [IPCP ConfRej id=%x ",ipcp->fsm.recv_id); - - ipcp_hdr->proto=htons(PPP_IPCP); - ipcp_hdr->code=CONFREJ; - ipcp_hdr->id=ipcp->fsm.recv_id; - ipcp_hdr->len=0; - - ptr+=sizeof(*ipcp_hdr); - - list_for_each_entry(ropt,&ipcp->ropt_list,entry) - { - if (ropt->state==IPCP_OPT_REJ) - { - log_debug(" "); - if (ropt->lopt) ropt->lopt->h->print(log_debug,ropt->lopt,(uint8_t*)ropt->hdr); - else print_ropt(ropt); - memcpy(ptr,ropt->hdr,ropt->len); - ptr+=ropt->len; - } - } - - log_debug("]\n"); - - ipcp_hdr->len=htons((ptr-buf)-2); - ppp_unit_send(ipcp->ppp,ipcp_hdr,ptr-buf); -} - -static int ipcp_recv_conf_req(struct ppp_ipcp_t *ipcp,uint8_t *data,int size) -{ - struct ipcp_opt_hdr_t *hdr; - struct recv_opt_t *ropt; - struct ipcp_option_t *lopt; - int r,ret=1; - - ipcp->ropt_len=size; - - while(size>0) - { - hdr=(struct ipcp_opt_hdr_t *)data; - - ropt=malloc(sizeof(*ropt)); - memset(ropt,0,sizeof(*ropt)); - if (hdr->len>size) ropt->len=size; - else ropt->len=hdr->len; - ropt->hdr=hdr; - ropt->state=IPCP_OPT_NONE; - list_add_tail(&ropt->entry,&ipcp->ropt_list); - - data+=ropt->len; - size-=ropt->len; - } - - list_for_each_entry(lopt,&ipcp->options,entry) - lopt->state=IPCP_OPT_NONE; - - log_debug("recv [IPCP ConfReq id=%x",ipcp->fsm.recv_id); - list_for_each_entry(ropt,&ipcp->ropt_list,entry) - { - list_for_each_entry(lopt,&ipcp->options,entry) - { - if (lopt->id==ropt->hdr->id) - { - log_debug(" "); - lopt->h->print(log_debug,lopt,(uint8_t*)ropt->hdr); - r=lopt->h->recv_conf_req(ipcp,lopt,(uint8_t*)ropt->hdr); - lopt->state=r; - ropt->state=r; - ropt->lopt=lopt; - if (rlopt) - { - log_debug(" "); - print_ropt(ropt); - ropt->state=IPCP_OPT_REJ; - ret=IPCP_OPT_REJ; - } - } - log_debug("]\n"); - - /*list_for_each_entry(lopt,&ipcp->options,entry) - { - if (lopt->state==IPCP_OPT_NONE) - { - r=lopt->h->recv_conf_req(ipcp,lopt,NULL); - lopt->state=r; - if (rropt_list)) - { - ropt=list_entry(ipcp->ropt_list.next,typeof(*ropt),entry); - list_del(&ropt->entry); - free(ropt); - } -} - -static int ipcp_recv_conf_rej(struct ppp_ipcp_t *ipcp,uint8_t *data,int size) -{ - struct ipcp_opt_hdr_t *hdr; - struct ipcp_option_t *lopt; - int res=0; - - log_debug("recv [IPCP ConfRej id=%x",ipcp->fsm.recv_id); - - if (ipcp->fsm.recv_id!=ipcp->fsm.id) - { - log_debug(": id mismatch ]\n"); - return 0; - } - - while(size>0) - { - hdr=(struct ipcp_opt_hdr_t *)data; - - list_for_each_entry(lopt,&ipcp->options,entry) - { - if (lopt->id==hdr->id) - { - if (!lopt->h->recv_conf_rej) - res=-1; - else if (lopt->h->recv_conf_rej(ipcp,lopt,data)) - res=-1; - break; - } - } - - data+=hdr->len; - size-=hdr->len; - } - log_debug("]\n"); - return res; -} - -static int ipcp_recv_conf_nak(struct ppp_ipcp_t *ipcp,uint8_t *data,int size) -{ - struct ipcp_opt_hdr_t *hdr; - struct ipcp_option_t *lopt; - int res=0; - - log_debug("recv [IPCP ConfNak id=%x",ipcp->fsm.recv_id); - - if (ipcp->fsm.recv_id!=ipcp->fsm.id) - { - log_debug(": id mismatch ]\n"); - return 0; - } - - while(size>0) - { - hdr=(struct ipcp_opt_hdr_t *)data; - - list_for_each_entry(lopt,&ipcp->options,entry) - { - if (lopt->id==hdr->id) - { - log_debug(" "); - lopt->h->print(log_debug,lopt,data); - if (lopt->h->recv_conf_nak(ipcp,lopt,data)) - res=-1; - break; - } - } - - data+=hdr->len; - size-=hdr->len; - } - log_debug("]\n"); - return res; -} - -static int ipcp_recv_conf_ack(struct ppp_ipcp_t *ipcp,uint8_t *data,int size) -{ - struct ipcp_opt_hdr_t *hdr; - struct ipcp_option_t *lopt; - int res=0; - - log_debug("recv [IPCP ConfAck id=%x",ipcp->fsm.recv_id); - - if (ipcp->fsm.recv_id!=ipcp->fsm.id) - { - log_debug(": id mismatch ]\n"); - return 0; - } - - while(size>0) - { - hdr=(struct ipcp_opt_hdr_t *)data; - - list_for_each_entry(lopt,&ipcp->options,entry) - { - if (lopt->id==hdr->id) - { - log_debug(" "); - lopt->h->print(log_debug,lopt,data); - if (!lopt->h->recv_conf_ack) - break; - if (lopt->h->recv_conf_ack(ipcp,lopt,data)) - res=-1; - break; - } - } - - data+=hdr->len; - size-=hdr->len; - } - log_debug("]\n"); - return res; -} - -static void ipcp_recv(struct ppp_handler_t*h) -{ - struct ipcp_hdr_t *hdr; - struct ppp_ipcp_t *ipcp=container_of(h,typeof(*ipcp),hnd); - int r; - char *term_msg; - - if (ipcp->fsm.fsm_state==FSM_Initial || ipcp->fsm.fsm_state==FSM_Closed) - { - log_error("IPCP: discaring packet\n"); - return; - } - - if (ipcp->ppp->unit_buf_sizeppp->unit_buf; - if (ntohs(hdr->len)fsm.recv_id=hdr->id; - switch(hdr->code) - { - case CONFREQ: - r=ipcp_recv_conf_req(ipcp,(uint8_t*)(hdr+1),ntohs(hdr->len)-PPP_HDRLEN); - switch(r) - { - case IPCP_OPT_ACK: - ppp_fsm_recv_conf_req_ack(&ipcp->fsm); - break; - case IPCP_OPT_NAK: - ppp_fsm_recv_conf_req_nak(&ipcp->fsm); - break; - case IPCP_OPT_REJ: - ppp_fsm_recv_conf_req_rej(&ipcp->fsm); - break; - } - ipcp_free_conf_req(ipcp); - if (r==IPCP_OPT_FAIL) - ppp_terminate(ipcp->ppp, 0); - break; - case CONFACK: - if (ipcp_recv_conf_ack(ipcp,(uint8_t*)(hdr+1),ntohs(hdr->len)-PPP_HDRLEN)) - ppp_terminate(ipcp->ppp, 0); - else - ppp_fsm_recv_conf_ack(&ipcp->fsm); - break; - case CONFNAK: - ipcp_recv_conf_nak(ipcp,(uint8_t*)(hdr+1),ntohs(hdr->len)-PPP_HDRLEN); - ppp_fsm_recv_conf_rej(&ipcp->fsm); - break; - case CONFREJ: - if (ipcp_recv_conf_rej(ipcp,(uint8_t*)(hdr+1),ntohs(hdr->len)-PPP_HDRLEN)) - ppp_terminate(ipcp->ppp, 0); - else - ppp_fsm_recv_conf_rej(&ipcp->fsm); - break; - case TERMREQ: - term_msg=strndup((char*)(hdr+1),ntohs(hdr->len)); - log_debug("recv [IPCP TermReq id=%x \"%s\"]\n",hdr->id,term_msg); - free(term_msg); - ppp_fsm_recv_term_req(&ipcp->fsm); - ppp_terminate(ipcp->ppp, 0); - break; - case TERMACK: - term_msg=strndup((char*)(hdr+1),ntohs(hdr->len)); - log_debug("recv [IPCP TermAck id=%x \"%s\"]\n",hdr->id,term_msg); - free(term_msg); - ppp_fsm_recv_term_ack(&ipcp->fsm); - break; - case CODEREJ: - log_debug("recv [IPCP CodeRej id=%x]\n",hdr->id); - ppp_fsm_recv_code_rej_bad(&ipcp->fsm); - break; - default: - ppp_fsm_recv_unk(&ipcp->fsm); - break; - } -} - -int ipcp_option_register(struct ipcp_option_handler_t *h) -{ - /*struct ipcp_option_drv_t *p; - - list_for_each_entry(p,option_drv_list,entry) - if (p->id==h->id) - return -1;*/ - - list_add_tail(&h->entry,&option_handlers); - - return 0; -} - -static struct ppp_layer_t ipcp_layer= -{ - .init=ipcp_layer_init, - .start=ipcp_layer_start, - .finish=ipcp_layer_finish, - .free=ipcp_layer_free, -}; - -static void __init ipcp_init(void) -{ - ppp_register_layer("ipcp",&ipcp_layer); -} diff --git a/accel-pptpd/ppp_ipcp.h b/accel-pptpd/ppp_ipcp.h deleted file mode 100644 index a2ce3b3..0000000 --- a/accel-pptpd/ppp_ipcp.h +++ /dev/null @@ -1,94 +0,0 @@ -#ifndef PPP_IPCP_H -#define PPP_IPCP_H - -#include - -#include "triton/triton.h" -#include "ppp_fsm.h" -/* - * Options. - */ -#define CI_COMP 2 /* IP-Compress-Protocol */ -#define CI_ADDR 3 /* IP-Address */ -#define CI_DNS1 129 /* Primary-DNS-Address */ -#define CI_DNS2 131 /* Secondary-DNS-Address */ - -struct ipcp_hdr_t -{ - uint16_t proto; - uint8_t code; - uint8_t id; - uint16_t len; -} __attribute__((packed)); -struct ipcp_opt_hdr_t -{ - uint8_t id; - uint8_t len; -} __attribute__((packed)); -struct ipcp_opt8_t -{ - struct ipcp_opt_hdr_t hdr; - uint8_t val; -} __attribute__((packed)); -struct ipcp_opt16_t -{ - struct ipcp_opt_hdr_t hdr; - uint16_t val; -} __attribute__((packed)); -struct ipcp_opt32_t -{ - struct ipcp_opt_hdr_t hdr; - uint32_t val; -} __attribute__((packed)); - -#define IPCP_OPT_NONE 0 -#define IPCP_OPT_ACK 1 -#define IPCP_OPT_NAK -1 -#define IPCP_OPT_REJ -2 -#define IPCP_OPT_FAIL -3 - -struct ppp_ipcp_t; -struct ipcp_option_handler_t; - -struct ipcp_option_t -{ - struct list_head entry; - int id; - int len; - int state; - struct ipcp_option_handler_t *h; -}; - -struct ipcp_option_handler_t -{ - struct list_head entry; - struct ipcp_option_t* (*init)(struct ppp_ipcp_t*); - int (*send_conf_req)(struct ppp_ipcp_t*,struct ipcp_option_t*,uint8_t*); - int (*send_conf_rej)(struct ppp_ipcp_t*,struct ipcp_option_t*,uint8_t*); - int (*send_conf_nak)(struct ppp_ipcp_t*,struct ipcp_option_t*,uint8_t*); - int (*recv_conf_req)(struct ppp_ipcp_t*,struct ipcp_option_t*,uint8_t*); - int (*recv_conf_rej)(struct ppp_ipcp_t*,struct ipcp_option_t*,uint8_t*); - int (*recv_conf_nak)(struct ppp_ipcp_t*,struct ipcp_option_t*,uint8_t*); - int (*recv_conf_ack)(struct ppp_ipcp_t*,struct ipcp_option_t*,uint8_t*); - void (*free)(struct ppp_ipcp_t*,struct ipcp_option_t*); - void (*print)(void (*print)(const char *fmt,...), struct ipcp_option_t*,uint8_t*); -}; - -struct ppp_ipcp_t -{ - struct ppp_layer_data_t ld; - struct ppp_handler_t hnd; - struct ppp_fsm_t fsm; - struct ppp_t *ppp; - struct list_head options; - - struct list_head ropt_list; // last received ConfReq - int ropt_len; - - int conf_req_len; -}; - -int ipcp_option_register(struct ipcp_option_handler_t *h); - -#endif - diff --git a/accel-pptpd/ppp_lcp.c b/accel-pptpd/ppp_lcp.c deleted file mode 100644 index 6c5fd18..0000000 --- a/accel-pptpd/ppp_lcp.c +++ /dev/null @@ -1,658 +0,0 @@ -#include -#include -#include -#include -#include - -#include "triton/triton.h" - -#include "log.h" - -#include "ppp.h" -#include "ppp_lcp.h" - -struct recv_opt_t -{ - struct list_head entry; - struct lcp_opt_hdr_t *hdr; - int len; - int state; - struct lcp_option_t *lopt; -}; - -static int conf_echo_interval = 0; -static int conf_echo_failure = 3; - -static LIST_HEAD(option_handlers); - -static void lcp_layer_up(struct ppp_fsm_t*); -static void lcp_layer_down(struct ppp_fsm_t*); -static void send_conf_req(struct ppp_fsm_t*); -static void send_conf_ack(struct ppp_fsm_t*); -static void send_conf_nak(struct ppp_fsm_t*); -static void send_conf_rej(struct ppp_fsm_t*); -static void lcp_recv(struct ppp_handler_t*); -static void start_echo(struct ppp_lcp_t *lcp); -static void stop_echo(struct ppp_lcp_t *lcp); - -static void lcp_options_init(struct ppp_lcp_t *lcp) -{ - struct lcp_option_t *lopt; - struct lcp_option_handler_t *h; - - INIT_LIST_HEAD(&lcp->options); - - list_for_each_entry(h,&option_handlers,entry) - { - lopt=h->init(lcp); - if (lopt) - { - lopt->h=h; - list_add_tail(&lopt->entry,&lcp->options); - lcp->conf_req_len+=lopt->len; - } - } -} - -static void lcp_options_free(struct ppp_lcp_t *lcp) -{ - struct lcp_option_t *lopt; - - while(!list_empty(&lcp->options)) - { - lopt=list_entry(lcp->options.next,typeof(*lopt),entry); - list_del(&lopt->entry); - lopt->h->free(lcp,lopt); - } -} - -static struct ppp_layer_data_t *lcp_layer_init(struct ppp_t *ppp) -{ - struct ppp_lcp_t *lcp=malloc(sizeof(*lcp)); - memset(lcp,0,sizeof(*lcp)); - - log_debug("lcp_layer_init\n"); - - lcp->ppp=ppp; - lcp->fsm.ppp=ppp; - - lcp->hnd.proto=PPP_LCP; - lcp->hnd.recv=lcp_recv; - - ppp_register_chan_handler(ppp,&lcp->hnd); - - ppp_fsm_init(&lcp->fsm); - - lcp->fsm.layer_up=lcp_layer_up; - lcp->fsm.layer_finished=lcp_layer_down; - lcp->fsm.send_conf_req=send_conf_req; - lcp->fsm.send_conf_ack=send_conf_ack; - lcp->fsm.send_conf_nak=send_conf_nak; - lcp->fsm.send_conf_rej=send_conf_rej; - - INIT_LIST_HEAD(&lcp->ropt_list); - - return &lcp->ld; -} - -void lcp_layer_start(struct ppp_layer_data_t *ld) -{ - struct ppp_lcp_t *lcp=container_of(ld,typeof(*lcp),ld); - - log_debug("lcp_layer_start\n"); - - lcp_options_init(lcp); - ppp_fsm_lower_up(&lcp->fsm); - ppp_fsm_open(&lcp->fsm); -} - -void lcp_layer_finish(struct ppp_layer_data_t *ld) -{ - struct ppp_lcp_t *lcp=container_of(ld,typeof(*lcp),ld); - - log_debug("lcp_layer_finish\n"); - - stop_echo(lcp); - - ppp_fsm_close(&lcp->fsm); -} - -void lcp_layer_free(struct ppp_layer_data_t *ld) -{ - struct ppp_lcp_t *lcp=container_of(ld,typeof(*lcp),ld); - - log_debug("lcp_layer_free\n"); - - stop_echo(lcp); - ppp_unregister_handler(lcp->ppp,&lcp->hnd); - lcp_options_free(lcp); - ppp_fsm_free(&lcp->fsm); - - free(lcp); -} - -static void lcp_layer_up(struct ppp_fsm_t *fsm) -{ - struct ppp_lcp_t *lcp=container_of(fsm,typeof(*lcp),fsm); - log_debug("lcp_layer_started\n"); - ppp_layer_started(lcp->ppp,&lcp->ld); - - start_echo(lcp); -} - -static void lcp_layer_down(struct ppp_fsm_t *fsm) -{ - struct ppp_lcp_t *lcp=container_of(fsm,typeof(*lcp),fsm); - log_debug("lcp_layer_finished\n"); - stop_echo(lcp); - ppp_layer_finished(lcp->ppp,&lcp->ld); -} - -static void print_ropt(struct recv_opt_t *ropt) -{ - int i; - uint8_t *ptr=(uint8_t*)ropt->hdr; - - log_debug(" <"); - for(i=0; ilen; i++) - { - log_debug(" %x",ptr[i]); - } - log_debug(" >"); -} - -static void send_conf_req(struct ppp_fsm_t *fsm) -{ - struct ppp_lcp_t *lcp=container_of(fsm,typeof(*lcp),fsm); - uint8_t *buf=malloc(lcp->conf_req_len), *ptr=buf; - struct lcp_hdr_t *lcp_hdr=(struct lcp_hdr_t*)ptr; - struct lcp_option_t *lopt; - int n; - - log_debug("send [LCP ConfReq"); - lcp_hdr->proto=htons(PPP_LCP); - lcp_hdr->code=CONFREQ; - lcp_hdr->id=++lcp->fsm.id; - lcp_hdr->len=0; - log_debug(" id=%x",lcp_hdr->id); - - ptr+=sizeof(*lcp_hdr); - - list_for_each_entry(lopt,&lcp->options,entry) - { - n=lopt->h->send_conf_req(lcp,lopt,ptr); - if (n) - { - log_debug(" "); - lopt->h->print(log_debug,lopt,NULL); - ptr+=n; - } - } - - log_debug("]\n"); - - lcp_hdr->len=htons((ptr-buf)-2); - ppp_chan_send(lcp->ppp,lcp_hdr,ptr-buf); -} - -static void send_conf_ack(struct ppp_fsm_t *fsm) -{ - struct ppp_lcp_t *lcp=container_of(fsm,typeof(*lcp),fsm); - struct lcp_hdr_t *hdr=(struct lcp_hdr_t*)lcp->ppp->chan_buf; - - hdr->code=CONFACK; - log_debug("send [LCP ConfAck id=%x ]\n",lcp->fsm.recv_id); - - ppp_chan_send(lcp->ppp,hdr,ntohs(hdr->len)+2); -} - -static void send_conf_nak(struct ppp_fsm_t *fsm) -{ - struct ppp_lcp_t *lcp=container_of(fsm,typeof(*lcp),fsm); - uint8_t *buf=malloc(lcp->conf_req_len), *ptr=buf; - struct lcp_hdr_t *lcp_hdr=(struct lcp_hdr_t*)ptr; - struct recv_opt_t *ropt; - - log_debug("send [LCP ConfNak id=%x",lcp->fsm.recv_id); - - lcp_hdr->proto=htons(PPP_LCP); - lcp_hdr->code=CONFNAK; - lcp_hdr->id=lcp->fsm.recv_id; - lcp_hdr->len=0; - - ptr+=sizeof(*lcp_hdr); - - list_for_each_entry(ropt,&lcp->ropt_list,entry) - { - if (ropt->state==LCP_OPT_NAK) - { - log_debug(" "); - ropt->lopt->h->print(log_debug,ropt->lopt,NULL); - ptr+=ropt->lopt->h->send_conf_nak(lcp,ropt->lopt,ptr); - } - } - - log_debug("]\n"); - - lcp_hdr->len=htons((ptr-buf)-2); - ppp_chan_send(lcp->ppp,lcp_hdr,ptr-buf); -} - -static void send_conf_rej(struct ppp_fsm_t *fsm) -{ - struct ppp_lcp_t *lcp=container_of(fsm,typeof(*lcp),fsm); - uint8_t *buf=malloc(lcp->ropt_len), *ptr=buf; - struct lcp_hdr_t *lcp_hdr=(struct lcp_hdr_t*)ptr; - struct recv_opt_t *ropt; - - log_debug("send [LCP ConfRej id=%x ",lcp->fsm.recv_id); - - lcp_hdr->proto=htons(PPP_LCP); - lcp_hdr->code=CONFREJ; - lcp_hdr->id=lcp->fsm.recv_id; - lcp_hdr->len=0; - - ptr+=sizeof(*lcp_hdr); - - list_for_each_entry(ropt,&lcp->ropt_list,entry) - { - if (ropt->state==LCP_OPT_REJ) - { - log_debug(" "); - if (ropt->lopt) ropt->lopt->h->print(log_debug,ropt->lopt,(uint8_t*)ropt->hdr); - else print_ropt(ropt); - memcpy(ptr,ropt->hdr,ropt->len); - ptr+=ropt->len; - } - } - - log_debug("]\n"); - - lcp_hdr->len=htons((ptr-buf)-2); - ppp_chan_send(lcp->ppp,lcp_hdr,ptr-buf); -} - -static int lcp_recv_conf_req(struct ppp_lcp_t *lcp,uint8_t *data,int size) -{ - struct lcp_opt_hdr_t *hdr; - struct recv_opt_t *ropt; - struct lcp_option_t *lopt; - int r,ret=1; - - lcp->ropt_len=size; - - while(size>0) - { - hdr=(struct lcp_opt_hdr_t *)data; - - ropt=malloc(sizeof(*ropt)); - if (hdr->len>size) ropt->len=size; - else ropt->len=hdr->len; - ropt->hdr=hdr; - ropt->state=LCP_OPT_NONE; - list_add_tail(&ropt->entry,&lcp->ropt_list); - - data+=ropt->len; - size-=ropt->len; - } - - list_for_each_entry(lopt,&lcp->options,entry) - lopt->state=LCP_OPT_NONE; - - log_debug("recv [LCP ConfReq id=%x",lcp->fsm.recv_id); - list_for_each_entry(ropt,&lcp->ropt_list,entry) - { - list_for_each_entry(lopt,&lcp->options,entry) - { - if (lopt->id==ropt->hdr->id) - { - log_debug(" "); - lopt->h->print(log_debug,lopt,(uint8_t*)ropt->hdr); - r=lopt->h->recv_conf_req(lcp,lopt,(uint8_t*)ropt->hdr); - lopt->state=r; - ropt->state=r; - ropt->lopt=lopt; - if (rlopt) - { - log_debug(" "); - print_ropt(ropt); - ropt->state=LCP_OPT_REJ; - ret=LCP_OPT_REJ; - } - } - log_debug("]\n"); - - /*list_for_each_entry(lopt,&lcp->options,entry) - { - if (lopt->state==LCP_OPT_NONE) - { - r=lopt->h->recv_conf_req(lcp,lopt,NULL); - lopt->state=r; - if (rropt_list)) - { - ropt=list_entry(lcp->ropt_list.next,typeof(*ropt),entry); - list_del(&ropt->entry); - free(ropt); - } -} - -static int lcp_recv_conf_rej(struct ppp_lcp_t *lcp,uint8_t *data,int size) -{ - struct lcp_opt_hdr_t *hdr; - struct lcp_option_t *lopt; - int res=0; - - log_debug("recv [LCP ConfRej id=%x",lcp->fsm.recv_id); - - if (lcp->fsm.recv_id!=lcp->fsm.id) - { - log_debug(": id mismatch ]\n"); - return 0; - } - - while(size>0) - { - hdr=(struct lcp_opt_hdr_t *)data; - - list_for_each_entry(lopt,&lcp->options,entry) - { - if (lopt->id==hdr->id) - { - if (!lopt->h->recv_conf_rej) - res=-1; - else if (lopt->h->recv_conf_rej(lcp,lopt,data)) - res=-1; - break; - } - } - - data+=hdr->len; - size-=hdr->len; - } - log_debug("]\n"); - return res; -} - -static int lcp_recv_conf_nak(struct ppp_lcp_t *lcp,uint8_t *data,int size) -{ - struct lcp_opt_hdr_t *hdr; - struct lcp_option_t *lopt; - int res=0; - - log_debug("recv [LCP ConfNak id=%x",lcp->fsm.recv_id); - - if (lcp->fsm.recv_id!=lcp->fsm.id) - { - log_debug(": id mismatch ]\n"); - return 0; - } - - while(size>0) - { - hdr=(struct lcp_opt_hdr_t *)data; - - list_for_each_entry(lopt,&lcp->options,entry) - { - if (lopt->id==hdr->id) - { - log_debug(" "); - lopt->h->print(log_debug,lopt,data); - if (lopt->h->recv_conf_nak(lcp,lopt,data)) - res=-1; - break; - } - } - - data+=hdr->len; - size-=hdr->len; - } - log_debug("]\n"); - return res; -} - -static int lcp_recv_conf_ack(struct ppp_lcp_t *lcp,uint8_t *data,int size) -{ - struct lcp_opt_hdr_t *hdr; - struct lcp_option_t *lopt; - int res=0; - - log_debug("recv [LCP ConfAck id=%x",lcp->fsm.recv_id); - - if (lcp->fsm.recv_id!=lcp->fsm.id) - { - log_debug(": id mismatch ]\n"); - return 0; - } - - while(size>0) - { - hdr=(struct lcp_opt_hdr_t *)data; - - list_for_each_entry(lopt,&lcp->options,entry) - { - if (lopt->id==hdr->id) - { - log_debug(" "); - lopt->h->print(log_debug,lopt,data); - if (!lopt->h->recv_conf_ack) - break; - if (lopt->h->recv_conf_ack(lcp,lopt,data)) - res=-1; - break; - } - } - - data+=hdr->len; - size-=hdr->len; - } - log_debug("]\n"); - return res; -} - -static void lcp_recv_echo_repl(struct ppp_lcp_t *lcp,uint8_t *data,int size) -{ - uint32_t magic = *(uint32_t *)data; - - if (size != 4) { - log_error("lcp:echo: magic number size mismatch\n"); - ppp_terminate(lcp->ppp, 0); - } - - log_debug("recv [LCP EchoRep id=%x ]\n",lcp->fsm.recv_id,magic); - - if (magic == lcp->magic) { - log_error("lcp:echo: loop-back detected\n"); - ppp_terminate(lcp->ppp, 0); - } - - lcp->echo_sent = 0; -} - -static void send_echo_reply(struct ppp_lcp_t *lcp) -{ - struct lcp_hdr_t *hdr=(struct lcp_hdr_t*)lcp->ppp->chan_buf; - uint32_t magic = *(uint32_t *)(hdr+1); - - hdr->code=ECHOREP; - log_debug("send [LCP EchoRep id=%x ]\n", hdr->id, magic); - - ppp_chan_send(lcp->ppp,hdr,ntohs(hdr->len)+2); -} -static void send_echo_request(struct triton_timer_t *t) -{ - struct ppp_lcp_t *lcp = container_of(t, typeof(*lcp), echo_timer); - struct lcp_echo_req_t - { - struct lcp_hdr_t hdr; - uint32_t magic; - } __attribute__((packed)) msg = { - .hdr.proto = htons(PPP_LCP), - .hdr.code = ECHOREQ, - .hdr.id = ++lcp->fsm.id, - .hdr.len = htons(8), - .magic = lcp->magic, - }; - - if (++lcp->echo_sent > lcp->echo_failure) { - log_warn("lcp: no echo reply\n"); - ppp_terminate(lcp->ppp, 0); - } else { - log_debug("send [LCP EchoReq id=%x ]\n", msg.hdr.id, msg.magic); - ppp_chan_send(lcp->ppp,&msg,ntohs(msg.hdr.len)+2); - } -} - -static void start_echo(struct ppp_lcp_t *lcp) -{ - lcp->echo_interval = conf_echo_interval; - lcp->echo_failure = conf_echo_failure; - - lcp->echo_timer.period = lcp->echo_interval * 1000; - lcp->echo_timer.expire = send_echo_request; - triton_timer_add(lcp->ppp->ctrl->ctx, &lcp->echo_timer, 0); -} -static void stop_echo(struct ppp_lcp_t *lcp) -{ - if (lcp->echo_interval) { - triton_timer_del(&lcp->echo_timer); - lcp->echo_interval = 0; - } -} - -static void lcp_recv(struct ppp_handler_t*h) -{ - struct lcp_hdr_t *hdr; - struct ppp_lcp_t *lcp=container_of(h,typeof(*lcp),hnd); - int r; - char *term_msg; - - if (lcp->ppp->chan_buf_sizeppp->chan_buf; - if (ntohs(hdr->len)fsm.recv_id=hdr->id; - switch(hdr->code) - { - case CONFREQ: - r=lcp_recv_conf_req(lcp,(uint8_t*)(hdr+1),ntohs(hdr->len)-PPP_HDRLEN); - switch(r) - { - case LCP_OPT_ACK: - ppp_fsm_recv_conf_req_ack(&lcp->fsm); - break; - case LCP_OPT_NAK: - ppp_fsm_recv_conf_req_nak(&lcp->fsm); - break; - case LCP_OPT_REJ: - ppp_fsm_recv_conf_req_rej(&lcp->fsm); - break; - } - lcp_free_conf_req(lcp); - if (r==LCP_OPT_FAIL) - ppp_terminate(lcp->ppp, 0); - break; - case CONFACK: - if (lcp_recv_conf_ack(lcp,(uint8_t*)(hdr+1),ntohs(hdr->len)-PPP_HDRLEN)) - ppp_terminate(lcp->ppp, 0); - else - ppp_fsm_recv_conf_ack(&lcp->fsm); - break; - case CONFNAK: - lcp_recv_conf_nak(lcp,(uint8_t*)(hdr+1),ntohs(hdr->len)-PPP_HDRLEN); - ppp_fsm_recv_conf_rej(&lcp->fsm); - break; - case CONFREJ: - if (lcp_recv_conf_rej(lcp,(uint8_t*)(hdr+1),ntohs(hdr->len)-PPP_HDRLEN)) - ppp_terminate(lcp->ppp, 0); - else - ppp_fsm_recv_conf_rej(&lcp->fsm); - break; - case TERMREQ: - term_msg=strndup((char*)(hdr+1),ntohs(hdr->len)); - log_debug("recv [LCP TermReq id=%x \"%s\"]\n",hdr->id,term_msg); - free(term_msg); - ppp_fsm_recv_term_req(&lcp->fsm); - ppp_terminate(lcp->ppp, 0); - break; - case TERMACK: - term_msg=strndup((char*)(hdr+1),ntohs(hdr->len)); - log_debug("recv [LCP TermAck id=%x \"%s\"]\n",hdr->id,term_msg); - free(term_msg); - ppp_fsm_recv_term_ack(&lcp->fsm); - break; - case CODEREJ: - log_debug("recv [LCP CodeRej id=%x]\n",hdr->id); - ppp_fsm_recv_code_rej_bad(&lcp->fsm); - break; - case ECHOREQ: - send_echo_reply(lcp); - break; - case ECHOREP: - lcp_recv_echo_repl(lcp,(uint8_t*)(hdr+1),ntohs(hdr->len)-PPP_HDRLEN); - break; - default: - ppp_fsm_recv_unk(&lcp->fsm); - break; - } -} - -int lcp_option_register(struct lcp_option_handler_t *h) -{ - /*struct lcp_option_drv_t *p; - - list_for_each_entry(p,option_drv_list,entry) - if (p->id==h->id) - return -1;*/ - - list_add_tail(&h->entry,&option_handlers); - - return 0; -} - -static struct ppp_layer_t lcp_layer= -{ - .init=lcp_layer_init, - .start=lcp_layer_start, - .finish=lcp_layer_finish, - .free=lcp_layer_free, -}; - -static void __init lcp_init(void) -{ - char *opt; - - ppp_register_layer("lcp",&lcp_layer); - - opt = conf_get_opt("lcp", "echo-interval"); - if (opt && atoi(opt) > 0) - conf_echo_interval = atoi(opt); - - opt = conf_get_opt("lcp", "echo-failure"); - if (opt && atoi(opt) > 0) - conf_echo_failure = atoi(opt); - -} diff --git a/accel-pptpd/ppp_lcp.h b/accel-pptpd/ppp_lcp.h deleted file mode 100644 index 7110db2..0000000 --- a/accel-pptpd/ppp_lcp.h +++ /dev/null @@ -1,134 +0,0 @@ -#ifndef PPP_LCP_H -#define PPP_LCP_H - -#include - -#include "triton/triton.h" -#include "ppp_fsm.h" -/* - * Options. - */ -#define CI_VENDOR 0 /* Vendor Specific */ -#define CI_MRU 1 /* Maximum Receive Unit */ -#define CI_ASYNCMAP 2 /* Async Control Character Map */ -#define CI_AUTH 3 /* Authentication Type */ -#define CI_QUALITY 4 /* Quality Protocol */ -#define CI_MAGIC 5 /* Magic Number */ -#define CI_PCOMP 7 /* Protocol Field Compression */ -#define CI_ACCOMP 8 /* Address/Control Field Compression */ -#define CI_FCSALTERN 9 /* FCS-Alternatives */ -#define CI_SDP 10 /* Self-Describing-Pad */ -#define CI_NUMBERED 11 /* Numbered-Mode */ -#define CI_CALLBACK 13 /* callback */ -#define CI_MRRU 17 /* max reconstructed receive unit; multilink */ -#define CI_SSNHF 18 /* short sequence numbers for multilink */ -#define CI_EPDISC 19 /* endpoint discriminator */ -#define CI_MPPLUS 22 /* Multi-Link-Plus-Procedure */ -#define CI_LDISC 23 /* Link-Discriminator */ -#define CI_LCPAUTH 24 /* LCP Authentication */ -#define CI_COBS 25 /* Consistent Overhead Byte Stuffing */ -#define CI_PREFELIS 26 /* Prefix Elision */ -#define CI_MPHDRFMT 27 /* MP Header Format */ -#define CI_I18N 28 /* Internationalization */ -#define CI_SDL 29 /* Simple Data Link */ - -struct lcp_hdr_t -{ - uint16_t proto; - uint8_t code; - uint8_t id; - uint16_t len; -} __attribute__((packed)); -struct lcp_opt_hdr_t -{ - uint8_t id; - uint8_t len; -} __attribute__((packed)); -struct lcp_opt8_t -{ - struct lcp_opt_hdr_t hdr; - uint8_t val; -} __attribute__((packed)); -struct lcp_opt16_t -{ - struct lcp_opt_hdr_t hdr; - uint16_t val; -} __attribute__((packed)); -struct lcp_opt32_t -{ - struct lcp_opt_hdr_t hdr; - uint32_t val; -} __attribute__((packed)); - -/*struct lcp_options_t -{ - int magic; - int mtu; - int mru; - int accomp; // 0 - disabled, 1 - enable, 2 - allow, disabled, 3 - allow,enabled - int pcomp; // 0 - disabled, 1 - enable, 2 - allow, disabled, 3 - allow,enabled - // negotiated options; - int neg_mru; - int neg_mtu; - int neg_accomp; // -1 - rejected - int neg_pcomp; - int neg_auth[AUTH_MAX]; -};*/ - -#define LCP_OPT_NONE 0 -#define LCP_OPT_ACK 1 -#define LCP_OPT_NAK -1 -#define LCP_OPT_REJ -2 -#define LCP_OPT_FAIL -3 - -struct ppp_lcp_t; -struct lcp_option_handler_t; - -struct lcp_option_t -{ - struct list_head entry; - int id; - int len; - int state; - struct lcp_option_handler_t *h; -}; - -struct lcp_option_handler_t -{ - struct list_head entry; - struct lcp_option_t* (*init)(struct ppp_lcp_t*); - int (*send_conf_req)(struct ppp_lcp_t*,struct lcp_option_t*,uint8_t*); - int (*send_conf_rej)(struct ppp_lcp_t*,struct lcp_option_t*,uint8_t*); - int (*send_conf_nak)(struct ppp_lcp_t*,struct lcp_option_t*,uint8_t*); - int (*recv_conf_req)(struct ppp_lcp_t*,struct lcp_option_t*,uint8_t*); - int (*recv_conf_rej)(struct ppp_lcp_t*,struct lcp_option_t*,uint8_t*); - int (*recv_conf_nak)(struct ppp_lcp_t*,struct lcp_option_t*,uint8_t*); - int (*recv_conf_ack)(struct ppp_lcp_t*,struct lcp_option_t*,uint8_t*); - void (*free)(struct ppp_lcp_t*,struct lcp_option_t*); - void (*print)(void (*print)(const char *fmt,...), struct lcp_option_t*,uint8_t*); -}; - -struct ppp_lcp_t -{ - struct ppp_layer_data_t ld; - struct ppp_handler_t hnd; - struct ppp_fsm_t fsm; - struct ppp_t *ppp; - struct list_head options; - - struct triton_timer_t echo_timer; - int echo_interval; - int echo_failure; - int echo_sent; - int magic; - - struct list_head ropt_list; // last received ConfReq - int ropt_len; - - int conf_req_len; -}; - -int lcp_option_register(struct lcp_option_handler_t *h); - -#endif - diff --git a/accel-pptpd/pptp.c b/accel-pptpd/pptp.c deleted file mode 100644 index 2d69a6e..0000000 --- a/accel-pptpd/pptp.c +++ /dev/null @@ -1,563 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "if_pppox.h" - -#include "list.h" -#include "pptp_prot.h" -#include "triton/triton.h" -#include "pptpd.h" -#include "log.h" -#include "ppp.h" - - -#define STATE_IDLE 0 -#define STATE_ESTB 1 -#define STATE_PPP 2 -#define STATE_FIN 3 -#define STATE_CLOSE 4 - -struct pptp_conn_t -{ - struct triton_ctx_t ctx; - struct triton_md_handler_t hnd; - struct triton_timer_t timeout_timer; - struct triton_timer_t echo_timer; - int state; - int echo_sent; - - uint8_t *in_buf; - int in_size; - uint8_t *out_buf; - int out_size; - int out_pos; - - struct ppp_ctrl_t ctrl; - struct ppp_t ppp; -}; - -static int conf_timeout = 3; -static int conf_echo_interval = 0; - -static int pptp_read(struct triton_md_handler_t *h); -static int pptp_write(struct triton_md_handler_t *h); -static void pptp_timeout(struct triton_timer_t *); -static void ppp_started(struct ppp_t *); -static void ppp_finished(struct ppp_t *); - -static void disconnect(struct pptp_conn_t *conn) -{ - triton_md_unregister_handler(&conn->hnd); - close(conn->hnd.fd); - - if (conn->timeout_timer.period) { - triton_timer_del(&conn->timeout_timer); - conn->timeout_timer.period = 0; - } - - if (conn->echo_timer.period) { - triton_timer_del(&conn->echo_timer); - conn->echo_timer.period = 0; - } - - if (conn->state == STATE_PPP) { - conn->state = STATE_CLOSE; - ppp_terminate(&conn->ppp, 1); - } - - triton_unregister_ctx(&conn->ctx); - - free(conn->in_buf); - free(conn->out_buf); - free(conn); -} - -static int post_msg(struct pptp_conn_t *conn, void *buf, int size) -{ - int n; - if (conn->out_size) { - log_debug("post_msg: buffer is not empty\n"); - return -1; - } - - n=write(conn->hnd.fd, buf, size); - if (n < 0) { - if (errno == EINTR || errno == EAGAIN) - n = 0; - else { - if (errno != EPIPE) - log_debug("post_msg: failed to write socket %i\n",errno); - return -1; - } - } - - if ( nout_buf, buf + n, size - n); - triton_md_enable_handler(&conn->hnd, MD_MODE_WRITE); - } - - return 0; -} - -static int send_pptp_stop_ctrl_conn_rqst(struct pptp_conn_t *conn, int reason, int err_code) -{ - struct pptp_stop_ctrl_conn msg = { - .header = PPTP_HEADER_CTRL(PPTP_STOP_CTRL_CONN_RQST), - .reason_result = hton8(reason), - .error_code = hton8(err_code), - }; - - return post_msg(conn, &msg, sizeof(msg)); -} - -static int send_pptp_stop_ctrl_conn_rply(struct pptp_conn_t *conn, int reason, int err_code) -{ - struct pptp_stop_ctrl_conn msg = { - .header = PPTP_HEADER_CTRL(PPTP_STOP_CTRL_CONN_RPLY), - .reason_result = hton8(reason), - .error_code = hton8(err_code), - }; - - return post_msg(conn, &msg, sizeof(msg)); -} -static int pptp_stop_ctrl_conn_rqst(struct pptp_conn_t *conn) -{ - struct pptp_stop_ctrl_conn *msg = (struct pptp_stop_ctrl_conn *)conn->in_buf; - log_info("PPTP_STOP_CTRL_CONN_RQST reason=%i error_code=%i\n",msg->reason_result, msg->error_code); - - if (conn->state == STATE_PPP) { - conn->state = STATE_FIN; - ppp_terminate(&conn->ppp, 0); - } - - send_pptp_stop_ctrl_conn_rply(conn, PPTP_CONN_STOP_OK, 0); - return -1; -} - -static int send_pptp_start_ctrl_conn_rply(struct pptp_conn_t *conn, int res_code, int err_code) -{ - struct pptp_start_ctrl_conn msg = { - .header = PPTP_HEADER_CTRL(PPTP_START_CTRL_CONN_RPLY), - .version = htons(PPTP_VERSION), - .result_code = res_code, - .error_code = err_code, - .framing_cap = htonl(PPTP_FRAME_SYNC), - .bearer_cap = htonl(0), - .max_channels = htons(1), - .firmware_rev = htons(PPTP_FIRMWARE_VERSION), - }; - - memset(msg.hostname, 0, sizeof(msg.hostname)); - strcpy((char*)msg.hostname, PPTP_HOSTNAME); - - memset(msg.vendor, 0, sizeof(msg.vendor)); - strcpy((char*)msg.vendor, PPTP_VENDOR); - - return post_msg(conn, &msg, sizeof(msg)); -} -static int pptp_start_ctrl_conn_rqst(struct pptp_conn_t *conn) -{ - struct pptp_start_ctrl_conn *msg = (struct pptp_start_ctrl_conn *)conn->in_buf; - - if (conn->state != STATE_IDLE) { - log_info("unexpected PPTP_START_CTRL_CONN_RQST\n"); - if (send_pptp_start_ctrl_conn_rply(conn, PPTP_CONN_RES_EXISTS, 0)) - return -1; - return 0; - } - - if (msg->version != htons(PPTP_VERSION)) { - log_info("PPTP version mismatch: expecting %x, received %s\n", PPTP_VERSION, msg->version); - if (send_pptp_start_ctrl_conn_rply(conn, PPTP_CONN_RES_PROTOCOL, 0)) - return -1; - return 0; - } - if (!(ntohl(msg->framing_cap) & PPTP_FRAME_SYNC)) { - log_info("connection does not supports sync mode\n"); - if (send_pptp_start_ctrl_conn_rply(conn, PPTP_CONN_RES_GE, 0)) - return -1; - return 0; - } - if (send_pptp_start_ctrl_conn_rply(conn, PPTP_CONN_RES_SUCCESS, 0)) - return -1; - - triton_timer_mod(&conn->timeout_timer, 0); - - conn->state = STATE_ESTB; - - return 0; -} - -static int send_pptp_out_call_rply(struct pptp_conn_t *conn, struct pptp_out_call_rqst *rqst, int call_id, int res_code, int err_code) -{ - struct pptp_out_call_rply msg = { - .header = PPTP_HEADER_CTRL(PPTP_OUT_CALL_RPLY), - .call_id = htons(call_id), - .call_id_peer = rqst->call_id, - .result_code = res_code, - .error_code = err_code, - .cause_code = 0, - .speed = rqst->bps_max, - .recv_size = rqst->recv_size, - .delay = 0, - .channel = 0, - }; - - return post_msg(conn, &msg, sizeof(msg)); -} - -static int pptp_out_call_rqst(struct pptp_conn_t *conn) -{ - struct pptp_out_call_rqst *msg = (struct pptp_out_call_rqst *)conn->in_buf; - struct sockaddr_pppox src_addr, dst_addr; - struct sockaddr_in addr; - socklen_t addrlen; - int pptp_sock; - - if (conn->state != STATE_ESTB) { - log_error("unexpected PPTP_OUT_CALL_RQST\n"); - if (send_pptp_out_call_rply(conn, msg, 0, PPTP_CALL_RES_GE, PPTP_GE_NOCONN)) - return -1; - return 0; - } - - src_addr.sa_family = AF_PPPOX; - src_addr.sa_protocol = PX_PROTO_PPTP; - src_addr.sa_addr.pptp.call_id = 0; - addrlen = sizeof(addr); - getsockname(conn->hnd.fd, (struct sockaddr*)&addr, &addrlen); - src_addr.sa_addr.pptp.sin_addr = addr.sin_addr; - - dst_addr.sa_family = AF_PPPOX; - dst_addr.sa_protocol = PX_PROTO_PPTP; - dst_addr.sa_addr.pptp.call_id = htons(msg->call_id); - addrlen = sizeof(addr); - getpeername(conn->hnd.fd, (struct sockaddr*)&addr, &addrlen); - dst_addr.sa_addr.pptp.sin_addr = addr.sin_addr; - - pptp_sock = socket(AF_PPPOX, SOCK_STREAM, PX_PROTO_PPTP); - if (pptp_sock < 0) { - log_error("failed to create PPTP socket (%s)\n", strerror(errno)); - return -1; - } - if (bind(pptp_sock, (struct sockaddr*)&src_addr, sizeof(src_addr))) { - log_error("failed to bind PPTP socket (%s)\n", strerror(errno)); - close(pptp_sock); - return -1; - } - addrlen = sizeof(src_addr); - getsockname(pptp_sock, (struct sockaddr*)&src_addr, &addrlen); - - if (connect(pptp_sock, (struct sockaddr*)&dst_addr, sizeof(dst_addr))) { - log_error("failed to connect PPTP socket (%s)\n", strerror(errno)); - close(pptp_sock); - return -1; - } - - if (send_pptp_out_call_rply(conn, msg, src_addr.sa_addr.pptp.call_id, PPTP_CALL_RES_OK, 0)) - return -1; - - conn->ppp.fd = pptp_sock; - conn->ppp.chan_name = strdup(inet_ntoa(dst_addr.sa_addr.pptp.sin_addr)); - conn->ppp.ctrl = &conn->ctrl; - conn->ctrl.ctx = &conn->ctx; - conn->ctrl.started = ppp_started; - conn->ctrl.finished = ppp_finished; - if (establish_ppp(&conn->ppp)) { - close(pptp_sock); - //if (send_pptp_stop_ctrl_conn_rqst(conn, 0, 0)) - conn->state = STATE_FIN; - return -1; - } - conn->state = STATE_PPP; - - triton_timer_del(&conn->timeout_timer); - conn->timeout_timer.period = 0; - - if (conf_echo_interval) { - conn->echo_timer.period = conf_echo_interval * 1000; - triton_timer_add(&conn->ctx, &conn->echo_timer, 0); - } - - return 0; -} - -static int pptp_echo_rqst(struct pptp_conn_t *conn) -{ - struct pptp_echo_rqst *in_msg = (struct pptp_echo_rqst *)conn->in_buf; - struct pptp_echo_rply out_msg = { - .header = PPTP_HEADER_CTRL(PPTP_ECHO_RQST), - .identifier = in_msg->identifier, - .result_code = 1, - }; - - return post_msg(conn, &out_msg, sizeof(out_msg)); -} - -static int pptp_echo_rply(struct pptp_conn_t *conn) -{ - struct pptp_echo_rply *msg = (struct pptp_echo_rply *)conn->in_buf; - if (msg->identifier != conn->echo_sent) { - log_error("pptp:echo: identifier mismatch\n"); - return -1; - } - conn->echo_sent = 0; - return 0; -} -static void pptp_send_echo(struct triton_timer_t *t) -{ - struct pptp_conn_t *conn = container_of(t, typeof(*conn), echo_timer); - struct pptp_echo_rqst msg = { - .header = PPTP_HEADER_CTRL(PPTP_ECHO_RQST), - }; - - if (conn->echo_sent) { - log_warn("pptp: no echo reply\n"); - disconnect(conn); - return; - } - - conn->echo_sent = random(); - msg.identifier = conn->echo_sent; - - if (post_msg(conn, &msg, sizeof(msg))) - disconnect(conn); -} - -static int process_packet(struct pptp_conn_t *conn) -{ - struct pptp_header *hdr = (struct pptp_header *)conn->in_buf; - switch(ntohs(hdr->ctrl_type)) - { - case PPTP_START_CTRL_CONN_RQST: - return pptp_start_ctrl_conn_rqst(conn); - case PPTP_STOP_CTRL_CONN_RQST: - return pptp_stop_ctrl_conn_rqst(conn); - case PPTP_OUT_CALL_RQST: - return pptp_out_call_rqst(conn); - case PPTP_ECHO_RQST: - return pptp_echo_rqst(conn); - case PPTP_ECHO_RPLY: - return pptp_echo_rply(conn); - } - return 0; -} - -static int pptp_read(struct triton_md_handler_t *h) -{ - struct pptp_conn_t *conn=container_of(h,typeof(*conn),hnd); - struct pptp_header *hdr=(struct pptp_header *)conn->in_buf; - int n; - - while(1) { - n = read(h->fd,conn->in_buf,PPTP_CTRL_SIZE_MAX-conn->in_size); - if (n < 0) { - if (errno == EINTR) - continue; - if (errno == EAGAIN) - return 0; - log_error("pptp: read: %s\n",strerror(errno)); - goto drop; - } - if (n == 0) - goto drop; - conn->in_size += n; - if (conn->in_size >= sizeof(*hdr)) { - if (hdr->magic != htonl(PPTP_MAGIC)) - goto drop; - if (ntohs(hdr->length) >= PPTP_CTRL_SIZE_MAX) - goto drop; - if (ntohs(hdr->length) > conn->in_size) - goto drop; - if (ntohs(hdr->length) == conn->in_size) { - if (ntohs(hdr->length) != PPTP_CTRL_SIZE(ntohs(hdr->ctrl_type))) - goto drop; - if (process_packet(conn)) - goto drop; - conn->in_size = 0; - } - } - } -drop: - disconnect(conn); - return 1; -} -static int pptp_write(struct triton_md_handler_t *h) -{ - struct pptp_conn_t *conn = container_of(h, typeof(*conn), hnd); - int n; - - while (1) { - n = write(h->fd, conn->out_buf+conn->out_pos, conn->out_size-conn->out_pos); - - if (n < 0) { - if (errno == EINTR) - continue; - if (errno == EAGAIN) - n = 0; - else { - if (errno != EPIPE) - log_error("pptp:post_msg: %s\n", strerror(errno)); - disconnect(conn); - return 1; - } - } - - conn->out_pos += n; - if (conn->out_pos == conn->out_size) { - conn->out_pos = 0; - conn->out_size = 0; - triton_md_disable_handler(h, MD_MODE_WRITE); - return 0; - } - } -} -static void pptp_timeout(struct triton_timer_t *t) -{ - struct pptp_conn_t *conn = container_of(t, typeof(*conn), timeout_timer); - disconnect(conn); -} -static void pptp_close(struct triton_ctx_t *ctx) -{ - struct pptp_conn_t *conn = container_of(ctx, typeof(*conn), ctx); - if (conn->state == STATE_PPP) { - conn->state = STATE_FIN; - ppp_terminate(&conn->ppp, 0); - } else - disconnect(conn); -} -static void ppp_started(struct ppp_t *ppp) -{ - log_msg("ppp_started\n"); -} -static void ppp_finished(struct ppp_t *ppp) -{ - struct pptp_conn_t *conn = container_of(ppp, typeof(*conn), ppp); - - log_msg("ppp_finished\n"); - close(conn->ppp.fd); - //send_pptp_stop_ctrl_conn_rqst(conn, 0, 0); - if (conn->state != STATE_CLOSE) { - conn->state = STATE_CLOSE; - disconnect(conn); - } -} - -//================================== - -struct pptp_serv_t -{ - struct triton_ctx_t ctx; - struct triton_md_handler_t hnd; -}; - -static int pptp_connect(struct triton_md_handler_t *h) -{ - struct sockaddr_in addr; - socklen_t size = sizeof(addr); - int sock; - struct pptp_conn_t *conn; - - while(1) { - sock = accept(h->fd, (struct sockaddr *)&addr, &size); - if (sock < 0) { - if (errno == EAGAIN) - return 0; - log_error("pptp: accept failed: %s\n", strerror(errno)); - continue; - } - - log_info("pptp: new connection from %s\n", inet_ntoa(addr.sin_addr)); - - if (fcntl(sock, F_SETFL, O_NONBLOCK)) { - log_error("pptp: failed to set nonblocking mode: %s, closing connection...\n", strerror(errno)); - close(sock); - continue; - } - - conn = malloc(sizeof(*conn)); - memset(conn, 0, sizeof(*conn)); - conn->hnd.fd = sock; - conn->hnd.read = pptp_read; - conn->hnd.write = pptp_write; - conn->ctx.close = pptp_close; - conn->in_buf = malloc(PPTP_CTRL_SIZE_MAX); - conn->out_buf = malloc(PPTP_CTRL_SIZE_MAX); - conn->timeout_timer.expire = pptp_timeout; - conn->timeout_timer.period = conf_timeout * 1000; - conn->echo_timer.expire = pptp_send_echo; - - triton_register_ctx(&conn->ctx); - triton_md_register_handler(&conn->ctx, &conn->hnd); - triton_md_enable_handler(&conn->hnd,MD_MODE_READ); - triton_timer_add(&conn->ctx, &conn->timeout_timer, 0); - } - return 0; -} -static void pptp_serv_close(struct triton_ctx_t *ctx) -{ - struct pptp_serv_t *s=container_of(ctx,typeof(*s),ctx); - triton_md_unregister_handler(&s->hnd); - close(s->hnd.fd); -} - -static struct pptp_serv_t serv= -{ - .hnd.read=pptp_connect, - .ctx.close=pptp_serv_close, -}; - -static void __init pptp_init(void) -{ - struct sockaddr_in addr; - char *opt; - - serv.hnd.fd = socket (PF_INET, SOCK_STREAM, 0); - if (serv.hnd.fd < 0) { - log_error("pptp: failed to create server socket: %s\n", strerror(errno)); - return; - } - addr.sin_family = AF_INET; - addr.sin_port = htons (PPTP_PORT); - addr.sin_addr.s_addr = htonl (INADDR_ANY); - if (bind (serv.hnd.fd, (struct sockaddr *) &addr, sizeof (addr)) < 0) { - perror("pptp: bind"); - log_error("pptp: failed to bind socket: %s\n", strerror(errno)); - close(serv.hnd.fd); - return; - } - - if (listen (serv.hnd.fd, 100) < 0) { - log_error("pptp: failed to listen socket: %s\n", strerror(errno)); - close(serv.hnd.fd); - return; - } - - if (fcntl(serv.hnd.fd, F_SETFL, O_NONBLOCK)) { - log_error("pptp: failed to set nonblocking mode: %s\n", strerror(errno)); - close(serv.hnd.fd); - return; - } - - triton_register_ctx(&serv.ctx); - triton_md_register_handler(&serv.ctx, &serv.hnd); - triton_md_enable_handler(&serv.hnd, MD_MODE_READ); - - opt = conf_get_opt("pptp", "timeout"); - if (opt && atoi(opt) > 0) - conf_timeout = atoi(opt); - - opt = conf_get_opt("pptp", "echo-interval"); - if (opt && atoi(opt) > 0) - conf_echo_interval = atoi(opt); -} - diff --git a/accel-pptpd/pptp_prot.h b/accel-pptpd/pptp_prot.h deleted file mode 100644 index 7e3ebdd..0000000 --- a/accel-pptpd/pptp_prot.h +++ /dev/null @@ -1,297 +0,0 @@ -#ifndef PPTP_PROT_H -#define PPTP_PROT_H - -#include - -#define hton8(x) (x) -#define ntoh8(x) (x) -#define hton16(x) htons(x) -#define ntoh16(x) ntohs(x) -#define hton32(x) htonl(x) -#define ntoh32(x) ntohl(x) - -/* PPTP magic numbers: ----------------------------------------- */ - -#define PPTP_MAGIC 0x1A2B3C4D /* Magic cookie for PPTP datagrams */ -#define PPTP_PORT 1723 /* PPTP TCP port number */ -#define PPTP_PROTO 47 /* PPTP IP protocol number */ - -/* PPTP result codes:---------------------------------------- */ -#define PPTP_CONN_RES_SUCCESS 1 -#define PPTP_CONN_RES_GE 2 -#define PPTP_CONN_RES_EXISTS 3 -#define PPTP_CONN_RES_AUTH 4 -#define PPTP_CONN_RES_PROTOCOL 5 - -#define PPTP_CONN_STOP_OK 1 -#define PPTP_CONN_STOP_GE 2 - -#define PPTP_CALL_RES_OK 1 -#define PPTP_CALL_RES_GE 2 - -#define PPTP_GE_NOCONN 1 - -/* Control Connection Message Types: --------------------------- */ - -#define PPTP_MESSAGE_CONTROL 1 -#define PPTP_MESSAGE_MANAGE 2 - -/* Control Message Types: -------------------------------------- */ - -/* (Control Connection Management) */ -#define PPTP_START_CTRL_CONN_RQST 1 -#define PPTP_START_CTRL_CONN_RPLY 2 -#define PPTP_STOP_CTRL_CONN_RQST 3 -#define PPTP_STOP_CTRL_CONN_RPLY 4 -#define PPTP_ECHO_RQST 5 -#define PPTP_ECHO_RPLY 6 - -/* (Call Management) */ -#define PPTP_OUT_CALL_RQST 7 -#define PPTP_OUT_CALL_RPLY 8 -#define PPTP_IN_CALL_RQST 9 -#define PPTP_IN_CALL_RPLY 10 -#define PPTP_IN_CALL_CONNECT 11 -#define PPTP_CALL_CLEAR_RQST 12 -#define PPTP_CALL_CLEAR_NTFY 13 - -/* (Error Reporting) */ -#define PPTP_WAN_ERR_NTFY 14 - -/* (PPP Session Control) */ -#define PPTP_SET_LINK_INFO 15 - -/* PPTP version information: --------------------------------------*/ -#define PPTP_VERSION_STRING "1.00" -#define PPTP_VERSION 0x100 -#define PPTP_FIRMWARE_STRING "0.01" -#define PPTP_FIRMWARE_VERSION 0x001 - -#define PPTP_HOSTNAME "local" -#define PPTP_VENDOR "cananian" - -/* PPTP capabilities: ---------------------------------------------*/ - -/* (Framing capabilities for msg sender) */ -#define PPTP_FRAME_ASYNC 1 -#define PPTP_FRAME_SYNC 2 -#define PPTP_FRAME_ANY 3 - -/* (Bearer capabilities for msg sender) */ -#define PPTP_BEARER_ANALOG 1 -#define PPTP_BEARER_DIGITAL 2 -#define PPTP_BEARER_ANY 3 - -#define PPTP_RESULT_GENERAL_ERROR 2 - -/* (Reasons to close a connection) */ -#define PPTP_STOP_NONE 1 /* no good reason */ -#define PPTP_STOP_PROTOCOL 2 /* can't support peer's protocol version */ -#define PPTP_STOP_LOCAL_SHUTDOWN 3 /* requester is being shut down */ - -/* PPTP datagram structures (all data in network byte order): ----------*/ - -struct pptp_header -{ - uint16_t length; /* message length in octets, including header */ - uint16_t pptp_type; /* PPTP message type. 1 for control message. */ - uint32_t magic; /* this should be PPTP_MAGIC. */ - uint16_t ctrl_type; /* Control message type (0-15) */ - uint16_t reserved0; /* reserved. MUST BE ZERO. */ -}__attribute__((packed)); - -struct pptp_start_ctrl_conn /* for control message types 1 and 2 */ -{ - struct pptp_header header; - - uint16_t version; /* PPTP protocol version. = PPTP_VERSION */ - uint8_t result_code; /* these two fields should be zero on rqst msg*/ - uint8_t error_code; /* 0 unless result_code==2 (General Error) */ - uint32_t framing_cap; /* Framing capabilities */ - uint32_t bearer_cap; /* Bearer Capabilities */ - uint16_t max_channels; /* Maximum Channels (=0 for PNS, PAC ignores) */ - uint16_t firmware_rev; /* Firmware or Software Revision */ - uint8_t hostname[64]; /* Host Name (64 octets, zero terminated) */ - uint8_t vendor[64]; /* Vendor string (64 octets, zero term.) */ -}__attribute__((packed)); - -struct pptp_stop_ctrl_conn /* for control message types 3 and 4 */ -{ - struct pptp_header header; - - uint8_t reason_result; /* reason for rqst, result for rply */ - uint8_t error_code; /* MUST be 0, unless rply result==2 (general err)*/ - uint16_t reserved1; /* MUST be 0 */ -}__attribute__((packed)); - -struct pptp_echo_rqst /* for control message type 5 */ -{ - struct pptp_header header; - uint32_t identifier; /* arbitrary value set by sender which is used */ - /* to match up reply and request */ -}__attribute__((packed)); - -struct pptp_echo_rply /* for control message type 6 */ -{ - struct pptp_header header; - uint32_t identifier; /* should correspond to id of rqst */ - uint8_t result_code; - uint8_t error_code; /* =0, unless result_code==2 (general error) */ - uint16_t reserved1; /* MUST BE ZERO */ -}__attribute__((packed)); - -struct pptp_out_call_rqst /* for control message type 7 */ -{ - struct pptp_header header; - uint16_t call_id; /* Call ID (unique id used to multiplex data) */ - uint16_t call_sernum; /* Call Serial Number (used for logging) */ - uint32_t bps_min; /* Minimum BPS (lowest acceptable line speed) */ - uint32_t bps_max; /* Maximum BPS (highest acceptable line speed) */ - uint32_t bearer; /* Bearer type */ - uint32_t framing; /* Framing type */ - uint16_t recv_size; /* Recv. Window Size (no. of buffered packets) */ - uint16_t delay; /* Packet Processing Delay (in 1/10 sec) */ - uint16_t phone_len; /* Phone Number Length (num. of valid digits) */ - uint16_t reserved1; /* MUST BE ZERO */ - uint8_t phone_num[64]; /* Phone Number (64 octets, null term.) */ - uint8_t subaddress[64]; /* Subaddress (64 octets, null term.) */ -}__attribute__((packed)); - -struct pptp_out_call_rply /* for control message type 8 */ -{ - struct pptp_header header; - uint16_t call_id; /* Call ID (used to multiplex data over tunnel)*/ - uint16_t call_id_peer; /* Peer's Call ID (call_id of pptp_out_call_rqst)*/ - uint8_t result_code; /* Result Code (1 is no errors) */ - uint8_t error_code; /* Error Code (=0 unless result_code==2) */ - uint16_t cause_code; /* Cause Code (addt'l failure information) */ - uint32_t speed; /* Connect Speed (in BPS) */ - uint16_t recv_size; /* Recv. Window Size (no. of buffered packets) */ - uint16_t delay; /* Packet Processing Delay (in 1/10 sec) */ - uint32_t channel; /* Physical Channel ID (for logging) */ -}__attribute__((packed)); - -struct pptp_in_call_rqst /* for control message type 9 */ -{ - struct pptp_header header; - uint16_t call_id; /* Call ID (unique id used to multiplex data) */ - uint16_t call_sernum; /* Call Serial Number (used for logging) */ - uint32_t bearer; /* Bearer type */ - uint32_t channel; /* Physical Channel ID (for logging) */ - uint16_t dialed_len; /* Dialed Number Length (# of valid digits) */ - uint16_t dialing_len; /* Dialing Number Length (# of valid digits) */ - uint8_t dialed_num[64]; /* Dialed Number (64 octets, zero term.) */ - uint8_t dialing_num[64]; /* Dialing Number (64 octets, zero term.) */ - uint8_t subaddress[64]; /* Subaddress (64 octets, zero term.) */ -}__attribute__((packed)); - -struct pptp_in_call_rply /* for control message type 10 */ -{ - struct pptp_header header; - uint16_t call_id; /* Call ID (used to multiplex data over tunnel)*/ - uint16_t call_id_peer; /* Peer's Call ID (call_id of pptp_out_call_rqst)*/ - uint8_t result_code; /* Result Code (1 is no errors) */ - uint8_t error_code; /* Error Code (=0 unless result_code==2) */ - uint16_t recv_size; /* Recv. Window Size (no. of buffered packets) */ - uint16_t delay; /* Packet Processing Delay (in 1/10 sec) */ - uint16_t reserved1; /* MUST BE ZERO */ -}__attribute__((packed)); - -struct pptp_in_call_connect /* for control message type 11 */ -{ - struct pptp_header header; - uint16_t call_id_peer; /* Peer's Call ID (call_id of pptp_out_call_rqst)*/ - uint16_t reserved1; /* MUST BE ZERO */ - uint32_t speed; /* Connect Speed (in BPS) */ - uint16_t recv_size; /* Recv. Window Size (no. of buffered packets) */ - uint16_t delay; /* Packet Processing Delay (in 1/10 sec) */ - uint32_t framing; /* Framing type */ -}__attribute__((packed)); - -struct pptp_call_clear_rqst /* for control message type 12 */ -{ - struct pptp_header header; - uint16_t call_id; /* Call ID (used to multiplex data over tunnel)*/ - uint16_t reserved1; /* MUST BE ZERO */ -}__attribute__((packed)); - -struct pptp_call_clear_ntfy /* for control message type 13 */ -{ - struct pptp_header header; - uint16_t call_id; /* Call ID (used to multiplex data over tunnel)*/ - uint8_t result_code; /* Result Code */ - uint8_t error_code; /* Error Code (=0 unless result_code==2) */ - uint16_t cause_code; /* Cause Code (for ISDN, is Q.931 cause code) */ - uint16_t reserved1; /* MUST BE ZERO */ - uint8_t call_stats[128]; /* Call Statistics: 128 octets, ascii, 0-term */ -}__attribute__((packed)); - -struct pptp_wan_err_ntfy /* for control message type 14 */ -{ - struct pptp_header header; - uint16_t call_id_peer; /* Peer's Call ID (call_id of pptp_out_call_rqst)*/ - uint16_t reserved1; /* MUST BE ZERO */ - uint32_t crc_errors; /* CRC errors */ - uint32_t frame_errors; /* Framing errors */ - uint32_t hard_errors; /* Hardware overruns */ - uint32_t buff_errors; /* Buffer overruns */ - uint32_t time_errors; /* Time-out errors */ - uint32_t align_errors; /* Alignment errors */ -}__attribute__((packed)); - -struct pptp_set_link_info /* for control message type 15 */ -{ - struct pptp_header header; - uint16_t call_id_peer; /* Peer's Call ID (call_id of pptp_out_call_rqst) */ - uint16_t reserved1; /* MUST BE ZERO */ - uint32_t send_accm; /* Send ACCM (for PPP packets; default 0xFFFFFFFF)*/ - uint32_t recv_accm; /* Receive ACCM (for PPP pack.;default 0xFFFFFFFF)*/ -}__attribute__((packed)); - -/* helpful #defines: -------------------------------------------- */ -#define pptp_isvalid_ctrl(header, type, length) \ - (!( ( ntoh16(((struct pptp_header *)header)->length) < (length) ) || \ - ( ntoh16(((struct pptp_header *)header)->pptp_type) !=(type) ) || \ - ( ntoh32(((struct pptp_header *)header)->magic) !=PPTP_MAGIC) || \ - ( ntoh16(((struct pptp_header *)header)->ctrl_type) > PPTP_SET_LINK_INFO) || \ - ( ntoh16(((struct pptp_header *)header)->reserved0) !=0 ) )) - -#define PPTP_HEADER_CTRL(type) \ -{ hton16(PPTP_CTRL_SIZE(type)), \ - hton16(PPTP_MESSAGE_CONTROL), \ - hton32(PPTP_MAGIC), \ - hton16(type), 0 } - -#define PPTP_CTRL_SIZE(type) ( \ -(type==PPTP_START_CTRL_CONN_RQST)?sizeof(struct pptp_start_ctrl_conn): \ -(type==PPTP_START_CTRL_CONN_RPLY)?sizeof(struct pptp_start_ctrl_conn): \ -(type==PPTP_STOP_CTRL_CONN_RQST )?sizeof(struct pptp_stop_ctrl_conn): \ -(type==PPTP_STOP_CTRL_CONN_RPLY )?sizeof(struct pptp_stop_ctrl_conn): \ -(type==PPTP_ECHO_RQST )?sizeof(struct pptp_echo_rqst): \ -(type==PPTP_ECHO_RPLY )?sizeof(struct pptp_echo_rply): \ -(type==PPTP_OUT_CALL_RQST )?sizeof(struct pptp_out_call_rqst): \ -(type==PPTP_OUT_CALL_RPLY )?sizeof(struct pptp_out_call_rply): \ -(type==PPTP_IN_CALL_RQST )?sizeof(struct pptp_in_call_rqst): \ -(type==PPTP_IN_CALL_RPLY )?sizeof(struct pptp_in_call_rply): \ -(type==PPTP_IN_CALL_CONNECT )?sizeof(struct pptp_in_call_connect): \ -(type==PPTP_CALL_CLEAR_RQST )?sizeof(struct pptp_call_clear_rqst): \ -(type==PPTP_CALL_CLEAR_NTFY )?sizeof(struct pptp_call_clear_ntfy): \ -(type==PPTP_WAN_ERR_NTFY )?sizeof(struct pptp_wan_err_ntfy): \ -(type==PPTP_SET_LINK_INFO )?sizeof(struct pptp_set_link_info): \ -0) -#define max(a,b) (((a)>(b))?(a):(b)) -#define PPTP_CTRL_SIZE_MAX ( \ -max(sizeof(struct pptp_start_ctrl_conn), \ -max(sizeof(struct pptp_echo_rqst), \ -max(sizeof(struct pptp_echo_rply), \ -max(sizeof(struct pptp_out_call_rqst), \ -max(sizeof(struct pptp_out_call_rply), \ -max(sizeof(struct pptp_in_call_rqst), \ -max(sizeof(struct pptp_in_call_rply), \ -max(sizeof(struct pptp_in_call_connect), \ -max(sizeof(struct pptp_call_clear_rqst), \ -max(sizeof(struct pptp_call_clear_ntfy), \ -max(sizeof(struct pptp_wan_err_ntfy), \ -max(sizeof(struct pptp_set_link_info), 0))))))))))))) - -#endif diff --git a/accel-pptpd/pptpd.h b/accel-pptpd/pptpd.h deleted file mode 100644 index d98978e..0000000 --- a/accel-pptpd/pptpd.h +++ /dev/null @@ -1,26 +0,0 @@ -// -// C++ Interface: pptpd -// -// Description: -// -// -// Author: , (C) 2009 -// -// Copyright: See COPYING file that comes with this distribution -// -// - -#ifndef PPTPD_H -#define PPTPD_H - -struct ctrl_thread_t -{ - pthread_t thr; - pthread_mutex_t lock; - int count; - int pipe_fd[2]; -}; - -int ctrl_init(struct ctrl_thread_t*); - -#endif diff --git a/accel-pptpd/pwdb.c b/accel-pptpd/pwdb.c index 4a03846..37dd0c5 100644 --- a/accel-pptpd/pwdb.c +++ b/accel-pptpd/pwdb.c @@ -1,12 +1,12 @@ #include "pwdb.h" #include "ppp.h" -int pwdb_check(struct ppp_t *ppp,const char *username,const char *password) +__export int pwdb_check(struct ppp_t *ppp,const char *username,const char *password) { return 0; } -char *pwdb_get_passwd(struct ppp_t *ppp, const char *username) +__export char *pwdb_get_passwd(struct ppp_t *ppp, const char *username) { return strdup("test"); } diff --git a/accel-pptpd/triton/loader.c b/accel-pptpd/triton/loader.c index a390f0a..24d1cbc 100644 --- a/accel-pptpd/triton/loader.c +++ b/accel-pptpd/triton/loader.c @@ -2,7 +2,45 @@ #include #include #include -#include +#include +#include #include "triton_p.h" +int load_modules(const char *name) +{ + struct conf_sect_t *sect; + struct conf_option_t *opt; + + sect = conf_get_section(name); + if (!sect) { + fprintf(stderr, "loader: section '%s' not found\n", name); + return -1; + } + + char *cwd = getcwd(NULL,0); + + list_for_each_entry(opt, §->items, entry) { + if (!strcmp(opt->name,"path") && opt->val) { + if (chdir(opt->val)) { + fprintf(stderr,"loader: chdir '%s': %s\n", opt->val, strerror(errno)); + goto out_err; + } + continue; + } + if (!dlopen(opt->name, RTLD_NOW | RTLD_GLOBAL)) { + fprintf(stderr,"loader: failed to load module '%s': %s\n",opt->name, dlerror()); + goto out_err; + } + } + + chdir(cwd); + free(cwd); + return 0; + +out_err: + chdir(cwd); + free(cwd); + return -1; +} + diff --git a/accel-pptpd/triton/triton.c b/accel-pptpd/triton/triton.c index 8c8d5a3..9aa7462 100644 --- a/accel-pptpd/triton/triton.c +++ b/accel-pptpd/triton/triton.c @@ -188,7 +188,7 @@ void __export triton_unregister_ctx(struct triton_ctx_t *ud) spin_unlock(&ctx_list_lock); } -int __export triton_init(const char *conf_file) +int __export triton_init(const char *conf_file, const char *mod_sect) { ctx_pool = mempool_create(sizeof(struct _triton_ctx_t)); @@ -210,7 +210,10 @@ int __export triton_init(const char *conf_file) if (timer_init()) return -1; - + + if (load_modules(mod_sect)) + return -1; + return 0; } diff --git a/accel-pptpd/triton/triton.h b/accel-pptpd/triton/triton.h index 2543a23..1009d67 100644 --- a/accel-pptpd/triton/triton.h +++ b/accel-pptpd/triton/triton.h @@ -67,7 +67,7 @@ char *conf_get_opt(const char *sect, const char *name); #define TRITON_ERR_NOMSG -6 #define TRITON_ERR_BUSY -5 -int triton_init(const char *conf_file); +int triton_init(const char *conf_file, const char *mod_sect); void triton_run(void); void triton_terminate(void); diff --git a/accel-pptpd/triton/triton_p.h b/accel-pptpd/triton/triton_p.h index 24850bf..0f7e716 100644 --- a/accel-pptpd/triton/triton_p.h +++ b/accel-pptpd/triton/triton_p.h @@ -75,5 +75,6 @@ void triton_thread_wakeup(struct _triton_thread_t*); int conf_load(const char *fname); void triton_log_error(const char *fmt,...); void triton_log_debug(const char *fmt,...); +int load_modules(const char *name); #endif -- cgit v1.2.3