Commit | Line | Data |
---|---|---|
15637ed4 RG |
1 | /* |
2 | * Copyright (c) 1982, 1986, 1989 Regents of the University of California. | |
3 | * 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 | * | |
38e82238 | 33 | * from: @(#)ip_icmp.c 7.15 (Berkeley) 4/20/91 |
4cf615e1 | 34 | * $Id: ip_icmp.c,v 1.6 1993/12/19 00:52:42 wollman Exp $ |
15637ed4 RG |
35 | */ |
36 | ||
37 | #include "param.h" | |
38 | #include "systm.h" | |
39 | #include "malloc.h" | |
40 | #include "mbuf.h" | |
41 | #include "protosw.h" | |
42 | #include "socket.h" | |
43 | #include "time.h" | |
44 | #include "kernel.h" | |
45 | ||
46 | #include "../net/route.h" | |
47 | #include "../net/if.h" | |
48 | ||
49 | #include "in.h" | |
50 | #include "in_systm.h" | |
51 | #include "in_var.h" | |
52 | #include "ip.h" | |
53 | #include "ip_icmp.h" | |
54 | #include "icmp_var.h" | |
55 | ||
56 | /* | |
57 | * ICMP routines: error generation, receive packet processing, and | |
58 | * routines to turnaround packets back to the originator, and | |
59 | * host table maintenance routines. | |
60 | */ | |
15637ed4 | 61 | |
2cb63509 GW |
62 | #define satosin(sa) ((struct sockaddr_in *)(sa)) |
63 | static void icmp_reflect(struct mbuf *); | |
64 | static void icmp_send(struct mbuf *, struct mbuf *); | |
65 | ||
66 | ||
15637ed4 RG |
67 | /* |
68 | * Generate an error packet of type error | |
69 | * in response to bad packet ip. | |
70 | */ | |
2cb63509 GW |
71 | void |
72 | icmp_error(n, type, code, dest, mtu) | |
15637ed4 RG |
73 | struct mbuf *n; |
74 | int type, code; | |
75 | struct in_addr dest; | |
2cb63509 | 76 | int mtu; /* mtu for ICMP_UNREACH_SRCFRAG */ |
15637ed4 RG |
77 | { |
78 | register struct ip *oip = mtod(n, struct ip *), *nip; | |
79 | register unsigned oiplen = oip->ip_hl << 2; | |
80 | register struct icmp *icp; | |
4c45483e | 81 | register struct mbuf *m = 0; |
15637ed4 | 82 | unsigned icmplen; |
2cb63509 | 83 | u_long oaddr; |
15637ed4 RG |
84 | |
85 | #ifdef ICMPPRINTFS | |
86 | if (icmpprintfs) | |
87 | printf("icmp_error(%x, %d, %d)\n", oip, type, code); | |
88 | #endif | |
89 | if (type != ICMP_REDIRECT) | |
90 | icmpstat.icps_error++; | |
2cb63509 GW |
91 | |
92 | /* | |
93 | * Quoth RFC 1122 (Requirements for Internet Hosts): | |
94 | * | |
95 | * An ICMP error message MUST NOT be sent as the result of | |
96 | * receiving: | |
97 | * - an ICMP error message, or | |
98 | * - a datagram destined to an IP broadcast or IP multicast | |
99 | * address, or | |
100 | * - a datagram sent as a link-layer broadcast, or | |
101 | * - a non-initial fragment, or | |
102 | * - a datagram whose source address does not define a single | |
103 | * host -- e.g., a zero address, a loopback address, a | |
104 | * broadcast address, a multicast address, or a Class E | |
105 | * address. | |
106 | * | |
107 | * NOTE: THESE RESTRICTIONS TAKE PRECEDENCE OVER ANY REQUIREMENT | |
108 | * ELSEWHERE IN THIS DOCUMENT FOR SENDING ICMP ERROR MESSAGES. | |
109 | */ | |
110 | ||
111 | oaddr = ntohl(oip->ip_src.s_addr); | |
112 | ||
113 | /* | |
114 | * Don't send error messages to multicast or broadcast addresses. | |
115 | */ | |
116 | if (IN_MULTICAST(oaddr) | |
117 | || oaddr == INADDR_BROADCAST | |
118 | || n->m_flags & (M_BCAST | M_MCAST)) { | |
119 | icmpstat.icps_oldmcast++; | |
120 | goto freeit; | |
121 | } | |
122 | ||
123 | /* | |
124 | * Don't send error messages to zero addresses or class E's. | |
125 | */ | |
126 | if (IN_EXPERIMENTAL(oaddr) | |
127 | || ! in_lnaof(oip->ip_src) | |
128 | || ! in_netof(oip->ip_src)) { | |
129 | icmpstat.icps_oldbadaddr++; | |
130 | goto freeit; | |
131 | } | |
132 | ||
133 | /* | |
134 | * Don't send error messages to loopback addresses. | |
135 | * As a special (unauthorized) exception, we check to see | |
136 | * if the packet came from the loopback interface. If it | |
137 | * did, then we should allow the errors through, because | |
138 | * the upper layers rely on them. | |
139 | */ | |
140 | if(in_netof(oip->ip_src) == IN_LOOPBACKNET | |
141 | && !(m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK)) { | |
142 | icmpstat.icps_oldbadaddr++; | |
143 | goto freeit; | |
144 | } | |
145 | ||
15637ed4 RG |
146 | /* |
147 | * Don't send error if not the first fragment of message. | |
148 | * Don't error if the old packet protocol was ICMP | |
149 | * error message, only known informational types. | |
150 | */ | |
151 | if (oip->ip_off &~ (IP_MF|IP_DF)) | |
152 | goto freeit; | |
153 | if (oip->ip_p == IPPROTO_ICMP && type != ICMP_REDIRECT && | |
154 | n->m_len >= oiplen + ICMP_MINLEN && | |
155 | !ICMP_INFOTYPE(((struct icmp *)((caddr_t)oip + oiplen))->icmp_type)) { | |
156 | icmpstat.icps_oldicmp++; | |
157 | goto freeit; | |
158 | } | |
4cf615e1 JH |
159 | #ifdef MULTICAST |
160 | /* Don't send error in response to a multicast or broadcast packet */ | |
161 | if(n->m_flags & (M_MCAST | M_BCAST)) | |
162 | goto freeit; | |
163 | #endif | |
15637ed4 RG |
164 | |
165 | /* | |
166 | * First, formulate icmp message | |
167 | */ | |
168 | m = m_gethdr(M_DONTWAIT, MT_HEADER); | |
169 | if (m == NULL) | |
170 | goto freeit; | |
171 | icmplen = oiplen + min(8, oip->ip_len); | |
172 | m->m_len = icmplen + ICMP_MINLEN; | |
173 | MH_ALIGN(m, m->m_len); | |
174 | icp = mtod(m, struct icmp *); | |
175 | if ((u_int)type > ICMP_MAXTYPE) | |
176 | panic("icmp_error"); | |
177 | icmpstat.icps_outhist[type]++; | |
178 | icp->icmp_type = type; | |
179 | if (type == ICMP_REDIRECT) | |
180 | icp->icmp_gwaddr = dest; | |
2cb63509 GW |
181 | else if (type == ICMP_UNREACH && code == ICMP_UNREACH_NEEDFRAG) { |
182 | icp->icmp_mtu = htons(mtu); | |
183 | icp->icmp_mtuvoid = 0; | |
184 | } else { | |
15637ed4 | 185 | icp->icmp_void = 0; |
2cb63509 | 186 | } |
15637ed4 RG |
187 | if (type == ICMP_PARAMPROB) { |
188 | icp->icmp_pptr = code; | |
189 | code = 0; | |
190 | } | |
191 | icp->icmp_code = code; | |
192 | bcopy((caddr_t)oip, (caddr_t)&icp->icmp_ip, icmplen); | |
193 | nip = &icp->icmp_ip; | |
194 | nip->ip_len = htons((u_short)(nip->ip_len + oiplen)); | |
195 | ||
196 | /* | |
197 | * Now, copy old ip header (without options) | |
198 | * in front of icmp message. | |
199 | */ | |
200 | if (m->m_data - sizeof(struct ip) < m->m_pktdat) | |
201 | panic("icmp len"); | |
202 | m->m_data -= sizeof(struct ip); | |
203 | m->m_len += sizeof(struct ip); | |
204 | m->m_pkthdr.len = m->m_len; | |
205 | m->m_pkthdr.rcvif = n->m_pkthdr.rcvif; | |
206 | nip = mtod(m, struct ip *); | |
207 | bcopy((caddr_t)oip, (caddr_t)nip, oiplen); | |
208 | nip->ip_len = m->m_len; | |
209 | nip->ip_hl = sizeof(struct ip) >> 2; | |
210 | nip->ip_p = IPPROTO_ICMP; | |
211 | icmp_reflect(m); | |
212 | ||
213 | freeit: | |
214 | m_freem(n); | |
215 | } | |
216 | ||
217 | static struct sockproto icmproto = { AF_INET, IPPROTO_ICMP }; | |
218 | static struct sockaddr_in icmpsrc = { sizeof (struct sockaddr_in), AF_INET }; | |
219 | static struct sockaddr_in icmpdst = { sizeof (struct sockaddr_in), AF_INET }; | |
220 | static struct sockaddr_in icmpgw = { sizeof (struct sockaddr_in), AF_INET }; | |
15637ed4 RG |
221 | |
222 | /* | |
223 | * Process a received ICMP message. | |
224 | */ | |
2cb63509 | 225 | void |
15637ed4 RG |
226 | icmp_input(m, hlen) |
227 | register struct mbuf *m; | |
228 | int hlen; | |
229 | { | |
230 | register struct icmp *icp; | |
231 | register struct ip *ip = mtod(m, struct ip *); | |
232 | int icmplen = ip->ip_len; | |
233 | register int i; | |
234 | struct in_ifaddr *ia; | |
fde1aeb2 GW |
235 | in_ctlinput_t *ctlfunc; |
236 | int code; | |
15637ed4 RG |
237 | |
238 | /* | |
239 | * Locate icmp structure in mbuf, and check | |
240 | * that not corrupted and of at least minimum length. | |
241 | */ | |
242 | #ifdef ICMPPRINTFS | |
243 | if (icmpprintfs) | |
244 | printf("icmp_input from %x, len %d\n", ip->ip_src, icmplen); | |
245 | #endif | |
246 | if (icmplen < ICMP_MINLEN) { | |
247 | icmpstat.icps_tooshort++; | |
248 | goto freeit; | |
249 | } | |
250 | i = hlen + MIN(icmplen, ICMP_ADVLENMIN); | |
251 | if (m->m_len < i && (m = m_pullup(m, i)) == 0) { | |
252 | icmpstat.icps_tooshort++; | |
253 | return; | |
254 | } | |
255 | ip = mtod(m, struct ip *); | |
256 | m->m_len -= hlen; | |
257 | m->m_data += hlen; | |
258 | icp = mtod(m, struct icmp *); | |
259 | if (in_cksum(m, icmplen)) { | |
260 | icmpstat.icps_checksum++; | |
261 | goto freeit; | |
262 | } | |
263 | m->m_len += hlen; | |
264 | m->m_data -= hlen; | |
265 | ||
266 | #ifdef ICMPPRINTFS | |
267 | /* | |
268 | * Message type specific processing. | |
269 | */ | |
270 | if (icmpprintfs) | |
271 | printf("icmp_input, type %d code %d\n", icp->icmp_type, | |
272 | icp->icmp_code); | |
273 | #endif | |
274 | if (icp->icmp_type > ICMP_MAXTYPE) | |
275 | goto raw; | |
276 | icmpstat.icps_inhist[icp->icmp_type]++; | |
277 | code = icp->icmp_code; | |
278 | switch (icp->icmp_type) { | |
279 | ||
280 | case ICMP_UNREACH: | |
2cb63509 | 281 | if (code > ICMP_UNREACH_MAXCODE) |
15637ed4 | 282 | goto badcode; |
2cb63509 GW |
283 | if (code == ICMP_UNREACH_NEEDFRAG) { |
284 | #ifdef MTUDISC | |
285 | /* | |
286 | * Need to adjust the routing tables immediately; | |
287 | * when ULP's get the PRC_MSGSIZE, it is their | |
288 | * responsibility to notice it and update their | |
289 | * internal ideas of MTU-derived protocol parameters. | |
290 | */ | |
291 | in_mtureduce(icp->icmp_ip.ip_dst, | |
292 | ntohs(icp->icmp_mtu)); | |
293 | code = PRC_MSGSIZE; | |
294 | #endif /* MTUDISC */ | |
295 | } else { | |
296 | code += PRC_UNREACH_NET; | |
297 | } | |
15637ed4 RG |
298 | goto deliver; |
299 | ||
300 | case ICMP_TIMXCEED: | |
2cb63509 | 301 | if (code > ICMP_TIMXCEED_MAXCODE) |
15637ed4 RG |
302 | goto badcode; |
303 | code += PRC_TIMXCEED_INTRANS; | |
304 | goto deliver; | |
305 | ||
306 | case ICMP_PARAMPROB: | |
2cb63509 | 307 | if (code > ICMP_PARAMPROB_MAXCODE) |
15637ed4 | 308 | goto badcode; |
2cb63509 | 309 | code += PRC_PARAMPROB; |
15637ed4 RG |
310 | goto deliver; |
311 | ||
312 | case ICMP_SOURCEQUENCH: | |
313 | if (code) | |
314 | goto badcode; | |
315 | code = PRC_QUENCH; | |
316 | deliver: | |
317 | /* | |
318 | * Problem with datagram; advise higher level routines. | |
319 | */ | |
320 | if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) || | |
321 | icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) { | |
322 | icmpstat.icps_badlen++; | |
323 | goto freeit; | |
324 | } | |
325 | NTOHS(icp->icmp_ip.ip_len); | |
326 | #ifdef ICMPPRINTFS | |
327 | if (icmpprintfs) | |
328 | printf("deliver to protocol %d\n", icp->icmp_ip.ip_p); | |
329 | #endif | |
330 | icmpsrc.sin_addr = icp->icmp_ip.ip_dst; | |
331 | if (ctlfunc = inetsw[ip_protox[icp->icmp_ip.ip_p]].pr_ctlinput) | |
332 | (*ctlfunc)(code, (struct sockaddr *)&icmpsrc, | |
fde1aeb2 | 333 | &icp->icmp_ip); |
15637ed4 RG |
334 | break; |
335 | ||
336 | badcode: | |
337 | icmpstat.icps_badcode++; | |
338 | break; | |
339 | ||
2cb63509 GW |
340 | /* |
341 | * Always respond to pings from valid addresses. | |
342 | * Don't respond to broadcast pings unless ipbroadcastecho | |
343 | * is set. Don't respond to multicast pings unless | |
344 | * ipbraodcastecho is set AND we support multicasting | |
345 | * to begin with. (Per RFC 1122, we may choose either.) | |
346 | */ | |
15637ed4 | 347 | case ICMP_ECHO: |
2cb63509 GW |
348 | { |
349 | u_long srcaddr = ntohl(icp->icmp_ip.ip_src.s_addr); | |
350 | #ifdef MULTICAST | |
351 | if(IN_MULTICAST(srcaddr) && !ipbroadcastecho) | |
352 | break; | |
353 | #else /* not MULTICAST */ | |
354 | if(IN_MULTICAST(srcaddr)) | |
355 | break; | |
356 | #endif /* not MULTICAST */ | |
357 | if((srcaddr == INADDR_BROADCAST | |
358 | || m->m_flags & M_BCAST) | |
359 | && !ipbroadcastecho) | |
360 | break; | |
361 | } | |
362 | ||
15637ed4 RG |
363 | icp->icmp_type = ICMP_ECHOREPLY; |
364 | goto reflect; | |
365 | ||
366 | case ICMP_TSTAMP: | |
367 | if (icmplen < ICMP_TSLEN) { | |
368 | icmpstat.icps_badlen++; | |
369 | break; | |
370 | } | |
371 | icp->icmp_type = ICMP_TSTAMPREPLY; | |
372 | icp->icmp_rtime = iptime(); | |
373 | icp->icmp_ttime = icp->icmp_rtime; /* bogus, do later! */ | |
374 | goto reflect; | |
375 | ||
2cb63509 GW |
376 | /* |
377 | * Per RFC 1122, only respond to ICMP mask requests | |
378 | * if the administrator has SPECIFICALLY CONFIGURED | |
379 | * this host as an address mask agent. | |
380 | */ | |
15637ed4 | 381 | case ICMP_MASKREQ: |
2cb63509 GW |
382 | if (!ipmaskagent) |
383 | break; | |
15637ed4 RG |
384 | if (icmplen < ICMP_MASKLEN || |
385 | (ia = ifptoia(m->m_pkthdr.rcvif)) == 0) | |
386 | break; | |
387 | icp->icmp_type = ICMP_MASKREPLY; | |
388 | icp->icmp_mask = ia->ia_sockmask.sin_addr.s_addr; | |
389 | if (ip->ip_src.s_addr == 0) { | |
390 | if (ia->ia_ifp->if_flags & IFF_BROADCAST) | |
391 | ip->ip_src = satosin(&ia->ia_broadaddr)->sin_addr; | |
392 | else if (ia->ia_ifp->if_flags & IFF_POINTOPOINT) | |
393 | ip->ip_src = satosin(&ia->ia_dstaddr)->sin_addr; | |
394 | } | |
395 | reflect: | |
396 | ip->ip_len += hlen; /* since ip_input deducts this */ | |
397 | icmpstat.icps_reflect++; | |
398 | icmpstat.icps_outhist[icp->icmp_type]++; | |
399 | icmp_reflect(m); | |
400 | return; | |
401 | ||
402 | case ICMP_REDIRECT: | |
403 | if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp)) { | |
404 | icmpstat.icps_badlen++; | |
405 | break; | |
406 | } | |
407 | /* | |
408 | * Short circuit routing redirects to force | |
409 | * immediate change in the kernel's routing | |
410 | * tables. The message is also handed to anyone | |
411 | * listening on a raw socket (e.g. the routing | |
412 | * daemon for use in updating its tables). | |
413 | */ | |
414 | icmpgw.sin_addr = ip->ip_src; | |
415 | icmpdst.sin_addr = icp->icmp_gwaddr; | |
416 | #ifdef ICMPPRINTFS | |
417 | if (icmpprintfs) | |
418 | printf("redirect dst %x to %x\n", icp->icmp_ip.ip_dst, | |
419 | icp->icmp_gwaddr); | |
420 | #endif | |
2cb63509 GW |
421 | /* |
422 | * Per RFC 1122, throw away redirects that | |
423 | * suggested places we can't get to, or | |
424 | * an interface other than the one the packet | |
425 | * arrived on. | |
426 | * | |
427 | * It also says that we SHOULD throw away | |
428 | * redirects that come from someone other | |
429 | * than the current first-hop gateway for the | |
430 | * specified destination. | |
431 | * | |
432 | * These are both implemented as general policy | |
433 | * by rtredirect(). | |
434 | */ | |
15637ed4 | 435 | if (code == ICMP_REDIRECT_NET || code == ICMP_REDIRECT_TOSNET) { |
15637ed4 RG |
436 | icmpsrc.sin_addr = |
437 | in_makeaddr(in_netof(icp->icmp_ip.ip_dst), INADDR_ANY); | |
438 | in_sockmaskof(icp->icmp_ip.ip_dst, &icmpmask); | |
439 | rtredirect((struct sockaddr *)&icmpsrc, | |
440 | (struct sockaddr *)&icmpdst, | |
441 | (struct sockaddr *)&icmpmask, RTF_GATEWAY, | |
442 | (struct sockaddr *)&icmpgw, (struct rtentry **)0); | |
443 | icmpsrc.sin_addr = icp->icmp_ip.ip_dst; | |
444 | pfctlinput(PRC_REDIRECT_NET, | |
445 | (struct sockaddr *)&icmpsrc); | |
446 | } else { | |
447 | icmpsrc.sin_addr = icp->icmp_ip.ip_dst; | |
448 | rtredirect((struct sockaddr *)&icmpsrc, | |
449 | (struct sockaddr *)&icmpdst, | |
450 | (struct sockaddr *)0, RTF_GATEWAY | RTF_HOST, | |
451 | (struct sockaddr *)&icmpgw, (struct rtentry **)0); | |
452 | pfctlinput(PRC_REDIRECT_HOST, | |
453 | (struct sockaddr *)&icmpsrc); | |
454 | } | |
455 | break; | |
456 | ||
457 | /* | |
458 | * No kernel processing for the following; | |
459 | * just fall through to send to raw listener. | |
460 | */ | |
461 | case ICMP_ECHOREPLY: | |
462 | case ICMP_TSTAMPREPLY: | |
463 | case ICMP_IREQREPLY: | |
464 | case ICMP_MASKREPLY: | |
465 | default: | |
466 | break; | |
467 | } | |
468 | ||
469 | raw: | |
470 | icmpsrc.sin_addr = ip->ip_src; | |
471 | icmpdst.sin_addr = ip->ip_dst; | |
472 | (void) raw_input(m, &icmproto, (struct sockaddr *)&icmpsrc, | |
473 | (struct sockaddr *)&icmpdst); | |
474 | return; | |
475 | ||
476 | freeit: | |
477 | m_freem(m); | |
478 | } | |
479 | ||
480 | /* | |
481 | * Reflect the ip packet back to the source | |
482 | */ | |
2cb63509 | 483 | static void |
15637ed4 RG |
484 | icmp_reflect(m) |
485 | struct mbuf *m; | |
486 | { | |
487 | register struct ip *ip = mtod(m, struct ip *); | |
488 | register struct in_ifaddr *ia; | |
489 | struct in_addr t; | |
490 | struct mbuf *opts = 0, *ip_srcroute(); | |
491 | int optlen = (ip->ip_hl << 2) - sizeof(struct ip); | |
492 | ||
493 | t = ip->ip_dst; | |
494 | ip->ip_dst = ip->ip_src; | |
495 | /* | |
496 | * If the incoming packet was addressed directly to us, | |
497 | * use dst as the src for the reply. Otherwise (broadcast | |
498 | * or anonymous), use the address which corresponds | |
499 | * to the incoming interface. | |
500 | */ | |
501 | for (ia = in_ifaddr; ia; ia = ia->ia_next) { | |
502 | if (t.s_addr == IA_SIN(ia)->sin_addr.s_addr) | |
503 | break; | |
504 | if ((ia->ia_ifp->if_flags & IFF_BROADCAST) && | |
505 | t.s_addr == satosin(&ia->ia_broadaddr)->sin_addr.s_addr) | |
506 | break; | |
507 | } | |
508 | if (ia == (struct in_ifaddr *)0) | |
509 | ia = ifptoia(m->m_pkthdr.rcvif); | |
510 | if (ia == (struct in_ifaddr *)0) | |
511 | ia = in_ifaddr; | |
512 | t = IA_SIN(ia)->sin_addr; | |
513 | ip->ip_src = t; | |
514 | ip->ip_ttl = MAXTTL; | |
515 | ||
516 | if (optlen > 0) { | |
517 | register u_char *cp; | |
518 | int opt, cnt; | |
519 | u_int len; | |
520 | ||
521 | /* | |
522 | * Retrieve any source routing from the incoming packet; | |
523 | * add on any record-route or timestamp options. | |
524 | */ | |
525 | cp = (u_char *) (ip + 1); | |
526 | if ((opts = ip_srcroute()) == 0 && | |
527 | (opts = m_gethdr(M_DONTWAIT, MT_HEADER))) { | |
528 | opts->m_len = sizeof(struct in_addr); | |
529 | mtod(opts, struct in_addr *)->s_addr = 0; | |
530 | } | |
531 | if (opts) { | |
532 | #ifdef ICMPPRINTFS | |
533 | if (icmpprintfs) | |
534 | printf("icmp_reflect optlen %d rt %d => ", | |
535 | optlen, opts->m_len); | |
536 | #endif | |
537 | for (cnt = optlen; cnt > 0; cnt -= len, cp += len) { | |
538 | opt = cp[IPOPT_OPTVAL]; | |
539 | if (opt == IPOPT_EOL) | |
540 | break; | |
541 | if (opt == IPOPT_NOP) | |
542 | len = 1; | |
543 | else { | |
544 | len = cp[IPOPT_OLEN]; | |
545 | if (len <= 0 || len > cnt) | |
546 | break; | |
547 | } | |
548 | /* | |
549 | * should check for overflow, but it "can't happen" | |
550 | */ | |
551 | if (opt == IPOPT_RR || opt == IPOPT_TS) { | |
552 | bcopy((caddr_t)cp, | |
553 | mtod(opts, caddr_t) + opts->m_len, len); | |
554 | opts->m_len += len; | |
555 | } | |
556 | } | |
557 | if (opts->m_len % 4 != 0) { | |
558 | *(mtod(opts, caddr_t) + opts->m_len) = IPOPT_EOL; | |
559 | opts->m_len++; | |
560 | } | |
561 | #ifdef ICMPPRINTFS | |
562 | if (icmpprintfs) | |
563 | printf("%d\n", opts->m_len); | |
564 | #endif | |
565 | } | |
566 | /* | |
567 | * Now strip out original options by copying rest of first | |
568 | * mbuf's data back, and adjust the IP length. | |
569 | */ | |
570 | ip->ip_len -= optlen; | |
571 | ip->ip_hl = sizeof(struct ip) >> 2; | |
572 | m->m_len -= optlen; | |
573 | if (m->m_flags & M_PKTHDR) | |
574 | m->m_pkthdr.len -= optlen; | |
575 | optlen += sizeof(struct ip); | |
576 | bcopy((caddr_t)ip + optlen, (caddr_t)(ip + 1), | |
577 | (unsigned)(m->m_len - sizeof(struct ip))); | |
578 | } | |
579 | icmp_send(m, opts); | |
580 | if (opts) | |
581 | (void)m_free(opts); | |
582 | } | |
583 | ||
584 | struct in_ifaddr * | |
585 | ifptoia(ifp) | |
586 | struct ifnet *ifp; | |
587 | { | |
588 | register struct in_ifaddr *ia; | |
589 | ||
590 | for (ia = in_ifaddr; ia; ia = ia->ia_next) | |
591 | if (ia->ia_ifp == ifp) | |
592 | return (ia); | |
593 | return ((struct in_ifaddr *)0); | |
594 | } | |
595 | ||
596 | /* | |
597 | * Send an icmp packet back to the ip level, | |
598 | * after supplying a checksum. | |
599 | */ | |
2cb63509 | 600 | static void |
15637ed4 RG |
601 | icmp_send(m, opts) |
602 | register struct mbuf *m; | |
603 | struct mbuf *opts; | |
604 | { | |
605 | register struct ip *ip = mtod(m, struct ip *); | |
606 | register int hlen; | |
607 | register struct icmp *icp; | |
608 | ||
609 | hlen = ip->ip_hl << 2; | |
610 | m->m_data += hlen; | |
611 | m->m_len -= hlen; | |
612 | icp = mtod(m, struct icmp *); | |
613 | icp->icmp_cksum = 0; | |
614 | icp->icmp_cksum = in_cksum(m, ip->ip_len - hlen); | |
615 | m->m_data -= hlen; | |
616 | m->m_len += hlen; | |
617 | #ifdef ICMPPRINTFS | |
618 | if (icmpprintfs) | |
619 | printf("icmp_send dst %x src %x\n", ip->ip_dst, ip->ip_src); | |
620 | #endif | |
621 | (void) ip_output(m, opts, (struct route *)0, 0); | |
622 | } | |
623 | ||
624 | n_time | |
625 | iptime() | |
626 | { | |
627 | struct timeval atv; | |
628 | u_long t; | |
629 | ||
630 | microtime(&atv); | |
631 | t = (atv.tv_sec % (24*60*60)) * 1000 + atv.tv_usec / 1000; | |
632 | return (htonl(t)); | |
633 | } |