Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
/* $NetBSD: pfs.c,v 1.2 2015/06/16 23:04:14 christos Exp $ */

/*-
 * Copyright (c) 2010 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * 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.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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/cdefs.h>

#ifndef lint
__RCSID("$NetBSD: pfs.c,v 1.2 2015/06/16 23:04:14 christos Exp $");
#endif

#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/stat.h>

#include <net/if.h>
#include <netinet/in.h>
#define TCPSTATES
#include <netinet/tcp_fsm.h>
#include <net/pfvar.h>
#include <arpa/inet.h>

#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>

#include "parser.h"

__dead static void usage(void);
static int setlock(int, int, int);
static int get_states(int, int, struct pfioc_states*);
static int dump_states_binary(int, int, const char*);
static int restore_states_binary(int, int, const char*);
static int dump_states_ascii(int, int, const char*);
static int restore_states_ascii(int, int, const char*);
static char* print_host(const struct pfsync_state_host *h, sa_family_t, char*, size_t);
static void print_peer(const struct pfsync_state_peer *peer, uint8_t, FILE*);
static int print_states(int, int, FILE*);
static void display_states(const struct pfioc_states*, int, FILE*);
static int test_ascii_dump(int, const char*, const char*);

static char pf_device[] = "/dev/pf";

__dead static void
usage(void) 
{
	fprintf(stderr, 
			"usage : %s [-v] [-u | -l | -w <filename> | -r <filename> |\n"
			"			[ -W <filename> | -R <filename> ]\n",
			getprogname());
	exit(EXIT_FAILURE);
}

/*
 * The state table must be locked before calling this function
 * Return the number of state in case of success, -1 in case of failure
 * ps::ps_buf must be freed by user after use (in case of success)
 */
static int
get_states(int fd, int verbose __unused, struct pfioc_states* ps)
{
	memset(ps, 0, sizeof(*ps));
	ps->ps_len = 0;
	char* inbuf;

	// ask the kernel how much memory we need to allocate
	if (ioctl(fd, DIOCGETSTATES, ps) == -1) {
		err(EXIT_FAILURE, "DIOCGETSTATES");
	}	

	/* no state */
	if (ps->ps_len == 0)
		return 0;

	inbuf = malloc(ps->ps_len);
	if (inbuf == NULL)
		err(EXIT_FAILURE, NULL);

	ps->ps_buf = inbuf;

	// really retrieve the different states
	if (ioctl(fd, DIOCGETSTATES, ps) == -1) { 
		free(ps->ps_buf);
		err(EXIT_FAILURE, "DIOCGETSTATES");
	}

	return (ps->ps_len / sizeof(struct pfsync_state));
}

static int
dump_states_binary(int fd, int verbose, const char* filename)
{
	int wfd; 
	struct pfioc_states ps;
	struct pfsync_state *p = NULL;
	int nb_states;
	int i;
	int error = 0;
	int errno_saved = 0;

	wfd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, 0600);
	if (wfd == -1)
		err(EXIT_FAILURE, "Cannot open %s", filename);

	nb_states = get_states(fd, verbose, &ps);
	if (nb_states <= 0) {
		close(wfd);
		return nb_states;
	}

	/*
	 * In the file, write the number of states, then store the different states
	 * When we will switch to text format, we probably don't care any more about the len
	 */
	if (write(wfd, &nb_states, sizeof(nb_states)) != sizeof(nb_states)) {
		error = EXIT_FAILURE;
		errno_saved = errno;
		goto done;
	}

	p = ps.ps_states;
	for (i = 0; i < nb_states; i++) {
		if (write(wfd, &p[i], sizeof(*p)) != sizeof(*p)) {
			error = EXIT_FAILURE; 
			errno_saved = errno;
			goto done;
		}
	}

done:
	free(p);
	close(wfd);
	// close can't modify errno
	if (error) {
		errno = errno_saved;
		err(error, NULL);
	}

	return 0;
}

static int
restore_states_binary(int fd, int verbose __unused, const char* filename)
{
	int rfd;
	struct pfioc_states ps;
	struct pfsync_state *p;
	int nb_states;
	int errno_saved = 0;
	int i;

	rfd = open(filename, O_RDONLY, 0600);
	if (rfd == -1)
		err(EXIT_FAILURE, "Cannot open %s", filename);

	if (read(rfd, &nb_states, sizeof(nb_states)) != sizeof(nb_states)) {
		errno_saved = errno;
		close(rfd);
		errno = errno_saved;
		err(EXIT_FAILURE, NULL);
	}

	ps.ps_len = nb_states * sizeof(struct pfsync_state);
	ps.ps_states = malloc(ps.ps_len);
	if (ps.ps_states == NULL) {
		errno_saved = errno;
		close(rfd);
		errno = errno_saved;
		err(EXIT_FAILURE, NULL);
	}

	p = ps.ps_states;

	for (i = 0; i < nb_states; i++) {
		if (read(rfd, &p[i], sizeof(*p)) != sizeof(*p)) {
			errno_saved = errno;
			close(rfd);
			free(ps.ps_states);
			errno = errno_saved;
			err(EXIT_FAILURE, NULL);
		}
	}

	if (ioctl(fd, DIOCADDSTATES, &ps) == -1) {
		errno_saved = errno;
		close(rfd);
		free(ps.ps_states);
		errno = errno_saved;
		err(EXIT_FAILURE, "DIOCADDSTATES");
	}

	free(ps.ps_states);
	close(rfd);
	return 0;
}

static char*
print_host(const struct pfsync_state_host *h, sa_family_t af, char* buf, 
		size_t size_buf)
{
	uint16_t port;
	char	buf_addr[48];

	port = ntohs(h->port);
	if (inet_ntop(af, &(h->addr) , buf_addr, sizeof(buf_addr)) == NULL) {
		strcpy(buf_addr, "?");
	} 

	snprintf(buf, size_buf, "%s:[%d]", buf_addr, port);
	return buf;
}

static void
print_peer(const struct pfsync_state_peer* peer, uint8_t proto, FILE* f)
{
	if (proto == IPPROTO_TCP) { 
		if (peer->state < TCP_NSTATES)
			fprintf(f, "state %s", tcpstates[peer->state]);

		if (peer->seqdiff != 0) 
			fprintf(f, " seq [%" PRIu32 ":%" PRIu32 ",%" PRIu32"]",
					peer->seqlo, peer->seqhi, peer->seqdiff);
		else 
			fprintf(f, " seq [%" PRIu32 ":%" PRIu32 "]",
					peer->seqlo, peer->seqhi);

		if (peer->mss != 0) 
			fprintf(f, " max_win %" PRIu16 " mss %" PRIu16 " wscale %" PRIu8, 
					peer->max_win, peer->mss, peer->wscale);
		else
			fprintf(f, " max_win %" PRIu16 " wscale %" PRIu8, peer->max_win, 
					peer->wscale);
				
	} else {
		if (proto == IPPROTO_UDP) {
			const char *mystates[] = PFUDPS_NAMES;
			if (peer->state < PFUDPS_NSTATES)
				fprintf(f, "state %s", mystates[peer->state]);
		} else if (proto == IPPROTO_ICMP || proto == IPPROTO_ICMPV6) { 
			fprintf(f, " state %" PRIu8, peer->state);
		} else {
			const char *mystates[] = PFOTHERS_NAMES;
			if (peer->state < PFOTHERS_NSTATES)
				fprintf(f, " state %s", mystates[peer->state]);
		}
	}

	if (peer->scrub.scrub_flag == PFSYNC_SCRUB_FLAG_VALID) {
		fprintf(f, " scrub flags %" PRIu16 "ttl %" PRIu8 "mod %"PRIu32,
				peer->scrub.pfss_flags, peer->scrub.pfss_ttl, peer->scrub.pfss_ts_mod);
	} else {
		fprintf(f, " no-scrub");
	}
}

static void 
display_states(const struct pfioc_states *ps, int verbose __unused, FILE* f)
{
	struct pfsync_state *p = NULL;
	struct pfsync_state_peer *src, *dst;
	struct protoent *proto;
	int nb_states;
	int i;
	uint64_t id;

	p = ps->ps_states;
	nb_states = ps->ps_len / sizeof(struct pfsync_state);

	for (i = 0; i < nb_states; i++, p++) {
		fprintf(f, "state %s ", p->direction == PF_OUT ? "out" : "in");
		fprintf(f, "on %s ", p->ifname);

		if ((proto = getprotobynumber(p->proto)) != NULL)
			fprintf(f, "proto %s ", proto->p_name);
		else
			fprintf(f, "proto %u ", p->proto);


		if (PF_ANEQ(&p->lan.addr, &p->gwy.addr, p->af) || 
				(p->lan.port != p->gwy.port)) {
					
			char buf1[64], buf2[64], buf3[64];
			fprintf(f, "from %s to %s using %s", 
					print_host(&p->lan, p->af, buf1, sizeof(buf1)),
					print_host(&p->ext, p->af, buf2, sizeof(buf2)),
					print_host(&p->gwy, p->af, buf3, sizeof(buf3)));
		} else {
			char buf1[64], buf2[64];
			fprintf(f, "from %s to %s", 
					print_host(&p->lan, p->af, buf1, sizeof(buf1)),
					print_host(&p->ext, p->af, buf2, sizeof(buf2)));
		}

		memcpy(&id, p->id, sizeof(p->id));
		fprintf(f, " id %" PRIu64 " cid %" PRIu32 " expire %" PRIu32 " timeout %" PRIu8,
				id , p->creatorid, p->expire, p->timeout);

		if (p->direction == PF_OUT) {
			src = &p->src;
			dst = &p->dst;
		} else {
			src = &p->dst;
			dst = &p->src;
		}

		fprintf(f, " src "); 
		print_peer(src, p->proto, f);
		fprintf(f, " dst ");
		print_peer(dst, p->proto, f);

		fprintf(f, "\n");
	}
}

static int
print_states(int fd, int verbose, FILE* f)
{
	struct pfioc_states ps;
	int nb_states;

	nb_states = get_states(fd, verbose, &ps);
	if (nb_states <= 0) {
		return nb_states;
	}

	display_states(&ps, verbose, f);
					
	free(ps.ps_states);
	return 0;
}

static int
dump_states_ascii(int fd, int verbose, const char* filename)
{
	FILE *f;

	if (strcmp(filename, "-") == 0) {
		f = stdout;
	} else {
		f = fopen(filename, "w");
		if (f == NULL) 
			err(EXIT_FAILURE, "Can't open %s", filename);
	}

	print_states(fd, verbose, f);

	if (f != stdout)
		fclose(f);

	return 0;
}

static int
restore_states_ascii(int fd, int verbose __unused, const char* filename)
{
	FILE *f;
	struct pfioc_states ps;
	int errno_saved;

	f = fopen(filename, "r");
	if (f == NULL)
		err(EXIT_FAILURE, "Can't open %s", filename);

	parse(f, &ps);

	if (ioctl(fd, DIOCADDSTATES, &ps) == -1) {
		errno_saved = errno;
		fclose(f);
		free(ps.ps_states);
		errno = errno_saved;
		err(EXIT_FAILURE, "DIOCADDSTATES");
	}

	free(ps.ps_states);
	fclose(f);
	return 0;
}
			
static int
setlock(int fd, int verbose, int lock)
{
	if (verbose)
		printf("Turning lock %s\n", lock ? "on" : "off");

	if (ioctl(fd, DIOCSETLCK, &lock) == -1) 
		err(EXIT_FAILURE, "DIOCSETLCK");

	return 0;
}

static int
test_ascii_dump(int verbose, const char* file1, const char *file2)
{
	FILE *f1, *f2;
	struct pfioc_states ps;
	int errno_saved;
	
	f1 = fopen(file1, "r");
	if (f1 == NULL)
		err(EXIT_FAILURE, "Can't open %s", file1);


	f2 = fopen(file2, "w");
	if (f2 == NULL) {
		errno_saved = errno;
		fclose(f2);
		errno = errno_saved;
		err(EXIT_FAILURE, "Can't open %s", file2);
	}

	parse(f1, &ps);
	display_states(&ps, verbose, f2);

	free(ps.ps_states);
	fclose(f1);
	fclose(f2);

	return 0;
}

int main(int argc, char *argv[])
{
	setprogname(argv[0]);

	int lock = 0;
	int set = 0;
	int dump = 0;
	int restore = 0;
	int verbose = 0;
	int test = 0;
	bool binary = false;
	char* filename = NULL;
	char* filename2 = NULL;
	int error = 0;
	int fd;
	int c;

	while ((c = getopt(argc, argv, "ulvw:r:R:W:bt:o:")) != -1) 
		switch (c) {
		case 'u' : 
			lock = 0;
			set = 1;
			break;

		case 'l' :
			lock = 1;
			set = 1;
			break;

		case 'b':
			binary = true;
			break;

		case 'r':
			restore = 1;
			filename = optarg;
			break;

		case 'v':
			verbose=1;
			break;

		case 'w':
			dump=1;
			filename=optarg;
			break;

		case 'R':
			restore = 1;
			set = 1;
			filename = optarg;
			break;

		case 'W':
			dump = 1;
			set = 1;
			filename = optarg;
			break;

		case 't':
			test=1;
			filename = optarg;
			break;

		case 'o':
			filename2 = optarg; 
			break;

		case '?' :
		default:
			usage();
		}

	if (set == 0 && dump == 0 && restore == 0 && test == 0) 
		usage();

	if (dump == 1 && restore == 1)
		usage();

	if (test == 1) {
		if (filename2 == NULL) {
			fprintf(stderr, "-o <file> is required when using -t\n");
			err(EXIT_FAILURE, NULL);
		}
		error = test_ascii_dump(verbose, filename, filename2);
	} else {
		fd = open(pf_device, O_RDWR);
		if (fd == -1) 
			err(EXIT_FAILURE, "Cannot open %s", pf_device);

		if (set != 0 && dump == 0 && restore == 0)
			error = setlock(fd, verbose, lock);

		if (dump) {
			if (set) 
				error = setlock(fd, verbose, 1);

			if (binary) 
				error = dump_states_binary(fd, verbose, filename);
			else
				error = dump_states_ascii(fd, verbose, filename);

			if (set)
				error = setlock(fd, verbose, 0);
		}

		if (restore) {
			if (set) 
				error = setlock(fd, verbose, 1);

			if (binary) 
				error = restore_states_binary(fd, verbose, filename);
			else 
				error = restore_states_ascii(fd, verbose, filename);

			if (set)
				error = setlock(fd, verbose, 0);
		}

		close(fd);
	}

	return error;
}