]> git.proxmox.com Git - mirror_qemu.git/blame - slirp/slirp.h
slirp: avoid use-after-free in slirp_pollfds_poll() if soread() returns an error
[mirror_qemu.git] / slirp / slirp.h
CommitLineData
f0cbd3ec
FB
1#ifndef __COMMON_H__
2#define __COMMON_H__
3
f0cbd3ec
FB
4#include "slirp_config.h"
5
379ff53d 6#ifdef _WIN32
379ff53d 7
379ff53d
FB
8typedef char *caddr_t;
9
34444131 10# include <windows.h>
379ff53d 11# include <winsock2.h>
116842ee 12# include <ws2tcpip.h>
379ff53d
FB
13# include <sys/timeb.h>
14# include <iphlpapi.h>
15
379ff53d 16#else
4a2b39d3
AF
17# if !defined(__HAIKU__)
18# define O_BINARY 0
19# endif
379ff53d
FB
20#endif
21
f0cbd3ec
FB
22#ifdef HAVE_SYS_BITYPES_H
23# include <sys/bitypes.h>
24#endif
25
170c6f87 26
f0cbd3ec
FB
27#ifndef HAVE_MEMMOVE
28#define memmove(x, y, z) bcopy(y, x, z)
29#endif
30
379ff53d 31#ifndef _WIN32
f0cbd3ec 32#include <sys/uio.h>
379ff53d 33#endif
f0cbd3ec 34
379ff53d 35#ifndef _WIN32
f0cbd3ec
FB
36#include <netinet/in.h>
37#include <arpa/inet.h>
379ff53d 38#endif
f0cbd3ec 39
f0cbd3ec
FB
40/* Systems lacking strdup() definition in <string.h>. */
41#if defined(ultrix)
6cb9c6d3 42char *strdup(const char *);
f0cbd3ec
FB
43#endif
44
45/* Systems lacking malloc() definition in <stdlib.h>. */
46#if defined(ultrix) || defined(hcx)
6cb9c6d3
BS
47void *malloc(size_t arg);
48void free(void *ptr);
f0cbd3ec
FB
49#endif
50
f0cbd3ec
FB
51#ifndef NO_UNIX_SOCKETS
52#include <sys/un.h>
53#endif
f0cbd3ec
FB
54#ifdef HAVE_SYS_SIGNAL_H
55# include <sys/signal.h>
56#endif
379ff53d 57#ifndef _WIN32
f0cbd3ec 58#include <sys/socket.h>
379ff53d 59#endif
f0cbd3ec 60
ee2654ac 61#if defined(HAVE_SYS_IOCTL_H)
f0cbd3ec 62# include <sys/ioctl.h>
f0cbd3ec
FB
63#endif
64
f0cbd3ec
FB
65#ifdef HAVE_SYS_SELECT_H
66# include <sys/select.h>
67#endif
68
69#ifdef HAVE_SYS_WAIT_H
70# include <sys/wait.h>
71#endif
72
73#ifdef HAVE_SYS_FILIO_H
74# include <sys/filio.h>
75#endif
76
77#ifdef USE_PPP
78#include <ppp/slirppp.h>
79#endif
80
f0cbd3ec
FB
81/* Avoid conflicting with the libc insque() and remque(), which
82 have different prototypes. */
83#define insque slirp_insque
84#define remque slirp_remque
67e3eee4 85#define quehead slirp_quehead
f0cbd3ec
FB
86
87#ifdef HAVE_SYS_STROPTS_H
88#include <sys/stropts.h>
89#endif
90
0d6ff71a
GS
91#include <glib.h>
92
f0cbd3ec
FB
93#include "debug.h"
94
1de7afc9
PB
95#include "qemu/queue.h"
96#include "qemu/sockets.h"
9c7ffe26 97#include "net/eth.h"
b1c99fcd 98
460fec67 99#include "libslirp.h"
f0cbd3ec 100#include "ip.h"
0d6ff71a 101#include "ip6.h"
f0cbd3ec
FB
102#include "tcp.h"
103#include "tcp_timer.h"
104#include "tcp_var.h"
105#include "tcpip.h"
106#include "udp.h"
e6d43cfb 107#include "ip_icmp.h"
0d6ff71a 108#include "ip6_icmp.h"
f0cbd3ec
FB
109#include "mbuf.h"
110#include "sbuf.h"
111#include "socket.h"
112#include "if.h"
113#include "main.h"
114#include "misc.h"
f0cbd3ec
FB
115#ifdef USE_PPP
116#include "ppp/pppd.h"
117#include "ppp/ppp.h"
118#endif
119
120#include "bootp.h"
c7f74643 121#include "tftp.h"
460fec67 122
1a0ca1e1
FC
123#define ARPOP_REQUEST 1 /* ARP request */
124#define ARPOP_REPLY 2 /* ARP reply */
125
126struct ethhdr {
127 unsigned char h_dest[ETH_ALEN]; /* destination eth addr */
128 unsigned char h_source[ETH_ALEN]; /* source ether addr */
129 unsigned short h_proto; /* packet type ID field */
130};
131
132struct arphdr {
133 unsigned short ar_hrd; /* format of hardware address */
134 unsigned short ar_pro; /* format of protocol address */
135 unsigned char ar_hln; /* length of hardware address */
136 unsigned char ar_pln; /* length of protocol address */
137 unsigned short ar_op; /* ARP opcode (command) */
138
139 /*
140 * Ethernet looks like this : This bit is variable sized however...
141 */
142 unsigned char ar_sha[ETH_ALEN]; /* sender hardware address */
143 uint32_t ar_sip; /* sender IP address */
144 unsigned char ar_tha[ETH_ALEN]; /* target hardware address */
145 uint32_t ar_tip; /* target IP address */
541dc0d4 146} QEMU_PACKED;
1a0ca1e1
FC
147
148#define ARP_TABLE_SIZE 16
149
150typedef struct ArpTable {
151 struct arphdr table[ARP_TABLE_SIZE];
152 int next_victim;
153} ArpTable;
154
5a371a2e 155void arp_table_add(Slirp *slirp, uint32_t ip_addr, uint8_t ethaddr[ETH_ALEN]);
1a0ca1e1 156
5a371a2e 157bool arp_table_search(Slirp *slirp, uint32_t ip_addr,
1a0ca1e1 158 uint8_t out_ethaddr[ETH_ALEN]);
40ff6d7e 159
0d6ff71a
GS
160struct ndpentry {
161 unsigned char eth_addr[ETH_ALEN]; /* sender hardware address */
162 struct in6_addr ip_addr; /* sender IP address */
163} QEMU_PACKED;
164
165#define NDP_TABLE_SIZE 16
166
167typedef struct NdpTable {
168 struct ndpentry table[NDP_TABLE_SIZE];
169 int next_victim;
170} NdpTable;
171
172void ndp_table_add(Slirp *slirp, struct in6_addr ip_addr,
173 uint8_t ethaddr[ETH_ALEN]);
174bool ndp_table_search(Slirp *slirp, struct in6_addr ip_addr,
175 uint8_t out_ethaddr[ETH_ALEN]);
176
460fec67 177struct Slirp {
72cf2d4f 178 QTAILQ_ENTRY(Slirp) entry;
fe0ff43c
LPF
179 u_int time_fasttimo;
180 u_int last_slowtimo;
181 bool do_slowtimo;
b1c99fcd 182
0b11c036
ST
183 bool in_enabled, in6_enabled;
184
460fec67
JK
185 /* virtual network configuration */
186 struct in_addr vnetwork_addr;
187 struct in_addr vnetwork_mask;
188 struct in_addr vhost_addr;
0d6ff71a
GS
189 struct in6_addr vprefix_addr6;
190 uint8_t vprefix_len;
191 struct in6_addr vhost_addr6;
460fec67
JK
192 struct in_addr vdhcp_startaddr;
193 struct in_addr vnameserver_addr;
05061d85 194 struct in6_addr vnameserver_addr6;
460fec67 195
460fec67
JK
196 struct in_addr client_ipaddr;
197 char client_hostname[33];
198
199 int restricted;
460fec67
JK
200 struct ex_list *exec_list;
201
202 /* mbuf states */
67e3eee4
ST
203 struct quehead m_freelist;
204 struct quehead m_usedlist;
460fec67
JK
205 int mbuf_alloced;
206
207 /* if states */
67e3eee4
ST
208 struct quehead if_fastq; /* fast queue (for interactive data) */
209 struct quehead if_batchq; /* queue for non-interactive data */
460fec67 210 struct mbuf *next_m; /* pointer to next mbuf to output */
953e7f54 211 bool if_start_busy; /* avoid if_start recursion */
460fec67
JK
212
213 /* ip states */
214 struct ipq ipq; /* ip reass. queue */
b6dce92e 215 uint16_t ip_id; /* ip packet ctr, for ids */
460fec67
JK
216
217 /* bootp/dhcp states */
218 BOOTPClient bootp_clients[NB_BOOTP_CLIENTS];
219 char *bootp_filename;
63d2960b
KS
220 size_t vdnssearch_len;
221 uint8_t *vdnssearch;
460fec67
JK
222
223 /* tcp states */
224 struct socket tcb;
225 struct socket *tcp_last_so;
226 tcp_seq tcp_iss; /* tcp initial send seq # */
b6dce92e 227 uint32_t tcp_now; /* for RFC 1323 timestamps */
460fec67
JK
228
229 /* udp states */
230 struct socket udb;
231 struct socket *udp_last_so;
232
e6d43cfb
JK
233 /* icmp states */
234 struct socket icmp;
235 struct socket *icmp_last_so;
236
460fec67
JK
237 /* tftp states */
238 char *tftp_prefix;
239 struct tftp_session tftp_sessions[TFTP_SESSIONS_MAX];
240
1a0ca1e1 241 ArpTable arp_table;
0d6ff71a
GS
242 NdpTable ndp_table;
243
244 GRand *grand;
245 QEMUTimer *ra_timer;
1a0ca1e1 246
9f8bd042 247 void *opaque;
460fec67
JK
248};
249
ad0d8c4c 250extern Slirp *slirp_instance;
f0cbd3ec 251
f0cbd3ec
FB
252#ifndef NULL
253#define NULL (void *)0
254#endif
255
256#ifndef FULL_BOLT
6cb9c6d3 257void if_start(Slirp *);
f0cbd3ec 258#else
6cb9c6d3 259void if_start(struct ttys *);
f0cbd3ec
FB
260#endif
261
f0cbd3ec 262#ifndef HAVE_STRERROR
64b85a8f 263 char *strerror(int error);
f0cbd3ec
FB
264#endif
265
266#ifndef HAVE_INDEX
6cb9c6d3 267 char *index(const char *, int);
f0cbd3ec
FB
268#endif
269
270#ifndef HAVE_GETHOSTID
6cb9c6d3 271 long gethostid(void);
f0cbd3ec
FB
272#endif
273
379ff53d 274#ifndef _WIN32
f0cbd3ec 275#include <netdb.h>
379ff53d 276#endif
f0cbd3ec
FB
277
278#define DEFAULT_BAUD 115200
279
9634d903
BS
280#define SO_OPTIONS DO_KEEPALIVE
281#define TCP_MAXIDLE (TCPTV_KEEPCNT * TCPTV_KEEPINTVL)
282
63d2960b
KS
283/* dnssearch.c */
284int translate_dnssearch(Slirp *s, const char ** names);
285
f0cbd3ec
FB
286/* cksum.c */
287int cksum(struct mbuf *m, int len);
0d6ff71a 288int ip6_cksum(struct mbuf *m);
f0cbd3ec
FB
289
290/* if.c */
6cb9c6d3
BS
291void if_init(Slirp *);
292void if_output(struct socket *, struct mbuf *);
f0cbd3ec
FB
293
294/* ip_input.c */
6cb9c6d3 295void ip_init(Slirp *);
a68adc22 296void ip_cleanup(Slirp *);
6cb9c6d3
BS
297void ip_input(struct mbuf *);
298void ip_slowtimo(Slirp *);
299void ip_stripoptions(register struct mbuf *, struct mbuf *);
f0cbd3ec
FB
300
301/* ip_output.c */
6cb9c6d3 302int ip_output(struct socket *, struct mbuf *);
f0cbd3ec 303
0d6ff71a
GS
304/* ip6_input.c */
305void ip6_init(Slirp *);
306void ip6_cleanup(Slirp *);
307void ip6_input(struct mbuf *);
308
309/* ip6_output */
310int ip6_output(struct socket *, struct mbuf *, int fast);
311
f0cbd3ec 312/* tcp_input.c */
9dfbf250 313void tcp_input(register struct mbuf *, int, struct socket *, unsigned short af);
6cb9c6d3 314int tcp_mss(register struct tcpcb *, u_int);
f0cbd3ec
FB
315
316/* tcp_output.c */
6cb9c6d3
BS
317int tcp_output(register struct tcpcb *);
318void tcp_setpersist(register struct tcpcb *);
f0cbd3ec
FB
319
320/* tcp_subr.c */
6cb9c6d3 321void tcp_init(Slirp *);
a68adc22 322void tcp_cleanup(Slirp *);
6cb9c6d3 323void tcp_template(struct tcpcb *);
9dfbf250
GS
324void tcp_respond(struct tcpcb *, register struct tcpiphdr *,
325 register struct mbuf *, tcp_seq, tcp_seq, int, unsigned short);
6cb9c6d3
BS
326struct tcpcb * tcp_newtcpcb(struct socket *);
327struct tcpcb * tcp_close(register struct tcpcb *);
328void tcp_sockclosed(struct tcpcb *);
cc573a69 329int tcp_fconnect(struct socket *, unsigned short af);
6cb9c6d3
BS
330void tcp_connect(struct socket *);
331int tcp_attach(struct socket *);
b6dce92e 332uint8_t tcp_tos(struct socket *);
6cb9c6d3
BS
333int tcp_emu(struct socket *, struct mbuf *);
334int tcp_ctl(struct socket *);
9fafc9ea 335struct tcpcb *tcp_drop(struct tcpcb *tp, int err);
f0cbd3ec
FB
336
337#ifdef USE_PPP
338#define MIN_MRU MINMRU
339#define MAX_MRU MAXMRU
340#else
341#define MIN_MRU 128
342#define MAX_MRU 16384
343#endif
344
379ff53d
FB
345#ifndef _WIN32
346#define min(x,y) ((x) < (y) ? (x) : (y))
347#define max(x,y) ((x) > (y) ? (x) : (y))
348#endif
349
02d2c54c 350#ifdef _WIN32
ef6ff6b7 351#undef errno
02d2c54c
FB
352#define errno (WSAGetLastError())
353#endif
354
f0cbd3ec 355#endif