]> Git Repo - qemu.git/blob - slirp/udp.c
slirp: Factorizing tcpiphdr structure with an union
[qemu.git] / slirp / udp.c
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.
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.
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.
36  *
37  * Please read the file COPYRIGHT for the
38  * terms and conditions of the copyright.
39  */
40
41 #include "qemu/osdep.h"
42 #include <slirp.h>
43 #include "ip_icmp.h"
44
45 static uint8_t udp_tos(struct socket *so);
46
47 void
48 udp_init(Slirp *slirp)
49 {
50     slirp->udb.so_next = slirp->udb.so_prev = &slirp->udb;
51     slirp->udp_last_so = &slirp->udb;
52 }
53
54 void udp_cleanup(Slirp *slirp)
55 {
56     while (slirp->udb.so_next != &slirp->udb) {
57         udp_detach(slirp->udb.so_next);
58     }
59 }
60
61 /* m->m_data  points at ip packet header
62  * m->m_len   length ip packet
63  * ip->ip_len length data (IPDU)
64  */
65 void
66 udp_input(register struct mbuf *m, int iphlen)
67 {
68         Slirp *slirp = m->slirp;
69         register struct ip *ip;
70         register struct udphdr *uh;
71         int len;
72         struct ip save_ip;
73         struct socket *so;
74         struct sockaddr_storage lhost;
75         struct sockaddr_in *lhost4;
76
77         DEBUG_CALL("udp_input");
78         DEBUG_ARG("m = %p", m);
79         DEBUG_ARG("iphlen = %d", iphlen);
80
81         /*
82          * Strip IP options, if any; should skip this,
83          * make available to user, and use on returned packets,
84          * but we don't yet have a way to check the checksum
85          * with options still present.
86          */
87         if(iphlen > sizeof(struct ip)) {
88                 ip_stripoptions(m, (struct mbuf *)0);
89                 iphlen = sizeof(struct ip);
90         }
91
92         /*
93          * Get IP and UDP header together in first mbuf.
94          */
95         ip = mtod(m, struct ip *);
96         uh = (struct udphdr *)((caddr_t)ip + iphlen);
97
98         /*
99          * Make mbuf data length reflect UDP length.
100          * If not enough data to reflect UDP length, drop.
101          */
102         len = ntohs((uint16_t)uh->uh_ulen);
103
104         if (ip->ip_len != len) {
105                 if (len > ip->ip_len) {
106                         goto bad;
107                 }
108                 m_adj(m, len - ip->ip_len);
109                 ip->ip_len = len;
110         }
111
112         /*
113          * Save a copy of the IP header in case we want restore it
114          * for sending an ICMP error message in response.
115          */
116         save_ip = *ip;
117         save_ip.ip_len+= iphlen;         /* tcp_input subtracts this */
118
119         /*
120          * Checksum extended UDP header and data.
121          */
122         if (uh->uh_sum) {
123       memset(&((struct ipovly *)ip)->ih_mbuf, 0, sizeof(struct mbuf_ptr));
124           ((struct ipovly *)ip)->ih_x1 = 0;
125           ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
126           if(cksum(m, len + sizeof(struct ip))) {
127             goto bad;
128           }
129         }
130
131         /*
132          *  handle DHCP/BOOTP
133          */
134         if (ntohs(uh->uh_dport) == BOOTP_SERVER &&
135             (ip->ip_dst.s_addr == slirp->vhost_addr.s_addr ||
136              ip->ip_dst.s_addr == 0xffffffff)) {
137                 bootp_input(m);
138                 goto bad;
139             }
140
141         /*
142          *  handle TFTP
143          */
144         if (ntohs(uh->uh_dport) == TFTP_SERVER &&
145             ip->ip_dst.s_addr == slirp->vhost_addr.s_addr) {
146             tftp_input(m);
147             goto bad;
148         }
149
150         if (slirp->restricted) {
151             goto bad;
152         }
153
154         /*
155          * Locate pcb for datagram.
156          */
157         lhost.ss_family = AF_INET;
158         lhost4 = (struct sockaddr_in *) &lhost;
159         lhost4->sin_addr = ip->ip_src;
160         lhost4->sin_port = uh->uh_sport;
161
162         so = solookup(&slirp->udp_last_so, &slirp->udb, &lhost, NULL);
163
164         if (so == NULL) {
165           /*
166            * If there's no socket for this packet,
167            * create one
168            */
169           so = socreate(slirp);
170           if (!so) {
171               goto bad;
172           }
173           if (udp_attach(so, AF_INET) == -1) {
174             DEBUG_MISC((dfd," udp_attach errno = %d-%s\n",
175                         errno,strerror(errno)));
176             sofree(so);
177             goto bad;
178           }
179
180           /*
181            * Setup fields
182            */
183           so->so_lfamily = AF_INET;
184           so->so_laddr = ip->ip_src;
185           so->so_lport = uh->uh_sport;
186
187           if ((so->so_iptos = udp_tos(so)) == 0)
188             so->so_iptos = ip->ip_tos;
189
190           /*
191            * XXXXX Here, check if it's in udpexec_list,
192            * and if it is, do the fork_exec() etc.
193            */
194         }
195
196         so->so_ffamily = AF_INET;
197         so->so_faddr = ip->ip_dst; /* XXX */
198         so->so_fport = uh->uh_dport; /* XXX */
199
200         iphlen += sizeof(struct udphdr);
201         m->m_len -= iphlen;
202         m->m_data += iphlen;
203
204         /*
205          * Now we sendto() the packet.
206          */
207         if(sosendto(so,m) == -1) {
208           m->m_len += iphlen;
209           m->m_data -= iphlen;
210           *ip=save_ip;
211           DEBUG_MISC((dfd,"udp tx errno = %d-%s\n",errno,strerror(errno)));
212           icmp_send_error(m, ICMP_UNREACH, ICMP_UNREACH_NET, 0,
213                           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
230 int udp_output(struct socket *so, struct mbuf *m,
231                 struct sockaddr_in *saddr, struct sockaddr_in *daddr,
232                 int iptos)
233 {
234         register struct udpiphdr *ui;
235         int error = 0;
236
237         DEBUG_CALL("udp_output");
238         DEBUG_ARG("so = %p", so);
239         DEBUG_ARG("m = %p", m);
240         DEBUG_ARG("saddr = %lx", (long)saddr->sin_addr.s_addr);
241         DEBUG_ARG("daddr = %lx", (long)daddr->sin_addr.s_addr);
242
243         /*
244          * Adjust for header
245          */
246         m->m_data -= sizeof(struct udpiphdr);
247         m->m_len += sizeof(struct udpiphdr);
248
249         /*
250          * Fill in mbuf with extended UDP header
251          * and addresses and length put into network format.
252          */
253         ui = mtod(m, struct udpiphdr *);
254     memset(&ui->ui_i.ih_mbuf, 0 , sizeof(struct mbuf_ptr));
255         ui->ui_x1 = 0;
256         ui->ui_pr = IPPROTO_UDP;
257         ui->ui_len = htons(m->m_len - sizeof(struct ip));
258         /* XXXXX Check for from-one-location sockets, or from-any-location sockets */
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
281 int
282 udp_attach(struct socket *so, unsigned short af)
283 {
284   so->s = qemu_socket(af, SOCK_DGRAM, 0);
285   if (so->s != -1) {
286     so->so_expire = curtime + SO_EXPIRE;
287     insque(so, &so->slirp->udb);
288   }
289   return(so->s);
290 }
291
292 void
293 udp_detach(struct socket *so)
294 {
295         closesocket(so->s);
296         sofree(so);
297 }
298
299 static const struct tos_t udptos[] = {
300         {0, 53, IPTOS_LOWDELAY, 0},                     /* DNS */
301         {0, 0, 0, 0}
302 };
303
304 static uint8_t
305 udp_tos(struct socket *so)
306 {
307         int i = 0;
308
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)) {
312                         so->so_emu = udptos[i].emu;
313                         return udptos[i].tos;
314                 }
315                 i++;
316         }
317
318         return 0;
319 }
320
321 struct socket *
322 udp_listen(Slirp *slirp, uint32_t haddr, u_int hport, uint32_t laddr,
323            u_int lport, int flags)
324 {
325         struct sockaddr_in addr;
326         struct socket *so;
327         socklen_t addrlen = sizeof(struct sockaddr_in);
328
329         so = socreate(slirp);
330         if (!so) {
331             return NULL;
332         }
333         so->s = qemu_socket(AF_INET,SOCK_DGRAM,0);
334         so->so_expire = curtime + SO_EXPIRE;
335         insque(so, &slirp->udb);
336
337         addr.sin_family = AF_INET;
338         addr.sin_addr.s_addr = haddr;
339         addr.sin_port = hport;
340
341         if (bind(so->s,(struct sockaddr *)&addr, addrlen) < 0) {
342                 udp_detach(so);
343                 return NULL;
344         }
345         socket_set_fast_reuse(so->s);
346
347         getsockname(so->s,(struct sockaddr *)&addr,&addrlen);
348         so->fhost.sin = addr;
349         sotranslate_accept(so);
350         so->so_lfamily = AF_INET;
351         so->so_lport = lport;
352         so->so_laddr.s_addr = laddr;
353         if (flags != SS_FACCEPTONCE)
354            so->so_expire = 0;
355
356         so->so_state &= SS_PERSISTENT_MASK;
357         so->so_state |= SS_ISFCONNECTED | flags;
358
359         return so;
360 }
This page took 0.042566 seconds and 4 git commands to generate.