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