1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
5  * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
6  * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * a) Redistributions of source code must retain the above copyright notice,
12  *    this list of conditions and the following disclaimer.
13  *
14  * b) Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the distribution.
17  *
18  * c) Neither the name of Cisco Systems, Inc. nor the names of its
19  *    contributors may be used to endorse or promote products derived
20  *    from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32  * THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #if defined(__Userspace__)
36 #include <sys/types.h>
37 #if !defined (__Userspace_os_Windows)
38 #include <sys/wait.h>
39 #include <unistd.h>
40 #include <pthread.h>
41 #endif
42 #if defined(__Userspace_os_NaCl)
43 #include <sys/select.h>
44 #endif
45 #include <stdlib.h>
46 #include <string.h>
47 #include <stdio.h>
48 #include <errno.h>
49 #include <user_atomic.h>
50 #include <netinet/sctp_sysctl.h>
51 #include <netinet/sctp_pcb.h>
52 #else
53 #include <netinet/sctp_os.h>
54 #include <netinet/sctp_callout.h>
55 #include <netinet/sctp_pcb.h>
56 #endif
57 
58 /*
59  * Callout/Timer routines for OS that doesn't have them
60  */
61 #if defined(__APPLE__) || defined(__Userspace__)
62 static uint32_t ticks = 0;
63 #else
64 extern int ticks;
65 #endif
66 
sctp_get_tick_count(void)67 uint32_t sctp_get_tick_count(void) {
68 	uint32_t ret;
69 
70 	SCTP_TIMERQ_LOCK();
71 	ret = ticks;
72 	SCTP_TIMERQ_UNLOCK();
73 	return ret;
74 }
75 
76 /*
77  * SCTP_TIMERQ_LOCK protects:
78  * - SCTP_BASE_INFO(callqueue)
79  * - sctp_os_timer_next: next timer to check
80  */
81 static sctp_os_timer_t *sctp_os_timer_next = NULL;
82 
83 void
sctp_os_timer_init(sctp_os_timer_t * c)84 sctp_os_timer_init(sctp_os_timer_t *c)
85 {
86 	memset(c, 0, sizeof(*c));
87 }
88 
89 void
sctp_os_timer_start(sctp_os_timer_t * c,uint32_t to_ticks,void (* ftn)(void *),void * arg)90 sctp_os_timer_start(sctp_os_timer_t *c, uint32_t to_ticks, void (*ftn) (void *),
91                     void *arg)
92 {
93 	/* paranoia */
94 	if ((c == NULL) || (ftn == NULL))
95 	    return;
96 
97 	SCTP_TIMERQ_LOCK();
98 	/* check to see if we're rescheduling a timer */
99 	if (c->c_flags & SCTP_CALLOUT_PENDING) {
100 		if (c == sctp_os_timer_next) {
101 			sctp_os_timer_next = TAILQ_NEXT(c, tqe);
102 		}
103 		TAILQ_REMOVE(&SCTP_BASE_INFO(callqueue), c, tqe);
104 		/*
105 		 * part of the normal "stop a pending callout" process
106 		 * is to clear the CALLOUT_ACTIVE and CALLOUT_PENDING
107 		 * flags.  We don't bother since we are setting these
108 		 * below and we still hold the lock.
109 		 */
110 	}
111 
112 	/*
113 	 * We could unlock/splx here and lock/spl at the TAILQ_INSERT_TAIL,
114 	 * but there's no point since doing this setup doesn't take much time.
115 	 */
116 	if (to_ticks == 0)
117 		to_ticks = 1;
118 
119 	c->c_arg = arg;
120 	c->c_flags = (SCTP_CALLOUT_ACTIVE | SCTP_CALLOUT_PENDING);
121 	c->c_func = ftn;
122 	c->c_time = ticks + to_ticks;
123 	TAILQ_INSERT_TAIL(&SCTP_BASE_INFO(callqueue), c, tqe);
124 	SCTP_TIMERQ_UNLOCK();
125 }
126 
127 int
sctp_os_timer_stop(sctp_os_timer_t * c)128 sctp_os_timer_stop(sctp_os_timer_t *c)
129 {
130 	SCTP_TIMERQ_LOCK();
131 	/*
132 	 * Don't attempt to delete a callout that's not on the queue.
133 	 */
134 	if (!(c->c_flags & SCTP_CALLOUT_PENDING)) {
135 		c->c_flags &= ~SCTP_CALLOUT_ACTIVE;
136 		SCTP_TIMERQ_UNLOCK();
137 		return (0);
138 	}
139 	c->c_flags &= ~(SCTP_CALLOUT_ACTIVE | SCTP_CALLOUT_PENDING);
140 	if (c == sctp_os_timer_next) {
141 		sctp_os_timer_next = TAILQ_NEXT(c, tqe);
142 	}
143 	TAILQ_REMOVE(&SCTP_BASE_INFO(callqueue), c, tqe);
144 	SCTP_TIMERQ_UNLOCK();
145 	return (1);
146 }
147 
148 void
sctp_handle_tick(uint32_t elapsed_ticks)149 sctp_handle_tick(uint32_t elapsed_ticks)
150 {
151 	sctp_os_timer_t *c;
152 	void (*c_func)(void *);
153 	void *c_arg;
154 
155 	SCTP_TIMERQ_LOCK();
156 	/* update our tick count */
157 	ticks += elapsed_ticks;
158 	c = TAILQ_FIRST(&SCTP_BASE_INFO(callqueue));
159 	while (c) {
160 		if (SCTP_UINT32_GE(ticks, c->c_time)) {
161 			sctp_os_timer_next = TAILQ_NEXT(c, tqe);
162 			TAILQ_REMOVE(&SCTP_BASE_INFO(callqueue), c, tqe);
163 			c_func = c->c_func;
164 			c_arg = c->c_arg;
165 			c->c_flags &= ~SCTP_CALLOUT_PENDING;
166 			SCTP_TIMERQ_UNLOCK();
167 			c_func(c_arg);
168 			SCTP_TIMERQ_LOCK();
169 			c = sctp_os_timer_next;
170 		} else {
171 			c = TAILQ_NEXT(c, tqe);
172 		}
173 	}
174 	sctp_os_timer_next = NULL;
175 	SCTP_TIMERQ_UNLOCK();
176 }
177 
178 #if defined(__APPLE__)
179 void
sctp_timeout(void * arg SCTP_UNUSED)180 sctp_timeout(void *arg SCTP_UNUSED)
181 {
182 	sctp_handle_tick(SCTP_BASE_VAR(sctp_main_timer_ticks));
183 	sctp_start_main_timer();
184 }
185 #endif
186 
187 #if defined(__Userspace__)
188 #define TIMEOUT_INTERVAL 10
189 
190 void *
user_sctp_timer_iterate(void * arg)191 user_sctp_timer_iterate(void *arg)
192 {
193 	sctp_userspace_set_threadname("SCTP timer");
194 	for (;;) {
195 #if defined (__Userspace_os_Windows)
196 		Sleep(TIMEOUT_INTERVAL);
197 #else
198 		struct timespec amount, remaining;
199 
200 		remaining.tv_sec = 0;
201 		remaining.tv_nsec = TIMEOUT_INTERVAL * 1000 * 1000;
202 		do {
203 			amount = remaining;
204 		} while (nanosleep(&amount, &remaining) == -1);
205 #endif
206 		if (atomic_cmpset_int(&SCTP_BASE_VAR(timer_thread_should_exit), 1, 1)) {
207 			break;
208 		}
209 		sctp_handle_tick(MSEC_TO_TICKS(TIMEOUT_INTERVAL));
210 	}
211 	return (NULL);
212 }
213 
214 void
sctp_start_timer(void)215 sctp_start_timer(void)
216 {
217 	/*
218 	 * No need to do SCTP_TIMERQ_LOCK_INIT();
219 	 * here, it is being done in sctp_pcb_init()
220 	 */
221 	int rc;
222 
223 	rc = sctp_userspace_thread_create(&SCTP_BASE_VAR(timer_thread), user_sctp_timer_iterate);
224 	if (rc) {
225 		SCTP_PRINTF("ERROR; return code from sctp_thread_create() is %d\n", rc);
226 	}
227 }
228 
229 #endif
230