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 | /*
|
---|
234 | * DNS proxy
|
---|
235 | */
|
---|
236 | if ( (ip->ip_dst.s_addr == htonl(ntohl(special_addr.s_addr) | CTL_DNS))
|
---|
237 | && (ntohs(uh->uh_dport) == 53))
|
---|
238 | {
|
---|
239 | dnsproxy_query(pData, so, m, iphlen);
|
---|
240 | goto bad; /* it isn't bad, probably better to add additional label done for boot/tftf :) */
|
---|
241 | }
|
---|
242 |
|
---|
243 | iphlen += sizeof(struct udphdr);
|
---|
244 | m->m_len -= iphlen;
|
---|
245 | m->m_data += iphlen;
|
---|
246 |
|
---|
247 | /*
|
---|
248 | * Now we sendto() the packet.
|
---|
249 | */
|
---|
250 | if (so->so_emu)
|
---|
251 | udp_emu(pData, so, m);
|
---|
252 |
|
---|
253 | ttl = ip->ip_ttl = save_ip.ip_ttl;
|
---|
254 | ret = setsockopt(so->s, IPPROTO_IP, IP_TTL, (const char*)&ttl, sizeof(ttl));
|
---|
255 | if (ret < 0)
|
---|
256 | LogRel(("NAT: Error (%s) occurred while setting TTL(%d) attribute "
|
---|
257 | "of IP packet to socket %R[natsock]\n", strerror(errno), ip->ip_ttl, so));
|
---|
258 |
|
---|
259 | if (sosendto(pData, so, m) == -1)
|
---|
260 | {
|
---|
261 | m->m_len += iphlen;
|
---|
262 | m->m_data -= iphlen;
|
---|
263 | *ip = save_ip;
|
---|
264 | DEBUG_MISC((dfd,"NAT: UDP tx errno = %d-%s (on sent to %R[IP4])\n", errno,
|
---|
265 | strerror(errno), &ip->ip_dst));
|
---|
266 | icmp_error(pData, m, ICMP_UNREACH, ICMP_UNREACH_NET, 0, strerror(errno));
|
---|
267 | /* in case we receive ICMP on this socket we'll aware that ICMP has been already sent to host*/
|
---|
268 | so->so_m = NULL;
|
---|
269 | }
|
---|
270 |
|
---|
271 | m_free(pData, so->so_m); /* used for ICMP if error on sorecvfrom */
|
---|
272 |
|
---|
273 | /* restore the orig mbuf packet */
|
---|
274 | m->m_len += iphlen;
|
---|
275 | m->m_data -= iphlen;
|
---|
276 | *ip = save_ip;
|
---|
277 | so->so_m = m; /* ICMP backup */
|
---|
278 |
|
---|
279 | return;
|
---|
280 |
|
---|
281 | bad:
|
---|
282 | Log2(("NAT: UDP datagram to %R[IP4] with size(%d) claimed as bad\n",
|
---|
283 | &ip->ip_dst, ip->ip_len));
|
---|
284 | m_freem(pData, m);
|
---|
285 | return;
|
---|
286 | }
|
---|
287 |
|
---|
288 | int udp_output2(PNATState pData, struct socket *so, struct mbuf *m,
|
---|
289 | struct sockaddr_in *saddr, struct sockaddr_in *daddr,
|
---|
290 | int iptos)
|
---|
291 | {
|
---|
292 | register struct udpiphdr *ui;
|
---|
293 | int error = 0;
|
---|
294 |
|
---|
295 | DEBUG_CALL("udp_output");
|
---|
296 | DEBUG_ARG("so = %lx", (long)so);
|
---|
297 | DEBUG_ARG("m = %lx", (long)m);
|
---|
298 | DEBUG_ARG("saddr = %lx", (long)saddr->sin_addr.s_addr);
|
---|
299 | DEBUG_ARG("daddr = %lx", (long)daddr->sin_addr.s_addr);
|
---|
300 |
|
---|
301 | /*
|
---|
302 | * Adjust for header
|
---|
303 | */
|
---|
304 | m->m_data -= sizeof(struct udpiphdr);
|
---|
305 | m->m_len += sizeof(struct udpiphdr);
|
---|
306 |
|
---|
307 | /*
|
---|
308 | * Fill in mbuf with extended UDP header
|
---|
309 | * and addresses and length put into network format.
|
---|
310 | */
|
---|
311 | ui = mtod(m, struct udpiphdr *);
|
---|
312 | memset(ui->ui_x1, 0, 9);
|
---|
313 | ui->ui_pr = IPPROTO_UDP;
|
---|
314 | ui->ui_len = htons(m->m_len - sizeof(struct ip)); /* + sizeof (struct udphdr)); */
|
---|
315 | /* XXXXX Check for from-one-location sockets, or from-any-location sockets */
|
---|
316 | ui->ui_src = saddr->sin_addr;
|
---|
317 | ui->ui_dst = daddr->sin_addr;
|
---|
318 | ui->ui_sport = saddr->sin_port;
|
---|
319 | ui->ui_dport = daddr->sin_port;
|
---|
320 | ui->ui_ulen = ui->ui_len;
|
---|
321 |
|
---|
322 | /*
|
---|
323 | * Stuff checksum and output datagram.
|
---|
324 | */
|
---|
325 | ui->ui_sum = 0;
|
---|
326 | if (udpcksum)
|
---|
327 | {
|
---|
328 | if ((ui->ui_sum = cksum(m, /* sizeof (struct udpiphdr) + */ m->m_len)) == 0)
|
---|
329 | ui->ui_sum = 0xffff;
|
---|
330 | }
|
---|
331 | ((struct ip *)ui)->ip_len = m->m_len;
|
---|
332 | ((struct ip *)ui)->ip_ttl = ip_defttl;
|
---|
333 | ((struct ip *)ui)->ip_tos = iptos;
|
---|
334 |
|
---|
335 | udpstat.udps_opackets++;
|
---|
336 |
|
---|
337 | error = ip_output(pData, so, m);
|
---|
338 |
|
---|
339 | return error;
|
---|
340 | }
|
---|
341 |
|
---|
342 | int udp_output(PNATState pData, struct socket *so, struct mbuf *m,
|
---|
343 | struct sockaddr_in *addr)
|
---|
344 | {
|
---|
345 | struct sockaddr_in saddr, daddr;
|
---|
346 |
|
---|
347 | saddr = *addr;
|
---|
348 | if ((so->so_faddr.s_addr & htonl(pData->netmask)) == special_addr.s_addr)
|
---|
349 | {
|
---|
350 | saddr.sin_addr.s_addr = so->so_faddr.s_addr;
|
---|
351 | if ((so->so_faddr.s_addr & htonl(~pData->netmask)) == htonl(~pData->netmask))
|
---|
352 | saddr.sin_addr.s_addr = alias_addr.s_addr;
|
---|
353 | }
|
---|
354 |
|
---|
355 | /* Any UDP packet to the loopback address must be translated to be from
|
---|
356 | * the forwarding address, i.e. 10.0.2.2. */
|
---|
357 | if ( (saddr.sin_addr.s_addr & htonl(IN_CLASSA_NET))
|
---|
358 | == htonl(INADDR_LOOPBACK & IN_CLASSA_NET))
|
---|
359 | saddr.sin_addr.s_addr = alias_addr.s_addr;
|
---|
360 |
|
---|
361 | daddr.sin_addr = so->so_laddr;
|
---|
362 | daddr.sin_port = so->so_lport;
|
---|
363 |
|
---|
364 | return udp_output2(pData, so, m, &saddr, &daddr, so->so_iptos);
|
---|
365 | }
|
---|
366 |
|
---|
367 | int
|
---|
368 | udp_attach(PNATState pData, struct socket *so, int service_port)
|
---|
369 | {
|
---|
370 | struct sockaddr_in addr;
|
---|
371 | struct sockaddr sa_addr;
|
---|
372 | socklen_t socklen = sizeof(struct sockaddr);
|
---|
373 | int status;
|
---|
374 |
|
---|
375 | if ((so->s = socket(AF_INET, SOCK_DGRAM, 0)) != -1)
|
---|
376 | {
|
---|
377 | /*
|
---|
378 | * Here, we bind() the socket. Although not really needed
|
---|
379 | * (sendto() on an unbound socket will bind it), it's done
|
---|
380 | * here so that emulation of ytalk etc. don't have to do it
|
---|
381 | */
|
---|
382 | memset(&addr, 0, sizeof(addr));
|
---|
383 | #ifdef RT_OS_DARWIN
|
---|
384 | addr.sin_len = sizeof(addr);
|
---|
385 | #endif
|
---|
386 | addr.sin_family = AF_INET;
|
---|
387 | addr.sin_port = service_port;
|
---|
388 | addr.sin_addr.s_addr = pData->bindIP.s_addr;
|
---|
389 | fd_nonblock(so->s);
|
---|
390 | if (bind(so->s, (struct sockaddr *)&addr, sizeof(addr)) < 0)
|
---|
391 | {
|
---|
392 | int lasterrno = errno;
|
---|
393 | closesocket(so->s);
|
---|
394 | so->s = -1;
|
---|
395 | #ifdef RT_OS_WINDOWS
|
---|
396 | WSASetLastError(lasterrno);
|
---|
397 | #else
|
---|
398 | errno = lasterrno;
|
---|
399 | #endif
|
---|
400 | }
|
---|
401 | else
|
---|
402 | {
|
---|
403 | int opt = 1;
|
---|
404 | /* success, insert in queue */
|
---|
405 | so->so_expire = curtime + SO_EXPIRE;
|
---|
406 | /* enable broadcast for later use */
|
---|
407 | setsockopt(so->s, SOL_SOCKET, SO_BROADCAST, (const char *)&opt, sizeof(opt));
|
---|
408 | status = getsockname(so->s, &sa_addr, &socklen);
|
---|
409 | Assert(status == 0 && sa_addr.sa_family == AF_INET);
|
---|
410 | so->so_hlport = ((struct sockaddr_in *)&sa_addr)->sin_port;
|
---|
411 | so->so_hladdr.s_addr = ((struct sockaddr_in *)&sa_addr)->sin_addr.s_addr;
|
---|
412 | SOCKET_LOCK_CREATE(so);
|
---|
413 | QSOCKET_LOCK(udb);
|
---|
414 | insque(pData, so, &udb);
|
---|
415 | NSOCK_INC();
|
---|
416 | QSOCKET_UNLOCK(udb);
|
---|
417 | }
|
---|
418 | }
|
---|
419 | else
|
---|
420 | {
|
---|
421 | LogRel(("NAT: can't create datagramm socket\n"));
|
---|
422 | }
|
---|
423 | return so->s;
|
---|
424 | }
|
---|
425 |
|
---|
426 | void
|
---|
427 | udp_detach(PNATState pData, struct socket *so)
|
---|
428 | {
|
---|
429 | if (so != &pData->icmp_socket)
|
---|
430 | {
|
---|
431 | QSOCKET_LOCK(udb);
|
---|
432 | SOCKET_LOCK(so);
|
---|
433 | QSOCKET_UNLOCK(udb);
|
---|
434 | closesocket(so->s);
|
---|
435 | sofree(pData, so);
|
---|
436 | SOCKET_UNLOCK(so);
|
---|
437 | }
|
---|
438 | }
|
---|
439 |
|
---|
440 | static const struct tos_t udptos[] =
|
---|
441 | {
|
---|
442 | { 0, 53, IPTOS_LOWDELAY, 0 }, /* DNS */
|
---|
443 | { 517, 517, IPTOS_LOWDELAY, EMU_TALK }, /* talk */
|
---|
444 | { 518, 518, IPTOS_LOWDELAY, EMU_NTALK }, /* ntalk */
|
---|
445 | { 0, 7648, IPTOS_LOWDELAY, EMU_CUSEEME }, /* Cu-Seeme */
|
---|
446 | { 0, 0, 0, 0 }
|
---|
447 | };
|
---|
448 |
|
---|
449 | u_int8_t
|
---|
450 | udp_tos(struct socket *so)
|
---|
451 | {
|
---|
452 | int i = 0;
|
---|
453 |
|
---|
454 | while(udptos[i].tos)
|
---|
455 | {
|
---|
456 | if ( (udptos[i].fport && ntohs(so->so_fport) == udptos[i].fport)
|
---|
457 | || (udptos[i].lport && ntohs(so->so_lport) == udptos[i].lport))
|
---|
458 | {
|
---|
459 | so->so_emu = udptos[i].emu;
|
---|
460 | return udptos[i].tos;
|
---|
461 | }
|
---|
462 | i++;
|
---|
463 | }
|
---|
464 |
|
---|
465 | return 0;
|
---|
466 | }
|
---|
467 |
|
---|
468 | #ifdef EMULATE_TALK
|
---|
469 | #include "talkd.h"
|
---|
470 | #endif
|
---|
471 |
|
---|
472 | /*
|
---|
473 | * Here, talk/ytalk/ntalk requests must be emulated
|
---|
474 | */
|
---|
475 | void
|
---|
476 | udp_emu(PNATState pData, struct socket *so, struct mbuf *m)
|
---|
477 | {
|
---|
478 | #ifndef VBOX_WITH_SLIRP_ALIAS
|
---|
479 | struct sockaddr_in addr;
|
---|
480 | socklen_t addrlen = sizeof(addr);
|
---|
481 | #ifdef EMULATE_TALK
|
---|
482 | CTL_MSG_OLD *omsg;
|
---|
483 | CTL_MSG *nmsg;
|
---|
484 | char buff[sizeof(CTL_MSG)];
|
---|
485 | u_char type;
|
---|
486 |
|
---|
487 | struct talk_request
|
---|
488 | {
|
---|
489 | struct talk_request *next;
|
---|
490 | struct socket *udp_so;
|
---|
491 | struct socket *tcp_so;
|
---|
492 | } *req;
|
---|
493 |
|
---|
494 | static struct talk_request *req_tbl = 0;
|
---|
495 |
|
---|
496 | #endif
|
---|
497 |
|
---|
498 | struct cu_header
|
---|
499 | {
|
---|
500 | uint16_t d_family; /* destination family */
|
---|
501 | uint16_t d_port; /* destination port */
|
---|
502 | uint32_t d_addr; /* destination address */
|
---|
503 | uint16_t s_family; /* source family */
|
---|
504 | uint16_t s_port; /* source port */
|
---|
505 | uint32_t so_addr; /* source address */
|
---|
506 | uint32_t seqn; /* sequence number */
|
---|
507 | uint16_t message; /* message */
|
---|
508 | uint16_t data_type; /* data type */
|
---|
509 | uint16_t pkt_len; /* packet length */
|
---|
510 | } *cu_head;
|
---|
511 |
|
---|
512 | switch(so->so_emu)
|
---|
513 | {
|
---|
514 | #ifdef EMULATE_TALK
|
---|
515 | case EMU_TALK:
|
---|
516 | case EMU_NTALK:
|
---|
517 | /*
|
---|
518 | * Talk emulation. We always change the ctl_addr to get
|
---|
519 | * some answers from the daemon. When an ANNOUNCE comes,
|
---|
520 | * we send LEAVE_INVITE to the local daemons. Also when a
|
---|
521 | * DELETE comes, we send copies to the local daemons.
|
---|
522 | */
|
---|
523 | if (getsockname(so->s, (struct sockaddr *)&addr, &addrlen) < 0)
|
---|
524 | return;
|
---|
525 |
|
---|
526 | #define IS_OLD (so->so_emu == EMU_TALK)
|
---|
527 |
|
---|
528 | #define COPY_MSG(dest, src) \
|
---|
529 | do { \
|
---|
530 | dest->type = src->type; \
|
---|
531 | dest->id_num = src->id_num; \
|
---|
532 | dest->pid = src->pid; \
|
---|
533 | dest->addr = src->addr; \
|
---|
534 | dest->ctl_addr = src->ctl_addr; \
|
---|
535 | memcpy(&dest->l_name, &src->l_name, NAME_SIZE_OLD); \
|
---|
536 | memcpy(&dest->r_name, &src->r_name, NAME_SIZE_OLD); \
|
---|
537 | memcpy(&dest->r_tty, &src->r_tty, TTY_SIZE);
|
---|
538 | } while (0)
|
---|
539 |
|
---|
540 | #define OTOSIN(ptr, field) ((struct sockaddr_in *)&ptr->field)
|
---|
541 | /* old_sockaddr to sockaddr_in */
|
---|
542 |
|
---|
543 |
|
---|
544 | if (IS_OLD)
|
---|
545 | {
|
---|
546 | /* old talk */
|
---|
547 | omsg = mtod(m, CTL_MSG_OLD*);
|
---|
548 | nmsg = (CTL_MSG *) buff;
|
---|
549 | type = omsg->type;
|
---|
550 | OTOSIN(omsg, ctl_addr)->sin_port = addr.sin_port;
|
---|
551 | OTOSIN(omsg, ctl_addr)->sin_addr = our_addr;
|
---|
552 | strncpy(omsg->l_name, getlogin(), NAME_SIZE_OLD);
|
---|
553 | }
|
---|
554 | else
|
---|
555 | {
|
---|
556 | /* new talk */
|
---|
557 | omsg = (CTL_MSG_OLD *) buff;
|
---|
558 | nmsg = mtod(m, CTL_MSG *);
|
---|
559 | type = nmsg->type;
|
---|
560 | OTOSIN(nmsg, ctl_addr)->sin_port = addr.sin_port;
|
---|
561 | OTOSIN(nmsg, ctl_addr)->sin_addr = our_addr;
|
---|
562 | strncpy(nmsg->l_name, getlogin(), NAME_SIZE_OLD);
|
---|
563 | }
|
---|
564 |
|
---|
565 | if (type == LOOK_UP)
|
---|
566 | return; /* for LOOK_UP this is enough */
|
---|
567 |
|
---|
568 | if (IS_OLD)
|
---|
569 | {
|
---|
570 | /* make a copy of the message */
|
---|
571 | COPY_MSG(nmsg, omsg);
|
---|
572 | nmsg->vers = 1;
|
---|
573 | nmsg->answer = 0;
|
---|
574 | }
|
---|
575 | else
|
---|
576 | COPY_MSG(omsg, nmsg);
|
---|
577 |
|
---|
578 | /*
|
---|
579 | * If if is an ANNOUNCE message, we go through the
|
---|
580 | * request table to see if a tcp port has already
|
---|
581 | * been redirected for this socket. If not, we solisten()
|
---|
582 | * a new socket and add this entry to the table.
|
---|
583 | * The port number of the tcp socket and our IP
|
---|
584 | * are put to the addr field of the message structures.
|
---|
585 | * Then a LEAVE_INVITE is sent to both local daemon
|
---|
586 | * ports, 517 and 518. This is why we have two copies
|
---|
587 | * of the message, one in old talk and one in new talk
|
---|
588 | * format.
|
---|
589 | */
|
---|
590 |
|
---|
591 | if (type == ANNOUNCE)
|
---|
592 | {
|
---|
593 | int s;
|
---|
594 | u_short temp_port;
|
---|
595 |
|
---|
596 | for (req = req_tbl; req; req = req->next)
|
---|
597 | if (so == req->udp_so)
|
---|
598 | break; /* found it */
|
---|
599 |
|
---|
600 | if (!req)
|
---|
601 | {
|
---|
602 | /* no entry for so, create new */
|
---|
603 | req = (struct talk_request *)RTMemAlloc(sizeof(struct talk_request));
|
---|
604 | req->udp_so = so;
|
---|
605 | req->tcp_so = solisten(0,
|
---|
606 | OTOSIN(omsg, addr)->sin_addr.s_addr,
|
---|
607 | OTOSIN(omsg, addr)->sin_port,
|
---|
608 | SS_FACCEPTONCE);
|
---|
609 | req->next = req_tbl;
|
---|
610 | req_tbl = req;
|
---|
611 | }
|
---|
612 |
|
---|
613 | /* replace port number in addr field */
|
---|
614 | addrlen = sizeof(addr);
|
---|
615 | getsockname(req->tcp_so->s, (struct sockaddr *) &addr, &addrlen);
|
---|
616 | OTOSIN(omsg, addr)->sin_port = addr.sin_port;
|
---|
617 | OTOSIN(omsg, addr)->sin_addr = our_addr;
|
---|
618 | OTOSIN(nmsg, addr)->sin_port = addr.sin_port;
|
---|
619 | OTOSIN(nmsg, addr)->sin_addr = our_addr;
|
---|
620 |
|
---|
621 | /* send LEAVE_INVITEs */
|
---|
622 | temp_port = OTOSIN(omsg, ctl_addr)->sin_port;
|
---|
623 | OTOSIN(omsg, ctl_addr)->sin_port = 0;
|
---|
624 | OTOSIN(nmsg, ctl_addr)->sin_port = 0;
|
---|
625 | omsg->type = nmsg->type = LEAVE_INVITE;
|
---|
626 |
|
---|
627 | s = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
|
---|
628 | addr.sin_addr = our_addr;
|
---|
629 | addr.sin_family = AF_INET;
|
---|
630 | addr.sin_port = htons(517);
|
---|
631 | sendto(s, (char *)omsg, sizeof(*omsg), 0,
|
---|
632 | (struct sockaddr *)&addr, sizeof(addr));
|
---|
633 | addr.sin_port = htons(518);
|
---|
634 | sendto(s, (char *)nmsg, sizeof(*nmsg), 0,
|
---|
635 | (struct sockaddr *) &addr, sizeof(addr));
|
---|
636 | closesocket(s) ;
|
---|
637 |
|
---|
638 | omsg->type = nmsg->type = ANNOUNCE;
|
---|
639 | OTOSIN(omsg, ctl_addr)->sin_port = temp_port;
|
---|
640 | OTOSIN(nmsg, ctl_addr)->sin_port = temp_port;
|
---|
641 | }
|
---|
642 |
|
---|
643 | /*
|
---|
644 | * If it is a DELETE message, we send a copy to the
|
---|
645 | * local daemons. Then we delete the entry corresponding
|
---|
646 | * to our socket from the request table.
|
---|
647 | */
|
---|
648 |
|
---|
649 | if (type == DELETE)
|
---|
650 | {
|
---|
651 | struct talk_request *temp_req, *req_next;
|
---|
652 | int s;
|
---|
653 | u_short temp_port;
|
---|
654 |
|
---|
655 | temp_port = OTOSIN(omsg, ctl_addr)->sin_port;
|
---|
656 | OTOSIN(omsg, ctl_addr)->sin_port = 0;
|
---|
657 | OTOSIN(nmsg, ctl_addr)->sin_port = 0;
|
---|
658 |
|
---|
659 | s = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
|
---|
660 | addr.sin_addr = our_addr;
|
---|
661 | addr.sin_family = AF_INET;
|
---|
662 | addr.sin_port = htons(517);
|
---|
663 | sendto(s, (char *)omsg, sizeof(*omsg), 0,
|
---|
664 | (struct sockaddr *)&addr, sizeof(addr));
|
---|
665 | addr.sin_port = htons(518);
|
---|
666 | sendto(s, (char *)nmsg, sizeof(*nmsg), 0,
|
---|
667 | (struct sockaddr *)&addr, sizeof(addr));
|
---|
668 | closesocket(s);
|
---|
669 |
|
---|
670 | OTOSIN(omsg, ctl_addr)->sin_port = temp_port;
|
---|
671 | OTOSIN(nmsg, ctl_addr)->sin_port = temp_port;
|
---|
672 |
|
---|
673 | /* delete table entry */
|
---|
674 | if (so == req_tbl->udp_so)
|
---|
675 | {
|
---|
676 | temp_req = req_tbl;
|
---|
677 | req_tbl = req_tbl->next;
|
---|
678 | RTMemFree(temp_req);
|
---|
679 | }
|
---|
680 | else
|
---|
681 | {
|
---|
682 | temp_req = req_tbl;
|
---|
683 | for (req = req_tbl->next; req; req = req_next)
|
---|
684 | {
|
---|
685 | req_next = req->next;
|
---|
686 | if (so == req->udp_so)
|
---|
687 | {
|
---|
688 | temp_req->next = req_next;
|
---|
689 | RTMemFree(req);
|
---|
690 | break;
|
---|
691 | }
|
---|
692 | else
|
---|
693 | temp_req = req;
|
---|
694 | }
|
---|
695 | }
|
---|
696 | }
|
---|
697 |
|
---|
698 | return;
|
---|
699 | #endif
|
---|
700 |
|
---|
701 | case EMU_CUSEEME:
|
---|
702 | /*
|
---|
703 | * Cu-SeeMe emulation.
|
---|
704 | * Hopefully the packet is more that 16 bytes long. We don't
|
---|
705 | * do any other tests, just replace the address and port
|
---|
706 | * fields.
|
---|
707 | */
|
---|
708 | if (m->m_len >= sizeof (*cu_head))
|
---|
709 | {
|
---|
710 | if (getsockname(so->s, (struct sockaddr *)&addr, &addrlen) < 0)
|
---|
711 | return;
|
---|
712 | cu_head = mtod(m, struct cu_header *);
|
---|
713 | cu_head->s_port = addr.sin_port;
|
---|
714 | cu_head->so_addr = our_addr.s_addr;
|
---|
715 | }
|
---|
716 | return;
|
---|
717 | }
|
---|
718 | #else /*!VBOX_WITH_SLIRP_ALIAS*/
|
---|
719 | so->so_emu = 0;
|
---|
720 | #endif /* VBOX_WITH_SLIRP_ALIAS */
|
---|
721 | }
|
---|
722 |
|
---|
723 | struct socket *
|
---|
724 | udp_listen(PNATState pData, u_int32_t bind_addr, u_int port, u_int32_t laddr, u_int lport, int flags)
|
---|
725 | {
|
---|
726 | struct sockaddr_in addr;
|
---|
727 | struct socket *so;
|
---|
728 | socklen_t addrlen = sizeof(struct sockaddr_in);
|
---|
729 | int opt = 1;
|
---|
730 |
|
---|
731 | if ((so = socreate()) == NULL)
|
---|
732 | return NULL;
|
---|
733 |
|
---|
734 | so->s = socket(AF_INET, SOCK_DGRAM, 0);
|
---|
735 | if (so->s == -1)
|
---|
736 | {
|
---|
737 | LogRel(("NAT: can't create datagram socket\n"));
|
---|
738 | RTMemFree(so);
|
---|
739 | return NULL;
|
---|
740 | }
|
---|
741 | so->so_expire = curtime + SO_EXPIRE;
|
---|
742 | fd_nonblock(so->s);
|
---|
743 | SOCKET_LOCK_CREATE(so);
|
---|
744 | QSOCKET_LOCK(udb);
|
---|
745 | insque(pData, so,&udb);
|
---|
746 | NSOCK_INC();
|
---|
747 | QSOCKET_UNLOCK(udb);
|
---|
748 |
|
---|
749 | memset(&addr, 0, sizeof(addr));
|
---|
750 | #ifdef RT_OS_DARWIN
|
---|
751 | addr.sin_len = sizeof(addr);
|
---|
752 | #endif
|
---|
753 | addr.sin_family = AF_INET;
|
---|
754 | addr.sin_addr.s_addr = bind_addr;
|
---|
755 | addr.sin_port = port;
|
---|
756 |
|
---|
757 | if (bind(so->s,(struct sockaddr *)&addr, addrlen) < 0)
|
---|
758 | {
|
---|
759 | LogRel(("NAT: bind to %R[IP4] has been failed\n", &addr.sin_addr));
|
---|
760 | udp_detach(pData, so);
|
---|
761 | return NULL;
|
---|
762 | }
|
---|
763 | setsockopt(so->s, SOL_SOCKET, SO_REUSEADDR,(char *)&opt, sizeof(int));
|
---|
764 | /* setsockopt(so->s, SOL_SOCKET, SO_OOBINLINE,(char *)&opt, sizeof(int)); */
|
---|
765 |
|
---|
766 | getsockname(so->s,(struct sockaddr *)&addr,&addrlen);
|
---|
767 | so->so_fport = addr.sin_port;
|
---|
768 | /* The original check was completely broken, as the commented out
|
---|
769 | * if statement was always true (INADDR_ANY=0). */
|
---|
770 | /* if (addr.sin_addr.s_addr == 0 || addr.sin_addr.s_addr == loopback_addr.s_addr) */
|
---|
771 | if (1 == 0) /* always use the else part */
|
---|
772 | so->so_faddr = alias_addr;
|
---|
773 | else
|
---|
774 | so->so_faddr = addr.sin_addr;
|
---|
775 |
|
---|
776 | so->so_lport = lport;
|
---|
777 | so->so_laddr.s_addr = laddr;
|
---|
778 | if (flags != SS_FACCEPTONCE)
|
---|
779 | so->so_expire = 0;
|
---|
780 |
|
---|
781 | so->so_state = SS_ISFCONNECTED;
|
---|
782 |
|
---|
783 | return so;
|
---|
784 | }
|
---|