📄 lcp.c
字号:
*
* None
*
****************************************************************************/
VOID LCP_Send_Config_Req(DV_DEVICE_ENTRY *dev_ptr)
{
LCP_HEADER *lcp_pkt;
LCP_LAYER *lcp;
UINT8 *lcp_pkt_ptr;
UINT16 len;
NET_BUFFER *buf_ptr;
LINK_LAYER *link_layer;
link_layer = (LINK_LAYER*)dev_ptr->dev_ppp_layer;
#ifdef NU_DEBUG_PPP
_PRINT ("send config req ");
#endif
len = 0;
/* Get a pointer to the lcp structure for this device. */
lcp = &link_layer->lcp;
/* Is there a negotiation packet that can be reused? */
if (lcp->negotiation_pkt != NU_NULL)
buf_ptr = lcp->negotiation_pkt;
else
/* Allocate a buffer for the LCP packet. */
lcp->negotiation_pkt = buf_ptr = MEM_Buffer_Dequeue (&MEM_Buffer_Freelist);
/* Make sure a buffer was available */
if (buf_ptr == NU_NULL)
{
NERRS_Log_Error (TCP_SEVERE, __FILE__, __LINE__);
/* Get out */
return;
}
/* Set the data ptr */
buf_ptr->data_ptr = buf_ptr->mem_parent_packet + dev_ptr->dev_hdrlen;
lcp_pkt = (LCP_HEADER *) buf_ptr->data_ptr;
/* Set up the initial configure request packet. This packet will contain
all the configuration options we need to configure. */
lcp_pkt->code = LCP_CONFIGURE_REQUEST;
lcp_pkt->identifier = LCP_Random_Number();
/* Get a pointer to the data part of the LCP packet. */
lcp_pkt_ptr = &lcp_pkt->data;
/* Go through all options and see which need to be configured. */
/* Check the MRU (or MTU) */
if (lcp->local_options.use_max_rx_unit)
{
lcp_pkt_ptr[len++] = LCP_MAX_RX_UNIT;
lcp_pkt_ptr[len++] = LCP_MRU_LENGTH;
lcp_pkt_ptr[len++] = (UINT8) ((dev_ptr->dev_mtu >> 8) & PPP_BIT_MASK);
lcp_pkt_ptr[len++] = (UINT8) (dev_ptr->dev_mtu & PPP_BIT_MASK);
}
/* Check the Async Control Char Map. */
if (lcp->local_options.use_accm)
{
lcp_pkt_ptr[len++] = LCP_ASYNC_CONTROL_CHAR_MAP;
lcp_pkt_ptr[len++] = LCP_ACCM_LENGTH;
/* Put it in the packet. */
PUT32 (lcp_pkt_ptr, (unsigned int)len, lcp->local_options.accm);
/* Bump the length by 4, 32 bits. */
len += LCP_ACCM_VALUE_SIZE;
}
/* Check the Authentication Protocol. We will only send a challenge
if we are in sever mode and there is an authentication protocol to
be used. */
if (link_layer->ipcp.mode == NCP_SERVER)
{
if (lcp->local_options.authentication_protocol == PPP_CHAP_PROTOCOL)
{
lcp_pkt_ptr[len++] = LCP_AUTHENTICATION_PROTOCOL;
lcp_pkt_ptr[len++] = LCP_CHAP_LENGTH;
/* Put in the type of authentication */
lcp_pkt_ptr[len++] = (UINT8) ((lcp->local_options.authentication_protocol >> 8)
& PPP_BIT_MASK);
lcp_pkt_ptr[len++] = (UINT8) (lcp->local_options.authentication_protocol
& PPP_BIT_MASK);
/* Put in the type of algorithm used, ours will be MD5 */
lcp_pkt_ptr[len++] = LCP_CHAP_MD5;
}
else
{
if (lcp->local_options.authentication_protocol == PPP_PAP_PROTOCOL)
{
lcp_pkt_ptr[len++] = LCP_AUTHENTICATION_PROTOCOL;
lcp_pkt_ptr[len++] = LCP_PAP_LENGTH;
/* Put in the type of authentication */
lcp_pkt_ptr[len++] = (UINT8) ((lcp->local_options.authentication_protocol >> 8)
& PPP_BIT_MASK);
lcp_pkt_ptr[len++] = (UINT8) (lcp->local_options.authentication_protocol
& PPP_BIT_MASK);
}
}
}
/* Check the Quality protocol. */
/* There is no support for this at this time. */
/* Check the Magic Number. */
if (lcp->local_options.magic_number)
{
lcp_pkt_ptr[len++] = LCP_MAGIC_NUMBER;
lcp_pkt_ptr[len++] = LCP_MAGIC_NUMBER_LENGTH;
lcp->local_options.magic_number = LCP_Random_Number32();
/* Put it in the packet. */
PUT32 (lcp_pkt_ptr, (unsigned int)len, lcp->local_options.magic_number);
/* Bump the length by 4, 32 bits. */
len += LCP_MAGIC_NUMBER_VALUE_SIZE;
}
/* Protocol Field Compression. */
if (lcp->local_options.protocol_field_compression)
{
lcp_pkt_ptr[len++] = LCP_PROTOCOL_FIELD_COMPRESS;
lcp_pkt_ptr[len++] = LCP_PROTOCOL_COMPRESS_LENGTH;
}
/* Address and Control Compresssion. */
if (lcp->local_options.address_field_compression)
{
lcp_pkt_ptr[len++] = LCP_ADDRESS_FIELD_COMPRESS;
lcp_pkt_ptr[len++] = LCP_ADDRESS_COMPRESS_LENGTH;
}
/* Fill in the length for the LCP packet. */
len += LCP_HEADER_LENGTH;
lcp_pkt->length = INTSWAP (len);
/* Set the length for the buffer. */
buf_ptr->data_len = buf_ptr->mem_total_data_len = len;
/* Store this identifier for checking receive pkts. */
lcp->identifier = lcp_pkt->identifier;
/* Ok, now send the pkt and let the automaton begin. */
/* Bump the transmission count. */
lcp->num_transmissions--;
/* Set the packet type. */
buf_ptr->mem_flags = NET_LCP;
/* Set the dlist to NULL so this buffer will not be
put on the free list. */
buf_ptr->mem_dlist = NU_NULL;
/* Send the configure request packet. */
dev_ptr->dev_output (buf_ptr, dev_ptr, NU_NULL, NU_NULL);
}
/***************************************************************************
*
* FUNCTION
*
* LCP_Random_Number
*
* DESCRIPTION
*
* This routine will handle Pseudo-random sequence generation. The
* algorithms and code was taken from the draft of the C standards
* document dated November 11, 1985.
*
* INPUTS
*
* None
*
* OUTPUTS
*
* Returns a value in the range of 0 to RAND_MAX.
*
****************************************************************************/
#define LCP_RAND_MAX 256
UINT8 LCP_Random_Number(VOID)
{
static UINT32 next = 1;
next = next * NU_Retrieve_Clock() + 12345;
return ((UINT8) ((next / 32) % (LCP_RAND_MAX + 1)));
} /* end rand routine */
/***************************************************************************
*
* FUNCTION
*
* LCP_Random_Number32
*
* DESCRIPTION
*
* This routine will handle Pseudo-random sequence generation. The
* algorithms and code was taken from the draft of the C standards
* document dated November 11, 1985.
*
* INPUTS
*
* None
*
* OUTPUTS
*
* Returns a value in the range of 0 to RAND_MAX.
*
****************************************************************************/
#define LCP_RAND_MAX32 1000000000ul
UINT32 LCP_Random_Number32(VOID)
{
static UINT32 next = 1;
next = next * NU_Retrieve_Clock() + 12345;
return (next % (LCP_RAND_MAX32 + 1));
} /* end rand routine */
/***************************************************************************
*
* FUNCTION
*
* LCP_Timer_Expire
*
* DESCRIPTION
*
* This is the expiration routine for the LCP negotiation timer. It will
* retransmit a LCP packet or signal PPP that negotiation failed. All is
* based on the current state of LCP.
*
* INPUTS
*
* UNSIGNED dev_ptr Address of the device structure
* that the timer expired for.
*
* OUTPUTS
*
* None
*
****************************************************************************/
VOID LCP_Timer_Expire(UNSIGNED dev_ptr)
{
DV_DEVICE_ENTRY *device;
LCP_LAYER *lcp;
LINK_LAYER *link_layer;
#ifdef NU_DEBUG_PPP
_PRINT ("\nlcp_timer ");
#endif
/* Point the device stucture to the address of dev_ptr */
device = (DV_DEVICE_ENTRY *)dev_ptr;
link_layer = (LINK_LAYER*)device->dev_ppp_layer;
/* Get the LCP stucture for this device. */
lcp = &link_layer->lcp;
/* Check the current state to see what we can do. */
switch (lcp->state)
{
case CLOSING :
#ifdef NU_DEBUG_PPP
_PRINT ("closing ");
#endif
/* See if we get another try at terminating. */
if (lcp->num_transmissions-- > 0)
{
#ifdef NU_DEBUG_PPP
_PRINT ("send term req again ");
#endif
/* Set the event to send the packet again. */
UTL_Timerset (LCP_RESEND, dev_ptr, NU_NULL, NU_NULL);
}
else
{
/* Stop any PPP timer that may still be running */
PPP_Stop_All_Timers(device);
/* Change states for LCP*/
lcp->state = CLOSED;
#ifdef NU_DEBUG_PPP
_PRINT ("LCP State: CLOSED ");
#endif
/* Set the next state to goto after the modem hangsup */
link_layer->lcp.echo_counter = INITIAL;
/* Set the event to hang the modem up. */
UTL_Timerset (MDM_HANGUP, dev_ptr, NU_NULL, NU_NULL);
}
break;
case STOPPING :
#ifdef NU_DEBUG_PPP
_PRINT ("stopping\n");
#endif
/* See if we get another try at terminating. */
if (lcp->num_transmissions-- > 0)
{
#ifdef NU_DEBUG_PPP
_PRINT ("send term req again ");
#endif
/* Set the event to send the packet again. */
UTL_Timerset (LCP_RESEND, dev_ptr, NU_NULL, NU_NULL);
}
else
{
/* Stop any PPP timer that may still be running */
PPP_Stop_All_Timers(device);
/* Otherwise we have tried to many times, set the event
that will tell initialization we could not configure. Do
this only if NCP is not in the open state, this will
suggest that we are still negotiating the link and this
terminate is probaly because we could not authenticate. */
if (link_layer->ipcp.state != OPENED)
NU_Set_Events (&link_layer->negotiation_progression,
LCP_CONFIG_FAIL, NU_OR);
/* Change states for LCP */
lcp->state = STOPPED;
#ifdef NU_DEBUG_PPP
_PRINT ("LCP State: STOPPED ");
#endif
/* Set the next state to goto after the modem hangsup */
link_layer->lcp.echo_counter = STARTING;
/* Set the event to hang the modem up. */
UTL_Timerset (MDM_HANGUP, dev_ptr, NU_NULL, NU_NULL);
}
break;
case REQ_SENT :
#ifdef NU_DEBUG_PPP
_PRINT ("req_sent ");
#endif
/* See if we get another try at configuring. */
if (lcp->num_transmissions-- > 0)
{
#ifdef NU_DEBUG_PPP
_PRINT ("send config req again ");
#endif
/* Set the event to send the packet again. */
UTL_Timerset (LCP_RESEND, dev_ptr, NU_NULL, NU_NULL);
}
else
{
/* Stop any PPP timer that may still be running */
PPP_Stop_All_Timers(device);
/* Change states */
lcp->state = STOPPED;
#ifdef NU_DEBUG_PPP
_PRINT ("LCP State: STOPPED ");
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -