nuttx-apps/netutils/pppd/ipcp.c

544 lines
15 KiB
C
Raw Normal View History

/****************************************************************************
* apps/netutils/pppd/ipcp.c
* PPP IPCP (intrnet protocol) Processor/Handler
2015-03-11 14:53:04 +01:00
*
* 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.
2015-03-11 14:53:04 +01:00
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
2015-03-11 14:53:04 +01:00
* 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.
*
****************************************************************************/
2015-03-11 14:53:04 +01:00
/****************************************************************************
* Included Files
****************************************************************************/
2015-03-11 14:53:04 +01:00
#include "ppp_conf.h"
#include "ppp_arch.h"
#include "ipcp.h"
#include "ppp.h"
#include "ahdlc.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
2015-03-11 14:53:04 +01:00
#if PPP_DEBUG
# define DEBUG1(x) debug_printf x
#else
# define DEBUG1(x)
#endif
/****************************************************************************
* Private Types
****************************************************************************/
2015-03-11 14:53:04 +01:00
/* In the future add compression protocol and name servers (possibly for servers
* only)
*/
static const uint8_t g_ipcplist[] =
2015-03-11 14:53:04 +01:00
{
IPCP_IPADDRESS,
2015-03-11 14:53:04 +01:00
0
};
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: printip
****************************************************************************/
2015-03-11 14:53:04 +01:00
#if PPP_DEBUG
void printip(struct in_addr ip2)
2015-03-11 14:53:04 +01:00
{
char *ip = (FAR uint8_t *) & ip2.s_addr;
DEBUG1((" %d.%d.%d.%d ", ip[0], ip[1], ip[2], ip[3]));
2015-03-11 14:53:04 +01:00
}
#else
# define printip(x)
#endif
/****************************************************************************
* Name: ipcp_init
****************************************************************************/
2015-03-11 14:53:04 +01:00
void ipcp_init(FAR struct ppp_context_s *ctx)
2015-03-11 14:53:04 +01:00
{
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
*
****************************************************************************/
2015-03-11 14:53:04 +01:00
void ipcp_rx(FAR struct ppp_context_s *ctx, FAR uint8_t * buffer,
uint16_t count)
2015-03-11 14:53:04 +01:00
{
FAR uint8_t *bptr = buffer;
uint16_t len;
2015-03-11 14:53:04 +01:00
DEBUG1(("IPCP len %d\n", count));
2015-03-11 14:53:04 +01:00
switch (*bptr++)
{
case CONF_REQ:
/* Parse request and see if we can ACK it */
++bptr;
len = (*bptr++ << 8);
len |= *bptr++;
/* len-=2; */
DEBUG1(("check lcplist\n"));
if (scan_packet(ctx, IPCP, g_ipcplist, buffer, bptr, (uint16_t)(len - 4)))
{
DEBUG1(("option was bad\n"));
}
else
{
DEBUG1(("IPCP options are good\n"));
/* Parse out the results */
/* lets try to implement what peer wants */
/* Reject any protocol not */
/* Error? if we we need to send a config Reject ++++ this is good for
* a subroutine.
*/
/* All we should get is the peer IP address */
if (IPCP_IPADDRESS == *bptr++)
{
/* Dump length */
++bptr;
2015-03-11 14:53:04 +01:00
#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++;
2015-03-11 14:53:04 +01:00
DEBUG1(("Peer IP "));
printip(ctx->peer_ip);
DEBUG1(("\n"));
2015-03-11 14:53:04 +01:00
netlib_set_dripv4addr((char *)ctx->ifname, &ctx->peer_ip);
2015-03-11 14:53:04 +01:00
#else
bptr += 4;
2015-03-11 14:53:04 +01:00
#endif
}
else
{
DEBUG1(("HMMMM this shouldn't happen IPCP1\n"));
}
2015-03-11 14:53:04 +01:00
#if 0
if (error)
{
/* Write the config NAK packet we've built above, take on the
* header
*/
bptr = buffer;
*bptr++ = CONF_NAK; /* Write Conf_rej */
*bptr++;
2015-03-11 14:53:04 +01:00
/* tptr++; *//* skip over ID */
2015-03-11 14:53:04 +01:00
/* Write new length */
2015-03-11 14:53:04 +01:00
*bptr++ = 0;
*bptr = tptr - buffer;
2015-03-11 14:53:04 +01:00
/* Write the reject frame */
2015-03-11 14:53:04 +01:00
DEBUG1(("Writing NAK frame\n"));
ahdlc_tx(IPCP, buffer, (uint16_t)(tptr - buffer));
DEBUG1(("- End NAK Write frame\n"));
}
else
{
}
2015-03-11 14:53:04 +01:00
#endif
/* If we get here then we are OK, lets send an ACK and tell the rest
* of our modules our negotiated config.
*/
ctx->ipcp_state |= IPCP_RX_UP;
DEBUG1(("Send IPCP ACK!\n"));
bptr = buffer;
*bptr++ = CONF_ACK; /* Write Conf_ACK */
bptr++; /* Skip ID (send same one) */
2015-03-11 14:53:04 +01:00
/* Set stuff */
2015-03-11 14:53:04 +01:00
/* ppp_flags |= tflag; */
2015-03-11 14:53:04 +01:00
DEBUG1(("SET- stuff -- are we up? c=%d dif=%d\n",
count, (uint16_t)(bptr - buffer)));
2015-03-11 14:53:04 +01:00
/* Write the ACK frame */
2015-03-11 14:53:04 +01:00
DEBUG1(("Writing ACK frame\n"));
2015-03-11 14:53:04 +01:00
/* Send packet ahdlc_txz(procol,header,data,headerlen,datalen); */
ahdlc_tx(ctx, IPCP, 0, buffer, 0, count /* bptr-buffer */);
DEBUG1(("- End ACK Write frame\n"));
}
break;
2015-03-11 14:53:04 +01:00
case CONF_ACK: /* config Ack */
DEBUG1(("CONF ACK\n"));
2015-03-11 14:53:04 +01:00
/* Parse out the results Dump the ID and get the length. */
2015-03-11 14:53:04 +01:00
/* Dump the ID */
2015-03-11 14:53:04 +01:00
bptr++;
2015-03-11 14:53:04 +01:00
/* Get the length */
2015-03-11 14:53:04 +01:00
len = (*bptr++ << 8);
len |= *bptr++;
2015-03-11 14:53:04 +01:00
#if 0
/* Parse ACK and set data */
2015-03-11 14:53:04 +01:00
while (bptr < buffer + len)
2015-03-11 14:53:04 +01:00
{
switch (*bptr++)
{
case IPCP_IPADDRESS:
/* Dump length */
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"));
}
2015-03-11 14:53:04 +01:00
}
#endif
ctx->ipcp_state |= IPCP_TX_UP;
/* ppp_ipcp_state &= ~IPCP_RX_UP; */
2015-03-11 14:53:04 +01:00
DEBUG1(("were up!\n"));
printip(ctx->local_ip);
2015-03-11 14:53:04 +01:00
#ifdef IPCP_GET_PRI_DNS
printip(ctx->pri_dns_addr);
2015-03-11 14:53:04 +01:00
#endif
#ifdef IPCP_GET_SEC_DNS
printip(ctx->sec_dns_addr);
2015-03-11 14:53:04 +01:00
#endif
DEBUG1(("\n"));
break;
2015-03-11 14:53:04 +01:00
case CONF_NAK: /* Config Nack */
DEBUG1(("CONF NAK\n"));
2015-03-11 14:53:04 +01:00
/* Dump the ID */
2015-03-11 14:53:04 +01:00
bptr++;
2015-03-11 14:53:04 +01:00
/* Get the length */
2015-03-11 14:53:04 +01:00
len = (*bptr++ << 8);
len |= *bptr++;
2015-03-11 14:53:04 +01:00
/* Parse ACK and set data */
2015-03-11 14:53:04 +01:00
while (bptr < buffer + len)
2015-03-11 14:53:04 +01:00
{
switch (*bptr++)
{
case IPCP_IPADDRESS:
/* Dump length */
bptr++;
2015-03-11 14:53:04 +01:00
((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++;
2015-03-11 14:53:04 +01:00
netlib_ifup((char *)ctx->ifname);
netlib_set_ipv4addr((char *)ctx->ifname, &ctx->local_ip);
break;
2015-03-11 14:53:04 +01:00
#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;
2015-03-11 14:53:04 +01:00
#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;
2015-03-11 14:53:04 +01:00
#endif
default:
DEBUG1(("IPCP CONFIG_ACK problem 2\n"));
}
2015-03-11 14:53:04 +01:00
}
ctx->ppp_id++;
2015-03-11 14:53:04 +01:00
printip(ctx->local_ip);
2015-03-11 14:53:04 +01:00
#ifdef IPCP_GET_PRI_DNS
printip(ctx->pri_dns_addr);
2015-03-11 14:53:04 +01:00
#endif
#ifdef IPCP_GET_PRI_DNS
printip(ctx->sec_dns_addr);
2015-03-11 14:53:04 +01:00
#endif
DEBUG1(("\n"));
break;
2015-03-11 14:53:04 +01:00
case CONF_REJ: /* Config Reject */
DEBUG1(("CONF REJ\n"));
2015-03-11 14:53:04 +01:00
/* Remove the offending options */
2015-03-11 14:53:04 +01:00
ctx->ppp_id++;
2015-03-11 14:53:04 +01:00
/* Dump the ID */
2015-03-11 14:53:04 +01:00
bptr++;
2015-03-11 14:53:04 +01:00
/* Get the length */
2015-03-11 14:53:04 +01:00
len = (*bptr++ << 8);
len |= *bptr++;
2015-03-11 14:53:04 +01:00
/* Parse ACK and set data */
2015-03-11 14:53:04 +01:00
while (bptr < buffer + len)
2015-03-11 14:53:04 +01:00
{
switch (*bptr++)
{
case IPCP_IPADDRESS:
ctx->ipcp_state |= IPCP_IP_BIT;
bptr += 5;
break;
2015-03-11 14:53:04 +01:00
#ifdef IPCP_GET_PRI_DNS
case IPCP_PRIMARY_DNS:
ctx->ipcp_state |= IPCP_PRI_DNS_BIT;
bptr += 5;
break;
2015-03-11 14:53:04 +01:00
#endif
#ifdef IPCP_GET_PRI_DNS
case IPCP_SECONDARY_DNS:
ctx->ipcp_state |= IPCP_SEC_DNS_BIT;
bptr += 5;
break;
2015-03-11 14:53:04 +01:00
#endif
default:
DEBUG1(("IPCP this shouldn't happen 3\n"));
}
2015-03-11 14:53:04 +01:00
}
break;
2015-03-11 14:53:04 +01:00
default:
DEBUG1(("-Unknown 4\n"));
}
2015-03-11 14:53:04 +01:00
}
/****************************************************************************
* Name: ipcp_task
****************************************************************************/
2015-03-11 14:53:04 +01:00
void ipcp_task(FAR struct ppp_context_s *ctx, FAR uint8_t * buffer)
2015-03-11 14:53:04 +01:00
{
FAR uint8_t *bptr;
uint16_t t;
2015-03-11 14:53:04 +01:00
IPCPPKT *pkt;
/* IPCP tx not up and hasn't timed out then lets see if we need to send a
* request
*/
2015-03-11 14:53:04 +01:00
if (!(ctx->ipcp_state & IPCP_TX_UP) && !(ctx->ipcp_state & IPCP_TX_TIMEOUT))
{
/* Check if we have a request pending */
2015-03-11 14:53:04 +01:00
if ((ppp_arch_clock_seconds() - ctx->ipcp_prev_seconds) > IPCP_TIMEOUT)
{
ctx->ipcp_prev_seconds = ppp_arch_clock_seconds();
2015-03-11 14:53:04 +01:00
/* No pending request, lets build one */
2015-03-11 14:53:04 +01:00
pkt = (IPCPPKT *) buffer;
2015-03-11 14:53:04 +01:00
/* Configure-Request only here, write id */
2015-03-11 14:53:04 +01:00
pkt->code = CONF_REQ;
pkt->id = ctx->ppp_id;
2015-03-11 14:53:04 +01:00
bptr = pkt->data;
2015-03-11 14:53:04 +01:00
/* Write options, we want IP address, and DNS addresses if set.
* Write zeros for IP address the first time
*/
2015-03-11 14:53:04 +01:00
*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];
2015-03-11 14:53:04 +01:00
#ifdef IPCP_GET_PRI_DNS
if ((ctx->ipcp_state & IPCP_PRI_DNS_BIT) == 0)
{
/* Write zeros for IP address the first time */
*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];
}
2015-03-11 14:53:04 +01:00
#endif
#ifdef IPCP_GET_SEC_DNS
if ((ctx->ipcp_state & IPCP_SEC_DNS_BIT) == 0)
{
/* Write zeros for IP address the first time */
*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];
}
2015-03-11 14:53:04 +01:00
#endif
/* Write length */
2015-03-11 14:53:04 +01:00
t = bptr - buffer;
2015-03-11 14:53:04 +01:00
/* length here - code and ID + */
2015-03-11 14:53:04 +01:00
pkt->len = htons(t);
2015-03-11 14:53:04 +01:00
DEBUG1(("\n**Sending IPCP Request packet\n"));
2015-03-11 14:53:04 +01:00
/* Send packet ahdlc_txz(procol,header,data,headerlen,datalen); */
2015-03-11 14:53:04 +01:00
ahdlc_tx(ctx, IPCP, 0, buffer, 0, t);
2015-03-11 14:53:04 +01:00
/* Inc retry */
2015-03-11 14:53:04 +01:00
ctx->ipcp_retry++;
2015-03-11 14:53:04 +01:00
/* Have we timed out? (combine the timers?) */
2015-03-11 14:53:04 +01:00
if (ctx->ipcp_retry > IPCP_RETRY_COUNT)
{
ctx->ipcp_state |= IPCP_TX_TIMEOUT;
}
}
}
2015-03-11 14:53:04 +01:00
}