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