1 /* $OpenBSD: print-pflog.c,v 1.32 2018/10/22 16:12:45 kn Exp $ */ 2 3 /* 4 * Copyright (c) 1990, 1991, 1993, 1994, 1995, 1996 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that: (1) source code distributions 9 * retain the above copyright notice and this paragraph in its entirety, (2) 10 * distributions including binary code include the above copyright notice and 11 * this paragraph in its entirety in the documentation or other materials 12 * provided with the distribution, and (3) all advertising materials mentioning 13 * features or use of this software display the following acknowledgement: 14 * ``This product includes software developed by the University of California, 15 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of 16 * the University nor the names of its contributors may be used to endorse 17 * or promote products derived from this software without specific prior 18 * written permission. 19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 20 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 22 */ 23 24 #include <sys/param.h> /* MAXCOMLEN */ 25 #include <sys/time.h> 26 #include <sys/socket.h> 27 #include <sys/file.h> 28 #include <sys/ioctl.h> 29 #include <sys/queue.h> 30 #include <sys/mbuf.h> 31 32 #ifndef NO_PID 33 #define NO_PID (99999+1) 34 #endif 35 36 struct rtentry; 37 38 #include <netinet/in.h> 39 #include <netinet/ip.h> 40 #include <net/if.h> 41 #include <net/pfvar.h> 42 #include <net/if_pflog.h> 43 44 #include <arpa/inet.h> 45 46 #include <ctype.h> 47 #include <netdb.h> 48 #include <pcap.h> 49 #include <signal.h> 50 #include <stdio.h> 51 52 #include "interface.h" 53 #include "addrtoname.h" 54 55 char *pf_reasons[PFRES_MAX+2] = PFRES_NAMES; 56 57 void 58 pflog_if_print(u_char *user, const struct pcap_pkthdr *h, 59 const u_char *p) 60 { 61 u_int length = h->len; 62 u_int hdrlen; 63 u_int caplen = h->caplen; 64 const struct ip *ip; 65 const struct ip6_hdr *ip6; 66 const struct pfloghdr *hdr; 67 u_int8_t af; 68 69 ts_print(&h->ts); 70 71 /* check length */ 72 if (caplen < sizeof(u_int8_t)) { 73 printf("[|pflog]"); 74 goto out; 75 } 76 77 #define MIN_PFLOG_HDRLEN 45 78 hdr = (struct pfloghdr *)p; 79 if (hdr->length < MIN_PFLOG_HDRLEN) { 80 printf("[pflog: invalid header length!]"); 81 goto out; 82 } 83 hdrlen = (hdr->length + 3) & 0xfc; 84 85 if (caplen < hdrlen) { 86 printf("[|pflog]"); 87 goto out; 88 } 89 90 /* 91 * Some printers want to get back at the link level addresses, 92 * and/or check that they're not walking off the end of the packet. 93 * Rather than pass them all the way down, we set these globals. 94 */ 95 packetp = p; 96 snapend = p + caplen; 97 98 hdr = (struct pfloghdr *)p; 99 if (eflag) { 100 printf("rule "); 101 if (ntohl(hdr->rulenr) == (u_int32_t) -1) 102 printf("def"); 103 else { 104 printf("%u", ntohl(hdr->rulenr)); 105 if (hdr->ruleset[0]) { 106 printf(".%s", hdr->ruleset); 107 if (ntohl(hdr->subrulenr) == (u_int32_t) -1) 108 printf(".def"); 109 else 110 printf(".%u", ntohl(hdr->subrulenr)); 111 } 112 } 113 if (hdr->reason < PFRES_MAX) 114 printf("/(%s) ", pf_reasons[hdr->reason]); 115 else 116 printf("/(unkn %u) ", (unsigned)hdr->reason); 117 if (vflag) 118 printf("[uid %u, pid %u] ", (unsigned)hdr->rule_uid, 119 (unsigned)hdr->rule_pid); 120 121 switch (hdr->action) { 122 case PF_MATCH: 123 printf("match"); 124 break; 125 case PF_SCRUB: 126 printf("scrub"); 127 break; 128 case PF_PASS: 129 printf("pass"); 130 break; 131 case PF_DROP: 132 printf("block"); 133 break; 134 case PF_NAT: 135 case PF_NONAT: 136 printf("nat"); 137 break; 138 case PF_BINAT: 139 case PF_NOBINAT: 140 printf("binat"); 141 break; 142 case PF_RDR: 143 case PF_NORDR: 144 printf("rdr"); 145 break; 146 } 147 printf(" %s on %s: ", 148 hdr->dir == PF_OUT ? "out" : "in", 149 hdr->ifname); 150 if (vflag && hdr->pid != NO_PID) 151 printf("[uid %u, pid %u] ", (unsigned)hdr->uid, 152 (unsigned)hdr->pid); 153 if (vflag && hdr->rewritten) { 154 char buf[48]; 155 156 if (inet_ntop(hdr->af, &hdr->saddr.v4, buf, 157 sizeof(buf)) == NULL) 158 printf("[orig src ?, "); 159 else 160 printf("[orig src %s:%u, ", buf, 161 ntohs(hdr->sport)); 162 if (inet_ntop(hdr->af, &hdr->daddr.v4, buf, 163 sizeof(buf)) == NULL) 164 printf("dst ?] "); 165 else 166 printf("dst %s:%u] ", buf, 167 ntohs(hdr->dport)); 168 } 169 } 170 af = hdr->naf; 171 length -= hdrlen; 172 if (af == AF_INET) { 173 ip = (struct ip *)(p + hdrlen); 174 ip_print((const u_char *)ip, length); 175 if (xflag) 176 default_print((const u_char *)ip, 177 caplen - hdrlen); 178 } else { 179 ip6 = (struct ip6_hdr *)(p + hdrlen); 180 ip6_print((const u_char *)ip6, length); 181 if (xflag) 182 default_print((const u_char *)ip6, 183 caplen - hdrlen); 184 } 185 186 out: 187 putchar('\n'); 188 } 189