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#include <linux/types.h>
45#include <linux/list.h>
46#include <linux/socket.h>
47#include <linux/ip.h>
48#include <linux/time.h>
49#include <linux/slab.h>
50#include <net/ip.h>
51#include <net/icmp.h>
52#include <net/snmp.h>
53#include <net/sock.h>
54#include <net/xfrm.h>
55#include <net/sctp/sctp.h>
56#include <net/sctp/sm.h>
57#include <net/sctp/checksum.h>
58#include <net/net_namespace.h>
59#include <linux/rhashtable.h>
60#include <net/sock_reuseport.h>
61
62
63static int sctp_rcv_ootb(struct sk_buff *);
64static struct sctp_association *__sctp_rcv_lookup(struct net *net,
65 struct sk_buff *skb,
66 const union sctp_addr *paddr,
67 const union sctp_addr *laddr,
68 struct sctp_transport **transportp);
69static struct sctp_endpoint *__sctp_rcv_lookup_endpoint(
70 struct net *net, struct sk_buff *skb,
71 const union sctp_addr *laddr,
72 const union sctp_addr *daddr);
73static struct sctp_association *__sctp_lookup_association(
74 struct net *net,
75 const union sctp_addr *local,
76 const union sctp_addr *peer,
77 struct sctp_transport **pt);
78
79static int sctp_add_backlog(struct sock *sk, struct sk_buff *skb);
80
81
82
83static inline int sctp_rcv_checksum(struct net *net, struct sk_buff *skb)
84{
85 struct sctphdr *sh = sctp_hdr(skb);
86 __le32 cmp = sh->checksum;
87 __le32 val = sctp_compute_cksum(skb, 0);
88
89 if (val != cmp) {
90
91 __SCTP_INC_STATS(net, SCTP_MIB_CHECKSUMERRORS);
92 return -1;
93 }
94 return 0;
95}
96
97
98
99
100int sctp_rcv(struct sk_buff *skb)
101{
102 struct sock *sk;
103 struct sctp_association *asoc;
104 struct sctp_endpoint *ep = NULL;
105 struct sctp_ep_common *rcvr;
106 struct sctp_transport *transport = NULL;
107 struct sctp_chunk *chunk;
108 union sctp_addr src;
109 union sctp_addr dest;
110 int family;
111 struct sctp_af *af;
112 struct net *net = dev_net(skb->dev);
113 bool is_gso = skb_is_gso(skb) && skb_is_gso_sctp(skb);
114
115 if (skb->pkt_type != PACKET_HOST)
116 goto discard_it;
117
118 __SCTP_INC_STATS(net, SCTP_MIB_INSCTPPACKS);
119
120
121
122
123 if (skb->len < sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr) +
124 skb_transport_offset(skb))
125 goto discard_it;
126
127
128
129
130
131 if ((!is_gso && skb_linearize(skb)) ||
132 !pskb_may_pull(skb, sizeof(struct sctphdr)))
133 goto discard_it;
134
135
136 __skb_pull(skb, skb_transport_offset(skb));
137
138 skb->csum_valid = 0;
139 if (skb_csum_unnecessary(skb))
140 __skb_decr_checksum_unnecessary(skb);
141 else if (!sctp_checksum_disable &&
142 !is_gso &&
143 sctp_rcv_checksum(net, skb) < 0)
144 goto discard_it;
145 skb->csum_valid = 1;
146
147 __skb_pull(skb, sizeof(struct sctphdr));
148
149 family = ipver2af(ip_hdr(skb)->version);
150 af = sctp_get_af_specific(family);
151 if (unlikely(!af))
152 goto discard_it;
153 SCTP_INPUT_CB(skb)->af = af;
154
155
156 af->from_skb(&src, skb, 1);
157 af->from_skb(&dest, skb, 0);
158
159
160
161
162
163
164
165
166
167
168
169
170 if (!af->addr_valid(&src, NULL, skb) ||
171 !af->addr_valid(&dest, NULL, skb))
172 goto discard_it;
173
174 asoc = __sctp_rcv_lookup(net, skb, &src, &dest, &transport);
175
176 if (!asoc)
177 ep = __sctp_rcv_lookup_endpoint(net, skb, &dest, &src);
178
179
180 rcvr = asoc ? &asoc->base : &ep->base;
181 sk = rcvr->sk;
182
183
184
185
186
187 if (sk->sk_bound_dev_if && (sk->sk_bound_dev_if != af->skb_iif(skb))) {
188 if (transport) {
189 sctp_transport_put(transport);
190 asoc = NULL;
191 transport = NULL;
192 } else {
193 sctp_endpoint_put(ep);
194 ep = NULL;
195 }
196 sk = net->sctp.ctl_sock;
197 ep = sctp_sk(sk)->ep;
198 sctp_endpoint_hold(ep);
199 rcvr = &ep->base;
200 }
201
202
203
204
205
206
207
208
209
210 if (!asoc) {
211 if (sctp_rcv_ootb(skb)) {
212 __SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
213 goto discard_release;
214 }
215 }
216
217 if (!xfrm_policy_check(sk, XFRM_POLICY_IN, skb, family))
218 goto discard_release;
219 nf_reset(skb);
220
221 if (sk_filter(sk, skb))
222 goto discard_release;
223
224
225 chunk = sctp_chunkify(skb, asoc, sk, GFP_ATOMIC);
226 if (!chunk)
227 goto discard_release;
228 SCTP_INPUT_CB(skb)->chunk = chunk;
229
230
231 chunk->rcvr = rcvr;
232
233
234 chunk->sctp_hdr = sctp_hdr(skb);
235
236
237 sctp_init_addrs(chunk, &src, &dest);
238
239
240 chunk->transport = transport;
241
242
243
244
245
246 bh_lock_sock(sk);
247
248 if (sk != rcvr->sk) {
249
250
251
252
253
254
255
256 bh_unlock_sock(sk);
257 sk = rcvr->sk;
258 bh_lock_sock(sk);
259 }
260
261 if (sock_owned_by_user(sk) || !sctp_newsk_ready(sk)) {
262 if (sctp_add_backlog(sk, skb)) {
263 bh_unlock_sock(sk);
264 sctp_chunk_free(chunk);
265 skb = NULL;
266 goto discard_release;
267 }
268 __SCTP_INC_STATS(net, SCTP_MIB_IN_PKT_BACKLOG);
269 } else {
270 __SCTP_INC_STATS(net, SCTP_MIB_IN_PKT_SOFTIRQ);
271 sctp_inq_push(&chunk->rcvr->inqueue, chunk);
272 }
273
274 bh_unlock_sock(sk);
275
276
277 if (transport)
278 sctp_transport_put(transport);
279 else
280 sctp_endpoint_put(ep);
281
282 return 0;
283
284discard_it:
285 __SCTP_INC_STATS(net, SCTP_MIB_IN_PKT_DISCARDS);
286 kfree_skb(skb);
287 return 0;
288
289discard_release:
290
291 if (transport)
292 sctp_transport_put(transport);
293 else
294 sctp_endpoint_put(ep);
295
296 goto discard_it;
297}
298
299
300
301
302
303
304int sctp_backlog_rcv(struct sock *sk, struct sk_buff *skb)
305{
306 struct sctp_chunk *chunk = SCTP_INPUT_CB(skb)->chunk;
307 struct sctp_inq *inqueue = &chunk->rcvr->inqueue;
308 struct sctp_transport *t = chunk->transport;
309 struct sctp_ep_common *rcvr = NULL;
310 int backloged = 0;
311
312 rcvr = chunk->rcvr;
313
314
315
316
317
318 if (rcvr->dead) {
319 sctp_chunk_free(chunk);
320 goto done;
321 }
322
323 if (unlikely(rcvr->sk != sk)) {
324
325
326
327
328
329
330
331
332
333
334
335 sk = rcvr->sk;
336 local_bh_disable();
337 bh_lock_sock(sk);
338
339 if (sock_owned_by_user(sk) || !sctp_newsk_ready(sk)) {
340 if (sk_add_backlog(sk, skb, sk->sk_rcvbuf))
341 sctp_chunk_free(chunk);
342 else
343 backloged = 1;
344 } else
345 sctp_inq_push(inqueue, chunk);
346
347 bh_unlock_sock(sk);
348 local_bh_enable();
349
350
351 if (backloged)
352 return 0;
353 } else {
354 if (!sctp_newsk_ready(sk)) {
355 if (!sk_add_backlog(sk, skb, sk->sk_rcvbuf))
356 return 0;
357 sctp_chunk_free(chunk);
358 } else {
359 sctp_inq_push(inqueue, chunk);
360 }
361 }
362
363done:
364
365 if (SCTP_EP_TYPE_ASSOCIATION == rcvr->type)
366 sctp_transport_put(t);
367 else if (SCTP_EP_TYPE_SOCKET == rcvr->type)
368 sctp_endpoint_put(sctp_ep(rcvr));
369 else
370 BUG();
371
372 return 0;
373}
374
375static int sctp_add_backlog(struct sock *sk, struct sk_buff *skb)
376{
377 struct sctp_chunk *chunk = SCTP_INPUT_CB(skb)->chunk;
378 struct sctp_transport *t = chunk->transport;
379 struct sctp_ep_common *rcvr = chunk->rcvr;
380 int ret;
381
382 ret = sk_add_backlog(sk, skb, sk->sk_rcvbuf);
383 if (!ret) {
384
385
386
387
388 if (SCTP_EP_TYPE_ASSOCIATION == rcvr->type)
389 sctp_transport_hold(t);
390 else if (SCTP_EP_TYPE_SOCKET == rcvr->type)
391 sctp_endpoint_hold(sctp_ep(rcvr));
392 else
393 BUG();
394 }
395 return ret;
396
397}
398
399
400void sctp_icmp_frag_needed(struct sock *sk, struct sctp_association *asoc,
401 struct sctp_transport *t, __u32 pmtu)
402{
403 if (!t ||
404 (t->pathmtu <= pmtu &&
405 t->pl.probe_size + sctp_transport_pl_hlen(t) <= pmtu))
406 return;
407
408 if (sock_owned_by_user(sk)) {
409 atomic_set(&t->mtu_info, pmtu);
410 asoc->pmtu_pending = 1;
411 t->pmtu_pending = 1;
412 return;
413 }
414
415 if (!(t->param_flags & SPP_PMTUD_ENABLE))
416
417
418
419
420 return;
421
422
423
424
425
426 if (!sctp_transport_update_pmtu(t, pmtu))
427 return;
428
429
430 sctp_assoc_sync_pmtu(asoc);
431
432
433 sctp_retransmit(&asoc->outqueue, t, SCTP_RTXR_PMTUD);
434}
435
436void sctp_icmp_redirect(struct sock *sk, struct sctp_transport *t,
437 struct sk_buff *skb)
438{
439 struct dst_entry *dst;
440
441 if (sock_owned_by_user(sk) || !t)
442 return;
443 dst = sctp_transport_dst_check(t);
444 if (dst)
445 dst->ops->redirect(dst, sk, skb);
446}
447
448
449
450
451
452
453
454
455
456
457
458
459void sctp_icmp_proto_unreachable(struct sock *sk,
460 struct sctp_association *asoc,
461 struct sctp_transport *t)
462{
463 if (sock_owned_by_user(sk)) {
464 if (timer_pending(&t->proto_unreach_timer))
465 return;
466 else {
467 if (!mod_timer(&t->proto_unreach_timer,
468 jiffies + (HZ/20)))
469 sctp_transport_hold(t);
470 }
471 } else {
472 struct net *net = sock_net(sk);
473
474 pr_debug("%s: unrecognized next header type "
475 "encountered!\n", __func__);
476
477 if (del_timer(&t->proto_unreach_timer))
478 sctp_transport_put(t);
479
480 sctp_do_sm(net, SCTP_EVENT_T_OTHER,
481 SCTP_ST_OTHER(SCTP_EVENT_ICMP_PROTO_UNREACH),
482 asoc->state, asoc->ep, asoc, t,
483 GFP_ATOMIC);
484 }
485}
486
487
488struct sock *sctp_err_lookup(struct net *net, int family, struct sk_buff *skb,
489 struct sctphdr *sctphdr,
490 struct sctp_association **app,
491 struct sctp_transport **tpp)
492{
493 struct sctp_init_chunk *chunkhdr, _chunkhdr;
494 union sctp_addr saddr;
495 union sctp_addr daddr;
496 struct sctp_af *af;
497 struct sock *sk = NULL;
498 struct sctp_association *asoc;
499 struct sctp_transport *transport = NULL;
500 __u32 vtag = ntohl(sctphdr->vtag);
501
502 *app = NULL; *tpp = NULL;
503
504 af = sctp_get_af_specific(family);
505 if (unlikely(!af)) {
506 return NULL;
507 }
508
509
510 af->from_skb(&saddr, skb, 1);
511 af->from_skb(&daddr, skb, 0);
512
513
514
515
516 asoc = __sctp_lookup_association(net, &saddr, &daddr, &transport);
517 if (!asoc)
518 return NULL;
519
520 sk = asoc->base.sk;
521
522
523
524
525
526
527
528
529
530
531
532
533
534 if (vtag == 0) {
535
536 chunkhdr = skb_header_pointer(skb, skb_transport_offset(skb) +
537 sizeof(struct sctphdr),
538 sizeof(struct sctp_chunkhdr) +
539 sizeof(__be32), &_chunkhdr);
540 if (!chunkhdr ||
541 chunkhdr->chunk_hdr.type != SCTP_CID_INIT ||
542 ntohl(chunkhdr->init_hdr.init_tag) != asoc->c.my_vtag)
543 goto out;
544
545 } else if (vtag != asoc->c.peer_vtag) {
546 goto out;
547 }
548
549 bh_lock_sock(sk);
550
551
552
553
554 if (sock_owned_by_user(sk))
555 __NET_INC_STATS(net, LINUX_MIB_LOCKDROPPEDICMPS);
556
557 *app = asoc;
558 *tpp = transport;
559 return sk;
560
561out:
562 sctp_transport_put(transport);
563 return NULL;
564}
565
566
567void sctp_err_finish(struct sock *sk, struct sctp_transport *t)
568{
569 bh_unlock_sock(sk);
570 sctp_transport_put(t);
571}
572
573static void sctp_v4_err_handle(struct sctp_transport *t, struct sk_buff *skb,
574 __u8 type, __u8 code, __u32 info)
575{
576 struct sctp_association *asoc = t->asoc;
577 struct sock *sk = asoc->base.sk;
578 int err = 0;
579
580 switch (type) {
581 case ICMP_PARAMETERPROB:
582 err = EPROTO;
583 break;
584 case ICMP_DEST_UNREACH:
585 if (code > NR_ICMP_UNREACH)
586 return;
587 if (code == ICMP_FRAG_NEEDED) {
588 sctp_icmp_frag_needed(sk, asoc, t, SCTP_TRUNC4(info));
589 return;
590 }
591 if (code == ICMP_PROT_UNREACH) {
592 sctp_icmp_proto_unreachable(sk, asoc, t);
593 return;
594 }
595 err = icmp_err_convert[code].errno;
596 break;
597 case ICMP_TIME_EXCEEDED:
598 if (code == ICMP_EXC_FRAGTIME)
599 return;
600
601 err = EHOSTUNREACH;
602 break;
603 case ICMP_REDIRECT:
604 sctp_icmp_redirect(sk, t, skb);
605 default:
606 return;
607 }
608 if (!sock_owned_by_user(sk) && inet_sk(sk)->recverr) {
609 sk->sk_err = err;
610 sk_error_report(sk);
611 } else {
612 sk->sk_err_soft = err;
613 }
614}
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631int sctp_v4_err(struct sk_buff *skb, __u32 info)
632{
633 const struct iphdr *iph = (const struct iphdr *)skb->data;
634 const int type = icmp_hdr(skb)->type;
635 const int code = icmp_hdr(skb)->code;
636 struct net *net = dev_net(skb->dev);
637 struct sctp_transport *transport;
638 struct sctp_association *asoc;
639 __u16 saveip, savesctp;
640 struct sock *sk;
641
642
643 saveip = skb->network_header;
644 savesctp = skb->transport_header;
645 skb_reset_network_header(skb);
646 skb_set_transport_header(skb, iph->ihl * 4);
647 sk = sctp_err_lookup(net, AF_INET, skb, sctp_hdr(skb), &asoc, &transport);
648
649 skb->network_header = saveip;
650 skb->transport_header = savesctp;
651 if (!sk) {
652 __ICMP_INC_STATS(net, ICMP_MIB_INERRORS);
653 return -ENOENT;
654 }
655
656 sctp_v4_err_handle(transport, skb, type, code, info);
657 sctp_err_finish(sk, transport);
658
659 return 0;
660}
661
662int sctp_udp_v4_err(struct sock *sk, struct sk_buff *skb)
663{
664 struct net *net = dev_net(skb->dev);
665 struct sctp_association *asoc;
666 struct sctp_transport *t;
667 struct icmphdr *hdr;
668 __u32 info = 0;
669
670 skb->transport_header += sizeof(struct udphdr);
671 sk = sctp_err_lookup(net, AF_INET, skb, sctp_hdr(skb), &asoc, &t);
672 if (!sk) {
673 __ICMP_INC_STATS(net, ICMP_MIB_INERRORS);
674 return -ENOENT;
675 }
676
677 skb->transport_header -= sizeof(struct udphdr);
678 hdr = (struct icmphdr *)(skb_network_header(skb) - sizeof(struct icmphdr));
679 if (hdr->type == ICMP_REDIRECT) {
680
681 sctp_err_finish(sk, t);
682 return 0;
683 }
684 if (hdr->type == ICMP_DEST_UNREACH && hdr->code == ICMP_FRAG_NEEDED)
685 info = ntohs(hdr->un.frag.mtu);
686 sctp_v4_err_handle(t, skb, hdr->type, hdr->code, info);
687
688 sctp_err_finish(sk, t);
689 return 1;
690}
691
692
693
694
695
696
697
698
699
700
701
702
703
704static int sctp_rcv_ootb(struct sk_buff *skb)
705{
706 struct sctp_chunkhdr *ch, _ch;
707 int ch_end, offset = 0;
708
709
710 do {
711
712 if (offset + sizeof(_ch) > skb->len)
713 break;
714
715 ch = skb_header_pointer(skb, offset, sizeof(*ch), &_ch);
716
717
718 if (!ch || ntohs(ch->length) < sizeof(_ch))
719 break;
720
721 ch_end = offset + SCTP_PAD4(ntohs(ch->length));
722 if (ch_end > skb->len)
723 break;
724
725
726
727
728
729 if (SCTP_CID_ABORT == ch->type)
730 goto discard;
731
732
733
734
735
736 if (SCTP_CID_SHUTDOWN_COMPLETE == ch->type)
737 goto discard;
738
739
740
741
742
743
744 if (SCTP_CID_INIT == ch->type && (void *)ch != skb->data)
745 goto discard;
746
747 offset = ch_end;
748 } while (ch_end < skb->len);
749
750 return 0;
751
752discard:
753 return 1;
754}
755
756
757static int __sctp_hash_endpoint(struct sctp_endpoint *ep)
758{
759 struct sock *sk = ep->base.sk;
760 struct net *net = sock_net(sk);
761 struct sctp_hashbucket *head;
762
763 ep->hashent = sctp_ep_hashfn(net, ep->base.bind_addr.port);
764 head = &sctp_ep_hashtable[ep->hashent];
765
766 if (sk->sk_reuseport) {
767 bool any = sctp_is_ep_boundall(sk);
768 struct sctp_endpoint *ep2;
769 struct list_head *list;
770 int cnt = 0, err = 1;
771
772 list_for_each(list, &ep->base.bind_addr.address_list)
773 cnt++;
774
775 sctp_for_each_hentry(ep2, &head->chain) {
776 struct sock *sk2 = ep2->base.sk;
777
778 if (!net_eq(sock_net(sk2), net) || sk2 == sk ||
779 !uid_eq(sock_i_uid(sk2), sock_i_uid(sk)) ||
780 !sk2->sk_reuseport)
781 continue;
782
783 err = sctp_bind_addrs_check(sctp_sk(sk2),
784 sctp_sk(sk), cnt);
785 if (!err) {
786 err = reuseport_add_sock(sk, sk2, any);
787 if (err)
788 return err;
789 break;
790 } else if (err < 0) {
791 return err;
792 }
793 }
794
795 if (err) {
796 err = reuseport_alloc(sk, any);
797 if (err)
798 return err;
799 }
800 }
801
802 write_lock(&head->lock);
803 hlist_add_head(&ep->node, &head->chain);
804 write_unlock(&head->lock);
805 return 0;
806}
807
808
809int sctp_hash_endpoint(struct sctp_endpoint *ep)
810{
811 int err;
812
813 local_bh_disable();
814 err = __sctp_hash_endpoint(ep);
815 local_bh_enable();
816
817 return err;
818}
819
820
821static void __sctp_unhash_endpoint(struct sctp_endpoint *ep)
822{
823 struct sock *sk = ep->base.sk;
824 struct sctp_hashbucket *head;
825
826 ep->hashent = sctp_ep_hashfn(sock_net(sk), ep->base.bind_addr.port);
827
828 head = &sctp_ep_hashtable[ep->hashent];
829
830 if (rcu_access_pointer(sk->sk_reuseport_cb))
831 reuseport_detach_sock(sk);
832
833 write_lock(&head->lock);
834 hlist_del_init(&ep->node);
835 write_unlock(&head->lock);
836}
837
838
839void sctp_unhash_endpoint(struct sctp_endpoint *ep)
840{
841 local_bh_disable();
842 __sctp_unhash_endpoint(ep);
843 local_bh_enable();
844}
845
846static inline __u32 sctp_hashfn(const struct net *net, __be16 lport,
847 const union sctp_addr *paddr, __u32 seed)
848{
849 __u32 addr;
850
851 if (paddr->sa.sa_family == AF_INET6)
852 addr = jhash(&paddr->v6.sin6_addr, 16, seed);
853 else
854 addr = (__force __u32)paddr->v4.sin_addr.s_addr;
855
856 return jhash_3words(addr, ((__force __u32)paddr->v4.sin_port) << 16 |
857 (__force __u32)lport, net_hash_mix(net), seed);
858}
859
860
861static struct sctp_endpoint *__sctp_rcv_lookup_endpoint(
862 struct net *net, struct sk_buff *skb,
863 const union sctp_addr *laddr,
864 const union sctp_addr *paddr)
865{
866 struct sctp_hashbucket *head;
867 struct sctp_endpoint *ep;
868 struct sock *sk;
869 __be16 lport;
870 int hash;
871
872 lport = laddr->v4.sin_port;
873 hash = sctp_ep_hashfn(net, ntohs(lport));
874 head = &sctp_ep_hashtable[hash];
875 read_lock(&head->lock);
876 sctp_for_each_hentry(ep, &head->chain) {
877 if (sctp_endpoint_is_match(ep, net, laddr))
878 goto hit;
879 }
880
881 ep = sctp_sk(net->sctp.ctl_sock)->ep;
882
883hit:
884 sk = ep->base.sk;
885 if (sk->sk_reuseport) {
886 __u32 phash = sctp_hashfn(net, lport, paddr, 0);
887
888 sk = reuseport_select_sock(sk, phash, skb,
889 sizeof(struct sctphdr));
890 if (sk)
891 ep = sctp_sk(sk)->ep;
892 }
893 sctp_endpoint_hold(ep);
894 read_unlock(&head->lock);
895 return ep;
896}
897
898
899struct sctp_hash_cmp_arg {
900 const union sctp_addr *paddr;
901 const struct net *net;
902 __be16 lport;
903};
904
905static inline int sctp_hash_cmp(struct rhashtable_compare_arg *arg,
906 const void *ptr)
907{
908 struct sctp_transport *t = (struct sctp_transport *)ptr;
909 const struct sctp_hash_cmp_arg *x = arg->key;
910 int err = 1;
911
912 if (!sctp_cmp_addr_exact(&t->ipaddr, x->paddr))
913 return err;
914 if (!sctp_transport_hold(t))
915 return err;
916
917 if (!net_eq(t->asoc->base.net, x->net))
918 goto out;
919 if (x->lport != htons(t->asoc->base.bind_addr.port))
920 goto out;
921
922 err = 0;
923out:
924 sctp_transport_put(t);
925 return err;
926}
927
928static inline __u32 sctp_hash_obj(const void *data, u32 len, u32 seed)
929{
930 const struct sctp_transport *t = data;
931
932 return sctp_hashfn(t->asoc->base.net,
933 htons(t->asoc->base.bind_addr.port),
934 &t->ipaddr, seed);
935}
936
937static inline __u32 sctp_hash_key(const void *data, u32 len, u32 seed)
938{
939 const struct sctp_hash_cmp_arg *x = data;
940
941 return sctp_hashfn(x->net, x->lport, x->paddr, seed);
942}
943
944static const struct rhashtable_params sctp_hash_params = {
945 .head_offset = offsetof(struct sctp_transport, node),
946 .hashfn = sctp_hash_key,
947 .obj_hashfn = sctp_hash_obj,
948 .obj_cmpfn = sctp_hash_cmp,
949 .automatic_shrinking = true,
950};
951
952int sctp_transport_hashtable_init(void)
953{
954 return rhltable_init(&sctp_transport_hashtable, &sctp_hash_params);
955}
956
957void sctp_transport_hashtable_destroy(void)
958{
959 rhltable_destroy(&sctp_transport_hashtable);
960}
961
962int sctp_hash_transport(struct sctp_transport *t)
963{
964 struct sctp_transport *transport;
965 struct rhlist_head *tmp, *list;
966 struct sctp_hash_cmp_arg arg;
967 int err;
968
969 if (t->asoc->temp)
970 return 0;
971
972 arg.net = sock_net(t->asoc->base.sk);
973 arg.paddr = &t->ipaddr;
974 arg.lport = htons(t->asoc->base.bind_addr.port);
975
976 rcu_read_lock();
977 list = rhltable_lookup(&sctp_transport_hashtable, &arg,
978 sctp_hash_params);
979
980 rhl_for_each_entry_rcu(transport, tmp, list, node)
981 if (transport->asoc->ep == t->asoc->ep) {
982 rcu_read_unlock();
983 return -EEXIST;
984 }
985 rcu_read_unlock();
986
987 err = rhltable_insert_key(&sctp_transport_hashtable, &arg,
988 &t->node, sctp_hash_params);
989 if (err)
990 pr_err_once("insert transport fail, errno %d\n", err);
991
992 return err;
993}
994
995void sctp_unhash_transport(struct sctp_transport *t)
996{
997 if (t->asoc->temp)
998 return;
999
1000 rhltable_remove(&sctp_transport_hashtable, &t->node,
1001 sctp_hash_params);
1002}
1003
1004
1005struct sctp_transport *sctp_addrs_lookup_transport(
1006 struct net *net,
1007 const union sctp_addr *laddr,
1008 const union sctp_addr *paddr)
1009{
1010 struct rhlist_head *tmp, *list;
1011 struct sctp_transport *t;
1012 struct sctp_hash_cmp_arg arg = {
1013 .paddr = paddr,
1014 .net = net,
1015 .lport = laddr->v4.sin_port,
1016 };
1017
1018 list = rhltable_lookup(&sctp_transport_hashtable, &arg,
1019 sctp_hash_params);
1020
1021 rhl_for_each_entry_rcu(t, tmp, list, node) {
1022 if (!sctp_transport_hold(t))
1023 continue;
1024
1025 if (sctp_bind_addr_match(&t->asoc->base.bind_addr,
1026 laddr, sctp_sk(t->asoc->base.sk)))
1027 return t;
1028 sctp_transport_put(t);
1029 }
1030
1031 return NULL;
1032}
1033
1034
1035struct sctp_transport *sctp_epaddr_lookup_transport(
1036 const struct sctp_endpoint *ep,
1037 const union sctp_addr *paddr)
1038{
1039 struct net *net = sock_net(ep->base.sk);
1040 struct rhlist_head *tmp, *list;
1041 struct sctp_transport *t;
1042 struct sctp_hash_cmp_arg arg = {
1043 .paddr = paddr,
1044 .net = net,
1045 .lport = htons(ep->base.bind_addr.port),
1046 };
1047
1048 list = rhltable_lookup(&sctp_transport_hashtable, &arg,
1049 sctp_hash_params);
1050
1051 rhl_for_each_entry_rcu(t, tmp, list, node)
1052 if (ep == t->asoc->ep)
1053 return t;
1054
1055 return NULL;
1056}
1057
1058
1059static struct sctp_association *__sctp_lookup_association(
1060 struct net *net,
1061 const union sctp_addr *local,
1062 const union sctp_addr *peer,
1063 struct sctp_transport **pt)
1064{
1065 struct sctp_transport *t;
1066 struct sctp_association *asoc = NULL;
1067
1068 t = sctp_addrs_lookup_transport(net, local, peer);
1069 if (!t)
1070 goto out;
1071
1072 asoc = t->asoc;
1073 *pt = t;
1074
1075out:
1076 return asoc;
1077}
1078
1079
1080static
1081struct sctp_association *sctp_lookup_association(struct net *net,
1082 const union sctp_addr *laddr,
1083 const union sctp_addr *paddr,
1084 struct sctp_transport **transportp)
1085{
1086 struct sctp_association *asoc;
1087
1088 rcu_read_lock();
1089 asoc = __sctp_lookup_association(net, laddr, paddr, transportp);
1090 rcu_read_unlock();
1091
1092 return asoc;
1093}
1094
1095
1096bool sctp_has_association(struct net *net,
1097 const union sctp_addr *laddr,
1098 const union sctp_addr *paddr)
1099{
1100 struct sctp_transport *transport;
1101
1102 if (sctp_lookup_association(net, laddr, paddr, &transport)) {
1103 sctp_transport_put(transport);
1104 return true;
1105 }
1106
1107 return false;
1108}
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128static struct sctp_association *__sctp_rcv_init_lookup(struct net *net,
1129 struct sk_buff *skb,
1130 const union sctp_addr *laddr, struct sctp_transport **transportp)
1131{
1132 struct sctp_association *asoc;
1133 union sctp_addr addr;
1134 union sctp_addr *paddr = &addr;
1135 struct sctphdr *sh = sctp_hdr(skb);
1136 union sctp_params params;
1137 struct sctp_init_chunk *init;
1138 struct sctp_af *af;
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156 init = (struct sctp_init_chunk *)skb->data;
1157
1158
1159 sctp_walk_params(params, init, init_hdr.params) {
1160
1161
1162 af = sctp_get_af_specific(param_type2af(params.p->type));
1163 if (!af)
1164 continue;
1165
1166 if (!af->from_addr_param(paddr, params.addr, sh->source, 0))
1167 continue;
1168
1169 asoc = __sctp_lookup_association(net, laddr, paddr, transportp);
1170 if (asoc)
1171 return asoc;
1172 }
1173
1174 return NULL;
1175}
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191static struct sctp_association *__sctp_rcv_asconf_lookup(
1192 struct net *net,
1193 struct sctp_chunkhdr *ch,
1194 const union sctp_addr *laddr,
1195 __be16 peer_port,
1196 struct sctp_transport **transportp)
1197{
1198 struct sctp_addip_chunk *asconf = (struct sctp_addip_chunk *)ch;
1199 struct sctp_af *af;
1200 union sctp_addr_param *param;
1201 union sctp_addr paddr;
1202
1203 if (ntohs(ch->length) < sizeof(*asconf) + sizeof(struct sctp_paramhdr))
1204 return NULL;
1205
1206
1207 param = (union sctp_addr_param *)(asconf + 1);
1208
1209 af = sctp_get_af_specific(param_type2af(param->p.type));
1210 if (unlikely(!af))
1211 return NULL;
1212
1213 if (!af->from_addr_param(&paddr, param, peer_port, 0))
1214 return NULL;
1215
1216 return __sctp_lookup_association(net, laddr, &paddr, transportp);
1217}
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229static struct sctp_association *__sctp_rcv_walk_lookup(struct net *net,
1230 struct sk_buff *skb,
1231 const union sctp_addr *laddr,
1232 struct sctp_transport **transportp)
1233{
1234 struct sctp_association *asoc = NULL;
1235 struct sctp_chunkhdr *ch;
1236 int have_auth = 0;
1237 unsigned int chunk_num = 1;
1238 __u8 *ch_end;
1239
1240
1241
1242
1243 ch = (struct sctp_chunkhdr *)skb->data;
1244 do {
1245
1246 if (ntohs(ch->length) < sizeof(*ch))
1247 break;
1248
1249 ch_end = ((__u8 *)ch) + SCTP_PAD4(ntohs(ch->length));
1250 if (ch_end > skb_tail_pointer(skb))
1251 break;
1252
1253 switch (ch->type) {
1254 case SCTP_CID_AUTH:
1255 have_auth = chunk_num;
1256 break;
1257
1258 case SCTP_CID_COOKIE_ECHO:
1259
1260
1261
1262
1263
1264
1265
1266 if (have_auth == 1 && chunk_num == 2)
1267 return NULL;
1268 break;
1269
1270 case SCTP_CID_ASCONF:
1271 if (have_auth || net->sctp.addip_noauth)
1272 asoc = __sctp_rcv_asconf_lookup(
1273 net, ch, laddr,
1274 sctp_hdr(skb)->source,
1275 transportp);
1276 default:
1277 break;
1278 }
1279
1280 if (asoc)
1281 break;
1282
1283 ch = (struct sctp_chunkhdr *)ch_end;
1284 chunk_num++;
1285 } while (ch_end + sizeof(*ch) < skb_tail_pointer(skb));
1286
1287 return asoc;
1288}
1289
1290
1291
1292
1293
1294
1295
1296static struct sctp_association *__sctp_rcv_lookup_harder(struct net *net,
1297 struct sk_buff *skb,
1298 const union sctp_addr *laddr,
1299 struct sctp_transport **transportp)
1300{
1301 struct sctp_chunkhdr *ch;
1302
1303
1304
1305
1306
1307
1308 if (skb_is_gso(skb) && skb_is_gso_sctp(skb))
1309 return NULL;
1310
1311 ch = (struct sctp_chunkhdr *)skb->data;
1312
1313
1314
1315
1316
1317
1318 if (SCTP_PAD4(ntohs(ch->length)) > skb->len)
1319 return NULL;
1320
1321
1322 if (ch->type == SCTP_CID_INIT || ch->type == SCTP_CID_INIT_ACK)
1323 return __sctp_rcv_init_lookup(net, skb, laddr, transportp);
1324
1325 return __sctp_rcv_walk_lookup(net, skb, laddr, transportp);
1326}
1327
1328
1329static struct sctp_association *__sctp_rcv_lookup(struct net *net,
1330 struct sk_buff *skb,
1331 const union sctp_addr *paddr,
1332 const union sctp_addr *laddr,
1333 struct sctp_transport **transportp)
1334{
1335 struct sctp_association *asoc;
1336
1337 asoc = __sctp_lookup_association(net, laddr, paddr, transportp);
1338 if (asoc)
1339 goto out;
1340
1341
1342
1343
1344
1345 asoc = __sctp_rcv_lookup_harder(net, skb, laddr, transportp);
1346 if (asoc)
1347 goto out;
1348
1349 if (paddr->sa.sa_family == AF_INET)
1350 pr_debug("sctp: asoc not found for src:%pI4:%d dst:%pI4:%d\n",
1351 &laddr->v4.sin_addr, ntohs(laddr->v4.sin_port),
1352 &paddr->v4.sin_addr, ntohs(paddr->v4.sin_port));
1353 else
1354 pr_debug("sctp: asoc not found for src:%pI6:%d dst:%pI6:%d\n",
1355 &laddr->v6.sin6_addr, ntohs(laddr->v6.sin6_port),
1356 &paddr->v6.sin6_addr, ntohs(paddr->v6.sin6_port));
1357
1358out:
1359 return asoc;
1360}
1361