]> git.proxmox.com Git - mirror_qemu.git/blame - slirp/src/udp.c
Merge remote-tracking branch 'remotes/bonzini/tags/for-upstream' into staging
[mirror_qemu.git] / slirp / src / udp.c
CommitLineData
f0cbd3ec
FB
1/*
2 * Copyright (c) 1982, 1986, 1988, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
2f5f8996 13 * 3. Neither the name of the University nor the names of its contributors
f0cbd3ec
FB
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * @(#)udp_usrreq.c 8.4 (Berkeley) 1/21/94
30 * udp_usrreq.c,v 1.4 1994/10/02 17:48:45 phk Exp
31 */
32
33/*
34 * Changes and additions relating to SLiRP
35 * Copyright (c) 1995 Danny Gasparovski.
5fafdf24
TS
36 *
37 * Please read the file COPYRIGHT for the
f0cbd3ec
FB
38 * terms and conditions of the copyright.
39 */
40
a9c94277 41#include "slirp.h"
f0cbd3ec
FB
42#include "ip_icmp.h"
43
b6dce92e 44static uint8_t udp_tos(struct socket *so);
9634d903 45
f0cbd3ec 46void
460fec67 47udp_init(Slirp *slirp)
f0cbd3ec 48{
460fec67
JK
49 slirp->udb.so_next = slirp->udb.so_prev = &slirp->udb;
50 slirp->udp_last_so = &slirp->udb;
f0cbd3ec 51}
a68adc22
JK
52
53void udp_cleanup(Slirp *slirp)
54{
55 while (slirp->udb.so_next != &slirp->udb) {
56 udp_detach(slirp->udb.so_next);
57 }
58}
59
5fafdf24
TS
60/* m->m_data points at ip packet header
61 * m->m_len length ip packet
f0cbd3ec
FB
62 * ip->ip_len length data (IPDU)
63 */
64void
aeed97c4 65udp_input(register struct mbuf *m, int iphlen)
f0cbd3ec 66{
460fec67 67 Slirp *slirp = m->slirp;
f0cbd3ec
FB
68 register struct ip *ip;
69 register struct udphdr *uh;
f0cbd3ec 70 int len;
5fafdf24 71 struct ip save_ip;
f0cbd3ec 72 struct socket *so;
8a87f121
GS
73 struct sockaddr_storage lhost;
74 struct sockaddr_in *lhost4;
5fafdf24 75
f0cbd3ec 76 DEBUG_CALL("udp_input");
ecc804ca 77 DEBUG_ARG("m = %p", m);
f0cbd3ec 78 DEBUG_ARG("iphlen = %d", iphlen);
5fafdf24 79
f0cbd3ec
FB
80 /*
81 * Strip IP options, if any; should skip this,
82 * make available to user, and use on returned packets,
83 * but we don't yet have a way to check the checksum
84 * with options still present.
85 */
86 if(iphlen > sizeof(struct ip)) {
87 ip_stripoptions(m, (struct mbuf *)0);
88 iphlen = sizeof(struct ip);
89 }
90
91 /*
92 * Get IP and UDP header together in first mbuf.
93 */
94 ip = mtod(m, struct ip *);
d7df0b41 95 uh = (struct udphdr *)((char *)ip + iphlen);
f0cbd3ec
FB
96
97 /*
98 * Make mbuf data length reflect UDP length.
99 * If not enough data to reflect UDP length, drop.
100 */
b6dce92e 101 len = ntohs((uint16_t)uh->uh_ulen);
f0cbd3ec
FB
102
103 if (ip->ip_len != len) {
104 if (len > ip->ip_len) {
f0cbd3ec
FB
105 goto bad;
106 }
107 m_adj(m, len - ip->ip_len);
108 ip->ip_len = len;
109 }
5fafdf24 110
f0cbd3ec
FB
111 /*
112 * Save a copy of the IP header in case we want restore it
113 * for sending an ICMP error message in response.
114 */
5fafdf24 115 save_ip = *ip;
f0cbd3ec
FB
116 save_ip.ip_len+= iphlen; /* tcp_input subtracts this */
117
118 /*
119 * Checksum extended UDP header and data.
120 */
0d62c4cf 121 if (uh->uh_sum) {
429d0a3d 122 memset(&((struct ipovly *)ip)->ih_mbuf, 0, sizeof(struct mbuf_ptr));
f0cbd3ec
FB
123 ((struct ipovly *)ip)->ih_x1 = 0;
124 ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
f0cbd3ec 125 if(cksum(m, len + sizeof(struct ip))) {
f0cbd3ec
FB
126 goto bad;
127 }
128 }
129
fad7fb9c
TH
130 lhost.ss_family = AF_INET;
131 lhost4 = (struct sockaddr_in *) &lhost;
132 lhost4->sin_addr = ip->ip_src;
133 lhost4->sin_port = uh->uh_sport;
134
f0cbd3ec
FB
135 /*
136 * handle DHCP/BOOTP
137 */
5a82362a
JK
138 if (ntohs(uh->uh_dport) == BOOTP_SERVER &&
139 (ip->ip_dst.s_addr == slirp->vhost_addr.s_addr ||
140 ip->ip_dst.s_addr == 0xffffffff)) {
141 bootp_input(m);
142 goto bad;
143 }
f0cbd3ec 144
c7f74643
FB
145 /*
146 * handle TFTP
147 */
5a82362a
JK
148 if (ntohs(uh->uh_dport) == TFTP_SERVER &&
149 ip->ip_dst.s_addr == slirp->vhost_addr.s_addr) {
fad7fb9c
TH
150 m->m_data += iphlen;
151 m->m_len -= iphlen;
152 tftp_input(&lhost, m);
153 m->m_data -= iphlen;
154 m->m_len += iphlen;
c7f74643
FB
155 goto bad;
156 }
157
12b513d8
JK
158 if (slirp->restricted) {
159 goto bad;
160 }
161
f0cbd3ec
FB
162 /*
163 * Locate pcb for datagram.
164 */
8a87f121 165 so = solookup(&slirp->udp_last_so, &slirp->udb, &lhost, NULL);
5fafdf24 166
f0cbd3ec
FB
167 if (so == NULL) {
168 /*
169 * If there's no socket for this packet,
170 * create one
171 */
460fec67 172 so = socreate(slirp);
9b5a30dc 173 if (udp_attach(so, AF_INET) == -1) {
226ea7a9 174 DEBUG_MISC(" udp_attach errno = %d-%s", errno, strerror(errno));
f0cbd3ec
FB
175 sofree(so);
176 goto bad;
177 }
3b46e624 178
f0cbd3ec
FB
179 /*
180 * Setup fields
181 */
eae303ff 182 so->so_lfamily = AF_INET;
f0cbd3ec
FB
183 so->so_laddr = ip->ip_src;
184 so->so_lport = uh->uh_sport;
3b46e624 185
f0cbd3ec
FB
186 if ((so->so_iptos = udp_tos(so)) == 0)
187 so->so_iptos = ip->ip_tos;
3b46e624 188
f0cbd3ec
FB
189 /*
190 * XXXXX Here, check if it's in udpexec_list,
191 * and if it is, do the fork_exec() etc.
192 */
193 }
194
eae303ff 195 so->so_ffamily = AF_INET;
54fd9cdf
TS
196 so->so_faddr = ip->ip_dst; /* XXX */
197 so->so_fport = uh->uh_dport; /* XXX */
198
f0cbd3ec
FB
199 iphlen += sizeof(struct udphdr);
200 m->m_len -= iphlen;
201 m->m_data += iphlen;
202
203 /*
204 * Now we sendto() the packet.
205 */
f0cbd3ec
FB
206 if(sosendto(so,m) == -1) {
207 m->m_len += iphlen;
208 m->m_data -= iphlen;
209 *ip=save_ip;
226ea7a9 210 DEBUG_MISC("udp tx errno = %d-%s", errno, strerror(errno));
de40abfe
YB
211 icmp_send_error(m, ICMP_UNREACH, ICMP_UNREACH_NET, 0,
212 strerror(errno));
86c9e1e9 213 goto bad;
f0cbd3ec
FB
214 }
215
216 m_free(so->so_m); /* used for ICMP if error on sorecvfrom */
217
218 /* restore the orig mbuf packet */
219 m->m_len += iphlen;
220 m->m_data -= iphlen;
221 *ip=save_ip;
222 so->so_m=m; /* ICMP backup */
223
224 return;
225bad:
3acccfc6 226 m_free(m);
f0cbd3ec
FB
227}
228
5379229a 229int udp_output(struct socket *so, struct mbuf *m,
f0cbd3ec
FB
230 struct sockaddr_in *saddr, struct sockaddr_in *daddr,
231 int iptos)
232{
233 register struct udpiphdr *ui;
234 int error = 0;
235
236 DEBUG_CALL("udp_output");
ecc804ca
SW
237 DEBUG_ARG("so = %p", so);
238 DEBUG_ARG("m = %p", m);
3ad9319f
AK
239 DEBUG_ARG("saddr = %s", inet_ntoa(saddr->sin_addr));
240 DEBUG_ARG("daddr = %s", inet_ntoa(daddr->sin_addr));
f0cbd3ec
FB
241
242 /*
243 * Adjust for header
244 */
245 m->m_data -= sizeof(struct udpiphdr);
246 m->m_len += sizeof(struct udpiphdr);
5fafdf24 247
f0cbd3ec
FB
248 /*
249 * Fill in mbuf with extended UDP header
250 * and addresses and length put into network format.
251 */
252 ui = mtod(m, struct udpiphdr *);
429d0a3d 253 memset(&ui->ui_i.ih_mbuf, 0 , sizeof(struct mbuf_ptr));
f0cbd3ec
FB
254 ui->ui_x1 = 0;
255 ui->ui_pr = IPPROTO_UDP;
0d62c4cf 256 ui->ui_len = htons(m->m_len - sizeof(struct ip));
f0cbd3ec
FB
257 /* XXXXX Check for from-one-location sockets, or from-any-location sockets */
258 ui->ui_src = saddr->sin_addr;
259 ui->ui_dst = daddr->sin_addr;
260 ui->ui_sport = saddr->sin_port;
261 ui->ui_dport = daddr->sin_port;
262 ui->ui_ulen = ui->ui_len;
263
264 /*
265 * Stuff checksum and output datagram.
266 */
267 ui->ui_sum = 0;
0d62c4cf 268 if ((ui->ui_sum = cksum(m, m->m_len)) == 0)
f0cbd3ec 269 ui->ui_sum = 0xffff;
f0cbd3ec
FB
270 ((struct ip *)ui)->ip_len = m->m_len;
271
9634d903 272 ((struct ip *)ui)->ip_ttl = IPDEFTTL;
f0cbd3ec 273 ((struct ip *)ui)->ip_tos = iptos;
5fafdf24 274
f0cbd3ec 275 error = ip_output(so, m);
5fafdf24 276
f0cbd3ec
FB
277 return (error);
278}
279
f0cbd3ec 280int
9b5a30dc 281udp_attach(struct socket *so, unsigned short af)
f0cbd3ec 282{
707bd47e 283 so->s = slirp_socket(af, SOCK_DGRAM, 0);
9b5a30dc 284 if (so->s != -1) {
97df1ee5
ES
285 so->so_expire = curtime + SO_EXPIRE;
286 insque(so, &so->slirp->udb);
f0cbd3ec
FB
287 }
288 return(so->s);
289}
290
291void
aeed97c4 292udp_detach(struct socket *so)
f0cbd3ec 293{
3e0fad3a 294 so->slirp->cb->unregister_poll_fd(so->s, so->slirp->opaque);
fdbfba8c 295 closesocket(so->s);
f0cbd3ec
FB
296 sofree(so);
297}
298
9634d903 299static const struct tos_t udptos[] = {
f0cbd3ec 300 {0, 53, IPTOS_LOWDELAY, 0}, /* DNS */
f0cbd3ec
FB
301 {0, 0, 0, 0}
302};
303
b6dce92e 304static uint8_t
9634d903 305udp_tos(struct socket *so)
f0cbd3ec
FB
306{
307 int i = 0;
5fafdf24 308
f0cbd3ec
FB
309 while(udptos[i].tos) {
310 if ((udptos[i].fport && ntohs(so->so_fport) == udptos[i].fport) ||
311 (udptos[i].lport && ntohs(so->so_lport) == udptos[i].lport)) {
72e21db7 312 so->so_emu = udptos[i].emu;
f0cbd3ec
FB
313 return udptos[i].tos;
314 }
315 i++;
316 }
5fafdf24 317
f0cbd3ec
FB
318 return 0;
319}
320
f0cbd3ec 321struct socket *
d7df0b41
MAL
322udp_listen(Slirp *slirp, uint32_t haddr, unsigned hport, uint32_t laddr,
323 unsigned lport, int flags)
f0cbd3ec 324{
ffe02f55 325 /* TODO: IPv6 */
f0cbd3ec
FB
326 struct sockaddr_in addr;
327 struct socket *so;
aad1239a 328 socklen_t addrlen = sizeof(struct sockaddr_in);
5fafdf24 329
460fec67 330 so = socreate(slirp);
707bd47e 331 so->s = slirp_socket(AF_INET,SOCK_DGRAM,0);
4577b09a
PM
332 if (so->s < 0) {
333 sofree(so);
334 return NULL;
335 }
f0cbd3ec 336 so->so_expire = curtime + SO_EXPIRE;
460fec67 337 insque(so, &slirp->udb);
f0cbd3ec
FB
338
339 addr.sin_family = AF_INET;
3c6a0580
JK
340 addr.sin_addr.s_addr = haddr;
341 addr.sin_port = hport;
f0cbd3ec
FB
342
343 if (bind(so->s,(struct sockaddr *)&addr, addrlen) < 0) {
344 udp_detach(so);
345 return NULL;
346 }
707bd47e 347 slirp_socket_set_fast_reuse(so->s);
5fafdf24 348
f0cbd3ec 349 getsockname(so->s,(struct sockaddr *)&addr,&addrlen);
5379229a
GS
350 so->fhost.sin = addr;
351 sotranslate_accept(so);
eae303ff 352 so->so_lfamily = AF_INET;
f0cbd3ec
FB
353 so->so_lport = lport;
354 so->so_laddr.s_addr = laddr;
355 if (flags != SS_FACCEPTONCE)
356 so->so_expire = 0;
5fafdf24 357
f932b6ce 358 so->so_state &= SS_PERSISTENT_MASK;
6dd5ffb6 359 so->so_state |= SS_ISFCONNECTED | flags;
5fafdf24 360
f0cbd3ec
FB
361 return so;
362}