Home | History | Annotate | Line # | Download | only in dist
      1   1.1  christos /*
      2   1.1  christos  * Copyright (c) 1990, 1991, 1993, 1994, 1995, 1996
      3   1.1  christos  *	The Regents of the University of California.  All rights reserved.
      4   1.1  christos  *
      5   1.1  christos  * Redistribution and use in source and binary forms, with or without
      6   1.1  christos  * modification, are permitted provided that: (1) source code distributions
      7   1.1  christos  * retain the above copyright notice and this paragraph in its entirety, (2)
      8   1.1  christos  * distributions including binary code include the above copyright notice and
      9   1.1  christos  * this paragraph in its entirety in the documentation or other materials
     10   1.1  christos  * provided with the distribution, and (3) all advertising materials mentioning
     11   1.1  christos  * features or use of this software display the following acknowledgement:
     12   1.1  christos  * ``This product includes software developed by the University of California,
     13   1.1  christos  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
     14   1.1  christos  * the University nor the names of its contributors may be used to endorse
     15   1.1  christos  * or promote products derived from this software without specific prior
     16   1.1  christos  * written permission.
     17   1.1  christos  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
     18   1.1  christos  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
     19   1.1  christos  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
     20   1.1  christos  */
     21   1.1  christos 
     22   1.2  christos #include <sys/cdefs.h>
     23   1.1  christos #ifndef lint
     24  1.11  christos __RCSID("$NetBSD: print-pflog.c,v 1.11 2026/03/19 00:05:13 christos Exp $");
     25   1.1  christos #endif
     26   1.1  christos 
     27   1.9  christos /* \summary: *BSD/Darwin packet filter log file printer */
     28   1.8       spz 
     29   1.9  christos #include <config.h>
     30   1.1  christos 
     31   1.9  christos #include "netdissect-stdinc.h"
     32   1.1  christos 
     33   1.7  christos #include "netdissect.h"
     34   1.5  christos #include "extract.h"
     35   1.9  christos #include "af.h"
     36   1.1  christos 
     37   1.9  christos #include "pflog.h"
     38   1.1  christos 
     39   1.4  christos static const struct tok pf_reasons[] = {
     40   1.9  christos 	{ PFRES_MATCH,		"0(match)" },
     41   1.9  christos 	{ PFRES_BADOFF,		"1(bad-offset)" },
     42   1.9  christos 	{ PFRES_FRAG,		"2(fragment)" },
     43  1.10  christos 	{ PFRES_SHORT,		"3(short)" },
     44   1.9  christos 	{ PFRES_NORM,		"4(normalize)" },
     45   1.9  christos 	{ PFRES_MEMORY,		"5(memory)" },
     46   1.9  christos 	{ PFRES_TS,		"6(bad-timestamp)" },
     47   1.9  christos 	{ PFRES_CONGEST,	"7(congestion)" },
     48   1.9  christos 	{ PFRES_IPOPTIONS,	"8(ip-option)" },
     49   1.9  christos 	{ PFRES_PROTCKSUM,	"9(proto-cksum)" },
     50   1.9  christos 	{ PFRES_BADSTATE,	"10(state-mismatch)" },
     51   1.9  christos 	{ PFRES_STATEINS,	"11(state-insert)" },
     52   1.9  christos 	{ PFRES_MAXSTATES,	"12(state-limit)" },
     53   1.9  christos 	{ PFRES_SRCLIMIT,	"13(src-limit)" },
     54   1.9  christos 	{ PFRES_SYNPROXY,	"14(synproxy)" },
     55   1.9  christos #if defined(__FreeBSD__)
     56   1.9  christos 	{ PFRES_MAPFAILED,	"15(map-failed)" },
     57   1.9  christos #elif defined(__NetBSD__)
     58   1.9  christos 	{ PFRES_STATELOCKED,	"15(state-locked)" },
     59   1.9  christos #elif defined(__OpenBSD__)
     60   1.9  christos 	{ PFRES_TRANSLATE,	"15(translate)" },
     61   1.9  christos 	{ PFRES_NOROUTE,	"16(no-route)" },
     62   1.9  christos #elif defined(__APPLE__)
     63   1.9  christos 	{ PFRES_DUMMYNET,	"15(dummynet)" },
     64   1.9  christos #endif
     65   1.1  christos 	{ 0,	NULL }
     66   1.1  christos };
     67   1.1  christos 
     68   1.4  christos static const struct tok pf_actions[] = {
     69   1.1  christos 	{ PF_PASS,		"pass" },
     70   1.1  christos 	{ PF_DROP,		"block" },
     71   1.1  christos 	{ PF_SCRUB,		"scrub" },
     72  1.11  christos 	{ PF_NOSCRUB,		"noscrub" },
     73   1.1  christos 	{ PF_NAT,		"nat" },
     74   1.9  christos 	{ PF_NONAT,		"nonat" },
     75   1.1  christos 	{ PF_BINAT,		"binat" },
     76   1.9  christos 	{ PF_NOBINAT,		"nobinat" },
     77   1.1  christos 	{ PF_RDR,		"rdr" },
     78   1.9  christos 	{ PF_NORDR,		"nordr" },
     79   1.1  christos 	{ PF_SYNPROXY_DROP,	"synproxy-drop" },
     80   1.9  christos #if defined(__FreeBSD__)
     81   1.9  christos 	{ PF_DEFER,		"defer" },
     82  1.11  christos 	{ PF_MATCH,		"match" },
     83   1.9  christos #elif defined(__OpenBSD__)
     84   1.9  christos 	{ PF_DEFER,		"defer" },
     85   1.9  christos 	{ PF_MATCH,		"match" },
     86   1.9  christos 	{ PF_DIVERT,		"divert" },
     87   1.9  christos 	{ PF_RT,		"rt" },
     88   1.9  christos 	{ PF_AFRT,		"afrt" },
     89   1.9  christos #elif defined(__APPLE__)
     90   1.9  christos 	{ PF_DUMMYNET,		"dummynet" },
     91   1.9  christos 	{ PF_NODUMMYNET,	"nodummynet" },
     92   1.9  christos 	{ PF_NAT64,		"nat64" },
     93   1.9  christos 	{ PF_NONAT64,		"nonat64" },
     94   1.9  christos #endif
     95   1.1  christos 	{ 0,			NULL }
     96   1.1  christos };
     97   1.1  christos 
     98   1.4  christos static const struct tok pf_directions[] = {
     99   1.1  christos 	{ PF_INOUT,	"in/out" },
    100   1.1  christos 	{ PF_IN,	"in" },
    101   1.1  christos 	{ PF_OUT,	"out" },
    102   1.9  christos #if defined(__OpenBSD__)
    103   1.9  christos 	{ PF_FWD,	"fwd" },
    104   1.9  christos #endif
    105   1.1  christos 	{ 0,		NULL }
    106   1.1  christos };
    107   1.1  christos 
    108   1.1  christos static void
    109   1.5  christos pflog_print(netdissect_options *ndo, const struct pfloghdr *hdr)
    110   1.1  christos {
    111   1.5  christos 	uint32_t rulenr, subrulenr;
    112   1.1  christos 
    113   1.9  christos 	ndo->ndo_protocol = "pflog";
    114  1.10  christos 	rulenr = GET_BE_U_4(hdr->rulenr);
    115  1.10  christos 	subrulenr = GET_BE_U_4(hdr->subrulenr);
    116   1.5  christos 	if (subrulenr == (uint32_t)-1)
    117   1.9  christos 		ND_PRINT("rule %u/", rulenr);
    118   1.9  christos 	else {
    119   1.9  christos 		ND_PRINT("rule %u.", rulenr);
    120   1.9  christos 		nd_printjnp(ndo, (const u_char*)hdr->ruleset, PFLOG_RULESET_NAME_SIZE);
    121   1.9  christos 		ND_PRINT(".%u/", subrulenr);
    122   1.9  christos 	}
    123   1.9  christos 
    124   1.9  christos 	ND_PRINT("%s: %s %s on ",
    125  1.10  christos 	    tok2str(pf_reasons, "unkn(%u)", GET_U_1(hdr->reason)),
    126  1.10  christos 	    tok2str(pf_actions, "unkn(%u)", GET_U_1(hdr->action)),
    127  1.10  christos 	    tok2str(pf_directions, "unkn(%u)", GET_U_1(hdr->dir)));
    128   1.9  christos 	nd_printjnp(ndo, (const u_char*)hdr->ifname, PFLOG_IFNAMSIZ);
    129   1.9  christos 	ND_PRINT(": ");
    130   1.1  christos }
    131   1.1  christos 
    132   1.9  christos void
    133   1.5  christos pflog_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h,
    134   1.9  christos                const u_char *p)
    135   1.1  christos {
    136   1.1  christos 	u_int length = h->len;
    137   1.1  christos 	u_int hdrlen;
    138   1.1  christos 	u_int caplen = h->caplen;
    139   1.1  christos 	const struct pfloghdr *hdr;
    140   1.5  christos 	uint8_t af;
    141   1.1  christos 
    142   1.9  christos 	ndo->ndo_protocol = "pflog";
    143   1.1  christos 	/* check length */
    144   1.5  christos 	if (caplen < sizeof(uint8_t)) {
    145   1.9  christos 		nd_print_trunc(ndo);
    146   1.9  christos 		ndo->ndo_ll_hdr_len += h->caplen;
    147   1.9  christos 		return;
    148   1.1  christos 	}
    149   1.1  christos 
    150   1.8       spz 	hdr = (const struct pfloghdr *)p;
    151  1.10  christos 	hdrlen = GET_U_1(hdr->length);
    152  1.10  christos 	if (hdrlen < MIN_PFLOG_HDRLEN) {
    153   1.9  christos 		ND_PRINT("[pflog: invalid header length!]");
    154  1.10  christos 		ndo->ndo_ll_hdr_len += hdrlen;	/* XXX: not really */
    155   1.9  christos 		return;
    156   1.1  christos 	}
    157  1.10  christos 	hdrlen = roundup2(hdrlen, 4);
    158   1.1  christos 
    159   1.1  christos 	if (caplen < hdrlen) {
    160   1.9  christos 		nd_print_trunc(ndo);
    161   1.9  christos 		ndo->ndo_ll_hdr_len += hdrlen;	/* XXX: true? */
    162   1.9  christos 		return;
    163   1.1  christos 	}
    164   1.1  christos 
    165   1.1  christos 	/* print what we know */
    166   1.9  christos 	ND_TCHECK_SIZE(hdr);
    167   1.5  christos 	if (ndo->ndo_eflag)
    168   1.5  christos 		pflog_print(ndo, hdr);
    169   1.5  christos 
    170   1.1  christos 	/* skip to the real packet */
    171  1.10  christos 	af = GET_U_1(hdr->af);
    172   1.1  christos 	length -= hdrlen;
    173   1.1  christos 	caplen -= hdrlen;
    174   1.1  christos 	p += hdrlen;
    175   1.1  christos 	switch (af) {
    176   1.1  christos 
    177   1.9  christos 		/*
    178   1.9  christos 		 * If there's a system that doesn't use the AF_INET
    179   1.9  christos 		 * from 4.2BSD, feel free to add its value to af.h
    180   1.9  christos 		 * and use it here.
    181   1.9  christos 		 *
    182   1.9  christos 		 * Hopefully, there isn't.
    183   1.9  christos 		 */
    184   1.9  christos 		case BSD_AFNUM_INET:
    185   1.5  christos 		        ip_print(ndo, p, length);
    186   1.1  christos 			break;
    187   1.1  christos 
    188   1.9  christos 		/*
    189   1.9  christos 		 * Try all AF_INET6 values for all systems with pflog,
    190   1.9  christos 		 * including Darwin.
    191   1.9  christos 		 */
    192   1.9  christos 		case BSD_AFNUM_INET6_BSD:
    193   1.9  christos 		case BSD_AFNUM_INET6_FREEBSD:
    194   1.9  christos 		case BSD_AFNUM_INET6_DARWIN:
    195   1.5  christos 			ip6_print(ndo, p, length);
    196   1.1  christos 			break;
    197   1.1  christos 
    198   1.1  christos 	default:
    199   1.1  christos 		/* address family not handled, print raw packet */
    200   1.5  christos 		if (!ndo->ndo_eflag)
    201   1.5  christos 			pflog_print(ndo, hdr);
    202   1.5  christos 		if (!ndo->ndo_suppress_default_print)
    203   1.5  christos 			ND_DEFAULTPRINT(p, caplen);
    204   1.1  christos 	}
    205   1.5  christos 
    206   1.9  christos 	ndo->ndo_ll_hdr_len += hdrlen;
    207   1.9  christos 	return;
    208   1.1  christos trunc:
    209   1.9  christos 	nd_print_trunc(ndo);
    210   1.9  christos 	ndo->ndo_ll_hdr_len += hdrlen;
    211   1.1  christos }
    212