* apps/netutils/pppd/ipcp.c
* PPP IPCP (intrnet protocol) Processor/Handler
*
* Version: 0.1 Original Version Jun 3, 2000
* Copyright (C) 2000, Mycal Labs www.mycal.com
* Copyright (c) 2003, Mike Johnson, Mycal Labs, www.mycal.net
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Mike Johnson/Mycal Labs
* www.mycal.net.
* 4. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
* Included Files
****************************************************************************/
#include "ppp_conf.h"
#include "ppp_arch.h"
#include "ipcp.h"
#include "ppp.h"
#include "ahdlc.h"
* Pre-processor Definitions
****************************************************************************/
#if PPP_DEBUG
# define DEBUG1(x) debug_printf x
#else
# define DEBUG1(x)
#endif
* Private Types
****************************************************************************/
* servers only)
*/
static const uint8_t g_ipcplist[] =
{
IPCP_IPADDRESS,
0
};
* Private Functions
****************************************************************************/
* Public Functions
****************************************************************************/
* Name: printip
****************************************************************************/
#if PPP_DEBUG
void printip(struct in_addr ip2)
{
FAR uint8_t *ip = (FAR uint8_t *)&ip2.s_addr;
DEBUG1((" %u.%u.%u.%u ", ip[0], ip[1], ip[2], ip[3]));
}
#else
# define printip(x)
#endif
* Name: ipcp_init
****************************************************************************/
void ipcp_init(FAR struct ppp_context_s *ctx)
{
DEBUG1(("ipcp init\n"));
ctx->ipcp_state = 0;
ctx->ipcp_retry = 0;
ctx->ipcp_prev_seconds = 0;
memset(&ctx->local_ip, 0, sizeof(struct in_addr));
#ifdef IPCP_GET_PEER_IP
memset(&ctx->peer_ip, 0, sizeof(struct in_addr));
#endif
#ifdef IPCP_GET_PRI_DNS
memset(&ctx->pri_dns_addr, 0, sizeof(struct in_addr));
#endif
#ifdef IPCP_GET_SEC_DNS
memset(&ctx->sec_dns_addr, 0, sizeof(struct in_addr));
#endif
}
* Name: ipcp_rx
*
* Description:
* IPCP RX protocol Handler
*
****************************************************************************/
void ipcp_rx(FAR struct ppp_context_s *ctx, FAR uint8_t * buffer,
uint16_t count)
{
FAR uint8_t *bptr = buffer;
uint16_t len;
DEBUG1(("IPCP len %d\n", count));
switch (*bptr++)
{
case CONF_REQ:
++bptr;
len = (*bptr++ << 8);
len |= *bptr++;
DEBUG1(("check lcplist\n"));
if (scan_packet(ctx, IPCP, g_ipcplist, buffer, bptr, len - 4))
{
DEBUG1(("option was bad\n"));
}
else
{
DEBUG1(("IPCP options are good\n"));
* for a subroutine.
*/
if (IPCP_IPADDRESS == *bptr++)
{
++bptr;
#ifdef IPCP_GET_PEER_IP
((FAR uint8_t *)&ctx->peer_ip)[0] = *bptr++;
((FAR uint8_t *)&ctx->peer_ip)[1] = *bptr++;
((FAR uint8_t *)&ctx->peer_ip)[2] = *bptr++;
((FAR uint8_t *)&ctx->peer_ip)[3] = *bptr++;
DEBUG1(("Peer IP "));
printip(ctx->peer_ip);
DEBUG1(("\n"));
netlib_set_dripv4addr((char *)ctx->ifname, &ctx->peer_ip);
#else
bptr += 4;
#endif
}
else
{
DEBUG1(("HMMMM this shouldn't happen IPCP1\n"));
}
#if 0
if (error)
{
* header
*/
bptr = buffer;
*bptr++ = CONF_NAK;
*bptr++;
*bptr++ = 0;
*bptr = tptr - buffer;
DEBUG1(("Writing NAK frame\n"));
ahdlc_tx(IPCP, buffer, (uint16_t)(tptr - buffer));
DEBUG1(("- End NAK Write frame\n"));
}
else
{
}
#endif
* rest of our modules our negotiated config.
*/
ctx->ipcp_state |= IPCP_RX_UP;
DEBUG1(("Send IPCP ACK!\n"));
bptr = buffer;
*bptr++ = CONF_ACK;
bptr++;
DEBUG1(("SET- stuff -- are we up? c=%d dif=%d\n",
count, (uint16_t)(bptr - buffer)));
DEBUG1(("Writing ACK frame\n"));
ahdlc_tx(ctx, IPCP, 0, buffer, 0, count );
DEBUG1(("- End ACK Write frame\n"));
}
break;
case CONF_ACK:
DEBUG1(("CONF ACK\n"));
bptr++;
len = (*bptr++ << 8);
len |= *bptr++;
#if 0
while (bptr < buffer + len)
{
switch (*bptr++)
{
case IPCP_IPADDRESS:
bptr++;
((FAR uint8_t *)&ctx->local_ip)[0] = *bptr++;
((FAR uint8_t *)&ctx->local_ip)[1] = *bptr++;
((FAR uint8_t *)&ctx->local_ip)[2] = *bptr++;
((FAR uint8_t *)&ctx->local_ip)[3] = *bptr++;
break;
# ifdef IPCP_GET_PRI_DNS
case IPCP_PRIMARY_DNS:
bptr++;
((FAR uint8_t *)&ctx->pri_dns_addr)[0] = *bptr++;
((FAR uint8_t *)&ctx->pri_dns_addr)[1] = *bptr++;
((FAR uint8_t *)&ctx->pri_dns_addr)[2] = *bptr++;
((FAR uint8_t *)&ctx->pri_dns_addr)[3] = *bptr++;
break;
# endif
# ifdef IPCP_GET_SEC_DNS
case IPCP_SECONDARY_DNS:
bptr++;
((FAR uint8_t *)&ctx->sec_dns_addr)[0] = *bptr++;
((FAR uint8_t *)&ctx->sec_dns_addr)[1] = *bptr++;
((FAR uint8_t *)&ctx->sec_dns_addr)[2] = *bptr++;
((FAR uint8_t *)&ctx->sec_dns_addr)[3] = *bptr++;
break;
# endif
default:
DEBUG1(("IPCP CONFIG_ACK problem1\n"));
}
}
#endif
ctx->ipcp_state |= IPCP_TX_UP;
DEBUG1(("were up!\n"));
printip(ctx->local_ip);
#ifdef IPCP_GET_PRI_DNS
printip(ctx->pri_dns_addr);
#endif
#ifdef IPCP_GET_SEC_DNS
printip(ctx->sec_dns_addr);
#endif
DEBUG1(("\n"));
break;
case CONF_NAK:
DEBUG1(("CONF NAK\n"));
bptr++;
len = (*bptr++ << 8);
len |= *bptr++;
while (bptr < buffer + len)
{
switch (*bptr++)
{
case IPCP_IPADDRESS:
bptr++;
((FAR uint8_t *)&ctx->local_ip)[0] = (char)*bptr++;
((FAR uint8_t *)&ctx->local_ip)[1] = (char)*bptr++;
((FAR uint8_t *)&ctx->local_ip)[2] = (char)*bptr++;
((FAR uint8_t *)&ctx->local_ip)[3] = (char)*bptr++;
netlib_ifup((FAR char *)ctx->ifname);
netlib_set_ipv4addr((FAR char *)ctx->ifname, &ctx->local_ip);
break;
#ifdef IPCP_GET_PRI_DNS
case IPCP_PRIMARY_DNS:
bptr++;
((FAR uint8_t *)&ctx->pri_dns_addr)[0] = *bptr++;
((FAR uint8_t *)&ctx->pri_dns_addr)[1] = *bptr++;
((FAR uint8_t *)&ctx->pri_dns_addr)[2] = *bptr++;
((FAR uint8_t *)&ctx->pri_dns_addr)[3] = *bptr++;
netlib_set_ipv4dnsaddr(&ctx->pri_dns_addr);
break;
#endif
#ifdef IPCP_GET_SEC_DNS
case IPCP_SECONDARY_DNS:
bptr++;
((FAR uint8_t *)&ctx->sec_dns_addr)[0] = *bptr++;
((FAR uint8_t *)&ctx->sec_dns_addr)[1] = *bptr++;
((FAR uint8_t *)&ctx->sec_dns_addr)[2] = *bptr++;
((FAR uint8_t *)&ctx->sec_dns_addr)[3] = *bptr++;
netlib_set_ipv4dnsaddr(&ctx->sec_dns_addr);
break;
#endif
default:
DEBUG1(("IPCP CONFIG_ACK problem 2\n"));
}
}
ctx->ppp_id++;
printip(ctx->local_ip);
#ifdef IPCP_GET_PRI_DNS
printip(ctx->pri_dns_addr);
#endif
#ifdef IPCP_GET_PRI_DNS
printip(ctx->sec_dns_addr);
#endif
DEBUG1(("\n"));
break;
case CONF_REJ:
DEBUG1(("CONF REJ\n"));
ctx->ppp_id++;
bptr++;
len = (*bptr++ << 8);
len |= *bptr++;
while (bptr < buffer + len)
{
switch (*bptr++)
{
case IPCP_IPADDRESS:
ctx->ipcp_state |= IPCP_IP_BIT;
bptr += 5;
break;
#ifdef IPCP_GET_PRI_DNS
case IPCP_PRIMARY_DNS:
ctx->ipcp_state |= IPCP_PRI_DNS_BIT;
bptr += 5;
break;
#endif
#ifdef IPCP_GET_PRI_DNS
case IPCP_SECONDARY_DNS:
ctx->ipcp_state |= IPCP_SEC_DNS_BIT;
bptr += 5;
break;
#endif
default:
DEBUG1(("IPCP this shouldn't happen 3\n"));
}
}
break;
default:
DEBUG1(("-Unknown 4\n"));
}
}
* Name: ipcp_task
****************************************************************************/
void ipcp_task(FAR struct ppp_context_s *ctx, FAR uint8_t * buffer)
{
FAR uint8_t *bptr;
uint16_t t;
IPCPPKT *pkt;
* request
*/
if (!(ctx->ipcp_state & IPCP_TX_UP) &&
!(ctx->ipcp_state & IPCP_TX_TIMEOUT))
{
if ((ppp_arch_clock_seconds() - ctx->ipcp_prev_seconds) > IPCP_TIMEOUT)
{
ctx->ipcp_prev_seconds = ppp_arch_clock_seconds();
pkt = (IPCPPKT *)buffer;
pkt->code = CONF_REQ;
pkt->id = ctx->ppp_id;
bptr = pkt->data;
* Write zeros for IP address the first time
*/
*bptr++ = IPCP_IPADDRESS;
*bptr++ = 0x6;
*bptr++ = (uint8_t)((FAR uint8_t *)&ctx->local_ip)[0];
*bptr++ = (uint8_t)((FAR uint8_t *)&ctx->local_ip)[1];
*bptr++ = (uint8_t)((FAR uint8_t *)&ctx->local_ip)[2];
*bptr++ = (uint8_t)((FAR uint8_t *)&ctx->local_ip)[3];
#ifdef IPCP_GET_PRI_DNS
if ((ctx->ipcp_state & IPCP_PRI_DNS_BIT) == 0)
{
*bptr++ = IPCP_PRIMARY_DNS;
*bptr++ = 0x6;
*bptr++ = ((FAR uint8_t *)&ctx->pri_dns_addr)[0];
*bptr++ = ((FAR uint8_t *)&ctx->pri_dns_addr)[1];
*bptr++ = ((FAR uint8_t *)&ctx->pri_dns_addr)[2];
*bptr++ = ((FAR uint8_t *)&ctx->pri_dns_addr)[3];
}
#endif
#ifdef IPCP_GET_SEC_DNS
if ((ctx->ipcp_state & IPCP_SEC_DNS_BIT) == 0)
{
*bptr++ = IPCP_SECONDARY_DNS;
*bptr++ = 0x6;
*bptr++ = ((FAR uint8_t *)&ctx->sec_dns_addr)[0];
*bptr++ = ((FAR uint8_t *)&ctx->sec_dns_addr)[1];
*bptr++ = ((FAR uint8_t *)&ctx->sec_dns_addr)[2];
*bptr++ = ((FAR uint8_t *)&ctx->sec_dns_addr)[3];
}
#endif
t = bptr - buffer;
pkt->len = htons(t);
DEBUG1(("\n**Sending IPCP Request packet\n"));
ahdlc_tx(ctx, IPCP, 0, buffer, 0, t);
ctx->ipcp_retry++;
if (ctx->ipcp_retry > IPCP_RETRY_COUNT)
{
ctx->ipcp_state |= IPCP_TX_TIMEOUT;
}
}
}
}