From c9dfb400f808374a0b2a209abb4c481243ef7c27 Mon Sep 17 00:00:00 2001 From: the Date: Sat, 11 Nov 2023 17:57:28 +0800 Subject: [PATCH] add ppp pap auth unit test --- src/dbg.c | 93 +++++++++++++++------- src/dbg.h | 5 +- src/pppd/codec.c | 154 ++++++++++++++++++------------------ src/pppd/codec.h | 8 +- src/pppd/fsm.c | 20 +++-- src/pppd/fsm.h | 1 + src/pppd/header.h | 5 +- src/pppd/pppd.c | 1 + src/pppd/pppd.h | 4 +- unit_test/pppd/codec_test.c | 130 +++++++++++++++++++++++++++++- unit_test/test.c | 2 + unit_test/test.h | 2 + 12 files changed, 295 insertions(+), 130 deletions(-) diff --git a/src/dbg.c b/src/dbg.c index d7dcfd7..039017f 100644 --- a/src/dbg.c +++ b/src/dbg.c @@ -16,9 +16,6 @@ #define DBG_VRG_MSG_LEN 256 #define LOGGER_BUF_LEN 1024 -char *PPP_state2str(U16 state); -char *DHCP_state2str(U16 state); - static VRG_t *vrg_ccb; char *loglvl2str(U8 level) @@ -55,22 +52,6 @@ U8 logstr2lvl(const char *log_str) return LOGUNKNOWN; } -void PPPLOGMSG(void *ccb, char *buf) -{ - PPP_INFO_t *s_ppp_ccb = (PPP_INFO_t *)ccb; - if (s_ppp_ccb) { - sprintf(buf, "pppd> Session id [%x.%s] ", rte_be_to_cpu_16(s_ppp_ccb->session_id), PPP_state2str(s_ppp_ccb->ppp_phase[s_ppp_ccb->cp].state)); - } -} - -void DHCPLOGMSG(void *ccb, char *buf) -{ - dhcp_ccb_t *dhcp_ccb = (dhcp_ccb_t *)ccb; - if (dhcp_ccb) { - sprintf(buf,"dhcpd> ip pool index, user index[%u, %u, %s] ", dhcp_ccb->cur_ip_pool_index, dhcp_ccb->cur_lan_user_index, DHCP_state2str(dhcp_ccb->lan_user_info[dhcp_ccb->cur_lan_user_index].state)); - } -} - /*------------------------------------------------------------------- * PPP_state2str * @@ -83,17 +64,17 @@ char *PPP_state2str(U16 state) PPP_STATE state; char str[20]; } ppp_state_desc_tbl[] = { - { S_INIT, "INIT " }, - { S_STARTING, "STARTING " }, - { S_CLOSED, "CLOSED " }, - { S_STOPPED, "STOPPED " }, - { S_CLOSING, "CLOSING " }, - { S_STOPPING, "STOPPONG " }, + { S_INIT, "INIT" }, + { S_STARTING, "STARTING" }, + { S_CLOSED, "CLOSED" }, + { S_STOPPED, "STOPPED" }, + { S_CLOSING, "CLOSING" }, + { S_STOPPING, "STOPPONG" }, { S_REQUEST_SENT, "REQUEST_SENT" }, { S_ACK_RECEIVED, "ACK_RECEIVED" }, - { S_ACK_SENT, "ACK_SENT " }, - { S_OPENED, "OPENED " }, - { S_INVLD, "Unknwn " }, + { S_ACK_SENT, "ACK_SENT" }, + { S_OPENED, "OPENED" }, + { S_INVLD, "Unknwn" }, }; U8 i; @@ -107,6 +88,46 @@ char *PPP_state2str(U16 state) return ppp_state_desc_tbl[i].str; } +/*------------------------------------------------------------------- + * PPP_event2str + * + * input : event + * return: string of corresponding event value + *------------------------------------------------------------------*/ +char *PPP_event2str(U16 event) +{ + static struct { + PPP_EVENT_TYPE event; + char str[64]; + } ppp_event_desc_tbl[] = { + { E_UP, "UP" }, + { E_DOWN, "DOWN" }, + { E_OPEN, "OPEN" }, + { E_CLOSE, "CLOSE" }, + { E_TIMEOUT_COUNTER_POSITIVE, "TIMEOUT_COUNTER_POSITIVE" }, + { E_TIMEOUT_COUNTER_EXPIRED, "TIMEOUT_COUNTER_EXPIRED" }, + { E_RECV_GOOD_CONFIG_REQUEST, "RECV_GOOD_CONFIG_REQUEST" }, + { E_RECV_BAD_CONFIG_REQUEST, "RECV_BAD_CONFIG_REQUEST" }, + { E_RECV_CONFIG_ACK, "RECV_CONFIG_ACK" }, + { E_RECV_CONFIG_NAK_REJ, "RECV_CONFIG_NAK_REJECT" }, + { E_RECV_TERMINATE_REQUEST, "RECV_TERMINATE_REQUEST" }, + { E_RECV_TERMINATE_ACK, "RECV_TERMINATE_ACK" }, + { E_RECV_UNKNOWN_CODE, "RECV_UNKNOWN_CODE" }, + { E_RECV_GOOD_CODE_PROTOCOL_REJECT, "RECV_GOOD_CODE_PROTOCOL_REJECT" }, + { E_RECV_BAD_CODE_PROTOCOL_REJECT, "RECV_BAD_CODE_PROTOCOL_REJECT" }, + { E_RECV_ECHO_REPLY_REQUEST_DISCARD_REQUEST, "RECV_ECHO_REPLY_REQUEST_DISCARD_REQUEST" }, + { E_UNKNOWN, "UNKNOWN" }, + }; + + U8 i; + + for(i=0; ppp_event_desc_tbl[i].event != E_UNKNOWN; i++) { + if (ppp_event_desc_tbl[i].event == event) break; + } + + return ppp_event_desc_tbl[i].str; +} + /*------------------------------------------------------------------- * DHCP_state2str * @@ -139,6 +160,22 @@ char *DHCP_state2str(U16 state) return dhcp_state_desc_tbl[i].str; } +void PPPLOGMSG(void *ccb, char *buf) +{ + PPP_INFO_t *s_ppp_ccb = (PPP_INFO_t *)ccb; + if (s_ppp_ccb) { + sprintf(buf, "pppd> Session id [%x] ", rte_be_to_cpu_16(s_ppp_ccb->session_id)); + } +} + +void DHCPLOGMSG(void *ccb, char *buf) +{ + dhcp_ccb_t *dhcp_ccb = (dhcp_ccb_t *)ccb; + if (dhcp_ccb) { + sprintf(buf,"dhcpd> ip pool index, user index[%u, %u, %s] ", dhcp_ccb->cur_ip_pool_index, dhcp_ccb->cur_lan_user_index, DHCP_state2str(dhcp_ccb->lan_user_info[dhcp_ccb->cur_lan_user_index].state)); + } +} + /*************************************************** * LOGGER: ***************************************************/ diff --git a/src/dbg.h b/src/dbg.h index 6d46387..849e77d 100644 --- a/src/dbg.h +++ b/src/dbg.h @@ -15,8 +15,9 @@ #define LOGERR 4U #define LOGUNKNOWN 0U -extern char *PPP_state2str(U16 state); -extern char *DHCP_state2str(U16 state); +char *PPP_state2str(U16 state); +char *PPP_event2str(U16 event); +char *DHCP_state2str(U16 state); /* log level, logfile fp, log msg */ #define VRG_LOG(lvl, fp, ccb, ccb2str, ...) LOGGER(LOG ## lvl, __FILE__, __LINE__, fp, ccb, ccb2str, __VA_ARGS__) diff --git a/src/pppd/codec.c b/src/pppd/codec.c index 53ee311..469f08d 100644 --- a/src/pppd/codec.c +++ b/src/pppd/codec.c @@ -187,8 +187,8 @@ STATUS PPP_decode_frame(tVRG_MBX *mail, struct rte_ether_hdr *eth_hdr, vlan_head else if (ppp_payload->ppp_protocol == rte_cpu_to_be_16(PAP_PROTOCOL)) { if (s_ppp_ccb->phase != AUTH_PHASE) return ERROR; - ppp_pap_ack_nak_t ppp_pap_ack_nak, *tmp_ppp_pap_ack_nak = (ppp_pap_ack_nak_t *)(tmp_ppp_hdr + 1); - rte_memcpy(&ppp_pap_ack_nak,tmp_ppp_pap_ack_nak,tmp_ppp_pap_ack_nak->msg_length + sizeof(U8)); + //ppp_pap_ack_nak_t ppp_pap_ack_nak, *tmp_ppp_pap_ack_nak = (ppp_pap_ack_nak_t *)(tmp_ppp_hdr + 1); + //rte_memcpy(&ppp_pap_ack_nak,tmp_ppp_pap_ack_nak,tmp_ppp_pap_ack_nak->msg_length + sizeof(U8)); if (ppp_hdr->code == PAP_ACK) { VRG_LOG(INFO, vrg_ccb->fp, s_ppp_ccb, PPPLOGMSG, "User %" PRIu16 " auth success.", s_ppp_ccb->user_num); s_ppp_ccb->phase = IPCP_PHASE; @@ -214,9 +214,8 @@ STATUS PPP_decode_frame(tVRG_MBX *mail, struct rte_ether_hdr *eth_hdr, vlan_head tmp_s_ppp_ccb.ppp_phase[0].ppp_options = NULL; tmp_s_ppp_ccb.cp = 0; tmp_s_ppp_ccb.session_id = s_ppp_ccb->session_id; - if (build_auth_ack_pap(buffer, &tmp_s_ppp_ccb, &mulen) < 0) - return ERROR; - + + build_auth_ack_pap(buffer, &mulen, &tmp_s_ppp_ccb); drv_xmit(vrg_ccb, buffer, mulen); VRG_LOG(INFO, vrg_ccb->fp, s_ppp_ccb, PPPLOGMSG, "User %" PRIu16 " recv pap request.\n", s_ppp_ccb->user_num); return TRUE; @@ -992,105 +991,104 @@ STATUS build_code_reject(__attribute__((unused)) unsigned char* buffer, __attrib /** * build_auth_request_pap * - * purpose: For PAP auth, send after LCP nego complete. - * input: *buffer - packet buffer, - * *s_ppp_ccb, - * *mulen - packet length - * output: TRUE/FALSE - * return: packet buffer + * @brief + * For PAP auth, send after LCP nego complete. + * @param buffer + * The buffer to be processed by the codec. + * @param s_ppp_ccb + * The ppp ccb. + * @param len + * The length of the buffer. + * @return + * void */ -STATUS build_auth_request_pap(unsigned char* buffer, PPP_INFO_t *s_ppp_ccb, U16 *mulen) +void build_auth_request_pap(unsigned char* buffer, U16 *mulen, PPP_INFO_t *s_ppp_ccb) { - ppp_header_t ppp_pap_header; - U8 peer_id_length = strlen((const char *)(s_ppp_ccb->ppp_user_id)); - U8 peer_passwd_length = strlen((const char *)(s_ppp_ccb->ppp_passwd)); - struct rte_ether_hdr *eth_hdr = s_ppp_ccb->ppp_phase[s_ppp_ccb->cp].eth_hdr; - vlan_header_t *vlan_header = s_ppp_ccb->ppp_phase[s_ppp_ccb->cp].vlan_header; - pppoe_header_t *pppoe_header = s_ppp_ccb->ppp_phase[s_ppp_ccb->cp].pppoe_header; - ppp_payload_t *ppp_payload = s_ppp_ccb->ppp_phase[s_ppp_ccb->cp].ppp_payload; - ppp_header_t *ppp_hdr = s_ppp_ccb->ppp_phase[s_ppp_ccb->cp].ppp_hdr; + struct rte_ether_hdr *eth_hdr = (struct rte_ether_hdr *)buffer; + vlan_header_t *vlan_header = (vlan_header_t *)(eth_hdr + 1); + pppoe_header_t *pppoe_header = (pppoe_header_t *)(vlan_header + 1); + ppp_payload_t *ppp_payload = (ppp_payload_t *)(pppoe_header + 1); + ppp_header_t *ppp_pap_header = (ppp_header_t *)(ppp_payload + 1); + U8 peer_id_length = strlen((const char *)(s_ppp_ccb->ppp_user_id)); + U8 peer_passwd_length = strlen((const char *)(s_ppp_ccb->ppp_passwd)); + U8 *pap_account = (U8 *)(ppp_pap_header + 1); + U8 *pap_password = pap_account + peer_id_length + sizeof(U8)/* pap account length field */; s_ppp_ccb->phase = AUTH_PHASE; rte_ether_addr_copy(&vrg_ccb->nic_info.hsi_wan_src_mac, ð_hdr->src_addr); rte_ether_addr_copy(&s_ppp_ccb->PPP_dst_mac, ð_hdr->dst_addr); + eth_hdr->ether_type = rte_cpu_to_be_16(VLAN); + + *vlan_header = *(s_ppp_ccb->ppp_phase[s_ppp_ccb->cp].vlan_header); + *pppoe_header = *(s_ppp_ccb->ppp_phase[s_ppp_ccb->cp].pppoe_header); + ppp_payload->ppp_protocol = rte_cpu_to_be_16(PAP_PROTOCOL); - ppp_payload->ppp_protocol = rte_cpu_to_be_16(PAP_PROTOCOL); - ppp_pap_header.code = PAP_REQUEST; - ppp_pap_header.identifier = ppp_hdr->identifier; + ppp_pap_header->code = PAP_REQUEST; + ppp_pap_header->identifier = s_ppp_ccb->identifier; - ppp_pap_header.length = 2 * sizeof(U8) + peer_id_length + peer_passwd_length + sizeof(ppp_header_t); - pppoe_header->length = ppp_pap_header.length + sizeof(ppp_payload_t); - ppp_pap_header.length = rte_cpu_to_be_16(ppp_pap_header.length); + *(U8 *)pap_account = peer_id_length; + rte_memcpy(pap_account + sizeof(U8), s_ppp_ccb->ppp_user_id, peer_id_length); + *(U8 *)pap_password = peer_passwd_length; + rte_memcpy(pap_password + sizeof(U8), s_ppp_ccb->ppp_passwd, peer_passwd_length); + + ppp_pap_header->length = 2 * sizeof(U8)/* for pap account length and pap password length */ + + peer_id_length + peer_passwd_length + sizeof(ppp_header_t); + pppoe_header->length = ppp_pap_header->length + sizeof(ppp_payload_t); + ppp_pap_header->length = rte_cpu_to_be_16(ppp_pap_header->length); pppoe_header->length = rte_cpu_to_be_16(pppoe_header->length); *mulen = ntohs(pppoe_header->length) + sizeof(struct rte_ether_hdr) + sizeof(pppoe_header_t) + sizeof(vlan_header_t); - - memset(buffer,0,MSG_BUF); - rte_memcpy(buffer,eth_hdr,sizeof(struct rte_ether_hdr)); - rte_memcpy(buffer+sizeof(struct rte_ether_hdr),vlan_header,sizeof(vlan_header_t)); - rte_memcpy(buffer+sizeof(struct rte_ether_hdr)+sizeof(vlan_header_t),pppoe_header,sizeof(pppoe_header_t)); - rte_memcpy(buffer+sizeof(struct rte_ether_hdr)+sizeof(vlan_header_t)+sizeof(pppoe_header_t),ppp_payload,sizeof(ppp_payload_t)); - rte_memcpy(buffer+sizeof(struct rte_ether_hdr)+sizeof(vlan_header_t)+sizeof(pppoe_header_t)+sizeof(ppp_payload_t),&ppp_pap_header,sizeof(ppp_header_t)); - rte_memcpy(buffer+sizeof(struct rte_ether_hdr)+sizeof(vlan_header_t)+sizeof(pppoe_header_t)+sizeof(ppp_payload_t)+sizeof(ppp_header_t),&peer_id_length,sizeof(U8)); - rte_memcpy(buffer+sizeof(struct rte_ether_hdr)+sizeof(vlan_header_t)+sizeof(pppoe_header_t)+sizeof(ppp_payload_t)+sizeof(ppp_header_t)+sizeof(U8),s_ppp_ccb->ppp_user_id,peer_id_length); - rte_memcpy(buffer+sizeof(struct rte_ether_hdr)+sizeof(vlan_header_t)+sizeof(pppoe_header_t)+sizeof(ppp_payload_t)+sizeof(ppp_header_t)+sizeof(U8)+peer_id_length,&peer_passwd_length,sizeof(U8)); - rte_memcpy(buffer+sizeof(struct rte_ether_hdr)+sizeof(vlan_header_t)+sizeof(pppoe_header_t)+sizeof(ppp_payload_t)+sizeof(ppp_header_t)+sizeof(U8)+peer_id_length+sizeof(U8),s_ppp_ccb->ppp_passwd,peer_passwd_length); VRG_LOG(DBG, vrg_ccb->fp, s_ppp_ccb, PPPLOGMSG, "User %" PRIu16 " pap request built.", s_ppp_ccb->user_num); - return TRUE; } /** * build_auth_ack_pap * - * purpose: For Spirent test center, in pap, we will receive pap request packet. - * input: *buffer - packet buffer, - * *s_ppp_ccb, - * *mulen - packet length - * output: TRUE/FALSE - * return: packet buffer + * @brief + * For Spirent test center, in pap, we will receive pap request packet. + * @param buffer + * The buffer to be processed by the codec. + * @param s_ppp_ccb + * The ppp ccb. + * @param len + * The length of the buffer. + * @return + * void */ -STATUS build_auth_ack_pap(unsigned char *buffer, PPP_INFO_t *s_ppp_ccb, U16 *mulen) +void build_auth_ack_pap(unsigned char *buffer, U16 *mulen, PPP_INFO_t *s_ppp_ccb) { - ppp_header_t ppp_pap_header; const char *login_msg = "Login ok"; - ppp_pap_ack_nak_t ppp_pap_ack_nak; - struct rte_ether_addr tmp_mac; - struct rte_ether_hdr *eth_hdr = s_ppp_ccb->ppp_phase[s_ppp_ccb->cp].eth_hdr; - vlan_header_t *vlan_header = s_ppp_ccb->ppp_phase[s_ppp_ccb->cp].vlan_header; - pppoe_header_t *pppoe_header = s_ppp_ccb->ppp_phase[s_ppp_ccb->cp].pppoe_header; - ppp_payload_t *ppp_payload = s_ppp_ccb->ppp_phase[s_ppp_ccb->cp].ppp_payload; - ppp_header_t *ppp_hdr = s_ppp_ccb->ppp_phase[s_ppp_ccb->cp].ppp_hdr; - - rte_ether_addr_copy(ð_hdr->src_addr, &tmp_mac); - rte_ether_addr_copy(ð_hdr->dst_addr, ð_hdr->src_addr); - rte_ether_addr_copy(&tmp_mac, ð_hdr->dst_addr); - - ppp_payload->ppp_protocol = rte_cpu_to_be_16(PAP_PROTOCOL); - ppp_pap_header.code = PAP_ACK; - ppp_pap_header.identifier = ppp_hdr->identifier; - - ppp_pap_ack_nak.msg_length = strlen(login_msg); - ppp_pap_ack_nak.msg = (U8 *)login_msg; - - ppp_pap_header.length = sizeof(ppp_header_t) + ppp_pap_ack_nak.msg_length + sizeof(ppp_pap_ack_nak.msg_length); - pppoe_header->length = ppp_pap_header.length + sizeof(ppp_payload_t); - ppp_pap_header.length = rte_cpu_to_be_16(ppp_pap_header.length); - pppoe_header->length = rte_cpu_to_be_16(pppoe_header->length); + struct rte_ether_hdr *eth_hdr = (struct rte_ether_hdr *)buffer; + vlan_header_t *vlan_header = (vlan_header_t *)(eth_hdr + 1); + pppoe_header_t *pppoe_header = (pppoe_header_t *)(vlan_header + 1); + ppp_payload_t *ppp_payload = (ppp_payload_t *)(pppoe_header + 1); + ppp_header_t *ppp_pap_header = (ppp_header_t *)(ppp_payload + 1); + ppp_pap_ack_nak_t *ppp_pap_ack_nak = (ppp_pap_ack_nak_t *)(ppp_pap_header + 1); + + rte_ether_addr_copy(&vrg_ccb->nic_info.hsi_wan_src_mac, ð_hdr->src_addr); + rte_ether_addr_copy(&s_ppp_ccb->PPP_dst_mac, ð_hdr->dst_addr); + eth_hdr->ether_type = rte_cpu_to_be_16(VLAN); - *mulen = ntohs(pppoe_header->length) + sizeof(struct rte_ether_hdr) + sizeof(pppoe_header_t) + sizeof(vlan_header_t); + *vlan_header = *(s_ppp_ccb->ppp_phase[s_ppp_ccb->cp].vlan_header); + *pppoe_header = *(s_ppp_ccb->ppp_phase[s_ppp_ccb->cp].pppoe_header); + ppp_payload->ppp_protocol = rte_cpu_to_be_16(PAP_PROTOCOL); + + ppp_pap_header->code = PAP_ACK; + ppp_pap_header->identifier = s_ppp_ccb->identifier; - memset(buffer,0,MSG_BUF); - rte_memcpy(buffer,eth_hdr,sizeof(struct rte_ether_hdr)); - rte_memcpy(buffer+14,vlan_header,sizeof(vlan_header_t)); - rte_memcpy(buffer+14+sizeof(vlan_header_t),pppoe_header,sizeof(pppoe_header_t)); - rte_memcpy(buffer+14+sizeof(vlan_header_t)+sizeof(pppoe_header_t),ppp_payload,sizeof(ppp_payload_t)); - rte_memcpy(buffer+14+sizeof(vlan_header_t)+sizeof(pppoe_header_t)+sizeof(ppp_payload_t),&ppp_pap_header,sizeof(ppp_header_t)); - rte_memcpy(buffer+14+sizeof(vlan_header_t)+sizeof(pppoe_header_t)+sizeof(ppp_payload_t)+sizeof(ppp_header_t),&ppp_pap_ack_nak,sizeof(ppp_pap_ack_nak.msg_length)+ppp_pap_ack_nak.msg_length); + ppp_pap_ack_nak->msg_length = strlen(login_msg); + rte_memcpy(ppp_pap_ack_nak->msg, login_msg, ppp_pap_ack_nak->msg_length); + + ppp_pap_header->length = sizeof(ppp_header_t) + ppp_pap_ack_nak->msg_length + sizeof(ppp_pap_ack_nak->msg_length); + pppoe_header->length = ppp_pap_header->length + sizeof(ppp_payload_t); + *mulen = pppoe_header->length + sizeof(struct rte_ether_hdr) + sizeof(pppoe_header_t) + sizeof(vlan_header_t); + + ppp_pap_header->length = rte_cpu_to_be_16(ppp_pap_header->length); + pppoe_header->length = rte_cpu_to_be_16(pppoe_header->length); VRG_LOG(DBG, vrg_ccb->fp, s_ppp_ccb, PPPLOGMSG, "User %" PRIu16 " pap ack built.", s_ppp_ccb->user_num); - return TRUE; } /* TODO: not yet tested */ diff --git a/src/pppd/codec.h b/src/pppd/codec.h index b21884b..d836bc8 100644 --- a/src/pppd/codec.h +++ b/src/pppd/codec.h @@ -24,15 +24,15 @@ void build_config_request(U8 *buffer, U16 *mulen, PPP_INFO_t *s_ppp_ccb); void build_config_ack(U8 *buffer, U16 *mulen, PPP_INFO_t *s_ppp_ccb); void build_config_nak_rej(U8 *buffer, U16 *mulen, PPP_INFO_t *s_ppp_ccb); void build_terminate_ack(U8 *buffer, U16 *mulen, PPP_INFO_t *s_ppp_ccb); -extern STATUS build_code_reject(unsigned char *buffer, PPP_INFO_t *s_ppp_ccb, U16 *mulen); +extern STATUS build_code_reject(U8 *buffer, PPP_INFO_t *s_ppp_ccb, U16 *mulen); void build_terminate_request(U8 *buffer, U16 *mulen, PPP_INFO_t *s_ppp_ccb); void build_echo_reply(U8 *buffer, U16 *mulen, PPP_INFO_t *s_ppp_ccb); -extern STATUS build_auth_request_pap(unsigned char *buffer, PPP_INFO_t *s_ppp_ccb, U16 *mulen); -extern STATUS build_auth_ack_pap(unsigned char *buffer, PPP_INFO_t *s_ppp_ccb, U16 *mulen); +void build_auth_request_pap(U8 *buffer, U16 *mulen, PPP_INFO_t *s_ppp_ccb); +void build_auth_ack_pap(U8 *buffer, U16 *mulen, PPP_INFO_t *s_ppp_ccb); +STATUS build_auth_response_chap(U8 *buffer, PPP_INFO_t *s_ppp_ccb, U16 *mulen, ppp_chap_data_t *ppp_chap_data); STATUS check_nak_reject(U8 flag, pppoe_header_t *pppoe_header, ppp_payload_t *ppp_payload, ppp_header_t *ppp_hdr, ppp_options_t *ppp_options, U16 total_lcp_length); STATUS check_ipcp_nak_rej(U8 flag, pppoe_header_t *pppoe_header, ppp_payload_t *ppp_payload, ppp_header_t *ppp_hdr, ppp_options_t *ppp_options, U16 total_lcp_length); -STATUS build_auth_response_chap(unsigned char* buffer, PPP_INFO_t *s_ppp_ccb, U16 *mulen, ppp_chap_data_t *ppp_chap_data); STATUS build_padi(U8 *buffer, U16 *mulen, PPP_INFO_t *s_ppp_ccb); STATUS build_padr(U8 *buffer, U16 *mulen, PPP_INFO_t *s_ppp_ccb); diff --git a/src/pppd/fsm.c b/src/pppd/fsm.c index b3964f9..ce4c58f 100644 --- a/src/pppd/fsm.c +++ b/src/pppd/fsm.c @@ -626,7 +626,7 @@ STATUS PPP_FSM(struct rte_timer *ppp, PPP_INFO_t *s_ppp_ccb, U16 event) { register int i,j; int retval; - char str1[30],str2[30]; + char *str1, *str2; if (!s_ppp_ccb) { VRG_LOG(ERR, vrg_ccb->fp, (U8 *)s_ppp_ccb, PPPLOGMSG, "Error! No port found for the event(%d)",event); @@ -638,11 +638,11 @@ STATUS PPP_FSM(struct rte_timer *ppp, PPP_INFO_t *s_ppp_ccb, U16 event) if (ppp_fsm_tbl[s_ppp_ccb->cp][i].state == s_ppp_ccb->ppp_phase[s_ppp_ccb->cp].state) break; - VRG_LOG(DBG, vrg_ccb->fp, (U8 *)s_ppp_ccb, PPPLOGMSG, "Current state is %s\n", PPP_state2str(ppp_fsm_tbl[s_ppp_ccb->cp][i].state)); + VRG_LOG(DBG, vrg_ccb->fp, (U8 *)s_ppp_ccb, PPPLOGMSG, "Current state is %s, event is %s\n", PPP_state2str(ppp_fsm_tbl[s_ppp_ccb->cp][i].state), PPP_event2str(event)); if (ppp_fsm_tbl[s_ppp_ccb->cp][i].state == S_INVLD) { - VRG_LOG(ERR, vrg_ccb->fp, (U8 *)s_ppp_ccb, PPPLOGMSG, "Error! user %" PRIu16 " unknown state(%d) specified for the event(%d)", - s_ppp_ccb->user_num, s_ppp_ccb->ppp_phase[s_ppp_ccb->cp].state,event); + VRG_LOG(ERR, vrg_ccb->fp, (U8 *)s_ppp_ccb, PPPLOGMSG, "Error! user %" PRIu16 " unknown state(%d) specified for the event(%d), current ppp fsm state is %s", + s_ppp_ccb->user_num, s_ppp_ccb->ppp_phase[s_ppp_ccb->cp].state,event, PPP_state2str(ppp_fsm_tbl[s_ppp_ccb->cp][i].state)); return FALSE; } @@ -662,8 +662,8 @@ STATUS PPP_FSM(struct rte_timer *ppp, PPP_INFO_t *s_ppp_ccb, U16 event) /* Correct state found */ if (s_ppp_ccb->ppp_phase[s_ppp_ccb->cp].state != ppp_fsm_tbl[s_ppp_ccb->cp][i].next_state) { - strcpy(str1,PPP_state2str(s_ppp_ccb->ppp_phase[s_ppp_ccb->cp].state)); - strcpy(str2,PPP_state2str(ppp_fsm_tbl[s_ppp_ccb->cp][i].next_state)); + str1 = PPP_state2str(s_ppp_ccb->ppp_phase[s_ppp_ccb->cp].state); + str2 = PPP_state2str(ppp_fsm_tbl[s_ppp_ccb->cp][i].next_state); VRG_LOG(DBG, vrg_ccb->fp, (U8 *)s_ppp_ccb, PPPLOGMSG,"User %" PRIu16 " %s state changed from %s to %s.", s_ppp_ccb->user_num, (s_ppp_ccb->cp == 1 ? "IPCP" : "LCP"), str1, str2); s_ppp_ccb->ppp_phase[s_ppp_ccb->cp].state = ppp_fsm_tbl[s_ppp_ccb->cp][i].next_state; } @@ -700,11 +700,9 @@ STATUS A_this_layer_up(__attribute__((unused)) struct rte_timer *tim, __attribut if (s_ppp_ccb->ppp_phase[s_ppp_ccb->cp].ppp_payload->ppp_protocol == rte_cpu_to_be_16(LCP_PROTOCOL)) { memset(buffer,0,MSG_BUF); rte_timer_reset(&(s_ppp_ccb->ppp_alive), ppp_interval*rte_get_timer_hz(), SINGLE, lcore.timer_thread, (rte_timer_cb_t)exit_ppp, s_ppp_ccb); - if (s_ppp_ccb->auth_method == PAP_PROTOCOL) { - if (build_auth_request_pap(buffer,s_ppp_ccb,&mulen) < 0) - return FALSE; - } - drv_xmit(vrg_ccb, buffer, mulen); + if (s_ppp_ccb->auth_method == PAP_PROTOCOL) + build_auth_request_pap(buffer, &mulen, s_ppp_ccb); + drv_xmit(vrg_ccb, buffer, mulen); VRG_LOG(INFO, vrg_ccb->fp, s_ppp_ccb, PPPLOGMSG, "User %" PRIu16 " LCP connection establish successfully.", s_ppp_ccb->user_num); VRG_LOG(INFO, vrg_ccb->fp, s_ppp_ccb, PPPLOGMSG, "User %" PRIu16 " starting Authentication.", s_ppp_ccb->user_num); } diff --git a/src/pppd/fsm.h b/src/pppd/fsm.h index d90ec4d..a047552 100644 --- a/src/pppd/fsm.h +++ b/src/pppd/fsm.h @@ -53,6 +53,7 @@ typedef enum { E_RECV_GOOD_CODE_PROTOCOL_REJECT, E_RECV_BAD_CODE_PROTOCOL_REJECT, E_RECV_ECHO_REPLY_REQUEST_DISCARD_REQUEST, + E_UNKNOWN, } PPP_EVENT_TYPE; typedef enum { diff --git a/src/pppd/header.h b/src/pppd/header.h index dc3cc78..71ad50a 100644 --- a/src/pppd/header.h +++ b/src/pppd/header.h @@ -98,7 +98,7 @@ typedef struct ppp_header { typedef struct ppp_pap_ack_nak { U8 msg_length; - U8 *msg; + U8 msg[0]; }ppp_pap_ack_nak_t; typedef struct ppp_chap_data { @@ -128,7 +128,8 @@ typedef struct pppoe_phase { }pppoe_phase_t; typedef struct ppp_phase { - U8 state; + U16 state; + U16 event; struct rte_ether_hdr *eth_hdr; vlan_header_t *vlan_header; pppoe_header_t *pppoe_header; diff --git a/src/pppd/pppd.c b/src/pppd/pppd.c index b14198f..9be0cc1 100644 --- a/src/pppd/pppd.c +++ b/src/pppd/pppd.c @@ -340,6 +340,7 @@ STATUS ppp_process(void *mail) } cp = (ppp_payload.ppp_protocol == rte_cpu_to_be_16(IPCP_PROTOCOL)) ? 1 : 0; ppp_ccb[session_index].cp = cp; + ppp_ccb[session_index].ppp_phase[cp].event = event; PPP_FSM(&(ppp_ccb[session_index].ppp), &ppp_ccb[session_index], event); return TRUE; diff --git a/src/pppd/pppd.h b/src/pppd/pppd.h index f907924..b85b2eb 100644 --- a/src/pppd/pppd.h +++ b/src/pppd/pppd.h @@ -50,8 +50,8 @@ typedef struct { U32 magic_num; /* ppp pkt magic number, in network order */ BOOL is_pap_auth; /* pap auth boolean flag */ U16 auth_method; /* use chap or pap */ - unsigned char *ppp_user_id; /* pap/chap account */ - unsigned char *ppp_passwd; /* pap/chap password */ + U8 *ppp_user_id; /* pap/chap account */ + U8 *ppp_passwd; /* pap/chap password */ rte_atomic16_t ppp_bool; /* boolean flag for accept ppp packets at data plane */ rte_atomic16_t dp_start_bool; /* hsi data plane starting boolean flag */ BOOL ppp_processing; /* boolean flag for checking ppp is disconnecting */ diff --git a/unit_test/pppd/codec_test.c b/unit_test/pppd/codec_test.c index e03bfaa..be90bb9 100644 --- a/unit_test/pppd/codec_test.c +++ b/unit_test/pppd/codec_test.c @@ -496,7 +496,8 @@ void test_build_config_nak_rej() assert(memcmp(buffer, pkt_ipcp_2, sizeof(pkt_ipcp_2)) == 0); } -void test_build_terminate_ack() { +void test_build_terminate_ack() +{ U8 buffer[80]; U16 mulen = 0; @@ -591,7 +592,8 @@ void test_build_terminate_ack() { assert(memcmp(buffer, pkt_ipcp, sizeof(pkt_ipcp)) == 0); } -void test_build_echo_reply() { +void test_build_echo_reply() +{ U8 buffer[80]; U16 mulen = 0; @@ -639,7 +641,6 @@ void test_build_echo_reply() { 0x11, 0x00, 0x00, 0x0a, 0x00, 0x0a, /* ppp protocol */0xc0, 0x21, /* ppp hdr*/ 0x0a, 0x01, 0x00, 0x08, /* magic number */0x01, 0x02, 0x03, 0x04}; - /* test LCP */ build_echo_reply(buffer, &mulen, &s_ppp_ccb_1); assert(mulen == sizeof(pkt_lcp_1)); assert(memcmp(buffer, pkt_lcp_1, sizeof(pkt_lcp_1)) == 0); @@ -655,3 +656,126 @@ void test_build_echo_reply() { assert(mulen == sizeof(pkt_lcp_2)); assert(memcmp(buffer, pkt_lcp_2, sizeof(pkt_lcp_2)) == 0); } + +void test_build_auth_request_pap() +{ + U8 buffer[80]; + U16 mulen = 0; + + PPP_INFO_t s_ppp_ccb_1 = { + .ppp_phase = {{ + .timer_counter = 0, + .max_retransmit = 10, + .eth_hdr = &(struct rte_ether_hdr) { + .ether_type = htons(VLAN), + }, + .vlan_header = &(vlan_header_t) { + .tci_union.tci_value = htons(0002), + .next_proto = htons(ETH_P_PPP_SES), + }, + .pppoe_header = &(pppoe_header_t) { + .code = 0, + .ver_type = 0x11, + .session_id = htons(0x000a), + .length = htons(0x0000), + }, + .ppp_payload = &(ppp_payload_t) { + .ppp_protocol = htons(LCP_PROTOCOL), + }, + .ppp_hdr = &(ppp_header_t) { + .code = CONFIG_REQUEST, + .identifier = 0x01, + .length = htons(0x0000), + }, + },{},}, + .user_num = 1, + .vlan = 2, + .PPP_dst_mac = (struct rte_ether_addr){ + .addr_bytes = {0x74, 0x4d, 0x28, 0x8d, 0x00, 0x31}, + }, + .session_id = htons(0x000a), + .cp = 0, + .magic_num = htonl(0x01020304), + .ppp_user_id = (U8 *)"asdf", // 0x61, 0x73, 0x64, 0x66 + .ppp_passwd = (U8 *)"zxcv", // 0x7a, 0x78, 0x63, 0x76 + .identifier = 0xfe, + }; + + char pkt_lcp_1[] = {/* mac */0x74, 0x4d, 0x28, 0x8d, 0x00, 0x31, 0x9c, 0x69, 0xb4, + 0x61, 0x16, 0xdd, 0x81, 0x00, /* vlan */0x00, 0x02, 0x88, 0x64, /* pppoe hdr */ + 0x11, 0x00, 0x00, 0x0a, 0x00, 0x10, /* ppp protocol */0xc0, 0x23, /* ppp hdr*/ + 0x01, 0xfe, 0x00, 0x0e, /* pap user */0x04, 0x61, 0x73, 0x64, 0x66, /* pap passwd */ + 0x04, 0x7a, 0x78, 0x63, 0x76}; + + build_auth_request_pap(buffer, &mulen, &s_ppp_ccb_1); + assert(mulen == sizeof(pkt_lcp_1)); + assert(memcmp(buffer, pkt_lcp_1, sizeof(pkt_lcp_1)) == 0); + + char pkt_lcp_2[] = {/* mac */0x74, 0x4d, 0x28, 0x8d, 0x00, 0x31, 0x9c, 0x69, 0xb4, + 0x61, 0x16, 0xdd, 0x81, 0x00, /* vlan */0x00, 0x02, 0x88, 0x64, /* pppoe hdr */ + 0x11, 0x00, 0x00, 0x0a, 0x00, 0x20, /* ppp protocol */0xc0, 0x23, /* ppp hdr*/ + 0x01, 0xfe, 0x00, 0x1e, /* pap user */0x08, 0x31, 0x71, 0x61, 0x7a, 0x32, 0x77, + 0x73, 0x78, /* pap passwd */0x10, 0x33, 0x65, 0x64, 0x63, 0x34, 0x72, 0x66, 0x76, + 0x35, 0x74, 0x67, 0x62, 0x36, 0x79, 0x68, 0x6e}; + s_ppp_ccb_1.ppp_user_id = (U8 *)"1qaz2wsx"; // 0x31, 0x71, 0x61, 0x7a, 0x32, 0x77, 0x73, 0x78 + s_ppp_ccb_1.ppp_passwd = (U8 *)"3edc4rfv5tgb6yhn"; // 0x33, 0x65, 0x64, 0x63, 0x34, 0x72, 0x66, 0x76, 0x35, 0x74, 0x67, 0x62, 0x36, 0x79, 0x68, 0x6e + + build_auth_request_pap(buffer, &mulen, &s_ppp_ccb_1); + assert(mulen == sizeof(pkt_lcp_2)); + assert(memcmp(buffer, pkt_lcp_2, sizeof(pkt_lcp_2)) == 0); +} + +void test_build_auth_ack_pap() +{ + U8 buffer[80]; + U16 mulen = 0; + + PPP_INFO_t s_ppp_ccb_1 = { + .ppp_phase = {{ + .timer_counter = 0, + .max_retransmit = 10, + .eth_hdr = &(struct rte_ether_hdr) { + .ether_type = htons(VLAN), + }, + .vlan_header = &(vlan_header_t) { + .tci_union.tci_value = htons(0002), + .next_proto = htons(ETH_P_PPP_SES), + }, + .pppoe_header = &(pppoe_header_t) { + .code = 0, + .ver_type = 0x11, + .session_id = htons(0x000a), + .length = htons(0x0000), + }, + .ppp_payload = &(ppp_payload_t) { + .ppp_protocol = htons(LCP_PROTOCOL), + }, + .ppp_hdr = &(ppp_header_t) { + .code = CONFIG_REQUEST, + .identifier = 0x01, + .length = htons(0x0000), + }, + },{},}, + .user_num = 1, + .vlan = 2, + .PPP_dst_mac = (struct rte_ether_addr){ + .addr_bytes = {0x74, 0x4d, 0x28, 0x8d, 0x00, 0x31}, + }, + .session_id = htons(0x000a), + .cp = 0, + .magic_num = htonl(0x01020304), + .ppp_user_id = (U8 *)"asdf", // 0x61, 0x73, 0x64, 0x66 + .ppp_passwd = (U8 *)"zxcv", // 0x7a, 0x78, 0x63, 0x76 + .identifier = 0xfe, + }; + + char pkt_lcp_1[] = {/* mac */0x74, 0x4d, 0x28, 0x8d, 0x00, 0x31, 0x9c, 0x69, 0xb4, + 0x61, 0x16, 0xdd, 0x81, 0x00, /* vlan */0x00, 0x02, 0x88, 0x64, /* pppoe hdr */ + 0x11, 0x00, 0x00, 0x0a, 0x00, 0x0f, /* ppp protocol */0xc0, 0x23, /* ppp hdr*/ + 0x02, 0xfe, 0x00, 0x0d, /* Login ok */0x08, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x20, + 0x6f, 0x6b}; + + build_auth_ack_pap(buffer, &mulen, &s_ppp_ccb_1); + assert(mulen == sizeof(pkt_lcp_1)); + assert(memcmp(buffer, pkt_lcp_1, sizeof(pkt_lcp_1)) == 0); +} diff --git a/unit_test/test.c b/unit_test/test.c index b94620c..8b05a71 100644 --- a/unit_test/test.c +++ b/unit_test/test.c @@ -39,6 +39,8 @@ int main() test_build_terminate_request(); test_build_terminate_ack(); test_build_echo_reply(); + test_build_auth_request_pap(); + test_build_auth_ack_pap(); puts("ok!"); puts("\nall test successfully"); diff --git a/unit_test/test.h b/unit_test/test.h index 7e52610..a1362c2 100644 --- a/unit_test/test.h +++ b/unit_test/test.h @@ -10,5 +10,7 @@ void test_build_terminate_request(); void test_build_config_nak_rej(); void test_build_terminate_ack(); void test_build_echo_reply(); +void test_build_auth_request_pap(); +void test_build_auth_ack_pap(); #endif \ No newline at end of file