Home | History | Annotate | Line # | Download | only in dist
      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 (at) mit.edu>
     22  */
     23 
     24 #include <sys/cdefs.h>
     25 #ifndef lint
     26 __RCSID("$NetBSD: print-pppoe.c,v 1.9 2024/09/02 16:15:32 christos Exp $");
     27 #endif
     28 
     29 /* \summary: PPP-over-Ethernet (PPPoE) printer */
     30 
     31 #include <config.h>
     32 
     33 #include "netdissect-stdinc.h"
     34 
     35 #include "netdissect-ctype.h"
     36 
     37 #define ND_LONGJMP_FROM_TCHECK
     38 #include "netdissect.h"
     39 #include "extract.h"
     40 
     41 /* Codes */
     42 enum {
     43 	PPPOE_PADI = 0x09,
     44 	PPPOE_PADO = 0x07,
     45 	PPPOE_PADR = 0x19,
     46 	PPPOE_PADS = 0x65,
     47 	PPPOE_PADT = 0xa7
     48 };
     49 
     50 static const struct tok pppoecode2str[] = {
     51 	{ PPPOE_PADI, "PADI" },
     52 	{ PPPOE_PADO, "PADO" },
     53 	{ PPPOE_PADR, "PADR" },
     54 	{ PPPOE_PADS, "PADS" },
     55 	{ PPPOE_PADT, "PADT" },
     56 	{ 0, "" }, /* PPP Data */
     57 	{ 0, NULL }
     58 };
     59 
     60 /* Tags */
     61 enum {
     62 	PPPOE_EOL = 0,
     63 	PPPOE_SERVICE_NAME = 0x0101,
     64 	PPPOE_AC_NAME = 0x0102,
     65 	PPPOE_HOST_UNIQ = 0x0103,
     66 	PPPOE_AC_COOKIE = 0x0104,
     67 	PPPOE_VENDOR = 0x0105,
     68 	PPPOE_RELAY_SID = 0x0110,
     69 	PPPOE_MAX_PAYLOAD = 0x0120,
     70 	PPPOE_SERVICE_NAME_ERROR = 0x0201,
     71 	PPPOE_AC_SYSTEM_ERROR = 0x0202,
     72 	PPPOE_GENERIC_ERROR = 0x0203
     73 };
     74 
     75 static const struct tok pppoetag2str[] = {
     76 	{ PPPOE_EOL, "EOL" },
     77 	{ PPPOE_SERVICE_NAME, "Service-Name" },
     78 	{ PPPOE_AC_NAME, "AC-Name" },
     79 	{ PPPOE_HOST_UNIQ, "Host-Uniq" },
     80 	{ PPPOE_AC_COOKIE, "AC-Cookie" },
     81 	{ PPPOE_VENDOR, "Vendor-Specific" },
     82 	{ PPPOE_RELAY_SID, "Relay-Session-ID" },
     83 	{ PPPOE_MAX_PAYLOAD, "PPP-Max-Payload" },
     84 	{ PPPOE_SERVICE_NAME_ERROR, "Service-Name-Error" },
     85 	{ PPPOE_AC_SYSTEM_ERROR, "AC-System-Error" },
     86 	{ PPPOE_GENERIC_ERROR, "Generic-Error" },
     87 	{ 0, NULL }
     88 };
     89 
     90 #define PPPOE_HDRLEN 6
     91 #define MAXTAGPRINT 80
     92 
     93 void
     94 pppoe_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, const u_char *p)
     95 {
     96 	ndo->ndo_protocol = "pppoe";
     97 	ndo->ndo_ll_hdr_len += pppoe_print(ndo, p, h->len);
     98 }
     99 
    100 u_int
    101 pppoe_print(netdissect_options *ndo, const u_char *bp, u_int length)
    102 {
    103 	uint16_t pppoe_ver, pppoe_type, pppoe_code, pppoe_sessionid;
    104 	u_int pppoe_length;
    105 	const u_char *pppoe_packet, *pppoe_payload;
    106 
    107 	ndo->ndo_protocol = "pppoe";
    108 	if (length < PPPOE_HDRLEN) {
    109 		ND_PRINT(" (length %u < %u)", length, PPPOE_HDRLEN);
    110 		goto invalid;
    111 	}
    112 	length -= PPPOE_HDRLEN;
    113 	pppoe_packet = bp;
    114 	ND_TCHECK_LEN(pppoe_packet, PPPOE_HDRLEN);
    115 	pppoe_ver  = (GET_U_1(pppoe_packet) & 0xF0) >> 4;
    116 	pppoe_type  = (GET_U_1(pppoe_packet) & 0x0F);
    117 	pppoe_code = GET_U_1(pppoe_packet + 1);
    118 	pppoe_sessionid = GET_BE_U_2(pppoe_packet + 2);
    119 	pppoe_length    = GET_BE_U_2(pppoe_packet + 4);
    120 	pppoe_payload = pppoe_packet + PPPOE_HDRLEN;
    121 
    122 	if (pppoe_ver != 1) {
    123 		ND_PRINT(" [ver %u]",pppoe_ver);
    124 	}
    125 	if (pppoe_type != 1) {
    126 		ND_PRINT(" [type %u]",pppoe_type);
    127 	}
    128 
    129 	ND_PRINT("PPPoE %s", tok2str(pppoecode2str, "PAD-%x", pppoe_code));
    130 	if (pppoe_code == PPPOE_PADI && pppoe_length > 1484 - PPPOE_HDRLEN) {
    131 		ND_PRINT(" [len %u!]",pppoe_length);
    132 	}
    133 	if (pppoe_length > length) {
    134 		ND_PRINT(" [len %u > %u!]", pppoe_length, length);
    135 		pppoe_length = length;
    136 	}
    137 	if (pppoe_sessionid) {
    138 		ND_PRINT(" [ses 0x%x]", pppoe_sessionid);
    139 	}
    140 
    141 	if (pppoe_code) {
    142 		/* PPP session packets don't contain tags */
    143 		u_short tag_type = 0xffff, tag_len;
    144 		const u_char *p = pppoe_payload;
    145 
    146 		/*
    147 		 * loop invariant:
    148 		 * p points to current tag,
    149 		 * tag_type is previous tag or 0xffff for first iteration
    150 		 */
    151 		while (tag_type && p < pppoe_payload + pppoe_length) {
    152 			tag_type = GET_BE_U_2(p);
    153 			tag_len = GET_BE_U_2(p + 2);
    154 			p += 4;
    155 			/* p points to tag_value */
    156 
    157 			if (tag_len) {
    158 				unsigned ascii_count = 0, garbage_count = 0;
    159 				const u_char *v;
    160 				char tag_str[MAXTAGPRINT];
    161 				unsigned tag_str_len = 0;
    162 
    163 				/* TODO print UTF-8 decoded text */
    164 				ND_TCHECK_LEN(p, tag_len);
    165 				for (v = p; v < p + tag_len && tag_str_len < MAXTAGPRINT-1; v++)
    166 					if (ND_ASCII_ISPRINT(GET_U_1(v))) {
    167 						tag_str[tag_str_len++] = GET_U_1(v);
    168 						ascii_count++;
    169 					} else {
    170 						tag_str[tag_str_len++] = '.';
    171 						garbage_count++;
    172 					}
    173 				tag_str[tag_str_len] = 0;
    174 
    175 				if (ascii_count > garbage_count) {
    176 					ND_PRINT(" [%s \"%*.*s\"]",
    177 					       tok2str(pppoetag2str, "TAG-0x%x", tag_type),
    178 					       (int)tag_str_len,
    179 					       (int)tag_str_len,
    180 					       tag_str);
    181 				} else {
    182 					/* Print hex, not fast to abuse printf but this doesn't get used much */
    183 					ND_PRINT(" [%s 0x", tok2str(pppoetag2str, "TAG-0x%x", tag_type));
    184 					for (v=p; v<p+tag_len; v++) {
    185 						ND_PRINT("%02X", GET_U_1(v));
    186 					}
    187 					ND_PRINT("]");
    188 				}
    189 
    190 
    191 			} else
    192 				ND_PRINT(" [%s]", tok2str(pppoetag2str,
    193 				    "TAG-0x%x", tag_type));
    194 
    195 			p += tag_len;
    196 			/* p points to next tag */
    197 		}
    198 		return PPPOE_HDRLEN;
    199 	} else {
    200 		/* PPPoE data */
    201 		ND_PRINT(" ");
    202 		return (PPPOE_HDRLEN + ppp_print(ndo, pppoe_payload, pppoe_length));
    203 	}
    204 	/* NOTREACHED */
    205 
    206 invalid:
    207 	nd_print_invalid(ndo);
    208 	return 0;
    209 }
    210