input.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  1. /* SCTP kernel reference 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 reference Implementation
  10. *
  11. * These functions handle all input from the IP layer into SCTP.
  12. *
  13. * The SCTP reference 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. * The SCTP reference 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. /* Forward declarations for internal helpers. */
  63. static int sctp_rcv_ootb(struct sk_buff *);
  64. static struct sctp_association *__sctp_rcv_lookup(struct sk_buff *skb,
  65. const union sctp_addr *laddr,
  66. const union sctp_addr *paddr,
  67. struct sctp_transport **transportp);
  68. static struct sctp_endpoint *__sctp_rcv_lookup_endpoint(const union sctp_addr *laddr);
  69. static struct sctp_association *__sctp_lookup_association(
  70. const union sctp_addr *local,
  71. const union sctp_addr *peer,
  72. struct sctp_transport **pt);
  73. static void sctp_add_backlog(struct sock *sk, struct sk_buff *skb);
  74. /* Calculate the SCTP checksum of an SCTP packet. */
  75. static inline int sctp_rcv_checksum(struct sk_buff *skb)
  76. {
  77. struct sctphdr *sh;
  78. __u32 cmp, val;
  79. struct sk_buff *list = skb_shinfo(skb)->frag_list;
  80. sh = (struct sctphdr *) skb->h.raw;
  81. cmp = ntohl(sh->checksum);
  82. val = sctp_start_cksum((__u8 *)sh, skb_headlen(skb));
  83. for (; list; list = list->next)
  84. val = sctp_update_cksum((__u8 *)list->data, skb_headlen(list),
  85. val);
  86. val = sctp_end_cksum(val);
  87. if (val != cmp) {
  88. /* CRC failure, dump it. */
  89. SCTP_INC_STATS_BH(SCTP_MIB_CHECKSUMERRORS);
  90. return -1;
  91. }
  92. return 0;
  93. }
  94. struct sctp_input_cb {
  95. union {
  96. struct inet_skb_parm h4;
  97. #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
  98. struct inet6_skb_parm h6;
  99. #endif
  100. } header;
  101. struct sctp_chunk *chunk;
  102. };
  103. #define SCTP_INPUT_CB(__skb) ((struct sctp_input_cb *)&((__skb)->cb[0]))
  104. /*
  105. * This is the routine which IP calls when receiving an SCTP packet.
  106. */
  107. int sctp_rcv(struct sk_buff *skb)
  108. {
  109. struct sock *sk;
  110. struct sctp_association *asoc;
  111. struct sctp_endpoint *ep = NULL;
  112. struct sctp_ep_common *rcvr;
  113. struct sctp_transport *transport = NULL;
  114. struct sctp_chunk *chunk;
  115. struct sctphdr *sh;
  116. union sctp_addr src;
  117. union sctp_addr dest;
  118. int family;
  119. struct sctp_af *af;
  120. if (skb->pkt_type!=PACKET_HOST)
  121. goto discard_it;
  122. SCTP_INC_STATS_BH(SCTP_MIB_INSCTPPACKS);
  123. sh = (struct sctphdr *) skb->h.raw;
  124. /* Pull up the IP and SCTP headers. */
  125. __skb_pull(skb, skb->h.raw - skb->data);
  126. if (skb->len < sizeof(struct sctphdr))
  127. goto discard_it;
  128. if ((skb->ip_summed != CHECKSUM_UNNECESSARY) &&
  129. (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(skb->nh.iph->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. /* SCTP seems to always need a timestamp right now (FIXME) */
  195. if (skb->tstamp.off_sec == 0) {
  196. __net_timestamp(skb);
  197. sock_enable_timestamp(sk);
  198. }
  199. if (!xfrm_policy_check(sk, XFRM_POLICY_IN, skb, family))
  200. goto discard_release;
  201. nf_reset(skb);
  202. if (sk_filter(sk, skb))
  203. goto discard_release;
  204. /* Create an SCTP packet structure. */
  205. chunk = sctp_chunkify(skb, asoc, sk);
  206. if (!chunk)
  207. goto discard_release;
  208. SCTP_INPUT_CB(skb)->chunk = chunk;
  209. /* Remember what endpoint is to handle this packet. */
  210. chunk->rcvr = rcvr;
  211. /* Remember the SCTP header. */
  212. chunk->sctp_hdr = sh;
  213. /* Set the source and destination addresses of the incoming chunk. */
  214. sctp_init_addrs(chunk, &src, &dest);
  215. /* Remember where we came from. */
  216. chunk->transport = transport;
  217. /* Acquire access to the sock lock. Note: We are safe from other
  218. * bottom halves on this lock, but a user may be in the lock too,
  219. * so check if it is busy.
  220. */
  221. sctp_bh_lock_sock(sk);
  222. if (sock_owned_by_user(sk)) {
  223. SCTP_INC_STATS_BH(SCTP_MIB_IN_PKT_BACKLOG);
  224. sctp_add_backlog(sk, skb);
  225. } else {
  226. SCTP_INC_STATS_BH(SCTP_MIB_IN_PKT_SOFTIRQ);
  227. sctp_inq_push(&chunk->rcvr->inqueue, chunk);
  228. }
  229. sctp_bh_unlock_sock(sk);
  230. /* Release the asoc/ep ref we took in the lookup calls. */
  231. if (asoc)
  232. sctp_association_put(asoc);
  233. else
  234. sctp_endpoint_put(ep);
  235. return 0;
  236. discard_it:
  237. SCTP_INC_STATS_BH(SCTP_MIB_IN_PKT_DISCARDS);
  238. kfree_skb(skb);
  239. return 0;
  240. discard_release:
  241. /* Release the asoc/ep ref we took in the lookup calls. */
  242. if (asoc)
  243. sctp_association_put(asoc);
  244. else
  245. sctp_endpoint_put(ep);
  246. goto discard_it;
  247. }
  248. /* Process the backlog queue of the socket. Every skb on
  249. * the backlog holds a ref on an association or endpoint.
  250. * We hold this ref throughout the state machine to make
  251. * sure that the structure we need is still around.
  252. */
  253. int sctp_backlog_rcv(struct sock *sk, struct sk_buff *skb)
  254. {
  255. struct sctp_chunk *chunk = SCTP_INPUT_CB(skb)->chunk;
  256. struct sctp_inq *inqueue = &chunk->rcvr->inqueue;
  257. struct sctp_ep_common *rcvr = NULL;
  258. int backloged = 0;
  259. rcvr = chunk->rcvr;
  260. /* If the rcvr is dead then the association or endpoint
  261. * has been deleted and we can safely drop the chunk
  262. * and refs that we are holding.
  263. */
  264. if (rcvr->dead) {
  265. sctp_chunk_free(chunk);
  266. goto done;
  267. }
  268. if (unlikely(rcvr->sk != sk)) {
  269. /* In this case, the association moved from one socket to
  270. * another. We are currently sitting on the backlog of the
  271. * old socket, so we need to move.
  272. * However, since we are here in the process context we
  273. * need to take make sure that the user doesn't own
  274. * the new socket when we process the packet.
  275. * If the new socket is user-owned, queue the chunk to the
  276. * backlog of the new socket without dropping any refs.
  277. * Otherwise, we can safely push the chunk on the inqueue.
  278. */
  279. sk = rcvr->sk;
  280. sctp_bh_lock_sock(sk);
  281. if (sock_owned_by_user(sk)) {
  282. sk_add_backlog(sk, skb);
  283. backloged = 1;
  284. } else
  285. sctp_inq_push(inqueue, chunk);
  286. sctp_bh_unlock_sock(sk);
  287. /* If the chunk was backloged again, don't drop refs */
  288. if (backloged)
  289. return 0;
  290. } else {
  291. sctp_inq_push(inqueue, chunk);
  292. }
  293. done:
  294. /* Release the refs we took in sctp_add_backlog */
  295. if (SCTP_EP_TYPE_ASSOCIATION == rcvr->type)
  296. sctp_association_put(sctp_assoc(rcvr));
  297. else if (SCTP_EP_TYPE_SOCKET == rcvr->type)
  298. sctp_endpoint_put(sctp_ep(rcvr));
  299. else
  300. BUG();
  301. return 0;
  302. }
  303. static void sctp_add_backlog(struct sock *sk, struct sk_buff *skb)
  304. {
  305. struct sctp_chunk *chunk = SCTP_INPUT_CB(skb)->chunk;
  306. struct sctp_ep_common *rcvr = chunk->rcvr;
  307. /* Hold the assoc/ep while hanging on the backlog queue.
  308. * This way, we know structures we need will not disappear from us
  309. */
  310. if (SCTP_EP_TYPE_ASSOCIATION == rcvr->type)
  311. sctp_association_hold(sctp_assoc(rcvr));
  312. else if (SCTP_EP_TYPE_SOCKET == rcvr->type)
  313. sctp_endpoint_hold(sctp_ep(rcvr));
  314. else
  315. BUG();
  316. sk_add_backlog(sk, skb);
  317. }
  318. /* Handle icmp frag needed error. */
  319. void sctp_icmp_frag_needed(struct sock *sk, struct sctp_association *asoc,
  320. struct sctp_transport *t, __u32 pmtu)
  321. {
  322. if (sock_owned_by_user(sk) || !t || (t->pathmtu == pmtu))
  323. return;
  324. if (t->param_flags & SPP_PMTUD_ENABLE) {
  325. if (unlikely(pmtu < SCTP_DEFAULT_MINSEGMENT)) {
  326. printk(KERN_WARNING "%s: Reported pmtu %d too low, "
  327. "using default minimum of %d\n",
  328. __FUNCTION__, pmtu,
  329. SCTP_DEFAULT_MINSEGMENT);
  330. /* Use default minimum segment size and disable
  331. * pmtu discovery on this transport.
  332. */
  333. t->pathmtu = SCTP_DEFAULT_MINSEGMENT;
  334. t->param_flags = (t->param_flags & ~SPP_HB) |
  335. SPP_PMTUD_DISABLE;
  336. } else {
  337. t->pathmtu = pmtu;
  338. }
  339. /* Update association pmtu. */
  340. sctp_assoc_sync_pmtu(asoc);
  341. }
  342. /* Retransmit with the new pmtu setting.
  343. * Normally, if PMTU discovery is disabled, an ICMP Fragmentation
  344. * Needed will never be sent, but if a message was sent before
  345. * PMTU discovery was disabled that was larger than the PMTU, it
  346. * would not be fragmented, so it must be re-transmitted fragmented.
  347. */
  348. sctp_retransmit(&asoc->outqueue, t, SCTP_RTXR_PMTUD);
  349. }
  350. /*
  351. * SCTP Implementer's Guide, 2.37 ICMP handling procedures
  352. *
  353. * ICMP8) If the ICMP code is a "Unrecognized next header type encountered"
  354. * or a "Protocol Unreachable" treat this message as an abort
  355. * with the T bit set.
  356. *
  357. * This function sends an event to the state machine, which will abort the
  358. * association.
  359. *
  360. */
  361. void sctp_icmp_proto_unreachable(struct sock *sk,
  362. struct sctp_association *asoc,
  363. struct sctp_transport *t)
  364. {
  365. SCTP_DEBUG_PRINTK("%s\n", __FUNCTION__);
  366. sctp_do_sm(SCTP_EVENT_T_OTHER,
  367. SCTP_ST_OTHER(SCTP_EVENT_ICMP_PROTO_UNREACH),
  368. asoc->state, asoc->ep, asoc, t,
  369. GFP_ATOMIC);
  370. }
  371. /* Common lookup code for icmp/icmpv6 error handler. */
  372. struct sock *sctp_err_lookup(int family, struct sk_buff *skb,
  373. struct sctphdr *sctphdr,
  374. struct sctp_association **app,
  375. struct sctp_transport **tpp)
  376. {
  377. union sctp_addr saddr;
  378. union sctp_addr daddr;
  379. struct sctp_af *af;
  380. struct sock *sk = NULL;
  381. struct sctp_association *asoc;
  382. struct sctp_transport *transport = NULL;
  383. *app = NULL; *tpp = NULL;
  384. af = sctp_get_af_specific(family);
  385. if (unlikely(!af)) {
  386. return NULL;
  387. }
  388. /* Initialize local addresses for lookups. */
  389. af->from_skb(&saddr, skb, 1);
  390. af->from_skb(&daddr, skb, 0);
  391. /* Look for an association that matches the incoming ICMP error
  392. * packet.
  393. */
  394. asoc = __sctp_lookup_association(&saddr, &daddr, &transport);
  395. if (!asoc)
  396. return NULL;
  397. sk = asoc->base.sk;
  398. if (ntohl(sctphdr->vtag) != asoc->c.peer_vtag) {
  399. ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
  400. goto out;
  401. }
  402. sctp_bh_lock_sock(sk);
  403. /* If too many ICMPs get dropped on busy
  404. * servers this needs to be solved differently.
  405. */
  406. if (sock_owned_by_user(sk))
  407. NET_INC_STATS_BH(LINUX_MIB_LOCKDROPPEDICMPS);
  408. *app = asoc;
  409. *tpp = transport;
  410. return sk;
  411. out:
  412. if (asoc)
  413. sctp_association_put(asoc);
  414. return NULL;
  415. }
  416. /* Common cleanup code for icmp/icmpv6 error handler. */
  417. void sctp_err_finish(struct sock *sk, struct sctp_association *asoc)
  418. {
  419. sctp_bh_unlock_sock(sk);
  420. if (asoc)
  421. sctp_association_put(asoc);
  422. }
  423. /*
  424. * This routine is called by the ICMP module when it gets some
  425. * sort of error condition. If err < 0 then the socket should
  426. * be closed and the error returned to the user. If err > 0
  427. * it's just the icmp type << 8 | icmp code. After adjustment
  428. * header points to the first 8 bytes of the sctp header. We need
  429. * to find the appropriate port.
  430. *
  431. * The locking strategy used here is very "optimistic". When
  432. * someone else accesses the socket the ICMP is just dropped
  433. * and for some paths there is no check at all.
  434. * A more general error queue to queue errors for later handling
  435. * is probably better.
  436. *
  437. */
  438. void sctp_v4_err(struct sk_buff *skb, __u32 info)
  439. {
  440. struct iphdr *iph = (struct iphdr *)skb->data;
  441. struct sctphdr *sh = (struct sctphdr *)(skb->data + (iph->ihl <<2));
  442. int type = skb->h.icmph->type;
  443. int code = skb->h.icmph->code;
  444. struct sock *sk;
  445. struct sctp_association *asoc = NULL;
  446. struct sctp_transport *transport;
  447. struct inet_sock *inet;
  448. char *saveip, *savesctp;
  449. int err;
  450. if (skb->len < ((iph->ihl << 2) + 8)) {
  451. ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
  452. return;
  453. }
  454. /* Fix up skb to look at the embedded net header. */
  455. saveip = skb->nh.raw;
  456. savesctp = skb->h.raw;
  457. skb->nh.iph = iph;
  458. skb->h.raw = (char *)sh;
  459. sk = sctp_err_lookup(AF_INET, skb, sh, &asoc, &transport);
  460. /* Put back, the original pointers. */
  461. skb->nh.raw = saveip;
  462. skb->h.raw = savesctp;
  463. if (!sk) {
  464. ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
  465. return;
  466. }
  467. /* Warning: The sock lock is held. Remember to call
  468. * sctp_err_finish!
  469. */
  470. switch (type) {
  471. case ICMP_PARAMETERPROB:
  472. err = EPROTO;
  473. break;
  474. case ICMP_DEST_UNREACH:
  475. if (code > NR_ICMP_UNREACH)
  476. goto out_unlock;
  477. /* PMTU discovery (RFC1191) */
  478. if (ICMP_FRAG_NEEDED == code) {
  479. sctp_icmp_frag_needed(sk, asoc, transport, info);
  480. goto out_unlock;
  481. }
  482. else {
  483. if (ICMP_PROT_UNREACH == code) {
  484. sctp_icmp_proto_unreachable(sk, asoc,
  485. transport);
  486. goto out_unlock;
  487. }
  488. }
  489. err = icmp_err_convert[code].errno;
  490. break;
  491. case ICMP_TIME_EXCEEDED:
  492. /* Ignore any time exceeded errors due to fragment reassembly
  493. * timeouts.
  494. */
  495. if (ICMP_EXC_FRAGTIME == code)
  496. goto out_unlock;
  497. err = EHOSTUNREACH;
  498. break;
  499. default:
  500. goto out_unlock;
  501. }
  502. inet = inet_sk(sk);
  503. if (!sock_owned_by_user(sk) && inet->recverr) {
  504. sk->sk_err = err;
  505. sk->sk_error_report(sk);
  506. } else { /* Only an error on timeout */
  507. sk->sk_err_soft = err;
  508. }
  509. out_unlock:
  510. sctp_err_finish(sk, asoc);
  511. }
  512. /*
  513. * RFC 2960, 8.4 - Handle "Out of the blue" Packets.
  514. *
  515. * This function scans all the chunks in the OOTB packet to determine if
  516. * the packet should be discarded right away. If a response might be needed
  517. * for this packet, or, if further processing is possible, the packet will
  518. * be queued to a proper inqueue for the next phase of handling.
  519. *
  520. * Output:
  521. * Return 0 - If further processing is needed.
  522. * Return 1 - If the packet can be discarded right away.
  523. */
  524. int sctp_rcv_ootb(struct sk_buff *skb)
  525. {
  526. sctp_chunkhdr_t *ch;
  527. __u8 *ch_end;
  528. sctp_errhdr_t *err;
  529. ch = (sctp_chunkhdr_t *) skb->data;
  530. /* Scan through all the chunks in the packet. */
  531. do {
  532. /* Break out if chunk length is less then minimal. */
  533. if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
  534. break;
  535. ch_end = ((__u8 *)ch) + WORD_ROUND(ntohs(ch->length));
  536. if (ch_end > skb->tail)
  537. break;
  538. /* RFC 8.4, 2) If the OOTB packet contains an ABORT chunk, the
  539. * receiver MUST silently discard the OOTB packet and take no
  540. * further action.
  541. */
  542. if (SCTP_CID_ABORT == ch->type)
  543. goto discard;
  544. /* RFC 8.4, 6) If the packet contains a SHUTDOWN COMPLETE
  545. * chunk, the receiver should silently discard the packet
  546. * and take no further action.
  547. */
  548. if (SCTP_CID_SHUTDOWN_COMPLETE == ch->type)
  549. goto discard;
  550. /* RFC 8.4, 7) If the packet contains a "Stale cookie" ERROR
  551. * or a COOKIE ACK the SCTP Packet should be silently
  552. * discarded.
  553. */
  554. if (SCTP_CID_COOKIE_ACK == ch->type)
  555. goto discard;
  556. if (SCTP_CID_ERROR == ch->type) {
  557. sctp_walk_errors(err, ch) {
  558. if (SCTP_ERROR_STALE_COOKIE == err->cause)
  559. goto discard;
  560. }
  561. }
  562. ch = (sctp_chunkhdr_t *) ch_end;
  563. } while (ch_end < skb->tail);
  564. return 0;
  565. discard:
  566. return 1;
  567. }
  568. /* Insert endpoint into the hash table. */
  569. static void __sctp_hash_endpoint(struct sctp_endpoint *ep)
  570. {
  571. struct sctp_ep_common **epp;
  572. struct sctp_ep_common *epb;
  573. struct sctp_hashbucket *head;
  574. epb = &ep->base;
  575. epb->hashent = sctp_ep_hashfn(epb->bind_addr.port);
  576. head = &sctp_ep_hashtable[epb->hashent];
  577. sctp_write_lock(&head->lock);
  578. epp = &head->chain;
  579. epb->next = *epp;
  580. if (epb->next)
  581. (*epp)->pprev = &epb->next;
  582. *epp = epb;
  583. epb->pprev = epp;
  584. sctp_write_unlock(&head->lock);
  585. }
  586. /* Add an endpoint to the hash. Local BH-safe. */
  587. void sctp_hash_endpoint(struct sctp_endpoint *ep)
  588. {
  589. sctp_local_bh_disable();
  590. __sctp_hash_endpoint(ep);
  591. sctp_local_bh_enable();
  592. }
  593. /* Remove endpoint from the hash table. */
  594. static void __sctp_unhash_endpoint(struct sctp_endpoint *ep)
  595. {
  596. struct sctp_hashbucket *head;
  597. struct sctp_ep_common *epb;
  598. epb = &ep->base;
  599. epb->hashent = sctp_ep_hashfn(epb->bind_addr.port);
  600. head = &sctp_ep_hashtable[epb->hashent];
  601. sctp_write_lock(&head->lock);
  602. if (epb->pprev) {
  603. if (epb->next)
  604. epb->next->pprev = epb->pprev;
  605. *epb->pprev = epb->next;
  606. epb->pprev = NULL;
  607. }
  608. sctp_write_unlock(&head->lock);
  609. }
  610. /* Remove endpoint from the hash. Local BH-safe. */
  611. void sctp_unhash_endpoint(struct sctp_endpoint *ep)
  612. {
  613. sctp_local_bh_disable();
  614. __sctp_unhash_endpoint(ep);
  615. sctp_local_bh_enable();
  616. }
  617. /* Look up an endpoint. */
  618. static struct sctp_endpoint *__sctp_rcv_lookup_endpoint(const union sctp_addr *laddr)
  619. {
  620. struct sctp_hashbucket *head;
  621. struct sctp_ep_common *epb;
  622. struct sctp_endpoint *ep;
  623. int hash;
  624. hash = sctp_ep_hashfn(laddr->v4.sin_port);
  625. head = &sctp_ep_hashtable[hash];
  626. read_lock(&head->lock);
  627. for (epb = head->chain; epb; epb = epb->next) {
  628. ep = sctp_ep(epb);
  629. if (sctp_endpoint_is_match(ep, laddr))
  630. goto hit;
  631. }
  632. ep = sctp_sk((sctp_get_ctl_sock()))->ep;
  633. epb = &ep->base;
  634. hit:
  635. sctp_endpoint_hold(ep);
  636. read_unlock(&head->lock);
  637. return ep;
  638. }
  639. /* Insert association into the hash table. */
  640. static void __sctp_hash_established(struct sctp_association *asoc)
  641. {
  642. struct sctp_ep_common **epp;
  643. struct sctp_ep_common *epb;
  644. struct sctp_hashbucket *head;
  645. epb = &asoc->base;
  646. /* Calculate which chain this entry will belong to. */
  647. epb->hashent = sctp_assoc_hashfn(epb->bind_addr.port, asoc->peer.port);
  648. head = &sctp_assoc_hashtable[epb->hashent];
  649. sctp_write_lock(&head->lock);
  650. epp = &head->chain;
  651. epb->next = *epp;
  652. if (epb->next)
  653. (*epp)->pprev = &epb->next;
  654. *epp = epb;
  655. epb->pprev = epp;
  656. sctp_write_unlock(&head->lock);
  657. }
  658. /* Add an association to the hash. Local BH-safe. */
  659. void sctp_hash_established(struct sctp_association *asoc)
  660. {
  661. sctp_local_bh_disable();
  662. __sctp_hash_established(asoc);
  663. sctp_local_bh_enable();
  664. }
  665. /* Remove association from the hash table. */
  666. static void __sctp_unhash_established(struct sctp_association *asoc)
  667. {
  668. struct sctp_hashbucket *head;
  669. struct sctp_ep_common *epb;
  670. epb = &asoc->base;
  671. epb->hashent = sctp_assoc_hashfn(epb->bind_addr.port,
  672. asoc->peer.port);
  673. head = &sctp_assoc_hashtable[epb->hashent];
  674. sctp_write_lock(&head->lock);
  675. if (epb->pprev) {
  676. if (epb->next)
  677. epb->next->pprev = epb->pprev;
  678. *epb->pprev = epb->next;
  679. epb->pprev = NULL;
  680. }
  681. sctp_write_unlock(&head->lock);
  682. }
  683. /* Remove association from the hash table. Local BH-safe. */
  684. void sctp_unhash_established(struct sctp_association *asoc)
  685. {
  686. sctp_local_bh_disable();
  687. __sctp_unhash_established(asoc);
  688. sctp_local_bh_enable();
  689. }
  690. /* Look up an association. */
  691. static struct sctp_association *__sctp_lookup_association(
  692. const union sctp_addr *local,
  693. const union sctp_addr *peer,
  694. struct sctp_transport **pt)
  695. {
  696. struct sctp_hashbucket *head;
  697. struct sctp_ep_common *epb;
  698. struct sctp_association *asoc;
  699. struct sctp_transport *transport;
  700. int hash;
  701. /* Optimize here for direct hit, only listening connections can
  702. * have wildcards anyways.
  703. */
  704. hash = sctp_assoc_hashfn(local->v4.sin_port, peer->v4.sin_port);
  705. head = &sctp_assoc_hashtable[hash];
  706. read_lock(&head->lock);
  707. for (epb = head->chain; epb; epb = epb->next) {
  708. asoc = sctp_assoc(epb);
  709. transport = sctp_assoc_is_match(asoc, local, peer);
  710. if (transport)
  711. goto hit;
  712. }
  713. read_unlock(&head->lock);
  714. return NULL;
  715. hit:
  716. *pt = transport;
  717. sctp_association_hold(asoc);
  718. read_unlock(&head->lock);
  719. return asoc;
  720. }
  721. /* Look up an association. BH-safe. */
  722. SCTP_STATIC
  723. struct sctp_association *sctp_lookup_association(const union sctp_addr *laddr,
  724. const union sctp_addr *paddr,
  725. struct sctp_transport **transportp)
  726. {
  727. struct sctp_association *asoc;
  728. sctp_local_bh_disable();
  729. asoc = __sctp_lookup_association(laddr, paddr, transportp);
  730. sctp_local_bh_enable();
  731. return asoc;
  732. }
  733. /* Is there an association matching the given local and peer addresses? */
  734. int sctp_has_association(const union sctp_addr *laddr,
  735. const union sctp_addr *paddr)
  736. {
  737. struct sctp_association *asoc;
  738. struct sctp_transport *transport;
  739. if ((asoc = sctp_lookup_association(laddr, paddr, &transport))) {
  740. sctp_association_put(asoc);
  741. return 1;
  742. }
  743. return 0;
  744. }
  745. /*
  746. * SCTP Implementors Guide, 2.18 Handling of address
  747. * parameters within the INIT or INIT-ACK.
  748. *
  749. * D) When searching for a matching TCB upon reception of an INIT
  750. * or INIT-ACK chunk the receiver SHOULD use not only the
  751. * source address of the packet (containing the INIT or
  752. * INIT-ACK) but the receiver SHOULD also use all valid
  753. * address parameters contained within the chunk.
  754. *
  755. * 2.18.3 Solution description
  756. *
  757. * This new text clearly specifies to an implementor the need
  758. * to look within the INIT or INIT-ACK. Any implementation that
  759. * does not do this, may not be able to establish associations
  760. * in certain circumstances.
  761. *
  762. */
  763. static struct sctp_association *__sctp_rcv_init_lookup(struct sk_buff *skb,
  764. const union sctp_addr *laddr, struct sctp_transport **transportp)
  765. {
  766. struct sctp_association *asoc;
  767. union sctp_addr addr;
  768. union sctp_addr *paddr = &addr;
  769. struct sctphdr *sh = (struct sctphdr *) skb->h.raw;
  770. sctp_chunkhdr_t *ch;
  771. union sctp_params params;
  772. sctp_init_chunk_t *init;
  773. struct sctp_transport *transport;
  774. struct sctp_af *af;
  775. ch = (sctp_chunkhdr_t *) skb->data;
  776. /* If this is INIT/INIT-ACK look inside the chunk too. */
  777. switch (ch->type) {
  778. case SCTP_CID_INIT:
  779. case SCTP_CID_INIT_ACK:
  780. break;
  781. default:
  782. return NULL;
  783. }
  784. /* The code below will attempt to walk the chunk and extract
  785. * parameter information. Before we do that, we need to verify
  786. * that the chunk length doesn't cause overflow. Otherwise, we'll
  787. * walk off the end.
  788. */
  789. if (WORD_ROUND(ntohs(ch->length)) > skb->len)
  790. return NULL;
  791. /*
  792. * This code will NOT touch anything inside the chunk--it is
  793. * strictly READ-ONLY.
  794. *
  795. * RFC 2960 3 SCTP packet Format
  796. *
  797. * Multiple chunks can be bundled into one SCTP packet up to
  798. * the MTU size, except for the INIT, INIT ACK, and SHUTDOWN
  799. * COMPLETE chunks. These chunks MUST NOT be bundled with any
  800. * other chunk in a packet. See Section 6.10 for more details
  801. * on chunk bundling.
  802. */
  803. /* Find the start of the TLVs and the end of the chunk. This is
  804. * the region we search for address parameters.
  805. */
  806. init = (sctp_init_chunk_t *)skb->data;
  807. /* Walk the parameters looking for embedded addresses. */
  808. sctp_walk_params(params, init, init_hdr.params) {
  809. /* Note: Ignoring hostname addresses. */
  810. af = sctp_get_af_specific(param_type2af(params.p->type));
  811. if (!af)
  812. continue;
  813. af->from_addr_param(paddr, params.addr, ntohs(sh->source), 0);
  814. asoc = __sctp_lookup_association(laddr, paddr, &transport);
  815. if (asoc)
  816. return asoc;
  817. }
  818. return NULL;
  819. }
  820. /* Lookup an association for an inbound skb. */
  821. static struct sctp_association *__sctp_rcv_lookup(struct sk_buff *skb,
  822. const union sctp_addr *paddr,
  823. const union sctp_addr *laddr,
  824. struct sctp_transport **transportp)
  825. {
  826. struct sctp_association *asoc;
  827. asoc = __sctp_lookup_association(laddr, paddr, transportp);
  828. /* Further lookup for INIT/INIT-ACK packets.
  829. * SCTP Implementors Guide, 2.18 Handling of address
  830. * parameters within the INIT or INIT-ACK.
  831. */
  832. if (!asoc)
  833. asoc = __sctp_rcv_init_lookup(skb, laddr, transportp);
  834. return asoc;
  835. }