PPPoE application with W5500
Dependencies: W5500Interface mbed pppoe
- How to connect PPPoE with WIZ550 ioShield and mbed platform (Korean version)
- How to connect ioShield to mbed platform(ST nucleo) of ST Microelectronics via SPI (Korean version)
- Example PPPoE server(RB750) setting (Korean version)
http://hjjeon0608.wordpress.com/2014/10/28/rb750pppoe-server-setting%ED%95%98%EA%B8%B0/
- W5500(PPPoE client) setting (Korean version)
http://hjjeon0608.wordpress.com/2014/10/29/temp/
- PPPoE library
http://developer.mbed.org/teams/EthernetInterfaceW5500-makers/code/pppoe/
pppoe/PPPoE.cpp@1:8ef7820bf777, 2014-10-15 (annotated)
- Committer:
- hjjeon
- Date:
- Wed Oct 15 06:27:12 2014 +0000
- Revision:
- 1:8ef7820bf777
- Child:
- 3:39ed2b6d91b0
PPPoE application with W5500
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hjjeon | 1:8ef7820bf777 | 1 | #include <stdint.h> |
hjjeon | 1:8ef7820bf777 | 2 | #include "PPPoE.h" |
hjjeon | 1:8ef7820bf777 | 3 | |
hjjeon | 1:8ef7820bf777 | 4 | |
hjjeon | 1:8ef7820bf777 | 5 | // ID and Password for PAP from main.c |
hjjeon | 1:8ef7820bf777 | 6 | extern uint8_t pppoe_id[];//={}; |
hjjeon | 1:8ef7820bf777 | 7 | extern uint8_t pppoe_id_len; |
hjjeon | 1:8ef7820bf777 | 8 | extern uint8_t pppoe_ip[];//={}; |
hjjeon | 1:8ef7820bf777 | 9 | extern uint8_t pppoe_pdns[];//={}; |
hjjeon | 1:8ef7820bf777 | 10 | extern uint8_t pppoe_sdns[];//={}; |
hjjeon | 1:8ef7820bf777 | 11 | extern uint8_t pppoe_pw[];//={}; |
hjjeon | 1:8ef7820bf777 | 12 | extern uint8_t pppoe_pw_len; |
hjjeon | 1:8ef7820bf777 | 13 | |
hjjeon | 1:8ef7820bf777 | 14 | |
hjjeon | 1:8ef7820bf777 | 15 | //IPCP Reject flag |
hjjeon | 1:8ef7820bf777 | 16 | uint8_t ip_rjt; |
hjjeon | 1:8ef7820bf777 | 17 | uint8_t pdns_rjt; |
hjjeon | 1:8ef7820bf777 | 18 | uint8_t sdns_rjt; |
hjjeon | 1:8ef7820bf777 | 19 | |
hjjeon | 1:8ef7820bf777 | 20 | |
hjjeon | 1:8ef7820bf777 | 21 | // PPPoE Frame structure for send |
hjjeon | 1:8ef7820bf777 | 22 | PPPMSG PPPMSG_req; // PPPoE frame |
hjjeon | 1:8ef7820bf777 | 23 | PROTOCOL PPPPROTO; // Tag and Protocol data |
hjjeon | 1:8ef7820bf777 | 24 | |
hjjeon | 1:8ef7820bf777 | 25 | // TxRx Buffers pointer from main |
hjjeon | 1:8ef7820bf777 | 26 | uint8_t* buf; |
hjjeon | 1:8ef7820bf777 | 27 | |
hjjeon | 1:8ef7820bf777 | 28 | // Server MAC and Assigned Session ID and IP address from NAS using PPPoE |
hjjeon | 1:8ef7820bf777 | 29 | uint8_t NAS_mac[6]; |
hjjeon | 1:8ef7820bf777 | 30 | uint16_t NAS_sessionid = 0; |
hjjeon | 1:8ef7820bf777 | 31 | |
hjjeon | 1:8ef7820bf777 | 32 | // kind of authentication protocol and algorithm; decided by LCP phase |
hjjeon | 1:8ef7820bf777 | 33 | // Authentication protocol : PAP - 0xC023, CHAP - 0xC223 |
hjjeon | 1:8ef7820bf777 | 34 | // Algorithm : MD5 - 0x05, MS-CHAP - 0x80, MS-CHAP-V2 - 0x81 |
hjjeon | 1:8ef7820bf777 | 35 | uint16_t auth_protocol; |
hjjeon | 1:8ef7820bf777 | 36 | uint8_t chap_algorithm; |
hjjeon | 1:8ef7820bf777 | 37 | |
hjjeon | 1:8ef7820bf777 | 38 | // For MD5 calculation |
hjjeon | 1:8ef7820bf777 | 39 | MD5_CTX context; |
hjjeon | 1:8ef7820bf777 | 40 | uint8_t digest[16]; |
hjjeon | 1:8ef7820bf777 | 41 | |
hjjeon | 1:8ef7820bf777 | 42 | // Identifier for PPPoE Protocols (increase per message sending) |
hjjeon | 1:8ef7820bf777 | 43 | uint8_t protocol_id = 0x01; |
hjjeon | 1:8ef7820bf777 | 44 | |
hjjeon | 1:8ef7820bf777 | 45 | |
hjjeon | 1:8ef7820bf777 | 46 | |
hjjeon | 1:8ef7820bf777 | 47 | uint16_t pppoe_state = PPPoE_DISCOVERY; |
hjjeon | 1:8ef7820bf777 | 48 | // PPPoE stage control flags |
hjjeon | 1:8ef7820bf777 | 49 | uint16_t pppoe_control_flag = 0; |
hjjeon | 1:8ef7820bf777 | 50 | |
hjjeon | 1:8ef7820bf777 | 51 | //PPPoE retry count and send retry count |
hjjeon | 1:8ef7820bf777 | 52 | uint8_t pppoe_retry_send_count = 0; |
hjjeon | 1:8ef7820bf777 | 53 | extern uint16_t pppoe_retry_count; |
hjjeon | 1:8ef7820bf777 | 54 | uint8_t pppoe_recv_count = 0; |
hjjeon | 1:8ef7820bf777 | 55 | |
hjjeon | 1:8ef7820bf777 | 56 | |
hjjeon | 1:8ef7820bf777 | 57 | // Tmp variable |
hjjeon | 1:8ef7820bf777 | 58 | uint16_t tmp_protocol; |
hjjeon | 1:8ef7820bf777 | 59 | uint8_t tmp_pcode; |
hjjeon | 1:8ef7820bf777 | 60 | |
hjjeon | 1:8ef7820bf777 | 61 | PPPOEClient::PPPOEClient() |
hjjeon | 1:8ef7820bf777 | 62 | { |
hjjeon | 1:8ef7820bf777 | 63 | eth = WIZnet_Chip::getInstance(); |
hjjeon | 1:8ef7820bf777 | 64 | } |
hjjeon | 1:8ef7820bf777 | 65 | |
hjjeon | 1:8ef7820bf777 | 66 | |
hjjeon | 1:8ef7820bf777 | 67 | void PPPOEClient::set_pppinfo(uint8_t * nas_mac, uint8_t * ppp_ip, uint16_t nas_sessionid) |
hjjeon | 1:8ef7820bf777 | 68 | { |
hjjeon | 1:8ef7820bf777 | 69 | |
hjjeon | 1:8ef7820bf777 | 70 | #ifdef __DEF_PPP_DBG1__ |
hjjeon | 1:8ef7820bf777 | 71 | uint8_t str[8];//debug var |
hjjeon | 1:8ef7820bf777 | 72 | uint16_t psid; |
hjjeon | 1:8ef7820bf777 | 73 | |
hjjeon | 1:8ef7820bf777 | 74 | printf("set_pppinfo() Start...\r\n"); |
hjjeon | 1:8ef7820bf777 | 75 | #endif |
hjjeon | 1:8ef7820bf777 | 76 | /* Set PPPoE bit in MR(Common Mode Register) : enable PPPoE */ |
hjjeon | 1:8ef7820bf777 | 77 | eth->setMR(eth->getMR() | MR_PPPOE); |
hjjeon | 1:8ef7820bf777 | 78 | |
hjjeon | 1:8ef7820bf777 | 79 | // Write PPPoE server's MAC address, Session ID and IP address. |
hjjeon | 1:8ef7820bf777 | 80 | // must be setted these value. |
hjjeon | 1:8ef7820bf777 | 81 | #ifdef __DEF_PPP_DBG1__ |
hjjeon | 1:8ef7820bf777 | 82 | printf("Server's MAC : %.2x:%.2x:%.2x:%.2x:%.2x:%.2x\r\n", nas_mac[0], nas_mac[1], nas_mac[2], nas_mac[3], nas_mac[4], nas_mac[5]); |
hjjeon | 1:8ef7820bf777 | 83 | printf("PPPoE IP : %.3d.%.3d.%.3d.%.3d\r\n", ppp_ip[0], ppp_ip[1], ppp_ip[2], ppp_ip[3]); |
hjjeon | 1:8ef7820bf777 | 84 | printf("Session ID : 0x%.2x%.2x\r\n", (uint8_t)(nas_sessionid >> 8), (uint8_t)nas_sessionid); |
hjjeon | 1:8ef7820bf777 | 85 | #endif |
hjjeon | 1:8ef7820bf777 | 86 | |
hjjeon | 1:8ef7820bf777 | 87 | eth->setPHAR(nas_mac); |
hjjeon | 1:8ef7820bf777 | 88 | eth->setSIPR(ppp_ip); |
hjjeon | 1:8ef7820bf777 | 89 | eth->setPSID(nas_sessionid); |
hjjeon | 1:8ef7820bf777 | 90 | |
hjjeon | 1:8ef7820bf777 | 91 | #ifdef __DEF_PPP_DBG1__ |
hjjeon | 1:8ef7820bf777 | 92 | eth->getPHAR(str); |
hjjeon | 1:8ef7820bf777 | 93 | printf( "Read PHAR register : %.2x:%.2x:%.2x:%.2x:%.2x:%.2x\r\n", str[0], str[1], str[2], str[3], str[4], str[5]); |
hjjeon | 1:8ef7820bf777 | 94 | eth->getSIPR(str); |
hjjeon | 1:8ef7820bf777 | 95 | printf( "Read SIP register : %.3d.%.3d.%.3d.%.3d\r\n", str[0], str[1], str[2], str[3]); |
hjjeon | 1:8ef7820bf777 | 96 | psid = eth->getPSID(); |
hjjeon | 1:8ef7820bf777 | 97 | printf("Read PSID register : %x\r\n", psid); |
hjjeon | 1:8ef7820bf777 | 98 | #endif |
hjjeon | 1:8ef7820bf777 | 99 | |
hjjeon | 1:8ef7820bf777 | 100 | //open socket in pppoe mode |
hjjeon | 1:8ef7820bf777 | 101 | eth->setPTIMER(0); |
hjjeon | 1:8ef7820bf777 | 102 | |
hjjeon | 1:8ef7820bf777 | 103 | |
hjjeon | 1:8ef7820bf777 | 104 | #ifdef __DEF_PPP_DBG1__ |
hjjeon | 1:8ef7820bf777 | 105 | printf("set_pppinfo() End...\r\n"); |
hjjeon | 1:8ef7820bf777 | 106 | #endif |
hjjeon | 1:8ef7820bf777 | 107 | #ifdef __DEF_PPP_DBG1__ |
hjjeon | 1:8ef7820bf777 | 108 | printf("pppoe PDNS : %.3d.%.3d.%.3d.%.3d\r\n", pppoe_pdns[0], pppoe_pdns[1], pppoe_pdns[2], pppoe_pdns[3]); |
hjjeon | 1:8ef7820bf777 | 109 | printf("pppoe SDNS : %.3d.%.3d.%.3d.%.3d\r\n", pppoe_sdns[0], pppoe_sdns[1], pppoe_sdns[2], pppoe_sdns[3]); |
hjjeon | 1:8ef7820bf777 | 110 | #endif |
hjjeon | 1:8ef7820bf777 | 111 | |
hjjeon | 1:8ef7820bf777 | 112 | } |
hjjeon | 1:8ef7820bf777 | 113 | |
hjjeon | 1:8ef7820bf777 | 114 | |
hjjeon | 1:8ef7820bf777 | 115 | void PPPOEClient::ppp_send(void) |
hjjeon | 1:8ef7820bf777 | 116 | { |
hjjeon | 1:8ef7820bf777 | 117 | uint8_t *ptr; |
hjjeon | 1:8ef7820bf777 | 118 | uint8_t sn = 0; |
hjjeon | 1:8ef7820bf777 | 119 | uint16_t tmp16 = 0; |
hjjeon | 1:8ef7820bf777 | 120 | uint16_t txbuf_len = 0; |
hjjeon | 1:8ef7820bf777 | 121 | |
hjjeon | 1:8ef7820bf777 | 122 | txbuf_len = sizeof(PPPMSG_req); |
hjjeon | 1:8ef7820bf777 | 123 | |
hjjeon | 1:8ef7820bf777 | 124 | |
hjjeon | 1:8ef7820bf777 | 125 | |
hjjeon | 1:8ef7820bf777 | 126 | if(pppoe_state == PPPoE_DISCOVERY) |
hjjeon | 1:8ef7820bf777 | 127 | ptr = (uint8_t *)&PPPPROTO.opt; |
hjjeon | 1:8ef7820bf777 | 128 | else |
hjjeon | 1:8ef7820bf777 | 129 | ptr = (uint8_t *)&PPPPROTO; |
hjjeon | 1:8ef7820bf777 | 130 | |
hjjeon | 1:8ef7820bf777 | 131 | |
hjjeon | 1:8ef7820bf777 | 132 | // Fill the Tx buffer |
hjjeon | 1:8ef7820bf777 | 133 | //memcpy(txbuf, (uint8_t *)&PPPMSG_req, txbuf_len); |
hjjeon | 1:8ef7820bf777 | 134 | memcpy(buf, (uint8_t *)&PPPMSG_req, txbuf_len); |
hjjeon | 1:8ef7820bf777 | 135 | //change MSB and LSB because of different endian. |
hjjeon | 1:8ef7820bf777 | 136 | tmp16 = 0; |
hjjeon | 1:8ef7820bf777 | 137 | tmp16 = (PPPMSG_req.len & 0xFF) << 8; |
hjjeon | 1:8ef7820bf777 | 138 | tmp16 |= ((PPPMSG_req.len >> 8) & 0xFF); |
hjjeon | 1:8ef7820bf777 | 139 | |
hjjeon | 1:8ef7820bf777 | 140 | //memcpy(txbuf + txbuf_len, ptr, tmp16); |
hjjeon | 1:8ef7820bf777 | 141 | memcpy(buf + txbuf_len, ptr, tmp16); |
hjjeon | 1:8ef7820bf777 | 142 | txbuf_len += tmp16; |
hjjeon | 1:8ef7820bf777 | 143 | |
hjjeon | 1:8ef7820bf777 | 144 | #ifdef __DEF_PPP_DBG2__ |
hjjeon | 1:8ef7820bf777 | 145 | printf("Send data : "); |
hjjeon | 1:8ef7820bf777 | 146 | for(i=0; i<txbuf_len; i++) |
hjjeon | 1:8ef7820bf777 | 147 | { |
hjjeon | 1:8ef7820bf777 | 148 | if((i % 16) == 0) printf("\r\n"); |
hjjeon | 1:8ef7820bf777 | 149 | printf("%.2x ", buf[i]); |
hjjeon | 1:8ef7820bf777 | 150 | } |
hjjeon | 1:8ef7820bf777 | 151 | printf("\r\n\r\n"); |
hjjeon | 1:8ef7820bf777 | 152 | #endif |
hjjeon | 1:8ef7820bf777 | 153 | // Send MACRAW data |
hjjeon | 1:8ef7820bf777 | 154 | eth->send(sn, (char*)buf, txbuf_len); |
hjjeon | 1:8ef7820bf777 | 155 | //setSn_CR(sn, Sn_CR_SEND); |
hjjeon | 1:8ef7820bf777 | 156 | eth->setSn_CR(sn, Sn_CR_SEND); |
hjjeon | 1:8ef7820bf777 | 157 | while( eth->getSn_CR(sn) ) ; |
hjjeon | 1:8ef7820bf777 | 158 | } |
hjjeon | 1:8ef7820bf777 | 159 | |
hjjeon | 1:8ef7820bf777 | 160 | |
hjjeon | 1:8ef7820bf777 | 161 | void PPPOEClient::ppp_recv( uint16_t received_len ) |
hjjeon | 1:8ef7820bf777 | 162 | { |
hjjeon | 1:8ef7820bf777 | 163 | |
hjjeon | 1:8ef7820bf777 | 164 | uint16_t i; |
hjjeon | 1:8ef7820bf777 | 165 | uint8_t sn = 0; |
hjjeon | 1:8ef7820bf777 | 166 | uint8_t head[2] ={0,}; |
hjjeon | 1:8ef7820bf777 | 167 | |
hjjeon | 1:8ef7820bf777 | 168 | uint16_t ethertype = 0; |
hjjeon | 1:8ef7820bf777 | 169 | uint8_t pppoecode = 0; |
hjjeon | 1:8ef7820bf777 | 170 | uint16_t taglen = 0; |
hjjeon | 1:8ef7820bf777 | 171 | uint16_t tagname; |
hjjeon | 1:8ef7820bf777 | 172 | |
hjjeon | 1:8ef7820bf777 | 173 | uint8_t get_protocol_id = 0; |
hjjeon | 1:8ef7820bf777 | 174 | uint16_t t_idx = 0, acknak_idx = 0, rjt_idx = 0; |
hjjeon | 1:8ef7820bf777 | 175 | uint16_t ppp_tag_len = 0, getlen = 0, opt_len = 0; |
hjjeon | 1:8ef7820bf777 | 176 | uint8_t acknak_opt[OPTMSG_LEN]; |
hjjeon | 1:8ef7820bf777 | 177 | uint8_t rjt_opt[OPTMSG_LEN]; |
hjjeon | 1:8ef7820bf777 | 178 | uint8_t opt_code; |
hjjeon | 1:8ef7820bf777 | 179 | uint8_t ppp_code; |
hjjeon | 1:8ef7820bf777 | 180 | |
hjjeon | 1:8ef7820bf777 | 181 | uint8_t str[OPTMSG_LEN]; |
hjjeon | 1:8ef7820bf777 | 182 | uint16_t str_len = 0; |
hjjeon | 1:8ef7820bf777 | 183 | uint8_t tmp8 = 0; |
hjjeon | 1:8ef7820bf777 | 184 | uint8_t mac[6]; |
hjjeon | 1:8ef7820bf777 | 185 | |
hjjeon | 1:8ef7820bf777 | 186 | //reset servicename flag |
hjjeon | 1:8ef7820bf777 | 187 | pppoe_control_flag = pppoe_control_flag & ~FLAG_PADO_SERVICENAME; |
hjjeon | 1:8ef7820bf777 | 188 | |
hjjeon | 1:8ef7820bf777 | 189 | |
hjjeon | 1:8ef7820bf777 | 190 | //getSHAR(mac); |
hjjeon | 1:8ef7820bf777 | 191 | eth->getSHAR(mac); |
hjjeon | 1:8ef7820bf777 | 192 | |
hjjeon | 1:8ef7820bf777 | 193 | // MACRAW Receive |
hjjeon | 1:8ef7820bf777 | 194 | //receive header(packet length) of macraw packet |
hjjeon | 1:8ef7820bf777 | 195 | |
hjjeon | 1:8ef7820bf777 | 196 | eth->recv(sn, (char*) head, 2); |
hjjeon | 1:8ef7820bf777 | 197 | |
hjjeon | 1:8ef7820bf777 | 198 | eth->setSn_CR(sn,Sn_CR_RECV); |
hjjeon | 1:8ef7820bf777 | 199 | |
hjjeon | 1:8ef7820bf777 | 200 | while( eth->getSn_CR(sn) ) ; |
hjjeon | 1:8ef7820bf777 | 201 | |
hjjeon | 1:8ef7820bf777 | 202 | received_len = 0; |
hjjeon | 1:8ef7820bf777 | 203 | received_len = head[0] << 8; |
hjjeon | 1:8ef7820bf777 | 204 | received_len = received_len | head[1]; |
hjjeon | 1:8ef7820bf777 | 205 | received_len = received_len - 2; |
hjjeon | 1:8ef7820bf777 | 206 | |
hjjeon | 1:8ef7820bf777 | 207 | |
hjjeon | 1:8ef7820bf777 | 208 | eth->recv(sn, (char*) buf, received_len); |
hjjeon | 1:8ef7820bf777 | 209 | |
hjjeon | 1:8ef7820bf777 | 210 | eth->setSn_CR(sn,Sn_CR_RECV); |
hjjeon | 1:8ef7820bf777 | 211 | |
hjjeon | 1:8ef7820bf777 | 212 | while( eth->getSn_CR(sn) ) ; |
hjjeon | 1:8ef7820bf777 | 213 | |
hjjeon | 1:8ef7820bf777 | 214 | |
hjjeon | 1:8ef7820bf777 | 215 | // Check the MAC in received packet |
hjjeon | 1:8ef7820bf777 | 216 | tmp8 = buf[0] - mac[0] + buf[1] - mac[1] + buf[2] - mac[2] + buf[3] - mac[3] + buf[4] - mac[4] + buf[5] - mac[5]; |
hjjeon | 1:8ef7820bf777 | 217 | |
hjjeon | 1:8ef7820bf777 | 218 | if(tmp8==0) |
hjjeon | 1:8ef7820bf777 | 219 | { |
hjjeon | 1:8ef7820bf777 | 220 | #ifdef __DEF_PPP_DBG2__ |
hjjeon | 1:8ef7820bf777 | 221 | printf("Received packet :"); |
hjjeon | 1:8ef7820bf777 | 222 | for(i = 0; i < received_len; i++) |
hjjeon | 1:8ef7820bf777 | 223 | { |
hjjeon | 1:8ef7820bf777 | 224 | if((i % 16) == 0) printf("\r\n"); |
hjjeon | 1:8ef7820bf777 | 225 | printf("%.2x ", buf[i]); |
hjjeon | 1:8ef7820bf777 | 226 | } |
hjjeon | 1:8ef7820bf777 | 227 | printf("\r\n"); |
hjjeon | 1:8ef7820bf777 | 228 | #endif |
hjjeon | 1:8ef7820bf777 | 229 | ethertype = buf[12]; |
hjjeon | 1:8ef7820bf777 | 230 | ethertype = (ethertype << 8) + buf[13]; |
hjjeon | 1:8ef7820bf777 | 231 | |
hjjeon | 1:8ef7820bf777 | 232 | pppoecode = buf[15]; |
hjjeon | 1:8ef7820bf777 | 233 | |
hjjeon | 1:8ef7820bf777 | 234 | taglen = buf[18]; |
hjjeon | 1:8ef7820bf777 | 235 | taglen = (taglen << 8) + buf[19]; |
hjjeon | 1:8ef7820bf777 | 236 | ppp_code = buf[22]; |
hjjeon | 1:8ef7820bf777 | 237 | |
hjjeon | 1:8ef7820bf777 | 238 | //Check the Ether-Type and Code in received packet |
hjjeon | 1:8ef7820bf777 | 239 | t_idx = 20; |
hjjeon | 1:8ef7820bf777 | 240 | switch (ethertype) |
hjjeon | 1:8ef7820bf777 | 241 | { |
hjjeon | 1:8ef7820bf777 | 242 | case PPPoE_DISCOVERY : |
hjjeon | 1:8ef7820bf777 | 243 | if (pppoecode == PPPoE_PADO) |
hjjeon | 1:8ef7820bf777 | 244 | { |
hjjeon | 1:8ef7820bf777 | 245 | #ifdef __DEF_PPP_DBG1__ |
hjjeon | 1:8ef7820bf777 | 246 | printf("PPPoE Discovery: PADO received\r\n\r\n"); |
hjjeon | 1:8ef7820bf777 | 247 | #endif |
hjjeon | 1:8ef7820bf777 | 248 | for(i = 0; i < 6; i++) NAS_mac[i] = buf[6+i]; |
hjjeon | 1:8ef7820bf777 | 249 | // PPPoE Frame |
hjjeon | 1:8ef7820bf777 | 250 | while(taglen) |
hjjeon | 1:8ef7820bf777 | 251 | { |
hjjeon | 1:8ef7820bf777 | 252 | tagname = buf[t_idx]; |
hjjeon | 1:8ef7820bf777 | 253 | tagname = (tagname << 8) + buf[t_idx+1]; |
hjjeon | 1:8ef7820bf777 | 254 | |
hjjeon | 1:8ef7820bf777 | 255 | ppp_tag_len = buf[t_idx+2]; |
hjjeon | 1:8ef7820bf777 | 256 | ppp_tag_len = (ppp_tag_len << 8) + buf[t_idx+3]; |
hjjeon | 1:8ef7820bf777 | 257 | |
hjjeon | 1:8ef7820bf777 | 258 | // Check option field overflow |
hjjeon | 1:8ef7820bf777 | 259 | // (OPTMSG_LEN defined maximum option field length.) |
hjjeon | 1:8ef7820bf777 | 260 | if((acknak_idx + (ppp_tag_len+4)) > OPTMSG_LEN) |
hjjeon | 1:8ef7820bf777 | 261 | { |
hjjeon | 1:8ef7820bf777 | 262 | #ifdef __DEF_PPP_DBG__ |
hjjeon | 1:8ef7820bf777 | 263 | printf("PPPoE Protocol option field overflow occuerd!\r\n"); |
hjjeon | 1:8ef7820bf777 | 264 | #endif |
hjjeon | 1:8ef7820bf777 | 265 | break; |
hjjeon | 1:8ef7820bf777 | 266 | } |
hjjeon | 1:8ef7820bf777 | 267 | else |
hjjeon | 1:8ef7820bf777 | 268 | { |
hjjeon | 1:8ef7820bf777 | 269 | switch(tagname) |
hjjeon | 1:8ef7820bf777 | 270 | { |
hjjeon | 1:8ef7820bf777 | 271 | case PPPoED_SERVICE_NAME : |
hjjeon | 1:8ef7820bf777 | 272 | if ((pppoe_control_flag & FLAG_PADO_SERVICENAME) == 0) |
hjjeon | 1:8ef7820bf777 | 273 | { |
hjjeon | 1:8ef7820bf777 | 274 | memcpy(&acknak_opt[acknak_idx], &buf[t_idx], ppp_tag_len+4); |
hjjeon | 1:8ef7820bf777 | 275 | acknak_idx += (ppp_tag_len+4); |
hjjeon | 1:8ef7820bf777 | 276 | pppoe_control_flag = pppoe_control_flag | FLAG_PADO_SERVICENAME; |
hjjeon | 1:8ef7820bf777 | 277 | } |
hjjeon | 1:8ef7820bf777 | 278 | break; |
hjjeon | 1:8ef7820bf777 | 279 | case PPPoED_HOST_UNIQ : |
hjjeon | 1:8ef7820bf777 | 280 | case PPPoED_AC_COOKIE : |
hjjeon | 1:8ef7820bf777 | 281 | memcpy(&acknak_opt[acknak_idx], &buf[t_idx], ppp_tag_len+4); |
hjjeon | 1:8ef7820bf777 | 282 | acknak_idx += (ppp_tag_len+4); |
hjjeon | 1:8ef7820bf777 | 283 | break; |
hjjeon | 1:8ef7820bf777 | 284 | default : |
hjjeon | 1:8ef7820bf777 | 285 | //case PPPoED_AC_NAME : |
hjjeon | 1:8ef7820bf777 | 286 | break; |
hjjeon | 1:8ef7820bf777 | 287 | } |
hjjeon | 1:8ef7820bf777 | 288 | } |
hjjeon | 1:8ef7820bf777 | 289 | t_idx += (ppp_tag_len+4); |
hjjeon | 1:8ef7820bf777 | 290 | taglen -= (ppp_tag_len+4); |
hjjeon | 1:8ef7820bf777 | 291 | } |
hjjeon | 1:8ef7820bf777 | 292 | |
hjjeon | 1:8ef7820bf777 | 293 | memcpy(&PPPPROTO.opt[0], &acknak_opt[0], acknak_idx); |
hjjeon | 1:8ef7820bf777 | 294 | |
hjjeon | 1:8ef7820bf777 | 295 | for(i = 0; i < 6; i++) |
hjjeon | 1:8ef7820bf777 | 296 | { |
hjjeon | 1:8ef7820bf777 | 297 | PPPMSG_req.dst_mac[i] = NAS_mac[i]; // NAS MAC address |
hjjeon | 1:8ef7820bf777 | 298 | } |
hjjeon | 1:8ef7820bf777 | 299 | PPPMSG_req.frame_code = PPPoE_PADR; |
hjjeon | 1:8ef7820bf777 | 300 | //change MSB and LSB because of different endian. |
hjjeon | 1:8ef7820bf777 | 301 | PPPMSG_req.len = 0; |
hjjeon | 1:8ef7820bf777 | 302 | PPPMSG_req.len = (acknak_idx & 0xFF) << 8; |
hjjeon | 1:8ef7820bf777 | 303 | PPPMSG_req.len |= ((acknak_idx >> 8) & 0xFF); |
hjjeon | 1:8ef7820bf777 | 304 | |
hjjeon | 1:8ef7820bf777 | 305 | #ifdef __DEF_PPP_DBG1__ |
hjjeon | 1:8ef7820bf777 | 306 | printf("PPPoE Discovery : PADR send\r\n"); |
hjjeon | 1:8ef7820bf777 | 307 | #endif |
hjjeon | 1:8ef7820bf777 | 308 | ppp_send(); |
hjjeon | 1:8ef7820bf777 | 309 | pppoe_control_flag = pppoe_control_flag | FLAG_DISCOVERY_RCV_PADO; |
hjjeon | 1:8ef7820bf777 | 310 | |
hjjeon | 1:8ef7820bf777 | 311 | |
hjjeon | 1:8ef7820bf777 | 312 | } |
hjjeon | 1:8ef7820bf777 | 313 | else if(pppoecode == PPPoE_PADS) |
hjjeon | 1:8ef7820bf777 | 314 | { |
hjjeon | 1:8ef7820bf777 | 315 | #ifdef __DEF_PPP_DBG1__ |
hjjeon | 1:8ef7820bf777 | 316 | printf("PPPoE Discovery: PADS received\r\n\r\n"); |
hjjeon | 1:8ef7820bf777 | 317 | #endif |
hjjeon | 1:8ef7820bf777 | 318 | pppoe_control_flag = pppoe_control_flag | FLAG_DISCOVERY_RCV_PADS; |
hjjeon | 1:8ef7820bf777 | 319 | |
hjjeon | 1:8ef7820bf777 | 320 | NAS_sessionid = buf[16]; |
hjjeon | 1:8ef7820bf777 | 321 | NAS_sessionid = (NAS_sessionid << 8) + buf[17]; |
hjjeon | 1:8ef7820bf777 | 322 | } |
hjjeon | 1:8ef7820bf777 | 323 | #ifdef __DEF_PPP_DBG1__ |
hjjeon | 1:8ef7820bf777 | 324 | else printf("Not necessary packet received\r\n"); |
hjjeon | 1:8ef7820bf777 | 325 | #endif |
hjjeon | 1:8ef7820bf777 | 326 | break; |
hjjeon | 1:8ef7820bf777 | 327 | case PPPoE_SESSION : |
hjjeon | 1:8ef7820bf777 | 328 | |
hjjeon | 1:8ef7820bf777 | 329 | // Process LCP |
hjjeon | 1:8ef7820bf777 | 330 | if ((buf[20] == 0xc0) && (buf[21] == 0x21)) |
hjjeon | 1:8ef7820bf777 | 331 | { |
hjjeon | 1:8ef7820bf777 | 332 | //change MSB and LSB because of different endian. |
hjjeon | 1:8ef7820bf777 | 333 | PPPPROTO.protocol = 0; |
hjjeon | 1:8ef7820bf777 | 334 | PPPPROTO.protocol = (PPPoE_LCP & 0xFF) << 8; |
hjjeon | 1:8ef7820bf777 | 335 | PPPPROTO.protocol |= ((PPPoE_LCP >> 8) & 0xFF); |
hjjeon | 1:8ef7820bf777 | 336 | |
hjjeon | 1:8ef7820bf777 | 337 | switch (ppp_code) |
hjjeon | 1:8ef7820bf777 | 338 | { |
hjjeon | 1:8ef7820bf777 | 339 | // when lcp_cr_rcv flag set && lcp_cr_sent flag set, goto PAP or CHAP |
hjjeon | 1:8ef7820bf777 | 340 | case PPP_CONFIG_REQ : //Configuration Request receive, and then ack or reject send |
hjjeon | 1:8ef7820bf777 | 341 | // when ack sent, lcp_cr_rcv flag set |
hjjeon | 1:8ef7820bf777 | 342 | #ifdef __DEF_PPP_DBG1__ |
hjjeon | 1:8ef7820bf777 | 343 | printf("PPPoE Session LCP: Configure-Request received\r\n\r\n"); |
hjjeon | 1:8ef7820bf777 | 344 | #endif |
hjjeon | 1:8ef7820bf777 | 345 | get_protocol_id = buf[23]; |
hjjeon | 1:8ef7820bf777 | 346 | getlen = buf[24]; |
hjjeon | 1:8ef7820bf777 | 347 | getlen = (getlen<<8) + buf[25]; |
hjjeon | 1:8ef7820bf777 | 348 | |
hjjeon | 1:8ef7820bf777 | 349 | getlen -= 4; |
hjjeon | 1:8ef7820bf777 | 350 | t_idx = 26; |
hjjeon | 1:8ef7820bf777 | 351 | while (getlen) |
hjjeon | 1:8ef7820bf777 | 352 | { |
hjjeon | 1:8ef7820bf777 | 353 | opt_code = buf[t_idx]; |
hjjeon | 1:8ef7820bf777 | 354 | opt_len = buf[t_idx+1]; |
hjjeon | 1:8ef7820bf777 | 355 | // Check option field overflow |
hjjeon | 1:8ef7820bf777 | 356 | // (OPTMSG_LEN defined maximum option field length.) |
hjjeon | 1:8ef7820bf777 | 357 | if((acknak_idx + opt_len) > OPTMSG_LEN || (rjt_idx + opt_len) > OPTMSG_LEN) |
hjjeon | 1:8ef7820bf777 | 358 | { |
hjjeon | 1:8ef7820bf777 | 359 | #ifdef __DEF_PPP_DBG__ |
hjjeon | 1:8ef7820bf777 | 360 | printf("PPPoE Protocol option field overflow occuerd!\r\n"); |
hjjeon | 1:8ef7820bf777 | 361 | #endif |
hjjeon | 1:8ef7820bf777 | 362 | break; |
hjjeon | 1:8ef7820bf777 | 363 | } |
hjjeon | 1:8ef7820bf777 | 364 | else |
hjjeon | 1:8ef7820bf777 | 365 | { |
hjjeon | 1:8ef7820bf777 | 366 | switch (opt_code) |
hjjeon | 1:8ef7820bf777 | 367 | { |
hjjeon | 1:8ef7820bf777 | 368 | case LCP_AUTH : // Authentication-Protocol |
hjjeon | 1:8ef7820bf777 | 369 | auth_protocol = buf[t_idx+2]; |
hjjeon | 1:8ef7820bf777 | 370 | auth_protocol = (auth_protocol << 8) + buf[t_idx+3]; |
hjjeon | 1:8ef7820bf777 | 371 | chap_algorithm = buf[t_idx+4]; |
hjjeon | 1:8ef7820bf777 | 372 | memcpy(&acknak_opt[acknak_idx], &buf[t_idx], opt_len); |
hjjeon | 1:8ef7820bf777 | 373 | acknak_idx += opt_len; |
hjjeon | 1:8ef7820bf777 | 374 | break; |
hjjeon | 1:8ef7820bf777 | 375 | case LCP_MRU : // MRU (Maximum-Receive-Unit) |
hjjeon | 1:8ef7820bf777 | 376 | memcpy(&acknak_opt[acknak_idx], &buf[t_idx], opt_len); |
hjjeon | 1:8ef7820bf777 | 377 | acknak_idx += opt_len; |
hjjeon | 1:8ef7820bf777 | 378 | break; |
hjjeon | 1:8ef7820bf777 | 379 | case LCP_MAGICNUM : // Magic-Number |
hjjeon | 1:8ef7820bf777 | 380 | // opt_code : 0x01, 0x03, 0x05 shared process part |
hjjeon | 1:8ef7820bf777 | 381 | memcpy(&acknak_opt[acknak_idx], &buf[t_idx], opt_len); |
hjjeon | 1:8ef7820bf777 | 382 | acknak_idx += opt_len; |
hjjeon | 1:8ef7820bf777 | 383 | |
hjjeon | 1:8ef7820bf777 | 384 | break; |
hjjeon | 1:8ef7820bf777 | 385 | default : |
hjjeon | 1:8ef7820bf777 | 386 | memcpy(&rjt_opt[rjt_idx], &buf[t_idx], opt_len); |
hjjeon | 1:8ef7820bf777 | 387 | rjt_idx += opt_len; |
hjjeon | 1:8ef7820bf777 | 388 | break; |
hjjeon | 1:8ef7820bf777 | 389 | } |
hjjeon | 1:8ef7820bf777 | 390 | } |
hjjeon | 1:8ef7820bf777 | 391 | t_idx += opt_len; |
hjjeon | 1:8ef7820bf777 | 392 | getlen -= opt_len; |
hjjeon | 1:8ef7820bf777 | 393 | } |
hjjeon | 1:8ef7820bf777 | 394 | |
hjjeon | 1:8ef7820bf777 | 395 | |
hjjeon | 1:8ef7820bf777 | 396 | |
hjjeon | 1:8ef7820bf777 | 397 | if (rjt_idx) |
hjjeon | 1:8ef7820bf777 | 398 | { |
hjjeon | 1:8ef7820bf777 | 399 | // reject send, then wait cr |
hjjeon | 1:8ef7820bf777 | 400 | PPPPROTO.pcode = PPP_CONFIG_REJ; // Reject |
hjjeon | 1:8ef7820bf777 | 401 | memcpy(&PPPPROTO.opt[0], &rjt_opt[0], rjt_idx); |
hjjeon | 1:8ef7820bf777 | 402 | PPPPROTO.id = get_protocol_id; |
hjjeon | 1:8ef7820bf777 | 403 | |
hjjeon | 1:8ef7820bf777 | 404 | //change MSB and LSB because of different endian. |
hjjeon | 1:8ef7820bf777 | 405 | PPPPROTO.len = 0; |
hjjeon | 1:8ef7820bf777 | 406 | PPPPROTO.len = ((rjt_idx+4) & 0xFF) << 8; |
hjjeon | 1:8ef7820bf777 | 407 | PPPPROTO.len |= (((rjt_idx+4) >> 8) & 0xFF); |
hjjeon | 1:8ef7820bf777 | 408 | |
hjjeon | 1:8ef7820bf777 | 409 | //change MSB and LSB because of different endian. |
hjjeon | 1:8ef7820bf777 | 410 | PPPMSG_req.len = 0; |
hjjeon | 1:8ef7820bf777 | 411 | PPPMSG_req.len = ((rjt_idx+6) & 0xFF) << 8; |
hjjeon | 1:8ef7820bf777 | 412 | PPPMSG_req.len |= (((rjt_idx+6) >> 8) & 0xFF); |
hjjeon | 1:8ef7820bf777 | 413 | |
hjjeon | 1:8ef7820bf777 | 414 | ppp_send(); |
hjjeon | 1:8ef7820bf777 | 415 | } |
hjjeon | 1:8ef7820bf777 | 416 | else |
hjjeon | 1:8ef7820bf777 | 417 | { |
hjjeon | 1:8ef7820bf777 | 418 | // ack send, lcp_cr_rcv flag set |
hjjeon | 1:8ef7820bf777 | 419 | //change MSB and LSB because of different endian. |
hjjeon | 1:8ef7820bf777 | 420 | PPPPROTO.protocol = 0; |
hjjeon | 1:8ef7820bf777 | 421 | PPPPROTO.protocol = (PPPoE_LCP & 0xFF) << 8; |
hjjeon | 1:8ef7820bf777 | 422 | PPPPROTO.protocol |= ((PPPoE_LCP >> 8) & 0xFF); |
hjjeon | 1:8ef7820bf777 | 423 | |
hjjeon | 1:8ef7820bf777 | 424 | PPPPROTO.pcode = PPP_CONFIG_ACK; // ack |
hjjeon | 1:8ef7820bf777 | 425 | memcpy(&PPPPROTO.opt[0], &acknak_opt[0], acknak_idx); |
hjjeon | 1:8ef7820bf777 | 426 | PPPPROTO.id = get_protocol_id; |
hjjeon | 1:8ef7820bf777 | 427 | //change MSB and LSB because of different endian. |
hjjeon | 1:8ef7820bf777 | 428 | PPPPROTO.len = 0; |
hjjeon | 1:8ef7820bf777 | 429 | PPPPROTO.len = ((acknak_idx+4) & 0xFF) << 8; |
hjjeon | 1:8ef7820bf777 | 430 | PPPPROTO.len |= (((acknak_idx+4) >> 8) & 0xFF); |
hjjeon | 1:8ef7820bf777 | 431 | |
hjjeon | 1:8ef7820bf777 | 432 | //change MSB and LSB because of different endian. |
hjjeon | 1:8ef7820bf777 | 433 | PPPMSG_req.len = 0; |
hjjeon | 1:8ef7820bf777 | 434 | PPPMSG_req.len = ((acknak_idx+6) & 0xFF) << 8; |
hjjeon | 1:8ef7820bf777 | 435 | PPPMSG_req.len |= (((acknak_idx+6) >> 8) & 0xFF); |
hjjeon | 1:8ef7820bf777 | 436 | #ifdef __DEF_PPP_DBG1__ |
hjjeon | 1:8ef7820bf777 | 437 | printf("LCP Configuration Ack send\r\n"); |
hjjeon | 1:8ef7820bf777 | 438 | #endif |
hjjeon | 1:8ef7820bf777 | 439 | ppp_send();//ack send |
hjjeon | 1:8ef7820bf777 | 440 | pppoe_control_flag = pppoe_control_flag | FLAG_LCP_CR_RCV; |
hjjeon | 1:8ef7820bf777 | 441 | |
hjjeon | 1:8ef7820bf777 | 442 | } |
hjjeon | 1:8ef7820bf777 | 443 | break; |
hjjeon | 1:8ef7820bf777 | 444 | |
hjjeon | 1:8ef7820bf777 | 445 | |
hjjeon | 1:8ef7820bf777 | 446 | case PPP_CONFIG_ACK : //ack, then lcp_cr_sent flag set |
hjjeon | 1:8ef7820bf777 | 447 | #ifdef __DEF_PPP_DBG1__ |
hjjeon | 1:8ef7820bf777 | 448 | printf("PPPoE Session LCP: Configuration Ack received\r\n\r\n"); |
hjjeon | 1:8ef7820bf777 | 449 | #endif |
hjjeon | 1:8ef7820bf777 | 450 | pppoe_control_flag = pppoe_control_flag | FLAG_LCP_CR_SNT; |
hjjeon | 1:8ef7820bf777 | 451 | break; |
hjjeon | 1:8ef7820bf777 | 452 | |
hjjeon | 1:8ef7820bf777 | 453 | |
hjjeon | 1:8ef7820bf777 | 454 | case PPP_TERM_ACK : |
hjjeon | 1:8ef7820bf777 | 455 | #ifdef __DEF_PPP_DBG1__ |
hjjeon | 1:8ef7820bf777 | 456 | printf("Termination Ack received\r\n\r\n"); |
hjjeon | 1:8ef7820bf777 | 457 | #endif |
hjjeon | 1:8ef7820bf777 | 458 | pppoe_control_flag = pppoe_control_flag | FLAG_TERMINATION_ACK_RCV; |
hjjeon | 1:8ef7820bf777 | 459 | |
hjjeon | 1:8ef7820bf777 | 460 | break; |
hjjeon | 1:8ef7820bf777 | 461 | |
hjjeon | 1:8ef7820bf777 | 462 | |
hjjeon | 1:8ef7820bf777 | 463 | case PPP_TERM_REQ: |
hjjeon | 1:8ef7820bf777 | 464 | #ifdef __DEF_PPP_DBG1__ |
hjjeon | 1:8ef7820bf777 | 465 | printf("Termination request received\r\n\r\n"); |
hjjeon | 1:8ef7820bf777 | 466 | #endif |
hjjeon | 1:8ef7820bf777 | 467 | |
hjjeon | 1:8ef7820bf777 | 468 | //change MSB and LSB because of different endian. |
hjjeon | 1:8ef7820bf777 | 469 | PPPMSG_req.ether_type = 0; |
hjjeon | 1:8ef7820bf777 | 470 | PPPMSG_req.ether_type = (PPPoE_SESSION & 0xFF) << 8; |
hjjeon | 1:8ef7820bf777 | 471 | PPPMSG_req.ether_type |= ((PPPoE_SESSION >> 8) & 0xFF);// session |
hjjeon | 1:8ef7820bf777 | 472 | |
hjjeon | 1:8ef7820bf777 | 473 | PPPMSG_req.frame_code = 0x00; // session data |
hjjeon | 1:8ef7820bf777 | 474 | //change MSB and LSB because of different endian. |
hjjeon | 1:8ef7820bf777 | 475 | PPPMSG_req.session_id = 0; |
hjjeon | 1:8ef7820bf777 | 476 | PPPMSG_req.session_id = (NAS_sessionid & 0xFF) << 8; |
hjjeon | 1:8ef7820bf777 | 477 | PPPMSG_req.session_id |= ((NAS_sessionid >> 8) & 0xFF); |
hjjeon | 1:8ef7820bf777 | 478 | |
hjjeon | 1:8ef7820bf777 | 479 | //change MSB and LSB because of different endian. |
hjjeon | 1:8ef7820bf777 | 480 | PPPPROTO.protocol = 0; |
hjjeon | 1:8ef7820bf777 | 481 | PPPPROTO.protocol = (PPPoE_LCP & 0xFF) << 8; |
hjjeon | 1:8ef7820bf777 | 482 | PPPPROTO.protocol |= ((PPPoE_LCP >> 8) & 0xFF); |
hjjeon | 1:8ef7820bf777 | 483 | |
hjjeon | 1:8ef7820bf777 | 484 | PPPPROTO.pcode = PPP_TERM_ACK; // Terminate-ack |
hjjeon | 1:8ef7820bf777 | 485 | |
hjjeon | 1:8ef7820bf777 | 486 | PPPPROTO.opt[0] = 0x00; // Magic number |
hjjeon | 1:8ef7820bf777 | 487 | PPPPROTO.opt[1] = 0x01; |
hjjeon | 1:8ef7820bf777 | 488 | PPPPROTO.opt[2] = 0x02; |
hjjeon | 1:8ef7820bf777 | 489 | PPPPROTO.opt[3] = 0x03; |
hjjeon | 1:8ef7820bf777 | 490 | |
hjjeon | 1:8ef7820bf777 | 491 | PPPPROTO.id = protocol_id++; |
hjjeon | 1:8ef7820bf777 | 492 | //change MSB and LSB because of different endian. |
hjjeon | 1:8ef7820bf777 | 493 | PPPPROTO.len = 0; |
hjjeon | 1:8ef7820bf777 | 494 | PPPPROTO.len = (8 & 0xFF) << 8; |
hjjeon | 1:8ef7820bf777 | 495 | PPPPROTO.len |= ((8 >> 8) & 0xFF); |
hjjeon | 1:8ef7820bf777 | 496 | |
hjjeon | 1:8ef7820bf777 | 497 | //change MSB and LSB because of different endian. |
hjjeon | 1:8ef7820bf777 | 498 | PPPMSG_req.len = 0; |
hjjeon | 1:8ef7820bf777 | 499 | PPPMSG_req.len = (10 & 0xFF) << 8; |
hjjeon | 1:8ef7820bf777 | 500 | PPPMSG_req.len |= ((10 >> 8) & 0xFF); |
hjjeon | 1:8ef7820bf777 | 501 | |
hjjeon | 1:8ef7820bf777 | 502 | pppoe_control_flag = pppoe_control_flag | FLAG_TERMINATION_REQ_RCV; |
hjjeon | 1:8ef7820bf777 | 503 | |
hjjeon | 1:8ef7820bf777 | 504 | #ifdef __DEF_PPP_DBG1__ |
hjjeon | 1:8ef7820bf777 | 505 | printf("Termination ack send\r\n"); |
hjjeon | 1:8ef7820bf777 | 506 | #endif |
hjjeon | 1:8ef7820bf777 | 507 | ppp_send(); |
hjjeon | 1:8ef7820bf777 | 508 | |
hjjeon | 1:8ef7820bf777 | 509 | break; |
hjjeon | 1:8ef7820bf777 | 510 | |
hjjeon | 1:8ef7820bf777 | 511 | |
hjjeon | 1:8ef7820bf777 | 512 | /* |
hjjeon | 1:8ef7820bf777 | 513 | * Notice : This part is not implemented. |
hjjeon | 1:8ef7820bf777 | 514 | * If necessary, please implement more for reply for request from NAS. |
hjjeon | 1:8ef7820bf777 | 515 | * |
hjjeon | 1:8ef7820bf777 | 516 | case 0x04 : //reject |
hjjeon | 1:8ef7820bf777 | 517 | break; |
hjjeon | 1:8ef7820bf777 | 518 | |
hjjeon | 1:8ef7820bf777 | 519 | case 0x09 : // Echo-Response |
hjjeon | 1:8ef7820bf777 | 520 | // Backup |
hjjeon | 1:8ef7820bf777 | 521 | tmp_protocol = PPPPROTO.protocol; |
hjjeon | 1:8ef7820bf777 | 522 | tmp_pcode = PPPPROTO.pcode; |
hjjeon | 1:8ef7820bf777 | 523 | |
hjjeon | 1:8ef7820bf777 | 524 | PPPPROTO.protocol = PPPoE_LCP; |
hjjeon | 1:8ef7820bf777 | 525 | PPPPROTO.pcode = PPP_ECHO_REP; |
hjjeon | 1:8ef7820bf777 | 526 | |
hjjeon | 1:8ef7820bf777 | 527 | PPPPROTO.id = buf[23]; |
hjjeon | 1:8ef7820bf777 | 528 | |
hjjeon | 1:8ef7820bf777 | 529 | PPPPROTO.len = buf[24]; |
hjjeon | 1:8ef7820bf777 | 530 | PPPPROTO.len = (PPPPROTO.len << 8) + buf[25]; |
hjjeon | 1:8ef7820bf777 | 531 | |
hjjeon | 1:8ef7820bf777 | 532 | memcpy(&PPPPROTO.opt[0], &buf[26], PPPPROTO.len-4); |
hjjeon | 1:8ef7820bf777 | 533 | ppp_send(); |
hjjeon | 1:8ef7820bf777 | 534 | |
hjjeon | 1:8ef7820bf777 | 535 | // Recover |
hjjeon | 1:8ef7820bf777 | 536 | PPPPROTO.protocol = tmp_protocol; |
hjjeon | 1:8ef7820bf777 | 537 | PPPPROTO.pcode = tmp_pcode; |
hjjeon | 1:8ef7820bf777 | 538 | break; |
hjjeon | 1:8ef7820bf777 | 539 | */ |
hjjeon | 1:8ef7820bf777 | 540 | |
hjjeon | 1:8ef7820bf777 | 541 | default: |
hjjeon | 1:8ef7820bf777 | 542 | #ifdef __DEF_PPP_DBG1__ |
hjjeon | 1:8ef7820bf777 | 543 | printf("Not necessary packet received\r\n"); |
hjjeon | 1:8ef7820bf777 | 544 | #endif |
hjjeon | 1:8ef7820bf777 | 545 | break; |
hjjeon | 1:8ef7820bf777 | 546 | } |
hjjeon | 1:8ef7820bf777 | 547 | } |
hjjeon | 1:8ef7820bf777 | 548 | // Process PAP |
hjjeon | 1:8ef7820bf777 | 549 | else if ((buf[20] == 0xc0) && (buf[21] == 0x23)) |
hjjeon | 1:8ef7820bf777 | 550 | { |
hjjeon | 1:8ef7820bf777 | 551 | if(ppp_code == PPP_CONFIG_ACK) // PPP_CONFIG_ACK = 0x02 |
hjjeon | 1:8ef7820bf777 | 552 | { |
hjjeon | 1:8ef7820bf777 | 553 | pppoe_control_flag = pppoe_control_flag | FLAG_PAP_ACK_RCV;// receice ack |
hjjeon | 1:8ef7820bf777 | 554 | |
hjjeon | 1:8ef7820bf777 | 555 | #ifdef __DEF_PPP_DBG1__ |
hjjeon | 1:8ef7820bf777 | 556 | printf("PPPoE Session PAP: Authentication ACK received\r\n\r\n"); |
hjjeon | 1:8ef7820bf777 | 557 | #endif |
hjjeon | 1:8ef7820bf777 | 558 | } |
hjjeon | 1:8ef7820bf777 | 559 | else if(ppp_code == PPP_TERM_ACK) |
hjjeon | 1:8ef7820bf777 | 560 | { |
hjjeon | 1:8ef7820bf777 | 561 | #ifdef __DEF_PPP_DBG1__ |
hjjeon | 1:8ef7820bf777 | 562 | printf("Termination Ack received\r\n\r\n"); |
hjjeon | 1:8ef7820bf777 | 563 | #endif |
hjjeon | 1:8ef7820bf777 | 564 | pppoe_control_flag = pppoe_control_flag | FLAG_TERMINATION_ACK_RCV; |
hjjeon | 1:8ef7820bf777 | 565 | } |
hjjeon | 1:8ef7820bf777 | 566 | } |
hjjeon | 1:8ef7820bf777 | 567 | // Process CHAP |
hjjeon | 1:8ef7820bf777 | 568 | |
hjjeon | 1:8ef7820bf777 | 569 | else if ((buf[20] == 0xc2) && (buf[21] == 0x23)) |
hjjeon | 1:8ef7820bf777 | 570 | { |
hjjeon | 1:8ef7820bf777 | 571 | switch(chap_algorithm) |
hjjeon | 1:8ef7820bf777 | 572 | { |
hjjeon | 1:8ef7820bf777 | 573 | case MD5 : // 0x05, MD5 |
hjjeon | 1:8ef7820bf777 | 574 | |
hjjeon | 1:8ef7820bf777 | 575 | get_protocol_id = buf[23]; |
hjjeon | 1:8ef7820bf777 | 576 | // length of [code ~ packet end] |
hjjeon | 1:8ef7820bf777 | 577 | getlen = buf[24]; |
hjjeon | 1:8ef7820bf777 | 578 | getlen = (getlen<<8) + buf[25]; |
hjjeon | 1:8ef7820bf777 | 579 | // so, 'CHAP data' length is getlen - length of [code(1), ID(1), Length(2)]. |
hjjeon | 1:8ef7820bf777 | 580 | getlen -= 4; //PPP CHAP total(getlen) - 4(header) = data length(value size(1) + value + name ) |
hjjeon | 1:8ef7820bf777 | 581 | t_idx = 26; |
hjjeon | 1:8ef7820bf777 | 582 | |
hjjeon | 1:8ef7820bf777 | 583 | switch(ppp_code) |
hjjeon | 1:8ef7820bf777 | 584 | { |
hjjeon | 1:8ef7820bf777 | 585 | case 0x01 : |
hjjeon | 1:8ef7820bf777 | 586 | #ifdef __DEF_PPP_DBG1__ |
hjjeon | 1:8ef7820bf777 | 587 | printf("PPPoE Session CHAP: CHAP Challenge received\r\n\r\n"); |
hjjeon | 1:8ef7820bf777 | 588 | #endif |
hjjeon | 1:8ef7820bf777 | 589 | // Challenge, parse the packet and return 'Response' to NAS |
hjjeon | 1:8ef7820bf777 | 590 | //-- MD5 calc input consist of ID(1), password(pwlen) and Challenge value(16). |
hjjeon | 1:8ef7820bf777 | 591 | |
hjjeon | 1:8ef7820bf777 | 592 | //-- buf[t_idx] => CV(challenge value) size, buf[t_idx+1] => start byte of CV |
hjjeon | 1:8ef7820bf777 | 593 | |
hjjeon | 1:8ef7820bf777 | 594 | memcpy(&str[str_len], &get_protocol_id, 1); // ID(value) |
hjjeon | 1:8ef7820bf777 | 595 | str_len += 1; |
hjjeon | 1:8ef7820bf777 | 596 | memcpy(&str[str_len], &pppoe_pw[0], pppoe_pw_len); // user password |
hjjeon | 1:8ef7820bf777 | 597 | str_len += pppoe_pw_len; |
hjjeon | 1:8ef7820bf777 | 598 | memcpy(&str[str_len], &buf[t_idx+1], buf[t_idx]); // CV(value) |
hjjeon | 1:8ef7820bf777 | 599 | str_len += buf[t_idx]; |
hjjeon | 1:8ef7820bf777 | 600 | |
hjjeon | 1:8ef7820bf777 | 601 | /* |
hjjeon | 1:8ef7820bf777 | 602 | memcpy(&str[str_len], &buf[t_idx+1], buf[t_idx]); // CV(value) |
hjjeon | 1:8ef7820bf777 | 603 | str_len += buf[t_idx]; |
hjjeon | 1:8ef7820bf777 | 604 | memcpy(&str[str_len], &pppoe_pw, pppoe_pw_len); // user password |
hjjeon | 1:8ef7820bf777 | 605 | str_len += pppoe_pw_len; |
hjjeon | 1:8ef7820bf777 | 606 | memcpy(&str[str_len], &pppoe_id, pppoe_id_len); // user id |
hjjeon | 1:8ef7820bf777 | 607 | str_len += pppoe_id_len; |
hjjeon | 1:8ef7820bf777 | 608 | */ |
hjjeon | 1:8ef7820bf777 | 609 | |
hjjeon | 1:8ef7820bf777 | 610 | /* |
hjjeon | 1:8ef7820bf777 | 611 | MD5Init(&context); |
hjjeon | 1:8ef7820bf777 | 612 | MD5Update(&context, str, str_len); |
hjjeon | 1:8ef7820bf777 | 613 | MD5Final(digest, &context); |
hjjeon | 1:8ef7820bf777 | 614 | */ |
hjjeon | 1:8ef7820bf777 | 615 | |
hjjeon | 1:8ef7820bf777 | 616 | |
hjjeon | 1:8ef7820bf777 | 617 | MD5Init(&context); |
hjjeon | 1:8ef7820bf777 | 618 | MD5Update(&context, &get_protocol_id, 1); |
hjjeon | 1:8ef7820bf777 | 619 | MD5Update(&context, pppoe_pw, pppoe_pw_len); |
hjjeon | 1:8ef7820bf777 | 620 | MD5Update(&context, (unsigned char *)(&buf[t_idx+1]), 16); |
hjjeon | 1:8ef7820bf777 | 621 | MD5Final(digest, &context); |
hjjeon | 1:8ef7820bf777 | 622 | |
hjjeon | 1:8ef7820bf777 | 623 | // making response msg |
hjjeon | 1:8ef7820bf777 | 624 | acknak_opt[acknak_idx++] = CV_HV_LEN; // fixed value, 16 |
hjjeon | 1:8ef7820bf777 | 625 | memcpy(&acknak_opt[acknak_idx], &digest, CV_HV_LEN); |
hjjeon | 1:8ef7820bf777 | 626 | acknak_idx += CV_HV_LEN; |
hjjeon | 1:8ef7820bf777 | 627 | |
hjjeon | 1:8ef7820bf777 | 628 | |
hjjeon | 1:8ef7820bf777 | 629 | memcpy(&acknak_opt[acknak_idx], &pppoe_id, pppoe_id_len); // Name: User ID |
hjjeon | 1:8ef7820bf777 | 630 | acknak_idx += pppoe_id_len; |
hjjeon | 1:8ef7820bf777 | 631 | |
hjjeon | 1:8ef7820bf777 | 632 | |
hjjeon | 1:8ef7820bf777 | 633 | //change MSB and LSB because of different endian. |
hjjeon | 1:8ef7820bf777 | 634 | PPPPROTO.protocol = 0; |
hjjeon | 1:8ef7820bf777 | 635 | PPPPROTO.protocol = (PPPoE_CHAP & 0xFF) << 8; |
hjjeon | 1:8ef7820bf777 | 636 | PPPPROTO.protocol |= ((PPPoE_CHAP >> 8) & 0xFF); |
hjjeon | 1:8ef7820bf777 | 637 | |
hjjeon | 1:8ef7820bf777 | 638 | PPPPROTO.pcode = 0x02; // response |
hjjeon | 1:8ef7820bf777 | 639 | PPPPROTO.id = get_protocol_id; |
hjjeon | 1:8ef7820bf777 | 640 | //change MSB and LSB because of different endian. |
hjjeon | 1:8ef7820bf777 | 641 | PPPPROTO.len = 0; |
hjjeon | 1:8ef7820bf777 | 642 | PPPPROTO.len = ((acknak_idx + 4) & 0xFF) << 8; |
hjjeon | 1:8ef7820bf777 | 643 | PPPPROTO.len |= (((acknak_idx + 4) >> 8) & 0xFF); |
hjjeon | 1:8ef7820bf777 | 644 | |
hjjeon | 1:8ef7820bf777 | 645 | //change MSB and LSB because of different endian. |
hjjeon | 1:8ef7820bf777 | 646 | PPPMSG_req.len = 0; |
hjjeon | 1:8ef7820bf777 | 647 | PPPMSG_req.len = ((acknak_idx + 6) & 0xFF) << 8; |
hjjeon | 1:8ef7820bf777 | 648 | PPPMSG_req.len |= (((acknak_idx + 6) >> 8) & 0xFF); |
hjjeon | 1:8ef7820bf777 | 649 | |
hjjeon | 1:8ef7820bf777 | 650 | |
hjjeon | 1:8ef7820bf777 | 651 | memcpy(&PPPPROTO.opt[0], &acknak_opt[0], acknak_idx); |
hjjeon | 1:8ef7820bf777 | 652 | #ifdef __DEF_PPP_DBG1__ |
hjjeon | 1:8ef7820bf777 | 653 | printf("CHAP response MSG send \r\n\r\n"); |
hjjeon | 1:8ef7820bf777 | 654 | #endif |
hjjeon | 1:8ef7820bf777 | 655 | ppp_send(); |
hjjeon | 1:8ef7820bf777 | 656 | break; |
hjjeon | 1:8ef7820bf777 | 657 | |
hjjeon | 1:8ef7820bf777 | 658 | |
hjjeon | 1:8ef7820bf777 | 659 | case 0x03 : // PPP_SUCCESS |
hjjeon | 1:8ef7820bf777 | 660 | #ifdef __DEF_PPP_DBG1__ |
hjjeon | 1:8ef7820bf777 | 661 | printf("PPPoE Session CHAP: CHAP Succeed packet received\r\n\r\n"); |
hjjeon | 1:8ef7820bf777 | 662 | #endif |
hjjeon | 1:8ef7820bf777 | 663 | pppoe_control_flag = pppoe_control_flag | FLAG_CHAP_SUC_RCV; |
hjjeon | 1:8ef7820bf777 | 664 | |
hjjeon | 1:8ef7820bf777 | 665 | break; |
hjjeon | 1:8ef7820bf777 | 666 | |
hjjeon | 1:8ef7820bf777 | 667 | |
hjjeon | 1:8ef7820bf777 | 668 | case 0x04 : // PPP_FAIL |
hjjeon | 1:8ef7820bf777 | 669 | #ifdef __DEF_PPP_DBG1__ |
hjjeon | 1:8ef7820bf777 | 670 | printf("PPPoE Session CHAP: CHAP failure packet received\r\n\r\n"); |
hjjeon | 1:8ef7820bf777 | 671 | #endif |
hjjeon | 1:8ef7820bf777 | 672 | |
hjjeon | 1:8ef7820bf777 | 673 | break; |
hjjeon | 1:8ef7820bf777 | 674 | |
hjjeon | 1:8ef7820bf777 | 675 | default : |
hjjeon | 1:8ef7820bf777 | 676 | break; |
hjjeon | 1:8ef7820bf777 | 677 | } |
hjjeon | 1:8ef7820bf777 | 678 | break; |
hjjeon | 1:8ef7820bf777 | 679 | #ifdef __DEF_PPP_DBG1__ |
hjjeon | 1:8ef7820bf777 | 680 | case MS_CHAP : // 0x80, MS-CHAP |
hjjeon | 1:8ef7820bf777 | 681 | case MS_CHAP_V2 : // 0x81, MS-CHAP-V2 |
hjjeon | 1:8ef7820bf777 | 682 | printf("This CHAP Algorithm is not supported : "); |
hjjeon | 1:8ef7820bf777 | 683 | if(chap_algorithm == MS_CHAP) printf("MS-CHAP\r\n"); |
hjjeon | 1:8ef7820bf777 | 684 | else printf("MS-CHAP-V2\r\n"); |
hjjeon | 1:8ef7820bf777 | 685 | break; |
hjjeon | 1:8ef7820bf777 | 686 | #endif |
hjjeon | 1:8ef7820bf777 | 687 | default : |
hjjeon | 1:8ef7820bf777 | 688 | break; |
hjjeon | 1:8ef7820bf777 | 689 | } |
hjjeon | 1:8ef7820bf777 | 690 | } |
hjjeon | 1:8ef7820bf777 | 691 | // Process IPCP |
hjjeon | 1:8ef7820bf777 | 692 | else if ((buf[20] == 0x80) && (buf[21] == 0x21)) |
hjjeon | 1:8ef7820bf777 | 693 | { |
hjjeon | 1:8ef7820bf777 | 694 | switch(ppp_code) |
hjjeon | 1:8ef7820bf777 | 695 | { |
hjjeon | 1:8ef7820bf777 | 696 | case PPP_CONFIG_REQ : // cr, send ack |
hjjeon | 1:8ef7820bf777 | 697 | case PPP_CONFIG_NAK : // nak, save ip addr and send config requset |
hjjeon | 1:8ef7820bf777 | 698 | |
hjjeon | 1:8ef7820bf777 | 699 | #ifdef __DEF_PPP_DBG1__ |
hjjeon | 1:8ef7820bf777 | 700 | if(ppp_code == 0x01) printf("PPPoE Session IPCP: Configure-Request received\r\n\r\n"); |
hjjeon | 1:8ef7820bf777 | 701 | else if (ppp_code == 0x03) printf("PPPoE Session IPCP: Configure-Nak received\r\n\r\n"); |
hjjeon | 1:8ef7820bf777 | 702 | #endif |
hjjeon | 1:8ef7820bf777 | 703 | get_protocol_id = buf[23]; |
hjjeon | 1:8ef7820bf777 | 704 | getlen = buf[24]; |
hjjeon | 1:8ef7820bf777 | 705 | getlen = (getlen<<8) + buf[25]; |
hjjeon | 1:8ef7820bf777 | 706 | |
hjjeon | 1:8ef7820bf777 | 707 | getlen -= 4; |
hjjeon | 1:8ef7820bf777 | 708 | t_idx = 26; |
hjjeon | 1:8ef7820bf777 | 709 | |
hjjeon | 1:8ef7820bf777 | 710 | while (getlen) |
hjjeon | 1:8ef7820bf777 | 711 | { |
hjjeon | 1:8ef7820bf777 | 712 | opt_code = buf[t_idx]; |
hjjeon | 1:8ef7820bf777 | 713 | opt_len = buf[t_idx+1]; |
hjjeon | 1:8ef7820bf777 | 714 | |
hjjeon | 1:8ef7820bf777 | 715 | // Check option field overflow |
hjjeon | 1:8ef7820bf777 | 716 | // (OPTMSG_LEN defined maximum option field length.) |
hjjeon | 1:8ef7820bf777 | 717 | if((acknak_idx + opt_len) > OPTMSG_LEN || (rjt_idx + opt_len) > OPTMSG_LEN) |
hjjeon | 1:8ef7820bf777 | 718 | { |
hjjeon | 1:8ef7820bf777 | 719 | #ifdef __DEF_PPP_DBG__ |
hjjeon | 1:8ef7820bf777 | 720 | printf("PPPoE Protocol option field overflow occuerd!\r\n"); |
hjjeon | 1:8ef7820bf777 | 721 | #endif |
hjjeon | 1:8ef7820bf777 | 722 | break; |
hjjeon | 1:8ef7820bf777 | 723 | } |
hjjeon | 1:8ef7820bf777 | 724 | else |
hjjeon | 1:8ef7820bf777 | 725 | { |
hjjeon | 1:8ef7820bf777 | 726 | switch (opt_code) |
hjjeon | 1:8ef7820bf777 | 727 | { |
hjjeon | 1:8ef7820bf777 | 728 | //case 0x02 : // type : ip compression protocol |
hjjeon | 1:8ef7820bf777 | 729 | case 0x03 : // type : ip address |
hjjeon | 1:8ef7820bf777 | 730 | memcpy(&acknak_opt[acknak_idx], &buf[t_idx], opt_len); |
hjjeon | 1:8ef7820bf777 | 731 | memcpy(&pppoe_ip, &buf[t_idx+2], opt_len-2); |
hjjeon | 1:8ef7820bf777 | 732 | |
hjjeon | 1:8ef7820bf777 | 733 | acknak_idx += opt_len; |
hjjeon | 1:8ef7820bf777 | 734 | break; |
hjjeon | 1:8ef7820bf777 | 735 | case 0x81 : //PDNS |
hjjeon | 1:8ef7820bf777 | 736 | memcpy(&acknak_opt[acknak_idx], &buf[t_idx], opt_len); |
hjjeon | 1:8ef7820bf777 | 737 | memcpy(&pppoe_pdns, &buf[t_idx+2], opt_len-2); |
hjjeon | 1:8ef7820bf777 | 738 | |
hjjeon | 1:8ef7820bf777 | 739 | acknak_idx += opt_len; |
hjjeon | 1:8ef7820bf777 | 740 | break; |
hjjeon | 1:8ef7820bf777 | 741 | case 0x83 : //SDNS |
hjjeon | 1:8ef7820bf777 | 742 | memcpy(&acknak_opt[acknak_idx], &buf[t_idx], opt_len); |
hjjeon | 1:8ef7820bf777 | 743 | memcpy(&pppoe_sdns, &buf[t_idx+2], opt_len-2); |
hjjeon | 1:8ef7820bf777 | 744 | |
hjjeon | 1:8ef7820bf777 | 745 | acknak_idx += opt_len; |
hjjeon | 1:8ef7820bf777 | 746 | break; |
hjjeon | 1:8ef7820bf777 | 747 | default : // reject |
hjjeon | 1:8ef7820bf777 | 748 | memcpy(&rjt_opt[rjt_idx], &buf[t_idx], opt_len); |
hjjeon | 1:8ef7820bf777 | 749 | rjt_idx += opt_len; |
hjjeon | 1:8ef7820bf777 | 750 | break; |
hjjeon | 1:8ef7820bf777 | 751 | } |
hjjeon | 1:8ef7820bf777 | 752 | } |
hjjeon | 1:8ef7820bf777 | 753 | t_idx += opt_len; |
hjjeon | 1:8ef7820bf777 | 754 | getlen -= opt_len; |
hjjeon | 1:8ef7820bf777 | 755 | } |
hjjeon | 1:8ef7820bf777 | 756 | |
hjjeon | 1:8ef7820bf777 | 757 | |
hjjeon | 1:8ef7820bf777 | 758 | |
hjjeon | 1:8ef7820bf777 | 759 | if (rjt_idx)//if reject |
hjjeon | 1:8ef7820bf777 | 760 | { |
hjjeon | 1:8ef7820bf777 | 761 | #ifdef __DEF_PPP_DBG1__ |
hjjeon | 1:8ef7820bf777 | 762 | printf("reject send!\r\n"); |
hjjeon | 1:8ef7820bf777 | 763 | #endif |
hjjeon | 1:8ef7820bf777 | 764 | // reject send, then wait cr |
hjjeon | 1:8ef7820bf777 | 765 | //change MSB and LSB because of different endian. |
hjjeon | 1:8ef7820bf777 | 766 | PPPPROTO.protocol = 0; |
hjjeon | 1:8ef7820bf777 | 767 | PPPPROTO.protocol = (PPPoE_IPCP & 0xFF) << 8; |
hjjeon | 1:8ef7820bf777 | 768 | PPPPROTO.protocol |= ((PPPoE_IPCP >> 8) & 0xFF); |
hjjeon | 1:8ef7820bf777 | 769 | |
hjjeon | 1:8ef7820bf777 | 770 | PPPPROTO.pcode = 0x04; // Reject |
hjjeon | 1:8ef7820bf777 | 771 | |
hjjeon | 1:8ef7820bf777 | 772 | memcpy(&PPPPROTO.opt[0], &rjt_opt[0], rjt_idx); |
hjjeon | 1:8ef7820bf777 | 773 | |
hjjeon | 1:8ef7820bf777 | 774 | PPPPROTO.id = get_protocol_id; |
hjjeon | 1:8ef7820bf777 | 775 | //change MSB and LSB because of different endian. |
hjjeon | 1:8ef7820bf777 | 776 | PPPPROTO.len = 0; |
hjjeon | 1:8ef7820bf777 | 777 | PPPPROTO.len = ((rjt_idx+4) & 0xFF) << 8; |
hjjeon | 1:8ef7820bf777 | 778 | PPPPROTO.len |= (((rjt_idx+4) >> 8) & 0xFF); |
hjjeon | 1:8ef7820bf777 | 779 | //change MSB and LSB because of different endian. |
hjjeon | 1:8ef7820bf777 | 780 | PPPMSG_req.len = 0; |
hjjeon | 1:8ef7820bf777 | 781 | PPPMSG_req.len = ((rjt_idx+6) & 0xFF) << 8; |
hjjeon | 1:8ef7820bf777 | 782 | PPPMSG_req.len |= (((rjt_idx+6) >> 8) & 0xFF); |
hjjeon | 1:8ef7820bf777 | 783 | |
hjjeon | 1:8ef7820bf777 | 784 | ppp_send(); |
hjjeon | 1:8ef7820bf777 | 785 | } |
hjjeon | 1:8ef7820bf777 | 786 | else // if not reject |
hjjeon | 1:8ef7820bf777 | 787 | { |
hjjeon | 1:8ef7820bf777 | 788 | // ack send, lcp_cr_rcv flag set |
hjjeon | 1:8ef7820bf777 | 789 | //change MSB and LSB because of different endian. |
hjjeon | 1:8ef7820bf777 | 790 | PPPPROTO.protocol = 0; |
hjjeon | 1:8ef7820bf777 | 791 | PPPPROTO.protocol = (PPPoE_IPCP & 0xFF) << 8; |
hjjeon | 1:8ef7820bf777 | 792 | PPPPROTO.protocol |= ((PPPoE_IPCP >> 8) & 0xFF); |
hjjeon | 1:8ef7820bf777 | 793 | |
hjjeon | 1:8ef7820bf777 | 794 | if(ppp_code == PPP_CONFIG_REQ) |
hjjeon | 1:8ef7820bf777 | 795 | { |
hjjeon | 1:8ef7820bf777 | 796 | PPPPROTO.pcode = 0x02; // if configuration request MSG received, send ack |
hjjeon | 1:8ef7820bf777 | 797 | #ifdef __DEF_PPP_DBG1__ |
hjjeon | 1:8ef7820bf777 | 798 | printf("IPCP configuration ack send!\r\n"); |
hjjeon | 1:8ef7820bf777 | 799 | #endif |
hjjeon | 1:8ef7820bf777 | 800 | } |
hjjeon | 1:8ef7820bf777 | 801 | else |
hjjeon | 1:8ef7820bf777 | 802 | { |
hjjeon | 1:8ef7820bf777 | 803 | PPPPROTO.pcode = 0x01; // if nak received, send cr |
hjjeon | 1:8ef7820bf777 | 804 | #ifdef __DEF_PPP_DBG1__ |
hjjeon | 1:8ef7820bf777 | 805 | printf("IPCP Configuration Request send!\r\n"); |
hjjeon | 1:8ef7820bf777 | 806 | #endif |
hjjeon | 1:8ef7820bf777 | 807 | } |
hjjeon | 1:8ef7820bf777 | 808 | |
hjjeon | 1:8ef7820bf777 | 809 | memcpy(&PPPPROTO.opt[0], &acknak_opt[0], acknak_idx); |
hjjeon | 1:8ef7820bf777 | 810 | |
hjjeon | 1:8ef7820bf777 | 811 | PPPPROTO.id = get_protocol_id; |
hjjeon | 1:8ef7820bf777 | 812 | //change MSB and LSB because of different endian. |
hjjeon | 1:8ef7820bf777 | 813 | PPPPROTO.len = 0; |
hjjeon | 1:8ef7820bf777 | 814 | PPPPROTO.len = ((acknak_idx+4) & 0xFF) << 8; |
hjjeon | 1:8ef7820bf777 | 815 | PPPPROTO.len |= (((acknak_idx+4) >> 8) & 0xFF); |
hjjeon | 1:8ef7820bf777 | 816 | //change MSB and LSB because of different endian. |
hjjeon | 1:8ef7820bf777 | 817 | PPPMSG_req.len = 0; |
hjjeon | 1:8ef7820bf777 | 818 | PPPMSG_req.len = ((acknak_idx+6) & 0xFF) << 8; |
hjjeon | 1:8ef7820bf777 | 819 | PPPMSG_req.len |= (((acknak_idx+6) >> 8) & 0xFF); |
hjjeon | 1:8ef7820bf777 | 820 | |
hjjeon | 1:8ef7820bf777 | 821 | ppp_send(); |
hjjeon | 1:8ef7820bf777 | 822 | if(ppp_code == 0x01) pppoe_control_flag = pppoe_control_flag | FLAG_IPCP_CR_RCV; |
hjjeon | 1:8ef7820bf777 | 823 | else pppoe_control_flag = pppoe_control_flag | FLAG_IPCP_NAK_RCV; |
hjjeon | 1:8ef7820bf777 | 824 | } |
hjjeon | 1:8ef7820bf777 | 825 | break; |
hjjeon | 1:8ef7820bf777 | 826 | |
hjjeon | 1:8ef7820bf777 | 827 | |
hjjeon | 1:8ef7820bf777 | 828 | case PPP_CONFIG_REJ : // Reject receive. |
hjjeon | 1:8ef7820bf777 | 829 | #ifdef __DEF_PPP_DBG1__ |
hjjeon | 1:8ef7820bf777 | 830 | printf("IPCP reject message received!!\r\n"); |
hjjeon | 1:8ef7820bf777 | 831 | #endif |
hjjeon | 1:8ef7820bf777 | 832 | |
hjjeon | 1:8ef7820bf777 | 833 | get_protocol_id = buf[23]; |
hjjeon | 1:8ef7820bf777 | 834 | getlen = buf[24]; |
hjjeon | 1:8ef7820bf777 | 835 | getlen = (getlen<<8) + buf[25]; |
hjjeon | 1:8ef7820bf777 | 836 | |
hjjeon | 1:8ef7820bf777 | 837 | getlen -= 4; |
hjjeon | 1:8ef7820bf777 | 838 | t_idx = 26; |
hjjeon | 1:8ef7820bf777 | 839 | |
hjjeon | 1:8ef7820bf777 | 840 | ip_rjt = 0; |
hjjeon | 1:8ef7820bf777 | 841 | pdns_rjt =0; |
hjjeon | 1:8ef7820bf777 | 842 | sdns_rjt = 0; |
hjjeon | 1:8ef7820bf777 | 843 | |
hjjeon | 1:8ef7820bf777 | 844 | while (getlen) |
hjjeon | 1:8ef7820bf777 | 845 | { |
hjjeon | 1:8ef7820bf777 | 846 | opt_code = buf[t_idx]; |
hjjeon | 1:8ef7820bf777 | 847 | opt_len = buf[t_idx+1]; |
hjjeon | 1:8ef7820bf777 | 848 | |
hjjeon | 1:8ef7820bf777 | 849 | switch (opt_code) |
hjjeon | 1:8ef7820bf777 | 850 | { |
hjjeon | 1:8ef7820bf777 | 851 | //case 0x02 : // type : ip compression protocol |
hjjeon | 1:8ef7820bf777 | 852 | case 0x03 : // ip address |
hjjeon | 1:8ef7820bf777 | 853 | ip_rjt = 1; |
hjjeon | 1:8ef7820bf777 | 854 | break; |
hjjeon | 1:8ef7820bf777 | 855 | case 0x81 : // PDNS |
hjjeon | 1:8ef7820bf777 | 856 | pdns_rjt = 1; |
hjjeon | 1:8ef7820bf777 | 857 | break; |
hjjeon | 1:8ef7820bf777 | 858 | case 0x83 : // SDNS |
hjjeon | 1:8ef7820bf777 | 859 | sdns_rjt = 1; |
hjjeon | 1:8ef7820bf777 | 860 | break; |
hjjeon | 1:8ef7820bf777 | 861 | default : |
hjjeon | 1:8ef7820bf777 | 862 | break; |
hjjeon | 1:8ef7820bf777 | 863 | } |
hjjeon | 1:8ef7820bf777 | 864 | t_idx += opt_len; |
hjjeon | 1:8ef7820bf777 | 865 | getlen -= opt_len; |
hjjeon | 1:8ef7820bf777 | 866 | } |
hjjeon | 1:8ef7820bf777 | 867 | |
hjjeon | 1:8ef7820bf777 | 868 | // Configuration send without reject protocol. |
hjjeon | 1:8ef7820bf777 | 869 | |
hjjeon | 1:8ef7820bf777 | 870 | //change MSB and LSB because of different endian. |
hjjeon | 1:8ef7820bf777 | 871 | PPPPROTO.protocol = 0; |
hjjeon | 1:8ef7820bf777 | 872 | PPPPROTO.protocol = (PPPoE_IPCP & 0xFF) << 8; |
hjjeon | 1:8ef7820bf777 | 873 | PPPPROTO.protocol |= ((PPPoE_IPCP >> 8) & 0xFF); |
hjjeon | 1:8ef7820bf777 | 874 | |
hjjeon | 1:8ef7820bf777 | 875 | PPPPROTO.pcode = PPP_CONFIG_REQ ; |
hjjeon | 1:8ef7820bf777 | 876 | t_idx = 0; |
hjjeon | 1:8ef7820bf777 | 877 | if( ip_rjt == 0 ) |
hjjeon | 1:8ef7820bf777 | 878 | { |
hjjeon | 1:8ef7820bf777 | 879 | PPPPROTO.opt[t_idx++] = 0x03; // option code, IP address |
hjjeon | 1:8ef7820bf777 | 880 | PPPPROTO.opt[t_idx++] = 0x06; // option len |
hjjeon | 1:8ef7820bf777 | 881 | PPPPROTO.opt[t_idx++] = 0x00; |
hjjeon | 1:8ef7820bf777 | 882 | PPPPROTO.opt[t_idx++] = 0x00; |
hjjeon | 1:8ef7820bf777 | 883 | PPPPROTO.opt[t_idx++] = 0x00; |
hjjeon | 1:8ef7820bf777 | 884 | PPPPROTO.opt[t_idx++] = 0x00; |
hjjeon | 1:8ef7820bf777 | 885 | } |
hjjeon | 1:8ef7820bf777 | 886 | |
hjjeon | 1:8ef7820bf777 | 887 | if( pdns_rjt == 0 ) |
hjjeon | 1:8ef7820bf777 | 888 | { |
hjjeon | 1:8ef7820bf777 | 889 | PPPPROTO.opt[t_idx++] = 0x81; // option code, PDNS address |
hjjeon | 1:8ef7820bf777 | 890 | PPPPROTO.opt[t_idx++] = 0x06; // option len |
hjjeon | 1:8ef7820bf777 | 891 | PPPPROTO.opt[t_idx++] = 0x00; |
hjjeon | 1:8ef7820bf777 | 892 | PPPPROTO.opt[t_idx++] = 0x00; |
hjjeon | 1:8ef7820bf777 | 893 | PPPPROTO.opt[t_idx++] = 0x00; |
hjjeon | 1:8ef7820bf777 | 894 | PPPPROTO.opt[t_idx++] = 0x00; |
hjjeon | 1:8ef7820bf777 | 895 | } |
hjjeon | 1:8ef7820bf777 | 896 | |
hjjeon | 1:8ef7820bf777 | 897 | if( sdns_rjt == 0 ) |
hjjeon | 1:8ef7820bf777 | 898 | { |
hjjeon | 1:8ef7820bf777 | 899 | PPPPROTO.opt[t_idx++] = 0x83; // option code, SDNS address |
hjjeon | 1:8ef7820bf777 | 900 | PPPPROTO.opt[t_idx++] = 0x06; // option len |
hjjeon | 1:8ef7820bf777 | 901 | PPPPROTO.opt[t_idx++] = 0x00; |
hjjeon | 1:8ef7820bf777 | 902 | PPPPROTO.opt[t_idx++] = 0x00; |
hjjeon | 1:8ef7820bf777 | 903 | PPPPROTO.opt[t_idx++] = 0x00; |
hjjeon | 1:8ef7820bf777 | 904 | PPPPROTO.opt[t_idx++] = 0x00; |
hjjeon | 1:8ef7820bf777 | 905 | } |
hjjeon | 1:8ef7820bf777 | 906 | |
hjjeon | 1:8ef7820bf777 | 907 | PPPPROTO.id = get_protocol_id; |
hjjeon | 1:8ef7820bf777 | 908 | //change MSB and LSB because of different endian. |
hjjeon | 1:8ef7820bf777 | 909 | PPPPROTO.len = 0; |
hjjeon | 1:8ef7820bf777 | 910 | PPPPROTO.len = ((t_idx+4) & 0xFF) << 8; |
hjjeon | 1:8ef7820bf777 | 911 | PPPPROTO.len |= (((t_idx+4) >> 8) & 0xFF); |
hjjeon | 1:8ef7820bf777 | 912 | //change MSB and LSB because of different endian. |
hjjeon | 1:8ef7820bf777 | 913 | PPPMSG_req.len = 0; |
hjjeon | 1:8ef7820bf777 | 914 | PPPMSG_req.len = ((t_idx+6) & 0xFF) << 8; |
hjjeon | 1:8ef7820bf777 | 915 | PPPMSG_req.len |= (((t_idx+6) >> 8) & 0xFF); |
hjjeon | 1:8ef7820bf777 | 916 | |
hjjeon | 1:8ef7820bf777 | 917 | if( ip_rjt && pdns_rjt && sdns_rjt) |
hjjeon | 1:8ef7820bf777 | 918 | { |
hjjeon | 1:8ef7820bf777 | 919 | #ifdef __DEF_PPP_DBG1__ |
hjjeon | 1:8ef7820bf777 | 920 | printf("IPCP session : All IPCP options are rejected!!\r\n"); |
hjjeon | 1:8ef7820bf777 | 921 | #endif |
hjjeon | 1:8ef7820bf777 | 922 | do_lcp_terminate(); |
hjjeon | 1:8ef7820bf777 | 923 | } |
hjjeon | 1:8ef7820bf777 | 924 | else |
hjjeon | 1:8ef7820bf777 | 925 | { |
hjjeon | 1:8ef7820bf777 | 926 | #ifdef __DEF_PPP_DBG1__ |
hjjeon | 1:8ef7820bf777 | 927 | printf("IPCP configuration request send without reject protocol\r\n"); |
hjjeon | 1:8ef7820bf777 | 928 | #endif |
hjjeon | 1:8ef7820bf777 | 929 | ppp_send(); |
hjjeon | 1:8ef7820bf777 | 930 | } |
hjjeon | 1:8ef7820bf777 | 931 | |
hjjeon | 1:8ef7820bf777 | 932 | |
hjjeon | 1:8ef7820bf777 | 933 | break; |
hjjeon | 1:8ef7820bf777 | 934 | case PPP_CONFIG_ACK : // ack, then ipcp_cr_snt flag set |
hjjeon | 1:8ef7820bf777 | 935 | #ifdef __DEF_PPP_DBG1__ |
hjjeon | 1:8ef7820bf777 | 936 | printf("PPPoE Session IPCP: Configure-Ack received\r\n"); |
hjjeon | 1:8ef7820bf777 | 937 | #endif |
hjjeon | 1:8ef7820bf777 | 938 | if((pppoe_control_flag & FLAG_IPCP_NAK_RCV) == FLAG_IPCP_NAK_RCV) pppoe_control_flag = pppoe_control_flag | FLAG_IPCP_CR_SNT; |
hjjeon | 1:8ef7820bf777 | 939 | break; |
hjjeon | 1:8ef7820bf777 | 940 | } |
hjjeon | 1:8ef7820bf777 | 941 | } |
hjjeon | 1:8ef7820bf777 | 942 | break; |
hjjeon | 1:8ef7820bf777 | 943 | default: |
hjjeon | 1:8ef7820bf777 | 944 | break; |
hjjeon | 1:8ef7820bf777 | 945 | } |
hjjeon | 1:8ef7820bf777 | 946 | } |
hjjeon | 1:8ef7820bf777 | 947 | } |
hjjeon | 1:8ef7820bf777 | 948 | |
hjjeon | 1:8ef7820bf777 | 949 | |
hjjeon | 1:8ef7820bf777 | 950 | |
hjjeon | 1:8ef7820bf777 | 951 | void PPPOEClient::do_discovery(void) |
hjjeon | 1:8ef7820bf777 | 952 | { |
hjjeon | 1:8ef7820bf777 | 953 | uint16_t i = 0; |
hjjeon | 1:8ef7820bf777 | 954 | uint8_t mac[6] = {0,}; |
hjjeon | 1:8ef7820bf777 | 955 | //getSHAR(mac); |
hjjeon | 1:8ef7820bf777 | 956 | eth->getSHAR(mac); |
hjjeon | 1:8ef7820bf777 | 957 | |
hjjeon | 1:8ef7820bf777 | 958 | // PPPoE Frame |
hjjeon | 1:8ef7820bf777 | 959 | for(i = 0; i < 6; i++) |
hjjeon | 1:8ef7820bf777 | 960 | { |
hjjeon | 1:8ef7820bf777 | 961 | PPPMSG_req.dst_mac[i] = 0xFF; // Broadcast MAC address |
hjjeon | 1:8ef7820bf777 | 962 | PPPMSG_req.src_mac[i] = mac[i]; // Source MAC address |
hjjeon | 1:8ef7820bf777 | 963 | //-- Opt. Device MAC Address |
hjjeon | 1:8ef7820bf777 | 964 | PPPPROTO.opt[10+i] = mac[i]; |
hjjeon | 1:8ef7820bf777 | 965 | } |
hjjeon | 1:8ef7820bf777 | 966 | //Reset control flag. |
hjjeon | 1:8ef7820bf777 | 967 | pppoe_control_flag = 0; |
hjjeon | 1:8ef7820bf777 | 968 | |
hjjeon | 1:8ef7820bf777 | 969 | //change MSB and LSB because of different endian. |
hjjeon | 1:8ef7820bf777 | 970 | PPPMSG_req.ether_type = 0; |
hjjeon | 1:8ef7820bf777 | 971 | PPPMSG_req.ether_type = (PPPoE_DISCOVERY & 0xFF) << 8;//session data |
hjjeon | 1:8ef7820bf777 | 972 | PPPMSG_req.ether_type |= ((PPPoE_DISCOVERY >> 8) & 0xFF);//session data |
hjjeon | 1:8ef7820bf777 | 973 | |
hjjeon | 1:8ef7820bf777 | 974 | PPPMSG_req.version_type = PPPoE_VER_TYPE; // Ver : 0001, Type : 0001 |
hjjeon | 1:8ef7820bf777 | 975 | PPPMSG_req.frame_code = PPPoE_PADI; // PADI |
hjjeon | 1:8ef7820bf777 | 976 | |
hjjeon | 1:8ef7820bf777 | 977 | PPPMSG_req.session_id = 0; |
hjjeon | 1:8ef7820bf777 | 978 | |
hjjeon | 1:8ef7820bf777 | 979 | |
hjjeon | 1:8ef7820bf777 | 980 | // Tag name : PPPoED_SERVICE_NAME |
hjjeon | 1:8ef7820bf777 | 981 | PPPPROTO.opt[0] = 0x01; |
hjjeon | 1:8ef7820bf777 | 982 | PPPPROTO.opt[1] = 0x01; |
hjjeon | 1:8ef7820bf777 | 983 | // Tag len |
hjjeon | 1:8ef7820bf777 | 984 | PPPPROTO.opt[2] = 0x00; |
hjjeon | 1:8ef7820bf777 | 985 | PPPPROTO.opt[3] = 0x00; |
hjjeon | 1:8ef7820bf777 | 986 | |
hjjeon | 1:8ef7820bf777 | 987 | // Tag name : PPPoED_HOST_UNIQ |
hjjeon | 1:8ef7820bf777 | 988 | PPPPROTO.opt[4] = 0x01; |
hjjeon | 1:8ef7820bf777 | 989 | PPPPROTO.opt[5] = 0x03; |
hjjeon | 1:8ef7820bf777 | 990 | // Tag len (2bytes) |
hjjeon | 1:8ef7820bf777 | 991 | PPPPROTO.opt[6] = 0x00; |
hjjeon | 1:8ef7820bf777 | 992 | PPPPROTO.opt[7] = 0x08; |
hjjeon | 1:8ef7820bf777 | 993 | // Fill the Host-Uniq field using MAC address |
hjjeon | 1:8ef7820bf777 | 994 | PPPPROTO.opt[8] = 0x00; |
hjjeon | 1:8ef7820bf777 | 995 | PPPPROTO.opt[9] = 0x00; |
hjjeon | 1:8ef7820bf777 | 996 | //PPPPROTO.opt[10~15] refer to "//-- Opt. Device MAC Address" |
hjjeon | 1:8ef7820bf777 | 997 | |
hjjeon | 1:8ef7820bf777 | 998 | //change MSB and LSB because of different endian. |
hjjeon | 1:8ef7820bf777 | 999 | PPPMSG_req.len = 0; |
hjjeon | 1:8ef7820bf777 | 1000 | PPPMSG_req.len = (16 & 0xFF) << 8; |
hjjeon | 1:8ef7820bf777 | 1001 | PPPMSG_req.len |= ((16 >> 8) & 0xFF);//size of opt[0-9]+ opt[10-15] |
hjjeon | 1:8ef7820bf777 | 1002 | |
hjjeon | 1:8ef7820bf777 | 1003 | #ifdef __DEF_PPP_DBG1__ |
hjjeon | 1:8ef7820bf777 | 1004 | printf("PPPoE Discovery : PADI send\r\n"); |
hjjeon | 1:8ef7820bf777 | 1005 | #endif |
hjjeon | 1:8ef7820bf777 | 1006 | ppp_send(); |
hjjeon | 1:8ef7820bf777 | 1007 | |
hjjeon | 1:8ef7820bf777 | 1008 | } |
hjjeon | 1:8ef7820bf777 | 1009 | |
hjjeon | 1:8ef7820bf777 | 1010 | void PPPOEClient::do_lcp(void) |
hjjeon | 1:8ef7820bf777 | 1011 | { |
hjjeon | 1:8ef7820bf777 | 1012 | |
hjjeon | 1:8ef7820bf777 | 1013 | //change MSB and LSB because of different endian. |
hjjeon | 1:8ef7820bf777 | 1014 | PPPMSG_req.ether_type = 0; |
hjjeon | 1:8ef7820bf777 | 1015 | PPPMSG_req.ether_type = (PPPoE_SESSION & 0xFF) << 8; |
hjjeon | 1:8ef7820bf777 | 1016 | PPPMSG_req.ether_type |= ((PPPoE_SESSION >> 8) & 0xFF);//session data |
hjjeon | 1:8ef7820bf777 | 1017 | |
hjjeon | 1:8ef7820bf777 | 1018 | PPPMSG_req.frame_code = 0x00; //session data |
hjjeon | 1:8ef7820bf777 | 1019 | //change MSB and LSB because of different endian. |
hjjeon | 1:8ef7820bf777 | 1020 | PPPMSG_req.session_id = 0; |
hjjeon | 1:8ef7820bf777 | 1021 | PPPMSG_req.session_id = (NAS_sessionid & 0xFF) << 8; |
hjjeon | 1:8ef7820bf777 | 1022 | PPPMSG_req.session_id |= ((NAS_sessionid >> 8) & 0xFF);//session data |
hjjeon | 1:8ef7820bf777 | 1023 | |
hjjeon | 1:8ef7820bf777 | 1024 | //change MSB and LSB because of different endian. |
hjjeon | 1:8ef7820bf777 | 1025 | PPPPROTO.protocol = 0; |
hjjeon | 1:8ef7820bf777 | 1026 | PPPPROTO.protocol = (PPPoE_LCP & 0xFF) << 8; |
hjjeon | 1:8ef7820bf777 | 1027 | PPPPROTO.protocol |= ((PPPoE_LCP >> 8) & 0xFF); |
hjjeon | 1:8ef7820bf777 | 1028 | |
hjjeon | 1:8ef7820bf777 | 1029 | |
hjjeon | 1:8ef7820bf777 | 1030 | PPPPROTO.pcode = PPP_CONFIG_REQ; // CR |
hjjeon | 1:8ef7820bf777 | 1031 | |
hjjeon | 1:8ef7820bf777 | 1032 | PPPPROTO.opt[0] = 0x05; |
hjjeon | 1:8ef7820bf777 | 1033 | PPPPROTO.opt[1] = 0x06; |
hjjeon | 1:8ef7820bf777 | 1034 | PPPPROTO.opt[2] = 0x00; |
hjjeon | 1:8ef7820bf777 | 1035 | PPPPROTO.opt[3] = 0x01; |
hjjeon | 1:8ef7820bf777 | 1036 | PPPPROTO.opt[4] = 0x02; |
hjjeon | 1:8ef7820bf777 | 1037 | PPPPROTO.opt[5] = 0x03; |
hjjeon | 1:8ef7820bf777 | 1038 | |
hjjeon | 1:8ef7820bf777 | 1039 | PPPPROTO.id = protocol_id++; |
hjjeon | 1:8ef7820bf777 | 1040 | //change MSB and LSB because of different endian. |
hjjeon | 1:8ef7820bf777 | 1041 | PPPPROTO.len = 0; |
hjjeon | 1:8ef7820bf777 | 1042 | PPPPROTO.len = (10 & 0xFF) << 8; |
hjjeon | 1:8ef7820bf777 | 1043 | PPPPROTO.len |= ((10 >> 8) & 0xFF); |
hjjeon | 1:8ef7820bf777 | 1044 | |
hjjeon | 1:8ef7820bf777 | 1045 | //change MSB and LSB because of different endian. |
hjjeon | 1:8ef7820bf777 | 1046 | PPPMSG_req.len = 0; |
hjjeon | 1:8ef7820bf777 | 1047 | PPPMSG_req.len = (12 & 0xFF) << 8; |
hjjeon | 1:8ef7820bf777 | 1048 | PPPMSG_req.len |= ((12 >> 8) & 0xFF); |
hjjeon | 1:8ef7820bf777 | 1049 | |
hjjeon | 1:8ef7820bf777 | 1050 | #ifdef __DEF_PPP_DBG1__ |
hjjeon | 1:8ef7820bf777 | 1051 | printf("LCP configuration Request send\r\n"); |
hjjeon | 1:8ef7820bf777 | 1052 | #endif |
hjjeon | 1:8ef7820bf777 | 1053 | ppp_send(); |
hjjeon | 1:8ef7820bf777 | 1054 | |
hjjeon | 1:8ef7820bf777 | 1055 | } |
hjjeon | 1:8ef7820bf777 | 1056 | |
hjjeon | 1:8ef7820bf777 | 1057 | void PPPOEClient::do_lcp_echo(void) |
hjjeon | 1:8ef7820bf777 | 1058 | { |
hjjeon | 1:8ef7820bf777 | 1059 | |
hjjeon | 1:8ef7820bf777 | 1060 | //change MSB and LSB because of different endian. |
hjjeon | 1:8ef7820bf777 | 1061 | PPPMSG_req.ether_type = 0; |
hjjeon | 1:8ef7820bf777 | 1062 | PPPMSG_req.ether_type = (PPPoE_SESSION & 0xFF) << 8; |
hjjeon | 1:8ef7820bf777 | 1063 | PPPMSG_req.ether_type |= ((PPPoE_SESSION >> 8) & 0xFF);// session |
hjjeon | 1:8ef7820bf777 | 1064 | |
hjjeon | 1:8ef7820bf777 | 1065 | PPPMSG_req.frame_code = 0x00; // session data |
hjjeon | 1:8ef7820bf777 | 1066 | //change MSB and LSB because of different endian. |
hjjeon | 1:8ef7820bf777 | 1067 | PPPMSG_req.session_id = 0; |
hjjeon | 1:8ef7820bf777 | 1068 | PPPMSG_req.session_id = (NAS_sessionid & 0xFF) << 8; |
hjjeon | 1:8ef7820bf777 | 1069 | PPPMSG_req.session_id |= ((NAS_sessionid >> 8) & 0xFF);// session id |
hjjeon | 1:8ef7820bf777 | 1070 | |
hjjeon | 1:8ef7820bf777 | 1071 | //change MSB and LSB because of different endian. |
hjjeon | 1:8ef7820bf777 | 1072 | PPPPROTO.protocol = 0; |
hjjeon | 1:8ef7820bf777 | 1073 | PPPPROTO.protocol = (PPPoE_LCP & 0xFF) << 8; |
hjjeon | 1:8ef7820bf777 | 1074 | PPPPROTO.protocol |= ((PPPoE_LCP >> 8) & 0xFF); |
hjjeon | 1:8ef7820bf777 | 1075 | |
hjjeon | 1:8ef7820bf777 | 1076 | PPPPROTO.pcode = PPP_ECHO_REQ; // Echo-Requset |
hjjeon | 1:8ef7820bf777 | 1077 | |
hjjeon | 1:8ef7820bf777 | 1078 | PPPPROTO.opt[0] = 0x00; // Magic number |
hjjeon | 1:8ef7820bf777 | 1079 | PPPPROTO.opt[1] = 0x01; |
hjjeon | 1:8ef7820bf777 | 1080 | PPPPROTO.opt[2] = 0x02; |
hjjeon | 1:8ef7820bf777 | 1081 | PPPPROTO.opt[3] = 0x03; |
hjjeon | 1:8ef7820bf777 | 1082 | |
hjjeon | 1:8ef7820bf777 | 1083 | PPPPROTO.id = protocol_id++; |
hjjeon | 1:8ef7820bf777 | 1084 | //change MSB and LSB because of different endian. |
hjjeon | 1:8ef7820bf777 | 1085 | PPPPROTO.len = 0; |
hjjeon | 1:8ef7820bf777 | 1086 | PPPPROTO.len = (8 & 0xFF) << 8; |
hjjeon | 1:8ef7820bf777 | 1087 | PPPPROTO.len |= ((8 >> 8) & 0xFF); |
hjjeon | 1:8ef7820bf777 | 1088 | //change MSB and LSB because of different endian. |
hjjeon | 1:8ef7820bf777 | 1089 | PPPMSG_req.len = 0; |
hjjeon | 1:8ef7820bf777 | 1090 | PPPMSG_req.len = (10 & 0xFF) << 8; |
hjjeon | 1:8ef7820bf777 | 1091 | PPPMSG_req.len |= ((10 >> 8) & 0xFF); |
hjjeon | 1:8ef7820bf777 | 1092 | #ifdef __DEF_PPP_DBG1__ |
hjjeon | 1:8ef7820bf777 | 1093 | printf("LCP echo request send\r\n"); |
hjjeon | 1:8ef7820bf777 | 1094 | #endif |
hjjeon | 1:8ef7820bf777 | 1095 | ppp_send(); |
hjjeon | 1:8ef7820bf777 | 1096 | |
hjjeon | 1:8ef7820bf777 | 1097 | } |
hjjeon | 1:8ef7820bf777 | 1098 | |
hjjeon | 1:8ef7820bf777 | 1099 | //return -- 1 : success, 0 : failed |
hjjeon | 1:8ef7820bf777 | 1100 | uint8_t PPPOEClient::do_lcp_terminate(void) |
hjjeon | 1:8ef7820bf777 | 1101 | { |
hjjeon | 1:8ef7820bf777 | 1102 | uint16_t received_len = 0; |
hjjeon | 1:8ef7820bf777 | 1103 | uint8_t sock_num = 0; |
hjjeon | 1:8ef7820bf777 | 1104 | |
hjjeon | 1:8ef7820bf777 | 1105 | |
hjjeon | 1:8ef7820bf777 | 1106 | pppoe_retry_count = 0; |
hjjeon | 1:8ef7820bf777 | 1107 | |
hjjeon | 1:8ef7820bf777 | 1108 | //while( (pppoe_control_flag & FLAG_TERMINATION_ACK_RCV) == 0 ) |
hjjeon | 1:8ef7820bf777 | 1109 | while(1) |
hjjeon | 1:8ef7820bf777 | 1110 | { |
hjjeon | 1:8ef7820bf777 | 1111 | //change MSB and LSB because of different endian. |
hjjeon | 1:8ef7820bf777 | 1112 | PPPMSG_req.ether_type = 0; |
hjjeon | 1:8ef7820bf777 | 1113 | PPPMSG_req.ether_type = (PPPoE_SESSION & 0xFF) << 8; |
hjjeon | 1:8ef7820bf777 | 1114 | PPPMSG_req.ether_type |= ((PPPoE_SESSION >> 8) & 0xFF);// session |
hjjeon | 1:8ef7820bf777 | 1115 | |
hjjeon | 1:8ef7820bf777 | 1116 | PPPMSG_req.frame_code = 0x00; // session data |
hjjeon | 1:8ef7820bf777 | 1117 | //change MSB and LSB because of different endian. |
hjjeon | 1:8ef7820bf777 | 1118 | PPPMSG_req.session_id = 0; |
hjjeon | 1:8ef7820bf777 | 1119 | PPPMSG_req.session_id = (NAS_sessionid & 0xFF) << 8; |
hjjeon | 1:8ef7820bf777 | 1120 | PPPMSG_req.session_id |= ((NAS_sessionid >> 8) & 0xFF); |
hjjeon | 1:8ef7820bf777 | 1121 | |
hjjeon | 1:8ef7820bf777 | 1122 | //change MSB and LSB because of different endian. |
hjjeon | 1:8ef7820bf777 | 1123 | PPPPROTO.protocol = 0; |
hjjeon | 1:8ef7820bf777 | 1124 | PPPPROTO.protocol = (PPPoE_LCP & 0xFF) << 8; |
hjjeon | 1:8ef7820bf777 | 1125 | PPPPROTO.protocol |= ((PPPoE_LCP >> 8) & 0xFF); |
hjjeon | 1:8ef7820bf777 | 1126 | |
hjjeon | 1:8ef7820bf777 | 1127 | PPPPROTO.pcode = PPP_TERM_REQ; // Terminate-Requset |
hjjeon | 1:8ef7820bf777 | 1128 | |
hjjeon | 1:8ef7820bf777 | 1129 | PPPPROTO.opt[0] = 0x00; // Magic number |
hjjeon | 1:8ef7820bf777 | 1130 | PPPPROTO.opt[1] = 0x01; |
hjjeon | 1:8ef7820bf777 | 1131 | PPPPROTO.opt[2] = 0x02; |
hjjeon | 1:8ef7820bf777 | 1132 | PPPPROTO.opt[3] = 0x03; |
hjjeon | 1:8ef7820bf777 | 1133 | |
hjjeon | 1:8ef7820bf777 | 1134 | PPPPROTO.id = protocol_id++; |
hjjeon | 1:8ef7820bf777 | 1135 | //change MSB and LSB because of different endian. |
hjjeon | 1:8ef7820bf777 | 1136 | PPPPROTO.len = 0; |
hjjeon | 1:8ef7820bf777 | 1137 | PPPPROTO.len = (8 & 0xFF) << 8; |
hjjeon | 1:8ef7820bf777 | 1138 | PPPPROTO.len |= ((8 >> 8) & 0xFF); |
hjjeon | 1:8ef7820bf777 | 1139 | |
hjjeon | 1:8ef7820bf777 | 1140 | //change MSB and LSB because of different endian. |
hjjeon | 1:8ef7820bf777 | 1141 | PPPMSG_req.len = 0; |
hjjeon | 1:8ef7820bf777 | 1142 | PPPMSG_req.len = (10 & 0xFF) << 8; |
hjjeon | 1:8ef7820bf777 | 1143 | PPPMSG_req.len |= ((10 >> 8) & 0xFF); |
hjjeon | 1:8ef7820bf777 | 1144 | |
hjjeon | 1:8ef7820bf777 | 1145 | |
hjjeon | 1:8ef7820bf777 | 1146 | #ifdef __DEF_PPP_DBG1__ |
hjjeon | 1:8ef7820bf777 | 1147 | printf("Termination Request send\r\n"); |
hjjeon | 1:8ef7820bf777 | 1148 | #endif |
hjjeon | 1:8ef7820bf777 | 1149 | ppp_send(); |
hjjeon | 1:8ef7820bf777 | 1150 | pppoe_retry_count++; |
hjjeon | 1:8ef7820bf777 | 1151 | |
hjjeon | 1:8ef7820bf777 | 1152 | |
hjjeon | 1:8ef7820bf777 | 1153 | if(pppoe_retry_count > PPP_MAX_RETRY_COUNT) |
hjjeon | 1:8ef7820bf777 | 1154 | { |
hjjeon | 1:8ef7820bf777 | 1155 | printf("Termination Failed\r\n"); |
hjjeon | 1:8ef7820bf777 | 1156 | return 0;//termination fail |
hjjeon | 1:8ef7820bf777 | 1157 | } |
hjjeon | 1:8ef7820bf777 | 1158 | |
hjjeon | 1:8ef7820bf777 | 1159 | |
hjjeon | 1:8ef7820bf777 | 1160 | pppoe_recv_count = 0; |
hjjeon | 1:8ef7820bf777 | 1161 | while((pppoe_control_flag & FLAG_TERMINATION_ACK_RCV) == 0 && pppoe_recv_count < PPP_MAX_RETRYRECV_COUNT) |
hjjeon | 1:8ef7820bf777 | 1162 | { |
hjjeon | 1:8ef7820bf777 | 1163 | wait(0.4); |
hjjeon | 1:8ef7820bf777 | 1164 | pppoe_recv_count ++; |
hjjeon | 1:8ef7820bf777 | 1165 | |
hjjeon | 1:8ef7820bf777 | 1166 | |
hjjeon | 1:8ef7820bf777 | 1167 | received_len = eth->getSn_RX_RSR(sock_num); |
hjjeon | 1:8ef7820bf777 | 1168 | |
hjjeon | 1:8ef7820bf777 | 1169 | if(received_len > 0) |
hjjeon | 1:8ef7820bf777 | 1170 | { |
hjjeon | 1:8ef7820bf777 | 1171 | ppp_recv(received_len); |
hjjeon | 1:8ef7820bf777 | 1172 | if((pppoe_control_flag & FLAG_TERMINATION_ACK_RCV) == FLAG_TERMINATION_ACK_RCV) // Termination success |
hjjeon | 1:8ef7820bf777 | 1173 | { |
hjjeon | 1:8ef7820bf777 | 1174 | pppoe_state = PPPoE_FAILED; |
hjjeon | 1:8ef7820bf777 | 1175 | |
hjjeon | 1:8ef7820bf777 | 1176 | // Flags reset |
hjjeon | 1:8ef7820bf777 | 1177 | pppoe_control_flag = 0; |
hjjeon | 1:8ef7820bf777 | 1178 | // Clear Session ID for new PPPoE Discovery process |
hjjeon | 1:8ef7820bf777 | 1179 | NAS_sessionid = 0; |
hjjeon | 1:8ef7820bf777 | 1180 | |
hjjeon | 1:8ef7820bf777 | 1181 | NAS_mac[0] = 0; |
hjjeon | 1:8ef7820bf777 | 1182 | NAS_mac[1] = 0; |
hjjeon | 1:8ef7820bf777 | 1183 | NAS_mac[2] = 0; |
hjjeon | 1:8ef7820bf777 | 1184 | NAS_mac[3] = 0; |
hjjeon | 1:8ef7820bf777 | 1185 | NAS_mac[4] = 0; |
hjjeon | 1:8ef7820bf777 | 1186 | NAS_mac[5] = 0; |
hjjeon | 1:8ef7820bf777 | 1187 | |
hjjeon | 1:8ef7820bf777 | 1188 | pppoe_ip[0] = 0; |
hjjeon | 1:8ef7820bf777 | 1189 | pppoe_ip[1] = 0; |
hjjeon | 1:8ef7820bf777 | 1190 | pppoe_ip[2] = 0; |
hjjeon | 1:8ef7820bf777 | 1191 | pppoe_ip[3] = 0; |
hjjeon | 1:8ef7820bf777 | 1192 | |
hjjeon | 1:8ef7820bf777 | 1193 | printf("Termination completed\r\n"); |
hjjeon | 1:8ef7820bf777 | 1194 | return 1;//termination success |
hjjeon | 1:8ef7820bf777 | 1195 | } |
hjjeon | 1:8ef7820bf777 | 1196 | } |
hjjeon | 1:8ef7820bf777 | 1197 | } |
hjjeon | 1:8ef7820bf777 | 1198 | |
hjjeon | 1:8ef7820bf777 | 1199 | } |
hjjeon | 1:8ef7820bf777 | 1200 | |
hjjeon | 1:8ef7820bf777 | 1201 | } |
hjjeon | 1:8ef7820bf777 | 1202 | |
hjjeon | 1:8ef7820bf777 | 1203 | |
hjjeon | 1:8ef7820bf777 | 1204 | void PPPOEClient::do_pap(void) |
hjjeon | 1:8ef7820bf777 | 1205 | { |
hjjeon | 1:8ef7820bf777 | 1206 | |
hjjeon | 1:8ef7820bf777 | 1207 | uint16_t i=0, j=0; |
hjjeon | 1:8ef7820bf777 | 1208 | |
hjjeon | 1:8ef7820bf777 | 1209 | //change MSB and LSB because of different endian. |
hjjeon | 1:8ef7820bf777 | 1210 | PPPPROTO.protocol = 0; |
hjjeon | 1:8ef7820bf777 | 1211 | PPPPROTO.protocol = (PPPoE_PAP & 0xFF) << 8; |
hjjeon | 1:8ef7820bf777 | 1212 | PPPPROTO.protocol |= ((PPPoE_PAP >> 8) & 0xFF); |
hjjeon | 1:8ef7820bf777 | 1213 | |
hjjeon | 1:8ef7820bf777 | 1214 | PPPPROTO.pcode = PPP_CONFIG_REQ; // cr |
hjjeon | 1:8ef7820bf777 | 1215 | PPPPROTO.opt[i++] = pppoe_id_len; |
hjjeon | 1:8ef7820bf777 | 1216 | |
hjjeon | 1:8ef7820bf777 | 1217 | for(j = 0; j < pppoe_id_len; j++) |
hjjeon | 1:8ef7820bf777 | 1218 | { |
hjjeon | 1:8ef7820bf777 | 1219 | PPPPROTO.opt[i++] = pppoe_id[j]; |
hjjeon | 1:8ef7820bf777 | 1220 | } |
hjjeon | 1:8ef7820bf777 | 1221 | |
hjjeon | 1:8ef7820bf777 | 1222 | PPPPROTO.opt[i++] = pppoe_pw_len; |
hjjeon | 1:8ef7820bf777 | 1223 | for(j = 0; j < pppoe_pw_len; j++) |
hjjeon | 1:8ef7820bf777 | 1224 | { |
hjjeon | 1:8ef7820bf777 | 1225 | PPPPROTO.opt[i++] = pppoe_pw[j]; |
hjjeon | 1:8ef7820bf777 | 1226 | } |
hjjeon | 1:8ef7820bf777 | 1227 | |
hjjeon | 1:8ef7820bf777 | 1228 | PPPPROTO.id = protocol_id++; |
hjjeon | 1:8ef7820bf777 | 1229 | //change MSB and LSB because of different endian. |
hjjeon | 1:8ef7820bf777 | 1230 | PPPPROTO.len = 0; |
hjjeon | 1:8ef7820bf777 | 1231 | PPPPROTO.len = ((i+4) & 0xFF) << 8; |
hjjeon | 1:8ef7820bf777 | 1232 | PPPPROTO.len |= (((i+4) >> 8) & 0xFF); |
hjjeon | 1:8ef7820bf777 | 1233 | //change MSB and LSB because of different endian. |
hjjeon | 1:8ef7820bf777 | 1234 | PPPMSG_req.len = 0; |
hjjeon | 1:8ef7820bf777 | 1235 | PPPMSG_req.len = (((i+4)+2) & 0xFF) << 8; |
hjjeon | 1:8ef7820bf777 | 1236 | PPPMSG_req.len |= ((((i+4)+2) >> 8) & 0xFF); |
hjjeon | 1:8ef7820bf777 | 1237 | |
hjjeon | 1:8ef7820bf777 | 1238 | #ifdef __DEF_PPP_DBG1__ |
hjjeon | 1:8ef7820bf777 | 1239 | printf("PAP authenticate-Request send\r\n"); |
hjjeon | 1:8ef7820bf777 | 1240 | #endif |
hjjeon | 1:8ef7820bf777 | 1241 | |
hjjeon | 1:8ef7820bf777 | 1242 | ppp_send(); |
hjjeon | 1:8ef7820bf777 | 1243 | |
hjjeon | 1:8ef7820bf777 | 1244 | } |
hjjeon | 1:8ef7820bf777 | 1245 | |
hjjeon | 1:8ef7820bf777 | 1246 | void PPPOEClient::do_ipcp(void) |
hjjeon | 1:8ef7820bf777 | 1247 | { |
hjjeon | 1:8ef7820bf777 | 1248 | |
hjjeon | 1:8ef7820bf777 | 1249 | //change MSB and LSB because of different endian. |
hjjeon | 1:8ef7820bf777 | 1250 | PPPPROTO.protocol = 0; |
hjjeon | 1:8ef7820bf777 | 1251 | PPPPROTO.protocol = (PPPoE_IPCP & 0xFF) << 8; |
hjjeon | 1:8ef7820bf777 | 1252 | PPPPROTO.protocol |= ((PPPoE_IPCP >> 8) & 0xFF); |
hjjeon | 1:8ef7820bf777 | 1253 | |
hjjeon | 1:8ef7820bf777 | 1254 | PPPPROTO.pcode = PPP_CONFIG_REQ; // configuration-req |
hjjeon | 1:8ef7820bf777 | 1255 | |
hjjeon | 1:8ef7820bf777 | 1256 | PPPPROTO.opt[0] = 0x03; // option code, IP address |
hjjeon | 1:8ef7820bf777 | 1257 | PPPPROTO.opt[1] = 0x06; // option len |
hjjeon | 1:8ef7820bf777 | 1258 | PPPPROTO.opt[2] = 0x00; |
hjjeon | 1:8ef7820bf777 | 1259 | PPPPROTO.opt[3] = 0x00; |
hjjeon | 1:8ef7820bf777 | 1260 | PPPPROTO.opt[4] = 0x00; |
hjjeon | 1:8ef7820bf777 | 1261 | PPPPROTO.opt[5] = 0x00; |
hjjeon | 1:8ef7820bf777 | 1262 | //Option PDNS |
hjjeon | 1:8ef7820bf777 | 1263 | PPPPROTO.opt[6] = 0x81; //op code, PDNS |
hjjeon | 1:8ef7820bf777 | 1264 | PPPPROTO.opt[7] = 0x06; //option len |
hjjeon | 1:8ef7820bf777 | 1265 | PPPPROTO.opt[8] = 0x00; |
hjjeon | 1:8ef7820bf777 | 1266 | PPPPROTO.opt[9] = 0x00; |
hjjeon | 1:8ef7820bf777 | 1267 | PPPPROTO.opt[10] = 0x00; |
hjjeon | 1:8ef7820bf777 | 1268 | PPPPROTO.opt[11] = 0x00; |
hjjeon | 1:8ef7820bf777 | 1269 | //Option SDNS |
hjjeon | 1:8ef7820bf777 | 1270 | PPPPROTO.opt[12] = 0x83; //op code, SDNS |
hjjeon | 1:8ef7820bf777 | 1271 | PPPPROTO.opt[13] = 0x06; //option len |
hjjeon | 1:8ef7820bf777 | 1272 | PPPPROTO.opt[14] = 0x00; |
hjjeon | 1:8ef7820bf777 | 1273 | PPPPROTO.opt[15] = 0x00; |
hjjeon | 1:8ef7820bf777 | 1274 | PPPPROTO.opt[16] = 0x00; |
hjjeon | 1:8ef7820bf777 | 1275 | PPPPROTO.opt[17] = 0x00; |
hjjeon | 1:8ef7820bf777 | 1276 | |
hjjeon | 1:8ef7820bf777 | 1277 | |
hjjeon | 1:8ef7820bf777 | 1278 | PPPPROTO.id = protocol_id++; |
hjjeon | 1:8ef7820bf777 | 1279 | |
hjjeon | 1:8ef7820bf777 | 1280 | //change MSB and LSB because of different endian. |
hjjeon | 1:8ef7820bf777 | 1281 | /* |
hjjeon | 1:8ef7820bf777 | 1282 | PPPPROTO.len = 0; |
hjjeon | 1:8ef7820bf777 | 1283 | PPPPROTO.len = (10 & 0xFF) << 8; |
hjjeon | 1:8ef7820bf777 | 1284 | PPPPROTO.len |= ((10 >> 8) & 0xFF); |
hjjeon | 1:8ef7820bf777 | 1285 | |
hjjeon | 1:8ef7820bf777 | 1286 | //change MSB and LSB because of different endian. |
hjjeon | 1:8ef7820bf777 | 1287 | PPPMSG_req.len = 0; |
hjjeon | 1:8ef7820bf777 | 1288 | PPPMSG_req.len = (12 & 0xFF) << 8; |
hjjeon | 1:8ef7820bf777 | 1289 | PPPMSG_req.len |= ((12 >> 8) & 0xFF); |
hjjeon | 1:8ef7820bf777 | 1290 | */ |
hjjeon | 1:8ef7820bf777 | 1291 | PPPPROTO.len = 0; |
hjjeon | 1:8ef7820bf777 | 1292 | PPPPROTO.len = (22 & 0xFF) << 8; |
hjjeon | 1:8ef7820bf777 | 1293 | PPPPROTO.len |= ((22 >> 8) & 0xFF); |
hjjeon | 1:8ef7820bf777 | 1294 | |
hjjeon | 1:8ef7820bf777 | 1295 | //change MSB and LSB because of different endian. |
hjjeon | 1:8ef7820bf777 | 1296 | PPPMSG_req.len = 0; |
hjjeon | 1:8ef7820bf777 | 1297 | PPPMSG_req.len = (24 & 0xFF) << 8; |
hjjeon | 1:8ef7820bf777 | 1298 | PPPMSG_req.len |= ((24 >> 8) & 0xFF); |
hjjeon | 1:8ef7820bf777 | 1299 | #ifdef __DEF_PPP_DBG1__ |
hjjeon | 1:8ef7820bf777 | 1300 | printf("IPCP Configuration request send\r\n"); |
hjjeon | 1:8ef7820bf777 | 1301 | #endif |
hjjeon | 1:8ef7820bf777 | 1302 | |
hjjeon | 1:8ef7820bf777 | 1303 | ppp_send(); |
hjjeon | 1:8ef7820bf777 | 1304 | |
hjjeon | 1:8ef7820bf777 | 1305 | } |
hjjeon | 1:8ef7820bf777 | 1306 | |
hjjeon | 1:8ef7820bf777 | 1307 | |
hjjeon | 1:8ef7820bf777 | 1308 | |
hjjeon | 1:8ef7820bf777 | 1309 | // PPPoE start |
hjjeon | 1:8ef7820bf777 | 1310 | //ret = Success : 1, Fail : 0, continue : 2 |
hjjeon | 1:8ef7820bf777 | 1311 | uint8_t PPPOEClient::ppp_start(uint8_t * pppoe_buf) |
hjjeon | 1:8ef7820bf777 | 1312 | { |
hjjeon | 1:8ef7820bf777 | 1313 | uint8_t sock_num; |
hjjeon | 1:8ef7820bf777 | 1314 | uint8_t mFlag; |
hjjeon | 1:8ef7820bf777 | 1315 | uint16_t dummyPort; |
hjjeon | 1:8ef7820bf777 | 1316 | uint8_t ret = 2; |
hjjeon | 1:8ef7820bf777 | 1317 | uint16_t received_len = 0; |
hjjeon | 1:8ef7820bf777 | 1318 | |
hjjeon | 1:8ef7820bf777 | 1319 | buf = pppoe_buf; |
hjjeon | 1:8ef7820bf777 | 1320 | //-- Init. param |
hjjeon | 1:8ef7820bf777 | 1321 | sock_num = 0; |
hjjeon | 1:8ef7820bf777 | 1322 | dummyPort = 0; |
hjjeon | 1:8ef7820bf777 | 1323 | mFlag = 0x80; //MAC filter enable in MACRAW |
hjjeon | 1:8ef7820bf777 | 1324 | |
hjjeon | 1:8ef7820bf777 | 1325 | |
hjjeon | 1:8ef7820bf777 | 1326 | switch(eth->getSn_SR(sock_num)) |
hjjeon | 1:8ef7820bf777 | 1327 | { |
hjjeon | 1:8ef7820bf777 | 1328 | case SOCK_CLOSED: |
hjjeon | 1:8ef7820bf777 | 1329 | eth->close(sock_num); // Close the SOCKET |
hjjeon | 1:8ef7820bf777 | 1330 | Socket_macraw(sock_num, dummyPort, mFlag); // Open the SOCKET with MACRAW mode |
hjjeon | 1:8ef7820bf777 | 1331 | #ifdef __DEF_PPP_DBG1__ |
hjjeon | 1:8ef7820bf777 | 1332 | printf("No.%d socket is opened with MACRAW and flag is 0x%2x\r\n", sock_num, mFlag); |
hjjeon | 1:8ef7820bf777 | 1333 | #endif |
hjjeon | 1:8ef7820bf777 | 1334 | #ifdef __DEF_PPP_DBG__ |
hjjeon | 1:8ef7820bf777 | 1335 | printf("\r\n=======================================\r\n"); |
hjjeon | 1:8ef7820bf777 | 1336 | printf("PHASE 0. PPPoE setup\r\n"); |
hjjeon | 1:8ef7820bf777 | 1337 | printf("=======================================\r\n"); |
hjjeon | 1:8ef7820bf777 | 1338 | #endif |
hjjeon | 1:8ef7820bf777 | 1339 | break; |
hjjeon | 1:8ef7820bf777 | 1340 | |
hjjeon | 1:8ef7820bf777 | 1341 | case SOCK_MACRAW: |
hjjeon | 1:8ef7820bf777 | 1342 | |
hjjeon | 1:8ef7820bf777 | 1343 | if(pppoe_retry_send_count > PPP_MAX_RETRYSEND_COUNT) pppoe_state = PPPoE_FAILED; |
hjjeon | 1:8ef7820bf777 | 1344 | |
hjjeon | 1:8ef7820bf777 | 1345 | switch(pppoe_state) |
hjjeon | 1:8ef7820bf777 | 1346 | { |
hjjeon | 1:8ef7820bf777 | 1347 | case PPPoE_DISCOVERY : // Discovery |
hjjeon | 1:8ef7820bf777 | 1348 | // PPPoE Discoveryecv( |
hjjeon | 1:8ef7820bf777 | 1349 | //if((pppoe_control_flag & FLAG_DISCOVERY_RCV_PADO) == 0) |
hjjeon | 1:8ef7820bf777 | 1350 | if((pppoe_control_flag & FLAG_DISCOVERY_RCV_PADO) == 0 || (pppoe_control_flag & FLAG_DISCOVERY_RCV_PADS) == 0) //Not recv PADO or PADS |
hjjeon | 1:8ef7820bf777 | 1351 | { |
hjjeon | 1:8ef7820bf777 | 1352 | #ifdef __DEF_PPP_DBG__ |
hjjeon | 1:8ef7820bf777 | 1353 | printf("\r\n=======================================\r\n"); |
hjjeon | 1:8ef7820bf777 | 1354 | printf("PHASE 1. PPPoE Discovery\r\n"); |
hjjeon | 1:8ef7820bf777 | 1355 | printf("=======================================\r\n"); |
hjjeon | 1:8ef7820bf777 | 1356 | #endif |
hjjeon | 1:8ef7820bf777 | 1357 | #ifdef __DEF_PPP_DBG1__ |
hjjeon | 1:8ef7820bf777 | 1358 | printf("Retry send count : %d\r\n", pppoe_retry_send_count); |
hjjeon | 1:8ef7820bf777 | 1359 | #endif |
hjjeon | 1:8ef7820bf777 | 1360 | do_discovery(); // Send PADI |
hjjeon | 1:8ef7820bf777 | 1361 | pppoe_retry_send_count++; |
hjjeon | 1:8ef7820bf777 | 1362 | } |
hjjeon | 1:8ef7820bf777 | 1363 | |
hjjeon | 1:8ef7820bf777 | 1364 | pppoe_recv_count = 0; |
hjjeon | 1:8ef7820bf777 | 1365 | while((pppoe_control_flag & FLAG_DISCOVERY_RCV_PADS) == 0 && pppoe_recv_count < (PPP_MAX_RETRYRECV_COUNT) * 2) |
hjjeon | 1:8ef7820bf777 | 1366 | { |
hjjeon | 1:8ef7820bf777 | 1367 | wait(0.2); |
hjjeon | 1:8ef7820bf777 | 1368 | pppoe_recv_count ++; |
hjjeon | 1:8ef7820bf777 | 1369 | |
hjjeon | 1:8ef7820bf777 | 1370 | received_len = eth->getSn_RX_RSR(sock_num); |
hjjeon | 1:8ef7820bf777 | 1371 | |
hjjeon | 1:8ef7820bf777 | 1372 | if(received_len > 0) |
hjjeon | 1:8ef7820bf777 | 1373 | { |
hjjeon | 1:8ef7820bf777 | 1374 | ppp_recv(received_len); |
hjjeon | 1:8ef7820bf777 | 1375 | |
hjjeon | 1:8ef7820bf777 | 1376 | |
hjjeon | 1:8ef7820bf777 | 1377 | if((pppoe_control_flag & FLAG_DISCOVERY_RCV_PADS) == FLAG_DISCOVERY_RCV_PADS)// Discovery success |
hjjeon | 1:8ef7820bf777 | 1378 | { |
hjjeon | 1:8ef7820bf777 | 1379 | pppoe_state = PPPoE_LCP; |
hjjeon | 1:8ef7820bf777 | 1380 | pppoe_retry_send_count = 0;//reset |
hjjeon | 1:8ef7820bf777 | 1381 | } |
hjjeon | 1:8ef7820bf777 | 1382 | } |
hjjeon | 1:8ef7820bf777 | 1383 | } |
hjjeon | 1:8ef7820bf777 | 1384 | |
hjjeon | 1:8ef7820bf777 | 1385 | break; |
hjjeon | 1:8ef7820bf777 | 1386 | |
hjjeon | 1:8ef7820bf777 | 1387 | case PPPoE_LCP : // LCP |
hjjeon | 1:8ef7820bf777 | 1388 | if((pppoe_control_flag & FLAG_LCP_CR_RCV) == 0) |
hjjeon | 1:8ef7820bf777 | 1389 | { |
hjjeon | 1:8ef7820bf777 | 1390 | #ifdef __DEF_PPP_DBG__ |
hjjeon | 1:8ef7820bf777 | 1391 | printf("\r\n=======================================\r\n"); |
hjjeon | 1:8ef7820bf777 | 1392 | printf("PHASE 2. PPPoE LCP\r\n"); |
hjjeon | 1:8ef7820bf777 | 1393 | printf("=======================================\r\n"); |
hjjeon | 1:8ef7820bf777 | 1394 | #endif |
hjjeon | 1:8ef7820bf777 | 1395 | do_lcp_echo(); |
hjjeon | 1:8ef7820bf777 | 1396 | |
hjjeon | 1:8ef7820bf777 | 1397 | #ifdef __DEF_PPP_DBG1__ |
hjjeon | 1:8ef7820bf777 | 1398 | printf("Retry send count : %d\r\n", pppoe_retry_send_count); |
hjjeon | 1:8ef7820bf777 | 1399 | #endif |
hjjeon | 1:8ef7820bf777 | 1400 | pppoe_retry_send_count++; |
hjjeon | 1:8ef7820bf777 | 1401 | } |
hjjeon | 1:8ef7820bf777 | 1402 | |
hjjeon | 1:8ef7820bf777 | 1403 | pppoe_recv_count = 0; |
hjjeon | 1:8ef7820bf777 | 1404 | while((pppoe_control_flag & FLAG_LCP_CR_RCV) == 0 && pppoe_recv_count < PPP_MAX_RETRYRECV_COUNT) |
hjjeon | 1:8ef7820bf777 | 1405 | { |
hjjeon | 1:8ef7820bf777 | 1406 | wait(0.2); |
hjjeon | 1:8ef7820bf777 | 1407 | pppoe_recv_count++; |
hjjeon | 1:8ef7820bf777 | 1408 | |
hjjeon | 1:8ef7820bf777 | 1409 | received_len = eth->getSn_RX_RSR(sock_num); |
hjjeon | 1:8ef7820bf777 | 1410 | if(received_len > 0) |
hjjeon | 1:8ef7820bf777 | 1411 | { |
hjjeon | 1:8ef7820bf777 | 1412 | ppp_recv(received_len); |
hjjeon | 1:8ef7820bf777 | 1413 | if((pppoe_control_flag & FLAG_LCP_CR_RCV) == FLAG_LCP_CR_RCV) pppoe_retry_send_count = 0;//reset |
hjjeon | 1:8ef7820bf777 | 1414 | } |
hjjeon | 1:8ef7820bf777 | 1415 | } |
hjjeon | 1:8ef7820bf777 | 1416 | |
hjjeon | 1:8ef7820bf777 | 1417 | |
hjjeon | 1:8ef7820bf777 | 1418 | if((pppoe_control_flag & FLAG_LCP_CR_RCV) == FLAG_LCP_CR_RCV) |
hjjeon | 1:8ef7820bf777 | 1419 | { |
hjjeon | 1:8ef7820bf777 | 1420 | do_lcp(); |
hjjeon | 1:8ef7820bf777 | 1421 | |
hjjeon | 1:8ef7820bf777 | 1422 | #ifdef __DEF_PPP_DBG1__ |
hjjeon | 1:8ef7820bf777 | 1423 | printf("Retry send count : %d\r\n", pppoe_retry_send_count); |
hjjeon | 1:8ef7820bf777 | 1424 | #endif |
hjjeon | 1:8ef7820bf777 | 1425 | pppoe_retry_send_count++; |
hjjeon | 1:8ef7820bf777 | 1426 | |
hjjeon | 1:8ef7820bf777 | 1427 | pppoe_recv_count = 0; |
hjjeon | 1:8ef7820bf777 | 1428 | while((pppoe_control_flag & FLAG_LCP_CR_SNT) == 0 && pppoe_recv_count < PPP_MAX_RETRYRECV_COUNT) |
hjjeon | 1:8ef7820bf777 | 1429 | { |
hjjeon | 1:8ef7820bf777 | 1430 | wait(0.2); |
hjjeon | 1:8ef7820bf777 | 1431 | pppoe_recv_count++; |
hjjeon | 1:8ef7820bf777 | 1432 | |
hjjeon | 1:8ef7820bf777 | 1433 | received_len = eth->getSn_RX_RSR(sock_num); |
hjjeon | 1:8ef7820bf777 | 1434 | if(received_len > 0) |
hjjeon | 1:8ef7820bf777 | 1435 | { |
hjjeon | 1:8ef7820bf777 | 1436 | ppp_recv(received_len); |
hjjeon | 1:8ef7820bf777 | 1437 | if((pppoe_control_flag & FLAG_LCP_CR_SNT) == FLAG_LCP_CR_SNT) |
hjjeon | 1:8ef7820bf777 | 1438 | { |
hjjeon | 1:8ef7820bf777 | 1439 | // PAP |
hjjeon | 1:8ef7820bf777 | 1440 | if(auth_protocol == PPPoE_PAP) |
hjjeon | 1:8ef7820bf777 | 1441 | { |
hjjeon | 1:8ef7820bf777 | 1442 | pppoe_state = PPPoE_PAP; |
hjjeon | 1:8ef7820bf777 | 1443 | pppoe_retry_send_count = 0;//reset |
hjjeon | 1:8ef7820bf777 | 1444 | } |
hjjeon | 1:8ef7820bf777 | 1445 | // CHAP |
hjjeon | 1:8ef7820bf777 | 1446 | else if(auth_protocol == PPPoE_CHAP) |
hjjeon | 1:8ef7820bf777 | 1447 | { |
hjjeon | 1:8ef7820bf777 | 1448 | pppoe_state = PPPoE_CHAP; |
hjjeon | 1:8ef7820bf777 | 1449 | pppoe_retry_send_count = 0;//reset |
hjjeon | 1:8ef7820bf777 | 1450 | } |
hjjeon | 1:8ef7820bf777 | 1451 | // unknown auth protocol |
hjjeon | 1:8ef7820bf777 | 1452 | else |
hjjeon | 1:8ef7820bf777 | 1453 | { |
hjjeon | 1:8ef7820bf777 | 1454 | #ifdef __DEF_PPP_DBG1__ |
hjjeon | 1:8ef7820bf777 | 1455 | printf("\r\nError : Unknown authentication protocol"); |
hjjeon | 1:8ef7820bf777 | 1456 | #endif |
hjjeon | 1:8ef7820bf777 | 1457 | pppoe_state = PPPoE_FAILED; |
hjjeon | 1:8ef7820bf777 | 1458 | } |
hjjeon | 1:8ef7820bf777 | 1459 | |
hjjeon | 1:8ef7820bf777 | 1460 | } |
hjjeon | 1:8ef7820bf777 | 1461 | } |
hjjeon | 1:8ef7820bf777 | 1462 | } |
hjjeon | 1:8ef7820bf777 | 1463 | } |
hjjeon | 1:8ef7820bf777 | 1464 | break; |
hjjeon | 1:8ef7820bf777 | 1465 | |
hjjeon | 1:8ef7820bf777 | 1466 | case PPPoE_PAP : // PAP |
hjjeon | 1:8ef7820bf777 | 1467 | #ifdef __DEF_PPP_DBG__ |
hjjeon | 1:8ef7820bf777 | 1468 | printf("\r\n=======================================\r\n"); |
hjjeon | 1:8ef7820bf777 | 1469 | printf("PHASE 3. PPPoE PAP\r\n"); |
hjjeon | 1:8ef7820bf777 | 1470 | printf("=======================================\r\n"); |
hjjeon | 1:8ef7820bf777 | 1471 | #endif |
hjjeon | 1:8ef7820bf777 | 1472 | |
hjjeon | 1:8ef7820bf777 | 1473 | |
hjjeon | 1:8ef7820bf777 | 1474 | if((pppoe_control_flag & FLAG_PAP_ACK_RCV) == 0 ) |
hjjeon | 1:8ef7820bf777 | 1475 | { |
hjjeon | 1:8ef7820bf777 | 1476 | do_pap(); |
hjjeon | 1:8ef7820bf777 | 1477 | pppoe_retry_send_count++; |
hjjeon | 1:8ef7820bf777 | 1478 | #ifdef __DEF_PPP_DBG1__ |
hjjeon | 1:8ef7820bf777 | 1479 | printf("Retry send count : %d\r\n", pppoe_retry_send_count ); |
hjjeon | 1:8ef7820bf777 | 1480 | #endif |
hjjeon | 1:8ef7820bf777 | 1481 | } |
hjjeon | 1:8ef7820bf777 | 1482 | |
hjjeon | 1:8ef7820bf777 | 1483 | pppoe_recv_count = 0; |
hjjeon | 1:8ef7820bf777 | 1484 | while((pppoe_control_flag & FLAG_PAP_ACK_RCV) == 0 && pppoe_recv_count < PPP_MAX_RETRYRECV_COUNT) |
hjjeon | 1:8ef7820bf777 | 1485 | { |
hjjeon | 1:8ef7820bf777 | 1486 | wait(0.4); |
hjjeon | 1:8ef7820bf777 | 1487 | pppoe_recv_count++; |
hjjeon | 1:8ef7820bf777 | 1488 | |
hjjeon | 1:8ef7820bf777 | 1489 | received_len = eth->getSn_RX_RSR(sock_num); |
hjjeon | 1:8ef7820bf777 | 1490 | if(received_len > 0) |
hjjeon | 1:8ef7820bf777 | 1491 | { |
hjjeon | 1:8ef7820bf777 | 1492 | ppp_recv(received_len); |
hjjeon | 1:8ef7820bf777 | 1493 | |
hjjeon | 1:8ef7820bf777 | 1494 | if((pppoe_control_flag & FLAG_PAP_ACK_RCV) == FLAG_PAP_ACK_RCV)// pap auth success |
hjjeon | 1:8ef7820bf777 | 1495 | { |
hjjeon | 1:8ef7820bf777 | 1496 | pppoe_state = PPPoE_IPCP; |
hjjeon | 1:8ef7820bf777 | 1497 | pppoe_retry_send_count = 0;//reset |
hjjeon | 1:8ef7820bf777 | 1498 | } |
hjjeon | 1:8ef7820bf777 | 1499 | } |
hjjeon | 1:8ef7820bf777 | 1500 | } |
hjjeon | 1:8ef7820bf777 | 1501 | break; |
hjjeon | 1:8ef7820bf777 | 1502 | |
hjjeon | 1:8ef7820bf777 | 1503 | case PPPoE_CHAP : // CHAP |
hjjeon | 1:8ef7820bf777 | 1504 | #ifdef __DEF_PPP_DBG__ |
hjjeon | 1:8ef7820bf777 | 1505 | printf("\r\n=======================================\r\n"); |
hjjeon | 1:8ef7820bf777 | 1506 | printf("PHASE 3. PPPoE CHAP\r\n"); |
hjjeon | 1:8ef7820bf777 | 1507 | printf("=======================================\r\n"); |
hjjeon | 1:8ef7820bf777 | 1508 | #endif |
hjjeon | 1:8ef7820bf777 | 1509 | |
hjjeon | 1:8ef7820bf777 | 1510 | |
hjjeon | 1:8ef7820bf777 | 1511 | pppoe_recv_count = 0; |
hjjeon | 1:8ef7820bf777 | 1512 | while((pppoe_control_flag & FLAG_CHAP_SUC_RCV) == 0 && pppoe_recv_count < 50) |
hjjeon | 1:8ef7820bf777 | 1513 | { |
hjjeon | 1:8ef7820bf777 | 1514 | wait(0.4); |
hjjeon | 1:8ef7820bf777 | 1515 | pppoe_recv_count++; |
hjjeon | 1:8ef7820bf777 | 1516 | |
hjjeon | 1:8ef7820bf777 | 1517 | printf("test\r\n"); |
hjjeon | 1:8ef7820bf777 | 1518 | |
hjjeon | 1:8ef7820bf777 | 1519 | received_len = eth->getSn_RX_RSR(sock_num); |
hjjeon | 1:8ef7820bf777 | 1520 | if(received_len > 0) |
hjjeon | 1:8ef7820bf777 | 1521 | { |
hjjeon | 1:8ef7820bf777 | 1522 | ppp_recv(received_len); |
hjjeon | 1:8ef7820bf777 | 1523 | |
hjjeon | 1:8ef7820bf777 | 1524 | if((pppoe_control_flag & FLAG_CHAP_SUC_RCV) == FLAG_CHAP_SUC_RCV) |
hjjeon | 1:8ef7820bf777 | 1525 | { |
hjjeon | 1:8ef7820bf777 | 1526 | pppoe_state = PPPoE_IPCP; |
hjjeon | 1:8ef7820bf777 | 1527 | } |
hjjeon | 1:8ef7820bf777 | 1528 | |
hjjeon | 1:8ef7820bf777 | 1529 | if((pppoe_control_flag & FLAG_TERMINATION_REQ_RCV) == FLAG_TERMINATION_REQ_RCV) |
hjjeon | 1:8ef7820bf777 | 1530 | { |
hjjeon | 1:8ef7820bf777 | 1531 | pppoe_state = PPPoE_FAILED; |
hjjeon | 1:8ef7820bf777 | 1532 | |
hjjeon | 1:8ef7820bf777 | 1533 | break; |
hjjeon | 1:8ef7820bf777 | 1534 | } |
hjjeon | 1:8ef7820bf777 | 1535 | } |
hjjeon | 1:8ef7820bf777 | 1536 | } |
hjjeon | 1:8ef7820bf777 | 1537 | |
hjjeon | 1:8ef7820bf777 | 1538 | if( pppoe_recv_count >= 50) |
hjjeon | 1:8ef7820bf777 | 1539 | { |
hjjeon | 1:8ef7820bf777 | 1540 | if(do_lcp_terminate() == 0) //if termination is failed. |
hjjeon | 1:8ef7820bf777 | 1541 | return 0; |
hjjeon | 1:8ef7820bf777 | 1542 | } |
hjjeon | 1:8ef7820bf777 | 1543 | |
hjjeon | 1:8ef7820bf777 | 1544 | break; |
hjjeon | 1:8ef7820bf777 | 1545 | |
hjjeon | 1:8ef7820bf777 | 1546 | case PPPoE_IPCP : // IPCP |
hjjeon | 1:8ef7820bf777 | 1547 | #ifdef __DEF_PPP_DBG__ |
hjjeon | 1:8ef7820bf777 | 1548 | printf("\r\n=======================================\r\n"); |
hjjeon | 1:8ef7820bf777 | 1549 | printf("PHASE 4. PPPoE IPCP\r\n"); |
hjjeon | 1:8ef7820bf777 | 1550 | printf("=======================================\r\n"); |
hjjeon | 1:8ef7820bf777 | 1551 | #endif |
hjjeon | 1:8ef7820bf777 | 1552 | |
hjjeon | 1:8ef7820bf777 | 1553 | pppoe_recv_count = 0; |
hjjeon | 1:8ef7820bf777 | 1554 | while((pppoe_control_flag & FLAG_IPCP_CR_RCV) == 0 && pppoe_recv_count < PPP_MAX_RETRYRECV_COUNT) |
hjjeon | 1:8ef7820bf777 | 1555 | { |
hjjeon | 1:8ef7820bf777 | 1556 | wait(1); |
hjjeon | 1:8ef7820bf777 | 1557 | pppoe_recv_count++; |
hjjeon | 1:8ef7820bf777 | 1558 | |
hjjeon | 1:8ef7820bf777 | 1559 | received_len = eth->getSn_RX_RSR(sock_num); |
hjjeon | 1:8ef7820bf777 | 1560 | if(received_len > 0) |
hjjeon | 1:8ef7820bf777 | 1561 | { |
hjjeon | 1:8ef7820bf777 | 1562 | ppp_recv(received_len); |
hjjeon | 1:8ef7820bf777 | 1563 | |
hjjeon | 1:8ef7820bf777 | 1564 | if((pppoe_control_flag & FLAG_IPCP_CR_RCV) == FLAG_IPCP_CR_RCV) pppoe_retry_send_count = 0;//reset |
hjjeon | 1:8ef7820bf777 | 1565 | |
hjjeon | 1:8ef7820bf777 | 1566 | } |
hjjeon | 1:8ef7820bf777 | 1567 | |
hjjeon | 1:8ef7820bf777 | 1568 | } |
hjjeon | 1:8ef7820bf777 | 1569 | // After received IPCP Configuration-Request and sent ACK, |
hjjeon | 1:8ef7820bf777 | 1570 | // User device have to send IPCP Configuration-Request and receive ACK. |
hjjeon | 1:8ef7820bf777 | 1571 | if((pppoe_control_flag & FLAG_IPCP_CR_RCV) == FLAG_IPCP_CR_RCV) |
hjjeon | 1:8ef7820bf777 | 1572 | { |
hjjeon | 1:8ef7820bf777 | 1573 | do_ipcp(); |
hjjeon | 1:8ef7820bf777 | 1574 | pppoe_retry_send_count++; |
hjjeon | 1:8ef7820bf777 | 1575 | |
hjjeon | 1:8ef7820bf777 | 1576 | pppoe_recv_count = 0; |
hjjeon | 1:8ef7820bf777 | 1577 | while((pppoe_control_flag & FLAG_IPCP_CR_SNT) == 0 && pppoe_recv_count < PPP_MAX_RETRYRECV_COUNT) |
hjjeon | 1:8ef7820bf777 | 1578 | { |
hjjeon | 1:8ef7820bf777 | 1579 | wait(1); |
hjjeon | 1:8ef7820bf777 | 1580 | pppoe_recv_count++; |
hjjeon | 1:8ef7820bf777 | 1581 | |
hjjeon | 1:8ef7820bf777 | 1582 | received_len = eth->getSn_RX_RSR(sock_num); |
hjjeon | 1:8ef7820bf777 | 1583 | if(received_len > 0) |
hjjeon | 1:8ef7820bf777 | 1584 | { |
hjjeon | 1:8ef7820bf777 | 1585 | ppp_recv(received_len); |
hjjeon | 1:8ef7820bf777 | 1586 | |
hjjeon | 1:8ef7820bf777 | 1587 | if((pppoe_control_flag & FLAG_IPCP_CR_SNT) == FLAG_IPCP_CR_SNT) |
hjjeon | 1:8ef7820bf777 | 1588 | { |
hjjeon | 1:8ef7820bf777 | 1589 | pppoe_retry_send_count = 0;//reset |
hjjeon | 1:8ef7820bf777 | 1590 | #ifdef __DEF_PPP_DBG__ |
hjjeon | 1:8ef7820bf777 | 1591 | printf("\r\n=======================================\r\n"); |
hjjeon | 1:8ef7820bf777 | 1592 | printf("PHASE 5. PPPoE Socket open\r\n"); |
hjjeon | 1:8ef7820bf777 | 1593 | printf("=======================================\r\n"); |
hjjeon | 1:8ef7820bf777 | 1594 | #endif |
hjjeon | 1:8ef7820bf777 | 1595 | set_pppinfo(NAS_mac, pppoe_ip, NAS_sessionid); |
hjjeon | 1:8ef7820bf777 | 1596 | ret = PPP_SUCCESS; |
hjjeon | 1:8ef7820bf777 | 1597 | } |
hjjeon | 1:8ef7820bf777 | 1598 | else if( (pppoe_control_flag & FLAG_TERMINATION_ACK_RCV) == FLAG_TERMINATION_ACK_RCV) |
hjjeon | 1:8ef7820bf777 | 1599 | { |
hjjeon | 1:8ef7820bf777 | 1600 | return 0; |
hjjeon | 1:8ef7820bf777 | 1601 | } |
hjjeon | 1:8ef7820bf777 | 1602 | else if( (pppoe_control_flag & FLAG_TERMINATION_REQ_RCV) == FLAG_TERMINATION_REQ_RCV) |
hjjeon | 1:8ef7820bf777 | 1603 | { |
hjjeon | 1:8ef7820bf777 | 1604 | return 0; |
hjjeon | 1:8ef7820bf777 | 1605 | } |
hjjeon | 1:8ef7820bf777 | 1606 | } |
hjjeon | 1:8ef7820bf777 | 1607 | } |
hjjeon | 1:8ef7820bf777 | 1608 | } |
hjjeon | 1:8ef7820bf777 | 1609 | break; |
hjjeon | 1:8ef7820bf777 | 1610 | |
hjjeon | 1:8ef7820bf777 | 1611 | case PPPoE_FAILED : |
hjjeon | 1:8ef7820bf777 | 1612 | pppoe_retry_count++; |
hjjeon | 1:8ef7820bf777 | 1613 | #ifdef __DEF_PPP_DBG__ |
hjjeon | 1:8ef7820bf777 | 1614 | printf("\r\nPPPoE FAILED !!!\r\n"); |
hjjeon | 1:8ef7820bf777 | 1615 | #endif |
hjjeon | 1:8ef7820bf777 | 1616 | #ifdef __DEF_PPP_DBG1__ |
hjjeon | 1:8ef7820bf777 | 1617 | printf("Retry count : %d\r\n", pppoe_retry_count); |
hjjeon | 1:8ef7820bf777 | 1618 | #endif |
hjjeon | 1:8ef7820bf777 | 1619 | // All flags reset |
hjjeon | 1:8ef7820bf777 | 1620 | pppoe_control_flag = 0; |
hjjeon | 1:8ef7820bf777 | 1621 | |
hjjeon | 1:8ef7820bf777 | 1622 | // Clear Session ID for new PPPoE Discovery process |
hjjeon | 1:8ef7820bf777 | 1623 | //NAS_sessionid = 0; |
hjjeon | 1:8ef7820bf777 | 1624 | |
hjjeon | 1:8ef7820bf777 | 1625 | pppoe_retry_send_count = 0; |
hjjeon | 1:8ef7820bf777 | 1626 | pppoe_state = PPPoE_DISCOVERY; |
hjjeon | 1:8ef7820bf777 | 1627 | |
hjjeon | 1:8ef7820bf777 | 1628 | break; |
hjjeon | 1:8ef7820bf777 | 1629 | |
hjjeon | 1:8ef7820bf777 | 1630 | |
hjjeon | 1:8ef7820bf777 | 1631 | default : |
hjjeon | 1:8ef7820bf777 | 1632 | #ifdef __DEF_PPP_DBG__ |
hjjeon | 1:8ef7820bf777 | 1633 | printf("\r\nUndefined state!\r\n"); |
hjjeon | 1:8ef7820bf777 | 1634 | #endif |
hjjeon | 1:8ef7820bf777 | 1635 | pppoe_state = PPPoE_FAILED; |
hjjeon | 1:8ef7820bf777 | 1636 | break; |
hjjeon | 1:8ef7820bf777 | 1637 | } |
hjjeon | 1:8ef7820bf777 | 1638 | |
hjjeon | 1:8ef7820bf777 | 1639 | // PPPoE packet send count over : connection terminate |
hjjeon | 1:8ef7820bf777 | 1640 | if(pppoe_retry_send_count >= PPP_MAX_RETRYSEND_COUNT) pppoe_state = PPPoE_FAILED; |
hjjeon | 1:8ef7820bf777 | 1641 | break; |
hjjeon | 1:8ef7820bf777 | 1642 | |
hjjeon | 1:8ef7820bf777 | 1643 | default: |
hjjeon | 1:8ef7820bf777 | 1644 | break; |
hjjeon | 1:8ef7820bf777 | 1645 | } |
hjjeon | 1:8ef7820bf777 | 1646 | return ret; |
hjjeon | 1:8ef7820bf777 | 1647 | } |
hjjeon | 1:8ef7820bf777 | 1648 | /* |
hjjeon | 1:8ef7820bf777 | 1649 | void PPPOEClient::delay_ms(uint32_t time) |
hjjeon | 1:8ef7820bf777 | 1650 | { |
hjjeon | 1:8ef7820bf777 | 1651 | uint32_t i; |
hjjeon | 1:8ef7820bf777 | 1652 | |
hjjeon | 1:8ef7820bf777 | 1653 | for(i=0; i<time; i++) |
hjjeon | 1:8ef7820bf777 | 1654 | { |
hjjeon | 1:8ef7820bf777 | 1655 | xSysCtlDelay((xSysCtlClockGet()/1000)); // wait 1ms |
hjjeon | 1:8ef7820bf777 | 1656 | } |
hjjeon | 1:8ef7820bf777 | 1657 | } |
hjjeon | 1:8ef7820bf777 | 1658 | */ |