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 | #define IP4_ADDR_PRINTF_DECOMP(ip) ((ip) >> 24), ((ip) >> 16) & 0xff, ((ip) >> 8) & 0xff, (ip) & 0xff
|
---|
18 | #define IP4_ADDR_PRINTF_FORMAT "%u.%u.%u.%u"
|
---|
19 | /*
|
---|
20 | * Dump a packet in the same format as tcpdump -x
|
---|
21 | */
|
---|
22 | #ifdef DEBUG
|
---|
23 | void
|
---|
24 | dump_packet(void *dat, int n)
|
---|
25 | {
|
---|
26 | Log(("nat: PACKET DUMPED:\n%.*Rhxd\n", n, dat));
|
---|
27 | }
|
---|
28 | #endif
|
---|
29 |
|
---|
30 | #ifdef LOG_ENABLED
|
---|
31 | static void
|
---|
32 | lprint(const char *pszFormat, ...)
|
---|
33 | {
|
---|
34 | va_list args;
|
---|
35 | va_start(args, pszFormat);
|
---|
36 | RTLogPrintfV(pszFormat, args);
|
---|
37 | va_end(args);
|
---|
38 | }
|
---|
39 |
|
---|
40 | void
|
---|
41 | ipstats(PNATState pData)
|
---|
42 | {
|
---|
43 | lprint(" \n");
|
---|
44 |
|
---|
45 | lprint("IP stats:\n");
|
---|
46 | lprint(" %6d total packets received (%d were unaligned)\n",
|
---|
47 | ipstat.ips_total, ipstat.ips_unaligned);
|
---|
48 | lprint(" %6d with incorrect version\n", ipstat.ips_badvers);
|
---|
49 | lprint(" %6d with bad header checksum\n", ipstat.ips_badsum);
|
---|
50 | lprint(" %6d with length too short (len < sizeof(iphdr))\n", ipstat.ips_tooshort);
|
---|
51 | lprint(" %6d with length too small (len < ip->len)\n", ipstat.ips_toosmall);
|
---|
52 | lprint(" %6d with bad header length\n", ipstat.ips_badhlen);
|
---|
53 | lprint(" %6d with bad packet length\n", ipstat.ips_badlen);
|
---|
54 | lprint(" %6d fragments received\n", ipstat.ips_fragments);
|
---|
55 | lprint(" %6d fragments dropped\n", ipstat.ips_fragdropped);
|
---|
56 | lprint(" %6d fragments timed out\n", ipstat.ips_fragtimeout);
|
---|
57 | lprint(" %6d packets reassembled ok\n", ipstat.ips_reassembled);
|
---|
58 | lprint(" %6d outgoing packets fragmented\n", ipstat.ips_fragmented);
|
---|
59 | lprint(" %6d total outgoing fragments\n", ipstat.ips_ofragments);
|
---|
60 | lprint(" %6d with bad protocol field\n", ipstat.ips_noproto);
|
---|
61 | lprint(" %6d total packets delivered\n", ipstat.ips_delivered);
|
---|
62 | }
|
---|
63 |
|
---|
64 | void
|
---|
65 | tcpstats(PNATState pData)
|
---|
66 | {
|
---|
67 | lprint(" \n");
|
---|
68 |
|
---|
69 | lprint("TCP stats:\n");
|
---|
70 |
|
---|
71 | lprint(" %6d packets sent\n", tcpstat.tcps_sndtotal);
|
---|
72 | lprint(" %6d data packets (%d bytes)\n",
|
---|
73 | tcpstat.tcps_sndpack, tcpstat.tcps_sndbyte);
|
---|
74 | lprint(" %6d data packets retransmitted (%d bytes)\n",
|
---|
75 | tcpstat.tcps_sndrexmitpack, tcpstat.tcps_sndrexmitbyte);
|
---|
76 | lprint(" %6d ack-only packets (%d delayed)\n",
|
---|
77 | tcpstat.tcps_sndacks, tcpstat.tcps_delack);
|
---|
78 | lprint(" %6d URG only packets\n", tcpstat.tcps_sndurg);
|
---|
79 | lprint(" %6d window probe packets\n", tcpstat.tcps_sndprobe);
|
---|
80 | lprint(" %6d window update packets\n", tcpstat.tcps_sndwinup);
|
---|
81 | lprint(" %6d control (SYN/FIN/RST) packets\n", tcpstat.tcps_sndctrl);
|
---|
82 | lprint(" %6d times tcp_output did nothing\n", tcpstat.tcps_didnuttin);
|
---|
83 |
|
---|
84 | lprint(" %6d packets received\n", tcpstat.tcps_rcvtotal);
|
---|
85 | lprint(" %6d acks (for %d bytes)\n",
|
---|
86 | tcpstat.tcps_rcvackpack, tcpstat.tcps_rcvackbyte);
|
---|
87 | lprint(" %6d duplicate acks\n", tcpstat.tcps_rcvdupack);
|
---|
88 | lprint(" %6d acks for unsent data\n", tcpstat.tcps_rcvacktoomuch);
|
---|
89 | lprint(" %6d packets received in sequence (%d bytes)\n",
|
---|
90 | tcpstat.tcps_rcvpack, tcpstat.tcps_rcvbyte);
|
---|
91 | lprint(" %6d completely duplicate packets (%d bytes)\n",
|
---|
92 | tcpstat.tcps_rcvduppack, tcpstat.tcps_rcvdupbyte);
|
---|
93 |
|
---|
94 | lprint(" %6d packets with some duplicate data (%d bytes duped)\n",
|
---|
95 | tcpstat.tcps_rcvpartduppack, tcpstat.tcps_rcvpartdupbyte);
|
---|
96 | lprint(" %6d out-of-order packets (%d bytes)\n",
|
---|
97 | tcpstat.tcps_rcvoopack, tcpstat.tcps_rcvoobyte);
|
---|
98 | lprint(" %6d packets of data after window (%d bytes)\n",
|
---|
99 | tcpstat.tcps_rcvpackafterwin, tcpstat.tcps_rcvbyteafterwin);
|
---|
100 | lprint(" %6d window probes\n", tcpstat.tcps_rcvwinprobe);
|
---|
101 | lprint(" %6d window update packets\n", tcpstat.tcps_rcvwinupd);
|
---|
102 | lprint(" %6d packets received after close\n", tcpstat.tcps_rcvafterclose);
|
---|
103 | lprint(" %6d discarded for bad checksums\n", tcpstat.tcps_rcvbadsum);
|
---|
104 | lprint(" %6d discarded for bad header offset fields\n",
|
---|
105 | tcpstat.tcps_rcvbadoff);
|
---|
106 |
|
---|
107 | lprint(" %6d connection requests\n", tcpstat.tcps_connattempt);
|
---|
108 | lprint(" %6d connection accepts\n", tcpstat.tcps_accepts);
|
---|
109 | lprint(" %6d connections established (including accepts)\n", tcpstat.tcps_connects);
|
---|
110 | lprint(" %6d connections closed (including %d drop)\n",
|
---|
111 | tcpstat.tcps_closed, tcpstat.tcps_drops);
|
---|
112 | lprint(" %6d embryonic connections dropped\n", tcpstat.tcps_conndrops);
|
---|
113 | lprint(" %6d segments we tried to get rtt (%d succeeded)\n",
|
---|
114 | tcpstat.tcps_segstimed, tcpstat.tcps_rttupdated);
|
---|
115 | lprint(" %6d retransmit timeouts\n", tcpstat.tcps_rexmttimeo);
|
---|
116 | lprint(" %6d connections dropped by rxmt timeout\n",
|
---|
117 | tcpstat.tcps_timeoutdrop);
|
---|
118 | lprint(" %6d persist timeouts\n", tcpstat.tcps_persisttimeo);
|
---|
119 | lprint(" %6d keepalive timeouts\n", tcpstat.tcps_keeptimeo);
|
---|
120 | lprint(" %6d keepalive probes sent\n", tcpstat.tcps_keepprobe);
|
---|
121 | lprint(" %6d connections dropped by keepalive\n", tcpstat.tcps_keepdrops);
|
---|
122 | lprint(" %6d correct ACK header predictions\n", tcpstat.tcps_predack);
|
---|
123 | lprint(" %6d correct data packet header predictions\n", tcpstat.tcps_preddat);
|
---|
124 | lprint(" %6d TCP cache misses\n", tcpstat.tcps_socachemiss);
|
---|
125 |
|
---|
126 | /* lprint(" Packets received too short: %d\n", tcpstat.tcps_rcvshort); */
|
---|
127 | /* lprint(" Segments dropped due to PAWS: %d\n", tcpstat.tcps_pawsdrop); */
|
---|
128 |
|
---|
129 | }
|
---|
130 |
|
---|
131 | void
|
---|
132 | udpstats(PNATState pData)
|
---|
133 | {
|
---|
134 | lprint(" \n");
|
---|
135 |
|
---|
136 | lprint("UDP stats:\n");
|
---|
137 | lprint(" %6d datagrams received\n", udpstat.udps_ipackets);
|
---|
138 | lprint(" %6d with packets shorter than header\n", udpstat.udps_hdrops);
|
---|
139 | lprint(" %6d with bad checksums\n", udpstat.udps_badsum);
|
---|
140 | lprint(" %6d with data length larger than packet\n", udpstat.udps_badlen);
|
---|
141 | lprint(" %6d UDP socket cache misses\n", udpstat.udpps_pcbcachemiss);
|
---|
142 | lprint(" %6d datagrams sent\n", udpstat.udps_opackets);
|
---|
143 | }
|
---|
144 |
|
---|
145 | void
|
---|
146 | icmpstats(PNATState pData)
|
---|
147 | {
|
---|
148 | lprint(" \n");
|
---|
149 | lprint("ICMP stats:\n");
|
---|
150 | lprint(" %6d ICMP packets received\n", icmpstat.icps_received);
|
---|
151 | lprint(" %6d were too short\n", icmpstat.icps_tooshort);
|
---|
152 | lprint(" %6d with bad checksums\n", icmpstat.icps_checksum);
|
---|
153 | lprint(" %6d with type not supported\n", icmpstat.icps_notsupp);
|
---|
154 | lprint(" %6d with bad type feilds\n", icmpstat.icps_badtype);
|
---|
155 | lprint(" %6d ICMP packets sent in reply\n", icmpstat.icps_reflect);
|
---|
156 | }
|
---|
157 |
|
---|
158 | void
|
---|
159 | mbufstats(PNATState pData)
|
---|
160 | {
|
---|
161 | #ifndef VBOX_WITH_SLIRP_BSD_MBUF
|
---|
162 | /*
|
---|
163 | * (vvl) this static code can't work with mbuf zone anymore
|
---|
164 | * @todo: make statistic correct
|
---|
165 | */
|
---|
166 | #if 0
|
---|
167 | struct mbuf *m;
|
---|
168 | int i;
|
---|
169 |
|
---|
170 | lprint(" \n");
|
---|
171 |
|
---|
172 | lprint("Mbuf stats:\n");
|
---|
173 |
|
---|
174 | lprint(" %6d mbufs allocated (%d max)\n", mbuf_alloced, mbuf_max);
|
---|
175 |
|
---|
176 | i = 0;
|
---|
177 | for (m = m_freelist.m_next; m != &m_freelist; m = m->m_next)
|
---|
178 | i++;
|
---|
179 | lprint(" %6d mbufs on free list\n", i);
|
---|
180 |
|
---|
181 | i = 0;
|
---|
182 | for (m = m_usedlist.m_next; m != &m_usedlist; m = m->m_next)
|
---|
183 | i++;
|
---|
184 | lprint(" %6d mbufs on used list\n", i);
|
---|
185 | lprint(" %6d mbufs queued as packets\n\n", if_queued);
|
---|
186 | #endif
|
---|
187 | #endif
|
---|
188 | }
|
---|
189 |
|
---|
190 | void
|
---|
191 | sockstats(PNATState pData)
|
---|
192 | {
|
---|
193 | char buff[256];
|
---|
194 | int n;
|
---|
195 | struct socket *so, *so_next;
|
---|
196 |
|
---|
197 | lprint(" \n");
|
---|
198 |
|
---|
199 | lprint(
|
---|
200 | "Proto[state] Sock Local Address, Port Remote Address, Port RecvQ SendQ\n");
|
---|
201 |
|
---|
202 | QSOCKET_FOREACH(so, so_next, tcp)
|
---|
203 | /* { */
|
---|
204 | n = RTStrPrintf(buff, sizeof(buff), "tcp[%s]", so->so_tcpcb?tcpstates[so->so_tcpcb->t_state]:"NONE");
|
---|
205 | while (n < 17)
|
---|
206 | buff[n++] = ' ';
|
---|
207 | buff[17] = 0;
|
---|
208 | lprint("%s %3d %15s %5d ",
|
---|
209 | buff, so->s, inet_ntoa(so->so_laddr), RT_N2H_U16(so->so_lport));
|
---|
210 | lprint("%15s %5d %5d %5d\n",
|
---|
211 | inet_ntoa(so->so_faddr), RT_N2H_U16(so->so_fport),
|
---|
212 | so->so_rcv.sb_cc, so->so_snd.sb_cc);
|
---|
213 | LOOP_LABEL(tcp, so, so_next);
|
---|
214 | }
|
---|
215 |
|
---|
216 | QSOCKET_FOREACH(so, so_next, udp)
|
---|
217 | /* { */
|
---|
218 | n = RTStrPrintf(buff, sizeof(buff), "udp[%d sec]", (so->so_expire - curtime) / 1000);
|
---|
219 | while (n < 17)
|
---|
220 | buff[n++] = ' ';
|
---|
221 | buff[17] = 0;
|
---|
222 | lprint("%s %3d %15s %5d ",
|
---|
223 | buff, so->s, inet_ntoa(so->so_laddr), RT_N2H_U16(so->so_lport));
|
---|
224 | lprint("%15s %5d %5d %5d\n",
|
---|
225 | inet_ntoa(so->so_faddr), RT_N2H_U16(so->so_fport),
|
---|
226 | so->so_rcv.sb_cc, so->so_snd.sb_cc);
|
---|
227 | LOOP_LABEL(udp, so, so_next);
|
---|
228 | }
|
---|
229 | }
|
---|
230 | #endif
|
---|
231 |
|
---|
232 | static DECLCALLBACK(size_t)
|
---|
233 | print_ipv4_address(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
|
---|
234 | const char *pszType, void const *pvValue,
|
---|
235 | int cchWidth, int cchPrecision, unsigned fFlags,
|
---|
236 | void *pvUser)
|
---|
237 | {
|
---|
238 | uint32_t ip;
|
---|
239 |
|
---|
240 | AssertReturn(strcmp(pszType, "IP4") == 0, 0);
|
---|
241 |
|
---|
242 | ip = RT_N2H_U32(*(uint32_t*)pvValue);
|
---|
243 | return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, IP4_ADDR_PRINTF_FORMAT,
|
---|
244 | IP4_ADDR_PRINTF_DECOMP(ip));
|
---|
245 | }
|
---|
246 |
|
---|
247 | static DECLCALLBACK(size_t)
|
---|
248 | print_ether_address(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
|
---|
249 | const char *pszType, void const *pvValue,
|
---|
250 | int cchWidth, int cchPrecision, unsigned fFlags,
|
---|
251 | void *pvUser)
|
---|
252 | {
|
---|
253 | char *ether = (char *)pvUser;
|
---|
254 |
|
---|
255 | AssertReturn(strcmp(pszType, "ether") == 0, 0);
|
---|
256 | if (ether != NULL)
|
---|
257 | return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
|
---|
258 | "[ether %hhx:%hhx:%hhx:%hhx:%hhx:%hhx]",
|
---|
259 | ether[0], ether[1], ether[2],
|
---|
260 | ether[3], ether[4], ether[5]);
|
---|
261 | else
|
---|
262 | return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "[ether null]");
|
---|
263 | }
|
---|
264 |
|
---|
265 | static DECLCALLBACK(size_t)
|
---|
266 | print_socket(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
|
---|
267 | const char *pszType, void const *pvValue,
|
---|
268 | int cchWidth, int cchPrecision, unsigned fFlags,
|
---|
269 | void *pvUser)
|
---|
270 | {
|
---|
271 | struct socket *so = (struct socket*)pvValue;
|
---|
272 | uint32_t ip;
|
---|
273 | struct sockaddr addr;
|
---|
274 | struct sockaddr_in *in_addr;
|
---|
275 | socklen_t socklen = sizeof(struct sockaddr);
|
---|
276 | int status = 0;
|
---|
277 |
|
---|
278 | AssertReturn(strcmp(pszType, "natsock") == 0, 0);
|
---|
279 | if (so == NULL)
|
---|
280 | return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
|
---|
281 | "socket is null");
|
---|
282 | if (so->so_state == SS_NOFDREF || so->s == -1)
|
---|
283 | return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
|
---|
284 | "socket(%d) SS_NODREF",so->s);
|
---|
285 | status = getsockname(so->s, &addr, &socklen);
|
---|
286 |
|
---|
287 | if(status != 0 || addr.sa_family != AF_INET)
|
---|
288 | {
|
---|
289 | return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
|
---|
290 | "socket(%d) is invalid(probably closed)",so->s);
|
---|
291 | }
|
---|
292 |
|
---|
293 | in_addr = (struct sockaddr_in *)&addr;
|
---|
294 | ip = RT_N2H_U32(so->so_faddr.s_addr);
|
---|
295 | return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "socket %4d:(proto:%u) "
|
---|
296 | "state=%04x ip=" IP4_ADDR_PRINTF_FORMAT ":%d "
|
---|
297 | "name=" IP4_ADDR_PRINTF_FORMAT ":%d",
|
---|
298 | so->s, so->so_type, so->so_state, IP4_ADDR_PRINTF_DECOMP(ip),
|
---|
299 | RT_N2H_U16(so->so_fport),
|
---|
300 | IP4_ADDR_PRINTF_DECOMP(RT_N2H_U32(in_addr->sin_addr.s_addr)),
|
---|
301 | RT_N2H_U16(in_addr->sin_port));
|
---|
302 | }
|
---|
303 |
|
---|
304 | static DECLCALLBACK(size_t)
|
---|
305 | print_networkevents(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
|
---|
306 | const char *pszType, void const *pvValue,
|
---|
307 | int cchWidth, int cchPrecision, unsigned fFlags,
|
---|
308 | void *pvUser)
|
---|
309 | {
|
---|
310 | size_t cb = 0;
|
---|
311 | #ifdef RT_OS_WINDOWS
|
---|
312 | WSANETWORKEVENTS *pNetworkEvents = (WSANETWORKEVENTS*)pvValue;
|
---|
313 | bool fDelim = false;
|
---|
314 |
|
---|
315 | AssertReturn(strcmp(pszType, "natwinnetevents") == 0, 0);
|
---|
316 |
|
---|
317 | cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "events=%02x (",
|
---|
318 | pNetworkEvents->lNetworkEvents);
|
---|
319 | # define DO_BIT(bit) \
|
---|
320 | if (pNetworkEvents->lNetworkEvents & FD_ ## bit) \
|
---|
321 | { \
|
---|
322 | cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, \
|
---|
323 | "%s" #bit "(%d)", fDelim ? "," : "", \
|
---|
324 | pNetworkEvents->iErrorCode[FD_ ## bit ## _BIT]); \
|
---|
325 | fDelim = true; \
|
---|
326 | }
|
---|
327 | DO_BIT(READ);
|
---|
328 | DO_BIT(WRITE);
|
---|
329 | DO_BIT(OOB);
|
---|
330 | DO_BIT(ACCEPT);
|
---|
331 | DO_BIT(CONNECT);
|
---|
332 | DO_BIT(CLOSE);
|
---|
333 | DO_BIT(QOS);
|
---|
334 | # undef DO_BIT
|
---|
335 | cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, ")");
|
---|
336 | #endif
|
---|
337 | return cb;
|
---|
338 | }
|
---|
339 |
|
---|
340 | #if 0
|
---|
341 | /*
|
---|
342 | * Debugging
|
---|
343 | */
|
---|
344 | int errno_func(const char *file, int line)
|
---|
345 | {
|
---|
346 | int err = WSAGetLastError();
|
---|
347 | LogRel(("errno=%d (%s:%d)\n", err, file, line));
|
---|
348 | return err;
|
---|
349 | }
|
---|
350 | #endif
|
---|
351 |
|
---|
352 | int
|
---|
353 | debug_init()
|
---|
354 | {
|
---|
355 | int rc = VINF_SUCCESS;
|
---|
356 |
|
---|
357 | static int g_fFormatRegistered;
|
---|
358 |
|
---|
359 | if (!g_fFormatRegistered)
|
---|
360 | {
|
---|
361 | /*
|
---|
362 | * XXX(r - frank): Move this to IPRT using RTNETADDRIPV4.
|
---|
363 | * Use the specifier %RNAipv4.
|
---|
364 | */
|
---|
365 | rc = RTStrFormatTypeRegister("IP4", print_ipv4_address, NULL);
|
---|
366 | AssertRC(rc);
|
---|
367 | rc = RTStrFormatTypeRegister("ether", print_ether_address, NULL);
|
---|
368 | AssertRC(rc);
|
---|
369 | rc = RTStrFormatTypeRegister("natsock", print_socket, NULL);
|
---|
370 | AssertRC(rc);
|
---|
371 | rc = RTStrFormatTypeRegister("natwinnetevents",
|
---|
372 | print_networkevents, NULL);
|
---|
373 | AssertRC(rc);
|
---|
374 | g_fFormatRegistered = 1;
|
---|
375 | }
|
---|
376 |
|
---|
377 | return rc;
|
---|
378 | }
|
---|