diff options
Diffstat (limited to 'accel-pppd/ppp')
-rw-r--r-- | accel-pppd/ppp/CMakeLists.txt | 19 | ||||
-rw-r--r-- | accel-pppd/ppp/ccp_mppe.c | 259 | ||||
-rw-r--r-- | accel-pppd/ppp/ipcp_opt_dns.c | 155 | ||||
-rw-r--r-- | accel-pppd/ppp/ipcp_opt_ipaddr.c | 227 | ||||
-rw-r--r-- | accel-pppd/ppp/lcp_opt_accomp.c | 106 | ||||
-rw-r--r-- | accel-pppd/ppp/lcp_opt_magic.c | 91 | ||||
-rw-r--r-- | accel-pppd/ppp/lcp_opt_mru.c | 195 | ||||
-rw-r--r-- | accel-pppd/ppp/lcp_opt_pcomp.c | 106 | ||||
-rw-r--r-- | accel-pppd/ppp/ppp.c | 681 | ||||
-rw-r--r-- | accel-pppd/ppp/ppp.h | 189 | ||||
-rw-r--r-- | accel-pppd/ppp/ppp_auth.c | 346 | ||||
-rw-r--r-- | accel-pppd/ppp/ppp_auth.h | 37 | ||||
-rw-r--r-- | accel-pppd/ppp/ppp_ccp.c | 759 | ||||
-rw-r--r-- | accel-pppd/ppp/ppp_ccp.h | 96 | ||||
-rw-r--r-- | accel-pppd/ppp/ppp_fsm.c | 544 | ||||
-rw-r--r-- | accel-pppd/ppp/ppp_fsm.h | 72 | ||||
-rw-r--r-- | accel-pppd/ppp/ppp_ipcp.c | 665 | ||||
-rw-r--r-- | accel-pppd/ppp/ppp_ipcp.h | 96 | ||||
-rw-r--r-- | accel-pppd/ppp/ppp_lcp.c | 847 | ||||
-rw-r--r-- | accel-pppd/ppp/ppp_lcp.h | 136 | ||||
-rw-r--r-- | accel-pppd/ppp/ppp_notify.c | 54 | ||||
-rw-r--r-- | accel-pppd/ppp/ppp_pd.c | 14 |
22 files changed, 5694 insertions, 0 deletions
diff --git a/accel-pppd/ppp/CMakeLists.txt b/accel-pppd/ppp/CMakeLists.txt new file mode 100644 index 00000000..f4c0f04a --- /dev/null +++ b/accel-pppd/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-pppd/ppp/ccp_mppe.c b/accel-pppd/ppp/ccp_mppe.c new file mode 100644 index 00000000..0952aa01 --- /dev/null +++ b/accel-pppd/ppp/ccp_mppe.c @@ -0,0 +1,259 @@ +#include <stdlib.h> +#include <string.h> +#include <errno.h> +#include <sys/socket.h> +#include <sys/ioctl.h> +#include "linux_ppp.h" + +#include "ppp.h" +#include "ppp_ccp.h" +#include "log.h" +#include "events.h" + +#include "memdebug.h" + +#define MPPE_H (1 << 24) +#define MPPE_M (1 << 7) +#define MPPE_S (1 << 6) +#define MPPE_L (1 << 5) +#define MPPE_D (1 << 4) +#define MPPE_C (1 << 0) + +#define MPPE_PAD 4 + +static struct ccp_option_t *mppe_init(struct ppp_ccp_t *ccp); +static void mppe_free(struct ppp_ccp_t *ccp, struct ccp_option_t *opt); +static int mppe_send_conf_req(struct ppp_ccp_t *ccp, struct ccp_option_t *opt, uint8_t *ptr); +static int mppe_recv_conf_req(struct ppp_ccp_t *ccp, struct ccp_option_t *opt, uint8_t *ptr); +static int mppe_recv_conf_nak(struct ppp_ccp_t *ccp, struct ccp_option_t *opt, uint8_t *ptr); +static int mppe_recv_conf_rej(struct ppp_ccp_t *ccp, struct ccp_option_t *opt, uint8_t *ptr); +static void mppe_print(void (*print)(const char *fmt,...),struct ccp_option_t*, uint8_t *ptr); + +struct mppe_option_t +{ + struct ccp_option_t opt; + int mppe; + uint8_t recv_key[16]; + uint8_t send_key[16]; + int policy; // 1 - allowed, 2 - required +}; + +static struct ccp_option_handler_t mppe_opt_hnd = { + .init = mppe_init, + .send_conf_req = mppe_send_conf_req, + .send_conf_nak = mppe_send_conf_req, + .recv_conf_req = mppe_recv_conf_req, + .recv_conf_nak = mppe_recv_conf_nak, + .recv_conf_rej = mppe_recv_conf_rej, + .free = mppe_free, + .print = mppe_print, +}; + +static struct ccp_option_t *mppe_init(struct ppp_ccp_t *ccp) +{ + struct mppe_option_t *mppe_opt = _malloc(sizeof(*mppe_opt)); + memset(mppe_opt, 0, sizeof(*mppe_opt)); + mppe_opt->mppe = -1; + mppe_opt->opt.id = CI_MPPE; + mppe_opt->opt.len = 6; + + return &mppe_opt->opt; +} + +static void mppe_free(struct ppp_ccp_t *ccp, struct ccp_option_t *opt) +{ + struct mppe_option_t *mppe_opt = container_of(opt, typeof(*mppe_opt), opt); + + _free(mppe_opt); +} + +static int setup_mppe_key(int fd, int transmit, uint8_t *key) +{ + struct ppp_option_data data; + uint8_t buf[6 + 16]; + + memset(buf, 0, sizeof(buf)); + buf[0] = CI_MPPE; + buf[1] = 6; + *(uint32_t*)(buf + 2) = htonl(MPPE_S | MPPE_H); + if (key) + memcpy(buf + 6, key, 16); + + memset(&data, 0, sizeof(data)); + data.ptr = buf; + data.length = sizeof(buf); + data.transmit = transmit; + + if (ioctl(fd, PPPIOCSCOMPRESS, &data)) { + log_ppp_warn("mppe: MPPE requested but not supported by kernel\n"); + return -1; + } + + return 0; +} + +static int decrease_mtu(struct ppp_t *ppp) +{ + struct ifreq ifr; + + strcpy(ifr.ifr_name, ppp->ifname); + + if (ioctl(sock_fd, SIOCGIFMTU, &ifr)) { + log_ppp_error("mppe: failed to get MTU: %s\n", strerror(errno)); + return -1; + } + + ifr.ifr_mtu -= MPPE_PAD; + + if (ioctl(sock_fd, SIOCSIFMTU, &ifr)) { + log_ppp_error("mppe: failed to set MTU: %s\n", strerror(errno)); + return -1; + } + + return 0; +} + +static int mppe_send_conf_req(struct ppp_ccp_t *ccp, struct ccp_option_t *opt, uint8_t *ptr) +{ + struct mppe_option_t *mppe_opt = container_of(opt,typeof(*mppe_opt),opt); + struct ccp_opt32_t *opt32 = (struct ccp_opt32_t*)ptr; + + if (mppe_opt->mppe != -1) { + opt32->hdr.id = CI_MPPE; + opt32->hdr.len = 6; + opt32->val = mppe_opt->mppe ? htonl(MPPE_S | MPPE_H) : 0; + + if (mppe_opt->mppe && setup_mppe_key(ccp->ppp->unit_fd, 0, mppe_opt->recv_key)) + return 0; + + return 6; + } + return 0; +} + +static int mppe_recv_conf_req(struct ppp_ccp_t *ccp, struct ccp_option_t *opt, uint8_t *ptr) +{ + struct mppe_option_t *mppe_opt = container_of(opt, typeof(*mppe_opt), opt); + struct ccp_opt32_t *opt32 = (struct ccp_opt32_t *)ptr; + + /*if (!ptr) { + if (mppe_opt->policy == 2) + return CCP_OPT_NAK; + return CCP_OPT_ACK; + }*/ + + if (opt32->hdr.len != 6) + return CCP_OPT_REJ; + + if (mppe_opt->policy == 2) { + if (ntohl(opt32->val) != (MPPE_S | MPPE_H)) + return CCP_OPT_NAK; + } else if (mppe_opt->policy == 1) { + if (ntohl(opt32->val) == (MPPE_S | MPPE_H)) + mppe_opt->mppe = 1; + else if ((ntohl(opt32->val) & (MPPE_S | MPPE_H)) == (MPPE_S | MPPE_H)) { + mppe_opt->mppe = 1; + return CCP_OPT_NAK; + } else if (opt32->val) { + mppe_opt->mppe = 0; + return CCP_OPT_NAK; + } else + mppe_opt->mppe = 0; + } else + return CCP_OPT_REJ; + + if (mppe_opt->mppe) { + if (setup_mppe_key(ccp->ppp->unit_fd, 1, mppe_opt->send_key)) + return CCP_OPT_REJ; + + decrease_mtu(ccp->ppp); + + log_ppp_debug(" (mppe enabled)"); + } + + return CCP_OPT_ACK; +} + +static int mppe_recv_conf_rej(struct ppp_ccp_t *ccp, struct ccp_option_t *opt, uint8_t *ptr) +{ + struct mppe_option_t *mppe_opt = container_of(opt, typeof(*mppe_opt), opt); + + if (mppe_opt->mppe != 2) { + mppe_opt->mppe = -1; + return 0; + } + + return -1; +} + +static int mppe_recv_conf_nak(struct ppp_ccp_t *ccp, struct ccp_option_t *opt, uint8_t *ptr) +{ + struct mppe_option_t *mppe_opt = container_of(opt, typeof(*mppe_opt), opt); + struct ccp_opt32_t *opt32 = (struct ccp_opt32_t *)ptr; + + if (opt32->hdr.len != 6) + return -1; + + if (mppe_opt->policy == 2) { + if (ntohl(opt32->val) == (MPPE_S | MPPE_H)) + return -1; + } else if (mppe_opt->policy == 1) { + if ((ntohl(opt32->val) & (MPPE_S | MPPE_H)) == (MPPE_S | MPPE_H)) + mppe_opt->mppe = 0; + else + mppe_opt->mppe = 1; + } else { + if (opt32->val == 0) + return -1; + } + + return 0; +} + +static void mppe_print(void (*print)(const char *fmt,...),struct ccp_option_t *opt, uint8_t *ptr) +{ + struct mppe_option_t *mppe_opt = container_of(opt, typeof(*mppe_opt), opt); + struct ccp_opt32_t *opt32 = (struct ccp_opt32_t *)ptr; + uint32_t bits; + + if (ptr) + bits = ntohl(opt32->val); + else + if (mppe_opt->mppe) + bits = MPPE_S | MPPE_H; + else + bits = 0; + + print("<mppe %sH %sM %sS %sL %sD %sC>", + bits & MPPE_H ? "+" : "-", + bits & MPPE_M ? "+" : "-", + bits & MPPE_S ? "+" : "-", + bits & MPPE_L ? "+" : "-", + bits & MPPE_D ? "+" : "-", + bits & MPPE_C ? "+" : "-" + ); +} + +static void ev_mppe_keys(struct ev_mppe_keys_t *ev) +{ + struct mppe_option_t *mppe_opt = container_of(ccp_find_option(ev->ppp, &mppe_opt_hnd), typeof(*mppe_opt), opt); + + if ((ev->type & 0x04) == 0) { + log_ppp_warn("mppe: 128-bit session keys not allowed, disabling mppe ...\n"); + return; + } + + memcpy(mppe_opt->recv_key, ev->recv_key, 16); + memcpy(mppe_opt->send_key, ev->send_key, 16); + mppe_opt->policy = ev->policy; + + if (ev->policy == 2) + mppe_opt->mppe = 1; +} + +static void __init mppe_opt_init() +{ + ccp_option_register(&mppe_opt_hnd); + triton_event_register_handler(EV_MPPE_KEYS, (triton_event_func)ev_mppe_keys); +} + diff --git a/accel-pppd/ppp/ipcp_opt_dns.c b/accel-pppd/ppp/ipcp_opt_dns.c new file mode 100644 index 00000000..c1770922 --- /dev/null +++ b/accel-pppd/ppp/ipcp_opt_dns.c @@ -0,0 +1,155 @@ +#include <stdlib.h> +#include <string.h> +#include <arpa/inet.h> + +#include "ppp.h" +#include "ppp_ipcp.h" +#include "log.h" +#include "ipdb.h" + +#include "memdebug.h" + +static in_addr_t conf_dns1; +static in_addr_t conf_dns2; + +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) +{ + return 0; +} + +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 (opt32->hdr.len != 6) + return IPCP_OPT_REJ; + + if (!dns_opt->addr) + { + if (dns_opt->opt.id == CI_DNS1 && conf_dns1) dns_opt->addr=conf_dns1; + else if (dns_opt->opt.id == CI_DNS2 && conf_dns2) dns_opt->addr=conf_dns2; + + 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("<dns1 %s>",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("<dns2 %s>",inet_ntoa(in)); +} + +static void __init dns_opt_init() +{ + char *opt; + + opt = conf_get_opt("dns", "dns1"); + if (opt) + conf_dns1 = inet_addr(opt); + + opt = conf_get_opt("dns", "dns2"); + if (opt) + conf_dns2 = inet_addr(opt); + + ipcp_option_register(&dns1_opt_hnd); + ipcp_option_register(&dns2_opt_hnd); +} diff --git a/accel-pppd/ppp/ipcp_opt_ipaddr.c b/accel-pppd/ppp/ipcp_opt_ipaddr.c new file mode 100644 index 00000000..334f4256 --- /dev/null +++ b/accel-pppd/ppp/ipcp_opt_ipaddr.c @@ -0,0 +1,227 @@ +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <errno.h> +#include <arpa/inet.h> +#include <sys/socket.h> +#include <sys/ioctl.h> +#include "linux_ppp.h" + +#include "ppp.h" +#include "ppp_ipcp.h" +#include "log.h" +#include "ipdb.h" +#include "iprange.h" +#include "events.h" + +#include "memdebug.h" + +static int conf_check_exists; + +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; + struct ipdb_item_t *ip; + int started:1; +}; + +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)); + 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); + + if (ipaddr_opt->ip) + ipdb_put(ipcp->ppp, ipaddr_opt->ip); + + _free(ipaddr_opt); +} + +static int check_exists(struct ppp_t *self_ppp, in_addr_t addr) +{ + struct ppp_t *ppp; + int r = 0; + + pthread_rwlock_rdlock(&ppp_lock); + list_for_each_entry(ppp, &ppp_list, entry) { + if (!ppp->terminating && ppp->peer_ipaddr == addr && ppp != self_ppp) { + log_ppp_warn("ppp:ipcp: requested IP already assigned to %s\n", ppp->ifname); + r = 1; + break; + } + } + pthread_rwlock_unlock(&ppp_lock); + + return r; +} + +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; + + 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"); + return -1; + } + } + + if (iprange_tunnel_check(ipaddr_opt->ip->peer_addr)) { + log_ppp_warn("ppp:ipcp: to avoid kernel soft lockup requested IP cannot be assigned (%i.%i.%i.%i)\n", + ipaddr_opt->ip->peer_addr&0xff, + (ipaddr_opt->ip->peer_addr >> 8)&0xff, + (ipaddr_opt->ip->peer_addr >> 16)&0xff, + (ipaddr_opt->ip->peer_addr >> 24)&0xff); + return -1; + } + + if (conf_check_exists && check_exists(ipcp->ppp, ipaddr_opt->ip->peer_addr)) + return -1; + + opt32->hdr.id=CI_ADDR; + opt32->hdr.len=6; + opt32->val=ipaddr_opt->ip->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->ip->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 (opt32->hdr.len != 6) + return IPCP_OPT_REJ; + + if (ipaddr_opt->ip->peer_addr == opt32->val) + goto ack; + + /*if (!ipaddr_opt->peer_addr) { + ipaddr_opt->peer_addr = opt32->val; + goto ack; + }*/ + + return IPCP_OPT_NAK; + +ack: + if (ipaddr_opt->started) + return IPCP_OPT_ACK; + + ipaddr_opt->started = 1; + + ipcp->ppp->ipaddr = ipaddr_opt->ip->addr; + ipcp->ppp->peer_ipaddr = ipaddr_opt->ip->peer_addr; + + triton_event_fire(EV_PPP_ACCT_START, ipcp->ppp); + if (ipcp->ppp->stop_time) + return IPCP_OPT_ACK; + + triton_event_fire(EV_PPP_PRE_UP, ipcp->ppp); + if (ipcp->ppp->stop_time) + return IPCP_OPT_ACK; + + memset(&ifr, 0, sizeof(ifr)); + memset(&addr, 0, sizeof(addr)); + + strcpy(ifr.ifr_name, ipcp->ppp->ifname); + + addr.sin_family = AF_INET; + addr.sin_addr.s_addr = ipaddr_opt->ip->addr; + memcpy(&ifr.ifr_addr,&addr,sizeof(addr)); + + if (ioctl(sock_fd, SIOCSIFADDR, &ifr)) + log_ppp_error("ipcp: failed to set PA address: %s\n", strerror(errno)); + + addr.sin_addr.s_addr = ipaddr_opt->ip->peer_addr; + memcpy(&ifr.ifr_dstaddr,&addr,sizeof(addr)); + + if (ioctl(sock_fd, SIOCSIFDSTADDR, &ifr)) + log_ppp_error("ipcp: failed to set remote PA address: %s\n", strerror(errno)); + + if (ioctl(sock_fd, SIOCGIFFLAGS, &ifr)) + log_ppp_error("ipcp: failed to get interface flags: %s\n", strerror(errno)); + + ifr.ifr_flags |= IFF_UP | IFF_POINTOPOINT; + + if (ioctl(sock_fd, SIOCSIFFLAGS, &ifr)) + log_ppp_error("ipcp: 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_ppp_error("ipcp: 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 = { .s_addr = 0, }; + + if (ptr) + in.s_addr = opt32->val; + else if (ipaddr_opt->ip) + in.s_addr = ipaddr_opt->ip->addr; + + print("<addr %s>",inet_ntoa(in)); +} + +static void load_config(void) +{ + const char *opt; + + opt = conf_get_opt("ppp", "check-ip"); + if (opt && atoi(opt) > 0) + conf_check_exists = 1; +} + +static void __init ipaddr_opt_init() +{ + ipcp_option_register(&ipaddr_opt_hnd); + load_config(); + triton_event_register_handler(EV_CONFIG_RELOAD, (triton_event_func)load_config); +} + diff --git a/accel-pppd/ppp/lcp_opt_accomp.c b/accel-pppd/ppp/lcp_opt_accomp.c new file mode 100644 index 00000000..241b0e06 --- /dev/null +++ b/accel-pppd/ppp/lcp_opt_accomp.c @@ -0,0 +1,106 @@ +#include <stdlib.h> +#include <string.h> +#include <arpa/inet.h> + +#include "ppp.h" +#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); +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 + int require; +}; + +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); + struct lcp_opt_hdr_t *opt0=(struct lcp_opt_hdr_t*)ptr; + + /*if (!ptr) { + if (accomp_opt->require) + return LCP_OPT_NAK; + accomp_opt->accomp=0; + return LCP_OPT_ACK; + }*/ + + if (opt0->len != 2) + return LCP_OPT_REJ; + + 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("<accomp>"); +} + +static void __init accomp_opt_init() +{ + lcp_option_register(&accomp_opt_hnd); +} + diff --git a/accel-pppd/ppp/lcp_opt_magic.c b/accel-pppd/ppp/lcp_opt_magic.c new file mode 100644 index 00000000..4a61ef91 --- /dev/null +++ b/accel-pppd/ppp/lcp_opt_magic.c @@ -0,0 +1,91 @@ +#include <stdlib.h> +#include <string.h> +#include <arpa/inet.h> + +#include "ppp.h" +#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); +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 (!ptr) + return LCP_OPT_NAK;*/ + + if (opt32->hdr.len != 6) + return LCP_OPT_REJ; + + if (magic_opt->magic==ntohl(opt32->val)) + { + log_ppp_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 %04x>",magic_opt->magic); +} + +static void __init magic_opt_init() +{ + lcp_option_register(&magic_opt_hnd); +} diff --git a/accel-pppd/ppp/lcp_opt_mru.c b/accel-pppd/ppp/lcp_opt_mru.c new file mode 100644 index 00000000..78e06b5a --- /dev/null +++ b/accel-pppd/ppp/lcp_opt_mru.c @@ -0,0 +1,195 @@ +#include <stdlib.h> +#include <string.h> +#include <errno.h> +#include <stdio.h> +#include <unistd.h> +#include <sys/socket.h> +#include <arpa/inet.h> +#include <sys/ioctl.h> +#include "linux_ppp.h" + +#include "ppp.h" +#include "ppp_lcp.h" +#include "log.h" +#include "events.h" + +#include "memdebug.h" + +static int conf_mtu; +static int conf_mru; +static int conf_min_mtu = 100; +static int conf_max_mtu = 1500; + +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 int mru_recv_conf_nak(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; + int naked:1; +}; + +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, + .recv_conf_nak=mru_recv_conf_nak, + .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->mru = (conf_mru && conf_mru <= lcp->ppp->ctrl->max_mtu) ? conf_mru : lcp->ppp->ctrl->max_mtu; + if (mru_opt->mru > conf_max_mtu) + mru_opt->mru = conf_max_mtu; + mru_opt->mtu = (conf_mtu && conf_mtu <= lcp->ppp->ctrl->max_mtu) ? conf_mtu : lcp->ppp->ctrl->max_mtu; + if (mru_opt->mtu > conf_max_mtu) + mru_opt->mtu = conf_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; + + if (mru_opt->naked) + return 0; + + 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 (!ptr) + return LCP_OPT_NAK;*/ + + if (opt16->hdr.len != 4) + return LCP_OPT_REJ; + + if (ntohs(opt16->val) < conf_min_mtu || ntohs(opt16->val) > lcp->ppp->ctrl->max_mtu || ntohs(opt16->val) > conf_max_mtu) + return LCP_OPT_NAK; + + mru_opt->mtu = ntohs(opt16->val); + return LCP_OPT_ACK; +} + +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, + }; + + strcpy(ifr.ifr_name, lcp->ppp->ifname); + + if (ioctl(lcp->ppp->unit_fd, PPPIOCSMRU, &mru_opt->mru)) + log_ppp_error("lcp:mru: failed to set MRU: %s\n", strerror(errno)); + + if (ioctl(sock_fd, SIOCSIFMTU, &ifr)) + log_ppp_error("lcp:mru: failed to set MTU: %s\n", strerror(errno)); + + return 0; +} + +static int mru_recv_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); + mru_opt->naked = 1; + 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("<mru %i>",ntohs(opt16->val)); + else + print("<mru %i>",mru_opt->mru); +} + +static void load_config(void) +{ + char *opt; + + opt = conf_get_opt("ppp", "mtu"); + if (opt && atoi(opt) > 0) + conf_mtu = atoi(opt); + + opt = conf_get_opt("ppp", "mru"); + if (opt && atoi(opt) > 0) + conf_mru = atoi(opt); + + opt = conf_get_opt("ppp", "min-mtu"); + if (opt && atoi(opt) > 0) + conf_min_mtu = atoi(opt); + + opt = conf_get_opt("ppp", "max-mtu"); + if (opt && atoi(opt) > 0) + conf_max_mtu = atoi(opt); + + if (conf_min_mtu > conf_mru) { + log_emerg("min-mtu cann't be greater then mtu/mru\n"); + conf_min_mtu = conf_mru; + } + + if (conf_min_mtu > 1500) { + log_emerg("min-mtu cann't be greater then 1500\n"); + conf_min_mtu = 1500; + } + + if (conf_mru > 1500 || conf_mtu > 1500) { + log_emerg("mtu/mru cann't be greater then 1500\n"); + conf_mru = 1500; + } +} + +static void __init mru_opt_init() +{ + load_config(); + lcp_option_register(&mru_opt_hnd); + triton_event_register_handler(EV_CONFIG_RELOAD, (triton_event_func)load_config); +} + diff --git a/accel-pppd/ppp/lcp_opt_pcomp.c b/accel-pppd/ppp/lcp_opt_pcomp.c new file mode 100644 index 00000000..1f8532bb --- /dev/null +++ b/accel-pppd/ppp/lcp_opt_pcomp.c @@ -0,0 +1,106 @@ +#include <stdlib.h> +#include <string.h> +#include <arpa/inet.h> + +#include "ppp.h" +#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); +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 + int require; +}; + +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); + struct lcp_opt_hdr_t *opt0=(struct lcp_opt_hdr_t*)ptr; + + /*if (!ptr) { + if (pcomp_opt->require) + return LCP_OPT_NAK; + pcomp_opt->pcomp=0; + return LCP_OPT_ACK; + }*/ + + if (opt0->len != 2) + return LCP_OPT_REJ; + + 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("<pcomp>"); +} + +static void __init pcomp_opt_init() +{ + lcp_option_register(&pcomp_opt_hnd); +} + diff --git a/accel-pppd/ppp/ppp.c b/accel-pppd/ppp/ppp.c new file mode 100644 index 00000000..f578e8eb --- /dev/null +++ b/accel-pppd/ppp/ppp.c @@ -0,0 +1,681 @@ +#include <stdio.h> +#include <fcntl.h> +#include <unistd.h> +#include <stdlib.h> +#include <stdint.h> +#include <string.h> +#include <errno.h> +#include <sys/ioctl.h> +#include <arpa/inet.h> +#include <features.h> +#include <signal.h> +#include "linux_ppp.h" + +#include <openssl/md5.h> + +#include "triton.h" + +#include "events.h" +#include "ppp.h" +#include "ppp_fsm.h" +#include "log.h" +#include "spinlock.h" + +#include "memdebug.h" + +int __export conf_ppp_verbose; +static int conf_sid_ucase; + +pthread_rwlock_t __export ppp_lock = PTHREAD_RWLOCK_INITIALIZER; +__export LIST_HEAD(ppp_list); + +static LIST_HEAD(layers); +int __export sock_fd; + +int __export ppp_shutdown; + +static unsigned long long seq; +#if __WORDSIZE == 32 +static spinlock_t seq_lock; +#endif + + +struct ppp_stat_t ppp_stat; + +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 *); + +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) +{ + if (ppp->chan_buf) + free(ppp->chan_buf); + if (ppp->unit_buf) + _free(ppp->unit_buf); + if (ppp->username) + _free(ppp->username); +} + +static void generate_sessionid(struct ppp_t *ppp) +{ + unsigned long long sid; + +#if __WORDSIZE == 32 + spin_lock(&seq_lock); + sid = ++seq; + spin_unlock(&seq_lock); +#else + sid = __sync_add_and_fetch(&seq, 1); +#endif + + if (conf_sid_ucase) + sprintf(ppp->sessionid, "%016llX", sid); + else + sprintf(ppp->sessionid, "%016llx", sid); +} + +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_ppp_error("ioctl(PPPIOCGCHAN): %s\n", strerror(errno)); + return -1; + } + + ppp->chan_fd = open("/dev/ppp", O_RDWR); + if (ppp->chan_fd < 0) { + log_ppp_error("open(chan) /dev/ppp: %s\n", strerror(errno)); + return -1; + } + + if (ioctl(ppp->chan_fd, PPPIOCATTCHAN, &ppp->chan_idx) < 0) { + log_ppp_error("ioctl(PPPIOCATTCHAN): %s\n", strerror(errno)); + goto exit_close_chan; + } + + ppp->unit_fd = open("/dev/ppp", O_RDWR); + if (ppp->unit_fd < 0) { + log_ppp_error("open(unit) /dev/ppp: %s\n", strerror(errno)); + goto exit_close_chan; + } + + ppp->unit_idx = -1; + if (ioctl(ppp->unit_fd, PPPIOCNEWUNIT, &ppp->unit_idx) < 0) { + log_ppp_error("ioctl(PPPIOCNEWUNIT): %s\n", strerror(errno)); + goto exit_close_unit; + } + + if (ioctl(ppp->chan_fd, PPPIOCCONNECT, &ppp->unit_idx) < 0) { + log_ppp_error("ioctl(PPPIOCCONNECT): %s\n", strerror(errno)); + goto exit_close_unit; + } + + if (fcntl(ppp->chan_fd, F_SETFL, O_NONBLOCK)) { + log_ppp_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_ppp_error("ppp: cann't to set nonblocking mode: %s\n", strerror(errno)); + goto exit_close_unit; + } + + ppp->start_time = time(NULL); + generate_sessionid(ppp); + sprintf(ppp->ifname, "ppp%i", ppp->unit_idx); + + log_ppp_info1("connect: %s <--> %s(%s)\n", ppp->ifname, ppp->ctrl->name, ppp->chan_name); + + init_layers(ppp); + + if (list_empty(&ppp->layers)) { + log_ppp_error("no layers to start\n"); + goto exit_close_unit; + } + + ppp->chan_buf = _malloc(PPP_MRU); + ppp->unit_buf = _malloc(PPP_MRU); + + ppp->chan_hnd.fd = ppp->chan_fd; + ppp->chan_hnd.read = ppp_chan_read; + ppp->unit_hnd.fd = ppp->unit_fd; + ppp->unit_hnd.read = ppp_unit_read; + 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); + + ppp->state = PPP_STATE_STARTING; + __sync_add_and_fetch(&ppp_stat.starting, 1); + + pthread_rwlock_wrlock(&ppp_lock); + list_add_tail(&ppp->entry, &ppp_list); + pthread_rwlock_unlock(&ppp_lock); + + log_ppp_debug("ppp established\n"); + + triton_event_fire(EV_PPP_STARTING, ppp); + + 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) +{ + pthread_rwlock_wrlock(&ppp_lock); + list_del(&ppp->entry); + pthread_rwlock_unlock(&ppp_lock); + + switch (ppp->state) { + case PPP_STATE_ACTIVE: + __sync_sub_and_fetch(&ppp_stat.active, 1); + break; + case PPP_STATE_STARTING: + __sync_sub_and_fetch(&ppp_stat.starting, 1); + break; + case PPP_STATE_FINISHING: + __sync_sub_and_fetch(&ppp_stat.finishing, 1); + break; + } + + triton_md_unregister_handler(&ppp->chan_hnd); + triton_md_unregister_handler(&ppp->unit_hnd); + + close(ppp->unit_fd); + close(ppp->chan_fd); + close(ppp->fd); + + ppp->unit_fd = -1; + ppp->chan_fd = -1; + ppp->fd = -1; + + _free(ppp->unit_buf); + _free(ppp->chan_buf); + + _free_layers(ppp); + + ppp->terminated = 1; + + 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; + } + + if (ppp_shutdown && !ppp_stat.starting && !ppp_stat.active && !ppp_stat.finishing) + kill(getpid(), SIGTERM); +} + +/*void print_buf(uint8_t *buf, int size) +{ + int i; + for(i=0;i<size;i++) + printf("%x ",buf[i]); + printf("\n"); +}*/ + +int __export ppp_chan_send(struct ppp_t *ppp, void *data, int size) +{ + int n; + + //printf("ppp_chan_send: "); + //print_buf((uint8_t*)data,size); + + n = write(ppp->chan_fd,data,size); + if (n < size) + log_ppp_error("ppp_chan_send: short write %i, excpected %i\n", n, size); + return n; +} + +int __export ppp_unit_send(struct ppp_t *ppp, void *data, int size) +{ + int n; + + //printf("ppp_unit_send: "); + //print_buf((uint8_t*)data,size); + + n=write(ppp->unit_fd, data, size); + if (n < size) + log_ppp_error("ppp_unit_send: short write %i, excpected %i\n",n,size); + return n; +} + +static int ppp_chan_read(struct triton_md_handler_t *h) +{ + struct ppp_t *ppp = container_of(h, typeof(*ppp), chan_hnd); + struct ppp_handler_t *ppp_h; + uint16_t proto; + + while(1) { +cont: + ppp->chan_buf_size = read(h->fd, ppp->chan_buf, PPP_MRU); + if (ppp->chan_buf_size < 0) { + if (errno == EAGAIN) + return 0; + log_ppp_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 == 0) { + ppp_terminate(ppp, 1, TERM_NAS_ERROR); + return 1; + } + + if (ppp->chan_buf_size < 2) { + log_ppp_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; + } + } + + lcp_send_proto_rej(ppp, proto); + //log_ppp_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 == EAGAIN) + return 0; + log_ppp_error("ppp_unit_read: %s\n",strerror(errno)); + return 0; + } + + md_check(ppp->unit_buf); + //printf("ppp_unit_read: "); + //print_buf(ppp->unit_buf,ppp->unit_buf_size); + + if (ppp->unit_buf_size == 0) { + ppp_terminate(ppp, 1, TERM_NAS_ERROR); + return 1; + } + + if (ppp->unit_buf_size < 2) { + log_ppp_error("ppp_unit_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; + } + } + lcp_send_proto_rej(ppp, proto); + //log_ppp_warn("ppp_unit_read: discarding unknown packet %x\n", proto); + } +} + +void ppp_recv_proto_rej(struct ppp_t *ppp, uint16_t proto) +{ + struct ppp_handler_t *ppp_h; + + list_for_each_entry(ppp_h, &ppp->chan_handlers, entry) { + if (ppp_h->proto == proto) { + if (ppp_h->recv_proto_rej) + ppp_h->recv_proto_rej(ppp_h); + return; + } + } + + list_for_each_entry(ppp_h, &ppp->unit_handlers, entry) { + if (ppp_h->proto == proto) { + if (ppp_h->recv_proto_rej) + ppp_h->recv_proto_rej(ppp_h); + return; + } + } +} + +void __export ppp_layer_started(struct ppp_t *ppp, struct ppp_layer_data_t *d) +{ + struct layer_node_t *n = d->node; + + if (d->started) + return; + + d->started = 1; + + list_for_each_entry(d, &n->items, entry) + if (!d->started) return; + + if (n->entry.next == &ppp->layers) { + ppp->state = PPP_STATE_ACTIVE; + __sync_sub_and_fetch(&ppp_stat.starting, 1); + __sync_add_and_fetch(&ppp_stat.active, 1); + ppp->ctrl->started(ppp); + triton_event_fire(EV_PPP_STARTED, ppp); + } else { + n = list_entry(n->entry.next, typeof(*n), entry); + list_for_each_entry(d, &n->items, entry) { + d->starting = 1; + if (d->layer->start(d)) { + ppp_terminate(ppp, TERM_NAS_ERROR, 0); + return; + } + } + } +} + +void __export ppp_layer_finished(struct ppp_t *ppp, struct ppp_layer_data_t *d) +{ + struct layer_node_t *n = d->node; + + d->finished = 1; + d->starting = 0; + + list_for_each_entry(n, &ppp->layers, entry) { + list_for_each_entry(d, &n->items, entry) { + if (d->starting && !d->finished) + return; + } + } + + destablish_ppp(ppp); +} + +void __export ppp_terminate(struct ppp_t *ppp, int cause, int hard) +{ + struct layer_node_t *n; + struct ppp_layer_data_t *d; + int s = 0; + + if (ppp->terminated) + return; + + if (!ppp->stop_time) + time(&ppp->stop_time); + + if (!ppp->terminate_cause) + ppp->terminate_cause = cause; + + if (ppp->terminating) { + if (hard) + destablish_ppp(ppp); + return; + } + + ppp->terminating = 1; + if (ppp->state == PPP_STATE_ACTIVE) + __sync_sub_and_fetch(&ppp_stat.active, 1); + else + __sync_sub_and_fetch(&ppp_stat.starting, 1); + __sync_add_and_fetch(&ppp_stat.finishing, 1); + ppp->state = PPP_STATE_FINISHING; + + log_ppp_debug("ppp_terminate\n"); + + triton_event_fire(EV_PPP_FINISHING, ppp); + + 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,"ccp")) return 2; + if (!strcmp(name,"ipcp")) 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 (order < n->order) { + 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; + + list_for_each_entry(n,&layers,entry) { + n1 = _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; + if (d->layer->start(d)) { + ppp_terminate(ppp, TERM_NAS_ERROR, 0); + return; + } + } +} + +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; +} + +void ppp_shutdown_soft(void) +{ + ppp_shutdown = 1; + + if (!ppp_stat.starting && !ppp_stat.active && !ppp_stat.finishing) + kill(getpid(), SIGTERM); +} + +static void save_seq(void) +{ + FILE *f; + char *opt = conf_get_opt("ppp", "seq-file"); + if (!opt) + opt = "/var/run/accel-ppp/seq"; + + f = fopen(opt, "w"); + if (f) { + fprintf(f, "%llu", seq); + fclose(f); + } +} + +static void load_config(void) +{ + char *opt; + + opt = conf_get_opt("ppp", "verbose"); + if (opt && atoi(opt) > 0) + conf_ppp_verbose = 1; + + opt = conf_get_opt("ppp", "sid-case"); + if (opt) { + if (!strcmp(opt, "upper")) + conf_sid_ucase = 1; + else if (strcmp(opt, "lower")) + log_emerg("ppp: sid-case: invalid format\n"); + } +} + +static void __init init(void) +{ + char *opt; + FILE *f; + + sock_fd = socket(AF_INET, SOCK_DGRAM, 0); + if (sock_fd < 0) { + perror("socket"); + _exit(EXIT_FAILURE); + } + + opt = conf_get_opt("ppp", "seq-file"); + if (!opt) + opt = "/var/run/accel-ppp/seq"; + + f = fopen(opt, "r"); + if (f) { + fscanf(f, "%llu", &seq); + fclose(f); + } else + seq = (unsigned long long)random() * (unsigned long long)random(); + + load_config(); + triton_event_register_handler(EV_CONFIG_RELOAD, (triton_event_func)load_config); + + atexit(save_seq); +} + diff --git a/accel-pppd/ppp/ppp.h b/accel-pppd/ppp/ppp.h new file mode 100644 index 00000000..c633135c --- /dev/null +++ b/accel-pppd/ppp/ppp.h @@ -0,0 +1,189 @@ +#ifndef PPP_H +#define PPP_H + +#include <sys/types.h> +#include <time.h> +#include <netinet/in.h> +#include <pthread.h> + +#include "triton.h" +#include "list.h" + +/* + * Packet header = Code, id, length. + */ +#define PPP_HEADERLEN 4 +#define PPP_MTU 1500 + + +/* + * 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_SESSIONID_LEN 16 +#define PPP_IFNAME_LEN 10 + +#define PPP_STATE_STARTING 1 +#define PPP_STATE_ACTIVE 2 +#define PPP_STATE_FINISHING 3 + +#define TERM_USER_REQUEST 1 +#define TERM_SESSION_TIMEOUT 2 +#define TERM_ADMIN_RESET 3 +#define TERM_USER_ERROR 4 +#define TERM_NAS_ERROR 5 +#define TERM_NAS_REQUEST 6 +#define TERM_NAS_REBOOT 7 +#define TERM_AUTH_ERROR 8 + + +struct ppp_t; + +struct ppp_ctrl_t +{ + struct triton_context_t *ctx; + const char *name; + int max_mtu; + char *calling_station_id; + char *called_station_id; + void (*started)(struct ppp_t*); + void (*finished)(struct ppp_t*); +}; + +struct ppp_pd_t +{ + struct list_head entry; + void *key; +}; + +struct ppp_t +{ + struct list_head entry; + 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; + + int state; + char *chan_name; + char ifname[PPP_IFNAME_LEN]; + char sessionid[PPP_SESSIONID_LEN+1]; + time_t start_time; + time_t stop_time; + char *username; + in_addr_t ipaddr; + in_addr_t peer_ipaddr; + + struct ppp_ctrl_t *ctrl; + + int terminating:1; + int terminated:1; + int terminate_cause; + + 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 list_head pd_list; +}; + +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; + int finished:1; +}; + +struct ppp_layer_t +{ + struct list_head entry; + struct ppp_layer_data_t *(*init)(struct ppp_t *); + int (*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*); + void (*recv_proto_rej)(struct ppp_handler_t *h); +}; + +struct ppp_stat_t +{ + unsigned int active; + unsigned int starting; + unsigned int finishing; +}; + +struct ppp_t *alloc_ppp(void); +void ppp_init(struct ppp_t *ppp); +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); +void lcp_send_proto_rej(struct ppp_t *ppp, uint16_t proto); +void ppp_recv_proto_rej(struct ppp_t *ppp, uint16_t proto); + +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, int cause); + +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 ppp_shutdown; +void ppp_shutdown_soft(void); + +extern int conf_ppp_verbose; + +extern pthread_rwlock_t ppp_lock; +extern struct list_head ppp_list; + +extern struct ppp_stat_t ppp_stat; + +extern int sock_fd; // internet socket for ioctls +#endif diff --git a/accel-pppd/ppp/ppp_auth.c b/accel-pppd/ppp/ppp_auth.c new file mode 100644 index 00000000..32413c6b --- /dev/null +++ b/accel-pppd/ppp/ppp_auth.c @@ -0,0 +1,346 @@ +#include <stdlib.h> +#include <string.h> +#include <arpa/inet.h> + +#include "ppp.h" +#include "events.h" +#include "ppp_lcp.h" +#include "log.h" + +#include "ppp_auth.h" + +#include "memdebug.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 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 *); + +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; + int started:1; +}; + +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; + + if (auth_opt->started && auth_opt->auth) { + auth_opt->auth->h->finish(lcp->ppp, auth_opt->auth); + auth_opt->started = 0; + } + + 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; + + if (!ptr) + return LCP_OPT_ACK; + + + 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_ppp_error("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_ppp_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_ppp_error("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_ppp_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_ppp_error("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) && (!d->h->check || d->h->check((uint8_t *)(opt16 + 1)))) + goto print_d; + } + + print("<auth %02x>", ntohs(opt16->val)); + return; + } else if (auth_opt->auth) + d = auth_opt->auth; + else + return; + +print_d: + print("<auth %s>", d->h->name); +} + +static struct ppp_layer_data_t *auth_layer_init(struct ppp_t *ppp) +{ + struct auth_layer_data_t *ad = _malloc(sizeof(*ad)); + + log_ppp_debug("auth_layer_init\n"); + + memset(ad, 0, sizeof(*ad)); + + ad->ppp = ppp; + + return &ad->ld; +} + +static int auth_layer_start(struct ppp_layer_data_t *ld) +{ + struct auth_layer_data_t *ad = container_of(ld,typeof(*ad),ld); + + log_ppp_debug("auth_layer_start\n"); + + if (ad->auth_opt.auth) { + ad->auth_opt.started = 1; + ad->auth_opt.auth->h->start(ad->ppp, ad->auth_opt.auth); + } else { + log_ppp_debug("auth_layer_started\n"); + ppp_layer_started(ad->ppp, ld); + } + + return 0; +} + +static void auth_layer_finish(struct ppp_layer_data_t *ld) +{ + struct auth_layer_data_t *ad = container_of(ld, typeof(*ad), ld); + + log_ppp_debug("auth_layer_finish\n"); + + if (ad->auth_opt.auth) + ad->auth_opt.auth->h->finish(ad->ppp, ad->auth_opt.auth); + + ad->auth_opt.started = 0; + + log_ppp_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_ppp_debug("auth_layer_free\n"); + + _free(ad); +} + +void __export ppp_auth_successed(struct ppp_t *ppp, char *username) +{ + struct auth_layer_data_t *ad = container_of(ppp_find_layer_data(ppp, &auth_layer), typeof(*ad), ld); + log_ppp_debug("auth_layer_started\n"); + ppp->username = username; + ppp_layer_started(ppp, &ad->ld); + log_ppp_info1("%s: authentication successed\n", username); + triton_event_fire(EV_PPP_AUTHORIZED, ppp); +} + +void __export ppp_auth_failed(struct ppp_t *ppp, const char *username) +{ + if (username) + log_ppp_info1("%s: authentication failed\n", username); + else + log_ppp_info1("authentication failed\n"); + ppp_terminate(ppp, TERM_AUTH_ERROR, 0); +} + +int __export ppp_auth_register_handler(struct ppp_auth_handler_t *h) +{ + list_add_tail(&h->entry, &auth_handlers); + return 0; +} + +int __export ppp_auth_restart(struct ppp_t *ppp) +{ + struct auth_layer_data_t *ad = container_of(ppp_find_layer_data(ppp, &auth_layer), typeof(*ad), ld); + log_ppp_debug("ppp_auth_restart\n"); + + if (!ad->auth_opt.auth->h->restart) + return -1; + + if (ad->auth_opt.auth->h->restart(ppp, ad->auth_opt.auth)) + return -1; + + return 0; +} + +static void __init ppp_auth_init() +{ + ppp_register_layer("auth", &auth_layer); + lcp_option_register(&auth_opt_hnd); +} + diff --git a/accel-pppd/ppp/ppp_auth.h b/accel-pppd/ppp/ppp_auth.h new file mode 100644 index 00000000..87cc7420 --- /dev/null +++ b/accel-pppd/ppp/ppp_auth.h @@ -0,0 +1,37 @@ +#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 (*check)(uint8_t *); + int (*restart)(struct ppp_t*,struct auth_data_t*); +}; + +int ppp_auth_register_handler(struct ppp_auth_handler_t*); + +void ppp_auth_successed(struct ppp_t *ppp, char *username); +void ppp_auth_failed(struct ppp_t *ppp, const char *username); +int ppp_auth_restart(struct ppp_t *ppp); + +#endif + diff --git a/accel-pppd/ppp/ppp_ccp.c b/accel-pppd/ppp/ppp_ccp.c new file mode 100644 index 00000000..721dd9b6 --- /dev/null +++ b/accel-pppd/ppp/ppp_ccp.c @@ -0,0 +1,759 @@ +#include <stdlib.h> +#include <string.h> +#include <errno.h> +#include <sys/socket.h> +#include <sys/ioctl.h> +#include "linux_ppp.h" + +#include "triton.h" + +#include "log.h" +#include "events.h" + +#include "ppp.h" +#include "ppp_ccp.h" + +#include "memdebug.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 int conf_ccp = 1; + +static struct ppp_layer_t ccp_layer; +static LIST_HEAD(option_handlers); + +static void ccp_layer_up(struct ppp_fsm_t*); +static void ccp_layer_down(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*); +static void send_term_req(struct ppp_fsm_t *fsm); +static void send_term_ack(struct ppp_fsm_t *fsm); +static void ccp_recv(struct ppp_handler_t*); +static void ccp_recv_proto_rej(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; + + ccp->conf_req_len = sizeof(struct ccp_hdr_t); + + 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 int ccp_set_flags(int fd, int isopen, int isup) +{ + int flags; + + if (ioctl(fd, PPPIOCGFLAGS, &flags)) { + log_ppp_error("ccp: failed to get flags: %s\n", strerror(errno)); + return -1; + } + + flags &= ~(SC_CCP_OPEN | SC_CCP_UP); + flags |= (isopen ? SC_CCP_OPEN : 0) | (isup ? SC_CCP_UP : 0); + + if (ioctl(fd, PPPIOCSFLAGS, &flags)) { + log_ppp_error("ccp: failed to set flags: %s\n", strerror(errno)); + return -1; + } + + return 0; +} + +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_ppp_debug("ccp_layer_init\n"); + + ccp->ppp = ppp; + ccp->fsm.ppp = ppp; + + ccp->hnd.proto = PPP_CCP; + ccp->hnd.recv = ccp_recv; + ccp->hnd.recv_proto_rej = ccp_recv_proto_rej; + + ppp_register_unit_handler(ppp, &ccp->hnd); + + INIT_LIST_HEAD(&ccp->options); + ccp_options_init(ccp); + + ccp->passive = 0; + + ccp->fsm.proto = PPP_CCP; + 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; + ccp->fsm.send_term_req = send_term_req; + ccp->fsm.send_term_ack = send_term_ack; + + INIT_LIST_HEAD(&ccp->ropt_list); + + return &ccp->ld; +} + +int ccp_layer_start(struct ppp_layer_data_t *ld) +{ + struct ppp_ccp_t *ccp = container_of(ld, typeof(*ccp), ld); + + log_ppp_debug("ccp_layer_start\n"); + + if (list_empty(&ccp->options) || !conf_ccp) { + ppp_layer_started(ccp->ppp, &ccp->ld); + return 0; + } + + ppp_fsm_lower_up(&ccp->fsm); + if (ppp_fsm_open(&ccp->fsm)) + return -1; + + if (ccp_set_flags(ccp->ppp->unit_fd, 1, 0)) { + ppp_fsm_close(&ccp->fsm); + return -1; + } + + return 0; +} + +void ccp_layer_finish(struct ppp_layer_data_t *ld) +{ + struct ppp_ccp_t *ccp = container_of(ld, typeof(*ccp), ld); + + log_ppp_debug("ccp_layer_finish\n"); + + ccp_set_flags(ccp->ppp->unit_fd, 0, 0); + + ccp->fsm.fsm_state = FSM_Closed; + + log_ppp_debug("ccp_layer_finished\n"); + ppp_layer_finished(ccp->ppp, &ccp->ld); +} + +void ccp_layer_free(struct ppp_layer_data_t *ld) +{ + struct ppp_ccp_t *ccp = container_of(ld, typeof(*ccp), ld); + + log_ppp_debug("ccp_layer_free\n"); + + ppp_unregister_handler(ccp->ppp, &ccp->hnd); + ccp_options_free(ccp); + ppp_fsm_free(&ccp->fsm); + + _free(ccp); +} + +static void ccp_layer_up(struct ppp_fsm_t *fsm) +{ + struct ppp_ccp_t *ccp = container_of(fsm, typeof(*ccp), fsm); + + log_ppp_debug("ccp_layer_started\n"); + + if (!ccp->started) { + ccp->started = 1; + if (ccp_set_flags(ccp->ppp->unit_fd, 1, 1)) { + ppp_terminate(ccp->ppp, TERM_NAS_ERROR, 0); + return; + } + 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_ppp_debug("ccp_layer_finished\n"); + + if (!ccp->started) + ppp_layer_started(ccp->ppp, &ccp->ld); + ccp->started = 0; + 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_ppp_info2("<"); + for (i = 0; i < ropt->len; i++) { + log_ppp_info2(" %x", ptr[i]); + } + log_ppp_info2(" >"); +} + +static int send_conf_req(struct ppp_fsm_t *fsm) +{ + struct ppp_ccp_t *ccp = container_of(fsm, typeof(*ccp), fsm); + uint8_t *buf, *ptr; + struct ccp_hdr_t *ccp_hdr; + struct ccp_option_t *lopt; + int n; + + ccp->need_req = 0; + + if (ccp->passive) { + ccp->passive--; + return 0; + } + + buf = _malloc(ccp->conf_req_len); + ccp_hdr = (struct ccp_hdr_t*)buf; + + ccp_hdr->proto = htons(PPP_CCP); + ccp_hdr->code = CONFREQ; + ccp_hdr->id = ++ccp->fsm.id; + ccp_hdr->len = 0; + + ptr = (uint8_t*)(ccp_hdr + 1); + + if (conf_ppp_verbose) + log_ppp_info2("send [CCP ConfReq id=%x", ccp_hdr->id); + + list_for_each_entry(lopt, &ccp->options, entry) { + n = lopt->h->send_conf_req(ccp, lopt, ptr); + if (n < 0) + return -1; + if (n) { + if (conf_ppp_verbose) { + log_ppp_info2(" "); + lopt->h->print(log_ppp_info2, lopt, NULL); + } + } + ptr += n; + } + + if (conf_ppp_verbose) + log_ppp_info2("]\n"); + + ccp_hdr->len = htons(ptr - buf - 2); + ppp_unit_send(ccp->ppp, ccp_hdr, ptr - buf); + + _free(buf); + + return 0; +} + +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; + + if (conf_ppp_verbose) + log_ppp_info2("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; + + if (conf_ppp_verbose) + log_ppp_info2("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) { + if (conf_ppp_verbose) { + log_ppp_info2(" "); + ropt->lopt->h->print(log_ppp_info2, ropt->lopt, NULL); + } + ptr += ropt->lopt->h->send_conf_nak(ccp, ropt->lopt, ptr); + } + } + + if (conf_ppp_verbose) + log_ppp_info2("]\n"); + + 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 + sizeof(struct ccp_hdr_t)), *ptr = buf; + struct ccp_hdr_t *ccp_hdr = (struct ccp_hdr_t*)ptr; + struct recv_opt_t *ropt; + + if (conf_ppp_verbose) + log_ppp_info2("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) { + if (conf_ppp_verbose) { + log_ppp_info2(" "); + if (ropt->lopt) + ropt->lopt->h->print(log_ppp_info2, ropt->lopt, (uint8_t*)ropt->hdr); + else + print_ropt(ropt); + } + memcpy(ptr, ropt->hdr, ropt->len); + ptr += ropt->len; + } + } + + if (conf_ppp_verbose) + log_ppp_info2("]\n"); + + 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) +{ + struct ccp_opt_hdr_t *hdr; + struct recv_opt_t *ropt; + struct ccp_option_t *lopt; + int r, ret = 1, ack = 0; + + ccp->need_req = 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; + } + + if (conf_ppp_verbose) + log_ppp_info2("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) { + if (conf_ppp_verbose) { + log_ppp_info2(" "); + lopt->h->print(log_ppp_info2, 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 { + /*if (lopt->state == CCP_OPT_NAK && r == CCP_OPT_ACK) + ccp->need_req = 1;*/ + lopt->state = r; + ropt->state = r; + } + ropt->lopt = lopt; + if (r < ret) + ret = r; + break; + } + } + if (ropt->state == CCP_OPT_ACK || ropt->state == CCP_OPT_NAK) + ack = 1; + else if (!ropt->lopt) { + if (conf_ppp_verbose) { + log_ppp_info2(" "); + print_ropt(ropt); + } + ropt->state = CCP_OPT_REJ; + ret = CCP_OPT_REJ; + } + } + + if (conf_ppp_verbose) + log_ppp_info2("]\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 (r<ret) ret=r; + } + }*/ + + return ret; +} + +static void ccp_free_conf_req(struct ppp_ccp_t *ccp) +{ + struct recv_opt_t *ropt; + + while (!list_empty(&ccp->ropt_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; + + if (conf_ppp_verbose) + log_ppp_info2("recv [CCP ConfRej id=%x", ccp->fsm.recv_id); + + if (ccp->fsm.recv_id != ccp->fsm.id) { + if (conf_ppp_verbose) + log_ppp_info2(": 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; + } + + if (conf_ppp_verbose) + log_ppp_info2("]\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; + + if (conf_ppp_verbose) + log_ppp_info2("recv [CCP ConfNak id=%x", ccp->fsm.recv_id); + + if (ccp->fsm.recv_id != ccp->fsm.id) { + if (conf_ppp_verbose) + log_ppp_info2(": 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 (conf_ppp_verbose) { + log_ppp_info2(" "); + lopt->h->print(log_ppp_info2, lopt, data); + } + if (lopt->h->recv_conf_nak && lopt->h->recv_conf_nak(ccp, lopt, data)) + res = -1; + //lopt->state = CCP_OPT_NAK; + //ccp->need_req = 1; + break; + } + } + + data += hdr->len; + size -= hdr->len; + } + + if (conf_ppp_verbose) + log_ppp_info2("]\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; + + if (conf_ppp_verbose) + log_ppp_info2("recv [CCP ConfAck id=%x", ccp->fsm.recv_id); + + if (ccp->fsm.recv_id != ccp->fsm.id) { + if (conf_ppp_verbose) + log_ppp_info2(": 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 (conf_ppp_verbose) { + log_ppp_info2(" "); + lopt->h->print(log_ppp_info2,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; + } + + if (conf_ppp_verbose) + log_ppp_info2("]\n"); + + return res; +} + +static void send_term_req(struct ppp_fsm_t *fsm) +{ + struct ppp_ccp_t *ccp = container_of(fsm, typeof(*ccp), fsm); + struct ccp_hdr_t hdr = { + .proto = htons(PPP_CCP), + .code = TERMREQ, + .id = ++ccp->fsm.id, + .len = htons(4), + }; + + if (conf_ppp_verbose) + log_ppp_info2("send [CCP TermReq id=%i]\n", hdr.id); + + ppp_chan_send(ccp->ppp, &hdr, 6); +} + +static void send_term_ack(struct ppp_fsm_t *fsm) +{ + struct ppp_ccp_t *ccp = container_of(fsm, typeof(*ccp), fsm); + struct ccp_hdr_t hdr = { + .proto = htons(PPP_CCP), + .code = TERMACK, + .id = ccp->fsm.recv_id, + .len = htons(4), + }; + + if (conf_ppp_verbose) + log_ppp_info2("send [CCP TermAck id=%i]\n", hdr.id); + + ppp_chan_send(ccp->ppp, &hdr, 6); +} + +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; + + if (ccp->fsm.fsm_state == FSM_Initial || ccp->fsm.fsm_state == FSM_Closed || ccp->ppp->terminating) { + if (conf_ppp_verbose) + log_ppp_warn("CCP: discarding packet\n"); + if (ccp->fsm.fsm_state == FSM_Closed || !conf_ccp) + lcp_send_proto_rej(ccp->ppp, PPP_CCP); + return; + } + + if (ccp->ppp->unit_buf_size < PPP_HEADERLEN + 2) { + log_ppp_warn("CCP: short packet received\n"); + return; + } + + hdr = (struct ccp_hdr_t *)ccp->ppp->unit_buf; + if (ntohs(hdr->len) < PPP_HEADERLEN) { + log_ppp_warn("CCP: short packet received\n"); + return; + } + + ccp->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_ACK && ccp->passive) { + ccp->passive = 0; + send_conf_req(&ccp->fsm); + } + if (r == CCP_OPT_FAIL) + ppp_terminate(ccp->ppp, TERM_USER_ERROR, 0); + break; + case CONFACK: + if (ccp_recv_conf_ack(ccp, (uint8_t*)(hdr + 1), ntohs(hdr->len) - PPP_HDRLEN)) + ppp_terminate(ccp->ppp, TERM_USER_ERROR, 0); + else { + ppp_fsm_recv_conf_ack(&ccp->fsm); + if (ccp->need_req) + send_conf_req(&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, TERM_USER_ERROR, 0); + else + ppp_fsm_recv_conf_rej(&ccp->fsm); + break; + case TERMREQ: + if (conf_ppp_verbose) + log_ppp_info2("recv [CCP TermReq id=%x]\n", hdr->id); + ppp_fsm_recv_term_req(&ccp->fsm); + ppp_fsm_close(&ccp->fsm); + break; + case TERMACK: + if (conf_ppp_verbose) + log_ppp_info2("recv [CCP TermAck id=%x]\n", hdr->id); + ppp_fsm_recv_term_ack(&ccp->fsm); + break; + case CODEREJ: + if (conf_ppp_verbose) + log_ppp_info2("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; + } +} + +static void ccp_recv_proto_rej(struct ppp_handler_t *h) +{ + struct ppp_ccp_t *ccp = container_of(h, typeof(*ccp), hnd); + + if (ccp->fsm.fsm_state == FSM_Initial || ccp->fsm.fsm_state == FSM_Closed) + return; + + ppp_fsm_lower_down(&ccp->fsm); + ppp_fsm_close(&ccp->fsm); +} + +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; +} + +struct ccp_option_t *ccp_find_option(struct ppp_t *ppp, struct ccp_option_handler_t *h) +{ + struct ppp_ccp_t *ccp = container_of(ppp_find_layer_data(ppp, &ccp_layer), typeof(*ccp), ld); + struct ccp_option_t *opt; + + list_for_each_entry(opt, &ccp->options, entry) + if (opt->h == h) + return opt; + + log_emerg("ccp: BUG: option not found\n"); + abort(); +} + +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 load_config(void) +{ + const char *opt; + + opt = conf_get_opt("ppp", "ccp"); + if (opt && atoi(opt) >= 0) + conf_ccp = atoi(opt); +} + +static void __init ccp_init(void) +{ + ppp_register_layer("ccp", &ccp_layer); + + load_config(); + triton_event_register_handler(EV_CONFIG_RELOAD, (triton_event_func)load_config); +} + diff --git a/accel-pppd/ppp/ppp_ccp.h b/accel-pppd/ppp/ppp_ccp.h new file mode 100644 index 00000000..2c2dc6dd --- /dev/null +++ b/accel-pppd/ppp/ppp_ccp.h @@ -0,0 +1,96 @@ +#ifndef PPP_CCP_H +#define PPP_CCP_H + +#include <stdint.h> + +#include "triton.h" +#include "ppp_fsm.h" +/* + * Options. + */ + +#define CI_MPPE 18 /* MPPE */ + +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 passive; + int started:1; + int need_req:1; +}; + +int ccp_option_register(struct ccp_option_handler_t *h); +struct ccp_option_t *ccp_find_option(struct ppp_t *ppp, struct ccp_option_handler_t *h); + +#endif + diff --git a/accel-pppd/ppp/ppp_fsm.c b/accel-pppd/ppp/ppp_fsm.c new file mode 100644 index 00000000..c6bc430e --- /dev/null +++ b/accel-pppd/ppp/ppp_fsm.c @@ -0,0 +1,544 @@ +#include <arpa/inet.h> +#include <stdlib.h> + +#include "triton.h" + +#include "ppp.h" +#include "ppp_fsm.h" +#include "ppp_lcp.h" +#include "log.h" +#include "events.h" + +#include "memdebug.h" + +static int conf_max_terminate = 2; +static int conf_max_configure = 10; +static int conf_max_failure = 10; +static int conf_timeout = 5; + +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_timer.period = conf_timeout * 1000; + 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); +} + +int 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) + 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) +{ + 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; + } +} + +int 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) + if (layer->send_conf_req(layer)) + return -1; + 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; + } + return 0; +} + +void ppp_fsm_close(struct ppp_fsm_t *layer) +{ + switch(layer->fsm_state) + { + case FSM_Starting: + stop_timer(layer); + layer->fsm_state=FSM_Initial; + if (layer->layer_finished) layer->layer_finished(layer); + break; + case FSM_Stopped: + layer->fsm_state=FSM_Closed; + stop_timer(layer); + 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); + layer->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: + --layer->restart_counter; + layer->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; + --layer->id; + 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); + layer->fsm_state=FSM_Closed; + if (layer->layer_finished) layer->layer_finished(layer); + break; + case FSM_Stopping: + stop_timer(layer); + layer->fsm_state=FSM_Stopped; + if (layer->layer_finished) layer->layer_finished(layer); + break; + case FSM_Ack_Rcvd: + case FSM_Req_Sent: + case FSM_Ack_Sent: + stop_timer(layer); + layer->fsm_state=FSM_Stopped; + if (layer->layer_finished) layer->layer_finished(layer); + break; + default: + break; + } +} + +void ppp_fsm_recv_conf_req_ack(struct ppp_fsm_t *layer) +{ + switch(layer->fsm_state) + { + case FSM_Closed: + layer->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: + layer->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->conf_failure == layer->max_failure) { + if (layer->layer_finished) layer->layer_finished(layer); + return; + } + 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: + layer->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->conf_failure == layer->max_failure) { + if (layer->layer_down) layer->layer_down(layer); + return; + } + 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->conf_failure == layer->max_failure) { + if (layer->layer_finished) layer->layer_finished(layer); + return; + } + 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: + layer->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: + layer->send_term_ack(layer); + break; + case FSM_Req_Sent: + if (++layer->conf_failure == layer->max_failure) { + if (layer->layer_down) layer->layer_down(layer); + return; + } + //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); + 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: + layer->send_term_ack(layer); + layer->fsm_state=FSM_Req_Sent; + break; + default: + layer->send_term_ack(layer); + break; + } +} + +void ppp_fsm_recv_term_ack(struct ppp_fsm_t *layer) +{ + stop_timer(layer); + switch(layer->fsm_state) + { + case FSM_Closing: + layer->fsm_state=FSM_Closed; + if (layer->layer_finished) layer->layer_finished(layer); + break; + case FSM_Stopping: + layer->fsm_state=FSM_Stopped; + if (layer->layer_finished) layer->layer_finished(layer); + 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_code_rej) layer->send_code_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); + --layer->restart_counter; + layer->send_term_req(layer); + layer->fsm_state=FSM_Stopping; + break; + case FSM_Closing: + layer->fsm_state=FSM_Closed; + if (layer->layer_finished) layer->layer_finished(layer); + break; + case FSM_Stopping: + case FSM_Req_Sent: + case FSM_Ack_Rcvd: + case FSM_Ack_Sent: + layer->fsm_state=FSM_Stopped; + if (layer->layer_finished) layer->layer_finished(layer); + break; + default: + break; + } +} + +static void stop_timer(struct ppp_fsm_t *fsm) +{ + if (fsm->restart_timer.tpd) + triton_timer_del(&fsm->restart_timer); +} +static void init_req_counter(struct ppp_fsm_t *layer,int timeout) +{ + layer->restart_counter = timeout; + + if (!layer->restart_timer.tpd) + triton_timer_add(layer->ppp->ctrl->ctx, &layer->restart_timer, 0); +} +static void zero_req_counter(struct ppp_fsm_t *layer) +{ + layer->restart_counter=0; + + if (!layer->restart_timer.tpd) + triton_timer_add(layer->ppp->ctrl->ctx, &layer->restart_timer, 0); +} + +static void restart_timer_func(struct triton_timer_t *t) +{ + struct ppp_fsm_t *layer = container_of(t, typeof(*layer), restart_timer); + + log_ppp_debug("fsm timeout\n"); + + if (layer->restart_counter>0) + ppp_fsm_timeout0(layer); + else + ppp_fsm_timeout1(layer); +} + +static void load_config(void) +{ + char *opt; + + opt = conf_get_opt("ppp", "max-terminate"); + if (opt && atoi(opt) > 0) + conf_max_terminate = atoi(opt); + + opt = conf_get_opt("ppp", "max-configure"); + if (opt && atoi(opt) > 0) + conf_max_configure = atoi(opt); + + opt = conf_get_opt("ppp", "max-failure"); + if (opt && atoi(opt) > 0) + conf_max_failure = atoi(opt); + + opt = conf_get_opt("ppp", "timeout"); + if (opt && atoi(opt) > 0) + conf_timeout = atoi(opt); +} + +void __init fsm_init(void) +{ + load_config(); + triton_event_register_handler(EV_CONFIG_RELOAD, (triton_event_func)load_config); +} + diff --git a/accel-pppd/ppp/ppp_fsm.h b/accel-pppd/ppp/ppp_fsm.h new file mode 100644 index 00000000..6010240a --- /dev/null +++ b/accel-pppd/ppp/ppp_fsm.h @@ -0,0 +1,72 @@ +#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 PROTOREJ 8 /* Code Reject */ +#define ECHOREQ 9 /* Echo Request */ +#define ECHOREP 10 /* Echo Reply */ +#define IDENT 12 /* Identification */ + +struct ppp_t; + +struct ppp_fsm_t +{ + struct ppp_t *ppp; + FSM_STATE fsm_state; + uint16_t proto; + + struct triton_timer_t restart_timer; + int restart_counter; + int max_terminate; + int max_configure; + int max_failure; + int timeout; + int conf_failure; + + 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*); + 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*); + void (*send_code_rej)(struct ppp_fsm_t*); + void (*send_term_req)(struct ppp_fsm_t*); + void (*send_term_ack)(struct ppp_fsm_t*); +}; + +void ppp_fsm_init(struct ppp_fsm_t*); +void ppp_fsm_free(struct ppp_fsm_t*); + +int ppp_fsm_lower_up(struct ppp_fsm_t*); +void ppp_fsm_lower_down(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); +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-pppd/ppp/ppp_ipcp.c b/accel-pppd/ppp/ppp_ipcp.c new file mode 100644 index 00000000..7cdcdbc1 --- /dev/null +++ b/accel-pppd/ppp/ppp_ipcp.c @@ -0,0 +1,665 @@ +#include <stdlib.h> +#include <string.h> +#include <sys/socket.h> +#include <arpa/inet.h> +#include "linux_ppp.h" + +#include "triton.h" + +#include "log.h" + +#include "ppp.h" +#include "ppp_ipcp.h" + +#include "memdebug.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 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*); +static void ipcp_recv(struct ppp_handler_t*); +static void send_term_req(struct ppp_fsm_t *fsm); +static void send_term_ack(struct ppp_fsm_t *fsm); + +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); + 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_ppp_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); + + ipcp->fsm.proto = PPP_IPCP; + 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; + ipcp->fsm.send_term_req = send_term_req; + ipcp->fsm.send_term_ack = send_term_ack; + + INIT_LIST_HEAD(&ipcp->options); + INIT_LIST_HEAD(&ipcp->ropt_list); + + return &ipcp->ld; +} + +int ipcp_layer_start(struct ppp_layer_data_t *ld) +{ + struct ppp_ipcp_t *ipcp = container_of(ld, typeof(*ipcp), ld); + + log_ppp_debug("ipcp_layer_start\n"); + + ipcp_options_init(ipcp); + ppp_fsm_lower_up(&ipcp->fsm); + if (ppp_fsm_open(&ipcp->fsm)) + return -1; + + return 0; +} + +void ipcp_layer_finish(struct ppp_layer_data_t *ld) +{ + struct ppp_ipcp_t *ipcp = container_of(ld, typeof(*ipcp), ld); + + log_ppp_debug("ipcp_layer_finish\n"); + + ipcp->fsm.fsm_state = FSM_Closed; + + log_ppp_debug("ipcp_layer_finished\n"); + ppp_layer_finished(ipcp->ppp, &ipcp->ld); +} + +void ipcp_layer_free(struct ppp_layer_data_t *ld) +{ + struct ppp_ipcp_t *ipcp = container_of(ld, typeof(*ipcp), ld); + + log_ppp_debug("ipcp_layer_free\n"); + + ppp_unregister_handler(ipcp->ppp, &ipcp->hnd); + ipcp_options_free(ipcp); + ppp_fsm_free(&ipcp->fsm); + + _free(ipcp); +} + +static void ipcp_layer_up(struct ppp_fsm_t *fsm) +{ + struct ppp_ipcp_t *ipcp = container_of(fsm, typeof(*ipcp), fsm); + + log_ppp_debug("ipcp_layer_started\n"); + + if (!ipcp->started) { + ipcp->started = 1; + 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_ppp_debug("ipcp_layer_finished\n"); + + if (ipcp->started) { + ipcp->started = 0; + ppp_layer_finished(ipcp->ppp, &ipcp->ld); + } else + ppp_terminate(ipcp->ppp, TERM_NAS_ERROR, 0); +} + +static void print_ropt(struct recv_opt_t *ropt) +{ + int i; + uint8_t *ptr = (uint8_t*)ropt->hdr; + + log_ppp_info2("<"); + for (i = 0; i < ropt->len; i++) { + log_ppp_info2(" %x", ptr[i]); + } + log_ppp_info2(" >"); +} + +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; + struct ipcp_hdr_t *ipcp_hdr = (struct ipcp_hdr_t*)ptr; + struct ipcp_option_t *lopt; + int n; + + ipcp_hdr->proto = htons(PPP_IPCP); + ipcp_hdr->code = CONFREQ; + ipcp_hdr->id = ++ipcp->fsm.id; + ipcp_hdr->len = 0; + + ptr += sizeof(*ipcp_hdr); + + list_for_each_entry(lopt, &ipcp->options, entry) { + 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_ppp_info2("send [IPCP ConfReq id=%x", ipcp_hdr->id); + list_for_each_entry(lopt,&ipcp->options,entry) { + if (lopt->print) { + log_ppp_info2(" "); + lopt->h->print(log_ppp_info2, lopt, NULL); + } + } + log_ppp_info2("]\n"); + } + + ipcp_hdr->len = htons(ptr - buf - 2); + ppp_unit_send(ipcp->ppp, ipcp_hdr, ptr - buf); + + _free(buf); + + return 0; +} + +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; + + if (conf_ppp_verbose) + log_ppp_info2("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; + + if (conf_ppp_verbose) + log_ppp_info2("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) { + if (conf_ppp_verbose) { + log_ppp_info2(" "); + ropt->lopt->h->print(log_ppp_info2, ropt->lopt, NULL); + } + ptr += ropt->lopt->h->send_conf_nak(ipcp, ropt->lopt, ptr); + } + } + + if (conf_ppp_verbose) + log_ppp_info2("]\n"); + + 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 + sizeof(struct ipcp_hdr_t)), *ptr = buf; + struct ipcp_hdr_t *ipcp_hdr = (struct ipcp_hdr_t*)ptr; + struct recv_opt_t *ropt; + + if (conf_ppp_verbose) + log_ppp_info2("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) { + if (conf_ppp_verbose) { + log_ppp_info2(" "); + if (ropt->lopt) + ropt->lopt->h->print(log_ppp_info2, ropt->lopt, (uint8_t*)ropt->hdr); + else + print_ropt(ropt); + } + memcpy(ptr, ropt->hdr, ropt->len); + ptr += ropt->len; + } + } + + if (conf_ppp_verbose) + log_ppp_info2("]\n"); + + 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) +{ + 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; + + if (conf_ppp_verbose) { + log_ppp_info2("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) { + ropt->lopt = lopt; + log_ppp_info2(" "); + lopt->h->print(log_ppp_info2, lopt, (uint8_t*)ropt->hdr); + break; + } + } + if (!ropt->lopt) { + log_ppp_info2(" "); + print_ropt(ropt); + } + } + log_ppp_info2("]\n"); + } + + list_for_each_entry(ropt, &ipcp->ropt_list, entry) { + list_for_each_entry(lopt, &ipcp->options, entry) { + if (lopt->id == ropt->hdr->id) { + r = lopt->h->recv_conf_req(ipcp, lopt, (uint8_t*)ropt->hdr); + if (ipcp->ppp->stop_time) + return -1; + lopt->state = r; + ropt->state = r; + ropt->lopt = lopt; + if (r < ret) + ret = r; + break; + } + } + if (!ropt->lopt) { + ropt->state = IPCP_OPT_REJ; + ret = IPCP_OPT_REJ; + } + } + + + /*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; + + if (conf_ppp_verbose) + log_ppp_info2("recv [IPCP ConfRej id=%x", ipcp->fsm.recv_id); + + if (ipcp->fsm.recv_id != ipcp->fsm.id) { + if (conf_ppp_verbose) + log_ppp_info2(": 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; + } + + if (conf_ppp_verbose) + log_ppp_info2("]\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; + + if (conf_ppp_verbose) + log_ppp_info2("recv [IPCP ConfNak id=%x", ipcp->fsm.recv_id); + + if (ipcp->fsm.recv_id != ipcp->fsm.id) { + if (conf_ppp_verbose) + log_ppp_info2(": 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 (conf_ppp_verbose) { + log_ppp_info2(" "); + lopt->h->print(log_ppp_info2,lopt,data); + } + if (lopt->h->recv_conf_nak && lopt->h->recv_conf_nak(ipcp, lopt, data)) + res =- 1; + break; + } + } + + data += hdr->len; + size -= hdr->len; + } + + if (conf_ppp_verbose) + log_ppp_info2("]\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; + + if (conf_ppp_verbose) + log_ppp_info2("recv [IPCP ConfAck id=%x", ipcp->fsm.recv_id); + + if (ipcp->fsm.recv_id != ipcp->fsm.id) { + if (conf_ppp_verbose) + log_ppp_info2(": 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 (conf_ppp_verbose) { + log_ppp_info2(" "); + lopt->h->print(log_ppp_info2, 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; + } + + if (conf_ppp_verbose) + log_ppp_info2("]\n"); + + return res; +} + +static void send_term_req(struct ppp_fsm_t *fsm) +{ + struct ppp_ipcp_t *ipcp = container_of(fsm, typeof(*ipcp), fsm); + struct ipcp_hdr_t hdr = { + .proto = htons(PPP_IPCP), + .code = TERMREQ, + .id = ++ipcp->fsm.id, + .len = htons(4), + }; + + if (conf_ppp_verbose) + log_ppp_info2("send [IPCP TermReq id=%i]\n", hdr.id); + + ppp_unit_send(ipcp->ppp, &hdr, 6); +} + +static void send_term_ack(struct ppp_fsm_t *fsm) +{ + struct ppp_ipcp_t *ipcp = container_of(fsm, typeof(*ipcp), fsm); + struct ipcp_hdr_t hdr = { + .proto = htons(PPP_IPCP), + .code = TERMACK, + .id = ipcp->fsm.recv_id, + .len = htons(4), + }; + + if (conf_ppp_verbose) + log_ppp_info2("send [IPCP TermAck id=%i]\n", hdr.id); + + ppp_unit_send(ipcp->ppp, &hdr, 6); +} + +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; + + if (ipcp->fsm.fsm_state == FSM_Initial || ipcp->fsm.fsm_state == FSM_Closed || ipcp->ppp->terminating) { + if (conf_ppp_verbose) + log_ppp_warn("IPCP: discarding packet\n"); + return; + } + + if (ipcp->ppp->unit_buf_size < PPP_HEADERLEN + 2) { + log_ppp_warn("IPCP: short packet received\n"); + return; + } + + hdr = (struct ipcp_hdr_t *)ipcp->ppp->unit_buf; + if (ntohs(hdr->len) < PPP_HEADERLEN) { + log_ppp_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); + if (ipcp->ppp->stop_time) { + ipcp_free_conf_req(ipcp); + return; + } + 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, TERM_USER_ERROR, 0); + break; + case CONFACK: + if (ipcp_recv_conf_ack(ipcp,(uint8_t*)(hdr + 1), ntohs(hdr->len) - PPP_HDRLEN)) + ppp_terminate(ipcp->ppp, TERM_USER_ERROR, 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, TERM_USER_ERROR, 0); + else + ppp_fsm_recv_conf_rej(&ipcp->fsm); + break; + case TERMREQ: + if (conf_ppp_verbose) + log_ppp_info2("recv [IPCP TermReq id=%x]\n", hdr->id); + ppp_fsm_recv_term_req(&ipcp->fsm); + ppp_terminate(ipcp->ppp, TERM_USER_REQUEST, 0); + break; + case TERMACK: + if (conf_ppp_verbose) + log_ppp_info2("recv [IPCP TermAck id=%x]\n", hdr->id); + //ppp_fsm_recv_term_ack(&ipcp->fsm); + //ppp_terminate(ipcp->ppp, 0); + break; + case CODEREJ: + if (conf_ppp_verbose) + log_ppp_info2("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-pppd/ppp/ppp_ipcp.h b/accel-pppd/ppp/ppp_ipcp.h new file mode 100644 index 00000000..c9559872 --- /dev/null +++ b/accel-pppd/ppp/ppp_ipcp.h @@ -0,0 +1,96 @@ +#ifndef PPP_IPCP_H +#define PPP_IPCP_H + +#include <stdint.h> + +#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; + int print:1; + 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 started:1; +}; + +int ipcp_option_register(struct ipcp_option_handler_t *h); + +#endif + diff --git a/accel-pppd/ppp/ppp_lcp.c b/accel-pppd/ppp/ppp_lcp.c new file mode 100644 index 00000000..e40e3212 --- /dev/null +++ b/accel-pppd/ppp/ppp_lcp.c @@ -0,0 +1,847 @@ +#include <stdlib.h> +#include <string.h> +#include <sys/socket.h> +#include <arpa/inet.h> +#include "linux_ppp.h" + +#include "triton.h" + +#include "log.h" + +#include "ppp.h" +#include "ppp_lcp.h" +#include "events.h" + +#include "memdebug.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 struct ppp_layer_t lcp_layer; + +static void lcp_layer_up(struct ppp_fsm_t*); +static void lcp_layer_down(struct ppp_fsm_t*); +static void lcp_layer_finished(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*); +static void send_code_rej(struct ppp_fsm_t*); +static void start_echo(struct ppp_lcp_t *lcp); +static void stop_echo(struct ppp_lcp_t *lcp); +static void send_term_req(struct ppp_fsm_t *fsm); +static void send_term_ack(struct ppp_fsm_t *fsm); +static void lcp_recv(struct ppp_handler_t*); + +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); + + lcp->conf_req_len = sizeof(struct lcp_hdr_t); + + 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_ppp_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); + + lcp->fsm.proto = PPP_LCP; + ppp_fsm_init(&lcp->fsm); + + lcp->fsm.layer_up = lcp_layer_up; + lcp->fsm.layer_down = lcp_layer_down; + lcp->fsm.layer_finished = lcp_layer_finished; + 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; + lcp->fsm.send_code_rej = send_code_rej; + lcp->fsm.send_term_req = send_term_req; + lcp->fsm.send_term_ack = send_term_ack; + + INIT_LIST_HEAD(&lcp->ropt_list); + + return &lcp->ld; +} + +int lcp_layer_start(struct ppp_layer_data_t *ld) +{ + struct ppp_lcp_t *lcp = container_of(ld, typeof(*lcp), ld); + + log_ppp_debug("lcp_layer_start\n"); + + lcp_options_init(lcp); + ppp_fsm_lower_up(&lcp->fsm); + if (ppp_fsm_open(&lcp->fsm)) + return -1; + + return 0; +} + +static void _lcp_layer_finished(struct ppp_lcp_t *lcp) +{ + ppp_layer_finished(lcp->ppp, &lcp->ld); +} + +void lcp_layer_finish(struct ppp_layer_data_t *ld) +{ + struct ppp_lcp_t *lcp = container_of(ld,typeof(*lcp),ld); + + log_ppp_debug("lcp_layer_finish\n"); + + if (lcp->started) { + stop_echo(lcp); + ppp_fsm_close(&lcp->fsm); + } else + triton_context_call(lcp->ppp->ctrl->ctx, (triton_event_func)_lcp_layer_finished, lcp); +} + +void lcp_layer_free(struct ppp_layer_data_t *ld) +{ + struct ppp_lcp_t *lcp = container_of(ld, typeof(*lcp), ld); + + log_ppp_debug("lcp_layer_free\n"); + + stop_echo(lcp); + ppp_unregister_handler(lcp->ppp, &lcp->hnd); + lcp_options_free(lcp); + ppp_fsm_free(&lcp->fsm); + triton_cancel_call(lcp->ppp->ctrl->ctx, (triton_event_func)_lcp_layer_finished); + + _free(lcp); +} + +static void lcp_layer_up(struct ppp_fsm_t *fsm) +{ + struct ppp_lcp_t *lcp = container_of(fsm, typeof(*lcp), fsm); + + log_ppp_debug("lcp_layer_started\n"); + + if (!lcp->started) { + lcp->started = 1; + 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); + //ppp_fsm_close(&lcp->fsm); + stop_echo(lcp); + //ppp_layer_finished(lcp->ppp,&lcp->ld); +} + +static void lcp_layer_finished(struct ppp_fsm_t *fsm) +{ + struct ppp_lcp_t *lcp = container_of(fsm, typeof(*lcp), fsm); + + log_ppp_debug("lcp_layer_finished\n"); + + stop_echo(lcp); + if (lcp->started) { + lcp->started = 0; + if (lcp->ppp->terminating) + ppp_layer_finished(lcp->ppp, &lcp->ld); + else + ppp_terminate(lcp->ppp, TERM_NAS_ERROR, 0); + } else + ppp_terminate(lcp->ppp, TERM_NAS_ERROR, 0); +} + +static void print_ropt(struct recv_opt_t *ropt) +{ + int i; + uint8_t *ptr = (uint8_t*)ropt->hdr; + + log_ppp_info2("<"); + for (i = 0; i < ropt->len; i++) { + log_ppp_info2(" %x", ptr[i]); + } + log_ppp_info2(" >"); +} + +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; + struct lcp_hdr_t *lcp_hdr = (struct lcp_hdr_t*)ptr; + struct lcp_option_t *lopt; + int n; + + lcp_hdr->proto = htons(PPP_LCP); + lcp_hdr->code = CONFREQ; + lcp_hdr->id = ++lcp->fsm.id; + lcp_hdr->len = 0; + + 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) { + ptr += n; + lopt->print = 1; + } else + lopt->print = 0; + } + + if (conf_ppp_verbose) { + log_ppp_info2("send [LCP ConfReq id=%x", lcp_hdr->id); + list_for_each_entry(lopt,&lcp->options,entry) { + if (lopt->print) { + log_ppp_info2(" "); + lopt->h->print(log_ppp_info2, lopt, NULL); + } + } + log_ppp_info2("]\n"); + } + + lcp_hdr->len = htons(ptr - buf - 2); + ppp_chan_send(lcp->ppp, lcp_hdr, ptr-buf); + + _free(buf); + + return 0; +} + +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; + + if (conf_ppp_verbose) + log_ppp_info2("send [LCP ConfAck id=%x ]\n", lcp->fsm.recv_id); + + ppp_chan_send(lcp->ppp, hdr, ntohs(hdr->len) + 2); +} + +static void send_code_rej(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; + + if (conf_ppp_verbose) + log_ppp_info2("send [LCP CodeRej %x id=%x ]\n",hdr->code, 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 lcp_option_t *lopt; + + if (conf_ppp_verbose) + log_ppp_info2("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(lopt, &lcp->options, entry) { + if (lopt->state == LCP_OPT_NAK) { + ptr+=lopt->h->send_conf_nak(lcp,lopt,ptr); + + if (conf_ppp_verbose) { + log_ppp_info2(" "); + lopt->h->print(log_ppp_info2, lopt, NULL); + } + } + } + + if (conf_ppp_verbose) + log_ppp_info2("]\n"); + + lcp_hdr->len = htons(ptr - buf - 2); + ppp_chan_send(lcp->ppp, lcp_hdr,ptr - buf); + + _free(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 + sizeof(struct lcp_hdr_t)), *ptr = buf; + struct lcp_hdr_t *lcp_hdr = (struct lcp_hdr_t*)ptr; + struct recv_opt_t *ropt; + + if (conf_ppp_verbose) + log_ppp_info2("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) { + memcpy(ptr, ropt->hdr, ropt->len); + ptr += ropt->len; + + if (conf_ppp_verbose) { + log_ppp_info2(" "); + if (ropt->lopt) + ropt->lopt->h->print(log_ppp_info2, ropt->lopt, (uint8_t*)ropt->hdr); + else + print_ropt(ropt); + } + } + } + + if (conf_ppp_verbose) + log_ppp_info2("]\n"); + + 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) +{ + 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)); + memset(ropt, 0, 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; + + if (conf_ppp_verbose) + log_ppp_info2("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) { + if (conf_ppp_verbose) { + log_ppp_info2(" "); + lopt->h->print(log_ppp_info2, 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 (r<ret) + ret = r; + break; + } + } + if (!ropt->lopt) { + if (conf_ppp_verbose) { + log_ppp_info2(" "); + print_ropt(ropt); + } + ropt->state=LCP_OPT_REJ; + ret=LCP_OPT_REJ; + } + } + + if (conf_ppp_verbose) + log_ppp_info2("]\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 (r<ret) ret=r; + } + }*/ + + return ret; +} + +static void lcp_free_conf_req(struct ppp_lcp_t *lcp) +{ + struct recv_opt_t *ropt; + + while (!list_empty(&lcp->ropt_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; + + if (conf_ppp_verbose) + log_ppp_info2("recv [LCP ConfRej id=%x", lcp->fsm.recv_id); + + if (lcp->fsm.recv_id != lcp->fsm.id) { + if (conf_ppp_verbose) + log_ppp_info2(": 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 (conf_ppp_verbose) { + log_ppp_info2(" "); + lopt->h->print(log_ppp_info2, lopt, (uint8_t*)hdr); + } + 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; + } + + if (conf_ppp_verbose) + log_ppp_info2("]\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; + + if (conf_ppp_verbose) + log_ppp_info2("recv [LCP ConfNak id=%x", lcp->fsm.recv_id); + + if (lcp->fsm.recv_id != lcp->fsm.id) { + if (conf_ppp_verbose) + log_ppp_info2(": 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 (conf_ppp_verbose) { + log_ppp_info2(" "); + lopt->h->print(log_ppp_info2, lopt, data); + } + if (lopt->h->recv_conf_nak && lopt->h->recv_conf_nak(lcp, lopt, data)) + res = -1; + break; + } + } + + data += hdr->len; + size -= hdr->len; + } + + if (conf_ppp_verbose) + log_ppp_info2("]\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; + + if (conf_ppp_verbose) + log_ppp_info2("recv [LCP ConfAck id=%x", lcp->fsm.recv_id); + + if (lcp->fsm.recv_id != lcp->fsm.id) { + if (conf_ppp_verbose) + log_ppp_info2(": 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 (conf_ppp_verbose) { + log_ppp_info2(" "); + lopt->h->print(log_ppp_info2, 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; + } + + if (conf_ppp_verbose) + log_ppp_info2("]\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_ppp_error("lcp:echo: magic number size mismatch\n"); + ppp_terminate(lcp->ppp, TERM_USER_ERROR, 0); + } + + if (conf_ppp_verbose) + log_ppp_debug("recv [LCP EchoRep id=%x <magic %x>]\n", lcp->fsm.recv_id, magic); + + if (magic == lcp->magic) { + log_ppp_error("lcp: echo: loop-back detected\n"); + ppp_terminate(lcp->ppp, TERM_NAS_ERROR, 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; + *(uint32_t *)(hdr + 1) = lcp->magic; + + if (conf_ppp_verbose) + log_ppp_debug("send [LCP EchoRep id=%x <magic %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_ppp_warn("lcp: no echo reply\n"); + ppp_terminate(lcp->ppp, TERM_USER_ERROR, 1); + } else { + if (conf_ppp_verbose) + log_ppp_debug("send [LCP EchoReq id=%x <magic %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 && !lcp->echo_timer.tpd) + triton_timer_add(lcp->ppp->ctrl->ctx, &lcp->echo_timer, 0); +} +static void stop_echo(struct ppp_lcp_t *lcp) +{ + if (lcp->echo_timer.tpd) + triton_timer_del(&lcp->echo_timer); +} + +static void send_term_req(struct ppp_fsm_t *fsm) +{ + struct ppp_lcp_t *lcp=container_of(fsm,typeof(*lcp),fsm); + struct lcp_hdr_t hdr = { + .proto = htons(PPP_LCP), + .code = TERMREQ, + .id = ++lcp->fsm.id, + .len = htons(4), + }; + + if (conf_ppp_verbose) + log_ppp_info2("send [LCP TermReq id=%i]\n", hdr.id); + + ppp_chan_send(lcp->ppp, &hdr, 6); +} + +static void send_term_ack(struct ppp_fsm_t *fsm) +{ + struct ppp_lcp_t *lcp = container_of(fsm, typeof(*lcp), fsm); + struct lcp_hdr_t hdr = { + .proto = htons(PPP_LCP), + .code = TERMACK, + .id = lcp->fsm.recv_id, + .len = htons(4), + }; + + if (conf_ppp_verbose) + log_ppp_info2("send [LCP TermAck id=%i]\n", hdr.id); + + ppp_chan_send(lcp->ppp, &hdr, 6); +} + +void lcp_send_proto_rej(struct ppp_t *ppp, uint16_t proto) +{ + struct ppp_lcp_t *lcp = container_of(ppp_find_layer_data(ppp, &lcp_layer), typeof(*lcp), ld); + struct rej_msg_t + { + struct lcp_hdr_t hdr; + uint16_t proto; + } __attribute__((packed)) msg = { + .hdr.proto = htons(PPP_LCP), + .hdr.code = PROTOREJ, + .hdr.id = ++lcp->fsm.id, + .hdr.len = htons(6), + .proto = ntohs(proto), + }; + + if (conf_ppp_verbose) + log_ppp_info2("send [LCP ProtoRej id=%i <%04x>]\n", msg.hdr.id, proto); + + ppp_chan_send(lcp->ppp, &msg, sizeof(msg)); +} + +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->fsm.fsm_state == FSM_Initial || lcp->fsm.fsm_state == FSM_Closed || lcp->ppp->terminating) { + /*if (conf_ppp_verbose) + log_ppp_warn("LCP: discaring packet\n"); + lcp_send_proto_rej(ccp->ppp, htons(PPP_CCP));*/ + return; + } + + if (lcp->ppp->chan_buf_size < PPP_HEADERLEN + 2) { + log_ppp_warn("LCP: short packet received\n"); + return; + } + + hdr = (struct lcp_hdr_t *)lcp->ppp->chan_buf; + if (ntohs(hdr->len) < PPP_HEADERLEN) { + log_ppp_warn("LCP: short packet received\n"); + return; + } + + lcp->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, TERM_USER_ERROR, 0); + break; + case CONFACK: + if (lcp_recv_conf_ack(lcp,(uint8_t*)(hdr + 1), ntohs(hdr->len) - PPP_HDRLEN)) + ppp_terminate(lcp->ppp, TERM_USER_ERROR, 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, TERM_USER_ERROR, 0); + else + if (lcp->fsm.recv_id != lcp->fsm.id) + break; + ppp_fsm_recv_conf_rej(&lcp->fsm); + break; + case TERMREQ: + if (conf_ppp_verbose) + log_ppp_info2("recv [LCP TermReq id=%x]\n", hdr->id); + ppp_fsm_recv_term_req(&lcp->fsm); + ppp_terminate(lcp->ppp, TERM_USER_REQUEST, 0); + break; + case TERMACK: + if (conf_ppp_verbose) + log_ppp_info2("recv [LCP TermAck id=%x]\n", hdr->id); + ppp_fsm_recv_term_ack(&lcp->fsm); + break; + case CODEREJ: + if (conf_ppp_verbose) + log_ppp_info2("recv [LCP CodeRej id=%x]\n", hdr->id); + ppp_fsm_recv_code_rej_bad(&lcp->fsm); + break; + case ECHOREQ: + if (conf_ppp_verbose) + log_ppp_debug("recv [LCP EchoReq id=%x <magic %x>]\n", hdr->id, *(uint32_t*)(hdr + 1)); + send_echo_reply(lcp); + break; + case ECHOREP: + lcp_recv_echo_repl(lcp, (uint8_t*)(hdr + 1), ntohs(hdr->len) - PPP_HDRLEN); + break; + case PROTOREJ: + if (conf_ppp_verbose) + log_ppp_info2("recv [LCP ProtoRej id=%x <%04x>]\n", hdr->code, hdr->id, ntohs(*(uint16_t*)(hdr + 1))); + ppp_recv_proto_rej(lcp->ppp, ntohs(*(uint16_t *)(hdr + 1))); + break; + case IDENT: + if (conf_ppp_verbose) { + term_msg = _strndup((char*)(hdr + 1) + 4, ntohs(hdr->len) - 4 - 4); + log_ppp_info2("recv [LCP Ident id=%x <%s>]\n", hdr->id, term_msg); + _free(term_msg); + } + break; + default: + if (conf_ppp_verbose) + log_ppp_info2("recv [LCP Unknown %x]\n", hdr->code); + 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 load_config(void) +{ + char *opt; + + 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); +} + +static void __init lcp_init(void) +{ + load_config(); + + ppp_register_layer("lcp", &lcp_layer); + + triton_event_register_handler(EV_CONFIG_RELOAD, (triton_event_func)load_config); +} + diff --git a/accel-pppd/ppp/ppp_lcp.h b/accel-pppd/ppp/ppp_lcp.h new file mode 100644 index 00000000..6d67b698 --- /dev/null +++ b/accel-pppd/ppp/ppp_lcp.h @@ -0,0 +1,136 @@ +#ifndef PPP_LCP_H +#define PPP_LCP_H + +#include <stdint.h> + +#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; + int print:1; + 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 started:1; +}; + +int lcp_option_register(struct lcp_option_handler_t *h); + +#endif + diff --git a/accel-pppd/ppp/ppp_notify.c b/accel-pppd/ppp/ppp_notify.c new file mode 100644 index 00000000..ad9fd1fc --- /dev/null +++ b/accel-pppd/ppp/ppp_notify.c @@ -0,0 +1,54 @@ +#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_starting(struct ppp_t *ppp) +{ + struct ppp_notified_t *n; + + list_for_each_entry(n, ¬ified_list, entry) { + if (n->starting) + n->starting(n, ppp); + } +} + +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); + } +} + +void ppp_notify_finishing(struct ppp_t *ppp) +{ + struct ppp_notified_t *n; + + list_for_each_entry(n, ¬ified_list, entry) { + if (n->finishing) + n->finishing(n, ppp); + } +} + diff --git a/accel-pppd/ppp/ppp_pd.c b/accel-pppd/ppp/ppp_pd.c new file mode 100644 index 00000000..f7702083 --- /dev/null +++ b/accel-pppd/ppp/ppp_pd.c @@ -0,0 +1,14 @@ +#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; + + list_for_each_entry(pd, &ppp->pd_list, entry) + if (pd->key == key) + return -1; + + +} |