input.c 27 KB

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