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