2 * Copyright (c) 2016 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
36 #include <sys/types.h>
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/malloc.h>
41 #include <sys/kernel.h>
43 #include <sys/socket.h>
44 #include <sys/socketvar.h>
45 #include <sys/socketvar2.h>
46 #include <sys/socketops.h>
47 #include <sys/sysctl.h>
48 #include <sys/syslog.h>
49 #include <sys/ucred.h>
50 #include <sys/in_cksum.h>
52 #include <sys/kthread.h>
53 #include <sys/thread2.h>
54 #include <sys/mplock2.h>
56 #include <netinet/in.h>
57 #include <netinet/in_systm.h>
58 #include <netinet/in_var.h>
59 #include <netinet/in_pcb.h>
60 #include <netinet/ip.h>
61 #include <netinet/ip_var.h>
62 #include <netinet/ip_icmp.h>
63 #include <netinet/tcp.h>
64 #include <netinet/tcp_timer.h>
65 #include <netinet/tcp_var.h>
66 #include <netinet/tcpip.h>
67 #include <netinet/udp.h>
68 #include <netinet/udp_var.h>
69 #include <netinet/ip_divert.h>
70 #include <netinet/if_ether.h>
73 #include <net/route.h>
75 #include <net/netmsg2.h>
76 #include <net/ethernet.h>
78 #include <net/ipfw3/ip_fw.h>
79 #include <net/ipfw3/ip_fw3_sync.h>
81 MALLOC_DEFINE(M_IPFW3_SYNC
, "IPFW3_SYNC", "mem for ipfw3sync");
83 extern struct ipfw_context
*ipfw_ctx
[MAXCPU
];
84 extern struct ipfw_sync_context sync_ctx
;
85 ipfw_sync_send_state_t
*ipfw_sync_send_state_prt
= NULL
;
86 ipfw_sync_install_state_t
*ipfw_sync_install_state_prt
= NULL
;
89 * ipfw3sync show config
92 ipfw_ctl_sync_show_conf(struct sockopt
*sopt
)
94 struct ipfw_ioc_sync_context
*tmp_sync_ctx
;
97 size
= 3 * sizeof(int) + sync_ctx
.count
* sizeof(struct ipfw_sync_edge
);
98 if (sopt
->sopt_valsize
< size
) {
99 /* sopt_val is not big enough */
100 bzero(sopt
->sopt_val
, sopt
->sopt_valsize
);
103 tmp_sync_ctx
= (struct ipfw_ioc_sync_context
*)sopt
->sopt_val
;
104 tmp_sync_ctx
->edge_port
= sync_ctx
.edge_port
;
105 tmp_sync_ctx
->hw_same
= sync_ctx
.hw_same
;
106 tmp_sync_ctx
->count
= sync_ctx
.count
;
107 bcopy(sync_ctx
.edges
, tmp_sync_ctx
->edges
,
108 sync_ctx
.count
* sizeof(struct ipfw_sync_edge
));
109 sopt
->sopt_valsize
= size
;
114 * ipfw3sync show status
117 ipfw_ctl_sync_show_status(struct sockopt
*sopt
)
120 running
= (int *)sopt
->sopt_val
;
121 *running
= sync_ctx
.running
;
122 sopt
->sopt_valsize
= sizeof(int);
126 * ipfw3sync config centre
129 ipfw_ctl_sync_centre_conf(struct sockopt
*sopt
)
131 struct ipfw_ioc_sync_centre
*ioc_centre
;
134 ioc_centre
= sopt
->sopt_val
;
135 size
= ioc_centre
->count
* sizeof(struct ipfw_sync_edge
);
136 if (sync_ctx
.count
== 0) {
137 sync_ctx
.edges
= kmalloc(size
, M_IPFW3_SYNC
, M_NOWAIT
| M_ZERO
);
139 sync_ctx
.edges
= krealloc(sync_ctx
.edges
,
140 size
, M_TEMP
, M_WAITOK
);
142 sync_ctx
.count
= ioc_centre
->count
;
143 bcopy(ioc_centre
->edges
, sync_ctx
.edges
,
144 ioc_centre
->count
* sizeof(struct ipfw_sync_edge
));
149 * ipfw3sync config edge
152 ipfw_ctl_sync_edge_conf(struct sockopt
*sopt
)
154 struct ipfw_ioc_sync_edge
*ioc_edge
;
159 size
= sopt
->sopt_valsize
;
160 ioc_edge
= sopt
->sopt_val
;
161 if (size
!= sizeof(struct ipfw_ioc_sync_edge
)) {
164 sync_ctx
.edge_port
= ioc_edge
->port
;
165 sync_ctx
.hw_same
= ioc_edge
->hw_same
;
167 td
= curthread
->td_proc
? curthread
: &thread0
;
168 error
= socreate(AF_INET
, &sync_ctx
.edge_sock
,
169 SOCK_DGRAM
, IPPROTO_UDP
, td
);
171 kprintf("ipfw3sync edge socreate failed: %d\n", error
);
178 sync_edge_socket_handler(void *dummy
)
182 struct sockaddr_in sin
;
185 int error
, flags
, *type
;
187 so
= sync_ctx
.edge_sock
;
188 flags
= MSG_FBLOCKING
;
190 bzero(&sin
, sizeof(struct sockaddr_in
));
191 sin
.sin_family
= AF_INET
;
192 sin
.sin_port
= htons(sync_ctx
.edge_port
);
193 sin
.sin_len
= sizeof(struct sockaddr_in
);
194 sa
= (struct sockaddr
*)&sin
;
195 while (sync_ctx
.running
& 1) {
196 sbinit(&sio
, 1000000000);
197 error
= so_pru_soreceive(so
, NULL
, NULL
, &sio
, NULL
, &flags
);
201 type
= (int *)m
->m_data
;
202 if (*type
== SYNC_TYPE_SEND_TEST
) {
203 struct cmd_send_test
*cmd
;
204 cmd
= (struct cmd_send_test
*)m
->m_data
;
205 kprintf("test received %d\n", cmd
->num
);
206 } else if (*type
== SYNC_TYPE_SEND_STATE
) {
207 struct cmd_send_state
*cmd
;
208 cmd
= (struct cmd_send_state
*)m
->m_data
;
209 if (ipfw_sync_install_state_prt
!= NULL
) {
210 (*ipfw_sync_install_state_prt
)(cmd
);
212 } else if (*type
== SYNC_TYPE_SEND_NAT
) {
213 /* TODO sync NAT records */
214 kprintf("nat received\n");
216 kprintf("Error ignore\n");
219 soshutdown(sync_ctx
.edge_sock
, SHUT_RD
);
220 sofree(sync_ctx
.edge_sock
);
225 ipfw_ctl_sync_edge_start(struct sockopt
*sopt
)
227 struct sockaddr_in sin
;
231 if (sync_ctx
.running
& 1) {
234 td
= curthread
->td_proc
? curthread
: &thread0
;
235 bzero(&sin
, sizeof(struct sockaddr_in
));
236 sin
.sin_family
= AF_INET
;
237 sin
.sin_len
= sizeof(struct sockaddr_in
);
238 sin
.sin_port
= htons(sync_ctx
.edge_port
);
239 sin
.sin_addr
.s_addr
= INADDR_ANY
;
240 error
= sobind(sync_ctx
.edge_sock
, (struct sockaddr
*)&sin
, td
);
242 if (error
!= EADDRINUSE
) {
243 kprintf("ipfw3sync edge sobind failed: %d\n", error
);
245 kprintf("ipfw3sync edge address in use: %d\n", error
);
250 sync_ctx
.running
|= 1;
251 soreference(sync_ctx
.edge_sock
);
252 error
= kthread_create(sync_edge_socket_handler
, NULL
,
253 &sync_ctx
.edge_td
, "sync_edge_thread");
255 panic("sync_edge_socket_handler:error %d",error
);
261 ipfw_ctl_sync_centre_start(struct sockopt
*sopt
)
263 struct sockaddr_in sin
;
265 struct ipfw_sync_edge
*edge
;
268 sync_ctx
.running
|= 2;
269 td
= curthread
->td_proc
? curthread
: &thread0
;
271 for (i
= 0; i
< sync_ctx
.count
; i
++) {
272 error
= socreate(AF_INET
, &sync_ctx
.centre_socks
[i
],
273 SOCK_DGRAM
, IPPROTO_UDP
, td
);
275 kprintf("ipfw3sync centre socreate failed: %d\n",
279 edge
= sync_ctx
.edges
;
281 bzero(&sin
, sizeof(struct sockaddr_in
));
282 sin
.sin_family
= AF_INET
;
283 sin
.sin_port
= htons(edge
->port
);
284 sin
.sin_addr
.s_addr
= edge
->addr
;
285 sin
.sin_len
= sizeof(struct sockaddr_in
);
286 error
= soconnect(sync_ctx
.centre_socks
[i
],
287 (struct sockaddr
*)&sin
, td
, TRUE
);
289 kprintf("ipfw3sync: centre soconnect failed: %d\n",
299 ipfw_ctl_sync_edge_test(struct sockopt
*sopt
)
305 ipfw_ctl_sync_centre_test(struct sockopt
*sopt
)
307 struct cmd_send_test cmd
;
310 int error
, i
, len
, nsize
, *num
;
312 if (sopt
->sopt_valsize
!= sizeof(int)) {
313 kprintf("ipfw3sync: invalid centre test parameter\n");
316 if ((sync_ctx
.running
& 2) == 0) {
317 kprintf("ipfw3sync: centre not running\n");
320 num
= sopt
->sopt_val
;
321 len
= sizeof(struct cmd_send_test
);
322 m
= m_getl(len
, M_WAITOK
, MT_DATA
, M_PKTHDR
, &nsize
);
324 kprintf("ipfw3sync: MGET failed\n");
329 memcpy(m
->m_data
, &cmd
, len
);
332 m
->m_pkthdr
.len
= len
;
334 td
= curthread
->td_proc
? curthread
: &thread0
;
335 for (i
= 0; i
< sync_ctx
.count
; i
++) {
336 error
= so_pru_sosend(sync_ctx
.centre_socks
[i
],
337 NULL
, NULL
, m
, NULL
, 0 ,td
);
339 kprintf("ipfw3sync: centre sosend failed: %d\n", error
);
347 ipfw_ctl_sync_edge_stop(struct sockopt
*sopt
)
349 if (sync_ctx
.running
& 1) {
350 sync_ctx
.running
&= 2;
351 soclose(sync_ctx
.edge_sock
, 0);
357 ipfw_ctl_sync_centre_stop(struct sockopt
*sopt
)
361 if (sync_ctx
.running
& 2) {
362 sync_ctx
.running
&= 1;
363 for (i
= 0; i
< sync_ctx
.count
; i
++) {
364 soclose(sync_ctx
.centre_socks
[i
], 0);
371 ipfw_ctl_sync_edge_clear(struct sockopt
*sopt
)
377 ipfw_ctl_sync_centre_clear(struct sockopt
*sopt
)
386 ipfw_ctl_sync_sockopt(struct sockopt
*sopt
)
389 switch (sopt
->sopt_name
) {
390 case IP_FW_SYNC_EDGE_CONF
:
391 error
= ipfw_ctl_sync_edge_conf(sopt
);
393 case IP_FW_SYNC_CENTRE_CONF
:
394 error
= ipfw_ctl_sync_centre_conf(sopt
);
396 case IP_FW_SYNC_SHOW_CONF
:
397 error
= ipfw_ctl_sync_show_conf(sopt
);
399 case IP_FW_SYNC_SHOW_STATUS
:
400 error
= ipfw_ctl_sync_show_status(sopt
);
402 case IP_FW_SYNC_EDGE_START
:
403 error
= ipfw_ctl_sync_edge_start(sopt
);
405 case IP_FW_SYNC_CENTRE_START
:
406 error
= ipfw_ctl_sync_centre_start(sopt
);
408 case IP_FW_SYNC_EDGE_STOP
:
409 error
= ipfw_ctl_sync_edge_stop(sopt
);
411 case IP_FW_SYNC_CENTRE_STOP
:
412 error
= ipfw_ctl_sync_centre_stop(sopt
);
414 case IP_FW_SYNC_EDGE_CLEAR
:
415 error
= ipfw_ctl_sync_edge_clear(sopt
);
417 case IP_FW_SYNC_CENTRE_CLEAR
:
418 error
= ipfw_ctl_sync_centre_clear(sopt
);
420 case IP_FW_SYNC_EDGE_TEST
:
421 error
= ipfw_ctl_sync_edge_test(sopt
);
423 case IP_FW_SYNC_CENTRE_TEST
:
424 error
= ipfw_ctl_sync_centre_test(sopt
);
427 kprintf("ipfw3 sync invalid socket option %d\n",
434 ipfw_sync_send_state(struct ip_fw_state
*state
, int cpu
, int hash
)
438 int error
, i
, len
, nsize
;
439 struct cmd_send_state cmd
;
441 len
= sizeof(struct cmd_send_state
);
442 m
= m_getl(len
, M_WAITOK
, MT_DATA
, M_PKTHDR
, &nsize
);
444 kprintf("ipfw3sync: MGET failed\n");
449 cmd
.rulenum
= state
->stub
->rulenum
;
450 cmd
.lifetime
= state
->lifetime
;
451 cmd
.expiry
= state
->expiry
;
455 memcpy(&cmd
.flow
, &state
->flow_id
, sizeof(struct ipfw_flow_id
));
456 memcpy(m
->m_data
, &cmd
, len
);
459 m
->m_pkthdr
.len
= len
;
461 td
= curthread
->td_proc
? curthread
: &thread0
;
462 for (i
= 0; i
< sync_ctx
.count
; i
++) {
463 error
= so_pru_sosend(sync_ctx
.centre_socks
[i
],
464 NULL
, NULL
, m
, NULL
, 0 ,td
);
466 kprintf("ipfw3sync: centre sosend failed: %d\n", error
);
474 ipfw3_sync_modevent(int type
)
478 ipfw_sync_send_state_prt
= ipfw_sync_send_state
;
481 if (sync_ctx
.edges
!= NULL
) {
482 kfree(sync_ctx
.edges
, M_IPFW3_SYNC
);
484 if (sync_ctx
.running
& 1) {
485 sync_ctx
.running
= 0;
486 soclose(sync_ctx
.edge_sock
, 0);
487 sync_ctx
.edge_td
= NULL
;
489 if (sync_ctx
.running
& 2) {
491 for (i
= 0; i
< sync_ctx
.count
; i
++) {
492 soclose(sync_ctx
.centre_socks
[i
], 0);