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 = RT_N2H_U16((u_int16_t)uh->uh_ulen);
|
---|
109 | Assert((ip->ip_len == len));
|
---|
110 | Assert((ip->ip_len + iphlen == m->m_len));
|
---|
111 |
|
---|
112 | if (ip->ip_len != len)
|
---|
113 | {
|
---|
114 | if (len > ip->ip_len)
|
---|
115 | {
|
---|
116 | udpstat.udps_badlen++;
|
---|
117 | Log3(("NAT: IP(id: %hd) has bad size\n", ip->ip_id));
|
---|
118 | }
|
---|
119 | m_adj(m, len - ip->ip_len);
|
---|
120 | ip->ip_len = len;
|
---|
121 | }
|
---|
122 |
|
---|
123 | /*
|
---|
124 | * Save a copy of the IP header in case we want restore it
|
---|
125 | * for sending an ICMP error message in response.
|
---|
126 | */
|
---|
127 | save_ip = *ip;
|
---|
128 | save_ip.ip_len+= iphlen; /* tcp_input subtracts this */
|
---|
129 |
|
---|
130 | /*
|
---|
131 | * Checksum extended UDP header and data.
|
---|
132 | */
|
---|
133 | if (udpcksum && uh->uh_sum)
|
---|
134 | {
|
---|
135 | memset(((struct ipovly *)ip)->ih_x1, 0, 9);
|
---|
136 | ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
|
---|
137 | #if 0
|
---|
138 | /* keep uh_sum for ICMP reply */
|
---|
139 | uh->uh_sum = cksum(m, len + sizeof (struct ip));
|
---|
140 | if (uh->uh_sum)
|
---|
141 | {
|
---|
142 |
|
---|
143 | #endif
|
---|
144 | if(cksum(m, len + iphlen))
|
---|
145 | {
|
---|
146 | udpstat.udps_badsum++;
|
---|
147 | Log3(("NAT: IP(id: %hd) has bad (udp) cksum\n", ip->ip_id));
|
---|
148 | goto bad;
|
---|
149 | }
|
---|
150 | }
|
---|
151 | #if 0
|
---|
152 | }
|
---|
153 | #endif
|
---|
154 |
|
---|
155 | /*
|
---|
156 | * handle DHCP/BOOTP
|
---|
157 | */
|
---|
158 | if (uh->uh_dport == RT_H2N_U16_C(BOOTP_SERVER))
|
---|
159 | {
|
---|
160 | bootp_input(pData, m);
|
---|
161 | goto done;
|
---|
162 | }
|
---|
163 |
|
---|
164 | if ( pData->use_host_resolver
|
---|
165 | && uh->uh_dport == RT_H2N_U16_C(53)
|
---|
166 | && CTL_CHECK(RT_N2H_U32(ip->ip_dst.s_addr), CTL_DNS))
|
---|
167 | {
|
---|
168 | struct sockaddr_in dst, src;
|
---|
169 | src.sin_addr.s_addr = ip->ip_dst.s_addr;
|
---|
170 | src.sin_port = uh->uh_dport;
|
---|
171 | dst.sin_addr.s_addr = ip->ip_src.s_addr;
|
---|
172 | dst.sin_port = uh->uh_sport;
|
---|
173 | /* udp_output2 will do opposite operations on mbuf*/
|
---|
174 |
|
---|
175 | m->m_data += sizeof(struct udpiphdr);
|
---|
176 | m->m_len -= sizeof(struct udpiphdr);
|
---|
177 | udp_output2(pData, NULL, m, &src, &dst, IPTOS_LOWDELAY);
|
---|
178 | goto done;
|
---|
179 | }
|
---|
180 | /*
|
---|
181 | * handle TFTP
|
---|
182 | */
|
---|
183 | if ( uh->uh_dport == RT_H2N_U16_C(TFTP_SERVER)
|
---|
184 | && CTL_CHECK(RT_N2H_U32(ip->ip_dst.s_addr), CTL_TFTP))
|
---|
185 | {
|
---|
186 | tftp_input(pData, m);
|
---|
187 | goto done;
|
---|
188 | }
|
---|
189 |
|
---|
190 | /*
|
---|
191 | * Locate pcb for datagram.
|
---|
192 | */
|
---|
193 | so = udp_last_so;
|
---|
194 | if ( so->so_lport != uh->uh_sport
|
---|
195 | || so->so_laddr.s_addr != ip->ip_src.s_addr)
|
---|
196 | {
|
---|
197 | struct socket *tmp;
|
---|
198 |
|
---|
199 | for (tmp = udb.so_next; tmp != &udb; tmp = tmp->so_next)
|
---|
200 | {
|
---|
201 | if ( tmp->so_lport == uh->uh_sport
|
---|
202 | && tmp->so_laddr.s_addr == ip->ip_src.s_addr)
|
---|
203 | {
|
---|
204 | so = tmp;
|
---|
205 | break;
|
---|
206 | }
|
---|
207 | }
|
---|
208 | if (tmp == &udb)
|
---|
209 | so = NULL;
|
---|
210 | else
|
---|
211 | {
|
---|
212 | udpstat.udpps_pcbcachemiss++;
|
---|
213 | udp_last_so = so;
|
---|
214 | }
|
---|
215 | }
|
---|
216 |
|
---|
217 | if (so == NULL)
|
---|
218 | {
|
---|
219 | /*
|
---|
220 | * If there's no socket for this packet,
|
---|
221 | * create one
|
---|
222 | */
|
---|
223 | if ((so = socreate()) == NULL)
|
---|
224 | {
|
---|
225 | Log3(("NAT: IP(id: %hd) failed to create socket\n", ip->ip_id));
|
---|
226 | goto bad;
|
---|
227 | }
|
---|
228 | if (udp_attach(pData, so, 0) == -1)
|
---|
229 | {
|
---|
230 | Log3(("NAT: IP(id: %hd) udp_attach errno = %d-%s\n",
|
---|
231 | ip->ip_id, errno, strerror(errno)));
|
---|
232 | sofree(pData, so);
|
---|
233 | goto bad;
|
---|
234 | }
|
---|
235 |
|
---|
236 | /*
|
---|
237 | * Setup fields
|
---|
238 | */
|
---|
239 | /* udp_last_so = so; */
|
---|
240 | so->so_laddr = ip->ip_src;
|
---|
241 | so->so_lport = uh->uh_sport;
|
---|
242 |
|
---|
243 | if ((so->so_iptos = udp_tos(so)) == 0)
|
---|
244 | so->so_iptos = ip->ip_tos;
|
---|
245 |
|
---|
246 | /*
|
---|
247 | * XXXXX Here, check if it's in udpexec_list,
|
---|
248 | * and if it is, do the fork_exec() etc.
|
---|
249 | */
|
---|
250 | }
|
---|
251 |
|
---|
252 | so->so_faddr = ip->ip_dst; /* XXX */
|
---|
253 | so->so_fport = uh->uh_dport; /* XXX */
|
---|
254 |
|
---|
255 | /*
|
---|
256 | * DNS proxy
|
---|
257 | */
|
---|
258 | if ( pData->use_dns_proxy
|
---|
259 | && (ip->ip_dst.s_addr == RT_H2N_U32(RT_N2H_U32(pData->special_addr.s_addr) | CTL_DNS))
|
---|
260 | && (uh->uh_dport == RT_H2N_U16_C(53)))
|
---|
261 | {
|
---|
262 | dnsproxy_query(pData, so, m, iphlen);
|
---|
263 | goto done;
|
---|
264 | }
|
---|
265 |
|
---|
266 | iphlen += sizeof(struct udphdr);
|
---|
267 | m->m_len -= iphlen;
|
---|
268 | m->m_data += iphlen;
|
---|
269 |
|
---|
270 | /*
|
---|
271 | * Now we sendto() the packet.
|
---|
272 | */
|
---|
273 | if (so->so_emu)
|
---|
274 | udp_emu(pData, so, m);
|
---|
275 |
|
---|
276 | ttl = ip->ip_ttl = save_ip.ip_ttl;
|
---|
277 | ret = setsockopt(so->s, IPPROTO_IP, IP_TTL, (const char*)&ttl, sizeof(ttl));
|
---|
278 | if (ret < 0)
|
---|
279 | LogRel(("NAT: Error (%s) occurred while setting TTL(%d) attribute "
|
---|
280 | "of IP packet to socket %R[natsock]\n", strerror(errno), ip->ip_ttl, so));
|
---|
281 |
|
---|
282 | if (sosendto(pData, so, m) == -1)
|
---|
283 | {
|
---|
284 | m->m_len += iphlen;
|
---|
285 | m->m_data -= iphlen;
|
---|
286 | *ip = save_ip;
|
---|
287 | DEBUG_MISC((dfd,"NAT: UDP tx errno = %d-%s (on sent to %R[IP4])\n", errno,
|
---|
288 | strerror(errno), &ip->ip_dst));
|
---|
289 | icmp_error(pData, m, ICMP_UNREACH, ICMP_UNREACH_NET, 0, strerror(errno));
|
---|
290 | /* in case we receive ICMP on this socket we'll aware that ICMP has been already sent to host*/
|
---|
291 | so->so_m = NULL;
|
---|
292 | }
|
---|
293 |
|
---|
294 | if (so->so_m)
|
---|
295 | m_free(pData, so->so_m); /* used for ICMP if error on sorecvfrom */
|
---|
296 |
|
---|
297 | /* restore the orig mbuf packet */
|
---|
298 | m->m_len += iphlen;
|
---|
299 | m->m_data -= iphlen;
|
---|
300 | *ip = save_ip;
|
---|
301 | so->so_m = m; /* ICMP backup */
|
---|
302 |
|
---|
303 | return;
|
---|
304 |
|
---|
305 | bad:
|
---|
306 | Log2(("NAT: UDP(id: %hd) datagram to %R[IP4] with size(%d) claimed as bad\n",
|
---|
307 | ip->ip_id, &ip->ip_dst, ip->ip_len));
|
---|
308 | done:
|
---|
309 | /* some services like bootp(built-in), dns(buildt-in) and dhcp don't need sockets
|
---|
310 | * and create new m'buffers to send them to guest, so we'll free their incomming
|
---|
311 | * buffers here.
|
---|
312 | */
|
---|
313 | m_freem(pData, m);
|
---|
314 | return;
|
---|
315 | }
|
---|
316 |
|
---|
317 | int udp_output2(PNATState pData, struct socket *so, struct mbuf *m,
|
---|
318 | struct sockaddr_in *saddr, struct sockaddr_in *daddr,
|
---|
319 | int iptos)
|
---|
320 | {
|
---|
321 | register struct udpiphdr *ui;
|
---|
322 | int error = 0;
|
---|
323 |
|
---|
324 | DEBUG_CALL("udp_output");
|
---|
325 | DEBUG_ARG("so = %lx", (long)so);
|
---|
326 | DEBUG_ARG("m = %lx", (long)m);
|
---|
327 | DEBUG_ARG("saddr = %lx", (long)saddr->sin_addr.s_addr);
|
---|
328 | DEBUG_ARG("daddr = %lx", (long)daddr->sin_addr.s_addr);
|
---|
329 |
|
---|
330 | /*
|
---|
331 | * Adjust for header
|
---|
332 | */
|
---|
333 | m->m_data -= sizeof(struct udpiphdr);
|
---|
334 | m->m_len += sizeof(struct udpiphdr);
|
---|
335 |
|
---|
336 | /*
|
---|
337 | * Fill in mbuf with extended UDP header
|
---|
338 | * and addresses and length put into network format.
|
---|
339 | */
|
---|
340 | ui = mtod(m, struct udpiphdr *);
|
---|
341 | memset(ui->ui_x1, 0, 9);
|
---|
342 | ui->ui_pr = IPPROTO_UDP;
|
---|
343 | ui->ui_len = RT_H2N_U16(m->m_len - sizeof(struct ip));
|
---|
344 | /* XXXXX Check for from-one-location sockets, or from-any-location sockets */
|
---|
345 | ui->ui_src = saddr->sin_addr;
|
---|
346 | ui->ui_dst = daddr->sin_addr;
|
---|
347 | ui->ui_sport = saddr->sin_port;
|
---|
348 | ui->ui_dport = daddr->sin_port;
|
---|
349 | ui->ui_ulen = ui->ui_len;
|
---|
350 |
|
---|
351 | /*
|
---|
352 | * Stuff checksum and output datagram.
|
---|
353 | */
|
---|
354 | ui->ui_sum = 0;
|
---|
355 | if (udpcksum)
|
---|
356 | {
|
---|
357 | if ((ui->ui_sum = cksum(m, /* sizeof (struct udpiphdr) + */ m->m_len)) == 0)
|
---|
358 | ui->ui_sum = 0xffff;
|
---|
359 | }
|
---|
360 | ((struct ip *)ui)->ip_len = m->m_len;
|
---|
361 | ((struct ip *)ui)->ip_ttl = ip_defttl;
|
---|
362 | ((struct ip *)ui)->ip_tos = iptos;
|
---|
363 |
|
---|
364 | udpstat.udps_opackets++;
|
---|
365 |
|
---|
366 | error = ip_output(pData, so, m);
|
---|
367 |
|
---|
368 | return error;
|
---|
369 | }
|
---|
370 |
|
---|
371 | int udp_output(PNATState pData, struct socket *so, struct mbuf *m,
|
---|
372 | struct sockaddr_in *addr)
|
---|
373 | {
|
---|
374 | struct sockaddr_in saddr, daddr;
|
---|
375 |
|
---|
376 | saddr = *addr;
|
---|
377 | if ((so->so_faddr.s_addr & RT_H2N_U32(pData->netmask)) == pData->special_addr.s_addr)
|
---|
378 | {
|
---|
379 | saddr.sin_addr.s_addr = so->so_faddr.s_addr;
|
---|
380 | if ((so->so_faddr.s_addr & RT_H2N_U32(~pData->netmask)) == RT_H2N_U32(~pData->netmask))
|
---|
381 | saddr.sin_addr.s_addr = alias_addr.s_addr;
|
---|
382 | }
|
---|
383 |
|
---|
384 | /* Any UDP packet to the loopback address must be translated to be from
|
---|
385 | * the forwarding address, i.e. 10.0.2.2. */
|
---|
386 | if ( (saddr.sin_addr.s_addr & RT_H2N_U32_C(IN_CLASSA_NET))
|
---|
387 | == RT_H2N_U32_C(INADDR_LOOPBACK & IN_CLASSA_NET))
|
---|
388 | saddr.sin_addr.s_addr = alias_addr.s_addr;
|
---|
389 |
|
---|
390 | daddr.sin_addr = so->so_laddr;
|
---|
391 | daddr.sin_port = so->so_lport;
|
---|
392 |
|
---|
393 | return udp_output2(pData, so, m, &saddr, &daddr, so->so_iptos);
|
---|
394 | }
|
---|
395 |
|
---|
396 | int
|
---|
397 | udp_attach(PNATState pData, struct socket *so, int service_port)
|
---|
398 | {
|
---|
399 | struct sockaddr_in *addr;
|
---|
400 | struct sockaddr sa_addr;
|
---|
401 | socklen_t socklen = sizeof(struct sockaddr);
|
---|
402 | int status;
|
---|
403 | int opt = 1;
|
---|
404 |
|
---|
405 | if ((so->s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
|
---|
406 | goto error;
|
---|
407 | /*
|
---|
408 | * Here, we bind() the socket. Although not really needed
|
---|
409 | * (sendto() on an unbound socket will bind it), it's done
|
---|
410 | * here so that emulation of ytalk etc. don't have to do it
|
---|
411 | */
|
---|
412 | memset(&sa_addr, 0, sizeof(struct sockaddr));
|
---|
413 | addr = (struct sockaddr_in *)&sa_addr;
|
---|
414 | #ifdef RT_OS_DARWIN
|
---|
415 | addr->sin_len = sizeof(struct sockaddr_in);
|
---|
416 | #endif
|
---|
417 | addr->sin_family = AF_INET;
|
---|
418 | addr->sin_port = service_port;
|
---|
419 | addr->sin_addr.s_addr = pData->bindIP.s_addr;
|
---|
420 | fd_nonblock(so->s);
|
---|
421 | if (bind(so->s, &sa_addr, sizeof(struct sockaddr_in)) < 0)
|
---|
422 | {
|
---|
423 | int lasterrno = errno;
|
---|
424 | closesocket(so->s);
|
---|
425 | so->s = -1;
|
---|
426 | #ifdef RT_OS_WINDOWS
|
---|
427 | WSASetLastError(lasterrno);
|
---|
428 | #else
|
---|
429 | errno = lasterrno;
|
---|
430 | #endif
|
---|
431 | goto error;
|
---|
432 | }
|
---|
433 | /* success, insert in queue */
|
---|
434 | so->so_expire = curtime + SO_EXPIRE;
|
---|
435 | /* enable broadcast for later use */
|
---|
436 | setsockopt(so->s, SOL_SOCKET, SO_BROADCAST, (const char *)&opt, sizeof(opt));
|
---|
437 | status = getsockname(so->s, &sa_addr, &socklen);
|
---|
438 | Assert(status == 0 && sa_addr.sa_family == AF_INET);
|
---|
439 | so->so_hlport = ((struct sockaddr_in *)&sa_addr)->sin_port;
|
---|
440 | so->so_hladdr.s_addr = ((struct sockaddr_in *)&sa_addr)->sin_addr.s_addr;
|
---|
441 | SOCKET_LOCK_CREATE(so);
|
---|
442 | QSOCKET_LOCK(udb);
|
---|
443 | insque(pData, so, &udb);
|
---|
444 | NSOCK_INC();
|
---|
445 | QSOCKET_UNLOCK(udb);
|
---|
446 | return so->s;
|
---|
447 | error:
|
---|
448 | LogRel(("NAT: can't create datagramm socket\n"));
|
---|
449 | return -1;
|
---|
450 | }
|
---|
451 |
|
---|
452 | void
|
---|
453 | udp_detach(PNATState pData, struct socket *so)
|
---|
454 | {
|
---|
455 | if (so != &pData->icmp_socket)
|
---|
456 | {
|
---|
457 | QSOCKET_LOCK(udb);
|
---|
458 | SOCKET_LOCK(so);
|
---|
459 | QSOCKET_UNLOCK(udb);
|
---|
460 | closesocket(so->s);
|
---|
461 | sofree(pData, so);
|
---|
462 | SOCKET_UNLOCK(so);
|
---|
463 | }
|
---|
464 | }
|
---|
465 |
|
---|
466 | static const struct tos_t udptos[] =
|
---|
467 | {
|
---|
468 | { 0, 53, IPTOS_LOWDELAY, 0 }, /* DNS */
|
---|
469 | { 517, 517, IPTOS_LOWDELAY, EMU_TALK }, /* talk */
|
---|
470 | { 518, 518, IPTOS_LOWDELAY, EMU_NTALK }, /* ntalk */
|
---|
471 | { 0, 7648, IPTOS_LOWDELAY, EMU_CUSEEME }, /* Cu-Seeme */
|
---|
472 | { 0, 0, 0, 0 }
|
---|
473 | };
|
---|
474 |
|
---|
475 | u_int8_t
|
---|
476 | udp_tos(struct socket *so)
|
---|
477 | {
|
---|
478 | int i = 0;
|
---|
479 |
|
---|
480 | while(udptos[i].tos)
|
---|
481 | {
|
---|
482 | if ( (udptos[i].fport && RT_N2H_U16(so->so_fport) == udptos[i].fport)
|
---|
483 | || (udptos[i].lport && RT_N2H_U16(so->so_lport) == udptos[i].lport))
|
---|
484 | {
|
---|
485 | so->so_emu = udptos[i].emu;
|
---|
486 | return udptos[i].tos;
|
---|
487 | }
|
---|
488 | i++;
|
---|
489 | }
|
---|
490 |
|
---|
491 | return 0;
|
---|
492 | }
|
---|
493 |
|
---|
494 | #ifdef EMULATE_TALK
|
---|
495 | #include "talkd.h"
|
---|
496 | #endif
|
---|
497 |
|
---|
498 | /*
|
---|
499 | * Here, talk/ytalk/ntalk requests must be emulated
|
---|
500 | */
|
---|
501 | void
|
---|
502 | udp_emu(PNATState pData, struct socket *so, struct mbuf *m)
|
---|
503 | {
|
---|
504 | so->so_emu = 0;
|
---|
505 | }
|
---|
506 |
|
---|
507 | struct socket *
|
---|
508 | udp_listen(PNATState pData, u_int32_t bind_addr, u_int port, u_int32_t laddr, u_int lport, int flags)
|
---|
509 | {
|
---|
510 | struct sockaddr_in addr;
|
---|
511 | struct socket *so;
|
---|
512 | socklen_t addrlen = sizeof(struct sockaddr_in);
|
---|
513 | int opt = 1;
|
---|
514 |
|
---|
515 | if ((so = socreate()) == NULL)
|
---|
516 | return NULL;
|
---|
517 |
|
---|
518 | so->s = socket(AF_INET, SOCK_DGRAM, 0);
|
---|
519 | if (so->s == -1)
|
---|
520 | {
|
---|
521 | LogRel(("NAT: can't create datagram socket\n"));
|
---|
522 | RTMemFree(so);
|
---|
523 | return NULL;
|
---|
524 | }
|
---|
525 | so->so_expire = curtime + SO_EXPIRE;
|
---|
526 | fd_nonblock(so->s);
|
---|
527 | SOCKET_LOCK_CREATE(so);
|
---|
528 | QSOCKET_LOCK(udb);
|
---|
529 | insque(pData, so, &udb);
|
---|
530 | NSOCK_INC();
|
---|
531 | QSOCKET_UNLOCK(udb);
|
---|
532 |
|
---|
533 | memset(&addr, 0, sizeof(addr));
|
---|
534 | #ifdef RT_OS_DARWIN
|
---|
535 | addr.sin_len = sizeof(addr);
|
---|
536 | #endif
|
---|
537 | addr.sin_family = AF_INET;
|
---|
538 | addr.sin_addr.s_addr = bind_addr;
|
---|
539 | addr.sin_port = port;
|
---|
540 |
|
---|
541 | if (bind(so->s,(struct sockaddr *)&addr, addrlen) < 0)
|
---|
542 | {
|
---|
543 | LogRel(("NAT: bind to %R[IP4] has been failed\n", &addr.sin_addr));
|
---|
544 | udp_detach(pData, so);
|
---|
545 | return NULL;
|
---|
546 | }
|
---|
547 | setsockopt(so->s, SOL_SOCKET, SO_REUSEADDR,(char *)&opt, sizeof(int));
|
---|
548 | /* setsockopt(so->s, SOL_SOCKET, SO_OOBINLINE,(char *)&opt, sizeof(int)); */
|
---|
549 |
|
---|
550 | getsockname(so->s,(struct sockaddr *)&addr,&addrlen);
|
---|
551 | so->so_fport = addr.sin_port;
|
---|
552 | /* The original check was completely broken, as the commented out
|
---|
553 | * if statement was always true (INADDR_ANY=0). */
|
---|
554 | /* if (addr.sin_addr.s_addr == 0 || addr.sin_addr.s_addr == loopback_addr.s_addr) */
|
---|
555 | if (1 == 0) /* always use the else part */
|
---|
556 | so->so_faddr = alias_addr;
|
---|
557 | else
|
---|
558 | so->so_faddr = addr.sin_addr;
|
---|
559 |
|
---|
560 | so->so_lport = lport;
|
---|
561 | so->so_laddr.s_addr = laddr;
|
---|
562 | if (flags != SS_FACCEPTONCE)
|
---|
563 | so->so_expire = 0;
|
---|
564 |
|
---|
565 | so->so_state = SS_ISFCONNECTED;
|
---|
566 |
|
---|
567 | return so;
|
---|
568 | }
|
---|