lcpout.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Copyright (C) 2001-2004 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/lcpout.c
  37. * \brief PPP LCP output functions.
  38. *
  39. * \verbatim
  40. * $Id: lcpout.c 4602 2012-09-13 09:39:43Z haraldkipp $
  41. * \endverbatim
  42. */
  43. #include <string.h>
  44. #include <dev/ppp.h>
  45. #include <sys/types.h>
  46. #include <sys/heap.h>
  47. #include <netinet/in.h>
  48. #include <net/if_var.h>
  49. #include <netinet/if_ppp.h>
  50. #include <netinet/ppp_fsm.h>
  51. #include <net/ppp.h>
  52. #include <dev/usart.h>
  53. #include <io.h>
  54. /*!
  55. * \addtogroup xgLCP
  56. */
  57. /*@{*/
  58. extern uint32_t new_magic;
  59. /*!
  60. * \brief Send a LCP packet.
  61. *
  62. * \note Applications typically do not call this function.
  63. *
  64. * \param dev Identifies the device to use.
  65. * \param code Type subcode.
  66. * \param id Exchange identifier.
  67. * \param nb Network buffer structure containing the packet to send
  68. * or null if the packet contains no information.
  69. * The structure must have been allocated by a previous
  70. * call NutNetBufAlloc() and will be freed when this function
  71. * returns.
  72. *
  73. * \return 0 on success, -1 in case of any errors.
  74. */
  75. int NutLcpOutput(NUTDEVICE * dev, uint8_t code, uint8_t id, NETBUF * nb)
  76. {
  77. XCPHDR *lcp;
  78. if ((nb = NutNetBufAlloc(nb, NBAF_NETWORK, sizeof(XCPHDR))) == 0)
  79. return -1;
  80. lcp = nb->nb_nw.vp;
  81. lcp->xch_code = code;
  82. lcp->xch_id = id;
  83. lcp->xch_len = htons(nb->nb_nw.sz + nb->nb_tp.sz + nb->nb_ap.sz);
  84. if (NutPppOutput(dev, PPP_LCP, 0, nb) == 0)
  85. NutNetBufFree(nb);
  86. return 0;
  87. }
  88. static INLINE void LcpResetOptions(NUTDEVICE * dev)
  89. {
  90. PPPDCB *dcb = dev->dev_dcb;
  91. dcb->dcb_compr = 0;
  92. if ((dcb->dcb_user && *dcb->dcb_user) || (dcb->dcb_pass && *dcb->dcb_pass)) {
  93. dcb->dcb_auth = PPP_PAP;
  94. } else {
  95. dcb->dcb_auth = 0;
  96. }
  97. dcb->dcb_neg_magic = new_magic;
  98. dcb->dcb_loc_magic = 0;
  99. dcb->dcb_rem_magic = 0;
  100. dcb->dcb_accm = 0xFFffFFff;
  101. dcb->dcb_loc_mru = 1500;
  102. _ioctl(dcb->dcb_fd, HDLC_SETTXACCM, &dcb->dcb_accm );
  103. }
  104. /*
  105. * Send a Configure-Request.
  106. */
  107. void LcpTxConfReq(NUTDEVICE * dev, uint8_t id, uint8_t rejected)
  108. {
  109. PPPDCB *dcb = dev->dev_dcb;
  110. XCPOPT *xcpo;
  111. NETBUF *nb;
  112. /*
  113. * Not currently negotiating, reset options.
  114. */
  115. if (dcb->dcb_lcp_state != PPPS_REQSENT && dcb->dcb_lcp_state != PPPS_ACKRCVD && dcb->dcb_lcp_state != PPPS_ACKSENT) {
  116. LcpResetOptions(dev);
  117. dcb->dcb_lcp_naks = 0;
  118. }
  119. dcb->dcb_acked = 0;
  120. dcb->dcb_retries = 0;
  121. /*
  122. * Create the request.
  123. */
  124. if ((nb = NutNetBufAlloc(0, NBAF_APPLICATION, rejected ? 6 : 12)) != 0) {
  125. xcpo = nb->nb_ap.vp;
  126. xcpo->xcpo_type = LCP_ASYNCMAP;
  127. xcpo->xcpo_len = 6;
  128. xcpo->xcpo_.ul = htonl(LCP_DEFOPT_ASYNCMAP); /* Should this be "= 0;" instead? */
  129. /*
  130. * This is a temporary hack. In the initial version
  131. * we sent the ASYNCMAP only and never expected any
  132. * rejects. The MAGICNUMBER had been added later
  133. * to support echo requests, but some servers reject
  134. * this option. Now we still do not provide full
  135. * reject processing but blindly assume, that the
  136. * MAGICNUMBER is the rejected option.
  137. */
  138. if (!rejected) {
  139. xcpo = (XCPOPT *) ((char *) xcpo + xcpo->xcpo_len);
  140. xcpo->xcpo_type = LCP_MAGICNUMBER;
  141. xcpo->xcpo_len = 6;
  142. xcpo->xcpo_.ul = dcb->dcb_neg_magic;
  143. }
  144. NutLcpOutput(dev, XCP_CONFREQ, id, nb);
  145. }
  146. }
  147. /*
  148. * Send a Protocol-Reject for some protocol.
  149. */
  150. void LcpTxProtRej(NUTDEVICE * dev, uint16_t protocol, NETBUF * nb)
  151. {
  152. PPPDCB *dcb = dev->dev_dcb;
  153. NETBUF *nbr;
  154. uint16_t *sp;
  155. if ((nbr = NutNetBufAlloc(0, NBAF_APPLICATION, nb->nb_nw.sz)) != 0) {
  156. sp = nbr->nb_ap.vp;
  157. *sp++ = htons(protocol);
  158. memcpy(sp, nb->nb_nw.vp, nb->nb_nw.sz - 2);
  159. NutNetBufFree(nb);
  160. NutLcpOutput(dev, LCP_PROTREJ, ++dcb->dcb_rejid, nbr);
  161. } else {
  162. NutNetBufFree(nb);
  163. }
  164. }
  165. /*@}*/