xref: /freebsd/sys/netinet/ip_icmp.c (revision 6b773dff30ea223c1ece875d88921ddc8876806e)
1c398230bSWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1988, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
14df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
15df8bae1dSRodney W. Grimes  *    without specific prior written permission.
16df8bae1dSRodney W. Grimes  *
17df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
28df8bae1dSRodney W. Grimes  *
29df8bae1dSRodney W. Grimes  *	@(#)ip_icmp.c	8.2 (Berkeley) 1/4/94
30c3aac50fSPeter Wemm  * $FreeBSD$
31df8bae1dSRodney W. Grimes  */
32df8bae1dSRodney W. Grimes 
336a800098SYoshinobu Inoue #include "opt_ipsec.h"
340070e096SRobert Watson #include "opt_mac.h"
356a800098SYoshinobu Inoue 
36df8bae1dSRodney W. Grimes #include <sys/param.h>
37df8bae1dSRodney W. Grimes #include <sys/systm.h>
380070e096SRobert Watson #include <sys/mac.h>
39df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
40df8bae1dSRodney W. Grimes #include <sys/protosw.h>
41df8bae1dSRodney W. Grimes #include <sys/socket.h>
42df8bae1dSRodney W. Grimes #include <sys/time.h>
43df8bae1dSRodney W. Grimes #include <sys/kernel.h>
44b5e8ce9fSBruce Evans #include <sys/sysctl.h>
45df8bae1dSRodney W. Grimes 
46df8bae1dSRodney W. Grimes #include <net/if.h>
479494d596SBrooks Davis #include <net/if_types.h>
48df8bae1dSRodney W. Grimes #include <net/route.h>
49df8bae1dSRodney W. Grimes 
50df8bae1dSRodney W. Grimes #include <netinet/in.h>
5197d8d152SAndre Oppermann #include <netinet/in_pcb.h>
52df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
53df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
54df8bae1dSRodney W. Grimes #include <netinet/ip.h>
55df8bae1dSRodney W. Grimes #include <netinet/ip_icmp.h>
56b5e8ce9fSBruce Evans #include <netinet/ip_var.h>
5797d8d152SAndre Oppermann #include <netinet/tcp.h>
5897d8d152SAndre Oppermann #include <netinet/tcp_var.h>
5997d8d152SAndre Oppermann #include <netinet/tcpip.h>
60df8bae1dSRodney W. Grimes #include <netinet/icmp_var.h>
61df8bae1dSRodney W. Grimes 
626a800098SYoshinobu Inoue #ifdef IPSEC
636a800098SYoshinobu Inoue #include <netinet6/ipsec.h>
646a800098SYoshinobu Inoue #include <netkey/key.h>
656a800098SYoshinobu Inoue #endif
666a800098SYoshinobu Inoue 
67b9234fafSSam Leffler #ifdef FAST_IPSEC
68b9234fafSSam Leffler #include <netipsec/ipsec.h>
69b9234fafSSam Leffler #include <netipsec/key.h>
70b9234fafSSam Leffler #define	IPSEC
71b9234fafSSam Leffler #endif
72b9234fafSSam Leffler 
7372a52a35SJonathan Lemon #include <machine/in_cksum.h>
7472a52a35SJonathan Lemon 
75df8bae1dSRodney W. Grimes /*
76df8bae1dSRodney W. Grimes  * ICMP routines: error generation, receive packet processing, and
77df8bae1dSRodney W. Grimes  * routines to turnaround packets back to the originator, and
78df8bae1dSRodney W. Grimes  * host table maintenance routines.
79df8bae1dSRodney W. Grimes  */
80df8bae1dSRodney W. Grimes 
81cc5934f5SMax Laier struct	icmpstat icmpstat;
82c73d99b5SRuslan Ermilov SYSCTL_STRUCT(_net_inet_icmp, ICMPCTL_STATS, stats, CTLFLAG_RW,
830312fbe9SPoul-Henning Kamp 	&icmpstat, icmpstat, "");
840312fbe9SPoul-Henning Kamp 
850312fbe9SPoul-Henning Kamp static int	icmpmaskrepl = 0;
860312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_icmp, ICMPCTL_MASKREPL, maskrepl, CTLFLAG_RW,
8757842a38SMatthew N. Dodd 	&icmpmaskrepl, 0, "Reply to ICMP Address Mask Request packets.");
8857842a38SMatthew N. Dodd 
8957842a38SMatthew N. Dodd static u_int	icmpmaskfake = 0;
9057842a38SMatthew N. Dodd SYSCTL_UINT(_net_inet_icmp, OID_AUTO, maskfake, CTLFLAG_RW,
9157842a38SMatthew N. Dodd 	&icmpmaskfake, 0, "Fake reply to ICMP Address Mask Request packets.");
920312fbe9SPoul-Henning Kamp 
9318d3153eSDag-Erling Smørgrav static int	drop_redirect = 0;
9418d3153eSDag-Erling Smørgrav SYSCTL_INT(_net_inet_icmp, OID_AUTO, drop_redirect, CTLFLAG_RW,
9518d3153eSDag-Erling Smørgrav 	&drop_redirect, 0, "");
9618d3153eSDag-Erling Smørgrav 
976c3b5f69SDag-Erling Smørgrav static int	log_redirect = 0;
986c3b5f69SDag-Erling Smørgrav SYSCTL_INT(_net_inet_icmp, OID_AUTO, log_redirect, CTLFLAG_RW,
996c3b5f69SDag-Erling Smørgrav 	&log_redirect, 0, "");
1006c3b5f69SDag-Erling Smørgrav 
101173c0f9fSWarner Losh static int      icmplim = 200;
10251508de1SMatthew Dillon SYSCTL_INT(_net_inet_icmp, ICMPCTL_ICMPLIM, icmplim, CTLFLAG_RW,
10351508de1SMatthew Dillon 	&icmplim, 0, "");
1045fce7fc4SMatthew Dillon 
1054f14ee00SDan Moschuk static int	icmplim_output = 1;
1064f14ee00SDan Moschuk SYSCTL_INT(_net_inet_icmp, OID_AUTO, icmplim_output, CTLFLAG_RW,
1074f14ee00SDan Moschuk 	&icmplim_output, 0, "");
10851508de1SMatthew Dillon 
109d0946241SMaxim Konovalov static char	reply_src[IFNAMSIZ];
110b74d89bbSAndre Oppermann SYSCTL_STRING(_net_inet_icmp, OID_AUTO, reply_src, CTLFLAG_RW,
111b74d89bbSAndre Oppermann 	&reply_src, IFNAMSIZ, "icmp reply source for non-local packets.");
112b74d89bbSAndre Oppermann 
113a0866c8dSAndre Oppermann static int	icmp_rfi = 0;
114a0866c8dSAndre Oppermann SYSCTL_INT(_net_inet_icmp, OID_AUTO, reply_from_interface, CTLFLAG_RW,
115a0866c8dSAndre Oppermann 	&icmp_rfi, 0, "ICMP reply from incoming interface for "
116a0866c8dSAndre Oppermann 	"non-local packets");
117a0866c8dSAndre Oppermann 
118e875dfb8SAndre Oppermann static int	icmp_quotelen = 8;
119e875dfb8SAndre Oppermann SYSCTL_INT(_net_inet_icmp, OID_AUTO, quotelen, CTLFLAG_RW,
120e875dfb8SAndre Oppermann 	&icmp_quotelen, 0, "Number of bytes from original packet to "
121e875dfb8SAndre Oppermann 	"quote in ICMP reply");
122e875dfb8SAndre Oppermann 
1235fce7fc4SMatthew Dillon /*
1245fce7fc4SMatthew Dillon  * ICMP broadcast echo sysctl
1255fce7fc4SMatthew Dillon  */
1265fce7fc4SMatthew Dillon 
12761a4defdSJoseph Koshy static int	icmpbmcastecho = 0;
12818d3153eSDag-Erling Smørgrav SYSCTL_INT(_net_inet_icmp, OID_AUTO, bmcastecho, CTLFLAG_RW,
12918d3153eSDag-Erling Smørgrav 	&icmpbmcastecho, 0, "");
1307022ea0aSGarrett Wollman 
13151508de1SMatthew Dillon 
132df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS
133df8bae1dSRodney W. Grimes int	icmpprintfs = 0;
134df8bae1dSRodney W. Grimes #endif
135df8bae1dSRodney W. Grimes 
1364d77a549SAlfred Perlstein static void	icmp_reflect(struct mbuf *);
13702c1c707SAndre Oppermann static void	icmp_send(struct mbuf *, struct mbuf *);
1380312fbe9SPoul-Henning Kamp 
139df8bae1dSRodney W. Grimes extern	struct protosw inetsw[];
140df8bae1dSRodney W. Grimes 
141df8bae1dSRodney W. Grimes /*
142df8bae1dSRodney W. Grimes  * Generate an error packet of type error
143df8bae1dSRodney W. Grimes  * in response to bad packet ip.
144df8bae1dSRodney W. Grimes  */
145df8bae1dSRodney W. Grimes void
146c773494eSAndre Oppermann icmp_error(n, type, code, dest, mtu)
147df8bae1dSRodney W. Grimes 	struct mbuf *n;
148df8bae1dSRodney W. Grimes 	int type, code;
149df8bae1dSRodney W. Grimes 	n_long dest;
150c773494eSAndre Oppermann 	int mtu;
151df8bae1dSRodney W. Grimes {
152df8bae1dSRodney W. Grimes 	register struct ip *oip = mtod(n, struct ip *), *nip;
15353be11f6SPoul-Henning Kamp 	register unsigned oiplen = oip->ip_hl << 2;
154df8bae1dSRodney W. Grimes 	register struct icmp *icp;
155df8bae1dSRodney W. Grimes 	register struct mbuf *m;
156df8bae1dSRodney W. Grimes 	unsigned icmplen;
157df8bae1dSRodney W. Grimes 
158df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS
159df8bae1dSRodney W. Grimes 	if (icmpprintfs)
160623ae52eSPoul-Henning Kamp 		printf("icmp_error(%p, %x, %d)\n", oip, type, code);
161df8bae1dSRodney W. Grimes #endif
162df8bae1dSRodney W. Grimes 	if (type != ICMP_REDIRECT)
163df8bae1dSRodney W. Grimes 		icmpstat.icps_error++;
164df8bae1dSRodney W. Grimes 	/*
165cad1917dSHajimu UMEMOTO 	 * Don't send error if the original packet was encrypted.
166df8bae1dSRodney W. Grimes 	 * Don't send error if not the first fragment of message.
167df8bae1dSRodney W. Grimes 	 * Don't error if the old packet protocol was ICMP
168df8bae1dSRodney W. Grimes 	 * error message, only known informational types.
169df8bae1dSRodney W. Grimes 	 */
170cad1917dSHajimu UMEMOTO 	if (n->m_flags & M_DECRYPTED)
171cad1917dSHajimu UMEMOTO 		goto freeit;
172df8bae1dSRodney W. Grimes 	if (oip->ip_off &~ (IP_MF|IP_DF))
173df8bae1dSRodney W. Grimes 		goto freeit;
174df8bae1dSRodney W. Grimes 	if (oip->ip_p == IPPROTO_ICMP && type != ICMP_REDIRECT &&
175df8bae1dSRodney W. Grimes 	  n->m_len >= oiplen + ICMP_MINLEN &&
176df8bae1dSRodney W. Grimes 	  !ICMP_INFOTYPE(((struct icmp *)((caddr_t)oip + oiplen))->icmp_type)) {
177df8bae1dSRodney W. Grimes 		icmpstat.icps_oldicmp++;
178df8bae1dSRodney W. Grimes 		goto freeit;
179df8bae1dSRodney W. Grimes 	}
180df8bae1dSRodney W. Grimes 	/* Don't send error in response to a multicast or broadcast packet */
181df8bae1dSRodney W. Grimes 	if (n->m_flags & (M_BCAST|M_MCAST))
182df8bae1dSRodney W. Grimes 		goto freeit;
183df8bae1dSRodney W. Grimes 	/*
184df8bae1dSRodney W. Grimes 	 * First, formulate icmp message
185df8bae1dSRodney W. Grimes 	 */
1866b773dffSAndre Oppermann 	MGETHDR(m, M_DONTWAIT, MT_HEADER);
187df8bae1dSRodney W. Grimes 	if (m == NULL)
188df8bae1dSRodney W. Grimes 		goto freeit;
1890070e096SRobert Watson #ifdef MAC
1900070e096SRobert Watson 	mac_create_mbuf_netlayer(n, m);
1910070e096SRobert Watson #endif
192e875dfb8SAndre Oppermann 	/*
193e875dfb8SAndre Oppermann 	 * Calculate length to quote from original packet and
194e875dfb8SAndre Oppermann 	 * prevent the ICMP mbuf from overflowing.
195e875dfb8SAndre Oppermann 	 */
1966b773dffSAndre Oppermann 	if (oip->ip_p == IPPROTO_TCP) {
1976b773dffSAndre Oppermann 		struct tcphdr *th;
1986b773dffSAndre Oppermann 		int tcphlen;
1996b773dffSAndre Oppermann 
2006b773dffSAndre Oppermann 		if (n->m_len < oiplen + sizeof(struct tcphdr) &&
2016b773dffSAndre Oppermann 		    ((n = m_pullup(n, oiplen + sizeof(struct tcphdr))) == NULL))
2026b773dffSAndre Oppermann 			goto freeit;
2036b773dffSAndre Oppermann 		th = (struct tcphdr *)((caddr_t)oip + oiplen);
2046b773dffSAndre Oppermann 		tcphlen = th->th_off << 2;
2056b773dffSAndre Oppermann 		if (tcphlen < sizeof(struct tcphdr))
2066b773dffSAndre Oppermann 			goto freeit;
2076b773dffSAndre Oppermann 		if (oip->ip_len < oiplen + tcphlen)
2086b773dffSAndre Oppermann 			goto freeit;
2096b773dffSAndre Oppermann 		if (n->m_len < oiplen + tcphlen &&
2106b773dffSAndre Oppermann 		    ((n = m_pullup(n, oiplen + tcphlen)) == NULL))
2116b773dffSAndre Oppermann 			goto freeit;
2126b773dffSAndre Oppermann 		icmplen = max(oiplen + tcphlen, min(icmp_quotelen, oip->ip_len));
2136b773dffSAndre Oppermann 	} else
214bb10780fSAndre Oppermann 		icmplen = min(oiplen + max(8, icmp_quotelen), oip->ip_len);
215bfef7ed4SIan Dowse 	if (icmplen < sizeof(struct ip))
216bfef7ed4SIan Dowse 		panic("icmp_error: bad length");
2176b773dffSAndre Oppermann 	if (icmplen + ICMP_MINLEN + sizeof(struct ip) > MHLEN)
2186b773dffSAndre Oppermann 		MCLGET(m, M_DONTWAIT);
2196b773dffSAndre Oppermann 	if (!(m->m_flags & M_EXT))
2206b773dffSAndre Oppermann 		goto freeit;
221df8bae1dSRodney W. Grimes 	m->m_len = icmplen + ICMP_MINLEN;
222df8bae1dSRodney W. Grimes 	MH_ALIGN(m, m->m_len);
223df8bae1dSRodney W. Grimes 	icp = mtod(m, struct icmp *);
224df8bae1dSRodney W. Grimes 	if ((u_int)type > ICMP_MAXTYPE)
225df8bae1dSRodney W. Grimes 		panic("icmp_error");
226df8bae1dSRodney W. Grimes 	icmpstat.icps_outhist[type]++;
227df8bae1dSRodney W. Grimes 	icp->icmp_type = type;
228df8bae1dSRodney W. Grimes 	if (type == ICMP_REDIRECT)
229df8bae1dSRodney W. Grimes 		icp->icmp_gwaddr.s_addr = dest;
230df8bae1dSRodney W. Grimes 	else {
231df8bae1dSRodney W. Grimes 		icp->icmp_void = 0;
232df8bae1dSRodney W. Grimes 		/*
233df8bae1dSRodney W. Grimes 		 * The following assignments assume an overlay with the
234df8bae1dSRodney W. Grimes 		 * zeroed icmp_void field.
235df8bae1dSRodney W. Grimes 		 */
236df8bae1dSRodney W. Grimes 		if (type == ICMP_PARAMPROB) {
237df8bae1dSRodney W. Grimes 			icp->icmp_pptr = code;
238df8bae1dSRodney W. Grimes 			code = 0;
239df8bae1dSRodney W. Grimes 		} else if (type == ICMP_UNREACH &&
240c773494eSAndre Oppermann 			code == ICMP_UNREACH_NEEDFRAG && mtu) {
241c773494eSAndre Oppermann 			icp->icmp_nextmtu = htons(mtu);
242df8bae1dSRodney W. Grimes 		}
243df8bae1dSRodney W. Grimes 	}
244df8bae1dSRodney W. Grimes 
245df8bae1dSRodney W. Grimes 	icp->icmp_code = code;
246bfef7ed4SIan Dowse 	m_copydata(n, 0, icmplen, (caddr_t)&icp->icmp_ip);
247df8bae1dSRodney W. Grimes 	nip = &icp->icmp_ip;
24804287599SRuslan Ermilov 
24904287599SRuslan Ermilov 	/*
25004287599SRuslan Ermilov 	 * Convert fields to network representation.
25104287599SRuslan Ermilov 	 */
252fd8e4ebcSMike Barcroft 	nip->ip_len = htons(nip->ip_len);
253fd8e4ebcSMike Barcroft 	nip->ip_off = htons(nip->ip_off);
254df8bae1dSRodney W. Grimes 
255df8bae1dSRodney W. Grimes 	/*
256df8bae1dSRodney W. Grimes 	 * Now, copy old ip header (without options)
257df8bae1dSRodney W. Grimes 	 * in front of icmp message.
258df8bae1dSRodney W. Grimes 	 */
259df8bae1dSRodney W. Grimes 	if (m->m_data - sizeof(struct ip) < m->m_pktdat)
260df8bae1dSRodney W. Grimes 		panic("icmp len");
261c550f220SMax Laier 	/*
262c550f220SMax Laier 	 * If the original mbuf was meant to bypass the firewall, the error
263c550f220SMax Laier 	 * reply should bypass as well.
264c550f220SMax Laier 	 */
265c550f220SMax Laier 	m->m_flags |= n->m_flags & M_SKIP_FIREWALL;
266df8bae1dSRodney W. Grimes 	m->m_data -= sizeof(struct ip);
267df8bae1dSRodney W. Grimes 	m->m_len += sizeof(struct ip);
268df8bae1dSRodney W. Grimes 	m->m_pkthdr.len = m->m_len;
269df8bae1dSRodney W. Grimes 	m->m_pkthdr.rcvif = n->m_pkthdr.rcvif;
270df8bae1dSRodney W. Grimes 	nip = mtod(m, struct ip *);
271df8bae1dSRodney W. Grimes 	bcopy((caddr_t)oip, (caddr_t)nip, sizeof(struct ip));
272df8bae1dSRodney W. Grimes 	nip->ip_len = m->m_len;
27353be11f6SPoul-Henning Kamp 	nip->ip_v = IPVERSION;
27453be11f6SPoul-Henning Kamp 	nip->ip_hl = 5;
275df8bae1dSRodney W. Grimes 	nip->ip_p = IPPROTO_ICMP;
276df8bae1dSRodney W. Grimes 	nip->ip_tos = 0;
277df8bae1dSRodney W. Grimes 	icmp_reflect(m);
278df8bae1dSRodney W. Grimes 
279df8bae1dSRodney W. Grimes freeit:
280df8bae1dSRodney W. Grimes 	m_freem(n);
281df8bae1dSRodney W. Grimes }
282df8bae1dSRodney W. Grimes 
283df8bae1dSRodney W. Grimes /*
284df8bae1dSRodney W. Grimes  * Process a received ICMP message.
285df8bae1dSRodney W. Grimes  */
286df8bae1dSRodney W. Grimes void
287f0ffb944SJulian Elischer icmp_input(m, off)
28816d6c90fSAndre Oppermann 	struct mbuf *m;
289f0ffb944SJulian Elischer 	int off;
290df8bae1dSRodney W. Grimes {
29116d6c90fSAndre Oppermann 	struct icmp *icp;
292df8bae1dSRodney W. Grimes 	struct in_ifaddr *ia;
29316d6c90fSAndre Oppermann 	struct ip *ip = mtod(m, struct ip *);
29416d6c90fSAndre Oppermann 	struct sockaddr_in icmpsrc, icmpdst, icmpgw;
29516d6c90fSAndre Oppermann 	int hlen = off;
29616d6c90fSAndre Oppermann 	int icmplen = ip->ip_len;
29716d6c90fSAndre Oppermann 	int i, code;
2984d77a549SAlfred Perlstein 	void (*ctlfunc)(int, struct sockaddr *, void *);
299df8bae1dSRodney W. Grimes 
300df8bae1dSRodney W. Grimes 	/*
301df8bae1dSRodney W. Grimes 	 * Locate icmp structure in mbuf, and check
302df8bae1dSRodney W. Grimes 	 * that not corrupted and of at least minimum length.
303df8bae1dSRodney W. Grimes 	 */
304df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS
3052b758395SGarrett Wollman 	if (icmpprintfs) {
3062b758395SGarrett Wollman 		char buf[4 * sizeof "123"];
3072b758395SGarrett Wollman 		strcpy(buf, inet_ntoa(ip->ip_src));
3082b758395SGarrett Wollman 		printf("icmp_input from %s to %s, len %d\n",
3092b758395SGarrett Wollman 		       buf, inet_ntoa(ip->ip_dst), icmplen);
3102b758395SGarrett Wollman 	}
311df8bae1dSRodney W. Grimes #endif
312df8bae1dSRodney W. Grimes 	if (icmplen < ICMP_MINLEN) {
313df8bae1dSRodney W. Grimes 		icmpstat.icps_tooshort++;
314df8bae1dSRodney W. Grimes 		goto freeit;
315df8bae1dSRodney W. Grimes 	}
316df8bae1dSRodney W. Grimes 	i = hlen + min(icmplen, ICMP_ADVLENMIN);
317df8bae1dSRodney W. Grimes 	if (m->m_len < i && (m = m_pullup(m, i)) == 0)  {
318df8bae1dSRodney W. Grimes 		icmpstat.icps_tooshort++;
319df8bae1dSRodney W. Grimes 		return;
320df8bae1dSRodney W. Grimes 	}
321df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
322df8bae1dSRodney W. Grimes 	m->m_len -= hlen;
323df8bae1dSRodney W. Grimes 	m->m_data += hlen;
324df8bae1dSRodney W. Grimes 	icp = mtod(m, struct icmp *);
325df8bae1dSRodney W. Grimes 	if (in_cksum(m, icmplen)) {
326df8bae1dSRodney W. Grimes 		icmpstat.icps_checksum++;
327df8bae1dSRodney W. Grimes 		goto freeit;
328df8bae1dSRodney W. Grimes 	}
329df8bae1dSRodney W. Grimes 	m->m_len += hlen;
330df8bae1dSRodney W. Grimes 	m->m_data -= hlen;
331df8bae1dSRodney W. Grimes 
3326a800098SYoshinobu Inoue 	if (m->m_pkthdr.rcvif && m->m_pkthdr.rcvif->if_type == IFT_FAITH) {
3336a800098SYoshinobu Inoue 		/*
3346a800098SYoshinobu Inoue 		 * Deliver very specific ICMP type only.
3356a800098SYoshinobu Inoue 		 */
3366a800098SYoshinobu Inoue 		switch (icp->icmp_type) {
3376a800098SYoshinobu Inoue 		case ICMP_UNREACH:
3386a800098SYoshinobu Inoue 		case ICMP_TIMXCEED:
3396a800098SYoshinobu Inoue 			break;
3406a800098SYoshinobu Inoue 		default:
3416a800098SYoshinobu Inoue 			goto freeit;
3426a800098SYoshinobu Inoue 		}
3436a800098SYoshinobu Inoue 	}
3446a800098SYoshinobu Inoue 
345df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS
346df8bae1dSRodney W. Grimes 	if (icmpprintfs)
347df8bae1dSRodney W. Grimes 		printf("icmp_input, type %d code %d\n", icp->icmp_type,
348df8bae1dSRodney W. Grimes 		    icp->icmp_code);
349df8bae1dSRodney W. Grimes #endif
3505b7ee6edSGarrett Wollman 
3515b7ee6edSGarrett Wollman 	/*
3525b7ee6edSGarrett Wollman 	 * Message type specific processing.
3535b7ee6edSGarrett Wollman 	 */
354df8bae1dSRodney W. Grimes 	if (icp->icmp_type > ICMP_MAXTYPE)
355df8bae1dSRodney W. Grimes 		goto raw;
35616d6c90fSAndre Oppermann 
35716d6c90fSAndre Oppermann 	/* Initialize */
35816d6c90fSAndre Oppermann 	bzero(&icmpsrc, sizeof(icmpsrc));
35916d6c90fSAndre Oppermann 	icmpsrc.sin_len = sizeof(struct sockaddr_in);
36016d6c90fSAndre Oppermann 	icmpsrc.sin_family = AF_INET;
36116d6c90fSAndre Oppermann 	bzero(&icmpdst, sizeof(icmpdst));
36216d6c90fSAndre Oppermann 	icmpdst.sin_len = sizeof(struct sockaddr_in);
36316d6c90fSAndre Oppermann 	icmpdst.sin_family = AF_INET;
36416d6c90fSAndre Oppermann 	bzero(&icmpgw, sizeof(icmpgw));
36516d6c90fSAndre Oppermann 	icmpgw.sin_len = sizeof(struct sockaddr_in);
36616d6c90fSAndre Oppermann 	icmpgw.sin_family = AF_INET;
36716d6c90fSAndre Oppermann 
368df8bae1dSRodney W. Grimes 	icmpstat.icps_inhist[icp->icmp_type]++;
369df8bae1dSRodney W. Grimes 	code = icp->icmp_code;
370df8bae1dSRodney W. Grimes 	switch (icp->icmp_type) {
371df8bae1dSRodney W. Grimes 
372df8bae1dSRodney W. Grimes 	case ICMP_UNREACH:
373df8bae1dSRodney W. Grimes 		switch (code) {
374df8bae1dSRodney W. Grimes 			case ICMP_UNREACH_NET:
375df8bae1dSRodney W. Grimes 			case ICMP_UNREACH_HOST:
376df8bae1dSRodney W. Grimes 			case ICMP_UNREACH_SRCFAIL:
377e4bb5b05SJonathan Lemon 			case ICMP_UNREACH_NET_UNKNOWN:
378e4bb5b05SJonathan Lemon 			case ICMP_UNREACH_HOST_UNKNOWN:
379e4bb5b05SJonathan Lemon 			case ICMP_UNREACH_ISOLATED:
380e4bb5b05SJonathan Lemon 			case ICMP_UNREACH_TOSNET:
381e4bb5b05SJonathan Lemon 			case ICMP_UNREACH_TOSHOST:
382e4bb5b05SJonathan Lemon 			case ICMP_UNREACH_HOST_PRECEDENCE:
383e4bb5b05SJonathan Lemon 			case ICMP_UNREACH_PRECEDENCE_CUTOFF:
384e4bb5b05SJonathan Lemon 				code = PRC_UNREACH_NET;
385df8bae1dSRodney W. Grimes 				break;
386df8bae1dSRodney W. Grimes 
387df8bae1dSRodney W. Grimes 			case ICMP_UNREACH_NEEDFRAG:
388df8bae1dSRodney W. Grimes 				code = PRC_MSGSIZE;
389df8bae1dSRodney W. Grimes 				break;
390df8bae1dSRodney W. Grimes 
391e4bb5b05SJonathan Lemon 			/*
392e4bb5b05SJonathan Lemon 			 * RFC 1122, Sections 3.2.2.1 and 4.2.3.9.
393e4bb5b05SJonathan Lemon 			 * Treat subcodes 2,3 as immediate RST
394e4bb5b05SJonathan Lemon 			 */
395e4bb5b05SJonathan Lemon 			case ICMP_UNREACH_PROTOCOL:
396e4bb5b05SJonathan Lemon 			case ICMP_UNREACH_PORT:
397b77d155dSJesper Skriver 				code = PRC_UNREACH_PORT;
398b11d7a4aSPoul-Henning Kamp 				break;
39990fcbbd6SPoul-Henning Kamp 
40090fcbbd6SPoul-Henning Kamp 			case ICMP_UNREACH_NET_PROHIB:
40190fcbbd6SPoul-Henning Kamp 			case ICMP_UNREACH_HOST_PROHIB:
4029c4b2574SPaul Traina 			case ICMP_UNREACH_FILTER_PROHIB:
40390fcbbd6SPoul-Henning Kamp 				code = PRC_UNREACH_ADMIN_PROHIB;
404b11d7a4aSPoul-Henning Kamp 				break;
405b11d7a4aSPoul-Henning Kamp 
406df8bae1dSRodney W. Grimes 			default:
407df8bae1dSRodney W. Grimes 				goto badcode;
408df8bae1dSRodney W. Grimes 		}
409df8bae1dSRodney W. Grimes 		goto deliver;
410df8bae1dSRodney W. Grimes 
411df8bae1dSRodney W. Grimes 	case ICMP_TIMXCEED:
412df8bae1dSRodney W. Grimes 		if (code > 1)
413df8bae1dSRodney W. Grimes 			goto badcode;
414df8bae1dSRodney W. Grimes 		code += PRC_TIMXCEED_INTRANS;
415df8bae1dSRodney W. Grimes 		goto deliver;
416df8bae1dSRodney W. Grimes 
417df8bae1dSRodney W. Grimes 	case ICMP_PARAMPROB:
418df8bae1dSRodney W. Grimes 		if (code > 1)
419df8bae1dSRodney W. Grimes 			goto badcode;
420df8bae1dSRodney W. Grimes 		code = PRC_PARAMPROB;
421df8bae1dSRodney W. Grimes 		goto deliver;
422df8bae1dSRodney W. Grimes 
423df8bae1dSRodney W. Grimes 	case ICMP_SOURCEQUENCH:
424df8bae1dSRodney W. Grimes 		if (code)
425df8bae1dSRodney W. Grimes 			goto badcode;
426df8bae1dSRodney W. Grimes 		code = PRC_QUENCH;
427df8bae1dSRodney W. Grimes 	deliver:
428df8bae1dSRodney W. Grimes 		/*
429df8bae1dSRodney W. Grimes 		 * Problem with datagram; advise higher level routines.
430df8bae1dSRodney W. Grimes 		 */
431df8bae1dSRodney W. Grimes 		if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) ||
43253be11f6SPoul-Henning Kamp 		    icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) {
433df8bae1dSRodney W. Grimes 			icmpstat.icps_badlen++;
434df8bae1dSRodney W. Grimes 			goto freeit;
435df8bae1dSRodney W. Grimes 		}
436fd8e4ebcSMike Barcroft 		icp->icmp_ip.ip_len = ntohs(icp->icmp_ip.ip_len);
4375b7ee6edSGarrett Wollman 		/* Discard ICMP's in response to multicast packets */
4385b7ee6edSGarrett Wollman 		if (IN_MULTICAST(ntohl(icp->icmp_ip.ip_dst.s_addr)))
4395b7ee6edSGarrett Wollman 			goto badcode;
440df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS
441df8bae1dSRodney W. Grimes 		if (icmpprintfs)
442df8bae1dSRodney W. Grimes 			printf("deliver to protocol %d\n", icp->icmp_ip.ip_p);
443df8bae1dSRodney W. Grimes #endif
444df8bae1dSRodney W. Grimes 		icmpsrc.sin_addr = icp->icmp_ip.ip_dst;
4456a800098SYoshinobu Inoue 		/*
4466a800098SYoshinobu Inoue 		 * XXX if the packet contains [IPv4 AH TCP], we can't make a
4476a800098SYoshinobu Inoue 		 * notification to TCP layer.
4486a800098SYoshinobu Inoue 		 */
449623ae52eSPoul-Henning Kamp 		ctlfunc = inetsw[ip_protox[icp->icmp_ip.ip_p]].pr_ctlinput;
450623ae52eSPoul-Henning Kamp 		if (ctlfunc)
451df8bae1dSRodney W. Grimes 			(*ctlfunc)(code, (struct sockaddr *)&icmpsrc,
452b62d102cSBruce Evans 				   (void *)&icp->icmp_ip);
453df8bae1dSRodney W. Grimes 		break;
454df8bae1dSRodney W. Grimes 
455df8bae1dSRodney W. Grimes 	badcode:
456df8bae1dSRodney W. Grimes 		icmpstat.icps_badcode++;
457df8bae1dSRodney W. Grimes 		break;
458df8bae1dSRodney W. Grimes 
459df8bae1dSRodney W. Grimes 	case ICMP_ECHO:
4607022ea0aSGarrett Wollman 		if (!icmpbmcastecho
461d311884fSDavid Greenman 		    && (m->m_flags & (M_MCAST | M_BCAST)) != 0) {
4627022ea0aSGarrett Wollman 			icmpstat.icps_bmcastecho++;
4637022ea0aSGarrett Wollman 			break;
4647022ea0aSGarrett Wollman 		}
465df8bae1dSRodney W. Grimes 		icp->icmp_type = ICMP_ECHOREPLY;
466a57815efSBosko Milekic 		if (badport_bandlim(BANDLIM_ICMP_ECHO) < 0)
46709f81a46SBosko Milekic 			goto freeit;
46809f81a46SBosko Milekic 		else
469df8bae1dSRodney W. Grimes 			goto reflect;
470df8bae1dSRodney W. Grimes 
471df8bae1dSRodney W. Grimes 	case ICMP_TSTAMP:
472fe0fb8abSGarrett Wollman 		if (!icmpbmcastecho
473d311884fSDavid Greenman 		    && (m->m_flags & (M_MCAST | M_BCAST)) != 0) {
474fe0fb8abSGarrett Wollman 			icmpstat.icps_bmcasttstamp++;
475fe0fb8abSGarrett Wollman 			break;
476fe0fb8abSGarrett Wollman 		}
477df8bae1dSRodney W. Grimes 		if (icmplen < ICMP_TSLEN) {
478df8bae1dSRodney W. Grimes 			icmpstat.icps_badlen++;
479df8bae1dSRodney W. Grimes 			break;
480df8bae1dSRodney W. Grimes 		}
481df8bae1dSRodney W. Grimes 		icp->icmp_type = ICMP_TSTAMPREPLY;
482df8bae1dSRodney W. Grimes 		icp->icmp_rtime = iptime();
483df8bae1dSRodney W. Grimes 		icp->icmp_ttime = icp->icmp_rtime;	/* bogus, do later! */
484a57815efSBosko Milekic 		if (badport_bandlim(BANDLIM_ICMP_TSTAMP) < 0)
48509f81a46SBosko Milekic 			goto freeit;
48609f81a46SBosko Milekic 		else
487df8bae1dSRodney W. Grimes 			goto reflect;
488df8bae1dSRodney W. Grimes 
489df8bae1dSRodney W. Grimes 	case ICMP_MASKREQ:
490df8bae1dSRodney W. Grimes 		if (icmpmaskrepl == 0)
491df8bae1dSRodney W. Grimes 			break;
492df8bae1dSRodney W. Grimes 		/*
493df8bae1dSRodney W. Grimes 		 * We are not able to respond with all ones broadcast
494df8bae1dSRodney W. Grimes 		 * unless we receive it over a point-to-point interface.
495df8bae1dSRodney W. Grimes 		 */
496df8bae1dSRodney W. Grimes 		if (icmplen < ICMP_MASKLEN)
497df8bae1dSRodney W. Grimes 			break;
498df8bae1dSRodney W. Grimes 		switch (ip->ip_dst.s_addr) {
499df8bae1dSRodney W. Grimes 
500df8bae1dSRodney W. Grimes 		case INADDR_BROADCAST:
501df8bae1dSRodney W. Grimes 		case INADDR_ANY:
502df8bae1dSRodney W. Grimes 			icmpdst.sin_addr = ip->ip_src;
503df8bae1dSRodney W. Grimes 			break;
504df8bae1dSRodney W. Grimes 
505df8bae1dSRodney W. Grimes 		default:
506df8bae1dSRodney W. Grimes 			icmpdst.sin_addr = ip->ip_dst;
507df8bae1dSRodney W. Grimes 		}
508df8bae1dSRodney W. Grimes 		ia = (struct in_ifaddr *)ifaof_ifpforaddr(
509df8bae1dSRodney W. Grimes 			    (struct sockaddr *)&icmpdst, m->m_pkthdr.rcvif);
510df8bae1dSRodney W. Grimes 		if (ia == 0)
511df8bae1dSRodney W. Grimes 			break;
5127e6f7714SPoul-Henning Kamp 		if (ia->ia_ifp == 0)
5137e6f7714SPoul-Henning Kamp 			break;
514df8bae1dSRodney W. Grimes 		icp->icmp_type = ICMP_MASKREPLY;
51557842a38SMatthew N. Dodd 		if (icmpmaskfake == 0)
516df8bae1dSRodney W. Grimes 			icp->icmp_mask = ia->ia_sockmask.sin_addr.s_addr;
51757842a38SMatthew N. Dodd 		else
51857842a38SMatthew N. Dodd 			icp->icmp_mask = icmpmaskfake;
519df8bae1dSRodney W. Grimes 		if (ip->ip_src.s_addr == 0) {
520df8bae1dSRodney W. Grimes 			if (ia->ia_ifp->if_flags & IFF_BROADCAST)
521df8bae1dSRodney W. Grimes 			    ip->ip_src = satosin(&ia->ia_broadaddr)->sin_addr;
522df8bae1dSRodney W. Grimes 			else if (ia->ia_ifp->if_flags & IFF_POINTOPOINT)
523df8bae1dSRodney W. Grimes 			    ip->ip_src = satosin(&ia->ia_dstaddr)->sin_addr;
524df8bae1dSRodney W. Grimes 		}
525df8bae1dSRodney W. Grimes reflect:
526df8bae1dSRodney W. Grimes 		ip->ip_len += hlen;	/* since ip_input deducts this */
527df8bae1dSRodney W. Grimes 		icmpstat.icps_reflect++;
528df8bae1dSRodney W. Grimes 		icmpstat.icps_outhist[icp->icmp_type]++;
529df8bae1dSRodney W. Grimes 		icmp_reflect(m);
530df8bae1dSRodney W. Grimes 		return;
531df8bae1dSRodney W. Grimes 
532df8bae1dSRodney W. Grimes 	case ICMP_REDIRECT:
53318d3153eSDag-Erling Smørgrav 		if (log_redirect) {
53418d3153eSDag-Erling Smørgrav 			u_long src, dst, gw;
53518d3153eSDag-Erling Smørgrav 
53618d3153eSDag-Erling Smørgrav 			src = ntohl(ip->ip_src.s_addr);
53718d3153eSDag-Erling Smørgrav 			dst = ntohl(icp->icmp_ip.ip_dst.s_addr);
53818d3153eSDag-Erling Smørgrav 			gw = ntohl(icp->icmp_gwaddr.s_addr);
53918d3153eSDag-Erling Smørgrav 			printf("icmp redirect from %d.%d.%d.%d: "
54018d3153eSDag-Erling Smørgrav 			       "%d.%d.%d.%d => %d.%d.%d.%d\n",
54118d3153eSDag-Erling Smørgrav 			       (int)(src >> 24), (int)((src >> 16) & 0xff),
54218d3153eSDag-Erling Smørgrav 			       (int)((src >> 8) & 0xff), (int)(src & 0xff),
54318d3153eSDag-Erling Smørgrav 			       (int)(dst >> 24), (int)((dst >> 16) & 0xff),
54418d3153eSDag-Erling Smørgrav 			       (int)((dst >> 8) & 0xff), (int)(dst & 0xff),
54518d3153eSDag-Erling Smørgrav 			       (int)(gw >> 24), (int)((gw >> 16) & 0xff),
54618d3153eSDag-Erling Smørgrav 			       (int)((gw >> 8) & 0xff), (int)(gw & 0xff));
54718d3153eSDag-Erling Smørgrav 		}
54887c3bd27SAndre Oppermann 		/*
54987c3bd27SAndre Oppermann 		 * RFC1812 says we must ignore ICMP redirects if we
55087c3bd27SAndre Oppermann 		 * are acting as router.
55187c3bd27SAndre Oppermann 		 */
55287c3bd27SAndre Oppermann 		if (drop_redirect || ipforwarding)
55318d3153eSDag-Erling Smørgrav 			break;
554df8bae1dSRodney W. Grimes 		if (code > 3)
555df8bae1dSRodney W. Grimes 			goto badcode;
556df8bae1dSRodney W. Grimes 		if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) ||
55753be11f6SPoul-Henning Kamp 		    icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) {
558df8bae1dSRodney W. Grimes 			icmpstat.icps_badlen++;
559df8bae1dSRodney W. Grimes 			break;
560df8bae1dSRodney W. Grimes 		}
561df8bae1dSRodney W. Grimes 		/*
562df8bae1dSRodney W. Grimes 		 * Short circuit routing redirects to force
563df8bae1dSRodney W. Grimes 		 * immediate change in the kernel's routing
564df8bae1dSRodney W. Grimes 		 * tables.  The message is also handed to anyone
565df8bae1dSRodney W. Grimes 		 * listening on a raw socket (e.g. the routing
566df8bae1dSRodney W. Grimes 		 * daemon for use in updating its tables).
567df8bae1dSRodney W. Grimes 		 */
568df8bae1dSRodney W. Grimes 		icmpgw.sin_addr = ip->ip_src;
569df8bae1dSRodney W. Grimes 		icmpdst.sin_addr = icp->icmp_gwaddr;
570df8bae1dSRodney W. Grimes #ifdef	ICMPPRINTFS
5712b758395SGarrett Wollman 		if (icmpprintfs) {
5722b758395SGarrett Wollman 			char buf[4 * sizeof "123"];
5732b758395SGarrett Wollman 			strcpy(buf, inet_ntoa(icp->icmp_ip.ip_dst));
5742b758395SGarrett Wollman 
5752b758395SGarrett Wollman 			printf("redirect dst %s to %s\n",
5762b758395SGarrett Wollman 			       buf, inet_ntoa(icp->icmp_gwaddr));
5772b758395SGarrett Wollman 		}
578df8bae1dSRodney W. Grimes #endif
579df8bae1dSRodney W. Grimes 		icmpsrc.sin_addr = icp->icmp_ip.ip_dst;
580df8bae1dSRodney W. Grimes 		rtredirect((struct sockaddr *)&icmpsrc,
581df8bae1dSRodney W. Grimes 		  (struct sockaddr *)&icmpdst,
582df8bae1dSRodney W. Grimes 		  (struct sockaddr *)0, RTF_GATEWAY | RTF_HOST,
583d1dd20beSSam Leffler 		  (struct sockaddr *)&icmpgw);
584df8bae1dSRodney W. Grimes 		pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&icmpsrc);
5856a800098SYoshinobu Inoue #ifdef IPSEC
5866a800098SYoshinobu Inoue 		key_sa_routechange((struct sockaddr *)&icmpsrc);
5876a800098SYoshinobu Inoue #endif
588df8bae1dSRodney W. Grimes 		break;
589df8bae1dSRodney W. Grimes 
590df8bae1dSRodney W. Grimes 	/*
591df8bae1dSRodney W. Grimes 	 * No kernel processing for the following;
592df8bae1dSRodney W. Grimes 	 * just fall through to send to raw listener.
593df8bae1dSRodney W. Grimes 	 */
594df8bae1dSRodney W. Grimes 	case ICMP_ECHOREPLY:
595df8bae1dSRodney W. Grimes 	case ICMP_ROUTERADVERT:
596df8bae1dSRodney W. Grimes 	case ICMP_ROUTERSOLICIT:
597df8bae1dSRodney W. Grimes 	case ICMP_TSTAMPREPLY:
598df8bae1dSRodney W. Grimes 	case ICMP_IREQREPLY:
599df8bae1dSRodney W. Grimes 	case ICMP_MASKREPLY:
600df8bae1dSRodney W. Grimes 	default:
601df8bae1dSRodney W. Grimes 		break;
602df8bae1dSRodney W. Grimes 	}
603df8bae1dSRodney W. Grimes 
604df8bae1dSRodney W. Grimes raw:
605f0ffb944SJulian Elischer 	rip_input(m, off);
606df8bae1dSRodney W. Grimes 	return;
607df8bae1dSRodney W. Grimes 
608df8bae1dSRodney W. Grimes freeit:
609df8bae1dSRodney W. Grimes 	m_freem(m);
610df8bae1dSRodney W. Grimes }
611df8bae1dSRodney W. Grimes 
612df8bae1dSRodney W. Grimes /*
613df8bae1dSRodney W. Grimes  * Reflect the ip packet back to the source
614df8bae1dSRodney W. Grimes  */
6150312fbe9SPoul-Henning Kamp static void
616df8bae1dSRodney W. Grimes icmp_reflect(m)
617df8bae1dSRodney W. Grimes 	struct mbuf *m;
618df8bae1dSRodney W. Grimes {
619ca925d9cSJonathan Lemon 	struct ip *ip = mtod(m, struct ip *);
620ca925d9cSJonathan Lemon 	struct ifaddr *ifa;
621b74d89bbSAndre Oppermann 	struct ifnet *ifn;
622ca925d9cSJonathan Lemon 	struct in_ifaddr *ia;
623df8bae1dSRodney W. Grimes 	struct in_addr t;
624b5e8ce9fSBruce Evans 	struct mbuf *opts = 0;
62553be11f6SPoul-Henning Kamp 	int optlen = (ip->ip_hl << 2) - sizeof(struct ip);
626df8bae1dSRodney W. Grimes 
627df8bae1dSRodney W. Grimes 	if (!in_canforward(ip->ip_src) &&
628df8bae1dSRodney W. Grimes 	    ((ntohl(ip->ip_src.s_addr) & IN_CLASSA_NET) !=
629df8bae1dSRodney W. Grimes 	     (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))) {
630df8bae1dSRodney W. Grimes 		m_freem(m);	/* Bad return address */
631bd714208SRuslan Ermilov 		icmpstat.icps_badaddr++;
632df8bae1dSRodney W. Grimes 		goto done;	/* Ip_output() will check for broadcast */
633df8bae1dSRodney W. Grimes 	}
634df8bae1dSRodney W. Grimes 	t = ip->ip_dst;
635df8bae1dSRodney W. Grimes 	ip->ip_dst = ip->ip_src;
6361488eac8SAndre Oppermann 
637df8bae1dSRodney W. Grimes 	/*
6381488eac8SAndre Oppermann 	 * Source selection for ICMP replies:
6391488eac8SAndre Oppermann 	 *
6401488eac8SAndre Oppermann 	 * If the incoming packet was addressed directly to one of our
6411488eac8SAndre Oppermann 	 * own addresses, use dst as the src for the reply.
642df8bae1dSRodney W. Grimes 	 */
643ca925d9cSJonathan Lemon 	LIST_FOREACH(ia, INADDR_HASH(t.s_addr), ia_hash)
644df8bae1dSRodney W. Grimes 		if (t.s_addr == IA_SIN(ia)->sin_addr.s_addr)
645ca925d9cSJonathan Lemon 			goto match;
6461488eac8SAndre Oppermann 	/*
6471488eac8SAndre Oppermann 	 * If the incoming packet was addressed to one of our broadcast
6481488eac8SAndre Oppermann 	 * addresses, use the first non-broadcast address which corresponds
6491488eac8SAndre Oppermann 	 * to the incoming interface.
6501488eac8SAndre Oppermann 	 */
65184caf008SRuslan Ermilov 	if (m->m_pkthdr.rcvif != NULL &&
65284caf008SRuslan Ermilov 	    m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST) {
653ca925d9cSJonathan Lemon 		TAILQ_FOREACH(ifa, &m->m_pkthdr.rcvif->if_addrhead, ifa_link) {
654ca925d9cSJonathan Lemon 			if (ifa->ifa_addr->sa_family != AF_INET)
655ca925d9cSJonathan Lemon 				continue;
656ca925d9cSJonathan Lemon 			ia = ifatoia(ifa);
657ca925d9cSJonathan Lemon 			if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
658ca925d9cSJonathan Lemon 			    t.s_addr)
659ca925d9cSJonathan Lemon 				goto match;
660df8bae1dSRodney W. Grimes 		}
661ca925d9cSJonathan Lemon 	}
6621488eac8SAndre Oppermann 	/*
663a0866c8dSAndre Oppermann 	 * If the packet was transiting through us, use the address of
664a0866c8dSAndre Oppermann 	 * the interface the packet came through in.  If that interface
665a0866c8dSAndre Oppermann 	 * doesn't have a suitable IP address, the normal selection
666a0866c8dSAndre Oppermann 	 * criteria apply.
667a0866c8dSAndre Oppermann 	 */
668a0866c8dSAndre Oppermann 	if (icmp_rfi && m->m_pkthdr.rcvif != NULL) {
669a0866c8dSAndre Oppermann 		TAILQ_FOREACH(ifa, &m->m_pkthdr.rcvif->if_addrhead, ifa_link) {
670a0866c8dSAndre Oppermann 			if (ifa->ifa_addr->sa_family != AF_INET)
671a0866c8dSAndre Oppermann 				continue;
672a0866c8dSAndre Oppermann 			ia = ifatoia(ifa);
673a0866c8dSAndre Oppermann 			goto match;
674a0866c8dSAndre Oppermann 		}
675a0866c8dSAndre Oppermann 	}
676a0866c8dSAndre Oppermann 	/*
677b74d89bbSAndre Oppermann 	 * If the incoming packet was not addressed directly to us, use
678b74d89bbSAndre Oppermann 	 * designated interface for icmp replies specified by sysctl
679b74d89bbSAndre Oppermann 	 * net.inet.icmp.reply_src (default not set). Otherwise continue
680b74d89bbSAndre Oppermann 	 * with normal source selection.
681b74d89bbSAndre Oppermann 	 */
682b74d89bbSAndre Oppermann 	if (reply_src[0] != '\0' && (ifn = ifunit(reply_src))) {
683b74d89bbSAndre Oppermann 		TAILQ_FOREACH(ifa, &ifn->if_addrhead, ifa_link) {
684b74d89bbSAndre Oppermann 			if (ifa->ifa_addr->sa_family != AF_INET)
685b74d89bbSAndre Oppermann 				continue;
686b74d89bbSAndre Oppermann 			ia = ifatoia(ifa);
687b74d89bbSAndre Oppermann 			goto match;
688b74d89bbSAndre Oppermann 		}
689b74d89bbSAndre Oppermann 	}
690b74d89bbSAndre Oppermann 	/*
6911488eac8SAndre Oppermann 	 * If the packet was transiting through us, use the address of
6921488eac8SAndre Oppermann 	 * the interface that is the closest to the packet source.
6931488eac8SAndre Oppermann 	 * When we don't have a route back to the packet source, stop here
6941488eac8SAndre Oppermann 	 * and drop the packet.
6951488eac8SAndre Oppermann 	 */
69602c1c707SAndre Oppermann 	ia = ip_rtaddr(ip->ip_dst);
6970d4bef5dSDima Dorfman 	if (ia == NULL) {
6980d4bef5dSDima Dorfman 		m_freem(m);
699bd714208SRuslan Ermilov 		icmpstat.icps_noroute++;
7000d4bef5dSDima Dorfman 		goto done;
7010d4bef5dSDima Dorfman 	}
702ca925d9cSJonathan Lemon match:
703baee0c3eSRobert Watson #ifdef MAC
704baee0c3eSRobert Watson 	mac_reflect_mbuf_icmp(m);
705baee0c3eSRobert Watson #endif
706df8bae1dSRodney W. Grimes 	t = IA_SIN(ia)->sin_addr;
707df8bae1dSRodney W. Grimes 	ip->ip_src = t;
7088ce3f3ddSRuslan Ermilov 	ip->ip_ttl = ip_defttl;
709df8bae1dSRodney W. Grimes 
710df8bae1dSRodney W. Grimes 	if (optlen > 0) {
711df8bae1dSRodney W. Grimes 		register u_char *cp;
712df8bae1dSRodney W. Grimes 		int opt, cnt;
713df8bae1dSRodney W. Grimes 		u_int len;
714df8bae1dSRodney W. Grimes 
715df8bae1dSRodney W. Grimes 		/*
716df8bae1dSRodney W. Grimes 		 * Retrieve any source routing from the incoming packet;
717df8bae1dSRodney W. Grimes 		 * add on any record-route or timestamp options.
718df8bae1dSRodney W. Grimes 		 */
719df8bae1dSRodney W. Grimes 		cp = (u_char *) (ip + 1);
720e0982661SAndre Oppermann 		if ((opts = ip_srcroute(m)) == 0 &&
721a163d034SWarner Losh 		    (opts = m_gethdr(M_DONTWAIT, MT_HEADER))) {
722df8bae1dSRodney W. Grimes 			opts->m_len = sizeof(struct in_addr);
723df8bae1dSRodney W. Grimes 			mtod(opts, struct in_addr *)->s_addr = 0;
724df8bae1dSRodney W. Grimes 		}
725df8bae1dSRodney W. Grimes 		if (opts) {
726df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS
727df8bae1dSRodney W. Grimes 		    if (icmpprintfs)
728df8bae1dSRodney W. Grimes 			    printf("icmp_reflect optlen %d rt %d => ",
729df8bae1dSRodney W. Grimes 				optlen, opts->m_len);
730df8bae1dSRodney W. Grimes #endif
731df8bae1dSRodney W. Grimes 		    for (cnt = optlen; cnt > 0; cnt -= len, cp += len) {
732df8bae1dSRodney W. Grimes 			    opt = cp[IPOPT_OPTVAL];
733df8bae1dSRodney W. Grimes 			    if (opt == IPOPT_EOL)
734df8bae1dSRodney W. Grimes 				    break;
735df8bae1dSRodney W. Grimes 			    if (opt == IPOPT_NOP)
736df8bae1dSRodney W. Grimes 				    len = 1;
737df8bae1dSRodney W. Grimes 			    else {
738707d00a3SJonathan Lemon 				    if (cnt < IPOPT_OLEN + sizeof(*cp))
739707d00a3SJonathan Lemon 					    break;
740df8bae1dSRodney W. Grimes 				    len = cp[IPOPT_OLEN];
741707d00a3SJonathan Lemon 				    if (len < IPOPT_OLEN + sizeof(*cp) ||
742707d00a3SJonathan Lemon 				        len > cnt)
743df8bae1dSRodney W. Grimes 					    break;
744df8bae1dSRodney W. Grimes 			    }
745df8bae1dSRodney W. Grimes 			    /*
746df8bae1dSRodney W. Grimes 			     * Should check for overflow, but it "can't happen"
747df8bae1dSRodney W. Grimes 			     */
748df8bae1dSRodney W. Grimes 			    if (opt == IPOPT_RR || opt == IPOPT_TS ||
749df8bae1dSRodney W. Grimes 				opt == IPOPT_SECURITY) {
750df8bae1dSRodney W. Grimes 				    bcopy((caddr_t)cp,
751df8bae1dSRodney W. Grimes 					mtod(opts, caddr_t) + opts->m_len, len);
752df8bae1dSRodney W. Grimes 				    opts->m_len += len;
753df8bae1dSRodney W. Grimes 			    }
754df8bae1dSRodney W. Grimes 		    }
755df8bae1dSRodney W. Grimes 		    /* Terminate & pad, if necessary */
756623ae52eSPoul-Henning Kamp 		    cnt = opts->m_len % 4;
757623ae52eSPoul-Henning Kamp 		    if (cnt) {
758df8bae1dSRodney W. Grimes 			    for (; cnt < 4; cnt++) {
759df8bae1dSRodney W. Grimes 				    *(mtod(opts, caddr_t) + opts->m_len) =
760df8bae1dSRodney W. Grimes 					IPOPT_EOL;
761df8bae1dSRodney W. Grimes 				    opts->m_len++;
762df8bae1dSRodney W. Grimes 			    }
763df8bae1dSRodney W. Grimes 		    }
764df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS
765df8bae1dSRodney W. Grimes 		    if (icmpprintfs)
766df8bae1dSRodney W. Grimes 			    printf("%d\n", opts->m_len);
767df8bae1dSRodney W. Grimes #endif
768df8bae1dSRodney W. Grimes 		}
769df8bae1dSRodney W. Grimes 		/*
770df8bae1dSRodney W. Grimes 		 * Now strip out original options by copying rest of first
771df8bae1dSRodney W. Grimes 		 * mbuf's data back, and adjust the IP length.
772df8bae1dSRodney W. Grimes 		 */
773df8bae1dSRodney W. Grimes 		ip->ip_len -= optlen;
77453be11f6SPoul-Henning Kamp 		ip->ip_v = IPVERSION;
77553be11f6SPoul-Henning Kamp 		ip->ip_hl = 5;
776df8bae1dSRodney W. Grimes 		m->m_len -= optlen;
777df8bae1dSRodney W. Grimes 		if (m->m_flags & M_PKTHDR)
778df8bae1dSRodney W. Grimes 			m->m_pkthdr.len -= optlen;
779df8bae1dSRodney W. Grimes 		optlen += sizeof(struct ip);
780df8bae1dSRodney W. Grimes 		bcopy((caddr_t)ip + optlen, (caddr_t)(ip + 1),
781df8bae1dSRodney W. Grimes 			 (unsigned)(m->m_len - sizeof(struct ip)));
782df8bae1dSRodney W. Grimes 	}
7839c855a36SSam Leffler 	m_tag_delete_nonpersistent(m);
784df8bae1dSRodney W. Grimes 	m->m_flags &= ~(M_BCAST|M_MCAST);
78502c1c707SAndre Oppermann 	icmp_send(m, opts);
786df8bae1dSRodney W. Grimes done:
787df8bae1dSRodney W. Grimes 	if (opts)
788df8bae1dSRodney W. Grimes 		(void)m_free(opts);
789df8bae1dSRodney W. Grimes }
790df8bae1dSRodney W. Grimes 
791df8bae1dSRodney W. Grimes /*
792df8bae1dSRodney W. Grimes  * Send an icmp packet back to the ip level,
793df8bae1dSRodney W. Grimes  * after supplying a checksum.
794df8bae1dSRodney W. Grimes  */
7950312fbe9SPoul-Henning Kamp static void
79602c1c707SAndre Oppermann icmp_send(m, opts)
797df8bae1dSRodney W. Grimes 	register struct mbuf *m;
798df8bae1dSRodney W. Grimes 	struct mbuf *opts;
799df8bae1dSRodney W. Grimes {
800df8bae1dSRodney W. Grimes 	register struct ip *ip = mtod(m, struct ip *);
801df8bae1dSRodney W. Grimes 	register int hlen;
802df8bae1dSRodney W. Grimes 	register struct icmp *icp;
803df8bae1dSRodney W. Grimes 
80453be11f6SPoul-Henning Kamp 	hlen = ip->ip_hl << 2;
805df8bae1dSRodney W. Grimes 	m->m_data += hlen;
806df8bae1dSRodney W. Grimes 	m->m_len -= hlen;
807df8bae1dSRodney W. Grimes 	icp = mtod(m, struct icmp *);
808df8bae1dSRodney W. Grimes 	icp->icmp_cksum = 0;
809df8bae1dSRodney W. Grimes 	icp->icmp_cksum = in_cksum(m, ip->ip_len - hlen);
810df8bae1dSRodney W. Grimes 	m->m_data -= hlen;
811df8bae1dSRodney W. Grimes 	m->m_len += hlen;
81294446a2eSArchie Cobbs 	m->m_pkthdr.rcvif = (struct ifnet *)0;
813df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS
8142b758395SGarrett Wollman 	if (icmpprintfs) {
8152b758395SGarrett Wollman 		char buf[4 * sizeof "123"];
8162b758395SGarrett Wollman 		strcpy(buf, inet_ntoa(ip->ip_dst));
8172b758395SGarrett Wollman 		printf("icmp_send dst %s src %s\n",
8182b758395SGarrett Wollman 		       buf, inet_ntoa(ip->ip_src));
8192b758395SGarrett Wollman 	}
820df8bae1dSRodney W. Grimes #endif
82102c1c707SAndre Oppermann 	(void) ip_output(m, opts, NULL, 0, NULL, NULL);
822df8bae1dSRodney W. Grimes }
823df8bae1dSRodney W. Grimes 
824df8bae1dSRodney W. Grimes n_time
825df8bae1dSRodney W. Grimes iptime()
826df8bae1dSRodney W. Grimes {
827df8bae1dSRodney W. Grimes 	struct timeval atv;
828df8bae1dSRodney W. Grimes 	u_long t;
829df8bae1dSRodney W. Grimes 
83016cd6db0SBill Fumerola 	getmicrotime(&atv);
831df8bae1dSRodney W. Grimes 	t = (atv.tv_sec % (24*60*60)) * 1000 + atv.tv_usec / 1000;
832df8bae1dSRodney W. Grimes 	return (htonl(t));
833df8bae1dSRodney W. Grimes }
834df8bae1dSRodney W. Grimes 
8355cbf3e08SGarrett Wollman /*
8365cbf3e08SGarrett Wollman  * Return the next larger or smaller MTU plateau (table from RFC 1191)
8375cbf3e08SGarrett Wollman  * given current value MTU.  If DIR is less than zero, a larger plateau
8385cbf3e08SGarrett Wollman  * is returned; otherwise, a smaller value is returned.
8395cbf3e08SGarrett Wollman  */
8401aedbd9cSAndre Oppermann int
8415cbf3e08SGarrett Wollman ip_next_mtu(mtu, dir)
8425cbf3e08SGarrett Wollman 	int mtu;
8435cbf3e08SGarrett Wollman 	int dir;
8445cbf3e08SGarrett Wollman {
8455cbf3e08SGarrett Wollman 	static int mtutab[] = {
8464c037f8dSAndre Oppermann 		65535, 32000, 17914, 8166, 4352, 2002, 1492, 1280, 1006, 508,
8474c037f8dSAndre Oppermann 		296, 68, 0
8485cbf3e08SGarrett Wollman 	};
8495cbf3e08SGarrett Wollman 	int i;
8505cbf3e08SGarrett Wollman 
8515cbf3e08SGarrett Wollman 	for (i = 0; i < (sizeof mtutab) / (sizeof mtutab[0]); i++) {
8525cbf3e08SGarrett Wollman 		if (mtu >= mtutab[i])
8535cbf3e08SGarrett Wollman 			break;
8545cbf3e08SGarrett Wollman 	}
8555cbf3e08SGarrett Wollman 
8565cbf3e08SGarrett Wollman 	if (dir < 0) {
8575cbf3e08SGarrett Wollman 		if (i == 0) {
8585cbf3e08SGarrett Wollman 			return 0;
8595cbf3e08SGarrett Wollman 		} else {
8605cbf3e08SGarrett Wollman 			return mtutab[i - 1];
8615cbf3e08SGarrett Wollman 		}
8625cbf3e08SGarrett Wollman 	} else {
8635cbf3e08SGarrett Wollman 		if (mtutab[i] == 0) {
8645cbf3e08SGarrett Wollman 			return 0;
8655cbf3e08SGarrett Wollman 		} else if(mtu > mtutab[i]) {
8665cbf3e08SGarrett Wollman 			return mtutab[i];
8675cbf3e08SGarrett Wollman 		} else {
8685cbf3e08SGarrett Wollman 			return mtutab[i + 1];
8695cbf3e08SGarrett Wollman 		}
8705cbf3e08SGarrett Wollman 	}
8715cbf3e08SGarrett Wollman }
87251508de1SMatthew Dillon 
87351508de1SMatthew Dillon 
87451508de1SMatthew Dillon /*
87551508de1SMatthew Dillon  * badport_bandlim() - check for ICMP bandwidth limit
87651508de1SMatthew Dillon  *
87751508de1SMatthew Dillon  *	Return 0 if it is ok to send an ICMP error response, -1 if we have
87851508de1SMatthew Dillon  *	hit our bandwidth limit and it is not ok.
87951508de1SMatthew Dillon  *
88051508de1SMatthew Dillon  *	If icmplim is <= 0, the feature is disabled and 0 is returned.
88151508de1SMatthew Dillon  *
88251508de1SMatthew Dillon  *	For now we separate the TCP and UDP subsystems w/ different 'which'
88351508de1SMatthew Dillon  *	values.  We may eventually remove this separation (and simplify the
88451508de1SMatthew Dillon  *	code further).
88551508de1SMatthew Dillon  *
88651508de1SMatthew Dillon  *	Note that the printing of the error message is delayed so we can
88751508de1SMatthew Dillon  *	properly print the icmp error rate that the system was trying to do
88851508de1SMatthew Dillon  *	(i.e. 22000/100 pps, etc...).  This can cause long delays in printing
88951508de1SMatthew Dillon  *	the 'final' error, but it doesn't make sense to solve the printing
89051508de1SMatthew Dillon  *	delay with more complex code.
89151508de1SMatthew Dillon  */
89251508de1SMatthew Dillon 
89351508de1SMatthew Dillon int
89451508de1SMatthew Dillon badport_bandlim(int which)
89551508de1SMatthew Dillon {
89600f21882SSam Leffler #define	N(a)	(sizeof (a) / sizeof (a[0]))
89700f21882SSam Leffler 	static struct rate {
89800f21882SSam Leffler 		const char	*type;
89900f21882SSam Leffler 		struct timeval	lasttime;
900439dfb0cSStefan Farfeleder 		int		curpps;
90100f21882SSam Leffler 	} rates[BANDLIM_MAX+1] = {
90200f21882SSam Leffler 		{ "icmp unreach response" },
90300f21882SSam Leffler 		{ "icmp ping response" },
90400f21882SSam Leffler 		{ "icmp tstamp response" },
90500f21882SSam Leffler 		{ "closed port RST response" },
90600f21882SSam Leffler 		{ "open port RST response" }
90709f81a46SBosko Milekic 	};
90851508de1SMatthew Dillon 
90951508de1SMatthew Dillon 	/*
91000f21882SSam Leffler 	 * Return ok status if feature disabled or argument out of range.
91151508de1SMatthew Dillon 	 */
91200f21882SSam Leffler 	if (icmplim > 0 && (u_int) which < N(rates)) {
91300f21882SSam Leffler 		struct rate *r = &rates[which];
91400f21882SSam Leffler 		int opps = r->curpps;
91551508de1SMatthew Dillon 
91600f21882SSam Leffler 		if (!ppsratecheck(&r->lasttime, &r->curpps, icmplim))
91700f21882SSam Leffler 			return -1;	/* discard packet */
91851508de1SMatthew Dillon 		/*
91900f21882SSam Leffler 		 * If we've dropped below the threshold after having
92000f21882SSam Leffler 		 * rate-limited traffic print the message.  This preserves
92100f21882SSam Leffler 		 * the previous behaviour at the expense of added complexity.
92251508de1SMatthew Dillon 		 */
923069f35d3SSam Leffler 		if (icmplim_output && opps > icmplim)
92400f21882SSam Leffler 			printf("Limiting %s from %d to %d packets/sec\n",
925069f35d3SSam Leffler 				r->type, opps, icmplim);
92651508de1SMatthew Dillon 	}
92700f21882SSam Leffler 	return 0;			/* okay to send packet */
92800f21882SSam Leffler #undef N
92951508de1SMatthew Dillon }
930