-
Notifications
You must be signed in to change notification settings - Fork 2
/
l2tp_transport.c
1438 lines (1240 loc) · 45.2 KB
/
l2tp_transport.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
*
*****************************************************************************/
/*
* All data passing through an L2TP tunnel goes through a dedicated
* socket. Each tunnel has one socket.
*
* Userspace l2tpd receives only control packets on its UDP sockets
* and should send only control packets. Control frames are passed
* through a reliable transport (implemented in this module) which
* retransmits frames until they are acknowledged or time out..
*/
#include "usl.h"
#include "l2tp_private.h"
#ifndef aligned_u64
/* should be defined in sys/types.h */
#define aligned_u64 unsigned long long __attribute__((aligned(8)))
#endif
#include <sys/ioctl.h>
#include <linux/if.h>
#include <linux/if_ether.h>
#include <linux/if_pppox.h>
#include <linux/ppp_defs.h>
#include <linux/if_ppp.h>
#include <linux/if_pppol2tp.h>
/*****************************************************************************
* Transport contexts
*****************************************************************************/
struct l2tp_xprt_params {
uint16_t max_retries;
uint16_t rx_window_size; /* max length of receive pipe */
uint16_t tx_window_size; /* max unack'd transmits */
uint16_t retry_timeout;
uint16_t hello_timeout;
struct in_addr our_addr;
struct in_addr peer_addr;
};
struct l2tp_xprt_stats {
uint32_t retransmits;
uint32_t oos_control_packets;
uint32_t oos_control_discards;
uint32_t tx_zlbs;
uint32_t tx_zlb_fails;
uint32_t rx_zlbs;
uint32_t data_pkt_discards;
uint32_t duplicate_pkt_discards;
uint32_t rx_hellos;
uint32_t tx_hellos;
uint32_t tx_hello_fails;
uint64_t rx_packets;
uint64_t rx_bytes;
uint64_t tx_packets;
uint64_t tx_bytes;
};
struct l2tp_xprt {
uint16_t tunnel_id;
uint16_t peer_tunnel_id;
uint16_t ns; /* next ns to use for transmit control packets */
uint16_t nr; /* nr expected to be received next (control) */
uint16_t peer_ns;
uint16_t peer_nr;
int fd;
int kernel_fd;
struct l2tp_xprt_params params;
struct l2tp_xprt_stats stats;
void *retry_timer;
void *hello_timer;
void *zlb_timer;
int retry_duration; /* exponentially increases */
uint16_t cwnd; /* slow start congestion window */
uint16_t ssthresh; /* slow start threshold */
uint16_t congpkt_acc; /* congestion packet accumulator */
uint16_t is_congested:1; /* 0=>slow start, 1=>congestion avoidance */
uint16_t is_lac:1; /* are we the LAC? */
uint16_t is_closing:1;
uint16_t is_tx_stalled:1;
uint16_t has_acked:1;
struct usl_list_head ackq; /* queue of transmitted pkts waiting acks */
struct usl_list_head txq; /* queue of pkts waiting transmit slot */
struct usl_list_head rxq; /* in-sequence queue of received pkts waiting to go up to protocol */
uint16_t nr_next; /* nr to deliver next */
uint16_t nr_last_dequeued; /* nr last delivered */
int is_reordering; /* are pkts waiting in rxq? */
struct usl_list_head list; /* the list we're on */
void *tunnel;
struct l2tp_peer *peer;
unsigned long data_rx_packets; /* to check if data packets rx'd since last hello */
};
/* Local tick count for low resolution packet resequencing timers */
static unsigned int l2tp_xprt_jiffies;
#ifdef DEBUG
/* To check for seq number wrap test cases */
static int l2tp_xprt_nr_wraps = 0;
static int l2tp_xprt_ns_wraps = 0;
#endif
static void l2tp_xprt_retry_timeout(void *data);
static void l2tp_xprt_zlb_ack_send(struct l2tp_xprt *xprt, uint16_t nr);
static void l2tp_xprt_hello_timeout(void *data);
static void l2tp_xprt_send_hello(struct l2tp_xprt *xprt);
static void l2tp_xprt_process_txq(struct l2tp_xprt *xprt);
static void l2tp_xprt_zlb_timeout(void *data);
static int l2tp_xprt_sendmsg(struct l2tp_xprt *xprt, struct l2tp_packet *pkt);
static USL_LIST_HEAD(l2tp_xprt_list); /* active tunnels */
static USL_LIST_HEAD(l2tp_xprt_delete_list); /* deleting tunnels */
/*****************************************************************************
* Implements the slow start algorithm, defined in RFC2661 Appendix A.
*****************************************************************************/
static int l2tp_xprt_is_tx_window_open(struct l2tp_xprt *xprt, uint16_t ns)
{
uint16_t tws; /* current transmit window used */
if (ns >= xprt->peer_nr) {
tws = ns - xprt->peer_nr;
} else {
tws = (0xffff - xprt->peer_nr) + ns;
}
L2TP_DEBUG(L2TP_XPRT, "%s: tunl %hu: ns=%hu, ns/nr=%hu/%hu peer=%hu/%hu tws=%hu", __func__,
xprt->tunnel_id, ns, xprt->ns, xprt->nr, xprt->peer_ns, xprt->peer_nr, tws);
if (xprt->is_congested == 0) {
/* slow start phase */
if (tws < xprt->cwnd) {
return 1;
}
} else {
/* congested phase */
if (tws < xprt->params.tx_window_size) {
return 1;
}
}
l2tp_tunnel_log(xprt->tunnel, L2TP_XPRT, LOG_DEBUG, "XPRT: tunl %hu: tx window closed", xprt->tunnel_id);
return 0;
}
static void l2tp_xprt_tx_window_ack(struct l2tp_xprt *xprt)
{
L2TP_DEBUG(L2TP_XPRT, "%s: tunl %hu", __func__, xprt->tunnel_id);
/* Since this is called for all packets ack'd, including ZLBs,
* we do not update ns/nr here.
*/
if (xprt->is_congested == 0) {
/* slow start phase */
L2TP_DEBUG(L2TP_XPRT, "%s: tid=%d slow start: cwnd=%hu ssthresh=%hu acc=%hu", __func__,
xprt->tunnel_id, xprt->cwnd, xprt->ssthresh, xprt->congpkt_acc);
xprt->cwnd++;
if (xprt->cwnd >= xprt->ssthresh) {
xprt->congpkt_acc = 0;
xprt->is_congested = 1;
}
} else {
/* congested phase */
L2TP_DEBUG(L2TP_XPRT, "%s: tid=%d congested: cwnd=%hu acc=%hu", __func__,
xprt->tunnel_id, xprt->cwnd, xprt->congpkt_acc);
xprt->congpkt_acc++;
if (xprt->congpkt_acc >= xprt->cwnd) {
xprt->cwnd++;
if (xprt->cwnd > xprt->params.tx_window_size) {
xprt->cwnd = xprt->params.tx_window_size;
}
xprt->congpkt_acc = 0;
}
}
}
static void l2tp_xprt_tx_window_retransmit_ind(struct l2tp_xprt *xprt)
{
L2TP_DEBUG(L2TP_XPRT, "%s: tunl %hu: cwnd=%hu ssthresh=%hu", __func__,
xprt->tunnel_id, xprt->cwnd, xprt->ssthresh);
if (xprt->ssthresh < 2) {
xprt->ssthresh = 1;
} else {
xprt->ssthresh = xprt->cwnd / 2;
}
xprt->cwnd = 1;
xprt->is_congested = 0; /* re-enter slow-start phase */
}
/* Determine whether the sequence number nr is "less than or equal" as defined
* by RFC2661, section 5.8. Return 1 if less than or equal, else 0.
*/
static inline int l2tp_xprt_is_less_than_equal(struct l2tp_xprt *xprt, uint16_t nr1, uint16_t nr2)
{
uint16_t low = nr1;
uint16_t mid = nr2;
uint16_t high = nr1 + 0x8000;
L2TP_DEBUG(L2TP_XPRT, "%s: tunl %hu: n1=%hu n2=%hu", __func__,
l2tp_tunnel_id(xprt->tunnel), nr1, nr2);
if (low < high) {
if ((low <= mid) && (mid < high)) {
return 1;
} else {
return 0;
}
} else if ((low <= mid) || (mid < high)) {
return 1;
}
return 0;
}
/******************************************************************************
* In-sequence delivery to protocol.
* If packets are received out-of-sequence, they are held in xprt->rxq
* for resequencing.
*****************************************************************************/
static void l2tp_xprt_rxq_dequeue(struct l2tp_xprt *xprt, struct l2tp_packet *pkt)
{
L2TP_DEBUG(L2TP_XPRT, "%s: tunl %hu: pkt %hu, nr_next=%hu", __func__, xprt->tunnel_id, pkt->ns, xprt->nr_next);
usl_list_del_init(&pkt->list);
if (l2tp_xprt_is_less_than_equal(xprt, xprt->nr_last_dequeued + 1, pkt->ns)) {
xprt->nr_last_dequeued = pkt->ns;
L2TP_DEBUG(L2TP_XPRT, "%s: tunl %hu: update nr_last_dequeued to %hu", __func__, xprt->tunnel_id, xprt->nr_last_dequeued);
}
if (l2tp_xprt_is_less_than_equal(xprt, xprt->nr_next, pkt->ns)) {
L2TP_DEBUG(L2TP_XPRT, "%s: tunl %hu: update nr_next from %hu to %hu", __func__, xprt->tunnel_id, xprt->nr_next, pkt->ns + 1);
xprt->nr_next = pkt->ns + 1;
}
}
static void l2tp_xprt_deliver_up(struct l2tp_xprt *xprt, struct l2tp_packet *pkt)
{
struct sockaddr_in const *peer_addr;
L2TP_DEBUG(L2TP_XPRT, "%s: tunl %hu", __func__, xprt->tunnel_id);
if (pkt->xprt != xprt) {
L2TP_DEBUG(L2TP_XPRT, "tunl %hu: tunnel and packet inconsistent: %p/%p",
xprt->tunnel_id, pkt->xprt, xprt);
goto out;
}
peer_addr = l2tp_tunnel_get_peer_addr(xprt->tunnel);
l2tp_tunnel_log(xprt->tunnel, L2TP_DATA, LOG_DEBUG, "DATA: RX: tunl %hu/%hu: rcv %d bytes from peer %s, packet ns/nr %hu/%hu type %d",
xprt->tunnel_id, pkt->session_id, pkt->total_len, inet_ntoa(peer_addr->sin_addr), pkt->ns, pkt->nr, pkt->msg_type);
/* Tunnel nr state is updated when packets are delivered in-sequence to protocol. */
l2tp_tunnel_log(xprt->tunnel, L2TP_XPRT, LOG_DEBUG, "XPRT: tunl %hu: update nr from %hu to %hu",
xprt->tunnel_id, xprt->nr, pkt->ns + 1);
xprt->nr = pkt->ns + 1;
#ifdef DEBUG
if (xprt->nr == 0) {
l2tp_xprt_nr_wraps++;
}
#endif /* DEBUG */
l2tp_tunnel_recv(xprt->tunnel, xprt->tunnel_id, pkt->session_id, pkt);
out:
l2tp_pkt_free(pkt);
}
/* Deliver pkt in order of nr. The packet is first queued in rxq,
* which ensures in-order delivery of management frames to the
* protocol as required by RFC2661, section 5.8.
*/
static void l2tp_xprt_deliver_to_protocol(struct l2tp_xprt *xprt, struct l2tp_packet *pkt)
{
struct usl_list_head *walk;
struct usl_list_head *tmp;
struct l2tp_packet *q_pkt;
pkt->expires_at = l2tp_xprt_jiffies + USL_TIMER_HZ - 1; /* hold for approx 1 second */
L2TP_DEBUG(L2TP_XPRT, "%s: tunl %hu: ns=%hu, nr_next=%hu, expires=%u", __func__, xprt->tunnel_id, pkt->ns, xprt->nr_next, pkt->expires_at);
usl_list_for_each(walk, tmp, &xprt->rxq) {
q_pkt = usl_list_entry(walk, struct l2tp_packet, list);
if (q_pkt->ns > pkt->ns) {
L2TP_DEBUG(L2TP_XPRT, "%s: tunl %hu: pkt %hu, inserted before %hu", __func__, xprt->tunnel_id, pkt->ns, q_pkt->ns);
xprt->stats.oos_control_packets++;
usl_list_add(&pkt->list, q_pkt->list.prev);
goto dequeue;
}
}
usl_list_add(&pkt->list, &xprt->rxq);
dequeue:
/* If the pkt at the head of the queue has the nr that we
* expect to send up next, dequeue it and any other
* in-sequence packets behind it.
*/
usl_list_for_each(walk, tmp, &xprt->rxq) {
q_pkt = usl_list_entry(walk, struct l2tp_packet, list);
L2TP_DEBUG(L2TP_XPRT, "%s: tunl %hu: looking at pkt %hu", __func__, xprt->tunnel_id, q_pkt->ns);
if (q_pkt->ns == xprt->nr_next) {
l2tp_xprt_rxq_dequeue(xprt, q_pkt);
l2tp_xprt_deliver_up(xprt, q_pkt);
} else {
l2tp_tunnel_log(xprt->tunnel, L2TP_XPRT, LOG_DEBUG, "tunl %hu: holding out-of-sequence pkt %hu len=%d/%d, waiting for %hu, last_dequeud=%hu",
xprt->tunnel_id, q_pkt->ns, q_pkt->total_len, q_pkt->avp_len, xprt->nr_next, xprt->nr_last_dequeued);
break;
}
}
/* if there are packets waiting, enable reorder poll */
if (!usl_list_empty(&xprt->rxq)) {
xprt->is_reordering = 1;
} else {
xprt->is_reordering = 0;
}
}
/* Come here on every timer tick if the transport's is_reordering flag
* is set, i.e. packets are waiting in rxq for out-of-sequence
* packets to release them.
*/
static void l2tp_xprt_rxq_dequeue_poll(struct l2tp_xprt *xprt)
{
struct usl_list_head *walk;
struct usl_list_head *tmp;
struct l2tp_packet *q_pkt;
/* dequeue packets that are in sequence */
usl_list_for_each(walk, tmp, &xprt->rxq) {
q_pkt = usl_list_entry(walk, struct l2tp_packet, list);
if (q_pkt->ns == xprt->nr_next) {
l2tp_xprt_rxq_dequeue(xprt, q_pkt);
l2tp_xprt_deliver_up(xprt, q_pkt);
} else {
L2TP_DEBUG(L2TP_XPRT, "%s: tunl %hu: looking at pkt %hu, nr_next=%hu", __func__, xprt->tunnel_id, q_pkt->ns, xprt->nr_next);
break;
}
}
/* check for expired packets */
usl_list_for_each(walk, tmp, &xprt->rxq) {
q_pkt = usl_list_entry(walk, struct l2tp_packet, list);
if (q_pkt->expires_at < l2tp_xprt_jiffies) {
l2tp_tunnel_log(xprt->tunnel, L2TP_XPRT, LOG_DEBUG, "tunl %hu: out-of-sequence pkt %hu dropped, expecting %hu", xprt->tunnel_id, q_pkt->ns, xprt->nr_next);
l2tp_xprt_rxq_dequeue(xprt, q_pkt);
l2tp_pkt_free(q_pkt);
xprt->stats.oos_control_discards++;
}
}
/* if reorder queue now empty, update state */
if (usl_list_empty(&xprt->rxq)) {
L2TP_DEBUG(L2TP_XPRT, "%s: tunl %hu: queue empty when nr_next=%hu", __func__, xprt->tunnel_id, xprt->nr_next);
if (l2tp_xprt_is_less_than_equal(xprt, xprt->nr_next, xprt->nr_last_dequeued)) {
L2TP_DEBUG(L2TP_XPRT, "%s: tunl %hu: update nr_next from %hu to %hu", __func__, xprt->tunnel_id, xprt->nr_next, xprt->nr_last_dequeued + 1);
xprt->nr_next = xprt->nr_last_dequeued + 1;
}
xprt->is_reordering = 0;
}
}
/*****************************************************************************
* Receive handling
*****************************************************************************/
/* All packets with ns <= nr have been acked. Remove them from the
* ack-pending list.
*/
static void l2tp_xprt_flush_ack_list(struct l2tp_xprt *xprt, uint16_t nr)
{
struct l2tp_packet *pkt;
L2TP_DEBUG(L2TP_XPRT, "%s: tunl %hu: nr=%hu", __func__, xprt->tunnel_id, nr);
/* Walk the ack queue, flushing all entries with nr "less than or equal to"
* the supplied nr.
*/
for (;;) {
pkt = l2tp_pkt_peek(&xprt->ackq);
if (pkt == NULL) {
break;
}
L2TP_DEBUG(L2TP_XPRT, "%s: tunl %hu: looking at pkt with ns/nr=%hu/%hu", __func__,
xprt->tunnel_id, pkt->ns, pkt->nr);
if (l2tp_xprt_is_less_than_equal(xprt, pkt->ns + 1, nr)) {
l2tp_tunnel_log(xprt->tunnel, L2TP_XPRT, LOG_DEBUG, "XPRT: tunl %hu: pkt %hu/%hu is acked by nr %hu",
xprt->tunnel_id, pkt->ns, pkt->nr, nr);
xprt->has_acked = 1;
l2tp_pkt_unlink(pkt);
l2tp_pkt_free(pkt);
/* RFC2661: indicate ack to slow start algorithm */
l2tp_xprt_tx_window_ack(xprt);
} else {
break;
}
}
}
/* We come here when a valid HELLO message is received on a tunnel
* See l2tp_tunnel_recv().
*/
void l2tp_xprt_hello_rcvd(struct l2tp_xprt *xprt)
{
xprt->stats.rx_hellos++;
}
/* Process received frames. We don't expect data frames here.
* We allocate an l2tp_packet data structure for the packet
* and pass it up.
*/
int l2tp_xprt_recv(struct l2tp_xprt *xprt, struct l2tp_packet *pkt)
{
if (xprt == NULL) {
goto drop;
}
L2TP_DEBUG(L2TP_XPRT, "%s: %hu: pktlen=%d", __func__, l2tp_tunnel_id(xprt->tunnel), pkt->total_len);
pkt->xprt = xprt;
xprt->stats.rx_packets++;
xprt->stats.rx_bytes += pkt->total_len;
l2tp_tunnel_log(xprt->tunnel, L2TP_XPRT, LOG_DEBUG, "XPRT: RX: tunl %hu/%hu: len=%d ns/nr=%hu/%hu, our ns/nr=%hu/%hu, peer ns/nr=%hu/%hu",
xprt->tunnel_id, pkt->session_id, pkt->total_len, pkt->ns, pkt->nr, xprt->ns, xprt->nr, xprt->peer_ns, xprt->peer_nr);
if (pkt->avp_len > 0) {
/* Check ns is not "less than or equal to" the last received nr.
* If so, we treat the packet as a duplicate. See RFC2661, section 5.8.
*/
if (l2tp_xprt_is_less_than_equal(xprt, pkt->ns + 1, xprt->nr)) {
l2tp_tunnel_log(xprt->tunnel, L2TP_XPRT, LOG_DEBUG,
"XPRT: tunl %hu: duplicate packet ns/nr=%hu/%hu, our ns/nr=%hu/%hu",
xprt->tunnel_id, pkt->ns, pkt->nr, xprt->ns, xprt->nr);
l2tp_xprt_zlb_ack_send(xprt, pkt->ns + 1);
xprt->stats.duplicate_pkt_discards++;
goto drop;
}
} else {
l2tp_tunnel_log(xprt->tunnel, L2TP_XPRT, LOG_DEBUG, "XPRT: tunl %hu: zlb ack received: ns/nr=%hu/%hu",
xprt->tunnel_id, pkt->ns, pkt->nr);
xprt->stats.rx_zlbs++;
}
/* Take care not to move the peer ns/nr backwards */
if (l2tp_xprt_is_less_than_equal(xprt, xprt->peer_ns, pkt->ns)) {
xprt->peer_ns = pkt->ns;
}
if (l2tp_xprt_is_less_than_equal(xprt, xprt->peer_nr, pkt->nr)) {
xprt->peer_nr = pkt->nr;
}
l2tp_tunnel_log(xprt->tunnel, L2TP_XPRT, LOG_DEBUG,
"XPRT: tunl %hu: peer ns/nr is %hu/%hu", xprt->tunnel_id, xprt->peer_ns, xprt->peer_nr);
/* Restart the hello timer. Since Hello messages are used to
* query whether the peer is still operational, there is no
* need to send Hellos if we are successfully receiving frames
* from him. So we restart the hello timer when a good (seq
* numbers ok) control frame is received. Thus, this timer
* expires when the control channel is idle.
*/
if ((!xprt->is_closing) && (xprt->hello_timer != NULL)) {
usl_timer_restart(xprt->hello_timer);
}
/* RFC2661: nr is used to acknowledge messages from the peer. We can flush
* all entries in our ack-pending queue that have ns <= nr.
*/
l2tp_xprt_flush_ack_list(xprt, xprt->peer_nr);
/* Queue the packet on the tunnel's receive queue to enforce
* in-order delivery to userspace. If the nr isn't in sequence,
* packets wait in the queue until all preceding nr's have
* been received, at which point they are pulled from the
* queue and delivered upwards. Normal case for in-order
* delivery is that the packet is immediately pulled from the
* queue and delivered upwards.
*
* The tunnel xprt->nr is only updated when packets are delivered
* in-sequence to the upper protocol.
*/
if (pkt->avp_len > 0) {
l2tp_xprt_deliver_to_protocol(xprt, pkt);
/* If the txq is empty, it means we don't have any packets queued to
* the peer. Since we're here because we've received a packet from our peer,
* start the ZLB timer which will send a ZLB ack if/when it expires.
*/
if (l2tp_pkt_queue_empty(&xprt->txq) && (!usl_timer_is_running(xprt->zlb_timer))) {
L2TP_DEBUG(L2TP_XPRT, "%s: tunl %hu: start zlb timer", __func__, xprt->tunnel_id);
usl_timer_restart(xprt->zlb_timer);
}
} else {
l2tp_pkt_free(pkt);
}
out:
/* We received a packet so now try to make progress on our txq */
l2tp_xprt_process_txq(xprt);
return 0;
drop:
l2tp_pkt_free(pkt);
goto out;
}
/*****************************************************************************
* Timers
*****************************************************************************/
static void l2tp_xprt_retry_timeout(void *data)
{
struct l2tp_xprt *xprt = data;
struct l2tp_packet *pkt;
struct usl_list_head *walk;
struct usl_list_head *tmp;
int result;
L2TP_DEBUG(L2TP_XPRT, "%s: tunl %hu: has_acked=%d period=%d, timer=%p", __func__, xprt->tunnel_id,
xprt->has_acked ? 1 : 0, xprt->retry_duration, xprt->retry_timer);
/* Stop the retry timer if both ackq & txq are empty. */
pkt = l2tp_pkt_peek(&xprt->ackq);
if (pkt == NULL) {
L2TP_DEBUG(L2TP_XPRT, "%s: tunl %hu: ackq empty", __func__, xprt->tunnel_id);
if (l2tp_pkt_queue_empty(&xprt->txq)) {
L2TP_DEBUG(L2TP_XPRT, "%s: tunl %hu: txq empty", __func__, xprt->tunnel_id);
usl_timer_stop(xprt->retry_timer);
}
if (xprt->retry_duration != xprt->params.retry_timeout) {
xprt->retry_duration = xprt->params.retry_timeout;
L2TP_DEBUG(L2TP_XPRT, "%s: tunl %hu: reset retry interval", __func__,
xprt->tunnel_id);
}
xprt->has_acked = 0;
goto out;
} else {
L2TP_DEBUG(L2TP_XPRT, "%s: tunl %hu: ackq has pkt ns/nr %hu/%hu", __func__,
xprt->tunnel_id, pkt->ns, pkt->nr);
}
/* Check if packets have been acked since the last timer
* expiry. If so, don't try to retransmit any from the ackq.
*/
if (xprt->has_acked) {
xprt->has_acked = 0;
if (xprt->retry_duration != xprt->params.retry_timeout) {
xprt->retry_duration = xprt->params.retry_timeout;
L2TP_DEBUG(L2TP_XPRT, "%s: tunl %hu: reset retry interval", __func__,
xprt->tunnel_id);
}
goto out;
}
/* Exponentially increase the retry timer until it reaches a maximum 8 seconds */
if (xprt->retry_duration < 8) {
xprt->retry_duration <<= 1;
l2tp_tunnel_log(xprt->tunnel, L2TP_XPRT, LOG_DEBUG, "XPRT: tunl %hu: set retry interval to %d",
xprt->tunnel_id, xprt->retry_duration);
}
/* RFC2661: indicate retransmission to slow start algororithm */
l2tp_xprt_tx_window_retransmit_ind(xprt);
/* Retransmit all packets in the ack queue.
* Don't retransmit the packet if it has only just been put in the ackq.
*/
usl_list_for_each(walk, tmp, (struct usl_list_head *) &xprt->ackq) {
pkt = usl_list_entry(walk, struct l2tp_packet, list);
pkt->requeue_count++;
if (pkt->requeue_count == 0) {
break;
}
xprt->stats.retransmits++;
l2tp_stats.total_retransmitted_control_frames++;
if (pkt->requeue_count > xprt->params.max_retries) {
goto too_many_retries;
}
pkt->nr = xprt->nr;
l2tp_xprt_sendmsg(xprt, pkt);
}
out:
l2tp_xprt_process_txq(xprt);
usl_timer_interval_set(xprt->retry_timer, USL_TIMER_TICKS(xprt->retry_duration));
return;
too_many_retries:
/* If we've reached the retry count, give up. Purge the ack-pending
* and tx-pending queues, stop the timer and move the tunnel into
* a delete-pending list.
*/
l2tp_tunnel_log(xprt->tunnel, L2TP_XPRT, LOG_DEBUG,
"XPRT: tunl %hu: retry failure", xprt->tunnel_id);
usl_list_del_init(&xprt->list);
usl_list_add_tail(&xprt->list, &l2tp_xprt_delete_list);
xprt->retry_duration = xprt->params.retry_timeout;
usl_timer_stop(xprt->retry_timer);
/* tell that tunnel should be cleaned up */
result = l2tp_tunnel_queue_event(xprt->tunnel_id, L2TP_CCE_EVENT_XPRT_DOWN);
if (result < 0) {
L2TP_DEBUG(L2TP_XPRT, "Failed to send internal event for tunnel %d",
xprt->tunnel_id);
l2tp_stats.event_queue_full_errors++;
}
}
/* Called when hello timer expires.
* The timer is reset when a control message is sent or received so we should
* only get here if the control channel is idle. We send a Hello only if our
* transmit queue is empty and no data packets have been received on the tunnel
* since last time.
*/
static void l2tp_xprt_hello_timeout(void *data)
{
struct l2tp_api_tunnel_stats stats;
struct l2tp_xprt *xprt = data;
int txq_empty = l2tp_pkt_queue_empty(&xprt->txq);
L2TP_DEBUG(L2TP_XPRT, "%s: tunl %hu: hello timeout, txq_empty=%d, timer=%p", __func__,
xprt->tunnel_id, txq_empty, xprt->hello_timer);
if ((xprt->params.hello_timeout != 0) && (txq_empty)) {
if (l2tp_xprt_kernel_get(xprt, &stats) < 0) {
stats.data_rx_packets = 0;
}
if ((stats.data_rx_packets == 0) ||
(stats.data_rx_packets == xprt->data_rx_packets)) {
l2tp_xprt_send_hello(xprt);
}
xprt->data_rx_packets = stats.data_rx_packets;
} else {
L2TP_DEBUG(L2TP_XPRT, "%s: tunl %hu: hello timeout, rx_pkts=%lu", __func__,
xprt->tunnel_id, xprt->data_rx_packets);
}
}
/* Timer adjust function for the hello timer.
* This is used to add some randomness to the periodic hello interval.
*/
static int l2tp_xprt_hello_timer_adjust(int start_interval, int current_interval)
{
int adjust = 0;
if (start_interval > 4) {
#if USL_TIMER_HZ != 4
#error Fix code here to match new USL_TIMER_HZ value
#endif
adjust = l2tp_random(-3, 3);
L2TP_DEBUG(L2TP_FUNC, "%s: adjust=%d interval=%d", __func__, adjust,
start_interval + adjust);
}
return start_interval + adjust;
}
/* The ZLB timer expires if we receive a message and we don't send a
* message back to the peer for a period.
*/
static void l2tp_xprt_zlb_timeout(void *data)
{
struct l2tp_xprt *xprt = data;
int txq_empty = l2tp_pkt_queue_empty(&xprt->txq);
L2TP_DEBUG(L2TP_FUNC, "%s: tunl %hu: ns/nr=%hu/%hu, txq empty=%d", __func__,
xprt->tunnel_id, xprt->ns, xprt->nr, txq_empty);
if (txq_empty) {
l2tp_xprt_zlb_ack_send(xprt, xprt->nr);
}
}
/* Come here on every timer tick. We poll each transport regularly to try
* to make progress on any stalled transmits.
*/
static void l2tp_xprt_tick(void)
{
struct usl_list_head *tmp;
struct usl_list_head *walk;
struct l2tp_xprt *xprt;
l2tp_xprt_jiffies++;
usl_list_for_each(walk, tmp, &l2tp_xprt_list) {
xprt = usl_list_entry(walk, struct l2tp_xprt, list);
if (!xprt->is_tx_stalled) {
l2tp_xprt_process_txq(xprt);
}
if (xprt->is_reordering) {
l2tp_xprt_rxq_dequeue_poll(xprt);
}
}
}
/*****************************************************************************
* Transmit code
*****************************************************************************/
/* A ZLB is an L2TP control frame with 0 length (no AVPs).
*/
static void l2tp_xprt_zlb_ack_send(struct l2tp_xprt *xprt, uint16_t nr)
{
struct l2tp_packet *pkt;
void *l2tph;
int l2tph_len;
if (xprt->peer_tunnel_id == 0) {
return;
}
pkt = l2tp_pkt_alloc(1);
if (pkt == NULL) {
goto nomem_pkt;
}
if (l2tp_net_build_header(&l2tph, &l2tph_len, xprt->ns, nr, xprt->peer_tunnel_id, 0) < 0) {
goto nomem_lh;
}
l2tp_tunnel_log(xprt->tunnel, L2TP_XPRT, LOG_DEBUG, "XPRT: tunl %hu: send zlb ack, ns/nr=%hu/%hu",
xprt->tunnel_id, xprt->ns, nr);
pkt->iov[0].iov_base = l2tph;
pkt->iov[0].iov_len = l2tph_len;
pkt->total_len = l2tph_len;
pkt->ns = xprt->ns;
pkt->nr = nr;
pkt->tunnel_id = xprt->tunnel_id;
pkt->session_id = 0;
(void) l2tp_xprt_sendmsg(xprt, pkt);
l2tp_pkt_free(pkt);
return;
nomem_lh:
l2tp_pkt_free(pkt);
nomem_pkt:
xprt->stats.tx_zlb_fails++;
}
/* A HELLO is sent to the peer after a period of inactivity as a tunnel keepalive.
*/
static void l2tp_xprt_send_hello(struct l2tp_xprt *xprt)
{
int result;
if (xprt->peer_tunnel_id == 0) {
return;
}
result = l2tp_tunnel_send_hello(xprt->tunnel);
if (result < 0) {
xprt->stats.tx_hello_fails++;
} else {
xprt->stats.tx_hellos++;
}
}
static int l2tp_xprt_sendmsg(struct l2tp_xprt *xprt, struct l2tp_packet *pkt)
{
int result;
struct msghdr msg;
struct l2tp_tunnel *tunnel;
struct sockaddr_in const *peer_addr;
L2TP_DEBUG(L2TP_XPRT, "%s: tunl %hu: fd=%d len=%d", __func__, xprt->tunnel_id, xprt->fd, pkt->total_len);
/* If the ZLB timer is running, stop it now since we're about to send
* a message which will include the ack.
*/
if (usl_timer_is_running(xprt->zlb_timer)) {
L2TP_DEBUG(L2TP_XPRT, "%s: tunl %hu: stop zlb timer", __func__, xprt->tunnel_id);
usl_timer_stop(xprt->zlb_timer);
}
#ifdef L2TP_TEST
if (l2tp_test_is_fake_tx_drop(xprt->tunnel_id, pkt->session_id)) {
L2TP_DEBUG(L2TP_DATA, "%s: fake tx drop: tid=%hu/%hu, ns/nr=%hu/%hu, msg=%d", __func__,
xprt->tunnel_id, pkt->session_id, pkt->ns, pkt->nr, pkt->msg_type);
result = 0;
goto out;
}
#endif
/* Build the struct msghdr to send the packet */
memset(&msg, 0, sizeof(msg));
result = l2tp_net_prepare_msghdr(&msg, pkt);
if (result < 0) {
l2tp_stats.no_control_frame_resources++;
goto out;
}
tunnel = xprt->tunnel;
/* If peer socket isn't yet connected, use sendto() semantics.
* The socket will be connected as soon as we know the peer's
* port number, which we can derive from his first packet.
*/
peer_addr = l2tp_tunnel_get_peer_addr(tunnel);
if (!l2tp_tunnel_is_fd_connected(tunnel)) {
msg.msg_namelen = sizeof(*peer_addr);
msg.msg_name = (struct sockaddr_in *) peer_addr;
L2TP_DEBUG(L2TP_DATA, "%s: sendto(): peer %x/%hu on fd=%d", __func__,
htonl(peer_addr->sin_addr.s_addr), htons(peer_addr->sin_port), l2tp_tunnel_get_fd(tunnel));
}
if (pkt->avp_len > 0) {
l2tp_tunnel_log(xprt->tunnel, L2TP_DATA, LOG_DEBUG, "DATA: TX: tunl %hu/%hu: %ssend %d bytes to peer %s, "
"packet ns/nr %hu/%hu type %d, retry %d",
xprt->tunnel_id, pkt->session_id, (pkt->requeue_count > 0) ? "re" : "", pkt->total_len,
inet_ntoa(peer_addr->sin_addr), pkt->ns, pkt->nr, pkt->msg_type,
(pkt->requeue_count >= 0) ? pkt->requeue_count : 0);
}
result = sendmsg(xprt->fd, &msg, MSG_DONTWAIT | MSG_NOSIGNAL);
if (result < pkt->total_len) {
L2TP_DEBUG(L2TP_DATA, "%s: send() failed: peer %x/%hu, fd %d, result=%d, total_len=%d", __func__,
htonl(peer_addr->sin_addr.s_addr), htons(peer_addr->sin_port), l2tp_tunnel_get_fd(tunnel),
result, pkt->total_len);
result = -EIO;
if (pkt->avp_len > 0) {
l2tp_stats.total_control_frame_send_fails++;
} else {
xprt->stats.tx_zlb_fails++;
}
} else {
xprt->stats.tx_packets++;
xprt->stats.tx_bytes += pkt->total_len;
if (pkt->avp_len > 0) {
if ((pkt->msg_type > 0) && (pkt->msg_type < L2TP_AVP_MSG_COUNT)) {
l2tp_stats.total_sent_control_frames++;
l2tp_stats.messages[pkt->msg_type].tx++;
}
} else {
xprt->stats.tx_zlbs++;
}
}
out:
return result;
}
/* Process a transport's transmit queue.
* We come here whenever a timer fires or a packet is received to try to make
* progress transmitting any queued packets to our peer. Packets are held in the
* transmit queue when the transmit window is full, i.e. we're waiting for other
* packets to be acked.
*/
static void l2tp_xprt_process_txq(struct l2tp_xprt *xprt)
{
struct l2tp_packet *pkt;
/* Dequeue as many control packets as we can, subject to the sliding window.
* ZLB's are not subject to the sliding window and are never queued to txq.
*/
for (;;) {
int status;
pkt = l2tp_pkt_peek(&xprt->txq);
if (pkt == NULL) {
break;
}
#ifdef DEBUG
if (pkt->avp_len == 0) {
L2TP_DEBUG(L2TP_XPRT, "%s: tunl %hu: zlb pkt ns/nr=%hu/%hu wrongly queued",
__func__, xprt->tunnel_id, xprt->ns, xprt->nr);
l2tp_pkt_unlink(pkt);
l2tp_pkt_free(pkt);
continue;
}
#endif /* DEBUG */
if (!l2tp_xprt_is_tx_window_open(xprt, xprt->ns)) {
if (!xprt->is_tx_stalled) {
xprt->is_tx_stalled = 1;
L2TP_DEBUG(L2TP_XPRT, "%s: tunl %hu: ns/nr=%hu/%hu, tx window is closed", __func__,
xprt->tunnel_id, xprt->ns, xprt->nr);
}
break;
} else {
if (xprt->is_tx_stalled) {
xprt->is_tx_stalled = 0;
L2TP_DEBUG(L2TP_XPRT, "%s: tunl %hu: ns/nr=%hu/%hu, tx window now open", __func__,
xprt->tunnel_id, xprt->ns, xprt->nr);
}
}
/* Dequeue packet */
pkt = l2tp_pkt_dequeue(&xprt->txq);
pkt->ns = xprt->ns;
pkt->nr = xprt->nr;
xprt->ns++;
l2tp_tunnel_log(xprt->tunnel, L2TP_XPRT, LOG_DEBUG, "XPRT: tunl %hu: update ns to %hu",
xprt->tunnel_id, xprt->ns);
#ifdef DEBUG
if (xprt->ns == 0) {
l2tp_xprt_ns_wraps++;
}
#endif /* DEBUG */
l2tp_net_update_header(pkt->iov[0].iov_base, pkt->ns, pkt->nr);
l2tp_tunnel_log(xprt->tunnel, L2TP_XPRT, LOG_DEBUG, "XPRT: tunl %hu: adding packet to ackq, type %hu, len %d, ns/nr %hu/%hu",
xprt->tunnel_id, pkt->msg_type, pkt->total_len, pkt->ns, pkt->nr);
l2tp_pkt_queue_add(&xprt->ackq, pkt);
status = l2tp_xprt_sendmsg(xprt, pkt);
if (status < 0) {
break;
}
}
/* If we're closing and txq and ackq are both empty, the peer has acknowledged
* any tunnel close STOPCCN that we sent. We can safely delete the tunnel
* now. Note that RFC2661 says we should hang around long enough for control
* protocol messages to be acknowledged - they are now.
*/
if (xprt->is_closing && l2tp_pkt_queue_empty(&xprt->ackq) && l2tp_pkt_queue_empty(&xprt->txq)) {
l2tp_tunnel_close_now(xprt->tunnel);
goto out;
}
/* Restart retry timer if ackq is not empty */
if ((!l2tp_pkt_queue_empty(&xprt->ackq)) && (!usl_timer_is_running(xprt->retry_timer))) {
usl_timer_restart(xprt->retry_timer);
}
out:
return;
}
/*****************************************************************************
* Public interface
*****************************************************************************/
/* Give plugins access to our kernel file descriptor.
*/
int l2tp_xprt_get_kernel_fd(struct l2tp_tunnel const *tunnel)
{
struct l2tp_xprt *xprt = l2tp_tunnel_get_xprt(tunnel);
return xprt->kernel_fd;
}
/* Try to transmit any queued packets before sending the new packet.
* Packets are queued subject to the sliding window.
*/
int l2tp_xprt_send(struct l2tp_xprt *xprt, struct l2tp_packet *pkt)
{
int result = 0;
pkt->ns = xprt->ns;
pkt->nr = xprt->nr;
L2TP_DEBUG(L2TP_FUNC, "%s: tunl %hu: pkt=%p len=%d msgtype=%hu", __func__, xprt->tunnel_id, pkt, pkt->total_len, pkt->msg_type);
L2TP_DEBUG(L2TP_DATA, "%s: tunl %hu: fd=%d peer=%x/%hu", __func__, xprt->tunnel_id,
l2tp_tunnel_get_fd(xprt->tunnel), ntohl(peer_addr->sin_addr.s_addr), ntohs(peer_addr->sin_port));
L2TP_DEBUG(L2TP_XPRT, "%s: tunl %hu: pkt=%p len=%d", __func__, xprt->tunnel_id, pkt, pkt->total_len);
L2TP_DEBUG(L2TP_DATA, "%s: data=%02x %02x %02x %02x %02x %02x %02x %02x", __func__,
data[0], data[1], data[2], data[3],
data[4], data[5], data[6], data[7]);
pkt->xprt = xprt;
#ifdef DEBUG
/* ZLB messages should not be queued here */
if (pkt->avp_len == 0) {
L2TP_DEBUG(L2TP_XPRT, "%s: tunl %hu: zlb pkt @ ns/nr=%hu/%hu wrongly queued",
__func__, xprt->tunnel_id, xprt->ns, xprt->nr);