xref: /dragonfly/sbin/ipfw3/ipfw3sync.c (revision 017817f0)
1 /*
2  * Copyright (c) 2016 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Bill Yuan <bycn82@dragonflybsd.org>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #include <sys/param.h>
36 #include <sys/mbuf.h>
37 #include <sys/socket.h>
38 #include <sys/sockio.h>
39 #include <sys/sysctl.h>
40 #include <sys/time.h>
41 #include <sys/wait.h>
42 
43 #include <arpa/inet.h>
44 #include <ctype.h>
45 #include <dlfcn.h>
46 #include <err.h>
47 #include <errno.h>
48 #include <grp.h>
49 #include <limits.h>
50 #include <netdb.h>
51 #include <pwd.h>
52 #include <sysexits.h>
53 #include <signal.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <stdarg.h>
57 #include <string.h>
58 #include <timeconv.h>
59 #include <unistd.h>
60 
61 #include <netinet/in.h>
62 #include <netinet/in_systm.h>
63 #include <netinet/ip.h>
64 #include <netinet/ip_icmp.h>
65 #include <netinet/tcp.h>
66 #include <net/if.h>
67 #include <net/if_dl.h>
68 #include <net/route.h>
69 #include <net/ethernet.h>
70 
71 #include "../../sys/net/ipfw3/ip_fw3.h"
72 #include "../../sys/net/ipfw3/ip_fw3_table.h"
73 #include "../../sys/net/ipfw3/ip_fw3_sync.h"
74 #include "../../sys/net/dummynet3/ip_dummynet3.h"
75 #include "../../sys/net/libalias/alias.h"
76 #include "../../sys/net/ipfw3_basic/ip_fw3_basic.h"
77 #include "../../sys/net/ipfw3_nat/ip_fw3_nat.h"
78 
79 #include "ipfw3.h"
80 #include "ipfw3sync.h"
81 
82 void
83 sync_config_edge(int ac, char *av[])
84 {
85 	struct ipfw_ioc_sync_edge ioc_edge;
86 	NEXT_ARG;
87 	if (isdigit(**av)) {
88 		ioc_edge.port = atoi(*av);
89 		if (ioc_edge.port == 0) {
90 			errx(EX_USAGE, "invalid edge port `%s'", *av);
91 		}
92 		NEXT_ARG;
93 		if (strcmp(*av, "all") == 0) {
94 			ioc_edge.hw_same = 1;
95 		} else {
96 			ioc_edge.hw_same = 0;
97 		}
98 		if(do_set_x(IP_FW_SYNC_EDGE_CONF,
99 				&ioc_edge, sizeof(ioc_edge)) < 0) {
100 			err(EX_UNAVAILABLE, "do_set_x(IP_FW_SYNC_EDGE_CONF)");
101 		}
102 	} else {
103 		errx(EX_USAGE, "invalid edge port `%s'", *av);
104 	}
105 }
106 
107 void
108 sync_config_centre(int ac, char *av[])
109 {
110 	struct ipfw_ioc_sync_centre *centre;
111 	struct ipfw_sync_edge *edge;
112 	struct in_addr addr;
113 	char *tok;
114 	char *str;
115 	int count = 0, step = 10, len, data_len;
116 
117 	void *data = NULL;
118 
119 	NEXT_ARG;
120 	tok = strtok(*av, ",");
121 	len = sizeof(int);
122 
123 	data_len = len + step * sizeof(struct ipfw_sync_edge);
124 	data = malloc(data_len);
125 	centre = (struct ipfw_ioc_sync_centre *)data;
126 	edge = centre->edges;
127 	while (tok != NULL) {
128 		str = strchr(tok,':');
129 		if (str != NULL) {
130 			*(str++) = '\0';
131 			edge->port = (u_short)strtoul(str, NULL, 0);
132 			if (edge->port == 0) {
133 				errx(EX_USAGE, "edge `%s:%s' invalid",
134 						tok, str);
135 			}
136 		} else {
137 			err(EX_UNAVAILABLE, "dst invalid");
138 		}
139 		inet_aton(tok, &addr);
140 		edge->addr = addr.s_addr;
141 		if (count >= step) {
142 			step += 10;
143 			data_len = len + step * sizeof(struct ipfw_sync_edge);
144 			if ((data = realloc(data, data_len)) == NULL) {
145 				err(EX_OSERR, "realloc in config sync centre");
146 			}
147 		}
148 
149 		tok = strtok (NULL, ",");
150 		count++;
151 		edge++;
152 	}
153 	if (count > MAX_EDGES) {
154 		err(EX_OSERR,"too much edges");
155 	}
156 	centre->count = count;
157 	len += count * sizeof(struct ipfw_sync_edge);
158 	if(do_set_x(IP_FW_SYNC_CENTRE_CONF, data, len) < 0) {
159 		err(EX_UNAVAILABLE, "do_set_x(IP_FW_SYNC_CENTRE_CONF)");
160 	}
161 
162 }
163 
164 void
165 sync_show_config(int ac, char *av[])
166 {
167 	void *data = NULL;
168 	int nalloc = 1000, nbytes;
169 	nbytes = nalloc;
170 
171 	while (nbytes >= nalloc) {
172 		nalloc = nalloc * 2 + 321;
173 		nbytes = nalloc;
174 		if (data == NULL) {
175 			if ((data = malloc(nbytes)) == NULL) {
176 				err(EX_OSERR, "malloc");
177 			}
178 		} else if ((data = realloc(data, nbytes)) == NULL) {
179 			err(EX_OSERR, "realloc");
180 		}
181 		if (do_get_x(IP_FW_SYNC_SHOW_CONF, data, &nbytes) < 0) {
182 			err(EX_OSERR, "getsockopt(IP_FW_SYNC_SHOW_CONF)");
183 		}
184 	}
185 	struct ipfw_ioc_sync_context *sync_ctx;
186 	sync_ctx = (struct ipfw_ioc_sync_context *)data;
187 	if (sync_ctx->edge_port != 0) {
188 		printf("ipfw3sync edge on %d %s\n", sync_ctx->edge_port,
189 				sync_ctx->hw_same == 1 ? "all" : "");
190 	}
191 	if (sync_ctx->count > 0) {
192 		struct ipfw_sync_edge *edge;
193 		int i;
194 
195 		edge = sync_ctx->edges;
196 		printf("ipfw3sync centre to %d edge(s)\n", sync_ctx->count);
197 		for (i = 0; i < sync_ctx->count; i++) {
198 			struct in_addr in;
199 			in.s_addr = edge->addr;
200 			printf("edge on %s:%d\n", inet_ntoa(in), edge->port);
201 			edge++;
202 		}
203 	}
204 
205 }
206 
207 void
208 sync_show_status(int ac, char *av[])
209 {
210 	int running, len;
211 	len = sizeof(running);
212 	if (do_get_x(IP_FW_SYNC_SHOW_STATUS, &running, &len) < 0) {
213 		err(EX_OSERR, "getsockopt(IP_FW_SYNC_SHOW_STATUS)");
214 	}
215 	if (running & 1) {
216 		printf("edge is running\n");
217 	}
218 	if (running & 2) {
219 		printf("centre is running\n");
220 	}
221 }
222 
223 void
224 sync_edge_start(int ac, char *av[])
225 {
226 	int i = 0;
227 	if(do_set_x(IP_FW_SYNC_EDGE_START, &i, sizeof(i)) < 0) {
228 		err(EX_UNAVAILABLE, "do_set_x(IP_FW_SYNC_EDGE_START)");
229 	}
230 }
231 
232 void
233 sync_centre_start(int ac, char *av[])
234 {
235 	int i = 0;
236 	if(do_set_x(IP_FW_SYNC_CENTRE_START, &i, sizeof(i)) < 0) {
237 		err(EX_UNAVAILABLE, "do_set_x(IP_FW_SYNC_CENTRE_START)");
238 	}
239 }
240 
241 void
242 sync_edge_stop(int ac, char *av[])
243 {
244 	int i = 0;
245 	if(do_set_x(IP_FW_SYNC_EDGE_STOP, &i, sizeof(i)) < 0) {
246 		err(EX_UNAVAILABLE, "do_set_x(IP_FW_SYNC_EDGE_STOP)");
247 	}
248 }
249 
250 void
251 sync_centre_stop(int ac, char *av[])
252 {
253 	int i = 0;
254 	if(do_set_x(IP_FW_SYNC_CENTRE_STOP, &i, sizeof(i)) < 0) {
255 		err(EX_UNAVAILABLE, "do_set_x(IP_FW_SYNC_CENTRE_STOP");
256 	}
257 }
258 
259 void
260 sync_edge_clear(int ac, char *av[])
261 {
262 	int i = 0;
263 	if(do_set_x(IP_FW_SYNC_EDGE_CLEAR, &i, sizeof(i)) < 0) {
264 		err(EX_UNAVAILABLE, "do_set_x(IP_FW_SYNC_EDGE_CLEAR)");
265 	}
266 }
267 
268 void
269 sync_centre_clear(int ac, char *av[])
270 {
271 	int i = 0;
272 	if(do_set_x(IP_FW_SYNC_CENTRE_CLEAR, &i, sizeof(i)) < 0) {
273 		err(EX_UNAVAILABLE, "do_set_x(IP_FW_SYNC_CENTRE_CLEAR)");
274 	}
275 }
276 
277 void
278 sync_edge_test(int ac, char *av[])
279 {
280 	int i = 0;
281 	if(do_set_x(IP_FW_SYNC_EDGE_TEST, &i, sizeof(i)) < 0) {
282 		err(EX_UNAVAILABLE, "do_set_x(IP_FW_SYNC_EDGE_CLEAR)");
283 	}
284 }
285 
286 void
287 sync_centre_test(int ac, char *av[])
288 {
289 	int n;
290 	NEXT_ARG;
291 	if (!isdigit(**av)) {
292 		errx(EX_DATAERR, "invalid test number %s\n", *av);
293 	}
294 	n = atoi(*av);
295 	if(do_set_x(IP_FW_SYNC_CENTRE_TEST, &n, sizeof(n)) < 0) {
296 		err(EX_UNAVAILABLE, "do_set_x(IP_FW_SYNC_CENTRE_TEST)");
297 	}
298 	printf("centre test %d sent\n", n);
299 }
300