udp.c 9.7 KB
Newer Older
B
bellard 已提交
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * Copyright (c) 1982, 1986, 1988, 1990, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
13
 * 3. Neither the name of the University nor the names of its contributors
B
bellard 已提交
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *	@(#)udp_usrreq.c	8.4 (Berkeley) 1/21/94
 * udp_usrreq.c,v 1.4 1994/10/02 17:48:45 phk Exp
 */

/*
 * Changes and additions relating to SLiRP
 * Copyright (c) 1995 Danny Gasparovski.
36 37
 *
 * Please read the file COPYRIGHT for the
B
bellard 已提交
38 39 40 41 42 43
 * terms and conditions of the copyright.
 */

#include <slirp.h>
#include "ip_icmp.h"

44
static uint8_t udp_tos(struct socket *so);
45

B
bellard 已提交
46
void
47
udp_init(Slirp *slirp)
B
bellard 已提交
48
{
49 50
    slirp->udb.so_next = slirp->udb.so_prev = &slirp->udb;
    slirp->udp_last_so = &slirp->udb;
B
bellard 已提交
51
}
52 53 54 55 56 57 58 59

void udp_cleanup(Slirp *slirp)
{
    while (slirp->udb.so_next != &slirp->udb) {
        udp_detach(slirp->udb.so_next);
    }
}

60 61
/* m->m_data  points at ip packet header
 * m->m_len   length ip packet
B
bellard 已提交
62 63 64
 * ip->ip_len length data (IPDU)
 */
void
B
blueswir1 已提交
65
udp_input(register struct mbuf *m, int iphlen)
B
bellard 已提交
66
{
67
	Slirp *slirp = m->slirp;
B
bellard 已提交
68 69 70
	register struct ip *ip;
	register struct udphdr *uh;
	int len;
71
	struct ip save_ip;
B
bellard 已提交
72
	struct socket *so;
73

B
bellard 已提交
74 75 76
	DEBUG_CALL("udp_input");
	DEBUG_ARG("m = %lx", (long)m);
	DEBUG_ARG("iphlen = %d", iphlen);
77

B
bellard 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
	/*
	 * Strip IP options, if any; should skip this,
	 * make available to user, and use on returned packets,
	 * but we don't yet have a way to check the checksum
	 * with options still present.
	 */
	if(iphlen > sizeof(struct ip)) {
		ip_stripoptions(m, (struct mbuf *)0);
		iphlen = sizeof(struct ip);
	}

	/*
	 * Get IP and UDP header together in first mbuf.
	 */
	ip = mtod(m, struct ip *);
	uh = (struct udphdr *)((caddr_t)ip + iphlen);

	/*
	 * Make mbuf data length reflect UDP length.
	 * If not enough data to reflect UDP length, drop.
	 */
99
	len = ntohs((uint16_t)uh->uh_ulen);
B
bellard 已提交
100 101 102 103 104 105 106 107

	if (ip->ip_len != len) {
		if (len > ip->ip_len) {
			goto bad;
		}
		m_adj(m, len - ip->ip_len);
		ip->ip_len = len;
	}
108

B
bellard 已提交
109 110 111 112
	/*
	 * Save a copy of the IP header in case we want restore it
	 * for sending an ICMP error message in response.
	 */
113
	save_ip = *ip;
B
bellard 已提交
114 115 116 117 118
	save_ip.ip_len+= iphlen;         /* tcp_input subtracts this */

	/*
	 * Checksum extended UDP header and data.
	 */
J
Jan Kiszka 已提交
119
	if (uh->uh_sum) {
B
blueswir1 已提交
120
      memset(&((struct ipovly *)ip)->ih_mbuf, 0, sizeof(struct mbuf_ptr));
B
bellard 已提交
121 122 123 124 125 126 127 128 129 130
	  ((struct ipovly *)ip)->ih_x1 = 0;
	  ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
	  if(cksum(m, len + sizeof(struct ip))) {
	    goto bad;
	  }
	}

        /*
         *  handle DHCP/BOOTP
         */
131 132 133 134 135 136
        if (ntohs(uh->uh_dport) == BOOTP_SERVER &&
            (ip->ip_dst.s_addr == slirp->vhost_addr.s_addr ||
             ip->ip_dst.s_addr == 0xffffffff)) {
                bootp_input(m);
                goto bad;
            }
B
bellard 已提交
137

B
bellard 已提交
138 139 140
        /*
         *  handle TFTP
         */
141 142
        if (ntohs(uh->uh_dport) == TFTP_SERVER &&
            ip->ip_dst.s_addr == slirp->vhost_addr.s_addr) {
B
bellard 已提交
143 144 145 146
            tftp_input(m);
            goto bad;
        }

J
Jan Kiszka 已提交
147 148 149 150
        if (slirp->restricted) {
            goto bad;
        }

B
bellard 已提交
151 152 153
	/*
	 * Locate pcb for datagram.
	 */
154
	so = slirp->udp_last_so;
B
bellard 已提交
155 156 157
	if (so->so_lport != uh->uh_sport ||
	    so->so_laddr.s_addr != ip->ip_src.s_addr) {
		struct socket *tmp;
158

159 160
		for (tmp = slirp->udb.so_next; tmp != &slirp->udb;
		     tmp = tmp->so_next) {
B
bellard 已提交
161 162 163 164 165 166
			if (tmp->so_lport == uh->uh_sport &&
			    tmp->so_laddr.s_addr == ip->ip_src.s_addr) {
				so = tmp;
				break;
			}
		}
167
		if (tmp == &slirp->udb) {
B
bellard 已提交
168 169
		  so = NULL;
		} else {
170
		  slirp->udp_last_so = so;
B
bellard 已提交
171 172
		}
	}
173

B
bellard 已提交
174 175 176 177 178
	if (so == NULL) {
	  /*
	   * If there's no socket for this packet,
	   * create one
	   */
179 180 181 182
	  so = socreate(slirp);
	  if (!so) {
	      goto bad;
	  }
B
bellard 已提交
183
	  if(udp_attach(so) == -1) {
184
	    DEBUG_MISC((dfd," udp_attach errno = %d-%s\n",
B
bellard 已提交
185 186 187 188
			errno,strerror(errno)));
	    sofree(so);
	    goto bad;
	  }
189

B
bellard 已提交
190 191 192 193 194
	  /*
	   * Setup fields
	   */
	  so->so_laddr = ip->ip_src;
	  so->so_lport = uh->uh_sport;
195

B
bellard 已提交
196 197
	  if ((so->so_iptos = udp_tos(so)) == 0)
	    so->so_iptos = ip->ip_tos;
198

B
bellard 已提交
199 200 201 202 203 204
	  /*
	   * XXXXX Here, check if it's in udpexec_list,
	   * and if it is, do the fork_exec() etc.
	   */
	}

T
ths 已提交
205 206 207
        so->so_faddr = ip->ip_dst; /* XXX */
        so->so_fport = uh->uh_dport; /* XXX */

B
bellard 已提交
208 209 210 211 212 213 214 215 216 217 218 219
	iphlen += sizeof(struct udphdr);
	m->m_len -= iphlen;
	m->m_data += iphlen;

	/*
	 * Now we sendto() the packet.
	 */
	if(sosendto(so,m) == -1) {
	  m->m_len += iphlen;
	  m->m_data -= iphlen;
	  *ip=save_ip;
	  DEBUG_MISC((dfd,"udp tx errno = %d-%s\n",errno,strerror(errno)));
220
	  icmp_error(m, ICMP_UNREACH,ICMP_UNREACH_NET, 0,strerror(errno));
B
bellard 已提交
221 222 223 224 225 226 227 228 229 230 231 232
	}

	m_free(so->so_m);   /* used for ICMP if error on sorecvfrom */

	/* restore the orig mbuf packet */
	m->m_len += iphlen;
	m->m_data -= iphlen;
	*ip=save_ip;
	so->so_m=m;         /* ICMP backup */

	return;
bad:
J
Jan Kiszka 已提交
233
	m_free(m);
B
bellard 已提交
234 235
}

236
int udp_output2(struct socket *so, struct mbuf *m,
B
bellard 已提交
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
                struct sockaddr_in *saddr, struct sockaddr_in *daddr,
                int iptos)
{
	register struct udpiphdr *ui;
	int error = 0;

	DEBUG_CALL("udp_output");
	DEBUG_ARG("so = %lx", (long)so);
	DEBUG_ARG("m = %lx", (long)m);
	DEBUG_ARG("saddr = %lx", (long)saddr->sin_addr.s_addr);
	DEBUG_ARG("daddr = %lx", (long)daddr->sin_addr.s_addr);

	/*
	 * Adjust for header
	 */
	m->m_data -= sizeof(struct udpiphdr);
	m->m_len += sizeof(struct udpiphdr);
254

B
bellard 已提交
255 256 257 258 259
	/*
	 * Fill in mbuf with extended UDP header
	 * and addresses and length put into network format.
	 */
	ui = mtod(m, struct udpiphdr *);
B
blueswir1 已提交
260
    memset(&ui->ui_i.ih_mbuf, 0 , sizeof(struct mbuf_ptr));
B
bellard 已提交
261 262
	ui->ui_x1 = 0;
	ui->ui_pr = IPPROTO_UDP;
J
Jan Kiszka 已提交
263
	ui->ui_len = htons(m->m_len - sizeof(struct ip));
B
bellard 已提交
264 265 266 267 268 269 270 271 272 273 274
	/* XXXXX Check for from-one-location sockets, or from-any-location sockets */
        ui->ui_src = saddr->sin_addr;
	ui->ui_dst = daddr->sin_addr;
	ui->ui_sport = saddr->sin_port;
	ui->ui_dport = daddr->sin_port;
	ui->ui_ulen = ui->ui_len;

	/*
	 * Stuff checksum and output datagram.
	 */
	ui->ui_sum = 0;
J
Jan Kiszka 已提交
275
	if ((ui->ui_sum = cksum(m, m->m_len)) == 0)
B
bellard 已提交
276 277 278
		ui->ui_sum = 0xffff;
	((struct ip *)ui)->ip_len = m->m_len;

279
	((struct ip *)ui)->ip_ttl = IPDEFTTL;
B
bellard 已提交
280
	((struct ip *)ui)->ip_tos = iptos;
281

B
bellard 已提交
282
	error = ip_output(so, m);
283

B
bellard 已提交
284 285 286
	return (error);
}

287
int udp_output(struct socket *so, struct mbuf *m,
B
bellard 已提交
288 289 290
               struct sockaddr_in *addr)

{
291
    Slirp *slirp = so->slirp;
B
bellard 已提交
292 293 294
    struct sockaddr_in saddr, daddr;

    saddr = *addr;
295 296 297 298 299 300
    if ((so->so_faddr.s_addr & slirp->vnetwork_mask.s_addr) ==
        slirp->vnetwork_addr.s_addr) {
        uint32_t inv_mask = ~slirp->vnetwork_mask.s_addr;

        if ((so->so_faddr.s_addr & inv_mask) == inv_mask) {
            saddr.sin_addr = slirp->vhost_addr;
301
        } else if (addr->sin_addr.s_addr == loopback_addr.s_addr ||
302
                   so->so_faddr.s_addr != slirp->vhost_addr.s_addr) {
303 304
            saddr.sin_addr = so->so_faddr;
        }
305
    }
B
bellard 已提交
306 307
    daddr.sin_addr = so->so_laddr;
    daddr.sin_port = so->so_lport;
308

B
bellard 已提交
309 310 311 312
    return udp_output2(so, m, &saddr, &daddr, so->so_iptos);
}

int
B
blueswir1 已提交
313
udp_attach(struct socket *so)
B
bellard 已提交
314
{
K
Kevin Wolf 已提交
315
  if((so->s = qemu_socket(AF_INET,SOCK_DGRAM,0)) != -1) {
316 317
    so->so_expire = curtime + SO_EXPIRE;
    insque(so, &so->slirp->udb);
B
bellard 已提交
318 319 320 321 322
  }
  return(so->s);
}

void
B
blueswir1 已提交
323
udp_detach(struct socket *so)
B
bellard 已提交
324
{
B
bellard 已提交
325
	closesocket(so->s);
B
bellard 已提交
326 327 328
	sofree(so);
}

329
static const struct tos_t udptos[] = {
B
bellard 已提交
330 331 332 333
	{0, 53, IPTOS_LOWDELAY, 0},			/* DNS */
	{0, 0, 0, 0}
};

334
static uint8_t
335
udp_tos(struct socket *so)
B
bellard 已提交
336 337
{
	int i = 0;
338

B
bellard 已提交
339 340 341 342 343 344 345 346
	while(udptos[i].tos) {
		if ((udptos[i].fport && ntohs(so->so_fport) == udptos[i].fport) ||
		    (udptos[i].lport && ntohs(so->so_lport) == udptos[i].lport)) {
		    	so->so_emu = udptos[i].emu;
			return udptos[i].tos;
		}
		i++;
	}
347

B
bellard 已提交
348 349 350 351
	return 0;
}

struct socket *
352
udp_listen(Slirp *slirp, uint32_t haddr, u_int hport, uint32_t laddr,
353
           u_int lport, int flags)
B
bellard 已提交
354 355 356
{
	struct sockaddr_in addr;
	struct socket *so;
357
	socklen_t addrlen = sizeof(struct sockaddr_in);
358

359 360 361
	so = socreate(slirp);
	if (!so) {
	    return NULL;
B
bellard 已提交
362
	}
K
Kevin Wolf 已提交
363
	so->s = qemu_socket(AF_INET,SOCK_DGRAM,0);
B
bellard 已提交
364
	so->so_expire = curtime + SO_EXPIRE;
365
	insque(so, &slirp->udb);
B
bellard 已提交
366 367

	addr.sin_family = AF_INET;
368 369
	addr.sin_addr.s_addr = haddr;
	addr.sin_port = hport;
B
bellard 已提交
370 371 372 373 374

	if (bind(so->s,(struct sockaddr *)&addr, addrlen) < 0) {
		udp_detach(so);
		return NULL;
	}
375
	socket_set_fast_reuse(so->s);
376

B
bellard 已提交
377 378
	getsockname(so->s,(struct sockaddr *)&addr,&addrlen);
	so->so_fport = addr.sin_port;
379 380
	if (addr.sin_addr.s_addr == 0 ||
	    addr.sin_addr.s_addr == loopback_addr.s_addr) {
381
	   so->so_faddr = slirp->vhost_addr;
382
	} else {
B
bellard 已提交
383
	   so->so_faddr = addr.sin_addr;
384
	}
B
bellard 已提交
385 386 387 388
	so->so_lport = lport;
	so->so_laddr.s_addr = laddr;
	if (flags != SS_FACCEPTONCE)
	   so->so_expire = 0;
389

390
	so->so_state &= SS_PERSISTENT_MASK;
391
	so->so_state |= SS_ISFCONNECTED | flags;
392

B
bellard 已提交
393 394
	return so;
}