input.c 32 KB

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