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