2 * Copyright (c) 2016 - 2018 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Bill Yuan <bycn82@dragonflybsd.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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
35 #include <sys/param.h>
37 #include <sys/socket.h>
38 #include <sys/sockio.h>
39 #include <sys/sysctl.h>
43 #include <arpa/inet.h>
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>
67 #include <net/if_dl.h>
68 #include <net/route.h>
69 #include <net/ethernet.h>
71 #include <net/ipfw3/ip_fw3.h>
72 #include <net/ipfw3_basic/ip_fw3_table.h>
73 #include <net/ipfw3_basic/ip_fw3_sync.h>
74 #include <net/ipfw3_basic/ip_fw3_basic.h>
75 #include <net/ipfw3_nat/ip_fw3_nat.h>
76 #include <net/dummynet3/ip_dummynet3.h>
79 #include "ipfw3sync.h"
82 sync_config_edge(int ac
, char *av
[])
84 struct ipfw3_ioc_sync_edge ioc_edge
;
87 ioc_edge
.port
= atoi(*av
);
88 if (ioc_edge
.port
== 0) {
89 errx(EX_USAGE
, "invalid edge port `%s'", *av
);
92 if (strcmp(*av
, "all") == 0) {
97 if(do_set_x(IP_FW_SYNC_EDGE_CONF
,
98 &ioc_edge
, sizeof(ioc_edge
)) < 0) {
99 err(EX_UNAVAILABLE
, "do_set_x(IP_FW_SYNC_EDGE_CONF)");
102 errx(EX_USAGE
, "invalid edge port `%s'", *av
);
107 sync_config_centre(int ac
, char *av
[])
109 struct ipfw3_ioc_sync_centre
*centre
;
110 struct ipfw3_sync_edge
*edge
;
114 int count
= 0, step
= 10, len
, data_len
;
119 tok
= strtok(*av
, ",");
122 data_len
= len
+ step
* LEN_SYNC_EDGE
;
123 data
= malloc(data_len
);
124 centre
= (struct ipfw3_ioc_sync_centre
*)data
;
125 edge
= centre
->edges
;
126 while (tok
!= NULL
) {
127 str
= strchr(tok
,':');
130 edge
->port
= (u_short
)strtoul(str
, NULL
, 0);
131 if (edge
->port
== 0) {
132 errx(EX_USAGE
, "edge `%s:%s' invalid",
136 err(EX_UNAVAILABLE
, "dst invalid");
138 inet_aton(tok
, &addr
);
139 edge
->addr
= addr
.s_addr
;
142 data_len
= len
+ step
* LEN_SYNC_EDGE
;
143 if ((data
= realloc(data
, data_len
)) == NULL
) {
144 err(EX_OSERR
, "realloc in config sync centre");
148 tok
= strtok (NULL
, ",");
152 if (count
> MAX_EDGES
) {
153 err(EX_OSERR
,"too much edges");
155 centre
->count
= count
;
156 len
+= count
* LEN_SYNC_EDGE
;
157 if(do_set_x(IP_FW_SYNC_CENTRE_CONF
, data
, len
) < 0) {
158 err(EX_UNAVAILABLE
, "do_set_x(IP_FW_SYNC_CENTRE_CONF)");
164 sync_show_config(int ac
, char *av
[])
167 int nalloc
= 1000, nbytes
;
170 while (nbytes
>= nalloc
) {
171 nalloc
= nalloc
* 2 + 321;
174 if ((data
= malloc(nbytes
)) == NULL
) {
175 err(EX_OSERR
, "malloc");
177 } else if ((data
= realloc(data
, nbytes
)) == NULL
) {
178 err(EX_OSERR
, "realloc");
180 if (do_get_x(IP_FW_SYNC_SHOW_CONF
, data
, &nbytes
) < 0) {
181 err(EX_OSERR
, "getsockopt(IP_FW_SYNC_SHOW_CONF)");
184 struct ipfw3_ioc_sync_context
*sync_ctx
;
185 sync_ctx
= (struct ipfw3_ioc_sync_context
*)data
;
186 if (sync_ctx
->edge_port
!= 0) {
187 printf("ipfw3sync edge on %d %s\n", sync_ctx
->edge_port
,
188 sync_ctx
->hw_same
== 1 ? "all" : "");
190 if (sync_ctx
->count
> 0) {
191 struct ipfw3_sync_edge
*edge
;
194 edge
= sync_ctx
->edges
;
195 printf("ipfw3sync centre to %d edge(s)\n", sync_ctx
->count
);
196 for (i
= 0; i
< sync_ctx
->count
; i
++) {
198 in
.s_addr
= edge
->addr
;
199 printf("edge on %s:%d\n", inet_ntoa(in
), edge
->port
);
206 sync_show_status(int ac
, char *av
[])
209 len
= sizeof(running
);
210 if (do_get_x(IP_FW_SYNC_SHOW_STATUS
, &running
, &len
) < 0) {
211 err(EX_OSERR
, "getsockopt(IP_FW_SYNC_SHOW_STATUS)");
214 printf("edge is running\n");
217 printf("centre is running\n");
222 sync_edge_start(int ac
, char *av
[])
225 if(do_set_x(IP_FW_SYNC_EDGE_START
, &i
, sizeof(i
)) < 0) {
226 err(EX_UNAVAILABLE
, "do_set_x(IP_FW_SYNC_EDGE_START)");
231 sync_centre_start(int ac
, char *av
[])
234 if(do_set_x(IP_FW_SYNC_CENTRE_START
, &i
, sizeof(i
)) < 0) {
235 err(EX_UNAVAILABLE
, "do_set_x(IP_FW_SYNC_CENTRE_START)");
240 sync_edge_stop(int ac
, char *av
[])
243 if(do_set_x(IP_FW_SYNC_EDGE_STOP
, &i
, sizeof(i
)) < 0) {
244 err(EX_UNAVAILABLE
, "do_set_x(IP_FW_SYNC_EDGE_STOP)");
249 sync_centre_stop(int ac
, char *av
[])
252 if(do_set_x(IP_FW_SYNC_CENTRE_STOP
, &i
, sizeof(i
)) < 0) {
253 err(EX_UNAVAILABLE
, "do_set_x(IP_FW_SYNC_CENTRE_STOP");
258 sync_edge_clear(int ac
, char *av
[])
261 if(do_set_x(IP_FW_SYNC_EDGE_CLEAR
, &i
, sizeof(i
)) < 0) {
262 err(EX_UNAVAILABLE
, "do_set_x(IP_FW_SYNC_EDGE_CLEAR)");
267 sync_centre_clear(int ac
, char *av
[])
270 if(do_set_x(IP_FW_SYNC_CENTRE_CLEAR
, &i
, sizeof(i
)) < 0) {
271 err(EX_UNAVAILABLE
, "do_set_x(IP_FW_SYNC_CENTRE_CLEAR)");
276 sync_edge_test(int ac
, char *av
[])
279 if(do_set_x(IP_FW_SYNC_EDGE_TEST
, &i
, sizeof(i
)) < 0) {
280 err(EX_UNAVAILABLE
, "do_set_x(IP_FW_SYNC_EDGE_CLEAR)");
285 sync_centre_test(int ac
, char *av
[])
289 if (!isdigit(**av
)) {
290 errx(EX_DATAERR
, "invalid test number %s\n", *av
);
293 if(do_set_x(IP_FW_SYNC_CENTRE_TEST
, &n
, sizeof(n
)) < 0) {
294 err(EX_UNAVAILABLE
, "do_set_x(IP_FW_SYNC_CENTRE_TEST)");
296 printf("centre test %d sent\n", n
);
301 sync_main(int ac
, char **av
)
303 if (!strncmp(*av
, "edge", strlen(*av
))) {
304 sync_config_edge(ac
, av
);
305 } else if (!strncmp(*av
, "centre", strlen(*av
))) {
306 sync_config_centre(ac
, av
);
307 } else if (!strncmp(*av
, "show", strlen(*av
))) {
309 if (!strncmp(*av
, "config", strlen(*av
))) {
310 sync_show_config(ac
, av
);
311 } else if (!strncmp(*av
, "status", strlen(*av
))) {
312 sync_show_status(ac
, av
);
314 errx(EX_USAGE
, "bad show command `%s'", *av
);
316 } else if (!strncmp(*av
, "start", strlen(*av
))) {
318 if (!strncmp(*av
, "edge", strlen(*av
))) {
319 sync_edge_start(ac
, av
);
320 } else if (!strncmp(*av
, "centre", strlen(*av
))) {
321 sync_centre_start(ac
, av
);
323 } else if (!strncmp(*av
, "stop", strlen(*av
))) {
325 if (!strncmp(*av
, "edge", strlen(*av
))) {
326 sync_edge_stop(ac
, av
);
327 } else if (!strncmp(*av
, "centre", strlen(*av
))) {
328 sync_centre_stop(ac
, av
);
330 } else if (!strncmp(*av
, "clear", strlen(*av
))) {
332 if (!strncmp(*av
, "edge", strlen(*av
))) {
333 sync_edge_clear(ac
, av
);
334 } else if (!strncmp(*av
, "centre", strlen(*av
))) {
335 sync_centre_clear(ac
, av
);
337 } else if (!strncmp(*av
, "test", strlen(*av
))) {
339 if (!strncmp(*av
, "edge", strlen(*av
))) {
340 sync_edge_test(ac
, av
);
341 } else if (!strncmp(*av
, "centre", strlen(*av
))) {
342 sync_centre_test(ac
, av
);
345 errx(EX_USAGE
, "bad ipfw sync command `%s'", *av
);