aboutsummaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-pptp.c
blob: be6b0fe9466b89b9a98950b4294a5847830eaab1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
/* packet-pptp.c
 * Routines for the Point-to-Point Tunnelling Protocol (PPTP) (RFC 2637)
 * Brad Robel-Forrest <brad.robel-forrest@watchguard.com>
 *
 * 10/2010 - Rework PPTP Dissector
 * Alexis La Goutte <alexis.lagoutte at gmail dot com>
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "config.h"

#include <epan/packet.h>
#include <epan/expert.h>

void proto_register_pptp(void);
void proto_reg_handoff_pptp(void);

static int proto_pptp = -1;
static int hf_pptp_length = -1;
static int hf_pptp_message_type = -1;
static int hf_pptp_magic_cookie = -1;
static int hf_pptp_control_message_type = -1;
static int hf_pptp_reserved = -1;
static int hf_pptp_protocol_version = -1;
static int hf_pptp_framing_capabilities = -1;
static int hf_pptp_bearer_capabilities = -1;
static int hf_pptp_maximum_channels = -1;
static int hf_pptp_firmware_revision = -1;
static int hf_pptp_host_name = -1;
static int hf_pptp_vendor_name = -1;
static int hf_pptp_control_result = -1;
static int hf_pptp_error = -1;
static int hf_pptp_reason = -1;
static int hf_pptp_stop_result = -1;
static int hf_pptp_identifier = -1;
static int hf_pptp_echo_result = -1;
static int hf_pptp_call_id = -1;
static int hf_pptp_call_serial_number = -1;
static int hf_pptp_minimum_bps = -1;
static int hf_pptp_maximum_bps = -1;
static int hf_pptp_bearer_type = -1;
static int hf_pptp_framing_type = -1;
static int hf_pptp_packet_receive_window_size = -1;
static int hf_pptp_packet_processing_delay = -1;
static int hf_pptp_phone_number_length = -1;
static int hf_pptp_phone_number = -1;
static int hf_pptp_subaddress = -1;
static int hf_pptp_peer_call_id = -1;
static int hf_pptp_out_result = -1;
static int hf_pptp_cause = -1;
static int hf_pptp_connect_speed = -1;
static int hf_pptp_physical_channel_id = -1;
static int hf_pptp_dialed_number_length = -1;
static int hf_pptp_dialed_number = -1;
static int hf_pptp_dialing_number_length = -1;
static int hf_pptp_dialing_number = -1;
static int hf_pptp_in_result = -1;
static int hf_pptp_disc_result = -1;
static int hf_pptp_call_statistics = -1;
static int hf_pptp_crc_errors = -1;
static int hf_pptp_framing_errors = -1;
static int hf_pptp_hardware_overruns = -1;
static int hf_pptp_buffer_overruns = -1;
static int hf_pptp_timeout_errors = -1;
static int hf_pptp_alignment_errors = -1;
static int hf_pptp_send_accm = -1;
static int hf_pptp_receive_accm = -1;

static gint ett_pptp = -1;

static expert_field ei_pptp_incorrect_magic_cookie = EI_INIT;

#define TCP_PORT_PPTP           1723

#define MAGIC_COOKIE            0x1A2B3C4D

#define CNTRL_REQ       0x01
#define CNTRL_REPLY     0x02
#define STOP_REQ        0x03
#define STOP_REPLY      0x04
#define ECHO_REQ        0x05
#define ECHO_REPLY      0x06
#define OUT_REQ         0x07
#define OUT_REPLY       0x08
#define IN_REQ          0x09
#define IN_REPLY        0x0A
#define IN_CONNECTED    0x0B
#define CLEAR_REQ       0x0C
#define DISC_NOTIFY     0x0D
#define ERROR_NOTIFY    0x0E
#define SET_LINK        0x0F

static const value_string control_message_type_vals[] = {
  { CNTRL_REQ,    "Start-Control-Connection-Request" },
  { CNTRL_REPLY,  "Start-Control-Connection-Reply" },
  { STOP_REQ,     "Stop-Control-Connection-Request" },
  { STOP_REPLY,   "Stop-Control-Connection-Reply" },
  { ECHO_REQ,     "Echo-Request" },
  { ECHO_REPLY,   "Echo-Reply" },
  { OUT_REQ,      "Outgoing-Call-Request" },
  { OUT_REPLY,    "Outgoing-Call-Reply" },
  { IN_REQ,       "Incoming-Call-Request" },
  { IN_REPLY,     "Incoming-Call-Reply" },
  { IN_CONNECTED, "Incoming-Call-Connected" },
  { CLEAR_REQ,    "Call-Clear-Request" },
  { DISC_NOTIFY,  "Call-Disconnect-Notify" },
  { ERROR_NOTIFY, "WAN-Error-Notify" },
  { SET_LINK,     "Set-Link-Info" },
  { 0,  NULL },
};
static const value_string msgtype_vals[] = {
  { 1, "Control Message" },
  { 2, "Management Message" },
  { 0, NULL }
};

static const value_string frametype_vals[] = {
  { 1, "Asynchronous Framing supported" },
  { 2, "Synchronous Framing supported"},
  { 3, "Either Framing supported" },
  { 0, NULL }
};

static const value_string bearertype_vals[] = {
  { 1, "Analog access supported" },
  { 2, "Digital access supported" },
  { 3, "Either access supported" },
  { 0, NULL }
};

static const value_string control_resulttype_vals[] = {
  { 1, "Successful channel establishment" },
  { 2, "General error" },
  { 3, "Command channel already exists" },
  { 4, "Requester not authorized" },
  { 5, "Protocol version not supported" },
  { 0, NULL }
};

static const value_string errortype_vals[] = {
  { 0, "None" },
  { 1, "Not-Connected" },
  { 2, "Bad-Format" },
  { 3, "Bad-Value" },
  { 4, "No-Resource" },
  { 5, "Bad-Call ID" },
  { 6, "PAC-Error" },
  { 0, NULL }
};

static const value_string reasontype_vals[] = {
  { 1, "None" },
  { 2, "Stop-Protocol" },
  { 3, "Stop-Local-Shutdown" },
  { 0, NULL }
};

static const value_string stop_resulttype_vals[] = {
  { 1, "OK" },
  { 2, "General error" },
  { 0, NULL }
};

static const value_string echo_resulttype_vals[] = {
  { 1, "OK" },
  { 2, "General error" },
  { 0, NULL }
};

static const value_string out_resulttype_vals[] = {
  { 1, "Connected" },
  { 2, "General Error" },
  { 3, "No Carrier" },
  { 4, "Busy" },
  { 5, "No Dial Tone" },
  { 6, "Time-out" },
  { 7, "Do Not Accept" },
  { 0, NULL }
};

static const value_string in_resulttype_vals[] = {
  { 1, "Connect" },
  { 2, "General error" },
  { 3, "Do Not Accept" },
  { 0, NULL }
};

static const value_string disc_resulttype_vals[] = {
  { 1, "Lost Carrier" },
  { 2, "General Error" },
  { 3, "Admin Shutdown" },
  { 4, "Request" },
  { 0, NULL }
};

static void
dissect_unknown(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree)
{
  call_data_dissector(tvb_new_subset_remaining(tvb, offset), pinfo, tree);
}

static void
dissect_cntrl_req(tvbuff_t *tvb, int offset, packet_info *pinfo _U_, proto_tree *tree)
{
  if (!tree)
    return;

  proto_tree_add_uint_format_value(tree, hf_pptp_protocol_version, tvb, offset,
                               2, tvb_get_ntohs(tvb, offset), "%u.%u",
                               tvb_get_guint8(tvb, offset), tvb_get_guint8(tvb, offset + 1));
  offset += 2;

  proto_tree_add_item(tree, hf_pptp_reserved,             tvb, offset, 2, ENC_NA);
  offset += 2;

  proto_tree_add_item(tree, hf_pptp_framing_capabilities, tvb, offset, 4, ENC_BIG_ENDIAN);
  offset += 4;

  proto_tree_add_item(tree, hf_pptp_bearer_capabilities,  tvb, offset, 4, ENC_BIG_ENDIAN);
  offset += 4;

  proto_tree_add_item(tree, hf_pptp_maximum_channels,     tvb, offset, 2, ENC_BIG_ENDIAN);
  offset += 2;

  proto_tree_add_item(tree, hf_pptp_firmware_revision,    tvb, offset, 2, ENC_BIG_ENDIAN);
  offset += 2;

  proto_tree_add_item(tree, hf_pptp_host_name,            tvb, offset, 64, ENC_ASCII|ENC_NA);
  offset += 64;

  proto_tree_add_item(tree, hf_pptp_vendor_name,          tvb, offset, 64, ENC_ASCII|ENC_NA);
}

static void
dissect_cntrl_reply(tvbuff_t *tvb, int offset, packet_info *pinfo _U_, proto_tree *tree)
{
  if (!tree)
    return;

  proto_tree_add_uint_format_value(tree, hf_pptp_protocol_version, tvb, offset,
                               2, tvb_get_ntohs(tvb, offset), "%u.%u",
                               tvb_get_guint8(tvb, offset), tvb_get_guint8(tvb, offset + 1));
  offset += 2;

  proto_tree_add_item(tree, hf_pptp_control_result,       tvb, offset, 1, ENC_BIG_ENDIAN);
  offset += 1;

  proto_tree_add_item(tree, hf_pptp_error,                tvb, offset, 1, ENC_BIG_ENDIAN);
  offset += 1;

  proto_tree_add_item(tree, hf_pptp_framing_capabilities, tvb, offset, 4, ENC_BIG_ENDIAN);
  offset += 4;

  proto_tree_add_item(tree, hf_pptp_bearer_capabilities,  tvb, offset, 4, ENC_BIG_ENDIAN);
  offset += 4;

  proto_tree_add_item(tree, hf_pptp_maximum_channels,     tvb, offset, 2, ENC_BIG_ENDIAN);
  offset += 2;

  proto_tree_add_item(tree, hf_pptp_firmware_revision,    tvb, offset, 2, ENC_BIG_ENDIAN);
  offset += 2;

  proto_tree_add_item(tree, hf_pptp_host_name,            tvb, offset, 64, ENC_ASCII|ENC_NA);
  offset += 64;

  proto_tree_add_item(tree, hf_pptp_vendor_name,          tvb, offset, 64, ENC_ASCII|ENC_NA);

}

static void
dissect_stop_req(tvbuff_t *tvb, int offset, packet_info *pinfo _U_, proto_tree *tree)
{
  if (!tree)
    return;

  proto_tree_add_item(tree, hf_pptp_reason,   tvb, offset, 1, ENC_BIG_ENDIAN);
  offset += 1;

  proto_tree_add_item(tree, hf_pptp_reserved, tvb, offset, 1, ENC_NA);
  offset += 1;

  proto_tree_add_item(tree, hf_pptp_reserved, tvb, offset, 2, ENC_NA);
}

static void
dissect_stop_reply(tvbuff_t *tvb, int offset, packet_info *pinfo _U_, proto_tree *tree)
{
  if (!tree)
    return;

  proto_tree_add_item(tree, hf_pptp_stop_result, tvb, offset, 1, ENC_BIG_ENDIAN);
  offset += 1;

  proto_tree_add_item(tree, hf_pptp_error,       tvb, offset, 1, ENC_BIG_ENDIAN);
  offset += 1;

  proto_tree_add_item(tree, hf_pptp_reserved,    tvb, offset, 2, ENC_NA);

}

static void
dissect_echo_req(tvbuff_t *tvb, int offset, packet_info *pinfo _U_, proto_tree *tree)
{
  if (!tree)
    return;

  proto_tree_add_item(tree, hf_pptp_identifier, tvb, offset, 4, ENC_BIG_ENDIAN);
}

static void
dissect_echo_reply(tvbuff_t *tvb, int offset, packet_info *pinfo _U_, proto_tree *tree)
{
  if (!tree)
    return;

  proto_tree_add_item(tree, hf_pptp_identifier,  tvb, offset, 4, ENC_BIG_ENDIAN);
  offset += 4;

  proto_tree_add_item(tree, hf_pptp_echo_result, tvb, offset, 1, ENC_BIG_ENDIAN);
  offset += 1;

  proto_tree_add_item(tree, hf_pptp_error,       tvb, offset, 1, ENC_BIG_ENDIAN);
  offset += 1;

  proto_tree_add_item(tree, hf_pptp_reserved,    tvb, offset, 2, ENC_NA);
}

static void
dissect_out_req(tvbuff_t *tvb, int offset, packet_info *pinfo _U_, proto_tree *tree)
{
  if (!tree)
    return;

  proto_tree_add_item(tree, hf_pptp_call_id,                    tvb, offset, 2, ENC_BIG_ENDIAN);
  offset += 2;

  proto_tree_add_item(tree, hf_pptp_call_serial_number,         tvb, offset, 2, ENC_BIG_ENDIAN);
  offset += 2;

  proto_tree_add_item(tree, hf_pptp_minimum_bps,                tvb, offset, 4, ENC_BIG_ENDIAN);
  offset += 4;

  proto_tree_add_item(tree, hf_pptp_maximum_bps,                tvb, offset, 4, ENC_BIG_ENDIAN);
  offset += 4;

  proto_tree_add_item(tree, hf_pptp_bearer_type,                tvb, offset, 4, ENC_BIG_ENDIAN);
  offset += 4;

  proto_tree_add_item(tree, hf_pptp_framing_type,               tvb, offset, 4, ENC_BIG_ENDIAN);
  offset += 4;

  proto_tree_add_item(tree, hf_pptp_packet_receive_window_size, tvb, offset, 2, ENC_BIG_ENDIAN);
  offset += 2;

  proto_tree_add_item(tree, hf_pptp_packet_processing_delay,    tvb, offset, 2, ENC_BIG_ENDIAN);
  offset += 2;

  proto_tree_add_item(tree, hf_pptp_phone_number_length,        tvb, offset, 2, ENC_BIG_ENDIAN);
  offset += 2;

  proto_tree_add_item(tree, hf_pptp_reserved,                   tvb, offset, 2, ENC_NA);
  offset += 2;

  proto_tree_add_item(tree, hf_pptp_phone_number,               tvb, offset, 64, ENC_ASCII|ENC_NA);
  offset += 64;

  proto_tree_add_item(tree, hf_pptp_subaddress,                 tvb, offset, 64, ENC_ASCII|ENC_NA);
}

static void
dissect_out_reply(tvbuff_t *tvb, int offset, packet_info *pinfo _U_, proto_tree *tree)
{
  if (!tree)
    return;

  proto_tree_add_item(tree, hf_pptp_call_id,                    tvb, offset, 2, ENC_BIG_ENDIAN);
  offset += 2;

  proto_tree_add_item(tree, hf_pptp_peer_call_id,               tvb, offset, 2, ENC_BIG_ENDIAN);
  offset += 2;

  proto_tree_add_item(tree, hf_pptp_out_result,                 tvb, offset, 1, ENC_BIG_ENDIAN);
  offset += 1;

  proto_tree_add_item(tree, hf_pptp_error,                      tvb, offset, 1, ENC_BIG_ENDIAN);
  offset += 1;

  proto_tree_add_item(tree, hf_pptp_cause,                      tvb, offset, 2, ENC_BIG_ENDIAN);
  offset += 2;

  proto_tree_add_item(tree, hf_pptp_connect_speed,              tvb, offset, 4, ENC_BIG_ENDIAN);
  offset += 4;

  proto_tree_add_item(tree, hf_pptp_packet_receive_window_size, tvb, offset, 2, ENC_BIG_ENDIAN);
  offset += 2;

  proto_tree_add_item(tree, hf_pptp_packet_processing_delay,    tvb, offset, 2, ENC_BIG_ENDIAN);
  offset += 2;

  proto_tree_add_item(tree, hf_pptp_physical_channel_id,        tvb, offset, 4, ENC_BIG_ENDIAN);
}

static void
dissect_in_req(tvbuff_t *tvb, int offset, packet_info *pinfo _U_, proto_tree *tree)
{
  if (!tree)
    return;

  proto_tree_add_item(tree, hf_pptp_call_id,               tvb, offset, 2, ENC_BIG_ENDIAN);
  offset += 2;

  proto_tree_add_item(tree, hf_pptp_call_serial_number,    tvb, offset, 2, ENC_BIG_ENDIAN);
  offset += 2;

  proto_tree_add_item(tree, hf_pptp_bearer_type,           tvb, offset, 4, ENC_BIG_ENDIAN);
  offset += 4;

  proto_tree_add_item(tree, hf_pptp_physical_channel_id,   tvb, offset, 4, ENC_BIG_ENDIAN);
  offset += 4;

  proto_tree_add_item(tree, hf_pptp_dialed_number_length,  tvb, offset, 2, ENC_BIG_ENDIAN);
  offset += 2;

  proto_tree_add_item(tree, hf_pptp_dialing_number_length, tvb, offset, 2, ENC_BIG_ENDIAN);
  offset += 2;

  proto_tree_add_item(tree, hf_pptp_dialed_number,         tvb, offset, 64, ENC_ASCII|ENC_NA);
  offset += 64;

  proto_tree_add_item(tree, hf_pptp_dialing_number,        tvb, offset, 64, ENC_ASCII|ENC_NA);
  offset += 64;

  proto_tree_add_item(tree, hf_pptp_subaddress,            tvb, offset, 64, ENC_ASCII|ENC_NA);
}

static void
dissect_in_reply(tvbuff_t *tvb, int offset, packet_info *pinfo _U_, proto_tree *tree)
{
  if (!tree)
    return;

  proto_tree_add_item(tree, hf_pptp_call_id,                    tvb, offset, 2, ENC_BIG_ENDIAN);
  offset += 2;

  proto_tree_add_item(tree, hf_pptp_peer_call_id,               tvb, offset, 2, ENC_BIG_ENDIAN);
  offset += 2;

  proto_tree_add_item(tree, hf_pptp_in_result,                  tvb, offset, 1, ENC_BIG_ENDIAN);
  offset += 1;

  proto_tree_add_item(tree, hf_pptp_error,                      tvb, offset, 1, ENC_BIG_ENDIAN);
  offset += 1;

  proto_tree_add_item(tree, hf_pptp_packet_receive_window_size, tvb, offset, 2, ENC_BIG_ENDIAN);
  offset += 2;

  proto_tree_add_item(tree, hf_pptp_packet_processing_delay,    tvb, offset, 2, ENC_BIG_ENDIAN);
  offset += 2;

  proto_tree_add_item(tree, hf_pptp_reserved,                   tvb, offset, 2, ENC_NA);
}

static void
dissect_in_connected(tvbuff_t *tvb, int offset, packet_info *pinfo _U_, proto_tree *tree)
{
  if (!tree)
    return;

  proto_tree_add_item(tree, hf_pptp_peer_call_id,               tvb, offset, 2, ENC_BIG_ENDIAN);
  offset += 2;

  proto_tree_add_item(tree, hf_pptp_reserved,                   tvb, offset, 2, ENC_NA);
  offset += 2;

  proto_tree_add_item(tree, hf_pptp_connect_speed,              tvb, offset, 4, ENC_BIG_ENDIAN);
  offset += 4;

  proto_tree_add_item(tree, hf_pptp_packet_receive_window_size, tvb, offset, 2, ENC_BIG_ENDIAN);
  offset += 2;

  proto_tree_add_item(tree, hf_pptp_packet_processing_delay,    tvb, offset, 2, ENC_BIG_ENDIAN);
  offset += 2;

  proto_tree_add_item(tree, hf_pptp_framing_type,               tvb, offset, 4, ENC_BIG_ENDIAN);
}

static void
dissect_clear_req(tvbuff_t *tvb, int offset, packet_info *pinfo _U_, proto_tree *tree)
{
  if (!tree)
    return;

  proto_tree_add_item(tree, hf_pptp_call_id,  tvb, offset, 2, ENC_BIG_ENDIAN);
  offset += 2;

  proto_tree_add_item(tree, hf_pptp_reserved, tvb, offset, 2, ENC_NA);
}

static void
dissect_disc_notify(tvbuff_t *tvb, int offset, packet_info *pinfo _U_, proto_tree *tree)
{
  if (!tree)
    return;

  proto_tree_add_item(tree, hf_pptp_call_id,         tvb, offset, 2, ENC_BIG_ENDIAN);
  offset += 2;

  proto_tree_add_item(tree, hf_pptp_disc_result,     tvb, offset, 1, ENC_BIG_ENDIAN);
  offset += 1;

  proto_tree_add_item(tree, hf_pptp_error,           tvb, offset, 1, ENC_BIG_ENDIAN);
  offset += 1;

  proto_tree_add_item(tree, hf_pptp_cause,           tvb, offset, 2, ENC_BIG_ENDIAN);
  offset += 2;

  proto_tree_add_item(tree, hf_pptp_reserved,        tvb, offset, 2, ENC_NA);
  offset += 2;

  proto_tree_add_item(tree, hf_pptp_call_statistics, tvb, offset, 64, ENC_ASCII|ENC_NA);
}

static void
dissect_error_notify(tvbuff_t *tvb, int offset, packet_info *pinfo _U_, proto_tree *tree)
{
  if (!tree)
    return;

  proto_tree_add_item(tree, hf_pptp_peer_call_id,      tvb, offset, 2, ENC_BIG_ENDIAN);
  offset += 2;

  proto_tree_add_item(tree, hf_pptp_reserved,          tvb, offset, 2, ENC_NA);
  offset += 2;

  proto_tree_add_item(tree, hf_pptp_crc_errors,        tvb, offset, 4, ENC_BIG_ENDIAN);
  offset += 4;

  proto_tree_add_item(tree, hf_pptp_framing_errors,    tvb, offset, 4, ENC_BIG_ENDIAN);
  offset += 4;

  proto_tree_add_item(tree, hf_pptp_hardware_overruns, tvb, offset, 4, ENC_BIG_ENDIAN);
  offset += 4;

  proto_tree_add_item(tree, hf_pptp_buffer_overruns,   tvb, offset, 4, ENC_BIG_ENDIAN);
  offset += 4;

  proto_tree_add_item(tree, hf_pptp_timeout_errors,    tvb, offset, 4, ENC_BIG_ENDIAN);
  offset += 4;

  proto_tree_add_item(tree, hf_pptp_alignment_errors,  tvb, offset, 4, ENC_BIG_ENDIAN);
}

static void
dissect_set_link(tvbuff_t *tvb, int offset, packet_info *pinfo _U_, proto_tree *tree)
{
  if (!tree)
      return;

  proto_tree_add_item(tree, hf_pptp_peer_call_id, tvb, offset, 2, ENC_BIG_ENDIAN);
  offset += 2;

  proto_tree_add_item(tree, hf_pptp_reserved,     tvb, offset, 2, ENC_NA);
  offset += 2;

  proto_tree_add_item(tree, hf_pptp_send_accm,    tvb, offset, 4, ENC_BIG_ENDIAN);
  offset += 4;

  proto_tree_add_item(tree, hf_pptp_receive_accm, tvb, offset, 4, ENC_BIG_ENDIAN);
}

static int
dissect_pptp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
{
  proto_tree *pptp_tree = NULL;
  proto_item *item      = NULL;
  int         offset    = 0;
  guint16     len;
  guint16     control_message_type;

  col_set_str(pinfo->cinfo, COL_PROTOCOL, "PPTP");
  col_clear(pinfo->cinfo, COL_INFO);

  len        = tvb_get_ntohs(tvb, offset);
  control_message_type = tvb_get_ntohs(tvb, offset + 8);

  col_add_str(pinfo->cinfo, COL_INFO,
              val_to_str(control_message_type, control_message_type_vals,
                         "Unknown control type (%d)"));

  if (tree) {
    proto_item *ti;

    ti = proto_tree_add_item(tree, proto_pptp, tvb, offset, len, ENC_NA);
    pptp_tree = proto_item_add_subtree(ti, ett_pptp);

    proto_tree_add_item(pptp_tree, hf_pptp_length, tvb, offset, 2, ENC_BIG_ENDIAN);

    proto_tree_add_item(pptp_tree, hf_pptp_message_type, tvb, offset+2, 2, ENC_BIG_ENDIAN);

    item = proto_tree_add_item(pptp_tree, hf_pptp_magic_cookie, tvb, offset+4, 4, ENC_BIG_ENDIAN);
  }

  if (tvb_get_ntohl(tvb, offset+4) == MAGIC_COOKIE)
    proto_item_append_text(item," (correct)");
  else {
    proto_item_append_text(item," (incorrect)");
    expert_add_info(pinfo, item, &ei_pptp_incorrect_magic_cookie);
  }

  if (tree) {
    proto_tree_add_item(pptp_tree, hf_pptp_control_message_type, tvb, offset+8, 2, ENC_BIG_ENDIAN);

    proto_tree_add_item(pptp_tree, hf_pptp_reserved, tvb, offset+10, 2, ENC_NA);
  }

  offset += offset + 12;

  switch(control_message_type){
    case CNTRL_REQ: /* Start-Control-Connection-Request */
      dissect_cntrl_req(tvb, offset, pinfo, pptp_tree);
      break;
    case CNTRL_REPLY: /* Start-Control-Connection-Reply */
      dissect_cntrl_reply(tvb, offset, pinfo, pptp_tree);
      break;
    case STOP_REQ: /* Stop-Control-Connection-Request */
      dissect_stop_req(tvb, offset, pinfo, pptp_tree);
      break;
    case STOP_REPLY: /* Stop-Control-Connection-Reply */
      dissect_stop_reply(tvb, offset, pinfo, pptp_tree);
      break;
    case ECHO_REQ: /* Echo-Request */
      dissect_echo_req(tvb, offset, pinfo, pptp_tree);
      break;
    case ECHO_REPLY: /* Echo-Reply */
      dissect_echo_reply(tvb, offset, pinfo, pptp_tree);
      break;
    case OUT_REQ: /* Outgoing-Call-Request */
      dissect_out_req(tvb, offset, pinfo, pptp_tree);
      break;
    case OUT_REPLY: /* Outgoing-Call-Reply */
      dissect_out_reply(tvb, offset, pinfo, pptp_tree);
      break;
    case IN_REQ: /* Incoming-Call-Request */
      dissect_in_req(tvb, offset, pinfo, pptp_tree);
      break;
    case IN_REPLY: /* Incoming-Call-Reply */
      dissect_in_reply(tvb, offset, pinfo, pptp_tree);
      break;
    case IN_CONNECTED: /* Incoming-Call-Connected */
      dissect_in_connected(tvb, offset, pinfo, pptp_tree);
      break;
    case CLEAR_REQ: /* Call-Clear-Request */
      dissect_clear_req(tvb, offset, pinfo, pptp_tree);
      break;
    case DISC_NOTIFY: /* Call-Disconnect-Notify */
      dissect_disc_notify(tvb, offset, pinfo, pptp_tree);
      break;
    case ERROR_NOTIFY: /* WAN-Error-Notify */
      dissect_error_notify(tvb, offset, pinfo, pptp_tree);
      break;
    case SET_LINK: /* Set-Link-Info */
      dissect_set_link(tvb, offset, pinfo, pptp_tree);
      break;
    default: /* Unknown Type... */
      dissect_unknown(tvb, offset, pinfo, pptp_tree);
      break;
  }
  return tvb_captured_length(tvb);
}

void
proto_register_pptp(void)
{
  static gint *ett[] = {
    &ett_pptp,
  };

  static hf_register_info hf[] = {
    { &hf_pptp_length,
      { "Length", "pptp.length",
        FT_UINT16, BASE_DEC, NULL, 0x0,
        "Total length in octets of this PPTP message", HFILL }
    },
    { &hf_pptp_message_type,
      { "Message type", "pptp.type",
        FT_UINT16, BASE_DEC, VALS(msgtype_vals), 0x0,
        "PPTP message type", HFILL }
    },
    { &hf_pptp_magic_cookie,
      { "Magic Cookie", "pptp.magic_cookie",
        FT_UINT32, BASE_HEX, NULL, 0x0,
        "This constant value is used as a sanity check on received messages", HFILL }
    },
    { &hf_pptp_control_message_type,
      { "Control Message Type", "pptp.control_message_type",
        FT_UINT16, BASE_DEC, VALS(control_message_type_vals), 0x0,
        NULL, HFILL }
    },
    { &hf_pptp_reserved,
      { "Reserved", "pptp.reserved",
        FT_BYTES, BASE_NONE, NULL, 0x0,
        "This field MUST be 0", HFILL }
    },
    { &hf_pptp_protocol_version,
      { "Protocol version", "pptp.protocol_version",
        FT_UINT16, BASE_DEC, NULL, 0x0,
        "The version of the PPTP protocol", HFILL }
    },
    { &hf_pptp_framing_capabilities,
      { "Framing Capabilities", "pptp.framing_capabilities",
        FT_UINT32, BASE_DEC, VALS(frametype_vals), 0x0,
        "A set of bits indicating the type of framing", HFILL }
    },
    { &hf_pptp_bearer_capabilities,
      { "Bearer Capabilities", "pptp.bearer_capabilities",
        FT_UINT32, BASE_DEC, VALS(bearertype_vals), 0x0,
        "A set of bits indicating the type of bearer", HFILL }
    },
    { &hf_pptp_maximum_channels,
      { "Maximum Channels", "pptp.maximum_channels",
        FT_UINT16, BASE_DEC, NULL, 0x0,
        "The total number of individual PPP sessions this PAC can support", HFILL }
    },
    { &hf_pptp_firmware_revision,
      { "Firmware Revision", "pptp.firmware_revision",
        FT_UINT16, BASE_DEC, NULL, 0x0,
        "This field contains the firmware revision", HFILL }
    },
    { &hf_pptp_host_name,
      { "Host Name", "pptp.host_name",
        FT_STRING, BASE_NONE, NULL, 0x0,
        "A 64 octet field containing the DNS name", HFILL }
    },
    { &hf_pptp_vendor_name,
      { "Vendor Name", "pptp.vendor_name",
        FT_STRING, BASE_NONE, NULL, 0x0,
        "A 64 octet field containing a vendor", HFILL }
    },
    { &hf_pptp_control_result,
      { "Result Code", "pptp.control_result",
        FT_UINT8, BASE_DEC, VALS(control_resulttype_vals), 0x0,
        "Indicates the result of the command channel establishment attempt", HFILL }
    },
    { &hf_pptp_error,
      { "Error Code", "pptp.error",
        FT_UINT8, BASE_DEC, VALS(errortype_vals), 0x0,
        NULL, HFILL }
    },
    { &hf_pptp_reason,
      { "Reason", "pptp.reason",
        FT_UINT8, BASE_DEC, VALS(reasontype_vals), 0x0,
        "Indicates the reason for the control connection being close", HFILL }
    },
    { &hf_pptp_stop_result,
      { "Result Code", "pptp.stop_result",
        FT_UINT8, BASE_DEC, VALS(stop_resulttype_vals), 0x0,
        "Indicates the result of the attempt to close the control connection", HFILL }
    },
    { &hf_pptp_identifier,
      { "Identifier", "pptp.identifier",
        FT_UINT32, BASE_DEC, NULL, 0x0,
        NULL, HFILL }
    },
    { &hf_pptp_echo_result,
      { "Result Code", "pptp.echo_result",
        FT_UINT8, BASE_DEC, VALS(echo_resulttype_vals), 0x0,
        "Indicates the result of the receipt of the Echo-Request", HFILL }
    },
    { &hf_pptp_call_id,
      { "Call ID", "pptp.call_id",
        FT_UINT16, BASE_DEC, NULL, 0x0,
        "A unique identifier, unique to a particular PAC-PNS pair assigned by the PNS", HFILL }
    },
    { &hf_pptp_call_serial_number,
      { "Call Serial Number", "pptp.call_serial_number",
        FT_UINT16, BASE_DEC, NULL, 0x0,
        "An identifier assigned by the PNS to this session for the purpose of identifying this particular session in logged session information", HFILL }
    },
   { &hf_pptp_minimum_bps,
     { "Minimum BPS", "pptp.minimum_bps",
       FT_UINT32, BASE_DEC, NULL, 0x0,
       "The lowest acceptable line speed (in bits/second) for this session", HFILL }
   },
   { &hf_pptp_maximum_bps,
     { "Maximum BPS", "pptp.maximum_bps",
       FT_UINT32, BASE_DEC, NULL, 0x0,
       "The highest acceptable line speed (in bits/second) for this session", HFILL }
   },
    { &hf_pptp_framing_type,
      { "Framing Type", "pptp.framing_type",
        FT_UINT32, BASE_DEC, VALS(frametype_vals), 0x0,
        "A value indicating the type of PPP framing to be used for this outgoing call", HFILL }
    },
    { &hf_pptp_bearer_type,
      { "Bearer Type", "pptp.bearer_type",
        FT_UINT32, BASE_DEC, VALS(bearertype_vals), 0x0,
        "A value indicating the bearer capability required for this outgoing call", HFILL }
    },
    { &hf_pptp_packet_receive_window_size,
      { "Packet Receive Window Size", "pptp.packet_receive_window_size",
        FT_UINT16, BASE_DEC, NULL, 0x0,
        "A unique identifier, unique to a particular PAC-PNS pair assigned by the PNS", HFILL }
    },
    { &hf_pptp_packet_processing_delay,
      { "Packet Processing Delay", "pptp.packet_processing_delay",
        FT_UINT16, BASE_DEC, NULL, 0x0,
        "A measure of the packet processing delay that might be imposed on data sent to the PNS from the PAC", HFILL }
    },
    { &hf_pptp_phone_number_length,
      { "Phone Number Length", "pptp.phone_number_length",
        FT_UINT16, BASE_DEC, NULL, 0x0,
        "The actual number of valid digits in the Phone Number field", HFILL }
    },
    { &hf_pptp_phone_number,
      { "Phone Number", "pptp.phone_number",
        FT_STRING, BASE_NONE, NULL, 0x0,
        "The number to be dialed to establish the outgoing session", HFILL }
    },
    { &hf_pptp_subaddress,
      { "Subaddress", "pptp.subaddress",
        FT_STRING, BASE_NONE, NULL, 0x0,
        "A 64 octet field used to specify additional dialing information.", HFILL }
    },
    { &hf_pptp_peer_call_id,
      { "Peer Call ID", "pptp.peer_call_id",
        FT_UINT16, BASE_DEC, NULL, 0x0,
        "This field is set to the value received in the Call ID field of the corresponding Outgoing-Call-Request message", HFILL }
    },
    { &hf_pptp_out_result,
      { "Result Code", "pptp.out_result",
        FT_UINT8, BASE_DEC, VALS(out_resulttype_vals), 0x0,
        "Indicates the result of the receipt of the Outgoing-Call-Request attempt", HFILL }
    },
    { &hf_pptp_cause,
      { "Cause Code", "pptp.cause",
        FT_UINT16, BASE_DEC, NULL, 0x0,
        "This field gives additional information", HFILL }
    },
    { &hf_pptp_connect_speed,
      { "Connect Speed", "pptp.connect_speed",
        FT_UINT32, BASE_DEC, NULL, 0x0,
        "The actual connection speed used, in bits/second.", HFILL }
    },
    { &hf_pptp_physical_channel_id,
      { "Physical Channel ID", "pptp.physical_channel_id",
        FT_UINT32, BASE_DEC, NULL, 0x0,
        "This field is set by the PAC in a vendor-specific manner to the physical channel number used to place this call", HFILL }
    },
    { &hf_pptp_dialed_number_length,
      { "Dialed Number Length", "pptp.dialed_number_length",
        FT_UINT16, BASE_DEC, NULL, 0x0,
        "The actual number of valid digits in the Dialed Number field", HFILL }
    },
    { &hf_pptp_dialed_number,
      { "Dialed Number", "pptp.dialed_number",
        FT_STRING, BASE_NONE, NULL, 0x0,
        "The number that was dialed by the caller", HFILL }
    },

    { &hf_pptp_dialing_number_length,
      { "Dialing Number Length", "pptp.dialing_number_length",
        FT_UINT16, BASE_DEC, NULL, 0x0,
        "The actual number of valid digits in the Dialing Number field", HFILL }
    },
    { &hf_pptp_dialing_number,
      { "Dialing Number", "pptp.dialing_number",
        FT_STRING, BASE_NONE, NULL, 0x0,
        "The number from which the call was placed", HFILL }
    },
    { &hf_pptp_in_result,
      { "Result Code", "pptp.in_result",
        FT_UINT8, BASE_DEC, VALS(in_resulttype_vals), 0x0,
        "This value indicates the result of the Incoming-Call-Request attempt", HFILL }
    },
    { &hf_pptp_disc_result,
      { "Result Code", "pptp.disc_result",
        FT_UINT8, BASE_DEC, VALS(disc_resulttype_vals), 0x0,
        "This value indicates the reason for the disconnect", HFILL }
    },
    { &hf_pptp_call_statistics,
      { "Call Statistics", "pptp.call_Statistics",
        FT_STRING, BASE_NONE, NULL, 0x0,
        "This field is an ASCII string containing vendor-specific call statistics that can be logged for diagnostic purpose", HFILL }
    },
    { &hf_pptp_crc_errors,
      { "CRC Errors", "pptp.crc_errors",
        FT_UINT32, BASE_DEC, NULL, 0x0,
        "Number of PPP frames received with CRC errors since session was established", HFILL }
    },
    { &hf_pptp_framing_errors,
      { "Framing Errors", "pptp.framing_errors",
        FT_UINT32, BASE_DEC, NULL, 0x0,
        "Number of improperly framed PPP packets received", HFILL }
    },
    { &hf_pptp_hardware_overruns,
      { "Hardware overruns", "pptp.hardware_overruns",
        FT_UINT32, BASE_DEC, NULL, 0x0,
        "Number of receive buffer over-runs since session was established", HFILL }
    },
    { &hf_pptp_buffer_overruns,
      { "Buffer overruns", "pptp.buffer_overruns",
        FT_UINT32, BASE_DEC, NULL, 0x0,
        "Number of buffer over-runs detected since session was established", HFILL }
    },
    { &hf_pptp_timeout_errors,
      { "Time-out Errors", "pptp.timeout_errors",
        FT_UINT32, BASE_DEC, NULL, 0x0,
        "Number of time-outs since call was established", HFILL }
    },
    { &hf_pptp_alignment_errors,
      { "Alignment Errors", "pptp.alignment_errors",
        FT_UINT32, BASE_DEC, NULL, 0x0,
        "Number of Alignment errors since call was established", HFILL }
    },
    { &hf_pptp_send_accm,
      { "Send ACCM", "pptp.send_accm",
        FT_UINT32, BASE_HEX, NULL, 0x0,
        "The send ACCM value the client should use to process outgoing PPP packets", HFILL }
    },
    { &hf_pptp_receive_accm,
      { "Receive ACCM", "pptp.receive_accm",
        FT_UINT32, BASE_HEX, NULL, 0x0,
        "The Receive ACCM value the client should use to process incoming PPP packets", HFILL }
    },
  };

  static ei_register_info ei[] = {
     { &ei_pptp_incorrect_magic_cookie, { "pptp.magic_cookie.incorrect", PI_PROTOCOL, PI_WARN, "Incorrect Magic Cookie", EXPFILL }},
  };

  expert_module_t* expert_pptp;

  proto_pptp = proto_register_protocol("Point-to-Point Tunnelling Protocol",
                                       "PPTP", "pptp");
  proto_register_field_array(proto_pptp, hf, array_length(hf));
  proto_register_subtree_array(ett, array_length(ett));
  expert_pptp = expert_register_protocol(proto_pptp);
  expert_register_field_array(expert_pptp, ei, array_length(ei));
}

void
proto_reg_handoff_pptp(void)
{
  dissector_handle_t pptp_handle;

  pptp_handle = create_dissector_handle(dissect_pptp, proto_pptp);
  dissector_add_uint("tcp.port", TCP_PORT_PPTP, pptp_handle);
}

/*
 * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
 *
 * Local Variables:
 * c-basic-offset: 2
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 *
 * ex: set shiftwidth=2 tabstop=8 expandtab:
 * :indentSize=2:tabSize=8:noTabs=true:
 */