xref: /freebsd/sys/netinet/ip_icmp.c (revision 9ac7c6cfed7262ebe46ad5b43978cc96ae333b8e)
1c398230bSWarner Losh /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1988, 1993
5df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
6df8bae1dSRodney W. Grimes  *
7df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
8df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
9df8bae1dSRodney W. Grimes  * are met:
10df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
12df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
13df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
14df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
16df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
17df8bae1dSRodney W. Grimes  *    without specific prior written permission.
18df8bae1dSRodney W. Grimes  *
19df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
30df8bae1dSRodney W. Grimes  *
31df8bae1dSRodney W. Grimes  *	@(#)ip_icmp.c	8.2 (Berkeley) 1/4/94
32df8bae1dSRodney W. Grimes  */
33df8bae1dSRodney W. Grimes 
344b421e2dSMike Silbersack #include <sys/cdefs.h>
354b421e2dSMike Silbersack __FBSDID("$FreeBSD$");
364b421e2dSMike Silbersack 
37b8e463e6SBjoern A. Zeeb #include "opt_inet.h"
386a800098SYoshinobu Inoue 
39df8bae1dSRodney W. Grimes #include <sys/param.h>
40df8bae1dSRodney W. Grimes #include <sys/systm.h>
41df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
42df8bae1dSRodney W. Grimes #include <sys/protosw.h>
43df8bae1dSRodney W. Grimes #include <sys/socket.h>
44df8bae1dSRodney W. Grimes #include <sys/time.h>
45df8bae1dSRodney W. Grimes #include <sys/kernel.h>
46cc0a3c8cSAndrey V. Elsukov #include <sys/lock.h>
47cc0a3c8cSAndrey V. Elsukov #include <sys/rmlock.h>
48b5e8ce9fSBruce Evans #include <sys/sysctl.h>
4940fe9effSAndre Oppermann #include <sys/syslog.h>
50df8bae1dSRodney W. Grimes 
51df8bae1dSRodney W. Grimes #include <net/if.h>
5276039bc8SGleb Smirnoff #include <net/if_var.h>
539494d596SBrooks Davis #include <net/if_types.h>
54df8bae1dSRodney W. Grimes #include <net/route.h>
55*9ac7c6cfSAlexander V. Chernikov #include <net/route/nhop.h>
56eddfbb76SRobert Watson #include <net/vnet.h>
57df8bae1dSRodney W. Grimes 
58df8bae1dSRodney W. Grimes #include <netinet/in.h>
599977be4aSAlexander V. Chernikov #include <netinet/in_fib.h>
6097d8d152SAndre Oppermann #include <netinet/in_pcb.h>
61df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
62df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
63df8bae1dSRodney W. Grimes #include <netinet/ip.h>
64df8bae1dSRodney W. Grimes #include <netinet/ip_icmp.h>
65b5e8ce9fSBruce Evans #include <netinet/ip_var.h>
66ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
67f88d0cfeSMichael Tuexen #include <netinet/sctp.h>
6897d8d152SAndre Oppermann #include <netinet/tcp.h>
6997d8d152SAndre Oppermann #include <netinet/tcp_var.h>
7097d8d152SAndre Oppermann #include <netinet/tcpip.h>
71df8bae1dSRodney W. Grimes #include <netinet/icmp_var.h>
72df8bae1dSRodney W. Grimes 
73d7c5a620SMatt Macy 
74b8e463e6SBjoern A. Zeeb #ifdef INET
75b9234fafSSam Leffler 
7672a52a35SJonathan Lemon #include <machine/in_cksum.h>
7772a52a35SJonathan Lemon 
78aed55708SRobert Watson #include <security/mac/mac_framework.h>
79b8e463e6SBjoern A. Zeeb #endif /* INET */
80aed55708SRobert Watson 
81df8bae1dSRodney W. Grimes /*
82df8bae1dSRodney W. Grimes  * ICMP routines: error generation, receive packet processing, and
83df8bae1dSRodney W. Grimes  * routines to turnaround packets back to the originator, and
84df8bae1dSRodney W. Grimes  * host table maintenance routines.
85df8bae1dSRodney W. Grimes  */
865f901c92SAndrew Turner VNET_DEFINE_STATIC(int, icmplim) = 200;
87b8e463e6SBjoern A. Zeeb #define	V_icmplim			VNET(icmplim)
886df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_icmp, ICMPCTL_ICMPLIM, icmplim, CTLFLAG_VNET | CTLFLAG_RW,
89b8e463e6SBjoern A. Zeeb 	&VNET_NAME(icmplim), 0,
90b8e463e6SBjoern A. Zeeb 	"Maximum number of ICMP responses per second");
91b8e463e6SBjoern A. Zeeb 
925f901c92SAndrew Turner VNET_DEFINE_STATIC(int, icmplim_output) = 1;
93b8e463e6SBjoern A. Zeeb #define	V_icmplim_output		VNET(icmplim_output)
946df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_icmp, OID_AUTO, icmplim_output, CTLFLAG_VNET | CTLFLAG_RW,
95b8e463e6SBjoern A. Zeeb 	&VNET_NAME(icmplim_output), 0,
96316efdb3SEitan Adler 	"Enable logging of ICMP response rate limiting");
97b8e463e6SBjoern A. Zeeb 
98b8e463e6SBjoern A. Zeeb #ifdef INET
995b7cb97cSAndrey V. Elsukov VNET_PCPUSTAT_DEFINE(struct icmpstat, icmpstat);
1005b7cb97cSAndrey V. Elsukov VNET_PCPUSTAT_SYSINIT(icmpstat);
1015b7cb97cSAndrey V. Elsukov SYSCTL_VNET_PCPUSTAT(_net_inet_icmp, ICMPCTL_STATS, stats, struct icmpstat,
1025b7cb97cSAndrey V. Elsukov     icmpstat, "ICMP statistics (struct icmpstat, netinet/icmp_var.h)");
1035b7cb97cSAndrey V. Elsukov 
1045b7cb97cSAndrey V. Elsukov #ifdef VIMAGE
1055b7cb97cSAndrey V. Elsukov VNET_PCPUSTAT_SYSUNINIT(icmpstat);
1065b7cb97cSAndrey V. Elsukov #endif /* VIMAGE */
107eddfbb76SRobert Watson 
1085f901c92SAndrew Turner VNET_DEFINE_STATIC(int, icmpmaskrepl) = 0;
10982cea7e6SBjoern A. Zeeb #define	V_icmpmaskrepl			VNET(icmpmaskrepl)
1106df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_icmp, ICMPCTL_MASKREPL, maskrepl, CTLFLAG_VNET | CTLFLAG_RW,
111eddfbb76SRobert Watson 	&VNET_NAME(icmpmaskrepl), 0,
112e8800f3cSMark Johnston 	"Reply to ICMP Address Mask Request packets");
11357842a38SMatthew N. Dodd 
1145f901c92SAndrew Turner VNET_DEFINE_STATIC(u_int, icmpmaskfake) = 0;
11582cea7e6SBjoern A. Zeeb #define	V_icmpmaskfake			VNET(icmpmaskfake)
1166df8a710SGleb Smirnoff SYSCTL_UINT(_net_inet_icmp, OID_AUTO, maskfake, CTLFLAG_VNET | CTLFLAG_RW,
117eddfbb76SRobert Watson 	&VNET_NAME(icmpmaskfake), 0,
118e8800f3cSMark Johnston 	"Fake reply to ICMP Address Mask Request packets");
1190312fbe9SPoul-Henning Kamp 
1203c2824b9SAlexander V. Chernikov VNET_DEFINE(int, drop_redirect) = 0;
121670e8b3bSAlexander V. Chernikov #define	V_drop_redirect			VNET(drop_redirect)
122670e8b3bSAlexander V. Chernikov SYSCTL_INT(_net_inet_icmp, OID_AUTO, drop_redirect, CTLFLAG_VNET | CTLFLAG_RW,
123e8800f3cSMark Johnston 	&VNET_NAME(drop_redirect), 0,
124e8800f3cSMark Johnston 	"Ignore ICMP redirects");
12518d3153eSDag-Erling Smørgrav 
1265f901c92SAndrew Turner VNET_DEFINE_STATIC(int, log_redirect) = 0;
12782cea7e6SBjoern A. Zeeb #define	V_log_redirect			VNET(log_redirect)
1286df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_icmp, OID_AUTO, log_redirect, CTLFLAG_VNET | CTLFLAG_RW,
129eddfbb76SRobert Watson 	&VNET_NAME(log_redirect), 0,
130eddfbb76SRobert Watson 	"Log ICMP redirects to the console");
1316c3b5f69SDag-Erling Smørgrav 
13234a5582cSAlexander V. Chernikov VNET_DEFINE_STATIC(int, redirtimeout) = 60 * 10; /* 10 minutes */
13334a5582cSAlexander V. Chernikov #define	V_redirtimeout			VNET(redirtimeout)
13434a5582cSAlexander V. Chernikov SYSCTL_INT(_net_inet_icmp, OID_AUTO, redirtimeout, CTLFLAG_VNET | CTLFLAG_RW,
13534a5582cSAlexander V. Chernikov 	&VNET_NAME(redirtimeout), 0,
13634a5582cSAlexander V. Chernikov 	"Delay in seconds before expiring redirect route");
13734a5582cSAlexander V. Chernikov 
1385f901c92SAndrew Turner VNET_DEFINE_STATIC(char, reply_src[IFNAMSIZ]);
13982cea7e6SBjoern A. Zeeb #define	V_reply_src			VNET(reply_src)
1406df8a710SGleb Smirnoff SYSCTL_STRING(_net_inet_icmp, OID_AUTO, reply_src, CTLFLAG_VNET | CTLFLAG_RW,
141eddfbb76SRobert Watson 	&VNET_NAME(reply_src), IFNAMSIZ,
142e8800f3cSMark Johnston 	"ICMP reply source for non-local packets");
143b74d89bbSAndre Oppermann 
1445f901c92SAndrew Turner VNET_DEFINE_STATIC(int, icmp_rfi) = 0;
14582cea7e6SBjoern A. Zeeb #define	V_icmp_rfi			VNET(icmp_rfi)
1466df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_icmp, OID_AUTO, reply_from_interface, CTLFLAG_VNET | CTLFLAG_RW,
147eddfbb76SRobert Watson 	&VNET_NAME(icmp_rfi), 0,
148eddfbb76SRobert Watson 	"ICMP reply from incoming interface for non-local packets");
1498de9ac5eSRandall Stewart /* Router requirements RFC 1812 section 4.3.2.3 requires 576 - 28. */
1501e0582fdSAndrew Turner VNET_DEFINE_STATIC(int, icmp_quotelen) = 548;
15182cea7e6SBjoern A. Zeeb #define	V_icmp_quotelen			VNET(icmp_quotelen)
1526df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_icmp, OID_AUTO, quotelen, CTLFLAG_VNET | CTLFLAG_RW,
153eddfbb76SRobert Watson 	&VNET_NAME(icmp_quotelen), 0,
154eddfbb76SRobert Watson 	"Number of bytes from original packet to quote in ICMP reply");
155e875dfb8SAndre Oppermann 
1565f901c92SAndrew Turner VNET_DEFINE_STATIC(int, icmpbmcastecho) = 0;
15782cea7e6SBjoern A. Zeeb #define	V_icmpbmcastecho		VNET(icmpbmcastecho)
1586df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_icmp, OID_AUTO, bmcastecho, CTLFLAG_VNET | CTLFLAG_RW,
159eddfbb76SRobert Watson 	&VNET_NAME(icmpbmcastecho), 0,
160e8800f3cSMark Johnston 	"Reply to multicast ICMP Echo Request and Timestamp packets");
1617022ea0aSGarrett Wollman 
1625f901c92SAndrew Turner VNET_DEFINE_STATIC(int, icmptstamprepl) = 1;
16300cb6befSMark Johnston #define	V_icmptstamprepl		VNET(icmptstamprepl)
1647762bbc3SMark Johnston SYSCTL_INT(_net_inet_icmp, OID_AUTO, tstamprepl, CTLFLAG_VNET | CTLFLAG_RW,
165e8800f3cSMark Johnston 	&VNET_NAME(icmptstamprepl), 0,
166e8800f3cSMark Johnston 	"Respond to ICMP Timestamp packets");
16751508de1SMatthew Dillon 
168410634efSEugene Grosbein VNET_DEFINE_STATIC(int, error_keeptags) = 0;
169410634efSEugene Grosbein #define	V_error_keeptags		VNET(error_keeptags)
170410634efSEugene Grosbein SYSCTL_INT(_net_inet_icmp, OID_AUTO, error_keeptags, CTLFLAG_VNET | CTLFLAG_RW,
171410634efSEugene Grosbein 	&VNET_NAME(error_keeptags), 0,
172410634efSEugene Grosbein 	"ICMP error response keeps copy of mbuf_tags of original packet");
173410634efSEugene Grosbein 
174df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS
175df8bae1dSRodney W. Grimes int	icmpprintfs = 0;
176df8bae1dSRodney W. Grimes #endif
177df8bae1dSRodney W. Grimes 
1784d77a549SAlfred Perlstein static void	icmp_reflect(struct mbuf *);
17902c1c707SAndre Oppermann static void	icmp_send(struct mbuf *, struct mbuf *);
18034a5582cSAlexander V. Chernikov static int	icmp_verify_redirect_gateway(struct sockaddr_in *,
18134a5582cSAlexander V. Chernikov     struct sockaddr_in *, struct sockaddr_in *, u_int);
1820312fbe9SPoul-Henning Kamp 
183df8bae1dSRodney W. Grimes extern	struct protosw inetsw[];
184df8bae1dSRodney W. Grimes 
185df8bae1dSRodney W. Grimes /*
186315e3e38SRobert Watson  * Kernel module interface for updating icmpstat.  The argument is an index
187315e3e38SRobert Watson  * into icmpstat treated as an array of u_long.  While this encodes the
188315e3e38SRobert Watson  * general layout of icmpstat into the caller, it doesn't encode its
189315e3e38SRobert Watson  * location, so that future changes to add, for example, per-CPU stats
190315e3e38SRobert Watson  * support won't cause binary compatibility problems for kernel modules.
191315e3e38SRobert Watson  */
192315e3e38SRobert Watson void
193315e3e38SRobert Watson kmod_icmpstat_inc(int statnum)
194315e3e38SRobert Watson {
195315e3e38SRobert Watson 
1965b7cb97cSAndrey V. Elsukov 	counter_u64_add(VNET(icmpstat)[statnum], 1);
197315e3e38SRobert Watson }
198315e3e38SRobert Watson 
199315e3e38SRobert Watson /*
200df8bae1dSRodney W. Grimes  * Generate an error packet of type error
201df8bae1dSRodney W. Grimes  * in response to bad packet ip.
202df8bae1dSRodney W. Grimes  */
203df8bae1dSRodney W. Grimes void
204d685b6eeSLuigi Rizzo icmp_error(struct mbuf *n, int type, int code, uint32_t dest, int mtu)
205df8bae1dSRodney W. Grimes {
206f415d666SAndrey V. Elsukov 	struct ip *oip, *nip;
2073e85b721SEd Maste 	struct icmp *icp;
2083e85b721SEd Maste 	struct mbuf *m;
209f415d666SAndrey V. Elsukov 	unsigned icmplen, icmpelen, nlen, oiphlen;
210df8bae1dSRodney W. Grimes 
211f415d666SAndrey V. Elsukov 	KASSERT((u_int)type <= ICMP_MAXTYPE, ("%s: illegal ICMP type",
212f415d666SAndrey V. Elsukov 	    __func__));
213f415d666SAndrey V. Elsukov 
214df8bae1dSRodney W. Grimes 	if (type != ICMP_REDIRECT)
215e27b0c87SRobert Watson 		ICMPSTAT_INC(icps_error);
216df8bae1dSRodney W. Grimes 	/*
217e86ebebcSAndre Oppermann 	 * Don't send error:
218e86ebebcSAndre Oppermann 	 *  if the original packet was encrypted.
219e86ebebcSAndre Oppermann 	 *  if not the first fragment of message.
220e86ebebcSAndre Oppermann 	 *  in response to a multicast or broadcast packet.
221e86ebebcSAndre Oppermann 	 *  if the old packet protocol was an ICMP error message.
222df8bae1dSRodney W. Grimes 	 */
223cad1917dSHajimu UMEMOTO 	if (n->m_flags & M_DECRYPTED)
224cad1917dSHajimu UMEMOTO 		goto freeit;
225e86ebebcSAndre Oppermann 	if (n->m_flags & (M_BCAST|M_MCAST))
226e86ebebcSAndre Oppermann 		goto freeit;
227f415d666SAndrey V. Elsukov 
228f415d666SAndrey V. Elsukov 	/* Drop if IP header plus 8 bytes is not contiguous in first mbuf. */
229f415d666SAndrey V. Elsukov 	if (n->m_len < sizeof(struct ip) + ICMP_MINLEN)
230f415d666SAndrey V. Elsukov 		goto freeit;
231f415d666SAndrey V. Elsukov 	oip = mtod(n, struct ip *);
232f415d666SAndrey V. Elsukov 	oiphlen = oip->ip_hl << 2;
233f415d666SAndrey V. Elsukov 	if (n->m_len < oiphlen + ICMP_MINLEN)
234f415d666SAndrey V. Elsukov 		goto freeit;
235f415d666SAndrey V. Elsukov #ifdef ICMPPRINTFS
236f415d666SAndrey V. Elsukov 	if (icmpprintfs)
237f415d666SAndrey V. Elsukov 		printf("icmp_error(%p, %x, %d)\n", oip, type, code);
238f415d666SAndrey V. Elsukov #endif
239f415d666SAndrey V. Elsukov 	if (oip->ip_off & htons(~(IP_MF|IP_DF)))
240f415d666SAndrey V. Elsukov 		goto freeit;
241df8bae1dSRodney W. Grimes 	if (oip->ip_p == IPPROTO_ICMP && type != ICMP_REDIRECT &&
242f415d666SAndrey V. Elsukov 	    !ICMP_INFOTYPE(((struct icmp *)((caddr_t)oip +
243f415d666SAndrey V. Elsukov 		oiphlen))->icmp_type)) {
244e27b0c87SRobert Watson 		ICMPSTAT_INC(icps_oldicmp);
245df8bae1dSRodney W. Grimes 		goto freeit;
246df8bae1dSRodney W. Grimes 	}
247df8bae1dSRodney W. Grimes 	/*
248e86ebebcSAndre Oppermann 	 * Calculate length to quote from original packet and
249e86ebebcSAndre Oppermann 	 * prevent the ICMP mbuf from overflowing.
250a4641f4eSPedro F. Giffuni 	 * Unfortunately this is non-trivial since ip_forward()
251e86ebebcSAndre Oppermann 	 * sends us truncated packets.
252df8bae1dSRodney W. Grimes 	 */
253e86ebebcSAndre Oppermann 	nlen = m_length(n, NULL);
254e86ebebcSAndre Oppermann 	if (oip->ip_p == IPPROTO_TCP) {
255e86ebebcSAndre Oppermann 		struct tcphdr *th;
256e86ebebcSAndre Oppermann 		int tcphlen;
257e86ebebcSAndre Oppermann 
258e86ebebcSAndre Oppermann 		if (oiphlen + sizeof(struct tcphdr) > n->m_len &&
259e86ebebcSAndre Oppermann 		    n->m_next == NULL)
260e86ebebcSAndre Oppermann 			goto stdreply;
261e86ebebcSAndre Oppermann 		if (n->m_len < oiphlen + sizeof(struct tcphdr) &&
262f415d666SAndrey V. Elsukov 		    (n = m_pullup(n, oiphlen + sizeof(struct tcphdr))) == NULL)
263e86ebebcSAndre Oppermann 			goto freeit;
264f415d666SAndrey V. Elsukov 		oip = mtod(n, struct ip *);
265f415d666SAndrey V. Elsukov 		th = mtodo(n, oiphlen);
266e86ebebcSAndre Oppermann 		tcphlen = th->th_off << 2;
267e86ebebcSAndre Oppermann 		if (tcphlen < sizeof(struct tcphdr))
268e86ebebcSAndre Oppermann 			goto freeit;
2698f134647SGleb Smirnoff 		if (ntohs(oip->ip_len) < oiphlen + tcphlen)
270e86ebebcSAndre Oppermann 			goto freeit;
271e86ebebcSAndre Oppermann 		if (oiphlen + tcphlen > n->m_len && n->m_next == NULL)
272e86ebebcSAndre Oppermann 			goto stdreply;
273e86ebebcSAndre Oppermann 		if (n->m_len < oiphlen + tcphlen &&
274f415d666SAndrey V. Elsukov 		    (n = m_pullup(n, oiphlen + tcphlen)) == NULL)
275e86ebebcSAndre Oppermann 			goto freeit;
27654e67534SJonathan T. Looney 		oip = mtod(n, struct ip *);
2778f134647SGleb Smirnoff 		icmpelen = max(tcphlen, min(V_icmp_quotelen,
2788f134647SGleb Smirnoff 		    ntohs(oip->ip_len) - oiphlen));
279f88d0cfeSMichael Tuexen 	} else if (oip->ip_p == IPPROTO_SCTP) {
280f88d0cfeSMichael Tuexen 		struct sctphdr *sh;
281f88d0cfeSMichael Tuexen 		struct sctp_chunkhdr *ch;
282f88d0cfeSMichael Tuexen 
283f88d0cfeSMichael Tuexen 		if (ntohs(oip->ip_len) < oiphlen + sizeof(struct sctphdr))
284f88d0cfeSMichael Tuexen 			goto stdreply;
285f88d0cfeSMichael Tuexen 		if (oiphlen + sizeof(struct sctphdr) > n->m_len &&
286f88d0cfeSMichael Tuexen 		    n->m_next == NULL)
287f88d0cfeSMichael Tuexen 			goto stdreply;
288f88d0cfeSMichael Tuexen 		if (n->m_len < oiphlen + sizeof(struct sctphdr) &&
289f88d0cfeSMichael Tuexen 		    (n = m_pullup(n, oiphlen + sizeof(struct sctphdr))) == NULL)
290f88d0cfeSMichael Tuexen 			goto freeit;
291f415d666SAndrey V. Elsukov 		oip = mtod(n, struct ip *);
292f88d0cfeSMichael Tuexen 		icmpelen = max(sizeof(struct sctphdr),
293f88d0cfeSMichael Tuexen 		    min(V_icmp_quotelen, ntohs(oip->ip_len) - oiphlen));
294f415d666SAndrey V. Elsukov 		sh = mtodo(n, oiphlen);
295f88d0cfeSMichael Tuexen 		if (ntohl(sh->v_tag) == 0 &&
296f415d666SAndrey V. Elsukov 		    ntohs(oip->ip_len) >= oiphlen +
297f415d666SAndrey V. Elsukov 		    sizeof(struct sctphdr) + 8 &&
298f88d0cfeSMichael Tuexen 		    (n->m_len >= oiphlen + sizeof(struct sctphdr) + 8 ||
299f88d0cfeSMichael Tuexen 		     n->m_next != NULL)) {
300f88d0cfeSMichael Tuexen 			if (n->m_len < oiphlen + sizeof(struct sctphdr) + 8 &&
301f415d666SAndrey V. Elsukov 			    (n = m_pullup(n, oiphlen +
302f415d666SAndrey V. Elsukov 			    sizeof(struct sctphdr) + 8)) == NULL)
303f88d0cfeSMichael Tuexen 				goto freeit;
304f415d666SAndrey V. Elsukov 			oip = mtod(n, struct ip *);
305f415d666SAndrey V. Elsukov 			sh = mtodo(n, oiphlen);
306f88d0cfeSMichael Tuexen 			ch = (struct sctp_chunkhdr *)(sh + 1);
307f88d0cfeSMichael Tuexen 			if (ch->chunk_type == SCTP_INITIATION) {
308f88d0cfeSMichael Tuexen 				icmpelen = max(sizeof(struct sctphdr) + 8,
309f415d666SAndrey V. Elsukov 				    min(V_icmp_quotelen, ntohs(oip->ip_len) -
310f415d666SAndrey V. Elsukov 				    oiphlen));
311f88d0cfeSMichael Tuexen 			}
312f88d0cfeSMichael Tuexen 		}
313e86ebebcSAndre Oppermann 	} else
314f415d666SAndrey V. Elsukov stdreply:	icmpelen = max(8, min(V_icmp_quotelen, ntohs(oip->ip_len) -
315f415d666SAndrey V. Elsukov 		    oiphlen));
316e86ebebcSAndre Oppermann 
317e86ebebcSAndre Oppermann 	icmplen = min(oiphlen + icmpelen, nlen);
318e86ebebcSAndre Oppermann 	if (icmplen < sizeof(struct ip))
319e86ebebcSAndre Oppermann 		goto freeit;
320e86ebebcSAndre Oppermann 
321e86ebebcSAndre Oppermann 	if (MHLEN > sizeof(struct ip) + ICMP_MINLEN + icmplen)
322eb1b1807SGleb Smirnoff 		m = m_gethdr(M_NOWAIT, MT_DATA);
323e86ebebcSAndre Oppermann 	else
324eb1b1807SGleb Smirnoff 		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
325df8bae1dSRodney W. Grimes 	if (m == NULL)
326df8bae1dSRodney W. Grimes 		goto freeit;
3270070e096SRobert Watson #ifdef MAC
328a13e21f7SRobert Watson 	mac_netinet_icmp_reply(n, m);
3290070e096SRobert Watson #endif
330f415d666SAndrey V. Elsukov 	icmplen = min(icmplen, M_TRAILINGSPACE(m) -
331f415d666SAndrey V. Elsukov 	    sizeof(struct ip) - ICMP_MINLEN);
3322bfaf585SEd Maste 	m_align(m, sizeof(struct ip) + ICMP_MINLEN + icmplen);
3332bfaf585SEd Maste 	m->m_data += sizeof(struct ip);
334e86ebebcSAndre Oppermann 	m->m_len = ICMP_MINLEN + icmplen;
3356b773dffSAndre Oppermann 
3368b07e49aSJulian Elischer 	/* XXX MRT  make the outgoing packet use the same FIB
3378b07e49aSJulian Elischer 	 * that was associated with the incoming packet
3388b07e49aSJulian Elischer 	 */
3398b07e49aSJulian Elischer 	M_SETFIB(m, M_GETFIB(n));
340df8bae1dSRodney W. Grimes 	icp = mtod(m, struct icmp *);
341e27b0c87SRobert Watson 	ICMPSTAT_INC(icps_outhist[type]);
342df8bae1dSRodney W. Grimes 	icp->icmp_type = type;
343df8bae1dSRodney W. Grimes 	if (type == ICMP_REDIRECT)
344df8bae1dSRodney W. Grimes 		icp->icmp_gwaddr.s_addr = dest;
345df8bae1dSRodney W. Grimes 	else {
346df8bae1dSRodney W. Grimes 		icp->icmp_void = 0;
347df8bae1dSRodney W. Grimes 		/*
348df8bae1dSRodney W. Grimes 		 * The following assignments assume an overlay with the
349e86ebebcSAndre Oppermann 		 * just zeroed icmp_void field.
350df8bae1dSRodney W. Grimes 		 */
351df8bae1dSRodney W. Grimes 		if (type == ICMP_PARAMPROB) {
352df8bae1dSRodney W. Grimes 			icp->icmp_pptr = code;
353df8bae1dSRodney W. Grimes 			code = 0;
354df8bae1dSRodney W. Grimes 		} else if (type == ICMP_UNREACH &&
355c773494eSAndre Oppermann 			code == ICMP_UNREACH_NEEDFRAG && mtu) {
356c773494eSAndre Oppermann 			icp->icmp_nextmtu = htons(mtu);
357df8bae1dSRodney W. Grimes 		}
358df8bae1dSRodney W. Grimes 	}
359df8bae1dSRodney W. Grimes 	icp->icmp_code = code;
36004287599SRuslan Ermilov 
36104287599SRuslan Ermilov 	/*
362e86ebebcSAndre Oppermann 	 * Copy the quotation into ICMP message and
363e86ebebcSAndre Oppermann 	 * convert quoted IP header back to network representation.
36404287599SRuslan Ermilov 	 */
365e86ebebcSAndre Oppermann 	m_copydata(n, 0, icmplen, (caddr_t)&icp->icmp_ip);
366e86ebebcSAndre Oppermann 	nip = &icp->icmp_ip;
367df8bae1dSRodney W. Grimes 
368df8bae1dSRodney W. Grimes 	/*
369e86ebebcSAndre Oppermann 	 * Set up ICMP message mbuf and copy old IP header (without options
370e86ebebcSAndre Oppermann 	 * in front of ICMP message.
371c550f220SMax Laier 	 * If the original mbuf was meant to bypass the firewall, the error
372c550f220SMax Laier 	 * reply should bypass as well.
373c550f220SMax Laier 	 */
374c550f220SMax Laier 	m->m_flags |= n->m_flags & M_SKIP_FIREWALL;
3752bfaf585SEd Maste 	KASSERT(M_LEADINGSPACE(m) >= sizeof(struct ip),
3762bfaf585SEd Maste 	    ("insufficient space for ip header"));
377df8bae1dSRodney W. Grimes 	m->m_data -= sizeof(struct ip);
378df8bae1dSRodney W. Grimes 	m->m_len += sizeof(struct ip);
379df8bae1dSRodney W. Grimes 	m->m_pkthdr.len = m->m_len;
380df8bae1dSRodney W. Grimes 	m->m_pkthdr.rcvif = n->m_pkthdr.rcvif;
381df8bae1dSRodney W. Grimes 	nip = mtod(m, struct ip *);
382df8bae1dSRodney W. Grimes 	bcopy((caddr_t)oip, (caddr_t)nip, sizeof(struct ip));
3838f134647SGleb Smirnoff 	nip->ip_len = htons(m->m_len);
38453be11f6SPoul-Henning Kamp 	nip->ip_v = IPVERSION;
38553be11f6SPoul-Henning Kamp 	nip->ip_hl = 5;
386df8bae1dSRodney W. Grimes 	nip->ip_p = IPPROTO_ICMP;
387df8bae1dSRodney W. Grimes 	nip->ip_tos = 0;
38841ea685cSAndrey V. Elsukov 	nip->ip_off = 0;
389410634efSEugene Grosbein 
390410634efSEugene Grosbein 	if (V_error_keeptags)
391410634efSEugene Grosbein 		m_tag_copy_chain(m, n, M_NOWAIT);
392410634efSEugene Grosbein 
393df8bae1dSRodney W. Grimes 	icmp_reflect(m);
394df8bae1dSRodney W. Grimes 
395df8bae1dSRodney W. Grimes freeit:
396df8bae1dSRodney W. Grimes 	m_freem(n);
397df8bae1dSRodney W. Grimes }
398df8bae1dSRodney W. Grimes 
399df8bae1dSRodney W. Grimes /*
400df8bae1dSRodney W. Grimes  * Process a received ICMP message.
401df8bae1dSRodney W. Grimes  */
4028f5a8818SKevin Lo int
4038f5a8818SKevin Lo icmp_input(struct mbuf **mp, int *offp, int proto)
404df8bae1dSRodney W. Grimes {
40516d6c90fSAndre Oppermann 	struct icmp *icp;
406df8bae1dSRodney W. Grimes 	struct in_ifaddr *ia;
4078f5a8818SKevin Lo 	struct mbuf *m = *mp;
40816d6c90fSAndre Oppermann 	struct ip *ip = mtod(m, struct ip *);
40916d6c90fSAndre Oppermann 	struct sockaddr_in icmpsrc, icmpdst, icmpgw;
4108f5a8818SKevin Lo 	int hlen = *offp;
4118f5a8818SKevin Lo 	int icmplen = ntohs(ip->ip_len) - *offp;
41216d6c90fSAndre Oppermann 	int i, code;
4134d77a549SAlfred Perlstein 	void (*ctlfunc)(int, struct sockaddr *, void *);
4148b07e49aSJulian Elischer 	int fibnum;
415df8bae1dSRodney W. Grimes 
416b8a6e03fSGleb Smirnoff 	NET_EPOCH_ASSERT();
417b8a6e03fSGleb Smirnoff 
4188f5a8818SKevin Lo 	*mp = NULL;
4198f5a8818SKevin Lo 
420df8bae1dSRodney W. Grimes 	/*
421df8bae1dSRodney W. Grimes 	 * Locate icmp structure in mbuf, and check
422df8bae1dSRodney W. Grimes 	 * that not corrupted and of at least minimum length.
423df8bae1dSRodney W. Grimes 	 */
424df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS
4252b758395SGarrett Wollman 	if (icmpprintfs) {
4268144690aSEric van Gyzen 		char srcbuf[INET_ADDRSTRLEN];
4278144690aSEric van Gyzen 		char dstbuf[INET_ADDRSTRLEN];
4288144690aSEric van Gyzen 
4292b758395SGarrett Wollman 		printf("icmp_input from %s to %s, len %d\n",
4308144690aSEric van Gyzen 		    inet_ntoa_r(ip->ip_src, srcbuf),
4318144690aSEric van Gyzen 		    inet_ntoa_r(ip->ip_dst, dstbuf), icmplen);
4322b758395SGarrett Wollman 	}
433df8bae1dSRodney W. Grimes #endif
434df8bae1dSRodney W. Grimes 	if (icmplen < ICMP_MINLEN) {
435e27b0c87SRobert Watson 		ICMPSTAT_INC(icps_tooshort);
436df8bae1dSRodney W. Grimes 		goto freeit;
437df8bae1dSRodney W. Grimes 	}
438df8bae1dSRodney W. Grimes 	i = hlen + min(icmplen, ICMP_ADVLENMIN);
439852da713SBjoern A. Zeeb 	if (m->m_len < i && (m = m_pullup(m, i)) == NULL)  {
440e27b0c87SRobert Watson 		ICMPSTAT_INC(icps_tooshort);
4418f5a8818SKevin Lo 		return (IPPROTO_DONE);
442df8bae1dSRodney W. Grimes 	}
443df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
444df8bae1dSRodney W. Grimes 	m->m_len -= hlen;
445df8bae1dSRodney W. Grimes 	m->m_data += hlen;
446df8bae1dSRodney W. Grimes 	icp = mtod(m, struct icmp *);
447df8bae1dSRodney W. Grimes 	if (in_cksum(m, icmplen)) {
448e27b0c87SRobert Watson 		ICMPSTAT_INC(icps_checksum);
449df8bae1dSRodney W. Grimes 		goto freeit;
450df8bae1dSRodney W. Grimes 	}
451df8bae1dSRodney W. Grimes 	m->m_len += hlen;
452df8bae1dSRodney W. Grimes 	m->m_data -= hlen;
453df8bae1dSRodney W. Grimes 
454df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS
455df8bae1dSRodney W. Grimes 	if (icmpprintfs)
456df8bae1dSRodney W. Grimes 		printf("icmp_input, type %d code %d\n", icp->icmp_type,
457df8bae1dSRodney W. Grimes 		    icp->icmp_code);
458df8bae1dSRodney W. Grimes #endif
4595b7ee6edSGarrett Wollman 
4605b7ee6edSGarrett Wollman 	/*
4615b7ee6edSGarrett Wollman 	 * Message type specific processing.
4625b7ee6edSGarrett Wollman 	 */
463df8bae1dSRodney W. Grimes 	if (icp->icmp_type > ICMP_MAXTYPE)
464df8bae1dSRodney W. Grimes 		goto raw;
46516d6c90fSAndre Oppermann 
46616d6c90fSAndre Oppermann 	/* Initialize */
46716d6c90fSAndre Oppermann 	bzero(&icmpsrc, sizeof(icmpsrc));
46816d6c90fSAndre Oppermann 	icmpsrc.sin_len = sizeof(struct sockaddr_in);
46916d6c90fSAndre Oppermann 	icmpsrc.sin_family = AF_INET;
47016d6c90fSAndre Oppermann 	bzero(&icmpdst, sizeof(icmpdst));
47116d6c90fSAndre Oppermann 	icmpdst.sin_len = sizeof(struct sockaddr_in);
47216d6c90fSAndre Oppermann 	icmpdst.sin_family = AF_INET;
47316d6c90fSAndre Oppermann 	bzero(&icmpgw, sizeof(icmpgw));
47416d6c90fSAndre Oppermann 	icmpgw.sin_len = sizeof(struct sockaddr_in);
47516d6c90fSAndre Oppermann 	icmpgw.sin_family = AF_INET;
47616d6c90fSAndre Oppermann 
477e27b0c87SRobert Watson 	ICMPSTAT_INC(icps_inhist[icp->icmp_type]);
478df8bae1dSRodney W. Grimes 	code = icp->icmp_code;
479df8bae1dSRodney W. Grimes 	switch (icp->icmp_type) {
480df8bae1dSRodney W. Grimes 
481df8bae1dSRodney W. Grimes 	case ICMP_UNREACH:
482df8bae1dSRodney W. Grimes 		switch (code) {
483df8bae1dSRodney W. Grimes 			case ICMP_UNREACH_NET:
484df8bae1dSRodney W. Grimes 			case ICMP_UNREACH_HOST:
485df8bae1dSRodney W. Grimes 			case ICMP_UNREACH_SRCFAIL:
486e4bb5b05SJonathan Lemon 			case ICMP_UNREACH_NET_UNKNOWN:
487e4bb5b05SJonathan Lemon 			case ICMP_UNREACH_HOST_UNKNOWN:
488e4bb5b05SJonathan Lemon 			case ICMP_UNREACH_ISOLATED:
489e4bb5b05SJonathan Lemon 			case ICMP_UNREACH_TOSNET:
490e4bb5b05SJonathan Lemon 			case ICMP_UNREACH_TOSHOST:
491e4bb5b05SJonathan Lemon 			case ICMP_UNREACH_HOST_PRECEDENCE:
492e4bb5b05SJonathan Lemon 			case ICMP_UNREACH_PRECEDENCE_CUTOFF:
493e4bb5b05SJonathan Lemon 				code = PRC_UNREACH_NET;
494df8bae1dSRodney W. Grimes 				break;
495df8bae1dSRodney W. Grimes 
496df8bae1dSRodney W. Grimes 			case ICMP_UNREACH_NEEDFRAG:
497df8bae1dSRodney W. Grimes 				code = PRC_MSGSIZE;
498df8bae1dSRodney W. Grimes 				break;
499df8bae1dSRodney W. Grimes 
500e4bb5b05SJonathan Lemon 			/*
501e4bb5b05SJonathan Lemon 			 * RFC 1122, Sections 3.2.2.1 and 4.2.3.9.
502e4bb5b05SJonathan Lemon 			 * Treat subcodes 2,3 as immediate RST
503e4bb5b05SJonathan Lemon 			 */
504e4bb5b05SJonathan Lemon 			case ICMP_UNREACH_PROTOCOL:
5053e146575SMichael Tuexen 				code = PRC_UNREACH_PROTOCOL;
5063e146575SMichael Tuexen 				break;
507e4bb5b05SJonathan Lemon 			case ICMP_UNREACH_PORT:
508b77d155dSJesper Skriver 				code = PRC_UNREACH_PORT;
509b11d7a4aSPoul-Henning Kamp 				break;
51090fcbbd6SPoul-Henning Kamp 
51190fcbbd6SPoul-Henning Kamp 			case ICMP_UNREACH_NET_PROHIB:
51290fcbbd6SPoul-Henning Kamp 			case ICMP_UNREACH_HOST_PROHIB:
5139c4b2574SPaul Traina 			case ICMP_UNREACH_FILTER_PROHIB:
51490fcbbd6SPoul-Henning Kamp 				code = PRC_UNREACH_ADMIN_PROHIB;
515b11d7a4aSPoul-Henning Kamp 				break;
516b11d7a4aSPoul-Henning Kamp 
517df8bae1dSRodney W. Grimes 			default:
518df8bae1dSRodney W. Grimes 				goto badcode;
519df8bae1dSRodney W. Grimes 		}
520df8bae1dSRodney W. Grimes 		goto deliver;
521df8bae1dSRodney W. Grimes 
522df8bae1dSRodney W. Grimes 	case ICMP_TIMXCEED:
523df8bae1dSRodney W. Grimes 		if (code > 1)
524df8bae1dSRodney W. Grimes 			goto badcode;
525df8bae1dSRodney W. Grimes 		code += PRC_TIMXCEED_INTRANS;
526df8bae1dSRodney W. Grimes 		goto deliver;
527df8bae1dSRodney W. Grimes 
528df8bae1dSRodney W. Grimes 	case ICMP_PARAMPROB:
529df8bae1dSRodney W. Grimes 		if (code > 1)
530df8bae1dSRodney W. Grimes 			goto badcode;
531df8bae1dSRodney W. Grimes 		code = PRC_PARAMPROB;
532df8bae1dSRodney W. Grimes 	deliver:
533df8bae1dSRodney W. Grimes 		/*
534df8bae1dSRodney W. Grimes 		 * Problem with datagram; advise higher level routines.
535df8bae1dSRodney W. Grimes 		 */
536df8bae1dSRodney W. Grimes 		if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) ||
53753be11f6SPoul-Henning Kamp 		    icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) {
538e27b0c87SRobert Watson 			ICMPSTAT_INC(icps_badlen);
539df8bae1dSRodney W. Grimes 			goto freeit;
540df8bae1dSRodney W. Grimes 		}
5415b7ee6edSGarrett Wollman 		/* Discard ICMP's in response to multicast packets */
5425b7ee6edSGarrett Wollman 		if (IN_MULTICAST(ntohl(icp->icmp_ip.ip_dst.s_addr)))
5435b7ee6edSGarrett Wollman 			goto badcode;
544df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS
545df8bae1dSRodney W. Grimes 		if (icmpprintfs)
546df8bae1dSRodney W. Grimes 			printf("deliver to protocol %d\n", icp->icmp_ip.ip_p);
547df8bae1dSRodney W. Grimes #endif
548df8bae1dSRodney W. Grimes 		icmpsrc.sin_addr = icp->icmp_ip.ip_dst;
5496a800098SYoshinobu Inoue 		/*
5506a800098SYoshinobu Inoue 		 * XXX if the packet contains [IPv4 AH TCP], we can't make a
5516a800098SYoshinobu Inoue 		 * notification to TCP layer.
5526a800098SYoshinobu Inoue 		 */
553f77b8427SMichael Tuexen 		i = sizeof(struct ip) + min(icmplen, ICMP_ADVLENPREF(icp));
554f77b8427SMichael Tuexen 		ip_stripoptions(m);
555f77b8427SMichael Tuexen 		if (m->m_len < i && (m = m_pullup(m, i)) == NULL) {
556f77b8427SMichael Tuexen 			/* This should actually not happen */
557f77b8427SMichael Tuexen 			ICMPSTAT_INC(icps_tooshort);
558f77b8427SMichael Tuexen 			return (IPPROTO_DONE);
559f77b8427SMichael Tuexen 		}
560f77b8427SMichael Tuexen 		ip = mtod(m, struct ip *);
561f77b8427SMichael Tuexen 		icp = (struct icmp *)(ip + 1);
562f77b8427SMichael Tuexen 		/*
563f77b8427SMichael Tuexen 		 * The upper layer handler can rely on:
564f77b8427SMichael Tuexen 		 * - The outer IP header has no options.
565f77b8427SMichael Tuexen 		 * - The outer IP header, the ICMP header, the inner IP header,
566f77b8427SMichael Tuexen 		 *   and the first n bytes of the inner payload are contiguous.
567f77b8427SMichael Tuexen 		 *   n is at least 8, but might be larger based on
568f77b8427SMichael Tuexen 		 *   ICMP_ADVLENPREF. See its definition in ip_icmp.h.
569f77b8427SMichael Tuexen 		 */
570623ae52eSPoul-Henning Kamp 		ctlfunc = inetsw[ip_protox[icp->icmp_ip.ip_p]].pr_ctlinput;
571623ae52eSPoul-Henning Kamp 		if (ctlfunc)
572df8bae1dSRodney W. Grimes 			(*ctlfunc)(code, (struct sockaddr *)&icmpsrc,
573b62d102cSBruce Evans 				   (void *)&icp->icmp_ip);
574df8bae1dSRodney W. Grimes 		break;
575df8bae1dSRodney W. Grimes 
576df8bae1dSRodney W. Grimes 	badcode:
577e27b0c87SRobert Watson 		ICMPSTAT_INC(icps_badcode);
578df8bae1dSRodney W. Grimes 		break;
579df8bae1dSRodney W. Grimes 
580df8bae1dSRodney W. Grimes 	case ICMP_ECHO:
5818b615593SMarko Zec 		if (!V_icmpbmcastecho
582d311884fSDavid Greenman 		    && (m->m_flags & (M_MCAST | M_BCAST)) != 0) {
583e27b0c87SRobert Watson 			ICMPSTAT_INC(icps_bmcastecho);
5847022ea0aSGarrett Wollman 			break;
5857022ea0aSGarrett Wollman 		}
586a57815efSBosko Milekic 		if (badport_bandlim(BANDLIM_ICMP_ECHO) < 0)
58709f81a46SBosko Milekic 			goto freeit;
588382a6bbcSJonathan T. Looney 		icp->icmp_type = ICMP_ECHOREPLY;
589df8bae1dSRodney W. Grimes 		goto reflect;
590df8bae1dSRodney W. Grimes 
591df8bae1dSRodney W. Grimes 	case ICMP_TSTAMP:
59200cb6befSMark Johnston 		if (V_icmptstamprepl == 0)
59300cb6befSMark Johnston 			break;
5948b615593SMarko Zec 		if (!V_icmpbmcastecho
595d311884fSDavid Greenman 		    && (m->m_flags & (M_MCAST | M_BCAST)) != 0) {
596e27b0c87SRobert Watson 			ICMPSTAT_INC(icps_bmcasttstamp);
597fe0fb8abSGarrett Wollman 			break;
598fe0fb8abSGarrett Wollman 		}
599df8bae1dSRodney W. Grimes 		if (icmplen < ICMP_TSLEN) {
600e27b0c87SRobert Watson 			ICMPSTAT_INC(icps_badlen);
601df8bae1dSRodney W. Grimes 			break;
602df8bae1dSRodney W. Grimes 		}
603382a6bbcSJonathan T. Looney 		if (badport_bandlim(BANDLIM_ICMP_TSTAMP) < 0)
604382a6bbcSJonathan T. Looney 			goto freeit;
605df8bae1dSRodney W. Grimes 		icp->icmp_type = ICMP_TSTAMPREPLY;
606df8bae1dSRodney W. Grimes 		icp->icmp_rtime = iptime();
607df8bae1dSRodney W. Grimes 		icp->icmp_ttime = icp->icmp_rtime;	/* bogus, do later! */
608df8bae1dSRodney W. Grimes 		goto reflect;
609df8bae1dSRodney W. Grimes 
610df8bae1dSRodney W. Grimes 	case ICMP_MASKREQ:
6118b615593SMarko Zec 		if (V_icmpmaskrepl == 0)
612df8bae1dSRodney W. Grimes 			break;
613df8bae1dSRodney W. Grimes 		/*
614df8bae1dSRodney W. Grimes 		 * We are not able to respond with all ones broadcast
615df8bae1dSRodney W. Grimes 		 * unless we receive it over a point-to-point interface.
616df8bae1dSRodney W. Grimes 		 */
617df8bae1dSRodney W. Grimes 		if (icmplen < ICMP_MASKLEN)
618df8bae1dSRodney W. Grimes 			break;
619df8bae1dSRodney W. Grimes 		switch (ip->ip_dst.s_addr) {
620df8bae1dSRodney W. Grimes 
621df8bae1dSRodney W. Grimes 		case INADDR_BROADCAST:
622df8bae1dSRodney W. Grimes 		case INADDR_ANY:
623df8bae1dSRodney W. Grimes 			icmpdst.sin_addr = ip->ip_src;
624df8bae1dSRodney W. Grimes 			break;
625df8bae1dSRodney W. Grimes 
626df8bae1dSRodney W. Grimes 		default:
627df8bae1dSRodney W. Grimes 			icmpdst.sin_addr = ip->ip_dst;
628df8bae1dSRodney W. Grimes 		}
629df8bae1dSRodney W. Grimes 		ia = (struct in_ifaddr *)ifaof_ifpforaddr(
630df8bae1dSRodney W. Grimes 			    (struct sockaddr *)&icmpdst, m->m_pkthdr.rcvif);
6318c0fec80SRobert Watson 		if (ia == NULL)
632df8bae1dSRodney W. Grimes 			break;
6334f6c66ccSMatt Macy 		if (ia->ia_ifp == NULL)
6347e6f7714SPoul-Henning Kamp 			break;
635df8bae1dSRodney W. Grimes 		icp->icmp_type = ICMP_MASKREPLY;
6368b615593SMarko Zec 		if (V_icmpmaskfake == 0)
637df8bae1dSRodney W. Grimes 			icp->icmp_mask = ia->ia_sockmask.sin_addr.s_addr;
63857842a38SMatthew N. Dodd 		else
6398b615593SMarko Zec 			icp->icmp_mask = V_icmpmaskfake;
640df8bae1dSRodney W. Grimes 		if (ip->ip_src.s_addr == 0) {
641df8bae1dSRodney W. Grimes 			if (ia->ia_ifp->if_flags & IFF_BROADCAST)
642df8bae1dSRodney W. Grimes 			    ip->ip_src = satosin(&ia->ia_broadaddr)->sin_addr;
643df8bae1dSRodney W. Grimes 			else if (ia->ia_ifp->if_flags & IFF_POINTOPOINT)
644df8bae1dSRodney W. Grimes 			    ip->ip_src = satosin(&ia->ia_dstaddr)->sin_addr;
645df8bae1dSRodney W. Grimes 		}
646df8bae1dSRodney W. Grimes reflect:
647e27b0c87SRobert Watson 		ICMPSTAT_INC(icps_reflect);
648e27b0c87SRobert Watson 		ICMPSTAT_INC(icps_outhist[icp->icmp_type]);
649df8bae1dSRodney W. Grimes 		icmp_reflect(m);
6508f5a8818SKevin Lo 		return (IPPROTO_DONE);
651df8bae1dSRodney W. Grimes 
652df8bae1dSRodney W. Grimes 	case ICMP_REDIRECT:
6538b615593SMarko Zec 		if (V_log_redirect) {
65418d3153eSDag-Erling Smørgrav 			u_long src, dst, gw;
65518d3153eSDag-Erling Smørgrav 
65618d3153eSDag-Erling Smørgrav 			src = ntohl(ip->ip_src.s_addr);
65718d3153eSDag-Erling Smørgrav 			dst = ntohl(icp->icmp_ip.ip_dst.s_addr);
65818d3153eSDag-Erling Smørgrav 			gw = ntohl(icp->icmp_gwaddr.s_addr);
65918d3153eSDag-Erling Smørgrav 			printf("icmp redirect from %d.%d.%d.%d: "
66018d3153eSDag-Erling Smørgrav 			       "%d.%d.%d.%d => %d.%d.%d.%d\n",
66118d3153eSDag-Erling Smørgrav 			       (int)(src >> 24), (int)((src >> 16) & 0xff),
66218d3153eSDag-Erling Smørgrav 			       (int)((src >> 8) & 0xff), (int)(src & 0xff),
66318d3153eSDag-Erling Smørgrav 			       (int)(dst >> 24), (int)((dst >> 16) & 0xff),
66418d3153eSDag-Erling Smørgrav 			       (int)((dst >> 8) & 0xff), (int)(dst & 0xff),
66518d3153eSDag-Erling Smørgrav 			       (int)(gw >> 24), (int)((gw >> 16) & 0xff),
66618d3153eSDag-Erling Smørgrav 			       (int)((gw >> 8) & 0xff), (int)(gw & 0xff));
66718d3153eSDag-Erling Smørgrav 		}
66887c3bd27SAndre Oppermann 		/*
66987c3bd27SAndre Oppermann 		 * RFC1812 says we must ignore ICMP redirects if we
67087c3bd27SAndre Oppermann 		 * are acting as router.
67187c3bd27SAndre Oppermann 		 */
6728b615593SMarko Zec 		if (V_drop_redirect || V_ipforwarding)
67318d3153eSDag-Erling Smørgrav 			break;
674df8bae1dSRodney W. Grimes 		if (code > 3)
675df8bae1dSRodney W. Grimes 			goto badcode;
676df8bae1dSRodney W. Grimes 		if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) ||
67753be11f6SPoul-Henning Kamp 		    icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) {
678e27b0c87SRobert Watson 			ICMPSTAT_INC(icps_badlen);
679df8bae1dSRodney W. Grimes 			break;
680df8bae1dSRodney W. Grimes 		}
681df8bae1dSRodney W. Grimes 		/*
682df8bae1dSRodney W. Grimes 		 * Short circuit routing redirects to force
683df8bae1dSRodney W. Grimes 		 * immediate change in the kernel's routing
684df8bae1dSRodney W. Grimes 		 * tables.  The message is also handed to anyone
685df8bae1dSRodney W. Grimes 		 * listening on a raw socket (e.g. the routing
686df8bae1dSRodney W. Grimes 		 * daemon for use in updating its tables).
687df8bae1dSRodney W. Grimes 		 */
688df8bae1dSRodney W. Grimes 		icmpgw.sin_addr = ip->ip_src;
689df8bae1dSRodney W. Grimes 		icmpdst.sin_addr = icp->icmp_gwaddr;
690df8bae1dSRodney W. Grimes #ifdef	ICMPPRINTFS
6912b758395SGarrett Wollman 		if (icmpprintfs) {
6928144690aSEric van Gyzen 			char dstbuf[INET_ADDRSTRLEN];
6938144690aSEric van Gyzen 			char gwbuf[INET_ADDRSTRLEN];
6942b758395SGarrett Wollman 
6952b758395SGarrett Wollman 			printf("redirect dst %s to %s\n",
6968144690aSEric van Gyzen 			       inet_ntoa_r(icp->icmp_ip.ip_dst, dstbuf),
6978144690aSEric van Gyzen 			       inet_ntoa_r(icp->icmp_gwaddr, gwbuf));
6982b758395SGarrett Wollman 		}
699df8bae1dSRodney W. Grimes #endif
700df8bae1dSRodney W. Grimes 		icmpsrc.sin_addr = icp->icmp_ip.ip_dst;
70134a5582cSAlexander V. Chernikov 
70234a5582cSAlexander V. Chernikov 		/*
70334a5582cSAlexander V. Chernikov 		 * RFC 1122 says network (code 0,2) redirects SHOULD
70434a5582cSAlexander V. Chernikov 		 * be treated identically to the host redirects.
70534a5582cSAlexander V. Chernikov 		 * Given that, ignore network masks.
70634a5582cSAlexander V. Chernikov 		 */
70734a5582cSAlexander V. Chernikov 
70834a5582cSAlexander V. Chernikov 		/*
70934a5582cSAlexander V. Chernikov 		 * Variable values:
71034a5582cSAlexander V. Chernikov 		 * icmpsrc: route destination
71134a5582cSAlexander V. Chernikov 		 * icmpdst: route gateway
71234a5582cSAlexander V. Chernikov 		 * icmpgw: message source
71334a5582cSAlexander V. Chernikov 		 */
71434a5582cSAlexander V. Chernikov 
71534a5582cSAlexander V. Chernikov 		if (icmp_verify_redirect_gateway(&icmpgw, &icmpsrc, &icmpdst,
71634a5582cSAlexander V. Chernikov 		    M_GETFIB(m)) != 0) {
71734a5582cSAlexander V. Chernikov 			/* TODO: increment bad redirects here */
71834a5582cSAlexander V. Chernikov 			break;
71934a5582cSAlexander V. Chernikov 		}
72034a5582cSAlexander V. Chernikov 
7218b07e49aSJulian Elischer 		for ( fibnum = 0; fibnum < rt_numfibs; fibnum++) {
72234a5582cSAlexander V. Chernikov 			rib_add_redirect(fibnum, (struct sockaddr *)&icmpsrc,
723df8bae1dSRodney W. Grimes 			    (struct sockaddr *)&icmpdst,
72434a5582cSAlexander V. Chernikov 			    (struct sockaddr *)&icmpgw, m->m_pkthdr.rcvif,
72534a5582cSAlexander V. Chernikov 			    RTF_GATEWAY, V_redirtimeout);
7268b07e49aSJulian Elischer 		}
727df8bae1dSRodney W. Grimes 		pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&icmpsrc);
728df8bae1dSRodney W. Grimes 		break;
729df8bae1dSRodney W. Grimes 
730df8bae1dSRodney W. Grimes 	/*
731df8bae1dSRodney W. Grimes 	 * No kernel processing for the following;
732df8bae1dSRodney W. Grimes 	 * just fall through to send to raw listener.
733df8bae1dSRodney W. Grimes 	 */
734df8bae1dSRodney W. Grimes 	case ICMP_ECHOREPLY:
735df8bae1dSRodney W. Grimes 	case ICMP_ROUTERADVERT:
736df8bae1dSRodney W. Grimes 	case ICMP_ROUTERSOLICIT:
737df8bae1dSRodney W. Grimes 	case ICMP_TSTAMPREPLY:
738df8bae1dSRodney W. Grimes 	case ICMP_IREQREPLY:
739df8bae1dSRodney W. Grimes 	case ICMP_MASKREPLY:
740d1f79a3bSAlexander V. Chernikov 	case ICMP_SOURCEQUENCH:
741df8bae1dSRodney W. Grimes 	default:
742df8bae1dSRodney W. Grimes 		break;
743df8bae1dSRodney W. Grimes 	}
744df8bae1dSRodney W. Grimes 
745df8bae1dSRodney W. Grimes raw:
7468f5a8818SKevin Lo 	*mp = m;
7478f5a8818SKevin Lo 	rip_input(mp, offp, proto);
7488f5a8818SKevin Lo 	return (IPPROTO_DONE);
749df8bae1dSRodney W. Grimes 
750df8bae1dSRodney W. Grimes freeit:
751df8bae1dSRodney W. Grimes 	m_freem(m);
7528f5a8818SKevin Lo 	return (IPPROTO_DONE);
753df8bae1dSRodney W. Grimes }
754df8bae1dSRodney W. Grimes 
755df8bae1dSRodney W. Grimes /*
756df8bae1dSRodney W. Grimes  * Reflect the ip packet back to the source
757df8bae1dSRodney W. Grimes  */
7580312fbe9SPoul-Henning Kamp static void
759f2565d68SRobert Watson icmp_reflect(struct mbuf *m)
760df8bae1dSRodney W. Grimes {
761cc0a3c8cSAndrey V. Elsukov 	struct rm_priotracker in_ifa_tracker;
762ca925d9cSJonathan Lemon 	struct ip *ip = mtod(m, struct ip *);
763ca925d9cSJonathan Lemon 	struct ifaddr *ifa;
76433c4f96dSRobert Watson 	struct ifnet *ifp;
765ca925d9cSJonathan Lemon 	struct in_ifaddr *ia;
766df8bae1dSRodney W. Grimes 	struct in_addr t;
7679977be4aSAlexander V. Chernikov 	struct nhop4_extended nh_ext;
76899d628d5SPedro F. Giffuni 	struct mbuf *opts = NULL;
76953be11f6SPoul-Henning Kamp 	int optlen = (ip->ip_hl << 2) - sizeof(struct ip);
770df8bae1dSRodney W. Grimes 
771b8a6e03fSGleb Smirnoff 	NET_EPOCH_ASSERT();
772b8a6e03fSGleb Smirnoff 
7736b9ff6b7SGeorge V. Neville-Neil 	if (IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
7746b9ff6b7SGeorge V. Neville-Neil 	    IN_EXPERIMENTAL(ntohl(ip->ip_src.s_addr)) ||
7756b9ff6b7SGeorge V. Neville-Neil 	    IN_ZERONET(ntohl(ip->ip_src.s_addr)) ) {
776df8bae1dSRodney W. Grimes 		m_freem(m);	/* Bad return address */
777e27b0c87SRobert Watson 		ICMPSTAT_INC(icps_badaddr);
778df8bae1dSRodney W. Grimes 		goto done;	/* Ip_output() will check for broadcast */
779df8bae1dSRodney W. Grimes 	}
7806b9ff6b7SGeorge V. Neville-Neil 
781df8bae1dSRodney W. Grimes 	t = ip->ip_dst;
782df8bae1dSRodney W. Grimes 	ip->ip_dst = ip->ip_src;
7831488eac8SAndre Oppermann 
784df8bae1dSRodney W. Grimes 	/*
7851488eac8SAndre Oppermann 	 * Source selection for ICMP replies:
7861488eac8SAndre Oppermann 	 *
7871488eac8SAndre Oppermann 	 * If the incoming packet was addressed directly to one of our
7881488eac8SAndre Oppermann 	 * own addresses, use dst as the src for the reply.
789df8bae1dSRodney W. Grimes 	 */
790cc0a3c8cSAndrey V. Elsukov 	IN_IFADDR_RLOCK(&in_ifa_tracker);
79133c4f96dSRobert Watson 	LIST_FOREACH(ia, INADDR_HASH(t.s_addr), ia_hash) {
79233c4f96dSRobert Watson 		if (t.s_addr == IA_SIN(ia)->sin_addr.s_addr) {
79333c4f96dSRobert Watson 			t = IA_SIN(ia)->sin_addr;
794cc0a3c8cSAndrey V. Elsukov 			IN_IFADDR_RUNLOCK(&in_ifa_tracker);
795ca925d9cSJonathan Lemon 			goto match;
79633c4f96dSRobert Watson 		}
79733c4f96dSRobert Watson 	}
798cc0a3c8cSAndrey V. Elsukov 	IN_IFADDR_RUNLOCK(&in_ifa_tracker);
7992d9cfabaSRobert Watson 
8001488eac8SAndre Oppermann 	/*
8011488eac8SAndre Oppermann 	 * If the incoming packet was addressed to one of our broadcast
8021488eac8SAndre Oppermann 	 * addresses, use the first non-broadcast address which corresponds
8031488eac8SAndre Oppermann 	 * to the incoming interface.
8041488eac8SAndre Oppermann 	 */
80533c4f96dSRobert Watson 	ifp = m->m_pkthdr.rcvif;
80633c4f96dSRobert Watson 	if (ifp != NULL && ifp->if_flags & IFF_BROADCAST) {
807d7c5a620SMatt Macy 		CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
808ca925d9cSJonathan Lemon 			if (ifa->ifa_addr->sa_family != AF_INET)
809ca925d9cSJonathan Lemon 				continue;
810ca925d9cSJonathan Lemon 			ia = ifatoia(ifa);
811ca925d9cSJonathan Lemon 			if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
81233c4f96dSRobert Watson 			    t.s_addr) {
81333c4f96dSRobert Watson 				t = IA_SIN(ia)->sin_addr;
814ca925d9cSJonathan Lemon 				goto match;
815df8bae1dSRodney W. Grimes 			}
816ca925d9cSJonathan Lemon 		}
81733c4f96dSRobert Watson 	}
8181488eac8SAndre Oppermann 	/*
819a0866c8dSAndre Oppermann 	 * If the packet was transiting through us, use the address of
820a0866c8dSAndre Oppermann 	 * the interface the packet came through in.  If that interface
821a0866c8dSAndre Oppermann 	 * doesn't have a suitable IP address, the normal selection
822a0866c8dSAndre Oppermann 	 * criteria apply.
823a0866c8dSAndre Oppermann 	 */
82433c4f96dSRobert Watson 	if (V_icmp_rfi && ifp != NULL) {
825d7c5a620SMatt Macy 		CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
826a0866c8dSAndre Oppermann 			if (ifa->ifa_addr->sa_family != AF_INET)
827a0866c8dSAndre Oppermann 				continue;
828a0866c8dSAndre Oppermann 			ia = ifatoia(ifa);
82933c4f96dSRobert Watson 			t = IA_SIN(ia)->sin_addr;
830a0866c8dSAndre Oppermann 			goto match;
831a0866c8dSAndre Oppermann 		}
832a0866c8dSAndre Oppermann 	}
833a0866c8dSAndre Oppermann 	/*
834b74d89bbSAndre Oppermann 	 * If the incoming packet was not addressed directly to us, use
835b74d89bbSAndre Oppermann 	 * designated interface for icmp replies specified by sysctl
836b74d89bbSAndre Oppermann 	 * net.inet.icmp.reply_src (default not set). Otherwise continue
837b74d89bbSAndre Oppermann 	 * with normal source selection.
838b74d89bbSAndre Oppermann 	 */
83933c4f96dSRobert Watson 	if (V_reply_src[0] != '\0' && (ifp = ifunit(V_reply_src))) {
840d7c5a620SMatt Macy 		CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
841b74d89bbSAndre Oppermann 			if (ifa->ifa_addr->sa_family != AF_INET)
842b74d89bbSAndre Oppermann 				continue;
843b74d89bbSAndre Oppermann 			ia = ifatoia(ifa);
84433c4f96dSRobert Watson 			t = IA_SIN(ia)->sin_addr;
845b74d89bbSAndre Oppermann 			goto match;
846b74d89bbSAndre Oppermann 		}
847b74d89bbSAndre Oppermann 	}
848b74d89bbSAndre Oppermann 	/*
8491488eac8SAndre Oppermann 	 * If the packet was transiting through us, use the address of
8501488eac8SAndre Oppermann 	 * the interface that is the closest to the packet source.
8511488eac8SAndre Oppermann 	 * When we don't have a route back to the packet source, stop here
8521488eac8SAndre Oppermann 	 * and drop the packet.
8531488eac8SAndre Oppermann 	 */
8549977be4aSAlexander V. Chernikov 	if (fib4_lookup_nh_ext(M_GETFIB(m), ip->ip_dst, 0, 0, &nh_ext) != 0) {
8550d4bef5dSDima Dorfman 		m_freem(m);
856e27b0c87SRobert Watson 		ICMPSTAT_INC(icps_noroute);
8570d4bef5dSDima Dorfman 		goto done;
8580d4bef5dSDima Dorfman 	}
8599977be4aSAlexander V. Chernikov 	t = nh_ext.nh_src;
860ca925d9cSJonathan Lemon match:
861baee0c3eSRobert Watson #ifdef MAC
862a13e21f7SRobert Watson 	mac_netinet_icmp_replyinplace(m);
863baee0c3eSRobert Watson #endif
864df8bae1dSRodney W. Grimes 	ip->ip_src = t;
865603724d3SBjoern A. Zeeb 	ip->ip_ttl = V_ip_defttl;
866df8bae1dSRodney W. Grimes 
867df8bae1dSRodney W. Grimes 	if (optlen > 0) {
8683e85b721SEd Maste 		u_char *cp;
869df8bae1dSRodney W. Grimes 		int opt, cnt;
870df8bae1dSRodney W. Grimes 		u_int len;
871df8bae1dSRodney W. Grimes 
872df8bae1dSRodney W. Grimes 		/*
873df8bae1dSRodney W. Grimes 		 * Retrieve any source routing from the incoming packet;
874df8bae1dSRodney W. Grimes 		 * add on any record-route or timestamp options.
875df8bae1dSRodney W. Grimes 		 */
876df8bae1dSRodney W. Grimes 		cp = (u_char *) (ip + 1);
87799d628d5SPedro F. Giffuni 		if ((opts = ip_srcroute(m)) == NULL &&
878eb1b1807SGleb Smirnoff 		    (opts = m_gethdr(M_NOWAIT, MT_DATA))) {
879df8bae1dSRodney W. Grimes 			opts->m_len = sizeof(struct in_addr);
880df8bae1dSRodney W. Grimes 			mtod(opts, struct in_addr *)->s_addr = 0;
881df8bae1dSRodney W. Grimes 		}
882df8bae1dSRodney W. Grimes 		if (opts) {
883df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS
884df8bae1dSRodney W. Grimes 		    if (icmpprintfs)
885df8bae1dSRodney W. Grimes 			    printf("icmp_reflect optlen %d rt %d => ",
886df8bae1dSRodney W. Grimes 				optlen, opts->m_len);
887df8bae1dSRodney W. Grimes #endif
888df8bae1dSRodney W. Grimes 		    for (cnt = optlen; cnt > 0; cnt -= len, cp += len) {
889df8bae1dSRodney W. Grimes 			    opt = cp[IPOPT_OPTVAL];
890df8bae1dSRodney W. Grimes 			    if (opt == IPOPT_EOL)
891df8bae1dSRodney W. Grimes 				    break;
892df8bae1dSRodney W. Grimes 			    if (opt == IPOPT_NOP)
893df8bae1dSRodney W. Grimes 				    len = 1;
894df8bae1dSRodney W. Grimes 			    else {
895707d00a3SJonathan Lemon 				    if (cnt < IPOPT_OLEN + sizeof(*cp))
896707d00a3SJonathan Lemon 					    break;
897df8bae1dSRodney W. Grimes 				    len = cp[IPOPT_OLEN];
898707d00a3SJonathan Lemon 				    if (len < IPOPT_OLEN + sizeof(*cp) ||
899707d00a3SJonathan Lemon 				        len > cnt)
900df8bae1dSRodney W. Grimes 					    break;
901df8bae1dSRodney W. Grimes 			    }
902df8bae1dSRodney W. Grimes 			    /*
903df8bae1dSRodney W. Grimes 			     * Should check for overflow, but it "can't happen"
904df8bae1dSRodney W. Grimes 			     */
905df8bae1dSRodney W. Grimes 			    if (opt == IPOPT_RR || opt == IPOPT_TS ||
906df8bae1dSRodney W. Grimes 				opt == IPOPT_SECURITY) {
907df8bae1dSRodney W. Grimes 				    bcopy((caddr_t)cp,
908df8bae1dSRodney W. Grimes 					mtod(opts, caddr_t) + opts->m_len, len);
909df8bae1dSRodney W. Grimes 				    opts->m_len += len;
910df8bae1dSRodney W. Grimes 			    }
911df8bae1dSRodney W. Grimes 		    }
912df8bae1dSRodney W. Grimes 		    /* Terminate & pad, if necessary */
913623ae52eSPoul-Henning Kamp 		    cnt = opts->m_len % 4;
914623ae52eSPoul-Henning Kamp 		    if (cnt) {
915df8bae1dSRodney W. Grimes 			    for (; cnt < 4; cnt++) {
916df8bae1dSRodney W. Grimes 				    *(mtod(opts, caddr_t) + opts->m_len) =
917df8bae1dSRodney W. Grimes 					IPOPT_EOL;
918df8bae1dSRodney W. Grimes 				    opts->m_len++;
919df8bae1dSRodney W. Grimes 			    }
920df8bae1dSRodney W. Grimes 		    }
921df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS
922df8bae1dSRodney W. Grimes 		    if (icmpprintfs)
923df8bae1dSRodney W. Grimes 			    printf("%d\n", opts->m_len);
924df8bae1dSRodney W. Grimes #endif
925df8bae1dSRodney W. Grimes 		}
9269e2a372fSGleb Smirnoff 		ip_stripoptions(m);
927df8bae1dSRodney W. Grimes 	}
9289c855a36SSam Leffler 	m_tag_delete_nonpersistent(m);
929df8bae1dSRodney W. Grimes 	m->m_flags &= ~(M_BCAST|M_MCAST);
93002c1c707SAndre Oppermann 	icmp_send(m, opts);
931df8bae1dSRodney W. Grimes done:
932df8bae1dSRodney W. Grimes 	if (opts)
933df8bae1dSRodney W. Grimes 		(void)m_free(opts);
934df8bae1dSRodney W. Grimes }
935df8bae1dSRodney W. Grimes 
936df8bae1dSRodney W. Grimes /*
93734a5582cSAlexander V. Chernikov  * Verifies if redirect message is valid, according to RFC 1122
93834a5582cSAlexander V. Chernikov  *
93934a5582cSAlexander V. Chernikov  * @src: sockaddr with address of redirect originator
94034a5582cSAlexander V. Chernikov  * @dst: sockaddr with destination in question
94134a5582cSAlexander V. Chernikov  * @gateway: new proposed gateway
94234a5582cSAlexander V. Chernikov  *
94334a5582cSAlexander V. Chernikov  * Returns 0 on success.
94434a5582cSAlexander V. Chernikov  */
94534a5582cSAlexander V. Chernikov static int
94634a5582cSAlexander V. Chernikov icmp_verify_redirect_gateway(struct sockaddr_in *src, struct sockaddr_in *dst,
94734a5582cSAlexander V. Chernikov     struct sockaddr_in *gateway, u_int fibnum)
94834a5582cSAlexander V. Chernikov {
949*9ac7c6cfSAlexander V. Chernikov 	struct nhop_object *nh;
95034a5582cSAlexander V. Chernikov 	struct ifaddr *ifa;
95134a5582cSAlexander V. Chernikov 
95234a5582cSAlexander V. Chernikov 	NET_EPOCH_ASSERT();
95334a5582cSAlexander V. Chernikov 
95434a5582cSAlexander V. Chernikov 	/* Verify the gateway is directly reachable. */
95534a5582cSAlexander V. Chernikov 	if ((ifa = ifa_ifwithnet((struct sockaddr *)gateway, 0, fibnum))==NULL)
95634a5582cSAlexander V. Chernikov 		return (ENETUNREACH);
95734a5582cSAlexander V. Chernikov 
95834a5582cSAlexander V. Chernikov 	/* TODO: fib-aware. */
95934a5582cSAlexander V. Chernikov 	if (ifa_ifwithaddr_check((struct sockaddr *)gateway))
96034a5582cSAlexander V. Chernikov 		return (EHOSTUNREACH);
96134a5582cSAlexander V. Chernikov 
962*9ac7c6cfSAlexander V. Chernikov 	nh = fib4_lookup(fibnum, dst->sin_addr, 0, NHR_NONE, 0);
963*9ac7c6cfSAlexander V. Chernikov 	if (nh == NULL)
96434a5582cSAlexander V. Chernikov 		return (EINVAL);
96534a5582cSAlexander V. Chernikov 
96634a5582cSAlexander V. Chernikov 	/*
96734a5582cSAlexander V. Chernikov 	 * If the redirect isn't from our current router for this dst,
96834a5582cSAlexander V. Chernikov 	 * it's either old or wrong.  If it redirects us to ourselves,
96934a5582cSAlexander V. Chernikov 	 * we have a routing loop, perhaps as a result of an interface
97034a5582cSAlexander V. Chernikov 	 * going down recently.
97134a5582cSAlexander V. Chernikov 	 */
972*9ac7c6cfSAlexander V. Chernikov 	if (!sa_equal((struct sockaddr *)src, &nh->gw_sa))
97334a5582cSAlexander V. Chernikov 		return (EINVAL);
974*9ac7c6cfSAlexander V. Chernikov 	if (nh->nh_ifa != ifa && ifa->ifa_addr->sa_family != AF_LINK)
97534a5582cSAlexander V. Chernikov 		return (EINVAL);
97634a5582cSAlexander V. Chernikov 
97734a5582cSAlexander V. Chernikov 	/* If host route already exists, ignore redirect. */
978*9ac7c6cfSAlexander V. Chernikov 	if (nh->nh_flags & NHF_HOST)
97934a5582cSAlexander V. Chernikov 		return (EEXIST);
98034a5582cSAlexander V. Chernikov 
98134a5582cSAlexander V. Chernikov 	/* If the prefix is directly reachable, ignore redirect. */
982*9ac7c6cfSAlexander V. Chernikov 	if (!(nh->nh_flags & NHF_GATEWAY))
98334a5582cSAlexander V. Chernikov 		return (EEXIST);
98434a5582cSAlexander V. Chernikov 
98534a5582cSAlexander V. Chernikov 	return (0);
98634a5582cSAlexander V. Chernikov }
98734a5582cSAlexander V. Chernikov 
98834a5582cSAlexander V. Chernikov 
98934a5582cSAlexander V. Chernikov /*
990df8bae1dSRodney W. Grimes  * Send an icmp packet back to the ip level,
991df8bae1dSRodney W. Grimes  * after supplying a checksum.
992df8bae1dSRodney W. Grimes  */
9930312fbe9SPoul-Henning Kamp static void
994f2565d68SRobert Watson icmp_send(struct mbuf *m, struct mbuf *opts)
995df8bae1dSRodney W. Grimes {
9963e85b721SEd Maste 	struct ip *ip = mtod(m, struct ip *);
9973e85b721SEd Maste 	int hlen;
9983e85b721SEd Maste 	struct icmp *icp;
999df8bae1dSRodney W. Grimes 
100053be11f6SPoul-Henning Kamp 	hlen = ip->ip_hl << 2;
1001df8bae1dSRodney W. Grimes 	m->m_data += hlen;
1002df8bae1dSRodney W. Grimes 	m->m_len -= hlen;
1003df8bae1dSRodney W. Grimes 	icp = mtod(m, struct icmp *);
1004df8bae1dSRodney W. Grimes 	icp->icmp_cksum = 0;
10058f134647SGleb Smirnoff 	icp->icmp_cksum = in_cksum(m, ntohs(ip->ip_len) - hlen);
1006df8bae1dSRodney W. Grimes 	m->m_data -= hlen;
1007df8bae1dSRodney W. Grimes 	m->m_len += hlen;
100894446a2eSArchie Cobbs 	m->m_pkthdr.rcvif = (struct ifnet *)0;
1009df8bae1dSRodney W. Grimes #ifdef ICMPPRINTFS
10102b758395SGarrett Wollman 	if (icmpprintfs) {
10118144690aSEric van Gyzen 		char dstbuf[INET_ADDRSTRLEN];
10128144690aSEric van Gyzen 		char srcbuf[INET_ADDRSTRLEN];
10138144690aSEric van Gyzen 
10142b758395SGarrett Wollman 		printf("icmp_send dst %s src %s\n",
10158144690aSEric van Gyzen 		    inet_ntoa_r(ip->ip_dst, dstbuf),
10168144690aSEric van Gyzen 		    inet_ntoa_r(ip->ip_src, srcbuf));
10172b758395SGarrett Wollman 	}
1018df8bae1dSRodney W. Grimes #endif
101902c1c707SAndre Oppermann 	(void) ip_output(m, opts, NULL, 0, NULL, NULL);
1020df8bae1dSRodney W. Grimes }
1021df8bae1dSRodney W. Grimes 
1022d685b6eeSLuigi Rizzo /*
102361f26caeSWarner Losh  * Return milliseconds since 00:00 UTC in network format.
1024d685b6eeSLuigi Rizzo  */
1025d685b6eeSLuigi Rizzo uint32_t
1026f2565d68SRobert Watson iptime(void)
1027df8bae1dSRodney W. Grimes {
1028df8bae1dSRodney W. Grimes 	struct timeval atv;
1029df8bae1dSRodney W. Grimes 	u_long t;
1030df8bae1dSRodney W. Grimes 
103116cd6db0SBill Fumerola 	getmicrotime(&atv);
1032df8bae1dSRodney W. Grimes 	t = (atv.tv_sec % (24*60*60)) * 1000 + atv.tv_usec / 1000;
1033df8bae1dSRodney W. Grimes 	return (htonl(t));
1034df8bae1dSRodney W. Grimes }
1035df8bae1dSRodney W. Grimes 
10365cbf3e08SGarrett Wollman /*
10375cbf3e08SGarrett Wollman  * Return the next larger or smaller MTU plateau (table from RFC 1191)
10385cbf3e08SGarrett Wollman  * given current value MTU.  If DIR is less than zero, a larger plateau
10395cbf3e08SGarrett Wollman  * is returned; otherwise, a smaller value is returned.
10405cbf3e08SGarrett Wollman  */
10411aedbd9cSAndre Oppermann int
1042f2565d68SRobert Watson ip_next_mtu(int mtu, int dir)
10435cbf3e08SGarrett Wollman {
10445cbf3e08SGarrett Wollman 	static int mtutab[] = {
10454c037f8dSAndre Oppermann 		65535, 32000, 17914, 8166, 4352, 2002, 1492, 1280, 1006, 508,
10464c037f8dSAndre Oppermann 		296, 68, 0
10475cbf3e08SGarrett Wollman 	};
104806003a1eSAndre Oppermann 	int i, size;
10495cbf3e08SGarrett Wollman 
105006003a1eSAndre Oppermann 	size = (sizeof mtutab) / (sizeof mtutab[0]);
105106003a1eSAndre Oppermann 	if (dir >= 0) {
10521c0b0f52SGleb Smirnoff 		for (i = 0; i < size; i++)
105306003a1eSAndre Oppermann 			if (mtu > mtutab[i])
10545cbf3e08SGarrett Wollman 				return mtutab[i];
10555cbf3e08SGarrett Wollman 	} else {
105606003a1eSAndre Oppermann 		for (i = size - 1; i >= 0; i--)
105706003a1eSAndre Oppermann 			if (mtu < mtutab[i])
105806003a1eSAndre Oppermann 				return mtutab[i];
105906003a1eSAndre Oppermann 		if (mtu == mtutab[0])
106006003a1eSAndre Oppermann 			return mtutab[0];
10615cbf3e08SGarrett Wollman 	}
106206003a1eSAndre Oppermann 	return 0;
10635cbf3e08SGarrett Wollman }
1064b8e463e6SBjoern A. Zeeb #endif /* INET */
106551508de1SMatthew Dillon 
106651508de1SMatthew Dillon 
106751508de1SMatthew Dillon /*
106851508de1SMatthew Dillon  * badport_bandlim() - check for ICMP bandwidth limit
106951508de1SMatthew Dillon  *
107051508de1SMatthew Dillon  *	Return 0 if it is ok to send an ICMP error response, -1 if we have
107151508de1SMatthew Dillon  *	hit our bandwidth limit and it is not ok.
107251508de1SMatthew Dillon  *
107351508de1SMatthew Dillon  *	If icmplim is <= 0, the feature is disabled and 0 is returned.
107451508de1SMatthew Dillon  *
107551508de1SMatthew Dillon  *	For now we separate the TCP and UDP subsystems w/ different 'which'
107651508de1SMatthew Dillon  *	values.  We may eventually remove this separation (and simplify the
107751508de1SMatthew Dillon  *	code further).
107851508de1SMatthew Dillon  *
107951508de1SMatthew Dillon  *	Note that the printing of the error message is delayed so we can
108051508de1SMatthew Dillon  *	properly print the icmp error rate that the system was trying to do
108151508de1SMatthew Dillon  *	(i.e. 22000/100 pps, etc...).  This can cause long delays in printing
108251508de1SMatthew Dillon  *	the 'final' error, but it doesn't make sense to solve the printing
108351508de1SMatthew Dillon  *	delay with more complex code.
108451508de1SMatthew Dillon  */
10853cbee8caSGleb Smirnoff struct icmp_rate {
10863cbee8caSGleb Smirnoff 	const char *descr;
10873cbee8caSGleb Smirnoff 	struct counter_rate cr;
10883cbee8caSGleb Smirnoff };
10895f901c92SAndrew Turner VNET_DEFINE_STATIC(struct icmp_rate, icmp_rates[BANDLIM_MAX]) = {
109000f21882SSam Leffler 	{ "icmp unreach response" },
109100f21882SSam Leffler 	{ "icmp ping response" },
109200f21882SSam Leffler 	{ "icmp tstamp response" },
109300f21882SSam Leffler 	{ "closed port RST response" },
109408af97b7SRobert Watson 	{ "open port RST response" },
10955ad9e57bSMichael Tuexen 	{ "icmp6 unreach response" },
10965ad9e57bSMichael Tuexen 	{ "sctp ootb response" }
109709f81a46SBosko Milekic };
10983cbee8caSGleb Smirnoff #define	V_icmp_rates	VNET(icmp_rates)
109951508de1SMatthew Dillon 
11003cbee8caSGleb Smirnoff static void
11013cbee8caSGleb Smirnoff icmp_bandlimit_init(void)
11023cbee8caSGleb Smirnoff {
110351508de1SMatthew Dillon 
11043cbee8caSGleb Smirnoff 	for (int i = 0; i < BANDLIM_MAX; i++) {
11053cbee8caSGleb Smirnoff 		V_icmp_rates[i].cr.cr_rate = counter_u64_alloc(M_WAITOK);
11063cbee8caSGleb Smirnoff 		V_icmp_rates[i].cr.cr_ticks = ticks;
110751508de1SMatthew Dillon 	}
11083cbee8caSGleb Smirnoff }
11093cbee8caSGleb Smirnoff VNET_SYSINIT(icmp_bandlimit, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY,
11103cbee8caSGleb Smirnoff     icmp_bandlimit_init, NULL);
11113cbee8caSGleb Smirnoff 
11123cbee8caSGleb Smirnoff static void
11133cbee8caSGleb Smirnoff icmp_bandlimit_uninit(void)
11143cbee8caSGleb Smirnoff {
11153cbee8caSGleb Smirnoff 
11163cbee8caSGleb Smirnoff 	for (int i = 0; i < BANDLIM_MAX; i++)
11173cbee8caSGleb Smirnoff 		counter_u64_free(V_icmp_rates[i].cr.cr_rate);
11183cbee8caSGleb Smirnoff }
11193cbee8caSGleb Smirnoff VNET_SYSUNINIT(icmp_bandlimit, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD,
11203cbee8caSGleb Smirnoff     icmp_bandlimit_uninit, NULL);
11213cbee8caSGleb Smirnoff 
11223cbee8caSGleb Smirnoff int
11233cbee8caSGleb Smirnoff badport_bandlim(int which)
11243cbee8caSGleb Smirnoff {
11253cbee8caSGleb Smirnoff 	int64_t pps;
11263cbee8caSGleb Smirnoff 
11273cbee8caSGleb Smirnoff 	if (V_icmplim == 0 || which == BANDLIM_UNLIMITED)
11283cbee8caSGleb Smirnoff 		return (0);
11293cbee8caSGleb Smirnoff 
11303cbee8caSGleb Smirnoff 	KASSERT(which >= 0 && which < BANDLIM_MAX,
11313cbee8caSGleb Smirnoff 	    ("%s: which %d", __func__, which));
11323cbee8caSGleb Smirnoff 
11333cbee8caSGleb Smirnoff 	pps = counter_ratecheck(&V_icmp_rates[which].cr, V_icmplim);
11343cbee8caSGleb Smirnoff 	if (pps == -1)
11353cbee8caSGleb Smirnoff 		return (-1);
11363cbee8caSGleb Smirnoff 	if (pps > 0 && V_icmplim_output)
11378c70a353SGleb Smirnoff 		log(LOG_NOTICE, "Limiting %s from %jd to %d packets/sec\n",
11388c70a353SGleb Smirnoff 			V_icmp_rates[which].descr, (intmax_t )pps, V_icmplim);
11393cbee8caSGleb Smirnoff 	return (0);
114051508de1SMatthew Dillon }
1141