input.c 23 KB

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