input.c 26 KB

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