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