Rename the routines that handle dissector tables with unsigned integer
[obnox/wireshark/wip.git] / epan / dissectors / packet-pppoe.c
1 /* packet-pppoe.c
2  * Routines for PPP Over Ethernet (PPPoE) packet disassembly (RFC2516)
3  * Up to date with http://www.iana.org/assignments/pppoe-parameters (2008-04-30)
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <glib.h>
31 #include <epan/packet.h>
32 #include <epan/strutil.h>
33 #include <epan/etypes.h>
34 #include <epan/prefs.h>
35 #include <epan/expert.h>
36 #include <epan/ppptypes.h>
37
38 static int proto_pppoed = -1;
39
40 /* Common to session and discovery protocols */
41 static gint hf_pppoe_version = -1;
42 static gint hf_pppoe_type = -1;
43 static gint hf_pppoe_code = -1;
44 static gint hf_pppoe_session_id = -1;
45 static gint hf_pppoe_payload_length = -1;
46
47 /* Discovery protocol fields */
48 static gint hf_pppoed_tags = -1;
49 static gint hf_pppoed_tag = -1;
50 static gint hf_pppoed_tag_length = -1;
51 static gint hf_pppoed_tag_length_8 = -1;
52 static gint hf_pppoed_tag_unknown_data = -1;
53 static gint hf_pppoed_tag_service_name = -1;
54 static gint hf_pppoed_tag_ac_name = -1;
55 static gint hf_pppoed_tag_host_uniq = -1;
56 static gint hf_pppoed_tag_ac_cookie = -1;
57 static gint hf_pppoed_tag_vendor_id = -1;
58 static gint hf_pppoed_tag_vendor_unspecified = -1;
59 static gint hf_pppoed_tag_vspec_tags = -1;
60 static gint hf_pppoed_tag_vspec_tag = -1;
61 static gint hf_pppoed_tag_vspec_circuit_id = -1;
62 static gint hf_pppoed_tag_vspec_remote_id = -1;
63 static gint hf_pppoed_tag_vspec_act_data_rate_up = -1;
64 static gint hf_pppoed_tag_vspec_act_data_rate_down = -1;
65 static gint hf_pppoed_tag_vspec_min_data_rate_up = -1;
66 static gint hf_pppoed_tag_vspec_min_data_rate_down = -1;
67 static gint hf_pppoed_tag_vspec_attainable_data_rate_up = -1;
68 static gint hf_pppoed_tag_vspec_attainable_data_rate_down = -1;
69 static gint hf_pppoed_tag_vspec_max_data_rate_up = -1;
70 static gint hf_pppoed_tag_vspec_max_data_rate_down = -1;
71 static gint hf_pppoed_tag_vspec_min_data_rate_up_lp = -1;
72 static gint hf_pppoed_tag_vspec_min_data_rate_down_lp = -1;
73 static gint hf_pppoed_tag_vspec_max_int_delay_up = -1;
74 static gint hf_pppoed_tag_vspec_act_int_delay_up = -1;
75 static gint hf_pppoed_tag_vspec_max_int_delay_down = -1;
76 static gint hf_pppoed_tag_vspec_act_int_delay_down = -1;
77 static gint hf_pppoed_tag_vspec_access_loop_encapsulation = -1;
78 static gint hf_pppoed_tag_vspec_access_loop_encap_data_link = -1;
79 static gint hf_pppoed_tag_vspec_access_loop_encap_encap_1 = -1;
80 static gint hf_pppoed_tag_vspec_access_loop_encap_encap_2 = -1;
81 static gint hf_pppoed_tag_credits = -1;
82 static gint hf_pppoed_tag_credits_fcn = -1;
83 static gint hf_pppoed_tag_credits_bcn = -1;
84 static gint hf_pppoed_tag_metrics = -1;
85 static gint hf_pppoed_tag_metrics_r = -1;
86 static gint hf_pppoed_tag_metrics_rlq = -1;
87 static gint hf_pppoed_tag_metrics_resource = -1;
88 static gint hf_pppoed_tag_metrics_latency = -1;
89 static gint hf_pppoed_tag_metrics_curr_drate = -1;
90 static gint hf_pppoed_tag_metrics_max_drate = -1;
91 static gint hf_pppoed_tag_mdr_units = -1;
92 static gint hf_pppoed_tag_cdr_units = -1;
93 static gint hf_pppoed_tag_seq_num = -1;
94 static gint hf_pppoed_tag_cred_scale = -1;
95 static gint hf_pppoed_tag_relay_session_id = -1;
96 static gint hf_pppoed_tag_hurl = -1;
97 static gint hf_pppoed_tag_motm = -1;
98 static gint hf_pppoed_tag_max_payload = -1;
99 static gint hf_pppoed_tag_ip_route_add = -1;
100 static gint hf_pppoed_tag_service_name_error = -1;
101 static gint hf_pppoed_tag_ac_system_error = -1;
102 static gint hf_pppoed_tag_generic_error = -1;
103
104 /* Session protocol fields */
105 static gint hf_pppoes_tags = -1;
106 static gint hf_pppoes_tag = -1;
107 static gint hf_pppoes_tag_credits = -1;
108 static gint hf_pppoes_tag_credits_fcn = -1;
109 static gint hf_pppoes_tag_credits_bcn = -1;
110
111 /* Session protocol fields */
112
113 static gint ett_pppoed = -1;
114 static gint ett_pppoed_tags = -1;
115 static gint ett_pppoed_tag_vspec_dslf_access_loop_encaps = -1;
116
117 static int proto_pppoes = -1;
118
119 static gint ett_pppoes = -1;
120 static gint ett_pppoes_tags = -1;
121
122 /* PPPoE parent fields */
123
124 static int proto_pppoe = -1;
125 static gint ett_pppoe = -1;
126
127
128 /* Handle for calling for ppp dissector to handle session data */
129 static dissector_handle_t ppp_handle;
130
131
132 /* Preference for showing discovery tag values and lengths */
133 static gboolean global_pppoe_show_tags_and_lengths = FALSE;
134
135
136 #define PPPOE_CODE_SESSION    0x00
137 #define PPPOE_CODE_PADO       0x07
138 #define PPPOE_CODE_PADI       0x09
139 #define PPPOE_CODE_PADG       0x0a
140 #define PPPOE_CODE_PADC       0x0b
141 #define PPPOE_CODE_PADQ       0x0c
142 #define PPPOE_CODE_PADR       0x19
143 #define PPPOE_CODE_PADS       0x65
144 #define PPPOE_CODE_PADT       0xa7
145 #define PPPOE_CODE_PADM       0xd3
146 #define PPPOE_CODE_PADN       0xd4
147
148 #define PPPOE_TAG_EOL         0x0000
149 #define PPPOE_TAG_SVC_NAME    0x0101
150 #define PPPOE_TAG_AC_NAME     0x0102
151 #define PPPOE_TAG_HOST_UNIQ   0x0103
152 #define PPPOE_TAG_AC_COOKIE   0x0104
153 #define PPPOE_TAG_VENDOR      0x0105
154 #define PPPOE_TAG_CREDITS     0x0106
155 #define PPPOE_TAG_METRICS     0x0107
156 #define PPPOE_TAG_SEQ_NUM     0x0108
157 #define PPPOE_TAG_CRED_SCALE  0x0109
158 #define PPPOE_TAG_RELAY_ID    0x0110
159 #define PPPOE_TAG_HURL        0x0111
160 #define PPPOE_TAG_MOTM        0x0112
161 #define PPPOE_TAG_MAX_PAYLD   0x0120
162 #define PPPOE_TAG_IP_RT_ADD   0x0121
163 #define PPPOE_TAG_SVC_ERR     0x0201
164 #define PPPOE_TAG_AC_ERR      0x0202
165 #define PPPOE_TAG_GENERIC_ERR 0x0203
166
167 #define PPPOE_VENDOR_ID_DSLF  3561
168
169 #define PPPOE_TAG_VSPEC_DSLF_CIRCUIT_ID                0x01
170 #define PPPOE_TAG_VSPEC_DSLF_REMOTE_ID                 0x02
171 #define PPPOE_TAG_VSPEC_DSLF_ACT_DATA_RATE_UP          0x81
172 #define PPPOE_TAG_VSPEC_DSLF_ACT_DATA_RATE_DOWN        0x82
173 #define PPPOE_TAG_VSPEC_DSLF_MIN_DATA_RATE_UP          0x83
174 #define PPPOE_TAG_VSPEC_DSLF_MIN_DATA_RATE_DOWN        0x84
175 #define PPPOE_TAG_VSPEC_DSLF_ATTAINABLE_DATA_RATE_UP   0x85
176 #define PPPOE_TAG_VSPEC_DSLF_ATTAINABLE_DATA_RATE_DOWN 0x86
177 #define PPPOE_TAG_VSPEC_DSLF_MAX_DATA_RATE_UP          0x87
178 #define PPPOE_TAG_VSPEC_DSLF_MAX_DATA_RATE_DOWN        0x88
179 #define PPPOE_TAG_VSPEC_DSLF_MIN_DATA_RATE_UP_LP       0x89
180 #define PPPOE_TAG_VSPEC_DSLF_MIN_DATA_RATE_DOWN_LP     0x8a
181 #define PPPOE_TAG_VSPEC_DSLF_MAX_INT_DELAY_UP          0x8b
182 #define PPPOE_TAG_VSPEC_DSLF_ACT_INT_DELAY_UP          0x8c
183 #define PPPOE_TAG_VSPEC_DSLF_MAX_INT_DELAY_DOWN        0x8d
184 #define PPPOE_TAG_VSPEC_DSLF_ACT_INT_DELAY_DOWN        0x8e
185 #define PPPOE_TAG_VSPEC_DSLF_ACCESS_LOOP_ENCAPSULATION 0x90
186
187 #define PPPOE_TAG_VSPEC_DSLF_ACCESS_LOOP_ENCAP_DATA_LINK_ATM 0x00
188 #define PPPOE_TAG_VSPEC_DSLF_ACCESS_LOOP_ENCAP_DATA_LINK_ETH 0x01
189
190 #define PPPOE_TAG_VSPEC_DSLF_ACCESS_LOOP_ENCAP_ENCAPS_1_NA               0x00
191 #define PPPOE_TAG_VSPEC_DSLF_ACCESS_LOOP_ENCAP_ENCAPS_1_UNTAGGED_ETH     0x01
192 #define PPPOE_TAG_VSPEC_DSLF_ACCESS_LOOP_ENCAP_ENCAPS_1_SINLE_TAGGED_ETH 0x02
193
194 #define PPPOE_TAG_VSPEC_DSLF_ACCESS_LOOP_ENCAP_ENCAPS_2_NA                             0x00
195 #define PPPOE_TAG_VSPEC_DSLF_ACCESS_LOOP_ENCAP_ENCAPS_2_PPPOA_LLC                      0x01
196 #define PPPOE_TAG_VSPEC_DSLF_ACCESS_LOOP_ENCAP_ENCAPS_2_PPPOA_NULL                     0x02
197 #define PPPOE_TAG_VSPEC_DSLF_ACCESS_LOOP_ENCAP_ENCAPS_2_IPOA_LLC                       0x03
198 #define PPPOE_TAG_VSPEC_DSLF_ACCESS_LOOP_ENCAP_ENCAPS_2_IPOA_NULL                      0x04
199 #define PPPOE_TAG_VSPEC_DSLF_ACCESS_LOOP_ENCAP_ENCAPS_2_ETH_OVER_AAL5_LLC_WITH_FCS     0x05
200 #define PPPOE_TAG_VSPEC_DSLF_ACCESS_LOOP_ENCAP_ENCAPS_2_ETH_OVER_AAL5_LLC_WITHOUT_FCS  0x06
201 #define PPPOE_TAG_VSPEC_DSLF_ACCESS_LOOP_ENCAP_ENCAPS_2_ETH_OVER_AAL5_NULL_WITH_FCS    0x07
202 #define PPPOE_TAG_VSPEC_DSLF_ACCESS_LOOP_ENCAP_ENCAPS_2_ETH_OVER_AAL5_NULL_WITHOUT_FCS 0x08
203
204 #define PPPOE_CDR_MASK        0x06
205 #define PPPOE_MDR_MASK        0x18
206 #define PPPOE_RCV_ONLY_MASK   0x01
207
208 #define PPPOE_SCALE_KBPS      0x00
209 #define PPPOE_SCALE_MBPS      0x01
210 #define PPPOE_SCALE_GBPS      0x02
211 #define PPPOE_SCALE_TBPS      0x03
212
213
214 static const value_string code_vals[] = {
215                 {PPPOE_CODE_SESSION, "Session Data"                             },
216                 {PPPOE_CODE_PADO, "Active Discovery Offer (PADO)"               },
217                 {PPPOE_CODE_PADI, "Active Discovery Initiation (PADI)"          },
218                 {PPPOE_CODE_PADG, "Active Discovery Session-Grant (PADG)"       },
219                 {PPPOE_CODE_PADC, "Active Discovery Session-Credit Resp.(PADC)" },
220                 {PPPOE_CODE_PADQ, "Active Discovery Quality (PADQ)"             },
221                 {PPPOE_CODE_PADR, "Active Discovery Request (PADR)"             },
222                 {PPPOE_CODE_PADS, "Active Discovery Session-confirmation (PADS)"},
223                 {PPPOE_CODE_PADT, "Active Discovery Terminate (PADT)"           },
224                 {PPPOE_CODE_PADM, "Active Discovery Message (PADM)"             },
225                 {PPPOE_CODE_PADN, "Active Discovery Network (PADN)"             },
226                 {0,               NULL                                          }
227 };
228
229
230 static const value_string tag_vals[] = {
231                 {PPPOE_TAG_EOL,        "End-Of-List"       },
232                 {PPPOE_TAG_SVC_NAME,   "Service-Name"      },
233                 {PPPOE_TAG_AC_NAME,    "AC-Name"           },
234                 {PPPOE_TAG_HOST_UNIQ,  "Host-Uniq"         },
235                 {PPPOE_TAG_AC_COOKIE,  "AC-Cookie"         },
236                 {PPPOE_TAG_VENDOR,     "Vendor-Specific"   },
237                 {PPPOE_TAG_CREDITS,    "Credits"           },
238                 {PPPOE_TAG_METRICS,    "Metrics"           },
239                 {PPPOE_TAG_SEQ_NUM,    "Sequence Number"    },
240                 {PPPOE_TAG_CRED_SCALE, "Credit Scale Factor"},
241                 {PPPOE_TAG_RELAY_ID,   "Relay-Session-Id"  },
242                 {PPPOE_TAG_HURL,       "HURL"              },
243                 {PPPOE_TAG_MOTM,       "MOTM"              },
244                 {PPPOE_TAG_MAX_PAYLD,  "PPP-Max-Payload"   },
245                 {PPPOE_TAG_IP_RT_ADD,  "IP Route Add"      },
246                 {PPPOE_TAG_SVC_ERR,    "Service-Name-Error"},
247                 {PPPOE_TAG_AC_ERR,     "AC-System-Error"   },
248                 {PPPOE_TAG_GENERIC_ERR,"Generic-Error"     },
249                 {0,                    NULL                }
250 };
251
252 static const value_string vspec_tag_vals[] = {
253                 {PPPOE_TAG_VSPEC_DSLF_CIRCUIT_ID,                "Circuit-ID"                    },
254                 {PPPOE_TAG_VSPEC_DSLF_REMOTE_ID,                 "Remote-ID"                     },
255                 {PPPOE_TAG_VSPEC_DSLF_ACT_DATA_RATE_UP,          "Actual-Data-Rate-Up"           },
256                 {PPPOE_TAG_VSPEC_DSLF_ACT_DATA_RATE_DOWN,        "Actual-Data-Rate-Down"         },
257                 {PPPOE_TAG_VSPEC_DSLF_MIN_DATA_RATE_UP,          "Min-Data-Rate-Up"              },
258                 {PPPOE_TAG_VSPEC_DSLF_MIN_DATA_RATE_DOWN,        "Min-Data-Rate-Down"            },
259                 {PPPOE_TAG_VSPEC_DSLF_ATTAINABLE_DATA_RATE_UP,   "Attainable-Data-Rate-Up"       },
260                 {PPPOE_TAG_VSPEC_DSLF_ATTAINABLE_DATA_RATE_DOWN, "Attainable-Data-Rate-Down"     },
261                 {PPPOE_TAG_VSPEC_DSLF_MAX_DATA_RATE_UP,          "Max-Data-Rate-Up"              },
262                 {PPPOE_TAG_VSPEC_DSLF_MAX_DATA_RATE_DOWN,        "Max-Data-Rate-Down"            },
263                 {PPPOE_TAG_VSPEC_DSLF_MIN_DATA_RATE_UP_LP,       "Min-Data-Rate-Up-Low-Power"    },
264                 {PPPOE_TAG_VSPEC_DSLF_MIN_DATA_RATE_DOWN_LP,     "Min-Data-Rate-Down-Low-Power"  },
265                 {PPPOE_TAG_VSPEC_DSLF_MAX_INT_DELAY_UP,          "Max-Interleaving-Delay-Up"     },
266                 {PPPOE_TAG_VSPEC_DSLF_ACT_INT_DELAY_UP,          "Actual-Interleaving-Delay-Up"  },
267                 {PPPOE_TAG_VSPEC_DSLF_MAX_INT_DELAY_DOWN,        "Max-Interleaving-Delay-Down"   },
268                 {PPPOE_TAG_VSPEC_DSLF_ACT_INT_DELAY_DOWN,        "Actual-Interleaving-Delay-Down"},
269                 {PPPOE_TAG_VSPEC_DSLF_ACCESS_LOOP_ENCAPSULATION, "Access-Loop-Encapsulation"     },
270                 {0,                                              NULL                            }
271 };
272
273 static const value_string vspec_tag_dslf_access_loop_encap_data_link_vals[] = {
274                 {PPPOE_TAG_VSPEC_DSLF_ACCESS_LOOP_ENCAP_DATA_LINK_ATM, "ATM AAL5"},
275                 {PPPOE_TAG_VSPEC_DSLF_ACCESS_LOOP_ENCAP_DATA_LINK_ETH, "Ethernet"},
276                 {0,                                                     NULL     }
277 };
278
279 static const value_string vspec_tag_dslf_access_loop_encap_encap_1_vals[] = {
280                 {PPPOE_TAG_VSPEC_DSLF_ACCESS_LOOP_ENCAP_ENCAPS_1_NA,               "NA"                    },
281                 {PPPOE_TAG_VSPEC_DSLF_ACCESS_LOOP_ENCAP_ENCAPS_1_UNTAGGED_ETH,     "Untagged Ethernet"     },
282                 {PPPOE_TAG_VSPEC_DSLF_ACCESS_LOOP_ENCAP_ENCAPS_1_SINLE_TAGGED_ETH, "Single-tagged Ethernet"},
283                 {0,                                                     NULL                               }
284 };
285
286 static const value_string vspec_tag_dslf_access_loop_encap_encap_2_vals[] = {
287                 {PPPOE_TAG_VSPEC_DSLF_ACCESS_LOOP_ENCAP_ENCAPS_2_NA,                            "NA"                             },
288                 {PPPOE_TAG_VSPEC_DSLF_ACCESS_LOOP_ENCAP_ENCAPS_2_PPPOA_LLC,                     "PPPoA LLC"                      },
289                 {PPPOE_TAG_VSPEC_DSLF_ACCESS_LOOP_ENCAP_ENCAPS_2_PPPOA_NULL,                    "PPPoA Null"                     },
290                 {PPPOE_TAG_VSPEC_DSLF_ACCESS_LOOP_ENCAP_ENCAPS_2_IPOA_LLC,                      "IPoA LLC"                       },
291                 {PPPOE_TAG_VSPEC_DSLF_ACCESS_LOOP_ENCAP_ENCAPS_2_IPOA_NULL,                     "IPoA Null"                      },
292                 {PPPOE_TAG_VSPEC_DSLF_ACCESS_LOOP_ENCAP_ENCAPS_2_ETH_OVER_AAL5_LLC_WITH_FCS,    "Ethernet over AAL5 LLC w FCS"   },
293                 {PPPOE_TAG_VSPEC_DSLF_ACCESS_LOOP_ENCAP_ENCAPS_2_ETH_OVER_AAL5_LLC_WITHOUT_FCS, "Ethernet over AAL5 LLC w/o FCS" },
294                 {PPPOE_TAG_VSPEC_DSLF_ACCESS_LOOP_ENCAP_ENCAPS_2_ETH_OVER_AAL5_NULL_WITH_FCS,   "Ethernet over AAL5 Null w FCS"  },
295                 {PPPOE_TAG_VSPEC_DSLF_ACCESS_LOOP_ENCAP_ENCAPS_2_ETH_OVER_AAL5_NULL_WITHOUT_FCS,"Ethernet over AAL5 Null w/o FCS"},
296                 {0,                                                     NULL                               }
297 };
298
299 const value_string datarate_scale_vals[] = {
300                 {PPPOE_SCALE_KBPS,      "kilobits per second"},
301                 {PPPOE_SCALE_MBPS,      "megabits per second"},
302                 {PPPOE_SCALE_GBPS,      "gigabits per second"},
303                 {PPPOE_SCALE_TBPS,      "terabits per second"},
304                 {0,                     NULL                 }
305 };
306
307
308 #define CASE_VSPEC_DSLF_TAG(tag_name, relation, length, hf_var) case tag_name: \
309                 if (!(poe_tag_length relation length)) { \
310                         expert_add_info_format(pinfo, pppoe_tree, PI_MALFORMED, PI_WARN, "%s: Wrong length: %u (expected %s %d)", \
311                                         val_to_str(poe_tag, vspec_tag_vals, "Unknown"), poe_tag_length, #relation, length); \
312                 } else { \
313                         proto_tree_add_item(pppoe_tree, hf_var, tvb, \
314                                 tagstart+2, poe_tag_length, FALSE); \
315                 } \
316         break;
317
318 /* Dissect Vendor-Specific Tags introduced by the DSLF */
319 static void
320 dissect_pppoe_subtags_dslf(tvbuff_t *tvb, packet_info *pinfo _U_, int offset, proto_tree *tree,
321                    int payload_length)
322 {
323         guint8 poe_tag;
324         guint8 poe_tag_length;
325         int tagstart;
326
327         proto_tree  *pppoe_tree, *ti, *encaps_tree;
328
329         /* Start Decoding Here. */
330         if (tree)
331         {
332                 /* Create tags subtree */
333                 ti = proto_tree_add_item(tree, hf_pppoed_tag_vspec_tags, tvb, offset, payload_length, FALSE);
334                 pppoe_tree = proto_item_add_subtree(ti, ett_pppoed_tags);
335
336                 tagstart = offset;
337
338                 /* Loop until all data seen or End-Of-List tag found */
339                 while (tagstart <= offset + payload_length-2)
340                 {
341                         poe_tag = tvb_get_guint8(tvb, tagstart);
342                         poe_tag_length = tvb_get_guint8(tvb, tagstart + 1);
343
344                         /* Tag value and data length */
345                         if (global_pppoe_show_tags_and_lengths)
346                         {
347                                 proto_tree_add_item(pppoe_tree, hf_pppoed_tag_vspec_tag, tvb, tagstart, 1, FALSE);
348                                 proto_tree_add_item(pppoe_tree, hf_pppoed_tag_length_8, tvb, tagstart+1, 1, FALSE);
349                         }
350
351                         /* Show tag data */
352                         switch (poe_tag)
353                         {
354                                 CASE_VSPEC_DSLF_TAG(PPPOE_TAG_VSPEC_DSLF_CIRCUIT_ID, <=, 63, 
355                                                 hf_pppoed_tag_vspec_circuit_id)
356                                 CASE_VSPEC_DSLF_TAG(PPPOE_TAG_VSPEC_DSLF_REMOTE_ID, <=, 63, 
357                                                 hf_pppoed_tag_vspec_remote_id)
358                                 CASE_VSPEC_DSLF_TAG(PPPOE_TAG_VSPEC_DSLF_ACT_DATA_RATE_UP, ==, 4, 
359                                                 hf_pppoed_tag_vspec_act_data_rate_up)
360                                 CASE_VSPEC_DSLF_TAG(PPPOE_TAG_VSPEC_DSLF_ACT_DATA_RATE_DOWN, ==, 4, 
361                                                 hf_pppoed_tag_vspec_act_data_rate_down)
362                                 CASE_VSPEC_DSLF_TAG(PPPOE_TAG_VSPEC_DSLF_MIN_DATA_RATE_UP, ==, 4, 
363                                                 hf_pppoed_tag_vspec_min_data_rate_up)
364                                 CASE_VSPEC_DSLF_TAG(PPPOE_TAG_VSPEC_DSLF_MIN_DATA_RATE_DOWN, ==, 4, 
365                                                 hf_pppoed_tag_vspec_min_data_rate_down)
366                                 CASE_VSPEC_DSLF_TAG(PPPOE_TAG_VSPEC_DSLF_ATTAINABLE_DATA_RATE_UP, ==, 4, 
367                                                 hf_pppoed_tag_vspec_attainable_data_rate_up)
368                                 CASE_VSPEC_DSLF_TAG(PPPOE_TAG_VSPEC_DSLF_ATTAINABLE_DATA_RATE_DOWN, ==, 4, 
369                                                 hf_pppoed_tag_vspec_attainable_data_rate_down)
370                                 CASE_VSPEC_DSLF_TAG(PPPOE_TAG_VSPEC_DSLF_MAX_DATA_RATE_UP, ==, 4, 
371                                                 hf_pppoed_tag_vspec_max_data_rate_up)
372                                 CASE_VSPEC_DSLF_TAG(PPPOE_TAG_VSPEC_DSLF_MAX_DATA_RATE_DOWN, ==, 4, 
373                                                 hf_pppoed_tag_vspec_max_data_rate_down)
374                                 CASE_VSPEC_DSLF_TAG(PPPOE_TAG_VSPEC_DSLF_MIN_DATA_RATE_UP_LP, ==, 4, 
375                                                 hf_pppoed_tag_vspec_min_data_rate_up_lp)
376                                 CASE_VSPEC_DSLF_TAG(PPPOE_TAG_VSPEC_DSLF_MIN_DATA_RATE_DOWN_LP, ==, 4, 
377                                                 hf_pppoed_tag_vspec_min_data_rate_down_lp)
378                                 CASE_VSPEC_DSLF_TAG(PPPOE_TAG_VSPEC_DSLF_MAX_INT_DELAY_UP, ==, 4, 
379                                                 hf_pppoed_tag_vspec_max_int_delay_up)
380                                 CASE_VSPEC_DSLF_TAG(PPPOE_TAG_VSPEC_DSLF_ACT_INT_DELAY_UP, ==, 4, 
381                                                 hf_pppoed_tag_vspec_act_int_delay_up)
382                                 CASE_VSPEC_DSLF_TAG(PPPOE_TAG_VSPEC_DSLF_MAX_INT_DELAY_DOWN, ==, 4, 
383                                                 hf_pppoed_tag_vspec_max_int_delay_down)
384                                 CASE_VSPEC_DSLF_TAG(PPPOE_TAG_VSPEC_DSLF_ACT_INT_DELAY_DOWN, ==, 4, 
385                                                 hf_pppoed_tag_vspec_act_int_delay_down)
386                                 case PPPOE_TAG_VSPEC_DSLF_ACCESS_LOOP_ENCAPSULATION:
387                                         ti = proto_tree_add_item(pppoe_tree, hf_pppoed_tag_vspec_access_loop_encapsulation, tvb,
388                                                         tagstart+2, 3, FALSE);
389                                         if (poe_tag_length != 3) {
390                                                 expert_add_info_format(pinfo, ti, PI_MALFORMED, PI_WARN,
391                                                                 "%s: Wrong length: %u (expected 3)",
392                                                                 val_to_str(poe_tag, vspec_tag_vals, "Unknown"), poe_tag_length);
393                                         }
394                                         encaps_tree = proto_item_add_subtree(ti, ett_pppoed_tag_vspec_dslf_access_loop_encaps);
395                                         proto_tree_add_item(encaps_tree, hf_pppoed_tag_vspec_access_loop_encap_data_link,
396                                                         tvb, tagstart+2, 1, FALSE);
397                                         proto_tree_add_item(encaps_tree, hf_pppoed_tag_vspec_access_loop_encap_encap_1,
398                                                         tvb, tagstart+3, 1, FALSE);
399                                         proto_tree_add_item(encaps_tree, hf_pppoed_tag_vspec_access_loop_encap_encap_2,
400                                                         tvb, tagstart+4, 1, FALSE);
401                                         
402                                         break;
403                                 default:
404                                         if (poe_tag_length > 0 )
405                                         {
406                                                 /* Presumably unknown tag;
407                                                    show tag value if we didn't do it above */
408                                                 if (!global_pppoe_show_tags_and_lengths)
409                                                 {
410                                                         proto_tree_add_item(pppoe_tree, hf_pppoed_tag, tvb, tagstart, 1, FALSE);
411                                                         proto_tree_add_item(pppoe_tree, hf_pppoed_tag_length_8, tvb, tagstart+1, 1, FALSE);
412                                                 }
413                                                 proto_tree_add_item(pppoe_tree, hf_pppoed_tag_unknown_data, tvb,
414                                                                 tagstart+1, poe_tag_length, FALSE);
415                                         }
416                         }
417
418                         tagstart += (2 + poe_tag_length);
419                 }
420         }
421 }
422
423
424 /* Dissect discovery protocol tags */
425 static void
426 dissect_pppoe_tags(tvbuff_t *tvb, packet_info *pinfo, int offset, proto_tree *tree,
427                    int payload_length)
428 {
429         guint16 poe_tag;
430         guint16 poe_tag_length;
431         int tagstart;
432         guint16 poe_rsv = 0;
433
434         proto_tree  *pppoe_tree;
435         proto_item  *ti;
436         proto_item  *pppoe_tree_tag_length_item = NULL;
437         proto_item  *item;
438
439         /* Start Decoding Here. */
440         if (tree)
441         {
442                 /* Create tags subtree */
443                 ti = proto_tree_add_item(tree, hf_pppoed_tags, tvb, offset, payload_length-6, FALSE);
444                 pppoe_tree = proto_item_add_subtree(ti, ett_pppoed_tags);
445
446                 tagstart = offset;
447
448                 /* Loop until all data seen or End-Of-List tag found */
449                 while (tagstart <= payload_length-2)
450                 {
451                         poe_tag = tvb_get_ntohs(tvb, tagstart);
452                         poe_tag_length = tvb_get_ntohs(tvb, tagstart + 2);
453
454                         /* Tag value and data length */
455                         if (global_pppoe_show_tags_and_lengths)
456                         {
457                                 proto_tree_add_item(pppoe_tree, hf_pppoed_tag, tvb, tagstart, 2, FALSE);
458                                 pppoe_tree_tag_length_item = 
459                                         proto_tree_add_item(pppoe_tree, hf_pppoed_tag_length, tvb, tagstart+2, 2, FALSE);
460                         }
461
462                         /* Show tag data */
463                         switch (poe_tag)
464                         {
465                                 case PPPOE_TAG_SVC_NAME:
466                                         if (poe_tag_length > 0)
467                                         {
468                                                 proto_tree_add_item(pppoe_tree, hf_pppoed_tag_service_name, tvb,
469                                                                     tagstart+4, poe_tag_length, FALSE);
470                                         }
471                                         break;
472                                 case PPPOE_TAG_AC_NAME:
473                                         proto_tree_add_item(pppoe_tree, hf_pppoed_tag_ac_name, tvb,
474                                                             tagstart+4, poe_tag_length, FALSE);
475                                         /* Show AC-Name in info column */
476                                         if (check_col(pinfo->cinfo, COL_INFO))
477                                         {
478                                                 col_append_fstr(pinfo->cinfo, COL_INFO, " AC-Name='%s'",
479                                                                tvb_get_ephemeral_string(tvb, tagstart+4, poe_tag_length));
480                                         }
481                                         break;
482                                 case PPPOE_TAG_HOST_UNIQ:
483                                         proto_tree_add_item(pppoe_tree, hf_pppoed_tag_host_uniq, tvb,
484                                                             tagstart+4, poe_tag_length, FALSE);
485                                         break;
486                                 case PPPOE_TAG_AC_COOKIE:
487                                         proto_tree_add_item(pppoe_tree, hf_pppoed_tag_ac_cookie, tvb,
488                                                             tagstart+4, poe_tag_length, FALSE);
489                                         break;
490                                 case PPPOE_TAG_VENDOR:
491                                         if (poe_tag_length >= 4)
492                                         {
493                                                 proto_tree_add_item(pppoe_tree, hf_pppoed_tag_vendor_id, tvb,
494                                                                                         tagstart+4, 4, FALSE);
495                                         }
496                                         if (poe_tag_length > 4)
497                                         {
498                                                 guint32 vendor_id = tvb_get_ntohl(tvb, tagstart+4);
499                                                 switch (vendor_id)
500                                                 {
501                                                         case PPPOE_VENDOR_ID_DSLF:
502                                                                 dissect_pppoe_subtags_dslf(tvb,pinfo,tagstart+4+4,pppoe_tree,poe_tag_length-4);
503                                                                 break;
504                                                         default:
505                                                                 proto_tree_add_item(pppoe_tree, hf_pppoed_tag_vendor_unspecified, tvb,
506                                                                                     tagstart+4+4, poe_tag_length-4, FALSE);
507                                                                 
508                                                 }
509                                         }
510                                         break;
511                                 case PPPOE_TAG_CREDITS:
512                                         if (poe_tag_length == 4)
513                                         {
514                                                 proto_tree_add_item(pppoe_tree, hf_pppoed_tag_credits_fcn, tvb,
515                                                                     tagstart+4, 2, FALSE);
516                                                 proto_tree_add_item(pppoe_tree, hf_pppoed_tag_credits_bcn, tvb,
517                                                                     tagstart+6, 2, FALSE);
518                                         } else {
519                                                 proto_tree_add_item(pppoe_tree, hf_pppoed_tag_credits, tvb,
520                                                                     tagstart+4, poe_tag_length, FALSE);
521                                         }
522                                         break;
523                                 case PPPOE_TAG_METRICS:
524                                         if (poe_tag_length == 10)
525                                         {
526                                                 poe_rsv = tvb_get_ntohs(tvb, tagstart+4);
527
528                                                 proto_tree_add_item(pppoe_tree, hf_pppoed_tag_mdr_units, tvb,
529                                                                     tagstart+4, 2, FALSE);
530                                                 proto_tree_add_item(pppoe_tree, hf_pppoed_tag_cdr_units, tvb,
531                                                                     tagstart+4, 2, FALSE);
532                                                 proto_tree_add_item(pppoe_tree, hf_pppoed_tag_metrics_r, tvb,
533                                                                     tagstart+4, 2, FALSE);
534                                                 proto_tree_add_item(pppoe_tree, hf_pppoed_tag_metrics_rlq, tvb,
535                                                                     tagstart+6, 1, FALSE);
536                                                 proto_tree_add_item(pppoe_tree, hf_pppoed_tag_metrics_resource, tvb,
537                                                                     tagstart+7, 1, FALSE);
538                                                 proto_tree_add_item(pppoe_tree, hf_pppoed_tag_metrics_latency, tvb,
539                                                                     tagstart+8, 2, FALSE);
540
541                                                 /* CDR */
542                                                 ti = proto_tree_add_item(pppoe_tree, hf_pppoed_tag_metrics_curr_drate, tvb,
543                                                                          tagstart+10, 2, FALSE);
544
545                                                 switch ((poe_rsv & PPPOE_CDR_MASK) >> 1)
546                                                 {
547                                                 case (PPPOE_SCALE_KBPS):
548                                                     proto_item_append_text(ti, " kbps");
549                                                     break;
550                                                 case (PPPOE_SCALE_MBPS):
551                                                     proto_item_append_text(ti, " mbps");
552                                                     break;
553                                                 case (PPPOE_SCALE_GBPS):
554                                                     proto_item_append_text(ti, " gbps");
555                                                     break;
556                                                 case (PPPOE_SCALE_TBPS):
557                                                     proto_item_append_text(ti, " tbps");
558                                                     break;
559                                                 }
560
561                                                 /* MDR */
562                                                 ti = proto_tree_add_item(pppoe_tree, hf_pppoed_tag_metrics_max_drate, tvb,
563                                                                     tagstart+12, 2, FALSE);
564
565                                                 switch ((poe_rsv & PPPOE_MDR_MASK) >> 3)
566                                                 {
567                                                 case (PPPOE_SCALE_KBPS):
568                                                     proto_item_append_text(ti, " kbps");
569                                                     break;
570                                                 case (PPPOE_SCALE_MBPS):
571                                                     proto_item_append_text(ti, " mbps");
572                                                     break;
573                                                 case (PPPOE_SCALE_GBPS):
574                                                     proto_item_append_text(ti, " gbps");
575                                                     break;
576                                                 case (PPPOE_SCALE_TBPS):
577                                                     proto_item_append_text(ti, " tbps");
578                                                     break;
579                                                 }
580
581                                         } else {
582                                                 proto_tree_add_item(pppoe_tree, hf_pppoed_tag_metrics, tvb,
583                                                                     tagstart+4, poe_tag_length, FALSE);
584                                         }
585                                         break;
586                                 case PPPOE_TAG_SEQ_NUM:
587                                         if (poe_tag_length == 2) {
588                                                 proto_tree_add_item(pppoe_tree, hf_pppoed_tag_seq_num, tvb,
589                                                                     tagstart+4, poe_tag_length, FALSE);
590                                         } else {
591                                                 if (global_pppoe_show_tags_and_lengths) {
592                                                         proto_item_append_text(pppoe_tree_tag_length_item, " [Wrong: should be 2]");
593                                                         item = pppoe_tree_tag_length_item;
594                                                 } else {
595                                                         item = proto_tree_add_text(pppoe_tree, tvb, tagstart+4, poe_tag_length,
596                                                             "%s: Wrong length: %u (expected 2)",
597                                                             proto_registrar_get_name(hf_pppoed_tag_seq_num),
598                                                             poe_tag_length);
599                                                 }
600                                                 expert_add_info_format(pinfo, item, PI_MALFORMED, PI_WARN, 
601                                                                        "Sequence Number tag: Wrong length: %u (expected 2)",
602                                                                        poe_tag_length);
603                                         }
604                                         break;
605                                 case PPPOE_TAG_CRED_SCALE:
606                                         if (poe_tag_length == 2) {
607                                                 proto_tree_add_item(pppoe_tree, hf_pppoed_tag_cred_scale, tvb,
608                                                                     tagstart+4, poe_tag_length, FALSE);
609                                         } else {
610                                                 if (global_pppoe_show_tags_and_lengths) {
611                                                         proto_item_append_text(pppoe_tree_tag_length_item, " [Wrong: should be 2]");
612                                                         item = pppoe_tree_tag_length_item;
613                                                 } else {
614                                                         item = proto_tree_add_text(pppoe_tree, tvb, tagstart+4, poe_tag_length,
615                                                             "%s: Wrong length: %u (expected 2)",
616                                                             proto_registrar_get_name(hf_pppoed_tag_cred_scale),
617                                                             poe_tag_length);
618                                                 }
619                                                 expert_add_info_format(pinfo, item, PI_MALFORMED, PI_WARN, 
620                                                                        "Credit Scale Factor tag: Wrong length: %u (expected 2)",
621                                                                        poe_tag_length);
622                                         }
623                                         break;
624                                 case PPPOE_TAG_RELAY_ID:
625                                         proto_tree_add_item(pppoe_tree, hf_pppoed_tag_relay_session_id, tvb,
626                                                             tagstart+4, poe_tag_length, FALSE);
627                                         break;
628                                 case PPPOE_TAG_HURL:
629                                         proto_tree_add_item(pppoe_tree, hf_pppoed_tag_hurl, tvb,
630                                                             tagstart+4, poe_tag_length, FALSE);
631                                         break;
632                                 case PPPOE_TAG_MOTM:
633                                         proto_tree_add_item(pppoe_tree, hf_pppoed_tag_motm, tvb,
634                                                             tagstart+4, poe_tag_length, FALSE);
635                                         break;
636                                 case PPPOE_TAG_MAX_PAYLD:
637                                         proto_tree_add_item(pppoe_tree, hf_pppoed_tag_max_payload, tvb,
638                                                             tagstart+4, poe_tag_length, FALSE);
639                                         break;
640                                 case PPPOE_TAG_IP_RT_ADD:
641                                         proto_tree_add_item(pppoe_tree, hf_pppoed_tag_ip_route_add, tvb,
642                                                             tagstart+4, poe_tag_length, FALSE);
643                                         break;
644
645                                 /* These error tag values should be interpreted as a utf-8 unterminated
646                                    strings. */
647                                 case PPPOE_TAG_SVC_ERR:
648                                         proto_tree_add_item(pppoe_tree, hf_pppoed_tag_service_name_error, tvb,
649                                                             tagstart+4, poe_tag_length, FALSE);
650                                         break;
651                                 case PPPOE_TAG_AC_ERR:
652                                         proto_tree_add_item(pppoe_tree, hf_pppoed_tag_ac_system_error, tvb,
653                                                             tagstart+4, poe_tag_length, FALSE);
654                                         break;
655                                 case PPPOE_TAG_GENERIC_ERR:
656                                         proto_tree_add_item(pppoe_tree, hf_pppoed_tag_generic_error, tvb,
657                                                             tagstart+4, poe_tag_length, FALSE);
658                                         break;
659
660                                 /* Get out if see end-of-list tag */
661                                 case PPPOE_TAG_EOL:
662                                         return;
663
664                                 default:
665                                         if (poe_tag_length > 0 )
666                                         {
667                                                 /* Presumably unknown tag;
668                                                    show tag value if we didn't
669                                                    do it above */
670                                                 if (!global_pppoe_show_tags_and_lengths)
671                                                 {
672                                                         proto_tree_add_item(pppoe_tree, hf_pppoed_tag, tvb, tagstart, 2, FALSE);
673                                                         proto_tree_add_item(pppoe_tree, hf_pppoed_tag_length, tvb, tagstart+2, 2, FALSE);
674                                                 }
675                                                 proto_tree_add_item(pppoe_tree, hf_pppoed_tag_unknown_data, tvb,
676                                                                 tagstart+2, poe_tag_length, FALSE);
677                                         }
678                         }
679
680                         tagstart += (4 + poe_tag_length);
681                 }
682         }
683 }
684
685
686 /* Discovery protocol, i.e. PPP session not yet established */
687 static void dissect_pppoed(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
688 {
689         guint8  pppoe_code;
690         guint16 reported_payload_length;
691
692         proto_tree  *pppoe_tree = NULL;
693         proto_item  *ti;
694
695         col_set_str(pinfo->cinfo, COL_PROTOCOL, "PPPoED");
696         col_clear(pinfo->cinfo, COL_INFO);
697
698         /* Start Decoding Here. */
699         pppoe_code = tvb_get_guint8(tvb, 1);
700
701         if (check_col(pinfo->cinfo, COL_INFO))
702         {
703                 col_append_str(pinfo->cinfo, COL_INFO, val_to_str(pppoe_code, code_vals, "Unknown"));
704         }
705
706         /* Read length of payload */
707         reported_payload_length = tvb_get_ntohs(tvb, 4);
708
709         if (tree)
710         {
711                 ti = proto_tree_add_item(tree, proto_pppoed, tvb, 0, reported_payload_length+6, FALSE);
712                 pppoe_tree = proto_item_add_subtree(ti, ett_pppoed);
713
714                 /* Dissect fixed fields */
715                 proto_tree_add_item(pppoe_tree, hf_pppoe_version, tvb, 0, 1, FALSE);
716                 proto_tree_add_item(pppoe_tree, hf_pppoe_type, tvb, 0, 1, FALSE);
717                 proto_tree_add_item(pppoe_tree, hf_pppoe_code, tvb, 1, 1, FALSE);
718                 proto_tree_add_item(pppoe_tree, hf_pppoe_session_id, tvb, 2, 2, FALSE);
719                 proto_tree_add_item(pppoe_tree, hf_pppoe_payload_length, tvb, 4, 2, FALSE);
720         }
721
722         /* Now dissect any tags */
723         if (reported_payload_length > 0)
724         {
725                 dissect_pppoe_tags(tvb, pinfo, 6, pppoe_tree, 6+reported_payload_length);
726         }
727
728 }
729
730 void proto_register_pppoed(void)
731 {
732         static hf_register_info hf[] =
733         {
734                 /* Discovery tag fields */
735                 { &hf_pppoed_tags,
736                         { "PPPoE Tags", "pppoed.tags", FT_NONE, BASE_NONE,
737                                  NULL, 0x0, NULL, HFILL
738                         }
739                 },
740                 { &hf_pppoed_tag,
741                         { "Tag", "pppoed.tag", FT_UINT16, BASE_HEX,
742                                  VALS(tag_vals), 0x0, NULL, HFILL
743                         }
744                 },
745                 { &hf_pppoed_tag_length,
746                         { "Tag Length", "pppoed.tag_length", FT_UINT16, BASE_DEC,
747                                  NULL, 0x0, NULL, HFILL
748                         }
749                 },
750                 { &hf_pppoed_tag_length_8,
751                         { "Tag Length", "pppoed.tag_length_8", FT_UINT8, BASE_DEC,
752                                  NULL, 0x0, NULL, HFILL
753                         }
754                 },
755                 { &hf_pppoed_tag_unknown_data,
756                         { "Unknown Data", "pppoed.tag.unknown_data", FT_BYTES, BASE_NONE,
757                                  NULL, 0x0, NULL, HFILL
758                         }
759                 },
760                 { &hf_pppoed_tag_service_name,
761                         { "Service-Name", "pppoed.tags.service_name", FT_STRING, BASE_NONE,
762                                  NULL, 0x0, NULL, HFILL
763                         }
764                 },
765                 { &hf_pppoed_tag_ac_name,
766                         { "AC-Name", "pppoed.tags.ac_name", FT_STRING, BASE_NONE,
767                                  NULL, 0x0, NULL, HFILL
768                         }
769                 },
770                 { &hf_pppoed_tag_host_uniq,
771                         { "Host-Uniq", "pppoed.tags.host_uniq", FT_BYTES, BASE_NONE,
772                                  NULL, 0x0, NULL, HFILL
773                         }
774                 },
775                 { &hf_pppoed_tag_ac_cookie,
776                         { "AC-Cookie", "pppoed.tags.ac_cookie", FT_BYTES, BASE_NONE,
777                                  NULL, 0x0, NULL, HFILL
778                         }
779                 },
780                 { &hf_pppoed_tag_vendor_id,
781                         { "Vendor id", "pppoed.tags.vendor_id", FT_UINT32, BASE_DEC,
782                                  NULL, 0x0, NULL, HFILL
783                         }
784                 },
785                 { &hf_pppoed_tag_vendor_unspecified,
786                         { "Vendor unspecified", "pppoed.tags.vendor_unspecified", FT_BYTES, BASE_NONE,
787                                  NULL, 0x0, NULL, HFILL
788                         }
789                 },
790                 { &hf_pppoed_tag_vspec_tags,
791                         { "Vendor Specific PPPoE Tags", "pppoed.tags.vendorspecific.tags", FT_NONE, BASE_NONE,
792                                  NULL, 0x0, NULL, HFILL
793                         }
794                 },
795                 { &hf_pppoed_tag_vspec_tag,
796                         { "Tag", "pppoed.tags.vendorspecific.tag", FT_UINT8, BASE_HEX,
797                                  VALS(vspec_tag_vals), 0x0, NULL, HFILL
798                         }
799                 },
800                 { &hf_pppoed_tag_vspec_circuit_id,
801                         { "Circuit ID", "pppoed.tags.circuit_id", FT_STRING, BASE_NONE,
802                                  NULL, 0x0, NULL, HFILL
803                         }
804                 },
805                 { &hf_pppoed_tag_vspec_remote_id,
806                         { "Remote ID", "pppoed.tags.remote_id", FT_STRING, BASE_NONE,
807                                  NULL, 0x0, NULL, HFILL
808                         }
809                 },
810                 { &hf_pppoed_tag_vspec_act_data_rate_up,
811                         { "Actual Data Rate Upstream", "pppoed.tags.act_data_rate_up", FT_UINT32, BASE_DEC,
812                                  NULL, 0x0, NULL, HFILL
813                         }
814                 },
815                 { &hf_pppoed_tag_vspec_act_data_rate_down,
816                         { "Actual Data Rate Downstream", "pppoed.tags.act_data_rate_down", FT_UINT32, BASE_DEC,
817                                  NULL, 0x0, NULL, HFILL
818                         }
819                 },
820                 { &hf_pppoed_tag_vspec_min_data_rate_up,
821                         { "Minimum Data Rate Upstream", "pppoed.tags.min_data_rate_up", FT_UINT32, BASE_DEC,
822                                  NULL, 0x0, NULL, HFILL
823                         }
824                 },
825                 { &hf_pppoed_tag_vspec_min_data_rate_down,
826                         { "Minimum Data Rate Downstream", "pppoed.tags.min_data_rate_down", FT_UINT32, BASE_DEC,
827                                  NULL, 0x0, NULL, HFILL
828                         }
829                 },
830                 { &hf_pppoed_tag_vspec_attainable_data_rate_up,
831                         { "Attainable DataRate Upstream", "pppoed.tags.attainable_data_rate_up", FT_UINT32, BASE_DEC,
832                                  NULL, 0x0, NULL, HFILL
833                         }
834                 },
835                 { &hf_pppoed_tag_vspec_attainable_data_rate_down,
836                         { "Attainable DataRate Downstream", "pppoed.tags.attainable_data_rate_down", FT_UINT32, BASE_DEC,
837                                  NULL, 0x0, NULL, HFILL
838                         }
839                 },
840                 { &hf_pppoed_tag_vspec_max_data_rate_up,
841                         { "Maximum Data Rate Upstream", "pppoed.tags.max_data_rate_up", FT_UINT32, BASE_DEC,
842                                  NULL, 0x0, NULL, HFILL
843                         }
844                 },
845                 { &hf_pppoed_tag_vspec_max_data_rate_down,
846                         { "Maximum Data Rate Downstream", "pppoed.tags.max_data_rate_down", FT_UINT32, BASE_DEC,
847                                  NULL, 0x0, NULL, HFILL
848                         }
849                 },
850                 { &hf_pppoed_tag_vspec_min_data_rate_up_lp,
851                         { "Min DataRate Upstream in low power state", "pppoed.tags.min_data_rate_up_lp", FT_UINT32, BASE_DEC,
852                                  NULL, 0x0, NULL, HFILL
853                         }
854                 },
855                 { &hf_pppoed_tag_vspec_min_data_rate_down_lp,
856                         { "Minimum Data Rate Downstream in low power state", "pppoed.tags.min_data_rate_down_lp", FT_UINT32, BASE_DEC,
857                                  NULL, 0x0, NULL, HFILL
858                         }
859                 },
860                 { &hf_pppoed_tag_vspec_max_int_delay_up,
861                         { "Max Interleaving Delay Upstream", "pppoed.tags.max_int_delay_up", FT_UINT32, BASE_DEC,
862                                  NULL, 0x0, NULL, HFILL
863                         }
864                 },
865                 { &hf_pppoed_tag_vspec_act_int_delay_up,
866                         { "Actual Interleaving Delay Upstream", "pppoed.tags.act_int_delay_up", FT_UINT32, BASE_DEC,
867                                  NULL, 0x0, NULL, HFILL
868                         }
869                 },
870                 { &hf_pppoed_tag_vspec_max_int_delay_down,
871                         { "Maximum Interleaving Delay Downstream", "pppoed.tags.max_int_delay_down", FT_UINT32, BASE_DEC,
872                                  NULL, 0x0, NULL, HFILL
873                         }
874                 },
875                 { &hf_pppoed_tag_vspec_act_int_delay_down,
876                         { "Actual Interleaving Delay Downstream", "pppoed.tags.act_int_delay_down", FT_UINT32, BASE_DEC,
877                                  NULL, 0x0, NULL, HFILL
878                         }
879                 },
880                 { &hf_pppoed_tag_vspec_access_loop_encapsulation,
881                         { "Access-Loop-Encapsulation", "pppoed.tags.access_loop_encap", FT_NONE, BASE_NONE,
882                                  NULL, 0x0, NULL, HFILL
883                         }
884                 },
885                 { &hf_pppoed_tag_vspec_access_loop_encap_data_link,
886                         { "Data link", "pppoed.tags.access_loop_encap.data_link", FT_UINT8, BASE_HEX,
887                                  VALS(vspec_tag_dslf_access_loop_encap_data_link_vals), 0x0, NULL, HFILL
888                         }
889                 },
890                 { &hf_pppoed_tag_vspec_access_loop_encap_encap_1,
891                         { "Encaps 1", "pppoed.tags.access_loop_encap.encap_1", FT_UINT8, BASE_HEX,
892                                  VALS(vspec_tag_dslf_access_loop_encap_encap_1_vals), 0x0, NULL, HFILL
893                         }
894                 },
895                 { &hf_pppoed_tag_vspec_access_loop_encap_encap_2,
896                         { "Encaps 1", "pppoed.tags.access_loop_encap.encap_2", FT_UINT8, BASE_HEX,
897                                  VALS(vspec_tag_dslf_access_loop_encap_encap_2_vals), 0x0, NULL, HFILL
898                         }
899                 },
900                 { &hf_pppoed_tag_credits,
901                         { "Credits", "pppoed.tags.credits", FT_BYTES, BASE_NONE,
902                                  NULL, 0x0, NULL, HFILL
903                         }
904                 },
905                 { &hf_pppoed_tag_credits_fcn,
906                         { "FCN", "pppoed.tags.credits.fcn", FT_UINT16, BASE_DEC,
907                                  NULL, 0x0, NULL, HFILL
908                         }
909                 },
910                 { &hf_pppoed_tag_credits_bcn,
911                         { "BCN", "pppoed.tags.credits.bcn", FT_UINT16, BASE_DEC,
912                                  NULL, 0x0, NULL, HFILL
913                         }
914                 },
915                 { &hf_pppoed_tag_metrics,
916                         { "Metrics", "pppoed.tags.metrics", FT_BYTES, BASE_NONE,
917                                  NULL, 0x0, NULL, HFILL
918                         }
919                 },
920                 { &hf_pppoed_tag_metrics_r,
921                         { "Receive Only", "pppoed.tags.metrics.r", FT_BOOLEAN, 16,
922                                  NULL, PPPOE_RCV_ONLY_MASK, NULL, HFILL
923                         }
924                 },
925                 { &hf_pppoed_tag_mdr_units,
926                         { "MDR Units", "pppoed.tags.metrics.mdr_units", FT_UINT16, BASE_HEX,
927                                  VALS(datarate_scale_vals), PPPOE_MDR_MASK, NULL, HFILL
928                         }
929                 },
930                 { &hf_pppoed_tag_cdr_units,
931                         { "CDR Units", "pppoed.tags.metrics.cdr_units", FT_UINT16, BASE_HEX,
932                                  VALS(datarate_scale_vals), PPPOE_CDR_MASK, NULL, HFILL
933                         }
934                 },
935                 { &hf_pppoed_tag_metrics_rlq,
936                         { "Relative Link Quality", "pppoed.tags.metrics.rlq", FT_UINT8, BASE_DEC,
937                                  NULL, 0x0, NULL, HFILL
938                         }
939                 },
940                 { &hf_pppoed_tag_metrics_resource,
941                         { "Resource", "pppoed.tags.metrics.resource", FT_UINT8, BASE_DEC,
942                                  NULL, 0x0, NULL, HFILL
943                         }
944                 },
945                 { &hf_pppoed_tag_metrics_latency,
946                         { "Latency", "pppoed.tags.metrics.latency", FT_UINT16, BASE_DEC,
947                                  NULL, 0x0, NULL, HFILL
948                         }
949                 },
950                 { &hf_pppoed_tag_metrics_curr_drate,
951                         { "Curr. datarate", "pppoed.tags.metrics.curr_drate", FT_UINT16, BASE_DEC,
952                                  NULL, 0x0, NULL, HFILL
953                         }
954                 },
955                 { &hf_pppoed_tag_metrics_max_drate,
956                         { "Max. datarate", "pppoed.tags.metrics.max_drate", FT_UINT16, BASE_DEC,
957                                  NULL, 0x0, NULL, HFILL
958                         }
959                 },
960                 { &hf_pppoed_tag_seq_num,
961                         { "Sequence Number", "pppoed.tags.seq_num", FT_UINT16, BASE_HEX,
962                                  NULL, 0x0, NULL, HFILL
963                         }
964                 },
965                 { &hf_pppoed_tag_cred_scale,
966                         { "Credit Scale Factor", "pppoed.tags.credit_scale", FT_UINT16, BASE_DEC,
967                                  NULL, 0x0, NULL, HFILL
968                         }
969                 },
970                 { &hf_pppoed_tag_relay_session_id,
971                         { "Relay-Session-Id", "pppoed.tags.relay_session_id", FT_BYTES, BASE_NONE,
972                                  NULL, 0x0, NULL, HFILL
973                         }
974                 },
975                 { &hf_pppoed_tag_hurl,
976                         { "HURL", "pppoed.tags.hurl", FT_BYTES, BASE_NONE,
977                                  NULL, 0x0, NULL, HFILL
978                         }
979                 },
980                 { &hf_pppoed_tag_motm,
981                         { "MOTM", "pppoed.tags.motm", FT_BYTES, BASE_NONE,
982                                  NULL, 0x0, NULL, HFILL
983                         }
984                 },
985                 { &hf_pppoed_tag_max_payload,
986                         { "PPP Max Palyload", "pppoed.tags.max_payload", FT_BYTES, BASE_NONE,
987                                  NULL, 0x0, NULL, HFILL
988                         }
989                 },
990                 { &hf_pppoed_tag_ip_route_add,
991                         { "IP Route Add", "pppoed.tags.ip_route_add", FT_BYTES, BASE_NONE,
992                                  NULL, 0x0, NULL, HFILL
993                         }
994                 },
995                 { &hf_pppoed_tag_service_name_error,
996                         { "Service-Name-Error", "pppoed.tags.service_name_error", FT_STRING, BASE_NONE,
997                                  NULL, 0x0, NULL, HFILL
998                         }
999                 },
1000                 { &hf_pppoed_tag_ac_system_error,
1001                         { "AC-System-Error", "pppoed.tags.ac_system_error", FT_STRING, BASE_NONE,
1002                                  NULL, 0x0, NULL, HFILL
1003                         }
1004                 },
1005                 { &hf_pppoed_tag_generic_error,
1006                         { "Generic-Error", "pppoed.tags.generic_error", FT_STRING, BASE_NONE,
1007                                  NULL, 0x0, NULL, HFILL
1008                         }
1009                 }
1010         };
1011
1012         static gint *ett[] = {
1013                 &ett_pppoed,
1014                 &ett_pppoed_tags,
1015                 &ett_pppoed_tag_vspec_dslf_access_loop_encaps
1016         };
1017
1018         module_t *pppoed_module;
1019
1020         /* Register protocol and fields */
1021         proto_pppoed = proto_register_protocol("PPP-over-Ethernet Discovery",
1022                                                "PPPoED", "pppoed");
1023         proto_register_subtree_array(ett, array_length(ett));
1024         proto_register_field_array(proto_pppoed, hf, array_length(hf));
1025
1026         /* Preference setting */
1027         pppoed_module = prefs_register_protocol(proto_pppoed, NULL);
1028         prefs_register_bool_preference(pppoed_module, "show_tags_and_lengths",
1029                                        "Show tag values and lengths",
1030                                        "Show values of tags and lengths of data fields",
1031                                        &global_pppoe_show_tags_and_lengths);
1032 }
1033
1034 void proto_reg_handoff_pppoed(void)
1035 {
1036         dissector_handle_t pppoed_handle;
1037
1038         pppoed_handle = create_dissector_handle(dissect_pppoed, proto_pppoed);
1039         dissector_add_uint("ethertype", ETHERTYPE_PPPOED, pppoed_handle);
1040 }
1041
1042
1043 /* Session protocol, i.e. PPP session established */
1044 static void dissect_pppoes(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1045 {
1046         guint8  pppoe_code;
1047         guint16 pppoe_session_id;
1048         guint16 reported_payload_length;
1049         guint16 poe_tag;
1050         guint16 poe_tag_length;
1051         gint    actual_payload_length;
1052         gint    length, reported_length;
1053         gint    credit_offset = 0, tagstart = 0;
1054         guint16 cp_code;
1055
1056         proto_tree  *pppoe_tree;
1057         proto_item  *ti = NULL;
1058         tvbuff_t    *next_tvb;
1059
1060         col_set_str(pinfo->cinfo, COL_PROTOCOL, "PPPoES");
1061         col_clear(pinfo->cinfo, COL_INFO);
1062
1063         /* Start Decoding Here. */
1064         pppoe_code = tvb_get_guint8(tvb, 1);
1065
1066         if (check_col(pinfo->cinfo,COL_INFO))
1067         {
1068                 col_add_str(pinfo->cinfo, COL_INFO,
1069                              val_to_str(pppoe_code, code_vals, "Unknown"));
1070         }
1071
1072         pppoe_session_id = tvb_get_ntohs(tvb, 2);
1073         reported_payload_length = tvb_get_ntohs(tvb, 4);
1074         actual_payload_length = tvb_reported_length_remaining(tvb, 6);
1075
1076         if (tree)
1077         {
1078                 ti = proto_tree_add_item(tree, proto_pppoes, tvb, 0, 6, FALSE);
1079                 pppoe_tree = proto_item_add_subtree(ti, ett_pppoe);
1080
1081                 proto_tree_add_item(pppoe_tree, hf_pppoe_version, tvb, 0, 1, FALSE);
1082                 proto_tree_add_item(pppoe_tree, hf_pppoe_type, tvb, 0, 1, FALSE);
1083                 proto_tree_add_item(pppoe_tree, hf_pppoe_code, tvb, 1, 1, FALSE);
1084                 proto_tree_add_item(pppoe_tree, hf_pppoe_session_id, tvb, 2, 2, FALSE);
1085                 ti = proto_tree_add_item(pppoe_tree, hf_pppoe_payload_length, tvb, 4, 2, FALSE);
1086
1087
1088                 if (PPPOE_TAG_CREDITS == tvb_get_ntohs(tvb, 6))
1089                 {
1090                         tagstart = 6;
1091                         poe_tag = tvb_get_ntohs(tvb, tagstart);
1092                         poe_tag_length = tvb_get_ntohs(tvb, tagstart + 2);
1093
1094                         /* Create tags subtree */
1095                         ti = proto_tree_add_item(pppoe_tree, hf_pppoes_tags, tvb, tagstart, 8, FALSE);
1096                         pppoe_tree = proto_item_add_subtree(ti, ett_pppoes_tags);
1097
1098                         /* Show tag data */
1099                         if (poe_tag_length == 4)
1100                         {
1101                                 proto_tree_add_item(pppoe_tree, hf_pppoes_tag_credits_fcn, tvb,
1102                                         tagstart+4, 2, FALSE);
1103                                 proto_tree_add_item(pppoe_tree, hf_pppoes_tag_credits_bcn, tvb,
1104                                         tagstart+6, 2, FALSE);
1105                         } else {
1106                                 proto_tree_add_item(pppoe_tree, hf_pppoed_tag_credits, tvb,
1107                                         tagstart+4, poe_tag_length, FALSE);
1108                         }
1109
1110                         credit_offset = 8;
1111                 }
1112         }
1113
1114         /*
1115          * The only reason why the payload length from the header
1116          * should differ from the remaining data in the packet
1117          * would be if the total packet length, including Ethernet
1118          * CRC, were < 64 bytes, so that padding was required.
1119          *
1120          * That means that you have 14 bytes of Ethernet header,
1121          * 4 bytes of FCS, and fewer than 46 bytes of PPPoE packet.
1122          *
1123          * If that's not the case, we report a difference between
1124          * the payload length in the packet, and the amount of
1125          * data following the PPPoE header, as an error.
1126          */
1127         if (tvb_reported_length(tvb) > 46) {
1128                 /*
1129                  * Be forgiving about a possible trailing FCS.
1130                  *
1131                  * XXX - this dissector currently doesn't know
1132                  * whether any extra data past the end of the PPP
1133                  * payload is an FCS or not.
1134                  *
1135                  * If we know that we have an FCS, or that we don't
1136                  * have an FCS, we should have been handed a tvbuff
1137                  * without the FCS, and we should just do the strict
1138                  * length check.
1139                  *
1140                  * If we don't know whether we have an FCS, then:
1141                  *
1142                  *   if this isn't over Ethernet - the "E" in "PPPoE"
1143                  *   nonwithstanding, it can also run on top of 802.11,
1144                  *   for example - there's no trailer, so any data
1145                  *   past the payload length is either an FCS or
1146                  *   bogus;
1147                  *
1148                  *   if this is over Ethernet, there shouldn't be
1149                  *   a trailer, as the packet is long enough not to
1150                  *   require a trailer, as per the above;
1151                  *
1152                  * so perhaps we should assume that if we have exactly
1153                  * 4 bytes of extra information, it's an FCS, otherwise
1154                  * it's not.
1155                  *
1156                  * Perhaps we need to have a routine to call to
1157                  * do all the length checking, etc., and call it
1158                  * from here and from other dissectors where the
1159                  * protocol has a length field, or have a way to
1160                  * tell the dissector that called us which field
1161                  * has the length field and have *that* dissector
1162                  * do the length checking and add the expert info
1163                  * to the length field, *after* it does all the
1164                  * FCS heuristics.
1165                  */
1166
1167                 /* retrieve the control protocol code if it's there */
1168                 cp_code = tvb_get_ntohs(tvb, 6);
1169                 /*
1170                  * The session payload length expressly does not include pad bytes
1171                  *  when LCP or IPCP are present, so avoid the spurious error message
1172                  */
1173                 if ((cp_code != PPP_LCP) && (cp_code != PPP_IPCP) &&
1174                         (reported_payload_length != actual_payload_length) &&
1175                         ((reported_payload_length + 4) != actual_payload_length)) {
1176                         proto_item_append_text(ti, " [incorrect, should be %u]",
1177                                 actual_payload_length);
1178                         expert_add_info_format(pinfo, ti, PI_MALFORMED,
1179                                 PI_WARN, "Possible bad payload length %u != %u",
1180                                 reported_payload_length, actual_payload_length);
1181                 }
1182         }
1183
1184         /*
1185          * Construct a tvbuff containing the PPP packet.
1186          */
1187         length = tvb_length_remaining(tvb, 6);
1188         reported_length = tvb_reported_length_remaining(tvb, 6);
1189         DISSECTOR_ASSERT(length >= 0);
1190         DISSECTOR_ASSERT(reported_length >= 0);
1191         if (length > reported_length)
1192                 length = reported_length;
1193         if ((guint)length > reported_payload_length)
1194                 length = reported_payload_length;
1195         if ((guint)reported_length > reported_payload_length)
1196                 reported_length = reported_payload_length;
1197         next_tvb = tvb_new_subset(tvb,(6 + credit_offset),
1198                                 (length - credit_offset),
1199                                 (reported_length - credit_offset));
1200         call_dissector(ppp_handle,next_tvb,pinfo,tree);
1201 }
1202
1203 void proto_register_pppoes(void)
1204 {
1205
1206         static hf_register_info hf[] =
1207         {
1208                 { &hf_pppoes_tags,
1209                         { "PPPoE Tags", "pppoes.tags", FT_NONE, BASE_NONE,
1210                                  NULL, 0x0, NULL, HFILL
1211                         }
1212                 },
1213                 { &hf_pppoes_tag,
1214                         { "Tag", "pppoes.tag", FT_UINT16, BASE_HEX,
1215                                  VALS(tag_vals), 0x0, NULL, HFILL
1216                         }
1217                 },
1218                 { &hf_pppoes_tag_credits,
1219                         { "Credits", "pppoes.tags.credits", FT_BYTES, BASE_NONE,
1220                                  NULL, 0x0, NULL, HFILL
1221                         }
1222                 },
1223                 { &hf_pppoes_tag_credits_fcn,
1224                         { "FCN", "pppoes.tags.credits.fcn", FT_UINT16, BASE_DEC,
1225                                  NULL, 0x0, NULL, HFILL
1226                         }
1227                 },
1228                 { &hf_pppoes_tag_credits_bcn,
1229                         { "BCN", "pppoes.tags.credits.bcn", FT_UINT16, BASE_DEC,
1230                                  NULL, 0x0, NULL, HFILL
1231                         }
1232                 }
1233         };
1234
1235         static gint *ett[] = {
1236                 &ett_pppoes,
1237                 &ett_pppoes_tags
1238         };
1239
1240         /* Register protocol */
1241         proto_pppoes = proto_register_protocol("PPP-over-Ethernet Session", "PPPoES", "pppoes");
1242
1243         proto_register_subtree_array(ett, array_length(ett));
1244         proto_register_field_array(proto_pppoes, hf, array_length(hf));
1245 }
1246
1247 void proto_register_pppoe(void)
1248 {
1249         static hf_register_info hf[] =
1250         {
1251                 /* These fields common to discovery and session protocols */
1252                 { &hf_pppoe_version,
1253                         { "Version", "pppoe.version", FT_UINT8, BASE_DEC,
1254                                  NULL, 0xf0, NULL, HFILL
1255                         }
1256                 },
1257                 { &hf_pppoe_type,
1258                         { "Type", "pppoe.type", FT_UINT8, BASE_DEC,
1259                                  NULL, 0x0f, NULL, HFILL
1260                         }
1261                 },
1262                 { &hf_pppoe_code,
1263                         { "Code", "pppoe.code", FT_UINT8, BASE_HEX,
1264                                  VALS(code_vals), 0x0, NULL, HFILL
1265                         }
1266                 },
1267                 { &hf_pppoe_session_id,
1268                         { "Session ID", "pppoe.session_id", FT_UINT16, BASE_HEX,
1269                                  NULL, 0x0, NULL, HFILL
1270                         }
1271                 },
1272                 { &hf_pppoe_payload_length,
1273                         { "Payload Length", "pppoe.payload_length", FT_UINT16, BASE_DEC,
1274                                  NULL, 0x0, NULL, HFILL
1275                         }
1276                 }
1277         };
1278
1279         static gint *ett[] = {
1280                 &ett_pppoe
1281         };
1282
1283         /* Register protocol */
1284         proto_pppoe = proto_register_protocol("PPP-over-Ethernet", "PPPoE", "pppoe");
1285
1286         proto_register_subtree_array(ett, array_length(ett));
1287         proto_register_field_array(proto_pppoe, hf, array_length(hf));
1288
1289 }
1290
1291 void proto_reg_handoff_pppoes(void)
1292 {
1293         dissector_handle_t pppoes_handle  =
1294             create_dissector_handle(dissect_pppoes, proto_pppoes);
1295         dissector_add_uint("ethertype", ETHERTYPE_PPPOES, pppoes_handle);
1296
1297         /* Get a handle for the PPP dissector */
1298         ppp_handle = find_dissector("ppp");
1299 }