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