papin.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright (C) 2001-2003 by egnite Software GmbH
  3. * Copyright (c) 1989 by Carnegie Mellon University
  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.ethernut.de/
  34. */
  35. /*!
  36. * \file net/papin.c
  37. * \brief PPP PAP output functions.
  38. *
  39. * \verbatim
  40. * $Id: papin.c 5505 2014-01-01 11:15:16Z mifi $
  41. * \endverbatim
  42. */
  43. #include <dev/ppp.h>
  44. #include <sys/types.h>
  45. #include <net/if_var.h>
  46. #include <netinet/if_ppp.h>
  47. #include <netinet/ppp_fsm.h>
  48. #include <netinet/in.h>
  49. /*!
  50. * \addtogroup xgPAP
  51. *
  52. */
  53. /*@{*/
  54. /*
  55. * No server support.
  56. */
  57. void PapRxAuthReq(NUTDEVICE *dev, uint8_t id, NETBUF *nb)
  58. {
  59. NutPapOutput(dev, XCP_CONFACK, id, nb);
  60. }
  61. void PapRxAuthAck(NUTDEVICE *dev, uint8_t id, NETBUF *nb)
  62. {
  63. PPPDCB *dcb = dev->dev_dcb;
  64. (void)id;
  65. (void)nb;
  66. if(dcb->dcb_auth_state == PAPCS_AUTHREQ) {
  67. /*
  68. * Flag us open and start the network.
  69. */
  70. dcb->dcb_auth_state = PAPCS_OPEN;
  71. IpcpLowerUp(dev);
  72. }
  73. }
  74. void PapRxAuthNak(NUTDEVICE *dev, uint8_t id, NETBUF *nb)
  75. {
  76. PPPDCB *dcb = dev->dev_dcb;
  77. (void)id;
  78. (void)nb;
  79. if(dcb->dcb_auth_state == PAPCS_AUTHREQ) {
  80. dcb->dcb_auth_state = PAPCS_BADAUTH;
  81. IpcpLowerDown(dev);
  82. }
  83. }
  84. /*!
  85. * \brief Handle incoming PAP packets.
  86. *
  87. *
  88. * \param dev Identifies the device that received the packet.
  89. * \param nb Pointer to a network buffer structure containing
  90. * the PAP packet.
  91. */
  92. void NutPapInput(NUTDEVICE * dev, NETBUF * nb)
  93. {
  94. XCPHDR *pap;
  95. uint16_t len;
  96. /*
  97. * Drop packets with illegal lengths.
  98. */
  99. if (nb->nb_nw.sz < sizeof(XCPHDR)) {
  100. NutNetBufFree(nb);
  101. return;
  102. }
  103. pap = (XCPHDR *) nb->nb_nw.vp;
  104. if ((len = htons(pap->xch_len)) < sizeof(XCPHDR) || len > nb->nb_nw.sz) {
  105. NutNetBufFree(nb);
  106. return;
  107. }
  108. /*
  109. * Split the PAP packet.
  110. */
  111. nb->nb_ap.vp = pap + 1;
  112. nb->nb_ap.sz = htons(pap->xch_len) - sizeof(XCPHDR);
  113. /*
  114. * Action depends on code.
  115. */
  116. switch (pap->xch_code) {
  117. case XCP_CONFREQ:
  118. PapRxAuthReq(dev, pap->xch_id, nb);
  119. break;
  120. case XCP_CONFACK:
  121. PapRxAuthAck(dev, pap->xch_id, nb);
  122. break;
  123. case XCP_CONFNAK:
  124. PapRxAuthNak(dev, pap->xch_id, nb);
  125. break;
  126. default:
  127. break;
  128. }
  129. NutNetBufFree(nb);
  130. }
  131. /*@}*/