print-pppoe.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that: (1) source code distributions
  7. * retain the above copyright notice and this paragraph in its entirety, (2)
  8. * distributions including binary code include the above copyright notice and
  9. * this paragraph in its entirety in the documentation or other materials
  10. * provided with the distribution, and (3) all advertising materials mentioning
  11. * features or use of this software display the following acknowledgement:
  12. * ``This product includes software developed by the University of California,
  13. * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  14. * the University nor the names of its contributors may be used to endorse
  15. * or promote products derived from this software without specific prior
  16. * written permission.
  17. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  18. * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  19. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  20. *
  21. * Original code by Greg Stark <gsstark@mit.edu>
  22. */
  23. /* \summary: PPP-over-Ethernet (PPPoE) printer */
  24. #ifdef HAVE_CONFIG_H
  25. #include "config.h"
  26. #endif
  27. #include <netdissect-stdinc.h>
  28. #include "netdissect.h"
  29. #include "extract.h"
  30. /* Codes */
  31. enum {
  32. PPPOE_PADI = 0x09,
  33. PPPOE_PADO = 0x07,
  34. PPPOE_PADR = 0x19,
  35. PPPOE_PADS = 0x65,
  36. PPPOE_PADT = 0xa7
  37. };
  38. static const struct tok pppoecode2str[] = {
  39. { PPPOE_PADI, "PADI" },
  40. { PPPOE_PADO, "PADO" },
  41. { PPPOE_PADR, "PADR" },
  42. { PPPOE_PADS, "PADS" },
  43. { PPPOE_PADT, "PADT" },
  44. { 0, "" }, /* PPP Data */
  45. { 0, NULL }
  46. };
  47. /* Tags */
  48. enum {
  49. PPPOE_EOL = 0,
  50. PPPOE_SERVICE_NAME = 0x0101,
  51. PPPOE_AC_NAME = 0x0102,
  52. PPPOE_HOST_UNIQ = 0x0103,
  53. PPPOE_AC_COOKIE = 0x0104,
  54. PPPOE_VENDOR = 0x0105,
  55. PPPOE_RELAY_SID = 0x0110,
  56. PPPOE_MAX_PAYLOAD = 0x0120,
  57. PPPOE_SERVICE_NAME_ERROR = 0x0201,
  58. PPPOE_AC_SYSTEM_ERROR = 0x0202,
  59. PPPOE_GENERIC_ERROR = 0x0203
  60. };
  61. static const struct tok pppoetag2str[] = {
  62. { PPPOE_EOL, "EOL" },
  63. { PPPOE_SERVICE_NAME, "Service-Name" },
  64. { PPPOE_AC_NAME, "AC-Name" },
  65. { PPPOE_HOST_UNIQ, "Host-Uniq" },
  66. { PPPOE_AC_COOKIE, "AC-Cookie" },
  67. { PPPOE_VENDOR, "Vendor-Specific" },
  68. { PPPOE_RELAY_SID, "Relay-Session-ID" },
  69. { PPPOE_MAX_PAYLOAD, "PPP-Max-Payload" },
  70. { PPPOE_SERVICE_NAME_ERROR, "Service-Name-Error" },
  71. { PPPOE_AC_SYSTEM_ERROR, "AC-System-Error" },
  72. { PPPOE_GENERIC_ERROR, "Generic-Error" },
  73. { 0, NULL }
  74. };
  75. #define PPPOE_HDRLEN 6
  76. #define MAXTAGPRINT 80
  77. u_int
  78. pppoe_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, register const u_char *p)
  79. {
  80. return (pppoe_print(ndo, p, h->len));
  81. }
  82. u_int
  83. pppoe_print(netdissect_options *ndo, register const u_char *bp, u_int length)
  84. {
  85. uint16_t pppoe_ver, pppoe_type, pppoe_code, pppoe_sessionid;
  86. u_int pppoe_length;
  87. const u_char *pppoe_packet, *pppoe_payload;
  88. if (length < PPPOE_HDRLEN) {
  89. ND_PRINT((ndo, "truncated-pppoe %u", length));
  90. return (length);
  91. }
  92. length -= PPPOE_HDRLEN;
  93. pppoe_packet = bp;
  94. ND_TCHECK2(*pppoe_packet, PPPOE_HDRLEN);
  95. pppoe_ver = (pppoe_packet[0] & 0xF0) >> 4;
  96. pppoe_type = (pppoe_packet[0] & 0x0F);
  97. pppoe_code = pppoe_packet[1];
  98. pppoe_sessionid = EXTRACT_16BITS(pppoe_packet + 2);
  99. pppoe_length = EXTRACT_16BITS(pppoe_packet + 4);
  100. pppoe_payload = pppoe_packet + PPPOE_HDRLEN;
  101. if (pppoe_ver != 1) {
  102. ND_PRINT((ndo, " [ver %d]",pppoe_ver));
  103. }
  104. if (pppoe_type != 1) {
  105. ND_PRINT((ndo, " [type %d]",pppoe_type));
  106. }
  107. ND_PRINT((ndo, "PPPoE %s", tok2str(pppoecode2str, "PAD-%x", pppoe_code)));
  108. if (pppoe_code == PPPOE_PADI && pppoe_length > 1484 - PPPOE_HDRLEN) {
  109. ND_PRINT((ndo, " [len %u!]",pppoe_length));
  110. }
  111. if (pppoe_length > length) {
  112. ND_PRINT((ndo, " [len %u > %u!]", pppoe_length, length));
  113. pppoe_length = length;
  114. }
  115. if (pppoe_sessionid) {
  116. ND_PRINT((ndo, " [ses 0x%x]", pppoe_sessionid));
  117. }
  118. if (pppoe_code) {
  119. /* PPP session packets don't contain tags */
  120. u_short tag_type = 0xffff, tag_len;
  121. const u_char *p = pppoe_payload;
  122. /*
  123. * loop invariant:
  124. * p points to current tag,
  125. * tag_type is previous tag or 0xffff for first iteration
  126. */
  127. while (tag_type && p < pppoe_payload + pppoe_length) {
  128. ND_TCHECK2(*p, 4);
  129. tag_type = EXTRACT_16BITS(p);
  130. tag_len = EXTRACT_16BITS(p + 2);
  131. p += 4;
  132. /* p points to tag_value */
  133. if (tag_len) {
  134. unsigned ascii_count = 0, garbage_count = 0;
  135. const u_char *v;
  136. char tag_str[MAXTAGPRINT];
  137. unsigned tag_str_len = 0;
  138. /* TODO print UTF-8 decoded text */
  139. ND_TCHECK2(*p, tag_len);
  140. for (v = p; v < p + tag_len && tag_str_len < MAXTAGPRINT-1; v++)
  141. if (*v >= 32 && *v < 127) {
  142. tag_str[tag_str_len++] = *v;
  143. ascii_count++;
  144. } else {
  145. tag_str[tag_str_len++] = '.';
  146. garbage_count++;
  147. }
  148. tag_str[tag_str_len] = 0;
  149. if (ascii_count > garbage_count) {
  150. ND_PRINT((ndo, " [%s \"%*.*s\"]",
  151. tok2str(pppoetag2str, "TAG-0x%x", tag_type),
  152. (int)tag_str_len,
  153. (int)tag_str_len,
  154. tag_str));
  155. } else {
  156. /* Print hex, not fast to abuse printf but this doesn't get used much */
  157. ND_PRINT((ndo, " [%s 0x", tok2str(pppoetag2str, "TAG-0x%x", tag_type)));
  158. for (v=p; v<p+tag_len; v++) {
  159. ND_PRINT((ndo, "%02X", *v));
  160. }
  161. ND_PRINT((ndo, "]"));
  162. }
  163. } else
  164. ND_PRINT((ndo, " [%s]", tok2str(pppoetag2str,
  165. "TAG-0x%x", tag_type)));
  166. p += tag_len;
  167. /* p points to next tag */
  168. }
  169. return (0);
  170. } else {
  171. /* PPPoE data */
  172. ND_PRINT((ndo, " "));
  173. return (PPPOE_HDRLEN + ppp_print(ndo, pppoe_payload, pppoe_length));
  174. }
  175. trunc:
  176. ND_PRINT((ndo, "[|pppoe]"));
  177. return (PPPOE_HDRLEN);
  178. }