Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
| 76 | |
| 77 | /* Calculate the SCTP checksum of an SCTP packet. */ |
| 78 | static inline int sctp_rcv_checksum(struct sk_buff *skb) |
| 79 | { |
| 80 | struct sctphdr *sh; |
| 81 | __u32 cmp, val; |
| 82 | struct sk_buff *list = skb_shinfo(skb)->frag_list; |
| 83 | |
| 84 | sh = (struct sctphdr *) skb->h.raw; |
| 85 | cmp = ntohl(sh->checksum); |
| 86 | |
| 87 | val = sctp_start_cksum((__u8 *)sh, skb_headlen(skb)); |
| 88 | |
| 89 | for (; list; list = list->next) |
| 90 | val = sctp_update_cksum((__u8 *)list->data, skb_headlen(list), |
| 91 | val); |
| 92 | |
| 93 | val = sctp_end_cksum(val); |
| 94 | |
| 95 | if (val != cmp) { |
| 96 | /* CRC failure, dump it. */ |
| 97 | SCTP_INC_STATS_BH(SCTP_MIB_CHECKSUMERRORS); |
| 98 | return -1; |
| 99 | } |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | /* The free routine for skbuffs that sctp receives */ |
| 104 | static void sctp_rfree(struct sk_buff *skb) |
| 105 | { |
| 106 | atomic_sub(sizeof(struct sctp_chunk),&skb->sk->sk_rmem_alloc); |
| 107 | sock_rfree(skb); |
| 108 | } |
| 109 | |
| 110 | /* The ownership wrapper routine to do receive buffer accounting */ |
| 111 | static void sctp_rcv_set_owner_r(struct sk_buff *skb, struct sock *sk) |
| 112 | { |
| 113 | skb_set_owner_r(skb,sk); |
| 114 | skb->destructor = sctp_rfree; |
| 115 | atomic_add(sizeof(struct sctp_chunk),&sk->sk_rmem_alloc); |
| 116 | } |
| 117 | |
David S. Miller | 79af02c | 2005-07-08 21:47:49 -0700 | [diff] [blame] | 118 | struct sctp_input_cb { |
| 119 | union { |
| 120 | struct inet_skb_parm h4; |
| 121 | #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE) |
| 122 | struct inet6_skb_parm h6; |
| 123 | #endif |
| 124 | } header; |
| 125 | struct sctp_chunk *chunk; |
| 126 | }; |
| 127 | #define SCTP_INPUT_CB(__skb) ((struct sctp_input_cb *)&((__skb)->cb[0])) |
| 128 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | /* |
| 130 | * This is the routine which IP calls when receiving an SCTP packet. |
| 131 | */ |
| 132 | int sctp_rcv(struct sk_buff *skb) |
| 133 | { |
| 134 | struct sock *sk; |
| 135 | struct sctp_association *asoc; |
| 136 | struct sctp_endpoint *ep = NULL; |
| 137 | struct sctp_ep_common *rcvr; |
| 138 | struct sctp_transport *transport = NULL; |
| 139 | struct sctp_chunk *chunk; |
| 140 | struct sctphdr *sh; |
| 141 | union sctp_addr src; |
| 142 | union sctp_addr dest; |
| 143 | int family; |
| 144 | struct sctp_af *af; |
| 145 | int ret = 0; |
| 146 | |
| 147 | if (skb->pkt_type!=PACKET_HOST) |
| 148 | goto discard_it; |
| 149 | |
| 150 | SCTP_INC_STATS_BH(SCTP_MIB_INSCTPPACKS); |
| 151 | |
| 152 | sh = (struct sctphdr *) skb->h.raw; |
| 153 | |
| 154 | /* Pull up the IP and SCTP headers. */ |
| 155 | __skb_pull(skb, skb->h.raw - skb->data); |
| 156 | if (skb->len < sizeof(struct sctphdr)) |
| 157 | goto discard_it; |
| 158 | if (sctp_rcv_checksum(skb) < 0) |
| 159 | goto discard_it; |
| 160 | |
| 161 | skb_pull(skb, sizeof(struct sctphdr)); |
| 162 | |
| 163 | /* Make sure we at least have chunk headers worth of data left. */ |
| 164 | if (skb->len < sizeof(struct sctp_chunkhdr)) |
| 165 | goto discard_it; |
| 166 | |
| 167 | family = ipver2af(skb->nh.iph->version); |
| 168 | af = sctp_get_af_specific(family); |
| 169 | if (unlikely(!af)) |
| 170 | goto discard_it; |
| 171 | |
| 172 | /* Initialize local addresses for lookups. */ |
| 173 | af->from_skb(&src, skb, 1); |
| 174 | af->from_skb(&dest, skb, 0); |
| 175 | |
| 176 | /* If the packet is to or from a non-unicast address, |
| 177 | * silently discard the packet. |
| 178 | * |
| 179 | * This is not clearly defined in the RFC except in section |
| 180 | * 8.4 - OOTB handling. However, based on the book "Stream Control |
| 181 | * Transmission Protocol" 2.1, "It is important to note that the |
| 182 | * IP address of an SCTP transport address must be a routable |
| 183 | * unicast address. In other words, IP multicast addresses and |
| 184 | * IP broadcast addresses cannot be used in an SCTP transport |
| 185 | * address." |
| 186 | */ |
| 187 | if (!af->addr_valid(&src, NULL) || !af->addr_valid(&dest, NULL)) |
| 188 | goto discard_it; |
| 189 | |
| 190 | asoc = __sctp_rcv_lookup(skb, &src, &dest, &transport); |
| 191 | |
Neil Horman | 0fd9a65 | 2005-06-13 15:11:24 -0700 | [diff] [blame] | 192 | if (!asoc) |
| 193 | ep = __sctp_rcv_lookup_endpoint(&dest); |
| 194 | |
| 195 | /* Retrieve the common input handling substructure. */ |
| 196 | rcvr = asoc ? &asoc->base : &ep->base; |
| 197 | sk = rcvr->sk; |
| 198 | |
| 199 | /* |
| 200 | * If a frame arrives on an interface and the receiving socket is |
| 201 | * bound to another interface, via SO_BINDTODEVICE, treat it as OOTB |
| 202 | */ |
| 203 | if (sk->sk_bound_dev_if && (sk->sk_bound_dev_if != af->skb_iif(skb))) |
| 204 | { |
| 205 | sock_put(sk); |
| 206 | if (asoc) { |
| 207 | sctp_association_put(asoc); |
| 208 | asoc = NULL; |
| 209 | } else { |
| 210 | sctp_endpoint_put(ep); |
| 211 | ep = NULL; |
| 212 | } |
| 213 | sk = sctp_get_ctl_sock(); |
| 214 | ep = sctp_sk(sk)->ep; |
| 215 | sctp_endpoint_hold(ep); |
| 216 | sock_hold(sk); |
| 217 | rcvr = &ep->base; |
| 218 | } |
| 219 | |
| 220 | if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf) |
| 221 | goto discard_release; |
| 222 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | /* |
| 224 | * RFC 2960, 8.4 - Handle "Out of the blue" Packets. |
| 225 | * An SCTP packet is called an "out of the blue" (OOTB) |
| 226 | * packet if it is correctly formed, i.e., passed the |
| 227 | * receiver's checksum check, but the receiver is not |
| 228 | * able to identify the association to which this |
| 229 | * packet belongs. |
| 230 | */ |
| 231 | if (!asoc) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | if (sctp_rcv_ootb(skb)) { |
| 233 | SCTP_INC_STATS_BH(SCTP_MIB_OUTOFBLUES); |
| 234 | goto discard_release; |
| 235 | } |
| 236 | } |
| 237 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | /* SCTP seems to always need a timestamp right now (FIXME) */ |
Patrick McHardy | a61bbcf | 2005-08-14 17:24:31 -0700 | [diff] [blame^] | 239 | if (skb->tstamp.off_sec == 0) { |
| 240 | __net_timestamp(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | sock_enable_timestamp(sk); |
| 242 | } |
| 243 | |
| 244 | if (!xfrm_policy_check(sk, XFRM_POLICY_IN, skb, family)) |
| 245 | goto discard_release; |
| 246 | |
| 247 | ret = sk_filter(sk, skb, 1); |
| 248 | if (ret) |
| 249 | goto discard_release; |
| 250 | |
| 251 | /* Create an SCTP packet structure. */ |
| 252 | chunk = sctp_chunkify(skb, asoc, sk); |
| 253 | if (!chunk) { |
| 254 | ret = -ENOMEM; |
| 255 | goto discard_release; |
| 256 | } |
David S. Miller | 79af02c | 2005-07-08 21:47:49 -0700 | [diff] [blame] | 257 | SCTP_INPUT_CB(skb)->chunk = chunk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | |
| 259 | sctp_rcv_set_owner_r(skb,sk); |
| 260 | |
| 261 | /* Remember what endpoint is to handle this packet. */ |
| 262 | chunk->rcvr = rcvr; |
| 263 | |
| 264 | /* Remember the SCTP header. */ |
| 265 | chunk->sctp_hdr = sh; |
| 266 | |
| 267 | /* Set the source and destination addresses of the incoming chunk. */ |
| 268 | sctp_init_addrs(chunk, &src, &dest); |
| 269 | |
| 270 | /* Remember where we came from. */ |
| 271 | chunk->transport = transport; |
| 272 | |
| 273 | /* Acquire access to the sock lock. Note: We are safe from other |
| 274 | * bottom halves on this lock, but a user may be in the lock too, |
| 275 | * so check if it is busy. |
| 276 | */ |
| 277 | sctp_bh_lock_sock(sk); |
| 278 | |
| 279 | if (sock_owned_by_user(sk)) |
David S. Miller | 79af02c | 2005-07-08 21:47:49 -0700 | [diff] [blame] | 280 | sk_add_backlog(sk, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | else |
David S. Miller | 79af02c | 2005-07-08 21:47:49 -0700 | [diff] [blame] | 282 | sctp_backlog_rcv(sk, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | |
| 284 | /* Release the sock and any reference counts we took in the |
| 285 | * lookup calls. |
| 286 | */ |
| 287 | sctp_bh_unlock_sock(sk); |
| 288 | if (asoc) |
| 289 | sctp_association_put(asoc); |
| 290 | else |
| 291 | sctp_endpoint_put(ep); |
| 292 | sock_put(sk); |
| 293 | return ret; |
| 294 | |
| 295 | discard_it: |
| 296 | kfree_skb(skb); |
| 297 | return ret; |
| 298 | |
| 299 | discard_release: |
| 300 | /* Release any structures we may be holding. */ |
Neil Horman | 0fd9a65 | 2005-06-13 15:11:24 -0700 | [diff] [blame] | 301 | sock_put(sk); |
| 302 | if (asoc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | sctp_association_put(asoc); |
Neil Horman | 0fd9a65 | 2005-06-13 15:11:24 -0700 | [diff] [blame] | 304 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | sctp_endpoint_put(ep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | |
| 307 | goto discard_it; |
| 308 | } |
| 309 | |
| 310 | /* Handle second half of inbound skb processing. If the sock was busy, |
| 311 | * we may have need to delay processing until later when the sock is |
| 312 | * released (on the backlog). If not busy, we call this routine |
| 313 | * directly from the bottom half. |
| 314 | */ |
| 315 | int sctp_backlog_rcv(struct sock *sk, struct sk_buff *skb) |
| 316 | { |
David S. Miller | 79af02c | 2005-07-08 21:47:49 -0700 | [diff] [blame] | 317 | struct sctp_chunk *chunk = SCTP_INPUT_CB(skb)->chunk; |
| 318 | struct sctp_inq *inqueue = &chunk->rcvr->inqueue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | |
| 320 | sctp_inq_push(inqueue, chunk); |
| 321 | return 0; |
| 322 | } |
| 323 | |
| 324 | /* Handle icmp frag needed error. */ |
| 325 | void sctp_icmp_frag_needed(struct sock *sk, struct sctp_association *asoc, |
| 326 | struct sctp_transport *t, __u32 pmtu) |
| 327 | { |
| 328 | if (unlikely(pmtu < SCTP_DEFAULT_MINSEGMENT)) { |
| 329 | printk(KERN_WARNING "%s: Reported pmtu %d too low, " |
| 330 | "using default minimum of %d\n", __FUNCTION__, pmtu, |
| 331 | SCTP_DEFAULT_MINSEGMENT); |
| 332 | pmtu = SCTP_DEFAULT_MINSEGMENT; |
| 333 | } |
| 334 | |
| 335 | if (!sock_owned_by_user(sk) && t && (t->pmtu != pmtu)) { |
| 336 | t->pmtu = pmtu; |
| 337 | sctp_assoc_sync_pmtu(asoc); |
| 338 | sctp_retransmit(&asoc->outqueue, t, SCTP_RTXR_PMTUD); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | /* |
| 343 | * SCTP Implementer's Guide, 2.37 ICMP handling procedures |
| 344 | * |
| 345 | * ICMP8) If the ICMP code is a "Unrecognized next header type encountered" |
| 346 | * or a "Protocol Unreachable" treat this message as an abort |
| 347 | * with the T bit set. |
| 348 | * |
| 349 | * This function sends an event to the state machine, which will abort the |
| 350 | * association. |
| 351 | * |
| 352 | */ |
| 353 | void sctp_icmp_proto_unreachable(struct sock *sk, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | struct sctp_association *asoc, |
| 355 | struct sctp_transport *t) |
| 356 | { |
| 357 | SCTP_DEBUG_PRINTK("%s\n", __FUNCTION__); |
| 358 | |
| 359 | sctp_do_sm(SCTP_EVENT_T_OTHER, |
| 360 | SCTP_ST_OTHER(SCTP_EVENT_ICMP_PROTO_UNREACH), |
Frank Filz | 3f7a87d | 2005-06-20 13:14:57 -0700 | [diff] [blame] | 361 | asoc->state, asoc->ep, asoc, t, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | GFP_ATOMIC); |
| 363 | |
| 364 | } |
| 365 | |
| 366 | /* Common lookup code for icmp/icmpv6 error handler. */ |
| 367 | struct sock *sctp_err_lookup(int family, struct sk_buff *skb, |
| 368 | struct sctphdr *sctphdr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | struct sctp_association **app, |
| 370 | struct sctp_transport **tpp) |
| 371 | { |
| 372 | union sctp_addr saddr; |
| 373 | union sctp_addr daddr; |
| 374 | struct sctp_af *af; |
| 375 | struct sock *sk = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | struct sctp_association *asoc = NULL; |
| 377 | struct sctp_transport *transport = NULL; |
| 378 | |
Sridhar Samudrala | d1ad1ff | 2005-07-18 13:44:10 -0700 | [diff] [blame] | 379 | *app = NULL; *tpp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | |
| 381 | af = sctp_get_af_specific(family); |
| 382 | if (unlikely(!af)) { |
| 383 | return NULL; |
| 384 | } |
| 385 | |
| 386 | /* Initialize local addresses for lookups. */ |
| 387 | af->from_skb(&saddr, skb, 1); |
| 388 | af->from_skb(&daddr, skb, 0); |
| 389 | |
| 390 | /* Look for an association that matches the incoming ICMP error |
| 391 | * packet. |
| 392 | */ |
| 393 | asoc = __sctp_lookup_association(&saddr, &daddr, &transport); |
Sridhar Samudrala | d1ad1ff | 2005-07-18 13:44:10 -0700 | [diff] [blame] | 394 | if (!asoc) |
| 395 | return NULL; |
| 396 | |
| 397 | sk = asoc->base.sk; |
| 398 | |
| 399 | if (ntohl(sctphdr->vtag) != asoc->c.peer_vtag) { |
| 400 | ICMP_INC_STATS_BH(ICMP_MIB_INERRORS); |
| 401 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | } |
| 403 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | sctp_bh_lock_sock(sk); |
| 405 | |
| 406 | /* If too many ICMPs get dropped on busy |
| 407 | * servers this needs to be solved differently. |
| 408 | */ |
| 409 | if (sock_owned_by_user(sk)) |
| 410 | NET_INC_STATS_BH(LINUX_MIB_LOCKDROPPEDICMPS); |
| 411 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | *app = asoc; |
| 413 | *tpp = transport; |
| 414 | return sk; |
| 415 | |
| 416 | out: |
| 417 | sock_put(sk); |
| 418 | if (asoc) |
| 419 | sctp_association_put(asoc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | return NULL; |
| 421 | } |
| 422 | |
| 423 | /* Common cleanup code for icmp/icmpv6 error handler. */ |
Sridhar Samudrala | d1ad1ff | 2005-07-18 13:44:10 -0700 | [diff] [blame] | 424 | void sctp_err_finish(struct sock *sk, struct sctp_association *asoc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | { |
| 426 | sctp_bh_unlock_sock(sk); |
| 427 | sock_put(sk); |
| 428 | if (asoc) |
| 429 | sctp_association_put(asoc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | /* |
| 433 | * This routine is called by the ICMP module when it gets some |
| 434 | * sort of error condition. If err < 0 then the socket should |
| 435 | * be closed and the error returned to the user. If err > 0 |
| 436 | * it's just the icmp type << 8 | icmp code. After adjustment |
| 437 | * header points to the first 8 bytes of the sctp header. We need |
| 438 | * to find the appropriate port. |
| 439 | * |
| 440 | * The locking strategy used here is very "optimistic". When |
| 441 | * someone else accesses the socket the ICMP is just dropped |
| 442 | * and for some paths there is no check at all. |
| 443 | * A more general error queue to queue errors for later handling |
| 444 | * is probably better. |
| 445 | * |
| 446 | */ |
| 447 | void sctp_v4_err(struct sk_buff *skb, __u32 info) |
| 448 | { |
| 449 | struct iphdr *iph = (struct iphdr *)skb->data; |
| 450 | struct sctphdr *sh = (struct sctphdr *)(skb->data + (iph->ihl <<2)); |
| 451 | int type = skb->h.icmph->type; |
| 452 | int code = skb->h.icmph->code; |
| 453 | struct sock *sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | struct sctp_association *asoc; |
| 455 | struct sctp_transport *transport; |
| 456 | struct inet_sock *inet; |
| 457 | char *saveip, *savesctp; |
| 458 | int err; |
| 459 | |
| 460 | if (skb->len < ((iph->ihl << 2) + 8)) { |
| 461 | ICMP_INC_STATS_BH(ICMP_MIB_INERRORS); |
| 462 | return; |
| 463 | } |
| 464 | |
| 465 | /* Fix up skb to look at the embedded net header. */ |
| 466 | saveip = skb->nh.raw; |
| 467 | savesctp = skb->h.raw; |
| 468 | skb->nh.iph = iph; |
| 469 | skb->h.raw = (char *)sh; |
Sridhar Samudrala | d1ad1ff | 2005-07-18 13:44:10 -0700 | [diff] [blame] | 470 | sk = sctp_err_lookup(AF_INET, skb, sh, &asoc, &transport); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | /* Put back, the original pointers. */ |
| 472 | skb->nh.raw = saveip; |
| 473 | skb->h.raw = savesctp; |
| 474 | if (!sk) { |
| 475 | ICMP_INC_STATS_BH(ICMP_MIB_INERRORS); |
| 476 | return; |
| 477 | } |
| 478 | /* Warning: The sock lock is held. Remember to call |
| 479 | * sctp_err_finish! |
| 480 | */ |
| 481 | |
| 482 | switch (type) { |
| 483 | case ICMP_PARAMETERPROB: |
| 484 | err = EPROTO; |
| 485 | break; |
| 486 | case ICMP_DEST_UNREACH: |
| 487 | if (code > NR_ICMP_UNREACH) |
| 488 | goto out_unlock; |
| 489 | |
| 490 | /* PMTU discovery (RFC1191) */ |
| 491 | if (ICMP_FRAG_NEEDED == code) { |
| 492 | sctp_icmp_frag_needed(sk, asoc, transport, info); |
| 493 | goto out_unlock; |
| 494 | } |
| 495 | else { |
| 496 | if (ICMP_PROT_UNREACH == code) { |
Sridhar Samudrala | d1ad1ff | 2005-07-18 13:44:10 -0700 | [diff] [blame] | 497 | sctp_icmp_proto_unreachable(sk, asoc, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | transport); |
| 499 | goto out_unlock; |
| 500 | } |
| 501 | } |
| 502 | err = icmp_err_convert[code].errno; |
| 503 | break; |
| 504 | case ICMP_TIME_EXCEEDED: |
| 505 | /* Ignore any time exceeded errors due to fragment reassembly |
| 506 | * timeouts. |
| 507 | */ |
| 508 | if (ICMP_EXC_FRAGTIME == code) |
| 509 | goto out_unlock; |
| 510 | |
| 511 | err = EHOSTUNREACH; |
| 512 | break; |
| 513 | default: |
| 514 | goto out_unlock; |
| 515 | } |
| 516 | |
| 517 | inet = inet_sk(sk); |
| 518 | if (!sock_owned_by_user(sk) && inet->recverr) { |
| 519 | sk->sk_err = err; |
| 520 | sk->sk_error_report(sk); |
| 521 | } else { /* Only an error on timeout */ |
| 522 | sk->sk_err_soft = err; |
| 523 | } |
| 524 | |
| 525 | out_unlock: |
Sridhar Samudrala | d1ad1ff | 2005-07-18 13:44:10 -0700 | [diff] [blame] | 526 | sctp_err_finish(sk, asoc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | /* |
| 530 | * RFC 2960, 8.4 - Handle "Out of the blue" Packets. |
| 531 | * |
| 532 | * This function scans all the chunks in the OOTB packet to determine if |
| 533 | * the packet should be discarded right away. If a response might be needed |
| 534 | * for this packet, or, if further processing is possible, the packet will |
| 535 | * be queued to a proper inqueue for the next phase of handling. |
| 536 | * |
| 537 | * Output: |
| 538 | * Return 0 - If further processing is needed. |
| 539 | * Return 1 - If the packet can be discarded right away. |
| 540 | */ |
| 541 | int sctp_rcv_ootb(struct sk_buff *skb) |
| 542 | { |
| 543 | sctp_chunkhdr_t *ch; |
| 544 | __u8 *ch_end; |
| 545 | sctp_errhdr_t *err; |
| 546 | |
| 547 | ch = (sctp_chunkhdr_t *) skb->data; |
| 548 | ch_end = ((__u8 *) ch) + WORD_ROUND(ntohs(ch->length)); |
| 549 | |
| 550 | /* Scan through all the chunks in the packet. */ |
| 551 | while (ch_end > (__u8 *)ch && ch_end < skb->tail) { |
| 552 | |
| 553 | /* RFC 8.4, 2) If the OOTB packet contains an ABORT chunk, the |
| 554 | * receiver MUST silently discard the OOTB packet and take no |
| 555 | * further action. |
| 556 | */ |
| 557 | if (SCTP_CID_ABORT == ch->type) |
| 558 | goto discard; |
| 559 | |
| 560 | /* RFC 8.4, 6) If the packet contains a SHUTDOWN COMPLETE |
| 561 | * chunk, the receiver should silently discard the packet |
| 562 | * and take no further action. |
| 563 | */ |
| 564 | if (SCTP_CID_SHUTDOWN_COMPLETE == ch->type) |
| 565 | goto discard; |
| 566 | |
| 567 | /* RFC 8.4, 7) If the packet contains a "Stale cookie" ERROR |
| 568 | * or a COOKIE ACK the SCTP Packet should be silently |
| 569 | * discarded. |
| 570 | */ |
| 571 | if (SCTP_CID_COOKIE_ACK == ch->type) |
| 572 | goto discard; |
| 573 | |
| 574 | if (SCTP_CID_ERROR == ch->type) { |
| 575 | sctp_walk_errors(err, ch) { |
| 576 | if (SCTP_ERROR_STALE_COOKIE == err->cause) |
| 577 | goto discard; |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | ch = (sctp_chunkhdr_t *) ch_end; |
| 582 | ch_end = ((__u8 *) ch) + WORD_ROUND(ntohs(ch->length)); |
| 583 | } |
| 584 | |
| 585 | return 0; |
| 586 | |
| 587 | discard: |
| 588 | return 1; |
| 589 | } |
| 590 | |
| 591 | /* Insert endpoint into the hash table. */ |
| 592 | static void __sctp_hash_endpoint(struct sctp_endpoint *ep) |
| 593 | { |
| 594 | struct sctp_ep_common **epp; |
| 595 | struct sctp_ep_common *epb; |
| 596 | struct sctp_hashbucket *head; |
| 597 | |
| 598 | epb = &ep->base; |
| 599 | |
| 600 | epb->hashent = sctp_ep_hashfn(epb->bind_addr.port); |
| 601 | head = &sctp_ep_hashtable[epb->hashent]; |
| 602 | |
| 603 | sctp_write_lock(&head->lock); |
| 604 | epp = &head->chain; |
| 605 | epb->next = *epp; |
| 606 | if (epb->next) |
| 607 | (*epp)->pprev = &epb->next; |
| 608 | *epp = epb; |
| 609 | epb->pprev = epp; |
| 610 | sctp_write_unlock(&head->lock); |
| 611 | } |
| 612 | |
| 613 | /* Add an endpoint to the hash. Local BH-safe. */ |
| 614 | void sctp_hash_endpoint(struct sctp_endpoint *ep) |
| 615 | { |
| 616 | sctp_local_bh_disable(); |
| 617 | __sctp_hash_endpoint(ep); |
| 618 | sctp_local_bh_enable(); |
| 619 | } |
| 620 | |
| 621 | /* Remove endpoint from the hash table. */ |
| 622 | static void __sctp_unhash_endpoint(struct sctp_endpoint *ep) |
| 623 | { |
| 624 | struct sctp_hashbucket *head; |
| 625 | struct sctp_ep_common *epb; |
| 626 | |
| 627 | epb = &ep->base; |
| 628 | |
| 629 | epb->hashent = sctp_ep_hashfn(epb->bind_addr.port); |
| 630 | |
| 631 | head = &sctp_ep_hashtable[epb->hashent]; |
| 632 | |
| 633 | sctp_write_lock(&head->lock); |
| 634 | |
| 635 | if (epb->pprev) { |
| 636 | if (epb->next) |
| 637 | epb->next->pprev = epb->pprev; |
| 638 | *epb->pprev = epb->next; |
| 639 | epb->pprev = NULL; |
| 640 | } |
| 641 | |
| 642 | sctp_write_unlock(&head->lock); |
| 643 | } |
| 644 | |
| 645 | /* Remove endpoint from the hash. Local BH-safe. */ |
| 646 | void sctp_unhash_endpoint(struct sctp_endpoint *ep) |
| 647 | { |
| 648 | sctp_local_bh_disable(); |
| 649 | __sctp_unhash_endpoint(ep); |
| 650 | sctp_local_bh_enable(); |
| 651 | } |
| 652 | |
| 653 | /* Look up an endpoint. */ |
| 654 | static struct sctp_endpoint *__sctp_rcv_lookup_endpoint(const union sctp_addr *laddr) |
| 655 | { |
| 656 | struct sctp_hashbucket *head; |
| 657 | struct sctp_ep_common *epb; |
| 658 | struct sctp_endpoint *ep; |
| 659 | int hash; |
| 660 | |
| 661 | hash = sctp_ep_hashfn(laddr->v4.sin_port); |
| 662 | head = &sctp_ep_hashtable[hash]; |
| 663 | read_lock(&head->lock); |
| 664 | for (epb = head->chain; epb; epb = epb->next) { |
| 665 | ep = sctp_ep(epb); |
| 666 | if (sctp_endpoint_is_match(ep, laddr)) |
| 667 | goto hit; |
| 668 | } |
| 669 | |
| 670 | ep = sctp_sk((sctp_get_ctl_sock()))->ep; |
| 671 | epb = &ep->base; |
| 672 | |
| 673 | hit: |
| 674 | sctp_endpoint_hold(ep); |
| 675 | sock_hold(epb->sk); |
| 676 | read_unlock(&head->lock); |
| 677 | return ep; |
| 678 | } |
| 679 | |
| 680 | /* Insert association into the hash table. */ |
| 681 | static void __sctp_hash_established(struct sctp_association *asoc) |
| 682 | { |
| 683 | struct sctp_ep_common **epp; |
| 684 | struct sctp_ep_common *epb; |
| 685 | struct sctp_hashbucket *head; |
| 686 | |
| 687 | epb = &asoc->base; |
| 688 | |
| 689 | /* Calculate which chain this entry will belong to. */ |
| 690 | epb->hashent = sctp_assoc_hashfn(epb->bind_addr.port, asoc->peer.port); |
| 691 | |
| 692 | head = &sctp_assoc_hashtable[epb->hashent]; |
| 693 | |
| 694 | sctp_write_lock(&head->lock); |
| 695 | epp = &head->chain; |
| 696 | epb->next = *epp; |
| 697 | if (epb->next) |
| 698 | (*epp)->pprev = &epb->next; |
| 699 | *epp = epb; |
| 700 | epb->pprev = epp; |
| 701 | sctp_write_unlock(&head->lock); |
| 702 | } |
| 703 | |
| 704 | /* Add an association to the hash. Local BH-safe. */ |
| 705 | void sctp_hash_established(struct sctp_association *asoc) |
| 706 | { |
| 707 | sctp_local_bh_disable(); |
| 708 | __sctp_hash_established(asoc); |
| 709 | sctp_local_bh_enable(); |
| 710 | } |
| 711 | |
| 712 | /* Remove association from the hash table. */ |
| 713 | static void __sctp_unhash_established(struct sctp_association *asoc) |
| 714 | { |
| 715 | struct sctp_hashbucket *head; |
| 716 | struct sctp_ep_common *epb; |
| 717 | |
| 718 | epb = &asoc->base; |
| 719 | |
| 720 | epb->hashent = sctp_assoc_hashfn(epb->bind_addr.port, |
| 721 | asoc->peer.port); |
| 722 | |
| 723 | head = &sctp_assoc_hashtable[epb->hashent]; |
| 724 | |
| 725 | sctp_write_lock(&head->lock); |
| 726 | |
| 727 | if (epb->pprev) { |
| 728 | if (epb->next) |
| 729 | epb->next->pprev = epb->pprev; |
| 730 | *epb->pprev = epb->next; |
| 731 | epb->pprev = NULL; |
| 732 | } |
| 733 | |
| 734 | sctp_write_unlock(&head->lock); |
| 735 | } |
| 736 | |
| 737 | /* Remove association from the hash table. Local BH-safe. */ |
| 738 | void sctp_unhash_established(struct sctp_association *asoc) |
| 739 | { |
| 740 | sctp_local_bh_disable(); |
| 741 | __sctp_unhash_established(asoc); |
| 742 | sctp_local_bh_enable(); |
| 743 | } |
| 744 | |
| 745 | /* Look up an association. */ |
| 746 | static struct sctp_association *__sctp_lookup_association( |
| 747 | const union sctp_addr *local, |
| 748 | const union sctp_addr *peer, |
| 749 | struct sctp_transport **pt) |
| 750 | { |
| 751 | struct sctp_hashbucket *head; |
| 752 | struct sctp_ep_common *epb; |
| 753 | struct sctp_association *asoc; |
| 754 | struct sctp_transport *transport; |
| 755 | int hash; |
| 756 | |
| 757 | /* Optimize here for direct hit, only listening connections can |
| 758 | * have wildcards anyways. |
| 759 | */ |
| 760 | hash = sctp_assoc_hashfn(local->v4.sin_port, peer->v4.sin_port); |
| 761 | head = &sctp_assoc_hashtable[hash]; |
| 762 | read_lock(&head->lock); |
| 763 | for (epb = head->chain; epb; epb = epb->next) { |
| 764 | asoc = sctp_assoc(epb); |
| 765 | transport = sctp_assoc_is_match(asoc, local, peer); |
| 766 | if (transport) |
| 767 | goto hit; |
| 768 | } |
| 769 | |
| 770 | read_unlock(&head->lock); |
| 771 | |
| 772 | return NULL; |
| 773 | |
| 774 | hit: |
| 775 | *pt = transport; |
| 776 | sctp_association_hold(asoc); |
| 777 | sock_hold(epb->sk); |
| 778 | read_unlock(&head->lock); |
| 779 | return asoc; |
| 780 | } |
| 781 | |
| 782 | /* Look up an association. BH-safe. */ |
| 783 | SCTP_STATIC |
| 784 | struct sctp_association *sctp_lookup_association(const union sctp_addr *laddr, |
| 785 | const union sctp_addr *paddr, |
| 786 | struct sctp_transport **transportp) |
| 787 | { |
| 788 | struct sctp_association *asoc; |
| 789 | |
| 790 | sctp_local_bh_disable(); |
| 791 | asoc = __sctp_lookup_association(laddr, paddr, transportp); |
| 792 | sctp_local_bh_enable(); |
| 793 | |
| 794 | return asoc; |
| 795 | } |
| 796 | |
| 797 | /* Is there an association matching the given local and peer addresses? */ |
| 798 | int sctp_has_association(const union sctp_addr *laddr, |
| 799 | const union sctp_addr *paddr) |
| 800 | { |
| 801 | struct sctp_association *asoc; |
| 802 | struct sctp_transport *transport; |
| 803 | |
| 804 | if ((asoc = sctp_lookup_association(laddr, paddr, &transport))) { |
| 805 | sock_put(asoc->base.sk); |
| 806 | sctp_association_put(asoc); |
| 807 | return 1; |
| 808 | } |
| 809 | |
| 810 | return 0; |
| 811 | } |
| 812 | |
| 813 | /* |
| 814 | * SCTP Implementors Guide, 2.18 Handling of address |
| 815 | * parameters within the INIT or INIT-ACK. |
| 816 | * |
| 817 | * D) When searching for a matching TCB upon reception of an INIT |
| 818 | * or INIT-ACK chunk the receiver SHOULD use not only the |
| 819 | * source address of the packet (containing the INIT or |
| 820 | * INIT-ACK) but the receiver SHOULD also use all valid |
| 821 | * address parameters contained within the chunk. |
| 822 | * |
| 823 | * 2.18.3 Solution description |
| 824 | * |
| 825 | * This new text clearly specifies to an implementor the need |
| 826 | * to look within the INIT or INIT-ACK. Any implementation that |
| 827 | * does not do this, may not be able to establish associations |
| 828 | * in certain circumstances. |
| 829 | * |
| 830 | */ |
| 831 | static struct sctp_association *__sctp_rcv_init_lookup(struct sk_buff *skb, |
| 832 | const union sctp_addr *laddr, struct sctp_transport **transportp) |
| 833 | { |
| 834 | struct sctp_association *asoc; |
| 835 | union sctp_addr addr; |
| 836 | union sctp_addr *paddr = &addr; |
| 837 | struct sctphdr *sh = (struct sctphdr *) skb->h.raw; |
| 838 | sctp_chunkhdr_t *ch; |
| 839 | union sctp_params params; |
| 840 | sctp_init_chunk_t *init; |
| 841 | struct sctp_transport *transport; |
| 842 | struct sctp_af *af; |
| 843 | |
| 844 | ch = (sctp_chunkhdr_t *) skb->data; |
| 845 | |
| 846 | /* If this is INIT/INIT-ACK look inside the chunk too. */ |
| 847 | switch (ch->type) { |
| 848 | case SCTP_CID_INIT: |
| 849 | case SCTP_CID_INIT_ACK: |
| 850 | break; |
| 851 | default: |
| 852 | return NULL; |
| 853 | } |
| 854 | |
| 855 | /* The code below will attempt to walk the chunk and extract |
| 856 | * parameter information. Before we do that, we need to verify |
| 857 | * that the chunk length doesn't cause overflow. Otherwise, we'll |
| 858 | * walk off the end. |
| 859 | */ |
| 860 | if (WORD_ROUND(ntohs(ch->length)) > skb->len) |
| 861 | return NULL; |
| 862 | |
| 863 | /* |
| 864 | * This code will NOT touch anything inside the chunk--it is |
| 865 | * strictly READ-ONLY. |
| 866 | * |
| 867 | * RFC 2960 3 SCTP packet Format |
| 868 | * |
| 869 | * Multiple chunks can be bundled into one SCTP packet up to |
| 870 | * the MTU size, except for the INIT, INIT ACK, and SHUTDOWN |
| 871 | * COMPLETE chunks. These chunks MUST NOT be bundled with any |
| 872 | * other chunk in a packet. See Section 6.10 for more details |
| 873 | * on chunk bundling. |
| 874 | */ |
| 875 | |
| 876 | /* Find the start of the TLVs and the end of the chunk. This is |
| 877 | * the region we search for address parameters. |
| 878 | */ |
| 879 | init = (sctp_init_chunk_t *)skb->data; |
| 880 | |
| 881 | /* Walk the parameters looking for embedded addresses. */ |
| 882 | sctp_walk_params(params, init, init_hdr.params) { |
| 883 | |
| 884 | /* Note: Ignoring hostname addresses. */ |
| 885 | af = sctp_get_af_specific(param_type2af(params.p->type)); |
| 886 | if (!af) |
| 887 | continue; |
| 888 | |
| 889 | af->from_addr_param(paddr, params.addr, ntohs(sh->source), 0); |
| 890 | |
| 891 | asoc = __sctp_lookup_association(laddr, paddr, &transport); |
| 892 | if (asoc) |
| 893 | return asoc; |
| 894 | } |
| 895 | |
| 896 | return NULL; |
| 897 | } |
| 898 | |
| 899 | /* Lookup an association for an inbound skb. */ |
| 900 | static struct sctp_association *__sctp_rcv_lookup(struct sk_buff *skb, |
| 901 | const union sctp_addr *paddr, |
| 902 | const union sctp_addr *laddr, |
| 903 | struct sctp_transport **transportp) |
| 904 | { |
| 905 | struct sctp_association *asoc; |
| 906 | |
| 907 | asoc = __sctp_lookup_association(laddr, paddr, transportp); |
| 908 | |
| 909 | /* Further lookup for INIT/INIT-ACK packets. |
| 910 | * SCTP Implementors Guide, 2.18 Handling of address |
| 911 | * parameters within the INIT or INIT-ACK. |
| 912 | */ |
| 913 | if (!asoc) |
| 914 | asoc = __sctp_rcv_init_lookup(skb, laddr, transportp); |
| 915 | |
| 916 | return asoc; |
| 917 | } |