]> git.proxmox.com Git - qemu.git/blob - slirp/slirp.c
Merge remote-tracking branch 'kraxel/seabios-1.6.3.2' into staging
[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-timer.h"
26 #include "qemu-char.h"
27 #include "slirp.h"
28 #include "hw/hw.h"
29
30 /* host loopback address */
31 struct in_addr loopback_addr;
32
33 /* emulated hosts use the MAC addr 52:55:IP:IP:IP:IP */
34 static const uint8_t special_ethaddr[ETH_ALEN] = {
35 0x52, 0x55, 0x00, 0x00, 0x00, 0x00
36 };
37
38 static const uint8_t zero_ethaddr[ETH_ALEN] = { 0, 0, 0, 0, 0, 0 };
39
40 /* XXX: suppress those select globals */
41 fd_set *global_readfds, *global_writefds, *global_xfds;
42
43 u_int curtime;
44 static u_int time_fasttimo, last_slowtimo;
45 static int do_slowtimo;
46
47 static QTAILQ_HEAD(slirp_instances, Slirp) slirp_instances =
48 QTAILQ_HEAD_INITIALIZER(slirp_instances);
49
50 static struct in_addr dns_addr;
51 static u_int dns_addr_time;
52
53 #ifdef _WIN32
54
55 int get_dns_addr(struct in_addr *pdns_addr)
56 {
57 FIXED_INFO *FixedInfo=NULL;
58 ULONG BufLen;
59 DWORD ret;
60 IP_ADDR_STRING *pIPAddr;
61 struct in_addr tmp_addr;
62
63 if (dns_addr.s_addr != 0 && (curtime - dns_addr_time) < 1000) {
64 *pdns_addr = dns_addr;
65 return 0;
66 }
67
68 FixedInfo = (FIXED_INFO *)GlobalAlloc(GPTR, sizeof(FIXED_INFO));
69 BufLen = sizeof(FIXED_INFO);
70
71 if (ERROR_BUFFER_OVERFLOW == GetNetworkParams(FixedInfo, &BufLen)) {
72 if (FixedInfo) {
73 GlobalFree(FixedInfo);
74 FixedInfo = NULL;
75 }
76 FixedInfo = GlobalAlloc(GPTR, BufLen);
77 }
78
79 if ((ret = GetNetworkParams(FixedInfo, &BufLen)) != ERROR_SUCCESS) {
80 printf("GetNetworkParams failed. ret = %08x\n", (u_int)ret );
81 if (FixedInfo) {
82 GlobalFree(FixedInfo);
83 FixedInfo = NULL;
84 }
85 return -1;
86 }
87
88 pIPAddr = &(FixedInfo->DnsServerList);
89 inet_aton(pIPAddr->IpAddress.String, &tmp_addr);
90 *pdns_addr = tmp_addr;
91 dns_addr = tmp_addr;
92 dns_addr_time = curtime;
93 if (FixedInfo) {
94 GlobalFree(FixedInfo);
95 FixedInfo = NULL;
96 }
97 return 0;
98 }
99
100 static void winsock_cleanup(void)
101 {
102 WSACleanup();
103 }
104
105 #else
106
107 static struct stat dns_addr_stat;
108
109 int get_dns_addr(struct in_addr *pdns_addr)
110 {
111 char buff[512];
112 char buff2[257];
113 FILE *f;
114 int found = 0;
115 struct in_addr tmp_addr;
116
117 if (dns_addr.s_addr != 0) {
118 struct stat old_stat;
119 if ((curtime - dns_addr_time) < 1000) {
120 *pdns_addr = dns_addr;
121 return 0;
122 }
123 old_stat = dns_addr_stat;
124 if (stat("/etc/resolv.conf", &dns_addr_stat) != 0)
125 return -1;
126 if ((dns_addr_stat.st_dev == old_stat.st_dev)
127 && (dns_addr_stat.st_ino == old_stat.st_ino)
128 && (dns_addr_stat.st_size == old_stat.st_size)
129 && (dns_addr_stat.st_mtime == old_stat.st_mtime)) {
130 *pdns_addr = dns_addr;
131 return 0;
132 }
133 }
134
135 f = fopen("/etc/resolv.conf", "r");
136 if (!f)
137 return -1;
138
139 #ifdef DEBUG
140 lprint("IP address of your DNS(s): ");
141 #endif
142 while (fgets(buff, 512, f) != NULL) {
143 if (sscanf(buff, "nameserver%*[ \t]%256s", buff2) == 1) {
144 if (!inet_aton(buff2, &tmp_addr))
145 continue;
146 /* If it's the first one, set it to dns_addr */
147 if (!found) {
148 *pdns_addr = tmp_addr;
149 dns_addr = tmp_addr;
150 dns_addr_time = curtime;
151 }
152 #ifdef DEBUG
153 else
154 lprint(", ");
155 #endif
156 if (++found > 3) {
157 #ifdef DEBUG
158 lprint("(more)");
159 #endif
160 break;
161 }
162 #ifdef DEBUG
163 else
164 lprint("%s", inet_ntoa(tmp_addr));
165 #endif
166 }
167 }
168 fclose(f);
169 if (!found)
170 return -1;
171 return 0;
172 }
173
174 #endif
175
176 static void slirp_init_once(void)
177 {
178 static int initialized;
179 #ifdef _WIN32
180 WSADATA Data;
181 #endif
182
183 if (initialized) {
184 return;
185 }
186 initialized = 1;
187
188 #ifdef _WIN32
189 WSAStartup(MAKEWORD(2,0), &Data);
190 atexit(winsock_cleanup);
191 #endif
192
193 loopback_addr.s_addr = htonl(INADDR_LOOPBACK);
194 }
195
196 static void slirp_state_save(QEMUFile *f, void *opaque);
197 static int slirp_state_load(QEMUFile *f, void *opaque, int version_id);
198
199 Slirp *slirp_init(int restricted, struct in_addr vnetwork,
200 struct in_addr vnetmask, struct in_addr vhost,
201 const char *vhostname, const char *tftp_path,
202 const char *bootfile, struct in_addr vdhcp_start,
203 struct in_addr vnameserver, void *opaque)
204 {
205 Slirp *slirp = g_malloc0(sizeof(Slirp));
206
207 slirp_init_once();
208
209 slirp->restricted = restricted;
210
211 if_init(slirp);
212 ip_init(slirp);
213
214 /* Initialise mbufs *after* setting the MTU */
215 m_init(slirp);
216
217 slirp->vnetwork_addr = vnetwork;
218 slirp->vnetwork_mask = vnetmask;
219 slirp->vhost_addr = vhost;
220 if (vhostname) {
221 pstrcpy(slirp->client_hostname, sizeof(slirp->client_hostname),
222 vhostname);
223 }
224 if (tftp_path) {
225 slirp->tftp_prefix = g_strdup(tftp_path);
226 }
227 if (bootfile) {
228 slirp->bootp_filename = g_strdup(bootfile);
229 }
230 slirp->vdhcp_startaddr = vdhcp_start;
231 slirp->vnameserver_addr = vnameserver;
232
233 slirp->opaque = opaque;
234
235 register_savevm(NULL, "slirp", 0, 3,
236 slirp_state_save, slirp_state_load, slirp);
237
238 QTAILQ_INSERT_TAIL(&slirp_instances, slirp, entry);
239
240 return slirp;
241 }
242
243 void slirp_cleanup(Slirp *slirp)
244 {
245 QTAILQ_REMOVE(&slirp_instances, slirp, entry);
246
247 unregister_savevm(NULL, "slirp", slirp);
248
249 ip_cleanup(slirp);
250 m_cleanup(slirp);
251
252 g_free(slirp->tftp_prefix);
253 g_free(slirp->bootp_filename);
254 g_free(slirp);
255 }
256
257 #define CONN_CANFSEND(so) (((so)->so_state & (SS_FCANTSENDMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
258 #define CONN_CANFRCV(so) (((so)->so_state & (SS_FCANTRCVMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
259 #define UPD_NFDS(x) if (nfds < (x)) nfds = (x)
260
261 void slirp_select_fill(int *pnfds,
262 fd_set *readfds, fd_set *writefds, fd_set *xfds)
263 {
264 Slirp *slirp;
265 struct socket *so, *so_next;
266 int nfds;
267
268 if (QTAILQ_EMPTY(&slirp_instances)) {
269 return;
270 }
271
272 /* fail safe */
273 global_readfds = NULL;
274 global_writefds = NULL;
275 global_xfds = NULL;
276
277 nfds = *pnfds;
278 /*
279 * First, TCP sockets
280 */
281 do_slowtimo = 0;
282
283 QTAILQ_FOREACH(slirp, &slirp_instances, entry) {
284 /*
285 * *_slowtimo needs calling if there are IP fragments
286 * in the fragment queue, or there are TCP connections active
287 */
288 do_slowtimo |= ((slirp->tcb.so_next != &slirp->tcb) ||
289 (&slirp->ipq.ip_link != slirp->ipq.ip_link.next));
290
291 for (so = slirp->tcb.so_next; so != &slirp->tcb;
292 so = so_next) {
293 so_next = so->so_next;
294
295 /*
296 * See if we need a tcp_fasttimo
297 */
298 if (time_fasttimo == 0 && so->so_tcpcb->t_flags & TF_DELACK)
299 time_fasttimo = curtime; /* Flag when we want a fasttimo */
300
301 /*
302 * NOFDREF can include still connecting to local-host,
303 * newly socreated() sockets etc. Don't want to select these.
304 */
305 if (so->so_state & SS_NOFDREF || so->s == -1)
306 continue;
307
308 /*
309 * Set for reading sockets which are accepting
310 */
311 if (so->so_state & SS_FACCEPTCONN) {
312 FD_SET(so->s, readfds);
313 UPD_NFDS(so->s);
314 continue;
315 }
316
317 /*
318 * Set for writing sockets which are connecting
319 */
320 if (so->so_state & SS_ISFCONNECTING) {
321 FD_SET(so->s, writefds);
322 UPD_NFDS(so->s);
323 continue;
324 }
325
326 /*
327 * Set for writing if we are connected, can send more, and
328 * we have something to send
329 */
330 if (CONN_CANFSEND(so) && so->so_rcv.sb_cc) {
331 FD_SET(so->s, writefds);
332 UPD_NFDS(so->s);
333 }
334
335 /*
336 * Set for reading (and urgent data) if we are connected, can
337 * receive more, and we have room for it XXX /2 ?
338 */
339 if (CONN_CANFRCV(so) && (so->so_snd.sb_cc < (so->so_snd.sb_datalen/2))) {
340 FD_SET(so->s, readfds);
341 FD_SET(so->s, xfds);
342 UPD_NFDS(so->s);
343 }
344 }
345
346 /*
347 * UDP sockets
348 */
349 for (so = slirp->udb.so_next; so != &slirp->udb;
350 so = so_next) {
351 so_next = so->so_next;
352
353 /*
354 * See if it's timed out
355 */
356 if (so->so_expire) {
357 if (so->so_expire <= curtime) {
358 udp_detach(so);
359 continue;
360 } else
361 do_slowtimo = 1; /* Let socket expire */
362 }
363
364 /*
365 * When UDP packets are received from over the
366 * link, they're sendto()'d straight away, so
367 * no need for setting for writing
368 * Limit the number of packets queued by this session
369 * to 4. Note that even though we try and limit this
370 * to 4 packets, the session could have more queued
371 * if the packets needed to be fragmented
372 * (XXX <= 4 ?)
373 */
374 if ((so->so_state & SS_ISFCONNECTED) && so->so_queued <= 4) {
375 FD_SET(so->s, readfds);
376 UPD_NFDS(so->s);
377 }
378 }
379
380 /*
381 * ICMP sockets
382 */
383 for (so = slirp->icmp.so_next; so != &slirp->icmp;
384 so = so_next) {
385 so_next = so->so_next;
386
387 /*
388 * See if it's timed out
389 */
390 if (so->so_expire) {
391 if (so->so_expire <= curtime) {
392 icmp_detach(so);
393 continue;
394 } else {
395 do_slowtimo = 1; /* Let socket expire */
396 }
397 }
398
399 if (so->so_state & SS_ISFCONNECTED) {
400 FD_SET(so->s, readfds);
401 UPD_NFDS(so->s);
402 }
403 }
404 }
405
406 *pnfds = nfds;
407 }
408
409 void slirp_select_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds,
410 int select_error)
411 {
412 Slirp *slirp;
413 struct socket *so, *so_next;
414 int ret;
415
416 if (QTAILQ_EMPTY(&slirp_instances)) {
417 return;
418 }
419
420 global_readfds = readfds;
421 global_writefds = writefds;
422 global_xfds = xfds;
423
424 curtime = qemu_get_clock_ms(rt_clock);
425
426 QTAILQ_FOREACH(slirp, &slirp_instances, entry) {
427 /*
428 * See if anything has timed out
429 */
430 if (time_fasttimo && ((curtime - time_fasttimo) >= 2)) {
431 tcp_fasttimo(slirp);
432 time_fasttimo = 0;
433 }
434 if (do_slowtimo && ((curtime - last_slowtimo) >= 499)) {
435 ip_slowtimo(slirp);
436 tcp_slowtimo(slirp);
437 last_slowtimo = curtime;
438 }
439
440 /*
441 * Check sockets
442 */
443 if (!select_error) {
444 /*
445 * Check TCP sockets
446 */
447 for (so = slirp->tcb.so_next; so != &slirp->tcb;
448 so = so_next) {
449 so_next = so->so_next;
450
451 /*
452 * FD_ISSET is meaningless on these sockets
453 * (and they can crash the program)
454 */
455 if (so->so_state & SS_NOFDREF || so->s == -1)
456 continue;
457
458 /*
459 * Check for URG data
460 * This will soread as well, so no need to
461 * test for readfds below if this succeeds
462 */
463 if (FD_ISSET(so->s, xfds))
464 sorecvoob(so);
465 /*
466 * Check sockets for reading
467 */
468 else if (FD_ISSET(so->s, readfds)) {
469 /*
470 * Check for incoming connections
471 */
472 if (so->so_state & SS_FACCEPTCONN) {
473 tcp_connect(so);
474 continue;
475 } /* else */
476 ret = soread(so);
477
478 /* Output it if we read something */
479 if (ret > 0)
480 tcp_output(sototcpcb(so));
481 }
482
483 /*
484 * Check sockets for writing
485 */
486 if (FD_ISSET(so->s, writefds)) {
487 /*
488 * Check for non-blocking, still-connecting sockets
489 */
490 if (so->so_state & SS_ISFCONNECTING) {
491 /* Connected */
492 so->so_state &= ~SS_ISFCONNECTING;
493
494 ret = send(so->s, (const void *) &ret, 0, 0);
495 if (ret < 0) {
496 /* XXXXX Must fix, zero bytes is a NOP */
497 if (errno == EAGAIN || errno == EWOULDBLOCK ||
498 errno == EINPROGRESS || errno == ENOTCONN)
499 continue;
500
501 /* else failed */
502 so->so_state &= SS_PERSISTENT_MASK;
503 so->so_state |= SS_NOFDREF;
504 }
505 /* else so->so_state &= ~SS_ISFCONNECTING; */
506
507 /*
508 * Continue tcp_input
509 */
510 tcp_input((struct mbuf *)NULL, sizeof(struct ip), so);
511 /* continue; */
512 } else
513 ret = sowrite(so);
514 /*
515 * XXXXX If we wrote something (a lot), there
516 * could be a need for a window update.
517 * In the worst case, the remote will send
518 * a window probe to get things going again
519 */
520 }
521
522 /*
523 * Probe a still-connecting, non-blocking socket
524 * to check if it's still alive
525 */
526 #ifdef PROBE_CONN
527 if (so->so_state & SS_ISFCONNECTING) {
528 ret = qemu_recv(so->s, &ret, 0,0);
529
530 if (ret < 0) {
531 /* XXX */
532 if (errno == EAGAIN || errno == EWOULDBLOCK ||
533 errno == EINPROGRESS || errno == ENOTCONN)
534 continue; /* Still connecting, continue */
535
536 /* else failed */
537 so->so_state &= SS_PERSISTENT_MASK;
538 so->so_state |= SS_NOFDREF;
539
540 /* tcp_input will take care of it */
541 } else {
542 ret = send(so->s, &ret, 0,0);
543 if (ret < 0) {
544 /* XXX */
545 if (errno == EAGAIN || errno == EWOULDBLOCK ||
546 errno == EINPROGRESS || errno == ENOTCONN)
547 continue;
548 /* else failed */
549 so->so_state &= SS_PERSISTENT_MASK;
550 so->so_state |= SS_NOFDREF;
551 } else
552 so->so_state &= ~SS_ISFCONNECTING;
553
554 }
555 tcp_input((struct mbuf *)NULL, sizeof(struct ip),so);
556 } /* SS_ISFCONNECTING */
557 #endif
558 }
559
560 /*
561 * Now UDP sockets.
562 * Incoming packets are sent straight away, they're not buffered.
563 * Incoming UDP data isn't buffered either.
564 */
565 for (so = slirp->udb.so_next; so != &slirp->udb;
566 so = so_next) {
567 so_next = so->so_next;
568
569 if (so->s != -1 && FD_ISSET(so->s, readfds)) {
570 sorecvfrom(so);
571 }
572 }
573
574 /*
575 * Check incoming ICMP relies.
576 */
577 for (so = slirp->icmp.so_next; so != &slirp->icmp;
578 so = so_next) {
579 so_next = so->so_next;
580
581 if (so->s != -1 && FD_ISSET(so->s, readfds)) {
582 icmp_receive(so);
583 }
584 }
585 }
586
587 if_start(slirp);
588 }
589
590 /* clear global file descriptor sets.
591 * these reside on the stack in vl.c
592 * so they're unusable if we're not in
593 * slirp_select_fill or slirp_select_poll.
594 */
595 global_readfds = NULL;
596 global_writefds = NULL;
597 global_xfds = NULL;
598 }
599
600 static void arp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len)
601 {
602 struct arphdr *ah = (struct arphdr *)(pkt + ETH_HLEN);
603 uint8_t arp_reply[max(ETH_HLEN + sizeof(struct arphdr), 64)];
604 struct ethhdr *reh = (struct ethhdr *)arp_reply;
605 struct arphdr *rah = (struct arphdr *)(arp_reply + ETH_HLEN);
606 int ar_op;
607 struct ex_list *ex_ptr;
608
609 ar_op = ntohs(ah->ar_op);
610 switch(ar_op) {
611 case ARPOP_REQUEST:
612 if (ah->ar_tip == ah->ar_sip) {
613 /* Gratuitous ARP */
614 arp_table_add(slirp, ah->ar_sip, ah->ar_sha);
615 return;
616 }
617
618 if ((ah->ar_tip & slirp->vnetwork_mask.s_addr) ==
619 slirp->vnetwork_addr.s_addr) {
620 if (ah->ar_tip == slirp->vnameserver_addr.s_addr ||
621 ah->ar_tip == slirp->vhost_addr.s_addr)
622 goto arp_ok;
623 for (ex_ptr = slirp->exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
624 if (ex_ptr->ex_addr.s_addr == ah->ar_tip)
625 goto arp_ok;
626 }
627 return;
628 arp_ok:
629 memset(arp_reply, 0, sizeof(arp_reply));
630
631 arp_table_add(slirp, ah->ar_sip, ah->ar_sha);
632
633 /* ARP request for alias/dns mac address */
634 memcpy(reh->h_dest, pkt + ETH_ALEN, ETH_ALEN);
635 memcpy(reh->h_source, special_ethaddr, ETH_ALEN - 4);
636 memcpy(&reh->h_source[2], &ah->ar_tip, 4);
637 reh->h_proto = htons(ETH_P_ARP);
638
639 rah->ar_hrd = htons(1);
640 rah->ar_pro = htons(ETH_P_IP);
641 rah->ar_hln = ETH_ALEN;
642 rah->ar_pln = 4;
643 rah->ar_op = htons(ARPOP_REPLY);
644 memcpy(rah->ar_sha, reh->h_source, ETH_ALEN);
645 rah->ar_sip = ah->ar_tip;
646 memcpy(rah->ar_tha, ah->ar_sha, ETH_ALEN);
647 rah->ar_tip = ah->ar_sip;
648 slirp_output(slirp->opaque, arp_reply, sizeof(arp_reply));
649 }
650 break;
651 case ARPOP_REPLY:
652 arp_table_add(slirp, ah->ar_sip, ah->ar_sha);
653 break;
654 default:
655 break;
656 }
657 }
658
659 void slirp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len)
660 {
661 struct mbuf *m;
662 int proto;
663
664 if (pkt_len < ETH_HLEN)
665 return;
666
667 proto = ntohs(*(uint16_t *)(pkt + 12));
668 switch(proto) {
669 case ETH_P_ARP:
670 arp_input(slirp, pkt, pkt_len);
671 break;
672 case ETH_P_IP:
673 m = m_get(slirp);
674 if (!m)
675 return;
676 /* Note: we add to align the IP header */
677 if (M_FREEROOM(m) < pkt_len + 2) {
678 m_inc(m, pkt_len + 2);
679 }
680 m->m_len = pkt_len + 2;
681 memcpy(m->m_data + 2, pkt, pkt_len);
682
683 m->m_data += 2 + ETH_HLEN;
684 m->m_len -= 2 + ETH_HLEN;
685
686 ip_input(m);
687 break;
688 default:
689 break;
690 }
691 }
692
693 /* Output the IP packet to the ethernet device. Returns 0 if the packet must be
694 * re-queued.
695 */
696 int if_encap(Slirp *slirp, struct mbuf *ifm)
697 {
698 uint8_t buf[1600];
699 struct ethhdr *eh = (struct ethhdr *)buf;
700 uint8_t ethaddr[ETH_ALEN];
701 const struct ip *iph = (const struct ip *)ifm->m_data;
702
703 if (ifm->m_len + ETH_HLEN > sizeof(buf)) {
704 return 1;
705 }
706
707 if (!arp_table_search(slirp, iph->ip_dst.s_addr, ethaddr)) {
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
712 if (!ifm->arp_requested) {
713 /* If the client addr is not known, send an ARP request */
714 memset(reh->h_dest, 0xff, ETH_ALEN);
715 memcpy(reh->h_source, special_ethaddr, ETH_ALEN - 4);
716 memcpy(&reh->h_source[2], &slirp->vhost_addr, 4);
717 reh->h_proto = htons(ETH_P_ARP);
718 rah->ar_hrd = htons(1);
719 rah->ar_pro = htons(ETH_P_IP);
720 rah->ar_hln = ETH_ALEN;
721 rah->ar_pln = 4;
722 rah->ar_op = htons(ARPOP_REQUEST);
723
724 /* source hw addr */
725 memcpy(rah->ar_sha, special_ethaddr, ETH_ALEN - 4);
726 memcpy(&rah->ar_sha[2], &slirp->vhost_addr, 4);
727
728 /* source IP */
729 rah->ar_sip = slirp->vhost_addr.s_addr;
730
731 /* target hw addr (none) */
732 memset(rah->ar_tha, 0, ETH_ALEN);
733
734 /* target IP */
735 rah->ar_tip = iph->ip_dst.s_addr;
736 slirp->client_ipaddr = iph->ip_dst;
737 slirp_output(slirp->opaque, arp_req, sizeof(arp_req));
738 ifm->arp_requested = true;
739
740 /* Expire request and drop outgoing packet after 1 second */
741 ifm->expiration_date = qemu_get_clock_ns(rt_clock) + 1000000000ULL;
742 }
743 return 0;
744 } else {
745 memcpy(eh->h_dest, ethaddr, ETH_ALEN);
746 memcpy(eh->h_source, special_ethaddr, ETH_ALEN - 4);
747 /* XXX: not correct */
748 memcpy(&eh->h_source[2], &slirp->vhost_addr, 4);
749 eh->h_proto = htons(ETH_P_IP);
750 memcpy(buf + sizeof(struct ethhdr), ifm->m_data, ifm->m_len);
751 slirp_output(slirp->opaque, buf, ifm->m_len + ETH_HLEN);
752 return 1;
753 }
754 }
755
756 /* Drop host forwarding rule, return 0 if found. */
757 int slirp_remove_hostfwd(Slirp *slirp, int is_udp, struct in_addr host_addr,
758 int host_port)
759 {
760 struct socket *so;
761 struct socket *head = (is_udp ? &slirp->udb : &slirp->tcb);
762 struct sockaddr_in addr;
763 int port = htons(host_port);
764 socklen_t addr_len;
765
766 for (so = head->so_next; so != head; so = so->so_next) {
767 addr_len = sizeof(addr);
768 if ((so->so_state & SS_HOSTFWD) &&
769 getsockname(so->s, (struct sockaddr *)&addr, &addr_len) == 0 &&
770 addr.sin_addr.s_addr == host_addr.s_addr &&
771 addr.sin_port == port) {
772 close(so->s);
773 sofree(so);
774 return 0;
775 }
776 }
777
778 return -1;
779 }
780
781 int slirp_add_hostfwd(Slirp *slirp, int is_udp, struct in_addr host_addr,
782 int host_port, struct in_addr guest_addr, int guest_port)
783 {
784 if (!guest_addr.s_addr) {
785 guest_addr = slirp->vdhcp_startaddr;
786 }
787 if (is_udp) {
788 if (!udp_listen(slirp, host_addr.s_addr, htons(host_port),
789 guest_addr.s_addr, htons(guest_port), SS_HOSTFWD))
790 return -1;
791 } else {
792 if (!tcp_listen(slirp, host_addr.s_addr, htons(host_port),
793 guest_addr.s_addr, htons(guest_port), SS_HOSTFWD))
794 return -1;
795 }
796 return 0;
797 }
798
799 int slirp_add_exec(Slirp *slirp, int do_pty, const void *args,
800 struct in_addr *guest_addr, int guest_port)
801 {
802 if (!guest_addr->s_addr) {
803 guest_addr->s_addr = slirp->vnetwork_addr.s_addr |
804 (htonl(0x0204) & ~slirp->vnetwork_mask.s_addr);
805 }
806 if ((guest_addr->s_addr & slirp->vnetwork_mask.s_addr) !=
807 slirp->vnetwork_addr.s_addr ||
808 guest_addr->s_addr == slirp->vhost_addr.s_addr ||
809 guest_addr->s_addr == slirp->vnameserver_addr.s_addr) {
810 return -1;
811 }
812 return add_exec(&slirp->exec_list, do_pty, (char *)args, *guest_addr,
813 htons(guest_port));
814 }
815
816 ssize_t slirp_send(struct socket *so, const void *buf, size_t len, int flags)
817 {
818 if (so->s == -1 && so->extra) {
819 qemu_chr_fe_write(so->extra, buf, len);
820 return len;
821 }
822
823 return send(so->s, buf, len, flags);
824 }
825
826 static struct socket *
827 slirp_find_ctl_socket(Slirp *slirp, struct in_addr guest_addr, int guest_port)
828 {
829 struct socket *so;
830
831 for (so = slirp->tcb.so_next; so != &slirp->tcb; so = so->so_next) {
832 if (so->so_faddr.s_addr == guest_addr.s_addr &&
833 htons(so->so_fport) == guest_port) {
834 return so;
835 }
836 }
837 return NULL;
838 }
839
840 size_t slirp_socket_can_recv(Slirp *slirp, struct in_addr guest_addr,
841 int guest_port)
842 {
843 struct iovec iov[2];
844 struct socket *so;
845
846 so = slirp_find_ctl_socket(slirp, guest_addr, guest_port);
847
848 if (!so || so->so_state & SS_NOFDREF)
849 return 0;
850
851 if (!CONN_CANFRCV(so) || so->so_snd.sb_cc >= (so->so_snd.sb_datalen/2))
852 return 0;
853
854 return sopreprbuf(so, iov, NULL);
855 }
856
857 void slirp_socket_recv(Slirp *slirp, struct in_addr guest_addr, int guest_port,
858 const uint8_t *buf, int size)
859 {
860 int ret;
861 struct socket *so = slirp_find_ctl_socket(slirp, guest_addr, guest_port);
862
863 if (!so)
864 return;
865
866 ret = soreadbuf(so, (const char *)buf, size);
867
868 if (ret > 0)
869 tcp_output(sototcpcb(so));
870 }
871
872 static void slirp_tcp_save(QEMUFile *f, struct tcpcb *tp)
873 {
874 int i;
875
876 qemu_put_sbe16(f, tp->t_state);
877 for (i = 0; i < TCPT_NTIMERS; i++)
878 qemu_put_sbe16(f, tp->t_timer[i]);
879 qemu_put_sbe16(f, tp->t_rxtshift);
880 qemu_put_sbe16(f, tp->t_rxtcur);
881 qemu_put_sbe16(f, tp->t_dupacks);
882 qemu_put_be16(f, tp->t_maxseg);
883 qemu_put_sbyte(f, tp->t_force);
884 qemu_put_be16(f, tp->t_flags);
885 qemu_put_be32(f, tp->snd_una);
886 qemu_put_be32(f, tp->snd_nxt);
887 qemu_put_be32(f, tp->snd_up);
888 qemu_put_be32(f, tp->snd_wl1);
889 qemu_put_be32(f, tp->snd_wl2);
890 qemu_put_be32(f, tp->iss);
891 qemu_put_be32(f, tp->snd_wnd);
892 qemu_put_be32(f, tp->rcv_wnd);
893 qemu_put_be32(f, tp->rcv_nxt);
894 qemu_put_be32(f, tp->rcv_up);
895 qemu_put_be32(f, tp->irs);
896 qemu_put_be32(f, tp->rcv_adv);
897 qemu_put_be32(f, tp->snd_max);
898 qemu_put_be32(f, tp->snd_cwnd);
899 qemu_put_be32(f, tp->snd_ssthresh);
900 qemu_put_sbe16(f, tp->t_idle);
901 qemu_put_sbe16(f, tp->t_rtt);
902 qemu_put_be32(f, tp->t_rtseq);
903 qemu_put_sbe16(f, tp->t_srtt);
904 qemu_put_sbe16(f, tp->t_rttvar);
905 qemu_put_be16(f, tp->t_rttmin);
906 qemu_put_be32(f, tp->max_sndwnd);
907 qemu_put_byte(f, tp->t_oobflags);
908 qemu_put_byte(f, tp->t_iobc);
909 qemu_put_sbe16(f, tp->t_softerror);
910 qemu_put_byte(f, tp->snd_scale);
911 qemu_put_byte(f, tp->rcv_scale);
912 qemu_put_byte(f, tp->request_r_scale);
913 qemu_put_byte(f, tp->requested_s_scale);
914 qemu_put_be32(f, tp->ts_recent);
915 qemu_put_be32(f, tp->ts_recent_age);
916 qemu_put_be32(f, tp->last_ack_sent);
917 }
918
919 static void slirp_sbuf_save(QEMUFile *f, struct sbuf *sbuf)
920 {
921 uint32_t off;
922
923 qemu_put_be32(f, sbuf->sb_cc);
924 qemu_put_be32(f, sbuf->sb_datalen);
925 off = (uint32_t)(sbuf->sb_wptr - sbuf->sb_data);
926 qemu_put_sbe32(f, off);
927 off = (uint32_t)(sbuf->sb_rptr - sbuf->sb_data);
928 qemu_put_sbe32(f, off);
929 qemu_put_buffer(f, (unsigned char*)sbuf->sb_data, sbuf->sb_datalen);
930 }
931
932 static void slirp_socket_save(QEMUFile *f, struct socket *so)
933 {
934 qemu_put_be32(f, so->so_urgc);
935 qemu_put_be32(f, so->so_faddr.s_addr);
936 qemu_put_be32(f, so->so_laddr.s_addr);
937 qemu_put_be16(f, so->so_fport);
938 qemu_put_be16(f, so->so_lport);
939 qemu_put_byte(f, so->so_iptos);
940 qemu_put_byte(f, so->so_emu);
941 qemu_put_byte(f, so->so_type);
942 qemu_put_be32(f, so->so_state);
943 slirp_sbuf_save(f, &so->so_rcv);
944 slirp_sbuf_save(f, &so->so_snd);
945 slirp_tcp_save(f, so->so_tcpcb);
946 }
947
948 static void slirp_bootp_save(QEMUFile *f, Slirp *slirp)
949 {
950 int i;
951
952 for (i = 0; i < NB_BOOTP_CLIENTS; i++) {
953 qemu_put_be16(f, slirp->bootp_clients[i].allocated);
954 qemu_put_buffer(f, slirp->bootp_clients[i].macaddr, 6);
955 }
956 }
957
958 static void slirp_state_save(QEMUFile *f, void *opaque)
959 {
960 Slirp *slirp = opaque;
961 struct ex_list *ex_ptr;
962
963 for (ex_ptr = slirp->exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next)
964 if (ex_ptr->ex_pty == 3) {
965 struct socket *so;
966 so = slirp_find_ctl_socket(slirp, ex_ptr->ex_addr,
967 ntohs(ex_ptr->ex_fport));
968 if (!so)
969 continue;
970
971 qemu_put_byte(f, 42);
972 slirp_socket_save(f, so);
973 }
974 qemu_put_byte(f, 0);
975
976 qemu_put_be16(f, slirp->ip_id);
977
978 slirp_bootp_save(f, slirp);
979 }
980
981 static void slirp_tcp_load(QEMUFile *f, struct tcpcb *tp)
982 {
983 int i;
984
985 tp->t_state = qemu_get_sbe16(f);
986 for (i = 0; i < TCPT_NTIMERS; i++)
987 tp->t_timer[i] = qemu_get_sbe16(f);
988 tp->t_rxtshift = qemu_get_sbe16(f);
989 tp->t_rxtcur = qemu_get_sbe16(f);
990 tp->t_dupacks = qemu_get_sbe16(f);
991 tp->t_maxseg = qemu_get_be16(f);
992 tp->t_force = qemu_get_sbyte(f);
993 tp->t_flags = qemu_get_be16(f);
994 tp->snd_una = qemu_get_be32(f);
995 tp->snd_nxt = qemu_get_be32(f);
996 tp->snd_up = qemu_get_be32(f);
997 tp->snd_wl1 = qemu_get_be32(f);
998 tp->snd_wl2 = qemu_get_be32(f);
999 tp->iss = qemu_get_be32(f);
1000 tp->snd_wnd = qemu_get_be32(f);
1001 tp->rcv_wnd = qemu_get_be32(f);
1002 tp->rcv_nxt = qemu_get_be32(f);
1003 tp->rcv_up = qemu_get_be32(f);
1004 tp->irs = qemu_get_be32(f);
1005 tp->rcv_adv = qemu_get_be32(f);
1006 tp->snd_max = qemu_get_be32(f);
1007 tp->snd_cwnd = qemu_get_be32(f);
1008 tp->snd_ssthresh = qemu_get_be32(f);
1009 tp->t_idle = qemu_get_sbe16(f);
1010 tp->t_rtt = qemu_get_sbe16(f);
1011 tp->t_rtseq = qemu_get_be32(f);
1012 tp->t_srtt = qemu_get_sbe16(f);
1013 tp->t_rttvar = qemu_get_sbe16(f);
1014 tp->t_rttmin = qemu_get_be16(f);
1015 tp->max_sndwnd = qemu_get_be32(f);
1016 tp->t_oobflags = qemu_get_byte(f);
1017 tp->t_iobc = qemu_get_byte(f);
1018 tp->t_softerror = qemu_get_sbe16(f);
1019 tp->snd_scale = qemu_get_byte(f);
1020 tp->rcv_scale = qemu_get_byte(f);
1021 tp->request_r_scale = qemu_get_byte(f);
1022 tp->requested_s_scale = qemu_get_byte(f);
1023 tp->ts_recent = qemu_get_be32(f);
1024 tp->ts_recent_age = qemu_get_be32(f);
1025 tp->last_ack_sent = qemu_get_be32(f);
1026 tcp_template(tp);
1027 }
1028
1029 static int slirp_sbuf_load(QEMUFile *f, struct sbuf *sbuf)
1030 {
1031 uint32_t off, sb_cc, sb_datalen;
1032
1033 sb_cc = qemu_get_be32(f);
1034 sb_datalen = qemu_get_be32(f);
1035
1036 sbreserve(sbuf, sb_datalen);
1037
1038 if (sbuf->sb_datalen != sb_datalen)
1039 return -ENOMEM;
1040
1041 sbuf->sb_cc = sb_cc;
1042
1043 off = qemu_get_sbe32(f);
1044 sbuf->sb_wptr = sbuf->sb_data + off;
1045 off = qemu_get_sbe32(f);
1046 sbuf->sb_rptr = sbuf->sb_data + off;
1047 qemu_get_buffer(f, (unsigned char*)sbuf->sb_data, sbuf->sb_datalen);
1048
1049 return 0;
1050 }
1051
1052 static int slirp_socket_load(QEMUFile *f, struct socket *so)
1053 {
1054 if (tcp_attach(so) < 0)
1055 return -ENOMEM;
1056
1057 so->so_urgc = qemu_get_be32(f);
1058 so->so_faddr.s_addr = qemu_get_be32(f);
1059 so->so_laddr.s_addr = qemu_get_be32(f);
1060 so->so_fport = qemu_get_be16(f);
1061 so->so_lport = qemu_get_be16(f);
1062 so->so_iptos = qemu_get_byte(f);
1063 so->so_emu = qemu_get_byte(f);
1064 so->so_type = qemu_get_byte(f);
1065 so->so_state = qemu_get_be32(f);
1066 if (slirp_sbuf_load(f, &so->so_rcv) < 0)
1067 return -ENOMEM;
1068 if (slirp_sbuf_load(f, &so->so_snd) < 0)
1069 return -ENOMEM;
1070 slirp_tcp_load(f, so->so_tcpcb);
1071
1072 return 0;
1073 }
1074
1075 static void slirp_bootp_load(QEMUFile *f, Slirp *slirp)
1076 {
1077 int i;
1078
1079 for (i = 0; i < NB_BOOTP_CLIENTS; i++) {
1080 slirp->bootp_clients[i].allocated = qemu_get_be16(f);
1081 qemu_get_buffer(f, slirp->bootp_clients[i].macaddr, 6);
1082 }
1083 }
1084
1085 static int slirp_state_load(QEMUFile *f, void *opaque, int version_id)
1086 {
1087 Slirp *slirp = opaque;
1088 struct ex_list *ex_ptr;
1089
1090 while (qemu_get_byte(f)) {
1091 int ret;
1092 struct socket *so = socreate(slirp);
1093
1094 if (!so)
1095 return -ENOMEM;
1096
1097 ret = slirp_socket_load(f, so);
1098
1099 if (ret < 0)
1100 return ret;
1101
1102 if ((so->so_faddr.s_addr & slirp->vnetwork_mask.s_addr) !=
1103 slirp->vnetwork_addr.s_addr) {
1104 return -EINVAL;
1105 }
1106 for (ex_ptr = slirp->exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
1107 if (ex_ptr->ex_pty == 3 &&
1108 so->so_faddr.s_addr == ex_ptr->ex_addr.s_addr &&
1109 so->so_fport == ex_ptr->ex_fport) {
1110 break;
1111 }
1112 }
1113 if (!ex_ptr)
1114 return -EINVAL;
1115
1116 so->extra = (void *)ex_ptr->ex_exec;
1117 }
1118
1119 if (version_id >= 2) {
1120 slirp->ip_id = qemu_get_be16(f);
1121 }
1122
1123 if (version_id >= 3) {
1124 slirp_bootp_load(f, slirp);
1125 }
1126
1127 return 0;
1128 }