Nut/OS  4.10.3
API Reference
lcpout.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2001-2004 by egnite Software GmbH. All rights reserved.
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions
00006  * are met:
00007  *
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the copyright holders nor the names of
00014  *    contributors may be used to endorse or promote products derived
00015  *    from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY EGNITE SOFTWARE GMBH AND CONTRIBUTORS
00018  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00019  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00020  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL EGNITE
00021  * SOFTWARE GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00022  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00023  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00024  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00025  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00026  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
00027  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00028  * SUCH DAMAGE.
00029  *
00030  * For additional information see http://www.ethernut.de/
00031  *
00032  * -
00033  * Portions are 
00034  * Copyright (c) 1989 by Carnegie Mellon University.
00035  * All rights reserved.
00036  *
00037  * Redistribution and use in source and binary forms are permitted
00038  * provided that the above copyright notice and this paragraph are
00039  * duplicated in all such forms and that any documentation,
00040  * advertising materials, and other materials related to such
00041  * distribution and use acknowledge that the software was developed
00042  * by Carnegie Mellon University.  The name of the
00043  * University may not be used to endorse or promote products derived
00044  * from this software without specific prior written permission.
00045  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
00046  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
00047  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00048  */
00049 
00050 /*
00051  * $Log$
00052  * Revision 1.7  2008/08/11 07:00:30  haraldkipp
00053  * BSD types replaced by stdint types (feature request #1282721).
00054  *
00055  * Revision 1.6  2006/10/08 16:48:22  haraldkipp
00056  * Documentation fixed
00057  *
00058  * Revision 1.5  2005/04/08 15:20:51  olereinhardt
00059  * added <sys/types.h> (__APPLE__) and <netinet/in.h> (__linux__)
00060  * for htons and simmilar.
00061  *
00062  * Revision 1.4  2004/03/08 11:26:57  haraldkipp
00063  * Bugfix, establishing PPP sessions failed.
00064  *
00065  * Revision 1.3  2004/01/30 11:37:58  haraldkipp
00066  * Handle magic number rejects
00067  *
00068  * Revision 1.2  2003/08/14 15:18:41  haraldkipp
00069  * Negotiate local magic
00070  *
00071  * Revision 1.1.1.1  2003/05/09 14:41:34  haraldkipp
00072  * Initial using 3.2.1
00073  *
00074  * Revision 1.2  2003/05/06 18:15:30  harald
00075  * Use async map define
00076  *
00077  * Revision 1.1  2003/03/31 14:53:28  harald
00078  * Prepare release 3.1
00079  *
00080  */
00081 
00082 #include <string.h>
00083 
00084 #include <dev/ppp.h>
00085 #include <sys/types.h>
00086 #include <sys/heap.h>
00087 #include <netinet/in.h>
00088 #include <net/if_var.h>
00089 #include <netinet/if_ppp.h>
00090 #include <netinet/ppp_fsm.h>
00091 #include <net/ppp.h>
00092 #include <dev/usart.h>
00093 #include <io.h>
00094 
00099 
00100 extern uint32_t new_magic;
00101 
00118 int NutLcpOutput(NUTDEVICE * dev, uint8_t code, uint8_t id, NETBUF * nb)
00119 {
00120     XCPHDR *lcp;
00121 
00122     if ((nb = NutNetBufAlloc(nb, NBAF_NETWORK, sizeof(XCPHDR))) == 0)
00123         return -1;
00124 
00125     lcp = nb->nb_nw.vp;
00126     lcp->xch_code = code;
00127     lcp->xch_id = id;
00128     lcp->xch_len = htons(nb->nb_nw.sz + nb->nb_tp.sz + nb->nb_ap.sz);
00129 
00130     if (NutPppOutput(dev, PPP_LCP, 0, nb) == 0)
00131         NutNetBufFree(nb);
00132 
00133     return 0;
00134 }
00135 
00136 static INLINE void LcpResetOptions(NUTDEVICE * dev)
00137 {
00138     PPPDCB *dcb = dev->dev_dcb;
00139 
00140     dcb->dcb_compr = 0;
00141     dcb->dcb_auth = PPP_PAP;
00142     dcb->dcb_neg_magic = new_magic;
00143     dcb->dcb_loc_magic = 0;
00144     dcb->dcb_rem_magic = 0;
00145     dcb->dcb_accm = 0xFFffFFff;
00146     dcb->dcb_loc_mru = 1500;
00147 
00148     _ioctl(dcb->dcb_fd, HDLC_SETTXACCM, &dcb->dcb_accm );
00149 }
00150 
00151 /*
00152  * Send a Configure-Request.
00153  */
00154 void LcpTxConfReq(NUTDEVICE * dev, uint8_t id, uint8_t rejected)
00155 {
00156     PPPDCB *dcb = dev->dev_dcb;
00157     XCPOPT *xcpo;
00158     NETBUF *nb;
00159 
00160     /*
00161      * Not currently negotiating, reset options.
00162      */
00163     if (dcb->dcb_lcp_state != PPPS_REQSENT && dcb->dcb_lcp_state != PPPS_ACKRCVD && dcb->dcb_lcp_state != PPPS_ACKSENT) {
00164         LcpResetOptions(dev);
00165         dcb->dcb_lcp_naks = 0;
00166     }
00167     dcb->dcb_acked = 0;
00168     dcb->dcb_retries = 0;
00169 
00170     /*
00171      * Create the request.
00172      */
00173     if ((nb = NutNetBufAlloc(0, NBAF_APPLICATION, rejected ? 6 : 12)) != 0) {
00174         xcpo = nb->nb_ap.vp;
00175         xcpo->xcpo_type = LCP_ASYNCMAP;
00176         xcpo->xcpo_len = 6;
00177         xcpo->xcpo_.ul = htonl(LCP_DEFOPT_ASYNCMAP); /* Should this be "= 0;" instead? */
00178 
00179         /*
00180          * This is a temporary hack. In the initial version
00181          * we sent the ASYNCMAP only and never expected any
00182          * rejects. The MAGICNUMBER had been added later
00183          * to support echo requests, but some servers reject
00184          * this option. Now we still do not provide full
00185          * reject processing but blindly assume, that the 
00186          * MAGICNUMBER is the rejected option.
00187          */
00188         if (!rejected) {
00189             xcpo = (XCPOPT *) ((char *) xcpo + xcpo->xcpo_len);
00190             xcpo->xcpo_type = LCP_MAGICNUMBER;
00191             xcpo->xcpo_len = 6;
00192             xcpo->xcpo_.ul = dcb->dcb_neg_magic;
00193         }
00194 
00195         NutLcpOutput(dev, XCP_CONFREQ, id, nb);
00196     }
00197 }
00198 
00199 /*
00200  * Send a Protocol-Reject for some protocol.
00201  */
00202 void LcpTxProtRej(NUTDEVICE * dev, uint16_t protocol, NETBUF * nb)
00203 {
00204     PPPDCB *dcb = dev->dev_dcb;
00205     NETBUF *nbr;
00206     uint16_t *sp;
00207 
00208     if ((nbr = NutNetBufAlloc(0, NBAF_APPLICATION, nb->nb_nw.sz)) != 0) {
00209         sp = nbr->nb_ap.vp;
00210         *sp++ = htons(protocol);
00211         memcpy(sp, nb->nb_nw.vp, nb->nb_nw.sz - 2);
00212         NutNetBufFree(nb);
00213         NutLcpOutput(dev, LCP_PROTREJ, ++dcb->dcb_rejid, nbr);
00214     } else {
00215         NutNetBufFree(nb);
00216     }
00217 }
00218