input.c 27 KB

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