input.c 25 KB

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