input.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  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. 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 (sock_owned_by_user(sk) || !t || (t->pathmtu == pmtu))
  318. return;
  319. if (t->param_flags & SPP_PMTUD_ENABLE) {
  320. if (unlikely(pmtu < SCTP_DEFAULT_MINSEGMENT)) {
  321. printk(KERN_WARNING "%s: Reported pmtu %d too low, "
  322. "using default minimum of %d\n",
  323. __FUNCTION__, pmtu,
  324. SCTP_DEFAULT_MINSEGMENT);
  325. /* Use default minimum segment size and disable
  326. * pmtu discovery on this transport.
  327. */
  328. t->pathmtu = SCTP_DEFAULT_MINSEGMENT;
  329. t->param_flags = (t->param_flags & ~SPP_PMTUD) |
  330. SPP_PMTUD_DISABLE;
  331. } else {
  332. t->pathmtu = pmtu;
  333. }
  334. /* Update association pmtu. */
  335. sctp_assoc_sync_pmtu(asoc);
  336. }
  337. /* Retransmit with the new pmtu setting.
  338. * Normally, if PMTU discovery is disabled, an ICMP Fragmentation
  339. * Needed will never be sent, but if a message was sent before
  340. * PMTU discovery was disabled that was larger than the PMTU, it
  341. * would not be fragmented, so it must be re-transmitted fragmented.
  342. */
  343. sctp_retransmit(&asoc->outqueue, t, SCTP_RTXR_PMTUD);
  344. }
  345. /*
  346. * SCTP Implementer's Guide, 2.37 ICMP handling procedures
  347. *
  348. * ICMP8) If the ICMP code is a "Unrecognized next header type encountered"
  349. * or a "Protocol Unreachable" treat this message as an abort
  350. * with the T bit set.
  351. *
  352. * This function sends an event to the state machine, which will abort the
  353. * association.
  354. *
  355. */
  356. void sctp_icmp_proto_unreachable(struct sock *sk,
  357. struct sctp_association *asoc,
  358. struct sctp_transport *t)
  359. {
  360. SCTP_DEBUG_PRINTK("%s\n", __FUNCTION__);
  361. sctp_do_sm(SCTP_EVENT_T_OTHER,
  362. SCTP_ST_OTHER(SCTP_EVENT_ICMP_PROTO_UNREACH),
  363. asoc->state, asoc->ep, asoc, t,
  364. GFP_ATOMIC);
  365. }
  366. /* Common lookup code for icmp/icmpv6 error handler. */
  367. struct sock *sctp_err_lookup(int family, struct sk_buff *skb,
  368. struct sctphdr *sctphdr,
  369. struct sctp_association **app,
  370. struct sctp_transport **tpp)
  371. {
  372. union sctp_addr saddr;
  373. union sctp_addr daddr;
  374. struct sctp_af *af;
  375. struct sock *sk = NULL;
  376. struct sctp_association *asoc;
  377. struct sctp_transport *transport = NULL;
  378. *app = NULL; *tpp = NULL;
  379. af = sctp_get_af_specific(family);
  380. if (unlikely(!af)) {
  381. return NULL;
  382. }
  383. /* Initialize local addresses for lookups. */
  384. af->from_skb(&saddr, skb, 1);
  385. af->from_skb(&daddr, skb, 0);
  386. /* Look for an association that matches the incoming ICMP error
  387. * packet.
  388. */
  389. asoc = __sctp_lookup_association(&saddr, &daddr, &transport);
  390. if (!asoc)
  391. return NULL;
  392. sk = asoc->base.sk;
  393. if (ntohl(sctphdr->vtag) != asoc->c.peer_vtag) {
  394. ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
  395. goto out;
  396. }
  397. sctp_bh_lock_sock(sk);
  398. /* If too many ICMPs get dropped on busy
  399. * servers this needs to be solved differently.
  400. */
  401. if (sock_owned_by_user(sk))
  402. NET_INC_STATS_BH(LINUX_MIB_LOCKDROPPEDICMPS);
  403. *app = asoc;
  404. *tpp = transport;
  405. return sk;
  406. out:
  407. if (asoc)
  408. sctp_association_put(asoc);
  409. return NULL;
  410. }
  411. /* Common cleanup code for icmp/icmpv6 error handler. */
  412. void sctp_err_finish(struct sock *sk, struct sctp_association *asoc)
  413. {
  414. sctp_bh_unlock_sock(sk);
  415. if (asoc)
  416. sctp_association_put(asoc);
  417. }
  418. /*
  419. * This routine is called by the ICMP module when it gets some
  420. * sort of error condition. If err < 0 then the socket should
  421. * be closed and the error returned to the user. If err > 0
  422. * it's just the icmp type << 8 | icmp code. After adjustment
  423. * header points to the first 8 bytes of the sctp header. We need
  424. * to find the appropriate port.
  425. *
  426. * The locking strategy used here is very "optimistic". When
  427. * someone else accesses the socket the ICMP is just dropped
  428. * and for some paths there is no check at all.
  429. * A more general error queue to queue errors for later handling
  430. * is probably better.
  431. *
  432. */
  433. void sctp_v4_err(struct sk_buff *skb, __u32 info)
  434. {
  435. struct iphdr *iph = (struct iphdr *)skb->data;
  436. struct sctphdr *sh = (struct sctphdr *)(skb->data + (iph->ihl <<2));
  437. int type = skb->h.icmph->type;
  438. int code = skb->h.icmph->code;
  439. struct sock *sk;
  440. struct sctp_association *asoc = NULL;
  441. struct sctp_transport *transport;
  442. struct inet_sock *inet;
  443. char *saveip, *savesctp;
  444. int err;
  445. if (skb->len < ((iph->ihl << 2) + 8)) {
  446. ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
  447. return;
  448. }
  449. /* Fix up skb to look at the embedded net header. */
  450. saveip = skb->nh.raw;
  451. savesctp = skb->h.raw;
  452. skb->nh.iph = iph;
  453. skb->h.raw = (char *)sh;
  454. sk = sctp_err_lookup(AF_INET, skb, sh, &asoc, &transport);
  455. /* Put back, the original pointers. */
  456. skb->nh.raw = saveip;
  457. skb->h.raw = savesctp;
  458. if (!sk) {
  459. ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
  460. return;
  461. }
  462. /* Warning: The sock lock is held. Remember to call
  463. * sctp_err_finish!
  464. */
  465. switch (type) {
  466. case ICMP_PARAMETERPROB:
  467. err = EPROTO;
  468. break;
  469. case ICMP_DEST_UNREACH:
  470. if (code > NR_ICMP_UNREACH)
  471. goto out_unlock;
  472. /* PMTU discovery (RFC1191) */
  473. if (ICMP_FRAG_NEEDED == code) {
  474. sctp_icmp_frag_needed(sk, asoc, transport, info);
  475. goto out_unlock;
  476. }
  477. else {
  478. if (ICMP_PROT_UNREACH == code) {
  479. sctp_icmp_proto_unreachable(sk, asoc,
  480. transport);
  481. goto out_unlock;
  482. }
  483. }
  484. err = icmp_err_convert[code].errno;
  485. break;
  486. case ICMP_TIME_EXCEEDED:
  487. /* Ignore any time exceeded errors due to fragment reassembly
  488. * timeouts.
  489. */
  490. if (ICMP_EXC_FRAGTIME == code)
  491. goto out_unlock;
  492. err = EHOSTUNREACH;
  493. break;
  494. default:
  495. goto out_unlock;
  496. }
  497. inet = inet_sk(sk);
  498. if (!sock_owned_by_user(sk) && inet->recverr) {
  499. sk->sk_err = err;
  500. sk->sk_error_report(sk);
  501. } else { /* Only an error on timeout */
  502. sk->sk_err_soft = err;
  503. }
  504. out_unlock:
  505. sctp_err_finish(sk, asoc);
  506. }
  507. /*
  508. * RFC 2960, 8.4 - Handle "Out of the blue" Packets.
  509. *
  510. * This function scans all the chunks in the OOTB packet to determine if
  511. * the packet should be discarded right away. If a response might be needed
  512. * for this packet, or, if further processing is possible, the packet will
  513. * be queued to a proper inqueue for the next phase of handling.
  514. *
  515. * Output:
  516. * Return 0 - If further processing is needed.
  517. * Return 1 - If the packet can be discarded right away.
  518. */
  519. int sctp_rcv_ootb(struct sk_buff *skb)
  520. {
  521. sctp_chunkhdr_t *ch;
  522. __u8 *ch_end;
  523. sctp_errhdr_t *err;
  524. ch = (sctp_chunkhdr_t *) skb->data;
  525. /* Scan through all the chunks in the packet. */
  526. do {
  527. /* Break out if chunk length is less then minimal. */
  528. if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
  529. break;
  530. ch_end = ((__u8 *)ch) + WORD_ROUND(ntohs(ch->length));
  531. if (ch_end > skb->tail)
  532. break;
  533. /* RFC 8.4, 2) If the OOTB packet contains an ABORT chunk, the
  534. * receiver MUST silently discard the OOTB packet and take no
  535. * further action.
  536. */
  537. if (SCTP_CID_ABORT == ch->type)
  538. goto discard;
  539. /* RFC 8.4, 6) If the packet contains a SHUTDOWN COMPLETE
  540. * chunk, the receiver should silently discard the packet
  541. * and take no further action.
  542. */
  543. if (SCTP_CID_SHUTDOWN_COMPLETE == ch->type)
  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);
  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 **epp;
  567. struct sctp_ep_common *epb;
  568. struct sctp_hashbucket *head;
  569. epb = &ep->base;
  570. epb->hashent = sctp_ep_hashfn(epb->bind_addr.port);
  571. head = &sctp_ep_hashtable[epb->hashent];
  572. sctp_write_lock(&head->lock);
  573. epp = &head->chain;
  574. epb->next = *epp;
  575. if (epb->next)
  576. (*epp)->pprev = &epb->next;
  577. *epp = epb;
  578. epb->pprev = epp;
  579. sctp_write_unlock(&head->lock);
  580. }
  581. /* Add an endpoint to the hash. Local BH-safe. */
  582. void sctp_hash_endpoint(struct sctp_endpoint *ep)
  583. {
  584. sctp_local_bh_disable();
  585. __sctp_hash_endpoint(ep);
  586. sctp_local_bh_enable();
  587. }
  588. /* Remove endpoint from the hash table. */
  589. static void __sctp_unhash_endpoint(struct sctp_endpoint *ep)
  590. {
  591. struct sctp_hashbucket *head;
  592. struct sctp_ep_common *epb;
  593. epb = &ep->base;
  594. epb->hashent = sctp_ep_hashfn(epb->bind_addr.port);
  595. head = &sctp_ep_hashtable[epb->hashent];
  596. sctp_write_lock(&head->lock);
  597. if (epb->pprev) {
  598. if (epb->next)
  599. epb->next->pprev = epb->pprev;
  600. *epb->pprev = epb->next;
  601. epb->pprev = NULL;
  602. }
  603. sctp_write_unlock(&head->lock);
  604. }
  605. /* Remove endpoint from the hash. Local BH-safe. */
  606. void sctp_unhash_endpoint(struct sctp_endpoint *ep)
  607. {
  608. sctp_local_bh_disable();
  609. __sctp_unhash_endpoint(ep);
  610. sctp_local_bh_enable();
  611. }
  612. /* Look up an endpoint. */
  613. static struct sctp_endpoint *__sctp_rcv_lookup_endpoint(const union sctp_addr *laddr)
  614. {
  615. struct sctp_hashbucket *head;
  616. struct sctp_ep_common *epb;
  617. struct sctp_endpoint *ep;
  618. int hash;
  619. hash = sctp_ep_hashfn(laddr->v4.sin_port);
  620. head = &sctp_ep_hashtable[hash];
  621. read_lock(&head->lock);
  622. for (epb = head->chain; epb; epb = epb->next) {
  623. ep = sctp_ep(epb);
  624. if (sctp_endpoint_is_match(ep, laddr))
  625. goto hit;
  626. }
  627. ep = sctp_sk((sctp_get_ctl_sock()))->ep;
  628. epb = &ep->base;
  629. hit:
  630. sctp_endpoint_hold(ep);
  631. read_unlock(&head->lock);
  632. return ep;
  633. }
  634. /* Insert association into the hash table. */
  635. static void __sctp_hash_established(struct sctp_association *asoc)
  636. {
  637. struct sctp_ep_common **epp;
  638. struct sctp_ep_common *epb;
  639. struct sctp_hashbucket *head;
  640. epb = &asoc->base;
  641. /* Calculate which chain this entry will belong to. */
  642. epb->hashent = sctp_assoc_hashfn(epb->bind_addr.port, asoc->peer.port);
  643. head = &sctp_assoc_hashtable[epb->hashent];
  644. sctp_write_lock(&head->lock);
  645. epp = &head->chain;
  646. epb->next = *epp;
  647. if (epb->next)
  648. (*epp)->pprev = &epb->next;
  649. *epp = epb;
  650. epb->pprev = epp;
  651. sctp_write_unlock(&head->lock);
  652. }
  653. /* Add an association to the hash. Local BH-safe. */
  654. void sctp_hash_established(struct sctp_association *asoc)
  655. {
  656. sctp_local_bh_disable();
  657. __sctp_hash_established(asoc);
  658. sctp_local_bh_enable();
  659. }
  660. /* Remove association from the hash table. */
  661. static void __sctp_unhash_established(struct sctp_association *asoc)
  662. {
  663. struct sctp_hashbucket *head;
  664. struct sctp_ep_common *epb;
  665. epb = &asoc->base;
  666. epb->hashent = sctp_assoc_hashfn(epb->bind_addr.port,
  667. asoc->peer.port);
  668. head = &sctp_assoc_hashtable[epb->hashent];
  669. sctp_write_lock(&head->lock);
  670. if (epb->pprev) {
  671. if (epb->next)
  672. epb->next->pprev = epb->pprev;
  673. *epb->pprev = epb->next;
  674. epb->pprev = NULL;
  675. }
  676. sctp_write_unlock(&head->lock);
  677. }
  678. /* Remove association from the hash table. Local BH-safe. */
  679. void sctp_unhash_established(struct sctp_association *asoc)
  680. {
  681. sctp_local_bh_disable();
  682. __sctp_unhash_established(asoc);
  683. sctp_local_bh_enable();
  684. }
  685. /* Look up an association. */
  686. static struct sctp_association *__sctp_lookup_association(
  687. const union sctp_addr *local,
  688. const union sctp_addr *peer,
  689. struct sctp_transport **pt)
  690. {
  691. struct sctp_hashbucket *head;
  692. struct sctp_ep_common *epb;
  693. struct sctp_association *asoc;
  694. struct sctp_transport *transport;
  695. int hash;
  696. /* Optimize here for direct hit, only listening connections can
  697. * have wildcards anyways.
  698. */
  699. hash = sctp_assoc_hashfn(local->v4.sin_port, peer->v4.sin_port);
  700. head = &sctp_assoc_hashtable[hash];
  701. read_lock(&head->lock);
  702. for (epb = head->chain; epb; epb = epb->next) {
  703. asoc = sctp_assoc(epb);
  704. transport = sctp_assoc_is_match(asoc, local, peer);
  705. if (transport)
  706. goto hit;
  707. }
  708. read_unlock(&head->lock);
  709. return NULL;
  710. hit:
  711. *pt = transport;
  712. sctp_association_hold(asoc);
  713. read_unlock(&head->lock);
  714. return asoc;
  715. }
  716. /* Look up an association. BH-safe. */
  717. SCTP_STATIC
  718. struct sctp_association *sctp_lookup_association(const union sctp_addr *laddr,
  719. const union sctp_addr *paddr,
  720. struct sctp_transport **transportp)
  721. {
  722. struct sctp_association *asoc;
  723. sctp_local_bh_disable();
  724. asoc = __sctp_lookup_association(laddr, paddr, transportp);
  725. sctp_local_bh_enable();
  726. return asoc;
  727. }
  728. /* Is there an association matching the given local and peer addresses? */
  729. int sctp_has_association(const union sctp_addr *laddr,
  730. const union sctp_addr *paddr)
  731. {
  732. struct sctp_association *asoc;
  733. struct sctp_transport *transport;
  734. if ((asoc = sctp_lookup_association(laddr, paddr, &transport))) {
  735. sctp_association_put(asoc);
  736. return 1;
  737. }
  738. return 0;
  739. }
  740. /*
  741. * SCTP Implementors Guide, 2.18 Handling of address
  742. * parameters within the INIT or INIT-ACK.
  743. *
  744. * D) When searching for a matching TCB upon reception of an INIT
  745. * or INIT-ACK chunk the receiver SHOULD use not only the
  746. * source address of the packet (containing the INIT or
  747. * INIT-ACK) but the receiver SHOULD also use all valid
  748. * address parameters contained within the chunk.
  749. *
  750. * 2.18.3 Solution description
  751. *
  752. * This new text clearly specifies to an implementor the need
  753. * to look within the INIT or INIT-ACK. Any implementation that
  754. * does not do this, may not be able to establish associations
  755. * in certain circumstances.
  756. *
  757. */
  758. static struct sctp_association *__sctp_rcv_init_lookup(struct sk_buff *skb,
  759. const union sctp_addr *laddr, struct sctp_transport **transportp)
  760. {
  761. struct sctp_association *asoc;
  762. union sctp_addr addr;
  763. union sctp_addr *paddr = &addr;
  764. struct sctphdr *sh = (struct sctphdr *) skb->h.raw;
  765. sctp_chunkhdr_t *ch;
  766. union sctp_params params;
  767. sctp_init_chunk_t *init;
  768. struct sctp_transport *transport;
  769. struct sctp_af *af;
  770. ch = (sctp_chunkhdr_t *) skb->data;
  771. /* If this is INIT/INIT-ACK look inside the chunk too. */
  772. switch (ch->type) {
  773. case SCTP_CID_INIT:
  774. case SCTP_CID_INIT_ACK:
  775. break;
  776. default:
  777. return NULL;
  778. }
  779. /* The code below will attempt to walk the chunk and extract
  780. * parameter information. Before we do that, we need to verify
  781. * that the chunk length doesn't cause overflow. Otherwise, we'll
  782. * walk off the end.
  783. */
  784. if (WORD_ROUND(ntohs(ch->length)) > skb->len)
  785. return NULL;
  786. /*
  787. * This code will NOT touch anything inside the chunk--it is
  788. * strictly READ-ONLY.
  789. *
  790. * RFC 2960 3 SCTP packet Format
  791. *
  792. * Multiple chunks can be bundled into one SCTP packet up to
  793. * the MTU size, except for the INIT, INIT ACK, and SHUTDOWN
  794. * COMPLETE chunks. These chunks MUST NOT be bundled with any
  795. * other chunk in a packet. See Section 6.10 for more details
  796. * on chunk bundling.
  797. */
  798. /* Find the start of the TLVs and the end of the chunk. This is
  799. * the region we search for address parameters.
  800. */
  801. init = (sctp_init_chunk_t *)skb->data;
  802. /* Walk the parameters looking for embedded addresses. */
  803. sctp_walk_params(params, init, init_hdr.params) {
  804. /* Note: Ignoring hostname addresses. */
  805. af = sctp_get_af_specific(param_type2af(params.p->type));
  806. if (!af)
  807. continue;
  808. af->from_addr_param(paddr, params.addr, ntohs(sh->source), 0);
  809. asoc = __sctp_lookup_association(laddr, paddr, &transport);
  810. if (asoc)
  811. return asoc;
  812. }
  813. return NULL;
  814. }
  815. /* Lookup an association for an inbound skb. */
  816. static struct sctp_association *__sctp_rcv_lookup(struct sk_buff *skb,
  817. const union sctp_addr *paddr,
  818. const union sctp_addr *laddr,
  819. struct sctp_transport **transportp)
  820. {
  821. struct sctp_association *asoc;
  822. asoc = __sctp_lookup_association(laddr, paddr, transportp);
  823. /* Further lookup for INIT/INIT-ACK packets.
  824. * SCTP Implementors Guide, 2.18 Handling of address
  825. * parameters within the INIT or INIT-ACK.
  826. */
  827. if (!asoc)
  828. asoc = __sctp_rcv_init_lookup(skb, laddr, transportp);
  829. return asoc;
  830. }