Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* SCTP kernel reference 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 reference Implementation | |
10 | * | |
11 | * These functions handle all input from the IP layer into SCTP. | |
12 | * | |
13 | * The SCTP reference 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 | * The SCTP reference 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, write to | |
27 | * the Free Software Foundation, 59 Temple Place - Suite 330, | |
28 | * Boston, MA 02111-1307, USA. | |
29 | * | |
30 | * Please send any bug reports or fixes you make to the | |
31 | * email address(es): | |
32 | * lksctp developers <lksctp-developers@lists.sourceforge.net> | |
33 | * | |
34 | * Or submit a bug report through the following website: | |
35 | * http://www.sf.net/projects/lksctp | |
36 | * | |
37 | * Written or modified by: | |
38 | * La Monte H.P. Yarroll <piggy@acm.org> | |
39 | * Karl Knutson <karl@athena.chicago.il.us> | |
40 | * Xingang Guo <xingang.guo@intel.com> | |
41 | * Jon Grimm <jgrimm@us.ibm.com> | |
42 | * Hui Huang <hui.huang@nokia.com> | |
43 | * Daisy Chang <daisyc@us.ibm.com> | |
44 | * Sridhar Samudrala <sri@us.ibm.com> | |
45 | * Ardelle Fan <ardelle.fan@intel.com> | |
46 | * | |
47 | * Any bugs reported given to us we will try to fix... any fixes shared will | |
48 | * be incorporated into the next SCTP release. | |
49 | */ | |
50 | ||
51 | #include <linux/types.h> | |
52 | #include <linux/list.h> /* For struct list_head */ | |
53 | #include <linux/socket.h> | |
54 | #include <linux/ip.h> | |
55 | #include <linux/time.h> /* For struct timeval */ | |
56 | #include <net/ip.h> | |
57 | #include <net/icmp.h> | |
58 | #include <net/snmp.h> | |
59 | #include <net/sock.h> | |
60 | #include <net/xfrm.h> | |
61 | #include <net/sctp/sctp.h> | |
62 | #include <net/sctp/sm.h> | |
63 | ||
64 | /* Forward declarations for internal helpers. */ | |
65 | static int sctp_rcv_ootb(struct sk_buff *); | |
66 | static struct sctp_association *__sctp_rcv_lookup(struct sk_buff *skb, | |
67 | const union sctp_addr *laddr, | |
68 | const union sctp_addr *paddr, | |
69 | struct sctp_transport **transportp); | |
70 | static struct sctp_endpoint *__sctp_rcv_lookup_endpoint(const union sctp_addr *laddr); | |
71 | static struct sctp_association *__sctp_lookup_association( | |
72 | const union sctp_addr *local, | |
73 | const union sctp_addr *peer, | |
74 | struct sctp_transport **pt); | |
75 | ||
61c9fed4 VY |
76 | static void sctp_add_backlog(struct sock *sk, struct sk_buff *skb); |
77 | ||
1da177e4 LT |
78 | |
79 | /* Calculate the SCTP checksum of an SCTP packet. */ | |
80 | static inline int sctp_rcv_checksum(struct sk_buff *skb) | |
81 | { | |
82 | struct sctphdr *sh; | |
83 | __u32 cmp, val; | |
84 | struct sk_buff *list = skb_shinfo(skb)->frag_list; | |
85 | ||
86 | sh = (struct sctphdr *) skb->h.raw; | |
87 | cmp = ntohl(sh->checksum); | |
88 | ||
89 | val = sctp_start_cksum((__u8 *)sh, skb_headlen(skb)); | |
90 | ||
91 | for (; list; list = list->next) | |
92 | val = sctp_update_cksum((__u8 *)list->data, skb_headlen(list), | |
93 | val); | |
94 | ||
95 | val = sctp_end_cksum(val); | |
96 | ||
97 | if (val != cmp) { | |
98 | /* CRC failure, dump it. */ | |
99 | SCTP_INC_STATS_BH(SCTP_MIB_CHECKSUMERRORS); | |
100 | return -1; | |
101 | } | |
102 | return 0; | |
103 | } | |
104 | ||
79af02c2 DM |
105 | struct sctp_input_cb { |
106 | union { | |
107 | struct inet_skb_parm h4; | |
108 | #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE) | |
109 | struct inet6_skb_parm h6; | |
110 | #endif | |
111 | } header; | |
112 | struct sctp_chunk *chunk; | |
113 | }; | |
114 | #define SCTP_INPUT_CB(__skb) ((struct sctp_input_cb *)&((__skb)->cb[0])) | |
115 | ||
1da177e4 LT |
116 | /* |
117 | * This is the routine which IP calls when receiving an SCTP packet. | |
118 | */ | |
119 | int sctp_rcv(struct sk_buff *skb) | |
120 | { | |
121 | struct sock *sk; | |
122 | struct sctp_association *asoc; | |
123 | struct sctp_endpoint *ep = NULL; | |
124 | struct sctp_ep_common *rcvr; | |
125 | struct sctp_transport *transport = NULL; | |
126 | struct sctp_chunk *chunk; | |
127 | struct sctphdr *sh; | |
128 | union sctp_addr src; | |
129 | union sctp_addr dest; | |
1c7d1fc1 | 130 | union sctp_addr tmp; |
1da177e4 LT |
131 | int family; |
132 | struct sctp_af *af; | |
1da177e4 LT |
133 | |
134 | if (skb->pkt_type!=PACKET_HOST) | |
135 | goto discard_it; | |
136 | ||
137 | SCTP_INC_STATS_BH(SCTP_MIB_INSCTPPACKS); | |
138 | ||
28cd7752 HX |
139 | if (skb_linearize(skb)) |
140 | goto discard_it; | |
141 | ||
1da177e4 LT |
142 | sh = (struct sctphdr *) skb->h.raw; |
143 | ||
144 | /* Pull up the IP and SCTP headers. */ | |
145 | __skb_pull(skb, skb->h.raw - skb->data); | |
146 | if (skb->len < sizeof(struct sctphdr)) | |
147 | goto discard_it; | |
503b55fd SS |
148 | if ((skb->ip_summed != CHECKSUM_UNNECESSARY) && |
149 | (sctp_rcv_checksum(skb) < 0)) | |
1da177e4 LT |
150 | goto discard_it; |
151 | ||
152 | skb_pull(skb, sizeof(struct sctphdr)); | |
153 | ||
154 | /* Make sure we at least have chunk headers worth of data left. */ | |
155 | if (skb->len < sizeof(struct sctp_chunkhdr)) | |
156 | goto discard_it; | |
157 | ||
158 | family = ipver2af(skb->nh.iph->version); | |
159 | af = sctp_get_af_specific(family); | |
160 | if (unlikely(!af)) | |
161 | goto discard_it; | |
162 | ||
163 | /* Initialize local addresses for lookups. */ | |
164 | af->from_skb(&src, skb, 1); | |
165 | af->from_skb(&dest, skb, 0); | |
166 | ||
167 | /* If the packet is to or from a non-unicast address, | |
168 | * silently discard the packet. | |
169 | * | |
170 | * This is not clearly defined in the RFC except in section | |
171 | * 8.4 - OOTB handling. However, based on the book "Stream Control | |
172 | * Transmission Protocol" 2.1, "It is important to note that the | |
173 | * IP address of an SCTP transport address must be a routable | |
174 | * unicast address. In other words, IP multicast addresses and | |
175 | * IP broadcast addresses cannot be used in an SCTP transport | |
176 | * address." | |
177 | */ | |
5636bef7 VY |
178 | if (!af->addr_valid(&src, NULL, skb) || |
179 | !af->addr_valid(&dest, NULL, skb)) | |
1da177e4 LT |
180 | goto discard_it; |
181 | ||
182 | asoc = __sctp_rcv_lookup(skb, &src, &dest, &transport); | |
183 | ||
1c7d1fc1 AV |
184 | flip_to_n(&tmp, &dest); |
185 | ||
0fd9a65a | 186 | if (!asoc) |
1c7d1fc1 | 187 | ep = __sctp_rcv_lookup_endpoint(&tmp); |
0fd9a65a NH |
188 | |
189 | /* Retrieve the common input handling substructure. */ | |
190 | rcvr = asoc ? &asoc->base : &ep->base; | |
191 | sk = rcvr->sk; | |
192 | ||
193 | /* | |
194 | * If a frame arrives on an interface and the receiving socket is | |
195 | * bound to another interface, via SO_BINDTODEVICE, treat it as OOTB | |
196 | */ | |
197 | if (sk->sk_bound_dev_if && (sk->sk_bound_dev_if != af->skb_iif(skb))) | |
198 | { | |
0fd9a65a NH |
199 | if (asoc) { |
200 | sctp_association_put(asoc); | |
201 | asoc = NULL; | |
202 | } else { | |
203 | sctp_endpoint_put(ep); | |
204 | ep = NULL; | |
205 | } | |
206 | sk = sctp_get_ctl_sock(); | |
207 | ep = sctp_sk(sk)->ep; | |
208 | sctp_endpoint_hold(ep); | |
0fd9a65a NH |
209 | rcvr = &ep->base; |
210 | } | |
211 | ||
1da177e4 LT |
212 | /* |
213 | * RFC 2960, 8.4 - Handle "Out of the blue" Packets. | |
214 | * An SCTP packet is called an "out of the blue" (OOTB) | |
215 | * packet if it is correctly formed, i.e., passed the | |
216 | * receiver's checksum check, but the receiver is not | |
217 | * able to identify the association to which this | |
218 | * packet belongs. | |
219 | */ | |
220 | if (!asoc) { | |
1da177e4 LT |
221 | if (sctp_rcv_ootb(skb)) { |
222 | SCTP_INC_STATS_BH(SCTP_MIB_OUTOFBLUES); | |
223 | goto discard_release; | |
224 | } | |
225 | } | |
226 | ||
1da177e4 LT |
227 | if (!xfrm_policy_check(sk, XFRM_POLICY_IN, skb, family)) |
228 | goto discard_release; | |
b59c2701 | 229 | nf_reset(skb); |
1da177e4 | 230 | |
fda9ef5d | 231 | if (sk_filter(sk, skb)) |
1da177e4 LT |
232 | goto discard_release; |
233 | ||
234 | /* Create an SCTP packet structure. */ | |
235 | chunk = sctp_chunkify(skb, asoc, sk); | |
2babf9da | 236 | if (!chunk) |
1da177e4 | 237 | goto discard_release; |
79af02c2 | 238 | SCTP_INPUT_CB(skb)->chunk = chunk; |
1da177e4 | 239 | |
1da177e4 LT |
240 | /* Remember what endpoint is to handle this packet. */ |
241 | chunk->rcvr = rcvr; | |
242 | ||
243 | /* Remember the SCTP header. */ | |
244 | chunk->sctp_hdr = sh; | |
245 | ||
246 | /* Set the source and destination addresses of the incoming chunk. */ | |
247 | sctp_init_addrs(chunk, &src, &dest); | |
248 | ||
249 | /* Remember where we came from. */ | |
250 | chunk->transport = transport; | |
251 | ||
252 | /* Acquire access to the sock lock. Note: We are safe from other | |
253 | * bottom halves on this lock, but a user may be in the lock too, | |
254 | * so check if it is busy. | |
255 | */ | |
256 | sctp_bh_lock_sock(sk); | |
257 | ||
ac0b0462 SS |
258 | if (sock_owned_by_user(sk)) { |
259 | SCTP_INC_STATS_BH(SCTP_MIB_IN_PKT_BACKLOG); | |
61c9fed4 | 260 | sctp_add_backlog(sk, skb); |
ac0b0462 SS |
261 | } else { |
262 | SCTP_INC_STATS_BH(SCTP_MIB_IN_PKT_SOFTIRQ); | |
61c9fed4 | 263 | sctp_inq_push(&chunk->rcvr->inqueue, chunk); |
ac0b0462 | 264 | } |
1da177e4 | 265 | |
1da177e4 | 266 | sctp_bh_unlock_sock(sk); |
61c9fed4 VY |
267 | |
268 | /* Release the asoc/ep ref we took in the lookup calls. */ | |
269 | if (asoc) | |
270 | sctp_association_put(asoc); | |
271 | else | |
272 | sctp_endpoint_put(ep); | |
7a48f923 | 273 | |
2babf9da | 274 | return 0; |
1da177e4 LT |
275 | |
276 | discard_it: | |
ac0b0462 | 277 | SCTP_INC_STATS_BH(SCTP_MIB_IN_PKT_DISCARDS); |
1da177e4 | 278 | kfree_skb(skb); |
2babf9da | 279 | return 0; |
1da177e4 LT |
280 | |
281 | discard_release: | |
61c9fed4 | 282 | /* Release the asoc/ep ref we took in the lookup calls. */ |
0fd9a65a | 283 | if (asoc) |
1da177e4 | 284 | sctp_association_put(asoc); |
0fd9a65a | 285 | else |
1da177e4 | 286 | sctp_endpoint_put(ep); |
1da177e4 LT |
287 | |
288 | goto discard_it; | |
289 | } | |
290 | ||
61c9fed4 VY |
291 | /* Process the backlog queue of the socket. Every skb on |
292 | * the backlog holds a ref on an association or endpoint. | |
293 | * We hold this ref throughout the state machine to make | |
294 | * sure that the structure we need is still around. | |
1da177e4 LT |
295 | */ |
296 | int sctp_backlog_rcv(struct sock *sk, struct sk_buff *skb) | |
297 | { | |
79af02c2 | 298 | struct sctp_chunk *chunk = SCTP_INPUT_CB(skb)->chunk; |
61c9fed4 | 299 | struct sctp_inq *inqueue = &chunk->rcvr->inqueue; |
7a48f923 | 300 | struct sctp_ep_common *rcvr = NULL; |
61c9fed4 | 301 | int backloged = 0; |
7a48f923 SS |
302 | |
303 | rcvr = chunk->rcvr; | |
c4d2444e | 304 | |
61c9fed4 VY |
305 | /* If the rcvr is dead then the association or endpoint |
306 | * has been deleted and we can safely drop the chunk | |
307 | * and refs that we are holding. | |
308 | */ | |
309 | if (rcvr->dead) { | |
310 | sctp_chunk_free(chunk); | |
311 | goto done; | |
312 | } | |
313 | ||
314 | if (unlikely(rcvr->sk != sk)) { | |
315 | /* In this case, the association moved from one socket to | |
316 | * another. We are currently sitting on the backlog of the | |
317 | * old socket, so we need to move. | |
318 | * However, since we are here in the process context we | |
319 | * need to take make sure that the user doesn't own | |
320 | * the new socket when we process the packet. | |
321 | * If the new socket is user-owned, queue the chunk to the | |
322 | * backlog of the new socket without dropping any refs. | |
323 | * Otherwise, we can safely push the chunk on the inqueue. | |
324 | */ | |
325 | ||
326 | sk = rcvr->sk; | |
327 | sctp_bh_lock_sock(sk); | |
328 | ||
329 | if (sock_owned_by_user(sk)) { | |
330 | sk_add_backlog(sk, skb); | |
331 | backloged = 1; | |
332 | } else | |
333 | sctp_inq_push(inqueue, chunk); | |
334 | ||
335 | sctp_bh_unlock_sock(sk); | |
336 | ||
337 | /* If the chunk was backloged again, don't drop refs */ | |
338 | if (backloged) | |
339 | return 0; | |
340 | } else { | |
341 | sctp_inq_push(inqueue, chunk); | |
342 | } | |
343 | ||
344 | done: | |
345 | /* Release the refs we took in sctp_add_backlog */ | |
346 | if (SCTP_EP_TYPE_ASSOCIATION == rcvr->type) | |
347 | sctp_association_put(sctp_assoc(rcvr)); | |
348 | else if (SCTP_EP_TYPE_SOCKET == rcvr->type) | |
349 | sctp_endpoint_put(sctp_ep(rcvr)); | |
350 | else | |
351 | BUG(); | |
352 | ||
1da177e4 LT |
353 | return 0; |
354 | } | |
355 | ||
61c9fed4 | 356 | static void sctp_add_backlog(struct sock *sk, struct sk_buff *skb) |
c4d2444e | 357 | { |
61c9fed4 VY |
358 | struct sctp_chunk *chunk = SCTP_INPUT_CB(skb)->chunk; |
359 | struct sctp_ep_common *rcvr = chunk->rcvr; | |
c4d2444e | 360 | |
61c9fed4 VY |
361 | /* Hold the assoc/ep while hanging on the backlog queue. |
362 | * This way, we know structures we need will not disappear from us | |
363 | */ | |
364 | if (SCTP_EP_TYPE_ASSOCIATION == rcvr->type) | |
365 | sctp_association_hold(sctp_assoc(rcvr)); | |
366 | else if (SCTP_EP_TYPE_SOCKET == rcvr->type) | |
367 | sctp_endpoint_hold(sctp_ep(rcvr)); | |
368 | else | |
369 | BUG(); | |
370 | ||
371 | sk_add_backlog(sk, skb); | |
c4d2444e SS |
372 | } |
373 | ||
1da177e4 LT |
374 | /* Handle icmp frag needed error. */ |
375 | void sctp_icmp_frag_needed(struct sock *sk, struct sctp_association *asoc, | |
376 | struct sctp_transport *t, __u32 pmtu) | |
377 | { | |
52ccb8e9 FF |
378 | if (sock_owned_by_user(sk) || !t || (t->pathmtu == pmtu)) |
379 | return; | |
380 | ||
381 | if (t->param_flags & SPP_PMTUD_ENABLE) { | |
382 | if (unlikely(pmtu < SCTP_DEFAULT_MINSEGMENT)) { | |
383 | printk(KERN_WARNING "%s: Reported pmtu %d too low, " | |
384 | "using default minimum of %d\n", | |
385 | __FUNCTION__, pmtu, | |
386 | SCTP_DEFAULT_MINSEGMENT); | |
387 | /* Use default minimum segment size and disable | |
388 | * pmtu discovery on this transport. | |
389 | */ | |
390 | t->pathmtu = SCTP_DEFAULT_MINSEGMENT; | |
b56bab46 | 391 | t->param_flags = (t->param_flags & ~SPP_PMTUD) | |
52ccb8e9 FF |
392 | SPP_PMTUD_DISABLE; |
393 | } else { | |
394 | t->pathmtu = pmtu; | |
395 | } | |
1da177e4 | 396 | |
52ccb8e9 | 397 | /* Update association pmtu. */ |
1da177e4 | 398 | sctp_assoc_sync_pmtu(asoc); |
1da177e4 | 399 | } |
52ccb8e9 FF |
400 | |
401 | /* Retransmit with the new pmtu setting. | |
402 | * Normally, if PMTU discovery is disabled, an ICMP Fragmentation | |
403 | * Needed will never be sent, but if a message was sent before | |
404 | * PMTU discovery was disabled that was larger than the PMTU, it | |
405 | * would not be fragmented, so it must be re-transmitted fragmented. | |
406 | */ | |
407 | sctp_retransmit(&asoc->outqueue, t, SCTP_RTXR_PMTUD); | |
1da177e4 LT |
408 | } |
409 | ||
410 | /* | |
411 | * SCTP Implementer's Guide, 2.37 ICMP handling procedures | |
412 | * | |
413 | * ICMP8) If the ICMP code is a "Unrecognized next header type encountered" | |
414 | * or a "Protocol Unreachable" treat this message as an abort | |
415 | * with the T bit set. | |
416 | * | |
417 | * This function sends an event to the state machine, which will abort the | |
418 | * association. | |
419 | * | |
420 | */ | |
421 | void sctp_icmp_proto_unreachable(struct sock *sk, | |
1da177e4 LT |
422 | struct sctp_association *asoc, |
423 | struct sctp_transport *t) | |
424 | { | |
425 | SCTP_DEBUG_PRINTK("%s\n", __FUNCTION__); | |
426 | ||
427 | sctp_do_sm(SCTP_EVENT_T_OTHER, | |
428 | SCTP_ST_OTHER(SCTP_EVENT_ICMP_PROTO_UNREACH), | |
3f7a87d2 | 429 | asoc->state, asoc->ep, asoc, t, |
1da177e4 LT |
430 | GFP_ATOMIC); |
431 | ||
432 | } | |
433 | ||
434 | /* Common lookup code for icmp/icmpv6 error handler. */ | |
435 | struct sock *sctp_err_lookup(int family, struct sk_buff *skb, | |
436 | struct sctphdr *sctphdr, | |
1da177e4 LT |
437 | struct sctp_association **app, |
438 | struct sctp_transport **tpp) | |
439 | { | |
440 | union sctp_addr saddr; | |
441 | union sctp_addr daddr; | |
442 | struct sctp_af *af; | |
443 | struct sock *sk = NULL; | |
8de8c873 | 444 | struct sctp_association *asoc; |
1da177e4 LT |
445 | struct sctp_transport *transport = NULL; |
446 | ||
d1ad1ff2 | 447 | *app = NULL; *tpp = NULL; |
1da177e4 LT |
448 | |
449 | af = sctp_get_af_specific(family); | |
450 | if (unlikely(!af)) { | |
451 | return NULL; | |
452 | } | |
453 | ||
454 | /* Initialize local addresses for lookups. */ | |
455 | af->from_skb(&saddr, skb, 1); | |
456 | af->from_skb(&daddr, skb, 0); | |
457 | ||
458 | /* Look for an association that matches the incoming ICMP error | |
459 | * packet. | |
460 | */ | |
461 | asoc = __sctp_lookup_association(&saddr, &daddr, &transport); | |
d1ad1ff2 SS |
462 | if (!asoc) |
463 | return NULL; | |
1da177e4 | 464 | |
d1ad1ff2 | 465 | sk = asoc->base.sk; |
1da177e4 | 466 | |
d1ad1ff2 SS |
467 | if (ntohl(sctphdr->vtag) != asoc->c.peer_vtag) { |
468 | ICMP_INC_STATS_BH(ICMP_MIB_INERRORS); | |
469 | goto out; | |
470 | } | |
1da177e4 LT |
471 | |
472 | sctp_bh_lock_sock(sk); | |
473 | ||
474 | /* If too many ICMPs get dropped on busy | |
475 | * servers this needs to be solved differently. | |
476 | */ | |
477 | if (sock_owned_by_user(sk)) | |
478 | NET_INC_STATS_BH(LINUX_MIB_LOCKDROPPEDICMPS); | |
479 | ||
1da177e4 LT |
480 | *app = asoc; |
481 | *tpp = transport; | |
482 | return sk; | |
483 | ||
484 | out: | |
1da177e4 LT |
485 | if (asoc) |
486 | sctp_association_put(asoc); | |
1da177e4 LT |
487 | return NULL; |
488 | } | |
489 | ||
490 | /* Common cleanup code for icmp/icmpv6 error handler. */ | |
d1ad1ff2 | 491 | void sctp_err_finish(struct sock *sk, struct sctp_association *asoc) |
1da177e4 LT |
492 | { |
493 | sctp_bh_unlock_sock(sk); | |
1da177e4 LT |
494 | if (asoc) |
495 | sctp_association_put(asoc); | |
1da177e4 LT |
496 | } |
497 | ||
498 | /* | |
499 | * This routine is called by the ICMP module when it gets some | |
500 | * sort of error condition. If err < 0 then the socket should | |
501 | * be closed and the error returned to the user. If err > 0 | |
502 | * it's just the icmp type << 8 | icmp code. After adjustment | |
503 | * header points to the first 8 bytes of the sctp header. We need | |
504 | * to find the appropriate port. | |
505 | * | |
506 | * The locking strategy used here is very "optimistic". When | |
507 | * someone else accesses the socket the ICMP is just dropped | |
508 | * and for some paths there is no check at all. | |
509 | * A more general error queue to queue errors for later handling | |
510 | * is probably better. | |
511 | * | |
512 | */ | |
513 | void sctp_v4_err(struct sk_buff *skb, __u32 info) | |
514 | { | |
515 | struct iphdr *iph = (struct iphdr *)skb->data; | |
516 | struct sctphdr *sh = (struct sctphdr *)(skb->data + (iph->ihl <<2)); | |
517 | int type = skb->h.icmph->type; | |
518 | int code = skb->h.icmph->code; | |
519 | struct sock *sk; | |
8de8c873 | 520 | struct sctp_association *asoc = NULL; |
1da177e4 LT |
521 | struct sctp_transport *transport; |
522 | struct inet_sock *inet; | |
523 | char *saveip, *savesctp; | |
524 | int err; | |
525 | ||
526 | if (skb->len < ((iph->ihl << 2) + 8)) { | |
527 | ICMP_INC_STATS_BH(ICMP_MIB_INERRORS); | |
528 | return; | |
529 | } | |
530 | ||
531 | /* Fix up skb to look at the embedded net header. */ | |
532 | saveip = skb->nh.raw; | |
533 | savesctp = skb->h.raw; | |
534 | skb->nh.iph = iph; | |
535 | skb->h.raw = (char *)sh; | |
d1ad1ff2 | 536 | sk = sctp_err_lookup(AF_INET, skb, sh, &asoc, &transport); |
1da177e4 LT |
537 | /* Put back, the original pointers. */ |
538 | skb->nh.raw = saveip; | |
539 | skb->h.raw = savesctp; | |
540 | if (!sk) { | |
541 | ICMP_INC_STATS_BH(ICMP_MIB_INERRORS); | |
542 | return; | |
543 | } | |
544 | /* Warning: The sock lock is held. Remember to call | |
545 | * sctp_err_finish! | |
546 | */ | |
547 | ||
548 | switch (type) { | |
549 | case ICMP_PARAMETERPROB: | |
550 | err = EPROTO; | |
551 | break; | |
552 | case ICMP_DEST_UNREACH: | |
553 | if (code > NR_ICMP_UNREACH) | |
554 | goto out_unlock; | |
555 | ||
556 | /* PMTU discovery (RFC1191) */ | |
557 | if (ICMP_FRAG_NEEDED == code) { | |
558 | sctp_icmp_frag_needed(sk, asoc, transport, info); | |
559 | goto out_unlock; | |
560 | } | |
561 | else { | |
562 | if (ICMP_PROT_UNREACH == code) { | |
d1ad1ff2 | 563 | sctp_icmp_proto_unreachable(sk, asoc, |
1da177e4 LT |
564 | transport); |
565 | goto out_unlock; | |
566 | } | |
567 | } | |
568 | err = icmp_err_convert[code].errno; | |
569 | break; | |
570 | case ICMP_TIME_EXCEEDED: | |
571 | /* Ignore any time exceeded errors due to fragment reassembly | |
572 | * timeouts. | |
573 | */ | |
574 | if (ICMP_EXC_FRAGTIME == code) | |
575 | goto out_unlock; | |
576 | ||
577 | err = EHOSTUNREACH; | |
578 | break; | |
579 | default: | |
580 | goto out_unlock; | |
581 | } | |
582 | ||
583 | inet = inet_sk(sk); | |
584 | if (!sock_owned_by_user(sk) && inet->recverr) { | |
585 | sk->sk_err = err; | |
586 | sk->sk_error_report(sk); | |
587 | } else { /* Only an error on timeout */ | |
588 | sk->sk_err_soft = err; | |
589 | } | |
590 | ||
591 | out_unlock: | |
d1ad1ff2 | 592 | sctp_err_finish(sk, asoc); |
1da177e4 LT |
593 | } |
594 | ||
595 | /* | |
596 | * RFC 2960, 8.4 - Handle "Out of the blue" Packets. | |
597 | * | |
598 | * This function scans all the chunks in the OOTB packet to determine if | |
599 | * the packet should be discarded right away. If a response might be needed | |
600 | * for this packet, or, if further processing is possible, the packet will | |
601 | * be queued to a proper inqueue for the next phase of handling. | |
602 | * | |
603 | * Output: | |
604 | * Return 0 - If further processing is needed. | |
605 | * Return 1 - If the packet can be discarded right away. | |
606 | */ | |
607 | int sctp_rcv_ootb(struct sk_buff *skb) | |
608 | { | |
609 | sctp_chunkhdr_t *ch; | |
610 | __u8 *ch_end; | |
611 | sctp_errhdr_t *err; | |
612 | ||
613 | ch = (sctp_chunkhdr_t *) skb->data; | |
1da177e4 LT |
614 | |
615 | /* Scan through all the chunks in the packet. */ | |
a7d1f1b6 TF |
616 | do { |
617 | /* Break out if chunk length is less then minimal. */ | |
618 | if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t)) | |
619 | break; | |
620 | ||
621 | ch_end = ((__u8 *)ch) + WORD_ROUND(ntohs(ch->length)); | |
622 | if (ch_end > skb->tail) | |
623 | break; | |
1da177e4 LT |
624 | |
625 | /* RFC 8.4, 2) If the OOTB packet contains an ABORT chunk, the | |
626 | * receiver MUST silently discard the OOTB packet and take no | |
627 | * further action. | |
628 | */ | |
629 | if (SCTP_CID_ABORT == ch->type) | |
630 | goto discard; | |
631 | ||
632 | /* RFC 8.4, 6) If the packet contains a SHUTDOWN COMPLETE | |
633 | * chunk, the receiver should silently discard the packet | |
634 | * and take no further action. | |
635 | */ | |
636 | if (SCTP_CID_SHUTDOWN_COMPLETE == ch->type) | |
637 | goto discard; | |
638 | ||
639 | /* RFC 8.4, 7) If the packet contains a "Stale cookie" ERROR | |
640 | * or a COOKIE ACK the SCTP Packet should be silently | |
641 | * discarded. | |
642 | */ | |
643 | if (SCTP_CID_COOKIE_ACK == ch->type) | |
644 | goto discard; | |
645 | ||
646 | if (SCTP_CID_ERROR == ch->type) { | |
647 | sctp_walk_errors(err, ch) { | |
648 | if (SCTP_ERROR_STALE_COOKIE == err->cause) | |
649 | goto discard; | |
650 | } | |
651 | } | |
652 | ||
653 | ch = (sctp_chunkhdr_t *) ch_end; | |
a7d1f1b6 | 654 | } while (ch_end < skb->tail); |
1da177e4 LT |
655 | |
656 | return 0; | |
657 | ||
658 | discard: | |
659 | return 1; | |
660 | } | |
661 | ||
662 | /* Insert endpoint into the hash table. */ | |
663 | static void __sctp_hash_endpoint(struct sctp_endpoint *ep) | |
664 | { | |
665 | struct sctp_ep_common **epp; | |
666 | struct sctp_ep_common *epb; | |
667 | struct sctp_hashbucket *head; | |
668 | ||
669 | epb = &ep->base; | |
670 | ||
671 | epb->hashent = sctp_ep_hashfn(epb->bind_addr.port); | |
672 | head = &sctp_ep_hashtable[epb->hashent]; | |
673 | ||
674 | sctp_write_lock(&head->lock); | |
675 | epp = &head->chain; | |
676 | epb->next = *epp; | |
677 | if (epb->next) | |
678 | (*epp)->pprev = &epb->next; | |
679 | *epp = epb; | |
680 | epb->pprev = epp; | |
681 | sctp_write_unlock(&head->lock); | |
682 | } | |
683 | ||
684 | /* Add an endpoint to the hash. Local BH-safe. */ | |
685 | void sctp_hash_endpoint(struct sctp_endpoint *ep) | |
686 | { | |
687 | sctp_local_bh_disable(); | |
688 | __sctp_hash_endpoint(ep); | |
689 | sctp_local_bh_enable(); | |
690 | } | |
691 | ||
692 | /* Remove endpoint from the hash table. */ | |
693 | static void __sctp_unhash_endpoint(struct sctp_endpoint *ep) | |
694 | { | |
695 | struct sctp_hashbucket *head; | |
696 | struct sctp_ep_common *epb; | |
697 | ||
698 | epb = &ep->base; | |
699 | ||
700 | epb->hashent = sctp_ep_hashfn(epb->bind_addr.port); | |
701 | ||
702 | head = &sctp_ep_hashtable[epb->hashent]; | |
703 | ||
704 | sctp_write_lock(&head->lock); | |
705 | ||
706 | if (epb->pprev) { | |
707 | if (epb->next) | |
708 | epb->next->pprev = epb->pprev; | |
709 | *epb->pprev = epb->next; | |
710 | epb->pprev = NULL; | |
711 | } | |
712 | ||
713 | sctp_write_unlock(&head->lock); | |
714 | } | |
715 | ||
716 | /* Remove endpoint from the hash. Local BH-safe. */ | |
717 | void sctp_unhash_endpoint(struct sctp_endpoint *ep) | |
718 | { | |
719 | sctp_local_bh_disable(); | |
720 | __sctp_unhash_endpoint(ep); | |
721 | sctp_local_bh_enable(); | |
722 | } | |
723 | ||
724 | /* Look up an endpoint. */ | |
725 | static struct sctp_endpoint *__sctp_rcv_lookup_endpoint(const union sctp_addr *laddr) | |
726 | { | |
727 | struct sctp_hashbucket *head; | |
728 | struct sctp_ep_common *epb; | |
729 | struct sctp_endpoint *ep; | |
730 | int hash; | |
731 | ||
1c7d1fc1 | 732 | hash = sctp_ep_hashfn(ntohs(laddr->v4.sin_port)); |
1da177e4 LT |
733 | head = &sctp_ep_hashtable[hash]; |
734 | read_lock(&head->lock); | |
735 | for (epb = head->chain; epb; epb = epb->next) { | |
736 | ep = sctp_ep(epb); | |
737 | if (sctp_endpoint_is_match(ep, laddr)) | |
738 | goto hit; | |
739 | } | |
740 | ||
741 | ep = sctp_sk((sctp_get_ctl_sock()))->ep; | |
742 | epb = &ep->base; | |
743 | ||
744 | hit: | |
745 | sctp_endpoint_hold(ep); | |
1da177e4 LT |
746 | read_unlock(&head->lock); |
747 | return ep; | |
748 | } | |
749 | ||
750 | /* Insert association into the hash table. */ | |
751 | static void __sctp_hash_established(struct sctp_association *asoc) | |
752 | { | |
753 | struct sctp_ep_common **epp; | |
754 | struct sctp_ep_common *epb; | |
755 | struct sctp_hashbucket *head; | |
756 | ||
757 | epb = &asoc->base; | |
758 | ||
759 | /* Calculate which chain this entry will belong to. */ | |
760 | epb->hashent = sctp_assoc_hashfn(epb->bind_addr.port, asoc->peer.port); | |
761 | ||
762 | head = &sctp_assoc_hashtable[epb->hashent]; | |
763 | ||
764 | sctp_write_lock(&head->lock); | |
765 | epp = &head->chain; | |
766 | epb->next = *epp; | |
767 | if (epb->next) | |
768 | (*epp)->pprev = &epb->next; | |
769 | *epp = epb; | |
770 | epb->pprev = epp; | |
771 | sctp_write_unlock(&head->lock); | |
772 | } | |
773 | ||
774 | /* Add an association to the hash. Local BH-safe. */ | |
775 | void sctp_hash_established(struct sctp_association *asoc) | |
776 | { | |
de76e695 VY |
777 | if (asoc->temp) |
778 | return; | |
779 | ||
1da177e4 LT |
780 | sctp_local_bh_disable(); |
781 | __sctp_hash_established(asoc); | |
782 | sctp_local_bh_enable(); | |
783 | } | |
784 | ||
785 | /* Remove association from the hash table. */ | |
786 | static void __sctp_unhash_established(struct sctp_association *asoc) | |
787 | { | |
788 | struct sctp_hashbucket *head; | |
789 | struct sctp_ep_common *epb; | |
790 | ||
791 | epb = &asoc->base; | |
792 | ||
793 | epb->hashent = sctp_assoc_hashfn(epb->bind_addr.port, | |
794 | asoc->peer.port); | |
795 | ||
796 | head = &sctp_assoc_hashtable[epb->hashent]; | |
797 | ||
798 | sctp_write_lock(&head->lock); | |
799 | ||
800 | if (epb->pprev) { | |
801 | if (epb->next) | |
802 | epb->next->pprev = epb->pprev; | |
803 | *epb->pprev = epb->next; | |
804 | epb->pprev = NULL; | |
805 | } | |
806 | ||
807 | sctp_write_unlock(&head->lock); | |
808 | } | |
809 | ||
810 | /* Remove association from the hash table. Local BH-safe. */ | |
811 | void sctp_unhash_established(struct sctp_association *asoc) | |
812 | { | |
de76e695 VY |
813 | if (asoc->temp) |
814 | return; | |
815 | ||
1da177e4 LT |
816 | sctp_local_bh_disable(); |
817 | __sctp_unhash_established(asoc); | |
818 | sctp_local_bh_enable(); | |
819 | } | |
820 | ||
821 | /* Look up an association. */ | |
822 | static struct sctp_association *__sctp_lookup_association( | |
823 | const union sctp_addr *local, | |
824 | const union sctp_addr *peer, | |
825 | struct sctp_transport **pt) | |
826 | { | |
827 | struct sctp_hashbucket *head; | |
828 | struct sctp_ep_common *epb; | |
829 | struct sctp_association *asoc; | |
830 | struct sctp_transport *transport; | |
831 | int hash; | |
832 | ||
833 | /* Optimize here for direct hit, only listening connections can | |
834 | * have wildcards anyways. | |
835 | */ | |
836 | hash = sctp_assoc_hashfn(local->v4.sin_port, peer->v4.sin_port); | |
837 | head = &sctp_assoc_hashtable[hash]; | |
838 | read_lock(&head->lock); | |
839 | for (epb = head->chain; epb; epb = epb->next) { | |
840 | asoc = sctp_assoc(epb); | |
841 | transport = sctp_assoc_is_match(asoc, local, peer); | |
842 | if (transport) | |
843 | goto hit; | |
844 | } | |
845 | ||
846 | read_unlock(&head->lock); | |
847 | ||
848 | return NULL; | |
849 | ||
850 | hit: | |
851 | *pt = transport; | |
852 | sctp_association_hold(asoc); | |
1da177e4 LT |
853 | read_unlock(&head->lock); |
854 | return asoc; | |
855 | } | |
856 | ||
857 | /* Look up an association. BH-safe. */ | |
858 | SCTP_STATIC | |
859 | struct sctp_association *sctp_lookup_association(const union sctp_addr *laddr, | |
860 | const union sctp_addr *paddr, | |
861 | struct sctp_transport **transportp) | |
862 | { | |
863 | struct sctp_association *asoc; | |
864 | ||
865 | sctp_local_bh_disable(); | |
866 | asoc = __sctp_lookup_association(laddr, paddr, transportp); | |
867 | sctp_local_bh_enable(); | |
868 | ||
869 | return asoc; | |
870 | } | |
871 | ||
872 | /* Is there an association matching the given local and peer addresses? */ | |
873 | int sctp_has_association(const union sctp_addr *laddr, | |
874 | const union sctp_addr *paddr) | |
875 | { | |
876 | struct sctp_association *asoc; | |
877 | struct sctp_transport *transport; | |
878 | ||
879 | if ((asoc = sctp_lookup_association(laddr, paddr, &transport))) { | |
1da177e4 LT |
880 | sctp_association_put(asoc); |
881 | return 1; | |
882 | } | |
883 | ||
884 | return 0; | |
885 | } | |
886 | ||
887 | /* | |
888 | * SCTP Implementors Guide, 2.18 Handling of address | |
889 | * parameters within the INIT or INIT-ACK. | |
890 | * | |
891 | * D) When searching for a matching TCB upon reception of an INIT | |
892 | * or INIT-ACK chunk the receiver SHOULD use not only the | |
893 | * source address of the packet (containing the INIT or | |
894 | * INIT-ACK) but the receiver SHOULD also use all valid | |
895 | * address parameters contained within the chunk. | |
896 | * | |
897 | * 2.18.3 Solution description | |
898 | * | |
899 | * This new text clearly specifies to an implementor the need | |
900 | * to look within the INIT or INIT-ACK. Any implementation that | |
901 | * does not do this, may not be able to establish associations | |
902 | * in certain circumstances. | |
903 | * | |
904 | */ | |
905 | static struct sctp_association *__sctp_rcv_init_lookup(struct sk_buff *skb, | |
906 | const union sctp_addr *laddr, struct sctp_transport **transportp) | |
907 | { | |
908 | struct sctp_association *asoc; | |
909 | union sctp_addr addr; | |
910 | union sctp_addr *paddr = &addr; | |
911 | struct sctphdr *sh = (struct sctphdr *) skb->h.raw; | |
912 | sctp_chunkhdr_t *ch; | |
913 | union sctp_params params; | |
914 | sctp_init_chunk_t *init; | |
915 | struct sctp_transport *transport; | |
916 | struct sctp_af *af; | |
917 | ||
918 | ch = (sctp_chunkhdr_t *) skb->data; | |
919 | ||
920 | /* If this is INIT/INIT-ACK look inside the chunk too. */ | |
921 | switch (ch->type) { | |
922 | case SCTP_CID_INIT: | |
923 | case SCTP_CID_INIT_ACK: | |
924 | break; | |
925 | default: | |
926 | return NULL; | |
927 | } | |
928 | ||
929 | /* The code below will attempt to walk the chunk and extract | |
930 | * parameter information. Before we do that, we need to verify | |
931 | * that the chunk length doesn't cause overflow. Otherwise, we'll | |
932 | * walk off the end. | |
933 | */ | |
934 | if (WORD_ROUND(ntohs(ch->length)) > skb->len) | |
935 | return NULL; | |
936 | ||
937 | /* | |
938 | * This code will NOT touch anything inside the chunk--it is | |
939 | * strictly READ-ONLY. | |
940 | * | |
941 | * RFC 2960 3 SCTP packet Format | |
942 | * | |
943 | * Multiple chunks can be bundled into one SCTP packet up to | |
944 | * the MTU size, except for the INIT, INIT ACK, and SHUTDOWN | |
945 | * COMPLETE chunks. These chunks MUST NOT be bundled with any | |
946 | * other chunk in a packet. See Section 6.10 for more details | |
947 | * on chunk bundling. | |
948 | */ | |
949 | ||
950 | /* Find the start of the TLVs and the end of the chunk. This is | |
951 | * the region we search for address parameters. | |
952 | */ | |
953 | init = (sctp_init_chunk_t *)skb->data; | |
954 | ||
955 | /* Walk the parameters looking for embedded addresses. */ | |
956 | sctp_walk_params(params, init, init_hdr.params) { | |
957 | ||
958 | /* Note: Ignoring hostname addresses. */ | |
959 | af = sctp_get_af_specific(param_type2af(params.p->type)); | |
960 | if (!af) | |
961 | continue; | |
962 | ||
963 | af->from_addr_param(paddr, params.addr, ntohs(sh->source), 0); | |
964 | ||
965 | asoc = __sctp_lookup_association(laddr, paddr, &transport); | |
966 | if (asoc) | |
967 | return asoc; | |
968 | } | |
969 | ||
970 | return NULL; | |
971 | } | |
972 | ||
973 | /* Lookup an association for an inbound skb. */ | |
974 | static struct sctp_association *__sctp_rcv_lookup(struct sk_buff *skb, | |
975 | const union sctp_addr *paddr, | |
976 | const union sctp_addr *laddr, | |
977 | struct sctp_transport **transportp) | |
978 | { | |
979 | struct sctp_association *asoc; | |
980 | ||
981 | asoc = __sctp_lookup_association(laddr, paddr, transportp); | |
982 | ||
983 | /* Further lookup for INIT/INIT-ACK packets. | |
984 | * SCTP Implementors Guide, 2.18 Handling of address | |
985 | * parameters within the INIT or INIT-ACK. | |
986 | */ | |
987 | if (!asoc) | |
988 | asoc = __sctp_rcv_init_lookup(skb, laddr, transportp); | |
989 | ||
990 | return asoc; | |
991 | } |