input.c 31 KB

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