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