1 | /* |
---|
2 | * Copyright (c) 1982, 1986, 1988, 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 | * 4. Neither the name of the University nor the names of its contributors |
---|
14 | * may be used to endorse or promote products derived from this software |
---|
15 | * without specific prior written permission. |
---|
16 | * |
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
---|
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
---|
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
27 | * SUCH DAMAGE. |
---|
28 | * |
---|
29 | * @(#)ip_icmp.c 8.2 (Berkeley) 1/4/94 |
---|
30 | * $FreeBSD: src/sys/netinet/ip_icmp.c,v 1.101 2005/05/04 13:23:54 andre Exp $ |
---|
31 | */ |
---|
32 | |
---|
33 | /* |
---|
34 | * $Id$ |
---|
35 | */ |
---|
36 | |
---|
37 | #include <sys/param.h> |
---|
38 | #include <sys/systm.h> |
---|
39 | #include <sys/malloc.h> |
---|
40 | #include <sys/mbuf.h> |
---|
41 | #include <sys/protosw.h> |
---|
42 | #include <sys/socket.h> |
---|
43 | #include <sys/time.h> |
---|
44 | #include <sys/kernel.h> |
---|
45 | #include <sys/socket.h> |
---|
46 | #include <sys/sysctl.h> |
---|
47 | |
---|
48 | #include <net/if.h> |
---|
49 | #include <net/route.h> |
---|
50 | |
---|
51 | #define _IP_VHL |
---|
52 | #include <netinet/in.h> |
---|
53 | #include <netinet/in_systm.h> |
---|
54 | #include <netinet/in_var.h> |
---|
55 | #include <netinet/ip.h> |
---|
56 | #include <netinet/ip_icmp.h> |
---|
57 | #include <netinet/ip_var.h> |
---|
58 | #include <netinet/icmp_var.h> |
---|
59 | |
---|
60 | #ifdef IPSEC |
---|
61 | #include <netinet6/ipsec.h> |
---|
62 | #include <netkey/key.h> |
---|
63 | #endif |
---|
64 | |
---|
65 | #ifdef FAST_IPSEC |
---|
66 | #include <netipsec/ipsec.h> |
---|
67 | #include <netipsec/key.h> |
---|
68 | #define IPSEC |
---|
69 | #endif |
---|
70 | |
---|
71 | #include <machine/in_cksum.h> |
---|
72 | |
---|
73 | /* |
---|
74 | * ICMP routines: error generation, receive packet processing, and |
---|
75 | * routines to turnaround packets back to the originator, and |
---|
76 | * host table maintenance routines. |
---|
77 | */ |
---|
78 | |
---|
79 | struct icmpstat icmpstat; |
---|
80 | SYSCTL_STRUCT(_net_inet_icmp, ICMPCTL_STATS, stats, CTLFLAG_RD, |
---|
81 | &icmpstat, icmpstat, ""); |
---|
82 | |
---|
83 | static int icmpmaskrepl = 0; |
---|
84 | SYSCTL_INT(_net_inet_icmp, ICMPCTL_MASKREPL, maskrepl, CTLFLAG_RW, |
---|
85 | &icmpmaskrepl, 0, ""); |
---|
86 | |
---|
87 | static int icmpbmcastecho = 1; |
---|
88 | SYSCTL_INT(_net_inet_icmp, OID_AUTO, bmcastecho, CTLFLAG_RW, &icmpbmcastecho, |
---|
89 | 0, ""); |
---|
90 | |
---|
91 | static int icmpallecho = 1; |
---|
92 | SYSCTL_INT(_net_inet_icmp, OID_AUTO, allecho, CTLFLAG_RW, &icmpallecho, |
---|
93 | 0, ""); |
---|
94 | |
---|
95 | /* #define ICMPPRINTFS 1 */ |
---|
96 | #ifdef ICMPPRINTFS |
---|
97 | int icmpprintfs = 0; |
---|
98 | #endif |
---|
99 | |
---|
100 | static void icmp_reflect(struct mbuf *); |
---|
101 | static void icmp_send(struct mbuf *, struct mbuf *); |
---|
102 | |
---|
103 | extern struct protosw inetsw[]; |
---|
104 | unsigned int icmplenPanicAvoided; |
---|
105 | |
---|
106 | /* |
---|
107 | * Generate an error packet of type error |
---|
108 | * in response to bad packet ip. |
---|
109 | */ |
---|
110 | void |
---|
111 | icmp_error(struct mbuf *n, int type, int code, n_long dest, |
---|
112 | struct ifnet *destifp) |
---|
113 | { |
---|
114 | register struct ip *oip = mtod(n, struct ip *), *nip; |
---|
115 | #ifdef _IP_VHL |
---|
116 | register unsigned oiplen = IP_VHL_HL(oip->ip_vhl) << 2; |
---|
117 | #else |
---|
118 | register unsigned oiplen = oip->ip_hl << 2; |
---|
119 | #endif |
---|
120 | register struct icmp *icp; |
---|
121 | register struct mbuf *m; |
---|
122 | unsigned icmplen; |
---|
123 | |
---|
124 | #ifdef ICMPPRINTFS |
---|
125 | if (icmpprintfs) |
---|
126 | printf("icmp_error(%p, %x, %d)\n", oip, type, code); |
---|
127 | #endif |
---|
128 | if (type != ICMP_REDIRECT) |
---|
129 | icmpstat.icps_error++; |
---|
130 | /* |
---|
131 | * Don't send error if not the first fragment of message. |
---|
132 | * Don't error if the old packet protocol was ICMP |
---|
133 | * error message, only known informational types. |
---|
134 | */ |
---|
135 | if (oip->ip_off &~ (IP_MF|IP_DF)) |
---|
136 | goto freeit; |
---|
137 | if (oip->ip_p == IPPROTO_ICMP && type != ICMP_REDIRECT && |
---|
138 | n->m_len >= oiplen + ICMP_MINLEN && |
---|
139 | !ICMP_INFOTYPE(((struct icmp *)((caddr_t)oip + oiplen))->icmp_type)) { |
---|
140 | icmpstat.icps_oldicmp++; |
---|
141 | goto freeit; |
---|
142 | } |
---|
143 | /* Don't send error in response to a multicast or broadcast packet */ |
---|
144 | if (n->m_flags & (M_BCAST|M_MCAST)) |
---|
145 | goto freeit; |
---|
146 | /* Don't send error in response to malicious packet */ |
---|
147 | icmplen = min(oiplen + 8, oip->ip_len); |
---|
148 | if (icmplen < sizeof(struct ip)) { |
---|
149 | icmplenPanicAvoided++; |
---|
150 | goto freeit; |
---|
151 | } |
---|
152 | |
---|
153 | /* |
---|
154 | * First, formulate icmp message |
---|
155 | */ |
---|
156 | m = m_gethdr(M_DONTWAIT, MT_HEADER); |
---|
157 | if (m == NULL) |
---|
158 | goto freeit; |
---|
159 | #ifdef MAC |
---|
160 | mac_create_mbuf_netlayer(n, m); |
---|
161 | #endif |
---|
162 | m->m_len = icmplen + ICMP_MINLEN; |
---|
163 | MH_ALIGN(m, m->m_len); |
---|
164 | icp = mtod(m, struct icmp *); |
---|
165 | if ((u_int)type > ICMP_MAXTYPE) |
---|
166 | panic("icmp_error"); |
---|
167 | icmpstat.icps_outhist[type]++; |
---|
168 | icp->icmp_type = type; |
---|
169 | if (type == ICMP_REDIRECT) |
---|
170 | icp->icmp_gwaddr.s_addr = dest; |
---|
171 | else { |
---|
172 | icp->icmp_void = 0; |
---|
173 | /* |
---|
174 | * The following assignments assume an overlay with the |
---|
175 | * zeroed icmp_void field. |
---|
176 | */ |
---|
177 | if (type == ICMP_PARAMPROB) { |
---|
178 | icp->icmp_pptr = code; |
---|
179 | code = 0; |
---|
180 | } else if (type == ICMP_UNREACH && |
---|
181 | code == ICMP_UNREACH_NEEDFRAG && destifp) { |
---|
182 | icp->icmp_nextmtu = htons(destifp->if_mtu); |
---|
183 | } |
---|
184 | } |
---|
185 | |
---|
186 | icp->icmp_code = code; |
---|
187 | bcopy((caddr_t)oip, (caddr_t)&icp->icmp_ip, icmplen); |
---|
188 | nip = &icp->icmp_ip; |
---|
189 | nip->ip_len = htons((u_short)(nip->ip_len + oiplen)); |
---|
190 | |
---|
191 | /* |
---|
192 | * Now, copy old ip header (without options) |
---|
193 | * in front of icmp message. |
---|
194 | */ |
---|
195 | if (m->m_data - sizeof(struct ip) < m->m_pktdat) |
---|
196 | panic("icmp len"); |
---|
197 | m->m_data -= sizeof(struct ip); |
---|
198 | m->m_len += sizeof(struct ip); |
---|
199 | m->m_pkthdr.len = m->m_len; |
---|
200 | m->m_pkthdr.rcvif = n->m_pkthdr.rcvif; |
---|
201 | nip = mtod(m, struct ip *); |
---|
202 | bcopy((caddr_t)oip, (caddr_t)nip, sizeof(struct ip)); |
---|
203 | nip->ip_len = m->m_len; |
---|
204 | #ifdef _IP_VHL |
---|
205 | nip->ip_vhl = IP_VHL_BORING; |
---|
206 | #else |
---|
207 | nip->ip_v = IPVERSION; |
---|
208 | nip->ip_hl = 5; |
---|
209 | #endif |
---|
210 | nip->ip_p = IPPROTO_ICMP; |
---|
211 | nip->ip_tos = 0; |
---|
212 | icmp_reflect(m); |
---|
213 | |
---|
214 | freeit: |
---|
215 | m_freem(n); |
---|
216 | } |
---|
217 | |
---|
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 }; |
---|
221 | |
---|
222 | /* |
---|
223 | * Process a received ICMP message. |
---|
224 | */ |
---|
225 | void |
---|
226 | icmp_input(struct mbuf *m, int off) |
---|
227 | { |
---|
228 | struct icmp *icp; |
---|
229 | struct in_ifaddr *ia; |
---|
230 | struct ip *ip = mtod(m, struct ip *); |
---|
231 | int hlen = off; |
---|
232 | int icmplen = ip->ip_len; |
---|
233 | int i, code; |
---|
234 | void (*ctlfunc)(int, struct sockaddr *, void *); |
---|
235 | |
---|
236 | /* |
---|
237 | * Locate icmp structure in mbuf, and check |
---|
238 | * that not corrupted and of at least minimum length. |
---|
239 | */ |
---|
240 | #ifdef ICMPPRINTFS |
---|
241 | if (icmpprintfs) { |
---|
242 | char buf[4 * sizeof "123"]; |
---|
243 | strcpy(buf, inet_ntoa(ip->ip_src)); |
---|
244 | printf("icmp_input from %s to %s, len %d\n", |
---|
245 | buf, inet_ntoa(ip->ip_dst), icmplen); |
---|
246 | } |
---|
247 | #endif |
---|
248 | if (icmplen < ICMP_MINLEN) { |
---|
249 | icmpstat.icps_tooshort++; |
---|
250 | goto freeit; |
---|
251 | } |
---|
252 | i = hlen + min(icmplen, ICMP_ADVLENMIN); |
---|
253 | if (m->m_len < i && (m = m_pullup(m, i)) == 0) { |
---|
254 | icmpstat.icps_tooshort++; |
---|
255 | return; |
---|
256 | } |
---|
257 | ip = mtod(m, struct ip *); |
---|
258 | m->m_len -= hlen; |
---|
259 | m->m_data += hlen; |
---|
260 | icp = mtod(m, struct icmp *); |
---|
261 | if (in_cksum(m, icmplen)) { |
---|
262 | icmpstat.icps_checksum++; |
---|
263 | goto freeit; |
---|
264 | } |
---|
265 | m->m_len += hlen; |
---|
266 | m->m_data -= hlen; |
---|
267 | |
---|
268 | #ifdef ICMPPRINTFS |
---|
269 | if (icmpprintfs) |
---|
270 | printf("icmp_input, type %d code %d\n", icp->icmp_type, |
---|
271 | icp->icmp_code); |
---|
272 | #endif |
---|
273 | |
---|
274 | /* |
---|
275 | * Message type specific processing. |
---|
276 | */ |
---|
277 | if (icp->icmp_type > ICMP_MAXTYPE) |
---|
278 | goto raw; |
---|
279 | icmpstat.icps_inhist[icp->icmp_type]++; |
---|
280 | code = icp->icmp_code; |
---|
281 | switch (icp->icmp_type) { |
---|
282 | |
---|
283 | case ICMP_UNREACH: |
---|
284 | switch (code) { |
---|
285 | case ICMP_UNREACH_NET: |
---|
286 | case ICMP_UNREACH_HOST: |
---|
287 | case ICMP_UNREACH_PROTOCOL: |
---|
288 | case ICMP_UNREACH_PORT: |
---|
289 | case ICMP_UNREACH_SRCFAIL: |
---|
290 | code = PRC_UNREACH_NET; |
---|
291 | break; |
---|
292 | |
---|
293 | case ICMP_UNREACH_NEEDFRAG: |
---|
294 | code = PRC_MSGSIZE; |
---|
295 | break; |
---|
296 | |
---|
297 | case ICMP_UNREACH_NET_UNKNOWN: |
---|
298 | case ICMP_UNREACH_NET_PROHIB: |
---|
299 | case ICMP_UNREACH_TOSNET: |
---|
300 | code = PRC_UNREACH_NET; |
---|
301 | break; |
---|
302 | |
---|
303 | case ICMP_UNREACH_HOST_UNKNOWN: |
---|
304 | case ICMP_UNREACH_ISOLATED: |
---|
305 | case ICMP_UNREACH_HOST_PROHIB: |
---|
306 | case ICMP_UNREACH_TOSHOST: |
---|
307 | code = PRC_UNREACH_HOST; |
---|
308 | break; |
---|
309 | |
---|
310 | case ICMP_UNREACH_FILTER_PROHIB: |
---|
311 | case ICMP_UNREACH_HOST_PRECEDENCE: |
---|
312 | case ICMP_UNREACH_PRECEDENCE_CUTOFF: |
---|
313 | code = PRC_UNREACH_PORT; |
---|
314 | break; |
---|
315 | |
---|
316 | default: |
---|
317 | goto badcode; |
---|
318 | } |
---|
319 | goto deliver; |
---|
320 | |
---|
321 | case ICMP_TIMXCEED: |
---|
322 | if (code > 1) |
---|
323 | goto badcode; |
---|
324 | code += PRC_TIMXCEED_INTRANS; |
---|
325 | goto deliver; |
---|
326 | |
---|
327 | case ICMP_PARAMPROB: |
---|
328 | if (code > 1) |
---|
329 | goto badcode; |
---|
330 | code = PRC_PARAMPROB; |
---|
331 | goto deliver; |
---|
332 | |
---|
333 | case ICMP_SOURCEQUENCH: |
---|
334 | if (code) |
---|
335 | goto badcode; |
---|
336 | code = PRC_QUENCH; |
---|
337 | deliver: |
---|
338 | /* |
---|
339 | * Problem with datagram; advise higher level routines. |
---|
340 | */ |
---|
341 | if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) || |
---|
342 | #ifdef _IP_VHL |
---|
343 | IP_VHL_HL(icp->icmp_ip.ip_vhl) < (sizeof(struct ip) >> 2)) { |
---|
344 | #else |
---|
345 | icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) { |
---|
346 | #endif |
---|
347 | icmpstat.icps_badlen++; |
---|
348 | goto freeit; |
---|
349 | } |
---|
350 | NTOHS(icp->icmp_ip.ip_len); |
---|
351 | /* Discard ICMP's in response to multicast packets */ |
---|
352 | if (IN_MULTICAST(ntohl(icp->icmp_ip.ip_dst.s_addr))) |
---|
353 | goto badcode; |
---|
354 | #ifdef ICMPPRINTFS |
---|
355 | if (icmpprintfs) |
---|
356 | printf("deliver to protocol %d\n", icp->icmp_ip.ip_p); |
---|
357 | #endif |
---|
358 | icmpsrc.sin_addr = icp->icmp_ip.ip_dst; |
---|
359 | #if 1 |
---|
360 | /* |
---|
361 | * MTU discovery: |
---|
362 | * If we got a needfrag and there is a host route to the |
---|
363 | * original destination, and the MTU is not locked, then |
---|
364 | * set the MTU in the route to the suggested new value |
---|
365 | * (if given) and then notify as usual. The ULPs will |
---|
366 | * notice that the MTU has changed and adapt accordingly. |
---|
367 | * If no new MTU was suggested, then we guess a new one |
---|
368 | * less than the current value. If the new MTU is |
---|
369 | * unreasonably small (arbitrarily set at 296), then |
---|
370 | * we reset the MTU to the interface value and enable the |
---|
371 | * lock bit, indicating that we are no longer doing MTU |
---|
372 | * discovery. |
---|
373 | */ |
---|
374 | if (code == PRC_MSGSIZE) { |
---|
375 | struct rtentry *rt; |
---|
376 | int mtu; |
---|
377 | |
---|
378 | rt = rtalloc1((struct sockaddr *)&icmpsrc, 0, |
---|
379 | RTF_CLONING | RTF_PRCLONING); |
---|
380 | if (rt && (rt->rt_flags & RTF_HOST) |
---|
381 | && !(rt->rt_rmx.rmx_locks & RTV_MTU)) { |
---|
382 | mtu = ntohs(icp->icmp_nextmtu); |
---|
383 | if (!mtu) |
---|
384 | mtu = ip_next_mtu(rt->rt_rmx.rmx_mtu, |
---|
385 | 1); |
---|
386 | #ifdef DEBUG_MTUDISC |
---|
387 | printf("MTU for %s reduced to %d\n", |
---|
388 | inet_ntoa(icmpsrc.sin_addr), mtu); |
---|
389 | #endif |
---|
390 | if (mtu < 296) { |
---|
391 | /* rt->rt_rmx.rmx_mtu = |
---|
392 | rt->rt_ifp->if_mtu; */ |
---|
393 | rt->rt_rmx.rmx_locks |= RTV_MTU; |
---|
394 | } else if (rt->rt_rmx.rmx_mtu > mtu) { |
---|
395 | rt->rt_rmx.rmx_mtu = mtu; |
---|
396 | } |
---|
397 | } |
---|
398 | if (rt) |
---|
399 | RTFREE(rt); |
---|
400 | } |
---|
401 | |
---|
402 | #endif |
---|
403 | ctlfunc = inetsw[ip_protox[icp->icmp_ip.ip_p]].pr_ctlinput; |
---|
404 | if (ctlfunc) |
---|
405 | (*ctlfunc)(code, (struct sockaddr *)&icmpsrc, |
---|
406 | (void *)&icp->icmp_ip); |
---|
407 | break; |
---|
408 | |
---|
409 | badcode: |
---|
410 | icmpstat.icps_badcode++; |
---|
411 | break; |
---|
412 | |
---|
413 | case ICMP_ECHO: |
---|
414 | if (!icmpallecho) { |
---|
415 | icmpstat.icps_allecho++; |
---|
416 | break; |
---|
417 | } |
---|
418 | if (!icmpbmcastecho |
---|
419 | && (m->m_flags & (M_MCAST | M_BCAST)) != 0 |
---|
420 | && IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { |
---|
421 | icmpstat.icps_bmcastecho++; |
---|
422 | break; |
---|
423 | } |
---|
424 | icp->icmp_type = ICMP_ECHOREPLY; |
---|
425 | goto reflect; |
---|
426 | |
---|
427 | case ICMP_TSTAMP: |
---|
428 | if (!icmpbmcastecho |
---|
429 | && (m->m_flags & (M_MCAST | M_BCAST)) != 0 |
---|
430 | && IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { |
---|
431 | icmpstat.icps_bmcasttstamp++; |
---|
432 | break; |
---|
433 | } |
---|
434 | if (icmplen < ICMP_TSLEN) { |
---|
435 | icmpstat.icps_badlen++; |
---|
436 | break; |
---|
437 | } |
---|
438 | icp->icmp_type = ICMP_TSTAMPREPLY; |
---|
439 | icp->icmp_rtime = iptime(); |
---|
440 | icp->icmp_ttime = icp->icmp_rtime; /* bogus, do later! */ |
---|
441 | goto reflect; |
---|
442 | |
---|
443 | case ICMP_MASKREQ: |
---|
444 | #define satosin(sa) ((struct sockaddr_in *)(sa)) |
---|
445 | if (icmpmaskrepl == 0) |
---|
446 | break; |
---|
447 | /* |
---|
448 | * We are not able to respond with all ones broadcast |
---|
449 | * unless we receive it over a point-to-point interface. |
---|
450 | */ |
---|
451 | if (icmplen < ICMP_MASKLEN) |
---|
452 | break; |
---|
453 | switch (ip->ip_dst.s_addr) { |
---|
454 | |
---|
455 | case INADDR_BROADCAST: |
---|
456 | case INADDR_ANY: |
---|
457 | icmpdst.sin_addr = ip->ip_src; |
---|
458 | break; |
---|
459 | |
---|
460 | default: |
---|
461 | icmpdst.sin_addr = ip->ip_dst; |
---|
462 | } |
---|
463 | ia = (struct in_ifaddr *)ifaof_ifpforaddr( |
---|
464 | (struct sockaddr *)&icmpdst, m->m_pkthdr.rcvif); |
---|
465 | if (ia == 0) |
---|
466 | break; |
---|
467 | if (ia->ia_ifp == 0) |
---|
468 | break; |
---|
469 | icp->icmp_type = ICMP_MASKREPLY; |
---|
470 | icp->icmp_mask = ia->ia_sockmask.sin_addr.s_addr; |
---|
471 | if (ip->ip_src.s_addr == 0) { |
---|
472 | if (ia->ia_ifp->if_flags & IFF_BROADCAST) |
---|
473 | ip->ip_src = satosin(&ia->ia_broadaddr)->sin_addr; |
---|
474 | else if (ia->ia_ifp->if_flags & IFF_POINTOPOINT) |
---|
475 | ip->ip_src = satosin(&ia->ia_dstaddr)->sin_addr; |
---|
476 | } |
---|
477 | reflect: |
---|
478 | ip->ip_len += hlen; /* since ip_input deducts this */ |
---|
479 | icmpstat.icps_reflect++; |
---|
480 | icmpstat.icps_outhist[icp->icmp_type]++; |
---|
481 | icmp_reflect(m); |
---|
482 | return; |
---|
483 | |
---|
484 | case ICMP_REDIRECT: |
---|
485 | if (code > 3) |
---|
486 | goto badcode; |
---|
487 | if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) || |
---|
488 | #ifdef _IP_VHL |
---|
489 | IP_VHL_HL(icp->icmp_ip.ip_vhl) < (sizeof(struct ip) >> 2)) { |
---|
490 | #else |
---|
491 | icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) { |
---|
492 | #endif |
---|
493 | icmpstat.icps_badlen++; |
---|
494 | break; |
---|
495 | } |
---|
496 | /* |
---|
497 | * Short circuit routing redirects to force |
---|
498 | * immediate change in the kernel's routing |
---|
499 | * tables. The message is also handed to anyone |
---|
500 | * listening on a raw socket (e.g. the routing |
---|
501 | * daemon for use in updating its tables). |
---|
502 | */ |
---|
503 | icmpgw.sin_addr = ip->ip_src; |
---|
504 | icmpdst.sin_addr = icp->icmp_gwaddr; |
---|
505 | #ifdef ICMPPRINTFS |
---|
506 | if (icmpprintfs) { |
---|
507 | char buf[4 * sizeof "123"]; |
---|
508 | strcpy(buf, inet_ntoa(icp->icmp_ip.ip_dst)); |
---|
509 | |
---|
510 | printf("redirect dst %s to %s\n", |
---|
511 | buf, inet_ntoa(icp->icmp_gwaddr)); |
---|
512 | } |
---|
513 | #endif |
---|
514 | icmpsrc.sin_addr = icp->icmp_ip.ip_dst; |
---|
515 | rtredirect((struct sockaddr *)&icmpsrc, |
---|
516 | (struct sockaddr *)&icmpdst, |
---|
517 | (struct sockaddr *)0, RTF_GATEWAY | RTF_HOST, |
---|
518 | (struct sockaddr *)&icmpgw, (struct rtentry **)0); |
---|
519 | pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&icmpsrc); |
---|
520 | break; |
---|
521 | |
---|
522 | /* |
---|
523 | * No kernel processing for the following; |
---|
524 | * just fall through to send to raw listener. |
---|
525 | */ |
---|
526 | case ICMP_ECHOREPLY: |
---|
527 | case ICMP_ROUTERADVERT: |
---|
528 | case ICMP_ROUTERSOLICIT: |
---|
529 | case ICMP_TSTAMPREPLY: |
---|
530 | case ICMP_IREQREPLY: |
---|
531 | case ICMP_MASKREPLY: |
---|
532 | default: |
---|
533 | break; |
---|
534 | } |
---|
535 | |
---|
536 | raw: |
---|
537 | rip_input(m, hlen); |
---|
538 | return; |
---|
539 | |
---|
540 | freeit: |
---|
541 | m_freem(m); |
---|
542 | } |
---|
543 | |
---|
544 | /* |
---|
545 | * Reflect the ip packet back to the source |
---|
546 | */ |
---|
547 | static void |
---|
548 | icmp_reflect(struct mbuf *m) |
---|
549 | { |
---|
550 | struct ip *ip = mtod(m, struct ip *); |
---|
551 | struct in_ifaddr *ia; |
---|
552 | struct in_addr t; |
---|
553 | struct mbuf *opts = 0; |
---|
554 | #ifdef _IP_VHL |
---|
555 | int optlen = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof(struct ip); |
---|
556 | #else |
---|
557 | int optlen = (ip->ip_hl << 2) - sizeof(struct ip); |
---|
558 | #endif |
---|
559 | if (!in_canforward(ip->ip_src) && |
---|
560 | ((ntohl(ip->ip_src.s_addr) & IN_CLASSA_NET) != |
---|
561 | (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))) { |
---|
562 | m_freem(m); /* Bad return address */ |
---|
563 | goto done; /* Ip_output() will check for broadcast */ |
---|
564 | } |
---|
565 | t = ip->ip_dst; |
---|
566 | ip->ip_dst = ip->ip_src; |
---|
567 | /* |
---|
568 | * If the incoming packet was addressed directly to us, |
---|
569 | * use dst as the src for the reply. Otherwise (broadcast |
---|
570 | * or anonymous), use the address which corresponds |
---|
571 | * to the incoming interface. |
---|
572 | */ |
---|
573 | for (ia = in_ifaddr; ia; ia = ia->ia_next) { |
---|
574 | if (t.s_addr == IA_SIN(ia)->sin_addr.s_addr) |
---|
575 | break; |
---|
576 | if (ia->ia_ifp && (ia->ia_ifp->if_flags & IFF_BROADCAST) && |
---|
577 | t.s_addr == satosin(&ia->ia_broadaddr)->sin_addr.s_addr) |
---|
578 | break; |
---|
579 | } |
---|
580 | icmpdst.sin_addr = t; |
---|
581 | if ((ia == (struct in_ifaddr *)0) && m->m_pkthdr.rcvif) |
---|
582 | ia = (struct in_ifaddr *)ifaof_ifpforaddr( |
---|
583 | (struct sockaddr *)&icmpdst, m->m_pkthdr.rcvif); |
---|
584 | /* |
---|
585 | * The following happens if the packet was not addressed to us, |
---|
586 | * and was received on an interface with no IP address. |
---|
587 | */ |
---|
588 | if (ia == (struct in_ifaddr *)0) |
---|
589 | ia = in_ifaddr; |
---|
590 | t = IA_SIN(ia)->sin_addr; |
---|
591 | ip->ip_src = t; |
---|
592 | ip->ip_ttl = MAXTTL; |
---|
593 | |
---|
594 | if (optlen > 0) { |
---|
595 | register u_char *cp; |
---|
596 | int opt, cnt; |
---|
597 | u_int len; |
---|
598 | |
---|
599 | /* |
---|
600 | * Retrieve any source routing from the incoming packet; |
---|
601 | * add on any record-route or timestamp options. |
---|
602 | */ |
---|
603 | cp = (u_char *) (ip + 1); |
---|
604 | if ((opts = ip_srcroute()) == 0 && |
---|
605 | (opts = m_gethdr(M_DONTWAIT, MT_HEADER))) { |
---|
606 | opts->m_len = sizeof(struct in_addr); |
---|
607 | mtod(opts, struct in_addr *)->s_addr = 0; |
---|
608 | } |
---|
609 | if (opts) { |
---|
610 | #ifdef ICMPPRINTFS |
---|
611 | if (icmpprintfs) |
---|
612 | printf("icmp_reflect optlen %d rt %d => ", |
---|
613 | optlen, opts->m_len); |
---|
614 | #endif |
---|
615 | for (cnt = optlen; cnt > 0; cnt -= len, cp += len) { |
---|
616 | opt = cp[IPOPT_OPTVAL]; |
---|
617 | if (opt == IPOPT_EOL) |
---|
618 | break; |
---|
619 | if (opt == IPOPT_NOP) |
---|
620 | len = 1; |
---|
621 | else { |
---|
622 | if (cnt < IPOPT_OLEN + sizeof(*cp)) |
---|
623 | break; |
---|
624 | len = cp[IPOPT_OLEN]; |
---|
625 | if (len < IPOPT_OLEN + sizeof(*cp) || |
---|
626 | len > cnt) |
---|
627 | break; |
---|
628 | } |
---|
629 | /* |
---|
630 | * Should check for overflow, but it "can't happen" |
---|
631 | */ |
---|
632 | if (opt == IPOPT_RR || opt == IPOPT_TS || |
---|
633 | opt == IPOPT_SECURITY) { |
---|
634 | bcopy((caddr_t)cp, |
---|
635 | mtod(opts, caddr_t) + opts->m_len, len); |
---|
636 | opts->m_len += len; |
---|
637 | } |
---|
638 | } |
---|
639 | /* Terminate & pad, if necessary */ |
---|
640 | cnt = opts->m_len % 4; |
---|
641 | if (cnt) { |
---|
642 | for (; cnt < 4; cnt++) { |
---|
643 | *(mtod(opts, caddr_t) + opts->m_len) = |
---|
644 | IPOPT_EOL; |
---|
645 | opts->m_len++; |
---|
646 | } |
---|
647 | } |
---|
648 | #ifdef ICMPPRINTFS |
---|
649 | if (icmpprintfs) |
---|
650 | printf("%d\n", opts->m_len); |
---|
651 | #endif |
---|
652 | } |
---|
653 | /* |
---|
654 | * Now strip out original options by copying rest of first |
---|
655 | * mbuf's data back, and adjust the IP length. |
---|
656 | */ |
---|
657 | ip->ip_len -= optlen; |
---|
658 | #ifdef _IP_VHL |
---|
659 | ip->ip_vhl = IP_VHL_BORING; |
---|
660 | #else |
---|
661 | ip->ip_v = IPVERSION; |
---|
662 | ip->ip_hl = 5; |
---|
663 | #endif |
---|
664 | m->m_len -= optlen; |
---|
665 | if (m->m_flags & M_PKTHDR) |
---|
666 | m->m_pkthdr.len -= optlen; |
---|
667 | optlen += sizeof(struct ip); |
---|
668 | bcopy((caddr_t)ip + optlen, (caddr_t)(ip + 1), |
---|
669 | (unsigned)(m->m_len - sizeof(struct ip))); |
---|
670 | } |
---|
671 | m->m_flags &= ~(M_BCAST|M_MCAST); |
---|
672 | icmp_send(m, opts); |
---|
673 | done: |
---|
674 | if (opts) |
---|
675 | (void)m_free(opts); |
---|
676 | } |
---|
677 | |
---|
678 | /* |
---|
679 | * Send an icmp packet back to the ip level, |
---|
680 | * after supplying a checksum. |
---|
681 | */ |
---|
682 | static void |
---|
683 | icmp_send(struct mbuf *m, struct mbuf *opts) |
---|
684 | { |
---|
685 | register struct ip *ip = mtod(m, struct ip *); |
---|
686 | register int hlen; |
---|
687 | register struct icmp *icp; |
---|
688 | struct route ro; |
---|
689 | |
---|
690 | #ifdef _IP_VHL |
---|
691 | hlen = IP_VHL_HL(ip->ip_vhl) << 2; |
---|
692 | #else |
---|
693 | hlen = ip->ip_hl << 2; |
---|
694 | #endif |
---|
695 | m->m_data += hlen; |
---|
696 | m->m_len -= hlen; |
---|
697 | icp = mtod(m, struct icmp *); |
---|
698 | icp->icmp_cksum = 0; |
---|
699 | icp->icmp_cksum = in_cksum(m, ip->ip_len - hlen); |
---|
700 | m->m_data -= hlen; |
---|
701 | m->m_len += hlen; |
---|
702 | #ifdef ICMPPRINTFS |
---|
703 | if (icmpprintfs) { |
---|
704 | char buf[4 * sizeof "123"]; |
---|
705 | strcpy(buf, inet_ntoa(ip->ip_dst)); |
---|
706 | printf("icmp_send dst %s src %s\n", |
---|
707 | buf, inet_ntoa(ip->ip_src)); |
---|
708 | } |
---|
709 | #endif |
---|
710 | bzero(&ro, sizeof ro); |
---|
711 | (void) ip_output(m, opts, &ro, 0, NULL); |
---|
712 | if (ro.ro_rt) |
---|
713 | RTFREE(ro.ro_rt); |
---|
714 | } |
---|
715 | |
---|
716 | n_time |
---|
717 | iptime(void) |
---|
718 | { |
---|
719 | struct timeval atv; |
---|
720 | u_long t; |
---|
721 | |
---|
722 | microtime(&atv); |
---|
723 | t = (atv.tv_sec % (24L*60L*60L)) * 1000L + atv.tv_usec / 1000L; |
---|
724 | return (htonl(t)); |
---|
725 | } |
---|
726 | |
---|
727 | /* |
---|
728 | * Return the next larger or smaller MTU plateau (table from RFC 1191) |
---|
729 | * given current value MTU. If DIR is less than zero, a larger plateau |
---|
730 | * is returned; otherwise, a smaller value is returned. |
---|
731 | */ |
---|
732 | int |
---|
733 | ip_next_mtu(int mtu, int dir) |
---|
734 | { |
---|
735 | static int mtutab[] = { |
---|
736 | 65535, 32000, 17914, 8166, 4352, 2002, 1492, 1280, 1006, 508, |
---|
737 | 296, 68, 0 |
---|
738 | }; |
---|
739 | int i; |
---|
740 | |
---|
741 | for (i = 0; i < (sizeof mtutab) / (sizeof mtutab[0]); i++) { |
---|
742 | if (mtu >= mtutab[i]) |
---|
743 | break; |
---|
744 | } |
---|
745 | |
---|
746 | if (dir < 0) { |
---|
747 | if (i == 0) { |
---|
748 | return 0; |
---|
749 | } else { |
---|
750 | return mtutab[i - 1]; |
---|
751 | } |
---|
752 | } else { |
---|
753 | if (mtutab[i] == 0) { |
---|
754 | return 0; |
---|
755 | } else if(mtu > mtutab[i]) { |
---|
756 | return mtutab[i]; |
---|
757 | } else { |
---|
758 | return mtutab[i + 1]; |
---|
759 | } |
---|
760 | } |
---|
761 | } |
---|