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