]> asedeno.scripts.mit.edu Git - linux.git/blob - net/sctp/input.c
00f995e377956a06910ca20caf8c59db82b2526c
[linux.git] / net / sctp / input.c
1 /* SCTP kernel implementation
2  * Copyright (c) 1999-2000 Cisco, Inc.
3  * Copyright (c) 1999-2001 Motorola, Inc.
4  * Copyright (c) 2001-2003 International Business Machines, Corp.
5  * Copyright (c) 2001 Intel Corp.
6  * Copyright (c) 2001 Nokia, Inc.
7  * Copyright (c) 2001 La Monte H.P. Yarroll
8  *
9  * This file is part of the SCTP kernel implementation
10  *
11  * These functions handle all input from the IP layer into SCTP.
12  *
13  * This SCTP implementation is free software;
14  * you can redistribute it and/or modify it under the terms of
15  * the GNU General Public License as published by
16  * the Free Software Foundation; either version 2, or (at your option)
17  * any later version.
18  *
19  * This SCTP implementation is distributed in the hope that it
20  * will be useful, but WITHOUT ANY WARRANTY; without even the implied
21  *                 ************************
22  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
23  * See the GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with GNU CC; see the file COPYING.  If not, see
27  * <http://www.gnu.org/licenses/>.
28  *
29  * Please send any bug reports or fixes you make to the
30  * email address(es):
31  *    lksctp developers <linux-sctp@vger.kernel.org>
32  *
33  * Written or modified by:
34  *    La Monte H.P. Yarroll <piggy@acm.org>
35  *    Karl Knutson <karl@athena.chicago.il.us>
36  *    Xingang Guo <xingang.guo@intel.com>
37  *    Jon Grimm <jgrimm@us.ibm.com>
38  *    Hui Huang <hui.huang@nokia.com>
39  *    Daisy Chang <daisyc@us.ibm.com>
40  *    Sridhar Samudrala <sri@us.ibm.com>
41  *    Ardelle Fan <ardelle.fan@intel.com>
42  */
43
44 #include <linux/types.h>
45 #include <linux/list.h> /* For struct list_head */
46 #include <linux/socket.h>
47 #include <linux/ip.h>
48 #include <linux/time.h> /* For struct timeval */
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 /* Forward declarations for internal helpers. */
63 static int sctp_rcv_ootb(struct sk_buff *);
64 static 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);
69 static 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);
73 static 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
79 static int sctp_add_backlog(struct sock *sk, struct sk_buff *skb);
80
81
82 /* Calculate the SCTP checksum of an SCTP packet.  */
83 static 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                 /* CRC failure, dump it. */
91                 __SCTP_INC_STATS(net, SCTP_MIB_CHECKSUMERRORS);
92                 return -1;
93         }
94         return 0;
95 }
96
97 /*
98  * This is the routine which IP calls when receiving an SCTP packet.
99  */
100 int 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         /* If packet is too small to contain a single chunk, let's not
121          * waste time on it anymore.
122          */
123         if (skb->len < sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr) +
124                        skb_transport_offset(skb))
125                 goto discard_it;
126
127         /* If the packet is fragmented and we need to do crc checking,
128          * it's better to just linearize it otherwise crc computing
129          * takes longer.
130          */
131         if ((!is_gso && skb_linearize(skb)) ||
132             !pskb_may_pull(skb, sizeof(struct sctphdr)))
133                 goto discard_it;
134
135         /* Pull up the IP header. */
136         __skb_pull(skb, skb_transport_offset(skb));
137
138         skb->csum_valid = 0; /* Previous value not applicable */
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         /* Initialize local addresses for lookups. */
156         af->from_skb(&src, skb, 1);
157         af->from_skb(&dest, skb, 0);
158
159         /* If the packet is to or from a non-unicast address,
160          * silently discard the packet.
161          *
162          * This is not clearly defined in the RFC except in section
163          * 8.4 - OOTB handling.  However, based on the book "Stream Control
164          * Transmission Protocol" 2.1, "It is important to note that the
165          * IP address of an SCTP transport address must be a routable
166          * unicast address.  In other words, IP multicast addresses and
167          * IP broadcast addresses cannot be used in an SCTP transport
168          * address."
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         /* Retrieve the common input handling substructure. */
180         rcvr = asoc ? &asoc->base : &ep->base;
181         sk = rcvr->sk;
182
183         /*
184          * If a frame arrives on an interface and the receiving socket is
185          * bound to another interface, via SO_BINDTODEVICE, treat it as OOTB
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          * RFC 2960, 8.4 - Handle "Out of the blue" Packets.
204          * An SCTP packet is called an "out of the blue" (OOTB)
205          * packet if it is correctly formed, i.e., passed the
206          * receiver's checksum check, but the receiver is not
207          * able to identify the association to which this
208          * packet belongs.
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         /* Create an SCTP packet structure. */
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         /* Remember what endpoint is to handle this packet. */
231         chunk->rcvr = rcvr;
232
233         /* Remember the SCTP header. */
234         chunk->sctp_hdr = sctp_hdr(skb);
235
236         /* Set the source and destination addresses of the incoming chunk.  */
237         sctp_init_addrs(chunk, &src, &dest);
238
239         /* Remember where we came from.  */
240         chunk->transport = transport;
241
242         /* Acquire access to the sock lock. Note: We are safe from other
243          * bottom halves on this lock, but a user may be in the lock too,
244          * so check if it is busy.
245          */
246         bh_lock_sock(sk);
247
248         if (sk != rcvr->sk) {
249                 /* Our cached sk is different from the rcvr->sk.  This is
250                  * because migrate()/accept() may have moved the association
251                  * to a new socket and released all the sockets.  So now we
252                  * are holding a lock on the old socket while the user may
253                  * be doing something with the new socket.  Switch our veiw
254                  * of the current sk.
255                  */
256                 bh_unlock_sock(sk);
257                 sk = rcvr->sk;
258                 bh_lock_sock(sk);
259         }
260
261         if (sock_owned_by_user(sk)) {
262                 if (sctp_add_backlog(sk, skb)) {
263                         bh_unlock_sock(sk);
264                         sctp_chunk_free(chunk);
265                         skb = NULL; /* sctp_chunk_free already freed the skb */
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         /* Release the asoc/ep ref we took in the lookup calls. */
277         if (transport)
278                 sctp_transport_put(transport);
279         else
280                 sctp_endpoint_put(ep);
281
282         return 0;
283
284 discard_it:
285         __SCTP_INC_STATS(net, SCTP_MIB_IN_PKT_DISCARDS);
286         kfree_skb(skb);
287         return 0;
288
289 discard_release:
290         /* Release the asoc/ep ref we took in the lookup calls. */
291         if (transport)
292                 sctp_transport_put(transport);
293         else
294                 sctp_endpoint_put(ep);
295
296         goto discard_it;
297 }
298
299 /* Process the backlog queue of the socket.  Every skb on
300  * the backlog holds a ref on an association or endpoint.
301  * We hold this ref throughout the state machine to make
302  * sure that the structure we need is still around.
303  */
304 int 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         /* If the rcvr is dead then the association or endpoint
315          * has been deleted and we can safely drop the chunk
316          * and refs that we are holding.
317          */
318         if (rcvr->dead) {
319                 sctp_chunk_free(chunk);
320                 goto done;
321         }
322
323         if (unlikely(rcvr->sk != sk)) {
324                 /* In this case, the association moved from one socket to
325                  * another.  We are currently sitting on the backlog of the
326                  * old socket, so we need to move.
327                  * However, since we are here in the process context we
328                  * need to take make sure that the user doesn't own
329                  * the new socket when we process the packet.
330                  * If the new socket is user-owned, queue the chunk to the
331                  * backlog of the new socket without dropping any refs.
332                  * Otherwise, we can safely push the chunk on the inqueue.
333                  */
334
335                 sk = rcvr->sk;
336                 local_bh_disable();
337                 bh_lock_sock(sk);
338
339                 if (sock_owned_by_user(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                 /* If the chunk was backloged again, don't drop refs */
351                 if (backloged)
352                         return 0;
353         } else {
354                 sctp_inq_push(inqueue, chunk);
355         }
356
357 done:
358         /* Release the refs we took in sctp_add_backlog */
359         if (SCTP_EP_TYPE_ASSOCIATION == rcvr->type)
360                 sctp_transport_put(t);
361         else if (SCTP_EP_TYPE_SOCKET == rcvr->type)
362                 sctp_endpoint_put(sctp_ep(rcvr));
363         else
364                 BUG();
365
366         return 0;
367 }
368
369 static int sctp_add_backlog(struct sock *sk, struct sk_buff *skb)
370 {
371         struct sctp_chunk *chunk = SCTP_INPUT_CB(skb)->chunk;
372         struct sctp_transport *t = chunk->transport;
373         struct sctp_ep_common *rcvr = chunk->rcvr;
374         int ret;
375
376         ret = sk_add_backlog(sk, skb, sk->sk_rcvbuf);
377         if (!ret) {
378                 /* Hold the assoc/ep while hanging on the backlog queue.
379                  * This way, we know structures we need will not disappear
380                  * from us
381                  */
382                 if (SCTP_EP_TYPE_ASSOCIATION == rcvr->type)
383                         sctp_transport_hold(t);
384                 else if (SCTP_EP_TYPE_SOCKET == rcvr->type)
385                         sctp_endpoint_hold(sctp_ep(rcvr));
386                 else
387                         BUG();
388         }
389         return ret;
390
391 }
392
393 /* Handle icmp frag needed error. */
394 void sctp_icmp_frag_needed(struct sock *sk, struct sctp_association *asoc,
395                            struct sctp_transport *t, __u32 pmtu)
396 {
397         if (!t || (t->pathmtu <= pmtu))
398                 return;
399
400         if (sock_owned_by_user(sk)) {
401                 atomic_set(&t->mtu_info, pmtu);
402                 asoc->pmtu_pending = 1;
403                 t->pmtu_pending = 1;
404                 return;
405         }
406
407         if (!(t->param_flags & SPP_PMTUD_ENABLE))
408                 /* We can't allow retransmitting in such case, as the
409                  * retransmission would be sized just as before, and thus we
410                  * would get another icmp, and retransmit again.
411                  */
412                 return;
413
414         /* Update transports view of the MTU. Return if no update was needed.
415          * If an update wasn't needed/possible, it also doesn't make sense to
416          * try to retransmit now.
417          */
418         if (!sctp_transport_update_pmtu(t, pmtu))
419                 return;
420
421         /* Update association pmtu. */
422         sctp_assoc_sync_pmtu(asoc);
423
424         /* Retransmit with the new pmtu setting. */
425         sctp_retransmit(&asoc->outqueue, t, SCTP_RTXR_PMTUD);
426 }
427
428 void sctp_icmp_redirect(struct sock *sk, struct sctp_transport *t,
429                         struct sk_buff *skb)
430 {
431         struct dst_entry *dst;
432
433         if (sock_owned_by_user(sk) || !t)
434                 return;
435         dst = sctp_transport_dst_check(t);
436         if (dst)
437                 dst->ops->redirect(dst, sk, skb);
438 }
439
440 /*
441  * SCTP Implementer's Guide, 2.37 ICMP handling procedures
442  *
443  * ICMP8) If the ICMP code is a "Unrecognized next header type encountered"
444  *        or a "Protocol Unreachable" treat this message as an abort
445  *        with the T bit set.
446  *
447  * This function sends an event to the state machine, which will abort the
448  * association.
449  *
450  */
451 void sctp_icmp_proto_unreachable(struct sock *sk,
452                            struct sctp_association *asoc,
453                            struct sctp_transport *t)
454 {
455         if (sock_owned_by_user(sk)) {
456                 if (timer_pending(&t->proto_unreach_timer))
457                         return;
458                 else {
459                         if (!mod_timer(&t->proto_unreach_timer,
460                                                 jiffies + (HZ/20)))
461                                 sctp_association_hold(asoc);
462                 }
463         } else {
464                 struct net *net = sock_net(sk);
465
466                 pr_debug("%s: unrecognized next header type "
467                          "encountered!\n", __func__);
468
469                 if (del_timer(&t->proto_unreach_timer))
470                         sctp_association_put(asoc);
471
472                 sctp_do_sm(net, SCTP_EVENT_T_OTHER,
473                            SCTP_ST_OTHER(SCTP_EVENT_ICMP_PROTO_UNREACH),
474                            asoc->state, asoc->ep, asoc, t,
475                            GFP_ATOMIC);
476         }
477 }
478
479 /* Common lookup code for icmp/icmpv6 error handler. */
480 struct sock *sctp_err_lookup(struct net *net, int family, struct sk_buff *skb,
481                              struct sctphdr *sctphdr,
482                              struct sctp_association **app,
483                              struct sctp_transport **tpp)
484 {
485         struct sctp_init_chunk *chunkhdr, _chunkhdr;
486         union sctp_addr saddr;
487         union sctp_addr daddr;
488         struct sctp_af *af;
489         struct sock *sk = NULL;
490         struct sctp_association *asoc;
491         struct sctp_transport *transport = NULL;
492         __u32 vtag = ntohl(sctphdr->vtag);
493
494         *app = NULL; *tpp = NULL;
495
496         af = sctp_get_af_specific(family);
497         if (unlikely(!af)) {
498                 return NULL;
499         }
500
501         /* Initialize local addresses for lookups. */
502         af->from_skb(&saddr, skb, 1);
503         af->from_skb(&daddr, skb, 0);
504
505         /* Look for an association that matches the incoming ICMP error
506          * packet.
507          */
508         asoc = __sctp_lookup_association(net, &saddr, &daddr, &transport);
509         if (!asoc)
510                 return NULL;
511
512         sk = asoc->base.sk;
513
514         /* RFC 4960, Appendix C. ICMP Handling
515          *
516          * ICMP6) An implementation MUST validate that the Verification Tag
517          * contained in the ICMP message matches the Verification Tag of
518          * the peer.  If the Verification Tag is not 0 and does NOT
519          * match, discard the ICMP message.  If it is 0 and the ICMP
520          * message contains enough bytes to verify that the chunk type is
521          * an INIT chunk and that the Initiate Tag matches the tag of the
522          * peer, continue with ICMP7.  If the ICMP message is too short
523          * or the chunk type or the Initiate Tag does not match, silently
524          * discard the packet.
525          */
526         if (vtag == 0) {
527                 /* chunk header + first 4 octects of init header */
528                 chunkhdr = skb_header_pointer(skb, skb_transport_offset(skb) +
529                                               sizeof(struct sctphdr),
530                                               sizeof(struct sctp_chunkhdr) +
531                                               sizeof(__be32), &_chunkhdr);
532                 if (!chunkhdr ||
533                     chunkhdr->chunk_hdr.type != SCTP_CID_INIT ||
534                     ntohl(chunkhdr->init_hdr.init_tag) != asoc->c.my_vtag)
535                         goto out;
536
537         } else if (vtag != asoc->c.peer_vtag) {
538                 goto out;
539         }
540
541         bh_lock_sock(sk);
542
543         /* If too many ICMPs get dropped on busy
544          * servers this needs to be solved differently.
545          */
546         if (sock_owned_by_user(sk))
547                 __NET_INC_STATS(net, LINUX_MIB_LOCKDROPPEDICMPS);
548
549         *app = asoc;
550         *tpp = transport;
551         return sk;
552
553 out:
554         sctp_transport_put(transport);
555         return NULL;
556 }
557
558 /* Common cleanup code for icmp/icmpv6 error handler. */
559 void sctp_err_finish(struct sock *sk, struct sctp_transport *t)
560 {
561         bh_unlock_sock(sk);
562         sctp_transport_put(t);
563 }
564
565 /*
566  * This routine is called by the ICMP module when it gets some
567  * sort of error condition.  If err < 0 then the socket should
568  * be closed and the error returned to the user.  If err > 0
569  * it's just the icmp type << 8 | icmp code.  After adjustment
570  * header points to the first 8 bytes of the sctp header.  We need
571  * to find the appropriate port.
572  *
573  * The locking strategy used here is very "optimistic". When
574  * someone else accesses the socket the ICMP is just dropped
575  * and for some paths there is no check at all.
576  * A more general error queue to queue errors for later handling
577  * is probably better.
578  *
579  */
580 int sctp_v4_err(struct sk_buff *skb, __u32 info)
581 {
582         const struct iphdr *iph = (const struct iphdr *)skb->data;
583         const int ihlen = iph->ihl * 4;
584         const int type = icmp_hdr(skb)->type;
585         const int code = icmp_hdr(skb)->code;
586         struct sock *sk;
587         struct sctp_association *asoc = NULL;
588         struct sctp_transport *transport;
589         struct inet_sock *inet;
590         __u16 saveip, savesctp;
591         int err;
592         struct net *net = dev_net(skb->dev);
593
594         /* Fix up skb to look at the embedded net header. */
595         saveip = skb->network_header;
596         savesctp = skb->transport_header;
597         skb_reset_network_header(skb);
598         skb_set_transport_header(skb, ihlen);
599         sk = sctp_err_lookup(net, AF_INET, skb, sctp_hdr(skb), &asoc, &transport);
600         /* Put back, the original values. */
601         skb->network_header = saveip;
602         skb->transport_header = savesctp;
603         if (!sk) {
604                 __ICMP_INC_STATS(net, ICMP_MIB_INERRORS);
605                 return -ENOENT;
606         }
607         /* Warning:  The sock lock is held.  Remember to call
608          * sctp_err_finish!
609          */
610
611         switch (type) {
612         case ICMP_PARAMETERPROB:
613                 err = EPROTO;
614                 break;
615         case ICMP_DEST_UNREACH:
616                 if (code > NR_ICMP_UNREACH)
617                         goto out_unlock;
618
619                 /* PMTU discovery (RFC1191) */
620                 if (ICMP_FRAG_NEEDED == code) {
621                         sctp_icmp_frag_needed(sk, asoc, transport,
622                                               SCTP_TRUNC4(info));
623                         goto out_unlock;
624                 } else {
625                         if (ICMP_PROT_UNREACH == code) {
626                                 sctp_icmp_proto_unreachable(sk, asoc,
627                                                             transport);
628                                 goto out_unlock;
629                         }
630                 }
631                 err = icmp_err_convert[code].errno;
632                 break;
633         case ICMP_TIME_EXCEEDED:
634                 /* Ignore any time exceeded errors due to fragment reassembly
635                  * timeouts.
636                  */
637                 if (ICMP_EXC_FRAGTIME == code)
638                         goto out_unlock;
639
640                 err = EHOSTUNREACH;
641                 break;
642         case ICMP_REDIRECT:
643                 sctp_icmp_redirect(sk, transport, skb);
644                 /* Fall through to out_unlock. */
645         default:
646                 goto out_unlock;
647         }
648
649         inet = inet_sk(sk);
650         if (!sock_owned_by_user(sk) && inet->recverr) {
651                 sk->sk_err = err;
652                 sk->sk_error_report(sk);
653         } else {  /* Only an error on timeout */
654                 sk->sk_err_soft = err;
655         }
656
657 out_unlock:
658         sctp_err_finish(sk, transport);
659         return 0;
660 }
661
662 /*
663  * RFC 2960, 8.4 - Handle "Out of the blue" Packets.
664  *
665  * This function scans all the chunks in the OOTB packet to determine if
666  * the packet should be discarded right away.  If a response might be needed
667  * for this packet, or, if further processing is possible, the packet will
668  * be queued to a proper inqueue for the next phase of handling.
669  *
670  * Output:
671  * Return 0 - If further processing is needed.
672  * Return 1 - If the packet can be discarded right away.
673  */
674 static int sctp_rcv_ootb(struct sk_buff *skb)
675 {
676         struct sctp_chunkhdr *ch, _ch;
677         int ch_end, offset = 0;
678
679         /* Scan through all the chunks in the packet.  */
680         do {
681                 /* Make sure we have at least the header there */
682                 if (offset + sizeof(_ch) > skb->len)
683                         break;
684
685                 ch = skb_header_pointer(skb, offset, sizeof(*ch), &_ch);
686
687                 /* Break out if chunk length is less then minimal. */
688                 if (ntohs(ch->length) < sizeof(_ch))
689                         break;
690
691                 ch_end = offset + SCTP_PAD4(ntohs(ch->length));
692                 if (ch_end > skb->len)
693                         break;
694
695                 /* RFC 8.4, 2) If the OOTB packet contains an ABORT chunk, the
696                  * receiver MUST silently discard the OOTB packet and take no
697                  * further action.
698                  */
699                 if (SCTP_CID_ABORT == ch->type)
700                         goto discard;
701
702                 /* RFC 8.4, 6) If the packet contains a SHUTDOWN COMPLETE
703                  * chunk, the receiver should silently discard the packet
704                  * and take no further action.
705                  */
706                 if (SCTP_CID_SHUTDOWN_COMPLETE == ch->type)
707                         goto discard;
708
709                 /* RFC 4460, 2.11.2
710                  * This will discard packets with INIT chunk bundled as
711                  * subsequent chunks in the packet.  When INIT is first,
712                  * the normal INIT processing will discard the chunk.
713                  */
714                 if (SCTP_CID_INIT == ch->type && (void *)ch != skb->data)
715                         goto discard;
716
717                 offset = ch_end;
718         } while (ch_end < skb->len);
719
720         return 0;
721
722 discard:
723         return 1;
724 }
725
726 /* Insert endpoint into the hash table.  */
727 static void __sctp_hash_endpoint(struct sctp_endpoint *ep)
728 {
729         struct net *net = sock_net(ep->base.sk);
730         struct sctp_ep_common *epb;
731         struct sctp_hashbucket *head;
732
733         epb = &ep->base;
734
735         epb->hashent = sctp_ep_hashfn(net, epb->bind_addr.port);
736         head = &sctp_ep_hashtable[epb->hashent];
737
738         write_lock(&head->lock);
739         hlist_add_head(&epb->node, &head->chain);
740         write_unlock(&head->lock);
741 }
742
743 /* Add an endpoint to the hash. Local BH-safe. */
744 void sctp_hash_endpoint(struct sctp_endpoint *ep)
745 {
746         local_bh_disable();
747         __sctp_hash_endpoint(ep);
748         local_bh_enable();
749 }
750
751 /* Remove endpoint from the hash table.  */
752 static void __sctp_unhash_endpoint(struct sctp_endpoint *ep)
753 {
754         struct net *net = sock_net(ep->base.sk);
755         struct sctp_hashbucket *head;
756         struct sctp_ep_common *epb;
757
758         epb = &ep->base;
759
760         epb->hashent = sctp_ep_hashfn(net, epb->bind_addr.port);
761
762         head = &sctp_ep_hashtable[epb->hashent];
763
764         write_lock(&head->lock);
765         hlist_del_init(&epb->node);
766         write_unlock(&head->lock);
767 }
768
769 /* Remove endpoint from the hash.  Local BH-safe. */
770 void sctp_unhash_endpoint(struct sctp_endpoint *ep)
771 {
772         local_bh_disable();
773         __sctp_unhash_endpoint(ep);
774         local_bh_enable();
775 }
776
777 static inline __u32 sctp_hashfn(const struct net *net, __be16 lport,
778                                 const union sctp_addr *paddr, __u32 seed)
779 {
780         __u32 addr;
781
782         if (paddr->sa.sa_family == AF_INET6)
783                 addr = jhash(&paddr->v6.sin6_addr, 16, seed);
784         else
785                 addr = (__force __u32)paddr->v4.sin_addr.s_addr;
786
787         return  jhash_3words(addr, ((__force __u32)paddr->v4.sin_port) << 16 |
788                              (__force __u32)lport, net_hash_mix(net), seed);
789 }
790
791 /* Look up an endpoint. */
792 static struct sctp_endpoint *__sctp_rcv_lookup_endpoint(
793                                         struct net *net, struct sk_buff *skb,
794                                         const union sctp_addr *laddr,
795                                         const union sctp_addr *paddr)
796 {
797         struct sctp_hashbucket *head;
798         struct sctp_ep_common *epb;
799         struct sctp_endpoint *ep;
800         struct sock *sk;
801         __be16 lport;
802         int hash;
803
804         lport = laddr->v4.sin_port;
805         hash = sctp_ep_hashfn(net, ntohs(lport));
806         head = &sctp_ep_hashtable[hash];
807         read_lock(&head->lock);
808         sctp_for_each_hentry(epb, &head->chain) {
809                 ep = sctp_ep(epb);
810                 if (sctp_endpoint_is_match(ep, net, laddr))
811                         goto hit;
812         }
813
814         ep = sctp_sk(net->sctp.ctl_sock)->ep;
815
816 hit:
817         sk = ep->base.sk;
818         if (sk->sk_reuseport) {
819                 __u32 phash = sctp_hashfn(net, lport, paddr, 0);
820
821                 sk = reuseport_select_sock(sk, phash, skb,
822                                            sizeof(struct sctphdr));
823                 if (sk)
824                         ep = sctp_sk(sk)->ep;
825         }
826         sctp_endpoint_hold(ep);
827         read_unlock(&head->lock);
828         return ep;
829 }
830
831 /* rhashtable for transport */
832 struct sctp_hash_cmp_arg {
833         const union sctp_addr   *paddr;
834         const struct net        *net;
835         __be16                  lport;
836 };
837
838 static inline int sctp_hash_cmp(struct rhashtable_compare_arg *arg,
839                                 const void *ptr)
840 {
841         struct sctp_transport *t = (struct sctp_transport *)ptr;
842         const struct sctp_hash_cmp_arg *x = arg->key;
843         int err = 1;
844
845         if (!sctp_cmp_addr_exact(&t->ipaddr, x->paddr))
846                 return err;
847         if (!sctp_transport_hold(t))
848                 return err;
849
850         if (!net_eq(sock_net(t->asoc->base.sk), x->net))
851                 goto out;
852         if (x->lport != htons(t->asoc->base.bind_addr.port))
853                 goto out;
854
855         err = 0;
856 out:
857         sctp_transport_put(t);
858         return err;
859 }
860
861 static inline __u32 sctp_hash_obj(const void *data, u32 len, u32 seed)
862 {
863         const struct sctp_transport *t = data;
864
865         return sctp_hashfn(sock_net(t->asoc->base.sk),
866                            htons(t->asoc->base.bind_addr.port),
867                            &t->ipaddr, seed);
868 }
869
870 static inline __u32 sctp_hash_key(const void *data, u32 len, u32 seed)
871 {
872         const struct sctp_hash_cmp_arg *x = data;
873
874         return sctp_hashfn(x->net, x->lport, x->paddr, seed);
875 }
876
877 static const struct rhashtable_params sctp_hash_params = {
878         .head_offset            = offsetof(struct sctp_transport, node),
879         .hashfn                 = sctp_hash_key,
880         .obj_hashfn             = sctp_hash_obj,
881         .obj_cmpfn              = sctp_hash_cmp,
882         .automatic_shrinking    = true,
883 };
884
885 int sctp_transport_hashtable_init(void)
886 {
887         return rhltable_init(&sctp_transport_hashtable, &sctp_hash_params);
888 }
889
890 void sctp_transport_hashtable_destroy(void)
891 {
892         rhltable_destroy(&sctp_transport_hashtable);
893 }
894
895 int sctp_hash_transport(struct sctp_transport *t)
896 {
897         struct sctp_transport *transport;
898         struct rhlist_head *tmp, *list;
899         struct sctp_hash_cmp_arg arg;
900         int err;
901
902         if (t->asoc->temp)
903                 return 0;
904
905         arg.net   = sock_net(t->asoc->base.sk);
906         arg.paddr = &t->ipaddr;
907         arg.lport = htons(t->asoc->base.bind_addr.port);
908
909         rcu_read_lock();
910         list = rhltable_lookup(&sctp_transport_hashtable, &arg,
911                                sctp_hash_params);
912
913         rhl_for_each_entry_rcu(transport, tmp, list, node)
914                 if (transport->asoc->ep == t->asoc->ep) {
915                         rcu_read_unlock();
916                         return -EEXIST;
917                 }
918         rcu_read_unlock();
919
920         err = rhltable_insert_key(&sctp_transport_hashtable, &arg,
921                                   &t->node, sctp_hash_params);
922         if (err)
923                 pr_err_once("insert transport fail, errno %d\n", err);
924
925         return err;
926 }
927
928 void sctp_unhash_transport(struct sctp_transport *t)
929 {
930         if (t->asoc->temp)
931                 return;
932
933         rhltable_remove(&sctp_transport_hashtable, &t->node,
934                         sctp_hash_params);
935 }
936
937 /* return a transport with holding it */
938 struct sctp_transport *sctp_addrs_lookup_transport(
939                                 struct net *net,
940                                 const union sctp_addr *laddr,
941                                 const union sctp_addr *paddr)
942 {
943         struct rhlist_head *tmp, *list;
944         struct sctp_transport *t;
945         struct sctp_hash_cmp_arg arg = {
946                 .paddr = paddr,
947                 .net   = net,
948                 .lport = laddr->v4.sin_port,
949         };
950
951         list = rhltable_lookup(&sctp_transport_hashtable, &arg,
952                                sctp_hash_params);
953
954         rhl_for_each_entry_rcu(t, tmp, list, node) {
955                 if (!sctp_transport_hold(t))
956                         continue;
957
958                 if (sctp_bind_addr_match(&t->asoc->base.bind_addr,
959                                          laddr, sctp_sk(t->asoc->base.sk)))
960                         return t;
961                 sctp_transport_put(t);
962         }
963
964         return NULL;
965 }
966
967 /* return a transport without holding it, as it's only used under sock lock */
968 struct sctp_transport *sctp_epaddr_lookup_transport(
969                                 const struct sctp_endpoint *ep,
970                                 const union sctp_addr *paddr)
971 {
972         struct net *net = sock_net(ep->base.sk);
973         struct rhlist_head *tmp, *list;
974         struct sctp_transport *t;
975         struct sctp_hash_cmp_arg arg = {
976                 .paddr = paddr,
977                 .net   = net,
978                 .lport = htons(ep->base.bind_addr.port),
979         };
980
981         list = rhltable_lookup(&sctp_transport_hashtable, &arg,
982                                sctp_hash_params);
983
984         rhl_for_each_entry_rcu(t, tmp, list, node)
985                 if (ep == t->asoc->ep)
986                         return t;
987
988         return NULL;
989 }
990
991 /* Look up an association. */
992 static struct sctp_association *__sctp_lookup_association(
993                                         struct net *net,
994                                         const union sctp_addr *local,
995                                         const union sctp_addr *peer,
996                                         struct sctp_transport **pt)
997 {
998         struct sctp_transport *t;
999         struct sctp_association *asoc = NULL;
1000
1001         t = sctp_addrs_lookup_transport(net, local, peer);
1002         if (!t)
1003                 goto out;
1004
1005         asoc = t->asoc;
1006         *pt = t;
1007
1008 out:
1009         return asoc;
1010 }
1011
1012 /* Look up an association. protected by RCU read lock */
1013 static
1014 struct sctp_association *sctp_lookup_association(struct net *net,
1015                                                  const union sctp_addr *laddr,
1016                                                  const union sctp_addr *paddr,
1017                                                  struct sctp_transport **transportp)
1018 {
1019         struct sctp_association *asoc;
1020
1021         rcu_read_lock();
1022         asoc = __sctp_lookup_association(net, laddr, paddr, transportp);
1023         rcu_read_unlock();
1024
1025         return asoc;
1026 }
1027
1028 /* Is there an association matching the given local and peer addresses? */
1029 bool sctp_has_association(struct net *net,
1030                           const union sctp_addr *laddr,
1031                           const union sctp_addr *paddr)
1032 {
1033         struct sctp_transport *transport;
1034
1035         if (sctp_lookup_association(net, laddr, paddr, &transport)) {
1036                 sctp_transport_put(transport);
1037                 return true;
1038         }
1039
1040         return false;
1041 }
1042
1043 /*
1044  * SCTP Implementors Guide, 2.18 Handling of address
1045  * parameters within the INIT or INIT-ACK.
1046  *
1047  * D) When searching for a matching TCB upon reception of an INIT
1048  *    or INIT-ACK chunk the receiver SHOULD use not only the
1049  *    source address of the packet (containing the INIT or
1050  *    INIT-ACK) but the receiver SHOULD also use all valid
1051  *    address parameters contained within the chunk.
1052  *
1053  * 2.18.3 Solution description
1054  *
1055  * This new text clearly specifies to an implementor the need
1056  * to look within the INIT or INIT-ACK. Any implementation that
1057  * does not do this, may not be able to establish associations
1058  * in certain circumstances.
1059  *
1060  */
1061 static struct sctp_association *__sctp_rcv_init_lookup(struct net *net,
1062         struct sk_buff *skb,
1063         const union sctp_addr *laddr, struct sctp_transport **transportp)
1064 {
1065         struct sctp_association *asoc;
1066         union sctp_addr addr;
1067         union sctp_addr *paddr = &addr;
1068         struct sctphdr *sh = sctp_hdr(skb);
1069         union sctp_params params;
1070         struct sctp_init_chunk *init;
1071         struct sctp_af *af;
1072
1073         /*
1074          * This code will NOT touch anything inside the chunk--it is
1075          * strictly READ-ONLY.
1076          *
1077          * RFC 2960 3  SCTP packet Format
1078          *
1079          * Multiple chunks can be bundled into one SCTP packet up to
1080          * the MTU size, except for the INIT, INIT ACK, and SHUTDOWN
1081          * COMPLETE chunks.  These chunks MUST NOT be bundled with any
1082          * other chunk in a packet.  See Section 6.10 for more details
1083          * on chunk bundling.
1084          */
1085
1086         /* Find the start of the TLVs and the end of the chunk.  This is
1087          * the region we search for address parameters.
1088          */
1089         init = (struct sctp_init_chunk *)skb->data;
1090
1091         /* Walk the parameters looking for embedded addresses. */
1092         sctp_walk_params(params, init, init_hdr.params) {
1093
1094                 /* Note: Ignoring hostname addresses. */
1095                 af = sctp_get_af_specific(param_type2af(params.p->type));
1096                 if (!af)
1097                         continue;
1098
1099                 af->from_addr_param(paddr, params.addr, sh->source, 0);
1100
1101                 asoc = __sctp_lookup_association(net, laddr, paddr, transportp);
1102                 if (asoc)
1103                         return asoc;
1104         }
1105
1106         return NULL;
1107 }
1108
1109 /* ADD-IP, Section 5.2
1110  * When an endpoint receives an ASCONF Chunk from the remote peer
1111  * special procedures may be needed to identify the association the
1112  * ASCONF Chunk is associated with. To properly find the association
1113  * the following procedures SHOULD be followed:
1114  *
1115  * D2) If the association is not found, use the address found in the
1116  * Address Parameter TLV combined with the port number found in the
1117  * SCTP common header. If found proceed to rule D4.
1118  *
1119  * D2-ext) If more than one ASCONF Chunks are packed together, use the
1120  * address found in the ASCONF Address Parameter TLV of each of the
1121  * subsequent ASCONF Chunks. If found, proceed to rule D4.
1122  */
1123 static struct sctp_association *__sctp_rcv_asconf_lookup(
1124                                         struct net *net,
1125                                         struct sctp_chunkhdr *ch,
1126                                         const union sctp_addr *laddr,
1127                                         __be16 peer_port,
1128                                         struct sctp_transport **transportp)
1129 {
1130         struct sctp_addip_chunk *asconf = (struct sctp_addip_chunk *)ch;
1131         struct sctp_af *af;
1132         union sctp_addr_param *param;
1133         union sctp_addr paddr;
1134
1135         /* Skip over the ADDIP header and find the Address parameter */
1136         param = (union sctp_addr_param *)(asconf + 1);
1137
1138         af = sctp_get_af_specific(param_type2af(param->p.type));
1139         if (unlikely(!af))
1140                 return NULL;
1141
1142         af->from_addr_param(&paddr, param, peer_port, 0);
1143
1144         return __sctp_lookup_association(net, laddr, &paddr, transportp);
1145 }
1146
1147
1148 /* SCTP-AUTH, Section 6.3:
1149 *    If the receiver does not find a STCB for a packet containing an AUTH
1150 *    chunk as the first chunk and not a COOKIE-ECHO chunk as the second
1151 *    chunk, it MUST use the chunks after the AUTH chunk to look up an existing
1152 *    association.
1153 *
1154 * This means that any chunks that can help us identify the association need
1155 * to be looked at to find this association.
1156 */
1157 static struct sctp_association *__sctp_rcv_walk_lookup(struct net *net,
1158                                       struct sk_buff *skb,
1159                                       const union sctp_addr *laddr,
1160                                       struct sctp_transport **transportp)
1161 {
1162         struct sctp_association *asoc = NULL;
1163         struct sctp_chunkhdr *ch;
1164         int have_auth = 0;
1165         unsigned int chunk_num = 1;
1166         __u8 *ch_end;
1167
1168         /* Walk through the chunks looking for AUTH or ASCONF chunks
1169          * to help us find the association.
1170          */
1171         ch = (struct sctp_chunkhdr *)skb->data;
1172         do {
1173                 /* Break out if chunk length is less then minimal. */
1174                 if (ntohs(ch->length) < sizeof(*ch))
1175                         break;
1176
1177                 ch_end = ((__u8 *)ch) + SCTP_PAD4(ntohs(ch->length));
1178                 if (ch_end > skb_tail_pointer(skb))
1179                         break;
1180
1181                 switch (ch->type) {
1182                 case SCTP_CID_AUTH:
1183                         have_auth = chunk_num;
1184                         break;
1185
1186                 case SCTP_CID_COOKIE_ECHO:
1187                         /* If a packet arrives containing an AUTH chunk as
1188                          * a first chunk, a COOKIE-ECHO chunk as the second
1189                          * chunk, and possibly more chunks after them, and
1190                          * the receiver does not have an STCB for that
1191                          * packet, then authentication is based on
1192                          * the contents of the COOKIE- ECHO chunk.
1193                          */
1194                         if (have_auth == 1 && chunk_num == 2)
1195                                 return NULL;
1196                         break;
1197
1198                 case SCTP_CID_ASCONF:
1199                         if (have_auth || net->sctp.addip_noauth)
1200                                 asoc = __sctp_rcv_asconf_lookup(
1201                                                 net, ch, laddr,
1202                                                 sctp_hdr(skb)->source,
1203                                                 transportp);
1204                 default:
1205                         break;
1206                 }
1207
1208                 if (asoc)
1209                         break;
1210
1211                 ch = (struct sctp_chunkhdr *)ch_end;
1212                 chunk_num++;
1213         } while (ch_end < skb_tail_pointer(skb));
1214
1215         return asoc;
1216 }
1217
1218 /*
1219  * There are circumstances when we need to look inside the SCTP packet
1220  * for information to help us find the association.   Examples
1221  * include looking inside of INIT/INIT-ACK chunks or after the AUTH
1222  * chunks.
1223  */
1224 static struct sctp_association *__sctp_rcv_lookup_harder(struct net *net,
1225                                       struct sk_buff *skb,
1226                                       const union sctp_addr *laddr,
1227                                       struct sctp_transport **transportp)
1228 {
1229         struct sctp_chunkhdr *ch;
1230
1231         /* We do not allow GSO frames here as we need to linearize and
1232          * then cannot guarantee frame boundaries. This shouldn't be an
1233          * issue as packets hitting this are mostly INIT or INIT-ACK and
1234          * those cannot be on GSO-style anyway.
1235          */
1236         if (skb_is_gso(skb) && skb_is_gso_sctp(skb))
1237                 return NULL;
1238
1239         ch = (struct sctp_chunkhdr *)skb->data;
1240
1241         /* The code below will attempt to walk the chunk and extract
1242          * parameter information.  Before we do that, we need to verify
1243          * that the chunk length doesn't cause overflow.  Otherwise, we'll
1244          * walk off the end.
1245          */
1246         if (SCTP_PAD4(ntohs(ch->length)) > skb->len)
1247                 return NULL;
1248
1249         /* If this is INIT/INIT-ACK look inside the chunk too. */
1250         if (ch->type == SCTP_CID_INIT || ch->type == SCTP_CID_INIT_ACK)
1251                 return __sctp_rcv_init_lookup(net, skb, laddr, transportp);
1252
1253         return __sctp_rcv_walk_lookup(net, skb, laddr, transportp);
1254 }
1255
1256 /* Lookup an association for an inbound skb. */
1257 static struct sctp_association *__sctp_rcv_lookup(struct net *net,
1258                                       struct sk_buff *skb,
1259                                       const union sctp_addr *paddr,
1260                                       const union sctp_addr *laddr,
1261                                       struct sctp_transport **transportp)
1262 {
1263         struct sctp_association *asoc;
1264
1265         asoc = __sctp_lookup_association(net, laddr, paddr, transportp);
1266         if (asoc)
1267                 goto out;
1268
1269         /* Further lookup for INIT/INIT-ACK packets.
1270          * SCTP Implementors Guide, 2.18 Handling of address
1271          * parameters within the INIT or INIT-ACK.
1272          */
1273         asoc = __sctp_rcv_lookup_harder(net, skb, laddr, transportp);
1274         if (asoc)
1275                 goto out;
1276
1277         if (paddr->sa.sa_family == AF_INET)
1278                 pr_debug("sctp: asoc not found for src:%pI4:%d dst:%pI4:%d\n",
1279                          &laddr->v4.sin_addr, ntohs(laddr->v4.sin_port),
1280                          &paddr->v4.sin_addr, ntohs(paddr->v4.sin_port));
1281         else
1282                 pr_debug("sctp: asoc not found for src:%pI6:%d dst:%pI6:%d\n",
1283                          &laddr->v6.sin6_addr, ntohs(laddr->v6.sin6_port),
1284                          &paddr->v6.sin6_addr, ntohs(paddr->v6.sin6_port));
1285
1286 out:
1287         return asoc;
1288 }