input.c 26 KB

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