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.9 2023/08/17 20:19:40 christos Exp $"); 25 #endif 26 27 /* \summary: *BSD/Darwin packet filter log file printer */ 28 29 #ifdef HAVE_CONFIG_H 30 #include <config.h> 31 #endif 32 33 #include "netdissect-stdinc.h" 34 35 #include "netdissect.h" 36 #include "extract.h" 37 #include "af.h" 38 39 #include "pflog.h" 40 41 static const struct tok pf_reasons[] = { 42 { PFRES_MATCH, "0(match)" }, 43 { PFRES_BADOFF, "1(bad-offset)" }, 44 { PFRES_FRAG, "2(fragment)" }, 45 { PFRES_NORM, "3(short)" }, 46 { PFRES_NORM, "4(normalize)" }, 47 { PFRES_MEMORY, "5(memory)" }, 48 { PFRES_TS, "6(bad-timestamp)" }, 49 { PFRES_CONGEST, "7(congestion)" }, 50 { PFRES_IPOPTIONS, "8(ip-option)" }, 51 { PFRES_PROTCKSUM, "9(proto-cksum)" }, 52 { PFRES_BADSTATE, "10(state-mismatch)" }, 53 { PFRES_STATEINS, "11(state-insert)" }, 54 { PFRES_MAXSTATES, "12(state-limit)" }, 55 { PFRES_SRCLIMIT, "13(src-limit)" }, 56 { PFRES_SYNPROXY, "14(synproxy)" }, 57 #if defined(__FreeBSD__) 58 { PFRES_MAPFAILED, "15(map-failed)" }, 59 #elif defined(__NetBSD__) 60 { PFRES_STATELOCKED, "15(state-locked)" }, 61 #elif defined(__OpenBSD__) 62 { PFRES_TRANSLATE, "15(translate)" }, 63 { PFRES_NOROUTE, "16(no-route)" }, 64 #elif defined(__APPLE__) 65 { PFRES_DUMMYNET, "15(dummynet)" }, 66 #endif 67 { 0, NULL } 68 }; 69 70 static const struct tok pf_actions[] = { 71 { PF_PASS, "pass" }, 72 { PF_DROP, "block" }, 73 { PF_SCRUB, "scrub" }, 74 { PF_NAT, "nat" }, 75 { PF_NONAT, "nonat" }, 76 { PF_BINAT, "binat" }, 77 { PF_NOBINAT, "nobinat" }, 78 { PF_RDR, "rdr" }, 79 { PF_NORDR, "nordr" }, 80 { PF_SYNPROXY_DROP, "synproxy-drop" }, 81 #if defined(__FreeBSD__) 82 { PF_DEFER, "defer" }, 83 #elif defined(__OpenBSD__) 84 { PF_DEFER, "defer" }, 85 { PF_MATCH, "match" }, 86 { PF_DIVERT, "divert" }, 87 { PF_RT, "rt" }, 88 { PF_AFRT, "afrt" }, 89 #elif defined(__APPLE__) 90 { PF_DUMMYNET, "dummynet" }, 91 { PF_NODUMMYNET, "nodummynet" }, 92 { PF_NAT64, "nat64" }, 93 { PF_NONAT64, "nonat64" }, 94 #endif 95 { 0, NULL } 96 }; 97 98 static const struct tok pf_directions[] = { 99 { PF_INOUT, "in/out" }, 100 { PF_IN, "in" }, 101 { PF_OUT, "out" }, 102 #if defined(__OpenBSD__) 103 { PF_FWD, "fwd" }, 104 #endif 105 { 0, NULL } 106 }; 107 108 static void 109 pflog_print(netdissect_options *ndo, const struct pfloghdr *hdr) 110 { 111 uint32_t rulenr, subrulenr; 112 113 ndo->ndo_protocol = "pflog"; 114 rulenr = GET_BE_U_4(&hdr->rulenr); 115 subrulenr = GET_BE_U_4(&hdr->subrulenr); 116 if (subrulenr == (uint32_t)-1) 117 ND_PRINT("rule %u/", rulenr); 118 else { 119 ND_PRINT("rule %u.", rulenr); 120 nd_printjnp(ndo, (const u_char*)hdr->ruleset, PFLOG_RULESET_NAME_SIZE); 121 ND_PRINT(".%u/", subrulenr); 122 } 123 124 ND_PRINT("%s: %s %s on ", 125 tok2str(pf_reasons, "unkn(%u)", GET_U_1(&hdr->reason)), 126 tok2str(pf_actions, "unkn(%u)", GET_U_1(&hdr->action)), 127 tok2str(pf_directions, "unkn(%u)", GET_U_1(&hdr->dir))); 128 nd_printjnp(ndo, (const u_char*)hdr->ifname, PFLOG_IFNAMSIZ); 129 ND_PRINT(": "); 130 } 131 132 void 133 pflog_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, 134 const u_char *p) 135 { 136 u_int length = h->len; 137 u_int hdrlen; 138 u_int caplen = h->caplen; 139 const struct pfloghdr *hdr; 140 uint8_t af; 141 142 ndo->ndo_protocol = "pflog"; 143 /* check length */ 144 if (caplen < sizeof(uint8_t)) { 145 nd_print_trunc(ndo); 146 ndo->ndo_ll_hdr_len += h->caplen; 147 return; 148 } 149 150 #define MIN_PFLOG_HDRLEN 45 151 hdr = (const struct pfloghdr *)p; 152 if (GET_U_1(&hdr->length) < MIN_PFLOG_HDRLEN) { 153 ND_PRINT("[pflog: invalid header length!]"); 154 ndo->ndo_ll_hdr_len += GET_U_1(&hdr->length); /* XXX: not really */ 155 return; 156 } 157 hdrlen = roundup2(hdr->length, 4); 158 159 if (caplen < hdrlen) { 160 nd_print_trunc(ndo); 161 ndo->ndo_ll_hdr_len += hdrlen; /* XXX: true? */ 162 return; 163 } 164 165 /* print what we know */ 166 ND_TCHECK_SIZE(hdr); 167 if (ndo->ndo_eflag) 168 pflog_print(ndo, hdr); 169 170 /* skip to the real packet */ 171 af = GET_U_1(&hdr->af); 172 length -= hdrlen; 173 caplen -= hdrlen; 174 p += hdrlen; 175 switch (af) { 176 177 /* 178 * If there's a system that doesn't use the AF_INET 179 * from 4.2BSD, feel free to add its value to af.h 180 * and use it here. 181 * 182 * Hopefully, there isn't. 183 */ 184 case BSD_AFNUM_INET: 185 ip_print(ndo, p, length); 186 break; 187 188 /* 189 * Try all AF_INET6 values for all systems with pflog, 190 * including Darwin. 191 */ 192 case BSD_AFNUM_INET6_BSD: 193 case BSD_AFNUM_INET6_FREEBSD: 194 case BSD_AFNUM_INET6_DARWIN: 195 ip6_print(ndo, p, length); 196 break; 197 198 default: 199 /* address family not handled, print raw packet */ 200 if (!ndo->ndo_eflag) 201 pflog_print(ndo, hdr); 202 if (!ndo->ndo_suppress_default_print) 203 ND_DEFAULTPRINT(p, caplen); 204 } 205 206 ndo->ndo_ll_hdr_len += hdrlen; 207 return; 208 trunc: 209 nd_print_trunc(ndo); 210 ndo->ndo_ll_hdr_len += hdrlen; 211 } 212