input.c 24 KB

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