input.c 31 KB

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