root / slirp / ip_icmp.c @ fcc941fe
History | View | Annotate | Download (10.5 kB)
1 | f0cbd3ec | bellard | /*
|
---|---|---|---|
2 | f0cbd3ec | bellard | * Copyright (c) 1982, 1986, 1988, 1993
|
3 | f0cbd3ec | bellard | * The Regents of the University of California. All rights reserved.
|
4 | f0cbd3ec | bellard | *
|
5 | f0cbd3ec | bellard | * Redistribution and use in source and binary forms, with or without
|
6 | f0cbd3ec | bellard | * modification, are permitted provided that the following conditions
|
7 | f0cbd3ec | bellard | * are met:
|
8 | f0cbd3ec | bellard | * 1. Redistributions of source code must retain the above copyright
|
9 | f0cbd3ec | bellard | * notice, this list of conditions and the following disclaimer.
|
10 | f0cbd3ec | bellard | * 2. Redistributions in binary form must reproduce the above copyright
|
11 | f0cbd3ec | bellard | * notice, this list of conditions and the following disclaimer in the
|
12 | f0cbd3ec | bellard | * documentation and/or other materials provided with the distribution.
|
13 | f0cbd3ec | bellard | * 3. All advertising materials mentioning features or use of this software
|
14 | f0cbd3ec | bellard | * must display the following acknowledgement:
|
15 | f0cbd3ec | bellard | * This product includes software developed by the University of
|
16 | f0cbd3ec | bellard | * California, Berkeley and its contributors.
|
17 | f0cbd3ec | bellard | * 4. Neither the name of the University nor the names of its contributors
|
18 | f0cbd3ec | bellard | * may be used to endorse or promote products derived from this software
|
19 | f0cbd3ec | bellard | * without specific prior written permission.
|
20 | f0cbd3ec | bellard | *
|
21 | f0cbd3ec | bellard | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
22 | f0cbd3ec | bellard | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
23 | f0cbd3ec | bellard | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
24 | f0cbd3ec | bellard | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
25 | f0cbd3ec | bellard | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
26 | f0cbd3ec | bellard | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
27 | f0cbd3ec | bellard | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
28 | f0cbd3ec | bellard | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
29 | f0cbd3ec | bellard | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
30 | f0cbd3ec | bellard | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
31 | f0cbd3ec | bellard | * SUCH DAMAGE.
|
32 | f0cbd3ec | bellard | *
|
33 | f0cbd3ec | bellard | * @(#)ip_icmp.c 8.2 (Berkeley) 1/4/94
|
34 | f0cbd3ec | bellard | * ip_icmp.c,v 1.7 1995/05/30 08:09:42 rgrimes Exp
|
35 | f0cbd3ec | bellard | */
|
36 | f0cbd3ec | bellard | |
37 | f0cbd3ec | bellard | #include "slirp.h" |
38 | f0cbd3ec | bellard | #include "ip_icmp.h" |
39 | f0cbd3ec | bellard | |
40 | f0cbd3ec | bellard | struct icmpstat icmpstat;
|
41 | f0cbd3ec | bellard | |
42 | f0cbd3ec | bellard | /* The message sent when emulating PING */
|
43 | f0cbd3ec | bellard | /* Be nice and tell them it's just a psuedo-ping packet */
|
44 | f0cbd3ec | bellard | char icmp_ping_msg[] = "This is a psuedo-PING packet used by Slirp to emulate ICMP ECHO-REQUEST packets.\n"; |
45 | f0cbd3ec | bellard | |
46 | f0cbd3ec | bellard | /* list of actions for icmp_error() on RX of an icmp message */
|
47 | f0cbd3ec | bellard | static int icmp_flush[19] = { |
48 | f0cbd3ec | bellard | /* ECHO REPLY (0) */ 0, |
49 | f0cbd3ec | bellard | 1,
|
50 | f0cbd3ec | bellard | 1,
|
51 | f0cbd3ec | bellard | /* DEST UNREACH (3) */ 1, |
52 | f0cbd3ec | bellard | /* SOURCE QUENCH (4)*/ 1, |
53 | f0cbd3ec | bellard | /* REDIRECT (5) */ 1, |
54 | f0cbd3ec | bellard | 1,
|
55 | f0cbd3ec | bellard | 1,
|
56 | f0cbd3ec | bellard | /* ECHO (8) */ 0, |
57 | f0cbd3ec | bellard | /* ROUTERADVERT (9) */ 1, |
58 | f0cbd3ec | bellard | /* ROUTERSOLICIT (10) */ 1, |
59 | f0cbd3ec | bellard | /* TIME EXCEEDED (11) */ 1, |
60 | f0cbd3ec | bellard | /* PARAMETER PROBLEM (12) */ 1, |
61 | f0cbd3ec | bellard | /* TIMESTAMP (13) */ 0, |
62 | f0cbd3ec | bellard | /* TIMESTAMP REPLY (14) */ 0, |
63 | f0cbd3ec | bellard | /* INFO (15) */ 0, |
64 | f0cbd3ec | bellard | /* INFO REPLY (16) */ 0, |
65 | f0cbd3ec | bellard | /* ADDR MASK (17) */ 0, |
66 | f0cbd3ec | bellard | /* ADDR MASK REPLY (18) */ 0 |
67 | f0cbd3ec | bellard | }; |
68 | f0cbd3ec | bellard | |
69 | f0cbd3ec | bellard | /*
|
70 | f0cbd3ec | bellard | * Process a received ICMP message.
|
71 | f0cbd3ec | bellard | */
|
72 | f0cbd3ec | bellard | void
|
73 | f0cbd3ec | bellard | icmp_input(m, hlen) |
74 | f0cbd3ec | bellard | struct mbuf *m;
|
75 | f0cbd3ec | bellard | int hlen;
|
76 | f0cbd3ec | bellard | { |
77 | f0cbd3ec | bellard | register struct icmp *icp; |
78 | f0cbd3ec | bellard | register struct ip *ip=mtod(m, struct ip *); |
79 | f0cbd3ec | bellard | int icmplen=ip->ip_len;
|
80 | f0cbd3ec | bellard | /* int code; */
|
81 | f0cbd3ec | bellard | |
82 | f0cbd3ec | bellard | DEBUG_CALL("icmp_input");
|
83 | f0cbd3ec | bellard | DEBUG_ARG("m = %lx", (long )m); |
84 | f0cbd3ec | bellard | DEBUG_ARG("m_len = %d", m->m_len);
|
85 | f0cbd3ec | bellard | |
86 | f0cbd3ec | bellard | icmpstat.icps_received++; |
87 | f0cbd3ec | bellard | |
88 | f0cbd3ec | bellard | /*
|
89 | f0cbd3ec | bellard | * Locate icmp structure in mbuf, and check
|
90 | f0cbd3ec | bellard | * that its not corrupted and of at least minimum length.
|
91 | f0cbd3ec | bellard | */
|
92 | f0cbd3ec | bellard | if (icmplen < ICMP_MINLEN) { /* min 8 bytes payload */ |
93 | f0cbd3ec | bellard | icmpstat.icps_tooshort++; |
94 | f0cbd3ec | bellard | freeit:
|
95 | f0cbd3ec | bellard | m_freem(m); |
96 | f0cbd3ec | bellard | goto end_error;
|
97 | f0cbd3ec | bellard | } |
98 | f0cbd3ec | bellard | |
99 | f0cbd3ec | bellard | m->m_len -= hlen; |
100 | f0cbd3ec | bellard | m->m_data += hlen; |
101 | f0cbd3ec | bellard | icp = mtod(m, struct icmp *);
|
102 | f0cbd3ec | bellard | if (cksum(m, icmplen)) {
|
103 | f0cbd3ec | bellard | icmpstat.icps_checksum++; |
104 | f0cbd3ec | bellard | goto freeit;
|
105 | f0cbd3ec | bellard | } |
106 | f0cbd3ec | bellard | m->m_len += hlen; |
107 | f0cbd3ec | bellard | m->m_data -= hlen; |
108 | f0cbd3ec | bellard | |
109 | f0cbd3ec | bellard | /* icmpstat.icps_inhist[icp->icmp_type]++; */
|
110 | f0cbd3ec | bellard | /* code = icp->icmp_code; */
|
111 | f0cbd3ec | bellard | |
112 | f0cbd3ec | bellard | DEBUG_ARG("icmp_type = %d", icp->icmp_type);
|
113 | f0cbd3ec | bellard | switch (icp->icmp_type) {
|
114 | f0cbd3ec | bellard | case ICMP_ECHO:
|
115 | f0cbd3ec | bellard | icp->icmp_type = ICMP_ECHOREPLY; |
116 | f0cbd3ec | bellard | ip->ip_len += hlen; /* since ip_input subtracts this */
|
117 | f0cbd3ec | bellard | if (ip->ip_dst.s_addr == our_addr.s_addr ||
|
118 | f0cbd3ec | bellard | (ip->ip_dst.s_addr == (special_addr.s_addr|htonl(CTL_ALIAS))) ) { |
119 | f0cbd3ec | bellard | icmp_reflect(m); |
120 | f0cbd3ec | bellard | } else {
|
121 | f0cbd3ec | bellard | struct socket *so;
|
122 | f0cbd3ec | bellard | struct sockaddr_in addr;
|
123 | f0cbd3ec | bellard | if ((so = socreate()) == NULL) goto freeit; |
124 | f0cbd3ec | bellard | if(udp_attach(so) == -1) { |
125 | f0cbd3ec | bellard | DEBUG_MISC((dfd,"icmp_input udp_attach errno = %d-%s\n",
|
126 | f0cbd3ec | bellard | errno,strerror(errno))); |
127 | f0cbd3ec | bellard | sofree(so); |
128 | f0cbd3ec | bellard | m_free(m); |
129 | f0cbd3ec | bellard | goto end_error;
|
130 | f0cbd3ec | bellard | } |
131 | f0cbd3ec | bellard | so->so_m = m; |
132 | f0cbd3ec | bellard | so->so_faddr = ip->ip_dst; |
133 | f0cbd3ec | bellard | so->so_fport = htons(7);
|
134 | f0cbd3ec | bellard | so->so_laddr = ip->ip_src; |
135 | f0cbd3ec | bellard | so->so_lport = htons(9);
|
136 | f0cbd3ec | bellard | so->so_iptos = ip->ip_tos; |
137 | f0cbd3ec | bellard | so->so_type = IPPROTO_ICMP; |
138 | f0cbd3ec | bellard | so->so_state = SS_ISFCONNECTED; |
139 | f0cbd3ec | bellard | |
140 | f0cbd3ec | bellard | /* Send the packet */
|
141 | f0cbd3ec | bellard | addr.sin_family = AF_INET; |
142 | f0cbd3ec | bellard | if ((so->so_faddr.s_addr & htonl(0xffffff00)) == special_addr.s_addr) { |
143 | f0cbd3ec | bellard | /* It's an alias */
|
144 | f0cbd3ec | bellard | switch(ntohl(so->so_faddr.s_addr) & 0xff) { |
145 | f0cbd3ec | bellard | case CTL_DNS:
|
146 | f0cbd3ec | bellard | addr.sin_addr = dns_addr; |
147 | f0cbd3ec | bellard | break;
|
148 | f0cbd3ec | bellard | case CTL_ALIAS:
|
149 | f0cbd3ec | bellard | default:
|
150 | f0cbd3ec | bellard | addr.sin_addr = loopback_addr; |
151 | f0cbd3ec | bellard | break;
|
152 | f0cbd3ec | bellard | } |
153 | f0cbd3ec | bellard | } else {
|
154 | f0cbd3ec | bellard | addr.sin_addr = so->so_faddr; |
155 | f0cbd3ec | bellard | } |
156 | f0cbd3ec | bellard | addr.sin_port = so->so_fport; |
157 | f0cbd3ec | bellard | if(sendto(so->s, icmp_ping_msg, strlen(icmp_ping_msg), 0, |
158 | f0cbd3ec | bellard | (struct sockaddr *)&addr, sizeof(addr)) == -1) { |
159 | f0cbd3ec | bellard | DEBUG_MISC((dfd,"icmp_input udp sendto tx errno = %d-%s\n",
|
160 | f0cbd3ec | bellard | errno,strerror(errno))); |
161 | f0cbd3ec | bellard | icmp_error(m, ICMP_UNREACH,ICMP_UNREACH_NET, 0,strerror(errno));
|
162 | f0cbd3ec | bellard | udp_detach(so); |
163 | f0cbd3ec | bellard | } |
164 | f0cbd3ec | bellard | } /* if ip->ip_dst.s_addr == our_addr.s_addr */
|
165 | f0cbd3ec | bellard | break;
|
166 | f0cbd3ec | bellard | case ICMP_UNREACH:
|
167 | f0cbd3ec | bellard | /* XXX? report error? close socket? */
|
168 | f0cbd3ec | bellard | case ICMP_TIMXCEED:
|
169 | f0cbd3ec | bellard | case ICMP_PARAMPROB:
|
170 | f0cbd3ec | bellard | case ICMP_SOURCEQUENCH:
|
171 | f0cbd3ec | bellard | case ICMP_TSTAMP:
|
172 | f0cbd3ec | bellard | case ICMP_MASKREQ:
|
173 | f0cbd3ec | bellard | case ICMP_REDIRECT:
|
174 | f0cbd3ec | bellard | icmpstat.icps_notsupp++; |
175 | f0cbd3ec | bellard | m_freem(m); |
176 | f0cbd3ec | bellard | break;
|
177 | f0cbd3ec | bellard | |
178 | f0cbd3ec | bellard | default:
|
179 | f0cbd3ec | bellard | icmpstat.icps_badtype++; |
180 | f0cbd3ec | bellard | m_freem(m); |
181 | f0cbd3ec | bellard | } /* swith */
|
182 | f0cbd3ec | bellard | |
183 | f0cbd3ec | bellard | end_error:
|
184 | f0cbd3ec | bellard | /* m is m_free()'d xor put in a socket xor or given to ip_send */
|
185 | f0cbd3ec | bellard | return;
|
186 | f0cbd3ec | bellard | } |
187 | f0cbd3ec | bellard | |
188 | f0cbd3ec | bellard | |
189 | f0cbd3ec | bellard | /*
|
190 | f0cbd3ec | bellard | * Send an ICMP message in response to a situation
|
191 | f0cbd3ec | bellard | *
|
192 | f0cbd3ec | bellard | * RFC 1122: 3.2.2 MUST send at least the IP header and 8 bytes of header. MAY send more (we do).
|
193 | f0cbd3ec | bellard | * MUST NOT change this header information.
|
194 | f0cbd3ec | bellard | * MUST NOT reply to a multicast/broadcast IP address.
|
195 | f0cbd3ec | bellard | * MUST NOT reply to a multicast/broadcast MAC address.
|
196 | f0cbd3ec | bellard | * MUST reply to only the first fragment.
|
197 | f0cbd3ec | bellard | */
|
198 | f0cbd3ec | bellard | /*
|
199 | f0cbd3ec | bellard | * Send ICMP_UNREACH back to the source regarding msrc.
|
200 | f0cbd3ec | bellard | * mbuf *msrc is used as a template, but is NOT m_free()'d.
|
201 | f0cbd3ec | bellard | * It is reported as the bad ip packet. The header should
|
202 | f0cbd3ec | bellard | * be fully correct and in host byte order.
|
203 | f0cbd3ec | bellard | * ICMP fragmentation is illegal. All machines must accept 576 bytes in one
|
204 | f0cbd3ec | bellard | * packet. The maximum payload is 576-20(ip hdr)-8(icmp hdr)=548
|
205 | f0cbd3ec | bellard | */
|
206 | f0cbd3ec | bellard | |
207 | f0cbd3ec | bellard | #define ICMP_MAXDATALEN (IP_MSS-28) |
208 | f0cbd3ec | bellard | void
|
209 | f0cbd3ec | bellard | icmp_error(msrc, type, code, minsize, message) |
210 | f0cbd3ec | bellard | struct mbuf *msrc;
|
211 | f0cbd3ec | bellard | u_char type; |
212 | f0cbd3ec | bellard | u_char code; |
213 | f0cbd3ec | bellard | int minsize;
|
214 | f0cbd3ec | bellard | char *message;
|
215 | f0cbd3ec | bellard | { |
216 | f0cbd3ec | bellard | unsigned hlen, shlen, s_ip_len;
|
217 | f0cbd3ec | bellard | register struct ip *ip; |
218 | f0cbd3ec | bellard | register struct icmp *icp; |
219 | f0cbd3ec | bellard | register struct mbuf *m; |
220 | f0cbd3ec | bellard | |
221 | f0cbd3ec | bellard | DEBUG_CALL("icmp_error");
|
222 | f0cbd3ec | bellard | DEBUG_ARG("msrc = %lx", (long )msrc); |
223 | f0cbd3ec | bellard | DEBUG_ARG("msrc_len = %d", msrc->m_len);
|
224 | f0cbd3ec | bellard | |
225 | f0cbd3ec | bellard | if(type!=ICMP_UNREACH && type!=ICMP_TIMXCEED) goto end_error; |
226 | f0cbd3ec | bellard | |
227 | f0cbd3ec | bellard | /* check msrc */
|
228 | f0cbd3ec | bellard | if(!msrc) goto end_error; |
229 | f0cbd3ec | bellard | ip = mtod(msrc, struct ip *);
|
230 | f0cbd3ec | bellard | #if DEBUG
|
231 | f0cbd3ec | bellard | { char bufa[20], bufb[20]; |
232 | f0cbd3ec | bellard | strcpy(bufa, inet_ntoa(ip->ip_src)); |
233 | f0cbd3ec | bellard | strcpy(bufb, inet_ntoa(ip->ip_dst)); |
234 | f0cbd3ec | bellard | DEBUG_MISC((dfd, " %.16s to %.16s\n", bufa, bufb));
|
235 | f0cbd3ec | bellard | } |
236 | f0cbd3ec | bellard | #endif
|
237 | f0cbd3ec | bellard | if(ip->ip_off & IP_OFFMASK) goto end_error; /* Only reply to fragment 0 */ |
238 | f0cbd3ec | bellard | |
239 | f0cbd3ec | bellard | shlen=ip->ip_hl << 2;
|
240 | f0cbd3ec | bellard | s_ip_len=ip->ip_len; |
241 | f0cbd3ec | bellard | if(ip->ip_p == IPPROTO_ICMP) {
|
242 | f0cbd3ec | bellard | icp = (struct icmp *)((char *)ip + shlen); |
243 | f0cbd3ec | bellard | /*
|
244 | f0cbd3ec | bellard | * Assume any unknown ICMP type is an error. This isn't
|
245 | f0cbd3ec | bellard | * specified by the RFC, but think about it..
|
246 | f0cbd3ec | bellard | */
|
247 | f0cbd3ec | bellard | if(icp->icmp_type>18 || icmp_flush[icp->icmp_type]) goto end_error; |
248 | f0cbd3ec | bellard | } |
249 | f0cbd3ec | bellard | |
250 | f0cbd3ec | bellard | /* make a copy */
|
251 | f0cbd3ec | bellard | if(!(m=m_get())) goto end_error; /* get mbuf */ |
252 | f0cbd3ec | bellard | { int new_m_size;
|
253 | f0cbd3ec | bellard | new_m_size=sizeof(struct ip )+ICMP_MINLEN+msrc->m_len+ICMP_MAXDATALEN; |
254 | f0cbd3ec | bellard | if(new_m_size>m->m_size) m_inc(m, new_m_size);
|
255 | f0cbd3ec | bellard | } |
256 | f0cbd3ec | bellard | memcpy(m->m_data, msrc->m_data, msrc->m_len); |
257 | f0cbd3ec | bellard | m->m_len = msrc->m_len; /* copy msrc to m */
|
258 | f0cbd3ec | bellard | |
259 | f0cbd3ec | bellard | /* make the header of the reply packet */
|
260 | f0cbd3ec | bellard | ip = mtod(m, struct ip *);
|
261 | f0cbd3ec | bellard | hlen= sizeof(struct ip ); /* no options in reply */ |
262 | f0cbd3ec | bellard | |
263 | f0cbd3ec | bellard | /* fill in icmp */
|
264 | f0cbd3ec | bellard | m->m_data += hlen; |
265 | f0cbd3ec | bellard | m->m_len -= hlen; |
266 | f0cbd3ec | bellard | |
267 | f0cbd3ec | bellard | icp = mtod(m, struct icmp *);
|
268 | f0cbd3ec | bellard | |
269 | f0cbd3ec | bellard | if(minsize) s_ip_len=shlen+ICMP_MINLEN; /* return header+8b only */ |
270 | f0cbd3ec | bellard | else if(s_ip_len>ICMP_MAXDATALEN) /* maximum size */ |
271 | f0cbd3ec | bellard | s_ip_len=ICMP_MAXDATALEN; |
272 | f0cbd3ec | bellard | |
273 | f0cbd3ec | bellard | m->m_len=ICMP_MINLEN+s_ip_len; /* 8 bytes ICMP header */
|
274 | f0cbd3ec | bellard | |
275 | f0cbd3ec | bellard | /* min. size = 8+sizeof(struct ip)+8 */
|
276 | f0cbd3ec | bellard | |
277 | f0cbd3ec | bellard | icp->icmp_type = type; |
278 | f0cbd3ec | bellard | icp->icmp_code = code; |
279 | f0cbd3ec | bellard | icp->icmp_id = 0;
|
280 | f0cbd3ec | bellard | icp->icmp_seq = 0;
|
281 | f0cbd3ec | bellard | |
282 | f0cbd3ec | bellard | memcpy(&icp->icmp_ip, msrc->m_data, s_ip_len); /* report the ip packet */
|
283 | f0cbd3ec | bellard | HTONS(icp->icmp_ip.ip_len); |
284 | f0cbd3ec | bellard | HTONS(icp->icmp_ip.ip_id); |
285 | f0cbd3ec | bellard | HTONS(icp->icmp_ip.ip_off); |
286 | f0cbd3ec | bellard | |
287 | f0cbd3ec | bellard | #if DEBUG
|
288 | f0cbd3ec | bellard | if(message) { /* DEBUG : append message to ICMP packet */ |
289 | f0cbd3ec | bellard | int message_len;
|
290 | f0cbd3ec | bellard | char *cpnt;
|
291 | f0cbd3ec | bellard | message_len=strlen(message); |
292 | f0cbd3ec | bellard | if(message_len>ICMP_MAXDATALEN) message_len=ICMP_MAXDATALEN;
|
293 | f0cbd3ec | bellard | cpnt=(char *)m->m_data+m->m_len;
|
294 | f0cbd3ec | bellard | memcpy(cpnt, message, message_len); |
295 | f0cbd3ec | bellard | m->m_len+=message_len; |
296 | f0cbd3ec | bellard | } |
297 | f0cbd3ec | bellard | #endif
|
298 | f0cbd3ec | bellard | |
299 | f0cbd3ec | bellard | icp->icmp_cksum = 0;
|
300 | f0cbd3ec | bellard | icp->icmp_cksum = cksum(m, m->m_len); |
301 | f0cbd3ec | bellard | |
302 | f0cbd3ec | bellard | m->m_data -= hlen; |
303 | f0cbd3ec | bellard | m->m_len += hlen; |
304 | f0cbd3ec | bellard | |
305 | f0cbd3ec | bellard | /* fill in ip */
|
306 | f0cbd3ec | bellard | ip->ip_hl = hlen >> 2;
|
307 | f0cbd3ec | bellard | ip->ip_len = m->m_len; |
308 | f0cbd3ec | bellard | |
309 | f0cbd3ec | bellard | ip->ip_tos=((ip->ip_tos & 0x1E) | 0xC0); /* high priority for errors */ |
310 | f0cbd3ec | bellard | |
311 | f0cbd3ec | bellard | ip->ip_ttl = MAXTTL; |
312 | f0cbd3ec | bellard | ip->ip_p = IPPROTO_ICMP; |
313 | f0cbd3ec | bellard | ip->ip_dst = ip->ip_src; /* ip adresses */
|
314 | f0cbd3ec | bellard | ip->ip_src = our_addr; |
315 | f0cbd3ec | bellard | |
316 | f0cbd3ec | bellard | (void ) ip_output((struct socket *)NULL, m); |
317 | f0cbd3ec | bellard | |
318 | f0cbd3ec | bellard | icmpstat.icps_reflect++; |
319 | f0cbd3ec | bellard | |
320 | f0cbd3ec | bellard | end_error:
|
321 | f0cbd3ec | bellard | return;
|
322 | f0cbd3ec | bellard | } |
323 | f0cbd3ec | bellard | #undef ICMP_MAXDATALEN
|
324 | f0cbd3ec | bellard | |
325 | f0cbd3ec | bellard | /*
|
326 | f0cbd3ec | bellard | * Reflect the ip packet back to the source
|
327 | f0cbd3ec | bellard | */
|
328 | f0cbd3ec | bellard | void
|
329 | f0cbd3ec | bellard | icmp_reflect(m) |
330 | f0cbd3ec | bellard | struct mbuf *m;
|
331 | f0cbd3ec | bellard | { |
332 | f0cbd3ec | bellard | register struct ip *ip = mtod(m, struct ip *); |
333 | f0cbd3ec | bellard | int hlen = ip->ip_hl << 2; |
334 | f0cbd3ec | bellard | int optlen = hlen - sizeof(struct ip ); |
335 | f0cbd3ec | bellard | register struct icmp *icp; |
336 | f0cbd3ec | bellard | |
337 | f0cbd3ec | bellard | /*
|
338 | f0cbd3ec | bellard | * Send an icmp packet back to the ip level,
|
339 | f0cbd3ec | bellard | * after supplying a checksum.
|
340 | f0cbd3ec | bellard | */
|
341 | f0cbd3ec | bellard | m->m_data += hlen; |
342 | f0cbd3ec | bellard | m->m_len -= hlen; |
343 | f0cbd3ec | bellard | icp = mtod(m, struct icmp *);
|
344 | f0cbd3ec | bellard | |
345 | f0cbd3ec | bellard | icp->icmp_cksum = 0;
|
346 | f0cbd3ec | bellard | icp->icmp_cksum = cksum(m, ip->ip_len - hlen); |
347 | f0cbd3ec | bellard | |
348 | f0cbd3ec | bellard | m->m_data -= hlen; |
349 | f0cbd3ec | bellard | m->m_len += hlen; |
350 | f0cbd3ec | bellard | |
351 | f0cbd3ec | bellard | /* fill in ip */
|
352 | f0cbd3ec | bellard | if (optlen > 0) { |
353 | f0cbd3ec | bellard | /*
|
354 | f0cbd3ec | bellard | * Strip out original options by copying rest of first
|
355 | f0cbd3ec | bellard | * mbuf's data back, and adjust the IP length.
|
356 | f0cbd3ec | bellard | */
|
357 | f0cbd3ec | bellard | memmove((caddr_t)(ip + 1), (caddr_t)ip + hlen,
|
358 | f0cbd3ec | bellard | (unsigned )(m->m_len - hlen));
|
359 | f0cbd3ec | bellard | hlen -= optlen; |
360 | f0cbd3ec | bellard | ip->ip_hl = hlen >> 2;
|
361 | f0cbd3ec | bellard | ip->ip_len -= optlen; |
362 | f0cbd3ec | bellard | m->m_len -= optlen; |
363 | f0cbd3ec | bellard | } |
364 | f0cbd3ec | bellard | |
365 | f0cbd3ec | bellard | ip->ip_ttl = MAXTTL; |
366 | f0cbd3ec | bellard | { /* swap */
|
367 | f0cbd3ec | bellard | struct in_addr icmp_dst;
|
368 | f0cbd3ec | bellard | icmp_dst = ip->ip_dst; |
369 | f0cbd3ec | bellard | ip->ip_dst = ip->ip_src; |
370 | f0cbd3ec | bellard | ip->ip_src = icmp_dst; |
371 | f0cbd3ec | bellard | } |
372 | f0cbd3ec | bellard | |
373 | f0cbd3ec | bellard | (void ) ip_output((struct socket *)NULL, m); |
374 | f0cbd3ec | bellard | |
375 | f0cbd3ec | bellard | icmpstat.icps_reflect++; |
376 | f0cbd3ec | bellard | } |