-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathl2tp_avp.c
2450 lines (2135 loc) · 94.9 KB
/
l2tp_avp.c
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
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************
* Copyright (C) 2004,2005,2006,2007,2008 Katalix Systems Ltd
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*****************************************************************************/
#define _GNU_SOURCE /* for strndup() */
#include <string.h>
#include "usl.h"
#include "md5.h"
#include "l2tp_private.h"
#define L2TP_AVP_MAX_HIDE_PADDING 16
#define NO 0
#define YES 1
#define MAY 2
/* Builder may override the vendor name reported to peer */
#ifndef L2TP_VENDOR_INFO
#define L2TP_VENDOR_INFO L2TP_APP_VENDOR_INFO
#endif
/* This should be defined, but just in case... */
#ifndef _SC_HOST_NAME_MAX
#define _SC_HOST_NAME_MAX 256
#endif
struct l2tp_tunnel;
struct l2tp_msg_info;
typedef int (*encode_fn)(void *buffer, int buffer_len, struct l2tp_avp_desc const *data,
struct l2tp_msg_info const *msg_info,
struct l2tp_tunnel const *tunnel, int hide);
typedef int (*decode_fn)(struct l2tp_avp_hdr *hdr, struct l2tp_avp_desc *result, struct l2tp_tunnel const *tunnel);
struct l2tp_avp_info {
int type; /* AVP type */
uint16_t vendor; /* the vendor id associated w/ AVP */
uint8_t hide; /* can the AVP be hidden? */
uint8_t mandatory; /* is AVP mandatory? */
encode_fn encode; /* encode an AVP at hdr address */
decode_fn decode; /* decode an AVP at hdr address to result */
const char *name; /* for debugging */
};
/* Lazy typing macros... */
#define TYPE(name) L2TP_AVP_TYPE_##name
#define FUNCS(stem) l2tp_avp_encode_##stem, l2tp_avp_decode_##stem
#ifdef DEBUG
#define L2TP_AVP_HEXDUMP(flags, data, data_len) \
do { \
if (flags & L2TP_AVPDATA) { \
l2tp_mem_dump(LOG_DEBUG, data, data_len, 1); \
} \
} while(0)
#else
#define L2TP_AVP_HEXDUMP(flags, data, data_len) do { } while(0)
#endif
/* These AVPs are constant so are built at init time */
struct l2tp_avp_firmware_revision l2tp_avp_my_firmware_revision;
struct l2tp_avp_host_name *l2tp_avp_my_host_name;
struct l2tp_avp_vendor_name *l2tp_avp_my_vendor_name;
struct l2tp_avp_protocol_version l2tp_avp_my_protocol_version;
int l2tp_avp_my_host_name_len;
int l2tp_avp_my_vendor_name_len;
static int l2tp_avp_encode_message_type(void *buffer, int buffer_len, struct l2tp_avp_desc const *data,
struct l2tp_msg_info const *msg_info,
struct l2tp_tunnel const *tunnel, int hide);
static int l2tp_avp_decode_message_type(struct l2tp_avp_hdr *hdr, struct l2tp_avp_desc *result, struct l2tp_tunnel const *tunnel);
static int l2tp_avp_encode_result_code(void *buffer, int buffer_len, struct l2tp_avp_desc const *data,
struct l2tp_msg_info const *msg_info,
struct l2tp_tunnel const *tunnel, int hide);
static int l2tp_avp_decode_result_code(struct l2tp_avp_hdr *hdr, struct l2tp_avp_desc *result, struct l2tp_tunnel const *tunnel);
static int l2tp_avp_encode_protocol_version(void *buffer, int buffer_len, struct l2tp_avp_desc const *data,
struct l2tp_msg_info const *msg_info,
struct l2tp_tunnel const *tunnel, int hide);
static int l2tp_avp_decode_protocol_version(struct l2tp_avp_hdr *hdr, struct l2tp_avp_desc *result, struct l2tp_tunnel const *tunnel);
static int l2tp_avp_encode_framing_cap(void *buffer, int buffer_len, struct l2tp_avp_desc const *data,
struct l2tp_msg_info const *msg_info,
struct l2tp_tunnel const *tunnel, int hide);
static int l2tp_avp_decode_framing_cap(struct l2tp_avp_hdr *hdr, struct l2tp_avp_desc *result, struct l2tp_tunnel const *tunnel);
static int l2tp_avp_encode_bearer_cap(void *buffer, int buffer_len, struct l2tp_avp_desc const *data,
struct l2tp_msg_info const *msg_info,
struct l2tp_tunnel const *tunnel, int hide);
static int l2tp_avp_decode_bearer_cap(struct l2tp_avp_hdr *hdr, struct l2tp_avp_desc *result, struct l2tp_tunnel const *tunnel);
static int l2tp_avp_encode_tiebreaker(void *buffer, int buffer_len, struct l2tp_avp_desc const *data,
struct l2tp_msg_info const *msg_info,
struct l2tp_tunnel const *tunnel, int hide);
static int l2tp_avp_decode_tiebreaker(struct l2tp_avp_hdr *hdr, struct l2tp_avp_desc *result, struct l2tp_tunnel const *tunnel);
static int l2tp_avp_encode_firmware_revision(void *buffer, int buffer_len, struct l2tp_avp_desc const *data,
struct l2tp_msg_info const *msg_info,
struct l2tp_tunnel const *tunnel, int hide);
static int l2tp_avp_decode_firmware_revision(struct l2tp_avp_hdr *hdr, struct l2tp_avp_desc *result, struct l2tp_tunnel const *tunnel);
static int l2tp_avp_encode_host_name(void *buffer, int buffer_len, struct l2tp_avp_desc const *data,
struct l2tp_msg_info const *msg_info,
struct l2tp_tunnel const *tunnel, int hide);
static int l2tp_avp_decode_host_name(struct l2tp_avp_hdr *hdr, struct l2tp_avp_desc *result, struct l2tp_tunnel const *tunnel);
static int l2tp_avp_encode_vendor_name(void *buffer, int buffer_len, struct l2tp_avp_desc const *data,
struct l2tp_msg_info const *msg_info,
struct l2tp_tunnel const *tunnel, int hide);
static int l2tp_avp_decode_vendor_name(struct l2tp_avp_hdr *hdr, struct l2tp_avp_desc *result, struct l2tp_tunnel const *tunnel);
static int l2tp_avp_encode_tunnel_id(void *buffer, int buffer_len, struct l2tp_avp_desc const *data,
struct l2tp_msg_info const *msg_info,
struct l2tp_tunnel const *tunnel, int hide);
static int l2tp_avp_decode_tunnel_id(struct l2tp_avp_hdr *hdr, struct l2tp_avp_desc *result, struct l2tp_tunnel const *tunnel);
static int l2tp_avp_encode_rx_window_size(void *buffer, int buffer_len, struct l2tp_avp_desc const *data,
struct l2tp_msg_info const *msg_info,
struct l2tp_tunnel const *tunnel, int hide);
static int l2tp_avp_decode_rx_window_size(struct l2tp_avp_hdr *hdr, struct l2tp_avp_desc *result, struct l2tp_tunnel const *tunnel);
static int l2tp_avp_encode_challenge(void *buffer, int buffer_len, struct l2tp_avp_desc const *data,
struct l2tp_msg_info const *msg_info,
struct l2tp_tunnel const *tunnel, int hide);
static int l2tp_avp_decode_challenge(struct l2tp_avp_hdr *hdr, struct l2tp_avp_desc *result, struct l2tp_tunnel const *tunnel);
static int l2tp_avp_encode_q931_cause_code(void *buffer, int buffer_len, struct l2tp_avp_desc const *data,
struct l2tp_msg_info const *msg_info,
struct l2tp_tunnel const *tunnel, int hide);
static int l2tp_avp_decode_q931_cause_code(struct l2tp_avp_hdr *hdr, struct l2tp_avp_desc *result, struct l2tp_tunnel const *tunnel);
static int l2tp_avp_encode_challenge_response(void *buffer, int buffer_len, struct l2tp_avp_desc const *data,
struct l2tp_msg_info const *msg_info,
struct l2tp_tunnel const *tunnel, int hide);
static int l2tp_avp_decode_challenge_response(struct l2tp_avp_hdr *hdr, struct l2tp_avp_desc *result, struct l2tp_tunnel const *tunnel);
static int l2tp_avp_encode_session_id(void *buffer, int buffer_len, struct l2tp_avp_desc const *data,
struct l2tp_msg_info const *msg_info,
struct l2tp_tunnel const *tunnel, int hide);
static int l2tp_avp_decode_session_id(struct l2tp_avp_hdr *hdr, struct l2tp_avp_desc *result, struct l2tp_tunnel const *tunnel);
static int l2tp_avp_encode_call_serial_number(void *buffer, int buffer_len, struct l2tp_avp_desc const *data,
struct l2tp_msg_info const *msg_info,
struct l2tp_tunnel const *tunnel, int hide);
static int l2tp_avp_decode_call_serial_number(struct l2tp_avp_hdr *hdr, struct l2tp_avp_desc *result, struct l2tp_tunnel const *tunnel);
static int l2tp_avp_encode_minimum_bps(void *buffer, int buffer_len, struct l2tp_avp_desc const *data,
struct l2tp_msg_info const *msg_info,
struct l2tp_tunnel const *tunnel, int hide);
static int l2tp_avp_decode_minimum_bps(struct l2tp_avp_hdr *hdr, struct l2tp_avp_desc *result, struct l2tp_tunnel const *tunnel);
static int l2tp_avp_encode_maximum_bps(void *buffer, int buffer_len, struct l2tp_avp_desc const *data,
struct l2tp_msg_info const *msg_info,
struct l2tp_tunnel const *tunnel, int hide);
static int l2tp_avp_decode_maximum_bps(struct l2tp_avp_hdr *hdr, struct l2tp_avp_desc *result, struct l2tp_tunnel const *tunnel);
static int l2tp_avp_encode_bearer_type(void *buffer, int buffer_len, struct l2tp_avp_desc const *data,
struct l2tp_msg_info const *msg_info,
struct l2tp_tunnel const *tunnel, int hide);
static int l2tp_avp_decode_bearer_type(struct l2tp_avp_hdr *hdr, struct l2tp_avp_desc *result, struct l2tp_tunnel const *tunnel);
static int l2tp_avp_encode_framing_type(void *buffer, int buffer_len, struct l2tp_avp_desc const *data,
struct l2tp_msg_info const *msg_info,
struct l2tp_tunnel const *tunnel, int hide);
static int l2tp_avp_decode_framing_type(struct l2tp_avp_hdr *hdr, struct l2tp_avp_desc *result, struct l2tp_tunnel const *tunnel);
static int l2tp_avp_encode_packet_proc_delay(void *buffer, int buffer_len, struct l2tp_avp_desc const *data,
struct l2tp_msg_info const *msg_info,
struct l2tp_tunnel const *tunnel, int hide);
static int l2tp_avp_decode_packet_proc_delay(struct l2tp_avp_hdr *hdr, struct l2tp_avp_desc *result, struct l2tp_tunnel const *tunnel);
static int l2tp_avp_encode_called_number(void *buffer, int buffer_len, struct l2tp_avp_desc const *data,
struct l2tp_msg_info const *msg_info,
struct l2tp_tunnel const *tunnel, int hide);
static int l2tp_avp_decode_called_number(struct l2tp_avp_hdr *hdr, struct l2tp_avp_desc *result, struct l2tp_tunnel const *tunnel);
static int l2tp_avp_encode_calling_number(void *buffer, int buffer_len, struct l2tp_avp_desc const *data,
struct l2tp_msg_info const *msg_info,
struct l2tp_tunnel const *tunnel, int hide);
static int l2tp_avp_decode_calling_number(struct l2tp_avp_hdr *hdr, struct l2tp_avp_desc *result, struct l2tp_tunnel const *tunnel);
static int l2tp_avp_encode_sub_address(void *buffer, int buffer_len, struct l2tp_avp_desc const *data,
struct l2tp_msg_info const *msg_info,
struct l2tp_tunnel const *tunnel, int hide);
static int l2tp_avp_decode_sub_address(struct l2tp_avp_hdr *hdr, struct l2tp_avp_desc *result, struct l2tp_tunnel const *tunnel);
static int l2tp_avp_encode_tx_connect_speed(void *buffer, int buffer_len, struct l2tp_avp_desc const *data,
struct l2tp_msg_info const *msg_info,
struct l2tp_tunnel const *tunnel, int hide);
static int l2tp_avp_decode_tx_connect_speed(struct l2tp_avp_hdr *hdr, struct l2tp_avp_desc *result, struct l2tp_tunnel const *tunnel);
static int l2tp_avp_encode_physical_channel_id(void *buffer, int buffer_len, struct l2tp_avp_desc const *data,
struct l2tp_msg_info const *msg_info,
struct l2tp_tunnel const *tunnel, int hide);
static int l2tp_avp_decode_physical_channel_id(struct l2tp_avp_hdr *hdr, struct l2tp_avp_desc *result, struct l2tp_tunnel const *tunnel);
static int l2tp_avp_encode_initial_lcp_confreq(void *buffer, int buffer_len, struct l2tp_avp_desc const *data,
struct l2tp_msg_info const *msg_info,
struct l2tp_tunnel const *tunnel, int hide);
static int l2tp_avp_decode_initial_lcp_confreq(struct l2tp_avp_hdr *hdr, struct l2tp_avp_desc *result, struct l2tp_tunnel const *tunnel);
static int l2tp_avp_encode_last_sent_lcp_confreq(void *buffer, int buffer_len, struct l2tp_avp_desc const *data,
struct l2tp_msg_info const *msg_info,
struct l2tp_tunnel const *tunnel, int hide);
static int l2tp_avp_decode_last_sent_lcp_confreq(struct l2tp_avp_hdr *hdr, struct l2tp_avp_desc *result, struct l2tp_tunnel const *tunnel);
static int l2tp_avp_encode_last_rcvd_lcp_confreq(void *buffer, int buffer_len, struct l2tp_avp_desc const *data,
struct l2tp_msg_info const *msg_info,
struct l2tp_tunnel const *tunnel, int hide);
static int l2tp_avp_decode_last_rcvd_lcp_confreq(struct l2tp_avp_hdr *hdr, struct l2tp_avp_desc *result, struct l2tp_tunnel const *tunnel);
static int l2tp_avp_encode_proxy_auth_type(void *buffer, int buffer_len, struct l2tp_avp_desc const *data,
struct l2tp_msg_info const *msg_info,
struct l2tp_tunnel const *tunnel, int hide);
static int l2tp_avp_decode_proxy_auth_type(struct l2tp_avp_hdr *hdr, struct l2tp_avp_desc *result, struct l2tp_tunnel const *tunnel);
static int l2tp_avp_encode_proxy_auth_name(void *buffer, int buffer_len, struct l2tp_avp_desc const *data,
struct l2tp_msg_info const *msg_info,
struct l2tp_tunnel const *tunnel, int hide);
static int l2tp_avp_decode_proxy_auth_name(struct l2tp_avp_hdr *hdr, struct l2tp_avp_desc *result, struct l2tp_tunnel const *tunnel);
static int l2tp_avp_encode_proxy_auth_chall(void *buffer, int buffer_len, struct l2tp_avp_desc const *data,
struct l2tp_msg_info const *msg_info,
struct l2tp_tunnel const *tunnel, int hide);
static int l2tp_avp_decode_proxy_auth_chall(struct l2tp_avp_hdr *hdr, struct l2tp_avp_desc *result, struct l2tp_tunnel const *tunnel);
static int l2tp_avp_encode_proxy_auth_id(void *buffer, int buffer_len, struct l2tp_avp_desc const *data,
struct l2tp_msg_info const *msg_info,
struct l2tp_tunnel const *tunnel, int hide);
static int l2tp_avp_decode_proxy_auth_id(struct l2tp_avp_hdr *hdr, struct l2tp_avp_desc *result, struct l2tp_tunnel const *tunnel);
static int l2tp_avp_encode_proxy_auth_rsp(void *buffer, int buffer_len, struct l2tp_avp_desc const *data,
struct l2tp_msg_info const *msg_info,
struct l2tp_tunnel const *tunnel, int hide);
static int l2tp_avp_decode_proxy_auth_rsp(struct l2tp_avp_hdr *hdr, struct l2tp_avp_desc *result, struct l2tp_tunnel const *tunnel);
static int l2tp_avp_encode_call_errors(void *buffer, int buffer_len, struct l2tp_avp_desc const *data,
struct l2tp_msg_info const *msg_info,
struct l2tp_tunnel const *tunnel, int hide);
static int l2tp_avp_decode_call_errors(struct l2tp_avp_hdr *hdr, struct l2tp_avp_desc *result, struct l2tp_tunnel const *tunnel);
static int l2tp_avp_encode_accm(void *buffer, int buffer_len, struct l2tp_avp_desc const *data,
struct l2tp_msg_info const *msg_info,
struct l2tp_tunnel const *tunnel, int hide);
static int l2tp_avp_decode_accm(struct l2tp_avp_hdr *hdr, struct l2tp_avp_desc *result, struct l2tp_tunnel const *tunnel);
static int l2tp_avp_encode_random_vector(void *buffer, int buffer_len, struct l2tp_avp_desc const *data,
struct l2tp_msg_info const *msg_info,
struct l2tp_tunnel const *tunnel, int hide);
static int l2tp_avp_decode_random_vector(struct l2tp_avp_hdr *hdr, struct l2tp_avp_desc *result, struct l2tp_tunnel const *tunnel);
static int l2tp_avp_encode_priv_group_id(void *buffer, int buffer_len, struct l2tp_avp_desc const *data,
struct l2tp_msg_info const *msg_info,
struct l2tp_tunnel const *tunnel, int hide);
static int l2tp_avp_decode_priv_group_id(struct l2tp_avp_hdr *hdr, struct l2tp_avp_desc *result, struct l2tp_tunnel const *tunnel);
static int l2tp_avp_encode_rx_connect_speed(void *buffer, int buffer_len, struct l2tp_avp_desc const *data,
struct l2tp_msg_info const *msg_info,
struct l2tp_tunnel const *tunnel, int hide);
static int l2tp_avp_decode_rx_connect_speed(struct l2tp_avp_hdr *hdr, struct l2tp_avp_desc *result, struct l2tp_tunnel const *tunnel);
static int l2tp_avp_encode_sequencing_required(void *buffer, int buffer_len, struct l2tp_avp_desc const *data,
struct l2tp_msg_info const *msg_info,
struct l2tp_tunnel const *tunnel, int hide);
static int l2tp_avp_decode_sequencing_required(struct l2tp_avp_hdr *hdr, struct l2tp_avp_desc *result, struct l2tp_tunnel const *tunnel);
static int l2tp_avp_count_avps(struct l2tp_avp_desc *avp_data);
static void l2tp_avp_log_avp(struct l2tp_avp_hdr *avp, struct l2tp_tunnel const *tunnel);
/* avp_info array contains per generic AVP information including description.
* It also shows whether a given AVP is manditory, or hidden.
* Note that the array is sorted by AVP number. message avp 0 is index 0.
* This allows direct indexing (no searching required) for all AVPs with
* vendor_id 0. Don't change the order!
*/
static const struct l2tp_avp_info l2tp_avp_info[] =
{
/* type vend hide mand, encode/decode funcs name */
{ TYPE(MESSAGE), 0, NO, YES, FUNCS(message_type), "Message Type" },
{ TYPE(RESULT_CODE), 0, NO, YES, FUNCS(result_code), "Result Code" },
{ TYPE(PROTOCOL_VERSION), 0, NO, YES, FUNCS(protocol_version), "Protocol Version" },
{ TYPE(FRAMING_CAP), 0, MAY, YES, FUNCS(framing_cap), "Framing Capabilities" },
{ TYPE(BEARER_CAP), 0, MAY, YES, FUNCS(bearer_cap), "Bearer Capabilities" },
{ TYPE(TIEBREAKER), 0, NO, NO, FUNCS(tiebreaker), "Tie Breaker" },
{ TYPE(FIRMWARE_REVISION), 0, MAY, NO, FUNCS(firmware_revision), "Firmware Revision" },
{ TYPE(HOST_NAME), 0, NO, YES, FUNCS(host_name), "Host Name" },
{ TYPE(VENDOR_NAME), 0, MAY, NO, FUNCS(vendor_name), "Vendor Name" },
{ TYPE(TUNNEL_ID), 0, MAY, YES, FUNCS(tunnel_id), "Assigned Tunnel ID" },
{ TYPE(RX_WINDOW_SIZE), 0, NO, YES, FUNCS(rx_window_size), "Receive Window Size" },
{ TYPE(CHALLENGE), 0, MAY, YES, FUNCS(challenge), "Challenge" },
{ TYPE(Q931_CAUSE_CODE), 0, NO, YES, FUNCS(q931_cause_code), "Q.931 Cause Code" },
{ TYPE(CHALLENGE_RESPONSE), 0, MAY, YES, FUNCS(challenge_response), "Challenge Response" },
{ TYPE(SESSION_ID), 0, MAY, YES, FUNCS(session_id), "Assigned Session ID" },
{ TYPE(CALL_SERIAL_NUMBER), 0, MAY, YES, FUNCS(call_serial_number), "Call Serial Number" },
{ TYPE(MINIMUM_BPS), 0, MAY, YES, FUNCS(minimum_bps), "Minimum BPS" },
{ TYPE(MAXIMUM_BPS), 0, MAY, YES, FUNCS(maximum_bps), "Maximum BPS" },
{ TYPE(BEARER_TYPE), 0, MAY, YES, FUNCS(bearer_type), "Bearer Type" },
{ TYPE(FRAMING_TYPE), 0, MAY, YES, FUNCS(framing_type), "Framing Type" },
{ TYPE(PACKET_PROC_DELAY), 0, NO, NO, FUNCS(packet_proc_delay), "Packet Process Delay" },
{ TYPE(CALLED_NUMBER), 0, MAY, YES, FUNCS(called_number), "Dialed Number" },
{ TYPE(CALLING_NUMBER), 0, MAY, YES, FUNCS(calling_number), "Dialing Number" },
{ TYPE(SUB_ADDRESS), 0, MAY, YES, FUNCS(sub_address), "Sub-Address" },
{ TYPE(CONNECT_SPEED), 0, MAY, YES, FUNCS(tx_connect_speed), "Transmit Connect Speed" },
{ TYPE(PHYSICAL_CHANNEL_ID), 0, MAY, NO, FUNCS(physical_channel_id), "Physical channel ID" },
{ TYPE(INITIAL_RCVD_LCP_CONFREQ), 0, MAY, NO, FUNCS(initial_lcp_confreq), "Initial Received LCP Confreq" },
{ TYPE(LAST_SENT_LCP_CONFREQ),0, MAY, NO, FUNCS(last_sent_lcp_confreq), "Last Sent LCP Confreq" },
{ TYPE(LAST_RCVD_LCP_CONFREQ),0, MAY, NO, FUNCS(last_rcvd_lcp_confreq), "Last Received LCP Confreq" },
{ TYPE(PROXY_AUTH_TYPE), 0, MAY, NO, FUNCS(proxy_auth_type), "Proxy Authen Type" },
{ TYPE(PROXY_AUTH_NAME), 0, MAY, NO, FUNCS(proxy_auth_name), "Proxy Authen Name" },
{ TYPE(PROXY_AUTH_CHALLENGE), 0, MAY, NO, FUNCS(proxy_auth_chall), "Proxy Authen Challenge" },
{ TYPE(PROXY_AUTH_ID), 0, MAY, NO, FUNCS(proxy_auth_id), "Proxy Authen ID" },
{ TYPE(PROXY_AUTH_RESPONSE), 0, MAY, NO, FUNCS(proxy_auth_rsp), "Proxy Authen Response" },
{ TYPE(CALL_ERRORS), 0, MAY, YES, FUNCS(call_errors), "Call Errors" },
{ TYPE(ACCM), 0, MAY, YES, FUNCS(accm), "ACCM" },
{ TYPE(RANDOM_VECTOR), 0, NO, YES, FUNCS(random_vector), "Random Vector" },
{ TYPE(PRIV_GROUP_ID), 0, MAY, NO, FUNCS(priv_group_id), "Private Group ID" },
{ TYPE(RX_CONNECT_SPEED), 0, MAY, NO, FUNCS(rx_connect_speed), "Receive Connect Speed" },
{ TYPE(SEQUENCING_REQUIRED), 0, NO, YES, FUNCS(sequencing_required), "Sequencing Required" },
};
#define L2TP_AVP_TYPE_COUNT (sizeof(l2tp_avp_info) / sizeof(l2tp_avp_info[0]))
/* Message AVP parse tables */
struct l2tp_msg_info {
int type; /* message type */
uint64_t mandatory; /* bit set at (1 << avp_type) implies mandatory */
uint64_t optional; /* bit set at (1 << avp_type) implies optional */
const char *name; /* for debugging */
};
#define FBIT(avptype) (1LL << L2TP_AVP_TYPE_##avptype)
static const struct l2tp_msg_info l2tp_msg_info[] = {
{ L2TP_AVP_MSG_ILLEGAL,
0,
0,
"ILLEGAL" },
{ L2TP_AVP_MSG_SCCRQ,
FBIT(PROTOCOL_VERSION) | FBIT(HOST_NAME) | FBIT(FRAMING_CAP) | FBIT(TUNNEL_ID),
FBIT(BEARER_CAP) | FBIT(RX_WINDOW_SIZE) | FBIT(CHALLENGE) | FBIT(TIEBREAKER) |
FBIT(FIRMWARE_REVISION) | FBIT(VENDOR_NAME) | FBIT(RANDOM_VECTOR),
"SCCRQ" },
{ L2TP_AVP_MSG_SCCRP,
FBIT(PROTOCOL_VERSION) | FBIT(HOST_NAME) | FBIT(FRAMING_CAP) | FBIT(TUNNEL_ID),
FBIT(BEARER_CAP) | FBIT(RX_WINDOW_SIZE) | FBIT(CHALLENGE) |
FBIT(FIRMWARE_REVISION) | FBIT(VENDOR_NAME) | FBIT(CHALLENGE_RESPONSE) | FBIT(RANDOM_VECTOR),
"SCCRP" },
{ L2TP_AVP_MSG_SCCCN,
0,
FBIT(CHALLENGE_RESPONSE) | FBIT(RANDOM_VECTOR),
"SCCCN" },
{ L2TP_AVP_MSG_STOPCCN,
FBIT(TUNNEL_ID) | FBIT(RESULT_CODE),
FBIT(RANDOM_VECTOR),
"STOPCCN" },
{ L2TP_AVP_MSG_RESERVED1,
0,
0,
"RESERVED" },
{ L2TP_AVP_MSG_HELLO,
0,
0,
"HELLO" },
{ L2TP_AVP_MSG_OCRQ,
FBIT(SESSION_ID) | FBIT(CALL_SERIAL_NUMBER) | FBIT(MINIMUM_BPS) |
FBIT(MAXIMUM_BPS) | FBIT(BEARER_TYPE) | FBIT(FRAMING_TYPE) | FBIT(CALLED_NUMBER),
FBIT(SUB_ADDRESS) | FBIT(RANDOM_VECTOR),
"OCRQ" },
{ L2TP_AVP_MSG_OCRP,
FBIT(SESSION_ID),
FBIT(PHYSICAL_CHANNEL_ID) | FBIT(RANDOM_VECTOR),
"OCRP" },
{ L2TP_AVP_MSG_OCCN,
FBIT(CONNECT_SPEED) | FBIT(FRAMING_TYPE),
FBIT(RX_CONNECT_SPEED) | FBIT(SEQUENCING_REQUIRED) | FBIT(RANDOM_VECTOR),
"OCCN" },
{ L2TP_AVP_MSG_ICRQ,
FBIT(SESSION_ID) | FBIT(CALL_SERIAL_NUMBER),
FBIT(BEARER_TYPE) | FBIT(PHYSICAL_CHANNEL_ID) | FBIT(CALLING_NUMBER) |
FBIT(CALLED_NUMBER) | FBIT(SUB_ADDRESS) | FBIT(RANDOM_VECTOR),
"ICRQ" },
{ L2TP_AVP_MSG_ICRP,
FBIT(SESSION_ID),
FBIT(RANDOM_VECTOR),
"ICRP" },
{ L2TP_AVP_MSG_ICCN,
FBIT(CONNECT_SPEED) | FBIT(FRAMING_TYPE),
FBIT(INITIAL_RCVD_LCP_CONFREQ) | FBIT(LAST_SENT_LCP_CONFREQ) |
FBIT(LAST_RCVD_LCP_CONFREQ) | FBIT(PROXY_AUTH_NAME) |
FBIT(PROXY_AUTH_TYPE) | FBIT(PROXY_AUTH_CHALLENGE) | FBIT(PROXY_AUTH_ID) |
FBIT(PROXY_AUTH_RESPONSE) | FBIT(PRIV_GROUP_ID) | FBIT(RX_CONNECT_SPEED) |
FBIT(SEQUENCING_REQUIRED) | FBIT(RANDOM_VECTOR),
"ICCN" },
{ L2TP_AVP_MSG_RESERVED2,
0,
0,
"RESERVED" },
{ L2TP_AVP_MSG_CDN,
FBIT(RESULT_CODE) | FBIT(SESSION_ID),
FBIT(Q931_CAUSE_CODE) | FBIT(RANDOM_VECTOR),
"CDN" },
{ L2TP_AVP_MSG_WEN,
FBIT(CALL_ERRORS),
FBIT(RANDOM_VECTOR),
"WEN" },
{ L2TP_AVP_MSG_SLI,
FBIT(ACCM),
FBIT(RANDOM_VECTOR),
"SLI" }
};
#define L2TP_MSG_INFO_COUNT (sizeof(l2tp_msg_info) / sizeof(l2tp_msg_info[0]))
#undef FBIT
/*
* From RFC2661...
*
* Hiding an AVP value is done in several steps. The first step is to
* take the length and value fields of the original (cleartext) AVP and
* encode them into a Hidden AVP Subformat as follows:
*
* 0 1 2 3
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Length of Original Value | Original Attribute Value ...
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* ... | Padding ...
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*
* Length of Original Attribute Value: This is length of the Original
* Attribute Value to be obscured in octets. This is necessary to
* determine the original length of the Attribute Value which is lost
* when the additional Padding is added.
*
* Original Attribute Value: Attribute Value that is to be obscured.
*
* Padding: Random additional octets used to obscure length of the
* Attribute Value that is being hidden.
*
* To mask the size of the data being hidden, the resulting subformat
* MAY be padded as shown above. Padding does NOT alter the value placed
* in the Length of Original Attribute Value field, but does alter the
* length of the resultant AVP that is being created. For example, If an
* Attribute Value to be hidden is 4 octets in length, the unhidden AVP
* length would be 10 octets (6 + Attribute Value length). After hiding,
* the length of the AVP will become 6 + Attribute Value length + size
* of the Length of Original Attribute Value field + Padding. Thus, if
* Padding is 12 octets, the AVP length will be 6 + 4 + 2 + 12 = 24
* octets.
*
* Next, An MD5 hash is performed on the concatenation of:
*
* + the 2 octet Attribute number of the AVP
* + the shared secret
* + an arbitrary length random vector
*
* The value of the random vector used in this hash is passed in the
* value field of a Random Vector AVP. This Random Vector AVP must be
* placed in the message by the sender before any hidden AVPs. The same
* random vector may be used for more than one hidden AVP in the same
* message. If a different random vector is used for the hiding of
* subsequent AVPs then a new Random Vector AVP must be placed in the
* command message before the first AVP to which it applies.
*
* The MD5 hash value is then XORed with the first 16 octet (or less)
* segment of the Hidden AVP Subformat and placed in the Attribute Value
* field of the Hidden AVP. If the Hidden AVP Subformat is less than 16
* octets, the Subformat is transformed as if the Attribute Value field
* had been padded to 16 octets before the XOR, but only the actual
* octets present in the Subformat are modified, and the length of the
* AVP is not altered.
*
* If the Subformat is longer than 16 octets, a second one-way MD5 hash
* is calculated over a stream of octets consisting of the shared secret
* followed by the result of the first XOR. That hash is XORed with the
* second 16 octet (or less) segment of the Subformat and placed in the
* corresponding octets of the Value field of the Hidden AVP.
*
* If necessary, this operation is repeated, with the shared secret used
* along with each XOR result to generate the next hash to XOR the next
* segment of the value with.
*/
static int l2tp_avp_hide_unhide(int hide, struct l2tp_avp_hdr *hdr, void *buffer, int buffer_len,
unsigned char const *secret, int secret_len,
unsigned char const *random_vector, int random_vector_len)
{
uint8_t *data = buffer;
struct MD5Context ctx;
int buffer_pos;
int digest_pos;
uint8_t digest[16];
/* Compute initial digest */
MD5Init(&ctx);
MD5Update(&ctx, &hdr->type, 2);
MD5Update(&ctx, secret, secret_len);
MD5Update(&ctx, random_vector, random_vector_len);
MD5Final(digest, &ctx);
/* Insert value */
for (buffer_pos = 0, digest_pos = 0; buffer_pos < buffer_len; buffer_pos++, digest_pos++) {
if (digest_pos == 16) {
/* Compute new digest */
digest_pos = 0;
MD5Init(&ctx);
MD5Update(&ctx, secret, secret_len);
MD5Update(&ctx, &data[buffer_pos] - 16, 16);
MD5Final(digest, &ctx);
}
data[buffer_pos] ^= digest[digest_pos];
}
L2TP_DEBUG(L2TP_AVPDATA, "%s: avp=%hu flag_len=%hx vend=%hu, hidden data is %s", __func__,
ntohs(hdr->type), ntohs(hdr->flag_len), ntohs(hdr->vendor_id),
l2tp_buffer_hexify(buffer, buffer_len));
return 0;
}
static int l2tp_avp_hide(void **buffer, int *buffer_len,
unsigned char const *secret, int secret_len,
unsigned char const *random_vector, int random_vector_len)
{
int orig_buffer_len = *buffer_len;
void *orig_buffer = *buffer;
void *new_buffer;
uint16_t *orig_len;
uint16_t *flag_len;
uint16_t tmp;
int new_buffer_len;
int pad;
if ((secret == NULL) || (secret_len == 0)) {
return -EINVAL;
}
if ((random_vector == NULL) || (random_vector_len == 0)) {
return -EINVAL;
}
if (orig_buffer_len < L2TP_AVP_HEADER_LEN) {
return -EBADMSG;
}
/* Add a random amount of padding, between 1 and
* L2TP_AVP_MAX_HIDE_PADDING bytes. The method used here is
* copied from the rand() man page.
*/
pad = 1 + (int) ((((float) L2TP_AVP_MAX_HIDE_PADDING) * rand()) / (RAND_MAX + 1.0));
L2TP_DEBUG(L2TP_AVPDATA, "%s: pad=%d, orig_len=%d, data to hide is %s", __func__,
pad, orig_buffer_len, l2tp_buffer_hexify(orig_buffer + L2TP_AVP_HEADER_LEN, orig_buffer_len - L2TP_AVP_HEADER_LEN));
/* Allocate a new buffer big enough for the hidden AVP. Allow 2 bytes at the head
* for original length, plus 16 bytes slack for the MD5 algorithm. Use memmove()
* to copy data to new buffer because realloc() might return the original buffer
* and we just need to shift the data up 2 bytes.
*/
new_buffer_len = orig_buffer_len + 2 + pad + 16;
new_buffer = realloc(orig_buffer, new_buffer_len + L2TP_AVP_HEADER_LEN);
if (new_buffer == NULL) {
return -ENOMEM;
}
memmove(new_buffer + L2TP_AVP_HEADER_LEN + 2, orig_buffer + L2TP_AVP_HEADER_LEN, orig_buffer_len - L2TP_AVP_HEADER_LEN);
orig_len = new_buffer + L2TP_AVP_HEADER_LEN;
*orig_len = htons(orig_buffer_len - L2TP_AVP_HEADER_LEN);
if (new_buffer != orig_buffer) {
*buffer = new_buffer;
}
flag_len = new_buffer;
tmp = ntohs(*flag_len);
*flag_len = htons(tmp + 2 + pad);
*buffer_len = orig_buffer_len + 2 + pad;
L2TP_DEBUG(L2TP_AVPDATA, "%s: modified data to hide is %s", __func__,
l2tp_buffer_hexify(new_buffer + L2TP_AVP_HEADER_LEN, *buffer_len - L2TP_AVP_HEADER_LEN));
return l2tp_avp_hide_unhide(1 /* hide */, (void *) new_buffer, new_buffer + L2TP_AVP_HEADER_LEN, *buffer_len - L2TP_AVP_HEADER_LEN,
secret, secret_len, random_vector, random_vector_len);
}
static int l2tp_avp_unhide(void *buffer, int *buffer_len,
unsigned char const *secret, int secret_len,
unsigned char const *random_vector, int random_vector_len)
{
struct l2tp_avp_hdr *hdr = buffer;
uint16_t *orig_len = buffer + L2TP_AVP_HEADER_LEN;
uint16_t orig_buffer_len;
int result;
uint16_t flag_len;
if ((secret == NULL) || (secret_len == 0)) {
return -EINVAL;
}
if ((random_vector == NULL) || (random_vector_len == 0)) {
return -EINVAL;
}
L2TP_DEBUG(L2TP_AVPDATA, "%s: buffer_len=%d, data to unhide is %s", __func__,
*buffer_len, l2tp_buffer_hexify(buffer + L2TP_AVP_HEADER_LEN, *buffer_len - L2TP_AVP_HEADER_LEN));
if (*buffer_len < L2TP_AVP_HEADER_LEN) {
L2TP_DEBUG(L2TP_AVPDATA, "%s: bad message, buffer_len=%d", __func__, *buffer_len);
return -EBADMSG;
}
result = l2tp_avp_hide_unhide(0 /* unhide */, hdr, buffer + L2TP_AVP_HEADER_LEN, *buffer_len - L2TP_AVP_HEADER_LEN,
secret, secret_len, random_vector, random_vector_len);
if (result == 0) {
orig_buffer_len = ntohs(*orig_len);
if (orig_buffer_len > (*buffer_len - L2TP_AVP_HEADER_LEN)) {
L2TP_DEBUG(L2TP_AVPDATA, "%s: bad message, orig_len=%d", __func__, orig_buffer_len);
return -EBADMSG;
}
flag_len = ntohs(hdr->flag_len);
BUG_TRAP((flag_len & L2TP_AVP_HDR_HBIT) == 0);
flag_len &= ~0x03ff;
flag_len |= (orig_buffer_len + L2TP_AVP_HEADER_LEN);
hdr->flag_len = htons(flag_len);
memmove(buffer + L2TP_AVP_HEADER_LEN, buffer + L2TP_AVP_HEADER_LEN + 2, orig_buffer_len);
*buffer_len = orig_buffer_len + L2TP_AVP_HEADER_LEN;
}
return 0;
}
static void l2tp_avp_hdr_write(struct l2tp_avp_hdr *hdr,
struct l2tp_msg_info const *msg_info,
const uint16_t avp_type, const int var_len, int hide)
{
uint16_t flag_len;
flag_len = L2TP_AVP_HDR_LEN(L2TP_AVP_HEADER_LEN + var_len);
if (l2tp_avp_info[avp_type].mandatory) {
flag_len |= L2TP_AVP_HDR_MBIT;
}
if (hide && l2tp_avp_info[avp_type].hide) {
flag_len |= L2TP_AVP_HDR_HBIT;
}
hdr->vendor_id = 0;
hdr->type = htons(avp_type);
hdr->flag_len = htons(flag_len);
}
static char *l2tp_avp_stringify(void *buf, int buf_len)
{
static char string_buf[256];
int max_len = (buf_len >= (sizeof(string_buf) - 1)) ? sizeof(string_buf) - 1 : buf_len;
strncpy(&string_buf[0], buf, max_len);
string_buf[max_len] = '\0';
return &string_buf[0];
}
static int l2tp_avp_encode_message_type(void *buffer,
int buffer_len,
struct l2tp_avp_desc const *data,
struct l2tp_msg_info const *msg_info,
struct l2tp_tunnel const *tunnel, int hide)
{
struct l2tp_avp_hdr *hdr = buffer;
struct l2tp_avp_message_type *avp_data = (void *) &hdr->value[0];
l2tp_avp_hdr_write(hdr, msg_info, TYPE(MESSAGE), sizeof(struct l2tp_avp_message_type), 0);
avp_data->type = htons(data->value->message_type.type);
return 0;
}
static int l2tp_avp_decode_message_type(struct l2tp_avp_hdr *hdr,
struct l2tp_avp_desc *result, struct l2tp_tunnel const *tunnel)
{
uint16_t flag_len = ntohs(hdr->flag_len);
int avp_len = L2TP_AVP_HDR_LEN(flag_len);
if (avp_len != (sizeof(struct l2tp_avp_message_type) + L2TP_AVP_HEADER_LEN)) {
l2tp_tunnel_log(tunnel, L2TP_AVP, LOG_ERR, "AVP: tunl %hu: bad MESSAGE_TYPE message length=%hu",
l2tp_tunnel_id(tunnel), avp_len);
return -EBADMSG;
}
result->value = (void *) &hdr->value[0];
result->value_len = avp_len - L2TP_AVP_HEADER_LEN;
result->value->message_type.type = ntohs(result->value->message_type.type);
return avp_len;
}
static int l2tp_avp_encode_result_code(void *buffer,
int buffer_len,
struct l2tp_avp_desc const *data,
struct l2tp_msg_info const *msg_info,
struct l2tp_tunnel const *tunnel, int hide)
{
int result = buffer_len;
struct l2tp_avp_hdr *hdr = buffer;
struct l2tp_avp_result_code *avp_data = (void *) &hdr->value[0];
l2tp_avp_hdr_write(hdr, msg_info, TYPE(RESULT_CODE), data->value_len, hide);
avp_data->result_code = htons(data->value->result_code.result_code);
if (data->value_len > 2) {
avp_data->error_code = htons(data->value->result_code.error_code);
}
if (data->value_len > 4) {
memcpy(&avp_data->error_message[0], data->value->result_code.error_message, data->value_len - 4);
}
return result;
}
static int l2tp_avp_decode_result_code(struct l2tp_avp_hdr *hdr,
struct l2tp_avp_desc *result, struct l2tp_tunnel const *tunnel)
{
uint16_t flag_len = ntohs(hdr->flag_len);
int avp_len = L2TP_AVP_HDR_LEN(flag_len);
if (avp_len < (sizeof(result->value->result_code.result_code) + L2TP_AVP_HEADER_LEN)) {
return -EBADMSG;
}
result->value = (void *) &hdr->value[0];
result->value_len = avp_len - L2TP_AVP_HEADER_LEN;
result->value->result_code.result_code = ntohs(result->value->result_code.result_code);
if (result->value_len > 2) {
result->value->result_code.error_code = ntohs(result->value->result_code.error_code);
}
l2tp_tunnel_log(tunnel, L2TP_AVPDATA, LOG_DEBUG, "AVPDATA: RESULT_CODE: result=%hu error=%hu msg=%s",
result->value->result_code.result_code,
result->value_len > 2 ? result->value->result_code.error_code : 0,
result->value_len > 4 ? l2tp_avp_stringify(result->value->result_code.error_message,
result->value_len - 4) : "");
return avp_len;
}
static int l2tp_avp_encode_protocol_version(void *buffer,
int buffer_len,
struct l2tp_avp_desc const *data,
struct l2tp_msg_info const *msg_info,
struct l2tp_tunnel const *tunnel, int hide)
{
struct l2tp_avp_hdr *hdr = buffer;
struct l2tp_avp_protocol_version *avp_data = (void *) &hdr->value[0];
l2tp_avp_hdr_write(hdr, msg_info, TYPE(PROTOCOL_VERSION), sizeof(struct l2tp_avp_protocol_version), 0);
avp_data->ver = data->value->protocol_version.ver;
avp_data->rev = data->value->protocol_version.rev;
return 0;
}
static int l2tp_avp_decode_protocol_version(struct l2tp_avp_hdr *hdr,
struct l2tp_avp_desc *result, struct l2tp_tunnel const *tunnel)
{
uint16_t flag_len = ntohs(hdr->flag_len);
int avp_len = L2TP_AVP_HDR_LEN(flag_len);
if (avp_len != (sizeof(struct l2tp_avp_protocol_version) + L2TP_AVP_HEADER_LEN)) {
return -EBADMSG;
}
result->value = (void *) &hdr->value[0];
result->value_len = avp_len - L2TP_AVP_HEADER_LEN;
l2tp_tunnel_log(tunnel, L2TP_AVPDATA, LOG_DEBUG, "AVPDATA: PROTOCOL_VERSION: ver=%d rev=%d",
result->value->protocol_version.ver, result->value->protocol_version.rev);
return avp_len;
}
static int l2tp_avp_encode_framing_cap(void *buffer,
int buffer_len,
struct l2tp_avp_desc const *data,
struct l2tp_msg_info const *msg_info,
struct l2tp_tunnel const *tunnel, int hide)
{
int result = buffer_len;
struct l2tp_avp_hdr *hdr = buffer;
struct l2tp_avp_framing_cap *avp_data = (void *) &hdr->value[0];
l2tp_avp_hdr_write(hdr, msg_info, TYPE(FRAMING_CAP), sizeof(struct l2tp_avp_framing_cap), hide);
avp_data->value = htonl(data->value->framing_cap.value);
return result;
}
static int l2tp_avp_decode_framing_cap(struct l2tp_avp_hdr *hdr,
struct l2tp_avp_desc *result, struct l2tp_tunnel const *tunnel)
{
uint16_t flag_len = ntohs(hdr->flag_len);
int avp_len = L2TP_AVP_HDR_LEN(flag_len);
if (avp_len != (sizeof(struct l2tp_avp_framing_cap) + L2TP_AVP_HEADER_LEN)) {
return -EBADMSG;
}
result->value = (void *) &hdr->value[0];
result->value_len = avp_len - L2TP_AVP_HEADER_LEN;
result->value->framing_cap.value = ntohl(result->value->framing_cap.value);
l2tp_tunnel_log(tunnel, L2TP_AVPDATA, LOG_DEBUG, "AVPDATA: FRAMING_CAP: cap=%hu", result->value->framing_cap.value);
return avp_len;
}
static int l2tp_avp_encode_bearer_cap(void *buffer,
int buffer_len,
struct l2tp_avp_desc const *data,
struct l2tp_msg_info const *msg_info,
struct l2tp_tunnel const *tunnel, int hide)
{
int result = buffer_len;
struct l2tp_avp_hdr *hdr = buffer;
struct l2tp_avp_bearer_cap *avp_data = (void *) &hdr->value[0];
l2tp_avp_hdr_write(hdr, msg_info, TYPE(BEARER_CAP), sizeof(struct l2tp_avp_bearer_cap), hide);
avp_data->value = htonl(data->value->bearer_cap.value);
return result;
}
static int l2tp_avp_decode_bearer_cap(struct l2tp_avp_hdr *hdr,
struct l2tp_avp_desc *result, struct l2tp_tunnel const *tunnel)
{
uint16_t flag_len = ntohs(hdr->flag_len);
int avp_len = L2TP_AVP_HDR_LEN(flag_len);
if (avp_len != (sizeof(struct l2tp_avp_bearer_cap) + L2TP_AVP_HEADER_LEN)) {
return -EBADMSG;
}
result->value = (void *) &hdr->value[0];
result->value_len = avp_len - L2TP_AVP_HEADER_LEN;
result->value->bearer_cap.value = ntohl(result->value->bearer_cap.value);
l2tp_tunnel_log(tunnel, L2TP_AVPDATA, LOG_DEBUG, "AVPDATA: BEARER_CAP: cap=%hu", result->value->bearer_cap.value);
return avp_len;
}
static int l2tp_avp_encode_tiebreaker(void *buffer,
int buffer_len,
struct l2tp_avp_desc const *data,
struct l2tp_msg_info const *msg_info,
struct l2tp_tunnel const *tunnel, int hide)
{
struct l2tp_avp_hdr *hdr = buffer;
struct l2tp_avp_tiebreaker *avp_data = (void *) &hdr->value[0];
l2tp_avp_hdr_write(hdr, msg_info, TYPE(TIEBREAKER), sizeof(struct l2tp_avp_tiebreaker), 0);
memcpy(&avp_data->value[0], &data->value->tiebreaker.value[0], sizeof(struct l2tp_avp_tiebreaker));
return 0;
}
static int l2tp_avp_decode_tiebreaker(struct l2tp_avp_hdr *hdr,
struct l2tp_avp_desc *result, struct l2tp_tunnel const *tunnel)
{
uint16_t flag_len = ntohs(hdr->flag_len);
int avp_len = L2TP_AVP_HDR_LEN(flag_len);
if (avp_len != (sizeof(struct l2tp_avp_tiebreaker) + L2TP_AVP_HEADER_LEN)) {
return -EBADMSG;
}
result->value = (void *) &hdr->value[0];
result->value_len = avp_len - L2TP_AVP_HEADER_LEN;
l2tp_tunnel_log(tunnel, L2TP_AVPDATA, LOG_DEBUG, "AVPDATA: TIEBREAKER: value=%02x%02x%02x%02x%02x%02x%02x%02x",
result->value->tiebreaker.value[0], result->value->tiebreaker.value[1],
result->value->tiebreaker.value[2], result->value->tiebreaker.value[3],
result->value->tiebreaker.value[4], result->value->tiebreaker.value[5],
result->value->tiebreaker.value[6], result->value->tiebreaker.value[7]);
return avp_len;
}
static int l2tp_avp_encode_firmware_revision(void *buffer,
int buffer_len,
struct l2tp_avp_desc const *data,
struct l2tp_msg_info const *msg_info,
struct l2tp_tunnel const *tunnel, int hide)
{
int result = buffer_len;
struct l2tp_avp_hdr *hdr = buffer;
struct l2tp_avp_firmware_revision *avp_data = (void *) &hdr->value[0];
hdr = buffer;
l2tp_avp_hdr_write(hdr, msg_info, TYPE(FIRMWARE_REVISION), sizeof(struct l2tp_avp_firmware_revision), hide);
avp_data->value = htons(data->value->firmware_revision.value);
return result;
}
static int l2tp_avp_decode_firmware_revision(struct l2tp_avp_hdr *hdr,
struct l2tp_avp_desc *result, struct l2tp_tunnel const *tunnel)
{
uint16_t flag_len = ntohs(hdr->flag_len);
int avp_len = L2TP_AVP_HDR_LEN(flag_len);
if (avp_len != (sizeof(struct l2tp_avp_firmware_revision) + L2TP_AVP_HEADER_LEN)) {
return -EBADMSG;
}
result->value = (void *) &hdr->value[0];
result->value_len = avp_len - L2TP_AVP_HEADER_LEN;
result->value->firmware_revision.value = ntohs(result->value->firmware_revision.value);
l2tp_tunnel_log(tunnel, L2TP_AVPDATA, LOG_DEBUG, "AVPDATA: FIRMWARE_VERSION: revision=%hu", result->value->firmware_revision.value);
return avp_len;
}
static int l2tp_avp_encode_host_name(void *buffer,
int buffer_len,
struct l2tp_avp_desc const *data,
struct l2tp_msg_info const *msg_info,
struct l2tp_tunnel const *tunnel, int hide)
{
struct l2tp_avp_hdr *hdr = buffer;
struct l2tp_avp_host_name *avp_data = (void *) &hdr->value[0];
l2tp_avp_hdr_write(hdr, msg_info, TYPE(HOST_NAME), data->value_len, 0);
memcpy(&avp_data->string[0], &data->value->host_name.string[0], data->value_len);
return 0;
}
static int l2tp_avp_decode_host_name(struct l2tp_avp_hdr *hdr,
struct l2tp_avp_desc *result, struct l2tp_tunnel const *tunnel)
{
uint16_t flag_len = ntohs(hdr->flag_len);
int avp_len = L2TP_AVP_HDR_LEN(flag_len);
result->value = (void *) &hdr->value[0];
result->value_len = avp_len - L2TP_AVP_HEADER_LEN;
l2tp_tunnel_log(tunnel, L2TP_AVPDATA, LOG_DEBUG, "AVPDATA: HOST_NAME: name=%s",
l2tp_avp_stringify(result->value->host_name.string, result->value_len));
return avp_len;
}
static int l2tp_avp_encode_vendor_name(void *buffer,
int buffer_len,
struct l2tp_avp_desc const *data,
struct l2tp_msg_info const *msg_info,
struct l2tp_tunnel const *tunnel, int hide)
{
int result = buffer_len;
struct l2tp_avp_hdr *hdr = buffer;
struct l2tp_avp_vendor_name *avp_data = (void *) &hdr->value[0];
l2tp_avp_hdr_write(hdr, msg_info, TYPE(VENDOR_NAME), data->value_len, hide);
memcpy(&avp_data->string[0], &data->value->vendor_name.string[0], data->value_len);
return result;
}
static int l2tp_avp_decode_vendor_name(struct l2tp_avp_hdr *hdr,
struct l2tp_avp_desc *result, struct l2tp_tunnel const *tunnel)
{
uint16_t flag_len = ntohs(hdr->flag_len);
int avp_len = L2TP_AVP_HDR_LEN(flag_len);
result->value = (void *) &hdr->value[0];
result->value_len = avp_len - L2TP_AVP_HEADER_LEN;
l2tp_tunnel_log(tunnel, L2TP_AVPDATA, LOG_DEBUG, "AVPDATA: VENDOR_NAME: name=%s",
l2tp_avp_stringify(result->value->vendor_name.string, result->value_len));
return avp_len;
}
static int l2tp_avp_encode_tunnel_id(void *buffer,
int buffer_len,
struct l2tp_avp_desc const *data,
struct l2tp_msg_info const *msg_info,
struct l2tp_tunnel const *tunnel, int hide)
{
int result = buffer_len;
struct l2tp_avp_hdr *hdr = buffer;
struct l2tp_avp_tunnel_id *avp_data = (void *) &hdr->value[0];
l2tp_avp_hdr_write(hdr, msg_info, TYPE(TUNNEL_ID), sizeof(struct l2tp_avp_tunnel_id), hide);
avp_data->value = htons(data->value->tunnel_id.value);
return result;
}
static int l2tp_avp_decode_tunnel_id(struct l2tp_avp_hdr *hdr,
struct l2tp_avp_desc *result, struct l2tp_tunnel const *tunnel)
{
uint16_t flag_len = ntohs(hdr->flag_len);
int avp_len = L2TP_AVP_HDR_LEN(flag_len);
if (avp_len != (sizeof(struct l2tp_avp_tunnel_id) + L2TP_AVP_HEADER_LEN)) {
return -EBADMSG;
}
result->value = (void *) &hdr->value[0];
result->value_len = avp_len - L2TP_AVP_HEADER_LEN;
result->value->tunnel_id.value = ntohs(result->value->tunnel_id.value);
l2tp_tunnel_log(tunnel, L2TP_AVPDATA, LOG_DEBUG, "AVPDATA: TUNNEL_ID: id=%hu", result->value->tunnel_id.value);
return avp_len;
}
static int l2tp_avp_encode_rx_window_size(void *buffer,
int buffer_len,
struct l2tp_avp_desc const *data,
struct l2tp_msg_info const *msg_info,
struct l2tp_tunnel const *tunnel, int hide)
{
int result = buffer_len;
struct l2tp_avp_hdr *hdr = buffer;
struct l2tp_avp_rx_window_size *avp_data = (void *) &hdr->value[0];
l2tp_avp_hdr_write(hdr, msg_info, TYPE(RX_WINDOW_SIZE), sizeof(struct l2tp_avp_rx_window_size), hide);
avp_data->value = htons(data->value->rx_window_size.value);
return result;
}
static int l2tp_avp_decode_rx_window_size(struct l2tp_avp_hdr *hdr,
struct l2tp_avp_desc *result, struct l2tp_tunnel const *tunnel)
{
uint16_t flag_len = ntohs(hdr->flag_len);
int avp_len = L2TP_AVP_HDR_LEN(flag_len);
if (avp_len != (sizeof(struct l2tp_avp_rx_window_size) + L2TP_AVP_HEADER_LEN)) {
return -EBADMSG;
}
result->value = (void *) &hdr->value[0];
result->value_len = avp_len - L2TP_AVP_HEADER_LEN;
result->value->rx_window_size.value = ntohs(result->value->rx_window_size.value);
l2tp_tunnel_log(tunnel, L2TP_AVPDATA, LOG_DEBUG, "AVPDATA: RX_WINDOW_SIZE: size=%hu", result->value->rx_window_size.value);
return avp_len;
}
static int l2tp_avp_encode_challenge(void *buffer,
int buffer_len,
struct l2tp_avp_desc const *data,
struct l2tp_msg_info const *msg_info,
struct l2tp_tunnel const *tunnel, int hide)
{