blob: a1f31391341d3315a8e201f689710b91ccd6aa15 [file] [log] [blame]
Markus Armbruster121d0712016-06-29 10:12:57 +02001#ifndef SLIRP_H
2#define SLIRP_H
bellardf0cbd3e2004-04-22 00:10:48 +00003
Paolo Bonzini87776ab2016-03-15 15:36:13 +01004#include "qemu/host-utils.h"
bellardf0cbd3e2004-04-22 00:10:48 +00005#include "slirp_config.h"
6
bellard379ff532004-07-12 22:33:07 +00007#ifdef _WIN32
bellard379ff532004-07-12 22:33:07 +00008
bellard379ff532004-07-12 22:33:07 +00009typedef char *caddr_t;
10
bellard34444132005-01-10 23:19:34 +000011# include <windows.h>
bellard379ff532004-07-12 22:33:07 +000012# include <winsock2.h>
balrog116842e2008-05-17 18:07:00 +000013# include <ws2tcpip.h>
bellard379ff532004-07-12 22:33:07 +000014# include <sys/timeb.h>
15# include <iphlpapi.h>
16
bellard379ff532004-07-12 22:33:07 +000017#else
Andreas Färber4a2b39d2010-09-20 00:50:48 +020018# if !defined(__HAIKU__)
19# define O_BINARY 0
20# endif
bellard379ff532004-07-12 22:33:07 +000021#endif
22
bellardf0cbd3e2004-04-22 00:10:48 +000023#ifdef HAVE_SYS_BITYPES_H
24# include <sys/bitypes.h>
25#endif
26
bellard379ff532004-07-12 22:33:07 +000027#ifndef _WIN32
bellardf0cbd3e2004-04-22 00:10:48 +000028#include <sys/uio.h>
bellard379ff532004-07-12 22:33:07 +000029#endif
bellardf0cbd3e2004-04-22 00:10:48 +000030
bellard379ff532004-07-12 22:33:07 +000031#ifndef _WIN32
bellardf0cbd3e2004-04-22 00:10:48 +000032#include <netinet/in.h>
33#include <arpa/inet.h>
bellard379ff532004-07-12 22:33:07 +000034#endif
bellardf0cbd3e2004-04-22 00:10:48 +000035
bellardf0cbd3e2004-04-22 00:10:48 +000036#ifndef NO_UNIX_SOCKETS
37#include <sys/un.h>
38#endif
bellardf0cbd3e2004-04-22 00:10:48 +000039#ifdef HAVE_SYS_SIGNAL_H
40# include <sys/signal.h>
41#endif
bellard379ff532004-07-12 22:33:07 +000042#ifndef _WIN32
bellardf0cbd3e2004-04-22 00:10:48 +000043#include <sys/socket.h>
bellard379ff532004-07-12 22:33:07 +000044#endif
bellardf0cbd3e2004-04-22 00:10:48 +000045
bellardee2654a2004-07-12 21:11:45 +000046#if defined(HAVE_SYS_IOCTL_H)
bellardf0cbd3e2004-04-22 00:10:48 +000047# include <sys/ioctl.h>
bellardf0cbd3e2004-04-22 00:10:48 +000048#endif
49
bellardf0cbd3e2004-04-22 00:10:48 +000050#ifdef HAVE_SYS_SELECT_H
51# include <sys/select.h>
52#endif
53
54#ifdef HAVE_SYS_WAIT_H
55# include <sys/wait.h>
56#endif
57
58#ifdef HAVE_SYS_FILIO_H
59# include <sys/filio.h>
60#endif
61
bellardf0cbd3e2004-04-22 00:10:48 +000062/* Avoid conflicting with the libc insque() and remque(), which
63 have different prototypes. */
64#define insque slirp_insque
65#define remque slirp_remque
Samuel Thibault67e3eee2016-02-22 22:29:21 +010066#define quehead slirp_quehead
bellardf0cbd3e2004-04-22 00:10:48 +000067
68#ifdef HAVE_SYS_STROPTS_H
69#include <sys/stropts.h>
70#endif
71
Guillaume Subiron0d6ff712016-03-15 10:31:19 +010072
bellardf0cbd3e2004-04-22 00:10:48 +000073#include "debug.h"
74
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010075#include "qemu/queue.h"
76#include "qemu/sockets.h"
Dr. David Alan Gilbert9c7ffe22016-01-08 14:41:28 +000077#include "net/eth.h"
Jan Kiszkab1c99fc2009-06-24 14:42:31 +020078
Jan Kiszka460fec62009-06-24 14:42:31 +020079#include "libslirp.h"
bellardf0cbd3e2004-04-22 00:10:48 +000080#include "ip.h"
Guillaume Subiron0d6ff712016-03-15 10:31:19 +010081#include "ip6.h"
bellardf0cbd3e2004-04-22 00:10:48 +000082#include "tcp.h"
83#include "tcp_timer.h"
84#include "tcp_var.h"
85#include "tcpip.h"
86#include "udp.h"
Jan Kiszkae6d43cf2011-07-20 12:20:18 +020087#include "ip_icmp.h"
Guillaume Subiron0d6ff712016-03-15 10:31:19 +010088#include "ip6_icmp.h"
bellardf0cbd3e2004-04-22 00:10:48 +000089#include "mbuf.h"
90#include "sbuf.h"
91#include "socket.h"
92#include "if.h"
93#include "main.h"
94#include "misc.h"
bellardf0cbd3e2004-04-22 00:10:48 +000095
96#include "bootp.h"
bellardc7f74642004-08-24 21:57:12 +000097#include "tftp.h"
Jan Kiszka460fec62009-06-24 14:42:31 +020098
Fabien Chouteau1a0ca1e2011-08-03 12:52:54 +020099#define ARPOP_REQUEST 1 /* ARP request */
100#define ARPOP_REPLY 2 /* ARP reply */
101
102struct ethhdr {
103 unsigned char h_dest[ETH_ALEN]; /* destination eth addr */
104 unsigned char h_source[ETH_ALEN]; /* source ether addr */
105 unsigned short h_proto; /* packet type ID field */
106};
107
Thomas Huth1f8b56e2016-08-15 10:24:54 +0200108struct slirp_arphdr {
Fabien Chouteau1a0ca1e2011-08-03 12:52:54 +0200109 unsigned short ar_hrd; /* format of hardware address */
110 unsigned short ar_pro; /* format of protocol address */
111 unsigned char ar_hln; /* length of hardware address */
112 unsigned char ar_pln; /* length of protocol address */
113 unsigned short ar_op; /* ARP opcode (command) */
114
115 /*
116 * Ethernet looks like this : This bit is variable sized however...
117 */
118 unsigned char ar_sha[ETH_ALEN]; /* sender hardware address */
119 uint32_t ar_sip; /* sender IP address */
120 unsigned char ar_tha[ETH_ALEN]; /* target hardware address */
121 uint32_t ar_tip; /* target IP address */
Stefan Weil541dc0d2011-08-31 12:38:01 +0200122} QEMU_PACKED;
Fabien Chouteau1a0ca1e2011-08-03 12:52:54 +0200123
124#define ARP_TABLE_SIZE 16
125
126typedef struct ArpTable {
Thomas Huth1f8b56e2016-08-15 10:24:54 +0200127 struct slirp_arphdr table[ARP_TABLE_SIZE];
Fabien Chouteau1a0ca1e2011-08-03 12:52:54 +0200128 int next_victim;
129} ArpTable;
130
Jan Kiszka5a371a22011-08-05 12:51:11 +0200131void arp_table_add(Slirp *slirp, uint32_t ip_addr, uint8_t ethaddr[ETH_ALEN]);
Fabien Chouteau1a0ca1e2011-08-03 12:52:54 +0200132
Jan Kiszka5a371a22011-08-05 12:51:11 +0200133bool arp_table_search(Slirp *slirp, uint32_t ip_addr,
Fabien Chouteau1a0ca1e2011-08-03 12:52:54 +0200134 uint8_t out_ethaddr[ETH_ALEN]);
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100135
Guillaume Subiron0d6ff712016-03-15 10:31:19 +0100136struct ndpentry {
137 unsigned char eth_addr[ETH_ALEN]; /* sender hardware address */
138 struct in6_addr ip_addr; /* sender IP address */
139} QEMU_PACKED;
140
141#define NDP_TABLE_SIZE 16
142
143typedef struct NdpTable {
144 struct ndpentry table[NDP_TABLE_SIZE];
145 int next_victim;
146} NdpTable;
147
148void ndp_table_add(Slirp *slirp, struct in6_addr ip_addr,
149 uint8_t ethaddr[ETH_ALEN]);
150bool ndp_table_search(Slirp *slirp, struct in6_addr ip_addr,
151 uint8_t out_ethaddr[ETH_ALEN]);
152
Jan Kiszka460fec62009-06-24 14:42:31 +0200153struct Slirp {
Blue Swirl72cf2d42009-09-12 07:36:22 +0000154 QTAILQ_ENTRY(Slirp) entry;
Liu Ping Fanfe0ff432013-08-25 10:01:19 +0800155 u_int time_fasttimo;
156 u_int last_slowtimo;
157 bool do_slowtimo;
Jan Kiszkab1c99fc2009-06-24 14:42:31 +0200158
Samuel Thibault0b11c032016-03-20 12:29:54 +0100159 bool in_enabled, in6_enabled;
160
Jan Kiszka460fec62009-06-24 14:42:31 +0200161 /* virtual network configuration */
162 struct in_addr vnetwork_addr;
163 struct in_addr vnetwork_mask;
164 struct in_addr vhost_addr;
Guillaume Subiron0d6ff712016-03-15 10:31:19 +0100165 struct in6_addr vprefix_addr6;
166 uint8_t vprefix_len;
167 struct in6_addr vhost_addr6;
Jan Kiszka460fec62009-06-24 14:42:31 +0200168 struct in_addr vdhcp_startaddr;
169 struct in_addr vnameserver_addr;
Guillaume Subiron05061d82016-03-15 10:31:22 +0100170 struct in6_addr vnameserver_addr6;
Jan Kiszka460fec62009-06-24 14:42:31 +0200171
Jan Kiszka460fec62009-06-24 14:42:31 +0200172 struct in_addr client_ipaddr;
173 char client_hostname[33];
174
175 int restricted;
Jan Kiszka460fec62009-06-24 14:42:31 +0200176 struct ex_list *exec_list;
177
178 /* mbuf states */
Samuel Thibault67e3eee2016-02-22 22:29:21 +0100179 struct quehead m_freelist;
180 struct quehead m_usedlist;
Jan Kiszka460fec62009-06-24 14:42:31 +0200181 int mbuf_alloced;
182
183 /* if states */
Samuel Thibault67e3eee2016-02-22 22:29:21 +0100184 struct quehead if_fastq; /* fast queue (for interactive data) */
185 struct quehead if_batchq; /* queue for non-interactive data */
Jan Kiszka460fec62009-06-24 14:42:31 +0200186 struct mbuf *next_m; /* pointer to next mbuf to output */
Jan Kiszka953e7f52012-03-05 19:50:39 +0100187 bool if_start_busy; /* avoid if_start recursion */
Jan Kiszka460fec62009-06-24 14:42:31 +0200188
189 /* ip states */
190 struct ipq ipq; /* ip reass. queue */
Stefan Weilb6dce922010-07-22 22:15:23 +0200191 uint16_t ip_id; /* ip packet ctr, for ids */
Jan Kiszka460fec62009-06-24 14:42:31 +0200192
193 /* bootp/dhcp states */
194 BOOTPClient bootp_clients[NB_BOOTP_CLIENTS];
195 char *bootp_filename;
Klaus Stengel63d29602012-10-27 19:53:39 +0200196 size_t vdnssearch_len;
197 uint8_t *vdnssearch;
Jan Kiszka460fec62009-06-24 14:42:31 +0200198
199 /* tcp states */
200 struct socket tcb;
201 struct socket *tcp_last_so;
202 tcp_seq tcp_iss; /* tcp initial send seq # */
Stefan Weilb6dce922010-07-22 22:15:23 +0200203 uint32_t tcp_now; /* for RFC 1323 timestamps */
Jan Kiszka460fec62009-06-24 14:42:31 +0200204
205 /* udp states */
206 struct socket udb;
207 struct socket *udp_last_so;
208
Jan Kiszkae6d43cf2011-07-20 12:20:18 +0200209 /* icmp states */
210 struct socket icmp;
211 struct socket *icmp_last_so;
212
Jan Kiszka460fec62009-06-24 14:42:31 +0200213 /* tftp states */
214 char *tftp_prefix;
215 struct tftp_session tftp_sessions[TFTP_SESSIONS_MAX];
216
Fabien Chouteau1a0ca1e2011-08-03 12:52:54 +0200217 ArpTable arp_table;
Guillaume Subiron0d6ff712016-03-15 10:31:19 +0100218 NdpTable ndp_table;
219
220 GRand *grand;
221 QEMUTimer *ra_timer;
Fabien Chouteau1a0ca1e2011-08-03 12:52:54 +0200222
Jan Kiszka9f8bd042009-06-24 14:42:31 +0200223 void *opaque;
Jan Kiszka460fec62009-06-24 14:42:31 +0200224};
225
Jan Kiszkaad0d8c42009-06-24 14:42:31 +0200226extern Slirp *slirp_instance;
bellardf0cbd3e2004-04-22 00:10:48 +0000227
bellardf0cbd3e2004-04-22 00:10:48 +0000228#ifndef NULL
229#define NULL (void *)0
230#endif
231
Blue Swirl6cb9c6d2009-07-01 19:11:17 +0000232void if_start(Slirp *);
bellardf0cbd3e2004-04-22 00:10:48 +0000233
bellard379ff532004-07-12 22:33:07 +0000234#ifndef _WIN32
bellardf0cbd3e2004-04-22 00:10:48 +0000235#include <netdb.h>
bellard379ff532004-07-12 22:33:07 +0000236#endif
bellardf0cbd3e2004-04-22 00:10:48 +0000237
blueswir19634d902007-10-26 19:01:16 +0000238#define SO_OPTIONS DO_KEEPALIVE
239#define TCP_MAXIDLE (TCPTV_KEEPCNT * TCPTV_KEEPINTVL)
240
Klaus Stengel63d29602012-10-27 19:53:39 +0200241/* dnssearch.c */
242int translate_dnssearch(Slirp *s, const char ** names);
243
bellardf0cbd3e2004-04-22 00:10:48 +0000244/* cksum.c */
245int cksum(struct mbuf *m, int len);
Guillaume Subiron0d6ff712016-03-15 10:31:19 +0100246int ip6_cksum(struct mbuf *m);
bellardf0cbd3e2004-04-22 00:10:48 +0000247
248/* if.c */
Blue Swirl6cb9c6d2009-07-01 19:11:17 +0000249void if_init(Slirp *);
250void if_output(struct socket *, struct mbuf *);
bellardf0cbd3e2004-04-22 00:10:48 +0000251
252/* ip_input.c */
Blue Swirl6cb9c6d2009-07-01 19:11:17 +0000253void ip_init(Slirp *);
Jan Kiszkaa68adc22012-02-29 19:14:23 +0100254void ip_cleanup(Slirp *);
Blue Swirl6cb9c6d2009-07-01 19:11:17 +0000255void ip_input(struct mbuf *);
256void ip_slowtimo(Slirp *);
257void ip_stripoptions(register struct mbuf *, struct mbuf *);
bellardf0cbd3e2004-04-22 00:10:48 +0000258
259/* ip_output.c */
Blue Swirl6cb9c6d2009-07-01 19:11:17 +0000260int ip_output(struct socket *, struct mbuf *);
bellardf0cbd3e2004-04-22 00:10:48 +0000261
Guillaume Subiron0d6ff712016-03-15 10:31:19 +0100262/* ip6_input.c */
263void ip6_init(Slirp *);
264void ip6_cleanup(Slirp *);
265void ip6_input(struct mbuf *);
266
267/* ip6_output */
268int ip6_output(struct socket *, struct mbuf *, int fast);
269
bellardf0cbd3e2004-04-22 00:10:48 +0000270/* tcp_input.c */
Guillaume Subiron9dfbf252016-03-15 10:31:21 +0100271void tcp_input(register struct mbuf *, int, struct socket *, unsigned short af);
Blue Swirl6cb9c6d2009-07-01 19:11:17 +0000272int tcp_mss(register struct tcpcb *, u_int);
bellardf0cbd3e2004-04-22 00:10:48 +0000273
274/* tcp_output.c */
Blue Swirl6cb9c6d2009-07-01 19:11:17 +0000275int tcp_output(register struct tcpcb *);
276void tcp_setpersist(register struct tcpcb *);
bellardf0cbd3e2004-04-22 00:10:48 +0000277
278/* tcp_subr.c */
Blue Swirl6cb9c6d2009-07-01 19:11:17 +0000279void tcp_init(Slirp *);
Jan Kiszkaa68adc22012-02-29 19:14:23 +0100280void tcp_cleanup(Slirp *);
Blue Swirl6cb9c6d2009-07-01 19:11:17 +0000281void tcp_template(struct tcpcb *);
Guillaume Subiron9dfbf252016-03-15 10:31:21 +0100282void tcp_respond(struct tcpcb *, register struct tcpiphdr *,
283 register struct mbuf *, tcp_seq, tcp_seq, int, unsigned short);
Blue Swirl6cb9c6d2009-07-01 19:11:17 +0000284struct tcpcb * tcp_newtcpcb(struct socket *);
285struct tcpcb * tcp_close(register struct tcpcb *);
286void tcp_sockclosed(struct tcpcb *);
Guillaume Subironcc573a62015-12-19 22:25:03 +0100287int tcp_fconnect(struct socket *, unsigned short af);
Blue Swirl6cb9c6d2009-07-01 19:11:17 +0000288void tcp_connect(struct socket *);
289int tcp_attach(struct socket *);
Stefan Weilb6dce922010-07-22 22:15:23 +0200290uint8_t tcp_tos(struct socket *);
Blue Swirl6cb9c6d2009-07-01 19:11:17 +0000291int tcp_emu(struct socket *, struct mbuf *);
292int tcp_ctl(struct socket *);
bellard9fafc9e2004-04-26 19:50:09 +0000293struct tcpcb *tcp_drop(struct tcpcb *tp, int err);
bellardf0cbd3e2004-04-22 00:10:48 +0000294
bellard379ff532004-07-12 22:33:07 +0000295#ifndef _WIN32
296#define min(x,y) ((x) < (y) ? (x) : (y))
297#define max(x,y) ((x) > (y) ? (x) : (y))
298#endif
299
bellardf0cbd3e2004-04-22 00:10:48 +0000300#endif