tcp.c revision 1.8 1 1.8 itojun /* $NetBSD: tcp.c,v 1.8 2002/06/07 00:20:45 itojun Exp $ */
2 1.8 itojun /* $KAME: tcp.c,v 1.9 2002/05/26 01:17:02 itojun Exp $ */
3 1.1 itojun
4 1.1 itojun /*
5 1.1 itojun * Copyright (C) 1997 and 1998 WIDE Project.
6 1.1 itojun * All rights reserved.
7 1.2 itojun *
8 1.1 itojun * Redistribution and use in source and binary forms, with or without
9 1.1 itojun * modification, are permitted provided that the following conditions
10 1.1 itojun * are met:
11 1.1 itojun * 1. Redistributions of source code must retain the above copyright
12 1.1 itojun * notice, this list of conditions and the following disclaimer.
13 1.1 itojun * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 itojun * notice, this list of conditions and the following disclaimer in the
15 1.1 itojun * documentation and/or other materials provided with the distribution.
16 1.1 itojun * 3. Neither the name of the project nor the names of its contributors
17 1.1 itojun * may be used to endorse or promote products derived from this software
18 1.1 itojun * without specific prior written permission.
19 1.2 itojun *
20 1.1 itojun * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 1.1 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 1.1 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 1.1 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 1.1 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 1.1 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 1.1 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 1.1 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 1.1 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 1.1 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 1.1 itojun * SUCH DAMAGE.
31 1.1 itojun */
32 1.1 itojun
33 1.1 itojun #include <sys/param.h>
34 1.1 itojun #include <sys/types.h>
35 1.1 itojun #include <sys/socket.h>
36 1.1 itojun #include <sys/ioctl.h>
37 1.1 itojun #include <sys/time.h>
38 1.1 itojun #include <sys/wait.h>
39 1.1 itojun
40 1.1 itojun #include <stdio.h>
41 1.1 itojun #include <stdlib.h>
42 1.1 itojun #include <string.h>
43 1.1 itojun #include <syslog.h>
44 1.1 itojun #include <unistd.h>
45 1.1 itojun #include <errno.h>
46 1.1 itojun #include <fcntl.h>
47 1.1 itojun #include <signal.h>
48 1.1 itojun
49 1.1 itojun #include <netinet/in.h>
50 1.1 itojun #include <arpa/inet.h>
51 1.1 itojun #include <netdb.h>
52 1.1 itojun
53 1.1 itojun #include "faithd.h"
54 1.1 itojun
55 1.1 itojun static char tcpbuf[16*1024];
56 1.1 itojun /* bigger than MSS and may be lesser than window size */
57 1.1 itojun static int tblen, tboff, oob_exists;
58 1.1 itojun static fd_set readfds, writefds, exceptfds;
59 1.1 itojun static char atmark_buf[2];
60 1.1 itojun static pid_t cpid = (pid_t)0;
61 1.1 itojun static pid_t ppid = (pid_t)0;
62 1.7 itojun volatile time_t child_lastactive = (time_t)0;
63 1.1 itojun static time_t parent_lastactive = (time_t)0;
64 1.1 itojun
65 1.1 itojun static void sig_ctimeout __P((int));
66 1.1 itojun static void sig_child __P((int));
67 1.1 itojun static void notify_inactive __P((void));
68 1.1 itojun static void notify_active __P((void));
69 1.1 itojun static void send_data __P((int, int, const char *, int));
70 1.1 itojun static void relay __P((int, int, const char *, int));
71 1.1 itojun
72 1.1 itojun /*
73 1.1 itojun * Inactivity timer:
74 1.1 itojun * - child side (ppid != 0) will send SIGUSR1 to parent every (FAITH_TIMEOUT/4)
75 1.1 itojun * second if traffic is active. if traffic is inactive, don't send SIGUSR1.
76 1.1 itojun * - parent side (ppid == 0) will check the last SIGUSR1 it have seen.
77 1.1 itojun */
78 1.1 itojun static void
79 1.1 itojun sig_ctimeout(int sig)
80 1.1 itojun {
81 1.1 itojun /* parent side: record notification from the child */
82 1.1 itojun if (dflag)
83 1.1 itojun syslog(LOG_DEBUG, "activity timer from child");
84 1.1 itojun child_lastactive = time(NULL);
85 1.1 itojun }
86 1.1 itojun
87 1.1 itojun /* parent will terminate if child dies. */
88 1.1 itojun static void
89 1.1 itojun sig_child(int sig)
90 1.1 itojun {
91 1.1 itojun int status;
92 1.1 itojun pid_t pid;
93 1.1 itojun
94 1.1 itojun pid = wait3(&status, WNOHANG, (struct rusage *)0);
95 1.6 itojun if (pid > 0 && WEXITSTATUS(status))
96 1.8 itojun syslog(LOG_WARNING, "child %ld exit status 0x%x",
97 1.8 itojun (long)pid, status);
98 1.3 itojun exit_success("terminate connection due to child termination");
99 1.1 itojun }
100 1.1 itojun
101 1.1 itojun static void
102 1.1 itojun notify_inactive()
103 1.1 itojun {
104 1.1 itojun time_t t;
105 1.1 itojun
106 1.1 itojun /* only on parent side... */
107 1.1 itojun if (ppid)
108 1.1 itojun return;
109 1.1 itojun
110 1.1 itojun /* parent side should check for timeout. */
111 1.1 itojun t = time(NULL);
112 1.1 itojun if (dflag) {
113 1.1 itojun syslog(LOG_DEBUG, "parent side %sactive, child side %sactive",
114 1.1 itojun (FAITH_TIMEOUT < t - parent_lastactive) ? "in" : "",
115 1.1 itojun (FAITH_TIMEOUT < t - child_lastactive) ? "in" : "");
116 1.1 itojun }
117 1.1 itojun
118 1.1 itojun if (FAITH_TIMEOUT < t - child_lastactive
119 1.1 itojun && FAITH_TIMEOUT < t - parent_lastactive) {
120 1.1 itojun /* both side timeouted */
121 1.1 itojun signal(SIGCHLD, SIG_DFL);
122 1.1 itojun kill(cpid, SIGTERM);
123 1.1 itojun wait(NULL);
124 1.1 itojun exit_failure("connection timeout");
125 1.1 itojun /* NOTREACHED */
126 1.1 itojun }
127 1.1 itojun }
128 1.1 itojun
129 1.1 itojun static void
130 1.1 itojun notify_active()
131 1.1 itojun {
132 1.1 itojun if (ppid) {
133 1.1 itojun /* child side: notify parent of active traffic */
134 1.1 itojun time_t t;
135 1.1 itojun t = time(NULL);
136 1.1 itojun if (FAITH_TIMEOUT / 4 < t - child_lastactive) {
137 1.1 itojun if (kill(ppid, SIGUSR1) < 0) {
138 1.1 itojun exit_failure("terminate connection due to parent termination");
139 1.1 itojun /* NOTREACHED */
140 1.1 itojun }
141 1.1 itojun child_lastactive = t;
142 1.1 itojun }
143 1.1 itojun } else {
144 1.1 itojun /* parent side */
145 1.1 itojun parent_lastactive = time(NULL);
146 1.1 itojun }
147 1.1 itojun }
148 1.1 itojun
149 1.1 itojun static void
150 1.1 itojun send_data(int s_rcv, int s_snd, const char *service, int direction)
151 1.1 itojun {
152 1.1 itojun int cc;
153 1.1 itojun
154 1.1 itojun if (oob_exists) {
155 1.1 itojun cc = send(s_snd, atmark_buf, 1, MSG_OOB);
156 1.1 itojun if (cc == -1)
157 1.1 itojun goto retry_or_err;
158 1.1 itojun oob_exists = 0;
159 1.1 itojun FD_SET(s_rcv, &exceptfds);
160 1.1 itojun }
161 1.1 itojun
162 1.1 itojun for (; tboff < tblen; tboff += cc) {
163 1.1 itojun cc = write(s_snd, tcpbuf + tboff, tblen - tboff);
164 1.1 itojun if (cc < 0)
165 1.1 itojun goto retry_or_err;
166 1.1 itojun }
167 1.1 itojun #ifdef DEBUG
168 1.1 itojun if (tblen) {
169 1.1 itojun if (tblen >= sizeof(tcpbuf))
170 1.1 itojun tblen = sizeof(tcpbuf) - 1;
171 1.1 itojun tcpbuf[tblen] = '\0';
172 1.1 itojun syslog(LOG_DEBUG, "from %s (%dbytes): %s",
173 1.1 itojun direction == 1 ? "client" : "server", tblen, tcpbuf);
174 1.1 itojun }
175 1.1 itojun #endif /* DEBUG */
176 1.1 itojun tblen = 0; tboff = 0;
177 1.1 itojun FD_CLR(s_snd, &writefds);
178 1.1 itojun FD_SET(s_rcv, &readfds);
179 1.1 itojun return;
180 1.1 itojun retry_or_err:
181 1.1 itojun if (errno != EAGAIN)
182 1.5 itojun exit_failure("writing relay data failed: %s", strerror(errno));
183 1.1 itojun FD_SET(s_snd, &writefds);
184 1.1 itojun }
185 1.1 itojun
186 1.1 itojun static void
187 1.1 itojun relay(int s_rcv, int s_snd, const char *service, int direction)
188 1.1 itojun {
189 1.1 itojun int atmark, error, maxfd;
190 1.1 itojun struct timeval tv;
191 1.1 itojun fd_set oreadfds, owritefds, oexceptfds;
192 1.1 itojun
193 1.1 itojun FD_ZERO(&readfds);
194 1.1 itojun FD_ZERO(&writefds);
195 1.1 itojun FD_ZERO(&exceptfds);
196 1.1 itojun fcntl(s_snd, F_SETFD, O_NONBLOCK);
197 1.1 itojun oreadfds = readfds; owritefds = writefds; oexceptfds = exceptfds;
198 1.4 itojun FD_SET(s_rcv, &readfds);
199 1.4 itojun FD_SET(s_rcv, &exceptfds);
200 1.1 itojun oob_exists = 0;
201 1.1 itojun maxfd = (s_rcv > s_snd) ? s_rcv : s_snd;
202 1.1 itojun
203 1.1 itojun for (;;) {
204 1.1 itojun tv.tv_sec = FAITH_TIMEOUT / 4;
205 1.1 itojun tv.tv_usec = 0;
206 1.1 itojun oreadfds = readfds;
207 1.1 itojun owritefds = writefds;
208 1.1 itojun oexceptfds = exceptfds;
209 1.1 itojun error = select(maxfd + 1, &readfds, &writefds, &exceptfds, &tv);
210 1.1 itojun if (error == -1) {
211 1.1 itojun if (errno == EINTR)
212 1.1 itojun continue;
213 1.5 itojun exit_failure("select: %s", strerror(errno));
214 1.1 itojun } else if (error == 0) {
215 1.1 itojun readfds = oreadfds;
216 1.1 itojun writefds = owritefds;
217 1.1 itojun exceptfds = oexceptfds;
218 1.1 itojun notify_inactive();
219 1.1 itojun continue;
220 1.1 itojun }
221 1.1 itojun
222 1.1 itojun /* activity notification */
223 1.1 itojun notify_active();
224 1.1 itojun
225 1.1 itojun if (FD_ISSET(s_rcv, &exceptfds)) {
226 1.1 itojun error = ioctl(s_rcv, SIOCATMARK, &atmark);
227 1.1 itojun if (error != -1 && atmark == 1) {
228 1.1 itojun int cc;
229 1.1 itojun oob_read_retry:
230 1.1 itojun cc = read(s_rcv, atmark_buf, 1);
231 1.1 itojun if (cc == 1) {
232 1.1 itojun FD_CLR(s_rcv, &exceptfds);
233 1.1 itojun FD_SET(s_snd, &writefds);
234 1.1 itojun oob_exists = 1;
235 1.1 itojun } else if (cc == -1) {
236 1.1 itojun if (errno == EINTR)
237 1.1 itojun goto oob_read_retry;
238 1.1 itojun exit_failure("reading oob data failed"
239 1.1 itojun ": %s",
240 1.5 itojun strerror(errno));
241 1.1 itojun }
242 1.1 itojun }
243 1.1 itojun }
244 1.1 itojun if (FD_ISSET(s_rcv, &readfds)) {
245 1.1 itojun relaydata_read_retry:
246 1.1 itojun tblen = read(s_rcv, tcpbuf, sizeof(tcpbuf));
247 1.1 itojun tboff = 0;
248 1.1 itojun
249 1.1 itojun switch (tblen) {
250 1.1 itojun case -1:
251 1.1 itojun if (errno == EINTR)
252 1.1 itojun goto relaydata_read_retry;
253 1.1 itojun exit_failure("reading relay data failed: %s",
254 1.5 itojun strerror(errno));
255 1.1 itojun /* NOTREACHED */
256 1.1 itojun case 0:
257 1.1 itojun /* to close opposite-direction relay process */
258 1.1 itojun shutdown(s_snd, 0);
259 1.1 itojun
260 1.1 itojun close(s_rcv);
261 1.1 itojun close(s_snd);
262 1.1 itojun exit_success("terminating %s relay", service);
263 1.1 itojun /* NOTREACHED */
264 1.1 itojun default:
265 1.1 itojun FD_CLR(s_rcv, &readfds);
266 1.1 itojun FD_SET(s_snd, &writefds);
267 1.1 itojun break;
268 1.1 itojun }
269 1.1 itojun }
270 1.1 itojun if (FD_ISSET(s_snd, &writefds))
271 1.1 itojun send_data(s_rcv, s_snd, service, direction);
272 1.1 itojun }
273 1.1 itojun }
274 1.1 itojun
275 1.1 itojun void
276 1.1 itojun tcp_relay(int s_src, int s_dst, const char *service)
277 1.1 itojun {
278 1.1 itojun syslog(LOG_INFO, "starting %s relay", service);
279 1.1 itojun
280 1.1 itojun child_lastactive = parent_lastactive = time(NULL);
281 1.1 itojun
282 1.1 itojun cpid = fork();
283 1.1 itojun switch (cpid) {
284 1.1 itojun case -1:
285 1.5 itojun exit_failure("tcp_relay: can't fork grand child: %s",
286 1.5 itojun strerror(errno));
287 1.1 itojun /* NOTREACHED */
288 1.1 itojun case 0:
289 1.1 itojun /* child process: relay going traffic */
290 1.1 itojun ppid = getppid();
291 1.1 itojun /* this is child so reopen log */
292 1.1 itojun closelog();
293 1.1 itojun openlog(logname, LOG_PID | LOG_NOWAIT, LOG_DAEMON);
294 1.1 itojun relay(s_src, s_dst, service, 1);
295 1.1 itojun /* NOTREACHED */
296 1.1 itojun default:
297 1.1 itojun /* parent process: relay coming traffic */
298 1.1 itojun ppid = (pid_t)0;
299 1.1 itojun signal(SIGUSR1, sig_ctimeout);
300 1.1 itojun signal(SIGCHLD, sig_child);
301 1.1 itojun relay(s_dst, s_src, service, 0);
302 1.1 itojun /* NOTREACHED */
303 1.1 itojun }
304 1.1 itojun }
305