1 |
/*
|
2 |
* icmp.cpp - ip router
|
3 |
*
|
4 |
* Basilisk II (C) 1997-2005 Christian Bauer
|
5 |
*
|
6 |
* Windows platform specific code copyright (C) Lauri Pesonen
|
7 |
*
|
8 |
* This program is free software; you can redistribute it and/or modify
|
9 |
* it under the terms of the GNU General Public License as published by
|
10 |
* the Free Software Foundation; either version 2 of the License, or
|
11 |
* (at your option) any later version.
|
12 |
*
|
13 |
* This program is distributed in the hope that it will be useful,
|
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
16 |
* GNU General Public License for more details.
|
17 |
*
|
18 |
* You should have received a copy of the GNU General Public License
|
19 |
* along with this program; if not, write to the Free Software
|
20 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
21 |
*/
|
22 |
|
23 |
#include "sysdeps.h"
|
24 |
#include "cpu_emulation.h"
|
25 |
#include "ws2tcpip.h"
|
26 |
#include "prefs.h"
|
27 |
#include "ether_windows.h"
|
28 |
#include "ether.h"
|
29 |
#include "router.h"
|
30 |
#include "router_types.h"
|
31 |
#include "dynsockets.h"
|
32 |
#include "ipsocket.h"
|
33 |
#include "iphelp.h"
|
34 |
#include "icmp.h"
|
35 |
#include "dump.h"
|
36 |
|
37 |
|
38 |
#if DEBUG
|
39 |
#pragma optimize("",off)
|
40 |
#endif
|
41 |
|
42 |
#include "debug.h"
|
43 |
|
44 |
|
45 |
// Saved for cleanup.
|
46 |
static socket_t *icmp_incoming_s = 0;
|
47 |
|
48 |
|
49 |
void stop_icmp_listen()
|
50 |
{
|
51 |
if(icmp_incoming_s) {
|
52 |
delete icmp_incoming_s;
|
53 |
icmp_incoming_s = 0;
|
54 |
}
|
55 |
}
|
56 |
|
57 |
void start_icmp_listen()
|
58 |
{
|
59 |
if(!icmp_incoming_s) {
|
60 |
icmp_incoming_s = new socket_t(IPPROTO_ICMP);
|
61 |
|
62 |
icmp_incoming_s->permanent = TRUE;
|
63 |
icmp_incoming_s->s = _socket( AF_INET, SOCK_RAW, IPPROTO_ICMP );
|
64 |
|
65 |
memset( &icmp_incoming_s->from, 0, icmp_incoming_s->from_len );
|
66 |
icmp_incoming_s->from.sin_family = AF_INET;
|
67 |
|
68 |
if(icmp_incoming_s->s == INVALID_SOCKET) {
|
69 |
D(bug("Failed to create icmp listening socket (NT/no admin?)\r\n" ));
|
70 |
delete icmp_incoming_s;
|
71 |
icmp_incoming_s = 0;
|
72 |
} else {
|
73 |
D(bug("icmp listening socket created\r\n" ));
|
74 |
raw_sockets_available = true;
|
75 |
struct sockaddr_in to;
|
76 |
memset( &to, 0, sizeof(to) );
|
77 |
to.sin_family = AF_INET;
|
78 |
if( _bind ( icmp_incoming_s->s, (const struct sockaddr *)&to, sizeof(to) ) == SOCKET_ERROR ) {
|
79 |
D(bug("Listening to inbound icmp failed, error code = %d\r\n", _WSAGetLastError() ));
|
80 |
_closesocket( icmp_incoming_s->s );
|
81 |
delete icmp_incoming_s;
|
82 |
icmp_incoming_s = 0;
|
83 |
} else {
|
84 |
D(bug("icmp listening socket bound\r\n" ));
|
85 |
if(!icmp_incoming_s->b_recfrom()) {
|
86 |
D(bug("b_recfrom() from inbound icmp failed, error code = %d\r\n", _WSAGetLastError() ));
|
87 |
// _closesocket( icmp_incoming_s->s );
|
88 |
// delete icmp_incoming_s;
|
89 |
// icmp_incoming_s = 0;
|
90 |
}
|
91 |
}
|
92 |
}
|
93 |
}
|
94 |
}
|
95 |
|
96 |
void CALLBACK icmp_read_completion(
|
97 |
DWORD error,
|
98 |
DWORD bytes_read,
|
99 |
LPWSAOVERLAPPED lpOverlapped,
|
100 |
DWORD flags
|
101 |
)
|
102 |
{
|
103 |
D(bug("icmp_read_completion(error=0x%x, bytes_read=%d, flags=0x%x)\r\n", error, bytes_read, flags));
|
104 |
|
105 |
socket_t *cmpl = (socket_t *)lpOverlapped->hEvent;
|
106 |
|
107 |
if(error == 0 && macos_ip_address != 0) {
|
108 |
if(bytes_read > 1460) {
|
109 |
D(bug("discarding oversized icmp packet, size = \r\n", bytes_read));
|
110 |
} else {
|
111 |
int icmp_size = sizeof(mac_t) + bytes_read;
|
112 |
icmp_t *icmp = (icmp_t *)malloc( icmp_size );
|
113 |
if(icmp) {
|
114 |
mac_t *mac = (mac_t *)icmp;
|
115 |
ip_t *ip = (ip_t *)icmp;
|
116 |
|
117 |
memcpy( mac->dest, ether_addr, 6 );
|
118 |
memcpy( mac->src, router_mac_addr, 6 );
|
119 |
mac->type = htons(mac_type_ip4);
|
120 |
|
121 |
// Copy payload (used by ICMP checksum)
|
122 |
memcpy( (char *)icmp + sizeof(mac_t), cmpl->buffers[0].buf, bytes_read );
|
123 |
|
124 |
switch( icmp->type ) {
|
125 |
// May need to patch the returned ip header.
|
126 |
case icmp_Destination_unreachable:
|
127 |
case icmp_Source_quench:
|
128 |
case icmp_Redirect:
|
129 |
case icmp_Time_exceeded:
|
130 |
case icmp_Parameter_problem:
|
131 |
ip_t *ip_if = (ip_t *)( (char *)icmp + sizeof(icmp_t) + sizeof(uint32) - sizeof(mac_t) );
|
132 |
|
133 |
// This would be needed (traceroute)
|
134 |
// ip_if->ident = ??;
|
135 |
|
136 |
// Cannot fix some fields, this should be enough:
|
137 |
ip_if->src = htonl(macos_ip_address);
|
138 |
|
139 |
if(ip_if->proto == ip_proto_udp) {
|
140 |
udp_t *udp_if = (udp_t *)ip_if;
|
141 |
// udp_if->src_port = ... don't know!;
|
142 |
} else if(ip_if->proto == ip_proto_tcp) {
|
143 |
tcp_t *tcp_if = (tcp_t *)ip_if;
|
144 |
// tcp_if->src_port = ... don't know!;
|
145 |
}
|
146 |
break;
|
147 |
}
|
148 |
|
149 |
make_icmp_checksum( icmp, icmp_size );
|
150 |
|
151 |
// Replace the target ip address
|
152 |
ip->dest = htonl(macos_ip_address);
|
153 |
ip->ttl--;
|
154 |
make_ip4_checksum( ip );
|
155 |
|
156 |
dump_bytes( (uint8 *)icmp, icmp_size );
|
157 |
|
158 |
if( ip->ttl == 0 ) {
|
159 |
D(bug("icmp packet ttl expired\r\n"));
|
160 |
} else {
|
161 |
enqueue_packet( (uint8 *)icmp, icmp_size );
|
162 |
}
|
163 |
free(icmp);
|
164 |
}
|
165 |
}
|
166 |
}
|
167 |
|
168 |
memset( &cmpl->from, 0, cmpl->from_len );
|
169 |
|
170 |
if(is_router_shutting_down) {
|
171 |
delete cmpl;
|
172 |
} else if(cmpl->s == INVALID_SOCKET || !cmpl->b_recfrom()) {
|
173 |
// delete cmpl;
|
174 |
}
|
175 |
}
|
176 |
|
177 |
void write_icmp( icmp_t *icmp, int len )
|
178 |
{
|
179 |
struct in_addr ia;
|
180 |
ia.s_addr = icmp->ip.dest;
|
181 |
D(bug("write_icmp(%s)\r\n", _inet_ntoa(ia) ));
|
182 |
|
183 |
if(!raw_sockets_available) {
|
184 |
D(bug("write_icmp() cannot proceed, raw sockets not available\r\n" ));
|
185 |
return;
|
186 |
}
|
187 |
|
188 |
if(len < sizeof(icmp_t)) {
|
189 |
D(bug("Too small icmp packet(%d), dropped\r\n", len));
|
190 |
return;
|
191 |
}
|
192 |
|
193 |
// must be updated, ttl changed
|
194 |
make_icmp_checksum( icmp, len );
|
195 |
|
196 |
SOCKET s = _socket( AF_INET, SOCK_RAW, IPPROTO_ICMP );
|
197 |
if(s != INVALID_SOCKET) {
|
198 |
struct sockaddr_in to;
|
199 |
memset( &to, 0, sizeof(to) );
|
200 |
to.sin_family = AF_INET;
|
201 |
to.sin_addr.s_addr = icmp->ip.dest;
|
202 |
|
203 |
char *data = (char *)icmp + sizeof(ip_t);
|
204 |
int dlen = len - sizeof(ip_t);
|
205 |
|
206 |
int ttl = icmp->ip.ttl;
|
207 |
if(_setsockopt( s, IPPROTO_IP, IP_TTL, (const char *)&ttl, sizeof(int) ) == SOCKET_ERROR ) {
|
208 |
D(bug("could not set ttl to %d.\r\n", ttl));
|
209 |
} else {
|
210 |
D(bug("ttl set to %d.\r\n", ttl));
|
211 |
}
|
212 |
|
213 |
if(SOCKET_ERROR == _sendto( s, data, dlen, 0, (struct sockaddr *)&to, sizeof(to) )) {
|
214 |
D(bug("Failed to send icmp via raw socket\r\n" ));
|
215 |
}
|
216 |
_closesocket(s);
|
217 |
} else {
|
218 |
D(bug("Could not create raw socket for icmp\r\n" ));
|
219 |
}
|
220 |
}
|