input.c 32 KB

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