1 /* $NetBSD: print-pfsync.c,v 1.1 2010/12/05 05:11:30 christos Exp $ */ 2 /* $OpenBSD: print-pfsync.c,v 1.30 2007/05/31 04:16:26 mcbride Exp $ */ 3 4 /* 5 * Copyright (c) 2002 Michael Shalayeff 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT, 21 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 23 * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 25 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 26 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 27 * THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30 #include <sys/cdefs.h> 31 #ifndef lint 32 #if 0 33 static const char rcsid[] = 34 "@(#) $Header: /cvsroot/src/external/bsd/tcpdump/dist/print-pfsync.c,v 1.1 2010/12/05 05:11:30 christos Exp $"; 35 #else 36 __RCSID("$NetBSD: print-pfsync.c,v 1.1 2010/12/05 05:11:30 christos Exp $"); 37 #endif 38 #endif 39 40 #ifdef HAVE_CONFIG_H 41 #include "config.h" 42 #endif 43 44 #include <sys/param.h> 45 #include <sys/time.h> 46 #include <sys/socket.h> 47 #include <sys/file.h> 48 #include <sys/ioctl.h> 49 #include <sys/mbuf.h> 50 51 #ifdef __STDC__ 52 struct rtentry; 53 #endif 54 #include <net/if.h> 55 56 #include <netinet/in.h> 57 #include <netinet/in_systm.h> 58 #include <netinet/ip.h> 59 60 #include <net/pfvar.h> 61 #include <net/if_pfsync.h> 62 63 #include <ctype.h> 64 #include <netdb.h> 65 #include <pcap.h> 66 #include <signal.h> 67 #include <stdio.h> 68 #include <string.h> 69 70 #include "interface.h" 71 #include "addrtoname.h" 72 #include "pfctl_parser.h" 73 #include "pfctl.h" 74 75 const char *pfsync_acts[] = { PFSYNC_ACTIONS }; 76 77 static void pfsync_print(struct pfsync_header *, int); 78 79 u_int 80 pfsync_if_print(const struct pcap_pkthdr *h, const u_char *p) 81 { 82 u_int caplen = h->caplen; 83 84 ts_print(&h->ts); 85 86 if (caplen < PFSYNC_HDRLEN) { 87 printf("[|pfsync]"); 88 goto out; 89 } 90 91 pfsync_print((struct pfsync_header *)p, 92 caplen - sizeof(struct pfsync_header)); 93 out: 94 if (xflag) { 95 default_print((const u_char *)h, caplen); 96 } 97 //putchar('\n'); 98 99 return 0; 100 } 101 102 void 103 pfsync_ip_print(const u_char *bp, u_int len, const u_char *bp2 __unused) 104 { 105 struct pfsync_header *hdr = (struct pfsync_header *)bp; 106 107 if (len < PFSYNC_HDRLEN) 108 printf("[|pfsync]"); 109 else 110 pfsync_print(hdr, (len - sizeof(struct pfsync_header))); 111 //putchar('\n'); 112 } 113 114 static void 115 pfsync_print(struct pfsync_header *hdr, int len) 116 { 117 struct pfsync_state *s; 118 struct pfsync_state_upd *u; 119 struct pfsync_state_del *d; 120 struct pfsync_state_clr *c; 121 struct pfsync_state_upd_req *r; 122 struct pfsync_state_bus *b; 123 struct pfsync_tdb *t; 124 int i, flags = 0, min, sec; 125 u_int64_t id; 126 127 if (eflag) 128 printf("PFSYNCv%d count %d: ", 129 hdr->version, hdr->count); 130 131 if (hdr->action < PFSYNC_ACT_MAX) 132 printf("%s %s:", (vflag == 0) ? "PFSYNC" : "", 133 pfsync_acts[hdr->action]); 134 else 135 printf("%s %d?:", (vflag == 0) ? "PFSYNC" : "", 136 hdr->action); 137 138 if (!vflag) 139 return; 140 if (vflag) 141 flags |= PF_OPT_VERBOSE; 142 if (vflag > 1) 143 flags |= PF_OPT_VERBOSE2; 144 if (!nflag) 145 flags |= PF_OPT_USEDNS; 146 147 switch (hdr->action) { 148 case PFSYNC_ACT_CLR: 149 if (sizeof(*c) <= len) { 150 c = (void *)((char *)hdr + PFSYNC_HDRLEN); 151 printf("\n\tcreatorid: %08x", htonl(c->creatorid)); 152 if (c->ifname[0] != '\0') 153 printf(" interface: %s", c->ifname); 154 } 155 case PFSYNC_ACT_INS: 156 case PFSYNC_ACT_UPD: 157 case PFSYNC_ACT_DEL: 158 for (i = 1, s = (void *)((char *)hdr + PFSYNC_HDRLEN); 159 i <= hdr->count && i * sizeof(*s) <= len; i++, s++) { 160 161 putchar('\n'); 162 print_state(s, flags); 163 if (vflag > 1 && hdr->action == PFSYNC_ACT_UPD) 164 printf(" updates: %d", s->updates); 165 } 166 break; 167 case PFSYNC_ACT_UPD_C: 168 for (i = 1, u = (void *)((char *)hdr + PFSYNC_HDRLEN); 169 i <= hdr->count && i * sizeof(*u) <= len; i++, u++) { 170 memcpy(&id, &u->id, sizeof(id)); 171 printf("\n\tid: %" PRIu64 " creatorid: %08x", 172 be64toh(id), ntohl(u->creatorid)); 173 if (vflag > 1) 174 printf(" updates: %d", u->updates); 175 } 176 break; 177 case PFSYNC_ACT_DEL_C: 178 for (i = 1, d = (void *)((char *)hdr + PFSYNC_HDRLEN); 179 i <= hdr->count && i * sizeof(*d) <= len; i++, d++) { 180 memcpy(&id, &d->id, sizeof(id)); 181 printf("\n\tid: %" PRIu64 " creatorid: %08x", 182 be64toh(id), ntohl(d->creatorid)); 183 } 184 break; 185 case PFSYNC_ACT_UREQ: 186 for (i = 1, r = (void *)((char *)hdr + PFSYNC_HDRLEN); 187 i <= hdr->count && i * sizeof(*r) <= len; i++, r++) { 188 memcpy(&id, &r->id, sizeof(id)); 189 printf("\n\tid: %" PRIu64 " creatorid: %08x", 190 be64toh(id), ntohl(r->creatorid)); 191 } 192 break; 193 case PFSYNC_ACT_BUS: 194 if (sizeof(*b) <= len) { 195 b = (void *)((char *)hdr + PFSYNC_HDRLEN); 196 printf("\n\tcreatorid: %08x", htonl(b->creatorid)); 197 sec = b->endtime % 60; 198 b->endtime /= 60; 199 min = b->endtime % 60; 200 b->endtime /= 60; 201 printf(" age %.2u:%.2u:%.2u", b->endtime, min, sec); 202 switch (b->status) { 203 case PFSYNC_BUS_START: 204 printf(" status: start"); 205 break; 206 case PFSYNC_BUS_END: 207 printf(" status: end"); 208 break; 209 default: 210 printf(" status: ?"); 211 break; 212 } 213 } 214 break; 215 case PFSYNC_ACT_TDB_UPD: 216 for (i = 1, t = (void *)((char *)hdr + PFSYNC_HDRLEN); 217 i <= hdr->count && i * sizeof(*t) <= len; i++, t++) 218 printf("\n\tspi: %08x rpl: %u cur_bytes: %" PRIu64, 219 htonl(t->spi), htonl(t->rpl), 220 be64toh(t->cur_bytes)); 221 /* XXX add dst and sproto? */ 222 break; 223 default: 224 break; 225 } 226 } 227