2 * Copyright (C) 1993 AmiTCP/IP Group, <amitcp-group@hut.fi>
3 * Helsinki University of Technology, Finland.
5 * Copyright (C) 2005 Neil Cafferkey
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
24 * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
25 * All rights reserved.
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
35 * 3. All advertising materials mentioning features or use of this software
36 * must display the following acknowledgement:
37 * This product includes software developed by the University of
38 * California, Berkeley and its contributors.
39 * 4. Neither the name of the University nor the names of its contributors
40 * may be used to endorse or promote products derived from this software
41 * without specific prior written permission.
43 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * @(#)ip_icmp.c 7.15 (Berkeley) 4/20/91
60 #include <sys/param.h>
61 #include <sys/systm.h>
62 #include <sys/malloc.h>
64 #include <sys/protosw.h>
65 #include <sys/socket.h>
66 /*#include <sys/time.h>*/
67 #include <sys/kernel.h>
69 #include <net/route.h>
72 #include <netinet/in.h>
73 #include <netinet/in_systm.h>
74 #include <netinet/in_var.h>
75 #include <netinet/ip.h>
76 #include <netinet/ip_icmp.h>
77 #include <netinet/icmp_var.h>
79 #include <netinet/ip_icmp_protos.h>
80 #include <netinet/ip_input_protos.h>
81 #include <netinet/ip_output_protos.h>
82 #include <netinet/in_cksum_protos.h>
83 #include <netinet/in_protos.h>
84 #include <kern/uipc_domain_protos.h>
85 #include <net/raw_usrreq_protos.h>
87 struct icmpstat icmpstat
= { 0 };
90 * ICMP routines: error generation, receive packet processing, and
91 * routines to turnaround packets back to the originator, and
92 * host table maintenance routines.
94 int icmpprintfs
= 0; /* has effect only if ICMPPRINTFS is defined */
96 extern struct protosw inetsw
[];
99 * Generate an error packet of type error
100 * in response to bad packet ip.
104 icmp_error(n
, type
, code
, dest
)
109 register struct ip
*oip
= mtod(n
, struct ip
*), *nip
;
110 register unsigned oiplen
= oip
->ip_hl
<< 2;
111 register struct icmp
*icp
;
112 register struct mbuf
*m
;
117 printf("icmp_error(%lx, %ld, %ld)\n", (u_long
)oip
, type
, code
);
119 if (type
!= ICMP_REDIRECT
)
120 icmpstat
.icps_error
++;
122 * Don't send error if not the first fragment of message.
123 * Don't error if the old packet protocol was ICMP
124 * error message, only known informational types.
126 if (oip
->ip_off
&~ (IP_MF
|IP_DF
))
128 if (oip
->ip_p
== IPPROTO_ICMP
&& type
!= ICMP_REDIRECT
&&
129 n
->m_len
>= oiplen
+ ICMP_MINLEN
&&
130 !ICMP_INFOTYPE(((struct icmp
*)((caddr_t
)oip
+ oiplen
))->icmp_type
)) {
131 icmpstat
.icps_oldicmp
++;
136 * First, formulate icmp message
138 m
= m_gethdr(M_DONTWAIT
, MT_HEADER
);
141 icmplen
= oiplen
+ MIN(8, oip
->ip_len
);
142 m
->m_len
= icmplen
+ ICMP_MINLEN
;
143 MH_ALIGN(m
, m
->m_len
);
144 icp
= mtod(m
, struct icmp
*);
145 if ((u_int
)type
> ICMP_MAXTYPE
)
147 icmpstat
.icps_outhist
[type
]++;
148 icp
->icmp_type
= type
;
149 if (type
== ICMP_REDIRECT
)
150 icp
->icmp_gwaddr
= dest
;
153 if (type
== ICMP_PARAMPROB
) {
154 icp
->icmp_pptr
= code
;
157 icp
->icmp_code
= code
;
158 aligned_bcopy((caddr_t
)oip
, (caddr_t
)&icp
->icmp_ip
, icmplen
);
160 nip
->ip_len
= htons((u_short
)(nip
->ip_len
+ oiplen
));
163 * Now, copy old ip header (without options)
164 * in front of icmp message.
166 if (m
->m_data
- sizeof(struct ip
) < m
->m_pktdat
)
168 m
->m_data
-= sizeof(struct ip
);
169 m
->m_len
+= sizeof(struct ip
);
170 m
->m_pkthdr
.len
= m
->m_len
;
171 m
->m_pkthdr
.rcvif
= n
->m_pkthdr
.rcvif
;
172 nip
= mtod(m
, struct ip
*);
173 aligned_bcopy((caddr_t
)oip
, (caddr_t
)nip
, oiplen
);
174 nip
->ip_len
= m
->m_len
;
175 nip
->ip_hl
= sizeof(struct ip
) >> 2;
176 nip
->ip_p
= IPPROTO_ICMP
;
183 static struct sockproto icmproto
= { AF_INET
, IPPROTO_ICMP
};
184 static struct sockaddr_in icmpsrc
= { sizeof (struct sockaddr_in
), AF_INET
};
185 static struct sockaddr_in icmpdst
= { sizeof (struct sockaddr_in
), AF_INET
};
186 static struct sockaddr_in icmpgw
= { sizeof (struct sockaddr_in
), AF_INET
};
187 struct sockaddr_in icmpmask
= { 8, 0 };
188 struct in_ifaddr
*ifptoia();
191 * Process a received ICMP message.
193 void icmp_input(void *arg
, ...)
195 register struct mbuf
*m
= arg
;
196 register struct icmp
*icp
;
197 register struct ip
*ip
= mtod(m
, struct ip
*);
198 int icmplen
= ip
->ip_len
;
200 struct in_ifaddr
*ia
;
202 extern u_char ip_protox
[];
203 extern struct in_addr
in_makeaddr();
209 hlen
= va_arg(va
, int);
212 * Locate icmp structure in mbuf, and check
213 * that not corrupted and of at least minimum length.
217 printf("icmp_input from %lx, len %ld\n", ip
->ip_src
.s_addr
, icmplen
);
219 if (icmplen
< ICMP_MINLEN
) {
220 icmpstat
.icps_tooshort
++;
223 i
= hlen
+ MIN(icmplen
, ICMP_ADVLENMIN
);
224 if (m
->m_len
< i
&& (m
= m_pullup(m
, i
)) == 0) {
225 icmpstat
.icps_tooshort
++;
228 ip
= mtod(m
, struct ip
*);
231 icp
= mtod(m
, struct icmp
*);
232 if (in_cksum(m
, icmplen
)) {
233 icmpstat
.icps_checksum
++;
241 * Message type specific processing.
244 printf("icmp_input, type %ld code %ld\n", icp
->icmp_type
,
247 if (icp
->icmp_type
> ICMP_MAXTYPE
)
249 icmpstat
.icps_inhist
[icp
->icmp_type
]++;
250 code
= icp
->icmp_code
;
251 switch (icp
->icmp_type
) {
256 code
+= PRC_UNREACH_NET
;
262 code
+= PRC_TIMXCEED_INTRANS
;
268 code
= PRC_PARAMPROB
;
271 case ICMP_SOURCEQUENCH
:
277 * Problem with datagram; advise higher level routines.
279 if (icmplen
< ICMP_ADVLENMIN
|| icmplen
< ICMP_ADVLEN(icp
) ||
280 icp
->icmp_ip
.ip_hl
< (sizeof(struct ip
) >> 2)) {
281 icmpstat
.icps_badlen
++;
284 NTOHS(icp
->icmp_ip
.ip_len
);
287 printf("deliver to protocol %ld\n", icp
->icmp_ip
.ip_p
);
289 icmpsrc
.sin_addr
= icp
->icmp_ip
.ip_dst
;
290 if (inetsw
[ip_protox
[icp
->icmp_ip
.ip_p
]].pr_ctlinput
)
291 (*inetsw
[ip_protox
[icp
->icmp_ip
.ip_p
]].pr_ctlinput
)
292 (code
, (struct sockaddr
*)&icmpsrc
,
293 (caddr_t
) &icp
->icmp_ip
);
297 icmpstat
.icps_badcode
++;
301 icp
->icmp_type
= ICMP_ECHOREPLY
;
305 if (icmplen
< ICMP_TSLEN
) {
306 icmpstat
.icps_badlen
++;
309 icp
->icmp_type
= ICMP_TSTAMPREPLY
;
310 icp
->icmp_rtime
= iptime();
311 icp
->icmp_ttime
= icp
->icmp_rtime
; /* bogus, do later! */
315 #define satosin(sa) ((struct sockaddr_in *)(sa))
316 if (in_netof(ip
->ip_src
) == 0 &&
317 (ia
= ifptoia(m
->m_pkthdr
.rcvif
)))
318 ip
->ip_src
= in_makeaddr(in_netof(IA_SIN(ia
)->sin_addr
),
319 in_lnaof(ip
->ip_src
));
320 icp
->icmp_type
= ICMP_IREQREPLY
;
324 if (icmplen
< ICMP_MASKLEN
||
325 (ia
= ifptoia(m
->m_pkthdr
.rcvif
)) == 0)
327 icp
->icmp_type
= ICMP_MASKREPLY
;
328 icp
->icmp_mask
= ia
->ia_sockmask
.sin_addr
.s_addr
;
329 if (ip
->ip_src
.s_addr
== 0) {
330 if (ia
->ia_ifp
->if_flags
& IFF_BROADCAST
)
331 ip
->ip_src
= satosin(&ia
->ia_broadaddr
)->sin_addr
;
332 else if (ia
->ia_ifp
->if_flags
& IFF_POINTOPOINT
)
333 ip
->ip_src
= satosin(&ia
->ia_dstaddr
)->sin_addr
;
336 ip
->ip_len
+= hlen
; /* since ip_input deducts this */
337 icmpstat
.icps_reflect
++;
338 icmpstat
.icps_outhist
[icp
->icmp_type
]++;
343 if (icmplen
< ICMP_ADVLENMIN
|| icmplen
< ICMP_ADVLEN(icp
)) {
344 icmpstat
.icps_badlen
++;
348 * Short circuit routing redirects to force
349 * immediate change in the kernel's routing
350 * tables. The message is also handed to anyone
351 * listening on a raw socket (e.g. the routing
352 * daemon for use in updating its tables).
354 icmpgw
.sin_addr
= ip
->ip_src
;
355 icmpdst
.sin_addr
= icp
->icmp_gwaddr
;
358 printf("redirect dst %lx to %lx\n", icp
->icmp_ip
.ip_dst
.s_addr
,
359 icp
->icmp_gwaddr
.s_addr
);
361 if (code
== ICMP_REDIRECT_NET
|| code
== ICMP_REDIRECT_TOSNET
) {
364 in_makeaddr(in_netof(icp
->icmp_ip
.ip_dst
), INADDR_ANY
);
365 in_sockmaskof(icp
->icmp_ip
.ip_dst
, &icmpmask
);
366 rtredirect((struct sockaddr
*)&icmpsrc
,
367 (struct sockaddr
*)&icmpdst
,
368 (struct sockaddr
*)&icmpmask
, RTF_GATEWAY
,
369 (struct sockaddr
*)&icmpgw
, (struct rtentry
**)0);
370 icmpsrc
.sin_addr
= icp
->icmp_ip
.ip_dst
;
371 pfctlinput(PRC_REDIRECT_NET
,
372 (struct sockaddr
*)&icmpsrc
);
374 icmpsrc
.sin_addr
= icp
->icmp_ip
.ip_dst
;
375 rtredirect((struct sockaddr
*)&icmpsrc
,
376 (struct sockaddr
*)&icmpdst
,
377 (struct sockaddr
*)0, RTF_GATEWAY
| RTF_HOST
,
378 (struct sockaddr
*)&icmpgw
, (struct rtentry
**)0);
379 pfctlinput(PRC_REDIRECT_HOST
,
380 (struct sockaddr
*)&icmpsrc
);
385 * No kernel processing for the following;
386 * just fall through to send to raw listener.
389 case ICMP_TSTAMPREPLY
:
397 icmpsrc
.sin_addr
= ip
->ip_src
;
398 icmpdst
.sin_addr
= ip
->ip_dst
;
399 (void) raw_input(m
, &icmproto
, (struct sockaddr
*)&icmpsrc
,
400 (struct sockaddr
*)&icmpdst
);
408 * Reflect the ip packet back to the source
414 register struct ip
*ip
= mtod(m
, struct ip
*);
415 register struct in_ifaddr
*ia
;
417 struct mbuf
*opts
= 0;
418 int optlen
= (ip
->ip_hl
<< 2) - sizeof(struct ip
);
421 ip
->ip_dst
= ip
->ip_src
;
423 * If the incoming packet was addressed directly to us,
424 * use dst as the src for the reply. Otherwise (broadcast
425 * or anonymous), use the address which corresponds
426 * to the incoming interface.
428 for (ia
= in_ifaddr
; ia
; ia
= ia
->ia_next
) {
429 if (t
.s_addr
== IA_SIN(ia
)->sin_addr
.s_addr
)
431 if ((ia
->ia_ifp
->if_flags
& IFF_BROADCAST
) &&
432 t
.s_addr
== satosin(&ia
->ia_broadaddr
)->sin_addr
.s_addr
)
435 if (ia
== (struct in_ifaddr
*)0)
436 ia
= ifptoia(m
->m_pkthdr
.rcvif
);
437 if (ia
== (struct in_ifaddr
*)0)
439 t
= IA_SIN(ia
)->sin_addr
;
449 * Retrieve any source routing from the incoming packet;
450 * add on any record-route or timestamp options.
452 cp
= (u_char
*) (ip
+ 1);
453 if ((opts
= ip_srcroute()) == 0 &&
454 (opts
= m_gethdr(M_DONTWAIT
, MT_HEADER
))) {
455 opts
->m_len
= sizeof(struct in_addr
);
456 mtod(opts
, struct in_addr
*)->s_addr
= 0;
461 printf("icmp_reflect optlen %ld rt %ld => ",
462 optlen
, opts
->m_len
);
464 for (cnt
= optlen
; cnt
> 0; cnt
-= len
, cp
+= len
) {
465 opt
= cp
[IPOPT_OPTVAL
];
466 if (opt
== IPOPT_EOL
)
468 if (opt
== IPOPT_NOP
)
471 len
= cp
[IPOPT_OLEN
];
472 if (len
<= 0 || len
> cnt
)
476 * should check for overflow, but it "can't happen"
478 if (opt
== IPOPT_RR
|| opt
== IPOPT_TS
) {
480 mtod(opts
, caddr_t
) + opts
->m_len
, len
);
484 if (opts
->m_len
% 4 != 0) {
485 *(mtod(opts
, caddr_t
) + opts
->m_len
) = IPOPT_EOL
;
490 printf("%ld\n", opts
->m_len
);
494 * Now strip out original options by copying rest of first
495 * mbuf's data back, and adjust the IP length.
497 ip
->ip_len
-= optlen
;
498 ip
->ip_hl
= sizeof(struct ip
) >> 2;
500 if (m
->m_flags
& M_PKTHDR
)
501 m
->m_pkthdr
.len
-= optlen
;
502 optlen
+= sizeof(struct ip
);
503 bcopy((caddr_t
)ip
+ optlen
, (caddr_t
)(ip
+ 1),
504 (unsigned)(m
->m_len
- sizeof(struct ip
)));
515 register struct in_ifaddr
*ia
;
517 for (ia
= in_ifaddr
; ia
; ia
= ia
->ia_next
)
518 if (ia
->ia_ifp
== ifp
)
520 return ((struct in_ifaddr
*)0);
524 * Send an icmp packet back to the ip level,
525 * after supplying a checksum.
529 register struct mbuf
*m
;
532 register struct ip
*ip
= mtod(m
, struct ip
*);
534 register struct icmp
*icp
;
536 hlen
= ip
->ip_hl
<< 2;
539 icp
= mtod(m
, struct icmp
*);
541 icp
->icmp_cksum
= in_cksum(m
, ip
->ip_len
- hlen
);
546 printf("icmp_send dst %lx src %lx\n", ip
->ip_dst
.s_addr
, ip
->ip_src
.s_addr
);
548 (void) ip_output(m
, opts
, (struct route
*)0, 0);
558 t
= (atv
.tv_sec
% (24*60*60)) * 1000 + atv
.tv_usec
/ 1000;