input.c 29 KB

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