input.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241
  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(net, SCTP_MIB_CHECKSUMERRORS);
  82. return -1;
  83. }
  84. return 0;
  85. }
  86. /*
  87. * This is the routine which IP calls when receiving an SCTP packet.
  88. */
  89. int sctp_rcv(struct sk_buff *skb)
  90. {
  91. struct sock *sk;
  92. struct sctp_association *asoc;
  93. struct sctp_endpoint *ep = NULL;
  94. struct sctp_ep_common *rcvr;
  95. struct sctp_transport *transport = NULL;
  96. struct sctp_chunk *chunk;
  97. union sctp_addr src;
  98. union sctp_addr dest;
  99. int family;
  100. struct sctp_af *af;
  101. struct net *net = dev_net(skb->dev);
  102. if (skb->pkt_type != PACKET_HOST)
  103. goto discard_it;
  104. __SCTP_INC_STATS(net, SCTP_MIB_INSCTPPACKS);
  105. /* If packet is too small to contain a single chunk, let's not
  106. * waste time on it anymore.
  107. */
  108. if (skb->len < sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr) +
  109. skb_transport_offset(skb))
  110. goto discard_it;
  111. /* If the packet is fragmented and we need to do crc checking,
  112. * it's better to just linearize it otherwise crc computing
  113. * takes longer.
  114. */
  115. if ((!(skb_shinfo(skb)->gso_type & SKB_GSO_SCTP) &&
  116. skb_linearize(skb)) ||
  117. !pskb_may_pull(skb, sizeof(struct sctphdr)))
  118. goto discard_it;
  119. /* Pull up the IP header. */
  120. __skb_pull(skb, skb_transport_offset(skb));
  121. skb->csum_valid = 0; /* Previous value not applicable */
  122. if (skb_csum_unnecessary(skb))
  123. __skb_decr_checksum_unnecessary(skb);
  124. else if (!sctp_checksum_disable &&
  125. !(skb_shinfo(skb)->gso_type & SKB_GSO_SCTP) &&
  126. sctp_rcv_checksum(net, skb) < 0)
  127. goto discard_it;
  128. skb->csum_valid = 1;
  129. __skb_pull(skb, sizeof(struct sctphdr));
  130. family = ipver2af(ip_hdr(skb)->version);
  131. af = sctp_get_af_specific(family);
  132. if (unlikely(!af))
  133. goto discard_it;
  134. SCTP_INPUT_CB(skb)->af = af;
  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. if (transport) {
  164. sctp_transport_put(transport);
  165. asoc = NULL;
  166. transport = 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(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, GFP_ATOMIC);
  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 = sctp_hdr(skb);
  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. 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. bh_unlock_sock(sk);
  222. sk = rcvr->sk;
  223. bh_lock_sock(sk);
  224. }
  225. if (sock_owned_by_user(sk)) {
  226. if (sctp_add_backlog(sk, skb)) {
  227. 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(net, SCTP_MIB_IN_PKT_BACKLOG);
  233. } else {
  234. __SCTP_INC_STATS(net, SCTP_MIB_IN_PKT_SOFTIRQ);
  235. sctp_inq_push(&chunk->rcvr->inqueue, chunk);
  236. }
  237. bh_unlock_sock(sk);
  238. /* Release the asoc/ep ref we took in the lookup calls. */
  239. if (transport)
  240. sctp_transport_put(transport);
  241. else
  242. sctp_endpoint_put(ep);
  243. return 0;
  244. discard_it:
  245. __SCTP_INC_STATS(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 (transport)
  251. sctp_transport_put(transport);
  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_transport *t = chunk->transport;
  266. struct sctp_ep_common *rcvr = NULL;
  267. int backloged = 0;
  268. rcvr = chunk->rcvr;
  269. /* If the rcvr is dead then the association or endpoint
  270. * has been deleted and we can safely drop the chunk
  271. * and refs that we are holding.
  272. */
  273. if (rcvr->dead) {
  274. sctp_chunk_free(chunk);
  275. goto done;
  276. }
  277. if (unlikely(rcvr->sk != sk)) {
  278. /* In this case, the association moved from one socket to
  279. * another. We are currently sitting on the backlog of the
  280. * old socket, so we need to move.
  281. * However, since we are here in the process context we
  282. * need to take make sure that the user doesn't own
  283. * the new socket when we process the packet.
  284. * If the new socket is user-owned, queue the chunk to the
  285. * backlog of the new socket without dropping any refs.
  286. * Otherwise, we can safely push the chunk on the inqueue.
  287. */
  288. sk = rcvr->sk;
  289. local_bh_disable();
  290. bh_lock_sock(sk);
  291. if (sock_owned_by_user(sk)) {
  292. if (sk_add_backlog(sk, skb, sk->sk_rcvbuf))
  293. sctp_chunk_free(chunk);
  294. else
  295. backloged = 1;
  296. } else
  297. sctp_inq_push(inqueue, chunk);
  298. bh_unlock_sock(sk);
  299. local_bh_enable();
  300. /* If the chunk was backloged again, don't drop refs */
  301. if (backloged)
  302. return 0;
  303. } else {
  304. sctp_inq_push(inqueue, chunk);
  305. }
  306. done:
  307. /* Release the refs we took in sctp_add_backlog */
  308. if (SCTP_EP_TYPE_ASSOCIATION == rcvr->type)
  309. sctp_transport_put(t);
  310. else if (SCTP_EP_TYPE_SOCKET == rcvr->type)
  311. sctp_endpoint_put(sctp_ep(rcvr));
  312. else
  313. BUG();
  314. return 0;
  315. }
  316. static int sctp_add_backlog(struct sock *sk, struct sk_buff *skb)
  317. {
  318. struct sctp_chunk *chunk = SCTP_INPUT_CB(skb)->chunk;
  319. struct sctp_transport *t = chunk->transport;
  320. struct sctp_ep_common *rcvr = chunk->rcvr;
  321. int ret;
  322. ret = sk_add_backlog(sk, skb, sk->sk_rcvbuf);
  323. if (!ret) {
  324. /* Hold the assoc/ep while hanging on the backlog queue.
  325. * This way, we know structures we need will not disappear
  326. * from us
  327. */
  328. if (SCTP_EP_TYPE_ASSOCIATION == rcvr->type)
  329. sctp_transport_hold(t);
  330. else if (SCTP_EP_TYPE_SOCKET == rcvr->type)
  331. sctp_endpoint_hold(sctp_ep(rcvr));
  332. else
  333. BUG();
  334. }
  335. return ret;
  336. }
  337. /* Handle icmp frag needed error. */
  338. void sctp_icmp_frag_needed(struct sock *sk, struct sctp_association *asoc,
  339. struct sctp_transport *t, __u32 pmtu)
  340. {
  341. if (!t || (t->pathmtu <= pmtu))
  342. return;
  343. if (sock_owned_by_user(sk)) {
  344. asoc->pmtu_pending = 1;
  345. t->pmtu_pending = 1;
  346. return;
  347. }
  348. if (t->param_flags & SPP_PMTUD_ENABLE) {
  349. /* Update transports view of the MTU */
  350. sctp_transport_update_pmtu(sk, t, pmtu);
  351. /* Update association pmtu. */
  352. sctp_assoc_sync_pmtu(sk, asoc);
  353. }
  354. /* Retransmit with the new pmtu setting.
  355. * Normally, if PMTU discovery is disabled, an ICMP Fragmentation
  356. * Needed will never be sent, but if a message was sent before
  357. * PMTU discovery was disabled that was larger than the PMTU, it
  358. * would not be fragmented, so it must be re-transmitted fragmented.
  359. */
  360. sctp_retransmit(&asoc->outqueue, t, SCTP_RTXR_PMTUD);
  361. }
  362. void sctp_icmp_redirect(struct sock *sk, struct sctp_transport *t,
  363. struct sk_buff *skb)
  364. {
  365. struct dst_entry *dst;
  366. if (!t)
  367. return;
  368. dst = sctp_transport_dst_check(t);
  369. if (dst)
  370. dst->ops->redirect(dst, sk, skb);
  371. }
  372. /*
  373. * SCTP Implementer's Guide, 2.37 ICMP handling procedures
  374. *
  375. * ICMP8) If the ICMP code is a "Unrecognized next header type encountered"
  376. * or a "Protocol Unreachable" treat this message as an abort
  377. * with the T bit set.
  378. *
  379. * This function sends an event to the state machine, which will abort the
  380. * association.
  381. *
  382. */
  383. void sctp_icmp_proto_unreachable(struct sock *sk,
  384. struct sctp_association *asoc,
  385. struct sctp_transport *t)
  386. {
  387. if (sock_owned_by_user(sk)) {
  388. if (timer_pending(&t->proto_unreach_timer))
  389. return;
  390. else {
  391. if (!mod_timer(&t->proto_unreach_timer,
  392. jiffies + (HZ/20)))
  393. sctp_association_hold(asoc);
  394. }
  395. } else {
  396. struct net *net = sock_net(sk);
  397. pr_debug("%s: unrecognized next header type "
  398. "encountered!\n", __func__);
  399. if (del_timer(&t->proto_unreach_timer))
  400. sctp_association_put(asoc);
  401. sctp_do_sm(net, SCTP_EVENT_T_OTHER,
  402. SCTP_ST_OTHER(SCTP_EVENT_ICMP_PROTO_UNREACH),
  403. asoc->state, asoc->ep, asoc, t,
  404. GFP_ATOMIC);
  405. }
  406. }
  407. /* Common lookup code for icmp/icmpv6 error handler. */
  408. struct sock *sctp_err_lookup(struct net *net, int family, struct sk_buff *skb,
  409. struct sctphdr *sctphdr,
  410. struct sctp_association **app,
  411. struct sctp_transport **tpp)
  412. {
  413. union sctp_addr saddr;
  414. union sctp_addr daddr;
  415. struct sctp_af *af;
  416. struct sock *sk = NULL;
  417. struct sctp_association *asoc;
  418. struct sctp_transport *transport = NULL;
  419. struct sctp_init_chunk *chunkhdr;
  420. __u32 vtag = ntohl(sctphdr->vtag);
  421. int len = skb->len - ((void *)sctphdr - (void *)skb->data);
  422. *app = NULL; *tpp = NULL;
  423. af = sctp_get_af_specific(family);
  424. if (unlikely(!af)) {
  425. return NULL;
  426. }
  427. /* Initialize local addresses for lookups. */
  428. af->from_skb(&saddr, skb, 1);
  429. af->from_skb(&daddr, skb, 0);
  430. /* Look for an association that matches the incoming ICMP error
  431. * packet.
  432. */
  433. asoc = __sctp_lookup_association(net, &saddr, &daddr, &transport);
  434. if (!asoc)
  435. return NULL;
  436. sk = asoc->base.sk;
  437. /* RFC 4960, Appendix C. ICMP Handling
  438. *
  439. * ICMP6) An implementation MUST validate that the Verification Tag
  440. * contained in the ICMP message matches the Verification Tag of
  441. * the peer. If the Verification Tag is not 0 and does NOT
  442. * match, discard the ICMP message. If it is 0 and the ICMP
  443. * message contains enough bytes to verify that the chunk type is
  444. * an INIT chunk and that the Initiate Tag matches the tag of the
  445. * peer, continue with ICMP7. If the ICMP message is too short
  446. * or the chunk type or the Initiate Tag does not match, silently
  447. * discard the packet.
  448. */
  449. if (vtag == 0) {
  450. chunkhdr = (void *)sctphdr + sizeof(struct sctphdr);
  451. if (len < sizeof(struct sctphdr) + sizeof(sctp_chunkhdr_t)
  452. + sizeof(__be32) ||
  453. chunkhdr->chunk_hdr.type != SCTP_CID_INIT ||
  454. ntohl(chunkhdr->init_hdr.init_tag) != asoc->c.my_vtag) {
  455. goto out;
  456. }
  457. } else if (vtag != asoc->c.peer_vtag) {
  458. goto out;
  459. }
  460. bh_lock_sock(sk);
  461. /* If too many ICMPs get dropped on busy
  462. * servers this needs to be solved differently.
  463. */
  464. if (sock_owned_by_user(sk))
  465. __NET_INC_STATS(net, LINUX_MIB_LOCKDROPPEDICMPS);
  466. *app = asoc;
  467. *tpp = transport;
  468. return sk;
  469. out:
  470. sctp_transport_put(transport);
  471. return NULL;
  472. }
  473. /* Common cleanup code for icmp/icmpv6 error handler. */
  474. void sctp_err_finish(struct sock *sk, struct sctp_transport *t)
  475. {
  476. bh_unlock_sock(sk);
  477. sctp_transport_put(t);
  478. }
  479. /*
  480. * This routine is called by the ICMP module when it gets some
  481. * sort of error condition. If err < 0 then the socket should
  482. * be closed and the error returned to the user. If err > 0
  483. * it's just the icmp type << 8 | icmp code. After adjustment
  484. * header points to the first 8 bytes of the sctp header. We need
  485. * to find the appropriate port.
  486. *
  487. * The locking strategy used here is very "optimistic". When
  488. * someone else accesses the socket the ICMP is just dropped
  489. * and for some paths there is no check at all.
  490. * A more general error queue to queue errors for later handling
  491. * is probably better.
  492. *
  493. */
  494. void sctp_v4_err(struct sk_buff *skb, __u32 info)
  495. {
  496. const struct iphdr *iph = (const struct iphdr *)skb->data;
  497. const int ihlen = iph->ihl * 4;
  498. const int type = icmp_hdr(skb)->type;
  499. const int code = icmp_hdr(skb)->code;
  500. struct sock *sk;
  501. struct sctp_association *asoc = NULL;
  502. struct sctp_transport *transport;
  503. struct inet_sock *inet;
  504. __u16 saveip, savesctp;
  505. int err;
  506. struct net *net = dev_net(skb->dev);
  507. /* Fix up skb to look at the embedded net header. */
  508. saveip = skb->network_header;
  509. savesctp = skb->transport_header;
  510. skb_reset_network_header(skb);
  511. skb_set_transport_header(skb, ihlen);
  512. sk = sctp_err_lookup(net, AF_INET, skb, sctp_hdr(skb), &asoc, &transport);
  513. /* Put back, the original values. */
  514. skb->network_header = saveip;
  515. skb->transport_header = savesctp;
  516. if (!sk) {
  517. __ICMP_INC_STATS(net, ICMP_MIB_INERRORS);
  518. return;
  519. }
  520. /* Warning: The sock lock is held. Remember to call
  521. * sctp_err_finish!
  522. */
  523. switch (type) {
  524. case ICMP_PARAMETERPROB:
  525. err = EPROTO;
  526. break;
  527. case ICMP_DEST_UNREACH:
  528. if (code > NR_ICMP_UNREACH)
  529. goto out_unlock;
  530. /* PMTU discovery (RFC1191) */
  531. if (ICMP_FRAG_NEEDED == code) {
  532. sctp_icmp_frag_needed(sk, asoc, transport,
  533. SCTP_TRUNC4(info));
  534. goto out_unlock;
  535. } else {
  536. if (ICMP_PROT_UNREACH == code) {
  537. sctp_icmp_proto_unreachable(sk, asoc,
  538. transport);
  539. goto out_unlock;
  540. }
  541. }
  542. err = icmp_err_convert[code].errno;
  543. break;
  544. case ICMP_TIME_EXCEEDED:
  545. /* Ignore any time exceeded errors due to fragment reassembly
  546. * timeouts.
  547. */
  548. if (ICMP_EXC_FRAGTIME == code)
  549. goto out_unlock;
  550. err = EHOSTUNREACH;
  551. break;
  552. case ICMP_REDIRECT:
  553. sctp_icmp_redirect(sk, transport, skb);
  554. /* Fall through to out_unlock. */
  555. default:
  556. goto out_unlock;
  557. }
  558. inet = inet_sk(sk);
  559. if (!sock_owned_by_user(sk) && inet->recverr) {
  560. sk->sk_err = err;
  561. sk->sk_error_report(sk);
  562. } else { /* Only an error on timeout */
  563. sk->sk_err_soft = err;
  564. }
  565. out_unlock:
  566. sctp_err_finish(sk, transport);
  567. }
  568. /*
  569. * RFC 2960, 8.4 - Handle "Out of the blue" Packets.
  570. *
  571. * This function scans all the chunks in the OOTB packet to determine if
  572. * the packet should be discarded right away. If a response might be needed
  573. * for this packet, or, if further processing is possible, the packet will
  574. * be queued to a proper inqueue for the next phase of handling.
  575. *
  576. * Output:
  577. * Return 0 - If further processing is needed.
  578. * Return 1 - If the packet can be discarded right away.
  579. */
  580. static int sctp_rcv_ootb(struct sk_buff *skb)
  581. {
  582. sctp_chunkhdr_t *ch, _ch;
  583. int ch_end, offset = 0;
  584. /* Scan through all the chunks in the packet. */
  585. do {
  586. /* Make sure we have at least the header there */
  587. if (offset + sizeof(sctp_chunkhdr_t) > skb->len)
  588. break;
  589. ch = skb_header_pointer(skb, offset, sizeof(*ch), &_ch);
  590. /* Break out if chunk length is less then minimal. */
  591. if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
  592. break;
  593. ch_end = offset + SCTP_PAD4(ntohs(ch->length));
  594. if (ch_end > skb->len)
  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. offset = ch_end;
  616. } while (ch_end < skb->len);
  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. write_lock(&head->lock);
  631. hlist_add_head(&epb->node, &head->chain);
  632. 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. local_bh_disable();
  638. __sctp_hash_endpoint(ep);
  639. 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. write_lock(&head->lock);
  651. hlist_del_init(&epb->node);
  652. 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. local_bh_disable();
  658. __sctp_unhash_endpoint(ep);
  659. 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. /* rhashtable for transport */
  684. struct sctp_hash_cmp_arg {
  685. const union sctp_addr *paddr;
  686. const struct net *net;
  687. u16 lport;
  688. };
  689. static inline int sctp_hash_cmp(struct rhashtable_compare_arg *arg,
  690. const void *ptr)
  691. {
  692. struct sctp_transport *t = (struct sctp_transport *)ptr;
  693. const struct sctp_hash_cmp_arg *x = arg->key;
  694. int err = 1;
  695. if (!sctp_cmp_addr_exact(&t->ipaddr, x->paddr))
  696. return err;
  697. if (!sctp_transport_hold(t))
  698. return err;
  699. if (!net_eq(sock_net(t->asoc->base.sk), x->net))
  700. goto out;
  701. if (x->lport != htons(t->asoc->base.bind_addr.port))
  702. goto out;
  703. err = 0;
  704. out:
  705. sctp_transport_put(t);
  706. return err;
  707. }
  708. static inline u32 sctp_hash_obj(const void *data, u32 len, u32 seed)
  709. {
  710. const struct sctp_transport *t = data;
  711. const union sctp_addr *paddr = &t->ipaddr;
  712. const struct net *net = sock_net(t->asoc->base.sk);
  713. u16 lport = htons(t->asoc->base.bind_addr.port);
  714. u32 addr;
  715. if (paddr->sa.sa_family == AF_INET6)
  716. addr = jhash(&paddr->v6.sin6_addr, 16, seed);
  717. else
  718. addr = paddr->v4.sin_addr.s_addr;
  719. return jhash_3words(addr, ((__u32)paddr->v4.sin_port) << 16 |
  720. (__force __u32)lport, net_hash_mix(net), seed);
  721. }
  722. static inline u32 sctp_hash_key(const void *data, u32 len, u32 seed)
  723. {
  724. const struct sctp_hash_cmp_arg *x = data;
  725. const union sctp_addr *paddr = x->paddr;
  726. const struct net *net = x->net;
  727. u16 lport = x->lport;
  728. u32 addr;
  729. if (paddr->sa.sa_family == AF_INET6)
  730. addr = jhash(&paddr->v6.sin6_addr, 16, seed);
  731. else
  732. addr = paddr->v4.sin_addr.s_addr;
  733. return jhash_3words(addr, ((__u32)paddr->v4.sin_port) << 16 |
  734. (__force __u32)lport, net_hash_mix(net), seed);
  735. }
  736. static const struct rhashtable_params sctp_hash_params = {
  737. .head_offset = offsetof(struct sctp_transport, node),
  738. .hashfn = sctp_hash_key,
  739. .obj_hashfn = sctp_hash_obj,
  740. .obj_cmpfn = sctp_hash_cmp,
  741. .automatic_shrinking = true,
  742. };
  743. int sctp_transport_hashtable_init(void)
  744. {
  745. return rhltable_init(&sctp_transport_hashtable, &sctp_hash_params);
  746. }
  747. void sctp_transport_hashtable_destroy(void)
  748. {
  749. rhltable_destroy(&sctp_transport_hashtable);
  750. }
  751. int sctp_hash_transport(struct sctp_transport *t)
  752. {
  753. struct sctp_hash_cmp_arg arg;
  754. int err;
  755. if (t->asoc->temp)
  756. return 0;
  757. arg.net = sock_net(t->asoc->base.sk);
  758. arg.paddr = &t->ipaddr;
  759. arg.lport = htons(t->asoc->base.bind_addr.port);
  760. err = rhltable_insert_key(&sctp_transport_hashtable, &arg,
  761. &t->node, sctp_hash_params);
  762. if (err)
  763. pr_err_once("insert transport fail, errno %d\n", err);
  764. return err;
  765. }
  766. void sctp_unhash_transport(struct sctp_transport *t)
  767. {
  768. if (t->asoc->temp)
  769. return;
  770. rhltable_remove(&sctp_transport_hashtable, &t->node,
  771. sctp_hash_params);
  772. }
  773. /* return a transport with holding it */
  774. struct sctp_transport *sctp_addrs_lookup_transport(
  775. struct net *net,
  776. const union sctp_addr *laddr,
  777. const union sctp_addr *paddr)
  778. {
  779. struct rhlist_head *tmp, *list;
  780. struct sctp_transport *t;
  781. struct sctp_hash_cmp_arg arg = {
  782. .paddr = paddr,
  783. .net = net,
  784. .lport = laddr->v4.sin_port,
  785. };
  786. list = rhltable_lookup(&sctp_transport_hashtable, &arg,
  787. sctp_hash_params);
  788. rhl_for_each_entry_rcu(t, tmp, list, node) {
  789. if (!sctp_transport_hold(t))
  790. continue;
  791. if (sctp_bind_addr_match(&t->asoc->base.bind_addr,
  792. laddr, sctp_sk(t->asoc->base.sk)))
  793. return t;
  794. sctp_transport_put(t);
  795. }
  796. return NULL;
  797. }
  798. /* return a transport without holding it, as it's only used under sock lock */
  799. struct sctp_transport *sctp_epaddr_lookup_transport(
  800. const struct sctp_endpoint *ep,
  801. const union sctp_addr *paddr)
  802. {
  803. struct net *net = sock_net(ep->base.sk);
  804. struct rhlist_head *tmp, *list;
  805. struct sctp_transport *t;
  806. struct sctp_hash_cmp_arg arg = {
  807. .paddr = paddr,
  808. .net = net,
  809. .lport = htons(ep->base.bind_addr.port),
  810. };
  811. list = rhltable_lookup(&sctp_transport_hashtable, &arg,
  812. sctp_hash_params);
  813. rhl_for_each_entry_rcu(t, tmp, list, node)
  814. if (ep == t->asoc->ep)
  815. return t;
  816. return NULL;
  817. }
  818. /* Look up an association. */
  819. static struct sctp_association *__sctp_lookup_association(
  820. struct net *net,
  821. const union sctp_addr *local,
  822. const union sctp_addr *peer,
  823. struct sctp_transport **pt)
  824. {
  825. struct sctp_transport *t;
  826. struct sctp_association *asoc = NULL;
  827. t = sctp_addrs_lookup_transport(net, local, peer);
  828. if (!t)
  829. goto out;
  830. asoc = t->asoc;
  831. *pt = t;
  832. out:
  833. return asoc;
  834. }
  835. /* Look up an association. protected by RCU read lock */
  836. static
  837. struct sctp_association *sctp_lookup_association(struct net *net,
  838. const union sctp_addr *laddr,
  839. const union sctp_addr *paddr,
  840. struct sctp_transport **transportp)
  841. {
  842. struct sctp_association *asoc;
  843. rcu_read_lock();
  844. asoc = __sctp_lookup_association(net, laddr, paddr, transportp);
  845. rcu_read_unlock();
  846. return asoc;
  847. }
  848. /* Is there an association matching the given local and peer addresses? */
  849. int sctp_has_association(struct net *net,
  850. const union sctp_addr *laddr,
  851. const union sctp_addr *paddr)
  852. {
  853. struct sctp_association *asoc;
  854. struct sctp_transport *transport;
  855. if ((asoc = sctp_lookup_association(net, laddr, paddr, &transport))) {
  856. sctp_transport_put(transport);
  857. return 1;
  858. }
  859. return 0;
  860. }
  861. /*
  862. * SCTP Implementors Guide, 2.18 Handling of address
  863. * parameters within the INIT or INIT-ACK.
  864. *
  865. * D) When searching for a matching TCB upon reception of an INIT
  866. * or INIT-ACK chunk the receiver SHOULD use not only the
  867. * source address of the packet (containing the INIT or
  868. * INIT-ACK) but the receiver SHOULD also use all valid
  869. * address parameters contained within the chunk.
  870. *
  871. * 2.18.3 Solution description
  872. *
  873. * This new text clearly specifies to an implementor the need
  874. * to look within the INIT or INIT-ACK. Any implementation that
  875. * does not do this, may not be able to establish associations
  876. * in certain circumstances.
  877. *
  878. */
  879. static struct sctp_association *__sctp_rcv_init_lookup(struct net *net,
  880. struct sk_buff *skb,
  881. const union sctp_addr *laddr, struct sctp_transport **transportp)
  882. {
  883. struct sctp_association *asoc;
  884. union sctp_addr addr;
  885. union sctp_addr *paddr = &addr;
  886. struct sctphdr *sh = sctp_hdr(skb);
  887. union sctp_params params;
  888. sctp_init_chunk_t *init;
  889. struct sctp_af *af;
  890. /*
  891. * This code will NOT touch anything inside the chunk--it is
  892. * strictly READ-ONLY.
  893. *
  894. * RFC 2960 3 SCTP packet Format
  895. *
  896. * Multiple chunks can be bundled into one SCTP packet up to
  897. * the MTU size, except for the INIT, INIT ACK, and SHUTDOWN
  898. * COMPLETE chunks. These chunks MUST NOT be bundled with any
  899. * other chunk in a packet. See Section 6.10 for more details
  900. * on chunk bundling.
  901. */
  902. /* Find the start of the TLVs and the end of the chunk. This is
  903. * the region we search for address parameters.
  904. */
  905. init = (sctp_init_chunk_t *)skb->data;
  906. /* Walk the parameters looking for embedded addresses. */
  907. sctp_walk_params(params, init, init_hdr.params) {
  908. /* Note: Ignoring hostname addresses. */
  909. af = sctp_get_af_specific(param_type2af(params.p->type));
  910. if (!af)
  911. continue;
  912. af->from_addr_param(paddr, params.addr, sh->source, 0);
  913. asoc = __sctp_lookup_association(net, laddr, paddr, transportp);
  914. if (asoc)
  915. return asoc;
  916. }
  917. return NULL;
  918. }
  919. /* ADD-IP, Section 5.2
  920. * When an endpoint receives an ASCONF Chunk from the remote peer
  921. * special procedures may be needed to identify the association the
  922. * ASCONF Chunk is associated with. To properly find the association
  923. * the following procedures SHOULD be followed:
  924. *
  925. * D2) If the association is not found, use the address found in the
  926. * Address Parameter TLV combined with the port number found in the
  927. * SCTP common header. If found proceed to rule D4.
  928. *
  929. * D2-ext) If more than one ASCONF Chunks are packed together, use the
  930. * address found in the ASCONF Address Parameter TLV of each of the
  931. * subsequent ASCONF Chunks. If found, proceed to rule D4.
  932. */
  933. static struct sctp_association *__sctp_rcv_asconf_lookup(
  934. struct net *net,
  935. sctp_chunkhdr_t *ch,
  936. const union sctp_addr *laddr,
  937. __be16 peer_port,
  938. struct sctp_transport **transportp)
  939. {
  940. sctp_addip_chunk_t *asconf = (struct sctp_addip_chunk *)ch;
  941. struct sctp_af *af;
  942. union sctp_addr_param *param;
  943. union sctp_addr paddr;
  944. /* Skip over the ADDIP header and find the Address parameter */
  945. param = (union sctp_addr_param *)(asconf + 1);
  946. af = sctp_get_af_specific(param_type2af(param->p.type));
  947. if (unlikely(!af))
  948. return NULL;
  949. af->from_addr_param(&paddr, param, peer_port, 0);
  950. return __sctp_lookup_association(net, laddr, &paddr, transportp);
  951. }
  952. /* SCTP-AUTH, Section 6.3:
  953. * If the receiver does not find a STCB for a packet containing an AUTH
  954. * chunk as the first chunk and not a COOKIE-ECHO chunk as the second
  955. * chunk, it MUST use the chunks after the AUTH chunk to look up an existing
  956. * association.
  957. *
  958. * This means that any chunks that can help us identify the association need
  959. * to be looked at to find this association.
  960. */
  961. static struct sctp_association *__sctp_rcv_walk_lookup(struct net *net,
  962. struct sk_buff *skb,
  963. const union sctp_addr *laddr,
  964. struct sctp_transport **transportp)
  965. {
  966. struct sctp_association *asoc = NULL;
  967. sctp_chunkhdr_t *ch;
  968. int have_auth = 0;
  969. unsigned int chunk_num = 1;
  970. __u8 *ch_end;
  971. /* Walk through the chunks looking for AUTH or ASCONF chunks
  972. * to help us find the association.
  973. */
  974. ch = (sctp_chunkhdr_t *) skb->data;
  975. do {
  976. /* Break out if chunk length is less then minimal. */
  977. if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
  978. break;
  979. ch_end = ((__u8 *)ch) + SCTP_PAD4(ntohs(ch->length));
  980. if (ch_end > skb_tail_pointer(skb))
  981. break;
  982. switch (ch->type) {
  983. case SCTP_CID_AUTH:
  984. have_auth = chunk_num;
  985. break;
  986. case SCTP_CID_COOKIE_ECHO:
  987. /* If a packet arrives containing an AUTH chunk as
  988. * a first chunk, a COOKIE-ECHO chunk as the second
  989. * chunk, and possibly more chunks after them, and
  990. * the receiver does not have an STCB for that
  991. * packet, then authentication is based on
  992. * the contents of the COOKIE- ECHO chunk.
  993. */
  994. if (have_auth == 1 && chunk_num == 2)
  995. return NULL;
  996. break;
  997. case SCTP_CID_ASCONF:
  998. if (have_auth || net->sctp.addip_noauth)
  999. asoc = __sctp_rcv_asconf_lookup(
  1000. net, ch, laddr,
  1001. sctp_hdr(skb)->source,
  1002. transportp);
  1003. default:
  1004. break;
  1005. }
  1006. if (asoc)
  1007. break;
  1008. ch = (sctp_chunkhdr_t *) ch_end;
  1009. chunk_num++;
  1010. } while (ch_end < skb_tail_pointer(skb));
  1011. return asoc;
  1012. }
  1013. /*
  1014. * There are circumstances when we need to look inside the SCTP packet
  1015. * for information to help us find the association. Examples
  1016. * include looking inside of INIT/INIT-ACK chunks or after the AUTH
  1017. * chunks.
  1018. */
  1019. static struct sctp_association *__sctp_rcv_lookup_harder(struct net *net,
  1020. struct sk_buff *skb,
  1021. const union sctp_addr *laddr,
  1022. struct sctp_transport **transportp)
  1023. {
  1024. sctp_chunkhdr_t *ch;
  1025. /* We do not allow GSO frames here as we need to linearize and
  1026. * then cannot guarantee frame boundaries. This shouldn't be an
  1027. * issue as packets hitting this are mostly INIT or INIT-ACK and
  1028. * those cannot be on GSO-style anyway.
  1029. */
  1030. if ((skb_shinfo(skb)->gso_type & SKB_GSO_SCTP) == SKB_GSO_SCTP)
  1031. return NULL;
  1032. ch = (sctp_chunkhdr_t *) skb->data;
  1033. /* The code below will attempt to walk the chunk and extract
  1034. * parameter information. Before we do that, we need to verify
  1035. * that the chunk length doesn't cause overflow. Otherwise, we'll
  1036. * walk off the end.
  1037. */
  1038. if (SCTP_PAD4(ntohs(ch->length)) > skb->len)
  1039. return NULL;
  1040. /* If this is INIT/INIT-ACK look inside the chunk too. */
  1041. if (ch->type == SCTP_CID_INIT || ch->type == SCTP_CID_INIT_ACK)
  1042. return __sctp_rcv_init_lookup(net, skb, laddr, transportp);
  1043. return __sctp_rcv_walk_lookup(net, skb, laddr, transportp);
  1044. }
  1045. /* Lookup an association for an inbound skb. */
  1046. static struct sctp_association *__sctp_rcv_lookup(struct net *net,
  1047. struct sk_buff *skb,
  1048. const union sctp_addr *paddr,
  1049. const union sctp_addr *laddr,
  1050. struct sctp_transport **transportp)
  1051. {
  1052. struct sctp_association *asoc;
  1053. asoc = __sctp_lookup_association(net, laddr, paddr, transportp);
  1054. /* Further lookup for INIT/INIT-ACK packets.
  1055. * SCTP Implementors Guide, 2.18 Handling of address
  1056. * parameters within the INIT or INIT-ACK.
  1057. */
  1058. if (!asoc)
  1059. asoc = __sctp_rcv_lookup_harder(net, skb, laddr, transportp);
  1060. return asoc;
  1061. }