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.8 2017/02/05 04:05:05 spz Exp $"); 25 #endif 26 27 /* \summary: OpenBSD packet filter log file printer */ 28 29 #ifdef HAVE_CONFIG_H 30 #include "config.h" 31 #endif 32 33 #ifndef HAVE_NET_PFVAR_H 34 #error "No pf headers available" 35 #endif 36 #include <sys/types.h> 37 #include <sys/socket.h> 38 #include <net/if.h> 39 #include <net/pfvar.h> 40 #include <net/if_pflog.h> 41 42 #include <netdissect-stdinc.h> 43 44 #include "netdissect.h" 45 #include "extract.h" 46 47 static const char tstr[] = "[|pflog]"; 48 49 static const struct tok pf_reasons[] = { 50 { 0, "0(match)" }, 51 { 1, "1(bad-offset)" }, 52 { 2, "2(fragment)" }, 53 { 3, "3(short)" }, 54 { 4, "4(normalize)" }, 55 { 5, "5(memory)" }, 56 { 6, "6(bad-timestamp)" }, 57 { 7, "7(congestion)" }, 58 { 8, "8(ip-option)" }, 59 { 9, "9(proto-cksum)" }, 60 { 10, "10(state-mismatch)" }, 61 { 11, "11(state-insert)" }, 62 { 12, "12(state-limit)" }, 63 { 13, "13(src-limit)" }, 64 { 14, "14(synproxy)" }, 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, "nat" }, 74 { PF_BINAT, "binat" }, 75 { PF_NOBINAT, "binat" }, 76 { PF_RDR, "rdr" }, 77 { PF_NORDR, "rdr" }, 78 { PF_SYNPROXY_DROP, "synproxy-drop" }, 79 { 0, NULL } 80 }; 81 82 static const struct tok pf_directions[] = { 83 { PF_INOUT, "in/out" }, 84 { PF_IN, "in" }, 85 { PF_OUT, "out" }, 86 { 0, NULL } 87 }; 88 89 /* For reading capture files on other systems */ 90 #define OPENBSD_AF_INET 2 91 #define OPENBSD_AF_INET6 24 92 93 static void 94 pflog_print(netdissect_options *ndo, const struct pfloghdr *hdr) 95 { 96 uint32_t rulenr, subrulenr; 97 98 rulenr = EXTRACT_32BITS(&hdr->rulenr); 99 subrulenr = EXTRACT_32BITS(&hdr->subrulenr); 100 if (subrulenr == (uint32_t)-1) 101 ND_PRINT((ndo, "rule %u/", rulenr)); 102 else 103 ND_PRINT((ndo, "rule %u.%s.%u/", rulenr, hdr->ruleset, subrulenr)); 104 105 ND_PRINT((ndo, "%s: %s %s on %s: ", 106 tok2str(pf_reasons, "unkn(%u)", hdr->reason), 107 tok2str(pf_actions, "unkn(%u)", hdr->action), 108 tok2str(pf_directions, "unkn(%u)", hdr->dir), 109 hdr->ifname)); 110 } 111 112 u_int 113 pflog_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, 114 register const u_char *p) 115 { 116 u_int length = h->len; 117 u_int hdrlen; 118 u_int caplen = h->caplen; 119 const struct pfloghdr *hdr; 120 uint8_t af; 121 122 /* check length */ 123 if (caplen < sizeof(uint8_t)) { 124 ND_PRINT((ndo, "%s", tstr)); 125 return (caplen); 126 } 127 128 #define MIN_PFLOG_HDRLEN 45 129 hdr = (const struct pfloghdr *)p; 130 if (hdr->length < MIN_PFLOG_HDRLEN) { 131 ND_PRINT((ndo, "[pflog: invalid header length!]")); 132 return (hdr->length); /* XXX: not really */ 133 } 134 hdrlen = BPF_WORDALIGN(hdr->length); 135 136 if (caplen < hdrlen) { 137 ND_PRINT((ndo, "%s", tstr)); 138 return (hdrlen); /* XXX: true? */ 139 } 140 141 /* print what we know */ 142 ND_TCHECK(*hdr); 143 if (ndo->ndo_eflag) 144 pflog_print(ndo, hdr); 145 146 /* skip to the real packet */ 147 af = hdr->af; 148 length -= hdrlen; 149 caplen -= hdrlen; 150 p += hdrlen; 151 switch (af) { 152 153 case AF_INET: 154 #if OPENBSD_AF_INET != AF_INET 155 case OPENBSD_AF_INET: /* XXX: read pcap files */ 156 #endif 157 ip_print(ndo, p, length); 158 break; 159 160 #if defined(AF_INET6) || defined(OPENBSD_AF_INET6) 161 #ifdef AF_INET6 162 case AF_INET6: 163 #endif /* AF_INET6 */ 164 #if !defined(AF_INET6) || OPENBSD_AF_INET6 != AF_INET6 165 case OPENBSD_AF_INET6: /* XXX: read pcap files */ 166 #endif /* !defined(AF_INET6) || OPENBSD_AF_INET6 != AF_INET6 */ 167 ip6_print(ndo, p, length); 168 break; 169 #endif /* defined(AF_INET6) || defined(OPENBSD_AF_INET6) */ 170 171 default: 172 /* address family not handled, print raw packet */ 173 if (!ndo->ndo_eflag) 174 pflog_print(ndo, hdr); 175 if (!ndo->ndo_suppress_default_print) 176 ND_DEFAULTPRINT(p, caplen); 177 } 178 179 return (hdrlen); 180 trunc: 181 ND_PRINT((ndo, "%s", tstr)); 182 return (hdrlen); 183 } 184 185 /* 186 * Local Variables: 187 * c-style: whitesmith 188 * c-basic-offset: 8 189 * End: 190 */ 191