1*bc4097aaSchristos /* $NetBSD: printstate.c,v 1.1.1.1 2012/03/23 21:20:10 christos Exp $ */
2*bc4097aaSchristos
3*bc4097aaSchristos /*
4*bc4097aaSchristos * Copyright (C) 2012 by Darren Reed.
5*bc4097aaSchristos *
6*bc4097aaSchristos * See the IPFILTER.LICENCE file for details on licencing.
7*bc4097aaSchristos */
8*bc4097aaSchristos
9*bc4097aaSchristos #include "ipf.h"
10*bc4097aaSchristos #include "kmem.h"
11*bc4097aaSchristos
12*bc4097aaSchristos
13*bc4097aaSchristos ipstate_t *
printstate(sp,opts,now)14*bc4097aaSchristos printstate(sp, opts, now)
15*bc4097aaSchristos ipstate_t *sp;
16*bc4097aaSchristos int opts;
17*bc4097aaSchristos u_long now;
18*bc4097aaSchristos {
19*bc4097aaSchristos struct protoent *pr;
20*bc4097aaSchristos synclist_t ipsync;
21*bc4097aaSchristos
22*bc4097aaSchristos if ((opts & OPT_NORESOLVE) == 0)
23*bc4097aaSchristos pr = getprotobynumber(sp->is_p);
24*bc4097aaSchristos else
25*bc4097aaSchristos pr = NULL;
26*bc4097aaSchristos
27*bc4097aaSchristos PRINTF("%d:", sp->is_v);
28*bc4097aaSchristos if (pr != NULL)
29*bc4097aaSchristos PRINTF("%s", pr->p_name);
30*bc4097aaSchristos else
31*bc4097aaSchristos PRINTF("%d", sp->is_p);
32*bc4097aaSchristos
33*bc4097aaSchristos PRINTF(" src:%s", hostname(sp->is_family, &sp->is_src.in4));
34*bc4097aaSchristos if (sp->is_p == IPPROTO_UDP || sp->is_p == IPPROTO_TCP) {
35*bc4097aaSchristos if (sp->is_flags & IS_WSPORT)
36*bc4097aaSchristos PRINTF(",*");
37*bc4097aaSchristos else
38*bc4097aaSchristos PRINTF(",%d", ntohs(sp->is_sport));
39*bc4097aaSchristos }
40*bc4097aaSchristos
41*bc4097aaSchristos PRINTF(" dst:%s", hostname(sp->is_family, &sp->is_dst.in4));
42*bc4097aaSchristos if (sp->is_p == IPPROTO_UDP || sp->is_p == IPPROTO_TCP) {
43*bc4097aaSchristos if (sp->is_flags & IS_WDPORT)
44*bc4097aaSchristos PRINTF(",*");
45*bc4097aaSchristos else
46*bc4097aaSchristos PRINTF(",%d", ntohs(sp->is_dport));
47*bc4097aaSchristos }
48*bc4097aaSchristos
49*bc4097aaSchristos if (sp->is_p == IPPROTO_TCP) {
50*bc4097aaSchristos PRINTF(" state:%d/%d", sp->is_state[0], sp->is_state[1]);
51*bc4097aaSchristos }
52*bc4097aaSchristos
53*bc4097aaSchristos PRINTF(" %ld", sp->is_die - now);
54*bc4097aaSchristos if (sp->is_phnext == NULL)
55*bc4097aaSchristos PRINTF(" ORPHAN");
56*bc4097aaSchristos if (sp->is_flags & IS_CLONE)
57*bc4097aaSchristos PRINTF(" CLONE");
58*bc4097aaSchristos putchar('\n');
59*bc4097aaSchristos
60*bc4097aaSchristos if (sp->is_p == IPPROTO_TCP) {
61*bc4097aaSchristos PRINTF("\t%x:%x %hu<<%d:%hu<<%d\n",
62*bc4097aaSchristos sp->is_send, sp->is_dend,
63*bc4097aaSchristos sp->is_maxswin, sp->is_swinscale,
64*bc4097aaSchristos sp->is_maxdwin, sp->is_dwinscale);
65*bc4097aaSchristos if ((opts & OPT_VERBOSE) != 0) {
66*bc4097aaSchristos PRINTF("\tcmsk %04x smsk %04x isc %p s0 %08x/%08x\n",
67*bc4097aaSchristos sp->is_smsk[0], sp->is_smsk[1], sp->is_isc,
68*bc4097aaSchristos sp->is_s0[0], sp->is_s0[1]);
69*bc4097aaSchristos PRINTF("\tFWD: ISN inc %x sumd %x\n",
70*bc4097aaSchristos sp->is_isninc[0], sp->is_sumd[0]);
71*bc4097aaSchristos PRINTF("\tREV: ISN inc %x sumd %x\n",
72*bc4097aaSchristos sp->is_isninc[1], sp->is_sumd[1]);
73*bc4097aaSchristos #ifdef IPFILTER_SCAN
74*bc4097aaSchristos PRINTF("\tsbuf[0] [");
75*bc4097aaSchristos printsbuf(sp->is_sbuf[0]);
76*bc4097aaSchristos PRINTF("] sbuf[1] [");
77*bc4097aaSchristos printsbuf(sp->is_sbuf[1]);
78*bc4097aaSchristos PRINTF("]\n");
79*bc4097aaSchristos #endif
80*bc4097aaSchristos }
81*bc4097aaSchristos } else if (sp->is_p == IPPROTO_GRE) {
82*bc4097aaSchristos PRINTF("\tcall %hx/%hx\n", ntohs(sp->is_gre.gs_call[0]),
83*bc4097aaSchristos ntohs(sp->is_gre.gs_call[1]));
84*bc4097aaSchristos } else if (sp->is_p == IPPROTO_ICMP
85*bc4097aaSchristos #ifdef USE_INET6
86*bc4097aaSchristos || sp->is_p == IPPROTO_ICMPV6
87*bc4097aaSchristos #endif
88*bc4097aaSchristos ) {
89*bc4097aaSchristos PRINTF("\tid %hu seq %hu type %d\n", sp->is_icmp.ici_id,
90*bc4097aaSchristos sp->is_icmp.ici_seq, sp->is_icmp.ici_type);
91*bc4097aaSchristos }
92*bc4097aaSchristos
93*bc4097aaSchristos #ifdef USE_QUAD_T
94*bc4097aaSchristos PRINTF("\tFWD: IN pkts %"PRIu64" bytes %"PRIu64" OUT pkts %"PRIu64" bytes %"PRIu64"\n\tREV: IN pkts %"PRIu64" bytes %"PRIu64" OUT pkts %"PRIu64" bytes %"PRIu64"\n",
95*bc4097aaSchristos sp->is_pkts[0], sp->is_bytes[0],
96*bc4097aaSchristos sp->is_pkts[1], sp->is_bytes[1],
97*bc4097aaSchristos sp->is_pkts[2], sp->is_bytes[2],
98*bc4097aaSchristos sp->is_pkts[3], sp->is_bytes[3]);
99*bc4097aaSchristos #else
100*bc4097aaSchristos PRINTF("\tFWD: IN pkts %lu bytes %lu OUT pkts %lu bytes %lu\n\tREV: IN pkts %lu bytes %lu OUT pkts %lu bytes %lu\n",
101*bc4097aaSchristos sp->is_pkts[0], sp->is_bytes[0],
102*bc4097aaSchristos sp->is_pkts[1], sp->is_bytes[1],
103*bc4097aaSchristos sp->is_pkts[2], sp->is_bytes[2],
104*bc4097aaSchristos sp->is_pkts[3], sp->is_bytes[3]);
105*bc4097aaSchristos #endif
106*bc4097aaSchristos
107*bc4097aaSchristos PRINTF("\ttag %u pass %#x = ", sp->is_tag, sp->is_pass);
108*bc4097aaSchristos
109*bc4097aaSchristos /*
110*bc4097aaSchristos * Print out bits set in the result code for the state being
111*bc4097aaSchristos * kept as they would for a rule.
112*bc4097aaSchristos */
113*bc4097aaSchristos if (FR_ISPASS(sp->is_pass)) {
114*bc4097aaSchristos PRINTF("pass");
115*bc4097aaSchristos } else if (FR_ISBLOCK(sp->is_pass)) {
116*bc4097aaSchristos PRINTF("block");
117*bc4097aaSchristos switch (sp->is_pass & FR_RETMASK)
118*bc4097aaSchristos {
119*bc4097aaSchristos case FR_RETICMP :
120*bc4097aaSchristos PRINTF(" return-icmp");
121*bc4097aaSchristos break;
122*bc4097aaSchristos case FR_FAKEICMP :
123*bc4097aaSchristos PRINTF(" return-icmp-as-dest");
124*bc4097aaSchristos break;
125*bc4097aaSchristos case FR_RETRST :
126*bc4097aaSchristos PRINTF(" return-rst");
127*bc4097aaSchristos break;
128*bc4097aaSchristos default :
129*bc4097aaSchristos break;
130*bc4097aaSchristos }
131*bc4097aaSchristos } else if ((sp->is_pass & FR_LOGMASK) == FR_LOG) {
132*bc4097aaSchristos PRINTF("log");
133*bc4097aaSchristos if (sp->is_pass & FR_LOGBODY)
134*bc4097aaSchristos PRINTF(" body");
135*bc4097aaSchristos if (sp->is_pass & FR_LOGFIRST)
136*bc4097aaSchristos PRINTF(" first");
137*bc4097aaSchristos } else if (FR_ISACCOUNT(sp->is_pass)) {
138*bc4097aaSchristos PRINTF("count");
139*bc4097aaSchristos } else if (FR_ISPREAUTH(sp->is_pass)) {
140*bc4097aaSchristos PRINTF("preauth");
141*bc4097aaSchristos } else if (FR_ISAUTH(sp->is_pass))
142*bc4097aaSchristos PRINTF("auth");
143*bc4097aaSchristos
144*bc4097aaSchristos if (sp->is_pass & FR_OUTQUE)
145*bc4097aaSchristos PRINTF(" out");
146*bc4097aaSchristos else
147*bc4097aaSchristos PRINTF(" in");
148*bc4097aaSchristos
149*bc4097aaSchristos if ((sp->is_pass & FR_LOG) != 0) {
150*bc4097aaSchristos PRINTF(" log");
151*bc4097aaSchristos if (sp->is_pass & FR_LOGBODY)
152*bc4097aaSchristos PRINTF(" body");
153*bc4097aaSchristos if (sp->is_pass & FR_LOGFIRST)
154*bc4097aaSchristos PRINTF(" first");
155*bc4097aaSchristos if (sp->is_pass & FR_LOGORBLOCK)
156*bc4097aaSchristos PRINTF(" or-block");
157*bc4097aaSchristos }
158*bc4097aaSchristos if (sp->is_pass & FR_QUICK)
159*bc4097aaSchristos PRINTF(" quick");
160*bc4097aaSchristos if (sp->is_pass & FR_KEEPFRAG)
161*bc4097aaSchristos PRINTF(" keep frags");
162*bc4097aaSchristos /* a given; no? */
163*bc4097aaSchristos if (sp->is_pass & FR_KEEPSTATE) {
164*bc4097aaSchristos PRINTF(" keep state");
165*bc4097aaSchristos if (sp->is_pass & (FR_STATESYNC|FR_STSTRICT|FR_STLOOSE)) {
166*bc4097aaSchristos PRINTF(" (");
167*bc4097aaSchristos if (sp->is_pass & FR_STATESYNC)
168*bc4097aaSchristos PRINTF(" sync");
169*bc4097aaSchristos if (sp->is_pass & FR_STSTRICT)
170*bc4097aaSchristos PRINTF(" strict");
171*bc4097aaSchristos if (sp->is_pass & FR_STLOOSE)
172*bc4097aaSchristos PRINTF(" loose");
173*bc4097aaSchristos PRINTF(" )");
174*bc4097aaSchristos }
175*bc4097aaSchristos }
176*bc4097aaSchristos PRINTF("\n");
177*bc4097aaSchristos
178*bc4097aaSchristos if ((opts & OPT_VERBOSE) != 0) {
179*bc4097aaSchristos PRINTF("\tref %d", sp->is_ref);
180*bc4097aaSchristos PRINTF(" pkt_flags & %x(%x) = %x\n",
181*bc4097aaSchristos sp->is_flags & 0xf, sp->is_flags, sp->is_flags >> 4);
182*bc4097aaSchristos PRINTF("\tpkt_options & %x = %x, %x = %x \n", sp->is_optmsk[0],
183*bc4097aaSchristos sp->is_opt[0], sp->is_optmsk[1], sp->is_opt[1]);
184*bc4097aaSchristos PRINTF("\tpkt_security & %x = %x, pkt_auth & %x = %x\n",
185*bc4097aaSchristos sp->is_secmsk, sp->is_sec, sp->is_authmsk,
186*bc4097aaSchristos sp->is_auth);
187*bc4097aaSchristos PRINTF("\tis_flx %#x %#x %#x %#x\n", sp->is_flx[0][0],
188*bc4097aaSchristos sp->is_flx[0][1], sp->is_flx[1][0], sp->is_flx[1][1]);
189*bc4097aaSchristos }
190*bc4097aaSchristos PRINTF("\tinterfaces: in %s[%s", getifname(sp->is_ifp[0]),
191*bc4097aaSchristos sp->is_ifname[0]);
192*bc4097aaSchristos if (opts & OPT_DEBUG)
193*bc4097aaSchristos PRINTF("/%p", sp->is_ifp[0]);
194*bc4097aaSchristos putchar(']');
195*bc4097aaSchristos PRINTF(",%s[%s", getifname(sp->is_ifp[1]), sp->is_ifname[1]);
196*bc4097aaSchristos if (opts & OPT_DEBUG)
197*bc4097aaSchristos PRINTF("/%p", sp->is_ifp[1]);
198*bc4097aaSchristos putchar(']');
199*bc4097aaSchristos PRINTF(" out %s[%s", getifname(sp->is_ifp[2]), sp->is_ifname[2]);
200*bc4097aaSchristos if (opts & OPT_DEBUG)
201*bc4097aaSchristos PRINTF("/%p", sp->is_ifp[2]);
202*bc4097aaSchristos putchar(']');
203*bc4097aaSchristos PRINTF(",%s[%s", getifname(sp->is_ifp[3]), sp->is_ifname[3]);
204*bc4097aaSchristos if (opts & OPT_DEBUG)
205*bc4097aaSchristos PRINTF("/%p", sp->is_ifp[3]);
206*bc4097aaSchristos PRINTF("]\n");
207*bc4097aaSchristos
208*bc4097aaSchristos PRINTF("\tSync status: ");
209*bc4097aaSchristos if (sp->is_sync != NULL) {
210*bc4097aaSchristos if (kmemcpy((char *)&ipsync, (u_long)sp->is_sync,
211*bc4097aaSchristos sizeof(ipsync))) {
212*bc4097aaSchristos PRINTF("status could not be retrieved\n");
213*bc4097aaSchristos return NULL;
214*bc4097aaSchristos }
215*bc4097aaSchristos
216*bc4097aaSchristos PRINTF("idx %d num %d v %d pr %d rev %d\n",
217*bc4097aaSchristos ipsync.sl_idx, ipsync.sl_num, ipsync.sl_v,
218*bc4097aaSchristos ipsync.sl_p, ipsync.sl_rev);
219*bc4097aaSchristos } else {
220*bc4097aaSchristos PRINTF("not synchronized\n");
221*bc4097aaSchristos }
222*bc4097aaSchristos
223*bc4097aaSchristos return sp->is_next;
224*bc4097aaSchristos }
225