pppdebug.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*
  2. * Copyright (C) 2001-2004 by egnite Software GmbH
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the copyright holders nor the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * For additional information see http://www.ethernut.de/
  33. *
  34. */
  35. /*!
  36. * \file net/pppdebug.c
  37. * \brief PPP debug helper functions.
  38. *
  39. * \verbatim
  40. * $Id: pppdebug.c 5101 2013-04-30 13:14:20Z olereinhardt $
  41. * \endverbatim
  42. */
  43. #include <net/netdebug.h>
  44. #include <sys/types.h>
  45. #include <dev/ahdlc.h>
  46. #include <arpa/inet.h>
  47. #include <netinet/ppp_fsm.h>
  48. #include <netinet/if_ppp.h>
  49. #include <netinet/in.h>
  50. FILE *__ppp_trs; /*!< \brief PPP trace output stream. */
  51. uint_fast8_t __ppp_trf; /*!< \brief PPP trace flags. */
  52. static uint8_t ppp_header_sz; /* Size of the PPP header. */
  53. static const char dbg_confreq[] PROGMEM = "[CONFREQ]";
  54. static const char dbg_confack[] PROGMEM = "[CONFACK]";
  55. static const char dbg_confnak[] PROGMEM = "[CONFNAK]";
  56. static const char dbg_confrej[] PROGMEM = "[CONFREJ]";
  57. static const char dbg_termreq[] PROGMEM = "[TERMREQ]";
  58. static const char dbg_termack[] PROGMEM = "[TERMACK]";
  59. static const char dbg_coderej[] PROGMEM = "[CODEREJ]";
  60. static const char dbg_protrej[] PROGMEM = "[PROTREJ]";
  61. static const char dbg_echoreq[] PROGMEM = "[ECHOREQ]";
  62. static const char dbg_echorsp[] PROGMEM = "[ECHORSP]";
  63. static const char dbg_discreq[] PROGMEM = "[DISCREQ]";
  64. void NutDumpLcpOption(FILE * stream, NETBUF * nb)
  65. {
  66. XCPOPT *xcpo;
  67. uint16_t len;
  68. if ((len = nb->nb_ap.sz) != 0)
  69. xcpo = nb->nb_ap.vp;
  70. else {
  71. len = nb->nb_dl.sz - ppp_header_sz - sizeof(XCPHDR);
  72. xcpo = (XCPOPT *) (((char *) nb->nb_dl.vp) + ppp_header_sz + sizeof(XCPHDR));
  73. }
  74. fprintf(stream, "[OPT(%u)]", len);
  75. while (len) {
  76. switch (xcpo->xcpo_type) {
  77. case LCP_MRU:
  78. fprintf(stream, "[MRU=%u]", ntohs(xcpo->xcpo_.us));
  79. break;
  80. case LCP_ASYNCMAP:
  81. fprintf(stream, "[ACCM=0x%08lX]", ntohl(xcpo->xcpo_.ul));
  82. break;
  83. case LCP_AUTHTYPE:
  84. fprintf(stream, "[AUTH=0x%04X]", ntohs(xcpo->xcpo_.us));
  85. break;
  86. case LCP_MAGICNUMBER:
  87. fprintf(stream, "[MAGIC=0x%08lX]", ntohl(xcpo->xcpo_.ul));
  88. break;
  89. case LCP_PCOMPRESSION:
  90. fputs("[PCOMP]", stream);
  91. break;
  92. case LCP_ACCOMPRESSION:
  93. fputs("[ACOMP]", stream);
  94. break;
  95. default:
  96. fprintf(stream, "[OPT%u?]", xcpo->xcpo_type);
  97. break;
  98. }
  99. if (len < xcpo->xcpo_len) {
  100. fputs("[LEN?]", stream);
  101. break;
  102. }
  103. len -= xcpo->xcpo_len;
  104. xcpo = (XCPOPT *) ((char *) xcpo + xcpo->xcpo_len);
  105. }
  106. }
  107. void NutDumpLcp(FILE * stream, NETBUF * nb)
  108. {
  109. XCPHDR *lcp;
  110. uint16_t len;
  111. if ((len = nb->nb_nw.sz) != 0) {
  112. len = nb->nb_nw.sz + nb->nb_ap.sz;
  113. lcp = nb->nb_nw.vp;
  114. } else {
  115. len = nb->nb_dl.sz - ppp_header_sz;
  116. lcp = (XCPHDR *) (((char *) nb->nb_dl.vp) + ppp_header_sz);
  117. }
  118. fprintf(stream, "[LCP-%03u(%u)]", lcp->xch_id, ntohs(lcp->xch_len));
  119. if (len < sizeof(XCPHDR)) {
  120. fputs("[LEN?]", stream);
  121. return;
  122. }
  123. switch (lcp->xch_code) {
  124. case XCP_CONFREQ:
  125. fputs_P(dbg_confreq, stream);
  126. NutDumpLcpOption(stream, nb);
  127. break;
  128. case XCP_CONFACK:
  129. fputs_P(dbg_confack, stream);
  130. NutDumpLcpOption(stream, nb);
  131. break;
  132. case XCP_CONFNAK:
  133. fputs_P(dbg_confnak, stream);
  134. NutDumpLcpOption(stream, nb);
  135. break;
  136. case XCP_CONFREJ:
  137. fputs_P(dbg_confrej, stream);
  138. NutDumpLcpOption(stream, nb);
  139. break;
  140. case XCP_TERMREQ:
  141. fputs_P(dbg_termreq, stream);
  142. break;
  143. case XCP_TERMACK:
  144. fputs_P(dbg_termack, stream);
  145. break;
  146. case XCP_CODEREJ:
  147. fputs_P(dbg_coderej, stream);
  148. break;
  149. case LCP_PROTREJ:
  150. fputs_P(dbg_protrej, stream);
  151. break;
  152. case LCP_ERQ:
  153. fputs_P(dbg_echoreq, stream);
  154. break;
  155. case LCP_ERP:
  156. fputs_P(dbg_echorsp, stream);
  157. break;
  158. case LCP_DRQ:
  159. fputs_P(dbg_discreq, stream);
  160. break;
  161. default:
  162. fprintf(stream, "[CODE%u?]", lcp->xch_code);
  163. break;
  164. }
  165. }
  166. void NutDumpPapOption(FILE * stream, NETBUF * nb)
  167. {
  168. char *xcpo;
  169. uint16_t len;
  170. uint8_t i;
  171. if ((len = nb->nb_ap.sz) != 0)
  172. xcpo = nb->nb_ap.vp;
  173. else {
  174. len = nb->nb_dl.sz - ppp_header_sz - sizeof(XCPHDR);
  175. xcpo = ((char *) nb->nb_dl.vp) + ppp_header_sz + sizeof(XCPHDR);
  176. }
  177. fprintf(stream, "[OPT(%u)]", len);
  178. while (len) {
  179. if (*xcpo) {
  180. fputc('[', stream);
  181. for (i = 1; i <= *xcpo; i++)
  182. fputc(*(xcpo + i), stream);
  183. fputc(']', stream);
  184. }
  185. if (len < (uint16_t) (*xcpo + 1)) {
  186. fputs("[LEN?]", stream);
  187. break;
  188. }
  189. len -= *xcpo + 1;
  190. xcpo = xcpo + *xcpo + 1;
  191. }
  192. }
  193. void NutDumpPap(FILE * stream, NETBUF * nb)
  194. {
  195. XCPHDR *pap;
  196. uint16_t len;
  197. if ((len = nb->nb_nw.sz) != 0)
  198. pap = nb->nb_nw.vp;
  199. else {
  200. len = nb->nb_dl.sz - ppp_header_sz;
  201. pap = (XCPHDR *) (((char *) nb->nb_dl.vp) + ppp_header_sz);
  202. }
  203. fprintf(stream, "[PAP-%03u(%u)]", pap->xch_id, ntohs(pap->xch_len));
  204. if (len < sizeof(XCPHDR)) {
  205. fputs("[LEN?]", stream);
  206. return;
  207. }
  208. switch (pap->xch_code) {
  209. case XCP_CONFREQ:
  210. fputs_P(dbg_confreq, stream);
  211. NutDumpPapOption(stream, nb);
  212. break;
  213. case XCP_CONFACK:
  214. fputs_P(dbg_confack, stream);
  215. break;
  216. case XCP_CONFNAK:
  217. fputs_P(dbg_confnak, stream);
  218. break;
  219. default:
  220. fprintf(stream, "[CODE%u?]", pap->xch_code);
  221. break;
  222. }
  223. }
  224. void NutDumpIpcpOption(FILE * stream, NETBUF * nb)
  225. {
  226. XCPOPT *xcpo = NULL;
  227. uint16_t len;
  228. if ((len = nb->nb_ap.sz) != 0)
  229. xcpo = nb->nb_ap.vp;
  230. else {
  231. if ((nb->nb_dl.sz)>(ppp_header_sz + sizeof(XCPHDR)))
  232. {
  233. len = nb->nb_dl.sz - ppp_header_sz - sizeof(XCPHDR);
  234. xcpo = (XCPOPT *) (((char *) nb->nb_dl.vp) + ppp_header_sz + sizeof(XCPHDR));
  235. }
  236. else len=0;
  237. }
  238. fprintf(stream, "[OPT(%u)]", len);
  239. while (len) {
  240. switch (xcpo->xcpo_type) {
  241. case IPCP_ADDR:
  242. fprintf(stream, "[ADDR=%s]", inet_ntoa(xcpo->xcpo_.ul));
  243. break;
  244. case IPCP_COMPRESSTYPE:
  245. fputs("[COMP]", stream);
  246. break;
  247. case IPCP_MS_DNS1:
  248. fprintf(stream, "[DNS1=%s]", inet_ntoa(xcpo->xcpo_.ul));
  249. break;
  250. case IPCP_MS_DNS2:
  251. fprintf(stream, "[DNS2=%s]", inet_ntoa(xcpo->xcpo_.ul));
  252. break;
  253. default:
  254. fprintf(stream, "[OPT%u?]", xcpo->xcpo_type);
  255. break;
  256. }
  257. if (len < xcpo->xcpo_len) {
  258. fputs("[LEN?]", stream);
  259. break;
  260. }
  261. len -= xcpo->xcpo_len;
  262. xcpo = (XCPOPT *) ((char *) xcpo + xcpo->xcpo_len);
  263. }
  264. }
  265. void NutDumpIpcp(FILE * stream, NETBUF * nb)
  266. {
  267. XCPHDR *ipcp;
  268. uint16_t len;
  269. if ((len = nb->nb_nw.sz) != 0)
  270. ipcp = nb->nb_nw.vp;
  271. else {
  272. len = nb->nb_dl.sz - ppp_header_sz;
  273. ipcp = (XCPHDR *) (((char *) nb->nb_dl.vp) + ppp_header_sz);
  274. }
  275. fprintf(stream, "[IPCP-%03u(%u)]", ipcp->xch_id, ntohs(ipcp->xch_len));
  276. if (len < sizeof(XCPHDR)) {
  277. fputs("[LEN?]", stream);
  278. return;
  279. }
  280. switch (ipcp->xch_code) {
  281. case XCP_CONFREQ:
  282. fputs_P(dbg_confreq, stream);
  283. NutDumpIpcpOption(stream, nb);
  284. break;
  285. case XCP_CONFACK:
  286. fputs_P(dbg_confack, stream);
  287. NutDumpIpcpOption(stream, nb);
  288. break;
  289. case XCP_CONFNAK:
  290. fputs_P(dbg_confnak, stream);
  291. NutDumpIpcpOption(stream, nb);
  292. break;
  293. case XCP_CONFREJ:
  294. fputs_P(dbg_confrej, stream);
  295. NutDumpIpcpOption(stream, nb);
  296. break;
  297. case XCP_TERMREQ:
  298. fputs_P(dbg_termreq, stream);
  299. break;
  300. case XCP_TERMACK:
  301. fputs_P(dbg_termack, stream);
  302. break;
  303. case XCP_CODEREJ:
  304. fputs_P(dbg_coderej, stream);
  305. break;
  306. default:
  307. fprintf(stream, "[CODE%u?]", ipcp->xch_code);
  308. break;
  309. }
  310. }
  311. void NutDumpPpp(FILE * stream, NETBUF * nb)
  312. {
  313. PPPHDR *ph = (PPPHDR *) nb->nb_dl.vp;
  314. uint16_t protocol;
  315. fprintf(stream, "(%u)", nb->nb_dl.sz + nb->nb_nw.sz + nb->nb_tp.sz + nb->nb_ap.sz);
  316. /*
  317. * Check if the address and control field is compressed.
  318. * Thanks to Francois Rademeyer.
  319. */
  320. if (ph->address != AHDLC_ALLSTATIONS) {
  321. /*
  322. * Check for protocol compression.
  323. * LSB of 2nd octet for protocol is always 1.
  324. */
  325. if (((uint8_t *) nb->nb_dl.vp)[0] & 0x01) {
  326. ppp_header_sz = 1;
  327. protocol = *(uint8_t *) nb->nb_dl.vp;
  328. } else {
  329. char *cp = (char *)nb->nb_dl.vp;
  330. ppp_header_sz = 2;
  331. protocol = ntohs(((uint16_t)cp[0] << 8) | cp[1]);
  332. }
  333. } else {
  334. ppp_header_sz = sizeof(PPPHDR);
  335. protocol = ntohs(ph->prot_type);
  336. }
  337. if (nb->nb_dl.sz < ppp_header_sz) {
  338. fputs("[LEN?]", stream);
  339. return;
  340. }
  341. switch (protocol) {
  342. case PPP_IP:
  343. break;
  344. case PPP_LCP:
  345. NutDumpLcp(stream, nb);
  346. break;
  347. case PPP_IPCP:
  348. NutDumpIpcp(stream, nb);
  349. break;
  350. case PPP_PAP:
  351. NutDumpPap(stream, nb);
  352. break;
  353. default:
  354. fprintf(stream, "[TYPE 0x%04X?]", ntohs(ph->prot_type));
  355. break;
  356. }
  357. }
  358. /*!
  359. * \brief Control PPP tracing.
  360. *
  361. * \param stream Pointer to a previously opened stream or null to
  362. * disable trace output.
  363. * \param flags Flags to enable specific traces.
  364. */
  365. void NutTracePPP(FILE * stream, uint8_t flags)
  366. {
  367. if (stream)
  368. __ppp_trs = stream;
  369. if (__ppp_trs)
  370. __ppp_trf = flags;
  371. else
  372. __ppp_trf = 0;
  373. }