From 760d8427f133df486a145e6e7ac7610caf2356fc Mon Sep 17 00:00:00 2001 From: Kozlov Dmitry Date: Tue, 17 Aug 2010 17:29:46 +0400 Subject: reworked/rewrited lcp handling code to become more abstract --- accel-pptpd/pwdb.c | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 accel-pptpd/pwdb.c (limited to 'accel-pptpd/pwdb.c') diff --git a/accel-pptpd/pwdb.c b/accel-pptpd/pwdb.c new file mode 100644 index 00000000..12130b34 --- /dev/null +++ b/accel-pptpd/pwdb.c @@ -0,0 +1,7 @@ +#include "pwdb.h" +#include "ppp.h" + +int pwdb_check(struct ppp_t *ppp,const char *username,const char *password) +{ + return 0; +} -- cgit v1.2.3 From 9e52c15995a90fff71087cda4bb0c600b1d1d28d Mon Sep 17 00:00:00 2001 From: Kozlov Dmitry Date: Mon, 23 Aug 2010 18:27:31 +0400 Subject: implemented chap authentication fixed auth type selection when configure-nak received --- accel-pptpd/CMakeLists.txt | 3 +- accel-pptpd/auth_chap.c | 294 ++++++++++++++++++++++++++++++++++++++++++ accel-pptpd/auth_pap.c | 6 +- accel-pptpd/ipcp_opt_ipaddr.c | 1 + accel-pptpd/log.c | 4 +- accel-pptpd/ppp_auth.c | 52 +++++--- accel-pptpd/ppp_ccp.c | 20 ++- accel-pptpd/ppp_ipcp.c | 20 ++- accel-pptpd/ppp_lcp.c | 12 +- accel-pptpd/pwdb.c | 5 + accel-pptpd/pwdb.h | 1 + 11 files changed, 383 insertions(+), 35 deletions(-) create mode 100644 accel-pptpd/auth_chap.c (limited to 'accel-pptpd/pwdb.c') diff --git a/accel-pptpd/CMakeLists.txt b/accel-pptpd/CMakeLists.txt index bbe59b80..82c8a9e0 100644 --- a/accel-pptpd/CMakeLists.txt +++ b/accel-pptpd/CMakeLists.txt @@ -20,6 +20,7 @@ ADD_EXECUTABLE(pptpd ppp_auth.c auth_pap.c + auth_chap.c ppp_ipcp.c ipcp_opt_ipaddr.c @@ -30,4 +31,4 @@ ADD_EXECUTABLE(pptpd pwdb.c ipdb.c ) -TARGET_LINK_LIBRARIES(pptpd pthread triton) +TARGET_LINK_LIBRARIES(pptpd pthread triton ssl) diff --git a/accel-pptpd/auth_chap.c b/accel-pptpd/auth_chap.c new file mode 100644 index 00000000..4b3d0398 --- /dev/null +++ b/accel-pptpd/auth_chap.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_REJ; +} + +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); + } + + 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); + } + + 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_pap.c b/accel-pptpd/auth_pap.c index 85e71e60..92ddef52 100644 --- a/accel-pptpd/auth_pap.c +++ b/accel-pptpd/auth_pap.c @@ -6,6 +6,8 @@ #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" @@ -104,7 +106,7 @@ static int lcp_send_conf_req(struct ppp_t *ppp, struct auth_data_t *d, uint8_t * static int lcp_recv_conf_req(struct ppp_t *ppp, struct auth_data_t *d, uint8_t *ptr) { - return 0; + return LCP_OPT_ACK; } static void pap_send_ack(struct pap_auth_data_t *p, int id) @@ -168,7 +170,7 @@ static int pap_recv_req(struct pap_auth_data_t *p,struct pap_hdr_t *hdr) peer_id=strndup((const char*)peer_id,peer_id_len); passwd=strndup((const char*)ptr,passwd_len); - if (pwdb_check(peer_id,passwd)) + if (pwdb_check(p->ppp,peer_id,passwd)) { log_warn("PAP: authentication error\n"); pap_send_nak(p,hdr->id); diff --git a/accel-pptpd/ipcp_opt_ipaddr.c b/accel-pptpd/ipcp_opt_ipaddr.c index a68212f1..8eae84b6 100644 --- a/accel-pptpd/ipcp_opt_ipaddr.c +++ b/accel-pptpd/ipcp_opt_ipaddr.c @@ -12,6 +12,7 @@ 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 diff --git a/accel-pptpd/log.c b/accel-pptpd/log.c index 4ee86e1a..992f24d5 100644 --- a/accel-pptpd/log.c +++ b/accel-pptpd/log.c @@ -33,7 +33,7 @@ #define LOG_INFO 2 #define LOG_DEBUG 3 -static FILE *log_file=NULL; +static FILE *log_file; static int log_level=10; static int log_color=1; static const char* level_name[]={"error","warning","info","debug"}; @@ -47,6 +47,8 @@ static void do_log(int level,const char *fmt,va_list ap) struct timeval tv; //pthread_mutex_lock(&lock); + if (!log_file) + log_file=stdout; if (msg_completed) { gettimeofday(&tv,NULL); diff --git a/accel-pptpd/ppp_auth.c b/accel-pptpd/ppp_auth.c index ee173a79..c7075fb1 100644 --- a/accel-pptpd/ppp_auth.c +++ b/accel-pptpd/ppp_auth.c @@ -107,10 +107,15 @@ static int auth_send_conf_req(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, u if (list_empty(&auth_opt->auth_list)) return 0; - if (!auth_opt->auth) + if (!auth_opt->auth || auth_opt->auth->state==LCP_OPT_NAK) { - d=list_entry(auth_opt->auth_list.next,typeof(*d),entry); - auth_opt->auth=d; + 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; @@ -126,6 +131,7 @@ static int auth_recv_conf_req(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, u 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; @@ -134,10 +140,13 @@ static int auth_recv_conf_req(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, u { if (d->proto==ntohs(opt16->val)) { - if (d->h->recv_conf_req(lcp->ppp,d,(uint8_t*)(opt16+1))) + 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 LCP_OPT_ACK; + return r; } } @@ -166,20 +175,16 @@ static int auth_recv_conf_ack(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, u 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 lcp_opt16_t *opt16=(struct lcp_opt16_t*)ptr; struct auth_data_t *d; - list_for_each_entry(d,&auth_opt->auth_list,entry) + if (!auth_opt->auth) { - if (d->proto==ntohs(opt16->val)) - { - d->state=LCP_OPT_NAK; - if (d->h->recv_conf_req(lcp->ppp,d,(uint8_t*)(opt16+1))) - break; - auth_opt->auth=d; - return 0; - } + 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) { @@ -194,9 +199,22 @@ static int auth_recv_conf_nak(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, u 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 (list_empty(&auth_opt->auth_list)) - return 0; + 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; diff --git a/accel-pptpd/ppp_ccp.c b/accel-pptpd/ppp_ccp.c index 0a7246d6..3c8c5cdb 100644 --- a/accel-pptpd/ppp_ccp.c +++ b/accel-pptpd/ppp_ccp.c @@ -449,8 +449,10 @@ static int ccp_recv_conf_ack(struct ppp_ccp_t *ccp,uint8_t *data,int size) { log_debug(" "); lopt->h->print(log_debug,lopt,data); - if (lopt->h->recv_conf_ack) - lopt->h->recv_conf_ack(ccp,lopt,data); + if (!lopt->h->recv_conf_ack) + break; + if (lopt->h->recv_conf_ack(ccp,lopt,data)) + res=-1; break; } } @@ -468,7 +470,13 @@ static void ccp_recv(struct ppp_handler_t*h) 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); break; case CONFACK: - ccp_recv_conf_ack(ccp,(uint8_t*)(hdr+1),ntohs(hdr->len)-PPP_HDRLEN); - ppp_fsm_recv_conf_ack(&ccp->fsm); + if (ccp_recv_conf_ack(ccp,(uint8_t*)(hdr+1),ntohs(hdr->len)-PPP_HDRLEN)) + ppp_terminate(ccp->ppp); + 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); diff --git a/accel-pptpd/ppp_ipcp.c b/accel-pptpd/ppp_ipcp.c index fb089a5d..fc25230a 100644 --- a/accel-pptpd/ppp_ipcp.c +++ b/accel-pptpd/ppp_ipcp.c @@ -440,8 +440,10 @@ static int ipcp_recv_conf_ack(struct ppp_ipcp_t *ipcp,uint8_t *data,int size) { log_debug(" "); lopt->h->print(log_debug,lopt,data); - if (lopt->h->recv_conf_ack) - lopt->h->recv_conf_ack(ipcp,lopt,data); + if (!lopt->h->recv_conf_ack) + break; + if (lopt->h->recv_conf_ack(ipcp,lopt,data)) + res=-1; break; } } @@ -459,7 +461,13 @@ static void ipcp_recv(struct ppp_handler_t*h) 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); break; case CONFACK: - ipcp_recv_conf_ack(ipcp,(uint8_t*)(hdr+1),ntohs(hdr->len)-PPP_HDRLEN); - ppp_fsm_recv_conf_ack(&ipcp->fsm); + if (ipcp_recv_conf_ack(ipcp,(uint8_t*)(hdr+1),ntohs(hdr->len)-PPP_HDRLEN)) + ppp_terminate(ipcp->ppp); + 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); diff --git a/accel-pptpd/ppp_lcp.c b/accel-pptpd/ppp_lcp.c index b9d0de34..dbc819a1 100644 --- a/accel-pptpd/ppp_lcp.c +++ b/accel-pptpd/ppp_lcp.c @@ -436,8 +436,10 @@ static int lcp_recv_conf_ack(struct ppp_lcp_t *lcp,uint8_t *data,int size) { log_debug(" "); lopt->h->print(log_debug,lopt,data); - if (lopt->h->recv_conf_ack) - lopt->h->recv_conf_ack(lcp,lopt,data); + if (!lopt->h->recv_conf_ack) + break; + if (lopt->h->recv_conf_ack(lcp,lopt,data)) + res=-1; break; } } @@ -514,8 +516,10 @@ static void lcp_recv(struct ppp_handler_t*h) ppp_terminate(lcp->ppp); break; case CONFACK: - lcp_recv_conf_ack(lcp,(uint8_t*)(hdr+1),ntohs(hdr->len)-PPP_HDRLEN); - ppp_fsm_recv_conf_ack(&lcp->fsm); + if (lcp_recv_conf_ack(lcp,(uint8_t*)(hdr+1),ntohs(hdr->len)-PPP_HDRLEN)) + ppp_terminate(lcp->ppp); + 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); diff --git a/accel-pptpd/pwdb.c b/accel-pptpd/pwdb.c index 12130b34..4a03846e 100644 --- a/accel-pptpd/pwdb.c +++ b/accel-pptpd/pwdb.c @@ -5,3 +5,8 @@ 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) +{ + return strdup("test"); +} diff --git a/accel-pptpd/pwdb.h b/accel-pptpd/pwdb.h index 820e269c..07c45b09 100644 --- a/accel-pptpd/pwdb.h +++ b/accel-pptpd/pwdb.h @@ -4,6 +4,7 @@ struct ppp_t; int pwdb_check(struct ppp_t*,const char *username,const char *password); +char *pwdb_get_passwd(struct ppp_t*, const char *username); #endif -- cgit v1.2.3 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/pwdb.c') diff --git a/accel-pptpd/CMakeLists.txt b/accel-pptpd/CMakeLists.txt index fc6ac0ed..64f351fa 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 736f71af..fa2f128c 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 00000000..f0e6465c --- /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 00000000..5577794f --- /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 00000000..1cf5eb8b --- /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 00000000..502f3686 --- /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 00000000..92ddef52 --- /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 5577794f..00000000 --- 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 1cf5eb8b..00000000 --- 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 502f3686..00000000 --- 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 92ddef52..00000000 --- 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 00000000..97f8e0d8 --- /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 00000000..b503d85e --- /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 00000000..7e3ebdd4 --- /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 da327a1c..00000000 --- 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 00000000..da327a1c --- /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 00000000..77ab85b8 --- /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 00000000..4b6d9a91 --- /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 00000000..49a04dd9 --- /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 00000000..ae3fa2ef --- /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 00000000..527c8538 --- /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 00000000..5c6b7f47 --- /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 00000000..49027522 --- /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 00000000..360efe4d --- /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 00000000..6f35a2fe --- /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 b7417989..00000000 --- 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 2fa92519..00000000 --- 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 fb139710..8ac86cb6 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 c4c221ce..00000000 --- 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 dc94ac62..00000000 --- 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 e1c13b47..00000000 --- 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 8700bf49..00000000 --- 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 49045b7e..00000000 --- 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 992f24d5..a29ce846 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 05036251..c60d35fb 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 d1b38832..00000000 --- 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 b3b1be89..00000000 --- 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 00000000..f4c0f04a --- /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 00000000..b7417989 --- /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 00000000..2fa92519 --- /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 00000000..c4c221ce --- /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 00000000..dc94ac62 --- /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 00000000..e1c13b47 --- /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 00000000..8700bf49 --- /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 00000000..5c2eb910 --- /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 00000000..d880fc2a --- /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 00000000..32f63d5b --- /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 00000000..f1880d5d --- /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 00000000..fe10886e --- /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 00000000..3e459697 --- /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 00000000..85cf15a2 --- /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 00000000..bc958fe9 --- /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 00000000..6ac145b2 --- /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 00000000..9e0c99ee --- /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 00000000..afcfc102 --- /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 00000000..46bc17b9 --- /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 3208edac..00000000 --- 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 f1880d5d..00000000 --- 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 924df7d2..00000000 --- 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 6aca0ab1..00000000 --- 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 c2d00dc6..00000000 --- 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 bc958fe9..00000000 --- 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 4acf6947..00000000 --- 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 a2ce3b35..00000000 --- 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 6c5fd189..00000000 --- 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 7110db20..00000000 --- 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 2d69a6ef..00000000 --- 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 7e3ebdd4..00000000 --- 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 d98978e5..00000000 --- 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 4a03846e..37dd0c5f 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 a390f0a9..24d1cbcb 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 8c8d5a38..9aa7462b 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 2543a234..1009d67a 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 24850bfd..0f7e7160 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 From af5a96e2d04056b065a36ecbd140a16d0685c7e6 Mon Sep 17 00:00:00 2001 From: Kozlov Dmitry Date: Sat, 4 Sep 2010 16:20:11 +0400 Subject: started work on radius module --- accel-pptpd/CMakeLists.txt | 2 + accel-pptpd/accel-pptpd.conf | 4 + accel-pptpd/ppp/ppp.c | 5 +- accel-pptpd/ppp/ppp.h | 30 +++-- accel-pptpd/ppp/ppp_notify.c | 34 ++++++ accel-pptpd/ppp/ppp_pd.c | 12 ++ accel-pptpd/pwdb.c | 67 ++++++++++- accel-pptpd/pwdb.h | 23 +++- accel-pptpd/radius/CMakeLists.txt | 8 ++ accel-pptpd/radius/dict.c | 219 ++++++++++++++++++++++++++++++++++ accel-pptpd/radius/dictionary | 240 ++++++++++++++++++++++++++++++++++++++ accel-pptpd/radius/radius.c | 74 ++++++++++++ accel-pptpd/radius/radius.h | 28 +++++ 13 files changed, 729 insertions(+), 17 deletions(-) create mode 100644 accel-pptpd/ppp/ppp_notify.c create mode 100644 accel-pptpd/ppp/ppp_pd.c create mode 100644 accel-pptpd/radius/CMakeLists.txt create mode 100644 accel-pptpd/radius/dict.c create mode 100644 accel-pptpd/radius/dictionary create mode 100644 accel-pptpd/radius/radius.c create mode 100644 accel-pptpd/radius/radius.h (limited to 'accel-pptpd/pwdb.c') diff --git a/accel-pptpd/CMakeLists.txt b/accel-pptpd/CMakeLists.txt index 64f351fa..041c6681 100644 --- a/accel-pptpd/CMakeLists.txt +++ b/accel-pptpd/CMakeLists.txt @@ -8,6 +8,7 @@ INCLUDE_DIRECTORIES(include) ADD_SUBDIRECTORY(triton) ADD_SUBDIRECTORY(ctrl) ADD_SUBDIRECTORY(auth) +ADD_SUBDIRECTORY(radius) ADD_EXECUTABLE(pptpd ppp/ppp.c @@ -22,6 +23,7 @@ ADD_EXECUTABLE(pptpd ppp/ipcp_opt_ipaddr.c ppp/ipcp_opt_dns.c ppp/ppp_ccp.c + ppp/ppp_notify.c log.c pwdb.c diff --git a/accel-pptpd/accel-pptpd.conf b/accel-pptpd/accel-pptpd.conf index fa2f128c..9b635340 100644 --- a/accel-pptpd/accel-pptpd.conf +++ b/accel-pptpd/accel-pptpd.conf @@ -1,6 +1,7 @@ [modules] ./libpptp.so ./libauth_mschap_v2.so +./libradius.so [core] log-error=/dev/stderr @@ -11,3 +12,6 @@ echo-failure=3 [pptp] echo-interval=3 + +[radius] +dictionary=dictionary diff --git a/accel-pptpd/ppp/ppp.c b/accel-pptpd/ppp/ppp.c index 5c2eb910..4ad8221b 100644 --- a/accel-pptpd/ppp/ppp.c +++ b/accel-pptpd/ppp/ppp.c @@ -18,6 +18,7 @@ #include "log.h" static LIST_HEAD(layers); +int sock_fd; struct layer_node_t { @@ -94,6 +95,7 @@ int __export establish_ppp(struct ppp_t *ppp) INIT_LIST_HEAD(&ppp->chan_handlers); INIT_LIST_HEAD(&ppp->unit_handlers); + INIT_LIST_HEAD(&ppp->pd_list); init_layers(ppp); @@ -127,6 +129,7 @@ int __export establish_ppp(struct ppp_t *ppp) log_debug("ppp established\n"); + ppp_notify_started(ppp); start_first_layer(ppp); return 0; @@ -159,6 +162,7 @@ static void destablish_ppp(struct ppp_t *ppp) log_debug("ppp destablished\n"); + ppp_notify_finished(ppp); ppp->ctrl->finished(ppp); } @@ -481,7 +485,6 @@ struct ppp_layer_data_t *ppp_find_layer_data(struct ppp_t *ppp, struct ppp_layer return NULL; } -int sock_fd; static void __init ppp_init(void) { sock_fd = socket(AF_INET, SOCK_DGRAM, 0); diff --git a/accel-pptpd/ppp/ppp.h b/accel-pptpd/ppp/ppp.h index d880fc2a..da2fb97c 100644 --- a/accel-pptpd/ppp/ppp.h +++ b/accel-pptpd/ppp/ppp.h @@ -12,13 +12,6 @@ #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. @@ -48,8 +41,6 @@ #define PPP_LAYER_CCP 3 #define PPP_LAYER_IPCP 4 -#define AUTH_MAX 3 - struct ppp_t; struct ppp_ctrl_t @@ -59,6 +50,20 @@ struct ppp_ctrl_t void (*finished)(struct ppp_t*); }; +struct ppp_notified_t +{ + struct list_head entry; + void (*started)(struct ppp_notified_t *, struct ppp_t *); + void (*finished)(struct ppp_notified_t *, struct ppp_t *); + void (*authenticated)(struct ppp_notified_t *, struct ppp_t *); +}; + +struct ppp_pd_t +{ + struct list_head entry; + void *key; +}; + struct ppp_t { struct triton_md_handler_t chan_hnd; @@ -87,6 +92,8 @@ struct ppp_t struct list_head layers; struct ppp_lcp_t *lcp; + + struct list_head pd_list; }; struct ppp_layer_t; @@ -134,5 +141,10 @@ 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 *); +void ppp_register_notified(struct ppp_notified_t *); +void ppp_unregister_notified(struct ppp_notified_t *); +void ppp_notify_started(struct ppp_t *ppp); +void ppp_notify_finished(struct ppp_t *ppp); + extern int sock_fd; // internet socket for ioctls #endif diff --git a/accel-pptpd/ppp/ppp_notify.c b/accel-pptpd/ppp/ppp_notify.c new file mode 100644 index 00000000..94ceb6d2 --- /dev/null +++ b/accel-pptpd/ppp/ppp_notify.c @@ -0,0 +1,34 @@ +#include "ppp.h" + +static LIST_HEAD(notified_list); + +void __export ppp_register_notified(struct ppp_notified_t *n) +{ + list_add_tail(&n->entry, ¬ified_list); +} + +void __export ppp_unregister_notified(struct ppp_notified_t *n) +{ + list_del(&n->entry); +} + +void ppp_notify_started(struct ppp_t *ppp) +{ + struct ppp_notified_t *n; + + list_for_each_entry(n, ¬ified_list, entry) { + if (n->started) + n->started(n, ppp); + } +} + +void ppp_notify_finished(struct ppp_t *ppp) +{ + struct ppp_notified_t *n; + + list_for_each_entry(n, ¬ified_list, entry) { + if (n->finished) + n->finished(n, ppp); + } +} + diff --git a/accel-pptpd/ppp/ppp_pd.c b/accel-pptpd/ppp/ppp_pd.c new file mode 100644 index 00000000..fe51bc01 --- /dev/null +++ b/accel-pptpd/ppp/ppp_pd.c @@ -0,0 +1,12 @@ +#include "ppp.h" + +int ppp_store_pd(struct ppp_t *ppp, pd_key_t key, void *data) +{ + struct ppp_pd_t *pd; + + list_for_each_entry(pd, &ppp->pd_list, entry) + if (pd->key == key) + return -1; + + +} diff --git a/accel-pptpd/pwdb.c b/accel-pptpd/pwdb.c index 37dd0c5f..8bde8fff 100644 --- a/accel-pptpd/pwdb.c +++ b/accel-pptpd/pwdb.c @@ -1,12 +1,69 @@ +#include + +#include "triton.h" + #include "pwdb.h" -#include "ppp.h" -__export int pwdb_check(struct ppp_t *ppp,const char *username,const char *password) +static LIST_HEAD(pwdb_handlers); + +int __export pwdb_cleartext_check(struct ppp_t *ppp, const char *username,const char *password) +{ + struct pwdb_t *pwdb; + int r = PWDB_NO_IMPL; + + list_for_each_entry(pwdb, &pwdb_handlers, entry) { + if (!pwdb->cleartext_check) + continue; + r = pwdb->cleartext_check(pwdb, ppp, username, password); + if (r == PWDB_NO_IMPL) + continue; + break; + } + + return r; +} +int __export pwdb_encrypted_check(struct ppp_t *ppp, const char *username, int type, ...) +{ + struct pwdb_t *pwdb; + int r = PWDB_NO_IMPL; + va_list args; + + va_start(args, type); + + list_for_each_entry(pwdb, &pwdb_handlers, entry) { + if (!pwdb->encrypted_check) + continue; + r = pwdb->encrypted_check(pwdb, ppp, username, type, args); + if (r == PWDB_NO_IMPL) + continue; + break; + } + + return r; + +} +__export const char *pwdb_get_passwd(struct ppp_t *ppp, const char *username) { - return 0; + struct pwdb_t *pwdb; + const char *r = NULL; + + list_for_each_entry(pwdb, &pwdb_handlers, entry) { + if (!pwdb->get_passwd) + continue; + r = pwdb->get_passwd(pwdb, ppp, username); + if (r) + break; + } + + return r; } -__export char *pwdb_get_passwd(struct ppp_t *ppp, const char *username) +void __export pwdb_register(struct pwdb_t *pwdb) +{ + list_add_tail(&pwdb->entry, &pwdb_handlers); +} +void __export pwdb_unregister(struct pwdb_t *pwdb) { - return strdup("test"); + list_del(&pwdb->entry); } + diff --git a/accel-pptpd/pwdb.h b/accel-pptpd/pwdb.h index 07c45b09..42f9133c 100644 --- a/accel-pptpd/pwdb.h +++ b/accel-pptpd/pwdb.h @@ -1,10 +1,29 @@ #ifndef PWDB_H #define PWDB_H +#include +#include "list.h" + struct ppp_t; -int pwdb_check(struct ppp_t*,const char *username,const char *password); -char *pwdb_get_passwd(struct ppp_t*, const char *username); +#define PWDB_SUCCESS 0 +#define PWDB_DENIED 1 +#define PWDB_NO_IMPL 2 + +struct pwdb_t +{ + struct list_head entry; + int (*cleartext_check)(struct pwdb_t *, struct ppp_t *, const char *username, const char *password); + int (*encrypted_check)(struct pwdb_t *, struct ppp_t *, const char *username, int type, va_list args); + const char* (*get_passwd)(struct pwdb_t *, struct ppp_t *, const char *username); +}; + +int pwdb_cleartext_check(struct ppp_t *, const char *username,const char *password); +int pwdb_encrypted_check(struct ppp_t *, const char *username, int type, ...); +const char *pwdb_get_passwd(struct ppp_t *, const char *username); + +void pwdb_register(struct pwdb_t *); +void pwdb_unregister(struct pwdb_t *); #endif diff --git a/accel-pptpd/radius/CMakeLists.txt b/accel-pptpd/radius/CMakeLists.txt new file mode 100644 index 00000000..a53491f6 --- /dev/null +++ b/accel-pptpd/radius/CMakeLists.txt @@ -0,0 +1,8 @@ +SET(target radius) +SET(sources + radius.c + dict.c +) + +ADD_LIBRARY(radius SHARED ${sources}) + diff --git a/accel-pptpd/radius/dict.c b/accel-pptpd/radius/dict.c new file mode 100644 index 00000000..62a25cfd --- /dev/null +++ b/accel-pptpd/radius/dict.c @@ -0,0 +1,219 @@ +#include +#include +#include +#include + +#include "list.h" +#include "radius.h" +#include "log.h" + + +struct dict_value_t +{ + struct list_head entry; + rad_value_t val; + const char *name; +}; + +struct dict_attr_t +{ + struct list_head entry; + const char *name; + int id; + int type; + rad_value_t val; + struct list_head values; +}; + +static char *skip_word(char *ptr) +{ + for(; *ptr; ptr++) + if (*ptr == ' ' || *ptr == '\t' || *ptr == '\n') + break; + return ptr; +} +static char *skip_space(char *ptr) +{ + for(; *ptr; ptr++) + if (*ptr != ' ' && *ptr != '\t') + break; + return ptr; +} +static int split(char *buf, char **ptr) +{ + int i; + + for (i = 0; i < 3; i++) { + buf = skip_word(buf); + if (!*buf) + return -1; + + *buf = 0; + + buf = skip_space(buf + 1); + if (!*buf) + return -1; + + ptr[i] = buf; + } + + buf = skip_word(buf); + if (*buf == '\n') + *buf = 0; + else if (*buf) + return -1; + + return 0; +} + +struct dict_attr_t *find_attr(struct rad_dict_t *dict, const char *name) +{ + struct dict_attr_t *attr; + + list_for_each_entry(attr, &dict->items, entry) + if (!strcmp(attr->name, name)) + return attr; + + return NULL; +} + +#define BUF_SIZE 1024 +void *rad_load_dict(const char *fname) +{ + FILE *f; + char *buf, *ptr[3], *endptr; + int n = 0; + struct rad_dict_t *dict; + struct dict_attr_t *attr; + struct dict_value_t *val; + + f = fopen(fname, "r"); + if (!f) { + log_error("radius: open dictioanary '%s': %s\n", fname, strerror(errno)); + return NULL; + } + + buf = malloc(BUF_SIZE); + if (!buf) { + log_error("radius: out of memory\n"); + fclose(f); + return NULL; + } + + dict = malloc(sizeof(*dict)); + if (!dict) { + log_error("radius: out of memory\n"); + fclose(f); + free(buf); + return NULL; + } + + INIT_LIST_HEAD(&dict->items); + + while (fgets(buf, BUF_SIZE, f)) { + n++; + if (buf[0] == '#' || buf[0] == '\n' || buf[0] == 0) + continue; + if (split(buf, ptr)) { + log_error("radius:%s:%i: syntaxis error\n", fname, n); + goto out_err; + } + if (!strcmp(buf, "ATTRIBUTE")) { + attr = malloc(sizeof(*attr)); + if (!attr) { + log_error("radius: out of memory\n"); + goto out_err; + } + memset(attr, 0, sizeof(*attr)); + INIT_LIST_HEAD(&attr->values); + list_add_tail(&attr->entry, &dict->items); + attr->name = strdup(ptr[0]); + attr->id = strtol(ptr[1], &endptr, 10); + if (*endptr != 0) { + log_error("radius:%s:%i: syntaxis error\n", fname, n); + goto out_err; + } + if (!strcmp(ptr[2], "integer")) + attr->type = ATTR_TYPE_INTEGER; + else if (!strcmp(ptr[2], "string")) + attr->type = ATTR_TYPE_STRING; + else if (!strcmp(ptr[2], "date")) + attr->type = ATTR_TYPE_DATE; + else if (!strcmp(ptr[2], "ipaddr")) + attr->type = ATTR_TYPE_IPADDR; + else { + log_error("radius:%s:%i: unknown attribute type\n", fname, n); + goto out_err; + } + } else if (!strcmp(buf, "VALUE")) { + attr = find_attr(dict, ptr[0]); + if (!attr) { + log_error("radius:%s:%i: unknown attribute\n", fname, n); + goto out_err; + } + val = malloc(sizeof(*val)); + if (!val) { + log_error("radius: out of memory\n"); + goto out_err; + } + memset(val, 0, sizeof(*val)); + list_add_tail(&val->entry, &attr->values); + val->name = strdup(ptr[1]); + switch (attr->type) { + case ATTR_TYPE_INTEGER: + val->val.integer = strtol(ptr[2], &endptr, 10); + if (*endptr != 0) { + log_error("radius:%s:%i: syntaxis error\n", fname, n); + goto out_err; + } + break; + case ATTR_TYPE_STRING: + val->val.string = strdup(ptr[2]); + break; + case ATTR_TYPE_DATE: + log_warn("radius:%s:%i: VALUE of type 'date' is not implemented yet\n", fname, n); + break; + case ATTR_TYPE_IPADDR: + log_warn("radius:%s:%i: VALUE of type 'ipaddr' is not implemented yet\n", fname, n); + break; + } + } else { + log_error("radius:%s:%i: syntaxis error\n"); + goto out_err; + } + } + + free(buf); + fclose(f); + + return dict; + +out_err: + rad_free_dict(dict); + free(buf); + fclose(f); + return NULL; +} + +void rad_free_dict(struct rad_dict_t *dict) +{ + struct dict_attr_t *attr; + struct dict_value_t *val; + + while (!list_empty(&dict->items)) { + attr = list_entry(dict->items.next, typeof(*attr), entry); + while (!list_empty(&attr->values)) { + val = list_entry(attr->values.next, typeof(*val), entry); + list_del(&val->entry); + free((char*)val->name); + if (attr->type == ATTR_TYPE_STRING) + free((char*)val->val.string); + free(val); + } + list_del(&attr->entry); + free((char*)attr->name); + free(attr); + } + free(dict); +} + diff --git a/accel-pptpd/radius/dictionary b/accel-pptpd/radius/dictionary new file mode 100644 index 00000000..caebe9d7 --- /dev/null +++ b/accel-pptpd/radius/dictionary @@ -0,0 +1,240 @@ +# +# Updated 97/06/13 to livingston-radius-2.01 miquels@cistron.nl +# +# This file contains dictionary translations for parsing +# requests and generating responses. All transactions are +# composed of Attribute/Value Pairs. The value of each attribute +# is specified as one of 4 data types. Valid data types are: +# +# string - 0-253 octets +# ipaddr - 4 octets in network byte order +# integer - 32 bit value in big endian order (high byte first) +# date - 32 bit value in big endian order - seconds since +# 00:00:00 GMT, Jan. 1, 1970 +# +# Enumerated values are stored in the user file with dictionary +# VALUE translations for easy administration. +# +# Example: +# +# ATTRIBUTE VALUE +# --------------- ----- +# Framed-Protocol = PPP +# 7 = 1 (integer encoding) +# + +# +# Following are the proper new names. Use these. +# +ATTRIBUTE User-Name 1 string +ATTRIBUTE Password 2 string +ATTRIBUTE CHAP-Password 3 string +ATTRIBUTE NAS-IP-Address 4 ipaddr +ATTRIBUTE NAS-Port-Id 5 integer +ATTRIBUTE Service-Type 6 integer +ATTRIBUTE Framed-Protocol 7 integer +ATTRIBUTE Framed-IP-Address 8 ipaddr +ATTRIBUTE Framed-IP-Netmask 9 ipaddr +ATTRIBUTE Framed-Routing 10 integer +ATTRIBUTE Filter-Id 11 string +ATTRIBUTE Framed-MTU 12 integer +ATTRIBUTE Framed-Compression 13 integer +ATTRIBUTE Login-IP-Host 14 ipaddr +ATTRIBUTE Login-Service 15 integer +ATTRIBUTE Login-TCP-Port 16 integer +ATTRIBUTE Reply-Message 18 string +ATTRIBUTE Callback-Number 19 string +ATTRIBUTE Callback-Id 20 string +ATTRIBUTE Framed-Route 22 string +ATTRIBUTE Framed-IPX-Network 23 ipaddr +ATTRIBUTE State 24 string +ATTRIBUTE Class 25 string +ATTRIBUTE Vendor-Specific 26 string +ATTRIBUTE Session-Timeout 27 integer +ATTRIBUTE Idle-Timeout 28 integer +ATTRIBUTE Termination-Action 29 integer +ATTRIBUTE Called-Station-Id 30 string +ATTRIBUTE Calling-Station-Id 31 string +ATTRIBUTE NAS-Identifier 32 string +ATTRIBUTE Proxy-State 33 string +ATTRIBUTE Login-LAT-Service 34 string +ATTRIBUTE Login-LAT-Node 35 string +ATTRIBUTE Login-LAT-Group 36 string +ATTRIBUTE Framed-AppleTalk-Link 37 integer +ATTRIBUTE Framed-AppleTalk-Network 38 integer +ATTRIBUTE Framed-AppleTalk-Zone 39 string +ATTRIBUTE Acct-Status-Type 40 integer +ATTRIBUTE Acct-Delay-Time 41 integer +ATTRIBUTE Acct-Input-Octets 42 integer +ATTRIBUTE Acct-Output-Octets 43 integer +ATTRIBUTE Acct-Session-Id 44 string +ATTRIBUTE Acct-Authentic 45 integer +ATTRIBUTE Acct-Session-Time 46 integer +ATTRIBUTE Acct-Input-Packets 47 integer +ATTRIBUTE Acct-Output-Packets 48 integer +ATTRIBUTE Acct-Terminate-Cause 49 integer +ATTRIBUTE Acct-Multi-Session-Id 50 string +ATTRIBUTE Acct-Link-Count 51 integer +ATTRIBUTE Event-Timestamp 55 integer +ATTRIBUTE CHAP-Challenge 60 string +ATTRIBUTE NAS-Port-Type 61 integer +ATTRIBUTE Port-Limit 62 integer +ATTRIBUTE Login-LAT-Port 63 integer +ATTRIBUTE Connect-Info 77 string + +# +# RFC3162 IPv6 attributes +# +ATTRIBUTE NAS-IPv6-Address 95 string +ATTRIBUTE Framed-Interface-Id 96 string +ATTRIBUTE Framed-IPv6-Prefix 97 string +ATTRIBUTE Login-IPv6-Host 98 string +ATTRIBUTE Framed-IPv6-Route 99 string +ATTRIBUTE Framed-IPv6-Pool 100 string + +# +# Experimental Non Protocol Attributes used by Cistron-Radiusd +# +ATTRIBUTE Huntgroup-Name 221 string +ATTRIBUTE User-Category 1029 string +ATTRIBUTE Group-Name 1030 string +ATTRIBUTE Simultaneous-Use 1034 integer +ATTRIBUTE Strip-User-Name 1035 integer +ATTRIBUTE Fall-Through 1036 integer +ATTRIBUTE Add-Port-To-IP-Address 1037 integer +ATTRIBUTE Exec-Program 1038 string +ATTRIBUTE Exec-Program-Wait 1039 string +ATTRIBUTE Hint 1040 string + +# +# Non-Protocol Attributes +# These attributes are used internally by the server +# +ATTRIBUTE Expiration 21 date +ATTRIBUTE Auth-Type 1000 integer +ATTRIBUTE Menu 1001 string +ATTRIBUTE Termination-Menu 1002 string +ATTRIBUTE Prefix 1003 string +ATTRIBUTE Suffix 1004 string +ATTRIBUTE Group 1005 string +ATTRIBUTE Crypt-Password 1006 string +ATTRIBUTE Connect-Rate 1007 integer + +# +# Integer Translations +# + +# User Types + +VALUE Service-Type Login-User 1 +VALUE Service-Type Framed-User 2 +VALUE Service-Type Callback-Login-User 3 +VALUE Service-Type Callback-Framed-User 4 +VALUE Service-Type Outbound-User 5 +VALUE Service-Type Administrative-User 6 +VALUE Service-Type NAS-Prompt-User 7 + +# Framed Protocols + +VALUE Framed-Protocol PPP 1 +VALUE Framed-Protocol SLIP 2 + +# Framed Routing Values + +VALUE Framed-Routing None 0 +VALUE Framed-Routing Broadcast 1 +VALUE Framed-Routing Listen 2 +VALUE Framed-Routing Broadcast-Listen 3 + +# Framed Compression Types + +VALUE Framed-Compression None 0 +VALUE Framed-Compression Van-Jacobson-TCP-IP 1 + +# Login Services + +VALUE Login-Service Telnet 0 +VALUE Login-Service Rlogin 1 +VALUE Login-Service TCP-Clear 2 +VALUE Login-Service PortMaster 3 + +# Status Types + +VALUE Acct-Status-Type Start 1 +VALUE Acct-Status-Type Stop 2 +VALUE Acct-Status-Type Alive 3 +VALUE Acct-Status-Type Accounting-On 7 +VALUE Acct-Status-Type Accounting-Off 8 + +# Authentication Types + +VALUE Acct-Authentic RADIUS 1 +VALUE Acct-Authentic Local 2 +VALUE Acct-Authentic PowerLink128 100 + +# Termination Options + +VALUE Termination-Action Default 0 +VALUE Termination-Action RADIUS-Request 1 + +# NAS Port Types, available in 3.3.1 and later + +VALUE NAS-Port-Type Async 0 +VALUE NAS-Port-Type Sync 1 +VALUE NAS-Port-Type ISDN 2 +VALUE NAS-Port-Type ISDN-V120 3 +VALUE NAS-Port-Type ISDN-V110 4 + +# Acct Terminate Causes, available in 3.3.2 and later + +VALUE Acct-Terminate-Cause User-Request 1 +VALUE Acct-Terminate-Cause Lost-Carrier 2 +VALUE Acct-Terminate-Cause Lost-Service 3 +VALUE Acct-Terminate-Cause Idle-Timeout 4 +VALUE Acct-Terminate-Cause Session-Timeout 5 +VALUE Acct-Terminate-Cause Admin-Reset 6 +VALUE Acct-Terminate-Cause Admin-Reboot 7 +VALUE Acct-Terminate-Cause Port-Error 8 +VALUE Acct-Terminate-Cause NAS-Error 9 +VALUE Acct-Terminate-Cause NAS-Request 10 +VALUE Acct-Terminate-Cause NAS-Reboot 11 +VALUE Acct-Terminate-Cause Port-Unneeded 12 +VALUE Acct-Terminate-Cause Port-Preempted 13 +VALUE Acct-Terminate-Cause Port-Suspended 14 +VALUE Acct-Terminate-Cause Service-Unavailable 15 +VALUE Acct-Terminate-Cause Callback 16 +VALUE Acct-Terminate-Cause User-Error 17 +VALUE Acct-Terminate-Cause Host-Request 18 + +# +# Non-Protocol Integer Translations +# + +VALUE Auth-Type Local 0 +VALUE Auth-Type System 1 +VALUE Auth-Type SecurID 2 +VALUE Auth-Type Crypt-Local 3 +VALUE Auth-Type Reject 4 + +# +# Cistron extensions +# +VALUE Auth-Type Pam 253 +VALUE Auth-Type Accept 254 + +# +# Experimental Non-Protocol Integer Translations for Cistron-Radiusd +# +VALUE Fall-Through No 0 +VALUE Fall-Through Yes 1 +VALUE Add-Port-To-IP-Address No 0 +VALUE Add-Port-To-IP-Address Yes 1 + +# +# Configuration Values +# uncomment these two lines to turn account expiration on +# + +#VALUE Server-Config Password-Expiration 30 +#VALUE Server-Config Password-Warning 5 + diff --git a/accel-pptpd/radius/radius.c b/accel-pptpd/radius/radius.c new file mode 100644 index 00000000..10fef65f --- /dev/null +++ b/accel-pptpd/radius/radius.c @@ -0,0 +1,74 @@ +#include +#include +#include +#include + +#include "ppp.h" +#include "pwdb.h" +#include "radius.h" + +struct radius_pd_t +{ + struct ppp_pd_t pd; + struct ppp_t *ppp; +}; + +static struct ppp_notified_t notified; + +int cleartext_check(struct pwdb_t *pwdb, struct ppp_t *ppp, const char *username, const char *password) +{ + return PWDB_NO_IMPL; +} +int encrypted_check(struct pwdb_t *pwdb, struct ppp_t *ppp, const char *username, int type, va_list args) +{ + return PWDB_NO_IMPL; +} + + +static void ppp_started(struct ppp_notified_t *n, struct ppp_t *ppp) +{ + struct radius_pd_t *pd = malloc(sizeof(*pd)); + + memset(pd, 0, sizeof(*pd)); + pd->pd.key = n; + pd->ppp = ppp; + list_add_tail(&pd->pd.entry, &ppp->pd_list); +} + +static void ppp_finished(struct ppp_notified_t *n, struct ppp_t *ppp) +{ + struct ppp_pd_t *pd; + struct radius_pd_t *rpd; + + list_for_each_entry(pd, &ppp->pd_list, entry) { + if (pd->key == ¬ified) { + rpd = container_of(pd, typeof(*rpd), pd); + list_del(&pd->entry); + free(rpd); + return; + } + } +} + +struct pwdb_t pwdb = { + .cleartext_check = cleartext_check, + .encrypted_check = encrypted_check, +}; + +static struct ppp_notified_t notified = { + .started = ppp_started, + .finished = ppp_finished, +}; + +static void __init radius_init(void) +{ + char *dict = conf_get_opt("radius", "dictionary"); + if (!dict) { + fprintf(stderr, "radius: dictionary not specified\n"); + _exit(EXIT_FAILURE); + } + if (!rad_load_dict(dict)) + _exit(EXIT_FAILURE); + ppp_register_notified(¬ified); +} + diff --git a/accel-pptpd/radius/radius.h b/accel-pptpd/radius/radius.h new file mode 100644 index 00000000..aac1cd00 --- /dev/null +++ b/accel-pptpd/radius/radius.h @@ -0,0 +1,28 @@ +#ifndef __RADIUS_H +#define __RADIUS_H + +#include + +#define ATTR_TYPE_INTEGER 0 +#define ATTR_TYPE_STRING 1 +#define ATTR_TYPE_DATE 2 +#define ATTR_TYPE_IPADDR 3 + +typedef union +{ + int integer; + const char *string; + time_t date; + in_addr_t ipaddr; +} rad_value_t; + +struct rad_dict_t +{ + struct list_head items; +}; + +void *rad_load_dict(const char *fname); +void rad_free_dict(struct rad_dict_t *dict); + +#endif + -- cgit v1.2.3 From 45243dd2f2be49cd91be1dc28932e6c9040db6a1 Mon Sep 17 00:00:00 2001 From: Kozlov Dmitry Date: Mon, 6 Sep 2010 18:27:02 +0400 Subject: working on radius module --- accel-pptpd/accel-pptpd.conf | 4 + accel-pptpd/ctrl/pptp.c | 1 - accel-pptpd/pwdb.c | 31 ++---- accel-pptpd/pwdb.h | 6 +- accel-pptpd/radius/CMakeLists.txt | 2 + accel-pptpd/radius/dict.c | 55 +++++----- accel-pptpd/radius/packet.c | 172 ++++++++++++++++++++++++++++++ accel-pptpd/radius/radius.c | 105 +++++++++++++++--- accel-pptpd/radius/radius.h | 74 ++++++++++++- accel-pptpd/radius/req.c | 216 ++++++++++++++++++++++++++++++++++++++ accel-pptpd/triton/CMakeLists.txt | 1 + accel-pptpd/triton/event.c | 103 ++++++++++++++++++ accel-pptpd/triton/triton.h | 4 + accel-pptpd/triton/triton_p.h | 9 +- 14 files changed, 714 insertions(+), 69 deletions(-) create mode 100644 accel-pptpd/radius/packet.c create mode 100644 accel-pptpd/radius/req.c create mode 100644 accel-pptpd/triton/event.c (limited to 'accel-pptpd/pwdb.c') diff --git a/accel-pptpd/accel-pptpd.conf b/accel-pptpd/accel-pptpd.conf index 9b635340..47480220 100644 --- a/accel-pptpd/accel-pptpd.conf +++ b/accel-pptpd/accel-pptpd.conf @@ -15,3 +15,7 @@ echo-interval=3 [radius] dictionary=dictionary +nas-identifier=pptp +nas-ip-address=192.168.10.20 +auth_server=127.0.0.1:1812,secret +acct_server=127.0.0.1:1812,secret diff --git a/accel-pptpd/ctrl/pptp.c b/accel-pptpd/ctrl/pptp.c index b503d85e..66c09786 100644 --- a/accel-pptpd/ctrl/pptp.c +++ b/accel-pptpd/ctrl/pptp.c @@ -529,7 +529,6 @@ static void __init pptp_init(void) 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; diff --git a/accel-pptpd/pwdb.c b/accel-pptpd/pwdb.c index 8bde8fff..1b682a40 100644 --- a/accel-pptpd/pwdb.c +++ b/accel-pptpd/pwdb.c @@ -6,41 +6,26 @@ static LIST_HEAD(pwdb_handlers); -int __export pwdb_cleartext_check(struct ppp_t *ppp, const char *username,const char *password) +int __export pwdb_check(struct ppp_t *ppp, const char *username, int type, ...) { struct pwdb_t *pwdb; - int r = PWDB_NO_IMPL; - - list_for_each_entry(pwdb, &pwdb_handlers, entry) { - if (!pwdb->cleartext_check) - continue; - r = pwdb->cleartext_check(pwdb, ppp, username, password); - if (r == PWDB_NO_IMPL) - continue; - break; - } - - return r; -} -int __export pwdb_encrypted_check(struct ppp_t *ppp, const char *username, int type, ...) -{ - struct pwdb_t *pwdb; - int r = PWDB_NO_IMPL; + int r, res = PWDB_NO_IMPL; va_list args; va_start(args, type); list_for_each_entry(pwdb, &pwdb_handlers, entry) { - if (!pwdb->encrypted_check) + if (!pwdb->check) continue; - r = pwdb->encrypted_check(pwdb, ppp, username, type, args); + r = pwdb->check(pwdb, ppp, username, type, args); if (r == PWDB_NO_IMPL) continue; - break; + if (r == PWDB_SUCCESS) + return PWDB_SUCCESS; + res = r; } - return r; - + return res; } __export const char *pwdb_get_passwd(struct ppp_t *ppp, const char *username) { diff --git a/accel-pptpd/pwdb.h b/accel-pptpd/pwdb.h index 42f9133c..d3495435 100644 --- a/accel-pptpd/pwdb.h +++ b/accel-pptpd/pwdb.h @@ -13,13 +13,11 @@ struct ppp_t; struct pwdb_t { struct list_head entry; - int (*cleartext_check)(struct pwdb_t *, struct ppp_t *, const char *username, const char *password); - int (*encrypted_check)(struct pwdb_t *, struct ppp_t *, const char *username, int type, va_list args); + int (*check)(struct pwdb_t *, struct ppp_t *, const char *username, int type, va_list args); const char* (*get_passwd)(struct pwdb_t *, struct ppp_t *, const char *username); }; -int pwdb_cleartext_check(struct ppp_t *, const char *username,const char *password); -int pwdb_encrypted_check(struct ppp_t *, const char *username, int type, ...); +int pwdb_check(struct ppp_t *, const char *username, int type, ...); const char *pwdb_get_passwd(struct ppp_t *, const char *username); void pwdb_register(struct pwdb_t *); diff --git a/accel-pptpd/radius/CMakeLists.txt b/accel-pptpd/radius/CMakeLists.txt index a53491f6..17a85784 100644 --- a/accel-pptpd/radius/CMakeLists.txt +++ b/accel-pptpd/radius/CMakeLists.txt @@ -2,6 +2,8 @@ SET(target radius) SET(sources radius.c dict.c + req.c + packet.c ) ADD_LIBRARY(radius SHARED ${sources}) diff --git a/accel-pptpd/radius/dict.c b/accel-pptpd/radius/dict.c index 62a25cfd..992d7d40 100644 --- a/accel-pptpd/radius/dict.c +++ b/accel-pptpd/radius/dict.c @@ -7,23 +7,7 @@ #include "radius.h" #include "log.h" - -struct dict_value_t -{ - struct list_head entry; - rad_value_t val; - const char *name; -}; - -struct dict_attr_t -{ - struct list_head entry; - const char *name; - int id; - int type; - rad_value_t val; - struct list_head values; -}; +static struct rad_dict_t *dict; static char *skip_word(char *ptr) { @@ -78,26 +62,25 @@ struct dict_attr_t *find_attr(struct rad_dict_t *dict, const char *name) } #define BUF_SIZE 1024 -void *rad_load_dict(const char *fname) +int rad_dict_load(const char *fname) { FILE *f; char *buf, *ptr[3], *endptr; int n = 0; - struct rad_dict_t *dict; struct dict_attr_t *attr; struct dict_value_t *val; f = fopen(fname, "r"); if (!f) { log_error("radius: open dictioanary '%s': %s\n", fname, strerror(errno)); - return NULL; + return -1; } buf = malloc(BUF_SIZE); if (!buf) { log_error("radius: out of memory\n"); fclose(f); - return NULL; + return -1; } dict = malloc(sizeof(*dict)); @@ -105,7 +88,7 @@ void *rad_load_dict(const char *fname) log_error("radius: out of memory\n"); fclose(f); free(buf); - return NULL; + return -1; } INIT_LIST_HEAD(&dict->items); @@ -186,16 +169,16 @@ void *rad_load_dict(const char *fname) free(buf); fclose(f); - return dict; + return 0; out_err: - rad_free_dict(dict); + rad_dict_free(dict); free(buf); fclose(f); - return NULL; + return -1; } -void rad_free_dict(struct rad_dict_t *dict) +void rad_dict_free(struct rad_dict_t *dict) { struct dict_attr_t *attr; struct dict_value_t *val; @@ -217,3 +200,23 @@ void rad_free_dict(struct rad_dict_t *dict) free(dict); } +struct rad_dict_attr_t *rad_dict_find_attr(const char *name) +{ + struct rad_dict_attr_t *attr; + + list_for_each_entry(attr, &dict->items, entry) + if (!strcmp(attr->name, name)) + return attr; + + return NULL; +} +struct rad_dict_value_t *rad_dict_find_val(struct rad_dict_attr_t *attr, const char *name) +{ + struct rad_dict_value_t *val; + + list_for_each_entry(val, &attr->values, entry) + if (!strcmp(val->name, name)) + return val; + + return NULL; +} diff --git a/accel-pptpd/radius/packet.c b/accel-pptpd/radius/packet.c new file mode 100644 index 00000000..a22a611e --- /dev/null +++ b/accel-pptpd/radius/packet.c @@ -0,0 +1,172 @@ +#include +#include +#include +#include +#include + +#include "radius.h" + +static int urandom_fd; + +int rad_packet_build(struct rad_packet_t *pack) +{ + struct rad_req_attr_t *attr; + uint8_t *ptr; + + ptr = malloc(pack->len); + if (!ptr) { + log_error("radius:packet: out of memory\n"); + return -1; + } + + *ptr = pack->code; ptr++; + *ptr = pack->id; ptr++; + *(uint16_t*)ptr = pack->len; pt r+= 2; + while (1) { + if (read(erandom_fd, ptr, 16) != 16) { + if (errno == EINTR) + continue; + log_error("radius:packet:read urandom: %s\n", strerror(errno)); + goto out_err; + } + break; + } + ptr+=16; + + list_for_each_entry(attr, &pack->attrs, entry) { + *ptr = attr->attr.id; ptr++; + *ptr = attr->len; ptr++; + switch(attr->attr.type) { + case ATTR_TYPE_INTEGER: + *(uint32_t*)ptr = attr->val.integer; + break; + case ATTR_TYPE_STRING: + memcpy(ptr, attr->val.string); + break; + case ATTR_TYPE_IPADDR: + *(in_addr_t*)ptr = attr->val.ipaddr; + break; + case ATTR_TYPE_DATE: + *(uint32_t*)ptr = attr->val.date; + break; + default: + log_error("radius:packet: unknown attribute type\n"); + abort(); + } + ptr += attr->len; + } + + return 0; +} + +struct rad_packet_t *rad_packet_recv(int fd) +{ + struct rad_packet_t *pack; + struct rad_req_attr_t *attr; + struct rad_dict_attr_t *da; + uint8_t *ptr; + int n, type, len; + + pack = malloc(sizeof(*pack)); + if (!pack) { + log_error("radius:packet: out of memory\n"); + return NULL; + } + + memset(pack, 0, sizeof(*pack)); + INIT_LIST_HEAD(&pack->attrs); + + pack->buf = malloc(REQ_MAX_LENGTH); + if (!pack->buf) { + log_error("radius:packet: out of memory\n"); + free(pack); + return NULL; + } + + while (1) { + n = read(fd, pack->buf, REQ_MAX_LENGTH); + if (n < 0) { + if (errno == EINTR) + continue; + log_error("radius:packet:read: %s\n", strerror(errno)); + goto out_err; + } + break; + } + + if (n < 20) { + log_warn("radius:packet: short packed received (%i)\n", n); + goto out_err; + } + + ptr = (uint8_t *)pack->buf; + + pack->code = *ptr; ptr++; + pack->id = *ptr; ptr++; + pack->len = *(uint16_t*)ptr; ptr += 2; + + if (pack->len > n) { + log_warn("radius:packet: short packet received %i, expected %i\n", pack->len, n); + goto out_err; + } + + ptr += 16; + n -= 20; + + while (n>0) { + type = *ptr; ptr++; + len = *ptr; ptr++; + if (2 + len > n) { + log_error("radius:packet: too long attribute received (%i, %i)\n", type, len); + goto out_err; + } + da = rad_dict_find_attr_type(n); + if (da) { + attr = malloc(sizeof(*attr)); + if (!attr) { + log_error("radius:packet: out of memory\n"); + goto out_err; + } + attr->attr = da; + attr->type = type; + attr->len = len; + if (type == ATTR_TYPE_STRING) { + attr->val.string = malloc(len); + if (!attr->val.string) { + log_error("radius:packet: out of memory\n"); + free(attr); + goto out_err; + } + } else + memcpy(&attr->type.integer, ptr, 4); + list_add_tail(&attr->entry, &pack->attrs); + } else + log_warn("radius:packet: unknown attribute type received (%i)\n", type); + ptr += len; + n -= 2 + len; + } + + return pack; + +out_err: + rad_packet_free(pack); + return NULL; +} + +void rad_packet_free(struct rad_packet_t *pack) +{ + struct rad_req_attr_t *attr; + + if (pack->buf) + free(pack->buf); + + while(!list_empty(&pack->attrs)) { + attr = list_entry(pack->attrs.next, typeof(*attr), entry); + if (attr->attr.type == ATTR_TYPE_STRING) + free(attr->val.string); + list_del(&attr->entry); + free(attr); + } + + free(pack); +} diff --git a/accel-pptpd/radius/radius.c b/accel-pptpd/radius/radius.c index 10fef65f..4d020d5b 100644 --- a/accel-pptpd/radius/radius.c +++ b/accel-pptpd/radius/radius.c @@ -7,23 +7,105 @@ #include "pwdb.h" #include "radius.h" -struct radius_pd_t +static struct ppp_notified_t notified; + +static int check_pap(struct radius_pd_t *rpd, const char *username, va_list args) { - struct ppp_pd_t pd; - struct ppp_t *ppp; -}; + struct rad_req_t *req; + int i, r = PWDB_DENIED; + int id = va_arg(args, int); + const char *passwd = va_arg(args, const char *); -static struct ppp_notified_t notified; + req = rad_req_alloc(rpd, CODE_ACCESS_REQUEST); + if (!req) + return PWDB_DENIED; + + if (rad_req_add_str(req, "User-Password", passwd, strlen(passwd))) + goto out; + + for(i = 0; i < max_try; i++) { + if (rad_req_send(req)) + goto out; + + if (rad_req_wait(req, timeout)) + goto out; + + if (req->answer) + break; + } + +out: + rad_req_free(req); + + return r; +} + +static int check_chap_md5(struct radius_pd_t *rpd, const char *username, va_list args) +{ + int id = va_arg(args, int); + const uint8_t *challenge = va_arg(args, const uint8_t *); +} -int cleartext_check(struct pwdb_t *pwdb, struct ppp_t *ppp, const char *username, const char *password) +static int check_mschap_v1(struct radius_pd_t *rpd, const char *username, va_list args) { - return PWDB_NO_IMPL; + int id = va_arg(args, int); + const uint8_t *challenge = va_arg(args, const uint8_t *); + const uint8_t *lm_response = va_arg(args, const uint8_t *); + const uint8_t *nt_response = va_arg(args, const uint8_t *); + int flags = va_arg(args, int); } -int encrypted_check(struct pwdb_t *pwdb, struct ppp_t *ppp, const char *username, int type, va_list args) + +static int check_mschap_v2(struct radius_pd_t *rpd, const char *username, va_list args) { - return PWDB_NO_IMPL; + int id = va_arg(args, int); + const uint8_t *challenge = va_arg(args, const uint8_t *); + const uint8_t *peer_challenge = va_arg(args, const uint8_t *); + const uint8_t *response = va_arg(args, const uint8_t *); + int flags = va_arg(args, int); + uint8_t *authenticator = va_arg(args, uint8_t *); } +static int check(struct pwdb_t *pwdb, struct ppp_t *ppp, const char *username, int type, va_list _args) +{ + int r = PWDB_NO_IMPL; + va_list args; + int chap_type; + struct ppp_pd_t *pd; + struct radius_pd_t *rpd = NULL; + + list_for_each_entry(pd, &ppp->pd_list, entry) { + if (pd->key == ¬ified) { + rpd = container_of(pd, typeof(*rpd), pd); + break; + } + } + + va_copy(args, _args); + + switch(type) { + case PPP_PAP: + r = check_pap(rpd, username, args); + break; + case PPP_CHAP: + chap_type = va_arg(args, int); + switch(chap_type) { + case 0x05: + r = check_chap_md5(rpd, username, args); + break; + case 0x80: + r = check_mschap_v1(rpd, username, args); + break; + case 0x81: + r = check_mschap_v2(rpd, username, args); + break; + } + break; + } + + va_end(args); + + return r; +} static void ppp_started(struct ppp_notified_t *n, struct ppp_t *ppp) { @@ -50,9 +132,8 @@ static void ppp_finished(struct ppp_notified_t *n, struct ppp_t *ppp) } } -struct pwdb_t pwdb = { - .cleartext_check = cleartext_check, - .encrypted_check = encrypted_check, +static struct pwdb_t pwdb = { + .check = check, }; static struct ppp_notified_t notified = { diff --git a/accel-pptpd/radius/radius.h b/accel-pptpd/radius/radius.h index aac1cd00..3fe78621 100644 --- a/accel-pptpd/radius/radius.h +++ b/accel-pptpd/radius/radius.h @@ -2,12 +2,23 @@ #define __RADIUS_H #include +#include "triton.h" + +#define REQ_LENGTH_MAX 4096 #define ATTR_TYPE_INTEGER 0 #define ATTR_TYPE_STRING 1 #define ATTR_TYPE_DATE 2 #define ATTR_TYPE_IPADDR 3 +#define CODE_ACCESS_REQUEST 1 + +struct radius_pd_t +{ + struct ppp_pd_t pd; + struct ppp_t *ppp; +}; + typedef union { int integer; @@ -21,8 +32,67 @@ struct rad_dict_t struct list_head items; }; -void *rad_load_dict(const char *fname); -void rad_free_dict(struct rad_dict_t *dict); +struct rad_dict_value_t +{ + struct list_head entry; + rad_value_t val; + const char *name; +}; + +struct rad_dict_attr_t +{ + struct list_head entry; + const char *name; + int id; + int type; + struct list_head values; +}; + +struct rad_req_attr_t +{ + struct list_head entry; + struct rad_dict_attr_t *attr; + //struct rad_dict_value_t *val; + rad_value_t val; +}; + +struct rad_packet_t +{ + int code; + int id; + struct list_head attrs; + void *buf; +}; +struct rad_req_t +{ + struct triton_md_handler_t hnd; + struct triton_timer_t timeout; + struct rad_packet_t pack; + struct rad_packet_t *answer; + const char *server_host; + int server_port; + + struct radius_pd_t *rpd; +}; + + +int rad_dict_load(const char *fname); +void rad_dict_free(struct rad_dict_t *dict); +struct rad_dict_attr_t *rad_dict_find_attr(const char *name); +struct rad_dict_attr_t *rad_dict_find_attr_type(int type); +struct rad_dict_value_t *rad_dict_find_val(struct rad_dict_attr_t *, const char *name); + +struct rad_req_t *rad_rec_alloc(struct radius_pd_t *rpd, int code); +void rad_rec_free(struct rad_req_t *); +int rad_req_send(struct rad_req_t *); +int rad_req_add_int(struct rad_req_t *req, const char *name, int val); +int rad_req_add_val(struct rad_req_t *req, const char *name, const char *val, int len); +int rad_req_add_str(struct rad_req_t *req, const char *name, const char *val, int len); + +int rad_packet_build(struct rad_packet_t *pack); +struct rad_packet_t *rad_packet_recv(int fd); +void rad_packet_free(struct rad_packet_t *); + #endif diff --git a/accel-pptpd/radius/req.c b/accel-pptpd/radius/req.c new file mode 100644 index 00000000..d03ea9ae --- /dev/null +++ b/accel-pptpd/radius/req.c @@ -0,0 +1,216 @@ +#include +#include + +#include "triton.h" +#include "radius.h" + + +static int rad_req_read(struct triton_md_handler_t *h); +static void rd_req_timeout(struct triton_timer_t *t); + +struct rad_req_t *rad_rec_alloc(struct radius_pd_t *rpd, int code, const char *username); +{ + struct rad_req_t *req = malloc(sizeof(*req)); + + if (!req) + return NULL; + + memset(req, 0, sizeof(*req)); + INIT_LIST_HEAD(&req->pack.attrs); + req->rpd = rpd; + req->pack.code = code; + req->pack.len = 20; + req->hnd.fd = -1; + req->hnd.read = rad_req_read; + req->hnd.timeout.exoire = rad_req_timeout; + + if (rad_req_add_str(req, "User-Name", username, strlen(username))) + goto out_err; + if (conf_nas_identifier) + if (rad_req_add_str(req, "NAS-Identifier", conf_nas_identifier, strlen(conf_nas_identifier))) + goto out_err; + if (rad_req_add_int(req, "NAS-Port-Id", rpd->ppp->unit_idx, 4)) + goto out_err; + if (rad_req_add_str(req, "NAS-Port-Type", "Sync", 4)) + goto out_err; + if (rad_req_add_str(req, "Service-Type", "Framed-User", 11)) + goto out_err; + if (rad_req_add_str(req, "Framed-Protocol", "PPP", 3)) + goto out_err; + + return req; +} + +void rad_rec_free(struct rad_req_t *) +{ + +} + +int rad_req_send(struct rad_req_t *req) +{ + struct sockaddr_in addr; + int n; + + if (req->hnd.fd == -1) { + req->hnd.fd = socket(PF_INET, SOCK_DGRAM ,0); + if (!req->hnd.fd) { + log_error("radius:socket: %s\n", strerror(errno)); + return -1; + } + + if (conf_nas_ip_address) { + addr.sin_family = AF_INET; + addr.sin_addr.s_addr = htonl(conf_nas_ip_address); + if (bind(req->hnd.fd, (struct sockaddr *) &addr, sizeof(addr))) { + log_error("radius:bind: %s\n", strerror(errno)); + goto out_err; + } + } + + addr.sin_addr.s_addr = htonl(req->server_name); + addr.sin_port = htons(req->server_port); + + if (connect(req->hnd.fd (struct sockaddr *) &addr, sizeof(addr))) { + log_error("radius:connect: %s\n", strerror(errno)); + goto out_err; + } + + if (fcntl(req->hnd.fd, F_SETFL, O_NONBLOCK)) { + log_error("radius: failed to set nonblocking mode: %s\n", strerror(errno)); + goto out_err; + } + + if (rad_packet_build(&req->pack)) + goto out_err; + } + + while (1) { + n = write(req->hnd.fd, req->pack.buf, req->pack.len); + if (n < 0) { + if (errno == EINTR) + continue; + log_error("radius:write: %s\n", strerror(errno)); + goto out_err_free_pack; + } else if (n != req->pack.len) { + log_error("radius:write: short write %i, excpected %i\n", n, req->pack.len); + goto out_err_free_pack; + } + break; + } + + return 0; + +out_err_free_pack: + rad_packet_free(&req->pack); +out_err: + close(req->hnd.fd); + req->hnd.fd = -1; + return -1; +} + +int rad_req_add_int(struct rad_req_t *req, const char *name, int val) +{ + struct rad_req_attr_t *ra; + struct rad_dict_attr_t *attr; + + if (req->len + 2 + 4 >= REQ_LENGTH_MAX) + return -1; + + attr = rad_dict_find_attr(name); + if (!attr) + return -1; + + ra = malloc(sizeof(*ra)); + if (!ra) + return -1; + + ra->attr = attr; + ra->val.integer = val; + list_add_tail(&ra->entry, &req->pack.attrs); + req->len += 2 + 4; + + return 0; +} + +int rad_req_add_str(struct rad_req_t *req, const char *name, const char *val, int len) +{ + struct rad_req_attr_t *ra; + struct rad_dict_attr_t *attr; + + if (req->len + 2 + len >= REQ_LENGTH_MAX) + return -1; + + attr = rad_dict_find_attr(name); + if (!attr) + return -1; + + ra = malloc(sizeof(*ra)); + if (!ra) + return -1; + + ra->attr = attr; + ra->val.string = stdrdup(val); + list_add_tail(&ra->entry, &req->pack.attrs); + req->len += 2 + len; + + return 0; +} + +int rad_req_add_val(struct rad_req_t *req, const char *name, const char *val, int len) +{ + struct rad_req_attr_t *ra; + struct rad_dict_attr_t *attr; + struct rad_dict_value_t *v; + + if (req->len + 2 + len >= REQ_LENGTH_MAX) + return -1; + + attr = rad_dict_find_attr(name); + if (!attr) + return -1; + + v = rad_dict_find_val(attr, val); + if (!v) + return -1; + + ra = malloc(sizeof(*ra)); + if (!ra) + return -1; + + ra->attr = attr; + ra->val = v->val; + list_add_tail(&ra->entry, &req->attrs.pack); + req->len += 2 + len; + + return 0; +} + +static int rad_req_read(struct triton_md_handler_t *h) +{ + struct rad_req_t *req = container_of(h, typeof(*req), hnd); + + req->answer = rad_packet_recv(h->hnd.fd); +} +static void rd_req_timeout(struct triton_timer_t *t) +{ +} + +int rad_req_wait(struct rad_req_t *req) +{ + if (triton_md_register_handler(req->rpd->ppp->ctrl->ctx, &req->hnd)) + return -1; + if (triton_md_enable_handler(&req->hnd, MD_MODE_READ)) + return -1; + + req->timeout.period = conf_timeout * 1000; + if (triton_timer_add(&req->timeout)) + return -1; + + triton_ctx_schedule(&req->hnd, &req->timeout); + + triton_timer_del(&req->timeout); + triton_md_unregister_handler(&req->hnd); + + return 0; +} + diff --git a/accel-pptpd/triton/CMakeLists.txt b/accel-pptpd/triton/CMakeLists.txt index fd6b5182..41339d97 100644 --- a/accel-pptpd/triton/CMakeLists.txt +++ b/accel-pptpd/triton/CMakeLists.txt @@ -7,6 +7,7 @@ SET(sources_c loader.c log.c mempool.c + event.c ) INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}) ADD_DEFINITIONS("-DUSE_SPINLOCK") diff --git a/accel-pptpd/triton/event.c b/accel-pptpd/triton/event.c new file mode 100644 index 00000000..17483ec7 --- /dev/null +++ b/accel-pptpd/triton/event.c @@ -0,0 +1,103 @@ +#include +#include +#include + +#include "triton_p.h" + +static int max_events = 1024; +static struct _triton_event_t **events; + +struct event_handler_t +{ + struct list_head entry; + triton_event_func func; +}; + +int event_init(void) +{ + events = malloc(max_events * sizeof(void *)); + if (!events) { + fprintf(stderr,"event:cann't allocate memory\n"); + return -1; + } + + memset(events, 0, max_events * sizeof(void *)); + + return 0; +} + +int triton_event_register_handler(int ev_id, triton_event_func func) +{ + struct _triton_event_t *ev; + struct event_handler_t *h; + + if (ev_id >= max_events) + return -1; + + ev = events[ev_id]; + if (!ev) { + ev = malloc(sizeof(*ev)); + if (!ev) { + triton_log_error("event: out of memory\n"); + return -1; + } + INIT_LIST_HEAD(&ev->handlers); + events[ev_id] = ev; + } + + h = malloc(sizeof(*h)); + if (!h) { + triton_log_error("event: out of memory\n"); + return -1; + } + + h->func = func; + list_add_tail(&h->entry, &ev->handlers); + + return 0; +} + +/*int triton_event_unregister_handler(int ev_id, triton_event_func func) +{ + struct _triton_event_t *ev; + struct event_handler_t *h; + + if (ev_id >= max_events) + return -1; + + ev = events[ev_id]; + if (!ev) { + return -1; + } + + list_for_each_entry(h, &ev->handlers, entry) { + if (h->func == func) { + if (ev->in_progress) + h->func = NULL; + else { + list_del(&h->entry); + free(h); + } + return 0; + } + } + + return -1; +}*/ + +void triton_event_fire(int ev_id, void *arg) +{ + struct _triton_event_t *ev; + struct event_handler_t *h; + + if (ev_id >= max_events) + return; + + ev = events[ev_id]; + if (!ev) + return; + + list_for_each_entry(h, &ev->handlers, entry) + h->func(arg); +} + diff --git a/accel-pptpd/triton/triton.h b/accel-pptpd/triton/triton.h index 1009d67a..d130d86e 100644 --- a/accel-pptpd/triton/triton.h +++ b/accel-pptpd/triton/triton.h @@ -55,6 +55,10 @@ int triton_timer_add(struct triton_ctx_t *ctx, struct triton_timer_t*,int abs_ti int triton_timer_mod(struct triton_timer_t *,int abs_time); void triton_timer_del(struct triton_timer_t *); +typedef void (*triton_event_func)(void *); +int triton_event_register_handler(int ev_id, triton_event_func func); +void triton_event_fire(int ev_id, void *arg); + struct conf_sect_t *conf_get_section(const char *name); char *conf_get_opt(const char *sect, const char *name); diff --git a/accel-pptpd/triton/triton_p.h b/accel-pptpd/triton/triton_p.h index 0f7e7160..c31efd6e 100644 --- a/accel-pptpd/triton/triton_p.h +++ b/accel-pptpd/triton/triton_p.h @@ -57,6 +57,11 @@ struct _triton_timer_t struct triton_timer_t *ud; }; +struct _triton_event_t +{ + struct list_head handlers; +}; + typedef void * mempool_t; mempool_t *mempool_create(int size); void *mempool_alloc(mempool_t*); @@ -64,9 +69,11 @@ void mempool_free(void*); int log_init(void); int md_init(); +int timer_init(); +int event_init(); + void md_run(); void md_terminate(); -int timer_init(); void timer_run(); void timer_terminate(); struct triton_ctx_t *default_ctx; -- cgit v1.2.3 From 4c6469a9fd820db713251a645ac2499782f796ed Mon Sep 17 00:00:00 2001 From: Kozlov Dmitry Date: Wed, 8 Sep 2010 15:51:29 +0400 Subject: radius: implemented packet exchange radius: implemented PAP authorization radius: implemented IP assigning triton: implemented userspace context switching and other stuff --- accel-pptpd/accel-pptpd.conf | 13 ++- accel-pptpd/auth/auth_chap_md5.c | 4 +- accel-pptpd/auth/auth_mschap_v1.c | 4 +- accel-pptpd/auth/auth_mschap_v2.c | 4 +- accel-pptpd/auth/auth_pap.c | 25 +++-- accel-pptpd/ctrl/pptp.c | 14 +-- accel-pptpd/ipdb.c | 25 ++++- accel-pptpd/ipdb.h | 15 ++- accel-pptpd/ppp/ipcp_opt_ipaddr.c | 10 +- accel-pptpd/ppp/ppp.c | 18 +++- accel-pptpd/ppp/ppp.h | 6 +- accel-pptpd/ppp/ppp_auth.c | 6 +- accel-pptpd/ppp/ppp_ccp.c | 29 +++--- accel-pptpd/ppp/ppp_fsm.c | 14 ++- accel-pptpd/ppp/ppp_fsm.h | 6 +- accel-pptpd/ppp/ppp_ipcp.c | 53 ++++++---- accel-pptpd/ppp/ppp_ipcp.h | 1 + accel-pptpd/ppp/ppp_lcp.c | 35 +++++-- accel-pptpd/ppp/ppp_lcp.h | 1 + accel-pptpd/pwdb.c | 4 +- accel-pptpd/pwdb.h | 4 +- accel-pptpd/radius/dict.c | 26 ++++- accel-pptpd/radius/packet.c | 169 ++++++++++++++++++++++--------- accel-pptpd/radius/radius.c | 202 ++++++++++++++++++++++++++++++++++---- accel-pptpd/radius/radius.h | 26 +++-- accel-pptpd/radius/req.c | 145 ++++++++++++++++++--------- accel-pptpd/triton/md.c | 6 +- accel-pptpd/triton/timer.c | 6 +- accel-pptpd/triton/triton.c | 172 ++++++++++++++++++++++---------- accel-pptpd/triton/triton.h | 17 ++-- accel-pptpd/triton/triton_p.h | 27 +++-- 31 files changed, 803 insertions(+), 284 deletions(-) (limited to 'accel-pptpd/pwdb.c') diff --git a/accel-pptpd/accel-pptpd.conf b/accel-pptpd/accel-pptpd.conf index 47480220..ece04f2f 100644 --- a/accel-pptpd/accel-pptpd.conf +++ b/accel-pptpd/accel-pptpd.conf @@ -1,11 +1,14 @@ [modules] ./libpptp.so -./libauth_mschap_v2.so +./libauth_pap.so ./libradius.so [core] log-error=/dev/stderr +[ppp] +verbose=1 + [lcp] echo-interval=10 echo-failure=3 @@ -16,6 +19,8 @@ echo-interval=3 [radius] dictionary=dictionary nas-identifier=pptp -nas-ip-address=192.168.10.20 -auth_server=127.0.0.1:1812,secret -acct_server=127.0.0.1:1812,secret +nas-ip-address=127.0.0.1 +gw-ip-address=192.168.100.100 +auth_server=127.0.0.1:1812,testing123 +acct_server=127.0.0.1:1813,secret +verbose=1 diff --git a/accel-pptpd/auth/auth_chap_md5.c b/accel-pptpd/auth/auth_chap_md5.c index 5577794f..de2f4716 100644 --- a/accel-pptpd/auth/auth_chap_md5.c +++ b/accel-pptpd/auth/auth_chap_md5.c @@ -220,8 +220,8 @@ static void chap_recv_response(struct chap_auth_data_t *ad, struct chap_hdr_t *h ppp_terminate(ad->ppp, 0); } - name=strndup(msg->name,ntohs(msg->hdr.len)-sizeof(*msg)+2); - passwd=pwdb_get_passwd(ad->ppp,name); + name = strndup(msg->name,ntohs(msg->hdr.len)-sizeof(*msg)+2); + passwd = pwdb_get_passwd(ad->ppp,name); if (!passwd) { free(name); diff --git a/accel-pptpd/auth/auth_mschap_v1.c b/accel-pptpd/auth/auth_mschap_v1.c index 1cf5eb8b..595fb15e 100644 --- a/accel-pptpd/auth/auth_mschap_v1.c +++ b/accel-pptpd/auth/auth_mschap_v1.c @@ -282,8 +282,8 @@ static int chap_check_response(struct chap_auth_data_t *ad, struct chap_response char *name; int i; - name=strndup(msg->name,ntohs(msg->hdr.len)-sizeof(*msg)+2); - passwd=pwdb_get_passwd(ad->ppp,name); + name = strndup(msg->name,ntohs(msg->hdr.len)-sizeof(*msg)+2); + passwd = pwdb_get_passwd(ad->ppp,name); if (!passwd) { free(name); diff --git a/accel-pptpd/auth/auth_mschap_v2.c b/accel-pptpd/auth/auth_mschap_v2.c index 502f3686..01127cf7 100644 --- a/accel-pptpd/auth/auth_mschap_v2.c +++ b/accel-pptpd/auth/auth_mschap_v2.c @@ -193,8 +193,8 @@ static int generate_response(struct chap_auth_data_t *ad, struct chap_response_t 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); + name = strndup(msg->name,ntohs(msg->hdr.len)-sizeof(*msg)+2); + passwd = pwdb_get_passwd(ad->ppp,name); if (!passwd) { free(name); diff --git a/accel-pptpd/auth/auth_pap.c b/accel-pptpd/auth/auth_pap.c index 92ddef52..7337468a 100644 --- a/accel-pptpd/auth/auth_pap.c +++ b/accel-pptpd/auth/auth_pap.c @@ -125,7 +125,7 @@ static void pap_send_ack(struct pap_auth_data_t *p, int id) ppp_chan_send(p->ppp,msg,ntohs(msg->hdr.len)+2); } -static void pap_send_nak(struct pap_auth_data_t *p,int id) +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; @@ -143,9 +143,10 @@ static void pap_send_nak(struct pap_auth_data_t *p,int id) static int pap_recv_req(struct pap_auth_data_t *p,struct pap_hdr_t *hdr) { - int ret; + int ret, r; char *peer_id; char *passwd; + const char *passwd2; int peer_id_len; int passwd_len; uint8_t *ptr=(uint8_t*)(hdr+1); @@ -170,17 +171,23 @@ static int pap_recv_req(struct pap_auth_data_t *p,struct pap_hdr_t *hdr) 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)) - { + r = pwdb_check(p->ppp, peer_id, PPP_PAP, passwd); + if (r == PWDB_NO_IMPL) { + passwd2 = pwdb_get_passwd(p->ppp, peer_id); + if (!passwd2 || strcmp(passwd2, passwd)) + r = PWDB_DENIED; + else + r = PWDB_SUCCESS; + } + if (r == PWDB_DENIED) { log_warn("PAP: authentication error\n"); - pap_send_nak(p,hdr->id); + pap_send_nak(p, hdr->id); auth_failed(p->ppp); ret=-1; - }else - { - pap_send_ack(p,hdr->id); + } else { + pap_send_ack(p, hdr->id); auth_successed(p->ppp); - ret=0; + ret = 0; } free(peer_id); diff --git a/accel-pptpd/ctrl/pptp.c b/accel-pptpd/ctrl/pptp.c index 66c09786..3a3c2427 100644 --- a/accel-pptpd/ctrl/pptp.c +++ b/accel-pptpd/ctrl/pptp.c @@ -27,7 +27,7 @@ struct pptp_conn_t { - struct triton_ctx_t ctx; + struct triton_context_t ctx; struct triton_md_handler_t hnd; struct triton_timer_t timeout_timer; struct triton_timer_t echo_timer; @@ -73,7 +73,7 @@ static void disconnect(struct pptp_conn_t *conn) ppp_terminate(&conn->ppp, 1); } - triton_unregister_ctx(&conn->ctx); + triton_context_unregister(&conn->ctx); free(conn->in_buf); free(conn->out_buf); @@ -425,7 +425,7 @@ 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) +static void pptp_close(struct triton_context_t *ctx) { struct pptp_conn_t *conn = container_of(ctx, typeof(*conn), ctx); if (conn->state == STATE_PPP) { @@ -455,7 +455,7 @@ static void ppp_finished(struct ppp_t *ppp) struct pptp_serv_t { - struct triton_ctx_t ctx; + struct triton_context_t ctx; struct triton_md_handler_t hnd; }; @@ -495,14 +495,14 @@ static int pptp_connect(struct triton_md_handler_t *h) conn->timeout_timer.period = conf_timeout * 1000; conn->echo_timer.expire = pptp_send_echo; - triton_register_ctx(&conn->ctx); + triton_context_register(&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) +static void pptp_serv_close(struct triton_context_t *ctx) { struct pptp_serv_t *s=container_of(ctx,typeof(*s),ctx); triton_md_unregister_handler(&s->hnd); @@ -546,7 +546,7 @@ static void __init pptp_init(void) return; } - triton_register_ctx(&serv.ctx); + triton_context_register(&serv.ctx); triton_md_register_handler(&serv.ctx, &serv.hnd); triton_md_enable_handler(&serv.hnd, MD_MODE_READ); diff --git a/accel-pptpd/ipdb.c b/accel-pptpd/ipdb.c index 8ac86cb6..7b679dc7 100644 --- a/accel-pptpd/ipdb.c +++ b/accel-pptpd/ipdb.c @@ -1,11 +1,28 @@ #include "triton.h" #include "ipdb.h" -int __export ipdb_get(in_addr_t *addr, in_addr_t *peer_addr) +static LIST_HEAD(ipdb_handlers); + +int __export ipdb_get(struct ppp_t *ppp, in_addr_t *addr, in_addr_t *peer_addr) { - *addr=inet_addr("192.168.200.100"); - *peer_addr=inet_addr("192.168.200.200"); + struct ipdb_t *ipdb; + + list_for_each_entry(ipdb, &ipdb_handlers, entry) + if (!ipdb->get(ppp, addr, peer_addr)) + return 0; - return 0; + return -1; } +void __export ipdb_put(struct ppp_t *ppp, in_addr_t addr, in_addr_t peer_addr) +{ + struct ipdb_t *ipdb; + list_for_each_entry(ipdb, &ipdb_handlers, entry) + if (ipdb->put) + ipdb->put(ppp, addr, peer_addr); +} + +void __export ipdb_register(struct ipdb_t *ipdb) +{ + list_add_tail(&ipdb->entry, &ipdb_handlers); +} diff --git a/accel-pptpd/ipdb.h b/accel-pptpd/ipdb.h index ed7df6b3..6703a48b 100644 --- a/accel-pptpd/ipdb.h +++ b/accel-pptpd/ipdb.h @@ -3,7 +3,20 @@ #include -int ipdb_get(in_addr_t *addr, in_addr_t *peer_addr); +#include "ppp.h" +#include "list.h" + +struct ipdb_t +{ + struct list_head entry; + int (*get)(struct ppp_t *ppp, in_addr_t *addr, in_addr_t *peer_addr); + void (*put)(struct ppp_t *ppp, in_addr_t addr, in_addr_t peer_addr); +}; + +int ipdb_get(struct ppp_t *ppp, in_addr_t *addr, in_addr_t *peer_addr); +void ipdb_put(struct ppp_t *ppp, in_addr_t addr, in_addr_t peer_addr); + +void ipdb_register(struct ipdb_t *); #endif diff --git a/accel-pptpd/ppp/ipcp_opt_ipaddr.c b/accel-pptpd/ppp/ipcp_opt_ipaddr.c index 2fa92519..ddfb3d20 100644 --- a/accel-pptpd/ppp/ipcp_opt_ipaddr.c +++ b/accel-pptpd/ppp/ipcp_opt_ipaddr.c @@ -40,7 +40,6 @@ 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; @@ -51,6 +50,9 @@ 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); + if (ipaddr_opt->peer_addr) + ipdb_put(ipcp->ppp, ipaddr_opt->addr, ipaddr_opt->peer_addr); + free(ipaddr_opt); } @@ -58,6 +60,12 @@ static int ipaddr_send_conf_req(struct ppp_ipcp_t *ipcp, struct ipcp_option_t *o { struct ipaddr_option_t *ipaddr_opt=container_of(opt,typeof(*ipaddr_opt),opt); struct ipcp_opt32_t *opt32=(struct ipcp_opt32_t*)ptr; + + if (!ipaddr_opt->addr && ipdb_get(ipcp->ppp, &ipaddr_opt->addr, &ipaddr_opt->peer_addr)) { + log_warn("ppp:ipcp: no free IP address\n"); + return -1; + } + opt32->hdr.id=CI_ADDR; opt32->hdr.len=6; opt32->val=ipaddr_opt->addr; diff --git a/accel-pptpd/ppp/ppp.c b/accel-pptpd/ppp/ppp.c index 4ad8221b..247f8198 100644 --- a/accel-pptpd/ppp/ppp.c +++ b/accel-pptpd/ppp/ppp.c @@ -17,6 +17,8 @@ #include "ppp_fsm.h" #include "log.h" +int conf_ppp_verbose; + static LIST_HEAD(layers); int sock_fd; @@ -302,7 +304,10 @@ void __export ppp_layer_started(struct ppp_t *ppp, struct ppp_layer_data_t *d) list_for_each_entry(d,&n->items,entry) { d->starting=1; - d->layer->start(d); + if (d->layer->start(d)) { + ppp_terminate(ppp, 0); + return; + } } } } @@ -464,7 +469,10 @@ static void start_first_layer(struct ppp_t *ppp) list_for_each_entry(d,&n->items,entry) { d->starting=1; - d->layer->start(d); + if (d->layer->start(d)) { + ppp_terminate(ppp, 0); + return; + } } } @@ -487,9 +495,15 @@ struct ppp_layer_data_t *ppp_find_layer_data(struct ppp_t *ppp, struct ppp_layer static void __init ppp_init(void) { + char *opt; + sock_fd = socket(AF_INET, SOCK_DGRAM, 0); if (sock_fd < 0) { perror("socket"); _exit(EXIT_FAILURE); } + + opt = conf_get_opt("ppp", "verbose"); + if (opt && atoi(opt) > 0) + conf_ppp_verbose = 1; } diff --git a/accel-pptpd/ppp/ppp.h b/accel-pptpd/ppp/ppp.h index da2fb97c..e287b7b1 100644 --- a/accel-pptpd/ppp/ppp.h +++ b/accel-pptpd/ppp/ppp.h @@ -45,7 +45,7 @@ struct ppp_t; struct ppp_ctrl_t { - struct triton_ctx_t *ctx; + struct triton_context_t *ctx; void (*started)(struct ppp_t*); void (*finished)(struct ppp_t*); }; @@ -111,7 +111,7 @@ struct ppp_layer_t { struct list_head entry; struct ppp_layer_data_t *(*init)(struct ppp_t *); - void (*start)(struct ppp_layer_data_t*); + int (*start)(struct ppp_layer_data_t*); void (*finish)(struct ppp_layer_data_t*); void (*free)(struct ppp_layer_data_t *); }; @@ -146,5 +146,7 @@ void ppp_unregister_notified(struct ppp_notified_t *); void ppp_notify_started(struct ppp_t *ppp); void ppp_notify_finished(struct ppp_t *ppp); +extern int conf_ppp_verbose; + 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 index 32f63d5b..b767fdbd 100644 --- a/accel-pptpd/ppp/ppp_auth.c +++ b/accel-pptpd/ppp/ppp_auth.c @@ -22,7 +22,7 @@ static int auth_recv_conf_ack(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, u 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 int 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 *); @@ -257,7 +257,7 @@ static struct ppp_layer_data_t *auth_layer_init(struct ppp_t *ppp) return &ad->ld; } -static void auth_layer_start(struct ppp_layer_data_t *ld) +static int auth_layer_start(struct ppp_layer_data_t *ld) { struct auth_layer_data_t *ad=container_of(ld,typeof(*ad),ld); @@ -270,6 +270,8 @@ static void auth_layer_start(struct ppp_layer_data_t *ld) log_debug("auth_layer_started\n"); ppp_layer_started(ad->ppp,ld); } + + return 0; } static void auth_layer_finish(struct ppp_layer_data_t *ld) diff --git a/accel-pptpd/ppp/ppp_ccp.c b/accel-pptpd/ppp/ppp_ccp.c index fe10886e..f895da9b 100644 --- a/accel-pptpd/ppp/ppp_ccp.c +++ b/accel-pptpd/ppp/ppp_ccp.c @@ -24,7 +24,7 @@ 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 int 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*); @@ -90,7 +90,7 @@ static struct ppp_layer_data_t *ccp_layer_init(struct ppp_t *ppp) return &ccp->ld; } -void ccp_layer_start(struct ppp_layer_data_t *ld) +int ccp_layer_start(struct ppp_layer_data_t *ld) { struct ppp_ccp_t *ccp=container_of(ld,typeof(*ccp),ld); @@ -98,7 +98,10 @@ void ccp_layer_start(struct ppp_layer_data_t *ld) ccp_options_init(ccp); ppp_fsm_lower_up(&ccp->fsm); - ppp_fsm_open(&ccp->fsm); + if (ppp_fsm_open(&ccp->fsm)) + return -1; + + return 0; } void ccp_layer_finish(struct ppp_layer_data_t *ld) @@ -107,12 +110,8 @@ void ccp_layer_finish(struct ppp_layer_data_t *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); + ccp->fsm.fsm_state = FSM_Closed; + ppp_layer_finished(ccp->ppp,&ccp->ld); } void ccp_layer_free(struct ppp_layer_data_t *ld) @@ -121,6 +120,10 @@ void ccp_layer_free(struct ppp_layer_data_t *ld) log_debug("ccp_layer_free\n"); + ppp_unregister_handler(ccp->ppp,&ccp->hnd); + ccp_options_free(ccp); + ppp_fsm_free(&ccp->fsm); + free(ccp); } @@ -151,7 +154,7 @@ static void print_ropt(struct recv_opt_t *ropt) log_debug(" >"); } -static void send_conf_req(struct ppp_fsm_t *fsm) +static int 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; @@ -171,6 +174,8 @@ static void send_conf_req(struct ppp_fsm_t *fsm) list_for_each_entry(lopt,&ccp->options,entry) { n=lopt->h->send_conf_req(ccp,lopt,ptr); + if (n < 0) + return -1; if (n) { log_debug(" "); @@ -183,6 +188,8 @@ static void send_conf_req(struct ppp_fsm_t *fsm) ccp_hdr->len=htons((ptr-buf)-2); ppp_unit_send(ccp->ppp,ccp_hdr,ptr-buf); + + return 0; } static void send_conf_ack(struct ppp_fsm_t *fsm) @@ -473,7 +480,7 @@ static void ccp_recv(struct ppp_handler_t*h) if (ccp->fsm.fsm_state==FSM_Initial || ccp->fsm.fsm_state==FSM_Closed) { - log_error("CCP: discaring packet\n"); + log_warn("CCP: discaring packet\n"); return; } diff --git a/accel-pptpd/ppp/ppp_fsm.c b/accel-pptpd/ppp/ppp_fsm.c index 85cf15a2..a99453f0 100644 --- a/accel-pptpd/ppp/ppp_fsm.c +++ b/accel-pptpd/ppp/ppp_fsm.c @@ -38,7 +38,7 @@ void ppp_fsm_free(struct ppp_fsm_t *layer) stop_timer(layer); } -void ppp_fsm_lower_up(struct ppp_fsm_t *layer) +int ppp_fsm_lower_up(struct ppp_fsm_t *layer) { switch(layer->fsm_state) { @@ -49,12 +49,15 @@ void ppp_fsm_lower_up(struct ppp_fsm_t *layer) //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); + if (layer->send_conf_req) + if (layer->send_conf_req(layer)) + return -1; layer->fsm_state=FSM_Req_Sent; break; default: break; } + return 0; } void ppp_fsm_lower_down(struct ppp_fsm_t *layer) @@ -84,7 +87,7 @@ void ppp_fsm_lower_down(struct ppp_fsm_t *layer) } } -void ppp_fsm_open(struct ppp_fsm_t *layer) +int ppp_fsm_open(struct ppp_fsm_t *layer) { switch(layer->fsm_state) { @@ -98,7 +101,9 @@ void ppp_fsm_open(struct ppp_fsm_t *layer) //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); + if (layer->send_conf_req) + if (layer->send_conf_req(layer)) + return -1; layer->fsm_state=FSM_Req_Sent; break; case FSM_Closing: @@ -111,6 +116,7 @@ void ppp_fsm_open(struct ppp_fsm_t *layer) default: break; } + return 0; } void ppp_fsm_close(struct ppp_fsm_t *layer) diff --git a/accel-pptpd/ppp/ppp_fsm.h b/accel-pptpd/ppp/ppp_fsm.h index bc958fe9..908936db 100644 --- a/accel-pptpd/ppp/ppp_fsm.h +++ b/accel-pptpd/ppp/ppp_fsm.h @@ -37,7 +37,7 @@ 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*); + int (*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*); @@ -46,9 +46,9 @@ 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*); +int 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*); +int 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); diff --git a/accel-pptpd/ppp/ppp_ipcp.c b/accel-pptpd/ppp/ppp_ipcp.c index 6ac145b2..bb6369e0 100644 --- a/accel-pptpd/ppp/ppp_ipcp.c +++ b/accel-pptpd/ppp/ppp_ipcp.c @@ -24,7 +24,7 @@ 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 int 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*); @@ -90,7 +90,7 @@ static struct ppp_layer_data_t *ipcp_layer_init(struct ppp_t *ppp) return &ipcp->ld; } -void ipcp_layer_start(struct ppp_layer_data_t *ld) +int ipcp_layer_start(struct ppp_layer_data_t *ld) { struct ppp_ipcp_t *ipcp=container_of(ld,typeof(*ipcp),ld); @@ -98,7 +98,10 @@ void ipcp_layer_start(struct ppp_layer_data_t *ld) ipcp_options_init(ipcp); ppp_fsm_lower_up(&ipcp->fsm); - ppp_fsm_open(&ipcp->fsm); + if (ppp_fsm_open(&ipcp->fsm)) + return -1; + + return 0; } void ipcp_layer_finish(struct ppp_layer_data_t *ld) @@ -107,12 +110,8 @@ void ipcp_layer_finish(struct ppp_layer_data_t *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); + ipcp->fsm.fsm_state = FSM_Closed; + ppp_layer_finished(ipcp->ppp,&ipcp->ld); } void ipcp_layer_free(struct ppp_layer_data_t *ld) @@ -121,6 +120,10 @@ void ipcp_layer_free(struct ppp_layer_data_t *ld) log_debug("ipcp_layer_free\n"); + ppp_unregister_handler(ipcp->ppp,&ipcp->hnd); + ipcp_options_free(ipcp); + ppp_fsm_free(&ipcp->fsm); + free(ipcp); } @@ -151,7 +154,7 @@ static void print_ropt(struct recv_opt_t *ropt) log_debug(" >"); } -static void send_conf_req(struct ppp_fsm_t *fsm) +static int 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; @@ -159,30 +162,40 @@ static void send_conf_req(struct ppp_fsm_t *fsm) 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); + n = lopt->h->send_conf_req(ipcp, lopt, ptr); + if (n < 0) + return -1; + if (n) { ptr+=n; + lopt->print = 1; + } else + lopt->print = 0; + } + + if (conf_ppp_verbose) { + log_debug("send [IPCP ConfReq id=%x", ipcp_hdr->id); + list_for_each_entry(lopt,&ipcp->options,entry){ + if (lopt->print) { + log_debug(" "); + lopt->h->print(log_debug,lopt,NULL); + } } + log_debug("]\n"); } - - log_debug("]\n"); ipcp_hdr->len=htons((ptr-buf)-2); ppp_unit_send(ipcp->ppp,ipcp_hdr,ptr-buf); + + return 0; } static void send_conf_ack(struct ppp_fsm_t *fsm) @@ -464,7 +477,7 @@ static void ipcp_recv(struct ppp_handler_t*h) if (ipcp->fsm.fsm_state==FSM_Initial || ipcp->fsm.fsm_state==FSM_Closed) { - log_error("IPCP: discaring packet\n"); + log_warn("IPCP: discaring packet\n"); return; } diff --git a/accel-pptpd/ppp/ppp_ipcp.h b/accel-pptpd/ppp/ppp_ipcp.h index 9e0c99ee..266ab7df 100644 --- a/accel-pptpd/ppp/ppp_ipcp.h +++ b/accel-pptpd/ppp/ppp_ipcp.h @@ -56,6 +56,7 @@ struct ipcp_option_t int id; int len; int state; + int print:1; struct ipcp_option_handler_t *h; }; diff --git a/accel-pptpd/ppp/ppp_lcp.c b/accel-pptpd/ppp/ppp_lcp.c index afcfc102..22e6313d 100644 --- a/accel-pptpd/ppp/ppp_lcp.c +++ b/accel-pptpd/ppp/ppp_lcp.c @@ -27,7 +27,7 @@ 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 int 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*); @@ -95,7 +95,7 @@ static struct ppp_layer_data_t *lcp_layer_init(struct ppp_t *ppp) return &lcp->ld; } -void lcp_layer_start(struct ppp_layer_data_t *ld) +int lcp_layer_start(struct ppp_layer_data_t *ld) { struct ppp_lcp_t *lcp=container_of(ld,typeof(*lcp),ld); @@ -103,7 +103,10 @@ void lcp_layer_start(struct ppp_layer_data_t *ld) lcp_options_init(lcp); ppp_fsm_lower_up(&lcp->fsm); - ppp_fsm_open(&lcp->fsm); + if (ppp_fsm_open(&lcp->fsm)) + return -1; + + return 0; } void lcp_layer_finish(struct ppp_layer_data_t *ld) @@ -161,7 +164,7 @@ static void print_ropt(struct recv_opt_t *ropt) log_debug(" >"); } -static void send_conf_req(struct ppp_fsm_t *fsm) +static int 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; @@ -169,30 +172,42 @@ static void send_conf_req(struct ppp_fsm_t *fsm) 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 < 0) + return -1; if (n) { - log_debug(" "); - lopt->h->print(log_debug,lopt,NULL); ptr+=n; - } + lopt->print = 1; + } else + lopt->print = 0; } - log_debug("]\n"); + if (conf_ppp_verbose) { + log_debug("send [LCP ConfReq id=%x", lcp_hdr->id); + list_for_each_entry(lopt,&lcp->options,entry) + { + if (lopt->print) { + log_debug(" "); + lopt->h->print(log_debug,lopt,NULL); + } + } + log_debug("]\n"); + } lcp_hdr->len=htons((ptr-buf)-2); ppp_chan_send(lcp->ppp,lcp_hdr,ptr-buf); + + return 0; } static void send_conf_ack(struct ppp_fsm_t *fsm) diff --git a/accel-pptpd/ppp/ppp_lcp.h b/accel-pptpd/ppp/ppp_lcp.h index 46bc17b9..661f200c 100644 --- a/accel-pptpd/ppp/ppp_lcp.h +++ b/accel-pptpd/ppp/ppp_lcp.h @@ -90,6 +90,7 @@ struct lcp_option_t int id; int len; int state; + int print:1; struct lcp_option_handler_t *h; }; diff --git a/accel-pptpd/pwdb.c b/accel-pptpd/pwdb.c index 1b682a40..24515ec6 100644 --- a/accel-pptpd/pwdb.c +++ b/accel-pptpd/pwdb.c @@ -27,10 +27,10 @@ int __export pwdb_check(struct ppp_t *ppp, const char *username, int type, ...) return res; } -__export const char *pwdb_get_passwd(struct ppp_t *ppp, const char *username) +__export char *pwdb_get_passwd(struct ppp_t *ppp, const char *username) { struct pwdb_t *pwdb; - const char *r = NULL; + char *r = NULL; list_for_each_entry(pwdb, &pwdb_handlers, entry) { if (!pwdb->get_passwd) diff --git a/accel-pptpd/pwdb.h b/accel-pptpd/pwdb.h index d3495435..a1c2cedb 100644 --- a/accel-pptpd/pwdb.h +++ b/accel-pptpd/pwdb.h @@ -14,11 +14,11 @@ struct pwdb_t { struct list_head entry; int (*check)(struct pwdb_t *, struct ppp_t *, const char *username, int type, va_list args); - const char* (*get_passwd)(struct pwdb_t *, struct ppp_t *, const char *username); + char* (*get_passwd)(struct pwdb_t *, struct ppp_t *, const char *username); }; int pwdb_check(struct ppp_t *, const char *username, int type, ...); -const char *pwdb_get_passwd(struct ppp_t *, const char *username); +char *pwdb_get_passwd(struct ppp_t *, const char *username); void pwdb_register(struct pwdb_t *); void pwdb_unregister(struct pwdb_t *); diff --git a/accel-pptpd/radius/dict.c b/accel-pptpd/radius/dict.c index 00f67af7..d76c2c39 100644 --- a/accel-pptpd/radius/dict.c +++ b/accel-pptpd/radius/dict.c @@ -210,7 +210,17 @@ struct rad_dict_attr_t *rad_dict_find_attr(const char *name) return NULL; } -struct rad_dict_value_t *rad_dict_find_val(struct rad_dict_attr_t *attr, const char *name) +struct rad_dict_attr_t *rad_dict_find_attr_id(int id) +{ + struct rad_dict_attr_t *attr; + + list_for_each_entry(attr, &dict->items, entry) + if (attr->id == id) + return attr; + + return NULL; +} +struct rad_dict_value_t *rad_dict_find_val_name(struct rad_dict_attr_t *attr, const char *name) { struct rad_dict_value_t *val; @@ -220,3 +230,17 @@ struct rad_dict_value_t *rad_dict_find_val(struct rad_dict_attr_t *attr, const c return NULL; } + +struct rad_dict_value_t *rad_dict_find_val(struct rad_dict_attr_t *attr, rad_value_t v) +{ + struct rad_dict_value_t *val; + + if (attr->type != ATTR_TYPE_INTEGER) + return NULL; + + list_for_each_entry(val, &attr->values, entry) + if (val->val.integer == v.integer) + return val; + + return NULL; +} diff --git a/accel-pptpd/radius/packet.c b/accel-pptpd/radius/packet.c index f1b4ebea..627b6c5e 100644 --- a/accel-pptpd/radius/packet.c +++ b/accel-pptpd/radius/packet.c @@ -4,14 +4,40 @@ #include #include #include +#include #include "log.h" #include "radius.h" -static int urandom_fd; +struct rad_packet_t *rad_packet_alloc(int code) +{ + struct rad_packet_t *pack; + + pack = malloc(sizeof(*pack)); + if (!pack) { + log_error("radius:packet: out of memory\n"); + return NULL; + } + + memset(pack, 0, sizeof(*pack)); + pack->code = code; + pack->len = 20; + pack->id = 1; + INIT_LIST_HEAD(&pack->attrs); + + return pack; +} -int rad_packet_build(struct rad_packet_t *pack) +void print_buf(uint8_t *buf,int size) +{ + int i; + for(i=0;ibuf = ptr; *ptr = pack->code; ptr++; *ptr = pack->id; ptr++; - *(uint16_t*)ptr = pack->len; ptr+= 2; - while (1) { - if (read(urandom_fd, ptr, 16) != 16) { - if (errno == EINTR) - continue; - log_error("radius:packet:read urandom: %s\n", strerror(errno)); - goto out_err; - } - break; - } - ptr+=16; + *(uint16_t*)ptr = htons(pack->len); ptr+= 2; + memcpy(ptr, RA, 16); ptr+=16; list_for_each_entry(attr, &pack->attrs, entry) { - *ptr = attr->attr->type; ptr++; - *ptr = attr->len; ptr++; + *ptr = attr->attr->id; ptr++; + *ptr = attr->len + 2; ptr++; switch(attr->attr->type) { case ATTR_TYPE_INTEGER: - *(uint32_t*)ptr = attr->val.integer; + *(uint32_t*)ptr = htonl(attr->val.integer); break; case ATTR_TYPE_STRING: memcpy(ptr, attr->val.string, attr->len); @@ -50,20 +68,18 @@ int rad_packet_build(struct rad_packet_t *pack) *(in_addr_t*)ptr = attr->val.ipaddr; break; case ATTR_TYPE_DATE: - *(uint32_t*)ptr = attr->val.date; + *(uint32_t*)ptr = htonl(attr->val.date); break; default: - log_error("radius:packet: unknown attribute type\n"); + log_error("radius:packet:BUG: unknown attribute type\n"); abort(); } ptr += attr->len; } - return 0; + print_buf(pack->buf, pack->len); -out_err: - free(ptr); - return -1; + return 0; } struct rad_packet_t *rad_packet_recv(int fd) @@ -72,22 +88,16 @@ struct rad_packet_t *rad_packet_recv(int fd) struct rad_req_attr_t *attr; struct rad_dict_attr_t *da; uint8_t *ptr; - int n, type, len; - - pack = malloc(sizeof(*pack)); - if (!pack) { - log_error("radius:packet: out of memory\n"); - return NULL; - } + int n, id, len; - memset(pack, 0, sizeof(*pack)); - INIT_LIST_HEAD(&pack->attrs); + pack = rad_packet_alloc(0); + if (!pack) + return NULL; pack->buf = malloc(REQ_LENGTH_MAX); if (!pack->buf) { log_error("radius:packet: out of memory\n"); - free(pack); - return NULL; + goto out_err; } while (1) { @@ -110,7 +120,7 @@ struct rad_packet_t *rad_packet_recv(int fd) pack->code = *ptr; ptr++; pack->id = *ptr; ptr++; - pack->len = *(uint16_t*)ptr; ptr += 2; + pack->len = ntohs(*(uint16_t*)ptr); ptr += 2; if (pack->len > n) { log_warn("radius:packet: short packet received %i, expected %i\n", pack->len, n); @@ -121,13 +131,17 @@ struct rad_packet_t *rad_packet_recv(int fd) n -= 20; while (n>0) { - type = *ptr; ptr++; - len = *ptr; ptr++; + id = *ptr; ptr++; + len = *ptr - 2; ptr++; + if (len < 0) { + log_warn("radius:packet short attribute len received\n"); + goto out_err; + } if (2 + len > n) { - log_error("radius:packet: too long attribute received (%i, %i)\n", type, len); + log_warn("radius:packet: too long attribute received (%i, %i)\n", id, len); goto out_err; } - da = rad_dict_find_attr_type(type); + da = rad_dict_find_attr_id(id); if (da) { attr = malloc(sizeof(*attr)); if (!attr) { @@ -136,18 +150,28 @@ struct rad_packet_t *rad_packet_recv(int fd) } attr->attr = da; attr->len = len; - if (type == ATTR_TYPE_STRING) { - attr->val.string = malloc(len); - if (!attr->val.string) { - log_error("radius:packet: out of memory\n"); - free(attr); - goto out_err; - } - } else - memcpy(&attr->val.integer, ptr, 4); + switch (da->type) { + case ATTR_TYPE_STRING: + attr->val.string = malloc(len+1); + if (!attr->val.string) { + log_error("radius:packet: out of memory\n"); + free(attr); + goto out_err; + } + memcpy(attr->val.string, ptr, len); + attr->val.string[len] = 0; + break; + case ATTR_TYPE_DATE: + case ATTR_TYPE_INTEGER: + attr->val.integer = ntohl(*(uint32_t*)ptr); + break; + case ATTR_TYPE_IPADDR: + attr->val.integer = *(uint32_t*)ptr; + break; + } list_add_tail(&attr->entry, &pack->attrs); } else - log_warn("radius:packet: unknown attribute type received (%i)\n", type); + log_warn("radius:packet: unknown attribute received (%i)\n", id); ptr += len; n -= 2 + len; } @@ -176,3 +200,52 @@ void rad_packet_free(struct rad_packet_t *pack) free(pack); } + +void rad_packet_print(struct rad_packet_t *pack, void (*print)(const char *fmt, ...)) +{ + struct rad_req_attr_t *attr; + struct rad_dict_value_t *val; + + print("[RADIUS "); + switch(pack->code) { + case CODE_ACCESS_REQUEST: + print("Access-Request"); + break; + case CODE_ACCESS_CHALLENGE: + print("Access-Challenge"); + break; + case CODE_ACCESS_ACCEPT: + print("Access-Accept"); + break; + case CODE_ACCESS_REJECT: + print("Access-Reject"); + break; + default: + print("Unknown (%i)", pack->code); + } + print(" id=%x", pack->id); + + list_for_each_entry(attr, &pack->attrs, entry) { + print(" <%s ", attr->attr->name); + if (attr->printable) { + switch (attr->attr->type) { + case ATTR_TYPE_INTEGER: + val = rad_dict_find_val(attr->attr, attr->val); + if (val) + print("%s", val->name); + else + print("%i", attr->val.integer); + break; + case ATTR_TYPE_STRING: + print("\"%s\"", attr->val.string); + break; + case ATTR_TYPE_IPADDR: + print("%i.%i.%i.%i", attr->val.ipaddr & 0xff, (attr->val.ipaddr >> 8) & 0xff, (attr->val.ipaddr >> 16) & 0xff, (attr->val.ipaddr >> 24) & 0xff); + break; + } + } + print(">"); + } + print("]\n"); +} + diff --git a/accel-pptpd/radius/radius.c b/accel-pptpd/radius/radius.c index 2e444ded..a786dea5 100644 --- a/accel-pptpd/radius/radius.c +++ b/accel-pptpd/radius/radius.c @@ -3,43 +3,124 @@ #include #include #include +#include +#include + +#include "log.h" #include "ppp.h" #include "pwdb.h" +#include "ipdb.h" + #include "radius.h" int conf_max_try = 3; int conf_timeout = 3; char *conf_nas_identifier = "accel-pptpd"; char *conf_nas_ip_address; +char *conf_gw_ip_address; +int conf_verbose = 0; + +char *conf_auth_server; +int conf_auth_server_port = 1812; +char *conf_auth_server_secret; + +char *conf_acct_server; +int conf_acct_server_port = 1813; +char *conf_acct_server_secret; static struct ppp_notified_t notified; +static struct radius_pd_t *find_pd(struct ppp_t *ppp); + +static void proc_attrs(struct rad_req_t *req) +{ + struct rad_req_attr_t *attr; + + list_for_each_entry(attr, &req->reply->attrs, entry) { + if (!strcmp(attr->attr->name, "Framed-IP-Address")) { + req->rpd->ipaddr = attr->val.ipaddr; + } + } +} + +static uint8_t* encrypt_password(const char *passwd, const char *secret, const uint8_t *RA, int *epasswd_len) +{ + uint8_t *epasswd; + int i, j, chunk_cnt; + uint8_t b[16], c[16]; + MD5_CTX ctx; + + chunk_cnt = (strlen(passwd) - 1) / 16 + 1; + + epasswd = malloc(chunk_cnt * 16); + if (!epasswd) { + log_error("radius: out of memory\n"); + return NULL; + } + + memset(epasswd, 0, chunk_cnt * 16); + memcpy(epasswd, passwd, strlen(passwd)); + memcpy(c, RA, 16); + + for (i = 0; i < chunk_cnt; i++) { + MD5_Init(&ctx); + MD5_Update(&ctx, secret, strlen(secret)); + MD5_Update(&ctx, c, 16); + MD5_Final(b, &ctx); + + for(j = 0; j < 16; j++) + epasswd[i * 16 + j] ^= b[j]; + + memcpy(c, epasswd + i * 16, 16); + } + + *epasswd_len = chunk_cnt * 16; + return epasswd; +} + static int check_pap(struct radius_pd_t *rpd, const char *username, va_list args) { struct rad_req_t *req; int i, r = PWDB_DENIED; //int id = va_arg(args, int); const char *passwd = va_arg(args, const char *); + uint8_t *epasswd; + int epasswd_len; req = rad_req_alloc(rpd, CODE_ACCESS_REQUEST, username); if (!req) return PWDB_DENIED; + + req->server_name = conf_auth_server; + req->server_port = conf_auth_server_port; - if (rad_req_add_str(req, "User-Password", passwd, strlen(passwd))) + epasswd = encrypt_password(passwd, conf_auth_server_secret, req->RA, &epasswd_len); + if (!epasswd) goto out; + if (rad_req_add_str(req, "Password", (char*)epasswd, epasswd_len, 0)) { + free(epasswd); + goto out; + } + + free(epasswd); + for(i = 0; i < conf_max_try; i++) { if (rad_req_send(req)) goto out; - if (rad_req_wait(req, conf_timeout)) - goto out; + rad_req_wait(req, conf_timeout); if (req->reply) break; } + if (req->reply && req->reply->code == CODE_ACCESS_ACCEPT) { + proc_attrs(req); + r = PWDB_SUCCESS; + } + out: rad_req_free(req); @@ -79,15 +160,7 @@ static int check(struct pwdb_t *pwdb, struct ppp_t *ppp, const char *username, i int r = PWDB_NO_IMPL; va_list args; int chap_type; - struct ppp_pd_t *pd; - struct radius_pd_t *rpd = NULL; - - list_for_each_entry(pd, &ppp->pd_list, entry) { - if (pd->key == ¬ified) { - rpd = container_of(pd, typeof(*rpd), pd); - break; - } - } + struct radius_pd_t *rpd = find_pd(ppp); va_copy(args, _args); @@ -116,6 +189,22 @@ static int check(struct pwdb_t *pwdb, struct ppp_t *ppp, const char *username, i return r; } +static int get_ip(struct ppp_t *ppp, in_addr_t *addr, in_addr_t *peer_addr) +{ + struct radius_pd_t *rpd = find_pd(ppp); + + if (rpd->ipaddr) { + if (!conf_gw_ip_address) { + log_warn("radius: gw-ip-address not specified, cann't assign IP address...\n"); + return -1; + } + *peer_addr = rpd->ipaddr; + *addr = inet_addr(conf_gw_ip_address); + return 0; + } + return -1; +} + static void ppp_started(struct ppp_notified_t *n, struct ppp_t *ppp) { struct radius_pd_t *pd = malloc(sizeof(*pd)); @@ -127,6 +216,14 @@ static void ppp_started(struct ppp_notified_t *n, struct ppp_t *ppp) } static void ppp_finished(struct ppp_notified_t *n, struct ppp_t *ppp) +{ + struct radius_pd_t *rpd = find_pd(ppp); + + list_del(&rpd->pd.entry); + free(rpd); +} + +static struct radius_pd_t *find_pd(struct ppp_t *ppp) { struct ppp_pd_t *pd; struct radius_pd_t *rpd; @@ -134,13 +231,18 @@ static void ppp_finished(struct ppp_notified_t *n, struct ppp_t *ppp) list_for_each_entry(pd, &ppp->pd_list, entry) { if (pd->key == ¬ified) { rpd = container_of(pd, typeof(*rpd), pd); - list_del(&pd->entry); - free(rpd); - return; + return rpd; } } + log_error("radius:BUG: rpd not found\n"); + abort(); } + +static struct ipdb_t ipdb = { + .get = get_ip, +}; + static struct pwdb_t pwdb = { .check = check, }; @@ -150,17 +252,81 @@ static struct ppp_notified_t notified = { .finished = ppp_finished, }; +static int parse_server(const char *opt, char **name, int *port, char **secret) +{ + char *str = strdup(opt); + char *p1, *p2; + + p1 = strstr(str, ":"); + p2 = strstr(str, ","); + + if (p1) + *p1 = 0; + if (p2) + *p2 = 0; + else + return -1; + + *name = str; + if (p1) { + *port = atoi(p1 + 1); + if (*port <=0 ) + return -1; + } + *secret = p2 + 1; + + return 0; +} + static void __init radius_init(void) { - char *dict = conf_get_opt("radius", "dictionary"); - if (!dict) { + char *opt; + + opt = conf_get_opt("radius", "max-try"); + if (opt && atoi(opt) > 0) + conf_max_try = atoi(opt); + + opt = conf_get_opt("radius", "timeout"); + if (opt && atoi(opt) > 0) + conf_timeout = atoi(opt); + + opt = conf_get_opt("radius", "verbose"); + if (opt && atoi(opt) > 0) + conf_verbose = 1; + + opt = conf_get_opt("radius", "nas-ip-address"); + if (opt) + conf_nas_ip_address = opt; + + opt = conf_get_opt("radius", "gw-ip-address"); + if (opt) + conf_gw_ip_address = opt; + + opt = conf_get_opt("radius", "auth_server"); + if (!opt) { + log_error("radius: auth_server not specified\n"); + _exit(EXIT_FAILURE); + } else if (parse_server(opt, &conf_auth_server, &conf_auth_server_port, &conf_auth_server_secret)) { + log_error("radius: failed to parse auth_server\n"); + _exit(EXIT_FAILURE); + } + + opt = conf_get_opt("radius", "acct_server"); + if (opt && parse_server(opt, &conf_acct_server, &conf_acct_server_port, &conf_acct_server_secret)) { + log_error("radius: failed to parse acct_server\n"); + _exit(EXIT_FAILURE); + } + + opt = conf_get_opt("radius", "dictionary"); + if (!opt) { fprintf(stderr, "radius: dictionary not specified\n"); _exit(EXIT_FAILURE); } - if (!rad_dict_load(dict)) + if (rad_dict_load(opt)) _exit(EXIT_FAILURE); pwdb_register(&pwdb); + ipdb_register(&ipdb); ppp_register_notified(¬ified); } diff --git a/accel-pptpd/radius/radius.h b/accel-pptpd/radius/radius.h index 109981a6..40b83201 100644 --- a/accel-pptpd/radius/radius.h +++ b/accel-pptpd/radius/radius.h @@ -1,6 +1,7 @@ #ifndef __RADIUS_H #define __RADIUS_H +#include #include #include "triton.h" #include "ppp.h" @@ -13,17 +14,21 @@ #define ATTR_TYPE_IPADDR 3 #define CODE_ACCESS_REQUEST 1 +#define CODE_ACCESS_ACCEPT 2 +#define CODE_ACCESS_REJECT 3 +#define CODE_ACCESS_CHALLENGE 11 struct radius_pd_t { struct ppp_pd_t pd; struct ppp_t *ppp; + in_addr_t ipaddr; }; typedef union { int integer; - const char *string; + char *string; time_t date; in_addr_t ipaddr; } rad_value_t; @@ -56,6 +61,7 @@ struct rad_req_attr_t //struct rad_dict_value_t *val; rad_value_t val; int len; + int printable:1; }; struct rad_packet_t @@ -68,9 +74,11 @@ struct rad_packet_t }; struct rad_req_t { + struct triton_context_t ctx; struct triton_md_handler_t hnd; struct triton_timer_t timeout; - struct rad_packet_t pack; + uint8_t RA[16]; + struct rad_packet_t *pack; struct rad_packet_t *reply; const char *server_name; int server_port; @@ -81,14 +89,18 @@ struct rad_req_t extern int conf_max_try; extern int conf_timeout; +extern int conf_verbose; extern char *conf_nas_identifier; extern char *conf_nas_ip_address; +extern char *conf_auth_server; +extern char *conf_acct_server; int rad_dict_load(const char *fname); void rad_dict_free(struct rad_dict_t *dict); struct rad_dict_attr_t *rad_dict_find_attr(const char *name); -struct rad_dict_attr_t *rad_dict_find_attr_type(int type); -struct rad_dict_value_t *rad_dict_find_val(struct rad_dict_attr_t *, const char *name); +struct rad_dict_attr_t *rad_dict_find_attr_id(int type); +struct rad_dict_value_t *rad_dict_find_val_name(struct rad_dict_attr_t *, const char *name); +struct rad_dict_value_t *rad_dict_find_val(struct rad_dict_attr_t *, rad_value_t val); struct rad_req_t *rad_req_alloc(struct radius_pd_t *rpd, int code, const char *username); void rad_req_free(struct rad_req_t *); @@ -96,11 +108,13 @@ int rad_req_send(struct rad_req_t *); int rad_req_wait(struct rad_req_t *, int); int rad_req_add_int(struct rad_req_t *req, const char *name, int val); int rad_req_add_val(struct rad_req_t *req, const char *name, const char *val, int len); -int rad_req_add_str(struct rad_req_t *req, const char *name, const char *val, int len); +int rad_req_add_str(struct rad_req_t *req, const char *name, const char *val, int len, int printable); -int rad_packet_build(struct rad_packet_t *pack); +struct rad_packet_t *rad_packet_alloc(int code); +int rad_packet_build(struct rad_packet_t *pack, uint8_t *RA); struct rad_packet_t *rad_packet_recv(int fd); void rad_packet_free(struct rad_packet_t *); +void rad_packet_print(struct rad_packet_t *pack, void (*print)(const char *fmt, ...)); #endif diff --git a/accel-pptpd/radius/req.c b/accel-pptpd/radius/req.c index ae761547..cc59a431 100644 --- a/accel-pptpd/radius/req.c +++ b/accel-pptpd/radius/req.c @@ -11,11 +11,12 @@ #include "log.h" #include "radius.h" +static int urandom_fd; static int rad_req_read(struct triton_md_handler_t *h); static void rad_req_timeout(struct triton_timer_t *t); -struct rad_req_t *rad_rec_alloc(struct radius_pd_t *rpd, int code, const char *username) +struct rad_req_t *rad_req_alloc(struct radius_pd_t *rpd, int code, const char *username) { struct rad_req_t *req = malloc(sizeof(*req)); @@ -23,26 +24,37 @@ struct rad_req_t *rad_rec_alloc(struct radius_pd_t *rpd, int code, const char *u return NULL; memset(req, 0, sizeof(*req)); - INIT_LIST_HEAD(&req->pack.attrs); req->rpd = rpd; - req->pack.code = code; - req->pack.len = 20; req->hnd.fd = -1; req->hnd.read = rad_req_read; req->timeout.expire = rad_req_timeout; - if (rad_req_add_str(req, "User-Name", username, strlen(username))) + while (1) { + if (read(urandom_fd, req->RA, 16) != 16) { + if (errno == EINTR) + continue; + log_error("radius:req:read urandom: %s\n", strerror(errno)); + goto out_err; + } + break; + } + + req->pack = rad_packet_alloc(code); + if (!req->pack) + goto out_err; + + if (rad_req_add_str(req, "User-Name", username, strlen(username), 1)) goto out_err; if (conf_nas_identifier) - if (rad_req_add_str(req, "NAS-Identifier", conf_nas_identifier, strlen(conf_nas_identifier))) + if (rad_req_add_str(req, "NAS-Identifier", conf_nas_identifier, strlen(conf_nas_identifier), 1)) goto out_err; if (rad_req_add_int(req, "NAS-Port-Id", rpd->ppp->unit_idx)) goto out_err; - if (rad_req_add_str(req, "NAS-Port-Type", "Sync", 4)) + if (rad_req_add_val(req, "NAS-Port-Type", "Sync", 4)) goto out_err; - if (rad_req_add_str(req, "Service-Type", "Framed-User", 11)) + if (rad_req_add_val(req, "Service-Type", "Framed-User", 4)) goto out_err; - if (rad_req_add_str(req, "Framed-Protocol", "PPP", 3)) + if (rad_req_add_val(req, "Framed-Protocol", "PPP", 4)) goto out_err; return req; @@ -52,9 +64,15 @@ out_err: return NULL; } -void rad_rec_free(struct rad_req_t *req) +void rad_req_free(struct rad_req_t *req) { - + if (req->hnd.fd >= 0 ) + close(req->hnd.fd); + if (req->pack) + rad_packet_free(req->pack); + if (req->reply) + rad_packet_free(req->reply); + free(req); } int rad_req_send(struct rad_req_t *req) @@ -63,22 +81,24 @@ int rad_req_send(struct rad_req_t *req) int n; if (req->hnd.fd == -1) { - req->hnd.fd = socket(PF_INET, SOCK_DGRAM ,0); - if (!req->hnd.fd) { + req->hnd.fd = socket(PF_INET, SOCK_DGRAM, 0); + if (req->hnd.fd < 0) { log_error("radius:socket: %s\n", strerror(errno)); return -1; } + memset(&addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; + if (conf_nas_ip_address) { - addr.sin_family = AF_INET; - addr.sin_addr.s_addr = htonl(inet_addr(conf_nas_ip_address)); + addr.sin_addr.s_addr = inet_addr(conf_nas_ip_address); if (bind(req->hnd.fd, (struct sockaddr *) &addr, sizeof(addr))) { log_error("radius:bind: %s\n", strerror(errno)); goto out_err; } } - addr.sin_addr.s_addr = htonl(inet_addr(req->server_name)); + addr.sin_addr.s_addr = inet_addr(req->server_name); addr.sin_port = htons(req->server_port); if (connect(req->hnd.fd, (struct sockaddr *) &addr, sizeof(addr))) { @@ -91,28 +111,32 @@ int rad_req_send(struct rad_req_t *req) goto out_err; } - if (rad_packet_build(&req->pack)) + if (rad_packet_build(req->pack, req->RA)) goto out_err; } + if (conf_verbose) { + log_debug("send "); + rad_packet_print(req->pack, log_debug); + } + while (1) { - n = write(req->hnd.fd, req->pack.buf, req->pack.len); + n = write(req->hnd.fd, req->pack->buf, req->pack->len); + //n = sendto(req->hnd.fd, req->pack->buf, req->pack->len, 0, &addr, sizeof(addr)); if (n < 0) { if (errno == EINTR) continue; log_error("radius:write: %s\n", strerror(errno)); - goto out_err_free_pack; - } else if (n != req->pack.len) { - log_error("radius:write: short write %i, excpected %i\n", n, req->pack.len); - goto out_err_free_pack; + goto out_err; + } else if (n != req->pack->len) { + log_error("radius:write: short write %i, excpected %i\n", n, req->pack->len); + goto out_err; } break; } return 0; -out_err_free_pack: - rad_packet_free(&req->pack); out_err: close(req->hnd.fd); req->hnd.fd = -1; @@ -124,7 +148,7 @@ int rad_req_add_int(struct rad_req_t *req, const char *name, int val) struct rad_req_attr_t *ra; struct rad_dict_attr_t *attr; - if (req->pack.len + 2 + 4 >= REQ_LENGTH_MAX) + if (req->pack->len + 2 + 4 >= REQ_LENGTH_MAX) return -1; attr = rad_dict_find_attr(name); @@ -138,18 +162,19 @@ int rad_req_add_int(struct rad_req_t *req, const char *name, int val) ra->attr = attr; ra->len = 4; ra->val.integer = val; - list_add_tail(&ra->entry, &req->pack.attrs); - req->pack.len += 2 + 4; + ra->printable = 1; + list_add_tail(&ra->entry, &req->pack->attrs); + req->pack->len += 2 + 4; return 0; } -int rad_req_add_str(struct rad_req_t *req, const char *name, const char *val, int len) +int rad_req_add_str(struct rad_req_t *req, const char *name, const char *val, int len, int printable) { struct rad_req_attr_t *ra; struct rad_dict_attr_t *attr; - if (req->pack.len + 2 + len >= REQ_LENGTH_MAX) + if (req->pack->len + 2 + len >= REQ_LENGTH_MAX) return -1; attr = rad_dict_find_attr(name); @@ -157,14 +182,24 @@ int rad_req_add_str(struct rad_req_t *req, const char *name, const char *val, in return -1; ra = malloc(sizeof(*ra)); - if (!ra) + if (!ra) { + log_error("radius: aout of memory\n"); return -1; + } ra->attr = attr; ra->len = len; - ra->val.string = strdup(val); - list_add_tail(&ra->entry, &req->pack.attrs); - req->pack.len += 2 + len; + ra->val.string = malloc(len+1); + if (!ra->val.string) { + log_error("radius: out of memory\n"); + free(ra); + return -1; + } + memcpy(ra->val.string, val, len); + ra->val.string[len] = 0; + ra->printable = printable; + list_add_tail(&ra->entry, &req->pack->attrs); + req->pack->len += 2 + len; return 0; } @@ -175,14 +210,14 @@ int rad_req_add_val(struct rad_req_t *req, const char *name, const char *val, in struct rad_dict_attr_t *attr; struct rad_dict_value_t *v; - if (req->pack.len + 2 + len >= REQ_LENGTH_MAX) + if (req->pack->len + 2 + len >= REQ_LENGTH_MAX) return -1; attr = rad_dict_find_attr(name); if (!attr) return -1; - v = rad_dict_find_val(attr, val); + v = rad_dict_find_val_name(attr, val); if (!v) return -1; @@ -193,39 +228,61 @@ int rad_req_add_val(struct rad_req_t *req, const char *name, const char *val, in ra->attr = attr; ra->len = len; ra->val = v->val; - list_add_tail(&ra->entry, &req->pack.attrs); - req->pack.len += 2 + len; + ra->printable = 1; + list_add_tail(&ra->entry, &req->pack->attrs); + req->pack->len += 2 + len; return 0; } +static void req_wakeup(struct rad_req_t *req) +{ + triton_context_wakeup(req->rpd->ppp->ctrl->ctx); + triton_timer_del(&req->timeout); + triton_md_unregister_handler(&req->hnd); + triton_context_unregister(&req->ctx); +} static int rad_req_read(struct triton_md_handler_t *h) { struct rad_req_t *req = container_of(h, typeof(*req), hnd); req->reply = rad_packet_recv(h->fd); - + req_wakeup(req); + return 0; } static void rad_req_timeout(struct triton_timer_t *t) { + struct rad_req_t *req = container_of(t, typeof(*req), timeout); + + req_wakeup(req); } int rad_req_wait(struct rad_req_t *req, int timeout) { - triton_md_register_handler(req->rpd->ppp->ctrl->ctx, &req->hnd); + triton_context_register(&req->ctx); + triton_md_register_handler(&req->ctx, &req->hnd); if (triton_md_enable_handler(&req->hnd, MD_MODE_READ)) return -1; req->timeout.period = timeout * 1000; - if (triton_timer_add(req->rpd->ppp->ctrl->ctx, &req->timeout, 0)) + if (triton_timer_add(&req->ctx, &req->timeout, 0)) return -1; - triton_ctx_schedule(&req->hnd, &req->timeout); - - triton_timer_del(&req->timeout); - triton_md_unregister_handler(&req->hnd); + triton_context_schedule(req->rpd->ppp->ctrl->ctx); + if (conf_verbose && req->reply) { + log_debug("recv "); + rad_packet_print(req->reply, log_debug); + } return 0; } +void __init req_init(void) +{ + urandom_fd = open("/dev/urandom", O_RDONLY); + if (!urandom_fd) { + perror("radius:req: open /dev/urandom"); + _exit(EXIT_FAILURE); + } +} diff --git a/accel-pptpd/triton/md.c b/accel-pptpd/triton/md.c index 3cb47a6d..05d814cc 100644 --- a/accel-pptpd/triton/md.c +++ b/accel-pptpd/triton/md.c @@ -86,16 +86,16 @@ static void *md_thread(void *arg) return NULL; } -void __export triton_md_register_handler(struct triton_ctx_t *ctx, struct triton_md_handler_t *ud) +void __export triton_md_register_handler(struct triton_context_t *ctx, struct triton_md_handler_t *ud) { struct _triton_md_handler_t *h = mempool_alloc(md_pool); memset(h, 0, sizeof(*h)); h->ud = ud; h->epoll_event.data.ptr = h; if (ctx) - h->ctx = (struct _triton_ctx_t *)ctx->tpd; + h->ctx = (struct _triton_context_t *)ctx->tpd; else - h->ctx = (struct _triton_ctx_t *)default_ctx->tpd; + h->ctx = (struct _triton_context_t *)default_ctx->tpd; ud->tpd = h; spin_lock(&h->ctx->lock); list_add_tail(&h->entry, &h->ctx->handlers); diff --git a/accel-pptpd/triton/timer.c b/accel-pptpd/triton/timer.c index f34c19d1..e9fd66a0 100644 --- a/accel-pptpd/triton/timer.c +++ b/accel-pptpd/triton/timer.c @@ -86,7 +86,7 @@ void *timer_thread(void *arg) return NULL; } -int __export triton_timer_add(struct triton_ctx_t *ctx, struct triton_timer_t *ud, int abs_time) +int __export triton_timer_add(struct triton_context_t *ctx, struct triton_timer_t *ud, int abs_time) { struct _triton_timer_t *t = mempool_alloc(timer_pool); @@ -95,9 +95,9 @@ int __export triton_timer_add(struct triton_ctx_t *ctx, struct triton_timer_t *u t->epoll_event.data.ptr = t; t->epoll_event.events = EPOLLIN | EPOLLET; if (ctx) - t->ctx = (struct _triton_ctx_t *)ctx->tpd; + t->ctx = (struct _triton_context_t *)ctx->tpd; else - t->ctx = (struct _triton_ctx_t *)default_ctx->tpd; + t->ctx = (struct _triton_context_t *)default_ctx->tpd; t->fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK); if (t->fd < 0) { triton_log_error("timer:timerfd_create: %s" ,strerror(errno)); diff --git a/accel-pptpd/triton/triton.c b/accel-pptpd/triton/triton.c index 9aa7462b..ba08c122 100644 --- a/accel-pptpd/triton/triton.c +++ b/accel-pptpd/triton/triton.c @@ -7,7 +7,7 @@ #include "triton_p.h" -int thread_count = 4; +int thread_count = 1; int max_events = 64; static spinlock_t threads_lock = SPINLOCK_INITIALIZER; @@ -19,7 +19,7 @@ static LIST_HEAD(ctx_queue); static spinlock_t ctx_list_lock = SPINLOCK_INITIALIZER; static LIST_HEAD(ctx_list); -struct triton_ctx_t *default_ctx; +struct triton_context_t *default_ctx; static int terminate; static mempool_t *ctx_pool; @@ -31,61 +31,25 @@ void triton_thread_wakeup(struct _triton_thread_t *thread) static void* triton_thread(struct _triton_thread_t *thread) { - struct _triton_md_handler_t *h; - struct _triton_timer_t *t; sigset_t set; int sig; - uint64_t tt; sigemptyset(&set); sigaddset(&set, SIGUSR1); sigaddset(&set, SIGQUIT); - while(1){ + while (1) { sigwait(&set, &sig); cont: - if (thread->ctx->need_close) { - if (thread->ctx->ud->close) - thread->ctx->ud->close(thread->ctx->ud); - thread->ctx->need_close = 0; - } - - while (1) { - spin_lock(&thread->ctx->lock); - if (!list_empty(&thread->ctx->pending_timers)) { - t = list_entry(thread->ctx->pending_timers.next, typeof(*t), entry2); - list_del(&t->entry2); - t->pending = 0; - spin_unlock(&thread->ctx->lock); - read(t->fd, &tt, sizeof(tt)); - t->ud->expire(t->ud); - } - if (!list_empty(&thread->ctx->pending_handlers)) { - h = list_entry(thread->ctx->pending_handlers.next, typeof(*h), entry2); - list_del(&h->entry2); - h->pending = 0; - spin_unlock(&thread->ctx->lock); - - if (h->trig_epoll_events & (EPOLLIN | EPOLLERR | EPOLLHUP)) - if (h->ud->read) - if (h->ud->read(h->ud)) - continue; - if (h->trig_epoll_events & (EPOLLOUT | EPOLLERR | EPOLLHUP)) - if (h->ud->write) - if (h->ud->write(h->ud)) - continue; - h->trig_epoll_events = 0; - continue; - } - thread->ctx->thread = NULL; - spin_unlock(&thread->ctx->lock); - if (thread->ctx->need_free) - mempool_free(thread->ctx); - thread->ctx = NULL; - break; + if (swapcontext(&thread->uctx, &thread->ctx->uctx)) { + triton_log_error("swapcontext: %s\n", strerror(errno)); } + if (thread->ctx->need_free) + mempool_free(thread->ctx); + thread->ctx = NULL; + spin_lock(&threads_lock); if (!list_empty(&ctx_queue)) { thread->ctx = list_entry(ctx_queue.next, typeof(*thread->ctx), entry2); @@ -106,6 +70,55 @@ cont: } } +static void ctx_thread(struct _triton_context_t *ctx) +{ + struct _triton_md_handler_t *h; + struct _triton_timer_t *t; + uint64_t tt; + ucontext_t *uctx; + + while (1) { + uctx = &ctx->thread->uctx; + if (ctx->need_close) { + if (ctx->ud->close) + ctx->ud->close(ctx->ud); + ctx->need_close = 0; + } + + while (1) { + spin_lock(&ctx->lock); + if (!list_empty(&ctx->pending_timers)) { + t = list_entry(ctx->pending_timers.next, typeof(*t), entry2); + list_del(&t->entry2); + t->pending = 0; + spin_unlock(&ctx->lock); + read(t->fd, &tt, sizeof(tt)); + t->ud->expire(t->ud); + continue; + } + if (!list_empty(&ctx->pending_handlers)) { + h = list_entry(ctx->pending_handlers.next, typeof(*h), entry2); + list_del(&h->entry2); + h->pending = 0; + spin_unlock(&ctx->lock); + if (h->trig_epoll_events & (EPOLLIN | EPOLLERR | EPOLLHUP)) + if (h->ud && h->ud->read) + h->ud->read(h->ud); + if (h->trig_epoll_events & (EPOLLOUT | EPOLLERR | EPOLLHUP)) + if (h->ud && h->ud->write) + h->ud->write(h->ud); + h->trig_epoll_events = 0; + continue; + } + ctx->thread = NULL; + spin_unlock(&ctx->lock); + + if (swapcontext(&ctx->uctx, uctx)) + triton_log_error("swapcontext: %s\n", strerror(errno)); + } + } +} + struct _triton_thread_t *create_thread() { struct _triton_thread_t *thread = malloc(sizeof(*thread)); @@ -121,9 +134,9 @@ struct _triton_thread_t *create_thread() return thread; } -int triton_queue_ctx(struct _triton_ctx_t *ctx) +int triton_queue_ctx(struct _triton_context_t *ctx) { - if (ctx->thread || ctx->queued) + if (ctx->thread || ctx->queued || ctx->sleeping) return 0; spin_lock(&threads_lock); @@ -142,9 +155,9 @@ int triton_queue_ctx(struct _triton_ctx_t *ctx) return 1; } -void __export triton_register_ctx(struct triton_ctx_t *ud) +int __export triton_context_register(struct triton_context_t *ud) { - struct _triton_ctx_t *ctx = mempool_alloc(ctx_pool); + struct _triton_context_t *ctx = mempool_alloc(ctx_pool); memset(ctx, 0, sizeof(*ctx)); ctx->ud = ud; @@ -154,16 +167,33 @@ void __export triton_register_ctx(struct triton_ctx_t *ud) INIT_LIST_HEAD(&ctx->pending_handlers); INIT_LIST_HEAD(&ctx->pending_timers); + if (getcontext(&ctx->uctx)) { + triton_log_error("getcontext: %s\n", strerror(errno)); + free(ctx); + return -1; + } + + ctx->uctx.uc_stack.ss_size = CTX_STACK_SIZE; + ctx->uctx.uc_stack.ss_sp = malloc(CTX_STACK_SIZE); + if (!ctx->uctx.uc_stack.ss_sp) { + triton_log_error("out of memory\n"); + free(ctx); + return -1; + } + makecontext(&ctx->uctx, (void (*)())ctx_thread, 1, ctx); + ud->tpd = ctx; spin_lock(&ctx_list_lock); list_add_tail(&ctx->entry, &ctx_list); spin_unlock(&ctx_list_lock); + + return 0; } -void __export triton_unregister_ctx(struct triton_ctx_t *ud) +void __export triton_context_unregister(struct triton_context_t *ud) { - struct _triton_ctx_t *ctx = (struct _triton_ctx_t *)ud->tpd; + struct _triton_context_t *ctx = (struct _triton_context_t *)ud->tpd; if (!list_empty(&ctx->handlers)) { triton_log_error("BUG:ctx:triton_unregister_ctx: handlers is not empty"); @@ -175,6 +205,14 @@ void __export triton_unregister_ctx(struct triton_ctx_t *ud) } if (!list_empty(&ctx->timers)) { triton_log_error("BUG:ctx:triton_unregister_ctx: timers is not empty"); + { + struct _triton_timer_t *t; + while(!list_empty(&ctx->timers)) { + t = list_entry(ctx->timers.next, typeof(*t), entry); + t->ud->expire(t->ud); + list_del(&t->entry); + } + } abort(); } if (!list_empty(&ctx->pending_timers)) { @@ -187,17 +225,43 @@ void __export triton_unregister_ctx(struct triton_ctx_t *ud) list_del(&ctx->entry); spin_unlock(&ctx_list_lock); } +void __export triton_context_schedule(struct triton_context_t *ud) +{ + struct _triton_context_t *ctx = (struct _triton_context_t *)ud->tpd; + ucontext_t *uctx = &ctx->thread->uctx; + + spin_lock(&ctx->lock); + ctx->sleeping = 1; + ctx->thread = NULL; + spin_unlock(&ctx->lock); + + if (swapcontext(&ctx->uctx, uctx)) + triton_log_error("swaswpntext: %s\n", strerror(errno)); +} + +void __export triton_context_wakeup(struct triton_context_t *ud) +{ + struct _triton_context_t *ctx = (struct _triton_context_t *)ud->tpd; + int r; + + spin_lock(&ctx->lock); + ctx->sleeping = 0; + r = triton_queue_ctx(ctx); + spin_unlock(&ctx->lock); + if (r) + triton_thread_wakeup(ctx->thread); +} int __export triton_init(const char *conf_file, const char *mod_sect) { - ctx_pool = mempool_create(sizeof(struct _triton_ctx_t)); + ctx_pool = mempool_create(sizeof(struct _triton_context_t)); default_ctx = malloc(sizeof(*default_ctx)); if (!default_ctx) { fprintf(stderr,"cann't allocate memory\n"); return -1; } - triton_register_ctx(default_ctx); + triton_context_register(default_ctx); if (conf_load(conf_file)) return -1; @@ -237,7 +301,7 @@ void __export triton_run() void __export triton_terminate() { - struct _triton_ctx_t *ctx; + struct _triton_context_t *ctx; struct _triton_thread_t *t; md_terminate(); diff --git a/accel-pptpd/triton/triton.h b/accel-pptpd/triton/triton.h index 2cc5ccf6..735264a8 100644 --- a/accel-pptpd/triton/triton.h +++ b/accel-pptpd/triton/triton.h @@ -5,11 +5,11 @@ #include "list.h" -struct triton_ctx_t +struct triton_context_t { const void *tpd; // triton private data, don't touch! - void (*close)(struct triton_ctx_t*); - void (*free)(struct triton_ctx_t*); + void (*close)(struct triton_context_t*); + void (*free)(struct triton_context_t*); }; struct triton_md_handler_t @@ -41,18 +41,19 @@ struct conf_sect_t struct list_head items; }; -void triton_register_ctx(struct triton_ctx_t *); -void triton_unregister_ctx(struct triton_ctx_t *); -void triton_ctx_schedule(struct triton_md_handler_t *, struct triton_timer_t *); +int triton_context_register(struct triton_context_t *); +void triton_context_unregister(struct triton_context_t *); +void triton_context_schedule(struct triton_context_t *); +void triton_context_wakeup(struct triton_context_t *); #define MD_MODE_READ 1 #define MD_MODE_WRITE 2 -void triton_md_register_handler(struct triton_ctx_t *, struct triton_md_handler_t *); +void triton_md_register_handler(struct triton_context_t *, struct triton_md_handler_t *); void triton_md_unregister_handler(struct triton_md_handler_t *h); int triton_md_enable_handler(struct triton_md_handler_t *h, int mode); int triton_md_disable_handler(struct triton_md_handler_t *h,int mode); -int triton_timer_add(struct triton_ctx_t *ctx, struct triton_timer_t*,int abs_time); +int triton_timer_add(struct triton_context_t *ctx, struct triton_timer_t*,int abs_time); int triton_timer_mod(struct triton_timer_t *,int abs_time); void triton_timer_del(struct triton_timer_t *); diff --git a/accel-pptpd/triton/triton_p.h b/accel-pptpd/triton/triton_p.h index c31efd6e..0aa37b1f 100644 --- a/accel-pptpd/triton/triton_p.h +++ b/accel-pptpd/triton/triton_p.h @@ -3,43 +3,52 @@ #include #include +#include #include "triton.h" #include "list.h" #include "spinlock.h" +#define CTX_STACK_SIZE 8196 + struct _triton_thread_t { struct list_head entry; struct list_head entry2; pthread_t thread; int terminate:1; - struct _triton_ctx_t *ctx; + struct _triton_context_t *ctx; + ucontext_t uctx; }; -struct _triton_ctx_t +struct _triton_context_t { struct list_head entry; struct list_head entry2; + spinlock_t lock; + struct _triton_thread_t *thread; + struct list_head handlers; struct list_head timers; - - struct _triton_thread_t *thread; struct list_head pending_handlers; struct list_head pending_timers; + + ucontext_t uctx; + int queued:1; + int sleeping:1; int need_close:1; int need_free:1; - struct triton_ctx_t *ud; + struct triton_context_t *ud; }; struct _triton_md_handler_t { struct list_head entry; struct list_head entry2; - struct _triton_ctx_t *ctx; + struct _triton_context_t *ctx; struct epoll_event epoll_event; uint32_t trig_epoll_events; int pending:1; @@ -51,7 +60,7 @@ struct _triton_timer_t struct list_head entry; struct list_head entry2; struct epoll_event epoll_event; - struct _triton_ctx_t *ctx; + struct _triton_context_t *ctx; int fd; int pending:1; struct triton_timer_t *ud; @@ -76,8 +85,8 @@ void md_run(); void md_terminate(); void timer_run(); void timer_terminate(); -struct triton_ctx_t *default_ctx; -int triton_queue_ctx(struct _triton_ctx_t*); +struct triton_context_t *default_ctx; +int triton_queue_ctx(struct _triton_context_t*); void triton_thread_wakeup(struct _triton_thread_t*); int conf_load(const char *fname); void triton_log_error(const char *fmt,...); -- cgit v1.2.3 From b96fbc3f966b012720d2b74b1dfd2a0ab95086cf Mon Sep 17 00:00:00 2001 From: Dmitry Kozlov Date: Mon, 20 Sep 2010 01:09:07 +0400 Subject: fixed many bugs and memory leaks --- accel-pptpd/CMakeLists.txt | 3 +- accel-pptpd/accel-pptpd.conf | 29 +++++--- accel-pptpd/auth/auth_chap_md5.c | 14 ++-- accel-pptpd/auth/auth_mschap_v1.c | 16 ++-- accel-pptpd/auth/auth_mschap_v2.c | 22 +++--- accel-pptpd/auth/auth_pap.c | 14 ++-- accel-pptpd/ctrl/pptp.c | 19 +++-- accel-pptpd/extra/pppd_compat.c | 54 +++++++------- accel-pptpd/include/memdebug.h | 1 + accel-pptpd/ipdb.c | 2 + accel-pptpd/ippool.c | 5 +- accel-pptpd/iprange.c | 6 +- accel-pptpd/log.c | 42 ++++++++++- accel-pptpd/logs/log_file.c | 28 +++---- accel-pptpd/logs/log_pgsql.c | 46 +++++++----- accel-pptpd/main.c | 10 ++- accel-pptpd/memdebug.c | 153 ++++++++++++++++++++++++++++++++++++++ accel-pptpd/memdebug.h | 27 +++++++ accel-pptpd/ppp/ipcp_opt_dns.c | 8 +- accel-pptpd/ppp/ipcp_opt_ipaddr.c | 8 +- accel-pptpd/ppp/lcp_opt_accomp.c | 6 +- accel-pptpd/ppp/lcp_opt_magic.c | 6 +- accel-pptpd/ppp/lcp_opt_mru.c | 6 +- accel-pptpd/ppp/lcp_opt_pcomp.c | 6 +- accel-pptpd/ppp/ppp.c | 42 ++++++----- accel-pptpd/ppp/ppp_auth.c | 5 +- accel-pptpd/ppp/ppp_ccp.c | 32 +++++--- accel-pptpd/ppp/ppp_fsm.c | 6 +- accel-pptpd/ppp/ppp_ipcp.c | 32 +++++--- accel-pptpd/ppp/ppp_lcp.c | 37 ++++++--- accel-pptpd/ppp/ppp_pd.c | 2 + accel-pptpd/pwdb.c | 2 + accel-pptpd/radius/acct.c | 14 +++- accel-pptpd/radius/auth.c | 7 +- accel-pptpd/radius/dict.c | 24 +++--- accel-pptpd/radius/dm_coa.c | 2 + accel-pptpd/radius/packet.c | 55 +++++++------- accel-pptpd/radius/radius.c | 11 ++- accel-pptpd/radius/req.c | 6 +- accel-pptpd/sigchld.c | 109 +++++++++++++-------------- accel-pptpd/triton/conf_file.c | 27 +++---- accel-pptpd/triton/event.c | 4 +- accel-pptpd/triton/loader.c | 4 +- accel-pptpd/triton/log.c | 2 + accel-pptpd/triton/md.c | 22 +++++- accel-pptpd/triton/mempool.c | 89 +++++++++++++++++++++- accel-pptpd/triton/mempool.h | 17 ++++- accel-pptpd/triton/options.c | 2 + accel-pptpd/triton/timer.c | 22 +++++- accel-pptpd/triton/triton.c | 60 ++++++++++----- accel-pptpd/triton/triton.h | 20 ++++- accel-pptpd/utils.c | 1 + 52 files changed, 856 insertions(+), 331 deletions(-) create mode 120000 accel-pptpd/include/memdebug.h create mode 100644 accel-pptpd/memdebug.c create mode 100644 accel-pptpd/memdebug.h (limited to 'accel-pptpd/pwdb.c') diff --git a/accel-pptpd/CMakeLists.txt b/accel-pptpd/CMakeLists.txt index b27302ca..ee00a283 100644 --- a/accel-pptpd/CMakeLists.txt +++ b/accel-pptpd/CMakeLists.txt @@ -1,7 +1,7 @@ PROJECT (pptpd) cmake_minimum_required(VERSION 2.6) -SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -fvisibility=hidden -D_GNU_SOURCE -DSPINLOCK_GCC") +SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -fvisibility=hidden -D_GNU_SOURCE -DSPINLOCK_GCC -DMEMDEBUG") INCLUDE_DIRECTORIES(include) @@ -37,6 +37,7 @@ ADD_EXECUTABLE(pptpd log.c main.c + memdebug.c ) TARGET_LINK_LIBRARIES(pptpd triton rt pthread ssl) diff --git a/accel-pptpd/accel-pptpd.conf b/accel-pptpd/accel-pptpd.conf index 5ebff71c..230ddba6 100644 --- a/accel-pptpd/accel-pptpd.conf +++ b/accel-pptpd/accel-pptpd.conf @@ -33,22 +33,27 @@ dm_coa_secret=testing123 verbose=1 [client-ip-range] -192.168.10.20-20 -192.168.10.1/32 +192.168.11.20-20 +192.168.11.6/32 [ip-pool] -#gw-ip-address=192.168.100.10 -gw=192.168.100.210-211 -tunnel=192.168.100.200-201 +gw-ip-address=192.168.100.1 +#gw=192.168.100.210-211 +#tunnel=192.168.100.200-201 +192.168.100.2-255 +192.168.101.1-255 +192.168.102.1-255 +192.168.103.1-255 [log] -log-file=/dev/stdout -log-emerg=/dev/stderr -#copy=1 -#color=1 -#per-user-dir=per_user -#per-session-dir=per_session -#per-session=1 +log-file=general.log +log-emerg=emerg.log +log-debug=debug.log +copy=1 +color=1 +per-user-dir=per_user +per-session-dir=per_session +per-session=1 [log-pgsql] conninfo=user=log diff --git a/accel-pptpd/auth/auth_chap_md5.c b/accel-pptpd/auth/auth_chap_md5.c index 058dc677..7681fb5c 100644 --- a/accel-pptpd/auth/auth_chap_md5.c +++ b/accel-pptpd/auth/auth_chap_md5.c @@ -15,6 +15,8 @@ #include "ppp_lcp.h" #include "pwdb.h" +#include "memdebug.h" + #define CHAP_CHALLENGE 1 #define CHAP_RESPONSE 2 #define CHAP_SUCCESS 3 @@ -89,7 +91,7 @@ static void print_str(const char *buf,int size) static struct auth_data_t* auth_data_init(struct ppp_t *ppp) { - struct chap_auth_data_t *d=malloc(sizeof(*d)); + struct chap_auth_data_t *d=_malloc(sizeof(*d)); memset(d,0,sizeof(*d)); d->auth.proto=PPP_CHAP; @@ -102,7 +104,7 @@ 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); + _free(d); } static int chap_start(struct ppp_t *ppp, struct auth_data_t *auth) @@ -223,7 +225,7 @@ static void chap_recv_response(struct chap_auth_data_t *ad, struct chap_hdr_t *h ppp_terminate(ad->ppp, 0); } - name = strndup(msg->name,ntohs(msg->hdr.len)-sizeof(*msg)+2); + name = _strndup(msg->name,ntohs(msg->hdr.len)-sizeof(*msg)+2); r = pwdb_check(ad->ppp, name, PPP_CHAP, CHAP_MD5, ad->id, ad->val, VALUE_SIZE, msg->val); @@ -231,7 +233,7 @@ static void chap_recv_response(struct chap_auth_data_t *ad, struct chap_hdr_t *h passwd = pwdb_get_passwd(ad->ppp,name); if (!passwd) { - free(name); + _free(name); log_ppp_debug("chap-md5: user not found\n"); chap_send_failure(ad); return; @@ -253,11 +255,11 @@ static void chap_recv_response(struct chap_auth_data_t *ad, struct chap_hdr_t *h chap_send_success(ad); auth_successed(ad->ppp, name); } - free(passwd); + _free(passwd); } else if (r == PWDB_DENIED) { chap_send_failure(ad); auth_failed(ad->ppp); - free(name); + _free(name); } else { chap_send_success(ad); auth_successed(ad->ppp, name); diff --git a/accel-pptpd/auth/auth_mschap_v1.c b/accel-pptpd/auth/auth_mschap_v1.c index bc54ed46..818d60db 100644 --- a/accel-pptpd/auth/auth_mschap_v1.c +++ b/accel-pptpd/auth/auth_mschap_v1.c @@ -17,6 +17,8 @@ #include "ppp_lcp.h" #include "pwdb.h" +#include "memdebug.h" + #define MSCHAP_V1 0x80 #define CHAP_CHALLENGE 1 @@ -101,7 +103,7 @@ static void print_str(const char *buf,int size) static struct auth_data_t* auth_data_init(struct ppp_t *ppp) { - struct chap_auth_data_t *d=malloc(sizeof(*d)); + struct chap_auth_data_t *d=_malloc(sizeof(*d)); memset(d,0,sizeof(*d)); d->auth.proto=PPP_CHAP; @@ -114,7 +116,7 @@ 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); + _free(d); } static int chap_start(struct ppp_t *ppp, struct auth_data_t *auth) @@ -234,7 +236,7 @@ static void chap_recv_response(struct chap_auth_data_t *ad, struct chap_hdr_t *h auth_failed(ad->ppp); } - name = strndup(msg->name,ntohs(msg->hdr.len)-sizeof(*msg)+2); + name = _strndup(msg->name,ntohs(msg->hdr.len)-sizeof(*msg)+2); if (!name) { log_emerg("mschap-v2: out of memory\n"); auth_failed(ad->ppp); @@ -248,7 +250,7 @@ static void chap_recv_response(struct chap_auth_data_t *ad, struct chap_hdr_t *h if (r == PWDB_DENIED) { chap_send_failure(ad); auth_failed(ad->ppp); - free(name); + _free(name); } else { chap_send_success(ad); auth_successed(ad->ppp, name); @@ -301,7 +303,7 @@ static int chap_check_response(struct chap_auth_data_t *ad, struct chap_response return PWDB_DENIED; } - u_passwd=malloc(strlen(passwd)*2); + u_passwd=_malloc(strlen(passwd)*2); for(i=0; ival,z_hash+7,nt_hash+8); des_encrypt(ad->val,z_hash+14,nt_hash+16); - free(passwd); - free(u_passwd); + _free(passwd); + _free(u_passwd); return memcmp(nt_hash,msg->nt_hash,24) ? PWDB_DENIED : PWDB_SUCCESS; } diff --git a/accel-pptpd/auth/auth_mschap_v2.c b/accel-pptpd/auth/auth_mschap_v2.c index 798f6eed..8e507759 100644 --- a/accel-pptpd/auth/auth_mschap_v2.c +++ b/accel-pptpd/auth/auth_mschap_v2.c @@ -18,6 +18,8 @@ #include "ppp_lcp.h" #include "pwdb.h" +#include "memdebug.h" + #define MSCHAP_V2 0x81 #define CHAP_CHALLENGE 1 @@ -116,7 +118,7 @@ static void print_str(const char *buf,int size) static struct auth_data_t* auth_data_init(struct ppp_t *ppp) { - struct chap_auth_data_t *d=malloc(sizeof(*d)); + struct chap_auth_data_t *d=_malloc(sizeof(*d)); memset(d,0,sizeof(*d)); d->auth.proto=PPP_CHAP; @@ -129,7 +131,7 @@ 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); + _free(d); } static int chap_start(struct ppp_t *ppp, struct auth_data_t *auth) @@ -199,7 +201,7 @@ static int generate_response(struct chap_auth_data_t *ad, struct chap_response_t if (!passwd) return -1; - u_passwd=malloc(strlen(passwd)*2); + u_passwd=_malloc(strlen(passwd)*2); for(i=0; ippp, 0); } - name=strndup(msg->name,ntohs(msg->hdr.len)-sizeof(*msg)+2); + name=_strndup(msg->name,ntohs(msg->hdr.len)-sizeof(*msg)+2); if (!name) { log_emerg("mschap-v2: out of memory\n"); auth_failed(ad->ppp); @@ -327,7 +329,7 @@ static void chap_recv_response(struct chap_auth_data_t *ad, struct chap_hdr_t *h if (r == PWDB_DENIED) { chap_send_failure(ad); auth_failed(ad->ppp); - free(name); + _free(name); } else { chap_send_success(ad, msg, authenticator); auth_successed(ad->ppp, name); @@ -382,7 +384,7 @@ static int chap_check_response(struct chap_auth_data_t *ad, struct chap_response return -1; } - u_passwd=malloc(strlen(passwd)*2); + u_passwd=_malloc(strlen(passwd)*2); for(i=0; int_hash,24); } diff --git a/accel-pptpd/auth/auth_pap.c b/accel-pptpd/auth/auth_pap.c index 81098ce9..2abf5720 100644 --- a/accel-pptpd/auth/auth_pap.c +++ b/accel-pptpd/auth/auth_pap.c @@ -9,6 +9,8 @@ #include "ppp_lcp.h" #include "pwdb.h" +#include "memdebug.h" + #define MSG_FAILED "Authentication failed" #define MSG_SUCCESSED "Authentication successed" @@ -63,7 +65,7 @@ static struct ppp_auth_handler_t pap= static struct auth_data_t* auth_data_init(struct ppp_t *ppp) { - struct pap_auth_data_t *d=malloc(sizeof(*d)); + struct pap_auth_data_t *d=_malloc(sizeof(*d)); memset(d,0,sizeof(*d)); d->auth.proto=PPP_PAP; @@ -76,7 +78,7 @@ 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); + _free(d); } static int pap_start(struct ppp_t *ppp, struct auth_data_t *auth) @@ -168,8 +170,8 @@ static int pap_recv_req(struct pap_auth_data_t *p,struct pap_hdr_t *hdr) return -1; } - peer_id=strndup((const char*)peer_id,peer_id_len); - passwd=strndup((const char*)ptr,passwd_len); + peer_id=_strndup((const char*)peer_id,peer_id_len); + passwd=_strndup((const char*)ptr,passwd_len); r = pwdb_check(p->ppp, peer_id, PPP_PAP, passwd); if (r == PWDB_NO_IMPL) { @@ -184,14 +186,14 @@ static int pap_recv_req(struct pap_auth_data_t *p,struct pap_hdr_t *hdr) pap_send_nak(p, hdr->id); auth_failed(p->ppp); ret=-1; - free(peer_id); + _free(peer_id); } else { pap_send_ack(p, hdr->id); auth_successed(p->ppp, peer_id); ret = 0; } - free(passwd); + _free(passwd); return ret; } diff --git a/accel-pptpd/ctrl/pptp.c b/accel-pptpd/ctrl/pptp.c index dab807c8..b8bb3663 100644 --- a/accel-pptpd/ctrl/pptp.c +++ b/accel-pptpd/ctrl/pptp.c @@ -20,6 +20,8 @@ #include "ppp.h" #include "iprange.h" +#include "memdebug.h" + #define STATE_IDLE 0 #define STATE_ESTB 1 #define STATE_PPP 2 @@ -73,10 +75,13 @@ static void disconnect(struct pptp_conn_t *conn) triton_event_fire(EV_CTRL_FINISHED, &conn->ppp); triton_context_unregister(&conn->ctx); + + if (conn->ppp.chan_name) + _free(conn->ppp.chan_name); - free(conn->in_buf); - free(conn->out_buf); - free(conn); + _free(conn->in_buf); + _free(conn->out_buf); + _free(conn); } static int post_msg(struct pptp_conn_t *conn, void *buf, int size) @@ -265,7 +270,7 @@ static int pptp_out_call_rqst(struct pptp_conn_t *conn) return -1; conn->ppp.fd = pptp_sock; - conn->ppp.chan_name = strdup(inet_ntoa(dst_addr.sa_addr.pptp.sin_addr)); + conn->ppp.chan_name = _strdup(inet_ntoa(dst_addr.sa_addr.pptp.sin_addr)); triton_event_fire(EV_CTRL_STARTED, &conn->ppp); @@ -486,15 +491,15 @@ static int pptp_connect(struct triton_md_handler_t *h) continue; } - conn = malloc(sizeof(*conn)); + 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->ctx.before_switch = log_switch; - conn->in_buf = malloc(PPTP_CTRL_SIZE_MAX); - conn->out_buf = malloc(PPTP_CTRL_SIZE_MAX); + 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; diff --git a/accel-pptpd/extra/pppd_compat.c b/accel-pptpd/extra/pppd_compat.c index 1cf67413..dbba96d7 100644 --- a/accel-pptpd/extra/pppd_compat.c +++ b/accel-pptpd/extra/pppd_compat.c @@ -16,6 +16,8 @@ #include "utils.h" #include "sigchld.h" +#include "memdebug.h" + static char *conf_ip_up = "/etc/ppp/ip-up"; static char *conf_ip_down = "/etc/ppp/ip-down"; static char *conf_ip_change = "/etc/ppp/ip-change"; @@ -75,7 +77,7 @@ static void ip_change_handler(struct sigchld_handler_t *h, int status) static void ev_ppp_starting(struct ppp_t *ppp) { - struct pppd_compat_pd_t *pd = malloc(sizeof(*pd)); + struct pppd_compat_pd_t *pd = _malloc(sizeof(*pd)); if (!pd) { log_emerg("pppd_compat: out of memory\n"); @@ -134,14 +136,15 @@ static void ev_ppp_finished(struct ppp_t *ppp) if (!pd) return; - if (pd->started) { - pthread_mutex_lock(&pd->ip_up_hnd.lock); - if (pd->ip_up_hnd.pid) { - log_ppp_warn("pppd_compat: ip-up is not yet finished, terminating it ...\n"); - kill(pd->ip_up_hnd.pid, SIGTERM); - pthread_mutex_unlock(&pd->ip_up_hnd.lock); - } + if (!pd->started) + goto skip; + + pthread_mutex_lock(&pd->ip_up_hnd.lock); + if (pd->ip_up_hnd.pid) { + log_ppp_warn("pppd_compat: ip-up is not yet finished, terminating it ...\n"); + kill(pd->ip_up_hnd.pid, SIGTERM); } + pthread_mutex_unlock(&pd->ip_up_hnd.lock); argv[4] = ipaddr; argv[5] = peer_ipaddr; @@ -163,22 +166,21 @@ static void ev_ppp_finished(struct ppp_t *ppp) } else log_error("pppd_compat: fork: %s\n", strerror(errno)); - if (pd->started) { - pthread_mutex_lock(&pd->ip_up_hnd.lock); - if (pd->ip_up_hnd.pid) { - log_ppp_warn("pppd_compat: ip-up is not yet finished, killing it ...\n"); - kill(pd->ip_up_hnd.pid, SIGKILL); - pthread_mutex_unlock(&pd->ip_up_hnd.lock); - sigchld_unregister_handler(&pd->ip_up_hnd); - } else - pthread_mutex_unlock(&pd->ip_up_hnd.lock); - } + pthread_mutex_lock(&pd->ip_up_hnd.lock); + if (pd->ip_up_hnd.pid) { + log_ppp_warn("pppd_compat: ip-up is not yet finished, killing it ...\n"); + kill(pd->ip_up_hnd.pid, SIGKILL); + pthread_mutex_unlock(&pd->ip_up_hnd.lock); + sigchld_unregister_handler(&pd->ip_up_hnd); + } else + pthread_mutex_unlock(&pd->ip_up_hnd.lock); +skip: if (pd->radattr_saved) remove_radattr(ppp); list_del(&pd->pd.entry); - free(pd); + _free(pd); } static void ev_radius_access_accept(struct ev_radius_t *ev) @@ -230,7 +232,7 @@ static void remove_radattr(struct ppp_t *ppp) { char *fname; - fname = malloc(PATH_MAX); + fname = _malloc(PATH_MAX); if (!fname) { log_emerg("pppd_compat: out of memory\n"); return; @@ -243,7 +245,7 @@ static void remove_radattr(struct ppp_t *ppp) sprintf(fname, "%s_old.%s", conf_radattr_prefix, ppp->ifname); unlink(fname); - free(fname); + _free(fname); } static void write_radattr(struct ppp_t *ppp, struct rad_packet_t *pack, int save_old) @@ -254,17 +256,17 @@ static void write_radattr(struct ppp_t *ppp, struct rad_packet_t *pack, int save char *fname1, *fname2; int i; - fname1 = malloc(PATH_MAX); + fname1 = _malloc(PATH_MAX); if (!fname1) { log_emerg("pppd_compat: out of memory\n"); return; } if (save_old) { - fname2 = malloc(PATH_MAX); + fname2 = _malloc(PATH_MAX); if (!fname2) { log_emerg("pppd_compat: out of memory\n"); - free(fname1); + _free(fname1); return; } } @@ -309,9 +311,9 @@ static void write_radattr(struct ppp_t *ppp, struct rad_packet_t *pack, int save } else log_ppp_warn("pppd_compat: failed to create '%s': %s\n", fname1, strerror(errno)); - free(fname1); + _free(fname1); if (save_old) - free(fname2); + _free(fname2); } static struct pppd_compat_pd_t *find_pd(struct ppp_t *ppp) diff --git a/accel-pptpd/include/memdebug.h b/accel-pptpd/include/memdebug.h new file mode 120000 index 00000000..3ee61bc2 --- /dev/null +++ b/accel-pptpd/include/memdebug.h @@ -0,0 +1 @@ +../memdebug.h \ No newline at end of file diff --git a/accel-pptpd/ipdb.c b/accel-pptpd/ipdb.c index 62a99e9f..a08d3be2 100644 --- a/accel-pptpd/ipdb.c +++ b/accel-pptpd/ipdb.c @@ -1,6 +1,8 @@ #include "triton.h" #include "ipdb.h" +#include "memdebug.h" + static LIST_HEAD(ipdb_handlers); __export struct ipdb_item_t *ipdb_get(struct ppp_t *ppp) diff --git a/accel-pptpd/ippool.c b/accel-pptpd/ippool.c index eb79b02a..ec50a9bf 100644 --- a/accel-pptpd/ippool.c +++ b/accel-pptpd/ippool.c @@ -9,6 +9,8 @@ #include "list.h" #include "triton/spinlock.h" +#include "memdebug.h" + struct ippool_item_t { struct list_head entry; @@ -148,9 +150,6 @@ static void generate_pool(void) it->it.peer_addr = peer_addr->addr; list_add_tail(&it->entry, &ippool); - - free(addr); - free(peer_addr); } } diff --git a/accel-pptpd/iprange.c b/accel-pptpd/iprange.c index 317b66dc..0b1997e9 100644 --- a/accel-pptpd/iprange.c +++ b/accel-pptpd/iprange.c @@ -8,6 +8,8 @@ #include "iprange.h" +#include "memdebug.h" + struct iprange_t { struct list_head entry; @@ -39,7 +41,7 @@ static struct iprange_t *parse1(const char *str) if (m == 0 || m > 32) return NULL; - r = malloc(sizeof(*r)); + r = _malloc(sizeof(*r)); r->prefix = (f4 << 24) | (f3 << 16) | (f2 << 8) | f1; r->mask = 0; @@ -69,7 +71,7 @@ static struct iprange_t *parse2(const char *str) if (m < f4 || m > 255) return NULL; - r = malloc(sizeof(*r)); + r = _malloc(sizeof(*r)); r->prefix = (f4 << 24) | (f3 << 16) | (f2 << 8) | f1; r->end = (m << 24) | (f3 << 16) | (f2 << 8) | f1; r->mask = 0; diff --git a/accel-pptpd/log.c b/accel-pptpd/log.c index fbd50674..b0f6d520 100644 --- a/accel-pptpd/log.c +++ b/accel-pptpd/log.c @@ -13,12 +13,15 @@ #include "log.h" +#include "memdebug.h" + struct log_pd_t { struct ppp_pd_t pd; struct ppp_t *ppp; struct list_head msgs; struct log_msg_t *msg; + int authorized:1; }; struct _log_msg_t @@ -43,6 +46,7 @@ static __thread struct _log_msg_t *cur_msg; static __thread char stat_buf[LOG_MAX_SIZE+1]; static FILE *emerg_file; +static FILE *debug_file; static void *pd_key; @@ -50,6 +54,7 @@ static void _log_free_msg(struct _log_msg_t *msg); static struct log_msg_t *clone_msg(struct _log_msg_t *msg); static int add_msg(struct _log_msg_t *msg, const char *buf); static struct log_pd_t *find_pd(struct ppp_t *ppp); +static void write_msg(FILE *f, struct _log_msg_t *msg, struct ppp_t *ppp); static void do_log(int level, const char *fmt, va_list ap, struct ppp_t *ppp) { @@ -61,6 +66,7 @@ static void do_log(int level, const char *fmt, va_list ap, struct ppp_t *ppp) return; vsnprintf(stat_buf, LOG_MAX_SIZE, fmt, ap); + if (!cur_msg) { cur_msg = mempool_alloc(_msg_pool); if (!cur_msg) @@ -77,13 +83,16 @@ static void do_log(int level, const char *fmt, va_list ap, struct ppp_t *ppp) if (stat_buf[strlen(stat_buf) - 1] != '\n') return; + if (debug_file) + write_msg(debug_file, cur_msg, ppp); + if (ppp && !ppp->username) { lpd = find_pd(ppp); list_add_tail(&cur_msg->entry, &lpd->msgs); } list_for_each_entry(t, &targets, entry) { - if (ppp) { + if (ppp && ppp->username) { if (t->session_log) { m = clone_msg(cur_msg); if (!m) @@ -304,9 +313,25 @@ static struct log_pd_t *find_pd(struct ppp_t *ppp) abort(); } +static void write_msg(FILE *f, struct _log_msg_t *msg, struct ppp_t *ppp) +{ + struct log_chunk_t *chunk; + + if (ppp) + sprintf(stat_buf,"%s: %s: ", ppp->ifname, ppp->sessionid); + else + stat_buf[0] = 0; + + list_for_each_entry(chunk, &msg->chunks, entry) + strcat(stat_buf, chunk->msg); + + fwrite(stat_buf, strlen(stat_buf), 1, f); + fflush(f); +} + static void ev_ctrl_starting(struct ppp_t *ppp) { - struct log_pd_t *lpd = malloc(sizeof(*lpd)); + struct log_pd_t *lpd = _malloc(sizeof(*lpd)); if (!lpd) { log_emerg("log: out of memory\n"); return; @@ -331,7 +356,7 @@ static void ev_ctrl_finished(struct ppp_t *ppp) abort(); } - if (ppp->username) { + if (lpd->authorized) { if (!list_empty(&lpd->msgs)) { log_emerg("log:BUG: lpd->msgs is not empty\n"); abort(); @@ -358,7 +383,7 @@ static void ev_ctrl_finished(struct ppp_t *ppp) } list_del(&lpd->pd.entry); - free(lpd); + _free(lpd); } static void ev_ppp_authorized(struct ppp_t *ppp) @@ -387,6 +412,8 @@ static void ev_ppp_authorized(struct ppp_t *ppp) _log_free_msg(msg); } + + lpd->authorized = 1; } void __export log_switch(struct triton_context_t *ctx, void *arg) @@ -410,6 +437,13 @@ static void __init log_init(void) fprintf(stderr, "log:open: %s\n", strerror(errno)); } + opt = conf_get_opt("log", "log-debug"); + if (opt) { + debug_file = fopen(opt, "a"); + if (!emerg_file) + fprintf(stderr, "log:open: %s\n", strerror(errno)); + } + opt = conf_get_opt("log", "copy"); if (opt && atoi(opt) > 0) conf_copy = 1; diff --git a/accel-pptpd/logs/log_file.c b/accel-pptpd/logs/log_file.c index 10a575ef..f5bded9d 100644 --- a/accel-pptpd/logs/log_file.c +++ b/accel-pptpd/logs/log_file.c @@ -10,6 +10,8 @@ #include "ppp.h" #include "spinlock.h" +#include "memdebug.h" + #define RED_COLOR "\033[1;31m" #define GREEN_COLOR "\033[1;32m" #define YELLOW_COLOR "\033[1;33m" @@ -171,7 +173,7 @@ static int log_write(struct triton_md_handler_t *h) triton_md_unregister_handler(&lf->hnd); close(lf->hnd.fd); triton_context_unregister(&lf->ctx); - free(lf->lpd); + _free(lf->lpd); return 1; } lf->sleeping = 1; @@ -261,13 +263,13 @@ static void per_user_session_start(struct ppp_t *ppp) struct log_file_pd_t *lpd; char *fname; - fname = malloc(PATH_MAX + 32); + fname = _malloc(PATH_MAX + 32); if (!fname) { log_emerg("log_file: out of memory\n"); return; } - lpd = malloc(sizeof(*lpd)); + lpd = _malloc(sizeof(*lpd)); if (!lpd) { log_emerg("log_file: out of memory\n"); goto out_err; @@ -296,26 +298,26 @@ static void per_user_session_start(struct ppp_t *ppp) list_add_tail(&lpd->pd.entry, &ppp->pd_list); - free(fname); + _free(fname); return; out_err: - free(fname); + _free(fname); if (lpd) - free(lpd); + _free(lpd); } static void per_session_start(struct ppp_t *ppp) { struct log_file_pd_t *lpd; char *fname; - fname = malloc(PATH_MAX + 32); + fname = _malloc(PATH_MAX + 32); if (!fname) { log_emerg("log_file: out of memory\n"); return; } - lpd = malloc(sizeof(*lpd)); + lpd = _malloc(sizeof(*lpd)); if (!lpd) { log_emerg("log_file: out of memory\n"); goto out_err; @@ -336,13 +338,13 @@ static void per_session_start(struct ppp_t *ppp) goto out_err; list_add_tail(&lpd->pd.entry, &ppp->pd_list); - free(fname); + _free(fname); return; out_err: - free(fname); + _free(fname); if (lpd) - free(lpd); + _free(lpd); } static void session_stop(struct ppp_t *ppp, void *pd_key) @@ -400,10 +402,10 @@ static void __init init(void) opt = conf_get_opt("log", "log-file"); if (opt) { - log_file = malloc(sizeof(*log_file)); + log_file = _malloc(sizeof(*log_file)); memset(log_file, 0, sizeof(*log_file)); if (log_file_init(log_file, opt)) { - free(log_file); + _free(log_file); log_file = NULL; } } diff --git a/accel-pptpd/logs/log_pgsql.c b/accel-pptpd/logs/log_pgsql.c index cd06881d..f9cf7ed2 100644 --- a/accel-pptpd/logs/log_pgsql.c +++ b/accel-pptpd/logs/log_pgsql.c @@ -10,6 +10,8 @@ #include "list.h" #include "ppp.h" +#include "memdebug.h" + static char *conf_conninfo; static int conf_queue_max = 1000; static char *conf_query; @@ -48,22 +50,30 @@ static void unpack_msg(struct log_msg_t *msg) log_buf[0] = 0; } -static void set_hdr(struct log_msg_t *msg) +static void set_hdr(struct log_msg_t *msg, struct ppp_t *ppp) { struct tm tm; localtime_r(&msg->timestamp.tv_sec, &tm); strftime(msg->hdr->msg, LOG_CHUNK_SIZE, "%Y-%m-%d %H:%M:%S", &tm); - msg->hdr->len = strlen(msg->hdr->msg); + msg->hdr->len = strlen(msg->hdr->msg) + 1; + if (ppp && ppp->username) { + strcpy(msg->hdr->msg + msg->hdr->len, ppp->username); + msg->hdr->len += strlen(ppp->username) + 1; + strcpy(msg->hdr->msg + msg->hdr->len, ppp->sessionid); + msg->hdr->len += strlen(ppp->sessionid) + 1; + } else + memset(msg->hdr->msg + msg->hdr->len, 0, 2); + } static void write_next_msg(void) { struct log_msg_t *msg; - struct ppp_t *ppp; const char *paramValues[4]; int paramFormats[4] = {0, 0, 0, 0}; + char *ptr1, *ptr2; spin_lock(&queue_lock); if (!list_empty(&msg_queue)) { @@ -72,18 +82,13 @@ static void write_next_msg(void) --queue_size; spin_unlock(&queue_lock); - set_hdr(msg); unpack_msg(msg); - ppp = msg->tpd; - if (ppp) { - paramValues[1] = ppp->username; - paramValues[2] = ppp->sessionid; - } else { - paramValues[1] = NULL; - paramValues[2] = NULL; - } - + ptr1 = strchr(msg->hdr->msg, 0); + ptr2 = strchr(ptr1 + 1, 0); + + paramValues[1] = ptr1[1] ? ptr1 + 1 : NULL; + paramValues[2] = ptr2[1] ? ptr2 + 1 : NULL; paramValues[0] = msg->hdr->msg; paramValues[3] = log_buf; @@ -138,30 +143,33 @@ static void wakeup_log(void) static void queue_log(struct log_msg_t *msg) { - int r = 0; + int r = 0, f = 0; spin_lock(&queue_lock); if (queue_size < conf_queue_max) { list_add_tail(&msg->entry, &msg_queue); ++queue_size; r = sleeping; sleeping = 0; - } + } else + f = 1; spin_unlock(&queue_lock); if (r) triton_context_call(&pgsql_ctx, (void (*)(void*))wakeup_log, NULL); + else if (f) + log_free_msg(msg); } static void general_log(struct log_msg_t *msg) { - msg->tpd = NULL; + set_hdr(msg, NULL); queue_log(msg); } static void session_log(struct ppp_t *ppp, struct log_msg_t *msg) { - msg->tpd = ppp; + set_hdr(msg, ppp); queue_log(msg); } @@ -253,11 +261,11 @@ static void __init init(void) opt = conf_get_opt("log-pgsql", "log-table"); if (!opt || strlen(opt) > 32) opt = "log"; - conf_query = malloc(sizeof(QUERY_TEMPLATE) + strlen(opt)); + conf_query = _malloc(sizeof(QUERY_TEMPLATE) + strlen(opt)); sprintf(conf_query, QUERY_TEMPLATE, opt); } - log_buf = malloc(LOG_MAX_SIZE + 1); + log_buf = _malloc(LOG_MAX_SIZE + 1); if (!log_buf) { log_emerg("log_pgsql: out of memory\n"); return; diff --git a/accel-pptpd/main.c b/accel-pptpd/main.c index c60d35fb..147c248c 100644 --- a/accel-pptpd/main.c +++ b/accel-pptpd/main.c @@ -8,6 +8,8 @@ #include "triton/triton.h" +#include "memdebug.h" + static int goto_daemon; static char *pid_file; static char *conf_file; @@ -29,7 +31,7 @@ static int parse_cmdline(char ***argv) _exit(EXIT_FAILURE); } - *argv = malloc(ARG_MAX * sizeof(void *)); + *argv = _malloc(ARG_MAX * sizeof(void *)); memset(*argv, 0, ARG_MAX * sizeof(void *)); for(i = 0; i < ARG_MAX; i++) { @@ -69,7 +71,7 @@ static void __init __main(void) if (!conf_file) goto usage; - if (triton_init(conf_file, "modules")) + if (triton_init(conf_file)) _exit(EXIT_FAILURE); return; @@ -86,6 +88,9 @@ int main(int argc, char **argv) { sigset_t set; + if (triton_load_modules("modules")) + return EXIT_FAILURE; + if (goto_daemon) { pid_t pid = fork(); if (pid > 0) @@ -121,6 +126,7 @@ int main(int argc, char **argv) triton_run(); sigfillset(&set); + sigdelset(&set, SIGINT); sigdelset(&set, SIGTERM); sigdelset(&set, SIGSEGV); sigdelset(&set, SIGILL); diff --git a/accel-pptpd/memdebug.c b/accel-pptpd/memdebug.c new file mode 100644 index 00000000..780f16b8 --- /dev/null +++ b/accel-pptpd/memdebug.c @@ -0,0 +1,153 @@ +#undef MEMDEBUG + +#include +#include +#include +#include +#include +#include + +#include "spinlock.h" +#include "list.h" + +#define __init __attribute__((constructor)) +#define __export __attribute__((visibility("default"))) + +#undef offsetof +#ifdef __compiler_offsetof +#define offsetof(TYPE,MEMBER) __compiler_offsetof(TYPE,MEMBER) +#else +#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) +#endif + +#define container_of(ptr, type, member) ({ \ + const typeof( ((type *)0)->member ) *__mptr = (ptr); \ + (type *)( (char *)__mptr - offsetof(type,member) );}) + + +#define MAGIC1 0x1122334455667788llu + +struct mem_t +{ + struct list_head entry; + char fname[PATH_MAX]; + int line; + size_t size; + uint64_t magic2; + uint64_t magic1; + char data[0]; +}; + +static LIST_HEAD(mem_list); +static spinlock_t mem_list_lock; + +struct mem_t *_md_malloc(size_t size, const char *fname, int line) +{ + struct mem_t *mem = malloc(sizeof(*mem) + size + 8); + strcpy(mem->fname, fname); + mem->line = line; + mem->size = size; + mem->magic1 = MAGIC1; + mem->magic2 = (uint64_t)random() * (uint64_t)random(); + *(uint64_t*)(mem->data + size) = mem->magic2; + + spin_lock(&mem_list_lock); + list_add_tail(&mem->entry, &mem_list); + spin_unlock(&mem_list_lock); + + return mem; +} + +void __export *md_malloc(size_t size, const char *fname, int line) +{ + struct mem_t *mem = _md_malloc(size, fname, line); + + return mem->data; +} + +void __export md_free(void *ptr, const char *fname, int line) +{ + struct mem_t *mem = container_of(ptr, typeof(*mem), data); + + if (!ptr) { + printf("free null pointer at %s:%i\n", fname, line); + abort(); + } + + if (mem->magic1 != MAGIC1) { + printf("memory corruption:\nfree at %s:%i\n", fname, line); + abort(); + } + + if (mem->magic2 != *(uint64_t*)(mem->data + mem->size)) { + printf("memory corruption:\nmalloc(%lu) at %s:%i\nfree at %s:%i\n", mem->size, mem->fname, mem->line, fname, line); + abort(); + } + + mem->magic1 = 0; + mem->magic2 = 0; + + spin_lock(&mem_list_lock); + list_del(&mem->entry); + spin_unlock(&mem_list_lock); + + free(mem); + return; +} + +void __export *md_realloc(void *ptr, size_t size, const char *fname, int line) +{ + struct mem_t *mem = container_of(ptr, typeof(*mem), data); + struct mem_t *mem2; + + if (mem->magic1 != MAGIC1) { + printf("memory corruption:\nfree at %s:%i\n", fname, line); + abort(); + } + + if (mem->magic2 != *(uint64_t*)(mem->data + mem->size)) { + printf("memory corruption:\nmalloc(%lu) at %s:%i\nfree at %s:%i\n", mem->size, mem->fname, mem->line, fname, line); + abort(); + } + + mem2 = _md_malloc(size, fname, line); + memcpy(mem2->data, mem->data, mem->size); + + md_free(mem->data, fname, line); + + return mem2->data; +} + +char __export *md_strdup(const char *ptr, const char *fname, int line) +{ + struct mem_t *mem = _md_malloc(strlen(ptr) + 1, fname, line); + memcpy(mem->data, ptr, strlen(ptr) + 1); + return mem->data; +} + +char __export *md_strndup(const char *ptr, size_t n, const char *fname, int line) +{ + struct mem_t *mem = _md_malloc(n + 1, fname, line); + memcpy(mem->data, ptr, n); + mem->data[n] = 0; + return mem->data; +} + +static void siginfo(int num) +{ + struct mem_t *mem; + size_t total = 0; + + spin_lock(&mem_list_lock); + list_for_each_entry(mem, &mem_list, entry) { + printf("%s:%i %lu\n", mem->fname, mem->line, mem->size); + total += mem->size; + } + spin_unlock(&mem_list_lock); + printf("total = %lu\n", total); +} + +static void __init init(void) +{ + signal(36, siginfo); +} diff --git a/accel-pptpd/memdebug.h b/accel-pptpd/memdebug.h new file mode 100644 index 00000000..ad57f4a8 --- /dev/null +++ b/accel-pptpd/memdebug.h @@ -0,0 +1,27 @@ +#ifndef __MEMDEBUG_H +#define __MEMDEBUG_H + +#ifdef MEMDEBUG + +#include + +#define _malloc(size) md_malloc(size, __FILE__, __LINE__) +#define _realloc(ptr, size) md_realloc(ptr, size, __FILE__, __LINE__) +#define _free(ptr) md_free(ptr, __FILE__, __LINE__) +#define _strdup(str) md_strdup(str, __FILE__, __LINE__) +#define _strndup(str, size) md_strndup(str, size, __FILE__, __LINE__) + +void *md_malloc(size_t size, const char *fname, int line); +void *md_realloc(void *ptr, size_t size, const char *fname, int line); +void md_free(void *ptr, const char *fname, int line); +char* md_strdup(const char *ptr, const char *fname, int line); +char* md_strndup(const char *ptr, size_t size, const char *fname, int line); + +#else +#define _malloc(size) malloc(size) +#define _realloc(ptr, size) realloc(ptr, size) +#define _free(ptr) free(ptr) +#endif + +#endif + diff --git a/accel-pptpd/ppp/ipcp_opt_dns.c b/accel-pptpd/ppp/ipcp_opt_dns.c index b7417989..16ef7af4 100644 --- a/accel-pptpd/ppp/ipcp_opt_dns.c +++ b/accel-pptpd/ppp/ipcp_opt_dns.c @@ -7,6 +7,8 @@ #include "log.h" #include "ipdb.h" +#include "memdebug.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); @@ -43,7 +45,7 @@ static struct ipcp_option_handler_t dns2_opt_hnd= static struct ipcp_option_t *dns1_init(struct ppp_ipcp_t *ipcp) { - struct dns_option_t *dns_opt=malloc(sizeof(*dns_opt)); + 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; @@ -53,7 +55,7 @@ static struct ipcp_option_t *dns1_init(struct ppp_ipcp_t *ipcp) static struct ipcp_option_t *dns2_init(struct ppp_ipcp_t *ipcp) { - struct dns_option_t *dns_opt=malloc(sizeof(*dns_opt)); + 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; @@ -65,7 +67,7 @@ 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); + _free(dns_opt); } static int dns_send_conf_req(struct ppp_ipcp_t *ipcp, struct ipcp_option_t *opt, uint8_t *ptr) diff --git a/accel-pptpd/ppp/ipcp_opt_ipaddr.c b/accel-pptpd/ppp/ipcp_opt_ipaddr.c index 1f9af3e7..963d4736 100644 --- a/accel-pptpd/ppp/ipcp_opt_ipaddr.c +++ b/accel-pptpd/ppp/ipcp_opt_ipaddr.c @@ -13,6 +13,8 @@ #include "ipdb.h" #include "iprange.h" +#include "memdebug.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); @@ -39,7 +41,7 @@ static struct ipcp_option_handler_t ipaddr_opt_hnd= static struct ipcp_option_t *ipaddr_init(struct ppp_ipcp_t *ipcp) { - struct ipaddr_option_t *ipaddr_opt=malloc(sizeof(*ipaddr_opt)); + struct ipaddr_option_t *ipaddr_opt=_malloc(sizeof(*ipaddr_opt)); memset(ipaddr_opt,0,sizeof(*ipaddr_opt)); ipaddr_opt->opt.id=CI_ADDR; ipaddr_opt->opt.len=6; @@ -54,7 +56,7 @@ static void ipaddr_free(struct ppp_ipcp_t *ipcp, struct ipcp_option_t *opt) if (ipaddr_opt->ip) ipdb_put(ipcp->ppp, ipaddr_opt->ip); - free(ipaddr_opt); + _free(ipaddr_opt); } static int ipaddr_send_conf_req(struct ppp_ipcp_t *ipcp, struct ipcp_option_t *opt, uint8_t *ptr) @@ -65,7 +67,7 @@ static int ipaddr_send_conf_req(struct ppp_ipcp_t *ipcp, struct ipcp_option_t *o if (!ipaddr_opt->ip) { ipaddr_opt->ip = ipdb_get(ipcp->ppp); if (!ipaddr_opt->ip) { - log_ppp_warn("ppp:ipcp: no free IP address\n"); + log_ppp_warn("ppp:ipcp: no _free IP address\n"); return -1; } } diff --git a/accel-pptpd/ppp/lcp_opt_accomp.c b/accel-pptpd/ppp/lcp_opt_accomp.c index c4c221ce..8545f9d7 100644 --- a/accel-pptpd/ppp/lcp_opt_accomp.c +++ b/accel-pptpd/ppp/lcp_opt_accomp.c @@ -6,6 +6,8 @@ #include "ppp_lcp.h" #include "log.h" +#include "memdebug.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); @@ -31,7 +33,7 @@ static struct lcp_option_handler_t accomp_opt_hnd= static struct lcp_option_t *accomp_init(struct ppp_lcp_t *lcp) { - struct accomp_option_t *accomp_opt=malloc(sizeof(*accomp_opt)); + 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; @@ -44,7 +46,7 @@ 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); + _free(accomp_opt); } static int accomp_send_conf_req(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr) diff --git a/accel-pptpd/ppp/lcp_opt_magic.c b/accel-pptpd/ppp/lcp_opt_magic.c index d6094c0c..cec40ce4 100644 --- a/accel-pptpd/ppp/lcp_opt_magic.c +++ b/accel-pptpd/ppp/lcp_opt_magic.c @@ -6,6 +6,8 @@ #include "ppp_lcp.h" #include "log.h" +#include "memdebug.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); @@ -29,7 +31,7 @@ static struct lcp_option_handler_t magic_opt_hnd= static struct lcp_option_t *magic_init(struct ppp_lcp_t *lcp) { - struct magic_option_t *magic_opt=malloc(sizeof(*magic_opt)); + 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; @@ -44,7 +46,7 @@ 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); + _free(magic_opt); } static int magic_send_conf_req(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr) diff --git a/accel-pptpd/ppp/lcp_opt_mru.c b/accel-pptpd/ppp/lcp_opt_mru.c index fc79db38..4dc96851 100644 --- a/accel-pptpd/ppp/lcp_opt_mru.c +++ b/accel-pptpd/ppp/lcp_opt_mru.c @@ -11,6 +11,8 @@ #include "ppp_lcp.h" #include "log.h" +#include "memdebug.h" + #define MAX_MTU 1436 static struct lcp_option_t *mru_init(struct ppp_lcp_t *lcp); @@ -41,7 +43,7 @@ static struct lcp_option_handler_t mru_opt_hnd= static struct lcp_option_t *mru_init(struct ppp_lcp_t *lcp) { - struct mru_option_t *mru_opt=malloc(sizeof(*mru_opt)); + 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; @@ -55,7 +57,7 @@ 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); + _free(mru_opt); } static int mru_send_conf_req(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr) diff --git a/accel-pptpd/ppp/lcp_opt_pcomp.c b/accel-pptpd/ppp/lcp_opt_pcomp.c index 8700bf49..0ff4d11f 100644 --- a/accel-pptpd/ppp/lcp_opt_pcomp.c +++ b/accel-pptpd/ppp/lcp_opt_pcomp.c @@ -6,6 +6,8 @@ #include "ppp_lcp.h" #include "log.h" +#include "memdebug.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); @@ -31,7 +33,7 @@ static struct lcp_option_handler_t pcomp_opt_hnd= static struct lcp_option_t *pcomp_init(struct ppp_lcp_t *lcp) { - struct pcomp_option_t *pcomp_opt=malloc(sizeof(*pcomp_opt)); + 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; @@ -44,7 +46,7 @@ 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); + _free(pcomp_opt); } static int pcomp_send_conf_req(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr) diff --git a/accel-pptpd/ppp/ppp.c b/accel-pptpd/ppp/ppp.c index 87c8e8e0..4deb1356 100644 --- a/accel-pptpd/ppp/ppp.c +++ b/accel-pptpd/ppp/ppp.c @@ -19,6 +19,8 @@ #include "ppp_fsm.h" #include "log.h" +#include "memdebug.h" + int conf_ppp_verbose; static LIST_HEAD(layers); @@ -34,24 +36,25 @@ struct layer_node_t 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 _free_layers(struct ppp_t *); static void start_first_layer(struct ppp_t *); void __export ppp_init(struct ppp_t *ppp) { memset(ppp,0,sizeof(*ppp)); + INIT_LIST_HEAD(&ppp->layers); INIT_LIST_HEAD(&ppp->chan_handlers); INIT_LIST_HEAD(&ppp->unit_handlers); INIT_LIST_HEAD(&ppp->pd_list); } -static void free_ppp(struct ppp_t *ppp) +static void _free_ppp(struct ppp_t *ppp) { - free(ppp->chan_buf); - free(ppp->unit_buf); + _free(ppp->chan_buf); + _free(ppp->unit_buf); if (ppp->username) - free(ppp->username); + _free(ppp->username); } static void generate_sessionid(struct ppp_t *ppp) @@ -121,8 +124,8 @@ int __export establish_ppp(struct ppp_t *ppp) log_ppp_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); + ppp->chan_buf=_malloc(PPP_MRU); + ppp->unit_buf=_malloc(PPP_MRU); init_layers(ppp); @@ -166,7 +169,7 @@ exit_close_unit: exit_close_chan: close(ppp->chan_fd); - free_ppp(ppp); + _free_ppp(ppp); return -1; } @@ -183,15 +186,20 @@ static void destablish_ppp(struct ppp_t *ppp) ppp->unit_fd = -1; ppp->chan_fd = -1; - free(ppp->unit_buf); - free(ppp->chan_buf); + _free(ppp->unit_buf); + _free(ppp->chan_buf); - free_layers(ppp); + _free_layers(ppp); log_ppp_debug("ppp destablished\n"); triton_event_fire(EV_PPP_FINISHED, ppp); ppp->ctrl->finished(ppp); + + if (ppp->username) { + _free(ppp->username); + ppp->username = NULL; + } } void print_buf(uint8_t *buf,int size) @@ -424,7 +432,7 @@ int __export ppp_register_layer(const char *name, struct ppp_layer_t *layer) continue; if (orderorder) { - n1=malloc(sizeof(*n1)); + n1=_malloc(sizeof(*n1)); memset(n1,0,sizeof(*n1)); n1->order=order; INIT_LIST_HEAD(&n1->items); @@ -433,7 +441,7 @@ int __export ppp_register_layer(const char *name, struct ppp_layer_t *layer) } goto insert; } - n1=malloc(sizeof(*n1)); + n1=_malloc(sizeof(*n1)); memset(n1,0,sizeof(*n1)); n1->order=order; INIT_LIST_HEAD(&n1->items); @@ -455,10 +463,8 @@ static void init_layers(struct ppp_t *ppp) 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)); + n1 = _malloc(sizeof(*n1)); memset(n1, 0, sizeof(*n1)); INIT_LIST_HEAD(&n1->items); list_add_tail(&n1->entry, &ppp->layers); @@ -472,7 +478,7 @@ static void init_layers(struct ppp_t *ppp) } } -static void free_layers(struct ppp_t *ppp) +static void _free_layers(struct ppp_t *ppp) { struct layer_node_t *n; struct ppp_layer_data_t *d; @@ -485,7 +491,7 @@ static void free_layers(struct ppp_t *ppp) d->layer->free(d); } list_del(&n->entry); - free(n); + _free(n); } } diff --git a/accel-pptpd/ppp/ppp_auth.c b/accel-pptpd/ppp/ppp_auth.c index 5ad7444e..d141b817 100644 --- a/accel-pptpd/ppp/ppp_auth.c +++ b/accel-pptpd/ppp/ppp_auth.c @@ -9,6 +9,7 @@ #include "ppp_auth.h" +#include "memdebug.h" static LIST_HEAD(auth_handlers); static int extra_opt_len=0; @@ -247,7 +248,7 @@ print_d: 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)); + struct auth_layer_data_t *ad = _malloc(sizeof(*ad)); log_ppp_debug("auth_layer_init\n"); @@ -294,7 +295,7 @@ static void auth_layer_free(struct ppp_layer_data_t *ld) log_ppp_debug("auth_layer_free\n"); - free(ad); + _free(ad); } void __export auth_successed(struct ppp_t *ppp, char *username) diff --git a/accel-pptpd/ppp/ppp_ccp.c b/accel-pptpd/ppp/ppp_ccp.c index 8f240bc9..301c2385 100644 --- a/accel-pptpd/ppp/ppp_ccp.c +++ b/accel-pptpd/ppp/ppp_ccp.c @@ -11,6 +11,8 @@ #include "ppp.h" #include "ppp_ccp.h" +#include "memdebug.h" + struct recv_opt_t { struct list_head entry; @@ -35,6 +37,8 @@ static void ccp_options_init(struct ppp_ccp_t *ccp) struct ccp_option_t *lopt; struct ccp_option_handler_t *h; + ccp->conf_req_len = sizeof(struct ccp_hdr_t); + list_for_each_entry(h,&option_handlers,entry) { lopt=h->init(ccp); @@ -61,7 +65,7 @@ static void ccp_options_free(struct ppp_ccp_t *ccp) static struct ppp_layer_data_t *ccp_layer_init(struct ppp_t *ppp) { - struct ppp_ccp_t *ccp=malloc(sizeof(*ccp)); + struct ppp_ccp_t *ccp=_malloc(sizeof(*ccp)); memset(ccp,0,sizeof(*ccp)); log_ppp_debug("ccp_layer_init\n"); @@ -123,7 +127,7 @@ void ccp_layer_free(struct ppp_layer_data_t *ld) ccp_options_free(ccp); ppp_fsm_free(&ccp->fsm); - free(ccp); + _free(ccp); } static void ccp_layer_up(struct ppp_fsm_t *fsm) @@ -156,7 +160,7 @@ static void print_ropt(struct recv_opt_t *ropt) static int 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; + 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; @@ -188,6 +192,8 @@ static int send_conf_req(struct ppp_fsm_t *fsm) ccp_hdr->len=htons((ptr-buf)-2); ppp_unit_send(ccp->ppp,ccp_hdr,ptr-buf); + _free(buf); + return 0; } @@ -205,7 +211,7 @@ static void send_conf_ack(struct ppp_fsm_t *fsm) 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; + 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; @@ -232,12 +238,14 @@ static void send_conf_nak(struct ppp_fsm_t *fsm) ccp_hdr->len=htons((ptr-buf)-2); ppp_unit_send(ccp->ppp,ccp_hdr,ptr-buf); + + _free(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; + uint8_t *buf=_malloc(ccp->ropt_len + sizeof(struct ccp_hdr_t)), *ptr=buf; struct ccp_hdr_t *ccp_hdr=(struct ccp_hdr_t*)ptr; struct recv_opt_t *ropt; @@ -266,6 +274,8 @@ static void send_conf_rej(struct ppp_fsm_t *fsm) ccp_hdr->len=htons((ptr-buf)-2); ppp_unit_send(ccp->ppp,ccp_hdr,ptr-buf); + + _free(buf); } static int ccp_recv_conf_req(struct ppp_ccp_t *ccp,uint8_t *data,int size) @@ -281,7 +291,7 @@ static int ccp_recv_conf_req(struct ppp_ccp_t *ccp,uint8_t *data,int size) { hdr=(struct ccp_opt_hdr_t *)data; - ropt=malloc(sizeof(*ropt)); + ropt=_malloc(sizeof(*ropt)); memset(ropt,0,sizeof(*ropt)); if (hdr->len>size) ropt->len=size; else ropt->len=hdr->len; @@ -353,7 +363,7 @@ static void ccp_free_conf_req(struct ppp_ccp_t *ccp) { ropt=list_entry(ccp->ropt_list.next,typeof(*ropt),entry); list_del(&ropt->entry); - free(ropt); + _free(ropt); } } @@ -534,16 +544,16 @@ static void ccp_recv(struct ppp_handler_t*h) ppp_fsm_recv_conf_rej(&ccp->fsm); break; case TERMREQ: - term_msg=strndup((char*)(hdr+1),ntohs(hdr->len)); + term_msg=_strndup((char*)(hdr+1),ntohs(hdr->len)); log_ppp_debug("recv [CCP TermReq id=%x \"%s\"]\n",hdr->id,term_msg); - free(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)); + term_msg=_strndup((char*)(hdr+1),ntohs(hdr->len)); log_ppp_debug("recv [CCP TermAck id=%x \"%s\"]\n",hdr->id,term_msg); - free(term_msg); + _free(term_msg); ppp_fsm_recv_term_ack(&ccp->fsm); break; case CODEREJ: diff --git a/accel-pptpd/ppp/ppp_fsm.c b/accel-pptpd/ppp/ppp_fsm.c index 0e11b660..3b8d854d 100644 --- a/accel-pptpd/ppp/ppp_fsm.c +++ b/accel-pptpd/ppp/ppp_fsm.c @@ -8,6 +8,8 @@ #include "ppp_lcp.h" #include "log.h" +#include "memdebug.h" + static int conf_max_terminate = 2; static int conf_max_configure = 5; static int conf_max_failure = 5; @@ -372,7 +374,7 @@ void ppp_fsm_recv_term_req(struct ppp_fsm_t *layer) { case FSM_Opened: if (layer->layer_down) layer->layer_down(layer); - send_term_req(layer); + //send_term_req(layer); send_term_ack(layer); //if (layer->zero_req_cnt) layer->zero_req_cnt(layer); zero_req_counter(layer); @@ -381,7 +383,7 @@ void ppp_fsm_recv_term_req(struct ppp_fsm_t *layer) case FSM_Req_Sent: case FSM_Ack_Rcvd: case FSM_Ack_Sent: - send_term_req(layer); + send_term_ack(layer); layer->fsm_state=FSM_Req_Sent; break; default: diff --git a/accel-pptpd/ppp/ppp_ipcp.c b/accel-pptpd/ppp/ppp_ipcp.c index b8dfe7f7..f3c20a64 100644 --- a/accel-pptpd/ppp/ppp_ipcp.c +++ b/accel-pptpd/ppp/ppp_ipcp.c @@ -11,6 +11,8 @@ #include "ppp.h" #include "ppp_ipcp.h" +#include "memdebug.h" + struct recv_opt_t { struct list_head entry; @@ -35,6 +37,8 @@ static void ipcp_options_init(struct ppp_ipcp_t *ipcp) struct ipcp_option_t *lopt; struct ipcp_option_handler_t *h; + ipcp->conf_req_len = sizeof(struct ipcp_hdr_t); + list_for_each_entry(h,&option_handlers,entry) { lopt=h->init(ipcp); @@ -61,7 +65,7 @@ static void ipcp_options_free(struct ppp_ipcp_t *ipcp) static struct ppp_layer_data_t *ipcp_layer_init(struct ppp_t *ppp) { - struct ppp_ipcp_t *ipcp=malloc(sizeof(*ipcp)); + struct ppp_ipcp_t *ipcp=_malloc(sizeof(*ipcp)); memset(ipcp,0,sizeof(*ipcp)); log_ppp_debug("ipcp_layer_init\n"); @@ -123,7 +127,7 @@ void ipcp_layer_free(struct ppp_layer_data_t *ld) ipcp_options_free(ipcp); ppp_fsm_free(&ipcp->fsm); - free(ipcp); + _free(ipcp); } static void ipcp_layer_up(struct ppp_fsm_t *fsm) @@ -156,7 +160,7 @@ static void print_ropt(struct recv_opt_t *ropt) static int 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; + 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; @@ -194,6 +198,8 @@ static int send_conf_req(struct ppp_fsm_t *fsm) ipcp_hdr->len=htons((ptr-buf)-2); ppp_unit_send(ipcp->ppp,ipcp_hdr,ptr-buf); + _free(buf); + return 0; } @@ -211,7 +217,7 @@ static void send_conf_ack(struct ppp_fsm_t *fsm) 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; + 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; @@ -238,12 +244,14 @@ static void send_conf_nak(struct ppp_fsm_t *fsm) ipcp_hdr->len=htons((ptr-buf)-2); ppp_unit_send(ipcp->ppp,ipcp_hdr,ptr-buf); + + _free(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; + uint8_t *buf=_malloc(ipcp->ropt_len + sizeof(struct ipcp_hdr_t)), *ptr=buf; struct ipcp_hdr_t *ipcp_hdr=(struct ipcp_hdr_t*)ptr; struct recv_opt_t *ropt; @@ -272,6 +280,8 @@ static void send_conf_rej(struct ppp_fsm_t *fsm) ipcp_hdr->len=htons((ptr-buf)-2); ppp_unit_send(ipcp->ppp,ipcp_hdr,ptr-buf); + + _free(buf); } static int ipcp_recv_conf_req(struct ppp_ipcp_t *ipcp,uint8_t *data,int size) @@ -287,7 +297,7 @@ static int ipcp_recv_conf_req(struct ppp_ipcp_t *ipcp,uint8_t *data,int size) { hdr=(struct ipcp_opt_hdr_t *)data; - ropt=malloc(sizeof(*ropt)); + ropt=_malloc(sizeof(*ropt)); memset(ropt,0,sizeof(*ropt)); if (hdr->len>size) ropt->len=size; else ropt->len=hdr->len; @@ -350,7 +360,7 @@ static void ipcp_free_conf_req(struct ppp_ipcp_t *ipcp) { ropt=list_entry(ipcp->ropt_list.next,typeof(*ropt),entry); list_del(&ropt->entry); - free(ropt); + _free(ropt); } } @@ -531,16 +541,16 @@ static void ipcp_recv(struct ppp_handler_t*h) ppp_fsm_recv_conf_rej(&ipcp->fsm); break; case TERMREQ: - term_msg=strndup((char*)(hdr+1),ntohs(hdr->len)); + term_msg=_strndup((char*)(hdr+1),ntohs(hdr->len)); log_ppp_debug("recv [IPCP TermReq id=%x \"%s\"]\n",hdr->id,term_msg); - free(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)); + term_msg=_strndup((char*)(hdr+1),ntohs(hdr->len)); log_ppp_debug("recv [IPCP TermAck id=%x \"%s\"]\n",hdr->id,term_msg); - free(term_msg); + _free(term_msg); ppp_fsm_recv_term_ack(&ipcp->fsm); break; case CODEREJ: diff --git a/accel-pptpd/ppp/ppp_lcp.c b/accel-pptpd/ppp/ppp_lcp.c index b5afb5a3..a85b3d12 100644 --- a/accel-pptpd/ppp/ppp_lcp.c +++ b/accel-pptpd/ppp/ppp_lcp.c @@ -11,6 +11,8 @@ #include "ppp.h" #include "ppp_lcp.h" +#include "memdebug.h" + struct recv_opt_t { struct list_head entry; @@ -42,6 +44,8 @@ static void lcp_options_init(struct ppp_lcp_t *lcp) INIT_LIST_HEAD(&lcp->options); + lcp->conf_req_len = sizeof(struct lcp_hdr_t); + list_for_each_entry(h,&option_handlers,entry) { lopt=h->init(lcp); @@ -68,7 +72,7 @@ static void lcp_options_free(struct ppp_lcp_t *lcp) static struct ppp_layer_data_t *lcp_layer_init(struct ppp_t *ppp) { - struct ppp_lcp_t *lcp=malloc(sizeof(*lcp)); + struct ppp_lcp_t *lcp=_malloc(sizeof(*lcp)); memset(lcp,0,sizeof(*lcp)); log_ppp_debug("lcp_layer_init\n"); @@ -131,7 +135,7 @@ void lcp_layer_free(struct ppp_layer_data_t *ld) lcp_options_free(lcp); ppp_fsm_free(&lcp->fsm); - free(lcp); + _free(lcp); } static void lcp_layer_up(struct ppp_fsm_t *fsm) @@ -146,6 +150,7 @@ static void lcp_layer_up(struct ppp_fsm_t *fsm) static void lcp_layer_down(struct ppp_fsm_t *fsm) { struct ppp_lcp_t *lcp=container_of(fsm,typeof(*lcp),fsm); + ppp_fsm_close(&lcp->fsm); log_ppp_debug("lcp_layer_finished\n"); stop_echo(lcp); ppp_layer_finished(lcp->ppp,&lcp->ld); @@ -167,7 +172,7 @@ static void print_ropt(struct recv_opt_t *ropt) static int 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; + 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; @@ -207,6 +212,8 @@ static int send_conf_req(struct ppp_fsm_t *fsm) lcp_hdr->len=htons((ptr-buf)-2); ppp_chan_send(lcp->ppp,lcp_hdr,ptr-buf); + _free(buf); + return 0; } @@ -224,7 +231,7 @@ static void send_conf_ack(struct ppp_fsm_t *fsm) 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; + 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; @@ -256,7 +263,7 @@ static void send_conf_nak(struct ppp_fsm_t *fsm) 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; + uint8_t *buf=_malloc(lcp->ropt_len + sizeof(struct lcp_hdr_t)), *ptr=buf; struct lcp_hdr_t *lcp_hdr=(struct lcp_hdr_t*)ptr; struct recv_opt_t *ropt; @@ -285,6 +292,8 @@ static void send_conf_rej(struct ppp_fsm_t *fsm) lcp_hdr->len=htons((ptr-buf)-2); ppp_chan_send(lcp->ppp,lcp_hdr,ptr-buf); + + _free(buf); } static int lcp_recv_conf_req(struct ppp_lcp_t *lcp,uint8_t *data,int size) @@ -300,7 +309,7 @@ static int lcp_recv_conf_req(struct ppp_lcp_t *lcp,uint8_t *data,int size) { hdr=(struct lcp_opt_hdr_t *)data; - ropt=malloc(sizeof(*ropt)); + ropt=_malloc(sizeof(*ropt)); if (hdr->len>size) ropt->len=size; else ropt->len=hdr->len; ropt->hdr=hdr; @@ -362,7 +371,7 @@ static void lcp_free_conf_req(struct ppp_lcp_t *lcp) { ropt=list_entry(lcp->ropt_list.next,typeof(*ropt),entry); list_del(&ropt->entry); - free(ropt); + _free(ropt); } } @@ -594,29 +603,35 @@ static void lcp_recv(struct ppp_handler_t*h) if (lcp_recv_conf_ack(lcp,(uint8_t*)(hdr+1),ntohs(hdr->len)-PPP_HDRLEN)) ppp_terminate(lcp->ppp, 0); else + if (lcp->fsm.recv_id!=lcp->fsm.id) + break; ppp_fsm_recv_conf_ack(&lcp->fsm); break; case CONFNAK: lcp_recv_conf_nak(lcp,(uint8_t*)(hdr+1),ntohs(hdr->len)-PPP_HDRLEN); + if (lcp->fsm.recv_id!=lcp->fsm.id) + break; 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 + if (lcp->fsm.recv_id!=lcp->fsm.id) + break; ppp_fsm_recv_conf_rej(&lcp->fsm); break; case TERMREQ: - term_msg=strndup((char*)(hdr+1),ntohs(hdr->len)-4); + term_msg=_strndup((char*)(hdr+1),ntohs(hdr->len)-4); log_ppp_debug("recv [LCP TermReq id=%x \"%s\"]\n",hdr->id,term_msg); - free(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)-4); + term_msg=_strndup((char*)(hdr+1),ntohs(hdr->len)-4); log_ppp_debug("recv [LCP TermAck id=%x \"%s\"]\n",hdr->id,term_msg); - free(term_msg); + _free(term_msg); ppp_fsm_recv_term_ack(&lcp->fsm); break; case CODEREJ: diff --git a/accel-pptpd/ppp/ppp_pd.c b/accel-pptpd/ppp/ppp_pd.c index fe51bc01..f7702083 100644 --- a/accel-pptpd/ppp/ppp_pd.c +++ b/accel-pptpd/ppp/ppp_pd.c @@ -1,5 +1,7 @@ #include "ppp.h" +#include "memdebug.h" + int ppp_store_pd(struct ppp_t *ppp, pd_key_t key, void *data) { struct ppp_pd_t *pd; diff --git a/accel-pptpd/pwdb.c b/accel-pptpd/pwdb.c index 24515ec6..4c0ab02e 100644 --- a/accel-pptpd/pwdb.c +++ b/accel-pptpd/pwdb.c @@ -4,6 +4,8 @@ #include "pwdb.h" +#include "memdebug.h" + static LIST_HEAD(pwdb_handlers); int __export pwdb_check(struct ppp_t *ppp, const char *username, int type, ...) diff --git a/accel-pptpd/radius/acct.c b/accel-pptpd/radius/acct.c index 72e6df34..6004138d 100644 --- a/accel-pptpd/radius/acct.c +++ b/accel-pptpd/radius/acct.c @@ -10,6 +10,8 @@ #include "log.h" #include "radius_p.h" +#include "memdebug.h" + static int req_set_RA(struct rad_req_t *req, const char *secret) { MD5_CTX ctx; @@ -49,6 +51,9 @@ static int rad_acct_read(struct triton_md_handler_t *h) { struct rad_req_t *req = container_of(h, typeof(*req), hnd); + if (req->reply) + rad_packet_free(req->reply); + req->reply = rad_packet_recv(h->fd, NULL); if (!req->reply) return 0; @@ -63,7 +68,8 @@ static int rad_acct_read(struct triton_md_handler_t *h) req->reply = NULL; } else { req->pack->id++; - triton_timer_del(&req->timeout); + if (req->timeout.tpd) + triton_timer_del(&req->timeout); } return 0; @@ -159,6 +165,12 @@ void rad_acct_stop(struct radius_pd_t *rpd) req_set_stat(rpd->acct_req, rpd->ppp); req_set_RA(rpd->acct_req, conf_acct_secret); /// !!! rad_req_add_val(rpd->acct_req, "Acct-Terminate-Cause", ""); + + if (rpd->acct_req->reply) { + rad_packet_free(rpd->acct_req->reply); + rpd->acct_req->reply = NULL; + } + for(i = 0; i < conf_max_try; i++) { if (rad_req_send(rpd->acct_req)) break; diff --git a/accel-pptpd/radius/auth.c b/accel-pptpd/radius/auth.c index 92406b34..46adbd7a 100644 --- a/accel-pptpd/radius/auth.c +++ b/accel-pptpd/radius/auth.c @@ -9,6 +9,7 @@ #include "radius_p.h" +#include "memdebug.h" static uint8_t* encrypt_password(const char *passwd, const char *secret, const uint8_t *RA, int *epasswd_len) { @@ -19,7 +20,7 @@ static uint8_t* encrypt_password(const char *passwd, const char *secret, const u chunk_cnt = (strlen(passwd) - 1) / 16 + 1; - epasswd = malloc(chunk_cnt * 16); + epasswd = _malloc(chunk_cnt * 16); if (!epasswd) { log_emerg("radius: out of memory\n"); return NULL; @@ -93,11 +94,11 @@ int rad_auth_pap(struct radius_pd_t *rpd, const char *username, va_list args) goto out; if (rad_packet_add_octets(req->pack, "User-Password", epasswd, epasswd_len)) { - free(epasswd); + _free(epasswd); goto out; } - free(epasswd); + _free(epasswd); r = rad_auth_send(req); if (r == PWDB_SUCCESS) { diff --git a/accel-pptpd/radius/dict.c b/accel-pptpd/radius/dict.c index bd7a4ef3..54cf895e 100644 --- a/accel-pptpd/radius/dict.c +++ b/accel-pptpd/radius/dict.c @@ -8,6 +8,8 @@ #include "radius_p.h" #include "log.h" +#include "memdebug.h" + static struct rad_dict_t *dict; static char *skip_word(char *ptr) @@ -216,19 +218,19 @@ int rad_dict_load(const char *fname) INIT_LIST_HEAD(&dict->items); INIT_LIST_HEAD(&dict->vendors); - path = malloc(PATH_MAX); + path = _malloc(PATH_MAX); if (!path) { log_emerg("radius: out of memory\n"); goto out_free_dict; } - fname1 = malloc(PATH_MAX); + fname1 = _malloc(PATH_MAX); if (!fname1) { log_emerg("radius: out of memory\n"); goto out_free_path; } - buf = malloc(BUF_SIZE); + buf = _malloc(BUF_SIZE); if (!buf) { log_emerg("radius: out of memory\n"); goto out_free_fname1; @@ -239,9 +241,9 @@ int rad_dict_load(const char *fname) r = dict_load(fname); out_free_fname1: - free(fname1); + _free(fname1); out_free_path: - free(path); + _free(path); out_free_dict: if (r) rad_dict_free(dict); @@ -258,16 +260,16 @@ void rad_dict_free(struct rad_dict_t *dict) while (!list_empty(&attr->values)) { val = list_entry(attr->values.next, typeof(*val), entry); list_del(&val->entry); - free((char*)val->name); + _free((char*)val->name); if (attr->type == ATTR_TYPE_STRING) - free((char*)val->val.string); - free(val); + _free((char*)val->val.string); + _free(val); } list_del(&attr->entry); - free((char*)attr->name); - free(attr); + _free((char*)attr->name); + _free(attr); } - free(dict); + _free(dict); } static struct rad_dict_attr_t *dict_find_attr(struct list_head *items, const char *name) diff --git a/accel-pptpd/radius/dm_coa.c b/accel-pptpd/radius/dm_coa.c index f515f873..af7c0fe9 100644 --- a/accel-pptpd/radius/dm_coa.c +++ b/accel-pptpd/radius/dm_coa.c @@ -17,6 +17,8 @@ #include "radius_p.h" +#include "memdebug.h" + #define PD_COA_PORT 3799 struct dm_coa_serv_t diff --git a/accel-pptpd/radius/packet.c b/accel-pptpd/radius/packet.c index 9e7df01d..8623f523 100644 --- a/accel-pptpd/radius/packet.c +++ b/accel-pptpd/radius/packet.c @@ -10,11 +10,13 @@ #include "radius_p.h" +#include "memdebug.h" + struct rad_packet_t *rad_packet_alloc(int code) { struct rad_packet_t *pack; - pack = malloc(sizeof(*pack)); + pack = _malloc(sizeof(*pack)); if (!pack) { log_emerg("radius:packet: out of memory\n"); return NULL; @@ -43,9 +45,9 @@ int rad_packet_build(struct rad_packet_t *pack, uint8_t *RA) uint8_t *ptr; if (pack->buf) - ptr = realloc(pack->buf, pack->len); + ptr = _realloc(pack->buf, pack->len); else - ptr = malloc(pack->len); + ptr = _malloc(pack->len); if (!ptr) { log_emerg("radius:packet: out of memory\n"); @@ -105,7 +107,7 @@ struct rad_packet_t *rad_packet_recv(int fd, struct sockaddr_in *addr) if (!pack) return NULL; - pack->buf = malloc(REQ_LENGTH_MAX); + pack->buf = _malloc(REQ_LENGTH_MAX); if (!pack->buf) { log_emerg("radius:packet: out of memory\n"); goto out_err; @@ -169,7 +171,7 @@ struct rad_packet_t *rad_packet_recv(int fd, struct sockaddr_in *addr) vendor = NULL; da = rad_dict_find_attr_id(vendor, id); if (da) { - attr = malloc(sizeof(*attr)); + attr = _malloc(sizeof(*attr)); if (!attr) { log_emerg("radius:packet: out of memory\n"); goto out_err; @@ -180,20 +182,20 @@ struct rad_packet_t *rad_packet_recv(int fd, struct sockaddr_in *addr) attr->len = len; switch (da->type) { case ATTR_TYPE_STRING: - attr->val.string = malloc(len+1); + attr->val.string = _malloc(len+1); if (!attr->val.string) { log_emerg("radius:packet: out of memory\n"); - free(attr); + _free(attr); goto out_err; } memcpy(attr->val.string, ptr, len); attr->val.string[len] = 0; break; case ATTR_TYPE_OCTETS: - attr->val.octets = malloc(len); + attr->val.octets = _malloc(len); if (!attr->val.octets) { log_emerg("radius:packet: out of memory\n"); - free(attr); + _free(attr); goto out_err; } memcpy(attr->val.octets, ptr, len); @@ -225,17 +227,18 @@ void rad_packet_free(struct rad_packet_t *pack) struct rad_attr_t *attr; if (pack->buf) - free(pack->buf); + _free(pack->buf); while(!list_empty(&pack->attrs)) { attr = list_entry(pack->attrs.next, typeof(*attr), entry); - if (attr->attr->type == ATTR_TYPE_STRING || attr->attr->type == ATTR_TYPE_OCTETS) - free(attr->val.string); + log_ppp_debug("free: %s\n", attr->attr->name); list_del(&attr->entry); - free(attr); + if (attr->attr->type == ATTR_TYPE_STRING || attr->attr->type == ATTR_TYPE_OCTETS) + _free(attr->val.string); + _free(attr); } - free(pack); + _free(pack); } void rad_packet_print(struct rad_packet_t *pack, void (*print)(const char *fmt, ...)) @@ -323,7 +326,7 @@ int rad_packet_add_int(struct rad_packet_t *pack, const char *name, int val) if (!attr) return -1; - ra = malloc(sizeof(*ra)); + ra = _malloc(sizeof(*ra)); if (!ra) return -1; @@ -362,7 +365,7 @@ int rad_packet_add_octets(struct rad_packet_t *pack, const char *name, uint8_t * if (!attr) return -1; - ra = malloc(sizeof(*ra)); + ra = _malloc(sizeof(*ra)); if (!ra) { log_emerg("radius: out of memory\n"); return -1; @@ -371,10 +374,10 @@ int rad_packet_add_octets(struct rad_packet_t *pack, const char *name, uint8_t * memset(ra, 0, sizeof(*ra)); ra->attr = attr; ra->len = len; - ra->val.octets = malloc(len); + ra->val.octets = _malloc(len); if (!ra->val.octets) { log_emerg("radius: out of memory\n"); - free(ra); + _free(ra); return -1; } memcpy(ra->val.octets, val, len); @@ -395,7 +398,7 @@ int rad_packet_add_str(struct rad_packet_t *pack, const char *name, const char * if (!attr) return -1; - ra = malloc(sizeof(*ra)); + ra = _malloc(sizeof(*ra)); if (!ra) { log_emerg("radius: out of memory\n"); return -1; @@ -404,10 +407,10 @@ int rad_packet_add_str(struct rad_packet_t *pack, const char *name, const char * memset(ra, 0, sizeof(*ra)); ra->attr = attr; ra->len = len; - ra->val.string = malloc(len+1); + ra->val.string = _malloc(len+1); if (!ra->val.string) { log_emerg("radius: out of memory\n"); - free(ra); + _free(ra); return -1; } memcpy(ra->val.string, val, len); @@ -430,7 +433,7 @@ int rad_packet_change_str(struct rad_packet_t *pack, const char *name, const cha if (pack->len - ra->len + len >= REQ_LENGTH_MAX) return -1; - ra->val.string = realloc(ra->val.string, len + 1); + ra->val.string = _realloc(ra->val.string, len + 1); if (!ra->val.string) { log_emerg("radius: out of memory\n"); return -1; @@ -463,7 +466,7 @@ int rad_packet_add_val(struct rad_packet_t *pack, const char *name, const char * if (!v) return -1; - ra = malloc(sizeof(*ra)); + ra = _malloc(sizeof(*ra)); if (!ra) return -1; @@ -547,7 +550,7 @@ int rad_packet_add_vendor_octets(struct rad_packet_t *pack, const char *vendor_n if (!attr) return -1; - ra = malloc(sizeof(*ra)); + ra = _malloc(sizeof(*ra)); if (!ra) { log_emerg("radius: out of memory\n"); return -1; @@ -557,10 +560,10 @@ int rad_packet_add_vendor_octets(struct rad_packet_t *pack, const char *vendor_n ra->vendor = vendor; ra->attr = attr; ra->len = len; - ra->val.octets = malloc(len); + ra->val.octets = _malloc(len); if (!ra->val.octets) { log_emerg("radius: out of memory\n"); - free(ra); + _free(ra); return -1; } memcpy(ra->val.octets, val, len); diff --git a/accel-pptpd/radius/radius.c b/accel-pptpd/radius/radius.c index 04e41332..8a5e7b85 100644 --- a/accel-pptpd/radius/radius.c +++ b/accel-pptpd/radius/radius.c @@ -13,6 +13,8 @@ #include "radius_p.h" +#include "memdebug.h" + #define CHAP_MD5 5 #define MSCHAP_V1 0x80 #define MSCHAP_V2 0x81 @@ -102,7 +104,7 @@ static struct ipdb_item_t *get_ip(struct ppp_t *ppp) static void ppp_starting(struct ppp_t *ppp) { - struct radius_pd_t *pd = malloc(sizeof(*pd)); + struct radius_pd_t *pd = _malloc(sizeof(*pd)); memset(pd, 0, sizeof(*pd)); pd->pd.key = &pd_key; @@ -138,11 +140,14 @@ static void ppp_finished(struct ppp_t *ppp) pthread_mutex_unlock(&rpd->lock); pthread_rwlock_unlock(&sessions_lock); + if (rpd->acct_req) + rad_req_free(rpd->acct_req); + if (rpd->dm_coa_req) rad_packet_free(rpd->dm_coa_req); list_del(&rpd->pd.entry); - free(rpd); + _free(rpd); } struct radius_pd_t *find_pd(struct ppp_t *ppp) @@ -242,7 +247,7 @@ static struct pwdb_t pwdb = { static int parse_server(const char *opt, char **name, int *port, char **secret) { - char *str = strdup(opt); + char *str = _strdup(opt); char *p1, *p2; p1 = strstr(str, ":"); diff --git a/accel-pptpd/radius/req.c b/accel-pptpd/radius/req.c index a9192bb4..ac41f199 100644 --- a/accel-pptpd/radius/req.c +++ b/accel-pptpd/radius/req.c @@ -11,6 +11,8 @@ #include "log.h" #include "radius_p.h" +#include "memdebug.h" + static int urandom_fd; static int rad_req_read(struct triton_md_handler_t *h); @@ -18,7 +20,7 @@ static void rad_req_timeout(struct triton_timer_t *t); struct rad_req_t *rad_req_alloc(struct radius_pd_t *rpd, int code, const char *username) { - struct rad_req_t *req = malloc(sizeof(*req)); + struct rad_req_t *req = _malloc(sizeof(*req)); if (!req) return NULL; @@ -101,7 +103,7 @@ void rad_req_free(struct rad_req_t *req) rad_packet_free(req->pack); if (req->reply) rad_packet_free(req->reply); - free(req); + _free(req); } static int make_socket(struct rad_req_t *req) diff --git a/accel-pptpd/sigchld.c b/accel-pptpd/sigchld.c index 478f44c7..3843e4f7 100644 --- a/accel-pptpd/sigchld.c +++ b/accel-pptpd/sigchld.c @@ -11,32 +11,57 @@ #include "sigchld.h" +#include "memdebug.h" + static LIST_HEAD(handlers); -static int refs; -static int sleeping = 1; +static int lock_refs; static pthread_mutex_t handlers_lock = PTHREAD_MUTEX_INITIALIZER; -static pthread_mutex_t refs_lock = PTHREAD_MUTEX_INITIALIZER; - -static struct triton_context_t sigchld_ctx; +static pthread_cond_t refs_cond = PTHREAD_COND_INITIALIZER; +static pthread_t sigchld_thr; -static void sigchld_handler(void *arg) +static void* sigchld_thread(void *arg) { + sigset_t set; struct sigchld_handler_t *h, *h0; pid_t pid; - int status; + int status, sig; + + sigfillset(&set); + pthread_sigmask(SIG_BLOCK, &set, NULL); + + sigemptyset(&set); + sigaddset(&set, SIGUSR1); + sigaddset(&set, SIGQUIT); + sigaddset(&set, SIGSEGV); + sigaddset(&set, SIGFPE); + sigaddset(&set, SIGILL); + sigaddset(&set, SIGBUS); + sigaddset(&set, SIGCHLD); + pthread_sigmask(SIG_UNBLOCK, &set, NULL); + + sigemptyset(&set); + sigaddset(&set, SIGCHLD); + sigaddset(&set, SIGQUIT); while (1) { - pid = waitpid(0, &status, WNOHANG); - pthread_mutex_lock(&handlers_lock); - if (pid == 0 || (pid == -1 && errno == ECHILD)) { - sleeping = 1; - pthread_mutex_unlock(&handlers_lock); - return; - } else if (pid < 0) { - pthread_mutex_unlock(&handlers_lock); + pid = waitpid(0, &status, 0); + if (pid < 0) { + if (errno == EINTR) + continue; + if (errno == ECHILD) { + sigwait(&set, &sig); + if (sig == SIGQUIT) + break; + continue; + } log_error("sigchld: waitpid: %s\n", strerror(errno)); - return; + continue; } + + pthread_mutex_lock(&handlers_lock); + while (lock_refs) + pthread_cond_wait(&refs_cond, &handlers_lock); + h0 = NULL; list_for_each_entry(h, &handlers, entry) { if (h->pid == pid) { @@ -53,6 +78,8 @@ static void sigchld_handler(void *arg) pthread_mutex_unlock(&h0->lock); } } + + return NULL; } void __export sigchld_register_handler(struct sigchld_handler_t *h) @@ -78,57 +105,21 @@ void __export sigchld_unregister_handler(struct sigchld_handler_t *h) void __export sigchld_lock() { - sigset_t set; - - pthread_mutex_lock(&refs_lock); - if (refs == 0) { - sigemptyset(&set); - sigaddset(&set, SIGCHLD); - sigprocmask(SIG_BLOCK, &set, NULL); - } - ++refs; - pthread_mutex_unlock(&refs_lock); + pthread_mutex_lock(&handlers_lock); + ++lock_refs; + pthread_mutex_unlock(&handlers_lock); } void __export sigchld_unlock() { - sigset_t set; - - pthread_mutex_lock(&refs_lock); - if (refs == 1) { - sigemptyset(&set); - sigaddset(&set, SIGCHLD); - sigprocmask(SIG_UNBLOCK, &set, NULL); - } - --refs; - pthread_mutex_unlock(&refs_lock); - -} - -static void sigchld(int num) -{ - int s; - pthread_mutex_lock(&handlers_lock); - s = sleeping; - sleeping = 0; + if (--lock_refs == 0) + pthread_cond_signal(&refs_cond); pthread_mutex_unlock(&handlers_lock); - - if (s) - triton_context_call(&sigchld_ctx, sigchld_handler, NULL); } static void __init init(void) { - struct sigaction sa_sigchld = { - .sa_handler = sigchld, - .sa_flags = SA_NOCLDSTOP, - }; - - if (sigaction(SIGCHLD, &sa_sigchld, NULL)) { - fprintf(stderr, "sigchld: sigaction: %s\n", strerror(errno)); - return; - } - - triton_context_register(&sigchld_ctx, NULL); + if (pthread_create(&sigchld_thr, NULL, sigchld_thread, NULL)) + fprintf(stderr, "sigchld: pthread_create: %s\n", strerror(errno)); } diff --git a/accel-pptpd/triton/conf_file.c b/accel-pptpd/triton/conf_file.c index c6ba7453..6eb2e7a8 100644 --- a/accel-pptpd/triton/conf_file.c +++ b/accel-pptpd/triton/conf_file.c @@ -5,6 +5,8 @@ #include "triton_p.h" +#include "memdebug.h" + struct sect_t { struct list_head entry; @@ -35,15 +37,14 @@ int conf_load(const char *fname) return -1; } - buf = malloc(1024); - path0 = malloc(4096); - path = malloc(4096); + buf = _malloc(1024); + path0 = _malloc(4096); + path = _malloc(4096); getcwd(path0, 1024); while(!feof(f)) { - buf = fgets(buf, 1024, f); - if (!buf) + if (!fgets(buf, 1024, f)) break; ++cur_line; if (buf[strlen(buf) - 1] == '\n') @@ -103,9 +104,9 @@ int conf_load(const char *fname) sect_add_item(cur_sect, str, str2); } - free(buf); - free(path); - free(path0); + _free(buf); + _free(path); + _free(path0); fclose(f); return 0; @@ -132,9 +133,9 @@ static struct conf_sect_t *find_sect(const char *name) static struct conf_sect_t *create_sect(const char *name) { - struct sect_t *s = malloc(sizeof(struct sect_t)); + struct sect_t *s = _malloc(sizeof(struct sect_t)); - s->sect = malloc(sizeof(struct conf_sect_t)); + s->sect = _malloc(sizeof(struct conf_sect_t)); s->sect->name = (char*)strdup(name); INIT_LIST_HEAD(&s->sect->items); @@ -145,10 +146,10 @@ static struct conf_sect_t *create_sect(const char *name) static void sect_add_item(struct conf_sect_t *sect, const char *name, const char *val) { - struct conf_option_t *opt = malloc(sizeof(struct conf_option_t)); + struct conf_option_t *opt = _malloc(sizeof(struct conf_option_t)); - opt->name = strdup(name); - opt->val = val ? strdup(val) : NULL; + opt->name = _strdup(name); + opt->val = val ? _strdup(val) : NULL; list_add_tail(&opt->entry, §->items); } diff --git a/accel-pptpd/triton/event.c b/accel-pptpd/triton/event.c index 442543f2..d45eca01 100644 --- a/accel-pptpd/triton/event.c +++ b/accel-pptpd/triton/event.c @@ -4,6 +4,8 @@ #include "triton_p.h" +#include "memdebug.h" + static int max_events = 1024; static struct _triton_event_t **events; @@ -76,7 +78,7 @@ int __export triton_event_register_handler(int ev_id, triton_event_func func) h->func = NULL; else { list_del(&h->entry); - free(h); + _free(h); } return 0; } diff --git a/accel-pptpd/triton/loader.c b/accel-pptpd/triton/loader.c index 24d1cbcb..a8b9c500 100644 --- a/accel-pptpd/triton/loader.c +++ b/accel-pptpd/triton/loader.c @@ -7,6 +7,8 @@ #include "triton_p.h" +#include "memdebug.h" + int load_modules(const char *name) { struct conf_sect_t *sect; @@ -40,7 +42,7 @@ int load_modules(const char *name) out_err: chdir(cwd); - free(cwd); + _free(cwd); return -1; } diff --git a/accel-pptpd/triton/log.c b/accel-pptpd/triton/log.c index 4504db4f..7bb55598 100644 --- a/accel-pptpd/triton/log.c +++ b/accel-pptpd/triton/log.c @@ -3,6 +3,8 @@ #include "triton_p.h" +#include "memdebug.h" + static FILE *f_error; static FILE *f_debug; static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; diff --git a/accel-pptpd/triton/md.c b/accel-pptpd/triton/md.c index 05d814cc..06b2b30b 100644 --- a/accel-pptpd/triton/md.c +++ b/accel-pptpd/triton/md.c @@ -8,6 +8,8 @@ #include "triton_p.h" +#include "memdebug.h" + extern int max_events; static int epoll_fd; @@ -26,7 +28,7 @@ int md_init(void) return -1; } - epoll_events = malloc(max_events * sizeof(struct epoll_event)); + epoll_events = _malloc(max_events * sizeof(struct epoll_event)); if (!epoll_events) { fprintf(stderr,"md:cann't allocate memory\n"); return -1; @@ -54,6 +56,20 @@ static void *md_thread(void *arg) { int i,n,r; struct _triton_md_handler_t *h; + sigset_t set; + + sigfillset(&set); + pthread_sigmask(SIG_BLOCK, &set, NULL); + + sigemptyset(&set); + sigaddset(&set, SIGQUIT); + sigaddset(&set, SIGSEGV); + sigaddset(&set, SIGFPE); + sigaddset(&set, SIGILL); + sigaddset(&set, SIGBUS); + sigdelset(&set, 35); + sigdelset(&set, 36); + pthread_sigmask(SIG_UNBLOCK, &set, NULL); while(1) { n = epoll_wait(epoll_fd, epoll_events, max_events, -1); @@ -100,6 +116,8 @@ void __export triton_md_register_handler(struct triton_context_t *ctx, struct tr spin_lock(&h->ctx->lock); list_add_tail(&h->entry, &h->ctx->handlers); spin_unlock(&h->ctx->lock); + + __sync_fetch_and_add(&triton_stat.md_handler_count, 1); } void __export triton_md_unregister_handler(struct triton_md_handler_t *ud) { @@ -113,6 +131,8 @@ void __export triton_md_unregister_handler(struct triton_md_handler_t *ud) spin_unlock(&h->ctx->lock); sched_yield(); mempool_free(h); + + __sync_fetch_and_sub(&triton_stat.md_handler_count, 1); } int __export triton_md_enable_handler(struct triton_md_handler_t *ud, int mode) { diff --git a/accel-pptpd/triton/mempool.c b/accel-pptpd/triton/mempool.c index 6d9172a1..34739b43 100644 --- a/accel-pptpd/triton/mempool.c +++ b/accel-pptpd/triton/mempool.c @@ -1,11 +1,15 @@ #include #include #include +#include #include "triton_p.h" +#include "memdebug.h" + struct _mempool_t { + struct list_head entry; int size; struct list_head items; spinlock_t lock; @@ -20,9 +24,12 @@ struct _item_t char ptr[0]; }; +static LIST_HEAD(pools); +static spinlock_t pools_lock = SPINLOCK_INITIALIZER; + __export mempool_t *mempool_create(int size) { - struct _mempool_t *p = malloc(sizeof(*p)); + struct _mempool_t *p = _malloc(sizeof(*p)); memset(p, 0, sizeof(*p)); INIT_LIST_HEAD(&p->items); @@ -30,35 +37,82 @@ __export mempool_t *mempool_create(int size) p->size = size; p->magic = (uint64_t)random() * (uint64_t)random(); + spin_lock(&pools_lock); + list_add_tail(&p->entry, &pools); + spin_unlock(&pools_lock); + return (mempool_t *)p; } +#ifndef MEMDEBUG __export void *mempool_alloc(mempool_t *pool) { struct _mempool_t *p = (struct _mempool_t *)pool; struct _item_t *it; + uint32_t size = sizeof(*it) + p->size; + + spin_lock(&p->lock); + if (!list_empty(&p->items)) { + it = list_entry(p->items.next, typeof(*it), entry); + list_del(&it->entry); + spin_unlock(&p->lock); + + __sync_fetch_and_sub(&triton_stat.mempool_available, size); + + return it->ptr; + } + spin_unlock(&p->lock); + + it = _malloc(size); + if (!it) { + triton_log_error("mempool: out of memory\n"); + return NULL; + } + it->owner = p; + it->magic = p->magic; + + __sync_fetch_and_add(&triton_stat.mempool_allocated, size); + + return it->ptr; +} +#endif + +void __export *mempool_alloc_md(mempool_t *pool, const char *fname, int line) +{ + struct _mempool_t *p = (struct _mempool_t *)pool; + struct _item_t *it; + uint32_t size = sizeof(*it) + p->size; spin_lock(&p->lock); if (!list_empty(&p->items)) { it = list_entry(p->items.next, typeof(*it), entry); list_del(&it->entry); spin_unlock(&p->lock); + + __sync_fetch_and_sub(&triton_stat.mempool_available, size); + return it->ptr; } spin_unlock(&p->lock); - it = malloc(sizeof(*it) + p->size); + + it = md_malloc(size, fname, line); if (!it) { triton_log_error("mempool: out of memory\n"); return NULL; } it->owner = p; it->magic = p->magic; + + __sync_fetch_and_add(&triton_stat.mempool_allocated, size); + return it->ptr; } + __export void mempool_free(void *ptr) { struct _item_t *it = container_of(ptr, typeof(*it), ptr); + uint32_t size = sizeof(*it) + it->owner->size; if (it->magic != it->owner->magic) { triton_log_error("mempool: memory corruption detected"); @@ -67,5 +121,36 @@ __export void mempool_free(void *ptr) spin_lock(&it->owner->lock); list_add_tail(&it->entry,&it->owner->items); spin_unlock(&it->owner->lock); + + __sync_fetch_and_add(&triton_stat.mempool_available, size); +} + +void sigclean(int num) +{ + struct _mempool_t *p; + struct _item_t *it; + uint32_t size; + + triton_log_error("mempool: clean\n"); + + spin_lock(&pools_lock); + list_for_each_entry(p, &pools, entry) { + size = sizeof(*it) + p->size; + spin_lock(&p->lock); + while (!list_empty(&p->items)) { + it = list_entry(p->items.next, typeof(*it), entry); + list_del(&it->entry); + _free(it); + __sync_fetch_and_sub(&triton_stat.mempool_allocated, size); + __sync_fetch_and_sub(&triton_stat.mempool_available, size); + } + spin_unlock(&p->lock); + } + spin_unlock(&pools_lock); +} + +static void __init init(void) +{ + signal(35, sigclean); } diff --git a/accel-pptpd/triton/mempool.h b/accel-pptpd/triton/mempool.h index e8bcaf6a..d3539215 100644 --- a/accel-pptpd/triton/mempool.h +++ b/accel-pptpd/triton/mempool.h @@ -1,10 +1,25 @@ #ifndef __TRITON_MEMPOOL_H #define __TRITON_MEMPOOL_H +#include + +struct mempool_stat_t +{ + uint32_t allocated; + uint32_t available; +}; + typedef void * mempool_t; mempool_t *mempool_create(int size); -void *mempool_alloc(mempool_t*); void mempool_free(void*); +struct mempool_stat_t mempool_get_stat(void); + +#ifdef MEMDEBUG +void *mempool_alloc_md(mempool_t*, const char *fname, int line); +#define mempool_alloc(pool) mempool_alloc_md(pool, __FILE__, __LINE__) +#else +void *mempool_alloc(mempool_t*); +#endif #endif diff --git a/accel-pptpd/triton/options.c b/accel-pptpd/triton/options.c index ba7fc564..a5214e21 100644 --- a/accel-pptpd/triton/options.c +++ b/accel-pptpd/triton/options.c @@ -4,6 +4,8 @@ #include "triton_p.h" #include "conf_file.h" +#include "memdebug.h" + static struct conf_file_sect_t *sect=NULL; static const char* find_option(const char *name) diff --git a/accel-pptpd/triton/timer.c b/accel-pptpd/triton/timer.c index d2249786..53abd3b9 100644 --- a/accel-pptpd/triton/timer.c +++ b/accel-pptpd/triton/timer.c @@ -9,6 +9,8 @@ #include "triton_p.h" +#include "memdebug.h" + extern int max_events; static int epoll_fd; static struct epoll_event *epoll_events; @@ -26,7 +28,7 @@ int timer_init(void) return -1; } - epoll_events = malloc(max_events * sizeof(struct epoll_event)); + epoll_events = _malloc(max_events * sizeof(struct epoll_event)); if (!epoll_events) { fprintf(stderr,"timer:cann't allocate memory\n"); return -1; @@ -55,7 +57,19 @@ void *timer_thread(void *arg) { int i,n,r; struct _triton_timer_t *t; - + sigset_t set; + + sigfillset(&set); + pthread_sigmask(SIG_BLOCK, &set, NULL); + + sigemptyset(&set); + sigaddset(&set, SIGQUIT); + sigaddset(&set, SIGSEGV); + sigaddset(&set, SIGFPE); + sigaddset(&set, SIGILL); + sigaddset(&set, SIGBUS); + pthread_sigmask(SIG_UNBLOCK, &set, NULL); + while(1) { n = epoll_wait(epoll_fd, epoll_events, max_events, -1); if (n < 0) { @@ -129,6 +143,8 @@ int __export triton_timer_add(struct triton_context_t *ctx, struct triton_timer_ return -1; } + __sync_fetch_and_add(&triton_stat.timer_count, 1); + return 0; } int __export triton_timer_mod(struct triton_timer_t *ud,int abs_time) @@ -165,5 +181,7 @@ void __export triton_timer_del(struct triton_timer_t *ud) sched_yield(); mempool_free(t); ud->tpd = NULL; + + __sync_fetch_and_sub(&triton_stat.timer_count, 1); } diff --git a/accel-pptpd/triton/triton.c b/accel-pptpd/triton/triton.c index 9b9fd753..95930647 100644 --- a/accel-pptpd/triton/triton.c +++ b/accel-pptpd/triton/triton.c @@ -6,6 +6,7 @@ #include #include "triton_p.h" +#include "memdebug.h" int thread_count = 1; int max_events = 64; @@ -25,6 +26,8 @@ static int terminate; static mempool_t *ctx_pool; static mempool_t *call_pool; +__export struct triton_stat_t triton_stat; + void triton_thread_wakeup(struct _triton_thread_t *thread) { pthread_kill(thread->thread, SIGUSR1); @@ -33,25 +36,21 @@ void triton_thread_wakeup(struct _triton_thread_t *thread) static void* triton_thread(struct _triton_thread_t *thread) { sigset_t set; - int sig; sigfillset(&set); - pthread_sigmask(SIG_BLOCK, &set, NULL); - - sigdelset(&set, SIGUSR1); - sigdelset(&set, SIGQUIT); sigdelset(&set, SIGSEGV); sigdelset(&set, SIGFPE); sigdelset(&set, SIGILL); sigdelset(&set, SIGBUS); - pthread_sigmask(SIG_UNBLOCK, &set, NULL); + pthread_sigmask(SIG_SETMASK, &set, NULL); - sigemptyset(&set); - sigaddset(&set, SIGUSR1); - sigaddset(&set, SIGQUIT); + sigdelset(&set, SIGUSR1); + sigdelset(&set, SIGQUIT); while (1) { - sigwait(&set, &sig); + __sync_fetch_and_sub(&triton_stat.thread_active, 1); + sigsuspend(&set); + __sync_fetch_and_add(&triton_stat.thread_active, 1); cont: if (thread->ctx->ud->before_switch) @@ -72,6 +71,7 @@ cont: thread->ctx->thread = thread; thread->ctx->queued = 0; spin_unlock(&thread->ctx->lock); + __sync_fetch_and_sub(&triton_stat.context_pending, 1); goto cont; } else { if (!terminate) @@ -142,7 +142,7 @@ static void ctx_thread(struct _triton_context_t *ctx) struct _triton_thread_t *create_thread() { - struct _triton_thread_t *thread = malloc(sizeof(*thread)); + struct _triton_thread_t *thread = _malloc(sizeof(*thread)); if (!thread) return NULL; @@ -152,6 +152,9 @@ struct _triton_thread_t *create_thread() return NULL; } + triton_stat.thread_count++; + triton_stat.thread_active++; + return thread; } @@ -165,6 +168,7 @@ int triton_queue_ctx(struct _triton_context_t *ctx) list_add_tail(&ctx->entry2, &ctx_queue); spin_unlock(&threads_lock); ctx->queued = 1; + __sync_fetch_and_add(&triton_stat.context_pending, 1); return 0; } @@ -195,15 +199,15 @@ int __export triton_context_register(struct triton_context_t *ud, void *bf_arg) if (getcontext(&ctx->uctx)) { triton_log_error("getcontext: %s\n", strerror(errno)); - free(ctx); + _free(ctx); return -1; } ctx->uctx.uc_stack.ss_size = CTX_STACK_SIZE; - ctx->uctx.uc_stack.ss_sp = malloc(CTX_STACK_SIZE); + ctx->uctx.uc_stack.ss_sp = _malloc(CTX_STACK_SIZE); if (!ctx->uctx.uc_stack.ss_sp) { triton_log_error("out of memory\n"); - free(ctx); + _free(ctx); return -1; } makecontext(&ctx->uctx, (void (*)())ctx_thread, 1, ctx); @@ -214,12 +218,21 @@ int __export triton_context_register(struct triton_context_t *ud, void *bf_arg) list_add_tail(&ctx->entry, &ctx_list); spin_unlock(&ctx_list_lock); + __sync_fetch_and_add(&triton_stat.context_count, 1); + return 0; } void __export triton_context_unregister(struct triton_context_t *ud) { struct _triton_context_t *ctx = (struct _triton_context_t *)ud->tpd; + struct _triton_ctx_call_t *call; + + while (!list_empty(&ctx->pending_calls)) { + call = list_entry(ctx->pending_calls.next, typeof(*call), entry); + list_del(&call->entry); + mempool_free(call); + } if (!list_empty(&ctx->handlers)) { triton_log_error("BUG:ctx:triton_unregister_ctx: handlers is not empty"); @@ -246,10 +259,14 @@ void __export triton_context_unregister(struct triton_context_t *ud) abort(); } + _free(ctx->uctx.uc_stack.ss_sp); + ctx->need_free = 1; spin_lock(&ctx_list_lock); list_del(&ctx->entry); spin_unlock(&ctx_list_lock); + + __sync_fetch_and_sub(&triton_stat.context_count, 1); } void __export triton_context_schedule(struct triton_context_t *ud) { @@ -263,6 +280,8 @@ void __export triton_context_schedule(struct triton_context_t *ud) if (swapcontext(&ctx->uctx, uctx)) triton_log_error("swaswpntext: %s\n", strerror(errno)); + + __sync_fetch_and_add(&triton_stat.context_sleeping, 1); } void __export triton_context_wakeup(struct triton_context_t *ud) @@ -277,6 +296,8 @@ void __export triton_context_wakeup(struct triton_context_t *ud) if (r) triton_thread_wakeup(ctx->thread); + + __sync_fetch_and_sub(&triton_stat.context_sleeping, 1); } int __export triton_context_call(struct triton_context_t *ud, void (*func)(void *), void *arg) @@ -302,12 +323,12 @@ int __export triton_context_call(struct triton_context_t *ud, void (*func)(void return 0; } -int __export triton_init(const char *conf_file, const char *mod_sect) +int __export triton_init(const char *conf_file) { ctx_pool = mempool_create(sizeof(struct _triton_context_t)); call_pool = mempool_create(sizeof(struct _triton_ctx_call_t)); - default_ctx = malloc(sizeof(*default_ctx)); + default_ctx = _malloc(sizeof(*default_ctx)); if (!default_ctx) { fprintf(stderr,"cann't allocate memory\n"); return -1; @@ -329,9 +350,14 @@ int __export triton_init(const char *conf_file, const char *mod_sect) if (event_init()) return -1; + return 0; +} + +int __export triton_load_modules(const char *mod_sect) +{ if (load_modules(mod_sect)) return -1; - + return 0; } diff --git a/accel-pptpd/triton/triton.h b/accel-pptpd/triton/triton.h index 8510a512..c3c9d2a9 100644 --- a/accel-pptpd/triton/triton.h +++ b/accel-pptpd/triton/triton.h @@ -2,6 +2,7 @@ #define TRITON_H #include +#include #include "list.h" @@ -49,6 +50,22 @@ struct conf_sect_t struct list_head items; }; +struct triton_stat_t +{ + uint32_t mempool_allocated; + uint32_t mempool_available; + uint32_t thread_count; + uint32_t thread_active; + uint32_t context_count; + uint32_t context_sleeping; + uint32_t context_pending; + uint32_t md_handler_count; + uint32_t md_handler_pending; + uint32_t timer_count; + uint32_t timer_pending; +}; + +extern struct triton_stat_t triton_stat; int triton_context_register(struct triton_context_t *, void *arg); void triton_context_unregister(struct triton_context_t *); void triton_context_schedule(struct triton_context_t *); @@ -82,7 +99,8 @@ 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, const char *mod_sect); +int triton_init(const char *conf_file); +int triton_load_modules(const char *md_sect); void triton_run(void); void triton_terminate(void); diff --git a/accel-pptpd/utils.c b/accel-pptpd/utils.c index fd59b005..491e6507 100644 --- a/accel-pptpd/utils.c +++ b/accel-pptpd/utils.c @@ -3,6 +3,7 @@ #include "triton.h" #include "utils.h" +#include "memdebug.h" void __export u_inet_ntoa(in_addr_t addr, char *str) { -- cgit v1.2.3