-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathppp.c
More file actions
2515 lines (2133 loc) · 61.1 KB
/
Copy pathppp.c
File metadata and controls
2515 lines (2133 loc) · 61.1 KB
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
// L2TPNS PPP Stuff
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include "l2tpns.h"
#include "constants.h"
#include "plugin.h"
#include "util.h"
#include "tbf.h"
#include "cluster.h"
extern tunnelt *tunnel;
extern bundlet *bundle;
extern fragmentationt *frag;
extern sessiont *session;
extern radiust *radius;
extern int tunfd;
extern char hostname[];
extern uint32_t eth_tx;
extern time_t time_now;
extern configt *config;
static int add_lcp_auth(uint8_t *b, int size, int authtype);
static bundleidt new_bundle(void);
static int epdiscmp(epdist, epdist);
static void setepdis(epdist *, epdist);
static void ipcp_open(sessionidt s, tunnelidt t);
static int first_session_in_bundle(sessionidt s)
{
bundleidt i;
for (i = 1; i < MAXBUNDLE; i++)
if (bundle[i].state != BUNDLEFREE)
if (epdiscmp(session[s].epdis,bundle[i].epdis) && !strcmp(session[s].user, bundle[i].user))
return 0;
return 1;
}
// Process PAP messages
void processpap(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l)
{
char user[MAXUSER];
char pass[MAXPASS];
uint16_t hl;
uint16_t r;
CSTAT(processpap);
LOG_HEX(5, "PAP", p, l);
if (l < 4)
{
LOG(1, s, t, "Short PAP %u bytes\n", l);
STAT(tunnel_rx_errors);
sessionshutdown(s, "Short PAP packet.", CDN_ADMIN_DISC, TERM_USER_ERROR);
return;
}
if ((hl = ntohs(*(uint16_t *) (p + 2))) > l)
{
LOG(1, s, t, "Length mismatch PAP %u/%u\n", hl, l);
STAT(tunnel_rx_errors);
sessionshutdown(s, "PAP length mismatch.", CDN_ADMIN_DISC, TERM_USER_ERROR);
return;
}
l = hl;
if (*p != 1)
{
LOG(1, s, t, "Unexpected PAP code %d\n", *p);
STAT(tunnel_rx_errors);
sessionshutdown(s, "Unexpected PAP code.", CDN_ADMIN_DISC, TERM_USER_ERROR);
return;
}
if (session[s].ppp.phase != Authenticate)
{
LOG(2, s, t, "PAP ignored in %s phase\n", ppp_phase(session[s].ppp.phase));
return;
}
{
uint8_t *b = p;
b += 4;
user[0] = pass[0] = 0;
if (*b && *b < sizeof(user))
{
memcpy(user, b + 1, *b);
user[*b] = 0;
b += 1 + *b;
if (*b && *b < sizeof(pass))
{
memcpy(pass, b + 1, *b);
pass[*b] = 0;
}
}
LOG(3, s, t, "PAP login %s/%s\n", user, pass);
}
if (session[s].ip || !(r = radiusnew(s)))
{
// respond now, either no RADIUS available or already authenticated
uint8_t b[MAXETHER];
uint8_t id = p[1];
uint8_t *p = makeppp(b, sizeof(b), 0, 0, s, t, PPPPAP, 0, 0, 0);
if (!p) return;
if (session[s].ip)
*p = 2; // ACK
else
*p = 3; // cant authorise
p[1] = id;
*(uint16_t *) (p + 2) = htons(5); // length
p[4] = 0; // no message
tunnelsend(b, 5 + (p - b), t); // send it
if (session[s].ip)
{
LOG(3, s, t, "Already an IP allocated: %s (%d)\n",
fmtaddr(htonl(session[s].ip), 0), session[s].ip_pool_index);
}
else
{
LOG(1, s, t, "No RADIUS session available to authenticate session...\n");
sessionshutdown(s, "No free RADIUS sessions.", CDN_UNAVAILABLE, TERM_SERVICE_UNAVAILABLE);
}
}
else
{
// Run PRE_AUTH plugins
struct param_pre_auth packet = { &tunnel[t], &session[s], strdup(user), strdup(pass), PPPPAP, 1 };
run_plugins(PLUGIN_PRE_AUTH, &packet);
if (!packet.continue_auth)
{
LOG(3, s, t, "A plugin rejected PRE_AUTH\n");
if (packet.username) free(packet.username);
if (packet.password) free(packet.password);
return;
}
strncpy(session[s].user, packet.username, sizeof(session[s].user) - 1);
strncpy(radius[r].pass, packet.password, sizeof(radius[r].pass) - 1);
free(packet.username);
free(packet.password);
radius[r].id = p[1];
LOG(3, s, t, "Sending login for %s/%s to RADIUS\n", user, pass);
if ((session[s].mrru) && (!first_session_in_bundle(s)))
radiussend(r, RADIUSJUSTAUTH);
else
radiussend(r, RADIUSAUTH);
}
}
// Process CHAP messages
void processchap(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l)
{
uint16_t r;
uint16_t hl;
CSTAT(processchap);
LOG_HEX(5, "CHAP", p, l);
if (l < 4)
{
LOG(1, s, t, "Short CHAP %u bytes\n", l);
STAT(tunnel_rx_errors);
sessionshutdown(s, "Short CHAP packet.", CDN_ADMIN_DISC, TERM_USER_ERROR);
return;
}
if ((hl = ntohs(*(uint16_t *) (p + 2))) > l)
{
LOG(1, s, t, "Length mismatch CHAP %u/%u\n", hl, l);
STAT(tunnel_rx_errors);
sessionshutdown(s, "CHAP length mismatch.", CDN_ADMIN_DISC, TERM_USER_ERROR);
return;
}
l = hl;
if (*p != 2)
{
LOG(1, s, t, "Unexpected CHAP response code %d\n", *p);
STAT(tunnel_rx_errors);
sessionshutdown(s, "CHAP length mismatch.", CDN_ADMIN_DISC, TERM_USER_ERROR);
return;
}
if (session[s].ppp.phase != Authenticate)
{
LOG(2, s, t, "CHAP ignored in %s phase\n", ppp_phase(session[s].ppp.phase));
return;
}
r = sess_local[s].radius;
if (!r)
{
LOG(3, s, t, "Unexpected CHAP message\n");
// Some modems (Netgear DM602, possibly others) persist in using CHAP even
// after ACKing our ConfigReq for PAP.
if (sess_local[s].lcp_authtype == AUTHPAP && config->radius_authtypes & AUTHCHAP)
{
sess_local[s].lcp_authtype = AUTHCHAP;
sendchap(s, t);
}
return;
}
if (p[1] != radius[r].id)
{
LOG(1, s, t, "Wrong CHAP response ID %d (should be %d) (%d)\n", p[1], radius[r].id, r);
STAT(tunnel_rx_errors);
sessionshutdown(s, "Unexpected CHAP response ID.", CDN_ADMIN_DISC, TERM_USER_ERROR);
return;
}
if (l < 5 || p[4] != 16)
{
LOG(1, s, t, "Bad CHAP response length %d\n", l < 5 ? -1 : p[4]);
STAT(tunnel_rx_errors);
sessionshutdown(s, "Bad CHAP response length.", CDN_ADMIN_DISC, TERM_USER_ERROR);
return;
}
l -= 5;
p += 5;
if (l < 16 || l - 16 >= sizeof(session[s].user))
{
LOG(1, s, t, "CHAP user too long %d\n", l - 16);
STAT(tunnel_rx_errors);
sessionshutdown(s, "CHAP username too long.", CDN_ADMIN_DISC, TERM_USER_ERROR);
return;
}
// Run PRE_AUTH plugins
{
struct param_pre_auth packet = { &tunnel[t], &session[s], NULL, NULL, PPPCHAP, 1 };
packet.password = calloc(17, 1);
memcpy(packet.password, p, 16);
p += 16;
l -= 16;
packet.username = calloc(l + 1, 1);
memcpy(packet.username, p, l);
run_plugins(PLUGIN_PRE_AUTH, &packet);
if (!packet.continue_auth)
{
LOG(3, s, t, "A plugin rejected PRE_AUTH\n");
if (packet.username) free(packet.username);
if (packet.password) free(packet.password);
return;
}
strncpy(session[s].user, packet.username, sizeof(session[s].user) - 1);
memcpy(radius[r].pass, packet.password, 16);
free(packet.username);
free(packet.password);
}
radius[r].chap = 1;
LOG(3, s, t, "CHAP login %s\n", session[s].user);
if ((session[s].mrru) && (!first_session_in_bundle(s)))
radiussend(r, RADIUSJUSTAUTH);
else
radiussend(r, RADIUSAUTH);
}
static void dumplcp(uint8_t *p, int l)
{
int x = l - 4;
uint8_t *o = (p + 4);
LOG_HEX(5, "PPP LCP Packet", p, l);
LOG(4, 0, 0, "PPP LCP Packet type %d (%s len %d)\n", *p, ppp_code((int)*p), ntohs( ((uint16_t *) p)[1]) );
LOG(4, 0, 0, "Length: %d\n", l);
if (*p != ConfigReq && *p != ConfigRej && *p != ConfigAck)
return;
while (x > 2)
{
int type = o[0];
int length = o[1];
if (length < 2)
{
LOG(4, 0, 0, " Option length is %d...\n", length);
break;
}
if (type == 0)
{
LOG(4, 0, 0, " Option type is 0...\n");
x -= length;
o += length;
continue;
}
switch (type)
{
case 1: // Maximum-Receive-Unit
if (length == 4)
LOG(4, 0, 0, " %s %d\n", ppp_lcp_option(type), ntohs(*(uint16_t *)(o + 2)));
else
LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type), length);
break;
case 2: // Async-Control-Character-Map
if (length == 6)
{
uint32_t asyncmap = ntohl(*(uint32_t *)(o + 2));
LOG(4, 0, 0, " %s %x\n", ppp_lcp_option(type), asyncmap);
}
else
LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type), length);
break;
case 3: // Authentication-Protocol
if (length == 4)
{
int proto = ntohs(*(uint16_t *)(o + 2));
LOG(4, 0, 0, " %s 0x%x (%s)\n", ppp_lcp_option(type), proto,
proto == PPPPAP ? "PAP" : "UNSUPPORTED");
}
else if (length == 5)
{
int proto = ntohs(*(uint16_t *)(o + 2));
int algo = *(o + 4);
LOG(4, 0, 0, " %s 0x%x 0x%x (%s)\n", ppp_lcp_option(type), proto, algo,
(proto == PPPCHAP && algo == 5) ? "CHAP MD5" : "UNSUPPORTED");
}
else
LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type), length);
break;
case 4: // Quality-Protocol
{
uint32_t qp = ntohl(*(uint32_t *)(o + 2));
LOG(4, 0, 0, " %s %x\n", ppp_lcp_option(type), qp);
}
break;
case 5: // Magic-Number
if (length == 6)
{
uint32_t magicno = ntohl(*(uint32_t *)(o + 2));
LOG(4, 0, 0, " %s %x\n", ppp_lcp_option(type), magicno);
}
else
LOG(4, 0, 0, " %s odd length %d\n", ppp_lcp_option(type), length);
break;
case 7: // Protocol-Field-Compression
case 8: // Address-And-Control-Field-Compression
LOG(4, 0, 0, " %s\n", ppp_lcp_option(type));
break;
default:
LOG(2, 0, 0, " Unknown PPP LCP Option type %d\n", type);
break;
}
x -= length;
o += length;
}
}
void lcp_open(sessionidt s, tunnelidt t)
{
// transition to Authentication or Network phase:
session[s].ppp.phase = sess_local[s].lcp_authtype ? Authenticate : Network;
LOG(3, s, t, "LCP: Opened, phase %s\n", ppp_phase(session[s].ppp.phase));
// LCP now Opened
change_state(s, lcp, Opened);
if (session[s].ppp.phase == Authenticate)
{
if (sess_local[s].lcp_authtype == AUTHCHAP)
sendchap(s, t);
}
else
{
if(session[s].bundle == 0 || bundle[session[s].bundle].num_of_links == 1)
{
// This-Layer-Up
sendipcp(s, t);
change_state(s, ipcp, RequestSent);
// move to passive state for IPv6 (if configured), CCP
if (config->ipv6_prefix.s6_addr[0])
change_state(s, ipv6cp, Stopped);
else
change_state(s, ipv6cp, Closed);
change_state(s, ccp, Stopped);
}
else
{
sessionidt first_ses = bundle[session[s].bundle].members[0];
LOG(3, s, t, "MPPP: Skipping IPCP negotiation for session:%d, first session of bundle is:%d\n",s,first_ses);
ipcp_open(s, t);
}
}
}
static void lcp_restart(sessionidt s)
{
session[s].ppp.phase = Establish;
// This-Layer-Down
change_state(s, ipcp, Dead);
change_state(s, ipv6cp, Dead);
change_state(s, ccp, Dead);
}
static uint8_t *ppp_conf_rej(sessionidt s, uint8_t *buf, size_t blen, uint16_t mtype,
uint8_t **response, uint8_t *queued, uint8_t *packet, uint8_t *option)
{
if (!*response || **response != ConfigRej)
{
queued = *response = makeppp(buf, blen, packet, 2, s, session[s].tunnel, mtype, 0, 0, 0);
if (!queued)
return 0;
*queued = ConfigRej;
queued += 4;
}
if ((queued - buf + option[1]) > blen)
{
LOG(2, s, session[s].tunnel, "PPP overflow for ConfigRej (proto %u, option %u).\n", mtype, *option);
return 0;
}
memcpy(queued, option, option[1]);
return queued + option[1];
}
static uint8_t *ppp_conf_nak(sessionidt s, uint8_t *buf, size_t blen, uint16_t mtype,
uint8_t **response, uint8_t *queued, uint8_t *packet, uint8_t *option,
uint8_t *value, size_t vlen)
{
int *nak_sent;
switch (mtype)
{
case PPPLCP: nak_sent = &sess_local[s].lcp.nak_sent; break;
case PPPIPCP: nak_sent = &sess_local[s].ipcp.nak_sent; break;
case PPPIPV6CP: nak_sent = &sess_local[s].ipv6cp.nak_sent; break;
default: return 0; // ?
}
if (*response && **response != ConfigNak)
{
if (*nak_sent < config->ppp_max_failure) // reject queued
return queued;
return ppp_conf_rej(s, buf, blen, mtype, response, 0, packet, option);
}
if (!*response)
{
if (*nak_sent >= config->ppp_max_failure)
return ppp_conf_rej(s, buf, blen, mtype, response, 0, packet, option);
queued = *response = makeppp(buf, blen, packet, 2, s, session[s].tunnel, mtype, 0, 0, 0);
if (!queued)
return 0;
(*nak_sent)++;
*queued = ConfigNak;
queued += 4;
}
if ((queued - buf + vlen + 2) > blen)
{
LOG(2, s, session[s].tunnel, "PPP overflow for ConfigNak (proto %u, option %u).\n", mtype, *option);
return 0;
}
*queued++ = *option;
*queued++ = vlen + 2;
memcpy(queued, value, vlen);
return queued + vlen;
}
static void ppp_code_rej(sessionidt s, tunnelidt t, uint16_t proto,
char *pname, uint8_t *p, uint16_t l, uint8_t *buf, size_t size)
{
uint8_t *q;
int mru = session[s].mru;
if (mru < MINMTU) mru = MINMTU;
if (mru > size) mru = size;
l += 4;
if (l > mru) l = mru;
q = makeppp(buf, size, 0, 0, s, t, proto, 0, 0, 0);
if (!q) return;
*q = CodeRej;
*(q + 1) = ++sess_local[s].lcp_ident;
*(uint16_t *)(q + 2) = htons(l);
memcpy(q + 4, p, l - 4);
LOG(2, s, t, "Unexpected %s code %s\n", pname, ppp_code(*p));
LOG(3, s, t, "%s: send %s\n", pname, ppp_code(*q));
if (config->debug > 3) dumplcp(q, l);
tunnelsend(buf, l + (q - buf), t);
}
// Process LCP messages
void processlcp(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l)
{
uint8_t b[MAXETHER];
uint8_t *q = NULL;
uint16_t hl;
CSTAT(processlcp);
LOG_HEX(5, "LCP", p, l);
if (l < 4)
{
LOG(1, s, t, "Short LCP %d bytes\n", l);
STAT(tunnel_rx_errors);
return ;
}
if ((hl = ntohs(*(uint16_t *) (p + 2))) > l)
{
LOG(1, s, t, "Length mismatch LCP %u/%u\n", hl, l);
STAT(tunnel_rx_errors);
return ;
}
l = hl;
if (session[s].die) // going down...
return;
LOG((*p == EchoReq || *p == EchoReply) ? 4 : 3, s, t,
"LCP: recv %s\n", ppp_code(*p));
if (config->debug > 3) dumplcp(p, l);
if (*p == ConfigAck)
{
int x = l - 4;
uint8_t *o = (p + 4);
int authtype = 0;
while (x > 2)
{
int type = o[0];
int length = o[1];
if (length == 0 || type == 0 || x < length) break;
switch (type)
{
case 3: // Authentication-Protocol
{
int proto = ntohs(*(uint16_t *)(o + 2));
if (proto == PPPPAP)
authtype = AUTHPAP;
else if (proto == PPPCHAP && *(o + 4) == 5)
authtype = AUTHCHAP;
}
break;
}
x -= length;
o += length;
}
if (!session[s].ip && authtype)
sess_local[s].lcp_authtype = authtype;
switch (session[s].ppp.lcp)
{
case RequestSent:
initialise_restart_count(s, lcp);
change_state(s, lcp, AckReceived);
break;
case AckReceived:
case Opened:
LOG(2, s, t, "LCP: ConfigAck in state %s? Sending ConfigReq\n", ppp_state(session[s].ppp.lcp));
if (session[s].ppp.lcp == Opened)
lcp_restart(s);
sendlcp(s, t);
change_state(s, lcp, RequestSent);
break;
case AckSent:
lcp_open(s, t);
break;
default:
LOG(2, s, t, "LCP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.lcp));
}
}
else if (*p == ConfigReq)
{
int x = l - 4;
uint8_t *o = (p + 4);
uint8_t *response = 0;
static uint8_t asyncmap[4] = { 0, 0, 0, 0 }; // all zero
static uint8_t authproto[5];
int changed = 0;
while (x > 2)
{
int type = o[0];
int length = o[1];
if (length == 0 || type == 0 || x < length) break;
switch (type)
{
case 1: // Maximum-Receive-Unit
{
uint16_t mru = ntohs(*(uint16_t *)(o + 2));
if (mru >= MINMTU)
{
session[s].mru = mru;
changed++;
break;
}
LOG(3, s, t, " Remote requesting MRU of %u. Rejecting.\n", mru);
mru = htons(MRU);
q = ppp_conf_nak(s, b, sizeof(b), PPPLCP, &response, q, p, o, (uint8_t *) &mru, sizeof(mru));
}
break;
case 2: // Async-Control-Character-Map
if (!ntohl(*(uint32_t *)(o + 2))) // all bits zero is OK
break;
LOG(3, s, t, " Remote requesting asyncmap. Rejecting.\n");
q = ppp_conf_nak(s, b, sizeof(b), PPPLCP, &response, q, p, o, asyncmap, sizeof(asyncmap));
break;
case 3: // Authentication-Protocol
{
int proto = ntohs(*(uint16_t *)(o + 2));
char proto_name[] = "0x0000";
int alen;
if (proto == PPPPAP)
{
if (config->radius_authtypes & AUTHPAP)
{
sess_local[s].lcp_authtype = AUTHPAP;
break;
}
strcpy(proto_name, "PAP");
}
else if (proto == PPPCHAP)
{
if (config->radius_authtypes & AUTHCHAP
&& *(o + 4) == 5) // MD5
{
sess_local[s].lcp_authtype = AUTHCHAP;
break;
}
strcpy(proto_name, "CHAP");
}
else
sprintf(proto_name, "%#4.4x", proto);
LOG(3, s, t, " Remote requesting %s authentication. Rejecting.\n", proto_name);
alen = add_lcp_auth(authproto, sizeof(authproto), config->radius_authprefer);
if (alen < 2) break; // paranoia
q = ppp_conf_nak(s, b, sizeof(b), PPPLCP, &response, q, p, o, authproto + 2, alen - 2);
if (q && *response == ConfigNak &&
config->radius_authtypes != config->radius_authprefer)
{
// alternate type
alen = add_lcp_auth(authproto, sizeof(authproto), config->radius_authtypes & ~config->radius_authprefer);
if (alen < 2) break;
q = ppp_conf_nak(s, b, sizeof(b), PPPLCP, &response, q, p, o, authproto + 2, alen - 2);
}
break;
}
break;
case 4: // Quality-Protocol
case 5: // Magic-Number
case 7: // Protocol-Field-Compression
case 8: // Address-And-Control-Field-Compression
break;
case 17: // Multilink Max-Receive-Reconstructed-Unit
{
uint16_t mrru = ntohs(*(uint16_t *)(o + 2));
session[s].mrru = mrru;
changed++;
LOG(3, s, t, " Received PPP LCP option MRRU: %d\n",mrru);
}
break;
case 18: // Multilink Short Sequence Number Header Format
{
session[s].mssf = 1;
changed++;
LOG(3, s, t, " Received PPP LCP option MSSN format\n");
}
break;
case 19: // Multilink Endpoint Discriminator
{
uint8_t epdis_class = o[2];
int addr;
session[s].epdis.addr_class = epdis_class;
session[s].epdis.length = length - 3;
if (session[s].epdis.length > 20)
{
LOG(1, s, t, "Error: received EndDis Address Length more than 20: %d\n", session[s].epdis.length);
session[s].epdis.length = 20;
}
for (addr = 0; addr < session[s].epdis.length; addr++)
session[s].epdis.address[addr] = o[3+addr];
changed++;
switch (epdis_class)
{
case LOCALADDR:
LOG(3, s, t, " Received PPP LCP option Multilink EndDis Local Address Class: %d\n",epdis_class);
break;
case IPADDR:
LOG(3, s, t, " Received PPP LCP option Multilink EndDis IP Address Class: %d\n",epdis_class);
break;
case IEEEMACADDR:
LOG(3, s, t, " Received PPP LCP option Multilink EndDis IEEE MAC Address Class: %d\n",epdis_class);
break;
case PPPMAGIC:
LOG(3, s, t, " Received PPP LCP option Multilink EndDis PPP Magic No Class: %d\n",epdis_class);
break;
case PSNDN:
LOG(3, s, t, " Received PPP LCP option Multilink EndDis PSND No Class: %d\n",epdis_class);
break;
default:
LOG(3, s, t, " Received PPP LCP option Multilink EndDis NULL Class %d\n",epdis_class);
}
}
break;
default: // Reject any unknown options
LOG(3, s, t, " Rejecting unknown PPP LCP option %d\n", type);
q = ppp_conf_rej(s, b, sizeof(b), PPPLCP, &response, q, p, o);
}
x -= length;
o += length;
}
if (changed)
cluster_send_session(s);
if (response)
{
l = q - response; // LCP packet length
*((uint16_t *) (response + 2)) = htons(l); // update header
}
else
{
// Send packet back as ConfigAck
response = makeppp(b, sizeof(b), p, l, s, t, PPPLCP, 0, 0, 0);
if (!response) return;
*response = ConfigAck;
}
switch (session[s].ppp.lcp)
{
case Closed:
response = makeppp(b, sizeof(b), p, 2, s, t, PPPLCP, 0, 0, 0);
if (!response) return;
*response = TerminateAck;
*((uint16_t *) (response + 2)) = htons(l = 4);
break;
case Stopped:
initialise_restart_count(s, lcp);
sendlcp(s, t);
if (*response == ConfigAck)
change_state(s, lcp, AckSent);
else
change_state(s, lcp, RequestSent);
break;
case RequestSent:
if (*response == ConfigAck)
change_state(s, lcp, AckSent);
break;
case AckReceived:
if (*response == ConfigAck)
lcp_open(s, t);
break;
case Opened:
lcp_restart(s);
sendlcp(s, t);
/* fallthrough */
case AckSent:
if (*response == ConfigAck)
change_state(s, lcp, AckSent);
else
change_state(s, lcp, RequestSent);
break;
default:
LOG(2, s, t, "LCP: ignoring %s in state %s\n", ppp_code(*p), ppp_state(session[s].ppp.lcp));
return;
}
LOG(3, s, t, "LCP: send %s\n", ppp_code(*response));
if (config->debug > 3) dumplcp(response, l);
tunnelsend(b, l + (response - b), t);
}
else if (*p == ConfigNak || *p == ConfigRej)
{
int x = l - 4;
uint8_t *o = (p + 4);
int authtype = -1;
while (x > 2)
{
int type = o[0];
int length = o[1];
if (length == 0 || type == 0 || x < length) break;
switch (type)
{
case 1: // Maximum-Receive-Unit
if (*p == ConfigNak)
{
if (length < 4) break;
sess_local[s].ppp_mru = ntohs(*(uint16_t *)(o + 2));
LOG(3, s, t, " Remote requested MRU of %u\n", sess_local[s].ppp_mru);
}
else
{
sess_local[s].ppp_mru = 0;
LOG(3, s, t, " Remote rejected MRU negotiation\n");
}
break;
case 3: // Authentication-Protocol
if (authtype > 0)
break;
if (*p == ConfigNak)
{
int proto;
if (length < 4) break;
proto = ntohs(*(uint16_t *)(o + 2));
if (proto == PPPPAP)
{
authtype = config->radius_authtypes & AUTHPAP;
LOG(3, s, t, " Remote requested PAP authentication...%sing\n",
authtype ? "accept" : "reject");
}
else if (proto == PPPCHAP && length > 4 && *(o + 4) == 5)
{
authtype = config->radius_authtypes & AUTHCHAP;
LOG(3, s, t, " Remote requested CHAP authentication...%sing\n",
authtype ? "accept" : "reject");
}
else
{
LOG(3, s, t, " Rejecting unsupported authentication %#4x\n",
proto);
}
}
else
{
LOG(2, s, t, "LCP: remote rejected auth negotiation\n");
authtype = 0; // shutdown
}
break;
case 5: // Magic-Number
session[s].magic = 0;
if (*p == ConfigNak)
{
if (length < 6) break;
session[s].magic = ntohl(*(uint32_t *)(o + 2));
}
if (session[s].magic)
LOG(3, s, t, " Remote requested magic-no %x\n", session[s].magic);
else
LOG(3, s, t, " Remote rejected magic-no\n");
cluster_send_session(s);
break;
case 17: // Multilink Max-Receive-Reconstructed-Unit
{
if (*p == ConfigNak)
{
sess_local[s].mp_mrru = ntohs(*(uint16_t *)(o + 2));
LOG(3, s, t, " Remote requested MRRU of %u\n", sess_local[s].mp_mrru);
}
else
{
sess_local[s].mp_mrru = 0;
LOG(3, s, t, " Remote rejected MRRU negotiation\n");
}
}
break;
case 18: // Multilink Short Sequence Number Header Format
{
if (*p == ConfigNak)
{
sess_local[s].mp_mssf = 0;
LOG(3, s, t, " Remote requested Naked mssf\n");
}
else
{
sess_local[s].mp_mssf = 0;
LOG(3, s, t, " Remote rejected mssf\n");
}
}
break;
case 19: // Multilink Endpoint Discriminator
{
if (*p == ConfigNak)
{
LOG(2, s, t, " Remote should not configNak Endpoint Dis!\n");
}
else
{
sess_local[s].mp_epdis = 0;
LOG(3, s, t, " Remote rejected Endpoint Discriminator\n");
}
}
break;
default:
LOG(2, s, t, "LCP: remote sent %s for type %u?\n", ppp_code(*p), type);
sessionshutdown(s, "Unable to negotiate LCP.", CDN_ADMIN_DISC, TERM_USER_ERROR);
return;
}
x -= length;
o += length;
}
if (!authtype)
{
sessionshutdown(s, "Unsupported authentication.", CDN_ADMIN_DISC, TERM_USER_ERROR);
return;
}
if (authtype > 0)
sess_local[s].lcp_authtype = authtype;
switch (session[s].ppp.lcp)
{
case Closed:
case Stopped:
{
uint8_t *response = makeppp(b, sizeof(b), p, 2, s, t, PPPLCP, 0, 0, 0);
if (!response) return;
*response = TerminateAck;
*((uint16_t *) (response + 2)) = htons(l = 4);
LOG(3, s, t, "LCP: send %s\n", ppp_code(*response));
if (config->debug > 3) dumplcp(response, l);
tunnelsend(b, l + (response - b), t);
}
break;
case RequestSent:
case AckSent:
initialise_restart_count(s, lcp);
sendlcp(s, t);
break;
case AckReceived:
LOG(2, s, t, "LCP: ConfigNak in state %s? Sending ConfigReq\n", ppp_state(session[s].ppp.lcp));