input.c 31 KB

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