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