Move <machine/in_cksum.h> to <sys/in_cksum.h>. This file is now platform
[dragonfly.git] / contrib / ipfilter / printstate.c
1 /*
2  * Copyright (C) 2002 by Darren Reed.
3  *
4  * See the IPFILTER.LICENCE file for details on licencing.
5  */
6 #if defined(__sgi) && (IRIX > 602)
7 # include <sys/ptimers.h>
8 #endif
9 #include <sys/types.h>
10 #include <sys/param.h>
11 #include <sys/socket.h>
12 #include <sys/ioctl.h>
13 #include <netinet/in.h>
14 #include <arpa/inet.h>
15 #include <netinet/in_systm.h>
16 #include <net/if.h>
17 #include <stdio.h>
18 #if __FreeBSD_version >= 300000
19 # include <net/if_var.h>
20 #endif
21 #include "kmem.h"
22 #include "netinet/ip_compat.h"
23 #include "ipf.h"
24 #include "netinet/ip_fil.h"
25 #include "netinet/ip_state.h"
26
27 #define PRINTF  (void)printf
28 #define FPRINTF (void)fprintf
29
30 ipstate_t *printstate(sp, opts)
31 ipstate_t *sp;
32 int opts;
33 {
34         ipstate_t ips;
35
36         if (kmemcpy((char *)&ips, (u_long)sp, sizeof(ips)))
37                 return NULL;
38
39         PRINTF("%s -> ", hostname(ips.is_v, &ips.is_src.in4));
40         PRINTF("%s ttl %ld pass %#x pr %d state %d/%d\n",
41                 hostname(ips.is_v, &ips.is_dst.in4),
42                 ips.is_age, ips.is_pass, ips.is_p,
43                 ips.is_state[0], ips.is_state[1]);
44 #ifdef  USE_QUAD_T
45         PRINTF("\tpkts %qu bytes %qu", (unsigned long long) ips.is_pkts,
46                 (unsigned long long) ips.is_bytes);
47 #else
48         PRINTF("\tpkts %ld bytes %ld", ips.is_pkts, ips.is_bytes);
49 #endif
50         if (ips.is_p == IPPROTO_TCP)
51 #if defined(NetBSD) && (NetBSD >= 199905) && (NetBSD < 1991011) || \
52 (__FreeBSD_version >= 220000) || defined(__OpenBSD__)
53                 PRINTF("\t%hu -> %hu %x:%x %u<<%d:%u<<%d",
54                         ntohs(ips.is_sport), ntohs(ips.is_dport),
55                         ips.is_send, ips.is_dend,
56                         ips.is_maxswin>>ips.is_swscale, ips.is_swscale,
57                         ips.is_maxdwin>>ips.is_dwscale, ips.is_dwscale);
58 #else
59                 PRINTF("\t%hu -> %hu %x:%x %u<<%d:%u<<%d",
60                         ntohs(ips.is_sport), ntohs(ips.is_dport),
61                         ips.is_send, ips.is_dend,
62                         ips.is_maxswin>>ips.is_swscale, ips.is_swscale,
63                         ips.is_maxdwin>>ips.is_dwscale, ips.is_dwscale);
64 #endif
65         else if (ips.is_p == IPPROTO_UDP)
66                 PRINTF(" %hu -> %hu", ntohs(ips.is_sport),
67                         ntohs(ips.is_dport));
68         else if (ips.is_p == IPPROTO_ICMP
69 #ifdef  USE_INET6
70                  || ips.is_p == IPPROTO_ICMPV6
71 #endif
72                 )
73                 PRINTF(" id %hu seq %hu type %d", ntohs(ips.is_icmp.ics_id),
74                         ntohs(ips.is_icmp.ics_seq), ips.is_icmp.ics_type);
75
76         PRINTF("\n\t");
77
78         /*
79          * Print out bits set in the result code for the state being
80          * kept as they would for a rule.
81          */
82         if (ips.is_pass & FR_PASS) {
83                 PRINTF("pass");
84         } else if (ips.is_pass & FR_BLOCK) {
85                 PRINTF("block");
86                 switch (ips.is_pass & FR_RETMASK)
87                 {
88                 case FR_RETICMP :
89                         PRINTF(" return-icmp");
90                         break;
91                 case FR_FAKEICMP :
92                         PRINTF(" return-icmp-as-dest");
93                         break;
94                 case FR_RETRST :
95                         PRINTF(" return-rst");
96                         break;
97                 default :
98                         break;
99                 }
100         } else if ((ips.is_pass & FR_LOGMASK) == FR_LOG) {
101                         PRINTF("log");
102                 if (ips.is_pass & FR_LOGBODY)
103                         PRINTF(" body");
104                 if (ips.is_pass & FR_LOGFIRST)
105                         PRINTF(" first");
106         } else if (ips.is_pass & FR_ACCOUNT)
107                 PRINTF("count");
108
109         if (ips.is_pass & FR_OUTQUE)
110                 PRINTF(" out");
111         else
112                 PRINTF(" in");
113
114         if ((ips.is_pass & FR_LOG) != 0) {
115                 PRINTF(" log");
116                 if (ips.is_pass & FR_LOGBODY)
117                         PRINTF(" body");
118                 if (ips.is_pass & FR_LOGFIRST)
119                         PRINTF(" first");
120                 if (ips.is_pass & FR_LOGORBLOCK)
121                         PRINTF(" or-block");
122         }
123         if (ips.is_pass & FR_QUICK)
124                 PRINTF(" quick");
125         if (ips.is_pass & FR_KEEPFRAG)
126                 PRINTF(" keep frags");
127         /* a given; no? */
128         if (ips.is_pass & FR_KEEPSTATE)
129                 PRINTF(" keep state");
130         PRINTF("\tIPv%d", ips.is_v);
131         PRINTF("\n");
132
133         PRINTF("\tpkt_flags & %x(%x) = %x,\t",
134                 ips.is_flags & 0xf, ips.is_flags,
135                 ips.is_flags >> 4);
136         PRINTF("\tpkt_options & %x = %x\n", ips.is_optmsk,
137                 ips.is_opt);
138         PRINTF("\tpkt_security & %x = %x, pkt_auth & %x = %x\n",
139                 ips.is_secmsk, ips.is_sec, ips.is_authmsk,
140                 ips.is_auth);
141         PRINTF("\tinterfaces: in %s", getifname(ips.is_ifp[0]));
142         PRINTF(",%s", getifname(ips.is_ifp[1]));
143         PRINTF(" out %s", getifname(ips.is_ifp[2]));
144         PRINTF(",%s\n", getifname(ips.is_ifp[3]));
145
146         return ips.is_next;
147 }