input.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222
  1. /* SCTP kernel implementation
  2. * Copyright (c) 1999-2000 Cisco, Inc.
  3. * Copyright (c) 1999-2001 Motorola, Inc.
  4. * Copyright (c) 2001-2003 International Business Machines, Corp.
  5. * Copyright (c) 2001 Intel Corp.
  6. * Copyright (c) 2001 Nokia, Inc.
  7. * Copyright (c) 2001 La Monte H.P. Yarroll
  8. *
  9. * This file is part of the SCTP kernel implementation
  10. *
  11. * These functions handle all input from the IP layer into SCTP.
  12. *
  13. * This SCTP implementation is free software;
  14. * you can redistribute it and/or modify it under the terms of
  15. * the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2, or (at your option)
  17. * any later version.
  18. *
  19. * This SCTP implementation is distributed in the hope that it
  20. * will be useful, but WITHOUT ANY WARRANTY; without even the implied
  21. * ************************
  22. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  23. * See the GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with GNU CC; see the file COPYING. If not, see
  27. * <http://www.gnu.org/licenses/>.
  28. *
  29. * Please send any bug reports or fixes you make to the
  30. * email address(es):
  31. * lksctp developers <linux-sctp@vger.kernel.org>
  32. *
  33. * Written or modified by:
  34. * La Monte H.P. Yarroll <piggy@acm.org>
  35. * Karl Knutson <karl@athena.chicago.il.us>
  36. * Xingang Guo <xingang.guo@intel.com>
  37. * Jon Grimm <jgrimm@us.ibm.com>
  38. * Hui Huang <hui.huang@nokia.com>
  39. * Daisy Chang <daisyc@us.ibm.com>
  40. * Sridhar Samudrala <sri@us.ibm.com>
  41. * Ardelle Fan <ardelle.fan@intel.com>
  42. */
  43. #include <linux/types.h>
  44. #include <linux/list.h> /* For struct list_head */
  45. #include <linux/socket.h>
  46. #include <linux/ip.h>
  47. #include <linux/time.h> /* For struct timeval */
  48. #include <linux/slab.h>
  49. #include <net/ip.h>
  50. #include <net/icmp.h>
  51. #include <net/snmp.h>
  52. #include <net/sock.h>
  53. #include <net/xfrm.h>
  54. #include <net/sctp/sctp.h>
  55. #include <net/sctp/sm.h>
  56. #include <net/sctp/checksum.h>
  57. #include <net/net_namespace.h>
  58. /* Forward declarations for internal helpers. */
  59. static int sctp_rcv_ootb(struct sk_buff *);
  60. static struct sctp_association *__sctp_rcv_lookup(struct net *net,
  61. struct sk_buff *skb,
  62. const union sctp_addr *paddr,
  63. const union sctp_addr *laddr,
  64. struct sctp_transport **transportp);
  65. static struct sctp_endpoint *__sctp_rcv_lookup_endpoint(struct net *net,
  66. const union sctp_addr *laddr);
  67. static struct sctp_association *__sctp_lookup_association(
  68. struct net *net,
  69. const union sctp_addr *local,
  70. const union sctp_addr *peer,
  71. struct sctp_transport **pt);
  72. static int sctp_add_backlog(struct sock *sk, struct sk_buff *skb);
  73. /* Calculate the SCTP checksum of an SCTP packet. */
  74. static inline int sctp_rcv_checksum(struct net *net, struct sk_buff *skb)
  75. {
  76. struct sctphdr *sh = sctp_hdr(skb);
  77. __le32 cmp = sh->checksum;
  78. __le32 val = sctp_compute_cksum(skb, 0);
  79. if (val != cmp) {
  80. /* CRC failure, dump it. */
  81. __SCTP_INC_STATS(net, SCTP_MIB_CHECKSUMERRORS);
  82. return -1;
  83. }
  84. return 0;
  85. }
  86. /*
  87. * This is the routine which IP calls when receiving an SCTP packet.
  88. */
  89. int sctp_rcv(struct sk_buff *skb)
  90. {
  91. struct sock *sk;
  92. struct sctp_association *asoc;
  93. struct sctp_endpoint *ep = NULL;
  94. struct sctp_ep_common *rcvr;
  95. struct sctp_transport *transport = NULL;
  96. struct sctp_chunk *chunk;
  97. union sctp_addr src;
  98. union sctp_addr dest;
  99. int family;
  100. struct sctp_af *af;
  101. struct net *net = dev_net(skb->dev);
  102. if (skb->pkt_type != PACKET_HOST)
  103. goto discard_it;
  104. __SCTP_INC_STATS(net, SCTP_MIB_INSCTPPACKS);
  105. /* If packet is too small to contain a single chunk, let's not
  106. * waste time on it anymore.
  107. */
  108. if (skb->len < sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr) +
  109. skb_transport_offset(skb))
  110. goto discard_it;
  111. /* If the packet is fragmented and we need to do crc checking,
  112. * it's better to just linearize it otherwise crc computing
  113. * takes longer.
  114. */
  115. if ((!(skb_shinfo(skb)->gso_type & SKB_GSO_SCTP) &&
  116. skb_linearize(skb)) ||
  117. !pskb_may_pull(skb, sizeof(struct sctphdr)))
  118. goto discard_it;
  119. /* Pull up the IP header. */
  120. __skb_pull(skb, skb_transport_offset(skb));
  121. skb->csum_valid = 0; /* Previous value not applicable */
  122. if (skb_csum_unnecessary(skb))
  123. __skb_decr_checksum_unnecessary(skb);
  124. else if (!sctp_checksum_disable &&
  125. !(skb_shinfo(skb)->gso_type & SKB_GSO_SCTP) &&
  126. sctp_rcv_checksum(net, skb) < 0)
  127. goto discard_it;
  128. skb->csum_valid = 1;
  129. __skb_pull(skb, sizeof(struct sctphdr));
  130. family = ipver2af(ip_hdr(skb)->version);
  131. af = sctp_get_af_specific(family);
  132. if (unlikely(!af))
  133. goto discard_it;
  134. SCTP_INPUT_CB(skb)->af = af;
  135. /* Initialize local addresses for lookups. */
  136. af->from_skb(&src, skb, 1);
  137. af->from_skb(&dest, skb, 0);
  138. /* If the packet is to or from a non-unicast address,
  139. * silently discard the packet.
  140. *
  141. * This is not clearly defined in the RFC except in section
  142. * 8.4 - OOTB handling. However, based on the book "Stream Control
  143. * Transmission Protocol" 2.1, "It is important to note that the
  144. * IP address of an SCTP transport address must be a routable
  145. * unicast address. In other words, IP multicast addresses and
  146. * IP broadcast addresses cannot be used in an SCTP transport
  147. * address."
  148. */
  149. if (!af->addr_valid(&src, NULL, skb) ||
  150. !af->addr_valid(&dest, NULL, skb))
  151. goto discard_it;
  152. asoc = __sctp_rcv_lookup(net, skb, &src, &dest, &transport);
  153. if (!asoc)
  154. ep = __sctp_rcv_lookup_endpoint(net, &dest);
  155. /* Retrieve the common input handling substructure. */
  156. rcvr = asoc ? &asoc->base : &ep->base;
  157. sk = rcvr->sk;
  158. /*
  159. * If a frame arrives on an interface and the receiving socket is
  160. * bound to another interface, via SO_BINDTODEVICE, treat it as OOTB
  161. */
  162. if (sk->sk_bound_dev_if && (sk->sk_bound_dev_if != af->skb_iif(skb))) {
  163. if (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(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, GFP_ATOMIC);
  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 = sctp_hdr(skb);
  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. 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. bh_unlock_sock(sk);
  221. sk = rcvr->sk;
  222. bh_lock_sock(sk);
  223. }
  224. if (sock_owned_by_user(sk)) {
  225. if (sctp_add_backlog(sk, skb)) {
  226. 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(net, SCTP_MIB_IN_PKT_BACKLOG);
  232. } else {
  233. __SCTP_INC_STATS(net, SCTP_MIB_IN_PKT_SOFTIRQ);
  234. sctp_inq_push(&chunk->rcvr->inqueue, chunk);
  235. }
  236. 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(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. local_bh_disable();
  288. bh_lock_sock(sk);
  289. if (sock_owned_by_user(sk)) {
  290. if (sk_add_backlog(sk, skb, sk->sk_rcvbuf))
  291. sctp_chunk_free(chunk);
  292. else
  293. backloged = 1;
  294. } else
  295. sctp_inq_push(inqueue, chunk);
  296. bh_unlock_sock(sk);
  297. local_bh_enable();
  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(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(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,
  530. WORD_TRUNC(info));
  531. goto out_unlock;
  532. } else {
  533. if (ICMP_PROT_UNREACH == code) {
  534. sctp_icmp_proto_unreachable(sk, asoc,
  535. transport);
  536. goto out_unlock;
  537. }
  538. }
  539. err = icmp_err_convert[code].errno;
  540. break;
  541. case ICMP_TIME_EXCEEDED:
  542. /* Ignore any time exceeded errors due to fragment reassembly
  543. * timeouts.
  544. */
  545. if (ICMP_EXC_FRAGTIME == code)
  546. goto out_unlock;
  547. err = EHOSTUNREACH;
  548. break;
  549. case ICMP_REDIRECT:
  550. sctp_icmp_redirect(sk, transport, skb);
  551. /* Fall through to out_unlock. */
  552. default:
  553. goto out_unlock;
  554. }
  555. inet = inet_sk(sk);
  556. if (!sock_owned_by_user(sk) && inet->recverr) {
  557. sk->sk_err = err;
  558. sk->sk_error_report(sk);
  559. } else { /* Only an error on timeout */
  560. sk->sk_err_soft = err;
  561. }
  562. out_unlock:
  563. sctp_err_finish(sk, asoc);
  564. }
  565. /*
  566. * RFC 2960, 8.4 - Handle "Out of the blue" Packets.
  567. *
  568. * This function scans all the chunks in the OOTB packet to determine if
  569. * the packet should be discarded right away. If a response might be needed
  570. * for this packet, or, if further processing is possible, the packet will
  571. * be queued to a proper inqueue for the next phase of handling.
  572. *
  573. * Output:
  574. * Return 0 - If further processing is needed.
  575. * Return 1 - If the packet can be discarded right away.
  576. */
  577. static int sctp_rcv_ootb(struct sk_buff *skb)
  578. {
  579. sctp_chunkhdr_t *ch, _ch;
  580. int ch_end, offset = 0;
  581. /* Scan through all the chunks in the packet. */
  582. do {
  583. /* Make sure we have at least the header there */
  584. if (offset + sizeof(sctp_chunkhdr_t) > skb->len)
  585. break;
  586. ch = skb_header_pointer(skb, offset, sizeof(*ch), &_ch);
  587. /* Break out if chunk length is less then minimal. */
  588. if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
  589. break;
  590. ch_end = offset + WORD_ROUND(ntohs(ch->length));
  591. if (ch_end > skb->len)
  592. break;
  593. /* RFC 8.4, 2) If the OOTB packet contains an ABORT chunk, the
  594. * receiver MUST silently discard the OOTB packet and take no
  595. * further action.
  596. */
  597. if (SCTP_CID_ABORT == ch->type)
  598. goto discard;
  599. /* RFC 8.4, 6) If the packet contains a SHUTDOWN COMPLETE
  600. * chunk, the receiver should silently discard the packet
  601. * and take no further action.
  602. */
  603. if (SCTP_CID_SHUTDOWN_COMPLETE == ch->type)
  604. goto discard;
  605. /* RFC 4460, 2.11.2
  606. * This will discard packets with INIT chunk bundled as
  607. * subsequent chunks in the packet. When INIT is first,
  608. * the normal INIT processing will discard the chunk.
  609. */
  610. if (SCTP_CID_INIT == ch->type && (void *)ch != skb->data)
  611. goto discard;
  612. offset = ch_end;
  613. } while (ch_end < skb->len);
  614. return 0;
  615. discard:
  616. return 1;
  617. }
  618. /* Insert endpoint into the hash table. */
  619. static void __sctp_hash_endpoint(struct sctp_endpoint *ep)
  620. {
  621. struct net *net = sock_net(ep->base.sk);
  622. struct sctp_ep_common *epb;
  623. struct sctp_hashbucket *head;
  624. epb = &ep->base;
  625. epb->hashent = sctp_ep_hashfn(net, epb->bind_addr.port);
  626. head = &sctp_ep_hashtable[epb->hashent];
  627. write_lock(&head->lock);
  628. hlist_add_head(&epb->node, &head->chain);
  629. write_unlock(&head->lock);
  630. }
  631. /* Add an endpoint to the hash. Local BH-safe. */
  632. void sctp_hash_endpoint(struct sctp_endpoint *ep)
  633. {
  634. local_bh_disable();
  635. __sctp_hash_endpoint(ep);
  636. local_bh_enable();
  637. }
  638. /* Remove endpoint from the hash table. */
  639. static void __sctp_unhash_endpoint(struct sctp_endpoint *ep)
  640. {
  641. struct net *net = sock_net(ep->base.sk);
  642. struct sctp_hashbucket *head;
  643. struct sctp_ep_common *epb;
  644. epb = &ep->base;
  645. epb->hashent = sctp_ep_hashfn(net, epb->bind_addr.port);
  646. head = &sctp_ep_hashtable[epb->hashent];
  647. write_lock(&head->lock);
  648. hlist_del_init(&epb->node);
  649. write_unlock(&head->lock);
  650. }
  651. /* Remove endpoint from the hash. Local BH-safe. */
  652. void sctp_unhash_endpoint(struct sctp_endpoint *ep)
  653. {
  654. local_bh_disable();
  655. __sctp_unhash_endpoint(ep);
  656. local_bh_enable();
  657. }
  658. /* Look up an endpoint. */
  659. static struct sctp_endpoint *__sctp_rcv_lookup_endpoint(struct net *net,
  660. const union sctp_addr *laddr)
  661. {
  662. struct sctp_hashbucket *head;
  663. struct sctp_ep_common *epb;
  664. struct sctp_endpoint *ep;
  665. int hash;
  666. hash = sctp_ep_hashfn(net, ntohs(laddr->v4.sin_port));
  667. head = &sctp_ep_hashtable[hash];
  668. read_lock(&head->lock);
  669. sctp_for_each_hentry(epb, &head->chain) {
  670. ep = sctp_ep(epb);
  671. if (sctp_endpoint_is_match(ep, net, laddr))
  672. goto hit;
  673. }
  674. ep = sctp_sk(net->sctp.ctl_sock)->ep;
  675. hit:
  676. sctp_endpoint_hold(ep);
  677. read_unlock(&head->lock);
  678. return ep;
  679. }
  680. /* rhashtable for transport */
  681. struct sctp_hash_cmp_arg {
  682. const struct sctp_endpoint *ep;
  683. const union sctp_addr *laddr;
  684. const union sctp_addr *paddr;
  685. const struct net *net;
  686. };
  687. static inline int sctp_hash_cmp(struct rhashtable_compare_arg *arg,
  688. const void *ptr)
  689. {
  690. const struct sctp_hash_cmp_arg *x = arg->key;
  691. const struct sctp_transport *t = ptr;
  692. struct sctp_association *asoc = t->asoc;
  693. const struct net *net = x->net;
  694. if (!sctp_cmp_addr_exact(&t->ipaddr, x->paddr))
  695. return 1;
  696. if (!net_eq(sock_net(asoc->base.sk), net))
  697. return 1;
  698. if (x->ep) {
  699. if (x->ep != asoc->ep)
  700. return 1;
  701. } else {
  702. if (x->laddr->v4.sin_port != htons(asoc->base.bind_addr.port))
  703. return 1;
  704. if (!sctp_bind_addr_match(&asoc->base.bind_addr,
  705. x->laddr, sctp_sk(asoc->base.sk)))
  706. return 1;
  707. }
  708. return 0;
  709. }
  710. static inline u32 sctp_hash_obj(const void *data, u32 len, u32 seed)
  711. {
  712. const struct sctp_transport *t = data;
  713. const union sctp_addr *paddr = &t->ipaddr;
  714. const struct net *net = sock_net(t->asoc->base.sk);
  715. u16 lport = htons(t->asoc->base.bind_addr.port);
  716. u32 addr;
  717. if (paddr->sa.sa_family == AF_INET6)
  718. addr = jhash(&paddr->v6.sin6_addr, 16, seed);
  719. else
  720. addr = paddr->v4.sin_addr.s_addr;
  721. return jhash_3words(addr, ((__u32)paddr->v4.sin_port) << 16 |
  722. (__force __u32)lport, net_hash_mix(net), seed);
  723. }
  724. static inline u32 sctp_hash_key(const void *data, u32 len, u32 seed)
  725. {
  726. const struct sctp_hash_cmp_arg *x = data;
  727. const union sctp_addr *paddr = x->paddr;
  728. const struct net *net = x->net;
  729. u16 lport;
  730. u32 addr;
  731. lport = x->ep ? htons(x->ep->base.bind_addr.port) :
  732. x->laddr->v4.sin_port;
  733. if (paddr->sa.sa_family == AF_INET6)
  734. addr = jhash(&paddr->v6.sin6_addr, 16, seed);
  735. else
  736. addr = paddr->v4.sin_addr.s_addr;
  737. return jhash_3words(addr, ((__u32)paddr->v4.sin_port) << 16 |
  738. (__force __u32)lport, net_hash_mix(net), seed);
  739. }
  740. static const struct rhashtable_params sctp_hash_params = {
  741. .head_offset = offsetof(struct sctp_transport, node),
  742. .hashfn = sctp_hash_key,
  743. .obj_hashfn = sctp_hash_obj,
  744. .obj_cmpfn = sctp_hash_cmp,
  745. .automatic_shrinking = true,
  746. };
  747. int sctp_transport_hashtable_init(void)
  748. {
  749. return rhashtable_init(&sctp_transport_hashtable, &sctp_hash_params);
  750. }
  751. void sctp_transport_hashtable_destroy(void)
  752. {
  753. rhashtable_destroy(&sctp_transport_hashtable);
  754. }
  755. void sctp_hash_transport(struct sctp_transport *t)
  756. {
  757. struct sctp_hash_cmp_arg arg;
  758. if (t->asoc->temp)
  759. return;
  760. arg.ep = t->asoc->ep;
  761. arg.paddr = &t->ipaddr;
  762. arg.net = sock_net(t->asoc->base.sk);
  763. reinsert:
  764. if (rhashtable_lookup_insert_key(&sctp_transport_hashtable, &arg,
  765. &t->node, sctp_hash_params) == -EBUSY)
  766. goto reinsert;
  767. }
  768. void sctp_unhash_transport(struct sctp_transport *t)
  769. {
  770. if (t->asoc->temp)
  771. return;
  772. rhashtable_remove_fast(&sctp_transport_hashtable, &t->node,
  773. sctp_hash_params);
  774. }
  775. struct sctp_transport *sctp_addrs_lookup_transport(
  776. struct net *net,
  777. const union sctp_addr *laddr,
  778. const union sctp_addr *paddr)
  779. {
  780. struct sctp_hash_cmp_arg arg = {
  781. .ep = NULL,
  782. .laddr = laddr,
  783. .paddr = paddr,
  784. .net = net,
  785. };
  786. return rhashtable_lookup_fast(&sctp_transport_hashtable, &arg,
  787. sctp_hash_params);
  788. }
  789. struct sctp_transport *sctp_epaddr_lookup_transport(
  790. const struct sctp_endpoint *ep,
  791. const union sctp_addr *paddr)
  792. {
  793. struct net *net = sock_net(ep->base.sk);
  794. struct sctp_hash_cmp_arg arg = {
  795. .ep = ep,
  796. .paddr = paddr,
  797. .net = net,
  798. };
  799. return rhashtable_lookup_fast(&sctp_transport_hashtable, &arg,
  800. sctp_hash_params);
  801. }
  802. /* Look up an association. */
  803. static struct sctp_association *__sctp_lookup_association(
  804. struct net *net,
  805. const union sctp_addr *local,
  806. const union sctp_addr *peer,
  807. struct sctp_transport **pt)
  808. {
  809. struct sctp_transport *t;
  810. struct sctp_association *asoc = NULL;
  811. t = sctp_addrs_lookup_transport(net, local, peer);
  812. if (!t || !sctp_transport_hold(t))
  813. goto out;
  814. asoc = t->asoc;
  815. sctp_association_hold(asoc);
  816. *pt = t;
  817. sctp_transport_put(t);
  818. out:
  819. return asoc;
  820. }
  821. /* Look up an association. protected by RCU read lock */
  822. static
  823. struct sctp_association *sctp_lookup_association(struct net *net,
  824. const union sctp_addr *laddr,
  825. const union sctp_addr *paddr,
  826. struct sctp_transport **transportp)
  827. {
  828. struct sctp_association *asoc;
  829. rcu_read_lock();
  830. asoc = __sctp_lookup_association(net, laddr, paddr, transportp);
  831. rcu_read_unlock();
  832. return asoc;
  833. }
  834. /* Is there an association matching the given local and peer addresses? */
  835. int sctp_has_association(struct net *net,
  836. const union sctp_addr *laddr,
  837. const union sctp_addr *paddr)
  838. {
  839. struct sctp_association *asoc;
  840. struct sctp_transport *transport;
  841. if ((asoc = sctp_lookup_association(net, laddr, paddr, &transport))) {
  842. sctp_association_put(asoc);
  843. return 1;
  844. }
  845. return 0;
  846. }
  847. /*
  848. * SCTP Implementors Guide, 2.18 Handling of address
  849. * parameters within the INIT or INIT-ACK.
  850. *
  851. * D) When searching for a matching TCB upon reception of an INIT
  852. * or INIT-ACK chunk the receiver SHOULD use not only the
  853. * source address of the packet (containing the INIT or
  854. * INIT-ACK) but the receiver SHOULD also use all valid
  855. * address parameters contained within the chunk.
  856. *
  857. * 2.18.3 Solution description
  858. *
  859. * This new text clearly specifies to an implementor the need
  860. * to look within the INIT or INIT-ACK. Any implementation that
  861. * does not do this, may not be able to establish associations
  862. * in certain circumstances.
  863. *
  864. */
  865. static struct sctp_association *__sctp_rcv_init_lookup(struct net *net,
  866. struct sk_buff *skb,
  867. const union sctp_addr *laddr, struct sctp_transport **transportp)
  868. {
  869. struct sctp_association *asoc;
  870. union sctp_addr addr;
  871. union sctp_addr *paddr = &addr;
  872. struct sctphdr *sh = sctp_hdr(skb);
  873. union sctp_params params;
  874. sctp_init_chunk_t *init;
  875. struct sctp_transport *transport;
  876. struct sctp_af *af;
  877. /*
  878. * This code will NOT touch anything inside the chunk--it is
  879. * strictly READ-ONLY.
  880. *
  881. * RFC 2960 3 SCTP packet Format
  882. *
  883. * Multiple chunks can be bundled into one SCTP packet up to
  884. * the MTU size, except for the INIT, INIT ACK, and SHUTDOWN
  885. * COMPLETE chunks. These chunks MUST NOT be bundled with any
  886. * other chunk in a packet. See Section 6.10 for more details
  887. * on chunk bundling.
  888. */
  889. /* Find the start of the TLVs and the end of the chunk. This is
  890. * the region we search for address parameters.
  891. */
  892. init = (sctp_init_chunk_t *)skb->data;
  893. /* Walk the parameters looking for embedded addresses. */
  894. sctp_walk_params(params, init, init_hdr.params) {
  895. /* Note: Ignoring hostname addresses. */
  896. af = sctp_get_af_specific(param_type2af(params.p->type));
  897. if (!af)
  898. continue;
  899. af->from_addr_param(paddr, params.addr, sh->source, 0);
  900. asoc = __sctp_lookup_association(net, laddr, paddr, &transport);
  901. if (asoc)
  902. return asoc;
  903. }
  904. return NULL;
  905. }
  906. /* ADD-IP, Section 5.2
  907. * When an endpoint receives an ASCONF Chunk from the remote peer
  908. * special procedures may be needed to identify the association the
  909. * ASCONF Chunk is associated with. To properly find the association
  910. * the following procedures SHOULD be followed:
  911. *
  912. * D2) If the association is not found, use the address found in the
  913. * Address Parameter TLV combined with the port number found in the
  914. * SCTP common header. If found proceed to rule D4.
  915. *
  916. * D2-ext) If more than one ASCONF Chunks are packed together, use the
  917. * address found in the ASCONF Address Parameter TLV of each of the
  918. * subsequent ASCONF Chunks. If found, proceed to rule D4.
  919. */
  920. static struct sctp_association *__sctp_rcv_asconf_lookup(
  921. struct net *net,
  922. sctp_chunkhdr_t *ch,
  923. const union sctp_addr *laddr,
  924. __be16 peer_port,
  925. struct sctp_transport **transportp)
  926. {
  927. sctp_addip_chunk_t *asconf = (struct sctp_addip_chunk *)ch;
  928. struct sctp_af *af;
  929. union sctp_addr_param *param;
  930. union sctp_addr paddr;
  931. /* Skip over the ADDIP header and find the Address parameter */
  932. param = (union sctp_addr_param *)(asconf + 1);
  933. af = sctp_get_af_specific(param_type2af(param->p.type));
  934. if (unlikely(!af))
  935. return NULL;
  936. af->from_addr_param(&paddr, param, peer_port, 0);
  937. return __sctp_lookup_association(net, laddr, &paddr, transportp);
  938. }
  939. /* SCTP-AUTH, Section 6.3:
  940. * If the receiver does not find a STCB for a packet containing an AUTH
  941. * chunk as the first chunk and not a COOKIE-ECHO chunk as the second
  942. * chunk, it MUST use the chunks after the AUTH chunk to look up an existing
  943. * association.
  944. *
  945. * This means that any chunks that can help us identify the association need
  946. * to be looked at to find this association.
  947. */
  948. static struct sctp_association *__sctp_rcv_walk_lookup(struct net *net,
  949. struct sk_buff *skb,
  950. const union sctp_addr *laddr,
  951. struct sctp_transport **transportp)
  952. {
  953. struct sctp_association *asoc = NULL;
  954. sctp_chunkhdr_t *ch;
  955. int have_auth = 0;
  956. unsigned int chunk_num = 1;
  957. __u8 *ch_end;
  958. /* Walk through the chunks looking for AUTH or ASCONF chunks
  959. * to help us find the association.
  960. */
  961. ch = (sctp_chunkhdr_t *) skb->data;
  962. do {
  963. /* Break out if chunk length is less then minimal. */
  964. if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
  965. break;
  966. ch_end = ((__u8 *)ch) + WORD_ROUND(ntohs(ch->length));
  967. if (ch_end > skb_tail_pointer(skb))
  968. break;
  969. switch (ch->type) {
  970. case SCTP_CID_AUTH:
  971. have_auth = chunk_num;
  972. break;
  973. case SCTP_CID_COOKIE_ECHO:
  974. /* If a packet arrives containing an AUTH chunk as
  975. * a first chunk, a COOKIE-ECHO chunk as the second
  976. * chunk, and possibly more chunks after them, and
  977. * the receiver does not have an STCB for that
  978. * packet, then authentication is based on
  979. * the contents of the COOKIE- ECHO chunk.
  980. */
  981. if (have_auth == 1 && chunk_num == 2)
  982. return NULL;
  983. break;
  984. case SCTP_CID_ASCONF:
  985. if (have_auth || net->sctp.addip_noauth)
  986. asoc = __sctp_rcv_asconf_lookup(
  987. net, ch, laddr,
  988. sctp_hdr(skb)->source,
  989. transportp);
  990. default:
  991. break;
  992. }
  993. if (asoc)
  994. break;
  995. ch = (sctp_chunkhdr_t *) ch_end;
  996. chunk_num++;
  997. } while (ch_end < skb_tail_pointer(skb));
  998. return asoc;
  999. }
  1000. /*
  1001. * There are circumstances when we need to look inside the SCTP packet
  1002. * for information to help us find the association. Examples
  1003. * include looking inside of INIT/INIT-ACK chunks or after the AUTH
  1004. * chunks.
  1005. */
  1006. static struct sctp_association *__sctp_rcv_lookup_harder(struct net *net,
  1007. struct sk_buff *skb,
  1008. const union sctp_addr *laddr,
  1009. struct sctp_transport **transportp)
  1010. {
  1011. sctp_chunkhdr_t *ch;
  1012. /* We do not allow GSO frames here as we need to linearize and
  1013. * then cannot guarantee frame boundaries. This shouldn't be an
  1014. * issue as packets hitting this are mostly INIT or INIT-ACK and
  1015. * those cannot be on GSO-style anyway.
  1016. */
  1017. if ((skb_shinfo(skb)->gso_type & SKB_GSO_SCTP) == SKB_GSO_SCTP)
  1018. return NULL;
  1019. ch = (sctp_chunkhdr_t *) skb->data;
  1020. /* The code below will attempt to walk the chunk and extract
  1021. * parameter information. Before we do that, we need to verify
  1022. * that the chunk length doesn't cause overflow. Otherwise, we'll
  1023. * walk off the end.
  1024. */
  1025. if (WORD_ROUND(ntohs(ch->length)) > skb->len)
  1026. return NULL;
  1027. /* If this is INIT/INIT-ACK look inside the chunk too. */
  1028. if (ch->type == SCTP_CID_INIT || ch->type == SCTP_CID_INIT_ACK)
  1029. return __sctp_rcv_init_lookup(net, skb, laddr, transportp);
  1030. return __sctp_rcv_walk_lookup(net, skb, laddr, transportp);
  1031. }
  1032. /* Lookup an association for an inbound skb. */
  1033. static struct sctp_association *__sctp_rcv_lookup(struct net *net,
  1034. struct sk_buff *skb,
  1035. const union sctp_addr *paddr,
  1036. const union sctp_addr *laddr,
  1037. struct sctp_transport **transportp)
  1038. {
  1039. struct sctp_association *asoc;
  1040. asoc = __sctp_lookup_association(net, laddr, paddr, transportp);
  1041. /* Further lookup for INIT/INIT-ACK packets.
  1042. * SCTP Implementors Guide, 2.18 Handling of address
  1043. * parameters within the INIT or INIT-ACK.
  1044. */
  1045. if (!asoc)
  1046. asoc = __sctp_rcv_lookup_harder(net, skb, laddr, transportp);
  1047. return asoc;
  1048. }