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