blob: b80725a0d6d1c9d0bb54978f638c0b89d966f8a1 [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
bellardf0cbd3e2004-04-22 00:10:48 +00004#include "slirp_config.h"
5
bellard379ff532004-07-12 22:33:07 +00006#ifdef _WIN32
bellard379ff532004-07-12 22:33:07 +00007
bellard379ff532004-07-12 22:33:07 +00008typedef char *caddr_t;
9
bellard34444132005-01-10 23:19:34 +000010# include <windows.h>
bellard379ff532004-07-12 22:33:07 +000011# include <winsock2.h>
balrog116842e2008-05-17 18:07:00 +000012# include <ws2tcpip.h>
bellard379ff532004-07-12 22:33:07 +000013# include <sys/timeb.h>
14# include <iphlpapi.h>
15
bellard379ff532004-07-12 22:33:07 +000016#else
Andreas Färber4a2b39d2010-09-20 00:50:48 +020017# if !defined(__HAIKU__)
18# define O_BINARY 0
19# endif
bellard379ff532004-07-12 22:33:07 +000020#endif
21
bellardf0cbd3e2004-04-22 00:10:48 +000022#ifdef HAVE_SYS_BITYPES_H
23# include <sys/bitypes.h>
24#endif
25
bellard379ff532004-07-12 22:33:07 +000026#ifndef _WIN32
bellardf0cbd3e2004-04-22 00:10:48 +000027#include <sys/uio.h>
bellard379ff532004-07-12 22:33:07 +000028#endif
bellardf0cbd3e2004-04-22 00:10:48 +000029
bellard379ff532004-07-12 22:33:07 +000030#ifndef _WIN32
bellardf0cbd3e2004-04-22 00:10:48 +000031#include <netinet/in.h>
32#include <arpa/inet.h>
bellard379ff532004-07-12 22:33:07 +000033#endif
bellardf0cbd3e2004-04-22 00:10:48 +000034
bellardf0cbd3e2004-04-22 00:10:48 +000035#ifndef NO_UNIX_SOCKETS
36#include <sys/un.h>
37#endif
bellardf0cbd3e2004-04-22 00:10:48 +000038#ifdef HAVE_SYS_SIGNAL_H
39# include <sys/signal.h>
40#endif
bellard379ff532004-07-12 22:33:07 +000041#ifndef _WIN32
bellardf0cbd3e2004-04-22 00:10:48 +000042#include <sys/socket.h>
bellard379ff532004-07-12 22:33:07 +000043#endif
bellardf0cbd3e2004-04-22 00:10:48 +000044
bellardee2654a2004-07-12 21:11:45 +000045#if defined(HAVE_SYS_IOCTL_H)
bellardf0cbd3e2004-04-22 00:10:48 +000046# include <sys/ioctl.h>
bellardf0cbd3e2004-04-22 00:10:48 +000047#endif
48
bellardf0cbd3e2004-04-22 00:10:48 +000049#ifdef HAVE_SYS_SELECT_H
50# include <sys/select.h>
51#endif
52
53#ifdef HAVE_SYS_WAIT_H
54# include <sys/wait.h>
55#endif
56
57#ifdef HAVE_SYS_FILIO_H
58# include <sys/filio.h>
59#endif
60
bellardf0cbd3e2004-04-22 00:10:48 +000061/* Avoid conflicting with the libc insque() and remque(), which
62 have different prototypes. */
63#define insque slirp_insque
64#define remque slirp_remque
Samuel Thibault67e3eee2016-02-22 22:29:21 +010065#define quehead slirp_quehead
bellardf0cbd3e2004-04-22 00:10:48 +000066
67#ifdef HAVE_SYS_STROPTS_H
68#include <sys/stropts.h>
69#endif
70
Guillaume Subiron0d6ff712016-03-15 10:31:19 +010071
bellardf0cbd3e2004-04-22 00:10:48 +000072#include "debug.h"
73
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010074#include "qemu/queue.h"
75#include "qemu/sockets.h"
Dr. David Alan Gilbert9c7ffe22016-01-08 14:41:28 +000076#include "net/eth.h"
Jan Kiszkab1c99fc2009-06-24 14:42:31 +020077
Jan Kiszka460fec62009-06-24 14:42:31 +020078#include "libslirp.h"
bellardf0cbd3e2004-04-22 00:10:48 +000079#include "ip.h"
Guillaume Subiron0d6ff712016-03-15 10:31:19 +010080#include "ip6.h"
bellardf0cbd3e2004-04-22 00:10:48 +000081#include "tcp.h"
82#include "tcp_timer.h"
83#include "tcp_var.h"
84#include "tcpip.h"
85#include "udp.h"
Jan Kiszkae6d43cf2011-07-20 12:20:18 +020086#include "ip_icmp.h"
Guillaume Subiron0d6ff712016-03-15 10:31:19 +010087#include "ip6_icmp.h"
bellardf0cbd3e2004-04-22 00:10:48 +000088#include "mbuf.h"
89#include "sbuf.h"
90#include "socket.h"
91#include "if.h"
92#include "main.h"
93#include "misc.h"
bellardf0cbd3e2004-04-22 00:10:48 +000094
95#include "bootp.h"
bellardc7f74642004-08-24 21:57:12 +000096#include "tftp.h"
Jan Kiszka460fec62009-06-24 14:42:31 +020097
Fabien Chouteau1a0ca1e2011-08-03 12:52:54 +020098#define ARPOP_REQUEST 1 /* ARP request */
99#define ARPOP_REPLY 2 /* ARP reply */
100
101struct ethhdr {
102 unsigned char h_dest[ETH_ALEN]; /* destination eth addr */
103 unsigned char h_source[ETH_ALEN]; /* source ether addr */
104 unsigned short h_proto; /* packet type ID field */
105};
106
Thomas Huth1f8b56e2016-08-15 10:24:54 +0200107struct slirp_arphdr {
Fabien Chouteau1a0ca1e2011-08-03 12:52:54 +0200108 unsigned short ar_hrd; /* format of hardware address */
109 unsigned short ar_pro; /* format of protocol address */
110 unsigned char ar_hln; /* length of hardware address */
111 unsigned char ar_pln; /* length of protocol address */
112 unsigned short ar_op; /* ARP opcode (command) */
113
114 /*
115 * Ethernet looks like this : This bit is variable sized however...
116 */
117 unsigned char ar_sha[ETH_ALEN]; /* sender hardware address */
118 uint32_t ar_sip; /* sender IP address */
119 unsigned char ar_tha[ETH_ALEN]; /* target hardware address */
120 uint32_t ar_tip; /* target IP address */
Stefan Weil541dc0d2011-08-31 12:38:01 +0200121} QEMU_PACKED;
Fabien Chouteau1a0ca1e2011-08-03 12:52:54 +0200122
123#define ARP_TABLE_SIZE 16
124
125typedef struct ArpTable {
Thomas Huth1f8b56e2016-08-15 10:24:54 +0200126 struct slirp_arphdr table[ARP_TABLE_SIZE];
Fabien Chouteau1a0ca1e2011-08-03 12:52:54 +0200127 int next_victim;
128} ArpTable;
129
Jan Kiszka5a371a22011-08-05 12:51:11 +0200130void arp_table_add(Slirp *slirp, uint32_t ip_addr, uint8_t ethaddr[ETH_ALEN]);
Fabien Chouteau1a0ca1e2011-08-03 12:52:54 +0200131
Jan Kiszka5a371a22011-08-05 12:51:11 +0200132bool arp_table_search(Slirp *slirp, uint32_t ip_addr,
Fabien Chouteau1a0ca1e2011-08-03 12:52:54 +0200133 uint8_t out_ethaddr[ETH_ALEN]);
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100134
Guillaume Subiron0d6ff712016-03-15 10:31:19 +0100135struct ndpentry {
136 unsigned char eth_addr[ETH_ALEN]; /* sender hardware address */
137 struct in6_addr ip_addr; /* sender IP address */
138} QEMU_PACKED;
139
140#define NDP_TABLE_SIZE 16
141
142typedef struct NdpTable {
143 struct ndpentry table[NDP_TABLE_SIZE];
144 int next_victim;
145} NdpTable;
146
147void ndp_table_add(Slirp *slirp, struct in6_addr ip_addr,
148 uint8_t ethaddr[ETH_ALEN]);
149bool ndp_table_search(Slirp *slirp, struct in6_addr ip_addr,
150 uint8_t out_ethaddr[ETH_ALEN]);
151
Jan Kiszka460fec62009-06-24 14:42:31 +0200152struct Slirp {
Blue Swirl72cf2d42009-09-12 07:36:22 +0000153 QTAILQ_ENTRY(Slirp) entry;
Liu Ping Fanfe0ff432013-08-25 10:01:19 +0800154 u_int time_fasttimo;
155 u_int last_slowtimo;
156 bool do_slowtimo;
Jan Kiszkab1c99fc2009-06-24 14:42:31 +0200157
Samuel Thibault0b11c032016-03-20 12:29:54 +0100158 bool in_enabled, in6_enabled;
159
Jan Kiszka460fec62009-06-24 14:42:31 +0200160 /* virtual network configuration */
161 struct in_addr vnetwork_addr;
162 struct in_addr vnetwork_mask;
163 struct in_addr vhost_addr;
Guillaume Subiron0d6ff712016-03-15 10:31:19 +0100164 struct in6_addr vprefix_addr6;
165 uint8_t vprefix_len;
166 struct in6_addr vhost_addr6;
Jan Kiszka460fec62009-06-24 14:42:31 +0200167 struct in_addr vdhcp_startaddr;
168 struct in_addr vnameserver_addr;
Guillaume Subiron05061d82016-03-15 10:31:22 +0100169 struct in6_addr vnameserver_addr6;
Jan Kiszka460fec62009-06-24 14:42:31 +0200170
Jan Kiszka460fec62009-06-24 14:42:31 +0200171 struct in_addr client_ipaddr;
172 char client_hostname[33];
173
174 int restricted;
Jan Kiszka460fec62009-06-24 14:42:31 +0200175 struct ex_list *exec_list;
176
177 /* mbuf states */
Samuel Thibault67e3eee2016-02-22 22:29:21 +0100178 struct quehead m_freelist;
179 struct quehead m_usedlist;
Jan Kiszka460fec62009-06-24 14:42:31 +0200180 int mbuf_alloced;
181
182 /* if states */
Samuel Thibault67e3eee2016-02-22 22:29:21 +0100183 struct quehead if_fastq; /* fast queue (for interactive data) */
184 struct quehead if_batchq; /* queue for non-interactive data */
Jan Kiszka953e7f52012-03-05 19:50:39 +0100185 bool if_start_busy; /* avoid if_start recursion */
Jan Kiszka460fec62009-06-24 14:42:31 +0200186
187 /* ip states */
188 struct ipq ipq; /* ip reass. queue */
Stefan Weilb6dce922010-07-22 22:15:23 +0200189 uint16_t ip_id; /* ip packet ctr, for ids */
Jan Kiszka460fec62009-06-24 14:42:31 +0200190
191 /* bootp/dhcp states */
192 BOOTPClient bootp_clients[NB_BOOTP_CLIENTS];
193 char *bootp_filename;
Klaus Stengel63d29602012-10-27 19:53:39 +0200194 size_t vdnssearch_len;
195 uint8_t *vdnssearch;
Benjamin Drungf18d1372018-02-27 17:06:01 +0100196 char *vdomainname;
Jan Kiszka460fec62009-06-24 14:42:31 +0200197
198 /* tcp states */
199 struct socket tcb;
200 struct socket *tcp_last_so;
201 tcp_seq tcp_iss; /* tcp initial send seq # */
Stefan Weilb6dce922010-07-22 22:15:23 +0200202 uint32_t tcp_now; /* for RFC 1323 timestamps */
Jan Kiszka460fec62009-06-24 14:42:31 +0200203
204 /* udp states */
205 struct socket udb;
206 struct socket *udp_last_so;
207
Jan Kiszkae6d43cf2011-07-20 12:20:18 +0200208 /* icmp states */
209 struct socket icmp;
210 struct socket *icmp_last_so;
211
Jan Kiszka460fec62009-06-24 14:42:31 +0200212 /* tftp states */
213 char *tftp_prefix;
214 struct tftp_session tftp_sessions[TFTP_SESSIONS_MAX];
Fam Zheng0fca92b2018-09-14 15:26:16 +0800215 char *tftp_server_name;
Jan Kiszka460fec62009-06-24 14:42:31 +0200216
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
Cédric Le Goater47bb83c2017-04-14 10:35:03 +0200234/* ncsi.c */
235void ncsi_input(Slirp *slirp, const uint8_t *pkt, int pkt_len);
236
bellard379ff532004-07-12 22:33:07 +0000237#ifndef _WIN32
bellardf0cbd3e2004-04-22 00:10:48 +0000238#include <netdb.h>
bellard379ff532004-07-12 22:33:07 +0000239#endif
bellardf0cbd3e2004-04-22 00:10:48 +0000240
blueswir19634d902007-10-26 19:01:16 +0000241#define SO_OPTIONS DO_KEEPALIVE
242#define TCP_MAXIDLE (TCPTV_KEEPCNT * TCPTV_KEEPINTVL)
243
Klaus Stengel63d29602012-10-27 19:53:39 +0200244/* dnssearch.c */
245int translate_dnssearch(Slirp *s, const char ** names);
246
bellardf0cbd3e2004-04-22 00:10:48 +0000247/* cksum.c */
248int cksum(struct mbuf *m, int len);
Guillaume Subiron0d6ff712016-03-15 10:31:19 +0100249int ip6_cksum(struct mbuf *m);
bellardf0cbd3e2004-04-22 00:10:48 +0000250
251/* if.c */
Blue Swirl6cb9c6d2009-07-01 19:11:17 +0000252void if_init(Slirp *);
253void if_output(struct socket *, struct mbuf *);
bellardf0cbd3e2004-04-22 00:10:48 +0000254
255/* ip_input.c */
Blue Swirl6cb9c6d2009-07-01 19:11:17 +0000256void ip_init(Slirp *);
Jan Kiszkaa68adc22012-02-29 19:14:23 +0100257void ip_cleanup(Slirp *);
Blue Swirl6cb9c6d2009-07-01 19:11:17 +0000258void ip_input(struct mbuf *);
259void ip_slowtimo(Slirp *);
260void ip_stripoptions(register struct mbuf *, struct mbuf *);
bellardf0cbd3e2004-04-22 00:10:48 +0000261
262/* ip_output.c */
Blue Swirl6cb9c6d2009-07-01 19:11:17 +0000263int ip_output(struct socket *, struct mbuf *);
bellardf0cbd3e2004-04-22 00:10:48 +0000264
Guillaume Subiron0d6ff712016-03-15 10:31:19 +0100265/* ip6_input.c */
266void ip6_init(Slirp *);
267void ip6_cleanup(Slirp *);
268void ip6_input(struct mbuf *);
269
270/* ip6_output */
271int ip6_output(struct socket *, struct mbuf *, int fast);
272
bellardf0cbd3e2004-04-22 00:10:48 +0000273/* tcp_input.c */
Guillaume Subiron9dfbf252016-03-15 10:31:21 +0100274void tcp_input(register struct mbuf *, int, struct socket *, unsigned short af);
Blue Swirl6cb9c6d2009-07-01 19:11:17 +0000275int tcp_mss(register struct tcpcb *, u_int);
bellardf0cbd3e2004-04-22 00:10:48 +0000276
277/* tcp_output.c */
Blue Swirl6cb9c6d2009-07-01 19:11:17 +0000278int tcp_output(register struct tcpcb *);
279void tcp_setpersist(register struct tcpcb *);
bellardf0cbd3e2004-04-22 00:10:48 +0000280
281/* tcp_subr.c */
Blue Swirl6cb9c6d2009-07-01 19:11:17 +0000282void tcp_init(Slirp *);
Jan Kiszkaa68adc22012-02-29 19:14:23 +0100283void tcp_cleanup(Slirp *);
Blue Swirl6cb9c6d2009-07-01 19:11:17 +0000284void tcp_template(struct tcpcb *);
Guillaume Subiron9dfbf252016-03-15 10:31:21 +0100285void tcp_respond(struct tcpcb *, register struct tcpiphdr *,
286 register struct mbuf *, tcp_seq, tcp_seq, int, unsigned short);
Blue Swirl6cb9c6d2009-07-01 19:11:17 +0000287struct tcpcb * tcp_newtcpcb(struct socket *);
288struct tcpcb * tcp_close(register struct tcpcb *);
289void tcp_sockclosed(struct tcpcb *);
Guillaume Subironcc573a62015-12-19 22:25:03 +0100290int tcp_fconnect(struct socket *, unsigned short af);
Blue Swirl6cb9c6d2009-07-01 19:11:17 +0000291void tcp_connect(struct socket *);
292int tcp_attach(struct socket *);
Stefan Weilb6dce922010-07-22 22:15:23 +0200293uint8_t tcp_tos(struct socket *);
Blue Swirl6cb9c6d2009-07-01 19:11:17 +0000294int tcp_emu(struct socket *, struct mbuf *);
295int tcp_ctl(struct socket *);
bellard9fafc9e2004-04-26 19:50:09 +0000296struct tcpcb *tcp_drop(struct tcpcb *tp, int err);
bellardf0cbd3e2004-04-22 00:10:48 +0000297
bellardf0cbd3e2004-04-22 00:10:48 +0000298#endif