forked from xelerance/xl2tpd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
avp.c
1798 lines (1724 loc) · 50.4 KB
/
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
/*
* Layer Two Tunnelling Protocol Daemon
* Copyright (C) 1998 Adtran, Inc.
* Copyright (C) 2002 Jeff McAdams
*
* Mark Spencer
*
* This software is distributed under the terms
* of the GPL, which you should have received
* along with this source.
*
* Attribute Value Pair handler routines
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <netinet/in.h>
#include "l2tp.h"
#define AVP_MAX 39
struct avp avps[] = {
{0, 1, &message_type_avp, "Message Type"},
{1, 1, &result_code_avp, "Result Code"},
{2, 1, &protocol_version_avp, "Protocol Version"},
{3, 1, &framing_caps_avp, "Framing Capabilities"},
{4, 1, &bearer_caps_avp, "Bearer Capabilities"},
{5, 0, NULL, "Tie Breaker"},
{6, 0, &firmware_rev_avp, "Firmware Revision"},
{7, 0, &hostname_avp, "Host Name"},
{8, 1, &vendor_avp, "Vendor Name"},
{9, 1, &assigned_tunnel_avp, "Assigned Tunnel ID"},
{10, 1, &receive_window_size_avp, "Receive Window Size"},
{11, 1, &challenge_avp, "Challenge"},
{12, 0, NULL, "Q.931 Cause Code"},
{13, 1, &chalresp_avp, "Challenge Response"},
{14, 1, &assigned_call_avp, "Assigned Call ID"},
{15, 1, &call_serno_avp, "Call Serial Number"},
{16, 1, NULL, "Minimum BPS"},
{17, 1, NULL, "Maximum BPS"},
{18, 1, &bearer_type_avp, "Bearer Type"},
{19, 1, &frame_type_avp, "Framing Type"},
{20, 1, &packet_delay_avp, "Packet Processing Delay"},
{21, 1, &dialed_number_avp, "Dialed Number"},
{22, 1, &dialing_number_avp, "Dialing Number"},
{23, 1, &sub_address_avp, "Sub-Address"},
{24, 1, &tx_speed_avp, "Transmit Connect Speed"},
{25, 1, &call_physchan_avp, "Physical channel ID"},
{26, 0, NULL, "Initial Received LCP Confreq"},
{27, 0, NULL, "Last Sent LCP Confreq"},
{28, 0, NULL, "Last Received LCP Confreq"},
{29, 1, &ignore_avp, "Proxy Authen Type"},
{30, 0, &ignore_avp, "Proxy Authen Name"},
{31, 0, &ignore_avp, "Proxy Authen Challenge"},
{32, 0, &ignore_avp, "Proxy Authen ID"},
{33, 1, &ignore_avp, "Proxy Authen Response"},
{34, 1, NULL, "Call Errors"},
{35, 1, &ignore_avp, "ACCM"},
{36, 1, &rand_vector_avp, "Random Vector"},
{37, 1, NULL, "Private Group ID"},
{38, 0, &rx_speed_avp, "Receive Connect Speed"},
{39, 1, &seq_reqd_avp, "Sequencing Required"}
};
char *msgtypes[] = {
NULL,
"Start-Control-Connection-Request",
"Start-Control-Connection-Reply",
"Start-Control-Connection-Connected",
"Stop-Control-Connection-Notification",
NULL,
"Hello",
"Outgoing-Call-Request",
"Outgoing-Call-Reply",
"Outgoing-Call-Connected",
"Incoming-Call-Request",
"Incoming-Call-Reply",
"Incoming-Call-Connected",
NULL,
"Call-Disconnect-Notify",
"WAN-Error-Notify",
"Set-Link-Info"
};
char *stopccn_result_codes[] = {
"Reserved",
"General request to clear control connection",
"General error--Error Code indicates the problem",
"Control channel already exists",
"Requester is not authorized to establish a control channel",
"The protocol version of the requester is not supported--Error Code indicates the highest version supported",
"Requester is being shut down",
"Finite State Machine error"
};
char *cdn_result_codes[] = {
"Reserved",
"Call disconnected due to loss of carrier",
"Call disconnected for the reason indicated in error code",
"Call disconnected for administrative reasons",
"Call failed due to lack of appropriate facilities being available (temporary condition)",
"Call failed due to lack of appropriate facilities being available (permanent condition)",
"Invalid destination",
"Call failed due to no carrier detected",
"Call failed due to lack of a dial tone",
"Call was no established within time allotted by LAC",
"Call was connected but no appropriate framing was detect"
};
void wrong_length (struct call *c, char *field, int expected, int found,
int min)
{
if (min)
snprintf (c->errormsg, sizeof (c->errormsg),
"%s: expected at least %d, got %d", field, expected, found);
else
snprintf (c->errormsg, sizeof (c->errormsg),
"%s: expected %d, got %d", field, expected, found);
c->error = ERROR_LENGTH;
c->result = RESULT_ERROR;
c->needclose = -1;
}
struct unaligned_u16 {
_u16 s;
} __attribute__((packed));
/*
* t, c, data, and datalen may be assumed to be defined for all avp's
*/
int message_type_avp (struct tunnel *t, struct call *c, void *data,
int datalen)
{
/*
* This will be with every control message. It is critical that this
* procedure check for the validity of sending this kind of a message
* (assuming sanity check)
*/
struct unaligned_u16 *raw = data;
c->msgtype = ntohs (raw[3].s);
if (datalen != 8)
{
if (DEBUG)
l2tp_log (LOG_DEBUG, "%s: wrong size (%d != 8)\n", __FUNCTION__,
datalen);
wrong_length (c, "Message Type", 8, datalen, 0);
return -EINVAL;
}
if ((c->msgtype > MAX_MSG) || (!msgtypes[c->msgtype]))
{
if (DEBUG)
l2tp_log (LOG_DEBUG, "%s: unknown message type %d\n", __FUNCTION__,
c->msgtype);
return -EINVAL;
}
if (gconfig.debug_avp)
if (DEBUG)
l2tp_log (LOG_DEBUG, "%s: message type %d (%s)\n", __FUNCTION__,
c->msgtype, msgtypes[c->msgtype]);
#ifdef SANITY
if (t->sanity)
{
/*
* Look ou our state for each message and make sure everything
* make sense...
*/
if ((c != t->self) && (c->msgtype < Hello))
{
if (DEBUG)
l2tp_log (LOG_DEBUG,
"%s: attempting to negotiate tunnel inside a call!\n",
__FUNCTION__);
return -EINVAL;
}
switch (c->msgtype)
{
case SCCRQ:
if ((t->state != 0) && (t->state != SCCRQ))
{
/*
* When we handle tie breaker AVP's, then we'll check
* to see if we've both requested tunnels
*/
if (DEBUG)
l2tp_log (LOG_DEBUG,
"%s: attempting to negotiate SCCRQ with state != 0\n",
__FUNCTION__);
return -EINVAL;
}
break;
case SCCRP:
if (t->state != SCCRQ)
{
if (DEBUG)
l2tp_log (LOG_DEBUG,
"%s: attempting to negotiate SCCRP with state != SCCRQ!\n",
__FUNCTION__);
return -EINVAL;
}
break;
case SCCCN:
if (t->state != SCCRP)
{
if (DEBUG)
l2tp_log (LOG_DEBUG,
"%s: attempting to negotiate SCCCN with state != SCCRP!\n",
__FUNCTION__);
return -EINVAL;
}
break;
case ICRQ:
if (t->state != SCCCN)
{
if (DEBUG)
l2tp_log (LOG_DEBUG,
"%s: attempting to negotiate ICRQ when state != SCCCN\n",
__FUNCTION__);
return -EINVAL;
}
if (c != t->self)
{
if (DEBUG)
l2tp_log (LOG_DEBUG,
"%s: attempting to negotiate ICRQ on a call!\n",
__FUNCTION__);
return -EINVAL;
}
break;
case ICRP:
if (t->state != SCCCN)
{
if (DEBUG)
l2tp_log (LOG_DEBUG,
"%s: attempting to negotiate ICRP on tunnel!=SCCCN\n",
__FUNCTION__);
return -EINVAL;
}
if (c->state != ICRQ)
{
if (DEBUG)
l2tp_log (LOG_DEBUG,
"%s: attempting to negotiate ICRP when state != ICRQ\n",
__FUNCTION__);
return -EINVAL;
}
break;
case ICCN:
if (c->state != ICRP)
{
if (DEBUG)
l2tp_log (LOG_DEBUG,
"%s: attempting to negotiate ICCN when state != ICRP\n",
__FUNCTION__);
return -EINVAL;
}
break;
case SLI:
if (c->state != ICCN)
{
if (DEBUG)
l2tp_log (LOG_DEBUG,
"%s: attempting to negotiate SLI when state != ICCN\n",
__FUNCTION__);
return -EINVAL;
}
break;
case OCRP: /* jz: case for ORCP */
if (t->state != SCCCN)
{
if (DEBUG)
l2tp_log (LOG_DEBUG,
"%s: attempting to negotiate OCRP on tunnel!=SCCCN\n",
__FUNCTION__);
return -EINVAL;
}
if (c->state != OCRQ)
{
if (DEBUG)
l2tp_log (LOG_DEBUG,
"%s: attempting to negotiate OCRP when state != OCRQ\n",
__FUNCTION__);
return -EINVAL;
}
break;
case OCCN: /* jz: case for OCCN */
if (c->state != OCRQ)
{
if (DEBUG)
l2tp_log (LOG_DEBUG,
"%s: attempting to negotiate OCCN when state != OCRQ\n",
__FUNCTION__);
return -EINVAL;
}
break;
case StopCCN:
case CDN:
case Hello:
break;
default:
l2tp_log (LOG_WARNING, "%s: i don't know how to handle %s messages\n",
__FUNCTION__, msgtypes[c->msgtype]);
return -EINVAL;
}
}
#endif
if (c->msgtype == ICRQ)
{
struct call *tmp;
if (gconfig.debug_avp)
{
if (DEBUG)
l2tp_log (LOG_DEBUG, "%s: new incoming call\n", __FUNCTION__);
}
tmp = new_call (t);
if (!tmp)
{
l2tp_log (LOG_WARNING, "%s: unable to create new call\n", __FUNCTION__);
return -EINVAL;
}
tmp->next = t->call_head;
t->call_head = tmp;
t->count++;
/*
* Is this still safe to assume that the head will always
* be the most recent call being negotiated?
* Probably... FIXME anyway...
*/
}
return 0;
}
int rand_vector_avp (struct tunnel *t, struct call *c, void *data,
int datalen)
{
int size;
struct unaligned_u16 *raw = data;
size = raw[0].s & 0x03FF;
size -= sizeof (struct avp_hdr);
#ifdef SANITY
if (t->sanity)
{
if (size < 0)
{
if (DEBUG)
l2tp_log (LOG_DEBUG, "%s: Random vector too small (%d < 0)\n",
__FUNCTION__, size);
wrong_length (c, "Random Vector", 6, datalen, 1);
return -EINVAL;
}
if (size > MAX_VECTOR_SIZE)
{
if (DEBUG)
l2tp_log (LOG_DEBUG, "%s: Random vector too large (%d > %d)\n",
__FUNCTION__, datalen, MAX_VECTOR_SIZE);
wrong_length (c, "Random Vector", 6, datalen, 1);
return -EINVAL;
}
}
#endif
if (gconfig.debug_avp)
l2tp_log (LOG_DEBUG, "%s: Random Vector of %d octets\n", __FUNCTION__,
size);
t->chal_us.vector = (unsigned char *) &raw[3].s;
t->chal_us.vector_len = size;
return 0;
}
int ignore_avp (struct tunnel *t, struct call *c, void *data, int datalen)
{
/*
* The spec says we have to accept authentication information
* even if we just ignore it, so that's exactly what
* we're going to do at this point. Proxy authentication is such
* a rediculous security threat anyway except from local
* controled machines.
*
* FIXME: I need to handle proxy authentication as an option.
* One option is to simply change the options we pass to pppd.
*
*/
if (gconfig.debug_avp)
{
if (DEBUG)
l2tp_log (LOG_DEBUG, "%s : Ignoring AVP\n", __FUNCTION__);
}
return 0;
}
int seq_reqd_avp (struct tunnel *t, struct call *c, void *data, int datalen)
{
#ifdef SANITY
if (t->sanity)
{
if (datalen != 6)
{
if (DEBUG)
l2tp_log (LOG_DEBUG,
"%s: avp is incorrect size. %d != 6\n", __FUNCTION__,
datalen);
wrong_length (c, "Sequencing Required", 6, datalen, 1);
return -EINVAL;
}
switch (c->msgtype)
{
case ICCN:
break;
default:
if (DEBUG)
l2tp_log (LOG_DEBUG,
"%s: sequencing required not appropriate for %s!\n",
__FUNCTION__, msgtypes[c->msgtype]);
return -EINVAL;
}
}
#endif
if (gconfig.debug_avp)
{
if (DEBUG)
l2tp_log (LOG_DEBUG, "%s: peer requires sequencing.\n", __FUNCTION__);
}
c->seq_reqd = -1;
return 0;
}
int result_code_avp (struct tunnel *t, struct call *c, void *data,
int datalen)
{
/*
* Find out what version of l2tp the other side is using.
* I'm not sure what we're supposed to do with this but whatever..
*/
int error;
int result;
struct unaligned_u16 *raw = data;
#ifdef SANITY
if (t->sanity)
{
if (datalen < 10)
{
if (DEBUG)
l2tp_log (LOG_DEBUG,
"%s: avp is incorrect size. %d < 10\n", __FUNCTION__,
datalen);
wrong_length (c, "Result Code", 10, datalen, 1);
return -EINVAL;
}
switch (c->msgtype)
{
case CDN:
case StopCCN:
break;
default:
if (DEBUG)
l2tp_log (LOG_DEBUG,
"%s: result code not appropriate for %s. Ignoring.\n",
__FUNCTION__, msgtypes[c->msgtype]);
return 0;
}
}
#endif
result = ntohs (raw[3].s);
error = ntohs (raw[4].s);
/*
* from prepare_StopCCN and prepare_CDN, note missing htons() call
* http://www.opensource.apple.com/source/ppp/ppp-412.3/Drivers/L2TP/L2TP-plugin/l2tp.c
*/
if (((result & 0xFF) == 0) && (result >> 8 != 0))
{
if (DEBUG)
l2tp_log (LOG_DEBUG,
"%s: result code endianness fix for buggy Apple client. network=%d, le=%d\n",
__FUNCTION__, result, result >> 8);
result >>= 8;
}
if (((error & 0xFF) == 0) && (error >> 8 != 0))
{
if (DEBUG)
l2tp_log (LOG_DEBUG,
"%s: error code endianness fix for buggy Apple client. network=%d, le=%d\n",
__FUNCTION__, error, error >> 8);
error >>= 8;
}
if ((c->msgtype == StopCCN) && ((result > 7) || (result < 1)))
{
if (DEBUG)
l2tp_log (LOG_DEBUG,
"%s: result code out of range (%d %d %d). Ignoring.\n",
__FUNCTION__, result, error, datalen);
return 0;
}
if ((c->msgtype == CDN) && ((result > 11) || (result < 1)))
{
if (DEBUG)
l2tp_log (LOG_DEBUG,
"%s: result code out of range (%d %d %d). Ignoring.\n",
__FUNCTION__, result, error, datalen);
return 0;
}
c->error = error;
c->result = result;
safe_copy (c->errormsg, (char *) &raw[5].s, datalen - 10);
if (gconfig.debug_avp)
{
if (DEBUG && (c->msgtype == StopCCN))
{
l2tp_log (LOG_DEBUG,
"%s: peer closing for reason %d (%s), error = %d (%s)\n",
__FUNCTION__, result, stopccn_result_codes[result], error,
c->errormsg);
}
else
{
l2tp_log (LOG_DEBUG,
"%s: peer closing for reason %d (%s), error = %d (%s)\n",
__FUNCTION__, result, cdn_result_codes[result], error,
c->errormsg);
}
}
return 0;
}
int protocol_version_avp (struct tunnel *t, struct call *c, void *data,
int datalen)
{
/*
* Find out what version of l2tp the other side is using.
* I'm not sure what we're supposed to do with this but whatever..
*/
int ver;
struct unaligned_u16 *raw = data;
#ifdef SANITY
if (t->sanity)
{
if (datalen != 8)
{
if (DEBUG)
l2tp_log (LOG_DEBUG,
"%s: avp is incorrect size. %d != 8\n", __FUNCTION__,
datalen);
wrong_length (c, "Protocol Version", 8, datalen, 1);
return -EINVAL;
}
switch (c->msgtype)
{
case SCCRP:
case SCCRQ:
break;
default:
if (DEBUG)
l2tp_log (LOG_DEBUG,
"%s: protocol version not appropriate for %s. Ignoring.\n",
__FUNCTION__, msgtypes[c->msgtype]);
return 0;
}
}
#endif
ver = ntohs (raw[3].s);
if (gconfig.debug_avp)
{
if (DEBUG)
l2tp_log (LOG_DEBUG,
"%s: peer is using version %d, revision %d.\n", __FUNCTION__,
(ver >> 8), ver & 0xFF);
}
return 0;
}
int framing_caps_avp (struct tunnel *t, struct call *c, void *data,
int datalen)
{
/*
* Retrieve the framing capabilities
* from the peer
*/
int caps;
struct unaligned_u16 *raw = data;
#ifdef SANITY
if (t->sanity)
{
switch (c->msgtype)
{
case SCCRP:
case SCCRQ:
break;
default:
if (DEBUG)
l2tp_log (LOG_DEBUG,
"%s: framing capabilities not appropriate for %s. Ignoring.\n",
__FUNCTION__, msgtypes[c->msgtype]);
return 0;
}
if (datalen != 10)
{
if (DEBUG)
l2tp_log (LOG_DEBUG,
"%s: avp is incorrect size. %d != 10\n", __FUNCTION__,
datalen);
wrong_length (c, "Framming Capabilities", 10, datalen, 0);
return -EINVAL;
}
}
#endif
caps = ntohs (raw[4].s);
if (gconfig.debug_avp)
if (DEBUG)
l2tp_log (LOG_DEBUG,
"%s: supported peer frames:%s%s\n", __FUNCTION__,
caps & ASYNC_FRAMING ? " async" : "",
caps & SYNC_FRAMING ? " sync" : "");
t->fc = caps & (ASYNC_FRAMING | SYNC_FRAMING);
return 0;
}
int bearer_caps_avp (struct tunnel *t, struct call *c, void *data,
int datalen)
{
/*
* What kind of bearer channels does our peer support?
*/
int caps;
struct unaligned_u16 *raw = data;
#ifdef SANITY
if (t->sanity)
{
switch (c->msgtype)
{
case SCCRP:
case SCCRQ:
break;
default:
if (DEBUG)
l2tp_log (LOG_DEBUG,
"%s: bearer capabilities not appropriate for message %s. Ignoring.\n",
__FUNCTION__, msgtypes[c->msgtype]);
return 0;
}
if (datalen != 10)
{
if (DEBUG)
l2tp_log (LOG_DEBUG,
"%s: avp is incorrect size. %d != 10\n", __FUNCTION__,
datalen);
wrong_length (c, "Bearer Capabilities", 10, datalen, 0);
return -EINVAL;
}
}
#endif
caps = ntohs (raw[4].s);
if (gconfig.debug_avp)
{
if (DEBUG)
{
l2tp_log (LOG_DEBUG,
"%s: supported peer bearers:%s%s\n",
__FUNCTION__,
caps & ANALOG_BEARER ? " analog" : "",
caps & DIGITAL_BEARER ? " digital" : "");
}
}
t->bc = caps & (ANALOG_BEARER | DIGITAL_BEARER);
return 0;
}
/* FIXME: I need to handle tie breakers eventually */
int firmware_rev_avp (struct tunnel *t, struct call *c, void *data,
int datalen)
{
/*
* Report and record remote firmware version
*/
int ver;
struct unaligned_u16 *raw = data;
#ifdef SANITY
if (t->sanity)
{
switch (c->msgtype)
{
case SCCRP:
case SCCRQ:
break;
default:
if (DEBUG)
l2tp_log (LOG_DEBUG,
"%s: firmware revision not appropriate for message %s. Ignoring.\n",
__FUNCTION__, msgtypes[c->msgtype]);
return 0;
}
if (datalen != 8)
{
if (DEBUG)
l2tp_log (LOG_DEBUG,
"%s: avp is incorrect size. %d != 8\n", __FUNCTION__,
datalen);
wrong_length (c, "Firmware Revision", 8, datalen, 0);
return -EINVAL;
}
}
#endif
ver = ntohs (raw[3].s);
if (gconfig.debug_avp)
{
if (DEBUG)
l2tp_log (LOG_DEBUG,
"%s: peer reports firmware version %d (0x%.4x)\n",
__FUNCTION__, ver, ver);
}
t->firmware = ver;
return 0;
}
int bearer_type_avp (struct tunnel *t, struct call *c, void *data,
int datalen)
{
/*
* What kind of bearer channel is the call on?
*/
int b;
struct unaligned_u16 *raw = data;
#ifdef SANITY
if (t->sanity)
{
switch (c->msgtype)
{
case ICRQ:
case OCRQ:
break;
default:
if (DEBUG)
l2tp_log (LOG_DEBUG,
"%s: bearer type not appropriate for message %s. Ignoring.\n",
__FUNCTION__, msgtypes[c->msgtype]);
return 0;
}
if (datalen != 10)
{
if (DEBUG)
l2tp_log (LOG_DEBUG,
"%s: avp is incorrect size. %d != 10\n", __FUNCTION__,
datalen);
wrong_length (c, "Bearer Type", 10, datalen, 0);
return -EINVAL;
}
}
#endif
b = ntohs (raw[4].s);
if (gconfig.debug_avp)
{
if (DEBUG)
l2tp_log (LOG_DEBUG,
"%s: peer bears:%s\n", __FUNCTION__,
b & ANALOG_BEARER ? " analog" : "digital");
}
t->call_head->bearer = b;
return 0;
}
int frame_type_avp (struct tunnel *t, struct call *c, void *data, int datalen)
{
/*
* What kind of frame channel is the call on?
*/
int b;
struct unaligned_u16 *raw = data;
#ifdef SANITY
if (t->sanity)
{
switch (c->msgtype)
{
case ICCN:
case OCRQ:
case OCCN:
break;
default:
if (DEBUG)
l2tp_log (LOG_DEBUG,
"%s: frame type not appropriate for message %s. Ignoring.\n",
__FUNCTION__, msgtypes[c->msgtype]);
return 0;
}
if (datalen != 10)
{
if (DEBUG)
l2tp_log (LOG_DEBUG,
"%s: avp is incorrect size. %d != 10\n", __FUNCTION__,
datalen);
wrong_length (c, "Frame Type", 10, datalen, 0);
return -EINVAL;
}
}
#endif
b = ntohs (raw[4].s);
if (gconfig.debug_avp)
{
if (DEBUG)
l2tp_log (LOG_DEBUG,
"%s: peer uses:%s frames\n", __FUNCTION__,
b & ASYNC_FRAMING ? " async" : "sync");
}
c->frame = b;
return 0;
}
int hostname_avp (struct tunnel *t, struct call *c, void *data, int datalen)
{
/*
* What is the peer's name?
*/
int size;
struct unaligned_u16 *raw = data;
#ifdef SANITY
if (t->sanity)
{
switch (c->msgtype)
{
case SCCRP:
case SCCRQ:
break;
default:
if (DEBUG)
l2tp_log (LOG_DEBUG,
"%s: hostname not appropriate for message %s. Ignoring.\n",
__FUNCTION__, msgtypes[c->msgtype]);
return 0;
}
if (datalen < 6)
{
if (DEBUG)
l2tp_log (LOG_DEBUG,
"%s: avp is too small. %d < 6\n", __FUNCTION__,
datalen);
wrong_length (c, "Hostname", 6, datalen, 1);
return -EINVAL;
}
}
#endif
size = raw[0].s & 0x03FF;
size -= sizeof (struct avp_hdr);
if (size > MAXSTRLEN - 1)
{
if (DEBUG)
l2tp_log (LOG_DEBUG, "%s: truncating reported hostname (size is %d)\n",
__FUNCTION__, size);
size = MAXSTRLEN - 1;
}
safe_copy (t->hostname, (char *) &raw[3].s, size);
if (gconfig.debug_avp)
{
if (DEBUG)
l2tp_log (LOG_DEBUG,
"%s: peer reports hostname '%s'\n", __FUNCTION__,
t->hostname);
}
return 0;
}
int dialing_number_avp (struct tunnel *t, struct call *c, void *data,
int datalen)
{
/*
* What is the peer's name?
*/
int size;
struct unaligned_u16 *raw = data;
#ifdef SANITY
if (t->sanity)
{
switch (c->msgtype)
{
case ICRQ:
break;
default:
if (DEBUG)
l2tp_log (LOG_DEBUG,
"%s: dialing number not appropriate for message %s. Ignoring.\n",
__FUNCTION__, msgtypes[c->msgtype]);
return 0;
}
if (datalen < 6)
{
if (DEBUG)
l2tp_log (LOG_DEBUG,
"%s: avp is too small. %d < 6\n", __FUNCTION__,
datalen);
wrong_length (c, "Dialing Number", 6, datalen, 1);
return -EINVAL;
}
}
#endif
size = raw[0].s & 0x03FF;
size -= sizeof (struct avp_hdr);
if (size > MAXSTRLEN - 1)
{
if (DEBUG)
l2tp_log (LOG_DEBUG,
"%s: truncating reported dialing number (size is %d)\n",
__FUNCTION__, size);
size = MAXSTRLEN - 1;
}
safe_copy (t->call_head->dialing, (char *) &raw[3].s, size);
if (gconfig.debug_avp)
{
if (DEBUG)
l2tp_log (LOG_DEBUG,
"%s: peer reports dialing number '%s'\n", __FUNCTION__,
t->call_head->dialing);
}
return 0;
}
int dialed_number_avp (struct tunnel *t, struct call *c, void *data,
int datalen)
{
/*
* What is the peer's name?
*/
int size;
struct unaligned_u16 *raw = data;
#ifdef SANITY
if (t->sanity)
{
switch (c->msgtype)
{
case OCRQ:
case ICRQ:
break;
default:
if (DEBUG)
l2tp_log (LOG_DEBUG,
"%s: dialed number not appropriate for message %s. Ignoring.\n",
__FUNCTION__, msgtypes[c->msgtype]);
return 0;
}
if (datalen < 6)
{
if (DEBUG)
l2tp_log (LOG_DEBUG,
"%s: avp is too small. %d < 6\n", __FUNCTION__,
datalen);
wrong_length (c, "Dialed Number", 6, datalen, 1);
return -EINVAL;
}
}
#endif
size = raw[0].s & 0x03FF;
size -= sizeof (struct avp_hdr);
if (size > MAXSTRLEN - 1)
{
if (DEBUG)
l2tp_log (LOG_DEBUG,
"%s: truncating reported dialed number (size is %d)\n",
__FUNCTION__, size);
size = MAXSTRLEN - 1;
}
safe_copy (t->call_head->dialed, (char *) &raw[3].s, size);
if (gconfig.debug_avp)
{
if (DEBUG)
l2tp_log (LOG_DEBUG,
"%s: peer reports dialed number '%s'\n", __FUNCTION__,
t->call_head->dialed);
}
return 0;
}
int sub_address_avp (struct tunnel *t, struct call *c, void *data,
int datalen)
{
/*
* What is the peer's name?
*/
int size;
struct unaligned_u16 *raw = data;