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