1*13885a66Sdarrenr /* $NetBSD: ipsyncs.c,v 1.2 2012/07/22 14:27:51 darrenr Exp $ */
2bc4097aaSchristos
3bc4097aaSchristos /*
4c9d5dc6cSdarrenr * Copyright (C) 2012 by Darren Reed.
5bc4097aaSchristos *
6bc4097aaSchristos * See the IPFILTER.LICENCE file for details on licencing.
7bc4097aaSchristos */
8bc4097aaSchristos #if !defined(lint)
9bc4097aaSchristos static const char sccsid[] = "@(#)ip_fil.c 2.41 6/5/96 (C) 1993-2000 Darren Reed";
10*13885a66Sdarrenr static const char rcsid[] = "@(#)Id: ipsyncs.c,v 1.1.1.2 2012/07/22 13:44:58 darrenr Exp $";
11bc4097aaSchristos #endif
12bc4097aaSchristos #include <sys/types.h>
13bc4097aaSchristos #include <sys/time.h>
14bc4097aaSchristos #include <sys/socket.h>
15bc4097aaSchristos
16bc4097aaSchristos #include <netinet/in.h>
17bc4097aaSchristos #include <net/if.h>
18bc4097aaSchristos
19bc4097aaSchristos #include <arpa/inet.h>
20bc4097aaSchristos
21bc4097aaSchristos #include <stdio.h>
22bc4097aaSchristos #include <stdlib.h>
23bc4097aaSchristos #include <fcntl.h>
24bc4097aaSchristos #include <string.h>
25bc4097aaSchristos #include <unistd.h>
26bc4097aaSchristos #include <syslog.h>
27bc4097aaSchristos #include <errno.h>
28bc4097aaSchristos #include <signal.h>
29bc4097aaSchristos
30bc4097aaSchristos #include "netinet/ip_compat.h"
31bc4097aaSchristos #include "netinet/ip_fil.h"
32bc4097aaSchristos #include "netinet/ip_state.h"
33bc4097aaSchristos #include "netinet/ip_nat.h"
34bc4097aaSchristos #include "netinet/ip_sync.h"
35bc4097aaSchristos
36bc4097aaSchristos int main __P((int, char *[]));
37bc4097aaSchristos void usage __P((const char *progname));
38bc4097aaSchristos
39bc4097aaSchristos int terminate = 0;
40bc4097aaSchristos
usage(const char * progname)41bc4097aaSchristos void usage(const char *progname) {
42bc4097aaSchristos fprintf(stderr,
43bc4097aaSchristos "Usage: %s <destination IP> <destination port> [remote IP]\n",
44bc4097aaSchristos progname);
45bc4097aaSchristos }
46bc4097aaSchristos
47bc4097aaSchristos #if 0
48bc4097aaSchristos static void handleterm(int sig)
49bc4097aaSchristos {
50bc4097aaSchristos terminate = sig;
51bc4097aaSchristos }
52bc4097aaSchristos #endif
53bc4097aaSchristos
54bc4097aaSchristos #define BUFFERLEN 1400
55bc4097aaSchristos
main(argc,argv)56bc4097aaSchristos int main(argc, argv)
57bc4097aaSchristos int argc;
58bc4097aaSchristos char *argv[];
59bc4097aaSchristos {
60bc4097aaSchristos int nfd = -1 , lfd = -1;
61bc4097aaSchristos int n1, n2, n3, magic, len, inbuf;
62bc4097aaSchristos struct sockaddr_in sin;
63bc4097aaSchristos struct sockaddr_in in;
64bc4097aaSchristos char buff[BUFFERLEN];
65bc4097aaSchristos synclogent_t *sl;
66bc4097aaSchristos syncupdent_t *su;
67bc4097aaSchristos synchdr_t *sh;
68bc4097aaSchristos char *progname;
69bc4097aaSchristos
70bc4097aaSchristos progname = strrchr(argv[0], '/');
71bc4097aaSchristos if (progname) {
72bc4097aaSchristos progname++;
73bc4097aaSchristos } else {
74bc4097aaSchristos progname = argv[0];
75bc4097aaSchristos }
76bc4097aaSchristos
77bc4097aaSchristos if (argc < 2) {
78bc4097aaSchristos usage(progname);
79bc4097aaSchristos exit(1);
80bc4097aaSchristos }
81bc4097aaSchristos
82bc4097aaSchristos #if 0
83bc4097aaSchristos signal(SIGHUP, handleterm);
84bc4097aaSchristos signal(SIGINT, handleterm);
85bc4097aaSchristos signal(SIGTERM, handleterm);
86bc4097aaSchristos #endif
87bc4097aaSchristos
88bc4097aaSchristos openlog(progname, LOG_PID, LOG_SECURITY);
89bc4097aaSchristos
90bc4097aaSchristos lfd = open(IPSYNC_NAME, O_WRONLY);
91bc4097aaSchristos if (lfd == -1) {
92bc4097aaSchristos syslog(LOG_ERR, "Opening %s :%m", IPSYNC_NAME);
93bc4097aaSchristos exit(1);
94bc4097aaSchristos }
95bc4097aaSchristos
96bc4097aaSchristos bzero((char *)&sin, sizeof(sin));
97bc4097aaSchristos sin.sin_family = AF_INET;
98bc4097aaSchristos if (argc > 1)
99bc4097aaSchristos sin.sin_addr.s_addr = inet_addr(argv[1]);
100bc4097aaSchristos if (argc > 2)
101bc4097aaSchristos sin.sin_port = htons(atoi(argv[2]));
102bc4097aaSchristos else
103bc4097aaSchristos sin.sin_port = htons(43434);
104bc4097aaSchristos if (argc > 3)
105bc4097aaSchristos in.sin_addr.s_addr = inet_addr(argv[3]);
106bc4097aaSchristos else
107bc4097aaSchristos in.sin_addr.s_addr = 0;
108bc4097aaSchristos in.sin_port = 0;
109bc4097aaSchristos
110bc4097aaSchristos while(1) {
111bc4097aaSchristos
112bc4097aaSchristos if (lfd != -1)
113bc4097aaSchristos close(lfd);
114bc4097aaSchristos if (nfd != -1)
115bc4097aaSchristos close(nfd);
116bc4097aaSchristos
117bc4097aaSchristos lfd = open(IPSYNC_NAME, O_WRONLY);
118bc4097aaSchristos if (lfd == -1) {
119bc4097aaSchristos syslog(LOG_ERR, "Opening %s :%m", IPSYNC_NAME);
120bc4097aaSchristos goto tryagain;
121bc4097aaSchristos }
122bc4097aaSchristos
123bc4097aaSchristos nfd = socket(AF_INET, SOCK_DGRAM, 0);
124bc4097aaSchristos if (nfd == -1) {
125bc4097aaSchristos syslog(LOG_ERR, "Socket :%m");
126bc4097aaSchristos goto tryagain;
127bc4097aaSchristos }
128bc4097aaSchristos
129bc4097aaSchristos n1 = 1;
130bc4097aaSchristos setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, &n1, sizeof(n1));
131bc4097aaSchristos
132bc4097aaSchristos if (bind(nfd, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
133bc4097aaSchristos syslog(LOG_ERR, "Bind: %m");
134bc4097aaSchristos goto tryagain;
135bc4097aaSchristos }
136bc4097aaSchristos
137bc4097aaSchristos syslog(LOG_INFO, "Listening to %s", inet_ntoa(sin.sin_addr));
138bc4097aaSchristos
139bc4097aaSchristos inbuf = 0;
140bc4097aaSchristos while (1) {
141bc4097aaSchristos
142bc4097aaSchristos
143bc4097aaSchristos /*
144bc4097aaSchristos * XXX currently we do not check the source address
145bc4097aaSchristos * of a datagram, this can be a security risk
146bc4097aaSchristos */
147bc4097aaSchristos n1 = read(nfd, buff+inbuf, BUFFERLEN-inbuf);
148bc4097aaSchristos
149bc4097aaSchristos printf("header : %d bytes read (header = %d bytes)\n",
150bc4097aaSchristos n1, (int) sizeof(*sh));
151bc4097aaSchristos
152bc4097aaSchristos if (n1 < 0) {
153bc4097aaSchristos syslog(LOG_ERR, "Read error (header): %m");
154bc4097aaSchristos goto tryagain;
155bc4097aaSchristos }
156bc4097aaSchristos
157bc4097aaSchristos if (n1 == 0) {
158bc4097aaSchristos /* XXX can this happen??? */
159bc4097aaSchristos syslog(LOG_ERR,
160bc4097aaSchristos "Read error (header) : No data");
161bc4097aaSchristos sleep(1);
162bc4097aaSchristos continue;
163bc4097aaSchristos }
164bc4097aaSchristos
165bc4097aaSchristos inbuf += n1;
166bc4097aaSchristos
167bc4097aaSchristos moreinbuf:
168bc4097aaSchristos if (inbuf < sizeof(*sh)) {
169bc4097aaSchristos continue; /* need more data */
170bc4097aaSchristos }
171bc4097aaSchristos
172bc4097aaSchristos sh = (synchdr_t *)buff;
173bc4097aaSchristos len = ntohl(sh->sm_len);
174bc4097aaSchristos magic = ntohl(sh->sm_magic);
175bc4097aaSchristos
176bc4097aaSchristos if (magic != SYNHDRMAGIC) {
177bc4097aaSchristos syslog(LOG_ERR, "Invalid header magic %x",
178bc4097aaSchristos magic);
179bc4097aaSchristos goto tryagain;
180bc4097aaSchristos }
181bc4097aaSchristos
182bc4097aaSchristos #define IPSYNC_DEBUG
183bc4097aaSchristos #ifdef IPSYNC_DEBUG
184bc4097aaSchristos printf("v:%d p:%d len:%d magic:%x", sh->sm_v,
185bc4097aaSchristos sh->sm_p, len, magic);
186bc4097aaSchristos
187bc4097aaSchristos if (sh->sm_cmd == SMC_CREATE)
188bc4097aaSchristos printf(" cmd:CREATE");
189bc4097aaSchristos else if (sh->sm_cmd == SMC_UPDATE)
190bc4097aaSchristos printf(" cmd:UPDATE");
191bc4097aaSchristos else
192bc4097aaSchristos printf(" cmd:Unknown(%d)", sh->sm_cmd);
193bc4097aaSchristos
194bc4097aaSchristos if (sh->sm_table == SMC_NAT)
195bc4097aaSchristos printf(" table:NAT");
196bc4097aaSchristos else if (sh->sm_table == SMC_STATE)
197bc4097aaSchristos printf(" table:STATE");
198bc4097aaSchristos else
199bc4097aaSchristos printf(" table:Unknown(%d)", sh->sm_table);
200bc4097aaSchristos
201bc4097aaSchristos printf(" num:%d\n", (u_32_t)ntohl(sh->sm_num));
202bc4097aaSchristos #endif
203bc4097aaSchristos
204bc4097aaSchristos if (inbuf < sizeof(*sh) + len) {
205bc4097aaSchristos continue; /* need more data */
206bc4097aaSchristos goto tryagain;
207bc4097aaSchristos }
208bc4097aaSchristos
209bc4097aaSchristos #ifdef IPSYNC_DEBUG
210bc4097aaSchristos if (sh->sm_cmd == SMC_CREATE) {
211bc4097aaSchristos sl = (synclogent_t *)buff;
212bc4097aaSchristos
213bc4097aaSchristos } else if (sh->sm_cmd == SMC_UPDATE) {
214bc4097aaSchristos su = (syncupdent_t *)buff;
215bc4097aaSchristos if (sh->sm_p == IPPROTO_TCP) {
216bc4097aaSchristos printf(" TCP Update: age %lu state %d/%d\n",
217bc4097aaSchristos su->sup_tcp.stu_age,
218bc4097aaSchristos su->sup_tcp.stu_state[0],
219bc4097aaSchristos su->sup_tcp.stu_state[1]);
220bc4097aaSchristos }
221bc4097aaSchristos } else {
222bc4097aaSchristos printf("Unknown command\n");
223bc4097aaSchristos }
224bc4097aaSchristos #endif
225bc4097aaSchristos
226bc4097aaSchristos n2 = sizeof(*sh) + len;
227bc4097aaSchristos n3 = write(lfd, buff, n2);
228bc4097aaSchristos if (n3 <= 0) {
229bc4097aaSchristos syslog(LOG_ERR, "%s: Write error: %m",
230bc4097aaSchristos IPSYNC_NAME);
231bc4097aaSchristos goto tryagain;
232bc4097aaSchristos }
233bc4097aaSchristos
234bc4097aaSchristos
235bc4097aaSchristos if (n3 != n2) {
236bc4097aaSchristos syslog(LOG_ERR, "%s: Incomplete write (%d/%d)",
237bc4097aaSchristos IPSYNC_NAME, n3, n2);
238bc4097aaSchristos goto tryagain;
239bc4097aaSchristos }
240bc4097aaSchristos
241bc4097aaSchristos /* signal received? */
242bc4097aaSchristos if (terminate)
243bc4097aaSchristos break;
244bc4097aaSchristos
245bc4097aaSchristos /* move buffer to the front,we might need to make
246bc4097aaSchristos * this more efficient, by using a rolling pointer
247bc4097aaSchristos * over the buffer and only copying it, when
248bc4097aaSchristos * we are reaching the end
249bc4097aaSchristos */
250bc4097aaSchristos inbuf -= n2;
251bc4097aaSchristos if (inbuf) {
252bc4097aaSchristos bcopy(buff+n2, buff, inbuf);
253bc4097aaSchristos printf("More data in buffer\n");
254bc4097aaSchristos goto moreinbuf;
255bc4097aaSchristos }
256bc4097aaSchristos }
257bc4097aaSchristos
258bc4097aaSchristos if (terminate)
259bc4097aaSchristos break;
260bc4097aaSchristos tryagain:
261bc4097aaSchristos sleep(1);
262bc4097aaSchristos }
263bc4097aaSchristos
264bc4097aaSchristos
265bc4097aaSchristos /* terminate */
266bc4097aaSchristos if (lfd != -1)
267bc4097aaSchristos close(lfd);
268bc4097aaSchristos if (nfd != -1)
269bc4097aaSchristos close(nfd);
270bc4097aaSchristos
271bc4097aaSchristos syslog(LOG_ERR, "signal %d received, exiting...", terminate);
272bc4097aaSchristos
273bc4097aaSchristos exit(1);
274bc4097aaSchristos }
275