input.c 31 KB

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