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