input.c 31 KB

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