xref: /src/sbin/ipf/ipfsync/ipsyncm.c (revision 51e16cb8fc536913f490ac6bc9c17e92ebd0411b)
108ab34a0SCy Schubert 
208ab34a0SCy Schubert /*
308ab34a0SCy Schubert  * Copyright (C) 2012 by Darren Reed.
408ab34a0SCy Schubert  *
508ab34a0SCy Schubert  * See the IPFILTER.LICENCE file for details on licencing.
608ab34a0SCy Schubert  */
708ab34a0SCy Schubert #include <sys/types.h>
808ab34a0SCy Schubert #include <sys/time.h>
908ab34a0SCy Schubert #include <sys/socket.h>
1008ab34a0SCy Schubert 
1108ab34a0SCy Schubert #include <netinet/in.h>
1208ab34a0SCy Schubert #include <net/if.h>
1308ab34a0SCy Schubert 
1408ab34a0SCy Schubert #include <arpa/inet.h>
1508ab34a0SCy Schubert 
1608ab34a0SCy Schubert #include <stdio.h>
1708ab34a0SCy Schubert #include <stdlib.h>
1808ab34a0SCy Schubert #include <fcntl.h>
1908ab34a0SCy Schubert #include <unistd.h>
2008ab34a0SCy Schubert #include <string.h>
2108ab34a0SCy Schubert #include <syslog.h>
2208ab34a0SCy Schubert #include <signal.h>
2308ab34a0SCy Schubert 
2408ab34a0SCy Schubert #include "netinet/ip_compat.h"
2508ab34a0SCy Schubert #include "netinet/ip_fil.h"
2608ab34a0SCy Schubert #include "netinet/ip_nat.h"
2708ab34a0SCy Schubert #include "netinet/ip_state.h"
2808ab34a0SCy Schubert #include "netinet/ip_sync.h"
2908ab34a0SCy Schubert 
3008ab34a0SCy Schubert 
3108ab34a0SCy Schubert int	main(int, char *[]);
3208ab34a0SCy Schubert void	usage(const char *);
3308ab34a0SCy Schubert 
3408ab34a0SCy Schubert int	terminate = 0;
3508ab34a0SCy Schubert 
usage(const char * progname)3608ab34a0SCy Schubert void usage(const char *progname) {
3708ab34a0SCy Schubert 	fprintf(stderr, "Usage: %s <destination IP> <destination port>\n", progname);
3808ab34a0SCy Schubert }
3908ab34a0SCy Schubert 
4008ab34a0SCy Schubert #if 0
4108ab34a0SCy Schubert static void handleterm(int sig)
4208ab34a0SCy Schubert {
4308ab34a0SCy Schubert 	terminate = sig;
4408ab34a0SCy Schubert }
4508ab34a0SCy Schubert #endif
4608ab34a0SCy Schubert 
4708ab34a0SCy Schubert 
4808ab34a0SCy Schubert /* should be large enough to hold header + any datatype */
4908ab34a0SCy Schubert #define BUFFERLEN 1400
5008ab34a0SCy Schubert 
main(argc,argv)5108ab34a0SCy Schubert int main(argc, argv)
5208ab34a0SCy Schubert 	int argc;
5308ab34a0SCy Schubert 	char *argv[];
5408ab34a0SCy Schubert {
5508ab34a0SCy Schubert 	struct sockaddr_in sin;
5608ab34a0SCy Schubert 	char buff[BUFFERLEN];
5708ab34a0SCy Schubert 	synclogent_t *sl;
5808ab34a0SCy Schubert 	syncupdent_t *su;
5908ab34a0SCy Schubert 	int nfd = -1, lfd = -1, n1, n2, n3, len;
6008ab34a0SCy Schubert 	int inbuf;
6108ab34a0SCy Schubert 	u_32_t magic;
6208ab34a0SCy Schubert 	synchdr_t *sh;
6308ab34a0SCy Schubert 	char *progname;
6408ab34a0SCy Schubert 
6508ab34a0SCy Schubert 	progname = strrchr(argv[0], '/');
6608ab34a0SCy Schubert 	if (progname) {
6708ab34a0SCy Schubert 		progname++;
6808ab34a0SCy Schubert 	} else {
6908ab34a0SCy Schubert 		progname = argv[0];
7008ab34a0SCy Schubert 	}
7108ab34a0SCy Schubert 
7208ab34a0SCy Schubert 
7308ab34a0SCy Schubert 	if (argc < 2) {
7408ab34a0SCy Schubert 		usage(progname);
7508ab34a0SCy Schubert 		exit(1);
7608ab34a0SCy Schubert 	}
7708ab34a0SCy Schubert 
7808ab34a0SCy Schubert #if 0
7908ab34a0SCy Schubert        	signal(SIGHUP, handleterm);
8008ab34a0SCy Schubert        	signal(SIGINT, handleterm);
8108ab34a0SCy Schubert        	signal(SIGTERM, handleterm);
8208ab34a0SCy Schubert #endif
8308ab34a0SCy Schubert 
8408ab34a0SCy Schubert 	openlog(progname, LOG_PID, LOG_SECURITY);
8508ab34a0SCy Schubert 
8608ab34a0SCy Schubert 	bzero((char *)&sin, sizeof(sin));
8708ab34a0SCy Schubert 	sin.sin_family = AF_INET;
8808ab34a0SCy Schubert 	sin.sin_addr.s_addr = inet_addr(argv[1]);
8908ab34a0SCy Schubert 	if (argc > 2)
9008ab34a0SCy Schubert 		sin.sin_port = htons(atoi(argv[2]));
9108ab34a0SCy Schubert 	else
9208ab34a0SCy Schubert 		sin.sin_port = htons(43434);
9308ab34a0SCy Schubert 
9408ab34a0SCy Schubert 	while (1) {
9508ab34a0SCy Schubert 
9608ab34a0SCy Schubert 		if (lfd != -1)
9708ab34a0SCy Schubert 			close(lfd);
9808ab34a0SCy Schubert 		if (nfd != -1)
9908ab34a0SCy Schubert 			close(nfd);
10008ab34a0SCy Schubert 
10108ab34a0SCy Schubert 		lfd = open(IPSYNC_NAME, O_RDONLY);
10208ab34a0SCy Schubert 		if (lfd == -1) {
10308ab34a0SCy Schubert 			syslog(LOG_ERR, "Opening %s :%m", IPSYNC_NAME);
10408ab34a0SCy Schubert 			goto tryagain;
10508ab34a0SCy Schubert 		}
10608ab34a0SCy Schubert 
10708ab34a0SCy Schubert 		nfd = socket(AF_INET, SOCK_DGRAM, 0);
10808ab34a0SCy Schubert 		if (nfd == -1) {
10908ab34a0SCy Schubert 			syslog(LOG_ERR, "Socket :%m");
11008ab34a0SCy Schubert 			goto tryagain;
11108ab34a0SCy Schubert 		}
11208ab34a0SCy Schubert 
11308ab34a0SCy Schubert 		if (connect(nfd, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
11408ab34a0SCy Schubert 			syslog(LOG_ERR, "Connect: %m");
11508ab34a0SCy Schubert 			goto tryagain;
11608ab34a0SCy Schubert 		}
11708ab34a0SCy Schubert 
11808ab34a0SCy Schubert 		syslog(LOG_INFO, "Sending data to %s",
11908ab34a0SCy Schubert 		       inet_ntoa(sin.sin_addr));
12008ab34a0SCy Schubert 
12108ab34a0SCy Schubert 		inbuf = 0;
12208ab34a0SCy Schubert 		while (1) {
12308ab34a0SCy Schubert 
12408ab34a0SCy Schubert 			n1 = read(lfd, buff+inbuf, BUFFERLEN-inbuf);
12508ab34a0SCy Schubert 
12608ab34a0SCy Schubert 			printf("header : %d bytes read (header = %d bytes)\n",
12708ab34a0SCy Schubert 			       n1, (int) sizeof(*sh));
12808ab34a0SCy Schubert 
12908ab34a0SCy Schubert 			if (n1 < 0) {
13008ab34a0SCy Schubert 				syslog(LOG_ERR, "Read error (header): %m");
13108ab34a0SCy Schubert 				goto tryagain;
13208ab34a0SCy Schubert 			}
13308ab34a0SCy Schubert 
13408ab34a0SCy Schubert 			if (n1 == 0) {
13508ab34a0SCy Schubert 				/* XXX can this happen??? */
13608ab34a0SCy Schubert 				syslog(LOG_ERR,
13708ab34a0SCy Schubert 				       "Read error (header) : No data");
13808ab34a0SCy Schubert 				sleep(1);
13908ab34a0SCy Schubert 				continue;
14008ab34a0SCy Schubert 			}
14108ab34a0SCy Schubert 
14208ab34a0SCy Schubert 			inbuf += n1;
14308ab34a0SCy Schubert 
14408ab34a0SCy Schubert moreinbuf:
14508ab34a0SCy Schubert 			if (inbuf < sizeof(*sh)) {
14608ab34a0SCy Schubert 				continue; /* need more data */
14708ab34a0SCy Schubert 			}
14808ab34a0SCy Schubert 
14908ab34a0SCy Schubert 			sh = (synchdr_t *)buff;
15008ab34a0SCy Schubert 			len = ntohl(sh->sm_len);
15108ab34a0SCy Schubert 			magic = ntohl(sh->sm_magic);
15208ab34a0SCy Schubert 
15308ab34a0SCy Schubert 			if (magic != SYNHDRMAGIC) {
15408ab34a0SCy Schubert 				syslog(LOG_ERR,
15508ab34a0SCy Schubert 				       "Invalid header magic %x", magic);
15608ab34a0SCy Schubert 				goto tryagain;
15708ab34a0SCy Schubert 			}
15808ab34a0SCy Schubert 
15908ab34a0SCy Schubert #define IPSYNC_DEBUG
16008ab34a0SCy Schubert #ifdef IPSYNC_DEBUG
16108ab34a0SCy Schubert 			printf("v:%d p:%d len:%d magic:%x", sh->sm_v,
16208ab34a0SCy Schubert 			       sh->sm_p, len, magic);
16308ab34a0SCy Schubert 
16408ab34a0SCy Schubert 			if (sh->sm_cmd == SMC_CREATE)
16508ab34a0SCy Schubert 				printf(" cmd:CREATE");
16608ab34a0SCy Schubert 			else if (sh->sm_cmd == SMC_UPDATE)
16708ab34a0SCy Schubert 				printf(" cmd:UPDATE");
16808ab34a0SCy Schubert 			else
16908ab34a0SCy Schubert 				printf(" cmd:Unknown(%d)", sh->sm_cmd);
17008ab34a0SCy Schubert 
17108ab34a0SCy Schubert 			if (sh->sm_table == SMC_NAT)
17208ab34a0SCy Schubert 				printf(" table:NAT");
17308ab34a0SCy Schubert 			else if (sh->sm_table == SMC_STATE)
17408ab34a0SCy Schubert 				printf(" table:STATE");
17508ab34a0SCy Schubert 			else
17608ab34a0SCy Schubert 				printf(" table:Unknown(%d)", sh->sm_table);
17708ab34a0SCy Schubert 
17808ab34a0SCy Schubert 			printf(" num:%d\n", (u_32_t)ntohl(sh->sm_num));
17908ab34a0SCy Schubert #endif
18008ab34a0SCy Schubert 
18108ab34a0SCy Schubert 			if (inbuf < sizeof(*sh) + len) {
18208ab34a0SCy Schubert 				continue; /* need more data */
18308ab34a0SCy Schubert 				goto tryagain;
18408ab34a0SCy Schubert 			}
18508ab34a0SCy Schubert 
18608ab34a0SCy Schubert #ifdef IPSYNC_DEBUG
18708ab34a0SCy Schubert 			if (sh->sm_cmd == SMC_CREATE) {
18808ab34a0SCy Schubert 				sl = (synclogent_t *)buff;
18908ab34a0SCy Schubert 
19008ab34a0SCy Schubert 			} else if (sh->sm_cmd == SMC_UPDATE) {
19108ab34a0SCy Schubert 				su = (syncupdent_t *)buff;
19208ab34a0SCy Schubert 				if (sh->sm_p == IPPROTO_TCP) {
19308ab34a0SCy Schubert 					printf(" TCP Update: age %lu state %d/%d\n",
19408ab34a0SCy Schubert 						su->sup_tcp.stu_age,
19508ab34a0SCy Schubert 						su->sup_tcp.stu_state[0],
19608ab34a0SCy Schubert 						su->sup_tcp.stu_state[1]);
19708ab34a0SCy Schubert 				}
19808ab34a0SCy Schubert 			} else {
19908ab34a0SCy Schubert 				printf("Unknown command\n");
20008ab34a0SCy Schubert 			}
20108ab34a0SCy Schubert #endif
20208ab34a0SCy Schubert 
20308ab34a0SCy Schubert 			n2 = sizeof(*sh) + len;
20408ab34a0SCy Schubert 			n3 = write(nfd, buff, n2);
20508ab34a0SCy Schubert 			if (n3 <= 0) {
20608ab34a0SCy Schubert 				syslog(LOG_ERR, "Write error: %m");
20708ab34a0SCy Schubert 				goto tryagain;
20808ab34a0SCy Schubert 			}
20908ab34a0SCy Schubert 
21008ab34a0SCy Schubert 
21108ab34a0SCy Schubert 			if (n3 != n2) {
21208ab34a0SCy Schubert 				syslog(LOG_ERR, "Incomplete write (%d/%d)",
21308ab34a0SCy Schubert 				       n3, n2);
21408ab34a0SCy Schubert 				goto tryagain;
21508ab34a0SCy Schubert 			}
21608ab34a0SCy Schubert 
21708ab34a0SCy Schubert 			/* signal received? */
21808ab34a0SCy Schubert 			if (terminate)
21908ab34a0SCy Schubert 				break;
22008ab34a0SCy Schubert 
22108ab34a0SCy Schubert 			/* move buffer to the front,we might need to make
22208ab34a0SCy Schubert 			 * this more efficient, by using a rolling pointer
22308ab34a0SCy Schubert 			 * over the buffer and only copying it, when
22408ab34a0SCy Schubert 			 * we are reaching the end
22508ab34a0SCy Schubert 			 */
22608ab34a0SCy Schubert 			inbuf -= n2;
22708ab34a0SCy Schubert 			if (inbuf) {
22808ab34a0SCy Schubert 				bcopy(buff+n2, buff, inbuf);
22908ab34a0SCy Schubert 				printf("More data in buffer\n");
23008ab34a0SCy Schubert 				goto moreinbuf;
23108ab34a0SCy Schubert 			}
23208ab34a0SCy Schubert 		}
23308ab34a0SCy Schubert 
23408ab34a0SCy Schubert 		if (terminate)
23508ab34a0SCy Schubert 			break;
23608ab34a0SCy Schubert tryagain:
23708ab34a0SCy Schubert 		sleep(1);
23808ab34a0SCy Schubert 	}
23908ab34a0SCy Schubert 
24008ab34a0SCy Schubert 
24108ab34a0SCy Schubert 	/* terminate */
24208ab34a0SCy Schubert 	if (lfd != -1)
24308ab34a0SCy Schubert 		close(lfd);
24408ab34a0SCy Schubert 	if (nfd != -1)
24508ab34a0SCy Schubert 		close(nfd);
24608ab34a0SCy Schubert 
24708ab34a0SCy Schubert 	syslog(LOG_ERR, "signal %d received, exiting...", terminate);
24808ab34a0SCy Schubert 
24908ab34a0SCy Schubert 	exit(1);
25008ab34a0SCy Schubert }
25108ab34a0SCy Schubert 
252