From Didier Gautheron:
[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,    "Sequence 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         proto_item  *pppoe_tree_tag_length_item = NULL;
212         proto_item  *item;
213
214         /* Start Decoding Here. */
215         if (tree)
216         {
217                 /* Create tags subtree */
218                 ti = proto_tree_add_item(tree, hf_pppoed_tags, tvb, offset, payload_length-6, FALSE);
219                 pppoe_tree = proto_item_add_subtree(ti, ett_pppoed_tags);
220
221                 tagstart = offset;
222
223                 /* Loop until all data seen or End-Of-List tag found */
224                 while (tagstart <= payload_length-2)
225                 {
226                         poe_tag = tvb_get_ntohs(tvb, tagstart);
227                         poe_tag_length = tvb_get_ntohs(tvb, tagstart + 2);
228
229                         /* Tag value and data length */
230                         if (global_pppoe_show_tags_and_lengths)
231                         {
232                                 proto_tree_add_item(pppoe_tree, hf_pppoed_tag, tvb, tagstart, 2, FALSE);
233                                 pppoe_tree_tag_length_item = 
234                                         proto_tree_add_item(pppoe_tree, hf_pppoed_tag_length, tvb, tagstart+2, 2, FALSE);
235                         }
236
237                         /* Show tag data */
238                         switch (poe_tag)
239                         {
240                                 case PPPOE_TAG_SVC_NAME:
241                                         if (poe_tag_length > 0)
242                                         {
243                                                 proto_tree_add_item(pppoe_tree, hf_pppoed_tag_service_name, tvb,
244                                                                     tagstart+4, poe_tag_length, FALSE);
245                                         }
246                                         break;
247                                 case PPPOE_TAG_AC_NAME:
248                                         proto_tree_add_item(pppoe_tree, hf_pppoed_tag_ac_name, tvb,
249                                                             tagstart+4, poe_tag_length, FALSE);
250                                         /* Show AC-Name in info column */
251                                         if (check_col(pinfo->cinfo, COL_INFO))
252                                         {
253                                                 col_append_fstr(pinfo->cinfo, COL_INFO, " AC-Name='%s'",
254                                                                tvb_get_ephemeral_string(tvb, tagstart+4, poe_tag_length));
255                                         }
256                                         break;
257                                 case PPPOE_TAG_HOST_UNIQ:
258                                         proto_tree_add_item(pppoe_tree, hf_pppoed_tag_host_uniq, tvb,
259                                                             tagstart+4, poe_tag_length, FALSE);
260                                         break;
261                                 case PPPOE_TAG_AC_COOKIE:
262                                         proto_tree_add_item(pppoe_tree, hf_pppoed_tag_ac_cookie, tvb,
263                                                             tagstart+4, poe_tag_length, FALSE);
264                                         break;
265                                 case PPPOE_TAG_VENDOR:
266                                         if (poe_tag_length >= 4)
267                                         {
268                                                 proto_tree_add_item(pppoe_tree, hf_pppoed_tag_vendor_id, tvb,
269                                                                                         tagstart+4, 4, FALSE);
270                                         }
271                                         if (poe_tag_length > 4)
272                                         {
273                                                 proto_tree_add_item(pppoe_tree, hf_pppoed_tag_vendor_unspecified, tvb,
274                                                                     tagstart+4+4, poe_tag_length-4, FALSE);
275                                         }
276                                         break;
277                                 case PPPOE_TAG_CREDITS:
278                                         if (poe_tag_length == 4)
279                                         {
280                                                 proto_tree_add_item(pppoe_tree, hf_pppoed_tag_credits_fcn, tvb,
281                                                                     tagstart+4, 2, FALSE);
282                                                 proto_tree_add_item(pppoe_tree, hf_pppoed_tag_credits_bcn, tvb,
283                                                                     tagstart+6, 2, FALSE);
284                                         } else {
285                                                 proto_tree_add_item(pppoe_tree, hf_pppoed_tag_credits, tvb,
286                                                                     tagstart+4, poe_tag_length, FALSE);
287                                         }
288                                         break;
289                                 case PPPOE_TAG_METRICS:
290                                         if (poe_tag_length == 10)
291                                         {
292                                                 poe_rsv = tvb_get_ntohs(tvb, tagstart+4);
293
294                                                 proto_tree_add_item(pppoe_tree, hf_pppoed_tag_mdr_units, tvb,
295                                                                     tagstart+4, 2, FALSE);
296                                                 proto_tree_add_item(pppoe_tree, hf_pppoed_tag_cdr_units, tvb,
297                                                                     tagstart+4, 2, FALSE);
298                                                 proto_tree_add_item(pppoe_tree, hf_pppoed_tag_metrics_r, tvb,
299                                                                     tagstart+4, 2, FALSE);
300                                                 proto_tree_add_item(pppoe_tree, hf_pppoed_tag_metrics_rlq, tvb,
301                                                                     tagstart+6, 1, FALSE);
302                                                 proto_tree_add_item(pppoe_tree, hf_pppoed_tag_metrics_resource, tvb,
303                                                                     tagstart+7, 1, FALSE);
304                                                 proto_tree_add_item(pppoe_tree, hf_pppoed_tag_metrics_latency, tvb,
305                                                                     tagstart+8, 2, FALSE);
306
307                                                 /* CDR */
308                                                 ti = proto_tree_add_item(pppoe_tree, hf_pppoed_tag_metrics_curr_drate, tvb,
309                                                                          tagstart+10, 2, FALSE);
310
311                                                 switch ((poe_rsv & PPPOE_CDR_MASK) >> 1)
312                                                 {
313                                                 case (PPPOE_SCALE_KBPS):
314                                                     proto_item_append_text(ti, " kbps");
315                                                     break;
316                                                 case (PPPOE_SCALE_MBPS):
317                                                     proto_item_append_text(ti, " mbps");
318                                                     break;
319                                                 case (PPPOE_SCALE_GBPS):
320                                                     proto_item_append_text(ti, " gbps");
321                                                     break;
322                                                 case (PPPOE_SCALE_TBPS):
323                                                     proto_item_append_text(ti, " tbps");
324                                                     break;
325                                                 }
326
327                                                 /* MDR */
328                                                 ti = proto_tree_add_item(pppoe_tree, hf_pppoed_tag_metrics_max_drate, tvb,
329                                                                     tagstart+12, 2, FALSE);
330
331                                                 switch ((poe_rsv & PPPOE_MDR_MASK) >> 3)
332                                                 {
333                                                 case (PPPOE_SCALE_KBPS):
334                                                     proto_item_append_text(ti, " kbps");
335                                                     break;
336                                                 case (PPPOE_SCALE_MBPS):
337                                                     proto_item_append_text(ti, " mbps");
338                                                     break;
339                                                 case (PPPOE_SCALE_GBPS):
340                                                     proto_item_append_text(ti, " gbps");
341                                                     break;
342                                                 case (PPPOE_SCALE_TBPS):
343                                                     proto_item_append_text(ti, " tbps");
344                                                     break;
345                                                 }
346
347                                         } else {
348                                                 proto_tree_add_item(pppoe_tree, hf_pppoed_tag_metrics, tvb,
349                                                                     tagstart+4, poe_tag_length, FALSE);
350                                         }
351                                         break;
352                                 case PPPOE_TAG_SEQ_NUM:
353                                         if (poe_tag_length == 2) {
354                                                 proto_tree_add_item(pppoe_tree, hf_pppoed_tag_seq_num, tvb,
355                                                                     tagstart+4, poe_tag_length, FALSE);
356                                         } else {
357                                                 if (global_pppoe_show_tags_and_lengths) {
358                                                         proto_item_append_text(pppoe_tree_tag_length_item, " [Wrong: should be 2]");
359                                                         item = pppoe_tree_tag_length_item;
360                                                 } else {
361                                                         item = proto_tree_add_text(pppoe_tree, tvb, tagstart+4, poe_tag_length,
362                                                             "%s: Wrong length: %u (expected 2)",
363                                                             proto_registrar_get_name(hf_pppoed_tag_seq_num),
364                                                             poe_tag_length);
365                                                 }
366                                                 expert_add_info_format(pinfo, item, PI_MALFORMED, PI_WARN, 
367                                                                        "Sequence Number tag: Wrong length: %u (expected 2)",
368                                                                        poe_tag_length);
369                                         }
370                                         break;
371                                 case PPPOE_TAG_CRED_SCALE:
372                                         if (poe_tag_length == 2) {
373                                                 proto_tree_add_item(pppoe_tree, hf_pppoed_tag_cred_scale, tvb,
374                                                                     tagstart+4, poe_tag_length, FALSE);
375                                         } else {
376                                                 if (global_pppoe_show_tags_and_lengths) {
377                                                         proto_item_append_text(pppoe_tree_tag_length_item, " [Wrong: should be 2]");
378                                                         item = pppoe_tree_tag_length_item;
379                                                 } else {
380                                                         item = proto_tree_add_text(pppoe_tree, tvb, tagstart+4, poe_tag_length,
381                                                             "%s: Wrong length: %u (expected 2)",
382                                                             proto_registrar_get_name(hf_pppoed_tag_cred_scale),
383                                                             poe_tag_length);
384                                                 }
385                                                 expert_add_info_format(pinfo, item, PI_MALFORMED, PI_WARN, 
386                                                                        "Credit Scale Factor tag: Wrong length: %u (expected 2)",
387                                                                        poe_tag_length);
388                                         }
389                                         break;
390                                 case PPPOE_TAG_RELAY_ID:
391                                         proto_tree_add_item(pppoe_tree, hf_pppoed_tag_relay_session_id, tvb,
392                                                             tagstart+4, poe_tag_length, FALSE);
393                                         break;
394                                 case PPPOE_TAG_HURL:
395                                         proto_tree_add_item(pppoe_tree, hf_pppoed_tag_hurl, tvb,
396                                                             tagstart+4, poe_tag_length, FALSE);
397                                         break;
398                                 case PPPOE_TAG_MOTM:
399                                         proto_tree_add_item(pppoe_tree, hf_pppoed_tag_motm, tvb,
400                                                             tagstart+4, poe_tag_length, FALSE);
401                                         break;
402                                 case PPPOE_TAG_MAX_PAYLD:
403                                         proto_tree_add_item(pppoe_tree, hf_pppoed_tag_max_payload, tvb,
404                                                             tagstart+4, poe_tag_length, FALSE);
405                                         break;
406                                 case PPPOE_TAG_IP_RT_ADD:
407                                         proto_tree_add_item(pppoe_tree, hf_pppoed_tag_ip_route_add, tvb,
408                                                             tagstart+4, poe_tag_length, FALSE);
409                                         break;
410
411                                 /* These error tag values should be interpreted as a utf-8 unterminated
412                                    strings. */
413                                 case PPPOE_TAG_SVC_ERR:
414                                         proto_tree_add_item(pppoe_tree, hf_pppoed_tag_service_name_error, tvb,
415                                                             tagstart+4, poe_tag_length, FALSE);
416                                         break;
417                                 case PPPOE_TAG_AC_ERR:
418                                         proto_tree_add_item(pppoe_tree, hf_pppoed_tag_ac_system_error, tvb,
419                                                             tagstart+4, poe_tag_length, FALSE);
420                                         break;
421                                 case PPPOE_TAG_GENERIC_ERR:
422                                         proto_tree_add_item(pppoe_tree, hf_pppoed_tag_generic_error, tvb,
423                                                             tagstart+4, poe_tag_length, FALSE);
424                                         break;
425
426                                 /* Get out if see end-of-list tag */
427                                 case PPPOE_TAG_EOL:
428                                         return;
429
430                                 default:
431                                         if (poe_tag_length > 0 )
432                                         {
433                                                 /* Presumably unknown tag;
434                                                    show tag value if we didn't
435                                                    do it above */
436                                                 if (!global_pppoe_show_tags_and_lengths)
437                                                 {
438                                                         proto_tree_add_item(pppoe_tree, hf_pppoed_tag, tvb, tagstart, 2, FALSE);
439                                                         proto_tree_add_item(pppoe_tree, hf_pppoed_tag_length, tvb, tagstart+2, 2, FALSE);
440                                                 }
441                                                 proto_tree_add_item(pppoe_tree, hf_pppoed_tag_unknown_data, tvb,
442                                                                     tagstart+4, poe_tag_length, FALSE);
443                                         }
444                         }
445
446                         tagstart += (4 + poe_tag_length);
447                 }
448         }
449 }
450
451
452 /* Discovery protocol, i.e. PPP session not yet established */
453 static void dissect_pppoed(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
454 {
455         guint8  pppoe_code;
456         guint16 reported_payload_length;
457
458         proto_tree  *pppoe_tree = NULL;
459         proto_item  *ti;
460
461         col_set_str(pinfo->cinfo, COL_PROTOCOL, "PPPoED");
462         col_clear(pinfo->cinfo, COL_INFO);
463
464         /* Start Decoding Here. */
465         pppoe_code = tvb_get_guint8(tvb, 1);
466
467         if (check_col(pinfo->cinfo, COL_INFO))
468         {
469                 col_append_str(pinfo->cinfo, COL_INFO, val_to_str(pppoe_code, code_vals, "Unknown"));
470         }
471
472         /* Read length of payload */
473         reported_payload_length = tvb_get_ntohs(tvb, 4);
474
475         if (tree)
476         {
477                 ti = proto_tree_add_item(tree, proto_pppoed, tvb, 0, reported_payload_length+6, FALSE);
478                 pppoe_tree = proto_item_add_subtree(ti, ett_pppoed);
479
480                 /* Dissect fixed fields */
481                 proto_tree_add_item(pppoe_tree, hf_pppoe_version, tvb, 0, 1, FALSE);
482                 proto_tree_add_item(pppoe_tree, hf_pppoe_type, tvb, 0, 1, FALSE);
483                 proto_tree_add_item(pppoe_tree, hf_pppoe_code, tvb, 1, 1, FALSE);
484                 proto_tree_add_item(pppoe_tree, hf_pppoe_session_id, tvb, 2, 2, FALSE);
485                 proto_tree_add_item(pppoe_tree, hf_pppoe_payload_length, tvb, 4, 2, FALSE);
486         }
487
488         /* Now dissect any tags */
489         if (reported_payload_length > 0)
490         {
491                 dissect_pppoe_tags(tvb, pinfo, 6, pppoe_tree, 6+reported_payload_length);
492         }
493
494 }
495
496 void proto_register_pppoed(void)
497 {
498         static hf_register_info hf[] =
499         {
500                 /* Discovery tag fields */
501                 { &hf_pppoed_tags,
502                         { "PPPoE Tags", "pppoed.tags", FT_NONE, BASE_NONE,
503                                  NULL, 0x0, NULL, HFILL
504                         }
505                 },
506                 { &hf_pppoed_tag,
507                         { "Tag", "pppoed.tag", FT_UINT16, BASE_HEX,
508                                  VALS(tag_vals), 0x0, NULL, HFILL
509                         }
510                 },
511                 { &hf_pppoed_tag_length,
512                         { "Tag Length", "pppoed.tag_length", FT_UINT16, BASE_DEC,
513                                  NULL, 0x0, NULL, HFILL
514                         }
515                 },
516                 { &hf_pppoed_tag_unknown_data,
517                         { "Unknown Data", "pppoed.tag.unknown_data", FT_BYTES, BASE_NONE,
518                                  NULL, 0x0, NULL, HFILL
519                         }
520                 },
521                 { &hf_pppoed_tag_service_name,
522                         { "Service-Name", "pppoed.tags.service_name", FT_STRING, BASE_NONE,
523                                  NULL, 0x0, NULL, HFILL
524                         }
525                 },
526                 { &hf_pppoed_tag_ac_name,
527                         { "AC-Name", "pppoed.tags.ac_name", FT_STRING, BASE_NONE,
528                                  NULL, 0x0, NULL, HFILL
529                         }
530                 },
531                 { &hf_pppoed_tag_host_uniq,
532                         { "Host-Uniq", "pppoed.tags.host_uniq", FT_BYTES, BASE_NONE,
533                                  NULL, 0x0, NULL, HFILL
534                         }
535                 },
536                 { &hf_pppoed_tag_ac_cookie,
537                         { "AC-Cookie", "pppoed.tags.ac_cookie", FT_BYTES, BASE_NONE,
538                                  NULL, 0x0, NULL, HFILL
539                         }
540                 },
541                 { &hf_pppoed_tag_vendor_id,
542                         { "Vendor id", "pppoed.tags.vendor_id", FT_UINT32, BASE_HEX,
543                                  NULL, 0x0, NULL, HFILL
544                         }
545                 },
546                 { &hf_pppoed_tag_vendor_unspecified,
547                         { "Vendor unspecified", "pppoed.tags.vendor_unspecified", FT_BYTES, BASE_NONE,
548                                  NULL, 0x0, NULL, HFILL
549                         }
550                 },
551                 { &hf_pppoed_tag_credits,
552                         { "Credits", "pppoed.tags.credits", FT_BYTES, BASE_NONE,
553                                  NULL, 0x0, NULL, HFILL
554                         }
555                 },
556                 { &hf_pppoed_tag_credits_fcn,
557                         { "FCN", "pppoed.tags.credits.fcn", FT_UINT16, BASE_DEC,
558                                  NULL, 0x0, NULL, HFILL
559                         }
560                 },
561                 { &hf_pppoed_tag_credits_bcn,
562                         { "BCN", "pppoed.tags.credits.bcn", FT_UINT16, BASE_DEC,
563                                  NULL, 0x0, NULL, HFILL
564                         }
565                 },
566                 { &hf_pppoed_tag_metrics,
567                         { "Metrics", "pppoed.tags.metrics", FT_BYTES, BASE_NONE,
568                                  NULL, 0x0, NULL, HFILL
569                         }
570                 },
571                 { &hf_pppoed_tag_metrics_r,
572                         { "Receive Only", "pppoed.tags.metrics.r", FT_BOOLEAN, 16,
573                                  NULL, PPPOE_RCV_ONLY_MASK, NULL, HFILL
574                         }
575                 },
576                 { &hf_pppoed_tag_mdr_units,
577                         { "MDR Units", "pppoed.tags.metrics.mdr_units", FT_UINT16, BASE_HEX,
578                                  VALS(datarate_scale_vals), PPPOE_MDR_MASK, NULL, HFILL
579                         }
580                 },
581                 { &hf_pppoed_tag_cdr_units,
582                         { "CDR Units", "pppoed.tags.metrics.cdr_units", FT_UINT16, BASE_HEX,
583                                  VALS(datarate_scale_vals), PPPOE_CDR_MASK, NULL, HFILL
584                         }
585                 },
586                 { &hf_pppoed_tag_metrics_rlq,
587                         { "Relative Link Quality", "pppoed.tags.metrics.rlq", FT_UINT8, BASE_DEC,
588                                  NULL, 0x0, NULL, HFILL
589                         }
590                 },
591                 { &hf_pppoed_tag_metrics_resource,
592                         { "Resource", "pppoed.tags.metrics.resource", FT_UINT8, BASE_DEC,
593                                  NULL, 0x0, NULL, HFILL
594                         }
595                 },
596                 { &hf_pppoed_tag_metrics_latency,
597                         { "Latency", "pppoed.tags.metrics.latency", FT_UINT16, BASE_DEC,
598                                  NULL, 0x0, NULL, HFILL
599                         }
600                 },
601                 { &hf_pppoed_tag_metrics_curr_drate,
602                         { "Curr. datarate", "pppoed.tags.metrics.curr_drate", FT_UINT16, BASE_DEC,
603                                  NULL, 0x0, NULL, HFILL
604                         }
605                 },
606                 { &hf_pppoed_tag_metrics_max_drate,
607                         { "Max. datarate", "pppoed.tags.metrics.max_drate", FT_UINT16, BASE_DEC,
608                                  NULL, 0x0, NULL, HFILL
609                         }
610                 },
611                 { &hf_pppoed_tag_seq_num,
612                         { "Sequence Number", "pppoed.tags.seq_num", FT_UINT16, BASE_HEX,
613                                  NULL, 0x0, NULL, HFILL
614                         }
615                 },
616                 { &hf_pppoed_tag_cred_scale,
617                         { "Credit Scale Factor", "pppoed.tags.credit_scale", FT_UINT16, BASE_DEC,
618                                  NULL, 0x0, NULL, HFILL
619                         }
620                 },
621                 { &hf_pppoed_tag_relay_session_id,
622                         { "Relay-Session-Id", "pppoed.tags.relay_session_id", FT_BYTES, BASE_NONE,
623                                  NULL, 0x0, NULL, HFILL
624                         }
625                 },
626                 { &hf_pppoed_tag_hurl,
627                         { "HURL", "pppoed.tags.hurl", FT_BYTES, BASE_NONE,
628                                  NULL, 0x0, NULL, HFILL
629                         }
630                 },
631                 { &hf_pppoed_tag_motm,
632                         { "MOTM", "pppoed.tags.motm", FT_BYTES, BASE_NONE,
633                                  NULL, 0x0, NULL, HFILL
634                         }
635                 },
636                 { &hf_pppoed_tag_max_payload,
637                         { "PPP Max Palyload", "pppoed.tags.max_payload", FT_BYTES, BASE_NONE,
638                                  NULL, 0x0, NULL, HFILL
639                         }
640                 },
641                 { &hf_pppoed_tag_ip_route_add,
642                         { "IP Route Add", "pppoed.tags.ip_route_add", FT_BYTES, BASE_NONE,
643                                  NULL, 0x0, NULL, HFILL
644                         }
645                 },
646                 { &hf_pppoed_tag_service_name_error,
647                         { "Service-Name-Error", "pppoed.tags.service_name_error", FT_STRING, BASE_NONE,
648                                  NULL, 0x0, NULL, HFILL
649                         }
650                 },
651                 { &hf_pppoed_tag_ac_system_error,
652                         { "AC-System-Error", "pppoed.tags.ac_system_error", FT_STRING, BASE_NONE,
653                                  NULL, 0x0, NULL, HFILL
654                         }
655                 },
656                 { &hf_pppoed_tag_generic_error,
657                         { "Generic-Error", "pppoed.tags.generic_error", FT_STRING, BASE_NONE,
658                                  NULL, 0x0, NULL, HFILL
659                         }
660                 }
661         };
662
663         static gint *ett[] = {
664                 &ett_pppoed,
665                 &ett_pppoed_tags
666         };
667
668         module_t *pppoed_module;
669
670         /* Register protocol and fields */
671         proto_pppoed = proto_register_protocol("PPP-over-Ethernet Discovery",
672                                                "PPPoED", "pppoed");
673         proto_register_subtree_array(ett, array_length(ett));
674         proto_register_field_array(proto_pppoed, hf, array_length(hf));
675
676         /* Preference setting */
677         pppoed_module = prefs_register_protocol(proto_pppoed, NULL);
678         prefs_register_bool_preference(pppoed_module, "show_tags_and_lengths",
679                                        "Show tag values and lengths",
680                                        "Show values of tags and lengths of data fields",
681                                        &global_pppoe_show_tags_and_lengths);
682 }
683
684 void proto_reg_handoff_pppoed(void)
685 {
686         dissector_handle_t pppoed_handle;
687
688         pppoed_handle = create_dissector_handle(dissect_pppoed, proto_pppoed);
689         dissector_add("ethertype", ETHERTYPE_PPPOED, pppoed_handle);
690 }
691
692
693 /* Session protocol, i.e. PPP session established */
694 static void dissect_pppoes(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
695 {
696         guint8  pppoe_code;
697         guint16 pppoe_session_id;
698         guint16 reported_payload_length;
699         guint16 poe_tag;
700         guint16 poe_tag_length;
701         gint    actual_payload_length;
702         gint    length, reported_length;
703         gint    credit_offset = 0, tagstart = 0;
704
705         proto_tree  *pppoe_tree;
706         proto_item  *ti = NULL;
707         tvbuff_t    *next_tvb;
708
709         col_set_str(pinfo->cinfo, COL_PROTOCOL, "PPPoES");
710         col_clear(pinfo->cinfo, COL_INFO);
711
712         /* Start Decoding Here. */
713         pppoe_code = tvb_get_guint8(tvb, 1);
714
715         if (check_col(pinfo->cinfo,COL_INFO))
716         {
717                 col_add_str(pinfo->cinfo, COL_INFO,
718                              val_to_str(pppoe_code, code_vals, "Unknown"));
719         }
720
721         pppoe_session_id = tvb_get_ntohs(tvb, 2);
722         reported_payload_length = tvb_get_ntohs(tvb, 4);
723         actual_payload_length = tvb_reported_length_remaining(tvb, 6);
724
725         if (tree)
726         {
727                 ti = proto_tree_add_item(tree, proto_pppoes, tvb, 0, 6, FALSE);
728                 pppoe_tree = proto_item_add_subtree(ti, ett_pppoe);
729
730                 proto_tree_add_item(pppoe_tree, hf_pppoe_version, tvb, 0, 1, FALSE);
731                 proto_tree_add_item(pppoe_tree, hf_pppoe_type, tvb, 0, 1, FALSE);
732                 proto_tree_add_item(pppoe_tree, hf_pppoe_code, tvb, 1, 1, FALSE);
733                 proto_tree_add_item(pppoe_tree, hf_pppoe_session_id, tvb, 2, 2, FALSE);
734                 ti = proto_tree_add_item(pppoe_tree, hf_pppoe_payload_length, tvb, 4, 2, FALSE);
735
736
737                 if (PPPOE_TAG_CREDITS == tvb_get_ntohs(tvb, 6))
738                 {
739                         tagstart = 6;
740                         poe_tag = tvb_get_ntohs(tvb, tagstart);
741                         poe_tag_length = tvb_get_ntohs(tvb, tagstart + 2);
742
743                         /* Create tags subtree */
744                         ti = proto_tree_add_item(pppoe_tree, hf_pppoes_tags, tvb, tagstart, 8, FALSE);
745                         pppoe_tree = proto_item_add_subtree(ti, ett_pppoes_tags);
746
747                         /* Show tag data */
748                         if (poe_tag_length == 4)
749                         {
750                                 proto_tree_add_item(pppoe_tree, hf_pppoes_tag_credits_fcn, tvb,
751                                         tagstart+4, 2, FALSE);
752                                 proto_tree_add_item(pppoe_tree, hf_pppoes_tag_credits_bcn, tvb,
753                                         tagstart+6, 2, FALSE);
754                         } else {
755                                 proto_tree_add_item(pppoe_tree, hf_pppoed_tag_credits, tvb,
756                                         tagstart+4, poe_tag_length, FALSE);
757                         }
758
759                         credit_offset = 8;
760                 }
761         }
762
763         /*
764          * The only reason why the payload length from the header
765          * should differ from the remaining data in the packet
766          * would be if the total packet length, including Ethernet
767          * CRC, were < 64 bytes, so that padding was required.
768          *
769          * That means that you have 14 bytes of Ethernet header,
770          * 4 bytes of FCS, and fewer than 46 bytes of PPPoE packet.
771          *
772          * If that's not the case, we report a difference between
773          * the payload length in the packet, and the amount of
774          * data following the PPPoE header, as an error.
775          */
776         if (tvb_reported_length(tvb) > 46) {
777                 /*
778                  * Be forgiving about a possible trailing FCS.
779                  *
780                  * XXX - this dissector currently doesn't know
781                  * whether any extra data past the end of the PPP
782                  * payload is an FCS or not.
783                  *
784                  * If we know that we have an FCS, or that we don't
785                  * have an FCS, we should have been handed a tvbuff
786                  * without the FCS, and we should just do the strict
787                  * length check.
788                  *
789                  * If we don't know whether we have an FCS, then:
790                  *
791                  *   if this isn't over Ethernet - the "E" in "PPPoE"
792                  *   nonwithstanding, it can also run on top of 802.11,
793                  *   for example - there's no trailer, so any data
794                  *   past the payload length is either an FCS or
795                  *   bogus;
796                  *
797                  *   if this is over Ethernet, there shouldn't be
798                  *   a trailer, as the packet is long enough not to
799                  *   require a trailer, as per the above;
800                  *
801                  * so perhaps we should assume that if we have exactly
802                  * 4 bytes of extra information, it's an FCS, otherwise
803                  * it's not.
804                  *
805                  * Perhaps we need to have a routine to call to
806                  * do all the length checking, etc., and call it
807                  * from here and from other dissectors where the
808                  * protocol has a length field, or have a way to
809                  * tell the dissector that called us which field
810                  * has the length field and have *that* dissector
811                  * do the length checking and add the expert info
812                  * to the length field, *after* it does all the
813                  * FCS heuristics.
814                  */
815                 if ((reported_payload_length != actual_payload_length) &&
816                 ((reported_payload_length + 4) != actual_payload_length)) {
817                     proto_item_append_text(ti, " [incorrect, should be %u]",
818                         actual_payload_length);
819                     expert_add_info_format(pinfo, ti, PI_MALFORMED,
820                         PI_WARN, "Possible bad payload length %u != %u",
821                         reported_payload_length, actual_payload_length);
822                 }
823         }
824
825         /*
826          * Construct a tvbuff containing the PPP packet.
827          */
828         length = tvb_length_remaining(tvb, 6);
829         reported_length = tvb_reported_length_remaining(tvb, 6);
830         DISSECTOR_ASSERT(length >= 0);
831         DISSECTOR_ASSERT(reported_length >= 0);
832         if (length > reported_length)
833                 length = reported_length;
834         if ((guint)length > reported_payload_length)
835                 length = reported_payload_length;
836         if ((guint)reported_length > reported_payload_length)
837                 reported_length = reported_payload_length;
838         next_tvb = tvb_new_subset(tvb,(6 + credit_offset),
839                                 (length - credit_offset),
840                                 (reported_length - credit_offset));
841         call_dissector(ppp_handle,next_tvb,pinfo,tree);
842 }
843
844 void proto_register_pppoes(void)
845 {
846
847         static hf_register_info hf[] =
848         {
849                 { &hf_pppoes_tags,
850                         { "PPPoE Tags", "pppoes.tags", FT_NONE, BASE_NONE,
851                                  NULL, 0x0, NULL, HFILL
852                         }
853                 },
854                 { &hf_pppoes_tag,
855                         { "Tag", "pppoes.tag", FT_UINT16, BASE_HEX,
856                                  VALS(tag_vals), 0x0, NULL, HFILL
857                         }
858                 },
859                 { &hf_pppoes_tag_credits,
860                         { "Credits", "pppoes.tags.credits", FT_BYTES, BASE_NONE,
861                                  NULL, 0x0, NULL, HFILL
862                         }
863                 },
864                 { &hf_pppoes_tag_credits_fcn,
865                         { "FCN", "pppoes.tags.credits.fcn", FT_UINT16, BASE_DEC,
866                                  NULL, 0x0, NULL, HFILL
867                         }
868                 },
869                 { &hf_pppoes_tag_credits_bcn,
870                         { "BCN", "pppoes.tags.credits.bcn", FT_UINT16, BASE_DEC,
871                                  NULL, 0x0, NULL, HFILL
872                         }
873                 }
874         };
875
876         static gint *ett[] = {
877                 &ett_pppoes,
878                 &ett_pppoes_tags
879         };
880
881         /* Register protocol */
882         proto_pppoes = proto_register_protocol("PPP-over-Ethernet Session", "PPPoES", "pppoes");
883
884         proto_register_subtree_array(ett, array_length(ett));
885         proto_register_field_array(proto_pppoes, hf, array_length(hf));
886 }
887
888 void proto_register_pppoe(void)
889 {
890         static hf_register_info hf[] =
891         {
892                 /* These fields common to discovery and session protocols */
893                 { &hf_pppoe_version,
894                         { "Version", "pppoe.version", FT_UINT8, BASE_DEC,
895                                  NULL, 0xf0, NULL, HFILL
896                         }
897                 },
898                 { &hf_pppoe_type,
899                         { "Type", "pppoe.type", FT_UINT8, BASE_DEC,
900                                  NULL, 0x0f, NULL, HFILL
901                         }
902                 },
903                 { &hf_pppoe_code,
904                         { "Code", "pppoe.code", FT_UINT8, BASE_HEX,
905                                  VALS(code_vals), 0x0, NULL, HFILL
906                         }
907                 },
908                 { &hf_pppoe_session_id,
909                         { "Session ID", "pppoe.session_id", FT_UINT16, BASE_HEX,
910                                  NULL, 0x0, NULL, HFILL
911                         }
912                 },
913                 { &hf_pppoe_payload_length,
914                         { "Payload Length", "pppoe.payload_length", FT_UINT16, BASE_DEC,
915                                  NULL, 0x0, NULL, HFILL
916                         }
917                 }
918         };
919
920         static gint *ett[] = {
921                 &ett_pppoe
922         };
923
924         /* Register protocol */
925         proto_pppoe = proto_register_protocol("PPP-over-Ethernet", "PPPoE", "pppoe");
926
927         proto_register_subtree_array(ett, array_length(ett));
928         proto_register_field_array(proto_pppoe, hf, array_length(hf));
929
930 }
931
932 void proto_reg_handoff_pppoes(void)
933 {
934         dissector_handle_t pppoes_handle  =
935             create_dissector_handle(dissect_pppoes, proto_pppoes);
936         dissector_add("ethertype", ETHERTYPE_PPPOES, pppoes_handle);
937
938         /* Get a handle for the PPP dissector */
939         ppp_handle = find_dissector("ppp");
940 }