1 /* $OpenBSD: print-pflog.c,v 1.29 2016/04/04 16:26:00 sthen 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 #include <sys/proc.h> 32 33 #ifndef NO_PID 34 #define NO_PID (32766+1) 35 #endif 36 37 struct rtentry; 38 39 #include <netinet/in.h> 40 #include <netinet/ip.h> 41 #include <net/if.h> 42 #include <net/pfvar.h> 43 #include <net/if_pflog.h> 44 45 #include <arpa/inet.h> 46 47 #include <ctype.h> 48 #include <netdb.h> 49 #include <pcap.h> 50 #include <signal.h> 51 #include <stdio.h> 52 53 #include "interface.h" 54 #include "addrtoname.h" 55 56 char *pf_reasons[PFRES_MAX+2] = PFRES_NAMES; 57 58 void 59 pflog_if_print(u_char *user, const struct pcap_pkthdr *h, 60 const u_char *p) 61 { 62 u_int length = h->len; 63 u_int hdrlen; 64 u_int caplen = h->caplen; 65 const struct ip *ip; 66 #ifdef INET6 67 const struct ip6_hdr *ip6; 68 #endif 69 const struct pfloghdr *hdr; 70 u_int8_t af; 71 72 ts_print(&h->ts); 73 74 /* check length */ 75 if (caplen < sizeof(u_int8_t)) { 76 printf("[|pflog]"); 77 goto out; 78 } 79 80 #define MIN_PFLOG_HDRLEN 45 81 hdr = (struct pfloghdr *)p; 82 if (hdr->length < MIN_PFLOG_HDRLEN) { 83 printf("[pflog: invalid header length!]"); 84 goto out; 85 } 86 hdrlen = (hdr->length + 3) & 0xfc; 87 88 if (caplen < hdrlen) { 89 printf("[|pflog]"); 90 goto out; 91 } 92 93 /* 94 * Some printers want to get back at the link level addresses, 95 * and/or check that they're not walking off the end of the packet. 96 * Rather than pass them all the way down, we set these globals. 97 */ 98 packetp = p; 99 snapend = p + caplen; 100 101 hdr = (struct pfloghdr *)p; 102 if (eflag) { 103 printf("rule "); 104 if (ntohl(hdr->rulenr) == (u_int32_t) -1) 105 printf("def"); 106 else { 107 printf("%u", ntohl(hdr->rulenr)); 108 if (hdr->ruleset[0]) { 109 printf(".%s", hdr->ruleset); 110 if (ntohl(hdr->subrulenr) == (u_int32_t) -1) 111 printf(".def"); 112 else 113 printf(".%u", ntohl(hdr->subrulenr)); 114 } 115 } 116 if (hdr->reason < PFRES_MAX) 117 printf("/(%s) ", pf_reasons[hdr->reason]); 118 else 119 printf("/(unkn %u) ", (unsigned)hdr->reason); 120 if (vflag) 121 printf("[uid %u, pid %u] ", (unsigned)hdr->rule_uid, 122 (unsigned)hdr->rule_pid); 123 124 switch (hdr->action) { 125 case PF_MATCH: 126 printf("match"); 127 break; 128 case PF_SCRUB: 129 printf("scrub"); 130 break; 131 case PF_PASS: 132 printf("pass"); 133 break; 134 case PF_DROP: 135 printf("block"); 136 break; 137 case PF_NAT: 138 case PF_NONAT: 139 printf("nat"); 140 break; 141 case PF_BINAT: 142 case PF_NOBINAT: 143 printf("binat"); 144 break; 145 case PF_RDR: 146 case PF_NORDR: 147 printf("rdr"); 148 break; 149 } 150 printf(" %s on %s: ", 151 hdr->dir == PF_OUT ? "out" : "in", 152 hdr->ifname); 153 if (vflag && hdr->pid != NO_PID) 154 printf("[uid %u, pid %u] ", (unsigned)hdr->uid, 155 (unsigned)hdr->pid); 156 if (vflag && hdr->rewritten) { 157 char buf[48]; 158 159 if (inet_ntop(hdr->af, &hdr->saddr.v4, buf, 160 sizeof(buf)) == NULL) 161 printf("[orig src ?, "); 162 else 163 printf("[orig src %s:%u, ", buf, 164 ntohs(hdr->sport)); 165 if (inet_ntop(hdr->af, &hdr->daddr.v4, buf, 166 sizeof(buf)) == NULL) 167 printf("dst ?] "); 168 else 169 printf("dst %s:%u] ", buf, 170 ntohs(hdr->dport)); 171 } 172 } 173 af = hdr->naf; 174 length -= hdrlen; 175 if (af == AF_INET) { 176 ip = (struct ip *)(p + hdrlen); 177 ip_print((const u_char *)ip, length); 178 if (xflag) 179 default_print((const u_char *)ip, 180 caplen - hdrlen); 181 } else { 182 #ifdef INET6 183 ip6 = (struct ip6_hdr *)(p + hdrlen); 184 ip6_print((const u_char *)ip6, length); 185 if (xflag) 186 default_print((const u_char *)ip6, 187 caplen - hdrlen); 188 #endif 189 } 190 191 out: 192 putchar('\n'); 193 } 194