DragonFlyBSD Kernel Audit
sys/net/ipfw3_basic/ip_fw3_sync.c
← back
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/*
 * Copyright (c) 2016 - 2018 The DragonFly Project.  All rights reserved.
 *
 * This code is derived from software contributed to The DragonFly Project
 * by Bill Yuan <bycn82@dragonflybsd.org>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 3. Neither the name of The DragonFly Project nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific, prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */

#include <sys/types.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/kernel.h>
#include <sys/proc.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/socketvar2.h>
#include <sys/socketops.h>
#include <sys/sysctl.h>
#include <sys/syslog.h>
#include <sys/ucred.h>
#include <sys/in_cksum.h>
#include <sys/lock.h>
#include <sys/kthread.h>

#include <net/if.h>
#include <net/route.h>
#include <net/pfil.h>
#include <net/netmsg2.h>
#include <net/ethernet.h>

#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/in_var.h>
#include <netinet/in_pcb.h>
#include <netinet/ip.h>
#include <netinet/ip_var.h>
#include <netinet/ip_icmp.h>
#include <netinet/tcp.h>
#include <netinet/tcp_timer.h>
#include <netinet/tcp_var.h>
#include <netinet/tcpip.h>
#include <netinet/udp.h>
#include <netinet/udp_var.h>
#include <netinet/ip_divert.h>
#include <netinet/if_ether.h>

#include <net/ipfw3/ip_fw.h>
#include <net/ipfw3_basic/ip_fw3_sync.h>

#define LEN_IN_ADDR		sizeof(struct in_addr)

MALLOC_DEFINE(M_IPFW3_SYNC, "IPFW3_SYNC", "mem for ipfw3sync");

extern struct ipfw3_context		*fw3_ctx[MAXCPU];
extern struct ipfw3_sync_context	fw3_sync_ctx;
ipfw_sync_send_state_t			*ipfw_sync_send_state_prt = NULL;
ipfw_sync_install_state_t		*ipfw_sync_install_state_prt = NULL;



void
ip_fw3_sync_install_state(struct cmd_send_state *cmd)
{
	/* TODO */
}

/*
 * ipfw3sync show config
 */
int
ip_fw3_ctl_sync_show_conf(struct sockopt *sopt)
{
	struct ipfw3_ioc_sync_context *tmp_sync_ctx;
	int size;

	size = 3 * sizeof(int) + fw3_sync_ctx.count * LEN_SYNC_EDGE;
	if (sopt->sopt_valsize < size) {
		/* sopt_val is not big enough */
		bzero(sopt->sopt_val, sopt->sopt_valsize);
		return 0;
	}
	tmp_sync_ctx = (struct ipfw3_ioc_sync_context *)sopt->sopt_val;
	tmp_sync_ctx->edge_port = fw3_sync_ctx.edge_port;
	tmp_sync_ctx->hw_same = fw3_sync_ctx.hw_same;
	tmp_sync_ctx->count = fw3_sync_ctx.count;
	bcopy(fw3_sync_ctx.edges, tmp_sync_ctx->edges,
			fw3_sync_ctx.count * LEN_SYNC_EDGE);
	sopt->sopt_valsize = size;
	return 0;
}

/*
 * ipfw3sync show status
 */
int
ip_fw3_ctl_sync_show_status(struct sockopt *sopt)
{
	int *running;
	running = (int *)sopt->sopt_val;
	*running = fw3_sync_ctx.running;
	sopt->sopt_valsize = sizeof(int);
	return 0;
}
/*
 * ipfw3sync config centre
 */
int
ip_fw3_ctl_sync_centre_conf(struct sockopt *sopt)
{
	struct ipfw3_ioc_sync_centre *ioc_centre;
	int size;

	ioc_centre = sopt->sopt_val;
	size = ioc_centre->count * LEN_SYNC_EDGE;
	if (fw3_sync_ctx.count == 0) {
		fw3_sync_ctx.edges = kmalloc(size, M_IPFW3_SYNC, M_NOWAIT | M_ZERO);
	} else {
		fw3_sync_ctx.edges = krealloc(fw3_sync_ctx.edges,
				size, M_TEMP, M_WAITOK);
	}
	fw3_sync_ctx.count = ioc_centre->count;
	bcopy(ioc_centre->edges, fw3_sync_ctx.edges,
			ioc_centre->count * LEN_SYNC_EDGE);
	return 0;
}

/*
 * ipfw3sync config edge
 */
int
ip_fw3_ctl_sync_edge_conf(struct sockopt *sopt)
{
	struct ipfw3_ioc_sync_edge *ioc_edge;
	struct thread *td;
	size_t size;
	int error;

	size = sopt->sopt_valsize;
	ioc_edge = sopt->sopt_val;
	if (size != sizeof(struct ipfw3_ioc_sync_edge)) {
		return EINVAL;
	}
	fw3_sync_ctx.edge_port = ioc_edge->port;
	fw3_sync_ctx.hw_same = ioc_edge->hw_same;

	td = curthread->td_proc ? curthread : &thread0;
	error = socreate(AF_INET, &fw3_sync_ctx.edge_sock,
			SOCK_DGRAM, IPPROTO_UDP, td);
	if (error) {
		kprintf("ipfw3sync edge socreate failed: %d\n", error);
		return (error);
	}
	return 0;
}

void
ip_fw3_sync_edge_socket_handler(void *dummy)
{
	struct socket *so;
	struct sockbuf sio;
	struct sockaddr_in sin;
	struct mbuf *m;
	struct sockaddr *sa;
	int error, flags, *type;

	so = fw3_sync_ctx.edge_sock;
	flags = MSG_FBLOCKING;

	bzero(&sin, sizeof(struct sockaddr_in));
	sin.sin_family = AF_INET;
	sin.sin_port = htons(fw3_sync_ctx.edge_port);
	sin.sin_len = LEN_IN_ADDR;
	sa = (struct sockaddr *)&sin;
	while (fw3_sync_ctx.running & 1) {
		sbinit(&sio, 1000000000);
		error = so_pru_soreceive(so, NULL, NULL, &sio, NULL, &flags);
		if (error)
			break;
		m = sio.sb_mb;
		type = (int *)m->m_data;
		if (*type == SYNC_TYPE_SEND_TEST) {
			struct cmd_send_test *cmd;
			cmd = (struct cmd_send_test *)m->m_data;
			kprintf("test received %d\n", cmd->num);
		} else if (*type == SYNC_TYPE_SEND_STATE) {
			struct cmd_send_state *cmd;
			cmd = (struct cmd_send_state *)m->m_data;
			if (ipfw_sync_install_state_prt != NULL) {
				(*ipfw_sync_install_state_prt)(cmd);
			}
		} else if (*type == SYNC_TYPE_SEND_NAT) {
			/* TODO sync NAT records */
			kprintf("nat received\n");
		} else {
			kprintf("Error ignore\n");
		}
	}
	soshutdown(fw3_sync_ctx.edge_sock, SHUT_RD);
	sofree(fw3_sync_ctx.edge_sock);
	kthread_exit();
}

int
ip_fw3_ctl_sync_edge_start(struct sockopt *sopt)
{
	struct sockaddr_in sin;
	struct thread *td;
	int error;

	if (fw3_sync_ctx.running & 1) {
		return 0;
	}
	td = curthread->td_proc ? curthread : &thread0;
	bzero(&sin, sizeof(struct sockaddr_in));
	sin.sin_family = AF_INET;
	sin.sin_len = sizeof(struct sockaddr_in);
	sin.sin_port = htons(fw3_sync_ctx.edge_port);
	sin.sin_addr.s_addr = INADDR_ANY;
	error = sobind(fw3_sync_ctx.edge_sock, (struct sockaddr *)&sin, td);
	if (error) {
		if (error != EADDRINUSE) {
			kprintf("ipfw3sync edge sobind failed: %d\n", error);
		} else {
			kprintf("ipfw3sync edge address in use: %d\n", error);
		}
		return (error);
	}

	fw3_sync_ctx.running |= 1;
	soreference(fw3_sync_ctx.edge_sock);
	error = kthread_create(ip_fw3_sync_edge_socket_handler, NULL,
			&fw3_sync_ctx.edge_td, "sync_edge_thread");
	if (error) {
		panic("ip_fw3_sync_edge_socket_handler:error %d",error);
	}
	return 0;
}

int
ip_fw3_ctl_sync_centre_start(struct sockopt *sopt)
{
	struct sockaddr_in sin;
	struct thread *td;
	struct ipfw3_sync_edge *edge;
	int error, i;

	fw3_sync_ctx.running |= 2;
	td = curthread->td_proc ? curthread : &thread0;

	for (i = 0; i < fw3_sync_ctx.count; i++) {
		error = socreate(AF_INET, &fw3_sync_ctx.centre_socks[i],
				SOCK_DGRAM, IPPROTO_UDP, td);
		if (error) {
			kprintf("ipfw3sync centre socreate failed: %d\n",
					error);
			return error;
		}
		edge = fw3_sync_ctx.edges;

		bzero(&sin, sizeof(struct sockaddr_in));
		sin.sin_family = AF_INET;
		sin.sin_port = htons(edge->port);
		sin.sin_addr.s_addr = edge->addr;
		sin.sin_len = sizeof(struct sockaddr_in);
		error = soconnect(fw3_sync_ctx.centre_socks[i],
				(struct sockaddr *)&sin, td, TRUE);
		if (error) {
			kprintf("ipfw3sync: centre soconnect failed: %d\n",
					error);
			return error;
		}
	}

	return 0;
}

int
ip_fw3_ctl_sync_edge_test(struct sockopt *sopt)
{
	return 0;
}

int
ip_fw3_ctl_sync_centre_test(struct sockopt *sopt)
{
	struct cmd_send_test cmd;
	struct mbuf *m;
	struct thread *td;
	int error, i, len, nsize, *num;

	if (sopt->sopt_valsize != sizeof(int)) {
		kprintf("ipfw3sync: invalid centre test parameter\n");
		return -1;
	}
	if ((fw3_sync_ctx.running & 2) == 0) {
		kprintf("ipfw3sync: centre not running\n");
		return -1;
	}
	num = sopt->sopt_val;
	len = sizeof(struct cmd_send_test);
	m = m_getl(len, M_WAITOK, MT_DATA, M_PKTHDR, &nsize);
	cmd.type = 0;
	cmd.num = *num;
	memcpy(m->m_data, &cmd, len);

	m->m_len = len;
	m->m_pkthdr.len = len;

	td = curthread->td_proc ? curthread : &thread0;
	for (i = 0; i < fw3_sync_ctx.count; i++) {
		error = so_pru_sosend(fw3_sync_ctx.centre_socks[i],
				NULL, NULL, m, NULL, 0 ,td);
		if (error) {
			kprintf("ipfw3sync: centre sosend failed: %d\n", error);
			return -1;
		}
	}
	m_free(m);
	return 0;
}
int
ip_fw3_ctl_sync_edge_stop(struct sockopt *sopt)
{
	if (fw3_sync_ctx.running & 1) {
		fw3_sync_ctx.running &= 2;
		soclose(fw3_sync_ctx.edge_sock, 0);
	}
	return 0;
}

int
ip_fw3_ctl_sync_centre_stop(struct sockopt *sopt)
{
	int i;

	if (fw3_sync_ctx.running & 2) {
		fw3_sync_ctx.running &= 1;
		for (i = 0; i < fw3_sync_ctx.count; i++) {
			soclose(fw3_sync_ctx.centre_socks[i], 0);
		}
	}
	return 0;
}

int
ip_fw3_ctl_sync_edge_clear(struct sockopt *sopt)
{
	return 0;
}

int
ip_fw3_ctl_sync_centre_clear(struct sockopt *sopt)
{
	return 0;
}

/*
 * sockopt handler
 */
int
ip_fw3_ctl_sync_sockopt(struct sockopt *sopt)
{
	int error = 0;
	switch (sopt->sopt_name) {
		case IP_FW_SYNC_EDGE_CONF:
			error = ip_fw3_ctl_sync_edge_conf(sopt);
			break;
		case IP_FW_SYNC_CENTRE_CONF:
			error = ip_fw3_ctl_sync_centre_conf(sopt);
			break;
		case IP_FW_SYNC_SHOW_CONF:
			error = ip_fw3_ctl_sync_show_conf(sopt);
			break;
		case IP_FW_SYNC_SHOW_STATUS:
			error = ip_fw3_ctl_sync_show_status(sopt);
			break;
		case IP_FW_SYNC_EDGE_START:
			error = ip_fw3_ctl_sync_edge_start(sopt);
			break;
		case IP_FW_SYNC_CENTRE_START:
			error = ip_fw3_ctl_sync_centre_start(sopt);
			break;
		case IP_FW_SYNC_EDGE_STOP:
			error = ip_fw3_ctl_sync_edge_stop(sopt);
			break;
		case IP_FW_SYNC_CENTRE_STOP:
			error = ip_fw3_ctl_sync_centre_stop(sopt);
			break;
		case IP_FW_SYNC_EDGE_CLEAR:
			error = ip_fw3_ctl_sync_edge_clear(sopt);
			break;
		case IP_FW_SYNC_CENTRE_CLEAR:
			error = ip_fw3_ctl_sync_centre_clear(sopt);
			break;
		case IP_FW_SYNC_EDGE_TEST:
			error = ip_fw3_ctl_sync_edge_test(sopt);
			break;
		case IP_FW_SYNC_CENTRE_TEST:
			error = ip_fw3_ctl_sync_centre_test(sopt);
			break;
		default:
			kprintf("ipfw3 sync invalid socket option %d\n",
					sopt->sopt_name);
	}
	return error;
}

void
ip_fw3_sync_send_state(struct ipfw3_state *state, int cpu, int hash)
{
	struct mbuf *m;
	struct thread *td;
	int error, i, len, nsize;
	struct cmd_send_state cmd;

	len = sizeof(struct cmd_send_state);
	m = m_getl(len, M_WAITOK, MT_DATA, M_PKTHDR, &nsize);

	cmd.type = 1;
	cmd.cpu = cpu;
	cmd.hash = hash;

	memcpy(m->m_data, &cmd, len);

	m->m_len = len;
	m->m_pkthdr.len = len;

	td = curthread->td_proc ? curthread : &thread0;
	for (i = 0; i < fw3_sync_ctx.count; i++) {
		error = so_pru_sosend(fw3_sync_ctx.centre_socks[i],
				NULL, NULL, m, NULL, 0 ,td);
		if (error) {
			kprintf("ipfw3sync: centre sosend failed: %d\n", error);
			return;
		}
	}
	return;
}

void
ip_fw3_sync_modevent(int type)
{
	switch (type) {
		case MOD_LOAD:
			ipfw_sync_send_state_prt = ip_fw3_sync_send_state;
			break;
		case MOD_UNLOAD:
			if (fw3_sync_ctx.edges != NULL) {
				kfree(fw3_sync_ctx.edges, M_IPFW3_SYNC);
			}
			if (fw3_sync_ctx.running & 1) {
				fw3_sync_ctx.running = 0;
				soclose(fw3_sync_ctx.edge_sock, 0);
				fw3_sync_ctx.edge_td = NULL;
			}
			if (fw3_sync_ctx.running & 2) {
				int i;
				for (i = 0; i < fw3_sync_ctx.count; i++) {
					soclose(fw3_sync_ctx.centre_socks[i], 0);
				}
			}
			break;
		default:
			break;
	}
}