ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/slirp/slirp.c
Revision: 1.3
Committed: 2006-01-17T21:19:12Z (18 years, 10 months ago) by gbeauche
Content type: text/plain
Branch: MAIN
Changes since 1.2: +1 -1 lines
Log Message:
Packet headers can be examined through unaligned addresses. This patch
fixes this, especially for MIPS & SPARC platforms. [Initial patch from
Brian J. Johnson]

File Contents

# User Rev Content
1 gbeauche 1.1 #include "slirp.h"
2    
3     /* host address */
4     struct in_addr our_addr;
5     /* host dns address */
6     struct in_addr dns_addr;
7     /* host loopback address */
8     struct in_addr loopback_addr;
9    
10     /* address for slirp virtual addresses */
11     struct in_addr special_addr;
12    
13     const uint8_t special_ethaddr[6] = {
14     0x52, 0x54, 0x00, 0x12, 0x35, 0x00
15     };
16    
17     uint8_t client_ethaddr[6];
18    
19     int do_slowtimo;
20     int link_up;
21     struct timeval tt;
22     FILE *lfd;
23     struct ex_list *exec_list;
24    
25     /* XXX: suppress those select globals */
26     fd_set *global_readfds, *global_writefds, *global_xfds;
27    
28     #ifdef _WIN32
29    
30     static int get_dns_addr(struct in_addr *pdns_addr)
31     {
32     FIXED_INFO *FixedInfo=NULL;
33     ULONG BufLen;
34     DWORD ret;
35     IP_ADDR_STRING *pIPAddr;
36     struct in_addr tmp_addr;
37    
38     FixedInfo = (FIXED_INFO *)GlobalAlloc(GPTR, sizeof(FIXED_INFO));
39     BufLen = sizeof(FIXED_INFO);
40    
41     if (ERROR_BUFFER_OVERFLOW == GetNetworkParams(FixedInfo, &BufLen)) {
42     if (FixedInfo) {
43     GlobalFree(FixedInfo);
44     FixedInfo = NULL;
45     }
46     FixedInfo = GlobalAlloc(GPTR, BufLen);
47     }
48    
49     if ((ret = GetNetworkParams(FixedInfo, &BufLen)) != ERROR_SUCCESS) {
50     printf("GetNetworkParams failed. ret = %08x\n", (u_int)ret );
51     if (FixedInfo) {
52     GlobalFree(FixedInfo);
53     FixedInfo = NULL;
54     }
55     return -1;
56     }
57    
58     pIPAddr = &(FixedInfo->DnsServerList);
59     inet_aton(pIPAddr->IpAddress.String, &tmp_addr);
60     *pdns_addr = tmp_addr;
61     #if 0
62     printf( "DNS Servers:\n" );
63     printf( "DNS Addr:%s\n", pIPAddr->IpAddress.String );
64    
65     pIPAddr = FixedInfo -> DnsServerList.Next;
66     while ( pIPAddr ) {
67     printf( "DNS Addr:%s\n", pIPAddr ->IpAddress.String );
68     pIPAddr = pIPAddr ->Next;
69     }
70     #endif
71     if (FixedInfo) {
72     GlobalFree(FixedInfo);
73     FixedInfo = NULL;
74     }
75     return 0;
76     }
77    
78     #else
79    
80     static int get_dns_addr(struct in_addr *pdns_addr)
81     {
82     char buff[512];
83     char buff2[256];
84     FILE *f;
85     int found = 0;
86     struct in_addr tmp_addr;
87    
88     f = fopen("/etc/resolv.conf", "r");
89     if (!f)
90     return -1;
91    
92     lprint("IP address of your DNS(s): ");
93     while (fgets(buff, 512, f) != NULL) {
94     if (sscanf(buff, "nameserver%*[ \t]%256s", buff2) == 1) {
95     if (!inet_aton(buff2, &tmp_addr))
96     continue;
97     if (tmp_addr.s_addr == loopback_addr.s_addr)
98     tmp_addr = our_addr;
99     /* If it's the first one, set it to dns_addr */
100     if (!found)
101     *pdns_addr = tmp_addr;
102     else
103     lprint(", ");
104     if (++found > 3) {
105     lprint("(more)");
106     break;
107     } else
108     lprint("%s", inet_ntoa(tmp_addr));
109     }
110     }
111     fclose(f);
112     if (!found)
113     return -1;
114     return 0;
115     }
116    
117     #endif
118    
119     #ifdef _WIN32
120     void slirp_cleanup(void)
121     {
122     WSACleanup();
123     }
124     #endif
125    
126 gbeauche 1.2 int slirp_init(void)
127 gbeauche 1.1 {
128     // debug_init("/tmp/slirp.log", DEBUG_DEFAULT);
129    
130     #ifdef _WIN32
131     {
132     WSADATA Data;
133     WSAStartup(MAKEWORD(2,0), &Data);
134     atexit(slirp_cleanup);
135     }
136     #endif
137    
138     link_up = 1;
139    
140     if_init();
141     ip_init();
142    
143     /* Initialise mbufs *after* setting the MTU */
144     m_init();
145    
146     /* set default addresses */
147     getouraddr();
148     inet_aton("127.0.0.1", &loopback_addr);
149    
150 gbeauche 1.2 if (get_dns_addr(&dns_addr) < 0)
151     return -1;
152 gbeauche 1.1
153     inet_aton(CTL_SPECIAL, &special_addr);
154 gbeauche 1.2 return 0;
155 gbeauche 1.1 }
156    
157     #define CONN_CANFSEND(so) (((so)->so_state & (SS_FCANTSENDMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
158     #define CONN_CANFRCV(so) (((so)->so_state & (SS_FCANTRCVMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
159     #define UPD_NFDS(x) if (nfds < (x)) nfds = (x)
160    
161     /*
162     * curtime kept to an accuracy of 1ms
163     */
164     #ifdef _WIN32
165     static void updtime(void)
166     {
167     struct _timeb tb;
168    
169     _ftime(&tb);
170     curtime = (u_int)tb.time * (u_int)1000;
171     curtime += (u_int)tb.millitm;
172     }
173     #else
174     static void updtime(void)
175     {
176     gettimeofday(&tt, 0);
177    
178     curtime = (u_int)tt.tv_sec * (u_int)1000;
179     curtime += (u_int)tt.tv_usec / (u_int)1000;
180    
181     if ((tt.tv_usec % 1000) >= 500)
182     curtime++;
183     }
184     #endif
185    
186     void slirp_select_fill(int *pnfds,
187     fd_set *readfds, fd_set *writefds, fd_set *xfds)
188     {
189     struct socket *so, *so_next;
190     struct timeval timeout;
191     int nfds;
192     int tmp_time;
193    
194     /* fail safe */
195     global_readfds = NULL;
196     global_writefds = NULL;
197     global_xfds = NULL;
198    
199     nfds = *pnfds;
200     /*
201     * First, TCP sockets
202     */
203     do_slowtimo = 0;
204     if (link_up) {
205     /*
206     * *_slowtimo needs calling if there are IP fragments
207     * in the fragment queue, or there are TCP connections active
208     */
209     do_slowtimo = ((tcb.so_next != &tcb) ||
210     ((struct ipasfrag *)&ipq != (struct ipasfrag *)ipq.next));
211    
212     for (so = tcb.so_next; so != &tcb; so = so_next) {
213     so_next = so->so_next;
214    
215     /*
216     * See if we need a tcp_fasttimo
217     */
218     if (time_fasttimo == 0 && so->so_tcpcb->t_flags & TF_DELACK)
219     time_fasttimo = curtime; /* Flag when we want a fasttimo */
220    
221     /*
222     * NOFDREF can include still connecting to local-host,
223     * newly socreated() sockets etc. Don't want to select these.
224     */
225     if (so->so_state & SS_NOFDREF || so->s == -1)
226     continue;
227    
228     /*
229     * Set for reading sockets which are accepting
230     */
231     if (so->so_state & SS_FACCEPTCONN) {
232     FD_SET(so->s, readfds);
233     UPD_NFDS(so->s);
234     continue;
235     }
236    
237     /*
238     * Set for writing sockets which are connecting
239     */
240     if (so->so_state & SS_ISFCONNECTING) {
241     FD_SET(so->s, writefds);
242     UPD_NFDS(so->s);
243     continue;
244     }
245    
246     /*
247     * Set for writing if we are connected, can send more, and
248     * we have something to send
249     */
250     if (CONN_CANFSEND(so) && so->so_rcv.sb_cc) {
251     FD_SET(so->s, writefds);
252     UPD_NFDS(so->s);
253     }
254    
255     /*
256     * Set for reading (and urgent data) if we are connected, can
257     * receive more, and we have room for it XXX /2 ?
258     */
259     if (CONN_CANFRCV(so) && (so->so_snd.sb_cc < (so->so_snd.sb_datalen/2))) {
260     FD_SET(so->s, readfds);
261     FD_SET(so->s, xfds);
262     UPD_NFDS(so->s);
263     }
264     }
265    
266     /*
267     * UDP sockets
268     */
269     for (so = udb.so_next; so != &udb; so = so_next) {
270     so_next = so->so_next;
271    
272     /*
273     * See if it's timed out
274     */
275     if (so->so_expire) {
276     if (so->so_expire <= curtime) {
277     udp_detach(so);
278     continue;
279     } else
280     do_slowtimo = 1; /* Let socket expire */
281     }
282    
283     /*
284     * When UDP packets are received from over the
285     * link, they're sendto()'d straight away, so
286     * no need for setting for writing
287     * Limit the number of packets queued by this session
288     * to 4. Note that even though we try and limit this
289     * to 4 packets, the session could have more queued
290     * if the packets needed to be fragmented
291     * (XXX <= 4 ?)
292     */
293     if ((so->so_state & SS_ISFCONNECTED) && so->so_queued <= 4) {
294     FD_SET(so->s, readfds);
295     UPD_NFDS(so->s);
296     }
297     }
298     }
299    
300     /*
301     * Setup timeout to use minimum CPU usage, especially when idle
302     */
303    
304     /*
305     * First, see the timeout needed by *timo
306     */
307     timeout.tv_sec = 0;
308     timeout.tv_usec = -1;
309     /*
310     * If a slowtimo is needed, set timeout to 500ms from the last
311     * slow timeout. If a fast timeout is needed, set timeout within
312     * 200ms of when it was requested.
313     */
314     if (do_slowtimo) {
315     /* XXX + 10000 because some select()'s aren't that accurate */
316     timeout.tv_usec = ((500 - (curtime - last_slowtimo)) * 1000) + 10000;
317     if (timeout.tv_usec < 0)
318     timeout.tv_usec = 0;
319     else if (timeout.tv_usec > 510000)
320     timeout.tv_usec = 510000;
321    
322     /* Can only fasttimo if we also slowtimo */
323     if (time_fasttimo) {
324     tmp_time = (200 - (curtime - time_fasttimo)) * 1000;
325     if (tmp_time < 0)
326     tmp_time = 0;
327    
328     /* Choose the smallest of the 2 */
329     if (tmp_time < timeout.tv_usec)
330     timeout.tv_usec = (u_int)tmp_time;
331     }
332     }
333     *pnfds = nfds;
334     }
335    
336     void slirp_select_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds)
337     {
338     struct socket *so, *so_next;
339     int ret;
340    
341     global_readfds = readfds;
342     global_writefds = writefds;
343     global_xfds = xfds;
344    
345     /* Update time */
346     updtime();
347    
348     /*
349     * See if anything has timed out
350     */
351     if (link_up) {
352     if (time_fasttimo && ((curtime - time_fasttimo) >= 199)) {
353     tcp_fasttimo();
354     time_fasttimo = 0;
355     }
356     if (do_slowtimo && ((curtime - last_slowtimo) >= 499)) {
357     ip_slowtimo();
358     tcp_slowtimo();
359     last_slowtimo = curtime;
360     }
361     }
362    
363     /*
364     * Check sockets
365     */
366     if (link_up) {
367     /*
368     * Check TCP sockets
369     */
370     for (so = tcb.so_next; so != &tcb; so = so_next) {
371     so_next = so->so_next;
372    
373     /*
374     * FD_ISSET is meaningless on these sockets
375     * (and they can crash the program)
376     */
377     if (so->so_state & SS_NOFDREF || so->s == -1)
378     continue;
379    
380     /*
381     * Check for URG data
382     * This will soread as well, so no need to
383     * test for readfds below if this succeeds
384     */
385     if (FD_ISSET(so->s, xfds))
386     sorecvoob(so);
387     /*
388     * Check sockets for reading
389     */
390     else if (FD_ISSET(so->s, readfds)) {
391     /*
392     * Check for incoming connections
393     */
394     if (so->so_state & SS_FACCEPTCONN) {
395     tcp_connect(so);
396     continue;
397     } /* else */
398     ret = soread(so);
399    
400     /* Output it if we read something */
401     if (ret > 0)
402     tcp_output(sototcpcb(so));
403     }
404    
405     /*
406     * Check sockets for writing
407     */
408     if (FD_ISSET(so->s, writefds)) {
409     /*
410     * Check for non-blocking, still-connecting sockets
411     */
412     if (so->so_state & SS_ISFCONNECTING) {
413     /* Connected */
414     so->so_state &= ~SS_ISFCONNECTING;
415    
416     ret = send(so->s, &ret, 0, 0);
417     if (ret < 0) {
418     /* XXXXX Must fix, zero bytes is a NOP */
419     if (errno == EAGAIN || errno == EWOULDBLOCK ||
420     errno == EINPROGRESS || errno == ENOTCONN)
421     continue;
422    
423     /* else failed */
424     so->so_state = SS_NOFDREF;
425     }
426     /* else so->so_state &= ~SS_ISFCONNECTING; */
427    
428     /*
429     * Continue tcp_input
430     */
431     tcp_input((struct mbuf *)NULL, sizeof(struct ip), so);
432     /* continue; */
433     } else
434     ret = sowrite(so);
435     /*
436     * XXXXX If we wrote something (a lot), there
437     * could be a need for a window update.
438     * In the worst case, the remote will send
439     * a window probe to get things going again
440     */
441     }
442    
443     /*
444     * Probe a still-connecting, non-blocking socket
445     * to check if it's still alive
446     */
447     #ifdef PROBE_CONN
448     if (so->so_state & SS_ISFCONNECTING) {
449     ret = recv(so->s, (char *)&ret, 0,0);
450    
451     if (ret < 0) {
452     /* XXX */
453     if (errno == EAGAIN || errno == EWOULDBLOCK ||
454     errno == EINPROGRESS || errno == ENOTCONN)
455     continue; /* Still connecting, continue */
456    
457     /* else failed */
458     so->so_state = SS_NOFDREF;
459    
460     /* tcp_input will take care of it */
461     } else {
462     ret = send(so->s, &ret, 0,0);
463     if (ret < 0) {
464     /* XXX */
465     if (errno == EAGAIN || errno == EWOULDBLOCK ||
466     errno == EINPROGRESS || errno == ENOTCONN)
467     continue;
468     /* else failed */
469     so->so_state = SS_NOFDREF;
470     } else
471     so->so_state &= ~SS_ISFCONNECTING;
472    
473     }
474     tcp_input((struct mbuf *)NULL, sizeof(struct ip),so);
475     } /* SS_ISFCONNECTING */
476     #endif
477     }
478    
479     /*
480     * Now UDP sockets.
481     * Incoming packets are sent straight away, they're not buffered.
482     * Incoming UDP data isn't buffered either.
483     */
484     for (so = udb.so_next; so != &udb; so = so_next) {
485     so_next = so->so_next;
486    
487     if (so->s != -1 && FD_ISSET(so->s, readfds)) {
488     sorecvfrom(so);
489     }
490     }
491     }
492    
493     /*
494     * See if we can start outputting
495     */
496     if (if_queued && link_up)
497     if_start();
498    
499     /* clear global file descriptor sets.
500     * these reside on the stack in vl.c
501     * so they're unusable if we're not in
502     * slirp_select_fill or slirp_select_poll.
503     */
504     global_readfds = NULL;
505     global_writefds = NULL;
506     global_xfds = NULL;
507     }
508    
509     #define ETH_ALEN 6
510     #define ETH_HLEN 14
511    
512     #define ETH_P_IP 0x0800 /* Internet Protocol packet */
513     #define ETH_P_ARP 0x0806 /* Address Resolution packet */
514    
515     #define ARPOP_REQUEST 1 /* ARP request */
516     #define ARPOP_REPLY 2 /* ARP reply */
517    
518     struct ethhdr
519     {
520     unsigned char h_dest[ETH_ALEN]; /* destination eth addr */
521     unsigned char h_source[ETH_ALEN]; /* source ether addr */
522     unsigned short h_proto; /* packet type ID field */
523     };
524    
525     struct arphdr
526     {
527     unsigned short ar_hrd; /* format of hardware address */
528     unsigned short ar_pro; /* format of protocol address */
529     unsigned char ar_hln; /* length of hardware address */
530     unsigned char ar_pln; /* length of protocol address */
531     unsigned short ar_op; /* ARP opcode (command) */
532    
533     /*
534     * Ethernet looks like this : This bit is variable sized however...
535     */
536     unsigned char ar_sha[ETH_ALEN]; /* sender hardware address */
537     unsigned char ar_sip[4]; /* sender IP address */
538     unsigned char ar_tha[ETH_ALEN]; /* target hardware address */
539     unsigned char ar_tip[4]; /* target IP address */
540     };
541    
542     void arp_input(const uint8_t *pkt, int pkt_len)
543     {
544     struct ethhdr *eh = (struct ethhdr *)pkt;
545     struct arphdr *ah = (struct arphdr *)(pkt + ETH_HLEN);
546     uint8_t arp_reply[ETH_HLEN + sizeof(struct arphdr)];
547     struct ethhdr *reh = (struct ethhdr *)arp_reply;
548     struct arphdr *rah = (struct arphdr *)(arp_reply + ETH_HLEN);
549     int ar_op;
550     struct ex_list *ex_ptr;
551    
552     ar_op = ntohs(ah->ar_op);
553     switch(ar_op) {
554     case ARPOP_REQUEST:
555     if (!memcmp(ah->ar_tip, &special_addr, 3)) {
556     if (ah->ar_tip[3] == CTL_DNS || ah->ar_tip[3] == CTL_ALIAS)
557     goto arp_ok;
558     for (ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
559     if (ex_ptr->ex_addr == ah->ar_tip[3])
560     goto arp_ok;
561     }
562     return;
563     arp_ok:
564     /* XXX: make an ARP request to have the client address */
565     memcpy(client_ethaddr, eh->h_source, ETH_ALEN);
566    
567     /* ARP request for alias/dns mac address */
568     memcpy(reh->h_dest, pkt + ETH_ALEN, ETH_ALEN);
569     memcpy(reh->h_source, special_ethaddr, ETH_ALEN - 1);
570     reh->h_source[5] = ah->ar_tip[3];
571     reh->h_proto = htons(ETH_P_ARP);
572    
573     rah->ar_hrd = htons(1);
574     rah->ar_pro = htons(ETH_P_IP);
575     rah->ar_hln = ETH_ALEN;
576     rah->ar_pln = 4;
577     rah->ar_op = htons(ARPOP_REPLY);
578     memcpy(rah->ar_sha, reh->h_source, ETH_ALEN);
579     memcpy(rah->ar_sip, ah->ar_tip, 4);
580     memcpy(rah->ar_tha, ah->ar_sha, ETH_ALEN);
581     memcpy(rah->ar_tip, ah->ar_sip, 4);
582     slirp_output(arp_reply, sizeof(arp_reply));
583     }
584     break;
585     default:
586     break;
587     }
588     }
589    
590     void slirp_input(const uint8_t *pkt, int pkt_len)
591     {
592     struct mbuf *m;
593     int proto;
594    
595     if (pkt_len < ETH_HLEN)
596     return;
597    
598 gbeauche 1.3 proto = (pkt[12] << 8) | pkt[13];
599 gbeauche 1.1 switch(proto) {
600     case ETH_P_ARP:
601     arp_input(pkt, pkt_len);
602     break;
603     case ETH_P_IP:
604     m = m_get();
605     if (!m)
606     return;
607     m->m_len = pkt_len;
608     memcpy(m->m_data, pkt, pkt_len);
609    
610     m->m_data += ETH_HLEN;
611     m->m_len -= ETH_HLEN;
612    
613     ip_input(m);
614     break;
615     default:
616     break;
617     }
618     }
619    
620     /* output the IP packet to the ethernet device */
621     void if_encap(const uint8_t *ip_data, int ip_data_len)
622     {
623     uint8_t buf[1600];
624     struct ethhdr *eh = (struct ethhdr *)buf;
625    
626     if (ip_data_len + ETH_HLEN > sizeof(buf))
627     return;
628    
629     memcpy(eh->h_dest, client_ethaddr, ETH_ALEN);
630     memcpy(eh->h_source, special_ethaddr, ETH_ALEN - 1);
631     /* XXX: not correct */
632     eh->h_source[5] = CTL_ALIAS;
633     eh->h_proto = htons(ETH_P_IP);
634     memcpy(buf + sizeof(struct ethhdr), ip_data, ip_data_len);
635     slirp_output(buf, ip_data_len + ETH_HLEN);
636     }
637    
638     int slirp_redir(int is_udp, int host_port,
639     struct in_addr guest_addr, int guest_port)
640     {
641     if (is_udp) {
642     if (!udp_listen(htons(host_port), guest_addr.s_addr,
643     htons(guest_port), 0))
644     return -1;
645     } else {
646     if (!solisten(htons(host_port), guest_addr.s_addr,
647     htons(guest_port), 0))
648     return -1;
649     }
650     return 0;
651     }
652    
653     int slirp_add_exec(int do_pty, const char *args, int addr_low_byte,
654     int guest_port)
655     {
656     return add_exec(&exec_list, do_pty, (char *)args,
657     addr_low_byte, htons(guest_port));
658     }