PPPoE Application Note in MACRAW mode Version 1.0.0 2014 WIZnet Co., Ltd. All Rights Reserved. For more information, visit our website at http://www.wiznet.co.kr W7100A Application Note PPPoE in MACRAW mode v1.0.0
Table of Contents 1 Introduction... 3 2 Implementation... 5 3 Connection Process... 7 3.1 Socket 0 Open in MACRAW mode... 8 3.2 PPPoE Discovery Process... 9 3.3 PPP LCP Configuration Process... 11 3.4 PPP Authentication Process... 15 3.4.1 PAP (Password Authentication Protocol)... 15 3.4.2 CHAP (Challenge-Handshake Authentication Protocol)... 17 3.5 PPP IPCP Configuration Process... 20 3.6 PPPoE Configuration Setting Process... 24 4 Demonstration... 25 Document History Information... 26 Application Note PPPoE in MACRAW mode v1.0.0 2
1 Introduction WIZnet TCP/IP devices는 MACRAW 모드에서구현된 PPP/PPPoE Protocol을지원한다. PPP Protocol은 ISP(Internet Service Provider) 에서제공하는 Network Access Server(NAS) 에 pointto-point 연결을설정하고 IP data packet을전달하는 Link-layer protocol이다. PPP/PPPoE의대표적인이용예로는 ADSL이있으며, ADSL은전화선망을이용해서데이터통신을할수있게하는통신수단으로광범위한서비스에서사용되고있다. Figure 1. PPPoE (ADSL 의이용예 ) 본 Application note 에서는펌웨어상에서 MACRAW 모드를이용하여구현된 PPPoE 프로그 램의프로토콜의구성과인터넷에연결되기까지의과정을단계별로의사코드 (pseudo code) 를이용하여설명한다. MACRAW 모드는 Ethernet MAC 을기반으로그상위 Protocol 을 Host 가목적에맞도록유연 하게사용할수있게하는통신방법이다. MACRAW 모드에대한좀더자세한내용은 W7100A application note 중 How to implement MACRAW for W7100A 를참조하기바란다 구현된 PPPoE protocol 은 Figure 2 와같이동작한다. Figure 2. Simple flow of PPPoE with MACRAW mode Application Note PPPoE in MACRAW mode v1.0.0 3
먼저 MACRAW 모드로소켓을오픈한뒤 PPPoE Connection Process를수행한다. PPPoE Connection Process에서는단말기와 NAS가 Discovery, LCP, Authentication(PAP, CHAP), IPCP의각프로토콜에해당하는메시지를교환하게되며, 이를통해 NAS에서할당된 IP Address를 WIZnet TCP/IP device에설정하여동작하게함으로써 point-to-point 연결이설정된다. PPPoE 연결에서수행되는 protocol의동작내용은 3. Connection Process에서자세히다룬다. Application Note PPPoE in MACRAW mode v1.0.0 4
2 Implementation PPPoE Connection Process 의메시지교환과정은다음 Figure 3 과같이구현되어있다. Rx, Tx 버퍼는프로토콜데이터패킷의송, 수신을위해이용하는논리적메모리공간으로, 실제구현에서는배열을선언하여사용하였다. Figure 3. PPPoE Simple Implementation Diagram Application Note PPPoE in MACRAW mode v1.0.0 5
// PPPoE Start function uint8_t ppp_start(uint8_t * pppoe_buf); Table 1. MACRAW mode PPPoE Functions list // PPPoE Discovery function void do_discovery(void); // PPPoE protocol message generate functions void do_lcp(void); void do_lcp_echo(void); uint8_t do_lcp_terminate(void); void do_pap(void); void do_pap(void); // PPPoE protocol message send function void ppp_send(void); // PPPoE Packet check and response send function void ppp_recv( uint16_t received_len ); // Write Server MAC address and session ID void set_pppinfo(uint8_t * nas_mac, uint8_t * ppp_ip, uint16_t nas_sessionid); // PPPoE Delay function void delay_ms(uint32_t time); Application Note PPPoE in MACRAW mode v1.0.0 6
3 Connection Process MACRAW 모드를이용하여 PPPoE 연결을수행하기위해다음과같은과정을거치게된다. Figure 4. PPPoE Connection Process with MACRAW mode Phase 0. MAC PPPoE 연결및통신을위한기본적인환경설정을수행한다. Phase 1. PPPoE Discovery Process 연결을개시하기위해 PPPoE Server(NAS) 와연결을수행한다. Phase 2. PPP LCP Configuration Process NAS 와의협상을통해 PPPoE 연결을위한기본사항들을결정한다. Phase 3. PPP Authentication Process Authentication protocol 인 PAP 나 CHAP 를사용하여사용자인증을수행한다. Phase 4. PPP IPCP Configuration Process IP protocol 에서사용할 IP, Gateway, DNS address 등의주소를획득한다. Phase 5. PPPoE Configuration Setting Process PPPoE 모드로 Socket 을 open 하고목적지 IP, MAC address, Session ID 를 WIZnet TCP/IP device 에기록하며 Timeout setting 을수행한다. Application Note PPPoE in MACRAW mode v1.0.0 7
3.1 Socket 0 Open in MACRAW mode 단말기의 PPPoE 연결및통신을위한기본적인환경설정을수행한다. 이 Application note 에서구현하는 PPPoE 는 MACRAW 모드를사용하므로, MACRAW 모드로소켓 0 번을 open 한다. /* PPPoE Setup */ Table 2. Socket OPEN with MACRAW mode #define Sn_MR_MACRAW 0x04 sock_num = 0; // The SOCKET use only the SOCKET0. dummyport = 0; // The source port for the socket, not used port number. mflag = 0x80; // MAC filter enable in MACRAW /* OPEN SOCKET0 with MACRAW mode */ Switch(getSn_SR(sock_num) Case SOCK_CLOSED : close(sock_num); socket(sock_num, Sn_MR_MACRAW, dummyport, mflag); Case SOCK_MACRAW : Application Note PPPoE in MACRAW mode v1.0.0 8
3.2 PPPoE Discovery Process 단말기의연결을개시하기위해 PPPoE Server(NAS) 와연결을수행한다. - 연결할 NAS의 MAC address를획득 - NAS로부터통신에서사용될 Session ID를획득 Figure 5. PPPoE Discovery Process Table 3. PPPoE Discovery /* PPPoE Discovery */ pppoe_state = PPPoE_DISCOVERY; /* PPPoE Discovery : ppp_start() */ do_discovery(); // Send PADI massage using broadcast pppoe_retry_send_count++; while(! FLAG_DISCOVERY_RCV_PADS )// Not receive PADS received len = getsn_rx_rsr(sock_num) // Received packet length if( received_len > 0 ) // If packet received ppp_recv(received_len);// Receive packet and Parse process Application Note PPPoE in MACRAW mode v1.0.0 9
if( FLAG_DISCOVERY_RCV_PADS ) pppoe_state = PPPoE_LCP; // Go to the next phase: PPPoE_LCP /* PPPoE Discovery : ppp_recv(received_len) */ Case PPPoE_DISCOVERY : If( PPPoE_PADO ) // PADO massage received while( taglen ) // If tag length > 0 switch( tagname ) // Process Tags and making PADR massage case SERVICE_NAME : case HOST_UNIQ : case AC_NAME: case AC_COOKIE : // Making PADR massage taglen =- ppp_tag_len; // Length of all tags length of each tag ppp_send(); // Send PADR massage using unicast else if( PPPoE_PADS ) // PADS massage received // Session ID is used to whole connection process after PPPoE discovery process. NAS_sessionid = received NAS session ID; // Save Session ID from NAS pppoe_control_flag = pppoe_control_flag FLAG_DISCOVERY_RCV_PADS;// Received PADS indicate flag Break; Application Note PPPoE in MACRAW mode v1.0.0 10
3.3 PPP LCP Configuration Process NAS와의협상을통해 PPPoE 연결을위한기본사항들을결정한다. LCP 옵션항목을이용하여다음과같은사항을서로요청 (Config-Request) 하고응답 (Config- ACK) 하며, 각요청과응답은동일한 Magic Number를이용하여야한다. - MRU (Maximum Receive Unit) - Authentication Protocol (PAP, CHAP 등 ) <Notice> 제공하는예제펌웨어소스코드의수신 Packet을 Parsing하는 ppp_recv(received_len); 함수에는모든옵션이구현되어있는것이아니며, 기본적인 PPPoE 구현을위해필요한최소의옵션들만구현되어있다. 만약기본적으로구현된옵션외에추가옵션이요구될경우, 구현된기본옵션과 RFC에정의된해당프로토콜의옵션리스트를참고하여필요에따라구현하기바란다. 이에대한부분은예제코드상에 notice로표시되어있다. Figure 6. PPP LCP Configuration Process Application Note PPPoE in MACRAW mode v1.0.0 11
Table 4. PPP LCP Configuration /* PPP LCP Configuration : ppp_start() */ // Received packet length do_lcp_echo();// Send LCP Echo-Request pppoe_retry_send_count++; while(! FLAG_LCP_CR_RCV) received_len = getsn_rx_rsr(sock_num); if( received_len > 0 ) ppp_recv(received_len); // Receive packet and Parse process if (FLAG_LCP_CR_RCV ) do_lcp();// Send LCP Configuration-Request while(! FLAG_LCP_CR_SNT ) received_len = getsn_rx_rsr(sock_num); if( received_len > 0 ) ppp_recv(received_len); // Receive packet and Parse process if( FLAG_LCP_CR_SNT ) // Authentication protocol : PAP, Go to the next phase: PPPoE_PAP If( auth_protocol == PPPoE_PAP ) pppoe_state = PPPoE_PAP; // Authentication protocol : CHAP, Go to the next phase: PPPoE_CHAP else if( auth_protocol == PPPoE_CHAP ) pppoe_state = PPPoE_CHAP; // Unknown Authentication protocol, Go to the failed state: PPPoE_FAILED else pppoe_state = PPPoE_FAILED; Application Note PPPoE in MACRAW mode v1.0.0 12
/* PPP LCP Configuration : ppp_recv(received_len) */ Case PPPoE_SESSION : If( PPPoE_LCP ) Switch( codename ) Case CONFIG_REQ : getlen = all option length; While( getlen ) opt_code = option code; opt_len = option length; Switch( opt_code ) Case LCP_MRU : Case LCP_AUTH : Case LCP_MAGICNUM : // Parsing and making Config-Ack massage Default : // Making Config-Reject massage // and rej_idx += opt_len; getlen -= opt_len; // Send Response message for Request message from NAS If( rjt_idx ) // if any option is rejected, send reject message and then wait Config-Request // Making Config-Reject massage and send ppp_send(); else // Send Config-Ack, lcp_cr_rcv flag set // Making Config-Ack massage and send ppp_send(); Break; Application Note PPPoE in MACRAW mode v1.0.0 13
Case CONFIG_ACK : // ack, then lcp_cr_sent flag set FLAG_LCP_CR_SNT = 1; // Set flag Break; /* Notice : This part is not implemented. */ /* If necessary, please implement more for reply for request from NAS. */ /* case CONFIG_REJ : //reject case ECHO_REQ : // Echo-Request */ Default : Break; Break; Application Note PPPoE in MACRAW mode v1.0.0 14
3.4 PPP Authentication Process PPPoE 연결에서사용자인증을처리하기위한과정이다. 어떤사용자인증방법을이용할지에대해서는 Ch. 2.4 LCP Configuration Protocol에서결정된다. 현재구현된 PPPoE 프로그램에서는 Authentication protocol로 PAP와 CHAP를지원하며사용자가필요한경우추가적인인증방법을구현하여삽입하면된다. 3.4.1 PAP (Password Authentication Protocol) PAP는 NAS로사용자의 ID와 Password만보내면 NAS가확인하여 Ack( 올바른사용자 ) / Nak( 올바르지않은사용자 ) 만사용자에게전송하는간단한인증방식이다. 사용자가 Ack를수신하면인증은성공적으로이루어지고, 다음단계로넘어가게된다. Figure 7. PAP Authentication Process Table 5. PAP Authentication /* PPP PAP Authentication */ /* PPP PAP Authentication : ppp_start() */ do_pap();// Send PAP Authentication-Request pppoe_retry_send_count++; // Received packet length While(! FLAG_PAP_ACK_RCV ) received len = getsn_rx_rsr(sock_num) // Received packet length Application Note PPPoE in MACRAW mode v1.0.0 15
if( received_len > 0 ) // If packet received ppp_recv(received_len); // Receive packet and Parse process if( FLAG_PAP_ACK_RCV ) pppoe_state = PPPoE_IPCP; // Go to the next phase: PPPoE_IPCP /* PPP PAP Authentication : ppp_recv(received_len) */ Case PPPoE_SESSION : If( PPPoE_PAP ) If( codename == CONFIG_ACK ) FLAG_PAP_ACK_RCV = 1; // Set PAP Ack receive flag Break; Application Note PPPoE in MACRAW mode v1.0.0 16
3.4.2 CHAP (Challenge-Handshake Authentication Protocol) 또다른인증프로토콜인 CHAP는패스워드가직접전달되지않고, 암호화되어전송되기때문에 PAP보다보안성이높은특징을갖고있다. 기본적인 CHAP의인증절차는다음 Figure 7의 CHAP Authentication Process와같이 3-Way Handshaking 절차를수행한다. 1. NAS(PPPoE Server) 는랜덤한값인 Challenge Value(CV) 를포함한 CHAP-Challenge 패킷을단말로송신한다. 2. CHAP-Challenge 메시지를수신한단말은 CV 값과자신의 password, 그리고순서번호인 ID 값을이용하여 Message Digest 5(MD5) 방식으로 Hashed Value(HV) 를생성하고, 이 HV를담은 CHAP-Response 패킷을 NAS로전달한다. 3. 패킷을수신한인증서버는자신이생성했던 CV 값과자신의계정테이블에저장된사용자 ID와패킷일련번호를이용한 HV 값을생성하여수신된 HV와비교한다. 4. 만약 HV와 HV 값이일치한다면해당사용자를유효한사용자로판단하고 NAS는단말로 CHAP-Success 메시지를보내응답하며, 그렇지않다면 CHAP Fail 메시지로응답한다. Figure 8. CHAP Authentication Process Application Note PPPoE in MACRAW mode v1.0.0 17
Table 6. CHAP Authentication /* PPP CHAP Authentication */ /* PPP CHAP Authentication : ppp_start() */ // Received packet length While(! FLAG_CHAP_SUC_RCV ) received len = getsn_rx_rsr(sock_num) // Received packet length if( received_len > 0 ) // If packet received ppp_recv(received_len); // Receive packet and Parse process if( FLAG_CHAP_SUC_RCV ) pppoe_state = PPPoE_IPCP; // Go to the next phase: PPPoE_IPCP /* PPP CHAP Authentication : ppp_recv(received_len) */ Case PPPoE_SESSION : If( PPPoE_CHAP ) Switch( chap_algorithm ) Case MD5 : // 0x05, using MD5 algorithm Switch( codename ) Case 0x01 : // CHAP-Challenge // MD5 Calculation CV and send CHAP-Response to NAS ppp_send(); Case 0x03 : // CHAP-Success FLAG_CHAP_SUC_RCV = 1; Case 0x04 : // CHAP-Failed Default : /* Notice : This part is not implemented. */ /* If necessary, please implement more for the other CHAP algorithm */ Application Note PPPoE in MACRAW mode v1.0.0 18
/* Case MS_CHAP : // 0x80 Case MS_CHAP_V2 // 0x81 */ Default : Break; Application Note PPPoE in MACRAW mode v1.0.0 19
3.5 PPP IPCP Configuration Process IP protocol 에서사용할 IP, Gateway, DNS address 등의주소를획득한다. <Notice> 제공하는예제펌웨어소스코드의수신 Packet을 Parsing하는 ppp_recv(received_len); 함수에는모든옵션이구현되어있는것이아니며, 기본적인 PPPoE 구현을위해필요한최소의옵션들만구현되어있다. 만약기본적으로구현된옵션외에추가옵션이요구될경우, 구현된기본옵션과 RFC에정의된해당프로토콜의옵션리스트를참고하여필요에따라구현하기바란다. 이에대한부분은예제코드상에 notice로표시되어있다. Figure 9. PPP IPCP Configuration Process Application Note PPPoE in MACRAW mode v1.0.0 20
Table 7. PPP IPCP Configuration /* PPP IPCP Configuration */ /* PPP IPCP Configuration : ppp_start() */ // Received packet length While(! FLAG_IPCP_CR_RCV ) received len = getsn_rx_rsr(sock_num) // Received packet length if( received_len > 0 ) // If packet received ppp_recv(received_len); // Receive packet and Parse process if( FLAG_IPCP_CR_RCV ) pppoe_state = PPPoE_IPCP; if( FLAG_IPCP_CR_RCV ) do_ipcp(); While(! FLAG_IPCP_CR_SNT ) received len = getsn_rx_rsr(sock_num) // Received packet length if( received_len > 0 ) // If packet received ppp_recv(received_len); // Receive packet and Parse process if ( FLAG_IPCP_CR_SNT ) // PPPoE Configuration setting set_pppinfo(nas_mac, pppoe_ip, NAS_sessionid); // Return PPPoE Connection success ret = PPP_SUCCESS; /* PPP IPCP Configuration : ppp_recv(received_len) */ Case PPPoE_SESSION : If( PPPoE_IPCP ) Application Note PPPoE in MACRAW mode v1.0.0 21
Switch( codename ) Case CONFIG_REQ : Case CONFIG_NAK : getlen = all option length; While( getlen ) opt_code = option code; opt_len = option length; Switch( opt_code ) Case 0x02 : // IP compression Case 0x03 : // IP address // Parsing and making Config-Ack massage // Save assigned IP address /* Notice : This part is not fully implemented. */ /* If necessary, please implement more for DNS or etc.*/ default : // Making Config-Reject massage // and rej_idx += opt_len; getlen -= opt_len; // Send Response message for Request message from NAS If( rjt_idx ) // if any option is rejected, send reject message and then wait Config-Request // Making Config-Reject massage and send ppp_send(); else // Send Config-Ack, lcp_cr_rcv flag set // Making Config-Ack massage and send ppp_send(); FLAG_IPCP_NAK_RCV = 1; Application Note PPPoE in MACRAW mode v1.0.0 22
Case CONFIG_ACK : // Ack, then ipcp_cr_snt flag set if( flag_ipcp_nak_rcv ) FLAG_IPCP_CR_SNT = 1; Break; Application Note PPPoE in MACRAW mode v1.0.0 23
3.6 PPPoE Configuration Setting Process PPPoE connection을위해 Socket 0을 MACRAW mode로 open하고목적지 IP, MAC address, Session ID를 NAS로부터얻은후단말기에기록한다. 그후소켓을 PPPoE로사용하기위해 MR 레지스터 (Common Mode Register) 를 PPPoE로설정해주고 open하면이때부터사용자는 PPPoE를이용할수있게된다. 단말기는 PPPoE 연결이성공적으로이루어지면연결의지속을위해 H/W 로직으로구현된 LCP Echo Request를 Timer에정해진주기마다 NAS로전송한다. 이때 Timer의주기는 PTIMER 레지스터의설정을통해조절가능하다. Table 8. PPPoE Configuration Setting /* PPPoE Configuration Setting */ #define PTIMER (COMMON_BASE + 0x0028) #define Sn_MR_PPPOE 0x05 #define Sn_CR_OPEN 0x01 i = 0; // index for for statement /* Set PPPoE bit in MR(Common Mode Register) : Enable Socket 0 PPPoE */ IINCHIP_WRITE(MR,IINCHIP_READ(MR) MR_PPPOE); /* Set PPPoE Network information */ for (i = 0; i < 6; i++) IINCHIP_WRITE((Sn_DHAR0(0)+i), mac[i]); // NAS MAC address for (i = 0; i < 4; i++) IINCHIP_WRITE((Sn_DIPR0(0)+i), ip[i]); // Assigned IP address IINCHIP_WRITE((Sn_DPORT0(0)), (uint8)(sessionid >> 8)); // Session ID IINCHIP_WRITE((Sn_DPORT0(0)+1), (uint8)sessionid); setsn_ir(0, getsn_ir(0)); /* Set PPPoE Timer */ IINCHIP_WRITE(PTIMER,200); // 5 Sec timeout /* Open Socket in PPPoE mode */ IINCHIP_WRITE(Sn_MR(0),Sn_MR_PPPOE); IINCHIP_WRITE(Sn_CR(0),Sn_CR_OPEN); while( IINCHIP_READ(Sn_CR(0)) ); wait_1us(1); Application Note PPPoE in MACRAW mode v1.0.0 24
4 Demonstration 다음은 MACRAW 모드에서구현된 PPPoE Protocol과 W7100A를이용하여 NAS로부터 IP address를할당받기까지의과정을보인다. NAS는 Windows Server 2000을이용하였으며, 인증프로토콜은 PAP를사용하였고 192.168.200.42부터의 IP address를 IP pool로설정하여할당하도록구성하였다. IPCP를마지막으로 PPPoE 연결이올바르게수행되고, 3.6 PPPoE Configuration Setting Process의과정이수행되면 W7100A는지정된 PTIMER 시간마다 NAS로 LCP Echo Request를자동으로보내연결을유지한다. Figure 10. Serial Terminal capture of PPPoE Demonstration Figure 11. PPPoE Connection Process - Packet Capture Application Note PPPoE in MACRAW mode v1.0.0 25
Document History Information Version Date Descriptions Ver. 1.0 Feb, 2014 Release Copyright Notice Copyright 2014 WIZnet Co., Ltd. All Rights Reserved. Technical support : http://wizwiki.net/forum/ Sales & Distribution: sales@wiznet.co.kr For more information, visit our website at http://www.wiznet.co.kr and visit our wiki site at http://wizwiki.net/ Application Note PPPoE in MACRAW mode v1.0.0 26