input.c 30 KB

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