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 |
|
---|
11 | #ifndef VBOX
|
---|
12 | FILE *dfd = NULL;
|
---|
13 | #ifdef DEBUG
|
---|
14 | int dostats = 1;
|
---|
15 | #else
|
---|
16 | int dostats = 0;
|
---|
17 | #endif
|
---|
18 | int slirp_debug = 0;
|
---|
19 | #endif /* !VBOX */
|
---|
20 |
|
---|
21 | #ifndef VBOX
|
---|
22 | extern char *strerror _P((int));
|
---|
23 | #endif
|
---|
24 |
|
---|
25 | /* Carry over one item from main.c so that the tty's restored.
|
---|
26 | * Only done when the tty being used is /dev/tty --RedWolf */
|
---|
27 | extern struct termios slirp_tty_settings;
|
---|
28 | extern int slirp_tty_restore;
|
---|
29 |
|
---|
30 |
|
---|
31 | #ifndef VBOX
|
---|
32 | void
|
---|
33 | debug_init(file, dbg)
|
---|
34 | char *file;
|
---|
35 | int dbg;
|
---|
36 | {
|
---|
37 | /* Close the old debugging file */
|
---|
38 | if (dfd)
|
---|
39 | fclose(dfd);
|
---|
40 |
|
---|
41 | dfd = fopen(file,"w");
|
---|
42 | if (dfd != NULL) {
|
---|
43 | #if 0
|
---|
44 | fprintf(dfd,"Slirp %s - Debugging Started.\n", SLIRP_VERSION);
|
---|
45 | #endif
|
---|
46 | fprintf(dfd,"Debugging Started level %i.\r\n",dbg);
|
---|
47 | fflush(dfd);
|
---|
48 | slirp_debug = dbg;
|
---|
49 | } else {
|
---|
50 | lprint("Error: Debugging file \"%s\" could not be opened: %s\r\n",
|
---|
51 | file, strerror(errno));
|
---|
52 | }
|
---|
53 | }
|
---|
54 | #endif /* !VBOX */
|
---|
55 |
|
---|
56 | #ifndef VBOX
|
---|
57 | /*
|
---|
58 | * Dump a packet in the same format as tcpdump -x
|
---|
59 | */
|
---|
60 | #ifdef DEBUG
|
---|
61 | void
|
---|
62 | dump_packet(dat, n)
|
---|
63 | void *dat;
|
---|
64 | int n;
|
---|
65 | {
|
---|
66 | #ifndef VBOX
|
---|
67 | u_char *pptr = (u_char *)dat;
|
---|
68 | int j,k;
|
---|
69 |
|
---|
70 | n /= 16;
|
---|
71 | n++;
|
---|
72 | DEBUG_MISC((dfd, "PACKET DUMPED: \n"));
|
---|
73 | for(j = 0; j < n; j++) {
|
---|
74 | for(k = 0; k < 6; k++)
|
---|
75 | DEBUG_MISC((dfd, "%02x ", *pptr++));
|
---|
76 | DEBUG_MISC((dfd, "\n"));
|
---|
77 | fflush(dfd);
|
---|
78 | }
|
---|
79 | #else /* VBOX */
|
---|
80 | Log(("nat: PACKET DUMPED:\n%.*Vhxd\n", n, dat));
|
---|
81 | #endif /* VBOX */
|
---|
82 | }
|
---|
83 | #endif
|
---|
84 | #endif /* !VBOX */
|
---|
85 |
|
---|
86 | #if 0
|
---|
87 | /*
|
---|
88 | * Statistic routines
|
---|
89 | *
|
---|
90 | * These will print statistics to the screen, the debug file (dfd), or
|
---|
91 | * a buffer, depending on "type", so that the stats can be sent over
|
---|
92 | * the link as well.
|
---|
93 | */
|
---|
94 |
|
---|
95 | void
|
---|
96 | ttystats(ttyp)
|
---|
97 | struct ttys *ttyp;
|
---|
98 | {
|
---|
99 | struct slirp_ifstats *is = &ttyp->ifstats;
|
---|
100 | char buff[512];
|
---|
101 |
|
---|
102 | lprint(" \r\n");
|
---|
103 |
|
---|
104 | if (if_comp & IF_COMPRESS)
|
---|
105 | strcpy(buff, "on");
|
---|
106 | else if (if_comp & IF_NOCOMPRESS)
|
---|
107 | strcpy(buff, "off");
|
---|
108 | else
|
---|
109 | strcpy(buff, "off (for now)");
|
---|
110 | lprint("Unit %d:\r\n", ttyp->unit);
|
---|
111 | lprint(" using %s encapsulation (VJ compression is %s)\r\n", (
|
---|
112 | #ifdef USE_PPP
|
---|
113 | ttyp->proto==PROTO_PPP?"PPP":
|
---|
114 | #endif
|
---|
115 | "SLIP"), buff);
|
---|
116 | lprint(" %d baudrate\r\n", ttyp->baud);
|
---|
117 | lprint(" interface is %s\r\n", ttyp->up?"up":"down");
|
---|
118 | lprint(" using fd %d, guardian pid is %d\r\n", ttyp->fd, ttyp->pid);
|
---|
119 | #ifndef FULL_BOLT
|
---|
120 | lprint(" towrite is %d bytes\r\n", ttyp->towrite);
|
---|
121 | #endif
|
---|
122 | if (ttyp->zeros)
|
---|
123 | lprint(" %d zeros have been typed\r\n", ttyp->zeros);
|
---|
124 | else if (ttyp->ones)
|
---|
125 | lprint(" %d ones have been typed\r\n", ttyp->ones);
|
---|
126 | lprint("Interface stats:\r\n");
|
---|
127 | lprint(" %6d output packets sent (%d bytes)\r\n", is->out_pkts, is->out_bytes);
|
---|
128 | lprint(" %6d output packets dropped (%d bytes)\r\n", is->out_errpkts, is->out_errbytes);
|
---|
129 | lprint(" %6d input packets received (%d bytes)\r\n", is->in_pkts, is->in_bytes);
|
---|
130 | lprint(" %6d input packets dropped (%d bytes)\r\n", is->in_errpkts, is->in_errbytes);
|
---|
131 | lprint(" %6d bad input packets\r\n", is->in_mbad);
|
---|
132 | }
|
---|
133 |
|
---|
134 | void
|
---|
135 | allttystats()
|
---|
136 | {
|
---|
137 | struct ttys *ttyp;
|
---|
138 |
|
---|
139 | for (ttyp = ttys; ttyp; ttyp = ttyp->next)
|
---|
140 | ttystats(ttyp);
|
---|
141 | }
|
---|
142 | #endif
|
---|
143 |
|
---|
144 | void
|
---|
145 | #ifdef VBOX
|
---|
146 | ipstats(PNATState pData)
|
---|
147 | #else /* !VBOX */
|
---|
148 | ipstats()
|
---|
149 | #endif /* !VBOX */
|
---|
150 | {
|
---|
151 | lprint(" \r\n");
|
---|
152 |
|
---|
153 | lprint("IP stats:\r\n");
|
---|
154 | lprint(" %6d total packets received (%d were unaligned)\r\n",
|
---|
155 | ipstat.ips_total, ipstat.ips_unaligned);
|
---|
156 | lprint(" %6d with incorrect version\r\n", ipstat.ips_badvers);
|
---|
157 | lprint(" %6d with bad header checksum\r\n", ipstat.ips_badsum);
|
---|
158 | lprint(" %6d with length too short (len < sizeof(iphdr))\r\n", ipstat.ips_tooshort);
|
---|
159 | lprint(" %6d with length too small (len < ip->len)\r\n", ipstat.ips_toosmall);
|
---|
160 | lprint(" %6d with bad header length\r\n", ipstat.ips_badhlen);
|
---|
161 | lprint(" %6d with bad packet length\r\n", ipstat.ips_badlen);
|
---|
162 | lprint(" %6d fragments received\r\n", ipstat.ips_fragments);
|
---|
163 | lprint(" %6d fragments dropped\r\n", ipstat.ips_fragdropped);
|
---|
164 | lprint(" %6d fragments timed out\r\n", ipstat.ips_fragtimeout);
|
---|
165 | lprint(" %6d packets reassembled ok\r\n", ipstat.ips_reassembled);
|
---|
166 | lprint(" %6d outgoing packets fragmented\r\n", ipstat.ips_fragmented);
|
---|
167 | lprint(" %6d total outgoing fragments\r\n", ipstat.ips_ofragments);
|
---|
168 | lprint(" %6d with bad protocol field\r\n", ipstat.ips_noproto);
|
---|
169 | lprint(" %6d total packets delivered\r\n", ipstat.ips_delivered);
|
---|
170 | }
|
---|
171 |
|
---|
172 | #if 0
|
---|
173 | void
|
---|
174 | vjstats()
|
---|
175 | {
|
---|
176 | lprint(" \r\n");
|
---|
177 |
|
---|
178 | lprint("VJ compression stats:\r\n");
|
---|
179 |
|
---|
180 | lprint(" %6d outbound packets (%d compressed)\r\n",
|
---|
181 | comp_s.sls_packets, comp_s.sls_compressed);
|
---|
182 | lprint(" %6d searches for connection stats (%d misses)\r\n",
|
---|
183 | comp_s.sls_searches, comp_s.sls_misses);
|
---|
184 | lprint(" %6d inbound uncompressed packets\r\n", comp_s.sls_uncompressedin);
|
---|
185 | lprint(" %6d inbound compressed packets\r\n", comp_s.sls_compressedin);
|
---|
186 | lprint(" %6d inbound unknown type packets\r\n", comp_s.sls_errorin);
|
---|
187 | lprint(" %6d inbound packets tossed due to error\r\n", comp_s.sls_tossed);
|
---|
188 | }
|
---|
189 | #endif
|
---|
190 |
|
---|
191 | void
|
---|
192 | #ifdef VBOX
|
---|
193 | tcpstats(PNATState pData)
|
---|
194 | #else /* !VBOX */
|
---|
195 | tcpstats()
|
---|
196 | #endif /* !VBOX */
|
---|
197 | {
|
---|
198 | lprint(" \r\n");
|
---|
199 |
|
---|
200 | lprint("TCP stats:\r\n");
|
---|
201 |
|
---|
202 | lprint(" %6d packets sent\r\n", tcpstat.tcps_sndtotal);
|
---|
203 | lprint(" %6d data packets (%d bytes)\r\n",
|
---|
204 | tcpstat.tcps_sndpack, tcpstat.tcps_sndbyte);
|
---|
205 | lprint(" %6d data packets retransmitted (%d bytes)\r\n",
|
---|
206 | tcpstat.tcps_sndrexmitpack, tcpstat.tcps_sndrexmitbyte);
|
---|
207 | lprint(" %6d ack-only packets (%d delayed)\r\n",
|
---|
208 | tcpstat.tcps_sndacks, tcpstat.tcps_delack);
|
---|
209 | lprint(" %6d URG only packets\r\n", tcpstat.tcps_sndurg);
|
---|
210 | lprint(" %6d window probe packets\r\n", tcpstat.tcps_sndprobe);
|
---|
211 | lprint(" %6d window update packets\r\n", tcpstat.tcps_sndwinup);
|
---|
212 | lprint(" %6d control (SYN/FIN/RST) packets\r\n", tcpstat.tcps_sndctrl);
|
---|
213 | lprint(" %6d times tcp_output did nothing\r\n", tcpstat.tcps_didnuttin);
|
---|
214 |
|
---|
215 | lprint(" %6d packets received\r\n", tcpstat.tcps_rcvtotal);
|
---|
216 | lprint(" %6d acks (for %d bytes)\r\n",
|
---|
217 | tcpstat.tcps_rcvackpack, tcpstat.tcps_rcvackbyte);
|
---|
218 | lprint(" %6d duplicate acks\r\n", tcpstat.tcps_rcvdupack);
|
---|
219 | lprint(" %6d acks for unsent data\r\n", tcpstat.tcps_rcvacktoomuch);
|
---|
220 | lprint(" %6d packets received in sequence (%d bytes)\r\n",
|
---|
221 | tcpstat.tcps_rcvpack, tcpstat.tcps_rcvbyte);
|
---|
222 | lprint(" %6d completely duplicate packets (%d bytes)\r\n",
|
---|
223 | tcpstat.tcps_rcvduppack, tcpstat.tcps_rcvdupbyte);
|
---|
224 |
|
---|
225 | lprint(" %6d packets with some duplicate data (%d bytes duped)\r\n",
|
---|
226 | tcpstat.tcps_rcvpartduppack, tcpstat.tcps_rcvpartdupbyte);
|
---|
227 | lprint(" %6d out-of-order packets (%d bytes)\r\n",
|
---|
228 | tcpstat.tcps_rcvoopack, tcpstat.tcps_rcvoobyte);
|
---|
229 | lprint(" %6d packets of data after window (%d bytes)\r\n",
|
---|
230 | tcpstat.tcps_rcvpackafterwin, tcpstat.tcps_rcvbyteafterwin);
|
---|
231 | lprint(" %6d window probes\r\n", tcpstat.tcps_rcvwinprobe);
|
---|
232 | lprint(" %6d window update packets\r\n", tcpstat.tcps_rcvwinupd);
|
---|
233 | lprint(" %6d packets received after close\r\n", tcpstat.tcps_rcvafterclose);
|
---|
234 | lprint(" %6d discarded for bad checksums\r\n", tcpstat.tcps_rcvbadsum);
|
---|
235 | lprint(" %6d discarded for bad header offset fields\r\n",
|
---|
236 | tcpstat.tcps_rcvbadoff);
|
---|
237 |
|
---|
238 | lprint(" %6d connection requests\r\n", tcpstat.tcps_connattempt);
|
---|
239 | lprint(" %6d connection accepts\r\n", tcpstat.tcps_accepts);
|
---|
240 | lprint(" %6d connections established (including accepts)\r\n", tcpstat.tcps_connects);
|
---|
241 | lprint(" %6d connections closed (including %d drop)\r\n",
|
---|
242 | tcpstat.tcps_closed, tcpstat.tcps_drops);
|
---|
243 | lprint(" %6d embryonic connections dropped\r\n", tcpstat.tcps_conndrops);
|
---|
244 | lprint(" %6d segments we tried to get rtt (%d succeeded)\r\n",
|
---|
245 | tcpstat.tcps_segstimed, tcpstat.tcps_rttupdated);
|
---|
246 | lprint(" %6d retransmit timeouts\r\n", tcpstat.tcps_rexmttimeo);
|
---|
247 | lprint(" %6d connections dropped by rxmt timeout\r\n",
|
---|
248 | tcpstat.tcps_timeoutdrop);
|
---|
249 | lprint(" %6d persist timeouts\r\n", tcpstat.tcps_persisttimeo);
|
---|
250 | lprint(" %6d keepalive timeouts\r\n", tcpstat.tcps_keeptimeo);
|
---|
251 | lprint(" %6d keepalive probes sent\r\n", tcpstat.tcps_keepprobe);
|
---|
252 | lprint(" %6d connections dropped by keepalive\r\n", tcpstat.tcps_keepdrops);
|
---|
253 | lprint(" %6d correct ACK header predictions\r\n", tcpstat.tcps_predack);
|
---|
254 | lprint(" %6d correct data packet header predictions\n", tcpstat.tcps_preddat);
|
---|
255 | lprint(" %6d TCP cache misses\r\n", tcpstat.tcps_socachemiss);
|
---|
256 |
|
---|
257 |
|
---|
258 | /* lprint(" Packets received too short: %d\r\n", tcpstat.tcps_rcvshort); */
|
---|
259 | /* lprint(" Segments dropped due to PAWS: %d\r\n", tcpstat.tcps_pawsdrop); */
|
---|
260 |
|
---|
261 | }
|
---|
262 |
|
---|
263 | void
|
---|
264 | #ifdef VBOX
|
---|
265 | udpstats(PNATState pData)
|
---|
266 | #else /* !VBOX */
|
---|
267 | udpstats()
|
---|
268 | #endif /* !VBOX */
|
---|
269 | {
|
---|
270 | lprint(" \r\n");
|
---|
271 |
|
---|
272 | lprint("UDP stats:\r\n");
|
---|
273 | lprint(" %6d datagrams received\r\n", udpstat.udps_ipackets);
|
---|
274 | lprint(" %6d with packets shorter than header\r\n", udpstat.udps_hdrops);
|
---|
275 | lprint(" %6d with bad checksums\r\n", udpstat.udps_badsum);
|
---|
276 | lprint(" %6d with data length larger than packet\r\n", udpstat.udps_badlen);
|
---|
277 | lprint(" %6d UDP socket cache misses\r\n", udpstat.udpps_pcbcachemiss);
|
---|
278 | lprint(" %6d datagrams sent\r\n", udpstat.udps_opackets);
|
---|
279 | }
|
---|
280 |
|
---|
281 | void
|
---|
282 | #ifdef VBOX
|
---|
283 | icmpstats(PNATState pData)
|
---|
284 | #else /* !VBOX */
|
---|
285 | icmpstats()
|
---|
286 | #endif /* !VBOX */
|
---|
287 | {
|
---|
288 | lprint(" \r\n");
|
---|
289 | lprint("ICMP stats:\r\n");
|
---|
290 | lprint(" %6d ICMP packets received\r\n", icmpstat.icps_received);
|
---|
291 | lprint(" %6d were too short\r\n", icmpstat.icps_tooshort);
|
---|
292 | lprint(" %6d with bad checksums\r\n", icmpstat.icps_checksum);
|
---|
293 | lprint(" %6d with type not supported\r\n", icmpstat.icps_notsupp);
|
---|
294 | lprint(" %6d with bad type feilds\r\n", icmpstat.icps_badtype);
|
---|
295 | lprint(" %6d ICMP packets sent in reply\r\n", icmpstat.icps_reflect);
|
---|
296 | }
|
---|
297 |
|
---|
298 | void
|
---|
299 | #ifdef VBOX
|
---|
300 | mbufstats(PNATState pData)
|
---|
301 | #else /* !VBOX */
|
---|
302 | mbufstats()
|
---|
303 | #endif /* !VBOX */
|
---|
304 | {
|
---|
305 | struct mbuf *m;
|
---|
306 | int i;
|
---|
307 |
|
---|
308 | lprint(" \r\n");
|
---|
309 |
|
---|
310 | lprint("Mbuf stats:\r\n");
|
---|
311 |
|
---|
312 | lprint(" %6d mbufs allocated (%d max)\r\n", mbuf_alloced, mbuf_max);
|
---|
313 |
|
---|
314 | i = 0;
|
---|
315 | for (m = m_freelist.m_next; m != &m_freelist; m = m->m_next)
|
---|
316 | i++;
|
---|
317 | lprint(" %6d mbufs on free list\r\n", i);
|
---|
318 |
|
---|
319 | i = 0;
|
---|
320 | for (m = m_usedlist.m_next; m != &m_usedlist; m = m->m_next)
|
---|
321 | i++;
|
---|
322 | lprint(" %6d mbufs on used list\r\n", i);
|
---|
323 | lprint(" %6d mbufs queued as packets\r\n\r\n", if_queued);
|
---|
324 | }
|
---|
325 |
|
---|
326 | void
|
---|
327 | #ifdef VBOX
|
---|
328 | sockstats(PNATState pData)
|
---|
329 | #else /* !VBOX */
|
---|
330 | sockstats()
|
---|
331 | #endif /* !VBOX */
|
---|
332 | {
|
---|
333 | char buff[256];
|
---|
334 | int n;
|
---|
335 | struct socket *so;
|
---|
336 |
|
---|
337 | lprint(" \r\n");
|
---|
338 |
|
---|
339 | lprint(
|
---|
340 | "Proto[state] Sock Local Address, Port Remote Address, Port RecvQ SendQ\r\n");
|
---|
341 |
|
---|
342 | for (so = tcb.so_next; so != &tcb; so = so->so_next) {
|
---|
343 |
|
---|
344 | n = sprintf(buff, "tcp[%s]", so->so_tcpcb?tcpstates[so->so_tcpcb->t_state]:"NONE");
|
---|
345 | while (n < 17)
|
---|
346 | buff[n++] = ' ';
|
---|
347 | buff[17] = 0;
|
---|
348 | lprint("%s %3d %15s %5d ",
|
---|
349 | buff, so->s,
|
---|
350 | inet_ntoa(so->so_laddr), ntohs(so->so_lport));
|
---|
351 | lprint("%15s %5d %5d %5d\r\n",
|
---|
352 | inet_ntoa(so->so_faddr), ntohs(so->so_fport),
|
---|
353 | so->so_rcv.sb_cc, so->so_snd.sb_cc);
|
---|
354 | }
|
---|
355 |
|
---|
356 | for (so = udb.so_next; so != &udb; so = so->so_next) {
|
---|
357 |
|
---|
358 | n = sprintf(buff, "udp[%d sec]", (so->so_expire - curtime) / 1000);
|
---|
359 | while (n < 17)
|
---|
360 | buff[n++] = ' ';
|
---|
361 | buff[17] = 0;
|
---|
362 | lprint("%s %3d %15s %5d ",
|
---|
363 | buff, so->s,
|
---|
364 | inet_ntoa(so->so_laddr), ntohs(so->so_lport));
|
---|
365 | lprint("%15s %5d %5d %5d\r\n",
|
---|
366 | inet_ntoa(so->so_faddr), ntohs(so->so_fport),
|
---|
367 | so->so_rcv.sb_cc, so->so_snd.sb_cc);
|
---|
368 | }
|
---|
369 | }
|
---|
370 |
|
---|
371 | #if 0
|
---|
372 | void
|
---|
373 | slirp_exit(exit_status)
|
---|
374 | int exit_status;
|
---|
375 | {
|
---|
376 | struct ttys *ttyp;
|
---|
377 |
|
---|
378 | DEBUG_CALL("slirp_exit");
|
---|
379 | DEBUG_ARG("exit_status = %d", exit_status);
|
---|
380 |
|
---|
381 | if (dostats) {
|
---|
382 | lprint_print = (int (*) _P((void *, const char *, va_list)))vfprintf;
|
---|
383 | if (!dfd)
|
---|
384 | debug_init("slirp_stats", 0xf);
|
---|
385 | lprint_arg = (char **)&dfd;
|
---|
386 |
|
---|
387 | ipstats();
|
---|
388 | tcpstats();
|
---|
389 | udpstats();
|
---|
390 | icmpstats();
|
---|
391 | mbufstats();
|
---|
392 | sockstats();
|
---|
393 | allttystats();
|
---|
394 | vjstats();
|
---|
395 | }
|
---|
396 |
|
---|
397 | for (ttyp = ttys; ttyp; ttyp = ttyp->next)
|
---|
398 | tty_detached(ttyp, 1);
|
---|
399 |
|
---|
400 | if (slirp_forked) {
|
---|
401 | /* Menendez time */
|
---|
402 | if (kill(getppid(), SIGQUIT) < 0)
|
---|
403 | lprint("Couldn't kill parent process %ld!\n",
|
---|
404 | (long) getppid());
|
---|
405 | }
|
---|
406 |
|
---|
407 | /* Restore the terminal if we gotta */
|
---|
408 | if(slirp_tty_restore)
|
---|
409 | tcsetattr(0,TCSANOW, &slirp_tty_settings); /* NOW DAMMIT! */
|
---|
410 | exit(exit_status);
|
---|
411 | }
|
---|
412 | #endif
|
---|