#include <emem.h> not req'd
[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
37 static int proto_pppoed = -1;
38
39 /* Common to session and discovery protocols */
40 static gint hf_pppoe_version = -1;
41 static gint hf_pppoe_type = -1;
42 static gint hf_pppoe_code = -1;
43 static gint hf_pppoe_session_id = -1;
44 static gint hf_pppoe_payload_length = -1;
45
46 /* Discovery protocol fields */
47 static gint hf_pppoed_tags = -1;
48 static gint hf_pppoed_tag = -1;
49 static gint hf_pppoed_tag_length = -1;
50 static gint hf_pppoed_tag_unknown_data = -1;
51 static gint hf_pppoed_tag_service_name = -1;
52 static gint hf_pppoed_tag_ac_name = -1;
53 static gint hf_pppoed_tag_host_uniq = -1;
54 static gint hf_pppoed_tag_ac_cookie = -1;
55 static gint hf_pppoed_tag_vendor_id = -1;
56 static gint hf_pppoed_tag_vendor_unspecified = -1;
57 static gint hf_pppoed_tag_credits = -1;
58 static gint hf_pppoed_tag_credits_fcn = -1;
59 static gint hf_pppoed_tag_credits_bcn = -1;
60 static gint hf_pppoed_tag_metrics = -1;
61 static gint hf_pppoed_tag_metrics_r = -1;
62 static gint hf_pppoed_tag_metrics_rlq = -1;
63 static gint hf_pppoed_tag_metrics_resource = -1;
64 static gint hf_pppoed_tag_metrics_latency = -1;
65 static gint hf_pppoed_tag_metrics_curr_drate = -1;
66 static gint hf_pppoed_tag_metrics_max_drate = -1;
67 static gint hf_pppoed_tag_mdr_units = -1;
68 static gint hf_pppoed_tag_cdr_units = -1;
69 static gint hf_pppoed_tag_seq_num = -1;
70 static gint hf_pppoed_tag_cred_scale = -1;
71 static gint hf_pppoed_tag_relay_session_id = -1;
72 static gint hf_pppoed_tag_hurl = -1;
73 static gint hf_pppoed_tag_motm = -1;
74 static gint hf_pppoed_tag_max_payload = -1;
75 static gint hf_pppoed_tag_ip_route_add = -1;
76 static gint hf_pppoed_tag_service_name_error = -1;
77 static gint hf_pppoed_tag_ac_system_error = -1;
78 static gint hf_pppoed_tag_generic_error = -1;
79
80 /* Session protocol fields */
81 static gint hf_pppoes_tags = -1;
82 static gint hf_pppoes_tag = -1;
83 static gint hf_pppoes_tag_credits = -1;
84 static gint hf_pppoes_tag_credits_fcn = -1;
85 static gint hf_pppoes_tag_credits_bcn = -1;
86
87 /* Session protocol fields */
88
89 static gint ett_pppoed = -1;
90 static gint ett_pppoed_tags = -1;
91
92 static int proto_pppoes = -1;
93
94 static gint ett_pppoes = -1;
95 static gint ett_pppoes_tags = -1;
96
97 /* PPPoE parent fields */
98
99 static int proto_pppoe = -1;
100 static gint ett_pppoe = -1;
101
102
103 /* Handle for calling for ppp dissector to handle session data */
104 static dissector_handle_t ppp_handle;
105
106
107 /* Preference for showing discovery tag values and lengths */
108 static gboolean global_pppoe_show_tags_and_lengths = FALSE;
109
110
111 #define PPPOE_CODE_SESSION    0x00
112 #define PPPOE_CODE_PADO       0x07
113 #define PPPOE_CODE_PADI       0x09
114 #define PPPOE_CODE_PADG       0x0a
115 #define PPPOE_CODE_PADC       0x0b
116 #define PPPOE_CODE_PADQ       0x0c
117 #define PPPOE_CODE_PADR       0x19
118 #define PPPOE_CODE_PADS       0x65
119 #define PPPOE_CODE_PADT       0xa7
120 #define PPPOE_CODE_PADM       0xd3
121 #define PPPOE_CODE_PADN       0xd4
122
123 #define PPPOE_TAG_EOL         0x0000
124 #define PPPOE_TAG_SVC_NAME    0x0101
125 #define PPPOE_TAG_AC_NAME     0x0102
126 #define PPPOE_TAG_HOST_UNIQ   0x0103
127 #define PPPOE_TAG_AC_COOKIE   0x0104
128 #define PPPOE_TAG_VENDOR      0x0105
129 #define PPPOE_TAG_CREDITS     0x0106
130 #define PPPOE_TAG_METRICS     0x0107
131 #define PPPOE_TAG_SEQ_NUM     0x0108
132 #define PPPOE_TAG_CRED_SCALE  0x0109
133 #define PPPOE_TAG_RELAY_ID    0x0110
134 #define PPPOE_TAG_HURL        0x0111
135 #define PPPOE_TAG_MOTM        0x0112
136 #define PPPOE_TAG_MAX_PAYLD   0x0120
137 #define PPPOE_TAG_IP_RT_ADD   0x0121
138 #define PPPOE_TAG_SVC_ERR     0x0201
139 #define PPPOE_TAG_AC_ERR      0x0202
140 #define PPPOE_TAG_GENERIC_ERR 0x0203
141
142 #define PPPOE_CDR_MASK        0x06
143 #define PPPOE_MDR_MASK        0x18
144 #define PPPOE_RCV_ONLY_MASK   0x01
145
146 #define PPPOE_SCALE_KBPS      0x00
147 #define PPPOE_SCALE_MBPS      0x01
148 #define PPPOE_SCALE_GBPS      0x02
149 #define PPPOE_SCALE_TBPS      0x03
150
151
152 static const value_string code_vals[] = {
153                 {PPPOE_CODE_SESSION, "Session Data"                             },
154                 {PPPOE_CODE_PADO, "Active Discovery Offer (PADO)"               },
155                 {PPPOE_CODE_PADI, "Active Discovery Initiation (PADI)"          },
156                 {PPPOE_CODE_PADG, "Active Discovery Session-Grant (PADG)"       },
157                 {PPPOE_CODE_PADC, "Active Discovery Session-Credit Resp.(PADC)" },
158                 {PPPOE_CODE_PADQ, "Active Discovery Quality (PADQ)"             },
159                 {PPPOE_CODE_PADR, "Active Discovery Request (PADR)"             },
160                 {PPPOE_CODE_PADS, "Active Discovery Session-confirmation (PADS)"},
161                 {PPPOE_CODE_PADT, "Active Discovery Terminate (PADT)"           },
162                 {PPPOE_CODE_PADM, "Active Discovery Message (PADM)"             },
163                 {PPPOE_CODE_PADN, "Active Discovery Network (PADN)"             },
164                 {0,               NULL                                          }
165 };
166
167
168 static const value_string tag_vals[] = {
169                 {PPPOE_TAG_EOL,        "End-Of-List"       },
170                 {PPPOE_TAG_SVC_NAME,   "Service-Name"      },
171                 {PPPOE_TAG_AC_NAME,    "AC-Name"           },
172                 {PPPOE_TAG_HOST_UNIQ,  "Host-Uniq"         },
173                 {PPPOE_TAG_AC_COOKIE,  "AC-Cookie"         },
174                 {PPPOE_TAG_VENDOR,     "Vendor-Specific"   },
175                 {PPPOE_TAG_CREDITS,    "Credits"           },
176                 {PPPOE_TAG_METRICS,    "Metrics"           },
177                 {PPPOE_TAG_SEQ_NUM,    "Seqence Number"    },
178                 {PPPOE_TAG_CRED_SCALE, "Credit Scale Factor"},
179                 {PPPOE_TAG_RELAY_ID,   "Relay-Session-Id"  },
180                 {PPPOE_TAG_HURL,       "HURL"              },
181                 {PPPOE_TAG_MOTM,       "MOTM"              },
182                 {PPPOE_TAG_MAX_PAYLD,  "PPP-Max-Payload"   },
183                 {PPPOE_TAG_IP_RT_ADD,  "IP Route Add"      },
184                 {PPPOE_TAG_SVC_ERR,    "Service-Name-Error"},
185                 {PPPOE_TAG_AC_ERR,     "AC-System-Error"   },
186                 {PPPOE_TAG_GENERIC_ERR,"Generic-Error"     },
187                 {0,                    NULL                }
188 };
189
190 const value_string datarate_scale_vals[] = {
191                 {PPPOE_SCALE_KBPS,      "kilobits per second"},
192                 {PPPOE_SCALE_MBPS,      "megabits per second"},
193                 {PPPOE_SCALE_GBPS,      "gigabits per second"},
194                 {PPPOE_SCALE_TBPS,      "terabits per second"},
195                 {0,                     NULL                 }
196 };
197
198
199 /* Dissect discovery protocol tags */
200 static void
201 dissect_pppoe_tags(tvbuff_t *tvb, packet_info *pinfo, int offset, proto_tree *tree,
202                    int payload_length)
203 {
204         guint16 poe_tag;
205         guint16 poe_tag_length;
206         int tagstart;
207         guint16 poe_rsv = 0;
208
209         proto_tree  *pppoe_tree;
210         proto_item  *ti;
211
212         /* Start Decoding Here. */
213         if (tree)
214         {
215                 /* Create tags subtree */
216                 ti = proto_tree_add_item(tree, hf_pppoed_tags, tvb, offset, payload_length-6, FALSE);
217                 pppoe_tree = proto_item_add_subtree(ti, ett_pppoed_tags);
218
219                 tagstart = offset;
220
221                 /* Loop until all data seen or End-Of-List tag found */
222                 while (tagstart <= payload_length-2)
223                 {
224                         poe_tag = tvb_get_ntohs(tvb, tagstart);
225                         poe_tag_length = tvb_get_ntohs(tvb, tagstart + 2);
226
227                         /* Tag value and data length */
228                         if (global_pppoe_show_tags_and_lengths)
229                         {
230                                 proto_tree_add_item(pppoe_tree, hf_pppoed_tag, tvb, tagstart, 2, FALSE);
231                                 proto_tree_add_item(pppoe_tree, hf_pppoed_tag_length, tvb, tagstart+2, 2, FALSE);
232                         }
233
234                         /* Show tag data */
235                         switch (poe_tag)
236                         {
237                                 case PPPOE_TAG_SVC_NAME:
238                                         if (poe_tag_length > 0)
239                                         {
240                                                 proto_tree_add_item(pppoe_tree, hf_pppoed_tag_service_name, tvb,
241                                                                     tagstart+4, poe_tag_length, FALSE);
242                                         }
243                                         break;
244                                 case PPPOE_TAG_AC_NAME:
245                                         proto_tree_add_item(pppoe_tree, hf_pppoed_tag_ac_name, tvb,
246                                                             tagstart+4, poe_tag_length, FALSE);
247                                         /* Show AC-Name in info column */
248                                         if (check_col(pinfo->cinfo, COL_INFO))
249                                         {
250                                                 col_append_fstr(pinfo->cinfo, COL_INFO, " AC-Name='%s'",
251                                                                tvb_get_ephemeral_string(tvb, tagstart+4, poe_tag_length));
252                                         }
253                                         break;
254                                 case PPPOE_TAG_HOST_UNIQ:
255                                         proto_tree_add_item(pppoe_tree, hf_pppoed_tag_host_uniq, tvb,
256                                                             tagstart+4, poe_tag_length, FALSE);
257                                         break;
258                                 case PPPOE_TAG_AC_COOKIE:
259                                         proto_tree_add_item(pppoe_tree, hf_pppoed_tag_ac_cookie, tvb,
260                                                             tagstart+4, poe_tag_length, FALSE);
261                                         break;
262                                 case PPPOE_TAG_VENDOR:
263                                         if (poe_tag_length >= 4)
264                                         {
265                                                 proto_tree_add_item(pppoe_tree, hf_pppoed_tag_vendor_id, tvb,
266                                                                                         tagstart+4, 4, FALSE);
267                                         }
268                                         if (poe_tag_length > 4)
269                                         {
270                                                 proto_tree_add_item(pppoe_tree, hf_pppoed_tag_vendor_unspecified, tvb,
271                                                                     tagstart+4+4, poe_tag_length-4, FALSE);
272                                         }
273                                         break;
274                                 case PPPOE_TAG_CREDITS:
275                                         if (poe_tag_length == 4)
276                                         {
277                                                 proto_tree_add_item(pppoe_tree, hf_pppoed_tag_credits_fcn, tvb,
278                                                                     tagstart+4, 2, FALSE);
279                                                 proto_tree_add_item(pppoe_tree, hf_pppoed_tag_credits_bcn, tvb,
280                                                                     tagstart+6, 2, FALSE);
281                                         } else {
282                                                 proto_tree_add_item(pppoe_tree, hf_pppoed_tag_credits, tvb,
283                                                                     tagstart+4, poe_tag_length, FALSE);
284                                         }
285                                         break;
286                                 case PPPOE_TAG_METRICS:
287                                         if (poe_tag_length == 10)
288                                         {
289                                                 poe_rsv = tvb_get_ntohs(tvb, tagstart+4);
290
291                                                 proto_tree_add_item(pppoe_tree, hf_pppoed_tag_mdr_units, tvb,
292                                                                     tagstart+4, 2, FALSE);
293                                                 proto_tree_add_item(pppoe_tree, hf_pppoed_tag_cdr_units, tvb,
294                                                                     tagstart+4, 2, FALSE);
295                                                 proto_tree_add_item(pppoe_tree, hf_pppoed_tag_metrics_r, tvb,
296                                                                     tagstart+4, 2, FALSE);
297                                                 proto_tree_add_item(pppoe_tree, hf_pppoed_tag_metrics_rlq, tvb,
298                                                                     tagstart+6, 1, FALSE);
299                                                 proto_tree_add_item(pppoe_tree, hf_pppoed_tag_metrics_resource, tvb,
300                                                                     tagstart+7, 1, FALSE);
301                                                 proto_tree_add_item(pppoe_tree, hf_pppoed_tag_metrics_latency, tvb,
302                                                                     tagstart+8, 2, FALSE);
303
304                                                 /* CDR */
305                                                 ti = proto_tree_add_item(pppoe_tree, hf_pppoed_tag_metrics_curr_drate, tvb,
306                                                                          tagstart+10, 2, FALSE);
307
308                                                 switch ((poe_rsv & PPPOE_CDR_MASK) >> 1)
309                                                 {
310                                                 case (PPPOE_SCALE_KBPS):
311                                                     proto_item_append_text(ti, " kbps");
312                                                     break;
313                                                 case (PPPOE_SCALE_MBPS):
314                                                     proto_item_append_text(ti, " mbps");
315                                                     break;
316                                                 case (PPPOE_SCALE_GBPS):
317                                                     proto_item_append_text(ti, " gbps");
318                                                     break;
319                                                 case (PPPOE_SCALE_TBPS):
320                                                     proto_item_append_text(ti, " tbps");
321                                                     break;
322                                                 }
323
324                                                 /* MDR */
325                                                 ti = proto_tree_add_item(pppoe_tree, hf_pppoed_tag_metrics_max_drate, tvb,
326                                                                     tagstart+12, 2, FALSE);
327
328                                                 switch ((poe_rsv & PPPOE_MDR_MASK) >> 3)
329                                                 {
330                                                 case (PPPOE_SCALE_KBPS):
331                                                     proto_item_append_text(ti, " kbps");
332                                                     break;
333                                                 case (PPPOE_SCALE_MBPS):
334                                                     proto_item_append_text(ti, " mbps");
335                                                     break;
336                                                 case (PPPOE_SCALE_GBPS):
337                                                     proto_item_append_text(ti, " gbps");
338                                                     break;
339                                                 case (PPPOE_SCALE_TBPS):
340                                                     proto_item_append_text(ti, " tbps");
341                                                     break;
342                                                 }
343
344                                         } else {
345                                                 proto_tree_add_item(pppoe_tree, hf_pppoed_tag_metrics, tvb,
346                                                                     tagstart+4, poe_tag_length, FALSE);
347                                         }
348                                         break;
349                                 case PPPOE_TAG_SEQ_NUM:
350                                         proto_tree_add_item(pppoe_tree, hf_pppoed_tag_seq_num, tvb,
351                                                             tagstart+4, poe_tag_length, FALSE);
352                                         break;
353                                 case PPPOE_TAG_CRED_SCALE:
354                                         proto_tree_add_item(pppoe_tree, hf_pppoed_tag_cred_scale, tvb,
355                                                             tagstart+4, poe_tag_length, FALSE);
356                                         break;
357                                 case PPPOE_TAG_RELAY_ID:
358                                         proto_tree_add_item(pppoe_tree, hf_pppoed_tag_relay_session_id, tvb,
359                                                             tagstart+4, poe_tag_length, FALSE);
360                                         break;
361                                 case PPPOE_TAG_HURL:
362                                         proto_tree_add_item(pppoe_tree, hf_pppoed_tag_hurl, tvb,
363                                                             tagstart+4, poe_tag_length, FALSE);
364                                         break;
365                                 case PPPOE_TAG_MOTM:
366                                         proto_tree_add_item(pppoe_tree, hf_pppoed_tag_motm, tvb,
367                                                             tagstart+4, poe_tag_length, FALSE);
368                                         break;
369                                 case PPPOE_TAG_MAX_PAYLD:
370                                         proto_tree_add_item(pppoe_tree, hf_pppoed_tag_max_payload, tvb,
371                                                             tagstart+4, poe_tag_length, FALSE);
372                                         break;
373                                 case PPPOE_TAG_IP_RT_ADD:
374                                         proto_tree_add_item(pppoe_tree, hf_pppoed_tag_ip_route_add, tvb,
375                                                             tagstart+4, poe_tag_length, FALSE);
376                                         break;
377
378                                 /* These error tag values should be interpreted as a utf-8 unterminated
379                                    strings. */
380                                 case PPPOE_TAG_SVC_ERR:
381                                         proto_tree_add_item(pppoe_tree, hf_pppoed_tag_service_name_error, tvb,
382                                                             tagstart+4, poe_tag_length, FALSE);
383                                         break;
384                                 case PPPOE_TAG_AC_ERR:
385                                         proto_tree_add_item(pppoe_tree, hf_pppoed_tag_ac_system_error, tvb,
386                                                             tagstart+4, poe_tag_length, FALSE);
387                                         break;
388                                 case PPPOE_TAG_GENERIC_ERR:
389                                         proto_tree_add_item(pppoe_tree, hf_pppoed_tag_generic_error, tvb,
390                                                             tagstart+4, poe_tag_length, FALSE);
391                                         break;
392
393                                 /* Get out if see end-of-list tag */
394                                 case PPPOE_TAG_EOL:
395                                         return;
396
397                                 default:
398                                         if (poe_tag_length > 0 )
399                                         {
400                                                 /* Presumably unknown tag;
401                                                    show tag value if we didn't
402                                                    do it above */
403                                                 if (!global_pppoe_show_tags_and_lengths)
404                                                 {
405                                                         proto_tree_add_item(pppoe_tree, hf_pppoed_tag, tvb, tagstart, 2, FALSE);
406                                                         proto_tree_add_item(pppoe_tree, hf_pppoed_tag_length, tvb, tagstart+2, 2, FALSE);
407                                                 }
408                                                 proto_tree_add_item(pppoe_tree, hf_pppoed_tag_unknown_data, tvb,
409                                                                     tagstart+4, poe_tag_length, FALSE);
410                                         }
411                         }
412
413                         tagstart += (4 + poe_tag_length);
414                 }
415         }
416 }
417
418
419 /* Discovery protocol, i.e. PPP session not yet established */
420 static void dissect_pppoed(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
421 {
422         guint8  pppoe_code;
423         guint16 reported_payload_length;
424
425         proto_tree  *pppoe_tree = NULL;
426         proto_item  *ti;
427
428         if (check_col(pinfo->cinfo, COL_PROTOCOL))
429         {
430                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "PPPoED");
431         }
432         if (check_col(pinfo->cinfo, COL_INFO))
433         {
434                 col_clear(pinfo->cinfo, COL_INFO);
435         }
436
437         /* Start Decoding Here. */
438         pppoe_code = tvb_get_guint8(tvb, 1);
439
440         if (check_col(pinfo->cinfo, COL_INFO))
441         {
442                 col_append_str(pinfo->cinfo, COL_INFO, val_to_str(pppoe_code, code_vals, "Unknown"));
443         }
444
445         /* Read length of payload */
446         reported_payload_length = tvb_get_ntohs(tvb, 4);
447
448         if (tree)
449         {
450                 ti = proto_tree_add_item(tree, proto_pppoed, tvb, 0, reported_payload_length+6, FALSE);
451                 pppoe_tree = proto_item_add_subtree(ti, ett_pppoed);
452
453                 /* Dissect fixed fields */
454                 proto_tree_add_item(pppoe_tree, hf_pppoe_version, tvb, 0, 1, FALSE);
455                 proto_tree_add_item(pppoe_tree, hf_pppoe_type, tvb, 0, 1, FALSE);
456                 proto_tree_add_item(pppoe_tree, hf_pppoe_code, tvb, 1, 1, FALSE);
457                 proto_tree_add_item(pppoe_tree, hf_pppoe_session_id, tvb, 2, 2, FALSE);
458                 proto_tree_add_item(pppoe_tree, hf_pppoe_payload_length, tvb, 4, 2, FALSE);
459         }
460
461         /* Now dissect any tags */
462         if (reported_payload_length > 0)
463         {
464                 dissect_pppoe_tags(tvb, pinfo, 6, pppoe_tree, 6+reported_payload_length);
465         }
466
467 }
468
469 void proto_register_pppoed(void)
470 {
471         static hf_register_info hf[] =
472         {
473                 /* Discovery tag fields */
474                 { &hf_pppoed_tags,
475                         { "PPPoE Tags", "pppoed.tags", FT_NONE, BASE_NONE,
476                                  NULL, 0x0, "", HFILL
477                         }
478                 },
479                 { &hf_pppoed_tag,
480                         { "Tag", "pppoed.tag", FT_UINT16, BASE_HEX,
481                                  VALS(tag_vals), 0x0, "", HFILL
482                         }
483                 },
484                 { &hf_pppoed_tag_length,
485                         { "Tag Length", "pppoed.tag_length", FT_UINT16, BASE_DEC,
486                                  NULL, 0x0, "", HFILL
487                         }
488                 },
489                 { &hf_pppoed_tag_unknown_data,
490                         { "Unknown Data", "pppoed.tag.unknown_data", FT_BYTES, BASE_NONE,
491                                  NULL, 0x0, "", HFILL
492                         }
493                 },
494                 { &hf_pppoed_tag_service_name,
495                         { "Service-Name", "pppoed.tags.service_name", FT_STRING, BASE_NONE,
496                                  NULL, 0x0, "", HFILL
497                         }
498                 },
499                 { &hf_pppoed_tag_ac_name,
500                         { "AC-Name", "pppoed.tags.ac_name", FT_STRING, BASE_NONE,
501                                  NULL, 0x0, "", HFILL
502                         }
503                 },
504                 { &hf_pppoed_tag_host_uniq,
505                         { "Host-Uniq", "pppoed.tags.host_uniq", FT_BYTES, BASE_NONE,
506                                  NULL, 0x0, "", HFILL
507                         }
508                 },
509                 { &hf_pppoed_tag_ac_cookie,
510                         { "AC-Cookie", "pppoed.tags.ac_cookie", FT_BYTES, BASE_NONE,
511                                  NULL, 0x0, "", HFILL
512                         }
513                 },
514                 { &hf_pppoed_tag_vendor_id,
515                         { "Vendor id", "pppoed.tags.vendor_id", FT_UINT32, BASE_HEX,
516                                  NULL, 0x0, "", HFILL
517                         }
518                 },
519                 { &hf_pppoed_tag_vendor_unspecified,
520                         { "Vendor unspecified", "pppoed.tags.vendor_unspecified", FT_BYTES, BASE_HEX,
521                                  NULL, 0x0, "", HFILL
522                         }
523                 },
524                 { &hf_pppoed_tag_credits,
525                         { "Credits", "pppoed.tags.credits", FT_BYTES, BASE_HEX,
526                                  NULL, 0x0, "", HFILL
527                         }
528                 },
529                 { &hf_pppoed_tag_credits_fcn,
530                         { "FCN", "pppoed.tags.credits.fcn", FT_UINT16, BASE_DEC,
531                                  NULL, 0x0, "", HFILL
532                         }
533                 },
534                 { &hf_pppoed_tag_credits_bcn,
535                         { "BCN", "pppoed.tags.credits.bcn", FT_UINT16, BASE_DEC,
536                                  NULL, 0x0, "", HFILL
537                         }
538                 },
539                 { &hf_pppoed_tag_metrics,
540                         { "Metrics", "pppoed.tags.metrics", FT_BYTES, BASE_HEX,
541                                  NULL, 0x0, "", HFILL
542                         }
543                 },
544                 { &hf_pppoed_tag_metrics_r,
545                         { "Receive Only", "pppoed.tags.metrics.r", FT_BOOLEAN, 16,
546                                  NULL, PPPOE_RCV_ONLY_MASK, "", HFILL
547                         }
548                 },
549                 { &hf_pppoed_tag_mdr_units,
550                         { "MDR Units", "pppoed.tags.metrics.mdr_units", FT_UINT16, BASE_HEX,
551                                  VALS(datarate_scale_vals), PPPOE_MDR_MASK, "", HFILL
552                         }
553                 },
554                 { &hf_pppoed_tag_cdr_units,
555                         { "CDR Units", "pppoed.tags.metrics.cdr_units", FT_UINT16, BASE_HEX,
556                                  VALS(datarate_scale_vals), PPPOE_CDR_MASK, "", HFILL
557                         }
558                 },
559                 { &hf_pppoed_tag_metrics_rlq,
560                         { "Relative Link Quality", "pppoed.tags.metrics.rlq", FT_UINT8, BASE_DEC,
561                                  NULL, 0x0, "", HFILL
562                         }
563                 },
564                 { &hf_pppoed_tag_metrics_resource,
565                         { "Resource", "pppoed.tags.metrics.resource", FT_UINT8, BASE_DEC,
566                                  NULL, 0x0, "", HFILL
567                         }
568                 },
569                 { &hf_pppoed_tag_metrics_latency,
570                         { "Latency", "pppoed.tags.metrics.latency", FT_UINT16, BASE_DEC,
571                                  NULL, 0x0, "", HFILL
572                         }
573                 },
574                 { &hf_pppoed_tag_metrics_curr_drate,
575                         { "Curr. datarate", "pppoed.tags.metrics.curr_drate", FT_UINT16, BASE_DEC,
576                                  NULL, 0x0, "", HFILL
577                         }
578                 },
579                 { &hf_pppoed_tag_metrics_max_drate,
580                         { "Max. datarate", "pppoed.tags.metrics.max_drate", FT_UINT16, BASE_DEC,
581                                  NULL, 0x0, "", HFILL
582                         }
583                 },
584                 { &hf_pppoed_tag_seq_num,
585                         { "Sequence Number", "pppoed.tags.seq_num", FT_UINT16, BASE_HEX,
586                                  NULL, 0x0, "", HFILL
587                         }
588                 },
589                 { &hf_pppoed_tag_cred_scale,
590                         { "Credit Scale Factor", "pppoed.tags.credit_scale", FT_UINT16, BASE_DEC,
591                                  NULL, 0x0, "", HFILL
592                         }
593                 },
594                 { &hf_pppoed_tag_relay_session_id,
595                         { "Relay-Session-Id", "pppoed.tags.relay_session_id", FT_BYTES, BASE_NONE,
596                                  NULL, 0x0, "", HFILL
597                         }
598                 },
599                 { &hf_pppoed_tag_hurl,
600                         { "HURL", "pppoed.tags.hurl", FT_BYTES, BASE_HEX,
601                                  NULL, 0x0, "", HFILL
602                         }
603                 },
604                 { &hf_pppoed_tag_motm,
605                         { "MOTM", "pppoed.tags.motm", FT_BYTES, BASE_HEX,
606                                  NULL, 0x0, "", HFILL
607                         }
608                 },
609                 { &hf_pppoed_tag_max_payload,
610                         { "PPP Max Palyload", "pppoed.tags.max_payload", FT_BYTES, BASE_HEX,
611                                  NULL, 0x0, "", HFILL
612                         }
613                 },
614                 { &hf_pppoed_tag_ip_route_add,
615                         { "IP Route Add", "pppoed.tags.ip_route_add", FT_BYTES, BASE_HEX,
616                                  NULL, 0x0, "", HFILL
617                         }
618                 },
619                 { &hf_pppoed_tag_service_name_error,
620                         { "Service-Name-Error", "pppoed.tags.service_name_error", FT_STRING, BASE_NONE,
621                                  NULL, 0x0, "", HFILL
622                         }
623                 },
624                 { &hf_pppoed_tag_ac_system_error,
625                         { "AC-System-Error", "pppoed.tags.ac_system_error", FT_STRING, BASE_NONE,
626                                  NULL, 0x0, "", HFILL
627                         }
628                 },
629                 { &hf_pppoed_tag_generic_error,
630                         { "Generic-Error", "pppoed.tags.generic_error", FT_STRING, BASE_NONE,
631                                  NULL, 0x0, "", HFILL
632                         }
633                 }
634         };
635
636         static gint *ett[] = {
637                 &ett_pppoed,
638                 &ett_pppoed_tags
639         };
640
641         module_t *pppoed_module;
642
643         /* Register protocol and fields */
644         proto_pppoed = proto_register_protocol("PPP-over-Ethernet Discovery",
645                                                "PPPoED", "pppoed");
646         proto_register_subtree_array(ett, array_length(ett));
647         proto_register_field_array(proto_pppoed, hf, array_length(hf));
648
649         /* Preference setting */
650         pppoed_module = prefs_register_protocol(proto_pppoed, NULL);
651         prefs_register_bool_preference(pppoed_module, "show_tags_and_lengths",
652                                        "Show tag values and lengths",
653                                        "Show values of tags and lengths of data fields",
654                                        &global_pppoe_show_tags_and_lengths);
655 }
656
657 void proto_reg_handoff_pppoed(void)
658 {
659         dissector_handle_t pppoed_handle;
660
661         pppoed_handle = create_dissector_handle(dissect_pppoed, proto_pppoed);
662         dissector_add("ethertype", ETHERTYPE_PPPOED, pppoed_handle);
663 }
664
665
666 /* Session protocol, i.e. PPP session established */
667 static void dissect_pppoes(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
668 {
669         guint8  pppoe_code;
670         guint16 pppoe_session_id;
671         guint16 reported_payload_length;
672         guint16 poe_tag;
673         guint16 poe_tag_length;
674         gint    actual_payload_length;
675         gint    length, reported_length;
676         gint    credit_offset = 0, tagstart = 0;
677
678         proto_tree  *pppoe_tree;
679         proto_item  *ti = NULL;
680         tvbuff_t    *next_tvb;
681
682         if (check_col(pinfo->cinfo, COL_PROTOCOL))
683         {
684                 col_set_str(pinfo->cinfo,COL_PROTOCOL, "PPPoES");
685         }
686         if (check_col(pinfo->cinfo,COL_INFO))
687         {
688                 col_clear(pinfo->cinfo,COL_INFO);
689         }
690
691         /* Start Decoding Here. */
692         pppoe_code = tvb_get_guint8(tvb, 1);
693
694         if (check_col(pinfo->cinfo,COL_INFO))
695         {
696                 col_add_str(pinfo->cinfo, COL_INFO,
697                              val_to_str(pppoe_code, code_vals, "Unknown"));
698         }
699
700         pppoe_session_id = tvb_get_ntohs(tvb, 2);
701         reported_payload_length = tvb_get_ntohs(tvb, 4);
702         actual_payload_length = tvb_reported_length_remaining(tvb, 6);
703
704         if (tree)
705         {
706                 ti = proto_tree_add_item(tree, proto_pppoes, tvb, 0, 6, FALSE);
707                 pppoe_tree = proto_item_add_subtree(ti, ett_pppoe);
708
709                 proto_tree_add_item(pppoe_tree, hf_pppoe_version, tvb, 0, 1, FALSE);
710                 proto_tree_add_item(pppoe_tree, hf_pppoe_type, tvb, 0, 1, FALSE);
711                 proto_tree_add_item(pppoe_tree, hf_pppoe_code, tvb, 1, 1, FALSE);
712                 proto_tree_add_item(pppoe_tree, hf_pppoe_session_id, tvb, 2, 2, FALSE);
713                 ti = proto_tree_add_item(pppoe_tree, hf_pppoe_payload_length, tvb, 4, 2, FALSE);
714
715
716                 if (PPPOE_TAG_CREDITS == tvb_get_ntohs(tvb, 6))
717                 {
718                         tagstart = 6;
719                         poe_tag = tvb_get_ntohs(tvb, tagstart);
720                         poe_tag_length = tvb_get_ntohs(tvb, tagstart + 2);
721
722                         /* Create tags subtree */
723                         ti = proto_tree_add_item(pppoe_tree, hf_pppoes_tags, tvb, tagstart, 8, FALSE);
724                         pppoe_tree = proto_item_add_subtree(ti, ett_pppoes_tags);
725
726                         /* Show tag data */
727                         if (poe_tag_length == 4)
728                         {
729                                 proto_tree_add_item(pppoe_tree, hf_pppoes_tag_credits_fcn, tvb,
730                                         tagstart+4, 2, FALSE);
731                                 proto_tree_add_item(pppoe_tree, hf_pppoes_tag_credits_bcn, tvb,
732                                         tagstart+6, 2, FALSE);
733                         } else {
734                                 proto_tree_add_item(pppoe_tree, hf_pppoed_tag_credits, tvb,
735                                         tagstart+4, poe_tag_length, FALSE);
736                         }
737
738                         credit_offset = 8;
739                 }
740         }
741         /*
742          * The only reason why the payload length from the header
743          * should differ from the remaining data in the packet
744          * would be if the total packet length, including Ethernet
745          * CRC, were < 64 bytes, so that padding was required.
746          *
747          * That means that you have 14 bytes of Ethernet header,
748          * 4 bytes of FCS, and fewer than 46 bytes of PPPoE packet.
749          *
750          * If that's not the case, we report a difference between
751          * the payload length in the packet, and the amount of
752          * data following the PPPoE header, as an error.
753          */
754         if (tvb_reported_length(tvb) > 46 &&
755             reported_payload_length != actual_payload_length) {
756                 proto_item_append_text(ti, " [incorrect, should be %u]",
757                                        actual_payload_length);
758                 expert_add_info_format(pinfo, ti, PI_MALFORMED,
759                     PI_WARN, "Possible bad payload length %u != %u",
760                     reported_payload_length, actual_payload_length);
761         }
762
763         /*
764          * Construct a tvbuff containing the PPP packet.
765          */
766         length = tvb_length_remaining(tvb, 6);
767         reported_length = tvb_reported_length_remaining(tvb, 6);
768         DISSECTOR_ASSERT(length >= 0);
769         DISSECTOR_ASSERT(reported_length >= 0);
770         if (length > reported_length)
771                 length = reported_length;
772         if ((guint)length > reported_payload_length)
773                 length = reported_payload_length;
774         if ((guint)reported_length > reported_payload_length)
775                 reported_length = reported_payload_length;
776         next_tvb = tvb_new_subset(tvb,(6 + credit_offset),
777                                 (length - credit_offset),
778                                 (reported_length - credit_offset));
779         call_dissector(ppp_handle,next_tvb,pinfo,tree);
780 }
781
782 void proto_register_pppoes(void)
783 {
784
785         static hf_register_info hf[] =
786         {
787                 { &hf_pppoes_tags,
788                         { "PPPoE Tags", "pppoes.tags", FT_NONE, BASE_NONE,
789                                  NULL, 0x0, "", HFILL
790                         }
791                 },
792                 { &hf_pppoes_tag,
793                         { "Tag", "pppoes.tag", FT_UINT16, BASE_HEX,
794                                  VALS(tag_vals), 0x0, "", HFILL
795                         }
796                 },
797                 { &hf_pppoes_tag_credits,
798                         { "Credits", "pppoes.tags.credits", FT_BYTES, BASE_HEX,
799                                  NULL, 0x0, "", HFILL
800                         }
801                 },
802                 { &hf_pppoes_tag_credits_fcn,
803                         { "FCN", "pppoes.tags.credits.fcn", FT_UINT16, BASE_DEC,
804                                  NULL, 0x0, "", HFILL
805                         }
806                 },
807                 { &hf_pppoes_tag_credits_bcn,
808                         { "BCN", "pppoes.tags.credits.bcn", FT_UINT16, BASE_DEC,
809                                  NULL, 0x0, "", HFILL
810                         }
811                 }
812         };
813
814         static gint *ett[] = {
815                 &ett_pppoes,
816                 &ett_pppoes_tags
817         };
818
819         /* Register protocol */
820         proto_pppoes = proto_register_protocol("PPP-over-Ethernet Session", "PPPoES", "pppoes");
821
822         proto_register_subtree_array(ett, array_length(ett));
823         proto_register_field_array(proto_pppoes, hf, array_length(hf));
824 }
825
826 void proto_register_pppoe(void)
827 {
828         static hf_register_info hf[] =
829         {
830                 /* These fields common to discovery and session protocols */
831                 { &hf_pppoe_version,
832                         { "Version", "pppoe.version", FT_UINT8, BASE_DEC,
833                                  NULL, 0xf0, "", HFILL
834                         }
835                 },
836                 { &hf_pppoe_type,
837                         { "Type", "pppoe.type", FT_UINT8, BASE_DEC,
838                                  NULL, 0x0f, "", HFILL
839                         }
840                 },
841                 { &hf_pppoe_code,
842                         { "Code", "pppoe.code", FT_UINT8, BASE_HEX,
843                                  VALS(code_vals), 0x0, "", HFILL
844                         }
845                 },
846                 { &hf_pppoe_session_id,
847                         { "Session ID", "pppoe.session_id", FT_UINT16, BASE_HEX,
848                                  NULL, 0x0, "", HFILL
849                         }
850                 },
851                 { &hf_pppoe_payload_length,
852                         { "Payload Length", "pppoe.payload_length", FT_UINT16, BASE_DEC,
853                                  NULL, 0x0, "", HFILL
854                         }
855                 }
856         };
857
858         static gint *ett[] = {
859                 &ett_pppoe
860         };
861
862         /* Register protocol */
863         proto_pppoe = proto_register_protocol("PPP-over-Ethernet", "PPPoE", "pppoe");
864
865         proto_register_subtree_array(ett, array_length(ett));
866         proto_register_field_array(proto_pppoe, hf, array_length(hf));
867
868 }
869
870 void proto_reg_handoff_pppoes(void)
871 {
872         dissector_handle_t pppoes_handle  =
873             create_dissector_handle(dissect_pppoes, proto_pppoes);
874         dissector_add("ethertype", ETHERTYPE_PPPOES, pppoes_handle);
875
876         /* Get a handle for the PPP dissector */
877         ppp_handle = find_dissector("ppp");
878 }