blob: 02b3793e9f1dace1de9101780e448c3619dff0ff [file] [log] [blame]
bellardf0cbd3e2004-04-22 00:10:48 +00001/*
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.
aliguori2f5f8992009-01-26 19:37:41 +000013 * 3. Neither the name of the University nor the names of its contributors
bellardf0cbd3e2004-04-22 00:10:48 +000014 * 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.
ths5fafdf22007-09-16 21:08:06 +000036 *
37 * Please read the file COPYRIGHT for the
bellardf0cbd3e2004-04-22 00:10:48 +000038 * terms and conditions of the copyright.
39 */
40
41#include <slirp.h>
42#include "ip_icmp.h"
43
Stefan Weilb6dce922010-07-22 22:15:23 +020044static uint8_t udp_tos(struct socket *so);
blueswir19634d902007-10-26 19:01:16 +000045
bellardf0cbd3e2004-04-22 00:10:48 +000046void
Jan Kiszka460fec62009-06-24 14:42:31 +020047udp_init(Slirp *slirp)
bellardf0cbd3e2004-04-22 00:10:48 +000048{
Jan Kiszka460fec62009-06-24 14:42:31 +020049 slirp->udb.so_next = slirp->udb.so_prev = &slirp->udb;
50 slirp->udp_last_so = &slirp->udb;
bellardf0cbd3e2004-04-22 00:10:48 +000051}
ths5fafdf22007-09-16 21:08:06 +000052/* m->m_data points at ip packet header
53 * m->m_len length ip packet
bellardf0cbd3e2004-04-22 00:10:48 +000054 * ip->ip_len length data (IPDU)
55 */
56void
blueswir1aeed97c2009-04-18 07:32:41 +000057udp_input(register struct mbuf *m, int iphlen)
bellardf0cbd3e2004-04-22 00:10:48 +000058{
Jan Kiszka460fec62009-06-24 14:42:31 +020059 Slirp *slirp = m->slirp;
bellardf0cbd3e2004-04-22 00:10:48 +000060 register struct ip *ip;
61 register struct udphdr *uh;
bellardf0cbd3e2004-04-22 00:10:48 +000062 int len;
ths5fafdf22007-09-16 21:08:06 +000063 struct ip save_ip;
bellardf0cbd3e2004-04-22 00:10:48 +000064 struct socket *so;
ths5fafdf22007-09-16 21:08:06 +000065
bellardf0cbd3e2004-04-22 00:10:48 +000066 DEBUG_CALL("udp_input");
67 DEBUG_ARG("m = %lx", (long)m);
68 DEBUG_ARG("iphlen = %d", iphlen);
ths5fafdf22007-09-16 21:08:06 +000069
bellardf0cbd3e2004-04-22 00:10:48 +000070 /*
71 * Strip IP options, if any; should skip this,
72 * make available to user, and use on returned packets,
73 * but we don't yet have a way to check the checksum
74 * with options still present.
75 */
76 if(iphlen > sizeof(struct ip)) {
77 ip_stripoptions(m, (struct mbuf *)0);
78 iphlen = sizeof(struct ip);
79 }
80
81 /*
82 * Get IP and UDP header together in first mbuf.
83 */
84 ip = mtod(m, struct ip *);
85 uh = (struct udphdr *)((caddr_t)ip + iphlen);
86
87 /*
88 * Make mbuf data length reflect UDP length.
89 * If not enough data to reflect UDP length, drop.
90 */
Stefan Weilb6dce922010-07-22 22:15:23 +020091 len = ntohs((uint16_t)uh->uh_ulen);
bellardf0cbd3e2004-04-22 00:10:48 +000092
93 if (ip->ip_len != len) {
94 if (len > ip->ip_len) {
bellardf0cbd3e2004-04-22 00:10:48 +000095 goto bad;
96 }
97 m_adj(m, len - ip->ip_len);
98 ip->ip_len = len;
99 }
ths5fafdf22007-09-16 21:08:06 +0000100
bellardf0cbd3e2004-04-22 00:10:48 +0000101 /*
102 * Save a copy of the IP header in case we want restore it
103 * for sending an ICMP error message in response.
104 */
ths5fafdf22007-09-16 21:08:06 +0000105 save_ip = *ip;
bellardf0cbd3e2004-04-22 00:10:48 +0000106 save_ip.ip_len+= iphlen; /* tcp_input subtracts this */
107
108 /*
109 * Checksum extended UDP header and data.
110 */
Jan Kiszka0d62c4c2009-06-24 14:42:29 +0200111 if (uh->uh_sum) {
blueswir1429d0a32009-01-13 19:48:42 +0000112 memset(&((struct ipovly *)ip)->ih_mbuf, 0, sizeof(struct mbuf_ptr));
bellardf0cbd3e2004-04-22 00:10:48 +0000113 ((struct ipovly *)ip)->ih_x1 = 0;
114 ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
bellardf0cbd3e2004-04-22 00:10:48 +0000115 if(cksum(m, len + sizeof(struct ip))) {
bellardf0cbd3e2004-04-22 00:10:48 +0000116 goto bad;
117 }
118 }
119
120 /*
121 * handle DHCP/BOOTP
122 */
123 if (ntohs(uh->uh_dport) == BOOTP_SERVER) {
124 bootp_input(m);
125 goto bad;
126 }
127
Jan Kiszka460fec62009-06-24 14:42:31 +0200128 if (slirp->restricted) {
aliguoria9ba3a82009-01-08 19:24:00 +0000129 goto bad;
Jan Kiszka460fec62009-06-24 14:42:31 +0200130 }
aliguoria9ba3a82009-01-08 19:24:00 +0000131
bellardc7f74642004-08-24 21:57:12 +0000132 /*
133 * handle TFTP
134 */
135 if (ntohs(uh->uh_dport) == TFTP_SERVER) {
136 tftp_input(m);
137 goto bad;
138 }
139
bellardf0cbd3e2004-04-22 00:10:48 +0000140 /*
141 * Locate pcb for datagram.
142 */
Jan Kiszka460fec62009-06-24 14:42:31 +0200143 so = slirp->udp_last_so;
bellardf0cbd3e2004-04-22 00:10:48 +0000144 if (so->so_lport != uh->uh_sport ||
145 so->so_laddr.s_addr != ip->ip_src.s_addr) {
146 struct socket *tmp;
ths3b46e622007-09-17 08:09:54 +0000147
Jan Kiszka460fec62009-06-24 14:42:31 +0200148 for (tmp = slirp->udb.so_next; tmp != &slirp->udb;
149 tmp = tmp->so_next) {
bellardf0cbd3e2004-04-22 00:10:48 +0000150 if (tmp->so_lport == uh->uh_sport &&
151 tmp->so_laddr.s_addr == ip->ip_src.s_addr) {
bellardf0cbd3e2004-04-22 00:10:48 +0000152 so = tmp;
153 break;
154 }
155 }
Jan Kiszka460fec62009-06-24 14:42:31 +0200156 if (tmp == &slirp->udb) {
bellardf0cbd3e2004-04-22 00:10:48 +0000157 so = NULL;
158 } else {
Jan Kiszka460fec62009-06-24 14:42:31 +0200159 slirp->udp_last_so = so;
bellardf0cbd3e2004-04-22 00:10:48 +0000160 }
161 }
ths5fafdf22007-09-16 21:08:06 +0000162
bellardf0cbd3e2004-04-22 00:10:48 +0000163 if (so == NULL) {
164 /*
165 * If there's no socket for this packet,
166 * create one
167 */
Jan Kiszka460fec62009-06-24 14:42:31 +0200168 so = socreate(slirp);
169 if (!so) {
170 goto bad;
171 }
bellardf0cbd3e2004-04-22 00:10:48 +0000172 if(udp_attach(so) == -1) {
ths5fafdf22007-09-16 21:08:06 +0000173 DEBUG_MISC((dfd," udp_attach errno = %d-%s\n",
bellardf0cbd3e2004-04-22 00:10:48 +0000174 errno,strerror(errno)));
175 sofree(so);
176 goto bad;
177 }
ths3b46e622007-09-17 08:09:54 +0000178
bellardf0cbd3e2004-04-22 00:10:48 +0000179 /*
180 * Setup fields
181 */
bellardf0cbd3e2004-04-22 00:10:48 +0000182 so->so_laddr = ip->ip_src;
183 so->so_lport = uh->uh_sport;
ths3b46e622007-09-17 08:09:54 +0000184
bellardf0cbd3e2004-04-22 00:10:48 +0000185 if ((so->so_iptos = udp_tos(so)) == 0)
186 so->so_iptos = ip->ip_tos;
ths3b46e622007-09-17 08:09:54 +0000187
bellardf0cbd3e2004-04-22 00:10:48 +0000188 /*
189 * XXXXX Here, check if it's in udpexec_list,
190 * and if it is, do the fork_exec() etc.
191 */
192 }
193
ths54fd9cd2007-01-17 22:47:40 +0000194 so->so_faddr = ip->ip_dst; /* XXX */
195 so->so_fport = uh->uh_dport; /* XXX */
196
bellardf0cbd3e2004-04-22 00:10:48 +0000197 iphlen += sizeof(struct udphdr);
198 m->m_len -= iphlen;
199 m->m_data += iphlen;
200
201 /*
202 * Now we sendto() the packet.
203 */
bellardf0cbd3e2004-04-22 00:10:48 +0000204 if(sosendto(so,m) == -1) {
205 m->m_len += iphlen;
206 m->m_data -= iphlen;
207 *ip=save_ip;
208 DEBUG_MISC((dfd,"udp tx errno = %d-%s\n",errno,strerror(errno)));
ths3b46e622007-09-17 08:09:54 +0000209 icmp_error(m, ICMP_UNREACH,ICMP_UNREACH_NET, 0,strerror(errno));
bellardf0cbd3e2004-04-22 00:10:48 +0000210 }
211
212 m_free(so->so_m); /* used for ICMP if error on sorecvfrom */
213
214 /* restore the orig mbuf packet */
215 m->m_len += iphlen;
216 m->m_data -= iphlen;
217 *ip=save_ip;
218 so->so_m=m; /* ICMP backup */
219
220 return;
221bad:
222 m_freem(m);
bellardf0cbd3e2004-04-22 00:10:48 +0000223 return;
224}
225
ths5fafdf22007-09-16 21:08:06 +0000226int udp_output2(struct socket *so, struct mbuf *m,
bellardf0cbd3e2004-04-22 00:10:48 +0000227 struct sockaddr_in *saddr, struct sockaddr_in *daddr,
228 int iptos)
229{
230 register struct udpiphdr *ui;
231 int error = 0;
232
233 DEBUG_CALL("udp_output");
234 DEBUG_ARG("so = %lx", (long)so);
235 DEBUG_ARG("m = %lx", (long)m);
236 DEBUG_ARG("saddr = %lx", (long)saddr->sin_addr.s_addr);
237 DEBUG_ARG("daddr = %lx", (long)daddr->sin_addr.s_addr);
238
239 /*
240 * Adjust for header
241 */
242 m->m_data -= sizeof(struct udpiphdr);
243 m->m_len += sizeof(struct udpiphdr);
ths5fafdf22007-09-16 21:08:06 +0000244
bellardf0cbd3e2004-04-22 00:10:48 +0000245 /*
246 * Fill in mbuf with extended UDP header
247 * and addresses and length put into network format.
248 */
249 ui = mtod(m, struct udpiphdr *);
blueswir1429d0a32009-01-13 19:48:42 +0000250 memset(&ui->ui_i.ih_mbuf, 0 , sizeof(struct mbuf_ptr));
bellardf0cbd3e2004-04-22 00:10:48 +0000251 ui->ui_x1 = 0;
252 ui->ui_pr = IPPROTO_UDP;
Jan Kiszka0d62c4c2009-06-24 14:42:29 +0200253 ui->ui_len = htons(m->m_len - sizeof(struct ip));
bellardf0cbd3e2004-04-22 00:10:48 +0000254 /* XXXXX Check for from-one-location sockets, or from-any-location sockets */
255 ui->ui_src = saddr->sin_addr;
256 ui->ui_dst = daddr->sin_addr;
257 ui->ui_sport = saddr->sin_port;
258 ui->ui_dport = daddr->sin_port;
259 ui->ui_ulen = ui->ui_len;
260
261 /*
262 * Stuff checksum and output datagram.
263 */
264 ui->ui_sum = 0;
Jan Kiszka0d62c4c2009-06-24 14:42:29 +0200265 if ((ui->ui_sum = cksum(m, m->m_len)) == 0)
bellardf0cbd3e2004-04-22 00:10:48 +0000266 ui->ui_sum = 0xffff;
bellardf0cbd3e2004-04-22 00:10:48 +0000267 ((struct ip *)ui)->ip_len = m->m_len;
268
blueswir19634d902007-10-26 19:01:16 +0000269 ((struct ip *)ui)->ip_ttl = IPDEFTTL;
bellardf0cbd3e2004-04-22 00:10:48 +0000270 ((struct ip *)ui)->ip_tos = iptos;
ths5fafdf22007-09-16 21:08:06 +0000271
bellardf0cbd3e2004-04-22 00:10:48 +0000272 error = ip_output(so, m);
ths5fafdf22007-09-16 21:08:06 +0000273
bellardf0cbd3e2004-04-22 00:10:48 +0000274 return (error);
275}
276
ths5fafdf22007-09-16 21:08:06 +0000277int udp_output(struct socket *so, struct mbuf *m,
bellardf0cbd3e2004-04-22 00:10:48 +0000278 struct sockaddr_in *addr)
279
280{
Jan Kiszka460fec62009-06-24 14:42:31 +0200281 Slirp *slirp = so->slirp;
bellardf0cbd3e2004-04-22 00:10:48 +0000282 struct sockaddr_in saddr, daddr;
283
284 saddr = *addr;
Jan Kiszka460fec62009-06-24 14:42:31 +0200285 if ((so->so_faddr.s_addr & slirp->vnetwork_mask.s_addr) ==
286 slirp->vnetwork_addr.s_addr) {
287 uint32_t inv_mask = ~slirp->vnetwork_mask.s_addr;
288
289 if ((so->so_faddr.s_addr & inv_mask) == inv_mask) {
290 saddr.sin_addr = slirp->vhost_addr;
Jan Kiszkaa13a4122009-06-24 14:42:28 +0200291 } else if (addr->sin_addr.s_addr == loopback_addr.s_addr ||
Jan Kiszka460fec62009-06-24 14:42:31 +0200292 so->so_faddr.s_addr != slirp->vhost_addr.s_addr) {
Jan Kiszkaa13a4122009-06-24 14:42:28 +0200293 saddr.sin_addr = so->so_faddr;
294 }
bellardc904d612006-05-03 20:53:46 +0000295 }
bellardf0cbd3e2004-04-22 00:10:48 +0000296 daddr.sin_addr = so->so_laddr;
297 daddr.sin_port = so->so_lport;
ths3b46e622007-09-17 08:09:54 +0000298
bellardf0cbd3e2004-04-22 00:10:48 +0000299 return udp_output2(so, m, &saddr, &daddr, so->so_iptos);
300}
301
302int
blueswir1aeed97c2009-04-18 07:32:41 +0000303udp_attach(struct socket *so)
bellardf0cbd3e2004-04-22 00:10:48 +0000304{
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100305 if((so->s = qemu_socket(AF_INET,SOCK_DGRAM,0)) != -1) {
Ed Swierk97df1ee2009-07-22 17:53:15 -0700306 so->so_expire = curtime + SO_EXPIRE;
307 insque(so, &so->slirp->udb);
bellardf0cbd3e2004-04-22 00:10:48 +0000308 }
309 return(so->s);
310}
311
312void
blueswir1aeed97c2009-04-18 07:32:41 +0000313udp_detach(struct socket *so)
bellardf0cbd3e2004-04-22 00:10:48 +0000314{
bellard379ff532004-07-12 22:33:07 +0000315 closesocket(so->s);
bellardf0cbd3e2004-04-22 00:10:48 +0000316 sofree(so);
317}
318
blueswir19634d902007-10-26 19:01:16 +0000319static const struct tos_t udptos[] = {
bellardf0cbd3e2004-04-22 00:10:48 +0000320 {0, 53, IPTOS_LOWDELAY, 0}, /* DNS */
bellardf0cbd3e2004-04-22 00:10:48 +0000321 {0, 0, 0, 0}
322};
323
Stefan Weilb6dce922010-07-22 22:15:23 +0200324static uint8_t
blueswir19634d902007-10-26 19:01:16 +0000325udp_tos(struct socket *so)
bellardf0cbd3e2004-04-22 00:10:48 +0000326{
327 int i = 0;
ths5fafdf22007-09-16 21:08:06 +0000328
bellardf0cbd3e2004-04-22 00:10:48 +0000329 while(udptos[i].tos) {
330 if ((udptos[i].fport && ntohs(so->so_fport) == udptos[i].fport) ||
331 (udptos[i].lport && ntohs(so->so_lport) == udptos[i].lport)) {
332 so->so_emu = udptos[i].emu;
333 return udptos[i].tos;
334 }
335 i++;
336 }
ths5fafdf22007-09-16 21:08:06 +0000337
bellardf0cbd3e2004-04-22 00:10:48 +0000338 return 0;
339}
340
bellardf0cbd3e2004-04-22 00:10:48 +0000341struct socket *
Stefan Weilb6dce922010-07-22 22:15:23 +0200342udp_listen(Slirp *slirp, uint32_t haddr, u_int hport, uint32_t laddr,
Jan Kiszka460fec62009-06-24 14:42:31 +0200343 u_int lport, int flags)
bellardf0cbd3e2004-04-22 00:10:48 +0000344{
345 struct sockaddr_in addr;
346 struct socket *so;
balrog242acf32008-05-10 01:49:53 +0000347 socklen_t addrlen = sizeof(struct sockaddr_in), opt = 1;
ths5fafdf22007-09-16 21:08:06 +0000348
Jan Kiszka460fec62009-06-24 14:42:31 +0200349 so = socreate(slirp);
350 if (!so) {
351 return NULL;
bellardf0cbd3e2004-04-22 00:10:48 +0000352 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100353 so->s = qemu_socket(AF_INET,SOCK_DGRAM,0);
bellardf0cbd3e2004-04-22 00:10:48 +0000354 so->so_expire = curtime + SO_EXPIRE;
Jan Kiszka460fec62009-06-24 14:42:31 +0200355 insque(so, &slirp->udb);
bellardf0cbd3e2004-04-22 00:10:48 +0000356
357 addr.sin_family = AF_INET;
Jan Kiszka3c6a0582009-06-24 14:42:28 +0200358 addr.sin_addr.s_addr = haddr;
359 addr.sin_port = hport;
bellardf0cbd3e2004-04-22 00:10:48 +0000360
361 if (bind(so->s,(struct sockaddr *)&addr, addrlen) < 0) {
362 udp_detach(so);
363 return NULL;
364 }
365 setsockopt(so->s,SOL_SOCKET,SO_REUSEADDR,(char *)&opt,sizeof(int));
ths5fafdf22007-09-16 21:08:06 +0000366
bellardf0cbd3e2004-04-22 00:10:48 +0000367 getsockname(so->s,(struct sockaddr *)&addr,&addrlen);
368 so->so_fport = addr.sin_port;
Jan Kiszkaa13a4122009-06-24 14:42:28 +0200369 if (addr.sin_addr.s_addr == 0 ||
370 addr.sin_addr.s_addr == loopback_addr.s_addr) {
Jan Kiszka460fec62009-06-24 14:42:31 +0200371 so->so_faddr = slirp->vhost_addr;
Jan Kiszkaa13a4122009-06-24 14:42:28 +0200372 } else {
bellardf0cbd3e2004-04-22 00:10:48 +0000373 so->so_faddr = addr.sin_addr;
Jan Kiszkaa13a4122009-06-24 14:42:28 +0200374 }
bellardf0cbd3e2004-04-22 00:10:48 +0000375 so->so_lport = lport;
376 so->so_laddr.s_addr = laddr;
377 if (flags != SS_FACCEPTONCE)
378 so->so_expire = 0;
ths5fafdf22007-09-16 21:08:06 +0000379
Jan Kiszkaf932b6c2009-06-24 14:42:29 +0200380 so->so_state &= SS_PERSISTENT_MASK;
Jan Kiszka6dd5ffb2009-06-24 14:42:29 +0200381 so->so_state |= SS_ISFCONNECTED | flags;
ths5fafdf22007-09-16 21:08:06 +0000382
bellardf0cbd3e2004-04-22 00:10:48 +0000383 return so;
384}