1 /* $NetBSD: print-pfsync.c,v 1.2 2014/11/20 03:05:03 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.2 2014/11/20 03:05:03 christos Exp $"; 35 #else 36 __RCSID("$NetBSD: print-pfsync.c,v 1.2 2014/11/20 03:05:03 christos Exp $"); 37 #endif 38 #endif 39 40 #define NETDISECT_REWORKED 41 #ifdef HAVE_CONFIG_H 42 #include "config.h" 43 #endif 44 45 #include <tcpdump-stdinc.h> 46 47 #include <sys/param.h> 48 #include <sys/time.h> 49 #include <sys/socket.h> 50 #include <sys/file.h> 51 #include <sys/ioctl.h> 52 #include <sys/mbuf.h> 53 54 #ifdef __STDC__ 55 struct rtentry; 56 #endif 57 #include <net/if.h> 58 59 #if 0 60 #include <netinet/in.h> 61 #include <netinet/in_systm.h> 62 #include <netinet/ip.h> 63 #endif 64 65 #include <net/pfvar.h> 66 #include <net/if_pfsync.h> 67 68 #include <ctype.h> 69 #include <netdb.h> 70 #include <pcap.h> 71 #include <signal.h> 72 #include <stdio.h> 73 #include <string.h> 74 75 #include "interface.h" 76 #include "addrtoname.h" 77 #include "pfctl_parser.h" 78 #include "pfctl.h" 79 80 const char *pfsync_acts[] = { PFSYNC_ACTIONS }; 81 82 static void pfsync_print(struct pfsync_header *, int); 83 84 u_int 85 pfsync_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, const u_char *p) 86 { 87 u_int caplen = h->caplen; 88 89 ts_print(ndo, &h->ts); 90 91 if (caplen < PFSYNC_HDRLEN) { 92 ND_PRINT((ndo, "[|pfsync]")); 93 goto out; 94 } 95 96 pfsync_print((struct pfsync_header *)p, 97 caplen - sizeof(struct pfsync_header)); 98 out: 99 if (xflag) { 100 default_print((const u_char *)h, caplen); 101 } 102 //putchar('\n'); 103 104 return 0; 105 } 106 107 void 108 pfsync_ip_print(const u_char *bp, u_int len, const u_char *bp2 __unused) 109 { 110 struct pfsync_header *hdr = (struct pfsync_header *)bp; 111 112 if (len < PFSYNC_HDRLEN) 113 printf("[|pfsync]"); 114 else 115 pfsync_print(hdr, (len - sizeof(struct pfsync_header))); 116 //putchar('\n'); 117 } 118 119 static void 120 pfsync_print(struct pfsync_header *hdr, int len) 121 { 122 struct pfsync_state *s; 123 struct pfsync_state_upd *u; 124 struct pfsync_state_del *d; 125 struct pfsync_state_clr *c; 126 struct pfsync_state_upd_req *r; 127 struct pfsync_state_bus *b; 128 struct pfsync_tdb *t; 129 int i, flags = 0, min, sec; 130 u_int64_t id; 131 132 if (eflag) 133 printf("PFSYNCv%d count %d: ", 134 hdr->version, hdr->count); 135 136 if (hdr->action < PFSYNC_ACT_MAX) 137 printf("%s %s:", (vflag == 0) ? "PFSYNC" : "", 138 pfsync_acts[hdr->action]); 139 else 140 printf("%s %d?:", (vflag == 0) ? "PFSYNC" : "", 141 hdr->action); 142 143 if (!vflag) 144 return; 145 if (vflag) 146 flags |= PF_OPT_VERBOSE; 147 if (vflag > 1) 148 flags |= PF_OPT_VERBOSE2; 149 if (!nflag) 150 flags |= PF_OPT_USEDNS; 151 152 switch (hdr->action) { 153 case PFSYNC_ACT_CLR: 154 if (sizeof(*c) <= len) { 155 c = (void *)((char *)hdr + PFSYNC_HDRLEN); 156 printf("\n\tcreatorid: %08x", htonl(c->creatorid)); 157 if (c->ifname[0] != '\0') 158 printf(" interface: %s", c->ifname); 159 } 160 case PFSYNC_ACT_INS: 161 case PFSYNC_ACT_UPD: 162 case PFSYNC_ACT_DEL: 163 for (i = 1, s = (void *)((char *)hdr + PFSYNC_HDRLEN); 164 i <= hdr->count && i * sizeof(*s) <= len; i++, s++) { 165 166 putchar('\n'); 167 print_state(s, flags); 168 if (vflag > 1 && hdr->action == PFSYNC_ACT_UPD) 169 printf(" updates: %d", s->updates); 170 } 171 break; 172 case PFSYNC_ACT_UPD_C: 173 for (i = 1, u = (void *)((char *)hdr + PFSYNC_HDRLEN); 174 i <= hdr->count && i * sizeof(*u) <= len; i++, u++) { 175 memcpy(&id, &u->id, sizeof(id)); 176 printf("\n\tid: %" PRIu64 " creatorid: %08x", 177 be64toh(id), ntohl(u->creatorid)); 178 if (vflag > 1) 179 printf(" updates: %d", u->updates); 180 } 181 break; 182 case PFSYNC_ACT_DEL_C: 183 for (i = 1, d = (void *)((char *)hdr + PFSYNC_HDRLEN); 184 i <= hdr->count && i * sizeof(*d) <= len; i++, d++) { 185 memcpy(&id, &d->id, sizeof(id)); 186 printf("\n\tid: %" PRIu64 " creatorid: %08x", 187 be64toh(id), ntohl(d->creatorid)); 188 } 189 break; 190 case PFSYNC_ACT_UREQ: 191 for (i = 1, r = (void *)((char *)hdr + PFSYNC_HDRLEN); 192 i <= hdr->count && i * sizeof(*r) <= len; i++, r++) { 193 memcpy(&id, &r->id, sizeof(id)); 194 printf("\n\tid: %" PRIu64 " creatorid: %08x", 195 be64toh(id), ntohl(r->creatorid)); 196 } 197 break; 198 case PFSYNC_ACT_BUS: 199 if (sizeof(*b) <= len) { 200 b = (void *)((char *)hdr + PFSYNC_HDRLEN); 201 printf("\n\tcreatorid: %08x", htonl(b->creatorid)); 202 sec = b->endtime % 60; 203 b->endtime /= 60; 204 min = b->endtime % 60; 205 b->endtime /= 60; 206 printf(" age %.2u:%.2u:%.2u", b->endtime, min, sec); 207 switch (b->status) { 208 case PFSYNC_BUS_START: 209 printf(" status: start"); 210 break; 211 case PFSYNC_BUS_END: 212 printf(" status: end"); 213 break; 214 default: 215 printf(" status: ?"); 216 break; 217 } 218 } 219 break; 220 case PFSYNC_ACT_TDB_UPD: 221 for (i = 1, t = (void *)((char *)hdr + PFSYNC_HDRLEN); 222 i <= hdr->count && i * sizeof(*t) <= len; i++, t++) 223 printf("\n\tspi: %08x rpl: %u cur_bytes: %" PRIu64, 224 htonl(t->spi), htonl(t->rpl), 225 be64toh(t->cur_bytes)); 226 /* XXX add dst and sproto? */ 227 break; 228 default: 229 break; 230 } 231 } 232