input.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174
  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 *laddr,
  70. const union sctp_addr *paddr,
  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 (timer_pending(&t->proto_unreach_timer) &&
  407. del_timer(&t->proto_unreach_timer))
  408. sctp_association_put(asoc);
  409. sctp_do_sm(net, SCTP_EVENT_T_OTHER,
  410. SCTP_ST_OTHER(SCTP_EVENT_ICMP_PROTO_UNREACH),
  411. asoc->state, asoc->ep, asoc, t,
  412. GFP_ATOMIC);
  413. }
  414. }
  415. /* Common lookup code for icmp/icmpv6 error handler. */
  416. struct sock *sctp_err_lookup(struct net *net, int family, struct sk_buff *skb,
  417. struct sctphdr *sctphdr,
  418. struct sctp_association **app,
  419. struct sctp_transport **tpp)
  420. {
  421. union sctp_addr saddr;
  422. union sctp_addr daddr;
  423. struct sctp_af *af;
  424. struct sock *sk = NULL;
  425. struct sctp_association *asoc;
  426. struct sctp_transport *transport = NULL;
  427. struct sctp_init_chunk *chunkhdr;
  428. __u32 vtag = ntohl(sctphdr->vtag);
  429. int len = skb->len - ((void *)sctphdr - (void *)skb->data);
  430. *app = NULL; *tpp = NULL;
  431. af = sctp_get_af_specific(family);
  432. if (unlikely(!af)) {
  433. return NULL;
  434. }
  435. /* Initialize local addresses for lookups. */
  436. af->from_skb(&saddr, skb, 1);
  437. af->from_skb(&daddr, skb, 0);
  438. /* Look for an association that matches the incoming ICMP error
  439. * packet.
  440. */
  441. asoc = __sctp_lookup_association(net, &saddr, &daddr, &transport);
  442. if (!asoc)
  443. return NULL;
  444. sk = asoc->base.sk;
  445. /* RFC 4960, Appendix C. ICMP Handling
  446. *
  447. * ICMP6) An implementation MUST validate that the Verification Tag
  448. * contained in the ICMP message matches the Verification Tag of
  449. * the peer. If the Verification Tag is not 0 and does NOT
  450. * match, discard the ICMP message. If it is 0 and the ICMP
  451. * message contains enough bytes to verify that the chunk type is
  452. * an INIT chunk and that the Initiate Tag matches the tag of the
  453. * peer, continue with ICMP7. If the ICMP message is too short
  454. * or the chunk type or the Initiate Tag does not match, silently
  455. * discard the packet.
  456. */
  457. if (vtag == 0) {
  458. chunkhdr = (void *)sctphdr + sizeof(struct sctphdr);
  459. if (len < sizeof(struct sctphdr) + sizeof(sctp_chunkhdr_t)
  460. + sizeof(__be32) ||
  461. chunkhdr->chunk_hdr.type != SCTP_CID_INIT ||
  462. ntohl(chunkhdr->init_hdr.init_tag) != asoc->c.my_vtag) {
  463. goto out;
  464. }
  465. } else if (vtag != asoc->c.peer_vtag) {
  466. goto out;
  467. }
  468. sctp_bh_lock_sock(sk);
  469. /* If too many ICMPs get dropped on busy
  470. * servers this needs to be solved differently.
  471. */
  472. if (sock_owned_by_user(sk))
  473. NET_INC_STATS_BH(net, LINUX_MIB_LOCKDROPPEDICMPS);
  474. *app = asoc;
  475. *tpp = transport;
  476. return sk;
  477. out:
  478. if (asoc)
  479. sctp_association_put(asoc);
  480. return NULL;
  481. }
  482. /* Common cleanup code for icmp/icmpv6 error handler. */
  483. void sctp_err_finish(struct sock *sk, struct sctp_association *asoc)
  484. {
  485. sctp_bh_unlock_sock(sk);
  486. if (asoc)
  487. sctp_association_put(asoc);
  488. }
  489. /*
  490. * This routine is called by the ICMP module when it gets some
  491. * sort of error condition. If err < 0 then the socket should
  492. * be closed and the error returned to the user. If err > 0
  493. * it's just the icmp type << 8 | icmp code. After adjustment
  494. * header points to the first 8 bytes of the sctp header. We need
  495. * to find the appropriate port.
  496. *
  497. * The locking strategy used here is very "optimistic". When
  498. * someone else accesses the socket the ICMP is just dropped
  499. * and for some paths there is no check at all.
  500. * A more general error queue to queue errors for later handling
  501. * is probably better.
  502. *
  503. */
  504. void sctp_v4_err(struct sk_buff *skb, __u32 info)
  505. {
  506. const struct iphdr *iph = (const struct iphdr *)skb->data;
  507. const int ihlen = iph->ihl * 4;
  508. const int type = icmp_hdr(skb)->type;
  509. const int code = icmp_hdr(skb)->code;
  510. struct sock *sk;
  511. struct sctp_association *asoc = NULL;
  512. struct sctp_transport *transport;
  513. struct inet_sock *inet;
  514. sk_buff_data_t saveip, savesctp;
  515. int err;
  516. struct net *net = dev_net(skb->dev);
  517. if (skb->len < ihlen + 8) {
  518. ICMP_INC_STATS_BH(net, ICMP_MIB_INERRORS);
  519. return;
  520. }
  521. /* Fix up skb to look at the embedded net header. */
  522. saveip = skb->network_header;
  523. savesctp = skb->transport_header;
  524. skb_reset_network_header(skb);
  525. skb_set_transport_header(skb, ihlen);
  526. sk = sctp_err_lookup(net, AF_INET, skb, sctp_hdr(skb), &asoc, &transport);
  527. /* Put back, the original values. */
  528. skb->network_header = saveip;
  529. skb->transport_header = savesctp;
  530. if (!sk) {
  531. ICMP_INC_STATS_BH(net, ICMP_MIB_INERRORS);
  532. return;
  533. }
  534. /* Warning: The sock lock is held. Remember to call
  535. * sctp_err_finish!
  536. */
  537. switch (type) {
  538. case ICMP_PARAMETERPROB:
  539. err = EPROTO;
  540. break;
  541. case ICMP_DEST_UNREACH:
  542. if (code > NR_ICMP_UNREACH)
  543. goto out_unlock;
  544. /* PMTU discovery (RFC1191) */
  545. if (ICMP_FRAG_NEEDED == code) {
  546. sctp_icmp_frag_needed(sk, asoc, transport, info);
  547. goto out_unlock;
  548. }
  549. else {
  550. if (ICMP_PROT_UNREACH == code) {
  551. sctp_icmp_proto_unreachable(sk, asoc,
  552. transport);
  553. goto out_unlock;
  554. }
  555. }
  556. err = icmp_err_convert[code].errno;
  557. break;
  558. case ICMP_TIME_EXCEEDED:
  559. /* Ignore any time exceeded errors due to fragment reassembly
  560. * timeouts.
  561. */
  562. if (ICMP_EXC_FRAGTIME == code)
  563. goto out_unlock;
  564. err = EHOSTUNREACH;
  565. break;
  566. case ICMP_REDIRECT:
  567. sctp_icmp_redirect(sk, transport, skb);
  568. err = 0;
  569. break;
  570. default:
  571. goto out_unlock;
  572. }
  573. inet = inet_sk(sk);
  574. if (!sock_owned_by_user(sk) && inet->recverr) {
  575. sk->sk_err = err;
  576. sk->sk_error_report(sk);
  577. } else { /* Only an error on timeout */
  578. sk->sk_err_soft = err;
  579. }
  580. out_unlock:
  581. sctp_err_finish(sk, asoc);
  582. }
  583. /*
  584. * RFC 2960, 8.4 - Handle "Out of the blue" Packets.
  585. *
  586. * This function scans all the chunks in the OOTB packet to determine if
  587. * the packet should be discarded right away. If a response might be needed
  588. * for this packet, or, if further processing is possible, the packet will
  589. * be queued to a proper inqueue for the next phase of handling.
  590. *
  591. * Output:
  592. * Return 0 - If further processing is needed.
  593. * Return 1 - If the packet can be discarded right away.
  594. */
  595. static int sctp_rcv_ootb(struct sk_buff *skb)
  596. {
  597. sctp_chunkhdr_t *ch;
  598. __u8 *ch_end;
  599. ch = (sctp_chunkhdr_t *) skb->data;
  600. /* Scan through all the chunks in the packet. */
  601. do {
  602. /* Break out if chunk length is less then minimal. */
  603. if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
  604. break;
  605. ch_end = ((__u8 *)ch) + WORD_ROUND(ntohs(ch->length));
  606. if (ch_end > skb_tail_pointer(skb))
  607. break;
  608. /* RFC 8.4, 2) If the OOTB packet contains an ABORT chunk, the
  609. * receiver MUST silently discard the OOTB packet and take no
  610. * further action.
  611. */
  612. if (SCTP_CID_ABORT == ch->type)
  613. goto discard;
  614. /* RFC 8.4, 6) If the packet contains a SHUTDOWN COMPLETE
  615. * chunk, the receiver should silently discard the packet
  616. * and take no further action.
  617. */
  618. if (SCTP_CID_SHUTDOWN_COMPLETE == ch->type)
  619. goto discard;
  620. /* RFC 4460, 2.11.2
  621. * This will discard packets with INIT chunk bundled as
  622. * subsequent chunks in the packet. When INIT is first,
  623. * the normal INIT processing will discard the chunk.
  624. */
  625. if (SCTP_CID_INIT == ch->type && (void *)ch != skb->data)
  626. goto discard;
  627. ch = (sctp_chunkhdr_t *) ch_end;
  628. } while (ch_end < skb_tail_pointer(skb));
  629. return 0;
  630. discard:
  631. return 1;
  632. }
  633. /* Insert endpoint into the hash table. */
  634. static void __sctp_hash_endpoint(struct sctp_endpoint *ep)
  635. {
  636. struct net *net = sock_net(ep->base.sk);
  637. struct sctp_ep_common *epb;
  638. struct sctp_hashbucket *head;
  639. epb = &ep->base;
  640. epb->hashent = sctp_ep_hashfn(net, epb->bind_addr.port);
  641. head = &sctp_ep_hashtable[epb->hashent];
  642. sctp_write_lock(&head->lock);
  643. hlist_add_head(&epb->node, &head->chain);
  644. sctp_write_unlock(&head->lock);
  645. }
  646. /* Add an endpoint to the hash. Local BH-safe. */
  647. void sctp_hash_endpoint(struct sctp_endpoint *ep)
  648. {
  649. sctp_local_bh_disable();
  650. __sctp_hash_endpoint(ep);
  651. sctp_local_bh_enable();
  652. }
  653. /* Remove endpoint from the hash table. */
  654. static void __sctp_unhash_endpoint(struct sctp_endpoint *ep)
  655. {
  656. struct net *net = sock_net(ep->base.sk);
  657. struct sctp_hashbucket *head;
  658. struct sctp_ep_common *epb;
  659. epb = &ep->base;
  660. epb->hashent = sctp_ep_hashfn(net, epb->bind_addr.port);
  661. head = &sctp_ep_hashtable[epb->hashent];
  662. sctp_write_lock(&head->lock);
  663. hlist_del_init(&epb->node);
  664. sctp_write_unlock(&head->lock);
  665. }
  666. /* Remove endpoint from the hash. Local BH-safe. */
  667. void sctp_unhash_endpoint(struct sctp_endpoint *ep)
  668. {
  669. sctp_local_bh_disable();
  670. __sctp_unhash_endpoint(ep);
  671. sctp_local_bh_enable();
  672. }
  673. /* Look up an endpoint. */
  674. static struct sctp_endpoint *__sctp_rcv_lookup_endpoint(struct net *net,
  675. const union sctp_addr *laddr)
  676. {
  677. struct sctp_hashbucket *head;
  678. struct sctp_ep_common *epb;
  679. struct sctp_endpoint *ep;
  680. struct hlist_node *node;
  681. int hash;
  682. hash = sctp_ep_hashfn(net, ntohs(laddr->v4.sin_port));
  683. head = &sctp_ep_hashtable[hash];
  684. read_lock(&head->lock);
  685. sctp_for_each_hentry(epb, node, &head->chain) {
  686. ep = sctp_ep(epb);
  687. if (sctp_endpoint_is_match(ep, net, laddr))
  688. goto hit;
  689. }
  690. ep = sctp_sk(net->sctp.ctl_sock)->ep;
  691. hit:
  692. sctp_endpoint_hold(ep);
  693. read_unlock(&head->lock);
  694. return ep;
  695. }
  696. /* Insert association into the hash table. */
  697. static void __sctp_hash_established(struct sctp_association *asoc)
  698. {
  699. struct net *net = sock_net(asoc->base.sk);
  700. struct sctp_ep_common *epb;
  701. struct sctp_hashbucket *head;
  702. epb = &asoc->base;
  703. /* Calculate which chain this entry will belong to. */
  704. epb->hashent = sctp_assoc_hashfn(net, epb->bind_addr.port,
  705. asoc->peer.port);
  706. head = &sctp_assoc_hashtable[epb->hashent];
  707. sctp_write_lock(&head->lock);
  708. hlist_add_head(&epb->node, &head->chain);
  709. sctp_write_unlock(&head->lock);
  710. }
  711. /* Add an association to the hash. Local BH-safe. */
  712. void sctp_hash_established(struct sctp_association *asoc)
  713. {
  714. if (asoc->temp)
  715. return;
  716. sctp_local_bh_disable();
  717. __sctp_hash_established(asoc);
  718. sctp_local_bh_enable();
  719. }
  720. /* Remove association from the hash table. */
  721. static void __sctp_unhash_established(struct sctp_association *asoc)
  722. {
  723. struct net *net = sock_net(asoc->base.sk);
  724. struct sctp_hashbucket *head;
  725. struct sctp_ep_common *epb;
  726. epb = &asoc->base;
  727. epb->hashent = sctp_assoc_hashfn(net, epb->bind_addr.port,
  728. asoc->peer.port);
  729. head = &sctp_assoc_hashtable[epb->hashent];
  730. sctp_write_lock(&head->lock);
  731. hlist_del_init(&epb->node);
  732. sctp_write_unlock(&head->lock);
  733. }
  734. /* Remove association from the hash table. Local BH-safe. */
  735. void sctp_unhash_established(struct sctp_association *asoc)
  736. {
  737. if (asoc->temp)
  738. return;
  739. sctp_local_bh_disable();
  740. __sctp_unhash_established(asoc);
  741. sctp_local_bh_enable();
  742. }
  743. /* Look up an association. */
  744. static struct sctp_association *__sctp_lookup_association(
  745. struct net *net,
  746. const union sctp_addr *local,
  747. const union sctp_addr *peer,
  748. struct sctp_transport **pt)
  749. {
  750. struct sctp_hashbucket *head;
  751. struct sctp_ep_common *epb;
  752. struct sctp_association *asoc;
  753. struct sctp_transport *transport;
  754. struct hlist_node *node;
  755. int hash;
  756. /* Optimize here for direct hit, only listening connections can
  757. * have wildcards anyways.
  758. */
  759. hash = sctp_assoc_hashfn(net, ntohs(local->v4.sin_port),
  760. ntohs(peer->v4.sin_port));
  761. head = &sctp_assoc_hashtable[hash];
  762. read_lock(&head->lock);
  763. sctp_for_each_hentry(epb, node, &head->chain) {
  764. asoc = sctp_assoc(epb);
  765. transport = sctp_assoc_is_match(asoc, net, local, peer);
  766. if (transport)
  767. goto hit;
  768. }
  769. read_unlock(&head->lock);
  770. return NULL;
  771. hit:
  772. *pt = transport;
  773. sctp_association_hold(asoc);
  774. read_unlock(&head->lock);
  775. return asoc;
  776. }
  777. /* Look up an association. BH-safe. */
  778. SCTP_STATIC
  779. struct sctp_association *sctp_lookup_association(struct net *net,
  780. const union sctp_addr *laddr,
  781. const union sctp_addr *paddr,
  782. struct sctp_transport **transportp)
  783. {
  784. struct sctp_association *asoc;
  785. sctp_local_bh_disable();
  786. asoc = __sctp_lookup_association(net, laddr, paddr, transportp);
  787. sctp_local_bh_enable();
  788. return asoc;
  789. }
  790. /* Is there an association matching the given local and peer addresses? */
  791. int sctp_has_association(struct net *net,
  792. const union sctp_addr *laddr,
  793. const union sctp_addr *paddr)
  794. {
  795. struct sctp_association *asoc;
  796. struct sctp_transport *transport;
  797. if ((asoc = sctp_lookup_association(net, laddr, paddr, &transport))) {
  798. sctp_association_put(asoc);
  799. return 1;
  800. }
  801. return 0;
  802. }
  803. /*
  804. * SCTP Implementors Guide, 2.18 Handling of address
  805. * parameters within the INIT or INIT-ACK.
  806. *
  807. * D) When searching for a matching TCB upon reception of an INIT
  808. * or INIT-ACK chunk the receiver SHOULD use not only the
  809. * source address of the packet (containing the INIT or
  810. * INIT-ACK) but the receiver SHOULD also use all valid
  811. * address parameters contained within the chunk.
  812. *
  813. * 2.18.3 Solution description
  814. *
  815. * This new text clearly specifies to an implementor the need
  816. * to look within the INIT or INIT-ACK. Any implementation that
  817. * does not do this, may not be able to establish associations
  818. * in certain circumstances.
  819. *
  820. */
  821. static struct sctp_association *__sctp_rcv_init_lookup(struct net *net,
  822. struct sk_buff *skb,
  823. const union sctp_addr *laddr, struct sctp_transport **transportp)
  824. {
  825. struct sctp_association *asoc;
  826. union sctp_addr addr;
  827. union sctp_addr *paddr = &addr;
  828. struct sctphdr *sh = sctp_hdr(skb);
  829. union sctp_params params;
  830. sctp_init_chunk_t *init;
  831. struct sctp_transport *transport;
  832. struct sctp_af *af;
  833. /*
  834. * This code will NOT touch anything inside the chunk--it is
  835. * strictly READ-ONLY.
  836. *
  837. * RFC 2960 3 SCTP packet Format
  838. *
  839. * Multiple chunks can be bundled into one SCTP packet up to
  840. * the MTU size, except for the INIT, INIT ACK, and SHUTDOWN
  841. * COMPLETE chunks. These chunks MUST NOT be bundled with any
  842. * other chunk in a packet. See Section 6.10 for more details
  843. * on chunk bundling.
  844. */
  845. /* Find the start of the TLVs and the end of the chunk. This is
  846. * the region we search for address parameters.
  847. */
  848. init = (sctp_init_chunk_t *)skb->data;
  849. /* Walk the parameters looking for embedded addresses. */
  850. sctp_walk_params(params, init, init_hdr.params) {
  851. /* Note: Ignoring hostname addresses. */
  852. af = sctp_get_af_specific(param_type2af(params.p->type));
  853. if (!af)
  854. continue;
  855. af->from_addr_param(paddr, params.addr, sh->source, 0);
  856. asoc = __sctp_lookup_association(net, laddr, paddr, &transport);
  857. if (asoc)
  858. return asoc;
  859. }
  860. return NULL;
  861. }
  862. /* ADD-IP, Section 5.2
  863. * When an endpoint receives an ASCONF Chunk from the remote peer
  864. * special procedures may be needed to identify the association the
  865. * ASCONF Chunk is associated with. To properly find the association
  866. * the following procedures SHOULD be followed:
  867. *
  868. * D2) If the association is not found, use the address found in the
  869. * Address Parameter TLV combined with the port number found in the
  870. * SCTP common header. If found proceed to rule D4.
  871. *
  872. * D2-ext) If more than one ASCONF Chunks are packed together, use the
  873. * address found in the ASCONF Address Parameter TLV of each of the
  874. * subsequent ASCONF Chunks. If found, proceed to rule D4.
  875. */
  876. static struct sctp_association *__sctp_rcv_asconf_lookup(
  877. struct net *net,
  878. sctp_chunkhdr_t *ch,
  879. const union sctp_addr *laddr,
  880. __be16 peer_port,
  881. struct sctp_transport **transportp)
  882. {
  883. sctp_addip_chunk_t *asconf = (struct sctp_addip_chunk *)ch;
  884. struct sctp_af *af;
  885. union sctp_addr_param *param;
  886. union sctp_addr paddr;
  887. /* Skip over the ADDIP header and find the Address parameter */
  888. param = (union sctp_addr_param *)(asconf + 1);
  889. af = sctp_get_af_specific(param_type2af(param->p.type));
  890. if (unlikely(!af))
  891. return NULL;
  892. af->from_addr_param(&paddr, param, peer_port, 0);
  893. return __sctp_lookup_association(net, laddr, &paddr, transportp);
  894. }
  895. /* SCTP-AUTH, Section 6.3:
  896. * If the receiver does not find a STCB for a packet containing an AUTH
  897. * chunk as the first chunk and not a COOKIE-ECHO chunk as the second
  898. * chunk, it MUST use the chunks after the AUTH chunk to look up an existing
  899. * association.
  900. *
  901. * This means that any chunks that can help us identify the association need
  902. * to be looked at to find this association.
  903. */
  904. static struct sctp_association *__sctp_rcv_walk_lookup(struct net *net,
  905. struct sk_buff *skb,
  906. const union sctp_addr *laddr,
  907. struct sctp_transport **transportp)
  908. {
  909. struct sctp_association *asoc = NULL;
  910. sctp_chunkhdr_t *ch;
  911. int have_auth = 0;
  912. unsigned int chunk_num = 1;
  913. __u8 *ch_end;
  914. /* Walk through the chunks looking for AUTH or ASCONF chunks
  915. * to help us find the association.
  916. */
  917. ch = (sctp_chunkhdr_t *) skb->data;
  918. do {
  919. /* Break out if chunk length is less then minimal. */
  920. if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
  921. break;
  922. ch_end = ((__u8 *)ch) + WORD_ROUND(ntohs(ch->length));
  923. if (ch_end > skb_tail_pointer(skb))
  924. break;
  925. switch(ch->type) {
  926. case SCTP_CID_AUTH:
  927. have_auth = chunk_num;
  928. break;
  929. case SCTP_CID_COOKIE_ECHO:
  930. /* If a packet arrives containing an AUTH chunk as
  931. * a first chunk, a COOKIE-ECHO chunk as the second
  932. * chunk, and possibly more chunks after them, and
  933. * the receiver does not have an STCB for that
  934. * packet, then authentication is based on
  935. * the contents of the COOKIE- ECHO chunk.
  936. */
  937. if (have_auth == 1 && chunk_num == 2)
  938. return NULL;
  939. break;
  940. case SCTP_CID_ASCONF:
  941. if (have_auth || net->sctp.addip_noauth)
  942. asoc = __sctp_rcv_asconf_lookup(
  943. net, ch, laddr,
  944. sctp_hdr(skb)->source,
  945. transportp);
  946. default:
  947. break;
  948. }
  949. if (asoc)
  950. break;
  951. ch = (sctp_chunkhdr_t *) ch_end;
  952. chunk_num++;
  953. } while (ch_end < skb_tail_pointer(skb));
  954. return asoc;
  955. }
  956. /*
  957. * There are circumstances when we need to look inside the SCTP packet
  958. * for information to help us find the association. Examples
  959. * include looking inside of INIT/INIT-ACK chunks or after the AUTH
  960. * chunks.
  961. */
  962. static struct sctp_association *__sctp_rcv_lookup_harder(struct net *net,
  963. struct sk_buff *skb,
  964. const union sctp_addr *laddr,
  965. struct sctp_transport **transportp)
  966. {
  967. sctp_chunkhdr_t *ch;
  968. ch = (sctp_chunkhdr_t *) skb->data;
  969. /* The code below will attempt to walk the chunk and extract
  970. * parameter information. Before we do that, we need to verify
  971. * that the chunk length doesn't cause overflow. Otherwise, we'll
  972. * walk off the end.
  973. */
  974. if (WORD_ROUND(ntohs(ch->length)) > skb->len)
  975. return NULL;
  976. /* If this is INIT/INIT-ACK look inside the chunk too. */
  977. switch (ch->type) {
  978. case SCTP_CID_INIT:
  979. case SCTP_CID_INIT_ACK:
  980. return __sctp_rcv_init_lookup(net, skb, laddr, transportp);
  981. break;
  982. default:
  983. return __sctp_rcv_walk_lookup(net, skb, laddr, transportp);
  984. break;
  985. }
  986. return NULL;
  987. }
  988. /* Lookup an association for an inbound skb. */
  989. static struct sctp_association *__sctp_rcv_lookup(struct net *net,
  990. struct sk_buff *skb,
  991. const union sctp_addr *paddr,
  992. const union sctp_addr *laddr,
  993. struct sctp_transport **transportp)
  994. {
  995. struct sctp_association *asoc;
  996. asoc = __sctp_lookup_association(net, laddr, paddr, transportp);
  997. /* Further lookup for INIT/INIT-ACK packets.
  998. * SCTP Implementors Guide, 2.18 Handling of address
  999. * parameters within the INIT or INIT-ACK.
  1000. */
  1001. if (!asoc)
  1002. asoc = __sctp_rcv_lookup_harder(net, skb, laddr, transportp);
  1003. return asoc;
  1004. }