input.c 33 KB

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