input.c 31 KB

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