input.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  1. /* SCTP kernel 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 implementation
  10. *
  11. * These functions handle all input from the IP layer into SCTP.
  12. *
  13. * This SCTP 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. * This SCTP 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 <linux/slab.h>
  56. #include <net/ip.h>
  57. #include <net/icmp.h>
  58. #include <net/snmp.h>
  59. #include <net/sock.h>
  60. #include <net/xfrm.h>
  61. #include <net/sctp/sctp.h>
  62. #include <net/sctp/sm.h>
  63. #include <net/sctp/checksum.h>
  64. #include <net/net_namespace.h>
  65. /* Forward declarations for internal helpers. */
  66. static int sctp_rcv_ootb(struct sk_buff *);
  67. static struct sctp_association *__sctp_rcv_lookup(struct sk_buff *skb,
  68. const union sctp_addr *laddr,
  69. const union sctp_addr *paddr,
  70. struct sctp_transport **transportp);
  71. static struct sctp_endpoint *__sctp_rcv_lookup_endpoint(const union sctp_addr *laddr);
  72. static struct sctp_association *__sctp_lookup_association(
  73. const union sctp_addr *local,
  74. const union sctp_addr *peer,
  75. struct sctp_transport **pt);
  76. static int sctp_add_backlog(struct sock *sk, struct sk_buff *skb);
  77. /* Calculate the SCTP checksum of an SCTP packet. */
  78. static inline int sctp_rcv_checksum(struct sk_buff *skb)
  79. {
  80. struct sctphdr *sh = sctp_hdr(skb);
  81. __le32 cmp = sh->checksum;
  82. struct sk_buff *list;
  83. __le32 val;
  84. __u32 tmp = sctp_start_cksum((__u8 *)sh, skb_headlen(skb));
  85. skb_walk_frags(skb, list)
  86. tmp = sctp_update_cksum((__u8 *)list->data, skb_headlen(list),
  87. tmp);
  88. val = sctp_end_cksum(tmp);
  89. if (val != cmp) {
  90. /* CRC failure, dump it. */
  91. SCTP_INC_STATS_BH(SCTP_MIB_CHECKSUMERRORS);
  92. return -1;
  93. }
  94. return 0;
  95. }
  96. struct sctp_input_cb {
  97. union {
  98. struct inet_skb_parm h4;
  99. #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
  100. struct inet6_skb_parm h6;
  101. #endif
  102. } header;
  103. struct sctp_chunk *chunk;
  104. };
  105. #define SCTP_INPUT_CB(__skb) ((struct sctp_input_cb *)&((__skb)->cb[0]))
  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. if (skb->pkt_type!=PACKET_HOST)
  123. goto discard_it;
  124. SCTP_INC_STATS_BH(SCTP_MIB_INSCTPPACKS);
  125. if (skb_linearize(skb))
  126. goto discard_it;
  127. sh = sctp_hdr(skb);
  128. /* Pull up the IP and SCTP headers. */
  129. __skb_pull(skb, skb_transport_offset(skb));
  130. if (skb->len < sizeof(struct sctphdr))
  131. goto discard_it;
  132. if (!sctp_checksum_disable && !skb_csum_unnecessary(skb) &&
  133. sctp_rcv_checksum(skb) < 0)
  134. goto discard_it;
  135. skb_pull(skb, sizeof(struct sctphdr));
  136. /* Make sure we at least have chunk headers worth of data left. */
  137. if (skb->len < sizeof(struct sctp_chunkhdr))
  138. goto discard_it;
  139. family = ipver2af(ip_hdr(skb)->version);
  140. af = sctp_get_af_specific(family);
  141. if (unlikely(!af))
  142. goto discard_it;
  143. /* Initialize local addresses for lookups. */
  144. af->from_skb(&src, skb, 1);
  145. af->from_skb(&dest, skb, 0);
  146. /* If the packet is to or from a non-unicast address,
  147. * silently discard the packet.
  148. *
  149. * This is not clearly defined in the RFC except in section
  150. * 8.4 - OOTB handling. However, based on the book "Stream Control
  151. * Transmission Protocol" 2.1, "It is important to note that the
  152. * IP address of an SCTP transport address must be a routable
  153. * unicast address. In other words, IP multicast addresses and
  154. * IP broadcast addresses cannot be used in an SCTP transport
  155. * address."
  156. */
  157. if (!af->addr_valid(&src, NULL, skb) ||
  158. !af->addr_valid(&dest, NULL, skb))
  159. goto discard_it;
  160. asoc = __sctp_rcv_lookup(skb, &src, &dest, &transport);
  161. if (!asoc)
  162. ep = __sctp_rcv_lookup_endpoint(&dest);
  163. /* Retrieve the common input handling substructure. */
  164. rcvr = asoc ? &asoc->base : &ep->base;
  165. sk = rcvr->sk;
  166. /*
  167. * If a frame arrives on an interface and the receiving socket is
  168. * bound to another interface, via SO_BINDTODEVICE, treat it as OOTB
  169. */
  170. if (sk->sk_bound_dev_if && (sk->sk_bound_dev_if != af->skb_iif(skb)))
  171. {
  172. if (asoc) {
  173. sctp_association_put(asoc);
  174. asoc = NULL;
  175. } else {
  176. sctp_endpoint_put(ep);
  177. ep = NULL;
  178. }
  179. sk = sctp_get_ctl_sock();
  180. ep = sctp_sk(sk)->ep;
  181. sctp_endpoint_hold(ep);
  182. rcvr = &ep->base;
  183. }
  184. /*
  185. * RFC 2960, 8.4 - Handle "Out of the blue" Packets.
  186. * An SCTP packet is called an "out of the blue" (OOTB)
  187. * packet if it is correctly formed, i.e., passed the
  188. * receiver's checksum check, but the receiver is not
  189. * able to identify the association to which this
  190. * packet belongs.
  191. */
  192. if (!asoc) {
  193. if (sctp_rcv_ootb(skb)) {
  194. SCTP_INC_STATS_BH(SCTP_MIB_OUTOFBLUES);
  195. goto discard_release;
  196. }
  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))
  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. if (sk != rcvr->sk) {
  222. /* Our cached sk is different from the rcvr->sk. This is
  223. * because migrate()/accept() may have moved the association
  224. * to a new socket and released all the sockets. So now we
  225. * are holding a lock on the old socket while the user may
  226. * be doing something with the new socket. Switch our veiw
  227. * of the current sk.
  228. */
  229. sctp_bh_unlock_sock(sk);
  230. sk = rcvr->sk;
  231. sctp_bh_lock_sock(sk);
  232. }
  233. if (sock_owned_by_user(sk)) {
  234. if (sctp_add_backlog(sk, skb)) {
  235. sctp_bh_unlock_sock(sk);
  236. sctp_chunk_free(chunk);
  237. skb = NULL; /* sctp_chunk_free already freed the skb */
  238. goto discard_release;
  239. }
  240. SCTP_INC_STATS_BH(SCTP_MIB_IN_PKT_BACKLOG);
  241. } else {
  242. SCTP_INC_STATS_BH(SCTP_MIB_IN_PKT_SOFTIRQ);
  243. sctp_inq_push(&chunk->rcvr->inqueue, chunk);
  244. }
  245. sctp_bh_unlock_sock(sk);
  246. /* Release the asoc/ep ref we took in the lookup calls. */
  247. if (asoc)
  248. sctp_association_put(asoc);
  249. else
  250. sctp_endpoint_put(ep);
  251. return 0;
  252. discard_it:
  253. SCTP_INC_STATS_BH(SCTP_MIB_IN_PKT_DISCARDS);
  254. kfree_skb(skb);
  255. return 0;
  256. discard_release:
  257. /* Release the asoc/ep ref we took in the lookup calls. */
  258. if (asoc)
  259. sctp_association_put(asoc);
  260. else
  261. sctp_endpoint_put(ep);
  262. goto discard_it;
  263. }
  264. /* Process the backlog queue of the socket. Every skb on
  265. * the backlog holds a ref on an association or endpoint.
  266. * We hold this ref throughout the state machine to make
  267. * sure that the structure we need is still around.
  268. */
  269. int sctp_backlog_rcv(struct sock *sk, struct sk_buff *skb)
  270. {
  271. struct sctp_chunk *chunk = SCTP_INPUT_CB(skb)->chunk;
  272. struct sctp_inq *inqueue = &chunk->rcvr->inqueue;
  273. struct sctp_ep_common *rcvr = NULL;
  274. int backloged = 0;
  275. rcvr = chunk->rcvr;
  276. /* If the rcvr is dead then the association or endpoint
  277. * has been deleted and we can safely drop the chunk
  278. * and refs that we are holding.
  279. */
  280. if (rcvr->dead) {
  281. sctp_chunk_free(chunk);
  282. goto done;
  283. }
  284. if (unlikely(rcvr->sk != sk)) {
  285. /* In this case, the association moved from one socket to
  286. * another. We are currently sitting on the backlog of the
  287. * old socket, so we need to move.
  288. * However, since we are here in the process context we
  289. * need to take make sure that the user doesn't own
  290. * the new socket when we process the packet.
  291. * If the new socket is user-owned, queue the chunk to the
  292. * backlog of the new socket without dropping any refs.
  293. * Otherwise, we can safely push the chunk on the inqueue.
  294. */
  295. sk = rcvr->sk;
  296. sctp_bh_lock_sock(sk);
  297. if (sock_owned_by_user(sk)) {
  298. if (sk_add_backlog(sk, skb))
  299. sctp_chunk_free(chunk);
  300. else
  301. backloged = 1;
  302. } else
  303. sctp_inq_push(inqueue, chunk);
  304. sctp_bh_unlock_sock(sk);
  305. /* If the chunk was backloged again, don't drop refs */
  306. if (backloged)
  307. return 0;
  308. } else {
  309. sctp_inq_push(inqueue, chunk);
  310. }
  311. done:
  312. /* Release the refs we took in sctp_add_backlog */
  313. if (SCTP_EP_TYPE_ASSOCIATION == rcvr->type)
  314. sctp_association_put(sctp_assoc(rcvr));
  315. else if (SCTP_EP_TYPE_SOCKET == rcvr->type)
  316. sctp_endpoint_put(sctp_ep(rcvr));
  317. else
  318. BUG();
  319. return 0;
  320. }
  321. static int sctp_add_backlog(struct sock *sk, struct sk_buff *skb)
  322. {
  323. struct sctp_chunk *chunk = SCTP_INPUT_CB(skb)->chunk;
  324. struct sctp_ep_common *rcvr = chunk->rcvr;
  325. int ret;
  326. ret = sk_add_backlog(sk, skb);
  327. if (!ret) {
  328. /* Hold the assoc/ep while hanging on the backlog queue.
  329. * This way, we know structures we need will not disappear
  330. * from us
  331. */
  332. if (SCTP_EP_TYPE_ASSOCIATION == rcvr->type)
  333. sctp_association_hold(sctp_assoc(rcvr));
  334. else if (SCTP_EP_TYPE_SOCKET == rcvr->type)
  335. sctp_endpoint_hold(sctp_ep(rcvr));
  336. else
  337. BUG();
  338. }
  339. return ret;
  340. }
  341. /* Handle icmp frag needed error. */
  342. void sctp_icmp_frag_needed(struct sock *sk, struct sctp_association *asoc,
  343. struct sctp_transport *t, __u32 pmtu)
  344. {
  345. if (!t || (t->pathmtu <= pmtu))
  346. return;
  347. if (sock_owned_by_user(sk)) {
  348. asoc->pmtu_pending = 1;
  349. t->pmtu_pending = 1;
  350. return;
  351. }
  352. if (t->param_flags & SPP_PMTUD_ENABLE) {
  353. /* Update transports view of the MTU */
  354. sctp_transport_update_pmtu(t, pmtu);
  355. /* Update association pmtu. */
  356. sctp_assoc_sync_pmtu(asoc);
  357. }
  358. /* Retransmit with the new pmtu setting.
  359. * Normally, if PMTU discovery is disabled, an ICMP Fragmentation
  360. * Needed will never be sent, but if a message was sent before
  361. * PMTU discovery was disabled that was larger than the PMTU, it
  362. * would not be fragmented, so it must be re-transmitted fragmented.
  363. */
  364. sctp_retransmit(&asoc->outqueue, t, SCTP_RTXR_PMTUD);
  365. }
  366. /*
  367. * SCTP Implementer's Guide, 2.37 ICMP handling procedures
  368. *
  369. * ICMP8) If the ICMP code is a "Unrecognized next header type encountered"
  370. * or a "Protocol Unreachable" treat this message as an abort
  371. * with the T bit set.
  372. *
  373. * This function sends an event to the state machine, which will abort the
  374. * association.
  375. *
  376. */
  377. void sctp_icmp_proto_unreachable(struct sock *sk,
  378. struct sctp_association *asoc,
  379. struct sctp_transport *t)
  380. {
  381. SCTP_DEBUG_PRINTK("%s\n", __func__);
  382. sctp_do_sm(SCTP_EVENT_T_OTHER,
  383. SCTP_ST_OTHER(SCTP_EVENT_ICMP_PROTO_UNREACH),
  384. asoc->state, asoc->ep, asoc, t,
  385. GFP_ATOMIC);
  386. }
  387. /* Common lookup code for icmp/icmpv6 error handler. */
  388. struct sock *sctp_err_lookup(int family, struct sk_buff *skb,
  389. struct sctphdr *sctphdr,
  390. struct sctp_association **app,
  391. struct sctp_transport **tpp)
  392. {
  393. union sctp_addr saddr;
  394. union sctp_addr daddr;
  395. struct sctp_af *af;
  396. struct sock *sk = NULL;
  397. struct sctp_association *asoc;
  398. struct sctp_transport *transport = NULL;
  399. struct sctp_init_chunk *chunkhdr;
  400. __u32 vtag = ntohl(sctphdr->vtag);
  401. int len = skb->len - ((void *)sctphdr - (void *)skb->data);
  402. *app = NULL; *tpp = NULL;
  403. af = sctp_get_af_specific(family);
  404. if (unlikely(!af)) {
  405. return NULL;
  406. }
  407. /* Initialize local addresses for lookups. */
  408. af->from_skb(&saddr, skb, 1);
  409. af->from_skb(&daddr, skb, 0);
  410. /* Look for an association that matches the incoming ICMP error
  411. * packet.
  412. */
  413. asoc = __sctp_lookup_association(&saddr, &daddr, &transport);
  414. if (!asoc)
  415. return NULL;
  416. sk = asoc->base.sk;
  417. /* RFC 4960, Appendix C. ICMP Handling
  418. *
  419. * ICMP6) An implementation MUST validate that the Verification Tag
  420. * contained in the ICMP message matches the Verification Tag of
  421. * the peer. If the Verification Tag is not 0 and does NOT
  422. * match, discard the ICMP message. If it is 0 and the ICMP
  423. * message contains enough bytes to verify that the chunk type is
  424. * an INIT chunk and that the Initiate Tag matches the tag of the
  425. * peer, continue with ICMP7. If the ICMP message is too short
  426. * or the chunk type or the Initiate Tag does not match, silently
  427. * discard the packet.
  428. */
  429. if (vtag == 0) {
  430. chunkhdr = (struct sctp_init_chunk *)((void *)sctphdr
  431. + sizeof(struct sctphdr));
  432. if (len < sizeof(struct sctphdr) + sizeof(sctp_chunkhdr_t)
  433. + sizeof(__be32) ||
  434. chunkhdr->chunk_hdr.type != SCTP_CID_INIT ||
  435. ntohl(chunkhdr->init_hdr.init_tag) != asoc->c.my_vtag) {
  436. goto out;
  437. }
  438. } else if (vtag != asoc->c.peer_vtag) {
  439. goto out;
  440. }
  441. sctp_bh_lock_sock(sk);
  442. /* If too many ICMPs get dropped on busy
  443. * servers this needs to be solved differently.
  444. */
  445. if (sock_owned_by_user(sk))
  446. NET_INC_STATS_BH(&init_net, LINUX_MIB_LOCKDROPPEDICMPS);
  447. *app = asoc;
  448. *tpp = transport;
  449. return sk;
  450. out:
  451. if (asoc)
  452. sctp_association_put(asoc);
  453. return NULL;
  454. }
  455. /* Common cleanup code for icmp/icmpv6 error handler. */
  456. void sctp_err_finish(struct sock *sk, struct sctp_association *asoc)
  457. {
  458. sctp_bh_unlock_sock(sk);
  459. if (asoc)
  460. sctp_association_put(asoc);
  461. }
  462. /*
  463. * This routine is called by the ICMP module when it gets some
  464. * sort of error condition. If err < 0 then the socket should
  465. * be closed and the error returned to the user. If err > 0
  466. * it's just the icmp type << 8 | icmp code. After adjustment
  467. * header points to the first 8 bytes of the sctp header. We need
  468. * to find the appropriate port.
  469. *
  470. * The locking strategy used here is very "optimistic". When
  471. * someone else accesses the socket the ICMP is just dropped
  472. * and for some paths there is no check at all.
  473. * A more general error queue to queue errors for later handling
  474. * is probably better.
  475. *
  476. */
  477. void sctp_v4_err(struct sk_buff *skb, __u32 info)
  478. {
  479. struct iphdr *iph = (struct iphdr *)skb->data;
  480. const int ihlen = iph->ihl * 4;
  481. const int type = icmp_hdr(skb)->type;
  482. const int code = icmp_hdr(skb)->code;
  483. struct sock *sk;
  484. struct sctp_association *asoc = NULL;
  485. struct sctp_transport *transport;
  486. struct inet_sock *inet;
  487. sk_buff_data_t saveip, savesctp;
  488. int err;
  489. if (skb->len < ihlen + 8) {
  490. ICMP_INC_STATS_BH(&init_net, ICMP_MIB_INERRORS);
  491. return;
  492. }
  493. /* Fix up skb to look at the embedded net header. */
  494. saveip = skb->network_header;
  495. savesctp = skb->transport_header;
  496. skb_reset_network_header(skb);
  497. skb_set_transport_header(skb, ihlen);
  498. sk = sctp_err_lookup(AF_INET, skb, sctp_hdr(skb), &asoc, &transport);
  499. /* Put back, the original values. */
  500. skb->network_header = saveip;
  501. skb->transport_header = savesctp;
  502. if (!sk) {
  503. ICMP_INC_STATS_BH(&init_net, ICMP_MIB_INERRORS);
  504. return;
  505. }
  506. /* Warning: The sock lock is held. Remember to call
  507. * sctp_err_finish!
  508. */
  509. switch (type) {
  510. case ICMP_PARAMETERPROB:
  511. err = EPROTO;
  512. break;
  513. case ICMP_DEST_UNREACH:
  514. if (code > NR_ICMP_UNREACH)
  515. goto out_unlock;
  516. /* PMTU discovery (RFC1191) */
  517. if (ICMP_FRAG_NEEDED == code) {
  518. sctp_icmp_frag_needed(sk, asoc, transport, info);
  519. goto out_unlock;
  520. }
  521. else {
  522. if (ICMP_PROT_UNREACH == code) {
  523. sctp_icmp_proto_unreachable(sk, asoc,
  524. transport);
  525. goto out_unlock;
  526. }
  527. }
  528. err = icmp_err_convert[code].errno;
  529. break;
  530. case ICMP_TIME_EXCEEDED:
  531. /* Ignore any time exceeded errors due to fragment reassembly
  532. * timeouts.
  533. */
  534. if (ICMP_EXC_FRAGTIME == code)
  535. goto out_unlock;
  536. err = EHOSTUNREACH;
  537. break;
  538. default:
  539. goto out_unlock;
  540. }
  541. inet = inet_sk(sk);
  542. if (!sock_owned_by_user(sk) && inet->recverr) {
  543. sk->sk_err = err;
  544. sk->sk_error_report(sk);
  545. } else { /* Only an error on timeout */
  546. sk->sk_err_soft = err;
  547. }
  548. out_unlock:
  549. sctp_err_finish(sk, asoc);
  550. }
  551. /*
  552. * RFC 2960, 8.4 - Handle "Out of the blue" Packets.
  553. *
  554. * This function scans all the chunks in the OOTB packet to determine if
  555. * the packet should be discarded right away. If a response might be needed
  556. * for this packet, or, if further processing is possible, the packet will
  557. * be queued to a proper inqueue for the next phase of handling.
  558. *
  559. * Output:
  560. * Return 0 - If further processing is needed.
  561. * Return 1 - If the packet can be discarded right away.
  562. */
  563. static int sctp_rcv_ootb(struct sk_buff *skb)
  564. {
  565. sctp_chunkhdr_t *ch;
  566. __u8 *ch_end;
  567. sctp_errhdr_t *err;
  568. ch = (sctp_chunkhdr_t *) skb->data;
  569. /* Scan through all the chunks in the packet. */
  570. do {
  571. /* Break out if chunk length is less then minimal. */
  572. if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
  573. break;
  574. ch_end = ((__u8 *)ch) + WORD_ROUND(ntohs(ch->length));
  575. if (ch_end > skb_tail_pointer(skb))
  576. break;
  577. /* RFC 8.4, 2) If the OOTB packet contains an ABORT chunk, the
  578. * receiver MUST silently discard the OOTB packet and take no
  579. * further action.
  580. */
  581. if (SCTP_CID_ABORT == ch->type)
  582. goto discard;
  583. /* RFC 8.4, 6) If the packet contains a SHUTDOWN COMPLETE
  584. * chunk, the receiver should silently discard the packet
  585. * and take no further action.
  586. */
  587. if (SCTP_CID_SHUTDOWN_COMPLETE == ch->type)
  588. goto discard;
  589. /* RFC 4460, 2.11.2
  590. * This will discard packets with INIT chunk bundled as
  591. * subsequent chunks in the packet. When INIT is first,
  592. * the normal INIT processing will discard the chunk.
  593. */
  594. if (SCTP_CID_INIT == ch->type && (void *)ch != skb->data)
  595. goto discard;
  596. /* RFC 8.4, 7) If the packet contains a "Stale cookie" ERROR
  597. * or a COOKIE ACK the SCTP Packet should be silently
  598. * discarded.
  599. */
  600. if (SCTP_CID_COOKIE_ACK == ch->type)
  601. goto discard;
  602. if (SCTP_CID_ERROR == ch->type) {
  603. sctp_walk_errors(err, ch) {
  604. if (SCTP_ERROR_STALE_COOKIE == err->cause)
  605. goto discard;
  606. }
  607. }
  608. ch = (sctp_chunkhdr_t *) ch_end;
  609. } while (ch_end < skb_tail_pointer(skb));
  610. return 0;
  611. discard:
  612. return 1;
  613. }
  614. /* Insert endpoint into the hash table. */
  615. static void __sctp_hash_endpoint(struct sctp_endpoint *ep)
  616. {
  617. struct sctp_ep_common *epb;
  618. struct sctp_hashbucket *head;
  619. epb = &ep->base;
  620. epb->hashent = sctp_ep_hashfn(epb->bind_addr.port);
  621. head = &sctp_ep_hashtable[epb->hashent];
  622. sctp_write_lock(&head->lock);
  623. hlist_add_head(&epb->node, &head->chain);
  624. sctp_write_unlock(&head->lock);
  625. }
  626. /* Add an endpoint to the hash. Local BH-safe. */
  627. void sctp_hash_endpoint(struct sctp_endpoint *ep)
  628. {
  629. sctp_local_bh_disable();
  630. __sctp_hash_endpoint(ep);
  631. sctp_local_bh_enable();
  632. }
  633. /* Remove endpoint from the hash table. */
  634. static void __sctp_unhash_endpoint(struct sctp_endpoint *ep)
  635. {
  636. struct sctp_hashbucket *head;
  637. struct sctp_ep_common *epb;
  638. epb = &ep->base;
  639. if (hlist_unhashed(&epb->node))
  640. return;
  641. epb->hashent = sctp_ep_hashfn(epb->bind_addr.port);
  642. head = &sctp_ep_hashtable[epb->hashent];
  643. sctp_write_lock(&head->lock);
  644. __hlist_del(&epb->node);
  645. sctp_write_unlock(&head->lock);
  646. }
  647. /* Remove endpoint from the hash. Local BH-safe. */
  648. void sctp_unhash_endpoint(struct sctp_endpoint *ep)
  649. {
  650. sctp_local_bh_disable();
  651. __sctp_unhash_endpoint(ep);
  652. sctp_local_bh_enable();
  653. }
  654. /* Look up an endpoint. */
  655. static struct sctp_endpoint *__sctp_rcv_lookup_endpoint(const union sctp_addr *laddr)
  656. {
  657. struct sctp_hashbucket *head;
  658. struct sctp_ep_common *epb;
  659. struct sctp_endpoint *ep;
  660. struct hlist_node *node;
  661. int hash;
  662. hash = sctp_ep_hashfn(ntohs(laddr->v4.sin_port));
  663. head = &sctp_ep_hashtable[hash];
  664. read_lock(&head->lock);
  665. sctp_for_each_hentry(epb, node, &head->chain) {
  666. ep = sctp_ep(epb);
  667. if (sctp_endpoint_is_match(ep, laddr))
  668. goto hit;
  669. }
  670. ep = sctp_sk((sctp_get_ctl_sock()))->ep;
  671. hit:
  672. sctp_endpoint_hold(ep);
  673. read_unlock(&head->lock);
  674. return ep;
  675. }
  676. /* Insert association into the hash table. */
  677. static void __sctp_hash_established(struct sctp_association *asoc)
  678. {
  679. struct sctp_ep_common *epb;
  680. struct sctp_hashbucket *head;
  681. epb = &asoc->base;
  682. /* Calculate which chain this entry will belong to. */
  683. epb->hashent = sctp_assoc_hashfn(epb->bind_addr.port, asoc->peer.port);
  684. head = &sctp_assoc_hashtable[epb->hashent];
  685. sctp_write_lock(&head->lock);
  686. hlist_add_head(&epb->node, &head->chain);
  687. sctp_write_unlock(&head->lock);
  688. }
  689. /* Add an association to the hash. Local BH-safe. */
  690. void sctp_hash_established(struct sctp_association *asoc)
  691. {
  692. if (asoc->temp)
  693. return;
  694. sctp_local_bh_disable();
  695. __sctp_hash_established(asoc);
  696. sctp_local_bh_enable();
  697. }
  698. /* Remove association from the hash table. */
  699. static void __sctp_unhash_established(struct sctp_association *asoc)
  700. {
  701. struct sctp_hashbucket *head;
  702. struct sctp_ep_common *epb;
  703. epb = &asoc->base;
  704. epb->hashent = sctp_assoc_hashfn(epb->bind_addr.port,
  705. asoc->peer.port);
  706. head = &sctp_assoc_hashtable[epb->hashent];
  707. sctp_write_lock(&head->lock);
  708. __hlist_del(&epb->node);
  709. sctp_write_unlock(&head->lock);
  710. }
  711. /* Remove association from the hash table. Local BH-safe. */
  712. void sctp_unhash_established(struct sctp_association *asoc)
  713. {
  714. if (asoc->temp)
  715. return;
  716. sctp_local_bh_disable();
  717. __sctp_unhash_established(asoc);
  718. sctp_local_bh_enable();
  719. }
  720. /* Look up an association. */
  721. static struct sctp_association *__sctp_lookup_association(
  722. const union sctp_addr *local,
  723. const union sctp_addr *peer,
  724. struct sctp_transport **pt)
  725. {
  726. struct sctp_hashbucket *head;
  727. struct sctp_ep_common *epb;
  728. struct sctp_association *asoc;
  729. struct sctp_transport *transport;
  730. struct hlist_node *node;
  731. int hash;
  732. /* Optimize here for direct hit, only listening connections can
  733. * have wildcards anyways.
  734. */
  735. hash = sctp_assoc_hashfn(ntohs(local->v4.sin_port), ntohs(peer->v4.sin_port));
  736. head = &sctp_assoc_hashtable[hash];
  737. read_lock(&head->lock);
  738. sctp_for_each_hentry(epb, node, &head->chain) {
  739. asoc = sctp_assoc(epb);
  740. transport = sctp_assoc_is_match(asoc, local, peer);
  741. if (transport)
  742. goto hit;
  743. }
  744. read_unlock(&head->lock);
  745. return NULL;
  746. hit:
  747. *pt = transport;
  748. sctp_association_hold(asoc);
  749. read_unlock(&head->lock);
  750. return asoc;
  751. }
  752. /* Look up an association. BH-safe. */
  753. SCTP_STATIC
  754. struct sctp_association *sctp_lookup_association(const union sctp_addr *laddr,
  755. const union sctp_addr *paddr,
  756. struct sctp_transport **transportp)
  757. {
  758. struct sctp_association *asoc;
  759. sctp_local_bh_disable();
  760. asoc = __sctp_lookup_association(laddr, paddr, transportp);
  761. sctp_local_bh_enable();
  762. return asoc;
  763. }
  764. /* Is there an association matching the given local and peer addresses? */
  765. int sctp_has_association(const union sctp_addr *laddr,
  766. const union sctp_addr *paddr)
  767. {
  768. struct sctp_association *asoc;
  769. struct sctp_transport *transport;
  770. if ((asoc = sctp_lookup_association(laddr, paddr, &transport))) {
  771. sctp_association_put(asoc);
  772. return 1;
  773. }
  774. return 0;
  775. }
  776. /*
  777. * SCTP Implementors Guide, 2.18 Handling of address
  778. * parameters within the INIT or INIT-ACK.
  779. *
  780. * D) When searching for a matching TCB upon reception of an INIT
  781. * or INIT-ACK chunk the receiver SHOULD use not only the
  782. * source address of the packet (containing the INIT or
  783. * INIT-ACK) but the receiver SHOULD also use all valid
  784. * address parameters contained within the chunk.
  785. *
  786. * 2.18.3 Solution description
  787. *
  788. * This new text clearly specifies to an implementor the need
  789. * to look within the INIT or INIT-ACK. Any implementation that
  790. * does not do this, may not be able to establish associations
  791. * in certain circumstances.
  792. *
  793. */
  794. static struct sctp_association *__sctp_rcv_init_lookup(struct sk_buff *skb,
  795. const union sctp_addr *laddr, struct sctp_transport **transportp)
  796. {
  797. struct sctp_association *asoc;
  798. union sctp_addr addr;
  799. union sctp_addr *paddr = &addr;
  800. struct sctphdr *sh = sctp_hdr(skb);
  801. sctp_chunkhdr_t *ch;
  802. union sctp_params params;
  803. sctp_init_chunk_t *init;
  804. struct sctp_transport *transport;
  805. struct sctp_af *af;
  806. ch = (sctp_chunkhdr_t *) skb->data;
  807. /*
  808. * This code will NOT touch anything inside the chunk--it is
  809. * strictly READ-ONLY.
  810. *
  811. * RFC 2960 3 SCTP packet Format
  812. *
  813. * Multiple chunks can be bundled into one SCTP packet up to
  814. * the MTU size, except for the INIT, INIT ACK, and SHUTDOWN
  815. * COMPLETE chunks. These chunks MUST NOT be bundled with any
  816. * other chunk in a packet. See Section 6.10 for more details
  817. * on chunk bundling.
  818. */
  819. /* Find the start of the TLVs and the end of the chunk. This is
  820. * the region we search for address parameters.
  821. */
  822. init = (sctp_init_chunk_t *)skb->data;
  823. /* Walk the parameters looking for embedded addresses. */
  824. sctp_walk_params(params, init, init_hdr.params) {
  825. /* Note: Ignoring hostname addresses. */
  826. af = sctp_get_af_specific(param_type2af(params.p->type));
  827. if (!af)
  828. continue;
  829. af->from_addr_param(paddr, params.addr, sh->source, 0);
  830. asoc = __sctp_lookup_association(laddr, paddr, &transport);
  831. if (asoc)
  832. return asoc;
  833. }
  834. return NULL;
  835. }
  836. /* ADD-IP, Section 5.2
  837. * When an endpoint receives an ASCONF Chunk from the remote peer
  838. * special procedures may be needed to identify the association the
  839. * ASCONF Chunk is associated with. To properly find the association
  840. * the following procedures SHOULD be followed:
  841. *
  842. * D2) If the association is not found, use the address found in the
  843. * Address Parameter TLV combined with the port number found in the
  844. * SCTP common header. If found proceed to rule D4.
  845. *
  846. * D2-ext) If more than one ASCONF Chunks are packed together, use the
  847. * address found in the ASCONF Address Parameter TLV of each of the
  848. * subsequent ASCONF Chunks. If found, proceed to rule D4.
  849. */
  850. static struct sctp_association *__sctp_rcv_asconf_lookup(
  851. sctp_chunkhdr_t *ch,
  852. const union sctp_addr *laddr,
  853. __be16 peer_port,
  854. struct sctp_transport **transportp)
  855. {
  856. sctp_addip_chunk_t *asconf = (struct sctp_addip_chunk *)ch;
  857. struct sctp_af *af;
  858. union sctp_addr_param *param;
  859. union sctp_addr paddr;
  860. /* Skip over the ADDIP header and find the Address parameter */
  861. param = (union sctp_addr_param *)(asconf + 1);
  862. af = sctp_get_af_specific(param_type2af(param->v4.param_hdr.type));
  863. if (unlikely(!af))
  864. return NULL;
  865. af->from_addr_param(&paddr, param, peer_port, 0);
  866. return __sctp_lookup_association(laddr, &paddr, transportp);
  867. }
  868. /* SCTP-AUTH, Section 6.3:
  869. * If the receiver does not find a STCB for a packet containing an AUTH
  870. * chunk as the first chunk and not a COOKIE-ECHO chunk as the second
  871. * chunk, it MUST use the chunks after the AUTH chunk to look up an existing
  872. * association.
  873. *
  874. * This means that any chunks that can help us identify the association need
  875. * to be looked at to find this assocation.
  876. */
  877. static struct sctp_association *__sctp_rcv_walk_lookup(struct sk_buff *skb,
  878. const union sctp_addr *laddr,
  879. struct sctp_transport **transportp)
  880. {
  881. struct sctp_association *asoc = NULL;
  882. sctp_chunkhdr_t *ch;
  883. int have_auth = 0;
  884. unsigned int chunk_num = 1;
  885. __u8 *ch_end;
  886. /* Walk through the chunks looking for AUTH or ASCONF chunks
  887. * to help us find the association.
  888. */
  889. ch = (sctp_chunkhdr_t *) skb->data;
  890. do {
  891. /* Break out if chunk length is less then minimal. */
  892. if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
  893. break;
  894. ch_end = ((__u8 *)ch) + WORD_ROUND(ntohs(ch->length));
  895. if (ch_end > skb_tail_pointer(skb))
  896. break;
  897. switch(ch->type) {
  898. case SCTP_CID_AUTH:
  899. have_auth = chunk_num;
  900. break;
  901. case SCTP_CID_COOKIE_ECHO:
  902. /* If a packet arrives containing an AUTH chunk as
  903. * a first chunk, a COOKIE-ECHO chunk as the second
  904. * chunk, and possibly more chunks after them, and
  905. * the receiver does not have an STCB for that
  906. * packet, then authentication is based on
  907. * the contents of the COOKIE- ECHO chunk.
  908. */
  909. if (have_auth == 1 && chunk_num == 2)
  910. return NULL;
  911. break;
  912. case SCTP_CID_ASCONF:
  913. if (have_auth || sctp_addip_noauth)
  914. asoc = __sctp_rcv_asconf_lookup(ch, laddr,
  915. sctp_hdr(skb)->source,
  916. transportp);
  917. default:
  918. break;
  919. }
  920. if (asoc)
  921. break;
  922. ch = (sctp_chunkhdr_t *) ch_end;
  923. chunk_num++;
  924. } while (ch_end < skb_tail_pointer(skb));
  925. return asoc;
  926. }
  927. /*
  928. * There are circumstances when we need to look inside the SCTP packet
  929. * for information to help us find the association. Examples
  930. * include looking inside of INIT/INIT-ACK chunks or after the AUTH
  931. * chunks.
  932. */
  933. static struct sctp_association *__sctp_rcv_lookup_harder(struct sk_buff *skb,
  934. const union sctp_addr *laddr,
  935. struct sctp_transport **transportp)
  936. {
  937. sctp_chunkhdr_t *ch;
  938. ch = (sctp_chunkhdr_t *) skb->data;
  939. /* The code below will attempt to walk the chunk and extract
  940. * parameter information. Before we do that, we need to verify
  941. * that the chunk length doesn't cause overflow. Otherwise, we'll
  942. * walk off the end.
  943. */
  944. if (WORD_ROUND(ntohs(ch->length)) > skb->len)
  945. return NULL;
  946. /* If this is INIT/INIT-ACK look inside the chunk too. */
  947. switch (ch->type) {
  948. case SCTP_CID_INIT:
  949. case SCTP_CID_INIT_ACK:
  950. return __sctp_rcv_init_lookup(skb, laddr, transportp);
  951. break;
  952. default:
  953. return __sctp_rcv_walk_lookup(skb, laddr, transportp);
  954. break;
  955. }
  956. return NULL;
  957. }
  958. /* Lookup an association for an inbound skb. */
  959. static struct sctp_association *__sctp_rcv_lookup(struct sk_buff *skb,
  960. const union sctp_addr *paddr,
  961. const union sctp_addr *laddr,
  962. struct sctp_transport **transportp)
  963. {
  964. struct sctp_association *asoc;
  965. asoc = __sctp_lookup_association(laddr, paddr, transportp);
  966. /* Further lookup for INIT/INIT-ACK packets.
  967. * SCTP Implementors Guide, 2.18 Handling of address
  968. * parameters within the INIT or INIT-ACK.
  969. */
  970. if (!asoc)
  971. asoc = __sctp_rcv_lookup_harder(skb, laddr, transportp);
  972. return asoc;
  973. }