input.c 30 KB

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