2 * Copyright (c) 1982, 1986, 1988, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
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
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
34 * Changes and additions relating to SLiRP
35 * Copyright (c) 1995 Danny Gasparovski.
37 * Please read the file COPYRIGHT for the
38 * terms and conditions of the copyright.
44 static uint8_t udp_tos(struct socket
*so
);
47 udp_init(Slirp
*slirp
)
49 slirp
->udb
.so_next
= slirp
->udb
.so_prev
= &slirp
->udb
;
50 slirp
->udp_last_so
= &slirp
->udb
;
53 void udp_cleanup(Slirp
*slirp
)
55 while (slirp
->udb
.so_next
!= &slirp
->udb
) {
56 udp_detach(slirp
->udb
.so_next
);
60 /* m->m_data points at ip packet header
61 * m->m_len length ip packet
62 * ip->ip_len length data (IPDU)
65 udp_input(register struct mbuf
*m
, int iphlen
)
67 Slirp
*slirp
= m
->slirp
;
68 register struct ip
*ip
;
69 register struct udphdr
*uh
;
74 DEBUG_CALL("udp_input");
75 DEBUG_ARG("m = %p", m
);
76 DEBUG_ARG("iphlen = %d", iphlen
);
79 * Strip IP options, if any; should skip this,
80 * make available to user, and use on returned packets,
81 * but we don't yet have a way to check the checksum
82 * with options still present.
84 if(iphlen
> sizeof(struct ip
)) {
85 ip_stripoptions(m
, (struct mbuf
*)0);
86 iphlen
= sizeof(struct ip
);
90 * Get IP and UDP header together in first mbuf.
92 ip
= mtod(m
, struct ip
*);
93 uh
= (struct udphdr
*)((caddr_t
)ip
+ iphlen
);
96 * Make mbuf data length reflect UDP length.
97 * If not enough data to reflect UDP length, drop.
99 len
= ntohs((uint16_t)uh
->uh_ulen
);
101 if (ip
->ip_len
!= len
) {
102 if (len
> ip
->ip_len
) {
105 m_adj(m
, len
- ip
->ip_len
);
110 * Save a copy of the IP header in case we want restore it
111 * for sending an ICMP error message in response.
114 save_ip
.ip_len
+= iphlen
; /* tcp_input subtracts this */
117 * Checksum extended UDP header and data.
120 memset(&((struct ipovly
*)ip
)->ih_mbuf
, 0, sizeof(struct mbuf_ptr
));
121 ((struct ipovly
*)ip
)->ih_x1
= 0;
122 ((struct ipovly
*)ip
)->ih_len
= uh
->uh_ulen
;
123 if(cksum(m
, len
+ sizeof(struct ip
))) {
131 if (ntohs(uh
->uh_dport
) == BOOTP_SERVER
&&
132 (ip
->ip_dst
.s_addr
== slirp
->vhost_addr
.s_addr
||
133 ip
->ip_dst
.s_addr
== 0xffffffff)) {
141 if (ntohs(uh
->uh_dport
) == TFTP_SERVER
&&
142 ip
->ip_dst
.s_addr
== slirp
->vhost_addr
.s_addr
) {
147 if (slirp
->restricted
) {
152 * Locate pcb for datagram.
154 so
= solookup(&slirp
->udp_last_so
, &slirp
->udb
,
155 ip
->ip_src
, uh
->uh_sport
, (struct in_addr
) {0}, 0);
159 * If there's no socket for this packet,
162 so
= socreate(slirp
);
166 if(udp_attach(so
) == -1) {
167 DEBUG_MISC((dfd
," udp_attach errno = %d-%s\n",
168 errno
,strerror(errno
)));
176 so
->so_lfamily
= AF_INET
;
177 so
->so_laddr
= ip
->ip_src
;
178 so
->so_lport
= uh
->uh_sport
;
180 if ((so
->so_iptos
= udp_tos(so
)) == 0)
181 so
->so_iptos
= ip
->ip_tos
;
184 * XXXXX Here, check if it's in udpexec_list,
185 * and if it is, do the fork_exec() etc.
189 so
->so_ffamily
= AF_INET
;
190 so
->so_faddr
= ip
->ip_dst
; /* XXX */
191 so
->so_fport
= uh
->uh_dport
; /* XXX */
193 iphlen
+= sizeof(struct udphdr
);
198 * Now we sendto() the packet.
200 if(sosendto(so
,m
) == -1) {
204 DEBUG_MISC((dfd
,"udp tx errno = %d-%s\n",errno
,strerror(errno
)));
205 icmp_error(m
, ICMP_UNREACH
,ICMP_UNREACH_NET
, 0,strerror(errno
));
209 m_free(so
->so_m
); /* used for ICMP if error on sorecvfrom */
211 /* restore the orig mbuf packet */
215 so
->so_m
=m
; /* ICMP backup */
222 int udp_output(struct socket
*so
, struct mbuf
*m
,
223 struct sockaddr_in
*saddr
, struct sockaddr_in
*daddr
,
226 register struct udpiphdr
*ui
;
229 DEBUG_CALL("udp_output");
230 DEBUG_ARG("so = %p", so
);
231 DEBUG_ARG("m = %p", m
);
232 DEBUG_ARG("saddr = %lx", (long)saddr
->sin_addr
.s_addr
);
233 DEBUG_ARG("daddr = %lx", (long)daddr
->sin_addr
.s_addr
);
238 m
->m_data
-= sizeof(struct udpiphdr
);
239 m
->m_len
+= sizeof(struct udpiphdr
);
242 * Fill in mbuf with extended UDP header
243 * and addresses and length put into network format.
245 ui
= mtod(m
, struct udpiphdr
*);
246 memset(&ui
->ui_i
.ih_mbuf
, 0 , sizeof(struct mbuf_ptr
));
248 ui
->ui_pr
= IPPROTO_UDP
;
249 ui
->ui_len
= htons(m
->m_len
- sizeof(struct ip
));
250 /* XXXXX Check for from-one-location sockets, or from-any-location sockets */
251 ui
->ui_src
= saddr
->sin_addr
;
252 ui
->ui_dst
= daddr
->sin_addr
;
253 ui
->ui_sport
= saddr
->sin_port
;
254 ui
->ui_dport
= daddr
->sin_port
;
255 ui
->ui_ulen
= ui
->ui_len
;
258 * Stuff checksum and output datagram.
261 if ((ui
->ui_sum
= cksum(m
, m
->m_len
)) == 0)
263 ((struct ip
*)ui
)->ip_len
= m
->m_len
;
265 ((struct ip
*)ui
)->ip_ttl
= IPDEFTTL
;
266 ((struct ip
*)ui
)->ip_tos
= iptos
;
268 error
= ip_output(so
, m
);
274 udp_attach(struct socket
*so
)
276 if((so
->s
= qemu_socket(AF_INET
,SOCK_DGRAM
,0)) != -1) {
277 so
->so_expire
= curtime
+ SO_EXPIRE
;
278 insque(so
, &so
->slirp
->udb
);
284 udp_detach(struct socket
*so
)
290 static const struct tos_t udptos
[] = {
291 {0, 53, IPTOS_LOWDELAY
, 0}, /* DNS */
296 udp_tos(struct socket
*so
)
300 while(udptos
[i
].tos
) {
301 if ((udptos
[i
].fport
&& ntohs(so
->so_fport
) == udptos
[i
].fport
) ||
302 (udptos
[i
].lport
&& ntohs(so
->so_lport
) == udptos
[i
].lport
)) {
303 so
->so_emu
= udptos
[i
].emu
;
304 return udptos
[i
].tos
;
313 udp_listen(Slirp
*slirp
, uint32_t haddr
, u_int hport
, uint32_t laddr
,
314 u_int lport
, int flags
)
316 struct sockaddr_in addr
;
318 socklen_t addrlen
= sizeof(struct sockaddr_in
);
320 so
= socreate(slirp
);
324 so
->s
= qemu_socket(AF_INET
,SOCK_DGRAM
,0);
325 so
->so_expire
= curtime
+ SO_EXPIRE
;
326 insque(so
, &slirp
->udb
);
328 addr
.sin_family
= AF_INET
;
329 addr
.sin_addr
.s_addr
= haddr
;
330 addr
.sin_port
= hport
;
332 if (bind(so
->s
,(struct sockaddr
*)&addr
, addrlen
) < 0) {
336 socket_set_fast_reuse(so
->s
);
338 getsockname(so
->s
,(struct sockaddr
*)&addr
,&addrlen
);
339 so
->fhost
.sin
= addr
;
340 sotranslate_accept(so
);
341 so
->so_lfamily
= AF_INET
;
342 so
->so_lport
= lport
;
343 so
->so_laddr
.s_addr
= laddr
;
344 if (flags
!= SS_FACCEPTONCE
)
347 so
->so_state
&= SS_PERSISTENT_MASK
;
348 so
->so_state
|= SS_ISFCONNECTED
| flags
;