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#include <linux/types.h>
52#include <linux/fcntl.h>
53#include <linux/poll.h>
54#include <linux/init.h>
55
56#include <linux/slab.h>
57#include <linux/in.h>
58#include <net/ipv6.h>
59#include <net/sctp/sctp.h>
60#include <net/sctp/sm.h>
61
62
63static void sctp_assoc_bh_rcv(struct work_struct *work);
64static void sctp_assoc_free_asconf_acks(struct sctp_association *asoc);
65
66
67
68
69
70static struct sctp_association *sctp_association_init(struct sctp_association *asoc,
71 const struct sctp_endpoint *ep,
72 const struct sock *sk,
73 sctp_scope_t scope,
74 gfp_t gfp)
75{
76 struct sctp_sock *sp;
77 int i;
78 sctp_paramhdr_t *p;
79 int err;
80
81
82 sp = sctp_sk((struct sock *)sk);
83
84
85 memset(asoc, 0, sizeof(struct sctp_association));
86
87
88 asoc->ep = (struct sctp_endpoint *)ep;
89 sctp_endpoint_hold(asoc->ep);
90
91
92 asoc->base.sk = (struct sock *)sk;
93 sock_hold(asoc->base.sk);
94
95
96 asoc->base.type = SCTP_EP_TYPE_ASSOCIATION;
97
98
99 atomic_set(&asoc->base.refcnt, 1);
100 asoc->base.dead = 0;
101 asoc->base.malloced = 0;
102
103
104 sctp_bind_addr_init(&asoc->base.bind_addr, ep->base.bind_addr.port);
105
106 asoc->state = SCTP_STATE_CLOSED;
107
108
109
110
111 asoc->cookie_life.tv_sec = sp->assocparams.sasoc_cookie_life / 1000;
112 asoc->cookie_life.tv_usec = (sp->assocparams.sasoc_cookie_life % 1000)
113 * 1000;
114 asoc->frag_point = 0;
115
116
117
118
119 asoc->max_retrans = sp->assocparams.sasoc_asocmaxrxt;
120 asoc->rto_initial = msecs_to_jiffies(sp->rtoinfo.srto_initial);
121 asoc->rto_max = msecs_to_jiffies(sp->rtoinfo.srto_max);
122 asoc->rto_min = msecs_to_jiffies(sp->rtoinfo.srto_min);
123
124 asoc->overall_error_count = 0;
125
126
127
128
129 asoc->hbinterval = msecs_to_jiffies(sp->hbinterval);
130
131
132 asoc->pathmaxrxt = sp->pathmaxrxt;
133
134
135 asoc->pathmtu = sp->pathmtu;
136
137
138 asoc->sackdelay = msecs_to_jiffies(sp->sackdelay);
139 asoc->sackfreq = sp->sackfreq;
140
141
142
143
144 asoc->param_flags = sp->param_flags;
145
146
147
148
149 asoc->max_burst = sp->max_burst;
150
151
152 asoc->timeouts[SCTP_EVENT_TIMEOUT_NONE] = 0;
153 asoc->timeouts[SCTP_EVENT_TIMEOUT_T1_COOKIE] = asoc->rto_initial;
154 asoc->timeouts[SCTP_EVENT_TIMEOUT_T1_INIT] = asoc->rto_initial;
155 asoc->timeouts[SCTP_EVENT_TIMEOUT_T2_SHUTDOWN] = asoc->rto_initial;
156 asoc->timeouts[SCTP_EVENT_TIMEOUT_T3_RTX] = 0;
157 asoc->timeouts[SCTP_EVENT_TIMEOUT_T4_RTO] = 0;
158
159
160
161
162
163 asoc->timeouts[SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD]
164 = 5 * asoc->rto_max;
165
166 asoc->timeouts[SCTP_EVENT_TIMEOUT_HEARTBEAT] = 0;
167 asoc->timeouts[SCTP_EVENT_TIMEOUT_SACK] = asoc->sackdelay;
168 asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE] =
169 sp->autoclose * HZ;
170
171
172 for (i = SCTP_EVENT_TIMEOUT_NONE; i < SCTP_NUM_TIMEOUT_TYPES; ++i)
173 setup_timer(&asoc->timers[i], sctp_timer_events[i],
174 (unsigned long)asoc);
175
176
177
178
179
180 asoc->c.sinit_max_instreams = sp->initmsg.sinit_max_instreams;
181 asoc->c.sinit_num_ostreams = sp->initmsg.sinit_num_ostreams;
182 asoc->max_init_attempts = sp->initmsg.sinit_max_attempts;
183
184 asoc->max_init_timeo =
185 msecs_to_jiffies(sp->initmsg.sinit_max_init_timeo);
186
187
188
189
190 asoc->ssnmap = NULL;
191
192
193
194
195
196
197 if ((sk->sk_rcvbuf/2) < SCTP_DEFAULT_MINWINDOW)
198 asoc->rwnd = SCTP_DEFAULT_MINWINDOW;
199 else
200 asoc->rwnd = sk->sk_rcvbuf/2;
201
202 asoc->a_rwnd = asoc->rwnd;
203
204 asoc->rwnd_over = 0;
205
206
207 asoc->peer.rwnd = SCTP_DEFAULT_MAXWINDOW;
208
209
210 asoc->sndbuf_used = 0;
211
212
213 atomic_set(&asoc->rmem_alloc, 0);
214
215 init_waitqueue_head(&asoc->wait);
216
217 asoc->c.my_vtag = sctp_generate_tag(ep);
218 asoc->peer.i.init_tag = 0;
219 asoc->c.peer_vtag = 0;
220 asoc->c.my_ttag = 0;
221 asoc->c.peer_ttag = 0;
222 asoc->c.my_port = ep->base.bind_addr.port;
223
224 asoc->c.initial_tsn = sctp_generate_tsn(ep);
225
226 asoc->next_tsn = asoc->c.initial_tsn;
227
228 asoc->ctsn_ack_point = asoc->next_tsn - 1;
229 asoc->adv_peer_ack_point = asoc->ctsn_ack_point;
230 asoc->highest_sacked = asoc->ctsn_ack_point;
231 asoc->last_cwr_tsn = asoc->ctsn_ack_point;
232 asoc->unack_data = 0;
233
234
235
236
237
238
239
240
241
242
243
244 asoc->addip_serial = asoc->c.initial_tsn;
245
246 INIT_LIST_HEAD(&asoc->addip_chunk_list);
247 INIT_LIST_HEAD(&asoc->asconf_ack_list);
248
249
250 INIT_LIST_HEAD(&asoc->peer.transport_addr_list);
251 asoc->peer.transport_count = 0;
252
253
254
255
256
257
258
259
260
261
262
263
264 asoc->peer.sack_needed = 1;
265 asoc->peer.sack_cnt = 0;
266
267
268
269
270
271
272 asoc->peer.asconf_capable = 0;
273 if (sctp_addip_noauth)
274 asoc->peer.asconf_capable = 1;
275
276
277 sctp_inq_init(&asoc->base.inqueue);
278 sctp_inq_set_th_handler(&asoc->base.inqueue, sctp_assoc_bh_rcv);
279
280
281 sctp_outq_init(asoc, &asoc->outqueue);
282
283 if (!sctp_ulpq_init(&asoc->ulpq, asoc))
284 goto fail_init;
285
286 memset(&asoc->peer.tsn_map, 0, sizeof(struct sctp_tsnmap));
287
288 asoc->need_ecne = 0;
289
290 asoc->assoc_id = 0;
291
292
293
294
295 asoc->peer.ipv4_address = 1;
296 asoc->peer.ipv6_address = 1;
297 INIT_LIST_HEAD(&asoc->asocs);
298
299 asoc->autoclose = sp->autoclose;
300
301 asoc->default_stream = sp->default_stream;
302 asoc->default_ppid = sp->default_ppid;
303 asoc->default_flags = sp->default_flags;
304 asoc->default_context = sp->default_context;
305 asoc->default_timetolive = sp->default_timetolive;
306 asoc->default_rcv_context = sp->default_rcv_context;
307
308
309 INIT_LIST_HEAD(&asoc->endpoint_shared_keys);
310 err = sctp_auth_asoc_copy_shkeys(ep, asoc, gfp);
311 if (err)
312 goto fail_init;
313
314 asoc->active_key_id = ep->active_key_id;
315 asoc->asoc_shared_key = NULL;
316
317 asoc->default_hmac_id = 0;
318
319 if (ep->auth_hmacs_list)
320 memcpy(asoc->c.auth_hmacs, ep->auth_hmacs_list,
321 ntohs(ep->auth_hmacs_list->param_hdr.length));
322 if (ep->auth_chunk_list)
323 memcpy(asoc->c.auth_chunks, ep->auth_chunk_list,
324 ntohs(ep->auth_chunk_list->param_hdr.length));
325
326
327 p = (sctp_paramhdr_t *)asoc->c.auth_random;
328 p->type = SCTP_PARAM_RANDOM;
329 p->length = htons(sizeof(sctp_paramhdr_t) + SCTP_AUTH_RANDOM_LENGTH);
330 get_random_bytes(p+1, SCTP_AUTH_RANDOM_LENGTH);
331
332 return asoc;
333
334fail_init:
335 sctp_endpoint_put(asoc->ep);
336 sock_put(asoc->base.sk);
337 return NULL;
338}
339
340
341struct sctp_association *sctp_association_new(const struct sctp_endpoint *ep,
342 const struct sock *sk,
343 sctp_scope_t scope,
344 gfp_t gfp)
345{
346 struct sctp_association *asoc;
347
348 asoc = t_new(struct sctp_association, gfp);
349 if (!asoc)
350 goto fail;
351
352 if (!sctp_association_init(asoc, ep, sk, scope, gfp))
353 goto fail_init;
354
355 asoc->base.malloced = 1;
356 SCTP_DBG_OBJCNT_INC(assoc);
357 SCTP_DEBUG_PRINTK("Created asoc %p\n", asoc);
358
359 return asoc;
360
361fail_init:
362 kfree(asoc);
363fail:
364 return NULL;
365}
366
367
368
369
370void sctp_association_free(struct sctp_association *asoc)
371{
372 struct sock *sk = asoc->base.sk;
373 struct sctp_transport *transport;
374 struct list_head *pos, *temp;
375 int i;
376
377
378
379
380 if (!asoc->temp) {
381 list_del(&asoc->asocs);
382
383
384
385
386 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
387 sk->sk_ack_backlog--;
388 }
389
390
391
392
393 asoc->base.dead = 1;
394
395
396 sctp_outq_free(&asoc->outqueue);
397
398
399 sctp_ulpq_free(&asoc->ulpq);
400
401
402 sctp_inq_free(&asoc->base.inqueue);
403
404 sctp_tsnmap_free(&asoc->peer.tsn_map);
405
406
407 sctp_ssnmap_free(asoc->ssnmap);
408
409
410 sctp_bind_addr_free(&asoc->base.bind_addr);
411
412
413
414
415
416
417 for (i = SCTP_EVENT_TIMEOUT_NONE; i < SCTP_NUM_TIMEOUT_TYPES; ++i) {
418 if (timer_pending(&asoc->timers[i]) &&
419 del_timer(&asoc->timers[i]))
420 sctp_association_put(asoc);
421 }
422
423
424 kfree(asoc->peer.cookie);
425 kfree(asoc->peer.peer_random);
426 kfree(asoc->peer.peer_chunks);
427 kfree(asoc->peer.peer_hmacs);
428
429
430 list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
431 transport = list_entry(pos, struct sctp_transport, transports);
432 list_del(pos);
433 sctp_transport_free(transport);
434 }
435
436 asoc->peer.transport_count = 0;
437
438
439 sctp_assoc_free_asconf_acks(asoc);
440
441
442 if (asoc->addip_last_asconf)
443 sctp_chunk_free(asoc->addip_last_asconf);
444
445
446 sctp_auth_destroy_keys(&asoc->endpoint_shared_keys);
447
448
449 sctp_auth_key_put(asoc->asoc_shared_key);
450
451 sctp_association_put(asoc);
452}
453
454
455static void sctp_association_destroy(struct sctp_association *asoc)
456{
457 SCTP_ASSERT(asoc->base.dead, "Assoc is not dead", return);
458
459 sctp_endpoint_put(asoc->ep);
460 sock_put(asoc->base.sk);
461
462 if (asoc->assoc_id != 0) {
463 spin_lock_bh(&sctp_assocs_id_lock);
464 idr_remove(&sctp_assocs_id, asoc->assoc_id);
465 spin_unlock_bh(&sctp_assocs_id_lock);
466 }
467
468 WARN_ON(atomic_read(&asoc->rmem_alloc));
469
470 if (asoc->base.malloced) {
471 kfree(asoc);
472 SCTP_DBG_OBJCNT_DEC(assoc);
473 }
474}
475
476
477void sctp_assoc_set_primary(struct sctp_association *asoc,
478 struct sctp_transport *transport)
479{
480 int changeover = 0;
481
482
483
484
485 if (asoc->peer.primary_path != NULL &&
486 asoc->peer.primary_path != transport)
487 changeover = 1 ;
488
489 asoc->peer.primary_path = transport;
490
491
492 memcpy(&asoc->peer.primary_addr, &transport->ipaddr,
493 sizeof(union sctp_addr));
494
495
496
497
498 if ((transport->state == SCTP_ACTIVE) ||
499 (transport->state == SCTP_UNKNOWN))
500 asoc->peer.active_path = transport;
501
502
503
504
505
506
507
508
509
510
511
512
513 if (transport->cacc.changeover_active)
514 transport->cacc.cycling_changeover = changeover;
515
516
517
518
519 transport->cacc.changeover_active = changeover;
520
521
522
523
524 transport->cacc.next_tsn_at_change = asoc->next_tsn;
525}
526
527
528void sctp_assoc_rm_peer(struct sctp_association *asoc,
529 struct sctp_transport *peer)
530{
531 struct list_head *pos;
532 struct sctp_transport *transport;
533
534 SCTP_DEBUG_PRINTK_IPADDR("sctp_assoc_rm_peer:association %p addr: ",
535 " port: %d\n",
536 asoc,
537 (&peer->ipaddr),
538 ntohs(peer->ipaddr.v4.sin_port));
539
540
541
542
543 if (asoc->peer.retran_path == peer)
544 sctp_assoc_update_retran_path(asoc);
545
546
547 list_del(&peer->transports);
548
549
550 pos = asoc->peer.transport_addr_list.next;
551 transport = list_entry(pos, struct sctp_transport, transports);
552
553
554 if (asoc->peer.primary_path == peer)
555 sctp_assoc_set_primary(asoc, transport);
556 if (asoc->peer.active_path == peer)
557 asoc->peer.active_path = transport;
558 if (asoc->peer.last_data_from == peer)
559 asoc->peer.last_data_from = transport;
560
561
562
563
564
565
566 if (asoc->init_last_sent_to == peer)
567 asoc->init_last_sent_to = NULL;
568
569 asoc->peer.transport_count--;
570
571 sctp_transport_free(peer);
572}
573
574
575struct sctp_transport *sctp_assoc_add_peer(struct sctp_association *asoc,
576 const union sctp_addr *addr,
577 const gfp_t gfp,
578 const int peer_state)
579{
580 struct sctp_transport *peer;
581 struct sctp_sock *sp;
582 unsigned short port;
583
584 sp = sctp_sk(asoc->base.sk);
585
586
587 port = ntohs(addr->v4.sin_port);
588
589 SCTP_DEBUG_PRINTK_IPADDR("sctp_assoc_add_peer:association %p addr: ",
590 " port: %d state:%d\n",
591 asoc,
592 addr,
593 port,
594 peer_state);
595
596
597 if (0 == asoc->peer.port)
598 asoc->peer.port = port;
599
600
601 peer = sctp_assoc_lookup_paddr(asoc, addr);
602 if (peer) {
603
604
605
606
607 if (peer->state == SCTP_UNKNOWN) {
608 peer->state = SCTP_ACTIVE;
609 }
610 return peer;
611 }
612
613 peer = sctp_transport_new(addr, gfp);
614 if (!peer)
615 return NULL;
616
617 sctp_transport_set_owner(peer, asoc);
618
619
620
621
622 peer->hbinterval = asoc->hbinterval;
623
624
625 peer->pathmaxrxt = asoc->pathmaxrxt;
626
627
628
629
630 peer->sackdelay = asoc->sackdelay;
631 peer->sackfreq = asoc->sackfreq;
632
633
634
635
636 peer->param_flags = asoc->param_flags;
637
638
639 if (peer->param_flags & SPP_PMTUD_ENABLE)
640 sctp_transport_pmtu(peer);
641 else if (asoc->pathmtu)
642 peer->pathmtu = asoc->pathmtu;
643 else
644 peer->pathmtu = SCTP_DEFAULT_MAXSEGMENT;
645
646
647
648
649
650
651 if (asoc->pathmtu)
652 asoc->pathmtu = min_t(int, peer->pathmtu, asoc->pathmtu);
653 else
654 asoc->pathmtu = peer->pathmtu;
655
656 SCTP_DEBUG_PRINTK("sctp_assoc_add_peer:association %p PMTU set to "
657 "%d\n", asoc, asoc->pathmtu);
658 peer->pmtu_pending = 0;
659
660 asoc->frag_point = sctp_frag_point(sp, asoc->pathmtu);
661
662
663
664
665 sctp_packet_init(&peer->packet, peer, asoc->base.bind_addr.port,
666 asoc->peer.port);
667
668
669
670
671
672
673
674
675
676
677
678 peer->cwnd = min(4*asoc->pathmtu, max_t(__u32, 2*asoc->pathmtu, 4380));
679
680
681
682
683
684 peer->ssthresh = SCTP_DEFAULT_MAXWINDOW;
685
686 peer->partial_bytes_acked = 0;
687 peer->flight_size = 0;
688
689
690 peer->rto = asoc->rto_initial;
691
692
693 peer->state = peer_state;
694
695
696 list_add_tail(&peer->transports, &asoc->peer.transport_addr_list);
697 asoc->peer.transport_count++;
698
699
700 if (!asoc->peer.primary_path) {
701 sctp_assoc_set_primary(asoc, peer);
702 asoc->peer.retran_path = peer;
703 }
704
705 if (asoc->peer.active_path == asoc->peer.retran_path) {
706 asoc->peer.retran_path = peer;
707 }
708
709 return peer;
710}
711
712
713void sctp_assoc_del_peer(struct sctp_association *asoc,
714 const union sctp_addr *addr)
715{
716 struct list_head *pos;
717 struct list_head *temp;
718 struct sctp_transport *transport;
719
720 list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
721 transport = list_entry(pos, struct sctp_transport, transports);
722 if (sctp_cmp_addr_exact(addr, &transport->ipaddr)) {
723
724 sctp_assoc_rm_peer(asoc, transport);
725 break;
726 }
727 }
728}
729
730
731struct sctp_transport *sctp_assoc_lookup_paddr(
732 const struct sctp_association *asoc,
733 const union sctp_addr *address)
734{
735 struct sctp_transport *t;
736
737
738
739 list_for_each_entry(t, &asoc->peer.transport_addr_list,
740 transports) {
741 if (sctp_cmp_addr_exact(address, &t->ipaddr))
742 return t;
743 }
744
745 return NULL;
746}
747
748
749void sctp_assoc_del_nonprimary_peers(struct sctp_association *asoc,
750 struct sctp_transport *primary)
751{
752 struct sctp_transport *temp;
753 struct sctp_transport *t;
754
755 list_for_each_entry_safe(t, temp, &asoc->peer.transport_addr_list,
756 transports) {
757
758 if (t != primary)
759 sctp_assoc_rm_peer(asoc, t);
760 }
761
762 return;
763}
764
765
766
767
768
769void sctp_assoc_control_transport(struct sctp_association *asoc,
770 struct sctp_transport *transport,
771 sctp_transport_cmd_t command,
772 sctp_sn_error_t error)
773{
774 struct sctp_transport *t = NULL;
775 struct sctp_transport *first;
776 struct sctp_transport *second;
777 struct sctp_ulpevent *event;
778 struct sockaddr_storage addr;
779 int spc_state = 0;
780
781
782 switch (command) {
783 case SCTP_TRANSPORT_UP:
784
785
786
787
788 if (SCTP_UNCONFIRMED == transport->state &&
789 SCTP_HEARTBEAT_SUCCESS == error)
790 spc_state = SCTP_ADDR_CONFIRMED;
791 else
792 spc_state = SCTP_ADDR_AVAILABLE;
793 transport->state = SCTP_ACTIVE;
794 break;
795
796 case SCTP_TRANSPORT_DOWN:
797
798
799
800 if (transport->state != SCTP_UNCONFIRMED)
801 transport->state = SCTP_INACTIVE;
802
803 spc_state = SCTP_ADDR_UNREACHABLE;
804 break;
805
806 default:
807 return;
808 }
809
810
811
812
813 memset(&addr, 0, sizeof(struct sockaddr_storage));
814 memcpy(&addr, &transport->ipaddr, transport->af_specific->sockaddr_len);
815 event = sctp_ulpevent_make_peer_addr_change(asoc, &addr,
816 0, spc_state, error, GFP_ATOMIC);
817 if (event)
818 sctp_ulpq_tail_event(&asoc->ulpq, event);
819
820
821
822
823
824
825
826
827
828 first = NULL; second = NULL;
829
830 list_for_each_entry(t, &asoc->peer.transport_addr_list,
831 transports) {
832
833 if ((t->state == SCTP_INACTIVE) ||
834 (t->state == SCTP_UNCONFIRMED))
835 continue;
836 if (!first || t->last_time_heard > first->last_time_heard) {
837 second = first;
838 first = t;
839 }
840 if (!second || t->last_time_heard > second->last_time_heard)
841 second = t;
842 }
843
844
845
846
847
848
849
850
851
852
853
854 if (((asoc->peer.primary_path->state == SCTP_ACTIVE) ||
855 (asoc->peer.primary_path->state == SCTP_UNKNOWN)) &&
856 first != asoc->peer.primary_path) {
857 second = first;
858 first = asoc->peer.primary_path;
859 }
860
861
862
863
864 if (!first) {
865 first = asoc->peer.primary_path;
866 second = asoc->peer.primary_path;
867 }
868
869
870 asoc->peer.active_path = first;
871 asoc->peer.retran_path = second;
872}
873
874
875void sctp_association_hold(struct sctp_association *asoc)
876{
877 atomic_inc(&asoc->base.refcnt);
878}
879
880
881
882
883void sctp_association_put(struct sctp_association *asoc)
884{
885 if (atomic_dec_and_test(&asoc->base.refcnt))
886 sctp_association_destroy(asoc);
887}
888
889
890
891
892__u32 sctp_association_get_next_tsn(struct sctp_association *asoc)
893{
894
895
896
897
898
899 __u32 retval = asoc->next_tsn;
900 asoc->next_tsn++;
901 asoc->unack_data++;
902
903 return retval;
904}
905
906
907
908
909int sctp_cmp_addr_exact(const union sctp_addr *ss1,
910 const union sctp_addr *ss2)
911{
912 struct sctp_af *af;
913
914 af = sctp_get_af_specific(ss1->sa.sa_family);
915 if (unlikely(!af))
916 return 0;
917
918 return af->cmp_addr(ss1, ss2);
919}
920
921
922
923
924
925struct sctp_chunk *sctp_get_ecne_prepend(struct sctp_association *asoc)
926{
927 struct sctp_chunk *chunk;
928
929
930
931
932 if (asoc->need_ecne)
933 chunk = sctp_make_ecne(asoc, asoc->last_ecne_tsn);
934 else
935 chunk = NULL;
936
937 return chunk;
938}
939
940
941
942
943struct sctp_transport *sctp_assoc_lookup_tsn(struct sctp_association *asoc,
944 __u32 tsn)
945{
946 struct sctp_transport *active;
947 struct sctp_transport *match;
948 struct sctp_transport *transport;
949 struct sctp_chunk *chunk;
950 __be32 key = htonl(tsn);
951
952 match = NULL;
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969 active = asoc->peer.active_path;
970
971 list_for_each_entry(chunk, &active->transmitted,
972 transmitted_list) {
973
974 if (key == chunk->subh.data_hdr->tsn) {
975 match = active;
976 goto out;
977 }
978 }
979
980
981 list_for_each_entry(transport, &asoc->peer.transport_addr_list,
982 transports) {
983
984 if (transport == active)
985 break;
986 list_for_each_entry(chunk, &transport->transmitted,
987 transmitted_list) {
988 if (key == chunk->subh.data_hdr->tsn) {
989 match = transport;
990 goto out;
991 }
992 }
993 }
994out:
995 return match;
996}
997
998
999struct sctp_transport *sctp_assoc_is_match(struct sctp_association *asoc,
1000 const union sctp_addr *laddr,
1001 const union sctp_addr *paddr)
1002{
1003 struct sctp_transport *transport;
1004
1005 if ((htons(asoc->base.bind_addr.port) == laddr->v4.sin_port) &&
1006 (htons(asoc->peer.port) == paddr->v4.sin_port)) {
1007 transport = sctp_assoc_lookup_paddr(asoc, paddr);
1008 if (!transport)
1009 goto out;
1010
1011 if (sctp_bind_addr_match(&asoc->base.bind_addr, laddr,
1012 sctp_sk(asoc->base.sk)))
1013 goto out;
1014 }
1015 transport = NULL;
1016
1017out:
1018 return transport;
1019}
1020
1021
1022static void sctp_assoc_bh_rcv(struct work_struct *work)
1023{
1024 struct sctp_association *asoc =
1025 container_of(work, struct sctp_association,
1026 base.inqueue.immediate);
1027 struct sctp_endpoint *ep;
1028 struct sctp_chunk *chunk;
1029 struct sock *sk;
1030 struct sctp_inq *inqueue;
1031 int state;
1032 sctp_subtype_t subtype;
1033 int error = 0;
1034
1035
1036 ep = asoc->ep;
1037 sk = asoc->base.sk;
1038
1039 inqueue = &asoc->base.inqueue;
1040 sctp_association_hold(asoc);
1041 while (NULL != (chunk = sctp_inq_pop(inqueue))) {
1042 state = asoc->state;
1043 subtype = SCTP_ST_CHUNK(chunk->chunk_hdr->type);
1044
1045
1046
1047
1048
1049
1050
1051
1052 if (sctp_auth_recv_cid(subtype.chunk, asoc) && !chunk->auth)
1053 continue;
1054
1055
1056
1057
1058 if (sctp_chunk_is_data(chunk))
1059 asoc->peer.last_data_from = chunk->transport;
1060 else
1061 SCTP_INC_STATS(SCTP_MIB_INCTRLCHUNKS);
1062
1063 if (chunk->transport)
1064 chunk->transport->last_time_heard = jiffies;
1065
1066
1067 error = sctp_do_sm(SCTP_EVENT_T_CHUNK, subtype,
1068 state, ep, asoc, chunk, GFP_ATOMIC);
1069
1070
1071
1072
1073 if (asoc->base.dead)
1074 break;
1075
1076
1077 if (error && chunk)
1078 chunk->pdiscard = 1;
1079 }
1080 sctp_association_put(asoc);
1081}
1082
1083
1084void sctp_assoc_migrate(struct sctp_association *assoc, struct sock *newsk)
1085{
1086 struct sctp_sock *newsp = sctp_sk(newsk);
1087 struct sock *oldsk = assoc->base.sk;
1088
1089
1090
1091
1092 list_del_init(&assoc->asocs);
1093
1094
1095 if (sctp_style(oldsk, TCP))
1096 oldsk->sk_ack_backlog--;
1097
1098
1099 sctp_endpoint_put(assoc->ep);
1100 sock_put(assoc->base.sk);
1101
1102
1103 assoc->ep = newsp->ep;
1104 sctp_endpoint_hold(assoc->ep);
1105
1106
1107 assoc->base.sk = newsk;
1108 sock_hold(assoc->base.sk);
1109
1110
1111 sctp_endpoint_add_asoc(newsp->ep, assoc);
1112}
1113
1114
1115void sctp_assoc_update(struct sctp_association *asoc,
1116 struct sctp_association *new)
1117{
1118 struct sctp_transport *trans;
1119 struct list_head *pos, *temp;
1120
1121
1122 asoc->c = new->c;
1123 asoc->peer.rwnd = new->peer.rwnd;
1124 asoc->peer.sack_needed = new->peer.sack_needed;
1125 asoc->peer.i = new->peer.i;
1126 sctp_tsnmap_init(&asoc->peer.tsn_map, SCTP_TSN_MAP_INITIAL,
1127 asoc->peer.i.initial_tsn, GFP_ATOMIC);
1128
1129
1130 list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
1131 trans = list_entry(pos, struct sctp_transport, transports);
1132 if (!sctp_assoc_lookup_paddr(new, &trans->ipaddr))
1133 sctp_assoc_del_peer(asoc, &trans->ipaddr);
1134
1135 if (asoc->state >= SCTP_STATE_ESTABLISHED)
1136 sctp_transport_reset(trans);
1137 }
1138
1139
1140
1141
1142
1143
1144 if (asoc->state >= SCTP_STATE_ESTABLISHED) {
1145 asoc->next_tsn = new->next_tsn;
1146 asoc->ctsn_ack_point = new->ctsn_ack_point;
1147 asoc->adv_peer_ack_point = new->adv_peer_ack_point;
1148
1149
1150
1151
1152 sctp_ssnmap_clear(asoc->ssnmap);
1153
1154
1155
1156
1157
1158 sctp_ulpq_flush(&asoc->ulpq);
1159
1160
1161
1162
1163
1164 asoc->overall_error_count = 0;
1165
1166 } else {
1167
1168 list_for_each_entry(trans, &new->peer.transport_addr_list,
1169 transports) {
1170 if (!sctp_assoc_lookup_paddr(asoc, &trans->ipaddr))
1171 sctp_assoc_add_peer(asoc, &trans->ipaddr,
1172 GFP_ATOMIC, trans->state);
1173 }
1174
1175 asoc->ctsn_ack_point = asoc->next_tsn - 1;
1176 asoc->adv_peer_ack_point = asoc->ctsn_ack_point;
1177 if (!asoc->ssnmap) {
1178
1179 asoc->ssnmap = new->ssnmap;
1180 new->ssnmap = NULL;
1181 }
1182
1183 if (!asoc->assoc_id) {
1184
1185
1186
1187 sctp_assoc_set_id(asoc, GFP_ATOMIC);
1188 }
1189 }
1190
1191
1192
1193
1194 kfree(asoc->peer.peer_random);
1195 asoc->peer.peer_random = new->peer.peer_random;
1196 new->peer.peer_random = NULL;
1197
1198 kfree(asoc->peer.peer_chunks);
1199 asoc->peer.peer_chunks = new->peer.peer_chunks;
1200 new->peer.peer_chunks = NULL;
1201
1202 kfree(asoc->peer.peer_hmacs);
1203 asoc->peer.peer_hmacs = new->peer.peer_hmacs;
1204 new->peer.peer_hmacs = NULL;
1205
1206 sctp_auth_key_put(asoc->asoc_shared_key);
1207 sctp_auth_asoc_init_active_key(asoc, GFP_ATOMIC);
1208}
1209
1210
1211
1212
1213
1214
1215void sctp_assoc_update_retran_path(struct sctp_association *asoc)
1216{
1217 struct sctp_transport *t, *next;
1218 struct list_head *head = &asoc->peer.transport_addr_list;
1219 struct list_head *pos;
1220
1221 if (asoc->peer.transport_count == 1)
1222 return;
1223
1224
1225 t = asoc->peer.retran_path;
1226 pos = &t->transports;
1227 next = NULL;
1228
1229 while (1) {
1230
1231 if (pos->next == head)
1232 pos = head->next;
1233 else
1234 pos = pos->next;
1235
1236 t = list_entry(pos, struct sctp_transport, transports);
1237
1238
1239
1240
1241
1242 if (t == asoc->peer.retran_path) {
1243 t = next;
1244 break;
1245 }
1246
1247
1248
1249 if ((t->state == SCTP_ACTIVE) ||
1250 (t->state == SCTP_UNKNOWN)) {
1251 break;
1252 } else {
1253
1254
1255
1256 if (!next)
1257 next = t;
1258 }
1259 }
1260
1261 asoc->peer.retran_path = t;
1262
1263 SCTP_DEBUG_PRINTK_IPADDR("sctp_assoc_update_retran_path:association"
1264 " %p addr: ",
1265 " port: %d\n",
1266 asoc,
1267 (&t->ipaddr),
1268 ntohs(t->ipaddr.v4.sin_port));
1269}
1270
1271
1272struct sctp_transport *sctp_assoc_choose_init_transport(
1273 struct sctp_association *asoc)
1274{
1275 struct sctp_transport *t;
1276
1277
1278
1279
1280 if (!asoc->init_last_sent_to) {
1281 t = asoc->peer.active_path;
1282 } else {
1283 if (asoc->init_last_sent_to == asoc->peer.retran_path)
1284 sctp_assoc_update_retran_path(asoc);
1285 t = asoc->peer.retran_path;
1286 }
1287
1288 SCTP_DEBUG_PRINTK_IPADDR("sctp_assoc_update_retran_path:association"
1289 " %p addr: ",
1290 " port: %d\n",
1291 asoc,
1292 (&t->ipaddr),
1293 ntohs(t->ipaddr.v4.sin_port));
1294
1295 return t;
1296}
1297
1298
1299struct sctp_transport *sctp_assoc_choose_shutdown_transport(
1300 struct sctp_association *asoc)
1301{
1302
1303
1304
1305
1306 if (!asoc->shutdown_last_sent_to)
1307 return asoc->peer.active_path;
1308 else {
1309 if (asoc->shutdown_last_sent_to == asoc->peer.retran_path)
1310 sctp_assoc_update_retran_path(asoc);
1311 return asoc->peer.retran_path;
1312 }
1313
1314}
1315
1316
1317
1318
1319void sctp_assoc_sync_pmtu(struct sctp_association *asoc)
1320{
1321 struct sctp_transport *t;
1322 __u32 pmtu = 0;
1323
1324 if (!asoc)
1325 return;
1326
1327
1328 list_for_each_entry(t, &asoc->peer.transport_addr_list,
1329 transports) {
1330 if (t->pmtu_pending && t->dst) {
1331 sctp_transport_update_pmtu(t, dst_mtu(t->dst));
1332 t->pmtu_pending = 0;
1333 }
1334 if (!pmtu || (t->pathmtu < pmtu))
1335 pmtu = t->pathmtu;
1336 }
1337
1338 if (pmtu) {
1339 struct sctp_sock *sp = sctp_sk(asoc->base.sk);
1340 asoc->pathmtu = pmtu;
1341 asoc->frag_point = sctp_frag_point(sp, pmtu);
1342 }
1343
1344 SCTP_DEBUG_PRINTK("%s: asoc:%p, pmtu:%d, frag_point:%d\n",
1345 __func__, asoc, asoc->pathmtu, asoc->frag_point);
1346}
1347
1348
1349static inline int sctp_peer_needs_update(struct sctp_association *asoc)
1350{
1351 switch (asoc->state) {
1352 case SCTP_STATE_ESTABLISHED:
1353 case SCTP_STATE_SHUTDOWN_PENDING:
1354 case SCTP_STATE_SHUTDOWN_RECEIVED:
1355 case SCTP_STATE_SHUTDOWN_SENT:
1356 if ((asoc->rwnd > asoc->a_rwnd) &&
1357 ((asoc->rwnd - asoc->a_rwnd) >=
1358 min_t(__u32, (asoc->base.sk->sk_rcvbuf >> 1), asoc->pathmtu)))
1359 return 1;
1360 break;
1361 default:
1362 break;
1363 }
1364 return 0;
1365}
1366
1367
1368void sctp_assoc_rwnd_increase(struct sctp_association *asoc, unsigned len)
1369{
1370 struct sctp_chunk *sack;
1371 struct timer_list *timer;
1372
1373 if (asoc->rwnd_over) {
1374 if (asoc->rwnd_over >= len) {
1375 asoc->rwnd_over -= len;
1376 } else {
1377 asoc->rwnd += (len - asoc->rwnd_over);
1378 asoc->rwnd_over = 0;
1379 }
1380 } else {
1381 asoc->rwnd += len;
1382 }
1383
1384 SCTP_DEBUG_PRINTK("%s: asoc %p rwnd increased by %d to (%u, %u) "
1385 "- %u\n", __func__, asoc, len, asoc->rwnd,
1386 asoc->rwnd_over, asoc->a_rwnd);
1387
1388
1389
1390
1391
1392
1393 if (sctp_peer_needs_update(asoc)) {
1394 asoc->a_rwnd = asoc->rwnd;
1395 SCTP_DEBUG_PRINTK("%s: Sending window update SACK- asoc: %p "
1396 "rwnd: %u a_rwnd: %u\n", __func__,
1397 asoc, asoc->rwnd, asoc->a_rwnd);
1398 sack = sctp_make_sack(asoc);
1399 if (!sack)
1400 return;
1401
1402 asoc->peer.sack_needed = 0;
1403
1404 sctp_outq_tail(&asoc->outqueue, sack);
1405
1406
1407 timer = &asoc->timers[SCTP_EVENT_TIMEOUT_SACK];
1408 if (timer_pending(timer) && del_timer(timer))
1409 sctp_association_put(asoc);
1410 }
1411}
1412
1413
1414void sctp_assoc_rwnd_decrease(struct sctp_association *asoc, unsigned len)
1415{
1416 SCTP_ASSERT(asoc->rwnd, "rwnd zero", return);
1417 SCTP_ASSERT(!asoc->rwnd_over, "rwnd_over not zero", return);
1418 if (asoc->rwnd >= len) {
1419 asoc->rwnd -= len;
1420 } else {
1421 asoc->rwnd_over = len - asoc->rwnd;
1422 asoc->rwnd = 0;
1423 }
1424 SCTP_DEBUG_PRINTK("%s: asoc %p rwnd decreased by %d to (%u, %u)\n",
1425 __func__, asoc, len, asoc->rwnd,
1426 asoc->rwnd_over);
1427}
1428
1429
1430
1431
1432int sctp_assoc_set_bind_addr_from_ep(struct sctp_association *asoc,
1433 gfp_t gfp)
1434{
1435 sctp_scope_t scope;
1436 int flags;
1437
1438
1439
1440
1441 scope = sctp_scope(&asoc->peer.active_path->ipaddr);
1442 flags = (PF_INET6 == asoc->base.sk->sk_family) ? SCTP_ADDR6_ALLOWED : 0;
1443 if (asoc->peer.ipv4_address)
1444 flags |= SCTP_ADDR4_PEERSUPP;
1445 if (asoc->peer.ipv6_address)
1446 flags |= SCTP_ADDR6_PEERSUPP;
1447
1448 return sctp_bind_addr_copy(&asoc->base.bind_addr,
1449 &asoc->ep->base.bind_addr,
1450 scope, gfp, flags);
1451}
1452
1453
1454int sctp_assoc_set_bind_addr_from_cookie(struct sctp_association *asoc,
1455 struct sctp_cookie *cookie,
1456 gfp_t gfp)
1457{
1458 int var_size2 = ntohs(cookie->peer_init->chunk_hdr.length);
1459 int var_size3 = cookie->raw_addr_list_len;
1460 __u8 *raw = (__u8 *)cookie->peer_init + var_size2;
1461
1462 return sctp_raw_to_bind_addrs(&asoc->base.bind_addr, raw, var_size3,
1463 asoc->ep->base.bind_addr.port, gfp);
1464}
1465
1466
1467int sctp_assoc_lookup_laddr(struct sctp_association *asoc,
1468 const union sctp_addr *laddr)
1469{
1470 int found = 0;
1471
1472 if ((asoc->base.bind_addr.port == ntohs(laddr->v4.sin_port)) &&
1473 sctp_bind_addr_match(&asoc->base.bind_addr, laddr,
1474 sctp_sk(asoc->base.sk)))
1475 found = 1;
1476
1477 return found;
1478}
1479
1480
1481int sctp_assoc_set_id(struct sctp_association *asoc, gfp_t gfp)
1482{
1483 int assoc_id;
1484 int error = 0;
1485retry:
1486 if (unlikely(!idr_pre_get(&sctp_assocs_id, gfp)))
1487 return -ENOMEM;
1488
1489 spin_lock_bh(&sctp_assocs_id_lock);
1490 error = idr_get_new_above(&sctp_assocs_id, (void *)asoc,
1491 1, &assoc_id);
1492 spin_unlock_bh(&sctp_assocs_id_lock);
1493 if (error == -EAGAIN)
1494 goto retry;
1495 else if (error)
1496 return error;
1497
1498 asoc->assoc_id = (sctp_assoc_t) assoc_id;
1499 return error;
1500}
1501
1502
1503static void sctp_assoc_free_asconf_acks(struct sctp_association *asoc)
1504{
1505 struct sctp_chunk *ack;
1506 struct sctp_chunk *tmp;
1507
1508 list_for_each_entry_safe(ack, tmp, &asoc->asconf_ack_list,
1509 transmitted_list) {
1510 list_del_init(&ack->transmitted_list);
1511 sctp_chunk_free(ack);
1512 }
1513}
1514
1515
1516void sctp_assoc_clean_asconf_ack_cache(const struct sctp_association *asoc)
1517{
1518 struct sctp_chunk *ack;
1519 struct sctp_chunk *tmp;
1520
1521
1522
1523
1524 list_for_each_entry_safe(ack, tmp, &asoc->asconf_ack_list,
1525 transmitted_list) {
1526 if (ack->subh.addip_hdr->serial ==
1527 htonl(asoc->peer.addip_serial))
1528 break;
1529
1530 list_del_init(&ack->transmitted_list);
1531 sctp_chunk_free(ack);
1532 }
1533}
1534
1535
1536struct sctp_chunk *sctp_assoc_lookup_asconf_ack(
1537 const struct sctp_association *asoc,
1538 __be32 serial)
1539{
1540 struct sctp_chunk *ack;
1541
1542
1543
1544
1545 list_for_each_entry(ack, &asoc->asconf_ack_list, transmitted_list) {
1546 if (ack->subh.addip_hdr->serial == serial) {
1547 sctp_chunk_hold(ack);
1548 return ack;
1549 }
1550 }
1551
1552 return NULL;
1553}
1554