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