input.c 36 KB

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