]> The Tcpdump Group git mirrors - tcpdump/blobdiff - print-pppoe.c
bgp: Parse BGP extended message support capability
[tcpdump] / print-pppoe.c
index 5369a9834859dda18d4e029f64ccb23d465b27c7..65518dff16d97adde75b1d7eb537d602354f7998 100644 (file)
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * Original code by Greg Stark <[email protected]>
  */
 
-#ifndef lint
-static const char rcsid[] =
-"@(#) $Header: /tcpdump/master/tcpdump/print-pppoe.c,v 1.14 2001-06-20 07:40:44 guy Exp $ (LBL)";
-#endif
+/* \summary: PPP-over-Ethernet (PPPoE) printer */
 
 #ifdef HAVE_CONFIG_H
-#include "config.h"
+#include <config.h>
 #endif
 
-#include <sys/param.h>
-#include <sys/time.h>
-#include <sys/socket.h>
-
-#include <netinet/in.h>
+#include "netdissect-stdinc.h"
 
-#include <stdio.h>
-#include <string.h>
+#include "netdissect-ctype.h"
 
-#include "interface.h"
-#include "addrtoname.h"
-#include "ppp.h"
-#include "ethertype.h"
-#include "ether.h"
-#include "extract.h"                   /* must come after interface.h */
+#define ND_LONGJMP_FROM_TCHECK
+#include "netdissect.h"
+#include "extract.h"
 
 /* Codes */
 enum {
@@ -53,7 +44,7 @@ enum {
        PPPOE_PADT = 0xa7
 };
 
-static struct tok pppoecode2str[] = {
+static const struct tok pppoecode2str[] = {
        { PPPOE_PADI, "PADI" },
        { PPPOE_PADO, "PADO" },
        { PPPOE_PADR, "PADR" },
@@ -72,12 +63,13 @@ enum {
        PPPOE_AC_COOKIE = 0x0104,
        PPPOE_VENDOR = 0x0105,
        PPPOE_RELAY_SID = 0x0110,
+       PPPOE_MAX_PAYLOAD = 0x0120,
        PPPOE_SERVICE_NAME_ERROR = 0x0201,
        PPPOE_AC_SYSTEM_ERROR = 0x0202,
        PPPOE_GENERIC_ERROR = 0x0203
 };
 
-static struct tok pppoetag2str[] = {
+static const struct tok pppoetag2str[] = {
        { PPPOE_EOL, "EOL" },
        { PPPOE_SERVICE_NAME, "Service-Name" },
        { PPPOE_AC_NAME, "AC-Name" },
@@ -85,6 +77,7 @@ static struct tok pppoetag2str[] = {
        { PPPOE_AC_COOKIE, "AC-Cookie" },
        { PPPOE_VENDOR, "Vendor-Specific" },
        { PPPOE_RELAY_SID, "Relay-Session-ID" },
+       { PPPOE_MAX_PAYLOAD, "PPP-Max-Payload" },
        { PPPOE_SERVICE_NAME_ERROR, "Service-Name-Error" },
        { PPPOE_AC_SYSTEM_ERROR, "AC-System-Error" },
        { PPPOE_GENERIC_ERROR, "Generic-Error" },
@@ -92,74 +85,54 @@ static struct tok pppoetag2str[] = {
 };
 
 #define PPPOE_HDRLEN 6
+#define MAXTAGPRINT 80
 
 void
-pppoe_if_print(u_char *user, const struct pcap_pkthdr *h,
-            register const u_char *p)
+pppoe_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, const u_char *p)
 {
-       register u_int length = h->len;
-       register u_int caplen = h->caplen;
-
-       ts_print(&h->ts);
-
-       /*
-        * Some printers want to get back at the link level addresses,
-        * and/or check that they're not walking off the end of the packet.
-        * Rather than pass them all the way down, we set these globals.
-        */
-       packetp = p;
-       snapend = p + caplen;
-
-       pppoe_print(p, length);
+       ndo->ndo_protocol = "pppoe";
+       ndo->ndo_ll_hdr_len += pppoe_print(ndo, p, h->len);
 }
 
-void
-pppoe_print(register const u_char *bp, u_int length)
+u_int
+pppoe_print(netdissect_options *ndo, const u_char *bp, u_int length)
 {
-       u_short pppoe_ver, pppoe_type, pppoe_code, pppoe_sessionid, pppoe_length;
+       uint16_t pppoe_ver, pppoe_type, pppoe_code, pppoe_sessionid;
+       u_int pppoe_length;
        const u_char *pppoe_packet, *pppoe_payload;
 
-       pppoe_packet = bp;
-       if (pppoe_packet > snapend) {
-               printf("[|pppoe]");
-               return;
+       ndo->ndo_protocol = "pppoe";
+       if (length < PPPOE_HDRLEN) {
+               ND_PRINT(" (length %u < %u)", length, PPPOE_HDRLEN);
+               goto invalid;
        }
-
-       pppoe_ver  = (pppoe_packet[0] & 0xF0) >> 4;
-       pppoe_type  = (pppoe_packet[0] & 0x0F);
-       pppoe_code = pppoe_packet[1];
-       pppoe_sessionid = EXTRACT_16BITS(pppoe_packet + 2);
-       pppoe_length    = EXTRACT_16BITS(pppoe_packet + 4);
+       length -= PPPOE_HDRLEN;
+       pppoe_packet = bp;
+       ND_TCHECK_LEN(pppoe_packet, PPPOE_HDRLEN);
+       pppoe_ver  = (GET_U_1(pppoe_packet) & 0xF0) >> 4;
+       pppoe_type  = (GET_U_1(pppoe_packet) & 0x0F);
+       pppoe_code = GET_U_1(pppoe_packet + 1);
+       pppoe_sessionid = GET_BE_U_2(pppoe_packet + 2);
+       pppoe_length    = GET_BE_U_2(pppoe_packet + 4);
        pppoe_payload = pppoe_packet + PPPOE_HDRLEN;
 
-       if (snapend < pppoe_payload) {
-               printf(" truncated PPPoE");
-               return;
-       }
-
        if (pppoe_ver != 1) {
-               printf(" [ver %d]",pppoe_ver);
+               ND_PRINT(" [ver %u]",pppoe_ver);
        }
        if (pppoe_type != 1) {
-               printf(" [type %d]",pppoe_type);
+               ND_PRINT(" [type %u]",pppoe_type);
        }
 
-       printf("PPPoE %s", tok2str(pppoecode2str, "PAD-%x", pppoe_code));
+       ND_PRINT("PPPoE %s", tok2str(pppoecode2str, "PAD-%x", pppoe_code));
        if (pppoe_code == PPPOE_PADI && pppoe_length > 1484 - PPPOE_HDRLEN) {
-               printf(" [len %d!]",pppoe_length);
+               ND_PRINT(" [len %u!]",pppoe_length);
        }
-       if (pppoe_sessionid) {
-               printf(" [ses 0x%x]", pppoe_sessionid);
+       if (pppoe_length > length) {
+               ND_PRINT(" [len %u > %u!]", pppoe_length, length);
+               pppoe_length = length;
        }
-
-       if (pppoe_payload + pppoe_length < snapend) {
-#if 0
-               const u_char *x = pppoe_payload + pppoe_length;
-               printf(" [length %d (%d extra bytes)]",
-                   pppoe_length, snapend - pppoe_payload - pppoe_length);
-               default_print(x, snapend - x);
-#endif
-               snapend = pppoe_payload+pppoe_length;
+       if (pppoe_sessionid) {
+               ND_PRINT(" [ses 0x%x]", pppoe_sessionid);
        }
 
        if (pppoe_code) {
@@ -169,46 +142,65 @@ pppoe_print(register const u_char *bp, u_int length)
 
                /*
                 * loop invariant:
-                * p points to next tag,
+                * p points to current tag,
                 * tag_type is previous tag or 0xffff for first iteration
                 */
-               while (tag_type && p + 4 < pppoe_payload + length &&
-                   p + 4 < snapend) {
-                       tag_type = EXTRACT_16BITS(p);
-                       tag_len = EXTRACT_16BITS(p + 2);
+               while (tag_type && p < pppoe_payload + pppoe_length) {
+                       tag_type = GET_BE_U_2(p);
+                       tag_len = GET_BE_U_2(p + 2);
                        p += 4;
                        /* p points to tag_value */
 
                        if (tag_len) {
-                               int isascii = 1;
-                               const u_char *v = p;
-                               u_short l;
-
-                               for (v = p; v < p + tag_len; v++)
-                                       if (*v >= 127 || *v < 32) {
-                                               isascii = 0;
-                                               break;
+                               unsigned ascii_count = 0, garbage_count = 0;
+                               const u_char *v;
+                               char tag_str[MAXTAGPRINT];
+                               unsigned tag_str_len = 0;
+
+                               /* TODO print UTF-8 decoded text */
+                               ND_TCHECK_LEN(p, tag_len);
+                               for (v = p; v < p + tag_len && tag_str_len < MAXTAGPRINT-1; v++)
+                                       if (ND_ASCII_ISPRINT(GET_U_1(v))) {
+                                               tag_str[tag_str_len++] = GET_U_1(v);
+                                               ascii_count++;
+                                       } else {
+                                               tag_str[tag_str_len++] = '.';
+                                               garbage_count++;
+                                       }
+                               tag_str[tag_str_len] = 0;
+
+                               if (ascii_count > garbage_count) {
+                                       ND_PRINT(" [%s \"%*.*s\"]",
+                                              tok2str(pppoetag2str, "TAG-0x%x", tag_type),
+                                              (int)tag_str_len,
+                                              (int)tag_str_len,
+                                              tag_str);
+                               } else {
+                                       /* Print hex, not fast to abuse printf but this doesn't get used much */
+                                       ND_PRINT(" [%s 0x", tok2str(pppoetag2str, "TAG-0x%x", tag_type));
+                                       for (v=p; v<p+tag_len; v++) {
+                                               ND_PRINT("%02X", GET_U_1(v));
                                        }
+                                       ND_PRINT("]");
+                               }
+
 
-                               /* TODO print UTF8 decoded text */
-                               if (isascii) {
-                                       l = (tag_len < 80 ? tag_len : 80);
-                                       printf(" [%s \"%*.*s\"]",
-                                           tok2str(pppoetag2str, "TAG-0x%x", tag_type),
-                                           l, l, p);
-                               } else
-                                       printf(" [%s UTF8]",
-                                           tok2str(pppoetag2str, "TAG-0x%x", tag_type));
                        } else
-                               printf(" [%s]", tok2str(pppoetag2str,
+                               ND_PRINT(" [%s]", tok2str(pppoetag2str,
                                    "TAG-0x%x", tag_type));
 
                        p += tag_len;
                        /* p points to next tag */
                }
+               return PPPOE_HDRLEN;
        } else {
-               printf(" ");
-               ppp_print(pppoe_payload, pppoe_length);
+               /* PPPoE data */
+               ND_PRINT(" ");
+               return (PPPOE_HDRLEN + ppp_print(ndo, pppoe_payload, pppoe_length));
        }
-       return;
+       /* NOTREACHED */
+
+invalid:
+       nd_print_invalid(ndo);
+       return 0;
 }