]> git.proxmox.com Git - qemu.git/blob - slirp/slirp.c
slirp: Move smb, redir, tftp and bootp parameters and -net channel
[qemu.git] / slirp / slirp.c
1 /*
2 * libslirp glue
3 *
4 * Copyright (c) 2004-2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 #include "qemu-common.h"
25 #include "qemu-char.h"
26 #include "slirp.h"
27 #include "hw/hw.h"
28
29 /* host address */
30 struct in_addr our_addr;
31 /* host dns address */
32 struct in_addr dns_addr;
33 /* host loopback address */
34 struct in_addr loopback_addr;
35
36 /* address for slirp virtual addresses */
37 struct in_addr special_addr;
38 /* virtual address alias for host */
39 struct in_addr alias_addr;
40
41 static const uint8_t special_ethaddr[6] = {
42 0x52, 0x54, 0x00, 0x12, 0x35, 0x00
43 };
44
45 /* ARP cache for the guest IP addresses (XXX: allow many entries) */
46 uint8_t client_ethaddr[6];
47 static struct in_addr client_ipaddr;
48
49 static const uint8_t zero_ethaddr[6] = { 0, 0, 0, 0, 0, 0 };
50
51 const char *slirp_special_ip = CTL_SPECIAL;
52 int slirp_restrict;
53 static int do_slowtimo;
54 int link_up;
55 struct timeval tt;
56 FILE *lfd;
57 struct ex_list *exec_list;
58
59 /* XXX: suppress those select globals */
60 fd_set *global_readfds, *global_writefds, *global_xfds;
61
62 char slirp_hostname[33];
63
64 #ifdef _WIN32
65
66 static int get_dns_addr(struct in_addr *pdns_addr)
67 {
68 FIXED_INFO *FixedInfo=NULL;
69 ULONG BufLen;
70 DWORD ret;
71 IP_ADDR_STRING *pIPAddr;
72 struct in_addr tmp_addr;
73
74 FixedInfo = (FIXED_INFO *)GlobalAlloc(GPTR, sizeof(FIXED_INFO));
75 BufLen = sizeof(FIXED_INFO);
76
77 if (ERROR_BUFFER_OVERFLOW == GetNetworkParams(FixedInfo, &BufLen)) {
78 if (FixedInfo) {
79 GlobalFree(FixedInfo);
80 FixedInfo = NULL;
81 }
82 FixedInfo = GlobalAlloc(GPTR, BufLen);
83 }
84
85 if ((ret = GetNetworkParams(FixedInfo, &BufLen)) != ERROR_SUCCESS) {
86 printf("GetNetworkParams failed. ret = %08x\n", (u_int)ret );
87 if (FixedInfo) {
88 GlobalFree(FixedInfo);
89 FixedInfo = NULL;
90 }
91 return -1;
92 }
93
94 pIPAddr = &(FixedInfo->DnsServerList);
95 inet_aton(pIPAddr->IpAddress.String, &tmp_addr);
96 *pdns_addr = tmp_addr;
97 #if 0
98 printf( "DNS Servers:\n" );
99 printf( "DNS Addr:%s\n", pIPAddr->IpAddress.String );
100
101 pIPAddr = FixedInfo -> DnsServerList.Next;
102 while ( pIPAddr ) {
103 printf( "DNS Addr:%s\n", pIPAddr ->IpAddress.String );
104 pIPAddr = pIPAddr ->Next;
105 }
106 #endif
107 if (FixedInfo) {
108 GlobalFree(FixedInfo);
109 FixedInfo = NULL;
110 }
111 return 0;
112 }
113
114 #else
115
116 static int get_dns_addr(struct in_addr *pdns_addr)
117 {
118 char buff[512];
119 char buff2[257];
120 FILE *f;
121 int found = 0;
122 struct in_addr tmp_addr;
123
124 f = fopen("/etc/resolv.conf", "r");
125 if (!f)
126 return -1;
127
128 #ifdef DEBUG
129 lprint("IP address of your DNS(s): ");
130 #endif
131 while (fgets(buff, 512, f) != NULL) {
132 if (sscanf(buff, "nameserver%*[ \t]%256s", buff2) == 1) {
133 if (!inet_aton(buff2, &tmp_addr))
134 continue;
135 if (tmp_addr.s_addr == loopback_addr.s_addr)
136 tmp_addr = our_addr;
137 /* If it's the first one, set it to dns_addr */
138 if (!found)
139 *pdns_addr = tmp_addr;
140 #ifdef DEBUG
141 else
142 lprint(", ");
143 #endif
144 if (++found > 3) {
145 #ifdef DEBUG
146 lprint("(more)");
147 #endif
148 break;
149 }
150 #ifdef DEBUG
151 else
152 lprint("%s", inet_ntoa(tmp_addr));
153 #endif
154 }
155 }
156 fclose(f);
157 if (!found)
158 return -1;
159 return 0;
160 }
161
162 #endif
163
164 #ifdef _WIN32
165 static void slirp_cleanup(void)
166 {
167 WSACleanup();
168 }
169 #endif
170
171 static void slirp_state_save(QEMUFile *f, void *opaque);
172 static int slirp_state_load(QEMUFile *f, void *opaque, int version_id);
173
174 void slirp_init(int restricted, const char *special_ip, const char *tftp_path,
175 const char *bootfile)
176 {
177 // debug_init("/tmp/slirp.log", DEBUG_DEFAULT);
178
179 #ifdef _WIN32
180 {
181 WSADATA Data;
182 WSAStartup(MAKEWORD(2,0), &Data);
183 atexit(slirp_cleanup);
184 }
185 #endif
186
187 link_up = 1;
188 slirp_restrict = restricted;
189
190 if_init();
191 ip_init();
192
193 /* Initialise mbufs *after* setting the MTU */
194 m_init();
195
196 /* set default addresses */
197 inet_aton("127.0.0.1", &loopback_addr);
198
199 if (get_dns_addr(&dns_addr) < 0) {
200 dns_addr = loopback_addr;
201 fprintf (stderr, "Warning: No DNS servers found\n");
202 }
203
204 if (special_ip)
205 slirp_special_ip = special_ip;
206
207 qemu_free(tftp_prefix);
208 tftp_prefix = NULL;
209 if (tftp_path) {
210 tftp_prefix = qemu_strdup(tftp_path);
211 }
212 qemu_free(bootp_filename);
213 bootp_filename = NULL;
214 if (bootfile) {
215 bootp_filename = qemu_strdup(bootfile);
216 }
217
218 inet_aton(slirp_special_ip, &special_addr);
219 alias_addr.s_addr = special_addr.s_addr | htonl(CTL_ALIAS);
220 getouraddr();
221 register_savevm("slirp", 0, 1, slirp_state_save, slirp_state_load, NULL);
222 }
223
224 #define CONN_CANFSEND(so) (((so)->so_state & (SS_FCANTSENDMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
225 #define CONN_CANFRCV(so) (((so)->so_state & (SS_FCANTRCVMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
226 #define UPD_NFDS(x) if (nfds < (x)) nfds = (x)
227
228 /*
229 * curtime kept to an accuracy of 1ms
230 */
231 #ifdef _WIN32
232 static void updtime(void)
233 {
234 struct _timeb tb;
235
236 _ftime(&tb);
237 curtime = (u_int)tb.time * (u_int)1000;
238 curtime += (u_int)tb.millitm;
239 }
240 #else
241 static void updtime(void)
242 {
243 gettimeofday(&tt, NULL);
244
245 curtime = (u_int)tt.tv_sec * (u_int)1000;
246 curtime += (u_int)tt.tv_usec / (u_int)1000;
247
248 if ((tt.tv_usec % 1000) >= 500)
249 curtime++;
250 }
251 #endif
252
253 void slirp_select_fill(int *pnfds,
254 fd_set *readfds, fd_set *writefds, fd_set *xfds)
255 {
256 struct socket *so, *so_next;
257 struct timeval timeout;
258 int nfds;
259 int tmp_time;
260
261 /* fail safe */
262 global_readfds = NULL;
263 global_writefds = NULL;
264 global_xfds = NULL;
265
266 nfds = *pnfds;
267 /*
268 * First, TCP sockets
269 */
270 do_slowtimo = 0;
271 if (link_up) {
272 /*
273 * *_slowtimo needs calling if there are IP fragments
274 * in the fragment queue, or there are TCP connections active
275 */
276 do_slowtimo = ((tcb.so_next != &tcb) ||
277 (&ipq.ip_link != ipq.ip_link.next));
278
279 for (so = tcb.so_next; so != &tcb; so = so_next) {
280 so_next = so->so_next;
281
282 /*
283 * See if we need a tcp_fasttimo
284 */
285 if (time_fasttimo == 0 && so->so_tcpcb->t_flags & TF_DELACK)
286 time_fasttimo = curtime; /* Flag when we want a fasttimo */
287
288 /*
289 * NOFDREF can include still connecting to local-host,
290 * newly socreated() sockets etc. Don't want to select these.
291 */
292 if (so->so_state & SS_NOFDREF || so->s == -1)
293 continue;
294
295 /*
296 * Set for reading sockets which are accepting
297 */
298 if (so->so_state & SS_FACCEPTCONN) {
299 FD_SET(so->s, readfds);
300 UPD_NFDS(so->s);
301 continue;
302 }
303
304 /*
305 * Set for writing sockets which are connecting
306 */
307 if (so->so_state & SS_ISFCONNECTING) {
308 FD_SET(so->s, writefds);
309 UPD_NFDS(so->s);
310 continue;
311 }
312
313 /*
314 * Set for writing if we are connected, can send more, and
315 * we have something to send
316 */
317 if (CONN_CANFSEND(so) && so->so_rcv.sb_cc) {
318 FD_SET(so->s, writefds);
319 UPD_NFDS(so->s);
320 }
321
322 /*
323 * Set for reading (and urgent data) if we are connected, can
324 * receive more, and we have room for it XXX /2 ?
325 */
326 if (CONN_CANFRCV(so) && (so->so_snd.sb_cc < (so->so_snd.sb_datalen/2))) {
327 FD_SET(so->s, readfds);
328 FD_SET(so->s, xfds);
329 UPD_NFDS(so->s);
330 }
331 }
332
333 /*
334 * UDP sockets
335 */
336 for (so = udb.so_next; so != &udb; so = so_next) {
337 so_next = so->so_next;
338
339 /*
340 * See if it's timed out
341 */
342 if (so->so_expire) {
343 if (so->so_expire <= curtime) {
344 udp_detach(so);
345 continue;
346 } else
347 do_slowtimo = 1; /* Let socket expire */
348 }
349
350 /*
351 * When UDP packets are received from over the
352 * link, they're sendto()'d straight away, so
353 * no need for setting for writing
354 * Limit the number of packets queued by this session
355 * to 4. Note that even though we try and limit this
356 * to 4 packets, the session could have more queued
357 * if the packets needed to be fragmented
358 * (XXX <= 4 ?)
359 */
360 if ((so->so_state & SS_ISFCONNECTED) && so->so_queued <= 4) {
361 FD_SET(so->s, readfds);
362 UPD_NFDS(so->s);
363 }
364 }
365 }
366
367 /*
368 * Setup timeout to use minimum CPU usage, especially when idle
369 */
370
371 /*
372 * First, see the timeout needed by *timo
373 */
374 timeout.tv_sec = 0;
375 timeout.tv_usec = -1;
376 /*
377 * If a slowtimo is needed, set timeout to 500ms from the last
378 * slow timeout. If a fast timeout is needed, set timeout within
379 * 200ms of when it was requested.
380 */
381 if (do_slowtimo) {
382 /* XXX + 10000 because some select()'s aren't that accurate */
383 timeout.tv_usec = ((500 - (curtime - last_slowtimo)) * 1000) + 10000;
384 if (timeout.tv_usec < 0)
385 timeout.tv_usec = 0;
386 else if (timeout.tv_usec > 510000)
387 timeout.tv_usec = 510000;
388
389 /* Can only fasttimo if we also slowtimo */
390 if (time_fasttimo) {
391 tmp_time = (200 - (curtime - time_fasttimo)) * 1000;
392 if (tmp_time < 0)
393 tmp_time = 0;
394
395 /* Choose the smallest of the 2 */
396 if (tmp_time < timeout.tv_usec)
397 timeout.tv_usec = (u_int)tmp_time;
398 }
399 }
400 *pnfds = nfds;
401 }
402
403 void slirp_select_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds)
404 {
405 struct socket *so, *so_next;
406 int ret;
407
408 global_readfds = readfds;
409 global_writefds = writefds;
410 global_xfds = xfds;
411
412 /* Update time */
413 updtime();
414
415 /*
416 * See if anything has timed out
417 */
418 if (link_up) {
419 if (time_fasttimo && ((curtime - time_fasttimo) >= 2)) {
420 tcp_fasttimo();
421 time_fasttimo = 0;
422 }
423 if (do_slowtimo && ((curtime - last_slowtimo) >= 499)) {
424 ip_slowtimo();
425 tcp_slowtimo();
426 last_slowtimo = curtime;
427 }
428 }
429
430 /*
431 * Check sockets
432 */
433 if (link_up) {
434 /*
435 * Check TCP sockets
436 */
437 for (so = tcb.so_next; so != &tcb; so = so_next) {
438 so_next = so->so_next;
439
440 /*
441 * FD_ISSET is meaningless on these sockets
442 * (and they can crash the program)
443 */
444 if (so->so_state & SS_NOFDREF || so->s == -1)
445 continue;
446
447 /*
448 * Check for URG data
449 * This will soread as well, so no need to
450 * test for readfds below if this succeeds
451 */
452 if (FD_ISSET(so->s, xfds))
453 sorecvoob(so);
454 /*
455 * Check sockets for reading
456 */
457 else if (FD_ISSET(so->s, readfds)) {
458 /*
459 * Check for incoming connections
460 */
461 if (so->so_state & SS_FACCEPTCONN) {
462 tcp_connect(so);
463 continue;
464 } /* else */
465 ret = soread(so);
466
467 /* Output it if we read something */
468 if (ret > 0)
469 tcp_output(sototcpcb(so));
470 }
471
472 /*
473 * Check sockets for writing
474 */
475 if (FD_ISSET(so->s, writefds)) {
476 /*
477 * Check for non-blocking, still-connecting sockets
478 */
479 if (so->so_state & SS_ISFCONNECTING) {
480 /* Connected */
481 so->so_state &= ~SS_ISFCONNECTING;
482
483 ret = send(so->s, (const void *) &ret, 0, 0);
484 if (ret < 0) {
485 /* XXXXX Must fix, zero bytes is a NOP */
486 if (errno == EAGAIN || errno == EWOULDBLOCK ||
487 errno == EINPROGRESS || errno == ENOTCONN)
488 continue;
489
490 /* else failed */
491 so->so_state = SS_NOFDREF;
492 }
493 /* else so->so_state &= ~SS_ISFCONNECTING; */
494
495 /*
496 * Continue tcp_input
497 */
498 tcp_input((struct mbuf *)NULL, sizeof(struct ip), so);
499 /* continue; */
500 } else
501 ret = sowrite(so);
502 /*
503 * XXXXX If we wrote something (a lot), there
504 * could be a need for a window update.
505 * In the worst case, the remote will send
506 * a window probe to get things going again
507 */
508 }
509
510 /*
511 * Probe a still-connecting, non-blocking socket
512 * to check if it's still alive
513 */
514 #ifdef PROBE_CONN
515 if (so->so_state & SS_ISFCONNECTING) {
516 ret = recv(so->s, (char *)&ret, 0,0);
517
518 if (ret < 0) {
519 /* XXX */
520 if (errno == EAGAIN || errno == EWOULDBLOCK ||
521 errno == EINPROGRESS || errno == ENOTCONN)
522 continue; /* Still connecting, continue */
523
524 /* else failed */
525 so->so_state = SS_NOFDREF;
526
527 /* tcp_input will take care of it */
528 } else {
529 ret = send(so->s, &ret, 0,0);
530 if (ret < 0) {
531 /* XXX */
532 if (errno == EAGAIN || errno == EWOULDBLOCK ||
533 errno == EINPROGRESS || errno == ENOTCONN)
534 continue;
535 /* else failed */
536 so->so_state = SS_NOFDREF;
537 } else
538 so->so_state &= ~SS_ISFCONNECTING;
539
540 }
541 tcp_input((struct mbuf *)NULL, sizeof(struct ip),so);
542 } /* SS_ISFCONNECTING */
543 #endif
544 }
545
546 /*
547 * Now UDP sockets.
548 * Incoming packets are sent straight away, they're not buffered.
549 * Incoming UDP data isn't buffered either.
550 */
551 for (so = udb.so_next; so != &udb; so = so_next) {
552 so_next = so->so_next;
553
554 if (so->s != -1 && FD_ISSET(so->s, readfds)) {
555 sorecvfrom(so);
556 }
557 }
558 }
559
560 /*
561 * See if we can start outputting
562 */
563 if (if_queued && link_up)
564 if_start();
565
566 /* clear global file descriptor sets.
567 * these reside on the stack in vl.c
568 * so they're unusable if we're not in
569 * slirp_select_fill or slirp_select_poll.
570 */
571 global_readfds = NULL;
572 global_writefds = NULL;
573 global_xfds = NULL;
574 }
575
576 #define ETH_ALEN 6
577 #define ETH_HLEN 14
578
579 #define ETH_P_IP 0x0800 /* Internet Protocol packet */
580 #define ETH_P_ARP 0x0806 /* Address Resolution packet */
581
582 #define ARPOP_REQUEST 1 /* ARP request */
583 #define ARPOP_REPLY 2 /* ARP reply */
584
585 struct ethhdr
586 {
587 unsigned char h_dest[ETH_ALEN]; /* destination eth addr */
588 unsigned char h_source[ETH_ALEN]; /* source ether addr */
589 unsigned short h_proto; /* packet type ID field */
590 };
591
592 struct arphdr
593 {
594 unsigned short ar_hrd; /* format of hardware address */
595 unsigned short ar_pro; /* format of protocol address */
596 unsigned char ar_hln; /* length of hardware address */
597 unsigned char ar_pln; /* length of protocol address */
598 unsigned short ar_op; /* ARP opcode (command) */
599
600 /*
601 * Ethernet looks like this : This bit is variable sized however...
602 */
603 unsigned char ar_sha[ETH_ALEN]; /* sender hardware address */
604 unsigned char ar_sip[4]; /* sender IP address */
605 unsigned char ar_tha[ETH_ALEN]; /* target hardware address */
606 unsigned char ar_tip[4]; /* target IP address */
607 };
608
609 static void arp_input(const uint8_t *pkt, int pkt_len)
610 {
611 struct ethhdr *eh = (struct ethhdr *)pkt;
612 struct arphdr *ah = (struct arphdr *)(pkt + ETH_HLEN);
613 uint8_t arp_reply[ETH_HLEN + sizeof(struct arphdr)];
614 struct ethhdr *reh = (struct ethhdr *)arp_reply;
615 struct arphdr *rah = (struct arphdr *)(arp_reply + ETH_HLEN);
616 int ar_op;
617 struct ex_list *ex_ptr;
618
619 ar_op = ntohs(ah->ar_op);
620 switch(ar_op) {
621 case ARPOP_REQUEST:
622 if (!memcmp(ah->ar_tip, &special_addr, 3)) {
623 if (ah->ar_tip[3] == CTL_DNS || ah->ar_tip[3] == CTL_ALIAS)
624 goto arp_ok;
625 for (ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
626 if (ex_ptr->ex_addr == ah->ar_tip[3])
627 goto arp_ok;
628 }
629 return;
630 arp_ok:
631 /* XXX: make an ARP request to have the client address */
632 memcpy(client_ethaddr, eh->h_source, ETH_ALEN);
633
634 /* ARP request for alias/dns mac address */
635 memcpy(reh->h_dest, pkt + ETH_ALEN, ETH_ALEN);
636 memcpy(reh->h_source, special_ethaddr, ETH_ALEN - 1);
637 reh->h_source[5] = ah->ar_tip[3];
638 reh->h_proto = htons(ETH_P_ARP);
639
640 rah->ar_hrd = htons(1);
641 rah->ar_pro = htons(ETH_P_IP);
642 rah->ar_hln = ETH_ALEN;
643 rah->ar_pln = 4;
644 rah->ar_op = htons(ARPOP_REPLY);
645 memcpy(rah->ar_sha, reh->h_source, ETH_ALEN);
646 memcpy(rah->ar_sip, ah->ar_tip, 4);
647 memcpy(rah->ar_tha, ah->ar_sha, ETH_ALEN);
648 memcpy(rah->ar_tip, ah->ar_sip, 4);
649 slirp_output(arp_reply, sizeof(arp_reply));
650 }
651 break;
652 case ARPOP_REPLY:
653 /* reply to request of client mac address ? */
654 if (!memcmp(client_ethaddr, zero_ethaddr, ETH_ALEN) &&
655 !memcmp(ah->ar_sip, &client_ipaddr.s_addr, 4)) {
656 memcpy(client_ethaddr, ah->ar_sha, ETH_ALEN);
657 }
658 break;
659 default:
660 break;
661 }
662 }
663
664 void slirp_input(const uint8_t *pkt, int pkt_len)
665 {
666 struct mbuf *m;
667 int proto;
668
669 if (pkt_len < ETH_HLEN)
670 return;
671
672 proto = ntohs(*(uint16_t *)(pkt + 12));
673 switch(proto) {
674 case ETH_P_ARP:
675 arp_input(pkt, pkt_len);
676 break;
677 case ETH_P_IP:
678 m = m_get();
679 if (!m)
680 return;
681 /* Note: we add to align the IP header */
682 if (M_FREEROOM(m) < pkt_len + 2) {
683 m_inc(m, pkt_len + 2);
684 }
685 m->m_len = pkt_len + 2;
686 memcpy(m->m_data + 2, pkt, pkt_len);
687
688 m->m_data += 2 + ETH_HLEN;
689 m->m_len -= 2 + ETH_HLEN;
690
691 ip_input(m);
692 break;
693 default:
694 break;
695 }
696 }
697
698 /* output the IP packet to the ethernet device */
699 void if_encap(const uint8_t *ip_data, int ip_data_len)
700 {
701 uint8_t buf[1600];
702 struct ethhdr *eh = (struct ethhdr *)buf;
703
704 if (ip_data_len + ETH_HLEN > sizeof(buf))
705 return;
706
707 if (!memcmp(client_ethaddr, zero_ethaddr, ETH_ALEN)) {
708 uint8_t arp_req[ETH_HLEN + sizeof(struct arphdr)];
709 struct ethhdr *reh = (struct ethhdr *)arp_req;
710 struct arphdr *rah = (struct arphdr *)(arp_req + ETH_HLEN);
711 const struct ip *iph = (const struct ip *)ip_data;
712
713 /* If the client addr is not known, there is no point in
714 sending the packet to it. Normally the sender should have
715 done an ARP request to get its MAC address. Here we do it
716 in place of sending the packet and we hope that the sender
717 will retry sending its packet. */
718 memset(reh->h_dest, 0xff, ETH_ALEN);
719 memcpy(reh->h_source, special_ethaddr, ETH_ALEN - 1);
720 reh->h_source[5] = CTL_ALIAS;
721 reh->h_proto = htons(ETH_P_ARP);
722 rah->ar_hrd = htons(1);
723 rah->ar_pro = htons(ETH_P_IP);
724 rah->ar_hln = ETH_ALEN;
725 rah->ar_pln = 4;
726 rah->ar_op = htons(ARPOP_REQUEST);
727 /* source hw addr */
728 memcpy(rah->ar_sha, special_ethaddr, ETH_ALEN - 1);
729 rah->ar_sha[5] = CTL_ALIAS;
730 /* source IP */
731 memcpy(rah->ar_sip, &alias_addr, 4);
732 /* target hw addr (none) */
733 memset(rah->ar_tha, 0, ETH_ALEN);
734 /* target IP */
735 memcpy(rah->ar_tip, &iph->ip_dst, 4);
736 client_ipaddr = iph->ip_dst;
737 slirp_output(arp_req, sizeof(arp_req));
738 } else {
739 memcpy(eh->h_dest, client_ethaddr, ETH_ALEN);
740 memcpy(eh->h_source, special_ethaddr, ETH_ALEN - 1);
741 /* XXX: not correct */
742 eh->h_source[5] = CTL_ALIAS;
743 eh->h_proto = htons(ETH_P_IP);
744 memcpy(buf + sizeof(struct ethhdr), ip_data, ip_data_len);
745 slirp_output(buf, ip_data_len + ETH_HLEN);
746 }
747 }
748
749 /* Unlistens a redirection
750 *
751 * Return value: number of redirs removed */
752 int slirp_redir_rm(int is_udp, int host_port)
753 {
754 struct socket *so;
755 struct socket *head = (is_udp ? &udb : &tcb);
756 int fport = htons(host_port);
757 int n = 0;
758
759 loop_again:
760 for (so = head->so_next; so != head; so = so->so_next) {
761 if (so->so_fport == fport) {
762 close(so->s);
763 sofree(so);
764 n++;
765 goto loop_again;
766 }
767 }
768
769 return n;
770 }
771
772 int slirp_redir(int is_udp, int host_port,
773 struct in_addr guest_addr, int guest_port)
774 {
775 if (is_udp) {
776 if (!udp_listen(htons(host_port), guest_addr.s_addr,
777 htons(guest_port), 0))
778 return -1;
779 } else {
780 if (!solisten(htons(host_port), guest_addr.s_addr,
781 htons(guest_port), 0))
782 return -1;
783 }
784 return 0;
785 }
786
787 int slirp_add_exec(int do_pty, const void *args, int addr_low_byte,
788 int guest_port)
789 {
790 return add_exec(&exec_list, do_pty, (char *)args,
791 addr_low_byte, htons(guest_port));
792 }
793
794 ssize_t slirp_send(struct socket *so, const void *buf, size_t len, int flags)
795 {
796 if (so->s == -1 && so->extra) {
797 qemu_chr_write(so->extra, buf, len);
798 return len;
799 }
800
801 return send(so->s, buf, len, flags);
802 }
803
804 static struct socket *slirp_find_ctl_socket(int addr_low_byte, int guest_port)
805 {
806 struct socket *so;
807
808 for (so = tcb.so_next; so != &tcb; so = so->so_next) {
809 if ((so->so_faddr.s_addr & htonl(0xffffff00)) ==
810 special_addr.s_addr
811 && (ntohl(so->so_faddr.s_addr) & 0xff) ==
812 addr_low_byte
813 && htons(so->so_fport) == guest_port)
814 return so;
815 }
816
817 return NULL;
818 }
819
820 size_t slirp_socket_can_recv(int addr_low_byte, int guest_port)
821 {
822 struct iovec iov[2];
823 struct socket *so;
824
825 if (!link_up)
826 return 0;
827
828 so = slirp_find_ctl_socket(addr_low_byte, guest_port);
829
830 if (!so || so->so_state & SS_NOFDREF)
831 return 0;
832
833 if (!CONN_CANFRCV(so) || so->so_snd.sb_cc >= (so->so_snd.sb_datalen/2))
834 return 0;
835
836 return sopreprbuf(so, iov, NULL);
837 }
838
839 void slirp_socket_recv(int addr_low_byte, int guest_port, const uint8_t *buf,
840 int size)
841 {
842 int ret;
843 struct socket *so = slirp_find_ctl_socket(addr_low_byte, guest_port);
844
845 if (!so)
846 return;
847
848 ret = soreadbuf(so, (const char *)buf, size);
849
850 if (ret > 0)
851 tcp_output(sototcpcb(so));
852 }
853
854 static void slirp_tcp_save(QEMUFile *f, struct tcpcb *tp)
855 {
856 int i;
857
858 qemu_put_sbe16(f, tp->t_state);
859 for (i = 0; i < TCPT_NTIMERS; i++)
860 qemu_put_sbe16(f, tp->t_timer[i]);
861 qemu_put_sbe16(f, tp->t_rxtshift);
862 qemu_put_sbe16(f, tp->t_rxtcur);
863 qemu_put_sbe16(f, tp->t_dupacks);
864 qemu_put_be16(f, tp->t_maxseg);
865 qemu_put_sbyte(f, tp->t_force);
866 qemu_put_be16(f, tp->t_flags);
867 qemu_put_be32(f, tp->snd_una);
868 qemu_put_be32(f, tp->snd_nxt);
869 qemu_put_be32(f, tp->snd_up);
870 qemu_put_be32(f, tp->snd_wl1);
871 qemu_put_be32(f, tp->snd_wl2);
872 qemu_put_be32(f, tp->iss);
873 qemu_put_be32(f, tp->snd_wnd);
874 qemu_put_be32(f, tp->rcv_wnd);
875 qemu_put_be32(f, tp->rcv_nxt);
876 qemu_put_be32(f, tp->rcv_up);
877 qemu_put_be32(f, tp->irs);
878 qemu_put_be32(f, tp->rcv_adv);
879 qemu_put_be32(f, tp->snd_max);
880 qemu_put_be32(f, tp->snd_cwnd);
881 qemu_put_be32(f, tp->snd_ssthresh);
882 qemu_put_sbe16(f, tp->t_idle);
883 qemu_put_sbe16(f, tp->t_rtt);
884 qemu_put_be32(f, tp->t_rtseq);
885 qemu_put_sbe16(f, tp->t_srtt);
886 qemu_put_sbe16(f, tp->t_rttvar);
887 qemu_put_be16(f, tp->t_rttmin);
888 qemu_put_be32(f, tp->max_sndwnd);
889 qemu_put_byte(f, tp->t_oobflags);
890 qemu_put_byte(f, tp->t_iobc);
891 qemu_put_sbe16(f, tp->t_softerror);
892 qemu_put_byte(f, tp->snd_scale);
893 qemu_put_byte(f, tp->rcv_scale);
894 qemu_put_byte(f, tp->request_r_scale);
895 qemu_put_byte(f, tp->requested_s_scale);
896 qemu_put_be32(f, tp->ts_recent);
897 qemu_put_be32(f, tp->ts_recent_age);
898 qemu_put_be32(f, tp->last_ack_sent);
899 }
900
901 static void slirp_sbuf_save(QEMUFile *f, struct sbuf *sbuf)
902 {
903 uint32_t off;
904
905 qemu_put_be32(f, sbuf->sb_cc);
906 qemu_put_be32(f, sbuf->sb_datalen);
907 off = (uint32_t)(sbuf->sb_wptr - sbuf->sb_data);
908 qemu_put_sbe32(f, off);
909 off = (uint32_t)(sbuf->sb_rptr - sbuf->sb_data);
910 qemu_put_sbe32(f, off);
911 qemu_put_buffer(f, (unsigned char*)sbuf->sb_data, sbuf->sb_datalen);
912 }
913
914 static void slirp_socket_save(QEMUFile *f, struct socket *so)
915 {
916 qemu_put_be32(f, so->so_urgc);
917 qemu_put_be32(f, so->so_faddr.s_addr);
918 qemu_put_be32(f, so->so_laddr.s_addr);
919 qemu_put_be16(f, so->so_fport);
920 qemu_put_be16(f, so->so_lport);
921 qemu_put_byte(f, so->so_iptos);
922 qemu_put_byte(f, so->so_emu);
923 qemu_put_byte(f, so->so_type);
924 qemu_put_be32(f, so->so_state);
925 slirp_sbuf_save(f, &so->so_rcv);
926 slirp_sbuf_save(f, &so->so_snd);
927 slirp_tcp_save(f, so->so_tcpcb);
928 }
929
930 static void slirp_state_save(QEMUFile *f, void *opaque)
931 {
932 struct ex_list *ex_ptr;
933
934 for (ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next)
935 if (ex_ptr->ex_pty == 3) {
936 struct socket *so;
937 so = slirp_find_ctl_socket(ex_ptr->ex_addr, ntohs(ex_ptr->ex_fport));
938 if (!so)
939 continue;
940
941 qemu_put_byte(f, 42);
942 slirp_socket_save(f, so);
943 }
944 qemu_put_byte(f, 0);
945 }
946
947 static void slirp_tcp_load(QEMUFile *f, struct tcpcb *tp)
948 {
949 int i;
950
951 tp->t_state = qemu_get_sbe16(f);
952 for (i = 0; i < TCPT_NTIMERS; i++)
953 tp->t_timer[i] = qemu_get_sbe16(f);
954 tp->t_rxtshift = qemu_get_sbe16(f);
955 tp->t_rxtcur = qemu_get_sbe16(f);
956 tp->t_dupacks = qemu_get_sbe16(f);
957 tp->t_maxseg = qemu_get_be16(f);
958 tp->t_force = qemu_get_sbyte(f);
959 tp->t_flags = qemu_get_be16(f);
960 tp->snd_una = qemu_get_be32(f);
961 tp->snd_nxt = qemu_get_be32(f);
962 tp->snd_up = qemu_get_be32(f);
963 tp->snd_wl1 = qemu_get_be32(f);
964 tp->snd_wl2 = qemu_get_be32(f);
965 tp->iss = qemu_get_be32(f);
966 tp->snd_wnd = qemu_get_be32(f);
967 tp->rcv_wnd = qemu_get_be32(f);
968 tp->rcv_nxt = qemu_get_be32(f);
969 tp->rcv_up = qemu_get_be32(f);
970 tp->irs = qemu_get_be32(f);
971 tp->rcv_adv = qemu_get_be32(f);
972 tp->snd_max = qemu_get_be32(f);
973 tp->snd_cwnd = qemu_get_be32(f);
974 tp->snd_ssthresh = qemu_get_be32(f);
975 tp->t_idle = qemu_get_sbe16(f);
976 tp->t_rtt = qemu_get_sbe16(f);
977 tp->t_rtseq = qemu_get_be32(f);
978 tp->t_srtt = qemu_get_sbe16(f);
979 tp->t_rttvar = qemu_get_sbe16(f);
980 tp->t_rttmin = qemu_get_be16(f);
981 tp->max_sndwnd = qemu_get_be32(f);
982 tp->t_oobflags = qemu_get_byte(f);
983 tp->t_iobc = qemu_get_byte(f);
984 tp->t_softerror = qemu_get_sbe16(f);
985 tp->snd_scale = qemu_get_byte(f);
986 tp->rcv_scale = qemu_get_byte(f);
987 tp->request_r_scale = qemu_get_byte(f);
988 tp->requested_s_scale = qemu_get_byte(f);
989 tp->ts_recent = qemu_get_be32(f);
990 tp->ts_recent_age = qemu_get_be32(f);
991 tp->last_ack_sent = qemu_get_be32(f);
992 tcp_template(tp);
993 }
994
995 static int slirp_sbuf_load(QEMUFile *f, struct sbuf *sbuf)
996 {
997 uint32_t off, sb_cc, sb_datalen;
998
999 sb_cc = qemu_get_be32(f);
1000 sb_datalen = qemu_get_be32(f);
1001
1002 sbreserve(sbuf, sb_datalen);
1003
1004 if (sbuf->sb_datalen != sb_datalen)
1005 return -ENOMEM;
1006
1007 sbuf->sb_cc = sb_cc;
1008
1009 off = qemu_get_sbe32(f);
1010 sbuf->sb_wptr = sbuf->sb_data + off;
1011 off = qemu_get_sbe32(f);
1012 sbuf->sb_rptr = sbuf->sb_data + off;
1013 qemu_get_buffer(f, (unsigned char*)sbuf->sb_data, sbuf->sb_datalen);
1014
1015 return 0;
1016 }
1017
1018 static int slirp_socket_load(QEMUFile *f, struct socket *so)
1019 {
1020 if (tcp_attach(so) < 0)
1021 return -ENOMEM;
1022
1023 so->so_urgc = qemu_get_be32(f);
1024 so->so_faddr.s_addr = qemu_get_be32(f);
1025 so->so_laddr.s_addr = qemu_get_be32(f);
1026 so->so_fport = qemu_get_be16(f);
1027 so->so_lport = qemu_get_be16(f);
1028 so->so_iptos = qemu_get_byte(f);
1029 so->so_emu = qemu_get_byte(f);
1030 so->so_type = qemu_get_byte(f);
1031 so->so_state = qemu_get_be32(f);
1032 if (slirp_sbuf_load(f, &so->so_rcv) < 0)
1033 return -ENOMEM;
1034 if (slirp_sbuf_load(f, &so->so_snd) < 0)
1035 return -ENOMEM;
1036 slirp_tcp_load(f, so->so_tcpcb);
1037
1038 return 0;
1039 }
1040
1041 static int slirp_state_load(QEMUFile *f, void *opaque, int version_id)
1042 {
1043 struct ex_list *ex_ptr;
1044 int r;
1045
1046 while ((r = qemu_get_byte(f))) {
1047 int ret;
1048 struct socket *so = socreate();
1049
1050 if (!so)
1051 return -ENOMEM;
1052
1053 ret = slirp_socket_load(f, so);
1054
1055 if (ret < 0)
1056 return ret;
1057
1058 if ((so->so_faddr.s_addr & htonl(0xffffff00)) != special_addr.s_addr)
1059 return -EINVAL;
1060
1061 for (ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next)
1062 if (ex_ptr->ex_pty == 3 &&
1063 (ntohl(so->so_faddr.s_addr) & 0xff) == ex_ptr->ex_addr &&
1064 so->so_fport == ex_ptr->ex_fport)
1065 break;
1066
1067 if (!ex_ptr)
1068 return -EINVAL;
1069
1070 so->extra = (void *)ex_ptr->ex_exec;
1071 }
1072
1073 return 0;
1074 }