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