提交 29bfdacc 编写于 作者: K Kozlov Dmitry 提交者: Dmitry Kozlov

implemented base IPCP module

implemented IPCP IP-Address configuration option
上级 1dbf2a28
......@@ -11,15 +11,21 @@ ADD_EXECUTABLE(pptpd
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
ppp_ccp.c
ppp_ipcp.c
auth_pap.c
ipcp_opt_ipaddr.c
pwdb.c
ipdb.c
)
TARGET_LINK_LIBRARIES(pptpd pthread triton)
......@@ -114,7 +114,7 @@ static void pap_send_ack(struct pap_auth_data_t *p, int id)
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));
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));
......@@ -130,7 +130,7 @@ static void pap_send_nak(struct pap_auth_data_t *p,int id)
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));
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));
......
//
// C++ Interface: events
//
// Description:
//
//
// Author: <xeb@mail.ru>, (C) 2009
//
// Copyright: See COPYING file that comes with this distribution
//
//
#ifndef EVENTS_H
#define EVENTS_H
#define EV_PPP_PACKET 1
#endif
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#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 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;
if (ipaddr_opt->peer_addr==opt32->val)
return IPCP_OPT_ACK;
if (!ipaddr_opt->peer_addr)
{
ipaddr_opt->peer_addr=opt32->val;
return IPCP_OPT_ACK;
}
return IPCP_OPT_NAK;
}
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("<addr %s>",inet_ntoa(in));
}
static void __init ipaddr_opt_init()
{
ipcp_option_register(&ipaddr_opt_hnd);
}
#include "ipdb.h"
int 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");
return 0;
}
#ifndef IPDB_H
#define IPDB_H
#include <netinet/in.h>
int ipdb_get(in_addr_t *addr, in_addr_t *peer_addr);
#endif
#include "ppp_lcp.h"
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);
struct mru_option_t
{
struct lcp_option_t opt;
int mru;
int mtu;
};
static struct lcp_option_handler_t opt_mru=
{
.id=CI_MRU,
.init=mru_init,
.send_conf_req=mru_send_conf_req,
.send_conf_nak=mru_send_conf_nak,
.recv_conf_req=mru_recv_conf_req,
.free=mru_free,
};
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=1500;
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.type=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.type=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;
}
......@@ -15,7 +15,6 @@
#include "ppp.h"
#include "ppp_fsm.h"
#include "log.h"
#include "events.h"
static LIST_HEAD(layers);
......@@ -158,8 +157,8 @@ int ppp_chan_send(struct ppp_t *ppp, void *data, int size)
{
int n;
printf("ppp_chan_send: ");
print_buf((uint8_t*)data,size);
//printf("ppp_chan_send: ");
//print_buf((uint8_t*)data,size);
n=write(ppp->chan_fd,data,size);
if (n<size)
......@@ -171,8 +170,8 @@ int ppp_unit_send(struct ppp_t *ppp, void *data, int size)
{
int n;
printf("ppp_unit_send: ");
print_buf((uint8_t*)data,size);
//printf("ppp_unit_send: ");
//print_buf((uint8_t*)data,size);
n=write(ppp->unit_fd,data,size);
if (n<size)
......@@ -188,8 +187,8 @@ static void ppp_chan_read(struct triton_md_handler_t*h)
ppp->chan_buf_size=read(h->fd,ppp->chan_buf,PPP_MRU);
printf("ppp_chan_read: ");
print_buf(ppp->chan_buf,ppp->chan_buf_size);
//printf("ppp_chan_read: ");
//print_buf(ppp->chan_buf,ppp->chan_buf_size);
if (ppp->chan_buf_size<2)
{
......@@ -266,28 +265,26 @@ void ppp_layer_finished(struct ppp_t *ppp, struct ppp_layer_data_t *d)
d->started=0;
list_for_each_entry(d,&n->items,entry)
if (d->started) return;
if (n->entry.prev==&ppp->layers) destablish_ppp(ppp);
else
list_for_each_entry(n,&ppp->layers,entry)
{
n=list_entry(n->entry.prev,typeof(*n),entry);
list_for_each_entry(d,&n->items,entry)
if (d->started) d->layer->finish(d);
{
if (d->started)
return;
}
}
destablish_ppp(ppp);
}
void ppp_terminate(struct ppp_t *ppp)
{
struct layer_node_t *n;
struct list_head *p;
struct ppp_layer_data_t *d;
int s=0;
log_debug("ppp_terminate\n");
list_for_each_prev(p,&ppp->layers)
list_for_each_entry(n,&ppp->layers,entry)
{
list_for_each_entry(d,&n->items,entry)
{
......@@ -297,8 +294,8 @@ void ppp_terminate(struct ppp_t *ppp)
d->layer->finish(d);
}
}
if (s) return;
}
if (s) return;
destablish_ppp(ppp);
}
......
#include <stdlib.h>
#include <string.h>
#include <linux/ppp_defs.h>
#include <linux/if_ppp.h>
#include <arpa/inet.h>
#include "triton/triton.h"
#include "log.h"
#include "ppp.h"
#include "ppp_ipcp.h"
int ipcp_start(struct ppp_t *ppp)
struct recv_opt_t
{
return 0;
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_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; i<ropt->len; 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 ipcp_option_t *lopt;
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(lopt,&ipcp->options,entry)
{
if (lopt->state==IPCP_OPT_NAK)
{
log_debug(" ");
lopt->h->print(log_debug,lopt,NULL);
ptr+=lopt->h->send_conf_nak(ipcp,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 (r<ret) ret=r;
}
}
if (!ropt->lopt)
{
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 (r<ret) ret=r;
}
}*/
return ret;
}
static void ipcp_free_conf_req(struct ppp_ipcp_t *ipcp)
{
struct recv_opt_t *ropt;
while(!list_empty(&ipcp->ropt_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(ipcp,lopt,data))
res=-1;
break;
}
}
data+=hdr->len;
size-=hdr->len;
}
log_debug("]\n");
return res;
}
void ipcp_finish(struct ppp_t *ppp)
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)
lopt->h->recv_conf_ack(ipcp,lopt,data);
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->ppp->unit_buf_size<PPP_HEADERLEN+2)
{
log_warn("IPCP: short packet received\n");
return;
}
hdr=(struct ipcp_hdr_t *)ipcp->ppp->unit_buf;
if (ntohs(hdr->len)<PPP_HEADERLEN)
{
log_warn("IPCP: short packet received\n");
return;
}
ipcp->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);
break;
case CONFACK:
ipcp_recv_conf_ack(ipcp,(uint8_t*)(hdr+1),ntohs(hdr->len)-PPP_HDRLEN);
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:
ipcp_recv_conf_rej(ipcp,(uint8_t*)(hdr+1),ntohs(hdr->len)-PPP_HDRLEN);
ppp_fsm_recv_conf_rej(&ipcp->fsm);
break;
case TERMREQ:
term_msg=strndup((uint8_t*)(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);
break;
case TERMACK:
term_msg=strndup((uint8_t*)(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);
}
#ifndef PPP_IPCP_H
#define PPP_IPCP_H
#include <stdint.h>
#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 128 /* 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
......@@ -6,7 +6,6 @@
#include "triton/triton.h"
#include "events.h"
#include "log.h"
#include "ppp.h"
......@@ -110,7 +109,6 @@ void lcp_layer_finish(struct ppp_layer_data_t *ld)
ppp_unregister_handler(lcp->ppp,&lcp->hnd);
lcp_options_free(lcp);
}
void lcp_layer_free(struct ppp_layer_data_t *ld)
......@@ -299,9 +297,17 @@ static int lcp_recv_conf_req(struct ppp_lcp_t *lcp,uint8_t *data,int size)
r=lopt->h->recv_conf_req(lcp,lopt,(uint8_t*)ropt->hdr);
lopt->state=r;
ropt->state=r;
ropt->lopt=lopt;
if (r<ret) ret=r;
}
}
if (!ropt->lopt)
{
log_debug(" ");
print_ropt(ropt);
ropt->state=LCP_OPT_REJ;
ret=LCP_OPT_REJ;
}
}
log_debug("]\n");
......@@ -520,6 +526,7 @@ static void lcp_recv(struct ppp_handler_t*h)
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);
break;
case TERMACK:
term_msg=strndup((uint8_t*)(hdr+1),ntohs(hdr->len));
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册