input.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091
  1. /* SCTP kernel 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 implementation
  10. *
  11. * These functions handle all input from the IP layer into SCTP.
  12. *
  13. * This SCTP 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. * This SCTP 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", __func__);
  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. hit:
  621. sctp_endpoint_hold(ep);
  622. read_unlock(&head->lock);
  623. return ep;
  624. }
  625. /* Insert association into the hash table. */
  626. static void __sctp_hash_established(struct sctp_association *asoc)
  627. {
  628. struct sctp_ep_common *epb;
  629. struct sctp_hashbucket *head;
  630. epb = &asoc->base;
  631. /* Calculate which chain this entry will belong to. */
  632. epb->hashent = sctp_assoc_hashfn(epb->bind_addr.port, asoc->peer.port);
  633. head = &sctp_assoc_hashtable[epb->hashent];
  634. sctp_write_lock(&head->lock);
  635. hlist_add_head(&epb->node, &head->chain);
  636. sctp_write_unlock(&head->lock);
  637. }
  638. /* Add an association to the hash. Local BH-safe. */
  639. void sctp_hash_established(struct sctp_association *asoc)
  640. {
  641. if (asoc->temp)
  642. return;
  643. sctp_local_bh_disable();
  644. __sctp_hash_established(asoc);
  645. sctp_local_bh_enable();
  646. }
  647. /* Remove association from the hash table. */
  648. static void __sctp_unhash_established(struct sctp_association *asoc)
  649. {
  650. struct sctp_hashbucket *head;
  651. struct sctp_ep_common *epb;
  652. epb = &asoc->base;
  653. epb->hashent = sctp_assoc_hashfn(epb->bind_addr.port,
  654. asoc->peer.port);
  655. head = &sctp_assoc_hashtable[epb->hashent];
  656. sctp_write_lock(&head->lock);
  657. __hlist_del(&epb->node);
  658. sctp_write_unlock(&head->lock);
  659. }
  660. /* Remove association from the hash table. Local BH-safe. */
  661. void sctp_unhash_established(struct sctp_association *asoc)
  662. {
  663. if (asoc->temp)
  664. return;
  665. sctp_local_bh_disable();
  666. __sctp_unhash_established(asoc);
  667. sctp_local_bh_enable();
  668. }
  669. /* Look up an association. */
  670. static struct sctp_association *__sctp_lookup_association(
  671. const union sctp_addr *local,
  672. const union sctp_addr *peer,
  673. struct sctp_transport **pt)
  674. {
  675. struct sctp_hashbucket *head;
  676. struct sctp_ep_common *epb;
  677. struct sctp_association *asoc;
  678. struct sctp_transport *transport;
  679. struct hlist_node *node;
  680. int hash;
  681. /* Optimize here for direct hit, only listening connections can
  682. * have wildcards anyways.
  683. */
  684. hash = sctp_assoc_hashfn(ntohs(local->v4.sin_port), ntohs(peer->v4.sin_port));
  685. head = &sctp_assoc_hashtable[hash];
  686. read_lock(&head->lock);
  687. sctp_for_each_hentry(epb, node, &head->chain) {
  688. asoc = sctp_assoc(epb);
  689. transport = sctp_assoc_is_match(asoc, local, peer);
  690. if (transport)
  691. goto hit;
  692. }
  693. read_unlock(&head->lock);
  694. return NULL;
  695. hit:
  696. *pt = transport;
  697. sctp_association_hold(asoc);
  698. read_unlock(&head->lock);
  699. return asoc;
  700. }
  701. /* Look up an association. BH-safe. */
  702. SCTP_STATIC
  703. struct sctp_association *sctp_lookup_association(const union sctp_addr *laddr,
  704. const union sctp_addr *paddr,
  705. struct sctp_transport **transportp)
  706. {
  707. struct sctp_association *asoc;
  708. sctp_local_bh_disable();
  709. asoc = __sctp_lookup_association(laddr, paddr, transportp);
  710. sctp_local_bh_enable();
  711. return asoc;
  712. }
  713. /* Is there an association matching the given local and peer addresses? */
  714. int sctp_has_association(const union sctp_addr *laddr,
  715. const union sctp_addr *paddr)
  716. {
  717. struct sctp_association *asoc;
  718. struct sctp_transport *transport;
  719. if ((asoc = sctp_lookup_association(laddr, paddr, &transport))) {
  720. sctp_association_put(asoc);
  721. return 1;
  722. }
  723. return 0;
  724. }
  725. /*
  726. * SCTP Implementors Guide, 2.18 Handling of address
  727. * parameters within the INIT or INIT-ACK.
  728. *
  729. * D) When searching for a matching TCB upon reception of an INIT
  730. * or INIT-ACK chunk the receiver SHOULD use not only the
  731. * source address of the packet (containing the INIT or
  732. * INIT-ACK) but the receiver SHOULD also use all valid
  733. * address parameters contained within the chunk.
  734. *
  735. * 2.18.3 Solution description
  736. *
  737. * This new text clearly specifies to an implementor the need
  738. * to look within the INIT or INIT-ACK. Any implementation that
  739. * does not do this, may not be able to establish associations
  740. * in certain circumstances.
  741. *
  742. */
  743. static struct sctp_association *__sctp_rcv_init_lookup(struct sk_buff *skb,
  744. const union sctp_addr *laddr, struct sctp_transport **transportp)
  745. {
  746. struct sctp_association *asoc;
  747. union sctp_addr addr;
  748. union sctp_addr *paddr = &addr;
  749. struct sctphdr *sh = sctp_hdr(skb);
  750. sctp_chunkhdr_t *ch;
  751. union sctp_params params;
  752. sctp_init_chunk_t *init;
  753. struct sctp_transport *transport;
  754. struct sctp_af *af;
  755. ch = (sctp_chunkhdr_t *) skb->data;
  756. /*
  757. * This code will NOT touch anything inside the chunk--it is
  758. * strictly READ-ONLY.
  759. *
  760. * RFC 2960 3 SCTP packet Format
  761. *
  762. * Multiple chunks can be bundled into one SCTP packet up to
  763. * the MTU size, except for the INIT, INIT ACK, and SHUTDOWN
  764. * COMPLETE chunks. These chunks MUST NOT be bundled with any
  765. * other chunk in a packet. See Section 6.10 for more details
  766. * on chunk bundling.
  767. */
  768. /* Find the start of the TLVs and the end of the chunk. This is
  769. * the region we search for address parameters.
  770. */
  771. init = (sctp_init_chunk_t *)skb->data;
  772. /* Walk the parameters looking for embedded addresses. */
  773. sctp_walk_params(params, init, init_hdr.params) {
  774. /* Note: Ignoring hostname addresses. */
  775. af = sctp_get_af_specific(param_type2af(params.p->type));
  776. if (!af)
  777. continue;
  778. af->from_addr_param(paddr, params.addr, sh->source, 0);
  779. asoc = __sctp_lookup_association(laddr, paddr, &transport);
  780. if (asoc)
  781. return asoc;
  782. }
  783. return NULL;
  784. }
  785. /* ADD-IP, Section 5.2
  786. * When an endpoint receives an ASCONF Chunk from the remote peer
  787. * special procedures may be needed to identify the association the
  788. * ASCONF Chunk is associated with. To properly find the association
  789. * the following procedures SHOULD be followed:
  790. *
  791. * D2) If the association is not found, use the address found in the
  792. * Address Parameter TLV combined with the port number found in the
  793. * SCTP common header. If found proceed to rule D4.
  794. *
  795. * D2-ext) If more than one ASCONF Chunks are packed together, use the
  796. * address found in the ASCONF Address Parameter TLV of each of the
  797. * subsequent ASCONF Chunks. If found, proceed to rule D4.
  798. */
  799. static struct sctp_association *__sctp_rcv_asconf_lookup(
  800. sctp_chunkhdr_t *ch,
  801. const union sctp_addr *laddr,
  802. __be16 peer_port,
  803. struct sctp_transport **transportp)
  804. {
  805. sctp_addip_chunk_t *asconf = (struct sctp_addip_chunk *)ch;
  806. struct sctp_af *af;
  807. union sctp_addr_param *param;
  808. union sctp_addr paddr;
  809. /* Skip over the ADDIP header and find the Address parameter */
  810. param = (union sctp_addr_param *)(asconf + 1);
  811. af = sctp_get_af_specific(param_type2af(param->v4.param_hdr.type));
  812. if (unlikely(!af))
  813. return NULL;
  814. af->from_addr_param(&paddr, param, peer_port, 0);
  815. return __sctp_lookup_association(laddr, &paddr, transportp);
  816. }
  817. /* SCTP-AUTH, Section 6.3:
  818. * If the receiver does not find a STCB for a packet containing an AUTH
  819. * chunk as the first chunk and not a COOKIE-ECHO chunk as the second
  820. * chunk, it MUST use the chunks after the AUTH chunk to look up an existing
  821. * association.
  822. *
  823. * This means that any chunks that can help us identify the association need
  824. * to be looked at to find this assocation.
  825. */
  826. static struct sctp_association *__sctp_rcv_walk_lookup(struct sk_buff *skb,
  827. const union sctp_addr *laddr,
  828. struct sctp_transport **transportp)
  829. {
  830. struct sctp_association *asoc = NULL;
  831. sctp_chunkhdr_t *ch;
  832. int have_auth = 0;
  833. unsigned int chunk_num = 1;
  834. __u8 *ch_end;
  835. /* Walk through the chunks looking for AUTH or ASCONF chunks
  836. * to help us find the association.
  837. */
  838. ch = (sctp_chunkhdr_t *) skb->data;
  839. do {
  840. /* Break out if chunk length is less then minimal. */
  841. if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
  842. break;
  843. ch_end = ((__u8 *)ch) + WORD_ROUND(ntohs(ch->length));
  844. if (ch_end > skb_tail_pointer(skb))
  845. break;
  846. switch(ch->type) {
  847. case SCTP_CID_AUTH:
  848. have_auth = chunk_num;
  849. break;
  850. case SCTP_CID_COOKIE_ECHO:
  851. /* If a packet arrives containing an AUTH chunk as
  852. * a first chunk, a COOKIE-ECHO chunk as the second
  853. * chunk, and possibly more chunks after them, and
  854. * the receiver does not have an STCB for that
  855. * packet, then authentication is based on
  856. * the contents of the COOKIE- ECHO chunk.
  857. */
  858. if (have_auth == 1 && chunk_num == 2)
  859. return NULL;
  860. break;
  861. case SCTP_CID_ASCONF:
  862. if (have_auth || sctp_addip_noauth)
  863. asoc = __sctp_rcv_asconf_lookup(ch, laddr,
  864. sctp_hdr(skb)->source,
  865. transportp);
  866. default:
  867. break;
  868. }
  869. if (asoc)
  870. break;
  871. ch = (sctp_chunkhdr_t *) ch_end;
  872. chunk_num++;
  873. } while (ch_end < skb_tail_pointer(skb));
  874. return asoc;
  875. }
  876. /*
  877. * There are circumstances when we need to look inside the SCTP packet
  878. * for information to help us find the association. Examples
  879. * include looking inside of INIT/INIT-ACK chunks or after the AUTH
  880. * chunks.
  881. */
  882. static struct sctp_association *__sctp_rcv_lookup_harder(struct sk_buff *skb,
  883. const union sctp_addr *laddr,
  884. struct sctp_transport **transportp)
  885. {
  886. sctp_chunkhdr_t *ch;
  887. ch = (sctp_chunkhdr_t *) skb->data;
  888. /* The code below will attempt to walk the chunk and extract
  889. * parameter information. Before we do that, we need to verify
  890. * that the chunk length doesn't cause overflow. Otherwise, we'll
  891. * walk off the end.
  892. */
  893. if (WORD_ROUND(ntohs(ch->length)) > skb->len)
  894. return NULL;
  895. /* If this is INIT/INIT-ACK look inside the chunk too. */
  896. switch (ch->type) {
  897. case SCTP_CID_INIT:
  898. case SCTP_CID_INIT_ACK:
  899. return __sctp_rcv_init_lookup(skb, laddr, transportp);
  900. break;
  901. default:
  902. return __sctp_rcv_walk_lookup(skb, laddr, transportp);
  903. break;
  904. }
  905. return NULL;
  906. }
  907. /* Lookup an association for an inbound skb. */
  908. static struct sctp_association *__sctp_rcv_lookup(struct sk_buff *skb,
  909. const union sctp_addr *paddr,
  910. const union sctp_addr *laddr,
  911. struct sctp_transport **transportp)
  912. {
  913. struct sctp_association *asoc;
  914. asoc = __sctp_lookup_association(laddr, paddr, transportp);
  915. /* Further lookup for INIT/INIT-ACK packets.
  916. * SCTP Implementors Guide, 2.18 Handling of address
  917. * parameters within the INIT or INIT-ACK.
  918. */
  919. if (!asoc)
  920. asoc = __sctp_rcv_lookup_harder(skb, laddr, transportp);
  921. return asoc;
  922. }