input.c 31 KB

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