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. All advertising materials mentioning features or use of this software
|
---|
14 | * must display the following acknowledgement:
|
---|
15 | * This product includes software developed by the University of
|
---|
16 | * California, Berkeley and its contributors.
|
---|
17 | * 4. Neither the name of the University nor the names of its contributors
|
---|
18 | * may be used to endorse or promote products derived from this software
|
---|
19 | * without specific prior written permission.
|
---|
20 | *
|
---|
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
---|
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
---|
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
---|
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
---|
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
---|
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
---|
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
---|
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
---|
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
---|
31 | * SUCH DAMAGE.
|
---|
32 | *
|
---|
33 | * @(#)udp_usrreq.c 8.4 (Berkeley) 1/21/94
|
---|
34 | * udp_usrreq.c,v 1.4 1994/10/02 17:48:45 phk Exp
|
---|
35 | */
|
---|
36 |
|
---|
37 | /*
|
---|
38 | * Changes and additions relating to SLiRP
|
---|
39 | * Copyright (c) 1995 Danny Gasparovski.
|
---|
40 | *
|
---|
41 | * Please read the file COPYRIGHT for the
|
---|
42 | * terms and conditions of the copyright.
|
---|
43 | */
|
---|
44 |
|
---|
45 | #include <slirp.h>
|
---|
46 | #include "ip_icmp.h"
|
---|
47 | #include "ctl.h"
|
---|
48 |
|
---|
49 |
|
---|
50 | /*
|
---|
51 | * UDP protocol implementation.
|
---|
52 | * Per RFC 768, August, 1980.
|
---|
53 | */
|
---|
54 | #define udpcksum 1
|
---|
55 |
|
---|
56 | void
|
---|
57 | udp_init(PNATState pData)
|
---|
58 | {
|
---|
59 | udp_last_so = &udb;
|
---|
60 | udb.so_next = udb.so_prev = &udb;
|
---|
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 | */
|
---|
67 | void
|
---|
68 | udp_input(PNATState pData, register struct mbuf *m, int iphlen)
|
---|
69 | {
|
---|
70 | register struct ip *ip;
|
---|
71 | register struct udphdr *uh;
|
---|
72 | int len;
|
---|
73 | struct ip save_ip;
|
---|
74 | struct socket *so;
|
---|
75 | int ret;
|
---|
76 | int ttl;
|
---|
77 |
|
---|
78 | DEBUG_CALL("udp_input");
|
---|
79 | DEBUG_ARG("m = %lx", (long)m);
|
---|
80 | ip = mtod(m, struct ip *);
|
---|
81 | DEBUG_ARG("iphlen = %d", iphlen);
|
---|
82 | Log2(("%R[IP4] iphlen = %d\n", &ip->ip_dst, iphlen));
|
---|
83 |
|
---|
84 | udpstat.udps_ipackets++;
|
---|
85 |
|
---|
86 | /*
|
---|
87 | * Strip IP options, if any; should skip this,
|
---|
88 | * make available to user, and use on returned packets,
|
---|
89 | * but we don't yet have a way to check the checksum
|
---|
90 | * with options still present.
|
---|
91 | */
|
---|
92 | if (iphlen > sizeof(struct ip))
|
---|
93 | {
|
---|
94 | ip_stripoptions(m, (struct mbuf *)0);
|
---|
95 | iphlen = sizeof(struct ip);
|
---|
96 | }
|
---|
97 |
|
---|
98 | /*
|
---|
99 | * Get IP and UDP header together in first mbuf.
|
---|
100 | */
|
---|
101 | ip = mtod(m, struct ip *);
|
---|
102 | uh = (struct udphdr *)((caddr_t)ip + iphlen);
|
---|
103 |
|
---|
104 | /*
|
---|
105 | * Make mbuf data length reflect UDP length.
|
---|
106 | * If not enough data to reflect UDP length, drop.
|
---|
107 | */
|
---|
108 | len = ntohs((u_int16_t)uh->uh_ulen);
|
---|
109 |
|
---|
110 | if (ip->ip_len != len)
|
---|
111 | {
|
---|
112 | if (len > ip->ip_len)
|
---|
113 | {
|
---|
114 | udpstat.udps_badlen++;
|
---|
115 | goto bad;
|
---|
116 | }
|
---|
117 | m_adj(m, len - ip->ip_len);
|
---|
118 | ip->ip_len = len;
|
---|
119 | }
|
---|
120 |
|
---|
121 | /*
|
---|
122 | * Save a copy of the IP header in case we want restore it
|
---|
123 | * for sending an ICMP error message in response.
|
---|
124 | */
|
---|
125 | save_ip = *ip;
|
---|
126 | save_ip.ip_len+= iphlen; /* tcp_input subtracts this */
|
---|
127 |
|
---|
128 | /*
|
---|
129 | * Checksum extended UDP header and data.
|
---|
130 | */
|
---|
131 | if (udpcksum && uh->uh_sum)
|
---|
132 | {
|
---|
133 | memset(((struct ipovly *)ip)->ih_x1, 0, 9);
|
---|
134 | ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
|
---|
135 | #if 0
|
---|
136 | /* keep uh_sum for ICMP reply */
|
---|
137 | uh->uh_sum = cksum(m, len + sizeof (struct ip));
|
---|
138 | if (uh->uh_sum)
|
---|
139 | {
|
---|
140 |
|
---|
141 | #endif
|
---|
142 | if(cksum(m, len + sizeof(struct ip)))
|
---|
143 | {
|
---|
144 | udpstat.udps_badsum++;
|
---|
145 | goto bad;
|
---|
146 | }
|
---|
147 | }
|
---|
148 | #if 0
|
---|
149 | }
|
---|
150 | #endif
|
---|
151 |
|
---|
152 | /*
|
---|
153 | * handle DHCP/BOOTP
|
---|
154 | */
|
---|
155 | if (ntohs(uh->uh_dport) == BOOTP_SERVER)
|
---|
156 | {
|
---|
157 | bootp_input(pData, m);
|
---|
158 | goto bad;
|
---|
159 | }
|
---|
160 |
|
---|
161 | /*
|
---|
162 | * handle TFTP
|
---|
163 | */
|
---|
164 | if ( ntohs(uh->uh_dport) == TFTP_SERVER
|
---|
165 | && CTL_CHECK(ntohl(ip->ip_dst.s_addr), CTL_TFTP))
|
---|
166 | {
|
---|
167 | tftp_input(pData, m);
|
---|
168 | goto bad;
|
---|
169 | }
|
---|
170 |
|
---|
171 | /*
|
---|
172 | * Locate pcb for datagram.
|
---|
173 | */
|
---|
174 | so = udp_last_so;
|
---|
175 | if ( so->so_lport != uh->uh_sport
|
---|
176 | || so->so_laddr.s_addr != ip->ip_src.s_addr)
|
---|
177 | {
|
---|
178 | struct socket *tmp;
|
---|
179 |
|
---|
180 | for (tmp = udb.so_next; tmp != &udb; tmp = tmp->so_next)
|
---|
181 | {
|
---|
182 | if ( tmp->so_lport == uh->uh_sport
|
---|
183 | && tmp->so_laddr.s_addr == ip->ip_src.s_addr)
|
---|
184 | {
|
---|
185 | so = tmp;
|
---|
186 | break;
|
---|
187 | }
|
---|
188 | }
|
---|
189 | if (tmp == &udb)
|
---|
190 | so = NULL;
|
---|
191 | else
|
---|
192 | {
|
---|
193 | udpstat.udpps_pcbcachemiss++;
|
---|
194 | udp_last_so = so;
|
---|
195 | }
|
---|
196 | }
|
---|
197 |
|
---|
198 | if (so == NULL)
|
---|
199 | {
|
---|
200 | /*
|
---|
201 | * If there's no socket for this packet,
|
---|
202 | * create one
|
---|
203 | */
|
---|
204 | if ((so = socreate()) == NULL)
|
---|
205 | goto bad;
|
---|
206 | if (udp_attach(pData, so, slirp_get_service(IPPROTO_UDP, uh->uh_dport, uh->uh_sport)) == -1)
|
---|
207 | {
|
---|
208 | DEBUG_MISC((dfd," udp_attach errno = %d-%s\n",
|
---|
209 | errno, strerror(errno)));
|
---|
210 | sofree(pData, so);
|
---|
211 | goto bad;
|
---|
212 | }
|
---|
213 |
|
---|
214 | /*
|
---|
215 | * Setup fields
|
---|
216 | */
|
---|
217 | /* udp_last_so = so; */
|
---|
218 | so->so_laddr = ip->ip_src;
|
---|
219 | so->so_lport = uh->uh_sport;
|
---|
220 |
|
---|
221 | if ((so->so_iptos = udp_tos(so)) == 0)
|
---|
222 | so->so_iptos = ip->ip_tos;
|
---|
223 |
|
---|
224 | /*
|
---|
225 | * XXXXX Here, check if it's in udpexec_list,
|
---|
226 | * and if it is, do the fork_exec() etc.
|
---|
227 | */
|
---|
228 | }
|
---|
229 |
|
---|
230 | so->so_faddr = ip->ip_dst; /* XXX */
|
---|
231 | so->so_fport = uh->uh_dport; /* XXX */
|
---|
232 |
|
---|
233 | #ifdef VBOX_WITH_SLIRP_DNS_PROXY
|
---|
234 | if ( (ip->ip_dst.s_addr == htonl(ntohl(special_addr.s_addr) | CTL_DNS))
|
---|
235 | && (ntohs(uh->uh_dport) == 53))
|
---|
236 | {
|
---|
237 | dnsproxy_query(pData, so, m, iphlen);
|
---|
238 | goto bad; /* it isn't bad, probably better to add additional label done for boot/tftf :) */
|
---|
239 | }
|
---|
240 | #endif
|
---|
241 |
|
---|
242 | iphlen += sizeof(struct udphdr);
|
---|
243 | m->m_len -= iphlen;
|
---|
244 | m->m_data += iphlen;
|
---|
245 |
|
---|
246 | /*
|
---|
247 | * Now we sendto() the packet.
|
---|
248 | */
|
---|
249 | if (so->so_emu)
|
---|
250 | udp_emu(pData, so, m);
|
---|
251 |
|
---|
252 | ttl = ip->ip_ttl = save_ip.ip_ttl;
|
---|
253 | ret = setsockopt(so->s, IPPROTO_IP, IP_TTL, (const char*)&ttl, sizeof(ttl));
|
---|
254 | if (ret < 0)
|
---|
255 | LogRel(("NAT: Error (%s) occurred while setting TTL(%d) attribute "
|
---|
256 | "of IP packet to socket %R[natsock]\n", strerror(errno), ip->ip_ttl, so));
|
---|
257 |
|
---|
258 | if (sosendto(pData, so, m) == -1)
|
---|
259 | {
|
---|
260 | m->m_len += iphlen;
|
---|
261 | m->m_data -= iphlen;
|
---|
262 | *ip = save_ip;
|
---|
263 | DEBUG_MISC((dfd,"NAT: UDP tx errno = %d-%s (on sent to %R[IP4])\n", errno,
|
---|
264 | strerror(errno), &ip->ip_dst));
|
---|
265 | icmp_error(pData, m, ICMP_UNREACH, ICMP_UNREACH_NET, 0, strerror(errno));
|
---|
266 | /* in case we receive ICMP on this socket we'll aware that ICMP has been already sent to host*/
|
---|
267 | so->so_m = NULL;
|
---|
268 | }
|
---|
269 |
|
---|
270 | m_free(pData, so->so_m); /* used for ICMP if error on sorecvfrom */
|
---|
271 |
|
---|
272 | /* restore the orig mbuf packet */
|
---|
273 | m->m_len += iphlen;
|
---|
274 | m->m_data -= iphlen;
|
---|
275 | *ip = save_ip;
|
---|
276 | so->so_m = m; /* ICMP backup */
|
---|
277 |
|
---|
278 | return;
|
---|
279 |
|
---|
280 | bad:
|
---|
281 | Log2(("NAT: UDP datagram to %R[IP4] with size(%d) claimed as bad\n",
|
---|
282 | &ip->ip_dst, ip->ip_len));
|
---|
283 | m_freem(pData, m);
|
---|
284 | return;
|
---|
285 | }
|
---|
286 |
|
---|
287 | int udp_output2(PNATState pData, struct socket *so, struct mbuf *m,
|
---|
288 | struct sockaddr_in *saddr, struct sockaddr_in *daddr,
|
---|
289 | int iptos)
|
---|
290 | {
|
---|
291 | register struct udpiphdr *ui;
|
---|
292 | int error = 0;
|
---|
293 |
|
---|
294 | DEBUG_CALL("udp_output");
|
---|
295 | DEBUG_ARG("so = %lx", (long)so);
|
---|
296 | DEBUG_ARG("m = %lx", (long)m);
|
---|
297 | DEBUG_ARG("saddr = %lx", (long)saddr->sin_addr.s_addr);
|
---|
298 | DEBUG_ARG("daddr = %lx", (long)daddr->sin_addr.s_addr);
|
---|
299 |
|
---|
300 | /*
|
---|
301 | * Adjust for header
|
---|
302 | */
|
---|
303 | m->m_data -= sizeof(struct udpiphdr);
|
---|
304 | m->m_len += sizeof(struct udpiphdr);
|
---|
305 |
|
---|
306 | /*
|
---|
307 | * Fill in mbuf with extended UDP header
|
---|
308 | * and addresses and length put into network format.
|
---|
309 | */
|
---|
310 | ui = mtod(m, struct udpiphdr *);
|
---|
311 | memset(ui->ui_x1, 0, 9);
|
---|
312 | ui->ui_pr = IPPROTO_UDP;
|
---|
313 | ui->ui_len = htons(m->m_len - sizeof(struct ip)); /* + sizeof (struct udphdr)); */
|
---|
314 | /* XXXXX Check for from-one-location sockets, or from-any-location sockets */
|
---|
315 | ui->ui_src = saddr->sin_addr;
|
---|
316 | ui->ui_dst = daddr->sin_addr;
|
---|
317 | ui->ui_sport = saddr->sin_port;
|
---|
318 | ui->ui_dport = daddr->sin_port;
|
---|
319 | ui->ui_ulen = ui->ui_len;
|
---|
320 |
|
---|
321 | /*
|
---|
322 | * Stuff checksum and output datagram.
|
---|
323 | */
|
---|
324 | ui->ui_sum = 0;
|
---|
325 | if (udpcksum)
|
---|
326 | {
|
---|
327 | if ((ui->ui_sum = cksum(m, /* sizeof (struct udpiphdr) + */ m->m_len)) == 0)
|
---|
328 | ui->ui_sum = 0xffff;
|
---|
329 | }
|
---|
330 | ((struct ip *)ui)->ip_len = m->m_len;
|
---|
331 | ((struct ip *)ui)->ip_ttl = ip_defttl;
|
---|
332 | ((struct ip *)ui)->ip_tos = iptos;
|
---|
333 |
|
---|
334 | udpstat.udps_opackets++;
|
---|
335 |
|
---|
336 | error = ip_output(pData, so, m);
|
---|
337 |
|
---|
338 | return error;
|
---|
339 | }
|
---|
340 |
|
---|
341 | int udp_output(PNATState pData, struct socket *so, struct mbuf *m,
|
---|
342 | struct sockaddr_in *addr)
|
---|
343 | {
|
---|
344 | struct sockaddr_in saddr, daddr;
|
---|
345 |
|
---|
346 | saddr = *addr;
|
---|
347 | if ((so->so_faddr.s_addr & htonl(pData->netmask)) == special_addr.s_addr)
|
---|
348 | {
|
---|
349 | saddr.sin_addr.s_addr = so->so_faddr.s_addr;
|
---|
350 | if ((so->so_faddr.s_addr & htonl(~pData->netmask)) == htonl(~pData->netmask))
|
---|
351 | saddr.sin_addr.s_addr = alias_addr.s_addr;
|
---|
352 | }
|
---|
353 |
|
---|
354 | /* Any UDP packet to the loopback address must be translated to be from
|
---|
355 | * the forwarding address, i.e. 10.0.2.2. */
|
---|
356 | if ( (saddr.sin_addr.s_addr & htonl(IN_CLASSA_NET))
|
---|
357 | == htonl(INADDR_LOOPBACK & IN_CLASSA_NET))
|
---|
358 | saddr.sin_addr.s_addr = alias_addr.s_addr;
|
---|
359 |
|
---|
360 | daddr.sin_addr = so->so_laddr;
|
---|
361 | daddr.sin_port = so->so_lport;
|
---|
362 |
|
---|
363 | return udp_output2(pData, so, m, &saddr, &daddr, so->so_iptos);
|
---|
364 | }
|
---|
365 |
|
---|
366 | int
|
---|
367 | udp_attach(PNATState pData, struct socket *so, int service_port)
|
---|
368 | {
|
---|
369 | struct sockaddr_in addr;
|
---|
370 | struct sockaddr sa_addr;
|
---|
371 | socklen_t socklen = sizeof(struct sockaddr);
|
---|
372 | int status;
|
---|
373 |
|
---|
374 | if ((so->s = socket(AF_INET, SOCK_DGRAM, 0)) != -1)
|
---|
375 | {
|
---|
376 | /*
|
---|
377 | * Here, we bind() the socket. Although not really needed
|
---|
378 | * (sendto() on an unbound socket will bind it), it's done
|
---|
379 | * here so that emulation of ytalk etc. don't have to do it
|
---|
380 | */
|
---|
381 | addr.sin_family = AF_INET;
|
---|
382 | addr.sin_port = service_port;
|
---|
383 | addr.sin_addr.s_addr = INADDR_ANY;
|
---|
384 | fd_nonblock(so->s);
|
---|
385 | if (bind(so->s, (struct sockaddr *)&addr, sizeof(addr)) < 0)
|
---|
386 | {
|
---|
387 | int lasterrno = errno;
|
---|
388 | closesocket(so->s);
|
---|
389 | so->s = -1;
|
---|
390 | #ifdef RT_OS_WINDOWS
|
---|
391 | WSASetLastError(lasterrno);
|
---|
392 | #else
|
---|
393 | errno = lasterrno;
|
---|
394 | #endif
|
---|
395 | }
|
---|
396 | else
|
---|
397 | {
|
---|
398 | int opt = 1;
|
---|
399 | /* success, insert in queue */
|
---|
400 | so->so_expire = curtime + SO_EXPIRE;
|
---|
401 | /* enable broadcast for later use */
|
---|
402 | setsockopt(so->s, SOL_SOCKET, SO_BROADCAST, (const char *)&opt, sizeof(opt));
|
---|
403 | status = getsockname(so->s, &sa_addr, &socklen);
|
---|
404 | Assert(status == 0 && sa_addr.sa_family == AF_INET);
|
---|
405 | so->so_hlport = ((struct sockaddr_in *)&sa_addr)->sin_port;
|
---|
406 | so->so_hladdr.s_addr = ((struct sockaddr_in *)&sa_addr)->sin_addr.s_addr;
|
---|
407 | SOCKET_LOCK_CREATE(so);
|
---|
408 | QSOCKET_LOCK(udb);
|
---|
409 | insque(pData, so, &udb);
|
---|
410 | NSOCK_INC();
|
---|
411 | QSOCKET_UNLOCK(udb);
|
---|
412 | }
|
---|
413 | }
|
---|
414 | else
|
---|
415 | {
|
---|
416 | LogRel(("NAT: can't create datagramm socket\n"));
|
---|
417 | }
|
---|
418 | return so->s;
|
---|
419 | }
|
---|
420 |
|
---|
421 | void
|
---|
422 | udp_detach(PNATState pData, struct socket *so)
|
---|
423 | {
|
---|
424 | if (so != &pData->icmp_socket)
|
---|
425 | {
|
---|
426 | QSOCKET_LOCK(udb);
|
---|
427 | SOCKET_LOCK(so);
|
---|
428 | QSOCKET_UNLOCK(udb);
|
---|
429 | closesocket(so->s);
|
---|
430 | sofree(pData, so);
|
---|
431 | SOCKET_UNLOCK(so);
|
---|
432 | }
|
---|
433 | }
|
---|
434 |
|
---|
435 | static const struct tos_t udptos[] =
|
---|
436 | {
|
---|
437 | { 0, 53, IPTOS_LOWDELAY, 0 }, /* DNS */
|
---|
438 | { 517, 517, IPTOS_LOWDELAY, EMU_TALK }, /* talk */
|
---|
439 | { 518, 518, IPTOS_LOWDELAY, EMU_NTALK }, /* ntalk */
|
---|
440 | { 0, 7648, IPTOS_LOWDELAY, EMU_CUSEEME }, /* Cu-Seeme */
|
---|
441 | { 0, 0, 0, 0 }
|
---|
442 | };
|
---|
443 |
|
---|
444 | u_int8_t
|
---|
445 | udp_tos(struct socket *so)
|
---|
446 | {
|
---|
447 | int i = 0;
|
---|
448 |
|
---|
449 | while(udptos[i].tos)
|
---|
450 | {
|
---|
451 | if ( (udptos[i].fport && ntohs(so->so_fport) == udptos[i].fport)
|
---|
452 | || (udptos[i].lport && ntohs(so->so_lport) == udptos[i].lport))
|
---|
453 | {
|
---|
454 | so->so_emu = udptos[i].emu;
|
---|
455 | return udptos[i].tos;
|
---|
456 | }
|
---|
457 | i++;
|
---|
458 | }
|
---|
459 |
|
---|
460 | return 0;
|
---|
461 | }
|
---|
462 |
|
---|
463 | #ifdef EMULATE_TALK
|
---|
464 | #include "talkd.h"
|
---|
465 | #endif
|
---|
466 |
|
---|
467 | /*
|
---|
468 | * Here, talk/ytalk/ntalk requests must be emulated
|
---|
469 | */
|
---|
470 | void
|
---|
471 | udp_emu(PNATState pData, struct socket *so, struct mbuf *m)
|
---|
472 | {
|
---|
473 | #ifndef VBOX_WITH_SLIRP_ALIAS
|
---|
474 | struct sockaddr_in addr;
|
---|
475 | socklen_t addrlen = sizeof(addr);
|
---|
476 | #ifdef EMULATE_TALK
|
---|
477 | CTL_MSG_OLD *omsg;
|
---|
478 | CTL_MSG *nmsg;
|
---|
479 | char buff[sizeof(CTL_MSG)];
|
---|
480 | u_char type;
|
---|
481 |
|
---|
482 | struct talk_request
|
---|
483 | {
|
---|
484 | struct talk_request *next;
|
---|
485 | struct socket *udp_so;
|
---|
486 | struct socket *tcp_so;
|
---|
487 | } *req;
|
---|
488 |
|
---|
489 | static struct talk_request *req_tbl = 0;
|
---|
490 |
|
---|
491 | #endif
|
---|
492 |
|
---|
493 | struct cu_header
|
---|
494 | {
|
---|
495 | uint16_t d_family; /* destination family */
|
---|
496 | uint16_t d_port; /* destination port */
|
---|
497 | uint32_t d_addr; /* destination address */
|
---|
498 | uint16_t s_family; /* source family */
|
---|
499 | uint16_t s_port; /* source port */
|
---|
500 | uint32_t so_addr; /* source address */
|
---|
501 | uint32_t seqn; /* sequence number */
|
---|
502 | uint16_t message; /* message */
|
---|
503 | uint16_t data_type; /* data type */
|
---|
504 | uint16_t pkt_len; /* packet length */
|
---|
505 | } *cu_head;
|
---|
506 |
|
---|
507 | switch(so->so_emu)
|
---|
508 | {
|
---|
509 | #ifdef EMULATE_TALK
|
---|
510 | case EMU_TALK:
|
---|
511 | case EMU_NTALK:
|
---|
512 | /*
|
---|
513 | * Talk emulation. We always change the ctl_addr to get
|
---|
514 | * some answers from the daemon. When an ANNOUNCE comes,
|
---|
515 | * we send LEAVE_INVITE to the local daemons. Also when a
|
---|
516 | * DELETE comes, we send copies to the local daemons.
|
---|
517 | */
|
---|
518 | if (getsockname(so->s, (struct sockaddr *)&addr, &addrlen) < 0)
|
---|
519 | return;
|
---|
520 |
|
---|
521 | #define IS_OLD (so->so_emu == EMU_TALK)
|
---|
522 |
|
---|
523 | #define COPY_MSG(dest, src) \
|
---|
524 | do { \
|
---|
525 | dest->type = src->type; \
|
---|
526 | dest->id_num = src->id_num; \
|
---|
527 | dest->pid = src->pid; \
|
---|
528 | dest->addr = src->addr; \
|
---|
529 | dest->ctl_addr = src->ctl_addr; \
|
---|
530 | memcpy(&dest->l_name, &src->l_name, NAME_SIZE_OLD); \
|
---|
531 | memcpy(&dest->r_name, &src->r_name, NAME_SIZE_OLD); \
|
---|
532 | memcpy(&dest->r_tty, &src->r_tty, TTY_SIZE);
|
---|
533 | } while (0)
|
---|
534 |
|
---|
535 | #define OTOSIN(ptr, field) ((struct sockaddr_in *)&ptr->field)
|
---|
536 | /* old_sockaddr to sockaddr_in */
|
---|
537 |
|
---|
538 |
|
---|
539 | if (IS_OLD)
|
---|
540 | {
|
---|
541 | /* old talk */
|
---|
542 | omsg = mtod(m, CTL_MSG_OLD*);
|
---|
543 | nmsg = (CTL_MSG *) buff;
|
---|
544 | type = omsg->type;
|
---|
545 | OTOSIN(omsg, ctl_addr)->sin_port = addr.sin_port;
|
---|
546 | OTOSIN(omsg, ctl_addr)->sin_addr = our_addr;
|
---|
547 | strncpy(omsg->l_name, getlogin(), NAME_SIZE_OLD);
|
---|
548 | }
|
---|
549 | else
|
---|
550 | {
|
---|
551 | /* new talk */
|
---|
552 | omsg = (CTL_MSG_OLD *) buff;
|
---|
553 | nmsg = mtod(m, CTL_MSG *);
|
---|
554 | type = nmsg->type;
|
---|
555 | OTOSIN(nmsg, ctl_addr)->sin_port = addr.sin_port;
|
---|
556 | OTOSIN(nmsg, ctl_addr)->sin_addr = our_addr;
|
---|
557 | strncpy(nmsg->l_name, getlogin(), NAME_SIZE_OLD);
|
---|
558 | }
|
---|
559 |
|
---|
560 | if (type == LOOK_UP)
|
---|
561 | return; /* for LOOK_UP this is enough */
|
---|
562 |
|
---|
563 | if (IS_OLD)
|
---|
564 | {
|
---|
565 | /* make a copy of the message */
|
---|
566 | COPY_MSG(nmsg, omsg);
|
---|
567 | nmsg->vers = 1;
|
---|
568 | nmsg->answer = 0;
|
---|
569 | }
|
---|
570 | else
|
---|
571 | COPY_MSG(omsg, nmsg);
|
---|
572 |
|
---|
573 | /*
|
---|
574 | * If if is an ANNOUNCE message, we go through the
|
---|
575 | * request table to see if a tcp port has already
|
---|
576 | * been redirected for this socket. If not, we solisten()
|
---|
577 | * a new socket and add this entry to the table.
|
---|
578 | * The port number of the tcp socket and our IP
|
---|
579 | * are put to the addr field of the message structures.
|
---|
580 | * Then a LEAVE_INVITE is sent to both local daemon
|
---|
581 | * ports, 517 and 518. This is why we have two copies
|
---|
582 | * of the message, one in old talk and one in new talk
|
---|
583 | * format.
|
---|
584 | */
|
---|
585 |
|
---|
586 | if (type == ANNOUNCE)
|
---|
587 | {
|
---|
588 | int s;
|
---|
589 | u_short temp_port;
|
---|
590 |
|
---|
591 | for (req = req_tbl; req; req = req->next)
|
---|
592 | if (so == req->udp_so)
|
---|
593 | break; /* found it */
|
---|
594 |
|
---|
595 | if (!req)
|
---|
596 | {
|
---|
597 | /* no entry for so, create new */
|
---|
598 | req = (struct talk_request *)RTMemAlloc(sizeof(struct talk_request));
|
---|
599 | req->udp_so = so;
|
---|
600 | req->tcp_so = solisten(0,
|
---|
601 | OTOSIN(omsg, addr)->sin_addr.s_addr,
|
---|
602 | OTOSIN(omsg, addr)->sin_port,
|
---|
603 | SS_FACCEPTONCE);
|
---|
604 | req->next = req_tbl;
|
---|
605 | req_tbl = req;
|
---|
606 | }
|
---|
607 |
|
---|
608 | /* replace port number in addr field */
|
---|
609 | addrlen = sizeof(addr);
|
---|
610 | getsockname(req->tcp_so->s, (struct sockaddr *) &addr, &addrlen);
|
---|
611 | OTOSIN(omsg, addr)->sin_port = addr.sin_port;
|
---|
612 | OTOSIN(omsg, addr)->sin_addr = our_addr;
|
---|
613 | OTOSIN(nmsg, addr)->sin_port = addr.sin_port;
|
---|
614 | OTOSIN(nmsg, addr)->sin_addr = our_addr;
|
---|
615 |
|
---|
616 | /* send LEAVE_INVITEs */
|
---|
617 | temp_port = OTOSIN(omsg, ctl_addr)->sin_port;
|
---|
618 | OTOSIN(omsg, ctl_addr)->sin_port = 0;
|
---|
619 | OTOSIN(nmsg, ctl_addr)->sin_port = 0;
|
---|
620 | omsg->type = nmsg->type = LEAVE_INVITE;
|
---|
621 |
|
---|
622 | s = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
|
---|
623 | addr.sin_addr = our_addr;
|
---|
624 | addr.sin_family = AF_INET;
|
---|
625 | addr.sin_port = htons(517);
|
---|
626 | sendto(s, (char *)omsg, sizeof(*omsg), 0,
|
---|
627 | (struct sockaddr *)&addr, sizeof(addr));
|
---|
628 | addr.sin_port = htons(518);
|
---|
629 | sendto(s, (char *)nmsg, sizeof(*nmsg), 0,
|
---|
630 | (struct sockaddr *) &addr, sizeof(addr));
|
---|
631 | closesocket(s) ;
|
---|
632 |
|
---|
633 | omsg->type = nmsg->type = ANNOUNCE;
|
---|
634 | OTOSIN(omsg, ctl_addr)->sin_port = temp_port;
|
---|
635 | OTOSIN(nmsg, ctl_addr)->sin_port = temp_port;
|
---|
636 | }
|
---|
637 |
|
---|
638 | /*
|
---|
639 | * If it is a DELETE message, we send a copy to the
|
---|
640 | * local daemons. Then we delete the entry corresponding
|
---|
641 | * to our socket from the request table.
|
---|
642 | */
|
---|
643 |
|
---|
644 | if (type == DELETE)
|
---|
645 | {
|
---|
646 | struct talk_request *temp_req, *req_next;
|
---|
647 | int s;
|
---|
648 | u_short temp_port;
|
---|
649 |
|
---|
650 | temp_port = OTOSIN(omsg, ctl_addr)->sin_port;
|
---|
651 | OTOSIN(omsg, ctl_addr)->sin_port = 0;
|
---|
652 | OTOSIN(nmsg, ctl_addr)->sin_port = 0;
|
---|
653 |
|
---|
654 | s = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
|
---|
655 | addr.sin_addr = our_addr;
|
---|
656 | addr.sin_family = AF_INET;
|
---|
657 | addr.sin_port = htons(517);
|
---|
658 | sendto(s, (char *)omsg, sizeof(*omsg), 0,
|
---|
659 | (struct sockaddr *)&addr, sizeof(addr));
|
---|
660 | addr.sin_port = htons(518);
|
---|
661 | sendto(s, (char *)nmsg, sizeof(*nmsg), 0,
|
---|
662 | (struct sockaddr *)&addr, sizeof(addr));
|
---|
663 | closesocket(s);
|
---|
664 |
|
---|
665 | OTOSIN(omsg, ctl_addr)->sin_port = temp_port;
|
---|
666 | OTOSIN(nmsg, ctl_addr)->sin_port = temp_port;
|
---|
667 |
|
---|
668 | /* delete table entry */
|
---|
669 | if (so == req_tbl->udp_so)
|
---|
670 | {
|
---|
671 | temp_req = req_tbl;
|
---|
672 | req_tbl = req_tbl->next;
|
---|
673 | RTMemFree(temp_req);
|
---|
674 | }
|
---|
675 | else
|
---|
676 | {
|
---|
677 | temp_req = req_tbl;
|
---|
678 | for (req = req_tbl->next; req; req = req_next)
|
---|
679 | {
|
---|
680 | req_next = req->next;
|
---|
681 | if (so == req->udp_so)
|
---|
682 | {
|
---|
683 | temp_req->next = req_next;
|
---|
684 | RTMemFree(req);
|
---|
685 | break;
|
---|
686 | }
|
---|
687 | else
|
---|
688 | temp_req = req;
|
---|
689 | }
|
---|
690 | }
|
---|
691 | }
|
---|
692 |
|
---|
693 | return;
|
---|
694 | #endif
|
---|
695 |
|
---|
696 | case EMU_CUSEEME:
|
---|
697 | /*
|
---|
698 | * Cu-SeeMe emulation.
|
---|
699 | * Hopefully the packet is more that 16 bytes long. We don't
|
---|
700 | * do any other tests, just replace the address and port
|
---|
701 | * fields.
|
---|
702 | */
|
---|
703 | if (m->m_len >= sizeof (*cu_head))
|
---|
704 | {
|
---|
705 | if (getsockname(so->s, (struct sockaddr *)&addr, &addrlen) < 0)
|
---|
706 | return;
|
---|
707 | cu_head = mtod(m, struct cu_header *);
|
---|
708 | cu_head->s_port = addr.sin_port;
|
---|
709 | cu_head->so_addr = our_addr.s_addr;
|
---|
710 | }
|
---|
711 | return;
|
---|
712 | }
|
---|
713 | #else /*!VBOX_WITH_SLIRP_ALIAS*/
|
---|
714 | so->so_emu = 0;
|
---|
715 | #endif /* VBOX_WITH_SLIRP_ALIAS */
|
---|
716 | }
|
---|
717 |
|
---|
718 | struct socket *
|
---|
719 | udp_listen(PNATState pData, u_int port, u_int32_t laddr, u_int lport, int flags)
|
---|
720 | {
|
---|
721 | struct sockaddr_in addr;
|
---|
722 | struct socket *so;
|
---|
723 | socklen_t addrlen = sizeof(struct sockaddr_in);
|
---|
724 | int opt = 1;
|
---|
725 |
|
---|
726 | if ((so = socreate()) == NULL)
|
---|
727 | return NULL;
|
---|
728 |
|
---|
729 | so->s = socket(AF_INET, SOCK_DGRAM, 0);
|
---|
730 | if (so->s == -1)
|
---|
731 | {
|
---|
732 | LogRel(("NAT: can't create datagram socket\n"));
|
---|
733 | RTMemFree(so);
|
---|
734 | return NULL;
|
---|
735 | }
|
---|
736 | so->so_expire = curtime + SO_EXPIRE;
|
---|
737 | fd_nonblock(so->s);
|
---|
738 | SOCKET_LOCK_CREATE(so);
|
---|
739 | QSOCKET_LOCK(udb);
|
---|
740 | insque(pData, so,&udb);
|
---|
741 | NSOCK_INC();
|
---|
742 | QSOCKET_UNLOCK(udb);
|
---|
743 |
|
---|
744 | addr.sin_family = AF_INET;
|
---|
745 | addr.sin_addr.s_addr = INADDR_ANY;
|
---|
746 | addr.sin_port = port;
|
---|
747 |
|
---|
748 | if (bind(so->s,(struct sockaddr *)&addr, addrlen) < 0)
|
---|
749 | {
|
---|
750 | udp_detach(pData, so);
|
---|
751 | return NULL;
|
---|
752 | }
|
---|
753 | setsockopt(so->s, SOL_SOCKET, SO_REUSEADDR,(char *)&opt, sizeof(int));
|
---|
754 | /* setsockopt(so->s, SOL_SOCKET, SO_OOBINLINE,(char *)&opt, sizeof(int)); */
|
---|
755 |
|
---|
756 | getsockname(so->s,(struct sockaddr *)&addr,&addrlen);
|
---|
757 | so->so_fport = addr.sin_port;
|
---|
758 | /* The original check was completely broken, as the commented out
|
---|
759 | * if statement was always true (INADDR_ANY=0). */
|
---|
760 | /* if (addr.sin_addr.s_addr == 0 || addr.sin_addr.s_addr == loopback_addr.s_addr) */
|
---|
761 | if (1 == 0) /* always use the else part */
|
---|
762 | so->so_faddr = alias_addr;
|
---|
763 | else
|
---|
764 | so->so_faddr = addr.sin_addr;
|
---|
765 |
|
---|
766 | so->so_lport = lport;
|
---|
767 | so->so_laddr.s_addr = laddr;
|
---|
768 | if (flags != SS_FACCEPTONCE)
|
---|
769 | so->so_expire = 0;
|
---|
770 |
|
---|
771 | so->so_state = SS_ISFCONNECTED;
|
---|
772 |
|
---|
773 | return so;
|
---|
774 | }
|
---|