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
/*
 * Copyright (c) 2012 Gleb Smirnoff <glebius@FreeBSD.org>
 * Copyright (c) 2002 Michael Shalayeff
 * Copyright (c) 2001 Daniel Hartmeier
 * 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 AUTHOR ``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 AUTHOR OR HIS RELATIVES 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 MIND, 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.
 *
 * $OpenBSD: print-pfsync.c,v 1.38 2012/09/19 13:50:36 mikeb Exp $
 * $OpenBSD: pf_print_state.c,v 1.11 2012/07/08 17:48:37 lteo Exp $
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#ifndef HAVE_NET_PFVAR_H
#error "No pf headers available"
#endif
#include <sys/endian.h>
#include <net/if.h>
#include <net/pfvar.h>
#include <net/if_pfsync.h>
#define	TCPSTATES
#include <netinet/tcp_fsm.h>

#include <netdissect-stdinc.h>
#include <string.h>

#include "netdissect.h"
#include "interface.h"
#include "addrtoname.h"

static void	pfsync_print(netdissect_options *, struct pfsync_header *,
		    const u_char *, u_int);
static void	print_src_dst(netdissect_options *,
		    const struct pfsync_state_peer *,
		    const struct pfsync_state_peer *, uint8_t);
static void	print_state(netdissect_options *, struct pfsync_state *);

#ifdef notyet
void
pfsync_if_print(u_char *user, const struct pcap_pkthdr *h,
    register const u_char *p)
{
	u_int caplen = h->caplen;

	ts_print(&h->ts);

	if (caplen < PFSYNC_HDRLEN) {
		ND_PRINT((ndo, "[|pfsync]"));
		goto out;
	}

	pfsync_print((struct pfsync_header *)p,
	    p + sizeof(struct pfsync_header),
	    caplen - sizeof(struct pfsync_header));
out:
	if (xflag) {
		default_print((const u_char *)p, caplen);
	}
	safeputchar(ndo, '\n');
}
#endif /* notyet */

void
pfsync_ip_print(netdissect_options *ndo , const u_char *bp, u_int len)
{
	struct pfsync_header *hdr = (struct pfsync_header *)bp;

	if (len < PFSYNC_HDRLEN)
		ND_PRINT((ndo, "[|pfsync]"));
	else
		pfsync_print(ndo, hdr, bp + sizeof(struct pfsync_header),
		    len - sizeof(struct pfsync_header));
}

struct pfsync_actions {
	const char *name;
	size_t len;
	void (*print)(netdissect_options *, const void *);
};

static void	pfsync_print_clr(netdissect_options *, const void *);
static void	pfsync_print_state(netdissect_options *, const void *);
static void	pfsync_print_ins_ack(netdissect_options *, const void *);
static void	pfsync_print_upd_c(netdissect_options *, const void *);
static void	pfsync_print_upd_req(netdissect_options *, const void *);
static void	pfsync_print_del_c(netdissect_options *, const void *);
static void	pfsync_print_bus(netdissect_options *, const void *);
static void	pfsync_print_tdb(netdissect_options *, const void *);

struct pfsync_actions actions[] = {
	{ "clear all", sizeof(struct pfsync_clr),	pfsync_print_clr },
	{ "insert", sizeof(struct pfsync_state),	pfsync_print_state },
	{ "insert ack", sizeof(struct pfsync_ins_ack),	pfsync_print_ins_ack },
	{ "update", sizeof(struct pfsync_ins_ack),	pfsync_print_state },
	{ "update compressed", sizeof(struct pfsync_upd_c),
							pfsync_print_upd_c },
	{ "request uncompressed", sizeof(struct pfsync_upd_req),
							pfsync_print_upd_req },
	{ "delete", sizeof(struct pfsync_state),	pfsync_print_state },
	{ "delete compressed", sizeof(struct pfsync_del_c),
							pfsync_print_del_c },
	{ "frag insert", 0,				NULL },
	{ "frag delete", 0,				NULL },
	{ "bulk update status", sizeof(struct pfsync_bus),
							pfsync_print_bus },
	{ "tdb", 0,					pfsync_print_tdb },
	{ "eof", 0,					NULL },
};

static void
pfsync_print(netdissect_options *ndo, struct pfsync_header *hdr,
    const u_char *bp, u_int len)
{
	struct pfsync_subheader *subh;
	int count, plen, i;
	u_int alen;

	plen = ntohs(hdr->len);

	ND_PRINT((ndo, "PFSYNCv%d len %d", hdr->version, plen));

	if (hdr->version != PFSYNC_VERSION)
		return;

	plen -= sizeof(*hdr);

	while (plen > 0) {
		if (len < sizeof(*subh))
			break;

		subh = (struct pfsync_subheader *)bp;
		bp += sizeof(*subh);
		len -= sizeof(*subh);
		plen -= sizeof(*subh);

		if (subh->action >= PFSYNC_ACT_MAX) {
			ND_PRINT((ndo, "\n    act UNKNOWN id %d",
			    subh->action));
			return;
		}

		count = ntohs(subh->count);
		ND_PRINT((ndo, "\n    %s count %d", actions[subh->action].name,
		    count));
		alen = actions[subh->action].len;

		if (subh->action == PFSYNC_ACT_EOF)
			return;

		if (actions[subh->action].print == NULL) {
			ND_PRINT((ndo, "\n    unimplemented action %hhu",
			    subh->action));
			return;
		}

		for (i = 0; i < count; i++) {
			if (len < alen) {
				len = 0;
				break;
			}

			if (ndo->ndo_vflag)
				actions[subh->action].print(ndo, bp);

			bp += alen;
			len -= alen;
			plen -= alen;
		}
	}

	if (plen > 0) {
		ND_PRINT((ndo, "\n    ..."));
		return;
	}
	if (plen < 0) {
		ND_PRINT((ndo, "\n    invalid header length"));
		return;
	}
	if (len > 0)
		ND_PRINT((ndo, "\n    invalid packet length"));
}

static void
pfsync_print_clr(netdissect_options *ndo, const void *bp)
{
	const struct pfsync_clr *clr = bp;

	ND_PRINT((ndo, "\n\tcreatorid: %08x", htonl(clr->creatorid)));
	if (clr->ifname[0] != '\0')
		ND_PRINT((ndo, " interface: %s", clr->ifname));
}

static void
pfsync_print_state(netdissect_options *ndo, const void *bp)
{
	struct pfsync_state *st = (struct pfsync_state *)bp;

	safeputchar(ndo, '\n');
	print_state(ndo, st);
}

static void
pfsync_print_ins_ack(netdissect_options *ndo, const void *bp)
{
	const struct pfsync_ins_ack *iack = bp;

	ND_PRINT((ndo, "\n\tid: %016jx creatorid: %08x",
	    (uintmax_t)be64toh(iack->id), ntohl(iack->creatorid)));
}

static void
pfsync_print_upd_c(netdissect_options *ndo, const void *bp)
{
	const struct pfsync_upd_c *u = bp;

	ND_PRINT((ndo, "\n\tid: %016jx creatorid: %08x",
	    (uintmax_t)be64toh(u->id), ntohl(u->creatorid)));
	if (ndo->ndo_vflag > 2) {
		ND_PRINT((ndo, "\n\tTCP? :"));
		print_src_dst(ndo, &u->src, &u->dst, IPPROTO_TCP);
	}
}

static void
pfsync_print_upd_req(netdissect_options *ndo, const void *bp)
{
	const struct pfsync_upd_req *ur = bp;

	ND_PRINT((ndo, "\n\tid: %016jx creatorid: %08x",
	    (uintmax_t)be64toh(ur->id), ntohl(ur->creatorid)));
}

static void
pfsync_print_del_c(netdissect_options *ndo, const void *bp)
{
	const struct pfsync_del_c *d = bp;

	ND_PRINT((ndo, "\n\tid: %016jx creatorid: %08x",
	    (uintmax_t)be64toh(d->id), ntohl(d->creatorid)));
}

static void
pfsync_print_bus(netdissect_options *ndo, const void *bp)
{
	const struct pfsync_bus *b = bp;
	uint32_t endtime;
	int min, sec;
	const char *status;

	endtime = ntohl(b->endtime);
	sec = endtime % 60;
	endtime /= 60;
	min = endtime % 60;
	endtime /= 60;

	switch (b->status) {
	case PFSYNC_BUS_START:
		status = "start";
		break;
	case PFSYNC_BUS_END:
		status = "end";
		break;
	default:
		status = "UNKNOWN";
		break;
	}

	ND_PRINT((ndo, "\n\tcreatorid: %08x age: %.2u:%.2u:%.2u status: %s",
	    htonl(b->creatorid), endtime, min, sec, status));
}

static void
pfsync_print_tdb(netdissect_options *ndo, const void *bp)
{
	const struct pfsync_tdb *t = bp;

	ND_PRINT((ndo, "\n\tspi: 0x%08x rpl: %ju cur_bytes: %ju",
	    ntohl(t->spi), (uintmax_t )be64toh(t->rpl),
	    (uintmax_t )be64toh(t->cur_bytes)));
}

static void
print_host(netdissect_options *ndo, struct pf_addr *addr, uint16_t port,
    sa_family_t af, const char *proto)
{
	char buf[48];

	if (inet_ntop(af, addr, buf, sizeof(buf)) == NULL)
		ND_PRINT((ndo, "?"));
	else
		ND_PRINT((ndo, "%s", buf));

	if (port)
		ND_PRINT((ndo, ".%hu", ntohs(port)));
}

static void
print_seq(netdissect_options *ndo, const struct pfsync_state_peer *p)
{
	if (p->seqdiff)
		ND_PRINT((ndo, "[%u + %u](+%u)", ntohl(p->seqlo),
		    ntohl(p->seqhi) - ntohl(p->seqlo), ntohl(p->seqdiff)));
	else
		ND_PRINT((ndo, "[%u + %u]", ntohl(p->seqlo),
		    ntohl(p->seqhi) - ntohl(p->seqlo)));
}

static void
print_src_dst(netdissect_options *ndo, const struct pfsync_state_peer *src,
    const struct pfsync_state_peer *dst, uint8_t proto)
{

	if (proto == IPPROTO_TCP) {
		if (src->state <= TCPS_TIME_WAIT &&
		    dst->state <= TCPS_TIME_WAIT)
			ND_PRINT((ndo, "   %s:%s", tcpstates[src->state],
			    tcpstates[dst->state]));
		else if (src->state == PF_TCPS_PROXY_SRC ||
		    dst->state == PF_TCPS_PROXY_SRC)
			ND_PRINT((ndo, "   PROXY:SRC"));
		else if (src->state == PF_TCPS_PROXY_DST ||
		    dst->state == PF_TCPS_PROXY_DST)
			ND_PRINT((ndo, "   PROXY:DST"));
		else
			ND_PRINT((ndo, "   <BAD STATE LEVELS %u:%u>",
			    src->state, dst->state));
		if (ndo->ndo_vflag > 1) {
			ND_PRINT((ndo, "\n\t"));
			print_seq(ndo, src);
			if (src->wscale && dst->wscale)
				ND_PRINT((ndo, " wscale %u",
				    src->wscale & PF_WSCALE_MASK));
			ND_PRINT((ndo, "  "));
			print_seq(ndo, dst);
			if (src->wscale && dst->wscale)
				ND_PRINT((ndo, " wscale %u",
				    dst->wscale & PF_WSCALE_MASK));
		}
	} else if (proto == IPPROTO_UDP && src->state < PFUDPS_NSTATES &&
	    dst->state < PFUDPS_NSTATES) {
		const char *states[] = PFUDPS_NAMES;

		ND_PRINT((ndo, "   %s:%s", states[src->state], states[dst->state]));
	} else if (proto != IPPROTO_ICMP && src->state < PFOTHERS_NSTATES &&
	    dst->state < PFOTHERS_NSTATES) {
		/* XXX ICMP doesn't really have state levels */
		const char *states[] = PFOTHERS_NAMES;

		ND_PRINT((ndo, "   %s:%s", states[src->state], states[dst->state]));
	} else {
		ND_PRINT((ndo, "   %u:%u", src->state, dst->state));
	}
}

static void
print_state(netdissect_options *ndo, struct pfsync_state *s)
{
	struct pfsync_state_peer *src, *dst;
	struct pfsync_state_key *sk, *nk;
	int min, sec;

	if (s->direction == PF_OUT) {
		src = &s->src;
		dst = &s->dst;
		sk = &s->key[PF_SK_STACK];
		nk = &s->key[PF_SK_WIRE];
		if (s->proto == IPPROTO_ICMP || s->proto == IPPROTO_ICMPV6)
			sk->port[0] = nk->port[0];
	} else {
		src = &s->dst;
		dst = &s->src;
		sk = &s->key[PF_SK_WIRE];
		nk = &s->key[PF_SK_STACK];
		if (s->proto == IPPROTO_ICMP || s->proto == IPPROTO_ICMPV6)
			sk->port[1] = nk->port[1];
	}
	ND_PRINT((ndo, "\t%s ", s->ifname));
	ND_PRINT((ndo, "proto %u ", s->proto));

	print_host(ndo, &nk->addr[1], nk->port[1], s->af, NULL);
	if (PF_ANEQ(&nk->addr[1], &sk->addr[1], s->af) ||
	    nk->port[1] != sk->port[1]) {
		ND_PRINT((ndo, " ("));
		print_host(ndo, &sk->addr[1], sk->port[1], s->af, NULL);
		ND_PRINT((ndo, ")"));
	}
	if (s->direction == PF_OUT)
		ND_PRINT((ndo, " -> "));
	else
		ND_PRINT((ndo, " <- "));
	print_host(ndo, &nk->addr[0], nk->port[0], s->af, NULL);
	if (PF_ANEQ(&nk->addr[0], &sk->addr[0], s->af) ||
	    nk->port[0] != sk->port[0]) {
		ND_PRINT((ndo, " ("));
		print_host(ndo, &sk->addr[0], sk->port[0], s->af, NULL);
		ND_PRINT((ndo, ")"));
	}

	print_src_dst(ndo, src, dst, s->proto);

	if (ndo->ndo_vflag > 1) {
		uint64_t packets[2];
		uint64_t bytes[2];
		uint32_t creation = ntohl(s->creation);
		uint32_t expire = ntohl(s->expire);

		sec = creation % 60;
		creation /= 60;
		min = creation % 60;
		creation /= 60;
		ND_PRINT((ndo, "\n\tage %.2u:%.2u:%.2u", creation, min, sec));
		sec = expire % 60;
		expire /= 60;
		min = expire % 60;
		expire /= 60;
		ND_PRINT((ndo, ", expires in %.2u:%.2u:%.2u", expire, min, sec));

		bcopy(s->packets[0], &packets[0], sizeof(uint64_t));
		bcopy(s->packets[1], &packets[1], sizeof(uint64_t));
		bcopy(s->bytes[0], &bytes[0], sizeof(uint64_t));
		bcopy(s->bytes[1], &bytes[1], sizeof(uint64_t));
		ND_PRINT((ndo, ", %ju:%ju pkts, %ju:%ju bytes",
		    be64toh(packets[0]), be64toh(packets[1]),
		    be64toh(bytes[0]), be64toh(bytes[1])));
		if (s->anchor != ntohl(-1))
			ND_PRINT((ndo, ", anchor %u", ntohl(s->anchor)));
		if (s->rule != ntohl(-1))
			ND_PRINT((ndo, ", rule %u", ntohl(s->rule)));
	}
	if (ndo->ndo_vflag > 1) {
		uint64_t id;

		bcopy(&s->id, &id, sizeof(uint64_t));
		ND_PRINT((ndo, "\n\tid: %016jx creatorid: %08x",
		    (uintmax_t )be64toh(id), ntohl(s->creatorid)));
	}
}