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