pppin.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*!
  2. * Copyright (C) 2002 by Call Direct Cellular Solutions Pty. Ltd.
  3. * Copyright (C) 2001-2004 by egnite Software GmbH. All rights reserved
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the name of the copyright holders nor the names of
  17. * contributors may be used to endorse or promote products derived
  18. * from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  27. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  28. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  30. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. *
  33. * For additional information see http://www.calldirect.com.au/
  34. * For additional information see http://www.ethernut.de/
  35. */
  36. /*!
  37. * \file net/pppin.c
  38. * \brief PPP input functions.
  39. *
  40. * \verbatim
  41. * $Id: pppin.c 4602 2012-09-13 09:39:43Z haraldkipp $
  42. * \endverbatim
  43. */
  44. #include <cfg/os.h>
  45. #include <dev/ppp.h>
  46. #include <dev/ahdlc.h>
  47. #include <netinet/in.h>
  48. #include <netinet/if_ppp.h>
  49. #include <netinet/ppp_fsm.h>
  50. #include <netinet/ip.h>
  51. #include <net/ppp.h>
  52. #include <sys/types.h>
  53. #include <sys/timer.h>
  54. #ifdef NUTDEBUG
  55. #include <net/netdebug.h>
  56. #endif
  57. /*!
  58. * \addtogroup xgPPP
  59. */
  60. /*@{*/
  61. /*!
  62. * \brief Handle incoming PPP frames.
  63. *
  64. * Splits the PPP frame into the data link and the network part.
  65. * Then the frame is routed to the proper handler, based on the
  66. * type field in the header.
  67. *
  68. * \note This routine is called by the device driver on incoming
  69. * PPP frames. Applications typically do not call this function.
  70. *
  71. * \param dev Identifies the device that received the frame.
  72. * \param nb Pointer to a network buffer structure containing
  73. * the PPP frame.
  74. */
  75. void NutPppInput(NUTDEVICE * dev, NETBUF * nb)
  76. {
  77. PPPHDR *ph = (PPPHDR *) nb->nb_dl.vp;
  78. PPPDCB *dcb = dev->dev_dcb;
  79. uint16_t protocol;
  80. uint8_t protocolsz;
  81. #ifdef NUTDEBUG
  82. if (__ppp_trf) {
  83. fputs("\nPPP>", __ppp_trs);
  84. NutDumpPpp(__ppp_trs, nb);
  85. }
  86. #elif defined(__IMAGECRAFT__)
  87. /*
  88. * No idea what this is, but ICCAVR fails if this call isn't there.
  89. */
  90. NutSleep(100);
  91. #endif
  92. /*
  93. * Check if the address and control field is compressed.
  94. * Thanks to Francois Rademeyer.
  95. */
  96. if (ph->address != AHDLC_ALLSTATIONS) {
  97. /*
  98. * Check for protocol compression.
  99. * LSB of 2nd octet for protocol is always 1.
  100. */
  101. if (((uint8_t *) nb->nb_dl.vp)[0] & 0x01) {
  102. protocolsz = 1;
  103. protocol = *(uint8_t *) nb->nb_dl.vp;
  104. } else {
  105. char *cp = (char *)nb->nb_dl.vp;
  106. protocolsz = 2;
  107. protocol = ntohs(((uint16_t)cp[0] << 8) | cp[1]);
  108. }
  109. /*
  110. * Chop off the compressed header.
  111. */
  112. nb->nb_nw.vp = (char *)nb->nb_dl.vp + protocolsz;
  113. nb->nb_nw.sz = nb->nb_dl.sz - protocolsz;
  114. nb->nb_dl.sz = protocolsz;
  115. } else {
  116. /*
  117. * Chop off the PPP header.
  118. */
  119. nb->nb_nw.vp = ph + 1;
  120. nb->nb_nw.sz = nb->nb_dl.sz - sizeof(PPPHDR);
  121. nb->nb_dl.sz = sizeof(PPPHDR);
  122. protocol = ntohs(ph->prot_type);
  123. }
  124. /*
  125. * Toss all non-LCP packets unless LCP is OPEN.
  126. */
  127. if (protocol != PPP_LCP && dcb->dcb_lcp_state != PPPS_OPENED) {
  128. NutNetBufFree(nb);
  129. return;
  130. }
  131. /*
  132. * Until we get past the authentication phase, toss all packets
  133. * except LCP, LQR and authentication packets.
  134. */
  135. if (dcb->dcb_auth == PPP_PAP && dcb->dcb_auth_state != PAPCS_OPEN &&
  136. !(protocol == PPP_LCP || protocol == PPP_LQR || protocol == PPP_PAP || protocol == PPP_CHAP)) {
  137. NutNetBufFree(nb);
  138. return;
  139. }
  140. /*
  141. * Route frame to the proper handler.
  142. */
  143. switch (protocol) {
  144. case PPP_IP:
  145. /* Internet protocol. */
  146. if (dcb->dcb_ipcp_state == PPPS_OPENED)
  147. NutIpInput(dev, nb);
  148. else
  149. NutNetBufFree(nb);
  150. break;
  151. case PPP_LCP:
  152. /* Link control protocol. */
  153. NutLcpInput(dev, nb);
  154. break;
  155. case PPP_IPCP:
  156. /* IP control protocol. */
  157. NutIpcpInput(dev, nb);
  158. break;
  159. case PPP_PAP:
  160. /* Password authentication protocol. */
  161. NutPapInput(dev, nb);
  162. break;
  163. default:
  164. LcpTxProtRej(dev, protocol, nb);
  165. break;
  166. }
  167. }
  168. /*@}*/