input.c 25 KB

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