xref: /freebsd/sbin/ipf/ipfsync/ipsyncm.c (revision 51e16cb8fc536913f490ac6bc9c17e92ebd0411b)
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 <unistd.h>
20*08ab34a0SCy Schubert #include <string.h>
21*08ab34a0SCy Schubert #include <syslog.h>
22*08ab34a0SCy Schubert #include <signal.h>
23*08ab34a0SCy Schubert 
24*08ab34a0SCy Schubert #include "netinet/ip_compat.h"
25*08ab34a0SCy Schubert #include "netinet/ip_fil.h"
26*08ab34a0SCy Schubert #include "netinet/ip_nat.h"
27*08ab34a0SCy Schubert #include "netinet/ip_state.h"
28*08ab34a0SCy Schubert #include "netinet/ip_sync.h"
29*08ab34a0SCy Schubert 
30*08ab34a0SCy Schubert 
31*08ab34a0SCy Schubert int	main(int, char *[]);
32*08ab34a0SCy Schubert void	usage(const char *);
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, "Usage: %s <destination IP> <destination port>\n", progname);
38*08ab34a0SCy Schubert }
39*08ab34a0SCy Schubert 
40*08ab34a0SCy Schubert #if 0
41*08ab34a0SCy Schubert static void handleterm(int sig)
42*08ab34a0SCy Schubert {
43*08ab34a0SCy Schubert 	terminate = sig;
44*08ab34a0SCy Schubert }
45*08ab34a0SCy Schubert #endif
46*08ab34a0SCy Schubert 
47*08ab34a0SCy Schubert 
48*08ab34a0SCy Schubert /* should be large enough to hold header + any datatype */
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 	struct sockaddr_in sin;
56*08ab34a0SCy Schubert 	char buff[BUFFERLEN];
57*08ab34a0SCy Schubert 	synclogent_t *sl;
58*08ab34a0SCy Schubert 	syncupdent_t *su;
59*08ab34a0SCy Schubert 	int nfd = -1, lfd = -1, n1, n2, n3, len;
60*08ab34a0SCy Schubert 	int inbuf;
61*08ab34a0SCy Schubert 	u_32_t magic;
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 
73*08ab34a0SCy Schubert 	if (argc < 2) {
74*08ab34a0SCy Schubert 		usage(progname);
75*08ab34a0SCy Schubert 		exit(1);
76*08ab34a0SCy Schubert 	}
77*08ab34a0SCy Schubert 
78*08ab34a0SCy Schubert #if 0
79*08ab34a0SCy Schubert        	signal(SIGHUP, handleterm);
80*08ab34a0SCy Schubert        	signal(SIGINT, handleterm);
81*08ab34a0SCy Schubert        	signal(SIGTERM, handleterm);
82*08ab34a0SCy Schubert #endif
83*08ab34a0SCy Schubert 
84*08ab34a0SCy Schubert 	openlog(progname, LOG_PID, LOG_SECURITY);
85*08ab34a0SCy Schubert 
86*08ab34a0SCy Schubert 	bzero((char *)&sin, sizeof(sin));
87*08ab34a0SCy Schubert 	sin.sin_family = AF_INET;
88*08ab34a0SCy Schubert 	sin.sin_addr.s_addr = inet_addr(argv[1]);
89*08ab34a0SCy Schubert 	if (argc > 2)
90*08ab34a0SCy Schubert 		sin.sin_port = htons(atoi(argv[2]));
91*08ab34a0SCy Schubert 	else
92*08ab34a0SCy Schubert 		sin.sin_port = htons(43434);
93*08ab34a0SCy Schubert 
94*08ab34a0SCy Schubert 	while (1) {
95*08ab34a0SCy Schubert 
96*08ab34a0SCy Schubert 		if (lfd != -1)
97*08ab34a0SCy Schubert 			close(lfd);
98*08ab34a0SCy Schubert 		if (nfd != -1)
99*08ab34a0SCy Schubert 			close(nfd);
100*08ab34a0SCy Schubert 
101*08ab34a0SCy Schubert 		lfd = open(IPSYNC_NAME, O_RDONLY);
102*08ab34a0SCy Schubert 		if (lfd == -1) {
103*08ab34a0SCy Schubert 			syslog(LOG_ERR, "Opening %s :%m", IPSYNC_NAME);
104*08ab34a0SCy Schubert 			goto tryagain;
105*08ab34a0SCy Schubert 		}
106*08ab34a0SCy Schubert 
107*08ab34a0SCy Schubert 		nfd = socket(AF_INET, SOCK_DGRAM, 0);
108*08ab34a0SCy Schubert 		if (nfd == -1) {
109*08ab34a0SCy Schubert 			syslog(LOG_ERR, "Socket :%m");
110*08ab34a0SCy Schubert 			goto tryagain;
111*08ab34a0SCy Schubert 		}
112*08ab34a0SCy Schubert 
113*08ab34a0SCy Schubert 		if (connect(nfd, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
114*08ab34a0SCy Schubert 			syslog(LOG_ERR, "Connect: %m");
115*08ab34a0SCy Schubert 			goto tryagain;
116*08ab34a0SCy Schubert 		}
117*08ab34a0SCy Schubert 
118*08ab34a0SCy Schubert 		syslog(LOG_INFO, "Sending data to %s",
119*08ab34a0SCy Schubert 		       inet_ntoa(sin.sin_addr));
120*08ab34a0SCy Schubert 
121*08ab34a0SCy Schubert 		inbuf = 0;
122*08ab34a0SCy Schubert 		while (1) {
123*08ab34a0SCy Schubert 
124*08ab34a0SCy Schubert 			n1 = read(lfd, buff+inbuf, BUFFERLEN-inbuf);
125*08ab34a0SCy Schubert 
126*08ab34a0SCy Schubert 			printf("header : %d bytes read (header = %d bytes)\n",
127*08ab34a0SCy Schubert 			       n1, (int) sizeof(*sh));
128*08ab34a0SCy Schubert 
129*08ab34a0SCy Schubert 			if (n1 < 0) {
130*08ab34a0SCy Schubert 				syslog(LOG_ERR, "Read error (header): %m");
131*08ab34a0SCy Schubert 				goto tryagain;
132*08ab34a0SCy Schubert 			}
133*08ab34a0SCy Schubert 
134*08ab34a0SCy Schubert 			if (n1 == 0) {
135*08ab34a0SCy Schubert 				/* XXX can this happen??? */
136*08ab34a0SCy Schubert 				syslog(LOG_ERR,
137*08ab34a0SCy Schubert 				       "Read error (header) : No data");
138*08ab34a0SCy Schubert 				sleep(1);
139*08ab34a0SCy Schubert 				continue;
140*08ab34a0SCy Schubert 			}
141*08ab34a0SCy Schubert 
142*08ab34a0SCy Schubert 			inbuf += n1;
143*08ab34a0SCy Schubert 
144*08ab34a0SCy Schubert moreinbuf:
145*08ab34a0SCy Schubert 			if (inbuf < sizeof(*sh)) {
146*08ab34a0SCy Schubert 				continue; /* need more data */
147*08ab34a0SCy Schubert 			}
148*08ab34a0SCy Schubert 
149*08ab34a0SCy Schubert 			sh = (synchdr_t *)buff;
150*08ab34a0SCy Schubert 			len = ntohl(sh->sm_len);
151*08ab34a0SCy Schubert 			magic = ntohl(sh->sm_magic);
152*08ab34a0SCy Schubert 
153*08ab34a0SCy Schubert 			if (magic != SYNHDRMAGIC) {
154*08ab34a0SCy Schubert 				syslog(LOG_ERR,
155*08ab34a0SCy Schubert 				       "Invalid header magic %x", magic);
156*08ab34a0SCy Schubert 				goto tryagain;
157*08ab34a0SCy Schubert 			}
158*08ab34a0SCy Schubert 
159*08ab34a0SCy Schubert #define IPSYNC_DEBUG
160*08ab34a0SCy Schubert #ifdef IPSYNC_DEBUG
161*08ab34a0SCy Schubert 			printf("v:%d p:%d len:%d magic:%x", sh->sm_v,
162*08ab34a0SCy Schubert 			       sh->sm_p, len, magic);
163*08ab34a0SCy Schubert 
164*08ab34a0SCy Schubert 			if (sh->sm_cmd == SMC_CREATE)
165*08ab34a0SCy Schubert 				printf(" cmd:CREATE");
166*08ab34a0SCy Schubert 			else if (sh->sm_cmd == SMC_UPDATE)
167*08ab34a0SCy Schubert 				printf(" cmd:UPDATE");
168*08ab34a0SCy Schubert 			else
169*08ab34a0SCy Schubert 				printf(" cmd:Unknown(%d)", sh->sm_cmd);
170*08ab34a0SCy Schubert 
171*08ab34a0SCy Schubert 			if (sh->sm_table == SMC_NAT)
172*08ab34a0SCy Schubert 				printf(" table:NAT");
173*08ab34a0SCy Schubert 			else if (sh->sm_table == SMC_STATE)
174*08ab34a0SCy Schubert 				printf(" table:STATE");
175*08ab34a0SCy Schubert 			else
176*08ab34a0SCy Schubert 				printf(" table:Unknown(%d)", sh->sm_table);
177*08ab34a0SCy Schubert 
178*08ab34a0SCy Schubert 			printf(" num:%d\n", (u_32_t)ntohl(sh->sm_num));
179*08ab34a0SCy Schubert #endif
180*08ab34a0SCy Schubert 
181*08ab34a0SCy Schubert 			if (inbuf < sizeof(*sh) + len) {
182*08ab34a0SCy Schubert 				continue; /* need more data */
183*08ab34a0SCy Schubert 				goto tryagain;
184*08ab34a0SCy Schubert 			}
185*08ab34a0SCy Schubert 
186*08ab34a0SCy Schubert #ifdef IPSYNC_DEBUG
187*08ab34a0SCy Schubert 			if (sh->sm_cmd == SMC_CREATE) {
188*08ab34a0SCy Schubert 				sl = (synclogent_t *)buff;
189*08ab34a0SCy Schubert 
190*08ab34a0SCy Schubert 			} else if (sh->sm_cmd == SMC_UPDATE) {
191*08ab34a0SCy Schubert 				su = (syncupdent_t *)buff;
192*08ab34a0SCy Schubert 				if (sh->sm_p == IPPROTO_TCP) {
193*08ab34a0SCy Schubert 					printf(" TCP Update: age %lu state %d/%d\n",
194*08ab34a0SCy Schubert 						su->sup_tcp.stu_age,
195*08ab34a0SCy Schubert 						su->sup_tcp.stu_state[0],
196*08ab34a0SCy Schubert 						su->sup_tcp.stu_state[1]);
197*08ab34a0SCy Schubert 				}
198*08ab34a0SCy Schubert 			} else {
199*08ab34a0SCy Schubert 				printf("Unknown command\n");
200*08ab34a0SCy Schubert 			}
201*08ab34a0SCy Schubert #endif
202*08ab34a0SCy Schubert 
203*08ab34a0SCy Schubert 			n2 = sizeof(*sh) + len;
204*08ab34a0SCy Schubert 			n3 = write(nfd, buff, n2);
205*08ab34a0SCy Schubert 			if (n3 <= 0) {
206*08ab34a0SCy Schubert 				syslog(LOG_ERR, "Write error: %m");
207*08ab34a0SCy Schubert 				goto tryagain;
208*08ab34a0SCy Schubert 			}
209*08ab34a0SCy Schubert 
210*08ab34a0SCy Schubert 
211*08ab34a0SCy Schubert 			if (n3 != n2) {
212*08ab34a0SCy Schubert 				syslog(LOG_ERR, "Incomplete write (%d/%d)",
213*08ab34a0SCy Schubert 				       n3, n2);
214*08ab34a0SCy Schubert 				goto tryagain;
215*08ab34a0SCy Schubert 			}
216*08ab34a0SCy Schubert 
217*08ab34a0SCy Schubert 			/* signal received? */
218*08ab34a0SCy Schubert 			if (terminate)
219*08ab34a0SCy Schubert 				break;
220*08ab34a0SCy Schubert 
221*08ab34a0SCy Schubert 			/* move buffer to the front,we might need to make
222*08ab34a0SCy Schubert 			 * this more efficient, by using a rolling pointer
223*08ab34a0SCy Schubert 			 * over the buffer and only copying it, when
224*08ab34a0SCy Schubert 			 * we are reaching the end
225*08ab34a0SCy Schubert 			 */
226*08ab34a0SCy Schubert 			inbuf -= n2;
227*08ab34a0SCy Schubert 			if (inbuf) {
228*08ab34a0SCy Schubert 				bcopy(buff+n2, buff, inbuf);
229*08ab34a0SCy Schubert 				printf("More data in buffer\n");
230*08ab34a0SCy Schubert 				goto moreinbuf;
231*08ab34a0SCy Schubert 			}
232*08ab34a0SCy Schubert 		}
233*08ab34a0SCy Schubert 
234*08ab34a0SCy Schubert 		if (terminate)
235*08ab34a0SCy Schubert 			break;
236*08ab34a0SCy Schubert tryagain:
237*08ab34a0SCy Schubert 		sleep(1);
238*08ab34a0SCy Schubert 	}
239*08ab34a0SCy Schubert 
240*08ab34a0SCy Schubert 
241*08ab34a0SCy Schubert 	/* terminate */
242*08ab34a0SCy Schubert 	if (lfd != -1)
243*08ab34a0SCy Schubert 		close(lfd);
244*08ab34a0SCy Schubert 	if (nfd != -1)
245*08ab34a0SCy Schubert 		close(nfd);
246*08ab34a0SCy Schubert 
247*08ab34a0SCy Schubert 	syslog(LOG_ERR, "signal %d received, exiting...", terminate);
248*08ab34a0SCy Schubert 
249*08ab34a0SCy Schubert 	exit(1);
250*08ab34a0SCy Schubert }
251*08ab34a0SCy Schubert 
252