summaryrefslogtreecommitdiff
path: root/include/net/sctp/structs.h
blob: d136122af892faa5ed02a86195775403f41b449f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
/* SCTP kernel reference Implementation
 * Copyright (c) 1999-2000 Cisco, Inc.
 * Copyright (c) 1999-2001 Motorola, Inc.
 * Copyright (c) 2001 Intel Corp.
 * Copyright (c) 2001-2003 International Business Machines Corp.
 *
 * This file is part of the SCTP kernel reference Implementation
 *
 * The SCTP reference implementation 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, or (at your option)
 * any later version.
 *
 * The SCTP reference implementation 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 GNU CC; see the file COPYING.  If not, write to
 * the Free Software Foundation, 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Please send any bug reports or fixes you make to the
 * email addresses:
 *    lksctp developers <lksctp-developers@lists.sourceforge.net>
 *
 * Or submit a bug report through the following website:
 *    http://www.sf.net/projects/lksctp
 *
 * Written or modified by:
 *    Randall Stewart       <randall@sctp.chicago.il.us>
 *    Ken Morneau           <kmorneau@cisco.com>
 *    Qiaobing Xie          <qxie1@email.mot.com>
 *    La Monte H.P. Yarroll <piggy@acm.org>
 *    Karl Knutson          <karl@athena.chicago.il.us>
 *    Jon Grimm             <jgrimm@us.ibm.com>
 *    Xingang Guo           <xingang.guo@intel.com>
 *    Hui Huang             <hui.huang@nokia.com>
 *    Sridhar Samudrala     <sri@us.ibm.com>
 *    Daisy Chang	    <daisyc@us.ibm.com>
 *    Dajiang Zhang         <dajiang.zhang@nokia.com>
 *    Ardelle Fan           <ardelle.fan@intel.com>
 *
 * Any bugs reported given to us we will try to fix... any fixes shared will
 * be incorporated into the next SCTP release.
 */

#ifndef __sctp_structs_h__
#define __sctp_structs_h__

#include <linux/time.h>		/* We get struct timespec.    */
#include <linux/socket.h>	/* linux/in.h needs this!!    */
#include <linux/in.h>		/* We get struct sockaddr_in. */
#include <linux/in6.h>          /* We get struct in6_addr     */
#include <asm/param.h>		/* We get MAXHOSTNAMELEN.     */
#include <asm/atomic.h>		/* This gets us atomic counters.  */
#include <linux/skbuff.h>	/* We need sk_buff_head. */
#include <linux/workqueue.h>	/* We need tq_struct.    */
#include <linux/sctp.h>         /* We need sctp* header structs.  */

/*
 * This is (almost) a direct quote from RFC 2553.
 */

/*
 * Desired design of maximum size and alignment
 */
#define _SS_MAXSIZE    128		/* Implementation specific max size */
#define _SS_ALIGNSIZE  (sizeof (__s64))
				/* Implementation specific desired alignment */
/*
 * Definitions used for sockaddr_storage structure paddings design.
 */
#define _SS_PAD1SIZE   (_SS_ALIGNSIZE - sizeof (sa_family_t))
#define _SS_PAD2SIZE   (_SS_MAXSIZE - (sizeof (sa_family_t)+ \
                              _SS_PAD1SIZE + _SS_ALIGNSIZE))

struct sockaddr_storage {
	sa_family_t  __ss_family;		/* address family */
	/* Following fields are implementation specific */
	char      __ss_pad1[_SS_PAD1SIZE];
				/* 6 byte pad, to make implementation */
				/* specific pad up to alignment field that */
				/* follows explicit in the data structure */
	__s64   __ss_align;	/* field to force desired structure */
				/* storage alignment */
	char      __ss_pad2[_SS_PAD2SIZE];
				/* 112 byte pad to achieve desired size, */
				/* _SS_MAXSIZE value minus size of ss_family */
				/* __ss_pad1, __ss_align fields is 112 */
};

/* A convenience structure for handling sockaddr structures.
 * We should wean ourselves off this.
 */
union sctp_addr {
	struct sockaddr_in v4;
	struct sockaddr_in6 v6;
	struct sockaddr sa;
};


/* Forward declarations for data structures. */
struct sctp_protocol;
struct sctp_endpoint;
struct sctp_association;
struct sctp_transport;
struct sctp_packet;
struct sctp_chunk;
struct sctp_inq;
struct sctp_outq;
struct sctp_bind_addr;
struct sctp_ulpq;
struct sctp_opt;
struct sctp_endpoint_common;
struct sctp_ssnmap;

typedef struct sctp_protocol sctp_protocol_t;
typedef struct sctp_endpoint sctp_endpoint_t;
typedef struct sctp_association sctp_association_t;
typedef struct sctp_packet sctp_packet_t;
typedef struct sctp_chunk sctp_chunk_t;
typedef struct sctp_bind_addr sctp_bind_addr_t;
typedef struct sctp_endpoint_common sctp_endpoint_common_t;

#include <net/sctp/tsnmap.h>
#include <net/sctp/ulpevent.h>
#include <net/sctp/ulpqueue.h>

/* Structures useful for managing bind/connect. */

typedef struct sctp_bind_bucket {
	unsigned short 	port;
	unsigned short	fastreuse;
	struct sctp_bind_bucket *next;
	struct sctp_bind_bucket **pprev;
	struct sock	        *sk;
} sctp_bind_bucket_t;

typedef struct sctp_bind_hashbucket {
	spinlock_t	lock;
	struct sctp_bind_bucket	*chain;
} sctp_bind_hashbucket_t;

/* Used for hashing all associations.  */
typedef struct sctp_hashbucket {
	rwlock_t	lock;
	sctp_endpoint_common_t  *chain;
} sctp_hashbucket_t __attribute__((__aligned__(8)));


/* The SCTP protocol structure. */
struct sctp_protocol {
	/* RFC2960 Section 14. Suggested SCTP Protocol Parameter Values
	 *
	 * The following protocol parameters are RECOMMENDED:
	 *
	 * RTO.Initial              - 3  seconds
	 * RTO.Min                  - 1  second
	 * RTO.Max                 -  60 seconds
	 * RTO.Alpha                - 1/8  (3 when converted to right shifts.)
	 * RTO.Beta                 - 1/4  (2 when converted to right shifts.)
	 */
	__u32 rto_initial;
	__u32 rto_min;
	__u32 rto_max;

	/* Note: rto_alpha and rto_beta are really defined as inverse
	 * powers of two to facilitate integer operations.
	 */
	int rto_alpha;
	int rto_beta;

	/* Max.Burst		    - 4 */
	int max_burst;

	/* Valid.Cookie.Life        - 60  seconds  */
	int valid_cookie_life;

	/* Whether Cookie Preservative is enabled(1) or not(0) */
	int cookie_preserve_enable;

	/* Association.Max.Retrans  - 10 attempts
	 * Path.Max.Retrans         - 5  attempts (per destination address)
	 * Max.Init.Retransmits     - 8  attempts
	 */
	int max_retrans_association;
	int max_retrans_path;
	int max_retrans_init;

	/* HB.interval              - 30 seconds  */
	int hb_interval;

	/* The following variables are implementation specific.  */

	/* Default initialization values to be applied to new associations. */
	__u16 max_instreams;
	__u16 max_outstreams;

	/* This is a list of groups of functions for each address
	 * family that we support.
	 */
	struct list_head address_families;

	/* This is the hash of all endpoints. */
	int ep_hashsize;
	sctp_hashbucket_t *ep_hashbucket;

	/* This is the hash of all associations. */
	int assoc_hashsize;
	sctp_hashbucket_t *assoc_hashbucket;

	/* This is the sctp port control hash.  */
	int port_hashsize;
	int port_rover;
	spinlock_t port_alloc_lock;  /* Protects port_rover. */
	sctp_bind_hashbucket_t *port_hashtable;

	/* This is the global local address list.
	 * We actively maintain this complete list of interfaces on
	 * the system by catching routing events.
	 *
	 * It is a list of struct sockaddr_storage_list.
	 */
	struct list_head local_addr_list;
	spinlock_t local_addr_lock;
};


/*
 * Pointers to address related SCTP functions.
 * (i.e. things that depend on the address family.)
 */
struct sctp_af {
	int		(*sctp_xmit)	(struct sk_buff *skb,
					 struct sctp_transport *,
					 int ipfragok);
	int 		(*setsockopt)	(struct sock *sk,
					 int level,
					 int optname,
					 char *optval,
					 int optlen);
	int 		(*getsockopt)	(struct sock *sk,
					 int level,
					 int optname,
					 char *optval,
					 int *optlen);
	struct dst_entry *(*get_dst)	(struct sctp_association *asoc,
					 union sctp_addr *daddr,
					 union sctp_addr *saddr);
	void 		(*get_saddr)	(struct sctp_association *asoc,
					 struct dst_entry *dst,
					 union sctp_addr *daddr,
				 	 union sctp_addr *saddr);	 
	void            (*copy_addrlist) (struct list_head *,
					  struct net_device *);
	void            (*dst_saddr)    (union sctp_addr *saddr,
					 struct dst_entry *dst,
					 unsigned short port);
	int             (*cmp_addr)     (const union sctp_addr *addr1,
					 const union sctp_addr *addr2);
	void            (*addr_copy)    (union sctp_addr *dst,
					 union sctp_addr *src);
	void            (*from_skb)     (union sctp_addr *,
					 struct sk_buff *skb,
					 int saddr);
	void            (*from_sk)      (union sctp_addr *,
					 struct sock *sk);
	void            (*to_sk)        (union sctp_addr *,
					 struct sock *sk);
	int             (*addr_valid)   (union sctp_addr *);
	sctp_scope_t    (*scope) (union sctp_addr *);
	void            (*inaddr_any)   (union sctp_addr *, unsigned short);
	int             (*is_any)       (const union sctp_addr *);
	int             (*available)    (const union sctp_addr *);
	__u16		net_header_len;
	int		sockaddr_len;
	sa_family_t	sa_family;
	struct list_head list;
};

struct sctp_af *sctp_get_af_specific(sa_family_t);
int sctp_register_af(struct sctp_af *);

/* Protocol family functions. */
struct sctp_pf {
	void (*event_msgname)(struct sctp_ulpevent *, char *, int *);
	void (*skb_msgname)  (struct sk_buff *, char *, int *);
	int  (*af_supported) (sa_family_t);
	int  (*cmp_addr) (const union sctp_addr *,
			  const union sctp_addr *,
			  struct sctp_opt *);
	int  (*bind_verify) (struct sctp_opt *, union sctp_addr *);
	struct sctp_af *af;
};

/* SCTP Socket type: UDP or TCP style. */
typedef enum {
	SCTP_SOCKET_UDP = 0,
	SCTP_SOCKET_UDP_HIGH_BANDWIDTH,
	SCTP_SOCKET_TCP
} sctp_socket_type_t;

/* Per socket SCTP information. */
struct sctp_opt {
	/* What kind of a socket is this? */
	sctp_socket_type_t type;

	/* PF_ family specific functions.  */
	struct sctp_pf *pf;

	/* What is our base endpointer? */
	sctp_endpoint_t *ep;

	/* Various Socket Options.  */
	__u16 default_stream;
	__u32 default_ppid;
	struct sctp_initmsg initmsg;
	struct sctp_rtoinfo rtoinfo;
	struct sctp_paddrparams paddrparam;
	struct sctp_event_subscribe subscribe;
	__u32 autoclose;
	__u8 nodelay;
	__u8 disable_fragments;
	__u8 pd_mode;

	/* Receive to here while partial delivery is in effect. */
	struct sk_buff_head pd_lobby;
};



/* This is our APPLICATION-SPECIFIC state cookie.
 * THIS IS NOT DICTATED BY THE SPECIFICATION.
 */
/* These are the parts of an association which we send in the cookie.
 * Most of these are straight out of:
 * RFC2960 12.2 Parameters necessary per association (i.e. the TCB)
 *
 */

typedef struct sctp_cookie {

        /* My          : Tag expected in every inbound packet and sent
         * Verification: in the INIT or INIT ACK chunk.
         * Tag         :
         */
        __u32 my_vtag;

        /* Peer's      : Tag expected in every outbound packet except
         * Verification: in the INIT chunk.
         * Tag         :
         */
        __u32 peer_vtag;

        /* The rest of these are not from the spec, but really need to
         * be in the cookie.
         */

	/* My Tie Tag  : Assist in discovering a restarting association. */
	__u32 my_ttag;

	/* Peer's Tie Tag: Assist in discovering a restarting association. */
	__u32 peer_ttag;

        /* When does this cookie expire? */
        struct timeval expiration;

	/* Number of inbound/outbound streams which are set
	 * and negotiated during the INIT process.
	 */
	__u16 sinit_num_ostreams;
	__u16 sinit_max_instreams;

        /* This is the first sequence number I used.  */
	__u32 initial_tsn;

	/* This holds the originating address of the INIT packet.  */
	union sctp_addr peer_addr;

	/* This is a shim for my peer's INIT packet, followed by
	 * a copy of the raw address list of the association.
	 * The length of the raw address list is saved in the
	 * raw_addr_list_len field, which will be used at the time when
	 * the association TCB is re-constructed from the cookie.
	 */
	__u32 raw_addr_list_len;
	sctp_init_chunk_t peer_init[0];
} sctp_cookie_t;


/* The format of our cookie that we send to our peer. */
typedef struct sctp_signed_cookie {
	__u8 signature[SCTP_SECRET_SIZE];
	sctp_cookie_t c;
} sctp_signed_cookie_t;



/* This is another convenience type to allocate memory for address
 * params for the maximum size and pass such structures around
 * internally.
 */
typedef union {
	sctp_ipv4addr_param_t v4;
	sctp_ipv6addr_param_t v6;
} sctp_addr_param_t;

/* A convenience type to allow walking through the various
 * parameters and avoid casting all over the place.
 */
union sctp_params {
	void *v;
	sctp_paramhdr_t *p;
	sctp_cookie_preserve_param_t *life;
	sctp_hostname_param_t *dns;
	sctp_cookie_param_t *cookie;
	sctp_supported_addrs_param_t *sat;
	sctp_ipv4addr_param_t *v4;
	sctp_ipv6addr_param_t *v6;
	sctp_addr_param_t *addr;
};

/* RFC 2960.  Section 3.3.5 Heartbeat.
 *    Heartbeat Information: variable length
 *    The Sender-specific Heartbeat Info field should normally include
 *    information about the sender's current time when this HEARTBEAT
 *    chunk is sent and the destination transport address to which this
 *    HEARTBEAT is sent (see Section 8.3).
 */
typedef struct sctp_sender_hb_info {
	sctp_paramhdr_t param_hdr;
	union sctp_addr daddr;
	unsigned long sent_at;
} sctp_sender_hb_info_t __attribute__((packed));

/*
 *  RFC 2960 1.3.2 Sequenced Delivery within Streams
 *
 *  The term "stream" is used in SCTP to refer to a sequence of user
 *  messages that are to be delivered to the upper-layer protocol in
 *  order with respect to other messages within the same stream.  This is
 *  in contrast to its usage in TCP, where it refers to a sequence of
 *  bytes (in this document a byte is assumed to be eight bits).
 *  ...
 *
 *  This is the structure we use to track both our outbound and inbound
 *  SSN, or Stream Sequence Numbers.
 */

struct sctp_stream {
	__u16 *ssn;
	unsigned int len;
};

struct sctp_ssnmap {
	struct sctp_stream in;
	struct sctp_stream out;
	int malloced;
};

struct sctp_ssnmap *sctp_ssnmap_init(struct sctp_ssnmap *, __u16, __u16);
struct sctp_ssnmap *sctp_ssnmap_new(__u16 in, __u16 out, int priority);
void sctp_ssnmap_free(struct sctp_ssnmap *map);
void sctp_ssnmap_clear(struct sctp_ssnmap *map);

/* What is the current SSN number for this stream? */
static inline __u16 sctp_ssn_peek(struct sctp_stream *stream, __u16 id)
{
	return stream->ssn[id];
}

/* Return the next SSN number for this stream.  */
static inline __u16 sctp_ssn_next(struct sctp_stream *stream, __u16 id)
{
	return stream->ssn[id]++;
}


/* RFC2960 1.4 Key Terms
 *
 * o Chunk: A unit of information within an SCTP packet, consisting of
 * a chunk header and chunk-specific content.
 *
 * As a matter of convenience, we remember the SCTP common header for
 * each chunk as well as a few other header pointers...
 */
struct sctp_chunk {
	/* These first three elements MUST PRECISELY match the first
	 * three elements of struct sk_buff.  This allows us to reuse
	 * all the skb_* queue management functions.
	 */
	sctp_chunk_t *next;
	sctp_chunk_t *prev;
	struct sk_buff_head *list;

	/* This is our link to the per-transport transmitted list.  */
	struct list_head transmitted_list;

	/* This field is used by chunks that hold fragmented data.
	 * For the first fragment this is the list that holds the rest of
	 * fragments. For the remaining fragments, this is the link to the
	 * frag_list maintained in the first fragment.
	 */
	struct list_head frag_list;

	/* This points to the sk_buff containing the actual data.  */
	struct sk_buff *skb;

	/* These are the SCTP headers by reverse order in a packet.
	 * Note that some of these may happen more than once.  In that
	 * case, we point at the "current" one, whatever that means
	 * for that level of header.
	 */

	/* We point this at the FIRST TLV parameter to chunk_hdr.  */
	union sctp_params param_hdr;
	union {
		__u8 *v;
		sctp_datahdr_t *data_hdr;
		sctp_inithdr_t *init_hdr;
		sctp_sackhdr_t *sack_hdr;
		sctp_heartbeathdr_t *hb_hdr;
		sctp_sender_hb_info_t *hbs_hdr;
		sctp_shutdownhdr_t *shutdown_hdr;
		sctp_signed_cookie_t *cookie_hdr;
		sctp_ecnehdr_t *ecne_hdr;
		sctp_cwrhdr_t *ecn_cwr_hdr;
		sctp_errhdr_t *err_hdr;
	} subh;

	__u8 *chunk_end;

	sctp_chunkhdr_t *chunk_hdr;

	sctp_sctphdr_t  *sctp_hdr;

	/* This needs to be recoverable for SCTP_SEND_FAILED events. */
	struct sctp_sndrcvinfo sinfo;

	/* Which association does this belong to?  */
	sctp_association_t *asoc;

	/* What endpoint received this chunk? */
	sctp_endpoint_common_t *rcvr;

	/* We fill this in if we are calculating RTT. */
	unsigned long sent_at;

	__u8 rtt_in_progress;  /* Is this chunk used for RTT calculation? */
	__u8 num_times_sent;   /* How man times did we send this? */
	__u8 has_tsn;          /* Does this chunk have a TSN yet? */
	__u8 has_ssn;          /* Does this chunk have a SSN yet? */
	__u8 singleton;        /* Was this the only chunk in the packet? */
	__u8 end_of_packet;    /* Was this the last chunk in the packet? */
	__u8 ecn_ce_done;      /* Have we processed the ECN CE bit? */
	__u8 pdiscard;	  /* Discard the whole packet now? */
	__u8 tsn_gap_acked;	  /* Is this chunk acked by a GAP ACK? */
	__u8 fast_retransmit;    /* Is this chunk fast retransmitted? */
	__u8 tsn_missing_report; /* Data chunk missing counter. */

	/* What is the origin IP address for this chunk?  */
	union sctp_addr source;
	/* Destination address for this chunk. */
	union sctp_addr dest;

	/* For an inbound chunk, this tells us where it came from.
	 * For an outbound chunk, it tells us where we'd like it to
	 * go.  It is NULL if we have no preference.
	 */
	struct sctp_transport *transport;
};

sctp_chunk_t *sctp_make_chunk(const sctp_association_t *, __u8 type,
			      __u8 flags, int size);
void sctp_free_chunk(sctp_chunk_t *);
void  *sctp_addto_chunk(sctp_chunk_t *chunk, int len, const void *data);
sctp_chunk_t *sctp_chunkify(struct sk_buff *, const sctp_association_t *,
			    struct sock *);
void sctp_init_addrs(sctp_chunk_t *, union sctp_addr *, union sctp_addr *);
const union sctp_addr *sctp_source(const sctp_chunk_t *chunk);

/* This is a structure for holding either an IPv6 or an IPv4 address.  */
/* sin_family -- AF_INET or AF_INET6
 * sin_port -- ordinary port number
 * sin_addr -- cast to either (struct in_addr) or (struct in6_addr)
 */
struct sockaddr_storage_list {
	struct list_head list;
	union sctp_addr a;
};

typedef sctp_chunk_t *(sctp_packet_phandler_t)(sctp_association_t *);

/* This structure holds lists of chunks as we are assembling for
 * transmission.
 */
struct sctp_packet {
	/* These are the SCTP header values (host order) for the packet. */
	__u16 source_port;
	__u16 destination_port;
	__u32 vtag;

	/* This contains the payload chunks.  */
	struct sk_buff_head chunks;
	/* This is the total size of all chunks INCLUDING padding.  */
	size_t size;

	/* The packet is destined for this transport address.
	 * The function we finally use to pass down to the next lower
	 * layer lives in the transport structure.
	 */
	struct sctp_transport *transport;

	/* Allow a callback for getting a high priority chunk
	 * bundled early into the packet (This is used for ECNE).
	 */
	sctp_packet_phandler_t *get_prepend_chunk;

	/* This packet should advertise ECN capability to the network
	 * via the ECT bit.
	 */
	int ecn_capable;

	/* This packet contains a COOKIE-ECHO chunk. */
	int has_cookie_echo;

	/* SCTP cannot fragment this packet. So let ip fragment it. */
	int ipfragok;

	int malloced;
};

typedef int (sctp_outq_thandler_t)(struct sctp_outq *, void *);
typedef int (sctp_outq_ehandler_t)(struct sctp_outq *);
typedef sctp_packet_t *(sctp_outq_ohandler_init_t)
	(sctp_packet_t *,
         struct sctp_transport *,
         __u16 sport,
         __u16 dport);
typedef sctp_packet_t *(sctp_outq_ohandler_config_t)
        (sctp_packet_t *,
	 __u32 vtag,
	 int ecn_capable,
	 sctp_packet_phandler_t *get_prepend_chunk);
typedef sctp_xmit_t (sctp_outq_ohandler_t)(sctp_packet_t *,
                                               sctp_chunk_t *);
typedef int (sctp_outq_ohandler_force_t)(sctp_packet_t *);

sctp_outq_ohandler_init_t    sctp_packet_init;
sctp_outq_ohandler_config_t  sctp_packet_config;
sctp_outq_ohandler_t         sctp_packet_append_chunk;
sctp_outq_ohandler_t         sctp_packet_transmit_chunk;
sctp_outq_ohandler_force_t   sctp_packet_transmit;
void sctp_packet_free(sctp_packet_t *);


/* This represents a remote transport address.
 * For local transport addresses, we just use union sctp_addr.
 *
 * RFC2960 Section 1.4 Key Terms
 *
 *   o  Transport address:  A Transport Address is traditionally defined
 *      by Network Layer address, Transport Layer protocol and Transport
 *      Layer port number.  In the case of SCTP running over IP, a
 *      transport address is defined by the combination of an IP address
 *      and an SCTP port number (where SCTP is the Transport protocol).
 *
 * RFC2960 Section 7.1 SCTP Differences from TCP Congestion control
 *
 *   o  The sender keeps a separate congestion control parameter set for
 *      each of the destination addresses it can send to (not each
 *      source-destination pair but for each destination).  The parameters
 *      should decay if the address is not used for a long enough time
 *      period.
 *
 */
struct sctp_transport {
	/* A list of transports. */
	struct list_head transports;

	/* Reference counting. */
	atomic_t refcnt;
	int      dead;

	/* This is the peer's IP address and port. */
	union sctp_addr ipaddr;

	/* These are the functions we call to handle LLP stuff.  */
	struct sctp_af *af_specific;

	/* Which association do we belong to?  */
	sctp_association_t *asoc;

	/* RFC2960
	 *
	 * 12.3 Per Transport Address Data
	 *
	 * For each destination transport address in the peer's
	 * address list derived from the INIT or INIT ACK chunk, a
	 * number of data elements needs to be maintained including:
	 */
	__u32 rtt;		/* This is the most recent RTT.  */

	/* RTO         : The current retransmission timeout value.  */
	__u32 rto;

	/* RTTVAR      : The current RTT variation.  */
	__u32 rttvar;

	/* SRTT        : The current smoothed round trip time.  */
	__u32 srtt;

	/* RTO-Pending : A flag used to track if one of the DATA
	 *              chunks sent to this address is currently being
	 *              used to compute a RTT. If this flag is 0,
	 *              the next DATA chunk sent to this destination
	 *              should be used to compute a RTT and this flag
	 *              should be set. Every time the RTT
	 *              calculation completes (i.e. the DATA chunk
	 *              is SACK'd) clear this flag.
         */
	int rto_pending;


	/*
	 * These are the congestion stats.
	 */
	/* cwnd        : The current congestion window.  */
	__u32 cwnd;               /* This is the actual cwnd.  */

	/* ssthresh    : The current slow start threshold value.  */
	__u32 ssthresh;

	/* partial     : The tracking method for increase of cwnd when in
	 * bytes acked : congestion avoidance mode (see Section 6.2.2)
	 */
	__u32 partial_bytes_acked;

	/* Data that has been sent, but not acknowledged. */
	__u32 flight_size;

	/* PMTU       : The current known path MTU.  */
	__u32 pmtu;

	/* Destination */
	struct dst_entry *dst;
	/* Source address. */
	union sctp_addr saddr;

	/* When was the last time(in jiffies) that a data packet was sent on
	 * this transport?  This is used to adjust the cwnd when the transport
	 * becomes inactive.
	 */
	unsigned long last_time_used;

	/* Heartbeat interval: The endpoint sends out a Heartbeat chunk to
	 * the destination address every heartbeat interval.
	 */
	int hb_interval;

	/* When was the last time (in jiffies) that we heard from this
	 * transport?  We use this to pick new active and retran paths.
	 */
	unsigned long last_time_heard;

	/* Last time(in jiffies) when cwnd is reduced due to the congestion
	 * indication based on ECNE chunk.
	 */
	unsigned long last_time_ecne_reduced;

	/* active      : The current active state of this destination,
	 *             :  i.e. DOWN, UP, etc.
	 */
	int active;

	/* hb_allowed  : The current heartbeat state of this destination,
	 *             :  i.e. ALLOW-HB, NO-HEARTBEAT, etc.
	 */
	int hb_allowed;

	/* These are the error stats for this destination.  */

	/* Error count : The current error count for this destination.  */
	unsigned short error_count;

	/* Error       : Current error threshold for this destination
	 * Threshold   : i.e. what value marks the destination down if
	 *             : errorCount reaches this value.
	 */
	unsigned short error_threshold;

	/* This is the max_retrans value for the transport and will
	 * be initialized to proto.max_retrans.path.  This can be changed
	 * using SCTP_SET_PEER_ADDR_PARAMS socket option.
	 */
	int max_retrans;

	/* We use this name for debugging output... */
	char *debug_name;

	/* Per         : A timer used by each destination.
	 * Destination :
	 * Timer       :
	 *
	 * [Everywhere else in the text this is called T3-rtx. -ed]
	 */
	struct timer_list T3_rtx_timer;

	/* Heartbeat timer is per destination. */
	struct timer_list hb_timer;

	/* Since we're using per-destination retransmission timers
	 * (see above), we're also using per-destination "transmitted"
	 * queues.  This probably ought to be a private struct
	 * accessible only within the outqueue, but it's not, yet.
	 */
	struct list_head transmitted;

	/* We build bundle-able packets for this transport here.  */
	sctp_packet_t packet;

	/* This is the list of transports that have chunks to send.  */
	struct list_head send_ready;

	int malloced; /* Is this structure kfree()able? */
};

struct sctp_transport *sctp_transport_new(const union sctp_addr *, int);
struct sctp_transport *sctp_transport_init(struct sctp_transport *,
					   const union sctp_addr *, int);
void sctp_transport_set_owner(struct sctp_transport *, sctp_association_t *);
void sctp_transport_route(struct sctp_transport *, union sctp_addr *,
			  struct sctp_opt *);
void sctp_transport_pmtu(struct sctp_transport *);
void sctp_transport_free(struct sctp_transport *);
void sctp_transport_destroy(struct sctp_transport *);
void sctp_transport_reset_timers(struct sctp_transport *);
void sctp_transport_hold(struct sctp_transport *);
void sctp_transport_put(struct sctp_transport *);
void sctp_transport_update_rto(struct sctp_transport *, __u32);
void sctp_transport_raise_cwnd(struct sctp_transport *, __u32, __u32);
void sctp_transport_lower_cwnd(struct sctp_transport *, sctp_lower_cwnd_t);
unsigned long sctp_transport_timeout(struct sctp_transport *);


/* This is the structure we use to queue packets as they come into
 * SCTP.  We write packets to it and read chunks from it.
 */
struct sctp_inq {
	/* This is actually a queue of sctp_chunk each
	 * containing a partially decoded packet.
	 */
	struct sk_buff_head in;
	/* This is the packet which is currently off the in queue and is
	 * being worked on through the inbound chunk processing.
	 */
	sctp_chunk_t *in_progress;

	/* This is the delayed task to finish delivering inbound
	 * messages.
	 */
	struct work_struct immediate;

	int malloced;        /* Is this structure kfree()able?  */
};

struct sctp_inq *sctp_inq_new(void);
void sctp_inq_init(struct sctp_inq *);
void sctp_inq_free(struct sctp_inq *);
void sctp_inq_push(struct sctp_inq *, sctp_chunk_t *packet);
struct sctp_chunk *sctp_inq_pop(struct sctp_inq *);
void sctp_inq_set_th_handler(struct sctp_inq *, void (*)(void *), void *);

/* This is the structure we use to hold outbound chunks.  You push
 * chunks in and they automatically pop out the other end as bundled
 * packets (it calls (*output_handler)()).
 *
 * This structure covers sections 6.3, 6.4, 6.7, 6.8, 6.10, 7., 8.1,
 * and 8.2 of the v13 draft.
 *
 * It handles retransmissions.  The connection to the timeout portion
 * of the state machine is through sctp_..._timeout() and timeout_handler.
 *
 * If you feed it SACKs, it will eat them.
 *
 * If you give it big chunks, it will fragment them.
 *
 * It assigns TSN's to data chunks.  This happens at the last possible
 * instant before transmission.
 *
 * When free()'d, it empties itself out via output_handler().
 */
struct sctp_outq {
	sctp_association_t *asoc;

	/* BUG: This really should be an array of streams.
	 * This really holds a list of chunks (one stream).
	 * FIXME: If true, why so?
	 */
	struct sk_buff_head out;

	/* These are control chunks we want to send.  */
	struct sk_buff_head control;

	/* These are chunks that have been sacked but are above the
	 * CTSN, or cumulative tsn ack point.
	 */
	struct list_head sacked;

	/* Put chunks on this list to schedule them for
	 * retransmission.
	 */
	struct list_head retransmit;

	/* Call these functions to send chunks down to the next lower
	 * layer.  This is always SCTP_packet, but we separate the two
	 * structures to make testing simpler.
	 */
	sctp_outq_ohandler_init_t	*init_output;
	sctp_outq_ohandler_config_t	*config_output;
	sctp_outq_ohandler_t	*append_output;
	sctp_outq_ohandler_t	*build_output;
	sctp_outq_ohandler_force_t	*force_output;

	/* How many unackd bytes do we have in-flight?  */
	__u32 outstanding_bytes;

	/* Is this structure empty?  */
	int empty;

	/* Are we kfree()able? */
	int malloced;
};

struct sctp_outq *sctp_outq_new(sctp_association_t *);
void sctp_outq_init(sctp_association_t *, struct sctp_outq *);
void sctp_outq_teardown(struct sctp_outq *);
void sctp_outq_free(struct sctp_outq*);
int sctp_outq_tail(struct sctp_outq *, sctp_chunk_t *chunk);
int sctp_outq_flush(struct sctp_outq *, int);
int sctp_outq_sack(struct sctp_outq *, sctp_sackhdr_t *);
int sctp_outq_is_empty(const struct sctp_outq *);
int sctp_outq_set_output_handlers(struct sctp_outq *,
				  sctp_outq_ohandler_init_t init,
				  sctp_outq_ohandler_config_t config,
				  sctp_outq_ohandler_t append,
				  sctp_outq_ohandler_t build,
				  sctp_outq_ohandler_force_t force);
void sctp_outq_restart(struct sctp_outq *);
void sctp_retransmit(struct sctp_outq *, struct sctp_transport *,
		     sctp_retransmit_reason_t);
void sctp_retransmit_mark(struct sctp_outq *, struct sctp_transport *, __u8);


/* These bind address data fields common between endpoints and associations */
struct sctp_bind_addr {

	/* RFC 2960 12.1 Parameters necessary for the SCTP instance
	 *
	 * SCTP Port:   The local SCTP port number the endpoint is
	 * 		bound to.
	 */
	__u16 port;

	/* RFC 2960 12.1 Parameters necessary for the SCTP instance
	 *
	 * Address List: The list of IP addresses that this instance
	 *	has bound.  This information is passed to one's
	 *	peer(s) in INIT and INIT ACK chunks.
	 */
	struct list_head address_list;

	int malloced;        /* Are we kfree()able?  */
};

sctp_bind_addr_t *sctp_bind_addr_new(int gfp_mask);
void sctp_bind_addr_init(sctp_bind_addr_t *, __u16 port);
void sctp_bind_addr_free(sctp_bind_addr_t *);
int sctp_bind_addr_copy(sctp_bind_addr_t *dest, const sctp_bind_addr_t *src,
			sctp_scope_t scope, int priority,int flags);
int sctp_add_bind_addr(sctp_bind_addr_t *, union sctp_addr *,
		       int priority);
int sctp_del_bind_addr(sctp_bind_addr_t *, union sctp_addr *);
int sctp_bind_addr_match(sctp_bind_addr_t *, const union sctp_addr *,
			 struct sctp_opt *);
union sctp_params sctp_bind_addrs_to_raw(const sctp_bind_addr_t *bp,
					 int *addrs_len,
					 int priority);
int sctp_raw_to_bind_addrs(sctp_bind_addr_t *bp,
			   __u8 *raw_addr_list,
			   int addrs_len,
			   unsigned short port,
			   int priority);

sctp_scope_t sctp_scope(const union sctp_addr *);
int sctp_in_scope(const union sctp_addr *addr, const sctp_scope_t scope);
int sctp_is_any(const union sctp_addr *addr);
int sctp_addr_is_valid(const union sctp_addr *addr);


/* What type of sctp_endpoint_common?  */
typedef enum {
	SCTP_EP_TYPE_SOCKET,
	SCTP_EP_TYPE_ASSOCIATION,
} sctp_endpoint_type_t;

/*
 * A common base class to bridge the implmentation view of a
 * socket (usually listening) endpoint versus an association's
 * local endpoint.
 * This common structure is useful for several purposes:
 *   1) Common interface for lookup routines.
 *      a) Subfunctions work for either endpoint or association
 *      b) Single interface to lookup allows hiding the lookup lock rather
 *         than acquiring it externally.
 *   2) Common interface for the inbound chunk handling/state machine.
 *   3) Common object handling routines for reference counting, etc.
 *   4) Disentangle association lookup from endpoint lookup, where we
 *      do not have to find our endpoint to find our association.
 *
 */

struct sctp_endpoint_common {
	/* Fields to help us manage our entries in the hash tables. */
	sctp_endpoint_common_t *next;
	sctp_endpoint_common_t **pprev;
	int hashent;

	/* Runtime type information.  What kind of endpoint is this? */
	sctp_endpoint_type_t type;

	/* Some fields to help us manage this object.
	 *   refcnt   - Reference count access to this object.
	 *   dead     - Do not attempt to use this object.
	 *   malloced - Do we need to kfree this object?
	 */
	atomic_t    refcnt;
	char        dead;
	char        malloced;

	/* What socket does this endpoint belong to?  */
	struct sock *sk;

	/* This is where we receive inbound chunks.  */
	struct sctp_inq   inqueue;

	/* This substructure includes the defining parameters of the
	 * endpoint:
	 * bind_addr.port is our shared port number.
	 * bind_addr.address_list is our set of local IP addresses.
	 */
	sctp_bind_addr_t bind_addr;

	/* Protection during address list comparisons. */
	rwlock_t   addr_lock;
};


/* RFC Section 1.4 Key Terms
 *
 * o SCTP endpoint: The logical sender/receiver of SCTP packets. On a
 *   multi-homed host, an SCTP endpoint is represented to its peers as a
 *   combination of a set of eligible destination transport addresses to
 *   which SCTP packets can be sent and a set of eligible source
 *   transport addresses from which SCTP packets can be received.
 *   All transport addresses used by an SCTP endpoint must use the
 *   same port number, but can use multiple IP addresses. A transport
 *   address used by an SCTP endpoint must not be used by another
 *   SCTP endpoint. In other words, a transport address is unique
 *   to an SCTP endpoint.
 *
 * From an implementation perspective, each socket has one of these.
 * A TCP-style socket will have exactly one association on one of
 * these.  An UDP-style socket will have multiple associations hanging
 * off one of these.
 */

struct sctp_endpoint {
	/* Common substructure for endpoint and association. */
	sctp_endpoint_common_t base;

	/* These are the system-wide defaults and other stuff which is
	 * endpoint-independent.
	 */
	struct sctp_protocol *proto;

	/* Associations: A list of current associations and mappings
	 *            to the data consumers for each association. This
	 *            may be in the form of a hash table or other
	 *            implementation dependent structure. The data
	 *            consumers may be process identification
	 *            information such as file descriptors, named pipe
	 *            pointer, or table pointers dependent on how SCTP
	 *            is implemented.
	 */
	/* This is really a list of sctp_association_t entries. */
	struct list_head asocs;

	/* Secret Key: A secret key used by this endpoint to compute
	 *            the MAC.  This SHOULD be a cryptographic quality
	 *            random number with a sufficient length.
	 *	      Discussion in [RFC1750] can be helpful in
	 * 	      selection of the key.
	 */
	__u8 secret_key[SCTP_HOW_MANY_SECRETS][SCTP_SECRET_SIZE];
	int current_key;
	int last_key;
	int key_changed_at;

	/* Default timeouts.  */
	int timeouts[SCTP_NUM_TIMEOUT_TYPES];

	/* Various thresholds.  */

	/* Name for debugging output... */
	char *debug_name;
};

/* Recover the outter endpoint structure. */
static inline sctp_endpoint_t *sctp_ep(sctp_endpoint_common_t *base)
{
	sctp_endpoint_t *ep;

	ep = container_of(base, sctp_endpoint_t, base);
	return ep;
}

/* These are function signatures for manipulating endpoints.  */
sctp_endpoint_t *sctp_endpoint_new(sctp_protocol_t *, struct sock *, int);
sctp_endpoint_t *sctp_endpoint_init(sctp_endpoint_t *, sctp_protocol_t *,
				    struct sock *, int priority);
void sctp_endpoint_free(sctp_endpoint_t *);
void sctp_endpoint_put(sctp_endpoint_t *);
void sctp_endpoint_hold(sctp_endpoint_t *);
void sctp_endpoint_add_asoc(sctp_endpoint_t *, sctp_association_t *asoc);
sctp_association_t *sctp_endpoint_lookup_assoc(const sctp_endpoint_t *ep,
					       const union sctp_addr *paddr,
					       struct sctp_transport **);
int sctp_endpoint_is_peeled_off(sctp_endpoint_t *, const union sctp_addr *);
sctp_endpoint_t *sctp_endpoint_is_match(sctp_endpoint_t *,
					const union sctp_addr *);

int sctp_has_association(const union sctp_addr *laddr,
			 const union sctp_addr *paddr);

int sctp_verify_init(const sctp_association_t *asoc,
		     sctp_cid_t cid,
		     sctp_init_chunk_t *peer_init,
		     sctp_chunk_t *chunk,
		     sctp_chunk_t **err_chunk);
int sctp_process_init(sctp_association_t *asoc, sctp_cid_t cid,
		      const union sctp_addr *peer_addr,
		      sctp_init_chunk_t *peer_init, int priority);
int sctp_process_param(sctp_association_t *asoc, union sctp_params param,
		       const union sctp_addr *peer_addr, int priority);
__u32 sctp_generate_tag(const sctp_endpoint_t *ep);
__u32 sctp_generate_tsn(const sctp_endpoint_t *ep);


/* RFC2960
 *
 * 12. Recommended Transmission Control Block (TCB) Parameters
 *
 * This section details a recommended set of parameters that should
 * be contained within the TCB for an implementation. This section is
 * for illustrative purposes and should not be deemed as requirements
 * on an implementation or as an exhaustive list of all parameters
 * inside an SCTP TCB. Each implementation may need its own additional
 * parameters for optimization.
 */


/* Here we have information about each individual association. */
struct sctp_association {

	/* A base structure common to endpoint and association.
	 * In this context, it represents the associations's view
	 * of the local endpoint of the association.
	 */
	sctp_endpoint_common_t base;

	/* Associations on the same socket. */
	struct list_head asocs;

	/* This is a signature that lets us know that this is a
	 * sctp_association_t data structure.  Used for mapping an
	 * association id to an association.
	 */
	__u32 eyecatcher;

	/* This is our parent endpoint.  */
	sctp_endpoint_t *ep;

	/* These are those association elements needed in the cookie.  */
	sctp_cookie_t c;

	/* This is all information about our peer.  */
	struct {
		/* rwnd
		 *
		 * Peer Rwnd   : Current calculated value of the peer's rwnd.
		 */
		__u32 rwnd;

		/* transport_addr_list
		 *
		 * Peer        : A list of SCTP transport addresses that the
		 * Transport   : peer is bound to. This information is derived
		 * Address     : from the INIT or INIT ACK and is used to
		 * List        : associate an inbound packet with a given
		 *             : association. Normally this information is
		 *	       : hashed or keyed for quick lookup and access
		 *	       : of the TCB.
		 *
		 * It is a list of SCTP_transport's.
		 */
		struct list_head transport_addr_list;

		/* port
		 *   The transport layer port number.
		 */
		__u16 port;

		/* primary_path
		 *
		 * Primary     : This is the current primary destination
		 * Path        : transport address of the peer endpoint.  It
		 *             : may also specify a source transport address
		 *	       : on this endpoint.
		 *
		 * All of these paths live on transport_addr_list.
		 *
		 * At the bakeoffs, we discovered that the intent of
		 * primaryPath is that it only changes when the ULP
		 * asks to have it changed.  We add the activePath to
		 * designate the connection we are currently using to
		 * transmit new data and most control chunks.
		 */
		struct sctp_transport *primary_path;

		/* Cache the primary path address here, when we
		 * need a an address for msg_name.
		 */
		union sctp_addr primary_addr;

		/* active_path
		 *   The path that we are currently using to
		 *   transmit new data and most control chunks.
		 */
		struct sctp_transport *active_path;

		/* retran_path
		 *
		 * RFC2960 6.4 Multi-homed SCTP Endpoints
		 * ...
		 * Furthermore, when its peer is multi-homed, an
		 * endpoint SHOULD try to retransmit a chunk to an
		 * active destination transport address that is
		 * different from the last destination address to
		 * which the DATA chunk was sent.
		 */
		struct sctp_transport *retran_path;

		/* Pointer to last transport I have sent on.  */
		struct sctp_transport *last_sent_to;

		/* This is the last transport I have recieved DATA on.  */
		struct sctp_transport *last_data_from;

		/*
		 * Mapping  An array of bits or bytes indicating which out of
		 * Array    order TSN's have been received (relative to the
		 *          Last Rcvd TSN). If no gaps exist, i.e. no out of
		 *          order packets have been received, this array
		 *          will be set to all zero. This structure may be
		 *          in the form of a circular buffer or bit array.
		 *
		 * Last Rcvd   : This is the last TSN received in
		 * TSN	       : sequence. This value is set initially by
		 *             : taking the peer's Initial TSN, received in
		 *             : the INIT or INIT ACK chunk, and subtracting
		 *             : one from it.
		 *
		 * Throughout most of the specification this is called the
		 * "Cumulative TSN ACK Point".  In this case, we
		 * ignore the advice in 12.2 in favour of the term
		 * used in the bulk of the text.  This value is hidden
		 * in tsn_map--we get it by calling sctp_tsnmap_get_ctsn().
		 */
		struct sctp_tsnmap tsn_map;
		__u8 _map[sctp_tsnmap_storage_size(SCTP_TSN_MAP_SIZE)];

		/* Do we need to sack the peer? */
		__u8    sack_needed;
		/* These are capabilities which our peer advertised.  */
		__u8	ecn_capable;     /* Can peer do ECN? */
		__u8	ipv4_address;    /* Peer understands IPv4 addresses? */
		__u8	ipv6_address;    /* Peer understands IPv6 addresses? */
		__u8	hostname_address;/* Peer understands DNS addresses? */
		sctp_inithdr_t i;
		int cookie_len;
		void *cookie;

		/* ADDIP Extention (ADDIP)		--xguo */
		/* <expected peer-serial-number> minus 1 (ADDIP sec. 4.2 C1) */
		__u32 addip_serial;
	} peer;

	/* State       : A state variable indicating what state the
	 *	       : association is in, i.e. COOKIE-WAIT,
	 *             : COOKIE-ECHOED, ESTABLISHED, SHUTDOWN-PENDING,
	 *             : SHUTDOWN-SENT, SHUTDOWN-RECEIVED, SHUTDOWN-ACK-SENT.
	 *
	 *              Note: No "CLOSED" state is illustrated since if a
	 *              association is "CLOSED" its TCB SHOULD be removed.
	 *
	 * 		In this implementation we DO have a CLOSED
	 *		state which is used during initiation and shutdown.
	 *
	 * 		State takes values from SCTP_STATE_*.
	 */
	sctp_state_t state;

	/* When did we enter this state?  */
	int state_timestamp;

	/* The cookie life I award for any cookie.  */
	struct timeval cookie_life;

	/* Overall     : The overall association error count.
	 * Error Count : [Clear this any time I get something.]
	 */
	int overall_error_count;

	/* Overall     : The threshold for this association that if
	 * Error       : the Overall Error Count reaches will cause
	 * Threshold   : this association to be torn down.
	 */
	int overall_error_threshold;

	/* These are the association's initial, max, and min RTO values.
	 * These values will be initialized by system defaults, but can
	 * be modified via the SCTP_RTOINFO socket option.
	 */
	__u32 rto_initial;
	__u32 rto_max;
	__u32 rto_min;

	/* Maximum number of new data packets that can be sent in a burst.  */
	int max_burst;

	/* This is the max_retrans value for the association.  This value will
	 * be initialized initialized from system defaults, but can be
	 * modified by the SCTP_ASSOCINFO socket option.
	 */
	int max_retrans;

	/* Maximum number of times the endpoint will retransmit INIT  */
	__u16 max_init_attempts;

	/* How many times have we resent an INIT? */
	__u16 init_retries;

	/* The largest timeout or RTO value to use in attempting an INIT */
	__u16 max_init_timeo;


	int timeouts[SCTP_NUM_TIMEOUT_TYPES];
	struct timer_list timers[SCTP_NUM_TIMEOUT_TYPES];

	/* Transport to which SHUTDOWN chunk was last sent.  */
	struct sctp_transport *shutdown_last_sent_to;

	/* Next TSN    : The next TSN number to be assigned to a new
	 *             : DATA chunk.  This is sent in the INIT or INIT
	 *             : ACK chunk to the peer and incremented each
	 *             : time a DATA chunk is assigned a TSN
	 *             : (normally just prior to transmit or during
	 *	       : fragmentation).
	 */
	__u32 next_tsn;

	/*
	 * Last Rcvd   : This is the last TSN received in sequence.  This value
	 * TSN         : is set initially by taking the peer's Initial TSN,
	 *             : received in the INIT or INIT ACK chunk, and
	 *             : subtracting one from it.
	 *
	 * Most of RFC 2960 refers to this as the Cumulative TSN Ack Point.
	 */

	__u32 ctsn_ack_point;

	/* Highest TSN that is acknowledged by incoming SACKs. */
	__u32 highest_sacked;

	/* The number of unacknowledged data chunks.  Reported through
	 * the SCTP_STATUS sockopt.
	 */
	__u16 unack_data;

	/* This is the association's receive buffer space.  This value is used
	 * to set a_rwnd field in an INIT or a SACK chunk.
	 */
	__u32 rwnd;

	/* This is the last advertised value of rwnd over a SACK chunk. */
	__u32 a_rwnd;

	/* Number of bytes by which the rwnd has slopped.  The rwnd is allowed
	 * to slop over a maximum of the association's frag_point.
	 */
	__u32 rwnd_over;

	/* This is the sndbuf size in use for the association.
	 * This corresponds to the sndbuf size for the association,
	 * as specified in the sk->sndbuf.
	 */
	int sndbuf_used;

	/* This is the wait queue head for send requests waiting on
	 * the association sndbuf space.
	 */
	wait_queue_head_t	wait;

	/* Association : The smallest PMTU discovered for all of the
	 * PMTU        : peer's transport addresses.
	 */
	__u32 pmtu;

	/* The message size at which SCTP fragmentation will occur. */
	__u32 frag_point;

	/* Ack State   : This flag indicates if the next received
	 *             : packet is to be responded to with a
	 *             : SACK. This is initializedto 0.  When a packet
	 *             : is received it is incremented. If this value
	 *             : reaches 2 or more, a SACK is sent and the
	 *             : value is reset to 0. Note: This is used only
	 *             : when no DATA chunks are received out of
	 *	       : order.  When DATA chunks are out of order,
	 *             : SACK's are not delayed (see Section 6).
	 */
	/* Do we need to send an ack?
	 * When counters[SctpCounterAckState] is above 1 we do!
	 */
	int counters[SCTP_NUMBER_COUNTERS];

	struct {
		__u16 stream;
		__u16 flags;
		__u32 ppid;
		__u32 context;
		__u32 timetolive;
	} defaults;

	/* This tracks outbound ssn for a given stream.  */
	struct sctp_ssnmap *ssnmap;

	/* All outbound chunks go through this structure.  */
	struct sctp_outq outqueue;

	/* A smart pipe that will handle reordering and fragmentation,
	 * as well as handle passing events up to the ULP.
	 */
	struct sctp_ulpq ulpq;

	/* Need to send an ECNE Chunk? */
	int need_ecne;

	/* Last TSN that caused an ECNE Chunk to be sent.  */
	__u32 last_ecne_tsn;

	/* Last TSN that caused a CWR Chunk to be sent.  */
	__u32 last_cwr_tsn;

	/* How many duplicated TSNs have we seen?  */
	int numduptsns;

	/* Number of seconds of idle time before an association is closed.  */
	__u32 autoclose;

	/* Name for debugging output... */
	char *debug_name;

	/* These are to support
	 * "SCTP Extensions for Dynamic Reconfiguration of IP Addresses
	 *  and Enforcement of Flow and Message Limits"
	 * <draft-ietf-tsvwg-addip-sctp-02.txt>
	 * or "ADDIP" for short.
	 */

	/* Is the ADDIP extension enabled for this association? */
	int addip_enable;

	/* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
	 *
	 * R1) One and only one ASCONF Chunk MAY be in transit and
	 * unacknowledged at any one time.  If a sender, after sending
	 * an ASCONF chunk, decides it needs to transfer another
	 * ASCONF Chunk, it MUST wait until the ASCONF-ACK Chunk
	 * returns from the previous ASCONF Chunk before sending a
	 * subsequent ASCONF. Note this restriction binds each side,
	 * so at any time two ASCONF may be in-transit on any given
	 * association (one sent from each endpoint).
	 *
	 * [This is our one-and-only-one ASCONF in flight.  If we do
	 * not have an ASCONF in flight, this is NULL.]
	 */
	sctp_chunk_t *addip_last_asconf;

	/* ADDIP Section 4.2 Upon reception of an ASCONF Chunk.
	 *
	 * IMPLEMENTATION NOTE: As an optimization a receiver may wish
	 * to save the last ASCONF-ACK for some predetermined period
	 * of time and instead of re-processing the ASCONF (with the
	 * same serial number) it may just re-transmit the
	 * ASCONF-ACK. It may wish to use the arrival of a new serial
	 * number to discard the previously saved ASCONF-ACK or any
	 * other means it may choose to expire the saved ASCONF-ACK.
	 *
	 * [This is our saved ASCONF-ACK.  We invalidate it when a new
	 * ASCONF serial number arrives.]
	 */
	sctp_chunk_t *addip_last_asconf_ack;

	/* These ASCONF chunks are waiting to be sent.
	 *
	 * These chunaks can't be pushed to outqueue until receiving
	 * ASCONF_ACK for the previous ASCONF indicated by
	 * addip_last_asconf, so as to guarantee that only one ASCONF
	 * is in flight at any time.
	 *
	 * ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
	 *
	 * In defining the ASCONF Chunk transfer procedures, it is
	 * essential that these transfers MUST NOT cause congestion
	 * within the network.  To achieve this, we place these
	 * restrictions on the transfer of ASCONF Chunks:
	 *
	 * R1) One and only one ASCONF Chunk MAY be in transit and
	 * unacknowledged at any one time.  If a sender, after sending
	 * an ASCONF chunk, decides it needs to transfer another
	 * ASCONF Chunk, it MUST wait until the ASCONF-ACK Chunk
	 * returns from the previous ASCONF Chunk before sending a
	 * subsequent ASCONF. Note this restriction binds each side,
	 * so at any time two ASCONF may be in-transit on any given
	 * association (one sent from each endpoint).
	 *
	 *
	 * [I really think this is EXACTLY the sort of intelligence
	 *  which already resides in sctp_outq.  Please move this
	 *  queue and its supporting logic down there.  --piggy]
	 */
	struct sk_buff_head addip_chunks;

	/* ADDIP Section 4.1 ASCONF Chunk Procedures
	 *
	 * A2) A serial number should be assigned to the Chunk. The
	 * serial number should be a monotonically increasing
	 * number. All serial numbers are defined to be initialized at
	 * the start of the association to the same value as the
	 * Initial TSN.
	 *
	 * [and]
	 *
	 * ADDIP
	 * 3.1.1  Address/Stream Configuration Change Chunk (ASCONF)
	 *
	 * Serial Number : 32 bits (unsigned integer)
	 *
	 * This value represents a Serial Number for the ASCONF
	 * Chunk. The valid range of Serial Number is from 0 to
	 * 4294967295 (2**32 - 1).  Serial Numbers wrap back to 0
	 * after reaching 4294967295.
	 */
	__u32 addip_serial;
};


/* An eyecatcher for determining if we are really looking at an
 * association data structure.
 */
enum {
	SCTP_ASSOC_EYECATCHER = 0xa550c123,
};

/* Recover the outter association structure. */
static inline sctp_association_t *sctp_assoc(sctp_endpoint_common_t *base)
{
	sctp_association_t *asoc;

	asoc = container_of(base, sctp_association_t, base);
	return asoc;
}

/* These are function signatures for manipulating associations.  */


sctp_association_t *
sctp_association_new(const sctp_endpoint_t *, const struct sock *,
		     sctp_scope_t scope, int priority);
sctp_association_t *
sctp_association_init(sctp_association_t *, const sctp_endpoint_t *,
		      const struct sock *, sctp_scope_t scope,
		      int priority);
void sctp_association_free(sctp_association_t *);
void sctp_association_put(sctp_association_t *);
void sctp_association_hold(sctp_association_t *);

struct sctp_transport *sctp_assoc_choose_shutdown_transport(sctp_association_t *);
void sctp_assoc_update_retran_path(sctp_association_t *);
struct sctp_transport *sctp_assoc_lookup_paddr(const sctp_association_t *,
					  const union sctp_addr *);
struct sctp_transport *sctp_assoc_add_peer(sctp_association_t *,
				     const union sctp_addr *address,
				     const int priority);
void sctp_assoc_control_transport(sctp_association_t *,
				  struct sctp_transport *,
				  sctp_transport_cmd_t, sctp_sn_error_t);
struct sctp_transport *sctp_assoc_lookup_tsn(sctp_association_t *, __u32);
struct sctp_transport *sctp_assoc_is_match(sctp_association_t *,
					   const union sctp_addr *,
					   const union sctp_addr *);
void sctp_assoc_migrate(sctp_association_t *, struct sock *);
void sctp_assoc_update(sctp_association_t *dst, sctp_association_t *src);

__u32 __sctp_association_get_next_tsn(sctp_association_t *);
__u32 __sctp_association_get_tsn_block(sctp_association_t *, int);
__u16 __sctp_association_get_next_ssn(sctp_association_t *, __u16 sid);

void sctp_assoc_sync_pmtu(sctp_association_t *);
void sctp_assoc_rwnd_increase(sctp_association_t *, int);
void sctp_assoc_rwnd_decrease(sctp_association_t *, int);

int sctp_assoc_set_bind_addr_from_ep(sctp_association_t *, int);
int sctp_assoc_set_bind_addr_from_cookie(sctp_association_t *,
					 sctp_cookie_t *, int);

int sctp_cmp_addr_exact(const union sctp_addr *ss1,
		        const union sctp_addr *ss2);
sctp_chunk_t *sctp_get_ecne_prepend(sctp_association_t *asoc);
sctp_chunk_t *sctp_get_no_prepend(sctp_association_t *asoc);

/* A convenience structure to parse out SCTP specific CMSGs. */
typedef struct sctp_cmsgs {
	struct sctp_initmsg *init;
	struct sctp_sndrcvinfo *info;
} sctp_cmsgs_t;

/* Structure for tracking memory objects */
typedef struct {
	char *label;
	atomic_t *counter;
} sctp_dbg_objcnt_entry_t;

#endif /* __sctp_structs_h__ */