1 | /*
|
---|
2 | * Copyright (c) 1995 Danny Gasparovski.
|
---|
3 | * Portions copyright (c) 2000 Kelly Price.
|
---|
4 | *
|
---|
5 | * Please read the file COPYRIGHT for the
|
---|
6 | * terms and conditions of the copyright.
|
---|
7 | */
|
---|
8 |
|
---|
9 | #include <slirp.h>
|
---|
10 | #include <iprt/string.h>
|
---|
11 | #include <iprt/stream.h>
|
---|
12 |
|
---|
13 | #ifdef DEBUG
|
---|
14 | void dump_packet(void *, int);
|
---|
15 | #endif
|
---|
16 |
|
---|
17 | /*
|
---|
18 | * Dump a packet in the same format as tcpdump -x
|
---|
19 | */
|
---|
20 | #ifdef DEBUG
|
---|
21 | void
|
---|
22 | dump_packet(void *dat, int n)
|
---|
23 | {
|
---|
24 | Log(("nat: PACKET DUMPED:\n%.*Vhxd\n", n, dat));
|
---|
25 | }
|
---|
26 | #endif
|
---|
27 |
|
---|
28 | #ifdef LOG_ENABLED
|
---|
29 | static void
|
---|
30 | lprint(const char *pszFormat, ...)
|
---|
31 | {
|
---|
32 | va_list args;
|
---|
33 | va_start(args, pszFormat);
|
---|
34 | RTLogPrintfV(pszFormat, args);
|
---|
35 | va_end(args);
|
---|
36 | }
|
---|
37 |
|
---|
38 | void
|
---|
39 | ipstats(PNATState pData)
|
---|
40 | {
|
---|
41 | lprint(" \n");
|
---|
42 |
|
---|
43 | lprint("IP stats:\n");
|
---|
44 | lprint(" %6d total packets received (%d were unaligned)\n",
|
---|
45 | ipstat.ips_total, ipstat.ips_unaligned);
|
---|
46 | lprint(" %6d with incorrect version\n", ipstat.ips_badvers);
|
---|
47 | lprint(" %6d with bad header checksum\n", ipstat.ips_badsum);
|
---|
48 | lprint(" %6d with length too short (len < sizeof(iphdr))\n", ipstat.ips_tooshort);
|
---|
49 | lprint(" %6d with length too small (len < ip->len)\n", ipstat.ips_toosmall);
|
---|
50 | lprint(" %6d with bad header length\n", ipstat.ips_badhlen);
|
---|
51 | lprint(" %6d with bad packet length\n", ipstat.ips_badlen);
|
---|
52 | lprint(" %6d fragments received\n", ipstat.ips_fragments);
|
---|
53 | lprint(" %6d fragments dropped\n", ipstat.ips_fragdropped);
|
---|
54 | lprint(" %6d fragments timed out\n", ipstat.ips_fragtimeout);
|
---|
55 | lprint(" %6d packets reassembled ok\n", ipstat.ips_reassembled);
|
---|
56 | lprint(" %6d outgoing packets fragmented\n", ipstat.ips_fragmented);
|
---|
57 | lprint(" %6d total outgoing fragments\n", ipstat.ips_ofragments);
|
---|
58 | lprint(" %6d with bad protocol field\n", ipstat.ips_noproto);
|
---|
59 | lprint(" %6d total packets delivered\n", ipstat.ips_delivered);
|
---|
60 | }
|
---|
61 |
|
---|
62 | void
|
---|
63 | tcpstats(PNATState pData)
|
---|
64 | {
|
---|
65 | lprint(" \n");
|
---|
66 |
|
---|
67 | lprint("TCP stats:\n");
|
---|
68 |
|
---|
69 | lprint(" %6d packets sent\n", tcpstat.tcps_sndtotal);
|
---|
70 | lprint(" %6d data packets (%d bytes)\n",
|
---|
71 | tcpstat.tcps_sndpack, tcpstat.tcps_sndbyte);
|
---|
72 | lprint(" %6d data packets retransmitted (%d bytes)\n",
|
---|
73 | tcpstat.tcps_sndrexmitpack, tcpstat.tcps_sndrexmitbyte);
|
---|
74 | lprint(" %6d ack-only packets (%d delayed)\n",
|
---|
75 | tcpstat.tcps_sndacks, tcpstat.tcps_delack);
|
---|
76 | lprint(" %6d URG only packets\n", tcpstat.tcps_sndurg);
|
---|
77 | lprint(" %6d window probe packets\n", tcpstat.tcps_sndprobe);
|
---|
78 | lprint(" %6d window update packets\n", tcpstat.tcps_sndwinup);
|
---|
79 | lprint(" %6d control (SYN/FIN/RST) packets\n", tcpstat.tcps_sndctrl);
|
---|
80 | lprint(" %6d times tcp_output did nothing\n", tcpstat.tcps_didnuttin);
|
---|
81 |
|
---|
82 | lprint(" %6d packets received\n", tcpstat.tcps_rcvtotal);
|
---|
83 | lprint(" %6d acks (for %d bytes)\n",
|
---|
84 | tcpstat.tcps_rcvackpack, tcpstat.tcps_rcvackbyte);
|
---|
85 | lprint(" %6d duplicate acks\n", tcpstat.tcps_rcvdupack);
|
---|
86 | lprint(" %6d acks for unsent data\n", tcpstat.tcps_rcvacktoomuch);
|
---|
87 | lprint(" %6d packets received in sequence (%d bytes)\n",
|
---|
88 | tcpstat.tcps_rcvpack, tcpstat.tcps_rcvbyte);
|
---|
89 | lprint(" %6d completely duplicate packets (%d bytes)\n",
|
---|
90 | tcpstat.tcps_rcvduppack, tcpstat.tcps_rcvdupbyte);
|
---|
91 |
|
---|
92 | lprint(" %6d packets with some duplicate data (%d bytes duped)\n",
|
---|
93 | tcpstat.tcps_rcvpartduppack, tcpstat.tcps_rcvpartdupbyte);
|
---|
94 | lprint(" %6d out-of-order packets (%d bytes)\n",
|
---|
95 | tcpstat.tcps_rcvoopack, tcpstat.tcps_rcvoobyte);
|
---|
96 | lprint(" %6d packets of data after window (%d bytes)\n",
|
---|
97 | tcpstat.tcps_rcvpackafterwin, tcpstat.tcps_rcvbyteafterwin);
|
---|
98 | lprint(" %6d window probes\n", tcpstat.tcps_rcvwinprobe);
|
---|
99 | lprint(" %6d window update packets\n", tcpstat.tcps_rcvwinupd);
|
---|
100 | lprint(" %6d packets received after close\n", tcpstat.tcps_rcvafterclose);
|
---|
101 | lprint(" %6d discarded for bad checksums\n", tcpstat.tcps_rcvbadsum);
|
---|
102 | lprint(" %6d discarded for bad header offset fields\n",
|
---|
103 | tcpstat.tcps_rcvbadoff);
|
---|
104 |
|
---|
105 | lprint(" %6d connection requests\n", tcpstat.tcps_connattempt);
|
---|
106 | lprint(" %6d connection accepts\n", tcpstat.tcps_accepts);
|
---|
107 | lprint(" %6d connections established (including accepts)\n", tcpstat.tcps_connects);
|
---|
108 | lprint(" %6d connections closed (including %d drop)\n",
|
---|
109 | tcpstat.tcps_closed, tcpstat.tcps_drops);
|
---|
110 | lprint(" %6d embryonic connections dropped\n", tcpstat.tcps_conndrops);
|
---|
111 | lprint(" %6d segments we tried to get rtt (%d succeeded)\n",
|
---|
112 | tcpstat.tcps_segstimed, tcpstat.tcps_rttupdated);
|
---|
113 | lprint(" %6d retransmit timeouts\n", tcpstat.tcps_rexmttimeo);
|
---|
114 | lprint(" %6d connections dropped by rxmt timeout\n",
|
---|
115 | tcpstat.tcps_timeoutdrop);
|
---|
116 | lprint(" %6d persist timeouts\n", tcpstat.tcps_persisttimeo);
|
---|
117 | lprint(" %6d keepalive timeouts\n", tcpstat.tcps_keeptimeo);
|
---|
118 | lprint(" %6d keepalive probes sent\n", tcpstat.tcps_keepprobe);
|
---|
119 | lprint(" %6d connections dropped by keepalive\n", tcpstat.tcps_keepdrops);
|
---|
120 | lprint(" %6d correct ACK header predictions\n", tcpstat.tcps_predack);
|
---|
121 | lprint(" %6d correct data packet header predictions\n", tcpstat.tcps_preddat);
|
---|
122 | lprint(" %6d TCP cache misses\n", tcpstat.tcps_socachemiss);
|
---|
123 |
|
---|
124 | /* lprint(" Packets received too short: %d\n", tcpstat.tcps_rcvshort); */
|
---|
125 | /* lprint(" Segments dropped due to PAWS: %d\n", tcpstat.tcps_pawsdrop); */
|
---|
126 |
|
---|
127 | }
|
---|
128 |
|
---|
129 | void
|
---|
130 | udpstats(PNATState pData)
|
---|
131 | {
|
---|
132 | lprint(" \n");
|
---|
133 |
|
---|
134 | lprint("UDP stats:\n");
|
---|
135 | lprint(" %6d datagrams received\n", udpstat.udps_ipackets);
|
---|
136 | lprint(" %6d with packets shorter than header\n", udpstat.udps_hdrops);
|
---|
137 | lprint(" %6d with bad checksums\n", udpstat.udps_badsum);
|
---|
138 | lprint(" %6d with data length larger than packet\n", udpstat.udps_badlen);
|
---|
139 | lprint(" %6d UDP socket cache misses\n", udpstat.udpps_pcbcachemiss);
|
---|
140 | lprint(" %6d datagrams sent\n", udpstat.udps_opackets);
|
---|
141 | }
|
---|
142 |
|
---|
143 | void
|
---|
144 | icmpstats(PNATState pData)
|
---|
145 | {
|
---|
146 | lprint(" \n");
|
---|
147 | lprint("ICMP stats:\n");
|
---|
148 | lprint(" %6d ICMP packets received\n", icmpstat.icps_received);
|
---|
149 | lprint(" %6d were too short\n", icmpstat.icps_tooshort);
|
---|
150 | lprint(" %6d with bad checksums\n", icmpstat.icps_checksum);
|
---|
151 | lprint(" %6d with type not supported\n", icmpstat.icps_notsupp);
|
---|
152 | lprint(" %6d with bad type feilds\n", icmpstat.icps_badtype);
|
---|
153 | lprint(" %6d ICMP packets sent in reply\n", icmpstat.icps_reflect);
|
---|
154 | }
|
---|
155 |
|
---|
156 | void
|
---|
157 | mbufstats(PNATState pData)
|
---|
158 | {
|
---|
159 | struct mbuf *m;
|
---|
160 | int i;
|
---|
161 |
|
---|
162 | lprint(" \n");
|
---|
163 |
|
---|
164 | lprint("Mbuf stats:\n");
|
---|
165 |
|
---|
166 | lprint(" %6d mbufs allocated (%d max)\n", mbuf_alloced, mbuf_max);
|
---|
167 |
|
---|
168 | i = 0;
|
---|
169 | for (m = m_freelist.m_next; m != &m_freelist; m = m->m_next)
|
---|
170 | i++;
|
---|
171 | lprint(" %6d mbufs on free list\n", i);
|
---|
172 |
|
---|
173 | i = 0;
|
---|
174 | for (m = m_usedlist.m_next; m != &m_usedlist; m = m->m_next)
|
---|
175 | i++;
|
---|
176 | lprint(" %6d mbufs on used list\n", i);
|
---|
177 | lprint(" %6d mbufs queued as packets\n\n", if_queued);
|
---|
178 | }
|
---|
179 |
|
---|
180 | void
|
---|
181 | sockstats(PNATState pData)
|
---|
182 | {
|
---|
183 | char buff[256];
|
---|
184 | int n;
|
---|
185 | struct socket *so;
|
---|
186 |
|
---|
187 | lprint(" \n");
|
---|
188 |
|
---|
189 | lprint(
|
---|
190 | "Proto[state] Sock Local Address, Port Remote Address, Port RecvQ SendQ\n");
|
---|
191 |
|
---|
192 | for (so = tcb.so_next; so != &tcb; so = so->so_next)
|
---|
193 | {
|
---|
194 | n = sprintf(buff, "tcp[%s]", so->so_tcpcb?tcpstates[so->so_tcpcb->t_state]:"NONE");
|
---|
195 | while (n < 17)
|
---|
196 | buff[n++] = ' ';
|
---|
197 | buff[17] = 0;
|
---|
198 | lprint("%s %3d %15s %5d ",
|
---|
199 | buff, so->s, inet_ntoa(so->so_laddr), ntohs(so->so_lport));
|
---|
200 | lprint("%15s %5d %5d %5d\n",
|
---|
201 | inet_ntoa(so->so_faddr), ntohs(so->so_fport),
|
---|
202 | so->so_rcv.sb_cc, so->so_snd.sb_cc);
|
---|
203 | }
|
---|
204 |
|
---|
205 | for (so = udb.so_next; so != &udb; so = so->so_next)
|
---|
206 | {
|
---|
207 | n = sprintf(buff, "udp[%d sec]", (so->so_expire - curtime) / 1000);
|
---|
208 | while (n < 17)
|
---|
209 | buff[n++] = ' ';
|
---|
210 | buff[17] = 0;
|
---|
211 | lprint("%s %3d %15s %5d ",
|
---|
212 | buff, so->s, inet_ntoa(so->so_laddr), ntohs(so->so_lport));
|
---|
213 | lprint("%15s %5d %5d %5d\n",
|
---|
214 | inet_ntoa(so->so_faddr), ntohs(so->so_fport),
|
---|
215 | so->so_rcv.sb_cc, so->so_snd.sb_cc);
|
---|
216 | }
|
---|
217 | }
|
---|
218 | #endif
|
---|
219 |
|
---|
220 | static size_t
|
---|
221 | print_ipv4_address(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
|
---|
222 | const char *pszType, void const *pvValue,
|
---|
223 | int cchWidth, int cchPrecision, unsigned fFlags,
|
---|
224 | void *pvUser)
|
---|
225 | {
|
---|
226 | uint32_t ip;
|
---|
227 |
|
---|
228 | AssertReturn(strcmp(pszType, "IP4") == 0, 0);
|
---|
229 |
|
---|
230 | ip = ntohl(*(uint32_t*)pvValue);
|
---|
231 | return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%u.%u.%u.%u",
|
---|
232 | (ip >> 24), (ip >> 16) & 0xff, (ip >> 8) & 0xff, ip & 0xff);
|
---|
233 | }
|
---|
234 |
|
---|
235 | int
|
---|
236 | debug_init()
|
---|
237 | {
|
---|
238 | int rc = VINF_SUCCESS;
|
---|
239 |
|
---|
240 | rc = RTStrFormatTypeRegister("IP4", print_ipv4_address, NULL);
|
---|
241 | AssertRC(rc);
|
---|
242 |
|
---|
243 | return rc;
|
---|
244 | }
|
---|